Skip to content

Commit a2aea74

Browse files
committed
net_core: Decide about l2-processing based on l2_processed-flag
Use the l2_processed-flag to decide whether a network packet needs to be processed by an L2-handler. This could be used in the future to requeue packets for later processing by a different traffic class queue. Introduce a loopback-flag to aoivd passing the information around as argument. Signed-off-by: Cla Mattia Galliard <clamattia@gmail.com>
1 parent 322da1d commit a2aea74

File tree

14 files changed

+65
-56
lines changed

14 files changed

+65
-56
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/ipv4_fragment.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ static void reassemble_packet(struct net_ipv4_reassembly *reass)
206206
net_pkt_set_data(pkt, &ipv4_access);
207207
net_pkt_set_ip_reassembled(pkt, true);
208208

209+
/* If the packet is reassembled, then do not pass it to L2 as the
210+
* packet does not have link layer headers in it.
211+
*/
212+
net_pkt_set_l2_processed(pkt, true);
213+
209214
LOG_DBG("New pkt %p IPv4 len is %zd bytes", pkt, net_pkt_get_len(pkt));
210215

211216
/* We need to use the queue when feeding the packet back into the

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/ipv6_fragment.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ static void reassemble_packet(struct net_ipv6_reassembly *reass)
340340
net_pkt_set_data(pkt, &ipv6_access);
341341
net_pkt_set_ip_reassembled(pkt, true);
342342

343+
/* If the packet is reassembled, then do not pass it to L2 as the
344+
* packet does not have link layer headers in it.
345+
*/
346+
net_pkt_set_l2_processed(pkt, true);
347+
343348
NET_DBG("New pkt %p IPv6 len is %d bytes", pkt,
344349
len + NET_IPV6H_LEN);
345350

subsys/net/ip/net_core.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,16 @@ 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;
71-
bool locally_routed = false;
72-
73-
net_pkt_set_l2_processed(pkt, false);
7470

7571
/* Initial call will forward packets to SOCK_RAW packet sockets. */
7672
ret = net_packet_socket_input(pkt, ETH_P_ALL);
7773
if (ret != NET_CONTINUE) {
7874
return ret;
7975
}
8076

81-
/* If the packet is routed back to us when we have reassembled an IPv4 or IPv6 packet,
82-
* then do not pass it to L2 as the packet does not have link layer headers in it.
83-
*/
84-
if (net_pkt_is_ip_reassembled(pkt)) {
85-
locally_routed = true;
86-
}
87-
8877
/* If there is no data, then drop the packet. */
8978
if (!pkt->frags) {
9079
NET_DBG("Corrupted packet (frags %p)", pkt->frags);
@@ -93,8 +82,9 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
9382
return NET_DROP;
9483
}
9584

96-
if (!is_loopback && !locally_routed) {
85+
if (!net_pkt_is_l2_processed(pkt)) {
9786
ret = net_if_recv_data(net_pkt_iface(pkt), pkt);
87+
net_pkt_set_l2_processed(pkt, true);
9888
if (ret != NET_CONTINUE) {
9989
if (ret == NET_DROP) {
10090
NET_DBG("Packet %p discarded by L2", pkt);
@@ -106,8 +96,6 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
10696
}
10797
}
10898

109-
net_pkt_set_l2_processed(pkt, true);
110-
11199
/* L2 has modified the buffer starting point, it is easier
112100
* to re-initialize the cursor rather than updating it.
113101
*/
@@ -131,9 +119,9 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
131119
uint8_t vtc_vhl = NET_IPV6_HDR(pkt)->vtc & 0xf0;
132120

133121
if (IS_ENABLED(CONFIG_NET_IPV6) && vtc_vhl == 0x60) {
134-
return net_ipv6_input(pkt, is_loopback);
122+
return net_ipv6_input(pkt);
135123
} else if (IS_ENABLED(CONFIG_NET_IPV4) && vtc_vhl == 0x40) {
136-
return net_ipv4_input(pkt, is_loopback);
124+
return net_ipv4_input(pkt);
137125
}
138126

139127
NET_DBG("Unknown IP family packet (0x%x)", NET_IPV6_HDR(pkt)->vtc & 0xf0);
@@ -148,10 +136,10 @@ static inline enum net_verdict process_data(struct net_pkt *pkt,
148136
return NET_DROP;
149137
}
150138

151-
static void processing_data(struct net_pkt *pkt, bool is_loopback)
139+
static void processing_data(struct net_pkt *pkt)
152140
{
153141
again:
154-
switch (process_data(pkt, is_loopback)) {
142+
switch (process_data(pkt)) {
155143
case NET_CONTINUE:
156144
if (IS_ENABLED(CONFIG_NET_L2_VIRTUAL)) {
157145
/* If we have a tunneling packet, feed it back
@@ -421,7 +409,9 @@ int net_try_send_data(struct net_pkt *pkt, k_timeout_t timeout)
421409
* to RX processing.
422410
*/
423411
NET_DBG("Loopback pkt %p back to us", pkt);
424-
processing_data(pkt, true);
412+
net_pkt_set_loopback(pkt, true);
413+
net_pkt_set_l2_processed(pkt, true);
414+
processing_data(pkt);
425415
ret = 0;
426416
goto err;
427417
}
@@ -481,7 +471,6 @@ int net_try_send_data(struct net_pkt *pkt, k_timeout_t timeout)
481471

482472
static void net_rx(struct net_if *iface, struct net_pkt *pkt)
483473
{
484-
bool is_loopback = false;
485474
size_t pkt_len;
486475

487476
pkt_len = net_pkt_get_len(pkt);
@@ -493,12 +482,13 @@ static void net_rx(struct net_if *iface, struct net_pkt *pkt)
493482
if (IS_ENABLED(CONFIG_NET_LOOPBACK)) {
494483
#ifdef CONFIG_NET_L2_DUMMY
495484
if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
496-
is_loopback = true;
485+
net_pkt_set_loopback(pkt, true);
486+
net_pkt_set_l2_processed(pkt, true);
497487
}
498488
#endif
499489
}
500490

501-
processing_data(pkt, is_loopback);
491+
processing_data(pkt);
502492

503493
net_print_statistics();
504494
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 */

0 commit comments

Comments
 (0)