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