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