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