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