]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - share/man/man9/mbuf.9
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / share / man / man9 / mbuf.9
1 .\" Copyright (c) 2000 FreeBSD Inc.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED.  IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 .\" SUCH DAMAGE.
24 .\"
25 .\" $FreeBSD$
26 .\"
27 .Dd March 11, 2015
28 .Dt MBUF 9
29 .Os
30 .\"
31 .Sh NAME
32 .Nm mbuf
33 .Nd "memory management in the kernel IPC subsystem"
34 .\"
35 .Sh SYNOPSIS
36 .In sys/param.h
37 .In sys/systm.h
38 .In sys/mbuf.h
39 .\"
40 .Ss Mbuf allocation macros
41 .Fn MGET "struct mbuf *mbuf" "int how" "short type"
42 .Fn MGETHDR "struct mbuf *mbuf" "int how" "short type"
43 .Fn MCLGET "struct mbuf *mbuf" "int how"
44 .Fo MEXTADD
45 .Fa "struct mbuf *mbuf"
46 .Fa "caddr_t buf"
47 .Fa "u_int size"
48 .Fa "void (*free)(void *opt_arg1, void *opt_arg2)"
49 .Fa "void *opt_arg1"
50 .Fa "void *opt_arg2"
51 .Fa "short flags"
52 .Fa "int type"
53 .Fc
54 .Fn MEXTFREE "struct mbuf *mbuf"
55 .Fn MFREE "struct mbuf *mbuf" "struct mbuf *successor"
56 .\"
57 .Ss Mbuf utility macros
58 .Fn mtod "struct mbuf *mbuf" "type"
59 .Fn M_ALIGN "struct mbuf *mbuf" "u_int len"
60 .Fn MH_ALIGN "struct mbuf *mbuf" "u_int len"
61 .Ft int
62 .Fn M_LEADINGSPACE "struct mbuf *mbuf"
63 .Ft int
64 .Fn M_TRAILINGSPACE "struct mbuf *mbuf"
65 .Fn M_MOVE_PKTHDR "struct mbuf *to" "struct mbuf *from"
66 .Fn M_PREPEND "struct mbuf *mbuf" "int len" "int how"
67 .Fn MCHTYPE "struct mbuf *mbuf" "u_int type"
68 .Ft int
69 .Fn M_WRITABLE "struct mbuf *mbuf"
70 .\"
71 .Ss Mbuf allocation functions
72 .Ft struct mbuf *
73 .Fn m_get "int how" "int type"
74 .Ft struct mbuf *
75 .Fn m_getm "struct mbuf *orig" "int len" "int how" "int type"
76 .Ft struct mbuf *
77 .Fn m_getcl "int how" "short type" "int flags"
78 .Ft struct mbuf *
79 .Fn m_getclr "int how" "int type"
80 .Ft struct mbuf *
81 .Fn m_gethdr "int how" "int type"
82 .Ft struct mbuf *
83 .Fn m_free "struct mbuf *mbuf"
84 .Ft void
85 .Fn m_freem "struct mbuf *mbuf"
86 .\"
87 .Ss Mbuf utility functions
88 .Ft void
89 .Fn m_adj "struct mbuf *mbuf" "int len"
90 .Ft void
91 .Fn m_align "struct mbuf *mbuf" "int len"
92 .Ft int
93 .Fn m_append "struct mbuf *mbuf" "int len" "c_caddr_t cp"
94 .Ft struct mbuf *
95 .Fn m_prepend "struct mbuf *mbuf" "int len" "int how"
96 .Ft struct mbuf *
97 .Fn m_copyup "struct mbuf *mbuf" "int len" "int dstoff"
98 .Ft struct mbuf *
99 .Fn m_pullup "struct mbuf *mbuf" "int len"
100 .Ft struct mbuf *
101 .Fn m_pulldown "struct mbuf *mbuf" "int offset" "int len" "int *offsetp"
102 .Ft struct mbuf *
103 .Fn m_copym "struct mbuf *mbuf" "int offset" "int len" "int how"
104 .Ft struct mbuf *
105 .Fn m_copypacket "struct mbuf *mbuf" "int how"
106 .Ft struct mbuf *
107 .Fn m_dup "struct mbuf *mbuf" "int how"
108 .Ft void
109 .Fn m_copydata "const struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
110 .Ft void
111 .Fn m_copyback "struct mbuf *mbuf" "int offset" "int len" "caddr_t buf"
112 .Ft struct mbuf *
113 .Fo m_devget
114 .Fa "char *buf"
115 .Fa "int len"
116 .Fa "int offset"
117 .Fa "struct ifnet *ifp"
118 .Fa "void (*copy)(char *from, caddr_t to, u_int len)"
119 .Fc
120 .Ft void
121 .Fn m_cat "struct mbuf *m" "struct mbuf *n"
122 .Ft u_int
123 .Fn m_fixhdr "struct mbuf *mbuf"
124 .Ft void
125 .Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from"
126 .Ft void
127 .Fn m_move_pkthdr "struct mbuf *to" "struct mbuf *from"
128 .Ft u_int
129 .Fn m_length "struct mbuf *mbuf" "struct mbuf **last"
130 .Ft struct mbuf *
131 .Fn m_split "struct mbuf *mbuf" "int len" "int how"
132 .Ft int
133 .Fn m_apply "struct mbuf *mbuf" "int off" "int len" "int (*f)(void *arg, void *data, u_int len)" "void *arg"
134 .Ft struct mbuf *
135 .Fn m_getptr "struct mbuf *mbuf" "int loc" "int *off"
136 .Ft struct mbuf *
137 .Fn m_defrag "struct mbuf *m0" "int how"
138 .Ft struct mbuf *
139 .Fn m_collapse "struct mbuf *m0" "int how" "int maxfrags"
140 .Ft struct mbuf *
141 .Fn m_unshare "struct mbuf *m0" "int how"
142 .\"
143 .Sh DESCRIPTION
144 An
145 .Vt mbuf
146 is a basic unit of memory management in the kernel IPC subsystem.
147 Network packets and socket buffers are stored in
148 .Vt mbufs .
149 A network packet may span multiple
150 .Vt mbufs
151 arranged into a
152 .Vt mbuf chain
153 (linked list),
154 which allows adding or trimming
155 network headers with little overhead.
156 .Pp
157 While a developer should not bother with
158 .Vt mbuf
159 internals without serious
160 reason in order to avoid incompatibilities with future changes, it
161 is useful to understand the general structure of an
162 .Vt mbuf .
163 .Pp
164 An
165 .Vt mbuf
166 consists of a variable-sized header and a small internal
167 buffer for data.
168 The total size of an
169 .Vt mbuf ,
170 .Dv MSIZE ,
171 is a constant defined in
172 .In sys/param.h .
173 The
174 .Vt mbuf
175 header includes:
176 .Bl -tag -width "m_nextpkt" -offset indent
177 .It Va m_next
178 .Pq Vt struct mbuf *
179 A pointer to the next
180 .Vt mbuf
181 in the
182 .Vt mbuf chain .
183 .It Va m_nextpkt
184 .Pq Vt struct mbuf *
185 A pointer to the next
186 .Vt mbuf chain
187 in the queue.
188 .It Va m_data
189 .Pq Vt caddr_t
190 A pointer to data attached to this
191 .Vt mbuf .
192 .It Va m_len
193 .Pq Vt int
194 The length of the data.
195 .It Va m_type
196 .Pq Vt short
197 The type of the data.
198 .It Va m_flags
199 .Pq Vt int
200 The
201 .Vt mbuf
202 flags.
203 .El
204 .Pp
205 The
206 .Vt mbuf
207 flag bits are defined as follows:
208 .Bd -literal
209 /* mbuf flags */
210 #define M_EXT           0x0001  /* has associated external storage */
211 #define M_PKTHDR        0x0002  /* start of record */
212 #define M_EOR           0x0004  /* end of record */
213 #define M_RDONLY        0x0008  /* associated data marked read-only */
214 #define M_PROTO1        0x0010  /* protocol-specific */
215 #define M_PROTO2        0x0020  /* protocol-specific */
216 #define M_PROTO3        0x0040  /* protocol-specific */
217 #define M_PROTO4        0x0080  /* protocol-specific */
218 #define M_PROTO5        0x0100  /* protocol-specific */
219 #define M_PROTO6        0x4000  /* protocol-specific (avoid M_BCAST conflict) */
220 #define M_FREELIST      0x8000  /* mbuf is on the free list */
221
222 /* mbuf pkthdr flags (also stored in m_flags) */
223 #define M_BCAST         0x0200  /* send/received as link-level broadcast */
224 #define M_MCAST         0x0400  /* send/received as link-level multicast */
225 #define M_FRAG          0x0800  /* packet is fragment of larger packet */
226 #define M_FIRSTFRAG     0x1000  /* packet is first fragment */
227 #define M_LASTFRAG      0x2000  /* packet is last fragment */
228 .Ed
229 .Pp
230 The available
231 .Vt mbuf
232 types are defined as follows:
233 .Bd -literal
234 /* mbuf types */
235 #define MT_DATA         1       /* dynamic (data) allocation */
236 #define MT_HEADER       MT_DATA /* packet header */
237 #define MT_SONAME       8       /* socket name */
238 #define MT_CONTROL      14      /* extra-data protocol message */
239 #define MT_OOBDATA      15      /* expedited data */
240 .Ed
241 .Pp
242 The available external buffer types are defined as follows:
243 .Bd -literal
244 /* external buffer types */
245 #define EXT_CLUSTER     1       /* mbuf cluster */
246 #define EXT_SFBUF       2       /* sendfile(2)'s sf_bufs */
247 #define EXT_JUMBOP      3       /* jumbo cluster 4096 bytes */
248 #define EXT_JUMBO9      4       /* jumbo cluster 9216 bytes */
249 #define EXT_JUMBO16     5       /* jumbo cluster 16184 bytes */
250 #define EXT_PACKET      6       /* mbuf+cluster from packet zone */
251 #define EXT_MBUF        7       /* external mbuf reference (M_IOVEC) */
252 #define EXT_NET_DRV     100     /* custom ext_buf provided by net driver(s) */
253 #define EXT_MOD_TYPE    200     /* custom module's ext_buf type */
254 #define EXT_DISPOSABLE  300     /* can throw this buffer away w/page flipping */
255 #define EXT_EXTREF      400     /* has externally maintained ref_cnt ptr */
256 .Ed
257 .Pp
258 If the
259 .Dv M_PKTHDR
260 flag is set, a
261 .Vt struct pkthdr Va m_pkthdr
262 is added to the
263 .Vt mbuf
264 header.
265 It contains a pointer to the interface
266 the packet has been received from
267 .Pq Vt struct ifnet Va *rcvif ,
268 and the total packet length
269 .Pq Vt int Va len .
270 Optionally, it may also contain an attached list of packet tags
271 .Pq Vt "struct m_tag" .
272 See
273 .Xr mbuf_tags 9
274 for details.
275 Fields used in offloading checksum calculation to the hardware are kept in
276 .Va m_pkthdr
277 as well.
278 See
279 .Sx HARDWARE-ASSISTED CHECKSUM CALCULATION
280 for details.
281 .Pp
282 If small enough, data is stored in the internal data buffer of an
283 .Vt mbuf .
284 If the data is sufficiently large, another
285 .Vt mbuf
286 may be added to the
287 .Vt mbuf chain ,
288 or external storage may be associated with the
289 .Vt mbuf .
290 .Dv MHLEN
291 bytes of data can fit into an
292 .Vt mbuf
293 with the
294 .Dv M_PKTHDR
295 flag set,
296 .Dv MLEN
297 bytes can otherwise.
298 .Pp
299 If external storage is being associated with an
300 .Vt mbuf ,
301 the
302 .Va m_ext
303 header is added at the cost of losing the internal data buffer.
304 It includes a pointer to external storage, the size of the storage,
305 a pointer to a function used for freeing the storage,
306 a pointer to an optional argument that can be passed to the function,
307 and a pointer to a reference counter.
308 An
309 .Vt mbuf
310 using external storage has the
311 .Dv M_EXT
312 flag set.
313 .Pp
314 The system supplies a macro for allocating the desired external storage
315 buffer,
316 .Dv MEXTADD .
317 .Pp
318 The allocation and management of the reference counter is handled by the
319 subsystem.
320 .Pp
321 The system also supplies a default type of external storage buffer called an
322 .Vt mbuf cluster .
323 .Vt Mbuf clusters
324 can be allocated and configured with the use of the
325 .Dv MCLGET
326 macro.
327 Each
328 .Vt mbuf cluster
329 is
330 .Dv MCLBYTES
331 in size, where MCLBYTES is a machine-dependent constant.
332 The system defines an advisory macro
333 .Dv MINCLSIZE ,
334 which is the smallest amount of data to put into an
335 .Vt mbuf cluster .
336 It is equal to
337 .Dv MHLEN
338 plus one.
339 It is typically preferable to store data into the data region of an
340 .Vt mbuf ,
341 if size permits, as opposed to allocating a separate
342 .Vt mbuf cluster
343 to hold the same data.
344 .\"
345 .Ss Macros and Functions
346 There are numerous predefined macros and functions that provide the
347 developer with common utilities.
348 .\"
349 .Bl -ohang -offset indent
350 .It Fn mtod mbuf type
351 Convert an
352 .Fa mbuf
353 pointer to a data pointer.
354 The macro expands to the data pointer cast to the pointer of the specified
355 .Fa type .
356 .Sy Note :
357 It is advisable to ensure that there is enough contiguous data in
358 .Fa mbuf .
359 See
360 .Fn m_pullup
361 for details.
362 .It Fn MGET mbuf how type
363 Allocate an
364 .Vt mbuf
365 and initialize it to contain internal data.
366 .Fa mbuf
367 will point to the allocated
368 .Vt mbuf
369 on success, or be set to
370 .Dv NULL
371 on failure.
372 The
373 .Fa how
374 argument is to be set to
375 .Dv M_WAITOK
376 or
377 .Dv M_NOWAIT .
378 It specifies whether the caller is willing to block if necessary.
379 A number of other functions and macros related to
380 .Vt mbufs
381 have the same argument because they may
382 at some point need to allocate new
383 .Vt mbufs .
384 .Pp
385 Historical
386 .Vt mbuf
387 allocator (See
388 .Sx HISTORY
389 section) used allocation flags
390 .Dv M_WAIT
391 and
392 .Dv M_DONTWAIT .
393 These constants are kept for compatibility
394 and their use in new code is discouraged.
395 .It Fn MGETHDR mbuf how type
396 Allocate an
397 .Vt mbuf
398 and initialize it to contain a packet header
399 and internal data.
400 See
401 .Fn MGET
402 for details.
403 .It Fn MEXTADD mbuf buf size free opt_arg1 opt_arg2 flags type
404 Associate externally managed data with
405 .Fa mbuf .
406 Any internal data contained in the mbuf will be discarded, and the
407 .Dv M_EXT
408 flag will be set.
409 The
410 .Fa buf
411 and
412 .Fa size
413 arguments are the address and length, respectively, of the data.
414 The
415 .Fa free
416 argument points to a function which will be called to free the data
417 when the mbuf is freed; it is only used if
418 .Fa type
419 is
420 .Dv EXT_EXTREF .
421 The
422 .Fa opt_arg1
423 and
424 .Fa opt_arg2
425 arguments will be passed unmodified to
426 .Fa free .
427 The
428 .Fa flags
429 argument specifies additional
430 .Vt mbuf
431 flags; it is not necessary to specify
432 .Dv M_EXT .
433 Finally, the
434 .Fa type
435 argument specifies the type of external data, which controls how it
436 will be disposed of when the
437 .Vt mbuf
438 is freed.
439 In most cases, the correct value is
440 .Dv EXT_EXTREF .
441 .It Fn MCLGET mbuf how
442 Allocate and attach an
443 .Vt mbuf cluster
444 to
445 .Fa mbuf .
446 If the macro fails, the
447 .Dv M_EXT
448 flag will not be set in
449 .Fa mbuf .
450 .It Fn M_ALIGN mbuf len
451 Set the pointer
452 .Fa mbuf->m_data
453 to place an object of the size
454 .Fa len
455 at the end of the internal data area of
456 .Fa mbuf ,
457 long word aligned.
458 Applicable only if
459 .Fa mbuf
460 is newly allocated with
461 .Fn MGET
462 or
463 .Fn m_get .
464 .It Fn MH_ALIGN mbuf len
465 Serves the same purpose as
466 .Fn M_ALIGN
467 does, but only for
468 .Fa mbuf
469 newly allocated with
470 .Fn MGETHDR
471 or
472 .Fn m_gethdr ,
473 or initialized by
474 .Fn m_dup_pkthdr
475 or
476 .Fn m_move_pkthdr .
477 .It Fn m_align mbuf len
478 Services the same purpose as
479 .Fn M_ALIGN
480 but handles any type of mbuf.
481 .It Fn M_LEADINGSPACE mbuf
482 Returns the number of bytes available before the beginning
483 of data in
484 .Fa mbuf .
485 .It Fn M_TRAILINGSPACE mbuf
486 Returns the number of bytes available after the end of data in
487 .Fa mbuf .
488 .It Fn M_PREPEND mbuf len how
489 This macro operates on an
490 .Vt mbuf chain .
491 It is an optimized wrapper for
492 .Fn m_prepend
493 that can make use of possible empty space before data
494 (e.g.\& left after trimming of a link-layer header).
495 The new
496 .Vt mbuf chain
497 pointer or
498 .Dv NULL
499 is in
500 .Fa mbuf
501 after the call.
502 .It Fn M_MOVE_PKTHDR to from
503 Using this macro is equivalent to calling
504 .Fn m_move_pkthdr to from .
505 .It Fn M_WRITABLE mbuf
506 This macro will evaluate true if
507 .Fa mbuf
508 is not marked
509 .Dv M_RDONLY
510 and if either
511 .Fa mbuf
512 does not contain external storage or,
513 if it does,
514 then if the reference count of the storage is not greater than 1.
515 The
516 .Dv M_RDONLY
517 flag can be set in
518 .Fa mbuf->m_flags .
519 This can be achieved during setup of the external storage,
520 by passing the
521 .Dv M_RDONLY
522 bit as a
523 .Fa flags
524 argument to the
525 .Fn MEXTADD
526 macro, or can be directly set in individual
527 .Vt mbufs .
528 .It Fn MCHTYPE mbuf type
529 Change the type of
530 .Fa mbuf
531 to
532 .Fa type .
533 This is a relatively expensive operation and should be avoided.
534 .El
535 .Pp
536 The functions are:
537 .Bl -ohang -offset indent
538 .It Fn m_get how type
539 A function version of
540 .Fn MGET
541 for non-critical paths.
542 .It Fn m_getm orig len how type
543 Allocate
544 .Fa len
545 bytes worth of
546 .Vt mbufs
547 and
548 .Vt mbuf clusters
549 if necessary and append the resulting allocated
550 .Vt mbuf chain
551 to the
552 .Vt mbuf chain
553 .Fa orig ,
554 if it is
555 .No non- Ns Dv NULL .
556 If the allocation fails at any point,
557 free whatever was allocated and return
558 .Dv NULL .
559 If
560 .Fa orig
561 is
562 .No non- Ns Dv NULL ,
563 it will not be freed.
564 It is possible to use
565 .Fn m_getm
566 to either append
567 .Fa len
568 bytes to an existing
569 .Vt mbuf
570 or
571 .Vt mbuf chain
572 (for example, one which may be sitting in a pre-allocated ring)
573 or to simply perform an all-or-nothing
574 .Vt mbuf
575 and
576 .Vt mbuf cluster
577 allocation.
578 .It Fn m_gethdr how type
579 A function version of
580 .Fn MGETHDR
581 for non-critical paths.
582 .It Fn m_getcl how type flags
583 Fetch an
584 .Vt mbuf
585 with a
586 .Vt mbuf cluster
587 attached to it.
588 If one of the allocations fails, the entire allocation fails.
589 This routine is the preferred way of fetching both the
590 .Vt mbuf
591 and
592 .Vt mbuf cluster
593 together, as it avoids having to unlock/relock between allocations.
594 Returns
595 .Dv NULL
596 on failure.
597 .It Fn m_getclr how type
598 Allocate an
599 .Vt mbuf
600 and zero out the data region.
601 .It Fn m_free mbuf
602 Frees
603 .Vt mbuf .
604 Returns
605 .Va m_next
606 of the freed
607 .Vt mbuf .
608 .El
609 .Pp
610 The functions below operate on
611 .Vt mbuf chains .
612 .Bl -ohang -offset indent
613 .It Fn m_freem mbuf
614 Free an entire
615 .Vt mbuf chain ,
616 including any external storage.
617 .\"
618 .It Fn m_adj mbuf len
619 Trim
620 .Fa len
621 bytes from the head of an
622 .Vt mbuf chain
623 if
624 .Fa len
625 is positive, from the tail otherwise.
626 .\"
627 .It Fn m_append mbuf len cp
628 Append
629 .Vt len
630 bytes of data
631 .Vt cp
632 to the
633 .Vt mbuf chain .
634 Extend the mbuf chain if the new data does not fit in
635 existing space.
636 .\"
637 .It Fn m_prepend mbuf len how
638 Allocate a new
639 .Vt mbuf
640 and prepend it to the
641 .Vt mbuf chain ,
642 handle
643 .Dv M_PKTHDR
644 properly.
645 .Sy Note :
646 It does not allocate any
647 .Vt mbuf clusters ,
648 so
649 .Fa len
650 must be less than
651 .Dv MLEN
652 or
653 .Dv MHLEN ,
654 depending on the
655 .Dv M_PKTHDR
656 flag setting.
657 .\"
658 .It Fn m_copyup mbuf len dstoff
659 Similar to
660 .Fn m_pullup
661 but copies
662 .Fa len
663 bytes of data into a new mbuf at
664 .Fa dstoff
665 bytes into the mbuf.
666 The
667 .Fa dstoff
668 argument aligns the data and leaves room for a link layer header.
669 Returns the new
670 .Vt mbuf chain
671 on success,
672 and frees the
673 .Vt mbuf chain
674 and returns
675 .Dv NULL
676 on failure.
677 .Sy Note :
678 The function does not allocate
679 .Vt mbuf clusters ,
680 so
681 .Fa len + dstoff
682 must be less than
683 .Dv MHLEN .
684 .\"
685 .It Fn m_pullup mbuf len
686 Arrange that the first
687 .Fa len
688 bytes of an
689 .Vt mbuf chain
690 are contiguous and lay in the data area of
691 .Fa mbuf ,
692 so they are accessible with
693 .Fn mtod mbuf type .
694 It is important to remember that this may involve
695 reallocating some mbufs and moving data so all pointers
696 referencing data within the old mbuf chain
697 must be recalculated or made invalid.
698 Return the new
699 .Vt mbuf chain
700 on success,
701 .Dv NULL
702 on failure
703 (the
704 .Vt mbuf chain
705 is freed in this case).
706 .Sy Note :
707 It does not allocate any
708 .Vt mbuf clusters ,
709 so
710 .Fa len
711 must be less than or equal to
712 .Dv MHLEN .
713 .\"
714 .It Fn m_pulldown mbuf offset len offsetp
715 Arrange that
716 .Fa len
717 bytes between
718 .Fa offset
719 and
720 .Fa offset + len
721 in the
722 .Vt mbuf chain
723 are contiguous and lay in the data area of
724 .Fa mbuf ,
725 so they are accessible with
726 .Fn mtod mbuf type .
727 .Fa len
728 must be smaller than, or equal to, the size of an
729 .Vt mbuf cluster .
730 Return a pointer to an intermediate
731 .Vt mbuf
732 in the chain containing the requested region;
733 the offset in the data region of the
734 .Vt mbuf chain
735 to the data contained in the returned mbuf is stored in
736 .Fa *offsetp .
737 If
738 .Fa offp
739 is NULL, the region may be accessed using
740 .Fn mtod mbuf type .
741 If
742 .Fa offp
743 is non-NULL, the region may be accessed using
744 .Fn mtod mbuf uint8_t + *offsetp .
745 The region of the mbuf chain between its beginning and
746 .Fa off
747 is not modified, therefore it is safe to hold pointers to data within
748 this region before calling
749 .Fn m_pulldown .
750 .\"
751 .It Fn m_copym mbuf offset len how
752 Make a copy of an
753 .Vt mbuf chain
754 starting
755 .Fa offset
756 bytes from the beginning, continuing for
757 .Fa len
758 bytes.
759 If
760 .Fa len
761 is
762 .Dv M_COPYALL ,
763 copy to the end of the
764 .Vt mbuf chain .
765 .Sy Note :
766 The copy is read-only, because the
767 .Vt mbuf clusters
768 are not copied, only their reference counts are incremented.
769 .\"
770 .It Fn m_copypacket mbuf how
771 Copy an entire packet including header, which must be present.
772 This is an optimized version of the common case
773 .Fn m_copym mbuf 0 M_COPYALL how .
774 .Sy Note :
775 the copy is read-only, because the
776 .Vt mbuf clusters
777 are not copied, only their reference counts are incremented.
778 .\"
779 .It Fn m_dup mbuf how
780 Copy a packet header
781 .Vt mbuf chain
782 into a completely new
783 .Vt mbuf chain ,
784 including copying any
785 .Vt mbuf clusters .
786 Use this instead of
787 .Fn m_copypacket
788 when you need a writable copy of an
789 .Vt mbuf chain .
790 .\"
791 .It Fn m_copydata mbuf offset len buf
792 Copy data from an
793 .Vt mbuf chain
794 starting
795 .Fa off
796 bytes from the beginning, continuing for
797 .Fa len
798 bytes, into the indicated buffer
799 .Fa buf .
800 .\"
801 .It Fn m_copyback mbuf offset len buf
802 Copy
803 .Fa len
804 bytes from the buffer
805 .Fa buf
806 back into the indicated
807 .Vt mbuf chain ,
808 starting at
809 .Fa offset
810 bytes from the beginning of the
811 .Vt mbuf chain ,
812 extending the
813 .Vt mbuf chain
814 if necessary.
815 .Sy Note :
816 It does not allocate any
817 .Vt mbuf clusters ,
818 just adds
819 .Vt mbufs
820 to the
821 .Vt mbuf chain .
822 It is safe to set
823 .Fa offset
824 beyond the current
825 .Vt mbuf chain
826 end: zeroed
827 .Vt mbufs
828 will be allocated to fill the space.
829 .\"
830 .It Fn m_length mbuf last
831 Return the length of the
832 .Vt mbuf chain ,
833 and optionally a pointer to the last
834 .Vt mbuf .
835 .\"
836 .It Fn m_dup_pkthdr to from how
837 Upon the function's completion, the
838 .Vt mbuf
839 .Fa to
840 will contain an identical copy of
841 .Fa from->m_pkthdr
842 and the per-packet attributes found in the
843 .Vt mbuf chain
844 .Fa from .
845 The
846 .Vt mbuf
847 .Fa from
848 must have the flag
849 .Dv M_PKTHDR
850 initially set, and
851 .Fa to
852 must be empty on entry.
853 .\"
854 .It Fn m_move_pkthdr to from
855 Move
856 .Va m_pkthdr
857 and the per-packet attributes from the
858 .Vt mbuf chain
859 .Fa from
860 to the
861 .Vt mbuf
862 .Fa to .
863 The
864 .Vt mbuf
865 .Fa from
866 must have the flag
867 .Dv M_PKTHDR
868 initially set, and
869 .Fa to
870 must be empty on entry.
871 Upon the function's completion,
872 .Fa from
873 will have the flag
874 .Dv M_PKTHDR
875 and the per-packet attributes cleared.
876 .\"
877 .It Fn m_fixhdr mbuf
878 Set the packet-header length to the length of the
879 .Vt mbuf chain .
880 .\"
881 .It Fn m_devget buf len offset ifp copy
882 Copy data from a device local memory pointed to by
883 .Fa buf
884 to an
885 .Vt mbuf chain .
886 The copy is done using a specified copy routine
887 .Fa copy ,
888 or
889 .Fn bcopy
890 if
891 .Fa copy
892 is
893 .Dv NULL .
894 .\"
895 .It Fn m_cat m n
896 Concatenate
897 .Fa n
898 to
899 .Fa m .
900 Both
901 .Vt mbuf chains
902 must be of the same type.
903 .Fa N
904 is still valid after the function returned.
905 .Sy Note :
906 It does not handle
907 .Dv M_PKTHDR
908 and friends.
909 .\"
910 .It Fn m_split mbuf len how
911 Partition an
912 .Vt mbuf chain
913 in two pieces, returning the tail:
914 all but the first
915 .Fa len
916 bytes.
917 In case of failure, it returns
918 .Dv NULL
919 and attempts to restore the
920 .Vt mbuf chain
921 to its original state.
922 .\"
923 .It Fn m_apply mbuf off len f arg
924 Apply a function to an
925 .Vt mbuf chain ,
926 at offset
927 .Fa off ,
928 for length
929 .Fa len
930 bytes.
931 Typically used to avoid calls to
932 .Fn m_pullup
933 which would otherwise be unnecessary or undesirable.
934 .Fa arg
935 is a convenience argument which is passed to the callback function
936 .Fa f .
937 .Pp
938 Each time
939 .Fn f
940 is called, it will be passed
941 .Fa arg ,
942 a pointer to the
943 .Fa data
944 in the current mbuf, and the length
945 .Fa len
946 of the data in this mbuf to which the function should be applied.
947 .Pp
948 The function should return zero to indicate success;
949 otherwise, if an error is indicated, then
950 .Fn m_apply
951 will return the error and stop iterating through the
952 .Vt mbuf chain .
953 .\"
954 .It Fn m_getptr mbuf loc off
955 Return a pointer to the mbuf containing the data located at
956 .Fa loc
957 bytes from the beginning of the
958 .Vt mbuf chain .
959 The corresponding offset into the mbuf will be stored in
960 .Fa *off .
961 .It Fn m_defrag m0 how
962 Defragment an mbuf chain, returning the shortest possible
963 chain of mbufs and clusters.
964 If allocation fails and this can not be completed,
965 .Dv NULL
966 will be returned and the original chain will be unchanged.
967 Upon success, the original chain will be freed and the new
968 chain will be returned.
969 .Fa how
970 should be either
971 .Dv M_WAITOK
972 or
973 .Dv M_NOWAIT ,
974 depending on the caller's preference.
975 .Pp
976 This function is especially useful in network drivers, where
977 certain long mbuf chains must be shortened before being added
978 to TX descriptor lists.
979 .It Fn m_collapse m0 how maxfrags
980 Defragment an mbuf chain, returning a chain of at most
981 .Fa maxfrags
982 mbufs and clusters.
983 If allocation fails or the chain cannot be collapsed as requested,
984 .Dv NULL
985 will be returned, with the original chain possibly modified.
986 As with
987 .Fn m_defrag ,
988 .Fa how
989 should be one of
990 .Dv M_WAITOK
991 or
992 .Dv M_NOWAIT .
993 .It Fn m_unshare m0 how
994 Create a version of the specified mbuf chain whose
995 contents can be safely modified without affecting other users.
996 If allocation fails and this operation can not be completed,
997 .Dv NULL
998 will be returned.
999 The original mbuf chain is always reclaimed and the reference
1000 count of any shared mbuf clusters is decremented.
1001 .Fa how
1002 should be either
1003 .Dv M_WAITOK
1004 or
1005 .Dv M_NOWAIT ,
1006 depending on the caller's preference.
1007 As a side-effect of this process the returned
1008 mbuf chain may be compacted.
1009 .Pp
1010 This function is especially useful in the transmit path of
1011 network code, when data must be encrypted or otherwise
1012 altered prior to transmission.
1013 .El
1014 .Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
1015 This section currently applies to TCP/IP only.
1016 In order to save the host CPU resources, computing checksums is
1017 offloaded to the network interface hardware if possible.
1018 The
1019 .Va m_pkthdr
1020 member of the leading
1021 .Vt mbuf
1022 of a packet contains two fields used for that purpose,
1023 .Vt int Va csum_flags
1024 and
1025 .Vt int Va csum_data .
1026 The meaning of those fields depends on the direction a packet flows in,
1027 and on whether the packet is fragmented.
1028 Henceforth,
1029 .Va csum_flags
1030 or
1031 .Va csum_data
1032 of a packet
1033 will denote the corresponding field of the
1034 .Va m_pkthdr
1035 member of the leading
1036 .Vt mbuf
1037 in the
1038 .Vt mbuf chain
1039 containing the packet.
1040 .Pp
1041 On output, checksum offloading is attempted after the outgoing
1042 interface has been determined for a packet.
1043 The interface-specific field
1044 .Va ifnet.if_data.ifi_hwassist
1045 (see
1046 .Xr ifnet 9 )
1047 is consulted for the capabilities of the interface to assist in
1048 computing checksums.
1049 The
1050 .Va csum_flags
1051 field of the packet header is set to indicate which actions the interface
1052 is supposed to perform on it.
1053 The actions unsupported by the network interface are done in the
1054 software prior to passing the packet down to the interface driver;
1055 such actions will never be requested through
1056 .Va csum_flags .
1057 .Pp
1058 The flags demanding a particular action from an interface are as follows:
1059 .Bl -tag -width ".Dv CSUM_TCP" -offset indent
1060 .It Dv CSUM_IP
1061 The IP header checksum is to be computed and stored in the
1062 corresponding field of the packet.
1063 The hardware is expected to know the format of an IP header
1064 to determine the offset of the IP checksum field.
1065 .It Dv CSUM_TCP
1066 The TCP checksum is to be computed.
1067 (See below.)
1068 .It Dv CSUM_UDP
1069 The UDP checksum is to be computed.
1070 (See below.)
1071 .El
1072 .Pp
1073 Should a TCP or UDP checksum be offloaded to the hardware,
1074 the field
1075 .Va csum_data
1076 will contain the byte offset of the checksum field relative to the
1077 end of the IP header.
1078 In this case, the checksum field will be initially
1079 set by the TCP/IP module to the checksum of the pseudo header
1080 defined by the TCP and UDP specifications.
1081 .Pp
1082 On input, an interface indicates the actions it has performed
1083 on a packet by setting one or more of the following flags in
1084 .Va csum_flags
1085 associated with the packet:
1086 .Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
1087 .It Dv CSUM_IP_CHECKED
1088 The IP header checksum has been computed.
1089 .It Dv CSUM_IP_VALID
1090 The IP header has a valid checksum.
1091 This flag can appear only in combination with
1092 .Dv CSUM_IP_CHECKED .
1093 .It Dv CSUM_DATA_VALID
1094 The checksum of the data portion of the IP packet has been computed
1095 and stored in the field
1096 .Va csum_data
1097 in network byte order.
1098 .It Dv CSUM_PSEUDO_HDR
1099 Can be set only along with
1100 .Dv CSUM_DATA_VALID
1101 to indicate that the IP data checksum found in
1102 .Va csum_data
1103 allows for the pseudo header defined by the TCP and UDP specifications.
1104 Otherwise the checksum of the pseudo header must be calculated by
1105 the host CPU and added to
1106 .Va csum_data
1107 to obtain the final checksum to be used for TCP or UDP validation purposes.
1108 .El
1109 .Pp
1110 If a particular network interface just indicates success or
1111 failure of TCP or UDP checksum validation without returning
1112 the exact value of the checksum to the host CPU, its driver can mark
1113 .Dv CSUM_DATA_VALID
1114 and
1115 .Dv CSUM_PSEUDO_HDR
1116 in
1117 .Va csum_flags ,
1118 and set
1119 .Va csum_data
1120 to
1121 .Li 0xFFFF
1122 hexadecimal to indicate a valid checksum.
1123 It is a peculiarity of the algorithm used that the Internet checksum
1124 calculated over any valid packet will be
1125 .Li 0xFFFF
1126 as long as the original checksum field is included.
1127 .Sh STRESS TESTING
1128 When running a kernel compiled with the option
1129 .Dv MBUF_STRESS_TEST ,
1130 the following
1131 .Xr sysctl 8 Ns
1132 -controlled options may be used to create
1133 various failure/extreme cases for testing of network drivers
1134 and other parts of the kernel that rely on
1135 .Vt mbufs .
1136 .Bl -tag -width ident
1137 .It Va net.inet.ip.mbuf_frag_size
1138 Causes
1139 .Fn ip_output
1140 to fragment outgoing
1141 .Vt mbuf chains
1142 into fragments of the specified size.
1143 Setting this variable to 1 is an excellent way to
1144 test the long
1145 .Vt mbuf chain
1146 handling ability of network drivers.
1147 .It Va kern.ipc.m_defragrandomfailures
1148 Causes the function
1149 .Fn m_defrag
1150 to randomly fail, returning
1151 .Dv NULL .
1152 Any piece of code which uses
1153 .Fn m_defrag
1154 should be tested with this feature.
1155 .El
1156 .Sh RETURN VALUES
1157 See above.
1158 .Sh SEE ALSO
1159 .Xr ifnet 9 ,
1160 .Xr mbuf_tags 9
1161 .Sh HISTORY
1162 .\" Please correct me if I'm wrong
1163 .Vt Mbufs
1164 appeared in an early version of
1165 .Bx .
1166 Besides being used for network packets, they were used
1167 to store various dynamic structures, such as routing table
1168 entries, interface addresses, protocol control blocks, etc.
1169 In more recent
1170 .Fx
1171 use of
1172 .Vt mbufs
1173 is almost entirely limited to packet storage, with
1174 .Xr uma 9
1175 zones being used directly to store other network-related memory.
1176 .Pp
1177 Historically, the
1178 .Vt mbuf
1179 allocator has been a special-purpose memory allocator able to run in
1180 interrupt contexts and allocating from a special kernel address space map.
1181 As of
1182 .Fx 5.3 ,
1183 the
1184 .Vt mbuf
1185 allocator is a wrapper around
1186 .Xr uma 9 ,
1187 allowing caching of
1188 .Vt mbufs ,
1189 clusters, and
1190 .Vt mbuf
1191 + cluster pairs in per-CPU caches, as well as bringing other benefits of
1192 slab allocation.
1193 .Sh AUTHORS
1194 The original
1195 .Nm
1196 manual page was written by Yar Tikhiy.
1197 The
1198 .Xr uma 9
1199 .Vt mbuf
1200 allocator was written by Bosko Milekic.