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