Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.181.2.4
      1  1.181.2.4  pgoyette /*	$NetBSD: uipc_mbuf.c,v 1.181.2.4 2018/04/22 07:20:27 pgoyette Exp $	*/
      2       1.42   thorpej 
      3      1.177      maxv /*
      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  *
     20       1.42   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.42   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.42   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.42   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.42   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.42   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.42   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.42   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.42   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.42   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.42   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31       1.42   thorpej  */
     32       1.10       cgd 
     33        1.1       cgd /*
     34        1.9   mycroft  * Copyright (c) 1982, 1986, 1988, 1991, 1993
     35        1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
     36        1.1       cgd  *
     37        1.1       cgd  * Redistribution and use in source and binary forms, with or without
     38        1.1       cgd  * modification, are permitted provided that the following conditions
     39        1.1       cgd  * are met:
     40        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     41        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     42        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     43        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     44        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     45       1.70       agc  * 3. Neither the name of the University nor the names of its contributors
     46        1.1       cgd  *    may be used to endorse or promote products derived from this software
     47        1.1       cgd  *    without specific prior written permission.
     48        1.1       cgd  *
     49        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59        1.1       cgd  * SUCH DAMAGE.
     60        1.1       cgd  *
     61       1.26      fvdl  *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
     62        1.1       cgd  */
     63       1.56     lukem 
     64       1.56     lukem #include <sys/cdefs.h>
     65  1.181.2.4  pgoyette __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.181.2.4 2018/04/22 07:20:27 pgoyette Exp $");
     66       1.69    martin 
     67      1.163     pooka #ifdef _KERNEL_OPT
     68       1.69    martin #include "opt_mbuftrace.h"
     69      1.133     joerg #include "opt_nmbclusters.h"
     70      1.105      yamt #include "opt_ddb.h"
     71      1.163     pooka #endif
     72       1.24       mrg 
     73        1.6   mycroft #include <sys/param.h>
     74        1.6   mycroft #include <sys/systm.h>
     75      1.125      yamt #include <sys/atomic.h>
     76      1.124      yamt #include <sys/cpu.h>
     77        1.6   mycroft #include <sys/proc.h>
     78        1.6   mycroft #include <sys/mbuf.h>
     79        1.6   mycroft #include <sys/kernel.h>
     80        1.6   mycroft #include <sys/syslog.h>
     81        1.6   mycroft #include <sys/domain.h>
     82        1.6   mycroft #include <sys/protosw.h>
     83      1.124      yamt #include <sys/percpu.h>
     84       1.28   thorpej #include <sys/pool.h>
     85       1.27      matt #include <sys/socket.h>
     86       1.55    simonb #include <sys/sysctl.h>
     87       1.55    simonb 
     88       1.27      matt #include <net/if.h>
     89       1.14  christos 
     90      1.122        ad pool_cache_t mb_cache;	/* mbuf cache */
     91      1.122        ad pool_cache_t mcl_cache;	/* mbuf cluster cache */
     92       1.53   thorpej 
     93       1.18   thorpej struct mbstat mbstat;
     94       1.18   thorpej int	max_linkhdr;
     95       1.18   thorpej int	max_protohdr;
     96       1.18   thorpej int	max_hdr;
     97       1.18   thorpej int	max_datalen;
     98       1.18   thorpej 
     99       1.65   thorpej static int mb_ctor(void *, void *, int);
    100       1.65   thorpej 
    101      1.129     pooka static void	sysctl_kern_mbuf_setup(void);
    102      1.129     pooka 
    103      1.129     pooka static struct sysctllog *mbuf_sysctllog;
    104      1.129     pooka 
    105      1.179      maxv static struct mbuf *m_copym0(struct mbuf *, int, int, int, bool);
    106      1.181      maxv static struct mbuf *m_split0(struct mbuf *, int, int, bool);
    107       1.86      yamt static int m_copyback0(struct mbuf **, int, int, const void *, int, int);
    108       1.85      yamt 
    109       1.85      yamt /* flags for m_copyback0 */
    110       1.85      yamt #define	M_COPYBACK0_COPYBACK	0x0001	/* copyback from cp */
    111       1.85      yamt #define	M_COPYBACK0_PRESERVE	0x0002	/* preserve original data */
    112       1.85      yamt #define	M_COPYBACK0_COW		0x0004	/* do copy-on-write */
    113       1.85      yamt #define	M_COPYBACK0_EXTEND	0x0008	/* extend chain */
    114       1.28   thorpej 
    115      1.103   thorpej static const char mclpool_warnmsg[] =
    116      1.133     joerg     "WARNING: mclpool limit reached; increase kern.mbuf.nmbclusters";
    117       1.63   thorpej 
    118       1.63   thorpej MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
    119       1.42   thorpej 
    120      1.124      yamt static percpu_t *mbstat_percpu;
    121      1.124      yamt 
    122       1.64      matt #ifdef MBUFTRACE
    123       1.64      matt struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
    124       1.64      matt struct mowner unknown_mowners[] = {
    125      1.114    dogcow 	MOWNER_INIT("unknown", "free"),
    126      1.114    dogcow 	MOWNER_INIT("unknown", "data"),
    127      1.114    dogcow 	MOWNER_INIT("unknown", "header"),
    128      1.114    dogcow 	MOWNER_INIT("unknown", "soname"),
    129      1.114    dogcow 	MOWNER_INIT("unknown", "soopts"),
    130      1.114    dogcow 	MOWNER_INIT("unknown", "ftable"),
    131      1.114    dogcow 	MOWNER_INIT("unknown", "control"),
    132      1.114    dogcow 	MOWNER_INIT("unknown", "oobdata"),
    133       1.64      matt };
    134      1.114    dogcow struct mowner revoked_mowner = MOWNER_INIT("revoked", "");
    135       1.64      matt #endif
    136       1.64      matt 
    137      1.125      yamt #define	MEXT_ISEMBEDDED(m) ((m)->m_ext_ref == (m))
    138      1.125      yamt 
    139      1.125      yamt #define	MCLADDREFERENCE(o, n)						\
    140      1.125      yamt do {									\
    141      1.125      yamt 	KASSERT(((o)->m_flags & M_EXT) != 0);				\
    142      1.125      yamt 	KASSERT(((n)->m_flags & M_EXT) == 0);				\
    143      1.125      yamt 	KASSERT((o)->m_ext.ext_refcnt >= 1);				\
    144      1.125      yamt 	(n)->m_flags |= ((o)->m_flags & M_EXTCOPYFLAGS);		\
    145      1.125      yamt 	atomic_inc_uint(&(o)->m_ext.ext_refcnt);			\
    146      1.125      yamt 	(n)->m_ext_ref = (o)->m_ext_ref;				\
    147      1.125      yamt 	mowner_ref((n), (n)->m_flags);					\
    148      1.125      yamt 	MCLREFDEBUGN((n), __FILE__, __LINE__);				\
    149      1.125      yamt } while (/* CONSTCOND */ 0)
    150      1.125      yamt 
    151      1.133     joerg static int
    152      1.133     joerg nmbclusters_limit(void)
    153      1.133     joerg {
    154      1.136     pooka #if defined(PMAP_MAP_POOLPAGE)
    155      1.147      para 	/* direct mapping, doesn't use space in kmem_arena */
    156      1.133     joerg 	vsize_t max_size = physmem / 4;
    157      1.133     joerg #else
    158      1.145      para 	vsize_t max_size = MIN(physmem / 4, nkmempages / 4);
    159      1.133     joerg #endif
    160      1.133     joerg 
    161      1.133     joerg 	max_size = max_size * PAGE_SIZE / MCLBYTES;
    162      1.133     joerg #ifdef NMBCLUSTERS_MAX
    163      1.133     joerg 	max_size = MIN(max_size, NMBCLUSTERS_MAX);
    164      1.133     joerg #endif
    165      1.133     joerg 
    166      1.133     joerg #ifdef NMBCLUSTERS
    167      1.133     joerg 	return MIN(max_size, NMBCLUSTERS);
    168      1.133     joerg #else
    169      1.133     joerg 	return max_size;
    170      1.133     joerg #endif
    171      1.133     joerg }
    172      1.133     joerg 
    173       1.28   thorpej /*
    174       1.68    simonb  * Initialize the mbuf allocator.
    175       1.28   thorpej  */
    176        1.4       jtc void
    177       1.62   thorpej mbinit(void)
    178        1.1       cgd {
    179       1.65   thorpej 
    180      1.128      matt 	CTASSERT(sizeof(struct _m_ext) <= MHLEN);
    181      1.128      matt 	CTASSERT(sizeof(struct mbuf) == MSIZE);
    182       1.65   thorpej 
    183      1.129     pooka 	sysctl_kern_mbuf_setup();
    184      1.129     pooka 
    185      1.122        ad 	mb_cache = pool_cache_init(msize, 0, 0, 0, "mbpl",
    186      1.122        ad 	    NULL, IPL_VM, mb_ctor, NULL, NULL);
    187      1.122        ad 	KASSERT(mb_cache != NULL);
    188      1.122        ad 
    189      1.135     rmind 	mcl_cache = pool_cache_init(mclbytes, 0, 0, 0, "mclpl", NULL,
    190      1.135     rmind 	    IPL_VM, NULL, NULL, NULL);
    191      1.122        ad 	KASSERT(mcl_cache != NULL);
    192       1.59   thorpej 
    193      1.122        ad 	pool_cache_set_drain_hook(mb_cache, m_reclaim, NULL);
    194      1.122        ad 	pool_cache_set_drain_hook(mcl_cache, m_reclaim, NULL);
    195       1.37   thorpej 
    196       1.37   thorpej 	/*
    197      1.133     joerg 	 * Set an arbitrary default limit on the number of mbuf clusters.
    198      1.133     joerg 	 */
    199      1.133     joerg #ifdef NMBCLUSTERS
    200      1.133     joerg 	nmbclusters = nmbclusters_limit();
    201      1.133     joerg #else
    202      1.133     joerg 	nmbclusters = MAX(1024,
    203      1.133     joerg 	    (vsize_t)physmem * PAGE_SIZE / MCLBYTES / 16);
    204      1.133     joerg 	nmbclusters = MIN(nmbclusters, nmbclusters_limit());
    205      1.133     joerg #endif
    206      1.133     joerg 
    207      1.133     joerg 	/*
    208       1.39   thorpej 	 * Set the hard limit on the mclpool to the number of
    209       1.39   thorpej 	 * mbuf clusters the kernel is to support.  Log the limit
    210       1.39   thorpej 	 * reached message max once a minute.
    211       1.39   thorpej 	 */
    212      1.122        ad 	pool_cache_sethardlimit(mcl_cache, nmbclusters, mclpool_warnmsg, 60);
    213       1.42   thorpej 
    214      1.124      yamt 	mbstat_percpu = percpu_alloc(sizeof(struct mbstat_cpu));
    215      1.124      yamt 
    216       1.39   thorpej 	/*
    217       1.42   thorpej 	 * Set a low water mark for both mbufs and clusters.  This should
    218       1.42   thorpej 	 * help ensure that they can be allocated in a memory starvation
    219       1.42   thorpej 	 * situation.  This is important for e.g. diskless systems which
    220       1.42   thorpej 	 * must allocate mbufs in order for the pagedaemon to clean pages.
    221       1.37   thorpej 	 */
    222      1.122        ad 	pool_cache_setlowat(mb_cache, mblowat);
    223      1.122        ad 	pool_cache_setlowat(mcl_cache, mcllowat);
    224       1.64      matt 
    225       1.64      matt #ifdef MBUFTRACE
    226       1.64      matt 	{
    227       1.64      matt 		/*
    228       1.64      matt 		 * Attach the unknown mowners.
    229       1.64      matt 		 */
    230       1.64      matt 		int i;
    231       1.64      matt 		MOWNER_ATTACH(&revoked_mowner);
    232       1.64      matt 		for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
    233       1.64      matt 		     i-- > 0; )
    234       1.64      matt 			MOWNER_ATTACH(&unknown_mowners[i]);
    235       1.64      matt 	}
    236       1.64      matt #endif
    237       1.42   thorpej }
    238       1.42   thorpej 
    239       1.75    atatat /*
    240      1.133     joerg  * sysctl helper routine for the kern.mbuf subtree.
    241      1.133     joerg  * nmbclusters, mblowat and mcllowat need range
    242       1.75    atatat  * checking and pool tweaking after being reset.
    243       1.75    atatat  */
    244       1.75    atatat static int
    245       1.75    atatat sysctl_kern_mbuf(SYSCTLFN_ARGS)
    246       1.42   thorpej {
    247       1.42   thorpej 	int error, newval;
    248       1.75    atatat 	struct sysctlnode node;
    249       1.42   thorpej 
    250       1.75    atatat 	node = *rnode;
    251       1.75    atatat 	node.sysctl_data = &newval;
    252       1.75    atatat 	switch (rnode->sysctl_num) {
    253       1.42   thorpej 	case MBUF_NMBCLUSTERS:
    254       1.42   thorpej 	case MBUF_MBLOWAT:
    255       1.42   thorpej 	case MBUF_MCLLOWAT:
    256       1.75    atatat 		newval = *(int*)rnode->sysctl_data;
    257       1.75    atatat 		break;
    258       1.75    atatat 	default:
    259       1.75    atatat 		return (EOPNOTSUPP);
    260       1.75    atatat 	}
    261       1.75    atatat 
    262       1.75    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    263       1.75    atatat 	if (error || newp == NULL)
    264       1.42   thorpej 		return (error);
    265       1.75    atatat 	if (newval < 0)
    266       1.75    atatat 		return (EINVAL);
    267       1.75    atatat 
    268       1.75    atatat 	switch (node.sysctl_num) {
    269       1.75    atatat 	case MBUF_NMBCLUSTERS:
    270       1.75    atatat 		if (newval < nmbclusters)
    271       1.75    atatat 			return (EINVAL);
    272      1.133     joerg 		if (newval > nmbclusters_limit())
    273      1.133     joerg 			return (EINVAL);
    274       1.75    atatat 		nmbclusters = newval;
    275      1.122        ad 		pool_cache_sethardlimit(mcl_cache, nmbclusters,
    276      1.122        ad 		    mclpool_warnmsg, 60);
    277       1.75    atatat 		break;
    278       1.75    atatat 	case MBUF_MBLOWAT:
    279       1.75    atatat 		mblowat = newval;
    280      1.122        ad 		pool_cache_setlowat(mb_cache, mblowat);
    281       1.75    atatat 		break;
    282       1.75    atatat 	case MBUF_MCLLOWAT:
    283       1.76    atatat 		mcllowat = newval;
    284      1.122        ad 		pool_cache_setlowat(mcl_cache, mcllowat);
    285       1.75    atatat 		break;
    286       1.75    atatat 	}
    287       1.75    atatat 
    288       1.75    atatat 	return (0);
    289       1.75    atatat }
    290       1.75    atatat 
    291       1.64      matt #ifdef MBUFTRACE
    292      1.124      yamt static void
    293      1.124      yamt mowner_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
    294      1.124      yamt {
    295      1.124      yamt 	struct mowner_counter *mc = v1;
    296      1.124      yamt 	struct mowner_user *mo_user = v2;
    297      1.124      yamt 	int i;
    298      1.124      yamt 
    299      1.124      yamt 	for (i = 0; i < MOWNER_COUNTER_NCOUNTERS; i++) {
    300      1.124      yamt 		mo_user->mo_counter[i] += mc->mc_counter[i];
    301      1.124      yamt 	}
    302      1.124      yamt }
    303      1.124      yamt 
    304      1.124      yamt static void
    305      1.124      yamt mowner_convert_to_user(struct mowner *mo, struct mowner_user *mo_user)
    306      1.124      yamt {
    307      1.124      yamt 
    308      1.124      yamt 	memset(mo_user, 0, sizeof(*mo_user));
    309      1.128      matt 	CTASSERT(sizeof(mo_user->mo_name) == sizeof(mo->mo_name));
    310      1.128      matt 	CTASSERT(sizeof(mo_user->mo_descr) == sizeof(mo->mo_descr));
    311      1.124      yamt 	memcpy(mo_user->mo_name, mo->mo_name, sizeof(mo->mo_name));
    312      1.124      yamt 	memcpy(mo_user->mo_descr, mo->mo_descr, sizeof(mo->mo_descr));
    313      1.124      yamt 	percpu_foreach(mo->mo_counters, mowner_conver_to_user_cb, mo_user);
    314      1.124      yamt }
    315      1.124      yamt 
    316       1.75    atatat static int
    317       1.75    atatat sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
    318       1.75    atatat {
    319       1.75    atatat 	struct mowner *mo;
    320       1.75    atatat 	size_t len = 0;
    321       1.75    atatat 	int error = 0;
    322       1.75    atatat 
    323       1.75    atatat 	if (namelen != 0)
    324       1.75    atatat 		return (EINVAL);
    325       1.75    atatat 	if (newp != NULL)
    326       1.75    atatat 		return (EPERM);
    327       1.75    atatat 
    328       1.75    atatat 	LIST_FOREACH(mo, &mowners, mo_link) {
    329      1.124      yamt 		struct mowner_user mo_user;
    330      1.124      yamt 
    331      1.124      yamt 		mowner_convert_to_user(mo, &mo_user);
    332      1.124      yamt 
    333       1.75    atatat 		if (oldp != NULL) {
    334      1.124      yamt 			if (*oldlenp - len < sizeof(mo_user)) {
    335       1.75    atatat 				error = ENOMEM;
    336       1.75    atatat 				break;
    337       1.75    atatat 			}
    338      1.124      yamt 			error = copyout(&mo_user, (char *)oldp + len,
    339      1.124      yamt 			    sizeof(mo_user));
    340       1.75    atatat 			if (error)
    341       1.75    atatat 				break;
    342       1.64      matt 		}
    343      1.124      yamt 		len += sizeof(mo_user);
    344       1.75    atatat 	}
    345       1.75    atatat 
    346       1.75    atatat 	if (error == 0)
    347       1.64      matt 		*oldlenp = len;
    348       1.75    atatat 
    349       1.75    atatat 	return (error);
    350       1.75    atatat }
    351       1.75    atatat #endif /* MBUFTRACE */
    352       1.75    atatat 
    353      1.124      yamt static void
    354      1.124      yamt mbstat_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
    355      1.124      yamt {
    356      1.124      yamt 	struct mbstat_cpu *mbsc = v1;
    357      1.124      yamt 	struct mbstat *mbs = v2;
    358      1.124      yamt 	int i;
    359      1.124      yamt 
    360      1.124      yamt 	for (i = 0; i < __arraycount(mbs->m_mtypes); i++) {
    361      1.124      yamt 		mbs->m_mtypes[i] += mbsc->m_mtypes[i];
    362      1.124      yamt 	}
    363      1.124      yamt }
    364      1.124      yamt 
    365      1.124      yamt static void
    366      1.124      yamt mbstat_convert_to_user(struct mbstat *mbs)
    367      1.124      yamt {
    368      1.124      yamt 
    369      1.124      yamt 	memset(mbs, 0, sizeof(*mbs));
    370      1.124      yamt 	mbs->m_drain = mbstat.m_drain;
    371      1.124      yamt 	percpu_foreach(mbstat_percpu, mbstat_conver_to_user_cb, mbs);
    372      1.124      yamt }
    373      1.124      yamt 
    374      1.124      yamt static int
    375      1.124      yamt sysctl_kern_mbuf_stats(SYSCTLFN_ARGS)
    376      1.124      yamt {
    377      1.124      yamt 	struct sysctlnode node;
    378      1.124      yamt 	struct mbstat mbs;
    379      1.124      yamt 
    380      1.124      yamt 	mbstat_convert_to_user(&mbs);
    381      1.124      yamt 	node = *rnode;
    382      1.124      yamt 	node.sysctl_data = &mbs;
    383      1.124      yamt 	node.sysctl_size = sizeof(mbs);
    384      1.124      yamt 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    385      1.124      yamt }
    386      1.124      yamt 
    387      1.129     pooka static void
    388      1.131    cegger sysctl_kern_mbuf_setup(void)
    389       1.75    atatat {
    390       1.75    atatat 
    391      1.129     pooka 	KASSERT(mbuf_sysctllog == NULL);
    392      1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    393       1.80    atatat 		       CTLFLAG_PERMANENT,
    394       1.82    atatat 		       CTLTYPE_NODE, "mbuf",
    395       1.82    atatat 		       SYSCTL_DESCR("mbuf control variables"),
    396       1.75    atatat 		       NULL, 0, NULL, 0,
    397       1.75    atatat 		       CTL_KERN, KERN_MBUF, CTL_EOL);
    398       1.75    atatat 
    399      1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    400       1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    401       1.82    atatat 		       CTLTYPE_INT, "msize",
    402       1.82    atatat 		       SYSCTL_DESCR("mbuf base size"),
    403       1.75    atatat 		       NULL, msize, NULL, 0,
    404       1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
    405      1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    406       1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    407       1.82    atatat 		       CTLTYPE_INT, "mclbytes",
    408       1.82    atatat 		       SYSCTL_DESCR("mbuf cluster size"),
    409       1.75    atatat 		       NULL, mclbytes, NULL, 0,
    410       1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
    411      1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    412       1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    413       1.82    atatat 		       CTLTYPE_INT, "nmbclusters",
    414       1.82    atatat 		       SYSCTL_DESCR("Limit on the number of mbuf clusters"),
    415       1.75    atatat 		       sysctl_kern_mbuf, 0, &nmbclusters, 0,
    416       1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
    417      1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    418       1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    419       1.82    atatat 		       CTLTYPE_INT, "mblowat",
    420       1.82    atatat 		       SYSCTL_DESCR("mbuf low water mark"),
    421       1.75    atatat 		       sysctl_kern_mbuf, 0, &mblowat, 0,
    422       1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
    423      1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    424       1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    425       1.82    atatat 		       CTLTYPE_INT, "mcllowat",
    426       1.82    atatat 		       SYSCTL_DESCR("mbuf cluster low water mark"),
    427       1.75    atatat 		       sysctl_kern_mbuf, 0, &mcllowat, 0,
    428       1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
    429      1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    430       1.80    atatat 		       CTLFLAG_PERMANENT,
    431       1.82    atatat 		       CTLTYPE_STRUCT, "stats",
    432       1.82    atatat 		       SYSCTL_DESCR("mbuf allocation statistics"),
    433      1.124      yamt 		       sysctl_kern_mbuf_stats, 0, NULL, 0,
    434       1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
    435       1.75    atatat #ifdef MBUFTRACE
    436      1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    437       1.80    atatat 		       CTLFLAG_PERMANENT,
    438       1.82    atatat 		       CTLTYPE_STRUCT, "mowners",
    439       1.82    atatat 		       SYSCTL_DESCR("Information about mbuf owners"),
    440       1.75    atatat 		       sysctl_kern_mbuf_mowners, 0, NULL, 0,
    441       1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
    442       1.75    atatat #endif /* MBUFTRACE */
    443       1.28   thorpej }
    444       1.28   thorpej 
    445       1.65   thorpej static int
    446      1.116      yamt mb_ctor(void *arg, void *object, int flags)
    447       1.65   thorpej {
    448       1.65   thorpej 	struct mbuf *m = object;
    449       1.65   thorpej 
    450       1.65   thorpej #ifdef POOL_VTOPHYS
    451       1.65   thorpej 	m->m_paddr = POOL_VTOPHYS(m);
    452       1.65   thorpej #else
    453       1.65   thorpej 	m->m_paddr = M_PADDR_INVALID;
    454       1.65   thorpej #endif
    455       1.65   thorpej 	return (0);
    456        1.1       cgd }
    457        1.1       cgd 
    458  1.181.2.1  pgoyette void
    459  1.181.2.1  pgoyette m_pkthdr_remove(struct mbuf *m)
    460  1.181.2.1  pgoyette {
    461  1.181.2.1  pgoyette 	KASSERT(m->m_flags & M_PKTHDR);
    462  1.181.2.1  pgoyette 
    463  1.181.2.4  pgoyette 	if (M_READONLY(m)) {
    464  1.181.2.4  pgoyette 		/* Nothing we can do. */
    465  1.181.2.4  pgoyette 		return;
    466  1.181.2.4  pgoyette 	}
    467  1.181.2.4  pgoyette 
    468  1.181.2.1  pgoyette 	m_tag_delete_chain(m, NULL);
    469  1.181.2.1  pgoyette 	m->m_flags &= ~M_PKTHDR;
    470  1.181.2.1  pgoyette 	memset(&m->m_pkthdr, 0, sizeof(m->m_pkthdr));
    471  1.181.2.1  pgoyette }
    472  1.181.2.1  pgoyette 
    473      1.150  christos /*
    474      1.150  christos  * Add mbuf to the end of a chain
    475      1.150  christos  */
    476      1.150  christos struct mbuf *
    477      1.179      maxv m_add(struct mbuf *c, struct mbuf *m)
    478      1.179      maxv {
    479      1.150  christos 	struct mbuf *n;
    480      1.150  christos 
    481      1.150  christos 	if (c == NULL)
    482      1.150  christos 		return m;
    483      1.150  christos 
    484      1.150  christos 	for (n = c; n->m_next != NULL; n = n->m_next)
    485      1.150  christos 		continue;
    486      1.150  christos 	n->m_next = m;
    487      1.150  christos 	return c;
    488      1.150  christos }
    489      1.150  christos 
    490      1.150  christos /*
    491      1.150  christos  * Set the m_data pointer of a newly-allocated mbuf
    492      1.150  christos  * to place an object of the specified size at the
    493      1.150  christos  * end of the mbuf, longword aligned.
    494      1.150  christos  */
    495      1.150  christos void
    496      1.150  christos m_align(struct mbuf *m, int len)
    497      1.150  christos {
    498      1.156  christos 	int adjust;
    499      1.150  christos 
    500      1.156  christos 	KASSERT(len != M_COPYALL);
    501      1.156  christos 
    502      1.156  christos 	if (m->m_flags & M_EXT)
    503      1.156  christos 		adjust = m->m_ext.ext_size - len;
    504      1.156  christos 	else if (m->m_flags & M_PKTHDR)
    505      1.156  christos 		adjust = MHLEN - len;
    506      1.156  christos 	else
    507      1.156  christos 		adjust = MLEN - len;
    508      1.156  christos 	m->m_data += adjust &~ (sizeof(long)-1);
    509      1.150  christos }
    510      1.150  christos 
    511      1.150  christos /*
    512      1.150  christos  * Append the specified data to the indicated mbuf chain,
    513      1.150  christos  * Extend the mbuf chain if the new data does not fit in
    514      1.150  christos  * existing space.
    515      1.150  christos  *
    516      1.150  christos  * Return 1 if able to complete the job; otherwise 0.
    517      1.150  christos  */
    518      1.150  christos int
    519      1.150  christos m_append(struct mbuf *m0, int len, const void *cpv)
    520      1.150  christos {
    521      1.150  christos 	struct mbuf *m, *n;
    522      1.150  christos 	int remainder, space;
    523      1.150  christos 	const char *cp = cpv;
    524      1.150  christos 
    525      1.156  christos 	KASSERT(len != M_COPYALL);
    526      1.150  christos 	for (m = m0; m->m_next != NULL; m = m->m_next)
    527      1.150  christos 		continue;
    528      1.150  christos 	remainder = len;
    529      1.150  christos 	space = M_TRAILINGSPACE(m);
    530      1.150  christos 	if (space > 0) {
    531      1.150  christos 		/*
    532      1.150  christos 		 * Copy into available space.
    533      1.150  christos 		 */
    534      1.150  christos 		if (space > remainder)
    535      1.150  christos 			space = remainder;
    536      1.150  christos 		memmove(mtod(m, char *) + m->m_len, cp, space);
    537      1.150  christos 		m->m_len += space;
    538      1.150  christos 		cp = cp + space, remainder -= space;
    539      1.150  christos 	}
    540      1.150  christos 	while (remainder > 0) {
    541      1.150  christos 		/*
    542      1.150  christos 		 * Allocate a new mbuf; could check space
    543      1.150  christos 		 * and allocate a cluster instead.
    544      1.150  christos 		 */
    545      1.150  christos 		n = m_get(M_DONTWAIT, m->m_type);
    546      1.150  christos 		if (n == NULL)
    547      1.150  christos 			break;
    548      1.150  christos 		n->m_len = min(MLEN, remainder);
    549      1.150  christos 		memmove(mtod(n, void *), cp, n->m_len);
    550      1.150  christos 		cp += n->m_len, remainder -= n->m_len;
    551      1.150  christos 		m->m_next = n;
    552      1.150  christos 		m = n;
    553      1.150  christos 	}
    554      1.150  christos 	if (m0->m_flags & M_PKTHDR)
    555      1.150  christos 		m0->m_pkthdr.len += len - remainder;
    556      1.150  christos 	return (remainder == 0);
    557      1.150  christos }
    558      1.150  christos 
    559       1.14  christos void
    560      1.116      yamt m_reclaim(void *arg, int flags)
    561        1.1       cgd {
    562       1.27      matt 	struct domain *dp;
    563       1.81      matt 	const struct protosw *pr;
    564      1.160     ozaki 	struct ifnet *ifp;
    565      1.122        ad 	int s;
    566        1.1       cgd 
    567      1.122        ad 	KERNEL_LOCK(1, NULL);
    568      1.122        ad 	s = splvm();
    569       1.91      matt 	DOMAIN_FOREACH(dp) {
    570       1.33   thorpej 		for (pr = dp->dom_protosw;
    571       1.33   thorpej 		     pr < dp->dom_protoswNPROTOSW; pr++)
    572       1.33   thorpej 			if (pr->pr_drain)
    573       1.33   thorpej 				(*pr->pr_drain)();
    574       1.91      matt 	}
    575      1.165     ozaki 	/* XXX we cannot use psref in H/W interrupt */
    576      1.165     ozaki 	if (!cpu_intr_p()) {
    577      1.168     ozaki 		int bound = curlwp_bind();
    578      1.165     ozaki 		IFNET_READER_FOREACH(ifp) {
    579      1.165     ozaki 			struct psref psref;
    580      1.165     ozaki 
    581      1.171     ozaki 			if_acquire(ifp, &psref);
    582      1.165     ozaki 
    583      1.165     ozaki 			if (ifp->if_drain)
    584      1.165     ozaki 				(*ifp->if_drain)(ifp);
    585      1.165     ozaki 
    586      1.171     ozaki 			if_release(ifp, &psref);
    587      1.165     ozaki 		}
    588      1.168     ozaki 		curlwp_bindx(bound);
    589      1.160     ozaki 	}
    590        1.1       cgd 	splx(s);
    591        1.1       cgd 	mbstat.m_drain++;
    592      1.122        ad 	KERNEL_UNLOCK_ONE(NULL);
    593        1.1       cgd }
    594        1.1       cgd 
    595        1.1       cgd /*
    596        1.1       cgd  * Space allocation routines.
    597        1.1       cgd  * These are also available as macros
    598        1.1       cgd  * for critical paths.
    599        1.1       cgd  */
    600        1.1       cgd struct mbuf *
    601       1.62   thorpej m_get(int nowait, int type)
    602        1.1       cgd {
    603       1.27      matt 	struct mbuf *m;
    604        1.1       cgd 
    605      1.142    dyoung 	KASSERT(type != MT_FREE);
    606      1.142    dyoung 
    607      1.124      yamt 	m = pool_cache_get(mb_cache,
    608      1.173  christos 	    nowait == M_WAIT ? PR_WAITOK|PR_LIMITFAIL : PR_NOWAIT);
    609      1.124      yamt 	if (m == NULL)
    610      1.124      yamt 		return NULL;
    611      1.124      yamt 
    612      1.124      yamt 	mbstat_type_add(type, 1);
    613      1.164  knakahar 
    614  1.181.2.2  pgoyette 	mowner_init(m, type);
    615  1.181.2.2  pgoyette 	m->m_ext_ref = m; /* default */
    616  1.181.2.2  pgoyette 	m->m_type = type;
    617  1.181.2.2  pgoyette 	m->m_len = 0;
    618  1.181.2.2  pgoyette 	m->m_next = NULL;
    619  1.181.2.2  pgoyette 	m->m_nextpkt = NULL; /* default */
    620  1.181.2.2  pgoyette 	m->m_data = m->m_dat;
    621  1.181.2.2  pgoyette 	m->m_flags = 0; /* default */
    622      1.124      yamt 
    623      1.124      yamt 	return m;
    624        1.1       cgd }
    625        1.1       cgd 
    626        1.1       cgd struct mbuf *
    627       1.62   thorpej m_gethdr(int nowait, int type)
    628        1.1       cgd {
    629       1.27      matt 	struct mbuf *m;
    630        1.1       cgd 
    631      1.124      yamt 	m = m_get(nowait, type);
    632      1.124      yamt 	if (m == NULL)
    633      1.124      yamt 		return NULL;
    634      1.124      yamt 
    635  1.181.2.2  pgoyette 	m->m_data = m->m_pktdat;
    636  1.181.2.2  pgoyette 	m->m_flags = M_PKTHDR;
    637  1.181.2.2  pgoyette 
    638  1.181.2.2  pgoyette 	m_reset_rcvif(m);
    639  1.181.2.2  pgoyette 	m->m_pkthdr.len = 0;
    640  1.181.2.2  pgoyette 	m->m_pkthdr.csum_flags = 0;
    641  1.181.2.2  pgoyette 	m->m_pkthdr.csum_data = 0;
    642  1.181.2.2  pgoyette 	SLIST_INIT(&m->m_pkthdr.tags);
    643  1.181.2.2  pgoyette 
    644  1.181.2.2  pgoyette 	m->m_pkthdr.pattr_class = NULL;
    645  1.181.2.2  pgoyette 	m->m_pkthdr.pattr_af = AF_UNSPEC;
    646  1.181.2.2  pgoyette 	m->m_pkthdr.pattr_hdr = NULL;
    647      1.124      yamt 
    648      1.124      yamt 	return m;
    649        1.1       cgd }
    650        1.1       cgd 
    651       1.64      matt void
    652       1.64      matt m_clget(struct mbuf *m, int nowait)
    653       1.64      matt {
    654       1.71    simonb 
    655       1.64      matt 	MCLGET(m, nowait);
    656       1.64      matt }
    657       1.64      matt 
    658       1.64      matt #ifdef MBUFTRACE
    659       1.83  jonathan /*
    660       1.83  jonathan  * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
    661       1.83  jonathan  */
    662       1.64      matt void
    663       1.83  jonathan m_claimm(struct mbuf *m, struct mowner *mo)
    664       1.64      matt {
    665       1.71    simonb 
    666       1.64      matt 	for (; m != NULL; m = m->m_next)
    667       1.64      matt 		MCLAIM(m, mo);
    668       1.64      matt }
    669       1.64      matt #endif
    670       1.64      matt 
    671        1.1       cgd /*
    672        1.1       cgd  * Mbuffer utility routines.
    673        1.1       cgd  */
    674        1.1       cgd 
    675        1.1       cgd /*
    676        1.1       cgd  * Lesser-used path for M_PREPEND:
    677        1.1       cgd  * allocate new mbuf to prepend to chain,
    678        1.1       cgd  * copy junk along.
    679        1.1       cgd  */
    680        1.1       cgd struct mbuf *
    681       1.62   thorpej m_prepend(struct mbuf *m, int len, int how)
    682        1.1       cgd {
    683        1.1       cgd 	struct mbuf *mn;
    684        1.1       cgd 
    685      1.180      maxv 	if (__predict_false(len > MHLEN)) {
    686      1.180      maxv 		panic("%s: len > MHLEN", __func__);
    687      1.180      maxv 	}
    688      1.180      maxv 
    689      1.156  christos 	KASSERT(len != M_COPYALL);
    690      1.153  christos 	mn = m_get(how, m->m_type);
    691      1.143    plunky 	if (mn == NULL) {
    692        1.1       cgd 		m_freem(m);
    693      1.179      maxv 		return NULL;
    694        1.1       cgd 	}
    695      1.178      maxv 
    696        1.1       cgd 	if (m->m_flags & M_PKTHDR) {
    697      1.101      yamt 		M_MOVE_PKTHDR(mn, m);
    698       1.64      matt 	} else {
    699       1.64      matt 		MCLAIM(mn, m->m_owner);
    700        1.1       cgd 	}
    701        1.1       cgd 	mn->m_next = m;
    702        1.1       cgd 	m = mn;
    703      1.178      maxv 
    704      1.178      maxv 	if (m->m_flags & M_PKTHDR) {
    705      1.178      maxv 		if (len < MHLEN)
    706      1.178      maxv 			MH_ALIGN(m, len);
    707      1.178      maxv 	} else {
    708      1.178      maxv 		if (len < MLEN)
    709      1.178      maxv 			M_ALIGN(m, len);
    710      1.178      maxv 	}
    711      1.178      maxv 
    712        1.1       cgd 	m->m_len = len;
    713      1.179      maxv 	return m;
    714        1.1       cgd }
    715        1.1       cgd 
    716        1.1       cgd /*
    717        1.1       cgd  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    718        1.1       cgd  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    719        1.1       cgd  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    720        1.1       cgd  */
    721        1.1       cgd struct mbuf *
    722       1.62   thorpej m_copym(struct mbuf *m, int off0, int len, int wait)
    723        1.1       cgd {
    724       1.71    simonb 
    725      1.179      maxv 	return m_copym0(m, off0, len, wait, false); /* shallow copy on M_EXT */
    726       1.44    itojun }
    727       1.44    itojun 
    728       1.44    itojun struct mbuf *
    729       1.62   thorpej m_dup(struct mbuf *m, int off0, int len, int wait)
    730       1.44    itojun {
    731       1.71    simonb 
    732      1.179      maxv 	return m_copym0(m, off0, len, wait, true); /* deep copy */
    733       1.44    itojun }
    734       1.44    itojun 
    735      1.154  christos static inline int
    736      1.179      maxv m_copylen(int len, int copylen)
    737      1.179      maxv {
    738      1.179      maxv 	return (len == M_COPYALL) ? copylen : min(len, copylen);
    739      1.154  christos }
    740      1.154  christos 
    741       1.44    itojun static struct mbuf *
    742      1.179      maxv m_copym0(struct mbuf *m, int off0, int len, int wait, bool deep)
    743       1.44    itojun {
    744       1.27      matt 	struct mbuf *n, **np;
    745       1.27      matt 	int off = off0;
    746        1.1       cgd 	struct mbuf *top;
    747        1.1       cgd 	int copyhdr = 0;
    748        1.1       cgd 
    749      1.154  christos 	if (off < 0 || (len != M_COPYALL && len < 0))
    750       1.43   thorpej 		panic("m_copym: off %d, len %d", off, len);
    751        1.1       cgd 	if (off == 0 && m->m_flags & M_PKTHDR)
    752        1.1       cgd 		copyhdr = 1;
    753        1.1       cgd 	while (off > 0) {
    754      1.179      maxv 		if (m == NULL)
    755       1.90      matt 			panic("m_copym: m == 0, off %d", off);
    756        1.1       cgd 		if (off < m->m_len)
    757        1.1       cgd 			break;
    758        1.1       cgd 		off -= m->m_len;
    759        1.1       cgd 		m = m->m_next;
    760        1.1       cgd 	}
    761      1.179      maxv 
    762        1.1       cgd 	np = &top;
    763      1.179      maxv 	top = NULL;
    764      1.155     skrll 	while (len == M_COPYALL || len > 0) {
    765      1.179      maxv 		if (m == NULL) {
    766        1.1       cgd 			if (len != M_COPYALL)
    767       1.90      matt 				panic("m_copym: m == 0, len %d [!COPYALL]",
    768       1.90      matt 				    len);
    769        1.1       cgd 			break;
    770        1.1       cgd 		}
    771      1.179      maxv 
    772      1.153  christos 		n = m_get(wait, m->m_type);
    773        1.1       cgd 		*np = n;
    774      1.179      maxv 		if (n == NULL)
    775        1.1       cgd 			goto nospace;
    776       1.64      matt 		MCLAIM(n, m->m_owner);
    777      1.179      maxv 
    778        1.1       cgd 		if (copyhdr) {
    779        1.1       cgd 			M_COPY_PKTHDR(n, m);
    780        1.1       cgd 			if (len == M_COPYALL)
    781        1.1       cgd 				n->m_pkthdr.len -= off0;
    782        1.1       cgd 			else
    783        1.1       cgd 				n->m_pkthdr.len = len;
    784        1.1       cgd 			copyhdr = 0;
    785        1.1       cgd 		}
    786      1.154  christos 		n->m_len = m_copylen(len, m->m_len - off);
    787      1.179      maxv 
    788        1.1       cgd 		if (m->m_flags & M_EXT) {
    789       1.44    itojun 			if (!deep) {
    790       1.44    itojun 				n->m_data = m->m_data + off;
    791       1.44    itojun 				MCLADDREFERENCE(m, n);
    792       1.44    itojun 			} else {
    793       1.48    itojun 				/*
    794      1.181      maxv 				 * We don't care if MCLGET fails. n->m_len is
    795      1.181      maxv 				 * recomputed and handles that.
    796       1.48    itojun 				 */
    797       1.44    itojun 				MCLGET(n, wait);
    798      1.161   mlelstv 				n->m_len = 0;
    799       1.50    itojun 				n->m_len = M_TRAILINGSPACE(n);
    800      1.154  christos 				n->m_len = m_copylen(len, n->m_len);
    801       1.50    itojun 				n->m_len = min(n->m_len, m->m_len - off);
    802      1.119  christos 				memcpy(mtod(n, void *), mtod(m, char *) + off,
    803       1.44    itojun 				    (unsigned)n->m_len);
    804       1.44    itojun 			}
    805      1.179      maxv 		} else {
    806      1.119  christos 			memcpy(mtod(n, void *), mtod(m, char *) + off,
    807        1.1       cgd 			    (unsigned)n->m_len);
    808      1.179      maxv 		}
    809      1.179      maxv 
    810        1.1       cgd 		if (len != M_COPYALL)
    811        1.1       cgd 			len -= n->m_len;
    812       1.50    itojun 		off += n->m_len;
    813       1.50    itojun #ifdef DIAGNOSTIC
    814       1.50    itojun 		if (off > m->m_len)
    815      1.149     pooka 			panic("m_copym0 overrun %d %d", off, m->m_len);
    816       1.50    itojun #endif
    817       1.50    itojun 		if (off == m->m_len) {
    818       1.50    itojun 			m = m->m_next;
    819       1.50    itojun 			off = 0;
    820       1.50    itojun 		}
    821        1.1       cgd 		np = &n->m_next;
    822        1.1       cgd 	}
    823      1.179      maxv 
    824      1.179      maxv 	return top;
    825      1.179      maxv 
    826        1.1       cgd nospace:
    827        1.1       cgd 	m_freem(top);
    828      1.179      maxv 	return NULL;
    829        1.1       cgd }
    830        1.1       cgd 
    831        1.1       cgd /*
    832       1.18   thorpej  * Copy an entire packet, including header (which must be present).
    833      1.181      maxv  * An optimization of the common case 'm_copym(m, 0, M_COPYALL, how)'.
    834       1.18   thorpej  */
    835       1.18   thorpej struct mbuf *
    836       1.62   thorpej m_copypacket(struct mbuf *m, int how)
    837       1.18   thorpej {
    838       1.18   thorpej 	struct mbuf *top, *n, *o;
    839       1.18   thorpej 
    840      1.153  christos 	n = m_get(how, m->m_type);
    841       1.18   thorpej 	top = n;
    842       1.18   thorpej 	if (!n)
    843       1.18   thorpej 		goto nospace;
    844       1.18   thorpej 
    845       1.64      matt 	MCLAIM(n, m->m_owner);
    846       1.18   thorpej 	M_COPY_PKTHDR(n, m);
    847       1.18   thorpej 	n->m_len = m->m_len;
    848       1.18   thorpej 	if (m->m_flags & M_EXT) {
    849       1.18   thorpej 		n->m_data = m->m_data;
    850       1.18   thorpej 		MCLADDREFERENCE(m, n);
    851       1.18   thorpej 	} else {
    852       1.30     perry 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    853       1.18   thorpej 	}
    854       1.18   thorpej 
    855       1.18   thorpej 	m = m->m_next;
    856       1.18   thorpej 	while (m) {
    857      1.153  christos 		o = m_get(how, m->m_type);
    858       1.18   thorpej 		if (!o)
    859       1.18   thorpej 			goto nospace;
    860       1.18   thorpej 
    861       1.64      matt 		MCLAIM(o, m->m_owner);
    862       1.18   thorpej 		n->m_next = o;
    863       1.18   thorpej 		n = n->m_next;
    864       1.18   thorpej 
    865       1.18   thorpej 		n->m_len = m->m_len;
    866       1.18   thorpej 		if (m->m_flags & M_EXT) {
    867       1.18   thorpej 			n->m_data = m->m_data;
    868       1.18   thorpej 			MCLADDREFERENCE(m, n);
    869       1.18   thorpej 		} else {
    870       1.30     perry 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    871       1.18   thorpej 		}
    872       1.18   thorpej 
    873       1.18   thorpej 		m = m->m_next;
    874       1.18   thorpej 	}
    875       1.18   thorpej 	return top;
    876      1.181      maxv 
    877       1.18   thorpej nospace:
    878       1.18   thorpej 	m_freem(top);
    879       1.71    simonb 	return NULL;
    880       1.18   thorpej }
    881       1.18   thorpej 
    882       1.18   thorpej /*
    883        1.1       cgd  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    884        1.1       cgd  * continuing for "len" bytes, into the indicated buffer.
    885        1.1       cgd  */
    886       1.14  christos void
    887      1.100    martin m_copydata(struct mbuf *m, int off, int len, void *vp)
    888        1.1       cgd {
    889      1.179      maxv 	unsigned count;
    890      1.179      maxv 	void *cp = vp;
    891      1.179      maxv 	struct mbuf *m0 = m;
    892      1.179      maxv 	int len0 = len;
    893      1.179      maxv 	int off0 = off;
    894      1.179      maxv 	void *vp0 = vp;
    895        1.1       cgd 
    896      1.156  christos 	KASSERT(len != M_COPYALL);
    897        1.1       cgd 	if (off < 0 || len < 0)
    898       1.90      matt 		panic("m_copydata: off %d, len %d", off, len);
    899        1.1       cgd 	while (off > 0) {
    900       1.94      tron 		if (m == NULL)
    901      1.151      matt 			panic("m_copydata(%p,%d,%d,%p): m=NULL, off=%d (%d)",
    902      1.151      matt 			    m0, len0, off0, vp0, off, off0 - off);
    903        1.1       cgd 		if (off < m->m_len)
    904        1.1       cgd 			break;
    905        1.1       cgd 		off -= m->m_len;
    906        1.1       cgd 		m = m->m_next;
    907        1.1       cgd 	}
    908        1.1       cgd 	while (len > 0) {
    909       1.94      tron 		if (m == NULL)
    910      1.151      matt 			panic("m_copydata(%p,%d,%d,%p): "
    911      1.151      matt 			    "m=NULL, off=%d (%d), len=%d (%d)",
    912      1.151      matt 			    m0, len0, off0, vp0,
    913      1.151      matt 			    off, off0 - off, len, len0 - len);
    914        1.9   mycroft 		count = min(m->m_len - off, len);
    915      1.119  christos 		memcpy(cp, mtod(m, char *) + off, count);
    916        1.1       cgd 		len -= count;
    917      1.119  christos 		cp = (char *)cp + count;
    918        1.1       cgd 		off = 0;
    919        1.1       cgd 		m = m->m_next;
    920        1.1       cgd 	}
    921        1.1       cgd }
    922        1.1       cgd 
    923        1.1       cgd /*
    924        1.1       cgd  * Concatenate mbuf chain n to m.
    925       1.72    itojun  * n might be copied into m (when n->m_len is small), therefore data portion of
    926       1.72    itojun  * n could be copied into an mbuf of different mbuf type.
    927        1.1       cgd  * Any m_pkthdr is not updated.
    928        1.1       cgd  */
    929       1.14  christos void
    930       1.62   thorpej m_cat(struct mbuf *m, struct mbuf *n)
    931        1.1       cgd {
    932       1.73      yamt 
    933        1.1       cgd 	while (m->m_next)
    934        1.1       cgd 		m = m->m_next;
    935        1.1       cgd 	while (n) {
    936       1.77    itojun 		if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
    937        1.1       cgd 			/* just join the two chains */
    938        1.1       cgd 			m->m_next = n;
    939        1.1       cgd 			return;
    940        1.1       cgd 		}
    941        1.1       cgd 		/* splat the data from one into the other */
    942      1.119  christos 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
    943        1.1       cgd 		    (u_int)n->m_len);
    944        1.1       cgd 		m->m_len += n->m_len;
    945        1.1       cgd 		n = m_free(n);
    946        1.1       cgd 	}
    947        1.1       cgd }
    948        1.1       cgd 
    949       1.11   mycroft void
    950       1.62   thorpej m_adj(struct mbuf *mp, int req_len)
    951        1.1       cgd {
    952       1.27      matt 	int len = req_len;
    953       1.27      matt 	struct mbuf *m;
    954       1.27      matt 	int count;
    955        1.1       cgd 
    956        1.1       cgd 	if ((m = mp) == NULL)
    957        1.1       cgd 		return;
    958        1.1       cgd 	if (len >= 0) {
    959        1.1       cgd 		/*
    960        1.1       cgd 		 * Trim from head.
    961        1.1       cgd 		 */
    962        1.1       cgd 		while (m != NULL && len > 0) {
    963        1.1       cgd 			if (m->m_len <= len) {
    964        1.1       cgd 				len -= m->m_len;
    965        1.1       cgd 				m->m_len = 0;
    966        1.1       cgd 				m = m->m_next;
    967        1.1       cgd 			} else {
    968        1.1       cgd 				m->m_len -= len;
    969        1.1       cgd 				m->m_data += len;
    970        1.1       cgd 				len = 0;
    971        1.1       cgd 			}
    972        1.1       cgd 		}
    973        1.1       cgd 		if (mp->m_flags & M_PKTHDR)
    974      1.181      maxv 			mp->m_pkthdr.len -= (req_len - len);
    975        1.1       cgd 	} else {
    976        1.1       cgd 		/*
    977        1.1       cgd 		 * Trim from tail.  Scan the mbuf chain,
    978        1.1       cgd 		 * calculating its length and finding the last mbuf.
    979        1.1       cgd 		 * If the adjustment only affects this mbuf, then just
    980        1.1       cgd 		 * adjust and return.  Otherwise, rescan and truncate
    981        1.1       cgd 		 * after the remaining size.
    982        1.1       cgd 		 */
    983        1.1       cgd 		len = -len;
    984        1.1       cgd 		count = 0;
    985        1.1       cgd 		for (;;) {
    986        1.1       cgd 			count += m->m_len;
    987      1.181      maxv 			if (m->m_next == NULL)
    988        1.1       cgd 				break;
    989        1.1       cgd 			m = m->m_next;
    990        1.1       cgd 		}
    991        1.1       cgd 		if (m->m_len >= len) {
    992        1.1       cgd 			m->m_len -= len;
    993        1.8   deraadt 			if (mp->m_flags & M_PKTHDR)
    994        1.8   deraadt 				mp->m_pkthdr.len -= len;
    995        1.1       cgd 			return;
    996        1.1       cgd 		}
    997      1.181      maxv 
    998        1.1       cgd 		count -= len;
    999        1.1       cgd 		if (count < 0)
   1000        1.1       cgd 			count = 0;
   1001      1.181      maxv 
   1002        1.1       cgd 		/*
   1003        1.1       cgd 		 * Correct length for chain is "count".
   1004        1.1       cgd 		 * Find the mbuf with last data, adjust its length,
   1005        1.1       cgd 		 * and toss data from remaining mbufs on chain.
   1006        1.1       cgd 		 */
   1007        1.1       cgd 		m = mp;
   1008        1.1       cgd 		if (m->m_flags & M_PKTHDR)
   1009        1.1       cgd 			m->m_pkthdr.len = count;
   1010        1.1       cgd 		for (; m; m = m->m_next) {
   1011        1.1       cgd 			if (m->m_len >= count) {
   1012        1.1       cgd 				m->m_len = count;
   1013        1.1       cgd 				break;
   1014        1.1       cgd 			}
   1015        1.1       cgd 			count -= m->m_len;
   1016        1.1       cgd 		}
   1017      1.181      maxv 		if (m) {
   1018      1.110  christos 			while (m->m_next)
   1019      1.110  christos 				(m = m->m_next)->m_len = 0;
   1020      1.181      maxv 		}
   1021        1.1       cgd 	}
   1022        1.1       cgd }
   1023        1.1       cgd 
   1024        1.1       cgd /*
   1025      1.148     rmind  * m_ensure_contig: rearrange an mbuf chain that given length of bytes
   1026      1.148     rmind  * would be contiguous and in the data area of an mbuf (therefore, mtod()
   1027      1.148     rmind  * would work for a structure of given length).
   1028      1.148     rmind  *
   1029      1.148     rmind  * => On success, returns true and the resulting mbuf chain; false otherwise.
   1030      1.148     rmind  * => The mbuf chain may change, but is always preserved valid.
   1031        1.1       cgd  */
   1032      1.148     rmind bool
   1033      1.148     rmind m_ensure_contig(struct mbuf **m0, int len)
   1034        1.1       cgd {
   1035      1.148     rmind 	struct mbuf *n = *m0, *m;
   1036      1.148     rmind 	size_t count, space;
   1037        1.1       cgd 
   1038      1.156  christos 	KASSERT(len != M_COPYALL);
   1039        1.1       cgd 	/*
   1040        1.1       cgd 	 * If first mbuf has no cluster, and has room for len bytes
   1041        1.1       cgd 	 * without shifting current data, pullup into it,
   1042        1.1       cgd 	 * otherwise allocate a new mbuf to prepend to the chain.
   1043        1.1       cgd 	 */
   1044        1.1       cgd 	if ((n->m_flags & M_EXT) == 0 &&
   1045        1.1       cgd 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
   1046      1.148     rmind 		if (n->m_len >= len) {
   1047      1.148     rmind 			return true;
   1048      1.148     rmind 		}
   1049        1.1       cgd 		m = n;
   1050        1.1       cgd 		n = n->m_next;
   1051        1.1       cgd 		len -= m->m_len;
   1052        1.1       cgd 	} else {
   1053      1.148     rmind 		if (len > MHLEN) {
   1054      1.148     rmind 			return false;
   1055      1.148     rmind 		}
   1056      1.153  christos 		m = m_get(M_DONTWAIT, n->m_type);
   1057      1.148     rmind 		if (m == NULL) {
   1058      1.148     rmind 			return false;
   1059      1.148     rmind 		}
   1060       1.64      matt 		MCLAIM(m, n->m_owner);
   1061        1.1       cgd 		if (n->m_flags & M_PKTHDR) {
   1062      1.101      yamt 			M_MOVE_PKTHDR(m, n);
   1063        1.1       cgd 		}
   1064        1.1       cgd 	}
   1065        1.1       cgd 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
   1066        1.1       cgd 	do {
   1067      1.148     rmind 		count = MIN(MIN(MAX(len, max_protohdr), space), n->m_len);
   1068      1.119  christos 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
   1069        1.1       cgd 		  (unsigned)count);
   1070        1.1       cgd 		len -= count;
   1071        1.1       cgd 		m->m_len += count;
   1072        1.1       cgd 		n->m_len -= count;
   1073        1.1       cgd 		space -= count;
   1074        1.1       cgd 		if (n->m_len)
   1075        1.1       cgd 			n->m_data += count;
   1076        1.1       cgd 		else
   1077        1.1       cgd 			n = m_free(n);
   1078        1.1       cgd 	} while (len > 0 && n);
   1079      1.148     rmind 
   1080      1.148     rmind 	m->m_next = n;
   1081      1.148     rmind 	*m0 = m;
   1082      1.148     rmind 
   1083      1.148     rmind 	return len <= 0;
   1084      1.148     rmind }
   1085      1.148     rmind 
   1086      1.148     rmind /*
   1087      1.148     rmind  * m_pullup: same as m_ensure_contig(), but destroys mbuf chain on error.
   1088      1.148     rmind  */
   1089      1.148     rmind struct mbuf *
   1090      1.148     rmind m_pullup(struct mbuf *n, int len)
   1091      1.148     rmind {
   1092      1.148     rmind 	struct mbuf *m = n;
   1093      1.148     rmind 
   1094      1.156  christos 	KASSERT(len != M_COPYALL);
   1095      1.148     rmind 	if (!m_ensure_contig(&m, len)) {
   1096      1.148     rmind 		KASSERT(m != NULL);
   1097      1.148     rmind 		m_freem(m);
   1098      1.148     rmind 		m = NULL;
   1099        1.1       cgd 	}
   1100      1.148     rmind 	return m;
   1101       1.60   thorpej }
   1102       1.60   thorpej 
   1103       1.60   thorpej /*
   1104       1.60   thorpej  * Like m_pullup(), except a new mbuf is always allocated, and we allow
   1105       1.60   thorpej  * the amount of empty space before the data in the new mbuf to be specified
   1106       1.60   thorpej  * (in the event that the caller expects to prepend later).
   1107       1.60   thorpej  */
   1108       1.60   thorpej struct mbuf *
   1109       1.60   thorpej m_copyup(struct mbuf *n, int len, int dstoff)
   1110       1.60   thorpej {
   1111       1.60   thorpej 	struct mbuf *m;
   1112       1.60   thorpej 	int count, space;
   1113       1.60   thorpej 
   1114      1.156  christos 	KASSERT(len != M_COPYALL);
   1115  1.181.2.4  pgoyette 	if (len > ((int)MHLEN - dstoff))
   1116       1.60   thorpej 		goto bad;
   1117      1.153  christos 	m = m_get(M_DONTWAIT, n->m_type);
   1118       1.60   thorpej 	if (m == NULL)
   1119       1.60   thorpej 		goto bad;
   1120       1.64      matt 	MCLAIM(m, n->m_owner);
   1121       1.60   thorpej 	if (n->m_flags & M_PKTHDR) {
   1122      1.101      yamt 		M_MOVE_PKTHDR(m, n);
   1123       1.60   thorpej 	}
   1124       1.60   thorpej 	m->m_data += dstoff;
   1125       1.60   thorpej 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
   1126       1.60   thorpej 	do {
   1127       1.60   thorpej 		count = min(min(max(len, max_protohdr), space), n->m_len);
   1128      1.119  christos 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
   1129       1.60   thorpej 		    (unsigned)count);
   1130       1.60   thorpej 		len -= count;
   1131       1.60   thorpej 		m->m_len += count;
   1132       1.60   thorpej 		n->m_len -= count;
   1133       1.60   thorpej 		space -= count;
   1134       1.60   thorpej 		if (n->m_len)
   1135       1.60   thorpej 			n->m_data += count;
   1136       1.60   thorpej 		else
   1137       1.60   thorpej 			n = m_free(n);
   1138       1.60   thorpej 	} while (len > 0 && n);
   1139       1.60   thorpej 	if (len > 0) {
   1140       1.60   thorpej 		(void) m_free(m);
   1141       1.60   thorpej 		goto bad;
   1142       1.60   thorpej 	}
   1143       1.60   thorpej 	m->m_next = n;
   1144       1.60   thorpej 	return (m);
   1145       1.60   thorpej  bad:
   1146       1.60   thorpej 	m_freem(n);
   1147       1.60   thorpej 	return (NULL);
   1148        1.9   mycroft }
   1149        1.9   mycroft 
   1150        1.9   mycroft /*
   1151        1.9   mycroft  * Partition an mbuf chain in two pieces, returning the tail --
   1152        1.9   mycroft  * all but the first len0 bytes.  In case of failure, it returns NULL and
   1153        1.9   mycroft  * attempts to restore the chain to its original state.
   1154        1.9   mycroft  */
   1155        1.9   mycroft struct mbuf *
   1156       1.62   thorpej m_split(struct mbuf *m0, int len0, int wait)
   1157        1.9   mycroft {
   1158       1.85      yamt 
   1159      1.181      maxv 	return m_split0(m0, len0, wait, true);
   1160       1.85      yamt }
   1161       1.85      yamt 
   1162       1.85      yamt static struct mbuf *
   1163      1.181      maxv m_split0(struct mbuf *m0, int len0, int wait, bool copyhdr)
   1164       1.85      yamt {
   1165       1.27      matt 	struct mbuf *m, *n;
   1166       1.22   thorpej 	unsigned len = len0, remain, len_save;
   1167        1.9   mycroft 
   1168      1.156  christos 	KASSERT(len0 != M_COPYALL);
   1169        1.9   mycroft 	for (m = m0; m && len > m->m_len; m = m->m_next)
   1170        1.9   mycroft 		len -= m->m_len;
   1171      1.181      maxv 	if (m == NULL)
   1172      1.181      maxv 		return NULL;
   1173      1.181      maxv 
   1174        1.9   mycroft 	remain = m->m_len - len;
   1175       1.85      yamt 	if (copyhdr && (m0->m_flags & M_PKTHDR)) {
   1176      1.153  christos 		n = m_gethdr(wait, m0->m_type);
   1177      1.153  christos 		if (n == NULL)
   1178      1.153  christos 			return NULL;
   1179      1.181      maxv 
   1180      1.112     pavel 		MCLAIM(n, m0->m_owner);
   1181      1.167     ozaki 		m_copy_rcvif(n, m0);
   1182        1.9   mycroft 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
   1183       1.22   thorpej 		len_save = m0->m_pkthdr.len;
   1184        1.9   mycroft 		m0->m_pkthdr.len = len0;
   1185      1.181      maxv 
   1186        1.9   mycroft 		if (m->m_flags & M_EXT)
   1187        1.9   mycroft 			goto extpacket;
   1188      1.181      maxv 
   1189        1.9   mycroft 		if (remain > MHLEN) {
   1190        1.9   mycroft 			/* m can't be the lead packet */
   1191        1.9   mycroft 			MH_ALIGN(n, 0);
   1192      1.132    bouyer 			n->m_len = 0;
   1193        1.9   mycroft 			n->m_next = m_split(m, len, wait);
   1194      1.181      maxv 			if (n->m_next == NULL) {
   1195      1.181      maxv 				(void)m_free(n);
   1196       1.22   thorpej 				m0->m_pkthdr.len = len_save;
   1197      1.181      maxv 				return NULL;
   1198      1.181      maxv 			}
   1199      1.181      maxv 			return n;
   1200      1.181      maxv 		} else {
   1201        1.9   mycroft 			MH_ALIGN(n, remain);
   1202      1.181      maxv 		}
   1203        1.9   mycroft 	} else if (remain == 0) {
   1204        1.9   mycroft 		n = m->m_next;
   1205      1.181      maxv 		m->m_next = NULL;
   1206      1.181      maxv 		return n;
   1207        1.9   mycroft 	} else {
   1208      1.153  christos 		n = m_get(wait, m->m_type);
   1209      1.181      maxv 		if (n == NULL)
   1210      1.181      maxv 			return NULL;
   1211       1.64      matt 		MCLAIM(n, m->m_owner);
   1212        1.9   mycroft 		M_ALIGN(n, remain);
   1213        1.9   mycroft 	}
   1214      1.181      maxv 
   1215        1.9   mycroft extpacket:
   1216        1.9   mycroft 	if (m->m_flags & M_EXT) {
   1217      1.125      yamt 		n->m_data = m->m_data + len;
   1218       1.18   thorpej 		MCLADDREFERENCE(m, n);
   1219        1.9   mycroft 	} else {
   1220      1.119  christos 		memcpy(mtod(n, void *), mtod(m, char *) + len, remain);
   1221        1.9   mycroft 	}
   1222      1.181      maxv 
   1223        1.9   mycroft 	n->m_len = remain;
   1224        1.9   mycroft 	m->m_len = len;
   1225        1.9   mycroft 	n->m_next = m->m_next;
   1226      1.181      maxv 	m->m_next = NULL;
   1227      1.181      maxv 	return n;
   1228        1.9   mycroft }
   1229      1.181      maxv 
   1230        1.9   mycroft /*
   1231        1.9   mycroft  * Routine to copy from device local memory into mbufs.
   1232        1.9   mycroft  */
   1233        1.9   mycroft struct mbuf *
   1234       1.62   thorpej m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
   1235       1.62   thorpej     void (*copy)(const void *from, void *to, size_t len))
   1236        1.9   mycroft {
   1237       1.27      matt 	struct mbuf *m;
   1238      1.181      maxv 	struct mbuf *top = NULL, **mp = &top;
   1239       1.27      matt 	int off = off0, len;
   1240      1.181      maxv 	char *cp, *epkt;
   1241        1.9   mycroft 
   1242        1.9   mycroft 	cp = buf;
   1243        1.9   mycroft 	epkt = cp + totlen;
   1244        1.9   mycroft 	if (off) {
   1245       1.13       cgd 		/*
   1246       1.13       cgd 		 * If 'off' is non-zero, packet is trailer-encapsulated,
   1247       1.13       cgd 		 * so we have to skip the type and length fields.
   1248       1.13       cgd 		 */
   1249      1.104     perry 		cp += off + 2 * sizeof(uint16_t);
   1250      1.104     perry 		totlen -= 2 * sizeof(uint16_t);
   1251        1.9   mycroft 	}
   1252      1.181      maxv 
   1253      1.153  christos 	m = m_gethdr(M_DONTWAIT, MT_DATA);
   1254      1.153  christos 	if (m == NULL)
   1255      1.153  christos 		return NULL;
   1256      1.166     ozaki 	m_set_rcvif(m, ifp);
   1257        1.9   mycroft 	m->m_pkthdr.len = totlen;
   1258        1.9   mycroft 	m->m_len = MHLEN;
   1259        1.9   mycroft 
   1260        1.9   mycroft 	while (totlen > 0) {
   1261        1.9   mycroft 		if (top) {
   1262      1.153  christos 			m = m_get(M_DONTWAIT, MT_DATA);
   1263      1.181      maxv 			if (m == NULL) {
   1264        1.9   mycroft 				m_freem(top);
   1265      1.181      maxv 				return NULL;
   1266        1.9   mycroft 			}
   1267        1.9   mycroft 			m->m_len = MLEN;
   1268        1.9   mycroft 		}
   1269      1.181      maxv 
   1270        1.9   mycroft 		len = min(totlen, epkt - cp);
   1271      1.181      maxv 
   1272        1.9   mycroft 		if (len >= MINCLSIZE) {
   1273        1.9   mycroft 			MCLGET(m, M_DONTWAIT);
   1274       1.19   mycroft 			if ((m->m_flags & M_EXT) == 0) {
   1275       1.20   mycroft 				m_free(m);
   1276       1.19   mycroft 				m_freem(top);
   1277      1.181      maxv 				return NULL;
   1278       1.19   mycroft 			}
   1279       1.19   mycroft 			m->m_len = len = min(len, MCLBYTES);
   1280        1.9   mycroft 		} else {
   1281        1.9   mycroft 			/*
   1282        1.9   mycroft 			 * Place initial small packet/header at end of mbuf.
   1283        1.9   mycroft 			 */
   1284        1.9   mycroft 			if (len < m->m_len) {
   1285        1.9   mycroft 				if (top == 0 && len + max_linkhdr <= m->m_len)
   1286        1.9   mycroft 					m->m_data += max_linkhdr;
   1287        1.9   mycroft 				m->m_len = len;
   1288        1.9   mycroft 			} else
   1289        1.9   mycroft 				len = m->m_len;
   1290        1.9   mycroft 		}
   1291      1.181      maxv 
   1292        1.9   mycroft 		if (copy)
   1293      1.119  christos 			copy(cp, mtod(m, void *), (size_t)len);
   1294        1.9   mycroft 		else
   1295      1.119  christos 			memcpy(mtod(m, void *), cp, (size_t)len);
   1296      1.181      maxv 
   1297        1.9   mycroft 		cp += len;
   1298        1.9   mycroft 		*mp = m;
   1299        1.9   mycroft 		mp = &m->m_next;
   1300        1.9   mycroft 		totlen -= len;
   1301        1.9   mycroft 		if (cp == epkt)
   1302        1.9   mycroft 			cp = buf;
   1303        1.9   mycroft 	}
   1304      1.181      maxv 
   1305      1.181      maxv 	return top;
   1306       1.18   thorpej }
   1307       1.18   thorpej 
   1308       1.18   thorpej /*
   1309       1.18   thorpej  * Copy data from a buffer back into the indicated mbuf chain,
   1310       1.18   thorpej  * starting "off" bytes from the beginning, extending the mbuf
   1311       1.18   thorpej  * chain if necessary.
   1312       1.18   thorpej  */
   1313       1.18   thorpej void
   1314       1.86      yamt m_copyback(struct mbuf *m0, int off, int len, const void *cp)
   1315       1.18   thorpej {
   1316       1.85      yamt #if defined(DEBUG)
   1317       1.85      yamt 	struct mbuf *origm = m0;
   1318       1.85      yamt 	int error;
   1319      1.181      maxv #endif
   1320       1.85      yamt 
   1321       1.85      yamt 	if (m0 == NULL)
   1322       1.85      yamt 		return;
   1323       1.85      yamt 
   1324       1.85      yamt #if defined(DEBUG)
   1325       1.85      yamt 	error =
   1326      1.181      maxv #endif
   1327       1.85      yamt 	m_copyback0(&m0, off, len, cp,
   1328       1.85      yamt 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_EXTEND, M_DONTWAIT);
   1329       1.85      yamt 
   1330       1.85      yamt #if defined(DEBUG)
   1331       1.85      yamt 	if (error != 0 || (m0 != NULL && origm != m0))
   1332       1.85      yamt 		panic("m_copyback");
   1333      1.181      maxv #endif
   1334       1.85      yamt }
   1335       1.85      yamt 
   1336       1.85      yamt struct mbuf *
   1337       1.86      yamt m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
   1338       1.85      yamt {
   1339       1.85      yamt 	int error;
   1340       1.85      yamt 
   1341       1.85      yamt 	/* don't support chain expansion */
   1342      1.156  christos 	KASSERT(len != M_COPYALL);
   1343       1.85      yamt 	KDASSERT(off + len <= m_length(m0));
   1344       1.85      yamt 
   1345       1.85      yamt 	error = m_copyback0(&m0, off, len, cp,
   1346       1.85      yamt 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_COW, how);
   1347       1.85      yamt 	if (error) {
   1348       1.85      yamt 		/*
   1349       1.85      yamt 		 * no way to recover from partial success.
   1350       1.85      yamt 		 * just free the chain.
   1351       1.85      yamt 		 */
   1352       1.85      yamt 		m_freem(m0);
   1353       1.85      yamt 		return NULL;
   1354       1.85      yamt 	}
   1355       1.85      yamt 	return m0;
   1356       1.85      yamt }
   1357       1.85      yamt 
   1358       1.85      yamt /*
   1359       1.85      yamt  * m_makewritable: ensure the specified range writable.
   1360       1.85      yamt  */
   1361       1.85      yamt int
   1362       1.85      yamt m_makewritable(struct mbuf **mp, int off, int len, int how)
   1363       1.85      yamt {
   1364       1.85      yamt 	int error;
   1365       1.85      yamt #if defined(DEBUG)
   1366      1.156  christos 	int origlen = m_length(*mp);
   1367      1.181      maxv #endif
   1368       1.85      yamt 
   1369       1.85      yamt 	error = m_copyback0(mp, off, len, NULL,
   1370       1.85      yamt 	    M_COPYBACK0_PRESERVE|M_COPYBACK0_COW, how);
   1371       1.85      yamt 
   1372      1.170  christos 	if (error)
   1373      1.170  christos 		return error;
   1374      1.170  christos 
   1375       1.85      yamt #if defined(DEBUG)
   1376      1.156  christos 	int reslen = 0;
   1377      1.156  christos 	for (struct mbuf *n = *mp; n; n = n->m_next)
   1378       1.85      yamt 		reslen += n->m_len;
   1379       1.85      yamt 	if (origlen != reslen)
   1380       1.85      yamt 		panic("m_makewritable: length changed");
   1381       1.85      yamt 	if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
   1382       1.85      yamt 		panic("m_makewritable: inconsist");
   1383      1.181      maxv #endif
   1384       1.85      yamt 
   1385      1.170  christos 	return 0;
   1386       1.85      yamt }
   1387       1.85      yamt 
   1388      1.142    dyoung /*
   1389      1.142    dyoung  * Copy the mbuf chain to a new mbuf chain that is as short as possible.
   1390      1.142    dyoung  * Return the new mbuf chain on success, NULL on failure.  On success,
   1391      1.142    dyoung  * free the old mbuf chain.
   1392      1.142    dyoung  */
   1393      1.142    dyoung struct mbuf *
   1394      1.142    dyoung m_defrag(struct mbuf *mold, int flags)
   1395      1.142    dyoung {
   1396      1.142    dyoung 	struct mbuf *m0, *mn, *n;
   1397      1.142    dyoung 	size_t sz = mold->m_pkthdr.len;
   1398      1.142    dyoung 
   1399      1.181      maxv 	KASSERT((mold->m_flags & M_PKTHDR) != 0);
   1400      1.142    dyoung 
   1401      1.153  christos 	m0 = m_gethdr(flags, MT_DATA);
   1402      1.142    dyoung 	if (m0 == NULL)
   1403      1.142    dyoung 		return NULL;
   1404      1.142    dyoung 	M_COPY_PKTHDR(m0, mold);
   1405      1.142    dyoung 	mn = m0;
   1406      1.142    dyoung 
   1407      1.142    dyoung 	do {
   1408      1.142    dyoung 		if (sz > MHLEN) {
   1409      1.142    dyoung 			MCLGET(mn, M_DONTWAIT);
   1410      1.142    dyoung 			if ((mn->m_flags & M_EXT) == 0) {
   1411      1.142    dyoung 				m_freem(m0);
   1412      1.142    dyoung 				return NULL;
   1413      1.142    dyoung 			}
   1414      1.142    dyoung 		}
   1415      1.142    dyoung 
   1416      1.142    dyoung 		mn->m_len = MIN(sz, MCLBYTES);
   1417      1.142    dyoung 
   1418      1.142    dyoung 		m_copydata(mold, mold->m_pkthdr.len - sz, mn->m_len,
   1419      1.142    dyoung 		     mtod(mn, void *));
   1420      1.142    dyoung 
   1421      1.142    dyoung 		sz -= mn->m_len;
   1422      1.142    dyoung 
   1423      1.142    dyoung 		if (sz > 0) {
   1424      1.142    dyoung 			/* need more mbufs */
   1425      1.153  christos 			n = m_get(M_NOWAIT, MT_DATA);
   1426      1.142    dyoung 			if (n == NULL) {
   1427      1.142    dyoung 				m_freem(m0);
   1428      1.142    dyoung 				return NULL;
   1429      1.142    dyoung 			}
   1430      1.142    dyoung 
   1431      1.142    dyoung 			mn->m_next = n;
   1432      1.142    dyoung 			mn = n;
   1433      1.142    dyoung 		}
   1434      1.142    dyoung 	} while (sz > 0);
   1435      1.142    dyoung 
   1436      1.142    dyoung 	m_freem(mold);
   1437      1.142    dyoung 
   1438      1.142    dyoung 	return m0;
   1439      1.142    dyoung }
   1440      1.142    dyoung 
   1441       1.85      yamt int
   1442       1.86      yamt m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags,
   1443       1.86      yamt     int how)
   1444       1.85      yamt {
   1445       1.27      matt 	int mlen;
   1446       1.85      yamt 	struct mbuf *m, *n;
   1447       1.85      yamt 	struct mbuf **mp;
   1448       1.18   thorpej 	int totlen = 0;
   1449       1.86      yamt 	const char *cp = vp;
   1450       1.18   thorpej 
   1451       1.85      yamt 	KASSERT(mp0 != NULL);
   1452       1.85      yamt 	KASSERT(*mp0 != NULL);
   1453       1.85      yamt 	KASSERT((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL);
   1454       1.85      yamt 	KASSERT((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL);
   1455       1.85      yamt 
   1456      1.156  christos 	if (len == M_COPYALL)
   1457      1.156  christos 		len = m_length(*mp0) - off;
   1458      1.156  christos 
   1459      1.106      yamt 	/*
   1460      1.106      yamt 	 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
   1461      1.106      yamt 	 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
   1462      1.106      yamt 	 */
   1463      1.106      yamt 
   1464      1.106      yamt 	KASSERT((~flags & (M_COPYBACK0_EXTEND|M_COPYBACK0_COW)) != 0);
   1465      1.106      yamt 
   1466       1.85      yamt 	mp = mp0;
   1467       1.85      yamt 	m = *mp;
   1468       1.18   thorpej 	while (off > (mlen = m->m_len)) {
   1469       1.18   thorpej 		off -= mlen;
   1470       1.18   thorpej 		totlen += mlen;
   1471      1.109      yamt 		if (m->m_next == NULL) {
   1472      1.109      yamt 			int tspace;
   1473      1.109      yamt extend:
   1474       1.85      yamt 			if ((flags & M_COPYBACK0_EXTEND) == 0)
   1475       1.85      yamt 				goto out;
   1476      1.109      yamt 
   1477      1.109      yamt 			/*
   1478      1.109      yamt 			 * try to make some space at the end of "m".
   1479      1.109      yamt 			 */
   1480      1.109      yamt 
   1481      1.109      yamt 			mlen = m->m_len;
   1482      1.109      yamt 			if (off + len >= MINCLSIZE &&
   1483      1.109      yamt 			    (m->m_flags & M_EXT) == 0 && m->m_len == 0) {
   1484      1.109      yamt 				MCLGET(m, how);
   1485      1.109      yamt 			}
   1486      1.109      yamt 			tspace = M_TRAILINGSPACE(m);
   1487      1.109      yamt 			if (tspace > 0) {
   1488      1.109      yamt 				tspace = min(tspace, off + len);
   1489      1.109      yamt 				KASSERT(tspace > 0);
   1490      1.109      yamt 				memset(mtod(m, char *) + m->m_len, 0,
   1491      1.109      yamt 				    min(off, tspace));
   1492      1.109      yamt 				m->m_len += tspace;
   1493      1.109      yamt 				off += mlen;
   1494      1.109      yamt 				totlen -= mlen;
   1495      1.109      yamt 				continue;
   1496      1.109      yamt 			}
   1497      1.109      yamt 
   1498      1.109      yamt 			/*
   1499      1.109      yamt 			 * need to allocate an mbuf.
   1500      1.109      yamt 			 */
   1501      1.109      yamt 
   1502      1.109      yamt 			if (off + len >= MINCLSIZE) {
   1503      1.109      yamt 				n = m_getcl(how, m->m_type, 0);
   1504      1.109      yamt 			} else {
   1505      1.109      yamt 				n = m_get(how, m->m_type);
   1506      1.109      yamt 			}
   1507      1.109      yamt 			if (n == NULL) {
   1508       1.18   thorpej 				goto out;
   1509      1.109      yamt 			}
   1510      1.109      yamt 			n->m_len = min(M_TRAILINGSPACE(n), off + len);
   1511      1.109      yamt 			memset(mtod(n, char *), 0, min(n->m_len, off));
   1512       1.18   thorpej 			m->m_next = n;
   1513       1.18   thorpej 		}
   1514       1.85      yamt 		mp = &m->m_next;
   1515       1.18   thorpej 		m = m->m_next;
   1516       1.18   thorpej 	}
   1517       1.18   thorpej 	while (len > 0) {
   1518       1.85      yamt 		mlen = m->m_len - off;
   1519       1.85      yamt 		if (mlen != 0 && M_READONLY(m)) {
   1520       1.85      yamt 			char *datap;
   1521       1.85      yamt 			int eatlen;
   1522       1.85      yamt 
   1523       1.85      yamt 			/*
   1524       1.85      yamt 			 * this mbuf is read-only.
   1525       1.85      yamt 			 * allocate a new writable mbuf and try again.
   1526       1.85      yamt 			 */
   1527       1.85      yamt 
   1528       1.85      yamt #if defined(DIAGNOSTIC)
   1529       1.85      yamt 			if ((flags & M_COPYBACK0_COW) == 0)
   1530       1.85      yamt 				panic("m_copyback0: read-only");
   1531       1.85      yamt #endif /* defined(DIAGNOSTIC) */
   1532       1.85      yamt 
   1533       1.85      yamt 			/*
   1534       1.85      yamt 			 * if we're going to write into the middle of
   1535       1.85      yamt 			 * a mbuf, split it first.
   1536       1.85      yamt 			 */
   1537      1.137     seanb 			if (off > 0) {
   1538      1.181      maxv 				n = m_split0(m, off, how, false);
   1539       1.85      yamt 				if (n == NULL)
   1540       1.85      yamt 					goto enobufs;
   1541       1.85      yamt 				m->m_next = n;
   1542       1.85      yamt 				mp = &m->m_next;
   1543       1.85      yamt 				m = n;
   1544       1.85      yamt 				off = 0;
   1545       1.85      yamt 				continue;
   1546       1.85      yamt 			}
   1547       1.85      yamt 
   1548       1.85      yamt 			/*
   1549       1.85      yamt 			 * XXX TODO coalesce into the trailingspace of
   1550       1.85      yamt 			 * the previous mbuf when possible.
   1551       1.85      yamt 			 */
   1552       1.85      yamt 
   1553       1.85      yamt 			/*
   1554       1.85      yamt 			 * allocate a new mbuf.  copy packet header if needed.
   1555       1.85      yamt 			 */
   1556      1.153  christos 			n = m_get(how, m->m_type);
   1557       1.85      yamt 			if (n == NULL)
   1558       1.85      yamt 				goto enobufs;
   1559       1.85      yamt 			MCLAIM(n, m->m_owner);
   1560       1.85      yamt 			if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
   1561      1.101      yamt 				M_MOVE_PKTHDR(n, m);
   1562       1.85      yamt 				n->m_len = MHLEN;
   1563       1.85      yamt 			} else {
   1564       1.85      yamt 				if (len >= MINCLSIZE)
   1565       1.85      yamt 					MCLGET(n, M_DONTWAIT);
   1566       1.85      yamt 				n->m_len =
   1567       1.85      yamt 				    (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
   1568       1.85      yamt 			}
   1569       1.85      yamt 			if (n->m_len > len)
   1570       1.85      yamt 				n->m_len = len;
   1571       1.85      yamt 
   1572       1.85      yamt 			/*
   1573       1.85      yamt 			 * free the region which has been overwritten.
   1574       1.85      yamt 			 * copying data from old mbufs if requested.
   1575       1.85      yamt 			 */
   1576       1.85      yamt 			if (flags & M_COPYBACK0_PRESERVE)
   1577       1.85      yamt 				datap = mtod(n, char *);
   1578       1.85      yamt 			else
   1579       1.85      yamt 				datap = NULL;
   1580       1.85      yamt 			eatlen = n->m_len;
   1581       1.85      yamt 			while (m != NULL && M_READONLY(m) &&
   1582       1.85      yamt 			    n->m_type == m->m_type && eatlen > 0) {
   1583       1.85      yamt 				mlen = min(eatlen, m->m_len);
   1584       1.85      yamt 				if (datap) {
   1585       1.85      yamt 					m_copydata(m, 0, mlen, datap);
   1586       1.85      yamt 					datap += mlen;
   1587       1.85      yamt 				}
   1588       1.85      yamt 				m->m_data += mlen;
   1589       1.85      yamt 				m->m_len -= mlen;
   1590       1.85      yamt 				eatlen -= mlen;
   1591       1.85      yamt 				if (m->m_len == 0)
   1592       1.85      yamt 					*mp = m = m_free(m);
   1593       1.85      yamt 			}
   1594       1.85      yamt 			if (eatlen > 0)
   1595       1.85      yamt 				n->m_len -= eatlen;
   1596       1.85      yamt 			n->m_next = m;
   1597       1.85      yamt 			*mp = m = n;
   1598       1.85      yamt 			continue;
   1599       1.85      yamt 		}
   1600       1.85      yamt 		mlen = min(mlen, len);
   1601       1.85      yamt 		if (flags & M_COPYBACK0_COPYBACK) {
   1602      1.119  christos 			memcpy(mtod(m, char *) + off, cp, (unsigned)mlen);
   1603       1.85      yamt 			cp += mlen;
   1604       1.85      yamt 		}
   1605       1.18   thorpej 		len -= mlen;
   1606       1.18   thorpej 		mlen += off;
   1607       1.18   thorpej 		off = 0;
   1608       1.18   thorpej 		totlen += mlen;
   1609       1.18   thorpej 		if (len == 0)
   1610       1.18   thorpej 			break;
   1611      1.109      yamt 		if (m->m_next == NULL) {
   1612      1.109      yamt 			goto extend;
   1613       1.18   thorpej 		}
   1614       1.85      yamt 		mp = &m->m_next;
   1615       1.18   thorpej 		m = m->m_next;
   1616       1.18   thorpej 	}
   1617      1.106      yamt out:	if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) {
   1618      1.106      yamt 		KASSERT((flags & M_COPYBACK0_EXTEND) != 0);
   1619       1.18   thorpej 		m->m_pkthdr.len = totlen;
   1620      1.106      yamt 	}
   1621       1.85      yamt 
   1622       1.85      yamt 	return 0;
   1623       1.85      yamt 
   1624       1.85      yamt enobufs:
   1625       1.85      yamt 	return ENOBUFS;
   1626       1.66   thorpej }
   1627       1.66   thorpej 
   1628      1.101      yamt void
   1629      1.101      yamt m_move_pkthdr(struct mbuf *to, struct mbuf *from)
   1630      1.101      yamt {
   1631      1.101      yamt 
   1632      1.101      yamt 	KASSERT((to->m_flags & M_EXT) == 0);
   1633      1.101      yamt 	KASSERT((to->m_flags & M_PKTHDR) == 0 || m_tag_first(to) == NULL);
   1634      1.101      yamt 	KASSERT((from->m_flags & M_PKTHDR) != 0);
   1635      1.101      yamt 
   1636      1.101      yamt 	to->m_pkthdr = from->m_pkthdr;
   1637      1.101      yamt 	to->m_flags = from->m_flags & M_COPYFLAGS;
   1638      1.101      yamt 	to->m_data = to->m_pktdat;
   1639      1.101      yamt 
   1640      1.101      yamt 	from->m_flags &= ~M_PKTHDR;
   1641      1.101      yamt }
   1642      1.101      yamt 
   1643       1.66   thorpej /*
   1644       1.66   thorpej  * Apply function f to the data in an mbuf chain starting "off" bytes from the
   1645       1.66   thorpej  * beginning, continuing for "len" bytes.
   1646       1.66   thorpej  */
   1647       1.66   thorpej int
   1648       1.66   thorpej m_apply(struct mbuf *m, int off, int len,
   1649      1.119  christos     int (*f)(void *, void *, unsigned int), void *arg)
   1650       1.66   thorpej {
   1651       1.66   thorpej 	unsigned int count;
   1652       1.66   thorpej 	int rval;
   1653       1.66   thorpej 
   1654      1.156  christos 	KASSERT(len != M_COPYALL);
   1655       1.66   thorpej 	KASSERT(len >= 0);
   1656       1.66   thorpej 	KASSERT(off >= 0);
   1657       1.66   thorpej 
   1658       1.66   thorpej 	while (off > 0) {
   1659       1.66   thorpej 		KASSERT(m != NULL);
   1660       1.66   thorpej 		if (off < m->m_len)
   1661       1.66   thorpej 			break;
   1662       1.66   thorpej 		off -= m->m_len;
   1663       1.66   thorpej 		m = m->m_next;
   1664       1.66   thorpej 	}
   1665       1.66   thorpej 	while (len > 0) {
   1666       1.66   thorpej 		KASSERT(m != NULL);
   1667       1.66   thorpej 		count = min(m->m_len - off, len);
   1668       1.66   thorpej 
   1669      1.119  christos 		rval = (*f)(arg, mtod(m, char *) + off, count);
   1670       1.66   thorpej 		if (rval)
   1671      1.181      maxv 			return rval;
   1672       1.66   thorpej 
   1673       1.66   thorpej 		len -= count;
   1674       1.66   thorpej 		off = 0;
   1675       1.66   thorpej 		m = m->m_next;
   1676       1.66   thorpej 	}
   1677       1.66   thorpej 
   1678      1.181      maxv 	return 0;
   1679       1.66   thorpej }
   1680       1.66   thorpej 
   1681       1.66   thorpej /*
   1682       1.66   thorpej  * Return a pointer to mbuf/offset of location in mbuf chain.
   1683       1.66   thorpej  */
   1684       1.66   thorpej struct mbuf *
   1685       1.66   thorpej m_getptr(struct mbuf *m, int loc, int *off)
   1686       1.66   thorpej {
   1687       1.66   thorpej 
   1688       1.66   thorpej 	while (loc >= 0) {
   1689       1.66   thorpej 		/* Normal end of search */
   1690       1.66   thorpej 		if (m->m_len > loc) {
   1691      1.177      maxv 			*off = loc;
   1692      1.181      maxv 			return m;
   1693      1.181      maxv 		}
   1694      1.181      maxv 
   1695      1.181      maxv 		loc -= m->m_len;
   1696      1.181      maxv 
   1697      1.181      maxv 		if (m->m_next == NULL) {
   1698      1.181      maxv 			if (loc == 0) {
   1699      1.181      maxv 				/* Point at the end of valid data */
   1700      1.181      maxv 				*off = m->m_len;
   1701      1.181      maxv 				return m;
   1702      1.181      maxv 			}
   1703      1.181      maxv 			return NULL;
   1704       1.66   thorpej 		} else {
   1705      1.181      maxv 			m = m->m_next;
   1706       1.66   thorpej 		}
   1707      1.177      maxv 	}
   1708       1.66   thorpej 
   1709      1.181      maxv 	return NULL;
   1710        1.1       cgd }
   1711      1.105      yamt 
   1712      1.105      yamt #if defined(DDB)
   1713      1.105      yamt void
   1714      1.105      yamt m_print(const struct mbuf *m, const char *modif, void (*pr)(const char *, ...))
   1715      1.105      yamt {
   1716      1.105      yamt 	char ch;
   1717      1.118   thorpej 	bool opt_c = false;
   1718      1.105      yamt 	char buf[512];
   1719      1.105      yamt 
   1720      1.105      yamt 	while ((ch = *(modif++)) != '\0') {
   1721      1.105      yamt 		switch (ch) {
   1722      1.105      yamt 		case 'c':
   1723      1.118   thorpej 			opt_c = true;
   1724      1.105      yamt 			break;
   1725      1.105      yamt 		}
   1726      1.105      yamt 	}
   1727      1.105      yamt 
   1728      1.105      yamt nextchain:
   1729      1.105      yamt 	(*pr)("MBUF %p\n", m);
   1730      1.130  christos 	snprintb(buf, sizeof(buf), M_FLAGS_BITS, (u_int)m->m_flags);
   1731      1.138    cegger 	(*pr)("  data=%p, len=%d, type=%d, flags=%s\n",
   1732      1.105      yamt 	    m->m_data, m->m_len, m->m_type, buf);
   1733      1.105      yamt 	(*pr)("  owner=%p, next=%p, nextpkt=%p\n", m->m_owner, m->m_next,
   1734      1.105      yamt 	    m->m_nextpkt);
   1735      1.105      yamt 	(*pr)("  leadingspace=%u, trailingspace=%u, readonly=%u\n",
   1736      1.105      yamt 	    (int)M_LEADINGSPACE(m), (int)M_TRAILINGSPACE(m),
   1737      1.105      yamt 	    (int)M_READONLY(m));
   1738      1.105      yamt 	if ((m->m_flags & M_PKTHDR) != 0) {
   1739      1.130  christos 		snprintb(buf, sizeof(buf), M_CSUM_BITS, m->m_pkthdr.csum_flags);
   1740      1.172   msaitoh 		(*pr)("  pktlen=%d, rcvif=%p, csum_flags=%s, csum_data=0x%"
   1741      1.105      yamt 		    PRIx32 ", segsz=%u\n",
   1742      1.167     ozaki 		    m->m_pkthdr.len, m_get_rcvif_NOMPSAFE(m),
   1743      1.105      yamt 		    buf, m->m_pkthdr.csum_data, m->m_pkthdr.segsz);
   1744      1.105      yamt 	}
   1745      1.105      yamt 	if ((m->m_flags & M_EXT)) {
   1746      1.125      yamt 		(*pr)("  ext_refcnt=%u, ext_buf=%p, ext_size=%zd, "
   1747      1.105      yamt 		    "ext_free=%p, ext_arg=%p\n",
   1748      1.125      yamt 		    m->m_ext.ext_refcnt,
   1749      1.105      yamt 		    m->m_ext.ext_buf, m->m_ext.ext_size,
   1750      1.105      yamt 		    m->m_ext.ext_free, m->m_ext.ext_arg);
   1751      1.105      yamt 	}
   1752      1.105      yamt 	if ((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0) {
   1753      1.108      yamt 		vaddr_t sva = (vaddr_t)m->m_ext.ext_buf;
   1754      1.108      yamt 		vaddr_t eva = sva + m->m_ext.ext_size;
   1755      1.108      yamt 		int n = (round_page(eva) - trunc_page(sva)) >> PAGE_SHIFT;
   1756      1.108      yamt 		int i;
   1757      1.105      yamt 
   1758      1.105      yamt 		(*pr)("  pages:");
   1759      1.108      yamt 		for (i = 0; i < n; i ++) {
   1760      1.108      yamt 			(*pr)(" %p", m->m_ext.ext_pgs[i]);
   1761      1.105      yamt 		}
   1762      1.105      yamt 		(*pr)("\n");
   1763      1.105      yamt 	}
   1764      1.105      yamt 
   1765      1.105      yamt 	if (opt_c) {
   1766      1.105      yamt 		m = m->m_next;
   1767      1.105      yamt 		if (m != NULL) {
   1768      1.105      yamt 			goto nextchain;
   1769      1.105      yamt 		}
   1770      1.105      yamt 	}
   1771      1.105      yamt }
   1772      1.105      yamt #endif /* defined(DDB) */
   1773      1.124      yamt 
   1774      1.124      yamt void
   1775      1.124      yamt mbstat_type_add(int type, int diff)
   1776      1.124      yamt {
   1777      1.124      yamt 	struct mbstat_cpu *mb;
   1778      1.124      yamt 	int s;
   1779      1.124      yamt 
   1780      1.124      yamt 	s = splvm();
   1781      1.126   thorpej 	mb = percpu_getref(mbstat_percpu);
   1782      1.124      yamt 	mb->m_mtypes[type] += diff;
   1783      1.126   thorpej 	percpu_putref(mbstat_percpu);
   1784      1.124      yamt 	splx(s);
   1785      1.124      yamt }
   1786      1.124      yamt 
   1787      1.124      yamt #if defined(MBUFTRACE)
   1788      1.124      yamt void
   1789      1.124      yamt mowner_attach(struct mowner *mo)
   1790      1.124      yamt {
   1791      1.124      yamt 
   1792      1.124      yamt 	KASSERT(mo->mo_counters == NULL);
   1793      1.124      yamt 	mo->mo_counters = percpu_alloc(sizeof(struct mowner_counter));
   1794      1.124      yamt 
   1795      1.124      yamt 	/* XXX lock */
   1796      1.124      yamt 	LIST_INSERT_HEAD(&mowners, mo, mo_link);
   1797      1.124      yamt }
   1798      1.124      yamt 
   1799      1.124      yamt void
   1800      1.124      yamt mowner_detach(struct mowner *mo)
   1801      1.124      yamt {
   1802      1.124      yamt 
   1803      1.124      yamt 	KASSERT(mo->mo_counters != NULL);
   1804      1.124      yamt 
   1805      1.124      yamt 	/* XXX lock */
   1806      1.124      yamt 	LIST_REMOVE(mo, mo_link);
   1807      1.124      yamt 
   1808      1.124      yamt 	percpu_free(mo->mo_counters, sizeof(struct mowner_counter));
   1809      1.124      yamt 	mo->mo_counters = NULL;
   1810      1.124      yamt }
   1811      1.124      yamt 
   1812      1.124      yamt void
   1813      1.124      yamt mowner_init(struct mbuf *m, int type)
   1814      1.124      yamt {
   1815      1.124      yamt 	struct mowner_counter *mc;
   1816      1.124      yamt 	struct mowner *mo;
   1817      1.124      yamt 	int s;
   1818      1.124      yamt 
   1819      1.124      yamt 	m->m_owner = mo = &unknown_mowners[type];
   1820      1.124      yamt 	s = splvm();
   1821      1.126   thorpej 	mc = percpu_getref(mo->mo_counters);
   1822      1.124      yamt 	mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
   1823      1.126   thorpej 	percpu_putref(mo->mo_counters);
   1824      1.124      yamt 	splx(s);
   1825      1.124      yamt }
   1826      1.124      yamt 
   1827      1.124      yamt void
   1828      1.124      yamt mowner_ref(struct mbuf *m, int flags)
   1829      1.124      yamt {
   1830      1.124      yamt 	struct mowner *mo = m->m_owner;
   1831      1.124      yamt 	struct mowner_counter *mc;
   1832      1.124      yamt 	int s;
   1833      1.124      yamt 
   1834      1.124      yamt 	s = splvm();
   1835      1.126   thorpej 	mc = percpu_getref(mo->mo_counters);
   1836      1.124      yamt 	if ((flags & M_EXT) != 0)
   1837      1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
   1838      1.124      yamt 	if ((flags & M_CLUSTER) != 0)
   1839      1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
   1840      1.126   thorpej 	percpu_putref(mo->mo_counters);
   1841      1.124      yamt 	splx(s);
   1842      1.124      yamt }
   1843      1.124      yamt 
   1844      1.124      yamt void
   1845      1.124      yamt mowner_revoke(struct mbuf *m, bool all, int flags)
   1846      1.124      yamt {
   1847      1.124      yamt 	struct mowner *mo = m->m_owner;
   1848      1.124      yamt 	struct mowner_counter *mc;
   1849      1.124      yamt 	int s;
   1850      1.124      yamt 
   1851      1.124      yamt 	s = splvm();
   1852      1.126   thorpej 	mc = percpu_getref(mo->mo_counters);
   1853      1.124      yamt 	if ((flags & M_EXT) != 0)
   1854      1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_EXT_RELEASES]++;
   1855      1.124      yamt 	if ((flags & M_CLUSTER) != 0)
   1856      1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_CLUSTER_RELEASES]++;
   1857      1.124      yamt 	if (all)
   1858      1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_RELEASES]++;
   1859      1.126   thorpej 	percpu_putref(mo->mo_counters);
   1860      1.124      yamt 	splx(s);
   1861      1.124      yamt 	if (all)
   1862      1.124      yamt 		m->m_owner = &revoked_mowner;
   1863      1.124      yamt }
   1864      1.124      yamt 
   1865      1.124      yamt static void
   1866      1.124      yamt mowner_claim(struct mbuf *m, struct mowner *mo)
   1867      1.124      yamt {
   1868      1.124      yamt 	struct mowner_counter *mc;
   1869      1.124      yamt 	int flags = m->m_flags;
   1870      1.124      yamt 	int s;
   1871      1.124      yamt 
   1872      1.124      yamt 	s = splvm();
   1873      1.126   thorpej 	mc = percpu_getref(mo->mo_counters);
   1874      1.124      yamt 	mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
   1875      1.124      yamt 	if ((flags & M_EXT) != 0)
   1876      1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
   1877      1.124      yamt 	if ((flags & M_CLUSTER) != 0)
   1878      1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
   1879      1.126   thorpej 	percpu_putref(mo->mo_counters);
   1880      1.124      yamt 	splx(s);
   1881      1.124      yamt 	m->m_owner = mo;
   1882      1.124      yamt }
   1883      1.124      yamt 
   1884      1.124      yamt void
   1885      1.124      yamt m_claim(struct mbuf *m, struct mowner *mo)
   1886      1.124      yamt {
   1887      1.124      yamt 
   1888      1.124      yamt 	if (m->m_owner == mo || mo == NULL)
   1889      1.124      yamt 		return;
   1890      1.124      yamt 
   1891      1.124      yamt 	mowner_revoke(m, true, m->m_flags);
   1892      1.124      yamt 	mowner_claim(m, mo);
   1893      1.124      yamt }
   1894      1.124      yamt #endif /* defined(MBUFTRACE) */
   1895      1.169  christos 
   1896  1.181.2.3  pgoyette #ifdef DIAGNOSTIC
   1897  1.181.2.3  pgoyette /*
   1898  1.181.2.3  pgoyette  * Verify that the mbuf chain is not malformed. Used only for diagnostic.
   1899  1.181.2.3  pgoyette  * Panics on error.
   1900  1.181.2.3  pgoyette  */
   1901  1.181.2.3  pgoyette void
   1902  1.181.2.3  pgoyette m_verify_packet(struct mbuf *m)
   1903  1.181.2.3  pgoyette {
   1904  1.181.2.3  pgoyette 	struct mbuf *n = m;
   1905  1.181.2.3  pgoyette 	char *low, *high, *dat;
   1906  1.181.2.3  pgoyette 	int totlen = 0, len;
   1907  1.181.2.3  pgoyette 
   1908  1.181.2.3  pgoyette 	if (__predict_false((m->m_flags & M_PKTHDR) == 0)) {
   1909  1.181.2.3  pgoyette 		panic("%s: mbuf doesn't have M_PKTHDR", __func__);
   1910  1.181.2.3  pgoyette 	}
   1911  1.181.2.3  pgoyette 
   1912  1.181.2.3  pgoyette 	while (n != NULL) {
   1913  1.181.2.3  pgoyette 		if (__predict_false(n->m_type == MT_FREE)) {
   1914  1.181.2.3  pgoyette 			panic("%s: mbuf already freed (n = %p)", __func__, n);
   1915  1.181.2.3  pgoyette 		}
   1916  1.181.2.4  pgoyette #if 0
   1917  1.181.2.4  pgoyette 		/*
   1918  1.181.2.4  pgoyette 		 * This ought to be a rule of the mbuf API. Unfortunately,
   1919  1.181.2.4  pgoyette 		 * many places don't respect that rule.
   1920  1.181.2.4  pgoyette 		 */
   1921  1.181.2.3  pgoyette 		if (__predict_false((n != m) && (n->m_flags & M_PKTHDR) != 0)) {
   1922  1.181.2.3  pgoyette 			panic("%s: M_PKTHDR set on secondary mbuf", __func__);
   1923  1.181.2.3  pgoyette 		}
   1924  1.181.2.4  pgoyette #endif
   1925  1.181.2.3  pgoyette 		if (__predict_false(n->m_nextpkt != NULL)) {
   1926  1.181.2.3  pgoyette 			panic("%s: m_nextpkt not null (m_nextpkt = %p)",
   1927  1.181.2.3  pgoyette 			    __func__, n->m_nextpkt);
   1928  1.181.2.3  pgoyette 		}
   1929  1.181.2.3  pgoyette 
   1930  1.181.2.3  pgoyette 		dat = n->m_data;
   1931  1.181.2.3  pgoyette 		len = n->m_len;
   1932  1.181.2.3  pgoyette 
   1933  1.181.2.3  pgoyette 		if (n->m_flags & M_EXT) {
   1934  1.181.2.3  pgoyette 			low = n->m_ext.ext_buf;
   1935  1.181.2.3  pgoyette 			high = low + n->m_ext.ext_size;
   1936  1.181.2.3  pgoyette 		} else if (n->m_flags & M_PKTHDR) {
   1937  1.181.2.3  pgoyette 			low = n->m_pktdat;
   1938  1.181.2.3  pgoyette 			high = low + MHLEN;
   1939  1.181.2.3  pgoyette 		} else {
   1940  1.181.2.3  pgoyette 			low = n->m_dat;
   1941  1.181.2.3  pgoyette 			high = low + MLEN;
   1942  1.181.2.3  pgoyette 		}
   1943  1.181.2.4  pgoyette 		if (__predict_false(dat + len < dat)) {
   1944  1.181.2.3  pgoyette 			panic("%s: incorrect length (len = %d)", __func__, len);
   1945  1.181.2.3  pgoyette 		}
   1946  1.181.2.3  pgoyette 		if (__predict_false((dat < low) || (dat + len > high))) {
   1947  1.181.2.3  pgoyette 			panic("%s: m_data not in packet"
   1948  1.181.2.3  pgoyette 			    "(dat = %p, len = %d, low = %p, high = %p)",
   1949  1.181.2.3  pgoyette 			    __func__, dat, len, low, high);
   1950  1.181.2.3  pgoyette 		}
   1951  1.181.2.3  pgoyette 
   1952  1.181.2.3  pgoyette 		totlen += len;
   1953  1.181.2.3  pgoyette 		n = n->m_next;
   1954  1.181.2.3  pgoyette 	}
   1955  1.181.2.3  pgoyette 
   1956  1.181.2.3  pgoyette 	if (__predict_false(totlen != m->m_pkthdr.len)) {
   1957  1.181.2.3  pgoyette 		panic("%s: inconsistent mbuf length (%d != %d)", __func__,
   1958  1.181.2.3  pgoyette 		    totlen, m->m_pkthdr.len);
   1959  1.181.2.3  pgoyette 	}
   1960  1.181.2.3  pgoyette }
   1961  1.181.2.3  pgoyette #endif
   1962  1.181.2.3  pgoyette 
   1963  1.181.2.3  pgoyette /*
   1964  1.181.2.3  pgoyette  * Release a reference to the mbuf external storage.
   1965  1.181.2.3  pgoyette  *
   1966  1.181.2.3  pgoyette  * => free the mbuf m itself as well.
   1967  1.181.2.3  pgoyette  */
   1968  1.181.2.3  pgoyette static void
   1969  1.181.2.3  pgoyette m_ext_free(struct mbuf *m)
   1970  1.181.2.3  pgoyette {
   1971  1.181.2.3  pgoyette 	const bool embedded = MEXT_ISEMBEDDED(m);
   1972  1.181.2.3  pgoyette 	bool dofree = true;
   1973  1.181.2.3  pgoyette 	u_int refcnt;
   1974  1.181.2.3  pgoyette 
   1975  1.181.2.3  pgoyette 	KASSERT((m->m_flags & M_EXT) != 0);
   1976  1.181.2.3  pgoyette 	KASSERT(MEXT_ISEMBEDDED(m->m_ext_ref));
   1977  1.181.2.3  pgoyette 	KASSERT((m->m_ext_ref->m_flags & M_EXT) != 0);
   1978  1.181.2.3  pgoyette 	KASSERT((m->m_flags & M_EXT_CLUSTER) ==
   1979  1.181.2.3  pgoyette 	    (m->m_ext_ref->m_flags & M_EXT_CLUSTER));
   1980  1.181.2.3  pgoyette 
   1981  1.181.2.3  pgoyette 	if (__predict_false(m->m_type == MT_FREE)) {
   1982  1.181.2.3  pgoyette 		panic("mbuf %p already freed", m);
   1983  1.181.2.3  pgoyette 	}
   1984  1.181.2.3  pgoyette 
   1985  1.181.2.3  pgoyette 	if (__predict_true(m->m_ext.ext_refcnt == 1)) {
   1986  1.181.2.3  pgoyette 		refcnt = m->m_ext.ext_refcnt = 0;
   1987  1.181.2.3  pgoyette 	} else {
   1988  1.181.2.3  pgoyette 		refcnt = atomic_dec_uint_nv(&m->m_ext.ext_refcnt);
   1989  1.181.2.3  pgoyette 	}
   1990  1.181.2.3  pgoyette 
   1991  1.181.2.3  pgoyette 	if (refcnt > 0) {
   1992  1.181.2.3  pgoyette 		if (embedded) {
   1993  1.181.2.3  pgoyette 			/*
   1994  1.181.2.3  pgoyette 			 * other mbuf's m_ext_ref still points to us.
   1995  1.181.2.3  pgoyette 			 */
   1996  1.181.2.3  pgoyette 			dofree = false;
   1997  1.181.2.3  pgoyette 		} else {
   1998  1.181.2.3  pgoyette 			m->m_ext_ref = m;
   1999  1.181.2.3  pgoyette 		}
   2000  1.181.2.3  pgoyette 	} else {
   2001  1.181.2.3  pgoyette 		/*
   2002  1.181.2.3  pgoyette 		 * dropping the last reference
   2003  1.181.2.3  pgoyette 		 */
   2004  1.181.2.3  pgoyette 		if (!embedded) {
   2005  1.181.2.3  pgoyette 			m->m_ext.ext_refcnt++; /* XXX */
   2006  1.181.2.3  pgoyette 			m_ext_free(m->m_ext_ref);
   2007  1.181.2.3  pgoyette 			m->m_ext_ref = m;
   2008  1.181.2.3  pgoyette 		} else if ((m->m_flags & M_EXT_CLUSTER) != 0) {
   2009  1.181.2.3  pgoyette 			pool_cache_put_paddr((struct pool_cache *)
   2010  1.181.2.3  pgoyette 			    m->m_ext.ext_arg,
   2011  1.181.2.3  pgoyette 			    m->m_ext.ext_buf, m->m_ext.ext_paddr);
   2012  1.181.2.3  pgoyette 		} else if (m->m_ext.ext_free) {
   2013  1.181.2.3  pgoyette 			(*m->m_ext.ext_free)(m,
   2014  1.181.2.3  pgoyette 			    m->m_ext.ext_buf, m->m_ext.ext_size,
   2015  1.181.2.3  pgoyette 			    m->m_ext.ext_arg);
   2016  1.181.2.3  pgoyette 			/*
   2017  1.181.2.3  pgoyette 			 * 'm' is already freed by the ext_free callback.
   2018  1.181.2.3  pgoyette 			 */
   2019  1.181.2.3  pgoyette 			dofree = false;
   2020  1.181.2.3  pgoyette 		} else {
   2021  1.181.2.3  pgoyette 			free(m->m_ext.ext_buf, m->m_ext.ext_type);
   2022  1.181.2.3  pgoyette 		}
   2023  1.181.2.3  pgoyette 	}
   2024  1.181.2.3  pgoyette 
   2025  1.181.2.3  pgoyette 	if (dofree) {
   2026  1.181.2.3  pgoyette 		m->m_type = MT_FREE;
   2027  1.181.2.3  pgoyette 		m->m_data = NULL;
   2028  1.181.2.3  pgoyette 		pool_cache_put(mb_cache, m);
   2029  1.181.2.3  pgoyette 	}
   2030  1.181.2.3  pgoyette }
   2031  1.181.2.3  pgoyette 
   2032      1.169  christos /*
   2033      1.175      maxv  * Free a single mbuf and associated external storage. Return the
   2034      1.175      maxv  * successor, if any.
   2035      1.175      maxv  */
   2036      1.169  christos struct mbuf *
   2037      1.175      maxv m_free(struct mbuf *m)
   2038      1.169  christos {
   2039      1.169  christos 	struct mbuf *n;
   2040      1.169  christos 
   2041      1.175      maxv 	mowner_revoke(m, 1, m->m_flags);
   2042      1.175      maxv 	mbstat_type_add(m->m_type, -1);
   2043      1.175      maxv 
   2044      1.175      maxv 	if (m->m_flags & M_PKTHDR)
   2045      1.175      maxv 		m_tag_delete_chain(m, NULL);
   2046      1.175      maxv 
   2047      1.175      maxv 	n = m->m_next;
   2048      1.175      maxv 
   2049      1.175      maxv 	if (m->m_flags & M_EXT) {
   2050      1.175      maxv 		m_ext_free(m);
   2051      1.175      maxv 	} else {
   2052      1.175      maxv 		if (__predict_false(m->m_type == MT_FREE)) {
   2053      1.175      maxv 			panic("mbuf %p already freed", m);
   2054      1.175      maxv 		}
   2055      1.175      maxv 		m->m_type = MT_FREE;
   2056      1.175      maxv 		m->m_data = NULL;
   2057      1.175      maxv 		pool_cache_put(mb_cache, m);
   2058      1.175      maxv 	}
   2059      1.175      maxv 
   2060      1.175      maxv 	return n;
   2061      1.169  christos }
   2062      1.169  christos 
   2063      1.169  christos void
   2064      1.175      maxv m_freem(struct mbuf *m)
   2065      1.169  christos {
   2066      1.169  christos 	if (m == NULL)
   2067      1.169  christos 		return;
   2068      1.169  christos 	do {
   2069      1.175      maxv 		m = m_free(m);
   2070      1.169  christos 	} while (m);
   2071      1.169  christos }
   2072