Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.30
      1  1.30     perry /*	$NetBSD: uipc_mbuf.c,v 1.30 1998/08/04 04:03:17 perry Exp $	*/
      2  1.10       cgd 
      3   1.1       cgd /*
      4   1.9   mycroft  * Copyright (c) 1982, 1986, 1988, 1991, 1993
      5   1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35  1.26      fvdl  *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
     36   1.1       cgd  */
     37  1.24       mrg 
     38  1.24       mrg #include "opt_uvm.h"
     39   1.1       cgd 
     40   1.6   mycroft #include <sys/param.h>
     41   1.6   mycroft #include <sys/systm.h>
     42   1.6   mycroft #include <sys/proc.h>
     43   1.6   mycroft #include <sys/malloc.h>
     44   1.9   mycroft #include <sys/map.h>
     45   1.1       cgd #define MBTYPES
     46   1.6   mycroft #include <sys/mbuf.h>
     47   1.6   mycroft #include <sys/kernel.h>
     48   1.6   mycroft #include <sys/syslog.h>
     49   1.6   mycroft #include <sys/domain.h>
     50   1.6   mycroft #include <sys/protosw.h>
     51  1.28   thorpej #include <sys/pool.h>
     52  1.27      matt #include <sys/socket.h>
     53  1.27      matt #include <net/if.h>
     54   1.6   mycroft 
     55   1.6   mycroft #include <vm/vm.h>
     56  1.14  christos 
     57  1.23       mrg #if defined(UVM)
     58  1.23       mrg #include <uvm/uvm_extern.h>
     59  1.23       mrg #endif
     60  1.23       mrg 
     61  1.28   thorpej struct	pool mbpool;		/* mbuf pool */
     62  1.28   thorpej struct	pool mclpool;		/* mbuf cluster pool */
     63  1.28   thorpej 
     64  1.18   thorpej struct mbuf *mbutl;
     65  1.18   thorpej struct mbstat mbstat;
     66  1.18   thorpej union mcluster *mclfree;
     67  1.18   thorpej int	max_linkhdr;
     68  1.18   thorpej int	max_protohdr;
     69  1.18   thorpej int	max_hdr;
     70  1.18   thorpej int	max_datalen;
     71  1.18   thorpej 
     72   1.1       cgd extern	vm_map_t mb_map;
     73   1.1       cgd 
     74  1.28   thorpej void	*mclpool_alloc __P((unsigned long, int, int));
     75  1.28   thorpej void	mclpool_release __P((void *, unsigned long, int));
     76  1.28   thorpej 
     77  1.28   thorpej /*
     78  1.28   thorpej  * Initialize the mbuf allcator.  Note, this cannot allocate any
     79  1.28   thorpej  * memory itself; we are called before mb_map has been allocated.
     80  1.28   thorpej  */
     81   1.4       jtc void
     82   1.1       cgd mbinit()
     83   1.1       cgd {
     84   1.1       cgd 
     85  1.28   thorpej 	/* XXX malloc types! */
     86  1.28   thorpej 	pool_init(&mbpool, MSIZE, 0, 0, 0, "mbpl", 0, NULL, NULL, 0);
     87  1.28   thorpej 	pool_init(&mclpool, MCLBYTES, 0, 0, 0, "mclpl", 0, mclpool_alloc,
     88  1.28   thorpej 	    mclpool_release, 0);
     89  1.28   thorpej }
     90  1.28   thorpej 
     91  1.28   thorpej void *
     92  1.28   thorpej mclpool_alloc(sz, flags, mtype)
     93  1.28   thorpej 	unsigned long sz;
     94  1.28   thorpej 	int flags;
     95  1.28   thorpej 	int mtype;
     96  1.28   thorpej {
     97  1.28   thorpej 
     98  1.28   thorpej #if defined(UVM)
     99  1.28   thorpej 	return ((void *)uvm_km_alloc_poolpage1(mb_map, uvmexp.mb_object));
    100  1.28   thorpej #else
    101  1.28   thorpej 	return ((void *)kmem_alloc_poolpage1(mb_map));
    102  1.28   thorpej #endif
    103   1.1       cgd }
    104   1.1       cgd 
    105  1.28   thorpej void
    106  1.28   thorpej mclpool_release(v, sz, mtype)
    107  1.28   thorpej 	void *v;
    108  1.28   thorpej 	unsigned long sz;
    109  1.28   thorpej 	int mtype;
    110   1.1       cgd {
    111   1.1       cgd 
    112  1.23       mrg #if defined(UVM)
    113  1.28   thorpej 	uvm_km_free_poolpage1(mb_map, (vm_offset_t)v);
    114  1.23       mrg #else
    115  1.28   thorpej 	kmem_free_poolpage1(mb_map, (vm_offset_t)v);
    116  1.23       mrg #endif
    117   1.1       cgd }
    118   1.1       cgd 
    119   1.1       cgd /*
    120   1.1       cgd  * When MGET failes, ask protocols to free space when short of memory,
    121   1.1       cgd  * then re-attempt to allocate an mbuf.
    122   1.1       cgd  */
    123   1.1       cgd struct mbuf *
    124   1.1       cgd m_retry(i, t)
    125   1.1       cgd 	int i, t;
    126   1.1       cgd {
    127  1.27      matt 	struct mbuf *m;
    128   1.1       cgd 
    129  1.29   thorpej 	m_reclaim(i);
    130   1.1       cgd #define m_retry(i, t)	(struct mbuf *)0
    131   1.1       cgd 	MGET(m, i, t);
    132   1.1       cgd #undef m_retry
    133  1.18   thorpej 	if (m != NULL)
    134  1.18   thorpej 		mbstat.m_wait++;
    135  1.18   thorpej 	else
    136  1.18   thorpej 		mbstat.m_drops++;
    137   1.1       cgd 	return (m);
    138   1.1       cgd }
    139   1.1       cgd 
    140   1.1       cgd /*
    141   1.1       cgd  * As above; retry an MGETHDR.
    142   1.1       cgd  */
    143   1.1       cgd struct mbuf *
    144   1.1       cgd m_retryhdr(i, t)
    145   1.1       cgd 	int i, t;
    146   1.1       cgd {
    147  1.27      matt 	struct mbuf *m;
    148   1.1       cgd 
    149  1.29   thorpej 	m_reclaim(i);
    150   1.1       cgd #define m_retryhdr(i, t) (struct mbuf *)0
    151   1.1       cgd 	MGETHDR(m, i, t);
    152   1.1       cgd #undef m_retryhdr
    153  1.18   thorpej 	if (m != NULL)
    154  1.18   thorpej 		mbstat.m_wait++;
    155  1.18   thorpej 	else
    156  1.18   thorpej 		mbstat.m_drops++;
    157   1.1       cgd 	return (m);
    158   1.1       cgd }
    159   1.1       cgd 
    160  1.14  christos void
    161  1.29   thorpej m_reclaim(how)
    162  1.29   thorpej 	int how;
    163   1.1       cgd {
    164  1.27      matt 	struct domain *dp;
    165  1.27      matt 	struct protosw *pr;
    166  1.27      matt 	struct ifnet *ifp;
    167   1.1       cgd 	int s = splimp();
    168   1.1       cgd 
    169  1.29   thorpej 	/*
    170  1.29   thorpej 	 * Don't call the protocol drain routines if how == M_NOWAIT, which
    171  1.29   thorpej 	 * typically means we're in interrupt context.  Since we can be
    172  1.29   thorpej 	 * called from a network hardware interrupt, we could corrupt the
    173  1.29   thorpej 	 * protocol queues we try to drain them at that time.
    174  1.29   thorpej 	 */
    175  1.29   thorpej 	if (how == M_WAIT) {
    176  1.29   thorpej 		for (dp = domains; dp; dp = dp->dom_next)
    177  1.29   thorpej 			for (pr = dp->dom_protosw;
    178  1.29   thorpej 			     pr < dp->dom_protoswNPROTOSW; pr++)
    179  1.29   thorpej 				if (pr->pr_drain)
    180  1.29   thorpej 					(*pr->pr_drain)();
    181  1.29   thorpej 	}
    182  1.27      matt 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
    183  1.27      matt 		if (ifp->if_drain)
    184  1.27      matt 			(*ifp->if_drain)(ifp);
    185   1.1       cgd 	splx(s);
    186   1.1       cgd 	mbstat.m_drain++;
    187   1.1       cgd }
    188   1.1       cgd 
    189   1.1       cgd /*
    190   1.1       cgd  * Space allocation routines.
    191   1.1       cgd  * These are also available as macros
    192   1.1       cgd  * for critical paths.
    193   1.1       cgd  */
    194   1.1       cgd struct mbuf *
    195   1.5       cgd m_get(nowait, type)
    196   1.5       cgd 	int nowait, type;
    197   1.1       cgd {
    198  1.27      matt 	struct mbuf *m;
    199   1.1       cgd 
    200   1.5       cgd 	MGET(m, nowait, type);
    201   1.1       cgd 	return (m);
    202   1.1       cgd }
    203   1.1       cgd 
    204   1.1       cgd struct mbuf *
    205   1.5       cgd m_gethdr(nowait, type)
    206   1.5       cgd 	int nowait, type;
    207   1.1       cgd {
    208  1.27      matt 	struct mbuf *m;
    209   1.1       cgd 
    210   1.5       cgd 	MGETHDR(m, nowait, type);
    211   1.1       cgd 	return (m);
    212   1.1       cgd }
    213   1.1       cgd 
    214   1.1       cgd struct mbuf *
    215   1.5       cgd m_getclr(nowait, type)
    216   1.5       cgd 	int nowait, type;
    217   1.1       cgd {
    218  1.27      matt 	struct mbuf *m;
    219   1.1       cgd 
    220   1.5       cgd 	MGET(m, nowait, type);
    221   1.1       cgd 	if (m == 0)
    222   1.1       cgd 		return (0);
    223  1.30     perry 	memset(mtod(m, caddr_t), 0, MLEN);
    224   1.1       cgd 	return (m);
    225   1.1       cgd }
    226   1.1       cgd 
    227   1.1       cgd struct mbuf *
    228   1.1       cgd m_free(m)
    229   1.1       cgd 	struct mbuf *m;
    230   1.1       cgd {
    231  1.27      matt 	struct mbuf *n;
    232   1.1       cgd 
    233   1.1       cgd 	MFREE(m, n);
    234   1.1       cgd 	return (n);
    235   1.1       cgd }
    236   1.1       cgd 
    237   1.9   mycroft void
    238   1.1       cgd m_freem(m)
    239  1.27      matt 	struct mbuf *m;
    240   1.1       cgd {
    241  1.27      matt 	struct mbuf *n;
    242   1.1       cgd 
    243   1.1       cgd 	if (m == NULL)
    244   1.1       cgd 		return;
    245   1.1       cgd 	do {
    246   1.1       cgd 		MFREE(m, n);
    247  1.18   thorpej 		m = n;
    248  1.18   thorpej 	} while (m);
    249   1.1       cgd }
    250   1.1       cgd 
    251   1.1       cgd /*
    252   1.1       cgd  * Mbuffer utility routines.
    253   1.1       cgd  */
    254   1.1       cgd 
    255   1.1       cgd /*
    256   1.1       cgd  * Lesser-used path for M_PREPEND:
    257   1.1       cgd  * allocate new mbuf to prepend to chain,
    258   1.1       cgd  * copy junk along.
    259   1.1       cgd  */
    260   1.1       cgd struct mbuf *
    261   1.9   mycroft m_prepend(m, len, how)
    262  1.27      matt 	struct mbuf *m;
    263   1.9   mycroft 	int len, how;
    264   1.1       cgd {
    265   1.1       cgd 	struct mbuf *mn;
    266   1.1       cgd 
    267   1.9   mycroft 	MGET(mn, how, m->m_type);
    268   1.1       cgd 	if (mn == (struct mbuf *)NULL) {
    269   1.1       cgd 		m_freem(m);
    270   1.1       cgd 		return ((struct mbuf *)NULL);
    271   1.1       cgd 	}
    272   1.1       cgd 	if (m->m_flags & M_PKTHDR) {
    273   1.1       cgd 		M_COPY_PKTHDR(mn, m);
    274   1.1       cgd 		m->m_flags &= ~M_PKTHDR;
    275   1.1       cgd 	}
    276   1.1       cgd 	mn->m_next = m;
    277   1.1       cgd 	m = mn;
    278   1.1       cgd 	if (len < MHLEN)
    279   1.1       cgd 		MH_ALIGN(m, len);
    280   1.1       cgd 	m->m_len = len;
    281   1.1       cgd 	return (m);
    282   1.1       cgd }
    283   1.1       cgd 
    284   1.1       cgd /*
    285   1.1       cgd  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    286   1.1       cgd  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    287   1.1       cgd  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    288   1.1       cgd  */
    289   1.1       cgd int MCFail;
    290   1.1       cgd 
    291   1.1       cgd struct mbuf *
    292   1.1       cgd m_copym(m, off0, len, wait)
    293  1.27      matt 	struct mbuf *m;
    294   1.1       cgd 	int off0, wait;
    295  1.27      matt 	int len;
    296   1.1       cgd {
    297  1.27      matt 	struct mbuf *n, **np;
    298  1.27      matt 	int off = off0;
    299   1.1       cgd 	struct mbuf *top;
    300   1.1       cgd 	int copyhdr = 0;
    301   1.1       cgd 
    302   1.1       cgd 	if (off < 0 || len < 0)
    303   1.1       cgd 		panic("m_copym");
    304   1.1       cgd 	if (off == 0 && m->m_flags & M_PKTHDR)
    305   1.1       cgd 		copyhdr = 1;
    306   1.1       cgd 	while (off > 0) {
    307   1.1       cgd 		if (m == 0)
    308   1.1       cgd 			panic("m_copym");
    309   1.1       cgd 		if (off < m->m_len)
    310   1.1       cgd 			break;
    311   1.1       cgd 		off -= m->m_len;
    312   1.1       cgd 		m = m->m_next;
    313   1.1       cgd 	}
    314   1.1       cgd 	np = &top;
    315   1.1       cgd 	top = 0;
    316   1.1       cgd 	while (len > 0) {
    317   1.1       cgd 		if (m == 0) {
    318   1.1       cgd 			if (len != M_COPYALL)
    319   1.1       cgd 				panic("m_copym");
    320   1.1       cgd 			break;
    321   1.1       cgd 		}
    322   1.1       cgd 		MGET(n, wait, m->m_type);
    323   1.1       cgd 		*np = n;
    324   1.1       cgd 		if (n == 0)
    325   1.1       cgd 			goto nospace;
    326   1.1       cgd 		if (copyhdr) {
    327   1.1       cgd 			M_COPY_PKTHDR(n, m);
    328   1.1       cgd 			if (len == M_COPYALL)
    329   1.1       cgd 				n->m_pkthdr.len -= off0;
    330   1.1       cgd 			else
    331   1.1       cgd 				n->m_pkthdr.len = len;
    332   1.1       cgd 			copyhdr = 0;
    333   1.1       cgd 		}
    334   1.9   mycroft 		n->m_len = min(len, m->m_len - off);
    335   1.1       cgd 		if (m->m_flags & M_EXT) {
    336   1.1       cgd 			n->m_data = m->m_data + off;
    337   1.1       cgd 			n->m_ext = m->m_ext;
    338  1.18   thorpej 			MCLADDREFERENCE(m, n);
    339   1.1       cgd 		} else
    340  1.30     perry 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
    341   1.1       cgd 			    (unsigned)n->m_len);
    342   1.1       cgd 		if (len != M_COPYALL)
    343   1.1       cgd 			len -= n->m_len;
    344   1.1       cgd 		off = 0;
    345   1.1       cgd 		m = m->m_next;
    346   1.1       cgd 		np = &n->m_next;
    347   1.1       cgd 	}
    348   1.1       cgd 	if (top == 0)
    349   1.1       cgd 		MCFail++;
    350   1.1       cgd 	return (top);
    351   1.1       cgd nospace:
    352   1.1       cgd 	m_freem(top);
    353   1.1       cgd 	MCFail++;
    354   1.1       cgd 	return (0);
    355   1.1       cgd }
    356   1.1       cgd 
    357   1.1       cgd /*
    358  1.18   thorpej  * Copy an entire packet, including header (which must be present).
    359  1.18   thorpej  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    360  1.18   thorpej  */
    361  1.18   thorpej struct mbuf *
    362  1.18   thorpej m_copypacket(m, how)
    363  1.18   thorpej 	struct mbuf *m;
    364  1.18   thorpej 	int how;
    365  1.18   thorpej {
    366  1.18   thorpej 	struct mbuf *top, *n, *o;
    367  1.18   thorpej 
    368  1.18   thorpej 	MGET(n, how, m->m_type);
    369  1.18   thorpej 	top = n;
    370  1.18   thorpej 	if (!n)
    371  1.18   thorpej 		goto nospace;
    372  1.18   thorpej 
    373  1.18   thorpej 	M_COPY_PKTHDR(n, m);
    374  1.18   thorpej 	n->m_len = m->m_len;
    375  1.18   thorpej 	if (m->m_flags & M_EXT) {
    376  1.18   thorpej 		n->m_data = m->m_data;
    377  1.18   thorpej 		n->m_ext = m->m_ext;
    378  1.18   thorpej 		MCLADDREFERENCE(m, n);
    379  1.18   thorpej 	} else {
    380  1.30     perry 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    381  1.18   thorpej 	}
    382  1.18   thorpej 
    383  1.18   thorpej 	m = m->m_next;
    384  1.18   thorpej 	while (m) {
    385  1.18   thorpej 		MGET(o, how, m->m_type);
    386  1.18   thorpej 		if (!o)
    387  1.18   thorpej 			goto nospace;
    388  1.18   thorpej 
    389  1.18   thorpej 		n->m_next = o;
    390  1.18   thorpej 		n = n->m_next;
    391  1.18   thorpej 
    392  1.18   thorpej 		n->m_len = m->m_len;
    393  1.18   thorpej 		if (m->m_flags & M_EXT) {
    394  1.18   thorpej 			n->m_data = m->m_data;
    395  1.18   thorpej 			n->m_ext = m->m_ext;
    396  1.18   thorpej 			MCLADDREFERENCE(m, n);
    397  1.18   thorpej 		} else {
    398  1.30     perry 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    399  1.18   thorpej 		}
    400  1.18   thorpej 
    401  1.18   thorpej 		m = m->m_next;
    402  1.18   thorpej 	}
    403  1.18   thorpej 	return top;
    404  1.18   thorpej nospace:
    405  1.18   thorpej 	m_freem(top);
    406  1.18   thorpej 	MCFail++;
    407  1.18   thorpej 	return 0;
    408  1.18   thorpej }
    409  1.18   thorpej 
    410  1.18   thorpej /*
    411   1.1       cgd  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    412   1.1       cgd  * continuing for "len" bytes, into the indicated buffer.
    413   1.1       cgd  */
    414  1.14  christos void
    415   1.1       cgd m_copydata(m, off, len, cp)
    416  1.27      matt 	struct mbuf *m;
    417  1.27      matt 	int off;
    418  1.27      matt 	int len;
    419   1.1       cgd 	caddr_t cp;
    420   1.1       cgd {
    421  1.27      matt 	unsigned count;
    422   1.1       cgd 
    423   1.1       cgd 	if (off < 0 || len < 0)
    424   1.1       cgd 		panic("m_copydata");
    425   1.1       cgd 	while (off > 0) {
    426   1.1       cgd 		if (m == 0)
    427   1.1       cgd 			panic("m_copydata");
    428   1.1       cgd 		if (off < m->m_len)
    429   1.1       cgd 			break;
    430   1.1       cgd 		off -= m->m_len;
    431   1.1       cgd 		m = m->m_next;
    432   1.1       cgd 	}
    433   1.1       cgd 	while (len > 0) {
    434   1.1       cgd 		if (m == 0)
    435   1.1       cgd 			panic("m_copydata");
    436   1.9   mycroft 		count = min(m->m_len - off, len);
    437  1.30     perry 		memcpy(cp, mtod(m, caddr_t) + off, count);
    438   1.1       cgd 		len -= count;
    439   1.1       cgd 		cp += count;
    440   1.1       cgd 		off = 0;
    441   1.1       cgd 		m = m->m_next;
    442   1.1       cgd 	}
    443   1.1       cgd }
    444   1.1       cgd 
    445   1.1       cgd /*
    446   1.1       cgd  * Concatenate mbuf chain n to m.
    447   1.1       cgd  * Both chains must be of the same type (e.g. MT_DATA).
    448   1.1       cgd  * Any m_pkthdr is not updated.
    449   1.1       cgd  */
    450  1.14  christos void
    451   1.1       cgd m_cat(m, n)
    452  1.27      matt 	struct mbuf *m, *n;
    453   1.1       cgd {
    454   1.1       cgd 	while (m->m_next)
    455   1.1       cgd 		m = m->m_next;
    456   1.1       cgd 	while (n) {
    457   1.1       cgd 		if (m->m_flags & M_EXT ||
    458   1.1       cgd 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
    459   1.1       cgd 			/* just join the two chains */
    460   1.1       cgd 			m->m_next = n;
    461   1.1       cgd 			return;
    462   1.1       cgd 		}
    463   1.1       cgd 		/* splat the data from one into the other */
    464  1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    465   1.1       cgd 		    (u_int)n->m_len);
    466   1.1       cgd 		m->m_len += n->m_len;
    467   1.1       cgd 		n = m_free(n);
    468   1.1       cgd 	}
    469   1.1       cgd }
    470   1.1       cgd 
    471  1.11   mycroft void
    472   1.1       cgd m_adj(mp, req_len)
    473   1.1       cgd 	struct mbuf *mp;
    474   1.8   deraadt 	int req_len;
    475   1.1       cgd {
    476  1.27      matt 	int len = req_len;
    477  1.27      matt 	struct mbuf *m;
    478  1.27      matt 	int count;
    479   1.1       cgd 
    480   1.1       cgd 	if ((m = mp) == NULL)
    481   1.1       cgd 		return;
    482   1.1       cgd 	if (len >= 0) {
    483   1.1       cgd 		/*
    484   1.1       cgd 		 * Trim from head.
    485   1.1       cgd 		 */
    486   1.1       cgd 		while (m != NULL && len > 0) {
    487   1.1       cgd 			if (m->m_len <= len) {
    488   1.1       cgd 				len -= m->m_len;
    489   1.1       cgd 				m->m_len = 0;
    490   1.1       cgd 				m = m->m_next;
    491   1.1       cgd 			} else {
    492   1.1       cgd 				m->m_len -= len;
    493   1.1       cgd 				m->m_data += len;
    494   1.1       cgd 				len = 0;
    495   1.1       cgd 			}
    496   1.1       cgd 		}
    497   1.1       cgd 		m = mp;
    498   1.1       cgd 		if (mp->m_flags & M_PKTHDR)
    499   1.1       cgd 			m->m_pkthdr.len -= (req_len - len);
    500   1.1       cgd 	} else {
    501   1.1       cgd 		/*
    502   1.1       cgd 		 * Trim from tail.  Scan the mbuf chain,
    503   1.1       cgd 		 * calculating its length and finding the last mbuf.
    504   1.1       cgd 		 * If the adjustment only affects this mbuf, then just
    505   1.1       cgd 		 * adjust and return.  Otherwise, rescan and truncate
    506   1.1       cgd 		 * after the remaining size.
    507   1.1       cgd 		 */
    508   1.1       cgd 		len = -len;
    509   1.1       cgd 		count = 0;
    510   1.1       cgd 		for (;;) {
    511   1.1       cgd 			count += m->m_len;
    512   1.1       cgd 			if (m->m_next == (struct mbuf *)0)
    513   1.1       cgd 				break;
    514   1.1       cgd 			m = m->m_next;
    515   1.1       cgd 		}
    516   1.1       cgd 		if (m->m_len >= len) {
    517   1.1       cgd 			m->m_len -= len;
    518   1.8   deraadt 			if (mp->m_flags & M_PKTHDR)
    519   1.8   deraadt 				mp->m_pkthdr.len -= len;
    520   1.1       cgd 			return;
    521   1.1       cgd 		}
    522   1.1       cgd 		count -= len;
    523   1.1       cgd 		if (count < 0)
    524   1.1       cgd 			count = 0;
    525   1.1       cgd 		/*
    526   1.1       cgd 		 * Correct length for chain is "count".
    527   1.1       cgd 		 * Find the mbuf with last data, adjust its length,
    528   1.1       cgd 		 * and toss data from remaining mbufs on chain.
    529   1.1       cgd 		 */
    530   1.1       cgd 		m = mp;
    531   1.1       cgd 		if (m->m_flags & M_PKTHDR)
    532   1.1       cgd 			m->m_pkthdr.len = count;
    533   1.1       cgd 		for (; m; m = m->m_next) {
    534   1.1       cgd 			if (m->m_len >= count) {
    535   1.1       cgd 				m->m_len = count;
    536   1.1       cgd 				break;
    537   1.1       cgd 			}
    538   1.1       cgd 			count -= m->m_len;
    539   1.1       cgd 		}
    540  1.18   thorpej 		while (m->m_next)
    541  1.18   thorpej 			(m = m->m_next) ->m_len = 0;
    542   1.1       cgd 	}
    543   1.1       cgd }
    544   1.1       cgd 
    545   1.1       cgd /*
    546   1.1       cgd  * Rearange an mbuf chain so that len bytes are contiguous
    547   1.1       cgd  * and in the data area of an mbuf (so that mtod and dtom
    548   1.1       cgd  * will work for a structure of size len).  Returns the resulting
    549   1.1       cgd  * mbuf chain on success, frees it and returns null on failure.
    550   1.1       cgd  * If there is room, it will add up to max_protohdr-len extra bytes to the
    551   1.1       cgd  * contiguous region in an attempt to avoid being called next time.
    552   1.1       cgd  */
    553   1.1       cgd int MPFail;
    554   1.1       cgd 
    555   1.1       cgd struct mbuf *
    556   1.1       cgd m_pullup(n, len)
    557  1.27      matt 	struct mbuf *n;
    558   1.1       cgd 	int len;
    559   1.1       cgd {
    560  1.27      matt 	struct mbuf *m;
    561  1.27      matt 	int count;
    562   1.1       cgd 	int space;
    563   1.1       cgd 
    564   1.1       cgd 	/*
    565   1.1       cgd 	 * If first mbuf has no cluster, and has room for len bytes
    566   1.1       cgd 	 * without shifting current data, pullup into it,
    567   1.1       cgd 	 * otherwise allocate a new mbuf to prepend to the chain.
    568   1.1       cgd 	 */
    569   1.1       cgd 	if ((n->m_flags & M_EXT) == 0 &&
    570   1.1       cgd 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    571   1.1       cgd 		if (n->m_len >= len)
    572   1.1       cgd 			return (n);
    573   1.1       cgd 		m = n;
    574   1.1       cgd 		n = n->m_next;
    575   1.1       cgd 		len -= m->m_len;
    576   1.1       cgd 	} else {
    577   1.1       cgd 		if (len > MHLEN)
    578   1.1       cgd 			goto bad;
    579   1.1       cgd 		MGET(m, M_DONTWAIT, n->m_type);
    580   1.1       cgd 		if (m == 0)
    581   1.1       cgd 			goto bad;
    582   1.1       cgd 		m->m_len = 0;
    583   1.1       cgd 		if (n->m_flags & M_PKTHDR) {
    584   1.1       cgd 			M_COPY_PKTHDR(m, n);
    585   1.1       cgd 			n->m_flags &= ~M_PKTHDR;
    586   1.1       cgd 		}
    587   1.1       cgd 	}
    588   1.1       cgd 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    589   1.1       cgd 	do {
    590   1.1       cgd 		count = min(min(max(len, max_protohdr), space), n->m_len);
    591  1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    592   1.1       cgd 		  (unsigned)count);
    593   1.1       cgd 		len -= count;
    594   1.1       cgd 		m->m_len += count;
    595   1.1       cgd 		n->m_len -= count;
    596   1.1       cgd 		space -= count;
    597   1.1       cgd 		if (n->m_len)
    598   1.1       cgd 			n->m_data += count;
    599   1.1       cgd 		else
    600   1.1       cgd 			n = m_free(n);
    601   1.1       cgd 	} while (len > 0 && n);
    602   1.1       cgd 	if (len > 0) {
    603   1.1       cgd 		(void) m_free(m);
    604   1.1       cgd 		goto bad;
    605   1.1       cgd 	}
    606   1.1       cgd 	m->m_next = n;
    607   1.1       cgd 	return (m);
    608   1.1       cgd bad:
    609   1.1       cgd 	m_freem(n);
    610   1.1       cgd 	MPFail++;
    611   1.1       cgd 	return (0);
    612   1.9   mycroft }
    613   1.9   mycroft 
    614   1.9   mycroft /*
    615   1.9   mycroft  * Partition an mbuf chain in two pieces, returning the tail --
    616   1.9   mycroft  * all but the first len0 bytes.  In case of failure, it returns NULL and
    617   1.9   mycroft  * attempts to restore the chain to its original state.
    618   1.9   mycroft  */
    619   1.9   mycroft struct mbuf *
    620   1.9   mycroft m_split(m0, len0, wait)
    621  1.27      matt 	struct mbuf *m0;
    622   1.9   mycroft 	int len0, wait;
    623   1.9   mycroft {
    624  1.27      matt 	struct mbuf *m, *n;
    625  1.22   thorpej 	unsigned len = len0, remain, len_save;
    626   1.9   mycroft 
    627   1.9   mycroft 	for (m = m0; m && len > m->m_len; m = m->m_next)
    628   1.9   mycroft 		len -= m->m_len;
    629   1.9   mycroft 	if (m == 0)
    630   1.9   mycroft 		return (0);
    631   1.9   mycroft 	remain = m->m_len - len;
    632   1.9   mycroft 	if (m0->m_flags & M_PKTHDR) {
    633   1.9   mycroft 		MGETHDR(n, wait, m0->m_type);
    634   1.9   mycroft 		if (n == 0)
    635   1.9   mycroft 			return (0);
    636   1.9   mycroft 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    637   1.9   mycroft 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    638  1.22   thorpej 		len_save = m0->m_pkthdr.len;
    639   1.9   mycroft 		m0->m_pkthdr.len = len0;
    640   1.9   mycroft 		if (m->m_flags & M_EXT)
    641   1.9   mycroft 			goto extpacket;
    642   1.9   mycroft 		if (remain > MHLEN) {
    643   1.9   mycroft 			/* m can't be the lead packet */
    644   1.9   mycroft 			MH_ALIGN(n, 0);
    645   1.9   mycroft 			n->m_next = m_split(m, len, wait);
    646   1.9   mycroft 			if (n->m_next == 0) {
    647   1.9   mycroft 				(void) m_free(n);
    648  1.22   thorpej 				m0->m_pkthdr.len = len_save;
    649   1.9   mycroft 				return (0);
    650   1.9   mycroft 			} else
    651   1.9   mycroft 				return (n);
    652   1.9   mycroft 		} else
    653   1.9   mycroft 			MH_ALIGN(n, remain);
    654   1.9   mycroft 	} else if (remain == 0) {
    655   1.9   mycroft 		n = m->m_next;
    656   1.9   mycroft 		m->m_next = 0;
    657   1.9   mycroft 		return (n);
    658   1.9   mycroft 	} else {
    659   1.9   mycroft 		MGET(n, wait, m->m_type);
    660   1.9   mycroft 		if (n == 0)
    661   1.9   mycroft 			return (0);
    662   1.9   mycroft 		M_ALIGN(n, remain);
    663   1.9   mycroft 	}
    664   1.9   mycroft extpacket:
    665   1.9   mycroft 	if (m->m_flags & M_EXT) {
    666   1.9   mycroft 		n->m_ext = m->m_ext;
    667  1.18   thorpej 		MCLADDREFERENCE(m, n);
    668   1.9   mycroft 		n->m_data = m->m_data + len;
    669   1.9   mycroft 	} else {
    670  1.30     perry 		memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
    671   1.9   mycroft 	}
    672   1.9   mycroft 	n->m_len = remain;
    673   1.9   mycroft 	m->m_len = len;
    674   1.9   mycroft 	n->m_next = m->m_next;
    675   1.9   mycroft 	m->m_next = 0;
    676   1.9   mycroft 	return (n);
    677   1.9   mycroft }
    678   1.9   mycroft /*
    679   1.9   mycroft  * Routine to copy from device local memory into mbufs.
    680   1.9   mycroft  */
    681   1.9   mycroft struct mbuf *
    682   1.9   mycroft m_devget(buf, totlen, off0, ifp, copy)
    683   1.9   mycroft 	char *buf;
    684   1.9   mycroft 	int totlen, off0;
    685   1.9   mycroft 	struct ifnet *ifp;
    686  1.18   thorpej 	void (*copy) __P((const void *from, void *to, size_t len));
    687   1.9   mycroft {
    688  1.27      matt 	struct mbuf *m;
    689   1.9   mycroft 	struct mbuf *top = 0, **mp = &top;
    690  1.27      matt 	int off = off0, len;
    691  1.27      matt 	char *cp;
    692   1.9   mycroft 	char *epkt;
    693   1.9   mycroft 
    694   1.9   mycroft 	cp = buf;
    695   1.9   mycroft 	epkt = cp + totlen;
    696   1.9   mycroft 	if (off) {
    697  1.13       cgd 		/*
    698  1.13       cgd 		 * If 'off' is non-zero, packet is trailer-encapsulated,
    699  1.13       cgd 		 * so we have to skip the type and length fields.
    700  1.13       cgd 		 */
    701  1.13       cgd 		cp += off + 2 * sizeof(u_int16_t);
    702  1.13       cgd 		totlen -= 2 * sizeof(u_int16_t);
    703   1.9   mycroft 	}
    704   1.9   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    705   1.9   mycroft 	if (m == 0)
    706   1.9   mycroft 		return (0);
    707   1.9   mycroft 	m->m_pkthdr.rcvif = ifp;
    708   1.9   mycroft 	m->m_pkthdr.len = totlen;
    709   1.9   mycroft 	m->m_len = MHLEN;
    710   1.9   mycroft 
    711   1.9   mycroft 	while (totlen > 0) {
    712   1.9   mycroft 		if (top) {
    713   1.9   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
    714   1.9   mycroft 			if (m == 0) {
    715   1.9   mycroft 				m_freem(top);
    716   1.9   mycroft 				return (0);
    717   1.9   mycroft 			}
    718   1.9   mycroft 			m->m_len = MLEN;
    719   1.9   mycroft 		}
    720   1.9   mycroft 		len = min(totlen, epkt - cp);
    721   1.9   mycroft 		if (len >= MINCLSIZE) {
    722   1.9   mycroft 			MCLGET(m, M_DONTWAIT);
    723  1.19   mycroft 			if ((m->m_flags & M_EXT) == 0) {
    724  1.20   mycroft 				m_free(m);
    725  1.19   mycroft 				m_freem(top);
    726  1.19   mycroft 				return (0);
    727  1.19   mycroft 			}
    728  1.19   mycroft 			m->m_len = len = min(len, MCLBYTES);
    729   1.9   mycroft 		} else {
    730   1.9   mycroft 			/*
    731   1.9   mycroft 			 * Place initial small packet/header at end of mbuf.
    732   1.9   mycroft 			 */
    733   1.9   mycroft 			if (len < m->m_len) {
    734   1.9   mycroft 				if (top == 0 && len + max_linkhdr <= m->m_len)
    735   1.9   mycroft 					m->m_data += max_linkhdr;
    736   1.9   mycroft 				m->m_len = len;
    737   1.9   mycroft 			} else
    738   1.9   mycroft 				len = m->m_len;
    739   1.9   mycroft 		}
    740   1.9   mycroft 		if (copy)
    741  1.14  christos 			copy(cp, mtod(m, caddr_t), (size_t)len);
    742   1.9   mycroft 		else
    743  1.30     perry 			memcpy(mtod(m, caddr_t), cp, (size_t)len);
    744   1.9   mycroft 		cp += len;
    745   1.9   mycroft 		*mp = m;
    746   1.9   mycroft 		mp = &m->m_next;
    747   1.9   mycroft 		totlen -= len;
    748   1.9   mycroft 		if (cp == epkt)
    749   1.9   mycroft 			cp = buf;
    750   1.9   mycroft 	}
    751   1.9   mycroft 	return (top);
    752  1.18   thorpej }
    753  1.18   thorpej 
    754  1.18   thorpej /*
    755  1.18   thorpej  * Copy data from a buffer back into the indicated mbuf chain,
    756  1.18   thorpej  * starting "off" bytes from the beginning, extending the mbuf
    757  1.18   thorpej  * chain if necessary.
    758  1.18   thorpej  */
    759  1.18   thorpej void
    760  1.18   thorpej m_copyback(m0, off, len, cp)
    761  1.18   thorpej 	struct	mbuf *m0;
    762  1.27      matt 	int off;
    763  1.27      matt 	int len;
    764  1.18   thorpej 	caddr_t cp;
    765  1.18   thorpej {
    766  1.27      matt 	int mlen;
    767  1.27      matt 	struct mbuf *m = m0, *n;
    768  1.18   thorpej 	int totlen = 0;
    769  1.18   thorpej 
    770  1.18   thorpej 	if (m0 == 0)
    771  1.18   thorpej 		return;
    772  1.18   thorpej 	while (off > (mlen = m->m_len)) {
    773  1.18   thorpej 		off -= mlen;
    774  1.18   thorpej 		totlen += mlen;
    775  1.18   thorpej 		if (m->m_next == 0) {
    776  1.18   thorpej 			n = m_getclr(M_DONTWAIT, m->m_type);
    777  1.18   thorpej 			if (n == 0)
    778  1.18   thorpej 				goto out;
    779  1.18   thorpej 			n->m_len = min(MLEN, len + off);
    780  1.18   thorpej 			m->m_next = n;
    781  1.18   thorpej 		}
    782  1.18   thorpej 		m = m->m_next;
    783  1.18   thorpej 	}
    784  1.18   thorpej 	while (len > 0) {
    785  1.18   thorpej 		mlen = min (m->m_len - off, len);
    786  1.30     perry 		memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
    787  1.18   thorpej 		cp += mlen;
    788  1.18   thorpej 		len -= mlen;
    789  1.18   thorpej 		mlen += off;
    790  1.18   thorpej 		off = 0;
    791  1.18   thorpej 		totlen += mlen;
    792  1.18   thorpej 		if (len == 0)
    793  1.18   thorpej 			break;
    794  1.18   thorpej 		if (m->m_next == 0) {
    795  1.18   thorpej 			n = m_get(M_DONTWAIT, m->m_type);
    796  1.18   thorpej 			if (n == 0)
    797  1.18   thorpej 				break;
    798  1.18   thorpej 			n->m_len = min(MLEN, len);
    799  1.18   thorpej 			m->m_next = n;
    800  1.18   thorpej 		}
    801  1.18   thorpej 		m = m->m_next;
    802  1.18   thorpej 	}
    803  1.18   thorpej out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
    804  1.18   thorpej 		m->m_pkthdr.len = totlen;
    805   1.1       cgd }
    806