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