2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)mbuf.h 8.5 (Berkeley) 2/19/95
34 * $FreeBSD: src/sys/sys/mbuf.h,v 1.44.2.17 2003/04/15 06:15:02 silby Exp $
35 * $DragonFly: src/sys/sys/mbuf.h,v 1.12 2004/06/04 20:29:02 dillon Exp $
41 #include <sys/queue.h>
44 * Mbufs are of a single size, MSIZE (machine/param.h), which
45 * includes overhead. An mbuf may add a single "mbuf cluster" of size
46 * MCLBYTES (also in machine/param.h), which has no additional overhead
47 * and is used instead of the internal data area; this is done when
48 * at least MINCLSIZE of data must be stored.
50 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
51 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
52 #define MINCLSIZE (MHLEN + 1) /* smallest amount to put in cluster */
53 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */
56 * Macros for type conversion:
57 * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type.
58 * mtocl(x) - convert pointer within cluster to cluster index #
59 * cltom(x) - convert cluster # to ptr to beginning of cluster
61 #define mtod(m, t) ((t)((m)->m_data))
62 #define mtocl(x) (((uintptr_t)(x) - (uintptr_t)mbutl) >> MCLSHIFT)
63 #define cltom(x) ((caddr_t)((uintptr_t)mbutl + \
64 ((uintptr_t)(x) << MCLSHIFT)))
67 * Header present at the beginning of every mbuf.
70 struct mbuf *mh_next; /* next buffer in chain */
71 struct mbuf *mh_nextpkt; /* next chain in queue/record */
72 caddr_t mh_data; /* location of data */
73 int mh_len; /* amount of data in this mbuf */
74 short mh_type; /* type of data in this mbuf */
75 short mh_flags; /* flags; see below */
79 * Packet tag structure (see below for details).
82 SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */
83 u_int16_t m_tag_id; /* Tag ID */
84 u_int16_t m_tag_len; /* Length of data */
85 u_int32_t m_tag_cookie; /* ABI/Module ID */
89 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
92 struct ifnet *rcvif; /* rcv interface */
93 int len; /* total packet length */
94 /* variables for ip and tcp reassembly */
95 void *header; /* pointer to packet header */
96 /* variables for hardware checksum */
97 int csum_flags; /* flags regarding checksum */
98 int csum_data; /* data field used by csum routines */
99 SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
103 * Description of external storage mapped into mbuf; valid only if M_EXT is set.
106 caddr_t ext_buf; /* start of buffer */
107 void (*ext_free) /* free routine if not the usual */
109 u_int ext_size; /* size of buffer, for ext_free */
110 void (*ext_ref) /* add a reference to the ext object */
115 * The core of the mbuf object along with some shortcut defines for
116 * practical purposes.
122 struct pkthdr MH_pkthdr; /* M_PKTHDR set */
124 struct m_ext MH_ext; /* M_EXT set */
125 char MH_databuf[MHLEN];
128 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
131 #define m_next m_hdr.mh_next
132 #define m_len m_hdr.mh_len
133 #define m_data m_hdr.mh_data
134 #define m_type m_hdr.mh_type
135 #define m_flags m_hdr.mh_flags
136 #define m_nextpkt m_hdr.mh_nextpkt
137 #define m_pkthdr M_dat.MH.MH_pkthdr
138 #define m_ext M_dat.MH.MH_dat.MH_ext
139 #define m_pktdat M_dat.MH.MH_dat.MH_databuf
140 #define m_dat M_dat.M_databuf
143 * Code that uses m_act should be converted to use m_nextpkt
144 * instead; m_act is historical and deprecated.
146 #define m_act m_nextpkt
151 #define M_EXT 0x0001 /* has associated external storage */
152 #define M_PKTHDR 0x0002 /* start of record */
153 #define M_EOR 0x0004 /* end of record */
154 #define M_PROTO1 0x0008 /* protocol-specific */
155 #define M_PROTO2 0x0010 /* protocol-specific */
156 #define M_PROTO3 0x0020 /* protocol-specific */
157 #define M_PROTO4 0x0040 /* protocol-specific */
158 #define M_PROTO5 0x0080 /* protocol-specific */
161 * mbuf pkthdr flags (also stored in m_flags).
163 #define M_BCAST 0x0100 /* send/received as link-level broadcast */
164 #define M_MCAST 0x0200 /* send/received as link-level multicast */
165 #define M_FRAG 0x0400 /* packet is a fragment of a larger packet */
166 #define M_FIRSTFRAG 0x0800 /* packet is first fragment */
167 #define M_LASTFRAG 0x1000 /* packet is last fragment */
170 * Flags copied when copying m_pkthdr.
172 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO1|M_PROTO2|M_PROTO3 | \
173 M_PROTO4|M_PROTO5|M_BCAST|M_MCAST|M_FRAG | \
174 M_FIRSTFRAG|M_LASTFRAG)
177 * Flags indicating hw checksum support and sw checksum requirements.
179 #define CSUM_IP 0x0001 /* will csum IP */
180 #define CSUM_TCP 0x0002 /* will csum TCP */
181 #define CSUM_UDP 0x0004 /* will csum UDP */
182 #define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */
183 #define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */
185 #define CSUM_IP_CHECKED 0x0100 /* did csum IP */
186 #define CSUM_IP_VALID 0x0200 /* ... the csum is valid */
187 #define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */
188 #define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */
190 #define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP)
191 #define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */
196 #define MT_FREE 0 /* should be on free list */
197 #define MT_DATA 1 /* dynamic (data) allocation */
198 #define MT_HEADER 2 /* packet header */
200 #define MT_SOCKET 3 /* socket structure */
201 #define MT_PCB 4 /* protocol control block */
202 #define MT_RTABLE 5 /* routing tables */
203 #define MT_HTABLE 6 /* IMP host tables */
204 #define MT_ATABLE 7 /* address resolution tables */
206 #define MT_SONAME 8 /* socket name */
208 #define MT_SOOPTS 10 /* socket options */
210 #define MT_FTABLE 11 /* fragment reassembly header */
212 #define MT_RIGHTS 12 /* access rights */
213 #define MT_IFADDR 13 /* interface address */
215 #define MT_TAG 13 /* volatile metadata associated to pkts */
216 #define MT_CONTROL 14 /* extra-data protocol message */
217 #define MT_OOBDATA 15 /* expedited data */
218 #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */
221 * General mbuf allocator statistics structure.
224 u_long m_mbufs; /* mbufs obtained from page pool */
225 u_long m_clusters; /* clusters obtained from page pool */
226 u_long m_spare; /* spare field */
227 u_long m_clfree; /* free clusters */
228 u_long m_drops; /* times failed to find space */
229 u_long m_wait; /* times waited for space */
230 u_long m_drain; /* times drained protocols for space */
231 u_long m_mcfail; /* times m_copym failed */
232 u_long m_mpfail; /* times m_pullup failed */
233 u_long m_msize; /* length of an mbuf */
234 u_long m_mclbytes; /* length of an mbuf cluster */
235 u_long m_minclsize; /* min length of data to allocate a cluster */
236 u_long m_mlen; /* length of data in an mbuf */
237 u_long m_mhlen; /* length of data in a header mbuf */
241 * Flags specifying how an allocation should be made.
244 #define MB_DONTWAIT 0x4
245 #define MB_TRYWAIT 0x8
246 #define MB_WAIT MB_TRYWAIT
249 * Mbuf to Malloc Flag Conversion.
251 #define MBTOM(how) ((how) & MB_TRYWAIT ? M_WAITOK : M_NOWAIT)
255 * Normal mbuf clusters are normally treated as character arrays
256 * after allocation, but use the first word of the buffer as a free list
257 * pointer while on the free list.
260 union mcluster *mcl_next;
261 char mcl_buf[MCLBYTES];
266 * These are identifying numbers passed to the m_mballoc_wait function,
267 * allowing us to determine whether the call came from an MGETHDR or
274 * Wake up the next instance (if any) of m_mballoc_wait() which is
275 * waiting for an mbuf to be freed. This should be called at splimp().
277 * XXX: If there is another free mbuf, this routine will be called [again]
278 * from the m_mballoc_wait routine in order to wake another sleep instance.
280 #define MMBWAKEUP() do { \
281 if (m_mballoc_wid) { \
283 wakeup_one(&m_mballoc_wid); \
288 * Same as above, but for mbuf cluster(s).
290 #define MCLWAKEUP() do { \
291 if (m_clalloc_wid) { \
293 wakeup_one(&m_clalloc_wid); \
298 * mbuf utility macros:
301 * prevents a section of code from from being interrupted by network
304 #define MBUFLOCK(code) do { \
305 int _ms = splimp(); \
312 * mbuf allocation/deallocation macros (YYY deprecated, too big):
314 * MGET(struct mbuf *m, int how, int type)
315 * allocates an mbuf and initializes it to contain internal data.
317 * MGETHDR(struct mbuf *m, int how, int type)
318 * allocates an mbuf and initializes it to contain a packet header
321 #define MGET(m, how, type) do { \
322 (m) = m_get((how), (type)); \
325 #define MGETHDR(m, how, type) do { \
326 (m) = m_gethdr((how), (type)); \
330 * Mbuf cluster macros.
331 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
332 * MCLGET adds such clusters to a normal mbuf;
333 * the flag M_EXT is set upon success.
334 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
335 * freeing the cluster if the reference count has reached 0.
337 #define MCLALLOC(p, how) do { \
338 (p) = m_mclalloc(how); \
341 #define MCLGET(m, how) do { \
342 m_mclget((m), (how)); \
346 * NB: M_COPY_PKTHDR is deprecated; use either M_MOVE_PKTHDR
350 * Move mbuf pkthdr from "from" to "to".
351 * from should have M_PKTHDR set, and to must be empty.
352 * from no longer has a pkthdr after this operation.
354 #define M_MOVE_PKTHDR(_to, _from) m_move_pkthdr((_to), (_from))
357 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
358 * an object of the specified size at the end of the mbuf, longword aligned.
360 #define M_ALIGN(m, len) do { \
361 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \
365 * As above, for mbufs allocated with m_gethdr/MGETHDR
366 * or initialized by M_COPY_PKTHDR.
368 #define MH_ALIGN(m, len) do { \
369 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \
373 * Check if we can write to an mbuf.
375 #define M_EXT_WRITABLE(m) \
376 ((m)->m_ext.ext_free == NULL && mclrefcnt[mtocl((m)->m_ext.ext_buf)] == 1)
378 #define M_WRITABLE(m) (!((m)->m_flags & M_EXT) || \
382 * Compute the amount of space available
383 * before the current start of data in an mbuf.
385 * The M_WRITABLE() is a temporary, conservative safety measure: the burden
386 * of checking writability of the mbuf data area rests solely with the caller.
388 #define M_LEADINGSPACE(m) \
389 ((m)->m_flags & M_EXT ? \
390 (M_EXT_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \
391 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
392 (m)->m_data - (m)->m_dat)
395 * Compute the amount of space available
396 * after the end of data in an mbuf.
398 * The M_WRITABLE() is a temporary, conservative safety measure: the burden
399 * of checking writability of the mbuf data area rests solely with the caller.
401 #define M_TRAILINGSPACE(m) \
402 ((m)->m_flags & M_EXT ? \
403 (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \
404 - ((m)->m_data + (m)->m_len) : 0) : \
405 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
408 * Arrange to prepend space of size plen to mbuf m.
409 * If a new mbuf must be allocated, how specifies whether to wait.
410 * If how is MB_DONTWAIT and allocation fails, the original mbuf chain
411 * is freed and m is set to NULL.
413 #define M_PREPEND(m, plen, how) do { \
414 struct mbuf **_mmp = &(m); \
415 struct mbuf *_mm = *_mmp; \
416 int _mplen = (plen); \
417 int __mhow = (how); \
419 if (M_LEADINGSPACE(_mm) >= _mplen) { \
420 _mm->m_data -= _mplen; \
421 _mm->m_len += _mplen; \
423 _mm = m_prepend(_mm, _mplen, __mhow); \
424 if (_mm != NULL && _mm->m_flags & M_PKTHDR) \
425 _mm->m_pkthdr.len += _mplen; \
429 /* change mbuf to new type */
430 #define MCHTYPE(m, t) do { \
431 struct mbuf *_mm = (m); \
433 int _ms = splimp(); \
435 mbtypes[_mm->m_type]--; \
438 _mm->m_type = (_mt); \
441 /* Length to m_copy to copy all. */
442 #define M_COPYALL 1000000000
444 /* Compatibility with 4.3 */
445 #define m_copy(m, o, l) m_copym((m), (o), (l), MB_DONTWAIT)
448 extern u_int m_clalloc_wid; /* mbuf cluster wait count */
449 extern u_int m_mballoc_wid; /* mbuf wait count */
450 extern int max_linkhdr; /* largest link-level header */
451 extern int max_protohdr; /* largest protocol header */
452 extern int max_hdr; /* largest link+protocol header */
453 extern int max_datalen; /* MHLEN - max_hdr */
454 extern struct mbstat mbstat;
455 extern u_long mbtypes[MT_NTYPES]; /* per-type mbuf allocations */
456 extern int mbuf_wait; /* mbuf sleep time */
457 extern struct mbuf *mbutl; /* virtual address of mclusters */
458 extern struct mbuf *mbute; /* ending VA of mclusters */
459 extern char *mclrefcnt; /* cluster reference counts */
460 extern union mcluster *mclfree;
461 extern struct mbuf *mmbfree;
462 extern int nmbclusters;
467 void m_adj(struct mbuf *, int);
468 void m_cat(struct mbuf *, struct mbuf *);
469 int m_clalloc(int, int);
470 caddr_t m_clalloc_wait(void);
471 void m_copyback(struct mbuf *, int, int, caddr_t);
472 void m_copydata(const struct mbuf *, int, int, caddr_t);
473 struct mbuf *m_copym(const struct mbuf *, int, int, int);
474 struct mbuf *m_copypacket(struct mbuf *, int);
475 struct mbuf *m_defrag(struct mbuf *, int);
476 struct mbuf *m_devget(char *, int, int, struct ifnet *,
477 void (*copy)(char *, caddr_t, u_int));
478 struct mbuf *m_dup(struct mbuf *, int);
479 int m_dup_pkthdr(struct mbuf *, const struct mbuf *, int);
480 struct mbuf *m_free(struct mbuf *);
481 void m_freem(struct mbuf *);
482 struct mbuf *m_get(int, int);
483 struct mbuf *m_getcl(int how, short type, int flags);
484 struct mbuf *m_getclr(int, int);
485 struct mbuf *m_gethdr(int, int);
486 struct mbuf *m_getm(struct mbuf *, int, int, int);
487 int m_mballoc(int, int);
488 struct mbuf *m_mballoc_wait(int, int);
489 void m_move_pkthdr(struct mbuf *, struct mbuf *);
490 struct mbuf *m_prepend(struct mbuf *, int, int);
491 void m_print(const struct mbuf *m);
492 struct mbuf *m_pulldown(struct mbuf *, int, int, int *);
493 struct mbuf *m_pullup(struct mbuf *, int);
494 struct mbuf *m_retry(int, int);
495 struct mbuf *m_retryhdr(int, int);
496 struct mbuf *m_split(struct mbuf *, int, int);
497 struct mbuf *m_uiomove(struct uio *, int, int);
498 caddr_t m_mclalloc(int how);
499 void m_mclget(struct mbuf *m, int how);
500 void m_mclfree(caddr_t data);
503 * Packets may have annotations attached by affixing a list
504 * of "packet tags" to the pkthdr structure. Packet tags are
505 * dynamically allocated semi-opaque data structures that have
506 * a fixed header (struct m_tag) that specifies the size of the
507 * memory block and a <cookie,type> pair that identifies it.
508 * The cookie is a 32-bit unique unsigned value used to identify
509 * a module or ABI. By convention this value is chose as the
510 * date+time that the module is created, expressed as the number of
511 * seconds since the epoch (e.g. using date -u +'%s'). The type value
512 * is an ABI/module-specific value that identifies a particular annotation
513 * and is private to the module. For compatibility with systems
514 * like openbsd that define packet tags w/o an ABI/module cookie,
515 * the value PACKET_ABI_COMPAT is used to implement m_tag_get and
516 * m_tag_find compatibility shim functions and several tag types are
517 * defined below. Users that do not require compatibility should use
518 * a private cookie value so that packet tag-related definitions
519 * can be maintained privately.
521 * Note that the packet tag returned by m_tag_allocate has the default
522 * memory alignment implemented by malloc. To reference private data
523 * one can use a construct like:
525 * struct m_tag *mtag = m_tag_allocate(...);
526 * struct foo *p = (struct foo *)(mtag+1);
528 * if the alignment of struct m_tag is sufficient for referencing members
529 * of struct foo. Otherwise it is necessary to embed struct m_tag within
530 * the private data structure to insure proper alignment; e.g.
536 * struct foo *p = (struct foo *) m_tag_allocate(...);
537 * struct m_tag *mtag = &p->tag;
540 #define PACKET_TAG_NONE 0 /* Nadda */
542 /* Packet tag for use with PACKET_ABI_COMPAT */
543 #define PACKET_TAG_IPSEC_IN_DONE 1 /* IPsec applied, in */
544 #define PACKET_TAG_IPSEC_OUT_DONE 2 /* IPsec applied, out */
545 #define PACKET_TAG_IPSEC_IN_CRYPTO_DONE 3 /* NIC IPsec crypto done */
546 #define PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED 4 /* NIC IPsec crypto req'ed */
547 #define PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO 5 /* NIC notifies IPsec */
548 #define PACKET_TAG_IPSEC_PENDING_TDB 6 /* Reminder to do IPsec */
549 #define PACKET_TAG_BRIDGE 7 /* Bridge processing done */
550 #define PACKET_TAG_GIF 8 /* GIF processing done */
551 #define PACKET_TAG_GRE 9 /* GRE processing done */
552 #define PACKET_TAG_IN_PACKET_CHECKSUM 10 /* NIC checksumming done */
553 #define PACKET_TAG_ENCAP 11 /* Encap. processing */
554 #define PACKET_TAG_IPSEC_SOCKET 12 /* IPSEC socket ref */
555 #define PACKET_TAG_IPSEC_HISTORY 13 /* IPSEC history */
556 #define PACKET_TAG_IPV6_INPUT 14 /* IPV6 input processing */
559 * As a temporary and low impact solution to replace the even uglier
560 * approach used so far in some parts of the network stack (which relies
561 * on global variables), packet tag-like annotations are stored in MT_TAG
562 * mbufs (or lookalikes) prepended to the actual mbuf chain.
566 * m_next = next buffer in chain.
568 * BE VERY CAREFUL not to pass these blocks to the mbuf handling routines.
570 #define _m_tag_id m_hdr.mh_flags
572 /* Packet tags used in the FreeBSD network stack */
573 #define PACKET_TAG_DUMMYNET 15 /* dummynet info */
574 #define PACKET_TAG_IPFW 16 /* ipfw classification */
575 #define PACKET_TAG_DIVERT 17 /* divert info */
576 #define PACKET_TAG_IPFORWARD 18 /* ipforward info */
578 /* Packet tag routines */
579 struct m_tag *m_tag_alloc(u_int32_t, int, int, int);
580 void m_tag_free(struct m_tag *);
581 void m_tag_prepend(struct mbuf *, struct m_tag *);
582 void m_tag_unlink(struct mbuf *, struct m_tag *);
583 void m_tag_delete(struct mbuf *, struct m_tag *);
584 void m_tag_delete_chain(struct mbuf *, struct m_tag *);
585 struct m_tag *m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *);
586 struct m_tag *m_tag_copy(struct m_tag *, int);
587 int m_tag_copy_chain(struct mbuf *, const struct mbuf *, int);
588 void m_tag_init(struct mbuf *);
589 struct m_tag *m_tag_first(struct mbuf *);
590 struct m_tag *m_tag_next(struct mbuf *, struct m_tag *);
592 /* these are for openbsd compatibility */
593 #define MTAG_ABI_COMPAT 0 /* compatibility ABI */
595 static __inline struct m_tag *
596 m_tag_get(int type, int length, int wait)
598 return m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait);
601 static __inline struct m_tag *
602 m_tag_find(struct mbuf *m, int type, struct m_tag *start)
604 return m_tag_locate(m, MTAG_ABI_COMPAT, type, start);
608 #endif /* !_SYS_MBUF_H_ */