2 * Copyright (c) 1997-1998 University of Utah and the Flux Group.
5 * This file is part of the Flux OSKit. The OSKit is free software, also known
6 * as "open source;" you can redistribute it and/or modify it under the terms
7 * of the GNU General Public License (GPL), version 2, as published by the Free
8 * Software Foundation (FSF). To explore alternate licensing terms, contact
9 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
11 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
14 * received a copy of the GPL along with the OSKit; see the file COPYING. If
15 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
18 * Copyright (c) 1982, 1986, 1988, 1993
19 * The Regents of the University of California. All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. All advertising materials mentioning features or use of this software
30 * must display the following acknowledgement:
31 * This product includes software developed by the University of
32 * California, Berkeley and its contributors.
33 * 4. Neither the name of the University nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * @(#)mbuf.h 8.3 (Berkeley) 1/21/94
50 * $\Id: mbuf.h,v 1.9 1994/11/14 13:54:20 bde Exp $
57 #include <sys/malloc.h>
63 #define LOCAL_OSKIT_DEFINED
68 * Mbufs are of a single size, MSIZE (machine/machparam.h), which
69 * includes overhead. An mbuf may add a single "mbuf cluster" of size
70 * MCLBYTES (also in machine/machparam.h), which has no additional overhead
71 * and is used instead of the internal data area; this is done when
72 * at least MINCLSIZE of data must be stored.
75 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
76 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
78 #define MINCLSIZE (MHLEN + MLEN) /* smallest amount to put in cluster */
79 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */
82 * Macros for type conversion
83 * mtod(m,t) - convert mbuf pointer to data pointer of correct type
84 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX)
85 * mtocl(x) - convert pointer within cluster to cluster index #
86 * cltom(x) - convert cluster # to ptr to beginning of cluster
88 #define mtod(m,t) ((t)((m)->m_data))
90 #define dtom(x) ((struct mbuf *)((int)(x) & ~(MSIZE-1)))
92 #define dtom(x) ((struct mbuf *)((int)(x) - sizeof(struct m_hdr)))
95 #define mtocl(x) (((u_int)(x) - (u_int)mbutl) >> MCLSHIFT)
96 #define cltom(x) ((caddr_t)((u_int)mbutl + ((u_int)(x) << MCLSHIFT)))
99 /* header at beginning of each mbuf: */
101 struct mbuf
*mh_next
; /* next buffer in chain */
102 struct mbuf
*mh_nextpkt
; /* next chain in queue/record */
103 int mh_len
; /* amount of data in this mbuf */
104 caddr_t mh_data
; /* location of data */
105 short mh_type
; /* type of data in this mbuf */
106 short mh_flags
; /* flags; see below */
109 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
111 int len
; /* total packet length */
112 struct ifnet
*rcvif
; /* rcv interface */
115 /* description of external storage mapped into mbuf, valid if M_EXT set */
117 caddr_t ext_buf
; /* start of buffer */
119 struct oskit_bufio
*ext_bufio
; /* OS Kit bufio pointer */
121 void (*ext_free
) /* free routine if not the usual */
122 __P((caddr_t
, u_int
));
124 u_int ext_size
; /* size of buffer, for ext_free */
131 struct pkthdr MH_pkthdr
; /* M_PKTHDR set */
133 struct m_ext MH_ext
; /* M_EXT set */
134 char MH_databuf
[MHLEN
];
137 char M_databuf
[MLEN
]; /* !M_PKTHDR, !M_EXT */
140 #define m_next m_hdr.mh_next
141 #define m_len m_hdr.mh_len
142 #define m_data m_hdr.mh_data
143 #define m_type m_hdr.mh_type
144 #define m_flags m_hdr.mh_flags
145 #define m_nextpkt m_hdr.mh_nextpkt
146 #define m_act m_nextpkt
147 #define m_pkthdr M_dat.MH.MH_pkthdr
148 #define m_ext M_dat.MH.MH_dat.MH_ext
149 #define m_pktdat M_dat.MH.MH_dat.MH_databuf
150 #define m_dat M_dat.M_databuf
153 #if defined(OSKIT) && !defined(__REACTOS__)
154 #include <oskit/io/bufio.h>
156 * A small step for mankind, but a huge leap for BSD:
157 * We consistently use oskit_bufios for external mbufs
160 #define M_EXT 0x0001 /* has associated external storage */
161 #define M_PKTHDR 0x0002 /* start of record */
162 #define M_EOR 0x0004 /* end of record */
164 /* mbuf pkthdr flags, also in m_flags */
165 #define M_BCAST 0x0100 /* send/received as link-level broadcast */
166 #define M_MCAST 0x0200 /* send/received as link-level multicast */
168 /* flags copied when copying m_pkthdr */
169 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_BCAST|M_MCAST)
172 #define MT_FREE 0 /* should be on free list */
173 #define MT_DATA 1 /* dynamic (data) allocation */
174 #define MT_HEADER 2 /* packet header */
175 #define MT_SOCKET 3 /* socket structure */
176 #define MT_PCB 4 /* protocol control block */
177 #define MT_RTABLE 5 /* routing tables */
178 #define MT_HTABLE 6 /* IMP host tables */
179 #define MT_ATABLE 7 /* address resolution tables */
180 #define MT_SONAME 8 /* socket name */
181 #define MT_SOOPTS 10 /* socket options */
182 #define MT_FTABLE 11 /* fragment reassembly header */
183 #define MT_RIGHTS 12 /* access rights */
184 #define MT_IFADDR 13 /* interface address */
185 #define MT_CONTROL 14 /* extra-data protocol message */
186 #define MT_OOBDATA 15 /* expedited data */
188 /* flags to m_get/MGET */
189 #define M_DONTWAIT M_NOWAIT
190 #define M_WAIT M_WAITOK
193 * mbuf utility macros:
196 * prevents a section of code from from being interrupted by network
199 #define MBUFLOCK(code) \
200 { int ms = splimp(); \
206 * mbuf allocation/deallocation macros:
208 * MGET(struct mbuf *m, int how, int type)
209 * allocates an mbuf and initializes it to contain internal data.
211 * MGETHDR(struct mbuf *m, int how, int type)
212 * allocates an mbuf and initializes it to contain a packet header
215 #define MGET(m, how, type) { \
216 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
217 OS_DbgPrint(OSK_MID_TRACE,("(MGET) got mbuf @ %x\n", m)); \
219 (m)->m_type = (type); \
220 MBUFLOCK(mbstat.m_mtypes[type]++;) \
221 (m)->m_next = (struct mbuf *)NULL; \
222 (m)->m_nextpkt = (struct mbuf *)NULL; \
223 (m)->m_data = (m)->m_dat; \
226 (m) = m_retry((how), (type)); \
229 #define MGETHDR(m, how, type) { \
230 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
231 OS_DbgPrint(OSK_MID_TRACE,("(MGETHDR) got mbuf @ %x\n", m)); \
233 (m)->m_type = (type); \
234 MBUFLOCK(mbstat.m_mtypes[type]++;) \
235 (m)->m_next = (struct mbuf *)NULL; \
236 (m)->m_nextpkt = (struct mbuf *)NULL; \
237 (m)->m_data = (m)->m_pktdat; \
238 (m)->m_flags = M_PKTHDR; \
240 (m) = m_retryhdr((how), (type)); \
244 #define MGET_DONT_RECURSE(m, how, type) { \
245 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
246 OS_DbgPrint(OSK_MID_TRACE,("(MGET_DONT_RECURSE) got mbuf @ %x\n", m)); \
248 (m)->m_type = (type); \
249 MBUFLOCK(mbstat.m_mtypes[type]++;) \
250 (m)->m_next = (struct mbuf *)NULL; \
251 (m)->m_nextpkt = (struct mbuf *)NULL; \
252 (m)->m_data = (m)->m_dat; \
255 (m) = (struct mbuf *)0; \
258 #define MGETHDR_DONT_RECURSE(m, how, type) { \
259 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
260 OS_DbgPrint(OSK_MID_TRACE,("(MGETHDR_DONT_RECURSE) got mbuf @ %x\n", m)); \
262 (m)->m_type = (type); \
263 MBUFLOCK(mbstat.m_mtypes[type]++;) \
264 (m)->m_next = (struct mbuf *)NULL; \
265 (m)->m_nextpkt = (struct mbuf *)NULL; \
266 (m)->m_data = (m)->m_pktdat; \
267 (m)->m_flags = M_PKTHDR; \
269 (m) = (struct mbuf *)0; \
275 * Mbuf cluster macros.
276 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
277 * MCLGET adds such clusters to a normal mbuf;
278 * the flag M_EXT is set upon success.
279 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
280 * freeing the cluster if the reference count has reached 0.
282 * Normal mbuf clusters are normally treated as character arrays
283 * after allocation, but use the first word of the buffer as a free list
284 * pointer while on the free list.
287 union mcluster
*mcl_next
;
288 char mcl_buf
[MCLBYTES
];
291 #define MCLALLOC(p, how) \
293 OS_DbgPrint(OSK_MID_TRACE,("(MCLALLOC)\n")); \
295 (void)m_clalloc(1, (how)); \
296 (p) = (caddr_t)mclfree; \
298 ++mclrefcnt[mtocl(p)]; \
300 mclfree = ((union mcluster *)(p))->mcl_next; \
305 #define MCLGET(m, how) { \
306 OS_DbgPrint(OSK_MID_TRACE,("(MCLGET) m = %x\n", m)); \
307 (m)->m_ext.ext_buf = malloc(MCLBYTES,__FILE__,__LINE__); \
308 if ((m)->m_ext.ext_buf != NULL) { \
309 (m)->m_data = (m)->m_ext.ext_buf; \
310 (m)->m_flags |= M_EXT; \
311 (m)->m_ext.ext_size = MCLBYTES; \
315 #define MCLFREE(p) { \
319 #define MCLGET(m, how) \
320 { MCLALLOC((m)->m_ext.ext_buf, (how)); \
321 OS_DbgPrint(OSK_MID_TRACE,("(MCLGET) m = %x\n", m)); \
322 if ((m)->m_ext.ext_buf != NULL) { \
323 (m)->m_data = (m)->m_ext.ext_buf; \
324 (m)->m_flags |= M_EXT; \
325 (m)->m_ext.ext_size = MCLBYTES; \
331 OS_DbgPrint(OSK_MID_TRACE,("(MCLFREE)\n")); \
332 if (--mclrefcnt[mtocl(p)] == 0) { \
333 ((union mcluster *)(p))->mcl_next = mclfree; \
334 mclfree = (union mcluster *)(p); \
340 #define MCLGET(m, how) \
341 { (m)->m_ext.ext_bufio = oskit_bufio_create(MCLBYTES); \
342 OS_DbgPrint(OSK_MID_TRACE,("(!OSKIT MCLGET)\n")); \
343 oskit_bufio_map((m)->m_ext.ext_bufio, \
344 (void **)&((m)->m_ext.ext_buf), 0, MCLBYTES); \
345 if ((m)->m_ext.ext_buf != NULL) { \
346 (m)->m_data = (m)->m_ext.ext_buf; \
347 (m)->m_flags |= M_EXT; \
348 (m)->m_ext.ext_size = MCLBYTES; \
354 * MFREE(struct mbuf *m, struct mbuf *n)
355 * Free a single mbuf and associated external storage.
356 * Place the successor, if any, in n.
359 #define MFREE(m, n) \
360 { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
361 if ((m)->m_flags & M_EXT) { \
362 if ((m)->m_ext.ext_free) \
363 (*((m)->m_ext.ext_free))((m)->m_ext.ext_buf, \
364 (m)->m_ext.ext_size); \
366 MCLFREE((m)->m_ext.ext_buf); \
369 FREE((m), mbtypes[(m)->m_type]); \
374 #define MFREE(m, nn) \
375 { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
376 OS_DbgPrint(OSK_MID_TRACE,("(OSKIT MFREE) m = %x\n", m)); \
377 if ((m)->m_flags & M_EXT) { \
378 oskit_bufio_release((m)->m_ext.ext_bufio); \
380 (nn) = (m)->m_next; \
381 FREE((m), mbtypes[(m)->m_type]); \
385 #define MFREE(m, nn) \
386 { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
387 OS_DbgPrint(OSK_MID_TRACE,("(!OSKIT MFREE) m = %x\n", m)); \
388 if ((m)->m_flags & M_EXT) { \
389 MCLFREE((m)->m_ext.ext_buf); \
391 (nn) = (m)->m_next; \
392 FREE((m), mbtypes[(m)->m_type]); \
399 * Copy mbuf pkthdr from from to to.
400 * from must have M_PKTHDR set, and to must be empty.
402 #define M_COPY_PKTHDR(to, from) { \
403 OS_DbgPrint(OSK_MID_TRACE,("(M_COPY_PKTHDR) to %x from %x\n", to, from)); \
404 (to)->m_pkthdr = (from)->m_pkthdr; \
405 (to)->m_flags = (from)->m_flags & M_COPYFLAGS; \
406 (to)->m_data = (to)->m_pktdat; \
410 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
411 * an object of the specified size at the end of the mbuf, longword aligned.
413 #define M_ALIGN(m, len) \
415 OS_DbgPrint(OSK_MID_TRACE,("(M_ALIGN) %d @ %x\n", m, len)); \
416 (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); \
419 * As above, for mbufs allocated with m_gethdr/MGETHDR
420 * or initialized by M_COPY_PKTHDR.
422 #define MH_ALIGN(m, len) \
424 OS_DbgPrint(OSK_MID_TRACE,("(MH_ALIGN) %d @ %x\n", m, len)); \
425 (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); \
429 * Compute the amount of space available
430 * before the current start of data in an mbuf.
433 /* Be bold and strong: why shouldn't that work??? */
434 #define M_LEADINGSPACE(m) \
435 ((m)->m_flags & M_EXT ? (m)->m_data - (m)->m_ext.ext_buf : \
436 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
437 (m)->m_data - (m)->m_dat)
439 #define M_LEADINGSPACE(m) \
440 ((m)->m_flags & M_EXT ? /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \
441 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
442 (m)->m_data - (m)->m_dat)
446 * Compute the amount of space available
447 * after the end of data in an mbuf.
449 #define M_TRAILINGSPACE(m) \
450 OS_DbgPrint(OSK_MID_TRACE,("(M_TRAILINGSPACE) %x\n", m)); \
451 ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size - \
452 ((m)->m_data + (m)->m_len) : \
453 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
456 * Arrange to prepend space of size plen to mbuf m.
457 * If a new mbuf must be allocated, how specifies whether to wait.
458 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
459 * is freed and m is set to NULL.
461 #define M_PREPEND(m, plen, how) { \
462 OS_DbgPrint(OSK_MID_TRACE,("(M_PREPEND) %d on %x\n", plen, m)); \
463 if (M_LEADINGSPACE(m) >= (plen)) { \
464 (m)->m_data -= (plen); \
465 (m)->m_len += (plen); \
467 (m) = m_prepend((m), (plen), (how)); \
468 if ((m) && (m)->m_flags & M_PKTHDR) \
469 (m)->m_pkthdr.len += (plen); \
472 /* change mbuf to new type */
473 #define MCHTYPE(m, t) { \
474 OS_DbgPrint(OSK_MID_TRACE,("(MCHTYPE) %x %x\n", m, t)); \
475 MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[t]++;) \
479 /* length to m_copy to copy all */
480 #define M_COPYALL 1000000000
482 /* compatiblity with 4.3 */
483 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
489 u_long m_mbufs
; /* mbufs obtained from page pool */
490 u_long m_clusters
; /* clusters obtained from page pool */
491 u_long m_spare
; /* spare field */
492 u_long m_clfree
; /* free clusters */
493 u_long m_drops
; /* times failed to find space */
494 u_long m_wait
; /* times waited for space */
495 u_long m_drain
; /* times drained protocols for space */
496 u_short m_mtypes
[256]; /* type specific mbuf allocations */
501 extern struct mbuf
*mbutl
; /* virtual address of mclusters */
502 extern char *mclrefcnt
; /* cluster reference counts */
504 struct mbstat mbstat
;
506 extern int nmbclusters
;
507 union mcluster
*mclfree
;
509 int max_linkhdr
; /* largest link-level header */
510 int max_protohdr
; /* largest protocol header */
511 int max_hdr
; /* largest link+protocol header */
512 int max_datalen
; /* MHLEN - max_hdr */
513 extern int mbtypes
[]; /* XXX */
515 int m_clalloc
__P((int, int));
516 void m_copyback
__P((struct mbuf
*, int, int, caddr_t
));
517 struct mbuf
*m_retry
__P((int, int));
518 struct mbuf
*m_retryhdr
__P((int, int));
519 void m_reclaim
__P((void));
520 struct mbuf
*m_get
__P((int, int));
521 struct mbuf
*m_gethdr
__P((int, int));
522 struct mbuf
*m_getclr
__P((int, int));
523 struct mbuf
*m_free
__P((struct mbuf
*));
524 void m_freem
__P((struct mbuf
*));
525 struct mbuf
*m_prepend
__P((struct mbuf
*,int,int));
526 struct mbuf
*m_copym
__P((struct mbuf
*, int, int, int));
527 void m_copydata
__P((struct mbuf
*,int,int,caddr_t
));
528 void m_cat
__P((struct mbuf
*,struct mbuf
*));
529 void m_adj
__P((struct mbuf
*,int));
530 struct mbuf
*m_pullup
__P((struct mbuf
*, int));
531 struct mbuf
*m_split
__P((struct mbuf
*,int,int));
532 struct mbuf
*m_devget
__P((char *, int, int, struct ifnet
*,
533 void (*copy
)(struct mbuf
*, caddr_t
, u_int
)));
536 int mbtypes
[] = { /* XXX */
537 M_FREE
, /* MT_FREE 0 should be on free list */
538 M_MBUF
, /* MT_DATA 1 dynamic (data) allocation */
539 M_MBUF
, /* MT_HEADER 2 packet header */
540 M_SOCKET
, /* MT_SOCKET 3 socket structure */
541 M_PCB
, /* MT_PCB 4 protocol control block */
542 M_RTABLE
, /* MT_RTABLE 5 routing tables */
543 M_HTABLE
, /* MT_HTABLE 6 IMP host tables */
544 0, /* MT_ATABLE 7 address resolution tables */
545 M_MBUF
, /* MT_SONAME 8 socket name */
547 M_SOOPTS
, /* MT_SOOPTS 10 socket options */
548 M_FTABLE
, /* MT_FTABLE 11 fragment reassembly header */
549 M_MBUF
, /* MT_RIGHTS 12 access rights */
550 M_IFADDR
, /* MT_IFADDR 13 interface address */
551 M_MBUF
, /* MT_CONTROL 14 extra-data protocol message */
552 M_MBUF
, /* MT_OOBDATA 15 expedited data */
554 25, 26, 27, 28, 29, 30, 31, 32 /* datakit ugliness */
560 #ifdef LOCAL_OSKIT_DEFINED
561 #undef LOCAL_OSKIT_DEFINED
565 #endif /* !_SYS_MBUF_H_ */