Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.26
      1  1.26      fvdl /*	$NetBSD: uipc_mbuf.c,v 1.26 1998/03/01 02:22:33 fvdl Exp $	*/
      2  1.10       cgd 
      3   1.1       cgd /*
      4   1.9   mycroft  * Copyright (c) 1982, 1986, 1988, 1991, 1993
      5   1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35  1.26      fvdl  *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
     36   1.1       cgd  */
     37  1.24       mrg 
     38  1.24       mrg #include "opt_uvm.h"
     39   1.1       cgd 
     40   1.6   mycroft #include <sys/param.h>
     41   1.6   mycroft #include <sys/systm.h>
     42   1.6   mycroft #include <sys/proc.h>
     43   1.6   mycroft #include <sys/malloc.h>
     44   1.9   mycroft #include <sys/map.h>
     45   1.1       cgd #define MBTYPES
     46   1.6   mycroft #include <sys/mbuf.h>
     47   1.6   mycroft #include <sys/kernel.h>
     48   1.6   mycroft #include <sys/syslog.h>
     49   1.6   mycroft #include <sys/domain.h>
     50   1.6   mycroft #include <sys/protosw.h>
     51   1.6   mycroft 
     52   1.6   mycroft #include <vm/vm.h>
     53  1.14  christos 
     54  1.23       mrg #if defined(UVM)
     55  1.23       mrg #include <uvm/uvm_extern.h>
     56  1.23       mrg #endif
     57  1.23       mrg 
     58  1.18   thorpej struct mbuf *mbutl;
     59  1.18   thorpej struct mbstat mbstat;
     60  1.18   thorpej union mcluster *mclfree;
     61  1.18   thorpej int	max_linkhdr;
     62  1.18   thorpej int	max_protohdr;
     63  1.18   thorpej int	max_hdr;
     64  1.18   thorpej int	max_datalen;
     65  1.18   thorpej 
     66   1.1       cgd extern	vm_map_t mb_map;
     67   1.1       cgd 
     68   1.4       jtc void
     69   1.1       cgd mbinit()
     70   1.1       cgd {
     71   1.1       cgd 	int s;
     72   1.1       cgd 
     73  1.18   thorpej 	mclfree = NULL;
     74   1.1       cgd 	s = splimp();
     75  1.12   deraadt 	if (m_clalloc(max(4096/CLBYTES, 1), M_DONTWAIT) == 0)
     76   1.1       cgd 		goto bad;
     77   1.1       cgd 	splx(s);
     78   1.1       cgd 	return;
     79   1.1       cgd bad:
     80   1.1       cgd 	panic("mbinit");
     81   1.1       cgd }
     82   1.1       cgd 
     83   1.1       cgd /*
     84   1.1       cgd  * Allocate some number of mbuf clusters
     85   1.1       cgd  * and place on cluster free list.
     86   1.1       cgd  * Must be called at splimp.
     87   1.1       cgd  */
     88   1.1       cgd /* ARGSUSED */
     89  1.14  christos int
     90   1.5       cgd m_clalloc(ncl, nowait)
     91   1.1       cgd 	register int ncl;
     92   1.9   mycroft 	int nowait;
     93   1.1       cgd {
     94  1.17       gwr 	static volatile struct timeval lastlogged;
     95  1.16       cgd 	struct timeval curtime, logdiff;
     96   1.1       cgd 	register caddr_t p;
     97   1.1       cgd 	register int i;
     98  1.16       cgd 	int npg, s;
     99   1.1       cgd 
    100   1.1       cgd 	npg = ncl * CLSIZE;
    101  1.23       mrg #if defined(UVM)
    102  1.23       mrg 	p = (caddr_t)uvm_km_kmemalloc(mb_map, uvmexp.mb_object, ctob(npg),
    103  1.23       mrg 	    (nowait == M_DONTWAIT) ? UVM_KMF_NOWAIT : 0);
    104  1.23       mrg #else
    105  1.21        pk 	p = (caddr_t)kmem_malloc(mb_map, ctob(npg), nowait == 0);
    106  1.23       mrg #endif
    107   1.1       cgd 	if (p == NULL) {
    108  1.16       cgd 		s = splclock();
    109  1.16       cgd 		curtime = time;
    110  1.16       cgd 		splx(s);
    111  1.16       cgd 		timersub(&curtime, &lastlogged, &logdiff);
    112  1.16       cgd 		if (logdiff.tv_sec >= 60) {
    113  1.16       cgd 			lastlogged = curtime;
    114   1.1       cgd 			log(LOG_ERR, "mb_map full\n");
    115   1.1       cgd 		}
    116  1.16       cgd 		m_reclaim();
    117  1.16       cgd 		return (mclfree != NULL);
    118   1.1       cgd 	}
    119   1.1       cgd 	ncl = ncl * CLBYTES / MCLBYTES;
    120   1.1       cgd 	for (i = 0; i < ncl; i++) {
    121   1.1       cgd 		((union mcluster *)p)->mcl_next = mclfree;
    122   1.1       cgd 		mclfree = (union mcluster *)p;
    123   1.1       cgd 		p += MCLBYTES;
    124   1.1       cgd 		mbstat.m_clfree++;
    125   1.1       cgd 	}
    126   1.1       cgd 	mbstat.m_clusters += ncl;
    127   1.1       cgd 	return (1);
    128   1.1       cgd }
    129   1.1       cgd 
    130   1.1       cgd /*
    131   1.1       cgd  * When MGET failes, ask protocols to free space when short of memory,
    132   1.1       cgd  * then re-attempt to allocate an mbuf.
    133   1.1       cgd  */
    134   1.1       cgd struct mbuf *
    135   1.1       cgd m_retry(i, t)
    136   1.1       cgd 	int i, t;
    137   1.1       cgd {
    138   1.1       cgd 	register struct mbuf *m;
    139   1.1       cgd 
    140   1.1       cgd 	m_reclaim();
    141   1.1       cgd #define m_retry(i, t)	(struct mbuf *)0
    142   1.1       cgd 	MGET(m, i, t);
    143   1.1       cgd #undef m_retry
    144  1.18   thorpej 	if (m != NULL)
    145  1.18   thorpej 		mbstat.m_wait++;
    146  1.18   thorpej 	else
    147  1.18   thorpej 		mbstat.m_drops++;
    148   1.1       cgd 	return (m);
    149   1.1       cgd }
    150   1.1       cgd 
    151   1.1       cgd /*
    152   1.1       cgd  * As above; retry an MGETHDR.
    153   1.1       cgd  */
    154   1.1       cgd struct mbuf *
    155   1.1       cgd m_retryhdr(i, t)
    156   1.1       cgd 	int i, t;
    157   1.1       cgd {
    158   1.1       cgd 	register struct mbuf *m;
    159   1.1       cgd 
    160   1.1       cgd 	m_reclaim();
    161   1.1       cgd #define m_retryhdr(i, t) (struct mbuf *)0
    162   1.1       cgd 	MGETHDR(m, i, t);
    163   1.1       cgd #undef m_retryhdr
    164  1.18   thorpej 	if (m != NULL)
    165  1.18   thorpej 		mbstat.m_wait++;
    166  1.18   thorpej 	else
    167  1.18   thorpej 		mbstat.m_drops++;
    168   1.1       cgd 	return (m);
    169   1.1       cgd }
    170   1.1       cgd 
    171  1.14  christos void
    172   1.1       cgd m_reclaim()
    173   1.1       cgd {
    174   1.1       cgd 	register struct domain *dp;
    175   1.1       cgd 	register struct protosw *pr;
    176   1.1       cgd 	int s = splimp();
    177   1.1       cgd 
    178   1.1       cgd 	for (dp = domains; dp; dp = dp->dom_next)
    179   1.1       cgd 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
    180   1.1       cgd 			if (pr->pr_drain)
    181   1.1       cgd 				(*pr->pr_drain)();
    182   1.1       cgd 	splx(s);
    183   1.1       cgd 	mbstat.m_drain++;
    184   1.1       cgd }
    185   1.1       cgd 
    186   1.1       cgd /*
    187   1.1       cgd  * Space allocation routines.
    188   1.1       cgd  * These are also available as macros
    189   1.1       cgd  * for critical paths.
    190   1.1       cgd  */
    191   1.1       cgd struct mbuf *
    192   1.5       cgd m_get(nowait, type)
    193   1.5       cgd 	int nowait, type;
    194   1.1       cgd {
    195   1.1       cgd 	register struct mbuf *m;
    196   1.1       cgd 
    197   1.5       cgd 	MGET(m, nowait, type);
    198   1.1       cgd 	return (m);
    199   1.1       cgd }
    200   1.1       cgd 
    201   1.1       cgd struct mbuf *
    202   1.5       cgd m_gethdr(nowait, type)
    203   1.5       cgd 	int nowait, type;
    204   1.1       cgd {
    205   1.1       cgd 	register struct mbuf *m;
    206   1.1       cgd 
    207   1.5       cgd 	MGETHDR(m, nowait, type);
    208   1.1       cgd 	return (m);
    209   1.1       cgd }
    210   1.1       cgd 
    211   1.1       cgd struct mbuf *
    212   1.5       cgd m_getclr(nowait, type)
    213   1.5       cgd 	int nowait, type;
    214   1.1       cgd {
    215   1.1       cgd 	register struct mbuf *m;
    216   1.1       cgd 
    217   1.5       cgd 	MGET(m, nowait, type);
    218   1.1       cgd 	if (m == 0)
    219   1.1       cgd 		return (0);
    220   1.1       cgd 	bzero(mtod(m, caddr_t), MLEN);
    221   1.1       cgd 	return (m);
    222   1.1       cgd }
    223   1.1       cgd 
    224   1.1       cgd struct mbuf *
    225   1.1       cgd m_free(m)
    226   1.1       cgd 	struct mbuf *m;
    227   1.1       cgd {
    228   1.1       cgd 	register struct mbuf *n;
    229   1.1       cgd 
    230   1.1       cgd 	MFREE(m, n);
    231   1.1       cgd 	return (n);
    232   1.1       cgd }
    233   1.1       cgd 
    234   1.9   mycroft void
    235   1.1       cgd m_freem(m)
    236   1.1       cgd 	register struct mbuf *m;
    237   1.1       cgd {
    238   1.1       cgd 	register struct mbuf *n;
    239   1.1       cgd 
    240   1.1       cgd 	if (m == NULL)
    241   1.1       cgd 		return;
    242   1.1       cgd 	do {
    243   1.1       cgd 		MFREE(m, n);
    244  1.18   thorpej 		m = n;
    245  1.18   thorpej 	} while (m);
    246   1.1       cgd }
    247   1.1       cgd 
    248   1.1       cgd /*
    249   1.1       cgd  * Mbuffer utility routines.
    250   1.1       cgd  */
    251   1.1       cgd 
    252   1.1       cgd /*
    253   1.1       cgd  * Lesser-used path for M_PREPEND:
    254   1.1       cgd  * allocate new mbuf to prepend to chain,
    255   1.1       cgd  * copy junk along.
    256   1.1       cgd  */
    257   1.1       cgd struct mbuf *
    258   1.9   mycroft m_prepend(m, len, how)
    259   1.1       cgd 	register struct mbuf *m;
    260   1.9   mycroft 	int len, how;
    261   1.1       cgd {
    262   1.1       cgd 	struct mbuf *mn;
    263   1.1       cgd 
    264   1.9   mycroft 	MGET(mn, how, m->m_type);
    265   1.1       cgd 	if (mn == (struct mbuf *)NULL) {
    266   1.1       cgd 		m_freem(m);
    267   1.1       cgd 		return ((struct mbuf *)NULL);
    268   1.1       cgd 	}
    269   1.1       cgd 	if (m->m_flags & M_PKTHDR) {
    270   1.1       cgd 		M_COPY_PKTHDR(mn, m);
    271   1.1       cgd 		m->m_flags &= ~M_PKTHDR;
    272   1.1       cgd 	}
    273   1.1       cgd 	mn->m_next = m;
    274   1.1       cgd 	m = mn;
    275   1.1       cgd 	if (len < MHLEN)
    276   1.1       cgd 		MH_ALIGN(m, len);
    277   1.1       cgd 	m->m_len = len;
    278   1.1       cgd 	return (m);
    279   1.1       cgd }
    280   1.1       cgd 
    281   1.1       cgd /*
    282   1.1       cgd  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    283   1.1       cgd  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    284   1.1       cgd  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    285   1.1       cgd  */
    286   1.1       cgd int MCFail;
    287   1.1       cgd 
    288   1.1       cgd struct mbuf *
    289   1.1       cgd m_copym(m, off0, len, wait)
    290   1.1       cgd 	register struct mbuf *m;
    291   1.1       cgd 	int off0, wait;
    292   1.1       cgd 	register int len;
    293   1.1       cgd {
    294   1.1       cgd 	register struct mbuf *n, **np;
    295   1.1       cgd 	register int off = off0;
    296   1.1       cgd 	struct mbuf *top;
    297   1.1       cgd 	int copyhdr = 0;
    298   1.1       cgd 
    299   1.1       cgd 	if (off < 0 || len < 0)
    300   1.1       cgd 		panic("m_copym");
    301   1.1       cgd 	if (off == 0 && m->m_flags & M_PKTHDR)
    302   1.1       cgd 		copyhdr = 1;
    303   1.1       cgd 	while (off > 0) {
    304   1.1       cgd 		if (m == 0)
    305   1.1       cgd 			panic("m_copym");
    306   1.1       cgd 		if (off < m->m_len)
    307   1.1       cgd 			break;
    308   1.1       cgd 		off -= m->m_len;
    309   1.1       cgd 		m = m->m_next;
    310   1.1       cgd 	}
    311   1.1       cgd 	np = &top;
    312   1.1       cgd 	top = 0;
    313   1.1       cgd 	while (len > 0) {
    314   1.1       cgd 		if (m == 0) {
    315   1.1       cgd 			if (len != M_COPYALL)
    316   1.1       cgd 				panic("m_copym");
    317   1.1       cgd 			break;
    318   1.1       cgd 		}
    319   1.1       cgd 		MGET(n, wait, m->m_type);
    320   1.1       cgd 		*np = n;
    321   1.1       cgd 		if (n == 0)
    322   1.1       cgd 			goto nospace;
    323   1.1       cgd 		if (copyhdr) {
    324   1.1       cgd 			M_COPY_PKTHDR(n, m);
    325   1.1       cgd 			if (len == M_COPYALL)
    326   1.1       cgd 				n->m_pkthdr.len -= off0;
    327   1.1       cgd 			else
    328   1.1       cgd 				n->m_pkthdr.len = len;
    329   1.1       cgd 			copyhdr = 0;
    330   1.1       cgd 		}
    331   1.9   mycroft 		n->m_len = min(len, m->m_len - off);
    332   1.1       cgd 		if (m->m_flags & M_EXT) {
    333   1.1       cgd 			n->m_data = m->m_data + off;
    334   1.1       cgd 			n->m_ext = m->m_ext;
    335  1.18   thorpej 			MCLADDREFERENCE(m, n);
    336   1.1       cgd 		} else
    337   1.1       cgd 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
    338   1.1       cgd 			    (unsigned)n->m_len);
    339   1.1       cgd 		if (len != M_COPYALL)
    340   1.1       cgd 			len -= n->m_len;
    341   1.1       cgd 		off = 0;
    342   1.1       cgd 		m = m->m_next;
    343   1.1       cgd 		np = &n->m_next;
    344   1.1       cgd 	}
    345   1.1       cgd 	if (top == 0)
    346   1.1       cgd 		MCFail++;
    347   1.1       cgd 	return (top);
    348   1.1       cgd nospace:
    349   1.1       cgd 	m_freem(top);
    350   1.1       cgd 	MCFail++;
    351   1.1       cgd 	return (0);
    352   1.1       cgd }
    353   1.1       cgd 
    354   1.1       cgd /*
    355  1.18   thorpej  * Copy an entire packet, including header (which must be present).
    356  1.18   thorpej  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    357  1.18   thorpej  */
    358  1.18   thorpej struct mbuf *
    359  1.18   thorpej m_copypacket(m, how)
    360  1.18   thorpej 	struct mbuf *m;
    361  1.18   thorpej 	int how;
    362  1.18   thorpej {
    363  1.18   thorpej 	struct mbuf *top, *n, *o;
    364  1.18   thorpej 
    365  1.18   thorpej 	MGET(n, how, m->m_type);
    366  1.18   thorpej 	top = n;
    367  1.18   thorpej 	if (!n)
    368  1.18   thorpej 		goto nospace;
    369  1.18   thorpej 
    370  1.18   thorpej 	M_COPY_PKTHDR(n, m);
    371  1.18   thorpej 	n->m_len = m->m_len;
    372  1.18   thorpej 	if (m->m_flags & M_EXT) {
    373  1.18   thorpej 		n->m_data = m->m_data;
    374  1.18   thorpej 		n->m_ext = m->m_ext;
    375  1.18   thorpej 		MCLADDREFERENCE(m, n);
    376  1.18   thorpej 	} else {
    377  1.18   thorpej 		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
    378  1.18   thorpej 	}
    379  1.18   thorpej 
    380  1.18   thorpej 	m = m->m_next;
    381  1.18   thorpej 	while (m) {
    382  1.18   thorpej 		MGET(o, how, m->m_type);
    383  1.18   thorpej 		if (!o)
    384  1.18   thorpej 			goto nospace;
    385  1.18   thorpej 
    386  1.18   thorpej 		n->m_next = o;
    387  1.18   thorpej 		n = n->m_next;
    388  1.18   thorpej 
    389  1.18   thorpej 		n->m_len = m->m_len;
    390  1.18   thorpej 		if (m->m_flags & M_EXT) {
    391  1.18   thorpej 			n->m_data = m->m_data;
    392  1.18   thorpej 			n->m_ext = m->m_ext;
    393  1.18   thorpej 			MCLADDREFERENCE(m, n);
    394  1.18   thorpej 		} else {
    395  1.18   thorpej 			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
    396  1.18   thorpej 		}
    397  1.18   thorpej 
    398  1.18   thorpej 		m = m->m_next;
    399  1.18   thorpej 	}
    400  1.18   thorpej 	return top;
    401  1.18   thorpej nospace:
    402  1.18   thorpej 	m_freem(top);
    403  1.18   thorpej 	MCFail++;
    404  1.18   thorpej 	return 0;
    405  1.18   thorpej }
    406  1.18   thorpej 
    407  1.18   thorpej /*
    408   1.1       cgd  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    409   1.1       cgd  * continuing for "len" bytes, into the indicated buffer.
    410   1.1       cgd  */
    411  1.14  christos void
    412   1.1       cgd m_copydata(m, off, len, cp)
    413   1.1       cgd 	register struct mbuf *m;
    414   1.1       cgd 	register int off;
    415   1.1       cgd 	register int len;
    416   1.1       cgd 	caddr_t cp;
    417   1.1       cgd {
    418   1.1       cgd 	register unsigned count;
    419   1.1       cgd 
    420   1.1       cgd 	if (off < 0 || len < 0)
    421   1.1       cgd 		panic("m_copydata");
    422   1.1       cgd 	while (off > 0) {
    423   1.1       cgd 		if (m == 0)
    424   1.1       cgd 			panic("m_copydata");
    425   1.1       cgd 		if (off < m->m_len)
    426   1.1       cgd 			break;
    427   1.1       cgd 		off -= m->m_len;
    428   1.1       cgd 		m = m->m_next;
    429   1.1       cgd 	}
    430   1.1       cgd 	while (len > 0) {
    431   1.1       cgd 		if (m == 0)
    432   1.1       cgd 			panic("m_copydata");
    433   1.9   mycroft 		count = min(m->m_len - off, len);
    434   1.1       cgd 		bcopy(mtod(m, caddr_t) + off, cp, count);
    435   1.1       cgd 		len -= count;
    436   1.1       cgd 		cp += count;
    437   1.1       cgd 		off = 0;
    438   1.1       cgd 		m = m->m_next;
    439   1.1       cgd 	}
    440   1.1       cgd }
    441   1.1       cgd 
    442   1.1       cgd /*
    443   1.1       cgd  * Concatenate mbuf chain n to m.
    444   1.1       cgd  * Both chains must be of the same type (e.g. MT_DATA).
    445   1.1       cgd  * Any m_pkthdr is not updated.
    446   1.1       cgd  */
    447  1.14  christos void
    448   1.1       cgd m_cat(m, n)
    449   1.1       cgd 	register struct mbuf *m, *n;
    450   1.1       cgd {
    451   1.1       cgd 	while (m->m_next)
    452   1.1       cgd 		m = m->m_next;
    453   1.1       cgd 	while (n) {
    454   1.1       cgd 		if (m->m_flags & M_EXT ||
    455   1.1       cgd 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
    456   1.1       cgd 			/* just join the two chains */
    457   1.1       cgd 			m->m_next = n;
    458   1.1       cgd 			return;
    459   1.1       cgd 		}
    460   1.1       cgd 		/* splat the data from one into the other */
    461   1.1       cgd 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
    462   1.1       cgd 		    (u_int)n->m_len);
    463   1.1       cgd 		m->m_len += n->m_len;
    464   1.1       cgd 		n = m_free(n);
    465   1.1       cgd 	}
    466   1.1       cgd }
    467   1.1       cgd 
    468  1.11   mycroft void
    469   1.1       cgd m_adj(mp, req_len)
    470   1.1       cgd 	struct mbuf *mp;
    471   1.8   deraadt 	int req_len;
    472   1.1       cgd {
    473   1.1       cgd 	register int len = req_len;
    474   1.1       cgd 	register struct mbuf *m;
    475  1.25    kleink 	register int count;
    476   1.1       cgd 
    477   1.1       cgd 	if ((m = mp) == NULL)
    478   1.1       cgd 		return;
    479   1.1       cgd 	if (len >= 0) {
    480   1.1       cgd 		/*
    481   1.1       cgd 		 * Trim from head.
    482   1.1       cgd 		 */
    483   1.1       cgd 		while (m != NULL && len > 0) {
    484   1.1       cgd 			if (m->m_len <= len) {
    485   1.1       cgd 				len -= m->m_len;
    486   1.1       cgd 				m->m_len = 0;
    487   1.1       cgd 				m = m->m_next;
    488   1.1       cgd 			} else {
    489   1.1       cgd 				m->m_len -= len;
    490   1.1       cgd 				m->m_data += len;
    491   1.1       cgd 				len = 0;
    492   1.1       cgd 			}
    493   1.1       cgd 		}
    494   1.1       cgd 		m = mp;
    495   1.1       cgd 		if (mp->m_flags & M_PKTHDR)
    496   1.1       cgd 			m->m_pkthdr.len -= (req_len - len);
    497   1.1       cgd 	} else {
    498   1.1       cgd 		/*
    499   1.1       cgd 		 * Trim from tail.  Scan the mbuf chain,
    500   1.1       cgd 		 * calculating its length and finding the last mbuf.
    501   1.1       cgd 		 * If the adjustment only affects this mbuf, then just
    502   1.1       cgd 		 * adjust and return.  Otherwise, rescan and truncate
    503   1.1       cgd 		 * after the remaining size.
    504   1.1       cgd 		 */
    505   1.1       cgd 		len = -len;
    506   1.1       cgd 		count = 0;
    507   1.1       cgd 		for (;;) {
    508   1.1       cgd 			count += m->m_len;
    509   1.1       cgd 			if (m->m_next == (struct mbuf *)0)
    510   1.1       cgd 				break;
    511   1.1       cgd 			m = m->m_next;
    512   1.1       cgd 		}
    513   1.1       cgd 		if (m->m_len >= len) {
    514   1.1       cgd 			m->m_len -= len;
    515   1.8   deraadt 			if (mp->m_flags & M_PKTHDR)
    516   1.8   deraadt 				mp->m_pkthdr.len -= len;
    517   1.1       cgd 			return;
    518   1.1       cgd 		}
    519   1.1       cgd 		count -= len;
    520   1.1       cgd 		if (count < 0)
    521   1.1       cgd 			count = 0;
    522   1.1       cgd 		/*
    523   1.1       cgd 		 * Correct length for chain is "count".
    524   1.1       cgd 		 * Find the mbuf with last data, adjust its length,
    525   1.1       cgd 		 * and toss data from remaining mbufs on chain.
    526   1.1       cgd 		 */
    527   1.1       cgd 		m = mp;
    528   1.1       cgd 		if (m->m_flags & M_PKTHDR)
    529   1.1       cgd 			m->m_pkthdr.len = count;
    530   1.1       cgd 		for (; m; m = m->m_next) {
    531   1.1       cgd 			if (m->m_len >= count) {
    532   1.1       cgd 				m->m_len = count;
    533   1.1       cgd 				break;
    534   1.1       cgd 			}
    535   1.1       cgd 			count -= m->m_len;
    536   1.1       cgd 		}
    537  1.18   thorpej 		while (m->m_next)
    538  1.18   thorpej 			(m = m->m_next) ->m_len = 0;
    539   1.1       cgd 	}
    540   1.1       cgd }
    541   1.1       cgd 
    542   1.1       cgd /*
    543   1.1       cgd  * Rearange an mbuf chain so that len bytes are contiguous
    544   1.1       cgd  * and in the data area of an mbuf (so that mtod and dtom
    545   1.1       cgd  * will work for a structure of size len).  Returns the resulting
    546   1.1       cgd  * mbuf chain on success, frees it and returns null on failure.
    547   1.1       cgd  * If there is room, it will add up to max_protohdr-len extra bytes to the
    548   1.1       cgd  * contiguous region in an attempt to avoid being called next time.
    549   1.1       cgd  */
    550   1.1       cgd int MPFail;
    551   1.1       cgd 
    552   1.1       cgd struct mbuf *
    553   1.1       cgd m_pullup(n, len)
    554   1.1       cgd 	register struct mbuf *n;
    555   1.1       cgd 	int len;
    556   1.1       cgd {
    557   1.1       cgd 	register struct mbuf *m;
    558   1.1       cgd 	register int count;
    559   1.1       cgd 	int space;
    560   1.1       cgd 
    561   1.1       cgd 	/*
    562   1.1       cgd 	 * If first mbuf has no cluster, and has room for len bytes
    563   1.1       cgd 	 * without shifting current data, pullup into it,
    564   1.1       cgd 	 * otherwise allocate a new mbuf to prepend to the chain.
    565   1.1       cgd 	 */
    566   1.1       cgd 	if ((n->m_flags & M_EXT) == 0 &&
    567   1.1       cgd 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    568   1.1       cgd 		if (n->m_len >= len)
    569   1.1       cgd 			return (n);
    570   1.1       cgd 		m = n;
    571   1.1       cgd 		n = n->m_next;
    572   1.1       cgd 		len -= m->m_len;
    573   1.1       cgd 	} else {
    574   1.1       cgd 		if (len > MHLEN)
    575   1.1       cgd 			goto bad;
    576   1.1       cgd 		MGET(m, M_DONTWAIT, n->m_type);
    577   1.1       cgd 		if (m == 0)
    578   1.1       cgd 			goto bad;
    579   1.1       cgd 		m->m_len = 0;
    580   1.1       cgd 		if (n->m_flags & M_PKTHDR) {
    581   1.1       cgd 			M_COPY_PKTHDR(m, n);
    582   1.1       cgd 			n->m_flags &= ~M_PKTHDR;
    583   1.1       cgd 		}
    584   1.1       cgd 	}
    585   1.1       cgd 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    586   1.1       cgd 	do {
    587   1.1       cgd 		count = min(min(max(len, max_protohdr), space), n->m_len);
    588   1.1       cgd 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
    589   1.1       cgd 		  (unsigned)count);
    590   1.1       cgd 		len -= count;
    591   1.1       cgd 		m->m_len += count;
    592   1.1       cgd 		n->m_len -= count;
    593   1.1       cgd 		space -= count;
    594   1.1       cgd 		if (n->m_len)
    595   1.1       cgd 			n->m_data += count;
    596   1.1       cgd 		else
    597   1.1       cgd 			n = m_free(n);
    598   1.1       cgd 	} while (len > 0 && n);
    599   1.1       cgd 	if (len > 0) {
    600   1.1       cgd 		(void) m_free(m);
    601   1.1       cgd 		goto bad;
    602   1.1       cgd 	}
    603   1.1       cgd 	m->m_next = n;
    604   1.1       cgd 	return (m);
    605   1.1       cgd bad:
    606   1.1       cgd 	m_freem(n);
    607   1.1       cgd 	MPFail++;
    608   1.1       cgd 	return (0);
    609   1.9   mycroft }
    610   1.9   mycroft 
    611   1.9   mycroft /*
    612   1.9   mycroft  * Partition an mbuf chain in two pieces, returning the tail --
    613   1.9   mycroft  * all but the first len0 bytes.  In case of failure, it returns NULL and
    614   1.9   mycroft  * attempts to restore the chain to its original state.
    615   1.9   mycroft  */
    616   1.9   mycroft struct mbuf *
    617   1.9   mycroft m_split(m0, len0, wait)
    618   1.9   mycroft 	register struct mbuf *m0;
    619   1.9   mycroft 	int len0, wait;
    620   1.9   mycroft {
    621   1.9   mycroft 	register struct mbuf *m, *n;
    622  1.22   thorpej 	unsigned len = len0, remain, len_save;
    623   1.9   mycroft 
    624   1.9   mycroft 	for (m = m0; m && len > m->m_len; m = m->m_next)
    625   1.9   mycroft 		len -= m->m_len;
    626   1.9   mycroft 	if (m == 0)
    627   1.9   mycroft 		return (0);
    628   1.9   mycroft 	remain = m->m_len - len;
    629   1.9   mycroft 	if (m0->m_flags & M_PKTHDR) {
    630   1.9   mycroft 		MGETHDR(n, wait, m0->m_type);
    631   1.9   mycroft 		if (n == 0)
    632   1.9   mycroft 			return (0);
    633   1.9   mycroft 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    634   1.9   mycroft 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    635  1.22   thorpej 		len_save = m0->m_pkthdr.len;
    636   1.9   mycroft 		m0->m_pkthdr.len = len0;
    637   1.9   mycroft 		if (m->m_flags & M_EXT)
    638   1.9   mycroft 			goto extpacket;
    639   1.9   mycroft 		if (remain > MHLEN) {
    640   1.9   mycroft 			/* m can't be the lead packet */
    641   1.9   mycroft 			MH_ALIGN(n, 0);
    642   1.9   mycroft 			n->m_next = m_split(m, len, wait);
    643   1.9   mycroft 			if (n->m_next == 0) {
    644   1.9   mycroft 				(void) m_free(n);
    645  1.22   thorpej 				m0->m_pkthdr.len = len_save;
    646   1.9   mycroft 				return (0);
    647   1.9   mycroft 			} else
    648   1.9   mycroft 				return (n);
    649   1.9   mycroft 		} else
    650   1.9   mycroft 			MH_ALIGN(n, remain);
    651   1.9   mycroft 	} else if (remain == 0) {
    652   1.9   mycroft 		n = m->m_next;
    653   1.9   mycroft 		m->m_next = 0;
    654   1.9   mycroft 		return (n);
    655   1.9   mycroft 	} else {
    656   1.9   mycroft 		MGET(n, wait, m->m_type);
    657   1.9   mycroft 		if (n == 0)
    658   1.9   mycroft 			return (0);
    659   1.9   mycroft 		M_ALIGN(n, remain);
    660   1.9   mycroft 	}
    661   1.9   mycroft extpacket:
    662   1.9   mycroft 	if (m->m_flags & M_EXT) {
    663   1.9   mycroft 		n->m_ext = m->m_ext;
    664  1.18   thorpej 		MCLADDREFERENCE(m, n);
    665   1.9   mycroft 		n->m_data = m->m_data + len;
    666   1.9   mycroft 	} else {
    667   1.9   mycroft 		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
    668   1.9   mycroft 	}
    669   1.9   mycroft 	n->m_len = remain;
    670   1.9   mycroft 	m->m_len = len;
    671   1.9   mycroft 	n->m_next = m->m_next;
    672   1.9   mycroft 	m->m_next = 0;
    673   1.9   mycroft 	return (n);
    674   1.9   mycroft }
    675   1.9   mycroft /*
    676   1.9   mycroft  * Routine to copy from device local memory into mbufs.
    677   1.9   mycroft  */
    678   1.9   mycroft struct mbuf *
    679   1.9   mycroft m_devget(buf, totlen, off0, ifp, copy)
    680   1.9   mycroft 	char *buf;
    681   1.9   mycroft 	int totlen, off0;
    682   1.9   mycroft 	struct ifnet *ifp;
    683  1.18   thorpej 	void (*copy) __P((const void *from, void *to, size_t len));
    684   1.9   mycroft {
    685   1.9   mycroft 	register struct mbuf *m;
    686   1.9   mycroft 	struct mbuf *top = 0, **mp = &top;
    687   1.9   mycroft 	register int off = off0, len;
    688   1.9   mycroft 	register char *cp;
    689   1.9   mycroft 	char *epkt;
    690   1.9   mycroft 
    691   1.9   mycroft 	cp = buf;
    692   1.9   mycroft 	epkt = cp + totlen;
    693   1.9   mycroft 	if (off) {
    694  1.13       cgd 		/*
    695  1.13       cgd 		 * If 'off' is non-zero, packet is trailer-encapsulated,
    696  1.13       cgd 		 * so we have to skip the type and length fields.
    697  1.13       cgd 		 */
    698  1.13       cgd 		cp += off + 2 * sizeof(u_int16_t);
    699  1.13       cgd 		totlen -= 2 * sizeof(u_int16_t);
    700   1.9   mycroft 	}
    701   1.9   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    702   1.9   mycroft 	if (m == 0)
    703   1.9   mycroft 		return (0);
    704   1.9   mycroft 	m->m_pkthdr.rcvif = ifp;
    705   1.9   mycroft 	m->m_pkthdr.len = totlen;
    706   1.9   mycroft 	m->m_len = MHLEN;
    707   1.9   mycroft 
    708   1.9   mycroft 	while (totlen > 0) {
    709   1.9   mycroft 		if (top) {
    710   1.9   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
    711   1.9   mycroft 			if (m == 0) {
    712   1.9   mycroft 				m_freem(top);
    713   1.9   mycroft 				return (0);
    714   1.9   mycroft 			}
    715   1.9   mycroft 			m->m_len = MLEN;
    716   1.9   mycroft 		}
    717   1.9   mycroft 		len = min(totlen, epkt - cp);
    718   1.9   mycroft 		if (len >= MINCLSIZE) {
    719   1.9   mycroft 			MCLGET(m, M_DONTWAIT);
    720  1.19   mycroft 			if ((m->m_flags & M_EXT) == 0) {
    721  1.20   mycroft 				m_free(m);
    722  1.19   mycroft 				m_freem(top);
    723  1.19   mycroft 				return (0);
    724  1.19   mycroft 			}
    725  1.19   mycroft 			m->m_len = len = min(len, MCLBYTES);
    726   1.9   mycroft 		} else {
    727   1.9   mycroft 			/*
    728   1.9   mycroft 			 * Place initial small packet/header at end of mbuf.
    729   1.9   mycroft 			 */
    730   1.9   mycroft 			if (len < m->m_len) {
    731   1.9   mycroft 				if (top == 0 && len + max_linkhdr <= m->m_len)
    732   1.9   mycroft 					m->m_data += max_linkhdr;
    733   1.9   mycroft 				m->m_len = len;
    734   1.9   mycroft 			} else
    735   1.9   mycroft 				len = m->m_len;
    736   1.9   mycroft 		}
    737   1.9   mycroft 		if (copy)
    738  1.14  christos 			copy(cp, mtod(m, caddr_t), (size_t)len);
    739   1.9   mycroft 		else
    740  1.14  christos 			bcopy(cp, mtod(m, caddr_t), (size_t)len);
    741   1.9   mycroft 		cp += len;
    742   1.9   mycroft 		*mp = m;
    743   1.9   mycroft 		mp = &m->m_next;
    744   1.9   mycroft 		totlen -= len;
    745   1.9   mycroft 		if (cp == epkt)
    746   1.9   mycroft 			cp = buf;
    747   1.9   mycroft 	}
    748   1.9   mycroft 	return (top);
    749  1.18   thorpej }
    750  1.18   thorpej 
    751  1.18   thorpej /*
    752  1.18   thorpej  * Copy data from a buffer back into the indicated mbuf chain,
    753  1.18   thorpej  * starting "off" bytes from the beginning, extending the mbuf
    754  1.18   thorpej  * chain if necessary.
    755  1.18   thorpej  */
    756  1.18   thorpej void
    757  1.18   thorpej m_copyback(m0, off, len, cp)
    758  1.18   thorpej 	struct	mbuf *m0;
    759  1.18   thorpej 	register int off;
    760  1.18   thorpej 	register int len;
    761  1.18   thorpej 	caddr_t cp;
    762  1.18   thorpej {
    763  1.18   thorpej 	register int mlen;
    764  1.18   thorpej 	register struct mbuf *m = m0, *n;
    765  1.18   thorpej 	int totlen = 0;
    766  1.18   thorpej 
    767  1.18   thorpej 	if (m0 == 0)
    768  1.18   thorpej 		return;
    769  1.18   thorpej 	while (off > (mlen = m->m_len)) {
    770  1.18   thorpej 		off -= mlen;
    771  1.18   thorpej 		totlen += mlen;
    772  1.18   thorpej 		if (m->m_next == 0) {
    773  1.18   thorpej 			n = m_getclr(M_DONTWAIT, m->m_type);
    774  1.18   thorpej 			if (n == 0)
    775  1.18   thorpej 				goto out;
    776  1.18   thorpej 			n->m_len = min(MLEN, len + off);
    777  1.18   thorpej 			m->m_next = n;
    778  1.18   thorpej 		}
    779  1.18   thorpej 		m = m->m_next;
    780  1.18   thorpej 	}
    781  1.18   thorpej 	while (len > 0) {
    782  1.18   thorpej 		mlen = min (m->m_len - off, len);
    783  1.18   thorpej 		bcopy(cp, mtod(m, caddr_t) + off, (unsigned)mlen);
    784  1.18   thorpej 		cp += mlen;
    785  1.18   thorpej 		len -= mlen;
    786  1.18   thorpej 		mlen += off;
    787  1.18   thorpej 		off = 0;
    788  1.18   thorpej 		totlen += mlen;
    789  1.18   thorpej 		if (len == 0)
    790  1.18   thorpej 			break;
    791  1.18   thorpej 		if (m->m_next == 0) {
    792  1.18   thorpej 			n = m_get(M_DONTWAIT, m->m_type);
    793  1.18   thorpej 			if (n == 0)
    794  1.18   thorpej 				break;
    795  1.18   thorpej 			n->m_len = min(MLEN, len);
    796  1.18   thorpej 			m->m_next = n;
    797  1.18   thorpej 		}
    798  1.18   thorpej 		m = m->m_next;
    799  1.18   thorpej 	}
    800  1.18   thorpej out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
    801  1.18   thorpej 		m->m_pkthdr.len = totlen;
    802   1.1       cgd }
    803