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