Skip to content

Commit 701deeb

Browse files
committed
net_pkt: Store is_loopback info in packet meta-data
Store the flag in the packet meta-data so that processing may be deferred if necessary. Signed-off-by: Cla Mattia Galliard <clamattia@gmail.com>
1 parent f52d71c commit 701deeb

File tree

12 files changed

+52
-44
lines changed

12 files changed

+52
-44
lines changed

include/zephyr/net/net_pkt.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ struct net_pkt {
237237
uint8_t chksum_done : 1; /* Checksum has already been computed for
238238
* the packet.
239239
*/
240+
uint8_t loopback : 1; /* Packet is a loop back packet. */
240241
#if defined(CONFIG_NET_IP_FRAGMENT)
241242
uint8_t ip_reassembled : 1; /* Packet is a reassembled IP packet. */
242243
#endif
@@ -1020,6 +1021,17 @@ static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
10201021
}
10211022
#endif /* CONFIG_NET_IPV6_FRAGMENT */
10221023

1024+
static inline bool net_pkt_is_loopback(struct net_pkt *pkt)
1025+
{
1026+
return !!(pkt->loopback);
1027+
}
1028+
1029+
static inline void net_pkt_set_loopback(struct net_pkt *pkt,
1030+
bool loopback)
1031+
{
1032+
pkt->loopback = loopback;
1033+
}
1034+
10231035
#if defined(CONFIG_NET_IP_FRAGMENT)
10241036
static inline bool net_pkt_is_ip_reassembled(struct net_pkt *pkt)
10251037
{

subsys/net/ip/ipv4.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int net_ipv4_parse_hdr_options(struct net_pkt *pkt,
240240
}
241241
#endif
242242

243-
enum net_verdict net_ipv4_input(struct net_pkt *pkt, bool is_loopback)
243+
enum net_verdict net_ipv4_input(struct net_pkt *pkt)
244244
{
245245
NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(ipv4_access, struct net_ipv4_hdr);
246246
NET_PKT_DATA_ACCESS_DEFINE(udp_access, struct net_udp_hdr);
@@ -301,7 +301,7 @@ enum net_verdict net_ipv4_input(struct net_pkt *pkt, bool is_loopback)
301301
net_pkt_update_length(pkt, pkt_len);
302302
}
303303

304-
if (!is_loopback) {
304+
if (!net_pkt_is_loopback(pkt)) {
305305
if (net_ipv4_is_addr_loopback_raw(hdr->dst) ||
306306
net_ipv4_is_addr_loopback_raw(hdr->src)) {
307307
NET_DBG("DROP: localhost packet");

subsys/net/ip/ipv6.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ static inline bool is_src_non_tentative_itself(const uint8_t *src)
475475
return false;
476476
}
477477

478-
enum net_verdict net_ipv6_input(struct net_pkt *pkt, bool is_loopback)
478+
enum net_verdict net_ipv6_input(struct net_pkt *pkt)
479479
{
480480
NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(ipv6_access, struct net_ipv6_hdr);
481481
NET_PKT_DATA_ACCESS_DEFINE(udp_access, struct net_udp_hdr);
@@ -537,7 +537,7 @@ enum net_verdict net_ipv6_input(struct net_pkt *pkt, bool is_loopback)
537537
goto drop;
538538
}
539539

540-
if (!is_loopback) {
540+
if (!net_pkt_is_loopback(pkt)) {
541541
if (net_ipv6_is_addr_loopback_raw(hdr->dst) ||
542542
net_ipv6_is_addr_loopback_raw(hdr->src)) {
543543
NET_DBG("DROP: ::1 packet");
@@ -631,7 +631,7 @@ enum net_verdict net_ipv6_input(struct net_pkt *pkt, bool is_loopback)
631631
}
632632

633633
if ((IS_ENABLED(CONFIG_NET_ROUTING) || IS_ENABLED(CONFIG_NET_ROUTE_MCAST)) &&
634-
!is_loopback && is_src_non_tentative_itself(hdr->src)) {
634+
!net_pkt_is_loopback(pkt) && is_src_non_tentative_itself(hdr->src)) {
635635
NET_DBG("DROP: src addr is %s", "mine");
636636
goto drop;
637637
}

subsys/net/ip/net_core.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ LOG_MODULE_REGISTER(net_core, CONFIG_NET_CORE_LOG_LEVEL);
6464
#include "net_stats.h"
6565

6666
#if defined(CONFIG_NET_NATIVE)
67-
static inline enum net_verdict process_data(struct net_pkt *pkt,
68-
bool is_loopback)
67+
static inline enum net_verdict process_data(struct net_pkt *pkt)
6968
{
7069
int ret;
7170
bool locally_routed = false;
@@ -93,7 +92,7 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
9392
return NET_DROP;
9493
}
9594

96-
if (!is_loopback && !locally_routed) {
95+
if (!net_pkt_is_loopback(pkt) && !locally_routed) {
9796
ret = net_if_recv_data(net_pkt_iface(pkt), pkt);
9897
if (ret != NET_CONTINUE) {
9998
if (ret == NET_DROP) {
@@ -131,9 +130,9 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
131130
uint8_t vtc_vhl = NET_IPV6_HDR(pkt)->vtc & 0xf0;
132131

133132
if (IS_ENABLED(CONFIG_NET_IPV6) && vtc_vhl == 0x60) {
134-
return net_ipv6_input(pkt, is_loopback);
133+
return net_ipv6_input(pkt);
135134
} else if (IS_ENABLED(CONFIG_NET_IPV4) && vtc_vhl == 0x40) {
136-
return net_ipv4_input(pkt, is_loopback);
135+
return net_ipv4_input(pkt);
137136
}
138137

139138
NET_DBG("Unknown IP family packet (0x%x)", NET_IPV6_HDR(pkt)->vtc & 0xf0);
@@ -148,10 +147,10 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
148147
return NET_DROP;
149148
}
150149

151-
static void processing_data(struct net_pkt *pkt, bool is_loopback)
150+
static void processing_data(struct net_pkt *pkt)
152151
{
153152
again:
154-
switch (process_data(pkt, is_loopback)) {
153+
switch (process_data(pkt)) {
155154
case NET_CONTINUE:
156155
if (IS_ENABLED(CONFIG_NET_L2_VIRTUAL)) {
157156
/* If we have a tunneling packet, feed it back
@@ -421,7 +420,8 @@ int net_try_send_data(struct net_pkt *pkt, k_timeout_t timeout)
421420
* to RX processing.
422421
*/
423422
NET_DBG("Loopback pkt %p back to us", pkt);
424-
processing_data(pkt, true);
423+
net_pkt_set_loopback(pkt, true);
424+
processing_data(pkt);
425425
ret = 0;
426426
goto err;
427427
}
@@ -481,7 +481,6 @@ int net_try_send_data(struct net_pkt *pkt, k_timeout_t timeout)
481481

482482
static void net_rx(struct net_if *iface, struct net_pkt *pkt)
483483
{
484-
bool is_loopback = false;
485484
size_t pkt_len;
486485

487486
pkt_len = net_pkt_get_len(pkt);
@@ -493,12 +492,12 @@ static void net_rx(struct net_if *iface, struct net_pkt *pkt)
493492
if (IS_ENABLED(CONFIG_NET_LOOPBACK)) {
494493
#ifdef CONFIG_NET_L2_DUMMY
495494
if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
496-
is_loopback = true;
495+
net_pkt_set_loopback(pkt, true);
497496
}
498497
#endif
499498
}
500499

501-
processing_data(pkt, is_loopback);
500+
processing_data(pkt);
502501

503502
net_print_statistics();
504503
net_pkt_print();

subsys/net/ip/net_pkt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,7 @@ static void clone_pkt_attributes(struct net_pkt *pkt, struct net_pkt *clone_pkt)
20472047
net_pkt_set_rx_timestamping(clone_pkt, net_pkt_is_rx_timestamping(pkt));
20482048
net_pkt_set_forwarding(clone_pkt, net_pkt_forwarding(pkt));
20492049
net_pkt_set_chksum_done(clone_pkt, net_pkt_is_chksum_done(pkt));
2050+
net_pkt_set_loopback(pkt, net_pkt_is_loopback(pkt));
20502051
net_pkt_set_ip_reassembled(pkt, net_pkt_is_ip_reassembled(pkt));
20512052
net_pkt_set_cooked_mode(clone_pkt, net_pkt_is_cooked_mode(pkt));
20522053
net_pkt_set_ipv4_pmtu(clone_pkt, net_pkt_ipv4_pmtu(pkt));

subsys/net/ip/net_private.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,21 @@ extern void loopback_enable_address_swap(bool swap_addresses);
177177
#endif /* CONFIG_NET_TEST */
178178

179179
#if defined(CONFIG_NET_NATIVE)
180-
enum net_verdict net_ipv4_input(struct net_pkt *pkt, bool is_loopback);
181-
enum net_verdict net_ipv6_input(struct net_pkt *pkt, bool is_loopback);
180+
enum net_verdict net_ipv4_input(struct net_pkt *pkt);
181+
enum net_verdict net_ipv6_input(struct net_pkt *pkt);
182182
extern void net_tc_tx_init(void);
183183
extern void net_tc_rx_init(void);
184184
#else
185-
static inline enum net_verdict net_ipv4_input(struct net_pkt *pkt,
186-
bool is_loopback)
185+
static inline enum net_verdict net_ipv4_input(struct net_pkt *pkt)
187186
{
188187
ARG_UNUSED(pkt);
189-
ARG_UNUSED(is_loopback);
190188

191189
return NET_CONTINUE;
192190
}
193191

194-
static inline enum net_verdict net_ipv6_input(struct net_pkt *pkt,
195-
bool is_loopback)
192+
static inline enum net_verdict net_ipv6_input(struct net_pkt *pkt)
196193
{
197194
ARG_UNUSED(pkt);
198-
ARG_UNUSED(is_loopback);
199195

200196
return NET_CONTINUE;
201197
}

subsys/net/l2/virtual/ipip/ipip.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ static enum net_verdict interface_recv(struct net_if *iface,
374374

375375
net_pkt_cursor_restore(pkt, &hdr_start);
376376

377-
return net_ipv6_input(pkt, false);
377+
return net_ipv6_input(pkt);
378378
}
379379

380380
if (IS_ENABLED(CONFIG_NET_IPV4) && net_pkt_family(pkt) == AF_INET) {
@@ -421,7 +421,7 @@ static enum net_verdict interface_recv(struct net_if *iface,
421421

422422
net_pkt_cursor_restore(pkt, &hdr_start);
423423

424-
return net_ipv4_input(pkt, false);
424+
return net_ipv4_input(pkt);
425425
}
426426

427427
return NET_CONTINUE;

tests/net/dhcpv6/src/main.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ ZTEST(dhcpv6_tests, test_input_reject_client_initiated_messages)
636636
set_generic_client_options);
637637
zassert_not_null(pkt, "Failed to create fake pkt");
638638

639-
result = net_ipv6_input(pkt, false);
639+
result = net_ipv6_input(pkt);
640640
zassert_equal(result, NET_DROP, "Should've drop the message");
641641

642642
net_pkt_unref(pkt);
@@ -719,7 +719,7 @@ ZTEST(dhcpv6_tests, test_input_advertise)
719719
set_advertise_options);
720720
zassert_not_null(pkt, "Failed to create pkt");
721721

722-
result = net_ipv6_input(pkt, false);
722+
result = net_ipv6_input(pkt);
723723

724724
switch (state) {
725725
case NET_DHCPV6_SOLICITING:
@@ -826,7 +826,7 @@ ZTEST(dhcpv6_tests, test_input_reply)
826826
set_reply_options);
827827
zassert_not_null(pkt, "Failed to create pkt");
828828

829-
result = net_ipv6_input(pkt, false);
829+
result = net_ipv6_input(pkt);
830830

831831
switch (state) {
832832
case NET_DHCPV6_CONFIRMING:
@@ -891,7 +891,7 @@ static void test_solicit_expect_request_send_reply(struct net_if *iface,
891891
set_reply_options);
892892
zassert_not_null(reply, "Failed to create pkt");
893893

894-
result = net_ipv6_input(reply, false);
894+
result = net_ipv6_input(reply);
895895
zassert_equal(result, NET_OK, "Message should've been processed");
896896

897897
/* Verify client state */
@@ -936,7 +936,7 @@ static void test_solicit_expect_solicit_send_advertise(struct net_if *iface,
936936
set_advertise_options);
937937
zassert_not_null(reply, "Failed to create pkt");
938938

939-
result = net_ipv6_input(reply, false);
939+
result = net_ipv6_input(reply);
940940
zassert_equal(result, NET_OK, "Message should've been processed");
941941

942942
/* Verify client state */
@@ -993,7 +993,7 @@ static void expect_request_send_reply(struct net_if *iface, struct net_pkt *pkt)
993993
set_reply_options);
994994
zassert_not_null(reply, "Failed to create pkt");
995995

996-
result = net_ipv6_input(reply, false);
996+
result = net_ipv6_input(reply);
997997
zassert_equal(result, NET_OK, "Message should've been processed");
998998

999999
k_sem_give(&test_ctx.exchange_complete_sem);
@@ -1013,7 +1013,7 @@ static void expect_solicit_send_advertise(struct net_if *iface, struct net_pkt *
10131013
set_advertise_options);
10141014
zassert_not_null(reply, "Failed to create pkt");
10151015

1016-
result = net_ipv6_input(reply, false);
1016+
result = net_ipv6_input(reply);
10171017
zassert_equal(result, NET_OK, "Message should've been processed");
10181018
}
10191019

@@ -1058,7 +1058,7 @@ static void test_confirm_expect_confirm_send_reply(struct net_if *iface,
10581058
set_reply_options);
10591059
zassert_not_null(reply, "Failed to create pkt");
10601060

1061-
result = net_ipv6_input(reply, false);
1061+
result = net_ipv6_input(reply);
10621062
zassert_equal(result, NET_OK, "Message should've been processed");
10631063

10641064
/* Verify client state */
@@ -1127,7 +1127,7 @@ static void test_rebind_expect_rebind_send_reply(struct net_if *iface,
11271127
set_reply_options);
11281128
zassert_not_null(reply, "Failed to create pkt");
11291129

1130-
result = net_ipv6_input(reply, false);
1130+
result = net_ipv6_input(reply);
11311131
zassert_equal(result, NET_OK, "Message should've been processed");
11321132

11331133
/* Verify client state */
@@ -1201,7 +1201,7 @@ static void test_renew_expect_renew_send_reply(struct net_if *iface,
12011201
set_reply_options);
12021202
zassert_not_null(reply, "Failed to create pkt");
12031203

1204-
result = net_ipv6_input(reply, false);
1204+
result = net_ipv6_input(reply);
12051205
zassert_equal(result, NET_OK, "Message should've been processed");
12061206

12071207
/* Verify client state */

tests/net/icmpv4/src/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ static void icmpv4_send_echo_req(void)
452452
zassert_true(false, "EchoRequest packet prep failed");
453453
}
454454

455-
if (net_ipv4_input(pkt, false)) {
455+
if (net_ipv4_input(pkt)) {
456456
net_pkt_unref(pkt);
457457
zassert_true(false, "Failed to send");
458458
}
@@ -474,7 +474,7 @@ static void icmpv4_send_echo_rep(void)
474474
zassert_true(false, "EchoReply packet prep failed");
475475
}
476476

477-
if (net_ipv4_input(pkt, false)) {
477+
if (net_ipv4_input(pkt)) {
478478
net_pkt_unref(pkt);
479479
zassert_true(false, "Failed to send");
480480
}
@@ -494,7 +494,7 @@ ZTEST(net_icmpv4, test_icmpv4_send_echo_req_opt)
494494
zassert_true(false, "EchoRequest with opts packet prep failed");
495495
}
496496

497-
if (net_ipv4_input(pkt, false)) {
497+
if (net_ipv4_input(pkt)) {
498498
net_pkt_unref(pkt);
499499
zassert_true(false, "Failed to send");
500500
}
@@ -510,7 +510,7 @@ ZTEST(net_icmpv4, test_send_echo_req_bad_opt)
510510
"EchoRequest with bad opts packet prep failed");
511511
}
512512

513-
if (net_ipv4_input(pkt, false)) {
513+
if (net_ipv4_input(pkt)) {
514514
net_pkt_unref(pkt);
515515
}
516516
}

tests/net/igmp/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ static void igmp_send_query(bool is_imgpv3)
596596
pkt = prepare_igmp_query(net_iface, is_imgpv3);
597597
zassert_not_null(pkt, "IGMPv2 query packet prep failed");
598598

599-
zassert_equal(net_ipv4_input(pkt, false), NET_OK, "Failed to send");
599+
zassert_equal(net_ipv4_input(pkt), NET_OK, "Failed to send");
600600

601601
zassert_ok(k_sem_take(&wait_data, K_MSEC(WAIT_TIME)), "Timeout while waiting query event");
602602

0 commit comments

Comments
 (0)