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>
61 * Mbufs are of a single size, MSIZE (machine/machparam.h), which
62 * includes overhead. An mbuf may add a single "mbuf cluster" of size
63 * MCLBYTES (also in machine/machparam.h), which has no additional overhead
64 * and is used instead of the internal data area; this is done when
65 * at least MINCLSIZE of data must be stored.
68 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
69 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
71 #define MINCLSIZE (MHLEN + MLEN) /* smallest amount to put in cluster */
72 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */
75 * Macros for type conversion
76 * mtod(m,t) - convert mbuf pointer to data pointer of correct type
77 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX)
78 * mtocl(x) - convert pointer within cluster to cluster index #
79 * cltom(x) - convert cluster # to ptr to beginning of cluster
81 #define mtod(m,t) ((t)((m)->m_data))
83 #define dtom(x) ((struct mbuf *)((int)(x) & ~(MSIZE-1)))
85 #define dtom(x) ((struct mbuf *)((int)(x) - sizeof(struct m_hdr)))
88 #define mtocl(x) (((u_int)(x) - (u_int)mbutl) >> MCLSHIFT)
89 #define cltom(x) ((caddr_t)((u_int)mbutl + ((u_int)(x) << MCLSHIFT)))
92 /* header at beginning of each mbuf: */
94 struct mbuf
*mh_next
; /* next buffer in chain */
95 struct mbuf
*mh_nextpkt
; /* next chain in queue/record */
96 int mh_len
; /* amount of data in this mbuf */
97 caddr_t mh_data
; /* location of data */
98 short mh_type
; /* type of data in this mbuf */
99 short mh_flags
; /* flags; see below */
102 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
104 int len
; /* total packet length */
105 struct ifnet
*rcvif
; /* rcv interface */
108 /* description of external storage mapped into mbuf, valid if M_EXT set */
110 caddr_t ext_buf
; /* start of buffer */
112 struct oskit_bufio
*ext_bufio
; /* OS Kit bufio pointer */
114 void (*ext_free
) /* free routine if not the usual */
115 __P((caddr_t
, u_int
));
117 u_int ext_size
; /* size of buffer, for ext_free */
124 struct pkthdr MH_pkthdr
; /* M_PKTHDR set */
126 struct m_ext MH_ext
; /* M_EXT set */
127 char MH_databuf
[MHLEN
];
130 char M_databuf
[MLEN
]; /* !M_PKTHDR, !M_EXT */
133 #define m_next m_hdr.mh_next
134 #define m_len m_hdr.mh_len
135 #define m_data m_hdr.mh_data
136 #define m_type m_hdr.mh_type
137 #define m_flags m_hdr.mh_flags
138 #define m_nextpkt m_hdr.mh_nextpkt
139 #define m_act m_nextpkt
140 #define m_pkthdr M_dat.MH.MH_pkthdr
141 #define m_ext M_dat.MH.MH_dat.MH_ext
142 #define m_pktdat M_dat.MH.MH_dat.MH_databuf
143 #define m_dat M_dat.M_databuf
146 #if defined(OSKIT) && !defined(__REACTOS__)
147 #include <oskit/io/bufio.h>
149 * A small step for mankind, but a huge leap for BSD:
150 * We consistently use oskit_bufios for external mbufs
153 #define M_EXT 0x0001 /* has associated external storage */
154 #define M_PKTHDR 0x0002 /* start of record */
155 #define M_EOR 0x0004 /* end of record */
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 */
161 /* flags copied when copying m_pkthdr */
162 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_BCAST|M_MCAST)
165 #define MT_FREE 0 /* should be on free list */
166 #define MT_DATA 1 /* dynamic (data) allocation */
167 #define MT_HEADER 2 /* packet header */
168 #define MT_SOCKET 3 /* socket structure */
169 #define MT_PCB 4 /* protocol control block */
170 #define MT_RTABLE 5 /* routing tables */
171 #define MT_HTABLE 6 /* IMP host tables */
172 #define MT_ATABLE 7 /* address resolution tables */
173 #define MT_SONAME 8 /* socket name */
174 #define MT_SOOPTS 10 /* socket options */
175 #define MT_FTABLE 11 /* fragment reassembly header */
176 #define MT_RIGHTS 12 /* access rights */
177 #define MT_IFADDR 13 /* interface address */
178 #define MT_CONTROL 14 /* extra-data protocol message */
179 #define MT_OOBDATA 15 /* expedited data */
181 /* flags to m_get/MGET */
182 #define M_DONTWAIT M_NOWAIT
183 #define M_WAIT M_WAITOK
186 * mbuf utility macros:
189 * prevents a section of code from from being interrupted by network
192 #define MBUFLOCK(code) \
193 { int ms = splimp(); \
199 * mbuf allocation/deallocation macros:
201 * MGET(struct mbuf *m, int how, int type)
202 * allocates an mbuf and initializes it to contain internal data.
204 * MGETHDR(struct mbuf *m, int how, int type)
205 * allocates an mbuf and initializes it to contain a packet header
208 #define MGET(m, how, type) { \
209 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
210 OS_DbgPrint(OSK_MID_TRACE,("(MGET) got mbuf @ %x\n", m)); \
212 (m)->m_type = (type); \
213 MBUFLOCK(mbstat.m_mtypes[type]++;) \
214 (m)->m_next = (struct mbuf *)NULL; \
215 (m)->m_nextpkt = (struct mbuf *)NULL; \
216 (m)->m_data = (m)->m_dat; \
219 (m) = m_retry((how), (type)); \
222 #define MGETHDR(m, how, type) { \
223 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
224 OS_DbgPrint(OSK_MID_TRACE,("(MGETHDR) got mbuf @ %x\n", m)); \
226 (m)->m_type = (type); \
227 MBUFLOCK(mbstat.m_mtypes[type]++;) \
228 (m)->m_next = (struct mbuf *)NULL; \
229 (m)->m_nextpkt = (struct mbuf *)NULL; \
230 (m)->m_data = (m)->m_pktdat; \
231 (m)->m_flags = M_PKTHDR; \
233 (m) = m_retryhdr((how), (type)); \
237 #define MGET_DONT_RECURSE(m, how, type) { \
238 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
239 OS_DbgPrint(OSK_MID_TRACE,("(MGET_DONT_RECURSE) got mbuf @ %x\n", m)); \
241 (m)->m_type = (type); \
242 MBUFLOCK(mbstat.m_mtypes[type]++;) \
243 (m)->m_next = (struct mbuf *)NULL; \
244 (m)->m_nextpkt = (struct mbuf *)NULL; \
245 (m)->m_data = (m)->m_dat; \
248 (m) = (struct mbuf *)0; \
251 #define MGETHDR_DONT_RECURSE(m, how, type) { \
252 MALLOC((m), struct mbuf *, MSIZE, mbtypes[type], (how)); \
253 OS_DbgPrint(OSK_MID_TRACE,("(MGETHDR_DONT_RECURSE) got mbuf @ %x\n", m)); \
255 (m)->m_type = (type); \
256 MBUFLOCK(mbstat.m_mtypes[type]++;) \
257 (m)->m_next = (struct mbuf *)NULL; \
258 (m)->m_nextpkt = (struct mbuf *)NULL; \
259 (m)->m_data = (m)->m_pktdat; \
260 (m)->m_flags = M_PKTHDR; \
262 (m) = (struct mbuf *)0; \
268 * Mbuf cluster macros.
269 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
270 * MCLGET adds such clusters to a normal mbuf;
271 * the flag M_EXT is set upon success.
272 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
273 * freeing the cluster if the reference count has reached 0.
275 * Normal mbuf clusters are normally treated as character arrays
276 * after allocation, but use the first word of the buffer as a free list
277 * pointer while on the free list.
280 union mcluster
*mcl_next
;
281 char mcl_buf
[MCLBYTES
];
284 #define MCLALLOC(p, how) \
286 OS_DbgPrint(OSK_MID_TRACE,("(MCLALLOC)\n")); \
288 (void)m_clalloc(1, (how)); \
289 (p) = (caddr_t)mclfree; \
291 ++mclrefcnt[mtocl(p)]; \
293 mclfree = ((union mcluster *)(p))->mcl_next; \
297 #define MCLGET(m, how) \
298 { MCLALLOC((m)->m_ext.ext_buf, (how)); \
299 OS_DbgPrint(OSK_MID_TRACE,("(MCLGET) m = %x\n", m)); \
300 if ((m)->m_ext.ext_buf != NULL) { \
301 (m)->m_data = (m)->m_ext.ext_buf; \
302 (m)->m_flags |= M_EXT; \
303 (m)->m_ext.ext_size = MCLBYTES; \
309 OS_DbgPrint(OSK_MID_TRACE,("(MCLFREE)\n")); \
310 if (--mclrefcnt[mtocl(p)] == 0) { \
311 ((union mcluster *)(p))->mcl_next = mclfree; \
312 mclfree = (union mcluster *)(p); \
317 #define MCLGET(m, how) \
318 { (m)->m_ext.ext_bufio = oskit_bufio_create(MCLBYTES); \
319 OS_DbgPrint(OSK_MID_TRACE,("(!OSKIT MCLGET)\n")); \
320 oskit_bufio_map((m)->m_ext.ext_bufio, \
321 (void **)&((m)->m_ext.ext_buf), 0, MCLBYTES); \
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 * MFREE(struct mbuf *m, struct mbuf *n)
332 * Free a single mbuf and associated external storage.
333 * Place the successor, if any, in n.
336 #define MFREE(m, n) \
337 { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
338 if ((m)->m_flags & M_EXT) { \
339 if ((m)->m_ext.ext_free) \
340 (*((m)->m_ext.ext_free))((m)->m_ext.ext_buf, \
341 (m)->m_ext.ext_size); \
343 MCLFREE((m)->m_ext.ext_buf); \
346 FREE((m), mbtypes[(m)->m_type]); \
351 #define MFREE(m, nn) \
352 { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
353 OS_DbgPrint(OSK_MID_TRACE,("(OSKIT MFREE) m = %x\n", m)); \
354 if ((m)->m_flags & M_EXT) { \
355 oskit_bufio_release((m)->m_ext.ext_bufio); \
357 (nn) = (m)->m_next; \
358 FREE((m), mbtypes[(m)->m_type]); \
362 #define MFREE(m, nn) \
363 { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
364 OS_DbgPrint(OSK_MID_TRACE,("(!OSKIT MFREE) m = %x\n", m)); \
365 if ((m)->m_flags & M_EXT) { \
366 MCLFREE((m)->m_ext.ext_buf); \
368 (nn) = (m)->m_next; \
369 FREE((m), mbtypes[(m)->m_type]); \
376 * Copy mbuf pkthdr from from to to.
377 * from must have M_PKTHDR set, and to must be empty.
379 #define M_COPY_PKTHDR(to, from) { \
380 OS_DbgPrint(OSK_MID_TRACE,("(M_COPY_PKTHDR) to %x from %x\n", to, from)); \
381 (to)->m_pkthdr = (from)->m_pkthdr; \
382 (to)->m_flags = (from)->m_flags & M_COPYFLAGS; \
383 (to)->m_data = (to)->m_pktdat; \
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.
390 #define M_ALIGN(m, len) \
392 OS_DbgPrint(OSK_MID_TRACE,("(M_ALIGN) %d @ %x\n", m, len)); \
393 (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); \
396 * As above, for mbufs allocated with m_gethdr/MGETHDR
397 * or initialized by M_COPY_PKTHDR.
399 #define MH_ALIGN(m, len) \
401 OS_DbgPrint(OSK_MID_TRACE,("(MH_ALIGN) %d @ %x\n", m, len)); \
402 (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); \
406 * Compute the amount of space available
407 * before the current start of data in an mbuf.
410 /* Be bold and strong: why shouldn't that work??? */
411 #define M_LEADINGSPACE(m) \
412 ((m)->m_flags & M_EXT ? (m)->m_data - (m)->m_ext.ext_buf : \
413 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
414 (m)->m_data - (m)->m_dat)
416 #define M_LEADINGSPACE(m) \
417 ((m)->m_flags & M_EXT ? /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \
418 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
419 (m)->m_data - (m)->m_dat)
423 * Compute the amount of space available
424 * after the end of data in an mbuf.
426 #define M_TRAILINGSPACE(m) \
427 OS_DbgPrint(OSK_MID_TRACE,("(M_TRAILINGSPACE) %x\n", m)); \
428 ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size - \
429 ((m)->m_data + (m)->m_len) : \
430 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
433 * Arrange to prepend space of size plen to mbuf m.
434 * If a new mbuf must be allocated, how specifies whether to wait.
435 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
436 * is freed and m is set to NULL.
438 #define M_PREPEND(m, plen, how) { \
439 OS_DbgPrint(OSK_MID_TRACE,("(M_PREPEND) %d on %x\n", plen, m)); \
440 if (M_LEADINGSPACE(m) >= (plen)) { \
441 (m)->m_data -= (plen); \
442 (m)->m_len += (plen); \
444 (m) = m_prepend((m), (plen), (how)); \
445 if ((m) && (m)->m_flags & M_PKTHDR) \
446 (m)->m_pkthdr.len += (plen); \
449 /* change mbuf to new type */
450 #define MCHTYPE(m, t) { \
451 OS_DbgPrint(OSK_MID_TRACE,("(MCHTYPE) %x %x\n", m, t)); \
452 MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[t]++;) \
456 /* length to m_copy to copy all */
457 #define M_COPYALL 1000000000
459 /* compatiblity with 4.3 */
460 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
466 u_long m_mbufs
; /* mbufs obtained from page pool */
467 u_long m_clusters
; /* clusters obtained from page pool */
468 u_long m_spare
; /* spare field */
469 u_long m_clfree
; /* free clusters */
470 u_long m_drops
; /* times failed to find space */
471 u_long m_wait
; /* times waited for space */
472 u_long m_drain
; /* times drained protocols for space */
473 u_short m_mtypes
[256]; /* type specific mbuf allocations */
478 extern struct mbuf
*mbutl
; /* virtual address of mclusters */
479 extern char *mclrefcnt
; /* cluster reference counts */
481 struct mbstat mbstat
;
483 extern int nmbclusters
;
484 union mcluster
*mclfree
;
486 int max_linkhdr
; /* largest link-level header */
487 int max_protohdr
; /* largest protocol header */
488 int max_hdr
; /* largest link+protocol header */
489 int max_datalen
; /* MHLEN - max_hdr */
490 extern int mbtypes
[]; /* XXX */
492 int m_clalloc
__P((int, int));
493 void m_copyback
__P((struct mbuf
*, int, int, caddr_t
));
494 struct mbuf
*m_retry
__P((int, int));
495 struct mbuf
*m_retryhdr
__P((int, int));
496 void m_reclaim
__P((void));
497 struct mbuf
*m_get
__P((int, int));
498 struct mbuf
*m_gethdr
__P((int, int));
499 struct mbuf
*m_getclr
__P((int, int));
500 struct mbuf
*m_free
__P((struct mbuf
*));
501 void m_freem
__P((struct mbuf
*));
502 struct mbuf
*m_prepend
__P((struct mbuf
*,int,int));
503 struct mbuf
*m_copym
__P((struct mbuf
*, int, int, int));
504 void m_copydata
__P((struct mbuf
*,int,int,caddr_t
));
505 void m_cat
__P((struct mbuf
*,struct mbuf
*));
506 void m_adj
__P((struct mbuf
*,int));
507 struct mbuf
*m_pullup
__P((struct mbuf
*, int));
508 struct mbuf
*m_split
__P((struct mbuf
*,int,int));
509 struct mbuf
*m_devget
__P((char *, int, int, struct ifnet
*,
510 void (*copy
)(struct mbuf
*, caddr_t
, u_int
)));
513 int mbtypes
[] = { /* XXX */
514 M_FREE
, /* MT_FREE 0 should be on free list */
515 M_MBUF
, /* MT_DATA 1 dynamic (data) allocation */
516 M_MBUF
, /* MT_HEADER 2 packet header */
517 M_SOCKET
, /* MT_SOCKET 3 socket structure */
518 M_PCB
, /* MT_PCB 4 protocol control block */
519 M_RTABLE
, /* MT_RTABLE 5 routing tables */
520 M_HTABLE
, /* MT_HTABLE 6 IMP host tables */
521 0, /* MT_ATABLE 7 address resolution tables */
522 M_MBUF
, /* MT_SONAME 8 socket name */
524 M_SOOPTS
, /* MT_SOOPTS 10 socket options */
525 M_FTABLE
, /* MT_FTABLE 11 fragment reassembly header */
526 M_MBUF
, /* MT_RIGHTS 12 access rights */
527 M_IFADDR
, /* MT_IFADDR 13 interface address */
528 M_MBUF
, /* MT_CONTROL 14 extra-data protocol message */
529 M_MBUF
, /* MT_OOBDATA 15 expedited data */
531 25, 26, 27, 28, 29, 30, 31, 32 /* datakit ugliness */
537 #endif /* !_SYS_MBUF_H_ */