xref: /csrg-svn/sys/kern/uipc_mbuf.c (revision 41999)
1 /*
2  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)uipc_mbuf.c	7.15 (Berkeley) 05/15/90
18  */
19 
20 #include "param.h"
21 #include "user.h"
22 #include "proc.h"
23 #include "cmap.h"
24 #include "malloc.h"
25 #include "map.h"
26 #define MBTYPES
27 #include "mbuf.h"
28 #include "vm.h"
29 #include "kernel.h"
30 #include "syslog.h"
31 #include "domain.h"
32 #include "protosw.h"
33 #include "machine/pte.h"
34 
35 mbinit()
36 {
37 	int s;
38 
39 #if MCLBYTES < 4096
40 #define NCL_INIT	(4096/CLBYTES)
41 #else
42 #define NCL_INIT	1
43 #endif
44 	s = splimp();
45 	if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0)
46 		goto bad;
47 	splx(s);
48 	return;
49 bad:
50 	panic("mbinit");
51 }
52 
53 /*
54  * Allocate some number of mbuf clusters
55  * and place on cluster free list.
56  * Must be called at splimp.
57  */
58 /* ARGSUSED */
59 m_clalloc(ncl, canwait)
60 	register int ncl;
61 {
62 	int npg, mbx;
63 	register caddr_t p;
64 	register int i;
65 	static int logged;
66 
67 	npg = ncl * CLSIZE;
68 	mbx = rmalloc(mbmap, (long)npg);
69 	if (mbx == 0) {
70 		if (logged == 0) {
71 			logged++;
72 			log(LOG_ERR, "mbuf map full\n");
73 		}
74 		return (0);
75 	}
76 	p = cltom(mbx * NBPG / MCLBYTES);
77 	if (memall(&Mbmap[mbx], npg, proc, CSYS) == 0) {
78 		rmfree(mbmap, (long)npg, (long)mbx);
79 		return (0);
80 	}
81 	vmaccess(&Mbmap[mbx], p, npg);
82 	ncl = ncl * CLBYTES / MCLBYTES;
83 	for (i = 0; i < ncl; i++) {
84 		((union mcluster *)p)->mcl_next = mclfree;
85 		mclfree = (union mcluster *)p;
86 		p += MCLBYTES;
87 		mbstat.m_clfree++;
88 	}
89 	mbstat.m_clusters += ncl;
90 	return (1);
91 }
92 
93 /*
94  * When MGET failes, ask protocols to free space when short of memory,
95  * then re-attempt to allocate an mbuf.
96  */
97 struct mbuf *
98 m_retry(i, t)
99 	int i, t;
100 {
101 	register struct mbuf *m;
102 
103 	m_reclaim();
104 #define m_retry(i, t)	(struct mbuf *)0
105 	MGET(m, i, t);
106 #undef m_retry
107 	return (m);
108 }
109 
110 /*
111  * As above; retry an MGETHDR.
112  */
113 struct mbuf *
114 m_retryhdr(i, t)
115 	int i, t;
116 {
117 	register struct mbuf *m;
118 
119 	m_reclaim();
120 #define m_retryhdr(i, t) (struct mbuf *)0
121 	MGETHDR(m, i, t);
122 #undef m_retryhdr
123 	return (m);
124 }
125 
126 m_reclaim()
127 {
128 	register struct domain *dp;
129 	register struct protosw *pr;
130 	int s = splimp();
131 
132 	for (dp = domains; dp; dp = dp->dom_next)
133 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
134 			if (pr->pr_drain)
135 				(*pr->pr_drain)();
136 	splx(s);
137 	mbstat.m_drain++;
138 }
139 
140 /*
141  * Space allocation routines.
142  * These are also available as macros
143  * for critical paths.
144  */
145 struct mbuf *
146 m_get(canwait, type)
147 	int canwait, type;
148 {
149 	register struct mbuf *m;
150 
151 	MGET(m, canwait, type);
152 	return (m);
153 }
154 
155 struct mbuf *
156 m_gethdr(canwait, type)
157 	int canwait, type;
158 {
159 	register struct mbuf *m;
160 
161 	MGETHDR(m, canwait, type);
162 	return (m);
163 }
164 
165 struct mbuf *
166 m_getclr(canwait, type)
167 	int canwait, type;
168 {
169 	register struct mbuf *m;
170 
171 	MGET(m, canwait, type);
172 	if (m == 0)
173 		return (0);
174 	bzero(mtod(m, caddr_t), MLEN);
175 	return (m);
176 }
177 
178 struct mbuf *
179 m_free(m)
180 	struct mbuf *m;
181 {
182 	register struct mbuf *n;
183 
184 	MFREE(m, n);
185 	return (n);
186 }
187 
188 m_freem(m)
189 	register struct mbuf *m;
190 {
191 	register struct mbuf *n;
192 
193 	if (m == NULL)
194 		return;
195 	do {
196 		MFREE(m, n);
197 	} while (m = n);
198 }
199 
200 /*
201  * Mbuffer utility routines.
202  */
203 
204 /*
205  * Lesser-used path for M_PREPEND:
206  * allocate new mbuf to prepend to chain,
207  * copy junk along.
208  */
209 struct mbuf *
210 m_prepend(m, len, how)
211 	register struct mbuf *m;
212 	int len, how;
213 {
214 	struct mbuf *mn;
215 
216 	MGET(mn, how, m->m_type);
217 	if (mn == (struct mbuf *)NULL) {
218 		m_freem(m);
219 		return ((struct mbuf *)NULL);
220 	}
221 	if (m->m_flags & M_PKTHDR) {
222 		M_COPY_PKTHDR(mn, m);
223 		m->m_flags &= ~M_PKTHDR;
224 	}
225 	mn->m_next = m;
226 	m = mn;
227 	if (len < MHLEN)
228 		MH_ALIGN(m, len);
229 	m->m_len = len;
230 	return (m);
231 }
232 
233 /*
234  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
235  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
236  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
237  */
238 int MCFail;
239 
240 struct mbuf *
241 m_copym(m, off0, len, wait)
242 	register struct mbuf *m;
243 	int off0, wait;
244 	register int len;
245 {
246 	register struct mbuf *n, **np;
247 	register int off = off0;
248 	struct mbuf *top;
249 	int copyhdr = 0;
250 
251 	if (off < 0 || len < 0)
252 		panic("m_copym");
253 	if (off == 0 && m->m_flags & M_PKTHDR)
254 		copyhdr = 1;
255 	while (off > 0) {
256 		if (m == 0)
257 			panic("m_copym");
258 		if (off < m->m_len)
259 			break;
260 		off -= m->m_len;
261 		m = m->m_next;
262 	}
263 	np = &top;
264 	top = 0;
265 	while (len > 0) {
266 		if (m == 0) {
267 			if (len != M_COPYALL)
268 				panic("m_copym");
269 			break;
270 		}
271 		MGET(n, wait, m->m_type);
272 		*np = n;
273 		if (n == 0)
274 			goto nospace;
275 		if (copyhdr) {
276 			M_COPY_PKTHDR(n, m);
277 			if (len == M_COPYALL)
278 				n->m_pkthdr.len -= off0;
279 			else
280 				n->m_pkthdr.len = len;
281 			copyhdr = 0;
282 		}
283 		n->m_len = MIN(len, m->m_len - off);
284 		if (m->m_flags & M_EXT) {
285 			n->m_data = m->m_data + off;
286 			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
287 			n->m_ext = m->m_ext;
288 			n->m_flags |= M_EXT;
289 		} else
290 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
291 			    (unsigned)n->m_len);
292 		if (len != M_COPYALL)
293 			len -= n->m_len;
294 		off = 0;
295 		m = m->m_next;
296 		np = &n->m_next;
297 	}
298 	if (top == 0)
299 		MCFail++;
300 	return (top);
301 nospace:
302 	m_freem(top);
303 	MCFail++;
304 	return (0);
305 }
306 
307 /*
308  * Copy data from an mbuf chain starting "off" bytes from the beginning,
309  * continuing for "len" bytes, into the indicated buffer.
310  */
311 m_copydata(m, off, len, cp)
312 	register struct mbuf *m;
313 	register int off;
314 	register int len;
315 	caddr_t cp;
316 {
317 	register unsigned count;
318 
319 	if (off < 0 || len < 0)
320 		panic("m_copydata");
321 	while (off > 0) {
322 		if (m == 0)
323 			panic("m_copydata");
324 		if (off < m->m_len)
325 			break;
326 		off -= m->m_len;
327 		m = m->m_next;
328 	}
329 	while (len > 0) {
330 		if (m == 0)
331 			panic("m_copydata");
332 		count = MIN(m->m_len - off, len);
333 		bcopy(mtod(m, caddr_t) + off, cp, count);
334 		len -= count;
335 		cp += count;
336 		off = 0;
337 		m = m->m_next;
338 	}
339 }
340 
341 /*
342  * Concatenate mbuf chain n to m.
343  * Both chains must be of the same type (e.g. MT_DATA).
344  * Any m_pkthdr is not updated.
345  */
346 m_cat(m, n)
347 	register struct mbuf *m, *n;
348 {
349 	while (m->m_next)
350 		m = m->m_next;
351 	while (n) {
352 		if (m->m_flags & M_EXT ||
353 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
354 			/* just join the two chains */
355 			m->m_next = n;
356 			return;
357 		}
358 		/* splat the data from one into the other */
359 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
360 		    (u_int)n->m_len);
361 		m->m_len += n->m_len;
362 		n = m_free(n);
363 	}
364 }
365 
366 m_adj(mp, req_len)
367 	struct mbuf *mp;
368 {
369 	register int len = req_len;
370 	register struct mbuf *m;
371 	register count;
372 
373 	if ((m = mp) == NULL)
374 		return;
375 	if (len >= 0) {
376 		/*
377 		 * Trim from head.
378 		 */
379 		while (m != NULL && len > 0) {
380 			if (m->m_len <= len) {
381 				len -= m->m_len;
382 				m->m_len = 0;
383 				m = m->m_next;
384 			} else {
385 				m->m_len -= len;
386 				m->m_data += len;
387 				len = 0;
388 			}
389 		}
390 		m = mp;
391 		if (mp->m_flags & M_PKTHDR)
392 			m->m_pkthdr.len -= (req_len - len);
393 	} else {
394 		/*
395 		 * Trim from tail.  Scan the mbuf chain,
396 		 * calculating its length and finding the last mbuf.
397 		 * If the adjustment only affects this mbuf, then just
398 		 * adjust and return.  Otherwise, rescan and truncate
399 		 * after the remaining size.
400 		 */
401 		len = -len;
402 		count = 0;
403 		for (;;) {
404 			count += m->m_len;
405 			if (m->m_next == (struct mbuf *)0)
406 				break;
407 			m = m->m_next;
408 		}
409 		if (m->m_len >= len) {
410 			m->m_len -= len;
411 			if ((mp = m)->m_flags & M_PKTHDR)
412 				m->m_pkthdr.len -= len;
413 			return;
414 		}
415 		count -= len;
416 		if (count < 0)
417 			count = 0;
418 		/*
419 		 * Correct length for chain is "count".
420 		 * Find the mbuf with last data, adjust its length,
421 		 * and toss data from remaining mbufs on chain.
422 		 */
423 		m = mp;
424 		if (m->m_flags & M_PKTHDR)
425 			m->m_pkthdr.len = count;
426 		for (; m; m = m->m_next) {
427 			if (m->m_len >= count) {
428 				m->m_len = count;
429 				break;
430 			}
431 			count -= m->m_len;
432 		}
433 		while (m = m->m_next)
434 			m->m_len = 0;
435 	}
436 }
437 
438 /*
439  * Rearange an mbuf chain so that len bytes are contiguous
440  * and in the data area of an mbuf (so that mtod and dtom
441  * will work for a structure of size len).  Returns the resulting
442  * mbuf chain on success, frees it and returns null on failure.
443  * If there is room, it will add up to max_protohdr-len extra bytes to the
444  * contiguous region in an attempt to avoid being called next time.
445  */
446 int MPFail;
447 
448 struct mbuf *
449 m_pullup(n, len)
450 	register struct mbuf *n;
451 	int len;
452 {
453 	register struct mbuf *m;
454 	register int count;
455 	int space;
456 
457 	/*
458 	 * If first mbuf has no cluster, and has room for len bytes
459 	 * without shifting current data, pullup into it,
460 	 * otherwise allocate a new mbuf to prepend to the chain.
461 	 */
462 	if ((n->m_flags & M_EXT) == 0 &&
463 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
464 		if (n->m_len >= len)
465 			return (n);
466 		m = n;
467 		n = n->m_next;
468 		len -= m->m_len;
469 	} else {
470 		if (len > MHLEN)
471 			goto bad;
472 		MGET(m, M_DONTWAIT, n->m_type);
473 		if (m == 0)
474 			goto bad;
475 		m->m_len = 0;
476 		if (n->m_flags & M_PKTHDR)
477 			M_COPY_PKTHDR(m, n);
478 	}
479 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
480 	do {
481 		count = min(min(max(len, max_protohdr), space), n->m_len);
482 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
483 		  (unsigned)count);
484 		len -= count;
485 		m->m_len += count;
486 		n->m_len -= count;
487 		space -= count;
488 		if (n->m_len)
489 			n->m_data += count;
490 		else
491 			n = m_free(n);
492 	} while (len > 0 && n);
493 	if (len > 0) {
494 		(void) m_free(m);
495 		goto bad;
496 	}
497 	m->m_next = n;
498 	return (m);
499 bad:
500 	m_freem(n);
501 	MPFail++;
502 	return (0);
503 }
504