Skip to content

Commit 2dba489

Browse files
committed
drivers: net: loopback: Allow tests to control address swapping
Some of the network tests require that source and destination addresses are not swapped so allow test to control the address swapping from the test. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent b027710 commit 2dba489

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

drivers/net/loopback.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
2525

2626
#include <zephyr/net/dummy.h>
2727

28+
/* Allow network tests to control the IP addresses swapping */
29+
#if defined(CONFIG_NET_TEST)
30+
static bool loopback_dont_swap_addresses;
31+
32+
void loopback_enable_address_swap(bool swap_addresses)
33+
{
34+
loopback_dont_swap_addresses = !swap_addresses;
35+
}
36+
#endif /* CONFIG_NET_TEST */
37+
2838
int loopback_dev_init(const struct device *dev)
2939
{
3040
ARG_UNUSED(dev);
@@ -123,17 +133,22 @@ static int loopback_send(const struct device *dev, struct net_pkt *pkt)
123133

124134
/* We need to swap the IP addresses because otherwise
125135
* the packet will be dropped.
136+
*
137+
* Some of the network tests require that addresses are not swapped so allow
138+
* the test to control this remotely.
126139
*/
127-
if (net_pkt_family(pkt) == AF_INET6) {
128-
net_ipv6_addr_copy_raw(NET_IPV6_HDR(cloned)->src,
129-
NET_IPV6_HDR(pkt)->dst);
130-
net_ipv6_addr_copy_raw(NET_IPV6_HDR(cloned)->dst,
131-
NET_IPV6_HDR(pkt)->src);
132-
} else {
133-
net_ipv4_addr_copy_raw(NET_IPV4_HDR(cloned)->src,
134-
NET_IPV4_HDR(pkt)->dst);
135-
net_ipv4_addr_copy_raw(NET_IPV4_HDR(cloned)->dst,
136-
NET_IPV4_HDR(pkt)->src);
140+
if (!COND_CODE_1(CONFIG_NET_TEST, (loopback_dont_swap_addresses), (false))) {
141+
if (net_pkt_family(pkt) == AF_INET6) {
142+
net_ipv6_addr_copy_raw(NET_IPV6_HDR(cloned)->src,
143+
NET_IPV6_HDR(pkt)->dst);
144+
net_ipv6_addr_copy_raw(NET_IPV6_HDR(cloned)->dst,
145+
NET_IPV6_HDR(pkt)->src);
146+
} else {
147+
net_ipv4_addr_copy_raw(NET_IPV4_HDR(cloned)->src,
148+
NET_IPV4_HDR(pkt)->dst);
149+
net_ipv4_addr_copy_raw(NET_IPV4_HDR(cloned)->dst,
150+
NET_IPV4_HDR(pkt)->src);
151+
}
137152
}
138153

139154
res = net_recv_data(net_pkt_iface(cloned), cloned);

subsys/net/ip/net_private.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ extern void mdns_init_responder(void);
142142
static inline void mdns_init_responder(void) { }
143143
#endif /* CONFIG_MDNS_RESPONDER */
144144

145+
#if defined(CONFIG_NET_TEST)
146+
extern void loopback_enable_address_swap(bool swap_addresses);
147+
#endif /* CONFIG_NET_TEST */
148+
145149
#if defined(CONFIG_NET_NATIVE)
146150
enum net_verdict net_ipv4_input(struct net_pkt *pkt, bool is_loopback);
147151
enum net_verdict net_ipv6_input(struct net_pkt *pkt, bool is_loopback);

0 commit comments

Comments
 (0)