Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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#if defined(CONFIG_IEEE802154)
26#endif
27#include <zephyr/net/net_core.h>
29#include <zephyr/net/net_ip.h>
30#include <zephyr/net/net_if.h>
32#include <zephyr/net/net_time.h>
34#include <zephyr/net/ptp_time.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
47struct net_context;
48
49/* buffer cursor used in net_pkt */
52 struct net_buf *buf;
55};
56
63struct net_pkt {
69
71 struct k_mem_slab *slab;
72
74 union {
75 struct net_buf *frags;
76 struct net_buf *buffer;
77 };
78
81
84
86 struct net_if *iface;
87
90#if defined(CONFIG_NET_TCP)
92 sys_snode_t next;
93#endif
94#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
95 struct net_if *orig_iface; /* Original network interface */
96#endif
97
98#if defined(CONFIG_NET_PKT_TIMESTAMP) || defined(CONFIG_NET_PKT_TXTIME)
117 struct net_ptp_time timestamp;
118#endif
119
120#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
121 struct {
123 uint32_t create_time;
124
125#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
126 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
132 struct {
133 uint32_t stat[NET_PKT_DETAIL_STATS_COUNT];
134 int count;
135 } detail;
136#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
137 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
138 };
139#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
140
142 atomic_t atomic_ref;
143
144 /* Filled by layer 2 when network packet is received. */
145 struct net_linkaddr lladdr_src;
146 struct net_linkaddr lladdr_dst;
147 uint16_t ll_proto_type;
148
149#if defined(CONFIG_NET_IP)
150 uint8_t ip_hdr_len; /* pre-filled in order to avoid func call */
151#endif
152
153 uint8_t overwrite : 1; /* Is packet content being overwritten? */
154 uint8_t eof : 1; /* Last packet before EOF */
155 uint8_t ptp_pkt : 1; /* For outgoing packet: is this packet
156 * a L2 PTP packet.
157 * Used only if defined (CONFIG_NET_L2_PTP)
158 */
159 uint8_t forwarding : 1; /* Are we forwarding this pkt
160 * Used only if defined(CONFIG_NET_ROUTE)
161 */
162 uint8_t family : 3; /* Address family, see net_ip.h */
163
164 /* bitfield byte alignment boundary */
165
166#if defined(CONFIG_NET_IPV4_AUTO)
167 uint8_t ipv4_auto_arp_msg : 1; /* Is this pkt IPv4 autoconf ARP
168 * message.
169 * Note: family needs to be
170 * AF_INET.
171 */
172#endif
173#if defined(CONFIG_NET_LLDP)
174 uint8_t lldp_pkt : 1; /* Is this pkt an LLDP message.
175 * Note: family needs to be
176 * AF_UNSPEC.
177 */
178#endif
179 uint8_t ppp_msg : 1; /* This is a PPP message */
180#if defined(CONFIG_NET_TCP)
181 uint8_t tcp_first_msg : 1; /* Is this the first time this pkt is
182 * sent, or is this a resend of a TCP
183 * segment.
184 */
185#endif
186 uint8_t captured : 1; /* Set to 1 if this packet is already being
187 * captured
188 */
189 uint8_t l2_bridged : 1; /* set to 1 if this packet comes from a bridge
190 * and already contains its L2 header to be
191 * preserved. Useful only if
192 * defined(CONFIG_NET_ETHERNET_BRIDGE).
193 */
194 uint8_t l2_processed : 1; /* Set to 1 if this packet has already been
195 * processed by the L2
196 */
197
198 /* bitfield byte alignment boundary */
199
200#if defined(CONFIG_NET_IP)
201 union {
202 /* IPv6 hop limit or IPv4 ttl for this network packet.
203 * The value is shared between IPv6 and IPv4.
204 */
205#if defined(CONFIG_NET_IPV6)
206 uint8_t ipv6_hop_limit;
207#endif
208#if defined(CONFIG_NET_IPV4)
209 uint8_t ipv4_ttl;
210#endif
211 };
212
213 union {
214#if defined(CONFIG_NET_IPV4)
215 uint8_t ipv4_opts_len; /* length of IPv4 header options */
216#endif
217#if defined(CONFIG_NET_IPV6)
218 uint16_t ipv6_ext_len; /* length of extension headers */
219#endif
220 };
221
222#if defined(CONFIG_NET_IPV4_FRAGMENT) || defined(CONFIG_NET_IPV6_FRAGMENT)
223 union {
224#if defined(CONFIG_NET_IPV4_FRAGMENT)
225 struct {
226 uint16_t flags; /* Fragment offset and M (More Fragment) flag */
227 uint16_t id; /* Fragment ID */
228 } ipv4_fragment;
229#endif /* CONFIG_NET_IPV4_FRAGMENT */
230#if defined(CONFIG_NET_IPV6_FRAGMENT)
231 struct {
232 uint16_t flags; /* Fragment offset and M (More Fragment) flag */
233 uint32_t id; /* Fragment id */
234 uint16_t hdr_start; /* Where starts the fragment header */
235 } ipv6_fragment;
236#endif /* CONFIG_NET_IPV6_FRAGMENT */
237 };
238#endif /* CONFIG_NET_IPV4_FRAGMENT || CONFIG_NET_IPV6_FRAGMENT */
239
240#if defined(CONFIG_NET_IPV6)
241 /* Where is the start of the last header before payload data
242 * in IPv6 packet. This is offset value from start of the IPv6
243 * packet. Note that this value should be updated by who ever
244 * adds IPv6 extension headers to the network packet.
245 */
246 uint16_t ipv6_prev_hdr_start;
247
248 uint8_t ipv6_ext_opt_len; /* IPv6 ND option length */
249 uint8_t ipv6_next_hdr; /* What is the very first next header */
250#endif /* CONFIG_NET_IPV6 */
251
252#if defined(CONFIG_NET_IP_DSCP_ECN)
254 uint8_t ip_dscp : 6;
255
257 uint8_t ip_ecn : 2;
258#endif /* CONFIG_NET_IP_DSCP_ECN */
259#endif /* CONFIG_NET_IP */
260
261#if defined(CONFIG_NET_VLAN)
262 /* VLAN TCI (Tag Control Information). This contains the Priority
263 * Code Point (PCP), Drop Eligible Indicator (DEI) and VLAN
264 * Identifier (VID, called more commonly VLAN tag). This value is
265 * kept in host byte order.
266 */
267 uint16_t vlan_tci;
268#endif /* CONFIG_NET_VLAN */
269
270#if defined(NET_PKT_HAS_CONTROL_BLOCK)
271 /* TODO: Evolve this into a union of orthogonal
272 * control block declarations if further L2
273 * stacks require L2-specific attributes.
274 */
275#if defined(CONFIG_IEEE802154)
276 /* The following structure requires a 4-byte alignment
277 * boundary to avoid padding.
278 */
279 struct net_pkt_cb_ieee802154 cb;
280#endif /* CONFIG_IEEE802154 */
281#endif /* NET_PKT_HAS_CONTROL_BLOCK */
282
286 uint8_t priority;
287
288 /* @endcond */
289};
290
293/* The interface real ll address */
294static inline struct net_linkaddr *net_pkt_lladdr_if(struct net_pkt *pkt)
295{
296 return net_if_get_link_addr(pkt->iface);
297}
298
299static inline struct net_context *net_pkt_context(struct net_pkt *pkt)
300{
301 return pkt->context;
302}
303
304static inline void net_pkt_set_context(struct net_pkt *pkt,
305 struct net_context *ctx)
306{
307 pkt->context = ctx;
308}
309
310static inline struct net_if *net_pkt_iface(struct net_pkt *pkt)
311{
312 return pkt->iface;
313}
314
315static inline void net_pkt_set_iface(struct net_pkt *pkt, struct net_if *iface)
316{
317 pkt->iface = iface;
318
319 /* If the network interface is set in pkt, then also set the type of
320 * the network address that is stored in pkt. This is done here so
321 * that the address type is properly set and is not forgotten.
322 */
323 if (iface) {
324 uint8_t type = net_if_get_link_addr(iface)->type;
325
326 pkt->lladdr_src.type = type;
327 pkt->lladdr_dst.type = type;
328 }
329}
330
331static inline struct net_if *net_pkt_orig_iface(struct net_pkt *pkt)
332{
333#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
334 return pkt->orig_iface;
335#else
336 return pkt->iface;
337#endif
338}
339
340static inline void net_pkt_set_orig_iface(struct net_pkt *pkt,
341 struct net_if *iface)
342{
343#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
344 pkt->orig_iface = iface;
345#endif
346}
347
348static inline uint8_t net_pkt_family(struct net_pkt *pkt)
349{
350 return pkt->family;
351}
352
353static inline void net_pkt_set_family(struct net_pkt *pkt, uint8_t family)
354{
355 pkt->family = family;
356}
357
358static inline bool net_pkt_is_ptp(struct net_pkt *pkt)
359{
360 return !!(pkt->ptp_pkt);
361}
362
363static inline void net_pkt_set_ptp(struct net_pkt *pkt, bool is_ptp)
364{
365 pkt->ptp_pkt = is_ptp;
366}
367
368static inline bool net_pkt_is_captured(struct net_pkt *pkt)
369{
370 return !!(pkt->captured);
371}
372
373static inline void net_pkt_set_captured(struct net_pkt *pkt, bool is_captured)
374{
375 pkt->captured = is_captured;
376}
377
378static inline bool net_pkt_is_l2_bridged(struct net_pkt *pkt)
379{
380 return IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) ? !!(pkt->l2_bridged) : 0;
381}
382
383static inline void net_pkt_set_l2_bridged(struct net_pkt *pkt, bool is_l2_bridged)
384{
385 if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE)) {
386 pkt->l2_bridged = is_l2_bridged;
387 }
388}
389
390static inline bool net_pkt_is_l2_processed(struct net_pkt *pkt)
391{
392 return !!(pkt->l2_processed);
393}
394
395static inline void net_pkt_set_l2_processed(struct net_pkt *pkt,
396 bool is_l2_processed)
397{
398 pkt->l2_processed = is_l2_processed;
399}
400
401static inline uint8_t net_pkt_ip_hdr_len(struct net_pkt *pkt)
402{
403#if defined(CONFIG_NET_IP)
404 return pkt->ip_hdr_len;
405#else
406 return 0;
407#endif
408}
409
410static inline void net_pkt_set_ip_hdr_len(struct net_pkt *pkt, uint8_t len)
411{
412#if defined(CONFIG_NET_IP)
413 pkt->ip_hdr_len = len;
414#endif
415}
416
417static inline uint8_t net_pkt_ip_dscp(struct net_pkt *pkt)
418{
419#if defined(CONFIG_NET_IP_DSCP_ECN)
420 return pkt->ip_dscp;
421#else
422 return 0;
423#endif
424}
425
426static inline void net_pkt_set_ip_dscp(struct net_pkt *pkt, uint8_t dscp)
427{
428#if defined(CONFIG_NET_IP_DSCP_ECN)
429 pkt->ip_dscp = dscp;
430#endif
431}
432
433static inline uint8_t net_pkt_ip_ecn(struct net_pkt *pkt)
434{
435#if defined(CONFIG_NET_IP_DSCP_ECN)
436 return pkt->ip_ecn;
437#else
438 return 0;
439#endif
440}
441
442static inline void net_pkt_set_ip_ecn(struct net_pkt *pkt, uint8_t ecn)
443{
444#if defined(CONFIG_NET_IP_DSCP_ECN)
445 pkt->ip_ecn = ecn;
446#endif
447}
448
449static inline uint8_t net_pkt_tcp_1st_msg(struct net_pkt *pkt)
450{
451#if defined(CONFIG_NET_TCP)
452 return pkt->tcp_first_msg;
453#else
454 return true;
455#endif
456}
457
458static inline void net_pkt_set_tcp_1st_msg(struct net_pkt *pkt, bool is_1st)
459{
460#if defined(CONFIG_NET_TCP)
461 pkt->tcp_first_msg = is_1st;
462#else
463 ARG_UNUSED(pkt);
464 ARG_UNUSED(is_1st);
465#endif
466}
467
468static inline uint8_t net_pkt_eof(struct net_pkt *pkt)
469{
470 return pkt->eof;
471}
472
473static inline void net_pkt_set_eof(struct net_pkt *pkt, bool eof)
474{
475 pkt->eof = eof;
476}
477
478static inline bool net_pkt_forwarding(struct net_pkt *pkt)
479{
480 return !!(pkt->forwarding);
481}
482
483static inline void net_pkt_set_forwarding(struct net_pkt *pkt, bool forward)
484{
485 pkt->forwarding = forward;
486}
487
488#if defined(CONFIG_NET_IPV4)
489static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
490{
491 return pkt->ipv4_ttl;
492}
493
494static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
495 uint8_t ttl)
496{
497 pkt->ipv4_ttl = ttl;
498}
499
500static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
501{
502 return pkt->ipv4_opts_len;
503}
504
505static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
506 uint8_t opts_len)
507{
508 pkt->ipv4_opts_len = opts_len;
509}
510#else
511static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
512{
513 ARG_UNUSED(pkt);
514
515 return 0;
516}
517
518static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
519 uint8_t ttl)
520{
521 ARG_UNUSED(pkt);
522 ARG_UNUSED(ttl);
523}
524
525static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
526{
527 ARG_UNUSED(pkt);
528 return 0;
529}
530
531static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
532 uint8_t opts_len)
533{
534 ARG_UNUSED(pkt);
535 ARG_UNUSED(opts_len);
536}
537#endif
538
539#if defined(CONFIG_NET_IPV6)
540static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
541{
542 return pkt->ipv6_ext_opt_len;
543}
544
545static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
546 uint8_t len)
547{
548 pkt->ipv6_ext_opt_len = len;
549}
550
551static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
552{
553 return pkt->ipv6_next_hdr;
554}
555
556static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
557 uint8_t next_hdr)
558{
559 pkt->ipv6_next_hdr = next_hdr;
560}
561
562static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
563{
564 return pkt->ipv6_ext_len;
565}
566
567static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
568{
569 pkt->ipv6_ext_len = len;
570}
571
572static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
573{
574 return pkt->ipv6_prev_hdr_start;
575}
576
577static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
578 uint16_t offset)
579{
580 pkt->ipv6_prev_hdr_start = offset;
581}
582
583static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
584{
585 return pkt->ipv6_hop_limit;
586}
587
588static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
589 uint8_t hop_limit)
590{
591 pkt->ipv6_hop_limit = hop_limit;
592}
593#else /* CONFIG_NET_IPV6 */
594static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
595{
596 ARG_UNUSED(pkt);
597
598 return 0;
599}
600
601static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
602 uint8_t len)
603{
604 ARG_UNUSED(pkt);
605 ARG_UNUSED(len);
606}
607
608static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
609{
610 ARG_UNUSED(pkt);
611
612 return 0;
613}
614
615static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
616 uint8_t next_hdr)
617{
618 ARG_UNUSED(pkt);
619 ARG_UNUSED(next_hdr);
620}
621
622static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
623{
624 ARG_UNUSED(pkt);
625
626 return 0;
627}
628
629static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
630{
631 ARG_UNUSED(pkt);
632 ARG_UNUSED(len);
633}
634
635static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
636{
637 ARG_UNUSED(pkt);
638
639 return 0;
640}
641
642static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
643 uint16_t offset)
644{
645 ARG_UNUSED(pkt);
646 ARG_UNUSED(offset);
647}
648
649static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
650{
651 ARG_UNUSED(pkt);
652
653 return 0;
654}
655
656static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
657 uint8_t hop_limit)
658{
659 ARG_UNUSED(pkt);
660 ARG_UNUSED(hop_limit);
661}
662#endif /* CONFIG_NET_IPV6 */
663
664static inline uint16_t net_pkt_ip_opts_len(struct net_pkt *pkt)
665{
666#if defined(CONFIG_NET_IPV6)
667 return pkt->ipv6_ext_len;
668#elif defined(CONFIG_NET_IPV4)
669 return pkt->ipv4_opts_len;
670#else
671 ARG_UNUSED(pkt);
672
673 return 0;
674#endif
675}
676
677#if defined(CONFIG_NET_IPV4_FRAGMENT)
678static inline uint16_t net_pkt_ipv4_fragment_offset(struct net_pkt *pkt)
679{
680 return (pkt->ipv4_fragment.flags & NET_IPV4_FRAGH_OFFSET_MASK) * 8;
681}
682
683static inline bool net_pkt_ipv4_fragment_more(struct net_pkt *pkt)
684{
685 return (pkt->ipv4_fragment.flags & NET_IPV4_MORE_FRAG_MASK) != 0;
686}
687
688static inline void net_pkt_set_ipv4_fragment_flags(struct net_pkt *pkt, uint16_t flags)
689{
690 pkt->ipv4_fragment.flags = flags;
691}
692
693static inline uint32_t net_pkt_ipv4_fragment_id(struct net_pkt *pkt)
694{
695 return pkt->ipv4_fragment.id;
696}
697
698static inline void net_pkt_set_ipv4_fragment_id(struct net_pkt *pkt, uint32_t id)
699{
700 pkt->ipv4_fragment.id = id;
701}
702#else /* CONFIG_NET_IPV4_FRAGMENT */
703static inline uint16_t net_pkt_ipv4_fragment_offset(struct net_pkt *pkt)
704{
705 ARG_UNUSED(pkt);
706
707 return 0;
708}
709
710static inline bool net_pkt_ipv4_fragment_more(struct net_pkt *pkt)
711{
712 ARG_UNUSED(pkt);
713
714 return 0;
715}
716
717static inline void net_pkt_set_ipv4_fragment_flags(struct net_pkt *pkt, uint16_t flags)
718{
719 ARG_UNUSED(pkt);
720 ARG_UNUSED(flags);
721}
722
723static inline uint32_t net_pkt_ipv4_fragment_id(struct net_pkt *pkt)
724{
725 ARG_UNUSED(pkt);
726
727 return 0;
728}
729
730static inline void net_pkt_set_ipv4_fragment_id(struct net_pkt *pkt, uint32_t id)
731{
732 ARG_UNUSED(pkt);
733 ARG_UNUSED(id);
734}
735#endif /* CONFIG_NET_IPV4_FRAGMENT */
736
737#if defined(CONFIG_NET_IPV6_FRAGMENT)
738static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
739{
740 return pkt->ipv6_fragment.hdr_start;
741}
742
743static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
744 uint16_t start)
745{
746 pkt->ipv6_fragment.hdr_start = start;
747}
748
749static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
750{
751 return pkt->ipv6_fragment.flags & NET_IPV6_FRAGH_OFFSET_MASK;
752}
753static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
754{
755 return (pkt->ipv6_fragment.flags & 0x01) != 0;
756}
757
758static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
760{
761 pkt->ipv6_fragment.flags = flags;
762}
763
764static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
765{
766 return pkt->ipv6_fragment.id;
767}
768
769static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
770 uint32_t id)
771{
772 pkt->ipv6_fragment.id = id;
773}
774#else /* CONFIG_NET_IPV6_FRAGMENT */
775static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
776{
777 ARG_UNUSED(pkt);
778
779 return 0;
780}
781
782static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
783 uint16_t start)
784{
785 ARG_UNUSED(pkt);
786 ARG_UNUSED(start);
787}
788
789static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
790{
791 ARG_UNUSED(pkt);
792
793 return 0;
794}
795
796static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
797{
798 ARG_UNUSED(pkt);
799
800 return 0;
801}
802
803static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
805{
806 ARG_UNUSED(pkt);
807 ARG_UNUSED(flags);
808}
809
810static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
811{
812 ARG_UNUSED(pkt);
813
814 return 0;
815}
816
817static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
818 uint32_t id)
819{
820 ARG_UNUSED(pkt);
821 ARG_UNUSED(id);
822}
823#endif /* CONFIG_NET_IPV6_FRAGMENT */
824
825static inline uint8_t net_pkt_priority(struct net_pkt *pkt)
826{
827 return pkt->priority;
828}
829
830static inline void net_pkt_set_priority(struct net_pkt *pkt,
831 uint8_t priority)
832{
833 pkt->priority = priority;
834}
835
836#if defined(CONFIG_NET_VLAN)
837static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
838{
839 return net_eth_vlan_get_vid(pkt->vlan_tci);
840}
841
842static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
843{
844 pkt->vlan_tci = net_eth_vlan_set_vid(pkt->vlan_tci, tag);
845}
846
847static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
848{
849 return net_eth_vlan_get_pcp(pkt->vlan_tci);
850}
851
852static inline void net_pkt_set_vlan_priority(struct net_pkt *pkt,
853 uint8_t priority)
854{
855 pkt->vlan_tci = net_eth_vlan_set_pcp(pkt->vlan_tci, priority);
856}
857
858static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
859{
860 return net_eth_vlan_get_dei(pkt->vlan_tci);
861}
862
863static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
864{
865 pkt->vlan_tci = net_eth_vlan_set_dei(pkt->vlan_tci, dei);
866}
867
868static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
869{
870 pkt->vlan_tci = tci;
871}
872
873static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
874{
875 return pkt->vlan_tci;
876}
877#else
878static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
879{
880 return NET_VLAN_TAG_UNSPEC;
881}
882
883static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
884{
885 ARG_UNUSED(pkt);
886 ARG_UNUSED(tag);
887}
888
889static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
890{
891 ARG_UNUSED(pkt);
892 return 0;
893}
894
895static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
896{
897 return false;
898}
899
900static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
901{
902 ARG_UNUSED(pkt);
903 ARG_UNUSED(dei);
904}
905
906static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
907{
908 return NET_VLAN_TAG_UNSPEC; /* assumes priority is 0 */
909}
910
911static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
912{
913 ARG_UNUSED(pkt);
914 ARG_UNUSED(tci);
915}
916#endif
917
918#if defined(CONFIG_NET_PKT_TIMESTAMP) || defined(CONFIG_NET_PKT_TXTIME)
919static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
920{
921 return &pkt->timestamp;
922}
923
924static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
925 struct net_ptp_time *timestamp)
926{
927 pkt->timestamp.second = timestamp->second;
928 pkt->timestamp.nanosecond = timestamp->nanosecond;
929}
930
931static inline net_time_t net_pkt_timestamp_ns(struct net_pkt *pkt)
932{
933 return net_ptp_time_to_ns(&pkt->timestamp);
934}
935
936static inline void net_pkt_set_timestamp_ns(struct net_pkt *pkt, net_time_t timestamp)
937{
938 pkt->timestamp = ns_to_net_ptp_time(timestamp);
939}
940#else
941static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
942{
943 ARG_UNUSED(pkt);
944
945 return NULL;
946}
947
948static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
949 struct net_ptp_time *timestamp)
950{
951 ARG_UNUSED(pkt);
952 ARG_UNUSED(timestamp);
953}
954
955static inline net_time_t net_pkt_timestamp_ns(struct net_pkt *pkt)
956{
957 ARG_UNUSED(pkt);
958
959 return 0;
960}
961
962static inline void net_pkt_set_timestamp_ns(struct net_pkt *pkt, net_time_t timestamp)
963{
964 ARG_UNUSED(pkt);
965 ARG_UNUSED(timestamp);
966}
967#endif /* CONFIG_NET_PKT_TIMESTAMP || CONFIG_NET_PKT_TXTIME */
968
969#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
970static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
971{
972 return pkt->create_time;
973}
974
975static inline void net_pkt_set_create_time(struct net_pkt *pkt,
976 uint32_t create_time)
977{
978 pkt->create_time = create_time;
979}
980#else
981static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
982{
983 ARG_UNUSED(pkt);
984
985 return 0U;
986}
987
988static inline void net_pkt_set_create_time(struct net_pkt *pkt,
989 uint32_t create_time)
990{
991 ARG_UNUSED(pkt);
992 ARG_UNUSED(create_time);
993}
994#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
995
999static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
1000{
1001#if defined(CONFIG_NET_PKT_TXTIME)
1002 return pkt->timestamp.second * NSEC_PER_SEC + pkt->timestamp.nanosecond;
1003#else
1004 ARG_UNUSED(pkt);
1005
1006 return 0;
1007#endif /* CONFIG_NET_PKT_TXTIME */
1008}
1009
1014static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
1015{
1016#if defined(CONFIG_NET_PKT_TXTIME)
1017 pkt->timestamp.second = txtime / NSEC_PER_SEC;
1018 pkt->timestamp.nanosecond = txtime % NSEC_PER_SEC;
1019#else
1020 ARG_UNUSED(pkt);
1021 ARG_UNUSED(txtime);
1022#endif /* CONFIG_NET_PKT_TXTIME */
1023}
1024
1025#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
1026 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
1027static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
1028{
1029 return pkt->detail.stat;
1030}
1031
1032static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
1033{
1034 return pkt->detail.count;
1035}
1036
1037static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
1038{
1039 memset(&pkt->detail, 0, sizeof(pkt->detail));
1040}
1041
1042static ALWAYS_INLINE void net_pkt_set_stats_tick(struct net_pkt *pkt,
1043 uint32_t tick)
1044{
1045 if (pkt->detail.count >= NET_PKT_DETAIL_STATS_COUNT) {
1046 NET_ERR("Detail stats count overflow (%d >= %d)",
1047 pkt->detail.count, NET_PKT_DETAIL_STATS_COUNT);
1048 return;
1049 }
1050
1051 pkt->detail.stat[pkt->detail.count++] = tick;
1052}
1053
1054#define net_pkt_set_tx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
1055#define net_pkt_set_rx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
1056#else
1057static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
1058{
1059 ARG_UNUSED(pkt);
1060
1061 return NULL;
1062}
1063
1064static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
1065{
1066 ARG_UNUSED(pkt);
1067
1068 return 0;
1069}
1070
1071static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
1072{
1073 ARG_UNUSED(pkt);
1074}
1075
1076static inline void net_pkt_set_stats_tick(struct net_pkt *pkt, uint32_t tick)
1077{
1078 ARG_UNUSED(pkt);
1079 ARG_UNUSED(tick);
1080}
1081
1082#define net_pkt_set_tx_stats_tick(pkt, tick)
1083#define net_pkt_set_rx_stats_tick(pkt, tick)
1084#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
1085 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
1086
1087static inline size_t net_pkt_get_len(struct net_pkt *pkt)
1088{
1089 return net_buf_frags_len(pkt->frags);
1090}
1091
1092static inline uint8_t *net_pkt_data(struct net_pkt *pkt)
1093{
1094 return pkt->frags->data;
1095}
1096
1097static inline uint8_t *net_pkt_ip_data(struct net_pkt *pkt)
1098{
1099 return pkt->frags->data;
1100}
1101
1102static inline bool net_pkt_is_empty(struct net_pkt *pkt)
1103{
1104 return !pkt->buffer || !net_pkt_data(pkt) || pkt->buffer->len == 0;
1105}
1106
1107static inline struct net_linkaddr *net_pkt_lladdr_src(struct net_pkt *pkt)
1108{
1109 return &pkt->lladdr_src;
1110}
1111
1112static inline struct net_linkaddr *net_pkt_lladdr_dst(struct net_pkt *pkt)
1113{
1114 return &pkt->lladdr_dst;
1115}
1116
1117static inline void net_pkt_lladdr_swap(struct net_pkt *pkt)
1118{
1119 uint8_t *addr = net_pkt_lladdr_src(pkt)->addr;
1120
1121 net_pkt_lladdr_src(pkt)->addr = net_pkt_lladdr_dst(pkt)->addr;
1122 net_pkt_lladdr_dst(pkt)->addr = addr;
1123}
1124
1125static inline void net_pkt_lladdr_clear(struct net_pkt *pkt)
1126{
1127 net_pkt_lladdr_src(pkt)->addr = NULL;
1128 net_pkt_lladdr_src(pkt)->len = 0U;
1129}
1130
1131static inline uint16_t net_pkt_ll_proto_type(struct net_pkt *pkt)
1132{
1133 return pkt->ll_proto_type;
1134}
1135
1136static inline void net_pkt_set_ll_proto_type(struct net_pkt *pkt, uint16_t type)
1137{
1138 pkt->ll_proto_type = type;
1139}
1140
1141#if defined(CONFIG_NET_IPV4_AUTO)
1142static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1143{
1144 return !!(pkt->ipv4_auto_arp_msg);
1145}
1146
1147static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1148 bool is_auto_arp_msg)
1149{
1150 pkt->ipv4_auto_arp_msg = is_auto_arp_msg;
1151}
1152#else /* CONFIG_NET_IPV4_AUTO */
1153static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1154{
1155 ARG_UNUSED(pkt);
1156
1157 return false;
1158}
1159
1160static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1161 bool is_auto_arp_msg)
1162{
1163 ARG_UNUSED(pkt);
1164 ARG_UNUSED(is_auto_arp_msg);
1165}
1166#endif /* CONFIG_NET_IPV4_AUTO */
1167
1168#if defined(CONFIG_NET_LLDP)
1169static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1170{
1171 return !!(pkt->lldp_pkt);
1172}
1173
1174static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1175{
1176 pkt->lldp_pkt = is_lldp;
1177}
1178#else
1179static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1180{
1181 ARG_UNUSED(pkt);
1182
1183 return false;
1184}
1185
1186static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1187{
1188 ARG_UNUSED(pkt);
1189 ARG_UNUSED(is_lldp);
1190}
1191#endif /* CONFIG_NET_LLDP */
1192
1193#if defined(CONFIG_NET_L2_PPP)
1194static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1195{
1196 return !!(pkt->ppp_msg);
1197}
1198
1199static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1200 bool is_ppp_msg)
1201{
1202 pkt->ppp_msg = is_ppp_msg;
1203}
1204#else /* CONFIG_NET_L2_PPP */
1205static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1206{
1207 ARG_UNUSED(pkt);
1208
1209 return false;
1210}
1211
1212static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1213 bool is_ppp_msg)
1214{
1215 ARG_UNUSED(pkt);
1216 ARG_UNUSED(is_ppp_msg);
1217}
1218#endif /* CONFIG_NET_L2_PPP */
1219
1220#if defined(NET_PKT_HAS_CONTROL_BLOCK)
1221static inline void *net_pkt_cb(struct net_pkt *pkt)
1222{
1223 return &pkt->cb;
1224}
1225#else
1226static inline void *net_pkt_cb(struct net_pkt *pkt)
1227{
1228 ARG_UNUSED(pkt);
1229
1230 return NULL;
1231}
1232#endif
1233
1234#define NET_IPV6_HDR(pkt) ((struct net_ipv6_hdr *)net_pkt_ip_data(pkt))
1235#define NET_IPV4_HDR(pkt) ((struct net_ipv4_hdr *)net_pkt_ip_data(pkt))
1236
1237static inline void net_pkt_set_src_ipv6_addr(struct net_pkt *pkt)
1238{
1240 net_pkt_context(pkt)),
1241 (struct in6_addr *)NET_IPV6_HDR(pkt)->src);
1242}
1243
1244static inline void net_pkt_set_overwrite(struct net_pkt *pkt, bool overwrite)
1245{
1246 pkt->overwrite = overwrite;
1247}
1248
1249static inline bool net_pkt_is_being_overwritten(struct net_pkt *pkt)
1250{
1251 return !!(pkt->overwrite);
1252}
1253
1254#ifdef CONFIG_NET_PKT_FILTER
1255
1256bool net_pkt_filter_send_ok(struct net_pkt *pkt);
1257bool net_pkt_filter_recv_ok(struct net_pkt *pkt);
1258
1259#else
1260
1261static inline bool net_pkt_filter_send_ok(struct net_pkt *pkt)
1262{
1263 ARG_UNUSED(pkt);
1264
1265 return true;
1266}
1267
1268static inline bool net_pkt_filter_recv_ok(struct net_pkt *pkt)
1269{
1270 ARG_UNUSED(pkt);
1271
1272 return true;
1273}
1274
1275#endif /* CONFIG_NET_PKT_FILTER */
1276
1277#if defined(CONFIG_NET_PKT_FILTER) && \
1278 (defined(CONFIG_NET_PKT_FILTER_IPV4_HOOK) || defined(CONFIG_NET_PKT_FILTER_IPV6_HOOK))
1279
1280bool net_pkt_filter_ip_recv_ok(struct net_pkt *pkt);
1281
1282#else
1283
1284static inline bool net_pkt_filter_ip_recv_ok(struct net_pkt *pkt)
1285{
1286 ARG_UNUSED(pkt);
1287
1288 return true;
1289}
1290
1291#endif /* CONFIG_NET_PKT_FILTER_IPV4_HOOK || CONFIG_NET_PKT_FILTER_IPV6_HOOK */
1292
1293#if defined(CONFIG_NET_PKT_FILTER) && defined(CONFIG_NET_PKT_FILTER_LOCAL_IN_HOOK)
1294
1295bool net_pkt_filter_local_in_recv_ok(struct net_pkt *pkt);
1296
1297#else
1298
1299static inline bool net_pkt_filter_local_in_recv_ok(struct net_pkt *pkt)
1300{
1301 ARG_UNUSED(pkt);
1302
1303 return true;
1304}
1305
1306#endif /* CONFIG_NET_PKT_FILTER && CONFIG_NET_PKT_FILTER_LOCAL_IN_HOOK */
1307
1308/* @endcond */
1309
1323#define NET_PKT_SLAB_DEFINE(name, count) \
1324 K_MEM_SLAB_DEFINE(name, sizeof(struct net_pkt), count, 4)
1325
1326/* Backward compatibility macro */
1327#define NET_PKT_TX_SLAB_DEFINE(name, count) NET_PKT_SLAB_DEFINE(name, count)
1328
1342#define NET_PKT_DATA_POOL_DEFINE(name, count) \
1343 NET_BUF_POOL_DEFINE(name, count, CONFIG_NET_BUF_DATA_SIZE, \
1344 0, NULL)
1345
1348#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC) || \
1349 (CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG)
1350#define NET_PKT_DEBUG_ENABLED
1351#endif
1352
1353#if defined(NET_PKT_DEBUG_ENABLED)
1354
1355/* Debug versions of the net_pkt functions that are used when tracking
1356 * buffer usage.
1357 */
1358
1359struct net_buf *net_pkt_get_reserve_data_debug(struct net_buf_pool *pool,
1360 size_t min_len,
1361 k_timeout_t timeout,
1362 const char *caller,
1363 int line);
1364
1365#define net_pkt_get_reserve_data(pool, min_len, timeout) \
1366 net_pkt_get_reserve_data_debug(pool, min_len, timeout, __func__, __LINE__)
1367
1368struct net_buf *net_pkt_get_reserve_rx_data_debug(size_t min_len,
1369 k_timeout_t timeout,
1370 const char *caller,
1371 int line);
1372#define net_pkt_get_reserve_rx_data(min_len, timeout) \
1373 net_pkt_get_reserve_rx_data_debug(min_len, timeout, __func__, __LINE__)
1374
1375struct net_buf *net_pkt_get_reserve_tx_data_debug(size_t min_len,
1376 k_timeout_t timeout,
1377 const char *caller,
1378 int line);
1379#define net_pkt_get_reserve_tx_data(min_len, timeout) \
1380 net_pkt_get_reserve_tx_data_debug(min_len, timeout, __func__, __LINE__)
1381
1382struct net_buf *net_pkt_get_frag_debug(struct net_pkt *pkt, size_t min_len,
1383 k_timeout_t timeout,
1384 const char *caller, int line);
1385#define net_pkt_get_frag(pkt, min_len, timeout) \
1386 net_pkt_get_frag_debug(pkt, min_len, timeout, __func__, __LINE__)
1387
1388void net_pkt_unref_debug(struct net_pkt *pkt, const char *caller, int line);
1389#define net_pkt_unref(pkt) net_pkt_unref_debug(pkt, __func__, __LINE__)
1390
1391struct net_pkt *net_pkt_ref_debug(struct net_pkt *pkt, const char *caller,
1392 int line);
1393#define net_pkt_ref(pkt) net_pkt_ref_debug(pkt, __func__, __LINE__)
1394
1395struct net_buf *net_pkt_frag_ref_debug(struct net_buf *frag,
1396 const char *caller, int line);
1397#define net_pkt_frag_ref(frag) net_pkt_frag_ref_debug(frag, __func__, __LINE__)
1398
1399void net_pkt_frag_unref_debug(struct net_buf *frag,
1400 const char *caller, int line);
1401#define net_pkt_frag_unref(frag) \
1402 net_pkt_frag_unref_debug(frag, __func__, __LINE__)
1403
1404struct net_buf *net_pkt_frag_del_debug(struct net_pkt *pkt,
1405 struct net_buf *parent,
1406 struct net_buf *frag,
1407 const char *caller, int line);
1408#define net_pkt_frag_del(pkt, parent, frag) \
1409 net_pkt_frag_del_debug(pkt, parent, frag, __func__, __LINE__)
1410
1411void net_pkt_frag_add_debug(struct net_pkt *pkt, struct net_buf *frag,
1412 const char *caller, int line);
1413#define net_pkt_frag_add(pkt, frag) \
1414 net_pkt_frag_add_debug(pkt, frag, __func__, __LINE__)
1415
1416void net_pkt_frag_insert_debug(struct net_pkt *pkt, struct net_buf *frag,
1417 const char *caller, int line);
1418#define net_pkt_frag_insert(pkt, frag) \
1419 net_pkt_frag_insert_debug(pkt, frag, __func__, __LINE__)
1420#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC ||
1421 * CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG
1422 */
1432#if defined(NET_PKT_DEBUG_ENABLED)
1433void net_pkt_print_frags(struct net_pkt *pkt);
1434#else
1435#define net_pkt_print_frags(pkt)
1436#endif
1437
1452#if !defined(NET_PKT_DEBUG_ENABLED)
1453struct net_buf *net_pkt_get_reserve_rx_data(size_t min_len, k_timeout_t timeout);
1454#endif
1455
1470#if !defined(NET_PKT_DEBUG_ENABLED)
1471struct net_buf *net_pkt_get_reserve_tx_data(size_t min_len, k_timeout_t timeout);
1472#endif
1473
1486#if !defined(NET_PKT_DEBUG_ENABLED)
1487struct net_buf *net_pkt_get_frag(struct net_pkt *pkt, size_t min_len,
1488 k_timeout_t timeout);
1489#endif
1490
1500#if !defined(NET_PKT_DEBUG_ENABLED)
1501void net_pkt_unref(struct net_pkt *pkt);
1502#endif
1503
1513#if !defined(NET_PKT_DEBUG_ENABLED)
1514struct net_pkt *net_pkt_ref(struct net_pkt *pkt);
1515#endif
1516
1526#if !defined(NET_PKT_DEBUG_ENABLED)
1527struct net_buf *net_pkt_frag_ref(struct net_buf *frag);
1528#endif
1529
1535#if !defined(NET_PKT_DEBUG_ENABLED)
1536void net_pkt_frag_unref(struct net_buf *frag);
1537#endif
1538
1549#if !defined(NET_PKT_DEBUG_ENABLED)
1551 struct net_buf *parent,
1552 struct net_buf *frag);
1553#endif
1554
1561#if !defined(NET_PKT_DEBUG_ENABLED)
1562void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag);
1563#endif
1564
1571#if !defined(NET_PKT_DEBUG_ENABLED)
1572void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag);
1573#endif
1574
1581void net_pkt_compact(struct net_pkt *pkt);
1582
1591void net_pkt_get_info(struct k_mem_slab **rx,
1592 struct k_mem_slab **tx,
1593 struct net_buf_pool **rx_data,
1594 struct net_buf_pool **tx_data);
1595
1598#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
1602void net_pkt_print(void);
1603
1604typedef void (*net_pkt_allocs_cb_t)(struct net_pkt *pkt,
1605 struct net_buf *buf,
1606 const char *func_alloc,
1607 int line_alloc,
1608 const char *func_free,
1609 int line_free,
1610 bool in_use,
1611 void *user_data);
1612
1613void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data);
1614
1615const char *net_pkt_slab2str(struct k_mem_slab *slab);
1616const char *net_pkt_pool2str(struct net_buf_pool *pool);
1617
1618#else
1619#define net_pkt_print(...)
1620#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
1621
1622/* New allocator, and API are defined below.
1623 * This will be simpler when time will come to get rid of former API above.
1624 */
1625#if defined(NET_PKT_DEBUG_ENABLED)
1626
1627struct net_pkt *net_pkt_alloc_debug(k_timeout_t timeout,
1628 const char *caller, int line);
1629#define net_pkt_alloc(_timeout) \
1630 net_pkt_alloc_debug(_timeout, __func__, __LINE__)
1631
1632struct net_pkt *net_pkt_alloc_from_slab_debug(struct k_mem_slab *slab,
1633 k_timeout_t timeout,
1634 const char *caller, int line);
1635#define net_pkt_alloc_from_slab(_slab, _timeout) \
1636 net_pkt_alloc_from_slab_debug(_slab, _timeout, __func__, __LINE__)
1637
1638struct net_pkt *net_pkt_rx_alloc_debug(k_timeout_t timeout,
1639 const char *caller, int line);
1640#define net_pkt_rx_alloc(_timeout) \
1641 net_pkt_rx_alloc_debug(_timeout, __func__, __LINE__)
1642
1643struct net_pkt *net_pkt_alloc_on_iface_debug(struct net_if *iface,
1644 k_timeout_t timeout,
1645 const char *caller,
1646 int line);
1647#define net_pkt_alloc_on_iface(_iface, _timeout) \
1648 net_pkt_alloc_on_iface_debug(_iface, _timeout, __func__, __LINE__)
1649
1650struct net_pkt *net_pkt_rx_alloc_on_iface_debug(struct net_if *iface,
1651 k_timeout_t timeout,
1652 const char *caller,
1653 int line);
1654#define net_pkt_rx_alloc_on_iface(_iface, _timeout) \
1655 net_pkt_rx_alloc_on_iface_debug(_iface, _timeout, \
1656 __func__, __LINE__)
1657
1658int net_pkt_alloc_buffer_debug(struct net_pkt *pkt,
1659 size_t size,
1660 enum net_ip_protocol proto,
1661 k_timeout_t timeout,
1662 const char *caller, int line);
1663#define net_pkt_alloc_buffer(_pkt, _size, _proto, _timeout) \
1664 net_pkt_alloc_buffer_debug(_pkt, _size, _proto, _timeout, \
1665 __func__, __LINE__)
1666
1667struct net_pkt *net_pkt_alloc_with_buffer_debug(struct net_if *iface,
1668 size_t size,
1669 sa_family_t family,
1670 enum net_ip_protocol proto,
1671 k_timeout_t timeout,
1672 const char *caller,
1673 int line);
1674#define net_pkt_alloc_with_buffer(_iface, _size, _family, \
1675 _proto, _timeout) \
1676 net_pkt_alloc_with_buffer_debug(_iface, _size, _family, \
1677 _proto, _timeout, \
1678 __func__, __LINE__)
1679
1680struct net_pkt *net_pkt_rx_alloc_with_buffer_debug(struct net_if *iface,
1681 size_t size,
1682 sa_family_t family,
1683 enum net_ip_protocol proto,
1684 k_timeout_t timeout,
1685 const char *caller,
1686 int line);
1687#define net_pkt_rx_alloc_with_buffer(_iface, _size, _family, \
1688 _proto, _timeout) \
1689 net_pkt_rx_alloc_with_buffer_debug(_iface, _size, _family, \
1690 _proto, _timeout, \
1691 __func__, __LINE__)
1692#endif /* NET_PKT_DEBUG_ENABLED */
1705#if !defined(NET_PKT_DEBUG_ENABLED)
1707#endif
1708
1723#if !defined(NET_PKT_DEBUG_ENABLED)
1724struct net_pkt *net_pkt_alloc_from_slab(struct k_mem_slab *slab,
1725 k_timeout_t timeout);
1726#endif
1727
1738#if !defined(NET_PKT_DEBUG_ENABLED)
1740#endif
1741
1750#if !defined(NET_PKT_DEBUG_ENABLED)
1752 k_timeout_t timeout);
1753
1754/* Same as above but specifically for RX packet */
1756 k_timeout_t timeout);
1757#endif
1758
1774#if !defined(NET_PKT_DEBUG_ENABLED)
1776 size_t size,
1777 enum net_ip_protocol proto,
1778 k_timeout_t timeout);
1779#endif
1780
1792#if !defined(NET_PKT_DEBUG_ENABLED)
1794 size_t size,
1795 sa_family_t family,
1796 enum net_ip_protocol proto,
1797 k_timeout_t timeout);
1798
1799/* Same as above but specifically for RX packet */
1801 size_t size,
1802 sa_family_t family,
1803 enum net_ip_protocol proto,
1804 k_timeout_t timeout);
1805#endif
1806
1813void net_pkt_append_buffer(struct net_pkt *pkt, struct net_buf *buffer);
1814
1826
1843 enum net_ip_protocol proto);
1844
1854
1869int net_pkt_remove_tail(struct net_pkt *pkt, size_t length);
1870
1879
1886static inline void net_pkt_cursor_backup(struct net_pkt *pkt,
1887 struct net_pkt_cursor *backup)
1888{
1889 backup->buf = pkt->cursor.buf;
1890 backup->pos = pkt->cursor.pos;
1891}
1892
1899static inline void net_pkt_cursor_restore(struct net_pkt *pkt,
1900 struct net_pkt_cursor *backup)
1901{
1902 pkt->cursor.buf = backup->buf;
1903 pkt->cursor.pos = backup->pos;
1904}
1905
1913static inline void *net_pkt_cursor_get_pos(struct net_pkt *pkt)
1914{
1915 return pkt->cursor.pos;
1916}
1917
1938int net_pkt_skip(struct net_pkt *pkt, size_t length);
1939
1954int net_pkt_memset(struct net_pkt *pkt, int byte, size_t length);
1955
1969int net_pkt_copy(struct net_pkt *pkt_dst,
1970 struct net_pkt *pkt_src,
1971 size_t length);
1972
1982struct net_pkt *net_pkt_clone(struct net_pkt *pkt, k_timeout_t timeout);
1983
1993struct net_pkt *net_pkt_rx_clone(struct net_pkt *pkt, k_timeout_t timeout);
1994
2004 k_timeout_t timeout);
2005
2019int net_pkt_read(struct net_pkt *pkt, void *data, size_t length);
2020
2021/* Read uint8_t data data a net_pkt */
2022static inline int net_pkt_read_u8(struct net_pkt *pkt, uint8_t *data)
2023{
2024 return net_pkt_read(pkt, data, 1);
2025}
2026
2039int net_pkt_read_be16(struct net_pkt *pkt, uint16_t *data);
2040
2053int net_pkt_read_le16(struct net_pkt *pkt, uint16_t *data);
2054
2067int net_pkt_read_be32(struct net_pkt *pkt, uint32_t *data);
2068
2082int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length);
2083
2084/* Write uint8_t data into a net_pkt. */
2085static inline int net_pkt_write_u8(struct net_pkt *pkt, uint8_t data)
2086{
2087 return net_pkt_write(pkt, &data, sizeof(uint8_t));
2088}
2089
2090/* Write uint16_t big endian data into a net_pkt. */
2091static inline int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
2092{
2093 uint16_t data_be16 = htons(data);
2094
2095 return net_pkt_write(pkt, &data_be16, sizeof(uint16_t));
2096}
2097
2098/* Write uint32_t big endian data into a net_pkt. */
2099static inline int net_pkt_write_be32(struct net_pkt *pkt, uint32_t data)
2100{
2101 uint32_t data_be32 = htonl(data);
2102
2103 return net_pkt_write(pkt, &data_be32, sizeof(uint32_t));
2104}
2105
2106/* Write uint32_t little endian data into a net_pkt. */
2107static inline int net_pkt_write_le32(struct net_pkt *pkt, uint32_t data)
2108{
2109 uint32_t data_le32 = sys_cpu_to_le32(data);
2110
2111 return net_pkt_write(pkt, &data_le32, sizeof(uint32_t));
2112}
2113
2114/* Write uint16_t little endian data into a net_pkt. */
2115static inline int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
2116{
2117 uint16_t data_le16 = sys_cpu_to_le16(data);
2118
2119 return net_pkt_write(pkt, &data_le16, sizeof(uint16_t));
2120}
2121
2130
2143int net_pkt_update_length(struct net_pkt *pkt, size_t length);
2144
2157int net_pkt_pull(struct net_pkt *pkt, size_t length);
2158
2168
2180bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size);
2181
2191
2193#if !defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2194 void *data;
2195#endif
2196 const size_t size;
2197};
2198
2199#if defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2200#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2201 struct net_pkt_data_access _name = { \
2202 .size = sizeof(_type), \
2203 }
2204
2205#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2206 NET_PKT_DATA_ACCESS_DEFINE(_name, _type)
2207
2208#else
2209#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2210 _type _hdr_##_name; \
2211 struct net_pkt_data_access _name = { \
2212 .data = &_hdr_##_name, \
2213 .size = sizeof(_type), \
2214 }
2215
2216#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2217 struct net_pkt_data_access _name = { \
2218 .data = NULL, \
2219 .size = sizeof(_type), \
2220 }
2221
2222#endif /* CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS */
2223
2237void *net_pkt_get_data(struct net_pkt *pkt,
2238 struct net_pkt_data_access *access);
2239
2254 struct net_pkt_data_access *access);
2255
2260static inline int net_pkt_acknowledge_data(struct net_pkt *pkt,
2261 struct net_pkt_data_access *access)
2262{
2263 return net_pkt_skip(pkt, access->size);
2264}
2265
2270#ifdef __cplusplus
2271}
2272#endif
2273
2274#endif /* ZEPHYR_INCLUDE_NET_NET_PKT_H_ */
long atomic_t
Definition: atomic.h:22
#define ALWAYS_INLINE
Definition: common.h:129
VLAN specific definitions.
#define NSEC_PER_SEC
number of nanoseconds per second
Definition: sys_clock.h:107
unsigned short int sa_family_t
Socket address family type.
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
Protocol numbers from IANA/BSD.
Definition: net_ip.h:62
static size_t net_buf_frags_len(struct net_buf *buf)
Calculate amount of bytes stored in fragments.
Definition: buf.h:2454
static struct net_if * net_context_get_iface(struct net_context *context)
Get network interface for this context.
Definition: net_context.h:649
static struct net_linkaddr * net_if_get_link_addr(struct net_if *iface)
Get an network interface's link address.
Definition: net_if.h:945
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:1762
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:2099
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:1435
int net_pkt_update_length(struct net_pkt *pkt, size_t length)
Update the overall length of a packet.
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.
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.
static int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:2091
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:2085
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.
struct net_buf * net_pkt_get_reserve_tx_data(size_t min_len, k_timeout_t timeout)
Get TX DATA buffer from pool.
static void * net_pkt_cursor_get_pos(struct net_pkt *pkt)
Returns current position of the cursor.
Definition: net_pkt.h:1913
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:1886
void net_pkt_compact(struct net_pkt *pkt)
Compact the fragment list of a packet.
static int net_pkt_acknowledge_data(struct net_pkt *pkt, struct net_pkt_data_access *access)
Acknowledge previously contiguous data taken from a network packet Packet needs to be set to overwrit...
Definition: net_pkt.h:2260
static int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:2115
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:1899
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.
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.
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:2107
struct net_buf * net_pkt_get_reserve_rx_data(size_t min_len, k_timeout_t timeout)
Get RX DATA buffer from pool.
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:2022
struct net_buf * net_pkt_get_frag(struct net_pkt *pkt, size_t min_len, k_timeout_t timeout)
Get a data fragment that might be from user specific buffer pool or from global DATA pool.
size_t net_pkt_get_contiguous_len(struct net_pkt *pkt)
Get the contiguous buffer space.
int64_t net_time_t
Any occurrence of net_time_t specifies a concept of nanosecond resolution scalar time span,...
Definition: net_time.h:101
static net_time_t net_ptp_time_to_ns(struct net_ptp_time *ts)
Convert a PTP timestamp to a nanosecond precision timestamp, both related to the local network refere...
Definition: ptp_time.h:193
static struct net_ptp_time ns_to_net_ptp_time(net_time_t nsec)
Convert a nanosecond precision timestamp to a PTP timestamp, both related to the local network refere...
Definition: ptp_time.h:214
struct _snode sys_snode_t
Single-linked list node structure.
Definition: slist.h:39
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:124
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
Unspecified VLAN tag value.
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
Packet data common to all IEEE 802.15.4 L2 layers.
Buffer management.
Network context definitions.
Network core definitions.
Public API for network interface.
IPv6 and IPv4 definitions.
Public API for network link address.
Representation of nanosecond resolution elapsed time and timestamps in the network stack.
flags
Definition: parser.h:96
Public functions for the Precision Time Protocol time specification.
__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)
IPv6 address struct.
Definition: net_ip.h:139
Kernel timeout type.
Definition: sys_clock.h:65
Network buffer pool representation.
Definition: buf.h:971
Network buffer representation.
Definition: buf.h:906
uint8_t * data
Pointer to the start of data in the buffer.
Definition: buf.h:932
uint8_t user_data[]
System metadata for this buffer.
Definition: buf.h:951
uint16_t len
Length of the data behind the data pointer.
Definition: buf.h:935
Note that we do not store the actual source IP address in the context because the address is already ...
Definition: net_context.h:201
Network Interface structure.
Definition: net_if.h:595
Hardware link address structure.
Definition: net_linkaddr.h:67
uint8_t * addr
The array of byte representing the address.
Definition: net_linkaddr.h:69
uint8_t type
What kind of address is this for.
Definition: net_linkaddr.h:75
Definition: net_pkt.h:50
uint8_t * pos
Current position in the data buffer of the net_buf.
Definition: net_pkt.h:54
struct net_buf * buf
Current net_buf pointer by the cursor.
Definition: net_pkt.h:52
Definition: net_pkt.h:2192
const size_t size
Definition: net_pkt.h:2196
void * data
Definition: net_pkt.h:2194
Network packet.
Definition: net_pkt.h:63
struct net_buf * frags
Definition: net_pkt.h:75
struct net_context * context
Network connection context.
Definition: net_pkt.h:83
struct net_pkt_cursor cursor
Internal buffer iterator used for reading/writing.
Definition: net_pkt.h:80
struct net_if * iface
Network interface.
Definition: net_pkt.h:86
intptr_t fifo
The fifo is used by RX/TX threads and by socket layer.
Definition: net_pkt.h:68
struct net_buf * buffer
Definition: net_pkt.h:76
struct k_mem_slab * slab
Slab pointer from where it belongs to.
Definition: net_pkt.h:71
(Generalized) Precision Time Protocol Timestamp format.
Definition: ptp_time.h:109
uint32_t nanosecond
Nanoseconds.
Definition: ptp_time.h:127
uint64_t second
Definition: ptp_time.h:123
Definition: stat.h:92
#define sys_cpu_to_le32(val)
Convert 32-bit integer from host endianness to little-endian.
Definition: byteorder.h:262
#define sys_cpu_to_le16(val)
Convert 16-bit integer from host endianness to little-endian.
Definition: byteorder.h:258