FreeBSD/Linux Kernel Cross Reference
sys/sys/mbuf.h
1 /*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 * @(#)mbuf.h 8.5 (Berkeley) 2/19/95
34 * $FreeBSD$
35 */
36
37 #ifndef _SYS_MBUF_H_
38 #define _SYS_MBUF_H_
39
40 /*
41 * Mbufs are of a single size, MSIZE (machine/param.h), which
42 * includes overhead. An mbuf may add a single "mbuf cluster" of size
43 * MCLBYTES (also in machine/param.h), which has no additional overhead
44 * and is used instead of the internal data area; this is done when
45 * at least MINCLSIZE of data must be stored.
46 */
47
48 #define MLEN (MSIZE - sizeof(struct m_hdr)) /* normal data len */
49 #define MHLEN (MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
50
51 #define MINCLSIZE (MHLEN + 1) /* smallest amount to put in cluster */
52 #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */
53
54 /*
55 * Macros for type conversion
56 * mtod(m, t) - convert mbuf pointer to data pointer of correct type
57 * dtom(x) - convert data pointer within mbuf to mbuf pointer (XXX)
58 * mtocl(x) - convert pointer within cluster to cluster index #
59 * cltom(x) - convert cluster # to ptr to beginning of cluster
60 */
61 #define mtod(m, t) ((t)((m)->m_data))
62 #define dtom(x) ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1)))
63 #define mtocl(x) (((uintptr_t)(x) - (uintptr_t)mbutl) >> MCLSHIFT)
64 #define cltom(x) ((caddr_t)((uintptr_t)mbutl + \
65 ((uintptr_t)(x) << MCLSHIFT)))
66
67 /* header at beginning of each mbuf: */
68 struct m_hdr {
69 struct mbuf *mh_next; /* next buffer in chain */
70 struct mbuf *mh_nextpkt; /* next chain in queue/record */
71 caddr_t mh_data; /* location of data */
72 int mh_len; /* amount of data in this mbuf */
73 short mh_type; /* type of data in this mbuf */
74 short mh_flags; /* flags; see below */
75 };
76
77 /* record/packet header in first mbuf of chain; valid if M_PKTHDR set */
78 struct pkthdr {
79 struct ifnet *rcvif; /* rcv interface */
80 int len; /* total packet length */
81 /* variables for ip and tcp reassembly */
82 caddr_t header; /* pointer to packet header */
83 };
84
85 /* description of external storage mapped into mbuf, valid if M_EXT set */
86 struct m_ext {
87 caddr_t ext_buf; /* start of buffer */
88 void (*ext_free) /* free routine if not the usual */
89 __P((caddr_t, u_int));
90 u_int ext_size; /* size of buffer, for ext_free */
91 void (*ext_ref) /* add a reference to the ext object */
92 __P((caddr_t, u_int));
93 };
94
95 struct mbuf {
96 struct m_hdr m_hdr;
97 union {
98 struct {
99 struct pkthdr MH_pkthdr; /* M_PKTHDR set */
100 union {
101 struct m_ext MH_ext; /* M_EXT set */
102 char MH_databuf[MHLEN];
103 } MH_dat;
104 } MH;
105 char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
106 } M_dat;
107 };
108 #define m_next m_hdr.mh_next
109 #define m_len m_hdr.mh_len
110 #define m_data m_hdr.mh_data
111 #define m_type m_hdr.mh_type
112 #define m_flags m_hdr.mh_flags
113 #define m_nextpkt m_hdr.mh_nextpkt
114 #define m_act m_nextpkt
115 #define m_pkthdr M_dat.MH.MH_pkthdr
116 #define m_ext M_dat.MH.MH_dat.MH_ext
117 #define m_pktdat M_dat.MH.MH_dat.MH_databuf
118 #define m_dat M_dat.M_databuf
119
120 /* mbuf flags */
121 #define M_EXT 0x0001 /* has associated external storage */
122 #define M_PKTHDR 0x0002 /* start of record */
123 #define M_EOR 0x0004 /* end of record */
124 #define M_PROTO1 0x0008 /* protocol-specific */
125 #define M_PROTO2 0x0010 /* protocol-specific */
126 #define M_PROTO3 0x0020 /* protocol-specific */
127 #define M_PROTO4 0x0040 /* protocol-specific */
128 #define M_PROTO5 0x0080 /* protocol-specific */
129
130 /* mbuf pkthdr flags, also in m_flags */
131 #define M_BCAST 0x0100 /* send/received as link-level broadcast */
132 #define M_MCAST 0x0200 /* send/received as link-level multicast */
133 #define M_FRAG 0x0400 /* packet is a fragment of a larger packet */
134
135 /* flags copied when copying m_pkthdr */
136 #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO1|M_PROTO2|M_PROTO3 | \
137 M_PROTO4|M_PROTO5|M_BCAST|M_MCAST|M_FRAG)
138
139 /* mbuf types */
140 #define MT_FREE 0 /* should be on free list */
141 #define MT_DATA 1 /* dynamic (data) allocation */
142 #define MT_HEADER 2 /* packet header */
143 #if 0
144 #define MT_SOCKET 3 /* socket structure */
145 #define MT_PCB 4 /* protocol control block */
146 #define MT_RTABLE 5 /* routing tables */
147 #define MT_HTABLE 6 /* IMP host tables */
148 #define MT_ATABLE 7 /* address resolution tables */
149 #endif
150 #define MT_SONAME 8 /* socket name */
151 #if 0
152 #define MT_SOOPTS 10 /* socket options */
153 #endif
154 #define MT_FTABLE 11 /* fragment reassembly header */
155 #if 0
156 #define MT_RIGHTS 12 /* access rights */
157 #define MT_IFADDR 13 /* interface address */
158 #endif
159 #define MT_CONTROL 14 /* extra-data protocol message */
160 #define MT_OOBDATA 15 /* expedited data */
161
162 /*
163 * mbuf statistics
164 */
165 struct mbstat {
166 u_long m_mbufs; /* mbufs obtained from page pool */
167 u_long m_clusters; /* clusters obtained from page pool */
168 u_long m_spare; /* spare field */
169 u_long m_clfree; /* free clusters */
170 u_long m_drops; /* times failed to find space */
171 u_long m_wait; /* times waited for space */
172 u_long m_drain; /* times drained protocols for space */
173 u_short m_mtypes[256]; /* type specific mbuf allocations */
174 u_long m_mcfail; /* times m_copym failed */
175 u_long m_mpfail; /* times m_pullup failed */
176 u_long m_msize; /* length of an mbuf */
177 u_long m_mclbytes; /* length of an mbuf cluster */
178 u_long m_minclsize; /* min length of data to allocate a cluster */
179 u_long m_mlen; /* length of data in an mbuf */
180 u_long m_mhlen; /* length of data in a header mbuf */
181 };
182
183 /* flags to m_get/MGET */
184 #define M_DONTWAIT 1
185 #define M_WAIT 0
186
187 /* Freelists:
188 *
189 * Normal mbuf clusters are normally treated as character arrays
190 * after allocation, but use the first word of the buffer as a free list
191 * pointer while on the free list.
192 */
193 union mcluster {
194 union mcluster *mcl_next;
195 char mcl_buf[MCLBYTES];
196 };
197
198
199 /*
200 * These are identifying numbers passed to the m_mballoc_wait function,
201 * allowing us to determine whether the call came from an MGETHDR or
202 * an MGET.
203 */
204 #define MGETHDR_C 1
205 #define MGET_C 2
206
207 /*
208 * Wake up the next instance (if any) of m_mballoc_wait() which is
209 * waiting for an mbuf to be freed. This should be called at splimp().
210 *
211 * XXX: If there is another free mbuf, this routine will be called [again]
212 * from the m_mballoc_wait routine in order to wake another sleep instance.
213 */
214 #define MMBWAKEUP() do { \
215 if (m_mballoc_wid) { \
216 m_mballoc_wid--; \
217 wakeup_one(&m_mballoc_wid); \
218 } \
219 } while (0)
220
221 /*
222 * Same as above, but for mbuf cluster(s).
223 */
224 #define MCLWAKEUP() do { \
225 if (m_clalloc_wid) { \
226 m_clalloc_wid--; \
227 wakeup_one(&m_clalloc_wid); \
228 } \
229 } while (0)
230
231 /*
232 * mbuf utility macros:
233 *
234 * MBUFLOCK(code)
235 * prevents a section of code from from being interrupted by network
236 * drivers.
237 */
238 #define MBUFLOCK(code) do { \
239 int _ms = splimp(); \
240 \
241 { code } \
242 splx(_ms); \
243 } while (0)
244
245 /*
246 * mbuf allocation/deallocation macros:
247 *
248 * MGET(struct mbuf *m, int how, int type)
249 * allocates an mbuf and initializes it to contain internal data.
250 *
251 * MGETHDR(struct mbuf *m, int how, int type)
252 * allocates an mbuf and initializes it to contain a packet header
253 * and internal data.
254 */
255 #define MGET(m, how, type) do { \
256 struct mbuf *_mm; \
257 int _mhow = (how); \
258 int _mtype = (type); \
259 int _ms = splimp(); \
260 \
261 if (mmbfree == NULL) \
262 (void)m_mballoc(1, _mhow); \
263 _mm = mmbfree; \
264 if (_mm != NULL) { \
265 mmbfree = _mm->m_next; \
266 mbstat.m_mtypes[MT_FREE]--; \
267 _mm->m_type = _mtype; \
268 mbstat.m_mtypes[_mtype]++; \
269 _mm->m_next = NULL; \
270 _mm->m_nextpkt = NULL; \
271 _mm->m_data = _mm->m_dat; \
272 _mm->m_flags = 0; \
273 (m) = _mm; \
274 splx(_ms); \
275 } else { \
276 splx(_ms); \
277 _mm = m_retry(_mhow, _mtype); \
278 if (_mm == NULL && _mhow == M_WAIT) \
279 (m) = m_mballoc_wait(MGET_C, _mtype); \
280 else \
281 (m) = _mm; \
282 } \
283 } while (0)
284
285 #define MGETHDR(m, how, type) do { \
286 struct mbuf *_mm; \
287 int _mhow = (how); \
288 int _mtype = (type); \
289 int _ms = splimp(); \
290 \
291 if (mmbfree == NULL) \
292 (void)m_mballoc(1, _mhow); \
293 _mm = mmbfree; \
294 if (_mm != NULL) { \
295 mmbfree = _mm->m_next; \
296 mbstat.m_mtypes[MT_FREE]--; \
297 _mm->m_type = _mtype; \
298 mbstat.m_mtypes[_mtype]++; \
299 _mm->m_next = NULL; \
300 _mm->m_nextpkt = NULL; \
301 _mm->m_data = _mm->m_pktdat; \
302 _mm->m_flags = M_PKTHDR; \
303 _mm->m_pkthdr.rcvif = NULL; \
304 (m) = _mm; \
305 splx(_ms); \
306 } else { \
307 splx(_ms); \
308 _mm = m_retryhdr(_mhow, _mtype); \
309 if (_mm == NULL && _mhow == M_WAIT) \
310 (m) = m_mballoc_wait(MGETHDR_C, _mtype); \
311 else \
312 (m) = _mm; \
313 } \
314 } while (0)
315
316 /*
317 * Mbuf cluster macros.
318 * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
319 * MCLGET adds such clusters to a normal mbuf;
320 * the flag M_EXT is set upon success.
321 * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
322 * freeing the cluster if the reference count has reached 0.
323 */
324 #define MCLALLOC(p, how) do { \
325 caddr_t _mp; \
326 int _mhow = (how); \
327 int _ms = splimp(); \
328 \
329 if (mclfree == NULL) \
330 (void)m_clalloc(1, _mhow); \
331 _mp = (caddr_t)mclfree; \
332 if (_mp != NULL) { \
333 mclrefcnt[mtocl(_mp)]++; \
334 mbstat.m_clfree--; \
335 mclfree = ((union mcluster *)_mp)->mcl_next; \
336 (p) = _mp; \
337 splx(_ms); \
338 } else { \
339 splx(_ms); \
340 if (_mhow == M_WAIT) \
341 (p) = m_clalloc_wait(); \
342 else \
343 (p) = NULL; \
344 } \
345 } while (0)
346
347 #define MCLGET(m, how) do { \
348 struct mbuf *_mm = (m); \
349 \
350 MCLALLOC(_mm->m_ext.ext_buf, (how)); \
351 if (_mm->m_ext.ext_buf != NULL) { \
352 _mm->m_data = _mm->m_ext.ext_buf; \
353 _mm->m_flags |= M_EXT; \
354 _mm->m_ext.ext_free = NULL; \
355 _mm->m_ext.ext_ref = NULL; \
356 _mm->m_ext.ext_size = MCLBYTES; \
357 } \
358 } while (0)
359
360 #define MCLFREE1(p) do { \
361 union mcluster *_mp = (union mcluster *)(p); \
362 \
363 KASSERT(mclrefcnt[mtocl(_mp)] > 0, ("freeing free cluster")); \
364 if (--mclrefcnt[mtocl(_mp)] == 0) { \
365 _mp->mcl_next = mclfree; \
366 mclfree = _mp; \
367 mbstat.m_clfree++; \
368 MCLWAKEUP(); \
369 } \
370 } while (0)
371
372 #define MCLFREE(p) MBUFLOCK( \
373 MCLFREE1(p); \
374 )
375
376 #define MEXTFREE1(m) do { \
377 struct mbuf *_mm = (m); \
378 \
379 if (_mm->m_ext.ext_free != NULL) \
380 (*_mm->m_ext.ext_free)(_mm->m_ext.ext_buf, \
381 _mm->m_ext.ext_size); \
382 else \
383 MCLFREE1(_mm->m_ext.ext_buf); \
384 } while (0)
385
386 #define MEXTFREE(m) MBUFLOCK( \
387 MEXTFREE1(m); \
388 )
389
390 /*
391 * MFREE(struct mbuf *m, struct mbuf *n)
392 * Free a single mbuf and associated external storage.
393 * Place the successor, if any, in n.
394 */
395 #define MFREE(m, n) MBUFLOCK( \
396 struct mbuf *_mm = (m); \
397 \
398 KASSERT(_mm->m_type != MT_FREE, ("freeing free mbuf")); \
399 mbstat.m_mtypes[_mm->m_type]--; \
400 if (_mm->m_flags & M_EXT) \
401 MEXTFREE1(m); \
402 (n) = _mm->m_next; \
403 _mm->m_type = MT_FREE; \
404 mbstat.m_mtypes[MT_FREE]++; \
405 _mm->m_next = mmbfree; \
406 mmbfree = _mm; \
407 MMBWAKEUP(); \
408 )
409
410 /*
411 * Copy mbuf pkthdr from "from" to "to".
412 * from must have M_PKTHDR set, and to must be empty.
413 */
414 #define M_COPY_PKTHDR(to, from) do { \
415 struct mbuf *_mfrom = (from); \
416 struct mbuf *_mto = (to); \
417 \
418 _mto->m_data = _mto->m_pktdat; \
419 _mto->m_flags = _mfrom->m_flags & M_COPYFLAGS; \
420 _mto->m_pkthdr = _mfrom->m_pkthdr; \
421 } while (0)
422
423 /*
424 * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
425 * an object of the specified size at the end of the mbuf, longword aligned.
426 */
427 #define M_ALIGN(m, len) do { \
428 (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \
429 } while (0)
430
431 /*
432 * As above, for mbufs allocated with m_gethdr/MGETHDR
433 * or initialized by M_COPY_PKTHDR.
434 */
435 #define MH_ALIGN(m, len) do { \
436 (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \
437 } while (0)
438
439 /*
440 * Compute the amount of space available
441 * before the current start of data in an mbuf.
442 */
443 #define M_LEADINGSPACE(m) \
444 ((m)->m_flags & M_EXT ? \
445 /* (m)->m_data - (m)->m_ext.ext_buf */ 0 : \
446 (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \
447 (m)->m_data - (m)->m_dat)
448
449 /*
450 * Compute the amount of space available
451 * after the end of data in an mbuf.
452 */
453 #define M_TRAILINGSPACE(m) \
454 ((m)->m_flags & M_EXT ? (m)->m_ext.ext_buf + \
455 (m)->m_ext.ext_size - ((m)->m_data + (m)->m_len) : \
456 &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
457
458 /*
459 * Arrange to prepend space of size plen to mbuf m.
460 * If a new mbuf must be allocated, how specifies whether to wait.
461 * If how is M_DONTWAIT and allocation fails, the original mbuf chain
462 * is freed and m is set to NULL.
463 */
464 #define M_PREPEND(m, plen, how) do { \
465 struct mbuf **_mmp = &(m); \
466 struct mbuf *_mm = *_mmp; \
467 int _mplen = (plen); \
468 int __mhow = (how); \
469 \
470 if (M_LEADINGSPACE(_mm) >= _mplen) { \
471 _mm->m_data -= _mplen; \
472 _mm->m_len += _mplen; \
473 } else \
474 _mm = m_prepend(_mm, _mplen, __mhow); \
475 if (_mm != NULL && _mm->m_flags & M_PKTHDR) \
476 _mm->m_pkthdr.len += _mplen; \
477 *_mmp = _mm; \
478 } while (0)
479
480 /* change mbuf to new type */
481 #define MCHTYPE(m, t) do { \
482 struct mbuf *_mm = (m); \
483 int _mt = (t); \
484 int _ms = splimp(); \
485 \
486 mbstat.m_mtypes[_mm->m_type]--; \
487 mbstat.m_mtypes[_mt]++; \
488 splx(_ms); \
489 _mm->m_type = (_mt); \
490 } while (0)
491
492 /* length to m_copy to copy all */
493 #define M_COPYALL 1000000000
494
495 /* compatibility with 4.3 */
496 #define m_copy(m, o, l) m_copym((m), (o), (l), M_DONTWAIT)
497
498 #ifdef KERNEL
499 extern u_int m_clalloc_wid; /* mbuf cluster wait count */
500 extern u_int m_mballoc_wid; /* mbuf wait count */
501 extern int max_linkhdr; /* largest link-level header */
502 extern int max_protohdr; /* largest protocol header */
503 extern int max_hdr; /* largest link+protocol header */
504 extern int max_datalen; /* MHLEN - max_hdr */
505 extern struct mbstat mbstat;
506 extern int mbuf_wait; /* mbuf sleep time */
507 extern struct mbuf *mbutl; /* virtual address of mclusters */
508 extern char *mclrefcnt; /* cluster reference counts */
509 extern union mcluster *mclfree;
510 extern struct mbuf *mmbfree;
511 extern int nmbclusters;
512 extern int nmbufs;
513 extern int nsfbufs;
514
515 void m_adj __P((struct mbuf *, int));
516 void m_cat __P((struct mbuf *,struct mbuf *));
517 int m_clalloc __P((int, int));
518 caddr_t m_clalloc_wait __P((void));
519 void m_copyback __P((struct mbuf *, int, int, caddr_t));
520 void m_copydata __P((struct mbuf *,int,int,caddr_t));
521 struct mbuf *m_copym __P((struct mbuf *, int, int, int));
522 struct mbuf *m_copypacket __P((struct mbuf *, int));
523 struct mbuf *m_devget __P((char *, int, int, struct ifnet *,
524 void (*copy)(char *, caddr_t, u_int)));
525 struct mbuf *m_dup __P((struct mbuf *, int));
526 struct mbuf *m_free __P((struct mbuf *));
527 void m_freem __P((struct mbuf *));
528 struct mbuf *m_get __P((int, int));
529 struct mbuf *m_getclr __P((int, int));
530 struct mbuf *m_gethdr __P((int, int));
531 int m_mballoc __P((int, int));
532 struct mbuf *m_mballoc_wait __P((int, int));
533 struct mbuf *m_prepend __P((struct mbuf *,int,int));
534 void m_print __P((const struct mbuf *m));
535 struct mbuf *m_pullup __P((struct mbuf *, int));
536 struct mbuf *m_retry __P((int, int));
537 struct mbuf *m_retryhdr __P((int, int));
538 struct mbuf *m_split __P((struct mbuf *,int,int));
539 #endif /* KERNEL */
540
541 #endif /* !_SYS_MBUF_H_ */
Cache object: 809b26fcfe5cb85b61e6268d2646eeed
|