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