]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/mbuf.h
xnu-517.3.7.tar.gz
[apple/xnu.git] / bsd / sys / mbuf.h
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
26/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27/*
28 * Mach Operating System
29 * Copyright (c) 1987 Carnegie-Mellon University
30 * All rights reserved. The CMU software License Agreement specifies
31 * the terms and conditions for use and redistribution.
32 */
33/*
34 * Copyright (c) 1994 NeXT Computer, Inc. All rights reserved.
35 *
36 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
37 * All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)mbuf.h 8.3 (Berkeley) 1/21/94
68 **********************************************************************
69 * HISTORY
70 * 20-May-95 Mac Gillon (mgillon) at NeXT
71 * New version based on 4.4
72 * Purged old history
73 */
74
75#ifndef _SYS_MBUF_H_
76#define _SYS_MBUF_H_
77
9bccf70c 78#include <sys/appleapiopts.h>
1c79356b
A
79#include <sys/lock.h>
80
81/*
82 * Mbufs are of a single size, MSIZE (machine/param.h), which
83 * includes overhead. An mbuf may add a single "mbuf cluster" of size
84 * MCLBYTES (also in machine/param.h), which has no additional overhead
85 * and is used instead of the internal data area; this is done when
86 * at least MINCLSIZE of data must be stored.
87 */
88
9bccf70c 89#ifdef __APPLE_API_UNSTABLE
1c79356b
A
90#define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
91#define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
92
93#define MINCLSIZE (MHLEN + MLEN) /* smallest amount to put in cluster */
94#define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */
95
96#define NMBPCL (sizeof(union mcluster) / sizeof(struct mbuf))
97
1c79356b
A
98/*
99 * Macros for type conversion
100 * mtod(m,t) - convert mbuf pointer to data pointer of correct type
101 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX)
102 * mtocl(x) - convert pointer within cluster to cluster index #
103 * cltom(x) - convert cluster # to ptr to beginning of cluster
104 */
9bccf70c
A
105#define mtod(m,t) ((t)m_mtod(m))
106#define dtom(x) m_dtom(x)
107#define mtocl(x) m_mtocl(x)
108#define cltom(x) m_cltom(x)
1c79356b 109
9bccf70c
A
110#define MCLREF(p) m_mclref(p)
111#define MCLUNREF(p) m_mclunref(p)
1c79356b
A
112
113/* header at beginning of each mbuf: */
114struct m_hdr {
115 struct mbuf *mh_next; /* next buffer in chain */
116 struct mbuf *mh_nextpkt; /* next chain in queue/record */
117 long mh_len; /* amount of data in this mbuf */
118 caddr_t mh_data; /* location of data */
119 short mh_type; /* type of data in this mbuf */
120 short mh_flags; /* flags; see below */
121};
122
123/* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
124struct pkthdr {
125 int len; /* total packet length */
126 struct ifnet *rcvif; /* rcv interface */
127
128 /* variables for ip and tcp reassembly */
129 void *header; /* pointer to packet header */
130 /* variables for hardware checksum */
131 int csum_flags; /* flags regarding checksum */
132 int csum_data; /* data field used by csum routines */
133 struct mbuf *aux; /* extra data buffer; ipsec/others */
134 void *reserved1; /* for future use */
135 void *reserved2; /* for future use */
136};
137
138
139/* description of external storage mapped into mbuf, valid if M_EXT set */
140struct m_ext {
141 caddr_t ext_buf; /* start of buffer */
55e303ae 142 void (*ext_free)(caddr_t , u_int, caddr_t); /* free routine if not the usual */
1c79356b
A
143 u_int ext_size; /* size of buffer, for ext_free */
144 caddr_t ext_arg; /* additional ext_free argument */
145 struct ext_refsq { /* references held */
146 struct ext_refsq *forward, *backward;
147 } ext_refs;
148};
149
150struct mbuf {
151 struct m_hdr m_hdr;
152 union {
153 struct {
154 struct pkthdr MH_pkthdr; /* M_PKTHDR set */
155 union {
156 struct m_ext MH_ext; /* M_EXT set */
157 char MH_databuf[MHLEN];
158 } MH_dat;
159 } MH;
160 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
161 } M_dat;
162};
163
164#define m_next m_hdr.mh_next
165#define m_len m_hdr.mh_len
166#define m_data m_hdr.mh_data
167#define m_type m_hdr.mh_type
168#define m_flags m_hdr.mh_flags
169#define m_nextpkt m_hdr.mh_nextpkt
170#define m_act m_nextpkt
171#define m_pkthdr M_dat.MH.MH_pkthdr
172#define m_ext M_dat.MH.MH_dat.MH_ext
173#define m_pktdat M_dat.MH.MH_dat.MH_databuf
174#define m_dat M_dat.M_databuf
175
176/* mbuf flags */
177#define M_EXT 0x0001 /* has associated external storage */
178#define M_PKTHDR 0x0002 /* start of record */
179#define M_EOR 0x0004 /* end of record */
180#define M_PROTO1 0x0008 /* protocol-specific */
9bccf70c
A
181#define M_PROTO2 0x0010 /* protocol-specific */
182#define M_PROTO3 0x0020 /* protocol-specific */
183#define M_PROTO4 0x0040 /* protocol-specific */
184#define M_PROTO5 0x0080 /* protocol-specific */
1c79356b
A
185
186/* mbuf pkthdr flags, also in m_flags */
187#define M_BCAST 0x0100 /* send/received as link-level broadcast */
188#define M_MCAST 0x0200 /* send/received as link-level multicast */
9bccf70c
A
189#define M_FRAG 0x0400 /* packet is a fragment of a larger packet */
190#define M_FIRSTFRAG 0x0800 /* packet is first fragment */
191#define M_LASTFRAG 0x1000 /* packet is last fragment */
1c79356b
A
192
193/* flags copied when copying m_pkthdr */
9bccf70c
A
194#define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO1|M_PROTO2|M_PROTO3 | \
195 M_PROTO4|M_PROTO5|M_BCAST|M_MCAST|M_FRAG)
1c79356b
A
196
197/* flags indicating hw checksum support and sw checksum requirements [freebsd4.1]*/
198#define CSUM_IP 0x0001 /* will csum IP */
199#define CSUM_TCP 0x0002 /* will csum TCP */
200#define CSUM_UDP 0x0004 /* will csum UDP */
201#define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */
202#define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */
203
204#define CSUM_IP_CHECKED 0x0100 /* did csum IP */
205#define CSUM_IP_VALID 0x0200 /* ... the csum is valid */
206#define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */
207#define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */
0b4e3aa0 208#define CSUM_TCP_SUM16 0x1000 /* simple TCP Sum16 computation */
1c79356b
A
209
210#define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP)
211#define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */
212
213
214/* mbuf types */
215#define MT_FREE 0 /* should be on free list */
216#define MT_DATA 1 /* dynamic (data) allocation */
217#define MT_HEADER 2 /* packet header */
218#define MT_SOCKET 3 /* socket structure */
219#define MT_PCB 4 /* protocol control block */
220#define MT_RTABLE 5 /* routing tables */
221#define MT_HTABLE 6 /* IMP host tables */
222#define MT_ATABLE 7 /* address resolution tables */
223#define MT_SONAME 8 /* socket name */
224#define MT_SOOPTS 10 /* socket options */
225#define MT_FTABLE 11 /* fragment reassembly header */
226#define MT_RIGHTS 12 /* access rights */
227#define MT_IFADDR 13 /* interface address */
228#define MT_CONTROL 14 /* extra-data protocol message */
229#define MT_OOBDATA 15 /* expedited data */
230#define MT_MAX 32 /* enough? */
231
232/* flags to m_get/MGET */
233/* Need to include malloc.h to get right options for malloc */
234#include <sys/malloc.h>
235
236#define M_DONTWAIT M_NOWAIT
237#define M_WAIT M_WAITOK
238
9bccf70c
A
239#ifdef __APPLE_API_PRIVATE
240
1c79356b
A
241/*
242 * mbuf utility macros:
243 *
244 * MBUFLOCK(code)
245 * prevents a section of code from from being interrupted by network
246 * drivers.
247 */
248
249
250extern
251decl_simple_lock_data(, mbuf_slock);
0b4e3aa0
A
252#define MBUF_LOCK() usimple_lock(&mbuf_slock);
253#define MBUF_UNLOCK() usimple_unlock(&mbuf_slock);
1c79356b
A
254#define MBUF_LOCKINIT() simple_lock_init(&mbuf_slock);
255
9bccf70c 256#endif /* __APPLE_API_PRIVATE */
1c79356b
A
257
258/*
259 * mbuf allocation/deallocation macros:
260 *
261 * MGET(struct mbuf *m, int how, int type)
262 * allocates an mbuf and initializes it to contain internal data.
263 *
264 * MGETHDR(struct mbuf *m, int how, int type)
265 * allocates an mbuf and initializes it to contain a packet header
266 * and internal data.
267 */
268
e3027f41 269#if 1
9bccf70c 270#define MCHECK(m) m_mcheck(m)
1c79356b
A
271#else
272#define MCHECK(m)
273#endif
274
9bccf70c 275#ifdef __APPLE_API_PRIVATE
1c79356b
A
276extern struct mbuf *mfree; /* mbuf free list */
277extern simple_lock_data_t mbuf_slock;
9bccf70c 278#endif /* __APPLE_API_PRIVATE */
1c79356b 279
9bccf70c
A
280
281#define MGET(m, how, type) ((m) = m_get((how), (type)))
282
283#define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type)))
1c79356b
A
284
285/*
286 * Mbuf cluster macros.
287 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
288 * MCLGET adds such clusters to a normal mbuf;
289 * the flag M_EXT is set upon success.
290 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
291 * freeing the cluster if the reference count has reached 0.
292 *
293 * Normal mbuf clusters are normally treated as character arrays
294 * after allocation, but use the first word of the buffer as a free list
295 * pointer while on the free list.
296 */
297union mcluster {
298 union mcluster *mcl_next;
299 char mcl_buf[MCLBYTES];
300};
301
9bccf70c
A
302#define MCLALLOC(p, how) ((p) = m_mclalloc(how))
303
304#define MCLFREE(p) m_mclfree(p)
305
306#define MCLGET(m, how) ((m) = m_mclget(m, how))
307
308#define MCLHASREFERENCE(m) m_mclhasreference(m)
1c79356b
A
309
310/*
311 * MFREE(struct mbuf *m, struct mbuf *n)
312 * Free a single mbuf and associated external storage.
313 * Place the successor, if any, in n.
314 */
315
9bccf70c 316#define MFREE(m, n) ((n) = m_free(m))
1c79356b
A
317
318/*
319 * Copy mbuf pkthdr from from to to.
320 * from must have M_PKTHDR set, and to must be empty.
321 * aux pointer will be moved to `to'.
322 */
9bccf70c 323#define M_COPY_PKTHDR(to, from) m_copy_pkthdr(to, from)
1c79356b
A
324
325/*
326 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
327 * an object of the specified size at the end of the mbuf, longword aligned.
328 */
9bccf70c 329#define M_ALIGN(m, len) \
1c79356b
A
330 { (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); }
331/*
332 * As above, for mbufs allocated with m_gethdr/MGETHDR
333 * or initialized by M_COPY_PKTHDR.
334 */
335#define MH_ALIGN(m, len) \
336 { (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); }
337
338/*
339 * Compute the amount of space available
340 * before the current start of data in an mbuf.
341 * Subroutine - data not available if certain references.
342 */
1c79356b
A
343#define M_LEADINGSPACE(m) m_leadingspace(m)
344
345/*
346 * Compute the amount of space available
347 * after the end of data in an mbuf.
348 * Subroutine - data not available if certain references.
349 */
1c79356b
A
350#define M_TRAILINGSPACE(m) m_trailingspace(m)
351
352/*
353 * Arrange to prepend space of size plen to mbuf m.
354 * If a new mbuf must be allocated, how specifies whether to wait.
355 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
356 * is freed and m is set to NULL.
357 */
9bccf70c 358#define M_PREPEND(m, plen, how) ((m) = m_prepend_2((m), (plen), (how)))
1c79356b
A
359
360/* change mbuf to new type */
9bccf70c 361#define MCHTYPE(m, t) m_mchtype(m, t)
1c79356b
A
362
363/* length to m_copy to copy all */
364#define M_COPYALL 1000000000
365
366/* compatiblity with 4.3 */
367#define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
368
369/*
370 * Mbuf statistics.
371 */
372struct mbstat {
373 u_long m_mbufs; /* mbufs obtained from page pool */
374 u_long m_clusters; /* clusters obtained from page pool */
375 u_long m_spare; /* spare field */
376 u_long m_clfree; /* free clusters */
377 u_long m_drops; /* times failed to find space */
378 u_long m_wait; /* times waited for space */
379 u_long m_drain; /* times drained protocols for space */
380 u_short m_mtypes[256]; /* type specific mbuf allocations */
381 u_long m_mcfail; /* times m_copym failed */
382 u_long m_mpfail; /* times m_pullup failed */
383 u_long m_msize; /* length of an mbuf */
384 u_long m_mclbytes; /* length of an mbuf cluster */
385 u_long m_minclsize; /* min length of data to allocate a cluster */
386 u_long m_mlen; /* length of data in an mbuf */
387 u_long m_mhlen; /* length of data in a header mbuf */
388};
389
390/*
391 * pkthdr.aux type tags.
392 */
393struct mauxtag {
394 int af;
395 int type;
396};
397
398#ifdef KERNEL
399extern union mcluster *mbutl; /* virtual address of mclusters */
400extern union mcluster *embutl; /* ending virtual address of mclusters */
401extern short *mclrefcnt; /* cluster reference counts */
402extern int *mcl_paddr; /* physical addresses of clusters */
403extern struct mbstat mbstat; /* statistics */
404extern int nmbclusters; /* number of mapped clusters */
405extern union mcluster *mclfree; /* free mapped cluster list */
406extern int max_linkhdr; /* largest link-level header */
407extern int max_protohdr; /* largest protocol header */
408extern int max_hdr; /* largest link+protocol header */
409extern int max_datalen; /* MHLEN - max_hdr */
410
411struct mbuf *m_copym __P((struct mbuf *, int, int, int));
9bccf70c 412struct mbuf *m_split __P((struct mbuf *, int, int));
1c79356b
A
413struct mbuf *m_free __P((struct mbuf *));
414struct mbuf *m_get __P((int, int));
415struct mbuf *m_getpacket __P((void));
416struct mbuf *m_getclr __P((int, int));
417struct mbuf *m_gethdr __P((int, int));
418struct mbuf *m_prepend __P((struct mbuf *, int, int));
9bccf70c 419struct mbuf *m_prepend_2 __P((struct mbuf *, int, int));
1c79356b
A
420struct mbuf *m_pullup __P((struct mbuf *, int));
421struct mbuf *m_retry __P((int, int));
422struct mbuf *m_retryhdr __P((int, int));
423void m_adj __P((struct mbuf *, int));
424int m_clalloc __P((int, int));
425void m_freem __P((struct mbuf *));
426int m_freem_list __P((struct mbuf *));
427struct mbuf *m_devget __P((char *, int, int, struct ifnet *, void (*)()));
428char *mcl_to_paddr __P((char *));
9bccf70c 429struct mbuf *m_pulldown __P((struct mbuf*, int, int, int*));
1c79356b
A
430struct mbuf *m_aux_add __P((struct mbuf *, int, int));
431struct mbuf *m_aux_find __P((struct mbuf *, int, int));
432void m_aux_delete __P((struct mbuf *, struct mbuf *));
9bccf70c
A
433
434struct mbuf *m_mclget __P((struct mbuf *, int));
435caddr_t m_mclalloc __P((int));
436void m_mclfree __P((caddr_t p));
437int m_mclhasreference __P((struct mbuf *));
438void m_copy_pkthdr __P((struct mbuf *, struct mbuf*));
439
440int m_mclref __P((struct mbuf *));
441int m_mclunref __P((struct mbuf *));
442
443void * m_mtod __P((struct mbuf *));
444struct mbuf * m_dtom __P((void *));
445int m_mtocl __P((void *));
446union mcluster *m_cltom __P((int ));
447
448int m_trailingspace __P((struct mbuf *));
449int m_leadingspace __P((struct mbuf *));
450
451void m_mchtype __P((struct mbuf *m, int t));
452
453void m_mcheck __P((struct mbuf*));
454
1c79356b 455#endif
9bccf70c 456#endif /* __APPLE_API_UNSTABLE */
1c79356b 457#endif /* !_SYS_MBUF_H_ */