Zephyr Project API  3.4.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)
102 struct net_ptp_time timestamp;
103#endif
104
105#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
106 struct {
108 uint32_t create_time;
109
110#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
111 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
117 struct {
118 uint32_t stat[NET_PKT_DETAIL_STATS_COUNT];
119 int count;
120 } detail;
121#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
122 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
123 };
124#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
125
126#if defined(CONFIG_NET_PKT_TXTIME)
128 uint64_t txtime;
129#endif /* CONFIG_NET_PKT_TXTIME */
130
132 atomic_t atomic_ref;
133
134 /* Filled by layer 2 when network packet is received. */
135 struct net_linkaddr lladdr_src;
136 struct net_linkaddr lladdr_dst;
137 uint16_t ll_proto_type;
138
139#if defined(CONFIG_NET_IP)
140 uint8_t ip_hdr_len; /* pre-filled in order to avoid func call */
141#endif
142
143 uint8_t overwrite : 1; /* Is packet content being overwritten? */
144 uint8_t sent_or_eof : 1; /* For outgoing packet: is this sent or not
145 * For incoming packet of a socket: last
146 * packet before EOF
147 * Used only if defined(CONFIG_NET_TCP)
148 */
149 uint8_t pkt_queued : 1; /* For outgoing packet: is this packet
150 * queued to be sent but has not reached
151 * the driver yet.
152 * Used only if defined(CONFIG_NET_TCP)
153 */
154 uint8_t ptp_pkt : 1; /* For outgoing packet: is this packet
155 * a L2 PTP packet.
156 * Used only if defined (CONFIG_NET_L2_PTP)
157 */
158 uint8_t forwarding : 1; /* Are we forwarding this pkt
159 * Used only if defined(CONFIG_NET_ROUTE)
160 */
161 uint8_t family : 3; /* Address family, see net_ip.h */
162
163 /* bitfield byte alignment boundary */
164
165#if defined(CONFIG_NET_IPV4_AUTO)
166 uint8_t ipv4_auto_arp_msg : 1; /* Is this pkt IPv4 autoconf ARP
167 * message.
168 * Note: family needs to be
169 * AF_INET.
170 */
171#endif
172#if defined(CONFIG_NET_LLDP)
173 uint8_t lldp_pkt : 1; /* Is this pkt an LLDP message.
174 * Note: family needs to be
175 * AF_UNSPEC.
176 */
177#endif
178 uint8_t ppp_msg : 1; /* This is a PPP message */
179#if defined(CONFIG_NET_TCP)
180 uint8_t tcp_first_msg : 1; /* Is this the first time this pkt is
181 * sent, or is this a resend of a TCP
182 * segment.
183 */
184#endif
185 uint8_t captured : 1; /* Set to 1 if this packet is already being
186 * captured
187 */
188 uint8_t l2_bridged : 1; /* set to 1 if this packet comes from a bridge
189 * and already contains its L2 header to be
190 * preserved. Useful only if
191 * defined(CONFIG_NET_ETHERNET_BRIDGE).
192 */
193 uint8_t l2_processed : 1; /* Set to 1 if this packet has already been
194 * processed by the L2
195 */
196
197 /* bitfield byte alignment boundary */
198
199#if defined(CONFIG_NET_IP)
200 union {
201 /* IPv6 hop limit or IPv4 ttl for this network packet.
202 * The value is shared between IPv6 and IPv4.
203 */
204#if defined(CONFIG_NET_IPV6)
205 uint8_t ipv6_hop_limit;
206#endif
207#if defined(CONFIG_NET_IPV4)
208 uint8_t ipv4_ttl;
209#endif
210 };
211
212 union {
213#if defined(CONFIG_NET_IPV4)
214 uint8_t ipv4_opts_len; /* length of IPv4 header options */
215#endif
216#if defined(CONFIG_NET_IPV6)
217 uint16_t ipv6_ext_len; /* length of extension headers */
218#endif
219 };
220
221#if defined(CONFIG_NET_IPV4_FRAGMENT) || defined(CONFIG_NET_IPV6_FRAGMENT)
222 union {
223#if defined(CONFIG_NET_IPV4_FRAGMENT)
224 struct {
225 uint16_t flags; /* Fragment offset and M (More Fragment) flag */
226 uint16_t id; /* Fragment ID */
227 } ipv4_fragment;
228#endif /* CONFIG_NET_IPV4_FRAGMENT */
229#if defined(CONFIG_NET_IPV6_FRAGMENT)
230 struct {
231 uint16_t flags; /* Fragment offset and M (More Fragment) flag */
232 uint32_t id; /* Fragment id */
233 uint16_t hdr_start; /* Where starts the fragment header */
234 } ipv6_fragment;
235#endif /* CONFIG_NET_IPV6_FRAGMENT */
236 };
237#endif /* CONFIG_NET_IPV4_FRAGMENT || CONFIG_NET_IPV6_FRAGMENT */
238
239#if defined(CONFIG_NET_IPV6)
240 /* Where is the start of the last header before payload data
241 * in IPv6 packet. This is offset value from start of the IPv6
242 * packet. Note that this value should be updated by who ever
243 * adds IPv6 extension headers to the network packet.
244 */
245 uint16_t ipv6_prev_hdr_start;
246
247 uint8_t ipv6_ext_opt_len; /* IPv6 ND option length */
248 uint8_t ipv6_next_hdr; /* What is the very first next header */
249#endif /* CONFIG_NET_IPV6 */
250
251#if defined(CONFIG_NET_IP_DSCP_ECN)
253 uint8_t ip_dscp : 6;
254
256 uint8_t ip_ecn : 2;
257#endif /* CONFIG_NET_IP_DSCP_ECN */
258#endif /* CONFIG_NET_IP */
259
260#if defined(CONFIG_NET_VLAN)
261 /* VLAN TCI (Tag Control Information). This contains the Priority
262 * Code Point (PCP), Drop Eligible Indicator (DEI) and VLAN
263 * Identifier (VID, called more commonly VLAN tag). This value is
264 * kept in host byte order.
265 */
266 uint16_t vlan_tci;
267#endif /* CONFIG_NET_VLAN */
268
269#if defined(NET_PKT_HAS_CONTROL_BLOCK)
270 /* TODO: Evolve this into a union of orthogonal
271 * control block declarations if further L2
272 * stacks require L2-specific attributes.
273 */
274#if defined(CONFIG_IEEE802154)
275 /* The following structure requires a 4-byte alignment
276 * boundary to avoid padding.
277 */
278 struct net_pkt_cb_ieee802154 cb;
279#endif /* CONFIG_IEEE802154 */
280#endif /* NET_PKT_HAS_CONTROL_BLOCK */
281
285 uint8_t priority;
286
287 /* @endcond */
288};
289
292/* The interface real ll address */
293static inline struct net_linkaddr *net_pkt_lladdr_if(struct net_pkt *pkt)
294{
295 return net_if_get_link_addr(pkt->iface);
296}
297
298static inline struct net_context *net_pkt_context(struct net_pkt *pkt)
299{
300 return pkt->context;
301}
302
303static inline void net_pkt_set_context(struct net_pkt *pkt,
304 struct net_context *ctx)
305{
306 pkt->context = ctx;
307}
308
309static inline struct net_if *net_pkt_iface(struct net_pkt *pkt)
310{
311 return pkt->iface;
312}
313
314static inline void net_pkt_set_iface(struct net_pkt *pkt, struct net_if *iface)
315{
316 pkt->iface = iface;
317
318 /* If the network interface is set in pkt, then also set the type of
319 * the network address that is stored in pkt. This is done here so
320 * that the address type is properly set and is not forgotten.
321 */
322 if (iface) {
323 uint8_t type = net_if_get_link_addr(iface)->type;
324
325 pkt->lladdr_src.type = type;
326 pkt->lladdr_dst.type = type;
327 }
328}
329
330static inline struct net_if *net_pkt_orig_iface(struct net_pkt *pkt)
331{
332#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
333 return pkt->orig_iface;
334#else
335 return pkt->iface;
336#endif
337}
338
339static inline void net_pkt_set_orig_iface(struct net_pkt *pkt,
340 struct net_if *iface)
341{
342#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
343 pkt->orig_iface = iface;
344#endif
345}
346
347static inline uint8_t net_pkt_family(struct net_pkt *pkt)
348{
349 return pkt->family;
350}
351
352static inline void net_pkt_set_family(struct net_pkt *pkt, uint8_t family)
353{
354 pkt->family = family;
355}
356
357static inline bool net_pkt_is_ptp(struct net_pkt *pkt)
358{
359 return !!(pkt->ptp_pkt);
360}
361
362static inline void net_pkt_set_ptp(struct net_pkt *pkt, bool is_ptp)
363{
364 pkt->ptp_pkt = is_ptp;
365}
366
367static inline bool net_pkt_is_captured(struct net_pkt *pkt)
368{
369 return !!(pkt->captured);
370}
371
372static inline void net_pkt_set_captured(struct net_pkt *pkt, bool is_captured)
373{
374 pkt->captured = is_captured;
375}
376
377static inline bool net_pkt_is_l2_bridged(struct net_pkt *pkt)
378{
379 return IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) ? !!(pkt->l2_bridged) : 0;
380}
381
382static inline void net_pkt_set_l2_bridged(struct net_pkt *pkt, bool is_l2_bridged)
383{
384 if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE)) {
385 pkt->l2_bridged = is_l2_bridged;
386 }
387}
388
389static inline bool net_pkt_is_l2_processed(struct net_pkt *pkt)
390{
391 return !!(pkt->l2_processed);
392}
393
394static inline void net_pkt_set_l2_processed(struct net_pkt *pkt,
395 bool is_l2_processed)
396{
397 pkt->l2_processed = is_l2_processed;
398}
399
400static inline uint8_t net_pkt_ip_hdr_len(struct net_pkt *pkt)
401{
402#if defined(CONFIG_NET_IP)
403 return pkt->ip_hdr_len;
404#else
405 return 0;
406#endif
407}
408
409static inline void net_pkt_set_ip_hdr_len(struct net_pkt *pkt, uint8_t len)
410{
411#if defined(CONFIG_NET_IP)
412 pkt->ip_hdr_len = len;
413#endif
414}
415
416static inline uint8_t net_pkt_ip_dscp(struct net_pkt *pkt)
417{
418#if defined(CONFIG_NET_IP_DSCP_ECN)
419 return pkt->ip_dscp;
420#else
421 return 0;
422#endif
423}
424
425static inline void net_pkt_set_ip_dscp(struct net_pkt *pkt, uint8_t dscp)
426{
427#if defined(CONFIG_NET_IP_DSCP_ECN)
428 pkt->ip_dscp = dscp;
429#endif
430}
431
432static inline uint8_t net_pkt_ip_ecn(struct net_pkt *pkt)
433{
434#if defined(CONFIG_NET_IP_DSCP_ECN)
435 return pkt->ip_ecn;
436#else
437 return 0;
438#endif
439}
440
441static inline void net_pkt_set_ip_ecn(struct net_pkt *pkt, uint8_t ecn)
442{
443#if defined(CONFIG_NET_IP_DSCP_ECN)
444 pkt->ip_ecn = ecn;
445#endif
446}
447
448static inline uint8_t net_pkt_sent(struct net_pkt *pkt)
449{
450 return pkt->sent_or_eof;
451}
452
453static inline void net_pkt_set_sent(struct net_pkt *pkt, bool sent)
454{
455 pkt->sent_or_eof = sent;
456}
457
458static inline uint8_t net_pkt_queued(struct net_pkt *pkt)
459{
460 return pkt->pkt_queued;
461}
462
463static inline void net_pkt_set_queued(struct net_pkt *pkt, bool send)
464{
465 pkt->pkt_queued = send;
466}
467
468static inline uint8_t net_pkt_tcp_1st_msg(struct net_pkt *pkt)
469{
470#if defined(CONFIG_NET_TCP)
471 return pkt->tcp_first_msg;
472#else
473 return true;
474#endif
475}
476
477static inline void net_pkt_set_tcp_1st_msg(struct net_pkt *pkt, bool is_1st)
478{
479#if defined(CONFIG_NET_TCP)
480 pkt->tcp_first_msg = is_1st;
481#else
482 ARG_UNUSED(pkt);
483 ARG_UNUSED(is_1st);
484#endif
485}
486
487#if defined(CONFIG_NET_SOCKETS)
488static inline uint8_t net_pkt_eof(struct net_pkt *pkt)
489{
490 return pkt->sent_or_eof;
491}
492
493static inline void net_pkt_set_eof(struct net_pkt *pkt, bool eof)
494{
495 pkt->sent_or_eof = eof;
496}
497#endif
498
499#if defined(CONFIG_NET_ROUTE)
500static inline bool net_pkt_forwarding(struct net_pkt *pkt)
501{
502 return pkt->forwarding;
503}
504
505static inline void net_pkt_set_forwarding(struct net_pkt *pkt, bool forward)
506{
507 pkt->forwarding = forward;
508}
509#else
510static inline bool net_pkt_forwarding(struct net_pkt *pkt)
511{
512 return false;
513}
514#endif
515
516#if defined(CONFIG_NET_IPV4)
517static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
518{
519 return pkt->ipv4_ttl;
520}
521
522static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
523 uint8_t ttl)
524{
525 pkt->ipv4_ttl = ttl;
526}
527
528static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
529{
530 return pkt->ipv4_opts_len;
531}
532
533static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
534 uint8_t opts_len)
535{
536 pkt->ipv4_opts_len = opts_len;
537}
538#else
539static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
540{
541 ARG_UNUSED(pkt);
542
543 return 0;
544}
545
546static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
547 uint8_t ttl)
548{
549 ARG_UNUSED(pkt);
550 ARG_UNUSED(ttl);
551}
552
553static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
554{
555 ARG_UNUSED(pkt);
556 return 0;
557}
558
559static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
560 uint8_t opts_len)
561{
562 ARG_UNUSED(pkt);
563 ARG_UNUSED(opts_len);
564}
565#endif
566
567#if defined(CONFIG_NET_IPV6)
568static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
569{
570 return pkt->ipv6_ext_opt_len;
571}
572
573static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
574 uint8_t len)
575{
576 pkt->ipv6_ext_opt_len = len;
577}
578
579static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
580{
581 return pkt->ipv6_next_hdr;
582}
583
584static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
585 uint8_t next_hdr)
586{
587 pkt->ipv6_next_hdr = next_hdr;
588}
589
590static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
591{
592 return pkt->ipv6_ext_len;
593}
594
595static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
596{
597 pkt->ipv6_ext_len = len;
598}
599
600static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
601{
602 return pkt->ipv6_prev_hdr_start;
603}
604
605static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
606 uint16_t offset)
607{
608 pkt->ipv6_prev_hdr_start = offset;
609}
610
611static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
612{
613 return pkt->ipv6_hop_limit;
614}
615
616static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
617 uint8_t hop_limit)
618{
619 pkt->ipv6_hop_limit = hop_limit;
620}
621#else /* CONFIG_NET_IPV6 */
622static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
623{
624 ARG_UNUSED(pkt);
625
626 return 0;
627}
628
629static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
630 uint8_t len)
631{
632 ARG_UNUSED(pkt);
633 ARG_UNUSED(len);
634}
635
636static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
637{
638 ARG_UNUSED(pkt);
639
640 return 0;
641}
642
643static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
644 uint8_t next_hdr)
645{
646 ARG_UNUSED(pkt);
647 ARG_UNUSED(next_hdr);
648}
649
650static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
651{
652 ARG_UNUSED(pkt);
653
654 return 0;
655}
656
657static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
658{
659 ARG_UNUSED(pkt);
660 ARG_UNUSED(len);
661}
662
663static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
664{
665 ARG_UNUSED(pkt);
666
667 return 0;
668}
669
670static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
671 uint16_t offset)
672{
673 ARG_UNUSED(pkt);
674 ARG_UNUSED(offset);
675}
676
677static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
678{
679 ARG_UNUSED(pkt);
680
681 return 0;
682}
683
684static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
685 uint8_t hop_limit)
686{
687 ARG_UNUSED(pkt);
688 ARG_UNUSED(hop_limit);
689}
690#endif /* CONFIG_NET_IPV6 */
691
692static inline uint16_t net_pkt_ip_opts_len(struct net_pkt *pkt)
693{
694#if defined(CONFIG_NET_IPV6)
695 return pkt->ipv6_ext_len;
696#elif defined(CONFIG_NET_IPV4)
697 return pkt->ipv4_opts_len;
698#else
699 ARG_UNUSED(pkt);
700
701 return 0;
702#endif
703}
704
705#if defined(CONFIG_NET_IPV4_FRAGMENT)
706static inline uint16_t net_pkt_ipv4_fragment_offset(struct net_pkt *pkt)
707{
708 return (pkt->ipv4_fragment.flags & NET_IPV4_FRAGH_OFFSET_MASK) * 8;
709}
710
711static inline bool net_pkt_ipv4_fragment_more(struct net_pkt *pkt)
712{
713 return (pkt->ipv4_fragment.flags & NET_IPV4_MORE_FRAG_MASK) != 0;
714}
715
716static inline void net_pkt_set_ipv4_fragment_flags(struct net_pkt *pkt, uint16_t flags)
717{
718 pkt->ipv4_fragment.flags = flags;
719}
720
721static inline uint32_t net_pkt_ipv4_fragment_id(struct net_pkt *pkt)
722{
723 return pkt->ipv4_fragment.id;
724}
725
726static inline void net_pkt_set_ipv4_fragment_id(struct net_pkt *pkt, uint32_t id)
727{
728 pkt->ipv4_fragment.id = id;
729}
730#else /* CONFIG_NET_IPV4_FRAGMENT */
731static inline uint16_t net_pkt_ipv4_fragment_offset(struct net_pkt *pkt)
732{
733 ARG_UNUSED(pkt);
734
735 return 0;
736}
737
738static inline bool net_pkt_ipv4_fragment_more(struct net_pkt *pkt)
739{
740 ARG_UNUSED(pkt);
741
742 return 0;
743}
744
745static inline void net_pkt_set_ipv4_fragment_flags(struct net_pkt *pkt, uint16_t flags)
746{
747 ARG_UNUSED(pkt);
748 ARG_UNUSED(flags);
749}
750
751static inline uint32_t net_pkt_ipv4_fragment_id(struct net_pkt *pkt)
752{
753 ARG_UNUSED(pkt);
754
755 return 0;
756}
757
758static inline void net_pkt_set_ipv4_fragment_id(struct net_pkt *pkt, uint32_t id)
759{
760 ARG_UNUSED(pkt);
761 ARG_UNUSED(id);
762}
763#endif /* CONFIG_NET_IPV4_FRAGMENT */
764
765#if defined(CONFIG_NET_IPV6_FRAGMENT)
766static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
767{
768 return pkt->ipv6_fragment.hdr_start;
769}
770
771static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
772 uint16_t start)
773{
774 pkt->ipv6_fragment.hdr_start = start;
775}
776
777static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
778{
779 return pkt->ipv6_fragment.flags & NET_IPV6_FRAGH_OFFSET_MASK;
780}
781static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
782{
783 return (pkt->ipv6_fragment.flags & 0x01) != 0;
784}
785
786static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
788{
789 pkt->ipv6_fragment.flags = flags;
790}
791
792static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
793{
794 return pkt->ipv6_fragment.id;
795}
796
797static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
798 uint32_t id)
799{
800 pkt->ipv6_fragment.id = id;
801}
802#else /* CONFIG_NET_IPV6_FRAGMENT */
803static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
804{
805 ARG_UNUSED(pkt);
806
807 return 0;
808}
809
810static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
811 uint16_t start)
812{
813 ARG_UNUSED(pkt);
814 ARG_UNUSED(start);
815}
816
817static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
818{
819 ARG_UNUSED(pkt);
820
821 return 0;
822}
823
824static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
825{
826 ARG_UNUSED(pkt);
827
828 return 0;
829}
830
831static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
833{
834 ARG_UNUSED(pkt);
835 ARG_UNUSED(flags);
836}
837
838static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
839{
840 ARG_UNUSED(pkt);
841
842 return 0;
843}
844
845static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
846 uint32_t id)
847{
848 ARG_UNUSED(pkt);
849 ARG_UNUSED(id);
850}
851#endif /* CONFIG_NET_IPV6_FRAGMENT */
852
853static inline uint8_t net_pkt_priority(struct net_pkt *pkt)
854{
855 return pkt->priority;
856}
857
858static inline void net_pkt_set_priority(struct net_pkt *pkt,
859 uint8_t priority)
860{
861 pkt->priority = priority;
862}
863
864#if defined(CONFIG_NET_VLAN)
865static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
866{
867 return net_eth_vlan_get_vid(pkt->vlan_tci);
868}
869
870static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
871{
872 pkt->vlan_tci = net_eth_vlan_set_vid(pkt->vlan_tci, tag);
873}
874
875static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
876{
877 return net_eth_vlan_get_pcp(pkt->vlan_tci);
878}
879
880static inline void net_pkt_set_vlan_priority(struct net_pkt *pkt,
881 uint8_t priority)
882{
883 pkt->vlan_tci = net_eth_vlan_set_pcp(pkt->vlan_tci, priority);
884}
885
886static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
887{
888 return net_eth_vlan_get_dei(pkt->vlan_tci);
889}
890
891static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
892{
893 pkt->vlan_tci = net_eth_vlan_set_dei(pkt->vlan_tci, dei);
894}
895
896static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
897{
898 pkt->vlan_tci = tci;
899}
900
901static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
902{
903 return pkt->vlan_tci;
904}
905#else
906static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
907{
908 return NET_VLAN_TAG_UNSPEC;
909}
910
911static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
912{
913 ARG_UNUSED(pkt);
914 ARG_UNUSED(tag);
915}
916
917static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
918{
919 ARG_UNUSED(pkt);
920 return 0;
921}
922
923static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
924{
925 return false;
926}
927
928static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
929{
930 ARG_UNUSED(pkt);
931 ARG_UNUSED(dei);
932}
933
934static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
935{
936 return NET_VLAN_TAG_UNSPEC; /* assumes priority is 0 */
937}
938
939static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
940{
941 ARG_UNUSED(pkt);
942 ARG_UNUSED(tci);
943}
944#endif
945
946#if defined(CONFIG_NET_PKT_TIMESTAMP)
947static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
948{
949 return &pkt->timestamp;
950}
951
952static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
953 struct net_ptp_time *timestamp)
954{
955 pkt->timestamp.second = timestamp->second;
956 pkt->timestamp.nanosecond = timestamp->nanosecond;
957}
958#else
959static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
960{
961 ARG_UNUSED(pkt);
962
963 return NULL;
964}
965
966static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
967 struct net_ptp_time *timestamp)
968{
969 ARG_UNUSED(pkt);
970 ARG_UNUSED(timestamp);
971}
972#endif /* CONFIG_NET_PKT_TIMESTAMP */
973
974#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
975static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
976{
977 return pkt->create_time;
978}
979
980static inline void net_pkt_set_create_time(struct net_pkt *pkt,
981 uint32_t create_time)
982{
983 pkt->create_time = create_time;
984}
985#else
986static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
987{
988 ARG_UNUSED(pkt);
989
990 return 0U;
991}
992
993static inline void net_pkt_set_create_time(struct net_pkt *pkt,
994 uint32_t create_time)
995{
996 ARG_UNUSED(pkt);
997 ARG_UNUSED(create_time);
998}
999#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
1000
1001#if defined(CONFIG_NET_PKT_TXTIME)
1002static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
1003{
1004 return pkt->txtime;
1005}
1006
1007static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
1008{
1009 pkt->txtime = txtime;
1010}
1011#else
1012static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
1013{
1014 ARG_UNUSED(pkt);
1015
1016 return 0;
1017}
1018
1019static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
1020{
1021 ARG_UNUSED(pkt);
1022 ARG_UNUSED(txtime);
1023}
1024#endif /* CONFIG_NET_PKT_TXTIME */
1025
1026#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
1027 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
1028static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
1029{
1030 return pkt->detail.stat;
1031}
1032
1033static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
1034{
1035 return pkt->detail.count;
1036}
1037
1038static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
1039{
1040 memset(&pkt->detail, 0, sizeof(pkt->detail));
1041}
1042
1043static ALWAYS_INLINE void net_pkt_set_stats_tick(struct net_pkt *pkt,
1044 uint32_t tick)
1045{
1046 if (pkt->detail.count >= NET_PKT_DETAIL_STATS_COUNT) {
1047 NET_ERR("Detail stats count overflow (%d >= %d)",
1048 pkt->detail.count, NET_PKT_DETAIL_STATS_COUNT);
1049 return;
1050 }
1051
1052 pkt->detail.stat[pkt->detail.count++] = tick;
1053}
1054
1055#define net_pkt_set_tx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
1056#define net_pkt_set_rx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
1057#else
1058static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
1059{
1060 ARG_UNUSED(pkt);
1061
1062 return NULL;
1063}
1064
1065static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
1066{
1067 ARG_UNUSED(pkt);
1068
1069 return 0;
1070}
1071
1072static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
1073{
1074 ARG_UNUSED(pkt);
1075}
1076
1077static inline void net_pkt_set_stats_tick(struct net_pkt *pkt, uint32_t tick)
1078{
1079 ARG_UNUSED(pkt);
1080 ARG_UNUSED(tick);
1081}
1082
1083#define net_pkt_set_tx_stats_tick(pkt, tick)
1084#define net_pkt_set_rx_stats_tick(pkt, tick)
1085#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
1086 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
1087
1088static inline size_t net_pkt_get_len(struct net_pkt *pkt)
1089{
1090 return net_buf_frags_len(pkt->frags);
1091}
1092
1093static inline uint8_t *net_pkt_data(struct net_pkt *pkt)
1094{
1095 return pkt->frags->data;
1096}
1097
1098static inline uint8_t *net_pkt_ip_data(struct net_pkt *pkt)
1099{
1100 return pkt->frags->data;
1101}
1102
1103static inline bool net_pkt_is_empty(struct net_pkt *pkt)
1104{
1105 return !pkt->buffer || !net_pkt_data(pkt) || pkt->buffer->len == 0;
1106}
1107
1108static inline struct net_linkaddr *net_pkt_lladdr_src(struct net_pkt *pkt)
1109{
1110 return &pkt->lladdr_src;
1111}
1112
1113static inline struct net_linkaddr *net_pkt_lladdr_dst(struct net_pkt *pkt)
1114{
1115 return &pkt->lladdr_dst;
1116}
1117
1118static inline void net_pkt_lladdr_swap(struct net_pkt *pkt)
1119{
1120 uint8_t *addr = net_pkt_lladdr_src(pkt)->addr;
1121
1122 net_pkt_lladdr_src(pkt)->addr = net_pkt_lladdr_dst(pkt)->addr;
1123 net_pkt_lladdr_dst(pkt)->addr = addr;
1124}
1125
1126static inline void net_pkt_lladdr_clear(struct net_pkt *pkt)
1127{
1128 net_pkt_lladdr_src(pkt)->addr = NULL;
1129 net_pkt_lladdr_src(pkt)->len = 0U;
1130}
1131
1132static inline uint16_t net_pkt_ll_proto_type(struct net_pkt *pkt)
1133{
1134 return pkt->ll_proto_type;
1135}
1136
1137static inline void net_pkt_set_ll_proto_type(struct net_pkt *pkt, uint16_t type)
1138{
1139 pkt->ll_proto_type = type;
1140}
1141
1142#if defined(CONFIG_NET_IPV4_AUTO)
1143static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1144{
1145 return pkt->ipv4_auto_arp_msg;
1146}
1147
1148static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1149 bool is_auto_arp_msg)
1150{
1151 pkt->ipv4_auto_arp_msg = is_auto_arp_msg;
1152}
1153#else /* CONFIG_NET_IPV4_AUTO */
1154static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1155{
1156 ARG_UNUSED(pkt);
1157
1158 return false;
1159}
1160
1161static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1162 bool is_auto_arp_msg)
1163{
1164 ARG_UNUSED(pkt);
1165 ARG_UNUSED(is_auto_arp_msg);
1166}
1167#endif /* CONFIG_NET_IPV4_AUTO */
1168
1169#if defined(CONFIG_NET_LLDP)
1170static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1171{
1172 return pkt->lldp_pkt;
1173}
1174
1175static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1176{
1177 pkt->lldp_pkt = is_lldp;
1178}
1179#else
1180static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1181{
1182 ARG_UNUSED(pkt);
1183
1184 return false;
1185}
1186
1187static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1188{
1189 ARG_UNUSED(pkt);
1190 ARG_UNUSED(is_lldp);
1191}
1192#endif /* CONFIG_NET_LLDP */
1193
1194#if defined(CONFIG_NET_L2_PPP)
1195static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1196{
1197 return pkt->ppp_msg;
1198}
1199
1200static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1201 bool is_ppp_msg)
1202{
1203 pkt->ppp_msg = is_ppp_msg;
1204}
1205#else /* CONFIG_NET_L2_PPP */
1206static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1207{
1208 ARG_UNUSED(pkt);
1209
1210 return false;
1211}
1212
1213static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1214 bool is_ppp_msg)
1215{
1216 ARG_UNUSED(pkt);
1217 ARG_UNUSED(is_ppp_msg);
1218}
1219#endif /* CONFIG_NET_L2_PPP */
1220
1221#if defined(NET_PKT_HAS_CONTROL_BLOCK)
1222static inline void *net_pkt_cb(struct net_pkt *pkt)
1223{
1224 return &pkt->cb;
1225}
1226#else
1227static inline void *net_pkt_cb(struct net_pkt *pkt)
1228{
1229 ARG_UNUSED(pkt);
1230
1231 return NULL;
1232}
1233#endif
1234
1235#define NET_IPV6_HDR(pkt) ((struct net_ipv6_hdr *)net_pkt_ip_data(pkt))
1236#define NET_IPV4_HDR(pkt) ((struct net_ipv4_hdr *)net_pkt_ip_data(pkt))
1237
1238static inline void net_pkt_set_src_ipv6_addr(struct net_pkt *pkt)
1239{
1241 net_pkt_context(pkt)),
1242 (struct in6_addr *)NET_IPV6_HDR(pkt)->src);
1243}
1244
1245static inline void net_pkt_set_overwrite(struct net_pkt *pkt, bool overwrite)
1246{
1247 pkt->overwrite = overwrite;
1248}
1249
1250static inline bool net_pkt_is_being_overwritten(struct net_pkt *pkt)
1251{
1252 return pkt->overwrite;
1253}
1254
1255#ifdef CONFIG_NET_PKT_FILTER
1256
1257bool net_pkt_filter_send_ok(struct net_pkt *pkt);
1258bool net_pkt_filter_recv_ok(struct net_pkt *pkt);
1259
1260#else
1261
1262static inline bool net_pkt_filter_send_ok(struct net_pkt *pkt)
1263{
1264 ARG_UNUSED(pkt);
1265
1266 return true;
1267}
1268
1269static inline bool net_pkt_filter_recv_ok(struct net_pkt *pkt)
1270{
1271 ARG_UNUSED(pkt);
1272
1273 return true;
1274}
1275
1276#endif /* CONFIG_NET_PKT_FILTER */
1277
1278/* @endcond */
1279
1293#define NET_PKT_SLAB_DEFINE(name, count) \
1294 K_MEM_SLAB_DEFINE(name, sizeof(struct net_pkt), count, 4)
1295
1296/* Backward compatibility macro */
1297#define NET_PKT_TX_SLAB_DEFINE(name, count) NET_PKT_SLAB_DEFINE(name, count)
1298
1312#define NET_PKT_DATA_POOL_DEFINE(name, count) \
1313 NET_BUF_POOL_DEFINE(name, count, CONFIG_NET_BUF_DATA_SIZE, \
1314 0, NULL)
1315
1318#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC) || \
1319 (CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG)
1320#define NET_PKT_DEBUG_ENABLED
1321#endif
1322
1323#if defined(NET_PKT_DEBUG_ENABLED)
1324
1325/* Debug versions of the net_pkt functions that are used when tracking
1326 * buffer usage.
1327 */
1328
1329struct net_buf *net_pkt_get_reserve_data_debug(struct net_buf_pool *pool,
1330 size_t min_len,
1332 const char *caller,
1333 int line);
1334
1335#define net_pkt_get_reserve_data(pool, min_len, timeout) \
1336 net_pkt_get_reserve_data_debug(pool, min_len, timeout, __func__, __LINE__)
1337
1338struct net_buf *net_pkt_get_reserve_rx_data_debug(size_t min_len,
1340 const char *caller,
1341 int line);
1342#define net_pkt_get_reserve_rx_data(min_len, timeout) \
1343 net_pkt_get_reserve_rx_data_debug(min_len, timeout, __func__, __LINE__)
1344
1345struct net_buf *net_pkt_get_reserve_tx_data_debug(size_t min_len,
1347 const char *caller,
1348 int line);
1349#define net_pkt_get_reserve_tx_data(min_len, timeout) \
1350 net_pkt_get_reserve_tx_data_debug(min_len, timeout, __func__, __LINE__)
1351
1352struct net_buf *net_pkt_get_frag_debug(struct net_pkt *pkt, size_t min_len,
1354 const char *caller, int line);
1355#define net_pkt_get_frag(pkt, min_len, timeout) \
1356 net_pkt_get_frag_debug(pkt, min_len, timeout, __func__, __LINE__)
1357
1358void net_pkt_unref_debug(struct net_pkt *pkt, const char *caller, int line);
1359#define net_pkt_unref(pkt) net_pkt_unref_debug(pkt, __func__, __LINE__)
1360
1361struct net_pkt *net_pkt_ref_debug(struct net_pkt *pkt, const char *caller,
1362 int line);
1363#define net_pkt_ref(pkt) net_pkt_ref_debug(pkt, __func__, __LINE__)
1364
1365struct net_buf *net_pkt_frag_ref_debug(struct net_buf *frag,
1366 const char *caller, int line);
1367#define net_pkt_frag_ref(frag) net_pkt_frag_ref_debug(frag, __func__, __LINE__)
1368
1369void net_pkt_frag_unref_debug(struct net_buf *frag,
1370 const char *caller, int line);
1371#define net_pkt_frag_unref(frag) \
1372 net_pkt_frag_unref_debug(frag, __func__, __LINE__)
1373
1374struct net_buf *net_pkt_frag_del_debug(struct net_pkt *pkt,
1375 struct net_buf *parent,
1376 struct net_buf *frag,
1377 const char *caller, int line);
1378#define net_pkt_frag_del(pkt, parent, frag) \
1379 net_pkt_frag_del_debug(pkt, parent, frag, __func__, __LINE__)
1380
1381void net_pkt_frag_add_debug(struct net_pkt *pkt, struct net_buf *frag,
1382 const char *caller, int line);
1383#define net_pkt_frag_add(pkt, frag) \
1384 net_pkt_frag_add_debug(pkt, frag, __func__, __LINE__)
1385
1386void net_pkt_frag_insert_debug(struct net_pkt *pkt, struct net_buf *frag,
1387 const char *caller, int line);
1388#define net_pkt_frag_insert(pkt, frag) \
1389 net_pkt_frag_insert_debug(pkt, frag, __func__, __LINE__)
1390#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC ||
1391 * CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG
1392 */
1402#if defined(NET_PKT_DEBUG_ENABLED)
1403void net_pkt_print_frags(struct net_pkt *pkt);
1404#else
1405#define net_pkt_print_frags(pkt)
1406#endif
1407
1422#if !defined(NET_PKT_DEBUG_ENABLED)
1424#endif
1425
1440#if !defined(NET_PKT_DEBUG_ENABLED)
1442#endif
1443
1456#if !defined(NET_PKT_DEBUG_ENABLED)
1457struct net_buf *net_pkt_get_frag(struct net_pkt *pkt, size_t min_len,
1459#endif
1460
1470#if !defined(NET_PKT_DEBUG_ENABLED)
1471void net_pkt_unref(struct net_pkt *pkt);
1472#endif
1473
1483#if !defined(NET_PKT_DEBUG_ENABLED)
1484struct net_pkt *net_pkt_ref(struct net_pkt *pkt);
1485#endif
1486
1496#if !defined(NET_PKT_DEBUG_ENABLED)
1497struct net_buf *net_pkt_frag_ref(struct net_buf *frag);
1498#endif
1499
1505#if !defined(NET_PKT_DEBUG_ENABLED)
1506void net_pkt_frag_unref(struct net_buf *frag);
1507#endif
1508
1519#if !defined(NET_PKT_DEBUG_ENABLED)
1521 struct net_buf *parent,
1522 struct net_buf *frag);
1523#endif
1524
1531#if !defined(NET_PKT_DEBUG_ENABLED)
1532void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag);
1533#endif
1534
1541#if !defined(NET_PKT_DEBUG_ENABLED)
1542void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag);
1543#endif
1544
1551void net_pkt_compact(struct net_pkt *pkt);
1552
1561void net_pkt_get_info(struct k_mem_slab **rx,
1562 struct k_mem_slab **tx,
1563 struct net_buf_pool **rx_data,
1564 struct net_buf_pool **tx_data);
1565
1568#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
1572void net_pkt_print(void);
1573
1574typedef void (*net_pkt_allocs_cb_t)(struct net_pkt *pkt,
1575 struct net_buf *buf,
1576 const char *func_alloc,
1577 int line_alloc,
1578 const char *func_free,
1579 int line_free,
1580 bool in_use,
1581 void *user_data);
1582
1583void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data);
1584
1585const char *net_pkt_slab2str(struct k_mem_slab *slab);
1586const char *net_pkt_pool2str(struct net_buf_pool *pool);
1587
1588#else
1589#define net_pkt_print(...)
1590#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
1591
1592/* New allocator, and API are defined below.
1593 * This will be simpler when time will come to get rid of former API above.
1594 */
1595#if defined(NET_PKT_DEBUG_ENABLED)
1596
1597struct net_pkt *net_pkt_alloc_debug(k_timeout_t timeout,
1598 const char *caller, int line);
1599#define net_pkt_alloc(_timeout) \
1600 net_pkt_alloc_debug(_timeout, __func__, __LINE__)
1601
1602struct net_pkt *net_pkt_alloc_from_slab_debug(struct k_mem_slab *slab,
1604 const char *caller, int line);
1605#define net_pkt_alloc_from_slab(_slab, _timeout) \
1606 net_pkt_alloc_from_slab_debug(_slab, _timeout, __func__, __LINE__)
1607
1608struct net_pkt *net_pkt_rx_alloc_debug(k_timeout_t timeout,
1609 const char *caller, int line);
1610#define net_pkt_rx_alloc(_timeout) \
1611 net_pkt_rx_alloc_debug(_timeout, __func__, __LINE__)
1612
1613struct net_pkt *net_pkt_alloc_on_iface_debug(struct net_if *iface,
1615 const char *caller,
1616 int line);
1617#define net_pkt_alloc_on_iface(_iface, _timeout) \
1618 net_pkt_alloc_on_iface_debug(_iface, _timeout, __func__, __LINE__)
1619
1620struct net_pkt *net_pkt_rx_alloc_on_iface_debug(struct net_if *iface,
1622 const char *caller,
1623 int line);
1624#define net_pkt_rx_alloc_on_iface(_iface, _timeout) \
1625 net_pkt_rx_alloc_on_iface_debug(_iface, _timeout, \
1626 __func__, __LINE__)
1627
1628int net_pkt_alloc_buffer_debug(struct net_pkt *pkt,
1629 size_t size,
1630 enum net_ip_protocol proto,
1632 const char *caller, int line);
1633#define net_pkt_alloc_buffer(_pkt, _size, _proto, _timeout) \
1634 net_pkt_alloc_buffer_debug(_pkt, _size, _proto, _timeout, \
1635 __func__, __LINE__)
1636
1637struct net_pkt *net_pkt_alloc_with_buffer_debug(struct net_if *iface,
1638 size_t size,
1639 sa_family_t family,
1640 enum net_ip_protocol proto,
1642 const char *caller,
1643 int line);
1644#define net_pkt_alloc_with_buffer(_iface, _size, _family, \
1645 _proto, _timeout) \
1646 net_pkt_alloc_with_buffer_debug(_iface, _size, _family, \
1647 _proto, _timeout, \
1648 __func__, __LINE__)
1649
1650struct net_pkt *net_pkt_rx_alloc_with_buffer_debug(struct net_if *iface,
1651 size_t size,
1652 sa_family_t family,
1653 enum net_ip_protocol proto,
1655 const char *caller,
1656 int line);
1657#define net_pkt_rx_alloc_with_buffer(_iface, _size, _family, \
1658 _proto, _timeout) \
1659 net_pkt_rx_alloc_with_buffer_debug(_iface, _size, _family, \
1660 _proto, _timeout, \
1661 __func__, __LINE__)
1662#endif /* NET_PKT_DEBUG_ENABLED */
1675#if !defined(NET_PKT_DEBUG_ENABLED)
1677#endif
1678
1693#if !defined(NET_PKT_DEBUG_ENABLED)
1694struct net_pkt *net_pkt_alloc_from_slab(struct k_mem_slab *slab,
1696#endif
1697
1708#if !defined(NET_PKT_DEBUG_ENABLED)
1710#endif
1711
1720#if !defined(NET_PKT_DEBUG_ENABLED)
1723
1724/* Same as above but specifically for RX packet */
1727#endif
1728
1744#if !defined(NET_PKT_DEBUG_ENABLED)
1746 size_t size,
1747 enum net_ip_protocol proto,
1749#endif
1750
1762#if !defined(NET_PKT_DEBUG_ENABLED)
1764 size_t size,
1765 sa_family_t family,
1766 enum net_ip_protocol proto,
1768
1769/* Same as above but specifically for RX packet */
1771 size_t size,
1772 sa_family_t family,
1773 enum net_ip_protocol proto,
1775#endif
1776
1783void net_pkt_append_buffer(struct net_pkt *pkt, struct net_buf *buffer);
1784
1796
1813 enum net_ip_protocol proto);
1814
1824
1839int net_pkt_remove_tail(struct net_pkt *pkt, size_t length);
1840
1849
1856static inline void net_pkt_cursor_backup(struct net_pkt *pkt,
1857 struct net_pkt_cursor *backup)
1858{
1859 backup->buf = pkt->cursor.buf;
1860 backup->pos = pkt->cursor.pos;
1861}
1862
1869static inline void net_pkt_cursor_restore(struct net_pkt *pkt,
1870 struct net_pkt_cursor *backup)
1871{
1872 pkt->cursor.buf = backup->buf;
1873 pkt->cursor.pos = backup->pos;
1874}
1875
1883static inline void *net_pkt_cursor_get_pos(struct net_pkt *pkt)
1884{
1885 return pkt->cursor.pos;
1886}
1887
1908int net_pkt_skip(struct net_pkt *pkt, size_t length);
1909
1924int net_pkt_memset(struct net_pkt *pkt, int byte, size_t length);
1925
1939int net_pkt_copy(struct net_pkt *pkt_dst,
1940 struct net_pkt *pkt_src,
1941 size_t length);
1942
1953
1964
1975
1989int net_pkt_read(struct net_pkt *pkt, void *data, size_t length);
1990
1991/* Read uint8_t data data a net_pkt */
1992static inline int net_pkt_read_u8(struct net_pkt *pkt, uint8_t *data)
1993{
1994 return net_pkt_read(pkt, data, 1);
1995}
1996
2010
2024
2038
2052int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length);
2053
2054/* Write uint8_t data into a net_pkt. */
2055static inline int net_pkt_write_u8(struct net_pkt *pkt, uint8_t data)
2056{
2057 return net_pkt_write(pkt, &data, sizeof(uint8_t));
2058}
2059
2060/* Write uint16_t big endian data into a net_pkt. */
2061static inline int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
2062{
2063 uint16_t data_be16 = htons(data);
2064
2065 return net_pkt_write(pkt, &data_be16, sizeof(uint16_t));
2066}
2067
2068/* Write uint32_t big endian data into a net_pkt. */
2069static inline int net_pkt_write_be32(struct net_pkt *pkt, uint32_t data)
2070{
2071 uint32_t data_be32 = htonl(data);
2072
2073 return net_pkt_write(pkt, &data_be32, sizeof(uint32_t));
2074}
2075
2076/* Write uint32_t little endian data into a net_pkt. */
2077static inline int net_pkt_write_le32(struct net_pkt *pkt, uint32_t data)
2078{
2079 uint32_t data_le32 = sys_cpu_to_le32(data);
2080
2081 return net_pkt_write(pkt, &data_le32, sizeof(uint32_t));
2082}
2083
2084/* Write uint16_t little endian data into a net_pkt. */
2085static inline int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
2086{
2087 uint16_t data_le16 = sys_cpu_to_le16(data);
2088
2089 return net_pkt_write(pkt, &data_le16, sizeof(uint16_t));
2090}
2091
2100
2113int net_pkt_update_length(struct net_pkt *pkt, size_t length);
2114
2127int net_pkt_pull(struct net_pkt *pkt, size_t length);
2128
2138
2150bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size);
2151
2161
2163#if !defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2164 void *data;
2165#endif
2166 const size_t size;
2167};
2168
2169#if defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2170#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2171 struct net_pkt_data_access _name = { \
2172 .size = sizeof(_type), \
2173 }
2174
2175#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2176 NET_PKT_DATA_ACCESS_DEFINE(_name, _type)
2177
2178#else
2179#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2180 _type _hdr_##_name; \
2181 struct net_pkt_data_access _name = { \
2182 .data = &_hdr_##_name, \
2183 .size = sizeof(_type), \
2184 }
2185
2186#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2187 struct net_pkt_data_access _name = { \
2188 .data = NULL, \
2189 .size = sizeof(_type), \
2190 }
2191
2192#endif /* CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS */
2193
2207void *net_pkt_get_data(struct net_pkt *pkt,
2208 struct net_pkt_data_access *access);
2209
2224 struct net_pkt_data_access *access);
2225
2230static inline int net_pkt_acknowledge_data(struct net_pkt *pkt,
2231 struct net_pkt_data_access *access)
2232{
2233 return net_pkt_skip(pkt, access->size);
2234}
2235
2240#ifdef __cplusplus
2241}
2242#endif
2243
2244#endif /* ZEPHYR_INCLUDE_NET_NET_PKT_H_ */
long atomic_t
Definition: atomic.h:22
#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:2454
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:859
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:1653
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:2069
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:1405
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:2061
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:2055
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:1883
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:1856
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:2230
static int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:2085
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:1869
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:2077
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:1992
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: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
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:971
Network buffer representation.
Definition: buf.h:906
uint8_t * data
Definition: buf.h:932
uint16_t len
Definition: buf.h:935
Definition: net_context.h:201
Network Interface structure.
Definition: net_if.h:516
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:2162
const size_t size
Definition: net_pkt.h:2166
void * data
Definition: net_pkt.h:2164
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
#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
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