]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/mbuf.h
e9a59c1fbc3c6c71a5501e5908ad90ab8d30f857
[apple/xnu.git] / bsd / sys / mbuf.h
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
31 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 /*
33 * Mach Operating System
34 * Copyright (c) 1987 Carnegie-Mellon University
35 * All rights reserved. The CMU software License Agreement specifies
36 * the terms and conditions for use and redistribution.
37 */
38 /*
39 * Copyright (c) 1994 NeXT Computer, Inc. All rights reserved.
40 *
41 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)mbuf.h 8.3 (Berkeley) 1/21/94
73 **********************************************************************
74 * HISTORY
75 * 20-May-95 Mac Gillon (mgillon) at NeXT
76 * New version based on 4.4
77 * Purged old history
78 */
79
80 #ifndef _SYS_MBUF_H_
81 #define _SYS_MBUF_H_
82
83 #include <sys/cdefs.h>
84 #include <sys/appleapiopts.h>
85
86 #ifdef KERNEL_PRIVATE
87
88 #include <sys/lock.h>
89 #include <sys/queue.h>
90
91 /*
92 * Mbufs are of a single size, MSIZE (machine/param.h), which
93 * includes overhead. An mbuf may add a single "mbuf cluster" of size
94 * MCLBYTES (also in machine/param.h), which has no additional overhead
95 * and is used instead of the internal data area; this is done when
96 * at least MINCLSIZE of data must be stored.
97 */
98
99 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
100 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
101
102 #define MINCLSIZE (MHLEN + MLEN) /* smallest amount to put in cluster */
103 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */
104
105 #define NMBPCL (sizeof(union mcluster) / sizeof(struct mbuf))
106
107 /*
108 * Macros for type conversion
109 * mtod(m,t) - convert mbuf pointer to data pointer of correct type
110 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX)
111 * mtocl(x) - convert pointer within cluster to cluster index #
112 * cltom(x) - convert cluster # to ptr to beginning of cluster
113 */
114 #define mtod(m,t) ((t)m_mtod(m))
115 #define dtom(x) m_dtom(x)
116 #define mtocl(x) m_mtocl(x)
117 #define cltom(x) m_cltom(x)
118
119 #define MCLREF(p) m_mclref(p)
120 #define MCLUNREF(p) m_mclunref(p)
121
122 /* header at beginning of each mbuf: */
123 struct m_hdr {
124 struct mbuf *mh_next; /* next buffer in chain */
125 struct mbuf *mh_nextpkt; /* next chain in queue/record */
126 long mh_len; /* amount of data in this mbuf */
127 caddr_t mh_data; /* location of data */
128 short mh_type; /* type of data in this mbuf */
129 short mh_flags; /* flags; see below */
130 };
131
132 /*
133 * Packet tag structure (see below for details).
134 */
135 struct m_tag {
136 SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */
137 u_int16_t m_tag_type; /* Module specific type */
138 u_int16_t m_tag_len; /* Length of data */
139 u_int32_t m_tag_id; /* Module ID */
140 };
141
142 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
143 struct pkthdr {
144 int len; /* total packet length */
145 struct ifnet *rcvif; /* rcv interface */
146
147 /* variables for ip and tcp reassembly */
148 void *header; /* pointer to packet header */
149 /* variables for hardware checksum */
150 #ifdef KERNEL_PRIVATE
151 /* Note: csum_flags is used for hardware checksum and VLAN */
152 #endif KERNEL_PRIVATE
153 int csum_flags; /* flags regarding checksum */
154 int csum_data; /* data field used by csum routines */
155 struct mbuf *aux; /* extra data buffer; ipsec/others */
156 #ifdef KERNEL_PRIVATE
157 u_short vlan_tag; /* VLAN tag, host byte order */
158 u_short socket_id; /* socket id */
159 #else KERNEL_PRIVATE
160 u_int reserved1; /* for future use */
161 #endif KERNEL_PRIVATE
162 SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
163 };
164
165
166 /* description of external storage mapped into mbuf, valid if M_EXT set */
167 struct m_ext {
168 caddr_t ext_buf; /* start of buffer */
169 void (*ext_free)(caddr_t , u_int, caddr_t); /* free routine if not the usual */
170 u_int ext_size; /* size of buffer, for ext_free */
171 caddr_t ext_arg; /* additional ext_free argument */
172 struct ext_refsq { /* references held */
173 struct ext_refsq *forward, *backward;
174 } ext_refs;
175 };
176
177 struct mbuf {
178 struct m_hdr m_hdr;
179 union {
180 struct {
181 struct pkthdr MH_pkthdr; /* M_PKTHDR set */
182 union {
183 struct m_ext MH_ext; /* M_EXT set */
184 char MH_databuf[MHLEN];
185 } MH_dat;
186 } MH;
187 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
188 } M_dat;
189 };
190
191 #define m_next m_hdr.mh_next
192 #define m_len m_hdr.mh_len
193 #define m_data m_hdr.mh_data
194 #define m_type m_hdr.mh_type
195 #define m_flags m_hdr.mh_flags
196 #define m_nextpkt m_hdr.mh_nextpkt
197 #define m_act m_nextpkt
198 #define m_pkthdr M_dat.MH.MH_pkthdr
199 #define m_ext M_dat.MH.MH_dat.MH_ext
200 #define m_pktdat M_dat.MH.MH_dat.MH_databuf
201 #define m_dat M_dat.M_databuf
202
203 /* mbuf flags */
204 #define M_EXT 0x0001 /* has associated external storage */
205 #define M_PKTHDR 0x0002 /* start of record */
206 #define M_EOR 0x0004 /* end of record */
207 #define M_PROTO1 0x0008 /* protocol-specific */
208 #define M_PROTO2 0x0010 /* protocol-specific */
209 #define M_PROTO3 0x0020 /* protocol-specific */
210 #define M_PROTO4 0x0040 /* protocol-specific */
211 #define M_PROTO5 0x0080 /* protocol-specific */
212
213 /* mbuf pkthdr flags, also in m_flags */
214 #define M_BCAST 0x0100 /* send/received as link-level broadcast */
215 #define M_MCAST 0x0200 /* send/received as link-level multicast */
216 #define M_FRAG 0x0400 /* packet is a fragment of a larger packet */
217 #define M_FIRSTFRAG 0x0800 /* packet is first fragment */
218 #define M_LASTFRAG 0x1000 /* packet is last fragment */
219 #define M_PROMISC 0x2000 /* packet is promiscuous (shouldn't go to stack) */
220
221 /* flags copied when copying m_pkthdr */
222 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO2|M_PROTO3 | \
223 M_PROTO4|M_PROTO5|M_BCAST|M_MCAST|M_FRAG | \
224 M_FIRSTFRAG|M_LASTFRAG|M_PROMISC)
225
226 /* flags indicating hw checksum support and sw checksum requirements [freebsd4.1]*/
227 #define CSUM_IP 0x0001 /* will csum IP */
228 #define CSUM_TCP 0x0002 /* will csum TCP */
229 #define CSUM_UDP 0x0004 /* will csum UDP */
230 #define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */
231 #define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */
232
233 #define CSUM_IP_CHECKED 0x0100 /* did csum IP */
234 #define CSUM_IP_VALID 0x0200 /* ... the csum is valid */
235 #define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */
236 #define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */
237 #define CSUM_TCP_SUM16 0x1000 /* simple TCP Sum16 computation */
238
239 #define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP)
240 #define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */
241 /*
242 * Note: see also IF_HWASSIST_CSUM defined in <net/if_var.h>
243 */
244 /* bottom 16 bits reserved for hardware checksum */
245 #define CSUM_CHECKSUM_MASK 0xffff
246
247 /* VLAN tag present */
248 #define CSUM_VLAN_TAG_VALID 0x10000 /* vlan_tag field is valid */
249 #endif KERNEL_PRIVATE
250
251
252 /* mbuf types */
253 #define MT_FREE 0 /* should be on free list */
254 #define MT_DATA 1 /* dynamic (data) allocation */
255 #define MT_HEADER 2 /* packet header */
256 #define MT_SOCKET 3 /* socket structure */
257 #define MT_PCB 4 /* protocol control block */
258 #define MT_RTABLE 5 /* routing tables */
259 #define MT_HTABLE 6 /* IMP host tables */
260 #define MT_ATABLE 7 /* address resolution tables */
261 #define MT_SONAME 8 /* socket name */
262 #define MT_SOOPTS 10 /* socket options */
263 #define MT_FTABLE 11 /* fragment reassembly header */
264 #define MT_RIGHTS 12 /* access rights */
265 #define MT_IFADDR 13 /* interface address */
266 #define MT_CONTROL 14 /* extra-data protocol message */
267 #define MT_OOBDATA 15 /* expedited data */
268 #define MT_TAG 16 /* volatile metadata associated to pkts */
269 #define MT_MAX 32 /* enough? */
270
271 #ifdef KERNEL_PRIVATE
272
273 /* flags to m_get/MGET */
274 /* Need to include malloc.h to get right options for malloc */
275 #include <sys/malloc.h>
276
277 #define M_DONTWAIT M_NOWAIT
278 #define M_WAIT M_WAITOK
279
280 /*
281 * mbuf utility macros:
282 *
283 * MBUFLOCK(code)
284 * prevents a section of code from from being interrupted by network
285 * drivers.
286 */
287
288 #ifdef _KERN_LOCKS_H_
289 extern lck_mtx_t * mbuf_mlock;
290 #else
291 extern void * mbuf_mlock;
292 #endif
293
294 #define MBUF_LOCK() lck_mtx_lock(mbuf_mlock);
295 #define MBUF_UNLOCK() lck_mtx_unlock(mbuf_mlock);
296
297 /*
298 * mbuf allocation/deallocation macros:
299 *
300 * MGET(struct mbuf *m, int how, int type)
301 * allocates an mbuf and initializes it to contain internal data.
302 *
303 * MGETHDR(struct mbuf *m, int how, int type)
304 * allocates an mbuf and initializes it to contain a packet header
305 * and internal data.
306 */
307
308 #if 1
309 #define MCHECK(m) m_mcheck(m)
310 #else
311 #define MCHECK(m)
312 #endif
313
314 extern struct mbuf *mfree; /* mbuf free list */
315
316 #define MGET(m, how, type) ((m) = m_get((how), (type)))
317
318 #define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type)))
319
320 /*
321 * Mbuf cluster macros.
322 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
323 * MCLGET adds such clusters to a normal mbuf;
324 * the flag M_EXT is set upon success.
325 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
326 * freeing the cluster if the reference count has reached 0.
327 *
328 * Normal mbuf clusters are normally treated as character arrays
329 * after allocation, but use the first word of the buffer as a free list
330 * pointer while on the free list.
331 */
332 union mcluster {
333 union mcluster *mcl_next;
334 char mcl_buf[MCLBYTES];
335 };
336
337 #define MCLALLOC(p, how) ((p) = m_mclalloc(how))
338
339 #define MCLFREE(p) m_mclfree(p)
340
341 #define MCLGET(m, how) ((m) = m_mclget(m, how))
342
343 /*
344 * Mbuf big cluster
345 */
346
347 union mbigcluster {
348 union mbigcluster *mbc_next;
349 char mbc_buf[NBPG];
350 };
351
352
353 #define MCLHASREFERENCE(m) m_mclhasreference(m)
354
355 /*
356 * MFREE(struct mbuf *m, struct mbuf *n)
357 * Free a single mbuf and associated external storage.
358 * Place the successor, if any, in n.
359 */
360
361 #define MFREE(m, n) ((n) = m_free(m))
362
363 /*
364 * Copy mbuf pkthdr from from to to.
365 * from must have M_PKTHDR set, and to must be empty.
366 * aux pointer will be moved to `to'.
367 */
368 #define M_COPY_PKTHDR(to, from) m_copy_pkthdr(to, from)
369
370 /*
371 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
372 * an object of the specified size at the end of the mbuf, longword aligned.
373 */
374 #define M_ALIGN(m, len) \
375 { (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); }
376 /*
377 * As above, for mbufs allocated with m_gethdr/MGETHDR
378 * or initialized by M_COPY_PKTHDR.
379 */
380 #define MH_ALIGN(m, len) \
381 { (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); }
382
383 /*
384 * Compute the amount of space available
385 * before the current start of data in an mbuf.
386 * Subroutine - data not available if certain references.
387 */
388 #define M_LEADINGSPACE(m) m_leadingspace(m)
389
390 /*
391 * Compute the amount of space available
392 * after the end of data in an mbuf.
393 * Subroutine - data not available if certain references.
394 */
395 #define M_TRAILINGSPACE(m) m_trailingspace(m)
396
397 /*
398 * Arrange to prepend space of size plen to mbuf m.
399 * If a new mbuf must be allocated, how specifies whether to wait.
400 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
401 * is freed and m is set to NULL.
402 */
403 #define M_PREPEND(m, plen, how) ((m) = m_prepend_2((m), (plen), (how)))
404
405 /* change mbuf to new type */
406 #define MCHTYPE(m, t) m_mchtype(m, t)
407
408 /* length to m_copy to copy all */
409 #define M_COPYALL 1000000000
410
411 /* compatiblity with 4.3 */
412 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
413
414 #endif /* KERNEL_PRIVATE */
415
416 /*
417 * Mbuf statistics.
418 */
419 /* LP64todo - not 64-bit safe */
420 struct mbstat {
421 u_long m_mbufs; /* mbufs obtained from page pool */
422 u_long m_clusters; /* clusters obtained from page pool */
423 u_long m_spare; /* spare field */
424 u_long m_clfree; /* free clusters */
425 u_long m_drops; /* times failed to find space */
426 u_long m_wait; /* times waited for space */
427 u_long m_drain; /* times drained protocols for space */
428 u_short m_mtypes[256]; /* type specific mbuf allocations */
429 u_long m_mcfail; /* times m_copym failed */
430 u_long m_mpfail; /* times m_pullup failed */
431 u_long m_msize; /* length of an mbuf */
432 u_long m_mclbytes; /* length of an mbuf cluster */
433 u_long m_minclsize; /* min length of data to allocate a cluster */
434 u_long m_mlen; /* length of data in an mbuf */
435 u_long m_mhlen; /* length of data in a header mbuf */
436 u_long m_bigclusters; /* clusters obtained from page pool */
437 u_long m_bigclfree; /* free clusters */
438 u_long m_bigmclbytes; /* length of an mbuf cluster */
439 };
440
441 /* Compatibillity with 10.3 */
442 struct ombstat {
443 u_long m_mbufs; /* mbufs obtained from page pool */
444 u_long m_clusters; /* clusters obtained from page pool */
445 u_long m_spare; /* spare field */
446 u_long m_clfree; /* free clusters */
447 u_long m_drops; /* times failed to find space */
448 u_long m_wait; /* times waited for space */
449 u_long m_drain; /* times drained protocols for space */
450 u_short m_mtypes[256]; /* type specific mbuf allocations */
451 u_long m_mcfail; /* times m_copym failed */
452 u_long m_mpfail; /* times m_pullup failed */
453 u_long m_msize; /* length of an mbuf */
454 u_long m_mclbytes; /* length of an mbuf cluster */
455 u_long m_minclsize; /* min length of data to allocate a cluster */
456 u_long m_mlen; /* length of data in an mbuf */
457 u_long m_mhlen; /* length of data in a header mbuf */
458 };
459 #ifdef KERNEL_PRIVATE
460
461 /*
462 * pkthdr.aux type tags.
463 */
464 struct mauxtag {
465 int af;
466 int type;
467 };
468
469 #ifdef KERNEL
470 extern union mcluster *mbutl; /* virtual address of mclusters */
471 extern union mcluster *embutl; /* ending virtual address of mclusters */
472 extern short *mclrefcnt; /* cluster reference counts */
473 extern int *mcl_paddr; /* physical addresses of clusters */
474 extern struct mbstat mbstat; /* statistics */
475 extern int nmbclusters; /* number of mapped clusters */
476 extern union mcluster *mclfree; /* free mapped cluster list */
477 extern int max_linkhdr; /* largest link-level header */
478 extern int max_protohdr; /* largest protocol header */
479 extern int max_hdr; /* largest link+protocol header */
480 extern int max_datalen; /* MHLEN - max_hdr */
481
482 __BEGIN_DECLS
483 struct mbuf *m_copym(struct mbuf *, int, int, int);
484 struct mbuf *m_split(struct mbuf *, int, int);
485 struct mbuf *m_free(struct mbuf *);
486 struct mbuf *m_get(int, int);
487 struct mbuf *m_getpacket(void);
488 struct mbuf *m_getclr(int, int);
489 struct mbuf *m_gethdr(int, int);
490 struct mbuf *m_prepend(struct mbuf *, int, int);
491 struct mbuf *m_prepend_2(struct mbuf *, int, int);
492 struct mbuf *m_pullup(struct mbuf *, int);
493 struct mbuf *m_retry(int, int);
494 struct mbuf *m_retryhdr(int, int);
495 void m_adj(struct mbuf *, int);
496 void m_freem(struct mbuf *);
497 int m_freem_list(struct mbuf *);
498 struct mbuf *m_devget(char *, int, int, struct ifnet *, void (*)(const void *, void *, size_t));
499 char *mcl_to_paddr(char *);
500 struct mbuf *m_pulldown(struct mbuf*, int, int, int*);
501 struct mbuf *m_aux_add(struct mbuf *, int, int);
502 struct mbuf *m_aux_find(struct mbuf *, int, int);
503 void m_aux_delete(struct mbuf *, struct mbuf *);
504
505 struct mbuf *m_mclget(struct mbuf *, int);
506 caddr_t m_mclalloc(int);
507 void m_mclfree(caddr_t p);
508 int m_mclhasreference(struct mbuf *);
509 void m_copy_pkthdr(struct mbuf *, struct mbuf*);
510
511 int m_mclref(struct mbuf *);
512 int m_mclunref(struct mbuf *);
513
514 void * m_mtod(struct mbuf *);
515 struct mbuf * m_dtom(void *);
516 int m_mtocl(void *);
517 union mcluster *m_cltom(int );
518
519 int m_trailingspace(struct mbuf *);
520 int m_leadingspace(struct mbuf *);
521
522 void m_mchtype(struct mbuf *m, int t);
523 void m_mcheck(struct mbuf*);
524
525 void m_copyback(struct mbuf *, int , int , caddr_t);
526 void m_copydata(struct mbuf *, int , int , caddr_t);
527 struct mbuf* m_dup(struct mbuf *m, int how);
528 void m_cat(struct mbuf *, struct mbuf *);
529 struct mbuf *m_copym_with_hdrs(struct mbuf*, int, int, int, struct mbuf**, int*);
530 struct mbuf *m_getpackets(int, int, int);
531 struct mbuf * m_getpackethdrs(int , int );
532 struct mbuf* m_getpacket_how(int );
533 struct mbuf * m_getpackets_internal(unsigned int *, int , int , int , size_t);
534 struct mbuf * m_allocpacket_internal(unsigned int * , size_t , unsigned int *, int , int , size_t );
535
536 __END_DECLS
537
538 /*
539 Packets may have annotations attached by affixing a list of "packet
540 tags" to the pkthdr structure. Packet tags are dynamically allocated
541 semi-opaque data structures that have a fixed header (struct m_tag)
542 that specifies the size of the memory block and an <id,type> pair that
543 identifies it. The id identifies the module and the type identifies the
544 type of data for that module. The id of zero is reserved for the kernel.
545
546 Note that the packet tag returned by m_tag_allocate has the default
547 memory alignment implemented by malloc. To reference private data one
548 can use a construct like:
549
550 struct m_tag *mtag = m_tag_allocate(...);
551 struct foo *p = (struct foo *)(mtag+1);
552
553 if the alignment of struct m_tag is sufficient for referencing members
554 of struct foo. Otherwise it is necessary to embed struct m_tag within
555 the private data structure to insure proper alignment; e.g.
556
557 struct foo {
558 struct m_tag tag;
559 ...
560 };
561 struct foo *p = (struct foo *) m_tag_allocate(...);
562 struct m_tag *mtag = &p->tag;
563 */
564
565 #define KERNEL_MODULE_TAG_ID 0
566
567 enum {
568 KERNEL_TAG_TYPE_NONE = 0,
569 KERNEL_TAG_TYPE_DUMMYNET = 1,
570 KERNEL_TAG_TYPE_DIVERT = 2,
571 KERNEL_TAG_TYPE_IPFORWARD = 3,
572 KERNEL_TAG_TYPE_IPFILT = 4
573 };
574
575 /*
576 * As a temporary and low impact solution to replace the even uglier
577 * approach used so far in some parts of the network stack (which relies
578 * on global variables), packet tag-like annotations are stored in MT_TAG
579 * mbufs (or lookalikes) prepended to the actual mbuf chain.
580 *
581 * m_type = MT_TAG
582 * m_flags = m_tag_id
583 * m_next = next buffer in chain.
584 *
585 * BE VERY CAREFUL not to pass these blocks to the mbuf handling routines.
586 */
587 #define _m_tag_id m_hdr.mh_flags
588
589 __BEGIN_DECLS
590
591 /* Packet tag routines */
592 struct m_tag *m_tag_alloc(u_int32_t id, u_int16_t type, int len, int wait);
593 void m_tag_free(struct m_tag *);
594 void m_tag_prepend(struct mbuf *, struct m_tag *);
595 void m_tag_unlink(struct mbuf *, struct m_tag *);
596 void m_tag_delete(struct mbuf *, struct m_tag *);
597 void m_tag_delete_chain(struct mbuf *, struct m_tag *);
598 struct m_tag *m_tag_locate(struct mbuf *,u_int32_t id, u_int16_t type,
599 struct m_tag *);
600 struct m_tag *m_tag_copy(struct m_tag *, int wait);
601 int m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int wait);
602 void m_tag_init(struct mbuf *);
603 struct m_tag *m_tag_first(struct mbuf *);
604 struct m_tag *m_tag_next(struct mbuf *, struct m_tag *);
605
606 __END_DECLS
607
608 #endif /* KERNEL */
609
610 #endif /* KERNEL_PRIVATE */
611 #ifdef KERNEL
612 #include <sys/kpi_mbuf.h>
613 #endif
614 #endif /* !_SYS_MBUF_H_ */