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