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