Skip to content

Commit db886f3

Browse files
committed
net_pkt: Store meta-information of processing in the net_pkt
Store a flag about which layer has already processed a packet in its meta information. This enables deferred processing of it.
1 parent c287e48 commit db886f3

File tree

2 files changed

+78
-25
lines changed

2 files changed

+78
-25
lines changed

include/zephyr/net/net_pkt.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,18 @@ struct net_pkt {
231231
* preserved. Useful only if
232232
* defined(CONFIG_NET_ETHERNET_BRIDGE).
233233
*/
234+
uint8_t raw_processed : 1; /* Set to 1 if this packet has already been
235+
* processed by SOCK_RAW
236+
*/
237+
uint8_t dgram_processed : 1; /* Set to 1 if this packet has already been
238+
* processed by SOCK_DGRAM
239+
*/
234240
uint8_t l2_processed : 1; /* Set to 1 if this packet has already been
235241
* processed by the L2
236242
*/
243+
uint8_t l3_processed : 1; /* Set to 1 if this packet has already been
244+
* processed by the L3
245+
*/
237246
uint8_t chksum_done : 1; /* Checksum has already been computed for
238247
* the packet.
239248
*/
@@ -554,6 +563,28 @@ static inline void net_pkt_set_l2_bridged(struct net_pkt *pkt, bool is_l2_bridge
554563
}
555564
}
556565

566+
static inline bool net_pkt_is_raw_processed(struct net_pkt *pkt)
567+
{
568+
return !!(pkt->raw_processed);
569+
}
570+
571+
static inline void net_pkt_set_raw_processed(struct net_pkt *pkt,
572+
bool is_raw_processed)
573+
{
574+
pkt->raw_processed = is_raw_processed;
575+
}
576+
577+
static inline bool net_pkt_is_dgram_processed(struct net_pkt *pkt)
578+
{
579+
return !!(pkt->dgram_processed);
580+
}
581+
582+
static inline void net_pkt_set_dgram_processed(struct net_pkt *pkt,
583+
bool is_dgram_processed)
584+
{
585+
pkt->dgram_processed = is_dgram_processed;
586+
}
587+
557588
static inline bool net_pkt_is_l2_processed(struct net_pkt *pkt)
558589
{
559590
return !!(pkt->l2_processed);
@@ -565,6 +596,17 @@ static inline void net_pkt_set_l2_processed(struct net_pkt *pkt,
565596
pkt->l2_processed = is_l2_processed;
566597
}
567598

599+
static inline bool net_pkt_is_l3_processed(struct net_pkt *pkt)
600+
{
601+
return !!(pkt->l3_processed);
602+
}
603+
604+
static inline void net_pkt_set_l3_processed(struct net_pkt *pkt,
605+
bool is_l3_processed)
606+
{
607+
pkt->l3_processed = is_l3_processed;
608+
}
609+
568610
static inline bool net_pkt_is_chksum_done(struct net_pkt *pkt)
569611
{
570612
return !!(pkt->chksum_done);

subsys/net/ip/net_core.c

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ static inline enum net_verdict process_data(struct net_pkt *pkt)
6868
{
6969
int ret;
7070

71-
net_packet_socket_input(pkt, ETH_P_ALL, SOCK_RAW);
71+
if (!net_pkt_is_raw_processed(pkt)) {
72+
net_pkt_set_raw_processed(pkt);
73+
net_packet_socket_input(pkt, ETH_P_ALL, SOCK_RAW);
74+
}
7275

7376
/* If there is no data, then drop the packet. */
7477
if (!pkt->frags) {
@@ -79,8 +82,8 @@ static inline enum net_verdict process_data(struct net_pkt *pkt)
7982
}
8083

8184
if (!net_pkt_is_l2_processed(pkt)) {
82-
ret = net_if_recv_data(net_pkt_iface(pkt), pkt);
8385
net_pkt_set_l2_processed(pkt, true);
86+
ret = net_if_recv_data(net_pkt_iface(pkt), pkt);
8487
if (ret != NET_CONTINUE) {
8588
if (ret == NET_DROP) {
8689
NET_DBG("Packet %p discarded by L2", pkt);
@@ -90,40 +93,48 @@ static inline enum net_verdict process_data(struct net_pkt *pkt)
9093

9194
return ret;
9295
}
93-
}
9496

95-
/* L2 has modified the buffer starting point, it is easier
96-
* to re-initialize the cursor rather than updating it.
97-
*/
98-
net_pkt_cursor_init(pkt);
97+
/* L2 has modified the buffer starting point, it is easier
98+
* to re-initialize the cursor rather than updating it.
99+
*/
100+
net_pkt_cursor_init(pkt);
101+
}
99102

100-
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET_DGRAM)) {
103+
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET_DGRAM) &&
104+
!net_pkt_is_dgram_processed(pkt)) {
105+
net_pkt_set_dgram_processed(pkt);
101106
net_packet_socket_input(pkt, net_pkt_ll_proto_type(pkt), SOCK_DGRAM);
102107
}
103108

