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