Zephyr Project API  3.1.0
A Scalable Open Source RTOS
net_pkt.h
Go to the documentation of this file.
1
8/*
9 * Copyright (c) 2016 Intel Corporation
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 */
13
14/* Data buffer API - used for all data to/from net */
15
16#ifndef ZEPHYR_INCLUDE_NET_NET_PKT_H_
17#define ZEPHYR_INCLUDE_NET_NET_PKT_H_
18
19#include <zephyr/types.h>
20#include <stdbool.h>
21
22#include <zephyr/net/buf.h>
23
24#include <zephyr/net/net_core.h>
26#include <zephyr/net/net_ip.h>
27#include <zephyr/net/net_if.h>
30#include <zephyr/net/ptp_time.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
43struct net_context;
44
45/* buffer cursor used in net_pkt */
48 struct net_buf *buf;
51};
52
59struct net_pkt {
65
67 struct k_mem_slab *slab;
68
70 union {
71 struct net_buf *frags;
72 struct net_buf *buffer;
73 };
74
77
80
82 struct net_if *iface;
83
86#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
87 struct net_if *orig_iface; /* Original network interface */
88#endif
89
90#if defined(CONFIG_NET_PKT_TIMESTAMP)
92 struct net_ptp_time timestamp;
93#endif
94
95#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
96 struct {
98 uint32_t create_time;
99
100#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
101 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
107 struct {
108 uint32_t stat[NET_PKT_DETAIL_STATS_COUNT];
109 int count;
110 } detail;
111#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
112 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
113 };
114#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
115
116#if defined(CONFIG_NET_PKT_TXTIME)
118 uint64_t txtime;
119#endif /* CONFIG_NET_PKT_TXTIME */
120
122 atomic_t atomic_ref;
123
124 /* Filled by layer 2 when network packet is received. */
125 struct net_linkaddr lladdr_src;
126 struct net_linkaddr lladdr_dst;
127
128#if defined(CONFIG_NET_TCP)
130 sys_snode_t next;
131#endif
132
133 uint8_t ip_hdr_len; /* pre-filled in order to avoid func call */
134
135 uint8_t overwrite : 1; /* Is packet content being overwritten? */
136
137 uint8_t sent_or_eof: 1; /* For outgoing packet: is this sent or not
138 * For incoming packet of a socket: last
139 * packet before EOF
140 * Used only if defined(CONFIG_NET_TCP)
141 */
142 union {
143 uint8_t pkt_queued: 1; /* For outgoing packet: is this packet
144 * queued to be sent but has not reached
145 * the driver yet.
146 * Used only if defined(CONFIG_NET_TCP)
147 */
148 uint8_t ptp_pkt: 1; /* For outgoing packet: is this packet
149 * a L2 PTP packet.
150 * Used only if defined (CONFIG_NET_L2_PTP)
151 */
152 };
153
154 uint8_t forwarding : 1; /* Are we forwarding this pkt
155 * Used only if defined(CONFIG_NET_ROUTE)
156 */
157 uint8_t family : 3; /* IPv4 vs IPv6 */
158
159 union {
160 uint8_t ipv4_auto_arp_msg : 1; /* Is this pkt IPv4 autoconf ARP
161 * message. Used only if
162 * defined(CONFIG_NET_IPV4_AUTO).
163 * Note: family needs to be
164 * AF_INET.
165 */
166 uint8_t lldp_pkt : 1; /* Is this pkt an LLDP message.
167 * Used only if
168 * defined(CONFIG_NET_LLDP).
169 * Note: family needs to be
170 * AF_UNSPEC.
171 */
172 uint8_t ppp_msg : 1; /* This is a PPP message */
173 };
174
175#if defined(CONFIG_NET_TCP)
176 uint8_t tcp_first_msg : 1; /* Is this the first time this pkt is
177 * sent, or is this a resend of a TCP
178 * segment.
179 */
180#endif
181
182 uint8_t captured : 1; /* Set to 1 if this packet is already being
183 * captured
184 */
185
186 uint8_t l2_bridged : 1; /* set to 1 if this packet comes from a bridge
187 * and already contains its L2 header to be
188 * preserved. Useful only if
189 * defined(CONFIG_NET_ETHERNET_BRIDGE).
190 */
191
192 uint8_t l2_processed : 1; /* Set to 1 if this packet has already been
193 * processed by the L2
194 */
195
196 union {
197 /* IPv6 hop limit or IPv4 ttl for this network packet.
198 * The value is shared between IPv6 and IPv4.
199 */
200 uint8_t ipv6_hop_limit;
201 uint8_t ipv4_ttl;
202 };
203
204 union {
205#if defined(CONFIG_NET_IPV4)
206 uint8_t ipv4_opts_len; /* Length if IPv4 Header Options */
207#endif
208#if defined(CONFIG_NET_IPV6)
209 uint16_t ipv6_ext_len; /* length of extension headers */
210#endif
211 };
212
216 uint8_t priority;
217
218#if defined(CONFIG_NET_VLAN)
219 /* VLAN TCI (Tag Control Information). This contains the Priority
220 * Code Point (PCP), Drop Eligible Indicator (DEI) and VLAN
221 * Identifier (VID, called more commonly VLAN tag). This value is
222 * kept in host byte order.
223 */
224 uint16_t vlan_tci;
225#endif /* CONFIG_NET_VLAN */
226
227#if defined(CONFIG_NET_IPV6)
228 /* Where is the start of the last header before payload data
229 * in IPv6 packet. This is offset value from start of the IPv6
230 * packet. Note that this value should be updated by who ever
231 * adds IPv6 extension headers to the network packet.
232 */
233 uint16_t ipv6_prev_hdr_start;
234
235#if defined(CONFIG_NET_IPV6_FRAGMENT)
236 uint16_t ipv6_fragment_flags; /* Fragment offset and M (More Fragment) flag */
237 uint32_t ipv6_fragment_id; /* Fragment id */
238 uint16_t ipv6_frag_hdr_start; /* Where starts the fragment header */
239#endif /* CONFIG_NET_IPV6_FRAGMENT */
240
241 uint8_t ipv6_ext_opt_len; /* IPv6 ND option length */
242 uint8_t ipv6_next_hdr; /* What is the very first next header */
243#endif /* CONFIG_NET_IPV6 */
244
245#if defined(CONFIG_IEEE802154)
246 uint8_t ieee802154_rssi; /* Received Signal Strength Indication */
247 uint8_t ieee802154_lqi; /* Link Quality Indicator */
248 uint8_t ieee802154_arb : 1; /* ACK Request Bit is set in the frame */
249 uint8_t ieee802154_ack_fpb : 1; /* Frame Pending Bit was set in the ACK */
250 uint8_t ieee802154_frame_secured : 1; /* Frame is authenticated and
251 * encrypted according to its
252 * Auxiliary Security Header
253 */
254 uint8_t ieee802154_mac_hdr_rdy : 1; /* Indicates if frame's MAC header
255 * is ready to be transmitted or if
256 * it requires further modifications,
257 * e.g. Frame Counter injection.
258 */
259#if defined(CONFIG_IEEE802154_2015)
260 uint8_t ieee802154_fv2015 : 1; /* Frame version is IEEE 802.15.4-2015 */
261 uint8_t ieee802154_ack_seb : 1; /* Security Enabled Bit was set in the ACK */
262 uint32_t ieee802154_ack_fc; /* Frame counter set in the ACK */
263 uint8_t ieee802154_ack_keyid; /* Key index set in the ACK */
264#endif
265#endif
266 /* @endcond */
267};
268
271/* The interface real ll address */
272static inline struct net_linkaddr *net_pkt_lladdr_if(struct net_pkt *pkt)
273{
274 return net_if_get_link_addr(pkt->iface);
275}
276
277static inline struct net_context *net_pkt_context(struct net_pkt *pkt)
278{
279 return pkt->context;
280}
281
282static inline void net_pkt_set_context(struct net_pkt *pkt,
283 struct net_context *ctx)
284{
285 pkt->context = ctx;
286}
287
288static inline struct net_if *net_pkt_iface(struct net_pkt *pkt)
289{
290 return pkt->iface;
291}
292
293static inline void net_pkt_set_iface(struct net_pkt *pkt, struct net_if *iface)
294{
295 pkt->iface = iface;
296
297 /* If the network interface is set in pkt, then also set the type of
298 * the network address that is stored in pkt. This is done here so
299 * that the address type is properly set and is not forgotten.
300 */
301 if (iface) {
302 pkt->lladdr_src.type = net_if_get_link_addr(iface)->type;
303 pkt->lladdr_dst.type = net_if_get_link_addr(iface)->type;
304 }
305}
306
307static inline struct net_if *net_pkt_orig_iface(struct net_pkt *pkt)
308{
309#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
310 return pkt->orig_iface;
311#else
312 return pkt->iface;
313#endif
314}
315
316static inline void net_pkt_set_orig_iface(struct net_pkt *pkt,
317 struct net_if *iface)
318{
319#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
320 pkt->orig_iface = iface;
321#endif
322}
323
324static inline uint8_t net_pkt_family(struct net_pkt *pkt)
325{
326 return pkt->family;
327}
328
329static inline void net_pkt_set_family(struct net_pkt *pkt, uint8_t family)
330{
331 pkt->family = family;
332}
333
334static inline bool net_pkt_is_ptp(struct net_pkt *pkt)
335{
336 return !!(pkt->ptp_pkt);
337}
338
339static inline void net_pkt_set_ptp(struct net_pkt *pkt, bool is_ptp)
340{
341 pkt->ptp_pkt = is_ptp;
342}
343
344static inline bool net_pkt_is_captured(struct net_pkt *pkt)
345{
346 return !!(pkt->captured);
347}
348
349static inline void net_pkt_set_captured(struct net_pkt *pkt, bool is_captured)
350{
351 pkt->captured = is_captured;
352}
353
354static inline bool net_pkt_is_l2_bridged(struct net_pkt *pkt)
355{
356 return IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) ? !!(pkt->l2_bridged) : 0;
357}
358
359static inline void net_pkt_set_l2_bridged(struct net_pkt *pkt, bool is_l2_bridged)
360{
361 if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE)) {
362 pkt->l2_bridged = is_l2_bridged;
363 }
364}
365
366static inline bool net_pkt_is_l2_processed(struct net_pkt *pkt)
367{
368 return !!(pkt->l2_processed);
369}
370
371static inline void net_pkt_set_l2_processed(struct net_pkt *pkt,
372 bool is_l2_processed)
373{
374 pkt->l2_processed = is_l2_processed;
375}
376
377static inline uint8_t net_pkt_ip_hdr_len(struct net_pkt *pkt)
378{
379 return pkt->ip_hdr_len;
380}
381
382static inline void net_pkt_set_ip_hdr_len(struct net_pkt *pkt, uint8_t len)
383{
384 pkt->ip_hdr_len = len;
385}
386
387static inline uint8_t net_pkt_sent(struct net_pkt *pkt)
388{
389 return pkt->sent_or_eof;
390}
391
392static inline void net_pkt_set_sent(struct net_pkt *pkt, bool sent)
393{
394 pkt->sent_or_eof = sent;
395}
396
397static inline uint8_t net_pkt_queued(struct net_pkt *pkt)
398{
399 return pkt->pkt_queued;
400}
401
402static inline void net_pkt_set_queued(struct net_pkt *pkt, bool send)
403{
404 pkt->pkt_queued = send;
405}
406
407static inline uint8_t net_pkt_tcp_1st_msg(struct net_pkt *pkt)
408{
409#if defined(CONFIG_NET_TCP)
410 return pkt->tcp_first_msg;
411#else
412 return true;
413#endif
414}
415
416static inline void net_pkt_set_tcp_1st_msg(struct net_pkt *pkt, bool is_1st)
417{
418#if defined(CONFIG_NET_TCP)
419 pkt->tcp_first_msg = is_1st;
420#else
421 ARG_UNUSED(pkt);
422 ARG_UNUSED(is_1st);
423#endif
424}
425
426#if defined(CONFIG_NET_SOCKETS)
427static inline uint8_t net_pkt_eof(struct net_pkt *pkt)
428{
429 return pkt->sent_or_eof;
430}
431
432static inline void net_pkt_set_eof(struct net_pkt *pkt, bool eof)
433{
434 pkt->sent_or_eof = eof;
435}
436#endif
437
438#if defined(CONFIG_NET_ROUTE)
439static inline bool net_pkt_forwarding(struct net_pkt *pkt)
440{
441 return pkt->forwarding;
442}
443
444static inline void net_pkt_set_forwarding(struct net_pkt *pkt, bool forward)
445{
446 pkt->forwarding = forward;
447}
448#else
449static inline bool net_pkt_forwarding(struct net_pkt *pkt)
450{
451 return false;
452}
453#endif
454
455#if defined(CONFIG_NET_IPV4)
456static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
457{
458 return pkt->ipv4_ttl;
459}
460
461static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
462 uint8_t ttl)
463{
464 pkt->ipv4_ttl = ttl;
465}
466
467static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
468{
469 return pkt->ipv4_opts_len;
470}
471
472static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
473 uint8_t opts_len)
474{
475 pkt->ipv4_opts_len = opts_len;
476}
477#else
478static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
479{
480 ARG_UNUSED(pkt);
481
482 return 0;
483}
484
485static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
486 uint8_t ttl)
487{
488 ARG_UNUSED(pkt);
489 ARG_UNUSED(ttl);
490}
491
492static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
493{
494 ARG_UNUSED(pkt);
495 return 0;
496}
497
498static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
499 uint8_t opts_len)
500{
501 ARG_UNUSED(pkt);
502 ARG_UNUSED(opts_len);
503}
504#endif
505
506#if defined(CONFIG_NET_IPV6)
507static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
508{
509 return pkt->ipv6_ext_opt_len;
510}
511
512static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
513 uint8_t len)
514{
515 pkt->ipv6_ext_opt_len = len;
516}
517
518static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
519{
520 return pkt->ipv6_next_hdr;
521}
522
523static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
524 uint8_t next_hdr)
525{
526 pkt->ipv6_next_hdr = next_hdr;
527}
528
529static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
530{
531 return pkt->ipv6_ext_len;
532}
533
534static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
535{
536 pkt->ipv6_ext_len = len;
537}
538
539static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
540{
541 return pkt->ipv6_prev_hdr_start;
542}
543
544static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
545 uint16_t offset)
546{
547 pkt->ipv6_prev_hdr_start = offset;
548}
549
550static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
551{
552 return pkt->ipv6_hop_limit;
553}
554
555static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
556 uint8_t hop_limit)
557{
558 pkt->ipv6_hop_limit = hop_limit;
559}
560#else /* CONFIG_NET_IPV6 */
561static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
562{
563 ARG_UNUSED(pkt);
564
565 return 0;
566}
567
568static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
569 uint8_t len)
570{
571 ARG_UNUSED(pkt);
572 ARG_UNUSED(len);
573}
574
575static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
576{
577 ARG_UNUSED(pkt);
578
579 return 0;
580}
581
582static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
583 uint8_t next_hdr)
584{
585 ARG_UNUSED(pkt);
586 ARG_UNUSED(next_hdr);
587}
588
589static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
590{
591 ARG_UNUSED(pkt);
592
593 return 0;
594}
595
596static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
597{
598 ARG_UNUSED(pkt);
599 ARG_UNUSED(len);
600}
601
602static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
603{
604 ARG_UNUSED(pkt);
605
606 return 0;
607}
608
609static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
610 uint16_t offset)
611{
612 ARG_UNUSED(pkt);
613 ARG_UNUSED(offset);
614}
615
616static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
617{
618 ARG_UNUSED(pkt);
619
620 return 0;
621}
622
623static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
624 uint8_t hop_limit)
625{
626 ARG_UNUSED(pkt);
627 ARG_UNUSED(hop_limit);
628}
629#endif /* CONFIG_NET_IPV6 */
630
631static inline uint16_t net_pkt_ip_opts_len(struct net_pkt *pkt)
632{
633#if defined(CONFIG_NET_IPV6)
634 return pkt->ipv6_ext_len;
635#elif defined(CONFIG_NET_IPV4)
636 return pkt->ipv4_opts_len;
637#else
638 ARG_UNUSED(pkt);
639
640 return 0;
641#endif
642}
643
644#if defined(CONFIG_NET_IPV6_FRAGMENT)
645static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
646{
647 return pkt->ipv6_frag_hdr_start;
648}
649
650static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
651 uint16_t start)
652{
653 pkt->ipv6_frag_hdr_start = start;
654}
655
656static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
657{
658 return pkt->ipv6_fragment_flags & NET_IPV6_FRAGH_OFFSET_MASK;
659}
660static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
661{
662 return (pkt->ipv6_fragment_flags & 0x01) != 0;
663}
664
665static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
667{
668 pkt->ipv6_fragment_flags = flags;
669}
670
671static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
672{
673 return pkt->ipv6_fragment_id;
674}
675
676static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
677 uint32_t id)
678{
679 pkt->ipv6_fragment_id = id;
680}
681#else /* CONFIG_NET_IPV6_FRAGMENT */
682static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
683{
684 ARG_UNUSED(pkt);
685
686 return 0;
687}
688
689static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
690 uint16_t start)
691{
692 ARG_UNUSED(pkt);
693 ARG_UNUSED(start);
694}
695
696static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
697{
698 ARG_UNUSED(pkt);
699
700 return 0;
701}
702
703static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
704{
705 ARG_UNUSED(pkt);
706
707 return 0;
708}
709
710static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
712{
713 ARG_UNUSED(pkt);
714 ARG_UNUSED(flags);
715}
716
717static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
718{
719 ARG_UNUSED(pkt);
720
721 return 0;
722}
723
724static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
725 uint32_t id)
726{
727 ARG_UNUSED(pkt);
728 ARG_UNUSED(id);
729}
730#endif /* CONFIG_NET_IPV6_FRAGMENT */
731
732static inline uint8_t net_pkt_priority(struct net_pkt *pkt)
733{
734 return pkt->priority;
735}
736
737static inline void net_pkt_set_priority(struct net_pkt *pkt,
738 uint8_t priority)
739{
740 pkt->priority = priority;
741}
742
743#if defined(CONFIG_NET_VLAN)
744static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
745{
746 return net_eth_vlan_get_vid(pkt->vlan_tci);
747}
748
749static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
750{
751 pkt->vlan_tci = net_eth_vlan_set_vid(pkt->vlan_tci, tag);
752}
753
754static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
755{
756 return net_eth_vlan_get_pcp(pkt->vlan_tci);
757}
758
759static inline void net_pkt_set_vlan_priority(struct net_pkt *pkt,
760 uint8_t priority)
761{
762 pkt->vlan_tci = net_eth_vlan_set_pcp(pkt->vlan_tci, priority);
763}
764
765static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
766{
767 return net_eth_vlan_get_dei(pkt->vlan_tci);
768}
769
770static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
771{
772 pkt->vlan_tci = net_eth_vlan_set_dei(pkt->vlan_tci, dei);
773}
774
775static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
776{
777 pkt->vlan_tci = tci;
778}
779
780static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
781{
782 return pkt->vlan_tci;
783}
784#else
785static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
786{
787 return NET_VLAN_TAG_UNSPEC;
788}
789
790static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
791{
792 ARG_UNUSED(pkt);
793 ARG_UNUSED(tag);
794}
795
796static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
797{
798 ARG_UNUSED(pkt);
799 return 0;
800}
801
802static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
803{
804 return false;
805}
806
807static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
808{
809 ARG_UNUSED(pkt);
810 ARG_UNUSED(dei);
811}
812
813static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
814{
815 return NET_VLAN_TAG_UNSPEC; /* assumes priority is 0 */
816}
817
818static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
819{
820 ARG_UNUSED(pkt);
821 ARG_UNUSED(tci);
822}
823#endif
824
825#if defined(CONFIG_NET_PKT_TIMESTAMP)
826static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
827{
828 return &pkt->timestamp;
829}
830
831static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
832 struct net_ptp_time *timestamp)
833{
834 pkt->timestamp.second = timestamp->second;
835 pkt->timestamp.nanosecond = timestamp->nanosecond;
836}
837#else
838static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
839{
840 ARG_UNUSED(pkt);
841
842 return NULL;
843}
844
845static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
846 struct net_ptp_time *timestamp)
847{
848 ARG_UNUSED(pkt);
849 ARG_UNUSED(timestamp);
850}
851#endif /* CONFIG_NET_PKT_TIMESTAMP */
852
853#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
854static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
855{
856 return pkt->create_time;
857}
858
859static inline void net_pkt_set_create_time(struct net_pkt *pkt,
860 uint32_t create_time)
861{
862 pkt->create_time = create_time;
863}
864#else
865static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
866{
867 ARG_UNUSED(pkt);
868
869 return 0U;
870}
871
872static inline void net_pkt_set_create_time(struct net_pkt *pkt,
873 uint32_t create_time)
874{
875 ARG_UNUSED(pkt);
876 ARG_UNUSED(create_time);
877}
878#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
879
880#if defined(CONFIG_NET_PKT_TXTIME)
881static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
882{
883 return pkt->txtime;
884}
885
886static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
887{
888 pkt->txtime = txtime;
889}
890#else
891static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
892{
893 ARG_UNUSED(pkt);
894
895 return 0;
896}
897
898static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
899{
900 ARG_UNUSED(pkt);
901 ARG_UNUSED(txtime);
902}
903#endif /* CONFIG_NET_PKT_TXTIME */
904
905#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
906 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
907static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
908{
909 return pkt->detail.stat;
910}
911
912static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
913{
914 return pkt->detail.count;
915}
916
917static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
918{
919 memset(&pkt->detail, 0, sizeof(pkt->detail));
920}
921
922static ALWAYS_INLINE void net_pkt_set_stats_tick(struct net_pkt *pkt,
923 uint32_t tick)
924{
925 if (pkt->detail.count >= NET_PKT_DETAIL_STATS_COUNT) {
926 NET_ERR("Detail stats count overflow (%d >= %d)",
927 pkt->detail.count, NET_PKT_DETAIL_STATS_COUNT);
928 return;
929 }
930
931 pkt->detail.stat[pkt->detail.count++] = tick;
932}
933
934#define net_pkt_set_tx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
935#define net_pkt_set_rx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
936#else
937static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
938{
939 ARG_UNUSED(pkt);
940
941 return NULL;
942}
943
944static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
945{
946 ARG_UNUSED(pkt);
947
948 return 0;
949}
950
951static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
952{
953 ARG_UNUSED(pkt);
954}
955
956static inline void net_pkt_set_stats_tick(struct net_pkt *pkt, uint32_t tick)
957{
958 ARG_UNUSED(pkt);
959 ARG_UNUSED(tick);
960}
961
962#define net_pkt_set_tx_stats_tick(pkt, tick)
963#define net_pkt_set_rx_stats_tick(pkt, tick)
964#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
965 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
966
967static inline size_t net_pkt_get_len(struct net_pkt *pkt)
968{
969 return net_buf_frags_len(pkt->frags);
970}
971
972static inline uint8_t *net_pkt_data(struct net_pkt *pkt)
973{
974 return pkt->frags->data;
975}
976
977static inline uint8_t *net_pkt_ip_data(struct net_pkt *pkt)
978{
979 return pkt->frags->data;
980}
981
982static inline bool net_pkt_is_empty(struct net_pkt *pkt)
983{
984 return !pkt->buffer || !net_pkt_data(pkt) || pkt->buffer->len == 0;
985}
986
987static inline struct net_linkaddr *net_pkt_lladdr_src(struct net_pkt *pkt)
988{
989 return &pkt->lladdr_src;
990}
991
992static inline struct net_linkaddr *net_pkt_lladdr_dst(struct net_pkt *pkt)
993{
994 return &pkt->lladdr_dst;
995}
996
997static inline void net_pkt_lladdr_swap(struct net_pkt *pkt)
998{
999 uint8_t *addr = net_pkt_lladdr_src(pkt)->addr;
1000
1001 net_pkt_lladdr_src(pkt)->addr = net_pkt_lladdr_dst(pkt)->addr;
1002 net_pkt_lladdr_dst(pkt)->addr = addr;
1003}
1004
1005static inline void net_pkt_lladdr_clear(struct net_pkt *pkt)
1006{
1007 net_pkt_lladdr_src(pkt)->addr = NULL;
1008 net_pkt_lladdr_src(pkt)->len = 0U;
1009}
1010
1011#if defined(CONFIG_IEEE802154) || defined(CONFIG_IEEE802154_RAW_MODE)
1012static inline uint8_t net_pkt_ieee802154_rssi(struct net_pkt *pkt)
1013{
1014 return pkt->ieee802154_rssi;
1015}
1016
1017static inline void net_pkt_set_ieee802154_rssi(struct net_pkt *pkt,
1018 uint8_t rssi)
1019{
1020 pkt->ieee802154_rssi = rssi;
1021}
1022
1023static inline uint8_t net_pkt_ieee802154_lqi(struct net_pkt *pkt)
1024{
1025 return pkt->ieee802154_lqi;
1026}
1027
1028static inline void net_pkt_set_ieee802154_lqi(struct net_pkt *pkt,
1029 uint8_t lqi)
1030{
1031 pkt->ieee802154_lqi = lqi;
1032}
1033
1034static inline bool net_pkt_ieee802154_arb(struct net_pkt *pkt)
1035{
1036 return pkt->ieee802154_arb;
1037}
1038
1039static inline void net_pkt_set_ieee802154_arb(struct net_pkt *pkt, bool arb)
1040{
1041 pkt->ieee802154_arb = arb;
1042}
1043
1044static inline bool net_pkt_ieee802154_ack_fpb(struct net_pkt *pkt)
1045{
1046 return pkt->ieee802154_ack_fpb;
1047}
1048
1049static inline void net_pkt_set_ieee802154_ack_fpb(struct net_pkt *pkt,
1050 bool fpb)
1051{
1052 pkt->ieee802154_ack_fpb = fpb;
1053}
1054
1055static inline bool net_pkt_ieee802154_frame_secured(struct net_pkt *pkt)
1056{
1057 return pkt->ieee802154_frame_secured;
1058}
1059
1060static inline void net_pkt_set_ieee802154_frame_secured(struct net_pkt *pkt,
1061 bool secured)
1062{
1063 pkt->ieee802154_frame_secured = secured;
1064}
1065
1066static inline bool net_pkt_ieee802154_mac_hdr_rdy(struct net_pkt *pkt)
1067{
1068 return pkt->ieee802154_mac_hdr_rdy;
1069}
1070
1071static inline void net_pkt_set_ieee802154_mac_hdr_rdy(struct net_pkt *pkt,
1072 bool rdy)
1073{
1074 pkt->ieee802154_mac_hdr_rdy = rdy;
1075}
1076
1077#if defined(CONFIG_IEEE802154_2015)
1078static inline bool net_pkt_ieee802154_fv2015(struct net_pkt *pkt)
1079{
1080 return pkt->ieee802154_fv2015;
1081}
1082
1083static inline void net_pkt_set_ieee802154_fv2015(struct net_pkt *pkt, bool fv2015)
1084{
1085 pkt->ieee802154_fv2015 = fv2015;
1086}
1087
1088static inline bool net_pkt_ieee802154_ack_seb(struct net_pkt *pkt)
1089{
1090 return pkt->ieee802154_ack_seb;
1091}
1092
1093static inline void net_pkt_set_ieee802154_ack_seb(struct net_pkt *pkt, bool seb)
1094{
1095 pkt->ieee802154_ack_seb = seb;
1096}
1097
1098static inline uint32_t net_pkt_ieee802154_ack_fc(struct net_pkt *pkt)
1099{
1100 return pkt->ieee802154_ack_fc;
1101}
1102
1103static inline void net_pkt_set_ieee802154_ack_fc(struct net_pkt *pkt,
1104 uint32_t fc)
1105{
1106 pkt->ieee802154_ack_fc = fc;
1107}
1108
1109static inline uint8_t net_pkt_ieee802154_ack_keyid(struct net_pkt *pkt)
1110{
1111 return pkt->ieee802154_ack_keyid;
1112}
1113
1114static inline void net_pkt_set_ieee802154_ack_keyid(struct net_pkt *pkt,
1115 uint8_t keyid)
1116{
1117 pkt->ieee802154_ack_keyid = keyid;
1118}
1119#endif /* CONFIG_IEEE802154_2015 */
1120#endif /* CONFIG_IEEE802154 || CONFIG_IEEE802154_RAW_MODE */
1121
1122#if defined(CONFIG_NET_IPV4_AUTO)
1123static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1124{
1125 return pkt->ipv4_auto_arp_msg;
1126}
1127
1128static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1129 bool is_auto_arp_msg)
1130{
1131 pkt->ipv4_auto_arp_msg = is_auto_arp_msg;
1132}
1133#else /* CONFIG_NET_IPV4_AUTO */
1134static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1135{
1136 ARG_UNUSED(pkt);
1137
1138 return false;
1139}
1140
1141static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1142 bool is_auto_arp_msg)
1143{
1144 ARG_UNUSED(pkt);
1145 ARG_UNUSED(is_auto_arp_msg);
1146}
1147#endif /* CONFIG_NET_IPV4_AUTO */
1148
1149#if defined(CONFIG_NET_LLDP)
1150static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1151{
1152 return pkt->lldp_pkt;
1153}
1154
1155static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1156{
1157 pkt->lldp_pkt = is_lldp;
1158}
1159#else
1160static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1161{
1162 ARG_UNUSED(pkt);
1163
1164 return false;
1165}
1166
1167static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1168{
1169 ARG_UNUSED(pkt);
1170 ARG_UNUSED(is_lldp);
1171}
1172#endif /* CONFIG_NET_LLDP */
1173
1174#if defined(CONFIG_NET_PPP)
1175static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1176{
1177 return pkt->ppp_msg;
1178}
1179
1180static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1181 bool is_ppp_msg)
1182{
1183 pkt->ppp_msg = is_ppp_msg;
1184}
1185#else /* CONFIG_NET_PPP */
1186static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1187{
1188 ARG_UNUSED(pkt);
1189
1190 return false;
1191}
1192
1193static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1194 bool is_ppp_msg)
1195{
1196 ARG_UNUSED(pkt);
1197 ARG_UNUSED(is_ppp_msg);
1198}
1199#endif /* CONFIG_NET_PPP */
1200
1201#define NET_IPV6_HDR(pkt) ((struct net_ipv6_hdr *)net_pkt_ip_data(pkt))
1202#define NET_IPV4_HDR(pkt) ((struct net_ipv4_hdr *)net_pkt_ip_data(pkt))
1203
1204static inline void net_pkt_set_src_ipv6_addr(struct net_pkt *pkt)
1205{
1207 net_pkt_context(pkt)),
1208 (struct in6_addr *)NET_IPV6_HDR(pkt)->src);
1209}
1210
1211static inline void net_pkt_set_overwrite(struct net_pkt *pkt, bool overwrite)
1212{
1213 pkt->overwrite = overwrite;
1214}
1215
1216static inline bool net_pkt_is_being_overwritten(struct net_pkt *pkt)
1217{
1218 return pkt->overwrite;
1219}
1220
1221#ifdef CONFIG_NET_PKT_FILTER
1222
1223bool net_pkt_filter_send_ok(struct net_pkt *pkt);
1224bool net_pkt_filter_recv_ok(struct net_pkt *pkt);
1225
1226#else
1227
1228static inline bool net_pkt_filter_send_ok(struct net_pkt *pkt)
1229{
1230 ARG_UNUSED(pkt);
1231
1232 return true;
1233}
1234
1235static inline bool net_pkt_filter_recv_ok(struct net_pkt *pkt)
1236{
1237 ARG_UNUSED(pkt);
1238
1239 return true;
1240}
1241
1242#endif /* CONFIG_NET_PKT_FILTER */
1243
1244/* @endcond */
1245
1259#define NET_PKT_SLAB_DEFINE(name, count) \
1260 K_MEM_SLAB_DEFINE(name, sizeof(struct net_pkt), count, 4)
1261
1262/* Backward compatibility macro */
1263#define NET_PKT_TX_SLAB_DEFINE(name, count) NET_PKT_SLAB_DEFINE(name, count)
1264
1278#define NET_PKT_DATA_POOL_DEFINE(name, count) \
1279 NET_BUF_POOL_DEFINE(name, count, CONFIG_NET_BUF_DATA_SIZE, \
1280 0, NULL)
1281
1284#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC) || \
1285 (CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG)
1286#define NET_PKT_DEBUG_ENABLED
1287#endif
1288
1289#if defined(NET_PKT_DEBUG_ENABLED)
1290
1291/* Debug versions of the net_pkt functions that are used when tracking
1292 * buffer usage.
1293 */
1294
1295struct net_buf *net_pkt_get_reserve_data_debug(struct net_buf_pool *pool,
1297 const char *caller,
1298 int line);
1299
1300#define net_pkt_get_reserve_data(pool, timeout) \
1301 net_pkt_get_reserve_data_debug(pool, timeout, __func__, __LINE__)
1302
1303struct net_buf *net_pkt_get_reserve_rx_data_debug(k_timeout_t timeout,
1304 const char *caller,
1305 int line);
1306#define net_pkt_get_reserve_rx_data(timeout) \
1307 net_pkt_get_reserve_rx_data_debug(timeout, __func__, __LINE__)
1308
1309struct net_buf *net_pkt_get_reserve_tx_data_debug(k_timeout_t timeout,
1310 const char *caller,
1311 int line);
1312#define net_pkt_get_reserve_tx_data(timeout) \
1313 net_pkt_get_reserve_tx_data_debug(timeout, __func__, __LINE__)
1314
1315struct net_buf *net_pkt_get_frag_debug(struct net_pkt *pkt,
1317 const char *caller, int line);
1318#define net_pkt_get_frag(pkt, timeout) \
1319 net_pkt_get_frag_debug(pkt, timeout, __func__, __LINE__)
1320
1321void net_pkt_unref_debug(struct net_pkt *pkt, const char *caller, int line);
1322#define net_pkt_unref(pkt) net_pkt_unref_debug(pkt, __func__, __LINE__)
1323
1324struct net_pkt *net_pkt_ref_debug(struct net_pkt *pkt, const char *caller,
1325 int line);
1326#define net_pkt_ref(pkt) net_pkt_ref_debug(pkt, __func__, __LINE__)
1327
1328struct net_buf *net_pkt_frag_ref_debug(struct net_buf *frag,
1329 const char *caller, int line);
1330#define net_pkt_frag_ref(frag) net_pkt_frag_ref_debug(frag, __func__, __LINE__)
1331
1332void net_pkt_frag_unref_debug(struct net_buf *frag,
1333 const char *caller, int line);
1334#define net_pkt_frag_unref(frag) \
1335 net_pkt_frag_unref_debug(frag, __func__, __LINE__)
1336
1337struct net_buf *net_pkt_frag_del_debug(struct net_pkt *pkt,
1338 struct net_buf *parent,
1339 struct net_buf *frag,
1340 const char *caller, int line);
1341#define net_pkt_frag_del(pkt, parent, frag) \
1342 net_pkt_frag_del_debug(pkt, parent, frag, __func__, __LINE__)
1343
1344void net_pkt_frag_add_debug(struct net_pkt *pkt, struct net_buf *frag,
1345 const char *caller, int line);
1346#define net_pkt_frag_add(pkt, frag) \
1347 net_pkt_frag_add_debug(pkt, frag, __func__, __LINE__)
1348
1349void net_pkt_frag_insert_debug(struct net_pkt *pkt, struct net_buf *frag,
1350 const char *caller, int line);
1351#define net_pkt_frag_insert(pkt, frag) \
1352 net_pkt_frag_insert_debug(pkt, frag, __func__, __LINE__)
1353#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC ||
1354 * CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG
1355 */
1365#if defined(NET_PKT_DEBUG_ENABLED)
1366void net_pkt_print_frags(struct net_pkt *pkt);
1367#else
1368#define net_pkt_print_frags(pkt)
1369#endif
1370
1384#if !defined(NET_PKT_DEBUG_ENABLED)
1386#endif
1387
1401#if !defined(NET_PKT_DEBUG_ENABLED)
1403#endif
1404
1416#if !defined(NET_PKT_DEBUG_ENABLED)
1418#endif
1419
1429#if !defined(NET_PKT_DEBUG_ENABLED)
1430void net_pkt_unref(struct net_pkt *pkt);
1431#endif
1432
1442#if !defined(NET_PKT_DEBUG_ENABLED)
1443struct net_pkt *net_pkt_ref(struct net_pkt *pkt);
1444#endif
1445
1455#if !defined(NET_PKT_DEBUG_ENABLED)
1456struct net_buf *net_pkt_frag_ref(struct net_buf *frag);
1457#endif
1458
1464#if !defined(NET_PKT_DEBUG_ENABLED)
1465void net_pkt_frag_unref(struct net_buf *frag);
1466#endif
1467
1478#if !defined(NET_PKT_DEBUG_ENABLED)
1480 struct net_buf *parent,
1481 struct net_buf *frag);
1482#endif
1483
1490#if !defined(NET_PKT_DEBUG_ENABLED)
1491void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag);
1492#endif
1493
1500#if !defined(NET_PKT_DEBUG_ENABLED)
1501void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag);
1502#endif
1503
1512bool net_pkt_compact(struct net_pkt *pkt);
1513
1522void net_pkt_get_info(struct k_mem_slab **rx,
1523 struct k_mem_slab **tx,
1524 struct net_buf_pool **rx_data,
1525 struct net_buf_pool **tx_data);
1526
1529#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
1533void net_pkt_print(void);
1534
1535typedef void (*net_pkt_allocs_cb_t)(struct net_pkt *pkt,
1536 struct net_buf *buf,
1537 const char *func_alloc,
1538 int line_alloc,
1539 const char *func_free,
1540 int line_free,
1541 bool in_use,
1542 void *user_data);
1543
1544void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data);
1545
1546const char *net_pkt_slab2str(struct k_mem_slab *slab);
1547const char *net_pkt_pool2str(struct net_buf_pool *pool);
1548
1549#else
1550#define net_pkt_print(...)
1551#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
1552
1553/* New allocator, and API are defined below.
1554 * This will be simpler when time will come to get rid of former API above.
1555 */
1556#if defined(NET_PKT_DEBUG_ENABLED)
1557
1558struct net_pkt *net_pkt_alloc_debug(k_timeout_t timeout,
1559 const char *caller, int line);
1560#define net_pkt_alloc(_timeout) \
1561 net_pkt_alloc_debug(_timeout, __func__, __LINE__)
1562
1563struct net_pkt *net_pkt_alloc_from_slab_debug(struct k_mem_slab *slab,
1565 const char *caller, int line);
1566#define net_pkt_alloc_from_slab(_slab, _timeout) \
1567 net_pkt_alloc_from_slab_debug(_slab, _timeout, __func__, __LINE__)
1568
1569struct net_pkt *net_pkt_rx_alloc_debug(k_timeout_t timeout,
1570 const char *caller, int line);
1571#define net_pkt_rx_alloc(_timeout) \
1572 net_pkt_rx_alloc_debug(_timeout, __func__, __LINE__)
1573
1574struct net_pkt *net_pkt_alloc_on_iface_debug(struct net_if *iface,
1576 const char *caller,
1577 int line);
1578#define net_pkt_alloc_on_iface(_iface, _timeout) \
1579 net_pkt_alloc_on_iface_debug(_iface, _timeout, __func__, __LINE__)
1580
1581struct net_pkt *net_pkt_rx_alloc_on_iface_debug(struct net_if *iface,
1583 const char *caller,
1584 int line);
1585#define net_pkt_rx_alloc_on_iface(_iface, _timeout) \
1586 net_pkt_rx_alloc_on_iface_debug(_iface, _timeout, \
1587 __func__, __LINE__)
1588
1589int net_pkt_alloc_buffer_debug(struct net_pkt *pkt,
1590 size_t size,
1591 enum net_ip_protocol proto,
1593 const char *caller, int line);
1594#define net_pkt_alloc_buffer(_pkt, _size, _proto, _timeout) \
1595 net_pkt_alloc_buffer_debug(_pkt, _size, _proto, _timeout, \
1596 __func__, __LINE__)
1597
1598struct net_pkt *net_pkt_alloc_with_buffer_debug(struct net_if *iface,
1599 size_t size,
1600 sa_family_t family,
1601 enum net_ip_protocol proto,
1603 const char *caller,
1604 int line);
1605#define net_pkt_alloc_with_buffer(_iface, _size, _family, \
1606 _proto, _timeout) \
1607 net_pkt_alloc_with_buffer_debug(_iface, _size, _family, \
1608 _proto, _timeout, \
1609 __func__, __LINE__)
1610
1611struct net_pkt *net_pkt_rx_alloc_with_buffer_debug(struct net_if *iface,
1612 size_t size,
1613 sa_family_t family,
1614 enum net_ip_protocol proto,
1616 const char *caller,
1617 int line);
1618#define net_pkt_rx_alloc_with_buffer(_iface, _size, _family, \
1619 _proto, _timeout) \
1620 net_pkt_rx_alloc_with_buffer_debug(_iface, _size, _family, \
1621 _proto, _timeout, \
1622 __func__, __LINE__)
1623#endif /* NET_PKT_DEBUG_ENABLED */
1636#if !defined(NET_PKT_DEBUG_ENABLED)
1638#endif
1639
1654#if !defined(NET_PKT_DEBUG_ENABLED)
1655struct net_pkt *net_pkt_alloc_from_slab(struct k_mem_slab *slab,
1657#endif
1658
1669#if !defined(NET_PKT_DEBUG_ENABLED)
1671#endif
1672
1681#if !defined(NET_PKT_DEBUG_ENABLED)
1684
1685/* Same as above but specifically for RX packet */
1688#endif
1689
1705#if !defined(NET_PKT_DEBUG_ENABLED)
1707 size_t size,
1708 enum net_ip_protocol proto,
1710#endif
1711
1723#if !defined(NET_PKT_DEBUG_ENABLED)
1725 size_t size,
1726 sa_family_t family,
1727 enum net_ip_protocol proto,
1729
1730/* Same as above but specifically for RX packet */
1732 size_t size,
1733 sa_family_t family,
1734 enum net_ip_protocol proto,
1736#endif
1737
1744void net_pkt_append_buffer(struct net_pkt *pkt, struct net_buf *buffer);
1745
1757
1774 enum net_ip_protocol proto);
1775
1785
1800int net_pkt_remove_tail(struct net_pkt *pkt, size_t length);
1801
1810
1817static inline void net_pkt_cursor_backup(struct net_pkt *pkt,
1818 struct net_pkt_cursor *backup)
1819{
1820 backup->buf = pkt->cursor.buf;
1821 backup->pos = pkt->cursor.pos;
1822}
1823
1830static inline void net_pkt_cursor_restore(struct net_pkt *pkt,
1831 struct net_pkt_cursor *backup)
1832{
1833 pkt->cursor.buf = backup->buf;
1834 pkt->cursor.pos = backup->pos;
1835}
1836
1844static inline void *net_pkt_cursor_get_pos(struct net_pkt *pkt)
1845{
1846 return pkt->cursor.pos;
1847}
1848
1869int net_pkt_skip(struct net_pkt *pkt, size_t length);
1870
1885int net_pkt_memset(struct net_pkt *pkt, int byte, size_t length);
1886
1900int net_pkt_copy(struct net_pkt *pkt_dst,
1901 struct net_pkt *pkt_src,
1902 size_t length);
1903
1914
1925
1936
1950int net_pkt_read(struct net_pkt *pkt, void *data, size_t length);
1951
1952/* Read uint8_t data data a net_pkt */
1953static inline int net_pkt_read_u8(struct net_pkt *pkt, uint8_t *data)
1954{
1955 return net_pkt_read(pkt, data, 1);
1956}
1957
1971
1985
1999
2013int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length);
2014
2015/* Write uint8_t data into a net_pkt. */
2016static inline int net_pkt_write_u8(struct net_pkt *pkt, uint8_t data)
2017{
2018 return net_pkt_write(pkt, &data, sizeof(uint8_t));
2019}
2020
2021/* Write uint16_t big endian data into a net_pkt. */
2022static inline int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
2023{
2024 uint16_t data_be16 = htons(data);
2025
2026 return net_pkt_write(pkt, &data_be16, sizeof(uint16_t));
2027}
2028
2029/* Write uint32_t big endian data into a net_pkt. */
2030static inline int net_pkt_write_be32(struct net_pkt *pkt, uint32_t data)
2031{
2032 uint32_t data_be32 = htonl(data);
2033
2034 return net_pkt_write(pkt, &data_be32, sizeof(uint32_t));
2035}
2036
2037/* Write uint32_t little endian data into a net_pkt. */
2038static inline int net_pkt_write_le32(struct net_pkt *pkt, uint32_t data)
2039{
2040 uint32_t data_le32 = sys_cpu_to_le32(data);
2041
2042 return net_pkt_write(pkt, &data_le32, sizeof(uint32_t));
2043}
2044
2045/* Write uint16_t little endian data into a net_pkt. */
2046static inline int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
2047{
2048 uint16_t data_le16 = sys_cpu_to_le16(data);
2049
2050 return net_pkt_write(pkt, &data_le16, sizeof(uint16_t));
2051}
2052
2061
2074int net_pkt_update_length(struct net_pkt *pkt, size_t length);
2075
2088int net_pkt_pull(struct net_pkt *pkt, size_t length);
2089
2099
2111bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size);
2112
2122
2124#if !defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2125 void *data;
2126#endif
2127 const size_t size;
2128};
2129
2130#if defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2131#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2132 struct net_pkt_data_access _name = { \
2133 .size = sizeof(_type), \
2134 }
2135
2136#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2137 NET_PKT_DATA_ACCESS_DEFINE(_name, _type)
2138
2139#else
2140#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2141 _type _hdr_##_name; \
2142 struct net_pkt_data_access _name = { \
2143 .data = &_hdr_##_name, \
2144 .size = sizeof(_type), \
2145 }
2146
2147#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2148 struct net_pkt_data_access _name = { \
2149 .data = NULL, \
2150 .size = sizeof(_type), \
2151 }
2152
2153#endif /* CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS */
2154
2168void *net_pkt_get_data(struct net_pkt *pkt,
2169 struct net_pkt_data_access *access);
2170
2185 struct net_pkt_data_access *access);
2186
2191static inline int net_pkt_acknowledge_data(struct net_pkt *pkt,
2192 struct net_pkt_data_access *access)
2193{
2194 return net_pkt_skip(pkt, access->size);
2195}
2196
2201#ifdef __cplusplus
2202}
2203#endif
2204
2205#endif /* ZEPHYR_INCLUDE_NET_NET_PKT_H_ */
long atomic_t
Definition: atomic.h:22
#define sys_cpu_to_le32(val)
Convert 32-bit integer from host endianness to little-endian.
Definition: byteorder.h:177
#define sys_cpu_to_le16(val)
Convert 16-bit integer from host endianness to little-endian.
Definition: byteorder.h:173
#define ALWAYS_INLINE
Definition: common.h:124
ZTEST_BMEM int timeout
Definition: main.c:31
ZTEST_BMEM int count
Definition: main.c:33
VLAN specific definitions.
unsigned short int sa_family_t
Definition: net_ip.h:164
#define htons(x)
Convert 16-bit value from host to network byte order.
Definition: net_ip.h:120
#define htonl(x)
Convert 32-bit value from host to network byte order.
Definition: net_ip.h:128
net_ip_protocol
Definition: net_ip.h:62
static ZTEST_BMEM char buffer[8]
Test mailbox enhance capabilities.
Definition: test_mbox_api.c:566
static size_t net_buf_frags_len(struct net_buf *buf)
Calculate amount of bytes stored in fragments.
Definition: buf.h:2465
static struct net_if * net_context_get_iface(struct net_context *context)
Get network interface for this context.
Definition: net_context.h:639
static struct net_linkaddr * net_if_get_link_addr(struct net_if *iface)
Get an network interface's link address.
Definition: net_if.h:725
static const struct in6_addr * net_if_ipv6_select_src_addr(struct net_if *iface, const struct in6_addr *dst)
Get a IPv6 source address that should be used when sending network data to destination.
Definition: net_if.h:1488
void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag)
Add a fragment to a packet at the end of its fragment list.
static int net_pkt_write_be32(struct net_pkt *pkt, uint32_t data)
Definition: net_pkt.h:2030
void net_pkt_cursor_init(struct net_pkt *pkt)
Initialize net_pkt cursor.
int net_pkt_skip(struct net_pkt *pkt, size_t length)
Skip some data from a net_pkt.
struct net_pkt * net_pkt_shallow_clone(struct net_pkt *pkt, k_timeout_t timeout)
Clone pkt and increase the refcount of its buffer.
void net_pkt_append_buffer(struct net_pkt *pkt, struct net_buf *buffer)
Append a buffer in packet.
#define net_pkt_print_frags(pkt)
Print fragment list and the fragment sizes.
Definition: net_pkt.h:1368
int net_pkt_update_length(struct net_pkt *pkt, size_t length)
Update the overall length of a packet.
struct net_buf * net_pkt_get_reserve_rx_data(k_timeout_t timeout)
Get RX DATA buffer from pool. Normally you should use net_pkt_get_frag() instead.
int net_pkt_pull(struct net_pkt *pkt, size_t length)
Remove data from the packet at current location.
int net_pkt_copy(struct net_pkt *pkt_dst, struct net_pkt *pkt_src, size_t length)
Copy data from a packet into another one.
struct net_pkt * net_pkt_rx_alloc(k_timeout_t timeout)
Allocate an initialized net_pkt for RX.
struct net_pkt * net_pkt_ref(struct net_pkt *pkt)
Increase the packet ref count.
int net_pkt_read_be16(struct net_pkt *pkt, uint16_t *data)
Read uint16_t big endian data from a net_pkt.
struct net_pkt * net_pkt_alloc_with_buffer(struct net_if *iface, size_t size, sa_family_t family, enum net_ip_protocol proto, k_timeout_t timeout)
Allocate a network packet and buffer at once.
void net_pkt_frag_unref(struct net_buf *frag)
Decrease the packet fragment ref count.
struct net_pkt * net_pkt_rx_alloc_with_buffer(struct net_if *iface, size_t size, sa_family_t family, enum net_ip_protocol proto, k_timeout_t timeout)
struct net_pkt * net_pkt_rx_clone(struct net_pkt *pkt, k_timeout_t timeout)
Clone pkt and its buffer. The cloned packet will be allocated on the RX packet poll.
void net_pkt_trim_buffer(struct net_pkt *pkt)
Trim net_pkt buffer.
struct net_pkt * net_pkt_alloc_on_iface(struct net_if *iface, k_timeout_t timeout)
Allocate a network packet for a specific network interface.
void net_pkt_get_info(struct k_mem_slab **rx, struct k_mem_slab **tx, struct net_buf_pool **rx_data, struct net_buf_pool **tx_data)
Get information about predefined RX, TX and DATA pools.
void net_pkt_unref(struct net_pkt *pkt)
Place packet back into the available packets slab.
bool net_pkt_compact(struct net_pkt *pkt)
Compact the fragment list of a packet.
static int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:2022
struct net_pkt * net_pkt_alloc(k_timeout_t timeout)
Allocate an initialized net_pkt.
int net_pkt_read(struct net_pkt *pkt, void *data, size_t length)
Read some data from a net_pkt.
struct net_buf * net_pkt_frag_del(struct net_pkt *pkt, struct net_buf *parent, struct net_buf *frag)
Delete existing fragment from a packet.
int net_pkt_set_data(struct net_pkt *pkt, struct net_pkt_data_access *access)
Set contiguous data into a network packet.
void * net_pkt_get_data(struct net_pkt *pkt, struct net_pkt_data_access *access)
Get data from a network packet in a contiguous way.
static int net_pkt_write_u8(struct net_pkt *pkt, uint8_t data)
Definition: net_pkt.h:2016
size_t net_pkt_available_payload_buffer(struct net_pkt *pkt, enum net_ip_protocol proto)
Get available buffer space for payload from a pkt.
int net_pkt_read_le16(struct net_pkt *pkt, uint16_t *data)
Read uint16_t little endian data from a net_pkt.
int net_pkt_read_be32(struct net_pkt *pkt, uint32_t *data)
Read uint32_t big endian data from a net_pkt.
struct net_pkt * net_pkt_rx_alloc_on_iface(struct net_if *iface, k_timeout_t timeout)
int net_pkt_remove_tail(struct net_pkt *pkt, size_t length)
Remove length bytes from tail of packet.
static void * net_pkt_cursor_get_pos(struct net_pkt *pkt)
Returns current position of the cursor.
Definition: net_pkt.h:1844
void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag)
Insert a fragment to a packet at the beginning of its fragment list.
int net_pkt_memset(struct net_pkt *pkt, int byte, size_t length)
Memset some data in a net_pkt.
static void net_pkt_cursor_backup(struct net_pkt *pkt, struct net_pkt_cursor *backup)
Backup net_pkt cursor.
Definition: net_pkt.h:1817
static int net_pkt_acknowledge_data(struct net_pkt *pkt, struct net_pkt_data_access *access)
Definition: net_pkt.h:2191
static int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:2046
struct net_buf * net_pkt_get_reserve_tx_data(k_timeout_t timeout)
Get TX DATA buffer from pool. Normally you should use net_pkt_get_frag() instead.
static void net_pkt_cursor_restore(struct net_pkt *pkt, struct net_pkt_cursor *backup)
Restore net_pkt cursor from a backup.
Definition: net_pkt.h:1830
uint16_t net_pkt_get_current_offset(struct net_pkt *pkt)
Get the actual offset in the packet from its cursor.
size_t net_pkt_remaining_data(struct net_pkt *pkt)
Get the amount of data which can be read from current cursor position.
int net_pkt_alloc_buffer(struct net_pkt *pkt, size_t size, enum net_ip_protocol proto, k_timeout_t timeout)
Allocate buffer for a net_pkt.
struct net_buf * net_pkt_get_frag(struct net_pkt *pkt, k_timeout_t timeout)
Get a data fragment that might be from user specific buffer pool or from global DATA pool.
int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length)
Write data into a net_pkt.
struct net_buf * net_pkt_frag_ref(struct net_buf *frag)
Increase the packet fragment ref count.
size_t net_pkt_available_buffer(struct net_pkt *pkt)
Get available buffer space from a pkt.
struct net_pkt * net_pkt_clone(struct net_pkt *pkt, k_timeout_t timeout)
Clone pkt and its buffer. The cloned packet will be allocated on the same pool as the original one.
struct net_pkt * net_pkt_alloc_from_slab(struct k_mem_slab *slab, k_timeout_t timeout)
Allocate an initialized net_pkt from a specific slab.
static int net_pkt_write_le32(struct net_pkt *pkt, uint32_t data)
Definition: net_pkt.h:2038
bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size)
Check if a data size could fit contiguously.
static int net_pkt_read_u8(struct net_pkt *pkt, uint8_t *data)
Definition: net_pkt.h:1953
size_t net_pkt_get_contiguous_len(struct net_pkt *pkt)
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:101
static uint16_t net_eth_vlan_set_vid(uint16_t tci, uint16_t vid)
Set VLAN identifier to TCI.
Definition: ethernet_vlan.h:76
static uint8_t net_eth_vlan_get_dei(uint16_t tci)
Get Drop Eligible Indicator from TCI.
Definition: ethernet_vlan.h:51
#define NET_VLAN_TAG_UNSPEC
Definition: ethernet_vlan.h:30
static uint16_t net_eth_vlan_set_dei(uint16_t tci, bool dei)
Set Drop Eligible Indicator to TCI.
Definition: ethernet_vlan.h:89
static uint16_t net_eth_vlan_get_vid(uint16_t tci)
Get VLAN identifier from TCI.
Definition: ethernet_vlan.h:39
static uint16_t net_eth_vlan_set_pcp(uint16_t tci, uint8_t pcp)
Set Priority Code Point to TCI.
Definition: ethernet_vlan.h:102
static uint8_t net_eth_vlan_get_pcp(uint16_t tci)
Get Priority Code Point from TCI.
Definition: ethernet_vlan.h:63
flags
Definition: http_parser.h:131
Buffer management.
Network context definitions.
Network core definitions.
Public API for network interface.
IPv6 and IPv4 definitions.
Public API for network link address.
static ssize_t send(int sock, const void *buf, size_t len, int flags)
Definition: socket.h:61
Public functions for the Precision Time Protocol time specification.
struct _snode sys_snode_t
Definition: slist.h:33
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__INTPTR_TYPE__ intptr_t
Definition: stdint.h:104
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
void * memset(void *buf, int c, size_t n)
Definition: net_ip.h:139
Kernel timeout type.
Definition: sys_clock.h:65
Network buffer pool representation.
Definition: buf.h:982
Network buffer representation.
Definition: buf.h:915
uint8_t * data
Definition: buf.h:943
uint16_t len
Definition: buf.h:946
Definition: net_context.h:201
Network Interface structure.
Definition: net_if.h:478
Hardware link address structure.
Definition: net_linkaddr.h:67
uint8_t * addr
Definition: net_linkaddr.h:69
uint8_t type
Definition: net_linkaddr.h:75
Definition: net_pkt.h:46
uint8_t * pos
Definition: net_pkt.h:50
struct net_buf * buf
Definition: net_pkt.h:48
Definition: net_pkt.h:2123
const size_t size
Definition: net_pkt.h:2127
void * data
Definition: net_pkt.h:2125
Network packet.
Definition: net_pkt.h:59
struct net_buf * frags
Definition: net_pkt.h:71
struct net_context * context
Definition: net_pkt.h:79
struct net_pkt_cursor cursor
Definition: net_pkt.h:76
struct net_if * iface
Definition: net_pkt.h:82
intptr_t fifo
Definition: net_pkt.h:64
struct net_buf * buffer
Definition: net_pkt.h:72
struct k_mem_slab * slab
Definition: net_pkt.h:67
Precision Time Protocol Timestamp format.
Definition: ptp_time.h:39
uint32_t nanosecond
Definition: ptp_time.h:59
uint64_t second
Definition: ptp_time.h:55
Definition: stat.h:39
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static const char * tag(void)
Definition: main.c:27
static const intptr_t user_data[5]
Definition: main.c:590