]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/mbuf.9
Import libxo-0.7.2; add xo_options.7.
[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 October 10, 2016
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 "caddr_t buf"
48 .Fa "u_int size"
49 .Fa "void (*free)(void *opt_arg1, void *opt_arg2)"
50 .Fa "void *opt_arg1"
51 .Fa "void *opt_arg2"
52 .Fa "short 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 passed unmodified to
420 .Fa free .
421 The
422 .Fa flags
423 argument specifies additional
424 .Vt mbuf
425 flags; it is not necessary to specify
426 .Dv M_EXT .
427 Finally, the
428 .Fa type
429 argument specifies the type of external data, which controls how it
430 will be disposed of when the
431 .Vt mbuf
432 is freed.
433 In most cases, the correct value is
434 .Dv EXT_EXTREF .
435 .It Fn MCLGET mbuf how
436 Allocate and attach an
437 .Vt mbuf cluster
438 to
439 .Fa mbuf .
440 On success, a non-zero value returned; otherwise, 0.
441 Historically, consumers would check for success by testing the
442 .Dv M_EXT
443 flag on the mbuf, but this is now discouraged to avoid unnecessary awareness
444 of the implementation of external storage in protocol stacks and device
445 drivers.
446 .It Fn M_ALIGN mbuf len
447 Set the pointer
448 .Fa mbuf->m_data
449 to place an object of the size
450 .Fa len
451 at the end of the internal data area of
452 .Fa mbuf ,
453 long word aligned.
454 Applicable only if
455 .Fa mbuf
456 is newly allocated with
457 .Fn MGET
458 or
459 .Fn m_get .
460 .It Fn MH_ALIGN mbuf len
461 Serves the same purpose as
462 .Fn M_ALIGN
463 does, but only for
464 .Fa mbuf
465 newly allocated with
466 .Fn MGETHDR
467 or
468 .Fn m_gethdr ,
469 or initialized by
470 .Fn m_dup_pkthdr
471 or
472 .Fn m_move_pkthdr .
473 .It Fn m_align mbuf len
474 Services the same purpose as
475 .Fn M_ALIGN
476 but handles any type of mbuf.
477 .It Fn M_LEADINGSPACE mbuf
478 Returns the number of bytes available before the beginning
479 of data in
480 .Fa mbuf .
481 .It Fn M_TRAILINGSPACE mbuf
482 Returns the number of bytes available after the end of data in
483 .Fa mbuf .
484 .It Fn M_PREPEND mbuf len how
485 This macro operates on an
486 .Vt mbuf chain .
487 It is an optimized wrapper for
488 .Fn m_prepend
489 that can make use of possible empty space before data
490 (e.g.\& left after trimming of a link-layer header).
491 The new
492 .Vt mbuf chain
493 pointer or
494 .Dv NULL
495 is in
496 .Fa mbuf
497 after the call.
498 .It Fn M_MOVE_PKTHDR to from
499 Using this macro is equivalent to calling
500 .Fn m_move_pkthdr to from .
501 .It Fn M_WRITABLE mbuf
502 This macro will evaluate true if
503 .Fa mbuf
504 is not marked
505 .Dv M_RDONLY
506 and if either
507 .Fa mbuf
508 does not contain external storage or,
509 if it does,
510 then if the reference count of the storage is not greater than 1.
511 The
512 .Dv M_RDONLY
513 flag can be set in
514 .Fa mbuf->m_flags .
515 This can be achieved during setup of the external storage,
516 by passing the
517 .Dv M_RDONLY
518 bit as a
519 .Fa flags
520 argument to the
521 .Fn MEXTADD
522 macro, or can be directly set in individual
523 .Vt mbufs .
524 .It Fn MCHTYPE mbuf type
525 Change the type of
526 .Fa mbuf
527 to
528 .Fa type .
529 This is a relatively expensive operation and should be avoided.
530 .El
531 .Pp
532 The functions are:
533 .Bl -ohang -offset indent
534 .It Fn m_get how type
535 A function version of
536 .Fn MGET
537 for non-critical paths.
538 .It Fn m_get2 size how type flags
539 Allocate an
540 .Vt mbuf
541 with enough space to hold specified amount of data.
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_getjcl how type flags size
598 This is like
599 .Fn m_getcl
600 but it the size of the cluster allocated will be large enough for
601 .Fa size
602 bytes.
603 .It Fn m_free mbuf
604 Frees
605 .Vt mbuf .
606 Returns
607 .Va m_next
608 of the freed
609 .Vt mbuf .
610 .El
611 .Pp
612 The functions below operate on
613 .Vt mbuf chains .
614 .Bl -ohang -offset indent
615 .It Fn m_freem mbuf
616 Free an entire
617 .Vt mbuf chain ,
618 including any external storage.
619 .\"
620 .It Fn m_adj mbuf len
621 Trim
622 .Fa len
623 bytes from the head of an
624 .Vt mbuf chain
625 if
626 .Fa len
627 is positive, from the tail otherwise.
628 .\"
629 .It Fn m_append mbuf len cp
630 Append
631 .Vt len
632 bytes of data
633 .Vt cp
634 to the
635 .Vt mbuf chain .
636 Extend the mbuf chain if the new data does not fit in
637 existing space.
638 .\"
639 .It Fn m_prepend mbuf len how
640 Allocate a new
641 .Vt mbuf
642 and prepend it to the
643 .Vt mbuf chain ,
644 handle
645 .Dv M_PKTHDR
646 properly.
647 .Sy Note :
648 It does not allocate any
649 .Vt mbuf clusters ,
650 so
651 .Fa len
652 must be less than
653 .Dv MLEN
654 or
655 .Dv MHLEN ,
656 depending on the
657 .Dv M_PKTHDR
658 flag setting.
659 .\"
660 .It Fn m_copyup mbuf len dstoff
661 Similar to
662 .Fn m_pullup
663 but copies
664 .Fa len
665 bytes of data into a new mbuf at
666 .Fa dstoff
667 bytes into the mbuf.
668 The
669 .Fa dstoff
670 argument aligns the data and leaves room for a link layer header.
671 Returns the new
672 .Vt mbuf chain
673 on success,
674 and frees the
675 .Vt mbuf chain
676 and returns
677 .Dv NULL
678 on failure.
679 .Sy Note :
680 The function does not allocate
681 .Vt mbuf clusters ,
682 so
683 .Fa len + dstoff
684 must be less than
685 .Dv MHLEN .
686 .\"
687 .It Fn m_pullup mbuf len
688 Arrange that the first
689 .Fa len
690 bytes of an
691 .Vt mbuf chain
692 are contiguous and lay in the data area of
693 .Fa mbuf ,
694 so they are accessible with
695 .Fn mtod mbuf type .
696 It is important to remember that this may involve
697 reallocating some mbufs and moving data so all pointers
698 referencing data within the old mbuf chain
699 must be recalculated or made invalid.
700 Return the new
701 .Vt mbuf chain
702 on success,
703 .Dv NULL
704 on failure
705 (the
706 .Vt mbuf chain
707 is freed in this case).
708 .Sy Note :
709 It does not allocate any
710 .Vt mbuf clusters ,
711 so
712 .Fa len
713 must be less than or equal to
714 .Dv MHLEN .
715 .\"
716 .It Fn m_pulldown mbuf offset len offsetp
717 Arrange that
718 .Fa len
719 bytes between
720 .Fa offset
721 and
722 .Fa offset + len
723 in the
724 .Vt mbuf chain
725 are contiguous and lay in the data area of
726 .Fa mbuf ,
727 so they are accessible with
728 .Fn mtod mbuf type .
729 .Fa len
730 must be smaller than, or equal to, the size of an
731 .Vt mbuf cluster .
732 Return a pointer to an intermediate
733 .Vt mbuf
734 in the chain containing the requested region;
735 the offset in the data region of the
736 .Vt mbuf chain
737 to the data contained in the returned mbuf is stored in
738 .Fa *offsetp .
739 If
740 .Fa offsetp
741 is NULL, the region may be accessed using
742 .Fn mtod mbuf type .
743 If
744 .Fa offsetp
745 is non-NULL, the region may be accessed using
746 .Fn mtod mbuf uint8_t
747 + *offsetp.
748 The region of the mbuf chain between its beginning and
749 .Fa offset
750 is not modified, therefore it is safe to hold pointers to data within
751 this region before calling
752 .Fn m_pulldown .
753 .\"
754 .It Fn m_copym mbuf offset len how
755 Make a copy of an
756 .Vt mbuf chain
757 starting
758 .Fa offset
759 bytes from the beginning, continuing for
760 .Fa len
761 bytes.
762 If
763 .Fa len
764 is
765 .Dv M_COPYALL ,
766 copy to the end of the
767 .Vt mbuf chain .
768 .Sy Note :
769 The copy is read-only, because the
770 .Vt mbuf clusters
771 are not copied, only their reference counts are incremented.
772 .\"
773 .It Fn m_copypacket mbuf how
774 Copy an entire packet including header, which must be present.
775 This is an optimized version of the common case
776 .Fn m_copym mbuf 0 M_COPYALL how .
777 .Sy Note :
778 the copy is read-only, because the
779 .Vt mbuf clusters
780 are not copied, only their reference counts are incremented.
781 .\"
782 .It Fn m_dup mbuf how
783 Copy a packet header
784 .Vt mbuf chain
785 into a completely new
786 .Vt mbuf chain ,
787 including copying any
788 .Vt mbuf clusters .
789 Use this instead of
790 .Fn m_copypacket
791 when you need a writable copy of an
792 .Vt mbuf chain .
793 .\"
794 .It Fn m_copydata mbuf offset len buf
795 Copy data from an
796 .Vt mbuf chain
797 starting
798 .Fa off
799 bytes from the beginning, continuing for
800 .Fa len
801 bytes, into the indicated buffer
802 .Fa buf .
803 .\"
804 .It Fn m_copyback mbuf offset len buf
805 Copy
806 .Fa len
807 bytes from the buffer
808 .Fa buf
809 back into the indicated
810 .Vt mbuf chain ,
811 starting at
812 .Fa offset
813 bytes from the beginning of the
814 .Vt mbuf chain ,
815 extending the
816 .Vt mbuf chain
817 if necessary.
818 .Sy Note :
819 It does not allocate any
820 .Vt mbuf clusters ,
821 just adds
822 .Vt mbufs
823 to the
824 .Vt mbuf chain .
825 It is safe to set
826 .Fa offset
827 beyond the current
828 .Vt mbuf chain
829 end: zeroed
830 .Vt mbufs
831 will be allocated to fill the space.
832 .\"
833 .It Fn m_length mbuf last
834 Return the length of the
835 .Vt mbuf chain ,
836 and optionally a pointer to the last
837 .Vt mbuf .
838 .\"
839 .It Fn m_dup_pkthdr to from how
840 Upon the function's completion, the
841 .Vt mbuf
842 .Fa to
843 will contain an identical copy of
844 .Fa from->m_pkthdr
845 and the per-packet attributes found in the
846 .Vt mbuf chain
847 .Fa from .
848 The
849 .Vt mbuf
850 .Fa from
851 must have the flag
852 .Dv M_PKTHDR
853 initially set, and
854 .Fa to
855 must be empty on entry.
856 .\"
857 .It Fn m_move_pkthdr to from
858 Move
859 .Va m_pkthdr
860 and the per-packet attributes from the
861 .Vt mbuf chain
862 .Fa from
863 to the
864 .Vt mbuf
865 .Fa to .
866 The
867 .Vt mbuf
868 .Fa from
869 must have the flag
870 .Dv M_PKTHDR
871 initially set, and
872 .Fa to
873 must be empty on entry.
874 Upon the function's completion,
875 .Fa from
876 will have the flag
877 .Dv M_PKTHDR
878 and the per-packet attributes cleared.
879 .\"
880 .It Fn m_fixhdr mbuf
881 Set the packet-header length to the length of the
882 .Vt mbuf chain .
883 .\"
884 .It Fn m_devget buf len offset ifp copy
885 Copy data from a device local memory pointed to by
886 .Fa buf
887 to an
888 .Vt mbuf chain .
889 The copy is done using a specified copy routine
890 .Fa copy ,
891 or
892 .Fn bcopy
893 if
894 .Fa copy
895 is
896 .Dv NULL .
897 .\"
898 .It Fn m_cat m n
899 Concatenate
900 .Fa n
901 to
902 .Fa m .
903 Both
904 .Vt mbuf chains
905 must be of the same type.
906 .Fa n
907 is not guaranteed to be valid after
908 .Fn m_cat
909 returns.
910 .Fn m_cat
911 does not update any packet header fields or free mbuf tags.
912 .\"
913 .It Fn m_catpkt m n
914 A variant of
915 .Fn m_cat
916 that operates on packets.
917 Both
918 .Fa m
919 and
920 .Fa n
921 must contain packet headers.
922 .Fa n
923 is not guaranteed to be valid after
924 .Fn m_catpkt
925 returns.
926 .\"
927 .It Fn m_split mbuf len how
928 Partition an
929 .Vt mbuf chain
930 in two pieces, returning the tail:
931 all but the first
932 .Fa len
933 bytes.
934 In case of failure, it returns
935 .Dv NULL
936 and attempts to restore the
937 .Vt mbuf chain
938 to its original state.
939 .\"
940 .It Fn m_apply mbuf off len f arg
941 Apply a function to an
942 .Vt mbuf chain ,
943 at offset
944 .Fa off ,
945 for length
946 .Fa len
947 bytes.
948 Typically used to avoid calls to
949 .Fn m_pullup
950 which would otherwise be unnecessary or undesirable.
951 .Fa arg
952 is a convenience argument which is passed to the callback function
953 .Fa f .
954 .Pp
955 Each time
956 .Fn f
957 is called, it will be passed
958 .Fa arg ,
959 a pointer to the
960 .Fa data
961 in the current mbuf, and the length
962 .Fa len
963 of the data in this mbuf to which the function should be applied.
964 .Pp
965 The function should return zero to indicate success;
966 otherwise, if an error is indicated, then
967 .Fn m_apply
968 will return the error and stop iterating through the
969 .Vt mbuf chain .
970 .\"
971 .It Fn m_getptr mbuf loc off
972 Return a pointer to the mbuf containing the data located at
973 .Fa loc
974 bytes from the beginning of the
975 .Vt mbuf chain .
976 The corresponding offset into the mbuf will be stored in
977 .Fa *off .
978 .It Fn m_defrag m0 how
979 Defragment an mbuf chain, returning the shortest possible
980 chain of mbufs and clusters.
981 If allocation fails and this can not be completed,
982 .Dv NULL
983 will be returned and the original chain will be unchanged.
984 Upon success, the original chain will be freed and the new
985 chain will be returned.
986 .Fa how
987 should be either
988 .Dv M_WAITOK
989 or
990 .Dv M_NOWAIT ,
991 depending on the caller's preference.
992 .Pp
993 This function is especially useful in network drivers, where
994 certain long mbuf chains must be shortened before being added
995 to TX descriptor lists.
996 .It Fn m_collapse m0 how maxfrags
997 Defragment an mbuf chain, returning a chain of at most
998 .Fa maxfrags
999 mbufs and clusters.
1000 If allocation fails or the chain cannot be collapsed as requested,
1001 .Dv NULL
1002 will be returned, with the original chain possibly modified.
1003 As with
1004 .Fn m_defrag ,
1005 .Fa how
1006 should be one of
1007 .Dv M_WAITOK
1008 or
1009 .Dv M_NOWAIT .
1010 .It Fn m_unshare m0 how
1011 Create a version of the specified mbuf chain whose
1012 contents can be safely modified without affecting other users.
1013 If allocation fails and this operation can not be completed,
1014 .Dv NULL
1015 will be returned.
1016 The original mbuf chain is always reclaimed and the reference
1017 count of any shared mbuf clusters is decremented.
1018 .Fa how
1019 should be either
1020 .Dv M_WAITOK
1021 or
1022 .Dv M_NOWAIT ,
1023 depending on the caller's preference.
1024 As a side-effect of this process the returned
1025 mbuf chain may be compacted.
1026 .Pp
1027 This function is especially useful in the transmit path of
1028 network code, when data must be encrypted or otherwise
1029 altered prior to transmission.
1030 .El
1031 .Sh HARDWARE-ASSISTED CHECKSUM CALCULATION
1032 This section currently applies to TCP/IP only.
1033 In order to save the host CPU resources, computing checksums is
1034 offloaded to the network interface hardware if possible.
1035 The
1036 .Va m_pkthdr
1037 member of the leading
1038 .Vt mbuf
1039 of a packet contains two fields used for that purpose,
1040 .Vt int Va csum_flags
1041 and
1042 .Vt int Va csum_data .
1043 The meaning of those fields depends on the direction a packet flows in,
1044 and on whether the packet is fragmented.
1045 Henceforth,
1046 .Va csum_flags
1047 or
1048 .Va csum_data
1049 of a packet
1050 will denote the corresponding field of the
1051 .Va m_pkthdr
1052 member of the leading
1053 .Vt mbuf
1054 in the
1055 .Vt mbuf chain
1056 containing the packet.
1057 .Pp
1058 On output, checksum offloading is attempted after the outgoing
1059 interface has been determined for a packet.
1060 The interface-specific field
1061 .Va ifnet.if_data.ifi_hwassist
1062 (see
1063 .Xr ifnet 9 )
1064 is consulted for the capabilities of the interface to assist in
1065 computing checksums.
1066 The
1067 .Va csum_flags
1068 field of the packet header is set to indicate which actions the interface
1069 is supposed to perform on it.
1070 The actions unsupported by the network interface are done in the
1071 software prior to passing the packet down to the interface driver;
1072 such actions will never be requested through
1073 .Va csum_flags .
1074 .Pp
1075 The flags demanding a particular action from an interface are as follows:
1076 .Bl -tag -width ".Dv CSUM_TCP" -offset indent
1077 .It Dv CSUM_IP
1078 The IP header checksum is to be computed and stored in the
1079 corresponding field of the packet.
1080 The hardware is expected to know the format of an IP header
1081 to determine the offset of the IP checksum field.
1082 .It Dv CSUM_TCP
1083 The TCP checksum is to be computed.
1084 (See below.)
1085 .It Dv CSUM_UDP
1086 The UDP checksum is to be computed.
1087 (See below.)
1088 .El
1089 .Pp
1090 Should a TCP or UDP checksum be offloaded to the hardware,
1091 the field
1092 .Va csum_data
1093 will contain the byte offset of the checksum field relative to the
1094 end of the IP header.
1095 In this case, the checksum field will be initially
1096 set by the TCP/IP module to the checksum of the pseudo header
1097 defined by the TCP and UDP specifications.
1098 .Pp
1099 On input, an interface indicates the actions it has performed
1100 on a packet by setting one or more of the following flags in
1101 .Va csum_flags
1102 associated with the packet:
1103 .Bl -tag -width ".Dv CSUM_IP_CHECKED" -offset indent
1104 .It Dv CSUM_IP_CHECKED
1105 The IP header checksum has been computed.
1106 .It Dv CSUM_IP_VALID
1107 The IP header has a valid checksum.
1108 This flag can appear only in combination with
1109 .Dv CSUM_IP_CHECKED .
1110 .It Dv CSUM_DATA_VALID
1111 The checksum of the data portion of the IP packet has been computed
1112 and stored in the field
1113 .Va csum_data
1114 in network byte order.
1115 .It Dv CSUM_PSEUDO_HDR
1116 Can be set only along with
1117 .Dv CSUM_DATA_VALID
1118 to indicate that the IP data checksum found in
1119 .Va csum_data
1120 allows for the pseudo header defined by the TCP and UDP specifications.
1121 Otherwise the checksum of the pseudo header must be calculated by
1122 the host CPU and added to
1123 .Va csum_data
1124 to obtain the final checksum to be used for TCP or UDP validation purposes.
1125 .El
1126 .Pp
1127 If a particular network interface just indicates success or
1128 failure of TCP or UDP checksum validation without returning
1129 the exact value of the checksum to the host CPU, its driver can mark
1130 .Dv CSUM_DATA_VALID
1131 and
1132 .Dv CSUM_PSEUDO_HDR
1133 in
1134 .Va csum_flags ,
1135 and set
1136 .Va csum_data
1137 to
1138 .Li 0xFFFF
1139 hexadecimal to indicate a valid checksum.
1140 It is a peculiarity of the algorithm used that the Internet checksum
1141 calculated over any valid packet will be
1142 .Li 0xFFFF
1143 as long as the original checksum field is included.
1144 .Sh STRESS TESTING
1145 When running a kernel compiled with the option
1146 .Dv MBUF_STRESS_TEST ,
1147 the following
1148 .Xr sysctl 8 Ns
1149 -controlled options may be used to create
1150 various failure/extreme cases for testing of network drivers
1151 and other parts of the kernel that rely on
1152 .Vt mbufs .
1153 .Bl -tag -width ident
1154 .It Va net.inet.ip.mbuf_frag_size
1155 Causes
1156 .Fn ip_output
1157 to fragment outgoing
1158 .Vt mbuf chains
1159 into fragments of the specified size.
1160 Setting this variable to 1 is an excellent way to
1161 test the long
1162 .Vt mbuf chain
1163 handling ability of network drivers.
1164 .It Va kern.ipc.m_defragrandomfailures
1165 Causes the function
1166 .Fn m_defrag
1167 to randomly fail, returning
1168 .Dv NULL .
1169 Any piece of code which uses
1170 .Fn m_defrag
1171 should be tested with this feature.
1172 .El
1173 .Sh RETURN VALUES
1174 See above.
1175 .Sh SEE ALSO
1176 .Xr ifnet 9 ,
1177 .Xr mbuf_tags 9
1178 .Sh HISTORY
1179 .\" Please correct me if I'm wrong
1180 .Vt Mbufs
1181 appeared in an early version of
1182 .Bx .
1183 Besides being used for network packets, they were used
1184 to store various dynamic structures, such as routing table
1185 entries, interface addresses, protocol control blocks, etc.
1186 In more recent
1187 .Fx
1188 use of
1189 .Vt mbufs
1190 is almost entirely limited to packet storage, with
1191 .Xr uma 9
1192 zones being used directly to store other network-related memory.
1193 .Pp
1194 Historically, the
1195 .Vt mbuf
1196 allocator has been a special-purpose memory allocator able to run in
1197 interrupt contexts and allocating from a special kernel address space map.
1198 As of
1199 .Fx 5.3 ,
1200 the
1201 .Vt mbuf
1202 allocator is a wrapper around
1203 .Xr uma 9 ,
1204 allowing caching of
1205 .Vt mbufs ,
1206 clusters, and
1207 .Vt mbuf
1208 + cluster pairs in per-CPU caches, as well as bringing other benefits of
1209 slab allocation.
1210 .Sh AUTHORS
1211 The original
1212 .Nm
1213 manual page was written by
1214 .An Yar Tikhiy .
1215 The
1216 .Xr uma 9
1217 .Vt mbuf
1218 allocator was written by
1219 .An Bosko Milekic .