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