From 8045c829d76a1cba8edc649bd952f523224259a3 Mon Sep 17 00:00:00 2001 From: whd <7058128+superwhd@users.noreply.github.com> Date: Wed, 9 Nov 2022 18:31:11 +0800 Subject: [PATCH] [cli] add commands for Border Routing counters (#8372) --- include/openthread/instance.h | 2 +- include/openthread/ip6.h | 8 +++++ src/cli/README.md | 12 ++++++- src/cli/cli.cpp | 66 +++++++++++++++++++++++++++++++++++ src/core/api/ip6_api.cpp | 5 +++ src/core/net/ip6.hpp | 6 ++++ 6 files changed, 97 insertions(+), 2 deletions(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 1ea231f48..4ee0f28c3 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (260) +#define OPENTHREAD_API_VERSION (261) /** * @addtogroup api-instance diff --git a/include/openthread/ip6.h b/include/openthread/ip6.h index 39534d880..078b189a8 100644 --- a/include/openthread/ip6.h +++ b/include/openthread/ip6.h @@ -885,6 +885,14 @@ typedef struct otBorderRoutingCounters */ const otBorderRoutingCounters *otIp6GetBorderRoutingCounters(otInstance *aInstance); +/** + * Resets the Border Routing counters. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + */ +void otIp6ResetBorderRoutingCounters(otInstance *aInstance); + /** * @} * diff --git a/src/cli/README.md b/src/cli/README.md index 241705ef5..ed8d64527 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -834,6 +834,7 @@ Get the supported counter names. ```bash > counters +br ip mac mle @@ -844,7 +845,10 @@ Done Get the counter value. -Note: `OPENTHREAD_CONFIG_UPTIME_ENABLE` is required for MLE role time tracking in `counters mle` +Note: + +- `OPENTHREAD_CONFIG_UPTIME_ENABLE` is required for MLE role time tracking in `counters mle` +- `OPENTHREAD_CONFIG_IP6_BR_COUNTERS_ENABLE` is required for `counters br` ```bash > counters mac @@ -903,6 +907,12 @@ TxFailed: 0 RxSuccess: 5 RxFailed: 0 Done +> counters br +Inbound Unicast: Packets 4 Bytes 320 +Inbound Multicast: Packets 0 Bytes 0 +Outbound Unicast: Packets 2 Bytes 160 +Outbound Multicast: Packets 0 Bytes 0 +Done ``` ### counters \ reset diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 6fae533b2..4e1392d78 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -2481,10 +2481,76 @@ template <> otError Interpreter::Process(Arg aArgs[]) */ if (aArgs[0].IsEmpty()) { +#if OPENTHREAD_CONFIG_IP6_BR_COUNTERS_ENABLE + OutputLine("br"); +#endif OutputLine("ip"); OutputLine("mac"); OutputLine("mle"); } +#if OPENTHREAD_CONFIG_IP6_BR_COUNTERS_ENABLE + /** + * @cli counters br + * @code + * counters br + * Inbound Unicast: Packets 4 Bytes 320 + * Inbound Multicast: Packets 0 Bytes 0 + * Outbound Unicast: Packets 2 Bytes 160 + * Outbound Multicast: Packets 0 Bytes 0 + * Done + * @endcode + * @par api_copy + * #otIp6GetBorderRoutingCounters + */ + else if (aArgs[0] == "br") + { + if (aArgs[1].IsEmpty()) + { + struct BrCounterName + { + const otPacketsAndBytes otBorderRoutingCounters::*mPacketsAndBytes; + const char * mName; + }; + + static const BrCounterName kCounterNames[] = { + {&otBorderRoutingCounters::mInboundUnicast, "Inbound Unicast"}, + {&otBorderRoutingCounters::mInboundMulticast, "Inbound Multicast"}, + {&otBorderRoutingCounters::mOutboundUnicast, "Outbound Unicast"}, + {&otBorderRoutingCounters::mOutboundMulticast, "Outbound Multicast"}, + }; + + const otBorderRoutingCounters *brCounters = otIp6GetBorderRoutingCounters(GetInstancePtr()); + Uint64StringBuffer uint64StringBuffer; + + for (const BrCounterName &counter : kCounterNames) + { + OutputFormat("%s:", counter.mName); + OutputFormat(" Packets %s", + Uint64ToString((brCounters->*counter.mPacketsAndBytes).mPackets, uint64StringBuffer)); + OutputFormat(" Bytes %s", + Uint64ToString((brCounters->*counter.mPacketsAndBytes).mBytes, uint64StringBuffer)); + OutputNewLine(); + } + } + /** + * @cli counters br reset + * @code + * counters br reset + * Done + * @endcode + * @par api_copy + * #otIp6ResetBorderRoutingCounters + */ + else if ((aArgs[1] == "reset") && aArgs[2].IsEmpty()) + { + otIp6ResetBorderRoutingCounters(GetInstancePtr()); + } + else + { + error = OT_ERROR_INVALID_ARGS; + } + } +#endif /** * @cli counters (mac) * @code diff --git a/src/core/api/ip6_api.cpp b/src/core/api/ip6_api.cpp index 78fb477eb..7990a596b 100644 --- a/src/core/api/ip6_api.cpp +++ b/src/core/api/ip6_api.cpp @@ -300,4 +300,9 @@ const otBorderRoutingCounters *otIp6GetBorderRoutingCounters(otInstance *aInstan { return &AsCoreType(aInstance).Get().GetBorderRoutingCounters(); } + +void otIp6ResetBorderRoutingCounters(otInstance *aInstance) +{ + AsCoreType(aInstance).Get().ResetBorderRoutingCounters(); +} #endif diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index 1b97dccdc..24e4c4b21 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -348,6 +348,12 @@ public: * */ const otBorderRoutingCounters &GetBorderRoutingCounters(void) const { return mBorderRoutingCounters; } + + /** + * This method resets the Border Routing counters. + * + */ + void ResetBorderRoutingCounters(void) { memset(&mBorderRoutingCounters, 0, sizeof(mBorderRoutingCounters)); } #endif private: