uipc_mbuf.c revision 1.58 1 /* $NetBSD: uipc_mbuf.c,v 1.58 2002/03/08 20:48:41 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1991, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
73 */
74
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.58 2002/03/08 20:48:41 thorpej Exp $");
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/proc.h>
81 #include <sys/malloc.h>
82 #include <sys/map.h>
83 #define MBTYPES
84 #include <sys/mbuf.h>
85 #include <sys/kernel.h>
86 #include <sys/syslog.h>
87 #include <sys/domain.h>
88 #include <sys/protosw.h>
89 #include <sys/pool.h>
90 #include <sys/socket.h>
91 #include <sys/sysctl.h>
92
93 #include <net/if.h>
94
95 #include <uvm/uvm_extern.h>
96
97
98 struct pool mbpool; /* mbuf pool */
99 struct pool mclpool; /* mbuf cluster pool */
100
101 struct pool_cache mbpool_cache;
102 struct pool_cache mclpool_cache;
103
104 struct mbstat mbstat;
105 int max_linkhdr;
106 int max_protohdr;
107 int max_hdr;
108 int max_datalen;
109
110 void *mclpool_alloc __P((struct pool *, int));
111 void mclpool_release __P((struct pool *, void *));
112
113 struct pool_allocator mclpool_allocator = {
114 mclpool_alloc, mclpool_release, 0,
115 };
116
117 static struct mbuf *m_copym0 __P((struct mbuf *, int, int, int, int));
118
119 const char mclpool_warnmsg[] =
120 "WARNING: mclpool limit reached; increase NMBCLUSTERS";
121
122 /*
123 * Initialize the mbuf allcator.
124 */
125 void
126 mbinit()
127 {
128
129 pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL);
130 pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator);
131
132 pool_cache_init(&mbpool_cache, &mbpool, NULL, NULL, NULL);
133 pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
134
135 /*
136 * Set the hard limit on the mclpool to the number of
137 * mbuf clusters the kernel is to support. Log the limit
138 * reached message max once a minute.
139 */
140 pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
141
142 /*
143 * Set a low water mark for both mbufs and clusters. This should
144 * help ensure that they can be allocated in a memory starvation
145 * situation. This is important for e.g. diskless systems which
146 * must allocate mbufs in order for the pagedaemon to clean pages.
147 */
148 pool_setlowat(&mbpool, mblowat);
149 pool_setlowat(&mclpool, mcllowat);
150 }
151
152 int
153 sysctl_dombuf(name, namelen, oldp, oldlenp, newp, newlen)
154 int *name;
155 u_int namelen;
156 void *oldp;
157 size_t *oldlenp;
158 void *newp;
159 size_t newlen;
160 {
161 int error, newval;
162
163 /* All sysctl names at this level are terminal. */
164 if (namelen != 1)
165 return (ENOTDIR); /* overloaded */
166
167 switch (name[0]) {
168 case MBUF_MSIZE:
169 return (sysctl_rdint(oldp, oldlenp, newp, msize));
170 case MBUF_MCLBYTES:
171 return (sysctl_rdint(oldp, oldlenp, newp, mclbytes));
172 case MBUF_NMBCLUSTERS:
173 /*
174 * If we have direct-mapped pool pages, we can adjust this
175 * number on the fly. If not, we're limited by the size
176 * of mb_map, and cannot change this value.
177 *
178 * Note: we only allow the value to be increased, never
179 * decreased.
180 */
181 if (mb_map == NULL) {
182 newval = nmbclusters;
183 error = sysctl_int(oldp, oldlenp, newp, newlen,
184 &newval);
185 if (error != 0)
186 return (error);
187 if (newp != NULL) {
188 if (newval >= nmbclusters) {
189 nmbclusters = newval;
190 pool_sethardlimit(&mclpool,
191 nmbclusters, mclpool_warnmsg, 60);
192 } else
193 error = EINVAL;
194 }
195 return (error);
196 } else
197 return (sysctl_rdint(oldp, oldlenp, newp, nmbclusters));
198 case MBUF_MBLOWAT:
199 case MBUF_MCLLOWAT:
200 /* New value must be >= 0. */
201 newval = (name[0] == MBUF_MBLOWAT) ? mblowat : mcllowat;
202 error = sysctl_int(oldp, oldlenp, newp, newlen, &newval);
203 if (error != 0)
204 return (error);
205 if (newp != NULL) {
206 if (newval >= 0) {
207 if (name[0] == MBUF_MBLOWAT) {
208 mblowat = newval;
209 pool_setlowat(&mbpool, newval);
210 } else {
211 mcllowat = newval;
212 pool_setlowat(&mclpool, newval);
213 }
214 } else
215 error = EINVAL;
216 }
217 return (error);
218 default:
219 return (EOPNOTSUPP);
220 }
221 /* NOTREACHED */
222 }
223
224 void *
225 mclpool_alloc(pp, flags)
226 struct pool *pp;
227 int flags;
228 {
229 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
230
231 return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
232 }
233
234 void
235 mclpool_release(pp, v)
236 struct pool *pp;
237 void *v;
238 {
239
240 uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
241 }
242
243 /*
244 * When MGET failes, ask protocols to free space when short of memory,
245 * then re-attempt to allocate an mbuf.
246 */
247 struct mbuf *
248 m_retry(i, t)
249 int i, t;
250 {
251 struct mbuf *m;
252
253 m_reclaim(i);
254 #define m_retry(i, t) (struct mbuf *)0
255 MGET(m, i, t);
256 #undef m_retry
257 if (m != NULL)
258 mbstat.m_wait++;
259 else
260 mbstat.m_drops++;
261 return (m);
262 }
263
264 /*
265 * As above; retry an MGETHDR.
266 */
267 struct mbuf *
268 m_retryhdr(i, t)
269 int i, t;
270 {
271 struct mbuf *m;
272
273 m_reclaim(i);
274 #define m_retryhdr(i, t) (struct mbuf *)0
275 MGETHDR(m, i, t);
276 #undef m_retryhdr
277 if (m != NULL)
278 mbstat.m_wait++;
279 else
280 mbstat.m_drops++;
281 return (m);
282 }
283
284 void
285 m_reclaim(how)
286 int how;
287 {
288 struct domain *dp;
289 struct protosw *pr;
290 struct ifnet *ifp;
291 int s = splvm();
292
293 for (dp = domains; dp; dp = dp->dom_next)
294 for (pr = dp->dom_protosw;
295 pr < dp->dom_protoswNPROTOSW; pr++)
296 if (pr->pr_drain)
297 (*pr->pr_drain)();
298 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
299 if (ifp->if_drain)
300 (*ifp->if_drain)(ifp);
301 splx(s);
302 mbstat.m_drain++;
303 }
304
305 /*
306 * Space allocation routines.
307 * These are also available as macros
308 * for critical paths.
309 */
310 struct mbuf *
311 m_get(nowait, type)
312 int nowait, type;
313 {
314 struct mbuf *m;
315
316 MGET(m, nowait, type);
317 return (m);
318 }
319
320 struct mbuf *
321 m_gethdr(nowait, type)
322 int nowait, type;
323 {
324 struct mbuf *m;
325
326 MGETHDR(m, nowait, type);
327 return (m);
328 }
329
330 struct mbuf *
331 m_getclr(nowait, type)
332 int nowait, type;
333 {
334 struct mbuf *m;
335
336 MGET(m, nowait, type);
337 if (m == 0)
338 return (0);
339 memset(mtod(m, caddr_t), 0, MLEN);
340 return (m);
341 }
342
343 struct mbuf *
344 m_free(m)
345 struct mbuf *m;
346 {
347 struct mbuf *n;
348
349 MFREE(m, n);
350 return (n);
351 }
352
353 void
354 m_freem(m)
355 struct mbuf *m;
356 {
357 struct mbuf *n;
358
359 if (m == NULL)
360 return;
361 do {
362 MFREE(m, n);
363 m = n;
364 } while (m);
365 }
366
367 /*
368 * Mbuffer utility routines.
369 */
370
371 /*
372 * Lesser-used path for M_PREPEND:
373 * allocate new mbuf to prepend to chain,
374 * copy junk along.
375 */
376 struct mbuf *
377 m_prepend(m, len, how)
378 struct mbuf *m;
379 int len, how;
380 {
381 struct mbuf *mn;
382
383 MGET(mn, how, m->m_type);
384 if (mn == (struct mbuf *)NULL) {
385 m_freem(m);
386 return ((struct mbuf *)NULL);
387 }
388 if (m->m_flags & M_PKTHDR) {
389 M_COPY_PKTHDR(mn, m);
390 m->m_flags &= ~M_PKTHDR;
391 }
392 mn->m_next = m;
393 m = mn;
394 if (len < MHLEN)
395 MH_ALIGN(m, len);
396 m->m_len = len;
397 return (m);
398 }
399
400 /*
401 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
402 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
403 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
404 */
405 int MCFail;
406
407 struct mbuf *
408 m_copym(m, off0, len, wait)
409 struct mbuf *m;
410 int off0, wait;
411 int len;
412 {
413 return m_copym0(m, off0, len, wait, 0); /* shallow copy on M_EXT */
414 }
415
416 struct mbuf *
417 m_dup(m, off0, len, wait)
418 struct mbuf *m;
419 int off0, wait;
420 int len;
421 {
422 return m_copym0(m, off0, len, wait, 1); /* deep copy */
423 }
424
425 static struct mbuf *
426 m_copym0(m, off0, len, wait, deep)
427 struct mbuf *m;
428 int off0, wait;
429 int len;
430 int deep; /* deep copy */
431 {
432 struct mbuf *n, **np;
433 int off = off0;
434 struct mbuf *top;
435 int copyhdr = 0;
436
437 if (off < 0 || len < 0)
438 panic("m_copym: off %d, len %d", off, len);
439 if (off == 0 && m->m_flags & M_PKTHDR)
440 copyhdr = 1;
441 while (off > 0) {
442 if (m == 0)
443 panic("m_copym: m == 0");
444 if (off < m->m_len)
445 break;
446 off -= m->m_len;
447 m = m->m_next;
448 }
449 np = ⊤
450 top = 0;
451 while (len > 0) {
452 if (m == 0) {
453 if (len != M_COPYALL)
454 panic("m_copym: m == 0 and not COPYALL");
455 break;
456 }
457 MGET(n, wait, m->m_type);
458 *np = n;
459 if (n == 0)
460 goto nospace;
461 if (copyhdr) {
462 M_COPY_PKTHDR(n, m);
463 if (len == M_COPYALL)
464 n->m_pkthdr.len -= off0;
465 else
466 n->m_pkthdr.len = len;
467 copyhdr = 0;
468 }
469 n->m_len = min(len, m->m_len - off);
470 if (m->m_flags & M_EXT) {
471 if (!deep) {
472 n->m_data = m->m_data + off;
473 n->m_ext = m->m_ext;
474 MCLADDREFERENCE(m, n);
475 } else {
476 /*
477 * we are unsure about the way m was allocated.
478 * copy into multiple MCLBYTES cluster mbufs.
479 */
480 MCLGET(n, wait);
481 n->m_len = 0;
482 n->m_len = M_TRAILINGSPACE(n);
483 n->m_len = min(n->m_len, len);
484 n->m_len = min(n->m_len, m->m_len - off);
485 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
486 (unsigned)n->m_len);
487 }
488 } else
489 memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
490 (unsigned)n->m_len);
491 if (len != M_COPYALL)
492 len -= n->m_len;
493 off += n->m_len;
494 #ifdef DIAGNOSTIC
495 if (off > m->m_len)
496 panic("m_copym0 overrun");
497 #endif
498 if (off == m->m_len) {
499 m = m->m_next;
500 off = 0;
501 }
502 np = &n->m_next;
503 }
504 if (top == 0)
505 MCFail++;
506 return (top);
507 nospace:
508 m_freem(top);
509 MCFail++;
510 return (0);
511 }
512
513 /*
514 * Copy an entire packet, including header (which must be present).
515 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
516 */
517 struct mbuf *
518 m_copypacket(m, how)
519 struct mbuf *m;
520 int how;
521 {
522 struct mbuf *top, *n, *o;
523
524 MGET(n, how, m->m_type);
525 top = n;
526 if (!n)
527 goto nospace;
528
529 M_COPY_PKTHDR(n, m);
530 n->m_len = m->m_len;
531 if (m->m_flags & M_EXT) {
532 n->m_data = m->m_data;
533 n->m_ext = m->m_ext;
534 MCLADDREFERENCE(m, n);
535 } else {
536 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
537 }
538
539 m = m->m_next;
540 while (m) {
541 MGET(o, how, m->m_type);
542 if (!o)
543 goto nospace;
544
545 n->m_next = o;
546 n = n->m_next;
547
548 n->m_len = m->m_len;
549 if (m->m_flags & M_EXT) {
550 n->m_data = m->m_data;
551 n->m_ext = m->m_ext;
552 MCLADDREFERENCE(m, n);
553 } else {
554 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
555 }
556
557 m = m->m_next;
558 }
559 return top;
560 nospace:
561 m_freem(top);
562 MCFail++;
563 return 0;
564 }
565
566 /*
567 * Copy data from an mbuf chain starting "off" bytes from the beginning,
568 * continuing for "len" bytes, into the indicated buffer.
569 */
570 void
571 m_copydata(m, off, len, cp)
572 struct mbuf *m;
573 int off;
574 int len;
575 caddr_t cp;
576 {
577 unsigned count;
578
579 if (off < 0 || len < 0)
580 panic("m_copydata");
581 while (off > 0) {
582 if (m == 0)
583 panic("m_copydata");
584 if (off < m->m_len)
585 break;
586 off -= m->m_len;
587 m = m->m_next;
588 }
589 while (len > 0) {
590 if (m == 0)
591 panic("m_copydata");
592 count = min(m->m_len - off, len);
593 memcpy(cp, mtod(m, caddr_t) + off, count);
594 len -= count;
595 cp += count;
596 off = 0;
597 m = m->m_next;
598 }
599 }
600
601 /*
602 * Concatenate mbuf chain n to m.
603 * Both chains must be of the same type (e.g. MT_DATA).
604 * Any m_pkthdr is not updated.
605 */
606 void
607 m_cat(m, n)
608 struct mbuf *m, *n;
609 {
610 while (m->m_next)
611 m = m->m_next;
612 while (n) {
613 if (m->m_flags & M_EXT ||
614 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
615 /* just join the two chains */
616 m->m_next = n;
617 return;
618 }
619 /* splat the data from one into the other */
620 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
621 (u_int)n->m_len);
622 m->m_len += n->m_len;
623 n = m_free(n);
624 }
625 }
626
627 void
628 m_adj(mp, req_len)
629 struct mbuf *mp;
630 int req_len;
631 {
632 int len = req_len;
633 struct mbuf *m;
634 int count;
635
636 if ((m = mp) == NULL)
637 return;
638 if (len >= 0) {
639 /*
640 * Trim from head.
641 */
642 while (m != NULL && len > 0) {
643 if (m->m_len <= len) {
644 len -= m->m_len;
645 m->m_len = 0;
646 m = m->m_next;
647 } else {
648 m->m_len -= len;
649 m->m_data += len;
650 len = 0;
651 }
652 }
653 m = mp;
654 if (mp->m_flags & M_PKTHDR)
655 m->m_pkthdr.len -= (req_len - len);
656 } else {
657 /*
658 * Trim from tail. Scan the mbuf chain,
659 * calculating its length and finding the last mbuf.
660 * If the adjustment only affects this mbuf, then just
661 * adjust and return. Otherwise, rescan and truncate
662 * after the remaining size.
663 */
664 len = -len;
665 count = 0;
666 for (;;) {
667 count += m->m_len;
668 if (m->m_next == (struct mbuf *)0)
669 break;
670 m = m->m_next;
671 }
672 if (m->m_len >= len) {
673 m->m_len -= len;
674 if (mp->m_flags & M_PKTHDR)
675 mp->m_pkthdr.len -= len;
676 return;
677 }
678 count -= len;
679 if (count < 0)
680 count = 0;
681 /*
682 * Correct length for chain is "count".
683 * Find the mbuf with last data, adjust its length,
684 * and toss data from remaining mbufs on chain.
685 */
686 m = mp;
687 if (m->m_flags & M_PKTHDR)
688 m->m_pkthdr.len = count;
689 for (; m; m = m->m_next) {
690 if (m->m_len >= count) {
691 m->m_len = count;
692 break;
693 }
694 count -= m->m_len;
695 }
696 while (m->m_next)
697 (m = m->m_next) ->m_len = 0;
698 }
699 }
700
701 /*
702 * Rearange an mbuf chain so that len bytes are contiguous
703 * and in the data area of an mbuf (so that mtod and dtom
704 * will work for a structure of size len). Returns the resulting
705 * mbuf chain on success, frees it and returns null on failure.
706 * If there is room, it will add up to max_protohdr-len extra bytes to the
707 * contiguous region in an attempt to avoid being called next time.
708 */
709 int MPFail;
710
711 struct mbuf *
712 m_pullup(n, len)
713 struct mbuf *n;
714 int len;
715 {
716 struct mbuf *m;
717 int count;
718 int space;
719
720 /*
721 * If first mbuf has no cluster, and has room for len bytes
722 * without shifting current data, pullup into it,
723 * otherwise allocate a new mbuf to prepend to the chain.
724 */
725 if ((n->m_flags & M_EXT) == 0 &&
726 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
727 if (n->m_len >= len)
728 return (n);
729 m = n;
730 n = n->m_next;
731 len -= m->m_len;
732 } else {
733 if (len > MHLEN)
734 goto bad;
735 MGET(m, M_DONTWAIT, n->m_type);
736 if (m == 0)
737 goto bad;
738 m->m_len = 0;
739 if (n->m_flags & M_PKTHDR) {
740 M_COPY_PKTHDR(m, n);
741 n->m_flags &= ~M_PKTHDR;
742 }
743 }
744 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
745 do {
746 count = min(min(max(len, max_protohdr), space), n->m_len);
747 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
748 (unsigned)count);
749 len -= count;
750 m->m_len += count;
751 n->m_len -= count;
752 space -= count;
753 if (n->m_len)
754 n->m_data += count;
755 else
756 n = m_free(n);
757 } while (len > 0 && n);
758 if (len > 0) {
759 (void) m_free(m);
760 goto bad;
761 }
762 m->m_next = n;
763 return (m);
764 bad:
765 m_freem(n);
766 MPFail++;
767 return (0);
768 }
769
770 /*
771 * Partition an mbuf chain in two pieces, returning the tail --
772 * all but the first len0 bytes. In case of failure, it returns NULL and
773 * attempts to restore the chain to its original state.
774 */
775 struct mbuf *
776 m_split(m0, len0, wait)
777 struct mbuf *m0;
778 int len0, wait;
779 {
780 struct mbuf *m, *n;
781 unsigned len = len0, remain, len_save;
782
783 for (m = m0; m && len > m->m_len; m = m->m_next)
784 len -= m->m_len;
785 if (m == 0)
786 return (0);
787 remain = m->m_len - len;
788 if (m0->m_flags & M_PKTHDR) {
789 MGETHDR(n, wait, m0->m_type);
790 if (n == 0)
791 return (0);
792 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
793 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
794 len_save = m0->m_pkthdr.len;
795 m0->m_pkthdr.len = len0;
796 if (m->m_flags & M_EXT)
797 goto extpacket;
798 if (remain > MHLEN) {
799 /* m can't be the lead packet */
800 MH_ALIGN(n, 0);
801 n->m_next = m_split(m, len, wait);
802 if (n->m_next == 0) {
803 (void) m_free(n);
804 m0->m_pkthdr.len = len_save;
805 return (0);
806 } else
807 return (n);
808 } else
809 MH_ALIGN(n, remain);
810 } else if (remain == 0) {
811 n = m->m_next;
812 m->m_next = 0;
813 return (n);
814 } else {
815 MGET(n, wait, m->m_type);
816 if (n == 0)
817 return (0);
818 M_ALIGN(n, remain);
819 }
820 extpacket:
821 if (m->m_flags & M_EXT) {
822 n->m_ext = m->m_ext;
823 MCLADDREFERENCE(m, n);
824 n->m_data = m->m_data + len;
825 } else {
826 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
827 }
828 n->m_len = remain;
829 m->m_len = len;
830 n->m_next = m->m_next;
831 m->m_next = 0;
832 return (n);
833 }
834 /*
835 * Routine to copy from device local memory into mbufs.
836 */
837 struct mbuf *
838 m_devget(buf, totlen, off0, ifp, copy)
839 char *buf;
840 int totlen, off0;
841 struct ifnet *ifp;
842 void (*copy) __P((const void *from, void *to, size_t len));
843 {
844 struct mbuf *m;
845 struct mbuf *top = 0, **mp = ⊤
846 int off = off0, len;
847 char *cp;
848 char *epkt;
849
850 cp = buf;
851 epkt = cp + totlen;
852 if (off) {
853 /*
854 * If 'off' is non-zero, packet is trailer-encapsulated,
855 * so we have to skip the type and length fields.
856 */
857 cp += off + 2 * sizeof(u_int16_t);
858 totlen -= 2 * sizeof(u_int16_t);
859 }
860 MGETHDR(m, M_DONTWAIT, MT_DATA);
861 if (m == 0)
862 return (0);
863 m->m_pkthdr.rcvif = ifp;
864 m->m_pkthdr.len = totlen;
865 m->m_len = MHLEN;
866
867 while (totlen > 0) {
868 if (top) {
869 MGET(m, M_DONTWAIT, MT_DATA);
870 if (m == 0) {
871 m_freem(top);
872 return (0);
873 }
874 m->m_len = MLEN;
875 }
876 len = min(totlen, epkt - cp);
877 if (len >= MINCLSIZE) {
878 MCLGET(m, M_DONTWAIT);
879 if ((m->m_flags & M_EXT) == 0) {
880 m_free(m);
881 m_freem(top);
882 return (0);
883 }
884 m->m_len = len = min(len, MCLBYTES);
885 } else {
886 /*
887 * Place initial small packet/header at end of mbuf.
888 */
889 if (len < m->m_len) {
890 if (top == 0 && len + max_linkhdr <= m->m_len)
891 m->m_data += max_linkhdr;
892 m->m_len = len;
893 } else
894 len = m->m_len;
895 }
896 if (copy)
897 copy(cp, mtod(m, caddr_t), (size_t)len);
898 else
899 memcpy(mtod(m, caddr_t), cp, (size_t)len);
900 cp += len;
901 *mp = m;
902 mp = &m->m_next;
903 totlen -= len;
904 if (cp == epkt)
905 cp = buf;
906 }
907 return (top);
908 }
909
910 /*
911 * Copy data from a buffer back into the indicated mbuf chain,
912 * starting "off" bytes from the beginning, extending the mbuf
913 * chain if necessary.
914 */
915 void
916 m_copyback(m0, off, len, cp)
917 struct mbuf *m0;
918 int off;
919 int len;
920 caddr_t cp;
921 {
922 int mlen;
923 struct mbuf *m = m0, *n;
924 int totlen = 0;
925
926 if (m0 == 0)
927 return;
928 while (off > (mlen = m->m_len)) {
929 off -= mlen;
930 totlen += mlen;
931 if (m->m_next == 0) {
932 n = m_getclr(M_DONTWAIT, m->m_type);
933 if (n == 0)
934 goto out;
935 n->m_len = min(MLEN, len + off);
936 m->m_next = n;
937 }
938 m = m->m_next;
939 }
940 while (len > 0) {
941 mlen = min (m->m_len - off, len);
942 memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
943 cp += mlen;
944 len -= mlen;
945 mlen += off;
946 off = 0;
947 totlen += mlen;
948 if (len == 0)
949 break;
950 if (m->m_next == 0) {
951 n = m_get(M_DONTWAIT, m->m_type);
952 if (n == 0)
953 break;
954 n->m_len = min(MLEN, len);
955 m->m_next = n;
956 }
957 m = m->m_next;
958 }
959 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
960 m->m_pkthdr.len = totlen;
961 }
962