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