]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/mbuf.h
xnu-4570.31.3.tar.gz
[apple/xnu.git] / bsd / sys / mbuf.h
CommitLineData
1c79356b 1/*
5ba3f43e 2 * Copyright (c) 1999-2017 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6d2010ae 5 *
2d21ac55
A
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.
6d2010ae 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
6d2010ae 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
6d2010ae 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
29/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
6d2010ae 30/*
1c79356b
A
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
1c79356b 71 */
2d21ac55
A
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 */
1c79356b
A
78
79#ifndef _SYS_MBUF_H_
80#define _SYS_MBUF_H_
81
91447636 82#include <sys/cdefs.h>
9bccf70c 83#include <sys/appleapiopts.h>
5ba3f43e
A
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 */
91447636 87
6d2010ae 88#ifdef XNU_KERNEL_PRIVATE
91447636 89
1c79356b 90#include <sys/lock.h>
91447636 91#include <sys/queue.h>
39236c6e 92#include <machine/endian.h>
1c79356b
A
93/*
94 * Mbufs are of a single size, MSIZE (machine/param.h), which
95 * includes overhead. An mbuf may add a single "mbuf cluster" of size
6d2010ae
A
96 * MCLBYTES/MBIGCLBYTES/M16KCLBYTES (also in machine/param.h), which has
97 * no additional overhead and is used instead of the internal data area;
98 * this is done when at least MINCLSIZE of data must be stored.
b0d623f7 99 */
b0d623f7
A
100
101/*
102 * The following _MLEN and _MHLEN macros are private to xnu. Private code
103 * that are outside of xnu must use the mbuf_get_{mlen,mhlen} routines since
104 * the sizes of the structures are dependent upon specific xnu configs.
105 */
106#define _MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
107#define _MHLEN (_MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
1c79356b 108
3e170ce0
A
109#define NMBPGSHIFT (PAGE_SHIFT - MSIZESHIFT)
110#define NMBPG (1 << NMBPGSHIFT) /* # of mbufs per page */
1c79356b 111
3e170ce0
A
112#define NCLPGSHIFT (PAGE_SHIFT - MCLSHIFT)
113#define NCLPG (1 << NCLPGSHIFT) /* # of cl per page */
6d2010ae 114
3e170ce0
A
115#define NBCLPGSHIFT (PAGE_SHIFT - MBIGCLSHIFT)
116#define NBCLPG (1 << NBCLPGSHIFT) /* # of big cl per page */
117
118#define NMBPCLSHIFT (MCLSHIFT - MSIZESHIFT)
6d2010ae
A
119#define NMBPCL (1 << NMBPCLSHIFT) /* # of mbufs per cl */
120
3e170ce0 121#define NCLPJCLSHIFT (M16KCLSHIFT - MCLSHIFT)
6d2010ae 122#define NCLPJCL (1 << NCLPJCLSHIFT) /* # of cl per jumbo cl */
1c79356b 123
3e170ce0
A
124#define NCLPBGSHIFT (MBIGCLSHIFT - MCLSHIFT)
125#define NCLPBG (1 << NCLPBGSHIFT) /* # of cl per big cl */
126
127#define NMBPBGSHIFT (MBIGCLSHIFT - MSIZESHIFT)
128#define NMBPBG (1 << NMBPBGSHIFT) /* # of mbufs per big cl */
129
1c79356b
A
130/*
131 * Macros for type conversion
132 * mtod(m,t) - convert mbuf pointer to data pointer of correct type
5ba3f43e 133 * mtodo(m, o) -- Same as above but with offset 'o' into data.
1c79356b 134 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX)
1c79356b 135 */
6d2010ae 136#define mtod(m, t) ((t)m_mtod(m))
5ba3f43e 137#define mtodo(m, o) ((void *)(mtod(m, uint8_t *) + (o)))
6d2010ae 138#define dtom(x) m_dtom(x)
1c79356b
A
139
140/* header at beginning of each mbuf: */
141struct m_hdr {
39236c6e
A
142 struct mbuf *mh_next; /* next buffer in chain */
143 struct mbuf *mh_nextpkt; /* next chain in queue/record */
144 caddr_t mh_data; /* location of data */
145 int32_t mh_len; /* amount of data in this mbuf */
146 u_int16_t mh_type; /* type of data in this mbuf */
147 u_int16_t mh_flags; /* flags; see below */
5ba3f43e
A
148#if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
149/* This is needed because of how _MLEN is defined and used. Ideally, _MLEN
150 * should be defined using the offsetof(struct mbuf, M_dat), since there is
151 * no guarantee that mbuf.M_dat will start where mbuf.m_hdr ends. The compiler
152 * may (and does in the armv7k case) insert padding between m_hdr and M_dat in
153 * mbuf. We cannot easily use offsetof, however, since _MLEN is referenced
154 * in the definition of mbuf.
155 */
156} __attribute__((aligned(8)));
157#else
1c79356b 158};
5ba3f43e 159#endif
1c79356b 160
91447636
A
161/*
162 * Packet tag structure (see below for details).
163 */
164struct m_tag {
6d2010ae
A
165 u_int64_t m_tag_cookie; /* Error checking */
166#ifndef __LP64__
167 u_int32_t pad; /* For structure alignment */
168#endif /* !__LP64__ */
91447636 169 SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */
6d2010ae
A
170 u_int16_t m_tag_type; /* Module specific type */
171 u_int16_t m_tag_len; /* Length of data */
172 u_int32_t m_tag_id; /* Module ID */
173};
174
6d2010ae 175#define M_TAG_ALIGN(len) \
316670eb 176 (P2ROUNDUP(len, sizeof (u_int64_t)) + sizeof (struct m_tag))
6d2010ae
A
177
178#define M_TAG_VALID_PATTERN 0xfeedfacefeedfaceULL
179#define M_TAG_FREE_PATTERN 0xdeadbeefdeadbeefULL
180
316670eb
A
181/*
182 * Packet tag header structure (at the top of mbuf). Pointers are
183 * 32-bit in ILP32; m_tag needs 64-bit alignment, hence padded.
184 */
6d2010ae 185struct m_taghdr {
316670eb
A
186#ifndef __LP64__
187 u_int32_t pad; /* For structure alignment */
188#endif /* !__LP64__ */
6d2010ae 189 u_int64_t refcnt; /* Number of tags in this mbuf */
91447636
A
190};
191
39236c6e
A
192/*
193 * Driver auxiliary metadata tag (KERNEL_TAG_TYPE_DRVAUX).
194 */
195struct m_drvaux_tag {
196 u_int32_t da_family; /* IFNET_FAMILY values */
197 u_int32_t da_subfamily; /* IFNET_SUBFAMILY values */
198 u_int32_t da_reserved; /* for future */
199 u_int32_t da_length; /* length of following data */
200};
316670eb 201
39236c6e
A
202/* Values for pftag_flags (16-bit wide) */
203#define PF_TAG_GENERATED 0x1 /* pkt generated by PF */
204#define PF_TAG_FRAGCACHE 0x2
205#define PF_TAG_TRANSLATE_LOCALHOST 0x4
206#if PF_ECN
207#define PF_TAG_HDR_INET 0x8 /* hdr points to IPv4 */
208#define PF_TAG_HDR_INET6 0x10 /* hdr points to IPv6 */
209#endif /* PF_ECN */
210/*
211 * PF mbuf tag
212 */
316670eb 213struct pf_mtag {
39236c6e
A
214 u_int16_t pftag_flags; /* PF_TAG flags */
215 u_int16_t pftag_rtableid; /* alternate routing table id */
216 u_int16_t pftag_tag;
217 u_int16_t pftag_routed;
39236c6e 218#if PF_ECN
316670eb 219 void *pftag_hdr; /* saved hdr pos in mbuf, for ECN */
39236c6e
A
220#endif /* PF_ECN */
221};
222
223/*
224 * TCP mbuf tag
225 */
226struct tcp_pktinfo {
316670eb
A
227 union {
228 struct {
39236c6e 229 u_int32_t segsz; /* segment size (actual MSS) */
39037602 230 u_int32_t start_seq; /* start seq of this packet */
39236c6e
A
231 } __tx;
232 struct {
233 u_int16_t lro_pktlen; /* max seg size encountered */
234 u_int8_t lro_npkts; /* # of coalesced TCP pkts */
235 u_int8_t lro_timediff; /* time spent in LRO */
236 } __rx;
237 } __offload;
238 union {
239 u_int32_t pri; /* send msg priority */
240 u_int32_t seq; /* recv msg sequence # */
241 } __msgattr;
242#define tso_segsz proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.segsz
39037602 243#define tx_start_seq proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.start_seq
39236c6e
A
244#define lro_pktlen proto_mtag.__pr_u.tcp.tm_tcp.__offload.__rx.lro_pktlen
245#define lro_npkts proto_mtag.__pr_u.tcp.tm_tcp.__offload.__rx.lro_npkts
246#define lro_elapsed proto_mtag.__pr_u.tcp.tm_tcp.__offload.__rx.lro_timediff
247#define msg_pri proto_mtag.__pr_u.tcp.tm_tcp.__msgattr.pri
248#define msg_seq proto_mtag.__pr_u.tcp.tm_tcp.__msgattr.seq
316670eb
A
249};
250
39236c6e
A
251/*
252 * MPTCP mbuf tag
253 */
254struct mptcp_pktinfo {
255 u_int64_t mtpi_dsn; /* MPTCP Data Sequence Number */
39037602 256 u_int32_t mtpi_rel_seq; /* Relative Seq Number */
5ba3f43e
A
257 u_int16_t mtpi_length; /* Length of mapping */
258 u_int16_t mtpi_csum;
39236c6e 259#define mp_dsn proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_dsn
39037602
A
260#define mp_rseq proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_rel_seq
261#define mp_rlen proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_length
5ba3f43e 262#define mp_csum proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_csum
39236c6e
A
263};
264
265/*
266 * TCP specific mbuf tag. Note that the current implementation uses
267 * MPTCP metadata strictly between MPTCP and the TCP subflow layers,
268 * hence tm_tcp and tm_mptcp are mutually exclusive. This also means
269 * that TCP messages functionality is currently incompatible with MPTCP.
270 */
316670eb 271struct tcp_mtag {
39236c6e
A
272 union {
273 struct tcp_pktinfo tm_tcp; /* TCP and below */
274 struct mptcp_pktinfo tm_mptcp; /* MPTCP-TCP only */
275 };
276};
277
39037602
A
278struct driver_mtag_ {
279 uintptr_t _drv_tx_compl_arg;
280 uintptr_t _drv_tx_compl_data;
281 kern_return_t _drv_tx_status;
282 uint16_t _drv_flowid;
283#define drv_tx_compl_arg builtin_mtag._drv_mtag._drv_tx_compl_arg
284#define drv_tx_compl_data builtin_mtag._drv_mtag._drv_tx_compl_data
285#define drv_tx_status builtin_mtag._drv_mtag._drv_tx_status
286#define drv_flowid builtin_mtag._drv_mtag._drv_flowid
287};
288
39236c6e
A
289/*
290 * Protocol specific mbuf tag (at most one protocol metadata per mbuf).
291 *
292 * Care must be taken to ensure that they are mutually exclusive, e.g.
293 * IPSec policy ID implies no TCP segment offload (which is fine given
294 * that the former is used on the virtual ipsec interface that does
295 * not advertise the TSO capability.)
296 */
39037602 297struct proto_mtag_ {
39236c6e
A
298 union {
299 struct tcp_mtag tcp; /* TCP specific */
39236c6e
A
300 } __pr_u;
301};
302
fe8ab488
A
303/*
304 * NECP specific mbuf tag.
305 */
39037602 306struct necp_mtag_ {
3e170ce0
A
307 u_int32_t necp_policy_id;
308 u_int32_t necp_last_interface_index;
309 u_int32_t necp_route_rule_id;
39037602
A
310 u_int32_t necp_app_id;
311};
312
313union builtin_mtag {
314 struct {
315 struct proto_mtag_ _proto_mtag; /* built-in protocol-specific tag */
316 struct pf_mtag _pf_mtag; /* built-in PF tag */
317 struct necp_mtag_ _necp_mtag; /* built-in NECP tag */
318 } _net_mtag;
319 struct driver_mtag_ _drv_mtag;
320#define necp_mtag builtin_mtag._net_mtag._necp_mtag
321#define proto_mtag builtin_mtag._net_mtag._proto_mtag
322#define driver_mtag builtin_mtag._drv_mtag
fe8ab488
A
323};
324
39236c6e
A
325/*
326 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR set.
327 */
39037602
A
328struct pkthdr {
329 struct ifnet *rcvif; /* rcv interface */
1c79356b 330 /* variables for ip and tcp reassembly */
39236c6e
A
331 void *pkt_hdr; /* pointer to packet header */
332 int32_t len; /* total packet length */
6d2010ae
A
333 /* variables for hardware checksum */
334 /* Note: csum_flags is used for hardware checksum and VLAN */
39236c6e
A
335 u_int32_t csum_flags; /* flags regarding checksum */
336 union {
337 struct {
338 u_int16_t val; /* checksum value */
339 u_int16_t start; /* checksum start offset */
340 } _csum_rx;
341#define csum_rx_val _csum_rx.val
342#define csum_rx_start _csum_rx.start
343 struct {
344 u_int16_t start; /* checksum start offset */
345 u_int16_t stuff; /* checksum stuff offset */
346 } _csum_tx;
347#define csum_tx_start _csum_tx.start
348#define csum_tx_stuff _csum_tx.stuff
349 u_int32_t csum_data; /* data field used by csum routines */
350 };
351 u_int16_t vlan_tag; /* VLAN tag, host byte order */
352 /*
353 * Packet classifier info
354 *
355 * PKTF_FLOW_ID set means valid flow ID. A non-zero flow ID value
356 * means the packet has been classified by one of the flow sources.
357 * It is also a prerequisite for flow control advisory, which is
358 * enabled by additionally setting PKTF_FLOW_ADV.
359 *
360 * The protocol value is a best-effort representation of the payload.
361 * It is opportunistically updated and used only for optimization.
362 * It is not a substitute for parsing the protocol header(s); use it
363 * only as a hint.
364 *
365 * If PKTF_IFAINFO is set, pkt_ifainfo contains one or both of the
366 * indices of interfaces which own the source and/or destination
367 * addresses of the packet. For the local/loopback case (PKTF_LOOP),
368 * both should be valid, and thus allows for the receiving end to
369 * quickly determine the actual interfaces used by the the addresses;
370 * they may not necessarily be the same or refer to the loopback
371 * interface. Otherwise, in the non-local/loopback case, the indices
372 * are opportunistically set, and because of that only one may be set
373 * (0 means the index has not been determined.) In addition, the
374 * interface address flags are also recorded. This allows us to avoid
375 * storing the corresponding {in,in6}_ifaddr in an mbuf tag. Ideally
376 * this would be a superset of {ia,ia6}_flags, but the namespaces are
377 * overlapping at present, so we'll need a new set of values in future
378 * to achieve this. For now, we will just rely on the address family
379 * related code paths examining this mbuf to interpret the flags.
380 */
381 u_int8_t pkt_proto; /* IPPROTO value */
382 u_int8_t pkt_flowsrc; /* FLOWSRC values */
383 u_int32_t pkt_flowid; /* flow ID */
384 u_int32_t pkt_flags; /* PKTF flags (see below) */
385 u_int32_t pkt_svc; /* MBUF_SVC value */
39037602
A
386
387 u_int32_t pkt_compl_context; /* Packet completion context */
388
39236c6e
A
389 union {
390 struct {
391 u_int16_t src; /* ifindex of src addr i/f */
392 u_int16_t src_flags; /* src PKT_IFAIFF flags */
393 u_int16_t dst; /* ifindex of dst addr i/f */
394 u_int16_t dst_flags; /* dst PKT_IFAIFF flags */
395 } _pkt_iaif;
396#define src_ifindex _pkt_iaif.src
397#define src_iff _pkt_iaif.src_flags
398#define dst_ifindex _pkt_iaif.dst
399#define dst_iff _pkt_iaif.dst_flags
400 u_int64_t pkt_ifainfo; /* data field used by ifainfo */
39037602
A
401 struct {
402 u_int32_t if_data; /* bytes in interface queue */
403 u_int32_t sndbuf_data; /* bytes in socket buffer */
404 } _pkt_bsr; /* Buffer status report used by cellular interface */
405#define bufstatus_if _pkt_bsr.if_data
406#define bufstatus_sndbuf _pkt_bsr.sndbuf_data
39236c6e 407 };
39037602 408 u_int64_t pkt_timestamp; /* enqueue time */
3e170ce0 409
39236c6e
A
410 /*
411 * Tags (external and built-in)
412 */
413 SLIST_HEAD(packet_tags, m_tag) tags; /* list of external tags */
39037602 414 union builtin_mtag builtin_mtag;
39236c6e
A
415 /*
416 * Module private scratch space (32-bit aligned), currently 16-bytes
39037602 417 * large. Anything stored here is not guaranteed to survive across
5ba3f43e
A
418 * modules. The AQM layer (outbound) uses all 16-bytes for both
419 * packet scheduling and flow advisory information.
39236c6e
A
420 */
421 struct {
422 union {
423 u_int8_t __mpriv8[16];
424 u_int16_t __mpriv16[8];
425 struct {
426 union {
427 u_int8_t __val8[4];
428 u_int16_t __val16[2];
429 u_int32_t __val32;
430 } __mpriv32_u;
431 } __mpriv32[4];
432 u_int64_t __mpriv64[2];
433 } __mpriv_u;
434 } pkt_mpriv __attribute__((aligned(4)));
5ba3f43e
A
435#define pkt_mpriv_hash pkt_mpriv.__mpriv_u.__mpriv32[0].__mpriv32_u.__val32
436#define pkt_mpriv_flags pkt_mpriv.__mpriv_u.__mpriv32[1].__mpriv32_u.__val32
437#define pkt_mpriv_srcid pkt_mpriv.__mpriv_u.__mpriv32[2].__mpriv32_u.__val32
438#define pkt_mpriv_fidx pkt_mpriv.__mpriv_u.__mpriv32[3].__mpriv32_u.__val32
439
39236c6e 440 u_int32_t redzone; /* red zone */
39037602 441 u_int32_t pkt_compl_callbacks; /* Packet completion callbacks */
1c79356b
A
442};
443
39236c6e
A
444/*
445 * Flow data source type. A data source module is responsible for generating
446 * a unique flow ID and associating it to each data flow as pkt_flowid.
447 * This is required for flow control/advisory, as it allows the output queue
448 * to identify the data source object and inform that it can resume its
449 * transmission (in the event it was flow controlled.)
450 */
451#define FLOWSRC_INPCB 1 /* flow ID generated by INPCB */
452#define FLOWSRC_IFNET 2 /* flow ID generated by interface */
453#define FLOWSRC_PF 3 /* flow ID generated by PF */
39037602 454#define FLOWSRC_CHANNEL 4 /* flow ID generated by channel */
39236c6e
A
455
456/*
457 * Packet flags. Unlike m_flags, all packet flags are copied along when
458 * copying m_pkthdr, i.e. no equivalent of M_COPYFLAGS here. These flags
459 * (and other classifier info) will be cleared during DLIL input.
460 *
461 * Some notes about M_LOOP and PKTF_LOOP:
462 *
463 * - M_LOOP flag is overloaded, and its use is discouraged. Historically,
464 * that flag was used by the KAME implementation for allowing certain
465 * certain exceptions to be made in the IP6_EXTHDR_CHECK() logic; this
466 * was originally meant to be set as the packet is looped back to the
467 * system, and in some circumstances temporarily set in ip6_output().
468 * Over time, this flag was used by the pre-output routines to indicate
469 * to the DLIL frameout and output routines, that the packet may be
470 * looped back to the system under the right conditions. In addition,
471 * this is an mbuf flag rather than an mbuf packet header flag.
472 *
473 * - PKTF_LOOP is an mbuf packet header flag, which is set if and only
474 * if the packet was looped back to the system. This flag should be
475 * used instead for newer code.
476 */
477#define PKTF_FLOW_ID 0x1 /* pkt has valid flowid value */
478#define PKTF_FLOW_ADV 0x2 /* pkt triggers local flow advisory */
479#define PKTF_FLOW_LOCALSRC 0x4 /* pkt is locally originated */
480#define PKTF_FLOW_RAWSOCK 0x8 /* pkt locally generated by raw sock */
481#define PKTF_PRIO_PRIVILEGED 0x10 /* packet priority is privileged */
482#define PKTF_PROXY_DST 0x20 /* processed but not locally destined */
483#define PKTF_INET_RESOLVE 0x40 /* IPv4 resolver packet */
484#define PKTF_INET6_RESOLVE 0x80 /* IPv6 resolver packet */
485#define PKTF_RESOLVE_RTR 0x100 /* pkt is for resolving router */
486#define PKTF_SW_LRO_PKT 0x200 /* pkt is a large coalesced pkt */
487#define PKTF_SW_LRO_DID_CSUM 0x400 /* IP and TCP checksums done by LRO */
488#define PKTF_MPTCP 0x800 /* TCP with MPTCP metadata */
489#define PKTF_MPSO 0x1000 /* MPTCP socket meta data */
490#define PKTF_LOOP 0x2000 /* loopbacked packet */
491#define PKTF_IFAINFO 0x4000 /* pkt has valid interface addr info */
492#define PKTF_SO_BACKGROUND 0x8000 /* data is from background source */
493#define PKTF_FORWARDED 0x10000 /* pkt was forwarded from another i/f */
fe8ab488
A
494#define PKTF_PRIV_GUARDED 0x20000 /* pkt_mpriv area guard enabled */
495#define PKTF_KEEPALIVE 0x40000 /* pkt is kernel-generated keepalive */
3e170ce0
A
496#define PKTF_SO_REALTIME 0x80000 /* data is realtime traffic */
497#define PKTF_VALID_UNSENT_DATA 0x100000 /* unsent data is valid */
498#define PKTF_TCP_REXMT 0x200000 /* packet is TCP retransmission */
39037602
A
499#define PKTF_REASSEMBLED 0x400000 /* Packet was reassembled */
500#define PKTF_TX_COMPL_TS_REQ 0x800000 /* tx completion timestamp requested */
5ba3f43e 501#define PKTF_TS_VALID 0x1000000 /* pkt timestamp is valid */
39037602
A
502#define PKTF_DRIVER_MTAG 0x2000000 /* driver mbuf tags fields inited */
503#define PKTF_NEW_FLOW 0x4000000 /* Data from a new flow */
504#define PKTF_START_SEQ 0x8000000 /* valid start sequence */
505#define PKTF_LAST_PKT 0x10000000 /* last packet in the flow */
5ba3f43e 506#define PKTF_MPTCP_REINJ 0x20000000 /* Packet has been reinjected for MPTCP */
5c9f4661 507#define PKTF_MPTCP_DFIN 0x40000000 /* Packet is a data-fin */
3e170ce0 508
39236c6e
A
509/* flags related to flow control/advisory and identification */
510#define PKTF_FLOW_MASK \
511 (PKTF_FLOW_ID | PKTF_FLOW_ADV | PKTF_FLOW_LOCALSRC | PKTF_FLOW_RAWSOCK)
512
513/*
514 * Description of external storage mapped into mbuf, valid only if M_EXT set.
515 */
813fb2f6 516typedef void (*m_ext_free_func_t)(caddr_t, u_int, caddr_t);
1c79356b
A
517struct m_ext {
518 caddr_t ext_buf; /* start of buffer */
813fb2f6 519 m_ext_free_func_t ext_free; /* free routine if not the usual */
1c79356b
A
520 u_int ext_size; /* size of buffer, for ext_free */
521 caddr_t ext_arg; /* additional ext_free argument */
2d21ac55 522 struct ext_ref {
39037602
A
523 struct mbuf *paired;
524 u_int16_t minref;
525 u_int16_t refcnt;
526 u_int16_t prefcnt;
527 u_int16_t flags;
528 u_int32_t priv;
813fb2f6 529 uintptr_t ext_token;
2d21ac55 530 } *ext_refflags;
1c79356b
A
531};
532
b0d623f7
A
533/* define m_ext to a type since it gets redefined below */
534typedef struct m_ext _m_ext_t;
535
39236c6e
A
536/*
537 * The mbuf object
538 */
1c79356b 539struct mbuf {
39037602 540 struct m_hdr m_hdr;
1c79356b
A
541 union {
542 struct {
39037602 543 struct pkthdr MH_pkthdr; /* M_PKTHDR set */
1c79356b 544 union {
39037602 545 struct m_ext MH_ext; /* M_EXT set */
b0d623f7 546 char MH_databuf[_MHLEN];
1c79356b
A
547 } MH_dat;
548 } MH;
b0d623f7 549 char M_databuf[_MLEN]; /* !M_PKTHDR, !M_EXT */
1c79356b
A
550 } M_dat;
551};
552
553#define m_next m_hdr.mh_next
554#define m_len m_hdr.mh_len
555#define m_data m_hdr.mh_data
556#define m_type m_hdr.mh_type
557#define m_flags m_hdr.mh_flags
558#define m_nextpkt m_hdr.mh_nextpkt
559#define m_act m_nextpkt
560#define m_pkthdr M_dat.MH.MH_pkthdr
561#define m_ext M_dat.MH.MH_dat.MH_ext
562#define m_pktdat M_dat.MH.MH_dat.MH_databuf
563#define m_dat M_dat.M_databuf
316670eb 564#define m_pktlen(_m) ((_m)->m_pkthdr.len)
39037602 565#define m_pftag(_m) (&(_m)->m_pkthdr.builtin_mtag._net_mtag._pf_mtag)
1c79356b 566
6d2010ae 567/* mbuf flags (private) */
1c79356b
A
568#define M_EXT 0x0001 /* has associated external storage */
569#define M_PKTHDR 0x0002 /* start of record */
570#define M_EOR 0x0004 /* end of record */
571#define M_PROTO1 0x0008 /* protocol-specific */
9bccf70c
A
572#define M_PROTO2 0x0010 /* protocol-specific */
573#define M_PROTO3 0x0020 /* protocol-specific */
39236c6e 574#define M_LOOP 0x0040 /* packet is looped back (also see PKTF_LOOP) */
9bccf70c 575#define M_PROTO5 0x0080 /* protocol-specific */
1c79356b 576
6d2010ae 577/* mbuf pkthdr flags, also in m_flags (private) */
1c79356b
A
578#define M_BCAST 0x0100 /* send/received as link-level broadcast */
579#define M_MCAST 0x0200 /* send/received as link-level multicast */
9bccf70c
A
580#define M_FRAG 0x0400 /* packet is a fragment of a larger packet */
581#define M_FIRSTFRAG 0x0800 /* packet is first fragment */
582#define M_LASTFRAG 0x1000 /* packet is last fragment */
91447636 583#define M_PROMISC 0x2000 /* packet is promiscuous (shouldn't go to stack) */
6d2010ae
A
584#define M_HASFCS 0x4000 /* packet has FCS */
585#define M_TAGHDR 0x8000 /* m_tag hdr structure at top of mbuf data */
586
587/*
588 * Flags to purge when crossing layers.
589 */
590#define M_PROTOFLAGS \
591 (M_PROTO1|M_PROTO2|M_PROTO3|M_PROTO5)
1c79356b
A
592
593/* flags copied when copying m_pkthdr */
6d2010ae
A
594#define M_COPYFLAGS \
595 (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO2|M_PROTO3 | \
596 M_LOOP|M_PROTO5|M_BCAST|M_MCAST|M_FRAG | \
597 M_FIRSTFRAG|M_LASTFRAG|M_PROMISC|M_HASFCS)
598
316670eb 599/* flags indicating hw checksum support and sw checksum requirements */
6d2010ae
A
600#define CSUM_IP 0x0001 /* will csum IP */
601#define CSUM_TCP 0x0002 /* will csum TCP */
602#define CSUM_UDP 0x0004 /* will csum UDP */
603#define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */
604#define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */
605#define CSUM_TCPIPV6 0x0020 /* will csum TCP for IPv6 */
606#define CSUM_UDPIPV6 0x0040 /* will csum UDP for IPv6 */
607#define CSUM_FRAGMENT_IPV6 0x0080 /* will do IPv6 fragmentation */
608
609#define CSUM_IP_CHECKED 0x0100 /* did csum IP */
610#define CSUM_IP_VALID 0x0200 /* ... the csum is valid */
611#define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */
612#define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */
39236c6e 613#define CSUM_PARTIAL 0x1000 /* simple Sum16 computation */
5ba3f43e 614#define CSUM_ZERO_INVERT 0x2000 /* invert 0 to -0 (0xffff) */
6d2010ae
A
615
616#define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP)
617#define CSUM_DELAY_IP (CSUM_IP) /* IPv4 only: no IPv6 IP cksum */
618#define CSUM_DELAY_IPV6_DATA (CSUM_TCPIPV6 | CSUM_UDPIPV6)
619#define CSUM_DATA_IPV6_VALID CSUM_DATA_VALID /* csum_data field is valid */
39236c6e
A
620
621#define CSUM_TX_FLAGS \
622 (CSUM_DELAY_IP | CSUM_DELAY_DATA | CSUM_DELAY_IPV6_DATA | \
5ba3f43e 623 CSUM_DATA_VALID | CSUM_PARTIAL | CSUM_ZERO_INVERT)
39236c6e
A
624
625#define CSUM_RX_FLAGS \
626 (CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_PSEUDO_HDR | \
627 CSUM_DATA_VALID | CSUM_PARTIAL)
628
4a249263
A
629/*
630 * Note: see also IF_HWASSIST_CSUM defined in <net/if_var.h>
631 */
4a249263
A
632
633/* VLAN tag present */
6d2010ae 634#define CSUM_VLAN_TAG_VALID 0x10000 /* vlan_tag field is valid */
b0d623f7
A
635
636/* TCP Segment Offloading requested on this mbuf */
6d2010ae
A
637#define CSUM_TSO_IPV4 0x100000 /* This mbuf needs to be segmented by the NIC */
638#define CSUM_TSO_IPV6 0x200000 /* This mbuf needs to be segmented by the NIC */
316670eb 639
39236c6e
A
640#define TSO_IPV4_OK(_ifp, _m) \
641 (((_ifp)->if_hwassist & IFNET_TSO_IPV4) && \
642 ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV4)) \
643
644#define TSO_IPV4_NOTOK(_ifp, _m) \
645 (!((_ifp)->if_hwassist & IFNET_TSO_IPV4) && \
646 ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV4)) \
647
648#define TSO_IPV6_OK(_ifp, _m) \
649 (((_ifp)->if_hwassist & IFNET_TSO_IPV6) && \
650 ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV6)) \
651
652#define TSO_IPV6_NOTOK(_ifp, _m) \
653 (!((_ifp)->if_hwassist & IFNET_TSO_IPV6) && \
654 ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV6)) \
655
6d2010ae 656#endif /* XNU_KERNEL_PRIVATE */
1c79356b
A
657
658/* mbuf types */
659#define MT_FREE 0 /* should be on free list */
660#define MT_DATA 1 /* dynamic (data) allocation */
661#define MT_HEADER 2 /* packet header */
662#define MT_SOCKET 3 /* socket structure */
663#define MT_PCB 4 /* protocol control block */
664#define MT_RTABLE 5 /* routing tables */
665#define MT_HTABLE 6 /* IMP host tables */
666#define MT_ATABLE 7 /* address resolution tables */
667#define MT_SONAME 8 /* socket name */
668#define MT_SOOPTS 10 /* socket options */
669#define MT_FTABLE 11 /* fragment reassembly header */
670#define MT_RIGHTS 12 /* access rights */
671#define MT_IFADDR 13 /* interface address */
6d2010ae
A
672#define MT_CONTROL 14 /* extra-data protocol message */
673#define MT_OOBDATA 15 /* expedited data */
674#define MT_TAG 16 /* volatile metadata associated to pkts */
675#define MT_MAX 32 /* enough? */
1c79356b 676
6d2010ae 677#ifdef XNU_KERNEL_PRIVATE
1c79356b
A
678/*
679 * mbuf allocation/deallocation macros:
680 *
681 * MGET(struct mbuf *m, int how, int type)
682 * allocates an mbuf and initializes it to contain internal data.
683 *
684 * MGETHDR(struct mbuf *m, int how, int type)
685 * allocates an mbuf and initializes it to contain a packet header
686 * and internal data.
687 */
688
e3027f41 689#if 1
6d2010ae 690#define MCHECK(m) m_mcheck(m)
1c79356b 691#else
6d2010ae 692#define MCHECK(m)
1c79356b
A
693#endif
694
9bccf70c
A
695#define MGET(m, how, type) ((m) = m_get((how), (type)))
696
697#define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type)))
1c79356b
A
698
699/*
700 * Mbuf cluster macros.
701 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
702 * MCLGET adds such clusters to a normal mbuf;
703 * the flag M_EXT is set upon success.
704 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
705 * freeing the cluster if the reference count has reached 0.
706 *
707 * Normal mbuf clusters are normally treated as character arrays
708 * after allocation, but use the first word of the buffer as a free list
709 * pointer while on the free list.
710 */
711union mcluster {
712 union mcluster *mcl_next;
713 char mcl_buf[MCLBYTES];
714};
715
9bccf70c
A
716#define MCLALLOC(p, how) ((p) = m_mclalloc(how))
717
6d2010ae 718#define MCLFREE(p) m_mclfree(p)
9bccf70c 719
6d2010ae 720#define MCLGET(m, how) ((m) = m_mclget(m, how))
9bccf70c 721
91447636
A
722/*
723 * Mbuf big cluster
724 */
91447636
A
725union mbigcluster {
726 union mbigcluster *mbc_next;
6d2010ae 727 char mbc_buf[MBIGCLBYTES];
91447636
A
728};
729
6d2010ae
A
730/*
731 * Mbuf jumbo cluster
732 */
2d21ac55
A
733union m16kcluster {
734 union m16kcluster *m16kcl_next;
735 char m16kcl_buf[M16KCLBYTES];
736};
91447636 737
6d2010ae 738#define MCLHASREFERENCE(m) m_mclhasreference(m)
1c79356b
A
739
740/*
741 * MFREE(struct mbuf *m, struct mbuf *n)
742 * Free a single mbuf and associated external storage.
743 * Place the successor, if any, in n.
744 */
745
9bccf70c 746#define MFREE(m, n) ((n) = m_free(m))
1c79356b
A
747
748/*
749 * Copy mbuf pkthdr from from to to.
750 * from must have M_PKTHDR set, and to must be empty.
751 * aux pointer will be moved to `to'.
752 */
9bccf70c 753#define M_COPY_PKTHDR(to, from) m_copy_pkthdr(to, from)
1c79356b 754
316670eb
A
755#define M_COPY_PFTAG(to, from) m_copy_pftag(to, from)
756
39236c6e
A
757#define M_COPY_CLASSIFIER(to, from) m_copy_classifier(to, from)
758
1c79356b
A
759/*
760 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
761 * an object of the specified size at the end of the mbuf, longword aligned.
762 */
6d2010ae
A
763#define M_ALIGN(m, len) \
764do { \
765 (m)->m_data += (MLEN - (len)) &~ (sizeof (long) - 1); \
766} while (0)
767
1c79356b
A
768/*
769 * As above, for mbufs allocated with m_gethdr/MGETHDR
770 * or initialized by M_COPY_PKTHDR.
771 */
6d2010ae
A
772#define MH_ALIGN(m, len) \
773do { \
774 (m)->m_data += (MHLEN - (len)) &~ (sizeof (long) - 1); \
775} while (0)
1c79356b
A
776
777/*
778 * Compute the amount of space available
779 * before the current start of data in an mbuf.
780 * Subroutine - data not available if certain references.
781 */
1c79356b
A
782#define M_LEADINGSPACE(m) m_leadingspace(m)
783
784/*
785 * Compute the amount of space available
786 * after the end of data in an mbuf.
787 * Subroutine - data not available if certain references.
788 */
1c79356b
A
789#define M_TRAILINGSPACE(m) m_trailingspace(m)
790
791/*
792 * Arrange to prepend space of size plen to mbuf m.
793 * If a new mbuf must be allocated, how specifies whether to wait.
794 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
795 * is freed and m is set to NULL.
796 */
3e170ce0
A
797#define M_PREPEND(m, plen, how, align) \
798 ((m) = m_prepend_2((m), (plen), (how), (align)))
1c79356b
A
799
800/* change mbuf to new type */
6d2010ae 801#define MCHTYPE(m, t) m_mchtype(m, t)
1c79356b
A
802
803/* compatiblity with 4.3 */
6d2010ae 804#define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
1c79356b 805
b0d623f7 806#define MBSHIFT 20 /* 1MB */
6d2010ae 807#define MBSIZE (1 << MBSHIFT)
b0d623f7 808#define GBSHIFT 30 /* 1GB */
6d2010ae 809#define GBSIZE (1 << GBSHIFT)
c910b4d9 810
6d2010ae
A
811/*
812 * M_STRUCT_GET ensures that intermediate protocol header (from "off" to
316670eb 813 * "off+len") is located in single mbuf, on contiguous memory region.
6d2010ae
A
814 * The pointer to the region will be returned to pointer variable "val",
815 * with type "typ".
816 *
817 * M_STRUCT_GET0 does the same, except that it aligns the structure at
818 * very top of mbuf. GET0 is likely to make memory copy than GET.
819 */
820#define M_STRUCT_GET(val, typ, m, off, len) \
821do { \
822 struct mbuf *t; \
823 int tmp; \
824 \
825 if ((m)->m_len >= (off) + (len)) { \
826 (val) = (typ)(mtod((m), caddr_t) + (off)); \
827 } else { \
828 t = m_pulldown((m), (off), (len), &tmp); \
829 if (t != NULL) { \
830 if (t->m_len < tmp + (len)) \
831 panic("m_pulldown malfunction"); \
832 (val) = (typ)(mtod(t, caddr_t) + tmp); \
833 } else { \
834 (val) = (typ)NULL; \
835 (m) = NULL; \
836 } \
837 } \
838} while (0)
839
840#define M_STRUCT_GET0(val, typ, m, off, len) \
841do { \
842 struct mbuf *t; \
843 \
316670eb
A
844 if ((off) == 0 && ((m)->m_len >= (len))) { \
845 (val) = (typ)(void *)mtod(m, caddr_t); \
6d2010ae
A
846 } else { \
847 t = m_pulldown((m), (off), (len), NULL); \
848 if (t != NULL) { \
849 if (t->m_len < (len)) \
850 panic("m_pulldown malfunction"); \
316670eb 851 (val) = (typ)(void *)mtod(t, caddr_t); \
6d2010ae
A
852 } else { \
853 (val) = (typ)NULL; \
854 (m) = NULL; \
855 } \
856 } \
857} while (0)
858
859#define MBUF_INPUT_CHECK(m, rcvif) \
860do { \
861 if (!(m->m_flags & MBUF_PKTHDR) || \
862 m->m_len < 0 || \
863 m->m_len > ((njcl > 0) ? njclbytes : MBIGCLBYTES) || \
864 m->m_type == MT_FREE || \
865 ((m->m_flags & M_EXT) != 0 && m->m_ext.ext_buf == NULL)) { \
316670eb 866 panic_plain("Failed mbuf validity check: mbuf %p len %d " \
39236c6e 867 "type %d flags 0x%x data %p rcvif %s ifflags 0x%x", \
6d2010ae
A
868 m, m->m_len, m->m_type, m->m_flags, \
869 ((m->m_flags & M_EXT) ? m->m_ext.ext_buf : m->m_data), \
39236c6e 870 if_name(rcvif), \
6d2010ae
A
871 (rcvif->if_flags & 0xffff)); \
872 } \
873} while (0)
874
316670eb
A
875/*
876 * Simple mbuf queueing system
877 *
878 * This is basically a SIMPLEQ adapted to mbuf use (i.e. using
879 * m_nextpkt instead of field.sqe_next).
880 *
881 * m_next is ignored, so queueing chains of mbufs is possible
882 */
883#define MBUFQ_HEAD(name) \
884struct name { \
885 struct mbuf *mq_first; /* first packet */ \
886 struct mbuf **mq_last; /* addr of last next packet */ \
887}
888
889#define MBUFQ_INIT(q) do { \
890 MBUFQ_FIRST(q) = NULL; \
891 (q)->mq_last = &MBUFQ_FIRST(q); \
892} while (0)
893
894#define MBUFQ_PREPEND(q, m) do { \
895 if ((MBUFQ_NEXT(m) = MBUFQ_FIRST(q)) == NULL) \
896 (q)->mq_last = &MBUFQ_NEXT(m); \
897 MBUFQ_FIRST(q) = (m); \
898} while (0)
899
900#define MBUFQ_ENQUEUE(q, m) do { \
901 MBUFQ_NEXT(m) = NULL; \
902 *(q)->mq_last = (m); \
903 (q)->mq_last = &MBUFQ_NEXT(m); \
904} while (0)
905
906#define MBUFQ_ENQUEUE_MULTI(q, m, n) do { \
907 MBUFQ_NEXT(n) = NULL; \
908 *(q)->mq_last = (m); \
909 (q)->mq_last = &MBUFQ_NEXT(n); \
910} while (0)
911
912#define MBUFQ_DEQUEUE(q, m) do { \
913 if (((m) = MBUFQ_FIRST(q)) != NULL) { \
914 if ((MBUFQ_FIRST(q) = MBUFQ_NEXT(m)) == NULL) \
915 (q)->mq_last = &MBUFQ_FIRST(q); \
916 else \
917 MBUFQ_NEXT(m) = NULL; \
918 } \
919} while (0)
920
921#define MBUFQ_REMOVE(q, m) do { \
922 if (MBUFQ_FIRST(q) == (m)) { \
923 MBUFQ_DEQUEUE(q, m); \
924 } else { \
925 struct mbuf *_m = MBUFQ_FIRST(q); \
926 while (MBUFQ_NEXT(_m) != (m)) \
927 _m = MBUFQ_NEXT(_m); \
928 if ((MBUFQ_NEXT(_m) = \
929 MBUFQ_NEXT(MBUFQ_NEXT(_m))) == NULL) \
930 (q)->mq_last = &MBUFQ_NEXT(_m); \
931 } \
932} while (0)
933
934#define MBUFQ_DRAIN(q) do { \
935 struct mbuf *__m0; \
936 while ((__m0 = MBUFQ_FIRST(q)) != NULL) { \
937 MBUFQ_FIRST(q) = MBUFQ_NEXT(__m0); \
938 MBUFQ_NEXT(__m0) = NULL; \
939 m_freem(__m0); \
940 } \
941 (q)->mq_last = &MBUFQ_FIRST(q); \
942} while (0)
943
944#define MBUFQ_FOREACH(m, q) \
945 for ((m) = MBUFQ_FIRST(q); \
946 (m); \
947 (m) = MBUFQ_NEXT(m))
948
949#define MBUFQ_FOREACH_SAFE(m, q, tvar) \
950 for ((m) = MBUFQ_FIRST(q); \
951 (m) && ((tvar) = MBUFQ_NEXT(m), 1); \
952 (m) = (tvar))
953
954#define MBUFQ_EMPTY(q) ((q)->mq_first == NULL)
955#define MBUFQ_FIRST(q) ((q)->mq_first)
956#define MBUFQ_NEXT(m) ((m)->m_nextpkt)
39037602
A
957/*
958 * mq_last is initialized to point to mq_first, so check if they're
959 * equal and return NULL when the list is empty. Otherwise, we need
960 * to subtract the offset of MBUQ_NEXT (i.e. m_nextpkt field) to get
961 * to the base mbuf address to return to caller.
962 */
963#define MBUFQ_LAST(head) \
964 (((head)->mq_last == &MBUFQ_FIRST(head)) ? NULL : \
965 ((struct mbuf *)(void *)((char *)(head)->mq_last - \
966 (size_t)(&MBUFQ_NEXT((struct mbuf *)0)))))
316670eb
A
967
968#define max_linkhdr P2ROUNDUP(_max_linkhdr, sizeof (u_int32_t))
969#define max_protohdr P2ROUNDUP(_max_protohdr, sizeof (u_int32_t))
6d2010ae 970#endif /* XNU_KERNEL_PRIVATE */
91447636 971
1c79356b 972/*
2d21ac55 973 * Mbuf statistics (legacy).
1c79356b
A
974 */
975struct mbstat {
2d21ac55
A
976 u_int32_t m_mbufs; /* mbufs obtained from page pool */
977 u_int32_t m_clusters; /* clusters obtained from page pool */
978 u_int32_t m_spare; /* spare field */
979 u_int32_t m_clfree; /* free clusters */
980 u_int32_t m_drops; /* times failed to find space */
981 u_int32_t m_wait; /* times waited for space */
982 u_int32_t m_drain; /* times drained protocols for space */
983 u_short m_mtypes[256]; /* type specific mbuf allocations */
984 u_int32_t m_mcfail; /* times m_copym failed */
985 u_int32_t m_mpfail; /* times m_pullup failed */
986 u_int32_t m_msize; /* length of an mbuf */
987 u_int32_t m_mclbytes; /* length of an mbuf cluster */
988 u_int32_t m_minclsize; /* min length of data to allocate a cluster */
989 u_int32_t m_mlen; /* length of data in an mbuf */
990 u_int32_t m_mhlen; /* length of data in a header mbuf */
991 u_int32_t m_bigclusters; /* clusters obtained from page pool */
992 u_int32_t m_bigclfree; /* free clusters */
993 u_int32_t m_bigmclbytes; /* length of an mbuf cluster */
91447636
A
994};
995
996/* Compatibillity with 10.3 */
997struct ombstat {
2d21ac55
A
998 u_int32_t m_mbufs; /* mbufs obtained from page pool */
999 u_int32_t m_clusters; /* clusters obtained from page pool */
1000 u_int32_t m_spare; /* spare field */
1001 u_int32_t m_clfree; /* free clusters */
1002 u_int32_t m_drops; /* times failed to find space */
1003 u_int32_t m_wait; /* times waited for space */
1004 u_int32_t m_drain; /* times drained protocols for space */
1005 u_short m_mtypes[256]; /* type specific mbuf allocations */
1006 u_int32_t m_mcfail; /* times m_copym failed */
1007 u_int32_t m_mpfail; /* times m_pullup failed */
1008 u_int32_t m_msize; /* length of an mbuf */
1009 u_int32_t m_mclbytes; /* length of an mbuf cluster */
1010 u_int32_t m_minclsize; /* min length of data to allocate a cluster */
1011 u_int32_t m_mlen; /* length of data in an mbuf */
1012 u_int32_t m_mhlen; /* length of data in a header mbuf */
1c79356b
A
1013};
1014
1015/*
2d21ac55 1016 * mbuf class statistics.
1c79356b 1017 */
2d21ac55
A
1018#define MAX_MBUF_CNAME 15
1019
6d2010ae 1020#if defined(XNU_KERNEL_PRIVATE)
b0d623f7
A
1021/* For backwards compatibility with 32-bit userland process */
1022struct omb_class_stat {
1023 char mbcl_cname[MAX_MBUF_CNAME + 1]; /* class name */
1024 u_int32_t mbcl_size; /* buffer size */
1025 u_int32_t mbcl_total; /* # of buffers created */
1026 u_int32_t mbcl_active; /* # of active buffers */
1027 u_int32_t mbcl_infree; /* # of available buffers */
1028 u_int32_t mbcl_slab_cnt; /* # of available slabs */
1029 u_int64_t mbcl_alloc_cnt; /* # of times alloc is called */
1030 u_int64_t mbcl_free_cnt; /* # of times free is called */
1031 u_int64_t mbcl_notified; /* # of notified wakeups */
1032 u_int64_t mbcl_purge_cnt; /* # of purges so far */
1033 u_int64_t mbcl_fail_cnt; /* # of allocation failures */
1034 u_int32_t mbcl_ctotal; /* total only for this class */
fe8ab488 1035 u_int32_t mbcl_release_cnt; /* amount of memory returned */
b0d623f7
A
1036 /*
1037 * Cache layer statistics
1038 */
1039 u_int32_t mbcl_mc_state; /* cache state (see below) */
1040 u_int32_t mbcl_mc_cached; /* # of cached buffers */
1041 u_int32_t mbcl_mc_waiter_cnt; /* # waiters on the cache */
1042 u_int32_t mbcl_mc_wretry_cnt; /* # of wait retries */
1043 u_int32_t mbcl_mc_nwretry_cnt; /* # of no-wait retry attempts */
1044 u_int64_t mbcl_reserved[4]; /* for future use */
1045} __attribute__((__packed__));
6d2010ae 1046#endif /* XNU_KERNEL_PRIVATE */
b0d623f7 1047
2d21ac55
A
1048typedef struct mb_class_stat {
1049 char mbcl_cname[MAX_MBUF_CNAME + 1]; /* class name */
1050 u_int32_t mbcl_size; /* buffer size */
1051 u_int32_t mbcl_total; /* # of buffers created */
1052 u_int32_t mbcl_active; /* # of active buffers */
1053 u_int32_t mbcl_infree; /* # of available buffers */
1054 u_int32_t mbcl_slab_cnt; /* # of available slabs */
b0d623f7
A
1055#if defined(KERNEL) || defined(__LP64__)
1056 u_int32_t mbcl_pad; /* padding */
1057#endif /* KERNEL || __LP64__ */
2d21ac55
A
1058 u_int64_t mbcl_alloc_cnt; /* # of times alloc is called */
1059 u_int64_t mbcl_free_cnt; /* # of times free is called */
1060 u_int64_t mbcl_notified; /* # of notified wakeups */
1061 u_int64_t mbcl_purge_cnt; /* # of purges so far */
1062 u_int64_t mbcl_fail_cnt; /* # of allocation failures */
1063 u_int32_t mbcl_ctotal; /* total only for this class */
fe8ab488 1064 u_int32_t mbcl_release_cnt; /* amount of memory returned */
2d21ac55
A
1065 /*
1066 * Cache layer statistics
1067 */
1068 u_int32_t mbcl_mc_state; /* cache state (see below) */
1069 u_int32_t mbcl_mc_cached; /* # of cached buffers */
1070 u_int32_t mbcl_mc_waiter_cnt; /* # waiters on the cache */
1071 u_int32_t mbcl_mc_wretry_cnt; /* # of wait retries */
1072 u_int32_t mbcl_mc_nwretry_cnt; /* # of no-wait retry attempts */
fe8ab488
A
1073 u_int32_t mbcl_peak_reported; /* last usage peak reported */
1074 u_int32_t mbcl_reserved[7]; /* for future use */
2d21ac55
A
1075} mb_class_stat_t;
1076
1077#define MCS_DISABLED 0 /* cache is permanently disabled */
1078#define MCS_ONLINE 1 /* cache is online */
1079#define MCS_PURGING 2 /* cache is being purged */
1080#define MCS_OFFLINE 3 /* cache is offline (resizing) */
1081
6d2010ae 1082#if defined(XNU_KERNEL_PRIVATE)
b0d623f7
A
1083/* For backwards compatibility with 32-bit userland process */
1084struct omb_stat {
1085 u_int32_t mbs_cnt; /* number of classes */
1086 struct omb_class_stat mbs_class[1]; /* class array */
1087} __attribute__((__packed__));
6d2010ae 1088#endif /* XNU_KERNEL_PRIVATE */
b0d623f7 1089
2d21ac55
A
1090typedef struct mb_stat {
1091 u_int32_t mbs_cnt; /* number of classes */
b0d623f7
A
1092#if defined(KERNEL) || defined(__LP64__)
1093 u_int32_t mbs_pad; /* padding */
1094#endif /* KERNEL || __LP64__ */
2d21ac55
A
1095 mb_class_stat_t mbs_class[1]; /* class array */
1096} mb_stat_t;
1097
6d2010ae
A
1098#ifdef PRIVATE
1099#define MLEAK_STACK_DEPTH 16 /* Max PC stack depth */
1100
1101typedef struct mleak_trace_stat {
1102 u_int64_t mltr_collisions;
1103 u_int64_t mltr_hitcount;
1104 u_int64_t mltr_allocs;
1105 u_int64_t mltr_depth;
1106 u_int64_t mltr_addr[MLEAK_STACK_DEPTH];
1107} mleak_trace_stat_t;
1108
1109typedef struct mleak_stat {
1110 u_int32_t ml_isaddr64; /* 64-bit KVA? */
1111 u_int32_t ml_cnt; /* number of traces */
1112 mleak_trace_stat_t ml_trace[1]; /* trace array */
1113} mleak_stat_t;
1114
1115struct mleak_table {
1116 u_int32_t mleak_capture; /* sampling capture counter */
1117 u_int32_t mleak_sample_factor; /* sample factor */
1118
1119 /* Times two active records want to occupy the same spot */
1120 u_int64_t alloc_collisions;
1121 u_int64_t trace_collisions;
1122
1123 /* Times new record lands on spot previously occupied by freed alloc */
1124 u_int64_t alloc_overwrites;
1125 u_int64_t trace_overwrites;
1126
1127 /* Times a new alloc or trace is put into the hash table */
1128 u_int64_t alloc_recorded;
1129 u_int64_t trace_recorded;
1130
1131 /* Total number of outstanding allocs */
1132 u_int64_t outstanding_allocs;
1133
1134 /* Times mleak_log returned false because couldn't acquire the lock */
1135 u_int64_t total_conflicts;
1136};
1137#endif /* PRIVATE */
1138
2d21ac55 1139#ifdef KERNEL_PRIVATE
6d2010ae 1140__BEGIN_DECLS
1c79356b 1141
6d2010ae
A
1142/*
1143 * Exported (private)
1144 */
1145
1146extern struct mbstat mbstat; /* statistics */
1147
1148__END_DECLS
1149#endif /* KERNEL_PRIVATE */
1c79356b 1150
6d2010ae 1151#ifdef XNU_KERNEL_PRIVATE
91447636 1152__BEGIN_DECLS
6d2010ae
A
1153
1154/*
1155 * Not exported (xnu private)
1156 */
1157
1158/* flags to m_get/MGET */
1159/* Need to include malloc.h to get right options for malloc */
1160#include <sys/malloc.h>
1161
1162struct mbuf;
1163
1164/* length to m_copy to copy all */
1165#define M_COPYALL 1000000000
1166
1167#define M_DONTWAIT M_NOWAIT
1168#define M_WAIT M_WAITOK
1169
39236c6e
A
1170/* modes for m_copym and variants */
1171#define M_COPYM_NOOP_HDR 0 /* don't copy/move pkthdr contents */
1172#define M_COPYM_COPY_HDR 1 /* copy pkthdr from old to new */
1173#define M_COPYM_MOVE_HDR 2 /* move pkthdr from old to new */
fe8ab488
A
1174#define M_COPYM_MUST_COPY_HDR 3 /* MUST copy pkthdr from old to new */
1175#define M_COPYM_MUST_MOVE_HDR 4 /* MUST move pkthdr from old to new */
39236c6e 1176
6d2010ae
A
1177/*
1178 * These macros are mapped to the appropriate KPIs, so that private code
1179 * can be simply recompiled in order to be forward-compatible with future
1180 * changes toward the struture sizes.
1181 */
1182#define MLEN mbuf_get_mlen() /* normal data len */
1183#define MHLEN mbuf_get_mhlen() /* data len w/pkthdr */
1184
1185#define MINCLSIZE mbuf_get_minclsize() /* cluster usage threshold */
1186
1187extern void m_freem(struct mbuf *);
39236c6e 1188extern u_int64_t mcl_to_paddr(char *);
6d2010ae
A
1189extern void m_adj(struct mbuf *, int);
1190extern void m_cat(struct mbuf *, struct mbuf *);
1191extern void m_copydata(struct mbuf *, int, int, void *);
1192extern struct mbuf *m_copym(struct mbuf *, int, int, int);
39236c6e 1193extern struct mbuf *m_copym_mode(struct mbuf *, int, int, int, uint32_t);
6d2010ae
A
1194extern struct mbuf *m_get(int, int);
1195extern struct mbuf *m_gethdr(int, int);
1196extern struct mbuf *m_getpacket(void);
1197extern struct mbuf *m_getpackets(int, int, int);
1198extern struct mbuf *m_mclget(struct mbuf *, int);
1199extern void *m_mtod(struct mbuf *);
3e170ce0 1200extern struct mbuf *m_prepend_2(struct mbuf *, int, int, int);
6d2010ae
A
1201extern struct mbuf *m_pullup(struct mbuf *, int);
1202extern struct mbuf *m_split(struct mbuf *, int, int);
1203extern void m_mclfree(caddr_t p);
1204
316670eb
A
1205/*
1206 * On platforms which require strict alignment (currently for anything but
1207 * i386 or x86_64), this macro checks whether the data pointer of an mbuf
1208 * is 32-bit aligned (this is the expected minimum alignment for protocol
1209 * headers), and assert otherwise.
1210 */
1211#if defined(__i386__) || defined(__x86_64__)
1212#define MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(_m)
1213#else /* !__i386__ && !__x86_64__ */
1214#define MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(_m) do { \
1215 if (!IS_P2ALIGNED((_m)->m_data, sizeof (u_int32_t))) { \
1216 if (((_m)->m_flags & M_PKTHDR) && \
1217 (_m)->m_pkthdr.rcvif != NULL) { \
1218 panic_plain("\n%s: mbuf %p data ptr %p is not " \
39236c6e 1219 "32-bit aligned [%s: alignerrs=%lld]\n", \
316670eb 1220 __func__, (_m), (_m)->m_data, \
39236c6e 1221 if_name((_m)->m_pkthdr.rcvif), \
316670eb
A
1222 (_m)->m_pkthdr.rcvif->if_alignerrs); \
1223 } else { \
1224 panic_plain("\n%s: mbuf %p data ptr %p is not " \
1225 "32-bit aligned\n", \
1226 __func__, (_m), (_m)->m_data); \
1227 } \
1228 } \
1229} while (0)
1230#endif /* !__i386__ && !__x86_64__ */
1231
1232/* Maximum number of MBUF_SC values (excluding MBUF_SC_UNSPEC) */
1233#define MBUF_SC_MAX_CLASSES 10
1234
1235/*
1236 * These conversion macros rely on the corresponding MBUF_SC and
1237 * MBUF_TC values in order to establish the following mapping:
1238 *
1239 * MBUF_SC_BK_SYS ] ==> MBUF_TC_BK
1240 * MBUF_SC_BK ]
1241 *
1242 * MBUF_SC_BE ] ==> MBUF_TC_BE
1243 * MBUF_SC_RD ]
1244 * MBUF_SC_OAM ]
1245 *
1246 * MBUF_SC_AV ] ==> MBUF_TC_VI
1247 * MBUF_SC_RV ]
1248 * MBUF_SC_VI ]
1249 *
1250 * MBUF_SC_VO ] ==> MBUF_TC_VO
1251 * MBUF_SC_CTL ]
1252 *
1253 * The values assigned to each service class allows for a fast mapping to
1254 * the corresponding MBUF_TC traffic class values, as well as to retrieve the
1255 * assigned index; therefore care must be taken when comparing against these
1256 * values. Use the corresponding class and index macros to retrieve the
1257 * corresponding portion, and never assume that a higher class corresponds
1258 * to a higher index.
1259 */
1260#define MBUF_SCVAL(x) ((x) & 0xffff)
1261#define MBUF_SCIDX(x) ((((x) >> 16) & 0xff) >> 3)
1262#define MBUF_SC2TC(_sc) (MBUF_SCVAL(_sc) >> 7)
1263#define MBUF_TC2SCVAL(_tc) ((_tc) << 7)
1264#define IS_MBUF_SC_BACKGROUND(_sc) (((_sc) == MBUF_SC_BK_SYS) || \
1265 ((_sc) == MBUF_SC_BK))
3e170ce0
A
1266#define IS_MBUF_SC_REALTIME(_sc) ((_sc) >= MBUF_SC_AV && (_sc) <= MBUF_SC_VO)
1267#define IS_MBUF_SC_BESTEFFORT(_sc) ((_sc) == MBUF_SC_BE || \
1268 (_sc) == MBUF_SC_RD || (_sc) == MBUF_SC_OAM)
316670eb
A
1269
1270#define SCIDX_BK_SYS MBUF_SCIDX(MBUF_SC_BK_SYS)
1271#define SCIDX_BK MBUF_SCIDX(MBUF_SC_BK)
1272#define SCIDX_BE MBUF_SCIDX(MBUF_SC_BE)
1273#define SCIDX_RD MBUF_SCIDX(MBUF_SC_RD)
1274#define SCIDX_OAM MBUF_SCIDX(MBUF_SC_OAM)
1275#define SCIDX_AV MBUF_SCIDX(MBUF_SC_AV)
1276#define SCIDX_RV MBUF_SCIDX(MBUF_SC_RV)
1277#define SCIDX_VI MBUF_SCIDX(MBUF_SC_VI)
1278#define SCIDX_VO MBUF_SCIDX(MBUF_SC_VO)
1279#define SCIDX_CTL MBUF_SCIDX(MBUF_SC_CTL)
1280
1281#define SCVAL_BK_SYS MBUF_SCVAL(MBUF_SC_BK_SYS)
1282#define SCVAL_BK MBUF_SCVAL(MBUF_SC_BK)
1283#define SCVAL_BE MBUF_SCVAL(MBUF_SC_BE)
1284#define SCVAL_RD MBUF_SCVAL(MBUF_SC_RD)
1285#define SCVAL_OAM MBUF_SCVAL(MBUF_SC_OAM)
1286#define SCVAL_AV MBUF_SCVAL(MBUF_SC_AV)
1287#define SCVAL_RV MBUF_SCVAL(MBUF_SC_RV)
1288#define SCVAL_VI MBUF_SCVAL(MBUF_SC_VI)
1289#define SCVAL_VO MBUF_SCVAL(MBUF_SC_VO)
1290#define SCVAL_CTL MBUF_SCVAL(MBUF_SC_CTL)
1291
1292#define MBUF_VALID_SC(c) \
1293 (c == MBUF_SC_BK_SYS || c == MBUF_SC_BK || c == MBUF_SC_BE || \
1294 c == MBUF_SC_RD || c == MBUF_SC_OAM || c == MBUF_SC_AV || \
1295 c == MBUF_SC_RV || c == MBUF_SC_VI || c == MBUF_SC_VO || \
1296 c == MBUF_SC_CTL)
1297
1298#define MBUF_VALID_SCIDX(c) \
1299 (c == SCIDX_BK_SYS || c == SCIDX_BK || c == SCIDX_BE || \
1300 c == SCIDX_RD || c == SCIDX_OAM || c == SCIDX_AV || \
1301 c == SCIDX_RV || c == SCIDX_VI || c == SCIDX_VO || \
1302 c == SCIDX_CTL)
1303
1304#define MBUF_VALID_SCVAL(c) \
1305 (c == SCVAL_BK_SYS || c == SCVAL_BK || c == SCVAL_BE || \
1306 c == SCVAL_RD || c == SCVAL_OAM || c == SCVAL_AV || \
1307 c == SCVAL_RV || c == SCVAL_VI || c == SCVAL_VO || \
1308 c == SCVAL_CTL)
1309
3e170ce0
A
1310extern unsigned char *mbutl; /* start VA of mbuf pool */
1311extern unsigned char *embutl; /* end VA of mbuf pool */
39236c6e
A
1312extern unsigned int nmbclusters; /* number of mapped clusters */
1313extern int njcl; /* # of jumbo clusters */
1314extern int njclbytes; /* size of a jumbo cluster */
1315extern int max_hdr; /* largest link+protocol header */
1316extern int max_datalen; /* MHLEN - max_hdr */
6d2010ae 1317
316670eb 1318/* Use max_linkhdr instead of _max_linkhdr */
39236c6e 1319extern int _max_linkhdr; /* largest link-level header */
316670eb
A
1320
1321/* Use max_protohdr instead of _max_protohdr */
39236c6e 1322extern int _max_protohdr; /* largest protocol header */
316670eb 1323
6d2010ae 1324__private_extern__ unsigned int mbuf_default_ncl(int, u_int64_t);
2d21ac55
A
1325__private_extern__ void mbinit(void);
1326__private_extern__ struct mbuf *m_clattach(struct mbuf *, int, caddr_t,
39037602 1327 void (*)(caddr_t, u_int, caddr_t), u_int, caddr_t, int, int);
2d21ac55
A
1328__private_extern__ caddr_t m_bigalloc(int);
1329__private_extern__ void m_bigfree(caddr_t, u_int, caddr_t);
1330__private_extern__ struct mbuf *m_mbigget(struct mbuf *, int);
1331__private_extern__ caddr_t m_16kalloc(int);
1332__private_extern__ void m_16kfree(caddr_t, u_int, caddr_t);
1333__private_extern__ struct mbuf *m_m16kget(struct mbuf *, int);
39236c6e 1334__private_extern__ int m_reinit(struct mbuf *, int);
6d2010ae
A
1335__private_extern__ struct mbuf *m_free(struct mbuf *);
1336__private_extern__ struct mbuf *m_getclr(int, int);
1337__private_extern__ struct mbuf *m_getptr(struct mbuf *, int, int *);
1338__private_extern__ unsigned int m_length(struct mbuf *);
316670eb
A
1339__private_extern__ unsigned int m_length2(struct mbuf *, struct mbuf **);
1340__private_extern__ unsigned int m_fixhdr(struct mbuf *);
1341__private_extern__ struct mbuf *m_defrag(struct mbuf *, int);
1342__private_extern__ struct mbuf *m_defrag_offset(struct mbuf *, u_int32_t, int);
6d2010ae
A
1343__private_extern__ struct mbuf *m_prepend(struct mbuf *, int, int);
1344__private_extern__ struct mbuf *m_copyup(struct mbuf *, int, int);
1345__private_extern__ struct mbuf *m_retry(int, int);
1346__private_extern__ struct mbuf *m_retryhdr(int, int);
1347__private_extern__ int m_freem_list(struct mbuf *);
1348__private_extern__ int m_append(struct mbuf *, int, caddr_t);
1349__private_extern__ struct mbuf *m_last(struct mbuf *);
1350__private_extern__ struct mbuf *m_devget(char *, int, int, struct ifnet *,
1351 void (*)(const void *, void *, size_t));
1352__private_extern__ struct mbuf *m_pulldown(struct mbuf *, int, int, int *);
1353
1354__private_extern__ struct mbuf *m_getcl(int, int, int);
1355__private_extern__ caddr_t m_mclalloc(int);
1356__private_extern__ int m_mclhasreference(struct mbuf *);
1357__private_extern__ void m_copy_pkthdr(struct mbuf *, struct mbuf *);
316670eb 1358__private_extern__ void m_copy_pftag(struct mbuf *, struct mbuf *);
39236c6e 1359__private_extern__ void m_copy_classifier(struct mbuf *, struct mbuf *);
6d2010ae
A
1360
1361__private_extern__ struct mbuf *m_dtom(void *);
1362__private_extern__ int m_mtocl(void *);
1363__private_extern__ union mcluster *m_cltom(int);
1364
1365__private_extern__ int m_trailingspace(struct mbuf *);
1366__private_extern__ int m_leadingspace(struct mbuf *);
1367
1368__private_extern__ struct mbuf *m_normalize(struct mbuf *m);
1369__private_extern__ void m_mchtype(struct mbuf *m, int t);
1370__private_extern__ void m_mcheck(struct mbuf *);
1371
1372__private_extern__ void m_copyback(struct mbuf *, int, int, const void *);
1373__private_extern__ struct mbuf *m_copyback_cow(struct mbuf *, int, int,
1374 const void *, int);
1375__private_extern__ int m_makewritable(struct mbuf **, int, int, int);
1376__private_extern__ struct mbuf *m_dup(struct mbuf *m, int how);
1377__private_extern__ struct mbuf *m_copym_with_hdrs(struct mbuf *, int, int, int,
39236c6e 1378 struct mbuf **, int *, uint32_t);
6d2010ae
A
1379__private_extern__ struct mbuf *m_getpackethdrs(int, int);
1380__private_extern__ struct mbuf *m_getpacket_how(int);
1381__private_extern__ struct mbuf *m_getpackets_internal(unsigned int *, int,
1382 int, int, size_t);
1383__private_extern__ struct mbuf *m_allocpacket_internal(unsigned int *, size_t,
1384 unsigned int *, int, int, size_t);
9bccf70c 1385
39037602
A
1386__private_extern__ int m_ext_set_prop(struct mbuf *, uint32_t, uint32_t);
1387__private_extern__ uint32_t m_ext_get_prop(struct mbuf *);
1388__private_extern__ int m_ext_paired_is_active(struct mbuf *);
1389__private_extern__ void m_ext_paired_activate(struct mbuf *);
1390
fe8ab488
A
1391__private_extern__ void m_drain(void);
1392
91447636 1393/*
6d2010ae
A
1394 * Packets may have annotations attached by affixing a list of "packet
1395 * tags" to the pkthdr structure. Packet tags are dynamically allocated
1396 * semi-opaque data structures that have a fixed header (struct m_tag)
1397 * that specifies the size of the memory block and an <id,type> pair that
1398 * identifies it. The id identifies the module and the type identifies the
1399 * type of data for that module. The id of zero is reserved for the kernel.
1400 *
1401 * Note that the packet tag returned by m_tag_allocate has the default
1402 * memory alignment implemented by malloc. To reference private data one
1403 * can use a construct like:
1404 *
1405 * struct m_tag *mtag = m_tag_allocate(...);
1406 * struct foo *p = (struct foo *)(mtag+1);
1407 *
1408 * if the alignment of struct m_tag is sufficient for referencing members
1409 * of struct foo. Otherwise it is necessary to embed struct m_tag within
1410 * the private data structure to insure proper alignment; e.g.
1411 *
1412 * struct foo {
1413 * struct m_tag tag;
1414 * ...
1415 * };
1416 * struct foo *p = (struct foo *) m_tag_allocate(...);
1417 * struct m_tag *mtag = &p->tag;
91447636
A
1418 */
1419
6d2010ae 1420#define KERNEL_MODULE_TAG_ID 0
91447636
A
1421
1422enum {
1423 KERNEL_TAG_TYPE_NONE = 0,
1424 KERNEL_TAG_TYPE_DUMMYNET = 1,
1425 KERNEL_TAG_TYPE_DIVERT = 2,
1426 KERNEL_TAG_TYPE_IPFORWARD = 3,
2d21ac55
A
1427 KERNEL_TAG_TYPE_IPFILT = 4,
1428 KERNEL_TAG_TYPE_MACLABEL = 5,
1429 KERNEL_TAG_TYPE_MAC_POLICY_LABEL = 6,
1430 KERNEL_TAG_TYPE_ENCAP = 8,
1431 KERNEL_TAG_TYPE_INET6 = 9,
b0d623f7 1432 KERNEL_TAG_TYPE_IPSEC = 10,
fe8ab488 1433 KERNEL_TAG_TYPE_DRVAUX = 11,
91447636
A
1434};
1435
91447636 1436/* Packet tag routines */
6d2010ae
A
1437__private_extern__ struct m_tag *m_tag_alloc(u_int32_t, u_int16_t, int, int);
1438__private_extern__ struct m_tag *m_tag_create(u_int32_t, u_int16_t, int, int,
1439 struct mbuf *);
1440__private_extern__ void m_tag_free(struct m_tag *);
1441__private_extern__ void m_tag_prepend(struct mbuf *, struct m_tag *);
1442__private_extern__ void m_tag_unlink(struct mbuf *, struct m_tag *);
1443__private_extern__ void m_tag_delete(struct mbuf *, struct m_tag *);
1444__private_extern__ void m_tag_delete_chain(struct mbuf *, struct m_tag *);
1445__private_extern__ struct m_tag *m_tag_locate(struct mbuf *, u_int32_t,
1446 u_int16_t, struct m_tag *);
1447__private_extern__ struct m_tag *m_tag_copy(struct m_tag *, int);
1448__private_extern__ int m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
39236c6e 1449__private_extern__ void m_tag_init(struct mbuf *, int);
6d2010ae
A
1450__private_extern__ struct m_tag *m_tag_first(struct mbuf *);
1451__private_extern__ struct m_tag *m_tag_next(struct mbuf *, struct m_tag *);
1452
91447636 1453__END_DECLS
6d2010ae 1454#endif /* XNU_KERNEL_PRIVATE */
91447636
A
1455#ifdef KERNEL
1456#include <sys/kpi_mbuf.h>
316670eb
A
1457#ifdef XNU_KERNEL_PRIVATE
1458__BEGIN_DECLS
1459
39236c6e
A
1460__private_extern__ void m_scratch_init(struct mbuf *);
1461__private_extern__ u_int32_t m_scratch_get(struct mbuf *, u_int8_t **);
1462
1463__private_extern__ void m_classifier_init(struct mbuf *, uint32_t);
1464
316670eb
A
1465__private_extern__ int m_set_service_class(struct mbuf *, mbuf_svc_class_t);
1466__private_extern__ mbuf_svc_class_t m_get_service_class(struct mbuf *);
1467__private_extern__ mbuf_svc_class_t m_service_class_from_idx(u_int32_t);
1468__private_extern__ mbuf_svc_class_t m_service_class_from_val(u_int32_t);
1469__private_extern__ int m_set_traffic_class(struct mbuf *, mbuf_traffic_class_t);
1470__private_extern__ mbuf_traffic_class_t m_get_traffic_class(struct mbuf *);
1471
39236c6e
A
1472#define ADDCARRY(_x) do { \
1473 while (((_x) >> 16) != 0) \
1474 (_x) = ((_x) >> 16) + ((_x) & 0xffff); \
1475} while (0)
1476
1477__private_extern__ u_int16_t m_adj_sum16(struct mbuf *, u_int32_t,
5ba3f43e 1478 u_int32_t, u_int32_t, u_int32_t);
39236c6e
A
1479__private_extern__ u_int16_t m_sum16(struct mbuf *, u_int32_t, u_int32_t);
1480
5ba3f43e
A
1481__private_extern__ void m_set_ext(struct mbuf *, struct ext_ref *,
1482 m_ext_free_func_t, caddr_t);
813fb2f6
A
1483__private_extern__ struct ext_ref *m_get_rfa(struct mbuf *);
1484__private_extern__ m_ext_free_func_t m_get_ext_free(struct mbuf *);
1485__private_extern__ caddr_t m_get_ext_arg(struct mbuf *);
1486
39037602
A
1487extern void m_do_tx_compl_callback(struct mbuf *, struct ifnet *);
1488
316670eb
A
1489__END_DECLS
1490#endif /* XNU_KERNEL_PRIVATE */
b0d623f7 1491#endif /* KERNEL */
1c79356b 1492#endif /* !_SYS_MBUF_H_ */