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