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