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