Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmake/CliFboss2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ add_library(fboss2_config_lib
fboss/cli/fboss2/commands/config/l2/CmdConfigL2.h
fboss/cli/fboss2/commands/config/l2/learning_mode/CmdConfigL2LearningMode.cpp
fboss/cli/fboss2/commands/config/l2/learning_mode/CmdConfigL2LearningMode.h
fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.cpp
fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h
fboss/cli/fboss2/commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.cpp
fboss/cli/fboss2/commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.h
fboss/cli/fboss2/commands/config/protocol/CmdConfigProtocol.cpp
fboss/cli/fboss2/commands/config/protocol/CmdConfigProtocol.h
fboss/cli/fboss2/commands/config/protocol/bgp/BgpConfigSession.cpp
Expand Down
1 change: 1 addition & 0 deletions cmake/CliFboss2TestConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_executable(fboss2_cmd_config_test
fboss/cli/fboss2/test/config/CmdConfigInterfaceSwitchportAccessVlanTest.cpp
fboss/cli/fboss2/test/config/CmdConfigInterfaceTest.cpp
fboss/cli/fboss2/test/config/CmdConfigL2LearningModeTest.cpp
fboss/cli/fboss2/test/config/CmdConfigPtpTransparentClockTest.cpp
fboss/cli/fboss2/test/config/CmdConfigQosBufferPoolTest.cpp
fboss/cli/fboss2/test/config/CmdConfigReloadTest.cpp
fboss/cli/fboss2/test/config/CmdConfigSessionCommitTest.cpp
Expand Down
1 change: 1 addition & 0 deletions cmake/CliFboss2TestIntegrationTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_executable(fboss2_integration_test
fboss/cli/fboss2/test/integration_test/ConfigInterfaceProfileTest.cpp
fboss/cli/fboss2/test/integration_test/ConfigL2LearningModeTest.cpp
fboss/cli/fboss2/test/integration_test/ConfigPfcTest.cpp
fboss/cli/fboss2/test/integration_test/ConfigPtpTest.cpp
fboss/cli/fboss2/test/integration_test/ConfigPortQueueConfigTest.cpp
fboss/cli/fboss2/test/integration_test/ConfigQosPolicyMapTest.cpp
fboss/cli/fboss2/test/integration_test/ConfigSessionClearTest.cpp
Expand Down
4 changes: 4 additions & 0 deletions fboss/cli/fboss2/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,8 @@ cpp_library(
"commands/config/interface/switchport/access/vlan/CmdConfigInterfaceSwitchportAccessVlan.cpp",
"commands/config/l2/CmdConfigL2.cpp",
"commands/config/l2/learning_mode/CmdConfigL2LearningMode.cpp",
"commands/config/ptp/CmdConfigPtp.cpp",
"commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.cpp",
"commands/config/protocol/CmdConfigProtocol.cpp",
"commands/config/protocol/bgp/BgpConfigSession.cpp",
"commands/config/protocol/bgp/CmdConfigProtocolBgp.cpp",
Expand Down Expand Up @@ -1128,6 +1130,8 @@ cpp_library(
"commands/config/interface/switchport/access/vlan/CmdConfigInterfaceSwitchportAccessVlan.h",
"commands/config/l2/CmdConfigL2.h",
"commands/config/l2/learning_mode/CmdConfigL2LearningMode.h",
"commands/config/ptp/CmdConfigPtp.h",
"commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.h",
"commands/config/protocol/CmdConfigProtocol.h",
"commands/config/protocol/bgp/BgpConfigSession.h",
"commands/config/protocol/bgp/CmdConfigProtocolBgp.h",
Expand Down
16 changes: 16 additions & 0 deletions fboss/cli/fboss2/CmdListConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
#include "fboss/cli/fboss2/commands/config/protocol/bgp/peer/CmdConfigProtocolBgpPeerV4OverV6Nh.h"
#include "fboss/cli/fboss2/commands/config/protocol/bgp/peer/CmdConfigProtocolBgpPeerWarningLimit.h"
#include "fboss/cli/fboss2/commands/config/protocol/bgp/peer/CmdConfigProtocolBgpPeerWarningOnly.h"
#include "fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h"
#include "fboss/cli/fboss2/commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.h"
#include "fboss/cli/fboss2/commands/config/qos/CmdConfigQos.h"
#include "fboss/cli/fboss2/commands/config/qos/buffer_pool/CmdConfigQosBufferPool.h"
#include "fboss/cli/fboss2/commands/config/qos/policy/CmdConfigQosPolicy.h"
Expand Down Expand Up @@ -170,6 +172,20 @@ const CommandTree& kConfigCommandTree() {
}},
},

{
"config",
"ptp",
"Configure PTP settings",
commandHandler<CmdConfigPtp>,
argRegistrar<CmdConfigPtpTraits>,
{{
"transparent-clock",
"Enable or disable PTP transparent clock mode",
commandHandler<CmdConfigPtpTransparentClock>,
argRegistrar<CmdConfigPtpTransparentClockTraits>,
}},
},

{
"config",
"protocol",
Expand Down
20 changes: 20 additions & 0 deletions fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#include "fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h"

#include "fboss/cli/fboss2/CmdHandler.cpp"

namespace facebook::fboss {

// Explicit template instantiation
template void CmdHandler<CmdConfigPtp, CmdConfigPtpTraits>::run();

} // namespace facebook::fboss
35 changes: 35 additions & 0 deletions fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#pragma once

#include "fboss/cli/fboss2/CmdHandler.h"

namespace facebook::fboss {

struct CmdConfigPtpTraits : public WriteCommandTraits {
using ObjectArgType = utils::NoneArgType;
using RetType = std::string;
};

class CmdConfigPtp : public CmdHandler<CmdConfigPtp, CmdConfigPtpTraits> {
public:
using ObjectArgType = CmdConfigPtpTraits::ObjectArgType;
using RetType = CmdConfigPtpTraits::RetType;

RetType queryClient(const HostInfo& /* hostInfo */) {
throw std::runtime_error(
"Incomplete command, please use 'transparent-clock' subcommand");
}

void printOutput(const RetType& /* model */) {}
};

} // namespace facebook::fboss
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#include "fboss/cli/fboss2/commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.h"

#include "fboss/cli/fboss2/CmdHandler.cpp"

#include <fmt/format.h>
#include <folly/String.h>
#include <iostream>
#include "fboss/cli/fboss2/session/ConfigSession.h"

namespace facebook::fboss {

namespace {
constexpr std::string_view kEnable = "enable";
constexpr std::string_view kDisable = "disable";
} // namespace

PtpTransparentClockArg::PtpTransparentClockArg(std::vector<std::string> v) {
if (v.empty()) {
throw std::invalid_argument(
"PTP transparent clock state is required (enable or disable)");
}
if (v.size() != 1) {
throw std::invalid_argument(
"Expected exactly one argument (enable or disable)");
}

std::string state = v[0];
folly::toLowerAscii(state);
if (state == kEnable) {
enable_ = true;
} else if (state == kDisable) {
enable_ = false;
} else {
throw std::invalid_argument(
"Invalid PTP transparent clock state '" + v[0] +
"'. Expected 'enable' or 'disable'");
}
data_.push_back(v[0]);
}

CmdConfigPtpTransparentClockTraits::RetType
CmdConfigPtpTransparentClock::queryClient(
const HostInfo& /* hostInfo */,
const ObjectArgType& state) {
auto& session = ConfigSession::getInstance();
auto& config = session.getAgentConfig();
auto& swConfig = *config.sw();

bool newValue = state.getEnable();
bool currentValue = *swConfig.switchSettings()->ptpTcEnable();

if (currentValue == newValue) {
return fmt::format(
"PTP transparent clock is already {}",
newValue ? "enabled" : "disabled");
}

swConfig.switchSettings()->ptpTcEnable() = newValue;

session.saveConfig(cli::ServiceType::AGENT, cli::ConfigActionLevel::HITLESS);

return fmt::format(
"Successfully {} PTP transparent clock",
newValue ? "enabled" : "disabled");
}

void CmdConfigPtpTransparentClock::printOutput(const RetType& output) {
std::cout << output << std::endl;
}

// Explicit template instantiation
template void CmdHandler<
CmdConfigPtpTransparentClock,
CmdConfigPtpTransparentClockTraits>::run();

} // namespace facebook::fboss
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#pragma once

#include "fboss/cli/fboss2/CmdHandler.h"
#include "fboss/cli/fboss2/commands/config/ptp/CmdConfigPtp.h"

namespace facebook::fboss {

class PtpTransparentClockArg : public utils::BaseObjectArgType<std::string> {
public:
/* implicit */ PtpTransparentClockArg( // NOLINT(google-explicit-constructor)
std::vector<std::string> v);

bool getEnable() const {
return enable_;
}

private:
bool enable_{false};
};

struct CmdConfigPtpTransparentClockTraits : public WriteCommandTraits {
using ParentCmd = CmdConfigPtp;
static void addCliArg(CLI::App& cmd, std::vector<std::string>& args) {
cmd.add_option(
"state", args, "PTP transparent clock state (enable|disable)");
}
using ObjectArgType = PtpTransparentClockArg;
using RetType = std::string;
};

class CmdConfigPtpTransparentClock : public CmdHandler<
CmdConfigPtpTransparentClock,
CmdConfigPtpTransparentClockTraits> {
public:
using ObjectArgType = CmdConfigPtpTransparentClockTraits::ObjectArgType;
using RetType = CmdConfigPtpTransparentClockTraits::RetType;

RetType queryClient(const HostInfo& hostInfo, const ObjectArgType& state);

void printOutput(const RetType& output);
};

} // namespace facebook::fboss
1 change: 1 addition & 0 deletions fboss/cli/fboss2/test/config/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cpp_unittest(
"CmdConfigInterfaceSwitchportAccessVlanTest.cpp",
"CmdConfigInterfaceTest.cpp",
"CmdConfigL2LearningModeTest.cpp",
"CmdConfigPtpTransparentClockTest.cpp",
"CmdConfigQosBufferPoolTest.cpp",
"CmdConfigReloadTest.cpp",
"CmdConfigSessionCommitTest.cpp",
Expand Down
108 changes: 108 additions & 0 deletions fboss/cli/fboss2/test/config/CmdConfigPtpTransparentClockTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#include "fboss/cli/fboss2/commands/config/ptp/transparent_clock/CmdConfigPtpTransparentClock.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "fboss/cli/fboss2/session/ConfigSession.h"
#include "fboss/cli/fboss2/test/config/CmdConfigTestBase.h"
#include "fboss/cli/fboss2/utils/PortMap.h" // NOLINT(misc-include-cleaner)

using namespace ::testing;

namespace facebook::fboss {

// Seed mirrors a production switch config where ptpTcEnable is true
class CmdConfigPtpTransparentClockTestFixture : public CmdConfigTestBase {
public:
CmdConfigPtpTransparentClockTestFixture()
: CmdConfigTestBase(
"fboss_ptp_tc_test_%%%%-%%%%-%%%%-%%%%",
R"({
"sw": {
"switchSettings": {
"ptpTcEnable": true
}
}
})") {}

protected:
const std::string cmdPrefix_ = "config ptp transparent-clock";
};

// ==============================================================================
// PtpTransparentClockArg Validation Tests
// ==============================================================================

TEST_F(CmdConfigPtpTransparentClockTestFixture, argValidation) {
// Valid values
EXPECT_TRUE(PtpTransparentClockArg({"enable"}).getEnable());
EXPECT_FALSE(PtpTransparentClockArg({"disable"}).getEnable());

// Case-insensitive
EXPECT_TRUE(PtpTransparentClockArg({"ENABLE"}).getEnable());
EXPECT_FALSE(PtpTransparentClockArg({"DISABLE"}).getEnable());
EXPECT_TRUE(PtpTransparentClockArg({"Enable"}).getEnable());

// Invalid
EXPECT_THROW(PtpTransparentClockArg({}), std::invalid_argument);
EXPECT_THROW(
PtpTransparentClockArg({"enable", "extra"}), std::invalid_argument);
EXPECT_THROW(PtpTransparentClockArg({"on"}), std::invalid_argument);
EXPECT_THROW(PtpTransparentClockArg({"true"}), std::invalid_argument);
}

// ==============================================================================
// Command Execution Tests
// ==============================================================================

TEST_F(CmdConfigPtpTransparentClockTestFixture, disableWhenEnabled) {
// Seed has ptpTcEnable=true; disable it
setupTestableConfigSession(cmdPrefix_, "disable");
CmdConfigPtpTransparentClock cmd;
HostInfo hostInfo("testhost");
PtpTransparentClockArg arg({"disable"});

auto result = cmd.queryClient(hostInfo, arg);
EXPECT_THAT(result, HasSubstr("disable"));

auto& config = ConfigSession::getInstance().getAgentConfig();
EXPECT_FALSE(*config.sw()->switchSettings()->ptpTcEnable());
}

TEST_F(CmdConfigPtpTransparentClockTestFixture, enableWhenDisabled) {
// First disable, then re-enable
setupTestableConfigSession(cmdPrefix_, "disable");
CmdConfigPtpTransparentClock cmd;
HostInfo hostInfo("testhost");

PtpTransparentClockArg disableArg({"disable"});
cmd.queryClient(hostInfo, disableArg);

PtpTransparentClockArg enableArg({"enable"});
auto result = cmd.queryClient(hostInfo, enableArg);
EXPECT_THAT(result, HasSubstr("enable"));

auto& config = ConfigSession::getInstance().getAgentConfig();
EXPECT_TRUE(*config.sw()->switchSettings()->ptpTcEnable());
}

TEST_F(CmdConfigPtpTransparentClockTestFixture, alreadyEnabled) {
// Seed has ptpTcEnable=true; enabling again is a no-op
setupTestableConfigSession(cmdPrefix_, "enable");
CmdConfigPtpTransparentClock cmd;
HostInfo hostInfo("testhost");
PtpTransparentClockArg arg({"enable"});

auto result = cmd.queryClient(hostInfo, arg);
EXPECT_THAT(result, HasSubstr("already"));
}

} // namespace facebook::fboss
1 change: 1 addition & 0 deletions fboss/cli/fboss2/test/integration_test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cpp_binary(
"ConfigInterfaceProfileTest.cpp",
"ConfigL2LearningModeTest.cpp",
"ConfigPfcTest.cpp",
"ConfigPtpTest.cpp",
"ConfigPortQueueConfigTest.cpp",
"ConfigQosPolicyMapTest.cpp",
"ConfigSessionClearTest.cpp",
Expand Down
Loading
Loading