uipc_mbuf.c revision 48391
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 3448391Speter * $Id: uipc_mbuf.c,v 1.39 1999/04/12 10:07:15 des 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 6344078SdfrSYSCTL_DECL(_kern_ipc); 6423081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 6523081Swollman &max_linkhdr, 0, ""); 6623081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 6723081Swollman &max_protohdr, 0, ""); 6823081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 6923081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 7023081Swollman &max_datalen, 0, ""); 7123081SwollmanSYSCTL_STRUCT(_kern_ipc, KIPC_MBSTAT, mbstat, CTLFLAG_RW, &mbstat, mbstat, ""); 7223081Swollman 7312819Sphkstatic void m_reclaim __P((void)); 7412819Sphk 7515736Sphk/* "number of clusters of pages" */ 7615736Sphk#define NCL_INIT 1 7715736Sphk 7815744Sphk#define NMB_INIT 16 7915744Sphk 8010358Sjulian/* ARGSUSED*/ 8110358Sjulianstatic void 8212569Sbdembinit(dummy) 8312569Sbde void *dummy; 841541Srgrimes{ 851541Srgrimes int s; 861541Srgrimes 8715689Swollman mmbfree = NULL; mclfree = NULL; 8823081Swollman mbstat.m_msize = MSIZE; 8923081Swollman mbstat.m_mclbytes = MCLBYTES; 9023081Swollman mbstat.m_minclsize = MINCLSIZE; 9123081Swollman mbstat.m_mlen = MLEN; 9223081Swollman mbstat.m_mhlen = MHLEN; 9323081Swollman 941541Srgrimes s = splimp(); 9515689Swollman if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0) 9615689Swollman goto bad; 9722671Swollman#if MCLBYTES <= PAGE_SIZE 981541Srgrimes if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0) 991541Srgrimes goto bad; 10022671Swollman#else 10122671Swollman /* It's OK to call contigmalloc in this context. */ 10232036Sbde if (m_clalloc(16, M_WAIT) == 0) 10322671Swollman goto bad; 10422671Swollman#endif 1051541Srgrimes splx(s); 1061541Srgrimes return; 1071541Srgrimesbad: 1081541Srgrimes panic("mbinit"); 1091541Srgrimes} 1101541Srgrimes 1111541Srgrimes/* 11215689Swollman * Allocate at least nmb mbufs and place on mbuf free list. 11315689Swollman * Must be called at splimp. 11415689Swollman */ 11515689Swollman/* ARGSUSED */ 11615689Swollmanint 11732036Sbdem_mballoc(nmb, how) 11815689Swollman register int nmb; 11932036Sbde int how; 12015689Swollman{ 12115689Swollman register caddr_t p; 12215689Swollman register int i; 12315689Swollman int nbytes; 12415689Swollman 12515689Swollman /* Once we run out of map space, it will be impossible to get 12615689Swollman * any more (nothing is ever freed back to the map) (XXX which 12715689Swollman * is dumb). (however you are not dead as m_reclaim might 12815689Swollman * still be able to free a substantial amount of space). 12915689Swollman */ 13015689Swollman if (mb_map_full) 13115689Swollman return (0); 13215689Swollman 13315689Swollman nbytes = round_page(nmb * MSIZE); 13422899Swollman p = (caddr_t)kmem_malloc(mb_map, nbytes, M_NOWAIT); 13532036Sbde if (p == 0 && how == M_WAIT) { 13622899Swollman mbstat.m_wait++; 13722899Swollman p = (caddr_t)kmem_malloc(mb_map, nbytes, M_WAITOK); 13822899Swollman } 13922899Swollman 14015689Swollman /* 14132036Sbde * Either the map is now full, or `how' is M_NOWAIT and there 14215689Swollman * are no pages left. 14315689Swollman */ 14415689Swollman if (p == NULL) 14515689Swollman return (0); 14615689Swollman 14715689Swollman nmb = nbytes / MSIZE; 14815689Swollman for (i = 0; i < nmb; i++) { 14915689Swollman ((struct mbuf *)p)->m_next = mmbfree; 15015689Swollman mmbfree = (struct mbuf *)p; 15115689Swollman p += MSIZE; 15215689Swollman } 15315689Swollman mbstat.m_mbufs += nmb; 15415689Swollman return (1); 15515689Swollman} 15615689Swollman 15722671Swollman#if MCLBYTES > PAGE_SIZE 15822899Swollmanstatic int i_want_my_mcl; 15922671Swollman 16022899Swollmanstatic void 16122671Swollmankproc_mclalloc(void) 16222671Swollman{ 16322671Swollman int status; 16422671Swollman 16522671Swollman while (1) { 16622671Swollman tsleep(&i_want_my_mcl, PVM, "mclalloc", 0); 16722671Swollman 16822671Swollman for (; i_want_my_mcl; i_want_my_mcl--) { 16932036Sbde if (m_clalloc(1, M_WAIT) == 0) 17022671Swollman printf("m_clalloc failed even in process context!\n"); 17122671Swollman } 17222671Swollman } 17322671Swollman} 17422671Swollman 17522671Swollmanstatic struct proc *mclallocproc; 17622671Swollmanstatic struct kproc_desc mclalloc_kp = { 17722671Swollman "mclalloc", 17822671Swollman kproc_mclalloc, 17922671Swollman &mclallocproc 18022671Swollman}; 18148391SpeterSYSINIT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start, 18222671Swollman &mclalloc_kp); 18322671Swollman#endif 18422671Swollman 18515689Swollman/* 1861541Srgrimes * Allocate some number of mbuf clusters 1871541Srgrimes * and place on cluster free list. 1881541Srgrimes * Must be called at splimp. 1891541Srgrimes */ 1901541Srgrimes/* ARGSUSED */ 1911549Srgrimesint 19232036Sbdem_clalloc(ncl, how) 1931541Srgrimes register int ncl; 19432036Sbde int how; 1951541Srgrimes{ 1961541Srgrimes register caddr_t p; 1971541Srgrimes register int i; 1981541Srgrimes int npg; 1991541Srgrimes 2007066Sdg /* 2017066Sdg * Once we run out of map space, it will be impossible 2027066Sdg * to get any more (nothing is ever freed back to the 2037066Sdg * map). 2047066Sdg */ 20522899Swollman if (mb_map_full) { 20622899Swollman mbstat.m_drops++; 2077066Sdg return (0); 20822899Swollman } 2097066Sdg 21022671Swollman#if MCLBYTES > PAGE_SIZE 21132036Sbde if (how != M_WAIT) { 21222671Swollman i_want_my_mcl += ncl; 21322671Swollman wakeup(&i_want_my_mcl); 21422899Swollman mbstat.m_wait++; 21522671Swollman p = 0; 21622671Swollman } else { 21722671Swollman p = contigmalloc1(MCLBYTES * ncl, M_DEVBUF, M_WAITOK, 0ul, 21822671Swollman ~0ul, PAGE_SIZE, 0, mb_map); 21922671Swollman } 22022671Swollman#else 22115543Sphk npg = ncl; 22221737Sdg p = (caddr_t)kmem_malloc(mb_map, ctob(npg), 22332036Sbde how != M_WAIT ? M_NOWAIT : M_WAITOK); 22422671Swollman ncl = ncl * PAGE_SIZE / MCLBYTES; 22522671Swollman#endif 2267066Sdg /* 22732036Sbde * Either the map is now full, or `how' is M_NOWAIT and there 2287066Sdg * are no pages left. 2297066Sdg */ 23022899Swollman if (p == NULL) { 23122899Swollman mbstat.m_drops++; 2321541Srgrimes return (0); 23322899Swollman } 2347066Sdg 2351541Srgrimes for (i = 0; i < ncl; i++) { 2361541Srgrimes ((union mcluster *)p)->mcl_next = mclfree; 2371541Srgrimes mclfree = (union mcluster *)p; 2381541Srgrimes p += MCLBYTES; 2391541Srgrimes mbstat.m_clfree++; 2401541Srgrimes } 2411541Srgrimes mbstat.m_clusters += ncl; 2421541Srgrimes return (1); 2431541Srgrimes} 2441541Srgrimes 2451541Srgrimes/* 24645615Sdes * When MGET fails, ask protocols to free space when short of memory, 2471541Srgrimes * then re-attempt to allocate an mbuf. 2481541Srgrimes */ 2491541Srgrimesstruct mbuf * 2501541Srgrimesm_retry(i, t) 2511541Srgrimes int i, t; 2521541Srgrimes{ 2531541Srgrimes register struct mbuf *m; 2541541Srgrimes 25537878Sdg /* 25637878Sdg * Must only do the reclaim if not in an interrupt context. 25737878Sdg */ 25837878Sdg if (i == M_WAIT) 25937878Sdg m_reclaim(); 2601541Srgrimes#define m_retry(i, t) (struct mbuf *)0 2611541Srgrimes MGET(m, i, t); 2621541Srgrimes#undef m_retry 26336675Sdg if (m != NULL) { 2646669Sdg mbstat.m_wait++; 26536675Sdg } else { 26636675Sdg if (i == M_DONTWAIT) 26736675Sdg mbstat.m_drops++; 26836675Sdg else 26936675Sdg panic("Out of mbuf clusters"); 27036675Sdg } 2711541Srgrimes return (m); 2721541Srgrimes} 2731541Srgrimes 2741541Srgrimes/* 2751541Srgrimes * As above; retry an MGETHDR. 2761541Srgrimes */ 2771541Srgrimesstruct mbuf * 2781541Srgrimesm_retryhdr(i, t) 2791541Srgrimes int i, t; 2801541Srgrimes{ 2811541Srgrimes register struct mbuf *m; 2821541Srgrimes 28337878Sdg /* 28437878Sdg * Must only do the reclaim if not in an interrupt context. 28537878Sdg */ 28637878Sdg if (i == M_WAIT) 28737878Sdg m_reclaim(); 2881541Srgrimes#define m_retryhdr(i, t) (struct mbuf *)0 2891541Srgrimes MGETHDR(m, i, t); 2901541Srgrimes#undef m_retryhdr 29136675Sdg if (m != NULL) { 2926669Sdg mbstat.m_wait++; 29336675Sdg } else { 29436675Sdg if (i == M_DONTWAIT) 29536675Sdg mbstat.m_drops++; 29636675Sdg else 29736675Sdg panic("Out of mbuf clusters"); 29836675Sdg } 2991541Srgrimes return (m); 3001541Srgrimes} 3011541Srgrimes 30212819Sphkstatic void 3031541Srgrimesm_reclaim() 3041541Srgrimes{ 3051541Srgrimes register struct domain *dp; 3061541Srgrimes register struct protosw *pr; 3071541Srgrimes int s = splimp(); 3081541Srgrimes 3091541Srgrimes for (dp = domains; dp; dp = dp->dom_next) 3101541Srgrimes for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 3111541Srgrimes if (pr->pr_drain) 3121541Srgrimes (*pr->pr_drain)(); 3131541Srgrimes splx(s); 3141541Srgrimes mbstat.m_drain++; 3151541Srgrimes} 3161541Srgrimes 3171541Srgrimes/* 3181541Srgrimes * Space allocation routines. 3191541Srgrimes * These are also available as macros 3201541Srgrimes * for critical paths. 3211541Srgrimes */ 3221541Srgrimesstruct mbuf * 32332036Sbdem_get(how, type) 32432036Sbde int how, type; 3251541Srgrimes{ 3261541Srgrimes register struct mbuf *m; 3271541Srgrimes 32832036Sbde MGET(m, how, type); 3291541Srgrimes return (m); 3301541Srgrimes} 3311541Srgrimes 3321541Srgrimesstruct mbuf * 33332036Sbdem_gethdr(how, type) 33432036Sbde int how, type; 3351541Srgrimes{ 3361541Srgrimes register struct mbuf *m; 3371541Srgrimes 33832036Sbde MGETHDR(m, how, type); 3391541Srgrimes return (m); 3401541Srgrimes} 3411541Srgrimes 3421541Srgrimesstruct mbuf * 34332036Sbdem_getclr(how, type) 34432036Sbde int how, type; 3451541Srgrimes{ 3461541Srgrimes register struct mbuf *m; 3471541Srgrimes 34832036Sbde MGET(m, how, type); 3491541Srgrimes if (m == 0) 3501541Srgrimes return (0); 3511541Srgrimes bzero(mtod(m, caddr_t), MLEN); 3521541Srgrimes return (m); 3531541Srgrimes} 3541541Srgrimes 3551541Srgrimesstruct mbuf * 3561541Srgrimesm_free(m) 3571541Srgrimes struct mbuf *m; 3581541Srgrimes{ 3591541Srgrimes register struct mbuf *n; 3601541Srgrimes 3611541Srgrimes MFREE(m, n); 3621541Srgrimes return (n); 3631541Srgrimes} 3641541Srgrimes 3651541Srgrimesvoid 3661541Srgrimesm_freem(m) 3671541Srgrimes register struct mbuf *m; 3681541Srgrimes{ 3691541Srgrimes register struct mbuf *n; 3701541Srgrimes 3711541Srgrimes if (m == NULL) 3721541Srgrimes return; 3731541Srgrimes do { 3741541Srgrimes MFREE(m, n); 3753308Sphk m = n; 3763308Sphk } while (m); 3771541Srgrimes} 3781541Srgrimes 3791541Srgrimes/* 3801541Srgrimes * Mbuffer utility routines. 3811541Srgrimes */ 3821541Srgrimes 3831541Srgrimes/* 3841541Srgrimes * Lesser-used path for M_PREPEND: 3851541Srgrimes * allocate new mbuf to prepend to chain, 3861541Srgrimes * copy junk along. 3871541Srgrimes */ 3881541Srgrimesstruct mbuf * 3891541Srgrimesm_prepend(m, len, how) 3901541Srgrimes register struct mbuf *m; 3911541Srgrimes int len, how; 3921541Srgrimes{ 3931541Srgrimes struct mbuf *mn; 3941541Srgrimes 3951541Srgrimes MGET(mn, how, m->m_type); 3961541Srgrimes if (mn == (struct mbuf *)NULL) { 3971541Srgrimes m_freem(m); 3981541Srgrimes return ((struct mbuf *)NULL); 3991541Srgrimes } 4001541Srgrimes if (m->m_flags & M_PKTHDR) { 4011541Srgrimes M_COPY_PKTHDR(mn, m); 4021541Srgrimes m->m_flags &= ~M_PKTHDR; 4031541Srgrimes } 4041541Srgrimes mn->m_next = m; 4051541Srgrimes m = mn; 4061541Srgrimes if (len < MHLEN) 4071541Srgrimes MH_ALIGN(m, len); 4081541Srgrimes m->m_len = len; 4091541Srgrimes return (m); 4101541Srgrimes} 4111541Srgrimes 4121541Srgrimes/* 4131541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 4141541Srgrimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 4151541Srgrimes * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller. 4161541Srgrimes */ 41723081Swollman#define MCFail (mbstat.m_mcfail) 4181541Srgrimes 4191541Srgrimesstruct mbuf * 4201541Srgrimesm_copym(m, off0, len, wait) 4211541Srgrimes register struct mbuf *m; 4221541Srgrimes int off0, wait; 4231541Srgrimes register int len; 4241541Srgrimes{ 4251541Srgrimes register struct mbuf *n, **np; 4261541Srgrimes register int off = off0; 4271541Srgrimes struct mbuf *top; 4281541Srgrimes int copyhdr = 0; 4291541Srgrimes 4301541Srgrimes if (off < 0 || len < 0) 4311541Srgrimes panic("m_copym"); 4321541Srgrimes if (off == 0 && m->m_flags & M_PKTHDR) 4331541Srgrimes copyhdr = 1; 4341541Srgrimes while (off > 0) { 4351541Srgrimes if (m == 0) 4361541Srgrimes panic("m_copym"); 4371541Srgrimes if (off < m->m_len) 4381541Srgrimes break; 4391541Srgrimes off -= m->m_len; 4401541Srgrimes m = m->m_next; 4411541Srgrimes } 4421541Srgrimes np = ⊤ 4431541Srgrimes top = 0; 4441541Srgrimes while (len > 0) { 4451541Srgrimes if (m == 0) { 4461541Srgrimes if (len != M_COPYALL) 4471541Srgrimes panic("m_copym"); 4481541Srgrimes break; 4491541Srgrimes } 4501541Srgrimes MGET(n, wait, m->m_type); 4511541Srgrimes *np = n; 4521541Srgrimes if (n == 0) 4531541Srgrimes goto nospace; 4541541Srgrimes if (copyhdr) { 4551541Srgrimes M_COPY_PKTHDR(n, m); 4561541Srgrimes if (len == M_COPYALL) 4571541Srgrimes n->m_pkthdr.len -= off0; 4581541Srgrimes else 4591541Srgrimes n->m_pkthdr.len = len; 4601541Srgrimes copyhdr = 0; 4611541Srgrimes } 4621541Srgrimes n->m_len = min(len, m->m_len - off); 4631541Srgrimes if (m->m_flags & M_EXT) { 4641541Srgrimes n->m_data = m->m_data + off; 46517663Sjulian if(!m->m_ext.ext_ref) 46617663Sjulian mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 46717663Sjulian else 46817663Sjulian (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 46917663Sjulian m->m_ext.ext_size); 4701541Srgrimes n->m_ext = m->m_ext; 4711541Srgrimes n->m_flags |= M_EXT; 4721541Srgrimes } else 4731541Srgrimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 4741541Srgrimes (unsigned)n->m_len); 4751541Srgrimes if (len != M_COPYALL) 4761541Srgrimes len -= n->m_len; 4771541Srgrimes off = 0; 4781541Srgrimes m = m->m_next; 4791541Srgrimes np = &n->m_next; 4801541Srgrimes } 4811541Srgrimes if (top == 0) 4821541Srgrimes MCFail++; 4831541Srgrimes return (top); 4841541Srgrimesnospace: 4851541Srgrimes m_freem(top); 4861541Srgrimes MCFail++; 4871541Srgrimes return (0); 4881541Srgrimes} 4891541Srgrimes 4901541Srgrimes/* 49115689Swollman * Copy an entire packet, including header (which must be present). 49215689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 49315689Swollman */ 49415689Swollmanstruct mbuf * 49515689Swollmanm_copypacket(m, how) 49615689Swollman struct mbuf *m; 49715689Swollman int how; 49815689Swollman{ 49915689Swollman struct mbuf *top, *n, *o; 50015689Swollman 50115689Swollman MGET(n, how, m->m_type); 50215689Swollman top = n; 50315689Swollman if (!n) 50415689Swollman goto nospace; 50515689Swollman 50615689Swollman M_COPY_PKTHDR(n, m); 50715689Swollman n->m_len = m->m_len; 50815689Swollman if (m->m_flags & M_EXT) { 50915689Swollman n->m_data = m->m_data; 51037350Sphk if(!m->m_ext.ext_ref) 51137350Sphk mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 51237350Sphk else 51337350Sphk (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 51437350Sphk m->m_ext.ext_size); 51515689Swollman n->m_ext = m->m_ext; 51615689Swollman n->m_flags |= M_EXT; 51715689Swollman } else { 51815689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 51915689Swollman } 52015689Swollman 52115689Swollman m = m->m_next; 52215689Swollman while (m) { 52315689Swollman MGET(o, how, m->m_type); 52415689Swollman if (!o) 52515689Swollman goto nospace; 52615689Swollman 52715689Swollman n->m_next = o; 52815689Swollman n = n->m_next; 52915689Swollman 53015689Swollman n->m_len = m->m_len; 53115689Swollman if (m->m_flags & M_EXT) { 53215689Swollman n->m_data = m->m_data; 53337350Sphk if(!m->m_ext.ext_ref) 53437350Sphk mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 53537350Sphk else 53637350Sphk (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 53737350Sphk m->m_ext.ext_size); 53815689Swollman n->m_ext = m->m_ext; 53915689Swollman n->m_flags |= M_EXT; 54015689Swollman } else { 54115689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 54215689Swollman } 54315689Swollman 54415689Swollman m = m->m_next; 54515689Swollman } 54615689Swollman return top; 54715689Swollmannospace: 54815689Swollman m_freem(top); 54915689Swollman MCFail++; 55015689Swollman return 0; 55115689Swollman} 55215689Swollman 55315689Swollman/* 5541541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 5551541Srgrimes * continuing for "len" bytes, into the indicated buffer. 5561541Srgrimes */ 5571549Srgrimesvoid 5581541Srgrimesm_copydata(m, off, len, cp) 5591541Srgrimes register struct mbuf *m; 5601541Srgrimes register int off; 5611541Srgrimes register int len; 5621541Srgrimes caddr_t cp; 5631541Srgrimes{ 5641541Srgrimes register unsigned count; 5651541Srgrimes 5661541Srgrimes if (off < 0 || len < 0) 5671541Srgrimes panic("m_copydata"); 5681541Srgrimes while (off > 0) { 5691541Srgrimes if (m == 0) 5701541Srgrimes panic("m_copydata"); 5711541Srgrimes if (off < m->m_len) 5721541Srgrimes break; 5731541Srgrimes off -= m->m_len; 5741541Srgrimes m = m->m_next; 5751541Srgrimes } 5761541Srgrimes while (len > 0) { 5771541Srgrimes if (m == 0) 5781541Srgrimes panic("m_copydata"); 5791541Srgrimes count = min(m->m_len - off, len); 5801541Srgrimes bcopy(mtod(m, caddr_t) + off, cp, count); 5811541Srgrimes len -= count; 5821541Srgrimes cp += count; 5831541Srgrimes off = 0; 5841541Srgrimes m = m->m_next; 5851541Srgrimes } 5861541Srgrimes} 5871541Srgrimes 5881541Srgrimes/* 5891541Srgrimes * Concatenate mbuf chain n to m. 5901541Srgrimes * Both chains must be of the same type (e.g. MT_DATA). 5911541Srgrimes * Any m_pkthdr is not updated. 5921541Srgrimes */ 5931549Srgrimesvoid 5941541Srgrimesm_cat(m, n) 5951541Srgrimes register struct mbuf *m, *n; 5961541Srgrimes{ 5971541Srgrimes while (m->m_next) 5981541Srgrimes m = m->m_next; 5991541Srgrimes while (n) { 6001541Srgrimes if (m->m_flags & M_EXT || 6011541Srgrimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 6021541Srgrimes /* just join the two chains */ 6031541Srgrimes m->m_next = n; 6041541Srgrimes return; 6051541Srgrimes } 6061541Srgrimes /* splat the data from one into the other */ 6071541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 6081541Srgrimes (u_int)n->m_len); 6091541Srgrimes m->m_len += n->m_len; 6101541Srgrimes n = m_free(n); 6111541Srgrimes } 6121541Srgrimes} 6131541Srgrimes 6141549Srgrimesvoid 6151541Srgrimesm_adj(mp, req_len) 6161541Srgrimes struct mbuf *mp; 6171541Srgrimes int req_len; 6181541Srgrimes{ 6191541Srgrimes register int len = req_len; 6201541Srgrimes register struct mbuf *m; 62133678Sbde register int count; 6221541Srgrimes 6231541Srgrimes if ((m = mp) == NULL) 6241541Srgrimes return; 6251541Srgrimes if (len >= 0) { 6261541Srgrimes /* 6271541Srgrimes * Trim from head. 6281541Srgrimes */ 6291541Srgrimes while (m != NULL && len > 0) { 6301541Srgrimes if (m->m_len <= len) { 6311541Srgrimes len -= m->m_len; 6321541Srgrimes m->m_len = 0; 6331541Srgrimes m = m->m_next; 6341541Srgrimes } else { 6351541Srgrimes m->m_len -= len; 6361541Srgrimes m->m_data += len; 6371541Srgrimes len = 0; 6381541Srgrimes } 6391541Srgrimes } 6401541Srgrimes m = mp; 6411541Srgrimes if (mp->m_flags & M_PKTHDR) 6421541Srgrimes m->m_pkthdr.len -= (req_len - len); 6431541Srgrimes } else { 6441541Srgrimes /* 6451541Srgrimes * Trim from tail. Scan the mbuf chain, 6461541Srgrimes * calculating its length and finding the last mbuf. 6471541Srgrimes * If the adjustment only affects this mbuf, then just 6481541Srgrimes * adjust and return. Otherwise, rescan and truncate 6491541Srgrimes * after the remaining size. 6501541Srgrimes */ 6511541Srgrimes len = -len; 6521541Srgrimes count = 0; 6531541Srgrimes for (;;) { 6541541Srgrimes count += m->m_len; 6551541Srgrimes if (m->m_next == (struct mbuf *)0) 6561541Srgrimes break; 6571541Srgrimes m = m->m_next; 6581541Srgrimes } 6591541Srgrimes if (m->m_len >= len) { 6601541Srgrimes m->m_len -= len; 6611541Srgrimes if (mp->m_flags & M_PKTHDR) 6621541Srgrimes mp->m_pkthdr.len -= len; 6631541Srgrimes return; 6641541Srgrimes } 6651541Srgrimes count -= len; 6661541Srgrimes if (count < 0) 6671541Srgrimes count = 0; 6681541Srgrimes /* 6691541Srgrimes * Correct length for chain is "count". 6701541Srgrimes * Find the mbuf with last data, adjust its length, 6711541Srgrimes * and toss data from remaining mbufs on chain. 6721541Srgrimes */ 6731541Srgrimes m = mp; 6741541Srgrimes if (m->m_flags & M_PKTHDR) 6751541Srgrimes m->m_pkthdr.len = count; 6761541Srgrimes for (; m; m = m->m_next) { 6771541Srgrimes if (m->m_len >= count) { 6781541Srgrimes m->m_len = count; 6791541Srgrimes break; 6801541Srgrimes } 6811541Srgrimes count -= m->m_len; 6821541Srgrimes } 6833308Sphk while (m->m_next) 6843308Sphk (m = m->m_next) ->m_len = 0; 6851541Srgrimes } 6861541Srgrimes} 6871541Srgrimes 6881541Srgrimes/* 6891541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous 6901541Srgrimes * and in the data area of an mbuf (so that mtod and dtom 6911541Srgrimes * will work for a structure of size len). Returns the resulting 6921541Srgrimes * mbuf chain on success, frees it and returns null on failure. 6931541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the 6941541Srgrimes * contiguous region in an attempt to avoid being called next time. 6951541Srgrimes */ 69623081Swollman#define MPFail (mbstat.m_mpfail) 6971541Srgrimes 6981541Srgrimesstruct mbuf * 6991541Srgrimesm_pullup(n, len) 7001541Srgrimes register struct mbuf *n; 7011541Srgrimes int len; 7021541Srgrimes{ 7031541Srgrimes register struct mbuf *m; 7041541Srgrimes register int count; 7051541Srgrimes int space; 7061541Srgrimes 7071541Srgrimes /* 7081541Srgrimes * If first mbuf has no cluster, and has room for len bytes 7091541Srgrimes * without shifting current data, pullup into it, 7101541Srgrimes * otherwise allocate a new mbuf to prepend to the chain. 7111541Srgrimes */ 7121541Srgrimes if ((n->m_flags & M_EXT) == 0 && 7131541Srgrimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 7141541Srgrimes if (n->m_len >= len) 7151541Srgrimes return (n); 7161541Srgrimes m = n; 7171541Srgrimes n = n->m_next; 7181541Srgrimes len -= m->m_len; 7191541Srgrimes } else { 7201541Srgrimes if (len > MHLEN) 7211541Srgrimes goto bad; 7221541Srgrimes MGET(m, M_DONTWAIT, n->m_type); 7231541Srgrimes if (m == 0) 7241541Srgrimes goto bad; 7251541Srgrimes m->m_len = 0; 7261541Srgrimes if (n->m_flags & M_PKTHDR) { 7271541Srgrimes M_COPY_PKTHDR(m, n); 7281541Srgrimes n->m_flags &= ~M_PKTHDR; 7291541Srgrimes } 7301541Srgrimes } 7311541Srgrimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 7321541Srgrimes do { 7331541Srgrimes count = min(min(max(len, max_protohdr), space), n->m_len); 7341541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 7351541Srgrimes (unsigned)count); 7361541Srgrimes len -= count; 7371541Srgrimes m->m_len += count; 7381541Srgrimes n->m_len -= count; 7391541Srgrimes space -= count; 7401541Srgrimes if (n->m_len) 7411541Srgrimes n->m_data += count; 7421541Srgrimes else 7431541Srgrimes n = m_free(n); 7441541Srgrimes } while (len > 0 && n); 7451541Srgrimes if (len > 0) { 7461541Srgrimes (void) m_free(m); 7471541Srgrimes goto bad; 7481541Srgrimes } 7491541Srgrimes m->m_next = n; 7501541Srgrimes return (m); 7511541Srgrimesbad: 7521541Srgrimes m_freem(n); 7531541Srgrimes MPFail++; 7541541Srgrimes return (0); 7551541Srgrimes} 7561541Srgrimes 7571541Srgrimes/* 7581541Srgrimes * Partition an mbuf chain in two pieces, returning the tail -- 7591541Srgrimes * all but the first len0 bytes. In case of failure, it returns NULL and 7601541Srgrimes * attempts to restore the chain to its original state. 7611541Srgrimes */ 7621541Srgrimesstruct mbuf * 7631541Srgrimesm_split(m0, len0, wait) 7641541Srgrimes register struct mbuf *m0; 7651541Srgrimes int len0, wait; 7661541Srgrimes{ 7671541Srgrimes register struct mbuf *m, *n; 7681541Srgrimes unsigned len = len0, remain; 7691541Srgrimes 7701541Srgrimes for (m = m0; m && len > m->m_len; m = m->m_next) 7711541Srgrimes len -= m->m_len; 7721541Srgrimes if (m == 0) 7731541Srgrimes return (0); 7741541Srgrimes remain = m->m_len - len; 7751541Srgrimes if (m0->m_flags & M_PKTHDR) { 7761541Srgrimes MGETHDR(n, wait, m0->m_type); 7771541Srgrimes if (n == 0) 7781541Srgrimes return (0); 7791541Srgrimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 7801541Srgrimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 7811541Srgrimes m0->m_pkthdr.len = len0; 7821541Srgrimes if (m->m_flags & M_EXT) 7831541Srgrimes goto extpacket; 7841541Srgrimes if (remain > MHLEN) { 7851541Srgrimes /* m can't be the lead packet */ 7861541Srgrimes MH_ALIGN(n, 0); 7871541Srgrimes n->m_next = m_split(m, len, wait); 7881541Srgrimes if (n->m_next == 0) { 7891541Srgrimes (void) m_free(n); 7901541Srgrimes return (0); 7911541Srgrimes } else 7921541Srgrimes return (n); 7931541Srgrimes } else 7941541Srgrimes MH_ALIGN(n, remain); 7951541Srgrimes } else if (remain == 0) { 7961541Srgrimes n = m->m_next; 7971541Srgrimes m->m_next = 0; 7981541Srgrimes return (n); 7991541Srgrimes } else { 8001541Srgrimes MGET(n, wait, m->m_type); 8011541Srgrimes if (n == 0) 8021541Srgrimes return (0); 8031541Srgrimes M_ALIGN(n, remain); 8041541Srgrimes } 8051541Srgrimesextpacket: 8061541Srgrimes if (m->m_flags & M_EXT) { 8071541Srgrimes n->m_flags |= M_EXT; 8081541Srgrimes n->m_ext = m->m_ext; 80917663Sjulian if(!m->m_ext.ext_ref) 81017663Sjulian mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 81117663Sjulian else 81217663Sjulian (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 81317663Sjulian m->m_ext.ext_size); 8141541Srgrimes m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */ 8151541Srgrimes n->m_data = m->m_data + len; 8161541Srgrimes } else { 8171541Srgrimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 8181541Srgrimes } 8191541Srgrimes n->m_len = remain; 8201541Srgrimes m->m_len = len; 8211541Srgrimes n->m_next = m->m_next; 8221541Srgrimes m->m_next = 0; 8231541Srgrimes return (n); 8241541Srgrimes} 8251541Srgrimes/* 8261541Srgrimes * Routine to copy from device local memory into mbufs. 8271541Srgrimes */ 8281541Srgrimesstruct mbuf * 8291541Srgrimesm_devget(buf, totlen, off0, ifp, copy) 8301541Srgrimes char *buf; 8311541Srgrimes int totlen, off0; 8321541Srgrimes struct ifnet *ifp; 83312577Sbde void (*copy) __P((char *from, caddr_t to, u_int len)); 8341541Srgrimes{ 8351541Srgrimes register struct mbuf *m; 8361541Srgrimes struct mbuf *top = 0, **mp = ⊤ 8371541Srgrimes register int off = off0, len; 8381541Srgrimes register char *cp; 8391541Srgrimes char *epkt; 8401541Srgrimes 8411541Srgrimes cp = buf; 8421541Srgrimes epkt = cp + totlen; 8431541Srgrimes if (off) { 8441541Srgrimes cp += off + 2 * sizeof(u_short); 8451541Srgrimes totlen -= 2 * sizeof(u_short); 8461541Srgrimes } 8471541Srgrimes MGETHDR(m, M_DONTWAIT, MT_DATA); 8481541Srgrimes if (m == 0) 8491541Srgrimes return (0); 8501541Srgrimes m->m_pkthdr.rcvif = ifp; 8511541Srgrimes m->m_pkthdr.len = totlen; 8521541Srgrimes m->m_len = MHLEN; 8531541Srgrimes 8541541Srgrimes while (totlen > 0) { 8551541Srgrimes if (top) { 8561541Srgrimes MGET(m, M_DONTWAIT, MT_DATA); 8571541Srgrimes if (m == 0) { 8581541Srgrimes m_freem(top); 8591541Srgrimes return (0); 8601541Srgrimes } 8611541Srgrimes m->m_len = MLEN; 8621541Srgrimes } 8631541Srgrimes len = min(totlen, epkt - cp); 8641541Srgrimes if (len >= MINCLSIZE) { 8651541Srgrimes MCLGET(m, M_DONTWAIT); 8661541Srgrimes if (m->m_flags & M_EXT) 8671541Srgrimes m->m_len = len = min(len, MCLBYTES); 8681541Srgrimes else 8691541Srgrimes len = m->m_len; 8701541Srgrimes } else { 8711541Srgrimes /* 8721541Srgrimes * Place initial small packet/header at end of mbuf. 8731541Srgrimes */ 8741541Srgrimes if (len < m->m_len) { 8751541Srgrimes if (top == 0 && len + max_linkhdr <= m->m_len) 8761541Srgrimes m->m_data += max_linkhdr; 8771541Srgrimes m->m_len = len; 8781541Srgrimes } else 8791541Srgrimes len = m->m_len; 8801541Srgrimes } 8811541Srgrimes if (copy) 8821541Srgrimes copy(cp, mtod(m, caddr_t), (unsigned)len); 8831541Srgrimes else 8841541Srgrimes bcopy(cp, mtod(m, caddr_t), (unsigned)len); 8851541Srgrimes cp += len; 8861541Srgrimes *mp = m; 8871541Srgrimes mp = &m->m_next; 8881541Srgrimes totlen -= len; 8891541Srgrimes if (cp == epkt) 8901541Srgrimes cp = buf; 8911541Srgrimes } 8921541Srgrimes return (top); 8931541Srgrimes} 8943352Sphk 8953352Sphk/* 8963352Sphk * Copy data from a buffer back into the indicated mbuf chain, 8973352Sphk * starting "off" bytes from the beginning, extending the mbuf 8983352Sphk * chain if necessary. 8993352Sphk */ 9003352Sphkvoid 9013352Sphkm_copyback(m0, off, len, cp) 9023352Sphk struct mbuf *m0; 9033352Sphk register int off; 9043352Sphk register int len; 9053352Sphk caddr_t cp; 9063352Sphk{ 9073352Sphk register int mlen; 9083352Sphk register struct mbuf *m = m0, *n; 9093352Sphk int totlen = 0; 9103352Sphk 9113352Sphk if (m0 == 0) 9123352Sphk return; 9133352Sphk while (off > (mlen = m->m_len)) { 9143352Sphk off -= mlen; 9153352Sphk totlen += mlen; 9163352Sphk if (m->m_next == 0) { 9173352Sphk n = m_getclr(M_DONTWAIT, m->m_type); 9183352Sphk if (n == 0) 9193352Sphk goto out; 9203352Sphk n->m_len = min(MLEN, len + off); 9213352Sphk m->m_next = n; 9223352Sphk } 9233352Sphk m = m->m_next; 9243352Sphk } 9253352Sphk while (len > 0) { 9263352Sphk mlen = min (m->m_len - off, len); 9273352Sphk bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); 9283352Sphk cp += mlen; 9293352Sphk len -= mlen; 9303352Sphk mlen += off; 9313352Sphk off = 0; 9323352Sphk totlen += mlen; 9333352Sphk if (len == 0) 9343352Sphk break; 9353352Sphk if (m->m_next == 0) { 9363352Sphk n = m_get(M_DONTWAIT, m->m_type); 9373352Sphk if (n == 0) 9383352Sphk break; 9393352Sphk n->m_len = min(MLEN, len); 9403352Sphk m->m_next = n; 9413352Sphk } 9423352Sphk m = m->m_next; 9433352Sphk } 9443352Sphkout: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 9453352Sphk m->m_pkthdr.len = totlen; 9463352Sphk} 947