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