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