Zephyr API Documentation  2.7.0-rc2
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 <net/buf.h>
23
24#include <net/net_core.h>
25#include <net/net_linkaddr.h>
26#include <net/net_ip.h>
27#include <net/net_if.h>
28#include <net/net_context.h>
29#include <net/ethernet_vlan.h>
30#include <net/ptp_time.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
43struct net_context;
44struct canbus_net_isotp_tx_ctx;
45struct canbus_net_isotp_rx_ctx;
46
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_ROUTING)
90 struct net_if *orig_iface; /* Original network interface */
91#endif
92
93#if defined(CONFIG_NET_PKT_TIMESTAMP)
95 struct net_ptp_time timestamp;
96#endif
97
98#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
99 struct {
101 uint32_t create_time;
102
103#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
104 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
110 struct {
111 uint32_t stat[NET_PKT_DETAIL_STATS_COUNT];
112 int count;
113 } detail;
114#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
115 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
116 };
117#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
118
119#if defined(CONFIG_NET_PKT_TXTIME)
121 uint64_t txtime;
122#endif /* CONFIG_NET_PKT_TXTIME */
123
125 atomic_t atomic_ref;
126
127 /* Filled by layer 2 when network packet is received. */
128 struct net_linkaddr lladdr_src;
129 struct net_linkaddr lladdr_dst;
130
131#if defined(CONFIG_NET_TCP2)
133 sys_snode_t next;
134#endif
135
136 uint8_t ip_hdr_len; /* pre-filled in order to avoid func call */
137
138 uint8_t overwrite : 1; /* Is packet content being overwritten? */
139
140 uint8_t sent_or_eof: 1; /* For outgoing packet: is this sent or not
141 * For incoming packet of a socket: last
142 * packet before EOF
143 * Used only if defined(CONFIG_NET_TCP)
144 */
145 union {
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 gptp_pkt: 1; /* For outgoing packet: is this packet
152 * a GPTP packet.
153 * Used only if defined (CONFIG_NET_GPTP)
154 */
155 };
156
157 uint8_t forwarding : 1; /* Are we forwarding this pkt
158 * Used only if defined(CONFIG_NET_ROUTE)
159 */
160 uint8_t family : 3; /* IPv4 vs IPv6 */
161
162 union {
163 uint8_t ipv4_auto_arp_msg : 1; /* Is this pkt IPv4 autoconf ARP
164 * message. Used only if
165 * defined(CONFIG_NET_IPV4_AUTO).
166 * Note: family needs to be
167 * AF_INET.
168 */
169 uint8_t lldp_pkt : 1; /* Is this pkt an LLDP message.
170 * Used only if
171 * defined(CONFIG_NET_LLDP).
172 * Note: family needs to be
173 * AF_UNSPEC.
174 */
175 uint8_t ppp_msg : 1; /* This is a PPP message */
176 };
177
178#if defined(CONFIG_NET_TCP)
179 uint8_t tcp_first_msg : 1; /* Is this the first time this pkt is
180 * sent, or is this a resend of a TCP
181 * segment.
182 */
183#endif
184
185 uint8_t captured : 1; /* Set to 1 if this packet is already being
186 * captured
187 */
188
189 uint8_t l2_bridged : 1; /* set to 1 if this packet comes from a bridge
190 * and already contains its L2 header to be
191 * preserved. Useful only if
192 * defined(CONFIG_NET_ETHERNET_BRIDGE).
193 */
194
195 union {
196 /* IPv6 hop limit or IPv4 ttl for this network packet.
197 * The value is shared between IPv6 and IPv4.
198 */
199 uint8_t ipv6_hop_limit;
200 uint8_t ipv4_ttl;
201 };
202
203 union {
204#if defined(CONFIG_NET_IPV4)
205 uint8_t ipv4_opts_len; /* Length if IPv4 Header Options */
206#endif
207#if defined(CONFIG_NET_IPV6)
208 uint16_t ipv6_ext_len; /* length of extension headers */
209#endif
210 };
211
215 uint8_t priority;
216
217#if defined(CONFIG_NET_VLAN)
218 /* VLAN TCI (Tag Control Information). This contains the Priority
219 * Code Point (PCP), Drop Eligible Indicator (DEI) and VLAN
220 * Identifier (VID, called more commonly VLAN tag). This value is
221 * kept in host byte order.
222 */
223 uint16_t vlan_tci;
224#endif /* CONFIG_NET_VLAN */
225
226#if defined(CONFIG_NET_IPV6)
227 /* Where is the start of the last header before payload data
228 * in IPv6 packet. This is offset value from start of the IPv6
229 * packet. Note that this value should be updated by who ever
230 * adds IPv6 extension headers to the network packet.
231 */
232 uint16_t ipv6_prev_hdr_start;
233
234#if defined(CONFIG_NET_IPV6_FRAGMENT)
235 uint16_t ipv6_fragment_offset; /* Fragment offset of this packet */
236 uint32_t ipv6_fragment_id; /* Fragment id */
237 uint16_t ipv6_frag_hdr_start; /* Where starts the fragment header */
238#endif /* CONFIG_NET_IPV6_FRAGMENT */
239
240 uint8_t ipv6_ext_opt_len; /* IPv6 ND option length */
241 uint8_t ipv6_next_hdr; /* What is the very first next header */
242#endif /* CONFIG_NET_IPV6 */
243
244#if defined(CONFIG_IEEE802154)
245 uint8_t ieee802154_rssi; /* Received Signal Strength Indication */
246 uint8_t ieee802154_lqi; /* Link Quality Indicator */
247 uint8_t ieee802154_arb : 1; /* ACK Request Bit is set in the frame */
248 uint8_t ieee802154_ack_fpb : 1; /* Frame Pending Bit was set in the ACK */
249 uint8_t ieee802154_frame_secured : 1; /* Frame is authenticated and
250 * encrypted according to its
251 * Auxiliary Security Header
252 */
253 uint8_t ieee802154_mac_hdr_rdy : 1; /* Indicates if frame's MAC header
254 * is ready to be transmitted or if
255 * it requires further modifications,
256 * e.g. Frame Counter injection.
257 */
258#if defined(CONFIG_IEEE802154_2015)
259 uint8_t ieee802154_fv2015 : 1; /* Frame version is IEEE 802.15.4-2015 */
260 uint8_t ieee802154_ack_seb : 1; /* Security Enabled Bit was set in the ACK */
261 uint32_t ieee802154_ack_fc; /* Frame counter set in the ACK */
262 uint8_t ieee802154_ack_keyid; /* Key index set in the ACK */
263#endif
264#endif
265#if defined(CONFIG_NET_L2_CANBUS)
266 union {
267 struct canbus_isotp_tx_ctx *canbus_tx_ctx;
268 struct canbus_isotp_rx_ctx *canbus_rx_ctx;
269 };
270#endif
271 /* @endcond */
272};
273
276/* The interface real ll address */
277static inline struct net_linkaddr *net_pkt_lladdr_if(struct net_pkt *pkt)
278{
279 return net_if_get_link_addr(pkt->iface);
280}
281
282static inline struct net_context *net_pkt_context(struct net_pkt *pkt)
283{
284 return pkt->context;
285}
286
287static inline void net_pkt_set_context(struct net_pkt *pkt,
288 struct net_context *ctx)
289{
290 pkt->context = ctx;
291}
292
293static inline struct net_if *net_pkt_iface(struct net_pkt *pkt)
294{
295 return pkt->iface;
296}
297
298static inline void net_pkt_set_iface(struct net_pkt *pkt, struct net_if *iface)
299{
300 pkt->iface = iface;
301
302 /* If the network interface is set in pkt, then also set the type of
303 * the network address that is stored in pkt. This is done here so
304 * that the address type is properly set and is not forgotten.
305 */
306 if (iface) {
307 pkt->lladdr_src.type = net_if_get_link_addr(iface)->type;
308 pkt->lladdr_dst.type = net_if_get_link_addr(iface)->type;
309 }
310}
311
312static inline struct net_if *net_pkt_orig_iface(struct net_pkt *pkt)
313{
314#if defined(CONFIG_NET_ROUTING)
315 return pkt->orig_iface;
316#else
317 return pkt->iface;
318#endif
319}
320
321static inline void net_pkt_set_orig_iface(struct net_pkt *pkt,
322 struct net_if *iface)
323{
324#if defined(CONFIG_NET_ROUTING)
325 pkt->orig_iface = iface;
326#endif
327}
328
329static inline uint8_t net_pkt_family(struct net_pkt *pkt)
330{
331 return pkt->family;
332}
333
334static inline void net_pkt_set_family(struct net_pkt *pkt, uint8_t family)
335{
336 pkt->family = family;
337}
338
339static inline bool net_pkt_is_gptp(struct net_pkt *pkt)
340{
341 return !!(pkt->gptp_pkt);
342}
343
344static inline void net_pkt_set_gptp(struct net_pkt *pkt, bool is_gptp)
345{
346 pkt->gptp_pkt = is_gptp;
347}
348
349static inline bool net_pkt_is_captured(struct net_pkt *pkt)
350{
351 return !!(pkt->captured);
352}
353
354static inline void net_pkt_set_captured(struct net_pkt *pkt, bool is_captured)
355{
356 pkt->captured = is_captured;
357}
358
359static inline bool net_pkt_is_l2_bridged(struct net_pkt *pkt)
360{
361 return IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) ? !!(pkt->l2_bridged) : 0;
362}
363
364static inline void net_pkt_set_l2_bridged(struct net_pkt *pkt, bool is_l2_bridged)
365{
366 if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE)) {
367 pkt->l2_bridged = is_l2_bridged;
368 }
369}
370
371static inline uint8_t net_pkt_ip_hdr_len(struct net_pkt *pkt)
372{
373 return pkt->ip_hdr_len;
374}
375
376static inline void net_pkt_set_ip_hdr_len(struct net_pkt *pkt, uint8_t len)
377{
378 pkt->ip_hdr_len = len;
379}
380
381static inline uint8_t net_pkt_sent(struct net_pkt *pkt)
382{
383 return pkt->sent_or_eof;
384}
385
386static inline void net_pkt_set_sent(struct net_pkt *pkt, bool sent)
387{
388 pkt->sent_or_eof = sent;
389}
390
391static inline uint8_t net_pkt_queued(struct net_pkt *pkt)
392{
393 return pkt->pkt_queued;
394}
395
396static inline void net_pkt_set_queued(struct net_pkt *pkt, bool send)
397{
398 pkt->pkt_queued = send;
399}
400
401static inline uint8_t net_pkt_tcp_1st_msg(struct net_pkt *pkt)
402{
403#if defined(CONFIG_NET_TCP)
404 return pkt->tcp_first_msg;
405#else
406 return true;
407#endif
408}
409
410static inline void net_pkt_set_tcp_1st_msg(struct net_pkt *pkt, bool is_1st)
411{
412#if defined(CONFIG_NET_TCP)
413 pkt->tcp_first_msg = is_1st;
414#else
415 ARG_UNUSED(pkt);
416 ARG_UNUSED(is_1st);
417#endif
418}
419
420#if defined(CONFIG_NET_SOCKETS)
421static inline uint8_t net_pkt_eof(struct net_pkt *pkt)
422{
423 return pkt->sent_or_eof;
424}
425
426static inline void net_pkt_set_eof(struct net_pkt *pkt, bool eof)
427{
428 pkt->sent_or_eof = eof;
429}
430#endif
431
432#if defined(CONFIG_NET_ROUTE)
433static inline bool net_pkt_forwarding(struct net_pkt *pkt)
434{
435 return pkt->forwarding;
436}
437
438static inline void net_pkt_set_forwarding(struct net_pkt *pkt, bool forward)
439{
440 pkt->forwarding = forward;
441}
442#else
443static inline bool net_pkt_forwarding(struct net_pkt *pkt)
444{
445 return false;
446}
447#endif
448
449#if defined(CONFIG_NET_IPV4)
450static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
451{
452 return pkt->ipv4_ttl;
453}
454
455static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
456 uint8_t ttl)
457{
458 pkt->ipv4_ttl = ttl;
459}
460
461static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
462{
463 return pkt->ipv4_opts_len;
464}
465
466static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
467 uint8_t opts_len)
468{
469 pkt->ipv4_opts_len = opts_len;
470}
471#else
472static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
473{
474 ARG_UNUSED(pkt);
475
476 return 0;
477}
478
479static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
480 uint8_t ttl)
481{
482 ARG_UNUSED(pkt);
483 ARG_UNUSED(ttl);
484}
485
486static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
487{
488 ARG_UNUSED(pkt);
489 return 0;
490}
491
492static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
493 uint8_t opts_len)
494{
495 ARG_UNUSED(pkt);
496 ARG_UNUSED(opts_len);
497}
498#endif
499
500#if defined(CONFIG_NET_IPV6)
501static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
502{
503 return pkt->ipv6_ext_opt_len;
504}
505
506static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
507 uint8_t len)
508{
509 pkt->ipv6_ext_opt_len = len;
510}
511
512static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
513{
514 return pkt->ipv6_next_hdr;
515}
516
517static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
518 uint8_t next_hdr)
519{
520 pkt->ipv6_next_hdr = next_hdr;
521}
522
523static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
524{
525 return pkt->ipv6_ext_len;
526}
527
528static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
529{
530 pkt->ipv6_ext_len = len;
531}
532
533static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
534{
535 return pkt->ipv6_prev_hdr_start;
536}
537
538static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
539 uint16_t offset)
540{
541 pkt->ipv6_prev_hdr_start = offset;
542}
543
544static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
545{
546 return pkt->ipv6_hop_limit;
547}
548
549static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
550 uint8_t hop_limit)
551{
552 pkt->ipv6_hop_limit = hop_limit;
553}
554#else /* CONFIG_NET_IPV6 */
555static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
556{
557 ARG_UNUSED(pkt);
558
559 return 0;
560}
561
562static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
563 uint8_t len)
564{
565 ARG_UNUSED(pkt);
566 ARG_UNUSED(len);
567}
568
569static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
570{
571 ARG_UNUSED(pkt);
572
573 return 0;
574}
575
576static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
577 uint8_t next_hdr)
578{
579 ARG_UNUSED(pkt);
580 ARG_UNUSED(next_hdr);
581}
582
583static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
584{
585 ARG_UNUSED(pkt);
586
587 return 0;
588}
589
590static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
591{
592 ARG_UNUSED(pkt);
593 ARG_UNUSED(len);
594}
595
596static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
597{
598 ARG_UNUSED(pkt);
599
600 return 0;
601}
602
603static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
604 uint16_t offset)
605{
606 ARG_UNUSED(pkt);
607 ARG_UNUSED(offset);
608}
609
610static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
611{
612 ARG_UNUSED(pkt);
613
614 return 0;
615}
616
617static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
618 uint8_t hop_limit)
619{
620 ARG_UNUSED(pkt);
621 ARG_UNUSED(hop_limit);
622}
623#endif /* CONFIG_NET_IPV6 */
624
625static inline uint16_t net_pkt_ip_opts_len(struct net_pkt *pkt)
626{
627#if defined(CONFIG_NET_IPV6)
628 return pkt->ipv6_ext_len;
629#elif defined(CONFIG_NET_IPV4)
630 return pkt->ipv4_opts_len;
631#else
632 ARG_UNUSED(pkt);
633
634 return 0;
635#endif
636}
637
638#if defined(CONFIG_NET_IPV6_FRAGMENT)
639static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
640{
641 return pkt->ipv6_frag_hdr_start;
642}
643
644static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
645 uint16_t start)
646{
647 pkt->ipv6_frag_hdr_start = start;
648}
649
650static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
651{
652 return pkt->ipv6_fragment_offset;
653}
654
655static inline void net_pkt_set_ipv6_fragment_offset(struct net_pkt *pkt,
656 uint16_t offset)
657{
658 pkt->ipv6_fragment_offset = offset;
659}
660
661static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
662{
663 return pkt->ipv6_fragment_id;
664}
665
666static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
667 uint32_t id)
668{
669 pkt->ipv6_fragment_id = id;
670}
671#else /* CONFIG_NET_IPV6_FRAGMENT */
672static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
673{
674 ARG_UNUSED(pkt);
675
676 return 0;
677}
678
679static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
680 uint16_t start)
681{
682 ARG_UNUSED(pkt);
683 ARG_UNUSED(start);
684}
685
686static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
687{
688 ARG_UNUSED(pkt);
689
690 return 0;
691}
692
693static inline void net_pkt_set_ipv6_fragment_offset(struct net_pkt *pkt,
694 uint16_t offset)
695{
696 ARG_UNUSED(pkt);
697 ARG_UNUSED(offset);
698}
699
700static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
701{
702 ARG_UNUSED(pkt);
703
704 return 0;
705}
706
707static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
708 uint32_t id)
709{
710 ARG_UNUSED(pkt);
711 ARG_UNUSED(id);
712}
713#endif /* CONFIG_NET_IPV6_FRAGMENT */
714
715static inline uint8_t net_pkt_priority(struct net_pkt *pkt)
716{
717 return pkt->priority;
718}
719
720static inline void net_pkt_set_priority(struct net_pkt *pkt,
721 uint8_t priority)
722{
723 pkt->priority = priority;
724}
725
726#if defined(CONFIG_NET_VLAN)
727static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
728{
729 return net_eth_vlan_get_vid(pkt->vlan_tci);
730}
731
732static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
733{
734 pkt->vlan_tci = net_eth_vlan_set_vid(pkt->vlan_tci, tag);
735}
736
737static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
738{
739 return net_eth_vlan_get_pcp(pkt->vlan_tci);
740}
741
742static inline void net_pkt_set_vlan_priority(struct net_pkt *pkt,
743 uint8_t priority)
744{
745 pkt->vlan_tci = net_eth_vlan_set_pcp(pkt->vlan_tci, priority);
746}
747
748static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
749{
750 return net_eth_vlan_get_dei(pkt->vlan_tci);
751}
752
753static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
754{
755 pkt->vlan_tci = net_eth_vlan_set_dei(pkt->vlan_tci, dei);
756}
757
758static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
759{
760 pkt->vlan_tci = tci;
761}
762
763static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
764{
765 return pkt->vlan_tci;
766}
767#else
768static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
769{
770 return NET_VLAN_TAG_UNSPEC;
771}
772
773static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
774{
775 ARG_UNUSED(pkt);
776 ARG_UNUSED(tag);
777}
778
779static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
780{
781 ARG_UNUSED(pkt);
782 return 0;
783}
784
785static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
786{
787 return false;
788}
789
790static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
791{
792 ARG_UNUSED(pkt);
793 ARG_UNUSED(dei);
794}
795
796static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
797{
798 return NET_VLAN_TAG_UNSPEC; /* assumes priority is 0 */
799}
800
801static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
802{
803 ARG_UNUSED(pkt);
804 ARG_UNUSED(tci);
805}
806#endif
807
808#if defined(CONFIG_NET_PKT_TIMESTAMP)
809static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
810{
811 return &pkt->timestamp;
812}
813
814static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
815 struct net_ptp_time *timestamp)
816{
817 pkt->timestamp.second = timestamp->second;
818 pkt->timestamp.nanosecond = timestamp->nanosecond;
819}
820#else
821static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
822{
823 ARG_UNUSED(pkt);
824
825 return NULL;
826}
827
828static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
829 struct net_ptp_time *timestamp)
830{
831 ARG_UNUSED(pkt);
832 ARG_UNUSED(timestamp);
833}
834#endif /* CONFIG_NET_PKT_TIMESTAMP */
835
836#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
837static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
838{
839 return pkt->create_time;
840}
841
842static inline void net_pkt_set_create_time(struct net_pkt *pkt,
843 uint32_t create_time)
844{
845 pkt->create_time = create_time;
846}
847#else
848static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
849{
850 ARG_UNUSED(pkt);
851
852 return 0U;
853}
854
855static inline void net_pkt_set_create_time(struct net_pkt *pkt,
856 uint32_t create_time)
857{
858 ARG_UNUSED(pkt);
859 ARG_UNUSED(create_time);
860}
861#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
862
863#if defined(CONFIG_NET_PKT_TXTIME)
864static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
865{
866 return pkt->txtime;
867}
868
869static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
870{
871 pkt->txtime = txtime;
872}
873#else
874static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
875{
876 ARG_UNUSED(pkt);
877
878 return 0;
879}
880
881static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
882{
883 ARG_UNUSED(pkt);
884 ARG_UNUSED(txtime);
885}
886#endif /* CONFIG_NET_PKT_TXTIME */
887
888#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
889 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
890static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
891{
892 return pkt->detail.stat;
893}
894
895static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
896{
897 return pkt->detail.count;
898}
899
900static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
901{
902 memset(&pkt->detail, 0, sizeof(pkt->detail));
903}
904
905static ALWAYS_INLINE void net_pkt_set_stats_tick(struct net_pkt *pkt,
906 uint32_t tick)
907{
908 if (pkt->detail.count >= NET_PKT_DETAIL_STATS_COUNT) {
909 NET_ERR("Detail stats count overflow (%d >= %d)",
910 pkt->detail.count, NET_PKT_DETAIL_STATS_COUNT);
911 return;
912 }
913
914 pkt->detail.stat[pkt->detail.count++] = tick;
915}
916
917#define net_pkt_set_tx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
918#define net_pkt_set_rx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
919#else
920static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
921{
922 ARG_UNUSED(pkt);
923
924 return NULL;
925}
926
927static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
928{
929 ARG_UNUSED(pkt);
930
931 return 0;
932}
933
934static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
935{
936 ARG_UNUSED(pkt);
937}
938
939static inline void net_pkt_set_stats_tick(struct net_pkt *pkt, uint32_t tick)
940{
941 ARG_UNUSED(pkt);
942 ARG_UNUSED(tick);
943}
944
945#define net_pkt_set_tx_stats_tick(pkt, tick)
946#define net_pkt_set_rx_stats_tick(pkt, tick)
947#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
948 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
949
950static inline size_t net_pkt_get_len(struct net_pkt *pkt)
951{
952 return net_buf_frags_len(pkt->frags);
953}
954
955static inline uint8_t *net_pkt_data(struct net_pkt *pkt)
956{
957 return pkt->frags->data;
958}
959
960static inline uint8_t *net_pkt_ip_data(struct net_pkt *pkt)
961{
962 return pkt->frags->data;
963}
964
965static inline bool net_pkt_is_empty(struct net_pkt *pkt)
966{
967 return !pkt->buffer || !net_pkt_data(pkt) || pkt->buffer->len == 0;
968}
969
970static inline struct net_linkaddr *net_pkt_lladdr_src(struct net_pkt *pkt)
971{
972 return &pkt->lladdr_src;
973}
974
975static inline struct net_linkaddr *net_pkt_lladdr_dst(struct net_pkt *pkt)
976{
977 return &pkt->lladdr_dst;
978}
979
980static inline void net_pkt_lladdr_swap(struct net_pkt *pkt)
981{
982 uint8_t *addr = net_pkt_lladdr_src(pkt)->addr;
983
984 net_pkt_lladdr_src(pkt)->addr = net_pkt_lladdr_dst(pkt)->addr;
985 net_pkt_lladdr_dst(pkt)->addr = addr;
986}
987
988static inline void net_pkt_lladdr_clear(struct net_pkt *pkt)
989{
990 net_pkt_lladdr_src(pkt)->addr = NULL;
991 net_pkt_lladdr_src(pkt)->len = 0U;
992}
993
994#if defined(CONFIG_IEEE802154) || defined(CONFIG_IEEE802154_RAW_MODE)
995static inline uint8_t net_pkt_ieee802154_rssi(struct net_pkt *pkt)
996{
997 return pkt->ieee802154_rssi;
998}
999
1000static inline void net_pkt_set_ieee802154_rssi(struct net_pkt *pkt,
1001 uint8_t rssi)
1002{
1003 pkt->ieee802154_rssi = rssi;
1004}
1005
1006static inline uint8_t net_pkt_ieee802154_lqi(struct net_pkt *pkt)
1007{
1008 return pkt->ieee802154_lqi;
1009}
1010
1011static inline void net_pkt_set_ieee802154_lqi(struct net_pkt *pkt,
1012 uint8_t lqi)
1013{
1014 pkt->ieee802154_lqi = lqi;
1015}
1016
1017static inline bool net_pkt_ieee802154_arb(struct net_pkt *pkt)
1018{
1019 return pkt->ieee802154_arb;
1020}
1021
1022static inline void net_pkt_set_ieee802154_arb(struct net_pkt *pkt, bool arb)
1023{
1024 pkt->ieee802154_arb = arb;
1025}
1026
1027static inline bool net_pkt_ieee802154_ack_fpb(struct net_pkt *pkt)
1028{
1029 return pkt->ieee802154_ack_fpb;
1030}
1031
1032static inline void net_pkt_set_ieee802154_ack_fpb(struct net_pkt *pkt,
1033 bool fpb)
1034{
1035 pkt->ieee802154_ack_fpb = fpb;
1036}
1037
1038static inline bool net_pkt_ieee802154_frame_secured(struct net_pkt *pkt)
1039{
1040 return pkt->ieee802154_frame_secured;
1041}
1042
1043static inline void net_pkt_set_ieee802154_frame_secured(struct net_pkt *pkt,
1044 bool secured)
1045{
1046 pkt->ieee802154_frame_secured = secured;
1047}
1048
1049static inline bool net_pkt_ieee802154_mac_hdr_rdy(struct net_pkt *pkt)
1050{
1051 return pkt->ieee802154_mac_hdr_rdy;
1052}
1053
1054static inline void net_pkt_set_ieee802154_mac_hdr_rdy(struct net_pkt *pkt,
1055 bool rdy)
1056{
1057 pkt->ieee802154_mac_hdr_rdy = rdy;
1058}
1059
1060#if defined(CONFIG_IEEE802154_2015)
1061static inline bool net_pkt_ieee802154_fv2015(struct net_pkt *pkt)
1062{
1063 return pkt->ieee802154_fv2015;
1064}
1065
1066static inline void net_pkt_set_ieee802154_fv2015(struct net_pkt *pkt, bool fv2015)
1067{
1068 pkt->ieee802154_fv2015 = fv2015;
1069}
1070
1071static inline bool net_pkt_ieee802154_ack_seb(struct net_pkt *pkt)
1072{
1073 return pkt->ieee802154_ack_seb;
1074}
1075
1076static inline void net_pkt_set_ieee802154_ack_seb(struct net_pkt *pkt, bool seb)
1077{
1078 pkt->ieee802154_ack_seb = seb;
1079}
1080
1081static inline uint32_t net_pkt_ieee802154_ack_fc(struct net_pkt *pkt)
1082{
1083 return pkt->ieee802154_ack_fc;
1084}
1085
1086static inline void net_pkt_set_ieee802154_ack_fc(struct net_pkt *pkt,
1087 uint32_t fc)
1088{
1089 pkt->ieee802154_ack_fc = fc;
1090}
1091
1092static inline uint8_t net_pkt_ieee802154_ack_keyid(struct net_pkt *pkt)
1093{
1094 return pkt->ieee802154_ack_keyid;
1095}
1096
1097static inline void net_pkt_set_ieee802154_ack_keyid(struct net_pkt *pkt,
1098 uint8_t keyid)
1099{
1100 pkt->ieee802154_ack_keyid = keyid;
1101}
1102#endif /* CONFIG_IEEE802154_2015 */
1103#endif /* CONFIG_IEEE802154 || CONFIG_IEEE802154_RAW_MODE */
1104
1105#if defined(CONFIG_NET_IPV4_AUTO)
1106static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1107{
1108 return pkt->ipv4_auto_arp_msg;
1109}
1110
1111static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1112 bool is_auto_arp_msg)
1113{
1114 pkt->ipv4_auto_arp_msg = is_auto_arp_msg;
1115}
1116#else /* CONFIG_NET_IPV4_AUTO */
1117static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1118{
1119 ARG_UNUSED(pkt);
1120
1121 return false;
1122}
1123
1124static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1125 bool is_auto_arp_msg)
1126{
1127 ARG_UNUSED(pkt);
1128 ARG_UNUSED(is_auto_arp_msg);
1129}
1130#endif /* CONFIG_NET_IPV4_AUTO */
1131
1132#if defined(CONFIG_NET_LLDP)
1133static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1134{
1135 return pkt->lldp_pkt;
1136}
1137
1138static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1139{
1140 pkt->lldp_pkt = is_lldp;
1141}
1142#else
1143static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1144{
1145 ARG_UNUSED(pkt);
1146
1147 return false;
1148}
1149
1150static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1151{
1152 ARG_UNUSED(pkt);
1153 ARG_UNUSED(is_lldp);
1154}
1155#endif /* CONFIG_NET_LLDP */
1156
1157#if defined(CONFIG_NET_PPP)
1158static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1159{
1160 return pkt->ppp_msg;
1161}
1162
1163static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1164 bool is_ppp_msg)
1165{
1166 pkt->ppp_msg = is_ppp_msg;
1167}
1168#else /* CONFIG_NET_PPP */
1169static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1170{
1171 ARG_UNUSED(pkt);
1172
1173 return false;
1174}
1175
1176static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1177 bool is_ppp_msg)
1178{
1179 ARG_UNUSED(pkt);
1180 ARG_UNUSED(is_ppp_msg);
1181}
1182#endif /* CONFIG_NET_PPP */
1183
1184#define NET_IPV6_HDR(pkt) ((struct net_ipv6_hdr *)net_pkt_ip_data(pkt))
1185#define NET_IPV4_HDR(pkt) ((struct net_ipv4_hdr *)net_pkt_ip_data(pkt))
1186
1187static inline void net_pkt_set_src_ipv6_addr(struct net_pkt *pkt)
1188{
1190 net_pkt_context(pkt)),
1191 &NET_IPV6_HDR(pkt)->src);
1192}
1193
1194static inline void net_pkt_set_overwrite(struct net_pkt *pkt, bool overwrite)
1195{
1196 pkt->overwrite = overwrite;
1197}
1198
1199static inline bool net_pkt_is_being_overwritten(struct net_pkt *pkt)
1200{
1201 return pkt->overwrite;
1202}
1203
1204/* @endcond */
1205
1219#define NET_PKT_SLAB_DEFINE(name, count) \
1220 K_MEM_SLAB_DEFINE(name, sizeof(struct net_pkt), count, 4)
1221
1222/* Backward compatibility macro */
1223#define NET_PKT_TX_SLAB_DEFINE(name, count) NET_PKT_SLAB_DEFINE(name, count)
1224
1238#define NET_PKT_DATA_POOL_DEFINE(name, count) \
1239 NET_BUF_POOL_DEFINE(name, count, CONFIG_NET_BUF_DATA_SIZE, \
1240 CONFIG_NET_BUF_USER_DATA_SIZE, NULL)
1241
1244#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC) || \
1245 (CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG)
1246#define NET_PKT_DEBUG_ENABLED
1247#endif
1248
1249#if defined(NET_PKT_DEBUG_ENABLED)
1250
1251/* Debug versions of the net_pkt functions that are used when tracking
1252 * buffer usage.
1253 */
1254
1255struct net_buf *net_pkt_get_reserve_data_debug(struct net_buf_pool *pool,
1257 const char *caller,
1258 int line);
1259
1260#define net_pkt_get_reserve_data(pool, timeout) \
1261 net_pkt_get_reserve_data_debug(pool, timeout, __func__, __LINE__)
1262
1263struct net_buf *net_pkt_get_reserve_rx_data_debug(k_timeout_t timeout,
1264 const char *caller,
1265 int line);
1266#define net_pkt_get_reserve_rx_data(timeout) \
1267 net_pkt_get_reserve_rx_data_debug(timeout, __func__, __LINE__)
1268
1269struct net_buf *net_pkt_get_reserve_tx_data_debug(k_timeout_t timeout,
1270 const char *caller,
1271 int line);
1272#define net_pkt_get_reserve_tx_data(timeout) \
1273 net_pkt_get_reserve_tx_data_debug(timeout, __func__, __LINE__)
1274
1275struct net_buf *net_pkt_get_frag_debug(struct net_pkt *pkt,
1277 const char *caller, int line);
1278#define net_pkt_get_frag(pkt, timeout) \
1279 net_pkt_get_frag_debug(pkt, timeout, __func__, __LINE__)
1280
1281void net_pkt_unref_debug(struct net_pkt *pkt, const char *caller, int line);
1282#define net_pkt_unref(pkt) net_pkt_unref_debug(pkt, __func__, __LINE__)
1283
1284struct net_pkt *net_pkt_ref_debug(struct net_pkt *pkt, const char *caller,
1285 int line);
1286#define net_pkt_ref(pkt) net_pkt_ref_debug(pkt, __func__, __LINE__)
1287
1288struct net_buf *net_pkt_frag_ref_debug(struct net_buf *frag,
1289 const char *caller, int line);
1290#define net_pkt_frag_ref(frag) net_pkt_frag_ref_debug(frag, __func__, __LINE__)
1291
1292void net_pkt_frag_unref_debug(struct net_buf *frag,
1293 const char *caller, int line);
1294#define net_pkt_frag_unref(frag) \
1295 net_pkt_frag_unref_debug(frag, __func__, __LINE__)
1296
1297struct net_buf *net_pkt_frag_del_debug(struct net_pkt *pkt,
1298 struct net_buf *parent,
1299 struct net_buf *frag,
1300 const char *caller, int line);
1301#define net_pkt_frag_del(pkt, parent, frag) \
1302 net_pkt_frag_del_debug(pkt, parent, frag, __func__, __LINE__)
1303
1304void net_pkt_frag_add_debug(struct net_pkt *pkt, struct net_buf *frag,
1305 const char *caller, int line);
1306#define net_pkt_frag_add(pkt, frag) \
1307 net_pkt_frag_add_debug(pkt, frag, __func__, __LINE__)
1308
1309void net_pkt_frag_insert_debug(struct net_pkt *pkt, struct net_buf *frag,
1310 const char *caller, int line);
1311#define net_pkt_frag_insert(pkt, frag) \
1312 net_pkt_frag_insert_debug(pkt, frag, __func__, __LINE__)
1313#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC ||
1314 * CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG
1315 */
1325#if defined(NET_PKT_DEBUG_ENABLED)
1326void net_pkt_print_frags(struct net_pkt *pkt);
1327#else
1328#define net_pkt_print_frags(pkt)
1329#endif
1330
1344#if !defined(NET_PKT_DEBUG_ENABLED)
1346#endif
1347
1361#if !defined(NET_PKT_DEBUG_ENABLED)
1363#endif
1364
1376#if !defined(NET_PKT_DEBUG_ENABLED)
1378#endif
1379
1389#if !defined(NET_PKT_DEBUG_ENABLED)
1390void net_pkt_unref(struct net_pkt *pkt);
1391#endif
1392
1402#if !defined(NET_PKT_DEBUG_ENABLED)
1403struct net_pkt *net_pkt_ref(struct net_pkt *pkt);
1404#endif
1405
1415#if !defined(NET_PKT_DEBUG_ENABLED)
1416struct net_buf *net_pkt_frag_ref(struct net_buf *frag);
1417#endif
1418
1424#if !defined(NET_PKT_DEBUG_ENABLED)
1425void net_pkt_frag_unref(struct net_buf *frag);
1426#endif
1427
1438#if !defined(NET_PKT_DEBUG_ENABLED)
1440 struct net_buf *parent,
1441 struct net_buf *frag);
1442#endif
1443
1450#if !defined(NET_PKT_DEBUG_ENABLED)
1451void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag);
1452#endif
1453
1460#if !defined(NET_PKT_DEBUG_ENABLED)
1461void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag);
1462#endif
1463
1472bool net_pkt_compact(struct net_pkt *pkt);
1473
1482void net_pkt_get_info(struct k_mem_slab **rx,
1483 struct k_mem_slab **tx,
1484 struct net_buf_pool **rx_data,
1485 struct net_buf_pool **tx_data);
1486
1489#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
1493void net_pkt_print(void);
1494
1495typedef void (*net_pkt_allocs_cb_t)(struct net_pkt *pkt,
1496 struct net_buf *buf,
1497 const char *func_alloc,
1498 int line_alloc,
1499 const char *func_free,
1500 int line_free,
1501 bool in_use,
1502 void *user_data);
1503
1504void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data);
1505
1506const char *net_pkt_slab2str(struct k_mem_slab *slab);
1507const char *net_pkt_pool2str(struct net_buf_pool *pool);
1508
1509#else
1510#define net_pkt_print(...)
1511#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
1512
1513/* New allocator, and API are defined below.
1514 * This will be simpler when time will come to get rid of former API above.
1515 */
1516#if defined(NET_PKT_DEBUG_ENABLED)
1517
1518struct net_pkt *net_pkt_alloc_debug(k_timeout_t timeout,
1519 const char *caller, int line);
1520#define net_pkt_alloc(_timeout) \
1521 net_pkt_alloc_debug(_timeout, __func__, __LINE__)
1522
1523struct net_pkt *net_pkt_alloc_from_slab_debug(struct k_mem_slab *slab,
1525 const char *caller, int line);
1526#define net_pkt_alloc_from_slab(_slab, _timeout) \
1527 net_pkt_alloc_from_slab_debug(_slab, _timeout, __func__, __LINE__)
1528
1529struct net_pkt *net_pkt_rx_alloc_debug(k_timeout_t timeout,
1530 const char *caller, int line);
1531#define net_pkt_rx_alloc(_timeout) \
1532 net_pkt_rx_alloc_debug(_timeout, __func__, __LINE__)
1533
1534struct net_pkt *net_pkt_alloc_on_iface_debug(struct net_if *iface,
1536 const char *caller,
1537 int line);
1538#define net_pkt_alloc_on_iface(_iface, _timeout) \
1539 net_pkt_alloc_on_iface_debug(_iface, _timeout, __func__, __LINE__)
1540
1541struct net_pkt *net_pkt_rx_alloc_on_iface_debug(struct net_if *iface,
1543 const char *caller,
1544 int line);
1545#define net_pkt_rx_alloc_on_iface(_iface, _timeout) \
1546 net_pkt_rx_alloc_on_iface_debug(_iface, _timeout, \
1547 __func__, __LINE__)
1548
1549int net_pkt_alloc_buffer_debug(struct net_pkt *pkt,
1550 size_t size,
1551 enum net_ip_protocol proto,
1553 const char *caller, int line);
1554#define net_pkt_alloc_buffer(_pkt, _size, _proto, _timeout) \
1555 net_pkt_alloc_buffer_debug(_pkt, _size, _proto, _timeout, \
1556 __func__, __LINE__)
1557
1558struct net_pkt *net_pkt_alloc_with_buffer_debug(struct net_if *iface,
1559 size_t size,
1560 sa_family_t family,
1561 enum net_ip_protocol proto,
1563 const char *caller,
1564 int line);
1565#define net_pkt_alloc_with_buffer(_iface, _size, _family, \
1566 _proto, _timeout) \
1567 net_pkt_alloc_with_buffer_debug(_iface, _size, _family, \
1568 _proto, _timeout, \
1569 __func__, __LINE__)
1570
1571struct net_pkt *net_pkt_rx_alloc_with_buffer_debug(struct net_if *iface,
1572 size_t size,
1573 sa_family_t family,
1574 enum net_ip_protocol proto,
1576 const char *caller,
1577 int line);
1578#define net_pkt_rx_alloc_with_buffer(_iface, _size, _family, \
1579 _proto, _timeout) \
1580 net_pkt_rx_alloc_with_buffer_debug(_iface, _size, _family, \
1581 _proto, _timeout, \
1582 __func__, __LINE__)
1583#endif /* NET_PKT_DEBUG_ENABLED */
1596#if !defined(NET_PKT_DEBUG_ENABLED)
1598#endif
1599
1614#if !defined(NET_PKT_DEBUG_ENABLED)
1615struct net_pkt *net_pkt_alloc_from_slab(struct k_mem_slab *slab,
1617#endif
1618
1629#if !defined(NET_PKT_DEBUG_ENABLED)
1631#endif
1632
1641#if !defined(NET_PKT_DEBUG_ENABLED)
1644
1645/* Same as above but specifically for RX packet */
1648#endif
1649
1665#if !defined(NET_PKT_DEBUG_ENABLED)
1667 size_t size,
1668 enum net_ip_protocol proto,
1670#endif
1671
1683#if !defined(NET_PKT_DEBUG_ENABLED)
1685 size_t size,
1686 sa_family_t family,
1687 enum net_ip_protocol proto,
1689
1690/* Same as above but specifically for RX packet */
1692 size_t size,
1693 sa_family_t family,
1694 enum net_ip_protocol proto,
1696#endif
1697
1704void net_pkt_append_buffer(struct net_pkt *pkt, struct net_buf *buffer);
1705
1717
1734 enum net_ip_protocol proto);
1735
1745
1760int net_pkt_remove_tail(struct net_pkt *pkt, size_t length);
1761
1770
1777static inline void net_pkt_cursor_backup(struct net_pkt *pkt,
1778 struct net_pkt_cursor *backup)
1779{
1780 backup->buf = pkt->cursor.buf;
1781 backup->pos = pkt->cursor.pos;
1782}
1783
1790static inline void net_pkt_cursor_restore(struct net_pkt *pkt,
1791 struct net_pkt_cursor *backup)
1792{
1793 pkt->cursor.buf = backup->buf;
1794 pkt->cursor.pos = backup->pos;
1795}
1796
1804static inline void *net_pkt_cursor_get_pos(struct net_pkt *pkt)
1805{
1806 return pkt->cursor.pos;
1807}
1808
1829int net_pkt_skip(struct net_pkt *pkt, size_t length);
1830
1845int net_pkt_memset(struct net_pkt *pkt, int byte, size_t length);
1846
1860int net_pkt_copy(struct net_pkt *pkt_dst,
1861 struct net_pkt *pkt_src,
1862 size_t length);
1863
1873
1884
1898int net_pkt_read(struct net_pkt *pkt, void *data, size_t length);
1899
1900/* Read uint8_t data data a net_pkt */
1901static inline int net_pkt_read_u8(struct net_pkt *pkt, uint8_t *data)
1902{
1903 return net_pkt_read(pkt, data, 1);
1904}
1905
1919
1933
1947
1961int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length);
1962
1963/* Write uint8_t data into a net_pkt. */
1964static inline int net_pkt_write_u8(struct net_pkt *pkt, uint8_t data)
1965{
1966 return net_pkt_write(pkt, &data, sizeof(uint8_t));
1967}
1968
1969/* Write uint16_t big endian data into a net_pkt. */
1970static inline int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
1971{
1972 uint16_t data_be16 = htons(data);
1973
1974 return net_pkt_write(pkt, &data_be16, sizeof(uint16_t));
1975}
1976
1977/* Write uint32_t big endian data into a net_pkt. */
1978static inline int net_pkt_write_be32(struct net_pkt *pkt, uint32_t data)
1979{
1980 uint32_t data_be32 = htonl(data);
1981
1982 return net_pkt_write(pkt, &data_be32, sizeof(uint32_t));
1983}
1984
1985/* Write uint32_t little endian data into a net_pkt. */
1986static inline int net_pkt_write_le32(struct net_pkt *pkt, uint32_t data)
1987{
1988 uint32_t data_le32 = sys_cpu_to_le32(data);
1989
1990 return net_pkt_write(pkt, &data_le32, sizeof(uint32_t));
1991}
1992
1993/* Write uint16_t little endian data into a net_pkt. */
1994static inline int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
1995{
1996 uint16_t data_le16 = sys_cpu_to_le16(data);
1997
1998 return net_pkt_write(pkt, &data_le16, sizeof(uint16_t));
1999}
2000
2009
2022int net_pkt_update_length(struct net_pkt *pkt, size_t length);
2023
2036int net_pkt_pull(struct net_pkt *pkt, size_t length);
2037
2047
2059bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size);
2060
2070
2072#if !defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2073 void *data;
2074#endif
2075 const size_t size;
2076};
2077
2078#if defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2079#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2080 struct net_pkt_data_access _name = { \
2081 .size = sizeof(_type), \
2082 }
2083
2084#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2085 NET_PKT_DATA_ACCESS_DEFINE(_name, _type)
2086
2087#else
2088#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2089 _type _hdr_##_name; \
2090 struct net_pkt_data_access _name = { \
2091 .data = &_hdr_##_name, \
2092 .size = sizeof(_type), \
2093 }
2094
2095#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2096 struct net_pkt_data_access _name = { \
2097 .data = NULL, \
2098 .size = sizeof(_type), \
2099 }
2100
2101#endif /* CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS */
2102
2116void *net_pkt_get_data(struct net_pkt *pkt,
2117 struct net_pkt_data_access *access);
2118
2133 struct net_pkt_data_access *access);
2134
2139static inline int net_pkt_acknowledge_data(struct net_pkt *pkt,
2140 struct net_pkt_data_access *access)
2141{
2142 return net_pkt_skip(pkt, access->size);
2143}
2144
2149#ifdef __cplusplus
2150}
2151#endif
2152
2153#endif /* ZEPHYR_INCLUDE_NET_NET_PKT_H_ */
int atomic_t
Definition: atomic.h:21
#define sys_cpu_to_le32(val)
Convert 32-bit integer from host endianness to little-endian.
Definition: byteorder.h:177
#define sys_cpu_to_le16(val)
Convert 16-bit integer from host endianness to little-endian.
Definition: byteorder.h:173
#define ALWAYS_INLINE
Definition: common.h:116
ZTEST_BMEM int timeout
Definition: main.c:31
ZTEST_BMEM int count
Definition: main.c:33
void
Definition: eswifi_shell.c:15
VLAN specific definitions.
unsigned short int sa_family_t
Definition: net_ip.h:158
#define htons(x)
Convert 16-bit value from host to network byte order.
Definition: net_ip.h:120
#define htonl(x)
Convert 32-bit value from host to network byte order.
Definition: net_ip.h:128
net_ip_protocol
Definition: net_ip.h:62
static ZTEST_BMEM char buffer[8]
Test mailbox enhance capabilities.
Definition: test_mbox_api.c:566
static size_t net_buf_frags_len(struct net_buf *buf)
Calculate amount of bytes stored in fragments.
Definition: buf.h:2435
static struct net_if * net_context_get_iface(struct net_context *context)
Get network interface for this context.
Definition: net_context.h:633
static struct net_linkaddr * net_if_get_link_addr(struct net_if *iface)
Get an network interface's link address.
Definition: net_if.h:680
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:1428
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:1978
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:1328
int net_pkt_update_length(struct net_pkt *pkt, size_t length)
Update the overall length of a packet.
struct net_buf * net_pkt_get_reserve_rx_data(k_timeout_t timeout)
Get RX DATA buffer from pool. Normally you should use net_pkt_get_frag() instead.
int net_pkt_pull(struct net_pkt *pkt, size_t length)
Remove data from the packet at current location.
int net_pkt_copy(struct net_pkt *pkt_dst, struct net_pkt *pkt_src, size_t length)
Copy data from a packet into another one.
struct net_pkt * net_pkt_rx_alloc(k_timeout_t timeout)
Allocate an initialized net_pkt for RX.
struct net_pkt * net_pkt_ref(struct net_pkt *pkt)
Increase the packet ref count.
int net_pkt_read_be16(struct net_pkt *pkt, uint16_t *data)
Read uint16_t big endian data from a net_pkt.
struct net_pkt * net_pkt_alloc_with_buffer(struct net_if *iface, size_t size, sa_family_t family, enum net_ip_protocol proto, k_timeout_t timeout)
Allocate a network packet and buffer at once.
void net_pkt_frag_unref(struct net_buf *frag)
Decrease the packet fragment ref count.
struct net_pkt * net_pkt_rx_alloc_with_buffer(struct net_if *iface, size_t size, sa_family_t family, enum net_ip_protocol proto, k_timeout_t timeout)
void net_pkt_trim_buffer(struct net_pkt *pkt)
Trim net_pkt buffer.
struct net_pkt * net_pkt_alloc_on_iface(struct net_if *iface, k_timeout_t timeout)
Allocate a network packet for a specific network interface.
void net_pkt_get_info(struct k_mem_slab **rx, struct k_mem_slab **tx, struct net_buf_pool **rx_data, struct net_buf_pool **tx_data)
Get information about predefined RX, TX and DATA pools.
void net_pkt_unref(struct net_pkt *pkt)
Place packet back into the available packets slab.
bool net_pkt_compact(struct net_pkt *pkt)
Compact the fragment list of a packet.
static int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:1970
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:1964
size_t net_pkt_available_payload_buffer(struct net_pkt *pkt, enum net_ip_protocol proto)
Get available buffer space for payload from a pkt.
int net_pkt_read_le16(struct net_pkt *pkt, uint16_t *data)
Read uint16_t little endian data from a net_pkt.
int net_pkt_read_be32(struct net_pkt *pkt, uint32_t *data)
Read uint32_t big endian data from a net_pkt.
struct net_pkt * net_pkt_rx_alloc_on_iface(struct net_if *iface, k_timeout_t timeout)
int net_pkt_remove_tail(struct net_pkt *pkt, size_t length)
Remove length bytes from tail of packet.
static void * net_pkt_cursor_get_pos(struct net_pkt *pkt)
Returns current position of the cursor.
Definition: net_pkt.h:1804
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:1777
static int net_pkt_acknowledge_data(struct net_pkt *pkt, struct net_pkt_data_access *access)
Definition: net_pkt.h:2139
static int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:1994
struct net_buf * net_pkt_get_reserve_tx_data(k_timeout_t timeout)
Get TX DATA buffer from pool. Normally you should use net_pkt_get_frag() instead.
static void net_pkt_cursor_restore(struct net_pkt *pkt, struct net_pkt_cursor *backup)
Restore net_pkt cursor from a backup.
Definition: net_pkt.h:1790
uint16_t net_pkt_get_current_offset(struct net_pkt *pkt)
Get the actual offset in the packet from its cursor.
size_t net_pkt_remaining_data(struct net_pkt *pkt)
Get the amount of data which can be read from current cursor position.
int net_pkt_alloc_buffer(struct net_pkt *pkt, size_t size, enum net_ip_protocol proto, k_timeout_t timeout)
Allocate buffer for a net_pkt.
struct net_buf * net_pkt_get_frag(struct net_pkt *pkt, k_timeout_t timeout)
Get a data fragment that might be from user specific buffer pool or from global DATA pool.
int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length)
Write data into a net_pkt.
struct net_buf * net_pkt_frag_ref(struct net_buf *frag)
Increase the packet fragment ref count.
size_t net_pkt_available_buffer(struct net_pkt *pkt)
Get available buffer space from a pkt.
struct net_pkt * net_pkt_clone(struct net_pkt *pkt, k_timeout_t timeout)
Clone pkt and its buffer.
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:1986
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:1901
size_t net_pkt_get_contiguous_len(struct net_pkt *pkt)
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:101
static uint16_t net_eth_vlan_set_vid(uint16_t tci, uint16_t vid)
Set VLAN identifier to TCI.
Definition: ethernet_vlan.h:76
static uint8_t net_eth_vlan_get_dei(uint16_t tci)
Get Drop Eligible Indicator from TCI.
Definition: ethernet_vlan.h:51
#define NET_VLAN_TAG_UNSPEC
Definition: ethernet_vlan.h:30
static uint16_t net_eth_vlan_set_dei(uint16_t tci, bool dei)
Set Drop Eligible Indicator to TCI.
Definition: ethernet_vlan.h:89
static uint16_t net_eth_vlan_get_vid(uint16_t tci)
Get VLAN identifier from TCI.
Definition: ethernet_vlan.h:39
static uint16_t net_eth_vlan_set_pcp(uint16_t tci, uint8_t pcp)
Set Priority Code Point to TCI.
Definition: ethernet_vlan.h:102
static uint8_t net_eth_vlan_get_pcp(uint16_t tci)
Get Priority Code Point from TCI.
Definition: ethernet_vlan.h:63
Buffer management.
Network context definitions.
Network core definitions.
Public API for network interface.
IPv6 and IPv4 definitions.
Public API for network link address.
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:60
__INTPTR_TYPE__ intptr_t
Definition: stdint.h:74
__UINT64_TYPE__ uint64_t
Definition: stdint.h:61
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
void * memset(void *buf, int c, size_t n)
Kernel timeout type.
Definition: sys_clock.h:65
Network buffer pool representation.
Definition: buf.h:983
Network buffer representation.
Definition: buf.h:919
uint8_t * data
Definition: buf.h:944
uint16_t len
Definition: buf.h:947
Definition: net_context.h:201
Network Interface structure.
Definition: net_if.h:468
Hardware link address structure.
Definition: net_linkaddr.h:69
uint8_t * addr
Definition: net_linkaddr.h:71
uint8_t type
Definition: net_linkaddr.h:77
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:2071
const size_t size
Definition: net_pkt.h:2075
void * data
Definition: net_pkt.h:2073
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:59
uint64_t second
Definition: ptp_time.h:55
Definition: stat.h:39
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static const char * tag(void)
Definition: main.c:27
static const intptr_t user_data[5]
Definition: main.c:590
NMI_API sint16 send(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 u16Flags)