Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.48
      1  1.48    itojun /*	$NetBSD: uipc_mbuf.c,v 1.48 2000/08/18 14:12:47 itojun Exp $	*/
      2  1.42   thorpej 
      3  1.42   thorpej /*-
      4  1.42   thorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.42   thorpej  * All rights reserved.
      6  1.42   thorpej  *
      7  1.42   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.42   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.42   thorpej  * NASA Ames Research Center.
     10  1.42   thorpej  *
     11  1.42   thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.42   thorpej  * modification, are permitted provided that the following conditions
     13  1.42   thorpej  * are met:
     14  1.42   thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.42   thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.42   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.42   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.42   thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.42   thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.42   thorpej  *    must display the following acknowledgement:
     21  1.42   thorpej  *	This product includes software developed by the NetBSD
     22  1.42   thorpej  *	Foundation, Inc. and its contributors.
     23  1.42   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.42   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.42   thorpej  *    from this software without specific prior written permission.
     26  1.42   thorpej  *
     27  1.42   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.42   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.42   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.42   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.42   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.42   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.42   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.42   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.42   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.42   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.42   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.42   thorpej  */
     39  1.10       cgd 
     40   1.1       cgd /*
     41   1.9   mycroft  * Copyright (c) 1982, 1986, 1988, 1991, 1993
     42   1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
     43   1.1       cgd  *
     44   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     45   1.1       cgd  * modification, are permitted provided that the following conditions
     46   1.1       cgd  * are met:
     47   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     48   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     49   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     50   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     51   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     52   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     53   1.1       cgd  *    must display the following acknowledgement:
     54   1.1       cgd  *	This product includes software developed by the University of
     55   1.1       cgd  *	California, Berkeley and its contributors.
     56   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     57   1.1       cgd  *    may be used to endorse or promote products derived from this software
     58   1.1       cgd  *    without specific prior written permission.
     59   1.1       cgd  *
     60   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70   1.1       cgd  * SUCH DAMAGE.
     71   1.1       cgd  *
     72  1.26      fvdl  *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
     73   1.1       cgd  */
     74  1.24       mrg 
     75   1.6   mycroft #include <sys/param.h>
     76   1.6   mycroft #include <sys/systm.h>
     77   1.6   mycroft #include <sys/proc.h>
     78   1.6   mycroft #include <sys/malloc.h>
     79   1.9   mycroft #include <sys/map.h>
     80   1.1       cgd #define MBTYPES
     81   1.6   mycroft #include <sys/mbuf.h>
     82   1.6   mycroft #include <sys/kernel.h>
     83   1.6   mycroft #include <sys/syslog.h>
     84   1.6   mycroft #include <sys/domain.h>
     85   1.6   mycroft #include <sys/protosw.h>
     86  1.28   thorpej #include <sys/pool.h>
     87  1.27      matt #include <sys/socket.h>
     88  1.27      matt #include <net/if.h>
     89  1.14  christos 
     90  1.23       mrg #include <uvm/uvm_extern.h>
     91  1.23       mrg 
     92  1.42   thorpej #include <sys/sysctl.h>
     93  1.42   thorpej 
     94  1.28   thorpej struct	pool mbpool;		/* mbuf pool */
     95  1.28   thorpej struct	pool mclpool;		/* mbuf cluster pool */
     96  1.28   thorpej 
     97  1.18   thorpej struct mbstat mbstat;
     98  1.18   thorpej int	max_linkhdr;
     99  1.18   thorpej int	max_protohdr;
    100  1.18   thorpej int	max_hdr;
    101  1.18   thorpej int	max_datalen;
    102  1.18   thorpej 
    103  1.28   thorpej void	*mclpool_alloc __P((unsigned long, int, int));
    104  1.28   thorpej void	mclpool_release __P((void *, unsigned long, int));
    105  1.44    itojun static struct mbuf *m_copym0 __P((struct mbuf *, int, int, int, int));
    106  1.28   thorpej 
    107  1.42   thorpej const char *mclpool_warnmsg =
    108  1.42   thorpej     "WARNING: mclpool limit reached; increase NMBCLUSTERS";
    109  1.42   thorpej 
    110  1.28   thorpej /*
    111  1.40   thorpej  * Initialize the mbuf allcator.
    112  1.28   thorpej  */
    113   1.4       jtc void
    114   1.1       cgd mbinit()
    115   1.1       cgd {
    116   1.1       cgd 
    117  1.42   thorpej 	pool_init(&mbpool, msize, 0, 0, 0, "mbpl", 0, NULL, NULL, 0);
    118  1.42   thorpej 	pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", 0, mclpool_alloc,
    119  1.28   thorpej 	    mclpool_release, 0);
    120  1.37   thorpej 
    121  1.37   thorpej 	/*
    122  1.39   thorpej 	 * Set the hard limit on the mclpool to the number of
    123  1.39   thorpej 	 * mbuf clusters the kernel is to support.  Log the limit
    124  1.39   thorpej 	 * reached message max once a minute.
    125  1.39   thorpej 	 */
    126  1.42   thorpej 	pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    127  1.42   thorpej 
    128  1.39   thorpej 	/*
    129  1.42   thorpej 	 * Set a low water mark for both mbufs and clusters.  This should
    130  1.42   thorpej 	 * help ensure that they can be allocated in a memory starvation
    131  1.42   thorpej 	 * situation.  This is important for e.g. diskless systems which
    132  1.42   thorpej 	 * must allocate mbufs in order for the pagedaemon to clean pages.
    133  1.37   thorpej 	 */
    134  1.42   thorpej 	pool_setlowat(&mbpool, mblowat);
    135  1.42   thorpej 	pool_setlowat(&mclpool, mcllowat);
    136  1.42   thorpej }
    137  1.42   thorpej 
    138  1.42   thorpej int
    139  1.42   thorpej sysctl_dombuf(name, namelen, oldp, oldlenp, newp, newlen)
    140  1.42   thorpej 	int *name;
    141  1.42   thorpej 	u_int namelen;
    142  1.42   thorpej 	void *oldp;
    143  1.42   thorpej 	size_t *oldlenp;
    144  1.42   thorpej 	void *newp;
    145  1.42   thorpej 	size_t newlen;
    146  1.42   thorpej {
    147  1.42   thorpej 	int error, newval;
    148  1.42   thorpej 
    149  1.42   thorpej 	/* All sysctl names at this level are terminal. */
    150  1.42   thorpej 	if (namelen != 1)
    151  1.42   thorpej 		return (ENOTDIR);		/* overloaded */
    152  1.42   thorpej 
    153  1.42   thorpej 	switch (name[0]) {
    154  1.42   thorpej 	case MBUF_MSIZE:
    155  1.42   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, msize));
    156  1.42   thorpej 	case MBUF_MCLBYTES:
    157  1.42   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, mclbytes));
    158  1.42   thorpej 	case MBUF_NMBCLUSTERS:
    159  1.42   thorpej 		/*
    160  1.42   thorpej 		 * If we have direct-mapped pool pages, we can adjust this
    161  1.42   thorpej 		 * number on the fly.  If not, we're limited by the size
    162  1.42   thorpej 		 * of mb_map, and cannot change this value.
    163  1.42   thorpej 		 *
    164  1.42   thorpej 		 * Note: we only allow the value to be increased, never
    165  1.42   thorpej 		 * decreased.
    166  1.42   thorpej 		 */
    167  1.42   thorpej 		if (mb_map == NULL) {
    168  1.42   thorpej 			newval = nmbclusters;
    169  1.42   thorpej 			error = sysctl_int(oldp, oldlenp, newp, newlen,
    170  1.42   thorpej 			    &newval);
    171  1.42   thorpej 			if (error != 0)
    172  1.42   thorpej 				return (error);
    173  1.42   thorpej 			if (newp != NULL) {
    174  1.42   thorpej 				if (newval >= nmbclusters) {
    175  1.42   thorpej 					nmbclusters = newval;
    176  1.42   thorpej 					pool_sethardlimit(&mclpool,
    177  1.42   thorpej 					    nmbclusters, mclpool_warnmsg, 60);
    178  1.42   thorpej 				} else
    179  1.42   thorpej 					error = EINVAL;
    180  1.42   thorpej 			}
    181  1.42   thorpej 			return (error);
    182  1.42   thorpej 		} else
    183  1.42   thorpej 			return (sysctl_rdint(oldp, oldlenp, newp, nmbclusters));
    184  1.42   thorpej 	case MBUF_MBLOWAT:
    185  1.42   thorpej 	case MBUF_MCLLOWAT:
    186  1.42   thorpej 		/* New value must be >= 0. */
    187  1.42   thorpej 		newval = (name[0] == MBUF_MBLOWAT) ? mblowat : mcllowat;
    188  1.42   thorpej 		error = sysctl_int(oldp, oldlenp, newp, newlen, &newval);
    189  1.42   thorpej 		if (error != 0)
    190  1.42   thorpej 			return (error);
    191  1.42   thorpej 		if (newp != NULL) {
    192  1.42   thorpej 			if (newval >= 0) {
    193  1.42   thorpej 				if (name[0] == MBUF_MBLOWAT) {
    194  1.42   thorpej 					mblowat = newval;
    195  1.42   thorpej 					pool_setlowat(&mbpool, newval);
    196  1.42   thorpej 				} else {
    197  1.42   thorpej 					mcllowat = newval;
    198  1.42   thorpej 					pool_setlowat(&mclpool, newval);
    199  1.42   thorpej 				}
    200  1.42   thorpej 			} else
    201  1.42   thorpej 				error = EINVAL;
    202  1.42   thorpej 		}
    203  1.42   thorpej 		return (error);
    204  1.42   thorpej 	default:
    205  1.42   thorpej 		return (EOPNOTSUPP);
    206  1.42   thorpej 	}
    207  1.42   thorpej 	/* NOTREACHED */
    208  1.28   thorpej }
    209  1.28   thorpej 
    210  1.28   thorpej void *
    211  1.28   thorpej mclpool_alloc(sz, flags, mtype)
    212  1.28   thorpej 	unsigned long sz;
    213  1.28   thorpej 	int flags;
    214  1.28   thorpej 	int mtype;
    215  1.28   thorpej {
    216  1.32   thorpej 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
    217  1.28   thorpej 
    218  1.39   thorpej 	return ((void *)uvm_km_alloc_poolpage1(mb_map, uvmexp.mb_object,
    219  1.39   thorpej 	    waitok));
    220   1.1       cgd }
    221   1.1       cgd 
    222  1.28   thorpej void
    223  1.28   thorpej mclpool_release(v, sz, mtype)
    224  1.28   thorpej 	void *v;
    225  1.28   thorpej 	unsigned long sz;
    226  1.28   thorpej 	int mtype;
    227   1.1       cgd {
    228   1.1       cgd 
    229  1.31   thorpej 	uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
    230   1.1       cgd }
    231   1.1       cgd 
    232   1.1       cgd /*
    233   1.1       cgd  * When MGET failes, ask protocols to free space when short of memory,
    234   1.1       cgd  * then re-attempt to allocate an mbuf.
    235   1.1       cgd  */
    236   1.1       cgd struct mbuf *
    237   1.1       cgd m_retry(i, t)
    238   1.1       cgd 	int i, t;
    239   1.1       cgd {
    240  1.27      matt 	struct mbuf *m;
    241   1.1       cgd 
    242  1.29   thorpej 	m_reclaim(i);
    243   1.1       cgd #define m_retry(i, t)	(struct mbuf *)0
    244   1.1       cgd 	MGET(m, i, t);
    245   1.1       cgd #undef m_retry
    246  1.18   thorpej 	if (m != NULL)
    247  1.18   thorpej 		mbstat.m_wait++;
    248  1.18   thorpej 	else
    249  1.18   thorpej 		mbstat.m_drops++;
    250   1.1       cgd 	return (m);
    251   1.1       cgd }
    252   1.1       cgd 
    253   1.1       cgd /*
    254   1.1       cgd  * As above; retry an MGETHDR.
    255   1.1       cgd  */
    256   1.1       cgd struct mbuf *
    257   1.1       cgd m_retryhdr(i, t)
    258   1.1       cgd 	int i, t;
    259   1.1       cgd {
    260  1.27      matt 	struct mbuf *m;
    261   1.1       cgd 
    262  1.29   thorpej 	m_reclaim(i);
    263   1.1       cgd #define m_retryhdr(i, t) (struct mbuf *)0
    264   1.1       cgd 	MGETHDR(m, i, t);
    265   1.1       cgd #undef m_retryhdr
    266  1.18   thorpej 	if (m != NULL)
    267  1.18   thorpej 		mbstat.m_wait++;
    268  1.18   thorpej 	else
    269  1.18   thorpej 		mbstat.m_drops++;
    270   1.1       cgd 	return (m);
    271   1.1       cgd }
    272   1.1       cgd 
    273  1.14  christos void
    274  1.29   thorpej m_reclaim(how)
    275  1.29   thorpej 	int how;
    276   1.1       cgd {
    277  1.27      matt 	struct domain *dp;
    278  1.27      matt 	struct protosw *pr;
    279  1.27      matt 	struct ifnet *ifp;
    280   1.1       cgd 	int s = splimp();
    281   1.1       cgd 
    282  1.33   thorpej 	for (dp = domains; dp; dp = dp->dom_next)
    283  1.33   thorpej 		for (pr = dp->dom_protosw;
    284  1.33   thorpej 		     pr < dp->dom_protoswNPROTOSW; pr++)
    285  1.33   thorpej 			if (pr->pr_drain)
    286  1.33   thorpej 				(*pr->pr_drain)();
    287  1.27      matt 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
    288  1.27      matt 		if (ifp->if_drain)
    289  1.27      matt 			(*ifp->if_drain)(ifp);
    290   1.1       cgd 	splx(s);
    291   1.1       cgd 	mbstat.m_drain++;
    292   1.1       cgd }
    293   1.1       cgd 
    294   1.1       cgd /*
    295   1.1       cgd  * Space allocation routines.
    296   1.1       cgd  * These are also available as macros
    297   1.1       cgd  * for critical paths.
    298   1.1       cgd  */
    299   1.1       cgd struct mbuf *
    300   1.5       cgd m_get(nowait, type)
    301   1.5       cgd 	int nowait, type;
    302   1.1       cgd {
    303  1.27      matt 	struct mbuf *m;
    304   1.1       cgd 
    305   1.5       cgd 	MGET(m, nowait, type);
    306   1.1       cgd 	return (m);
    307   1.1       cgd }
    308   1.1       cgd 
    309   1.1       cgd struct mbuf *
    310   1.5       cgd m_gethdr(nowait, type)
    311   1.5       cgd 	int nowait, type;
    312   1.1       cgd {
    313  1.27      matt 	struct mbuf *m;
    314   1.1       cgd 
    315   1.5       cgd 	MGETHDR(m, nowait, type);
    316   1.1       cgd 	return (m);
    317   1.1       cgd }
    318   1.1       cgd 
    319   1.1       cgd struct mbuf *
    320   1.5       cgd m_getclr(nowait, type)
    321   1.5       cgd 	int nowait, type;
    322   1.1       cgd {
    323  1.27      matt 	struct mbuf *m;
    324   1.1       cgd 
    325   1.5       cgd 	MGET(m, nowait, type);
    326   1.1       cgd 	if (m == 0)
    327   1.1       cgd 		return (0);
    328  1.30     perry 	memset(mtod(m, caddr_t), 0, MLEN);
    329   1.1       cgd 	return (m);
    330   1.1       cgd }
    331   1.1       cgd 
    332   1.1       cgd struct mbuf *
    333   1.1       cgd m_free(m)
    334   1.1       cgd 	struct mbuf *m;
    335   1.1       cgd {
    336  1.27      matt 	struct mbuf *n;
    337   1.1       cgd 
    338   1.1       cgd 	MFREE(m, n);
    339   1.1       cgd 	return (n);
    340   1.1       cgd }
    341   1.1       cgd 
    342   1.9   mycroft void
    343   1.1       cgd m_freem(m)
    344  1.27      matt 	struct mbuf *m;
    345   1.1       cgd {
    346  1.27      matt 	struct mbuf *n;
    347   1.1       cgd 
    348   1.1       cgd 	if (m == NULL)
    349   1.1       cgd 		return;
    350  1.45    itojun 	if ((m->m_flags & M_PKTHDR) != 0 && m->m_pkthdr.aux) {
    351  1.45    itojun 		m_freem(m->m_pkthdr.aux);
    352  1.45    itojun 		m->m_pkthdr.aux = NULL;
    353  1.45    itojun 	}
    354   1.1       cgd 	do {
    355   1.1       cgd 		MFREE(m, n);
    356  1.18   thorpej 		m = n;
    357  1.18   thorpej 	} while (m);
    358   1.1       cgd }
    359   1.1       cgd 
    360   1.1       cgd /*
    361   1.1       cgd  * Mbuffer utility routines.
    362   1.1       cgd  */
    363   1.1       cgd 
    364   1.1       cgd /*
    365   1.1       cgd  * Lesser-used path for M_PREPEND:
    366   1.1       cgd  * allocate new mbuf to prepend to chain,
    367   1.1       cgd  * copy junk along.
    368   1.1       cgd  */
    369   1.1       cgd struct mbuf *
    370   1.9   mycroft m_prepend(m, len, how)
    371  1.27      matt 	struct mbuf *m;
    372   1.9   mycroft 	int len, how;
    373   1.1       cgd {
    374   1.1       cgd 	struct mbuf *mn;
    375   1.1       cgd 
    376   1.9   mycroft 	MGET(mn, how, m->m_type);
    377   1.1       cgd 	if (mn == (struct mbuf *)NULL) {
    378   1.1       cgd 		m_freem(m);
    379   1.1       cgd 		return ((struct mbuf *)NULL);
    380   1.1       cgd 	}
    381   1.1       cgd 	if (m->m_flags & M_PKTHDR) {
    382   1.1       cgd 		M_COPY_PKTHDR(mn, m);
    383   1.1       cgd 		m->m_flags &= ~M_PKTHDR;
    384   1.1       cgd 	}
    385   1.1       cgd 	mn->m_next = m;
    386   1.1       cgd 	m = mn;
    387   1.1       cgd 	if (len < MHLEN)
    388   1.1       cgd 		MH_ALIGN(m, len);
    389   1.1       cgd 	m->m_len = len;
    390   1.1       cgd 	return (m);
    391   1.1       cgd }
    392   1.1       cgd 
    393   1.1       cgd /*
    394   1.1       cgd  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    395   1.1       cgd  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    396   1.1       cgd  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    397   1.1       cgd  */
    398   1.1       cgd int MCFail;
    399   1.1       cgd 
    400   1.1       cgd struct mbuf *
    401   1.1       cgd m_copym(m, off0, len, wait)
    402  1.27      matt 	struct mbuf *m;
    403   1.1       cgd 	int off0, wait;
    404  1.27      matt 	int len;
    405   1.1       cgd {
    406  1.44    itojun 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    407  1.44    itojun }
    408  1.44    itojun 
    409  1.44    itojun struct mbuf *
    410  1.44    itojun m_dup(m, off0, len, wait)
    411  1.44    itojun 	struct mbuf *m;
    412  1.44    itojun 	int off0, wait;
    413  1.44    itojun 	int len;
    414  1.44    itojun {
    415  1.44    itojun 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    416  1.44    itojun }
    417  1.44    itojun 
    418  1.44    itojun static struct mbuf *
    419  1.44    itojun m_copym0(m, off0, len, wait, deep)
    420  1.44    itojun 	struct mbuf *m;
    421  1.44    itojun 	int off0, wait;
    422  1.44    itojun 	int len;
    423  1.44    itojun 	int deep;	/* deep copy */
    424  1.44    itojun {
    425  1.27      matt 	struct mbuf *n, **np;
    426  1.27      matt 	int off = off0;
    427   1.1       cgd 	struct mbuf *top;
    428   1.1       cgd 	int copyhdr = 0;
    429   1.1       cgd 
    430   1.1       cgd 	if (off < 0 || len < 0)
    431  1.43   thorpej 		panic("m_copym: off %d, len %d", off, len);
    432   1.1       cgd 	if (off == 0 && m->m_flags & M_PKTHDR)
    433   1.1       cgd 		copyhdr = 1;
    434   1.1       cgd 	while (off > 0) {
    435   1.1       cgd 		if (m == 0)
    436  1.43   thorpej 			panic("m_copym: m == 0");
    437   1.1       cgd 		if (off < m->m_len)
    438   1.1       cgd 			break;
    439   1.1       cgd 		off -= m->m_len;
    440   1.1       cgd 		m = m->m_next;
    441   1.1       cgd 	}
    442   1.1       cgd 	np = &top;
    443   1.1       cgd 	top = 0;
    444   1.1       cgd 	while (len > 0) {
    445   1.1       cgd 		if (m == 0) {
    446   1.1       cgd 			if (len != M_COPYALL)
    447  1.43   thorpej 				panic("m_copym: m == 0 and not COPYALL");
    448   1.1       cgd 			break;
    449   1.1       cgd 		}
    450   1.1       cgd 		MGET(n, wait, m->m_type);
    451   1.1       cgd 		*np = n;
    452   1.1       cgd 		if (n == 0)
    453   1.1       cgd 			goto nospace;
    454   1.1       cgd 		if (copyhdr) {
    455   1.1       cgd 			M_COPY_PKTHDR(n, m);
    456   1.1       cgd 			if (len == M_COPYALL)
    457   1.1       cgd 				n->m_pkthdr.len -= off0;
    458   1.1       cgd 			else
    459   1.1       cgd 				n->m_pkthdr.len = len;
    460   1.1       cgd 			copyhdr = 0;
    461   1.1       cgd 		}
    462   1.9   mycroft 		n->m_len = min(len, m->m_len - off);
    463   1.1       cgd 		if (m->m_flags & M_EXT) {
    464  1.44    itojun 			if (!deep) {
    465  1.44    itojun 				n->m_data = m->m_data + off;
    466  1.44    itojun 				n->m_ext = m->m_ext;
    467  1.44    itojun 				MCLADDREFERENCE(m, n);
    468  1.44    itojun 			} else {
    469  1.48    itojun 				/*
    470  1.48    itojun 				 * XXX the code falsely assumes that, if mbufs
    471  1.48    itojun 				 * are with M_EXT, the cluster region was
    472  1.48    itojun 				 * allocated with MCLGET() and is with the size
    473  1.48    itojun 				 * of MCLBYTES.
    474  1.48    itojun 				 * this is not the case.  for counter example,
    475  1.48    itojun 				 * see MEXTMALLOC() and MEXTADD().
    476  1.48    itojun 				 */
    477  1.44    itojun 				MCLGET(n, wait);
    478  1.44    itojun 				memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
    479  1.44    itojun 				    (unsigned)n->m_len);
    480  1.44    itojun 			}
    481   1.1       cgd 		} else
    482  1.30     perry 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
    483   1.1       cgd 			    (unsigned)n->m_len);
    484   1.1       cgd 		if (len != M_COPYALL)
    485   1.1       cgd 			len -= n->m_len;
    486   1.1       cgd 		off = 0;
    487   1.1       cgd 		m = m->m_next;
    488   1.1       cgd 		np = &n->m_next;
    489   1.1       cgd 	}
    490   1.1       cgd 	if (top == 0)
    491   1.1       cgd 		MCFail++;
    492   1.1       cgd 	return (top);
    493   1.1       cgd nospace:
    494   1.1       cgd 	m_freem(top);
    495   1.1       cgd 	MCFail++;
    496   1.1       cgd 	return (0);
    497   1.1       cgd }
    498   1.1       cgd 
    499   1.1       cgd /*
    500  1.18   thorpej  * Copy an entire packet, including header (which must be present).
    501  1.18   thorpej  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    502  1.18   thorpej  */
    503  1.18   thorpej struct mbuf *
    504  1.18   thorpej m_copypacket(m, how)
    505  1.18   thorpej 	struct mbuf *m;
    506  1.18   thorpej 	int how;
    507  1.18   thorpej {
    508  1.18   thorpej 	struct mbuf *top, *n, *o;
    509  1.18   thorpej 
    510  1.18   thorpej 	MGET(n, how, m->m_type);
    511  1.18   thorpej 	top = n;
    512  1.18   thorpej 	if (!n)
    513  1.18   thorpej 		goto nospace;
    514  1.18   thorpej 
    515  1.18   thorpej 	M_COPY_PKTHDR(n, m);
    516  1.18   thorpej 	n->m_len = m->m_len;
    517  1.18   thorpej 	if (m->m_flags & M_EXT) {
    518  1.18   thorpej 		n->m_data = m->m_data;
    519  1.18   thorpej 		n->m_ext = m->m_ext;
    520  1.18   thorpej 		MCLADDREFERENCE(m, n);
    521  1.18   thorpej 	} else {
    522  1.30     perry 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    523  1.18   thorpej 	}
    524  1.18   thorpej 
    525  1.18   thorpej 	m = m->m_next;
    526  1.18   thorpej 	while (m) {
    527  1.18   thorpej 		MGET(o, how, m->m_type);
    528  1.18   thorpej 		if (!o)
    529  1.18   thorpej 			goto nospace;
    530  1.18   thorpej 
    531  1.18   thorpej 		n->m_next = o;
    532  1.18   thorpej 		n = n->m_next;
    533  1.18   thorpej 
    534  1.18   thorpej 		n->m_len = m->m_len;
    535  1.18   thorpej 		if (m->m_flags & M_EXT) {
    536  1.18   thorpej 			n->m_data = m->m_data;
    537  1.18   thorpej 			n->m_ext = m->m_ext;
    538  1.18   thorpej 			MCLADDREFERENCE(m, n);
    539  1.18   thorpej 		} else {
    540  1.30     perry 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    541  1.18   thorpej 		}
    542  1.18   thorpej 
    543  1.18   thorpej 		m = m->m_next;
    544  1.18   thorpej 	}
    545  1.18   thorpej 	return top;
    546  1.18   thorpej nospace:
    547  1.18   thorpej 	m_freem(top);
    548  1.18   thorpej 	MCFail++;
    549  1.18   thorpej 	return 0;
    550  1.18   thorpej }
    551  1.18   thorpej 
    552  1.18   thorpej /*
    553   1.1       cgd  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    554   1.1       cgd  * continuing for "len" bytes, into the indicated buffer.
    555   1.1       cgd  */
    556  1.14  christos void
    557   1.1       cgd m_copydata(m, off, len, cp)
    558  1.27      matt 	struct mbuf *m;
    559  1.27      matt 	int off;
    560  1.27      matt 	int len;
    561   1.1       cgd 	caddr_t cp;
    562   1.1       cgd {
    563  1.27      matt 	unsigned count;
    564   1.1       cgd 
    565   1.1       cgd 	if (off < 0 || len < 0)
    566   1.1       cgd 		panic("m_copydata");
    567   1.1       cgd 	while (off > 0) {
    568   1.1       cgd 		if (m == 0)
    569   1.1       cgd 			panic("m_copydata");
    570   1.1       cgd 		if (off < m->m_len)
    571   1.1       cgd 			break;
    572   1.1       cgd 		off -= m->m_len;
    573   1.1       cgd 		m = m->m_next;
    574   1.1       cgd 	}
    575   1.1       cgd 	while (len > 0) {
    576   1.1       cgd 		if (m == 0)
    577   1.1       cgd 			panic("m_copydata");
    578   1.9   mycroft 		count = min(m->m_len - off, len);
    579  1.30     perry 		memcpy(cp, mtod(m, caddr_t) + off, count);
    580   1.1       cgd 		len -= count;
    581   1.1       cgd 		cp += count;
    582   1.1       cgd 		off = 0;
    583   1.1       cgd 		m = m->m_next;
    584   1.1       cgd 	}
    585   1.1       cgd }
    586   1.1       cgd 
    587   1.1       cgd /*
    588   1.1       cgd  * Concatenate mbuf chain n to m.
    589   1.1       cgd  * Both chains must be of the same type (e.g. MT_DATA).
    590   1.1       cgd  * Any m_pkthdr is not updated.
    591   1.1       cgd  */
    592  1.14  christos void
    593   1.1       cgd m_cat(m, n)
    594  1.27      matt 	struct mbuf *m, *n;
    595   1.1       cgd {
    596   1.1       cgd 	while (m->m_next)
    597   1.1       cgd 		m = m->m_next;
    598   1.1       cgd 	while (n) {
    599   1.1       cgd 		if (m->m_flags & M_EXT ||
    600   1.1       cgd 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
    601   1.1       cgd 			/* just join the two chains */
    602   1.1       cgd 			m->m_next = n;
    603   1.1       cgd 			return;
    604   1.1       cgd 		}
    605   1.1       cgd 		/* splat the data from one into the other */
    606  1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    607   1.1       cgd 		    (u_int)n->m_len);
    608   1.1       cgd 		m->m_len += n->m_len;
    609   1.1       cgd 		n = m_free(n);
    610   1.1       cgd 	}
    611   1.1       cgd }
    612   1.1       cgd 
    613  1.11   mycroft void
    614   1.1       cgd m_adj(mp, req_len)
    615   1.1       cgd 	struct mbuf *mp;
    616   1.8   deraadt 	int req_len;
    617   1.1       cgd {
    618  1.27      matt 	int len = req_len;
    619  1.27      matt 	struct mbuf *m;
    620  1.27      matt 	int count;
    621   1.1       cgd 
    622   1.1       cgd 	if ((m = mp) == NULL)
    623   1.1       cgd 		return;
    624   1.1       cgd 	if (len >= 0) {
    625   1.1       cgd 		/*
    626   1.1       cgd 		 * Trim from head.
    627   1.1       cgd 		 */
    628   1.1       cgd 		while (m != NULL && len > 0) {
    629   1.1       cgd 			if (m->m_len <= len) {
    630   1.1       cgd 				len -= m->m_len;
    631   1.1       cgd 				m->m_len = 0;
    632   1.1       cgd 				m = m->m_next;
    633   1.1       cgd 			} else {
    634   1.1       cgd 				m->m_len -= len;
    635   1.1       cgd 				m->m_data += len;
    636   1.1       cgd 				len = 0;
    637   1.1       cgd 			}
    638   1.1       cgd 		}
    639   1.1       cgd 		m = mp;
    640   1.1       cgd 		if (mp->m_flags & M_PKTHDR)
    641   1.1       cgd 			m->m_pkthdr.len -= (req_len - len);
    642   1.1       cgd 	} else {
    643   1.1       cgd 		/*
    644   1.1       cgd 		 * Trim from tail.  Scan the mbuf chain,
    645   1.1       cgd 		 * calculating its length and finding the last mbuf.
    646   1.1       cgd 		 * If the adjustment only affects this mbuf, then just
    647   1.1       cgd 		 * adjust and return.  Otherwise, rescan and truncate
    648   1.1       cgd 		 * after the remaining size.
    649   1.1       cgd 		 */
    650   1.1       cgd 		len = -len;
    651   1.1       cgd 		count = 0;
    652   1.1       cgd 		for (;;) {
    653   1.1       cgd 			count += m->m_len;
    654   1.1       cgd 			if (m->m_next == (struct mbuf *)0)
    655   1.1       cgd 				break;
    656   1.1       cgd 			m = m->m_next;
    657   1.1       cgd 		}
    658   1.1       cgd 		if (m->m_len >= len) {
    659   1.1       cgd 			m->m_len -= len;
    660   1.8   deraadt 			if (mp->m_flags & M_PKTHDR)
    661   1.8   deraadt 				mp->m_pkthdr.len -= len;
    662   1.1       cgd 			return;
    663   1.1       cgd 		}
    664   1.1       cgd 		count -= len;
    665   1.1       cgd 		if (count < 0)
    666   1.1       cgd 			count = 0;
    667   1.1       cgd 		/*
    668   1.1       cgd 		 * Correct length for chain is "count".
    669   1.1       cgd 		 * Find the mbuf with last data, adjust its length,
    670   1.1       cgd 		 * and toss data from remaining mbufs on chain.
    671   1.1       cgd 		 */
    672   1.1       cgd 		m = mp;
    673   1.1       cgd 		if (m->m_flags & M_PKTHDR)
    674   1.1       cgd 			m->m_pkthdr.len = count;
    675   1.1       cgd 		for (; m; m = m->m_next) {
    676   1.1       cgd 			if (m->m_len >= count) {
    677   1.1       cgd 				m->m_len = count;
    678   1.1       cgd 				break;
    679   1.1       cgd 			}
    680   1.1       cgd 			count -= m->m_len;
    681   1.1       cgd 		}
    682  1.18   thorpej 		while (m->m_next)
    683  1.18   thorpej 			(m = m->m_next) ->m_len = 0;
    684   1.1       cgd 	}
    685   1.1       cgd }
    686   1.1       cgd 
    687   1.1       cgd /*
    688   1.1       cgd  * Rearange an mbuf chain so that len bytes are contiguous
    689   1.1       cgd  * and in the data area of an mbuf (so that mtod and dtom
    690   1.1       cgd  * will work for a structure of size len).  Returns the resulting
    691   1.1       cgd  * mbuf chain on success, frees it and returns null on failure.
    692   1.1       cgd  * If there is room, it will add up to max_protohdr-len extra bytes to the
    693   1.1       cgd  * contiguous region in an attempt to avoid being called next time.
    694   1.1       cgd  */
    695   1.1       cgd int MPFail;
    696   1.1       cgd 
    697   1.1       cgd struct mbuf *
    698   1.1       cgd m_pullup(n, len)
    699  1.27      matt 	struct mbuf *n;
    700   1.1       cgd 	int len;
    701   1.1       cgd {
    702  1.27      matt 	struct mbuf *m;
    703  1.27      matt 	int count;
    704   1.1       cgd 	int space;
    705   1.1       cgd 
    706   1.1       cgd 	/*
    707   1.1       cgd 	 * If first mbuf has no cluster, and has room for len bytes
    708   1.1       cgd 	 * without shifting current data, pullup into it,
    709   1.1       cgd 	 * otherwise allocate a new mbuf to prepend to the chain.
    710   1.1       cgd 	 */
    711   1.1       cgd 	if ((n->m_flags & M_EXT) == 0 &&
    712   1.1       cgd 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    713   1.1       cgd 		if (n->m_len >= len)
    714   1.1       cgd 			return (n);
    715   1.1       cgd 		m = n;
    716   1.1       cgd 		n = n->m_next;
    717   1.1       cgd 		len -= m->m_len;
    718   1.1       cgd 	} else {
    719   1.1       cgd 		if (len > MHLEN)
    720   1.1       cgd 			goto bad;
    721   1.1       cgd 		MGET(m, M_DONTWAIT, n->m_type);
    722   1.1       cgd 		if (m == 0)
    723   1.1       cgd 			goto bad;
    724   1.1       cgd 		m->m_len = 0;
    725   1.1       cgd 		if (n->m_flags & M_PKTHDR) {
    726   1.1       cgd 			M_COPY_PKTHDR(m, n);
    727   1.1       cgd 			n->m_flags &= ~M_PKTHDR;
    728   1.1       cgd 		}
    729   1.1       cgd 	}
    730   1.1       cgd 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    731   1.1       cgd 	do {
    732   1.1       cgd 		count = min(min(max(len, max_protohdr), space), n->m_len);
    733  1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    734   1.1       cgd 		  (unsigned)count);
    735   1.1       cgd 		len -= count;
    736   1.1       cgd 		m->m_len += count;
    737   1.1       cgd 		n->m_len -= count;
    738   1.1       cgd 		space -= count;
    739   1.1       cgd 		if (n->m_len)
    740   1.1       cgd 			n->m_data += count;
    741   1.1       cgd 		else
    742   1.1       cgd 			n = m_free(n);
    743   1.1       cgd 	} while (len > 0 && n);
    744   1.1       cgd 	if (len > 0) {
    745   1.1       cgd 		(void) m_free(m);
    746   1.1       cgd 		goto bad;
    747   1.1       cgd 	}
    748   1.1       cgd 	m->m_next = n;
    749   1.1       cgd 	return (m);
    750   1.1       cgd bad:
    751   1.1       cgd 	m_freem(n);
    752   1.1       cgd 	MPFail++;
    753   1.1       cgd 	return (0);
    754   1.9   mycroft }
    755   1.9   mycroft 
    756   1.9   mycroft /*
    757   1.9   mycroft  * Partition an mbuf chain in two pieces, returning the tail --
    758   1.9   mycroft  * all but the first len0 bytes.  In case of failure, it returns NULL and
    759   1.9   mycroft  * attempts to restore the chain to its original state.
    760   1.9   mycroft  */
    761   1.9   mycroft struct mbuf *
    762   1.9   mycroft m_split(m0, len0, wait)
    763  1.27      matt 	struct mbuf *m0;
    764   1.9   mycroft 	int len0, wait;
    765   1.9   mycroft {
    766  1.27      matt 	struct mbuf *m, *n;
    767  1.22   thorpej 	unsigned len = len0, remain, len_save;
    768   1.9   mycroft 
    769   1.9   mycroft 	for (m = m0; m && len > m->m_len; m = m->m_next)
    770   1.9   mycroft 		len -= m->m_len;
    771   1.9   mycroft 	if (m == 0)
    772   1.9   mycroft 		return (0);
    773   1.9   mycroft 	remain = m->m_len - len;
    774   1.9   mycroft 	if (m0->m_flags & M_PKTHDR) {
    775   1.9   mycroft 		MGETHDR(n, wait, m0->m_type);
    776   1.9   mycroft 		if (n == 0)
    777   1.9   mycroft 			return (0);
    778   1.9   mycroft 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    779   1.9   mycroft 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    780  1.22   thorpej 		len_save = m0->m_pkthdr.len;
    781   1.9   mycroft 		m0->m_pkthdr.len = len0;
    782   1.9   mycroft 		if (m->m_flags & M_EXT)
    783   1.9   mycroft 			goto extpacket;
    784   1.9   mycroft 		if (remain > MHLEN) {
    785   1.9   mycroft 			/* m can't be the lead packet */
    786   1.9   mycroft 			MH_ALIGN(n, 0);
    787   1.9   mycroft 			n->m_next = m_split(m, len, wait);
    788   1.9   mycroft 			if (n->m_next == 0) {
    789   1.9   mycroft 				(void) m_free(n);
    790  1.22   thorpej 				m0->m_pkthdr.len = len_save;
    791   1.9   mycroft 				return (0);
    792   1.9   mycroft 			} else
    793   1.9   mycroft 				return (n);
    794   1.9   mycroft 		} else
    795   1.9   mycroft 			MH_ALIGN(n, remain);
    796   1.9   mycroft 	} else if (remain == 0) {
    797   1.9   mycroft 		n = m->m_next;
    798   1.9   mycroft 		m->m_next = 0;
    799   1.9   mycroft 		return (n);
    800   1.9   mycroft 	} else {
    801   1.9   mycroft 		MGET(n, wait, m->m_type);
    802   1.9   mycroft 		if (n == 0)
    803   1.9   mycroft 			return (0);
    804   1.9   mycroft 		M_ALIGN(n, remain);
    805   1.9   mycroft 	}
    806   1.9   mycroft extpacket:
    807   1.9   mycroft 	if (m->m_flags & M_EXT) {
    808   1.9   mycroft 		n->m_ext = m->m_ext;
    809  1.18   thorpej 		MCLADDREFERENCE(m, n);
    810   1.9   mycroft 		n->m_data = m->m_data + len;
    811   1.9   mycroft 	} else {
    812  1.30     perry 		memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
    813   1.9   mycroft 	}
    814   1.9   mycroft 	n->m_len = remain;
    815   1.9   mycroft 	m->m_len = len;
    816   1.9   mycroft 	n->m_next = m->m_next;
    817   1.9   mycroft 	m->m_next = 0;
    818   1.9   mycroft 	return (n);
    819   1.9   mycroft }
    820   1.9   mycroft /*
    821   1.9   mycroft  * Routine to copy from device local memory into mbufs.
    822   1.9   mycroft  */
    823   1.9   mycroft struct mbuf *
    824   1.9   mycroft m_devget(buf, totlen, off0, ifp, copy)
    825   1.9   mycroft 	char *buf;
    826   1.9   mycroft 	int totlen, off0;
    827   1.9   mycroft 	struct ifnet *ifp;
    828  1.18   thorpej 	void (*copy) __P((const void *from, void *to, size_t len));
    829   1.9   mycroft {
    830  1.27      matt 	struct mbuf *m;
    831   1.9   mycroft 	struct mbuf *top = 0, **mp = &top;
    832  1.27      matt 	int off = off0, len;
    833  1.27      matt 	char *cp;
    834   1.9   mycroft 	char *epkt;
    835   1.9   mycroft 
    836   1.9   mycroft 	cp = buf;
    837   1.9   mycroft 	epkt = cp + totlen;
    838   1.9   mycroft 	if (off) {
    839  1.13       cgd 		/*
    840  1.13       cgd 		 * If 'off' is non-zero, packet is trailer-encapsulated,
    841  1.13       cgd 		 * so we have to skip the type and length fields.
    842  1.13       cgd 		 */
    843  1.13       cgd 		cp += off + 2 * sizeof(u_int16_t);
    844  1.13       cgd 		totlen -= 2 * sizeof(u_int16_t);
    845   1.9   mycroft 	}
    846   1.9   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    847   1.9   mycroft 	if (m == 0)
    848   1.9   mycroft 		return (0);
    849   1.9   mycroft 	m->m_pkthdr.rcvif = ifp;
    850   1.9   mycroft 	m->m_pkthdr.len = totlen;
    851   1.9   mycroft 	m->m_len = MHLEN;
    852   1.9   mycroft 
    853   1.9   mycroft 	while (totlen > 0) {
    854   1.9   mycroft 		if (top) {
    855   1.9   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
    856   1.9   mycroft 			if (m == 0) {
    857   1.9   mycroft 				m_freem(top);
    858   1.9   mycroft 				return (0);
    859   1.9   mycroft 			}
    860   1.9   mycroft 			m->m_len = MLEN;
    861   1.9   mycroft 		}
    862   1.9   mycroft 		len = min(totlen, epkt - cp);
    863   1.9   mycroft 		if (len >= MINCLSIZE) {
    864   1.9   mycroft 			MCLGET(m, M_DONTWAIT);
    865  1.19   mycroft 			if ((m->m_flags & M_EXT) == 0) {
    866  1.20   mycroft 				m_free(m);
    867  1.19   mycroft 				m_freem(top);
    868  1.19   mycroft 				return (0);
    869  1.19   mycroft 			}
    870  1.19   mycroft 			m->m_len = len = min(len, MCLBYTES);
    871   1.9   mycroft 		} else {
    872   1.9   mycroft 			/*
    873   1.9   mycroft 			 * Place initial small packet/header at end of mbuf.
    874   1.9   mycroft 			 */
    875   1.9   mycroft 			if (len < m->m_len) {
    876   1.9   mycroft 				if (top == 0 && len + max_linkhdr <= m->m_len)
    877   1.9   mycroft 					m->m_data += max_linkhdr;
    878   1.9   mycroft 				m->m_len = len;
    879   1.9   mycroft 			} else
    880   1.9   mycroft 				len = m->m_len;
    881   1.9   mycroft 		}
    882   1.9   mycroft 		if (copy)
    883  1.14  christos 			copy(cp, mtod(m, caddr_t), (size_t)len);
    884   1.9   mycroft 		else
    885  1.30     perry 			memcpy(mtod(m, caddr_t), cp, (size_t)len);
    886   1.9   mycroft 		cp += len;
    887   1.9   mycroft 		*mp = m;
    888   1.9   mycroft 		mp = &m->m_next;
    889   1.9   mycroft 		totlen -= len;
    890   1.9   mycroft 		if (cp == epkt)
    891   1.9   mycroft 			cp = buf;
    892   1.9   mycroft 	}
    893   1.9   mycroft 	return (top);
    894  1.18   thorpej }
    895  1.18   thorpej 
    896  1.18   thorpej /*
    897  1.18   thorpej  * Copy data from a buffer back into the indicated mbuf chain,
    898  1.18   thorpej  * starting "off" bytes from the beginning, extending the mbuf
    899  1.18   thorpej  * chain if necessary.
    900  1.18   thorpej  */
    901  1.18   thorpej void
    902  1.18   thorpej m_copyback(m0, off, len, cp)
    903  1.18   thorpej 	struct	mbuf *m0;
    904  1.27      matt 	int off;
    905  1.27      matt 	int len;
    906  1.18   thorpej 	caddr_t cp;
    907  1.18   thorpej {
    908  1.27      matt 	int mlen;
    909  1.27      matt 	struct mbuf *m = m0, *n;
    910  1.18   thorpej 	int totlen = 0;
    911  1.18   thorpej 
    912  1.18   thorpej 	if (m0 == 0)
    913  1.18   thorpej 		return;
    914  1.18   thorpej 	while (off > (mlen = m->m_len)) {
    915  1.18   thorpej 		off -= mlen;
    916  1.18   thorpej 		totlen += mlen;
    917  1.18   thorpej 		if (m->m_next == 0) {
    918  1.18   thorpej 			n = m_getclr(M_DONTWAIT, m->m_type);
    919  1.18   thorpej 			if (n == 0)
    920  1.18   thorpej 				goto out;
    921  1.18   thorpej 			n->m_len = min(MLEN, len + off);
    922  1.18   thorpej 			m->m_next = n;
    923  1.18   thorpej 		}
    924  1.18   thorpej 		m = m->m_next;
    925  1.18   thorpej 	}
    926  1.18   thorpej 	while (len > 0) {
    927  1.18   thorpej 		mlen = min (m->m_len - off, len);
    928  1.30     perry 		memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
    929  1.18   thorpej 		cp += mlen;
    930  1.18   thorpej 		len -= mlen;
    931  1.18   thorpej 		mlen += off;
    932  1.18   thorpej 		off = 0;
    933  1.18   thorpej 		totlen += mlen;
    934  1.18   thorpej 		if (len == 0)
    935  1.18   thorpej 			break;
    936  1.18   thorpej 		if (m->m_next == 0) {
    937  1.18   thorpej 			n = m_get(M_DONTWAIT, m->m_type);
    938  1.18   thorpej 			if (n == 0)
    939  1.18   thorpej 				break;
    940  1.18   thorpej 			n->m_len = min(MLEN, len);
    941  1.18   thorpej 			m->m_next = n;
    942  1.18   thorpej 		}
    943  1.18   thorpej 		m = m->m_next;
    944  1.18   thorpej 	}
    945  1.18   thorpej out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
    946  1.18   thorpej 		m->m_pkthdr.len = totlen;
    947   1.1       cgd }
    948