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