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