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