1 /* 2 * Copyright (c) 1982, 1986, 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 34 * $Id$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/proc.h> 40 #include <sys/malloc.h> 41 #define MBTYPES 42 #include <sys/mbuf.h> 43 #include <sys/kernel.h> 44 #include <sys/syslog.h> 45 #include <sys/domain.h> 46 #include <sys/protosw.h> 47 48 #include <vm/vm.h> 49 #include <vm/vm_param.h> 50 #include <vm/vm_kern.h> 51 #include <vm/vm_extern.h> 52 53 static void mbinit __P((void *)); 54 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbinit, NULL) 55 56 struct mbuf *mbutl; 57 char *mclrefcnt; 58 struct mbstat mbstat; 59 struct mbuf *mmbfree; 60 union mcluster *mclfree; 61 int max_linkhdr; 62 int max_protohdr; 63 int max_hdr; 64 int max_datalen; 65 66 static void m_reclaim __P((void)); 67 68 /* "number of clusters of pages" */ 69 #define NCL_INIT 1 70 71 #define NMB_INIT 16 72 73 /* ARGSUSED*/ 74 static void 75 mbinit(dummy) 76 void *dummy; 77 { 78 int s; 79 80 mmbfree = NULL; mclfree = NULL; 81 s = splimp(); 82 if (m_mballoc(NMB_INIT, M_DONTWAIT) == 0) 83 goto bad; 84 #if MCLBYTES <= PAGE_SIZE 85 if (m_clalloc(NCL_INIT, M_DONTWAIT) == 0) 86 goto bad; 87 #else 88 /* It's OK to call contigmalloc in this context. */ 89 if (m_clalloc(16, 0) == 0) 90 goto bad; 91 #endif 92 splx(s); 93 return; 94 bad: 95 panic("mbinit"); 96 } 97 98 /* 99 * Allocate at least nmb mbufs and place on mbuf free list. 100 * Must be called at splimp. 101 */ 102 /* ARGSUSED */ 103 int 104 m_mballoc(nmb, nowait) 105 register int nmb; 106 int nowait; 107 { 108 register caddr_t p; 109 register int i; 110 int nbytes; 111 112 /* Once we run out of map space, it will be impossible to get 113 * any more (nothing is ever freed back to the map) (XXX which 114 * is dumb). (however you are not dead as m_reclaim might 115 * still be able to free a substantial amount of space). 116 */ 117 if (mb_map_full) 118 return (0); 119 120 nbytes = round_page(nmb * MSIZE); 121 p = (caddr_t)kmem_malloc(mb_map, nbytes, M_NOWAIT); 122 if (p == 0 && !nowait) { 123 mbstat.m_wait++; 124 p = (caddr_t)kmem_malloc(mb_map, nbytes, M_WAITOK); 125 } 126 127 /* 128 * Either the map is now full, or this is nowait and there 129 * are no pages left. 130 */ 131 if (p == NULL) 132 return (0); 133 134 nmb = nbytes / MSIZE; 135 for (i = 0; i < nmb; i++) { 136 ((struct mbuf *)p)->m_next = mmbfree; 137 mmbfree = (struct mbuf *)p; 138 p += MSIZE; 139 } 140 mbstat.m_mbufs += nmb; 141 return (1); 142 } 143 144 #if MCLBYTES > PAGE_SIZE 145 static int i_want_my_mcl; 146 147 static void 148 kproc_mclalloc(void) 149 { 150 int status; 151 152 while (1) { 153 tsleep(&i_want_my_mcl, PVM, "mclalloc", 0); 154 155 for (; i_want_my_mcl; i_want_my_mcl--) { 156 if (m_clalloc(1, 0) == 0) 157 printf("m_clalloc failed even in process context!\n"); 158 } 159 } 160 } 161 162 static struct proc *mclallocproc; 163 static struct kproc_desc mclalloc_kp = { 164 "mclalloc", 165 kproc_mclalloc, 166 &mclallocproc 167 }; 168 SYSINIT_KT(mclallocproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start, 169 &mclalloc_kp); 170 #endif 171 172 /* 173 * Allocate some number of mbuf clusters 174 * and place on cluster free list. 175 * Must be called at splimp. 176 */ 177 /* ARGSUSED */ 178 int 179 m_clalloc(ncl, nowait) 180 register int ncl; 181 int nowait; 182 { 183 register caddr_t p; 184 register int i; 185 int npg; 186 187 /* 188 * Once we run out of map space, it will be impossible 189 * to get any more (nothing is ever freed back to the 190 * map). 191 */ 192 if (mb_map_full) { 193 mbstat.m_drops++; 194 return (0); 195 } 196 197 #if MCLBYTES > PAGE_SIZE 198 if (nowait) { 199 i_want_my_mcl += ncl; 200 wakeup(&i_want_my_mcl); 201 mbstat.m_wait++; 202 p = 0; 203 } else { 204 p = contigmalloc1(MCLBYTES * ncl, M_DEVBUF, M_WAITOK, 0ul, 205 ~0ul, PAGE_SIZE, 0, mb_map); 206 } 207 #else 208 npg = ncl; 209 p = (caddr_t)kmem_malloc(mb_map, ctob(npg), 210 nowait ? M_NOWAIT : M_WAITOK); 211 ncl = ncl * PAGE_SIZE / MCLBYTES; 212 #endif 213 /* 214 * Either the map is now full, or this is nowait and there 215 * are no pages left. 216 */ 217 if (p == NULL) { 218 mbstat.m_drops++; 219 return (0); 220 } 221 222 for (i = 0; i < ncl; i++) { 223 ((union mcluster *)p)->mcl_next = mclfree; 224 mclfree = (union mcluster *)p; 225 p += MCLBYTES; 226 mbstat.m_clfree++; 227 } 228 mbstat.m_clusters += ncl; 229 return (1); 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 register struct mbuf *m; 241 242 m_reclaim(); 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 register struct mbuf *m; 261 262 m_reclaim(); 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 static void 274 m_reclaim() 275 { 276 register struct domain *dp; 277 register struct protosw *pr; 278 int s = splimp(); 279 280 for (dp = domains; dp; dp = dp->dom_next) 281 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) 282 if (pr->pr_drain) 283 (*pr->pr_drain)(); 284 splx(s); 285 mbstat.m_drain++; 286 } 287 288 /* 289 * Space allocation routines. 290 * These are also available as macros 291 * for critical paths. 292 */ 293 struct mbuf * 294 m_get(nowait, type) 295 int nowait, type; 296 { 297 register struct mbuf *m; 298 299 MGET(m, nowait, type); 300 return (m); 301 } 302 303 struct mbuf * 304 m_gethdr(nowait, type) 305 int nowait, type; 306 { 307 register struct mbuf *m; 308 309 MGETHDR(m, nowait, type); 310 return (m); 311 } 312 313 struct mbuf * 314 m_getclr(nowait, type) 315 int nowait, type; 316 { 317 register struct mbuf *m; 318 319 MGET(m, nowait, type); 320 if (m == 0) 321 return (0); 322 bzero(mtod(m, caddr_t), MLEN); 323 return (m); 324 } 325 326 struct mbuf * 327 m_free(m) 328 struct mbuf *m; 329 { 330 register struct mbuf *n; 331 332 MFREE(m, n); 333 return (n); 334 } 335 336 void 337 m_freem(m) 338 register struct mbuf *m; 339 { 340 register struct mbuf *n; 341 342 if (m == NULL) 343 return; 344 do { 345 MFREE(m, n); 346 m = n; 347 } while (m); 348 } 349 350 /* 351 * Mbuffer utility routines. 352 */ 353 354 /* 355 * Lesser-used path for M_PREPEND: 356 * allocate new mbuf to prepend to chain, 357 * copy junk along. 358 */ 359 struct mbuf * 360 m_prepend(m, len, how) 361 register struct mbuf *m; 362 int len, how; 363 { 364 struct mbuf *mn; 365 366 MGET(mn, how, m->m_type); 367 if (mn == (struct mbuf *)NULL) { 368 m_freem(m); 369 return ((struct mbuf *)NULL); 370 } 371 if (m->m_flags & M_PKTHDR) { 372 M_COPY_PKTHDR(mn, m); 373 m->m_flags &= ~M_PKTHDR; 374 } 375 mn->m_next = m; 376 m = mn; 377 if (len < MHLEN) 378 MH_ALIGN(m, len); 379 m->m_len = len; 380 return (m); 381 } 382 383 /* 384 * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 385 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 386 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller. 387 */ 388 static int MCFail; 389 390 struct mbuf * 391 m_copym(m, off0, len, wait) 392 register struct mbuf *m; 393 int off0, wait; 394 register int len; 395 { 396 register struct mbuf *n, **np; 397 register int off = off0; 398 struct mbuf *top; 399 int copyhdr = 0; 400 401 if (off < 0 || len < 0) 402 panic("m_copym"); 403 if (off == 0 && m->m_flags & M_PKTHDR) 404 copyhdr = 1; 405 while (off > 0) { 406 if (m == 0) 407 panic("m_copym"); 408 if (off < m->m_len) 409 break; 410 off -= m->m_len; 411 m = m->m_next; 412 } 413 np = ⊤ 414 top = 0; 415 while (len > 0) { 416 if (m == 0) { 417 if (len != M_COPYALL) 418 panic("m_copym"); 419 break; 420 } 421 MGET(n, wait, m->m_type); 422 *np = n; 423 if (n == 0) 424 goto nospace; 425 if (copyhdr) { 426 M_COPY_PKTHDR(n, m); 427 if (len == M_COPYALL) 428 n->m_pkthdr.len -= off0; 429 else 430 n->m_pkthdr.len = len; 431 copyhdr = 0; 432 } 433 n->m_len = min(len, m->m_len - off); 434 if (m->m_flags & M_EXT) { 435 n->m_data = m->m_data + off; 436 if(!m->m_ext.ext_ref) 437 mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 438 else 439 (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 440 m->m_ext.ext_size); 441 n->m_ext = m->m_ext; 442 n->m_flags |= M_EXT; 443 } else 444 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 445 (unsigned)n->m_len); 446 if (len != M_COPYALL) 447 len -= n->m_len; 448 off = 0; 449 m = m->m_next; 450 np = &n->m_next; 451 } 452 if (top == 0) 453 MCFail++; 454 return (top); 455 nospace: 456 m_freem(top); 457 MCFail++; 458 return (0); 459 } 460 461 /* 462 * Copy an entire packet, including header (which must be present). 463 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 464 */ 465 struct mbuf * 466 m_copypacket(m, how) 467 struct mbuf *m; 468 int how; 469 { 470 struct mbuf *top, *n, *o; 471 472 MGET(n, how, m->m_type); 473 top = n; 474 if (!n) 475 goto nospace; 476 477 M_COPY_PKTHDR(n, m); 478 n->m_len = m->m_len; 479 if (m->m_flags & M_EXT) { 480 n->m_data = m->m_data; 481 mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 482 n->m_ext = m->m_ext; 483 n->m_flags |= M_EXT; 484 } else { 485 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 486 } 487 488 m = m->m_next; 489 while (m) { 490 MGET(o, how, m->m_type); 491 if (!o) 492 goto nospace; 493 494 n->m_next = o; 495 n = n->m_next; 496 497 n->m_len = m->m_len; 498 if (m->m_flags & M_EXT) { 499 n->m_data = m->m_data; 500 mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 501 n->m_ext = m->m_ext; 502 n->m_flags |= M_EXT; 503 } else { 504 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 505 } 506 507 m = m->m_next; 508 } 509 return top; 510 nospace: 511 m_freem(top); 512 MCFail++; 513 return 0; 514 } 515 516 /* 517 * Copy data from an mbuf chain starting "off" bytes from the beginning, 518 * continuing for "len" bytes, into the indicated buffer. 519 */ 520 void 521 m_copydata(m, off, len, cp) 522 register struct mbuf *m; 523 register int off; 524 register int len; 525 caddr_t cp; 526 { 527 register unsigned count; 528 529 if (off < 0 || len < 0) 530 panic("m_copydata"); 531 while (off > 0) { 532 if (m == 0) 533 panic("m_copydata"); 534 if (off < m->m_len) 535 break; 536 off -= m->m_len; 537 m = m->m_next; 538 } 539 while (len > 0) { 540 if (m == 0) 541 panic("m_copydata"); 542 count = min(m->m_len - off, len); 543 bcopy(mtod(m, caddr_t) + off, cp, count); 544 len -= count; 545 cp += count; 546 off = 0; 547 m = m->m_next; 548 } 549 } 550 551 /* 552 * Concatenate mbuf chain n to m. 553 * Both chains must be of the same type (e.g. MT_DATA). 554 * Any m_pkthdr is not updated. 555 */ 556 void 557 m_cat(m, n) 558 register struct mbuf *m, *n; 559 { 560 while (m->m_next) 561 m = m->m_next; 562 while (n) { 563 if (m->m_flags & M_EXT || 564 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 565 /* just join the two chains */ 566 m->m_next = n; 567 return; 568 } 569 /* splat the data from one into the other */ 570 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 571 (u_int)n->m_len); 572 m->m_len += n->m_len; 573 n = m_free(n); 574 } 575 } 576 577 void 578 m_adj(mp, req_len) 579 struct mbuf *mp; 580 int req_len; 581 { 582 register int len = req_len; 583 register struct mbuf *m; 584 register count; 585 586 if ((m = mp) == NULL) 587 return; 588 if (len >= 0) { 589 /* 590 * Trim from head. 591 */ 592 while (m != NULL && len > 0) { 593 if (m->m_len <= len) { 594 len -= m->m_len; 595 m->m_len = 0; 596 m = m->m_next; 597 } else { 598 m->m_len -= len; 599 m->m_data += len; 600 len = 0; 601 } 602 } 603 m = mp; 604 if (mp->m_flags & M_PKTHDR) 605 m->m_pkthdr.len -= (req_len - len); 606 } else { 607 /* 608 * Trim from tail. Scan the mbuf chain, 609 * calculating its length and finding the last mbuf. 610 * If the adjustment only affects this mbuf, then just 611 * adjust and return. Otherwise, rescan and truncate 612 * after the remaining size. 613 */ 614 len = -len; 615 count = 0; 616 for (;;) { 617 count += m->m_len; 618 if (m->m_next == (struct mbuf *)0) 619 break; 620 m = m->m_next; 621 } 622 if (m->m_len >= len) { 623 m->m_len -= len; 624 if (mp->m_flags & M_PKTHDR) 625 mp->m_pkthdr.len -= len; 626 return; 627 } 628 count -= len; 629 if (count < 0) 630 count = 0; 631 /* 632 * Correct length for chain is "count". 633 * Find the mbuf with last data, adjust its length, 634 * and toss data from remaining mbufs on chain. 635 */ 636 m = mp; 637 if (m->m_flags & M_PKTHDR) 638 m->m_pkthdr.len = count; 639 for (; m; m = m->m_next) { 640 if (m->m_len >= count) { 641 m->m_len = count; 642 break; 643 } 644 count -= m->m_len; 645 } 646 while (m->m_next) 647 (m = m->m_next) ->m_len = 0; 648 } 649 } 650 651 /* 652 * Rearange an mbuf chain so that len bytes are contiguous 653 * and in the data area of an mbuf (so that mtod and dtom 654 * will work for a structure of size len). Returns the resulting 655 * mbuf chain on success, frees it and returns null on failure. 656 * If there is room, it will add up to max_protohdr-len extra bytes to the 657 * contiguous region in an attempt to avoid being called next time. 658 */ 659 static int MPFail; 660 661 struct mbuf * 662 m_pullup(n, len) 663 register struct mbuf *n; 664 int len; 665 { 666 register struct mbuf *m; 667 register int count; 668 int space; 669 670 /* 671 * If first mbuf has no cluster, and has room for len bytes 672 * without shifting current data, pullup into it, 673 * otherwise allocate a new mbuf to prepend to the chain. 674 */ 675 if ((n->m_flags & M_EXT) == 0 && 676 n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 677 if (n->m_len >= len) 678 return (n); 679 m = n; 680 n = n->m_next; 681 len -= m->m_len; 682 } else { 683 if (len > MHLEN) 684 goto bad; 685 MGET(m, M_DONTWAIT, n->m_type); 686 if (m == 0) 687 goto bad; 688 m->m_len = 0; 689 if (n->m_flags & M_PKTHDR) { 690 M_COPY_PKTHDR(m, n); 691 n->m_flags &= ~M_PKTHDR; 692 } 693 } 694 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 695 do { 696 count = min(min(max(len, max_protohdr), space), n->m_len); 697 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 698 (unsigned)count); 699 len -= count; 700 m->m_len += count; 701 n->m_len -= count; 702 space -= count; 703 if (n->m_len) 704 n->m_data += count; 705 else 706 n = m_free(n); 707 } while (len > 0 && n); 708 if (len > 0) { 709 (void) m_free(m); 710 goto bad; 711 } 712 m->m_next = n; 713 return (m); 714 bad: 715 m_freem(n); 716 MPFail++; 717 return (0); 718 } 719 720 /* 721 * Partition an mbuf chain in two pieces, returning the tail -- 722 * all but the first len0 bytes. In case of failure, it returns NULL and 723 * attempts to restore the chain to its original state. 724 */ 725 struct mbuf * 726 m_split(m0, len0, wait) 727 register struct mbuf *m0; 728 int len0, wait; 729 { 730 register struct mbuf *m, *n; 731 unsigned len = len0, remain; 732 733 for (m = m0; m && len > m->m_len; m = m->m_next) 734 len -= m->m_len; 735 if (m == 0) 736 return (0); 737 remain = m->m_len - len; 738 if (m0->m_flags & M_PKTHDR) { 739 MGETHDR(n, wait, m0->m_type); 740 if (n == 0) 741 return (0); 742 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 743 n->m_pkthdr.len = m0->m_pkthdr.len - len0; 744 m0->m_pkthdr.len = len0; 745 if (m->m_flags & M_EXT) 746 goto extpacket; 747 if (remain > MHLEN) { 748 /* m can't be the lead packet */ 749 MH_ALIGN(n, 0); 750 n->m_next = m_split(m, len, wait); 751 if (n->m_next == 0) { 752 (void) m_free(n); 753 return (0); 754 } else 755 return (n); 756 } else 757 MH_ALIGN(n, remain); 758 } else if (remain == 0) { 759 n = m->m_next; 760 m->m_next = 0; 761 return (n); 762 } else { 763 MGET(n, wait, m->m_type); 764 if (n == 0) 765 return (0); 766 M_ALIGN(n, remain); 767 } 768 extpacket: 769 if (m->m_flags & M_EXT) { 770 n->m_flags |= M_EXT; 771 n->m_ext = m->m_ext; 772 if(!m->m_ext.ext_ref) 773 mclrefcnt[mtocl(m->m_ext.ext_buf)]++; 774 else 775 (*(m->m_ext.ext_ref))(m->m_ext.ext_buf, 776 m->m_ext.ext_size); 777 m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */ 778 n->m_data = m->m_data + len; 779 } else { 780 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 781 } 782 n->m_len = remain; 783 m->m_len = len; 784 n->m_next = m->m_next; 785 m->m_next = 0; 786 return (n); 787 } 788 /* 789 * Routine to copy from device local memory into mbufs. 790 */ 791 struct mbuf * 792 m_devget(buf, totlen, off0, ifp, copy) 793 char *buf; 794 int totlen, off0; 795 struct ifnet *ifp; 796 void (*copy) __P((char *from, caddr_t to, u_int len)); 797 { 798 register struct mbuf *m; 799 struct mbuf *top = 0, **mp = ⊤ 800 register int off = off0, len; 801 register char *cp; 802 char *epkt; 803 804 cp = buf; 805 epkt = cp + totlen; 806 if (off) { 807 cp += off + 2 * sizeof(u_short); 808 totlen -= 2 * sizeof(u_short); 809 } 810 MGETHDR(m, M_DONTWAIT, MT_DATA); 811 if (m == 0) 812 return (0); 813 m->m_pkthdr.rcvif = ifp; 814 m->m_pkthdr.len = totlen; 815 m->m_len = MHLEN; 816 817 while (totlen > 0) { 818 if (top) { 819 MGET(m, M_DONTWAIT, MT_DATA); 820 if (m == 0) { 821 m_freem(top); 822 return (0); 823 } 824 m->m_len = MLEN; 825 } 826 len = min(totlen, epkt - cp); 827 if (len >= MINCLSIZE) { 828 MCLGET(m, M_DONTWAIT); 829 if (m->m_flags & M_EXT) 830 m->m_len = len = min(len, MCLBYTES); 831 else 832 len = m->m_len; 833 } else { 834 /* 835 * Place initial small packet/header at end of mbuf. 836 */ 837 if (len < m->m_len) { 838 if (top == 0 && len + max_linkhdr <= m->m_len) 839 m->m_data += max_linkhdr; 840 m->m_len = len; 841 } else 842 len = m->m_len; 843 } 844 if (copy) 845 copy(cp, mtod(m, caddr_t), (unsigned)len); 846 else 847 bcopy(cp, mtod(m, caddr_t), (unsigned)len); 848 cp += len; 849 *mp = m; 850 mp = &m->m_next; 851 totlen -= len; 852 if (cp == epkt) 853 cp = buf; 854 } 855 return (top); 856 } 857 858 /* 859 * Copy data from a buffer back into the indicated mbuf chain, 860 * starting "off" bytes from the beginning, extending the mbuf 861 * chain if necessary. 862 */ 863 void 864 m_copyback(m0, off, len, cp) 865 struct mbuf *m0; 866 register int off; 867 register int len; 868 caddr_t cp; 869 { 870 register int mlen; 871 register struct mbuf *m = m0, *n; 872 int totlen = 0; 873 874 if (m0 == 0) 875 return; 876 while (off > (mlen = m->m_len)) { 877 off -= mlen; 878 totlen += mlen; 879 if (m->m_next == 0) { 880 n = m_getclr(M_DONTWAIT, m->m_type); 881 if (n == 0) 882 goto out; 883 n->m_len = min(MLEN, len + off); 884 m->m_next = n; 885 } 886 m = m->m_next; 887 } 888 while (len > 0) { 889 mlen = min (m->m_len - off, len); 890 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); 891 cp += mlen; 892 len -= mlen; 893 mlen += off; 894 off = 0; 895 totlen += mlen; 896 if (len == 0) 897 break; 898 if (m->m_next == 0) { 899 n = m_get(M_DONTWAIT, m->m_type); 900 if (n == 0) 901 break; 902 n->m_len = min(MLEN, len); 903 m->m_next = n; 904 } 905 m = m->m_next; 906 } 907 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 908 m->m_pkthdr.len = totlen; 909 } 910