From ae4ed57a2b48927039f43686153fc0188a3d5c58 Mon Sep 17 00:00:00 2001 From: Seshagirirao Lekkala Date: Thu, 10 Sep 2026 16:57:37 -0700 Subject: [PATCH 1/3] fboss2: make interface traffic RPCs synchronous --- .../traffic/CmdShowInterfaceTraffic.cpp | 16 ++++------------ .../interface/traffic/CmdShowInterfaceTraffic.h | 1 - 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.cpp b/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.cpp index d9e7cc0aed2..7487e22a445 100644 --- a/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.cpp +++ b/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.cpp @@ -25,11 +25,8 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::queryClient( auto client = utils::createClient>(hostInfo); - folly::IOThreadPoolExecutor executor(2); - - // Gather port stats asynchrounously - auto portInfos = - client->semifuture_getAllPortInfo().via(executor.getEventBase()); + std::map portInfos; + client->sync_getAllPortInfo(portInfos); std::map counters; if (utils::isMultiSwitchEnabled(hostInfo)) { @@ -54,15 +51,10 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::queryClient( }; utils::runOnAllHwAgents(hostInfo, hwAgentQueryFn); } else { - auto entries = - client->semifuture_getCounters().via(executor.getEventBase()); - entries.wait(); - counters = entries.value(); + client->sync_getCounters(counters); } - portInfos.wait(); - - return createModel(portInfos.value(), counters, queriedIfs); + return createModel(portInfos, counters, queriedIfs); } CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::createModel( diff --git a/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.h b/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.h index 571ef1ecf66..9d4dc73787c 100644 --- a/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.h +++ b/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.h @@ -21,7 +21,6 @@ #include "fboss/cli/fboss2/commands/show/interface/traffic/gen-cpp2/model_types.h" #include "fboss/cli/fboss2/utils/CmdUtils.h" #include "fboss/cli/fboss2/utils/Table.h" -#include "folly/executors/IOThreadPoolExecutor.h" namespace facebook::fboss { From 8cb129d232830f0e4fda584f3e03f59c1b0a6714 Mon Sep 17 00:00:00 2001 From: Seshagirirao Lekkala Date: Thu, 10 Sep 2026 16:58:42 -0700 Subject: [PATCH 2/3] Export FB303 monotonic counter rates --- fboss/agent/hw/test/HwPortFb303StatsTests.cpp | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/fboss/agent/hw/test/HwPortFb303StatsTests.cpp b/fboss/agent/hw/test/HwPortFb303StatsTests.cpp index b8f12207158..9f3ae93b3fb 100644 --- a/fboss/agent/hw/test/HwPortFb303StatsTests.cpp +++ b/fboss/agent/hw/test/HwPortFb303StatsTests.cpp @@ -755,10 +755,14 @@ TEST(HwPortFb303StatsTest, ReInit) { true /* inCongestionDiscardSeenSupported */); stats.portNameChanged(kNewPortName); for (const auto& sName : stats.kPortMonotonicCounterStatKeys()) { - EXPECT_TRUE(fbData->getStatMap()->contains( - HwPortFb303Stats::statName(sName, kNewPortName))); - EXPECT_FALSE(fbData->getStatMap()->contains( - HwPortFb303Stats::statName(sName, kPortName))); + const auto newStatName = HwPortFb303Stats::statName(sName, kNewPortName); + const auto oldStatName = HwPortFb303Stats::statName(sName, kPortName); + EXPECT_TRUE(fbData->getStatMap()->contains(newStatName)); + EXPECT_FALSE(fbData->getStatMap()->contains(oldStatName)); + EXPECT_TRUE( + fbData->getCounterIfExists(newStatName + ".rate.60").has_value()); + EXPECT_FALSE( + fbData->getCounterIfExists(oldStatName + ".rate.60").has_value()); } for (auto statKey : stats.kPortFb303CounterStatKeys()) { EXPECT_FALSE( @@ -832,6 +836,21 @@ TEST(HwPortFb303StatsTest, ReInit) { // Hence, we don't check for their existence in this test. } +TEST(HwPortFb303StatsTest, MonotonicCountersExportRate) { + const std::string portName = "eth1/1/7"; + const auto statName = HwPortFb303Stats::statName(kInBytes(), portName); + const auto rateName = statName + ".rate.60"; + + { + HwPortFb303Stats stats(portName); + facebook::tcData().publishStats(); + EXPECT_TRUE(fbData->getCounterIfExists(rateName).has_value()); + } + + facebook::tcData().publishStats(); + EXPECT_FALSE(fbData->getCounterIfExists(rateName).has_value()); +} + TEST(HwPortFb303Stats, UpdateStats) { // Initialize with PFC priorities to ensure priority group counters are // created From 8625b178f6956d994e51496cf0ed1f38faee61bb Mon Sep 17 00:00:00 2001 From: Seshagirirao Lekkala Date: Thu, 10 Sep 2026 17:02:15 -0700 Subject: [PATCH 3/3] fboss2: validate and filter interface traffic counters --- .../traffic/CmdShowInterfaceTraffic.cpp | 81 ++++++++++++++----- .../traffic/CmdShowInterfaceTraffic.h | 2 +- .../test/CmdShowInterfaceTrafficTest.cpp | 69 ++++++++++++++++ 3 files changed, 129 insertions(+), 23 deletions(-) diff --git a/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.cpp b/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.cpp index 7487e22a445..e6d7698579d 100644 --- a/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.cpp +++ b/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.cpp @@ -17,6 +17,30 @@ #include #include +#include + +namespace { + +constexpr auto kTrafficCounterRegex = + ".*\\.(in_bytes|in_unicast_pkts|in_multicast_pkts|in_broadcast_pkts|" + "out_bytes|out_unicast_pkts|out_multicast_pkts|out_broadcast_pkts)" + "\\.rate\\.60$"; + +int64_t getTrafficCounter( + const std::map& counters, + const std::string& portName, + const std::string& suffix) { + const auto counterName = portName + suffix; + const auto counter = counters.find(counterName); + if (counter == counters.end()) { + throw std::runtime_error( + "Required HW-agent traffic counter is unavailable: " + counterName); + } + return counter->second; +} + +} // namespace + namespace facebook::fboss { CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::queryClient( @@ -37,7 +61,8 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::queryClient( #ifndef IS_OSS apache::thrift::Client monitoringClient{ client.getChannelShared()}; - monitoringClient.sync_getCounters(hwAgentCounters); + monitoringClient.sync_getRegexCounters( + hwAgentCounters, kTrafficCounterRegex); #else // FbossHwCtrl does not extend FacebookService; the OSS HwAgent // multiplex serves fb303 methods via a FacebookBase2 handler, so @@ -45,13 +70,14 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::queryClient( // getCounters) to fetch HwAgent counters. apache::thrift::Client fb303Client{ client.getChannelShared()}; - fb303Client.sync_getCounters(hwAgentCounters); + fb303Client.sync_getRegexCounters( + hwAgentCounters, kTrafficCounterRegex); #endif counters.merge(hwAgentCounters); }; utils::runOnAllHwAgents(hostInfo, hwAgentQueryFn); } else { - client->sync_getCounters(counters); + client->sync_getRegexCounters(counters, kTrafficCounterRegex); } return createModel(portInfos, counters, queriedIfs); @@ -59,7 +85,7 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::queryClient( CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::createModel( const std::map& portCounters, - std::map intCounters, + const std::map& intCounters, const std::vector& queriedIfs) { RetType ret; @@ -99,27 +125,39 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::createModel( errorCounters.giantErrors() = 0; errorCounters.txErrors() = outputErrors; + if (isNonZeroErrors(errorCounters)) { + ret.error_counters()->push_back(errorCounters); + } + + // Down ports cannot have current traffic and may not have HwAgent rate + // counters. Do not turn an unsupported counter into a false zero. + if (operState != facebook::fboss::PortOperState::UP) { + continue; + } + cli::TrafficCounters trafficCounters; // Getting various counters and converting to Mbps int64_t portSpeed = folly::copy(portInfo.speedMbps().value()); - long inSpeedBps = intCounters[pname + ".in_bytes.rate.60"] * 8; - - double inSpeedMbps = (double)inSpeedBps / 1000000; + const double inSpeedMbps = + getTrafficCounter(intCounters, pname, ".in_bytes.rate.60") * 8.0 / + 1000000.0; // Unicast + multicast + broadcast = PPS - int64_t inPPS = intCounters[pname + ".in_unicast_pkts.rate.60"] + - intCounters[pname + ".in_multicast_pkts.rate.60"] + - intCounters[pname + ".in_broadcast_pkts.rate.60"]; + const int64_t inPPS = + getTrafficCounter(intCounters, pname, ".in_unicast_pkts.rate.60") + + getTrafficCounter(intCounters, pname, ".in_multicast_pkts.rate.60") + + getTrafficCounter(intCounters, pname, ".in_broadcast_pkts.rate.60"); - long outSpeedBps = intCounters[pname + ".out_bytes.rate.60"] * 8; - - double outSpeedMbps = (double)outSpeedBps / 1000000; + const double outSpeedMbps = + getTrafficCounter(intCounters, pname, ".out_bytes.rate.60") * 8.0 / + 1000000.0; // Unicast + multicast + broadcast = PPS - int64_t outPPS = intCounters[pname + ".out_unicast_pkts.rate.60"] + - intCounters[pname + ".out_multicast_pkts.rate.60"] + - intCounters[pname + ".out_broadcast_pkts.rate.60"]; + const int64_t outPPS = + getTrafficCounter(intCounters, pname, ".out_unicast_pkts.rate.60") + + getTrafficCounter(intCounters, pname, ".out_multicast_pkts.rate.60") + + getTrafficCounter(intCounters, pname, ".out_broadcast_pkts.rate.60"); trafficCounters.interfaceName() = pname; trafficCounters.peerIf() = @@ -127,20 +165,16 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::createModel( trafficCounters.inMbps() = inSpeedMbps; trafficCounters.inPct() = calculateUtilizationPercent(inSpeedMbps, portSpeed); - trafficCounters.inKpps() = inPPS / 1000; + trafficCounters.inKpps() = static_cast(inPPS) / 1000.0; trafficCounters.outMbps() = outSpeedMbps; trafficCounters.outPct() = calculateUtilizationPercent(outSpeedMbps, portSpeed); - trafficCounters.outKpps() = outPPS / 1000; + trafficCounters.outKpps() = static_cast(outPPS) / 1000.0; trafficCounters.portSpeed() = portSpeed; // The original in fb_toolkit has an option for "show zero". Since we // can't accept arbitrarily deep parameters right now we will default to // show non-zero and revisit as an option down the road - if (isNonZeroErrors(errorCounters)) { - ret.error_counters()->push_back(errorCounters); - } - if (isInterestingTraffic(trafficCounters)) { ret.traffic_counters()->push_back(trafficCounters); } @@ -165,6 +199,9 @@ CmdShowInterfaceTraffic::RetType CmdShowInterfaceTraffic::createModel( double CmdShowInterfaceTraffic::calculateUtilizationPercent( double speedMbps, int bandwidth) { + if (bandwidth <= 0) { + return 0.0; + } return (speedMbps / bandwidth) * 100; } diff --git a/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.h b/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.h index 9d4dc73787c..be7e24acbff 100644 --- a/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.h +++ b/fboss/cli/fboss2/commands/show/interface/traffic/CmdShowInterfaceTraffic.h @@ -52,7 +52,7 @@ class CmdShowInterfaceTraffic : public CmdHandler< const std::vector& queriedIfs); RetType createModel( const std::map& portCounters, - std::map intCounters, + const std::map& intCounters, const std::vector& queriedIfs); double calculateUtilizationPercent(double speedMbps, int bandwidth); std::string extractExpectedPort(const std::string& portDescription); diff --git a/fboss/cli/fboss2/test/CmdShowInterfaceTrafficTest.cpp b/fboss/cli/fboss2/test/CmdShowInterfaceTrafficTest.cpp index 318fc986199..19ab638333b 100644 --- a/fboss/cli/fboss2/test/CmdShowInterfaceTrafficTest.cpp +++ b/fboss/cli/fboss2/test/CmdShowInterfaceTrafficTest.cpp @@ -4,7 +4,9 @@ #include #include #include +#include #include +#include #include #include "fboss/agent/if/gen-cpp2/ctrl_types.h" @@ -162,6 +164,69 @@ TEST_F(CmdShowInterfaceTrafficTestFixture, createModel) { EXPECT_NEAR(trafficCounters[2].outPct().value(), 19.7531, 0.0001); } +TEST_F(CmdShowInterfaceTrafficTestFixture, queryClient) { + setupMockedAgentServer(); + + EXPECT_CALL(getMockAgent(), getAllPortInfo(_)) + .WillOnce(Invoke([&](auto& entries) { entries = portInfo; })); + + MultiSwitchRunState runState; + runState.multiSwitchEnabled() = false; + EXPECT_CALL(getMockAgent(), getMultiSwitchRunState(_)) + .WillOnce(Invoke([&](auto& response) { response = runState; })); + + auto response = intCounters; + EXPECT_CALL(getMockAgent(), async_eb_getRegexCounters(_, _)) + .WillOnce(Invoke([&response](auto callback, auto regex) { + EXPECT_THAT(*regex, HasSuffix("\\.rate\\.60$")); + callback->result(std::move(response)); + })); + + auto model = CmdShowInterfaceTraffic().queryClient(localhost(), queriedIfs); + EXPECT_EQ(model.traffic_counters()->size(), 3); +} + +TEST_F(CmdShowInterfaceTrafficTestFixture, missingRateCounterFails) { + intCounters.erase("eth1/1/1.in_bytes.rate.60"); + + auto cmd = CmdShowInterfaceTraffic(); + EXPECT_THAT( + [&]() { cmd.createModel(portInfo, intCounters, queriedIfs); }, + ThrowsMessage(HasSubstr( + "Required HW-agent traffic counter is unavailable: " + "eth1/1/1.in_bytes.rate.60"))); +} + +TEST_F(CmdShowInterfaceTrafficTestFixture, downPortDoesNotRequireRateCounters) { + portInfo.at(1).operState() = facebook::fboss::PortOperState::DOWN; + for (auto counter = intCounters.begin(); counter != intCounters.end();) { + if (counter->first.rfind("eth1/1/1.", 0) == 0) { + counter = intCounters.erase(counter); + } else { + ++counter; + } + } + + auto cmd = CmdShowInterfaceTraffic(); + EXPECT_NO_THROW(cmd.createModel(portInfo, intCounters, queriedIfs)); +} + +TEST_F(CmdShowInterfaceTrafficTestFixture, preservesFractionalKpps) { + intCounters["eth1/1/1.in_unicast_pkts.rate.60"] = 100; + intCounters["eth1/1/1.in_multicast_pkts.rate.60"] = 200; + intCounters["eth1/1/1.in_broadcast_pkts.rate.60"] = 300; + + auto cmd = CmdShowInterfaceTraffic(); + auto model = cmd.createModel(portInfo, intCounters, queriedIfs); + + EXPECT_DOUBLE_EQ(model.traffic_counters()->at(0).inKpps().value(), 0.6); +} + +TEST_F(CmdShowInterfaceTrafficTestFixture, zeroBandwidthHasZeroUtilization) { + auto cmd = CmdShowInterfaceTraffic(); + EXPECT_DOUBLE_EQ(cmd.calculateUtilizationPercent(100.0, 0), 0.0); +} + TEST_F(CmdShowInterfaceTrafficTestFixture, printOutput) { auto cmd = CmdShowInterfaceTraffic(); auto model = cmd.createModel(portInfo, intCounters, queriedIfs); @@ -234,6 +299,10 @@ TEST_F(CmdShowInterfaceTrafficTestFixture, createModelFreeFormDescription) { // every rate counter, so isInterestingTraffic() filters out every row and the // Total is computed over an empty set. Totals must read 0.00%. TEST_F(CmdShowInterfaceTrafficTestFixture, printOutputNoInterestingTraffic) { + for (auto& [_, port] : portInfo) { + port.operState() = facebook::fboss::PortOperState::DOWN; + } + auto cmd = CmdShowInterfaceTraffic(); auto model = cmd.createModel(portInfo, {} /* intCounters */, queriedIfs);