ddc7ae7029a23ce9dec3be78d91e5408d7456ea1
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_data = malloc(MCLBYTES); \
308 (m)->m_flags |= M_EXT; \
309 (m)->m_ext.ext_size = MCLBYTES; \
312 #define MCLFREE(p) { \
313 OS_DbgPrint(OSK_MID_TRACE,("(MCLFREE) p = %x\n", p)); \
314 free( (m)->m_data ); \
317 #define MCLGET(m, how) \
318 { MCLALLOC((m)->m_ext.ext_buf, (how)); \
319 OS_DbgPrint(OSK_MID_TRACE,("(MCLGET) m = %x\n", m)); \
320 if ((m)->m_ext.ext_buf != NULL) { \
321 (m)->m_data = (m)->m_ext.ext_buf; \
322 (m)->m_flags |= M_EXT; \
323 (m)->m_ext.ext_size = MCLBYTES; \
329 OS_DbgPrint(OSK_MID_TRACE,("(MCLFREE)\n")); \
330 if (--mclrefcnt[mtocl(p)] == 0) { \
331 ((union mcluster *)(p))->mcl_next = mclfree; \
332 mclfree = (union mcluster *)(p); \
338 #define MCLGET(m, how) \
339 { (m)->m_ext.ext_bufio = oskit_bufio_create(MCLBYTES); \
340 OS_DbgPrint(OSK_MID_TRACE,("(!OSKIT MCLGET)\n")); \
341 oskit_bufio_map((m)->m_ext.ext_bufio, \
342 (void **)&((m)->m_ext.ext_buf), 0, MCLBYTES); \
343 if ((m)->m_ext.ext_buf != NULL) { \
344 (m)->m_data = (m)->m_ext.ext_buf; \
345 (m)->m_flags |= M_EXT; \
346 (m)->m_ext.ext_size = MCLBYTES; \
352 * MFREE(struct mbuf *m, struct mbuf *n)
353 * Free a single mbuf and associated external storage.
354 * Place the successor, if any, in n.
357 #define MFREE(m, n) \
358 { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
359 if ((m)->m_flags & M_EXT) { \
360 if ((m)->m_ext.ext_free) \
361 (*((m)->m_ext.ext_free))((m)->m_ext.ext_buf, \
362 (m)->m_ext.ext_size); \
364 MCLFREE((m)->m_ext.ext_buf); \
367 FREE((m), mbtypes[(m)->m_type]); \
371 #define MFREE(m, nn) \
372 { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
373 OS_DbgPrint(OSK_MID_TRACE,("(OSKIT MFREE) m = %x\n", m)); \
374 if ((m)->m_flags & M_EXT) { \
375 oskit_bufio_release((m)->m_ext.ext_bufio); \
377 (nn) = (m)->m_next; \
378 FREE((m), mbtypes[(m)->m_type]); \
381 #define MFREE(m, nn) \
382 { MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--;) \
383 OS_DbgPrint(OSK_MID_TRACE,("(!OSKIT MFREE) m = %x\n", m)); \
384 if ((m)->m_flags & M_EXT) { \
385 MCLFREE((m)->m_ext.ext_buf); \
387 (nn) = (m)->m_next; \
388 FREE((m), mbtypes[(m)->m_type]); \
394 * Copy mbuf pkthdr from from to to.
395 * from must have M_PKTHDR set, and to must be empty.
397 #define M_COPY_PKTHDR(to, from) { \
398 OS_DbgPrint(OSK_MID_TRACE,("(M_COPY_PKTHDR) to %x from %x\n", to, from)); \
399 (to)->m_pkthdr = (from)->m_pkthdr; \
400 (to)->m_flags = (from)->m_flags & M_COPYFLAGS; \
401 (to)->m_data = (to)->m_pktdat; \
405 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
406 * an object of the specified size at the end of the mbuf, longword aligned.
408 #define M_ALIGN(m, len) \
410 OS_DbgPrint(OSK_MID_TRACE,("(M_ALIGN) %d @ %x\n", m, len)); \
411 (m)->m_data += (MLEN - (len)) &~ (sizeof(long) - 1); \
414 * As above, for mbufs allocated with m_gethdr/MGETHDR
415 * or initialized by M_COPY_PKTHDR.
417 #define MH_ALIGN(m, len) \
419 OS_DbgPrint(OSK_MID_TRACE,("(MH_ALIGN) %d @ %x\n", m, len)); \
420 (m)->m_data += (MHLEN - (len)) &~ (sizeof(long) - 1); \
424 * Compute the amount of space available
425 * before the current start of data in an mbuf.
428 /* Be bold and strong: why shouldn't that work??? */
429 #define M_LEADINGSPACE(m) \
430 ((m)->m_flags & M_EXT ? (m)->m_data - (m)->m_ext.ext_buf : \
431 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
432 (m)->m_data - (m)->m_dat)
434 #define M_LEADINGSPACE(m) \
435 ((m)->m_flags & M_EXT ? /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \
436 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
437 (m)->m_data - (m)->m_dat)
441 * Compute the amount of space available
442 * after the end of data in an mbuf.
444 #define M_TRAILINGSPACE(m) \
445 OS_DbgPrint(OSK_MID_TRACE,("(M_TRAILINGSPACE) %x\n", m)); \
446 ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size - \
447 ((m)->m_data + (m)->m_len) : \
448 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
451 * Arrange to prepend space of size plen to mbuf m.
452 * If a new mbuf must be allocated, how specifies whether to wait.
453 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
454 * is freed and m is set to NULL.
456 #define M_PREPEND(m, plen, how) { \
457 OS_DbgPrint(OSK_MID_TRACE,("(M_PREPEND) %d on %x\n", plen, m)); \
458 if (M_LEADINGSPACE(m) >= (plen)) { \
459 (m)->m_data -= (plen); \
460 (m)->m_len += (plen); \
462 (m) = m_prepend((m), (plen), (how)); \
463 if ((m) && (m)->m_flags & M_PKTHDR) \
464 (m)->m_pkthdr.len += (plen); \
467 /* change mbuf to new type */
468 #define MCHTYPE(m, t) { \
469 OS_DbgPrint(OSK_MID_TRACE,("(MCHTYPE) %x %x\n", m, t)); \
470 MBUFLOCK(mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[t]++;) \
474 /* length to m_copy to copy all */
475 #define M_COPYALL 1000000000
477 /* compatiblity with 4.3 */
478 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
484 u_long m_mbufs
; /* mbufs obtained from page pool */
485 u_long m_clusters
; /* clusters obtained from page pool */
486 u_long m_spare
; /* spare field */
487 u_long m_clfree
; /* free clusters */
488 u_long m_drops
; /* times failed to find space */
489 u_long m_wait
; /* times waited for space */
490 u_long m_drain
; /* times drained protocols for space */
491 u_short m_mtypes
[256]; /* type specific mbuf allocations */
496 extern struct mbuf
*mbutl
; /* virtual address of mclusters */
497 extern char *mclrefcnt
; /* cluster reference counts */
499 struct mbstat mbstat
;
501 extern int nmbclusters
;
502 union mcluster
*mclfree
;
504 int max_linkhdr
; /* largest link-level header */
505 int max_protohdr
; /* largest protocol header */
506 int max_hdr
; /* largest link+protocol header */
507 int max_datalen
; /* MHLEN - max_hdr */
508 extern int mbtypes
[]; /* XXX */
510 int m_clalloc
__P((int, int));
511 void m_copyback
__P((struct mbuf
*, int, int, caddr_t
));
512 struct mbuf
*m_retry
__P((int, int));
513 struct mbuf
*m_retryhdr
__P((int, int));
514 void m_reclaim
__P((void));
515 struct mbuf
*m_get
__P((int, int));
516 struct mbuf
*m_gethdr
__P((int, int));
517 struct mbuf
*m_getclr
__P((int, int));
518 struct mbuf
*m_free
__P((struct mbuf
*));
519 void m_freem
__P((struct mbuf
*));
520 struct mbuf
*m_prepend
__P((struct mbuf
*,int,int));
521 struct mbuf
*m_copym
__P((struct mbuf
*, int, int, int));
522 void m_copydata
__P((struct mbuf
*,int,int,caddr_t
));
523 void m_cat
__P((struct mbuf
*,struct mbuf
*));
524 void m_adj
__P((struct mbuf
*,int));
525 struct mbuf
*m_pullup
__P((struct mbuf
*, int));
526 struct mbuf
*m_split
__P((struct mbuf
*,int,int));
527 struct mbuf
*m_devget
__P((char *, int, int, struct ifnet
*,
528 void (*copy
)(struct mbuf
*, caddr_t
, u_int
)));
531 int mbtypes
[] = { /* XXX */
532 M_FREE
, /* MT_FREE 0 should be on free list */
533 M_MBUF
, /* MT_DATA 1 dynamic (data) allocation */
534 M_MBUF
, /* MT_HEADER 2 packet header */
535 M_SOCKET
, /* MT_SOCKET 3 socket structure */
536 M_PCB
, /* MT_PCB 4 protocol control block */
537 M_RTABLE
, /* MT_RTABLE 5 routing tables */
538 M_HTABLE
, /* MT_HTABLE 6 IMP host tables */
539 0, /* MT_ATABLE 7 address resolution tables */
540 M_MBUF
, /* MT_SONAME 8 socket name */
542 M_SOOPTS
, /* MT_SOOPTS 10 socket options */
543 M_FTABLE
, /* MT_FTABLE 11 fragment reassembly header */
544 M_MBUF
, /* MT_RIGHTS 12 access rights */
545 M_IFADDR
, /* MT_IFADDR 13 interface address */
546 M_MBUF
, /* MT_CONTROL 14 extra-data protocol message */
547 M_MBUF
, /* MT_OOBDATA 15 expedited data */
549 25, 26, 27, 28, 29, 30, 31, 32 /* datakit ugliness */
555 #ifdef LOCAL_OSKIT_DEFINED
556 #undef LOCAL_OSKIT_DEFINED
560 #endif /* !_SYS_MBUF_H_ */