xref: /openbsd/share/man/man9/mbuf.9 (revision 5af055cd)
1.\"     $OpenBSD: mbuf.9,v 1.95 2015/11/23 17:53:57 jmc Exp $
2.\"
3.\" Copyright (c) 2001 Jean-Jacques Bernard-Gundol <jjbg@openbsd.org>
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. The name of the author may not be used to endorse or promote products
15.\"    derived from this software without specific prior written permission
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\"
28.Dd $Mdocdate: November 23 2015 $
29.Dt MGET 9
30.Os
31.Sh NAME
32.Nm m_copym2 ,
33.Nm m_copym ,
34.Nm m_free ,
35.Nm m_get ,
36.Nm MGET ,
37.Nm m_getclr ,
38.Nm m_gethdr ,
39.Nm m_resethdr ,
40.Nm MGETHDR ,
41.Nm m_prepend ,
42.Nm M_PREPEND ,
43.Nm m_pulldown ,
44.Nm m_pullup ,
45.Nm m_split ,
46.Nm m_inject ,
47.Nm m_getptr ,
48.Nm m_adj ,
49.Nm m_copyback ,
50.Nm m_defrag ,
51.Nm m_freem ,
52.Nm m_reclaim ,
53.Nm m_copydata ,
54.Nm m_cat ,
55.Nm m_devget ,
56.Nm m_apply ,
57.Nm MCLGET ,
58.Nm MCLGETI ,
59.Nm MEXTADD ,
60.Nm M_ALIGN ,
61.Nm MH_ALIGN ,
62.Nm M_READONLY ,
63.Nm M_LEADINGSPACE ,
64.Nm M_TRAILINGSPACE ,
65.Nm mtod ,
66.Nm m_dup_pkthdr
67.Nd kernel memory management for networking protocols
68.Sh SYNOPSIS
69.In sys/mbuf.h
70.Ft struct mbuf *
71.Fn m_copym2 "struct mbuf *m" "int off" "int len" "int wait"
72.Ft struct mbuf *
73.Fn m_copym "struct mbuf *m" "int off" "int len" "int wait"
74.Ft struct mbuf *
75.Fn m_free "struct mbuf *m"
76.Ft struct mbuf *
77.Fn m_get "int how" "int type"
78.Fn MGET "struct mbuf *m" "int how" "int type"
79.Ft struct mbuf *
80.Fn m_getclr "int how" "int type"
81.Ft void
82.Fn m_resethdr struct mbuf *
83.Ft struct mbuf *
84.Fn m_gethdr "int how" "int type"
85.Fn MGETHDR "struct mbuf *m" "int how" "int type"
86.Ft struct mbuf *
87.Fn m_prepend "struct mbuf *m" "int len" "int how"
88.Fn M_PREPEND "struct mbuf *m" "int plen" "int how"
89.Ft struct mbuf *
90.Fn m_pulldown "struct mbuf *m" "int off" "int len" "int *offp"
91.Ft struct mbuf *
92.Fn m_pullup "struct mbuf *n" "int len"
93.Ft struct mbuf *
94.Fn m_split "struct mbuf *m0" "int len0" "int wait"
95.Ft struct mbuf *
96.Fn m_inject "struct mbuf *m0" "int len0" "int siz" "int wait"
97.Ft struct mbuf *
98.Fn m_getptr "struct mbuf *m" "int loc" "int *off"
99.Ft void
100.Fn m_adj "struct mbuf *mp" "int req_len"
101.Ft int
102.Fn m_copyback "struct mbuf *m0" "int off" "int len" "const void *cp" "int wait"
103.Ft int
104.Fn m_defrag "struct mbuf *m" "int wait"
105.Ft void
106.Fn m_freem "struct mbuf *m"
107.Ft void
108.Fn m_reclaim "void"
109.Ft void
110.Fn m_copydata "struct mbuf *m" "int off" "int len" "caddr_t cp"
111.Ft void
112.Fn m_cat "struct mbuf *m" "struct mbuf *n"
113.Ft struct mbuf *
114.Fn m_devget "char *buf" "int totlen" "int off" "struct ifnet *ifp"
115.Ft int
116.Fn m_apply "struct mbuf *m" "int off" "int len" \
117"int (*func)(caddr_t, caddr_t, unsigned int)" "caddr_t fstate"
118.Fn MCLGET "struct mbuf *m" "int how"
119.Ft struct mbuf *
120.Fn MCLGETI "struct mbuf *m" "int how" "struct ifnet *ifp" "int len"
121.Fn MEXTADD "struct mbuf *m" "caddr_t buf" "u_int size" "int flags" \
122"void (*free)(caddr_t, u_int, void *)" "void *arg"
123.Fn M_ALIGN "struct mbuf *m" "int len"
124.Fn MH_ALIGN "struct mbuf *m" "int len"
125.Fn M_READONLY "struct mbuf *m"
126.Fn M_LEADINGSPACE "struct mbuf *m"
127.Fn M_TRAILINGSPACE "struct mbuf *m"
128.Ft int
129.Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from" "int how"
130.Bd -literal
131#define MSIZE           256
132
133#define MLEN            (MSIZE - sizeof(struct m_hdr))
134#define MHLEN           (MLEN - sizeof(struct pkthdr))
135
136#define MAXMCLBYTES     (64 * 1024)
137#define MINCLSIZE       (MHLEN + MLEN + 1)
138#define M_MAXCOMPRESS   (MHLEN / 2)
139
140#define MCLSHIFT        11
141
142#define MCLBYTES        (1 << MCLSHIFT)
143#define MCLOFSET        (MCLBYTES - 1)
144
145#define mtod(m,t)       ((t)((m)->m_data))
146
147struct m_hdr {
148        struct  mbuf *mh_next;
149        struct  mbuf *mh_nextpkt;
150        caddr_t mh_data;
151        u_int   mh_len;
152        short   mh_type;
153        u_short mh_flags;
154};
155
156struct pkthdr {
157	void			*ph_cookie;
158	SLIST_HEAD(, m_tag)	 ph_tags;
159	int			 len;
160	u_int16_t		 ph_tagsset;
161	u_int16_t		 ph_flowid;
162	u_int16_t		 csum_flags;
163	u_int16_t		 ether_vtag;
164	u_int			 ph_rtableid;
165	u_int			 ph_ifidx;
166	struct pkthdr_pf	 pf;
167};
168
169struct pkthdr_pf {
170	struct pf_state_key *statekey;
171	struct inpcb *inp;
172	u_int32_t qid;
173	u_int16_t tag;
174	u_int8_t  flags;
175	u_int8_t  routed;
176	u_int8_t  prio;
177	u_int8_t  pad[3];
178};
179
180struct mbuf_ext {
181	caddr_t ext_buf;
182	void    (*ext_free)(caddr_t, u_int, void *);
183	void    *ext_arg;
184	u_int   ext_size;
185	struct mbuf *ext_nextref;
186	struct mbuf *ext_prevref;
187};
188
189struct mbuf {
190        struct  m_hdr m_hdr;
191        union {
192                struct {
193                        struct  pkthdr MH_pkthdr;
194                        union {
195                                struct  mbuf_ext MH_ext;
196                                char    MH_databuf[MHLEN];
197                        } MH_dat;
198                } MH;
199                char    M_databuf[MLEN];
200        } M_dat;
201};
202
203#define m_next          m_hdr.mh_next
204#define m_len           m_hdr.mh_len
205#define m_data          m_hdr.mh_data
206#define m_type          m_hdr.mh_type
207#define m_flags         m_hdr.mh_flags
208#define m_nextpkt       m_hdr.mh_nextpkt
209#define m_pkthdr        M_dat.MH.MH_pkthdr
210#define m_ext           M_dat.MH.MH_dat.MH_ext
211#define m_pktdat        M_dat.MH.MH_dat.MH_databuf
212#define m_dat           M_dat.M_databuf
213.Ed
214.Sh DESCRIPTION
215The
216.Nm
217functions provide a way to manage the memory buffers used by the kernel's
218networking subsystem.
219Several functions and macros are used to allocate and deallocate mbufs,
220but also to get, inject, remove, copy, modify, prepend or append data
221inside these mbufs.
222The size of an
223.Nm
224is defined by MSIZE.
225.Pp
226An
227.Nm
228structure is defined as an
229.Fa m_hdr
230structure followed by a
231union.
232The header contains the following elements:
233.Bl -tag -width foobarmoocow
234.It Fa mh_next
235A pointer to the next mbuf in the mbuf chain.
236.It Fa mh_nextpkt
237A pointer to the next mbuf chain (i.e., packet) in the queue.
238.It Fa mh_data
239Indicates the address of the beginning of data in the mbuf.
240.It Fa mh_len
241Indicates the amount of data in the mbuf.
242.It Fa mh_type
243Indicates the type of data contained in the mbuf (see below).
244.It Fa mh_flags
245Flags (see below).
246.El
247.Pp
248The
249.Fa mh_type
250variable can take the following values:
251.Pp
252.Bl -tag -compact -offset indent -width XXXXXXXXXXXXXXXXXX
253.It Dv MT_FREE
254the mbuf should be on the free list.
255.It Dv MT_DATA
256the data in the mbuf was dynamically allocated.
257.It Dv MT_HEADER
258the data contains a packet header.
259.It Dv MT_SONAME
260the data is a socket name.
261.It Dv MT_SOOPTS
262the data are socket options.
263.It Dv MT_FTABLE
264the data is a fragment reassembly header.
265.It Dv MT_CONTROL
266the mbuf contains extra-data protocol message.
267.It Dv MT_OOBDATA
268the data consists of out-of-band data.
269.El
270.Pp
271The
272.Fa mh_flags
273variable can take the following values:
274.Pp
275.Bl -tag -compact -offset indent -width XXXXXXXXXXXXXXXXXX
276.It Dv M_EXT
277mbuf has associated external storage.
278.It Dv M_PKTHDR
279the mbuf is the first that forms a packet.
280.It Dv M_EOR
281end of record.
282.It Dv M_EXTWR
283external storage is writable.
284.It Dv M_PROTO1
285protocol-specific.
286.It Dv M_VLANTAG
287.Fa m_pkthdr.ether_vtag
288variable is valid.
289.It Dv M_LOOP
290for mbuf statistics.
291.It Dv M_FILDROP
292dropped by
293.Xr bpf 4
294filter.
295.It Dv M_BCAST
296packet send/received as link-level broadcast.
297.It Dv M_MCAST
298packet send/received as link-level multicast.
299.It Dv M_CONF
300packet was encrypted (ESP-transport).
301.It Dv M_AUTH
302packet was authenticated (AH or ESP).
303.It Dv M_TUNNEL
304header was IP-in-IP encapsulated by tunnel mode IPsec.
305.It Dv M_ZEROIZE
306Zero the data part of the mbufs in the mbuf chain pointed to by
307.Nm m_free .
308.It Dv M_COMP
309header was decompressed.
310.It Dv M_LINK0
311link layer specific flag.
312.El
313.Pp
314An external cluster is used when the data to hold in the mbuf is
315large.
316The size of an external cluster is between MCLBYTES and MAXMCLBYTES.
317A cluster should be used when the size of the data reach MINCLSIZE
318(the minimum size to be held by an external cluster).
319.Pp
320The combination of the M_EXT and M_PKTHDR flags give four types of
321mbuf.
322When none of these constants are in use, the mbuf is a "normal"
323one, where the data part of the mbuf has the following elements:
324.Bl -tag -width foobarmoocow
325.It Fa m_dat
326buffer holding the data (size MLEN).
327.El
328.Pp
329When only M_PKTHDR is set, the data contained in the mbuf is a packet header.
330The data itself is contained in the mbuf (just like the previous case),
331but part of the mbuf is used to store a packet header.
332The data part has then the following elements:
333.Bl -tag -width foobarmoocow
334.It Fa m_pkthdr
335packet header, containing the length of the data, a pointer to the
336interface on which the data was received, checksum information
337and list of
338.Xr mbuf_tags 9 .
339.It Fa m_pktdat
340buffer holding the data (size MHLEN).
341.El
342.Pp
343The
344.Fa m_pkthdr.csum_flags
345variable can take the following values:
346.Pp
347.Bl -tag -compact -offset indent -width XXXXXXXXXXXXXXXXXX
348.It Dv M_IPV4_CSUM_OUT
349IPv4 checksum needed.
350.It Dv M_TCP_CSUM_OUT
351TCP checksum needed.
352.It Dv M_UDP_CSUM_OUT
353UDP checksum needed.
354.It Dv M_ICMP_CSUM_OUT
355ICMP/ICMPv6 checksum needed.
356.It Dv M_IPV4_CSUM_IN_OK
357IPv4 checksum verified.
358.It Dv M_IPV4_CSUM_IN_BAD
359IPv4 checksum bad.
360.It Dv M_TCP_CSUM_IN_OK
361TCP checksum verified.
362.It Dv M_TCP_CSUM_IN_BAD
363TCP checksum bad.
364.It Dv M_UDP_CSUM_IN_OK
365UDP checksum verified.
366.It Dv M_UDP_CSUM_IN_BAD
367UDP checksum bad.
368.It Dv M_ICMP_CSUM_IN_OK
369ICMP/ICMPv6 checksum verified.
370.It Dv M_ICMP_CSUM_IN_BAD
371ICMP/ICMPv6 checksum bad.
372.El
373.Pp
374The
375.Fa m_pkthdr.flowid
376variable can contain a low resolution (15-bit) classification of a
377flow or connection that the current mbuf is part of.
378If the flowid is valid, it may be used as an alternative to hashing
379the packets content to pick between different paths for the traffic.
380The following masks can be ORed with the flowid:
381.Pp
382.Bl -tag -compact -offset indent -width XXXXXXXXXXXXXXXXXX
383.It Dv M_FLOWID_VALID
384The flow ID has been set.
385.It Dv M_FLOWID_MASK
386The flow ID.
387.El
388.Pp
389When only M_EXT flag is set, an external storage buffer is being used to
390hold the data, which is no longer stored in the mbuf.
391The data part of the mbuf has now the following elements:
392.Bl -tag -width foobarmoocow
393.It Fa m_pkthdr
394a packet header, just like the previous case, but it is empty.
395No information is stored here.
396.It Fa m_ext
397a structure containing information about the external storage
398buffer.
399The information consists of the address of the external buffer,
400a pointer to the function used to free the buffer, a pointer to the
401arguments of the function, the size of the buffer, the type of the
402buffer, and pointers to the previous and next mbufs using this
403cluster.
404.El
405.Pp
406When both the M_EXT and M_PKTHDR flags are set, an external storage buffer
407is being used to store the data and this data contains a packet header.
408The structure used is the same as the previous one except that the
409.Fa m_pkthdr
410element is not empty, it contains the same information as when
411M_PKTHDR is used alone.
412.Bl -tag -width Ds
413.It Fn m_copym "struct mbuf *m" "int off" "int len" "int wait"
414Copy an mbuf chain starting at
415.Fa off
416bytes from the beginning
417and continuing for
418.Fa len
419bytes.
420If
421.Fa off
422is zero and
423.Fa m
424has the M_PKTHDR flag set,
425the header is copied.
426If
427.Fa len
428is M_COPYALL
429the whole mbuf is copied.
430The
431.Fa wait
432parameter can be M_WAIT or
433M_DONTWAIT.
434It does not copy clusters, it just increases their reference count.
435.It Fn m_copym2 "struct mbuf *m" "int off" "int len" "int wait"
436The same as
437.Fn m_copym
438except that it copies cluster mbufs, whereas
439.Fn m_copym
440just increases the reference count of the clusters.
441.It Fn m_free "struct mbuf *m"
442Free the mbuf pointed to by
443.Fa m .
444A pointer to the successor of the mbuf,
445if it exists, is returned by the function.
446If
447.Fa m
448is a
449.Dv NULL
450pointer, no action occurs and
451.Dv NULL
452is returned.
453.It Fn m_get "int how" "int type"
454Return a pointer to an mbuf of the type specified.
455If the
456.Fa how
457argument is
458.Fa M_WAITOK ,
459the function may call
460.Xr tsleep 9
461to await resources.
462If
463.Fa how
464is
465.Fa M_DONTWAIT
466and resources are not available,
467.Fn m_get
468returns NULL.
469.It Fn MGET "struct mbuf *m" "int how" "int type"
470Return a pointer to an mbuf in
471.Fa m
472of the type specified.
473See
474.Fn m_get
475for a description of
476.Fa how .
477.It Fn m_getclr "int how" "int type"
478Return a pointer to an mbuf of the type specified, and clear the data
479area of the mbuf.
480See
481.Fn m_get
482for a description of
483.Fa how .
484.It Fn m_resethdr "struct mbuf *"
485Deletes all
486.Xr pf 4
487data and all tags attached to a
488.Fa mbuf .
489.It Fn m_gethdr "int how" "int type"
490Return a pointer to an mbuf of the type specified after initializing
491it to contain a packet header.
492See
493.Fn m_get
494for a description of
495.Fa how .
496.It Fn MGETHDR "struct mbuf *m" "int how" "int type"
497Return a pointer to an mbuf of the type specified after initializing
498it to contain a packet header.
499See
500.Fn m_get
501for a description of
502.Fa how .
503.It Fn m_prepend "struct mbuf *m" "int len" "int how"
504Allocate a new mbuf and prepend it to the mbuf chain pointed to by
505.Fa m .
506If
507.Fa m
508points to an mbuf with a packet header, it is moved to the new
509mbuf that has been prepended.
510The return value is a pointer on the new mbuf chain.
511If this function fails to allocate a new mbuf,
512.Fa m
513is freed.
514See
515.Fn m_get
516for a description of
517.Fa how .
518.Pp
519.Fn m_prepend
520should never be called directly.
521Use
522.Fn M_PREPEND
523instead.
524.It Fn M_PREPEND "struct mbuf *m" "int plen" "int how"
525Prepend space of size
526.Fa plen
527to the mbuf pointed to by
528.Fa m .
529If a new mbuf must be allocated,
530.Fa how
531specifies whether to wait or not.
532If this function fails to allocate a new mbuf,
533.Fa m
534is freed.
535.It Fn m_pulldown "struct mbuf *m" "int off" "int len" "int *offp"
536Ensure that the data in the mbuf chain starting at
537.Fa off
538and ending at
539.Fa off+len
540will be put in a continuous memory region.
541If memory must be allocated, then it will fail if the
542.Fa len
543argument is greater than MAXMCLBYTES.
544The pointer returned points to an mbuf in the chain and the new offset
545for data in this mbuf is
546.Fa *offp .
547If this function fails,
548.Fa m
549is freed.
550.It Fn m_pullup "struct mbuf *n" "int len"
551Ensure that the data in the mbuf chain starting at the beginning of
552the chain and ending at
553.Fa len
554will be put in continuous memory region.
555If memory must be allocated, then it will fail if the
556.Fa len
557argument is greater than MAXMCLBYTES.
558If this function fails,
559.Fa n
560is freed.
561.It Fn m_split "struct mbuf *m0" "int len0" "int wait"
562Split an mbuf chain in two pieces, returning a pointer to
563the tail (which is made of the previous mbuf chain except the first
564.Fa len0
565bytes).
566.It Fn m_inject "struct mbuf *m0" "int len0" "int siz" "int wait"
567Inject a new mbuf chain of length
568.Fa siz
569into the mbuf chain pointed to by
570.Fa m0
571at position
572.Fa len0 .
573If there is enough space for an object of size
574.Fa siz
575in the appropriate location, no memory will be allocated.
576On failure, the function returns NULL (the mbuf is left untouched) and
577on success, a pointer to the first injected mbuf is returned.
578.It Fn m_getptr "struct mbuf *m" "int loc" "int *off"
579Returns a pointer to the mbuf containing the data located at
580.Fa loc
581bytes of the beginning.
582The offset in the new mbuf is pointed to by
583.Fa off .
584.It Fn m_adj "struct mbuf *mp" "int req_len"
585Trims
586.Fa req_len
587bytes of data from the mbuf chain pointed to by
588.Fa mp .
589If
590.Fa req_len
591is positive, the data will be trimmed from the head of the mbuf chain
592and if it is negative, it will be trimmed from the tail of the mbuf
593chain.
594.It Fn m_copyback "struct mbuf *m0" "int off" "int len" "caddr_t cp" "int wait"
595Copy data from a buffer pointed to by
596.Fa cp
597back into the mbuf chain pointed to by
598.Fa m0
599starting at
600.Fa off
601bytes from the beginning, extending the mbuf chain if
602necessary, sleeping for mbufs if
603.Fa wait
604is
605.Fa M_WAIT .
606If
607.Fa M_NOWAIT
608is set and no mbufs are available,
609.Fn m_copyback
610returns
611.Er ENOBUFS .
612The mbuf chain must be initialized properly, including setting
613.Fa m_len .
614.It Fn m_defrag "struct mbuf *m" "int wait"
615Defragment the data mbufs referenced by
616.Fa m
617by replacing the chain with a copy of their contents made into a
618single mbuf or cluster.
619.Fa wait
620specifies whether it can wait or not for the replacement storage.
621.Fn m_defrag
622returns 0 on success or
623.Er ENOBUFS
624on failure.
625The mbuf pointer
626.Fa m
627remains in existence and unchanged on failure.
628.It Fn m_freem "struct mbuf *m"
629Free the mbuf chain pointed to by
630.Fa m .
631If
632.Fa m
633is a
634.Dv NULL
635pointer, no action occurs.
636.It Fn m_reclaim "void"
637Ask protocols to free unused memory space.
638.It Fn m_copydata "struct mbuf *m" "int off" "int len" "caddr_t cp"
639Copy data from the mbuf chain pointed to by
640.Fa m
641starting at
642.Fa off
643bytes from the beginning and continuing for
644.Fa len
645bytes into the buffer pointed to by
646.Fa cp .
647.It Fn m_cat "struct mbuf *m" "struct mbuf *n"
648Concatenate the mbuf chain pointed to by
649.Fa n
650to the mbuf chain pointed to by
651.Fa m .
652The mbuf chains must be of the same type.
653.It Fn m_devget "char *buf" "int totlen" "int off" "struct ifnet *ifp"
654Copy
655.Fa totlen
656bytes of data from device local memory pointed to by
657.Fa buf .
658The data is copied into an mbuf chain at offset
659.Fa off
660and a pointer to the head of the chain is returned.
661Returns NULL on failure.
662.It Fn m_apply "struct mbuf *m" "int off" "int len" \
663"int (*func)(caddr_t, caddr_t, unsigned int)" "caddr_t fstate"
664Apply the function
665.Fa func
666to the data in the mbuf chain pointed to by
667.Fa m
668starting at
669.Fa off
670bytes from the beginning and continuing for
671.Fa len
672bytes.
673.It Fn mtod "struct mbuf *m" "datatype"
674Return a pointer to the data contained in the specified mbuf
675.Fa m
676cast to
677.Fa datatype .
678.It Fn MCLGET "struct mbuf *m" "int how"
679Allocate and add an mbuf cluster to the mbuf pointed to by
680.Fa m .
681On success, the flag M_EXT is set in the mbuf.
682See
683.Fn m_get
684for a description of
685.Fa how .
686.It Fn MCLGETI "struct mbuf *m" "int how" "struct ifnet *ifp" "int len"
687If
688.Fa m
689is NULL, allocate it.
690Then allocate and add an mbuf cluster of length
691.Fa len
692to the mbuf pointed to by
693.Fa m .
694Returns either the mbuf
695.Fa m
696that was passed in, or the newly allocated one which was allocated; in
697either case the flag M_EXT is set in the mbuf.
698See
699.Fn m_get
700for a description of
701.Fa how .
702.It Fn MEXTADD "struct mbuf *m" "caddr_t buf" "u_int size" "int flags" \
703"void (*free)(caddr_t, u_int, void *)" "void *arg"
704Add pre-allocated storage to the mbuf pointed to by
705.Fa m .
706On success, the flag M_EXT is set in the mbuf, and M_EXTWR is specified in
707.Fa flags .
708.It Fn M_ALIGN "struct mbuf *m" "int len"
709Set the
710.Fa m_data
711pointer of the newly allocated mbuf with
712.Fn m_get
713or
714.Fn MGET
715pointed to by
716.Fa m
717to an object of the specified size
718.Fa len
719at the end of the mbuf, longword aligned.
720.It Fn MH_ALIGN "m" "len"
721Same as
722.Fn M_ALIGN
723except it is for an mbuf allocated with
724.Fn m_gethdr
725or
726.Fn MGETHDR .
727.It Fn M_READONLY "struct mbuf *m"
728Check if the data of the mbuf pointed to by
729.Fa m
730is read-only.
731This is true for non-cluster external storage and for clusters that
732are being referenced by more than one mbuf.
733.It Fn M_LEADINGSPACE "struct mbuf *m"
734Compute the amount of space available before the current start of data
735in the mbuf pointed to by
736.Fa m .
737.It Fn M_TRAILINGSPACE "struct mbuf *m"
738Compute the amount of space available after the end of data in the
739mbuf pointed to by
740.Fa m .
741.It Fn m_dup_pkthdr "struct mbuf *to" "struct mbuf *from" "int how"
742Copy mbuf packet header, including mbuf tags, from
743.Fa from
744to
745.Fa to .
746See
747.Fn m_get
748for a description of
749.Fa how .
750.El
751.Sh CODE REFERENCES
752The mbuf management functions are implemented in the files
753.Pa sys/kern/uipc_mbuf.c
754and
755.Pa sys/kern/uipc_mbuf2.c .
756The function prototypes and the macros are located in
757.Pa sys/sys/mbuf.h .
758.Sh SEE ALSO
759.Xr netstat 1 ,
760.Xr mbuf_tags 9 ,
761.Xr mutex 9 ,
762.Xr spl 9
763.Rs
764.%A Jun-Ichiro Hagino
765.%T "Mbuf issues in 4.4BSD IPv6/IPsec support (experiences from KAME IPv6/IPsec implementation)"
766.%B "Proceedings of the Freenix Track: 2000 USENIX Annual Technical Conference"
767.%D June 2000
768.Re
769