Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.133
      1  1.133     joerg /*	$NetBSD: uipc_mbuf.c,v 1.133 2010/02/08 19:02:33 joerg Exp $	*/
      2   1.42   thorpej 
      3   1.42   thorpej /*-
      4   1.53   thorpej  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
      5   1.42   thorpej  * All rights reserved.
      6   1.42   thorpej  *
      7   1.42   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.42   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.42   thorpej  * NASA Ames Research Center.
     10   1.42   thorpej  *
     11   1.42   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.42   thorpej  * modification, are permitted provided that the following conditions
     13   1.42   thorpej  * are met:
     14   1.42   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.42   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.42   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.42   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.42   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.42   thorpej  *
     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.133     joerg __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.133 2010/02/08 19:02:33 joerg Exp $");
     66   1.69    martin 
     67   1.69    martin #include "opt_mbuftrace.h"
     68  1.133     joerg #include "opt_nmbclusters.h"
     69  1.105      yamt #include "opt_ddb.h"
     70   1.24       mrg 
     71    1.6   mycroft #include <sys/param.h>
     72    1.6   mycroft #include <sys/systm.h>
     73  1.125      yamt #include <sys/atomic.h>
     74  1.124      yamt #include <sys/cpu.h>
     75    1.6   mycroft #include <sys/proc.h>
     76    1.6   mycroft #include <sys/malloc.h>
     77    1.1       cgd #define MBTYPES
     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.65   thorpej #include <uvm/uvm.h>
     91   1.23       mrg 
     92  1.122        ad pool_cache_t mb_cache;	/* mbuf cache */
     93  1.122        ad pool_cache_t mcl_cache;	/* mbuf cluster cache */
     94   1.53   thorpej 
     95   1.18   thorpej struct mbstat mbstat;
     96   1.18   thorpej int	max_linkhdr;
     97   1.18   thorpej int	max_protohdr;
     98   1.18   thorpej int	max_hdr;
     99   1.18   thorpej int	max_datalen;
    100   1.18   thorpej 
    101   1.65   thorpej static int mb_ctor(void *, void *, int);
    102   1.65   thorpej 
    103  1.103   thorpej static void	*mclpool_alloc(struct pool *, int);
    104  1.103   thorpej static void	mclpool_release(struct pool *, void *);
    105   1.58   thorpej 
    106  1.129     pooka static void	sysctl_kern_mbuf_setup(void);
    107  1.129     pooka 
    108  1.129     pooka static struct sysctllog *mbuf_sysctllog;
    109  1.129     pooka 
    110  1.103   thorpej static struct pool_allocator mclpool_allocator = {
    111  1.113  christos 	.pa_alloc = mclpool_alloc,
    112  1.113  christos 	.pa_free = mclpool_release,
    113   1.58   thorpej };
    114   1.58   thorpej 
    115   1.79  junyoung static struct mbuf *m_copym0(struct mbuf *, int, int, int, int);
    116   1.85      yamt static struct mbuf *m_split0(struct mbuf *, int, int, int);
    117   1.86      yamt static int m_copyback0(struct mbuf **, int, int, const void *, int, int);
    118   1.85      yamt 
    119   1.85      yamt /* flags for m_copyback0 */
    120   1.85      yamt #define	M_COPYBACK0_COPYBACK	0x0001	/* copyback from cp */
    121   1.85      yamt #define	M_COPYBACK0_PRESERVE	0x0002	/* preserve original data */
    122   1.85      yamt #define	M_COPYBACK0_COW		0x0004	/* do copy-on-write */
    123   1.85      yamt #define	M_COPYBACK0_EXTEND	0x0008	/* extend chain */
    124   1.28   thorpej 
    125  1.103   thorpej static const char mclpool_warnmsg[] =
    126  1.133     joerg     "WARNING: mclpool limit reached; increase kern.mbuf.nmbclusters";
    127   1.63   thorpej 
    128   1.63   thorpej MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
    129   1.42   thorpej 
    130  1.124      yamt static percpu_t *mbstat_percpu;
    131  1.124      yamt 
    132   1.64      matt #ifdef MBUFTRACE
    133   1.64      matt struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
    134   1.64      matt struct mowner unknown_mowners[] = {
    135  1.114    dogcow 	MOWNER_INIT("unknown", "free"),
    136  1.114    dogcow 	MOWNER_INIT("unknown", "data"),
    137  1.114    dogcow 	MOWNER_INIT("unknown", "header"),
    138  1.114    dogcow 	MOWNER_INIT("unknown", "soname"),
    139  1.114    dogcow 	MOWNER_INIT("unknown", "soopts"),
    140  1.114    dogcow 	MOWNER_INIT("unknown", "ftable"),
    141  1.114    dogcow 	MOWNER_INIT("unknown", "control"),
    142  1.114    dogcow 	MOWNER_INIT("unknown", "oobdata"),
    143   1.64      matt };
    144  1.114    dogcow struct mowner revoked_mowner = MOWNER_INIT("revoked", "");
    145   1.64      matt #endif
    146   1.64      matt 
    147  1.125      yamt #define	MEXT_ISEMBEDDED(m) ((m)->m_ext_ref == (m))
    148  1.125      yamt 
    149  1.125      yamt #define	MCLADDREFERENCE(o, n)						\
    150  1.125      yamt do {									\
    151  1.125      yamt 	KASSERT(((o)->m_flags & M_EXT) != 0);				\
    152  1.125      yamt 	KASSERT(((n)->m_flags & M_EXT) == 0);				\
    153  1.125      yamt 	KASSERT((o)->m_ext.ext_refcnt >= 1);				\
    154  1.125      yamt 	(n)->m_flags |= ((o)->m_flags & M_EXTCOPYFLAGS);		\
    155  1.125      yamt 	atomic_inc_uint(&(o)->m_ext.ext_refcnt);			\
    156  1.125      yamt 	(n)->m_ext_ref = (o)->m_ext_ref;				\
    157  1.125      yamt 	mowner_ref((n), (n)->m_flags);					\
    158  1.125      yamt 	MCLREFDEBUGN((n), __FILE__, __LINE__);				\
    159  1.125      yamt } while (/* CONSTCOND */ 0)
    160  1.125      yamt 
    161  1.133     joerg static int
    162  1.133     joerg nmbclusters_limit(void)
    163  1.133     joerg {
    164  1.133     joerg #ifdef PMAP_MAP_POOLPAGE
    165  1.133     joerg 	/* direct mapping, doesn't use space in kmem_map */
    166  1.133     joerg 	vsize_t max_size = physmem / 4;
    167  1.133     joerg #else
    168  1.133     joerg 	vsize_t max_size = MIN(physmem / 4, nkmempages / 2);
    169  1.133     joerg #endif
    170  1.133     joerg 
    171  1.133     joerg 	max_size = max_size * PAGE_SIZE / MCLBYTES;
    172  1.133     joerg #ifdef NMBCLUSTERS_MAX
    173  1.133     joerg 	max_size = MIN(max_size, NMBCLUSTERS_MAX);
    174  1.133     joerg #endif
    175  1.133     joerg 
    176  1.133     joerg #ifdef NMBCLUSTERS
    177  1.133     joerg 	return MIN(max_size, NMBCLUSTERS);
    178  1.133     joerg #else
    179  1.133     joerg 	return max_size;
    180  1.133     joerg #endif
    181  1.133     joerg }
    182  1.133     joerg 
    183   1.28   thorpej /*
    184   1.68    simonb  * Initialize the mbuf allocator.
    185   1.28   thorpej  */
    186    1.4       jtc void
    187   1.62   thorpej mbinit(void)
    188    1.1       cgd {
    189   1.65   thorpej 
    190  1.128      matt 	CTASSERT(sizeof(struct _m_ext) <= MHLEN);
    191  1.128      matt 	CTASSERT(sizeof(struct mbuf) == MSIZE);
    192   1.65   thorpej 
    193  1.129     pooka 	sysctl_kern_mbuf_setup();
    194  1.129     pooka 
    195  1.133     joerg 	mclpool_allocator.pa_backingmap = kmem_map;
    196   1.53   thorpej 
    197  1.122        ad 	mb_cache = pool_cache_init(msize, 0, 0, 0, "mbpl",
    198  1.122        ad 	    NULL, IPL_VM, mb_ctor, NULL, NULL);
    199  1.122        ad 	KASSERT(mb_cache != NULL);
    200  1.122        ad 
    201  1.122        ad 	mcl_cache = pool_cache_init(mclbytes, 0, 0, 0, "mclpl",
    202  1.122        ad 	    &mclpool_allocator, IPL_VM, NULL, NULL, NULL);
    203  1.122        ad 	KASSERT(mcl_cache != NULL);
    204   1.59   thorpej 
    205  1.122        ad 	pool_cache_set_drain_hook(mb_cache, m_reclaim, NULL);
    206  1.122        ad 	pool_cache_set_drain_hook(mcl_cache, m_reclaim, NULL);
    207   1.37   thorpej 
    208   1.37   thorpej 	/*
    209  1.133     joerg 	 * Set an arbitrary default limit on the number of mbuf clusters.
    210  1.133     joerg 	 */
    211  1.133     joerg #ifdef NMBCLUSTERS
    212  1.133     joerg 	nmbclusters = nmbclusters_limit();
    213  1.133     joerg #else
    214  1.133     joerg 	nmbclusters = MAX(1024,
    215  1.133     joerg 	    (vsize_t)physmem * PAGE_SIZE / MCLBYTES / 16);
    216  1.133     joerg 	nmbclusters = MIN(nmbclusters, nmbclusters_limit());
    217  1.133     joerg #endif
    218  1.133     joerg 
    219  1.133     joerg 	/*
    220   1.39   thorpej 	 * Set the hard limit on the mclpool to the number of
    221   1.39   thorpej 	 * mbuf clusters the kernel is to support.  Log the limit
    222   1.39   thorpej 	 * reached message max once a minute.
    223   1.39   thorpej 	 */
    224  1.122        ad 	pool_cache_sethardlimit(mcl_cache, nmbclusters, mclpool_warnmsg, 60);
    225   1.42   thorpej 
    226  1.124      yamt 	mbstat_percpu = percpu_alloc(sizeof(struct mbstat_cpu));
    227  1.124      yamt 
    228   1.39   thorpej 	/*
    229   1.42   thorpej 	 * Set a low water mark for both mbufs and clusters.  This should
    230   1.42   thorpej 	 * help ensure that they can be allocated in a memory starvation
    231   1.42   thorpej 	 * situation.  This is important for e.g. diskless systems which
    232   1.42   thorpej 	 * must allocate mbufs in order for the pagedaemon to clean pages.
    233   1.37   thorpej 	 */
    234  1.122        ad 	pool_cache_setlowat(mb_cache, mblowat);
    235  1.122        ad 	pool_cache_setlowat(mcl_cache, mcllowat);
    236   1.64      matt 
    237   1.64      matt #ifdef MBUFTRACE
    238   1.64      matt 	{
    239   1.64      matt 		/*
    240   1.64      matt 		 * Attach the unknown mowners.
    241   1.64      matt 		 */
    242   1.64      matt 		int i;
    243   1.64      matt 		MOWNER_ATTACH(&revoked_mowner);
    244   1.64      matt 		for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
    245   1.64      matt 		     i-- > 0; )
    246   1.64      matt 			MOWNER_ATTACH(&unknown_mowners[i]);
    247   1.64      matt 	}
    248   1.64      matt #endif
    249   1.42   thorpej }
    250   1.42   thorpej 
    251   1.75    atatat /*
    252  1.133     joerg  * sysctl helper routine for the kern.mbuf subtree.
    253  1.133     joerg  * nmbclusters, mblowat and mcllowat need range
    254   1.75    atatat  * checking and pool tweaking after being reset.
    255   1.75    atatat  */
    256   1.75    atatat static int
    257   1.75    atatat sysctl_kern_mbuf(SYSCTLFN_ARGS)
    258   1.42   thorpej {
    259   1.42   thorpej 	int error, newval;
    260   1.75    atatat 	struct sysctlnode node;
    261   1.42   thorpej 
    262   1.75    atatat 	node = *rnode;
    263   1.75    atatat 	node.sysctl_data = &newval;
    264   1.75    atatat 	switch (rnode->sysctl_num) {
    265   1.42   thorpej 	case MBUF_NMBCLUSTERS:
    266   1.42   thorpej 	case MBUF_MBLOWAT:
    267   1.42   thorpej 	case MBUF_MCLLOWAT:
    268   1.75    atatat 		newval = *(int*)rnode->sysctl_data;
    269   1.75    atatat 		break;
    270   1.75    atatat 	default:
    271   1.75    atatat 		return (EOPNOTSUPP);
    272   1.75    atatat 	}
    273   1.75    atatat 
    274   1.75    atatat 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    275   1.75    atatat 	if (error || newp == NULL)
    276   1.42   thorpej 		return (error);
    277   1.75    atatat 	if (newval < 0)
    278   1.75    atatat 		return (EINVAL);
    279   1.75    atatat 
    280   1.75    atatat 	switch (node.sysctl_num) {
    281   1.75    atatat 	case MBUF_NMBCLUSTERS:
    282   1.75    atatat 		if (newval < nmbclusters)
    283   1.75    atatat 			return (EINVAL);
    284  1.133     joerg 		if (newval > nmbclusters_limit())
    285  1.133     joerg 			return (EINVAL);
    286   1.75    atatat 		nmbclusters = newval;
    287  1.122        ad 		pool_cache_sethardlimit(mcl_cache, nmbclusters,
    288  1.122        ad 		    mclpool_warnmsg, 60);
    289   1.75    atatat 		break;
    290   1.75    atatat 	case MBUF_MBLOWAT:
    291   1.75    atatat 		mblowat = newval;
    292  1.122        ad 		pool_cache_setlowat(mb_cache, mblowat);
    293   1.75    atatat 		break;
    294   1.75    atatat 	case MBUF_MCLLOWAT:
    295   1.76    atatat 		mcllowat = newval;
    296  1.122        ad 		pool_cache_setlowat(mcl_cache, mcllowat);
    297   1.75    atatat 		break;
    298   1.75    atatat 	}
    299   1.75    atatat 
    300   1.75    atatat 	return (0);
    301   1.75    atatat }
    302   1.75    atatat 
    303   1.64      matt #ifdef MBUFTRACE
    304  1.124      yamt static void
    305  1.124      yamt mowner_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
    306  1.124      yamt {
    307  1.124      yamt 	struct mowner_counter *mc = v1;
    308  1.124      yamt 	struct mowner_user *mo_user = v2;
    309  1.124      yamt 	int i;
    310  1.124      yamt 
    311  1.124      yamt 	for (i = 0; i < MOWNER_COUNTER_NCOUNTERS; i++) {
    312  1.124      yamt 		mo_user->mo_counter[i] += mc->mc_counter[i];
    313  1.124      yamt 	}
    314  1.124      yamt }
    315  1.124      yamt 
    316  1.124      yamt static void
    317  1.124      yamt mowner_convert_to_user(struct mowner *mo, struct mowner_user *mo_user)
    318  1.124      yamt {
    319  1.124      yamt 
    320  1.124      yamt 	memset(mo_user, 0, sizeof(*mo_user));
    321  1.128      matt 	CTASSERT(sizeof(mo_user->mo_name) == sizeof(mo->mo_name));
    322  1.128      matt 	CTASSERT(sizeof(mo_user->mo_descr) == sizeof(mo->mo_descr));
    323  1.124      yamt 	memcpy(mo_user->mo_name, mo->mo_name, sizeof(mo->mo_name));
    324  1.124      yamt 	memcpy(mo_user->mo_descr, mo->mo_descr, sizeof(mo->mo_descr));
    325  1.124      yamt 	percpu_foreach(mo->mo_counters, mowner_conver_to_user_cb, mo_user);
    326  1.124      yamt }
    327  1.124      yamt 
    328   1.75    atatat static int
    329   1.75    atatat sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
    330   1.75    atatat {
    331   1.75    atatat 	struct mowner *mo;
    332   1.75    atatat 	size_t len = 0;
    333   1.75    atatat 	int error = 0;
    334   1.75    atatat 
    335   1.75    atatat 	if (namelen != 0)
    336   1.75    atatat 		return (EINVAL);
    337   1.75    atatat 	if (newp != NULL)
    338   1.75    atatat 		return (EPERM);
    339   1.75    atatat 
    340   1.75    atatat 	LIST_FOREACH(mo, &mowners, mo_link) {
    341  1.124      yamt 		struct mowner_user mo_user;
    342  1.124      yamt 
    343  1.124      yamt 		mowner_convert_to_user(mo, &mo_user);
    344  1.124      yamt 
    345   1.75    atatat 		if (oldp != NULL) {
    346  1.124      yamt 			if (*oldlenp - len < sizeof(mo_user)) {
    347   1.75    atatat 				error = ENOMEM;
    348   1.75    atatat 				break;
    349   1.75    atatat 			}
    350  1.124      yamt 			error = copyout(&mo_user, (char *)oldp + len,
    351  1.124      yamt 			    sizeof(mo_user));
    352   1.75    atatat 			if (error)
    353   1.75    atatat 				break;
    354   1.64      matt 		}
    355  1.124      yamt 		len += sizeof(mo_user);
    356   1.75    atatat 	}
    357   1.75    atatat 
    358   1.75    atatat 	if (error == 0)
    359   1.64      matt 		*oldlenp = len;
    360   1.75    atatat 
    361   1.75    atatat 	return (error);
    362   1.75    atatat }
    363   1.75    atatat #endif /* MBUFTRACE */
    364   1.75    atatat 
    365  1.124      yamt static void
    366  1.124      yamt mbstat_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
    367  1.124      yamt {
    368  1.124      yamt 	struct mbstat_cpu *mbsc = v1;
    369  1.124      yamt 	struct mbstat *mbs = v2;
    370  1.124      yamt 	int i;
    371  1.124      yamt 
    372  1.124      yamt 	for (i = 0; i < __arraycount(mbs->m_mtypes); i++) {
    373  1.124      yamt 		mbs->m_mtypes[i] += mbsc->m_mtypes[i];
    374  1.124      yamt 	}
    375  1.124      yamt }
    376  1.124      yamt 
    377  1.124      yamt static void
    378  1.124      yamt mbstat_convert_to_user(struct mbstat *mbs)
    379  1.124      yamt {
    380  1.124      yamt 
    381  1.124      yamt 	memset(mbs, 0, sizeof(*mbs));
    382  1.124      yamt 	mbs->m_drain = mbstat.m_drain;
    383  1.124      yamt 	percpu_foreach(mbstat_percpu, mbstat_conver_to_user_cb, mbs);
    384  1.124      yamt }
    385  1.124      yamt 
    386  1.124      yamt static int
    387  1.124      yamt sysctl_kern_mbuf_stats(SYSCTLFN_ARGS)
    388  1.124      yamt {
    389  1.124      yamt 	struct sysctlnode node;
    390  1.124      yamt 	struct mbstat mbs;
    391  1.124      yamt 
    392  1.124      yamt 	mbstat_convert_to_user(&mbs);
    393  1.124      yamt 	node = *rnode;
    394  1.124      yamt 	node.sysctl_data = &mbs;
    395  1.124      yamt 	node.sysctl_size = sizeof(mbs);
    396  1.124      yamt 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    397  1.124      yamt }
    398  1.124      yamt 
    399  1.129     pooka static void
    400  1.131    cegger sysctl_kern_mbuf_setup(void)
    401   1.75    atatat {
    402   1.75    atatat 
    403  1.129     pooka 	KASSERT(mbuf_sysctllog == NULL);
    404  1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    405   1.80    atatat 		       CTLFLAG_PERMANENT,
    406   1.75    atatat 		       CTLTYPE_NODE, "kern", NULL,
    407   1.75    atatat 		       NULL, 0, NULL, 0,
    408   1.75    atatat 		       CTL_KERN, CTL_EOL);
    409  1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    410   1.80    atatat 		       CTLFLAG_PERMANENT,
    411   1.82    atatat 		       CTLTYPE_NODE, "mbuf",
    412   1.82    atatat 		       SYSCTL_DESCR("mbuf control variables"),
    413   1.75    atatat 		       NULL, 0, NULL, 0,
    414   1.75    atatat 		       CTL_KERN, KERN_MBUF, CTL_EOL);
    415   1.75    atatat 
    416  1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    417   1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    418   1.82    atatat 		       CTLTYPE_INT, "msize",
    419   1.82    atatat 		       SYSCTL_DESCR("mbuf base size"),
    420   1.75    atatat 		       NULL, msize, NULL, 0,
    421   1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
    422  1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    423   1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    424   1.82    atatat 		       CTLTYPE_INT, "mclbytes",
    425   1.82    atatat 		       SYSCTL_DESCR("mbuf cluster size"),
    426   1.75    atatat 		       NULL, mclbytes, NULL, 0,
    427   1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
    428  1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    429   1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    430   1.82    atatat 		       CTLTYPE_INT, "nmbclusters",
    431   1.82    atatat 		       SYSCTL_DESCR("Limit on the number of mbuf clusters"),
    432   1.75    atatat 		       sysctl_kern_mbuf, 0, &nmbclusters, 0,
    433   1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
    434  1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    435   1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    436   1.82    atatat 		       CTLTYPE_INT, "mblowat",
    437   1.82    atatat 		       SYSCTL_DESCR("mbuf low water mark"),
    438   1.75    atatat 		       sysctl_kern_mbuf, 0, &mblowat, 0,
    439   1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
    440  1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    441   1.80    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    442   1.82    atatat 		       CTLTYPE_INT, "mcllowat",
    443   1.82    atatat 		       SYSCTL_DESCR("mbuf cluster low water mark"),
    444   1.75    atatat 		       sysctl_kern_mbuf, 0, &mcllowat, 0,
    445   1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
    446  1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    447   1.80    atatat 		       CTLFLAG_PERMANENT,
    448   1.82    atatat 		       CTLTYPE_STRUCT, "stats",
    449   1.82    atatat 		       SYSCTL_DESCR("mbuf allocation statistics"),
    450  1.124      yamt 		       sysctl_kern_mbuf_stats, 0, NULL, 0,
    451   1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
    452   1.75    atatat #ifdef MBUFTRACE
    453  1.129     pooka 	sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
    454   1.80    atatat 		       CTLFLAG_PERMANENT,
    455   1.82    atatat 		       CTLTYPE_STRUCT, "mowners",
    456   1.82    atatat 		       SYSCTL_DESCR("Information about mbuf owners"),
    457   1.75    atatat 		       sysctl_kern_mbuf_mowners, 0, NULL, 0,
    458   1.75    atatat 		       CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
    459   1.75    atatat #endif /* MBUFTRACE */
    460   1.28   thorpej }
    461   1.28   thorpej 
    462  1.103   thorpej static void *
    463  1.116      yamt mclpool_alloc(struct pool *pp, int flags)
    464   1.28   thorpej {
    465  1.118   thorpej 	bool waitok = (flags & PR_WAITOK) ? true : false;
    466   1.28   thorpej 
    467  1.133     joerg 	return ((void *)uvm_km_alloc_poolpage(kmem_map, waitok));
    468    1.1       cgd }
    469    1.1       cgd 
    470  1.103   thorpej static void
    471  1.116      yamt mclpool_release(struct pool *pp, void *v)
    472    1.1       cgd {
    473    1.1       cgd 
    474  1.133     joerg 	uvm_km_free_poolpage(kmem_map, (vaddr_t)v);
    475   1.65   thorpej }
    476   1.65   thorpej 
    477   1.65   thorpej /*ARGSUSED*/
    478   1.65   thorpej static int
    479  1.116      yamt mb_ctor(void *arg, void *object, int flags)
    480   1.65   thorpej {
    481   1.65   thorpej 	struct mbuf *m = object;
    482   1.65   thorpej 
    483   1.65   thorpej #ifdef POOL_VTOPHYS
    484   1.65   thorpej 	m->m_paddr = POOL_VTOPHYS(m);
    485   1.65   thorpej #else
    486   1.65   thorpej 	m->m_paddr = M_PADDR_INVALID;
    487   1.65   thorpej #endif
    488   1.65   thorpej 	return (0);
    489    1.1       cgd }
    490    1.1       cgd 
    491   1.14  christos void
    492  1.116      yamt m_reclaim(void *arg, int flags)
    493    1.1       cgd {
    494   1.27      matt 	struct domain *dp;
    495   1.81      matt 	const struct protosw *pr;
    496   1.27      matt 	struct ifnet *ifp;
    497  1.122        ad 	int s;
    498    1.1       cgd 
    499  1.122        ad 	KERNEL_LOCK(1, NULL);
    500  1.122        ad 	s = splvm();
    501   1.91      matt 	DOMAIN_FOREACH(dp) {
    502   1.33   thorpej 		for (pr = dp->dom_protosw;
    503   1.33   thorpej 		     pr < dp->dom_protoswNPROTOSW; pr++)
    504   1.33   thorpej 			if (pr->pr_drain)
    505   1.33   thorpej 				(*pr->pr_drain)();
    506   1.91      matt 	}
    507   1.92      matt 	IFNET_FOREACH(ifp) {
    508   1.27      matt 		if (ifp->if_drain)
    509   1.27      matt 			(*ifp->if_drain)(ifp);
    510   1.92      matt 	}
    511    1.1       cgd 	splx(s);
    512    1.1       cgd 	mbstat.m_drain++;
    513  1.122        ad 	KERNEL_UNLOCK_ONE(NULL);
    514    1.1       cgd }
    515    1.1       cgd 
    516    1.1       cgd /*
    517    1.1       cgd  * Space allocation routines.
    518    1.1       cgd  * These are also available as macros
    519    1.1       cgd  * for critical paths.
    520    1.1       cgd  */
    521    1.1       cgd struct mbuf *
    522   1.62   thorpej m_get(int nowait, int type)
    523    1.1       cgd {
    524   1.27      matt 	struct mbuf *m;
    525    1.1       cgd 
    526  1.124      yamt 	m = pool_cache_get(mb_cache,
    527  1.124      yamt 	    nowait == M_WAIT ? PR_WAITOK|PR_LIMITFAIL : 0);
    528  1.124      yamt 	if (m == NULL)
    529  1.124      yamt 		return NULL;
    530  1.124      yamt 
    531  1.124      yamt 	mbstat_type_add(type, 1);
    532  1.124      yamt 	mowner_init(m, type);
    533  1.125      yamt 	m->m_ext_ref = m;
    534  1.124      yamt 	m->m_type = type;
    535  1.124      yamt 	m->m_next = NULL;
    536  1.124      yamt 	m->m_nextpkt = NULL;
    537  1.124      yamt 	m->m_data = m->m_dat;
    538  1.124      yamt 	m->m_flags = 0;
    539  1.124      yamt 
    540  1.124      yamt 	return m;
    541    1.1       cgd }
    542    1.1       cgd 
    543    1.1       cgd struct mbuf *
    544   1.62   thorpej m_gethdr(int nowait, int type)
    545    1.1       cgd {
    546   1.27      matt 	struct mbuf *m;
    547    1.1       cgd 
    548  1.124      yamt 	m = m_get(nowait, type);
    549  1.124      yamt 	if (m == NULL)
    550  1.124      yamt 		return NULL;
    551  1.124      yamt 
    552  1.124      yamt 	m->m_data = m->m_pktdat;
    553  1.124      yamt 	m->m_flags = M_PKTHDR;
    554  1.124      yamt 	m->m_pkthdr.rcvif = NULL;
    555  1.124      yamt 	m->m_pkthdr.csum_flags = 0;
    556  1.124      yamt 	m->m_pkthdr.csum_data = 0;
    557  1.124      yamt 	SLIST_INIT(&m->m_pkthdr.tags);
    558  1.124      yamt 
    559  1.124      yamt 	return m;
    560    1.1       cgd }
    561    1.1       cgd 
    562    1.1       cgd struct mbuf *
    563   1.62   thorpej m_getclr(int nowait, int type)
    564    1.1       cgd {
    565   1.27      matt 	struct mbuf *m;
    566    1.1       cgd 
    567    1.5       cgd 	MGET(m, nowait, type);
    568    1.1       cgd 	if (m == 0)
    569   1.71    simonb 		return (NULL);
    570  1.119  christos 	memset(mtod(m, void *), 0, MLEN);
    571    1.1       cgd 	return (m);
    572    1.1       cgd }
    573    1.1       cgd 
    574   1.64      matt void
    575   1.64      matt m_clget(struct mbuf *m, int nowait)
    576   1.64      matt {
    577   1.71    simonb 
    578   1.64      matt 	MCLGET(m, nowait);
    579   1.64      matt }
    580   1.64      matt 
    581    1.1       cgd struct mbuf *
    582   1.62   thorpej m_free(struct mbuf *m)
    583    1.1       cgd {
    584   1.27      matt 	struct mbuf *n;
    585    1.1       cgd 
    586    1.1       cgd 	MFREE(m, n);
    587    1.1       cgd 	return (n);
    588    1.1       cgd }
    589    1.1       cgd 
    590    1.9   mycroft void
    591   1.62   thorpej m_freem(struct mbuf *m)
    592    1.1       cgd {
    593   1.27      matt 	struct mbuf *n;
    594    1.1       cgd 
    595    1.1       cgd 	if (m == NULL)
    596    1.1       cgd 		return;
    597    1.1       cgd 	do {
    598    1.1       cgd 		MFREE(m, n);
    599   1.18   thorpej 		m = n;
    600   1.18   thorpej 	} while (m);
    601    1.1       cgd }
    602    1.1       cgd 
    603   1.64      matt #ifdef MBUFTRACE
    604   1.83  jonathan /*
    605   1.83  jonathan  * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
    606   1.83  jonathan  */
    607   1.64      matt void
    608   1.83  jonathan m_claimm(struct mbuf *m, struct mowner *mo)
    609   1.64      matt {
    610   1.71    simonb 
    611   1.64      matt 	for (; m != NULL; m = m->m_next)
    612   1.64      matt 		MCLAIM(m, mo);
    613   1.64      matt }
    614   1.64      matt #endif
    615   1.64      matt 
    616    1.1       cgd /*
    617    1.1       cgd  * Mbuffer utility routines.
    618    1.1       cgd  */
    619    1.1       cgd 
    620    1.1       cgd /*
    621    1.1       cgd  * Lesser-used path for M_PREPEND:
    622    1.1       cgd  * allocate new mbuf to prepend to chain,
    623    1.1       cgd  * copy junk along.
    624    1.1       cgd  */
    625    1.1       cgd struct mbuf *
    626   1.62   thorpej m_prepend(struct mbuf *m, int len, int how)
    627    1.1       cgd {
    628    1.1       cgd 	struct mbuf *mn;
    629    1.1       cgd 
    630    1.9   mycroft 	MGET(mn, how, m->m_type);
    631    1.1       cgd 	if (mn == (struct mbuf *)NULL) {
    632    1.1       cgd 		m_freem(m);
    633    1.1       cgd 		return ((struct mbuf *)NULL);
    634    1.1       cgd 	}
    635    1.1       cgd 	if (m->m_flags & M_PKTHDR) {
    636  1.101      yamt 		M_MOVE_PKTHDR(mn, m);
    637   1.64      matt 	} else {
    638   1.64      matt 		MCLAIM(mn, m->m_owner);
    639    1.1       cgd 	}
    640    1.1       cgd 	mn->m_next = m;
    641    1.1       cgd 	m = mn;
    642    1.1       cgd 	if (len < MHLEN)
    643    1.1       cgd 		MH_ALIGN(m, len);
    644    1.1       cgd 	m->m_len = len;
    645    1.1       cgd 	return (m);
    646    1.1       cgd }
    647    1.1       cgd 
    648    1.1       cgd /*
    649    1.1       cgd  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    650    1.1       cgd  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    651    1.1       cgd  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    652    1.1       cgd  */
    653    1.1       cgd int MCFail;
    654    1.1       cgd 
    655    1.1       cgd struct mbuf *
    656   1.62   thorpej m_copym(struct mbuf *m, int off0, int len, int wait)
    657    1.1       cgd {
    658   1.71    simonb 
    659   1.44    itojun 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    660   1.44    itojun }
    661   1.44    itojun 
    662   1.44    itojun struct mbuf *
    663   1.62   thorpej m_dup(struct mbuf *m, int off0, int len, int wait)
    664   1.44    itojun {
    665   1.71    simonb 
    666   1.44    itojun 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    667   1.44    itojun }
    668   1.44    itojun 
    669   1.44    itojun static struct mbuf *
    670   1.62   thorpej m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
    671   1.44    itojun {
    672   1.27      matt 	struct mbuf *n, **np;
    673   1.27      matt 	int off = off0;
    674    1.1       cgd 	struct mbuf *top;
    675    1.1       cgd 	int copyhdr = 0;
    676    1.1       cgd 
    677    1.1       cgd 	if (off < 0 || len < 0)
    678   1.43   thorpej 		panic("m_copym: off %d, len %d", off, len);
    679    1.1       cgd 	if (off == 0 && m->m_flags & M_PKTHDR)
    680    1.1       cgd 		copyhdr = 1;
    681    1.1       cgd 	while (off > 0) {
    682    1.1       cgd 		if (m == 0)
    683   1.90      matt 			panic("m_copym: m == 0, off %d", off);
    684    1.1       cgd 		if (off < m->m_len)
    685    1.1       cgd 			break;
    686    1.1       cgd 		off -= m->m_len;
    687    1.1       cgd 		m = m->m_next;
    688    1.1       cgd 	}
    689    1.1       cgd 	np = &top;
    690    1.1       cgd 	top = 0;
    691    1.1       cgd 	while (len > 0) {
    692    1.1       cgd 		if (m == 0) {
    693    1.1       cgd 			if (len != M_COPYALL)
    694   1.90      matt 				panic("m_copym: m == 0, len %d [!COPYALL]",
    695   1.90      matt 				    len);
    696    1.1       cgd 			break;
    697    1.1       cgd 		}
    698    1.1       cgd 		MGET(n, wait, m->m_type);
    699    1.1       cgd 		*np = n;
    700    1.1       cgd 		if (n == 0)
    701    1.1       cgd 			goto nospace;
    702   1.64      matt 		MCLAIM(n, m->m_owner);
    703    1.1       cgd 		if (copyhdr) {
    704    1.1       cgd 			M_COPY_PKTHDR(n, m);
    705    1.1       cgd 			if (len == M_COPYALL)
    706    1.1       cgd 				n->m_pkthdr.len -= off0;
    707    1.1       cgd 			else
    708    1.1       cgd 				n->m_pkthdr.len = len;
    709    1.1       cgd 			copyhdr = 0;
    710    1.1       cgd 		}
    711    1.9   mycroft 		n->m_len = min(len, m->m_len - off);
    712    1.1       cgd 		if (m->m_flags & M_EXT) {
    713   1.44    itojun 			if (!deep) {
    714   1.44    itojun 				n->m_data = m->m_data + off;
    715   1.44    itojun 				MCLADDREFERENCE(m, n);
    716   1.44    itojun 			} else {
    717   1.48    itojun 				/*
    718   1.50    itojun 				 * we are unsure about the way m was allocated.
    719   1.50    itojun 				 * copy into multiple MCLBYTES cluster mbufs.
    720   1.48    itojun 				 */
    721   1.44    itojun 				MCLGET(n, wait);
    722   1.50    itojun 				n->m_len = 0;
    723   1.50    itojun 				n->m_len = M_TRAILINGSPACE(n);
    724   1.50    itojun 				n->m_len = min(n->m_len, len);
    725   1.50    itojun 				n->m_len = min(n->m_len, m->m_len - off);
    726  1.119  christos 				memcpy(mtod(n, void *), mtod(m, char *) + off,
    727   1.44    itojun 				    (unsigned)n->m_len);
    728   1.44    itojun 			}
    729    1.1       cgd 		} else
    730  1.119  christos 			memcpy(mtod(n, void *), mtod(m, char *) + off,
    731    1.1       cgd 			    (unsigned)n->m_len);
    732    1.1       cgd 		if (len != M_COPYALL)
    733    1.1       cgd 			len -= n->m_len;
    734   1.50    itojun 		off += n->m_len;
    735   1.50    itojun #ifdef DIAGNOSTIC
    736   1.50    itojun 		if (off > m->m_len)
    737   1.50    itojun 			panic("m_copym0 overrun");
    738   1.50    itojun #endif
    739   1.50    itojun 		if (off == m->m_len) {
    740   1.50    itojun 			m = m->m_next;
    741   1.50    itojun 			off = 0;
    742   1.50    itojun 		}
    743    1.1       cgd 		np = &n->m_next;
    744    1.1       cgd 	}
    745    1.1       cgd 	if (top == 0)
    746    1.1       cgd 		MCFail++;
    747    1.1       cgd 	return (top);
    748    1.1       cgd nospace:
    749    1.1       cgd 	m_freem(top);
    750    1.1       cgd 	MCFail++;
    751   1.71    simonb 	return (NULL);
    752    1.1       cgd }
    753    1.1       cgd 
    754    1.1       cgd /*
    755   1.18   thorpej  * Copy an entire packet, including header (which must be present).
    756   1.18   thorpej  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    757   1.18   thorpej  */
    758   1.18   thorpej struct mbuf *
    759   1.62   thorpej m_copypacket(struct mbuf *m, int how)
    760   1.18   thorpej {
    761   1.18   thorpej 	struct mbuf *top, *n, *o;
    762   1.18   thorpej 
    763   1.18   thorpej 	MGET(n, how, m->m_type);
    764   1.18   thorpej 	top = n;
    765   1.18   thorpej 	if (!n)
    766   1.18   thorpej 		goto nospace;
    767   1.18   thorpej 
    768   1.64      matt 	MCLAIM(n, m->m_owner);
    769   1.18   thorpej 	M_COPY_PKTHDR(n, m);
    770   1.18   thorpej 	n->m_len = m->m_len;
    771   1.18   thorpej 	if (m->m_flags & M_EXT) {
    772   1.18   thorpej 		n->m_data = m->m_data;
    773   1.18   thorpej 		MCLADDREFERENCE(m, n);
    774   1.18   thorpej 	} else {
    775   1.30     perry 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    776   1.18   thorpej 	}
    777   1.18   thorpej 
    778   1.18   thorpej 	m = m->m_next;
    779   1.18   thorpej 	while (m) {
    780   1.18   thorpej 		MGET(o, how, m->m_type);
    781   1.18   thorpej 		if (!o)
    782   1.18   thorpej 			goto nospace;
    783   1.18   thorpej 
    784   1.64      matt 		MCLAIM(o, m->m_owner);
    785   1.18   thorpej 		n->m_next = o;
    786   1.18   thorpej 		n = n->m_next;
    787   1.18   thorpej 
    788   1.18   thorpej 		n->m_len = m->m_len;
    789   1.18   thorpej 		if (m->m_flags & M_EXT) {
    790   1.18   thorpej 			n->m_data = m->m_data;
    791   1.18   thorpej 			MCLADDREFERENCE(m, n);
    792   1.18   thorpej 		} else {
    793   1.30     perry 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    794   1.18   thorpej 		}
    795   1.18   thorpej 
    796   1.18   thorpej 		m = m->m_next;
    797   1.18   thorpej 	}
    798   1.18   thorpej 	return top;
    799   1.18   thorpej nospace:
    800   1.18   thorpej 	m_freem(top);
    801   1.18   thorpej 	MCFail++;
    802   1.71    simonb 	return NULL;
    803   1.18   thorpej }
    804   1.18   thorpej 
    805   1.18   thorpej /*
    806    1.1       cgd  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    807    1.1       cgd  * continuing for "len" bytes, into the indicated buffer.
    808    1.1       cgd  */
    809   1.14  christos void
    810  1.100    martin m_copydata(struct mbuf *m, int off, int len, void *vp)
    811    1.1       cgd {
    812   1.94      tron 	unsigned	count;
    813  1.119  christos 	void *		cp = vp;
    814    1.1       cgd 
    815    1.1       cgd 	if (off < 0 || len < 0)
    816   1.90      matt 		panic("m_copydata: off %d, len %d", off, len);
    817    1.1       cgd 	while (off > 0) {
    818   1.94      tron 		if (m == NULL)
    819   1.94      tron 			panic("m_copydata: m == NULL, off %d", off);
    820    1.1       cgd 		if (off < m->m_len)
    821    1.1       cgd 			break;
    822    1.1       cgd 		off -= m->m_len;
    823    1.1       cgd 		m = m->m_next;
    824    1.1       cgd 	}
    825    1.1       cgd 	while (len > 0) {
    826   1.94      tron 		if (m == NULL)
    827   1.94      tron 			panic("m_copydata: m == NULL, len %d", len);
    828    1.9   mycroft 		count = min(m->m_len - off, len);
    829  1.119  christos 		memcpy(cp, mtod(m, char *) + off, count);
    830    1.1       cgd 		len -= count;
    831  1.119  christos 		cp = (char *)cp + count;
    832    1.1       cgd 		off = 0;
    833    1.1       cgd 		m = m->m_next;
    834    1.1       cgd 	}
    835    1.1       cgd }
    836    1.1       cgd 
    837    1.1       cgd /*
    838    1.1       cgd  * Concatenate mbuf chain n to m.
    839   1.72    itojun  * n might be copied into m (when n->m_len is small), therefore data portion of
    840   1.72    itojun  * n could be copied into an mbuf of different mbuf type.
    841    1.1       cgd  * Any m_pkthdr is not updated.
    842    1.1       cgd  */
    843   1.14  christos void
    844   1.62   thorpej m_cat(struct mbuf *m, struct mbuf *n)
    845    1.1       cgd {
    846   1.73      yamt 
    847    1.1       cgd 	while (m->m_next)
    848    1.1       cgd 		m = m->m_next;
    849    1.1       cgd 	while (n) {
    850   1.77    itojun 		if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
    851    1.1       cgd 			/* just join the two chains */
    852    1.1       cgd 			m->m_next = n;
    853    1.1       cgd 			return;
    854    1.1       cgd 		}
    855    1.1       cgd 		/* splat the data from one into the other */
    856  1.119  christos 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
    857    1.1       cgd 		    (u_int)n->m_len);
    858    1.1       cgd 		m->m_len += n->m_len;
    859    1.1       cgd 		n = m_free(n);
    860    1.1       cgd 	}
    861    1.1       cgd }
    862    1.1       cgd 
    863   1.11   mycroft void
    864   1.62   thorpej m_adj(struct mbuf *mp, int req_len)
    865    1.1       cgd {
    866   1.27      matt 	int len = req_len;
    867   1.27      matt 	struct mbuf *m;
    868   1.27      matt 	int count;
    869    1.1       cgd 
    870    1.1       cgd 	if ((m = mp) == NULL)
    871    1.1       cgd 		return;
    872    1.1       cgd 	if (len >= 0) {
    873    1.1       cgd 		/*
    874    1.1       cgd 		 * Trim from head.
    875    1.1       cgd 		 */
    876    1.1       cgd 		while (m != NULL && len > 0) {
    877    1.1       cgd 			if (m->m_len <= len) {
    878    1.1       cgd 				len -= m->m_len;
    879    1.1       cgd 				m->m_len = 0;
    880    1.1       cgd 				m = m->m_next;
    881    1.1       cgd 			} else {
    882    1.1       cgd 				m->m_len -= len;
    883    1.1       cgd 				m->m_data += len;
    884    1.1       cgd 				len = 0;
    885    1.1       cgd 			}
    886    1.1       cgd 		}
    887    1.1       cgd 		m = mp;
    888    1.1       cgd 		if (mp->m_flags & M_PKTHDR)
    889    1.1       cgd 			m->m_pkthdr.len -= (req_len - len);
    890    1.1       cgd 	} else {
    891    1.1       cgd 		/*
    892    1.1       cgd 		 * Trim from tail.  Scan the mbuf chain,
    893    1.1       cgd 		 * calculating its length and finding the last mbuf.
    894    1.1       cgd 		 * If the adjustment only affects this mbuf, then just
    895    1.1       cgd 		 * adjust and return.  Otherwise, rescan and truncate
    896    1.1       cgd 		 * after the remaining size.
    897    1.1       cgd 		 */
    898    1.1       cgd 		len = -len;
    899    1.1       cgd 		count = 0;
    900    1.1       cgd 		for (;;) {
    901    1.1       cgd 			count += m->m_len;
    902    1.1       cgd 			if (m->m_next == (struct mbuf *)0)
    903    1.1       cgd 				break;
    904    1.1       cgd 			m = m->m_next;
    905    1.1       cgd 		}
    906    1.1       cgd 		if (m->m_len >= len) {
    907    1.1       cgd 			m->m_len -= len;
    908    1.8   deraadt 			if (mp->m_flags & M_PKTHDR)
    909    1.8   deraadt 				mp->m_pkthdr.len -= len;
    910    1.1       cgd 			return;
    911    1.1       cgd 		}
    912    1.1       cgd 		count -= len;
    913    1.1       cgd 		if (count < 0)
    914    1.1       cgd 			count = 0;
    915    1.1       cgd 		/*
    916    1.1       cgd 		 * Correct length for chain is "count".
    917    1.1       cgd 		 * Find the mbuf with last data, adjust its length,
    918    1.1       cgd 		 * and toss data from remaining mbufs on chain.
    919    1.1       cgd 		 */
    920    1.1       cgd 		m = mp;
    921    1.1       cgd 		if (m->m_flags & M_PKTHDR)
    922    1.1       cgd 			m->m_pkthdr.len = count;
    923    1.1       cgd 		for (; m; m = m->m_next) {
    924    1.1       cgd 			if (m->m_len >= count) {
    925    1.1       cgd 				m->m_len = count;
    926    1.1       cgd 				break;
    927    1.1       cgd 			}
    928    1.1       cgd 			count -= m->m_len;
    929    1.1       cgd 		}
    930  1.110  christos 		if (m)
    931  1.110  christos 			while (m->m_next)
    932  1.110  christos 				(m = m->m_next)->m_len = 0;
    933    1.1       cgd 	}
    934    1.1       cgd }
    935    1.1       cgd 
    936    1.1       cgd /*
    937  1.102     skrll  * Rearrange an mbuf chain so that len bytes are contiguous
    938    1.1       cgd  * and in the data area of an mbuf (so that mtod and dtom
    939    1.1       cgd  * will work for a structure of size len).  Returns the resulting
    940    1.1       cgd  * mbuf chain on success, frees it and returns null on failure.
    941    1.1       cgd  * If there is room, it will add up to max_protohdr-len extra bytes to the
    942    1.1       cgd  * contiguous region in an attempt to avoid being called next time.
    943    1.1       cgd  */
    944    1.1       cgd int MPFail;
    945    1.1       cgd 
    946    1.1       cgd struct mbuf *
    947   1.62   thorpej m_pullup(struct mbuf *n, int len)
    948    1.1       cgd {
    949   1.27      matt 	struct mbuf *m;
    950   1.27      matt 	int count;
    951    1.1       cgd 	int space;
    952    1.1       cgd 
    953    1.1       cgd 	/*
    954    1.1       cgd 	 * If first mbuf has no cluster, and has room for len bytes
    955    1.1       cgd 	 * without shifting current data, pullup into it,
    956    1.1       cgd 	 * otherwise allocate a new mbuf to prepend to the chain.
    957    1.1       cgd 	 */
    958    1.1       cgd 	if ((n->m_flags & M_EXT) == 0 &&
    959    1.1       cgd 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    960    1.1       cgd 		if (n->m_len >= len)
    961    1.1       cgd 			return (n);
    962    1.1       cgd 		m = n;
    963    1.1       cgd 		n = n->m_next;
    964    1.1       cgd 		len -= m->m_len;
    965    1.1       cgd 	} else {
    966    1.1       cgd 		if (len > MHLEN)
    967    1.1       cgd 			goto bad;
    968    1.1       cgd 		MGET(m, M_DONTWAIT, n->m_type);
    969    1.1       cgd 		if (m == 0)
    970    1.1       cgd 			goto bad;
    971   1.64      matt 		MCLAIM(m, n->m_owner);
    972    1.1       cgd 		m->m_len = 0;
    973    1.1       cgd 		if (n->m_flags & M_PKTHDR) {
    974  1.101      yamt 			M_MOVE_PKTHDR(m, n);
    975    1.1       cgd 		}
    976    1.1       cgd 	}
    977    1.1       cgd 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    978    1.1       cgd 	do {
    979    1.1       cgd 		count = min(min(max(len, max_protohdr), space), n->m_len);
    980  1.119  christos 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
    981    1.1       cgd 		  (unsigned)count);
    982    1.1       cgd 		len -= count;
    983    1.1       cgd 		m->m_len += count;
    984    1.1       cgd 		n->m_len -= count;
    985    1.1       cgd 		space -= count;
    986    1.1       cgd 		if (n->m_len)
    987    1.1       cgd 			n->m_data += count;
    988    1.1       cgd 		else
    989    1.1       cgd 			n = m_free(n);
    990    1.1       cgd 	} while (len > 0 && n);
    991    1.1       cgd 	if (len > 0) {
    992    1.1       cgd 		(void) m_free(m);
    993    1.1       cgd 		goto bad;
    994    1.1       cgd 	}
    995    1.1       cgd 	m->m_next = n;
    996    1.1       cgd 	return (m);
    997    1.1       cgd bad:
    998    1.1       cgd 	m_freem(n);
    999    1.1       cgd 	MPFail++;
   1000   1.71    simonb 	return (NULL);
   1001   1.60   thorpej }
   1002   1.60   thorpej 
   1003   1.60   thorpej /*
   1004   1.60   thorpej  * Like m_pullup(), except a new mbuf is always allocated, and we allow
   1005   1.60   thorpej  * the amount of empty space before the data in the new mbuf to be specified
   1006   1.60   thorpej  * (in the event that the caller expects to prepend later).
   1007   1.60   thorpej  */
   1008   1.60   thorpej int MSFail;
   1009   1.60   thorpej 
   1010   1.60   thorpej struct mbuf *
   1011   1.60   thorpej m_copyup(struct mbuf *n, int len, int dstoff)
   1012   1.60   thorpej {
   1013   1.60   thorpej 	struct mbuf *m;
   1014   1.60   thorpej 	int count, space;
   1015   1.60   thorpej 
   1016   1.60   thorpej 	if (len > (MHLEN - dstoff))
   1017   1.60   thorpej 		goto bad;
   1018   1.60   thorpej 	MGET(m, M_DONTWAIT, n->m_type);
   1019   1.60   thorpej 	if (m == NULL)
   1020   1.60   thorpej 		goto bad;
   1021   1.64      matt 	MCLAIM(m, n->m_owner);
   1022   1.60   thorpej 	m->m_len = 0;
   1023   1.60   thorpej 	if (n->m_flags & M_PKTHDR) {
   1024  1.101      yamt 		M_MOVE_PKTHDR(m, n);
   1025   1.60   thorpej 	}
   1026   1.60   thorpej 	m->m_data += dstoff;
   1027   1.60   thorpej 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
   1028   1.60   thorpej 	do {
   1029   1.60   thorpej 		count = min(min(max(len, max_protohdr), space), n->m_len);
   1030  1.119  christos 		memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
   1031   1.60   thorpej 		    (unsigned)count);
   1032   1.60   thorpej 		len -= count;
   1033   1.60   thorpej 		m->m_len += count;
   1034   1.60   thorpej 		n->m_len -= count;
   1035   1.60   thorpej 		space -= count;
   1036   1.60   thorpej 		if (n->m_len)
   1037   1.60   thorpej 			n->m_data += count;
   1038   1.60   thorpej 		else
   1039   1.60   thorpej 			n = m_free(n);
   1040   1.60   thorpej 	} while (len > 0 && n);
   1041   1.60   thorpej 	if (len > 0) {
   1042   1.60   thorpej 		(void) m_free(m);
   1043   1.60   thorpej 		goto bad;
   1044   1.60   thorpej 	}
   1045   1.60   thorpej 	m->m_next = n;
   1046   1.60   thorpej 	return (m);
   1047   1.60   thorpej  bad:
   1048   1.60   thorpej 	m_freem(n);
   1049   1.60   thorpej 	MSFail++;
   1050   1.60   thorpej 	return (NULL);
   1051    1.9   mycroft }
   1052    1.9   mycroft 
   1053    1.9   mycroft /*
   1054    1.9   mycroft  * Partition an mbuf chain in two pieces, returning the tail --
   1055    1.9   mycroft  * all but the first len0 bytes.  In case of failure, it returns NULL and
   1056    1.9   mycroft  * attempts to restore the chain to its original state.
   1057    1.9   mycroft  */
   1058    1.9   mycroft struct mbuf *
   1059   1.62   thorpej m_split(struct mbuf *m0, int len0, int wait)
   1060    1.9   mycroft {
   1061   1.85      yamt 
   1062   1.87      yamt 	return m_split0(m0, len0, wait, 1);
   1063   1.85      yamt }
   1064   1.85      yamt 
   1065   1.85      yamt static struct mbuf *
   1066   1.85      yamt m_split0(struct mbuf *m0, int len0, int wait, int copyhdr)
   1067   1.85      yamt {
   1068   1.27      matt 	struct mbuf *m, *n;
   1069   1.22   thorpej 	unsigned len = len0, remain, len_save;
   1070    1.9   mycroft 
   1071    1.9   mycroft 	for (m = m0; m && len > m->m_len; m = m->m_next)
   1072    1.9   mycroft 		len -= m->m_len;
   1073    1.9   mycroft 	if (m == 0)
   1074   1.71    simonb 		return (NULL);
   1075    1.9   mycroft 	remain = m->m_len - len;
   1076   1.85      yamt 	if (copyhdr && (m0->m_flags & M_PKTHDR)) {
   1077    1.9   mycroft 		MGETHDR(n, wait, m0->m_type);
   1078    1.9   mycroft 		if (n == 0)
   1079   1.71    simonb 			return (NULL);
   1080  1.112     pavel 		MCLAIM(n, m0->m_owner);
   1081    1.9   mycroft 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
   1082    1.9   mycroft 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
   1083   1.22   thorpej 		len_save = m0->m_pkthdr.len;
   1084    1.9   mycroft 		m0->m_pkthdr.len = len0;
   1085    1.9   mycroft 		if (m->m_flags & M_EXT)
   1086    1.9   mycroft 			goto extpacket;
   1087    1.9   mycroft 		if (remain > MHLEN) {
   1088    1.9   mycroft 			/* m can't be the lead packet */
   1089    1.9   mycroft 			MH_ALIGN(n, 0);
   1090  1.132    bouyer 			n->m_len = 0;
   1091    1.9   mycroft 			n->m_next = m_split(m, len, wait);
   1092    1.9   mycroft 			if (n->m_next == 0) {
   1093    1.9   mycroft 				(void) m_free(n);
   1094   1.22   thorpej 				m0->m_pkthdr.len = len_save;
   1095   1.71    simonb 				return (NULL);
   1096    1.9   mycroft 			} else
   1097    1.9   mycroft 				return (n);
   1098    1.9   mycroft 		} else
   1099    1.9   mycroft 			MH_ALIGN(n, remain);
   1100    1.9   mycroft 	} else if (remain == 0) {
   1101    1.9   mycroft 		n = m->m_next;
   1102    1.9   mycroft 		m->m_next = 0;
   1103    1.9   mycroft 		return (n);
   1104    1.9   mycroft 	} else {
   1105    1.9   mycroft 		MGET(n, wait, m->m_type);
   1106    1.9   mycroft 		if (n == 0)
   1107   1.71    simonb 			return (NULL);
   1108   1.64      matt 		MCLAIM(n, m->m_owner);
   1109    1.9   mycroft 		M_ALIGN(n, remain);
   1110    1.9   mycroft 	}
   1111    1.9   mycroft extpacket:
   1112    1.9   mycroft 	if (m->m_flags & M_EXT) {
   1113  1.125      yamt 		n->m_data = m->m_data + len;
   1114   1.18   thorpej 		MCLADDREFERENCE(m, n);
   1115    1.9   mycroft 	} else {
   1116  1.119  christos 		memcpy(mtod(n, void *), mtod(m, char *) + len, remain);
   1117    1.9   mycroft 	}
   1118    1.9   mycroft 	n->m_len = remain;
   1119    1.9   mycroft 	m->m_len = len;
   1120    1.9   mycroft 	n->m_next = m->m_next;
   1121    1.9   mycroft 	m->m_next = 0;
   1122    1.9   mycroft 	return (n);
   1123    1.9   mycroft }
   1124    1.9   mycroft /*
   1125    1.9   mycroft  * Routine to copy from device local memory into mbufs.
   1126    1.9   mycroft  */
   1127    1.9   mycroft struct mbuf *
   1128   1.62   thorpej m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
   1129   1.62   thorpej     void (*copy)(const void *from, void *to, size_t len))
   1130    1.9   mycroft {
   1131   1.27      matt 	struct mbuf *m;
   1132    1.9   mycroft 	struct mbuf *top = 0, **mp = &top;
   1133   1.27      matt 	int off = off0, len;
   1134   1.27      matt 	char *cp;
   1135    1.9   mycroft 	char *epkt;
   1136    1.9   mycroft 
   1137    1.9   mycroft 	cp = buf;
   1138    1.9   mycroft 	epkt = cp + totlen;
   1139    1.9   mycroft 	if (off) {
   1140   1.13       cgd 		/*
   1141   1.13       cgd 		 * If 'off' is non-zero, packet is trailer-encapsulated,
   1142   1.13       cgd 		 * so we have to skip the type and length fields.
   1143   1.13       cgd 		 */
   1144  1.104     perry 		cp += off + 2 * sizeof(uint16_t);
   1145  1.104     perry 		totlen -= 2 * sizeof(uint16_t);
   1146    1.9   mycroft 	}
   1147    1.9   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1148    1.9   mycroft 	if (m == 0)
   1149   1.71    simonb 		return (NULL);
   1150    1.9   mycroft 	m->m_pkthdr.rcvif = ifp;
   1151    1.9   mycroft 	m->m_pkthdr.len = totlen;
   1152    1.9   mycroft 	m->m_len = MHLEN;
   1153    1.9   mycroft 
   1154    1.9   mycroft 	while (totlen > 0) {
   1155    1.9   mycroft 		if (top) {
   1156    1.9   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
   1157    1.9   mycroft 			if (m == 0) {
   1158    1.9   mycroft 				m_freem(top);
   1159   1.71    simonb 				return (NULL);
   1160    1.9   mycroft 			}
   1161    1.9   mycroft 			m->m_len = MLEN;
   1162    1.9   mycroft 		}
   1163    1.9   mycroft 		len = min(totlen, epkt - cp);
   1164    1.9   mycroft 		if (len >= MINCLSIZE) {
   1165    1.9   mycroft 			MCLGET(m, M_DONTWAIT);
   1166   1.19   mycroft 			if ((m->m_flags & M_EXT) == 0) {
   1167   1.20   mycroft 				m_free(m);
   1168   1.19   mycroft 				m_freem(top);
   1169   1.71    simonb 				return (NULL);
   1170   1.19   mycroft 			}
   1171   1.19   mycroft 			m->m_len = len = min(len, MCLBYTES);
   1172    1.9   mycroft 		} else {
   1173    1.9   mycroft 			/*
   1174    1.9   mycroft 			 * Place initial small packet/header at end of mbuf.
   1175    1.9   mycroft 			 */
   1176    1.9   mycroft 			if (len < m->m_len) {
   1177    1.9   mycroft 				if (top == 0 && len + max_linkhdr <= m->m_len)
   1178    1.9   mycroft 					m->m_data += max_linkhdr;
   1179    1.9   mycroft 				m->m_len = len;
   1180    1.9   mycroft 			} else
   1181    1.9   mycroft 				len = m->m_len;
   1182    1.9   mycroft 		}
   1183    1.9   mycroft 		if (copy)
   1184  1.119  christos 			copy(cp, mtod(m, void *), (size_t)len);
   1185    1.9   mycroft 		else
   1186  1.119  christos 			memcpy(mtod(m, void *), cp, (size_t)len);
   1187    1.9   mycroft 		cp += len;
   1188    1.9   mycroft 		*mp = m;
   1189    1.9   mycroft 		mp = &m->m_next;
   1190    1.9   mycroft 		totlen -= len;
   1191    1.9   mycroft 		if (cp == epkt)
   1192    1.9   mycroft 			cp = buf;
   1193    1.9   mycroft 	}
   1194    1.9   mycroft 	return (top);
   1195   1.18   thorpej }
   1196   1.18   thorpej 
   1197   1.18   thorpej /*
   1198   1.18   thorpej  * Copy data from a buffer back into the indicated mbuf chain,
   1199   1.18   thorpej  * starting "off" bytes from the beginning, extending the mbuf
   1200   1.18   thorpej  * chain if necessary.
   1201   1.18   thorpej  */
   1202   1.18   thorpej void
   1203   1.86      yamt m_copyback(struct mbuf *m0, int off, int len, const void *cp)
   1204   1.18   thorpej {
   1205   1.85      yamt #if defined(DEBUG)
   1206   1.85      yamt 	struct mbuf *origm = m0;
   1207   1.85      yamt 	int error;
   1208   1.85      yamt #endif /* defined(DEBUG) */
   1209   1.85      yamt 
   1210   1.85      yamt 	if (m0 == NULL)
   1211   1.85      yamt 		return;
   1212   1.85      yamt 
   1213   1.85      yamt #if defined(DEBUG)
   1214   1.85      yamt 	error =
   1215   1.85      yamt #endif /* defined(DEBUG) */
   1216   1.85      yamt 	m_copyback0(&m0, off, len, cp,
   1217   1.85      yamt 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_EXTEND, M_DONTWAIT);
   1218   1.85      yamt 
   1219   1.85      yamt #if defined(DEBUG)
   1220   1.85      yamt 	if (error != 0 || (m0 != NULL && origm != m0))
   1221   1.85      yamt 		panic("m_copyback");
   1222   1.85      yamt #endif /* defined(DEBUG) */
   1223   1.85      yamt }
   1224   1.85      yamt 
   1225   1.85      yamt struct mbuf *
   1226   1.86      yamt m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
   1227   1.85      yamt {
   1228   1.85      yamt 	int error;
   1229   1.85      yamt 
   1230   1.85      yamt 	/* don't support chain expansion */
   1231   1.85      yamt 	KDASSERT(off + len <= m_length(m0));
   1232   1.85      yamt 
   1233   1.85      yamt 	error = m_copyback0(&m0, off, len, cp,
   1234   1.85      yamt 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_COW, how);
   1235   1.85      yamt 	if (error) {
   1236   1.85      yamt 		/*
   1237   1.85      yamt 		 * no way to recover from partial success.
   1238   1.85      yamt 		 * just free the chain.
   1239   1.85      yamt 		 */
   1240   1.85      yamt 		m_freem(m0);
   1241   1.85      yamt 		return NULL;
   1242   1.85      yamt 	}
   1243   1.85      yamt 	return m0;
   1244   1.85      yamt }
   1245   1.85      yamt 
   1246   1.85      yamt /*
   1247   1.85      yamt  * m_makewritable: ensure the specified range writable.
   1248   1.85      yamt  */
   1249   1.85      yamt int
   1250   1.85      yamt m_makewritable(struct mbuf **mp, int off, int len, int how)
   1251   1.85      yamt {
   1252   1.85      yamt 	int error;
   1253   1.85      yamt #if defined(DEBUG)
   1254   1.85      yamt 	struct mbuf *n;
   1255   1.85      yamt 	int origlen, reslen;
   1256   1.85      yamt 
   1257   1.85      yamt 	origlen = m_length(*mp);
   1258   1.85      yamt #endif /* defined(DEBUG) */
   1259   1.85      yamt 
   1260   1.85      yamt #if 0 /* M_COPYALL is large enough */
   1261   1.85      yamt 	if (len == M_COPYALL)
   1262   1.85      yamt 		len = m_length(*mp) - off; /* XXX */
   1263   1.85      yamt #endif
   1264   1.85      yamt 
   1265   1.85      yamt 	error = m_copyback0(mp, off, len, NULL,
   1266   1.85      yamt 	    M_COPYBACK0_PRESERVE|M_COPYBACK0_COW, how);
   1267   1.85      yamt 
   1268   1.85      yamt #if defined(DEBUG)
   1269   1.85      yamt 	reslen = 0;
   1270   1.85      yamt 	for (n = *mp; n; n = n->m_next)
   1271   1.85      yamt 		reslen += n->m_len;
   1272   1.85      yamt 	if (origlen != reslen)
   1273   1.85      yamt 		panic("m_makewritable: length changed");
   1274   1.85      yamt 	if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
   1275   1.85      yamt 		panic("m_makewritable: inconsist");
   1276   1.85      yamt #endif /* defined(DEBUG) */
   1277   1.85      yamt 
   1278   1.85      yamt 	return error;
   1279   1.85      yamt }
   1280   1.85      yamt 
   1281   1.85      yamt int
   1282   1.86      yamt m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags,
   1283   1.86      yamt     int how)
   1284   1.85      yamt {
   1285   1.27      matt 	int mlen;
   1286   1.85      yamt 	struct mbuf *m, *n;
   1287   1.85      yamt 	struct mbuf **mp;
   1288   1.18   thorpej 	int totlen = 0;
   1289   1.86      yamt 	const char *cp = vp;
   1290   1.18   thorpej 
   1291   1.85      yamt 	KASSERT(mp0 != NULL);
   1292   1.85      yamt 	KASSERT(*mp0 != NULL);
   1293   1.85      yamt 	KASSERT((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL);
   1294   1.85      yamt 	KASSERT((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL);
   1295   1.85      yamt 
   1296  1.106      yamt 	/*
   1297  1.106      yamt 	 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
   1298  1.106      yamt 	 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
   1299  1.106      yamt 	 */
   1300  1.106      yamt 
   1301  1.106      yamt 	KASSERT((~flags & (M_COPYBACK0_EXTEND|M_COPYBACK0_COW)) != 0);
   1302  1.106      yamt 
   1303   1.85      yamt 	mp = mp0;
   1304   1.85      yamt 	m = *mp;
   1305   1.18   thorpej 	while (off > (mlen = m->m_len)) {
   1306   1.18   thorpej 		off -= mlen;
   1307   1.18   thorpej 		totlen += mlen;
   1308  1.109      yamt 		if (m->m_next == NULL) {
   1309  1.109      yamt 			int tspace;
   1310  1.109      yamt extend:
   1311   1.85      yamt 			if ((flags & M_COPYBACK0_EXTEND) == 0)
   1312   1.85      yamt 				goto out;
   1313  1.109      yamt 
   1314  1.109      yamt 			/*
   1315  1.109      yamt 			 * try to make some space at the end of "m".
   1316  1.109      yamt 			 */
   1317  1.109      yamt 
   1318  1.109      yamt 			mlen = m->m_len;
   1319  1.109      yamt 			if (off + len >= MINCLSIZE &&
   1320  1.109      yamt 			    (m->m_flags & M_EXT) == 0 && m->m_len == 0) {
   1321  1.109      yamt 				MCLGET(m, how);
   1322  1.109      yamt 			}
   1323  1.109      yamt 			tspace = M_TRAILINGSPACE(m);
   1324  1.109      yamt 			if (tspace > 0) {
   1325  1.109      yamt 				tspace = min(tspace, off + len);
   1326  1.109      yamt 				KASSERT(tspace > 0);
   1327  1.109      yamt 				memset(mtod(m, char *) + m->m_len, 0,
   1328  1.109      yamt 				    min(off, tspace));
   1329  1.109      yamt 				m->m_len += tspace;
   1330  1.109      yamt 				off += mlen;
   1331  1.109      yamt 				totlen -= mlen;
   1332  1.109      yamt 				continue;
   1333  1.109      yamt 			}
   1334  1.109      yamt 
   1335  1.109      yamt 			/*
   1336  1.109      yamt 			 * need to allocate an mbuf.
   1337  1.109      yamt 			 */
   1338  1.109      yamt 
   1339  1.109      yamt 			if (off + len >= MINCLSIZE) {
   1340  1.109      yamt 				n = m_getcl(how, m->m_type, 0);
   1341  1.109      yamt 			} else {
   1342  1.109      yamt 				n = m_get(how, m->m_type);
   1343  1.109      yamt 			}
   1344  1.109      yamt 			if (n == NULL) {
   1345   1.18   thorpej 				goto out;
   1346  1.109      yamt 			}
   1347  1.109      yamt 			n->m_len = 0;
   1348  1.109      yamt 			n->m_len = min(M_TRAILINGSPACE(n), off + len);
   1349  1.109      yamt 			memset(mtod(n, char *), 0, min(n->m_len, off));
   1350   1.18   thorpej 			m->m_next = n;
   1351   1.18   thorpej 		}
   1352   1.85      yamt 		mp = &m->m_next;
   1353   1.18   thorpej 		m = m->m_next;
   1354   1.18   thorpej 	}
   1355   1.18   thorpej 	while (len > 0) {
   1356   1.85      yamt 		mlen = m->m_len - off;
   1357   1.85      yamt 		if (mlen != 0 && M_READONLY(m)) {
   1358   1.85      yamt 			char *datap;
   1359   1.85      yamt 			int eatlen;
   1360   1.85      yamt 
   1361   1.85      yamt 			/*
   1362   1.85      yamt 			 * this mbuf is read-only.
   1363   1.85      yamt 			 * allocate a new writable mbuf and try again.
   1364   1.85      yamt 			 */
   1365   1.85      yamt 
   1366   1.85      yamt #if defined(DIAGNOSTIC)
   1367   1.85      yamt 			if ((flags & M_COPYBACK0_COW) == 0)
   1368   1.85      yamt 				panic("m_copyback0: read-only");
   1369   1.85      yamt #endif /* defined(DIAGNOSTIC) */
   1370   1.85      yamt 
   1371   1.85      yamt 			/*
   1372   1.85      yamt 			 * if we're going to write into the middle of
   1373   1.85      yamt 			 * a mbuf, split it first.
   1374   1.85      yamt 			 */
   1375   1.85      yamt 			if (off > 0 && len < mlen) {
   1376   1.85      yamt 				n = m_split0(m, off, how, 0);
   1377   1.85      yamt 				if (n == NULL)
   1378   1.85      yamt 					goto enobufs;
   1379   1.85      yamt 				m->m_next = n;
   1380   1.85      yamt 				mp = &m->m_next;
   1381   1.85      yamt 				m = n;
   1382   1.85      yamt 				off = 0;
   1383   1.85      yamt 				continue;
   1384   1.85      yamt 			}
   1385   1.85      yamt 
   1386   1.85      yamt 			/*
   1387   1.85      yamt 			 * XXX TODO coalesce into the trailingspace of
   1388   1.85      yamt 			 * the previous mbuf when possible.
   1389   1.85      yamt 			 */
   1390   1.85      yamt 
   1391   1.85      yamt 			/*
   1392   1.85      yamt 			 * allocate a new mbuf.  copy packet header if needed.
   1393   1.85      yamt 			 */
   1394   1.85      yamt 			MGET(n, how, m->m_type);
   1395   1.85      yamt 			if (n == NULL)
   1396   1.85      yamt 				goto enobufs;
   1397   1.85      yamt 			MCLAIM(n, m->m_owner);
   1398   1.85      yamt 			if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
   1399  1.101      yamt 				M_MOVE_PKTHDR(n, m);
   1400   1.85      yamt 				n->m_len = MHLEN;
   1401   1.85      yamt 			} else {
   1402   1.85      yamt 				if (len >= MINCLSIZE)
   1403   1.85      yamt 					MCLGET(n, M_DONTWAIT);
   1404   1.85      yamt 				n->m_len =
   1405   1.85      yamt 				    (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
   1406   1.85      yamt 			}
   1407   1.85      yamt 			if (n->m_len > len)
   1408   1.85      yamt 				n->m_len = len;
   1409   1.85      yamt 
   1410   1.85      yamt 			/*
   1411   1.85      yamt 			 * free the region which has been overwritten.
   1412   1.85      yamt 			 * copying data from old mbufs if requested.
   1413   1.85      yamt 			 */
   1414   1.85      yamt 			if (flags & M_COPYBACK0_PRESERVE)
   1415   1.85      yamt 				datap = mtod(n, char *);
   1416   1.85      yamt 			else
   1417   1.85      yamt 				datap = NULL;
   1418   1.85      yamt 			eatlen = n->m_len;
   1419   1.85      yamt 			KDASSERT(off == 0 || eatlen >= mlen);
   1420   1.85      yamt 			if (off > 0) {
   1421   1.85      yamt 				KDASSERT(len >= mlen);
   1422   1.85      yamt 				m->m_len = off;
   1423   1.85      yamt 				m->m_next = n;
   1424   1.85      yamt 				if (datap) {
   1425   1.85      yamt 					m_copydata(m, off, mlen, datap);
   1426   1.85      yamt 					datap += mlen;
   1427   1.85      yamt 				}
   1428   1.85      yamt 				eatlen -= mlen;
   1429   1.85      yamt 				mp = &m->m_next;
   1430   1.85      yamt 				m = m->m_next;
   1431   1.85      yamt 			}
   1432   1.85      yamt 			while (m != NULL && M_READONLY(m) &&
   1433   1.85      yamt 			    n->m_type == m->m_type && eatlen > 0) {
   1434   1.85      yamt 				mlen = min(eatlen, m->m_len);
   1435   1.85      yamt 				if (datap) {
   1436   1.85      yamt 					m_copydata(m, 0, mlen, datap);
   1437   1.85      yamt 					datap += mlen;
   1438   1.85      yamt 				}
   1439   1.85      yamt 				m->m_data += mlen;
   1440   1.85      yamt 				m->m_len -= mlen;
   1441   1.85      yamt 				eatlen -= mlen;
   1442   1.85      yamt 				if (m->m_len == 0)
   1443   1.85      yamt 					*mp = m = m_free(m);
   1444   1.85      yamt 			}
   1445   1.85      yamt 			if (eatlen > 0)
   1446   1.85      yamt 				n->m_len -= eatlen;
   1447   1.85      yamt 			n->m_next = m;
   1448   1.85      yamt 			*mp = m = n;
   1449   1.85      yamt 			continue;
   1450   1.85      yamt 		}
   1451   1.85      yamt 		mlen = min(mlen, len);
   1452   1.85      yamt 		if (flags & M_COPYBACK0_COPYBACK) {
   1453  1.119  christos 			memcpy(mtod(m, char *) + off, cp, (unsigned)mlen);
   1454   1.85      yamt 			cp += mlen;
   1455   1.85      yamt 		}
   1456   1.18   thorpej 		len -= mlen;
   1457   1.18   thorpej 		mlen += off;
   1458   1.18   thorpej 		off = 0;
   1459   1.18   thorpej 		totlen += mlen;
   1460   1.18   thorpej 		if (len == 0)
   1461   1.18   thorpej 			break;
   1462  1.109      yamt 		if (m->m_next == NULL) {
   1463  1.109      yamt 			goto extend;
   1464   1.18   thorpej 		}
   1465   1.85      yamt 		mp = &m->m_next;
   1466   1.18   thorpej 		m = m->m_next;
   1467   1.18   thorpej 	}
   1468  1.106      yamt out:	if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) {
   1469  1.106      yamt 		KASSERT((flags & M_COPYBACK0_EXTEND) != 0);
   1470   1.18   thorpej 		m->m_pkthdr.len = totlen;
   1471  1.106      yamt 	}
   1472   1.85      yamt 
   1473   1.85      yamt 	return 0;
   1474   1.85      yamt 
   1475   1.85      yamt enobufs:
   1476   1.85      yamt 	return ENOBUFS;
   1477   1.66   thorpej }
   1478   1.66   thorpej 
   1479  1.101      yamt void
   1480  1.101      yamt m_move_pkthdr(struct mbuf *to, struct mbuf *from)
   1481  1.101      yamt {
   1482  1.101      yamt 
   1483  1.101      yamt 	KASSERT((to->m_flags & M_EXT) == 0);
   1484  1.101      yamt 	KASSERT((to->m_flags & M_PKTHDR) == 0 || m_tag_first(to) == NULL);
   1485  1.101      yamt 	KASSERT((from->m_flags & M_PKTHDR) != 0);
   1486  1.101      yamt 
   1487  1.101      yamt 	to->m_pkthdr = from->m_pkthdr;
   1488  1.101      yamt 	to->m_flags = from->m_flags & M_COPYFLAGS;
   1489  1.101      yamt 	to->m_data = to->m_pktdat;
   1490  1.101      yamt 
   1491  1.101      yamt 	from->m_flags &= ~M_PKTHDR;
   1492  1.101      yamt }
   1493  1.101      yamt 
   1494   1.66   thorpej /*
   1495   1.66   thorpej  * Apply function f to the data in an mbuf chain starting "off" bytes from the
   1496   1.66   thorpej  * beginning, continuing for "len" bytes.
   1497   1.66   thorpej  */
   1498   1.66   thorpej int
   1499   1.66   thorpej m_apply(struct mbuf *m, int off, int len,
   1500  1.119  christos     int (*f)(void *, void *, unsigned int), void *arg)
   1501   1.66   thorpej {
   1502   1.66   thorpej 	unsigned int count;
   1503   1.66   thorpej 	int rval;
   1504   1.66   thorpej 
   1505   1.66   thorpej 	KASSERT(len >= 0);
   1506   1.66   thorpej 	KASSERT(off >= 0);
   1507   1.66   thorpej 
   1508   1.66   thorpej 	while (off > 0) {
   1509   1.66   thorpej 		KASSERT(m != NULL);
   1510   1.66   thorpej 		if (off < m->m_len)
   1511   1.66   thorpej 			break;
   1512   1.66   thorpej 		off -= m->m_len;
   1513   1.66   thorpej 		m = m->m_next;
   1514   1.66   thorpej 	}
   1515   1.66   thorpej 	while (len > 0) {
   1516   1.66   thorpej 		KASSERT(m != NULL);
   1517   1.66   thorpej 		count = min(m->m_len - off, len);
   1518   1.66   thorpej 
   1519  1.119  christos 		rval = (*f)(arg, mtod(m, char *) + off, count);
   1520   1.66   thorpej 		if (rval)
   1521   1.66   thorpej 			return (rval);
   1522   1.66   thorpej 
   1523   1.66   thorpej 		len -= count;
   1524   1.66   thorpej 		off = 0;
   1525   1.66   thorpej 		m = m->m_next;
   1526   1.66   thorpej 	}
   1527   1.66   thorpej 
   1528   1.66   thorpej 	return (0);
   1529   1.66   thorpej }
   1530   1.66   thorpej 
   1531   1.66   thorpej /*
   1532   1.66   thorpej  * Return a pointer to mbuf/offset of location in mbuf chain.
   1533   1.66   thorpej  */
   1534   1.66   thorpej struct mbuf *
   1535   1.66   thorpej m_getptr(struct mbuf *m, int loc, int *off)
   1536   1.66   thorpej {
   1537   1.66   thorpej 
   1538   1.66   thorpej 	while (loc >= 0) {
   1539   1.66   thorpej 		/* Normal end of search */
   1540   1.66   thorpej 		if (m->m_len > loc) {
   1541   1.66   thorpej 	    		*off = loc;
   1542   1.66   thorpej 	    		return (m);
   1543   1.66   thorpej 		} else {
   1544   1.66   thorpej 	    		loc -= m->m_len;
   1545   1.66   thorpej 
   1546   1.66   thorpej 	    		if (m->m_next == NULL) {
   1547   1.66   thorpej 				if (loc == 0) {
   1548   1.66   thorpej  					/* Point at the end of valid data */
   1549   1.66   thorpej 		    			*off = m->m_len;
   1550   1.66   thorpej 		    			return (m);
   1551   1.66   thorpej 				} else
   1552   1.66   thorpej 		  			return (NULL);
   1553   1.66   thorpej 	    		} else
   1554   1.66   thorpej 	      			m = m->m_next;
   1555   1.66   thorpej 		}
   1556   1.66   thorpej     	}
   1557   1.66   thorpej 
   1558   1.66   thorpej 	return (NULL);
   1559    1.1       cgd }
   1560  1.105      yamt 
   1561  1.125      yamt /*
   1562  1.125      yamt  * m_ext_free: release a reference to the mbuf external storage.
   1563  1.125      yamt  *
   1564  1.125      yamt  * => free the mbuf m itsself as well.
   1565  1.125      yamt  */
   1566  1.125      yamt 
   1567  1.125      yamt void
   1568  1.125      yamt m_ext_free(struct mbuf *m)
   1569  1.125      yamt {
   1570  1.125      yamt 	bool embedded = MEXT_ISEMBEDDED(m);
   1571  1.125      yamt 	bool dofree = true;
   1572  1.125      yamt 	u_int refcnt;
   1573  1.125      yamt 
   1574  1.125      yamt 	KASSERT((m->m_flags & M_EXT) != 0);
   1575  1.125      yamt 	KASSERT(MEXT_ISEMBEDDED(m->m_ext_ref));
   1576  1.125      yamt 	KASSERT((m->m_ext_ref->m_flags & M_EXT) != 0);
   1577  1.125      yamt 	KASSERT((m->m_flags & M_EXT_CLUSTER) ==
   1578  1.125      yamt 	    (m->m_ext_ref->m_flags & M_EXT_CLUSTER));
   1579  1.125      yamt 
   1580  1.125      yamt 	if (__predict_true(m->m_ext.ext_refcnt == 1)) {
   1581  1.125      yamt 		refcnt = m->m_ext.ext_refcnt = 0;
   1582  1.125      yamt 	} else {
   1583  1.125      yamt 		refcnt = atomic_dec_uint_nv(&m->m_ext.ext_refcnt);
   1584  1.125      yamt 	}
   1585  1.125      yamt 	if (refcnt > 0) {
   1586  1.125      yamt 		if (embedded) {
   1587  1.125      yamt 			/*
   1588  1.125      yamt 			 * other mbuf's m_ext_ref still points to us.
   1589  1.125      yamt 			 */
   1590  1.125      yamt 			dofree = false;
   1591  1.125      yamt 		} else {
   1592  1.125      yamt 			m->m_ext_ref = m;
   1593  1.125      yamt 		}
   1594  1.125      yamt 	} else {
   1595  1.125      yamt 		/*
   1596  1.125      yamt 		 * dropping the last reference
   1597  1.125      yamt 		 */
   1598  1.125      yamt 		if (!embedded) {
   1599  1.125      yamt 			m->m_ext.ext_refcnt++; /* XXX */
   1600  1.125      yamt 			m_ext_free(m->m_ext_ref);
   1601  1.125      yamt 			m->m_ext_ref = m;
   1602  1.125      yamt 		} else if ((m->m_flags & M_EXT_CLUSTER) != 0) {
   1603  1.125      yamt 			pool_cache_put_paddr((struct pool_cache *)
   1604  1.125      yamt 			    m->m_ext.ext_arg,
   1605  1.125      yamt 			    m->m_ext.ext_buf, m->m_ext.ext_paddr);
   1606  1.125      yamt 		} else if (m->m_ext.ext_free) {
   1607  1.125      yamt 			(*m->m_ext.ext_free)(m,
   1608  1.125      yamt 			    m->m_ext.ext_buf, m->m_ext.ext_size,
   1609  1.125      yamt 			    m->m_ext.ext_arg);
   1610  1.125      yamt 			/*
   1611  1.125      yamt 			 * 'm' is already freed by the ext_free callback.
   1612  1.125      yamt 			 */
   1613  1.125      yamt 			dofree = false;
   1614  1.125      yamt 		} else {
   1615  1.125      yamt 			free(m->m_ext.ext_buf, m->m_ext.ext_type);
   1616  1.125      yamt 		}
   1617  1.125      yamt 	}
   1618  1.125      yamt 	if (dofree) {
   1619  1.125      yamt 		pool_cache_put(mb_cache, m);
   1620  1.125      yamt 	}
   1621  1.125      yamt }
   1622  1.125      yamt 
   1623  1.105      yamt #if defined(DDB)
   1624  1.105      yamt void
   1625  1.105      yamt m_print(const struct mbuf *m, const char *modif, void (*pr)(const char *, ...))
   1626  1.105      yamt {
   1627  1.105      yamt 	char ch;
   1628  1.118   thorpej 	bool opt_c = false;
   1629  1.105      yamt 	char buf[512];
   1630  1.105      yamt 
   1631  1.105      yamt 	while ((ch = *(modif++)) != '\0') {
   1632  1.105      yamt 		switch (ch) {
   1633  1.105      yamt 		case 'c':
   1634  1.118   thorpej 			opt_c = true;
   1635  1.105      yamt 			break;
   1636  1.105      yamt 		}
   1637  1.105      yamt 	}
   1638  1.105      yamt 
   1639  1.105      yamt nextchain:
   1640  1.105      yamt 	(*pr)("MBUF %p\n", m);
   1641  1.130  christos 	snprintb(buf, sizeof(buf), M_FLAGS_BITS, (u_int)m->m_flags);
   1642  1.105      yamt 	(*pr)("  data=%p, len=%d, type=%d, flags=0x%s\n",
   1643  1.105      yamt 	    m->m_data, m->m_len, m->m_type, buf);
   1644  1.105      yamt 	(*pr)("  owner=%p, next=%p, nextpkt=%p\n", m->m_owner, m->m_next,
   1645  1.105      yamt 	    m->m_nextpkt);
   1646  1.105      yamt 	(*pr)("  leadingspace=%u, trailingspace=%u, readonly=%u\n",
   1647  1.105      yamt 	    (int)M_LEADINGSPACE(m), (int)M_TRAILINGSPACE(m),
   1648  1.105      yamt 	    (int)M_READONLY(m));
   1649  1.105      yamt 	if ((m->m_flags & M_PKTHDR) != 0) {
   1650  1.130  christos 		snprintb(buf, sizeof(buf), M_CSUM_BITS, m->m_pkthdr.csum_flags);
   1651  1.105      yamt 		(*pr)("  pktlen=%d, rcvif=%p, csum_flags=0x%s, csum_data=0x%"
   1652  1.105      yamt 		    PRIx32 ", segsz=%u\n",
   1653  1.105      yamt 		    m->m_pkthdr.len, m->m_pkthdr.rcvif,
   1654  1.105      yamt 		    buf, m->m_pkthdr.csum_data, m->m_pkthdr.segsz);
   1655  1.105      yamt 	}
   1656  1.105      yamt 	if ((m->m_flags & M_EXT)) {
   1657  1.125      yamt 		(*pr)("  ext_refcnt=%u, ext_buf=%p, ext_size=%zd, "
   1658  1.105      yamt 		    "ext_free=%p, ext_arg=%p\n",
   1659  1.125      yamt 		    m->m_ext.ext_refcnt,
   1660  1.105      yamt 		    m->m_ext.ext_buf, m->m_ext.ext_size,
   1661  1.105      yamt 		    m->m_ext.ext_free, m->m_ext.ext_arg);
   1662  1.105      yamt 	}
   1663  1.105      yamt 	if ((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0) {
   1664  1.108      yamt 		vaddr_t sva = (vaddr_t)m->m_ext.ext_buf;
   1665  1.108      yamt 		vaddr_t eva = sva + m->m_ext.ext_size;
   1666  1.108      yamt 		int n = (round_page(eva) - trunc_page(sva)) >> PAGE_SHIFT;
   1667  1.108      yamt 		int i;
   1668  1.105      yamt 
   1669  1.105      yamt 		(*pr)("  pages:");
   1670  1.108      yamt 		for (i = 0; i < n; i ++) {
   1671  1.108      yamt 			(*pr)(" %p", m->m_ext.ext_pgs[i]);
   1672  1.105      yamt 		}
   1673  1.105      yamt 		(*pr)("\n");
   1674  1.105      yamt 	}
   1675  1.105      yamt 
   1676  1.105      yamt 	if (opt_c) {
   1677  1.105      yamt 		m = m->m_next;
   1678  1.105      yamt 		if (m != NULL) {
   1679  1.105      yamt 			goto nextchain;
   1680  1.105      yamt 		}
   1681  1.105      yamt 	}
   1682  1.105      yamt }
   1683  1.105      yamt #endif /* defined(DDB) */
   1684  1.124      yamt 
   1685  1.124      yamt void
   1686  1.124      yamt mbstat_type_add(int type, int diff)
   1687  1.124      yamt {
   1688  1.124      yamt 	struct mbstat_cpu *mb;
   1689  1.124      yamt 	int s;
   1690  1.124      yamt 
   1691  1.124      yamt 	s = splvm();
   1692  1.126   thorpej 	mb = percpu_getref(mbstat_percpu);
   1693  1.124      yamt 	mb->m_mtypes[type] += diff;
   1694  1.126   thorpej 	percpu_putref(mbstat_percpu);
   1695  1.124      yamt 	splx(s);
   1696  1.124      yamt }
   1697  1.124      yamt 
   1698  1.124      yamt #if defined(MBUFTRACE)
   1699  1.124      yamt void
   1700  1.124      yamt mowner_attach(struct mowner *mo)
   1701  1.124      yamt {
   1702  1.124      yamt 
   1703  1.124      yamt 	KASSERT(mo->mo_counters == NULL);
   1704  1.124      yamt 	mo->mo_counters = percpu_alloc(sizeof(struct mowner_counter));
   1705  1.124      yamt 
   1706  1.124      yamt 	/* XXX lock */
   1707  1.124      yamt 	LIST_INSERT_HEAD(&mowners, mo, mo_link);
   1708  1.124      yamt }
   1709  1.124      yamt 
   1710  1.124      yamt void
   1711  1.124      yamt mowner_detach(struct mowner *mo)
   1712  1.124      yamt {
   1713  1.124      yamt 
   1714  1.124      yamt 	KASSERT(mo->mo_counters != NULL);
   1715  1.124      yamt 
   1716  1.124      yamt 	/* XXX lock */
   1717  1.124      yamt 	LIST_REMOVE(mo, mo_link);
   1718  1.124      yamt 
   1719  1.124      yamt 	percpu_free(mo->mo_counters, sizeof(struct mowner_counter));
   1720  1.124      yamt 	mo->mo_counters = NULL;
   1721  1.124      yamt }
   1722  1.124      yamt 
   1723  1.124      yamt void
   1724  1.124      yamt mowner_init(struct mbuf *m, int type)
   1725  1.124      yamt {
   1726  1.124      yamt 	struct mowner_counter *mc;
   1727  1.124      yamt 	struct mowner *mo;
   1728  1.124      yamt 	int s;
   1729  1.124      yamt 
   1730  1.124      yamt 	m->m_owner = mo = &unknown_mowners[type];
   1731  1.124      yamt 	s = splvm();
   1732  1.126   thorpej 	mc = percpu_getref(mo->mo_counters);
   1733  1.124      yamt 	mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
   1734  1.126   thorpej 	percpu_putref(mo->mo_counters);
   1735  1.124      yamt 	splx(s);
   1736  1.124      yamt }
   1737  1.124      yamt 
   1738  1.124      yamt void
   1739  1.124      yamt mowner_ref(struct mbuf *m, int flags)
   1740  1.124      yamt {
   1741  1.124      yamt 	struct mowner *mo = m->m_owner;
   1742  1.124      yamt 	struct mowner_counter *mc;
   1743  1.124      yamt 	int s;
   1744  1.124      yamt 
   1745  1.124      yamt 	s = splvm();
   1746  1.126   thorpej 	mc = percpu_getref(mo->mo_counters);
   1747  1.124      yamt 	if ((flags & M_EXT) != 0)
   1748  1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
   1749  1.124      yamt 	if ((flags & M_CLUSTER) != 0)
   1750  1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
   1751  1.126   thorpej 	percpu_putref(mo->mo_counters);
   1752  1.124      yamt 	splx(s);
   1753  1.124      yamt }
   1754  1.124      yamt 
   1755  1.124      yamt void
   1756  1.124      yamt mowner_revoke(struct mbuf *m, bool all, int flags)
   1757  1.124      yamt {
   1758  1.124      yamt 	struct mowner *mo = m->m_owner;
   1759  1.124      yamt 	struct mowner_counter *mc;
   1760  1.124      yamt 	int s;
   1761  1.124      yamt 
   1762  1.124      yamt 	s = splvm();
   1763  1.126   thorpej 	mc = percpu_getref(mo->mo_counters);
   1764  1.124      yamt 	if ((flags & M_EXT) != 0)
   1765  1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_EXT_RELEASES]++;
   1766  1.124      yamt 	if ((flags & M_CLUSTER) != 0)
   1767  1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_CLUSTER_RELEASES]++;
   1768  1.124      yamt 	if (all)
   1769  1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_RELEASES]++;
   1770  1.126   thorpej 	percpu_putref(mo->mo_counters);
   1771  1.124      yamt 	splx(s);
   1772  1.124      yamt 	if (all)
   1773  1.124      yamt 		m->m_owner = &revoked_mowner;
   1774  1.124      yamt }
   1775  1.124      yamt 
   1776  1.124      yamt static void
   1777  1.124      yamt mowner_claim(struct mbuf *m, struct mowner *mo)
   1778  1.124      yamt {
   1779  1.124      yamt 	struct mowner_counter *mc;
   1780  1.124      yamt 	int flags = m->m_flags;
   1781  1.124      yamt 	int s;
   1782  1.124      yamt 
   1783  1.124      yamt 	s = splvm();
   1784  1.126   thorpej 	mc = percpu_getref(mo->mo_counters);
   1785  1.124      yamt 	mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
   1786  1.124      yamt 	if ((flags & M_EXT) != 0)
   1787  1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
   1788  1.124      yamt 	if ((flags & M_CLUSTER) != 0)
   1789  1.124      yamt 		mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
   1790  1.126   thorpej 	percpu_putref(mo->mo_counters);
   1791  1.124      yamt 	splx(s);
   1792  1.124      yamt 	m->m_owner = mo;
   1793  1.124      yamt }
   1794  1.124      yamt 
   1795  1.124      yamt void
   1796  1.124      yamt m_claim(struct mbuf *m, struct mowner *mo)
   1797  1.124      yamt {
   1798  1.124      yamt 
   1799  1.124      yamt 	if (m->m_owner == mo || mo == NULL)
   1800  1.124      yamt 		return;
   1801  1.124      yamt 
   1802  1.124      yamt 	mowner_revoke(m, true, m->m_flags);
   1803  1.124      yamt 	mowner_claim(m, mo);
   1804  1.124      yamt }
   1805  1.124      yamt #endif /* defined(MBUFTRACE) */
   1806