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