1 //==========================================================================
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * Copyright (c) 1982, 1986, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)mbuf.h 8.5 (Berkeley) 2/19/95
55 * $FreeBSD: src/sys/sys/mbuf.h,v 1.44.2.10 2001/07/03 11:02:01 ume Exp $
62 * Mbufs are of a single size, MSIZE (machine/param.h), which
63 * includes overhead. An mbuf may add a single "mbuf cluster" of size
64 * MCLBYTES (also in machine/param.h), which has no additional overhead
65 * and is used instead of the internal data area; this is done when
66 * at least MINCLSIZE of data must be stored.
69 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
70 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
72 #define MINCLSIZE (MHLEN + 1) /* smallest amount to put in cluster */
73 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */
76 * Macros for type conversion
77 * mtod(m, t) - convert mbuf pointer to data pointer of correct type
78 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX)
79 * mtocl(x) - convert pointer within cluster to cluster index #
80 * cltom(x) - convert cluster # to ptr to beginning of cluster
82 #define mtod(m, t) ((t)((m)->m_data))
83 #define dtom(x) ((struct mbuf *)((CYG_ADDRESS)(x) & ~(MSIZE-1)))
84 extern int cyg_mtocl(u_long);
85 extern struct mbuf *cyg_cltom(u_long);
86 #define mtocl(x) cyg_mtocl((u_long)x)
87 #define cltom(x) cyg_cltom((u_long)x)
89 /* header at beginning of each mbuf: */
91 struct mbuf *mh_next; /* next buffer in chain */
92 struct mbuf *mh_nextpkt; /* next chain in queue/record */
93 caddr_t mh_data; /* location of data */
94 int mh_len; /* amount of data in this mbuf */
95 short mh_type; /* type of data in this mbuf */
96 short mh_flags; /* flags; see below */
99 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
101 struct ifnet *rcvif; /* rcv interface */
102 int len; /* total packet length */
103 /* variables for ip and tcp reassembly */
104 void *header; /* pointer to packet header */
105 /* variables for hardware checksum */
106 int csum_flags; /* flags regarding checksum */
107 int csum_data; /* data field used by csum routines */
108 struct mbuf *aux; /* extra data buffer; ipsec/others */
111 /* description of external storage mapped into mbuf, valid if M_EXT set */
113 caddr_t ext_buf; /* start of buffer */
114 void (*ext_free) /* free routine if not the usual */
115 __P((caddr_t, u_int));
116 u_int ext_size; /* size of buffer, for ext_free */
117 void (*ext_ref) /* add a reference to the ext object */
118 __P((caddr_t, u_int));
125 struct pkthdr MH_pkthdr; /* M_PKTHDR set */
127 struct m_ext MH_ext; /* M_EXT set */
128 char MH_databuf[MHLEN];
131 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
134 #define m_next m_hdr.mh_next
135 #define m_len m_hdr.mh_len
136 #define m_data m_hdr.mh_data
137 #define m_type m_hdr.mh_type
138 #define m_flags m_hdr.mh_flags
139 #define m_nextpkt m_hdr.mh_nextpkt
140 #define m_act m_nextpkt
141 #define m_pkthdr M_dat.MH.MH_pkthdr
142 #define m_ext M_dat.MH.MH_dat.MH_ext
143 #define m_pktdat M_dat.MH.MH_dat.MH_databuf
144 #define m_dat M_dat.M_databuf
147 #define M_EXT 0x0001 /* has associated external storage */
148 #define M_PKTHDR 0x0002 /* start of record */
149 #define M_EOR 0x0004 /* end of record */
150 #define M_PROTO1 0x0008 /* protocol-specific */
151 #define M_PROTO2 0x0010 /* protocol-specific */
152 #define M_PROTO3 0x0020 /* protocol-specific */
153 #define M_PROTO4 0x0040 /* protocol-specific */
154 #define M_PROTO5 0x0080 /* protocol-specific */
155 #define M_PROTO6 0x2000 /* protocol-specific */
157 /* mbuf pkthdr flags, also in m_flags */
158 #define M_BCAST 0x0100 /* send/received as link-level broadcast */
159 #define M_MCAST 0x0200 /* send/received as link-level multicast */
160 #define M_FRAG 0x0400 /* packet is a fragment of a larger packet */
161 #define M_FIRSTFRAG 0x0800 /* packet is first fragment */
162 #define M_LASTFRAG 0x1000 /* packet is last fragment */
164 #define M_AUX 0x4000 /* mbufs pointed to by m->m_pkthdr.aux */
166 /* flags copied when copying m_pkthdr */
167 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO1|M_PROTO2|M_PROTO3| \
168 M_PROTO4|M_PROTO5|M_PROTO6|M_BCAST|M_MCAST|M_FRAG| \
171 /* flags indicating hw checksum support and sw checksum requirements */
172 #define CSUM_IP 0x0001 /* will csum IP */
173 #define CSUM_TCP 0x0002 /* will csum TCP */
174 #define CSUM_UDP 0x0004 /* will csum UDP */
175 #define CSUM_IP_FRAGS 0x0008 /* will csum IP fragments */
176 #define CSUM_FRAGMENT 0x0010 /* will do IP fragmentation */
178 #define CSUM_IP_CHECKED 0x0100 /* did csum IP */
179 #define CSUM_IP_VALID 0x0200 /* ... the csum is valid */
180 #define CSUM_DATA_VALID 0x0400 /* csum_data field is valid */
181 #define CSUM_PSEUDO_HDR 0x0800 /* csum_data has pseudo hdr */
183 #define CSUM_DELAY_DATA (CSUM_TCP | CSUM_UDP)
184 #define CSUM_DELAY_IP (CSUM_IP) /* XXX add ipv6 here too? */
187 #define MT_FREE 0 /* should be on free list */
188 #define MT_DATA 1 /* dynamic (data) allocation */
189 #define MT_HEADER 2 /* packet header */
191 #define MT_SOCKET 3 /* socket structure */
192 #define MT_PCB 4 /* protocol control block */
193 #define MT_RTABLE 5 /* routing tables */
194 #define MT_HTABLE 6 /* IMP host tables */
195 #define MT_ATABLE 7 /* address resolution tables */
197 #define MT_SONAME 8 /* socket name */
199 #define MT_SOOPTS 10 /* socket options */
201 #define MT_FTABLE 11 /* fragment reassembly header */
203 #define MT_RIGHTS 12 /* access rights */
204 #define MT_IFADDR 13 /* interface address */
206 #define MT_CONTROL 14 /* extra-data protocol message */
207 #define MT_OOBDATA 15 /* expedited data */
209 #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */
215 u_long m_mbufs; /* mbufs obtained from page pool */
216 u_long m_clusters; /* clusters obtained from page pool */
217 u_long m_spare; /* spare field */
218 u_long m_clfree; /* free clusters */
219 u_long m_drops; /* times failed to find space */
220 u_long m_wait; /* times waited for space */
221 u_long m_drain; /* times drained protocols for space */
222 u_long m_mcfail; /* times m_copym failed */
223 u_long m_mpfail; /* times m_pullup failed */
224 u_long m_msize; /* length of an mbuf */
225 u_long m_mclbytes; /* length of an mbuf cluster */
226 u_long m_minclsize; /* min length of data to allocate a cluster */
227 u_long m_mlen; /* length of data in an mbuf */
228 u_long m_mhlen; /* length of data in a header mbuf */
230 u_quad_t m_exthdrget; /* # of calls to IP6_EXTHDR_GET */
231 u_quad_t m_exthdrget0; /* # of calls to IP6_EXTHDR_GET0 */
232 u_quad_t m_pulldowns; /* # of calls to m_pulldown */
233 u_quad_t m_pulldown_copy; /* # of mbuf copies in m_pulldown */
234 u_quad_t m_pulldown_alloc; /* # of mbuf allocs in m_pulldown */
235 u_quad_t m_pullups; /* # of calls to m_pullup */
236 u_quad_t m_pullup_copy; /* # of possible m_pullup copies */
237 u_quad_t m_pullup_alloc; /* # of possible m_pullup mallocs */
238 u_quad_t m_pullup_fail; /* # of possible m_pullup failures */
239 u_quad_t m_pullup2; /* # of calls to m_pullup2 */
240 u_quad_t m_pullup2_copy; /* # of possible m_pullup2 copies */
241 u_quad_t m_pullup2_alloc; /* # of possible m_pullup2 mallocs */
242 u_quad_t m_pullup2_fail; /* # of possible m_pullup2 failures */
245 /* flags to m_get/MGET */
251 * Normal mbuf clusters are normally treated as character arrays
252 * after allocation, but use the first word of the buffer as a free list
253 * pointer while on the free list.
256 union mcluster *mcl_next;
257 char mcl_buf[MCLBYTES];
262 * These are identifying numbers passed to the m_mballoc_wait function,
263 * allowing us to determine whether the call came from an MGETHDR or
270 * Wake up the next instance (if any) of m_mballoc_wait() which is
271 * waiting for an mbuf to be freed. This should be called at splimp().
273 * XXX: If there is another free mbuf, this routine will be called [again]
274 * from the m_mballoc_wait routine in order to wake another sleep instance.
276 #define MMBWAKEUP() do { \
277 if (m_mballoc_wid) { \
279 wakeup_one(&m_mballoc_wid); \
284 * Same as above, but for mbuf cluster(s).
286 #define MCLWAKEUP() do { \
287 if (m_clalloc_wid) { \
289 wakeup_one(&m_clalloc_wid); \
294 * mbuf utility macros:
297 * prevents a section of code from from being interrupted by network
300 #define MBUFLOCK(code) do { \
301 int _ms = splimp(); \
308 * mbuf allocation/deallocation macros:
310 * MGET(struct mbuf *m, int how, int type)
311 * allocates an mbuf and initializes it to contain internal data.
313 * MGETHDR(struct mbuf *m, int how, int type)
314 * allocates an mbuf and initializes it to contain a packet header
317 #define MGET(m, how, type) do { \
320 int _mtype = (type); \
321 int _ms = splimp(); \
323 if (mmbfree == NULL) \
324 (void)m_mballoc(1, _mhow); \
327 mmbfree = _mm->m_next; \
328 mbtypes[MT_FREE]--; \
329 _mm->m_type = _mtype; \
331 _mm->m_next = NULL; \
332 _mm->m_nextpkt = NULL; \
333 _mm->m_data = _mm->m_dat; \
339 _mm = m_retry(_mhow, _mtype); \
340 if (_mm == NULL && _mhow == M_WAIT) \
341 (m) = m_mballoc_wait(MGET_C, _mtype); \
347 #define MGETHDR(m, how, type) do { \
350 int _mtype = (type); \
351 int _ms = splimp(); \
353 if (mmbfree == NULL) \
354 (void)m_mballoc(1, _mhow); \
357 mmbfree = _mm->m_next; \
358 mbtypes[MT_FREE]--; \
359 _mm->m_type = _mtype; \
361 _mm->m_next = NULL; \
362 _mm->m_nextpkt = NULL; \
363 _mm->m_data = _mm->m_pktdat; \
364 _mm->m_flags = M_PKTHDR; \
365 bzero(&(_mm)->m_pkthdr, sizeof((_mm)->m_pkthdr)); \
370 _mm = m_retryhdr(_mhow, _mtype); \
371 if (_mm == NULL && _mhow == M_WAIT) \
372 (m) = m_mballoc_wait(MGETHDR_C, _mtype); \
379 * Mbuf cluster macros.
380 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
381 * MCLGET adds such clusters to a normal mbuf;
382 * the flag M_EXT is set upon success.
383 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
384 * freeing the cluster if the reference count has reached 0.
386 #define MCLALLOC(p, how) do { \
389 int _ms = splimp(); \
391 if (mclfree == NULL) \
392 (void)m_clalloc(1, _mhow); \
393 _mp = (caddr_t)mclfree; \
395 mclrefcnt[mtocl(_mp)]++; \
397 mclfree = ((union mcluster *)_mp)->mcl_next; \
402 if (_mhow == M_WAIT) \
403 (p) = m_clalloc_wait(); \
409 #define MCLGET(m, how) do { \
410 struct mbuf *_mm = (m); \
412 MCLALLOC(_mm->m_ext.ext_buf, (how)); \
413 if (_mm->m_ext.ext_buf != NULL) { \
414 _mm->m_data = _mm->m_ext.ext_buf; \
415 _mm->m_flags |= M_EXT; \
416 _mm->m_ext.ext_free = NULL; \
417 _mm->m_ext.ext_ref = NULL; \
418 _mm->m_ext.ext_size = MCLBYTES; \
422 #define MCLFREE1(p) do { \
423 union mcluster *_mp = (union mcluster *)(p); \
425 /* KASSERT(mclrefcnt[mtocl(_mp)] > 0, ("freeing free cluster")); */ \
426 if (--mclrefcnt[mtocl(_mp)] == 0) { \
427 _mp->mcl_next = mclfree; \
434 #define MCLFREE(p) MBUFLOCK( \
438 #define MEXTFREE1(m) do { \
439 struct mbuf *_mm = (m); \
441 if (_mm->m_ext.ext_free != NULL) \
442 (*_mm->m_ext.ext_free)(_mm->m_ext.ext_buf, \
443 _mm->m_ext.ext_size); \
445 MCLFREE1(_mm->m_ext.ext_buf); \
448 #define MEXTFREE(m) MBUFLOCK( \
453 * MFREE(struct mbuf *m, struct mbuf *n)
454 * Free a single mbuf and associated external storage.
455 * Place the successor, if any, in n.
457 * we do need to check non-first mbuf for m_aux, since some of existing
458 * code does not call M_PREPEND properly.
459 * (example: call to bpf_mtap from drivers)
461 #define MFREE(m, n) MBUFLOCK( \
462 struct mbuf *_mm = (m); \
464 /* KASSERT(_mm->m_type != MT_FREE, ("freeing free mbuf")); */ \
465 mbtypes[_mm->m_type]--; \
466 if ((_mm->m_flags & M_PKTHDR) != 0 && _mm->m_pkthdr.aux) { \
467 m_freem(_mm->m_pkthdr.aux); \
468 _mm->m_pkthdr.aux = NULL; \
470 if (_mm->m_flags & M_EXT) \
473 _mm->m_type = MT_FREE; \
474 mbtypes[MT_FREE]++; \
475 _mm->m_next = mmbfree; \
481 * Copy mbuf pkthdr from "from" to "to".
482 * from must have M_PKTHDR set, and to must be empty.
483 * aux pointer will be moved to `to'.
485 #define M_COPY_PKTHDR(to, from) do { \
486 struct mbuf *_mfrom = (from); \
487 struct mbuf *_mto = (to); \
489 _mto->m_data = _mto->m_pktdat; \
490 _mto->m_flags = _mfrom->m_flags & M_COPYFLAGS; \
491 _mto->m_pkthdr = _mfrom->m_pkthdr; \
492 _mfrom->m_pkthdr.aux = (struct mbuf *)NULL; \
496 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
497 * an object of the specified size at the end of the mbuf, longword aligned.
499 #define M_ALIGN(m, len) do { \
500 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \
504 * As above, for mbufs allocated with m_gethdr/MGETHDR
505 * or initialized by M_COPY_PKTHDR.
507 #define MH_ALIGN(m, len) do { \
508 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \
512 * Check if we can write to an mbuf.
514 #define M_WRITABLE(m) (!((m)->m_flags & M_EXT) || \
515 ((m)->m_ext.ext_free == NULL && mclrefcnt[mtocl((m)->m_ext.ext_buf)] == 1))
518 * Compute the amount of space available
519 * before the current start of data in an mbuf.
521 #define M_LEADINGSPACE(m) \
522 ((m)->m_flags & M_EXT ? \
523 /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \
524 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
525 (m)->m_data - (m)->m_dat)
528 * Compute the amount of space available
529 * after the end of data in an mbuf.
531 #define M_TRAILINGSPACE(m) \
532 ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + \
533 (m)->m_ext.ext_size - ((m)->m_data + (m)->m_len) : \
534 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
537 * Arrange to prepend space of size plen to mbuf m.
538 * If a new mbuf must be allocated, how specifies whether to wait.
539 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
540 * is freed and m is set to NULL.
542 #define M_PREPEND(m, plen, how) do { \
543 struct mbuf **_mmp = &(m); \
544 struct mbuf *_mm = *_mmp; \
545 int _mplen = (plen); \
546 int __mhow = (how); \
548 if (M_LEADINGSPACE(_mm) >= _mplen) { \
549 _mm->m_data -= _mplen; \
550 _mm->m_len += _mplen; \
552 _mm = m_prepend(_mm, _mplen, __mhow); \
553 if (_mm != NULL && _mm->m_flags & M_PKTHDR) \
554 _mm->m_pkthdr.len += _mplen; \
558 /* change mbuf to new type */
559 #define MCHTYPE(m, t) do { \
560 struct mbuf *_mm = (m); \
562 int _ms = splimp(); \
564 mbtypes[_mm->m_type]--; \
567 _mm->m_type = (_mt); \
570 /* length to m_copy to copy all */
571 #define M_COPYALL 1000000000
573 /* compatibility with 4.3 */
574 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
577 * pkthdr.aux type tags.
586 extern u_int m_clalloc_wid; /* mbuf cluster wait count */
587 extern u_int m_mballoc_wid; /* mbuf wait count */
588 extern int max_linkhdr; /* largest link-level header */
589 extern int max_protohdr; /* largest protocol header */
590 extern int max_hdr; /* largest link+protocol header */
591 extern int max_datalen; /* MHLEN - max_hdr */
592 extern struct mbstat mbstat;
593 extern u_long mbtypes[MT_NTYPES]; /* per-type mbuf allocations */
594 extern int mbuf_wait; /* mbuf sleep time */
595 extern struct mbuf *mbutl; /* virtual address of mclusters */
596 extern char *mclrefcnt; /* cluster reference counts */
597 extern union mcluster *mclfree;
598 extern struct mbuf *mmbfree;
599 extern int nmbclusters;
603 void m_adj __P((struct mbuf *, int));
604 void m_cat __P((struct mbuf *,struct mbuf *));
605 int m_clalloc __P((int, int));
606 caddr_t m_clalloc_wait __P((void));
607 void m_copyback __P((struct mbuf *, int, int, caddr_t));
608 void m_copydata __P((struct mbuf *,int,int,caddr_t));
609 struct mbuf *m_copym __P((struct mbuf *, int, int, int));
610 struct mbuf *m_copypacket __P((struct mbuf *, int));
611 struct mbuf *m_devget __P((char *, int, int, struct ifnet *,
612 void (*copy)(char *, caddr_t, u_int)));
613 struct mbuf *m_dup __P((struct mbuf *, int));
614 struct mbuf *m_free __P((struct mbuf *));
615 void m_freem __P((struct mbuf *));
616 struct mbuf *m_get __P((int, int));
617 struct mbuf *m_getclr __P((int, int));
618 struct mbuf *m_gethdr __P((int, int));
619 struct mbuf *m_getm __P((struct mbuf *, int, int, int));
620 int m_mballoc __P((int, int));
621 struct mbuf *m_mballoc_wait __P((int, int));
622 struct mbuf *m_prepend __P((struct mbuf *,int,int));
623 struct mbuf *m_pulldown __P((struct mbuf *, int, int, int *));
624 void m_print __P((const struct mbuf *m));
625 struct mbuf *m_pullup __P((struct mbuf *, int));
626 struct mbuf *m_retry __P((int, int));
627 struct mbuf *m_retryhdr __P((int, int));
628 struct mbuf *m_split __P((struct mbuf *,int,int));
629 struct mbuf *m_aux_add2 __P((struct mbuf *, int, int, void *));
630 struct mbuf *m_aux_find2 __P((struct mbuf *, int, int, void *));
631 struct mbuf *m_aux_add __P((struct mbuf *, int, int));
632 struct mbuf *m_aux_find __P((struct mbuf *, int, int));
633 void m_aux_delete __P((struct mbuf *, struct mbuf *));
634 extern void *cyg_net_mbuf_alloc(void);
635 extern void *cyg_net_cluster_alloc(void );
636 #ifdef CYGDBG_NET_SHOW_MBUFS
637 extern void cyg_net_show_mbufs(void);
641 #endif /* !_SYS_MBUF_H_ */