xref: /csrg-svn/sys/kern/uipc_mbuf.c (revision 40816)
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.13 (Berkeley) 04/05/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/MCLBYTES)
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 /*
235  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
236  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
237  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
238  */
239 struct mbuf *
240 m_copym(m, off0, len, wait)
241 	register struct mbuf *m;
242 	int off0, wait;
243 	register int len;
244 {
245 	register struct mbuf *n, **np;
246 	register int off = off0;
247 	struct mbuf *top;
248 	int copyhdr = 0;
249 
250 	if (off < 0 || len < 0)
251 		panic("m_copym");
252 	if (off == 0 && m->m_flags & M_PKTHDR)
253 		copyhdr = 1;
254 	while (off > 0) {
255 		if (m == 0)
256 			panic("m_copym");
257 		if (off < m->m_len)
258 			break;
259 		off -= m->m_len;
260 		m = m->m_next;
261 	}
262 	np = &top;
263 	top = 0;
264 	while (len > 0) {
265 		if (m == 0) {
266 			if (len != M_COPYALL)
267 				panic("m_copym");
268 			break;
269 		}
270 		MGET(n, wait, m->m_type);
271 		*np = n;
272 		if (n == 0)
273 			goto nospace;
274 		if (copyhdr) {
275 			M_COPY_PKTHDR(n, m);
276 			if (len == M_COPYALL)
277 				n->m_pkthdr.len -= off0;
278 			else
279 				n->m_pkthdr.len = len;
280 			copyhdr = 0;
281 		}
282 		n->m_len = MIN(len, m->m_len - off);
283 		if (m->m_flags & M_EXT) {
284 			n->m_data = m->m_data + off;
285 			mclrefcnt[mtocl(m->m_ext.ext_buf)]++;
286 			n->m_ext = m->m_ext;
287 			n->m_flags |= M_EXT;
288 		} else
289 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
290 			    (unsigned)n->m_len);
291 		if (len != M_COPYALL)
292 			len -= n->m_len;
293 		off = 0;
294 		m = m->m_next;
295 		np = &n->m_next;
296 	}
297 	return (top);
298 nospace:
299 	m_freem(top);
300 	return (0);
301 }
302 
303 /*
304  * Copy data from an mbuf chain starting "off" bytes from the beginning,
305  * continuing for "len" bytes, into the indicated buffer.
306  */
307 m_copydata(m, off, len, cp)
308 	register struct mbuf *m;
309 	register int off;
310 	register int len;
311 	caddr_t cp;
312 {
313 	register unsigned count;
314 
315 	if (off < 0 || len < 0)
316 		panic("m_copydata");
317 	while (off > 0) {
318 		if (m == 0)
319 			panic("m_copydata");
320 		if (off < m->m_len)
321 			break;
322 		off -= m->m_len;
323 		m = m->m_next;
324 	}
325 	while (len > 0) {
326 		if (m == 0)
327 			panic("m_copydata");
328 		count = MIN(m->m_len - off, len);
329 		bcopy(mtod(m, caddr_t) + off, cp, count);
330 		len -= count;
331 		cp += count;
332 		off = 0;
333 		m = m->m_next;
334 	}
335 }
336 
337 /*
338  * Concatenate mbuf chain n to m.
339  * Both chains must be of the same type (e.g. MT_DATA).
340  * Any m_pkthdr is not updated.
341  */
342 m_cat(m, n)
343 	register struct mbuf *m, *n;
344 {
345 	while (m->m_next)
346 		m = m->m_next;
347 	while (n) {
348 		if (m->m_flags & M_EXT ||
349 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
350 			/* just join the two chains */
351 			m->m_next = n;
352 			return;
353 		}
354 		/* splat the data from one into the other */
355 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
356 		    (u_int)n->m_len);
357 		m->m_len += n->m_len;
358 		n = m_free(n);
359 	}
360 }
361 
362 m_adj(mp, req_len)
363 	struct mbuf *mp;
364 {
365 	register int len = req_len;
366 	register struct mbuf *m;
367 	register count;
368 
369 	if ((m = mp) == NULL)
370 		return;
371 	if (len >= 0) {
372 		/*
373 		 * Trim from head.
374 		 */
375 		while (m != NULL && len > 0) {
376 			if (m->m_len <= len) {
377 				len -= m->m_len;
378 				m->m_len = 0;
379 				m = m->m_next;
380 			} else {
381 				m->m_len -= len;
382 				m->m_data += len;
383 				len = 0;
384 			}
385 		}
386 		m = mp;
387 		if (mp->m_flags & M_PKTHDR)
388 			m->m_pkthdr.len -= (req_len - len);
389 	} else {
390 		/*
391 		 * Trim from tail.  Scan the mbuf chain,
392 		 * calculating its length and finding the last mbuf.
393 		 * If the adjustment only affects this mbuf, then just
394 		 * adjust and return.  Otherwise, rescan and truncate
395 		 * after the remaining size.
396 		 */
397 		len = -len;
398 		count = 0;
399 		for (;;) {
400 			count += m->m_len;
401 			if (m->m_next == (struct mbuf *)0)
402 				break;
403 			m = m->m_next;
404 		}
405 		if (m->m_len >= len) {
406 			m->m_len -= len;
407 			if ((mp = m)->m_flags & M_PKTHDR)
408 				m->m_pkthdr.len -= len;
409 			return;
410 		}
411 		count -= len;
412 		if (count < 0)
413 			count = 0;
414 		/*
415 		 * Correct length for chain is "count".
416 		 * Find the mbuf with last data, adjust its length,
417 		 * and toss data from remaining mbufs on chain.
418 		 */
419 		m = mp;
420 		if (m->m_flags & M_PKTHDR)
421 			m->m_pkthdr.len = count;
422 		for (; m; m = m->m_next) {
423 			if (m->m_len >= count) {
424 				m->m_len = count;
425 				break;
426 			}
427 			count -= m->m_len;
428 		}
429 		while (m = m->m_next)
430 			m->m_len = 0;
431 	}
432 }
433 
434 /*
435  * Rearange an mbuf chain so that len bytes are contiguous
436  * and in the data area of an mbuf (so that mtod and dtom
437  * will work for a structure of size len).  Returns the resulting
438  * mbuf chain on success, frees it and returns null on failure.
439  * If there is room, it will add up to max_protohdr-len extra bytes to the
440  * contiguous region in an attempt to avoid being called next time.
441  */
442 struct mbuf *
443 m_pullup(n, len)
444 	register struct mbuf *n;
445 	int len;
446 {
447 	register struct mbuf *m;
448 	register int count;
449 	int space;
450 
451 	/*
452 	 * If first mbuf has no cluster, and has room for len bytes
453 	 * without shifting current data, pullup into it,
454 	 * otherwise allocate a new mbuf to prepend to the chain.
455 	 */
456 	if ((n->m_flags & M_EXT) == 0 &&
457 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
458 		if (n->m_len >= len)
459 			return (n);
460 		m = n;
461 		n = n->m_next;
462 		len -= m->m_len;
463 	} else {
464 		if (len > MHLEN)
465 			goto bad;
466 		MGET(m, M_DONTWAIT, n->m_type);
467 		if (m == 0)
468 			goto bad;
469 		m->m_len = 0;
470 		if (n->m_flags & M_PKTHDR)
471 			M_COPY_PKTHDR(m, n);
472 	}
473 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
474 	do {
475 		count = min(min(max(len, max_protohdr), space), n->m_len);
476 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
477 		  (unsigned)count);
478 		len -= count;
479 		m->m_len += count;
480 		n->m_len -= count;
481 		space -= count;
482 		if (n->m_len)
483 			n->m_data += count;
484 		else
485 			n = m_free(n);
486 	} while (len > 0 && n);
487 	if (len > 0) {
488 		(void) m_free(m);
489 		goto bad;
490 	}
491 	m->m_next = n;
492 	return (m);
493 bad:
494 	m_freem(n);
495 	return (0);
496 }
497