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