uipc_mbuf.c revision 37350
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 3437350Sphk * $Id: uipc_mbuf.c,v 1.35 1998/06/05 21:41:48 dg Exp $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes#include <sys/param.h> 381541Srgrimes#include <sys/systm.h> 3932036Sbde#include <sys/malloc.h> 401541Srgrimes#include <sys/mbuf.h> 411541Srgrimes#include <sys/kernel.h> 4223081Swollman#include <sys/sysctl.h> 431541Srgrimes#include <sys/domain.h> 441541Srgrimes#include <sys/protosw.h> 451541Srgrimes 461541Srgrimes#include <vm/vm.h> 479759Sbde#include <vm/vm_kern.h> 4812662Sdg#include <vm/vm_extern.h> 491541Srgrimes 5010653Sdgstatic void mbinit __P((void *)); 5110358SjulianSYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbinit, NULL) 5210358Sjulian 539759Sbdestruct mbuf *mbutl; 541541Srgrimeschar *mclrefcnt; 559759Sbdestruct mbstat mbstat; 5615689Swollmanstruct mbuf *mmbfree; 579759Sbdeunion mcluster *mclfree; 589759Sbdeint max_linkhdr; 599759Sbdeint max_protohdr; 609759Sbdeint max_hdr; 619759Sbdeint max_datalen; 621541Srgrimes 6323081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 6423081Swollman &max_linkhdr, 0, ""); 6523081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 6623081Swollman &max_protohdr, 0, ""); 6723081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 6823081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 6923081Swollman &max_datalen, 0, ""); 7023081SwollmanSYSCTL_STRUCT(_kern_ipc, KIPC_MBSTAT, mbstat, CTLFLAG_RW, &mbstat, mbstat, ""); 7123081Swollman 7212819Sphkstatic void m_reclaim __P((void)); 7312819Sphk 7415736Sphk/* "number of clusters of pages" */ 7515736Sphk#define NCL_INIT 1 7615736Sphk 7715744Sphk#define NMB_INIT 16 7815744Sphk 7910358Sjulian/* ARGSUSED*/ 8010358Sjulianstatic void 8112569Sbdembinit(dummy) 8212569Sbde void *dummy; 831541Srgrimes{ 841541Srgrimes int s; 851541Srgrimes 8615689Swollman mmbfree = NULL; mclfree = NULL; 8723081Swollman mbstat.m_msize = MSIZE; 8823081Swollman mbstat.m_mclbytes = MCLBYTES; 8923081Swollman mbstat.m_minclsize = MINCLSIZE; 9023081Swollman mbstat.m_mlen = MLEN; 9123081Swollman mbstat.m_mhlen = MHLEN; 9223081Swollman 931541Srgrimes s = splimp(); 9415689Swollman if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0) 9515689Swollman goto bad; 9622671Swollman#if MCLBYTES <= PAGE_SIZE 971541Srgrimes if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0) 981541Srgrimes goto bad; 9922671Swollman#else 10022671Swollman /* It's OK to call contigmalloc in this context. */ 10132036Sbde if (m_clalloc(16, M_WAIT) == 0) 10222671Swollman goto bad; 10322671Swollman#endif 1041541Srgrimes splx(s); 1051541Srgrimes return; 1061541Srgrimesbad: 1071541Srgrimes panic("mbinit"); 1081541Srgrimes} 1091541Srgrimes 1101541Srgrimes/* 11115689Swollman * Allocate at least nmb mbufs and place on mbuf free list. 11215689Swollman * Must be called at splimp. 11315689Swollman */ 11415689Swollman/* ARGSUSED */ 11515689Swollmanint 11632036Sbdem_mballoc(nmb, how) 11715689Swollman register int nmb; 11832036Sbde int how; 11915689Swollman{ 12015689Swollman register caddr_t p; 12115689Swollman register int i; 12215689Swollman int nbytes; 12315689Swollman 12415689Swollman /* Once we run out of map space, it will be impossible to get 12515689Swollman * any more (nothing is ever freed back to the map) (XXX which 12615689Swollman * is dumb). (however you are not dead as m_reclaim might 12715689Swollman * still be able to free a substantial amount of space). 12815689Swollman */ 12915689Swollman if (mb_map_full) 13015689Swollman return (0); 13115689Swollman 13215689Swollman nbytes = round_page(nmb * MSIZE); 13322899Swollman p = (caddr_t)kmem_malloc(mb_map, nbytes, M_NOWAIT); 13432036Sbde if (p == 0 && how == M_WAIT) { 13522899Swollman mbstat.m_wait++; 13622899Swollman p = (caddr_t)kmem_malloc(mb_map, nbytes, M_WAITOK); 13722899Swollman } 13822899Swollman 13915689Swollman /* 14032036Sbde * Either the map is now full, or `how' is M_NOWAIT and there 14115689Swollman * are no pages left. 14215689Swollman */ 14315689Swollman if (p == NULL) 14415689Swollman return (0); 14515689Swollman 14615689Swollman nmb = nbytes / MSIZE; 14715689Swollman for (i = 0; i < nmb; i++) { 14815689Swollman ((struct mbuf *)p)->m_next = mmbfree; 14915689Swollman mmbfree = (struct mbuf *)p; 15015689Swollman p += MSIZE; 15115689Swollman } 15215689Swollman mbstat.m_mbufs += nmb; 15315689Swollman return (1); 15415689Swollman} 15515689Swollman 15622671Swollman#if MCLBYTES > PAGE_SIZE 15722899Swollmanstatic int i_want_my_mcl; 15822671Swollman 15922899Swollmanstatic void 16022671Swollmankproc_mclalloc(void) 16122671Swollman{ 16222671Swollman int status; 16322671Swollman 16422671Swollman while (1) { 16522671Swollman tsleep(&i_want_my_mcl, PVM, "mclalloc", 0); 16622671Swollman 16722671Swollman for (; i_want_my_mcl; i_want_my_mcl--) { 16832036Sbde if (m_clalloc(1, M_WAIT) == 0) 16922671Swollman printf("m_clalloc failed even in process context!\n"); 17022671Swollman } 17122671Swollman } 17222671Swollman} 17322671Swollman 17422671Swollmanstatic struct proc *mclallocproc; 17522671Swollmanstatic struct kproc_desc mclalloc_kp = { 17622671Swollman "mclalloc", 17722671Swollman kproc_mclalloc, 17822671Swollman &mclallocproc 17922671Swollman}; 18022671SwollmanSYSINIT_KT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start, 18122671Swollman &mclalloc_kp); 18222671Swollman#endif 18322671Swollman 18415689Swollman/* 1851541Srgrimes * Allocate some number of mbuf clusters 1861541Srgrimes * and place on cluster free list. 1871541Srgrimes * Must be called at splimp. 1881541Srgrimes */ 1891541Srgrimes/* ARGSUSED */ 1901549Srgrimesint 19132036Sbdem_clalloc(ncl, how) 1921541Srgrimes register int ncl; 19332036Sbde int how; 1941541Srgrimes{ 1951541Srgrimes register caddr_t p; 1961541Srgrimes register int i; 1971541Srgrimes int npg; 1981541Srgrimes 1997066Sdg /* 2007066Sdg * Once we run out of map space, it will be impossible 2017066Sdg * to get any more (nothing is ever freed back to the 2027066Sdg * map). 2037066Sdg */ 20422899Swollman if (mb_map_full) { 20522899Swollman mbstat.m_drops++; 2067066Sdg return (0); 20722899Swollman } 2087066Sdg 20922671Swollman#if MCLBYTES > PAGE_SIZE 21032036Sbde if (how != M_WAIT) { 21122671Swollman i_want_my_mcl += ncl; 21222671Swollman wakeup(&i_want_my_mcl); 21322899Swollman mbstat.m_wait++; 21422671Swollman p = 0; 21522671Swollman } else { 21622671Swollman p = contigmalloc1(MCLBYTES * ncl, M_DEVBUF, M_WAITOK, 0ul, 21722671Swollman ~0ul, PAGE_SIZE, 0, mb_map); 21822671Swollman } 21922671Swollman#else 22015543Sphk npg = ncl; 22121737Sdg p = (caddr_t)kmem_malloc(mb_map, ctob(npg), 22232036Sbde how != M_WAIT ? M_NOWAIT : M_WAITOK); 22322671Swollman ncl = ncl * PAGE_SIZE / MCLBYTES; 22422671Swollman#endif 2257066Sdg /* 22632036Sbde * Either the map is now full, or `how' is M_NOWAIT and there 2277066Sdg * are no pages left. 2287066Sdg */ 22922899Swollman if (p == NULL) { 23022899Swollman mbstat.m_drops++; 2311541Srgrimes return (0); 23222899Swollman } 2337066Sdg 2341541Srgrimes for (i = 0; i < ncl; i++) { 2351541Srgrimes ((union mcluster *)p)->mcl_next = mclfree; 2361541Srgrimes mclfree = (union mcluster *)p; 2371541Srgrimes p += MCLBYTES; 2381541Srgrimes mbstat.m_clfree++; 2391541Srgrimes } 2401541Srgrimes mbstat.m_clusters += ncl; 2411541Srgrimes return (1); 2421541Srgrimes} 2431541Srgrimes 2441541Srgrimes/* 2451541Srgrimes * When MGET failes, ask protocols to free space when short of memory, 2461541Srgrimes * then re-attempt to allocate an mbuf. 2471541Srgrimes */ 2481541Srgrimesstruct mbuf * 2491541Srgrimesm_retry(i, t) 2501541Srgrimes int i, t; 2511541Srgrimes{ 2521541Srgrimes register struct mbuf *m; 2531541Srgrimes 2541541Srgrimes m_reclaim(); 2551541Srgrimes#define m_retry(i, t) (struct mbuf *)0 2561541Srgrimes MGET(m, i, t); 2571541Srgrimes#undef m_retry 25836675Sdg if (m != NULL) { 2596669Sdg mbstat.m_wait++; 26036675Sdg } else { 26136675Sdg if (i == M_DONTWAIT) 26236675Sdg mbstat.m_drops++; 26336675Sdg else 26436675Sdg panic("Out of mbuf clusters"); 26536675Sdg } 2661541Srgrimes return (m); 2671541Srgrimes} 2681541Srgrimes 2691541Srgrimes/* 2701541Srgrimes * As above; retry an MGETHDR. 2711541Srgrimes */ 2721541Srgrimesstruct mbuf * 2731541Srgrimesm_retryhdr(i, t) 2741541Srgrimes int i, t; 2751541Srgrimes{ 2761541Srgrimes register struct mbuf *m; 2771541Srgrimes 2781541Srgrimes m_reclaim(); 2791541Srgrimes#define m_retryhdr(i, t) (struct mbuf *)0 2801541Srgrimes MGETHDR(m, i, t); 2811541Srgrimes#undef m_retryhdr 28236675Sdg if (m != NULL) { 2836669Sdg mbstat.m_wait++; 28436675Sdg } else { 28536675Sdg if (i == M_DONTWAIT) 28636675Sdg mbstat.m_drops++; 28736675Sdg else 28836675Sdg panic("Out of mbuf clusters"); 28936675Sdg } 2901541Srgrimes return (m); 2911541Srgrimes} 2921541Srgrimes 29312819Sphkstatic void 2941541Srgrimesm_reclaim() 2951541Srgrimes{ 2961541Srgrimes register struct domain *dp; 2971541Srgrimes register struct protosw *pr; 2981541Srgrimes int s = splimp(); 2991541Srgrimes 3001541Srgrimes for (dp = domains; dp; dp = dp->dom_next) 3011541Srgrimes for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 3021541Srgrimes if (pr->pr_drain) 3031541Srgrimes (*pr->pr_drain)(); 3041541Srgrimes splx(s); 3051541Srgrimes mbstat.m_drain++; 3061541Srgrimes} 3071541Srgrimes 3081541Srgrimes/* 3091541Srgrimes * Space allocation routines. 3101541Srgrimes * These are also available as macros 3111541Srgrimes * for critical paths. 3121541Srgrimes */ 3131541Srgrimesstruct mbuf * 31432036Sbdem_get(how, type) 31532036Sbde int how, type; 3161541Srgrimes{ 3171541Srgrimes register struct mbuf *m; 3181541Srgrimes 31932036Sbde MGET(m, how, type); 3201541Srgrimes return (m); 3211541Srgrimes} 3221541Srgrimes 3231541Srgrimesstruct mbuf * 32432036Sbdem_gethdr(how, type) 32532036Sbde int how, type; 3261541Srgrimes{ 3271541Srgrimes register struct mbuf *m; 3281541Srgrimes 32932036Sbde MGETHDR(m, how, type); 3301541Srgrimes return (m); 3311541Srgrimes} 3321541Srgrimes 3331541Srgrimesstruct mbuf * 33432036Sbdem_getclr(how, type) 33532036Sbde int how, type; 3361541Srgrimes{ 3371541Srgrimes register struct mbuf *m; 3381541Srgrimes 33932036Sbde MGET(m, how, type); 3401541Srgrimes if (m == 0) 3411541Srgrimes return (0); 3421541Srgrimes bzero(mtod(m, caddr_t), MLEN); 3431541Srgrimes return (m); 3441541Srgrimes} 3451541Srgrimes 3461541Srgrimesstruct mbuf * 3471541Srgrimesm_free(m) 3481541Srgrimes struct mbuf *m; 3491541Srgrimes{ 3501541Srgrimes register struct mbuf *n; 3511541Srgrimes 3521541Srgrimes MFREE(m, n); 3531541Srgrimes return (n); 3541541Srgrimes} 3551541Srgrimes 3561541Srgrimesvoid 3571541Srgrimesm_freem(m) 3581541Srgrimes register struct mbuf *m; 3591541Srgrimes{ 3601541Srgrimes register struct mbuf *n; 3611541Srgrimes 3621541Srgrimes if (m == NULL) 3631541Srgrimes return; 3641541Srgrimes do { 3651541Srgrimes MFREE(m, n); 3663308Sphk m = n; 3673308Sphk } while (m); 3681541Srgrimes} 3691541Srgrimes 3701541Srgrimes/* 3711541Srgrimes * Mbuffer utility routines. 3721541Srgrimes */ 3731541Srgrimes 3741541Srgrimes/* 3751541Srgrimes * Lesser-used path for M_PREPEND: 3761541Srgrimes * allocate new mbuf to prepend to chain, 3771541Srgrimes * copy junk along. 3781541Srgrimes */ 3791541Srgrimesstruct mbuf * 3801541Srgrimesm_prepend(m, len, how) 3811541Srgrimes register struct mbuf *m; 3821541Srgrimes int len, how; 3831541Srgrimes{ 3841541Srgrimes struct mbuf *mn; 3851541Srgrimes 3861541Srgrimes MGET(mn, how, m->m_type); 3871541Srgrimes if (mn == (struct mbuf *)NULL) { 3881541Srgrimes m_freem(m); 3891541Srgrimes return ((struct mbuf *)NULL); 3901541Srgrimes } 3911541Srgrimes if (m->m_flags & M_PKTHDR) { 3921541Srgrimes M_COPY_PKTHDR(mn, m); 3931541Srgrimes m->m_flags &= ~M_PKTHDR; 3941541Srgrimes } 3951541Srgrimes mn->m_next = m; 3961541Srgrimes m = mn; 3971541Srgrimes if (len < MHLEN) 3981541Srgrimes MH_ALIGN(m, len); 3991541Srgrimes m->m_len = len; 4001541Srgrimes return (m); 4011541Srgrimes} 4021541Srgrimes 4031541Srgrimes/* 4041541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 4051541Srgrimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 4061541Srgrimes * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller. 4071541Srgrimes */ 40823081Swollman#define MCFail (mbstat.m_mcfail) 4091541Srgrimes 4101541Srgrimesstruct mbuf * 4111541Srgrimesm_copym(m, off0, len, wait) 4121541Srgrimes register struct mbuf *m; 4131541Srgrimes int off0, wait; 4141541Srgrimes register int len; 4151541Srgrimes{ 4161541Srgrimes register struct mbuf *n, **np; 4171541Srgrimes register int off = off0; 4181541Srgrimes struct mbuf *top; 4191541Srgrimes int copyhdr = 0; 4201541Srgrimes 4211541Srgrimes if (off < 0 || len < 0) 4221541Srgrimes panic("m_copym"); 4231541Srgrimes if (off == 0 && m->m_flags & M_PKTHDR) 4241541Srgrimes copyhdr = 1; 4251541Srgrimes while (off > 0) { 4261541Srgrimes if (m == 0) 4271541Srgrimes panic("m_copym"); 4281541Srgrimes if (off < m->m_len) 4291541Srgrimes break; 4301541Srgrimes off -= m->m_len; 4311541Srgrimes m = m->m_next; 4321541Srgrimes } 4331541Srgrimes np = ⊤ 4341541Srgrimes top = 0; 4351541Srgrimes while (len > 0) { 4361541Srgrimes if (m == 0) { 4371541Srgrimes if (len != M_COPYALL) 4381541Srgrimes panic("m_copym"); 4391541Srgrimes break; 4401541Srgrimes } 4411541Srgrimes MGET(n, wait, m->m_type); 4421541Srgrimes *np = n; 4431541Srgrimes if (n == 0) 4441541Srgrimes goto nospace; 4451541Srgrimes if (copyhdr) { 4461541Srgrimes M_COPY_PKTHDR(n, m); 4471541Srgrimes if (len == M_COPYALL) 4481541Srgrimes n->m_pkthdr.len -= off0; 4491541Srgrimes else 4501541Srgrimes n->m_pkthdr.len = len; 4511541Srgrimes copyhdr = 0; 4521541Srgrimes } 4531541Srgrimes n->m_len = min(len, m->m_len - off); 4541541Srgrimes if (m->m_flags & M_EXT) { 4551541Srgrimes n->m_data = m->m_data + off; 45617663Sjulian if(!m->m_ext.ext_ref) 45717663Sjulian mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 45817663Sjulian else 45917663Sjulian (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 46017663Sjulian m->m_ext.ext_size); 4611541Srgrimes n->m_ext = m->m_ext; 4621541Srgrimes n->m_flags |= M_EXT; 4631541Srgrimes } else 4641541Srgrimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 4651541Srgrimes (unsigned)n->m_len); 4661541Srgrimes if (len != M_COPYALL) 4671541Srgrimes len -= n->m_len; 4681541Srgrimes off = 0; 4691541Srgrimes m = m->m_next; 4701541Srgrimes np = &n->m_next; 4711541Srgrimes } 4721541Srgrimes if (top == 0) 4731541Srgrimes MCFail++; 4741541Srgrimes return (top); 4751541Srgrimesnospace: 4761541Srgrimes m_freem(top); 4771541Srgrimes MCFail++; 4781541Srgrimes return (0); 4791541Srgrimes} 4801541Srgrimes 4811541Srgrimes/* 48215689Swollman * Copy an entire packet, including header (which must be present). 48315689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 48415689Swollman */ 48515689Swollmanstruct mbuf * 48615689Swollmanm_copypacket(m, how) 48715689Swollman struct mbuf *m; 48815689Swollman int how; 48915689Swollman{ 49015689Swollman struct mbuf *top, *n, *o; 49115689Swollman 49215689Swollman MGET(n, how, m->m_type); 49315689Swollman top = n; 49415689Swollman if (!n) 49515689Swollman goto nospace; 49615689Swollman 49715689Swollman M_COPY_PKTHDR(n, m); 49815689Swollman n->m_len = m->m_len; 49915689Swollman if (m->m_flags & M_EXT) { 50015689Swollman n->m_data = m->m_data; 50137350Sphk if(!m->m_ext.ext_ref) 50237350Sphk mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 50337350Sphk else 50437350Sphk (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 50537350Sphk m->m_ext.ext_size); 50615689Swollman n->m_ext = m->m_ext; 50715689Swollman n->m_flags |= M_EXT; 50815689Swollman } else { 50915689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 51015689Swollman } 51115689Swollman 51215689Swollman m = m->m_next; 51315689Swollman while (m) { 51415689Swollman MGET(o, how, m->m_type); 51515689Swollman if (!o) 51615689Swollman goto nospace; 51715689Swollman 51815689Swollman n->m_next = o; 51915689Swollman n = n->m_next; 52015689Swollman 52115689Swollman n->m_len = m->m_len; 52215689Swollman if (m->m_flags & M_EXT) { 52315689Swollman n->m_data = m->m_data; 52437350Sphk if(!m->m_ext.ext_ref) 52537350Sphk mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 52637350Sphk else 52737350Sphk (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 52837350Sphk m->m_ext.ext_size); 52915689Swollman n->m_ext = m->m_ext; 53015689Swollman n->m_flags |= M_EXT; 53115689Swollman } else { 53215689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 53315689Swollman } 53415689Swollman 53515689Swollman m = m->m_next; 53615689Swollman } 53715689Swollman return top; 53815689Swollmannospace: 53915689Swollman m_freem(top); 54015689Swollman MCFail++; 54115689Swollman return 0; 54215689Swollman} 54315689Swollman 54415689Swollman/* 5451541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 5461541Srgrimes * continuing for "len" bytes, into the indicated buffer. 5471541Srgrimes */ 5481549Srgrimesvoid 5491541Srgrimesm_copydata(m, off, len, cp) 5501541Srgrimes register struct mbuf *m; 5511541Srgrimes register int off; 5521541Srgrimes register int len; 5531541Srgrimes caddr_t cp; 5541541Srgrimes{ 5551541Srgrimes register unsigned count; 5561541Srgrimes 5571541Srgrimes if (off < 0 || len < 0) 5581541Srgrimes panic("m_copydata"); 5591541Srgrimes while (off > 0) { 5601541Srgrimes if (m == 0) 5611541Srgrimes panic("m_copydata"); 5621541Srgrimes if (off < m->m_len) 5631541Srgrimes break; 5641541Srgrimes off -= m->m_len; 5651541Srgrimes m = m->m_next; 5661541Srgrimes } 5671541Srgrimes while (len > 0) { 5681541Srgrimes if (m == 0) 5691541Srgrimes panic("m_copydata"); 5701541Srgrimes count = min(m->m_len - off, len); 5711541Srgrimes bcopy(mtod(m, caddr_t) + off, cp, count); 5721541Srgrimes len -= count; 5731541Srgrimes cp += count; 5741541Srgrimes off = 0; 5751541Srgrimes m = m->m_next; 5761541Srgrimes } 5771541Srgrimes} 5781541Srgrimes 5791541Srgrimes/* 5801541Srgrimes * Concatenate mbuf chain n to m. 5811541Srgrimes * Both chains must be of the same type (e.g. MT_DATA). 5821541Srgrimes * Any m_pkthdr is not updated. 5831541Srgrimes */ 5841549Srgrimesvoid 5851541Srgrimesm_cat(m, n) 5861541Srgrimes register struct mbuf *m, *n; 5871541Srgrimes{ 5881541Srgrimes while (m->m_next) 5891541Srgrimes m = m->m_next; 5901541Srgrimes while (n) { 5911541Srgrimes if (m->m_flags & M_EXT || 5921541Srgrimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 5931541Srgrimes /* just join the two chains */ 5941541Srgrimes m->m_next = n; 5951541Srgrimes return; 5961541Srgrimes } 5971541Srgrimes /* splat the data from one into the other */ 5981541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 5991541Srgrimes (u_int)n->m_len); 6001541Srgrimes m->m_len += n->m_len; 6011541Srgrimes n = m_free(n); 6021541Srgrimes } 6031541Srgrimes} 6041541Srgrimes 6051549Srgrimesvoid 6061541Srgrimesm_adj(mp, req_len) 6071541Srgrimes struct mbuf *mp; 6081541Srgrimes int req_len; 6091541Srgrimes{ 6101541Srgrimes register int len = req_len; 6111541Srgrimes register struct mbuf *m; 61233678Sbde register int count; 6131541Srgrimes 6141541Srgrimes if ((m = mp) == NULL) 6151541Srgrimes return; 6161541Srgrimes if (len >= 0) { 6171541Srgrimes /* 6181541Srgrimes * Trim from head. 6191541Srgrimes */ 6201541Srgrimes while (m != NULL && len > 0) { 6211541Srgrimes if (m->m_len <= len) { 6221541Srgrimes len -= m->m_len; 6231541Srgrimes m->m_len = 0; 6241541Srgrimes m = m->m_next; 6251541Srgrimes } else { 6261541Srgrimes m->m_len -= len; 6271541Srgrimes m->m_data += len; 6281541Srgrimes len = 0; 6291541Srgrimes } 6301541Srgrimes } 6311541Srgrimes m = mp; 6321541Srgrimes if (mp->m_flags & M_PKTHDR) 6331541Srgrimes m->m_pkthdr.len -= (req_len - len); 6341541Srgrimes } else { 6351541Srgrimes /* 6361541Srgrimes * Trim from tail. Scan the mbuf chain, 6371541Srgrimes * calculating its length and finding the last mbuf. 6381541Srgrimes * If the adjustment only affects this mbuf, then just 6391541Srgrimes * adjust and return. Otherwise, rescan and truncate 6401541Srgrimes * after the remaining size. 6411541Srgrimes */ 6421541Srgrimes len = -len; 6431541Srgrimes count = 0; 6441541Srgrimes for (;;) { 6451541Srgrimes count += m->m_len; 6461541Srgrimes if (m->m_next == (struct mbuf *)0) 6471541Srgrimes break; 6481541Srgrimes m = m->m_next; 6491541Srgrimes } 6501541Srgrimes if (m->m_len >= len) { 6511541Srgrimes m->m_len -= len; 6521541Srgrimes if (mp->m_flags & M_PKTHDR) 6531541Srgrimes mp->m_pkthdr.len -= len; 6541541Srgrimes return; 6551541Srgrimes } 6561541Srgrimes count -= len; 6571541Srgrimes if (count < 0) 6581541Srgrimes count = 0; 6591541Srgrimes /* 6601541Srgrimes * Correct length for chain is "count". 6611541Srgrimes * Find the mbuf with last data, adjust its length, 6621541Srgrimes * and toss data from remaining mbufs on chain. 6631541Srgrimes */ 6641541Srgrimes m = mp; 6651541Srgrimes if (m->m_flags & M_PKTHDR) 6661541Srgrimes m->m_pkthdr.len = count; 6671541Srgrimes for (; m; m = m->m_next) { 6681541Srgrimes if (m->m_len >= count) { 6691541Srgrimes m->m_len = count; 6701541Srgrimes break; 6711541Srgrimes } 6721541Srgrimes count -= m->m_len; 6731541Srgrimes } 6743308Sphk while (m->m_next) 6753308Sphk (m = m->m_next) ->m_len = 0; 6761541Srgrimes } 6771541Srgrimes} 6781541Srgrimes 6791541Srgrimes/* 6801541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous 6811541Srgrimes * and in the data area of an mbuf (so that mtod and dtom 6821541Srgrimes * will work for a structure of size len). Returns the resulting 6831541Srgrimes * mbuf chain on success, frees it and returns null on failure. 6841541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the 6851541Srgrimes * contiguous region in an attempt to avoid being called next time. 6861541Srgrimes */ 68723081Swollman#define MPFail (mbstat.m_mpfail) 6881541Srgrimes 6891541Srgrimesstruct mbuf * 6901541Srgrimesm_pullup(n, len) 6911541Srgrimes register struct mbuf *n; 6921541Srgrimes int len; 6931541Srgrimes{ 6941541Srgrimes register struct mbuf *m; 6951541Srgrimes register int count; 6961541Srgrimes int space; 6971541Srgrimes 6981541Srgrimes /* 6991541Srgrimes * If first mbuf has no cluster, and has room for len bytes 7001541Srgrimes * without shifting current data, pullup into it, 7011541Srgrimes * otherwise allocate a new mbuf to prepend to the chain. 7021541Srgrimes */ 7031541Srgrimes if ((n->m_flags & M_EXT) == 0 && 7041541Srgrimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 7051541Srgrimes if (n->m_len >= len) 7061541Srgrimes return (n); 7071541Srgrimes m = n; 7081541Srgrimes n = n->m_next; 7091541Srgrimes len -= m->m_len; 7101541Srgrimes } else { 7111541Srgrimes if (len > MHLEN) 7121541Srgrimes goto bad; 7131541Srgrimes MGET(m, M_DONTWAIT, n->m_type); 7141541Srgrimes if (m == 0) 7151541Srgrimes goto bad; 7161541Srgrimes m->m_len = 0; 7171541Srgrimes if (n->m_flags & M_PKTHDR) { 7181541Srgrimes M_COPY_PKTHDR(m, n); 7191541Srgrimes n->m_flags &= ~M_PKTHDR; 7201541Srgrimes } 7211541Srgrimes } 7221541Srgrimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 7231541Srgrimes do { 7241541Srgrimes count = min(min(max(len, max_protohdr), space), n->m_len); 7251541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 7261541Srgrimes (unsigned)count); 7271541Srgrimes len -= count; 7281541Srgrimes m->m_len += count; 7291541Srgrimes n->m_len -= count; 7301541Srgrimes space -= count; 7311541Srgrimes if (n->m_len) 7321541Srgrimes n->m_data += count; 7331541Srgrimes else 7341541Srgrimes n = m_free(n); 7351541Srgrimes } while (len > 0 && n); 7361541Srgrimes if (len > 0) { 7371541Srgrimes (void) m_free(m); 7381541Srgrimes goto bad; 7391541Srgrimes } 7401541Srgrimes m->m_next = n; 7411541Srgrimes return (m); 7421541Srgrimesbad: 7431541Srgrimes m_freem(n); 7441541Srgrimes MPFail++; 7451541Srgrimes return (0); 7461541Srgrimes} 7471541Srgrimes 7481541Srgrimes/* 7491541Srgrimes * Partition an mbuf chain in two pieces, returning the tail -- 7501541Srgrimes * all but the first len0 bytes. In case of failure, it returns NULL and 7511541Srgrimes * attempts to restore the chain to its original state. 7521541Srgrimes */ 7531541Srgrimesstruct mbuf * 7541541Srgrimesm_split(m0, len0, wait) 7551541Srgrimes register struct mbuf *m0; 7561541Srgrimes int len0, wait; 7571541Srgrimes{ 7581541Srgrimes register struct mbuf *m, *n; 7591541Srgrimes unsigned len = len0, remain; 7601541Srgrimes 7611541Srgrimes for (m = m0; m && len > m->m_len; m = m->m_next) 7621541Srgrimes len -= m->m_len; 7631541Srgrimes if (m == 0) 7641541Srgrimes return (0); 7651541Srgrimes remain = m->m_len - len; 7661541Srgrimes if (m0->m_flags & M_PKTHDR) { 7671541Srgrimes MGETHDR(n, wait, m0->m_type); 7681541Srgrimes if (n == 0) 7691541Srgrimes return (0); 7701541Srgrimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 7711541Srgrimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 7721541Srgrimes m0->m_pkthdr.len = len0; 7731541Srgrimes if (m->m_flags & M_EXT) 7741541Srgrimes goto extpacket; 7751541Srgrimes if (remain > MHLEN) { 7761541Srgrimes /* m can't be the lead packet */ 7771541Srgrimes MH_ALIGN(n, 0); 7781541Srgrimes n->m_next = m_split(m, len, wait); 7791541Srgrimes if (n->m_next == 0) { 7801541Srgrimes (void) m_free(n); 7811541Srgrimes return (0); 7821541Srgrimes } else 7831541Srgrimes return (n); 7841541Srgrimes } else 7851541Srgrimes MH_ALIGN(n, remain); 7861541Srgrimes } else if (remain == 0) { 7871541Srgrimes n = m->m_next; 7881541Srgrimes m->m_next = 0; 7891541Srgrimes return (n); 7901541Srgrimes } else { 7911541Srgrimes MGET(n, wait, m->m_type); 7921541Srgrimes if (n == 0) 7931541Srgrimes return (0); 7941541Srgrimes M_ALIGN(n, remain); 7951541Srgrimes } 7961541Srgrimesextpacket: 7971541Srgrimes if (m->m_flags & M_EXT) { 7981541Srgrimes n->m_flags |= M_EXT; 7991541Srgrimes n->m_ext = m->m_ext; 80017663Sjulian if(!m->m_ext.ext_ref) 80117663Sjulian mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 80217663Sjulian else 80317663Sjulian (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 80417663Sjulian m->m_ext.ext_size); 8051541Srgrimes m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */ 8061541Srgrimes n->m_data = m->m_data + len; 8071541Srgrimes } else { 8081541Srgrimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 8091541Srgrimes } 8101541Srgrimes n->m_len = remain; 8111541Srgrimes m->m_len = len; 8121541Srgrimes n->m_next = m->m_next; 8131541Srgrimes m->m_next = 0; 8141541Srgrimes return (n); 8151541Srgrimes} 8161541Srgrimes/* 8171541Srgrimes * Routine to copy from device local memory into mbufs. 8181541Srgrimes */ 8191541Srgrimesstruct mbuf * 8201541Srgrimesm_devget(buf, totlen, off0, ifp, copy) 8211541Srgrimes char *buf; 8221541Srgrimes int totlen, off0; 8231541Srgrimes struct ifnet *ifp; 82412577Sbde void (*copy) __P((char *from, caddr_t to, u_int len)); 8251541Srgrimes{ 8261541Srgrimes register struct mbuf *m; 8271541Srgrimes struct mbuf *top = 0, **mp = ⊤ 8281541Srgrimes register int off = off0, len; 8291541Srgrimes register char *cp; 8301541Srgrimes char *epkt; 8311541Srgrimes 8321541Srgrimes cp = buf; 8331541Srgrimes epkt = cp + totlen; 8341541Srgrimes if (off) { 8351541Srgrimes cp += off + 2 * sizeof(u_short); 8361541Srgrimes totlen -= 2 * sizeof(u_short); 8371541Srgrimes } 8381541Srgrimes MGETHDR(m, M_DONTWAIT, MT_DATA); 8391541Srgrimes if (m == 0) 8401541Srgrimes return (0); 8411541Srgrimes m->m_pkthdr.rcvif = ifp; 8421541Srgrimes m->m_pkthdr.len = totlen; 8431541Srgrimes m->m_len = MHLEN; 8441541Srgrimes 8451541Srgrimes while (totlen > 0) { 8461541Srgrimes if (top) { 8471541Srgrimes MGET(m, M_DONTWAIT, MT_DATA); 8481541Srgrimes if (m == 0) { 8491541Srgrimes m_freem(top); 8501541Srgrimes return (0); 8511541Srgrimes } 8521541Srgrimes m->m_len = MLEN; 8531541Srgrimes } 8541541Srgrimes len = min(totlen, epkt - cp); 8551541Srgrimes if (len >= MINCLSIZE) { 8561541Srgrimes MCLGET(m, M_DONTWAIT); 8571541Srgrimes if (m->m_flags & M_EXT) 8581541Srgrimes m->m_len = len = min(len, MCLBYTES); 8591541Srgrimes else 8601541Srgrimes len = m->m_len; 8611541Srgrimes } else { 8621541Srgrimes /* 8631541Srgrimes * Place initial small packet/header at end of mbuf. 8641541Srgrimes */ 8651541Srgrimes if (len < m->m_len) { 8661541Srgrimes if (top == 0 && len + max_linkhdr <= m->m_len) 8671541Srgrimes m->m_data += max_linkhdr; 8681541Srgrimes m->m_len = len; 8691541Srgrimes } else 8701541Srgrimes len = m->m_len; 8711541Srgrimes } 8721541Srgrimes if (copy) 8731541Srgrimes copy(cp, mtod(m, caddr_t), (unsigned)len); 8741541Srgrimes else 8751541Srgrimes bcopy(cp, mtod(m, caddr_t), (unsigned)len); 8761541Srgrimes cp += len; 8771541Srgrimes *mp = m; 8781541Srgrimes mp = &m->m_next; 8791541Srgrimes totlen -= len; 8801541Srgrimes if (cp == epkt) 8811541Srgrimes cp = buf; 8821541Srgrimes } 8831541Srgrimes return (top); 8841541Srgrimes} 8853352Sphk 8863352Sphk/* 8873352Sphk * Copy data from a buffer back into the indicated mbuf chain, 8883352Sphk * starting "off" bytes from the beginning, extending the mbuf 8893352Sphk * chain if necessary. 8903352Sphk */ 8913352Sphkvoid 8923352Sphkm_copyback(m0, off, len, cp) 8933352Sphk struct mbuf *m0; 8943352Sphk register int off; 8953352Sphk register int len; 8963352Sphk caddr_t cp; 8973352Sphk{ 8983352Sphk register int mlen; 8993352Sphk register struct mbuf *m = m0, *n; 9003352Sphk int totlen = 0; 9013352Sphk 9023352Sphk if (m0 == 0) 9033352Sphk return; 9043352Sphk while (off > (mlen = m->m_len)) { 9053352Sphk off -= mlen; 9063352Sphk totlen += mlen; 9073352Sphk if (m->m_next == 0) { 9083352Sphk n = m_getclr(M_DONTWAIT, m->m_type); 9093352Sphk if (n == 0) 9103352Sphk goto out; 9113352Sphk n->m_len = min(MLEN, len + off); 9123352Sphk m->m_next = n; 9133352Sphk } 9143352Sphk m = m->m_next; 9153352Sphk } 9163352Sphk while (len > 0) { 9173352Sphk mlen = min (m->m_len - off, len); 9183352Sphk bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); 9193352Sphk cp += mlen; 9203352Sphk len -= mlen; 9213352Sphk mlen += off; 9223352Sphk off = 0; 9233352Sphk totlen += mlen; 9243352Sphk if (len == 0) 9253352Sphk break; 9263352Sphk if (m->m_next == 0) { 9273352Sphk n = m_get(M_DONTWAIT, m->m_type); 9283352Sphk if (n == 0) 9293352Sphk break; 9303352Sphk n->m_len = min(MLEN, len); 9313352Sphk m->m_next = n; 9323352Sphk } 9333352Sphk m = m->m_next; 9343352Sphk } 9353352Sphkout: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 9363352Sphk m->m_pkthdr.len = totlen; 9373352Sphk} 938