Zephyr Project API  3.2.0
A Scalable Open Source RTOS
net_pkt.h
Go to the documentation of this file.
1
8/*
9 * Copyright (c) 2016 Intel Corporation
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 */
13
14/* Data buffer API - used for all data to/from net */
15
16#ifndef ZEPHYR_INCLUDE_NET_NET_PKT_H_
17#define ZEPHYR_INCLUDE_NET_NET_PKT_H_
18
19#include <zephyr/types.h>
20#include <stdbool.h>
21
22#include <zephyr/net/buf.h>
23
24#include <zephyr/net/net_core.h>
26#include <zephyr/net/net_ip.h>
27#include <zephyr/net/net_if.h>
30#include <zephyr/net/ptp_time.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
43struct net_context;
44
45/* buffer cursor used in net_pkt */
48 struct net_buf *buf;
51};
52
59struct net_pkt {
65
67 struct k_mem_slab *slab;
68
70 union {
71 struct net_buf *frags;
72 struct net_buf *buffer;
73 };
74
77
80
82 struct net_if *iface;
83
86#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
87 struct net_if *orig_iface; /* Original network interface */
88#endif
89
90#if defined(CONFIG_NET_PKT_TIMESTAMP)
92 struct net_ptp_time timestamp;
93#endif
94
95#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
96 struct {
98 uint32_t create_time;
99
100#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
101 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
107 struct {
108 uint32_t stat[NET_PKT_DETAIL_STATS_COUNT];
109 int count;
110 } detail;
111#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
112 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
113 };
114#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
115
116#if defined(CONFIG_NET_PKT_TXTIME)
118 uint64_t txtime;
119#endif /* CONFIG_NET_PKT_TXTIME */
120
122 atomic_t atomic_ref;
123
124 /* Filled by layer 2 when network packet is received. */
125 struct net_linkaddr lladdr_src;
126 struct net_linkaddr lladdr_dst;
127 uint16_t ll_proto_type;
128
129#if defined(CONFIG_NET_TCP)
131 sys_snode_t next;
132#endif
133
134#if defined(CONFIG_NET_IP)
135 uint8_t ip_hdr_len; /* pre-filled in order to avoid func call */
136#endif
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 ptp_pkt: 1; /* For outgoing packet: is this packet
152 * a L2 PTP packet.
153 * Used only if defined (CONFIG_NET_L2_PTP)
154 */
155 };
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; /* Address family, see net_ip.h */
161
162 union {
163#if defined(CONFIG_NET_IPV4_AUTO)
164 uint8_t ipv4_auto_arp_msg : 1; /* Is this pkt IPv4 autoconf ARP
165 * message.
166 * Note: family needs to be
167 * AF_INET.
168 */
169#endif
170#if defined(CONFIG_NET_LLDP)
171 uint8_t lldp_pkt : 1; /* Is this pkt an LLDP message.
172 * Note: family needs to be
173 * AF_UNSPEC.
174 */
175#endif
176 uint8_t ppp_msg : 1; /* This is a PPP message */
177 };
178
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
186 uint8_t captured : 1; /* Set to 1 if this packet is already being
187 * captured
188 */
189
190 uint8_t l2_bridged : 1; /* set to 1 if this packet comes from a bridge
191 * and already contains its L2 header to be
192 * preserved. Useful only if
193 * defined(CONFIG_NET_ETHERNET_BRIDGE).
194 */
195
196 uint8_t l2_processed : 1; /* Set to 1 if this packet has already been
197 * processed by the L2
198 */
199
200#if defined(CONFIG_NET_IP)
201 union {
202 /* IPv6 hop limit or IPv4 ttl for this network packet.
203 * The value is shared between IPv6 and IPv4.
204 */
205#if defined(CONFIG_NET_IPV6)
206 uint8_t ipv6_hop_limit;
207#endif
208#if defined(CONFIG_NET_IPV4)
209 uint8_t ipv4_ttl;
210#endif
211 };
212
213 union {
214#if defined(CONFIG_NET_IPV4)
215 uint8_t ipv4_opts_len; /* Length if IPv4 Header Options */
216#endif
217#if defined(CONFIG_NET_IPV6)
218 uint16_t ipv6_ext_len; /* length of extension headers */
219#endif
220 };
221#endif /* CONFIG_NET_IP */
222
226 uint8_t priority;
227
228#if defined(CONFIG_NET_VLAN)
229 /* VLAN TCI (Tag Control Information). This contains the Priority
230 * Code Point (PCP), Drop Eligible Indicator (DEI) and VLAN
231 * Identifier (VID, called more commonly VLAN tag). This value is
232 * kept in host byte order.
233 */
234 uint16_t vlan_tci;
235#endif /* CONFIG_NET_VLAN */
236
237#if defined(CONFIG_NET_IPV6)
238 /* Where is the start of the last header before payload data
239 * in IPv6 packet. This is offset value from start of the IPv6
240 * packet. Note that this value should be updated by who ever
241 * adds IPv6 extension headers to the network packet.
242 */
243 uint16_t ipv6_prev_hdr_start;
244
245#if defined(CONFIG_NET_IPV6_FRAGMENT)
246 uint16_t ipv6_fragment_flags; /* Fragment offset and M (More Fragment) flag */
247 uint32_t ipv6_fragment_id; /* Fragment id */
248 uint16_t ipv6_frag_hdr_start; /* Where starts the fragment header */
249#endif /* CONFIG_NET_IPV6_FRAGMENT */
250
251 uint8_t ipv6_ext_opt_len; /* IPv6 ND option length */
252 uint8_t ipv6_next_hdr; /* What is the very first next header */
253#endif /* CONFIG_NET_IPV6 */
254
255#if defined(CONFIG_IEEE802154)
256#if defined(CONFIG_IEEE802154_2015)
257 uint32_t ieee802154_ack_fc; /* Frame counter set in the ACK */
258 uint8_t ieee802154_ack_keyid; /* Key index set in the ACK */
259#endif
260 uint8_t ieee802154_lqi; /* Link Quality Indicator */
261 union {
262 uint8_t ieee802154_rssi; /* Received Signal Strength Indication */
263#if defined(CONFIG_IEEE802154_SELECTIVE_TXPOWER)
264 int8_t ieee802154_txpwr; /* TX power in dBm. It should be clear from
265 * the context which field of the union
266 * is valid at the moment.
267 */
268#endif /* CONFIG_IEEE802154_SELECTIVE_TXPOWER */
269 };
270#if defined(CONFIG_IEEE802154_2015)
271 uint8_t ieee802154_fv2015 : 1; /* Frame version is IEEE 802.15.4-2015 */
272 uint8_t ieee802154_ack_seb : 1; /* Security Enabled Bit was set in the ACK */
273#endif
274 uint8_t ieee802154_arb : 1; /* ACK Request Bit is set in the frame */
275 uint8_t ieee802154_ack_fpb : 1; /* Frame Pending Bit was set in the ACK */
276 uint8_t ieee802154_frame_secured : 1; /* Frame is authenticated and
277 * encrypted according to its
278 * Auxiliary Security Header
279 */
280 uint8_t ieee802154_mac_hdr_rdy : 1; /* Indicates if frame's MAC header
281 * is ready to be transmitted or if
282 * it requires further modifications,
283 * e.g. Frame Counter injection.
284 */
285#endif
286 /* @endcond */
287};
288
291/* The interface real ll address */
292static inline struct net_linkaddr *net_pkt_lladdr_if(struct net_pkt *pkt)
293{
294 return net_if_get_link_addr(pkt->iface);
295}
296
297static inline struct net_context *net_pkt_context(struct net_pkt *pkt)
298{
299 return pkt->context;
300}
301
302static inline void net_pkt_set_context(struct net_pkt *pkt,
303 struct net_context *ctx)
304{
305 pkt->context = ctx;
306}
307
308static inline struct net_if *net_pkt_iface(struct net_pkt *pkt)
309{
310 return pkt->iface;
311}
312
313static inline void net_pkt_set_iface(struct net_pkt *pkt, struct net_if *iface)
314{
315 pkt->iface = iface;
316
317 /* If the network interface is set in pkt, then also set the type of
318 * the network address that is stored in pkt. This is done here so
319 * that the address type is properly set and is not forgotten.
320 */
321 if (iface) {
322 pkt->lladdr_src.type = net_if_get_link_addr(iface)->type;
323 pkt->lladdr_dst.type = net_if_get_link_addr(iface)->type;
324 }
325}
326
327static inline struct net_if *net_pkt_orig_iface(struct net_pkt *pkt)
328{
329#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
330 return pkt->orig_iface;
331#else
332 return pkt->iface;
333#endif
334}
335
336static inline void net_pkt_set_orig_iface(struct net_pkt *pkt,
337 struct net_if *iface)
338{
339#if defined(CONFIG_NET_ROUTING) || defined(CONFIG_NET_ETHERNET_BRIDGE)
340 pkt->orig_iface = iface;
341#endif
342}
343
344static inline uint8_t net_pkt_family(struct net_pkt *pkt)
345{
346 return pkt->family;
347}
348
349static inline void net_pkt_set_family(struct net_pkt *pkt, uint8_t family)
350{
351 pkt->family = family;
352}
353
354static inline bool net_pkt_is_ptp(struct net_pkt *pkt)
355{
356 return !!(pkt->ptp_pkt);
357}
358
359static inline void net_pkt_set_ptp(struct net_pkt *pkt, bool is_ptp)
360{
361 pkt->ptp_pkt = is_ptp;
362}
363
364static inline bool net_pkt_is_captured(struct net_pkt *pkt)
365{
366 return !!(pkt->captured);
367}
368
369static inline void net_pkt_set_captured(struct net_pkt *pkt, bool is_captured)
370{
371 pkt->captured = is_captured;
372}
373
374static inline bool net_pkt_is_l2_bridged(struct net_pkt *pkt)
375{
376 return IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) ? !!(pkt->l2_bridged) : 0;
377}
378
379static inline void net_pkt_set_l2_bridged(struct net_pkt *pkt, bool is_l2_bridged)
380{
381 if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE)) {
382 pkt->l2_bridged = is_l2_bridged;
383 }
384}
385
386static inline bool net_pkt_is_l2_processed(struct net_pkt *pkt)
387{
388 return !!(pkt->l2_processed);
389}
390
391static inline void net_pkt_set_l2_processed(struct net_pkt *pkt,
392 bool is_l2_processed)
393{
394 pkt->l2_processed = is_l2_processed;
395}
396
397static inline uint8_t net_pkt_ip_hdr_len(struct net_pkt *pkt)
398{
399#if defined(CONFIG_NET_IP)
400 return pkt->ip_hdr_len;
401#else
402 return 0;
403#endif
404}
405
406static inline void net_pkt_set_ip_hdr_len(struct net_pkt *pkt, uint8_t len)
407{
408#if defined(CONFIG_NET_IP)
409 pkt->ip_hdr_len = len;
410#endif
411}
412
413static inline uint8_t net_pkt_sent(struct net_pkt *pkt)
414{
415 return pkt->sent_or_eof;
416}
417
418static inline void net_pkt_set_sent(struct net_pkt *pkt, bool sent)
419{
420 pkt->sent_or_eof = sent;
421}
422
423static inline uint8_t net_pkt_queued(struct net_pkt *pkt)
424{
425 return pkt->pkt_queued;
426}
427
428static inline void net_pkt_set_queued(struct net_pkt *pkt, bool send)
429{
430 pkt->pkt_queued = send;
431}
432
433static inline uint8_t net_pkt_tcp_1st_msg(struct net_pkt *pkt)
434{
435#if defined(CONFIG_NET_TCP)
436 return pkt->tcp_first_msg;
437#else
438 return true;
439#endif
440}
441
442static inline void net_pkt_set_tcp_1st_msg(struct net_pkt *pkt, bool is_1st)
443{
444#if defined(CONFIG_NET_TCP)
445 pkt->tcp_first_msg = is_1st;
446#else
447 ARG_UNUSED(pkt);
448 ARG_UNUSED(is_1st);
449#endif
450}
451
452#if defined(CONFIG_NET_SOCKETS)
453static inline uint8_t net_pkt_eof(struct net_pkt *pkt)
454{
455 return pkt->sent_or_eof;
456}
457
458static inline void net_pkt_set_eof(struct net_pkt *pkt, bool eof)
459{
460 pkt->sent_or_eof = eof;
461}
462#endif
463
464#if defined(CONFIG_NET_ROUTE)
465static inline bool net_pkt_forwarding(struct net_pkt *pkt)
466{
467 return pkt->forwarding;
468}
469
470static inline void net_pkt_set_forwarding(struct net_pkt *pkt, bool forward)
471{
472 pkt->forwarding = forward;
473}
474#else
475static inline bool net_pkt_forwarding(struct net_pkt *pkt)
476{
477 return false;
478}
479#endif
480
481#if defined(CONFIG_NET_IPV4)
482static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
483{
484 return pkt->ipv4_ttl;
485}
486
487static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
488 uint8_t ttl)
489{
490 pkt->ipv4_ttl = ttl;
491}
492
493static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
494{
495 return pkt->ipv4_opts_len;
496}
497
498static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
499 uint8_t opts_len)
500{
501 pkt->ipv4_opts_len = opts_len;
502}
503#else
504static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
505{
506 ARG_UNUSED(pkt);
507
508 return 0;
509}
510
511static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
512 uint8_t ttl)
513{
514 ARG_UNUSED(pkt);
515 ARG_UNUSED(ttl);
516}
517
518static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
519{
520 ARG_UNUSED(pkt);
521 return 0;
522}
523
524static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
525 uint8_t opts_len)
526{
527 ARG_UNUSED(pkt);
528 ARG_UNUSED(opts_len);
529}
530#endif
531
532#if defined(CONFIG_NET_IPV6)
533static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
534{
535 return pkt->ipv6_ext_opt_len;
536}
537
538static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
539 uint8_t len)
540{
541 pkt->ipv6_ext_opt_len = len;
542}
543
544static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
545{
546 return pkt->ipv6_next_hdr;
547}
548
549static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
550 uint8_t next_hdr)
551{
552 pkt->ipv6_next_hdr = next_hdr;
553}
554
555static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
556{
557 return pkt->ipv6_ext_len;
558}
559
560static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
561{
562 pkt->ipv6_ext_len = len;
563}
564
565static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
566{
567 return pkt->ipv6_prev_hdr_start;
568}
569
570static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
571 uint16_t offset)
572{
573 pkt->ipv6_prev_hdr_start = offset;
574}
575
576static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
577{
578 return pkt->ipv6_hop_limit;
579}
580
581static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
582 uint8_t hop_limit)
583{
584 pkt->ipv6_hop_limit = hop_limit;
585}
586#else /* CONFIG_NET_IPV6 */
587static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
588{
589 ARG_UNUSED(pkt);
590
591 return 0;
592}
593
594static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
595 uint8_t len)
596{
597 ARG_UNUSED(pkt);
598 ARG_UNUSED(len);
599}
600
601static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
602{
603 ARG_UNUSED(pkt);
604
605 return 0;
606}
607
608static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
609 uint8_t next_hdr)
610{
611 ARG_UNUSED(pkt);
612 ARG_UNUSED(next_hdr);
613}
614
615static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
616{
617 ARG_UNUSED(pkt);
618
619 return 0;
620}
621
622static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
623{
624 ARG_UNUSED(pkt);
625 ARG_UNUSED(len);
626}
627
628static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
629{
630 ARG_UNUSED(pkt);
631
632 return 0;
633}
634
635static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
636 uint16_t offset)
637{
638 ARG_UNUSED(pkt);
639 ARG_UNUSED(offset);
640}
641
642static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
643{
644 ARG_UNUSED(pkt);
645
646 return 0;
647}
648
649static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
650 uint8_t hop_limit)
651{
652 ARG_UNUSED(pkt);
653 ARG_UNUSED(hop_limit);
654}
655#endif /* CONFIG_NET_IPV6 */
656
657static inline uint16_t net_pkt_ip_opts_len(struct net_pkt *pkt)
658{
659#if defined(CONFIG_NET_IPV6)
660 return pkt->ipv6_ext_len;
661#elif defined(CONFIG_NET_IPV4)
662 return pkt->ipv4_opts_len;
663#else
664 ARG_UNUSED(pkt);
665
666 return 0;
667#endif
668}
669
670#if defined(CONFIG_NET_IPV6_FRAGMENT)
671static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
672{
673 return pkt->ipv6_frag_hdr_start;
674}
675
676static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
677 uint16_t start)
678{
679 pkt->ipv6_frag_hdr_start = start;
680}
681
682static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
683{
684 return pkt->ipv6_fragment_flags & NET_IPV6_FRAGH_OFFSET_MASK;
685}
686static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
687{
688 return (pkt->ipv6_fragment_flags & 0x01) != 0;
689}
690
691static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
693{
694 pkt->ipv6_fragment_flags = flags;
695}
696
697static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
698{
699 return pkt->ipv6_fragment_id;
700}
701
702static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
703 uint32_t id)
704{
705 pkt->ipv6_fragment_id = id;
706}
707#else /* CONFIG_NET_IPV6_FRAGMENT */
708static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
709{
710 ARG_UNUSED(pkt);
711
712 return 0;
713}
714
715static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
716 uint16_t start)
717{
718 ARG_UNUSED(pkt);
719 ARG_UNUSED(start);
720}
721
722static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
723{
724 ARG_UNUSED(pkt);
725
726 return 0;
727}
728
729static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
730{
731 ARG_UNUSED(pkt);
732
733 return 0;
734}
735
736static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
738{
739 ARG_UNUSED(pkt);
740 ARG_UNUSED(flags);
741}
742
743static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
744{
745 ARG_UNUSED(pkt);
746
747 return 0;
748}
749
750static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
751 uint32_t id)
752{
753 ARG_UNUSED(pkt);
754 ARG_UNUSED(id);
755}
756#endif /* CONFIG_NET_IPV6_FRAGMENT */
757
758static inline uint8_t net_pkt_priority(struct net_pkt *pkt)
759{
760 return pkt->priority;
761}
762
763static inline void net_pkt_set_priority(struct net_pkt *pkt,
764 uint8_t priority)
765{
766 pkt->priority = priority;
767}
768
769#if defined(CONFIG_NET_VLAN)
770static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
771{
772 return net_eth_vlan_get_vid(pkt->vlan_tci);
773}
774
775static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
776{
777 pkt->vlan_tci = net_eth_vlan_set_vid(pkt->vlan_tci, tag);
778}
779
780static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
781{
782 return net_eth_vlan_get_pcp(pkt->vlan_tci);
783}
784
785static inline void net_pkt_set_vlan_priority(struct net_pkt *pkt,
786 uint8_t priority)
787{
788 pkt->vlan_tci = net_eth_vlan_set_pcp(pkt->vlan_tci, priority);
789}
790
791static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
792{
793 return net_eth_vlan_get_dei(pkt->vlan_tci);
794}
795
796static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
797{
798 pkt->vlan_tci = net_eth_vlan_set_dei(pkt->vlan_tci, dei);
799}
800
801static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
802{
803 pkt->vlan_tci = tci;
804}
805
806static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
807{
808 return pkt->vlan_tci;
809}
810#else
811static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
812{
813 return NET_VLAN_TAG_UNSPEC;
814}
815
816static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
817{
818 ARG_UNUSED(pkt);
819 ARG_UNUSED(tag);
820}
821
822static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
823{
824 ARG_UNUSED(pkt);
825 return 0;
826}
827
828static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
829{
830 return false;
831}
832
833static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
834{
835 ARG_UNUSED(pkt);
836 ARG_UNUSED(dei);
837}
838
839static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
840{
841 return NET_VLAN_TAG_UNSPEC; /* assumes priority is 0 */
842}
843
844static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
845{
846 ARG_UNUSED(pkt);
847 ARG_UNUSED(tci);
848}
849#endif
850
851#if defined(CONFIG_NET_PKT_TIMESTAMP)
852static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
853{
854 return &pkt->timestamp;
855}
856
857static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
858 struct net_ptp_time *timestamp)
859{
860 pkt->timestamp.second = timestamp->second;
861 pkt->timestamp.nanosecond = timestamp->nanosecond;
862}
863#else
864static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
865{
866 ARG_UNUSED(pkt);
867
868 return NULL;
869}
870
871static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
872 struct net_ptp_time *timestamp)
873{
874 ARG_UNUSED(pkt);
875 ARG_UNUSED(timestamp);
876}
877#endif /* CONFIG_NET_PKT_TIMESTAMP */
878
879#if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
880static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
881{
882 return pkt->create_time;
883}
884
885static inline void net_pkt_set_create_time(struct net_pkt *pkt,
886 uint32_t create_time)
887{
888 pkt->create_time = create_time;
889}
890#else
891static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
892{
893 ARG_UNUSED(pkt);
894
895 return 0U;
896}
897
898static inline void net_pkt_set_create_time(struct net_pkt *pkt,
899 uint32_t create_time)
900{
901 ARG_UNUSED(pkt);
902 ARG_UNUSED(create_time);
903}
904#endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
905
906#if defined(CONFIG_NET_PKT_TXTIME)
907static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
908{
909 return pkt->txtime;
910}
911
912static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
913{
914 pkt->txtime = txtime;
915}
916#else
917static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
918{
919 ARG_UNUSED(pkt);
920
921 return 0;
922}
923
924static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
925{
926 ARG_UNUSED(pkt);
927 ARG_UNUSED(txtime);
928}
929#endif /* CONFIG_NET_PKT_TXTIME */
930
931#if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
932 defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
933static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
934{
935 return pkt->detail.stat;
936}
937
938static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
939{
940 return pkt->detail.count;
941}
942
943static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
944{
945 memset(&pkt->detail, 0, sizeof(pkt->detail));
946}
947
948static ALWAYS_INLINE void net_pkt_set_stats_tick(struct net_pkt *pkt,
949 uint32_t tick)
950{
951 if (pkt->detail.count >= NET_PKT_DETAIL_STATS_COUNT) {
952 NET_ERR("Detail stats count overflow (%d >= %d)",
953 pkt->detail.count, NET_PKT_DETAIL_STATS_COUNT);
954 return;
955 }
956
957 pkt->detail.stat[pkt->detail.count++] = tick;
958}
959
960#define net_pkt_set_tx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
961#define net_pkt_set_rx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
962#else
963static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
964{
965 ARG_UNUSED(pkt);
966
967 return NULL;
968}
969
970static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
971{
972 ARG_UNUSED(pkt);
973
974 return 0;
975}
976
977static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
978{
979 ARG_UNUSED(pkt);
980}
981
982static inline void net_pkt_set_stats_tick(struct net_pkt *pkt, uint32_t tick)
983{
984 ARG_UNUSED(pkt);
985 ARG_UNUSED(tick);
986}
987
988#define net_pkt_set_tx_stats_tick(pkt, tick)
989#define net_pkt_set_rx_stats_tick(pkt, tick)
990#endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
991 CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
992
993static inline size_t net_pkt_get_len(struct net_pkt *pkt)
994{
995 return net_buf_frags_len(pkt->frags);
996}
997
998static inline uint8_t *net_pkt_data(struct net_pkt *pkt)
999{
1000 return pkt->frags->data;
1001}
1002
1003static inline uint8_t *net_pkt_ip_data(struct net_pkt *pkt)
1004{
1005 return pkt->frags->data;
1006}
1007
1008static inline bool net_pkt_is_empty(struct net_pkt *pkt)
1009{
1010 return !pkt->buffer || !net_pkt_data(pkt) || pkt->buffer->len == 0;
1011}
1012
1013static inline struct net_linkaddr *net_pkt_lladdr_src(struct net_pkt *pkt)
1014{
1015 return &pkt->lladdr_src;
1016}
1017
1018static inline struct net_linkaddr *net_pkt_lladdr_dst(struct net_pkt *pkt)
1019{
1020 return &pkt->lladdr_dst;
1021}
1022
1023static inline void net_pkt_lladdr_swap(struct net_pkt *pkt)
1024{
1025 uint8_t *addr = net_pkt_lladdr_src(pkt)->addr;
1026
1027 net_pkt_lladdr_src(pkt)->addr = net_pkt_lladdr_dst(pkt)->addr;
1028 net_pkt_lladdr_dst(pkt)->addr = addr;
1029}
1030
1031static inline void net_pkt_lladdr_clear(struct net_pkt *pkt)
1032{
1033 net_pkt_lladdr_src(pkt)->addr = NULL;
1034 net_pkt_lladdr_src(pkt)->len = 0U;
1035}
1036
1037static inline uint16_t net_pkt_ll_proto_type(struct net_pkt *pkt)
1038{
1039 return pkt->ll_proto_type;
1040}
1041
1042static inline void net_pkt_set_ll_proto_type(struct net_pkt *pkt, uint16_t type)
1043{
1044 pkt->ll_proto_type = type;
1045}
1046
1047#if defined(CONFIG_IEEE802154) || defined(CONFIG_IEEE802154_RAW_MODE)
1048static inline uint8_t net_pkt_ieee802154_rssi(struct net_pkt *pkt)
1049{
1050 return pkt->ieee802154_rssi;
1051}
1052
1053static inline void net_pkt_set_ieee802154_rssi(struct net_pkt *pkt,
1054 uint8_t rssi)
1055{
1056 pkt->ieee802154_rssi = rssi;
1057}
1058
1059static inline uint8_t net_pkt_ieee802154_lqi(struct net_pkt *pkt)
1060{
1061 return pkt->ieee802154_lqi;
1062}
1063
1064static inline void net_pkt_set_ieee802154_lqi(struct net_pkt *pkt,
1065 uint8_t lqi)
1066{
1067 pkt->ieee802154_lqi = lqi;
1068}
1069
1070static inline bool net_pkt_ieee802154_arb(struct net_pkt *pkt)
1071{
1072 return pkt->ieee802154_arb;
1073}
1074
1075static inline void net_pkt_set_ieee802154_arb(struct net_pkt *pkt, bool arb)
1076{
1077 pkt->ieee802154_arb = arb;
1078}
1079
1080static inline bool net_pkt_ieee802154_ack_fpb(struct net_pkt *pkt)
1081{
1082 return pkt->ieee802154_ack_fpb;
1083}
1084
1085static inline void net_pkt_set_ieee802154_ack_fpb(struct net_pkt *pkt,
1086 bool fpb)
1087{
1088 pkt->ieee802154_ack_fpb = fpb;
1089}
1090
1091static inline bool net_pkt_ieee802154_frame_secured(struct net_pkt *pkt)
1092{
1093 return pkt->ieee802154_frame_secured;
1094}
1095
1096static inline void net_pkt_set_ieee802154_frame_secured(struct net_pkt *pkt,
1097 bool secured)
1098{
1099 pkt->ieee802154_frame_secured = secured;
1100}
1101
1102static inline bool net_pkt_ieee802154_mac_hdr_rdy(struct net_pkt *pkt)
1103{
1104 return pkt->ieee802154_mac_hdr_rdy;
1105}
1106
1107static inline void net_pkt_set_ieee802154_mac_hdr_rdy(struct net_pkt *pkt,
1108 bool rdy)
1109{
1110 pkt->ieee802154_mac_hdr_rdy = rdy;
1111}
1112
1113#if defined(CONFIG_IEEE802154_2015)
1114static inline bool net_pkt_ieee802154_fv2015(struct net_pkt *pkt)
1115{
1116 return pkt->ieee802154_fv2015;
1117}
1118
1119static inline void net_pkt_set_ieee802154_fv2015(struct net_pkt *pkt, bool fv2015)
1120{
1121 pkt->ieee802154_fv2015 = fv2015;
1122}
1123
1124static inline bool net_pkt_ieee802154_ack_seb(struct net_pkt *pkt)
1125{
1126 return pkt->ieee802154_ack_seb;
1127}
1128
1129static inline void net_pkt_set_ieee802154_ack_seb(struct net_pkt *pkt, bool seb)
1130{
1131 pkt->ieee802154_ack_seb = seb;
1132}
1133
1134static inline uint32_t net_pkt_ieee802154_ack_fc(struct net_pkt *pkt)
1135{
1136 return pkt->ieee802154_ack_fc;
1137}
1138
1139static inline void net_pkt_set_ieee802154_ack_fc(struct net_pkt *pkt,
1140 uint32_t fc)
1141{
1142 pkt->ieee802154_ack_fc = fc;
1143}
1144
1145static inline uint8_t net_pkt_ieee802154_ack_keyid(struct net_pkt *pkt)
1146{
1147 return pkt->ieee802154_ack_keyid;
1148}
1149
1150static inline void net_pkt_set_ieee802154_ack_keyid(struct net_pkt *pkt,
1151 uint8_t keyid)
1152{
1153 pkt->ieee802154_ack_keyid = keyid;
1154}
1155#endif /* CONFIG_IEEE802154_2015 */
1156
1157#if defined(CONFIG_IEEE802154_SELECTIVE_TXPOWER)
1158static inline int8_t net_pkt_ieee802154_txpwr(struct net_pkt *pkt)
1159{
1160 return pkt->ieee802154_txpwr;
1161}
1162
1163static inline void net_pkt_set_ieee802154_txpwr(struct net_pkt *pkt,
1164 int8_t txpwr)
1165{
1166 pkt->ieee802154_txpwr = txpwr;
1167}
1168#endif /* CONFIG_IEEE802154_SELECTIVE_TXPOWER */
1169#endif /* CONFIG_IEEE802154 || CONFIG_IEEE802154_RAW_MODE */
1170
1171#if defined(CONFIG_NET_IPV4_AUTO)
1172static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1173{
1174 return pkt->ipv4_auto_arp_msg;
1175}
1176
1177static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1178 bool is_auto_arp_msg)
1179{
1180 pkt->ipv4_auto_arp_msg = is_auto_arp_msg;
1181}
1182#else /* CONFIG_NET_IPV4_AUTO */
1183static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
1184{
1185 ARG_UNUSED(pkt);
1186
1187 return false;
1188}
1189
1190static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
1191 bool is_auto_arp_msg)
1192{
1193 ARG_UNUSED(pkt);
1194 ARG_UNUSED(is_auto_arp_msg);
1195}
1196#endif /* CONFIG_NET_IPV4_AUTO */
1197
1198#if defined(CONFIG_NET_LLDP)
1199static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1200{
1201 return pkt->lldp_pkt;
1202}
1203
1204static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1205{
1206 pkt->lldp_pkt = is_lldp;
1207}
1208#else
1209static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
1210{
1211 ARG_UNUSED(pkt);
1212
1213 return false;
1214}
1215
1216static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
1217{
1218 ARG_UNUSED(pkt);
1219 ARG_UNUSED(is_lldp);
1220}
1221#endif /* CONFIG_NET_LLDP */
1222
1223#if defined(CONFIG_NET_PPP)
1224static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1225{
1226 return pkt->ppp_msg;
1227}
1228
1229static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1230 bool is_ppp_msg)
1231{
1232 pkt->ppp_msg = is_ppp_msg;
1233}
1234#else /* CONFIG_NET_PPP */
1235static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
1236{
1237 ARG_UNUSED(pkt);
1238
1239 return false;
1240}
1241
1242static inline void net_pkt_set_ppp(struct net_pkt *pkt,
1243 bool is_ppp_msg)
1244{
1245 ARG_UNUSED(pkt);
1246 ARG_UNUSED(is_ppp_msg);
1247}
1248#endif /* CONFIG_NET_PPP */
1249
1250#define NET_IPV6_HDR(pkt) ((struct net_ipv6_hdr *)net_pkt_ip_data(pkt))
1251#define NET_IPV4_HDR(pkt) ((struct net_ipv4_hdr *)net_pkt_ip_data(pkt))
1252
1253static inline void net_pkt_set_src_ipv6_addr(struct net_pkt *pkt)
1254{
1256 net_pkt_context(pkt)),
1257 (struct in6_addr *)NET_IPV6_HDR(pkt)->src);
1258}
1259
1260static inline void net_pkt_set_overwrite(struct net_pkt *pkt, bool overwrite)
1261{
1262 pkt->overwrite = overwrite;
1263}
1264
1265static inline bool net_pkt_is_being_overwritten(struct net_pkt *pkt)
1266{
1267 return pkt->overwrite;
1268}
1269
1270#ifdef CONFIG_NET_PKT_FILTER
1271
1272bool net_pkt_filter_send_ok(struct net_pkt *pkt);
1273bool net_pkt_filter_recv_ok(struct net_pkt *pkt);
1274
1275#else
1276
1277static inline bool net_pkt_filter_send_ok(struct net_pkt *pkt)
1278{
1279 ARG_UNUSED(pkt);
1280
1281 return true;
1282}
1283
1284static inline bool net_pkt_filter_recv_ok(struct net_pkt *pkt)
1285{
1286 ARG_UNUSED(pkt);
1287
1288 return true;
1289}
1290
1291#endif /* CONFIG_NET_PKT_FILTER */
1292
1293/* @endcond */
1294
1308#define NET_PKT_SLAB_DEFINE(name, count) \
1309 K_MEM_SLAB_DEFINE(name, sizeof(struct net_pkt), count, 4)
1310
1311/* Backward compatibility macro */
1312#define NET_PKT_TX_SLAB_DEFINE(name, count) NET_PKT_SLAB_DEFINE(name, count)
1313
1327#define NET_PKT_DATA_POOL_DEFINE(name, count) \
1328 NET_BUF_POOL_DEFINE(name, count, CONFIG_NET_BUF_DATA_SIZE, \
1329 0, NULL)
1330
1333#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC) || \
1334 (CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG)
1335#define NET_PKT_DEBUG_ENABLED
1336#endif
1337
1338#if defined(NET_PKT_DEBUG_ENABLED)
1339
1340/* Debug versions of the net_pkt functions that are used when tracking
1341 * buffer usage.
1342 */
1343
1344struct net_buf *net_pkt_get_reserve_data_debug(struct net_buf_pool *pool,
1346 const char *caller,
1347 int line);
1348
1349#define net_pkt_get_reserve_data(pool, timeout) \
1350 net_pkt_get_reserve_data_debug(pool, timeout, __func__, __LINE__)
1351
1352struct net_buf *net_pkt_get_reserve_rx_data_debug(k_timeout_t timeout,
1353 const char *caller,
1354 int line);
1355#define net_pkt_get_reserve_rx_data(timeout) \
1356 net_pkt_get_reserve_rx_data_debug(timeout, __func__, __LINE__)
1357
1358struct net_buf *net_pkt_get_reserve_tx_data_debug(k_timeout_t timeout,
1359 const char *caller,
1360 int line);
1361#define net_pkt_get_reserve_tx_data(timeout) \
1362 net_pkt_get_reserve_tx_data_debug(timeout, __func__, __LINE__)
1363
1364struct net_buf *net_pkt_get_frag_debug(struct net_pkt *pkt,
1366 const char *caller, int line);
1367#define net_pkt_get_frag(pkt, timeout) \
1368 net_pkt_get_frag_debug(pkt, timeout, __func__, __LINE__)
1369
1370void net_pkt_unref_debug(struct net_pkt *pkt, const char *caller, int line);
1371#define net_pkt_unref(pkt) net_pkt_unref_debug(pkt, __func__, __LINE__)
1372
1373struct net_pkt *net_pkt_ref_debug(struct net_pkt *pkt, const char *caller,
1374 int line);
1375#define net_pkt_ref(pkt) net_pkt_ref_debug(pkt, __func__, __LINE__)
1376
1377struct net_buf *net_pkt_frag_ref_debug(struct net_buf *frag,
1378 const char *caller, int line);
1379#define net_pkt_frag_ref(frag) net_pkt_frag_ref_debug(frag, __func__, __LINE__)
1380
1381void net_pkt_frag_unref_debug(struct net_buf *frag,
1382 const char *caller, int line);
1383#define net_pkt_frag_unref(frag) \
1384 net_pkt_frag_unref_debug(frag, __func__, __LINE__)
1385
1386struct net_buf *net_pkt_frag_del_debug(struct net_pkt *pkt,
1387 struct net_buf *parent,
1388 struct net_buf *frag,
1389 const char *caller, int line);
1390#define net_pkt_frag_del(pkt, parent, frag) \
1391 net_pkt_frag_del_debug(pkt, parent, frag, __func__, __LINE__)
1392
1393void net_pkt_frag_add_debug(struct net_pkt *pkt, struct net_buf *frag,
1394 const char *caller, int line);
1395#define net_pkt_frag_add(pkt, frag) \
1396 net_pkt_frag_add_debug(pkt, frag, __func__, __LINE__)
1397
1398void net_pkt_frag_insert_debug(struct net_pkt *pkt, struct net_buf *frag,
1399 const char *caller, int line);
1400#define net_pkt_frag_insert(pkt, frag) \
1401 net_pkt_frag_insert_debug(pkt, frag, __func__, __LINE__)
1402#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC ||
1403 * CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG
1404 */
1414#if defined(NET_PKT_DEBUG_ENABLED)
1415void net_pkt_print_frags(struct net_pkt *pkt);
1416#else
1417#define net_pkt_print_frags(pkt)
1418#endif
1419
1433#if !defined(NET_PKT_DEBUG_ENABLED)
1435#endif
1436
1450#if !defined(NET_PKT_DEBUG_ENABLED)
1452#endif
1453
1465#if !defined(NET_PKT_DEBUG_ENABLED)
1467#endif
1468
1478#if !defined(NET_PKT_DEBUG_ENABLED)
1479void net_pkt_unref(struct net_pkt *pkt);
1480#endif
1481
1491#if !defined(NET_PKT_DEBUG_ENABLED)
1492struct net_pkt *net_pkt_ref(struct net_pkt *pkt);
1493#endif
1494
1504#if !defined(NET_PKT_DEBUG_ENABLED)
1505struct net_buf *net_pkt_frag_ref(struct net_buf *frag);
1506#endif
1507
1513#if !defined(NET_PKT_DEBUG_ENABLED)
1514void net_pkt_frag_unref(struct net_buf *frag);
1515#endif
1516
1527#if !defined(NET_PKT_DEBUG_ENABLED)
1529 struct net_buf *parent,
1530 struct net_buf *frag);
1531#endif
1532
1539#if !defined(NET_PKT_DEBUG_ENABLED)
1540void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag);
1541#endif
1542
1549#if !defined(NET_PKT_DEBUG_ENABLED)
1550void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag);
1551#endif
1552
1559void net_pkt_compact(struct net_pkt *pkt);
1560
1569void net_pkt_get_info(struct k_mem_slab **rx,
1570 struct k_mem_slab **tx,
1571 struct net_buf_pool **rx_data,
1572 struct net_buf_pool **tx_data);
1573
1576#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
1580void net_pkt_print(void);
1581
1582typedef void (*net_pkt_allocs_cb_t)(struct net_pkt *pkt,
1583 struct net_buf *buf,
1584 const char *func_alloc,
1585 int line_alloc,
1586 const char *func_free,
1587 int line_free,
1588 bool in_use,
1589 void *user_data);
1590
1591void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data);
1592
1593const char *net_pkt_slab2str(struct k_mem_slab *slab);
1594const char *net_pkt_pool2str(struct net_buf_pool *pool);
1595
1596#else
1597#define net_pkt_print(...)
1598#endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
1599
1600/* New allocator, and API are defined below.
1601 * This will be simpler when time will come to get rid of former API above.
1602 */
1603#if defined(NET_PKT_DEBUG_ENABLED)
1604
1605struct net_pkt *net_pkt_alloc_debug(k_timeout_t timeout,
1606 const char *caller, int line);
1607#define net_pkt_alloc(_timeout) \
1608 net_pkt_alloc_debug(_timeout, __func__, __LINE__)
1609
1610struct net_pkt *net_pkt_alloc_from_slab_debug(struct k_mem_slab *slab,
1612 const char *caller, int line);
1613#define net_pkt_alloc_from_slab(_slab, _timeout) \
1614 net_pkt_alloc_from_slab_debug(_slab, _timeout, __func__, __LINE__)
1615
1616struct net_pkt *net_pkt_rx_alloc_debug(k_timeout_t timeout,
1617 const char *caller, int line);
1618#define net_pkt_rx_alloc(_timeout) \
1619 net_pkt_rx_alloc_debug(_timeout, __func__, __LINE__)
1620
1621struct net_pkt *net_pkt_alloc_on_iface_debug(struct net_if *iface,
1623 const char *caller,
1624 int line);
1625#define net_pkt_alloc_on_iface(_iface, _timeout) \
1626 net_pkt_alloc_on_iface_debug(_iface, _timeout, __func__, __LINE__)
1627
1628struct net_pkt *net_pkt_rx_alloc_on_iface_debug(struct net_if *iface,
1630 const char *caller,
1631 int line);
1632#define net_pkt_rx_alloc_on_iface(_iface, _timeout) \
1633 net_pkt_rx_alloc_on_iface_debug(_iface, _timeout, \
1634 __func__, __LINE__)
1635
1636int net_pkt_alloc_buffer_debug(struct net_pkt *pkt,
1637 size_t size,
1638 enum net_ip_protocol proto,
1640 const char *caller, int line);
1641#define net_pkt_alloc_buffer(_pkt, _size, _proto, _timeout) \
1642 net_pkt_alloc_buffer_debug(_pkt, _size, _proto, _timeout, \
1643 __func__, __LINE__)
1644
1645struct net_pkt *net_pkt_alloc_with_buffer_debug(struct net_if *iface,
1646 size_t size,
1647 sa_family_t family,
1648 enum net_ip_protocol proto,
1650 const char *caller,
1651 int line);
1652#define net_pkt_alloc_with_buffer(_iface, _size, _family, \
1653 _proto, _timeout) \
1654 net_pkt_alloc_with_buffer_debug(_iface, _size, _family, \
1655 _proto, _timeout, \
1656 __func__, __LINE__)
1657
1658struct net_pkt *net_pkt_rx_alloc_with_buffer_debug(struct net_if *iface,
1659 size_t size,
1660 sa_family_t family,
1661 enum net_ip_protocol proto,
1663 const char *caller,
1664 int line);
1665#define net_pkt_rx_alloc_with_buffer(_iface, _size, _family, \
1666 _proto, _timeout) \
1667 net_pkt_rx_alloc_with_buffer_debug(_iface, _size, _family, \
1668 _proto, _timeout, \
1669 __func__, __LINE__)
1670#endif /* NET_PKT_DEBUG_ENABLED */
1683#if !defined(NET_PKT_DEBUG_ENABLED)
1685#endif
1686
1701#if !defined(NET_PKT_DEBUG_ENABLED)
1702struct net_pkt *net_pkt_alloc_from_slab(struct k_mem_slab *slab,
1704#endif
1705
1716#if !defined(NET_PKT_DEBUG_ENABLED)
1718#endif
1719
1728#if !defined(NET_PKT_DEBUG_ENABLED)
1731
1732/* Same as above but specifically for RX packet */
1735#endif
1736
1752#if !defined(NET_PKT_DEBUG_ENABLED)
1754 size_t size,
1755 enum net_ip_protocol proto,
1757#endif
1758
1770#if !defined(NET_PKT_DEBUG_ENABLED)
1772 size_t size,
1773 sa_family_t family,
1774 enum net_ip_protocol proto,
1776
1777/* Same as above but specifically for RX packet */
1779 size_t size,
1780 sa_family_t family,
1781 enum net_ip_protocol proto,
1783#endif
1784
1791void net_pkt_append_buffer(struct net_pkt *pkt, struct net_buf *buffer);
1792
1804
1821 enum net_ip_protocol proto);
1822
1832
1847int net_pkt_remove_tail(struct net_pkt *pkt, size_t length);
1848
1857
1864static inline void net_pkt_cursor_backup(struct net_pkt *pkt,
1865 struct net_pkt_cursor *backup)
1866{
1867 backup->buf = pkt->cursor.buf;
1868 backup->pos = pkt->cursor.pos;
1869}
1870
1877static inline void net_pkt_cursor_restore(struct net_pkt *pkt,
1878 struct net_pkt_cursor *backup)
1879{
1880 pkt->cursor.buf = backup->buf;
1881 pkt->cursor.pos = backup->pos;
1882}
1883
1891static inline void *net_pkt_cursor_get_pos(struct net_pkt *pkt)
1892{
1893 return pkt->cursor.pos;
1894}
1895
1916int net_pkt_skip(struct net_pkt *pkt, size_t length);
1917
1932int net_pkt_memset(struct net_pkt *pkt, int byte, size_t length);
1933
1947int net_pkt_copy(struct net_pkt *pkt_dst,
1948 struct net_pkt *pkt_src,
1949 size_t length);
1950
1961
1972
1983
1997int net_pkt_read(struct net_pkt *pkt, void *data, size_t length);
1998
1999/* Read uint8_t data data a net_pkt */
2000static inline int net_pkt_read_u8(struct net_pkt *pkt, uint8_t *data)
2001{
2002 return net_pkt_read(pkt, data, 1);
2003}
2004
2018
2032
2046
2060int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length);
2061
2062/* Write uint8_t data into a net_pkt. */
2063static inline int net_pkt_write_u8(struct net_pkt *pkt, uint8_t data)
2064{
2065 return net_pkt_write(pkt, &data, sizeof(uint8_t));
2066}
2067
2068/* Write uint16_t big endian data into a net_pkt. */
2069static inline int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
2070{
2071 uint16_t data_be16 = htons(data);
2072
2073 return net_pkt_write(pkt, &data_be16, sizeof(uint16_t));
2074}
2075
2076/* Write uint32_t big endian data into a net_pkt. */
2077static inline int net_pkt_write_be32(struct net_pkt *pkt, uint32_t data)
2078{
2079 uint32_t data_be32 = htonl(data);
2080
2081 return net_pkt_write(pkt, &data_be32, sizeof(uint32_t));
2082}
2083
2084/* Write uint32_t little endian data into a net_pkt. */
2085static inline int net_pkt_write_le32(struct net_pkt *pkt, uint32_t data)
2086{
2087 uint32_t data_le32 = sys_cpu_to_le32(data);
2088
2089 return net_pkt_write(pkt, &data_le32, sizeof(uint32_t));
2090}
2091
2092/* Write uint16_t little endian data into a net_pkt. */
2093static inline int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
2094{
2095 uint16_t data_le16 = sys_cpu_to_le16(data);
2096
2097 return net_pkt_write(pkt, &data_le16, sizeof(uint16_t));
2098}
2099
2108
2121int net_pkt_update_length(struct net_pkt *pkt, size_t length);
2122
2135int net_pkt_pull(struct net_pkt *pkt, size_t length);
2136
2146
2158bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size);
2159
2169
2171#if !defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2172 void *data;
2173#endif
2174 const size_t size;
2175};
2176
2177#if defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
2178#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2179 struct net_pkt_data_access _name = { \
2180 .size = sizeof(_type), \
2181 }
2182
2183#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2184 NET_PKT_DATA_ACCESS_DEFINE(_name, _type)
2185
2186#else
2187#define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
2188 _type _hdr_##_name; \
2189 struct net_pkt_data_access _name = { \
2190 .data = &_hdr_##_name, \
2191 .size = sizeof(_type), \
2192 }
2193
2194#define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
2195 struct net_pkt_data_access _name = { \
2196 .data = NULL, \
2197 .size = sizeof(_type), \
2198 }
2199
2200#endif /* CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS */
2201
2215void *net_pkt_get_data(struct net_pkt *pkt,
2216 struct net_pkt_data_access *access);
2217
2232 struct net_pkt_data_access *access);
2233
2238static inline int net_pkt_acknowledge_data(struct net_pkt *pkt,
2239 struct net_pkt_data_access *access)
2240{
2241 return net_pkt_skip(pkt, access->size);
2242}
2243
2248#ifdef __cplusplus
2249}
2250#endif
2251
2252#endif /* ZEPHYR_INCLUDE_NET_NET_PKT_H_ */
long atomic_t
Definition: atomic.h:22
#define sys_cpu_to_le32(val)
Convert 32-bit integer from host endianness to little-endian.
Definition: byteorder.h:177
#define sys_cpu_to_le16(val)
Convert 16-bit integer from host endianness to little-endian.
Definition: byteorder.h:173
#define ALWAYS_INLINE
Definition: common.h:124
ZTEST_BMEM int timeout
Definition: main.c:31
ZTEST_BMEM int count
Definition: main.c:33
VLAN specific definitions.
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:2464
static struct net_if * net_context_get_iface(struct net_context *context)
Get network interface for this context.
Definition: net_context.h:640
static struct net_linkaddr * net_if_get_link_addr(struct net_if *iface)
Get an network interface's link address.
Definition: net_if.h:727
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:1490
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:2077
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:1417
int net_pkt_update_length(struct net_pkt *pkt, size_t length)
Update the overall length of a packet.
struct net_buf * net_pkt_get_reserve_rx_data(k_timeout_t timeout)
Get RX DATA buffer from pool. Normally you should use net_pkt_get_frag() instead.
int net_pkt_pull(struct net_pkt *pkt, size_t length)
Remove data from the packet at current location.
int net_pkt_copy(struct net_pkt *pkt_dst, struct net_pkt *pkt_src, size_t length)
Copy data from a packet into another one.
struct net_pkt * net_pkt_rx_alloc(k_timeout_t timeout)
Allocate an initialized net_pkt for RX.
struct net_pkt * net_pkt_ref(struct net_pkt *pkt)
Increase the packet ref count.
int net_pkt_read_be16(struct net_pkt *pkt, uint16_t *data)
Read uint16_t big endian data from a net_pkt.
struct net_pkt * net_pkt_alloc_with_buffer(struct net_if *iface, size_t size, sa_family_t family, enum net_ip_protocol proto, k_timeout_t timeout)
Allocate a network packet and buffer at once.
void net_pkt_frag_unref(struct net_buf *frag)
Decrease the packet fragment ref count.
struct net_pkt * net_pkt_rx_alloc_with_buffer(struct net_if *iface, size_t size, sa_family_t family, enum net_ip_protocol proto, k_timeout_t timeout)
struct net_pkt * net_pkt_rx_clone(struct net_pkt *pkt, k_timeout_t timeout)
Clone pkt and its buffer. The cloned packet will be allocated on the RX packet poll.
void net_pkt_trim_buffer(struct net_pkt *pkt)
Trim net_pkt buffer.
struct net_pkt * net_pkt_alloc_on_iface(struct net_if *iface, k_timeout_t timeout)
Allocate a network packet for a specific network interface.
void net_pkt_get_info(struct k_mem_slab **rx, struct k_mem_slab **tx, struct net_buf_pool **rx_data, struct net_buf_pool **tx_data)
Get information about predefined RX, TX and DATA pools.
void net_pkt_unref(struct net_pkt *pkt)
Place packet back into the available packets slab.
static int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:2069
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:2063
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:1891
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:1864
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:2238
static int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
Definition: net_pkt.h:2093
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:1877
uint16_t net_pkt_get_current_offset(struct net_pkt *pkt)
Get the actual offset in the packet from its cursor.
size_t net_pkt_remaining_data(struct net_pkt *pkt)
Get the amount of data which can be read from current cursor position.
int net_pkt_alloc_buffer(struct net_pkt *pkt, size_t size, enum net_ip_protocol proto, k_timeout_t timeout)
Allocate buffer for a net_pkt.
struct net_buf * net_pkt_get_frag(struct net_pkt *pkt, k_timeout_t timeout)
Get a data fragment that might be from user specific buffer pool or from global DATA pool.
int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length)
Write data into a net_pkt.
struct net_buf * net_pkt_frag_ref(struct net_buf *frag)
Increase the packet fragment ref count.
size_t net_pkt_available_buffer(struct net_pkt *pkt)
Get available buffer space from a pkt.
struct net_pkt * net_pkt_clone(struct net_pkt *pkt, k_timeout_t timeout)
Clone pkt and its buffer. The cloned packet will be allocated on the same pool as the original one.
struct net_pkt * net_pkt_alloc_from_slab(struct k_mem_slab *slab, k_timeout_t timeout)
Allocate an initialized net_pkt from a specific slab.
static int net_pkt_write_le32(struct net_pkt *pkt, uint32_t data)
Definition: net_pkt.h:2085
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:2000
size_t net_pkt_get_contiguous_len(struct net_pkt *pkt)
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:101
static uint16_t net_eth_vlan_set_vid(uint16_t tci, uint16_t vid)
Set VLAN identifier to TCI.
Definition: ethernet_vlan.h:76
static uint8_t net_eth_vlan_get_dei(uint16_t tci)
Get Drop Eligible Indicator from TCI.
Definition: ethernet_vlan.h:51
#define NET_VLAN_TAG_UNSPEC
Definition: ethernet_vlan.h:30
static uint16_t net_eth_vlan_set_dei(uint16_t tci, bool dei)
Set Drop Eligible Indicator to TCI.
Definition: ethernet_vlan.h:89
static uint16_t net_eth_vlan_get_vid(uint16_t tci)
Get VLAN identifier from TCI.
Definition: ethernet_vlan.h:39
static uint16_t net_eth_vlan_set_pcp(uint16_t tci, uint8_t pcp)
Set Priority Code Point to TCI.
Definition: ethernet_vlan.h:102
static uint8_t net_eth_vlan_get_pcp(uint16_t tci)
Get Priority Code Point from TCI.
Definition: ethernet_vlan.h:63
flags
Definition: http_parser.h:131
Buffer management.
Network context definitions.
Network core definitions.
Public API for network interface.
IPv6 and IPv4 definitions.
Public API for network link address.
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
__INT8_TYPE__ int8_t
Definition: stdint.h:72
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:981
Network buffer representation.
Definition: buf.h:914
uint8_t * data
Definition: buf.h:942
uint16_t len
Definition: buf.h:945
Definition: net_context.h:201
Network Interface structure.
Definition: net_if.h:480
Hardware link address structure.
Definition: net_linkaddr.h:67
uint8_t * addr
Definition: net_linkaddr.h:69
uint8_t type
Definition: net_linkaddr.h:75
Definition: net_pkt.h:46
uint8_t * pos
Definition: net_pkt.h:50
struct net_buf * buf
Definition: net_pkt.h:48
Definition: net_pkt.h:2170
const size_t size
Definition: net_pkt.h:2174
void * data
Definition: net_pkt.h:2172
Network packet.
Definition: net_pkt.h:59
struct net_buf * frags
Definition: net_pkt.h:71
struct net_context * context
Definition: net_pkt.h:79
struct net_pkt_cursor cursor
Definition: net_pkt.h:76
struct net_if * iface
Definition: net_pkt.h:82
intptr_t fifo
Definition: net_pkt.h:64
struct net_buf * buffer
Definition: net_pkt.h:72
struct k_mem_slab * slab
Definition: net_pkt.h:67
Precision Time Protocol Timestamp format.
Definition: ptp_time.h:39
uint32_t nanosecond
Definition: ptp_time.h:59
uint64_t second
Definition: ptp_time.h:55
Definition: stat.h:39
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static 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