1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions * Copyright (c) 1999-2024 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions *
4*43a90889SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions *
6*43a90889SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions *
15*43a90889SApple OSS Distributions * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions *
18*43a90889SApple OSS Distributions * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions * limitations under the License.
25*43a90889SApple OSS Distributions *
26*43a90889SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions */
28*43a90889SApple OSS Distributions /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
29*43a90889SApple OSS Distributions /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30*43a90889SApple OSS Distributions /*
31*43a90889SApple OSS Distributions * Mach Operating System
32*43a90889SApple OSS Distributions * Copyright (c) 1987 Carnegie-Mellon University
33*43a90889SApple OSS Distributions * All rights reserved. The CMU software License Agreement specifies
34*43a90889SApple OSS Distributions * the terms and conditions for use and redistribution.
35*43a90889SApple OSS Distributions */
36*43a90889SApple OSS Distributions /*
37*43a90889SApple OSS Distributions * Copyright (c) 1994 NeXT Computer, Inc. All rights reserved.
38*43a90889SApple OSS Distributions *
39*43a90889SApple OSS Distributions * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
40*43a90889SApple OSS Distributions * All rights reserved.
41*43a90889SApple OSS Distributions *
42*43a90889SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
43*43a90889SApple OSS Distributions * modification, are permitted provided that the following conditions
44*43a90889SApple OSS Distributions * are met:
45*43a90889SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
46*43a90889SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
47*43a90889SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
48*43a90889SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
49*43a90889SApple OSS Distributions * documentation and/or other materials provided with the distribution.
50*43a90889SApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
51*43a90889SApple OSS Distributions * must display the following acknowledgement:
52*43a90889SApple OSS Distributions * This product includes software developed by the University of
53*43a90889SApple OSS Distributions * California, Berkeley and its contributors.
54*43a90889SApple OSS Distributions * 4. Neither the name of the University nor the names of its contributors
55*43a90889SApple OSS Distributions * may be used to endorse or promote products derived from this software
56*43a90889SApple OSS Distributions * without specific prior written permission.
57*43a90889SApple OSS Distributions *
58*43a90889SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59*43a90889SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60*43a90889SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61*43a90889SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62*43a90889SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63*43a90889SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64*43a90889SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65*43a90889SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66*43a90889SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67*43a90889SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68*43a90889SApple OSS Distributions * SUCH DAMAGE.
69*43a90889SApple OSS Distributions *
70*43a90889SApple OSS Distributions * @(#)mbuf.h 8.3 (Berkeley) 1/21/94
71*43a90889SApple OSS Distributions */
72*43a90889SApple OSS Distributions /*
73*43a90889SApple OSS Distributions * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
74*43a90889SApple OSS Distributions * support for mandatory and extensible security protections. This notice
75*43a90889SApple OSS Distributions * is included in support of clause 2.2 (b) of the Apple Public License,
76*43a90889SApple OSS Distributions * Version 2.0.
77*43a90889SApple OSS Distributions */
78*43a90889SApple OSS Distributions
79*43a90889SApple OSS Distributions #ifndef _SYS_MBUF_H_
80*43a90889SApple OSS Distributions #define _SYS_MBUF_H_
81*43a90889SApple OSS Distributions
82*43a90889SApple OSS Distributions #include <sys/appleapiopts.h>
83*43a90889SApple OSS Distributions #include <sys/cdefs.h>
84*43a90889SApple OSS Distributions #include <sys/_types/_u_int32_t.h> /* u_int32_t */
85*43a90889SApple OSS Distributions #include <sys/_types/_u_int64_t.h> /* u_int64_t */
86*43a90889SApple OSS Distributions #include <sys/_types/_u_short.h> /* u_short */
87*43a90889SApple OSS Distributions
88*43a90889SApple OSS Distributions #ifdef KERNEL
89*43a90889SApple OSS Distributions #include <sys/kpi_mbuf.h>
90*43a90889SApple OSS Distributions #endif
91*43a90889SApple OSS Distributions
92*43a90889SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
93*43a90889SApple OSS Distributions #include <sys/lock.h>
94*43a90889SApple OSS Distributions #include <sys/queue.h>
95*43a90889SApple OSS Distributions #include <machine/endian.h>
96*43a90889SApple OSS Distributions /*
97*43a90889SApple OSS Distributions * Mbufs are of a single size, which includes overhead.
98*43a90889SApple OSS Distributions * An mbuf may add a single "mbuf cluster" of size
99*43a90889SApple OSS Distributions * MCLBYTES/MBIGCLBYTES/M16KCLBYTES (also in machine/param.h), which has
100*43a90889SApple OSS Distributions * no additional overhead and is used instead of the internal data area;
101*43a90889SApple OSS Distributions * this is done when at least MINCLSIZE of data must be stored.
102*43a90889SApple OSS Distributions */
103*43a90889SApple OSS Distributions #if CONFIG_MBUF_MCACHE
104*43a90889SApple OSS Distributions #define _MSIZESHIFT 8 /* 256 */
105*43a90889SApple OSS Distributions #define _MSIZE (1 << _MSIZESHIFT) /* size of an mbuf */
106*43a90889SApple OSS Distributions #else /* CONFIG_MBUF_MCACHE */
107*43a90889SApple OSS Distributions #define _MSIZE 512
108*43a90889SApple OSS Distributions #endif /* CONFIG_MBUF_MCACHE */
109*43a90889SApple OSS Distributions
110*43a90889SApple OSS Distributions #define NCLPGSHIFT (PAGE_SHIFT - MCLSHIFT)
111*43a90889SApple OSS Distributions #define NCLPG (1 << NCLPGSHIFT) /* # of cl per page */
112*43a90889SApple OSS Distributions
113*43a90889SApple OSS Distributions #define NBCLPGSHIFT (PAGE_SHIFT - MBIGCLSHIFT)
114*43a90889SApple OSS Distributions #define NBCLPG (1 << NBCLPGSHIFT) /* # of big cl per page */
115*43a90889SApple OSS Distributions
116*43a90889SApple OSS Distributions #define NMBPCL (MCLBYTES / _MSIZE)
117*43a90889SApple OSS Distributions
118*43a90889SApple OSS Distributions #define NCLPJCLSHIFT (M16KCLSHIFT - MCLSHIFT)
119*43a90889SApple OSS Distributions #define NCLPJCL (1 << NCLPJCLSHIFT) /* # of cl per jumbo cl */
120*43a90889SApple OSS Distributions
121*43a90889SApple OSS Distributions #define NCLPBGSHIFT (MBIGCLSHIFT - MCLSHIFT)
122*43a90889SApple OSS Distributions #define NCLPBG (1 << NCLPBGSHIFT) /* # of cl per big cl */
123*43a90889SApple OSS Distributions
124*43a90889SApple OSS Distributions /*
125*43a90889SApple OSS Distributions * Macros for type conversion
126*43a90889SApple OSS Distributions * mtod(m,t) - convert mbuf pointer to data pointer of correct type
127*43a90889SApple OSS Distributions * mtodo(m, o) -- Same as above but with offset 'o' into data.
128*43a90889SApple OSS Distributions */
129*43a90889SApple OSS Distributions #define mtod(m, t) ((t)(void *)m_mtod_current(m))
130*43a90889SApple OSS Distributions #define mtodo(m, o) ((void *)(mtod(m, uint8_t *) + (o)))
131*43a90889SApple OSS Distributions
132*43a90889SApple OSS Distributions /* header at beginning of each mbuf: */
133*43a90889SApple OSS Distributions struct m_hdr {
134*43a90889SApple OSS Distributions struct mbuf *mh_next; /* next buffer in chain */
135*43a90889SApple OSS Distributions struct mbuf *mh_nextpkt; /* next chain in queue/record */
136*43a90889SApple OSS Distributions uintptr_t mh_data; /* location of data */
137*43a90889SApple OSS Distributions int32_t mh_len; /* amount of data in this mbuf */
138*43a90889SApple OSS Distributions u_int16_t mh_type; /* type of data in this mbuf */
139*43a90889SApple OSS Distributions u_int16_t mh_flags; /* flags; see below */
140*43a90889SApple OSS Distributions #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
141*43a90889SApple OSS Distributions /* This is needed because of how _MLEN is defined and used. Ideally, _MLEN
142*43a90889SApple OSS Distributions * should be defined using the offsetof(struct mbuf, M_dat), since there is
143*43a90889SApple OSS Distributions * no guarantee that mbuf.M_dat will start where mbuf.m_hdr ends. The compiler
144*43a90889SApple OSS Distributions * may (and does in the armv7k case) insert padding between m_hdr and M_dat in
145*43a90889SApple OSS Distributions * mbuf. We cannot easily use offsetof, however, since _MLEN is referenced
146*43a90889SApple OSS Distributions * in the definition of mbuf.
147*43a90889SApple OSS Distributions */
148*43a90889SApple OSS Distributions } __attribute__((aligned(8)));
149*43a90889SApple OSS Distributions #else
150*43a90889SApple OSS Distributions };
151*43a90889SApple OSS Distributions #endif
152*43a90889SApple OSS Distributions
153*43a90889SApple OSS Distributions /*
154*43a90889SApple OSS Distributions * Packet tag structure (see below for details).
155*43a90889SApple OSS Distributions */
156*43a90889SApple OSS Distributions struct m_tag {
157*43a90889SApple OSS Distributions uint64_t m_tag_cookie; /* Error checking */
158*43a90889SApple OSS Distributions SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */
159*43a90889SApple OSS Distributions void *__sized_by(m_tag_len) m_tag_data;
160*43a90889SApple OSS Distributions uint16_t m_tag_type; /* Module specific type */
161*43a90889SApple OSS Distributions uint16_t m_tag_len; /* Length of data */
162*43a90889SApple OSS Distributions uint32_t m_tag_id; /* Module ID */
163*43a90889SApple OSS Distributions void *m_tag_mb_cl; /* pointer to mbuf or cluster container */
164*43a90889SApple OSS Distributions #ifndef __LP64__
165*43a90889SApple OSS Distributions u_int32_t m_tag_pad;
166*43a90889SApple OSS Distributions #endif /* !__LP64__ */
167*43a90889SApple OSS Distributions };
168*43a90889SApple OSS Distributions
169*43a90889SApple OSS Distributions #define M_TAG_ALIGN(len) \
170*43a90889SApple OSS Distributions (P2ROUNDUP(len, sizeof (u_int64_t)) + sizeof (struct m_tag))
171*43a90889SApple OSS Distributions
172*43a90889SApple OSS Distributions #define M_TAG_INIT(tag, id, type, len, data, mb_cl) { \
173*43a90889SApple OSS Distributions VERIFY(IS_P2ALIGNED((tag), sizeof(u_int64_t))); \
174*43a90889SApple OSS Distributions (tag)->m_tag_type = (type); \
175*43a90889SApple OSS Distributions (tag)->m_tag_len = (uint16_t)(len); \
176*43a90889SApple OSS Distributions (tag)->m_tag_id = (id); \
177*43a90889SApple OSS Distributions (tag)->m_tag_data = (data); \
178*43a90889SApple OSS Distributions (tag)->m_tag_mb_cl = (mb_cl); \
179*43a90889SApple OSS Distributions m_tag_create_cookie(tag); \
180*43a90889SApple OSS Distributions }
181*43a90889SApple OSS Distributions
182*43a90889SApple OSS Distributions #define M_TAG_VALID_PATTERN 0xfeedfacefeedfaceULL
183*43a90889SApple OSS Distributions #define M_TAG_FREE_PATTERN 0xdeadbeefdeadbeefULL
184*43a90889SApple OSS Distributions
185*43a90889SApple OSS Distributions /*
186*43a90889SApple OSS Distributions * Packet tag header structure at the top of mbuf whe mbufs are use for m_tag
187*43a90889SApple OSS Distributions * Pointers are 32-bit in ILP32; m_tag needs 64-bit alignment, hence padded.
188*43a90889SApple OSS Distributions */
189*43a90889SApple OSS Distributions struct m_taghdr {
190*43a90889SApple OSS Distributions #ifndef __LP64__
191*43a90889SApple OSS Distributions u_int32_t pad; /* For structure alignment */
192*43a90889SApple OSS Distributions #endif /* !__LP64__ */
193*43a90889SApple OSS Distributions u_int64_t mth_refcnt; /* Number of tags in this mbuf */
194*43a90889SApple OSS Distributions };
195*43a90889SApple OSS Distributions
196*43a90889SApple OSS Distributions /*
197*43a90889SApple OSS Distributions * Driver auxiliary metadata tag (KERNEL_TAG_TYPE_DRVAUX).
198*43a90889SApple OSS Distributions */
199*43a90889SApple OSS Distributions struct m_drvaux_tag {
200*43a90889SApple OSS Distributions u_int32_t da_family; /* IFNET_FAMILY values */
201*43a90889SApple OSS Distributions u_int32_t da_subfamily; /* IFNET_SUBFAMILY values */
202*43a90889SApple OSS Distributions u_int32_t da_reserved; /* for future */
203*43a90889SApple OSS Distributions u_int32_t da_length; /* length of following data */
204*43a90889SApple OSS Distributions };
205*43a90889SApple OSS Distributions
206*43a90889SApple OSS Distributions /* Values for pftag_flags (16-bit wide) */
207*43a90889SApple OSS Distributions #define PF_TAG_GENERATED 0x1 /* pkt generated by PF */
208*43a90889SApple OSS Distributions #define PF_TAG_FRAGCACHE 0x2
209*43a90889SApple OSS Distributions #define PF_TAG_TRANSLATE_LOCALHOST 0x4
210*43a90889SApple OSS Distributions #if PF_ECN
211*43a90889SApple OSS Distributions #define PF_TAG_HDR_INET 0x8 /* hdr points to IPv4 */
212*43a90889SApple OSS Distributions #define PF_TAG_HDR_INET6 0x10 /* hdr points to IPv6 */
213*43a90889SApple OSS Distributions #endif /* PF_ECN */
214*43a90889SApple OSS Distributions #define PF_TAG_REASSEMBLED 0x20 /* pkt reassembled by PF */
215*43a90889SApple OSS Distributions #define PF_TAG_REFRAGMENTED 0x40 /* pkt refragmented by PF */
216*43a90889SApple OSS Distributions /*
217*43a90889SApple OSS Distributions * PF mbuf tag
218*43a90889SApple OSS Distributions */
219*43a90889SApple OSS Distributions struct pf_mtag {
220*43a90889SApple OSS Distributions u_int16_t pftag_flags; /* PF_TAG flags */
221*43a90889SApple OSS Distributions u_int16_t pftag_rtableid; /* alternate routing table id */
222*43a90889SApple OSS Distributions u_int16_t pftag_tag;
223*43a90889SApple OSS Distributions u_int16_t pftag_routed;
224*43a90889SApple OSS Distributions #if PF_ECN
225*43a90889SApple OSS Distributions void *pftag_hdr; /* saved hdr pos in mbuf, for ECN */
226*43a90889SApple OSS Distributions #endif /* PF_ECN */
227*43a90889SApple OSS Distributions };
228*43a90889SApple OSS Distributions
229*43a90889SApple OSS Distributions /* System reserved PF tags */
230*43a90889SApple OSS Distributions #define PF_TAG_ID_SYSTEM_SERVICE 0xff00
231*43a90889SApple OSS Distributions #define PF_TAG_ID_STACK_DROP 0xff01
232*43a90889SApple OSS Distributions
233*43a90889SApple OSS Distributions /*
234*43a90889SApple OSS Distributions * PF fragment tag
235*43a90889SApple OSS Distributions */
236*43a90889SApple OSS Distributions struct pf_fragment_tag {
237*43a90889SApple OSS Distributions uint32_t ft_id; /* fragment id */
238*43a90889SApple OSS Distributions uint16_t ft_hdrlen; /* header length of reassembled pkt */
239*43a90889SApple OSS Distributions uint16_t ft_unfragpartlen; /* length of the per-fragment headers */
240*43a90889SApple OSS Distributions uint16_t ft_extoff; /* last extension header offset or 0 */
241*43a90889SApple OSS Distributions uint16_t ft_maxlen; /* maximum fragment payload length */
242*43a90889SApple OSS Distributions };
243*43a90889SApple OSS Distributions
244*43a90889SApple OSS Distributions /*
245*43a90889SApple OSS Distributions * TCP mbuf tag
246*43a90889SApple OSS Distributions */
247*43a90889SApple OSS Distributions struct tcp_pktinfo {
248*43a90889SApple OSS Distributions union {
249*43a90889SApple OSS Distributions struct {
250*43a90889SApple OSS Distributions uint16_t seg_size; /* segment size (actual MSS) */
251*43a90889SApple OSS Distributions uint16_t hdr_len; /* size of IP+TCP header, might be zero */
252*43a90889SApple OSS Distributions uint32_t start_seq; /* start seq of this packet */
253*43a90889SApple OSS Distributions pid_t pid;
254*43a90889SApple OSS Distributions pid_t e_pid;
255*43a90889SApple OSS Distributions } __tx;
256*43a90889SApple OSS Distributions struct {
257*43a90889SApple OSS Distributions uint8_t seg_cnt; /* # of coalesced TCP pkts */
258*43a90889SApple OSS Distributions } __rx;
259*43a90889SApple OSS Distributions } __offload;
260*43a90889SApple OSS Distributions #define tx_seg_size proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.seg_size
261*43a90889SApple OSS Distributions #define tso_segsz tx_seg_size
262*43a90889SApple OSS Distributions #define tx_hdr_len proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.hdr_len
263*43a90889SApple OSS Distributions #define tx_start_seq proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.start_seq
264*43a90889SApple OSS Distributions #define tx_tcp_pid proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.pid
265*43a90889SApple OSS Distributions #define tx_tcp_e_pid proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.e_pid
266*43a90889SApple OSS Distributions
267*43a90889SApple OSS Distributions #define rx_seg_cnt proto_mtag.__pr_u.tcp.tm_tcp.__offload.__rx.seg_cnt
268*43a90889SApple OSS Distributions };
269*43a90889SApple OSS Distributions
270*43a90889SApple OSS Distributions /*
271*43a90889SApple OSS Distributions * MPTCP mbuf tag
272*43a90889SApple OSS Distributions */
273*43a90889SApple OSS Distributions struct mptcp_pktinfo {
274*43a90889SApple OSS Distributions uint64_t mtpi_dsn; /* MPTCP Data Sequence Number */
275*43a90889SApple OSS Distributions uint32_t mtpi_rel_seq; /* Relative Seq Number */
276*43a90889SApple OSS Distributions uint16_t mtpi_length; /* Length of mapping */
277*43a90889SApple OSS Distributions uint16_t mtpi_csum;
278*43a90889SApple OSS Distributions #define mp_dsn proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_dsn
279*43a90889SApple OSS Distributions #define mp_rseq proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_rel_seq
280*43a90889SApple OSS Distributions #define mp_rlen proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_length
281*43a90889SApple OSS Distributions #define mp_csum proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_csum
282*43a90889SApple OSS Distributions };
283*43a90889SApple OSS Distributions
284*43a90889SApple OSS Distributions /*
285*43a90889SApple OSS Distributions * TCP specific mbuf tag. Note that the current implementation uses
286*43a90889SApple OSS Distributions * MPTCP metadata strictly between MPTCP and the TCP subflow layers,
287*43a90889SApple OSS Distributions * hence tm_tcp and tm_mptcp are mutually exclusive. This also means
288*43a90889SApple OSS Distributions * that TCP messages functionality is currently incompatible with MPTCP.
289*43a90889SApple OSS Distributions */
290*43a90889SApple OSS Distributions struct tcp_mtag {
291*43a90889SApple OSS Distributions union {
292*43a90889SApple OSS Distributions struct tcp_pktinfo tm_tcp; /* TCP and below */
293*43a90889SApple OSS Distributions struct mptcp_pktinfo tm_mptcp; /* MPTCP-TCP only */
294*43a90889SApple OSS Distributions };
295*43a90889SApple OSS Distributions };
296*43a90889SApple OSS Distributions
297*43a90889SApple OSS Distributions struct udp_mtag {
298*43a90889SApple OSS Distributions pid_t _pid;
299*43a90889SApple OSS Distributions pid_t _e_pid;
300*43a90889SApple OSS Distributions #define tx_udp_pid proto_mtag.__pr_u.udp._pid
301*43a90889SApple OSS Distributions #define tx_udp_e_pid proto_mtag.__pr_u.udp._e_pid
302*43a90889SApple OSS Distributions };
303*43a90889SApple OSS Distributions
304*43a90889SApple OSS Distributions struct rawip_mtag {
305*43a90889SApple OSS Distributions pid_t _pid;
306*43a90889SApple OSS Distributions pid_t _e_pid;
307*43a90889SApple OSS Distributions #define tx_rawip_pid proto_mtag.__pr_u.rawip._pid
308*43a90889SApple OSS Distributions #define tx_rawip_e_pid proto_mtag.__pr_u.rawip._e_pid
309*43a90889SApple OSS Distributions };
310*43a90889SApple OSS Distributions
311*43a90889SApple OSS Distributions struct driver_mtag_ {
312*43a90889SApple OSS Distributions uintptr_t _drv_tx_compl_arg;
313*43a90889SApple OSS Distributions uintptr_t _drv_tx_compl_data;
314*43a90889SApple OSS Distributions kern_return_t _drv_tx_status;
315*43a90889SApple OSS Distributions uint16_t _drv_flowid;
316*43a90889SApple OSS Distributions #define drv_tx_compl_arg builtin_mtag._drv_mtag._drv_tx_compl_arg
317*43a90889SApple OSS Distributions #define drv_tx_compl_data builtin_mtag._drv_mtag._drv_tx_compl_data
318*43a90889SApple OSS Distributions #define drv_tx_status builtin_mtag._drv_mtag._drv_tx_status
319*43a90889SApple OSS Distributions #define drv_flowid builtin_mtag._drv_mtag._drv_flowid
320*43a90889SApple OSS Distributions };
321*43a90889SApple OSS Distributions
322*43a90889SApple OSS Distributions /*
323*43a90889SApple OSS Distributions * Protocol specific mbuf tag (at most one protocol metadata per mbuf).
324*43a90889SApple OSS Distributions *
325*43a90889SApple OSS Distributions * Care must be taken to ensure that they are mutually exclusive, e.g.
326*43a90889SApple OSS Distributions * IPsec policy ID implies no TCP segment offload (which is fine given
327*43a90889SApple OSS Distributions * that the former is used on the virtual ipsec interface that does
328*43a90889SApple OSS Distributions * not advertise the TSO capability.)
329*43a90889SApple OSS Distributions */
330*43a90889SApple OSS Distributions struct proto_mtag_ {
331*43a90889SApple OSS Distributions union {
332*43a90889SApple OSS Distributions struct tcp_mtag tcp; /* TCP specific */
333*43a90889SApple OSS Distributions struct udp_mtag udp; /* UDP specific */
334*43a90889SApple OSS Distributions struct rawip_mtag rawip; /* raw IPv4/IPv6 specific */
335*43a90889SApple OSS Distributions } __pr_u;
336*43a90889SApple OSS Distributions };
337*43a90889SApple OSS Distributions
338*43a90889SApple OSS Distributions /*
339*43a90889SApple OSS Distributions * NECP specific mbuf tag.
340*43a90889SApple OSS Distributions */
341*43a90889SApple OSS Distributions struct necp_mtag_ {
342*43a90889SApple OSS Distributions u_int32_t necp_policy_id;
343*43a90889SApple OSS Distributions u_int32_t necp_skip_policy_id;
344*43a90889SApple OSS Distributions u_int32_t necp_route_rule_id;
345*43a90889SApple OSS Distributions u_int16_t necp_last_interface_index;
346*43a90889SApple OSS Distributions u_int16_t necp_app_id;
347*43a90889SApple OSS Distributions };
348*43a90889SApple OSS Distributions
349*43a90889SApple OSS Distributions union builtin_mtag {
350*43a90889SApple OSS Distributions struct {
351*43a90889SApple OSS Distributions struct proto_mtag_ _proto_mtag; /* built-in protocol-specific tag */
352*43a90889SApple OSS Distributions struct pf_mtag _pf_mtag; /* built-in PF tag */
353*43a90889SApple OSS Distributions struct necp_mtag_ _necp_mtag; /* built-in NECP tag */
354*43a90889SApple OSS Distributions } _net_mtag;
355*43a90889SApple OSS Distributions struct driver_mtag_ _drv_mtag;
356*43a90889SApple OSS Distributions #define necp_mtag builtin_mtag._net_mtag._necp_mtag
357*43a90889SApple OSS Distributions #define proto_mtag builtin_mtag._net_mtag._proto_mtag
358*43a90889SApple OSS Distributions #define driver_mtag builtin_mtag._drv_mtag
359*43a90889SApple OSS Distributions };
360*43a90889SApple OSS Distributions
361*43a90889SApple OSS Distributions /*
362*43a90889SApple OSS Distributions * Record/packet header in first mbuf of chain; valid only if M_PKTHDR set.
363*43a90889SApple OSS Distributions */
364*43a90889SApple OSS Distributions struct pkthdr {
365*43a90889SApple OSS Distributions struct ifnet *rcvif; /* rcv interface */
366*43a90889SApple OSS Distributions /* variables for ip and tcp reassembly */
367*43a90889SApple OSS Distributions void *pkt_hdr; /* pointer to packet header */
368*43a90889SApple OSS Distributions int32_t len; /* total packet length */
369*43a90889SApple OSS Distributions /* variables for hardware checksum */
370*43a90889SApple OSS Distributions /* Note: csum_flags is used for hardware checksum and VLAN */
371*43a90889SApple OSS Distributions u_int32_t csum_flags; /* flags regarding checksum */
372*43a90889SApple OSS Distributions union {
373*43a90889SApple OSS Distributions struct {
374*43a90889SApple OSS Distributions u_int16_t val; /* checksum value */
375*43a90889SApple OSS Distributions u_int16_t start; /* checksum start offset */
376*43a90889SApple OSS Distributions } _csum_rx;
377*43a90889SApple OSS Distributions #define csum_rx_val _csum_rx.val
378*43a90889SApple OSS Distributions #define csum_rx_start _csum_rx.start
379*43a90889SApple OSS Distributions struct {
380*43a90889SApple OSS Distributions u_int16_t start; /* checksum start offset */
381*43a90889SApple OSS Distributions u_int16_t stuff; /* checksum stuff offset */
382*43a90889SApple OSS Distributions } _csum_tx;
383*43a90889SApple OSS Distributions #define csum_tx_start _csum_tx.start
384*43a90889SApple OSS Distributions #define csum_tx_stuff _csum_tx.stuff
385*43a90889SApple OSS Distributions /*
386*43a90889SApple OSS Distributions * Generic data field used by csum routines.
387*43a90889SApple OSS Distributions * It gets used differently in different contexts.
388*43a90889SApple OSS Distributions */
389*43a90889SApple OSS Distributions u_int32_t csum_data;
390*43a90889SApple OSS Distributions };
391*43a90889SApple OSS Distributions u_int16_t vlan_tag; /* VLAN tag, host byte order */
392*43a90889SApple OSS Distributions /*
393*43a90889SApple OSS Distributions * Packet classifier info
394*43a90889SApple OSS Distributions *
395*43a90889SApple OSS Distributions * PKTF_FLOW_ID set means valid flow ID. A non-zero flow ID value
396*43a90889SApple OSS Distributions * means the packet has been classified by one of the flow sources.
397*43a90889SApple OSS Distributions * It is also a prerequisite for flow control advisory, which is
398*43a90889SApple OSS Distributions * enabled by additionally setting PKTF_FLOW_ADV.
399*43a90889SApple OSS Distributions *
400*43a90889SApple OSS Distributions * The protocol value is a best-effort representation of the payload.
401*43a90889SApple OSS Distributions * It is opportunistically updated and used only for optimization.
402*43a90889SApple OSS Distributions * It is not a substitute for parsing the protocol header(s); use it
403*43a90889SApple OSS Distributions * only as a hint.
404*43a90889SApple OSS Distributions *
405*43a90889SApple OSS Distributions * If PKTF_IFAINFO is set, pkt_ifainfo contains one or both of the
406*43a90889SApple OSS Distributions * indices of interfaces which own the source and/or destination
407*43a90889SApple OSS Distributions * addresses of the packet. For the local/loopback case (PKTF_LOOP),
408*43a90889SApple OSS Distributions * both should be valid, and thus allows for the receiving end to
409*43a90889SApple OSS Distributions * quickly determine the actual interfaces used by the the addresses;
410*43a90889SApple OSS Distributions * they may not necessarily be the same or refer to the loopback
411*43a90889SApple OSS Distributions * interface. Otherwise, in the non-local/loopback case, the indices
412*43a90889SApple OSS Distributions * are opportunistically set, and because of that only one may be set
413*43a90889SApple OSS Distributions * (0 means the index has not been determined.) In addition, the
414*43a90889SApple OSS Distributions * interface address flags are also recorded. This allows us to avoid
415*43a90889SApple OSS Distributions * storing the corresponding {in,in6}_ifaddr in an mbuf tag. Ideally
416*43a90889SApple OSS Distributions * this would be a superset of {ia,ia6}_flags, but the namespaces are
417*43a90889SApple OSS Distributions * overlapping at present, so we'll need a new set of values in future
418*43a90889SApple OSS Distributions * to achieve this. For now, we will just rely on the address family
419*43a90889SApple OSS Distributions * related code paths examining this mbuf to interpret the flags.
420*43a90889SApple OSS Distributions */
421*43a90889SApple OSS Distributions u_int8_t pkt_proto; /* IPPROTO value */
422*43a90889SApple OSS Distributions u_int8_t pkt_flowsrc; /* FLOWSRC values */
423*43a90889SApple OSS Distributions u_int32_t pkt_flowid; /* flow ID */
424*43a90889SApple OSS Distributions u_int32_t pkt_flags; /* PKTF flags (see below) */
425*43a90889SApple OSS Distributions u_int32_t pkt_svc; /* MBUF_SVC value */
426*43a90889SApple OSS Distributions
427*43a90889SApple OSS Distributions u_int32_t pkt_compl_context; /* Packet completion context */
428*43a90889SApple OSS Distributions
429*43a90889SApple OSS Distributions union {
430*43a90889SApple OSS Distributions struct {
431*43a90889SApple OSS Distributions u_int16_t src; /* ifindex of src addr i/f */
432*43a90889SApple OSS Distributions u_int16_t src_flags; /* src PKT_IFAIFF flags */
433*43a90889SApple OSS Distributions u_int16_t dst; /* ifindex of dst addr i/f */
434*43a90889SApple OSS Distributions u_int16_t dst_flags; /* dst PKT_IFAIFF flags */
435*43a90889SApple OSS Distributions } _pkt_iaif;
436*43a90889SApple OSS Distributions #define src_ifindex _pkt_iaif.src
437*43a90889SApple OSS Distributions #define src_iff _pkt_iaif.src_flags
438*43a90889SApple OSS Distributions #define dst_ifindex _pkt_iaif.dst
439*43a90889SApple OSS Distributions #define dst_iff _pkt_iaif.dst_flags
440*43a90889SApple OSS Distributions u_int64_t pkt_ifainfo; /* data field used by ifainfo */
441*43a90889SApple OSS Distributions struct {
442*43a90889SApple OSS Distributions u_int32_t if_data; /* bytes in interface queue */
443*43a90889SApple OSS Distributions u_int32_t sndbuf_data; /* bytes in socket buffer */
444*43a90889SApple OSS Distributions } _pkt_bsr; /* Buffer status report used by cellular interface */
445*43a90889SApple OSS Distributions #define bufstatus_if _pkt_bsr.if_data
446*43a90889SApple OSS Distributions #define bufstatus_sndbuf _pkt_bsr.sndbuf_data
447*43a90889SApple OSS Distributions };
448*43a90889SApple OSS Distributions u_int64_t pkt_timestamp; /* TX: enqueue time, RX: receive timestamp */
449*43a90889SApple OSS Distributions
450*43a90889SApple OSS Distributions /*
451*43a90889SApple OSS Distributions * Tags (external and built-in)
452*43a90889SApple OSS Distributions */
453*43a90889SApple OSS Distributions SLIST_HEAD(packet_tags, m_tag) tags; /* list of external tags */
454*43a90889SApple OSS Distributions union builtin_mtag builtin_mtag;
455*43a90889SApple OSS Distributions
456*43a90889SApple OSS Distributions uint32_t comp_gencnt;
457*43a90889SApple OSS Distributions uint32_t pkt_crumbs:16,
458*43a90889SApple OSS Distributions pkt_compl_callbacks:8,
459*43a90889SApple OSS Distributions pkt_ext_flags:3,
460*43a90889SApple OSS Distributions pkt_unused:5; /* Currently unused - feel free to grab those 5 bits */
461*43a90889SApple OSS Distributions /*
462*43a90889SApple OSS Distributions * Module private scratch space (32-bit aligned), currently 16-bytes
463*43a90889SApple OSS Distributions * large. Anything stored here is not guaranteed to survive across
464*43a90889SApple OSS Distributions * modules. The AQM layer (outbound) uses all 16-bytes for both
465*43a90889SApple OSS Distributions * packet scheduling and flow advisory information.
466*43a90889SApple OSS Distributions */
467*43a90889SApple OSS Distributions struct {
468*43a90889SApple OSS Distributions union {
469*43a90889SApple OSS Distributions u_int8_t __mpriv8[16];
470*43a90889SApple OSS Distributions u_int16_t __mpriv16[8];
471*43a90889SApple OSS Distributions struct {
472*43a90889SApple OSS Distributions union {
473*43a90889SApple OSS Distributions u_int8_t __val8[4];
474*43a90889SApple OSS Distributions u_int16_t __val16[2];
475*43a90889SApple OSS Distributions u_int32_t __val32;
476*43a90889SApple OSS Distributions } __mpriv32_u;
477*43a90889SApple OSS Distributions } __mpriv32[4];
478*43a90889SApple OSS Distributions u_int64_t __mpriv64[2];
479*43a90889SApple OSS Distributions } __mpriv_u;
480*43a90889SApple OSS Distributions } pkt_mpriv __attribute__((aligned(4)));
481*43a90889SApple OSS Distributions #define pkt_mpriv_hash pkt_mpriv.__mpriv_u.__mpriv32[0].__mpriv32_u.__val32
482*43a90889SApple OSS Distributions #define pkt_mpriv_flags pkt_mpriv.__mpriv_u.__mpriv32[1].__mpriv32_u.__val32
483*43a90889SApple OSS Distributions #define pkt_mpriv_srcid pkt_mpriv.__mpriv_u.__mpriv32[2].__mpriv32_u.__val32
484*43a90889SApple OSS Distributions #define pkt_mpriv_fidx pkt_mpriv.__mpriv_u.__mpriv32[3].__mpriv32_u.__val32
485*43a90889SApple OSS Distributions };
486*43a90889SApple OSS Distributions
487*43a90889SApple OSS Distributions /*
488*43a90889SApple OSS Distributions * Flow data source type. A data source module is responsible for generating
489*43a90889SApple OSS Distributions * a unique flow ID and associating it to each data flow as pkt_flowid.
490*43a90889SApple OSS Distributions * This is required for flow control/advisory, as it allows the output queue
491*43a90889SApple OSS Distributions * to identify the data source object and inform that it can resume its
492*43a90889SApple OSS Distributions * transmission (in the event it was flow controlled.)
493*43a90889SApple OSS Distributions */
494*43a90889SApple OSS Distributions #define FLOWSRC_INPCB 1 /* flow ID generated by INPCB */
495*43a90889SApple OSS Distributions #define FLOWSRC_IFNET 2 /* flow ID generated by interface */
496*43a90889SApple OSS Distributions #define FLOWSRC_PF 3 /* flow ID generated by PF */
497*43a90889SApple OSS Distributions #define FLOWSRC_CHANNEL 4 /* flow ID generated by channel */
498*43a90889SApple OSS Distributions
499*43a90889SApple OSS Distributions /*
500*43a90889SApple OSS Distributions * FLOWSRC_MPKL_INPUT is not a true flow data source and is used for
501*43a90889SApple OSS Distributions * multi-layer packet logging. We're usurping the pkt_flowsrc field because
502*43a90889SApple OSS Distributions * the mbuf packet header ran out of space and pkt_flowsrc is normally
503*43a90889SApple OSS Distributions * used on output so we assume we can safely overwrite the normal semantic of
504*43a90889SApple OSS Distributions * the field.
505*43a90889SApple OSS Distributions * This value is meant to be used on incoming packet from a lower level protocol
506*43a90889SApple OSS Distributions * to pass information to some upper level protocol. When FLOWSRC_MPKL_INPUT
507*43a90889SApple OSS Distributions * is set, the following fields are used:
508*43a90889SApple OSS Distributions * - pkt_proto: the IP protocol ID of the lower level protocol
509*43a90889SApple OSS Distributions * - pkt_flowid: the identifier of the packet at the lower protocol.
510*43a90889SApple OSS Distributions * For example ESP would set pkt_proto to IPPROTO_ESP and pkt_flowid to the SPI.
511*43a90889SApple OSS Distributions */
512*43a90889SApple OSS Distributions
513*43a90889SApple OSS Distributions /*
514*43a90889SApple OSS Distributions * Packet flags. Unlike m_flags, all packet flags are copied along when
515*43a90889SApple OSS Distributions * copying m_pkthdr, i.e. no equivalent of M_COPYFLAGS here. These flags
516*43a90889SApple OSS Distributions * (and other classifier info) will be cleared during DLIL input.
517*43a90889SApple OSS Distributions *
518*43a90889SApple OSS Distributions * Some notes about M_LOOP and PKTF_LOOP:
519*43a90889SApple OSS Distributions *
520*43a90889SApple OSS Distributions * - M_LOOP flag is overloaded, and its use is discouraged. Historically,
521*43a90889SApple OSS Distributions * that flag was used by the KAME implementation for allowing certain
522*43a90889SApple OSS Distributions * certain exceptions to be made in the IP6_EXTHDR_CHECK() logic; this
523*43a90889SApple OSS Distributions * was originally meant to be set as the packet is looped back to the
524*43a90889SApple OSS Distributions * system, and in some circumstances temporarily set in ip6_output().
525*43a90889SApple OSS Distributions * Over time, this flag was used by the pre-output routines to indicate
526*43a90889SApple OSS Distributions * to the DLIL frameout and output routines, that the packet may be
527*43a90889SApple OSS Distributions * looped back to the system under the right conditions. In addition,
528*43a90889SApple OSS Distributions * this is an mbuf flag rather than an mbuf packet header flag.
529*43a90889SApple OSS Distributions *
530*43a90889SApple OSS Distributions * - PKTF_LOOP is an mbuf packet header flag, which is set if and only
531*43a90889SApple OSS Distributions * if the packet was looped back to the system. This flag should be
532*43a90889SApple OSS Distributions * used instead for newer code.
533*43a90889SApple OSS Distributions */
534*43a90889SApple OSS Distributions #define PKTF_FLOW_ID 0x1 /* pkt has valid flowid value */
535*43a90889SApple OSS Distributions #define PKTF_FLOW_ADV 0x2 /* pkt triggers local flow advisory */
536*43a90889SApple OSS Distributions #define PKTF_FLOW_LOCALSRC 0x4 /* pkt is locally originated */
537*43a90889SApple OSS Distributions #define PKTF_FLOW_RAWSOCK 0x8 /* pkt locally generated by raw sock */
538*43a90889SApple OSS Distributions #define PKTF_PRIO_PRIVILEGED 0x10 /* packet priority is privileged */
539*43a90889SApple OSS Distributions #define PKTF_PROXY_DST 0x20 /* processed but not locally destined */
540*43a90889SApple OSS Distributions #define PKTF_INET_RESOLVE 0x40 /* IPv4 resolver packet */
541*43a90889SApple OSS Distributions #define PKTF_INET6_RESOLVE 0x80 /* IPv6 resolver packet */
542*43a90889SApple OSS Distributions #define PKTF_RESOLVE_RTR 0x100 /* pkt is for resolving router */
543*43a90889SApple OSS Distributions #define PKTF_SKIP_PKTAP 0x200 /* pkt has already passed through pktap */
544*43a90889SApple OSS Distributions #define PKTF_WAKE_PKT 0x400 /* packet caused system to wake from sleep */
545*43a90889SApple OSS Distributions #define PKTF_MPTCP 0x800 /* TCP with MPTCP metadata */
546*43a90889SApple OSS Distributions #define PKTF_MPSO 0x1000 /* MPTCP socket meta data */
547*43a90889SApple OSS Distributions #define PKTF_LOOP 0x2000 /* loopbacked packet */
548*43a90889SApple OSS Distributions #define PKTF_IFAINFO 0x4000 /* pkt has valid interface addr info */
549*43a90889SApple OSS Distributions #define PKTF_SO_BACKGROUND 0x8000 /* data is from background source */
550*43a90889SApple OSS Distributions #define PKTF_FORWARDED 0x10000 /* pkt was forwarded from another i/f */
551*43a90889SApple OSS Distributions #define PKTF_PRIV_GUARDED 0x20000 /* pkt_mpriv area guard enabled */
552*43a90889SApple OSS Distributions #define PKTF_KEEPALIVE 0x40000 /* pkt is kernel-generated keepalive */
553*43a90889SApple OSS Distributions #define PKTF_SO_REALTIME 0x80000 /* data is realtime traffic */
554*43a90889SApple OSS Distributions #define PKTF_VALID_UNSENT_DATA 0x100000 /* unsent data is valid */
555*43a90889SApple OSS Distributions #define PKTF_TCP_REXMT 0x200000 /* packet is TCP retransmission */
556*43a90889SApple OSS Distributions #define PKTF_REASSEMBLED 0x400000 /* Packet was reassembled */
557*43a90889SApple OSS Distributions #define PKTF_TX_COMPL_TS_REQ 0x800000 /* tx completion timestamp requested */
558*43a90889SApple OSS Distributions #define PKTF_TS_VALID 0x1000000 /* pkt timestamp is valid */
559*43a90889SApple OSS Distributions #define PKTF_DRIVER_MTAG 0x2000000 /* driver mbuf tags fields inited */
560*43a90889SApple OSS Distributions #define PKTF_NEW_FLOW 0x4000000 /* Data from a new flow */
561*43a90889SApple OSS Distributions #define PKTF_START_SEQ 0x8000000 /* valid start sequence */
562*43a90889SApple OSS Distributions #define PKTF_LAST_PKT 0x10000000 /* last packet in the flow */
563*43a90889SApple OSS Distributions #define PKTF_MPTCP_REINJ 0x20000000 /* Packet has been reinjected for MPTCP */
564*43a90889SApple OSS Distributions #define PKTF_MPTCP_DFIN 0x40000000 /* Packet is a data-fin */
565*43a90889SApple OSS Distributions #define PKTF_HBH_CHKED 0x80000000 /* HBH option is checked */
566*43a90889SApple OSS Distributions
567*43a90889SApple OSS Distributions #define PKTF_EXT_OUTPUT_SCOPE 0x1 /* outgoing packet has ipv6 address scope id */
568*43a90889SApple OSS Distributions #define PKTF_EXT_L4S 0x2 /* pkts is from a L4S connection */
569*43a90889SApple OSS Distributions #define PKTF_EXT_QUIC 0x4 /* flag to denote a QUIC packet */
570*43a90889SApple OSS Distributions
571*43a90889SApple OSS Distributions #define PKT_CRUMB_TS_COMP_REQ 0x0001 /* timestamp completion requested */
572*43a90889SApple OSS Distributions #define PKT_CRUMB_TS_COMP_CB 0x0002 /* timestamp callback called */
573*43a90889SApple OSS Distributions #define PKT_CRUMB_DLIL_OUTPUT 0x0004 /* dlil_output called */
574*43a90889SApple OSS Distributions #define PKT_CRUMB_FLOW_TX 0x0008 /* dp_flow_tx_process called */
575*43a90889SApple OSS Distributions #define PKT_CRUMB_FQ_ENQUEUE 0x0010 /* fq_enqueue called */
576*43a90889SApple OSS Distributions #define PKT_CRUMB_FQ_DEQUEUE 0x0020 /* fq_dequeue called */
577*43a90889SApple OSS Distributions #define PKT_CRUMB_SK_PKT_COPY 0x0040 /* copy from mbuf to skywalk packet */
578*43a90889SApple OSS Distributions #define PKT_CRUMB_TCP_OUTPUT 0x0080
579*43a90889SApple OSS Distributions #define PKT_CRUMB_UDP_OUTPUT 0x0100
580*43a90889SApple OSS Distributions #define PKT_CRUMB_SOSEND 0x0200
581*43a90889SApple OSS Distributions #define PKT_CRUMB_DLIL_INPUT 0x0400
582*43a90889SApple OSS Distributions #define PKT_CRUMB_IP_INPUT 0x0800
583*43a90889SApple OSS Distributions #define PKT_CRUMB_TCP_INPUT 0x1000
584*43a90889SApple OSS Distributions #define PKT_CRUMB_UDP_INPUT 0x2000
585*43a90889SApple OSS Distributions
586*43a90889SApple OSS Distributions /* m_hdr_common crumbs flags */
587*43a90889SApple OSS Distributions #define CRUMB_INPUT_FLAG 0x0000000000010000
588*43a90889SApple OSS Distributions #define CRUMB_INTERFACE_FLAG 0x000000000001ffff
589*43a90889SApple OSS Distributions
590*43a90889SApple OSS Distributions /* flags related to flow control/advisory and identification */
591*43a90889SApple OSS Distributions #define PKTF_FLOW_MASK \
592*43a90889SApple OSS Distributions (PKTF_FLOW_ID | PKTF_FLOW_ADV | PKTF_FLOW_LOCALSRC | PKTF_FLOW_RAWSOCK)
593*43a90889SApple OSS Distributions
594*43a90889SApple OSS Distributions /*
595*43a90889SApple OSS Distributions * Description of external storage mapped into mbuf, valid only if M_EXT set.
596*43a90889SApple OSS Distributions */
597*43a90889SApple OSS Distributions typedef void (*__single m_ext_free_func_t)(caddr_t, u_int, caddr_t);
598*43a90889SApple OSS Distributions struct m_ext {
599*43a90889SApple OSS Distributions caddr_t __counted_by(ext_size) ext_buf; /* start of buffer */
600*43a90889SApple OSS Distributions m_ext_free_func_t ext_free; /* free routine (plain-text), if not the usual */
601*43a90889SApple OSS Distributions u_int ext_size; /* size of the external buffer */
602*43a90889SApple OSS Distributions caddr_t ext_arg; /* additional ext_free argument (plain-text) */
603*43a90889SApple OSS Distributions struct ext_ref {
604*43a90889SApple OSS Distributions struct mbuf *paired;
605*43a90889SApple OSS Distributions u_int16_t minref;
606*43a90889SApple OSS Distributions u_int16_t refcnt;
607*43a90889SApple OSS Distributions u_int16_t prefcnt;
608*43a90889SApple OSS Distributions u_int16_t flags;
609*43a90889SApple OSS Distributions u_int32_t priv;
610*43a90889SApple OSS Distributions } *ext_refflags;
611*43a90889SApple OSS Distributions };
612*43a90889SApple OSS Distributions
613*43a90889SApple OSS Distributions /* define m_ext to a type since it gets redefined below */
614*43a90889SApple OSS Distributions typedef struct m_ext _m_ext_t;
615*43a90889SApple OSS Distributions
616*43a90889SApple OSS Distributions #if CONFIG_MBUF_MCACHE
617*43a90889SApple OSS Distributions /*
618*43a90889SApple OSS Distributions * The following _MLEN and _MHLEN macros are private to xnu. Private code
619*43a90889SApple OSS Distributions * that are outside of xnu must use the mbuf_get_{mlen,mhlen} routines since
620*43a90889SApple OSS Distributions * the sizes of the structures are dependent upon specific xnu configs.
621*43a90889SApple OSS Distributions */
622*43a90889SApple OSS Distributions #define _MLEN (_MSIZE - sizeof(struct m_hdr)) /* normal data len */
623*43a90889SApple OSS Distributions #define _MHLEN (_MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
624*43a90889SApple OSS Distributions
625*43a90889SApple OSS Distributions #define NMBPGSHIFT (PAGE_SHIFT - _MSIZESHIFT)
626*43a90889SApple OSS Distributions #define NMBPG (1 << NMBPGSHIFT) /* # of mbufs per page */
627*43a90889SApple OSS Distributions
628*43a90889SApple OSS Distributions #define NMBPCLSHIFT (MCLSHIFT - _MSIZESHIFT)
629*43a90889SApple OSS Distributions
630*43a90889SApple OSS Distributions /*
631*43a90889SApple OSS Distributions * The mbuf object
632*43a90889SApple OSS Distributions */
633*43a90889SApple OSS Distributions struct mbuf {
634*43a90889SApple OSS Distributions struct m_hdr m_hdr;
635*43a90889SApple OSS Distributions union {
636*43a90889SApple OSS Distributions struct {
637*43a90889SApple OSS Distributions struct pkthdr MH_pkthdr; /* M_PKTHDR set */
638*43a90889SApple OSS Distributions union {
639*43a90889SApple OSS Distributions struct m_ext MH_ext; /* M_EXT set */
640*43a90889SApple OSS Distributions char MH_databuf[_MHLEN];
641*43a90889SApple OSS Distributions } MH_dat;
642*43a90889SApple OSS Distributions } MH;
643*43a90889SApple OSS Distributions char M_databuf[_MLEN]; /* !M_PKTHDR, !M_EXT */
644*43a90889SApple OSS Distributions } M_dat;
645*43a90889SApple OSS Distributions };
646*43a90889SApple OSS Distributions
647*43a90889SApple OSS Distributions #define m_next m_hdr.mh_next
648*43a90889SApple OSS Distributions #define m_len m_hdr.mh_len
649*43a90889SApple OSS Distributions #define m_data m_hdr.mh_data
650*43a90889SApple OSS Distributions #define m_type m_hdr.mh_type
651*43a90889SApple OSS Distributions #define m_flags m_hdr.mh_flags
652*43a90889SApple OSS Distributions #define m_nextpkt m_hdr.mh_nextpkt
653*43a90889SApple OSS Distributions #define m_act m_nextpkt
654*43a90889SApple OSS Distributions
655*43a90889SApple OSS Distributions #define m_ext M_dat.MH.MH_dat.MH_ext
656*43a90889SApple OSS Distributions #define m_pkthdr M_dat.MH.MH_pkthdr
657*43a90889SApple OSS Distributions #define m_pktdat M_dat.MH.MH_dat.MH_databuf
658*43a90889SApple OSS Distributions
659*43a90889SApple OSS Distributions #else /* !CONFIG_MBUF_MCACHE */
660*43a90889SApple OSS Distributions /*
661*43a90889SApple OSS Distributions * The following _MLEN and _MHLEN macros are private to xnu. Private code
662*43a90889SApple OSS Distributions * that are outside of xnu must use the mbuf_get_{mlen,mhlen} routines since
663*43a90889SApple OSS Distributions * the sizes of the structures are dependent upon specific xnu configs.
664*43a90889SApple OSS Distributions */
665*43a90889SApple OSS Distributions #define _MLEN (_MSIZE - sizeof(struct m_hdr_common)) /* normal data len */
666*43a90889SApple OSS Distributions #define _MHLEN (_MLEN) /* data len w/pkthdr */
667*43a90889SApple OSS Distributions
668*43a90889SApple OSS Distributions struct m_hdr_common {
669*43a90889SApple OSS Distributions struct m_hdr M_hdr;
670*43a90889SApple OSS Distributions struct m_ext M_ext __attribute__((aligned(16))); /* M_EXT set */
671*43a90889SApple OSS Distributions #if defined(__arm64__)
672*43a90889SApple OSS Distributions uint64_t m_hdr_crumbs;
673*43a90889SApple OSS Distributions #endif
674*43a90889SApple OSS Distributions struct pkthdr M_pkthdr __attribute__((aligned(16))); /* M_PKTHDR set */
675*43a90889SApple OSS Distributions };
676*43a90889SApple OSS Distributions
677*43a90889SApple OSS Distributions _Static_assert(sizeof(struct m_hdr_common) == 224, "Crumbs effecting size of struct");
678*43a90889SApple OSS Distributions #if defined(__arm64__)
679*43a90889SApple OSS Distributions _Static_assert(sizeof(struct m_hdr_common) == 224, "Crumbs effecting size of struct");
680*43a90889SApple OSS Distributions #endif
681*43a90889SApple OSS Distributions
682*43a90889SApple OSS Distributions /*
683*43a90889SApple OSS Distributions * The mbuf object
684*43a90889SApple OSS Distributions */
685*43a90889SApple OSS Distributions struct mbuf {
686*43a90889SApple OSS Distributions struct m_hdr_common M_hdr_common;
687*43a90889SApple OSS Distributions union {
688*43a90889SApple OSS Distributions char MH_databuf[_MHLEN];
689*43a90889SApple OSS Distributions char M_databuf[_MLEN]; /* !M_PKTHDR, !M_EXT */
690*43a90889SApple OSS Distributions } M_dat __attribute__((aligned(16)));
691*43a90889SApple OSS Distributions };
692*43a90889SApple OSS Distributions
693*43a90889SApple OSS Distributions #define m_next M_hdr_common.M_hdr.mh_next
694*43a90889SApple OSS Distributions #define m_len M_hdr_common.M_hdr.mh_len
695*43a90889SApple OSS Distributions #define m_data M_hdr_common.M_hdr.mh_data
696*43a90889SApple OSS Distributions #define m_type M_hdr_common.M_hdr.mh_type
697*43a90889SApple OSS Distributions #define m_flags M_hdr_common.M_hdr.mh_flags
698*43a90889SApple OSS Distributions #define m_nextpkt M_hdr_common.M_hdr.mh_nextpkt
699*43a90889SApple OSS Distributions
700*43a90889SApple OSS Distributions #define m_ext M_hdr_common.M_ext
701*43a90889SApple OSS Distributions #define m_pkthdr M_hdr_common.M_pkthdr
702*43a90889SApple OSS Distributions #define m_pktdat M_dat.MH_databuf
703*43a90889SApple OSS Distributions #if defined(__arm64__)
704*43a90889SApple OSS Distributions #define m_mhdrcommon_crumbs M_hdr_common.m_hdr_crumbs
705*43a90889SApple OSS Distributions #endif /* __arm64__ */
706*43a90889SApple OSS Distributions #endif /* CONFIG_MBUF_MCACHE */
707*43a90889SApple OSS Distributions
708*43a90889SApple OSS Distributions #define m_act m_nextpkt
709*43a90889SApple OSS Distributions #define m_dat M_dat.M_databuf
710*43a90889SApple OSS Distributions #define m_pktlen(_m) ((_m)->m_pkthdr.len)
711*43a90889SApple OSS Distributions #define m_pftag(_m) (&(_m)->m_pkthdr.builtin_mtag._net_mtag._pf_mtag)
712*43a90889SApple OSS Distributions #define m_necptag(_m) (&(_m)->m_pkthdr.builtin_mtag._net_mtag._necp_mtag)
713*43a90889SApple OSS Distributions
714*43a90889SApple OSS Distributions /* mbuf flags (private) */
715*43a90889SApple OSS Distributions #define M_EXT 0x0001 /* has associated external storage */
716*43a90889SApple OSS Distributions #define M_PKTHDR 0x0002 /* start of record */
717*43a90889SApple OSS Distributions #define M_EOR 0x0004 /* end of record */
718*43a90889SApple OSS Distributions #define M_PROTO1 0x0008 /* protocol-specific */
719*43a90889SApple OSS Distributions #define M_PROTO2 0x0010 /* protocol-specific */
720*43a90889SApple OSS Distributions #define M_PROTO3 0x0020 /* protocol-specific */
721*43a90889SApple OSS Distributions #define M_LOOP 0x0040 /* packet is looped back (also see PKTF_LOOP) */
722*43a90889SApple OSS Distributions #define M_PROTO5 0x0080 /* protocol-specific */
723*43a90889SApple OSS Distributions
724*43a90889SApple OSS Distributions /* mbuf pkthdr flags, also in m_flags (private) */
725*43a90889SApple OSS Distributions #define M_BCAST 0x0100 /* send/received as link-level broadcast */
726*43a90889SApple OSS Distributions #define M_MCAST 0x0200 /* send/received as link-level multicast */
727*43a90889SApple OSS Distributions #define M_FRAG 0x0400 /* packet is a fragment of a larger packet */
728*43a90889SApple OSS Distributions #define M_FIRSTFRAG 0x0800 /* packet is first fragment */
729*43a90889SApple OSS Distributions #define M_LASTFRAG 0x1000 /* packet is last fragment */
730*43a90889SApple OSS Distributions #define M_PROMISC 0x2000 /* packet is promiscuous (shouldn't go to stack) */
731*43a90889SApple OSS Distributions #define M_HASFCS 0x4000 /* packet has FCS */
732*43a90889SApple OSS Distributions #define M_TAGHDR 0x8000 /* m_tag hdr structure at top of mbuf data */
733*43a90889SApple OSS Distributions
734*43a90889SApple OSS Distributions /*
735*43a90889SApple OSS Distributions * Flags to purge when crossing layers.
736*43a90889SApple OSS Distributions */
737*43a90889SApple OSS Distributions #define M_PROTOFLAGS \
738*43a90889SApple OSS Distributions (M_PROTO1|M_PROTO2|M_PROTO3|M_PROTO5)
739*43a90889SApple OSS Distributions
740*43a90889SApple OSS Distributions /* flags copied when copying m_pkthdr */
741*43a90889SApple OSS Distributions #define M_COPYFLAGS \
742*43a90889SApple OSS Distributions (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO2|M_PROTO3 | \
743*43a90889SApple OSS Distributions M_LOOP|M_PROTO5|M_BCAST|M_MCAST|M_FRAG | \
744*43a90889SApple OSS Distributions M_FIRSTFRAG|M_LASTFRAG|M_PROMISC|M_HASFCS)
745*43a90889SApple OSS Distributions
746*43a90889SApple OSS Distributions /* flags indicating hw checksum support and sw checksum requirements */
747*43a90889SApple OSS Distributions #define CSUM_IP 0x0001 /* will csum IP */
748*43a90889SApple OSS Distributions #define CSUM_TCP 0x0002 /* will csum TCP */
749*43a90889SApple OSS Distributions #define CSUM_UDP 0x0004 /* will csum UDP */
750*43a90889SApple OSS Distributions #define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */
751*43a90889SApple OSS Distributions #define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */
752*43a90889SApple OSS Distributions #define CSUM_TCPIPV6 0x0020 /* will csum TCP for IPv6 */
753*43a90889SApple OSS Distributions #define CSUM_UDPIPV6 0x0040 /* will csum UDP for IPv6 */
754*43a90889SApple OSS Distributions #define CSUM_FRAGMENT_IPV6 0x0080 /* will do IPv6 fragmentation */
755*43a90889SApple OSS Distributions
756*43a90889SApple OSS Distributions #define CSUM_IP_CHECKED 0x0100 /* did csum IP */
757*43a90889SApple OSS Distributions #define CSUM_IP_VALID 0x0200 /* ... the csum is valid */
758*43a90889SApple OSS Distributions #define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */
759*43a90889SApple OSS Distributions #define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */
760*43a90889SApple OSS Distributions #define CSUM_PARTIAL 0x1000 /* simple Sum16 computation */
761*43a90889SApple OSS Distributions #define CSUM_ZERO_INVERT 0x2000 /* invert 0 to -0 (0xffff) */
762*43a90889SApple OSS Distributions
763*43a90889SApple OSS Distributions #define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP)
764*43a90889SApple OSS Distributions #define CSUM_DELAY_IP (CSUM_IP) /* IPv4 only: no IPv6 IP cksum */
765*43a90889SApple OSS Distributions #define CSUM_DELAY_IPV6_DATA (CSUM_TCPIPV6 | CSUM_UDPIPV6)
766*43a90889SApple OSS Distributions #define CSUM_DATA_IPV6_VALID CSUM_DATA_VALID /* csum_data field is valid */
767*43a90889SApple OSS Distributions
768*43a90889SApple OSS Distributions #define CSUM_TX_FLAGS \
769*43a90889SApple OSS Distributions (CSUM_DELAY_IP | CSUM_DELAY_DATA | CSUM_DELAY_IPV6_DATA | \
770*43a90889SApple OSS Distributions CSUM_DATA_VALID | CSUM_PARTIAL | CSUM_ZERO_INVERT)
771*43a90889SApple OSS Distributions
772*43a90889SApple OSS Distributions #define CSUM_RX_FULL_FLAGS \
773*43a90889SApple OSS Distributions (CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_PSEUDO_HDR | \
774*43a90889SApple OSS Distributions CSUM_DATA_VALID)
775*43a90889SApple OSS Distributions
776*43a90889SApple OSS Distributions #define CSUM_RX_FLAGS \
777*43a90889SApple OSS Distributions (CSUM_RX_FULL_FLAGS | CSUM_PARTIAL)
778*43a90889SApple OSS Distributions
779*43a90889SApple OSS Distributions
780*43a90889SApple OSS Distributions
781*43a90889SApple OSS Distributions /*
782*43a90889SApple OSS Distributions * Note: see also IF_HWASSIST_CSUM defined in <net/if_var.h>
783*43a90889SApple OSS Distributions */
784*43a90889SApple OSS Distributions
785*43a90889SApple OSS Distributions /* VLAN tag present */
786*43a90889SApple OSS Distributions #define CSUM_VLAN_TAG_VALID 0x00010000 /* vlan_tag field is valid */
787*43a90889SApple OSS Distributions
788*43a90889SApple OSS Distributions /* checksum start adjustment has been done */
789*43a90889SApple OSS Distributions #define CSUM_ADJUST_DONE 0x00020000
790*43a90889SApple OSS Distributions
791*43a90889SApple OSS Distributions /* VLAN encapsulation present */
792*43a90889SApple OSS Distributions #define CSUM_VLAN_ENCAP_PRESENT 0x00040000 /* mbuf has vlan encapsulation */
793*43a90889SApple OSS Distributions
794*43a90889SApple OSS Distributions /* TCP Segment Offloading requested on this mbuf */
795*43a90889SApple OSS Distributions #define CSUM_TSO_IPV4 0x00100000 /* This mbuf needs to be segmented by the NIC */
796*43a90889SApple OSS Distributions #define CSUM_TSO_IPV6 0x00200000 /* This mbuf needs to be segmented by the NIC */
797*43a90889SApple OSS Distributions
798*43a90889SApple OSS Distributions #define TSO_IPV4_OK(_ifp, _m) \
799*43a90889SApple OSS Distributions (((_ifp)->if_hwassist & IFNET_TSO_IPV4) && \
800*43a90889SApple OSS Distributions ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV4)) \
801*43a90889SApple OSS Distributions
802*43a90889SApple OSS Distributions #define TSO_IPV4_NOTOK(_ifp, _m) \
803*43a90889SApple OSS Distributions (!((_ifp)->if_hwassist & IFNET_TSO_IPV4) && \
804*43a90889SApple OSS Distributions ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV4)) \
805*43a90889SApple OSS Distributions
806*43a90889SApple OSS Distributions #define TSO_IPV6_OK(_ifp, _m) \
807*43a90889SApple OSS Distributions (((_ifp)->if_hwassist & IFNET_TSO_IPV6) && \
808*43a90889SApple OSS Distributions ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV6)) \
809*43a90889SApple OSS Distributions
810*43a90889SApple OSS Distributions #define TSO_IPV6_NOTOK(_ifp, _m) \
811*43a90889SApple OSS Distributions (!((_ifp)->if_hwassist & IFNET_TSO_IPV6) && \
812*43a90889SApple OSS Distributions ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV6)) \
813*43a90889SApple OSS Distributions
814*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
815*43a90889SApple OSS Distributions
816*43a90889SApple OSS Distributions /* mbuf types */
817*43a90889SApple OSS Distributions #define MT_FREE 0 /* should be on free list */
818*43a90889SApple OSS Distributions #define MT_DATA 1 /* dynamic (data) allocation */
819*43a90889SApple OSS Distributions #define MT_HEADER 2 /* packet header */
820*43a90889SApple OSS Distributions #define MT_SOCKET 3 /* socket structure */
821*43a90889SApple OSS Distributions #define MT_PCB 4 /* protocol control block */
822*43a90889SApple OSS Distributions #define MT_RTABLE 5 /* routing tables */
823*43a90889SApple OSS Distributions #define MT_HTABLE 6 /* IMP host tables */
824*43a90889SApple OSS Distributions #define MT_ATABLE 7 /* address resolution tables */
825*43a90889SApple OSS Distributions #define MT_SONAME 8 /* socket name */
826*43a90889SApple OSS Distributions #define MT_SOOPTS 10 /* socket options */
827*43a90889SApple OSS Distributions #define MT_FTABLE 11 /* fragment reassembly header */
828*43a90889SApple OSS Distributions #define MT_RIGHTS 12 /* access rights */
829*43a90889SApple OSS Distributions #define MT_IFADDR 13 /* interface address */
830*43a90889SApple OSS Distributions #define MT_CONTROL 14 /* extra-data protocol message */
831*43a90889SApple OSS Distributions #define MT_OOBDATA 15 /* expedited data */
832*43a90889SApple OSS Distributions #define MT_TAG 16 /* volatile metadata associated to pkts */
833*43a90889SApple OSS Distributions #define MT_MAX 32 /* enough? */
834*43a90889SApple OSS Distributions
835*43a90889SApple OSS Distributions enum {
836*43a90889SApple OSS Distributions MTF_FREE = (1 << MT_FREE),
837*43a90889SApple OSS Distributions MTF_DATA = (1 << MT_DATA),
838*43a90889SApple OSS Distributions MTF_HEADER = (1 << MT_HEADER),
839*43a90889SApple OSS Distributions MTF_SOCKET = (1 << MT_SOCKET),
840*43a90889SApple OSS Distributions MTF_PCB = (1 << MT_PCB),
841*43a90889SApple OSS Distributions MTF_RTABLE = (1 << MT_RTABLE),
842*43a90889SApple OSS Distributions MTF_HTABLE = (1 << MT_HTABLE),
843*43a90889SApple OSS Distributions MTF_ATABLE = (1 << MT_ATABLE),
844*43a90889SApple OSS Distributions MTF_SONAME = (1 << MT_SONAME),
845*43a90889SApple OSS Distributions MTF_SOOPTS = (1 << MT_SOOPTS),
846*43a90889SApple OSS Distributions MTF_FTABLE = (1 << MT_FTABLE),
847*43a90889SApple OSS Distributions MTF_RIGHTS = (1 << MT_RIGHTS),
848*43a90889SApple OSS Distributions MTF_IFADDR = (1 << MT_IFADDR),
849*43a90889SApple OSS Distributions MTF_CONTROL = (1 << MT_CONTROL),
850*43a90889SApple OSS Distributions MTF_OOBDATA = (1 << MT_OOBDATA),
851*43a90889SApple OSS Distributions MTF_TAG = (1 << MT_TAG),
852*43a90889SApple OSS Distributions };
853*43a90889SApple OSS Distributions
854*43a90889SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
855*43a90889SApple OSS Distributions /*
856*43a90889SApple OSS Distributions * mbuf allocation/deallocation macros:
857*43a90889SApple OSS Distributions *
858*43a90889SApple OSS Distributions * MGET(struct mbuf *m, int how, int type)
859*43a90889SApple OSS Distributions * allocates an mbuf and initializes it to contain internal data.
860*43a90889SApple OSS Distributions *
861*43a90889SApple OSS Distributions * MGETHDR(struct mbuf *m, int how, int type)
862*43a90889SApple OSS Distributions * allocates an mbuf and initializes it to contain a packet header
863*43a90889SApple OSS Distributions * and internal data.
864*43a90889SApple OSS Distributions */
865*43a90889SApple OSS Distributions
866*43a90889SApple OSS Distributions #if 1
867*43a90889SApple OSS Distributions #define MCHECK(m) m_mcheck(m)
868*43a90889SApple OSS Distributions #else
869*43a90889SApple OSS Distributions #define MCHECK(m)
870*43a90889SApple OSS Distributions #endif
871*43a90889SApple OSS Distributions
872*43a90889SApple OSS Distributions #define MGET(m, how, type) ((m) = m_get((how), (type)))
873*43a90889SApple OSS Distributions
874*43a90889SApple OSS Distributions #define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type)))
875*43a90889SApple OSS Distributions
876*43a90889SApple OSS Distributions /*
877*43a90889SApple OSS Distributions * Mbuf cluster macros.
878*43a90889SApple OSS Distributions * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
879*43a90889SApple OSS Distributions * MCLGET adds such clusters to a normal mbuf;
880*43a90889SApple OSS Distributions * the flag M_EXT is set upon success.
881*43a90889SApple OSS Distributions * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
882*43a90889SApple OSS Distributions * freeing the cluster if the reference count has reached 0.
883*43a90889SApple OSS Distributions *
884*43a90889SApple OSS Distributions * Normal mbuf clusters are normally treated as character arrays
885*43a90889SApple OSS Distributions * after allocation, but use the first word of the buffer as a free list
886*43a90889SApple OSS Distributions * pointer while on the free list.
887*43a90889SApple OSS Distributions */
888*43a90889SApple OSS Distributions union mcluster {
889*43a90889SApple OSS Distributions union mcluster *mcl_next;
890*43a90889SApple OSS Distributions char mcl_buf[MCLBYTES];
891*43a90889SApple OSS Distributions };
892*43a90889SApple OSS Distributions
893*43a90889SApple OSS Distributions #define MCLALLOC(p, how) ((p) = m_mclalloc(how))
894*43a90889SApple OSS Distributions
895*43a90889SApple OSS Distributions #define MCLFREE(p) m_mclfree(p)
896*43a90889SApple OSS Distributions
897*43a90889SApple OSS Distributions #define MCLGET(m, how) ((m) = m_mclget(m, how))
898*43a90889SApple OSS Distributions
899*43a90889SApple OSS Distributions /*
900*43a90889SApple OSS Distributions * Mbuf big cluster
901*43a90889SApple OSS Distributions */
902*43a90889SApple OSS Distributions union mbigcluster {
903*43a90889SApple OSS Distributions union mbigcluster *mbc_next;
904*43a90889SApple OSS Distributions char mbc_buf[MBIGCLBYTES];
905*43a90889SApple OSS Distributions };
906*43a90889SApple OSS Distributions
907*43a90889SApple OSS Distributions /*
908*43a90889SApple OSS Distributions * Mbuf jumbo cluster
909*43a90889SApple OSS Distributions */
910*43a90889SApple OSS Distributions union m16kcluster {
911*43a90889SApple OSS Distributions union m16kcluster *m16kcl_next;
912*43a90889SApple OSS Distributions char m16kcl_buf[M16KCLBYTES];
913*43a90889SApple OSS Distributions };
914*43a90889SApple OSS Distributions
915*43a90889SApple OSS Distributions #define MCLHASREFERENCE(m) m_mclhasreference(m)
916*43a90889SApple OSS Distributions
917*43a90889SApple OSS Distributions /*
918*43a90889SApple OSS Distributions * MFREE(struct mbuf *m, struct mbuf *n)
919*43a90889SApple OSS Distributions * Free a single mbuf and associated external storage.
920*43a90889SApple OSS Distributions * Place the successor, if any, in n.
921*43a90889SApple OSS Distributions */
922*43a90889SApple OSS Distributions
923*43a90889SApple OSS Distributions #define MFREE(m, n) ((n) = m_free(m))
924*43a90889SApple OSS Distributions
925*43a90889SApple OSS Distributions /*
926*43a90889SApple OSS Distributions * Copy mbuf pkthdr from from to to.
927*43a90889SApple OSS Distributions * from must have M_PKTHDR set, and to must be empty.
928*43a90889SApple OSS Distributions * aux pointer will be moved to `to'.
929*43a90889SApple OSS Distributions */
930*43a90889SApple OSS Distributions #define M_COPY_PKTHDR(to, from) m_copy_pkthdr(to, from)
931*43a90889SApple OSS Distributions
932*43a90889SApple OSS Distributions #define M_COPY_PFTAG(to, from) m_copy_pftag(to, from)
933*43a90889SApple OSS Distributions
934*43a90889SApple OSS Distributions #define M_COPY_NECPTAG(to, from) m_copy_necptag(to, from)
935*43a90889SApple OSS Distributions
936*43a90889SApple OSS Distributions #define M_COPY_CLASSIFIER(to, from) m_copy_classifier(to, from)
937*43a90889SApple OSS Distributions
938*43a90889SApple OSS Distributions /*
939*43a90889SApple OSS Distributions * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can
940*43a90889SApple OSS Distributions * be both the local data payload, or an external buffer area, depending on
941*43a90889SApple OSS Distributions * whether M_EXT is set).
942*43a90889SApple OSS Distributions */
943*43a90889SApple OSS Distributions #define M_WRITABLE(m) (((m)->m_flags & M_EXT) == 0 || !MCLHASREFERENCE(m))
944*43a90889SApple OSS Distributions
945*43a90889SApple OSS Distributions /*
946*43a90889SApple OSS Distributions * These macros are mapped to the appropriate KPIs, so that private code
947*43a90889SApple OSS Distributions * can be simply recompiled in order to be forward-compatible with future
948*43a90889SApple OSS Distributions * changes toward the struture sizes.
949*43a90889SApple OSS Distributions */
950*43a90889SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
951*43a90889SApple OSS Distributions #define MLEN _MLEN
952*43a90889SApple OSS Distributions #define MHLEN _MHLEN
953*43a90889SApple OSS Distributions #define MINCLSIZE (MLEN + MHLEN)
954*43a90889SApple OSS Distributions #else
955*43a90889SApple OSS Distributions #define MLEN mbuf_get_mlen() /* normal mbuf data len */
956*43a90889SApple OSS Distributions #define MHLEN mbuf_get_mhlen() /* data len in an mbuf w/pkthdr */
957*43a90889SApple OSS Distributions #define MINCLSIZE mbuf_get_minclsize() /* cluster usage threshold */
958*43a90889SApple OSS Distributions #endif
959*43a90889SApple OSS Distributions /*
960*43a90889SApple OSS Distributions * Return the address of the start of the buffer associated with an mbuf,
961*43a90889SApple OSS Distributions * handling external storage, packet-header mbufs, and regular data mbufs.
962*43a90889SApple OSS Distributions */
963*43a90889SApple OSS Distributions #define M_START(m) \
964*43a90889SApple OSS Distributions (((m)->m_flags & M_EXT) ? (caddr_t)(m)->m_ext.ext_buf : \
965*43a90889SApple OSS Distributions ((m)->m_flags & M_PKTHDR) ? &(m)->m_pktdat[0] : \
966*43a90889SApple OSS Distributions &(m)->m_dat[0])
967*43a90889SApple OSS Distributions
968*43a90889SApple OSS Distributions /*
969*43a90889SApple OSS Distributions * Return the size of the buffer associated with an mbuf, handling external
970*43a90889SApple OSS Distributions * storage, packet-header mbufs, and regular data mbufs.
971*43a90889SApple OSS Distributions */
972*43a90889SApple OSS Distributions #define M_SIZE(m) \
973*43a90889SApple OSS Distributions (((m)->m_flags & M_EXT) ? (m)->m_ext.ext_size : \
974*43a90889SApple OSS Distributions ((m)->m_flags & M_PKTHDR) ? MHLEN : \
975*43a90889SApple OSS Distributions MLEN)
976*43a90889SApple OSS Distributions
977*43a90889SApple OSS Distributions #define M_ALIGN(m, len) m_align(m, len)
978*43a90889SApple OSS Distributions #define MH_ALIGN(m, len) m_align(m, len)
979*43a90889SApple OSS Distributions #define MEXT_ALIGN(m, len) m_align(m, len)
980*43a90889SApple OSS Distributions
981*43a90889SApple OSS Distributions /*
982*43a90889SApple OSS Distributions * Compute the amount of space available before the current start of data in
983*43a90889SApple OSS Distributions * an mbuf.
984*43a90889SApple OSS Distributions *
985*43a90889SApple OSS Distributions * The M_WRITABLE() is a temporary, conservative safety measure: the burden
986*43a90889SApple OSS Distributions * of checking writability of the mbuf data area rests solely with the caller.
987*43a90889SApple OSS Distributions */
988*43a90889SApple OSS Distributions #define M_LEADINGSPACE(m) \
989*43a90889SApple OSS Distributions (M_WRITABLE(m) ? ((m)->m_data - (uintptr_t)M_START(m)) : 0)
990*43a90889SApple OSS Distributions
991*43a90889SApple OSS Distributions /*
992*43a90889SApple OSS Distributions * Compute the amount of space available after the end of data in an mbuf.
993*43a90889SApple OSS Distributions *
994*43a90889SApple OSS Distributions * The M_WRITABLE() is a temporary, conservative safety measure: the burden
995*43a90889SApple OSS Distributions * of checking writability of the mbuf data area rests solely with the caller.
996*43a90889SApple OSS Distributions */
997*43a90889SApple OSS Distributions #define M_TRAILINGSPACE(m) \
998*43a90889SApple OSS Distributions (M_WRITABLE(m) ? \
999*43a90889SApple OSS Distributions ((M_START(m) + M_SIZE(m)) - (mtod(m, caddr_t) + (m)->m_len)) : 0)
1000*43a90889SApple OSS Distributions
1001*43a90889SApple OSS Distributions /*
1002*43a90889SApple OSS Distributions * Arrange to prepend space of size plen to mbuf m.
1003*43a90889SApple OSS Distributions * If a new mbuf must be allocated, how specifies whether to wait.
1004*43a90889SApple OSS Distributions * If how is M_DONTWAIT and allocation fails, the original mbuf chain
1005*43a90889SApple OSS Distributions * is freed and m is set to NULL.
1006*43a90889SApple OSS Distributions */
1007*43a90889SApple OSS Distributions #define M_PREPEND(m, plen, how, align) \
1008*43a90889SApple OSS Distributions ((m) = m_prepend_2((m), (plen), (how), (align)))
1009*43a90889SApple OSS Distributions
1010*43a90889SApple OSS Distributions /* change mbuf to new type */
1011*43a90889SApple OSS Distributions #define MCHTYPE(m, t) m_mchtype(m, t)
1012*43a90889SApple OSS Distributions
1013*43a90889SApple OSS Distributions /* compatiblity with 4.3 */
1014*43a90889SApple OSS Distributions #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
1015*43a90889SApple OSS Distributions
1016*43a90889SApple OSS Distributions #define MBSHIFT 20 /* 1MB */
1017*43a90889SApple OSS Distributions #define MBSIZE (1 << MBSHIFT)
1018*43a90889SApple OSS Distributions #define GBSHIFT 30 /* 1GB */
1019*43a90889SApple OSS Distributions #define GBSIZE (1 << GBSHIFT)
1020*43a90889SApple OSS Distributions
1021*43a90889SApple OSS Distributions /*
1022*43a90889SApple OSS Distributions * M_STRUCT_GET ensures that intermediate protocol header (from "off" to
1023*43a90889SApple OSS Distributions * "off+len") is located in single mbuf, on contiguous memory region.
1024*43a90889SApple OSS Distributions * The pointer to the region will be returned to pointer variable "val",
1025*43a90889SApple OSS Distributions * with type "typ".
1026*43a90889SApple OSS Distributions *
1027*43a90889SApple OSS Distributions * M_STRUCT_GET0 does the same, except that it aligns the structure at
1028*43a90889SApple OSS Distributions * very top of mbuf. GET0 is likely to make memory copy than GET.
1029*43a90889SApple OSS Distributions */
1030*43a90889SApple OSS Distributions #define M_STRUCT_GET(val, typ, m, off, len) \
1031*43a90889SApple OSS Distributions do { \
1032*43a90889SApple OSS Distributions struct mbuf *t; \
1033*43a90889SApple OSS Distributions int tmp; \
1034*43a90889SApple OSS Distributions \
1035*43a90889SApple OSS Distributions if ((m)->m_len >= (off) + (len)) { \
1036*43a90889SApple OSS Distributions (val) = (typ)(mtod((m), caddr_t) + (off)); \
1037*43a90889SApple OSS Distributions } else { \
1038*43a90889SApple OSS Distributions t = m_pulldown((m), (off), (len), &tmp); \
1039*43a90889SApple OSS Distributions if (t != NULL) { \
1040*43a90889SApple OSS Distributions if (t->m_len < tmp + (len)) \
1041*43a90889SApple OSS Distributions panic("m_pulldown malfunction"); \
1042*43a90889SApple OSS Distributions (val) = (typ)(mtod(t, caddr_t) + tmp); \
1043*43a90889SApple OSS Distributions } else { \
1044*43a90889SApple OSS Distributions (val) = (typ)NULL; \
1045*43a90889SApple OSS Distributions (m) = NULL; \
1046*43a90889SApple OSS Distributions } \
1047*43a90889SApple OSS Distributions } \
1048*43a90889SApple OSS Distributions } while (0)
1049*43a90889SApple OSS Distributions
1050*43a90889SApple OSS Distributions #define M_STRUCT_GET0(val, typ, m, off, len) \
1051*43a90889SApple OSS Distributions do { \
1052*43a90889SApple OSS Distributions struct mbuf *t; \
1053*43a90889SApple OSS Distributions \
1054*43a90889SApple OSS Distributions if ((off) == 0 && ((m)->m_len >= (len))) { \
1055*43a90889SApple OSS Distributions (val) = (typ)(void *)mtod(m, caddr_t); \
1056*43a90889SApple OSS Distributions } else { \
1057*43a90889SApple OSS Distributions t = m_pulldown((m), (off), (len), NULL); \
1058*43a90889SApple OSS Distributions if (t != NULL) { \
1059*43a90889SApple OSS Distributions if (t->m_len < (len)) \
1060*43a90889SApple OSS Distributions panic("m_pulldown malfunction"); \
1061*43a90889SApple OSS Distributions (val) = (typ)(void *)mtod(t, caddr_t); \
1062*43a90889SApple OSS Distributions } else { \
1063*43a90889SApple OSS Distributions (val) = (typ)NULL; \
1064*43a90889SApple OSS Distributions (m) = NULL; \
1065*43a90889SApple OSS Distributions } \
1066*43a90889SApple OSS Distributions } \
1067*43a90889SApple OSS Distributions } while (0)
1068*43a90889SApple OSS Distributions
1069*43a90889SApple OSS Distributions #define MBUF_INPUT_CHECK(m, rcvif) \
1070*43a90889SApple OSS Distributions do { \
1071*43a90889SApple OSS Distributions if (!(m->m_flags & MBUF_PKTHDR) || \
1072*43a90889SApple OSS Distributions m->m_len < 0 || \
1073*43a90889SApple OSS Distributions m->m_len > ((njcl > 0) ? njclbytes : MBIGCLBYTES) || \
1074*43a90889SApple OSS Distributions m->m_type == MT_FREE || \
1075*43a90889SApple OSS Distributions ((m->m_flags & M_EXT) != 0 && m->m_ext.ext_buf == NULL)) { \
1076*43a90889SApple OSS Distributions panic_plain("Failed mbuf validity check: mbuf %p len %d " \
1077*43a90889SApple OSS Distributions "type %d flags 0x%x data %p rcvif %s ifflags 0x%x", \
1078*43a90889SApple OSS Distributions m, m->m_len, m->m_type, m->m_flags, \
1079*43a90889SApple OSS Distributions ((m->m_flags & M_EXT) \
1080*43a90889SApple OSS Distributions ? m->m_ext.ext_buf \
1081*43a90889SApple OSS Distributions : (caddr_t __unsafe_indexable)m->m_data), \
1082*43a90889SApple OSS Distributions if_name(rcvif), \
1083*43a90889SApple OSS Distributions (rcvif->if_flags & 0xffff)); \
1084*43a90889SApple OSS Distributions } \
1085*43a90889SApple OSS Distributions } while (0)
1086*43a90889SApple OSS Distributions
1087*43a90889SApple OSS Distributions /*
1088*43a90889SApple OSS Distributions * Simple mbuf queueing system
1089*43a90889SApple OSS Distributions *
1090*43a90889SApple OSS Distributions * This is basically a SIMPLEQ adapted to mbuf use (i.e. using
1091*43a90889SApple OSS Distributions * m_nextpkt instead of field.sqe_next).
1092*43a90889SApple OSS Distributions *
1093*43a90889SApple OSS Distributions * m_next is ignored, so queueing chains of mbufs is possible
1094*43a90889SApple OSS Distributions */
1095*43a90889SApple OSS Distributions #define MBUFQ_HEAD(name) \
1096*43a90889SApple OSS Distributions struct name { \
1097*43a90889SApple OSS Distributions struct mbuf *mq_first; /* first packet */ \
1098*43a90889SApple OSS Distributions struct mbuf **mq_last; /* addr of last next packet */ \
1099*43a90889SApple OSS Distributions }
1100*43a90889SApple OSS Distributions
1101*43a90889SApple OSS Distributions #define MBUFQ_INIT(q) do { \
1102*43a90889SApple OSS Distributions MBUFQ_FIRST(q) = NULL; \
1103*43a90889SApple OSS Distributions (q)->mq_last = &MBUFQ_FIRST(q); \
1104*43a90889SApple OSS Distributions } while (0)
1105*43a90889SApple OSS Distributions
1106*43a90889SApple OSS Distributions #define MBUFQ_PREPEND(q, m) do { \
1107*43a90889SApple OSS Distributions if ((MBUFQ_NEXT(m) = MBUFQ_FIRST(q)) == NULL) \
1108*43a90889SApple OSS Distributions (q)->mq_last = &MBUFQ_NEXT(m); \
1109*43a90889SApple OSS Distributions MBUFQ_FIRST(q) = (m); \
1110*43a90889SApple OSS Distributions } while (0)
1111*43a90889SApple OSS Distributions
1112*43a90889SApple OSS Distributions #define MBUFQ_ENQUEUE(q, m) do { \
1113*43a90889SApple OSS Distributions MBUFQ_NEXT(m) = NULL; \
1114*43a90889SApple OSS Distributions *(q)->mq_last = (m); \
1115*43a90889SApple OSS Distributions (q)->mq_last = &MBUFQ_NEXT(m); \
1116*43a90889SApple OSS Distributions } while (0)
1117*43a90889SApple OSS Distributions
1118*43a90889SApple OSS Distributions #define MBUFQ_ENQUEUE_MULTI(q, m, n) do { \
1119*43a90889SApple OSS Distributions MBUFQ_NEXT(n) = NULL; \
1120*43a90889SApple OSS Distributions *(q)->mq_last = (m); \
1121*43a90889SApple OSS Distributions (q)->mq_last = &MBUFQ_NEXT(n); \
1122*43a90889SApple OSS Distributions } while (0)
1123*43a90889SApple OSS Distributions
1124*43a90889SApple OSS Distributions #define MBUFQ_DEQUEUE(q, m) do { \
1125*43a90889SApple OSS Distributions if (((m) = MBUFQ_FIRST(q)) != NULL) { \
1126*43a90889SApple OSS Distributions if ((MBUFQ_FIRST(q) = MBUFQ_NEXT(m)) == NULL) \
1127*43a90889SApple OSS Distributions (q)->mq_last = &MBUFQ_FIRST(q); \
1128*43a90889SApple OSS Distributions else \
1129*43a90889SApple OSS Distributions MBUFQ_NEXT(m) = NULL; \
1130*43a90889SApple OSS Distributions } \
1131*43a90889SApple OSS Distributions } while (0)
1132*43a90889SApple OSS Distributions
1133*43a90889SApple OSS Distributions #define MBUFQ_REMOVE(q, m) do { \
1134*43a90889SApple OSS Distributions if (MBUFQ_FIRST(q) == (m)) { \
1135*43a90889SApple OSS Distributions MBUFQ_DEQUEUE(q, m); \
1136*43a90889SApple OSS Distributions } else { \
1137*43a90889SApple OSS Distributions struct mbuf *_m = MBUFQ_FIRST(q); \
1138*43a90889SApple OSS Distributions while (MBUFQ_NEXT(_m) != (m)) \
1139*43a90889SApple OSS Distributions _m = MBUFQ_NEXT(_m); \
1140*43a90889SApple OSS Distributions if ((MBUFQ_NEXT(_m) = \
1141*43a90889SApple OSS Distributions MBUFQ_NEXT(MBUFQ_NEXT(_m))) == NULL) \
1142*43a90889SApple OSS Distributions (q)->mq_last = &MBUFQ_NEXT(_m); \
1143*43a90889SApple OSS Distributions } \
1144*43a90889SApple OSS Distributions } while (0)
1145*43a90889SApple OSS Distributions
1146*43a90889SApple OSS Distributions #define MBUFQ_DRAIN(q) do { \
1147*43a90889SApple OSS Distributions struct mbuf *__m0; \
1148*43a90889SApple OSS Distributions while ((__m0 = MBUFQ_FIRST(q)) != NULL) { \
1149*43a90889SApple OSS Distributions MBUFQ_FIRST(q) = MBUFQ_NEXT(__m0); \
1150*43a90889SApple OSS Distributions MBUFQ_NEXT(__m0) = NULL; \
1151*43a90889SApple OSS Distributions m_freem(__m0); \
1152*43a90889SApple OSS Distributions } \
1153*43a90889SApple OSS Distributions (q)->mq_last = &MBUFQ_FIRST(q); \
1154*43a90889SApple OSS Distributions } while (0)
1155*43a90889SApple OSS Distributions
1156*43a90889SApple OSS Distributions #define MBUFQ_DROP_AND_DRAIN(q, d, r) do { \
1157*43a90889SApple OSS Distributions struct mbuf *__m0; \
1158*43a90889SApple OSS Distributions while ((__m0 = MBUFQ_FIRST(q)) != NULL) { \
1159*43a90889SApple OSS Distributions MBUFQ_FIRST(q) = MBUFQ_NEXT(__m0); \
1160*43a90889SApple OSS Distributions MBUFQ_NEXT(__m0) = NULL; \
1161*43a90889SApple OSS Distributions m_drop(__m0, (d) | DROPTAP_FLAG_L2_MISSING, (r), NULL, 0); \
1162*43a90889SApple OSS Distributions } \
1163*43a90889SApple OSS Distributions (q)->mq_last = &MBUFQ_FIRST(q); \
1164*43a90889SApple OSS Distributions } while (0)
1165*43a90889SApple OSS Distributions
1166*43a90889SApple OSS Distributions #define MBUFQ_FOREACH(m, q) \
1167*43a90889SApple OSS Distributions for ((m) = MBUFQ_FIRST(q); \
1168*43a90889SApple OSS Distributions (m); \
1169*43a90889SApple OSS Distributions (m) = MBUFQ_NEXT(m))
1170*43a90889SApple OSS Distributions
1171*43a90889SApple OSS Distributions #define MBUFQ_FOREACH_SAFE(m, q, tvar) \
1172*43a90889SApple OSS Distributions for ((m) = MBUFQ_FIRST(q); \
1173*43a90889SApple OSS Distributions (m) && ((tvar) = MBUFQ_NEXT(m), 1); \
1174*43a90889SApple OSS Distributions (m) = (tvar))
1175*43a90889SApple OSS Distributions
1176*43a90889SApple OSS Distributions #define MBUFQ_EMPTY(q) ((q)->mq_first == NULL)
1177*43a90889SApple OSS Distributions #define MBUFQ_FIRST(q) ((q)->mq_first)
1178*43a90889SApple OSS Distributions #define MBUFQ_NEXT(m) ((m)->m_nextpkt)
1179*43a90889SApple OSS Distributions /*
1180*43a90889SApple OSS Distributions * mq_last is initialized to point to mq_first, so check if they're
1181*43a90889SApple OSS Distributions * equal and return NULL when the list is empty. Otherwise, we need
1182*43a90889SApple OSS Distributions * to subtract the offset of MBUQ_NEXT (i.e. m_nextpkt field) to get
1183*43a90889SApple OSS Distributions * to the base mbuf address to return to caller.
1184*43a90889SApple OSS Distributions */
1185*43a90889SApple OSS Distributions #define MBUFQ_LAST(head) \
1186*43a90889SApple OSS Distributions (((head)->mq_last == &MBUFQ_FIRST(head)) ? NULL : \
1187*43a90889SApple OSS Distributions __container_of((head)->mq_last, struct mbuf, m_nextpkt))
1188*43a90889SApple OSS Distributions
1189*43a90889SApple OSS Distributions #if (DEBUG || DEVELOPMENT)
1190*43a90889SApple OSS Distributions #define MBUFQ_ADD_CRUMB_MULTI(_q, _h, _t, _f) do { \
1191*43a90889SApple OSS Distributions struct mbuf * _saved = (_t)->m_nextpkt; \
1192*43a90889SApple OSS Distributions struct mbuf * _m; \
1193*43a90889SApple OSS Distributions for (_m = (_h); _m != NULL; _m = MBUFQ_NEXT(_m)) { \
1194*43a90889SApple OSS Distributions m_add_crumb((_m), (_f)); \
1195*43a90889SApple OSS Distributions } \
1196*43a90889SApple OSS Distributions (_t)->m_nextpkt = _saved; \
1197*43a90889SApple OSS Distributions } while (0)
1198*43a90889SApple OSS Distributions
1199*43a90889SApple OSS Distributions #define MBUFQ_ADD_CRUMB(_q, _m, _f) do { \
1200*43a90889SApple OSS Distributions m_add_crumb((_m), (_f)); \
1201*43a90889SApple OSS Distributions } while (0)
1202*43a90889SApple OSS Distributions #else
1203*43a90889SApple OSS Distributions #define MBUFQ_ADD_CRUMB_MULTI(_q, _h, _t, _f)
1204*43a90889SApple OSS Distributions #define MBUFQ_ADD_CRUMB(_q, _m, _f)
1205*43a90889SApple OSS Distributions #endif /* (DEBUG || DEVELOPMENT) */
1206*43a90889SApple OSS Distributions
1207*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
1208*43a90889SApple OSS Distributions
1209*43a90889SApple OSS Distributions /*
1210*43a90889SApple OSS Distributions * Mbuf statistics (legacy).
1211*43a90889SApple OSS Distributions */
1212*43a90889SApple OSS Distributions struct mbstat {
1213*43a90889SApple OSS Distributions u_int32_t m_mbufs; /* mbufs obtained from page pool */
1214*43a90889SApple OSS Distributions u_int32_t m_clusters; /* clusters obtained from page pool */
1215*43a90889SApple OSS Distributions u_int32_t m_spare; /* spare field */
1216*43a90889SApple OSS Distributions u_int32_t m_clfree; /* free clusters */
1217*43a90889SApple OSS Distributions u_int32_t m_drops; /* times failed to find space */
1218*43a90889SApple OSS Distributions u_int32_t m_wait; /* times waited for space */
1219*43a90889SApple OSS Distributions u_int32_t m_drain; /* times drained protocols for space */
1220*43a90889SApple OSS Distributions u_short m_mtypes[256]; /* type specific mbuf allocations */
1221*43a90889SApple OSS Distributions u_int32_t m_mcfail; /* times m_copym failed */
1222*43a90889SApple OSS Distributions u_int32_t m_mpfail; /* times m_pullup failed */
1223*43a90889SApple OSS Distributions u_int32_t m_msize; /* length of an mbuf */
1224*43a90889SApple OSS Distributions u_int32_t m_mclbytes; /* length of an mbuf cluster */
1225*43a90889SApple OSS Distributions u_int32_t m_minclsize; /* min length of data to allocate a cluster */
1226*43a90889SApple OSS Distributions u_int32_t m_mlen; /* length of data in an mbuf */
1227*43a90889SApple OSS Distributions u_int32_t m_mhlen; /* length of data in a header mbuf */
1228*43a90889SApple OSS Distributions u_int32_t m_bigclusters; /* clusters obtained from page pool */
1229*43a90889SApple OSS Distributions u_int32_t m_bigclfree; /* free clusters */
1230*43a90889SApple OSS Distributions u_int32_t m_bigmclbytes; /* length of an mbuf cluster */
1231*43a90889SApple OSS Distributions u_int32_t m_forcedefunct; /* times we force defunct'ed an app's sockets */
1232*43a90889SApple OSS Distributions };
1233*43a90889SApple OSS Distributions
1234*43a90889SApple OSS Distributions /* Compatibillity with 10.3 */
1235*43a90889SApple OSS Distributions struct ombstat {
1236*43a90889SApple OSS Distributions u_int32_t m_mbufs; /* mbufs obtained from page pool */
1237*43a90889SApple OSS Distributions u_int32_t m_clusters; /* clusters obtained from page pool */
1238*43a90889SApple OSS Distributions u_int32_t m_spare; /* spare field */
1239*43a90889SApple OSS Distributions u_int32_t m_clfree; /* free clusters */
1240*43a90889SApple OSS Distributions u_int32_t m_drops; /* times failed to find space */
1241*43a90889SApple OSS Distributions u_int32_t m_wait; /* times waited for space */
1242*43a90889SApple OSS Distributions u_int32_t m_drain; /* times drained protocols for space */
1243*43a90889SApple OSS Distributions u_short m_mtypes[256]; /* type specific mbuf allocations */
1244*43a90889SApple OSS Distributions u_int32_t m_mcfail; /* times m_copym failed */
1245*43a90889SApple OSS Distributions u_int32_t m_mpfail; /* times m_pullup failed */
1246*43a90889SApple OSS Distributions u_int32_t m_msize; /* length of an mbuf */
1247*43a90889SApple OSS Distributions u_int32_t m_mclbytes; /* length of an mbuf cluster */
1248*43a90889SApple OSS Distributions u_int32_t m_minclsize; /* min length of data to allocate a cluster */
1249*43a90889SApple OSS Distributions u_int32_t m_mlen; /* length of data in an mbuf */
1250*43a90889SApple OSS Distributions u_int32_t m_mhlen; /* length of data in a header mbuf */
1251*43a90889SApple OSS Distributions };
1252*43a90889SApple OSS Distributions
1253*43a90889SApple OSS Distributions /*
1254*43a90889SApple OSS Distributions * mbuf class statistics.
1255*43a90889SApple OSS Distributions */
1256*43a90889SApple OSS Distributions #define MAX_MBUF_CNAME 15
1257*43a90889SApple OSS Distributions
1258*43a90889SApple OSS Distributions #if defined(XNU_KERNEL_PRIVATE)
1259*43a90889SApple OSS Distributions /* For backwards compatibility with 32-bit userland process */
1260*43a90889SApple OSS Distributions struct omb_class_stat {
1261*43a90889SApple OSS Distributions char mbcl_cname[MAX_MBUF_CNAME + 1]; /* class name */
1262*43a90889SApple OSS Distributions u_int32_t mbcl_size; /* buffer size */
1263*43a90889SApple OSS Distributions u_int32_t mbcl_total; /* # of buffers created */
1264*43a90889SApple OSS Distributions u_int32_t mbcl_active; /* # of active buffers */
1265*43a90889SApple OSS Distributions u_int32_t mbcl_infree; /* # of available buffers */
1266*43a90889SApple OSS Distributions u_int32_t mbcl_slab_cnt; /* # of available slabs */
1267*43a90889SApple OSS Distributions u_int32_t mbcl_pad; /* padding */
1268*43a90889SApple OSS Distributions u_int64_t mbcl_alloc_cnt; /* # of times alloc is called */
1269*43a90889SApple OSS Distributions u_int64_t mbcl_free_cnt; /* # of times free is called */
1270*43a90889SApple OSS Distributions u_int64_t mbcl_notified; /* # of notified wakeups */
1271*43a90889SApple OSS Distributions u_int64_t mbcl_purge_cnt; /* # of purges so far */
1272*43a90889SApple OSS Distributions u_int64_t mbcl_fail_cnt; /* # of allocation failures */
1273*43a90889SApple OSS Distributions u_int32_t mbcl_ctotal; /* total only for this class */
1274*43a90889SApple OSS Distributions u_int32_t mbcl_release_cnt; /* amount of memory returned */
1275*43a90889SApple OSS Distributions /*
1276*43a90889SApple OSS Distributions * Cache layer statistics
1277*43a90889SApple OSS Distributions */
1278*43a90889SApple OSS Distributions u_int32_t mbcl_mc_state; /* cache state (see below) */
1279*43a90889SApple OSS Distributions u_int32_t mbcl_mc_cached; /* # of cached buffers */
1280*43a90889SApple OSS Distributions u_int32_t mbcl_mc_waiter_cnt; /* # waiters on the cache */
1281*43a90889SApple OSS Distributions u_int32_t mbcl_mc_wretry_cnt; /* # of wait retries */
1282*43a90889SApple OSS Distributions u_int32_t mbcl_mc_nwretry_cnt; /* # of no-wait retry attempts */
1283*43a90889SApple OSS Distributions u_int32_t mbcl_reserved[7]; /* for future use */
1284*43a90889SApple OSS Distributions } __attribute__((__packed__));
1285*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
1286*43a90889SApple OSS Distributions
1287*43a90889SApple OSS Distributions typedef struct mb_class_stat {
1288*43a90889SApple OSS Distributions char mbcl_cname[MAX_MBUF_CNAME + 1]; /* class name */
1289*43a90889SApple OSS Distributions u_int32_t mbcl_size; /* buffer size */
1290*43a90889SApple OSS Distributions u_int32_t mbcl_total; /* # of buffers created */
1291*43a90889SApple OSS Distributions u_int32_t mbcl_active; /* # of active buffers */
1292*43a90889SApple OSS Distributions u_int32_t mbcl_infree; /* # of available buffers */
1293*43a90889SApple OSS Distributions u_int32_t mbcl_slab_cnt; /* # of available slabs */
1294*43a90889SApple OSS Distributions #if defined(KERNEL) || defined(__LP64__)
1295*43a90889SApple OSS Distributions u_int32_t mbcl_pad; /* padding */
1296*43a90889SApple OSS Distributions #endif /* KERNEL || __LP64__ */
1297*43a90889SApple OSS Distributions u_int64_t mbcl_alloc_cnt; /* # of times alloc is called */
1298*43a90889SApple OSS Distributions u_int64_t mbcl_free_cnt; /* # of times free is called */
1299*43a90889SApple OSS Distributions u_int64_t mbcl_notified; /* # of notified wakeups */
1300*43a90889SApple OSS Distributions u_int64_t mbcl_purge_cnt; /* # of purges so far */
1301*43a90889SApple OSS Distributions u_int64_t mbcl_fail_cnt; /* # of allocation failures */
1302*43a90889SApple OSS Distributions u_int32_t mbcl_ctotal; /* total only for this class */
1303*43a90889SApple OSS Distributions u_int32_t mbcl_release_cnt; /* amount of memory returned */
1304*43a90889SApple OSS Distributions /*
1305*43a90889SApple OSS Distributions * Cache layer statistics
1306*43a90889SApple OSS Distributions */
1307*43a90889SApple OSS Distributions u_int32_t mbcl_mc_state; /* cache state (see below) */
1308*43a90889SApple OSS Distributions u_int32_t mbcl_mc_cached; /* # of cached buffers */
1309*43a90889SApple OSS Distributions u_int32_t mbcl_mc_waiter_cnt; /* # waiters on the cache */
1310*43a90889SApple OSS Distributions u_int32_t mbcl_mc_wretry_cnt; /* # of wait retries */
1311*43a90889SApple OSS Distributions u_int32_t mbcl_mc_nwretry_cnt; /* # of no-wait retry attempts */
1312*43a90889SApple OSS Distributions u_int32_t mbcl_reserved[7]; /* for future use */
1313*43a90889SApple OSS Distributions } mb_class_stat_t;
1314*43a90889SApple OSS Distributions
1315*43a90889SApple OSS Distributions #define MCS_DISABLED 0 /* cache is permanently disabled */
1316*43a90889SApple OSS Distributions #define MCS_ONLINE 1 /* cache is online */
1317*43a90889SApple OSS Distributions #define MCS_PURGING 2 /* cache is being purged */
1318*43a90889SApple OSS Distributions #define MCS_OFFLINE 3 /* cache is offline (resizing) */
1319*43a90889SApple OSS Distributions
1320*43a90889SApple OSS Distributions #if defined(XNU_KERNEL_PRIVATE)
1321*43a90889SApple OSS Distributions #define MB_STAT_MAX_MB_CLASSES 8 /* Max number of distinct Mbuf classes. */
1322*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
1323*43a90889SApple OSS Distributions
1324*43a90889SApple OSS Distributions #if defined(XNU_KERNEL_PRIVATE)
1325*43a90889SApple OSS Distributions /* For backwards compatibility with 32-bit userland process */
1326*43a90889SApple OSS Distributions struct omb_stat {
1327*43a90889SApple OSS Distributions u_int32_t mbs_cnt; /* number of classes */
1328*43a90889SApple OSS Distributions u_int32_t mbs_pad; /* padding */
1329*43a90889SApple OSS Distributions struct omb_class_stat mbs_class[MB_STAT_MAX_MB_CLASSES]; /* class array */
1330*43a90889SApple OSS Distributions } __attribute__((__packed__));
1331*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
1332*43a90889SApple OSS Distributions
1333*43a90889SApple OSS Distributions typedef struct mb_stat {
1334*43a90889SApple OSS Distributions u_int32_t mbs_cnt; /* number of classes */
1335*43a90889SApple OSS Distributions #if defined(KERNEL) || defined(__LP64__)
1336*43a90889SApple OSS Distributions u_int32_t mbs_pad; /* padding */
1337*43a90889SApple OSS Distributions #endif /* KERNEL || __LP64__ */
1338*43a90889SApple OSS Distributions #if defined(XNU_KERNEL_PRIVATE)
1339*43a90889SApple OSS Distributions mb_class_stat_t mbs_class[MB_STAT_MAX_MB_CLASSES];
1340*43a90889SApple OSS Distributions #else /* XNU_KERNEL_PRIVATE */
1341*43a90889SApple OSS Distributions mb_class_stat_t mbs_class[1]; /* class array */
1342*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
1343*43a90889SApple OSS Distributions } mb_stat_t;
1344*43a90889SApple OSS Distributions
1345*43a90889SApple OSS Distributions #ifdef PRIVATE
1346*43a90889SApple OSS Distributions #define MLEAK_STACK_DEPTH 16 /* Max PC stack depth */
1347*43a90889SApple OSS Distributions
1348*43a90889SApple OSS Distributions typedef struct mleak_trace_stat {
1349*43a90889SApple OSS Distributions u_int64_t mltr_collisions;
1350*43a90889SApple OSS Distributions u_int64_t mltr_hitcount;
1351*43a90889SApple OSS Distributions u_int64_t mltr_allocs;
1352*43a90889SApple OSS Distributions u_int64_t mltr_depth;
1353*43a90889SApple OSS Distributions u_int64_t mltr_addr[MLEAK_STACK_DEPTH];
1354*43a90889SApple OSS Distributions } mleak_trace_stat_t;
1355*43a90889SApple OSS Distributions
1356*43a90889SApple OSS Distributions typedef struct mleak_stat {
1357*43a90889SApple OSS Distributions u_int32_t ml_isaddr64; /* 64-bit KVA? */
1358*43a90889SApple OSS Distributions u_int32_t ml_cnt; /* number of traces */
1359*43a90889SApple OSS Distributions mleak_trace_stat_t ml_trace[1]; /* trace array */
1360*43a90889SApple OSS Distributions } mleak_stat_t;
1361*43a90889SApple OSS Distributions
1362*43a90889SApple OSS Distributions struct mleak_table {
1363*43a90889SApple OSS Distributions u_int32_t mleak_capture; /* sampling capture counter */
1364*43a90889SApple OSS Distributions u_int32_t mleak_sample_factor; /* sample factor */
1365*43a90889SApple OSS Distributions
1366*43a90889SApple OSS Distributions /* Times two active records want to occupy the same spot */
1367*43a90889SApple OSS Distributions u_int64_t alloc_collisions;
1368*43a90889SApple OSS Distributions u_int64_t trace_collisions;
1369*43a90889SApple OSS Distributions
1370*43a90889SApple OSS Distributions /* Times new record lands on spot previously occupied by freed alloc */
1371*43a90889SApple OSS Distributions u_int64_t alloc_overwrites;
1372*43a90889SApple OSS Distributions u_int64_t trace_overwrites;
1373*43a90889SApple OSS Distributions
1374*43a90889SApple OSS Distributions /* Times a new alloc or trace is put into the hash table */
1375*43a90889SApple OSS Distributions u_int64_t alloc_recorded;
1376*43a90889SApple OSS Distributions u_int64_t trace_recorded;
1377*43a90889SApple OSS Distributions
1378*43a90889SApple OSS Distributions /* Total number of outstanding allocs */
1379*43a90889SApple OSS Distributions u_int64_t outstanding_allocs;
1380*43a90889SApple OSS Distributions
1381*43a90889SApple OSS Distributions /* Times mleak_log returned false because couldn't acquire the lock */
1382*43a90889SApple OSS Distributions u_int64_t total_conflicts;
1383*43a90889SApple OSS Distributions };
1384*43a90889SApple OSS Distributions
1385*43a90889SApple OSS Distributions #define HAS_M_TAG_STATS 1
1386*43a90889SApple OSS Distributions
1387*43a90889SApple OSS Distributions struct m_tag_stats {
1388*43a90889SApple OSS Distributions u_int32_t mts_id;
1389*43a90889SApple OSS Distributions u_int16_t mts_type;
1390*43a90889SApple OSS Distributions u_int16_t mts_len;
1391*43a90889SApple OSS Distributions u_int64_t mts_alloc_count;
1392*43a90889SApple OSS Distributions u_int64_t mts_alloc_failed;
1393*43a90889SApple OSS Distributions u_int64_t mts_free_count;
1394*43a90889SApple OSS Distributions };
1395*43a90889SApple OSS Distributions
1396*43a90889SApple OSS Distributions
1397*43a90889SApple OSS Distributions #define M_TAG_TYPE_NAMES \
1398*43a90889SApple OSS Distributions "other,dummynet,ipfilt,encap,inet6,ipsec,cfil_udp,pf_reass,aqm,drvaux"
1399*43a90889SApple OSS Distributions
1400*43a90889SApple OSS Distributions #endif /* PRIVATE */
1401*43a90889SApple OSS Distributions
1402*43a90889SApple OSS Distributions #ifdef KERNEL_PRIVATE
1403*43a90889SApple OSS Distributions __BEGIN_DECLS
1404*43a90889SApple OSS Distributions
1405*43a90889SApple OSS Distributions /*
1406*43a90889SApple OSS Distributions * Exported (private)
1407*43a90889SApple OSS Distributions */
1408*43a90889SApple OSS Distributions
1409*43a90889SApple OSS Distributions extern struct mbstat mbstat; /* statistics */
1410*43a90889SApple OSS Distributions
1411*43a90889SApple OSS Distributions __END_DECLS
1412*43a90889SApple OSS Distributions #endif /* KERNEL_PRIVATE */
1413*43a90889SApple OSS Distributions
1414*43a90889SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
1415*43a90889SApple OSS Distributions __BEGIN_DECLS
1416*43a90889SApple OSS Distributions
1417*43a90889SApple OSS Distributions /*
1418*43a90889SApple OSS Distributions * Not exported (xnu private)
1419*43a90889SApple OSS Distributions */
1420*43a90889SApple OSS Distributions
1421*43a90889SApple OSS Distributions /* flags to m_get/MGET */
1422*43a90889SApple OSS Distributions /* Need to include malloc.h to get right options for malloc */
1423*43a90889SApple OSS Distributions #include <sys/malloc.h>
1424*43a90889SApple OSS Distributions
1425*43a90889SApple OSS Distributions struct mbuf;
1426*43a90889SApple OSS Distributions
1427*43a90889SApple OSS Distributions /* length to m_copy to copy all */
1428*43a90889SApple OSS Distributions #define M_COPYALL 1000000000
1429*43a90889SApple OSS Distributions
1430*43a90889SApple OSS Distributions #define M_DONTWAIT M_NOWAIT
1431*43a90889SApple OSS Distributions #define M_WAIT M_WAITOK
1432*43a90889SApple OSS Distributions
1433*43a90889SApple OSS Distributions /* modes for m_copym and variants */
1434*43a90889SApple OSS Distributions #define M_COPYM_NOOP_HDR 0 /* don't copy/move pkthdr contents */
1435*43a90889SApple OSS Distributions #define M_COPYM_COPY_HDR 1 /* copy pkthdr from old to new */
1436*43a90889SApple OSS Distributions #define M_COPYM_MOVE_HDR 2 /* move pkthdr from old to new */
1437*43a90889SApple OSS Distributions #define M_COPYM_MUST_COPY_HDR 3 /* MUST copy pkthdr from old to new */
1438*43a90889SApple OSS Distributions #define M_COPYM_MUST_MOVE_HDR 4 /* MUST move pkthdr from old to new */
1439*43a90889SApple OSS Distributions
1440*43a90889SApple OSS Distributions extern void m_freem(struct mbuf *) __XNU_INTERNAL(m_freem);
1441*43a90889SApple OSS Distributions extern void m_drop(mbuf_t, uint16_t, uint32_t, const char *, uint16_t);
1442*43a90889SApple OSS Distributions extern void m_drop_if(mbuf_t, struct ifnet *, uint16_t, uint32_t, const char *, uint16_t);
1443*43a90889SApple OSS Distributions extern void m_drop_list(mbuf_t, struct ifnet *, uint16_t, uint32_t, const char *, uint16_t);
1444*43a90889SApple OSS Distributions extern u_int64_t mcl_to_paddr(char *);
1445*43a90889SApple OSS Distributions extern void m_adj(struct mbuf *, int);
1446*43a90889SApple OSS Distributions extern void m_cat(struct mbuf *, struct mbuf *);
1447*43a90889SApple OSS Distributions extern void m_copydata(struct mbuf *, int, int len, void * __sized_by(len));
1448*43a90889SApple OSS Distributions extern struct mbuf *m_copym(struct mbuf *, int, int, int);
1449*43a90889SApple OSS Distributions extern struct mbuf *m_copym_mode(struct mbuf *, int, int, int, struct mbuf **, int *, uint32_t);
1450*43a90889SApple OSS Distributions extern struct mbuf *m_get(int, int);
1451*43a90889SApple OSS Distributions extern struct mbuf *m_gethdr(int, int);
1452*43a90889SApple OSS Distributions extern struct mbuf *m_getpacket(void);
1453*43a90889SApple OSS Distributions extern struct mbuf *m_getpackets(int, int, int);
1454*43a90889SApple OSS Distributions extern struct mbuf *m_mclget(struct mbuf *, int);
1455*43a90889SApple OSS Distributions extern void *__unsafe_indexable m_mtod(struct mbuf *);
1456*43a90889SApple OSS Distributions extern struct mbuf *m_prepend_2(struct mbuf *, int, int, int);
1457*43a90889SApple OSS Distributions extern struct mbuf *m_pullup(struct mbuf *, int);
1458*43a90889SApple OSS Distributions extern struct mbuf *m_split(struct mbuf *, int, int);
1459*43a90889SApple OSS Distributions extern void m_mclfree(caddr_t p);
1460*43a90889SApple OSS Distributions extern bool mbuf_class_under_pressure(struct mbuf *m);
1461*43a90889SApple OSS Distributions
1462*43a90889SApple OSS Distributions /*
1463*43a90889SApple OSS Distributions * Accessors for the mbuf data range.
1464*43a90889SApple OSS Distributions * The "lower bound" is the start of the memory range that m->m_data is allowed
1465*43a90889SApple OSS Distributions * to point into. The "start" is where m->m_data points to; equivalent to the
1466*43a90889SApple OSS Distributions * late m_mtod. The end is where m->m_data + m->m_len points to. The upper bound
1467*43a90889SApple OSS Distributions * is the end of the memory range that m->m_data + m->m_len is allowed to point
1468*43a90889SApple OSS Distributions * into.
1469*43a90889SApple OSS Distributions * In a well-formed range, lower bound <= start <= end <= upper bound. An
1470*43a90889SApple OSS Distributions * ill-formed range always means a programming error.
1471*43a90889SApple OSS Distributions */
1472*43a90889SApple OSS Distributions __stateful_pure static inline caddr_t __header_bidi_indexable
m_mtod_lower_bound(struct mbuf * m)1473*43a90889SApple OSS Distributions m_mtod_lower_bound(struct mbuf *m)
1474*43a90889SApple OSS Distributions {
1475*43a90889SApple OSS Distributions return M_START(m);
1476*43a90889SApple OSS Distributions }
1477*43a90889SApple OSS Distributions
1478*43a90889SApple OSS Distributions __stateful_pure static inline caddr_t __header_bidi_indexable
m_mtod_current(struct mbuf * m)1479*43a90889SApple OSS Distributions m_mtod_current(struct mbuf *m)
1480*43a90889SApple OSS Distributions {
1481*43a90889SApple OSS Distributions caddr_t data = m_mtod_lower_bound(m);
1482*43a90889SApple OSS Distributions return data + (m->m_data - (uintptr_t)data);
1483*43a90889SApple OSS Distributions }
1484*43a90889SApple OSS Distributions
1485*43a90889SApple OSS Distributions __stateful_pure static inline caddr_t __header_bidi_indexable
m_mtod_end(struct mbuf * m)1486*43a90889SApple OSS Distributions m_mtod_end(struct mbuf *m)
1487*43a90889SApple OSS Distributions {
1488*43a90889SApple OSS Distributions return m_mtod_current(m) + m->m_len;
1489*43a90889SApple OSS Distributions }
1490*43a90889SApple OSS Distributions
1491*43a90889SApple OSS Distributions __stateful_pure static inline caddr_t __header_bidi_indexable
m_mtod_upper_bound(struct mbuf * m)1492*43a90889SApple OSS Distributions m_mtod_upper_bound(struct mbuf *m)
1493*43a90889SApple OSS Distributions {
1494*43a90889SApple OSS Distributions return m_mtod_lower_bound(m) + M_SIZE(m);
1495*43a90889SApple OSS Distributions }
1496*43a90889SApple OSS Distributions
1497*43a90889SApple OSS Distributions static inline bool
m_has_mtype(const struct mbuf * m,int mtype_flags)1498*43a90889SApple OSS Distributions m_has_mtype(const struct mbuf *m, int mtype_flags)
1499*43a90889SApple OSS Distributions {
1500*43a90889SApple OSS Distributions return (1 << m->m_type) & mtype_flags;
1501*43a90889SApple OSS Distributions }
1502*43a90889SApple OSS Distributions
1503*43a90889SApple OSS Distributions /*
1504*43a90889SApple OSS Distributions * On platforms which require strict alignment (currently for anything but
1505*43a90889SApple OSS Distributions * i386 or x86_64 or arm64), this macro checks whether the data pointer of an mbuf
1506*43a90889SApple OSS Distributions * is 32-bit aligned (this is the expected minimum alignment for protocol
1507*43a90889SApple OSS Distributions * headers), and assert otherwise.
1508*43a90889SApple OSS Distributions */
1509*43a90889SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
1510*43a90889SApple OSS Distributions #define MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(_m)
1511*43a90889SApple OSS Distributions #else /* !__i386__ && !__x86_64__ && !__arm64__ */
1512*43a90889SApple OSS Distributions #define MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(_m) do { \
1513*43a90889SApple OSS Distributions if (!IS_P2ALIGNED((_m)->m_data, sizeof (u_int32_t))) { \
1514*43a90889SApple OSS Distributions if (((_m)->m_flags & M_PKTHDR) && \
1515*43a90889SApple OSS Distributions (_m)->m_pkthdr.rcvif != NULL) { \
1516*43a90889SApple OSS Distributions panic_plain("\n%s: mbuf %p data ptr %p is not " \
1517*43a90889SApple OSS Distributions "32-bit aligned [%s: alignerrs=%lld]\n", \
1518*43a90889SApple OSS Distributions __func__, (_m), \
1519*43a90889SApple OSS Distributions (caddr_t __unsafe_indexable)(_m)->m_data, \
1520*43a90889SApple OSS Distributions if_name((_m)->m_pkthdr.rcvif), \
1521*43a90889SApple OSS Distributions (_m)->m_pkthdr.rcvif->if_alignerrs); \
1522*43a90889SApple OSS Distributions } else { \
1523*43a90889SApple OSS Distributions panic_plain("\n%s: mbuf %p data ptr %p is not " \
1524*43a90889SApple OSS Distributions "32-bit aligned\n", \
1525*43a90889SApple OSS Distributions __func__, (_m), \
1526*43a90889SApple OSS Distributions (caddr_t __unsafe_indexable)(_m)->m_data); \
1527*43a90889SApple OSS Distributions } \
1528*43a90889SApple OSS Distributions } \
1529*43a90889SApple OSS Distributions } while (0)
1530*43a90889SApple OSS Distributions #endif /* !__i386__ && !__x86_64__ && !__arm64__ */
1531*43a90889SApple OSS Distributions
1532*43a90889SApple OSS Distributions /* Maximum number of MBUF_SC values (excluding MBUF_SC_UNSPEC) */
1533*43a90889SApple OSS Distributions #define MBUF_SC_MAX_CLASSES 10
1534*43a90889SApple OSS Distributions
1535*43a90889SApple OSS Distributions /*
1536*43a90889SApple OSS Distributions * These conversion macros rely on the corresponding MBUF_SC and
1537*43a90889SApple OSS Distributions * MBUF_TC values in order to establish the following mapping:
1538*43a90889SApple OSS Distributions *
1539*43a90889SApple OSS Distributions * MBUF_SC_BK_SYS ] ==> MBUF_TC_BK
1540*43a90889SApple OSS Distributions * MBUF_SC_BK ]
1541*43a90889SApple OSS Distributions *
1542*43a90889SApple OSS Distributions * MBUF_SC_BE ] ==> MBUF_TC_BE
1543*43a90889SApple OSS Distributions * MBUF_SC_RD ]
1544*43a90889SApple OSS Distributions * MBUF_SC_OAM ]
1545*43a90889SApple OSS Distributions *
1546*43a90889SApple OSS Distributions * MBUF_SC_AV ] ==> MBUF_TC_VI
1547*43a90889SApple OSS Distributions * MBUF_SC_RV ]
1548*43a90889SApple OSS Distributions * MBUF_SC_VI ]
1549*43a90889SApple OSS Distributions * MBUF_SC_SIG ]
1550*43a90889SApple OSS Distributions *
1551*43a90889SApple OSS Distributions * MBUF_SC_VO ] ==> MBUF_TC_VO
1552*43a90889SApple OSS Distributions * MBUF_SC_CTL ]
1553*43a90889SApple OSS Distributions *
1554*43a90889SApple OSS Distributions * The values assigned to each service class allows for a fast mapping to
1555*43a90889SApple OSS Distributions * the corresponding MBUF_TC traffic class values, as well as to retrieve the
1556*43a90889SApple OSS Distributions * assigned index; therefore care must be taken when comparing against these
1557*43a90889SApple OSS Distributions * values. Use the corresponding class and index macros to retrieve the
1558*43a90889SApple OSS Distributions * corresponding portion, and never assume that a higher class corresponds
1559*43a90889SApple OSS Distributions * to a higher index.
1560*43a90889SApple OSS Distributions */
1561*43a90889SApple OSS Distributions #define MBUF_SCVAL(x) ((x) & 0xffff)
1562*43a90889SApple OSS Distributions #define MBUF_SCIDX(x) ((((x) >> 16) & 0xff) >> 3)
1563*43a90889SApple OSS Distributions #define MBUF_SC2TC(_sc) (MBUF_SCVAL(_sc) >> 7)
1564*43a90889SApple OSS Distributions #define MBUF_TC2SCVAL(_tc) ((_tc) << 7)
1565*43a90889SApple OSS Distributions #define IS_MBUF_SC_BACKGROUND(_sc) (((_sc) == MBUF_SC_BK_SYS) || \
1566*43a90889SApple OSS Distributions ((_sc) == MBUF_SC_BK))
1567*43a90889SApple OSS Distributions #define IS_MBUF_SC_REALTIME(_sc) ((_sc) >= MBUF_SC_AV && (_sc) <= MBUF_SC_VO)
1568*43a90889SApple OSS Distributions #define IS_MBUF_SC_BESTEFFORT(_sc) ((_sc) == MBUF_SC_BE || \
1569*43a90889SApple OSS Distributions (_sc) == MBUF_SC_RD || (_sc) == MBUF_SC_OAM)
1570*43a90889SApple OSS Distributions
1571*43a90889SApple OSS Distributions #define SCIDX_BK_SYS MBUF_SCIDX(MBUF_SC_BK_SYS)
1572*43a90889SApple OSS Distributions #define SCIDX_BK MBUF_SCIDX(MBUF_SC_BK)
1573*43a90889SApple OSS Distributions #define SCIDX_BE MBUF_SCIDX(MBUF_SC_BE)
1574*43a90889SApple OSS Distributions #define SCIDX_RD MBUF_SCIDX(MBUF_SC_RD)
1575*43a90889SApple OSS Distributions #define SCIDX_OAM MBUF_SCIDX(MBUF_SC_OAM)
1576*43a90889SApple OSS Distributions #define SCIDX_AV MBUF_SCIDX(MBUF_SC_AV)
1577*43a90889SApple OSS Distributions #define SCIDX_RV MBUF_SCIDX(MBUF_SC_RV)
1578*43a90889SApple OSS Distributions #define SCIDX_VI MBUF_SCIDX(MBUF_SC_VI)
1579*43a90889SApple OSS Distributions #define SCIDX_SIG MBUF_SCIDX(MBUF_SC_SIG)
1580*43a90889SApple OSS Distributions #define SCIDX_VO MBUF_SCIDX(MBUF_SC_VO)
1581*43a90889SApple OSS Distributions #define SCIDX_CTL MBUF_SCIDX(MBUF_SC_CTL)
1582*43a90889SApple OSS Distributions
1583*43a90889SApple OSS Distributions #define SCVAL_BK_SYS MBUF_SCVAL(MBUF_SC_BK_SYS)
1584*43a90889SApple OSS Distributions #define SCVAL_BK MBUF_SCVAL(MBUF_SC_BK)
1585*43a90889SApple OSS Distributions #define SCVAL_BE MBUF_SCVAL(MBUF_SC_BE)
1586*43a90889SApple OSS Distributions #define SCVAL_RD MBUF_SCVAL(MBUF_SC_RD)
1587*43a90889SApple OSS Distributions #define SCVAL_OAM MBUF_SCVAL(MBUF_SC_OAM)
1588*43a90889SApple OSS Distributions #define SCVAL_AV MBUF_SCVAL(MBUF_SC_AV)
1589*43a90889SApple OSS Distributions #define SCVAL_RV MBUF_SCVAL(MBUF_SC_RV)
1590*43a90889SApple OSS Distributions #define SCVAL_VI MBUF_SCVAL(MBUF_SC_VI)
1591*43a90889SApple OSS Distributions #define SCVAL_SIG MBUF_SCVAL(MBUF_SC_SIG)
1592*43a90889SApple OSS Distributions #define SCVAL_VO MBUF_SCVAL(MBUF_SC_VO)
1593*43a90889SApple OSS Distributions #define SCVAL_CTL MBUF_SCVAL(MBUF_SC_CTL)
1594*43a90889SApple OSS Distributions
1595*43a90889SApple OSS Distributions #define MBUF_VALID_SC(c) \
1596*43a90889SApple OSS Distributions (c == MBUF_SC_BK_SYS || c == MBUF_SC_BK || c == MBUF_SC_BE || \
1597*43a90889SApple OSS Distributions c == MBUF_SC_RD || c == MBUF_SC_OAM || c == MBUF_SC_AV || \
1598*43a90889SApple OSS Distributions c == MBUF_SC_RV || c == MBUF_SC_VI || c == MBUF_SC_SIG || \
1599*43a90889SApple OSS Distributions c == MBUF_SC_VO || c == MBUF_SC_CTL)
1600*43a90889SApple OSS Distributions
1601*43a90889SApple OSS Distributions #define MBUF_VALID_SCIDX(c) \
1602*43a90889SApple OSS Distributions (c == SCIDX_BK_SYS || c == SCIDX_BK || c == SCIDX_BE || \
1603*43a90889SApple OSS Distributions c == SCIDX_RD || c == SCIDX_OAM || c == SCIDX_AV || \
1604*43a90889SApple OSS Distributions c == SCIDX_RV || c == SCIDX_VI || c == SCIDX_SIG || \
1605*43a90889SApple OSS Distributions c == SCIDX_VO || c == SCIDX_CTL)
1606*43a90889SApple OSS Distributions
1607*43a90889SApple OSS Distributions #define MBUF_VALID_SCVAL(c) \
1608*43a90889SApple OSS Distributions (c == SCVAL_BK_SYS || c == SCVAL_BK || c == SCVAL_BE || \
1609*43a90889SApple OSS Distributions c == SCVAL_RD || c == SCVAL_OAM || c == SCVAL_AV || \
1610*43a90889SApple OSS Distributions c == SCVAL_RV || c == SCVAL_VI || c == SCVAL_SIG || \
1611*43a90889SApple OSS Distributions c == SCVAL_VO || SCVAL_CTL)
1612*43a90889SApple OSS Distributions
1613*43a90889SApple OSS Distributions extern unsigned char *mbutl; /* start VA of mbuf pool */
1614*43a90889SApple OSS Distributions extern unsigned char *embutl; /* end VA of mbuf pool */
1615*43a90889SApple OSS Distributions extern unsigned int nmbclusters; /* number of mapped clusters */
1616*43a90889SApple OSS Distributions extern int njcl; /* # of jumbo clusters */
1617*43a90889SApple OSS Distributions extern int njclbytes; /* size of a jumbo cluster */
1618*43a90889SApple OSS Distributions extern int max_hdr; /* largest link+protocol header */
1619*43a90889SApple OSS Distributions extern int max_datalen; /* MHLEN - max_hdr */
1620*43a90889SApple OSS Distributions
1621*43a90889SApple OSS Distributions extern int max_linkhdr; /* largest link-level header */
1622*43a90889SApple OSS Distributions
1623*43a90889SApple OSS Distributions /* Use max_protohdr instead of _max_protohdr */
1624*43a90889SApple OSS Distributions extern int max_protohdr; /* largest protocol header */
1625*43a90889SApple OSS Distributions
1626*43a90889SApple OSS Distributions __private_extern__ unsigned int mbuf_default_ncl(uint64_t);
1627*43a90889SApple OSS Distributions __private_extern__ void mbinit(void);
1628*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_clattach(struct mbuf *, int, caddr_t __sized_by(extsize),
1629*43a90889SApple OSS Distributions void (*)(caddr_t, u_int, caddr_t), size_t extsize, caddr_t, int, int);
1630*43a90889SApple OSS Distributions __private_extern__ char * __sized_by_or_null(MBIGCLBYTES) m_bigalloc(int);
1631*43a90889SApple OSS Distributions __private_extern__ void m_bigfree(caddr_t, u_int, caddr_t);
1632*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_mbigget(struct mbuf *, int);
1633*43a90889SApple OSS Distributions __private_extern__ char * __sized_by_or_null(M16KCLBYTES) m_16kalloc(int);
1634*43a90889SApple OSS Distributions __private_extern__ void m_16kfree(caddr_t, u_int, caddr_t);
1635*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_m16kget(struct mbuf *, int);
1636*43a90889SApple OSS Distributions __private_extern__ int m_reinit(struct mbuf *, int);
1637*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_free(struct mbuf *) __XNU_INTERNAL(m_free);
1638*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_getclr(int, int);
1639*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_getptr(struct mbuf *, int, int *);
1640*43a90889SApple OSS Distributions __private_extern__ unsigned int m_length(struct mbuf *);
1641*43a90889SApple OSS Distributions __private_extern__ unsigned int m_length2(struct mbuf *, struct mbuf **);
1642*43a90889SApple OSS Distributions __private_extern__ unsigned int m_fixhdr(struct mbuf *);
1643*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_defrag(struct mbuf *, int);
1644*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_defrag_offset(struct mbuf *, u_int32_t, int);
1645*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_prepend(struct mbuf *, int, int);
1646*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_copyup(struct mbuf *, int, int);
1647*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_retry(int, int);
1648*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_retryhdr(int, int);
1649*43a90889SApple OSS Distributions __private_extern__ int m_freem_list(struct mbuf *);
1650*43a90889SApple OSS Distributions __private_extern__ int m_append(struct mbuf *, int len, caddr_t __sized_by(len));
1651*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_last(struct mbuf *);
1652*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_pulldown(struct mbuf *, int, int, int *);
1653*43a90889SApple OSS Distributions
1654*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_getcl(int, int, int);
1655*43a90889SApple OSS Distributions __private_extern__ char * __sized_by_or_null(MCLBYTES) m_mclalloc(int);
1656*43a90889SApple OSS Distributions __private_extern__ int m_mclhasreference(struct mbuf *);
1657*43a90889SApple OSS Distributions __private_extern__ void m_copy_pkthdr(struct mbuf *, struct mbuf *);
1658*43a90889SApple OSS Distributions __private_extern__ int m_dup_pkthdr(struct mbuf *, struct mbuf *, int);
1659*43a90889SApple OSS Distributions __private_extern__ void m_copy_pftag(struct mbuf *, struct mbuf *);
1660*43a90889SApple OSS Distributions __private_extern__ void m_copy_necptag(struct mbuf *, struct mbuf *);
1661*43a90889SApple OSS Distributions __private_extern__ void m_copy_classifier(struct mbuf *, struct mbuf *);
1662*43a90889SApple OSS Distributions
1663*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_dtom(void *);
1664*43a90889SApple OSS Distributions __private_extern__ int m_mtocl(void *);
1665*43a90889SApple OSS Distributions __private_extern__ union mcluster *m_cltom(int);
1666*43a90889SApple OSS Distributions
1667*43a90889SApple OSS Distributions __private_extern__ void m_align(struct mbuf *, int);
1668*43a90889SApple OSS Distributions
1669*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_normalize(struct mbuf *m);
1670*43a90889SApple OSS Distributions __private_extern__ void m_mchtype(struct mbuf *m, int t);
1671*43a90889SApple OSS Distributions __private_extern__ void m_mcheck(struct mbuf *);
1672*43a90889SApple OSS Distributions
1673*43a90889SApple OSS Distributions __private_extern__ void m_copyback(struct mbuf *, int, int len, const void * __sized_by(len));
1674*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_copyback_cow(struct mbuf *, int, int len,
1675*43a90889SApple OSS Distributions const void * __sized_by(len), int);
1676*43a90889SApple OSS Distributions __private_extern__ int m_makewritable(struct mbuf **, int, int, int);
1677*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_dup(struct mbuf *m, int how);
1678*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_copym_with_hdrs(struct mbuf *, int, int, int,
1679*43a90889SApple OSS Distributions struct mbuf **, int *, uint32_t);
1680*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_getpackethdrs(int, int);
1681*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_getpacket_how(int);
1682*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_getpackets_internal(unsigned int *, int,
1683*43a90889SApple OSS Distributions int, int, size_t);
1684*43a90889SApple OSS Distributions __private_extern__ struct mbuf *m_allocpacket_internal(unsigned int *, size_t,
1685*43a90889SApple OSS Distributions unsigned int *, int, int, size_t);
1686*43a90889SApple OSS Distributions
1687*43a90889SApple OSS Distributions __private_extern__ int m_ext_set_prop(struct mbuf *, uint32_t, uint32_t);
1688*43a90889SApple OSS Distributions __private_extern__ uint32_t m_ext_get_prop(struct mbuf *);
1689*43a90889SApple OSS Distributions __private_extern__ int m_ext_paired_is_active(struct mbuf *);
1690*43a90889SApple OSS Distributions __private_extern__ void m_ext_paired_activate(struct mbuf *);
1691*43a90889SApple OSS Distributions
1692*43a90889SApple OSS Distributions __private_extern__ void m_add_crumb(struct mbuf *, uint16_t);
1693*43a90889SApple OSS Distributions __private_extern__ void m_add_hdr_crumb(struct mbuf *, uint64_t, uint64_t);
1694*43a90889SApple OSS Distributions __private_extern__ void m_add_hdr_crumb_chain(struct mbuf *, uint64_t, uint64_t);
1695*43a90889SApple OSS Distributions
1696*43a90889SApple OSS Distributions static inline void
m_add_hdr_crumb_interface_output(mbuf_t m,int index,bool chain)1697*43a90889SApple OSS Distributions m_add_hdr_crumb_interface_output(mbuf_t m, int index, bool chain)
1698*43a90889SApple OSS Distributions {
1699*43a90889SApple OSS Distributions if (chain) {
1700*43a90889SApple OSS Distributions m_add_hdr_crumb_chain(m, index, CRUMB_INTERFACE_FLAG);
1701*43a90889SApple OSS Distributions } else {
1702*43a90889SApple OSS Distributions m_add_hdr_crumb(m, index, CRUMB_INTERFACE_FLAG);
1703*43a90889SApple OSS Distributions }
1704*43a90889SApple OSS Distributions }
1705*43a90889SApple OSS Distributions
1706*43a90889SApple OSS Distributions static inline void
m_add_hdr_crumb_interface_input(mbuf_t m,int index,bool chain)1707*43a90889SApple OSS Distributions m_add_hdr_crumb_interface_input(mbuf_t m, int index, bool chain)
1708*43a90889SApple OSS Distributions {
1709*43a90889SApple OSS Distributions if (chain) {
1710*43a90889SApple OSS Distributions m_add_hdr_crumb_chain(m, index | CRUMB_INPUT_FLAG, CRUMB_INTERFACE_FLAG);
1711*43a90889SApple OSS Distributions } else {
1712*43a90889SApple OSS Distributions m_add_hdr_crumb(m, index | CRUMB_INPUT_FLAG, CRUMB_INTERFACE_FLAG);
1713*43a90889SApple OSS Distributions }
1714*43a90889SApple OSS Distributions }
1715*43a90889SApple OSS Distributions __private_extern__ void mbuf_drain(boolean_t);
1716*43a90889SApple OSS Distributions
1717*43a90889SApple OSS Distributions /*
1718*43a90889SApple OSS Distributions * Packets may have annotations attached by affixing a list of "packet
1719*43a90889SApple OSS Distributions * tags" to the pkthdr structure. Packet tags are dynamically allocated
1720*43a90889SApple OSS Distributions * semi-opaque data structures that have a fixed header (struct m_tag)
1721*43a90889SApple OSS Distributions * that specifies the size of the memory block and an <id,type> pair that
1722*43a90889SApple OSS Distributions * identifies it. The id identifies the module and the type identifies the
1723*43a90889SApple OSS Distributions * type of data for that module. The id of zero is reserved for the kernel.
1724*43a90889SApple OSS Distributions *
1725*43a90889SApple OSS Distributions * By default packet tags are allocated via kalloc except on Intel that still
1726*43a90889SApple OSS Distributions * uses the legacy implementation of using mbufs for packet tags.
1727*43a90889SApple OSS Distributions *
1728*43a90889SApple OSS Distributions * When kalloc is used for allocation, packet tags returned by m_tag_allocate have
1729*43a90889SApple OSS Distributions * the default memory alignment implemented by kalloc.
1730*43a90889SApple OSS Distributions *
1731*43a90889SApple OSS Distributions * When mbufs are used for allocation packets tag returned by m_tag_allocate has
1732*43a90889SApple OSS Distributions * the default memory alignment implemented by malloc.
1733*43a90889SApple OSS Distributions *
1734*43a90889SApple OSS Distributions * To reference the private data one should use a construct like:
1735*43a90889SApple OSS Distributions * struct m_tag *mtag = m_tag_allocate(...);
1736*43a90889SApple OSS Distributions * struct foo *p = (struct foo *)(mtag->m_tag_data);
1737*43a90889SApple OSS Distributions *
1738*43a90889SApple OSS Distributions * There should be no assumption on the location of the private data relative to the
1739*43a90889SApple OSS Distributions * 'struct m_tag'
1740*43a90889SApple OSS Distributions *
1741*43a90889SApple OSS Distributions * When kalloc is used, packet tags that are internal to xnu use KERNEL_MODULE_TAG_ID and
1742*43a90889SApple OSS Distributions * they are allocated with kalloc_type using a single container data structure that has
1743*43a90889SApple OSS Distributions * the 'struct m_tag' followed by a data structure for the private data
1744*43a90889SApple OSS Distributions *
1745*43a90889SApple OSS Distributions * Packet tags that are allocated by KEXTs are external to xnu and type of the private data
1746*43a90889SApple OSS Distributions * is unknown to xnu, so they are allocated in two chunks:
1747*43a90889SApple OSS Distributions * - one allocation with kalloc_type for the 'struct m_tag'
1748*43a90889SApple OSS Distributions * - one allocation using kheap_alloc as for the private data
1749*43a90889SApple OSS Distributions *
1750*43a90889SApple OSS Distributions * Note that packet tags of type KERNEL_TAG_TYPE_DRVAUX are allocated by KEXTs with
1751*43a90889SApple OSS Distributions * a variable length so they are allocated in two chunks
1752*43a90889SApple OSS Distributions *
1753*43a90889SApple OSS Distributions * In all cases the 'struct m_tag' is allocated using kalloc_type to avoid type
1754*43a90889SApple OSS Distributions * confusion.
1755*43a90889SApple OSS Distributions */
1756*43a90889SApple OSS Distributions
1757*43a90889SApple OSS Distributions #define KERNEL_MODULE_TAG_ID 0
1758*43a90889SApple OSS Distributions
1759*43a90889SApple OSS Distributions enum {
1760*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_NONE = 0,
1761*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_DUMMYNET = 1,
1762*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_IPFILT = 2,
1763*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_ENCAP = 3,
1764*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_INET6 = 4,
1765*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_IPSEC = 5,
1766*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_CFIL_UDP = 6,
1767*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_PF_REASS = 7,
1768*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_AQM = 8,
1769*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_DRVAUX = 9,
1770*43a90889SApple OSS Distributions KERNEL_TAG_TYPE_COUNT = 10
1771*43a90889SApple OSS Distributions };
1772*43a90889SApple OSS Distributions
1773*43a90889SApple OSS Distributions /* Packet tag routines */
1774*43a90889SApple OSS Distributions __private_extern__ struct m_tag *m_tag_alloc(u_int32_t, u_int16_t, int, int);
1775*43a90889SApple OSS Distributions __private_extern__ struct m_tag *m_tag_create(u_int32_t, u_int16_t, int, int,
1776*43a90889SApple OSS Distributions struct mbuf *);
1777*43a90889SApple OSS Distributions __private_extern__ void m_tag_free(struct m_tag *);
1778*43a90889SApple OSS Distributions __private_extern__ void m_tag_prepend(struct mbuf *, struct m_tag *);
1779*43a90889SApple OSS Distributions __private_extern__ void m_tag_unlink(struct mbuf *, struct m_tag *);
1780*43a90889SApple OSS Distributions __private_extern__ void m_tag_delete(struct mbuf *, struct m_tag *);
1781*43a90889SApple OSS Distributions __private_extern__ void m_tag_delete_chain(struct mbuf *);
1782*43a90889SApple OSS Distributions __private_extern__ struct m_tag *m_tag_locate(struct mbuf *, u_int32_t,
1783*43a90889SApple OSS Distributions u_int16_t);
1784*43a90889SApple OSS Distributions __private_extern__ struct m_tag *m_tag_copy(struct m_tag *, int);
1785*43a90889SApple OSS Distributions __private_extern__ int m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
1786*43a90889SApple OSS Distributions __private_extern__ void m_tag_init(struct mbuf *, int);
1787*43a90889SApple OSS Distributions __private_extern__ struct m_tag *m_tag_first(struct mbuf *);
1788*43a90889SApple OSS Distributions __private_extern__ struct m_tag *m_tag_next(struct mbuf *, struct m_tag *);
1789*43a90889SApple OSS Distributions
1790*43a90889SApple OSS Distributions typedef struct m_tag * (*m_tag_kalloc_func_t)(u_int32_t id, u_int16_t type, uint16_t len, int wait);
1791*43a90889SApple OSS Distributions typedef void (*m_tag_kfree_func_t)(struct m_tag *tag);
1792*43a90889SApple OSS Distributions
1793*43a90889SApple OSS Distributions int m_register_internal_tag_type(uint16_t type, uint16_t len, m_tag_kalloc_func_t alloc_func, m_tag_kfree_func_t free_func);
1794*43a90889SApple OSS Distributions void m_tag_create_cookie(struct m_tag *);
1795*43a90889SApple OSS Distributions
1796*43a90889SApple OSS Distributions void mbuf_tag_init(void);
1797*43a90889SApple OSS Distributions
1798*43a90889SApple OSS Distributions __private_extern__ void m_scratch_init(struct mbuf *);
1799*43a90889SApple OSS Distributions __private_extern__ u_int32_t m_scratch_get(struct mbuf *, u_int8_t **);
1800*43a90889SApple OSS Distributions
1801*43a90889SApple OSS Distributions __private_extern__ void m_classifier_init(struct mbuf *, uint32_t);
1802*43a90889SApple OSS Distributions
1803*43a90889SApple OSS Distributions __private_extern__ int m_set_service_class(struct mbuf *, mbuf_svc_class_t);
1804*43a90889SApple OSS Distributions __private_extern__ mbuf_svc_class_t m_get_service_class(struct mbuf *);
1805*43a90889SApple OSS Distributions __private_extern__ mbuf_svc_class_t m_service_class_from_idx(u_int32_t);
1806*43a90889SApple OSS Distributions __private_extern__ mbuf_svc_class_t m_service_class_from_val(u_int32_t);
1807*43a90889SApple OSS Distributions __private_extern__ int m_set_traffic_class(struct mbuf *, mbuf_traffic_class_t);
1808*43a90889SApple OSS Distributions __private_extern__ mbuf_traffic_class_t m_get_traffic_class(struct mbuf *);
1809*43a90889SApple OSS Distributions
1810*43a90889SApple OSS Distributions __private_extern__ struct m_tag *m_tag_alloc(u_int32_t, u_int16_t, int, int);
1811*43a90889SApple OSS Distributions __private_extern__ void mbuf_tag_init(void);
1812*43a90889SApple OSS Distributions
1813*43a90889SApple OSS Distributions #define ADDCARRY(_x) do { \
1814*43a90889SApple OSS Distributions while (((_x) >> 16) != 0) \
1815*43a90889SApple OSS Distributions (_x) = ((_x) >> 16) + ((_x) & 0xffff); \
1816*43a90889SApple OSS Distributions } while (0)
1817*43a90889SApple OSS Distributions
1818*43a90889SApple OSS Distributions __private_extern__ u_int16_t m_adj_sum16(struct mbuf *, u_int32_t,
1819*43a90889SApple OSS Distributions u_int32_t, u_int32_t, u_int32_t);
1820*43a90889SApple OSS Distributions __private_extern__ u_int16_t m_sum16(struct mbuf *, u_int32_t, u_int32_t);
1821*43a90889SApple OSS Distributions
1822*43a90889SApple OSS Distributions __private_extern__ void m_set_ext(struct mbuf *, struct ext_ref *,
1823*43a90889SApple OSS Distributions m_ext_free_func_t, caddr_t);
1824*43a90889SApple OSS Distributions __private_extern__ struct ext_ref *m_get_rfa(struct mbuf *);
1825*43a90889SApple OSS Distributions __private_extern__ m_ext_free_func_t m_get_ext_free(struct mbuf *);
1826*43a90889SApple OSS Distributions __private_extern__ caddr_t m_get_ext_arg(struct mbuf *);
1827*43a90889SApple OSS Distributions
1828*43a90889SApple OSS Distributions __private_extern__ void m_do_tx_compl_callback(struct mbuf *, struct ifnet *);
1829*43a90889SApple OSS Distributions __private_extern__ mbuf_tx_compl_func m_get_tx_compl_callback(u_int32_t);
1830*43a90889SApple OSS Distributions
1831*43a90889SApple OSS Distributions
1832*43a90889SApple OSS Distributions
1833*43a90889SApple OSS Distributions __END_DECLS
1834*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
1835*43a90889SApple OSS Distributions #endif /* !_SYS_MBUF_H_ */
1836