Skip to content

Commit 202fd30

Browse files
abtinkjwhui
authored andcommitted
[ip6] simplify DetermineAction() (openthread#11392)
This commit simplifies the `Ip6::DetermineAction()` method, which determines the appropriate actions (`forwardThread`, `forwardHost`, `receive`) for an IPv6 message based on its destination address and origin. - The code now uses `ExitNow()` to exit the method as soon as a specific action is determined. This avoids deeply nested `if/else` blocks and makes the control flow easier to understand. - Some negative conditional checks have been refactored into positive checks with early exits. For example, a condition like `if (!cond1 || !cond2)` that guarded further processing is now expressed as `if (cond1 && cond2) { ExitNow(); }`, making the logic more direct. - New comments have been added to clarify more complex checks and conditions within the method. - The `RouteLookup()` method has been removed and its logic inlined directly into `DetermineAction()`. This improves code readability and allows for clearer distinction between forwarding to a host due to Border Router functionality versus forwarding as a last resort when no specific route exists.
1 parent c5f77ae commit 202fd30

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

src/core/net/ip6.cpp

+56-46
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,8 @@ void Ip6::DetermineAction(const Message &aMessage,
10801080
// Determine `aForwardThread`, `aForwardHost` and `aReceive`
10811081
// based on the destination address and message origin.
10821082

1083+
uint16_t rloc16;
1084+
10831085
aForwardThread = false;
10841086
aForwardHost = false;
10851087
aReceive = false;
@@ -1088,6 +1090,9 @@ void Ip6::DetermineAction(const Message &aMessage,
10881090
{
10891091
// Destination is multicast
10901092

1093+
// Forward multicast message to thread unless we received it
1094+
// on Thread netif.
1095+
10911096
aForwardThread = !aMessage.IsOriginThreadNetif();
10921097

10931098
#if OPENTHREAD_FTD
@@ -1101,43 +1106,70 @@ void Ip6::DetermineAction(const Message &aMessage,
11011106
// Always forward multicast packets to host network stack
11021107
aForwardHost = true;
11031108

1109+
// If subscribed to the multicast address, receive if it is from the
1110+
// Thread netif or if multicast loop is allowed.
1111+
11041112
if ((aMessage.IsOriginThreadNetif() || aMessage.GetMulticastLoop()) &&
11051113
Get<ThreadNetif>().IsMulticastSubscribed(aHeader.GetDestination()))
11061114
{
11071115
aReceive = true;
11081116
}
1117+
1118+
ExitNow();
11091119
}
1110-
else
1120+
1121+
// Destination is unicast
1122+
1123+
if (Get<ThreadNetif>().HasUnicastAddress(aHeader.GetDestination()))
11111124
{
1112-
// Destination is unicast
1125+
aReceive = true;
1126+
ExitNow();
1127+
}
11131128

1114-
if (Get<ThreadNetif>().HasUnicastAddress(aHeader.GetDestination()))
1115-
{
1116-
aReceive = true;
1117-
}
1118-
else if (!aMessage.IsOriginThreadNetif() || !aHeader.GetDestination().IsLinkLocalUnicast())
1119-
{
1120-
if (aHeader.GetDestination().IsLinkLocalUnicast())
1121-
{
1122-
aForwardThread = true;
1123-
}
1124-
else if (IsOnLink(aHeader.GetDestination()))
1125-
{
1129+
if (aHeader.GetDestination().IsLinkLocalUnicast())
1130+
{
1131+
// Forward a message with a link-local destination address
1132+
// to thread, unless it is received on the Thread netif.
1133+
1134+
aForwardThread = !aMessage.IsOriginThreadNetif();
1135+
ExitNow();
1136+
}
1137+
1138+
if (IsOnLink(aHeader.GetDestination()))
1139+
{
11261140
#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE
1127-
aForwardThread = (!aMessage.IsLoopbackToHostAllowed() ||
1128-
!Get<BackboneRouter::Manager>().ShouldForwardDuaToBackbone(aHeader.GetDestination()));
1141+
aForwardThread = (!aMessage.IsLoopbackToHostAllowed() ||
1142+
!Get<BackboneRouter::Manager>().ShouldForwardDuaToBackbone(aHeader.GetDestination()));
1143+
aForwardHost = !aForwardThread;
11291144
#else
1130-
aForwardThread = true;
1145+
aForwardThread = true;
11311146
#endif
1132-
}
1133-
else if (RouteLookup(aHeader.GetSource(), aHeader.GetDestination()) == kErrorNone)
1134-
{
1135-
aForwardThread = true;
1136-
}
1147+
ExitNow();
1148+
}
11371149

1138-
aForwardHost = !aForwardThread;
1139-
}
1150+
if (Get<NetworkData::Leader>().RouteLookup(aHeader.GetSource(), aHeader.GetDestination(), rloc16) != kErrorNone)
1151+
{
1152+
// No route in mesh, forward to host (as a last resort).
1153+
LogNote("Failed to find valid route for: %s", aHeader.GetDestination().ToString().AsCString());
1154+
aForwardHost = true;
1155+
ExitNow();
11401156
}
1157+
1158+
// `RouteLookup()` found a destination within the mesh. If we are
1159+
// the destination (device is acting as a Border Router), forward
1160+
// it to the host. Otherwise, forward to Thread.
1161+
1162+
if (Get<Mle::Mle>().HasRloc16(rloc16))
1163+
{
1164+
aForwardHost = true;
1165+
}
1166+
else
1167+
{
1168+
aForwardThread = true;
1169+
}
1170+
1171+
exit:
1172+
return;
11411173
}
11421174

11431175
Error Ip6::HandleDatagram(OwnedPtr<Message> aMessagePtr, bool aIsReassembled)
@@ -1385,28 +1417,6 @@ bool Ip6::IsOnLink(const Address &aAddress) const
13851417
return isOnLink;
13861418
}
13871419

1388-
Error Ip6::RouteLookup(const Address &aSource, const Address &aDestination) const
1389-
{
1390-
Error error;
1391-
uint16_t rloc;
1392-
1393-
error = Get<NetworkData::Leader>().RouteLookup(aSource, aDestination, rloc);
1394-
1395-
if (error == kErrorNone)
1396-
{
1397-
if (rloc == Get<Mle::MleRouter>().GetRloc16())
1398-
{
1399-
error = kErrorNoRoute;
1400-
}
1401-
}
1402-
else
1403-
{
1404-
LogNote("Failed to find valid route for: %s", aDestination.ToString().AsCString());
1405-
}
1406-
1407-
return error;
1408-
}
1409-
14101420
#if OPENTHREAD_CONFIG_IP6_BR_COUNTERS_ENABLE
14111421

14121422
void Ip6::UpdateBorderRoutingCounters(const Header &aHeader, uint16_t aMessageLength, bool aIsInbound)

src/core/net/ip6.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ class Ip6 : public InstanceLocator, private NonCopyable
388388
OwnedPtr<Message> &aMessagePtr,
389389
uint8_t aIpProto,
390390
Message::Ownership aMessageOwnership);
391-
Error RouteLookup(const Address &aSource, const Address &aDestination) const;
392391
#if OPENTHREAD_CONFIG_IP6_BR_COUNTERS_ENABLE
393392
void UpdateBorderRoutingCounters(const Header &aHeader, uint16_t aMessageLength, bool aIsInbound);
394393
#endif

0 commit comments

Comments
 (0)