From af36786f04e408d351138e4a36efdc328bc01290 Mon Sep 17 00:00:00 2001 From: whd <7058128+superwhd@users.noreply.github.com> Date: Wed, 29 Nov 2023 12:50:26 +0800 Subject: [PATCH] [posix] unify the infra netif and backbone netif (#9638) This commit refactors the code to unify the concept of 'Infrastructure network interface' (which is for border routing) and 'backbone network interface' (which is for backbone router). From now on they will both be referred as 'infrastructure network interface'. In general border routing and backbone router should be using the same infrastructure network interface so I made this change. This commit removes the `posix/platform/backbone.cpp` source file and put its functionality in `posix/platform/infra_if.cpp`. Previously `backbone.cpp` maintained its own variables `gBackboneNetifName` and `gBackboneNetifIndex` which are redundant compared to the ones maintained at `infra_if.cpp`. I removed these variables and changed the references to use `otSysGetInfraNetifName` and `otSysGetInfraNetifIndex` accordingly. --- src/posix/platform/CMakeLists.txt | 1 - src/posix/platform/backbone.cpp | 101 ------------------ .../include/openthread/openthread-system.h | 8 ++ src/posix/platform/infra_if.cpp | 93 +++++++++++++--- src/posix/platform/infra_if.hpp | 37 ++++++- src/posix/platform/multicast_routing.cpp | 4 +- src/posix/platform/multicast_routing.hpp | 2 +- src/posix/platform/openthread-posix-config.h | 3 +- src/posix/platform/platform-posix.h | 57 ---------- src/posix/platform/system.cpp | 33 ++---- src/posix/platform/udp.cpp | 11 +- 11 files changed, 141 insertions(+), 209 deletions(-) delete mode 100644 src/posix/platform/backbone.cpp diff --git a/src/posix/platform/CMakeLists.txt b/src/posix/platform/CMakeLists.txt index 3934d0ccb..6dd328811 100644 --- a/src/posix/platform/CMakeLists.txt +++ b/src/posix/platform/CMakeLists.txt @@ -124,7 +124,6 @@ endif() add_library(openthread-posix alarm.cpp - backbone.cpp backtrace.cpp configuration.cpp config_file.cpp diff --git a/src/posix/platform/backbone.cpp b/src/posix/platform/backbone.cpp deleted file mode 100644 index 517dc90f1..000000000 --- a/src/posix/platform/backbone.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) 2020, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file - * This file implements the platform Backbone interface management on Linux. - */ - -#include "openthread-posix-config.h" - -#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - -#include "multicast_routing.hpp" -#include "platform-posix.h" -#include "common/code_utils.hpp" -#include "posix/platform/mainloop.hpp" - -char gBackboneNetifName[IFNAMSIZ] = ""; -unsigned int gBackboneNetifIndex = 0; -#if OPENTHREAD_POSIX_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE -static ot::Posix::MulticastRoutingManager sMulticastRoutingManager; -#endif - -void platformBackboneInit(const char *aInterfaceName) -{ - if (aInterfaceName == nullptr || aInterfaceName[0] == '\0') - { - otLogWarnPlat("Backbone Router feature is disabled: infra/backbone interface is missing"); - ExitNow(); - } - - VerifyOrDie(strnlen(aInterfaceName, sizeof(gBackboneNetifName)) < sizeof(gBackboneNetifName), - OT_EXIT_INVALID_ARGUMENTS); - strcpy(gBackboneNetifName, aInterfaceName); - - gBackboneNetifIndex = if_nametoindex(gBackboneNetifName); - VerifyOrDie(gBackboneNetifIndex > 0, OT_EXIT_FAILURE); - - otLogInfoPlat("Backbone interface is configured to %s (%d)", gBackboneNetifName, gBackboneNetifIndex); - -exit: - return; -} - -void platformBackboneSetUp(void) -{ -#if OPENTHREAD_POSIX_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE - sMulticastRoutingManager.SetUp(); -#endif -} - -void platformBackboneTearDown(void) -{ -#if OPENTHREAD_POSIX_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE - sMulticastRoutingManager.TearDown(); -#endif -} - -void platformBackboneDeinit(void) -{ - gBackboneNetifIndex = 0; - - memset(gBackboneNetifName, 0, sizeof(gBackboneNetifName)); -} - -void platformBackboneStateChange(otInstance *aInstance, otChangedFlags aFlags) -{ - OT_UNUSED_VARIABLE(aInstance); - OT_UNUSED_VARIABLE(aFlags); - -#if OPENTHREAD_POSIX_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE - sMulticastRoutingManager.HandleStateChange(aInstance, aFlags); -#endif -} - -#endif diff --git a/src/posix/platform/include/openthread/openthread-system.h b/src/posix/platform/include/openthread/openthread-system.h index 820cf2b1f..29dc3ee80 100644 --- a/src/posix/platform/include/openthread/openthread-system.h +++ b/src/posix/platform/include/openthread/openthread-system.h @@ -187,6 +187,14 @@ unsigned int otSysGetThreadNetifIndex(void); */ const char *otSysGetInfraNetifName(void); +/** + * Returns the infrastructure network interface index. + * + * @returns The infrastructure network interface index. + * + */ +uint32_t otSysGetInfraNetifIndex(void); + /** * Returns the radio spinel metrics. * diff --git a/src/posix/platform/infra_if.cpp b/src/posix/platform/infra_if.cpp index 2435fbdbb..626d12822 100644 --- a/src/posix/platform/infra_if.cpp +++ b/src/posix/platform/infra_if.cpp @@ -91,6 +91,7 @@ exit: return ret; } +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE otError otPlatInfraIfSendIcmp6Nd(uint32_t aInfraIfIndex, const otIp6Address *aDestAddress, const uint8_t *aBuffer, @@ -98,6 +99,7 @@ otError otPlatInfraIfSendIcmp6Nd(uint32_t aInfraIfIndex, { return ot::Posix::InfraNetif::Get().SendIcmp6Nd(aInfraIfIndex, *aDestAddress, aBuffer, aBufferLength); } +#endif otError otPlatInfraIfDiscoverNat64Prefix(uint32_t aInfraIfIndex) { @@ -114,6 +116,8 @@ bool platformInfraIfIsRunning(void) { return ot::Posix::InfraNetif::Get().IsRunn const char *otSysGetInfraNetifName(void) { return ot::Posix::InfraNetif::Get().GetNetifName(); } +uint32_t otSysGetInfraNetifIndex(void) { return ot::Posix::InfraNetif::Get().GetNetifIndex(); } + uint32_t otSysGetInfraNetifFlags(void) { return ot::Posix::InfraNetif::Get().GetFlags(); } void otSysCountInfraNetifAddresses(otSysInfraNetIfAddressCounters *aAddressCounters) @@ -206,6 +210,7 @@ int CreateNetLinkSocket(void) return sock; } +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE otError InfraNetif::SendIcmp6Nd(uint32_t aInfraIfIndex, const otIp6Address &aDestAddress, const uint8_t *aBuffer, @@ -279,6 +284,7 @@ otError InfraNetif::SendIcmp6Nd(uint32_t aInfraIfIndex, exit: return error; } +#endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE bool InfraNetif::IsRunning(void) const { @@ -352,6 +358,20 @@ exit: return; } +#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE +void InfraNetif::HandleBackboneStateChange(otInstance *aInstance, otChangedFlags aFlags) +{ + OT_ASSERT(gInstance == aInstance); + + OT_UNUSED_VARIABLE(aInstance); + OT_UNUSED_VARIABLE(aFlags); + +#if OPENTHREAD_POSIX_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE + mMulticastRoutingManager.HandleStateChange(aInstance, aFlags); +#endif +} +#endif + bool InfraNetif::HasLinkLocalAddress(void) const { bool hasLla = false; @@ -389,18 +409,23 @@ void InfraNetif::Init(void) { mNetLinkSocket = CreateNetLinkSocket(); } void InfraNetif::SetInfraNetif(const char *aIfName, int aIcmp6Socket) { - uint32_t ifIndex = 0; - otBorderRoutingState state = otBorderRoutingGetState(gInstance); + uint32_t ifIndex = 0; + + OT_UNUSED_VARIABLE(aIcmp6Socket); OT_ASSERT(gInstance != nullptr); - VerifyOrDie(mNetLinkSocket != -1, OT_EXIT_INVALID_STATE); - VerifyOrDie(state == OT_BORDER_ROUTING_STATE_UNINITIALIZED || state == OT_BORDER_ROUTING_STATE_DISABLED, - OT_EXIT_INVALID_STATE); + +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE + SetInfraNetifIcmp6SocketForBorderRouting(aIcmp6Socket); +#endif +#if OPENTHREAD_POSIX_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE + VerifyOrDie(!mMulticastRoutingManager.IsEnabled(), OT_EXIT_INVALID_STATE); +#endif if (aIfName == nullptr || aIfName[0] == '\0') { - otLogWarnPlat("Border Routing feature is disabled: infra/backbone interface is missing"); + otLogWarnPlat("Border Routing/Backbone Router feature is disabled: infra interface is missing"); ExitNow(); } @@ -417,12 +442,6 @@ void InfraNetif::SetInfraNetif(const char *aIfName, int aIcmp6Socket) mInfraIfIndex = ifIndex; - if (mInfraIfIcmp6Socket != -1) - { - close(mInfraIfIcmp6Socket); - } - mInfraIfIcmp6Socket = aIcmp6Socket; - exit: return; } @@ -432,8 +451,15 @@ void InfraNetif::SetUp(void) OT_ASSERT(gInstance != nullptr); VerifyOrExit(mNetLinkSocket != -1); +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE SuccessOrDie(otBorderRoutingInit(gInstance, mInfraIfIndex, platformInfraIfIsRunning())); SuccessOrDie(otBorderRoutingSetEnabled(gInstance, /* aEnabled */ true)); +#endif + +#if OPENTHREAD_POSIX_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE + mMulticastRoutingManager.SetUp(); +#endif + Mainloop::Manager::Get().Add(*this); exit: return; @@ -441,17 +467,26 @@ exit: void InfraNetif::TearDown(void) { +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE IgnoreError(otBorderRoutingSetEnabled(gInstance, false)); +#endif + +#if OPENTHREAD_POSIX_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE + mMulticastRoutingManager.TearDown(); +#endif + Mainloop::Manager::Get().Remove(*this); } void InfraNetif::Deinit(void) { +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE if (mInfraIfIcmp6Socket != -1) { close(mInfraIfIcmp6Socket); mInfraIfIcmp6Socket = -1; } +#endif if (mNetLinkSocket != -1) { @@ -459,16 +494,20 @@ void InfraNetif::Deinit(void) mNetLinkSocket = -1; } - mInfraIfIndex = 0; + mInfraIfName[0] = '\0'; + mInfraIfIndex = 0; } void InfraNetif::Update(otSysMainloopContext &aContext) { - VerifyOrExit(mInfraIfIcmp6Socket != -1); VerifyOrExit(mNetLinkSocket != -1); +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE + VerifyOrExit(mInfraIfIcmp6Socket != -1); + FD_SET(mInfraIfIcmp6Socket, &aContext.mReadFdSet); aContext.mMaxFd = OT_MAX(aContext.mMaxFd, mInfraIfIcmp6Socket); +#endif FD_SET(mNetLinkSocket, &aContext.mReadFdSet); aContext.mMaxFd = OT_MAX(aContext.mMaxFd, mNetLinkSocket); @@ -506,7 +545,9 @@ void InfraNetif::ReceiveNetLinkMessage(void) case RTM_DELADDR: case RTM_NEWLINK: case RTM_DELLINK: +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE SuccessOrDie(otPlatInfraIfStateChanged(gInstance, mInfraIfIndex, platformInfraIfIsRunning())); +#endif break; case NLMSG_ERROR: { @@ -525,6 +566,7 @@ exit: return; } +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE void InfraNetif::ReceiveIcmp6Message(void) { otError error = OT_ERROR_NONE; @@ -596,6 +638,7 @@ exit: otLogDebgPlat("Failed to handle ICMPv6 message: %s", otThreadErrorToString(error)); } } +#endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE #if OPENTHREAD_POSIX_CONFIG_NAT64_AIL_PREFIX_ENABLE const char InfraNetif::kWellKnownIpv4OnlyName[] = "ipv4only.arpa"; @@ -671,7 +714,9 @@ void InfraNetif::DiscoverNat64PrefixDone(union sigval sv) } } +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE otPlatInfraIfDiscoverNat64PrefixDone(gInstance, Get().mInfraIfIndex, &prefix); +#endif exit: freeaddrinfo(res); @@ -726,15 +771,35 @@ exit: } #endif // OPENTHREAD_POSIX_CONFIG_NAT64_AIL_PREFIX_ENABLE +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE +void InfraNetif::SetInfraNetifIcmp6SocketForBorderRouting(int aIcmp6Socket) +{ + otBorderRoutingState state = otBorderRoutingGetState(gInstance); + + VerifyOrDie(state == OT_BORDER_ROUTING_STATE_UNINITIALIZED || state == OT_BORDER_ROUTING_STATE_DISABLED, + OT_EXIT_INVALID_STATE); + + if (mInfraIfIcmp6Socket != -1) + { + close(mInfraIfIcmp6Socket); + } + mInfraIfIcmp6Socket = aIcmp6Socket; +} +#endif + void InfraNetif::Process(const otSysMainloopContext &aContext) { +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE VerifyOrExit(mInfraIfIcmp6Socket != -1); +#endif VerifyOrExit(mNetLinkSocket != -1); +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE if (FD_ISSET(mInfraIfIcmp6Socket, &aContext.mReadFdSet)) { ReceiveIcmp6Message(); } +#endif if (FD_ISSET(mNetLinkSocket, &aContext.mReadFdSet)) { diff --git a/src/posix/platform/infra_if.hpp b/src/posix/platform/infra_if.hpp index b468e10a1..e8aedd2f9 100644 --- a/src/posix/platform/infra_if.hpp +++ b/src/posix/platform/infra_if.hpp @@ -37,6 +37,7 @@ #include #include +#include "multicast_routing.hpp" #include "core/common/non_copyable.hpp" #include "posix/platform/mainloop.hpp" @@ -82,7 +83,8 @@ public: * Sets the infrastructure network interface. * * @param[in] aIfName A pointer to infrastructure network interface name. - * @param[in] aIcmp6Socket A SOCK_RAW socket for sending/receiving ICMPv6 messages. + * @param[in] aIcmp6Socket A SOCK_RAW socket for sending/receiving ICMPv6 messages. If you don't need border + * routing feature, you can pass in -1. * */ void SetInfraNetif(const char *aIfName, int aIcmp6Socket); @@ -133,6 +135,15 @@ public: */ void CountAddresses(otSysInfraNetIfAddressCounters &aAddressCounters) const; + /** + * Handles the backbone state change events. + * + * @param[in] aInstance A pointer to the OpenThread instance. + * @param[in] aFlags Flags that denote the state change events. + * + */ + void HandleBackboneStateChange(otInstance *aInstance, otChangedFlags aFlags); + /** * Sends an ICMPv6 Neighbor Discovery message on given infrastructure interface. * @@ -176,6 +187,14 @@ public: */ const char *GetNetifName(void) const { return (mInfraIfIndex != 0) ? mInfraIfName : nullptr; } + /** + * Gets the infrastructure network interface index. + * + * @returns The infrastructure network interface index. + * + */ + uint32_t GetNetifIndex(void) const { return mInfraIfIndex; } + /** * Gets the infrastructure network interface singleton. * @@ -201,14 +220,22 @@ private: static const uint8_t kValidNat64PrefixLength[]; char mInfraIfName[IFNAMSIZ]; - uint32_t mInfraIfIndex = 0; - int mInfraIfIcmp6Socket = -1; - int mNetLinkSocket = -1; + uint32_t mInfraIfIndex = 0; + int mNetLinkSocket = -1; +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE + int mInfraIfIcmp6Socket = -1; +#endif +#if OPENTHREAD_POSIX_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE + MulticastRoutingManager mMulticastRoutingManager; +#endif void ReceiveNetLinkMessage(void); - void ReceiveIcmp6Message(void); bool HasLinkLocalAddress(void) const; static void DiscoverNat64PrefixDone(union sigval sv); +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE + void SetInfraNetifIcmp6SocketForBorderRouting(int aIcmp6Socket); + void ReceiveIcmp6Message(void); +#endif }; } // namespace Posix diff --git a/src/posix/platform/multicast_routing.cpp b/src/posix/platform/multicast_routing.cpp index abced8c4d..40af863a6 100644 --- a/src/posix/platform/multicast_routing.cpp +++ b/src/posix/platform/multicast_routing.cpp @@ -159,7 +159,7 @@ void MulticastRoutingManager::UpdateMldReport(const Ip6::Address &aAddress, bool struct ipv6_mreq ipv6mr; otError error = OT_ERROR_NONE; - ipv6mr.ipv6mr_interface = if_nametoindex(gBackboneNetifName); + ipv6mr.ipv6mr_interface = if_nametoindex(otSysGetInfraNetifName()); memcpy(&ipv6mr.ipv6mr_multiaddr, aAddress.GetBytes(), sizeof(ipv6mr.ipv6mr_multiaddr)); error = (setsockopt(mMulticastRouterSock, IPPROTO_IPV6, (isAdd ? IPV6_JOIN_GROUP : IPV6_LEAVE_GROUP), (void *)&ipv6mr, sizeof(ipv6mr)) @@ -243,7 +243,7 @@ void MulticastRoutingManager::InitMulticastRouterSock(void) // Add Backbone network interface to MIF mif6ctl.mif6c_mifi = kMifIndexBackbone; - mif6ctl.mif6c_pifi = if_nametoindex(gBackboneNetifName); + mif6ctl.mif6c_pifi = otSysGetInfraNetifIndex(); VerifyOrDie(mif6ctl.mif6c_pifi > 0, OT_EXIT_ERROR_ERRNO); VerifyOrDie(0 == setsockopt(mMulticastRouterSock, IPPROTO_IPV6, MRT6_ADD_MIF, &mif6ctl, sizeof(mif6ctl)), OT_EXIT_ERROR_ERRNO); diff --git a/src/posix/platform/multicast_routing.hpp b/src/posix/platform/multicast_routing.hpp index 08d87ca58..c4d7e32fd 100644 --- a/src/posix/platform/multicast_routing.hpp +++ b/src/posix/platform/multicast_routing.hpp @@ -58,6 +58,7 @@ public: { } + bool IsEnabled(void) const { return mMulticastRouterSock >= 0; } void SetUp(void); void TearDown(void); void Update(otSysMainloopContext &aContext) override; @@ -110,7 +111,6 @@ private: void Remove(const Ip6::Address &aAddress); void UpdateMldReport(const Ip6::Address &aAddress, bool isAdd); bool HasMulticastListener(const Ip6::Address &aAddress) const; - bool IsEnabled(void) const { return mMulticastRouterSock >= 0; } void InitMulticastRouterSock(void); void FinalizeMulticastRouterSock(void); void ProcessMulticastRouterMessages(void); diff --git a/src/posix/platform/openthread-posix-config.h b/src/posix/platform/openthread-posix-config.h index 65d65d6a2..cc8afe5cb 100644 --- a/src/posix/platform/openthread-posix-config.h +++ b/src/posix/platform/openthread-posix-config.h @@ -394,7 +394,8 @@ * not explicit defined. */ #ifndef OPENTHREAD_POSIX_CONFIG_INFRA_IF_ENABLE -#define OPENTHREAD_POSIX_CONFIG_INFRA_IF_ENABLE OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE +#define OPENTHREAD_POSIX_CONFIG_INFRA_IF_ENABLE \ + (OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE || OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE) #endif /** diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index 8f05602a2..0617bfc05 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -409,69 +409,12 @@ extern char gNetifName[IFNAMSIZ]; */ extern unsigned int gNetifIndex; -/** - * Initializes platform Backbone network. - * - * @note This function is called before OpenThread instance is created. - * - * @param[in] aInterfaceName A pointer to Thread network interface name. - * - */ -void platformBackboneInit(const char *aInterfaceName); - -/** - * Sets up platform Backbone network. - * - * @note This function is called after OpenThread instance is created. - * - * @param[in] aInstance A pointer to the OpenThread instance. - * - */ -void platformBackboneSetUp(void); - -/** - * Tears down platform Backbone network. - * - * @note This function is called before OpenThread instance is destructed. - * - */ -void platformBackboneTearDown(void); - -/** - * Shuts down the platform Backbone network. - * - * @note This function is called after OpenThread instance is destructed. - * - */ -void platformBackboneDeinit(void); - -/** - * Performs notifies state changes to platform Backbone network. - * - * @param[in] aInstance A pointer to the OpenThread instance. - * @param[in] aFlags Flags that denote the state change events. - * - */ -void platformBackboneStateChange(otInstance *aInstance, otChangedFlags aFlags); - /** * A pointer to the OpenThread instance. * */ extern otInstance *gInstance; -/** - * The name of Backbone network interface. - * - */ -extern char gBackboneNetifName[IFNAMSIZ]; - -/** - * The index of Backbone network interface. - * - */ -extern unsigned int gBackboneNetifIndex; - /** * Tells if the infrastructure interface is running. * diff --git a/src/posix/platform/system.cpp b/src/posix/platform/system.cpp index a154ff829..db10899d2 100644 --- a/src/posix/platform/system.cpp +++ b/src/posix/platform/system.cpp @@ -72,10 +72,7 @@ static void processStateChange(otChangedFlags aFlags, void *aContext) #endif #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - if (gBackboneNetifIndex != 0) - { - platformBackboneStateChange(instance, aFlags); - } + ot::Posix::InfraNetif::Get().HandleBackboneStateChange(instance, aFlags); #endif } #endif @@ -145,10 +142,6 @@ void platformInit(otPlatformConfig *aPlatformConfig) #endif platformRandomInit(); -#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - platformBackboneInit(aPlatformConfig->mBackboneInterfaceName); -#endif - #if OPENTHREAD_POSIX_CONFIG_INFRA_IF_ENABLE ot::Posix::InfraNetif::Get().Init(); @@ -178,16 +171,20 @@ void platformSetUp(otPlatformConfig *aPlatformConfig) VerifyOrExit(!gDryRun); -#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - platformBackboneSetUp(); -#endif - #if OPENTHREAD_POSIX_CONFIG_INFRA_IF_ENABLE if (aPlatformConfig->mBackboneInterfaceName != nullptr && strlen(aPlatformConfig->mBackboneInterfaceName) > 0) { - otSysSetInfraNetif(aPlatformConfig->mBackboneInterfaceName, - ot::Posix::InfraNetif::CreateIcmp6Socket(aPlatformConfig->mBackboneInterfaceName)); + int icmp6Sock = -1; + +#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE + icmp6Sock = ot::Posix::InfraNetif::CreateIcmp6Socket(aPlatformConfig->mBackboneInterfaceName); +#endif + + otSysSetInfraNetif(aPlatformConfig->mBackboneInterfaceName, icmp6Sock); } +#endif + +#if OPENTHREAD_POSIX_CONFIG_INFRA_IF_ENABLE ot::Posix::InfraNetif::Get().SetUp(); #endif @@ -246,10 +243,6 @@ void platformTearDown(void) ot::Posix::InfraNetif::Get().TearDown(); #endif -#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - platformBackboneTearDown(); -#endif - exit: return; } @@ -278,10 +271,6 @@ void platformDeinit(void) ot::Posix::InfraNetif::Get().Deinit(); #endif -#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - platformBackboneDeinit(); -#endif - exit: return; } diff --git a/src/posix/platform/udp.cpp b/src/posix/platform/udp.cpp index 7f78acccb..6d7c880a3 100644 --- a/src/posix/platform/udp.cpp +++ b/src/posix/platform/udp.cpp @@ -324,11 +324,12 @@ otError otPlatUdpBindToNetif(otUdpSocket *aUdpSocket, otNetifIdentifier aNetifId { #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE #if __linux__ - VerifyOrExit(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, gBackboneNetifName, strlen(gBackboneNetifName)) == 0, + VerifyOrExit(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, otSysGetInfraNetifName(), + strlen(otSysGetInfraNetifName())) == 0, error = OT_ERROR_FAILED); #else // __NetBSD__ || __FreeBSD__ || __APPLE__ - VerifyOrExit(setsockopt(fd, IPPROTO_IPV6, IPV6_BOUND_IF, &gBackboneNetifIndex, sizeof(gBackboneNetifIndex)) == - 0, + uint32_t backboneNetifIndex = otSysGetInfraNetifIndex(); + VerifyOrExit(setsockopt(fd, IPPROTO_IPV6, IPV6_BOUND_IF, &backboneNetifIndex, sizeof(backboneNetifIndex)) == 0, error = OT_ERROR_FAILED); #endif // __linux__ #else @@ -473,7 +474,7 @@ otError otPlatUdpJoinMulticastGroup(otUdpSocket *aUdpSocket, break; case OT_NETIF_BACKBONE: #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - mreq.ipv6mr_interface = gBackboneNetifIndex; + mreq.ipv6mr_interface = otSysGetInfraNetifIndex(); #else ExitNow(error = OT_ERROR_NOT_IMPLEMENTED); #endif @@ -513,7 +514,7 @@ otError otPlatUdpLeaveMulticastGroup(otUdpSocket *aUdpSocket, break; case OT_NETIF_BACKBONE: #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - mreq.ipv6mr_interface = gBackboneNetifIndex; + mreq.ipv6mr_interface = otSysGetInfraNetifIndex(); #else ExitNow(error = OT_ERROR_NOT_IMPLEMENTED); #endif