uipc_mbuf.c revision 248315
150397Sobrien/*- 2169689Skan * Copyright (c) 1982, 1986, 1988, 1991, 1993 390075Sobrien * The Regents of the University of California. All rights reserved. 450397Sobrien * 590075Sobrien * Redistribution and use in source and binary forms, with or without 650397Sobrien * modification, are permitted provided that the following conditions 790075Sobrien * are met: 890075Sobrien * 1. Redistributions of source code must retain the above copyright 990075Sobrien * notice, this list of conditions and the following disclaimer. 1090075Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1150397Sobrien * notice, this list of conditions and the following disclaimer in the 1290075Sobrien * documentation and/or other materials provided with the distribution. 1390075Sobrien * 4. Neither the name of the University nor the names of its contributors 1490075Sobrien * may be used to endorse or promote products derived from this software 1590075Sobrien * without specific prior written permission. 1650397Sobrien * 1750397Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1890075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20169689Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2150397Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2290075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23117395Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2590075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26117395Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27117395Skan * SUCH DAMAGE. 28117395Skan * 29169689Skan * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 30169689Skan */ 31169689Skan 32117395Skan#include <sys/cdefs.h> 3350397Sobrien__FBSDID("$FreeBSD: head/sys/kern/uipc_mbuf.c 248315 2013-03-15 10:15:07Z glebius $"); 3450397Sobrien 3550397Sobrien#include "opt_param.h" 36169689Skan#include "opt_mbuf_stress_test.h" 3750397Sobrien#include "opt_mbuf_profiling.h" 3850397Sobrien 39169689Skan#include <sys/param.h> 4050397Sobrien#include <sys/systm.h> 41169689Skan#include <sys/kernel.h> 4250397Sobrien#include <sys/limits.h> 43169689Skan#include <sys/lock.h> 44169689Skan#include <sys/malloc.h> 45169689Skan#include <sys/mbuf.h> 46169689Skan#include <sys/sysctl.h> 47169689Skan#include <sys/domain.h> 48169689Skan#include <sys/protosw.h> 49169689Skan#include <sys/uio.h> 50169689Skan 5150397Sobrienint max_linkhdr; 5250397Sobrienint max_protohdr; 53169689Skanint max_hdr; 5450397Sobrienint max_datalen; 55169689Skan#ifdef MBUF_STRESS_TEST 56169689Skanint m_defragpackets; 57169689Skanint m_defragbytes; 58169689Skanint m_defraguseless; 59169689Skanint m_defragfailure; 60169689Skanint m_defragrandomfailures; 61169689Skan#endif 62169689Skan 63117395Skan/* 6450397Sobrien * sysctl(8) exported objects 6590075Sobrien */ 6690075SobrienSYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RD, 6790075Sobrien &max_linkhdr, 0, "Size of largest link layer header"); 68117395SkanSYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RD, 6950397Sobrien &max_protohdr, 0, "Size of largest protocol layer header"); 7050397SobrienSYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RD, 7150397Sobrien &max_hdr, 0, "Size of largest link plus protocol header"); 72117395SkanSYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RD, 7390075Sobrien &max_datalen, 0, "Minimum space left in mbuf after max_hdr"); 7490075Sobrien#ifdef MBUF_STRESS_TEST 7590075SobrienSYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD, 76169689Skan &m_defragpackets, 0, ""); 77169689SkanSYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD, 78117395Skan &m_defragbytes, 0, ""); 7990075SobrienSYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD, 8050397Sobrien &m_defraguseless, 0, ""); 8150397SobrienSYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD, 8290075Sobrien &m_defragfailure, 0, ""); 83169689SkanSYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW, 8450397Sobrien &m_defragrandomfailures, 0, ""); 8550397Sobrien#endif 86132718Skan 8750397Sobrien/* 8890075Sobrien * m_get2() allocates minimum mbuf that would fit "size" argument. 89132718Skan */ 9050397Sobrienstruct mbuf * 9190075Sobrienm_get2(int size, int how, short type, int flags) 92169689Skan{ 9390075Sobrien struct mb_args args; 94169689Skan struct mbuf *m, *n; 95169689Skan 9650397Sobrien args.flags = flags; 97169689Skan args.type = type; 98169689Skan 99169689Skan if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0)) 10050397Sobrien return (uma_zalloc_arg(zone_mbuf, &args, how)); 101169689Skan if (size <= MCLBYTES) 102169689Skan return (uma_zalloc_arg(zone_pack, &args, how)); 103169689Skan 104169689Skan if (size > MJUMPAGESIZE) 105169689Skan return (NULL); 106169689Skan 107169689Skan m = uma_zalloc_arg(zone_mbuf, &args, how); 108169689Skan if (m == NULL) 109169689Skan return (NULL); 110169689Skan 111169689Skan n = uma_zalloc_arg(zone_jumbop, m, how); 112169689Skan if (n == NULL) { 113169689Skan uma_zfree(zone_mbuf, m); 114169689Skan return (NULL); 115169689Skan } 116169689Skan 117169689Skan return (m); 118169689Skan} 119169689Skan 120169689Skan/* 121169689Skan * m_getjcl() returns an mbuf with a cluster of the specified size attached. 122169689Skan * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 123169689Skan */ 124169689Skanstruct mbuf * 125169689Skanm_getjcl(int how, short type, int flags, int size) 126169689Skan{ 127169689Skan struct mb_args args; 12850397Sobrien struct mbuf *m, *n; 129132718Skan uma_zone_t zone; 13050397Sobrien 13150397Sobrien if (size == MCLBYTES) 132132718Skan return m_getcl(how, type, flags); 13350397Sobrien 13450397Sobrien args.flags = flags; 135132718Skan args.type = type; 13650397Sobrien 13750397Sobrien m = uma_zalloc_arg(zone_mbuf, &args, how); 138132718Skan if (m == NULL) 139132718Skan return (NULL); 14050397Sobrien 141132718Skan zone = m_getzone(size); 142132718Skan n = uma_zalloc_arg(zone, m, how); 14350397Sobrien if (n == NULL) { 144169689Skan uma_zfree(zone_mbuf, m); 145169689Skan return (NULL); 146169689Skan } 14750397Sobrien return (m); 148169689Skan} 149169689Skan 15050397Sobrien/* 151169689Skan * Allocate a given length worth of mbufs and/or clusters (whatever fits 152169689Skan * best) and return a pointer to the top of the allocated chain. If an 153169689Skan * existing mbuf chain is provided, then we will append the new chain 154169689Skan * to the existing one but still return the top of the newly allocated 155169689Skan * chain. 156169689Skan */ 157169689Skanstruct mbuf * 158169689Skanm_getm2(struct mbuf *m, int len, int how, short type, int flags) 159169689Skan{ 160169689Skan struct mbuf *mb, *nm = NULL, *mtail = NULL; 161169689Skan 162169689Skan KASSERT(len >= 0, ("%s: len is < 0", __func__)); 16390075Sobrien 16490075Sobrien /* Validate flags. */ 16590075Sobrien flags &= (M_PKTHDR | M_EOR); 166169689Skan 16750397Sobrien /* Packet header mbuf must be first in chain. */ 168169689Skan if ((flags & M_PKTHDR) && m != NULL) 169169689Skan flags &= ~M_PKTHDR; 17050397Sobrien 171169689Skan /* Loop and append maximum sized mbufs to the chain tail. */ 172169689Skan while (len > 0) { 173117395Skan if (len > MCLBYTES) 174169689Skan mb = m_getjcl(how, type, (flags & M_PKTHDR), 175169689Skan MJUMPAGESIZE); 17650397Sobrien else if (len >= MINCLSIZE) 17750397Sobrien mb = m_getcl(how, type, (flags & M_PKTHDR)); 17890075Sobrien else if (flags & M_PKTHDR) 179169689Skan mb = m_gethdr(how, type); 18050397Sobrien else 181169689Skan mb = m_get(how, type); 18290075Sobrien 183169689Skan /* Fail the whole operation if one mbuf can't be allocated. */ 184169689Skan if (mb == NULL) { 185169689Skan if (nm != NULL) 186169689Skan m_freem(nm); 18750397Sobrien return (NULL); 188169689Skan } 189169689Skan 19050397Sobrien /* Book keeping. */ 191169689Skan len -= (mb->m_flags & M_EXT) ? mb->m_ext.ext_size : 192169689Skan ((mb->m_flags & M_PKTHDR) ? MHLEN : MLEN); 19350397Sobrien if (mtail != NULL) 194169689Skan mtail->m_next = mb; 195169689Skan else 196169689Skan nm = mb; 197169689Skan mtail = mb; 198169689Skan flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */ 19950397Sobrien } 200169689Skan if (flags & M_EOR) 201169689Skan mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */ 20250397Sobrien 203169689Skan /* If mbuf was supplied, append new chain to the end of it. */ 204169689Skan if (m != NULL) { 205169689Skan for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next) 206169689Skan ; 207169689Skan mtail->m_next = nm; 208169689Skan mtail->m_flags &= ~M_EOR; 20950397Sobrien } else 210169689Skan m = nm; 211169689Skan 212169689Skan return (m); 213169689Skan} 214169689Skan 215169689Skan/* 216169689Skan * Free an entire chain of mbufs and associated external buffers, if 217169689Skan * applicable. 21890075Sobrien */ 219169689Skanvoid 220169689Skanm_freem(struct mbuf *mb) 221169689Skan{ 222169689Skan 223169689Skan while (mb != NULL) 224169689Skan mb = m_free(mb); 225169689Skan} 226169689Skan 227169689Skan/*- 228169689Skan * Configure a provided mbuf to refer to the provided external storage 229169689Skan * buffer and setup a reference count for said buffer. If the setting 230169689Skan * up of the reference count fails, the M_EXT bit will not be set. If 231169689Skan * successfull, the M_EXT bit is set in the mbuf's flags. 232169689Skan * 233169689Skan * Arguments: 234169689Skan * mb The existing mbuf to which to attach the provided buffer. 235169689Skan * buf The address of the provided external storage buffer. 236169689Skan * size The size of the provided buffer. 237169689Skan * freef A pointer to a routine that is responsible for freeing the 238169689Skan * provided external storage buffer. 239169689Skan * args A pointer to an argument structure (of any type) to be passed 240169689Skan * to the provided freef routine (may be NULL). 241169689Skan * flags Any other flags to be passed to the provided mbuf. 242169689Skan * type The type that the external storage buffer should be 243169689Skan * labeled with. 244169689Skan * 245169689Skan * Returns: 246169689Skan * Nothing. 247169689Skan */ 248169689Skanint 249169689Skanm_extadd(struct mbuf *mb, caddr_t buf, u_int size, 250169689Skan void (*freef)(void *, void *), void *arg1, void *arg2, int flags, int type, 251169689Skan int wait) 252169689Skan{ 253169689Skan KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__)); 254169689Skan 255169689Skan if (type != EXT_EXTREF) 256169689Skan mb->m_ext.ref_cnt = uma_zalloc(zone_ext_refcnt, wait); 257169689Skan 258169689Skan if (mb->m_ext.ref_cnt == NULL) 259169689Skan return (ENOMEM); 260169689Skan 261169689Skan *(mb->m_ext.ref_cnt) = 1; 262169689Skan mb->m_flags |= (M_EXT | flags); 263169689Skan mb->m_ext.ext_buf = buf; 264169689Skan mb->m_data = mb->m_ext.ext_buf; 265169689Skan mb->m_ext.ext_size = size; 266169689Skan mb->m_ext.ext_free = freef; 267169689Skan mb->m_ext.ext_arg1 = arg1; 268169689Skan mb->m_ext.ext_arg2 = arg2; 269169689Skan mb->m_ext.ext_type = type; 270169689Skan 271169689Skan return (0); 272169689Skan} 273169689Skan 274169689Skan/* 275169689Skan * Non-directly-exported function to clean up after mbufs with M_EXT 276169689Skan * storage attached to them if the reference count hits 1. 277169689Skan */ 278169689Skanvoid 279169689Skanmb_free_ext(struct mbuf *m) 280169689Skan{ 281169689Skan int skipmbuf; 282169689Skan 283169689Skan KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 284169689Skan KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 285169689Skan 286169689Skan 287169689Skan /* 288169689Skan * check if the header is embedded in the cluster 289169689Skan */ 290169689Skan skipmbuf = (m->m_flags & M_NOFREE); 291169689Skan 292169689Skan /* Free attached storage if this mbuf is the only reference to it. */ 293169689Skan if (*(m->m_ext.ref_cnt) == 1 || 294169689Skan atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 1) { 295169689Skan switch (m->m_ext.ext_type) { 296169689Skan case EXT_PACKET: /* The packet zone is special. */ 297169689Skan if (*(m->m_ext.ref_cnt) == 0) 298169689Skan *(m->m_ext.ref_cnt) = 1; 299169689Skan uma_zfree(zone_pack, m); 300169689Skan return; /* Job done. */ 301169689Skan case EXT_CLUSTER: 302169689Skan uma_zfree(zone_clust, m->m_ext.ext_buf); 303169689Skan break; 304169689Skan case EXT_JUMBOP: 305169689Skan uma_zfree(zone_jumbop, m->m_ext.ext_buf); 306169689Skan break; 307169689Skan case EXT_JUMBO9: 308169689Skan uma_zfree(zone_jumbo9, m->m_ext.ext_buf); 309169689Skan break; 310169689Skan case EXT_JUMBO16: 311169689Skan uma_zfree(zone_jumbo16, m->m_ext.ext_buf); 312169689Skan break; 313169689Skan case EXT_SFBUF: 314169689Skan case EXT_NET_DRV: 315169689Skan case EXT_MOD_TYPE: 316169689Skan case EXT_DISPOSABLE: 317169689Skan *(m->m_ext.ref_cnt) = 0; 318169689Skan uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *, 319169689Skan m->m_ext.ref_cnt)); 320169689Skan /* FALLTHROUGH */ 321169689Skan case EXT_EXTREF: 322169689Skan KASSERT(m->m_ext.ext_free != NULL, 323169689Skan ("%s: ext_free not set", __func__)); 324169689Skan (*(m->m_ext.ext_free))(m->m_ext.ext_arg1, 325169689Skan m->m_ext.ext_arg2); 326169689Skan break; 327169689Skan default: 328169689Skan KASSERT(m->m_ext.ext_type == 0, 329169689Skan ("%s: unknown ext_type", __func__)); 330169689Skan } 331169689Skan } 332169689Skan if (skipmbuf) 333169689Skan return; 334169689Skan 335169689Skan /* 336169689Skan * Free this mbuf back to the mbuf zone with all m_ext 337169689Skan * information purged. 338169689Skan */ 339169689Skan m->m_ext.ext_buf = NULL; 340169689Skan m->m_ext.ext_free = NULL; 341169689Skan m->m_ext.ext_arg1 = NULL; 342169689Skan m->m_ext.ext_arg2 = NULL; 343169689Skan m->m_ext.ref_cnt = NULL; 344169689Skan m->m_ext.ext_size = 0; 345169689Skan m->m_ext.ext_type = 0; 346169689Skan m->m_flags &= ~M_EXT; 347169689Skan uma_zfree(zone_mbuf, m); 348169689Skan} 349169689Skan 350169689Skan/* 351169689Skan * Attach the cluster from *m to *n, set up m_ext in *n 352169689Skan * and bump the refcount of the cluster. 353169689Skan */ 354169689Skanstatic void 355169689Skanmb_dupcl(struct mbuf *n, struct mbuf *m) 356169689Skan{ 357169689Skan KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 358169689Skan KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 359169689Skan KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__)); 360169689Skan 361169689Skan if (*(m->m_ext.ref_cnt) == 1) 362169689Skan *(m->m_ext.ref_cnt) += 1; 363169689Skan else 364169689Skan atomic_add_int(m->m_ext.ref_cnt, 1); 365169689Skan n->m_ext.ext_buf = m->m_ext.ext_buf; 366169689Skan n->m_ext.ext_free = m->m_ext.ext_free; 367169689Skan n->m_ext.ext_arg1 = m->m_ext.ext_arg1; 368169689Skan n->m_ext.ext_arg2 = m->m_ext.ext_arg2; 369169689Skan n->m_ext.ext_size = m->m_ext.ext_size; 370169689Skan n->m_ext.ref_cnt = m->m_ext.ref_cnt; 371169689Skan n->m_ext.ext_type = m->m_ext.ext_type; 372169689Skan n->m_flags |= M_EXT; 373169689Skan n->m_flags |= m->m_flags & M_RDONLY; 374169689Skan} 375169689Skan 376169689Skan/* 377169689Skan * Clean up mbuf (chain) from any tags and packet headers. 378169689Skan * If "all" is set then the first mbuf in the chain will be 379169689Skan * cleaned too. 380169689Skan */ 381169689Skanvoid 382169689Skanm_demote(struct mbuf *m0, int all) 383169689Skan{ 384169689Skan struct mbuf *m; 385169689Skan 386169689Skan for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) { 387169689Skan if (m->m_flags & M_PKTHDR) { 388169689Skan m_tag_delete_chain(m, NULL); 389169689Skan m->m_flags &= ~M_PKTHDR; 390169689Skan bzero(&m->m_pkthdr, sizeof(struct pkthdr)); 391169689Skan } 392169689Skan if (m != m0 && m->m_nextpkt != NULL) { 393169689Skan KASSERT(m->m_nextpkt == NULL, 394169689Skan ("%s: m_nextpkt not NULL", __func__)); 395169689Skan m_freem(m->m_nextpkt); 396169689Skan m->m_nextpkt = NULL; 397169689Skan } 398169689Skan m->m_flags = m->m_flags & (M_EXT|M_RDONLY|M_FREELIST|M_NOFREE); 399169689Skan } 400169689Skan} 401169689Skan 402169689Skan/* 403169689Skan * Sanity checks on mbuf (chain) for use in KASSERT() and general 404169689Skan * debugging. 405169689Skan * Returns 0 or panics when bad and 1 on all tests passed. 406169689Skan * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they 407169689Skan * blow up later. 408169689Skan */ 409169689Skanint 410169689Skanm_sanity(struct mbuf *m0, int sanitize) 411169689Skan{ 412169689Skan struct mbuf *m; 413169689Skan caddr_t a, b; 414169689Skan int pktlen = 0; 415169689Skan 416169689Skan#ifdef INVARIANTS 417169689Skan#define M_SANITY_ACTION(s) panic("mbuf %p: " s, m) 418169689Skan#else 419169689Skan#define M_SANITY_ACTION(s) printf("mbuf %p: " s, m) 420169689Skan#endif 421169689Skan 422169689Skan for (m = m0; m != NULL; m = m->m_next) { 423169689Skan /* 424169689Skan * Basic pointer checks. If any of these fails then some 425169689Skan * unrelated kernel memory before or after us is trashed. 426169689Skan * No way to recover from that. 427169689Skan */ 428169689Skan a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf : 429169689Skan ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) : 430169689Skan (caddr_t)(&m->m_dat)) ); 431169689Skan b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size : 432169689Skan ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN))); 433169689Skan if ((caddr_t)m->m_data < a) 434169689Skan M_SANITY_ACTION("m_data outside mbuf data range left"); 435169689Skan if ((caddr_t)m->m_data > b) 436169689Skan M_SANITY_ACTION("m_data outside mbuf data range right"); 437169689Skan if ((caddr_t)m->m_data + m->m_len > b) 438169689Skan M_SANITY_ACTION("m_data + m_len exeeds mbuf space"); 439169689Skan if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) { 440169689Skan if ((caddr_t)m->m_pkthdr.header < a || 441169689Skan (caddr_t)m->m_pkthdr.header > b) 442169689Skan M_SANITY_ACTION("m_pkthdr.header outside mbuf data range"); 443169689Skan } 444169689Skan 445169689Skan /* m->m_nextpkt may only be set on first mbuf in chain. */ 446169689Skan if (m != m0 && m->m_nextpkt != NULL) { 447169689Skan if (sanitize) { 448169689Skan m_freem(m->m_nextpkt); 449169689Skan m->m_nextpkt = (struct mbuf *)0xDEADC0DE; 450169689Skan } else 451169689Skan M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf"); 452169689Skan } 453169689Skan 454169689Skan /* packet length (not mbuf length!) calculation */ 455169689Skan if (m0->m_flags & M_PKTHDR) 456169689Skan pktlen += m->m_len; 457169689Skan 458169689Skan /* m_tags may only be attached to first mbuf in chain. */ 459169689Skan if (m != m0 && m->m_flags & M_PKTHDR && 460169689Skan !SLIST_EMPTY(&m->m_pkthdr.tags)) { 461169689Skan if (sanitize) { 462169689Skan m_tag_delete_chain(m, NULL); 463169689Skan /* put in 0xDEADC0DE perhaps? */ 464169689Skan } else 465169689Skan M_SANITY_ACTION("m_tags on in-chain mbuf"); 466169689Skan } 467169689Skan 468169689Skan /* M_PKTHDR may only be set on first mbuf in chain */ 469169689Skan if (m != m0 && m->m_flags & M_PKTHDR) { 470169689Skan if (sanitize) { 471169689Skan bzero(&m->m_pkthdr, sizeof(m->m_pkthdr)); 472169689Skan m->m_flags &= ~M_PKTHDR; 473169689Skan /* put in 0xDEADCODE and leave hdr flag in */ 474169689Skan } else 475169689Skan M_SANITY_ACTION("M_PKTHDR on in-chain mbuf"); 476169689Skan } 477169689Skan } 478169689Skan m = m0; 479169689Skan if (pktlen && pktlen != m->m_pkthdr.len) { 480169689Skan if (sanitize) 481169689Skan m->m_pkthdr.len = 0; 482169689Skan else 483169689Skan M_SANITY_ACTION("m_pkthdr.len != mbuf chain length"); 484169689Skan } 485169689Skan return 1; 486169689Skan 487169689Skan#undef M_SANITY_ACTION 488169689Skan} 489169689Skan 490169689Skan 491169689Skan/* 492169689Skan * "Move" mbuf pkthdr from "from" to "to". 493169689Skan * "from" must have M_PKTHDR set, and "to" must be empty. 494169689Skan */ 495169689Skanvoid 496169689Skanm_move_pkthdr(struct mbuf *to, struct mbuf *from) 497169689Skan{ 498169689Skan 499169689Skan#if 0 500169689Skan /* see below for why these are not enabled */ 501169689Skan M_ASSERTPKTHDR(to); 502169689Skan /* Note: with MAC, this may not be a good assertion. */ 503169689Skan KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 504169689Skan ("m_move_pkthdr: to has tags")); 505169689Skan#endif 506169689Skan#ifdef MAC 507169689Skan /* 508169689Skan * XXXMAC: It could be this should also occur for non-MAC? 509169689Skan */ 510169689Skan if (to->m_flags & M_PKTHDR) 511169689Skan m_tag_delete_chain(to, NULL); 512169689Skan#endif 513169689Skan to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 514169689Skan if ((to->m_flags & M_EXT) == 0) 515169689Skan to->m_data = to->m_pktdat; 516169689Skan to->m_pkthdr = from->m_pkthdr; /* especially tags */ 517169689Skan SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 518169689Skan from->m_flags &= ~M_PKTHDR; 519169689Skan} 520169689Skan 521169689Skan/* 522169689Skan * Duplicate "from"'s mbuf pkthdr in "to". 523169689Skan * "from" must have M_PKTHDR set, and "to" must be empty. 524169689Skan * In particular, this does a deep copy of the packet tags. 525169689Skan */ 526169689Skanint 527169689Skanm_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 528169689Skan{ 529169689Skan 530169689Skan#if 0 531169689Skan /* 532169689Skan * The mbuf allocator only initializes the pkthdr 533169689Skan * when the mbuf is allocated with MGETHDR. Many users 534169689Skan * (e.g. m_copy*, m_prepend) use MGET and then 535169689Skan * smash the pkthdr as needed causing these 536169689Skan * assertions to trip. For now just disable them. 537169689Skan */ 538169689Skan M_ASSERTPKTHDR(to); 539169689Skan /* Note: with MAC, this may not be a good assertion. */ 540169689Skan KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 541169689Skan#endif 542169689Skan MBUF_CHECKSLEEP(how); 543169689Skan#ifdef MAC 544169689Skan if (to->m_flags & M_PKTHDR) 545169689Skan m_tag_delete_chain(to, NULL); 546169689Skan#endif 547169689Skan to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 548169689Skan if ((to->m_flags & M_EXT) == 0) 549169689Skan to->m_data = to->m_pktdat; 550169689Skan to->m_pkthdr = from->m_pkthdr; 551169689Skan SLIST_INIT(&to->m_pkthdr.tags); 552169689Skan return (m_tag_copy_chain(to, from, MBTOM(how))); 553169689Skan} 554169689Skan 555169689Skan/* 556169689Skan * Lesser-used path for M_PREPEND: 557169689Skan * allocate new mbuf to prepend to chain, 558169689Skan * copy junk along. 559169689Skan */ 560169689Skanstruct mbuf * 561169689Skanm_prepend(struct mbuf *m, int len, int how) 562169689Skan{ 563169689Skan struct mbuf *mn; 564169689Skan 565169689Skan if (m->m_flags & M_PKTHDR) 566169689Skan MGETHDR(mn, how, m->m_type); 567169689Skan else 56890075Sobrien MGET(mn, how, m->m_type); 569 if (mn == NULL) { 570 m_freem(m); 571 return (NULL); 572 } 573 if (m->m_flags & M_PKTHDR) 574 M_MOVE_PKTHDR(mn, m); 575 mn->m_next = m; 576 m = mn; 577 if(m->m_flags & M_PKTHDR) { 578 if (len < MHLEN) 579 MH_ALIGN(m, len); 580 } else { 581 if (len < MLEN) 582 M_ALIGN(m, len); 583 } 584 m->m_len = len; 585 return (m); 586} 587 588/* 589 * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 590 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 591 * The wait parameter is a choice of M_WAITOK/M_NOWAIT from caller. 592 * Note that the copy is read-only, because clusters are not copied, 593 * only their reference counts are incremented. 594 */ 595struct mbuf * 596m_copym(struct mbuf *m, int off0, int len, int wait) 597{ 598 struct mbuf *n, **np; 599 int off = off0; 600 struct mbuf *top; 601 int copyhdr = 0; 602 603 KASSERT(off >= 0, ("m_copym, negative off %d", off)); 604 KASSERT(len >= 0, ("m_copym, negative len %d", len)); 605 MBUF_CHECKSLEEP(wait); 606 if (off == 0 && m->m_flags & M_PKTHDR) 607 copyhdr = 1; 608 while (off > 0) { 609 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 610 if (off < m->m_len) 611 break; 612 off -= m->m_len; 613 m = m->m_next; 614 } 615 np = ⊤ 616 top = 0; 617 while (len > 0) { 618 if (m == NULL) { 619 KASSERT(len == M_COPYALL, 620 ("m_copym, length > size of mbuf chain")); 621 break; 622 } 623 if (copyhdr) 624 MGETHDR(n, wait, m->m_type); 625 else 626 MGET(n, wait, m->m_type); 627 *np = n; 628 if (n == NULL) 629 goto nospace; 630 if (copyhdr) { 631 if (!m_dup_pkthdr(n, m, wait)) 632 goto nospace; 633 if (len == M_COPYALL) 634 n->m_pkthdr.len -= off0; 635 else 636 n->m_pkthdr.len = len; 637 copyhdr = 0; 638 } 639 n->m_len = min(len, m->m_len - off); 640 if (m->m_flags & M_EXT) { 641 n->m_data = m->m_data + off; 642 mb_dupcl(n, m); 643 } else 644 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 645 (u_int)n->m_len); 646 if (len != M_COPYALL) 647 len -= n->m_len; 648 off = 0; 649 m = m->m_next; 650 np = &n->m_next; 651 } 652 if (top == NULL) 653 mbstat.m_mcfail++; /* XXX: No consistency. */ 654 655 return (top); 656nospace: 657 m_freem(top); 658 mbstat.m_mcfail++; /* XXX: No consistency. */ 659 return (NULL); 660} 661 662/* 663 * Returns mbuf chain with new head for the prepending case. 664 * Copies from mbuf (chain) n from off for len to mbuf (chain) m 665 * either prepending or appending the data. 666 * The resulting mbuf (chain) m is fully writeable. 667 * m is destination (is made writeable) 668 * n is source, off is offset in source, len is len from offset 669 * dir, 0 append, 1 prepend 670 * how, wait or nowait 671 */ 672 673static int 674m_bcopyxxx(void *s, void *t, u_int len) 675{ 676 bcopy(s, t, (size_t)len); 677 return 0; 678} 679 680struct mbuf * 681m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len, 682 int prep, int how) 683{ 684 struct mbuf *mm, *x, *z, *prev = NULL; 685 caddr_t p; 686 int i, nlen = 0; 687 caddr_t buf[MLEN]; 688 689 KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source")); 690 KASSERT(off >= 0, ("m_copymdata, negative off %d", off)); 691 KASSERT(len >= 0, ("m_copymdata, negative len %d", len)); 692 KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep)); 693 694 mm = m; 695 if (!prep) { 696 while(mm->m_next) { 697 prev = mm; 698 mm = mm->m_next; 699 } 700 } 701 for (z = n; z != NULL; z = z->m_next) 702 nlen += z->m_len; 703 if (len == M_COPYALL) 704 len = nlen - off; 705 if (off + len > nlen || len < 1) 706 return NULL; 707 708 if (!M_WRITABLE(mm)) { 709 /* XXX: Use proper m_xxx function instead. */ 710 x = m_getcl(how, MT_DATA, mm->m_flags); 711 if (x == NULL) 712 return NULL; 713 bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size); 714 p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf); 715 x->m_data = p; 716 mm->m_next = NULL; 717 if (mm != m) 718 prev->m_next = x; 719 m_free(mm); 720 mm = x; 721 } 722 723 /* 724 * Append/prepend the data. Allocating mbufs as necessary. 725 */ 726 /* Shortcut if enough free space in first/last mbuf. */ 727 if (!prep && M_TRAILINGSPACE(mm) >= len) { 728 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) + 729 mm->m_len); 730 mm->m_len += len; 731 mm->m_pkthdr.len += len; 732 return m; 733 } 734 if (prep && M_LEADINGSPACE(mm) >= len) { 735 mm->m_data = mtod(mm, caddr_t) - len; 736 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t)); 737 mm->m_len += len; 738 mm->m_pkthdr.len += len; 739 return mm; 740 } 741 742 /* Expand first/last mbuf to cluster if possible. */ 743 if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) { 744 bcopy(mm->m_data, &buf, mm->m_len); 745 m_clget(mm, how); 746 if (!(mm->m_flags & M_EXT)) 747 return NULL; 748 bcopy(&buf, mm->m_ext.ext_buf, mm->m_len); 749 mm->m_data = mm->m_ext.ext_buf; 750 mm->m_pkthdr.header = NULL; 751 } 752 if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) { 753 bcopy(mm->m_data, &buf, mm->m_len); 754 m_clget(mm, how); 755 if (!(mm->m_flags & M_EXT)) 756 return NULL; 757 bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf + 758 mm->m_ext.ext_size - mm->m_len, mm->m_len); 759 mm->m_data = (caddr_t)mm->m_ext.ext_buf + 760 mm->m_ext.ext_size - mm->m_len; 761 mm->m_pkthdr.header = NULL; 762 } 763 764 /* Append/prepend as many mbuf (clusters) as necessary to fit len. */ 765 if (!prep && len > M_TRAILINGSPACE(mm)) { 766 if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA)) 767 return NULL; 768 } 769 if (prep && len > M_LEADINGSPACE(mm)) { 770 if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA))) 771 return NULL; 772 i = 0; 773 for (x = z; x != NULL; x = x->m_next) { 774 i += x->m_flags & M_EXT ? x->m_ext.ext_size : 775 (x->m_flags & M_PKTHDR ? MHLEN : MLEN); 776 if (!x->m_next) 777 break; 778 } 779 z->m_data += i - len; 780 m_move_pkthdr(mm, z); 781 x->m_next = mm; 782 mm = z; 783 } 784 785 /* Seek to start position in source mbuf. Optimization for long chains. */ 786 while (off > 0) { 787 if (off < n->m_len) 788 break; 789 off -= n->m_len; 790 n = n->m_next; 791 } 792 793 /* Copy data into target mbuf. */ 794 z = mm; 795 while (len > 0) { 796 KASSERT(z != NULL, ("m_copymdata, falling off target edge")); 797 i = M_TRAILINGSPACE(z); 798 m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len); 799 z->m_len += i; 800 /* fixup pkthdr.len if necessary */ 801 if ((prep ? mm : m)->m_flags & M_PKTHDR) 802 (prep ? mm : m)->m_pkthdr.len += i; 803 off += i; 804 len -= i; 805 z = z->m_next; 806 } 807 return (prep ? mm : m); 808} 809 810/* 811 * Copy an entire packet, including header (which must be present). 812 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 813 * Note that the copy is read-only, because clusters are not copied, 814 * only their reference counts are incremented. 815 * Preserve alignment of the first mbuf so if the creator has left 816 * some room at the beginning (e.g. for inserting protocol headers) 817 * the copies still have the room available. 818 */ 819struct mbuf * 820m_copypacket(struct mbuf *m, int how) 821{ 822 struct mbuf *top, *n, *o; 823 824 MBUF_CHECKSLEEP(how); 825 MGET(n, how, m->m_type); 826 top = n; 827 if (n == NULL) 828 goto nospace; 829 830 if (!m_dup_pkthdr(n, m, how)) 831 goto nospace; 832 n->m_len = m->m_len; 833 if (m->m_flags & M_EXT) { 834 n->m_data = m->m_data; 835 mb_dupcl(n, m); 836 } else { 837 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 838 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 839 } 840 841 m = m->m_next; 842 while (m) { 843 MGET(o, how, m->m_type); 844 if (o == NULL) 845 goto nospace; 846 847 n->m_next = o; 848 n = n->m_next; 849 850 n->m_len = m->m_len; 851 if (m->m_flags & M_EXT) { 852 n->m_data = m->m_data; 853 mb_dupcl(n, m); 854 } else { 855 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 856 } 857 858 m = m->m_next; 859 } 860 return top; 861nospace: 862 m_freem(top); 863 mbstat.m_mcfail++; /* XXX: No consistency. */ 864 return (NULL); 865} 866 867/* 868 * Copy data from an mbuf chain starting "off" bytes from the beginning, 869 * continuing for "len" bytes, into the indicated buffer. 870 */ 871void 872m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 873{ 874 u_int count; 875 876 KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 877 KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 878 while (off > 0) { 879 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 880 if (off < m->m_len) 881 break; 882 off -= m->m_len; 883 m = m->m_next; 884 } 885 while (len > 0) { 886 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 887 count = min(m->m_len - off, len); 888 bcopy(mtod(m, caddr_t) + off, cp, count); 889 len -= count; 890 cp += count; 891 off = 0; 892 m = m->m_next; 893 } 894} 895 896/* 897 * Copy a packet header mbuf chain into a completely new chain, including 898 * copying any mbuf clusters. Use this instead of m_copypacket() when 899 * you need a writable copy of an mbuf chain. 900 */ 901struct mbuf * 902m_dup(struct mbuf *m, int how) 903{ 904 struct mbuf **p, *top = NULL; 905 int remain, moff, nsize; 906 907 MBUF_CHECKSLEEP(how); 908 /* Sanity check */ 909 if (m == NULL) 910 return (NULL); 911 M_ASSERTPKTHDR(m); 912 913 /* While there's more data, get a new mbuf, tack it on, and fill it */ 914 remain = m->m_pkthdr.len; 915 moff = 0; 916 p = ⊤ 917 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 918 struct mbuf *n; 919 920 /* Get the next new mbuf */ 921 if (remain >= MINCLSIZE) { 922 n = m_getcl(how, m->m_type, 0); 923 nsize = MCLBYTES; 924 } else { 925 n = m_get(how, m->m_type); 926 nsize = MLEN; 927 } 928 if (n == NULL) 929 goto nospace; 930 931 if (top == NULL) { /* First one, must be PKTHDR */ 932 if (!m_dup_pkthdr(n, m, how)) { 933 m_free(n); 934 goto nospace; 935 } 936 if ((n->m_flags & M_EXT) == 0) 937 nsize = MHLEN; 938 } 939 n->m_len = 0; 940 941 /* Link it into the new chain */ 942 *p = n; 943 p = &n->m_next; 944 945 /* Copy data from original mbuf(s) into new mbuf */ 946 while (n->m_len < nsize && m != NULL) { 947 int chunk = min(nsize - n->m_len, m->m_len - moff); 948 949 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 950 moff += chunk; 951 n->m_len += chunk; 952 remain -= chunk; 953 if (moff == m->m_len) { 954 m = m->m_next; 955 moff = 0; 956 } 957 } 958 959 /* Check correct total mbuf length */ 960 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 961 ("%s: bogus m_pkthdr.len", __func__)); 962 } 963 return (top); 964 965nospace: 966 m_freem(top); 967 mbstat.m_mcfail++; /* XXX: No consistency. */ 968 return (NULL); 969} 970 971/* 972 * Concatenate mbuf chain n to m. 973 * Both chains must be of the same type (e.g. MT_DATA). 974 * Any m_pkthdr is not updated. 975 */ 976void 977m_cat(struct mbuf *m, struct mbuf *n) 978{ 979 while (m->m_next) 980 m = m->m_next; 981 while (n) { 982 if (!M_WRITABLE(m) || 983 M_TRAILINGSPACE(m) < n->m_len) { 984 /* just join the two chains */ 985 m->m_next = n; 986 return; 987 } 988 /* splat the data from one into the other */ 989 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 990 (u_int)n->m_len); 991 m->m_len += n->m_len; 992 n = m_free(n); 993 } 994} 995 996void 997m_adj(struct mbuf *mp, int req_len) 998{ 999 int len = req_len; 1000 struct mbuf *m; 1001 int count; 1002 1003 if ((m = mp) == NULL) 1004 return; 1005 if (len >= 0) { 1006 /* 1007 * Trim from head. 1008 */ 1009 while (m != NULL && len > 0) { 1010 if (m->m_len <= len) { 1011 len -= m->m_len; 1012 m->m_len = 0; 1013 m = m->m_next; 1014 } else { 1015 m->m_len -= len; 1016 m->m_data += len; 1017 len = 0; 1018 } 1019 } 1020 if (mp->m_flags & M_PKTHDR) 1021 mp->m_pkthdr.len -= (req_len - len); 1022 } else { 1023 /* 1024 * Trim from tail. Scan the mbuf chain, 1025 * calculating its length and finding the last mbuf. 1026 * If the adjustment only affects this mbuf, then just 1027 * adjust and return. Otherwise, rescan and truncate 1028 * after the remaining size. 1029 */ 1030 len = -len; 1031 count = 0; 1032 for (;;) { 1033 count += m->m_len; 1034 if (m->m_next == (struct mbuf *)0) 1035 break; 1036 m = m->m_next; 1037 } 1038 if (m->m_len >= len) { 1039 m->m_len -= len; 1040 if (mp->m_flags & M_PKTHDR) 1041 mp->m_pkthdr.len -= len; 1042 return; 1043 } 1044 count -= len; 1045 if (count < 0) 1046 count = 0; 1047 /* 1048 * Correct length for chain is "count". 1049 * Find the mbuf with last data, adjust its length, 1050 * and toss data from remaining mbufs on chain. 1051 */ 1052 m = mp; 1053 if (m->m_flags & M_PKTHDR) 1054 m->m_pkthdr.len = count; 1055 for (; m; m = m->m_next) { 1056 if (m->m_len >= count) { 1057 m->m_len = count; 1058 if (m->m_next != NULL) { 1059 m_freem(m->m_next); 1060 m->m_next = NULL; 1061 } 1062 break; 1063 } 1064 count -= m->m_len; 1065 } 1066 } 1067} 1068 1069/* 1070 * Rearange an mbuf chain so that len bytes are contiguous 1071 * and in the data area of an mbuf (so that mtod will work 1072 * for a structure of size len). Returns the resulting 1073 * mbuf chain on success, frees it and returns null on failure. 1074 * If there is room, it will add up to max_protohdr-len extra bytes to the 1075 * contiguous region in an attempt to avoid being called next time. 1076 */ 1077struct mbuf * 1078m_pullup(struct mbuf *n, int len) 1079{ 1080 struct mbuf *m; 1081 int count; 1082 int space; 1083 1084 /* 1085 * If first mbuf has no cluster, and has room for len bytes 1086 * without shifting current data, pullup into it, 1087 * otherwise allocate a new mbuf to prepend to the chain. 1088 */ 1089 if ((n->m_flags & M_EXT) == 0 && 1090 n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 1091 if (n->m_len >= len) 1092 return (n); 1093 m = n; 1094 n = n->m_next; 1095 len -= m->m_len; 1096 } else { 1097 if (len > MHLEN) 1098 goto bad; 1099 MGET(m, M_NOWAIT, n->m_type); 1100 if (m == NULL) 1101 goto bad; 1102 m->m_len = 0; 1103 if (n->m_flags & M_PKTHDR) 1104 M_MOVE_PKTHDR(m, n); 1105 } 1106 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1107 do { 1108 count = min(min(max(len, max_protohdr), space), n->m_len); 1109 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 1110 (u_int)count); 1111 len -= count; 1112 m->m_len += count; 1113 n->m_len -= count; 1114 space -= count; 1115 if (n->m_len) 1116 n->m_data += count; 1117 else 1118 n = m_free(n); 1119 } while (len > 0 && n); 1120 if (len > 0) { 1121 (void) m_free(m); 1122 goto bad; 1123 } 1124 m->m_next = n; 1125 return (m); 1126bad: 1127 m_freem(n); 1128 mbstat.m_mpfail++; /* XXX: No consistency. */ 1129 return (NULL); 1130} 1131 1132/* 1133 * Like m_pullup(), except a new mbuf is always allocated, and we allow 1134 * the amount of empty space before the data in the new mbuf to be specified 1135 * (in the event that the caller expects to prepend later). 1136 */ 1137int MSFail; 1138 1139struct mbuf * 1140m_copyup(struct mbuf *n, int len, int dstoff) 1141{ 1142 struct mbuf *m; 1143 int count, space; 1144 1145 if (len > (MHLEN - dstoff)) 1146 goto bad; 1147 MGET(m, M_NOWAIT, n->m_type); 1148 if (m == NULL) 1149 goto bad; 1150 m->m_len = 0; 1151 if (n->m_flags & M_PKTHDR) 1152 M_MOVE_PKTHDR(m, n); 1153 m->m_data += dstoff; 1154 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1155 do { 1156 count = min(min(max(len, max_protohdr), space), n->m_len); 1157 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), 1158 (unsigned)count); 1159 len -= count; 1160 m->m_len += count; 1161 n->m_len -= count; 1162 space -= count; 1163 if (n->m_len) 1164 n->m_data += count; 1165 else 1166 n = m_free(n); 1167 } while (len > 0 && n); 1168 if (len > 0) { 1169 (void) m_free(m); 1170 goto bad; 1171 } 1172 m->m_next = n; 1173 return (m); 1174 bad: 1175 m_freem(n); 1176 MSFail++; 1177 return (NULL); 1178} 1179 1180/* 1181 * Partition an mbuf chain in two pieces, returning the tail -- 1182 * all but the first len0 bytes. In case of failure, it returns NULL and 1183 * attempts to restore the chain to its original state. 1184 * 1185 * Note that the resulting mbufs might be read-only, because the new 1186 * mbuf can end up sharing an mbuf cluster with the original mbuf if 1187 * the "breaking point" happens to lie within a cluster mbuf. Use the 1188 * M_WRITABLE() macro to check for this case. 1189 */ 1190struct mbuf * 1191m_split(struct mbuf *m0, int len0, int wait) 1192{ 1193 struct mbuf *m, *n; 1194 u_int len = len0, remain; 1195 1196 MBUF_CHECKSLEEP(wait); 1197 for (m = m0; m && len > m->m_len; m = m->m_next) 1198 len -= m->m_len; 1199 if (m == NULL) 1200 return (NULL); 1201 remain = m->m_len - len; 1202 if (m0->m_flags & M_PKTHDR) { 1203 MGETHDR(n, wait, m0->m_type); 1204 if (n == NULL) 1205 return (NULL); 1206 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 1207 n->m_pkthdr.len = m0->m_pkthdr.len - len0; 1208 m0->m_pkthdr.len = len0; 1209 if (m->m_flags & M_EXT) 1210 goto extpacket; 1211 if (remain > MHLEN) { 1212 /* m can't be the lead packet */ 1213 MH_ALIGN(n, 0); 1214 n->m_next = m_split(m, len, wait); 1215 if (n->m_next == NULL) { 1216 (void) m_free(n); 1217 return (NULL); 1218 } else { 1219 n->m_len = 0; 1220 return (n); 1221 } 1222 } else 1223 MH_ALIGN(n, remain); 1224 } else if (remain == 0) { 1225 n = m->m_next; 1226 m->m_next = NULL; 1227 return (n); 1228 } else { 1229 MGET(n, wait, m->m_type); 1230 if (n == NULL) 1231 return (NULL); 1232 M_ALIGN(n, remain); 1233 } 1234extpacket: 1235 if (m->m_flags & M_EXT) { 1236 n->m_data = m->m_data + len; 1237 mb_dupcl(n, m); 1238 } else { 1239 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 1240 } 1241 n->m_len = remain; 1242 m->m_len = len; 1243 n->m_next = m->m_next; 1244 m->m_next = NULL; 1245 return (n); 1246} 1247/* 1248 * Routine to copy from device local memory into mbufs. 1249 * Note that `off' argument is offset into first mbuf of target chain from 1250 * which to begin copying the data to. 1251 */ 1252struct mbuf * 1253m_devget(char *buf, int totlen, int off, struct ifnet *ifp, 1254 void (*copy)(char *from, caddr_t to, u_int len)) 1255{ 1256 struct mbuf *m; 1257 struct mbuf *top = NULL, **mp = ⊤ 1258 int len; 1259 1260 if (off < 0 || off > MHLEN) 1261 return (NULL); 1262 1263 while (totlen > 0) { 1264 if (top == NULL) { /* First one, must be PKTHDR */ 1265 if (totlen + off >= MINCLSIZE) { 1266 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1267 len = MCLBYTES; 1268 } else { 1269 m = m_gethdr(M_NOWAIT, MT_DATA); 1270 len = MHLEN; 1271 1272 /* Place initial small packet/header at end of mbuf */ 1273 if (m && totlen + off + max_linkhdr <= MLEN) { 1274 m->m_data += max_linkhdr; 1275 len -= max_linkhdr; 1276 } 1277 } 1278 if (m == NULL) 1279 return NULL; 1280 m->m_pkthdr.rcvif = ifp; 1281 m->m_pkthdr.len = totlen; 1282 } else { 1283 if (totlen + off >= MINCLSIZE) { 1284 m = m_getcl(M_NOWAIT, MT_DATA, 0); 1285 len = MCLBYTES; 1286 } else { 1287 m = m_get(M_NOWAIT, MT_DATA); 1288 len = MLEN; 1289 } 1290 if (m == NULL) { 1291 m_freem(top); 1292 return NULL; 1293 } 1294 } 1295 if (off) { 1296 m->m_data += off; 1297 len -= off; 1298 off = 0; 1299 } 1300 m->m_len = len = min(totlen, len); 1301 if (copy) 1302 copy(buf, mtod(m, caddr_t), (u_int)len); 1303 else 1304 bcopy(buf, mtod(m, caddr_t), (u_int)len); 1305 buf += len; 1306 *mp = m; 1307 mp = &m->m_next; 1308 totlen -= len; 1309 } 1310 return (top); 1311} 1312 1313/* 1314 * Copy data from a buffer back into the indicated mbuf chain, 1315 * starting "off" bytes from the beginning, extending the mbuf 1316 * chain if necessary. 1317 */ 1318void 1319m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp) 1320{ 1321 int mlen; 1322 struct mbuf *m = m0, *n; 1323 int totlen = 0; 1324 1325 if (m0 == NULL) 1326 return; 1327 while (off > (mlen = m->m_len)) { 1328 off -= mlen; 1329 totlen += mlen; 1330 if (m->m_next == NULL) { 1331 n = m_get(M_NOWAIT, m->m_type); 1332 if (n == NULL) 1333 goto out; 1334 bzero(mtod(n, caddr_t), MLEN); 1335 n->m_len = min(MLEN, len + off); 1336 m->m_next = n; 1337 } 1338 m = m->m_next; 1339 } 1340 while (len > 0) { 1341 if (m->m_next == NULL && (len > m->m_len - off)) { 1342 m->m_len += min(len - (m->m_len - off), 1343 M_TRAILINGSPACE(m)); 1344 } 1345 mlen = min (m->m_len - off, len); 1346 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 1347 cp += mlen; 1348 len -= mlen; 1349 mlen += off; 1350 off = 0; 1351 totlen += mlen; 1352 if (len == 0) 1353 break; 1354 if (m->m_next == NULL) { 1355 n = m_get(M_NOWAIT, m->m_type); 1356 if (n == NULL) 1357 break; 1358 n->m_len = min(MLEN, len); 1359 m->m_next = n; 1360 } 1361 m = m->m_next; 1362 } 1363out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 1364 m->m_pkthdr.len = totlen; 1365} 1366 1367/* 1368 * Append the specified data to the indicated mbuf chain, 1369 * Extend the mbuf chain if the new data does not fit in 1370 * existing space. 1371 * 1372 * Return 1 if able to complete the job; otherwise 0. 1373 */ 1374int 1375m_append(struct mbuf *m0, int len, c_caddr_t cp) 1376{ 1377 struct mbuf *m, *n; 1378 int remainder, space; 1379 1380 for (m = m0; m->m_next != NULL; m = m->m_next) 1381 ; 1382 remainder = len; 1383 space = M_TRAILINGSPACE(m); 1384 if (space > 0) { 1385 /* 1386 * Copy into available space. 1387 */ 1388 if (space > remainder) 1389 space = remainder; 1390 bcopy(cp, mtod(m, caddr_t) + m->m_len, space); 1391 m->m_len += space; 1392 cp += space, remainder -= space; 1393 } 1394 while (remainder > 0) { 1395 /* 1396 * Allocate a new mbuf; could check space 1397 * and allocate a cluster instead. 1398 */ 1399 n = m_get(M_NOWAIT, m->m_type); 1400 if (n == NULL) 1401 break; 1402 n->m_len = min(MLEN, remainder); 1403 bcopy(cp, mtod(n, caddr_t), n->m_len); 1404 cp += n->m_len, remainder -= n->m_len; 1405 m->m_next = n; 1406 m = n; 1407 } 1408 if (m0->m_flags & M_PKTHDR) 1409 m0->m_pkthdr.len += len - remainder; 1410 return (remainder == 0); 1411} 1412 1413/* 1414 * Apply function f to the data in an mbuf chain starting "off" bytes from 1415 * the beginning, continuing for "len" bytes. 1416 */ 1417int 1418m_apply(struct mbuf *m, int off, int len, 1419 int (*f)(void *, void *, u_int), void *arg) 1420{ 1421 u_int count; 1422 int rval; 1423 1424 KASSERT(off >= 0, ("m_apply, negative off %d", off)); 1425 KASSERT(len >= 0, ("m_apply, negative len %d", len)); 1426 while (off > 0) { 1427 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1428 if (off < m->m_len) 1429 break; 1430 off -= m->m_len; 1431 m = m->m_next; 1432 } 1433 while (len > 0) { 1434 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1435 count = min(m->m_len - off, len); 1436 rval = (*f)(arg, mtod(m, caddr_t) + off, count); 1437 if (rval) 1438 return (rval); 1439 len -= count; 1440 off = 0; 1441 m = m->m_next; 1442 } 1443 return (0); 1444} 1445 1446/* 1447 * Return a pointer to mbuf/offset of location in mbuf chain. 1448 */ 1449struct mbuf * 1450m_getptr(struct mbuf *m, int loc, int *off) 1451{ 1452 1453 while (loc >= 0) { 1454 /* Normal end of search. */ 1455 if (m->m_len > loc) { 1456 *off = loc; 1457 return (m); 1458 } else { 1459 loc -= m->m_len; 1460 if (m->m_next == NULL) { 1461 if (loc == 0) { 1462 /* Point at the end of valid data. */ 1463 *off = m->m_len; 1464 return (m); 1465 } 1466 return (NULL); 1467 } 1468 m = m->m_next; 1469 } 1470 } 1471 return (NULL); 1472} 1473 1474void 1475m_print(const struct mbuf *m, int maxlen) 1476{ 1477 int len; 1478 int pdata; 1479 const struct mbuf *m2; 1480 1481 if (m == NULL) { 1482 printf("mbuf: %p\n", m); 1483 return; 1484 } 1485 1486 if (m->m_flags & M_PKTHDR) 1487 len = m->m_pkthdr.len; 1488 else 1489 len = -1; 1490 m2 = m; 1491 while (m2 != NULL && (len == -1 || len)) { 1492 pdata = m2->m_len; 1493 if (maxlen != -1 && pdata > maxlen) 1494 pdata = maxlen; 1495 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len, 1496 m2->m_next, m2->m_flags, "\20\20freelist\17skipfw" 1497 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly" 1498 "\3eor\2pkthdr\1ext", pdata ? "" : "\n"); 1499 if (pdata) 1500 printf(", %*D\n", pdata, (u_char *)m2->m_data, "-"); 1501 if (len != -1) 1502 len -= m2->m_len; 1503 m2 = m2->m_next; 1504 } 1505 if (len > 0) 1506 printf("%d bytes unaccounted for.\n", len); 1507 return; 1508} 1509 1510u_int 1511m_fixhdr(struct mbuf *m0) 1512{ 1513 u_int len; 1514 1515 len = m_length(m0, NULL); 1516 m0->m_pkthdr.len = len; 1517 return (len); 1518} 1519 1520u_int 1521m_length(struct mbuf *m0, struct mbuf **last) 1522{ 1523 struct mbuf *m; 1524 u_int len; 1525 1526 len = 0; 1527 for (m = m0; m != NULL; m = m->m_next) { 1528 len += m->m_len; 1529 if (m->m_next == NULL) 1530 break; 1531 } 1532 if (last != NULL) 1533 *last = m; 1534 return (len); 1535} 1536 1537/* 1538 * Defragment a mbuf chain, returning the shortest possible 1539 * chain of mbufs and clusters. If allocation fails and 1540 * this cannot be completed, NULL will be returned, but 1541 * the passed in chain will be unchanged. Upon success, 1542 * the original chain will be freed, and the new chain 1543 * will be returned. 1544 * 1545 * If a non-packet header is passed in, the original 1546 * mbuf (chain?) will be returned unharmed. 1547 */ 1548struct mbuf * 1549m_defrag(struct mbuf *m0, int how) 1550{ 1551 struct mbuf *m_new = NULL, *m_final = NULL; 1552 int progress = 0, length; 1553 1554 MBUF_CHECKSLEEP(how); 1555 if (!(m0->m_flags & M_PKTHDR)) 1556 return (m0); 1557 1558 m_fixhdr(m0); /* Needed sanity check */ 1559 1560#ifdef MBUF_STRESS_TEST 1561 if (m_defragrandomfailures) { 1562 int temp = arc4random() & 0xff; 1563 if (temp == 0xba) 1564 goto nospace; 1565 } 1566#endif 1567 1568 if (m0->m_pkthdr.len > MHLEN) 1569 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1570 else 1571 m_final = m_gethdr(how, MT_DATA); 1572 1573 if (m_final == NULL) 1574 goto nospace; 1575 1576 if (m_dup_pkthdr(m_final, m0, how) == 0) 1577 goto nospace; 1578 1579 m_new = m_final; 1580 1581 while (progress < m0->m_pkthdr.len) { 1582 length = m0->m_pkthdr.len - progress; 1583 if (length > MCLBYTES) 1584 length = MCLBYTES; 1585 1586 if (m_new == NULL) { 1587 if (length > MLEN) 1588 m_new = m_getcl(how, MT_DATA, 0); 1589 else 1590 m_new = m_get(how, MT_DATA); 1591 if (m_new == NULL) 1592 goto nospace; 1593 } 1594 1595 m_copydata(m0, progress, length, mtod(m_new, caddr_t)); 1596 progress += length; 1597 m_new->m_len = length; 1598 if (m_new != m_final) 1599 m_cat(m_final, m_new); 1600 m_new = NULL; 1601 } 1602#ifdef MBUF_STRESS_TEST 1603 if (m0->m_next == NULL) 1604 m_defraguseless++; 1605#endif 1606 m_freem(m0); 1607 m0 = m_final; 1608#ifdef MBUF_STRESS_TEST 1609 m_defragpackets++; 1610 m_defragbytes += m0->m_pkthdr.len; 1611#endif 1612 return (m0); 1613nospace: 1614#ifdef MBUF_STRESS_TEST 1615 m_defragfailure++; 1616#endif 1617 if (m_final) 1618 m_freem(m_final); 1619 return (NULL); 1620} 1621 1622/* 1623 * Defragment an mbuf chain, returning at most maxfrags separate 1624 * mbufs+clusters. If this is not possible NULL is returned and 1625 * the original mbuf chain is left in it's present (potentially 1626 * modified) state. We use two techniques: collapsing consecutive 1627 * mbufs and replacing consecutive mbufs by a cluster. 1628 * 1629 * NB: this should really be named m_defrag but that name is taken 1630 */ 1631struct mbuf * 1632m_collapse(struct mbuf *m0, int how, int maxfrags) 1633{ 1634 struct mbuf *m, *n, *n2, **prev; 1635 u_int curfrags; 1636 1637 /* 1638 * Calculate the current number of frags. 1639 */ 1640 curfrags = 0; 1641 for (m = m0; m != NULL; m = m->m_next) 1642 curfrags++; 1643 /* 1644 * First, try to collapse mbufs. Note that we always collapse 1645 * towards the front so we don't need to deal with moving the 1646 * pkthdr. This may be suboptimal if the first mbuf has much 1647 * less data than the following. 1648 */ 1649 m = m0; 1650again: 1651 for (;;) { 1652 n = m->m_next; 1653 if (n == NULL) 1654 break; 1655 if (M_WRITABLE(m) && 1656 n->m_len < M_TRAILINGSPACE(m)) { 1657 bcopy(mtod(n, void *), mtod(m, char *) + m->m_len, 1658 n->m_len); 1659 m->m_len += n->m_len; 1660 m->m_next = n->m_next; 1661 m_free(n); 1662 if (--curfrags <= maxfrags) 1663 return m0; 1664 } else 1665 m = n; 1666 } 1667 KASSERT(maxfrags > 1, 1668 ("maxfrags %u, but normal collapse failed", maxfrags)); 1669 /* 1670 * Collapse consecutive mbufs to a cluster. 1671 */ 1672 prev = &m0->m_next; /* NB: not the first mbuf */ 1673 while ((n = *prev) != NULL) { 1674 if ((n2 = n->m_next) != NULL && 1675 n->m_len + n2->m_len < MCLBYTES) { 1676 m = m_getcl(how, MT_DATA, 0); 1677 if (m == NULL) 1678 goto bad; 1679 bcopy(mtod(n, void *), mtod(m, void *), n->m_len); 1680 bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len, 1681 n2->m_len); 1682 m->m_len = n->m_len + n2->m_len; 1683 m->m_next = n2->m_next; 1684 *prev = m; 1685 m_free(n); 1686 m_free(n2); 1687 if (--curfrags <= maxfrags) /* +1 cl -2 mbufs */ 1688 return m0; 1689 /* 1690 * Still not there, try the normal collapse 1691 * again before we allocate another cluster. 1692 */ 1693 goto again; 1694 } 1695 prev = &n->m_next; 1696 } 1697 /* 1698 * No place where we can collapse to a cluster; punt. 1699 * This can occur if, for example, you request 2 frags 1700 * but the packet requires that both be clusters (we 1701 * never reallocate the first mbuf to avoid moving the 1702 * packet header). 1703 */ 1704bad: 1705 return NULL; 1706} 1707 1708#ifdef MBUF_STRESS_TEST 1709 1710/* 1711 * Fragment an mbuf chain. There's no reason you'd ever want to do 1712 * this in normal usage, but it's great for stress testing various 1713 * mbuf consumers. 1714 * 1715 * If fragmentation is not possible, the original chain will be 1716 * returned. 1717 * 1718 * Possible length values: 1719 * 0 no fragmentation will occur 1720 * > 0 each fragment will be of the specified length 1721 * -1 each fragment will be the same random value in length 1722 * -2 each fragment's length will be entirely random 1723 * (Random values range from 1 to 256) 1724 */ 1725struct mbuf * 1726m_fragment(struct mbuf *m0, int how, int length) 1727{ 1728 struct mbuf *m_new = NULL, *m_final = NULL; 1729 int progress = 0; 1730 1731 if (!(m0->m_flags & M_PKTHDR)) 1732 return (m0); 1733 1734 if ((length == 0) || (length < -2)) 1735 return (m0); 1736 1737 m_fixhdr(m0); /* Needed sanity check */ 1738 1739 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1740 1741 if (m_final == NULL) 1742 goto nospace; 1743 1744 if (m_dup_pkthdr(m_final, m0, how) == 0) 1745 goto nospace; 1746 1747 m_new = m_final; 1748 1749 if (length == -1) 1750 length = 1 + (arc4random() & 255); 1751 1752 while (progress < m0->m_pkthdr.len) { 1753 int fraglen; 1754 1755 if (length > 0) 1756 fraglen = length; 1757 else 1758 fraglen = 1 + (arc4random() & 255); 1759 if (fraglen > m0->m_pkthdr.len - progress) 1760 fraglen = m0->m_pkthdr.len - progress; 1761 1762 if (fraglen > MCLBYTES) 1763 fraglen = MCLBYTES; 1764 1765 if (m_new == NULL) { 1766 m_new = m_getcl(how, MT_DATA, 0); 1767 if (m_new == NULL) 1768 goto nospace; 1769 } 1770 1771 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t)); 1772 progress += fraglen; 1773 m_new->m_len = fraglen; 1774 if (m_new != m_final) 1775 m_cat(m_final, m_new); 1776 m_new = NULL; 1777 } 1778 m_freem(m0); 1779 m0 = m_final; 1780 return (m0); 1781nospace: 1782 if (m_final) 1783 m_freem(m_final); 1784 /* Return the original chain on failure */ 1785 return (m0); 1786} 1787 1788#endif 1789 1790/* 1791 * Copy the contents of uio into a properly sized mbuf chain. 1792 */ 1793struct mbuf * 1794m_uiotombuf(struct uio *uio, int how, int len, int align, int flags) 1795{ 1796 struct mbuf *m, *mb; 1797 int error, length; 1798 ssize_t total; 1799 int progress = 0; 1800 1801 /* 1802 * len can be zero or an arbitrary large value bound by 1803 * the total data supplied by the uio. 1804 */ 1805 if (len > 0) 1806 total = min(uio->uio_resid, len); 1807 else 1808 total = uio->uio_resid; 1809 1810 /* 1811 * The smallest unit returned by m_getm2() is a single mbuf 1812 * with pkthdr. We can't align past it. 1813 */ 1814 if (align >= MHLEN) 1815 return (NULL); 1816 1817 /* 1818 * Give us the full allocation or nothing. 1819 * If len is zero return the smallest empty mbuf. 1820 */ 1821 m = m_getm2(NULL, max(total + align, 1), how, MT_DATA, flags); 1822 if (m == NULL) 1823 return (NULL); 1824 m->m_data += align; 1825 1826 /* Fill all mbufs with uio data and update header information. */ 1827 for (mb = m; mb != NULL; mb = mb->m_next) { 1828 length = min(M_TRAILINGSPACE(mb), total - progress); 1829 1830 error = uiomove(mtod(mb, void *), length, uio); 1831 if (error) { 1832 m_freem(m); 1833 return (NULL); 1834 } 1835 1836 mb->m_len = length; 1837 progress += length; 1838 if (flags & M_PKTHDR) 1839 m->m_pkthdr.len += length; 1840 } 1841 KASSERT(progress == total, ("%s: progress != total", __func__)); 1842 1843 return (m); 1844} 1845 1846/* 1847 * Copy an mbuf chain into a uio limited by len if set. 1848 */ 1849int 1850m_mbuftouio(struct uio *uio, struct mbuf *m, int len) 1851{ 1852 int error, length, total; 1853 int progress = 0; 1854 1855 if (len > 0) 1856 total = min(uio->uio_resid, len); 1857 else 1858 total = uio->uio_resid; 1859 1860 /* Fill the uio with data from the mbufs. */ 1861 for (; m != NULL; m = m->m_next) { 1862 length = min(m->m_len, total - progress); 1863 1864 error = uiomove(mtod(m, void *), length, uio); 1865 if (error) 1866 return (error); 1867 1868 progress += length; 1869 } 1870 1871 return (0); 1872} 1873 1874/* 1875 * Set the m_data pointer of a newly-allocated mbuf 1876 * to place an object of the specified size at the 1877 * end of the mbuf, longword aligned. 1878 */ 1879void 1880m_align(struct mbuf *m, int len) 1881{ 1882 int adjust; 1883 1884 if (m->m_flags & M_EXT) 1885 adjust = m->m_ext.ext_size - len; 1886 else if (m->m_flags & M_PKTHDR) 1887 adjust = MHLEN - len; 1888 else 1889 adjust = MLEN - len; 1890 m->m_data += adjust &~ (sizeof(long)-1); 1891} 1892 1893/* 1894 * Create a writable copy of the mbuf chain. While doing this 1895 * we compact the chain with a goal of producing a chain with 1896 * at most two mbufs. The second mbuf in this chain is likely 1897 * to be a cluster. The primary purpose of this work is to create 1898 * a writable packet for encryption, compression, etc. The 1899 * secondary goal is to linearize the data so the data can be 1900 * passed to crypto hardware in the most efficient manner possible. 1901 */ 1902struct mbuf * 1903m_unshare(struct mbuf *m0, int how) 1904{ 1905 struct mbuf *m, *mprev; 1906 struct mbuf *n, *mfirst, *mlast; 1907 int len, off; 1908 1909 mprev = NULL; 1910 for (m = m0; m != NULL; m = mprev->m_next) { 1911 /* 1912 * Regular mbufs are ignored unless there's a cluster 1913 * in front of it that we can use to coalesce. We do 1914 * the latter mainly so later clusters can be coalesced 1915 * also w/o having to handle them specially (i.e. convert 1916 * mbuf+cluster -> cluster). This optimization is heavily 1917 * influenced by the assumption that we're running over 1918 * Ethernet where MCLBYTES is large enough that the max 1919 * packet size will permit lots of coalescing into a 1920 * single cluster. This in turn permits efficient 1921 * crypto operations, especially when using hardware. 1922 */ 1923 if ((m->m_flags & M_EXT) == 0) { 1924 if (mprev && (mprev->m_flags & M_EXT) && 1925 m->m_len <= M_TRAILINGSPACE(mprev)) { 1926 /* XXX: this ignores mbuf types */ 1927 memcpy(mtod(mprev, caddr_t) + mprev->m_len, 1928 mtod(m, caddr_t), m->m_len); 1929 mprev->m_len += m->m_len; 1930 mprev->m_next = m->m_next; /* unlink from chain */ 1931 m_free(m); /* reclaim mbuf */ 1932#if 0 1933 newipsecstat.ips_mbcoalesced++; 1934#endif 1935 } else { 1936 mprev = m; 1937 } 1938 continue; 1939 } 1940 /* 1941 * Writable mbufs are left alone (for now). 1942 */ 1943 if (M_WRITABLE(m)) { 1944 mprev = m; 1945 continue; 1946 } 1947 1948 /* 1949 * Not writable, replace with a copy or coalesce with 1950 * the previous mbuf if possible (since we have to copy 1951 * it anyway, we try to reduce the number of mbufs and 1952 * clusters so that future work is easier). 1953 */ 1954 KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags)); 1955 /* NB: we only coalesce into a cluster or larger */ 1956 if (mprev != NULL && (mprev->m_flags & M_EXT) && 1957 m->m_len <= M_TRAILINGSPACE(mprev)) { 1958 /* XXX: this ignores mbuf types */ 1959 memcpy(mtod(mprev, caddr_t) + mprev->m_len, 1960 mtod(m, caddr_t), m->m_len); 1961 mprev->m_len += m->m_len; 1962 mprev->m_next = m->m_next; /* unlink from chain */ 1963 m_free(m); /* reclaim mbuf */ 1964#if 0 1965 newipsecstat.ips_clcoalesced++; 1966#endif 1967 continue; 1968 } 1969 1970 /* 1971 * Allocate new space to hold the copy... 1972 */ 1973 /* XXX why can M_PKTHDR be set past the first mbuf? */ 1974 if (mprev == NULL && (m->m_flags & M_PKTHDR)) { 1975 /* 1976 * NB: if a packet header is present we must 1977 * allocate the mbuf separately from any cluster 1978 * because M_MOVE_PKTHDR will smash the data 1979 * pointer and drop the M_EXT marker. 1980 */ 1981 MGETHDR(n, how, m->m_type); 1982 if (n == NULL) { 1983 m_freem(m0); 1984 return (NULL); 1985 } 1986 M_MOVE_PKTHDR(n, m); 1987 MCLGET(n, how); 1988 if ((n->m_flags & M_EXT) == 0) { 1989 m_free(n); 1990 m_freem(m0); 1991 return (NULL); 1992 } 1993 } else { 1994 n = m_getcl(how, m->m_type, m->m_flags); 1995 if (n == NULL) { 1996 m_freem(m0); 1997 return (NULL); 1998 } 1999 } 2000 /* 2001 * ... and copy the data. We deal with jumbo mbufs 2002 * (i.e. m_len > MCLBYTES) by splitting them into 2003 * clusters. We could just malloc a buffer and make 2004 * it external but too many device drivers don't know 2005 * how to break up the non-contiguous memory when 2006 * doing DMA. 2007 */ 2008 len = m->m_len; 2009 off = 0; 2010 mfirst = n; 2011 mlast = NULL; 2012 for (;;) { 2013 int cc = min(len, MCLBYTES); 2014 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc); 2015 n->m_len = cc; 2016 if (mlast != NULL) 2017 mlast->m_next = n; 2018 mlast = n; 2019#if 0 2020 newipsecstat.ips_clcopied++; 2021#endif 2022 2023 len -= cc; 2024 if (len <= 0) 2025 break; 2026 off += cc; 2027 2028 n = m_getcl(how, m->m_type, m->m_flags); 2029 if (n == NULL) { 2030 m_freem(mfirst); 2031 m_freem(m0); 2032 return (NULL); 2033 } 2034 } 2035 n->m_next = m->m_next; 2036 if (mprev == NULL) 2037 m0 = mfirst; /* new head of chain */ 2038 else 2039 mprev->m_next = mfirst; /* replace old mbuf */ 2040 m_free(m); /* release old mbuf */ 2041 mprev = mfirst; 2042 } 2043 return (m0); 2044} 2045 2046#ifdef MBUF_PROFILING 2047 2048#define MP_BUCKETS 32 /* don't just change this as things may overflow.*/ 2049struct mbufprofile { 2050 uintmax_t wasted[MP_BUCKETS]; 2051 uintmax_t used[MP_BUCKETS]; 2052 uintmax_t segments[MP_BUCKETS]; 2053} mbprof; 2054 2055#define MP_MAXDIGITS 21 /* strlen("16,000,000,000,000,000,000") == 21 */ 2056#define MP_NUMLINES 6 2057#define MP_NUMSPERLINE 16 2058#define MP_EXTRABYTES 64 /* > strlen("used:\nwasted:\nsegments:\n") */ 2059/* work out max space needed and add a bit of spare space too */ 2060#define MP_MAXLINE ((MP_MAXDIGITS+1) * MP_NUMSPERLINE) 2061#define MP_BUFSIZE ((MP_MAXLINE * MP_NUMLINES) + 1 + MP_EXTRABYTES) 2062 2063char mbprofbuf[MP_BUFSIZE]; 2064 2065void 2066m_profile(struct mbuf *m) 2067{ 2068 int segments = 0; 2069 int used = 0; 2070 int wasted = 0; 2071 2072 while (m) { 2073 segments++; 2074 used += m->m_len; 2075 if (m->m_flags & M_EXT) { 2076 wasted += MHLEN - sizeof(m->m_ext) + 2077 m->m_ext.ext_size - m->m_len; 2078 } else { 2079 if (m->m_flags & M_PKTHDR) 2080 wasted += MHLEN - m->m_len; 2081 else 2082 wasted += MLEN - m->m_len; 2083 } 2084 m = m->m_next; 2085 } 2086 /* be paranoid.. it helps */ 2087 if (segments > MP_BUCKETS - 1) 2088 segments = MP_BUCKETS - 1; 2089 if (used > 100000) 2090 used = 100000; 2091 if (wasted > 100000) 2092 wasted = 100000; 2093 /* store in the appropriate bucket */ 2094 /* don't bother locking. if it's slightly off, so what? */ 2095 mbprof.segments[segments]++; 2096 mbprof.used[fls(used)]++; 2097 mbprof.wasted[fls(wasted)]++; 2098} 2099 2100static void 2101mbprof_textify(void) 2102{ 2103 int offset; 2104 char *c; 2105 uint64_t *p; 2106 2107 2108 p = &mbprof.wasted[0]; 2109 c = mbprofbuf; 2110 offset = snprintf(c, MP_MAXLINE + 10, 2111 "wasted:\n" 2112 "%ju %ju %ju %ju %ju %ju %ju %ju " 2113 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2114 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2115 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2116#ifdef BIG_ARRAY 2117 p = &mbprof.wasted[16]; 2118 c += offset; 2119 offset = snprintf(c, MP_MAXLINE, 2120 "%ju %ju %ju %ju %ju %ju %ju %ju " 2121 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2122 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2123 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2124#endif 2125 p = &mbprof.used[0]; 2126 c += offset; 2127 offset = snprintf(c, MP_MAXLINE + 10, 2128 "used:\n" 2129 "%ju %ju %ju %ju %ju %ju %ju %ju " 2130 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2131 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2132 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2133#ifdef BIG_ARRAY 2134 p = &mbprof.used[16]; 2135 c += offset; 2136 offset = snprintf(c, MP_MAXLINE, 2137 "%ju %ju %ju %ju %ju %ju %ju %ju " 2138 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2139 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2140 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2141#endif 2142 p = &mbprof.segments[0]; 2143 c += offset; 2144 offset = snprintf(c, MP_MAXLINE + 10, 2145 "segments:\n" 2146 "%ju %ju %ju %ju %ju %ju %ju %ju " 2147 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2148 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2149 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2150#ifdef BIG_ARRAY 2151 p = &mbprof.segments[16]; 2152 c += offset; 2153 offset = snprintf(c, MP_MAXLINE, 2154 "%ju %ju %ju %ju %ju %ju %ju %ju " 2155 "%ju %ju %ju %ju %ju %ju %ju %jju", 2156 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2157 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2158#endif 2159} 2160 2161static int 2162mbprof_handler(SYSCTL_HANDLER_ARGS) 2163{ 2164 int error; 2165 2166 mbprof_textify(); 2167 error = SYSCTL_OUT(req, mbprofbuf, strlen(mbprofbuf) + 1); 2168 return (error); 2169} 2170 2171static int 2172mbprof_clr_handler(SYSCTL_HANDLER_ARGS) 2173{ 2174 int clear, error; 2175 2176 clear = 0; 2177 error = sysctl_handle_int(oidp, &clear, 0, req); 2178 if (error || !req->newptr) 2179 return (error); 2180 2181 if (clear) { 2182 bzero(&mbprof, sizeof(mbprof)); 2183 } 2184 2185 return (error); 2186} 2187 2188 2189SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofile, CTLTYPE_STRING|CTLFLAG_RD, 2190 NULL, 0, mbprof_handler, "A", "mbuf profiling statistics"); 2191 2192SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofileclr, CTLTYPE_INT|CTLFLAG_RW, 2193 NULL, 0, mbprof_clr_handler, "I", "clear mbuf profiling statistics"); 2194#endif 2195 2196