uipc_mbuf.c revision 152035
1139804Simp/*- 21541Srgrimes * Copyright (c) 1982, 1986, 1988, 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 4. Neither the name of the University nor the names of its contributors 141541Srgrimes * may be used to endorse or promote products derived from this software 151541Srgrimes * without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 291541Srgrimes * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 301541Srgrimes */ 311541Srgrimes 32116182Sobrien#include <sys/cdefs.h> 33116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/uipc_mbuf.c 152035 2005-11-04 17:20:53Z andre $"); 34116182Sobrien 35101007Srwatson#include "opt_mac.h" 3677572Sobrien#include "opt_param.h" 37113490Ssilby#include "opt_mbuf_stress_test.h" 38101007Srwatson 391541Srgrimes#include <sys/param.h> 401541Srgrimes#include <sys/systm.h> 4176166Smarkm#include <sys/kernel.h> 42125296Ssilby#include <sys/limits.h> 4376166Smarkm#include <sys/lock.h> 44101173Srwatson#include <sys/mac.h> 4532036Sbde#include <sys/malloc.h> 461541Srgrimes#include <sys/mbuf.h> 4723081Swollman#include <sys/sysctl.h> 481541Srgrimes#include <sys/domain.h> 491541Srgrimes#include <sys/protosw.h> 50125296Ssilby#include <sys/uio.h> 5176166Smarkm 529759Sbdeint max_linkhdr; 539759Sbdeint max_protohdr; 549759Sbdeint max_hdr; 559759Sbdeint max_datalen; 56116455Ssilby#ifdef MBUF_STRESS_TEST 57112777Ssilbyint m_defragpackets; 58112777Ssilbyint m_defragbytes; 59112777Ssilbyint m_defraguseless; 60112777Ssilbyint m_defragfailure; 61113490Ssilbyint m_defragrandomfailures; 62113490Ssilby#endif 631541Srgrimes 6466475Sbmilekic/* 6566475Sbmilekic * sysctl(8) exported objects 6666475Sbmilekic */ 6744078SdfrSYSCTL_DECL(_kern_ipc); 6823081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 6923081Swollman &max_linkhdr, 0, ""); 7023081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 7123081Swollman &max_protohdr, 0, ""); 7223081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 7323081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 7423081Swollman &max_datalen, 0, ""); 75116455Ssilby#ifdef MBUF_STRESS_TEST 76112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD, 77112777Ssilby &m_defragpackets, 0, ""); 78112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD, 79112777Ssilby &m_defragbytes, 0, ""); 80112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD, 81112777Ssilby &m_defraguseless, 0, ""); 82112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD, 83112777Ssilby &m_defragfailure, 0, ""); 84113490SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW, 85113490Ssilby &m_defragrandomfailures, 0, ""); 86113490Ssilby#endif 8775112Sbmilekic 881541Srgrimes/* 89129906Sbmilekic * Allocate a given length worth of mbufs and/or clusters (whatever fits 90129906Sbmilekic * best) and return a pointer to the top of the allocated chain. If an 91129906Sbmilekic * existing mbuf chain is provided, then we will append the new chain 92129906Sbmilekic * to the existing one but still return the top of the newly allocated 93129906Sbmilekic * chain. 94129906Sbmilekic */ 95129906Sbmilekicstruct mbuf * 96129906Sbmilekicm_getm(struct mbuf *m, int len, int how, short type) 97129906Sbmilekic{ 98129906Sbmilekic struct mbuf *mb, *top, *cur, *mtail; 99129906Sbmilekic int num, rem; 100129906Sbmilekic int i; 101129906Sbmilekic 102129906Sbmilekic KASSERT(len >= 0, ("m_getm(): len is < 0")); 103129906Sbmilekic 104129906Sbmilekic /* If m != NULL, we will append to the end of that chain. */ 105129906Sbmilekic if (m != NULL) 106129906Sbmilekic for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next); 107129906Sbmilekic else 108129906Sbmilekic mtail = NULL; 109129906Sbmilekic 110129906Sbmilekic /* 111129906Sbmilekic * Calculate how many mbufs+clusters ("packets") we need and how much 112129906Sbmilekic * leftover there is after that and allocate the first mbuf+cluster 113129906Sbmilekic * if required. 114129906Sbmilekic */ 115129906Sbmilekic num = len / MCLBYTES; 116129906Sbmilekic rem = len % MCLBYTES; 117129906Sbmilekic top = cur = NULL; 118129906Sbmilekic if (num > 0) { 119129906Sbmilekic if ((top = cur = m_getcl(how, type, 0)) == NULL) 120129906Sbmilekic goto failed; 121130271Smux top->m_len = 0; 122129906Sbmilekic } 123129906Sbmilekic num--; 124129906Sbmilekic 125129906Sbmilekic for (i = 0; i < num; i++) { 126129906Sbmilekic mb = m_getcl(how, type, 0); 127129906Sbmilekic if (mb == NULL) 128129906Sbmilekic goto failed; 129129906Sbmilekic mb->m_len = 0; 130129906Sbmilekic cur = (cur->m_next = mb); 131129906Sbmilekic } 132129906Sbmilekic if (rem > 0) { 133129906Sbmilekic mb = (rem > MINCLSIZE) ? 134129906Sbmilekic m_getcl(how, type, 0) : m_get(how, type); 135129906Sbmilekic if (mb == NULL) 136129906Sbmilekic goto failed; 137129906Sbmilekic mb->m_len = 0; 138129906Sbmilekic if (cur == NULL) 139129906Sbmilekic top = mb; 140129906Sbmilekic else 141129906Sbmilekic cur->m_next = mb; 142129906Sbmilekic } 143129906Sbmilekic 144129906Sbmilekic if (mtail != NULL) 145129906Sbmilekic mtail->m_next = top; 146129906Sbmilekic return top; 147129906Sbmilekicfailed: 148129906Sbmilekic if (top != NULL) 149129906Sbmilekic m_freem(top); 150129906Sbmilekic return NULL; 151129906Sbmilekic} 152129906Sbmilekic 153129906Sbmilekic/* 154129906Sbmilekic * Free an entire chain of mbufs and associated external buffers, if 155129906Sbmilekic * applicable. 156129906Sbmilekic */ 157129906Sbmilekicvoid 158129906Sbmilekicm_freem(struct mbuf *mb) 159129906Sbmilekic{ 160129906Sbmilekic 161129906Sbmilekic while (mb != NULL) 162129906Sbmilekic mb = m_free(mb); 163129906Sbmilekic} 164129906Sbmilekic 165129906Sbmilekic/*- 166129906Sbmilekic * Configure a provided mbuf to refer to the provided external storage 167129906Sbmilekic * buffer and setup a reference count for said buffer. If the setting 168129906Sbmilekic * up of the reference count fails, the M_EXT bit will not be set. If 169129906Sbmilekic * successfull, the M_EXT bit is set in the mbuf's flags. 170129906Sbmilekic * 171129906Sbmilekic * Arguments: 172129906Sbmilekic * mb The existing mbuf to which to attach the provided buffer. 173129906Sbmilekic * buf The address of the provided external storage buffer. 174129906Sbmilekic * size The size of the provided buffer. 175129906Sbmilekic * freef A pointer to a routine that is responsible for freeing the 176129906Sbmilekic * provided external storage buffer. 177129906Sbmilekic * args A pointer to an argument structure (of any type) to be passed 178129906Sbmilekic * to the provided freef routine (may be NULL). 179129906Sbmilekic * flags Any other flags to be passed to the provided mbuf. 180129906Sbmilekic * type The type that the external storage buffer should be 181129906Sbmilekic * labeled with. 182129906Sbmilekic * 183129906Sbmilekic * Returns: 184129906Sbmilekic * Nothing. 185129906Sbmilekic */ 186129906Sbmilekicvoid 187129906Sbmilekicm_extadd(struct mbuf *mb, caddr_t buf, u_int size, 188129906Sbmilekic void (*freef)(void *, void *), void *args, int flags, int type) 189129906Sbmilekic{ 190151976Sandre KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__)); 191129906Sbmilekic 192151976Sandre if (type != EXT_EXTREF) 193151976Sandre mb->m_ext.ref_cnt = (u_int *)uma_zalloc(zone_ext_refcnt, M_NOWAIT); 194129906Sbmilekic if (mb->m_ext.ref_cnt != NULL) { 195129906Sbmilekic *(mb->m_ext.ref_cnt) = 1; 196129906Sbmilekic mb->m_flags |= (M_EXT | flags); 197129906Sbmilekic mb->m_ext.ext_buf = buf; 198129906Sbmilekic mb->m_data = mb->m_ext.ext_buf; 199129906Sbmilekic mb->m_ext.ext_size = size; 200129906Sbmilekic mb->m_ext.ext_free = freef; 201129906Sbmilekic mb->m_ext.ext_args = args; 202129906Sbmilekic mb->m_ext.ext_type = type; 203129906Sbmilekic } 204129906Sbmilekic} 205129906Sbmilekic 206129906Sbmilekic/* 207129906Sbmilekic * Non-directly-exported function to clean up after mbufs with M_EXT 208151976Sandre * storage attached to them if the reference count hits 1. 209129906Sbmilekic */ 210129906Sbmilekicvoid 211129906Sbmilekicmb_free_ext(struct mbuf *m) 212129906Sbmilekic{ 213151976Sandre KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 214151976Sandre KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 215129906Sbmilekic 216151976Sandre /* Free attached storage if this mbuf is the only reference to it. */ 217151976Sandre if (*(m->m_ext.ref_cnt) == 1 || 218152035Sandre atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 0) { 219151976Sandre switch (m->m_ext.ext_type) { 220152035Sandre case EXT_CLUSTER: /* The packet zone is special. */ 221152035Sandre if (*(m->m_ext.ref_cnt) == 0) 222152035Sandre *(m->m_ext.ref_cnt) = 1; 223151976Sandre uma_zfree(zone_pack, m); 224151976Sandre return; /* Job done. */ 225130289Sbmilekic break; 226151976Sandre case EXT_JUMBO9: 227151976Sandre uma_zfree(zone_jumbo9, m->m_ext.ext_buf); 228151976Sandre break; 229151976Sandre case EXT_JUMBO16: 230151976Sandre uma_zfree(zone_jumbo16, m->m_ext.ext_buf); 231151976Sandre break; 232151976Sandre case EXT_SFBUF: 233151976Sandre case EXT_NET_DRV: 234151976Sandre case EXT_MOD_TYPE: 235151976Sandre case EXT_DISPOSABLE: 236151976Sandre *(m->m_ext.ref_cnt) = 0; 237151976Sandre uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *, 238151976Sandre m->m_ext.ref_cnt)); 239151976Sandre /* FALLTHROUGH */ 240151976Sandre case EXT_EXTREF: 241151976Sandre KASSERT(m->m_ext.ext_free != NULL, 242151976Sandre ("%s: ext_free not set", __func__)); 243141668Sbmilekic (*(m->m_ext.ext_free))(m->m_ext.ext_buf, 244141668Sbmilekic m->m_ext.ext_args); 245151976Sandre break; 246151976Sandre default: 247151976Sandre KASSERT(m->m_ext.ext_type == 0, 248151976Sandre ("%s: unknown ext_type", __func__)); 249141668Sbmilekic } 250141668Sbmilekic } 251151976Sandre /* 252151976Sandre * Free this mbuf back to the mbuf zone with all m_ext 253151976Sandre * information purged. 254151976Sandre */ 255151976Sandre m->m_ext.ext_buf = NULL; 256151976Sandre m->m_ext.ext_free = NULL; 257151976Sandre m->m_ext.ext_args = NULL; 258151976Sandre m->m_ext.ref_cnt = NULL; 259151976Sandre m->m_ext.ext_size = 0; 260151976Sandre m->m_ext.ext_type = 0; 261151976Sandre m->m_flags &= ~M_EXT; 262130357Sbmilekic uma_zfree(zone_mbuf, m); 263129906Sbmilekic} 264129906Sbmilekic 265129906Sbmilekic/* 266151976Sandre * Attach the the cluster from *m to *n, set up m_ext in *n 267151976Sandre * and bump the refcount of the cluster. 268151976Sandre */ 269151976Sandrestatic void 270151976Sandremb_dupcl(struct mbuf *n, struct mbuf *m) 271151976Sandre{ 272151976Sandre KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 273151976Sandre KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 274151976Sandre KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__)); 275151976Sandre 276151976Sandre if (*(m->m_ext.ref_cnt) == 1) 277151976Sandre *(m->m_ext.ref_cnt) += 1; 278151976Sandre else 279151976Sandre atomic_add_int(m->m_ext.ref_cnt, 1); 280151976Sandre n->m_ext.ext_buf = m->m_ext.ext_buf; 281151976Sandre n->m_ext.ext_free = m->m_ext.ext_free; 282151976Sandre n->m_ext.ext_args = m->m_ext.ext_args; 283151976Sandre n->m_ext.ext_size = m->m_ext.ext_size; 284151976Sandre n->m_ext.ref_cnt = m->m_ext.ref_cnt; 285151976Sandre n->m_ext.ext_type = m->m_ext.ext_type; 286151976Sandre n->m_flags |= M_EXT; 287151976Sandre} 288151976Sandre 289151976Sandre/* 290149598Sandre * Clean up mbuf (chain) from any tags and packet headers. 291149647Sandre * If "all" is set then the first mbuf in the chain will be 292149647Sandre * cleaned too. 293149598Sandre */ 294149598Sandrevoid 295149647Sandrem_demote(struct mbuf *m0, int all) 296149598Sandre{ 297149598Sandre struct mbuf *m; 298149598Sandre 299149598Sandre for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) { 300149598Sandre if (m->m_flags & M_PKTHDR) { 301149598Sandre m_tag_delete_chain(m, NULL); 302149598Sandre m->m_flags &= ~M_PKTHDR; 303149598Sandre bzero(&m->m_pkthdr, sizeof(struct pkthdr)); 304149598Sandre } 305149643Sandre if (m->m_type == MT_HEADER) 306149598Sandre m->m_type = MT_DATA; 307149643Sandre if (m != m0 && m->m_nextpkt != NULL) 308149598Sandre m->m_nextpkt = NULL; 309149598Sandre m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST); 310149598Sandre } 311149598Sandre} 312149598Sandre 313149598Sandre/* 314149648Sandre * Sanity checks on mbuf (chain) for use in KASSERT() and general 315149648Sandre * debugging. 316149648Sandre * Returns 0 or panics when bad and 1 on all tests passed. 317149648Sandre * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they 318149648Sandre * blow up later. 319149599Sandre */ 320149599Sandreint 321149599Sandrem_sanity(struct mbuf *m0, int sanitize) 322149599Sandre{ 323149599Sandre struct mbuf *m; 324149599Sandre caddr_t a, b; 325149599Sandre int pktlen = 0; 326149599Sandre 327149599Sandre#define M_SANITY_ACTION(s) return (0) 328149599Sandre/* #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m) */ 329149599Sandre 330149648Sandre for (m = m0; m != NULL; m = m->m_next) { 331149599Sandre /* 332149599Sandre * Basic pointer checks. If any of these fails then some 333149599Sandre * unrelated kernel memory before or after us is trashed. 334149599Sandre * No way to recover from that. 335149599Sandre */ 336149648Sandre a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf : 337149648Sandre ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) : 338149599Sandre (caddr_t)(&m->m_dat)) ); 339149599Sandre b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size : 340149648Sandre ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN))); 341149599Sandre if ((caddr_t)m->m_data < a) 342149599Sandre M_SANITY_ACTION("m_data outside mbuf data range left"); 343149599Sandre if ((caddr_t)m->m_data > b) 344149599Sandre M_SANITY_ACTION("m_data outside mbuf data range right"); 345149599Sandre if ((caddr_t)m->m_data + m->m_len > b) 346149599Sandre M_SANITY_ACTION("m_data + m_len exeeds mbuf space"); 347149648Sandre if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) { 348149599Sandre if ((caddr_t)m->m_pkthdr.header < a || 349149599Sandre (caddr_t)m->m_pkthdr.header > b) 350149599Sandre M_SANITY_ACTION("m_pkthdr.header outside mbuf data range"); 351149599Sandre } 352149599Sandre 353149599Sandre /* m->m_nextpkt may only be set on first mbuf in chain. */ 354149648Sandre if (m != m0 && m->m_nextpkt != NULL) { 355149599Sandre if (sanitize) { 356149599Sandre m_freem(m->m_nextpkt); 357149599Sandre m->m_nextpkt = (struct mbuf *)0xDEADC0DE; 358149599Sandre } else 359149599Sandre M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf"); 360149599Sandre } 361149599Sandre 362149599Sandre /* correct type correlations. */ 363149599Sandre if (m->m_type == MT_HEADER && !(m->m_flags & M_PKTHDR)) { 364149599Sandre if (sanitize) 365149599Sandre m->m_type = MT_DATA; 366149599Sandre else 367149599Sandre M_SANITY_ACTION("MT_HEADER set but not M_PKTHDR"); 368149599Sandre } 369149599Sandre 370149599Sandre /* packet length (not mbuf length!) calculation */ 371149599Sandre if (m0->m_flags & M_PKTHDR) 372149599Sandre pktlen += m->m_len; 373149599Sandre 374149599Sandre /* m_tags may only be attached to first mbuf in chain. */ 375149599Sandre if (m != m0 && m->m_flags & M_PKTHDR && 376149599Sandre !SLIST_EMPTY(&m->m_pkthdr.tags)) { 377149599Sandre if (sanitize) { 378149599Sandre m_tag_delete_chain(m, NULL); 379149599Sandre /* put in 0xDEADC0DE perhaps? */ 380149648Sandre } else 381149599Sandre M_SANITY_ACTION("m_tags on in-chain mbuf"); 382149599Sandre } 383149599Sandre 384149599Sandre /* M_PKTHDR may only be set on first mbuf in chain */ 385149599Sandre if (m != m0 && m->m_flags & M_PKTHDR) { 386149599Sandre if (sanitize) { 387149599Sandre bzero(&m->m_pkthdr, sizeof(m->m_pkthdr)); 388149599Sandre m->m_flags &= ~M_PKTHDR; 389149599Sandre /* put in 0xDEADCODE and leave hdr flag in */ 390149599Sandre } else 391149599Sandre M_SANITY_ACTION("M_PKTHDR on in-chain mbuf"); 392149599Sandre } 393149599Sandre } 394149648Sandre m = m0; 395149648Sandre if (pktlen && pktlen != m->m_pkthdr.len) { 396149599Sandre if (sanitize) 397149648Sandre m->m_pkthdr.len = 0; 398149599Sandre else 399149599Sandre M_SANITY_ACTION("m_pkthdr.len != mbuf chain length"); 400149599Sandre } 401149648Sandre return 1; 402149648Sandre 403149599Sandre#undef M_SANITY_ACTION 404149599Sandre} 405149599Sandre 406149599Sandre 407149599Sandre/* 408108466Ssam * "Move" mbuf pkthdr from "from" to "to". 409100960Srwatson * "from" must have M_PKTHDR set, and "to" must be empty. 410100960Srwatson */ 411100960Srwatsonvoid 412108466Ssamm_move_pkthdr(struct mbuf *to, struct mbuf *from) 413100960Srwatson{ 414100960Srwatson 415100960Srwatson#if 0 416108466Ssam /* see below for why these are not enabled */ 417113255Sdes M_ASSERTPKTHDR(to); 418113487Srwatson /* Note: with MAC, this may not be a good assertion. */ 419108466Ssam KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 420108466Ssam ("m_move_pkthdr: to has tags")); 421100960Srwatson#endif 422101007Srwatson#ifdef MAC 423113487Srwatson /* 424113487Srwatson * XXXMAC: It could be this should also occur for non-MAC? 425113487Srwatson */ 426101007Srwatson if (to->m_flags & M_PKTHDR) 427113487Srwatson m_tag_delete_chain(to, NULL); 428101007Srwatson#endif 429143302Ssam to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 430143302Ssam if ((to->m_flags & M_EXT) == 0) 431143302Ssam to->m_data = to->m_pktdat; 432108466Ssam to->m_pkthdr = from->m_pkthdr; /* especially tags */ 433108466Ssam SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 434108466Ssam from->m_flags &= ~M_PKTHDR; 435108466Ssam} 436108466Ssam 437108466Ssam/* 438108466Ssam * Duplicate "from"'s mbuf pkthdr in "to". 439108466Ssam * "from" must have M_PKTHDR set, and "to" must be empty. 440108466Ssam * In particular, this does a deep copy of the packet tags. 441108466Ssam */ 442108466Ssamint 443108466Ssamm_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 444108466Ssam{ 445108466Ssam 446108466Ssam#if 0 447108466Ssam /* 448108466Ssam * The mbuf allocator only initializes the pkthdr 449108466Ssam * when the mbuf is allocated with MGETHDR. Many users 450108466Ssam * (e.g. m_copy*, m_prepend) use MGET and then 451108466Ssam * smash the pkthdr as needed causing these 452108466Ssam * assertions to trip. For now just disable them. 453108466Ssam */ 454113255Sdes M_ASSERTPKTHDR(to); 455113487Srwatson /* Note: with MAC, this may not be a good assertion. */ 456108466Ssam KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 457108466Ssam#endif 458132488Salfred MBUF_CHECKSLEEP(how); 459108466Ssam#ifdef MAC 460108466Ssam if (to->m_flags & M_PKTHDR) 461113487Srwatson m_tag_delete_chain(to, NULL); 462108466Ssam#endif 463112733Ssilby to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 464112733Ssilby if ((to->m_flags & M_EXT) == 0) 465112733Ssilby to->m_data = to->m_pktdat; 466100960Srwatson to->m_pkthdr = from->m_pkthdr; 467108466Ssam SLIST_INIT(&to->m_pkthdr.tags); 468113480Srwatson return (m_tag_copy_chain(to, from, MBTOM(how))); 469100960Srwatson} 470100960Srwatson 471100960Srwatson/* 4721541Srgrimes * Lesser-used path for M_PREPEND: 4731541Srgrimes * allocate new mbuf to prepend to chain, 4741541Srgrimes * copy junk along. 4751541Srgrimes */ 4761541Srgrimesstruct mbuf * 47772356Sbmilekicm_prepend(struct mbuf *m, int len, int how) 4781541Srgrimes{ 4791541Srgrimes struct mbuf *mn; 4801541Srgrimes 481117770Ssilby if (m->m_flags & M_PKTHDR) 482117770Ssilby MGETHDR(mn, how, m->m_type); 483117770Ssilby else 484117770Ssilby MGET(mn, how, m->m_type); 48572356Sbmilekic if (mn == NULL) { 4861541Srgrimes m_freem(m); 48772356Sbmilekic return (NULL); 4881541Srgrimes } 489113487Srwatson if (m->m_flags & M_PKTHDR) 490108466Ssam M_MOVE_PKTHDR(mn, m); 4911541Srgrimes mn->m_next = m; 4921541Srgrimes m = mn; 4931541Srgrimes if (len < MHLEN) 4941541Srgrimes MH_ALIGN(m, len); 4951541Srgrimes m->m_len = len; 4961541Srgrimes return (m); 4971541Srgrimes} 4981541Srgrimes 4991541Srgrimes/* 5001541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 5011541Srgrimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 502111119Simp * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller. 50354002Sarchie * Note that the copy is read-only, because clusters are not copied, 50454002Sarchie * only their reference counts are incremented. 5051541Srgrimes */ 5061541Srgrimesstruct mbuf * 50772356Sbmilekicm_copym(struct mbuf *m, int off0, int len, int wait) 5081541Srgrimes{ 50972356Sbmilekic struct mbuf *n, **np; 51072356Sbmilekic int off = off0; 5111541Srgrimes struct mbuf *top; 5121541Srgrimes int copyhdr = 0; 5131541Srgrimes 51452201Salfred KASSERT(off >= 0, ("m_copym, negative off %d", off)); 51552201Salfred KASSERT(len >= 0, ("m_copym, negative len %d", len)); 516132488Salfred MBUF_CHECKSLEEP(wait); 5171541Srgrimes if (off == 0 && m->m_flags & M_PKTHDR) 5181541Srgrimes copyhdr = 1; 5191541Srgrimes while (off > 0) { 52052201Salfred KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 5211541Srgrimes if (off < m->m_len) 5221541Srgrimes break; 5231541Srgrimes off -= m->m_len; 5241541Srgrimes m = m->m_next; 5251541Srgrimes } 5261541Srgrimes np = ⊤ 5271541Srgrimes top = 0; 5281541Srgrimes while (len > 0) { 52972356Sbmilekic if (m == NULL) { 53052201Salfred KASSERT(len == M_COPYALL, 53152201Salfred ("m_copym, length > size of mbuf chain")); 5321541Srgrimes break; 5331541Srgrimes } 534117770Ssilby if (copyhdr) 535117770Ssilby MGETHDR(n, wait, m->m_type); 536117770Ssilby else 537117770Ssilby MGET(n, wait, m->m_type); 5381541Srgrimes *np = n; 53972356Sbmilekic if (n == NULL) 5401541Srgrimes goto nospace; 5411541Srgrimes if (copyhdr) { 542108466Ssam if (!m_dup_pkthdr(n, m, wait)) 543108466Ssam goto nospace; 5441541Srgrimes if (len == M_COPYALL) 5451541Srgrimes n->m_pkthdr.len -= off0; 5461541Srgrimes else 5471541Srgrimes n->m_pkthdr.len = len; 5481541Srgrimes copyhdr = 0; 5491541Srgrimes } 5501541Srgrimes n->m_len = min(len, m->m_len - off); 5511541Srgrimes if (m->m_flags & M_EXT) { 5521541Srgrimes n->m_data = m->m_data + off; 553151976Sandre mb_dupcl(n, m); 5541541Srgrimes } else 5551541Srgrimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 556103569Sbmilekic (u_int)n->m_len); 5571541Srgrimes if (len != M_COPYALL) 5581541Srgrimes len -= n->m_len; 5591541Srgrimes off = 0; 5601541Srgrimes m = m->m_next; 5611541Srgrimes np = &n->m_next; 5621541Srgrimes } 56378592Sbmilekic if (top == NULL) 56478592Sbmilekic mbstat.m_mcfail++; /* XXX: No consistency. */ 56578592Sbmilekic 5661541Srgrimes return (top); 5671541Srgrimesnospace: 5681541Srgrimes m_freem(top); 56978592Sbmilekic mbstat.m_mcfail++; /* XXX: No consistency. */ 57072356Sbmilekic return (NULL); 5711541Srgrimes} 5721541Srgrimes 5731541Srgrimes/* 574149602Sandre * Returns mbuf chain with new head for the prepending case. 575149602Sandre * Copies from mbuf (chain) n from off for len to mbuf (chain) m 576149602Sandre * either prepending or appending the data. 577149602Sandre * The resulting mbuf (chain) m is fully writeable. 578149602Sandre * m is destination (is made writeable) 579149602Sandre * n is source, off is offset in source, len is len from offset 580149602Sandre * dir, 0 append, 1 prepend 581149602Sandre * how, wait or nowait 582149602Sandre */ 583149602Sandre 584149602Sandrestatic int 585149602Sandrem_bcopyxxx(void *s, void *t, u_int len) 586149602Sandre{ 587149602Sandre bcopy(s, t, (size_t)len); 588149602Sandre return 0; 589149602Sandre} 590149602Sandre 591149602Sandrestruct mbuf * 592149602Sandrem_copymdata(struct mbuf *m, struct mbuf *n, int off, int len, 593149602Sandre int prep, int how) 594149602Sandre{ 595151976Sandre struct mbuf *mm, *x, *z, *prev = NULL; 596149602Sandre caddr_t p; 597151976Sandre int i, nlen = 0; 598149602Sandre caddr_t buf[MLEN]; 599149602Sandre 600149602Sandre KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source")); 601149602Sandre KASSERT(off >= 0, ("m_copymdata, negative off %d", off)); 602149602Sandre KASSERT(len >= 0, ("m_copymdata, negative len %d", len)); 603149602Sandre KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep)); 604149602Sandre 605151976Sandre mm = m; 606151976Sandre if (!prep) { 607151976Sandre while(mm->m_next) { 608151976Sandre prev = mm; 609151976Sandre mm = mm->m_next; 610149602Sandre } 611149602Sandre } 612149602Sandre for (z = n; z != NULL; z = z->m_next) 613149602Sandre nlen += z->m_len; 614149602Sandre if (len == M_COPYALL) 615149602Sandre len = nlen - off; 616149602Sandre if (off + len > nlen || len < 1) 617149602Sandre return NULL; 618149602Sandre 619151976Sandre if (!M_WRITABLE(mm)) { 620151976Sandre /* XXX: Use proper m_xxx function instead. */ 621151976Sandre x = m_getcl(how, MT_DATA, mm->m_flags); 622151976Sandre if (x == NULL) 623151976Sandre return NULL; 624151976Sandre bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size); 625151976Sandre p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf); 626151976Sandre x->m_data = p; 627151976Sandre mm->m_next = NULL; 628151976Sandre if (mm != m) 629151976Sandre prev->m_next = x; 630151976Sandre m_free(mm); 631151976Sandre mm = x; 632151976Sandre } 633151976Sandre 634149602Sandre /* 635149602Sandre * Append/prepend the data. Allocating mbufs as necessary. 636149602Sandre */ 637149602Sandre /* Shortcut if enough free space in first/last mbuf. */ 638149602Sandre if (!prep && M_TRAILINGSPACE(mm) >= len) { 639149602Sandre m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) + 640149602Sandre mm->m_len); 641149602Sandre mm->m_len += len; 642149602Sandre mm->m_pkthdr.len += len; 643149602Sandre return m; 644149602Sandre } 645149602Sandre if (prep && M_LEADINGSPACE(mm) >= len) { 646149602Sandre mm->m_data = mtod(mm, caddr_t) - len; 647149602Sandre m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t)); 648149602Sandre mm->m_len += len; 649149602Sandre mm->m_pkthdr.len += len; 650149602Sandre return mm; 651149602Sandre } 652149602Sandre 653149602Sandre /* Expand first/last mbuf to cluster if possible. */ 654149602Sandre if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) { 655149602Sandre bcopy(mm->m_data, &buf, mm->m_len); 656149602Sandre m_clget(mm, how); 657149602Sandre if (!(mm->m_flags & M_EXT)) 658149602Sandre return NULL; 659149602Sandre bcopy(&buf, mm->m_ext.ext_buf, mm->m_len); 660149602Sandre mm->m_data = mm->m_ext.ext_buf; 661149602Sandre mm->m_pkthdr.header = NULL; 662149602Sandre } 663149602Sandre if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) { 664149602Sandre bcopy(mm->m_data, &buf, mm->m_len); 665149602Sandre m_clget(mm, how); 666149602Sandre if (!(mm->m_flags & M_EXT)) 667149602Sandre return NULL; 668149602Sandre bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf + 669149602Sandre mm->m_ext.ext_size - mm->m_len, mm->m_len); 670149602Sandre mm->m_data = (caddr_t)mm->m_ext.ext_buf + 671149602Sandre mm->m_ext.ext_size - mm->m_len; 672149602Sandre mm->m_pkthdr.header = NULL; 673149602Sandre } 674149602Sandre 675149602Sandre /* Append/prepend as many mbuf (clusters) as necessary to fit len. */ 676149602Sandre if (!prep && len > M_TRAILINGSPACE(mm)) { 677149602Sandre if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA)) 678149602Sandre return NULL; 679149602Sandre } 680149602Sandre if (prep && len > M_LEADINGSPACE(mm)) { 681149602Sandre if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA))) 682149602Sandre return NULL; 683149602Sandre i = 0; 684149602Sandre for (x = z; x != NULL; x = x->m_next) { 685149602Sandre i += x->m_flags & M_EXT ? x->m_ext.ext_size : 686149602Sandre (x->m_flags & M_PKTHDR ? MHLEN : MLEN); 687149602Sandre if (!x->m_next) 688149602Sandre break; 689149602Sandre } 690149602Sandre z->m_data += i - len; 691149602Sandre m_move_pkthdr(mm, z); 692149602Sandre x->m_next = mm; 693149602Sandre mm = z; 694149602Sandre } 695149602Sandre 696149602Sandre /* Seek to start position in source mbuf. Optimization for long chains. */ 697149602Sandre while (off > 0) { 698149602Sandre if (off < n->m_len) 699149602Sandre break; 700149602Sandre off -= n->m_len; 701149602Sandre n = n->m_next; 702149602Sandre } 703149602Sandre 704149602Sandre /* Copy data into target mbuf. */ 705149602Sandre z = mm; 706149602Sandre while (len > 0) { 707149602Sandre KASSERT(z != NULL, ("m_copymdata, falling off target edge")); 708149602Sandre i = M_TRAILINGSPACE(z); 709149602Sandre m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len); 710149602Sandre z->m_len += i; 711149602Sandre /* fixup pkthdr.len if necessary */ 712149602Sandre if ((prep ? mm : m)->m_flags & M_PKTHDR) 713149602Sandre (prep ? mm : m)->m_pkthdr.len += i; 714149602Sandre off += i; 715149602Sandre len -= i; 716149602Sandre z = z->m_next; 717149602Sandre } 718149602Sandre return (prep ? mm : m); 719149602Sandre} 720149602Sandre 721149602Sandre/* 72215689Swollman * Copy an entire packet, including header (which must be present). 72315689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 72454002Sarchie * Note that the copy is read-only, because clusters are not copied, 72554002Sarchie * only their reference counts are incremented. 72672750Sluigi * Preserve alignment of the first mbuf so if the creator has left 72772750Sluigi * some room at the beginning (e.g. for inserting protocol headers) 72872750Sluigi * the copies still have the room available. 72915689Swollman */ 73015689Swollmanstruct mbuf * 73172356Sbmilekicm_copypacket(struct mbuf *m, int how) 73215689Swollman{ 73315689Swollman struct mbuf *top, *n, *o; 73415689Swollman 735132488Salfred MBUF_CHECKSLEEP(how); 73615689Swollman MGET(n, how, m->m_type); 73715689Swollman top = n; 73872356Sbmilekic if (n == NULL) 73915689Swollman goto nospace; 74015689Swollman 741108466Ssam if (!m_dup_pkthdr(n, m, how)) 742108466Ssam goto nospace; 74315689Swollman n->m_len = m->m_len; 74415689Swollman if (m->m_flags & M_EXT) { 74515689Swollman n->m_data = m->m_data; 746151976Sandre mb_dupcl(n, m); 74715689Swollman } else { 74872750Sluigi n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 74915689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 75015689Swollman } 75115689Swollman 75215689Swollman m = m->m_next; 75315689Swollman while (m) { 75415689Swollman MGET(o, how, m->m_type); 75572356Sbmilekic if (o == NULL) 75615689Swollman goto nospace; 75715689Swollman 75815689Swollman n->m_next = o; 75915689Swollman n = n->m_next; 76015689Swollman 76115689Swollman n->m_len = m->m_len; 76215689Swollman if (m->m_flags & M_EXT) { 76315689Swollman n->m_data = m->m_data; 764151976Sandre mb_dupcl(n, m); 76515689Swollman } else { 76615689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 76715689Swollman } 76815689Swollman 76915689Swollman m = m->m_next; 77015689Swollman } 77115689Swollman return top; 77215689Swollmannospace: 77315689Swollman m_freem(top); 77478592Sbmilekic mbstat.m_mcfail++; /* XXX: No consistency. */ 77572356Sbmilekic return (NULL); 77615689Swollman} 77715689Swollman 77815689Swollman/* 7791541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 7801541Srgrimes * continuing for "len" bytes, into the indicated buffer. 7811541Srgrimes */ 7821549Srgrimesvoid 78381907Sjulianm_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 7841541Srgrimes{ 785103569Sbmilekic u_int count; 7861541Srgrimes 78752201Salfred KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 78852201Salfred KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 7891541Srgrimes while (off > 0) { 79052201Salfred KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 7911541Srgrimes if (off < m->m_len) 7921541Srgrimes break; 7931541Srgrimes off -= m->m_len; 7941541Srgrimes m = m->m_next; 7951541Srgrimes } 7961541Srgrimes while (len > 0) { 79752201Salfred KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 7981541Srgrimes count = min(m->m_len - off, len); 7991541Srgrimes bcopy(mtod(m, caddr_t) + off, cp, count); 8001541Srgrimes len -= count; 8011541Srgrimes cp += count; 8021541Srgrimes off = 0; 8031541Srgrimes m = m->m_next; 8041541Srgrimes } 8051541Srgrimes} 8061541Srgrimes 8071541Srgrimes/* 80854002Sarchie * Copy a packet header mbuf chain into a completely new chain, including 80954002Sarchie * copying any mbuf clusters. Use this instead of m_copypacket() when 81054002Sarchie * you need a writable copy of an mbuf chain. 81154002Sarchie */ 81254002Sarchiestruct mbuf * 81372356Sbmilekicm_dup(struct mbuf *m, int how) 81454002Sarchie{ 81554002Sarchie struct mbuf **p, *top = NULL; 81654002Sarchie int remain, moff, nsize; 81754002Sarchie 818132488Salfred MBUF_CHECKSLEEP(how); 81954002Sarchie /* Sanity check */ 82054002Sarchie if (m == NULL) 82172356Sbmilekic return (NULL); 822113255Sdes M_ASSERTPKTHDR(m); 82354002Sarchie 82454002Sarchie /* While there's more data, get a new mbuf, tack it on, and fill it */ 82554002Sarchie remain = m->m_pkthdr.len; 82654002Sarchie moff = 0; 82754002Sarchie p = ⊤ 82854002Sarchie while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 82954002Sarchie struct mbuf *n; 83054002Sarchie 83154002Sarchie /* Get the next new mbuf */ 832129906Sbmilekic if (remain >= MINCLSIZE) { 833129906Sbmilekic n = m_getcl(how, m->m_type, 0); 834129906Sbmilekic nsize = MCLBYTES; 835129906Sbmilekic } else { 836129906Sbmilekic n = m_get(how, m->m_type); 837129906Sbmilekic nsize = MLEN; 838129906Sbmilekic } 83954002Sarchie if (n == NULL) 84054002Sarchie goto nospace; 841129906Sbmilekic 842129906Sbmilekic if (top == NULL) { /* First one, must be PKTHDR */ 843129906Sbmilekic if (!m_dup_pkthdr(n, m, how)) { 844129906Sbmilekic m_free(n); 845108466Ssam goto nospace; 846129906Sbmilekic } 84754002Sarchie nsize = MHLEN; 84854002Sarchie } 84954002Sarchie n->m_len = 0; 85054002Sarchie 85154002Sarchie /* Link it into the new chain */ 85254002Sarchie *p = n; 85354002Sarchie p = &n->m_next; 85454002Sarchie 85554002Sarchie /* Copy data from original mbuf(s) into new mbuf */ 85654002Sarchie while (n->m_len < nsize && m != NULL) { 85754002Sarchie int chunk = min(nsize - n->m_len, m->m_len - moff); 85854002Sarchie 85954002Sarchie bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 86054002Sarchie moff += chunk; 86154002Sarchie n->m_len += chunk; 86254002Sarchie remain -= chunk; 86354002Sarchie if (moff == m->m_len) { 86454002Sarchie m = m->m_next; 86554002Sarchie moff = 0; 86654002Sarchie } 86754002Sarchie } 86854002Sarchie 86954002Sarchie /* Check correct total mbuf length */ 87054002Sarchie KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 87187594Sobrien ("%s: bogus m_pkthdr.len", __func__)); 87254002Sarchie } 87354002Sarchie return (top); 87454002Sarchie 87554002Sarchienospace: 87654002Sarchie m_freem(top); 87778592Sbmilekic mbstat.m_mcfail++; /* XXX: No consistency. */ 87872356Sbmilekic return (NULL); 87954002Sarchie} 88054002Sarchie 88154002Sarchie/* 8821541Srgrimes * Concatenate mbuf chain n to m. 8831541Srgrimes * Both chains must be of the same type (e.g. MT_DATA). 8841541Srgrimes * Any m_pkthdr is not updated. 8851541Srgrimes */ 8861549Srgrimesvoid 88772356Sbmilekicm_cat(struct mbuf *m, struct mbuf *n) 8881541Srgrimes{ 8891541Srgrimes while (m->m_next) 8901541Srgrimes m = m->m_next; 8911541Srgrimes while (n) { 8921541Srgrimes if (m->m_flags & M_EXT || 8931541Srgrimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 8941541Srgrimes /* just join the two chains */ 8951541Srgrimes m->m_next = n; 8961541Srgrimes return; 8971541Srgrimes } 8981541Srgrimes /* splat the data from one into the other */ 8991541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 9001541Srgrimes (u_int)n->m_len); 9011541Srgrimes m->m_len += n->m_len; 9021541Srgrimes n = m_free(n); 9031541Srgrimes } 9041541Srgrimes} 9051541Srgrimes 9061549Srgrimesvoid 90772356Sbmilekicm_adj(struct mbuf *mp, int req_len) 9081541Srgrimes{ 90972356Sbmilekic int len = req_len; 91072356Sbmilekic struct mbuf *m; 91172356Sbmilekic int count; 9121541Srgrimes 9131541Srgrimes if ((m = mp) == NULL) 9141541Srgrimes return; 9151541Srgrimes if (len >= 0) { 9161541Srgrimes /* 9171541Srgrimes * Trim from head. 9181541Srgrimes */ 9191541Srgrimes while (m != NULL && len > 0) { 9201541Srgrimes if (m->m_len <= len) { 9211541Srgrimes len -= m->m_len; 9221541Srgrimes m->m_len = 0; 9231541Srgrimes m = m->m_next; 9241541Srgrimes } else { 9251541Srgrimes m->m_len -= len; 9261541Srgrimes m->m_data += len; 9271541Srgrimes len = 0; 9281541Srgrimes } 9291541Srgrimes } 9301541Srgrimes m = mp; 9311541Srgrimes if (mp->m_flags & M_PKTHDR) 9321541Srgrimes m->m_pkthdr.len -= (req_len - len); 9331541Srgrimes } else { 9341541Srgrimes /* 9351541Srgrimes * Trim from tail. Scan the mbuf chain, 9361541Srgrimes * calculating its length and finding the last mbuf. 9371541Srgrimes * If the adjustment only affects this mbuf, then just 9381541Srgrimes * adjust and return. Otherwise, rescan and truncate 9391541Srgrimes * after the remaining size. 9401541Srgrimes */ 9411541Srgrimes len = -len; 9421541Srgrimes count = 0; 9431541Srgrimes for (;;) { 9441541Srgrimes count += m->m_len; 9451541Srgrimes if (m->m_next == (struct mbuf *)0) 9461541Srgrimes break; 9471541Srgrimes m = m->m_next; 9481541Srgrimes } 9491541Srgrimes if (m->m_len >= len) { 9501541Srgrimes m->m_len -= len; 9511541Srgrimes if (mp->m_flags & M_PKTHDR) 9521541Srgrimes mp->m_pkthdr.len -= len; 9531541Srgrimes return; 9541541Srgrimes } 9551541Srgrimes count -= len; 9561541Srgrimes if (count < 0) 9571541Srgrimes count = 0; 9581541Srgrimes /* 9591541Srgrimes * Correct length for chain is "count". 9601541Srgrimes * Find the mbuf with last data, adjust its length, 9611541Srgrimes * and toss data from remaining mbufs on chain. 9621541Srgrimes */ 9631541Srgrimes m = mp; 9641541Srgrimes if (m->m_flags & M_PKTHDR) 9651541Srgrimes m->m_pkthdr.len = count; 9661541Srgrimes for (; m; m = m->m_next) { 9671541Srgrimes if (m->m_len >= count) { 9681541Srgrimes m->m_len = count; 969142350Ssam if (m->m_next != NULL) { 970142350Ssam m_freem(m->m_next); 971142350Ssam m->m_next = NULL; 972142350Ssam } 9731541Srgrimes break; 9741541Srgrimes } 9751541Srgrimes count -= m->m_len; 9761541Srgrimes } 9771541Srgrimes } 9781541Srgrimes} 9791541Srgrimes 9801541Srgrimes/* 9811541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous 9821541Srgrimes * and in the data area of an mbuf (so that mtod and dtom 9831541Srgrimes * will work for a structure of size len). Returns the resulting 9841541Srgrimes * mbuf chain on success, frees it and returns null on failure. 9851541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the 9861541Srgrimes * contiguous region in an attempt to avoid being called next time. 9871541Srgrimes */ 9881541Srgrimesstruct mbuf * 98972356Sbmilekicm_pullup(struct mbuf *n, int len) 9901541Srgrimes{ 99172356Sbmilekic struct mbuf *m; 99272356Sbmilekic int count; 9931541Srgrimes int space; 9941541Srgrimes 9951541Srgrimes /* 9961541Srgrimes * If first mbuf has no cluster, and has room for len bytes 9971541Srgrimes * without shifting current data, pullup into it, 9981541Srgrimes * otherwise allocate a new mbuf to prepend to the chain. 9991541Srgrimes */ 10001541Srgrimes if ((n->m_flags & M_EXT) == 0 && 10011541Srgrimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 10021541Srgrimes if (n->m_len >= len) 10031541Srgrimes return (n); 10041541Srgrimes m = n; 10051541Srgrimes n = n->m_next; 10061541Srgrimes len -= m->m_len; 10071541Srgrimes } else { 10081541Srgrimes if (len > MHLEN) 10091541Srgrimes goto bad; 1010111119Simp MGET(m, M_DONTWAIT, n->m_type); 101172356Sbmilekic if (m == NULL) 10121541Srgrimes goto bad; 10131541Srgrimes m->m_len = 0; 1014108466Ssam if (n->m_flags & M_PKTHDR) 1015108466Ssam M_MOVE_PKTHDR(m, n); 10161541Srgrimes } 10171541Srgrimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 10181541Srgrimes do { 10191541Srgrimes count = min(min(max(len, max_protohdr), space), n->m_len); 10201541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 1021103569Sbmilekic (u_int)count); 10221541Srgrimes len -= count; 10231541Srgrimes m->m_len += count; 10241541Srgrimes n->m_len -= count; 10251541Srgrimes space -= count; 10261541Srgrimes if (n->m_len) 10271541Srgrimes n->m_data += count; 10281541Srgrimes else 10291541Srgrimes n = m_free(n); 10301541Srgrimes } while (len > 0 && n); 10311541Srgrimes if (len > 0) { 10321541Srgrimes (void) m_free(m); 10331541Srgrimes goto bad; 10341541Srgrimes } 10351541Srgrimes m->m_next = n; 10361541Srgrimes return (m); 10371541Srgrimesbad: 10381541Srgrimes m_freem(n); 103978592Sbmilekic mbstat.m_mpfail++; /* XXX: No consistency. */ 104072356Sbmilekic return (NULL); 10411541Srgrimes} 10421541Srgrimes 10431541Srgrimes/* 1044143761Sjmg * Like m_pullup(), except a new mbuf is always allocated, and we allow 1045143761Sjmg * the amount of empty space before the data in the new mbuf to be specified 1046143761Sjmg * (in the event that the caller expects to prepend later). 1047143761Sjmg */ 1048143761Sjmgint MSFail; 1049143761Sjmg 1050143761Sjmgstruct mbuf * 1051143761Sjmgm_copyup(struct mbuf *n, int len, int dstoff) 1052143761Sjmg{ 1053143761Sjmg struct mbuf *m; 1054143761Sjmg int count, space; 1055143761Sjmg 1056143761Sjmg if (len > (MHLEN - dstoff)) 1057143761Sjmg goto bad; 1058143761Sjmg MGET(m, M_DONTWAIT, n->m_type); 1059143761Sjmg if (m == NULL) 1060143761Sjmg goto bad; 1061143761Sjmg m->m_len = 0; 1062143761Sjmg if (n->m_flags & M_PKTHDR) 1063143761Sjmg M_MOVE_PKTHDR(m, n); 1064143761Sjmg m->m_data += dstoff; 1065143761Sjmg space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1066143761Sjmg do { 1067143761Sjmg count = min(min(max(len, max_protohdr), space), n->m_len); 1068143761Sjmg memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), 1069143761Sjmg (unsigned)count); 1070143761Sjmg len -= count; 1071143761Sjmg m->m_len += count; 1072143761Sjmg n->m_len -= count; 1073143761Sjmg space -= count; 1074143761Sjmg if (n->m_len) 1075143761Sjmg n->m_data += count; 1076143761Sjmg else 1077143761Sjmg n = m_free(n); 1078143761Sjmg } while (len > 0 && n); 1079143761Sjmg if (len > 0) { 1080143761Sjmg (void) m_free(m); 1081143761Sjmg goto bad; 1082143761Sjmg } 1083143761Sjmg m->m_next = n; 1084143761Sjmg return (m); 1085143761Sjmg bad: 1086143761Sjmg m_freem(n); 1087143761Sjmg MSFail++; 1088143761Sjmg return (NULL); 1089143761Sjmg} 1090143761Sjmg 1091143761Sjmg/* 10921541Srgrimes * Partition an mbuf chain in two pieces, returning the tail -- 10931541Srgrimes * all but the first len0 bytes. In case of failure, it returns NULL and 10941541Srgrimes * attempts to restore the chain to its original state. 109597681Sarchie * 109697681Sarchie * Note that the resulting mbufs might be read-only, because the new 109797681Sarchie * mbuf can end up sharing an mbuf cluster with the original mbuf if 109897681Sarchie * the "breaking point" happens to lie within a cluster mbuf. Use the 109997681Sarchie * M_WRITABLE() macro to check for this case. 11001541Srgrimes */ 11011541Srgrimesstruct mbuf * 110272356Sbmilekicm_split(struct mbuf *m0, int len0, int wait) 11031541Srgrimes{ 110472356Sbmilekic struct mbuf *m, *n; 1105103569Sbmilekic u_int len = len0, remain; 11061541Srgrimes 1107132488Salfred MBUF_CHECKSLEEP(wait); 11081541Srgrimes for (m = m0; m && len > m->m_len; m = m->m_next) 11091541Srgrimes len -= m->m_len; 111072356Sbmilekic if (m == NULL) 111172356Sbmilekic return (NULL); 11121541Srgrimes remain = m->m_len - len; 11131541Srgrimes if (m0->m_flags & M_PKTHDR) { 11141541Srgrimes MGETHDR(n, wait, m0->m_type); 111572356Sbmilekic if (n == NULL) 111672356Sbmilekic return (NULL); 11171541Srgrimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 11181541Srgrimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 11191541Srgrimes m0->m_pkthdr.len = len0; 11201541Srgrimes if (m->m_flags & M_EXT) 11211541Srgrimes goto extpacket; 11221541Srgrimes if (remain > MHLEN) { 11231541Srgrimes /* m can't be the lead packet */ 11241541Srgrimes MH_ALIGN(n, 0); 11251541Srgrimes n->m_next = m_split(m, len, wait); 112672356Sbmilekic if (n->m_next == NULL) { 11271541Srgrimes (void) m_free(n); 112872356Sbmilekic return (NULL); 112994471Shsu } else { 113094471Shsu n->m_len = 0; 11311541Srgrimes return (n); 113294471Shsu } 11331541Srgrimes } else 11341541Srgrimes MH_ALIGN(n, remain); 11351541Srgrimes } else if (remain == 0) { 11361541Srgrimes n = m->m_next; 113772356Sbmilekic m->m_next = NULL; 11381541Srgrimes return (n); 11391541Srgrimes } else { 11401541Srgrimes MGET(n, wait, m->m_type); 114172356Sbmilekic if (n == NULL) 114272356Sbmilekic return (NULL); 11431541Srgrimes M_ALIGN(n, remain); 11441541Srgrimes } 11451541Srgrimesextpacket: 11461541Srgrimes if (m->m_flags & M_EXT) { 11471541Srgrimes n->m_data = m->m_data + len; 1148151976Sandre mb_dupcl(n, m); 11491541Srgrimes } else { 11501541Srgrimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 11511541Srgrimes } 11521541Srgrimes n->m_len = remain; 11531541Srgrimes m->m_len = len; 11541541Srgrimes n->m_next = m->m_next; 115572356Sbmilekic m->m_next = NULL; 11561541Srgrimes return (n); 11571541Srgrimes} 11581541Srgrimes/* 11591541Srgrimes * Routine to copy from device local memory into mbufs. 116078508Sbmilekic * Note that `off' argument is offset into first mbuf of target chain from 116178508Sbmilekic * which to begin copying the data to. 11621541Srgrimes */ 11631541Srgrimesstruct mbuf * 116478508Sbmilekicm_devget(char *buf, int totlen, int off, struct ifnet *ifp, 116572356Sbmilekic void (*copy)(char *from, caddr_t to, u_int len)) 11661541Srgrimes{ 116772356Sbmilekic struct mbuf *m; 1168129906Sbmilekic struct mbuf *top = NULL, **mp = ⊤ 116978508Sbmilekic int len; 11701541Srgrimes 117178508Sbmilekic if (off < 0 || off > MHLEN) 117278508Sbmilekic return (NULL); 117378508Sbmilekic 1174129906Sbmilekic while (totlen > 0) { 1175129906Sbmilekic if (top == NULL) { /* First one, must be PKTHDR */ 1176129906Sbmilekic if (totlen + off >= MINCLSIZE) { 1177129906Sbmilekic m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1178129906Sbmilekic len = MCLBYTES; 1179129906Sbmilekic } else { 1180129906Sbmilekic m = m_gethdr(M_DONTWAIT, MT_DATA); 1181129906Sbmilekic len = MHLEN; 11821541Srgrimes 1183129906Sbmilekic /* Place initial small packet/header at end of mbuf */ 1184129906Sbmilekic if (m && totlen + off + max_linkhdr <= MLEN) { 1185129906Sbmilekic m->m_data += max_linkhdr; 1186129906Sbmilekic len -= max_linkhdr; 1187129906Sbmilekic } 1188129906Sbmilekic } 1189129906Sbmilekic if (m == NULL) 1190129906Sbmilekic return NULL; 1191129906Sbmilekic m->m_pkthdr.rcvif = ifp; 1192129906Sbmilekic m->m_pkthdr.len = totlen; 1193129906Sbmilekic } else { 1194129906Sbmilekic if (totlen + off >= MINCLSIZE) { 1195129906Sbmilekic m = m_getcl(M_DONTWAIT, MT_DATA, 0); 1196129906Sbmilekic len = MCLBYTES; 1197129906Sbmilekic } else { 1198129906Sbmilekic m = m_get(M_DONTWAIT, MT_DATA); 1199129906Sbmilekic len = MLEN; 1200129906Sbmilekic } 120172356Sbmilekic if (m == NULL) { 12021541Srgrimes m_freem(top); 1203129906Sbmilekic return NULL; 12041541Srgrimes } 12051541Srgrimes } 120678508Sbmilekic if (off) { 120778508Sbmilekic m->m_data += off; 120878508Sbmilekic len -= off; 120978508Sbmilekic off = 0; 121078508Sbmilekic } 121178508Sbmilekic m->m_len = len = min(totlen, len); 12121541Srgrimes if (copy) 1213103569Sbmilekic copy(buf, mtod(m, caddr_t), (u_int)len); 12141541Srgrimes else 1215103569Sbmilekic bcopy(buf, mtod(m, caddr_t), (u_int)len); 121678508Sbmilekic buf += len; 12171541Srgrimes *mp = m; 12181541Srgrimes mp = &m->m_next; 12191541Srgrimes totlen -= len; 12201541Srgrimes } 12211541Srgrimes return (top); 12221541Srgrimes} 12233352Sphk 12243352Sphk/* 12253352Sphk * Copy data from a buffer back into the indicated mbuf chain, 12263352Sphk * starting "off" bytes from the beginning, extending the mbuf 12273352Sphk * chain if necessary. 12283352Sphk */ 12293352Sphkvoid 1230128402Sluigim_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp) 12313352Sphk{ 123272356Sbmilekic int mlen; 123372356Sbmilekic struct mbuf *m = m0, *n; 12343352Sphk int totlen = 0; 12353352Sphk 123672356Sbmilekic if (m0 == NULL) 12373352Sphk return; 12383352Sphk while (off > (mlen = m->m_len)) { 12393352Sphk off -= mlen; 12403352Sphk totlen += mlen; 124172356Sbmilekic if (m->m_next == NULL) { 1242129906Sbmilekic n = m_get(M_DONTWAIT, m->m_type); 124372356Sbmilekic if (n == NULL) 12443352Sphk goto out; 1245129906Sbmilekic bzero(mtod(n, caddr_t), MLEN); 12463352Sphk n->m_len = min(MLEN, len + off); 12473352Sphk m->m_next = n; 12483352Sphk } 12493352Sphk m = m->m_next; 12503352Sphk } 12513352Sphk while (len > 0) { 12523352Sphk mlen = min (m->m_len - off, len); 1253103569Sbmilekic bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 12543352Sphk cp += mlen; 12553352Sphk len -= mlen; 12563352Sphk mlen += off; 12573352Sphk off = 0; 12583352Sphk totlen += mlen; 12593352Sphk if (len == 0) 12603352Sphk break; 126172356Sbmilekic if (m->m_next == NULL) { 1262111119Simp n = m_get(M_DONTWAIT, m->m_type); 126372356Sbmilekic if (n == NULL) 12643352Sphk break; 12653352Sphk n->m_len = min(MLEN, len); 12663352Sphk m->m_next = n; 12673352Sphk } 12683352Sphk m = m->m_next; 12693352Sphk } 12703352Sphkout: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 12713352Sphk m->m_pkthdr.len = totlen; 12723352Sphk} 127352756Sphk 1274123557Sbms/* 1275138541Ssam * Append the specified data to the indicated mbuf chain, 1276138541Ssam * Extend the mbuf chain if the new data does not fit in 1277138541Ssam * existing space. 1278138541Ssam * 1279138541Ssam * Return 1 if able to complete the job; otherwise 0. 1280138541Ssam */ 1281138541Ssamint 1282138541Ssamm_append(struct mbuf *m0, int len, c_caddr_t cp) 1283138541Ssam{ 1284138541Ssam struct mbuf *m, *n; 1285138541Ssam int remainder, space; 1286138541Ssam 1287138541Ssam for (m = m0; m->m_next != NULL; m = m->m_next) 1288138541Ssam ; 1289138541Ssam remainder = len; 1290138541Ssam space = M_TRAILINGSPACE(m); 1291138541Ssam if (space > 0) { 1292138541Ssam /* 1293138541Ssam * Copy into available space. 1294138541Ssam */ 1295138541Ssam if (space > remainder) 1296138541Ssam space = remainder; 1297138541Ssam bcopy(cp, mtod(m, caddr_t) + m->m_len, space); 1298138541Ssam m->m_len += space; 1299138541Ssam cp += space, remainder -= space; 1300138541Ssam } 1301138541Ssam while (remainder > 0) { 1302138541Ssam /* 1303138541Ssam * Allocate a new mbuf; could check space 1304138541Ssam * and allocate a cluster instead. 1305138541Ssam */ 1306138541Ssam n = m_get(M_DONTWAIT, m->m_type); 1307138541Ssam if (n == NULL) 1308138541Ssam break; 1309138541Ssam n->m_len = min(MLEN, remainder); 1310138894Ssam bcopy(cp, mtod(n, caddr_t), n->m_len); 1311138894Ssam cp += n->m_len, remainder -= n->m_len; 1312138541Ssam m->m_next = n; 1313138541Ssam m = n; 1314138541Ssam } 1315138541Ssam if (m0->m_flags & M_PKTHDR) 1316138541Ssam m0->m_pkthdr.len += len - remainder; 1317138541Ssam return (remainder == 0); 1318138541Ssam} 1319138541Ssam 1320138541Ssam/* 1321123557Sbms * Apply function f to the data in an mbuf chain starting "off" bytes from 1322123557Sbms * the beginning, continuing for "len" bytes. 1323123557Sbms */ 1324123557Sbmsint 1325123557Sbmsm_apply(struct mbuf *m, int off, int len, 1326123564Sbms int (*f)(void *, void *, u_int), void *arg) 1327123557Sbms{ 1328123564Sbms u_int count; 1329123557Sbms int rval; 1330123557Sbms 1331123557Sbms KASSERT(off >= 0, ("m_apply, negative off %d", off)); 1332123557Sbms KASSERT(len >= 0, ("m_apply, negative len %d", len)); 1333123557Sbms while (off > 0) { 1334123557Sbms KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1335123557Sbms if (off < m->m_len) 1336123557Sbms break; 1337123557Sbms off -= m->m_len; 1338123557Sbms m = m->m_next; 1339123557Sbms } 1340123557Sbms while (len > 0) { 1341123557Sbms KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1342123557Sbms count = min(m->m_len - off, len); 1343123557Sbms rval = (*f)(arg, mtod(m, caddr_t) + off, count); 1344123557Sbms if (rval) 1345123557Sbms return (rval); 1346123557Sbms len -= count; 1347123557Sbms off = 0; 1348123557Sbms m = m->m_next; 1349123557Sbms } 1350123557Sbms return (0); 1351123557Sbms} 1352123557Sbms 1353123557Sbms/* 1354123557Sbms * Return a pointer to mbuf/offset of location in mbuf chain. 1355123557Sbms */ 1356123557Sbmsstruct mbuf * 1357123557Sbmsm_getptr(struct mbuf *m, int loc, int *off) 1358123557Sbms{ 1359123557Sbms 1360123557Sbms while (loc >= 0) { 1361123564Sbms /* Normal end of search. */ 1362123557Sbms if (m->m_len > loc) { 1363123557Sbms *off = loc; 1364123557Sbms return (m); 1365123557Sbms } else { 1366123557Sbms loc -= m->m_len; 1367123557Sbms if (m->m_next == NULL) { 1368123557Sbms if (loc == 0) { 1369123564Sbms /* Point at the end of valid data. */ 1370123557Sbms *off = m->m_len; 1371123557Sbms return (m); 1372123564Sbms } 1373123564Sbms return (NULL); 1374123564Sbms } 1375123564Sbms m = m->m_next; 1376123557Sbms } 1377123557Sbms } 1378123557Sbms return (NULL); 1379123557Sbms} 1380123557Sbms 138152756Sphkvoid 1382135904Sjmgm_print(const struct mbuf *m, int maxlen) 138352756Sphk{ 138452756Sphk int len; 1385135904Sjmg int pdata; 138654906Seivind const struct mbuf *m2; 138752756Sphk 1388135904Sjmg if (m->m_flags & M_PKTHDR) 1389135904Sjmg len = m->m_pkthdr.len; 1390135904Sjmg else 1391135904Sjmg len = -1; 139252756Sphk m2 = m; 1393135904Sjmg while (m2 != NULL && (len == -1 || len)) { 1394135904Sjmg pdata = m2->m_len; 1395135904Sjmg if (maxlen != -1 && pdata > maxlen) 1396135904Sjmg pdata = maxlen; 1397135904Sjmg printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len, 1398135904Sjmg m2->m_next, m2->m_flags, "\20\20freelist\17skipfw" 1399135904Sjmg "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly" 1400135904Sjmg "\3eor\2pkthdr\1ext", pdata ? "" : "\n"); 1401135904Sjmg if (pdata) 1402135904Sjmg printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-"); 1403135904Sjmg if (len != -1) 1404135904Sjmg len -= m2->m_len; 140552756Sphk m2 = m2->m_next; 140652756Sphk } 1407135904Sjmg if (len > 0) 1408135904Sjmg printf("%d bytes unaccounted for.\n", len); 140952756Sphk return; 141052756Sphk} 1411103540Sphk 1412103569Sbmilekicu_int 1413103540Sphkm_fixhdr(struct mbuf *m0) 1414103540Sphk{ 1415103569Sbmilekic u_int len; 1416103540Sphk 1417103544Sphk len = m_length(m0, NULL); 1418103544Sphk m0->m_pkthdr.len = len; 1419103544Sphk return (len); 1420103544Sphk} 1421103544Sphk 1422103569Sbmilekicu_int 1423103544Sphkm_length(struct mbuf *m0, struct mbuf **last) 1424103544Sphk{ 1425103544Sphk struct mbuf *m; 1426103569Sbmilekic u_int len; 1427103544Sphk 1428103544Sphk len = 0; 1429103544Sphk for (m = m0; m != NULL; m = m->m_next) { 1430103540Sphk len += m->m_len; 1431103544Sphk if (m->m_next == NULL) 1432103544Sphk break; 1433103540Sphk } 1434103544Sphk if (last != NULL) 1435103544Sphk *last = m; 1436103544Sphk return (len); 1437103540Sphk} 1438112777Ssilby 1439112777Ssilby/* 1440112777Ssilby * Defragment a mbuf chain, returning the shortest possible 1441112777Ssilby * chain of mbufs and clusters. If allocation fails and 1442112777Ssilby * this cannot be completed, NULL will be returned, but 1443112777Ssilby * the passed in chain will be unchanged. Upon success, 1444112777Ssilby * the original chain will be freed, and the new chain 1445112777Ssilby * will be returned. 1446112777Ssilby * 1447112777Ssilby * If a non-packet header is passed in, the original 1448112777Ssilby * mbuf (chain?) will be returned unharmed. 1449112777Ssilby */ 1450112777Ssilbystruct mbuf * 1451112777Ssilbym_defrag(struct mbuf *m0, int how) 1452112777Ssilby{ 1453125472Ssilby struct mbuf *m_new = NULL, *m_final = NULL; 1454125472Ssilby int progress = 0, length; 1455112777Ssilby 1456132488Salfred MBUF_CHECKSLEEP(how); 1457112777Ssilby if (!(m0->m_flags & M_PKTHDR)) 1458112777Ssilby return (m0); 1459112777Ssilby 1460117770Ssilby m_fixhdr(m0); /* Needed sanity check */ 1461117770Ssilby 1462113490Ssilby#ifdef MBUF_STRESS_TEST 1463113490Ssilby if (m_defragrandomfailures) { 1464113490Ssilby int temp = arc4random() & 0xff; 1465113490Ssilby if (temp == 0xba) 1466113490Ssilby goto nospace; 1467113490Ssilby } 1468113490Ssilby#endif 1469112777Ssilby 1470112777Ssilby if (m0->m_pkthdr.len > MHLEN) 1471112777Ssilby m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1472112777Ssilby else 1473112777Ssilby m_final = m_gethdr(how, MT_DATA); 1474112777Ssilby 1475112777Ssilby if (m_final == NULL) 1476112777Ssilby goto nospace; 1477112777Ssilby 1478123740Speter if (m_dup_pkthdr(m_final, m0, how) == 0) 1479112777Ssilby goto nospace; 1480112777Ssilby 1481112777Ssilby m_new = m_final; 1482112777Ssilby 1483112777Ssilby while (progress < m0->m_pkthdr.len) { 1484112777Ssilby length = m0->m_pkthdr.len - progress; 1485112777Ssilby if (length > MCLBYTES) 1486112777Ssilby length = MCLBYTES; 1487112777Ssilby 1488112777Ssilby if (m_new == NULL) { 1489112777Ssilby if (length > MLEN) 1490112777Ssilby m_new = m_getcl(how, MT_DATA, 0); 1491112777Ssilby else 1492112777Ssilby m_new = m_get(how, MT_DATA); 1493112777Ssilby if (m_new == NULL) 1494112777Ssilby goto nospace; 1495112777Ssilby } 1496112777Ssilby 1497112777Ssilby m_copydata(m0, progress, length, mtod(m_new, caddr_t)); 1498112777Ssilby progress += length; 1499112777Ssilby m_new->m_len = length; 1500112777Ssilby if (m_new != m_final) 1501112777Ssilby m_cat(m_final, m_new); 1502112777Ssilby m_new = NULL; 1503112777Ssilby } 1504116455Ssilby#ifdef MBUF_STRESS_TEST 1505112777Ssilby if (m0->m_next == NULL) 1506112777Ssilby m_defraguseless++; 1507116455Ssilby#endif 1508112777Ssilby m_freem(m0); 1509112777Ssilby m0 = m_final; 1510116455Ssilby#ifdef MBUF_STRESS_TEST 1511112777Ssilby m_defragpackets++; 1512112777Ssilby m_defragbytes += m0->m_pkthdr.len; 1513116455Ssilby#endif 1514112777Ssilby return (m0); 1515112777Ssilbynospace: 1516116455Ssilby#ifdef MBUF_STRESS_TEST 1517112777Ssilby m_defragfailure++; 1518116455Ssilby#endif 1519112777Ssilby if (m_final) 1520112777Ssilby m_freem(m_final); 1521112777Ssilby return (NULL); 1522112777Ssilby} 1523119644Ssilby 1524119644Ssilby#ifdef MBUF_STRESS_TEST 1525119644Ssilby 1526119644Ssilby/* 1527119644Ssilby * Fragment an mbuf chain. There's no reason you'd ever want to do 1528119644Ssilby * this in normal usage, but it's great for stress testing various 1529119644Ssilby * mbuf consumers. 1530119644Ssilby * 1531119644Ssilby * If fragmentation is not possible, the original chain will be 1532119644Ssilby * returned. 1533119644Ssilby * 1534119644Ssilby * Possible length values: 1535119644Ssilby * 0 no fragmentation will occur 1536119644Ssilby * > 0 each fragment will be of the specified length 1537119644Ssilby * -1 each fragment will be the same random value in length 1538119644Ssilby * -2 each fragment's length will be entirely random 1539119644Ssilby * (Random values range from 1 to 256) 1540119644Ssilby */ 1541119644Ssilbystruct mbuf * 1542119644Ssilbym_fragment(struct mbuf *m0, int how, int length) 1543119644Ssilby{ 1544125472Ssilby struct mbuf *m_new = NULL, *m_final = NULL; 1545125472Ssilby int progress = 0; 1546119644Ssilby 1547119644Ssilby if (!(m0->m_flags & M_PKTHDR)) 1548119644Ssilby return (m0); 1549119644Ssilby 1550119644Ssilby if ((length == 0) || (length < -2)) 1551119644Ssilby return (m0); 1552119644Ssilby 1553119644Ssilby m_fixhdr(m0); /* Needed sanity check */ 1554119644Ssilby 1555119644Ssilby m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1556119644Ssilby 1557119644Ssilby if (m_final == NULL) 1558119644Ssilby goto nospace; 1559119644Ssilby 1560123823Ssilby if (m_dup_pkthdr(m_final, m0, how) == 0) 1561119644Ssilby goto nospace; 1562119644Ssilby 1563119644Ssilby m_new = m_final; 1564119644Ssilby 1565119644Ssilby if (length == -1) 1566119644Ssilby length = 1 + (arc4random() & 255); 1567119644Ssilby 1568119644Ssilby while (progress < m0->m_pkthdr.len) { 1569119644Ssilby int fraglen; 1570119644Ssilby 1571119644Ssilby if (length > 0) 1572119644Ssilby fraglen = length; 1573119644Ssilby else 1574119644Ssilby fraglen = 1 + (arc4random() & 255); 1575119644Ssilby if (fraglen > m0->m_pkthdr.len - progress) 1576119644Ssilby fraglen = m0->m_pkthdr.len - progress; 1577119644Ssilby 1578119644Ssilby if (fraglen > MCLBYTES) 1579119644Ssilby fraglen = MCLBYTES; 1580119644Ssilby 1581119644Ssilby if (m_new == NULL) { 1582119644Ssilby m_new = m_getcl(how, MT_DATA, 0); 1583119644Ssilby if (m_new == NULL) 1584119644Ssilby goto nospace; 1585119644Ssilby } 1586119644Ssilby 1587119644Ssilby m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t)); 1588119644Ssilby progress += fraglen; 1589119644Ssilby m_new->m_len = fraglen; 1590119644Ssilby if (m_new != m_final) 1591119644Ssilby m_cat(m_final, m_new); 1592119644Ssilby m_new = NULL; 1593119644Ssilby } 1594119644Ssilby m_freem(m0); 1595119644Ssilby m0 = m_final; 1596119644Ssilby return (m0); 1597119644Ssilbynospace: 1598119644Ssilby if (m_final) 1599119644Ssilby m_freem(m_final); 1600119644Ssilby /* Return the original chain on failure */ 1601119644Ssilby return (m0); 1602119644Ssilby} 1603119644Ssilby 1604119644Ssilby#endif 1605125296Ssilby 1606125296Ssilbystruct mbuf * 1607145883Semaxm_uiotombuf(struct uio *uio, int how, int len, int align) 1608125296Ssilby{ 1609125472Ssilby struct mbuf *m_new = NULL, *m_final = NULL; 1610125472Ssilby int progress = 0, error = 0, length, total; 1611125296Ssilby 1612125296Ssilby if (len > 0) 1613125296Ssilby total = min(uio->uio_resid, len); 1614125296Ssilby else 1615125296Ssilby total = uio->uio_resid; 1616145883Semax if (align >= MHLEN) 1617145883Semax goto nospace; 1618145883Semax if (total + align > MHLEN) 1619125296Ssilby m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1620125296Ssilby else 1621125296Ssilby m_final = m_gethdr(how, MT_DATA); 1622125296Ssilby if (m_final == NULL) 1623125296Ssilby goto nospace; 1624145883Semax m_final->m_data += align; 1625125296Ssilby m_new = m_final; 1626125296Ssilby while (progress < total) { 1627125296Ssilby length = total - progress; 1628125296Ssilby if (length > MCLBYTES) 1629125296Ssilby length = MCLBYTES; 1630125296Ssilby if (m_new == NULL) { 1631125296Ssilby if (length > MLEN) 1632125296Ssilby m_new = m_getcl(how, MT_DATA, 0); 1633125296Ssilby else 1634125296Ssilby m_new = m_get(how, MT_DATA); 1635125296Ssilby if (m_new == NULL) 1636125296Ssilby goto nospace; 1637125296Ssilby } 1638125296Ssilby error = uiomove(mtod(m_new, void *), length, uio); 1639125296Ssilby if (error) 1640125296Ssilby goto nospace; 1641125296Ssilby progress += length; 1642125296Ssilby m_new->m_len = length; 1643125296Ssilby if (m_new != m_final) 1644125296Ssilby m_cat(m_final, m_new); 1645125296Ssilby m_new = NULL; 1646125296Ssilby } 1647125296Ssilby m_fixhdr(m_final); 1648125296Ssilby return (m_final); 1649125296Ssilbynospace: 1650125296Ssilby if (m_new) 1651125296Ssilby m_free(m_new); 1652125296Ssilby if (m_final) 1653125296Ssilby m_freem(m_final); 1654125296Ssilby return (NULL); 1655125296Ssilby} 1656148552Ssam 1657148552Ssam/* 1658148552Ssam * Set the m_data pointer of a newly-allocated mbuf 1659148552Ssam * to place an object of the specified size at the 1660148552Ssam * end of the mbuf, longword aligned. 1661148552Ssam */ 1662148552Ssamvoid 1663148552Ssamm_align(struct mbuf *m, int len) 1664148552Ssam{ 1665148552Ssam int adjust; 1666148552Ssam 1667148552Ssam if (m->m_flags & M_EXT) 1668148552Ssam adjust = m->m_ext.ext_size - len; 1669148552Ssam else if (m->m_flags & M_PKTHDR) 1670148552Ssam adjust = MHLEN - len; 1671148552Ssam else 1672148552Ssam adjust = MLEN - len; 1673148552Ssam m->m_data += adjust &~ (sizeof(long)-1); 1674148552Ssam} 1675