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