uipc_mbuf.c revision 17663
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 3417663Sjulian * $Id: uipc_mbuf.c,v 1.23 1996/05/12 07:48:47 phk Exp $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes#include <sys/param.h> 381541Srgrimes#include <sys/systm.h> 391541Srgrimes#include <sys/proc.h> 401541Srgrimes#include <sys/malloc.h> 411541Srgrimes#define MBTYPES 421541Srgrimes#include <sys/mbuf.h> 431541Srgrimes#include <sys/kernel.h> 441541Srgrimes#include <sys/syslog.h> 451541Srgrimes#include <sys/domain.h> 461541Srgrimes#include <sys/protosw.h> 471541Srgrimes 481541Srgrimes#include <vm/vm.h> 4912662Sdg#include <vm/vm_param.h> 509759Sbde#include <vm/vm_kern.h> 5112662Sdg#include <vm/vm_extern.h> 521541Srgrimes 5310653Sdgstatic void mbinit __P((void *)); 5410358SjulianSYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbinit, NULL) 5510358Sjulian 569759Sbdestruct mbuf *mbutl; 571541Srgrimeschar *mclrefcnt; 589759Sbdestruct mbstat mbstat; 5915689Swollmanstruct mbuf *mmbfree; 609759Sbdeunion mcluster *mclfree; 619759Sbdeint max_linkhdr; 629759Sbdeint max_protohdr; 639759Sbdeint max_hdr; 649759Sbdeint max_datalen; 651541Srgrimes 6612819Sphkstatic void m_reclaim __P((void)); 6712819Sphk 6815736Sphk/* "number of clusters of pages" */ 6915736Sphk#define NCL_INIT 1 7015736Sphk 7115744Sphk#define NMB_INIT 16 7215744Sphk 7310358Sjulian/* ARGSUSED*/ 7410358Sjulianstatic void 7512569Sbdembinit(dummy) 7612569Sbde void *dummy; 771541Srgrimes{ 781541Srgrimes int s; 791541Srgrimes 8015689Swollman mmbfree = NULL; mclfree = NULL; 811541Srgrimes s = splimp(); 8215689Swollman if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0) 8315689Swollman goto bad; 841541Srgrimes if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0) 851541Srgrimes goto bad; 861541Srgrimes splx(s); 871541Srgrimes return; 881541Srgrimesbad: 891541Srgrimes panic("mbinit"); 901541Srgrimes} 911541Srgrimes 921541Srgrimes/* 9315689Swollman * Allocate at least nmb mbufs and place on mbuf free list. 9415689Swollman * Must be called at splimp. 9515689Swollman */ 9615689Swollman/* ARGSUSED */ 9715689Swollmanint 9815689Swollmanm_mballoc(nmb, nowait) 9915689Swollman register int nmb; 10015689Swollman int nowait; 10115689Swollman{ 10215689Swollman register caddr_t p; 10315689Swollman register int i; 10415689Swollman int nbytes; 10515689Swollman 10615689Swollman /* Once we run out of map space, it will be impossible to get 10715689Swollman * any more (nothing is ever freed back to the map) (XXX which 10815689Swollman * is dumb). (however you are not dead as m_reclaim might 10915689Swollman * still be able to free a substantial amount of space). 11015689Swollman */ 11115689Swollman if (mb_map_full) 11215689Swollman return (0); 11315689Swollman 11415689Swollman nbytes = round_page(nmb * MSIZE); 11515689Swollman p = (caddr_t)kmem_malloc(mb_map, nbytes, nowait ? M_NOWAIT : M_WAITOK); 11615689Swollman /* 11715689Swollman * Either the map is now full, or this is nowait and there 11815689Swollman * are no pages left. 11915689Swollman */ 12015689Swollman if (p == NULL) 12115689Swollman return (0); 12215689Swollman 12315689Swollman nmb = nbytes / MSIZE; 12415689Swollman for (i = 0; i < nmb; i++) { 12515689Swollman ((struct mbuf *)p)->m_next = mmbfree; 12615689Swollman mmbfree = (struct mbuf *)p; 12715689Swollman p += MSIZE; 12815689Swollman } 12915689Swollman mbstat.m_mbufs += nmb; 13015689Swollman return (1); 13115689Swollman} 13215689Swollman 13315689Swollman/* 1341541Srgrimes * Allocate some number of mbuf clusters 1351541Srgrimes * and place on cluster free list. 1361541Srgrimes * Must be called at splimp. 1371541Srgrimes */ 1381541Srgrimes/* ARGSUSED */ 1391549Srgrimesint 1401541Srgrimesm_clalloc(ncl, nowait) 1411541Srgrimes register int ncl; 1421541Srgrimes int nowait; 1431541Srgrimes{ 1441541Srgrimes register caddr_t p; 1451541Srgrimes register int i; 1461541Srgrimes int npg; 1471541Srgrimes 1487066Sdg /* 1497066Sdg * Once we run out of map space, it will be impossible 1507066Sdg * to get any more (nothing is ever freed back to the 1517066Sdg * map). 1527066Sdg */ 15315722Swollman if (mcl_map_full) 1547066Sdg return (0); 1557066Sdg 15615543Sphk npg = ncl; 15715722Swollman p = (caddr_t)kmem_malloc(mcl_map, ctob(npg), 1586191Sbde nowait ? M_NOWAIT : M_WAITOK); 1597066Sdg /* 1607066Sdg * Either the map is now full, or this is nowait and there 1617066Sdg * are no pages left. 1627066Sdg */ 1637066Sdg if (p == NULL) 1641541Srgrimes return (0); 1657066Sdg 16615543Sphk ncl = ncl * PAGE_SIZE / MCLBYTES; 1671541Srgrimes for (i = 0; i < ncl; i++) { 1681541Srgrimes ((union mcluster *)p)->mcl_next = mclfree; 1691541Srgrimes mclfree = (union mcluster *)p; 1701541Srgrimes p += MCLBYTES; 1711541Srgrimes mbstat.m_clfree++; 1721541Srgrimes } 1731541Srgrimes mbstat.m_clusters += ncl; 1741541Srgrimes return (1); 1751541Srgrimes} 1761541Srgrimes 1771541Srgrimes/* 1781541Srgrimes * When MGET failes, ask protocols to free space when short of memory, 1791541Srgrimes * then re-attempt to allocate an mbuf. 1801541Srgrimes */ 1811541Srgrimesstruct mbuf * 1821541Srgrimesm_retry(i, t) 1831541Srgrimes int i, t; 1841541Srgrimes{ 1851541Srgrimes register struct mbuf *m; 1861541Srgrimes 1871541Srgrimes m_reclaim(); 1881541Srgrimes#define m_retry(i, t) (struct mbuf *)0 1891541Srgrimes MGET(m, i, t); 1901541Srgrimes#undef m_retry 1916669Sdg if (m != NULL) 1926669Sdg mbstat.m_wait++; 1936669Sdg else 1946669Sdg mbstat.m_drops++; 1951541Srgrimes return (m); 1961541Srgrimes} 1971541Srgrimes 1981541Srgrimes/* 1991541Srgrimes * As above; retry an MGETHDR. 2001541Srgrimes */ 2011541Srgrimesstruct mbuf * 2021541Srgrimesm_retryhdr(i, t) 2031541Srgrimes int i, t; 2041541Srgrimes{ 2051541Srgrimes register struct mbuf *m; 2061541Srgrimes 2071541Srgrimes m_reclaim(); 2081541Srgrimes#define m_retryhdr(i, t) (struct mbuf *)0 2091541Srgrimes MGETHDR(m, i, t); 2101541Srgrimes#undef m_retryhdr 2116669Sdg if (m != NULL) 2126669Sdg mbstat.m_wait++; 2136669Sdg else 2146669Sdg mbstat.m_drops++; 2151541Srgrimes return (m); 2161541Srgrimes} 2171541Srgrimes 21812819Sphkstatic void 2191541Srgrimesm_reclaim() 2201541Srgrimes{ 2211541Srgrimes register struct domain *dp; 2221541Srgrimes register struct protosw *pr; 2231541Srgrimes int s = splimp(); 2241541Srgrimes 2251541Srgrimes for (dp = domains; dp; dp = dp->dom_next) 2261541Srgrimes for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 2271541Srgrimes if (pr->pr_drain) 2281541Srgrimes (*pr->pr_drain)(); 2291541Srgrimes splx(s); 2301541Srgrimes mbstat.m_drain++; 2311541Srgrimes} 2321541Srgrimes 2331541Srgrimes/* 2341541Srgrimes * Space allocation routines. 2351541Srgrimes * These are also available as macros 2361541Srgrimes * for critical paths. 2371541Srgrimes */ 2381541Srgrimesstruct mbuf * 2391541Srgrimesm_get(nowait, type) 2401541Srgrimes int nowait, type; 2411541Srgrimes{ 2421541Srgrimes register struct mbuf *m; 2431541Srgrimes 2441541Srgrimes MGET(m, nowait, type); 2451541Srgrimes return (m); 2461541Srgrimes} 2471541Srgrimes 2481541Srgrimesstruct mbuf * 2491541Srgrimesm_gethdr(nowait, type) 2501541Srgrimes int nowait, type; 2511541Srgrimes{ 2521541Srgrimes register struct mbuf *m; 2531541Srgrimes 2541541Srgrimes MGETHDR(m, nowait, type); 2551541Srgrimes return (m); 2561541Srgrimes} 2571541Srgrimes 2581541Srgrimesstruct mbuf * 2591541Srgrimesm_getclr(nowait, type) 2601541Srgrimes int nowait, type; 2611541Srgrimes{ 2621541Srgrimes register struct mbuf *m; 2631541Srgrimes 2641541Srgrimes MGET(m, nowait, type); 2651541Srgrimes if (m == 0) 2661541Srgrimes return (0); 2671541Srgrimes bzero(mtod(m, caddr_t), MLEN); 2681541Srgrimes return (m); 2691541Srgrimes} 2701541Srgrimes 2711541Srgrimesstruct mbuf * 2721541Srgrimesm_free(m) 2731541Srgrimes struct mbuf *m; 2741541Srgrimes{ 2751541Srgrimes register struct mbuf *n; 2761541Srgrimes 2771541Srgrimes MFREE(m, n); 2781541Srgrimes return (n); 2791541Srgrimes} 2801541Srgrimes 2811541Srgrimesvoid 2821541Srgrimesm_freem(m) 2831541Srgrimes register struct mbuf *m; 2841541Srgrimes{ 2851541Srgrimes register struct mbuf *n; 2861541Srgrimes 2871541Srgrimes if (m == NULL) 2881541Srgrimes return; 2891541Srgrimes do { 2901541Srgrimes MFREE(m, n); 2913308Sphk m = n; 2923308Sphk } while (m); 2931541Srgrimes} 2941541Srgrimes 2951541Srgrimes/* 2961541Srgrimes * Mbuffer utility routines. 2971541Srgrimes */ 2981541Srgrimes 2991541Srgrimes/* 3001541Srgrimes * Lesser-used path for M_PREPEND: 3011541Srgrimes * allocate new mbuf to prepend to chain, 3021541Srgrimes * copy junk along. 3031541Srgrimes */ 3041541Srgrimesstruct mbuf * 3051541Srgrimesm_prepend(m, len, how) 3061541Srgrimes register struct mbuf *m; 3071541Srgrimes int len, how; 3081541Srgrimes{ 3091541Srgrimes struct mbuf *mn; 3101541Srgrimes 3111541Srgrimes MGET(mn, how, m->m_type); 3121541Srgrimes if (mn == (struct mbuf *)NULL) { 3131541Srgrimes m_freem(m); 3141541Srgrimes return ((struct mbuf *)NULL); 3151541Srgrimes } 3161541Srgrimes if (m->m_flags & M_PKTHDR) { 3171541Srgrimes M_COPY_PKTHDR(mn, m); 3181541Srgrimes m->m_flags &= ~M_PKTHDR; 3191541Srgrimes } 3201541Srgrimes mn->m_next = m; 3211541Srgrimes m = mn; 3221541Srgrimes if (len < MHLEN) 3231541Srgrimes MH_ALIGN(m, len); 3241541Srgrimes m->m_len = len; 3251541Srgrimes return (m); 3261541Srgrimes} 3271541Srgrimes 3281541Srgrimes/* 3291541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 3301541Srgrimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 3311541Srgrimes * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller. 3321541Srgrimes */ 33312819Sphkstatic int MCFail; 3341541Srgrimes 3351541Srgrimesstruct mbuf * 3361541Srgrimesm_copym(m, off0, len, wait) 3371541Srgrimes register struct mbuf *m; 3381541Srgrimes int off0, wait; 3391541Srgrimes register int len; 3401541Srgrimes{ 3411541Srgrimes register struct mbuf *n, **np; 3421541Srgrimes register int off = off0; 3431541Srgrimes struct mbuf *top; 3441541Srgrimes int copyhdr = 0; 3451541Srgrimes 3461541Srgrimes if (off < 0 || len < 0) 3471541Srgrimes panic("m_copym"); 3481541Srgrimes if (off == 0 && m->m_flags & M_PKTHDR) 3491541Srgrimes copyhdr = 1; 3501541Srgrimes while (off > 0) { 3511541Srgrimes if (m == 0) 3521541Srgrimes panic("m_copym"); 3531541Srgrimes if (off < m->m_len) 3541541Srgrimes break; 3551541Srgrimes off -= m->m_len; 3561541Srgrimes m = m->m_next; 3571541Srgrimes } 3581541Srgrimes np = ⊤ 3591541Srgrimes top = 0; 3601541Srgrimes while (len > 0) { 3611541Srgrimes if (m == 0) { 3621541Srgrimes if (len != M_COPYALL) 3631541Srgrimes panic("m_copym"); 3641541Srgrimes break; 3651541Srgrimes } 3661541Srgrimes MGET(n, wait, m->m_type); 3671541Srgrimes *np = n; 3681541Srgrimes if (n == 0) 3691541Srgrimes goto nospace; 3701541Srgrimes if (copyhdr) { 3711541Srgrimes M_COPY_PKTHDR(n, m); 3721541Srgrimes if (len == M_COPYALL) 3731541Srgrimes n->m_pkthdr.len -= off0; 3741541Srgrimes else 3751541Srgrimes n->m_pkthdr.len = len; 3761541Srgrimes copyhdr = 0; 3771541Srgrimes } 3781541Srgrimes n->m_len = min(len, m->m_len - off); 3791541Srgrimes if (m->m_flags & M_EXT) { 3801541Srgrimes n->m_data = m->m_data + off; 38117663Sjulian if(!m->m_ext.ext_ref) 38217663Sjulian mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 38317663Sjulian else 38417663Sjulian (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 38517663Sjulian m->m_ext.ext_size); 3861541Srgrimes n->m_ext = m->m_ext; 3871541Srgrimes n->m_flags |= M_EXT; 3881541Srgrimes } else 3891541Srgrimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 3901541Srgrimes (unsigned)n->m_len); 3911541Srgrimes if (len != M_COPYALL) 3921541Srgrimes len -= n->m_len; 3931541Srgrimes off = 0; 3941541Srgrimes m = m->m_next; 3951541Srgrimes np = &n->m_next; 3961541Srgrimes } 3971541Srgrimes if (top == 0) 3981541Srgrimes MCFail++; 3991541Srgrimes return (top); 4001541Srgrimesnospace: 4011541Srgrimes m_freem(top); 4021541Srgrimes MCFail++; 4031541Srgrimes return (0); 4041541Srgrimes} 4051541Srgrimes 4061541Srgrimes/* 40715689Swollman * Copy an entire packet, including header (which must be present). 40815689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 40915689Swollman */ 41015689Swollmanstruct mbuf * 41115689Swollmanm_copypacket(m, how) 41215689Swollman struct mbuf *m; 41315689Swollman int how; 41415689Swollman{ 41515689Swollman struct mbuf *top, *n, *o; 41615689Swollman 41715689Swollman MGET(n, how, m->m_type); 41815689Swollman top = n; 41915689Swollman if (!n) 42015689Swollman goto nospace; 42115689Swollman 42215689Swollman M_COPY_PKTHDR(n, m); 42315689Swollman n->m_len = m->m_len; 42415689Swollman if (m->m_flags & M_EXT) { 42515689Swollman n->m_data = m->m_data; 42615689Swollman mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 42715689Swollman n->m_ext = m->m_ext; 42815689Swollman n->m_flags |= M_EXT; 42915689Swollman } else { 43015689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 43115689Swollman } 43215689Swollman 43315689Swollman m = m->m_next; 43415689Swollman while (m) { 43515689Swollman MGET(o, how, m->m_type); 43615689Swollman if (!o) 43715689Swollman goto nospace; 43815689Swollman 43915689Swollman n->m_next = o; 44015689Swollman n = n->m_next; 44115689Swollman 44215689Swollman n->m_len = m->m_len; 44315689Swollman if (m->m_flags & M_EXT) { 44415689Swollman n->m_data = m->m_data; 44515689Swollman mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 44615689Swollman n->m_ext = m->m_ext; 44715689Swollman n->m_flags |= M_EXT; 44815689Swollman } else { 44915689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 45015689Swollman } 45115689Swollman 45215689Swollman m = m->m_next; 45315689Swollman } 45415689Swollman return top; 45515689Swollmannospace: 45615689Swollman m_freem(top); 45715689Swollman MCFail++; 45815689Swollman return 0; 45915689Swollman} 46015689Swollman 46115689Swollman/* 4621541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 4631541Srgrimes * continuing for "len" bytes, into the indicated buffer. 4641541Srgrimes */ 4651549Srgrimesvoid 4661541Srgrimesm_copydata(m, off, len, cp) 4671541Srgrimes register struct mbuf *m; 4681541Srgrimes register int off; 4691541Srgrimes register int len; 4701541Srgrimes caddr_t cp; 4711541Srgrimes{ 4721541Srgrimes register unsigned count; 4731541Srgrimes 4741541Srgrimes if (off < 0 || len < 0) 4751541Srgrimes panic("m_copydata"); 4761541Srgrimes while (off > 0) { 4771541Srgrimes if (m == 0) 4781541Srgrimes panic("m_copydata"); 4791541Srgrimes if (off < m->m_len) 4801541Srgrimes break; 4811541Srgrimes off -= m->m_len; 4821541Srgrimes m = m->m_next; 4831541Srgrimes } 4841541Srgrimes while (len > 0) { 4851541Srgrimes if (m == 0) 4861541Srgrimes panic("m_copydata"); 4871541Srgrimes count = min(m->m_len - off, len); 4881541Srgrimes bcopy(mtod(m, caddr_t) + off, cp, count); 4891541Srgrimes len -= count; 4901541Srgrimes cp += count; 4911541Srgrimes off = 0; 4921541Srgrimes m = m->m_next; 4931541Srgrimes } 4941541Srgrimes} 4951541Srgrimes 4961541Srgrimes/* 4971541Srgrimes * Concatenate mbuf chain n to m. 4981541Srgrimes * Both chains must be of the same type (e.g. MT_DATA). 4991541Srgrimes * Any m_pkthdr is not updated. 5001541Srgrimes */ 5011549Srgrimesvoid 5021541Srgrimesm_cat(m, n) 5031541Srgrimes register struct mbuf *m, *n; 5041541Srgrimes{ 5051541Srgrimes while (m->m_next) 5061541Srgrimes m = m->m_next; 5071541Srgrimes while (n) { 5081541Srgrimes if (m->m_flags & M_EXT || 5091541Srgrimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 5101541Srgrimes /* just join the two chains */ 5111541Srgrimes m->m_next = n; 5121541Srgrimes return; 5131541Srgrimes } 5141541Srgrimes /* splat the data from one into the other */ 5151541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 5161541Srgrimes (u_int)n->m_len); 5171541Srgrimes m->m_len += n->m_len; 5181541Srgrimes n = m_free(n); 5191541Srgrimes } 5201541Srgrimes} 5211541Srgrimes 5221549Srgrimesvoid 5231541Srgrimesm_adj(mp, req_len) 5241541Srgrimes struct mbuf *mp; 5251541Srgrimes int req_len; 5261541Srgrimes{ 5271541Srgrimes register int len = req_len; 5281541Srgrimes register struct mbuf *m; 5291541Srgrimes register count; 5301541Srgrimes 5311541Srgrimes if ((m = mp) == NULL) 5321541Srgrimes return; 5331541Srgrimes if (len >= 0) { 5341541Srgrimes /* 5351541Srgrimes * Trim from head. 5361541Srgrimes */ 5371541Srgrimes while (m != NULL && len > 0) { 5381541Srgrimes if (m->m_len <= len) { 5391541Srgrimes len -= m->m_len; 5401541Srgrimes m->m_len = 0; 5411541Srgrimes m = m->m_next; 5421541Srgrimes } else { 5431541Srgrimes m->m_len -= len; 5441541Srgrimes m->m_data += len; 5451541Srgrimes len = 0; 5461541Srgrimes } 5471541Srgrimes } 5481541Srgrimes m = mp; 5491541Srgrimes if (mp->m_flags & M_PKTHDR) 5501541Srgrimes m->m_pkthdr.len -= (req_len - len); 5511541Srgrimes } else { 5521541Srgrimes /* 5531541Srgrimes * Trim from tail. Scan the mbuf chain, 5541541Srgrimes * calculating its length and finding the last mbuf. 5551541Srgrimes * If the adjustment only affects this mbuf, then just 5561541Srgrimes * adjust and return. Otherwise, rescan and truncate 5571541Srgrimes * after the remaining size. 5581541Srgrimes */ 5591541Srgrimes len = -len; 5601541Srgrimes count = 0; 5611541Srgrimes for (;;) { 5621541Srgrimes count += m->m_len; 5631541Srgrimes if (m->m_next == (struct mbuf *)0) 5641541Srgrimes break; 5651541Srgrimes m = m->m_next; 5661541Srgrimes } 5671541Srgrimes if (m->m_len >= len) { 5681541Srgrimes m->m_len -= len; 5691541Srgrimes if (mp->m_flags & M_PKTHDR) 5701541Srgrimes mp->m_pkthdr.len -= len; 5711541Srgrimes return; 5721541Srgrimes } 5731541Srgrimes count -= len; 5741541Srgrimes if (count < 0) 5751541Srgrimes count = 0; 5761541Srgrimes /* 5771541Srgrimes * Correct length for chain is "count". 5781541Srgrimes * Find the mbuf with last data, adjust its length, 5791541Srgrimes * and toss data from remaining mbufs on chain. 5801541Srgrimes */ 5811541Srgrimes m = mp; 5821541Srgrimes if (m->m_flags & M_PKTHDR) 5831541Srgrimes m->m_pkthdr.len = count; 5841541Srgrimes for (; m; m = m->m_next) { 5851541Srgrimes if (m->m_len >= count) { 5861541Srgrimes m->m_len = count; 5871541Srgrimes break; 5881541Srgrimes } 5891541Srgrimes count -= m->m_len; 5901541Srgrimes } 5913308Sphk while (m->m_next) 5923308Sphk (m = m->m_next) ->m_len = 0; 5931541Srgrimes } 5941541Srgrimes} 5951541Srgrimes 5961541Srgrimes/* 5971541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous 5981541Srgrimes * and in the data area of an mbuf (so that mtod and dtom 5991541Srgrimes * will work for a structure of size len). Returns the resulting 6001541Srgrimes * mbuf chain on success, frees it and returns null on failure. 6011541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the 6021541Srgrimes * contiguous region in an attempt to avoid being called next time. 6031541Srgrimes */ 60412819Sphkstatic int MPFail; 6051541Srgrimes 6061541Srgrimesstruct mbuf * 6071541Srgrimesm_pullup(n, len) 6081541Srgrimes register struct mbuf *n; 6091541Srgrimes int len; 6101541Srgrimes{ 6111541Srgrimes register struct mbuf *m; 6121541Srgrimes register int count; 6131541Srgrimes int space; 6141541Srgrimes 6151541Srgrimes /* 6161541Srgrimes * If first mbuf has no cluster, and has room for len bytes 6171541Srgrimes * without shifting current data, pullup into it, 6181541Srgrimes * otherwise allocate a new mbuf to prepend to the chain. 6191541Srgrimes */ 6201541Srgrimes if ((n->m_flags & M_EXT) == 0 && 6211541Srgrimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 6221541Srgrimes if (n->m_len >= len) 6231541Srgrimes return (n); 6241541Srgrimes m = n; 6251541Srgrimes n = n->m_next; 6261541Srgrimes len -= m->m_len; 6271541Srgrimes } else { 6281541Srgrimes if (len > MHLEN) 6291541Srgrimes goto bad; 6301541Srgrimes MGET(m, M_DONTWAIT, n->m_type); 6311541Srgrimes if (m == 0) 6321541Srgrimes goto bad; 6331541Srgrimes m->m_len = 0; 6341541Srgrimes if (n->m_flags & M_PKTHDR) { 6351541Srgrimes M_COPY_PKTHDR(m, n); 6361541Srgrimes n->m_flags &= ~M_PKTHDR; 6371541Srgrimes } 6381541Srgrimes } 6391541Srgrimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 6401541Srgrimes do { 6411541Srgrimes count = min(min(max(len, max_protohdr), space), n->m_len); 6421541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 6431541Srgrimes (unsigned)count); 6441541Srgrimes len -= count; 6451541Srgrimes m->m_len += count; 6461541Srgrimes n->m_len -= count; 6471541Srgrimes space -= count; 6481541Srgrimes if (n->m_len) 6491541Srgrimes n->m_data += count; 6501541Srgrimes else 6511541Srgrimes n = m_free(n); 6521541Srgrimes } while (len > 0 && n); 6531541Srgrimes if (len > 0) { 6541541Srgrimes (void) m_free(m); 6551541Srgrimes goto bad; 6561541Srgrimes } 6571541Srgrimes m->m_next = n; 6581541Srgrimes return (m); 6591541Srgrimesbad: 6601541Srgrimes m_freem(n); 6611541Srgrimes MPFail++; 6621541Srgrimes return (0); 6631541Srgrimes} 6641541Srgrimes 6651541Srgrimes/* 6661541Srgrimes * Partition an mbuf chain in two pieces, returning the tail -- 6671541Srgrimes * all but the first len0 bytes. In case of failure, it returns NULL and 6681541Srgrimes * attempts to restore the chain to its original state. 6691541Srgrimes */ 6701541Srgrimesstruct mbuf * 6711541Srgrimesm_split(m0, len0, wait) 6721541Srgrimes register struct mbuf *m0; 6731541Srgrimes int len0, wait; 6741541Srgrimes{ 6751541Srgrimes register struct mbuf *m, *n; 6761541Srgrimes unsigned len = len0, remain; 6771541Srgrimes 6781541Srgrimes for (m = m0; m && len > m->m_len; m = m->m_next) 6791541Srgrimes len -= m->m_len; 6801541Srgrimes if (m == 0) 6811541Srgrimes return (0); 6821541Srgrimes remain = m->m_len - len; 6831541Srgrimes if (m0->m_flags & M_PKTHDR) { 6841541Srgrimes MGETHDR(n, wait, m0->m_type); 6851541Srgrimes if (n == 0) 6861541Srgrimes return (0); 6871541Srgrimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 6881541Srgrimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 6891541Srgrimes m0->m_pkthdr.len = len0; 6901541Srgrimes if (m->m_flags & M_EXT) 6911541Srgrimes goto extpacket; 6921541Srgrimes if (remain > MHLEN) { 6931541Srgrimes /* m can't be the lead packet */ 6941541Srgrimes MH_ALIGN(n, 0); 6951541Srgrimes n->m_next = m_split(m, len, wait); 6961541Srgrimes if (n->m_next == 0) { 6971541Srgrimes (void) m_free(n); 6981541Srgrimes return (0); 6991541Srgrimes } else 7001541Srgrimes return (n); 7011541Srgrimes } else 7021541Srgrimes MH_ALIGN(n, remain); 7031541Srgrimes } else if (remain == 0) { 7041541Srgrimes n = m->m_next; 7051541Srgrimes m->m_next = 0; 7061541Srgrimes return (n); 7071541Srgrimes } else { 7081541Srgrimes MGET(n, wait, m->m_type); 7091541Srgrimes if (n == 0) 7101541Srgrimes return (0); 7111541Srgrimes M_ALIGN(n, remain); 7121541Srgrimes } 7131541Srgrimesextpacket: 7141541Srgrimes if (m->m_flags & M_EXT) { 7151541Srgrimes n->m_flags |= M_EXT; 7161541Srgrimes n->m_ext = m->m_ext; 71717663Sjulian if(!m->m_ext.ext_ref) 71817663Sjulian mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 71917663Sjulian else 72017663Sjulian (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 72117663Sjulian m->m_ext.ext_size); 7221541Srgrimes m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */ 7231541Srgrimes n->m_data = m->m_data + len; 7241541Srgrimes } else { 7251541Srgrimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 7261541Srgrimes } 7271541Srgrimes n->m_len = remain; 7281541Srgrimes m->m_len = len; 7291541Srgrimes n->m_next = m->m_next; 7301541Srgrimes m->m_next = 0; 7311541Srgrimes return (n); 7321541Srgrimes} 7331541Srgrimes/* 7341541Srgrimes * Routine to copy from device local memory into mbufs. 7351541Srgrimes */ 7361541Srgrimesstruct mbuf * 7371541Srgrimesm_devget(buf, totlen, off0, ifp, copy) 7381541Srgrimes char *buf; 7391541Srgrimes int totlen, off0; 7401541Srgrimes struct ifnet *ifp; 74112577Sbde void (*copy) __P((char *from, caddr_t to, u_int len)); 7421541Srgrimes{ 7431541Srgrimes register struct mbuf *m; 7441541Srgrimes struct mbuf *top = 0, **mp = ⊤ 7451541Srgrimes register int off = off0, len; 7461541Srgrimes register char *cp; 7471541Srgrimes char *epkt; 7481541Srgrimes 7491541Srgrimes cp = buf; 7501541Srgrimes epkt = cp + totlen; 7511541Srgrimes if (off) { 7521541Srgrimes cp += off + 2 * sizeof(u_short); 7531541Srgrimes totlen -= 2 * sizeof(u_short); 7541541Srgrimes } 7551541Srgrimes MGETHDR(m, M_DONTWAIT, MT_DATA); 7561541Srgrimes if (m == 0) 7571541Srgrimes return (0); 7581541Srgrimes m->m_pkthdr.rcvif = ifp; 7591541Srgrimes m->m_pkthdr.len = totlen; 7601541Srgrimes m->m_len = MHLEN; 7611541Srgrimes 7621541Srgrimes while (totlen > 0) { 7631541Srgrimes if (top) { 7641541Srgrimes MGET(m, M_DONTWAIT, MT_DATA); 7651541Srgrimes if (m == 0) { 7661541Srgrimes m_freem(top); 7671541Srgrimes return (0); 7681541Srgrimes } 7691541Srgrimes m->m_len = MLEN; 7701541Srgrimes } 7711541Srgrimes len = min(totlen, epkt - cp); 7721541Srgrimes if (len >= MINCLSIZE) { 7731541Srgrimes MCLGET(m, M_DONTWAIT); 7741541Srgrimes if (m->m_flags & M_EXT) 7751541Srgrimes m->m_len = len = min(len, MCLBYTES); 7761541Srgrimes else 7771541Srgrimes len = m->m_len; 7781541Srgrimes } else { 7791541Srgrimes /* 7801541Srgrimes * Place initial small packet/header at end of mbuf. 7811541Srgrimes */ 7821541Srgrimes if (len < m->m_len) { 7831541Srgrimes if (top == 0 && len + max_linkhdr <= m->m_len) 7841541Srgrimes m->m_data += max_linkhdr; 7851541Srgrimes m->m_len = len; 7861541Srgrimes } else 7871541Srgrimes len = m->m_len; 7881541Srgrimes } 7891541Srgrimes if (copy) 7901541Srgrimes copy(cp, mtod(m, caddr_t), (unsigned)len); 7911541Srgrimes else 7921541Srgrimes bcopy(cp, mtod(m, caddr_t), (unsigned)len); 7931541Srgrimes cp += len; 7941541Srgrimes *mp = m; 7951541Srgrimes mp = &m->m_next; 7961541Srgrimes totlen -= len; 7971541Srgrimes if (cp == epkt) 7981541Srgrimes cp = buf; 7991541Srgrimes } 8001541Srgrimes return (top); 8011541Srgrimes} 8023352Sphk 8033352Sphk/* 8043352Sphk * Copy data from a buffer back into the indicated mbuf chain, 8053352Sphk * starting "off" bytes from the beginning, extending the mbuf 8063352Sphk * chain if necessary. 8073352Sphk */ 8083352Sphkvoid 8093352Sphkm_copyback(m0, off, len, cp) 8103352Sphk struct mbuf *m0; 8113352Sphk register int off; 8123352Sphk register int len; 8133352Sphk caddr_t cp; 8143352Sphk{ 8153352Sphk register int mlen; 8163352Sphk register struct mbuf *m = m0, *n; 8173352Sphk int totlen = 0; 8183352Sphk 8193352Sphk if (m0 == 0) 8203352Sphk return; 8213352Sphk while (off > (mlen = m->m_len)) { 8223352Sphk off -= mlen; 8233352Sphk totlen += mlen; 8243352Sphk if (m->m_next == 0) { 8253352Sphk n = m_getclr(M_DONTWAIT, m->m_type); 8263352Sphk if (n == 0) 8273352Sphk goto out; 8283352Sphk n->m_len = min(MLEN, len + off); 8293352Sphk m->m_next = n; 8303352Sphk } 8313352Sphk m = m->m_next; 8323352Sphk } 8333352Sphk while (len > 0) { 8343352Sphk mlen = min (m->m_len - off, len); 8353352Sphk bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); 8363352Sphk cp += mlen; 8373352Sphk len -= mlen; 8383352Sphk mlen += off; 8393352Sphk off = 0; 8403352Sphk totlen += mlen; 8413352Sphk if (len == 0) 8423352Sphk break; 8433352Sphk if (m->m_next == 0) { 8443352Sphk n = m_get(M_DONTWAIT, m->m_type); 8453352Sphk if (n == 0) 8463352Sphk break; 8473352Sphk n->m_len = min(MLEN, len); 8483352Sphk m->m_next = n; 8493352Sphk } 8503352Sphk m = m->m_next; 8513352Sphk } 8523352Sphkout: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 8533352Sphk m->m_pkthdr.len = totlen; 8543352Sphk} 855