uipc_mbuf.c revision 52201
11541Srgrimes/* 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 * 3. All advertising materials mentioning features or use of this software 141541Srgrimes * must display the following acknowledgement: 151541Srgrimes * This product includes software developed by the University of 161541Srgrimes * California, Berkeley and its contributors. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 331541Srgrimes * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 3450477Speter * $FreeBSD: head/sys/kern/uipc_mbuf.c 52201 1999-10-13 09:55:42Z alfred $ 351541Srgrimes */ 361541Srgrimes 3748579Smsmith#include "opt_param.h" 381541Srgrimes#include <sys/param.h> 391541Srgrimes#include <sys/systm.h> 4032036Sbde#include <sys/malloc.h> 411541Srgrimes#include <sys/mbuf.h> 421541Srgrimes#include <sys/kernel.h> 4323081Swollman#include <sys/sysctl.h> 441541Srgrimes#include <sys/domain.h> 451541Srgrimes#include <sys/protosw.h> 461541Srgrimes 471541Srgrimes#include <vm/vm.h> 489759Sbde#include <vm/vm_kern.h> 4912662Sdg#include <vm/vm_extern.h> 501541Srgrimes 5110653Sdgstatic void mbinit __P((void *)); 5210358SjulianSYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbinit, NULL) 5310358Sjulian 549759Sbdestruct mbuf *mbutl; 551541Srgrimeschar *mclrefcnt; 569759Sbdestruct mbstat mbstat; 5715689Swollmanstruct mbuf *mmbfree; 589759Sbdeunion mcluster *mclfree; 599759Sbdeint max_linkhdr; 609759Sbdeint max_protohdr; 619759Sbdeint max_hdr; 629759Sbdeint max_datalen; 6348579Smsmithint nmbclusters; 6448579Smsmithint nmbufs; 651541Srgrimes 6644078SdfrSYSCTL_DECL(_kern_ipc); 6723081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 6823081Swollman &max_linkhdr, 0, ""); 6923081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 7023081Swollman &max_protohdr, 0, ""); 7123081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 7223081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 7323081Swollman &max_datalen, 0, ""); 7423081SwollmanSYSCTL_STRUCT(_kern_ipc, KIPC_MBSTAT, mbstat, CTLFLAG_RW, &mbstat, mbstat, ""); 7548579SmsmithSYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD, 7648579Smsmith &nmbclusters, 0, "Maximum number of mbuf clusters avaliable"); 7748579Smsmith#ifndef NMBCLUSTERS 7848579Smsmith#define NMBCLUSTERS (512 + MAXUSERS * 16) 7948579Smsmith#endif 8048579SmsmithTUNABLE_INT_DECL("kern.ipc.nmbclusters", NMBCLUSTERS, nmbclusters); 8148579SmsmithTUNABLE_INT_DECL("kern.ipc.nmbufs", NMBCLUSTERS * 4, nmbufs); /* XXX fixup? */ 8223081Swollman 8312819Sphkstatic void m_reclaim __P((void)); 8412819Sphk 8515736Sphk/* "number of clusters of pages" */ 8615736Sphk#define NCL_INIT 1 8715736Sphk 8815744Sphk#define NMB_INIT 16 8915744Sphk 9010358Sjulian/* ARGSUSED*/ 9110358Sjulianstatic void 9212569Sbdembinit(dummy) 9312569Sbde void *dummy; 941541Srgrimes{ 951541Srgrimes int s; 961541Srgrimes 9715689Swollman mmbfree = NULL; mclfree = NULL; 9823081Swollman mbstat.m_msize = MSIZE; 9923081Swollman mbstat.m_mclbytes = MCLBYTES; 10023081Swollman mbstat.m_minclsize = MINCLSIZE; 10123081Swollman mbstat.m_mlen = MLEN; 10223081Swollman mbstat.m_mhlen = MHLEN; 10323081Swollman 1041541Srgrimes s = splimp(); 10515689Swollman if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0) 10615689Swollman goto bad; 10722671Swollman#if MCLBYTES <= PAGE_SIZE 1081541Srgrimes if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0) 1091541Srgrimes goto bad; 11022671Swollman#else 11122671Swollman /* It's OK to call contigmalloc in this context. */ 11232036Sbde if (m_clalloc(16, M_WAIT) == 0) 11322671Swollman goto bad; 11422671Swollman#endif 1151541Srgrimes splx(s); 1161541Srgrimes return; 1171541Srgrimesbad: 1181541Srgrimes panic("mbinit"); 1191541Srgrimes} 1201541Srgrimes 1211541Srgrimes/* 12215689Swollman * Allocate at least nmb mbufs and place on mbuf free list. 12315689Swollman * Must be called at splimp. 12415689Swollman */ 12515689Swollman/* ARGSUSED */ 12615689Swollmanint 12732036Sbdem_mballoc(nmb, how) 12815689Swollman register int nmb; 12932036Sbde int how; 13015689Swollman{ 13115689Swollman register caddr_t p; 13215689Swollman register int i; 13315689Swollman int nbytes; 13415689Swollman 13515689Swollman /* Once we run out of map space, it will be impossible to get 13615689Swollman * any more (nothing is ever freed back to the map) (XXX which 13715689Swollman * is dumb). (however you are not dead as m_reclaim might 13815689Swollman * still be able to free a substantial amount of space). 13915689Swollman */ 14015689Swollman if (mb_map_full) 14115689Swollman return (0); 14215689Swollman 14315689Swollman nbytes = round_page(nmb * MSIZE); 14422899Swollman p = (caddr_t)kmem_malloc(mb_map, nbytes, M_NOWAIT); 14532036Sbde if (p == 0 && how == M_WAIT) { 14622899Swollman mbstat.m_wait++; 14722899Swollman p = (caddr_t)kmem_malloc(mb_map, nbytes, M_WAITOK); 14822899Swollman } 14922899Swollman 15015689Swollman /* 15132036Sbde * Either the map is now full, or `how' is M_NOWAIT and there 15215689Swollman * are no pages left. 15315689Swollman */ 15415689Swollman if (p == NULL) 15515689Swollman return (0); 15615689Swollman 15715689Swollman nmb = nbytes / MSIZE; 15815689Swollman for (i = 0; i < nmb; i++) { 15915689Swollman ((struct mbuf *)p)->m_next = mmbfree; 16015689Swollman mmbfree = (struct mbuf *)p; 16115689Swollman p += MSIZE; 16215689Swollman } 16315689Swollman mbstat.m_mbufs += nmb; 16415689Swollman return (1); 16515689Swollman} 16615689Swollman 16722671Swollman#if MCLBYTES > PAGE_SIZE 16822899Swollmanstatic int i_want_my_mcl; 16922671Swollman 17022899Swollmanstatic void 17122671Swollmankproc_mclalloc(void) 17222671Swollman{ 17322671Swollman int status; 17422671Swollman 17522671Swollman while (1) { 17622671Swollman tsleep(&i_want_my_mcl, PVM, "mclalloc", 0); 17722671Swollman 17822671Swollman for (; i_want_my_mcl; i_want_my_mcl--) { 17932036Sbde if (m_clalloc(1, M_WAIT) == 0) 18022671Swollman printf("m_clalloc failed even in process context!\n"); 18122671Swollman } 18222671Swollman } 18322671Swollman} 18422671Swollman 18522671Swollmanstatic struct proc *mclallocproc; 18622671Swollmanstatic struct kproc_desc mclalloc_kp = { 18722671Swollman "mclalloc", 18822671Swollman kproc_mclalloc, 18922671Swollman &mclallocproc 19022671Swollman}; 19148391SpeterSYSINIT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start, 19222671Swollman &mclalloc_kp); 19322671Swollman#endif 19422671Swollman 19515689Swollman/* 1961541Srgrimes * Allocate some number of mbuf clusters 1971541Srgrimes * and place on cluster free list. 1981541Srgrimes * Must be called at splimp. 1991541Srgrimes */ 2001541Srgrimes/* ARGSUSED */ 2011549Srgrimesint 20232036Sbdem_clalloc(ncl, how) 2031541Srgrimes register int ncl; 20432036Sbde int how; 2051541Srgrimes{ 2061541Srgrimes register caddr_t p; 2071541Srgrimes register int i; 2081541Srgrimes int npg; 2091541Srgrimes 2107066Sdg /* 2117066Sdg * Once we run out of map space, it will be impossible 2127066Sdg * to get any more (nothing is ever freed back to the 2137066Sdg * map). 2147066Sdg */ 21522899Swollman if (mb_map_full) { 21622899Swollman mbstat.m_drops++; 2177066Sdg return (0); 21822899Swollman } 2197066Sdg 22022671Swollman#if MCLBYTES > PAGE_SIZE 22132036Sbde if (how != M_WAIT) { 22222671Swollman i_want_my_mcl += ncl; 22322671Swollman wakeup(&i_want_my_mcl); 22422899Swollman mbstat.m_wait++; 22522671Swollman p = 0; 22622671Swollman } else { 22722671Swollman p = contigmalloc1(MCLBYTES * ncl, M_DEVBUF, M_WAITOK, 0ul, 22822671Swollman ~0ul, PAGE_SIZE, 0, mb_map); 22922671Swollman } 23022671Swollman#else 23115543Sphk npg = ncl; 23221737Sdg p = (caddr_t)kmem_malloc(mb_map, ctob(npg), 23332036Sbde how != M_WAIT ? M_NOWAIT : M_WAITOK); 23422671Swollman ncl = ncl * PAGE_SIZE / MCLBYTES; 23522671Swollman#endif 2367066Sdg /* 23732036Sbde * Either the map is now full, or `how' is M_NOWAIT and there 2387066Sdg * are no pages left. 2397066Sdg */ 24022899Swollman if (p == NULL) { 24122899Swollman mbstat.m_drops++; 2421541Srgrimes return (0); 24322899Swollman } 2447066Sdg 2451541Srgrimes for (i = 0; i < ncl; i++) { 2461541Srgrimes ((union mcluster *)p)->mcl_next = mclfree; 2471541Srgrimes mclfree = (union mcluster *)p; 2481541Srgrimes p += MCLBYTES; 2491541Srgrimes mbstat.m_clfree++; 2501541Srgrimes } 2511541Srgrimes mbstat.m_clusters += ncl; 2521541Srgrimes return (1); 2531541Srgrimes} 2541541Srgrimes 2551541Srgrimes/* 25645615Sdes * When MGET fails, ask protocols to free space when short of memory, 2571541Srgrimes * then re-attempt to allocate an mbuf. 2581541Srgrimes */ 2591541Srgrimesstruct mbuf * 2601541Srgrimesm_retry(i, t) 2611541Srgrimes int i, t; 2621541Srgrimes{ 2631541Srgrimes register struct mbuf *m; 2641541Srgrimes 26537878Sdg /* 26637878Sdg * Must only do the reclaim if not in an interrupt context. 26737878Sdg */ 26837878Sdg if (i == M_WAIT) 26937878Sdg m_reclaim(); 2701541Srgrimes#define m_retry(i, t) (struct mbuf *)0 2711541Srgrimes MGET(m, i, t); 2721541Srgrimes#undef m_retry 27336675Sdg if (m != NULL) { 2746669Sdg mbstat.m_wait++; 27536675Sdg } else { 27636675Sdg if (i == M_DONTWAIT) 27736675Sdg mbstat.m_drops++; 27836675Sdg else 27936675Sdg panic("Out of mbuf clusters"); 28036675Sdg } 2811541Srgrimes return (m); 2821541Srgrimes} 2831541Srgrimes 2841541Srgrimes/* 2851541Srgrimes * As above; retry an MGETHDR. 2861541Srgrimes */ 2871541Srgrimesstruct mbuf * 2881541Srgrimesm_retryhdr(i, t) 2891541Srgrimes int i, t; 2901541Srgrimes{ 2911541Srgrimes register struct mbuf *m; 2921541Srgrimes 29337878Sdg /* 29437878Sdg * Must only do the reclaim if not in an interrupt context. 29537878Sdg */ 29637878Sdg if (i == M_WAIT) 29737878Sdg m_reclaim(); 2981541Srgrimes#define m_retryhdr(i, t) (struct mbuf *)0 2991541Srgrimes MGETHDR(m, i, t); 3001541Srgrimes#undef m_retryhdr 30136675Sdg if (m != NULL) { 3026669Sdg mbstat.m_wait++; 30336675Sdg } else { 30436675Sdg if (i == M_DONTWAIT) 30536675Sdg mbstat.m_drops++; 30636675Sdg else 30736675Sdg panic("Out of mbuf clusters"); 30836675Sdg } 3091541Srgrimes return (m); 3101541Srgrimes} 3111541Srgrimes 31212819Sphkstatic void 3131541Srgrimesm_reclaim() 3141541Srgrimes{ 3151541Srgrimes register struct domain *dp; 3161541Srgrimes register struct protosw *pr; 3171541Srgrimes int s = splimp(); 3181541Srgrimes 3191541Srgrimes for (dp = domains; dp; dp = dp->dom_next) 3201541Srgrimes for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 3211541Srgrimes if (pr->pr_drain) 3221541Srgrimes (*pr->pr_drain)(); 3231541Srgrimes splx(s); 3241541Srgrimes mbstat.m_drain++; 3251541Srgrimes} 3261541Srgrimes 3271541Srgrimes/* 3281541Srgrimes * Space allocation routines. 3291541Srgrimes * These are also available as macros 3301541Srgrimes * for critical paths. 3311541Srgrimes */ 3321541Srgrimesstruct mbuf * 33332036Sbdem_get(how, type) 33432036Sbde int how, type; 3351541Srgrimes{ 3361541Srgrimes register struct mbuf *m; 3371541Srgrimes 33832036Sbde MGET(m, how, type); 3391541Srgrimes return (m); 3401541Srgrimes} 3411541Srgrimes 3421541Srgrimesstruct mbuf * 34332036Sbdem_gethdr(how, type) 34432036Sbde int how, type; 3451541Srgrimes{ 3461541Srgrimes register struct mbuf *m; 3471541Srgrimes 34832036Sbde MGETHDR(m, how, type); 3491541Srgrimes return (m); 3501541Srgrimes} 3511541Srgrimes 3521541Srgrimesstruct mbuf * 35332036Sbdem_getclr(how, type) 35432036Sbde int how, type; 3551541Srgrimes{ 3561541Srgrimes register struct mbuf *m; 3571541Srgrimes 35832036Sbde MGET(m, how, type); 3591541Srgrimes if (m == 0) 3601541Srgrimes return (0); 3611541Srgrimes bzero(mtod(m, caddr_t), MLEN); 3621541Srgrimes return (m); 3631541Srgrimes} 3641541Srgrimes 3651541Srgrimesstruct mbuf * 3661541Srgrimesm_free(m) 3671541Srgrimes struct mbuf *m; 3681541Srgrimes{ 3691541Srgrimes register struct mbuf *n; 3701541Srgrimes 3711541Srgrimes MFREE(m, n); 3721541Srgrimes return (n); 3731541Srgrimes} 3741541Srgrimes 3751541Srgrimesvoid 3761541Srgrimesm_freem(m) 3771541Srgrimes register struct mbuf *m; 3781541Srgrimes{ 3791541Srgrimes register struct mbuf *n; 3801541Srgrimes 3811541Srgrimes if (m == NULL) 3821541Srgrimes return; 3831541Srgrimes do { 3841541Srgrimes MFREE(m, n); 3853308Sphk m = n; 3863308Sphk } while (m); 3871541Srgrimes} 3881541Srgrimes 3891541Srgrimes/* 3901541Srgrimes * Mbuffer utility routines. 3911541Srgrimes */ 3921541Srgrimes 3931541Srgrimes/* 3941541Srgrimes * Lesser-used path for M_PREPEND: 3951541Srgrimes * allocate new mbuf to prepend to chain, 3961541Srgrimes * copy junk along. 3971541Srgrimes */ 3981541Srgrimesstruct mbuf * 3991541Srgrimesm_prepend(m, len, how) 4001541Srgrimes register struct mbuf *m; 4011541Srgrimes int len, how; 4021541Srgrimes{ 4031541Srgrimes struct mbuf *mn; 4041541Srgrimes 4051541Srgrimes MGET(mn, how, m->m_type); 4061541Srgrimes if (mn == (struct mbuf *)NULL) { 4071541Srgrimes m_freem(m); 4081541Srgrimes return ((struct mbuf *)NULL); 4091541Srgrimes } 4101541Srgrimes if (m->m_flags & M_PKTHDR) { 4111541Srgrimes M_COPY_PKTHDR(mn, m); 4121541Srgrimes m->m_flags &= ~M_PKTHDR; 4131541Srgrimes } 4141541Srgrimes mn->m_next = m; 4151541Srgrimes m = mn; 4161541Srgrimes if (len < MHLEN) 4171541Srgrimes MH_ALIGN(m, len); 4181541Srgrimes m->m_len = len; 4191541Srgrimes return (m); 4201541Srgrimes} 4211541Srgrimes 4221541Srgrimes/* 4231541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 4241541Srgrimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 4251541Srgrimes * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller. 4261541Srgrimes */ 42723081Swollman#define MCFail (mbstat.m_mcfail) 4281541Srgrimes 4291541Srgrimesstruct mbuf * 4301541Srgrimesm_copym(m, off0, len, wait) 4311541Srgrimes register struct mbuf *m; 4321541Srgrimes int off0, wait; 4331541Srgrimes register int len; 4341541Srgrimes{ 4351541Srgrimes register struct mbuf *n, **np; 4361541Srgrimes register int off = off0; 4371541Srgrimes struct mbuf *top; 4381541Srgrimes int copyhdr = 0; 4391541Srgrimes 44052201Salfred KASSERT(off >= 0, ("m_copym, negative off %d", off)); 44152201Salfred KASSERT(len >= 0, ("m_copym, negative len %d", len)); 4421541Srgrimes if (off == 0 && m->m_flags & M_PKTHDR) 4431541Srgrimes copyhdr = 1; 4441541Srgrimes while (off > 0) { 44552201Salfred KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 4461541Srgrimes if (off < m->m_len) 4471541Srgrimes break; 4481541Srgrimes off -= m->m_len; 4491541Srgrimes m = m->m_next; 4501541Srgrimes } 4511541Srgrimes np = ⊤ 4521541Srgrimes top = 0; 4531541Srgrimes while (len > 0) { 4541541Srgrimes if (m == 0) { 45552201Salfred KASSERT(len == M_COPYALL, 45652201Salfred ("m_copym, length > size of mbuf chain")); 4571541Srgrimes break; 4581541Srgrimes } 4591541Srgrimes MGET(n, wait, m->m_type); 4601541Srgrimes *np = n; 4611541Srgrimes if (n == 0) 4621541Srgrimes goto nospace; 4631541Srgrimes if (copyhdr) { 4641541Srgrimes M_COPY_PKTHDR(n, m); 4651541Srgrimes if (len == M_COPYALL) 4661541Srgrimes n->m_pkthdr.len -= off0; 4671541Srgrimes else 4681541Srgrimes n->m_pkthdr.len = len; 4691541Srgrimes copyhdr = 0; 4701541Srgrimes } 4711541Srgrimes n->m_len = min(len, m->m_len - off); 4721541Srgrimes if (m->m_flags & M_EXT) { 4731541Srgrimes n->m_data = m->m_data + off; 47417663Sjulian if(!m->m_ext.ext_ref) 47517663Sjulian mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 47617663Sjulian else 47717663Sjulian (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 47817663Sjulian m->m_ext.ext_size); 4791541Srgrimes n->m_ext = m->m_ext; 4801541Srgrimes n->m_flags |= M_EXT; 4811541Srgrimes } else 4821541Srgrimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 4831541Srgrimes (unsigned)n->m_len); 4841541Srgrimes if (len != M_COPYALL) 4851541Srgrimes len -= n->m_len; 4861541Srgrimes off = 0; 4871541Srgrimes m = m->m_next; 4881541Srgrimes np = &n->m_next; 4891541Srgrimes } 4901541Srgrimes if (top == 0) 4911541Srgrimes MCFail++; 4921541Srgrimes return (top); 4931541Srgrimesnospace: 4941541Srgrimes m_freem(top); 4951541Srgrimes MCFail++; 4961541Srgrimes return (0); 4971541Srgrimes} 4981541Srgrimes 4991541Srgrimes/* 50015689Swollman * Copy an entire packet, including header (which must be present). 50115689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 50215689Swollman */ 50315689Swollmanstruct mbuf * 50415689Swollmanm_copypacket(m, how) 50515689Swollman struct mbuf *m; 50615689Swollman int how; 50715689Swollman{ 50815689Swollman struct mbuf *top, *n, *o; 50915689Swollman 51015689Swollman MGET(n, how, m->m_type); 51115689Swollman top = n; 51215689Swollman if (!n) 51315689Swollman goto nospace; 51415689Swollman 51515689Swollman M_COPY_PKTHDR(n, m); 51615689Swollman n->m_len = m->m_len; 51715689Swollman if (m->m_flags & M_EXT) { 51815689Swollman n->m_data = m->m_data; 51937350Sphk if(!m->m_ext.ext_ref) 52037350Sphk mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 52137350Sphk else 52237350Sphk (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 52337350Sphk m->m_ext.ext_size); 52415689Swollman n->m_ext = m->m_ext; 52515689Swollman n->m_flags |= M_EXT; 52615689Swollman } else { 52715689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 52815689Swollman } 52915689Swollman 53015689Swollman m = m->m_next; 53115689Swollman while (m) { 53215689Swollman MGET(o, how, m->m_type); 53315689Swollman if (!o) 53415689Swollman goto nospace; 53515689Swollman 53615689Swollman n->m_next = o; 53715689Swollman n = n->m_next; 53815689Swollman 53915689Swollman n->m_len = m->m_len; 54015689Swollman if (m->m_flags & M_EXT) { 54115689Swollman n->m_data = m->m_data; 54237350Sphk if(!m->m_ext.ext_ref) 54337350Sphk mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 54437350Sphk else 54537350Sphk (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 54637350Sphk m->m_ext.ext_size); 54715689Swollman n->m_ext = m->m_ext; 54815689Swollman n->m_flags |= M_EXT; 54915689Swollman } else { 55015689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 55115689Swollman } 55215689Swollman 55315689Swollman m = m->m_next; 55415689Swollman } 55515689Swollman return top; 55615689Swollmannospace: 55715689Swollman m_freem(top); 55815689Swollman MCFail++; 55915689Swollman return 0; 56015689Swollman} 56115689Swollman 56215689Swollman/* 5631541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 5641541Srgrimes * continuing for "len" bytes, into the indicated buffer. 5651541Srgrimes */ 5661549Srgrimesvoid 5671541Srgrimesm_copydata(m, off, len, cp) 5681541Srgrimes register struct mbuf *m; 5691541Srgrimes register int off; 5701541Srgrimes register int len; 5711541Srgrimes caddr_t cp; 5721541Srgrimes{ 5731541Srgrimes register unsigned count; 5741541Srgrimes 57552201Salfred KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 57652201Salfred KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 5771541Srgrimes while (off > 0) { 57852201Salfred KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 5791541Srgrimes if (off < m->m_len) 5801541Srgrimes break; 5811541Srgrimes off -= m->m_len; 5821541Srgrimes m = m->m_next; 5831541Srgrimes } 5841541Srgrimes while (len > 0) { 58552201Salfred KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 5861541Srgrimes count = min(m->m_len - off, len); 5871541Srgrimes bcopy(mtod(m, caddr_t) + off, cp, count); 5881541Srgrimes len -= count; 5891541Srgrimes cp += count; 5901541Srgrimes off = 0; 5911541Srgrimes m = m->m_next; 5921541Srgrimes } 5931541Srgrimes} 5941541Srgrimes 5951541Srgrimes/* 5961541Srgrimes * Concatenate mbuf chain n to m. 5971541Srgrimes * Both chains must be of the same type (e.g. MT_DATA). 5981541Srgrimes * Any m_pkthdr is not updated. 5991541Srgrimes */ 6001549Srgrimesvoid 6011541Srgrimesm_cat(m, n) 6021541Srgrimes register struct mbuf *m, *n; 6031541Srgrimes{ 6041541Srgrimes while (m->m_next) 6051541Srgrimes m = m->m_next; 6061541Srgrimes while (n) { 6071541Srgrimes if (m->m_flags & M_EXT || 6081541Srgrimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 6091541Srgrimes /* just join the two chains */ 6101541Srgrimes m->m_next = n; 6111541Srgrimes return; 6121541Srgrimes } 6131541Srgrimes /* splat the data from one into the other */ 6141541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 6151541Srgrimes (u_int)n->m_len); 6161541Srgrimes m->m_len += n->m_len; 6171541Srgrimes n = m_free(n); 6181541Srgrimes } 6191541Srgrimes} 6201541Srgrimes 6211549Srgrimesvoid 6221541Srgrimesm_adj(mp, req_len) 6231541Srgrimes struct mbuf *mp; 6241541Srgrimes int req_len; 6251541Srgrimes{ 6261541Srgrimes register int len = req_len; 6271541Srgrimes register struct mbuf *m; 62833678Sbde register int count; 6291541Srgrimes 6301541Srgrimes if ((m = mp) == NULL) 6311541Srgrimes return; 6321541Srgrimes if (len >= 0) { 6331541Srgrimes /* 6341541Srgrimes * Trim from head. 6351541Srgrimes */ 6361541Srgrimes while (m != NULL && len > 0) { 6371541Srgrimes if (m->m_len <= len) { 6381541Srgrimes len -= m->m_len; 6391541Srgrimes m->m_len = 0; 6401541Srgrimes m = m->m_next; 6411541Srgrimes } else { 6421541Srgrimes m->m_len -= len; 6431541Srgrimes m->m_data += len; 6441541Srgrimes len = 0; 6451541Srgrimes } 6461541Srgrimes } 6471541Srgrimes m = mp; 6481541Srgrimes if (mp->m_flags & M_PKTHDR) 6491541Srgrimes m->m_pkthdr.len -= (req_len - len); 6501541Srgrimes } else { 6511541Srgrimes /* 6521541Srgrimes * Trim from tail. Scan the mbuf chain, 6531541Srgrimes * calculating its length and finding the last mbuf. 6541541Srgrimes * If the adjustment only affects this mbuf, then just 6551541Srgrimes * adjust and return. Otherwise, rescan and truncate 6561541Srgrimes * after the remaining size. 6571541Srgrimes */ 6581541Srgrimes len = -len; 6591541Srgrimes count = 0; 6601541Srgrimes for (;;) { 6611541Srgrimes count += m->m_len; 6621541Srgrimes if (m->m_next == (struct mbuf *)0) 6631541Srgrimes break; 6641541Srgrimes m = m->m_next; 6651541Srgrimes } 6661541Srgrimes if (m->m_len >= len) { 6671541Srgrimes m->m_len -= len; 6681541Srgrimes if (mp->m_flags & M_PKTHDR) 6691541Srgrimes mp->m_pkthdr.len -= len; 6701541Srgrimes return; 6711541Srgrimes } 6721541Srgrimes count -= len; 6731541Srgrimes if (count < 0) 6741541Srgrimes count = 0; 6751541Srgrimes /* 6761541Srgrimes * Correct length for chain is "count". 6771541Srgrimes * Find the mbuf with last data, adjust its length, 6781541Srgrimes * and toss data from remaining mbufs on chain. 6791541Srgrimes */ 6801541Srgrimes m = mp; 6811541Srgrimes if (m->m_flags & M_PKTHDR) 6821541Srgrimes m->m_pkthdr.len = count; 6831541Srgrimes for (; m; m = m->m_next) { 6841541Srgrimes if (m->m_len >= count) { 6851541Srgrimes m->m_len = count; 6861541Srgrimes break; 6871541Srgrimes } 6881541Srgrimes count -= m->m_len; 6891541Srgrimes } 6903308Sphk while (m->m_next) 6913308Sphk (m = m->m_next) ->m_len = 0; 6921541Srgrimes } 6931541Srgrimes} 6941541Srgrimes 6951541Srgrimes/* 6961541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous 6971541Srgrimes * and in the data area of an mbuf (so that mtod and dtom 6981541Srgrimes * will work for a structure of size len). Returns the resulting 6991541Srgrimes * mbuf chain on success, frees it and returns null on failure. 7001541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the 7011541Srgrimes * contiguous region in an attempt to avoid being called next time. 7021541Srgrimes */ 70323081Swollman#define MPFail (mbstat.m_mpfail) 7041541Srgrimes 7051541Srgrimesstruct mbuf * 7061541Srgrimesm_pullup(n, len) 7071541Srgrimes register struct mbuf *n; 7081541Srgrimes int len; 7091541Srgrimes{ 7101541Srgrimes register struct mbuf *m; 7111541Srgrimes register int count; 7121541Srgrimes int space; 7131541Srgrimes 7141541Srgrimes /* 7151541Srgrimes * If first mbuf has no cluster, and has room for len bytes 7161541Srgrimes * without shifting current data, pullup into it, 7171541Srgrimes * otherwise allocate a new mbuf to prepend to the chain. 7181541Srgrimes */ 7191541Srgrimes if ((n->m_flags & M_EXT) == 0 && 7201541Srgrimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 7211541Srgrimes if (n->m_len >= len) 7221541Srgrimes return (n); 7231541Srgrimes m = n; 7241541Srgrimes n = n->m_next; 7251541Srgrimes len -= m->m_len; 7261541Srgrimes } else { 7271541Srgrimes if (len > MHLEN) 7281541Srgrimes goto bad; 7291541Srgrimes MGET(m, M_DONTWAIT, n->m_type); 7301541Srgrimes if (m == 0) 7311541Srgrimes goto bad; 7321541Srgrimes m->m_len = 0; 7331541Srgrimes if (n->m_flags & M_PKTHDR) { 7341541Srgrimes M_COPY_PKTHDR(m, n); 7351541Srgrimes n->m_flags &= ~M_PKTHDR; 7361541Srgrimes } 7371541Srgrimes } 7381541Srgrimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 7391541Srgrimes do { 7401541Srgrimes count = min(min(max(len, max_protohdr), space), n->m_len); 7411541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 7421541Srgrimes (unsigned)count); 7431541Srgrimes len -= count; 7441541Srgrimes m->m_len += count; 7451541Srgrimes n->m_len -= count; 7461541Srgrimes space -= count; 7471541Srgrimes if (n->m_len) 7481541Srgrimes n->m_data += count; 7491541Srgrimes else 7501541Srgrimes n = m_free(n); 7511541Srgrimes } while (len > 0 && n); 7521541Srgrimes if (len > 0) { 7531541Srgrimes (void) m_free(m); 7541541Srgrimes goto bad; 7551541Srgrimes } 7561541Srgrimes m->m_next = n; 7571541Srgrimes return (m); 7581541Srgrimesbad: 7591541Srgrimes m_freem(n); 7601541Srgrimes MPFail++; 7611541Srgrimes return (0); 7621541Srgrimes} 7631541Srgrimes 7641541Srgrimes/* 7651541Srgrimes * Partition an mbuf chain in two pieces, returning the tail -- 7661541Srgrimes * all but the first len0 bytes. In case of failure, it returns NULL and 7671541Srgrimes * attempts to restore the chain to its original state. 7681541Srgrimes */ 7691541Srgrimesstruct mbuf * 7701541Srgrimesm_split(m0, len0, wait) 7711541Srgrimes register struct mbuf *m0; 7721541Srgrimes int len0, wait; 7731541Srgrimes{ 7741541Srgrimes register struct mbuf *m, *n; 7751541Srgrimes unsigned len = len0, remain; 7761541Srgrimes 7771541Srgrimes for (m = m0; m && len > m->m_len; m = m->m_next) 7781541Srgrimes len -= m->m_len; 7791541Srgrimes if (m == 0) 7801541Srgrimes return (0); 7811541Srgrimes remain = m->m_len - len; 7821541Srgrimes if (m0->m_flags & M_PKTHDR) { 7831541Srgrimes MGETHDR(n, wait, m0->m_type); 7841541Srgrimes if (n == 0) 7851541Srgrimes return (0); 7861541Srgrimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 7871541Srgrimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 7881541Srgrimes m0->m_pkthdr.len = len0; 7891541Srgrimes if (m->m_flags & M_EXT) 7901541Srgrimes goto extpacket; 7911541Srgrimes if (remain > MHLEN) { 7921541Srgrimes /* m can't be the lead packet */ 7931541Srgrimes MH_ALIGN(n, 0); 7941541Srgrimes n->m_next = m_split(m, len, wait); 7951541Srgrimes if (n->m_next == 0) { 7961541Srgrimes (void) m_free(n); 7971541Srgrimes return (0); 7981541Srgrimes } else 7991541Srgrimes return (n); 8001541Srgrimes } else 8011541Srgrimes MH_ALIGN(n, remain); 8021541Srgrimes } else if (remain == 0) { 8031541Srgrimes n = m->m_next; 8041541Srgrimes m->m_next = 0; 8051541Srgrimes return (n); 8061541Srgrimes } else { 8071541Srgrimes MGET(n, wait, m->m_type); 8081541Srgrimes if (n == 0) 8091541Srgrimes return (0); 8101541Srgrimes M_ALIGN(n, remain); 8111541Srgrimes } 8121541Srgrimesextpacket: 8131541Srgrimes if (m->m_flags & M_EXT) { 8141541Srgrimes n->m_flags |= M_EXT; 8151541Srgrimes n->m_ext = m->m_ext; 81617663Sjulian if(!m->m_ext.ext_ref) 81717663Sjulian mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 81817663Sjulian else 81917663Sjulian (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 82017663Sjulian m->m_ext.ext_size); 8211541Srgrimes m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */ 8221541Srgrimes n->m_data = m->m_data + len; 8231541Srgrimes } else { 8241541Srgrimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 8251541Srgrimes } 8261541Srgrimes n->m_len = remain; 8271541Srgrimes m->m_len = len; 8281541Srgrimes n->m_next = m->m_next; 8291541Srgrimes m->m_next = 0; 8301541Srgrimes return (n); 8311541Srgrimes} 8321541Srgrimes/* 8331541Srgrimes * Routine to copy from device local memory into mbufs. 8341541Srgrimes */ 8351541Srgrimesstruct mbuf * 8361541Srgrimesm_devget(buf, totlen, off0, ifp, copy) 8371541Srgrimes char *buf; 8381541Srgrimes int totlen, off0; 8391541Srgrimes struct ifnet *ifp; 84012577Sbde void (*copy) __P((char *from, caddr_t to, u_int len)); 8411541Srgrimes{ 8421541Srgrimes register struct mbuf *m; 8431541Srgrimes struct mbuf *top = 0, **mp = ⊤ 8441541Srgrimes register int off = off0, len; 8451541Srgrimes register char *cp; 8461541Srgrimes char *epkt; 8471541Srgrimes 8481541Srgrimes cp = buf; 8491541Srgrimes epkt = cp + totlen; 8501541Srgrimes if (off) { 8511541Srgrimes cp += off + 2 * sizeof(u_short); 8521541Srgrimes totlen -= 2 * sizeof(u_short); 8531541Srgrimes } 8541541Srgrimes MGETHDR(m, M_DONTWAIT, MT_DATA); 8551541Srgrimes if (m == 0) 8561541Srgrimes return (0); 8571541Srgrimes m->m_pkthdr.rcvif = ifp; 8581541Srgrimes m->m_pkthdr.len = totlen; 8591541Srgrimes m->m_len = MHLEN; 8601541Srgrimes 8611541Srgrimes while (totlen > 0) { 8621541Srgrimes if (top) { 8631541Srgrimes MGET(m, M_DONTWAIT, MT_DATA); 8641541Srgrimes if (m == 0) { 8651541Srgrimes m_freem(top); 8661541Srgrimes return (0); 8671541Srgrimes } 8681541Srgrimes m->m_len = MLEN; 8691541Srgrimes } 8701541Srgrimes len = min(totlen, epkt - cp); 8711541Srgrimes if (len >= MINCLSIZE) { 8721541Srgrimes MCLGET(m, M_DONTWAIT); 8731541Srgrimes if (m->m_flags & M_EXT) 8741541Srgrimes m->m_len = len = min(len, MCLBYTES); 8751541Srgrimes else 8761541Srgrimes len = m->m_len; 8771541Srgrimes } else { 8781541Srgrimes /* 8791541Srgrimes * Place initial small packet/header at end of mbuf. 8801541Srgrimes */ 8811541Srgrimes if (len < m->m_len) { 8821541Srgrimes if (top == 0 && len + max_linkhdr <= m->m_len) 8831541Srgrimes m->m_data += max_linkhdr; 8841541Srgrimes m->m_len = len; 8851541Srgrimes } else 8861541Srgrimes len = m->m_len; 8871541Srgrimes } 8881541Srgrimes if (copy) 8891541Srgrimes copy(cp, mtod(m, caddr_t), (unsigned)len); 8901541Srgrimes else 8911541Srgrimes bcopy(cp, mtod(m, caddr_t), (unsigned)len); 8921541Srgrimes cp += len; 8931541Srgrimes *mp = m; 8941541Srgrimes mp = &m->m_next; 8951541Srgrimes totlen -= len; 8961541Srgrimes if (cp == epkt) 8971541Srgrimes cp = buf; 8981541Srgrimes } 8991541Srgrimes return (top); 9001541Srgrimes} 9013352Sphk 9023352Sphk/* 9033352Sphk * Copy data from a buffer back into the indicated mbuf chain, 9043352Sphk * starting "off" bytes from the beginning, extending the mbuf 9053352Sphk * chain if necessary. 9063352Sphk */ 9073352Sphkvoid 9083352Sphkm_copyback(m0, off, len, cp) 9093352Sphk struct mbuf *m0; 9103352Sphk register int off; 9113352Sphk register int len; 9123352Sphk caddr_t cp; 9133352Sphk{ 9143352Sphk register int mlen; 9153352Sphk register struct mbuf *m = m0, *n; 9163352Sphk int totlen = 0; 9173352Sphk 9183352Sphk if (m0 == 0) 9193352Sphk return; 9203352Sphk while (off > (mlen = m->m_len)) { 9213352Sphk off -= mlen; 9223352Sphk totlen += mlen; 9233352Sphk if (m->m_next == 0) { 9243352Sphk n = m_getclr(M_DONTWAIT, m->m_type); 9253352Sphk if (n == 0) 9263352Sphk goto out; 9273352Sphk n->m_len = min(MLEN, len + off); 9283352Sphk m->m_next = n; 9293352Sphk } 9303352Sphk m = m->m_next; 9313352Sphk } 9323352Sphk while (len > 0) { 9333352Sphk mlen = min (m->m_len - off, len); 9343352Sphk bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); 9353352Sphk cp += mlen; 9363352Sphk len -= mlen; 9373352Sphk mlen += off; 9383352Sphk off = 0; 9393352Sphk totlen += mlen; 9403352Sphk if (len == 0) 9413352Sphk break; 9423352Sphk if (m->m_next == 0) { 9433352Sphk n = m_get(M_DONTWAIT, m->m_type); 9443352Sphk if (n == 0) 9453352Sphk break; 9463352Sphk n->m_len = min(MLEN, len); 9473352Sphk m->m_next = n; 9483352Sphk } 9493352Sphk m = m->m_next; 9503352Sphk } 9513352Sphkout: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 9523352Sphk m->m_pkthdr.len = totlen; 9533352Sphk} 954