]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/mbuf.h
xnu-1504.7.4.tar.gz
[apple/xnu.git] / bsd / sys / mbuf.h
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
29/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
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 * HISTORY
73 * 20-May-95 Mac Gillon (mgillon) at NeXT
74 * New version based on 4.4
75 * Purged old history
76 */
2d21ac55
A
77/*
78 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
79 * support for mandatory and extensible security protections. This notice
80 * is included in support of clause 2.2 (b) of the Apple Public License,
81 * Version 2.0.
82 */
1c79356b
A
83
84#ifndef _SYS_MBUF_H_
85#define _SYS_MBUF_H_
86
91447636 87#include <sys/cdefs.h>
9bccf70c 88#include <sys/appleapiopts.h>
91447636
A
89
90#ifdef KERNEL_PRIVATE
91
1c79356b 92#include <sys/lock.h>
91447636 93#include <sys/queue.h>
1c79356b 94
b0d623f7
A
95#if PF_PKTHDR
96#include <net/pf_mtag.h>
97#endif /* PF_PKTHDR */
98
1c79356b
A
99/*
100 * Mbufs are of a single size, MSIZE (machine/param.h), which
101 * includes overhead. An mbuf may add a single "mbuf cluster" of size
102 * MCLBYTES (also in machine/param.h), which has no additional overhead
103 * and is used instead of the internal data area; this is done when
104 * at least MINCLSIZE of data must be stored.
105 */
106
b0d623f7
A
107/*
108 * These macros are mapped to the appropriate KPIs, so that private code
109 * can be simply recompiled in order to be forward-compatible with future
110 * changes toward the struture sizes.
111 */
112#define MLEN mbuf_get_mlen() /* normal data len */
113#define MHLEN mbuf_get_mhlen() /* data len w/pkthdr */
114
115/*
116 * The following _MLEN and _MHLEN macros are private to xnu. Private code
117 * that are outside of xnu must use the mbuf_get_{mlen,mhlen} routines since
118 * the sizes of the structures are dependent upon specific xnu configs.
119 */
120#define _MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
121#define _MHLEN (_MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
1c79356b
A
122
123#define MINCLSIZE (MHLEN + MLEN) /* smallest amount to put in cluster */
124#define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */
125
126#define NMBPCL (sizeof(union mcluster) / sizeof(struct mbuf))
127
1c79356b
A
128/*
129 * Macros for type conversion
130 * mtod(m,t) - convert mbuf pointer to data pointer of correct type
131 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX)
1c79356b 132 */
9bccf70c
A
133#define mtod(m,t) ((t)m_mtod(m))
134#define dtom(x) m_dtom(x)
1c79356b
A
135
136/* header at beginning of each mbuf: */
137struct m_hdr {
138 struct mbuf *mh_next; /* next buffer in chain */
139 struct mbuf *mh_nextpkt; /* next chain in queue/record */
b0d623f7 140 int32_t mh_len; /* amount of data in this mbuf */
1c79356b
A
141 caddr_t mh_data; /* location of data */
142 short mh_type; /* type of data in this mbuf */
143 short mh_flags; /* flags; see below */
144};
145
91447636
A
146/*
147 * Packet tag structure (see below for details).
148 */
149struct m_tag {
150 SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */
151 u_int16_t m_tag_type; /* Module specific type */
152 u_int16_t m_tag_len; /* Length of data */
153 u_int32_t m_tag_id; /* Module ID */
154};
155
1c79356b
A
156/* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
157struct pkthdr {
158 int len; /* total packet length */
159 struct ifnet *rcvif; /* rcv interface */
160
161 /* variables for ip and tcp reassembly */
162 void *header; /* pointer to packet header */
163 /* variables for hardware checksum */
4a249263 164 /* Note: csum_flags is used for hardware checksum and VLAN */
1c79356b
A
165 int csum_flags; /* flags regarding checksum */
166 int csum_data; /* data field used by csum routines */
b0d623f7 167 u_int tso_segsz; /* TSO segment size (actual MSS) */
4a249263 168 u_short vlan_tag; /* VLAN tag, host byte order */
91447636 169 u_short socket_id; /* socket id */
91447636 170 SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
b0d623f7
A
171#if PF_PKTHDR
172 /*
173 * Be careful; {en,dis}abling PF_PKTHDR will require xnu recompile;
174 * private code outside of xnu must use mbuf_get_mhlen() instead
175 * of MHLEN.
176 */
177 struct pf_mtag pf_mtag;
178#endif /* PF_PKTHDR */
d1ecb069
A
179#if PKT_PRIORITY
180 u_int32_t prio; /* packet priority */
181#endif /* PKT_PRIORITY */
1c79356b
A
182};
183
184
185/* description of external storage mapped into mbuf, valid if M_EXT set */
186struct m_ext {
187 caddr_t ext_buf; /* start of buffer */
55e303ae 188 void (*ext_free)(caddr_t , u_int, caddr_t); /* free routine if not the usual */
1c79356b
A
189 u_int ext_size; /* size of buffer, for ext_free */
190 caddr_t ext_arg; /* additional ext_free argument */
191 struct ext_refsq { /* references held */
192 struct ext_refsq *forward, *backward;
193 } ext_refs;
2d21ac55
A
194 struct ext_ref {
195 u_int32_t refcnt;
196 u_int32_t flags;
197 } *ext_refflags;
1c79356b
A
198};
199
b0d623f7
A
200/* define m_ext to a type since it gets redefined below */
201typedef struct m_ext _m_ext_t;
202
1c79356b
A
203struct mbuf {
204 struct m_hdr m_hdr;
205 union {
206 struct {
207 struct pkthdr MH_pkthdr; /* M_PKTHDR set */
208 union {
209 struct m_ext MH_ext; /* M_EXT set */
b0d623f7 210 char MH_databuf[_MHLEN];
1c79356b
A
211 } MH_dat;
212 } MH;
b0d623f7 213 char M_databuf[_MLEN]; /* !M_PKTHDR, !M_EXT */
1c79356b
A
214 } M_dat;
215};
216
217#define m_next m_hdr.mh_next
218#define m_len m_hdr.mh_len
219#define m_data m_hdr.mh_data
220#define m_type m_hdr.mh_type
221#define m_flags m_hdr.mh_flags
222#define m_nextpkt m_hdr.mh_nextpkt
223#define m_act m_nextpkt
224#define m_pkthdr M_dat.MH.MH_pkthdr
225#define m_ext M_dat.MH.MH_dat.MH_ext
226#define m_pktdat M_dat.MH.MH_dat.MH_databuf
227#define m_dat M_dat.M_databuf
228
229/* mbuf flags */
230#define M_EXT 0x0001 /* has associated external storage */
231#define M_PKTHDR 0x0002 /* start of record */
232#define M_EOR 0x0004 /* end of record */
233#define M_PROTO1 0x0008 /* protocol-specific */
9bccf70c
A
234#define M_PROTO2 0x0010 /* protocol-specific */
235#define M_PROTO3 0x0020 /* protocol-specific */
236#define M_PROTO4 0x0040 /* protocol-specific */
237#define M_PROTO5 0x0080 /* protocol-specific */
1c79356b
A
238
239/* mbuf pkthdr flags, also in m_flags */
240#define M_BCAST 0x0100 /* send/received as link-level broadcast */
241#define M_MCAST 0x0200 /* send/received as link-level multicast */
9bccf70c
A
242#define M_FRAG 0x0400 /* packet is a fragment of a larger packet */
243#define M_FIRSTFRAG 0x0800 /* packet is first fragment */
244#define M_LASTFRAG 0x1000 /* packet is last fragment */
91447636 245#define M_PROMISC 0x2000 /* packet is promiscuous (shouldn't go to stack) */
1c79356b
A
246
247/* flags copied when copying m_pkthdr */
91447636
A
248#define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO2|M_PROTO3 | \
249 M_PROTO4|M_PROTO5|M_BCAST|M_MCAST|M_FRAG | \
250 M_FIRSTFRAG|M_LASTFRAG|M_PROMISC)
1c79356b
A
251
252/* flags indicating hw checksum support and sw checksum requirements [freebsd4.1]*/
253#define CSUM_IP 0x0001 /* will csum IP */
254#define CSUM_TCP 0x0002 /* will csum TCP */
255#define CSUM_UDP 0x0004 /* will csum UDP */
256#define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */
257#define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */
258
259#define CSUM_IP_CHECKED 0x0100 /* did csum IP */
260#define CSUM_IP_VALID 0x0200 /* ... the csum is valid */
261#define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */
262#define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */
0b4e3aa0 263#define CSUM_TCP_SUM16 0x1000 /* simple TCP Sum16 computation */
1c79356b
A
264
265#define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP)
266#define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */
4a249263
A
267/*
268 * Note: see also IF_HWASSIST_CSUM defined in <net/if_var.h>
269 */
270/* bottom 16 bits reserved for hardware checksum */
271#define CSUM_CHECKSUM_MASK 0xffff
272
273/* VLAN tag present */
274#define CSUM_VLAN_TAG_VALID 0x10000 /* vlan_tag field is valid */
b0d623f7
A
275
276/* TCP Segment Offloading requested on this mbuf */
277#define CSUM_TSO_IPV4 0x100000 /* This mbuf needs to be segmented by the NIC */
278#define CSUM_TSO_IPV6 0x200000 /* This mbuf needs to be segmented by the NIC */
279#endif /* KERNEL_PRIVATE */
1c79356b
A
280
281
282/* mbuf types */
283#define MT_FREE 0 /* should be on free list */
284#define MT_DATA 1 /* dynamic (data) allocation */
285#define MT_HEADER 2 /* packet header */
286#define MT_SOCKET 3 /* socket structure */
287#define MT_PCB 4 /* protocol control block */
288#define MT_RTABLE 5 /* routing tables */
289#define MT_HTABLE 6 /* IMP host tables */
290#define MT_ATABLE 7 /* address resolution tables */
291#define MT_SONAME 8 /* socket name */
292#define MT_SOOPTS 10 /* socket options */
293#define MT_FTABLE 11 /* fragment reassembly header */
294#define MT_RIGHTS 12 /* access rights */
295#define MT_IFADDR 13 /* interface address */
296#define MT_CONTROL 14 /* extra-data protocol message */
297#define MT_OOBDATA 15 /* expedited data */
91447636 298#define MT_TAG 16 /* volatile metadata associated to pkts */
1c79356b
A
299#define MT_MAX 32 /* enough? */
300
91447636
A
301#ifdef KERNEL_PRIVATE
302
1c79356b
A
303/* flags to m_get/MGET */
304/* Need to include malloc.h to get right options for malloc */
305#include <sys/malloc.h>
306
307#define M_DONTWAIT M_NOWAIT
308#define M_WAIT M_WAITOK
309
1c79356b
A
310/*
311 * mbuf allocation/deallocation macros:
312 *
313 * MGET(struct mbuf *m, int how, int type)
314 * allocates an mbuf and initializes it to contain internal data.
315 *
316 * MGETHDR(struct mbuf *m, int how, int type)
317 * allocates an mbuf and initializes it to contain a packet header
318 * and internal data.
319 */
320
e3027f41 321#if 1
9bccf70c 322#define MCHECK(m) m_mcheck(m)
1c79356b
A
323#else
324#define MCHECK(m)
325#endif
326
9bccf70c
A
327#define MGET(m, how, type) ((m) = m_get((how), (type)))
328
329#define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type)))
1c79356b
A
330
331/*
332 * Mbuf cluster macros.
333 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
334 * MCLGET adds such clusters to a normal mbuf;
335 * the flag M_EXT is set upon success.
336 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
337 * freeing the cluster if the reference count has reached 0.
338 *
339 * Normal mbuf clusters are normally treated as character arrays
340 * after allocation, but use the first word of the buffer as a free list
341 * pointer while on the free list.
342 */
343union mcluster {
344 union mcluster *mcl_next;
345 char mcl_buf[MCLBYTES];
346};
347
9bccf70c
A
348#define MCLALLOC(p, how) ((p) = m_mclalloc(how))
349
350#define MCLFREE(p) m_mclfree(p)
351
352#define MCLGET(m, how) ((m) = m_mclget(m, how))
353
91447636
A
354/*
355 * Mbuf big cluster
356 */
357
358union mbigcluster {
359 union mbigcluster *mbc_next;
360 char mbc_buf[NBPG];
361};
362
2d21ac55
A
363#define M16KCLBYTES (16 * 1024)
364
365union m16kcluster {
366 union m16kcluster *m16kcl_next;
367 char m16kcl_buf[M16KCLBYTES];
368};
91447636 369
9bccf70c 370#define MCLHASREFERENCE(m) m_mclhasreference(m)
1c79356b
A
371
372/*
373 * MFREE(struct mbuf *m, struct mbuf *n)
374 * Free a single mbuf and associated external storage.
375 * Place the successor, if any, in n.
376 */
377
9bccf70c 378#define MFREE(m, n) ((n) = m_free(m))
1c79356b
A
379
380/*
381 * Copy mbuf pkthdr from from to to.
382 * from must have M_PKTHDR set, and to must be empty.
383 * aux pointer will be moved to `to'.
384 */
9bccf70c 385#define M_COPY_PKTHDR(to, from) m_copy_pkthdr(to, from)
1c79356b
A
386
387/*
388 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
389 * an object of the specified size at the end of the mbuf, longword aligned.
390 */
9bccf70c 391#define M_ALIGN(m, len) \
1c79356b
A
392 { (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); }
393/*
394 * As above, for mbufs allocated with m_gethdr/MGETHDR
395 * or initialized by M_COPY_PKTHDR.
396 */
397#define MH_ALIGN(m, len) \
398 { (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); }
399
400/*
401 * Compute the amount of space available
402 * before the current start of data in an mbuf.
403 * Subroutine - data not available if certain references.
404 */
1c79356b
A
405#define M_LEADINGSPACE(m) m_leadingspace(m)
406
407/*
408 * Compute the amount of space available
409 * after the end of data in an mbuf.
410 * Subroutine - data not available if certain references.
411 */
1c79356b
A
412#define M_TRAILINGSPACE(m) m_trailingspace(m)
413
414/*
415 * Arrange to prepend space of size plen to mbuf m.
416 * If a new mbuf must be allocated, how specifies whether to wait.
417 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
418 * is freed and m is set to NULL.
419 */
9bccf70c 420#define M_PREPEND(m, plen, how) ((m) = m_prepend_2((m), (plen), (how)))
1c79356b
A
421
422/* change mbuf to new type */
9bccf70c 423#define MCHTYPE(m, t) m_mchtype(m, t)
1c79356b
A
424
425/* length to m_copy to copy all */
426#define M_COPYALL 1000000000
427
428/* compatiblity with 4.3 */
429#define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
430
b0d623f7
A
431#define MBSHIFT 20 /* 1MB */
432#define GBSHIFT 30 /* 1GB */
c910b4d9 433
91447636
A
434#endif /* KERNEL_PRIVATE */
435
1c79356b 436/*
2d21ac55 437 * Mbuf statistics (legacy).
1c79356b
A
438 */
439struct mbstat {
2d21ac55
A
440 u_int32_t m_mbufs; /* mbufs obtained from page pool */
441 u_int32_t m_clusters; /* clusters obtained from page pool */
442 u_int32_t m_spare; /* spare field */
443 u_int32_t m_clfree; /* free clusters */
444 u_int32_t m_drops; /* times failed to find space */
445 u_int32_t m_wait; /* times waited for space */
446 u_int32_t m_drain; /* times drained protocols for space */
447 u_short m_mtypes[256]; /* type specific mbuf allocations */
448 u_int32_t m_mcfail; /* times m_copym failed */
449 u_int32_t m_mpfail; /* times m_pullup failed */
450 u_int32_t m_msize; /* length of an mbuf */
451 u_int32_t m_mclbytes; /* length of an mbuf cluster */
452 u_int32_t m_minclsize; /* min length of data to allocate a cluster */
453 u_int32_t m_mlen; /* length of data in an mbuf */
454 u_int32_t m_mhlen; /* length of data in a header mbuf */
455 u_int32_t m_bigclusters; /* clusters obtained from page pool */
456 u_int32_t m_bigclfree; /* free clusters */
457 u_int32_t m_bigmclbytes; /* length of an mbuf cluster */
91447636
A
458};
459
460/* Compatibillity with 10.3 */
461struct ombstat {
2d21ac55
A
462 u_int32_t m_mbufs; /* mbufs obtained from page pool */
463 u_int32_t m_clusters; /* clusters obtained from page pool */
464 u_int32_t m_spare; /* spare field */
465 u_int32_t m_clfree; /* free clusters */
466 u_int32_t m_drops; /* times failed to find space */
467 u_int32_t m_wait; /* times waited for space */
468 u_int32_t m_drain; /* times drained protocols for space */
469 u_short m_mtypes[256]; /* type specific mbuf allocations */
470 u_int32_t m_mcfail; /* times m_copym failed */
471 u_int32_t m_mpfail; /* times m_pullup failed */
472 u_int32_t m_msize; /* length of an mbuf */
473 u_int32_t m_mclbytes; /* length of an mbuf cluster */
474 u_int32_t m_minclsize; /* min length of data to allocate a cluster */
475 u_int32_t m_mlen; /* length of data in an mbuf */
476 u_int32_t m_mhlen; /* length of data in a header mbuf */
1c79356b
A
477};
478
479/*
2d21ac55 480 * mbuf class statistics.
1c79356b 481 */
2d21ac55
A
482#define MAX_MBUF_CNAME 15
483
b0d623f7
A
484#if defined(KERNEL_PRIVATE)
485/* For backwards compatibility with 32-bit userland process */
486struct omb_class_stat {
487 char mbcl_cname[MAX_MBUF_CNAME + 1]; /* class name */
488 u_int32_t mbcl_size; /* buffer size */
489 u_int32_t mbcl_total; /* # of buffers created */
490 u_int32_t mbcl_active; /* # of active buffers */
491 u_int32_t mbcl_infree; /* # of available buffers */
492 u_int32_t mbcl_slab_cnt; /* # of available slabs */
493 u_int64_t mbcl_alloc_cnt; /* # of times alloc is called */
494 u_int64_t mbcl_free_cnt; /* # of times free is called */
495 u_int64_t mbcl_notified; /* # of notified wakeups */
496 u_int64_t mbcl_purge_cnt; /* # of purges so far */
497 u_int64_t mbcl_fail_cnt; /* # of allocation failures */
498 u_int32_t mbcl_ctotal; /* total only for this class */
499 /*
500 * Cache layer statistics
501 */
502 u_int32_t mbcl_mc_state; /* cache state (see below) */
503 u_int32_t mbcl_mc_cached; /* # of cached buffers */
504 u_int32_t mbcl_mc_waiter_cnt; /* # waiters on the cache */
505 u_int32_t mbcl_mc_wretry_cnt; /* # of wait retries */
506 u_int32_t mbcl_mc_nwretry_cnt; /* # of no-wait retry attempts */
507 u_int64_t mbcl_reserved[4]; /* for future use */
508} __attribute__((__packed__));
509#endif /* KERNEL_PRIVATE */
510
2d21ac55
A
511typedef struct mb_class_stat {
512 char mbcl_cname[MAX_MBUF_CNAME + 1]; /* class name */
513 u_int32_t mbcl_size; /* buffer size */
514 u_int32_t mbcl_total; /* # of buffers created */
515 u_int32_t mbcl_active; /* # of active buffers */
516 u_int32_t mbcl_infree; /* # of available buffers */
517 u_int32_t mbcl_slab_cnt; /* # of available slabs */
b0d623f7
A
518#if defined(KERNEL) || defined(__LP64__)
519 u_int32_t mbcl_pad; /* padding */
520#endif /* KERNEL || __LP64__ */
2d21ac55
A
521 u_int64_t mbcl_alloc_cnt; /* # of times alloc is called */
522 u_int64_t mbcl_free_cnt; /* # of times free is called */
523 u_int64_t mbcl_notified; /* # of notified wakeups */
524 u_int64_t mbcl_purge_cnt; /* # of purges so far */
525 u_int64_t mbcl_fail_cnt; /* # of allocation failures */
526 u_int32_t mbcl_ctotal; /* total only for this class */
527 /*
528 * Cache layer statistics
529 */
530 u_int32_t mbcl_mc_state; /* cache state (see below) */
531 u_int32_t mbcl_mc_cached; /* # of cached buffers */
532 u_int32_t mbcl_mc_waiter_cnt; /* # waiters on the cache */
533 u_int32_t mbcl_mc_wretry_cnt; /* # of wait retries */
534 u_int32_t mbcl_mc_nwretry_cnt; /* # of no-wait retry attempts */
535 u_int64_t mbcl_reserved[4]; /* for future use */
536} mb_class_stat_t;
537
538#define MCS_DISABLED 0 /* cache is permanently disabled */
539#define MCS_ONLINE 1 /* cache is online */
540#define MCS_PURGING 2 /* cache is being purged */
541#define MCS_OFFLINE 3 /* cache is offline (resizing) */
542
b0d623f7
A
543#if defined(KERNEL_PRIVATE)
544/* For backwards compatibility with 32-bit userland process */
545struct omb_stat {
546 u_int32_t mbs_cnt; /* number of classes */
547 struct omb_class_stat mbs_class[1]; /* class array */
548} __attribute__((__packed__));
549#endif /* KERNEL_PRIVATE */
550
2d21ac55
A
551typedef struct mb_stat {
552 u_int32_t mbs_cnt; /* number of classes */
b0d623f7
A
553#if defined(KERNEL) || defined(__LP64__)
554 u_int32_t mbs_pad; /* padding */
555#endif /* KERNEL || __LP64__ */
2d21ac55
A
556 mb_class_stat_t mbs_class[1]; /* class array */
557} mb_stat_t;
558
559#ifdef KERNEL_PRIVATE
1c79356b
A
560
561#ifdef KERNEL
562extern union mcluster *mbutl; /* virtual address of mclusters */
563extern union mcluster *embutl; /* ending virtual address of mclusters */
1c79356b 564extern struct mbstat mbstat; /* statistics */
b0d623f7 565extern unsigned int nmbclusters; /* number of mapped clusters */
2d21ac55
A
566extern int njcl; /* # of clusters for jumbo sizes */
567extern int njclbytes; /* size of a jumbo cluster */
1c79356b
A
568extern int max_linkhdr; /* largest link-level header */
569extern int max_protohdr; /* largest protocol header */
570extern int max_hdr; /* largest link+protocol header */
571extern int max_datalen; /* MHLEN - max_hdr */
572
91447636 573__BEGIN_DECLS
2d21ac55 574/* Not exported */
b0d623f7 575__private_extern__ unsigned int mbuf_default_ncl(int, uint64_t);
2d21ac55
A
576__private_extern__ void mbinit(void);
577__private_extern__ struct mbuf *m_clattach(struct mbuf *, int, caddr_t,
578 void (*)(caddr_t , u_int, caddr_t), u_int, caddr_t, int);
579__private_extern__ caddr_t m_bigalloc(int);
580__private_extern__ void m_bigfree(caddr_t, u_int, caddr_t);
581__private_extern__ struct mbuf *m_mbigget(struct mbuf *, int);
582__private_extern__ caddr_t m_16kalloc(int);
583__private_extern__ void m_16kfree(caddr_t, u_int, caddr_t);
584__private_extern__ struct mbuf *m_m16kget(struct mbuf *, int);
b0d623f7
A
585__private_extern__ void mbuf_growth_aggressive(void);
586__private_extern__ void mbuf_growth_normal(void);
2d21ac55
A
587
588/* Exported */
91447636
A
589struct mbuf *m_copym(struct mbuf *, int, int, int);
590struct mbuf *m_split(struct mbuf *, int, int);
591struct mbuf *m_free(struct mbuf *);
592struct mbuf *m_get(int, int);
593struct mbuf *m_getpacket(void);
594struct mbuf *m_getclr(int, int);
595struct mbuf *m_gethdr(int, int);
596struct mbuf *m_prepend(struct mbuf *, int, int);
597struct mbuf *m_prepend_2(struct mbuf *, int, int);
598struct mbuf *m_pullup(struct mbuf *, int);
599struct mbuf *m_retry(int, int);
600struct mbuf *m_retryhdr(int, int);
601void m_adj(struct mbuf *, int);
602void m_freem(struct mbuf *);
603int m_freem_list(struct mbuf *);
604struct mbuf *m_devget(char *, int, int, struct ifnet *, void (*)(const void *, void *, size_t));
605char *mcl_to_paddr(char *);
606struct mbuf *m_pulldown(struct mbuf*, int, int, int*);
91447636 607
b0d623f7 608extern struct mbuf *m_getcl(int, int, int);
91447636
A
609struct mbuf *m_mclget(struct mbuf *, int);
610caddr_t m_mclalloc(int);
611void m_mclfree(caddr_t p);
612int m_mclhasreference(struct mbuf *);
613void m_copy_pkthdr(struct mbuf *, struct mbuf*);
614
615int m_mclref(struct mbuf *);
616int m_mclunref(struct mbuf *);
617
618void * m_mtod(struct mbuf *);
619struct mbuf * m_dtom(void *);
620int m_mtocl(void *);
621union mcluster *m_cltom(int );
622
623int m_trailingspace(struct mbuf *);
624int m_leadingspace(struct mbuf *);
625
2d21ac55 626struct mbuf *m_normalize(struct mbuf *m);
91447636
A
627void m_mchtype(struct mbuf *m, int t);
628void m_mcheck(struct mbuf*);
629
b0d623f7
A
630extern void m_copyback(struct mbuf *, int , int , const void *);
631extern struct mbuf *m_copyback_cow(struct mbuf *, int, int, const void *, int);
632extern int m_makewritable(struct mbuf **, int, int, int);
633void m_copydata(struct mbuf *, int , int , void *);
91447636
A
634struct mbuf* m_dup(struct mbuf *m, int how);
635void m_cat(struct mbuf *, struct mbuf *);
636struct mbuf *m_copym_with_hdrs(struct mbuf*, int, int, int, struct mbuf**, int*);
637struct mbuf *m_getpackets(int, int, int);
638struct mbuf * m_getpackethdrs(int , int );
639struct mbuf* m_getpacket_how(int );
640struct mbuf * m_getpackets_internal(unsigned int *, int , int , int , size_t);
641struct mbuf * m_allocpacket_internal(unsigned int * , size_t , unsigned int *, int , int , size_t );
642
643__END_DECLS
9bccf70c 644
91447636
A
645/*
646 Packets may have annotations attached by affixing a list of "packet
647 tags" to the pkthdr structure. Packet tags are dynamically allocated
648 semi-opaque data structures that have a fixed header (struct m_tag)
649 that specifies the size of the memory block and an <id,type> pair that
650 identifies it. The id identifies the module and the type identifies the
651 type of data for that module. The id of zero is reserved for the kernel.
652
653 Note that the packet tag returned by m_tag_allocate has the default
654 memory alignment implemented by malloc. To reference private data one
655 can use a construct like:
656
657 struct m_tag *mtag = m_tag_allocate(...);
658 struct foo *p = (struct foo *)(mtag+1);
659
660 if the alignment of struct m_tag is sufficient for referencing members
661 of struct foo. Otherwise it is necessary to embed struct m_tag within
662 the private data structure to insure proper alignment; e.g.
663
664 struct foo {
665 struct m_tag tag;
666 ...
667 };
668 struct foo *p = (struct foo *) m_tag_allocate(...);
669 struct m_tag *mtag = &p->tag;
670 */
671
672#define KERNEL_MODULE_TAG_ID 0
673
674enum {
675 KERNEL_TAG_TYPE_NONE = 0,
676 KERNEL_TAG_TYPE_DUMMYNET = 1,
677 KERNEL_TAG_TYPE_DIVERT = 2,
678 KERNEL_TAG_TYPE_IPFORWARD = 3,
2d21ac55
A
679 KERNEL_TAG_TYPE_IPFILT = 4,
680 KERNEL_TAG_TYPE_MACLABEL = 5,
681 KERNEL_TAG_TYPE_MAC_POLICY_LABEL = 6,
682 KERNEL_TAG_TYPE_ENCAP = 8,
683 KERNEL_TAG_TYPE_INET6 = 9,
b0d623f7
A
684 KERNEL_TAG_TYPE_IPSEC = 10,
685 KERNEL_TAG_TYPE_PF = 11
91447636
A
686};
687
688/*
689 * As a temporary and low impact solution to replace the even uglier
690 * approach used so far in some parts of the network stack (which relies
691 * on global variables), packet tag-like annotations are stored in MT_TAG
692 * mbufs (or lookalikes) prepended to the actual mbuf chain.
693 *
694 * m_type = MT_TAG
695 * m_flags = m_tag_id
696 * m_next = next buffer in chain.
697 *
698 * BE VERY CAREFUL not to pass these blocks to the mbuf handling routines.
699 */
700#define _m_tag_id m_hdr.mh_flags
701
702__BEGIN_DECLS
703
704/* Packet tag routines */
705struct m_tag *m_tag_alloc(u_int32_t id, u_int16_t type, int len, int wait);
706void m_tag_free(struct m_tag *);
707void m_tag_prepend(struct mbuf *, struct m_tag *);
708void m_tag_unlink(struct mbuf *, struct m_tag *);
709void m_tag_delete(struct mbuf *, struct m_tag *);
710void m_tag_delete_chain(struct mbuf *, struct m_tag *);
711struct m_tag *m_tag_locate(struct mbuf *,u_int32_t id, u_int16_t type,
712 struct m_tag *);
713struct m_tag *m_tag_copy(struct m_tag *, int wait);
714int m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int wait);
715void m_tag_init(struct mbuf *);
716struct m_tag *m_tag_first(struct mbuf *);
717struct m_tag *m_tag_next(struct mbuf *, struct m_tag *);
718
d1ecb069
A
719extern void m_prio_init(struct mbuf *);
720extern void m_prio_background(struct mbuf *);
721
91447636
A
722__END_DECLS
723
724#endif /* KERNEL */
725
726#endif /* KERNEL_PRIVATE */
727#ifdef KERNEL
728#include <sys/kpi_mbuf.h>
b0d623f7 729#endif /* KERNEL */
1c79356b 730#endif /* !_SYS_MBUF_H_ */