uipc_mbuf.c revision 1.28 1 /* $NetBSD: uipc_mbuf.c,v 1.28 1998/08/01 01:35:20 thorpej 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.4 (Berkeley) 2/14/95
36 */
37
38 #include "opt_uvm.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/malloc.h>
44 #include <sys/map.h>
45 #define MBTYPES
46 #include <sys/mbuf.h>
47 #include <sys/kernel.h>
48 #include <sys/syslog.h>
49 #include <sys/domain.h>
50 #include <sys/protosw.h>
51 #include <sys/pool.h>
52 #include <sys/socket.h>
53 #include <net/if.h>
54
55 #include <vm/vm.h>
56
57 #if defined(UVM)
58 #include <uvm/uvm_extern.h>
59 #endif
60
61 struct pool mbpool; /* mbuf pool */
62 struct pool mclpool; /* mbuf cluster pool */
63
64 struct mbuf *mbutl;
65 struct mbstat mbstat;
66 union mcluster *mclfree;
67 int max_linkhdr;
68 int max_protohdr;
69 int max_hdr;
70 int max_datalen;
71
72 extern vm_map_t mb_map;
73
74 void *mclpool_alloc __P((unsigned long, int, int));
75 void mclpool_release __P((void *, unsigned long, int));
76
77 /*
78 * Initialize the mbuf allcator. Note, this cannot allocate any
79 * memory itself; we are called before mb_map has been allocated.
80 */
81 void
82 mbinit()
83 {
84
85 /* XXX malloc types! */
86 pool_init(&mbpool, MSIZE, 0, 0, 0, "mbpl", 0, NULL, NULL, 0);
87 pool_init(&mclpool, MCLBYTES, 0, 0, 0, "mclpl", 0, mclpool_alloc,
88 mclpool_release, 0);
89 }
90
91 void *
92 mclpool_alloc(sz, flags, mtype)
93 unsigned long sz;
94 int flags;
95 int mtype;
96 {
97
98 #if defined(UVM)
99 return ((void *)uvm_km_alloc_poolpage1(mb_map, uvmexp.mb_object));
100 #else
101 return ((void *)kmem_alloc_poolpage1(mb_map));
102 #endif
103 }
104
105 void
106 mclpool_release(v, sz, mtype)
107 void *v;
108 unsigned long sz;
109 int mtype;
110 {
111
112 #if defined(UVM)
113 uvm_km_free_poolpage1(mb_map, (vm_offset_t)v);
114 #else
115 kmem_free_poolpage1(mb_map, (vm_offset_t)v);
116 #endif
117 }
118
119 /*
120 * When MGET failes, ask protocols to free space when short of memory,
121 * then re-attempt to allocate an mbuf.
122 */
123 struct mbuf *
124 m_retry(i, t)
125 int i, t;
126 {
127 struct mbuf *m;
128
129 m_reclaim();
130 #define m_retry(i, t) (struct mbuf *)0
131 MGET(m, i, t);
132 #undef m_retry
133 if (m != NULL)
134 mbstat.m_wait++;
135 else
136 mbstat.m_drops++;
137 return (m);
138 }
139
140 /*
141 * As above; retry an MGETHDR.
142 */
143 struct mbuf *
144 m_retryhdr(i, t)
145 int i, t;
146 {
147 struct mbuf *m;
148
149 m_reclaim();
150 #define m_retryhdr(i, t) (struct mbuf *)0
151 MGETHDR(m, i, t);
152 #undef m_retryhdr
153 if (m != NULL)
154 mbstat.m_wait++;
155 else
156 mbstat.m_drops++;
157 return (m);
158 }
159
160 void
161 m_reclaim()
162 {
163 struct domain *dp;
164 struct protosw *pr;
165 struct ifnet *ifp;
166 int s = splimp();
167
168 for (dp = domains; dp; dp = dp->dom_next)
169 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
170 if (pr->pr_drain)
171 (*pr->pr_drain)();
172 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
173 if (ifp->if_drain)
174 (*ifp->if_drain)(ifp);
175 splx(s);
176 mbstat.m_drain++;
177 }
178
179 /*
180 * Space allocation routines.
181 * These are also available as macros
182 * for critical paths.
183 */
184 struct mbuf *
185 m_get(nowait, type)
186 int nowait, type;
187 {
188 struct mbuf *m;
189
190 MGET(m, nowait, type);
191 return (m);
192 }
193
194 struct mbuf *
195 m_gethdr(nowait, type)
196 int nowait, type;
197 {
198 struct mbuf *m;
199
200 MGETHDR(m, nowait, type);
201 return (m);
202 }
203
204 struct mbuf *
205 m_getclr(nowait, type)
206 int nowait, type;
207 {
208 struct mbuf *m;
209
210 MGET(m, nowait, type);
211 if (m == 0)
212 return (0);
213 bzero(mtod(m, caddr_t), MLEN);
214 return (m);
215 }
216
217 struct mbuf *
218 m_free(m)
219 struct mbuf *m;
220 {
221 struct mbuf *n;
222
223 MFREE(m, n);
224 return (n);
225 }
226
227 void
228 m_freem(m)
229 struct mbuf *m;
230 {
231 struct mbuf *n;
232
233 if (m == NULL)
234 return;
235 do {
236 MFREE(m, n);
237 m = n;
238 } while (m);
239 }
240
241 /*
242 * Mbuffer utility routines.
243 */
244
245 /*
246 * Lesser-used path for M_PREPEND:
247 * allocate new mbuf to prepend to chain,
248 * copy junk along.
249 */
250 struct mbuf *
251 m_prepend(m, len, how)
252 struct mbuf *m;
253 int len, how;
254 {
255 struct mbuf *mn;
256
257 MGET(mn, how, m->m_type);
258 if (mn == (struct mbuf *)NULL) {
259 m_freem(m);
260 return ((struct mbuf *)NULL);
261 }
262 if (m->m_flags & M_PKTHDR) {
263 M_COPY_PKTHDR(mn, m);
264 m->m_flags &= ~M_PKTHDR;
265 }
266 mn->m_next = m;
267 m = mn;
268 if (len < MHLEN)
269 MH_ALIGN(m, len);
270 m->m_len = len;
271 return (m);
272 }
273
274 /*
275 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
276 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
277 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
278 */
279 int MCFail;
280
281 struct mbuf *
282 m_copym(m, off0, len, wait)
283 struct mbuf *m;
284 int off0, wait;
285 int len;
286 {
287 struct mbuf *n, **np;
288 int off = off0;
289 struct mbuf *top;
290 int copyhdr = 0;
291
292 if (off < 0 || len < 0)
293 panic("m_copym");
294 if (off == 0 && m->m_flags & M_PKTHDR)
295 copyhdr = 1;
296 while (off > 0) {
297 if (m == 0)
298 panic("m_copym");
299 if (off < m->m_len)
300 break;
301 off -= m->m_len;
302 m = m->m_next;
303 }
304 np = ⊤
305 top = 0;
306 while (len > 0) {
307 if (m == 0) {
308 if (len != M_COPYALL)
309 panic("m_copym");
310 break;
311 }
312 MGET(n, wait, m->m_type);
313 *np = n;
314 if (n == 0)
315 goto nospace;
316 if (copyhdr) {
317 M_COPY_PKTHDR(n, m);
318 if (len == M_COPYALL)
319 n->m_pkthdr.len -= off0;
320 else
321 n->m_pkthdr.len = len;
322 copyhdr = 0;
323 }
324 n->m_len = min(len, m->m_len - off);
325 if (m->m_flags & M_EXT) {
326 n->m_data = m->m_data + off;
327 n->m_ext = m->m_ext;
328 MCLADDREFERENCE(m, n);
329 } else
330 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
331 (unsigned)n->m_len);
332 if (len != M_COPYALL)
333 len -= n->m_len;
334 off = 0;
335 m = m->m_next;
336 np = &n->m_next;
337 }
338 if (top == 0)
339 MCFail++;
340 return (top);
341 nospace:
342 m_freem(top);
343 MCFail++;
344 return (0);
345 }
346
347 /*
348 * Copy an entire packet, including header (which must be present).
349 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
350 */
351 struct mbuf *
352 m_copypacket(m, how)
353 struct mbuf *m;
354 int how;
355 {
356 struct mbuf *top, *n, *o;
357
358 MGET(n, how, m->m_type);
359 top = n;
360 if (!n)
361 goto nospace;
362
363 M_COPY_PKTHDR(n, m);
364 n->m_len = m->m_len;
365 if (m->m_flags & M_EXT) {
366 n->m_data = m->m_data;
367 n->m_ext = m->m_ext;
368 MCLADDREFERENCE(m, n);
369 } else {
370 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
371 }
372
373 m = m->m_next;
374 while (m) {
375 MGET(o, how, m->m_type);
376 if (!o)
377 goto nospace;
378
379 n->m_next = o;
380 n = n->m_next;
381
382 n->m_len = m->m_len;
383 if (m->m_flags & M_EXT) {
384 n->m_data = m->m_data;
385 n->m_ext = m->m_ext;
386 MCLADDREFERENCE(m, n);
387 } else {
388 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
389 }
390
391 m = m->m_next;
392 }
393 return top;
394 nospace:
395 m_freem(top);
396 MCFail++;
397 return 0;
398 }
399
400 /*
401 * Copy data from an mbuf chain starting "off" bytes from the beginning,
402 * continuing for "len" bytes, into the indicated buffer.
403 */
404 void
405 m_copydata(m, off, len, cp)
406 struct mbuf *m;
407 int off;
408 int len;
409 caddr_t cp;
410 {
411 unsigned count;
412
413 if (off < 0 || len < 0)
414 panic("m_copydata");
415 while (off > 0) {
416 if (m == 0)
417 panic("m_copydata");
418 if (off < m->m_len)
419 break;
420 off -= m->m_len;
421 m = m->m_next;
422 }
423 while (len > 0) {
424 if (m == 0)
425 panic("m_copydata");
426 count = min(m->m_len - off, len);
427 bcopy(mtod(m, caddr_t) + off, cp, count);
428 len -= count;
429 cp += count;
430 off = 0;
431 m = m->m_next;
432 }
433 }
434
435 /*
436 * Concatenate mbuf chain n to m.
437 * Both chains must be of the same type (e.g. MT_DATA).
438 * Any m_pkthdr is not updated.
439 */
440 void
441 m_cat(m, n)
442 struct mbuf *m, *n;
443 {
444 while (m->m_next)
445 m = m->m_next;
446 while (n) {
447 if (m->m_flags & M_EXT ||
448 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
449 /* just join the two chains */
450 m->m_next = n;
451 return;
452 }
453 /* splat the data from one into the other */
454 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
455 (u_int)n->m_len);
456 m->m_len += n->m_len;
457 n = m_free(n);
458 }
459 }
460
461 void
462 m_adj(mp, req_len)
463 struct mbuf *mp;
464 int req_len;
465 {
466 int len = req_len;
467 struct mbuf *m;
468 int count;
469
470 if ((m = mp) == NULL)
471 return;
472 if (len >= 0) {
473 /*
474 * Trim from head.
475 */
476 while (m != NULL && len > 0) {
477 if (m->m_len <= len) {
478 len -= m->m_len;
479 m->m_len = 0;
480 m = m->m_next;
481 } else {
482 m->m_len -= len;
483 m->m_data += len;
484 len = 0;
485 }
486 }
487 m = mp;
488 if (mp->m_flags & M_PKTHDR)
489 m->m_pkthdr.len -= (req_len - len);
490 } else {
491 /*
492 * Trim from tail. Scan the mbuf chain,
493 * calculating its length and finding the last mbuf.
494 * If the adjustment only affects this mbuf, then just
495 * adjust and return. Otherwise, rescan and truncate
496 * after the remaining size.
497 */
498 len = -len;
499 count = 0;
500 for (;;) {
501 count += m->m_len;
502 if (m->m_next == (struct mbuf *)0)
503 break;
504 m = m->m_next;
505 }
506 if (m->m_len >= len) {
507 m->m_len -= len;
508 if (mp->m_flags & M_PKTHDR)
509 mp->m_pkthdr.len -= len;
510 return;
511 }
512 count -= len;
513 if (count < 0)
514 count = 0;
515 /*
516 * Correct length for chain is "count".
517 * Find the mbuf with last data, adjust its length,
518 * and toss data from remaining mbufs on chain.
519 */
520 m = mp;
521 if (m->m_flags & M_PKTHDR)
522 m->m_pkthdr.len = count;
523 for (; m; m = m->m_next) {
524 if (m->m_len >= count) {
525 m->m_len = count;
526 break;
527 }
528 count -= m->m_len;
529 }
530 while (m->m_next)
531 (m = m->m_next) ->m_len = 0;
532 }
533 }
534
535 /*
536 * Rearange an mbuf chain so that len bytes are contiguous
537 * and in the data area of an mbuf (so that mtod and dtom
538 * will work for a structure of size len). Returns the resulting
539 * mbuf chain on success, frees it and returns null on failure.
540 * If there is room, it will add up to max_protohdr-len extra bytes to the
541 * contiguous region in an attempt to avoid being called next time.
542 */
543 int MPFail;
544
545 struct mbuf *
546 m_pullup(n, len)
547 struct mbuf *n;
548 int len;
549 {
550 struct mbuf *m;
551 int count;
552 int space;
553
554 /*
555 * If first mbuf has no cluster, and has room for len bytes
556 * without shifting current data, pullup into it,
557 * otherwise allocate a new mbuf to prepend to the chain.
558 */
559 if ((n->m_flags & M_EXT) == 0 &&
560 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
561 if (n->m_len >= len)
562 return (n);
563 m = n;
564 n = n->m_next;
565 len -= m->m_len;
566 } else {
567 if (len > MHLEN)
568 goto bad;
569 MGET(m, M_DONTWAIT, n->m_type);
570 if (m == 0)
571 goto bad;
572 m->m_len = 0;
573 if (n->m_flags & M_PKTHDR) {
574 M_COPY_PKTHDR(m, n);
575 n->m_flags &= ~M_PKTHDR;
576 }
577 }
578 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
579 do {
580 count = min(min(max(len, max_protohdr), space), n->m_len);
581 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
582 (unsigned)count);
583 len -= count;
584 m->m_len += count;
585 n->m_len -= count;
586 space -= count;
587 if (n->m_len)
588 n->m_data += count;
589 else
590 n = m_free(n);
591 } while (len > 0 && n);
592 if (len > 0) {
593 (void) m_free(m);
594 goto bad;
595 }
596 m->m_next = n;
597 return (m);
598 bad:
599 m_freem(n);
600 MPFail++;
601 return (0);
602 }
603
604 /*
605 * Partition an mbuf chain in two pieces, returning the tail --
606 * all but the first len0 bytes. In case of failure, it returns NULL and
607 * attempts to restore the chain to its original state.
608 */
609 struct mbuf *
610 m_split(m0, len0, wait)
611 struct mbuf *m0;
612 int len0, wait;
613 {
614 struct mbuf *m, *n;
615 unsigned len = len0, remain, len_save;
616
617 for (m = m0; m && len > m->m_len; m = m->m_next)
618 len -= m->m_len;
619 if (m == 0)
620 return (0);
621 remain = m->m_len - len;
622 if (m0->m_flags & M_PKTHDR) {
623 MGETHDR(n, wait, m0->m_type);
624 if (n == 0)
625 return (0);
626 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
627 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
628 len_save = m0->m_pkthdr.len;
629 m0->m_pkthdr.len = len0;
630 if (m->m_flags & M_EXT)
631 goto extpacket;
632 if (remain > MHLEN) {
633 /* m can't be the lead packet */
634 MH_ALIGN(n, 0);
635 n->m_next = m_split(m, len, wait);
636 if (n->m_next == 0) {
637 (void) m_free(n);
638 m0->m_pkthdr.len = len_save;
639 return (0);
640 } else
641 return (n);
642 } else
643 MH_ALIGN(n, remain);
644 } else if (remain == 0) {
645 n = m->m_next;
646 m->m_next = 0;
647 return (n);
648 } else {
649 MGET(n, wait, m->m_type);
650 if (n == 0)
651 return (0);
652 M_ALIGN(n, remain);
653 }
654 extpacket:
655 if (m->m_flags & M_EXT) {
656 n->m_ext = m->m_ext;
657 MCLADDREFERENCE(m, n);
658 n->m_data = m->m_data + len;
659 } else {
660 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
661 }
662 n->m_len = remain;
663 m->m_len = len;
664 n->m_next = m->m_next;
665 m->m_next = 0;
666 return (n);
667 }
668 /*
669 * Routine to copy from device local memory into mbufs.
670 */
671 struct mbuf *
672 m_devget(buf, totlen, off0, ifp, copy)
673 char *buf;
674 int totlen, off0;
675 struct ifnet *ifp;
676 void (*copy) __P((const void *from, void *to, size_t len));
677 {
678 struct mbuf *m;
679 struct mbuf *top = 0, **mp = ⊤
680 int off = off0, len;
681 char *cp;
682 char *epkt;
683
684 cp = buf;
685 epkt = cp + totlen;
686 if (off) {
687 /*
688 * If 'off' is non-zero, packet is trailer-encapsulated,
689 * so we have to skip the type and length fields.
690 */
691 cp += off + 2 * sizeof(u_int16_t);
692 totlen -= 2 * sizeof(u_int16_t);
693 }
694 MGETHDR(m, M_DONTWAIT, MT_DATA);
695 if (m == 0)
696 return (0);
697 m->m_pkthdr.rcvif = ifp;
698 m->m_pkthdr.len = totlen;
699 m->m_len = MHLEN;
700
701 while (totlen > 0) {
702 if (top) {
703 MGET(m, M_DONTWAIT, MT_DATA);
704 if (m == 0) {
705 m_freem(top);
706 return (0);
707 }
708 m->m_len = MLEN;
709 }
710 len = min(totlen, epkt - cp);
711 if (len >= MINCLSIZE) {
712 MCLGET(m, M_DONTWAIT);
713 if ((m->m_flags & M_EXT) == 0) {
714 m_free(m);
715 m_freem(top);
716 return (0);
717 }
718 m->m_len = len = min(len, MCLBYTES);
719 } else {
720 /*
721 * Place initial small packet/header at end of mbuf.
722 */
723 if (len < m->m_len) {
724 if (top == 0 && len + max_linkhdr <= m->m_len)
725 m->m_data += max_linkhdr;
726 m->m_len = len;
727 } else
728 len = m->m_len;
729 }
730 if (copy)
731 copy(cp, mtod(m, caddr_t), (size_t)len);
732 else
733 bcopy(cp, mtod(m, caddr_t), (size_t)len);
734 cp += len;
735 *mp = m;
736 mp = &m->m_next;
737 totlen -= len;
738 if (cp == epkt)
739 cp = buf;
740 }
741 return (top);
742 }
743
744 /*
745 * Copy data from a buffer back into the indicated mbuf chain,
746 * starting "off" bytes from the beginning, extending the mbuf
747 * chain if necessary.
748 */
749 void
750 m_copyback(m0, off, len, cp)
751 struct mbuf *m0;
752 int off;
753 int len;
754 caddr_t cp;
755 {
756 int mlen;
757 struct mbuf *m = m0, *n;
758 int totlen = 0;
759
760 if (m0 == 0)
761 return;
762 while (off > (mlen = m->m_len)) {
763 off -= mlen;
764 totlen += mlen;
765 if (m->m_next == 0) {
766 n = m_getclr(M_DONTWAIT, m->m_type);
767 if (n == 0)
768 goto out;
769 n->m_len = min(MLEN, len + off);
770 m->m_next = n;
771 }
772 m = m->m_next;
773 }
774 while (len > 0) {
775 mlen = min (m->m_len - off, len);
776 bcopy(cp, mtod(m, caddr_t) + off, (unsigned)mlen);
777 cp += mlen;
778 len -= mlen;
779 mlen += off;
780 off = 0;
781 totlen += mlen;
782 if (len == 0)
783 break;
784 if (m->m_next == 0) {
785 n = m_get(M_DONTWAIT, m->m_type);
786 if (n == 0)
787 break;
788 n->m_len = min(MLEN, len);
789 m->m_next = n;
790 }
791 m = m->m_next;
792 }
793 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
794 m->m_pkthdr.len = totlen;
795 }
796