Zephyr Project API  3.3.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#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>
33#include <zephyr/net/ptp_time.h>
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
46struct net_context;
47
48/* buffer cursor used in net_pkt */
51 struct net_buf *buf;
54};
55
62struct net_pkt {
68
70 struct k_mem_slab *slab;
71
73 union {
74 struct net_buf *frags;
75 struct net_buf *buffer;
76 };
77
80
83
85 struct net_if *iface;
86
89#if defined(CONFIG_NET_TCP)
91 sys_snode_t next;
92#endif
93#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
94 struct net_if *orig_iface; /* Original network interface */
95#endif
96
97#if defined(CONFIG_NET_PKT_TIMESTAMP)
99 struct net_ptp_time timestamp;
100#endif
101
102#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
103 struct {
105 uint32_t create_time;
106
107#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
108 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
114 struct {
115 uint32_t stat[NET_PKT_DETAIL_STATS_COUNT];
116 int count;
117 } detail;
118#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
119 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
120 };
121#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
122
123#if defined(CONFIG_NET_PKT_TXTIME)
125 uint64_t txtime;
126#endif /* CONFIG_NET_PKT_TXTIME */
127
129 atomic_t atomic_ref;
130
131 /* Filled by layer 2 when network packet is received. */
132 struct net_linkaddr lladdr_src;
133 struct net_linkaddr lladdr_dst;
134 uint16_t ll_proto_type;
135
136#if defined(CONFIG_NET_IP)
137 uint8_t ip_hdr_len; /* pre-filled in order to avoid func call */
138#endif
139
140 uint8_t overwrite : 1; /* Is packet content being overwritten? */
141 uint8_t sent_or_eof : 1; /* For outgoing packet: is this sent or not
142 * For incoming packet of a socket: last
143 * packet before EOF
144 * Used only if defined(CONFIG_NET_TCP)
145 */
146 uint8_t pkt_queued : 1; /* For outgoing packet: is this packet
147 * queued to be sent but has not reached
148 * the driver yet.
149 * Used only if defined(CONFIG_NET_TCP)
150 */
151 uint8_t ptp_pkt : 1; /* For outgoing packet: is this packet
152 * a L2 PTP packet.
153 * Used only if defined (CONFIG_NET_L2_PTP)
154 */
155 uint8_t forwarding : 1; /* Are we forwarding this pkt
156 * Used only if defined(CONFIG_NET_ROUTE)
157 */
158 uint8_t family : 3; /* Address family, see net_ip.h */
159
160 /* bitfield byte alignment boundary */
161
162#if defined(CONFIG_NET_IPV4_AUTO)
163 uint8_t ipv4_auto_arp_msg : 1; /* Is this pkt IPv4 autoconf ARP
164 * message.
165 * Note: family needs to be
166 * AF_INET.
167 */
168#endif
169#if defined(CONFIG_NET_LLDP)
170 uint8_t lldp_pkt : 1; /* Is this pkt an LLDP message.
171 * Note: family needs to be
172 * AF_UNSPEC.
173 */
174#endif
175 uint8_t ppp_msg : 1; /* This is a PPP message */
176#if defined(CONFIG_NET_TCP)
177 uint8_t tcp_first_msg : 1; /* Is this the first time this pkt is
178 * sent, or is this a resend of a TCP
179 * segment.
180 */
181#endif
182 uint8_t captured : 1; /* Set to 1 if this packet is already being
183 * captured
184 */
185 uint8_t l2_bridged : 1; /* set to 1 if this packet comes from a bridge
186 * and already contains its L2 header to be
187 * preserved. Useful only if
188 * defined(CONFIG_NET_ETHERNET_BRIDGE).
189 */
190 uint8_t l2_processed : 1; /* Set to 1 if this packet has already been
191 * processed by the L2
192 */
193
194 /* bitfield byte alignment boundary */
195
196#if defined(CONFIG_NET_IP)
197 union {
198 /* IPv6 hop limit or IPv4 ttl for this network packet.
199 * The value is shared between IPv6 and IPv4.
200 */
201#if defined(CONFIG_NET_IPV6)
202 uint8_t ipv6_hop_limit;
203#endif
204#if defined(CONFIG_NET_IPV4)
205 uint8_t ipv4_ttl;
206#endif
207 };
208
209 union {
210#if defined(CONFIG_NET_IPV4)
211 uint8_t ipv4_opts_len; /* length of IPv4 header options */
212#endif
213#if defined(CONFIG_NET_IPV6)
214 uint16_t ipv6_ext_len; /* length of extension headers */
215#endif
216 };
217
218#if defined(CONFIG_NET_IPV4_FRAGMENT) || defined(CONFIG_NET_IPV6_FRAGMENT)
219 union {
220#if defined(CONFIG_NET_IPV4_FRAGMENT)
221 struct {
222 uint16_t flags; /* Fragment offset and M (More Fragment) flag */
223 uint16_t id; /* Fragment ID */
224 } ipv4_fragment;
225#endif /* CONFIG_NET_IPV4_FRAGMENT */
226#if defined(CONFIG_NET_IPV6_FRAGMENT)
227 struct {
228 uint16_t flags; /* Fragment offset and M (More Fragment) flag */
229 uint32_t id; /* Fragment id */
230 uint16_t hdr_start; /* Where starts the fragment header */
231 } ipv6_fragment;
232#endif /* CONFIG_NET_IPV6_FRAGMENT */
233 };
234#endif /* CONFIG_NET_IPV4_FRAGMENT || CONFIG_NET_IPV6_FRAGMENT */
235
236#if defined(CONFIG_NET_IPV6)
237 /* Where is the start of the last header before payload data
238 * in IPv6 packet. This is offset value from start of the IPv6
239 * packet. Note that this value should be updated by who ever
240 * adds IPv6 extension headers to the network packet.
241 */
242 uint16_t ipv6_prev_hdr_start;
243
244 uint8_t ipv6_ext_opt_len; /* IPv6 ND option length */
245 uint8_t ipv6_next_hdr; /* What is the very first next header */
246#endif /* CONFIG_NET_IPV6 */
247
248#if defined(CONFIG_NET_IP_DSCP_ECN)
250 uint8_t ip_dscp : 6;
251
253 uint8_t ip_ecn : 2;
254#endif /* CONFIG_NET_IP_DSCP_ECN */
255#endif /* CONFIG_NET_IP */
256
257#if defined(CONFIG_NET_VLAN)
258 /* VLAN TCI (Tag Control Information). This contains the Priority
259 * Code Point (PCP), Drop Eligible Indicator (DEI) and VLAN
260 * Identifier (VID, called more commonly VLAN tag). This value is
261 * kept in host byte order.
262 */
263 uint16_t vlan_tci;
264#endif /* CONFIG_NET_VLAN */
265
266#if defined(NET_PKT_HAS_CONTROL_BLOCK)
267 /* TODO: Evolve this into a union of orthogonal
268 * control block declarations if further L2
269 * stacks require L2-specific attributes.
270 */
271#if defined(CONFIG_IEEE802154)
272 /* The following structure requires a 4-byte alignment
273 * boundary to avoid padding.
274 */
275 struct net_pkt_cb_ieee802154 cb;
276#endif /* CONFIG_IEEE802154 */
277#endif /* NET_PKT_HAS_CONTROL_BLOCK */
278
282 uint8_t priority;
283
284 /* @endcond */
285};
286
289/* The interface real ll address */
290static inline struct net_linkaddr *net_pkt_lladdr_if(struct net_pkt *pkt)
291{
292 return net_if_get_link_addr(pkt->iface);
293}
294
295static inline struct net_context *net_pkt_context(struct net_pkt *pkt)
296{
297 return pkt->context;
298}
299
300static inline void net_pkt_set_context(struct net_pkt *pkt,
301 struct net_context *ctx)
302{
303 pkt->context = ctx;
304}
305
306static inline struct net_if *net_pkt_iface(struct net_pkt *pkt)
307{
308 return pkt->iface;
309}
310
311static inline void net_pkt_set_iface(struct net_pkt *pkt, struct net_if *iface)
312{
313 pkt->iface = iface;
314
315 /* If the network interface is set in pkt, then also set the type of
316 * the network address that is stored in pkt. This is done here so
317 * that the address type is properly set and is not forgotten.
318 */
319 if (iface) {
320 pkt->lladdr_src.type = net_if_get_link_addr(iface)->type;
321 pkt->lladdr_dst.type = net_if_get_link_addr(iface)->type;
322 }
323}
324
325static inline struct net_if *net_pkt_orig_iface(struct net_pkt *pkt)
326{
327#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
328 return pkt->orig_iface;
329#else
330 return pkt->iface;
331#endif
332}
333
334static inline void net_pkt_set_orig_iface(struct net_pkt *pkt,
335 struct net_if *iface)
336{
337#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
338 pkt->orig_iface = iface;
339#endif
340}
341
342static inline uint8_t net_pkt_family(struct net_pkt *pkt)
343{
344 return pkt->family;
345}
346
347static inline void net_pkt_set_family(struct net_pkt *pkt, uint8_t family)
348{
349 pkt->family = family;
350}
351
352static inline bool net_pkt_is_ptp(struct net_pkt *pkt)
353{
354 return !!(pkt->ptp_pkt);
355}
356
357static inline void net_pkt_set_ptp(struct net_pkt *pkt, bool is_ptp)
358{
359 pkt->ptp_pkt = is_ptp;
360}
361
362static inline bool net_pkt_is_captured(struct net_pkt *pkt)
363{
364 return !!(pkt->captured);
365}
366
367static inline void net_pkt_set_captured(struct net_pkt *pkt, bool is_captured)
368{
369 pkt->captured = is_captured;
370}
371
372static inline bool net_pkt_is_l2_bridged(struct net_pkt *pkt)
373{
374 return IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) ? !!(pkt->l2_bridged) : 0;
375}
376
377static inline void net_pkt_set_l2_bridged(struct net_pkt *pkt, bool is_l2_bridged)
378{
379 if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE)) {
380 pkt->l2_bridged = is_l2_bridged;
381 }
382}
383
384static inline bool net_pkt_is_l2_processed(struct net_pkt *pkt)
385{
386 return !!(pkt->l2_processed);
387}
388
389static inline void net_pkt_set_l2_processed(struct net_pkt *pkt,
390 bool is_l2_processed)
391{
392 pkt->l2_processed = is_l2_processed;
393}
394
395static inline uint8_t net_pkt_ip_hdr_len(struct net_pkt *pkt)
396{
397#if defined(CONFIG_NET_IP)
398 return pkt->ip_hdr_len;
399#else
400 return 0;
401#endif
402}
403
404static inline void net_pkt_set_ip_hdr_len(struct net_pkt *pkt, uint8_t len)
405{
406#if defined(CONFIG_NET_IP)
407 pkt->ip_hdr_len = len;
408#endif
409}
410
411static inline uint8_t net_pkt_ip_dscp(struct net_pkt *pkt)
412{
413#if defined(CONFIG_NET_IP_DSCP_ECN)
414 return pkt->ip_dscp;
415#else
416 return 0;
417#endif
418}
419
420static inline void net_pkt_set_ip_dscp(struct net_pkt *pkt, uint8_t dscp)
421{
422#if defined(CONFIG_NET_IP_DSCP_ECN)
423 pkt->ip_dscp = dscp;
424#endif
425}
426
427static inline uint8_t net_pkt_ip_ecn(struct net_pkt *pkt)
428{
429#if defined(CONFIG_NET_IP_DSCP_ECN)
430 return pkt->ip_ecn;
431#else
432 return 0;
433#endif
434}
435
436static inline void net_pkt_set_ip_ecn(struct net_pkt *pkt, uint8_t ecn)
437{
438#if defined(CONFIG_NET_IP_DSCP_ECN)
439 pkt->ip_ecn = ecn;
440#endif
441}
442
443static inline uint8_t net_pkt_sent(struct net_pkt *pkt)
444{
445 return pkt->sent_or_eof;
446}
447
448static inline void net_pkt_set_sent(struct net_pkt *pkt, bool sent)
449{
450 pkt->sent_or_eof = sent;
451}
452
453static inline uint8_t net_pkt_queued(struct net_pkt *pkt)
454{
455 return pkt->pkt_queued;
456}
457
458static inline void net_pkt_set_queued(struct net_pkt *pkt, bool send)
459{
460 pkt->pkt_queued = send;
461}
462
463static inline uint8_t net_pkt_tcp_1st_msg(struct net_pkt *pkt)
464{
465#if defined(CONFIG_NET_TCP)
466 return pkt->tcp_first_msg;
467#else
468 return true;
469#endif
470}
471
472static inline void net_pkt_set_tcp_1st_msg(struct net_pkt *pkt, bool is_1st)
473{
474#if defined(CONFIG_NET_TCP)
475 pkt->tcp_first_msg = is_1st;
476#else
477 ARG_UNUSED(pkt);
478 ARG_UNUSED(is_1st);
479#endif
480}
481
482#if defined(CONFIG_NET_SOCKETS)
483static inline uint8_t net_pkt_eof(struct net_pkt *pkt)
484{
485 return pkt->sent_or_eof;
486}
487
488static inline void net_pkt_set_eof(struct net_pkt *pkt, bool eof)
489{
490 pkt->sent_or_eof = eof;
491}
492#endif
493
494#if defined(CONFIG_NET_ROUTE)
495static inline bool net_pkt_forwarding(struct net_pkt *pkt)
496{
497 return pkt->forwarding;
498}
499
500static inline void net_pkt_set_forwarding(struct net_pkt *pkt, bool forward)
501{
502 pkt->forwarding = forward;
503}
504#else
505static inline bool net_pkt_forwarding(struct net_pkt *pkt)
506{
507 return false;
508}
509#endif
510
511#if defined(CONFIG_NET_IPV4)
512static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
513{
514 return pkt->ipv4_ttl;
515}
516
517static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
518 uint8_t ttl)
519{
520 pkt->ipv4_ttl = ttl;
521}
522
523static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
524{
525 return pkt->ipv4_opts_len;
526}
527
528static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
529 uint8_t opts_len)
530{
531 pkt->ipv4_opts_len = opts_len;
532}
533#else
534static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
535{
536 ARG_UNUSED(pkt);
537
538 return 0;
539}
540
541static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
542 uint8_t ttl)
543{
544 ARG_UNUSED(pkt);
545 ARG_UNUSED(ttl);
546}
547
548static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
549{
550 ARG_UNUSED(pkt);
551 return 0;
552}
553
554static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
555 uint8_t opts_len)
556{
557 ARG_UNUSED(pkt);
558 ARG_UNUSED(opts_len);
559}
560#endif
561
562#if defined(CONFIG_NET_IPV6)
563static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
564{
565 return pkt->ipv6_ext_opt_len;
566}
567
568static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
569 uint8_t len)
570{
571 pkt->ipv6_ext_opt_len = len;
572}
573
574static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
575{
576 return pkt->ipv6_next_hdr;
577}
578
579static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
580 uint8_t next_hdr)
581{
582 pkt->ipv6_next_hdr = next_hdr;
583}
584
585static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
586{
587 return pkt->ipv6_ext_len;
588}
589
590static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
591{
592 pkt->ipv6_ext_len = len;
593}
594
595static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
596{
597 return pkt->ipv6_prev_hdr_start;
598}
599
600static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
601 uint16_t offset)
602{
603 pkt->ipv6_prev_hdr_start = offset;
604}
605
606static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
607{
608 return pkt->ipv6_hop_limit;
609}
610
611static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
612 uint8_t hop_limit)
613{
614 pkt->ipv6_hop_limit = hop_limit;
615}
616#else /* CONFIG_NET_IPV6 */
617static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
618{
619 ARG_UNUSED(pkt);
620
621 return 0;
622}
623
624static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
625 uint8_t len)
626{
627 ARG_UNUSED(pkt);
628 ARG_UNUSED(len);
629}
630
631static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
632{
633 ARG_UNUSED(pkt);
634
635 return 0;
636}
637
638static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
639 uint8_t next_hdr)
640{
641 ARG_UNUSED(pkt);
642 ARG_UNUSED(next_hdr);
643}
644
645static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
646{
647 ARG_UNUSED(pkt);
648
649 return 0;
650}
651
652static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
653{
654 ARG_UNUSED(pkt);
655 ARG_UNUSED(len);
656}
657
658static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
659{
660 ARG_UNUSED(pkt);
661
662 return 0;
663}
664
665static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
666 uint16_t offset)
667{
668 ARG_UNUSED(pkt);
669 ARG_UNUSED(offset);
670}
671
672static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
673{
674 ARG_UNUSED(pkt);
675
676 return 0;
677}
678
679static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
680 uint8_t hop_limit)
681{
682 ARG_UNUSED(pkt);
683 ARG_UNUSED(hop_limit);
684}
685#endif /* CONFIG_NET_IPV6 */
686
687static inline uint16_t net_pkt_ip_opts_len(struct net_pkt *pkt)
688{
689#if defined(CONFIG_NET_IPV6)
690 return pkt->ipv6_ext_len;
691#elif defined(CONFIG_NET_IPV4)
692 return pkt->ipv4_opts_len;
693#else
694 ARG_UNUSED(pkt);
695
696 return 0;
697#endif
698}
699
700#if defined(CONFIG_NET_IPV4_FRAGMENT)
701static inline uint16_t net_pkt_ipv4_fragment_offset(struct net_pkt *pkt)
702{
703 return (pkt->ipv4_fragment.flags & NET_IPV4_FRAGH_OFFSET_MASK) * 8;
704}
705
706static inline bool net_pkt_ipv4_fragment_more(struct net_pkt *pkt)
707{
708 return (pkt->ipv4_fragment.flags & NET_IPV4_MORE_FRAG_MASK) != 0;
709}
710
711static inline void net_pkt_set_ipv4_fragment_flags(struct net_pkt *pkt, uint16_t flags)
712{
713 pkt->ipv4_fragment.flags = flags;
714}
715
716static inline uint32_t net_pkt_ipv4_fragment_id(struct net_pkt *pkt)
717{
718 return pkt->ipv4_fragment.id;
719}
720
721static inline void net_pkt_set_ipv4_fragment_id(struct net_pkt *pkt, uint32_t id)
722{
723 pkt->ipv4_fragment.id = id;
724}
725#else /* CONFIG_NET_IPV4_FRAGMENT */
726static inline uint16_t net_pkt_ipv4_fragment_offset(struct net_pkt *pkt)
727{
728 ARG_UNUSED(pkt);
729
730 return 0;
731}
732
733static inline bool net_pkt_ipv4_fragment_more(struct net_pkt *pkt)
734{
735 ARG_UNUSED(pkt);
736
737 return 0;
738}
739
740static inline void net_pkt_set_ipv4_fragment_flags(struct net_pkt *pkt, uint16_t flags)
741{
742 ARG_UNUSED(pkt);
743 ARG_UNUSED(flags);
744}
745
746static inline uint32_t net_pkt_ipv4_fragment_id(struct net_pkt *pkt)
747{
748 ARG_UNUSED(pkt);
749
750 return 0;
751}
752
753static inline void net_pkt_set_ipv4_fragment_id(struct net_pkt *pkt, uint32_t id)
754{
755 ARG_UNUSED(pkt);
756 ARG_UNUSED(id);
757}
758#endif /* CONFIG_NET_IPV4_FRAGMENT */
759
760#if defined(CONFIG_NET_IPV6_FRAGMENT)
761static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
762{
763 return pkt->ipv6_fragment.hdr_start;
764}
765
766static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
767 uint16_t start)
768{
769 pkt->ipv6_fragment.hdr_start = start;
770}
771
772static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
773{
774 return pkt->ipv6_fragment.flags & NET_IPV6_FRAGH_OFFSET_MASK;
775}
776static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
777{
778 return (pkt->ipv6_fragment.flags & 0x01) != 0;
779}
780
781static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
783{
784 pkt->ipv6_fragment.flags = flags;
785}
786
787static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
788{
789 return pkt->ipv6_fragment.id;
790}
791
792static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
793 uint32_t id)
794{
795 pkt->ipv6_fragment.id = id;
796}
797#else /* CONFIG_NET_IPV6_FRAGMENT */
798static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
799{
800 ARG_UNUSED(pkt);
801
802 return 0;
803}
804
805static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
806 uint16_t start)
807{
808 ARG_UNUSED(pkt);
809 ARG_UNUSED(start);
810}
811
812static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
813{
814 ARG_UNUSED(pkt);
815
816 return 0;
817}
818
819static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
820{
821 ARG_UNUSED(pkt);
822
823 return 0;
824}
825
826static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
828{
829 ARG_UNUSED(pkt);
830 ARG_UNUSED(flags);
831}
832
833static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
834{
835 ARG_UNUSED(pkt);
836
837 return 0;
838}
839
840static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
841 uint32_t id)
842{
843 ARG_UNUSED(pkt);
844 ARG_UNUSED(id);
845}
846#endif /* CONFIG_NET_IPV6_FRAGMENT */
847
848static inline uint8_t net_pkt_priority(struct net_pkt *pkt)
849{
850 return pkt->priority;
851}
852
853static inline void net_pkt_set_priority(struct net_pkt *pkt,
854 uint8_t priority)
855{
856 pkt->priority = priority;
857}
858
859#if defined(CONFIG_NET_VLAN)
860static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
861{
862 return net_eth_vlan_get_vid(pkt->vlan_tci);
863}
864
865static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
866{
867 pkt->vlan_tci = net_eth_vlan_set_vid(pkt->vlan_tci, tag);
868}
869
870static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
871{
872 return net_eth_vlan_get_pcp(pkt->vlan_tci);
873}
874
875static inline void net_pkt_set_vlan_priority(struct net_pkt *pkt,
876 uint8_t priority)
877{
878 pkt->vlan_tci = net_eth_vlan_set_pcp(pkt->vlan_tci, priority);
879}
880
881static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
882{
883 return net_eth_vlan_get_dei(pkt->vlan_tci);
884}
885
886static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
887{
888 pkt->vlan_tci = net_eth_vlan_set_dei(pkt->vlan_tci, dei);
889}
890
891static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
892{
893 pkt->vlan_tci = tci;
894}
895
896static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
897{
898 return pkt->vlan_tci;
899}
900#else
901static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
902{
903 return NET_VLAN_TAG_UNSPEC;
904}
905
906static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
907{
908 ARG_UNUSED(pkt);
909 ARG_UNUSED(tag);
910}
911
912static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
913{
914 ARG_UNUSED(pkt);
915 return 0;
916}
917
918static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
919{
920 return false;
921}
922
923static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
924{
925 ARG_UNUSED(pkt);
926 ARG_UNUSED(dei);
927}
928
929static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
930{
931 return NET_VLAN_TAG_UNSPEC; /* assumes priority is 0 */
932}
933
934static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
935{
936 ARG_UNUSED(pkt);
937 ARG_UNUSED(tci);
938}
939#endif
940
941#if defined(CONFIG_NET_PKT_TIMESTAMP)
942static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
943{
944 return &pkt->timestamp;
945}
946
947static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
948 struct net_ptp_time *timestamp)
949{
950 pkt->timestamp.second = timestamp->second;
951 pkt->timestamp.nanosecond = timestamp->nanosecond;
952}
953#else
954static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
955{
956 ARG_UNUSED(pkt);
957
958 return NULL;
959}
960
961static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
962 struct net_ptp_time *timestamp)
963{
964 ARG_UNUSED(pkt);
965 ARG_UNUSED(timestamp);
966}
967#endif /* CONFIG_NET_PKT_TIMESTAMP */
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
996#if defined(CONFIG_NET_PKT_TXTIME)
997static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
998{
999 return pkt->txtime;
1000}
1001
1002static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
1003{
1004 pkt->txtime = txtime;
1005}
1006#else
1007static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
1008{
1009 ARG_UNUSED(pkt);
1010
1011 return 0;
1012}
1013
1014static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
1015{
1016 ARG_UNUSED(pkt);
1017 ARG_UNUSED(txtime);
1018}
1019#endif /* CONFIG_NET_PKT_TXTIME */
1020
1021#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
1022 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
1023static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
1024{
1025 return pkt->detail.stat;
1026}
1027
1028static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
1029{
1030 return pkt->detail.count;
1031}
1032
1033static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
1034{
1035 memset(&pkt->detail, 0, sizeof(pkt->detail));
1036}
1037
1038static ALWAYS_INLINE void net_pkt_set_stats_tick(struct net_pkt *pkt,
1039 uint32_t tick)
1040{
1041 if (pkt->detail.count >= NET_PKT_DETAIL_STATS_COUNT) {
1042 NET_ERR("Detail stats count overflow (%d >= %d)",
1043 pkt->detail.count, NET_PKT_DETAIL_STATS_COUNT);
1044 return;
1045 }
1046
1047 pkt->detail.stat[pkt->detail.count++] = tick;
1048}
1049
1050#define net_pkt_set_tx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
1051#define net_pkt_set_rx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
1052#else
1053static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
1054{
1055 ARG_UNUSED(pkt);
1056
1057 return NULL;
1058}
1059
1060static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
1061{
1062 ARG_UNUSED(pkt);
1063
1064 return 0;
1065}
1066
1067static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
1068{
1069 ARG_UNUSED(pkt);
1070}
1071
1072static inline void net_pkt_set_stats_tick(struct net_pkt *pkt, uint32_t tick)
1073{
1074 ARG_UNUSED(pkt);
1075 ARG_UNUSED(tick);
1076}
1077
1078#define net_pkt_set_tx_stats_tick(pkt, tick)
1079#define net_pkt_set_rx_stats_tick(pkt, tick)
1080#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
1081 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
1082
1083static inline size_t net_pkt_get_len(struct net_pkt *pkt)
1084{
1085 return net_buf_frags_len(pkt->frags);
1086}
1087
1088static inline uint8_t *net_pkt_data(struct net_pkt *pkt)
1089{
1090 return pkt->frags->data;
1091}
1092
1093static inline uint8_t *net_pkt_ip_data(struct net_pkt *pkt)
1094{
1095 return pkt->frags->data;
1096}
1097
1098static inline bool net_pkt_is_empty(struct net_pkt *pkt)
1099{
1100 return !pkt->buffer || !net_pkt_data(pkt) || pkt->buffer->len == 0;
1101}
1102
1103static inline struct net_linkaddr *net_pkt_lladdr_src(struct net_pkt *pkt)
1104{
1105 return &pkt->lladdr_src;
1106}
1107
1108static inline struct net_linkaddr *net_pkt_lladdr_dst(struct net_pkt *pkt)
1109{
1110 return &pkt->lladdr_dst;
1111}
1112
1113static inline void net_pkt_lladdr_swap(struct net_pkt *pkt)
1114{
1115 uint8_t *addr = net_pkt_lladdr_src(pkt)->addr;
1116
1117 net_pkt_lladdr_src(pkt)->addr = net_pkt_lladdr_dst(pkt)->addr;
1118 net_pkt_lladdr_dst(pkt)->addr = addr;
1119}
1120
1121static inline void net_pkt_lladdr_clear(struct net_pkt *pkt)
1122{
1123 net_pkt_lladdr_src(pkt)->addr = NULL;
1124 net_pkt_lladdr_src(pkt)->len = 0U;
1125}
1126
1127static inline uint16_t net_pkt_ll_proto_type(struct net_pkt *pkt)
1128{
1129 return pkt->ll_proto_type;
1130}
1131
1132static inline void net_pkt_set_ll_proto_type(struct net_pkt *pkt, uint16_t type)
1133{
1134 pkt->ll_proto_type = type;
1135}
1136
1137#if defined(CONFIG_NET_IPV4_AUTO)
1138static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1139{
1140 return pkt->ipv4_auto_arp_msg;
1141}
1142
1143static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1144 bool is_auto_arp_msg)
1145{
1146 pkt->ipv4_auto_arp_msg = is_auto_arp_msg;
1147}
1148#else /* CONFIG_NET_IPV4_AUTO */
1149static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1150{
1151 ARG_UNUSED(pkt);
1152
1153 return false;
1154}
1155
1156static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1157 bool is_auto_arp_msg)
1158{
1159 ARG_UNUSED(pkt);
1160 ARG_UNUSED(is_auto_arp_msg);
1161}
1162#endif /* CONFIG_NET_IPV4_AUTO */
1163
1164#if defined(CONFIG_NET_LLDP)
1165static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1166{
1167 return pkt->lldp_pkt;
1168}
1169
1170static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1171{
1172 pkt->lldp_pkt = is_lldp;
1173}
1174#else
1175static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1176{
1177 ARG_UNUSED(pkt);
1178
1179 return false;
1180}
1181
1182static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1183{
1184 ARG_UNUSED(pkt);
1185 ARG_UNUSED(is_lldp);
1186}
1187#endif /* CONFIG_NET_LLDP */
1188
1189#if defined(CONFIG_NET_L2_PPP)
1190static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1191{
1192 return pkt->ppp_msg;
1193}
1194
1195static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1196 bool is_ppp_msg)
1197{
1198 pkt->ppp_msg = is_ppp_msg;
1199}
1200#else /* CONFIG_NET_L2_PPP */
1201static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1202{
1203 ARG_UNUSED(pkt);
1204
1205 return false;
1206}
1207
1208static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1209 bool is_ppp_msg)
1210{
1211 ARG_UNUSED(pkt);
1212 ARG_UNUSED(is_ppp_msg);
1213}
1214#endif /* CONFIG_NET_L2_PPP */
1215
1216#if defined(NET_PKT_HAS_CONTROL_BLOCK)
1217static inline void *net_pkt_cb(struct net_pkt *pkt)
1218{
1219 return &pkt->cb;
1220}
1221#else
1222static inline void *net_pkt_cb(struct net_pkt *pkt)
1223{
1224 ARG_UNUSED(pkt);
1225
1226 return NULL;
1227}
1228#endif
1229
1230#define NET_IPV6_HDR(pkt) ((struct net_ipv6_hdr *)net_pkt_ip_data(pkt))
1231#define NET_IPV4_HDR(pkt) ((struct net_ipv4_hdr *)net_pkt_ip_data(pkt))
1232
1233static inline void net_pkt_set_src_ipv6_addr(struct net_pkt *pkt)
1234{
1236 net_pkt_context(pkt)),
1237 (struct in6_addr *)NET_IPV6_HDR(pkt)->src);
1238}
1239
1240static inline void net_pkt_set_overwrite(struct net_pkt *pkt, bool overwrite)
1241{
1242 pkt->overwrite = overwrite;
1243}
1244
1245static inline bool net_pkt_is_being_overwritten(struct net_pkt *pkt)
1246{
1247 return pkt->overwrite;
1248}
1249
1250#ifdef CONFIG_NET_PKT_FILTER
1251
1252bool net_pkt_filter_send_ok(struct net_pkt *pkt);
1253bool net_pkt_filter_recv_ok(struct net_pkt *pkt);
1254
1255#else
1256
1257static inline bool net_pkt_filter_send_ok(struct net_pkt *pkt)
1258{
1259 ARG_UNUSED(pkt);
1260
1261 return true;
1262}
1263
1264static inline bool net_pkt_filter_recv_ok(struct net_pkt *pkt)
1265{
1266 ARG_UNUSED(pkt);
1267
1268 return true;
1269}
1270
1271#endif /* CONFIG_NET_PKT_FILTER */
1272
1273/* @endcond */
1274
1288#define NET_PKT_SLAB_DEFINE(name, count) \
1289 K_MEM_SLAB_DEFINE(name, sizeof(struct net_pkt), count, 4)
1290
1291/* Backward compatibility macro */
1292#define NET_PKT_TX_SLAB_DEFINE(name, count) NET_PKT_SLAB_DEFINE(name, count)
1293
1307#define NET_PKT_DATA_POOL_DEFINE(name, count) \
1308 NET_BUF_POOL_DEFINE(name, count, CONFIG_NET_BUF_DATA_SIZE, \
1309 0, NULL)
1310
1313#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC) || \
1314 (CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG)
1315#define NET_PKT_DEBUG_ENABLED
1316#endif
1317
1318#if defined(NET_PKT_DEBUG_ENABLED)
1319
1320/* Debug versions of the net_pkt functions that are used when tracking
1321 * buffer usage.
1322 */
1323
1324struct net_buf *net_pkt_get_reserve_data_debug(struct net_buf_pool *pool,
1325 size_t min_len,
1327 const char *caller,
1328 int line);
1329
1330#define net_pkt_get_reserve_data(pool, min_len, timeout) \
1331 net_pkt_get_reserve_data_debug(pool, min_len, timeout, __func__, __LINE__)
1332
1333struct net_buf *net_pkt_get_reserve_rx_data_debug(size_t min_len,
1335 const char *caller,
1336 int line);
1337#define net_pkt_get_reserve_rx_data(min_len, timeout) \
1338 net_pkt_get_reserve_rx_data_debug(min_len, timeout, __func__, __LINE__)
1339
1340struct net_buf *net_pkt_get_reserve_tx_data_debug(size_t min_len,
1342 const char *caller,
1343 int line);
1344#define net_pkt_get_reserve_tx_data(min_len, timeout) \
1345 net_pkt_get_reserve_tx_data_debug(min_len, timeout, __func__, __LINE__)
1346
1347struct net_buf *net_pkt_get_frag_debug(struct net_pkt *pkt, size_t min_len,
1349 const char *caller, int line);
1350#define net_pkt_get_frag(pkt, min_len, timeout) \
1351 net_pkt_get_frag_debug(pkt, min_len, timeout, __func__, __LINE__)
1352
1353void net_pkt_unref_debug(struct net_pkt *pkt, const char *caller, int line);
1354#define net_pkt_unref(pkt) net_pkt_unref_debug(pkt, __func__, __LINE__)
1355
1356struct net_pkt *net_pkt_ref_debug(struct net_pkt *pkt, const char *caller,
1357 int line);
1358#define net_pkt_ref(pkt) net_pkt_ref_debug(pkt, __func__, __LINE__)
1359
1360struct net_buf *net_pkt_frag_ref_debug(struct net_buf *frag,
1361 const char *caller, int line);
1362#define net_pkt_frag_ref(frag) net_pkt_frag_ref_debug(frag, __func__, __LINE__)
1363
1364void net_pkt_frag_unref_debug(struct net_buf *frag,
1365 const char *caller, int line);
1366#define net_pkt_frag_unref(frag) \
1367 net_pkt_frag_unref_debug(frag, __func__, __LINE__)
1368
1369struct net_buf *net_pkt_frag_del_debug(struct net_pkt *pkt,
1370 struct net_buf *parent,
1371 struct net_buf *frag,
1372 const char *caller, int line);
1373#define net_pkt_frag_del(pkt, parent, frag) \
1374 net_pkt_frag_del_debug(pkt, parent, frag, __func__, __LINE__)
1375
1376void net_pkt_frag_add_debug(struct net_pkt *pkt, struct net_buf *frag,
1377 const char *caller, int line);
1378#define net_pkt_frag_add(pkt, frag) \
1379 net_pkt_frag_add_debug(pkt, frag, __func__, __LINE__)
1380
1381void net_pkt_frag_insert_debug(struct net_pkt *pkt, struct net_buf *frag,
1382 const char *caller, int line);
1383#define net_pkt_frag_insert(pkt, frag) \
1384 net_pkt_frag_insert_debug(pkt, frag, __func__, __LINE__)
1385#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC ||
1386 * CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG
1387 */
1397#if defined(NET_PKT_DEBUG_ENABLED)
1398void net_pkt_print_frags(struct net_pkt *pkt);
1399#else
1400#define net_pkt_print_frags(pkt)
1401#endif
1402
1417#if !defined(NET_PKT_DEBUG_ENABLED)
1419#endif
1420
1435#if !defined(NET_PKT_DEBUG_ENABLED)
1437#endif
1438
1451#if !defined(NET_PKT_DEBUG_ENABLED)
1452struct net_buf *net_pkt_get_frag(struct net_pkt *pkt, size_t min_len,
1454#endif
1455
1465#if !defined(NET_PKT_DEBUG_ENABLED)
1466void net_pkt_unref(struct net_pkt *pkt);
1467#endif
1468
1478#if !defined(NET_PKT_DEBUG_ENABLED)
1479struct net_pkt *net_pkt_ref(struct net_pkt *pkt);
1480#endif
1481
1491#if !defined(NET_PKT_DEBUG_ENABLED)
1492struct net_buf *net_pkt_frag_ref(struct net_buf *frag);
1493#endif
1494
1500#if !defined(NET_PKT_DEBUG_ENABLED)
1501void net_pkt_frag_unref(struct net_buf *frag);
1502#endif
1503
1514#if !defined(NET_PKT_DEBUG_ENABLED)
1516 struct net_buf *parent,
1517 struct net_buf *frag);
1518#endif
1519
1526#if !defined(NET_PKT_DEBUG_ENABLED)
1527void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag);
1528#endif
1529
1536#if !defined(NET_PKT_DEBUG_ENABLED)
1537void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag);
1538#endif
1539
1546void net_pkt_compact(struct net_pkt *pkt);
1547
1556void net_pkt_get_info(struct k_mem_slab **rx,
1557 struct k_mem_slab **tx,
1558 struct net_buf_pool **rx_data,
1559 struct net_buf_pool **tx_data);
1560
1563#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
1567void net_pkt_print(void);
1568
1569typedef void (*net_pkt_allocs_cb_t)(struct net_pkt *pkt,
1570 struct net_buf *buf,
1571 const char *func_alloc,
1572 int line_alloc,
1573 const char *func_free,
1574 int line_free,
1575 bool in_use,
1576 void *user_data);
1577
1578void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data);
1579
1580const char *net_pkt_slab2str(struct k_mem_slab *slab);
1581const char *net_pkt_pool2str(struct net_buf_pool *pool);
1582
1583#else
1584#define net_pkt_print(...)
1585#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
1586
1587/* New allocator, and API are defined below.
1588 * This will be simpler when time will come to get rid of former API above.
1589 */
1590#if defined(NET_PKT_DEBUG_ENABLED)
1591
1592struct net_pkt *net_pkt_alloc_debug(k_timeout_t timeout,
1593 const char *caller, int line);
1594#define net_pkt_alloc(_timeout) \
1595 net_pkt_alloc_debug(_timeout, __func__, __LINE__)
1596
1597struct net_pkt *net_pkt_alloc_from_slab_debug(struct k_mem_slab *slab,
1599 const char *caller, int line);
1600#define net_pkt_alloc_from_slab(_slab, _timeout) \
1601 net_pkt_alloc_from_slab_debug(_slab, _timeout, __func__, __LINE__)
1602
1603struct net_pkt *net_pkt_rx_alloc_debug(k_timeout_t timeout,
1604 const char *caller, int line);
1605#define net_pkt_rx_alloc(_timeout) \
1606 net_pkt_rx_alloc_debug(_timeout, __func__, __LINE__)
1607
1608struct net_pkt *net_pkt_alloc_on_iface_debug(struct net_if *iface,
1610 const char *caller,
1611 int line);
1612#define net_pkt_alloc_on_iface(_iface, _timeout) \
1613 net_pkt_alloc_on_iface_debug(_iface, _timeout, __func__, __LINE__)
1614
1615struct net_pkt *net_pkt_rx_alloc_on_iface_debug(struct net_if *iface,
1617 const char *caller,
1618 int line);
1619#define net_pkt_rx_alloc_on_iface(_iface, _timeout) \
1620 net_pkt_rx_alloc_on_iface_debug(_iface, _timeout, \
1621 __func__, __LINE__)
1622
1623int net_pkt_alloc_buffer_debug(struct net_pkt *pkt,
1624 size_t size,
1625 enum net_ip_protocol proto,
1627 const char *caller, int line);
1628#define net_pkt_alloc_buffer(_pkt, _size, _proto, _timeout) \
1629 net_pkt_alloc_buffer_debug(_pkt, _size, _proto, _timeout, \
1630 __func__, __LINE__)
1631
1632struct net_pkt *net_pkt_alloc_with_buffer_debug(struct net_if *iface,
1633 size_t size,
1634 sa_family_t family,
1635 enum net_ip_protocol proto,
1637 const char *caller,
1638 int line);
1639#define net_pkt_alloc_with_buffer(_iface, _size, _family, \
1640 _proto, _timeout) \
1641 net_pkt_alloc_with_buffer_debug(_iface, _size, _family, \
1642 _proto, _timeout, \
1643 __func__, __LINE__)
1644
1645struct net_pkt *net_pkt_rx_alloc_with_buffer_debug(struct net_if *iface,
1646 size_t size,
1647 sa_family_t family,
1648 enum net_ip_protocol proto,
1650 const char *caller,
1651 int line);
1652#define net_pkt_rx_alloc_with_buffer(_iface, _size, _family, \
1653 _proto, _timeout) \
1654 net_pkt_rx_alloc_with_buffer_debug(_iface, _size, _family, \
1655 _proto, _timeout, \
1656 __func__, __LINE__)
1657#endif /* NET_PKT_DEBUG_ENABLED */
1670#if !defined(NET_PKT_DEBUG_ENABLED)
1672#endif
1673
1688#if !defined(NET_PKT_DEBUG_ENABLED)
1689struct net_pkt *net_pkt_alloc_from_slab(struct k_mem_slab *slab,
1691#endif
1692
1703#if !defined(NET_PKT_DEBUG_ENABLED)
1705#endif
1706
1715#if !defined(NET_PKT_DEBUG_ENABLED)
1718
1719/* Same as above but specifically for RX packet */
1722#endif
1723
1739#if !defined(NET_PKT_DEBUG_ENABLED)
1741 size_t size,
1742 enum net_ip_protocol proto,
1744#endif
1745
1757#if !defined(NET_PKT_DEBUG_ENABLED)
1759 size_t size,
1760 sa_family_t family,
1761 enum net_ip_protocol proto,
1763
1764/* Same as above but specifically for RX packet */
1766 size_t size,
1767 sa_family_t family,
1768 enum net_ip_protocol proto,
1770#endif
1771
1778void net_pkt_append_buffer(struct net_pkt *pkt, struct net_buf *buffer);
1779
1791
1808 enum net_ip_protocol proto);
1809
1819
1834int net_pkt_remove_tail(struct net_pkt *pkt, size_t length);
1835
1844
1851static inline void net_pkt_cursor_backup(struct net_pkt *pkt,
1852 struct net_pkt_cursor *backup)
1853{
1854 backup->buf = pkt->cursor.buf;
1855 backup->pos = pkt->cursor.pos;
1856}
1857
1864static inline void net_pkt_cursor_restore(struct net_pkt *pkt,
1865 struct net_pkt_cursor *backup)
1866{
1867 pkt->cursor.buf = backup->buf;
1868 pkt->cursor.pos = backup->pos;
1869}
1870
1878static inline void *net_pkt_cursor_get_pos(struct net_pkt *pkt)
1879{
1880 return pkt->cursor.pos;
1881}
1882
1903int net_pkt_skip(struct net_pkt *pkt, size_t length);
1904
1919int net_pkt_memset(struct net_pkt *pkt, int byte, size_t length);
1920
1934int net_pkt_copy(struct net_pkt *pkt_dst,
1935 struct net_pkt *pkt_src,
1936 size_t length);
1937
1948
1959
1970
1984int net_pkt_read(struct net_pkt *pkt, void *data, size_t length);
1985
1986/* Read uint8_t data data a net_pkt */
1987static inline int net_pkt_read_u8(struct net_pkt *pkt, uint8_t *data)
1988{
1989 return net_pkt_read(pkt, data, 1);
1990}
1991
2005
2019
2033
2047int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length);
2048
2049/* Write uint8_t data into a net_pkt. */
2050static inline int net_pkt_write_u8(struct net_pkt *pkt, uint8_t data)
2051{
2052 return net_pkt_write(pkt, &data, sizeof(uint8_t));
2053}
2054
2055/* Write uint16_t big endian data into a net_pkt. */
2056static inline int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
2057{
2058 uint16_t data_be16 = htons(data);
2059
2060 return net_pkt_write(pkt, &data_be16, sizeof(uint16_t));
2061}
2062
2063/* Write uint32_t big endian data into a net_pkt. */
2064static inline int net_pkt_write_be32(struct net_pkt *pkt, uint32_t data)
2065{
2066 uint32_t data_be32 = htonl(data);
2067
2068 return net_pkt_write(pkt, &data_be32, sizeof(uint32_t));
2069}
2070
2071/* Write uint32_t little endian data into a net_pkt. */
2072static inline int net_pkt_write_le32(struct net_pkt *pkt, uint32_t data)
2073{
2074 uint32_t data_le32 = sys_cpu_to_le32(data);
2075
2076 return net_pkt_write(pkt, &data_le32, sizeof(uint32_t));
2077}
2078
2079/* Write uint16_t little endian data into a net_pkt. */
2080static inline int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
2081{
2082 uint16_t data_le16 = sys_cpu_to_le16(data);
2083
2084 return net_pkt_write(pkt, &data_le16, sizeof(uint16_t));
2085}
2086
2095
2108int net_pkt_update_length(struct net_pkt *pkt, size_t length);
2109
2122int net_pkt_pull(struct net_pkt *pkt, size_t length);
2123
2133
2145bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size);
2146
2156
2158#if !defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2159 void *data;
2160#endif
2161 const size_t size;
2162};
2163
2164#if defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2165#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2166 struct net_pkt_data_access _name = { \
2167 .size = sizeof(_type), \
2168 }
2169
2170#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2171 NET_PKT_DATA_ACCESS_DEFINE(_name, _type)
2172
2173#else
2174#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2175 _type _hdr_##_name; \
2176 struct net_pkt_data_access _name = { \
2177 .data = &_hdr_##_name, \
2178 .size = sizeof(_type), \
2179 }
2180
2181#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2182 struct net_pkt_data_access _name = { \
2183 .data = NULL, \
2184 .size = sizeof(_type), \
2185 }
2186
2187#endif /* CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS */
2188
2202void *net_pkt_get_data(struct net_pkt *pkt,
2203 struct net_pkt_data_access *access);
2204
2219 struct net_pkt_data_access *access);
2220
2225static inline int net_pkt_acknowledge_data(struct net_pkt *pkt,
2226 struct net_pkt_data_access *access)
2227{
2228 return net_pkt_skip(pkt, access->size);
2229}
2230
2235#ifdef __cplusplus
2236}
2237#endif
2238
2239#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:198
#define sys_cpu_to_le16(val)
Convert 16-bit integer from host endianness to little-endian.
Definition: byteorder.h:194
#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.
static ssize_t send(int sock, const void *buf, size_t len, int flags)
Definition: socket.h:738
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 size_t net_buf_frags_len(struct net_buf *buf)
Calculate amount of bytes stored in fragments.
Definition: buf.h:2453
static struct net_if * net_context_get_iface(struct net_context *context)
Get network interface for this context.
Definition: net_context.h:643
static struct net_linkaddr * net_if_get_link_addr(struct net_if *iface)
Get an network interface's link address.
Definition: net_if.h:807
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:1574
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:2064
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:1400
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. 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.
static int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:2056
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:2050
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. Normally you should use net_pkt_get_frag() instead.
static void * net_pkt_cursor_get_pos(struct net_pkt *pkt)
Returns current position of the cursor.
Definition: net_pkt.h:1878
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:1851
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)
Definition: net_pkt.h:2225
static int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:2080
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:1864
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. 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:2072
struct net_buf * net_pkt_get_reserve_rx_data(size_t min_len, k_timeout_t timeout)
Get RX DATA buffer from pool. Normally you should use net_pkt_get_frag() instead.
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:1987
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)
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:121
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
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.
flags
Definition: parser.h:96
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:970
Network buffer representation.
Definition: buf.h:905
uint8_t * data
Definition: buf.h:931
uint16_t len
Definition: buf.h:934
Definition: net_context.h:201
Network Interface structure.
Definition: net_if.h:510
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:49
uint8_t * pos
Definition: net_pkt.h:53
struct net_buf * buf
Definition: net_pkt.h:51
Definition: net_pkt.h:2157
const size_t size
Definition: net_pkt.h:2161
void * data
Definition: net_pkt.h:2159
Network packet.
Definition: net_pkt.h:62
struct net_buf * frags
Definition: net_pkt.h:74
struct net_context * context
Definition: net_pkt.h:82
struct net_pkt_cursor cursor
Definition: net_pkt.h:79
struct net_if * iface
Definition: net_pkt.h:85
intptr_t fifo
Definition: net_pkt.h:67
struct net_buf * buffer
Definition: net_pkt.h:75
struct k_mem_slab * slab
Definition: net_pkt.h:70
Precision Time Protocol Timestamp format.
Definition: ptp_time.h:39
uint32_t nanosecond
Definition: ptp_time.h:57
uint64_t second
Definition: ptp_time.h:53
Definition: stat.h:92
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static ZTEST_BMEM char buffer[8]
Definition: test_mbox_api.c:551
static const char * tag(void)
Definition: main.c:27
static const intptr_t user_data[5]
Definition: main.c:588