[nexus] use InstanceLocator in InfraIf (#12856)

This commit updates the Nexus platform `InfraIf` class to inherit from
`InstanceLocator`, aligning it with the standard OpenThread architectural
patterns.

The `mNode` and `mNodeId` member variables are removed as they are now
redundant. Access to the associated `Instance` and other platform-level
components is now managed through `GetInstance()` and the newly added
`Instance::Get<T>` template specializations for `Node`, `InfraIf`,
`Udp`, `Trel`, and `Mdns`.

The `InfraIf::Init()` method is renamed to `AfterInit()` to better
reflect its role in the node initialization lifecycle. All call sites in
`nexus_infra_if.cpp` are updated to use the locator-based accessors.
This commit is contained in:
Abtin Keshavarzian
2026-04-09 01:39:54 -05:00
committed by GitHub
parent 635d2ffaca
commit 0e8bb15545
4 changed files with 38 additions and 55 deletions
+2 -1
View File
@@ -348,7 +348,8 @@ Node &Core::CreateNode(void)
node->mLogging.Init(node->GetId()); node->mLogging.Init(node->GetId());
} }
node->mInfraIf.Init(*node); node->mInfraIf.AfterInit();
node->mMdns.Init(*node); node->mMdns.Init(*node);
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
node->mTrel.Init(*node); node->mTrel.Init(*node);
+28 -46
View File
@@ -35,8 +35,7 @@ namespace ot {
namespace Nexus { namespace Nexus {
InfraIf::InfraIf(Instance &aInstance) InfraIf::InfraIf(Instance &aInstance)
: mNode(nullptr) : InstanceLocator(aInstance)
, mNodeId(0)
, mIfIndex(0) , mIfIndex(0)
, mUdpHook(nullptr) , mUdpHook(nullptr)
, mHasRioPrefix(false) , mHasRioPrefix(false)
@@ -44,15 +43,13 @@ InfraIf::InfraIf(Instance &aInstance)
{ {
} }
void InfraIf::Init(Node &aNode) void InfraIf::AfterInit(void)
{ {
Ip6::Address address; Ip6::Address address;
LinkLayerAddress mac; LinkLayerAddress mac;
Ip6::InterfaceIdentifier iid; Ip6::InterfaceIdentifier iid;
mIfIndex = 1; mIfIndex = 1;
mNode = &aNode;
mNodeId = aNode.GetId();
GetLinkLayerAddress(mac); GetLinkLayerAddress(mac);
SuccessOrQuit(mac.ConvertToIid(iid)); SuccessOrQuit(mac.ConvertToIid(iid));
@@ -69,7 +66,7 @@ void InfraIf::AddAddress(const Ip6::Address &aAddress)
VerifyOrExit(!HasAddress(aAddress)); VerifyOrExit(!HasAddress(aAddress));
SuccessOrQuit(mAddresses.PushBack(aAddress)); SuccessOrQuit(mAddresses.PushBack(aAddress));
mNode->mMdns.HandleHostAddressEvent(aAddress, /* aAdded */ true); Get<Mdns>().HandleHostAddressEvent(aAddress, /* aAdded */ true);
exit: exit:
return; return;
@@ -83,7 +80,7 @@ void InfraIf::RemoveAddress(const Ip6::Address &aAddress)
{ {
mAddresses[index] = *mAddresses.Back(); mAddresses[index] = *mAddresses.Back();
mAddresses.PopBack(); mAddresses.PopBack();
mNode->mMdns.HandleHostAddressEvent(aAddress, /* aAdded */ false); Get<Mdns>().HandleHostAddressEvent(aAddress, /* aAdded */ false);
break; break;
} }
} }
@@ -92,7 +89,7 @@ void InfraIf::RemoveAddress(const Ip6::Address &aAddress)
void InfraIf::RemoveAllAddresses(void) void InfraIf::RemoveAllAddresses(void)
{ {
mAddresses.Clear(); mAddresses.Clear();
mNode->mMdns.HandleHostAddressRemoveAll(); Get<Mdns>().HandleHostAddressRemoveAll();
} }
const Ip6::Address *InfraIf::FindAddress(const char *aPrefix) const const Ip6::Address *InfraIf::FindAddress(const char *aPrefix) const
@@ -153,7 +150,7 @@ exit:
void InfraIf::SendIcmp6Nd(const Ip6::Address &aDestAddress, const uint8_t *aBuffer, uint16_t aBufferLength) void InfraIf::SendIcmp6Nd(const Ip6::Address &aDestAddress, const uint8_t *aBuffer, uint16_t aBufferLength)
{ {
Message *message = GetNode().Get<MessagePool>().Allocate(Message::kTypeIp6); Message *message = Get<MessagePool>().Allocate(Message::kTypeIp6);
Ip6::Header ip6Header; Ip6::Header ip6Header;
VerifyOrQuit(message != nullptr); VerifyOrQuit(message != nullptr);
@@ -298,14 +295,14 @@ void InfraIf::HandlePrefixInfoOption(const Ip6::Nd::PrefixInfoOption &aPio)
if (HasAddress(address)) if (HasAddress(address))
{ {
RemoveAddress(address); RemoveAddress(address);
Log("Node %lu (%s) removed address %s from RA (lifetime 0)", ToUlong(GetNode().GetId()), Log("Node %lu (%s) removed address %s from RA (lifetime 0)", ToUlong(GetInstance().GetId()),
GetNode().GetName(), address.ToString().AsCString()); Get<Node>().GetName(), address.ToString().AsCString());
} }
ExitNow(); ExitNow();
} }
AddAddress(address); AddAddress(address);
Log("Node %lu (%s) auto-configured address %s from RA", ToUlong(GetNode().GetId()), GetNode().GetName(), Log("Node %lu (%s) auto-configured address %s from RA", ToUlong(GetInstance().GetId()), Get<Node>().GetName(),
address.ToString().AsCString()); address.ToString().AsCString());
exit: exit:
@@ -342,7 +339,7 @@ void InfraIf::SendEchoRequest(const Ip6::Address &aSrcAddress,
Ip6::Header ip6Header; Ip6::Header ip6Header;
Ip6::Icmp::Header icmpHeader; Ip6::Icmp::Header icmpHeader;
message = GetNode().Get<Ip6::Ip6>().NewMessage(); message = Get<Ip6::Ip6>().NewMessage();
VerifyOrQuit(message != nullptr); VerifyOrQuit(message != nullptr);
ip6Header.Clear(); ip6Header.Clear();
@@ -411,7 +408,6 @@ void InfraIf::SendUdp(const Ip6::Address &aSrcAddress,
void InfraIf::Receive(Message &aMessage) void InfraIf::Receive(Message &aMessage)
{ {
Node &node = GetNode();
Ip6::Headers headers; Ip6::Headers headers;
aMessage.SetOffset(0); aMessage.SetOffset(0);
@@ -419,7 +415,7 @@ void InfraIf::Receive(Message &aMessage)
if (headers.IsIcmp6() && (headers.GetDestinationAddress() == Ip6::Address::GetLinkLocalAllNodesMulticast() || if (headers.IsIcmp6() && (headers.GetDestinationAddress() == Ip6::Address::GetLinkLocalAllNodesMulticast() ||
headers.GetDestinationAddress() == Ip6::Address::GetLinkLocalAllRoutersMulticast() || headers.GetDestinationAddress() == Ip6::Address::GetLinkLocalAllRoutersMulticast() ||
node.mInfraIf.HasAddress(headers.GetDestinationAddress()))) HasAddress(headers.GetDestinationAddress())))
{ {
switch (headers.GetIcmpHeader().GetType()) switch (headers.GetIcmpHeader().GetType())
{ {
@@ -433,9 +429,9 @@ void InfraIf::Receive(Message &aMessage)
SuccessOrQuit(payload.SetFrom(aMessage, offset, aMessage.GetLength() - offset)); SuccessOrQuit(payload.SetFrom(aMessage, offset, aMessage.GetLength() - offset));
otPlatInfraIfRecvIcmp6Nd(&node.GetInstance(), mIfIndex, &headers.GetSourceAddress(), payload.GetBytes(), otPlatInfraIfRecvIcmp6Nd(&GetInstance(), mIfIndex, &headers.GetSourceAddress(), payload.GetBytes(),
payload.GetLength()); payload.GetLength());
node.mInfraIf.ProcessIcmp6Nd(headers.GetSourceAddress(), payload.GetBytes(), payload.GetLength()); Get<InfraIf>().ProcessIcmp6Nd(headers.GetSourceAddress(), payload.GetBytes(), payload.GetLength());
ExitNow(); ExitNow();
} }
@@ -454,7 +450,7 @@ void InfraIf::Receive(Message &aMessage)
if (headers.IsUdp() && headers.GetDestinationPort() == Mdns::kUdpPort) if (headers.IsUdp() && headers.GetDestinationPort() == Mdns::kUdpPort)
{ {
if (headers.GetDestinationAddress().IsMulticast() || node.mInfraIf.HasAddress(headers.GetDestinationAddress())) if (headers.GetDestinationAddress().IsMulticast() || HasAddress(headers.GetDestinationAddress()))
{ {
Mdns::AddressInfo senderAddress; Mdns::AddressInfo senderAddress;
Message *payload = aMessage.Clone<kNoReservedHeader>(); Message *payload = aMessage.Clone<kNoReservedHeader>();
@@ -466,17 +462,16 @@ void InfraIf::Receive(Message &aMessage)
senderAddress.mPort = headers.GetSourcePort(); senderAddress.mPort = headers.GetSourcePort();
senderAddress.mInfraIfIndex = Mdns::kInfraIfIndex; senderAddress.mInfraIfIndex = Mdns::kInfraIfIndex;
node.mMdns.Receive(node.GetInstance(), *payload, !headers.GetDestinationAddress().IsMulticast(), Get<Mdns>().Receive(GetInstance(), *payload, !headers.GetDestinationAddress().IsMulticast(), senderAddress);
senderAddress);
payload->Free(); payload->Free();
} }
ExitNow(); ExitNow();
} }
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
if (headers.IsUdp() && headers.GetDestinationPort() == node.mTrel.mUdpPort && node.mTrel.mEnabled) if (headers.IsUdp() && headers.GetDestinationPort() == Get<Trel>().mUdpPort && Get<Trel>().mEnabled)
{ {
if (headers.GetDestinationAddress().IsMulticast() || node.mInfraIf.HasAddress(headers.GetDestinationAddress())) if (headers.GetDestinationAddress().IsMulticast() || HasAddress(headers.GetDestinationAddress()))
{ {
Ip6::SockAddr senderAddr; Ip6::SockAddr senderAddr;
Heap::Data payload; Heap::Data payload;
@@ -487,7 +482,7 @@ void InfraIf::Receive(Message &aMessage)
SuccessOrQuit( SuccessOrQuit(
payload.SetFrom(aMessage, offset, headers.GetUdpHeader().GetLength() - sizeof(Ip6::Udp::Header))); payload.SetFrom(aMessage, offset, headers.GetUdpHeader().GetLength() - sizeof(Ip6::Udp::Header)));
node.mTrel.Receive(node.GetInstance(), payload, senderAddr); Get<Trel>().Receive(GetInstance(), payload, senderAddr);
} }
ExitNow(); ExitNow();
} }
@@ -497,7 +492,7 @@ void InfraIf::Receive(Message &aMessage)
if (headers.IsUdp() && headers.GetDestinationPort() == UpstreamDns::kDnsPort) if (headers.IsUdp() && headers.GetDestinationPort() == UpstreamDns::kDnsPort)
{ {
aMessage.SetOffset(sizeof(Ip6::Header) + sizeof(Ip6::Udp::Header)); aMessage.SetOffset(sizeof(Ip6::Header) + sizeof(Ip6::Udp::Header));
if (node.mUpstreamDns.HandleUpstreamDnsResponse(headers.GetSourceAddress(), aMessage)) if (Get<UpstreamDns>().HandleUpstreamDnsResponse(headers.GetSourceAddress(), aMessage))
{ {
ExitNow(); ExitNow();
} }
@@ -505,7 +500,7 @@ void InfraIf::Receive(Message &aMessage)
} }
#endif #endif
if (headers.IsUdp() && node.mUdp.HandleReceive(aMessage, headers)) if (headers.IsUdp() && Get<Udp>().HandleReceive(aMessage, headers))
{ {
ExitNow(); ExitNow();
} }
@@ -519,7 +514,7 @@ void InfraIf::Receive(Message &aMessage)
messageInfo.SetSockPort(headers.GetDestinationPort()); messageInfo.SetSockPort(headers.GetDestinationPort());
aMessage.SetOffset(sizeof(Ip6::Header) + sizeof(Ip6::Udp::Header)); aMessage.SetOffset(sizeof(Ip6::Header) + sizeof(Ip6::Udp::Header));
if (mUdpHook(node.GetInstance(), aMessage, messageInfo)) if (mUdpHook(GetInstance(), aMessage, messageInfo))
{ {
ExitNow(); ExitNow();
} }
@@ -532,14 +527,14 @@ void InfraIf::Receive(Message &aMessage)
OwnedPtr<Message> messagePtr; OwnedPtr<Message> messagePtr;
Ip6::Header updatedHeader = headers.GetIp6Header(); Ip6::Header updatedHeader = headers.GetIp6Header();
VerifyOrExit(!node.mInfraIf.HasAddress(headers.GetSourceAddress())); VerifyOrExit(!HasAddress(headers.GetSourceAddress()));
VerifyOrExit(!node.Get<NetworkData::Leader>().IsOnMesh(headers.GetSourceAddress())); VerifyOrExit(!Get<NetworkData::Leader>().IsOnMesh(headers.GetSourceAddress()));
#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE
if (headers.GetDestinationAddress().IsMulticastLargerThanRealmLocal()) if (headers.GetDestinationAddress().IsMulticastLargerThanRealmLocal())
{ {
VerifyOrExit(node.Get<BackboneRouter::Local>().IsPrimary()); VerifyOrExit(Get<BackboneRouter::Local>().IsPrimary());
VerifyOrExit(node.Get<BackboneRouter::MulticastListenersTable>().Has(headers.GetDestinationAddress())); VerifyOrExit(Get<BackboneRouter::MulticastListenersTable>().Has(headers.GetDestinationAddress()));
} }
#endif #endif
@@ -553,7 +548,7 @@ void InfraIf::Receive(Message &aMessage)
messagePtr->SetOrigin(Message::kOriginHostUntrusted); messagePtr->SetOrigin(Message::kOriginHostUntrusted);
messagePtr->SetLoopbackToHostAllowed(false); messagePtr->SetLoopbackToHostAllowed(false);
IgnoreError(node.Get<Ip6::Ip6>().SendRaw(messagePtr.PassOwnership())); IgnoreError(Get<Ip6::Ip6>().SendRaw(messagePtr.PassOwnership()));
} }
exit: exit:
@@ -562,13 +557,12 @@ exit:
void InfraIf::HandleEchoRequest(const Ip6::Header &aHeader, Message &aMessage) void InfraIf::HandleEchoRequest(const Ip6::Header &aHeader, Message &aMessage)
{ {
Node &node = GetNode();
Message *replyMessage; Message *replyMessage;
Ip6::Header replyHeader; Ip6::Header replyHeader;
Ip6::Icmp::Header replyIcmp; Ip6::Icmp::Header replyIcmp;
uint16_t payloadLen = aMessage.GetLength() - sizeof(Ip6::Header); uint16_t payloadLen = aMessage.GetLength() - sizeof(Ip6::Header);
replyMessage = node.Get<MessagePool>().Allocate(Message::kTypeIp6); replyMessage = Get<MessagePool>().Allocate(Message::kTypeIp6);
VerifyOrQuit(replyMessage != nullptr); VerifyOrQuit(replyMessage != nullptr);
SuccessOrQuit(replyMessage->SetLength(payloadLen)); SuccessOrQuit(replyMessage->SetLength(payloadLen));
@@ -610,19 +604,7 @@ void InfraIf::GetLinkLayerAddress(LinkLayerAddress &aLinkLayerAddress) const
ClearAllBytes(aLinkLayerAddress); ClearAllBytes(aLinkLayerAddress);
aLinkLayerAddress.mLength = 6; aLinkLayerAddress.mLength = 6;
aLinkLayerAddress.mAddress[0] = 0x02; aLinkLayerAddress.mAddress[0] = 0x02;
BigEndian::WriteUint32(mNodeId, &aLinkLayerAddress.mAddress[2]); BigEndian::WriteUint32(GetInstance().GetId(), &aLinkLayerAddress.mAddress[2]);
}
Node &InfraIf::GetNode(void)
{
OT_ASSERT(mNode != nullptr);
return *mNode;
}
const Node &InfraIf::GetNode(void) const
{
OT_ASSERT(mNode != nullptr);
return *mNode;
} }
extern "C" { extern "C" {
+2 -7
View File
@@ -36,14 +36,14 @@ namespace Nexus {
class Node; class Node;
class InfraIf class InfraIf : public InstanceLocator
{ {
public: public:
using LinkLayerAddress = BorderRouter::InfraIf::LinkLayerAddress; using LinkLayerAddress = BorderRouter::InfraIf::LinkLayerAddress;
explicit InfraIf(Instance &aInstance); explicit InfraIf(Instance &aInstance);
void Init(Node &aNode); void AfterInit(void);
bool IsInitialized(void) const { return mIfIndex != 0; } bool IsInitialized(void) const { return mIfIndex != 0; }
@@ -88,9 +88,6 @@ public:
typedef bool (*UdpHook)(Instance &aInstance, Message &aMessage, const Ip6::MessageInfo &aMessageInfo); typedef bool (*UdpHook)(Instance &aInstance, Message &aMessage, const Ip6::MessageInfo &aMessageInfo);
void SetUdpHook(UdpHook aHook) { mUdpHook = aHook; } void SetUdpHook(UdpHook aHook) { mUdpHook = aHook; }
Node &GetNode(void);
const Node &GetNode(void) const;
MessageQueue mPendingTxQueue; MessageQueue mPendingTxQueue;
private: private:
@@ -103,8 +100,6 @@ private:
void HandleRaTimer(void); void HandleRaTimer(void);
Node *mNode;
uint32_t mNodeId;
uint32_t mIfIndex; uint32_t mIfIndex;
Heap::Array<Ip6::Address> mAddresses; Heap::Array<Ip6::Address> mAddresses;
Callback<EchoReplyHandler> mEchoReplyCallback; Callback<EchoReplyHandler> mEchoReplyCallback;
+6 -1
View File
@@ -180,7 +180,12 @@ inline Node &AsNode(otInstance *aInstance) { return Node::From(aInstance); }
} // namespace Nexus } // namespace Nexus
template <> inline Nexus::InfraIf &Instance::Get(void) { return static_cast<Nexus::Node *>(this)->mInfraIf; } template <> inline Nexus::Node &Instance::Get(void) { return Nexus::AsNode(this); }
template <> inline Nexus::InfraIf &Instance::Get(void) { return static_cast<Nexus::Node *>(this)->mInfraIf; }
template <> inline Nexus::Udp &Instance::Get(void) { return static_cast<Nexus::Node *>(this)->mUdp; }
template <> inline Nexus::Trel &Instance::Get(void) { return static_cast<Nexus::Node *>(this)->mTrel; }
template <> inline Nexus::Mdns &Instance::Get(void) { return static_cast<Nexus::Node *>(this)->mMdns; }
template <> inline Nexus::UpstreamDns &Instance::Get(void) { return static_cast<Nexus::Node *>(this)->mUpstreamDns; }
} // namespace ot } // namespace ot