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