104-
uint8_t family = net_pkt_family(pkt);
109+
if (net_pkt_is_l3_processed(pkt)) {
110+
net_pkt_set_l3_processed(pkt);
111+
uint8_t family = net_pkt_family(pkt);
105112

106-
if (IS_ENABLED(CONFIG_NET_IP) && (family == AF_INET || family == AF_INET6 ||
107-
family == AF_UNSPEC || family == AF_PACKET)) {
108-
/* IP version and header length. */
109-
uint8_t vtc_vhl = NET_IPV6_HDR(pkt)->vtc & 0xf0;
113+
if (IS_ENABLED(CONFIG_NET_IP) && (family == AF_INET || family == AF_INET6 ||
114+
family == AF_UNSPEC || family == AF_PACKET)) {
115+
/* IP version and header length. */
116+
uint8_t vtc_vhl = NET_IPV6_HDR(pkt)->vtc & 0xf0;
110117

111-
if (IS_ENABLED(CONFIG_NET_IPV6) && vtc_vhl == 0x60) {
112-
return net_ipv6_input(pkt);
113-
} else if (IS_ENABLED(CONFIG_NET_IPV4) && vtc_vhl == 0x40) {
114-
return net_ipv4_input(pkt);
115-
}
118+
if (IS_ENABLED(CONFIG_NET_IPV6) && vtc_vhl == 0x60) {
119+
return net_ipv6_input(pkt);
120+
} else if (IS_ENABLED(CONFIG_NET_IPV4) && vtc_vhl == 0x40) {
121+
return net_ipv4_input(pkt);
122+
}
116123

117-
NET_DBG("Unknown IP family packet (0x%x)", NET_IPV6_HDR(pkt)->vtc & 0xf0);
118-
net_stats_update_ip_errors_protoerr(net_pkt_iface(pkt));
119-
net_stats_update_ip_errors_vhlerr(net_pkt_iface(pkt));
120-
return NET_DROP;
121-
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) && family == AF_CAN) {
122-
return net_canbus_socket_input(pkt);
124+
NET_DBG("Unknown IP family packet (0x%x)", NET_IPV6_HDR(pkt)->vtc & 0xf0);
125+
net_stats_update_ip_errors_protoerr(net_pkt_iface(pkt));
126+
net_stats_update_ip_errors_vhlerr(net_pkt_iface(pkt));
127+
return NET_DROP;
128+
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) && family == AF_CAN) {
129+
return net_canbus_socket_input(pkt);
130+
} else {
131+
NET_DBG("Unknown protocol family packet (0x%x)", family);
132+
return NET_DROP;
133+
}
123134
}
124135

125-
NET_DBG("Unknown protocol family packet (0x%x)", family);
126136
return NET_DROP;
137+
127138
}
128139

129140
static void processing_data(struct net_pkt *pkt)

0 commit comments

Comments
 (0)