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