Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.69
      1  1.69    martin /*	$NetBSD: uipc_mbuf.c,v 1.69 2003/06/23 11:02:06 martin Exp $	*/
      2  1.42   thorpej 
      3  1.42   thorpej /*-
      4  1.53   thorpej  * Copyright (c) 1999, 2001 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.56     lukem 
     75  1.56     lukem #include <sys/cdefs.h>
     76  1.69    martin __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.69 2003/06/23 11:02:06 martin Exp $");
     77  1.69    martin 
     78  1.69    martin #include "opt_mbuftrace.h"
     79  1.24       mrg 
     80   1.6   mycroft #include <sys/param.h>
     81   1.6   mycroft #include <sys/systm.h>
     82   1.6   mycroft #include <sys/proc.h>
     83   1.6   mycroft #include <sys/malloc.h>
     84   1.1       cgd #define MBTYPES
     85   1.6   mycroft #include <sys/mbuf.h>
     86   1.6   mycroft #include <sys/kernel.h>
     87   1.6   mycroft #include <sys/syslog.h>
     88   1.6   mycroft #include <sys/domain.h>
     89   1.6   mycroft #include <sys/protosw.h>
     90  1.28   thorpej #include <sys/pool.h>
     91  1.27      matt #include <sys/socket.h>
     92  1.55    simonb #include <sys/sysctl.h>
     93  1.55    simonb 
     94  1.27      matt #include <net/if.h>
     95  1.14  christos 
     96  1.65   thorpej #include <uvm/uvm.h>
     97  1.23       mrg 
     98  1.42   thorpej 
     99  1.28   thorpej struct	pool mbpool;		/* mbuf pool */
    100  1.28   thorpej struct	pool mclpool;		/* mbuf cluster pool */
    101  1.28   thorpej 
    102  1.53   thorpej struct pool_cache mbpool_cache;
    103  1.53   thorpej struct pool_cache mclpool_cache;
    104  1.53   thorpej 
    105  1.18   thorpej struct mbstat mbstat;
    106  1.18   thorpej int	max_linkhdr;
    107  1.18   thorpej int	max_protohdr;
    108  1.18   thorpej int	max_hdr;
    109  1.18   thorpej int	max_datalen;
    110  1.18   thorpej 
    111  1.65   thorpej static int mb_ctor(void *, void *, int);
    112  1.65   thorpej 
    113  1.62   thorpej void	*mclpool_alloc(struct pool *, int);
    114  1.62   thorpej void	mclpool_release(struct pool *, void *);
    115  1.58   thorpej 
    116  1.58   thorpej struct pool_allocator mclpool_allocator = {
    117  1.58   thorpej 	mclpool_alloc, mclpool_release, 0,
    118  1.58   thorpej };
    119  1.58   thorpej 
    120  1.44    itojun static struct mbuf *m_copym0 __P((struct mbuf *, int, int, int, int));
    121  1.28   thorpej 
    122  1.57   thorpej const char mclpool_warnmsg[] =
    123  1.42   thorpej     "WARNING: mclpool limit reached; increase NMBCLUSTERS";
    124  1.63   thorpej 
    125  1.63   thorpej MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
    126  1.42   thorpej 
    127  1.64      matt #ifdef MBUFTRACE
    128  1.64      matt struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
    129  1.64      matt struct mowner unknown_mowners[] = {
    130  1.64      matt 	{ "unknown", "free" },
    131  1.64      matt 	{ "unknown", "data" },
    132  1.64      matt 	{ "unknown", "header" },
    133  1.64      matt 	{ "unknown", "soname" },
    134  1.64      matt 	{ "unknown", "soopts" },
    135  1.64      matt 	{ "unknown", "ftable" },
    136  1.64      matt 	{ "unknown", "control" },
    137  1.64      matt 	{ "unknown", "oobdata" },
    138  1.64      matt };
    139  1.64      matt struct mowner revoked_mowner = { "revoked", "" };
    140  1.64      matt #endif
    141  1.64      matt 
    142  1.28   thorpej /*
    143  1.68    simonb  * Initialize the mbuf allocator.
    144  1.28   thorpej  */
    145   1.4       jtc void
    146  1.62   thorpej mbinit(void)
    147   1.1       cgd {
    148  1.65   thorpej 
    149  1.65   thorpej 	KASSERT(sizeof(struct _m_ext) <= MHLEN);
    150  1.67    simonb 	KASSERT(sizeof(struct mbuf) == MSIZE);
    151  1.65   thorpej 
    152  1.58   thorpej 	pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL);
    153  1.58   thorpej 	pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator);
    154  1.53   thorpej 
    155  1.59   thorpej 	pool_set_drain_hook(&mbpool, m_reclaim, NULL);
    156  1.59   thorpej 	pool_set_drain_hook(&mclpool, m_reclaim, NULL);
    157  1.59   thorpej 
    158  1.65   thorpej 	pool_cache_init(&mbpool_cache, &mbpool, mb_ctor, NULL, NULL);
    159  1.53   thorpej 	pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
    160  1.37   thorpej 
    161  1.37   thorpej 	/*
    162  1.39   thorpej 	 * Set the hard limit on the mclpool to the number of
    163  1.39   thorpej 	 * mbuf clusters the kernel is to support.  Log the limit
    164  1.39   thorpej 	 * reached message max once a minute.
    165  1.39   thorpej 	 */
    166  1.42   thorpej 	pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    167  1.42   thorpej 
    168  1.39   thorpej 	/*
    169  1.42   thorpej 	 * Set a low water mark for both mbufs and clusters.  This should
    170  1.42   thorpej 	 * help ensure that they can be allocated in a memory starvation
    171  1.42   thorpej 	 * situation.  This is important for e.g. diskless systems which
    172  1.42   thorpej 	 * must allocate mbufs in order for the pagedaemon to clean pages.
    173  1.37   thorpej 	 */
    174  1.42   thorpej 	pool_setlowat(&mbpool, mblowat);
    175  1.42   thorpej 	pool_setlowat(&mclpool, mcllowat);
    176  1.64      matt 
    177  1.64      matt #ifdef MBUFTRACE
    178  1.64      matt 	{
    179  1.64      matt 		/*
    180  1.64      matt 		 * Attach the unknown mowners.
    181  1.64      matt 		 */
    182  1.64      matt 		int i;
    183  1.64      matt 		MOWNER_ATTACH(&revoked_mowner);
    184  1.64      matt 		for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
    185  1.64      matt 		     i-- > 0; )
    186  1.64      matt 			MOWNER_ATTACH(&unknown_mowners[i]);
    187  1.64      matt 	}
    188  1.64      matt #endif
    189  1.42   thorpej }
    190  1.42   thorpej 
    191  1.42   thorpej int
    192  1.62   thorpej sysctl_dombuf(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    193  1.62   thorpej     void *newp, size_t newlen)
    194  1.42   thorpej {
    195  1.42   thorpej 	int error, newval;
    196  1.42   thorpej 
    197  1.42   thorpej 	/* All sysctl names at this level are terminal. */
    198  1.42   thorpej 	if (namelen != 1)
    199  1.42   thorpej 		return (ENOTDIR);		/* overloaded */
    200  1.42   thorpej 
    201  1.42   thorpej 	switch (name[0]) {
    202  1.42   thorpej 	case MBUF_MSIZE:
    203  1.42   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, msize));
    204  1.42   thorpej 	case MBUF_MCLBYTES:
    205  1.42   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, mclbytes));
    206  1.42   thorpej 	case MBUF_NMBCLUSTERS:
    207  1.42   thorpej 		/*
    208  1.42   thorpej 		 * If we have direct-mapped pool pages, we can adjust this
    209  1.42   thorpej 		 * number on the fly.  If not, we're limited by the size
    210  1.42   thorpej 		 * of mb_map, and cannot change this value.
    211  1.42   thorpej 		 *
    212  1.42   thorpej 		 * Note: we only allow the value to be increased, never
    213  1.42   thorpej 		 * decreased.
    214  1.42   thorpej 		 */
    215  1.42   thorpej 		if (mb_map == NULL) {
    216  1.42   thorpej 			newval = nmbclusters;
    217  1.42   thorpej 			error = sysctl_int(oldp, oldlenp, newp, newlen,
    218  1.42   thorpej 			    &newval);
    219  1.42   thorpej 			if (error != 0)
    220  1.42   thorpej 				return (error);
    221  1.42   thorpej 			if (newp != NULL) {
    222  1.42   thorpej 				if (newval >= nmbclusters) {
    223  1.42   thorpej 					nmbclusters = newval;
    224  1.42   thorpej 					pool_sethardlimit(&mclpool,
    225  1.42   thorpej 					    nmbclusters, mclpool_warnmsg, 60);
    226  1.42   thorpej 				} else
    227  1.42   thorpej 					error = EINVAL;
    228  1.42   thorpej 			}
    229  1.42   thorpej 			return (error);
    230  1.42   thorpej 		} else
    231  1.42   thorpej 			return (sysctl_rdint(oldp, oldlenp, newp, nmbclusters));
    232  1.42   thorpej 	case MBUF_MBLOWAT:
    233  1.42   thorpej 	case MBUF_MCLLOWAT:
    234  1.42   thorpej 		/* New value must be >= 0. */
    235  1.42   thorpej 		newval = (name[0] == MBUF_MBLOWAT) ? mblowat : mcllowat;
    236  1.42   thorpej 		error = sysctl_int(oldp, oldlenp, newp, newlen, &newval);
    237  1.42   thorpej 		if (error != 0)
    238  1.42   thorpej 			return (error);
    239  1.42   thorpej 		if (newp != NULL) {
    240  1.42   thorpej 			if (newval >= 0) {
    241  1.42   thorpej 				if (name[0] == MBUF_MBLOWAT) {
    242  1.42   thorpej 					mblowat = newval;
    243  1.42   thorpej 					pool_setlowat(&mbpool, newval);
    244  1.42   thorpej 				} else {
    245  1.42   thorpej 					mcllowat = newval;
    246  1.42   thorpej 					pool_setlowat(&mclpool, newval);
    247  1.42   thorpej 				}
    248  1.42   thorpej 			} else
    249  1.42   thorpej 				error = EINVAL;
    250  1.42   thorpej 		}
    251  1.42   thorpej 		return (error);
    252  1.64      matt 	case MBUF_STATS:
    253  1.64      matt 		return (sysctl_rdstruct(oldp, oldlenp, newp,
    254  1.64      matt 		    &mbstat, sizeof(mbstat)));
    255  1.64      matt #ifdef MBUFTRACE
    256  1.64      matt 	case MBUF_MOWNERS: {
    257  1.64      matt 		struct mowner *mo;
    258  1.64      matt 		size_t len = 0;
    259  1.64      matt 		if (newp != NULL)
    260  1.64      matt 			return (EPERM);
    261  1.64      matt 		error = 0;
    262  1.64      matt 		LIST_FOREACH(mo, &mowners, mo_link) {
    263  1.64      matt 			if (oldp != NULL) {
    264  1.64      matt 				if (*oldlenp - len < sizeof(*mo)) {
    265  1.64      matt 					error = ENOMEM;
    266  1.64      matt 					break;
    267  1.64      matt 				}
    268  1.64      matt 				error = copyout(mo, (caddr_t) oldp + len,
    269  1.64      matt 					sizeof(*mo));
    270  1.64      matt 				if (error)
    271  1.64      matt 					break;
    272  1.64      matt 			}
    273  1.64      matt 			len += sizeof(*mo);
    274  1.64      matt 		}
    275  1.64      matt 		*oldlenp = len;
    276  1.64      matt 		return (error);
    277  1.64      matt 	}
    278  1.64      matt #endif
    279  1.42   thorpej 	default:
    280  1.42   thorpej 		return (EOPNOTSUPP);
    281  1.42   thorpej 	}
    282  1.42   thorpej 	/* NOTREACHED */
    283  1.28   thorpej }
    284  1.28   thorpej 
    285  1.28   thorpej void *
    286  1.62   thorpej mclpool_alloc(struct pool *pp, int flags)
    287  1.28   thorpej {
    288  1.32   thorpej 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
    289  1.28   thorpej 
    290  1.54       chs 	return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
    291   1.1       cgd }
    292   1.1       cgd 
    293  1.28   thorpej void
    294  1.62   thorpej mclpool_release(struct pool *pp, void *v)
    295   1.1       cgd {
    296   1.1       cgd 
    297  1.31   thorpej 	uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
    298  1.65   thorpej }
    299  1.65   thorpej 
    300  1.65   thorpej /*ARGSUSED*/
    301  1.65   thorpej static int
    302  1.65   thorpej mb_ctor(void *arg, void *object, int flags)
    303  1.65   thorpej {
    304  1.65   thorpej 	struct mbuf *m = object;
    305  1.65   thorpej 
    306  1.65   thorpej #ifdef POOL_VTOPHYS
    307  1.65   thorpej 	m->m_paddr = POOL_VTOPHYS(m);
    308  1.65   thorpej #else
    309  1.65   thorpej 	m->m_paddr = M_PADDR_INVALID;
    310  1.65   thorpej #endif
    311  1.65   thorpej 	return (0);
    312   1.1       cgd }
    313   1.1       cgd 
    314  1.14  christos void
    315  1.59   thorpej m_reclaim(void *arg, int flags)
    316   1.1       cgd {
    317  1.27      matt 	struct domain *dp;
    318  1.27      matt 	struct protosw *pr;
    319  1.27      matt 	struct ifnet *ifp;
    320  1.52   thorpej 	int s = splvm();
    321   1.1       cgd 
    322  1.33   thorpej 	for (dp = domains; dp; dp = dp->dom_next)
    323  1.33   thorpej 		for (pr = dp->dom_protosw;
    324  1.33   thorpej 		     pr < dp->dom_protoswNPROTOSW; pr++)
    325  1.33   thorpej 			if (pr->pr_drain)
    326  1.33   thorpej 				(*pr->pr_drain)();
    327  1.27      matt 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
    328  1.27      matt 		if (ifp->if_drain)
    329  1.27      matt 			(*ifp->if_drain)(ifp);
    330   1.1       cgd 	splx(s);
    331   1.1       cgd 	mbstat.m_drain++;
    332   1.1       cgd }
    333   1.1       cgd 
    334   1.1       cgd /*
    335   1.1       cgd  * Space allocation routines.
    336   1.1       cgd  * These are also available as macros
    337   1.1       cgd  * for critical paths.
    338   1.1       cgd  */
    339   1.1       cgd struct mbuf *
    340  1.62   thorpej m_get(int nowait, int type)
    341   1.1       cgd {
    342  1.27      matt 	struct mbuf *m;
    343   1.1       cgd 
    344   1.5       cgd 	MGET(m, nowait, type);
    345   1.1       cgd 	return (m);
    346   1.1       cgd }
    347   1.1       cgd 
    348   1.1       cgd struct mbuf *
    349  1.62   thorpej m_gethdr(int nowait, int type)
    350   1.1       cgd {
    351  1.27      matt 	struct mbuf *m;
    352   1.1       cgd 
    353   1.5       cgd 	MGETHDR(m, nowait, type);
    354   1.1       cgd 	return (m);
    355   1.1       cgd }
    356   1.1       cgd 
    357   1.1       cgd struct mbuf *
    358  1.62   thorpej m_getclr(int nowait, int type)
    359   1.1       cgd {
    360  1.27      matt 	struct mbuf *m;
    361   1.1       cgd 
    362   1.5       cgd 	MGET(m, nowait, type);
    363   1.1       cgd 	if (m == 0)
    364   1.1       cgd 		return (0);
    365  1.30     perry 	memset(mtod(m, caddr_t), 0, MLEN);
    366   1.1       cgd 	return (m);
    367   1.1       cgd }
    368   1.1       cgd 
    369  1.64      matt void
    370  1.64      matt m_clget(struct mbuf *m, int nowait)
    371  1.64      matt {
    372  1.64      matt 	MCLGET(m, nowait);
    373  1.64      matt }
    374  1.64      matt 
    375   1.1       cgd struct mbuf *
    376  1.62   thorpej m_free(struct mbuf *m)
    377   1.1       cgd {
    378  1.27      matt 	struct mbuf *n;
    379   1.1       cgd 
    380   1.1       cgd 	MFREE(m, n);
    381   1.1       cgd 	return (n);
    382   1.1       cgd }
    383   1.1       cgd 
    384   1.9   mycroft void
    385  1.62   thorpej m_freem(struct mbuf *m)
    386   1.1       cgd {
    387  1.27      matt 	struct mbuf *n;
    388   1.1       cgd 
    389   1.1       cgd 	if (m == NULL)
    390   1.1       cgd 		return;
    391   1.1       cgd 	do {
    392   1.1       cgd 		MFREE(m, n);
    393  1.18   thorpej 		m = n;
    394  1.18   thorpej 	} while (m);
    395   1.1       cgd }
    396   1.1       cgd 
    397  1.64      matt #ifdef MBUFTRACE
    398  1.64      matt void
    399  1.64      matt m_claim(struct mbuf *m, struct mowner *mo)
    400  1.64      matt {
    401  1.64      matt 	for (; m != NULL; m = m->m_next)
    402  1.64      matt 		MCLAIM(m, mo);
    403  1.64      matt }
    404  1.64      matt #endif
    405  1.64      matt 
    406   1.1       cgd /*
    407   1.1       cgd  * Mbuffer utility routines.
    408   1.1       cgd  */
    409   1.1       cgd 
    410   1.1       cgd /*
    411   1.1       cgd  * Lesser-used path for M_PREPEND:
    412   1.1       cgd  * allocate new mbuf to prepend to chain,
    413   1.1       cgd  * copy junk along.
    414   1.1       cgd  */
    415   1.1       cgd struct mbuf *
    416  1.62   thorpej m_prepend(struct mbuf *m, int len, int how)
    417   1.1       cgd {
    418   1.1       cgd 	struct mbuf *mn;
    419   1.1       cgd 
    420   1.9   mycroft 	MGET(mn, how, m->m_type);
    421   1.1       cgd 	if (mn == (struct mbuf *)NULL) {
    422   1.1       cgd 		m_freem(m);
    423   1.1       cgd 		return ((struct mbuf *)NULL);
    424   1.1       cgd 	}
    425   1.1       cgd 	if (m->m_flags & M_PKTHDR) {
    426   1.1       cgd 		M_COPY_PKTHDR(mn, m);
    427   1.1       cgd 		m->m_flags &= ~M_PKTHDR;
    428  1.64      matt 	} else {
    429  1.64      matt 		MCLAIM(mn, m->m_owner);
    430   1.1       cgd 	}
    431   1.1       cgd 	mn->m_next = m;
    432   1.1       cgd 	m = mn;
    433   1.1       cgd 	if (len < MHLEN)
    434   1.1       cgd 		MH_ALIGN(m, len);
    435   1.1       cgd 	m->m_len = len;
    436   1.1       cgd 	return (m);
    437   1.1       cgd }
    438   1.1       cgd 
    439   1.1       cgd /*
    440   1.1       cgd  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    441   1.1       cgd  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    442   1.1       cgd  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    443   1.1       cgd  */
    444   1.1       cgd int MCFail;
    445   1.1       cgd 
    446   1.1       cgd struct mbuf *
    447  1.62   thorpej m_copym(struct mbuf *m, int off0, int len, int wait)
    448   1.1       cgd {
    449  1.44    itojun 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    450  1.44    itojun }
    451  1.44    itojun 
    452  1.44    itojun struct mbuf *
    453  1.62   thorpej m_dup(struct mbuf *m, int off0, int len, int wait)
    454  1.44    itojun {
    455  1.44    itojun 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    456  1.44    itojun }
    457  1.44    itojun 
    458  1.44    itojun static struct mbuf *
    459  1.62   thorpej m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
    460  1.44    itojun {
    461  1.27      matt 	struct mbuf *n, **np;
    462  1.27      matt 	int off = off0;
    463   1.1       cgd 	struct mbuf *top;
    464   1.1       cgd 	int copyhdr = 0;
    465   1.1       cgd 
    466   1.1       cgd 	if (off < 0 || len < 0)
    467  1.43   thorpej 		panic("m_copym: off %d, len %d", off, len);
    468   1.1       cgd 	if (off == 0 && m->m_flags & M_PKTHDR)
    469   1.1       cgd 		copyhdr = 1;
    470   1.1       cgd 	while (off > 0) {
    471   1.1       cgd 		if (m == 0)
    472  1.43   thorpej 			panic("m_copym: m == 0");
    473   1.1       cgd 		if (off < m->m_len)
    474   1.1       cgd 			break;
    475   1.1       cgd 		off -= m->m_len;
    476   1.1       cgd 		m = m->m_next;
    477   1.1       cgd 	}
    478   1.1       cgd 	np = &top;
    479   1.1       cgd 	top = 0;
    480   1.1       cgd 	while (len > 0) {
    481   1.1       cgd 		if (m == 0) {
    482   1.1       cgd 			if (len != M_COPYALL)
    483  1.43   thorpej 				panic("m_copym: m == 0 and not COPYALL");
    484   1.1       cgd 			break;
    485   1.1       cgd 		}
    486   1.1       cgd 		MGET(n, wait, m->m_type);
    487   1.1       cgd 		*np = n;
    488   1.1       cgd 		if (n == 0)
    489   1.1       cgd 			goto nospace;
    490  1.64      matt 		MCLAIM(n, m->m_owner);
    491   1.1       cgd 		if (copyhdr) {
    492   1.1       cgd 			M_COPY_PKTHDR(n, m);
    493   1.1       cgd 			if (len == M_COPYALL)
    494   1.1       cgd 				n->m_pkthdr.len -= off0;
    495   1.1       cgd 			else
    496   1.1       cgd 				n->m_pkthdr.len = len;
    497   1.1       cgd 			copyhdr = 0;
    498   1.1       cgd 		}
    499   1.9   mycroft 		n->m_len = min(len, m->m_len - off);
    500   1.1       cgd 		if (m->m_flags & M_EXT) {
    501  1.44    itojun 			if (!deep) {
    502  1.44    itojun 				n->m_data = m->m_data + off;
    503  1.44    itojun 				n->m_ext = m->m_ext;
    504  1.44    itojun 				MCLADDREFERENCE(m, n);
    505  1.44    itojun 			} else {
    506  1.48    itojun 				/*
    507  1.50    itojun 				 * we are unsure about the way m was allocated.
    508  1.50    itojun 				 * copy into multiple MCLBYTES cluster mbufs.
    509  1.48    itojun 				 */
    510  1.44    itojun 				MCLGET(n, wait);
    511  1.50    itojun 				n->m_len = 0;
    512  1.50    itojun 				n->m_len = M_TRAILINGSPACE(n);
    513  1.50    itojun 				n->m_len = min(n->m_len, len);
    514  1.50    itojun 				n->m_len = min(n->m_len, m->m_len - off);
    515  1.50    itojun 				memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
    516  1.44    itojun 				    (unsigned)n->m_len);
    517  1.44    itojun 			}
    518   1.1       cgd 		} else
    519  1.30     perry 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
    520   1.1       cgd 			    (unsigned)n->m_len);
    521   1.1       cgd 		if (len != M_COPYALL)
    522   1.1       cgd 			len -= n->m_len;
    523  1.50    itojun 		off += n->m_len;
    524  1.50    itojun #ifdef DIAGNOSTIC
    525  1.50    itojun 		if (off > m->m_len)
    526  1.50    itojun 			panic("m_copym0 overrun");
    527  1.50    itojun #endif
    528  1.50    itojun 		if (off == m->m_len) {
    529  1.50    itojun 			m = m->m_next;
    530  1.50    itojun 			off = 0;
    531  1.50    itojun 		}
    532   1.1       cgd 		np = &n->m_next;
    533   1.1       cgd 	}
    534   1.1       cgd 	if (top == 0)
    535   1.1       cgd 		MCFail++;
    536   1.1       cgd 	return (top);
    537   1.1       cgd nospace:
    538   1.1       cgd 	m_freem(top);
    539   1.1       cgd 	MCFail++;
    540   1.1       cgd 	return (0);
    541   1.1       cgd }
    542   1.1       cgd 
    543   1.1       cgd /*
    544  1.18   thorpej  * Copy an entire packet, including header (which must be present).
    545  1.18   thorpej  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    546  1.18   thorpej  */
    547  1.18   thorpej struct mbuf *
    548  1.62   thorpej m_copypacket(struct mbuf *m, int how)
    549  1.18   thorpej {
    550  1.18   thorpej 	struct mbuf *top, *n, *o;
    551  1.18   thorpej 
    552  1.18   thorpej 	MGET(n, how, m->m_type);
    553  1.18   thorpej 	top = n;
    554  1.18   thorpej 	if (!n)
    555  1.18   thorpej 		goto nospace;
    556  1.18   thorpej 
    557  1.64      matt 	MCLAIM(n, m->m_owner);
    558  1.18   thorpej 	M_COPY_PKTHDR(n, m);
    559  1.18   thorpej 	n->m_len = m->m_len;
    560  1.18   thorpej 	if (m->m_flags & M_EXT) {
    561  1.18   thorpej 		n->m_data = m->m_data;
    562  1.18   thorpej 		n->m_ext = m->m_ext;
    563  1.18   thorpej 		MCLADDREFERENCE(m, n);
    564  1.18   thorpej 	} else {
    565  1.30     perry 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    566  1.18   thorpej 	}
    567  1.18   thorpej 
    568  1.18   thorpej 	m = m->m_next;
    569  1.18   thorpej 	while (m) {
    570  1.18   thorpej 		MGET(o, how, m->m_type);
    571  1.18   thorpej 		if (!o)
    572  1.18   thorpej 			goto nospace;
    573  1.18   thorpej 
    574  1.64      matt 		MCLAIM(o, m->m_owner);
    575  1.18   thorpej 		n->m_next = o;
    576  1.18   thorpej 		n = n->m_next;
    577  1.18   thorpej 
    578  1.18   thorpej 		n->m_len = m->m_len;
    579  1.18   thorpej 		if (m->m_flags & M_EXT) {
    580  1.18   thorpej 			n->m_data = m->m_data;
    581  1.18   thorpej 			n->m_ext = m->m_ext;
    582  1.18   thorpej 			MCLADDREFERENCE(m, n);
    583  1.18   thorpej 		} else {
    584  1.30     perry 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    585  1.18   thorpej 		}
    586  1.18   thorpej 
    587  1.18   thorpej 		m = m->m_next;
    588  1.18   thorpej 	}
    589  1.18   thorpej 	return top;
    590  1.18   thorpej nospace:
    591  1.18   thorpej 	m_freem(top);
    592  1.18   thorpej 	MCFail++;
    593  1.18   thorpej 	return 0;
    594  1.18   thorpej }
    595  1.18   thorpej 
    596  1.18   thorpej /*
    597   1.1       cgd  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    598   1.1       cgd  * continuing for "len" bytes, into the indicated buffer.
    599   1.1       cgd  */
    600  1.14  christos void
    601  1.62   thorpej m_copydata(struct mbuf *m, int off, int len, caddr_t cp)
    602   1.1       cgd {
    603  1.27      matt 	unsigned count;
    604   1.1       cgd 
    605   1.1       cgd 	if (off < 0 || len < 0)
    606   1.1       cgd 		panic("m_copydata");
    607   1.1       cgd 	while (off > 0) {
    608   1.1       cgd 		if (m == 0)
    609   1.1       cgd 			panic("m_copydata");
    610   1.1       cgd 		if (off < m->m_len)
    611   1.1       cgd 			break;
    612   1.1       cgd 		off -= m->m_len;
    613   1.1       cgd 		m = m->m_next;
    614   1.1       cgd 	}
    615   1.1       cgd 	while (len > 0) {
    616   1.1       cgd 		if (m == 0)
    617   1.1       cgd 			panic("m_copydata");
    618   1.9   mycroft 		count = min(m->m_len - off, len);
    619  1.30     perry 		memcpy(cp, mtod(m, caddr_t) + off, count);
    620   1.1       cgd 		len -= count;
    621   1.1       cgd 		cp += count;
    622   1.1       cgd 		off = 0;
    623   1.1       cgd 		m = m->m_next;
    624   1.1       cgd 	}
    625   1.1       cgd }
    626   1.1       cgd 
    627   1.1       cgd /*
    628   1.1       cgd  * Concatenate mbuf chain n to m.
    629   1.1       cgd  * Both chains must be of the same type (e.g. MT_DATA).
    630   1.1       cgd  * Any m_pkthdr is not updated.
    631   1.1       cgd  */
    632  1.14  christos void
    633  1.62   thorpej m_cat(struct mbuf *m, struct mbuf *n)
    634   1.1       cgd {
    635   1.1       cgd 	while (m->m_next)
    636   1.1       cgd 		m = m->m_next;
    637   1.1       cgd 	while (n) {
    638   1.1       cgd 		if (m->m_flags & M_EXT ||
    639   1.1       cgd 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
    640   1.1       cgd 			/* just join the two chains */
    641   1.1       cgd 			m->m_next = n;
    642   1.1       cgd 			return;
    643   1.1       cgd 		}
    644   1.1       cgd 		/* splat the data from one into the other */
    645  1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    646   1.1       cgd 		    (u_int)n->m_len);
    647   1.1       cgd 		m->m_len += n->m_len;
    648   1.1       cgd 		n = m_free(n);
    649   1.1       cgd 	}
    650   1.1       cgd }
    651   1.1       cgd 
    652  1.11   mycroft void
    653  1.62   thorpej m_adj(struct mbuf *mp, int req_len)
    654   1.1       cgd {
    655  1.27      matt 	int len = req_len;
    656  1.27      matt 	struct mbuf *m;
    657  1.27      matt 	int count;
    658   1.1       cgd 
    659   1.1       cgd 	if ((m = mp) == NULL)
    660   1.1       cgd 		return;
    661   1.1       cgd 	if (len >= 0) {
    662   1.1       cgd 		/*
    663   1.1       cgd 		 * Trim from head.
    664   1.1       cgd 		 */
    665   1.1       cgd 		while (m != NULL && len > 0) {
    666   1.1       cgd 			if (m->m_len <= len) {
    667   1.1       cgd 				len -= m->m_len;
    668   1.1       cgd 				m->m_len = 0;
    669   1.1       cgd 				m = m->m_next;
    670   1.1       cgd 			} else {
    671   1.1       cgd 				m->m_len -= len;
    672   1.1       cgd 				m->m_data += len;
    673   1.1       cgd 				len = 0;
    674   1.1       cgd 			}
    675   1.1       cgd 		}
    676   1.1       cgd 		m = mp;
    677   1.1       cgd 		if (mp->m_flags & M_PKTHDR)
    678   1.1       cgd 			m->m_pkthdr.len -= (req_len - len);
    679   1.1       cgd 	} else {
    680   1.1       cgd 		/*
    681   1.1       cgd 		 * Trim from tail.  Scan the mbuf chain,
    682   1.1       cgd 		 * calculating its length and finding the last mbuf.
    683   1.1       cgd 		 * If the adjustment only affects this mbuf, then just
    684   1.1       cgd 		 * adjust and return.  Otherwise, rescan and truncate
    685   1.1       cgd 		 * after the remaining size.
    686   1.1       cgd 		 */
    687   1.1       cgd 		len = -len;
    688   1.1       cgd 		count = 0;
    689   1.1       cgd 		for (;;) {
    690   1.1       cgd 			count += m->m_len;
    691   1.1       cgd 			if (m->m_next == (struct mbuf *)0)
    692   1.1       cgd 				break;
    693   1.1       cgd 			m = m->m_next;
    694   1.1       cgd 		}
    695   1.1       cgd 		if (m->m_len >= len) {
    696   1.1       cgd 			m->m_len -= len;
    697   1.8   deraadt 			if (mp->m_flags & M_PKTHDR)
    698   1.8   deraadt 				mp->m_pkthdr.len -= len;
    699   1.1       cgd 			return;
    700   1.1       cgd 		}
    701   1.1       cgd 		count -= len;
    702   1.1       cgd 		if (count < 0)
    703   1.1       cgd 			count = 0;
    704   1.1       cgd 		/*
    705   1.1       cgd 		 * Correct length for chain is "count".
    706   1.1       cgd 		 * Find the mbuf with last data, adjust its length,
    707   1.1       cgd 		 * and toss data from remaining mbufs on chain.
    708   1.1       cgd 		 */
    709   1.1       cgd 		m = mp;
    710   1.1       cgd 		if (m->m_flags & M_PKTHDR)
    711   1.1       cgd 			m->m_pkthdr.len = count;
    712   1.1       cgd 		for (; m; m = m->m_next) {
    713   1.1       cgd 			if (m->m_len >= count) {
    714   1.1       cgd 				m->m_len = count;
    715   1.1       cgd 				break;
    716   1.1       cgd 			}
    717   1.1       cgd 			count -= m->m_len;
    718   1.1       cgd 		}
    719  1.18   thorpej 		while (m->m_next)
    720  1.18   thorpej 			(m = m->m_next) ->m_len = 0;
    721   1.1       cgd 	}
    722   1.1       cgd }
    723   1.1       cgd 
    724   1.1       cgd /*
    725   1.1       cgd  * Rearange an mbuf chain so that len bytes are contiguous
    726   1.1       cgd  * and in the data area of an mbuf (so that mtod and dtom
    727   1.1       cgd  * will work for a structure of size len).  Returns the resulting
    728   1.1       cgd  * mbuf chain on success, frees it and returns null on failure.
    729   1.1       cgd  * If there is room, it will add up to max_protohdr-len extra bytes to the
    730   1.1       cgd  * contiguous region in an attempt to avoid being called next time.
    731   1.1       cgd  */
    732   1.1       cgd int MPFail;
    733   1.1       cgd 
    734   1.1       cgd struct mbuf *
    735  1.62   thorpej m_pullup(struct mbuf *n, int len)
    736   1.1       cgd {
    737  1.27      matt 	struct mbuf *m;
    738  1.27      matt 	int count;
    739   1.1       cgd 	int space;
    740   1.1       cgd 
    741   1.1       cgd 	/*
    742   1.1       cgd 	 * If first mbuf has no cluster, and has room for len bytes
    743   1.1       cgd 	 * without shifting current data, pullup into it,
    744   1.1       cgd 	 * otherwise allocate a new mbuf to prepend to the chain.
    745   1.1       cgd 	 */
    746   1.1       cgd 	if ((n->m_flags & M_EXT) == 0 &&
    747   1.1       cgd 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    748   1.1       cgd 		if (n->m_len >= len)
    749   1.1       cgd 			return (n);
    750   1.1       cgd 		m = n;
    751   1.1       cgd 		n = n->m_next;
    752   1.1       cgd 		len -= m->m_len;
    753   1.1       cgd 	} else {
    754   1.1       cgd 		if (len > MHLEN)
    755   1.1       cgd 			goto bad;
    756   1.1       cgd 		MGET(m, M_DONTWAIT, n->m_type);
    757   1.1       cgd 		if (m == 0)
    758   1.1       cgd 			goto bad;
    759  1.64      matt 		MCLAIM(m, n->m_owner);
    760   1.1       cgd 		m->m_len = 0;
    761   1.1       cgd 		if (n->m_flags & M_PKTHDR) {
    762   1.1       cgd 			M_COPY_PKTHDR(m, n);
    763   1.1       cgd 			n->m_flags &= ~M_PKTHDR;
    764   1.1       cgd 		}
    765   1.1       cgd 	}
    766   1.1       cgd 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    767   1.1       cgd 	do {
    768   1.1       cgd 		count = min(min(max(len, max_protohdr), space), n->m_len);
    769  1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    770   1.1       cgd 		  (unsigned)count);
    771   1.1       cgd 		len -= count;
    772   1.1       cgd 		m->m_len += count;
    773   1.1       cgd 		n->m_len -= count;
    774   1.1       cgd 		space -= count;
    775   1.1       cgd 		if (n->m_len)
    776   1.1       cgd 			n->m_data += count;
    777   1.1       cgd 		else
    778   1.1       cgd 			n = m_free(n);
    779   1.1       cgd 	} while (len > 0 && n);
    780   1.1       cgd 	if (len > 0) {
    781   1.1       cgd 		(void) m_free(m);
    782   1.1       cgd 		goto bad;
    783   1.1       cgd 	}
    784   1.1       cgd 	m->m_next = n;
    785   1.1       cgd 	return (m);
    786   1.1       cgd bad:
    787   1.1       cgd 	m_freem(n);
    788   1.1       cgd 	MPFail++;
    789   1.1       cgd 	return (0);
    790  1.60   thorpej }
    791  1.60   thorpej 
    792  1.60   thorpej /*
    793  1.60   thorpej  * Like m_pullup(), except a new mbuf is always allocated, and we allow
    794  1.60   thorpej  * the amount of empty space before the data in the new mbuf to be specified
    795  1.60   thorpej  * (in the event that the caller expects to prepend later).
    796  1.60   thorpej  */
    797  1.60   thorpej int MSFail;
    798  1.60   thorpej 
    799  1.60   thorpej struct mbuf *
    800  1.60   thorpej m_copyup(struct mbuf *n, int len, int dstoff)
    801  1.60   thorpej {
    802  1.60   thorpej 	struct mbuf *m;
    803  1.60   thorpej 	int count, space;
    804  1.60   thorpej 
    805  1.60   thorpej 	if (len > (MHLEN - dstoff))
    806  1.60   thorpej 		goto bad;
    807  1.60   thorpej 	MGET(m, M_DONTWAIT, n->m_type);
    808  1.60   thorpej 	if (m == NULL)
    809  1.60   thorpej 		goto bad;
    810  1.64      matt 	MCLAIM(m, n->m_owner);
    811  1.60   thorpej 	m->m_len = 0;
    812  1.60   thorpej 	if (n->m_flags & M_PKTHDR) {
    813  1.60   thorpej 		M_COPY_PKTHDR(m, n);
    814  1.60   thorpej 		n->m_flags &= ~M_PKTHDR;
    815  1.60   thorpej 	}
    816  1.60   thorpej 	m->m_data += dstoff;
    817  1.60   thorpej 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    818  1.60   thorpej 	do {
    819  1.60   thorpej 		count = min(min(max(len, max_protohdr), space), n->m_len);
    820  1.60   thorpej 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    821  1.60   thorpej 		    (unsigned)count);
    822  1.60   thorpej 		len -= count;
    823  1.60   thorpej 		m->m_len += count;
    824  1.60   thorpej 		n->m_len -= count;
    825  1.60   thorpej 		space -= count;
    826  1.60   thorpej 		if (n->m_len)
    827  1.60   thorpej 			n->m_data += count;
    828  1.60   thorpej 		else
    829  1.60   thorpej 			n = m_free(n);
    830  1.60   thorpej 	} while (len > 0 && n);
    831  1.60   thorpej 	if (len > 0) {
    832  1.60   thorpej 		(void) m_free(m);
    833  1.60   thorpej 		goto bad;
    834  1.60   thorpej 	}
    835  1.60   thorpej 	m->m_next = n;
    836  1.60   thorpej 	return (m);
    837  1.60   thorpej  bad:
    838  1.60   thorpej 	m_freem(n);
    839  1.60   thorpej 	MSFail++;
    840  1.60   thorpej 	return (NULL);
    841   1.9   mycroft }
    842   1.9   mycroft 
    843   1.9   mycroft /*
    844   1.9   mycroft  * Partition an mbuf chain in two pieces, returning the tail --
    845   1.9   mycroft  * all but the first len0 bytes.  In case of failure, it returns NULL and
    846   1.9   mycroft  * attempts to restore the chain to its original state.
    847   1.9   mycroft  */
    848   1.9   mycroft struct mbuf *
    849  1.62   thorpej m_split(struct mbuf *m0, int len0, int wait)
    850   1.9   mycroft {
    851  1.27      matt 	struct mbuf *m, *n;
    852  1.22   thorpej 	unsigned len = len0, remain, len_save;
    853   1.9   mycroft 
    854   1.9   mycroft 	for (m = m0; m && len > m->m_len; m = m->m_next)
    855   1.9   mycroft 		len -= m->m_len;
    856   1.9   mycroft 	if (m == 0)
    857   1.9   mycroft 		return (0);
    858   1.9   mycroft 	remain = m->m_len - len;
    859   1.9   mycroft 	if (m0->m_flags & M_PKTHDR) {
    860   1.9   mycroft 		MGETHDR(n, wait, m0->m_type);
    861   1.9   mycroft 		if (n == 0)
    862   1.9   mycroft 			return (0);
    863  1.64      matt 		MCLAIM(m, m0->m_owner);
    864   1.9   mycroft 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    865   1.9   mycroft 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    866  1.22   thorpej 		len_save = m0->m_pkthdr.len;
    867   1.9   mycroft 		m0->m_pkthdr.len = len0;
    868   1.9   mycroft 		if (m->m_flags & M_EXT)
    869   1.9   mycroft 			goto extpacket;
    870   1.9   mycroft 		if (remain > MHLEN) {
    871   1.9   mycroft 			/* m can't be the lead packet */
    872   1.9   mycroft 			MH_ALIGN(n, 0);
    873   1.9   mycroft 			n->m_next = m_split(m, len, wait);
    874   1.9   mycroft 			if (n->m_next == 0) {
    875   1.9   mycroft 				(void) m_free(n);
    876  1.22   thorpej 				m0->m_pkthdr.len = len_save;
    877   1.9   mycroft 				return (0);
    878   1.9   mycroft 			} else
    879   1.9   mycroft 				return (n);
    880   1.9   mycroft 		} else
    881   1.9   mycroft 			MH_ALIGN(n, remain);
    882   1.9   mycroft 	} else if (remain == 0) {
    883   1.9   mycroft 		n = m->m_next;
    884   1.9   mycroft 		m->m_next = 0;
    885   1.9   mycroft 		return (n);
    886   1.9   mycroft 	} else {
    887   1.9   mycroft 		MGET(n, wait, m->m_type);
    888   1.9   mycroft 		if (n == 0)
    889   1.9   mycroft 			return (0);
    890  1.64      matt 		MCLAIM(n, m->m_owner);
    891   1.9   mycroft 		M_ALIGN(n, remain);
    892   1.9   mycroft 	}
    893   1.9   mycroft extpacket:
    894   1.9   mycroft 	if (m->m_flags & M_EXT) {
    895   1.9   mycroft 		n->m_ext = m->m_ext;
    896  1.18   thorpej 		MCLADDREFERENCE(m, n);
    897   1.9   mycroft 		n->m_data = m->m_data + len;
    898   1.9   mycroft 	} else {
    899  1.30     perry 		memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
    900   1.9   mycroft 	}
    901   1.9   mycroft 	n->m_len = remain;
    902   1.9   mycroft 	m->m_len = len;
    903   1.9   mycroft 	n->m_next = m->m_next;
    904   1.9   mycroft 	m->m_next = 0;
    905   1.9   mycroft 	return (n);
    906   1.9   mycroft }
    907   1.9   mycroft /*
    908   1.9   mycroft  * Routine to copy from device local memory into mbufs.
    909   1.9   mycroft  */
    910   1.9   mycroft struct mbuf *
    911  1.62   thorpej m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
    912  1.62   thorpej     void (*copy)(const void *from, void *to, size_t len))
    913   1.9   mycroft {
    914  1.27      matt 	struct mbuf *m;
    915   1.9   mycroft 	struct mbuf *top = 0, **mp = &top;
    916  1.27      matt 	int off = off0, len;
    917  1.27      matt 	char *cp;
    918   1.9   mycroft 	char *epkt;
    919   1.9   mycroft 
    920   1.9   mycroft 	cp = buf;
    921   1.9   mycroft 	epkt = cp + totlen;
    922   1.9   mycroft 	if (off) {
    923  1.13       cgd 		/*
    924  1.13       cgd 		 * If 'off' is non-zero, packet is trailer-encapsulated,
    925  1.13       cgd 		 * so we have to skip the type and length fields.
    926  1.13       cgd 		 */
    927  1.13       cgd 		cp += off + 2 * sizeof(u_int16_t);
    928  1.13       cgd 		totlen -= 2 * sizeof(u_int16_t);
    929   1.9   mycroft 	}
    930   1.9   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    931   1.9   mycroft 	if (m == 0)
    932   1.9   mycroft 		return (0);
    933   1.9   mycroft 	m->m_pkthdr.rcvif = ifp;
    934   1.9   mycroft 	m->m_pkthdr.len = totlen;
    935   1.9   mycroft 	m->m_len = MHLEN;
    936   1.9   mycroft 
    937   1.9   mycroft 	while (totlen > 0) {
    938   1.9   mycroft 		if (top) {
    939   1.9   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
    940   1.9   mycroft 			if (m == 0) {
    941   1.9   mycroft 				m_freem(top);
    942   1.9   mycroft 				return (0);
    943   1.9   mycroft 			}
    944   1.9   mycroft 			m->m_len = MLEN;
    945   1.9   mycroft 		}
    946   1.9   mycroft 		len = min(totlen, epkt - cp);
    947   1.9   mycroft 		if (len >= MINCLSIZE) {
    948   1.9   mycroft 			MCLGET(m, M_DONTWAIT);
    949  1.19   mycroft 			if ((m->m_flags & M_EXT) == 0) {
    950  1.20   mycroft 				m_free(m);
    951  1.19   mycroft 				m_freem(top);
    952  1.19   mycroft 				return (0);
    953  1.19   mycroft 			}
    954  1.19   mycroft 			m->m_len = len = min(len, MCLBYTES);
    955   1.9   mycroft 		} else {
    956   1.9   mycroft 			/*
    957   1.9   mycroft 			 * Place initial small packet/header at end of mbuf.
    958   1.9   mycroft 			 */
    959   1.9   mycroft 			if (len < m->m_len) {
    960   1.9   mycroft 				if (top == 0 && len + max_linkhdr <= m->m_len)
    961   1.9   mycroft 					m->m_data += max_linkhdr;
    962   1.9   mycroft 				m->m_len = len;
    963   1.9   mycroft 			} else
    964   1.9   mycroft 				len = m->m_len;
    965   1.9   mycroft 		}
    966   1.9   mycroft 		if (copy)
    967  1.14  christos 			copy(cp, mtod(m, caddr_t), (size_t)len);
    968   1.9   mycroft 		else
    969  1.30     perry 			memcpy(mtod(m, caddr_t), cp, (size_t)len);
    970   1.9   mycroft 		cp += len;
    971   1.9   mycroft 		*mp = m;
    972   1.9   mycroft 		mp = &m->m_next;
    973   1.9   mycroft 		totlen -= len;
    974   1.9   mycroft 		if (cp == epkt)
    975   1.9   mycroft 			cp = buf;
    976   1.9   mycroft 	}
    977   1.9   mycroft 	return (top);
    978  1.18   thorpej }
    979  1.18   thorpej 
    980  1.18   thorpej /*
    981  1.18   thorpej  * Copy data from a buffer back into the indicated mbuf chain,
    982  1.18   thorpej  * starting "off" bytes from the beginning, extending the mbuf
    983  1.18   thorpej  * chain if necessary.
    984  1.18   thorpej  */
    985  1.18   thorpej void
    986  1.62   thorpej m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
    987  1.18   thorpej {
    988  1.27      matt 	int mlen;
    989  1.27      matt 	struct mbuf *m = m0, *n;
    990  1.18   thorpej 	int totlen = 0;
    991  1.18   thorpej 
    992  1.18   thorpej 	if (m0 == 0)
    993  1.18   thorpej 		return;
    994  1.18   thorpej 	while (off > (mlen = m->m_len)) {
    995  1.18   thorpej 		off -= mlen;
    996  1.18   thorpej 		totlen += mlen;
    997  1.18   thorpej 		if (m->m_next == 0) {
    998  1.18   thorpej 			n = m_getclr(M_DONTWAIT, m->m_type);
    999  1.18   thorpej 			if (n == 0)
   1000  1.18   thorpej 				goto out;
   1001  1.18   thorpej 			n->m_len = min(MLEN, len + off);
   1002  1.18   thorpej 			m->m_next = n;
   1003  1.18   thorpej 		}
   1004  1.18   thorpej 		m = m->m_next;
   1005  1.18   thorpej 	}
   1006  1.18   thorpej 	while (len > 0) {
   1007  1.18   thorpej 		mlen = min (m->m_len - off, len);
   1008  1.30     perry 		memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
   1009  1.18   thorpej 		cp += mlen;
   1010  1.18   thorpej 		len -= mlen;
   1011  1.18   thorpej 		mlen += off;
   1012  1.18   thorpej 		off = 0;
   1013  1.18   thorpej 		totlen += mlen;
   1014  1.18   thorpej 		if (len == 0)
   1015  1.18   thorpej 			break;
   1016  1.18   thorpej 		if (m->m_next == 0) {
   1017  1.18   thorpej 			n = m_get(M_DONTWAIT, m->m_type);
   1018  1.18   thorpej 			if (n == 0)
   1019  1.18   thorpej 				break;
   1020  1.18   thorpej 			n->m_len = min(MLEN, len);
   1021  1.18   thorpej 			m->m_next = n;
   1022  1.18   thorpej 		}
   1023  1.18   thorpej 		m = m->m_next;
   1024  1.18   thorpej 	}
   1025  1.18   thorpej out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
   1026  1.18   thorpej 		m->m_pkthdr.len = totlen;
   1027  1.66   thorpej }
   1028  1.66   thorpej 
   1029  1.66   thorpej /*
   1030  1.66   thorpej  * Apply function f to the data in an mbuf chain starting "off" bytes from the
   1031  1.66   thorpej  * beginning, continuing for "len" bytes.
   1032  1.66   thorpej  */
   1033  1.66   thorpej int
   1034  1.66   thorpej m_apply(struct mbuf *m, int off, int len,
   1035  1.66   thorpej     int (*f)(void *, caddr_t, unsigned int), void *arg)
   1036  1.66   thorpej {
   1037  1.66   thorpej 	unsigned int count;
   1038  1.66   thorpej 	int rval;
   1039  1.66   thorpej 
   1040  1.66   thorpej 	KASSERT(len >= 0);
   1041  1.66   thorpej 	KASSERT(off >= 0);
   1042  1.66   thorpej 
   1043  1.66   thorpej 	while (off > 0) {
   1044  1.66   thorpej 		KASSERT(m != NULL);
   1045  1.66   thorpej 		if (off < m->m_len)
   1046  1.66   thorpej 			break;
   1047  1.66   thorpej 		off -= m->m_len;
   1048  1.66   thorpej 		m = m->m_next;
   1049  1.66   thorpej 	}
   1050  1.66   thorpej 	while (len > 0) {
   1051  1.66   thorpej 		KASSERT(m != NULL);
   1052  1.66   thorpej 		count = min(m->m_len - off, len);
   1053  1.66   thorpej 
   1054  1.66   thorpej 		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
   1055  1.66   thorpej 		if (rval)
   1056  1.66   thorpej 			return (rval);
   1057  1.66   thorpej 
   1058  1.66   thorpej 		len -= count;
   1059  1.66   thorpej 		off = 0;
   1060  1.66   thorpej 		m = m->m_next;
   1061  1.66   thorpej 	}
   1062  1.66   thorpej 
   1063  1.66   thorpej 	return (0);
   1064  1.66   thorpej }
   1065  1.66   thorpej 
   1066  1.66   thorpej /*
   1067  1.66   thorpej  * Return a pointer to mbuf/offset of location in mbuf chain.
   1068  1.66   thorpej  */
   1069  1.66   thorpej struct mbuf *
   1070  1.66   thorpej m_getptr(struct mbuf *m, int loc, int *off)
   1071  1.66   thorpej {
   1072  1.66   thorpej 
   1073  1.66   thorpej 	while (loc >= 0) {
   1074  1.66   thorpej 		/* Normal end of search */
   1075  1.66   thorpej 		if (m->m_len > loc) {
   1076  1.66   thorpej 	    		*off = loc;
   1077  1.66   thorpej 	    		return (m);
   1078  1.66   thorpej 		} else {
   1079  1.66   thorpej 	    		loc -= m->m_len;
   1080  1.66   thorpej 
   1081  1.66   thorpej 	    		if (m->m_next == NULL) {
   1082  1.66   thorpej 				if (loc == 0) {
   1083  1.66   thorpej  					/* Point at the end of valid data */
   1084  1.66   thorpej 		    			*off = m->m_len;
   1085  1.66   thorpej 		    			return (m);
   1086  1.66   thorpej 				} else
   1087  1.66   thorpej 		  			return (NULL);
   1088  1.66   thorpej 	    		} else
   1089  1.66   thorpej 	      			m = m->m_next;
   1090  1.66   thorpej 		}
   1091  1.66   thorpej     	}
   1092  1.66   thorpej 
   1093  1.66   thorpej 	return (NULL);
   1094   1.1       cgd }
   1095