uipc_mbuf.c revision 149599
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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/kern/uipc_mbuf.c 149599 2005-08-29 19:58:56Z andre $"); 34 35#include "opt_mac.h" 36#include "opt_param.h" 37#include "opt_mbuf_stress_test.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/limits.h> 43#include <sys/lock.h> 44#include <sys/mac.h> 45#include <sys/malloc.h> 46#include <sys/mbuf.h> 47#include <sys/sysctl.h> 48#include <sys/domain.h> 49#include <sys/protosw.h> 50#include <sys/uio.h> 51 52int max_linkhdr; 53int max_protohdr; 54int max_hdr; 55int max_datalen; 56#ifdef MBUF_STRESS_TEST 57int m_defragpackets; 58int m_defragbytes; 59int m_defraguseless; 60int m_defragfailure; 61int m_defragrandomfailures; 62#endif 63 64/* 65 * sysctl(8) exported objects 66 */ 67SYSCTL_DECL(_kern_ipc); 68SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 69 &max_linkhdr, 0, ""); 70SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 71 &max_protohdr, 0, ""); 72SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 73SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 74 &max_datalen, 0, ""); 75#ifdef MBUF_STRESS_TEST 76SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD, 77 &m_defragpackets, 0, ""); 78SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD, 79 &m_defragbytes, 0, ""); 80SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD, 81 &m_defraguseless, 0, ""); 82SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD, 83 &m_defragfailure, 0, ""); 84SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW, 85 &m_defragrandomfailures, 0, ""); 86#endif 87 88/* 89 * Malloc-type for external ext_buf ref counts. 90 */ 91static MALLOC_DEFINE(M_MBUF, "mbextcnt", "mbuf external ref counts"); 92 93/* 94 * Allocate a given length worth of mbufs and/or clusters (whatever fits 95 * best) and return a pointer to the top of the allocated chain. If an 96 * existing mbuf chain is provided, then we will append the new chain 97 * to the existing one but still return the top of the newly allocated 98 * chain. 99 */ 100struct mbuf * 101m_getm(struct mbuf *m, int len, int how, short type) 102{ 103 struct mbuf *mb, *top, *cur, *mtail; 104 int num, rem; 105 int i; 106 107 KASSERT(len >= 0, ("m_getm(): len is < 0")); 108 109 /* If m != NULL, we will append to the end of that chain. */ 110 if (m != NULL) 111 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next); 112 else 113 mtail = NULL; 114 115 /* 116 * Calculate how many mbufs+clusters ("packets") we need and how much 117 * leftover there is after that and allocate the first mbuf+cluster 118 * if required. 119 */ 120 num = len / MCLBYTES; 121 rem = len % MCLBYTES; 122 top = cur = NULL; 123 if (num > 0) { 124 if ((top = cur = m_getcl(how, type, 0)) == NULL) 125 goto failed; 126 top->m_len = 0; 127 } 128 num--; 129 130 for (i = 0; i < num; i++) { 131 mb = m_getcl(how, type, 0); 132 if (mb == NULL) 133 goto failed; 134 mb->m_len = 0; 135 cur = (cur->m_next = mb); 136 } 137 if (rem > 0) { 138 mb = (rem > MINCLSIZE) ? 139 m_getcl(how, type, 0) : m_get(how, type); 140 if (mb == NULL) 141 goto failed; 142 mb->m_len = 0; 143 if (cur == NULL) 144 top = mb; 145 else 146 cur->m_next = mb; 147 } 148 149 if (mtail != NULL) 150 mtail->m_next = top; 151 return top; 152failed: 153 if (top != NULL) 154 m_freem(top); 155 return NULL; 156} 157 158/* 159 * Free an entire chain of mbufs and associated external buffers, if 160 * applicable. 161 */ 162void 163m_freem(struct mbuf *mb) 164{ 165 166 while (mb != NULL) 167 mb = m_free(mb); 168} 169 170/*- 171 * Configure a provided mbuf to refer to the provided external storage 172 * buffer and setup a reference count for said buffer. If the setting 173 * up of the reference count fails, the M_EXT bit will not be set. If 174 * successfull, the M_EXT bit is set in the mbuf's flags. 175 * 176 * Arguments: 177 * mb The existing mbuf to which to attach the provided buffer. 178 * buf The address of the provided external storage buffer. 179 * size The size of the provided buffer. 180 * freef A pointer to a routine that is responsible for freeing the 181 * provided external storage buffer. 182 * args A pointer to an argument structure (of any type) to be passed 183 * to the provided freef routine (may be NULL). 184 * flags Any other flags to be passed to the provided mbuf. 185 * type The type that the external storage buffer should be 186 * labeled with. 187 * 188 * Returns: 189 * Nothing. 190 */ 191void 192m_extadd(struct mbuf *mb, caddr_t buf, u_int size, 193 void (*freef)(void *, void *), void *args, int flags, int type) 194{ 195 u_int *ref_cnt = NULL; 196 197 /* XXX Shouldn't be adding EXT_CLUSTER with this API */ 198 if (type == EXT_CLUSTER) 199 ref_cnt = (u_int *)uma_find_refcnt(zone_clust, 200 mb->m_ext.ext_buf); 201 else if (type == EXT_EXTREF) 202 ref_cnt = __DEVOLATILE(u_int *, mb->m_ext.ref_cnt); 203 mb->m_ext.ref_cnt = (ref_cnt == NULL) ? 204 malloc(sizeof(u_int), M_MBUF, M_NOWAIT) : (u_int *)ref_cnt; 205 if (mb->m_ext.ref_cnt != NULL) { 206 *(mb->m_ext.ref_cnt) = 1; 207 mb->m_flags |= (M_EXT | flags); 208 mb->m_ext.ext_buf = buf; 209 mb->m_data = mb->m_ext.ext_buf; 210 mb->m_ext.ext_size = size; 211 mb->m_ext.ext_free = freef; 212 mb->m_ext.ext_args = args; 213 mb->m_ext.ext_type = type; 214 } 215} 216 217/* 218 * Non-directly-exported function to clean up after mbufs with M_EXT 219 * storage attached to them if the reference count hits 0. 220 */ 221void 222mb_free_ext(struct mbuf *m) 223{ 224 u_int cnt; 225 int dofree; 226 227 /* Account for lazy ref count assign. */ 228 if (m->m_ext.ref_cnt == NULL) 229 dofree = 1; 230 else 231 dofree = 0; 232 233 /* 234 * This is tricky. We need to make sure to decrement the 235 * refcount in a safe way but to also clean up if we're the 236 * last reference. This method seems to do it without race. 237 */ 238 while (dofree == 0) { 239 cnt = *(m->m_ext.ref_cnt); 240 if (atomic_cmpset_int(m->m_ext.ref_cnt, cnt, cnt - 1)) { 241 if (cnt == 1) 242 dofree = 1; 243 break; 244 } 245 } 246 247 if (dofree) { 248 /* 249 * Do the free, should be safe. 250 */ 251 if (m->m_ext.ext_type == EXT_PACKET) { 252 uma_zfree(zone_pack, m); 253 return; 254 } else if (m->m_ext.ext_type == EXT_CLUSTER) { 255 uma_zfree(zone_clust, m->m_ext.ext_buf); 256 m->m_ext.ext_buf = NULL; 257 } else { 258 (*(m->m_ext.ext_free))(m->m_ext.ext_buf, 259 m->m_ext.ext_args); 260 if (m->m_ext.ext_type != EXT_EXTREF) { 261 if (m->m_ext.ref_cnt != NULL) 262 free(__DEVOLATILE(u_int *, 263 m->m_ext.ref_cnt), M_MBUF); 264 m->m_ext.ref_cnt = NULL; 265 } 266 m->m_ext.ext_buf = NULL; 267 } 268 } 269 uma_zfree(zone_mbuf, m); 270} 271 272/* 273 * Clean up mbuf (chain) from any tags and packet headers. 274 * If "all" is set then the first mbuf in the chain will be 275 * cleaned too. 276 */ 277void 278m_demote(struct mbuf *m0, int all) 279{ 280 struct mbuf *m; 281 282 for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) { 283 if (m->m_flags & M_PKTHDR) { 284 m_tag_delete_chain(m, NULL); 285 m->m_flags &= ~M_PKTHDR; 286 bzero(&m->m_pkthdr, sizeof(struct pkthdr)); 287 } 288 if (m->m_type & MT_HEADER) 289 m->m_type = MT_DATA; 290 if (m != m0 && m->m_nextpkt) 291 m->m_nextpkt = NULL; 292 m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST); 293 } 294} 295 296/* 297 * Sanity checks on mbuf (chain). 298 * Returns 0 bad, 1 good, panic worse. 299 * sanitize, 0 run M_SANITY_ACTION, 1 garble things so they blow up later. 300 */ 301int 302m_sanity(struct mbuf *m0, int sanitize) 303{ 304 struct mbuf *m; 305 caddr_t a, b; 306 int pktlen = 0; 307 308#define M_SANITY_ACTION(s) return (0) 309/* #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m) */ 310 311 m = m0; 312 while (m) { 313 /* 314 * Basic pointer checks. If any of these fails then some 315 * unrelated kernel memory before or after us is trashed. 316 * No way to recover from that. 317 */ 318 a = (m->m_flags & M_EXT ? m->m_ext.ext_buf : 319 (m->m_flags & M_PKTHDR ? (caddr_t)(&m->m_pktdat) : 320 (caddr_t)(&m->m_dat)) ); 321 b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size : 322 (m->m_flags & M_PKTHDR ? MHLEN : MLEN))); 323 if ((caddr_t)m->m_data < a) 324 M_SANITY_ACTION("m_data outside mbuf data range left"); 325 if ((caddr_t)m->m_data > b) 326 M_SANITY_ACTION("m_data outside mbuf data range right"); 327 if ((caddr_t)m->m_data + m->m_len > b) 328 M_SANITY_ACTION("m_data + m_len exeeds mbuf space"); 329 if (m->m_flags & M_PKTHDR && m->m_pkthdr.header) { 330 if ((caddr_t)m->m_pkthdr.header < a || 331 (caddr_t)m->m_pkthdr.header > b) 332 M_SANITY_ACTION("m_pkthdr.header outside mbuf data range"); 333 } 334 335 /* m->m_nextpkt may only be set on first mbuf in chain. */ 336 if (m != m0 && m->m_nextpkt) { 337 if (sanitize) { 338 m_freem(m->m_nextpkt); 339 m->m_nextpkt = (struct mbuf *)0xDEADC0DE; 340 } else 341 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf"); 342 } 343 344 /* correct type correlations. */ 345 if (m->m_type == MT_HEADER && !(m->m_flags & M_PKTHDR)) { 346 if (sanitize) 347 m->m_type = MT_DATA; 348 else 349 M_SANITY_ACTION("MT_HEADER set but not M_PKTHDR"); 350 } 351 352 /* packet length (not mbuf length!) calculation */ 353 if (m0->m_flags & M_PKTHDR) 354 pktlen += m->m_len; 355 356 /* m_tags may only be attached to first mbuf in chain. */ 357 if (m != m0 && m->m_flags & M_PKTHDR && 358 !SLIST_EMPTY(&m->m_pkthdr.tags)) { 359 if (sanitize) { 360 m_tag_delete_chain(m, NULL); 361 /* put in 0xDEADC0DE perhaps? */ 362 } 363 else 364 M_SANITY_ACTION("m_tags on in-chain mbuf"); 365 } 366 367 /* M_PKTHDR may only be set on first mbuf in chain */ 368 if (m != m0 && m->m_flags & M_PKTHDR) { 369 if (sanitize) { 370 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr)); 371 m->m_flags &= ~M_PKTHDR; 372 /* put in 0xDEADCODE and leave hdr flag in */ 373 } else 374 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf"); 375 } 376 377 m = m->m_next; 378 } 379 if (pktlen && pktlen != m0->m_pkthdr.len) { 380 if (sanitize) 381 m0->m_pkthdr.len = 0; 382 else 383 M_SANITY_ACTION("m_pkthdr.len != mbuf chain length"); 384 } 385#undef M_SANITY_ACTION 386 387 return 1; 388} 389 390 391/* 392 * "Move" mbuf pkthdr from "from" to "to". 393 * "from" must have M_PKTHDR set, and "to" must be empty. 394 */ 395void 396m_move_pkthdr(struct mbuf *to, struct mbuf *from) 397{ 398 399#if 0 400 /* see below for why these are not enabled */ 401 M_ASSERTPKTHDR(to); 402 /* Note: with MAC, this may not be a good assertion. */ 403 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 404 ("m_move_pkthdr: to has tags")); 405#endif 406#ifdef MAC 407 /* 408 * XXXMAC: It could be this should also occur for non-MAC? 409 */ 410 if (to->m_flags & M_PKTHDR) 411 m_tag_delete_chain(to, NULL); 412#endif 413 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 414 if ((to->m_flags & M_EXT) == 0) 415 to->m_data = to->m_pktdat; 416 to->m_pkthdr = from->m_pkthdr; /* especially tags */ 417 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 418 from->m_flags &= ~M_PKTHDR; 419} 420 421/* 422 * Duplicate "from"'s mbuf pkthdr in "to". 423 * "from" must have M_PKTHDR set, and "to" must be empty. 424 * In particular, this does a deep copy of the packet tags. 425 */ 426int 427m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 428{ 429 430#if 0 431 /* 432 * The mbuf allocator only initializes the pkthdr 433 * when the mbuf is allocated with MGETHDR. Many users 434 * (e.g. m_copy*, m_prepend) use MGET and then 435 * smash the pkthdr as needed causing these 436 * assertions to trip. For now just disable them. 437 */ 438 M_ASSERTPKTHDR(to); 439 /* Note: with MAC, this may not be a good assertion. */ 440 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 441#endif 442 MBUF_CHECKSLEEP(how); 443#ifdef MAC 444 if (to->m_flags & M_PKTHDR) 445 m_tag_delete_chain(to, NULL); 446#endif 447 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 448 if ((to->m_flags & M_EXT) == 0) 449 to->m_data = to->m_pktdat; 450 to->m_pkthdr = from->m_pkthdr; 451 SLIST_INIT(&to->m_pkthdr.tags); 452 return (m_tag_copy_chain(to, from, MBTOM(how))); 453} 454 455/* 456 * Lesser-used path for M_PREPEND: 457 * allocate new mbuf to prepend to chain, 458 * copy junk along. 459 */ 460struct mbuf * 461m_prepend(struct mbuf *m, int len, int how) 462{ 463 struct mbuf *mn; 464 465 if (m->m_flags & M_PKTHDR) 466 MGETHDR(mn, how, m->m_type); 467 else 468 MGET(mn, how, m->m_type); 469 if (mn == NULL) { 470 m_freem(m); 471 return (NULL); 472 } 473 if (m->m_flags & M_PKTHDR) 474 M_MOVE_PKTHDR(mn, m); 475 mn->m_next = m; 476 m = mn; 477 if (len < MHLEN) 478 MH_ALIGN(m, len); 479 m->m_len = len; 480 return (m); 481} 482 483/* 484 * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 485 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 486 * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller. 487 * Note that the copy is read-only, because clusters are not copied, 488 * only their reference counts are incremented. 489 */ 490struct mbuf * 491m_copym(struct mbuf *m, int off0, int len, int wait) 492{ 493 struct mbuf *n, **np; 494 int off = off0; 495 struct mbuf *top; 496 int copyhdr = 0; 497 498 KASSERT(off >= 0, ("m_copym, negative off %d", off)); 499 KASSERT(len >= 0, ("m_copym, negative len %d", len)); 500 MBUF_CHECKSLEEP(wait); 501 if (off == 0 && m->m_flags & M_PKTHDR) 502 copyhdr = 1; 503 while (off > 0) { 504 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 505 if (off < m->m_len) 506 break; 507 off -= m->m_len; 508 m = m->m_next; 509 } 510 np = ⊤ 511 top = 0; 512 while (len > 0) { 513 if (m == NULL) { 514 KASSERT(len == M_COPYALL, 515 ("m_copym, length > size of mbuf chain")); 516 break; 517 } 518 if (copyhdr) 519 MGETHDR(n, wait, m->m_type); 520 else 521 MGET(n, wait, m->m_type); 522 *np = n; 523 if (n == NULL) 524 goto nospace; 525 if (copyhdr) { 526 if (!m_dup_pkthdr(n, m, wait)) 527 goto nospace; 528 if (len == M_COPYALL) 529 n->m_pkthdr.len -= off0; 530 else 531 n->m_pkthdr.len = len; 532 copyhdr = 0; 533 } 534 n->m_len = min(len, m->m_len - off); 535 if (m->m_flags & M_EXT) { 536 n->m_data = m->m_data + off; 537 n->m_ext = m->m_ext; 538 n->m_flags |= M_EXT; 539 MEXT_ADD_REF(m); 540 n->m_ext.ref_cnt = m->m_ext.ref_cnt; 541 } else 542 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 543 (u_int)n->m_len); 544 if (len != M_COPYALL) 545 len -= n->m_len; 546 off = 0; 547 m = m->m_next; 548 np = &n->m_next; 549 } 550 if (top == NULL) 551 mbstat.m_mcfail++; /* XXX: No consistency. */ 552 553 return (top); 554nospace: 555 m_freem(top); 556 mbstat.m_mcfail++; /* XXX: No consistency. */ 557 return (NULL); 558} 559 560/* 561 * Copy an entire packet, including header (which must be present). 562 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 563 * Note that the copy is read-only, because clusters are not copied, 564 * only their reference counts are incremented. 565 * Preserve alignment of the first mbuf so if the creator has left 566 * some room at the beginning (e.g. for inserting protocol headers) 567 * the copies still have the room available. 568 */ 569struct mbuf * 570m_copypacket(struct mbuf *m, int how) 571{ 572 struct mbuf *top, *n, *o; 573 574 MBUF_CHECKSLEEP(how); 575 MGET(n, how, m->m_type); 576 top = n; 577 if (n == NULL) 578 goto nospace; 579 580 if (!m_dup_pkthdr(n, m, how)) 581 goto nospace; 582 n->m_len = m->m_len; 583 if (m->m_flags & M_EXT) { 584 n->m_data = m->m_data; 585 n->m_ext = m->m_ext; 586 n->m_flags |= M_EXT; 587 MEXT_ADD_REF(m); 588 n->m_ext.ref_cnt = m->m_ext.ref_cnt; 589 } else { 590 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 591 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 592 } 593 594 m = m->m_next; 595 while (m) { 596 MGET(o, how, m->m_type); 597 if (o == NULL) 598 goto nospace; 599 600 n->m_next = o; 601 n = n->m_next; 602 603 n->m_len = m->m_len; 604 if (m->m_flags & M_EXT) { 605 n->m_data = m->m_data; 606 n->m_ext = m->m_ext; 607 n->m_flags |= M_EXT; 608 MEXT_ADD_REF(m); 609 n->m_ext.ref_cnt = m->m_ext.ref_cnt; 610 } else { 611 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 612 } 613 614 m = m->m_next; 615 } 616 return top; 617nospace: 618 m_freem(top); 619 mbstat.m_mcfail++; /* XXX: No consistency. */ 620 return (NULL); 621} 622 623/* 624 * Copy data from an mbuf chain starting "off" bytes from the beginning, 625 * continuing for "len" bytes, into the indicated buffer. 626 */ 627void 628m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 629{ 630 u_int count; 631 632 KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 633 KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 634 while (off > 0) { 635 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 636 if (off < m->m_len) 637 break; 638 off -= m->m_len; 639 m = m->m_next; 640 } 641 while (len > 0) { 642 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 643 count = min(m->m_len - off, len); 644 bcopy(mtod(m, caddr_t) + off, cp, count); 645 len -= count; 646 cp += count; 647 off = 0; 648 m = m->m_next; 649 } 650} 651 652/* 653 * Copy a packet header mbuf chain into a completely new chain, including 654 * copying any mbuf clusters. Use this instead of m_copypacket() when 655 * you need a writable copy of an mbuf chain. 656 */ 657struct mbuf * 658m_dup(struct mbuf *m, int how) 659{ 660 struct mbuf **p, *top = NULL; 661 int remain, moff, nsize; 662 663 MBUF_CHECKSLEEP(how); 664 /* Sanity check */ 665 if (m == NULL) 666 return (NULL); 667 M_ASSERTPKTHDR(m); 668 669 /* While there's more data, get a new mbuf, tack it on, and fill it */ 670 remain = m->m_pkthdr.len; 671 moff = 0; 672 p = ⊤ 673 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 674 struct mbuf *n; 675 676 /* Get the next new mbuf */ 677 if (remain >= MINCLSIZE) { 678 n = m_getcl(how, m->m_type, 0); 679 nsize = MCLBYTES; 680 } else { 681 n = m_get(how, m->m_type); 682 nsize = MLEN; 683 } 684 if (n == NULL) 685 goto nospace; 686 687 if (top == NULL) { /* First one, must be PKTHDR */ 688 if (!m_dup_pkthdr(n, m, how)) { 689 m_free(n); 690 goto nospace; 691 } 692 nsize = MHLEN; 693 } 694 n->m_len = 0; 695 696 /* Link it into the new chain */ 697 *p = n; 698 p = &n->m_next; 699 700 /* Copy data from original mbuf(s) into new mbuf */ 701 while (n->m_len < nsize && m != NULL) { 702 int chunk = min(nsize - n->m_len, m->m_len - moff); 703 704 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 705 moff += chunk; 706 n->m_len += chunk; 707 remain -= chunk; 708 if (moff == m->m_len) { 709 m = m->m_next; 710 moff = 0; 711 } 712 } 713 714 /* Check correct total mbuf length */ 715 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 716 ("%s: bogus m_pkthdr.len", __func__)); 717 } 718 return (top); 719 720nospace: 721 m_freem(top); 722 mbstat.m_mcfail++; /* XXX: No consistency. */ 723 return (NULL); 724} 725 726/* 727 * Concatenate mbuf chain n to m. 728 * Both chains must be of the same type (e.g. MT_DATA). 729 * Any m_pkthdr is not updated. 730 */ 731void 732m_cat(struct mbuf *m, struct mbuf *n) 733{ 734 while (m->m_next) 735 m = m->m_next; 736 while (n) { 737 if (m->m_flags & M_EXT || 738 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 739 /* just join the two chains */ 740 m->m_next = n; 741 return; 742 } 743 /* splat the data from one into the other */ 744 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 745 (u_int)n->m_len); 746 m->m_len += n->m_len; 747 n = m_free(n); 748 } 749} 750 751void 752m_adj(struct mbuf *mp, int req_len) 753{ 754 int len = req_len; 755 struct mbuf *m; 756 int count; 757 758 if ((m = mp) == NULL) 759 return; 760 if (len >= 0) { 761 /* 762 * Trim from head. 763 */ 764 while (m != NULL && len > 0) { 765 if (m->m_len <= len) { 766 len -= m->m_len; 767 m->m_len = 0; 768 m = m->m_next; 769 } else { 770 m->m_len -= len; 771 m->m_data += len; 772 len = 0; 773 } 774 } 775 m = mp; 776 if (mp->m_flags & M_PKTHDR) 777 m->m_pkthdr.len -= (req_len - len); 778 } else { 779 /* 780 * Trim from tail. Scan the mbuf chain, 781 * calculating its length and finding the last mbuf. 782 * If the adjustment only affects this mbuf, then just 783 * adjust and return. Otherwise, rescan and truncate 784 * after the remaining size. 785 */ 786 len = -len; 787 count = 0; 788 for (;;) { 789 count += m->m_len; 790 if (m->m_next == (struct mbuf *)0) 791 break; 792 m = m->m_next; 793 } 794 if (m->m_len >= len) { 795 m->m_len -= len; 796 if (mp->m_flags & M_PKTHDR) 797 mp->m_pkthdr.len -= len; 798 return; 799 } 800 count -= len; 801 if (count < 0) 802 count = 0; 803 /* 804 * Correct length for chain is "count". 805 * Find the mbuf with last data, adjust its length, 806 * and toss data from remaining mbufs on chain. 807 */ 808 m = mp; 809 if (m->m_flags & M_PKTHDR) 810 m->m_pkthdr.len = count; 811 for (; m; m = m->m_next) { 812 if (m->m_len >= count) { 813 m->m_len = count; 814 if (m->m_next != NULL) { 815 m_freem(m->m_next); 816 m->m_next = NULL; 817 } 818 break; 819 } 820 count -= m->m_len; 821 } 822 } 823} 824 825/* 826 * Rearange an mbuf chain so that len bytes are contiguous 827 * and in the data area of an mbuf (so that mtod and dtom 828 * will work for a structure of size len). Returns the resulting 829 * mbuf chain on success, frees it and returns null on failure. 830 * If there is room, it will add up to max_protohdr-len extra bytes to the 831 * contiguous region in an attempt to avoid being called next time. 832 */ 833struct mbuf * 834m_pullup(struct mbuf *n, int len) 835{ 836 struct mbuf *m; 837 int count; 838 int space; 839 840 /* 841 * If first mbuf has no cluster, and has room for len bytes 842 * without shifting current data, pullup into it, 843 * otherwise allocate a new mbuf to prepend to the chain. 844 */ 845 if ((n->m_flags & M_EXT) == 0 && 846 n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 847 if (n->m_len >= len) 848 return (n); 849 m = n; 850 n = n->m_next; 851 len -= m->m_len; 852 } else { 853 if (len > MHLEN) 854 goto bad; 855 MGET(m, M_DONTWAIT, n->m_type); 856 if (m == NULL) 857 goto bad; 858 m->m_len = 0; 859 if (n->m_flags & M_PKTHDR) 860 M_MOVE_PKTHDR(m, n); 861 } 862 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 863 do { 864 count = min(min(max(len, max_protohdr), space), n->m_len); 865 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 866 (u_int)count); 867 len -= count; 868 m->m_len += count; 869 n->m_len -= count; 870 space -= count; 871 if (n->m_len) 872 n->m_data += count; 873 else 874 n = m_free(n); 875 } while (len > 0 && n); 876 if (len > 0) { 877 (void) m_free(m); 878 goto bad; 879 } 880 m->m_next = n; 881 return (m); 882bad: 883 m_freem(n); 884 mbstat.m_mpfail++; /* XXX: No consistency. */ 885 return (NULL); 886} 887 888/* 889 * Like m_pullup(), except a new mbuf is always allocated, and we allow 890 * the amount of empty space before the data in the new mbuf to be specified 891 * (in the event that the caller expects to prepend later). 892 */ 893int MSFail; 894 895struct mbuf * 896m_copyup(struct mbuf *n, int len, int dstoff) 897{ 898 struct mbuf *m; 899 int count, space; 900 901 if (len > (MHLEN - dstoff)) 902 goto bad; 903 MGET(m, M_DONTWAIT, n->m_type); 904 if (m == NULL) 905 goto bad; 906 m->m_len = 0; 907 if (n->m_flags & M_PKTHDR) 908 M_MOVE_PKTHDR(m, n); 909 m->m_data += dstoff; 910 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 911 do { 912 count = min(min(max(len, max_protohdr), space), n->m_len); 913 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), 914 (unsigned)count); 915 len -= count; 916 m->m_len += count; 917 n->m_len -= count; 918 space -= count; 919 if (n->m_len) 920 n->m_data += count; 921 else 922 n = m_free(n); 923 } while (len > 0 && n); 924 if (len > 0) { 925 (void) m_free(m); 926 goto bad; 927 } 928 m->m_next = n; 929 return (m); 930 bad: 931 m_freem(n); 932 MSFail++; 933 return (NULL); 934} 935 936/* 937 * Partition an mbuf chain in two pieces, returning the tail -- 938 * all but the first len0 bytes. In case of failure, it returns NULL and 939 * attempts to restore the chain to its original state. 940 * 941 * Note that the resulting mbufs might be read-only, because the new 942 * mbuf can end up sharing an mbuf cluster with the original mbuf if 943 * the "breaking point" happens to lie within a cluster mbuf. Use the 944 * M_WRITABLE() macro to check for this case. 945 */ 946struct mbuf * 947m_split(struct mbuf *m0, int len0, int wait) 948{ 949 struct mbuf *m, *n; 950 u_int len = len0, remain; 951 952 MBUF_CHECKSLEEP(wait); 953 for (m = m0; m && len > m->m_len; m = m->m_next) 954 len -= m->m_len; 955 if (m == NULL) 956 return (NULL); 957 remain = m->m_len - len; 958 if (m0->m_flags & M_PKTHDR) { 959 MGETHDR(n, wait, m0->m_type); 960 if (n == NULL) 961 return (NULL); 962 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 963 n->m_pkthdr.len = m0->m_pkthdr.len - len0; 964 m0->m_pkthdr.len = len0; 965 if (m->m_flags & M_EXT) 966 goto extpacket; 967 if (remain > MHLEN) { 968 /* m can't be the lead packet */ 969 MH_ALIGN(n, 0); 970 n->m_next = m_split(m, len, wait); 971 if (n->m_next == NULL) { 972 (void) m_free(n); 973 return (NULL); 974 } else { 975 n->m_len = 0; 976 return (n); 977 } 978 } else 979 MH_ALIGN(n, remain); 980 } else if (remain == 0) { 981 n = m->m_next; 982 m->m_next = NULL; 983 return (n); 984 } else { 985 MGET(n, wait, m->m_type); 986 if (n == NULL) 987 return (NULL); 988 M_ALIGN(n, remain); 989 } 990extpacket: 991 if (m->m_flags & M_EXT) { 992 n->m_flags |= M_EXT; 993 n->m_ext = m->m_ext; 994 MEXT_ADD_REF(m); 995 n->m_ext.ref_cnt = m->m_ext.ref_cnt; 996 n->m_data = m->m_data + len; 997 } else { 998 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 999 } 1000 n->m_len = remain; 1001 m->m_len = len; 1002 n->m_next = m->m_next; 1003 m->m_next = NULL; 1004 return (n); 1005} 1006/* 1007 * Routine to copy from device local memory into mbufs. 1008 * Note that `off' argument is offset into first mbuf of target chain from 1009 * which to begin copying the data to. 1010 */ 1011struct mbuf * 1012m_devget(char *buf, int totlen, int off, struct ifnet *ifp, 1013 void (*copy)(char *from, caddr_t to, u_int len)) 1014{ 1015 struct mbuf *m; 1016 struct mbuf *top = NULL, **mp = ⊤ 1017 int len; 1018 1019 if (off < 0 || off > MHLEN) 1020 return (NULL); 1021 1022 while (totlen > 0) { 1023 if (top == NULL) { /* First one, must be PKTHDR */ 1024 if (totlen + off >= MINCLSIZE) { 1025 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1026 len = MCLBYTES; 1027 } else { 1028 m = m_gethdr(M_DONTWAIT, MT_DATA); 1029 len = MHLEN; 1030 1031 /* Place initial small packet/header at end of mbuf */ 1032 if (m && totlen + off + max_linkhdr <= MLEN) { 1033 m->m_data += max_linkhdr; 1034 len -= max_linkhdr; 1035 } 1036 } 1037 if (m == NULL) 1038 return NULL; 1039 m->m_pkthdr.rcvif = ifp; 1040 m->m_pkthdr.len = totlen; 1041 } else { 1042 if (totlen + off >= MINCLSIZE) { 1043 m = m_getcl(M_DONTWAIT, MT_DATA, 0); 1044 len = MCLBYTES; 1045 } else { 1046 m = m_get(M_DONTWAIT, MT_DATA); 1047 len = MLEN; 1048 } 1049 if (m == NULL) { 1050 m_freem(top); 1051 return NULL; 1052 } 1053 } 1054 if (off) { 1055 m->m_data += off; 1056 len -= off; 1057 off = 0; 1058 } 1059 m->m_len = len = min(totlen, len); 1060 if (copy) 1061 copy(buf, mtod(m, caddr_t), (u_int)len); 1062 else 1063 bcopy(buf, mtod(m, caddr_t), (u_int)len); 1064 buf += len; 1065 *mp = m; 1066 mp = &m->m_next; 1067 totlen -= len; 1068 } 1069 return (top); 1070} 1071 1072/* 1073 * Copy data from a buffer back into the indicated mbuf chain, 1074 * starting "off" bytes from the beginning, extending the mbuf 1075 * chain if necessary. 1076 */ 1077void 1078m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp) 1079{ 1080 int mlen; 1081 struct mbuf *m = m0, *n; 1082 int totlen = 0; 1083 1084 if (m0 == NULL) 1085 return; 1086 while (off > (mlen = m->m_len)) { 1087 off -= mlen; 1088 totlen += mlen; 1089 if (m->m_next == NULL) { 1090 n = m_get(M_DONTWAIT, m->m_type); 1091 if (n == NULL) 1092 goto out; 1093 bzero(mtod(n, caddr_t), MLEN); 1094 n->m_len = min(MLEN, len + off); 1095 m->m_next = n; 1096 } 1097 m = m->m_next; 1098 } 1099 while (len > 0) { 1100 mlen = min (m->m_len - off, len); 1101 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 1102 cp += mlen; 1103 len -= mlen; 1104 mlen += off; 1105 off = 0; 1106 totlen += mlen; 1107 if (len == 0) 1108 break; 1109 if (m->m_next == NULL) { 1110 n = m_get(M_DONTWAIT, m->m_type); 1111 if (n == NULL) 1112 break; 1113 n->m_len = min(MLEN, len); 1114 m->m_next = n; 1115 } 1116 m = m->m_next; 1117 } 1118out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 1119 m->m_pkthdr.len = totlen; 1120} 1121 1122/* 1123 * Append the specified data to the indicated mbuf chain, 1124 * Extend the mbuf chain if the new data does not fit in 1125 * existing space. 1126 * 1127 * Return 1 if able to complete the job; otherwise 0. 1128 */ 1129int 1130m_append(struct mbuf *m0, int len, c_caddr_t cp) 1131{ 1132 struct mbuf *m, *n; 1133 int remainder, space; 1134 1135 for (m = m0; m->m_next != NULL; m = m->m_next) 1136 ; 1137 remainder = len; 1138 space = M_TRAILINGSPACE(m); 1139 if (space > 0) { 1140 /* 1141 * Copy into available space. 1142 */ 1143 if (space > remainder) 1144 space = remainder; 1145 bcopy(cp, mtod(m, caddr_t) + m->m_len, space); 1146 m->m_len += space; 1147 cp += space, remainder -= space; 1148 } 1149 while (remainder > 0) { 1150 /* 1151 * Allocate a new mbuf; could check space 1152 * and allocate a cluster instead. 1153 */ 1154 n = m_get(M_DONTWAIT, m->m_type); 1155 if (n == NULL) 1156 break; 1157 n->m_len = min(MLEN, remainder); 1158 bcopy(cp, mtod(n, caddr_t), n->m_len); 1159 cp += n->m_len, remainder -= n->m_len; 1160 m->m_next = n; 1161 m = n; 1162 } 1163 if (m0->m_flags & M_PKTHDR) 1164 m0->m_pkthdr.len += len - remainder; 1165 return (remainder == 0); 1166} 1167 1168/* 1169 * Apply function f to the data in an mbuf chain starting "off" bytes from 1170 * the beginning, continuing for "len" bytes. 1171 */ 1172int 1173m_apply(struct mbuf *m, int off, int len, 1174 int (*f)(void *, void *, u_int), void *arg) 1175{ 1176 u_int count; 1177 int rval; 1178 1179 KASSERT(off >= 0, ("m_apply, negative off %d", off)); 1180 KASSERT(len >= 0, ("m_apply, negative len %d", len)); 1181 while (off > 0) { 1182 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1183 if (off < m->m_len) 1184 break; 1185 off -= m->m_len; 1186 m = m->m_next; 1187 } 1188 while (len > 0) { 1189 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1190 count = min(m->m_len - off, len); 1191 rval = (*f)(arg, mtod(m, caddr_t) + off, count); 1192 if (rval) 1193 return (rval); 1194 len -= count; 1195 off = 0; 1196 m = m->m_next; 1197 } 1198 return (0); 1199} 1200 1201/* 1202 * Return a pointer to mbuf/offset of location in mbuf chain. 1203 */ 1204struct mbuf * 1205m_getptr(struct mbuf *m, int loc, int *off) 1206{ 1207 1208 while (loc >= 0) { 1209 /* Normal end of search. */ 1210 if (m->m_len > loc) { 1211 *off = loc; 1212 return (m); 1213 } else { 1214 loc -= m->m_len; 1215 if (m->m_next == NULL) { 1216 if (loc == 0) { 1217 /* Point at the end of valid data. */ 1218 *off = m->m_len; 1219 return (m); 1220 } 1221 return (NULL); 1222 } 1223 m = m->m_next; 1224 } 1225 } 1226 return (NULL); 1227} 1228 1229void 1230m_print(const struct mbuf *m, int maxlen) 1231{ 1232 int len; 1233 int pdata; 1234 const struct mbuf *m2; 1235 1236 if (m->m_flags & M_PKTHDR) 1237 len = m->m_pkthdr.len; 1238 else 1239 len = -1; 1240 m2 = m; 1241 while (m2 != NULL && (len == -1 || len)) { 1242 pdata = m2->m_len; 1243 if (maxlen != -1 && pdata > maxlen) 1244 pdata = maxlen; 1245 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len, 1246 m2->m_next, m2->m_flags, "\20\20freelist\17skipfw" 1247 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly" 1248 "\3eor\2pkthdr\1ext", pdata ? "" : "\n"); 1249 if (pdata) 1250 printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-"); 1251 if (len != -1) 1252 len -= m2->m_len; 1253 m2 = m2->m_next; 1254 } 1255 if (len > 0) 1256 printf("%d bytes unaccounted for.\n", len); 1257 return; 1258} 1259 1260u_int 1261m_fixhdr(struct mbuf *m0) 1262{ 1263 u_int len; 1264 1265 len = m_length(m0, NULL); 1266 m0->m_pkthdr.len = len; 1267 return (len); 1268} 1269 1270u_int 1271m_length(struct mbuf *m0, struct mbuf **last) 1272{ 1273 struct mbuf *m; 1274 u_int len; 1275 1276 len = 0; 1277 for (m = m0; m != NULL; m = m->m_next) { 1278 len += m->m_len; 1279 if (m->m_next == NULL) 1280 break; 1281 } 1282 if (last != NULL) 1283 *last = m; 1284 return (len); 1285} 1286 1287/* 1288 * Defragment a mbuf chain, returning the shortest possible 1289 * chain of mbufs and clusters. If allocation fails and 1290 * this cannot be completed, NULL will be returned, but 1291 * the passed in chain will be unchanged. Upon success, 1292 * the original chain will be freed, and the new chain 1293 * will be returned. 1294 * 1295 * If a non-packet header is passed in, the original 1296 * mbuf (chain?) will be returned unharmed. 1297 */ 1298struct mbuf * 1299m_defrag(struct mbuf *m0, int how) 1300{ 1301 struct mbuf *m_new = NULL, *m_final = NULL; 1302 int progress = 0, length; 1303 1304 MBUF_CHECKSLEEP(how); 1305 if (!(m0->m_flags & M_PKTHDR)) 1306 return (m0); 1307 1308 m_fixhdr(m0); /* Needed sanity check */ 1309 1310#ifdef MBUF_STRESS_TEST 1311 if (m_defragrandomfailures) { 1312 int temp = arc4random() & 0xff; 1313 if (temp == 0xba) 1314 goto nospace; 1315 } 1316#endif 1317 1318 if (m0->m_pkthdr.len > MHLEN) 1319 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1320 else 1321 m_final = m_gethdr(how, MT_DATA); 1322 1323 if (m_final == NULL) 1324 goto nospace; 1325 1326 if (m_dup_pkthdr(m_final, m0, how) == 0) 1327 goto nospace; 1328 1329 m_new = m_final; 1330 1331 while (progress < m0->m_pkthdr.len) { 1332 length = m0->m_pkthdr.len - progress; 1333 if (length > MCLBYTES) 1334 length = MCLBYTES; 1335 1336 if (m_new == NULL) { 1337 if (length > MLEN) 1338 m_new = m_getcl(how, MT_DATA, 0); 1339 else 1340 m_new = m_get(how, MT_DATA); 1341 if (m_new == NULL) 1342 goto nospace; 1343 } 1344 1345 m_copydata(m0, progress, length, mtod(m_new, caddr_t)); 1346 progress += length; 1347 m_new->m_len = length; 1348 if (m_new != m_final) 1349 m_cat(m_final, m_new); 1350 m_new = NULL; 1351 } 1352#ifdef MBUF_STRESS_TEST 1353 if (m0->m_next == NULL) 1354 m_defraguseless++; 1355#endif 1356 m_freem(m0); 1357 m0 = m_final; 1358#ifdef MBUF_STRESS_TEST 1359 m_defragpackets++; 1360 m_defragbytes += m0->m_pkthdr.len; 1361#endif 1362 return (m0); 1363nospace: 1364#ifdef MBUF_STRESS_TEST 1365 m_defragfailure++; 1366#endif 1367 if (m_final) 1368 m_freem(m_final); 1369 return (NULL); 1370} 1371 1372#ifdef MBUF_STRESS_TEST 1373 1374/* 1375 * Fragment an mbuf chain. There's no reason you'd ever want to do 1376 * this in normal usage, but it's great for stress testing various 1377 * mbuf consumers. 1378 * 1379 * If fragmentation is not possible, the original chain will be 1380 * returned. 1381 * 1382 * Possible length values: 1383 * 0 no fragmentation will occur 1384 * > 0 each fragment will be of the specified length 1385 * -1 each fragment will be the same random value in length 1386 * -2 each fragment's length will be entirely random 1387 * (Random values range from 1 to 256) 1388 */ 1389struct mbuf * 1390m_fragment(struct mbuf *m0, int how, int length) 1391{ 1392 struct mbuf *m_new = NULL, *m_final = NULL; 1393 int progress = 0; 1394 1395 if (!(m0->m_flags & M_PKTHDR)) 1396 return (m0); 1397 1398 if ((length == 0) || (length < -2)) 1399 return (m0); 1400 1401 m_fixhdr(m0); /* Needed sanity check */ 1402 1403 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1404 1405 if (m_final == NULL) 1406 goto nospace; 1407 1408 if (m_dup_pkthdr(m_final, m0, how) == 0) 1409 goto nospace; 1410 1411 m_new = m_final; 1412 1413 if (length == -1) 1414 length = 1 + (arc4random() & 255); 1415 1416 while (progress < m0->m_pkthdr.len) { 1417 int fraglen; 1418 1419 if (length > 0) 1420 fraglen = length; 1421 else 1422 fraglen = 1 + (arc4random() & 255); 1423 if (fraglen > m0->m_pkthdr.len - progress) 1424 fraglen = m0->m_pkthdr.len - progress; 1425 1426 if (fraglen > MCLBYTES) 1427 fraglen = MCLBYTES; 1428 1429 if (m_new == NULL) { 1430 m_new = m_getcl(how, MT_DATA, 0); 1431 if (m_new == NULL) 1432 goto nospace; 1433 } 1434 1435 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t)); 1436 progress += fraglen; 1437 m_new->m_len = fraglen; 1438 if (m_new != m_final) 1439 m_cat(m_final, m_new); 1440 m_new = NULL; 1441 } 1442 m_freem(m0); 1443 m0 = m_final; 1444 return (m0); 1445nospace: 1446 if (m_final) 1447 m_freem(m_final); 1448 /* Return the original chain on failure */ 1449 return (m0); 1450} 1451 1452#endif 1453 1454struct mbuf * 1455m_uiotombuf(struct uio *uio, int how, int len, int align) 1456{ 1457 struct mbuf *m_new = NULL, *m_final = NULL; 1458 int progress = 0, error = 0, length, total; 1459 1460 if (len > 0) 1461 total = min(uio->uio_resid, len); 1462 else 1463 total = uio->uio_resid; 1464 if (align >= MHLEN) 1465 goto nospace; 1466 if (total + align > MHLEN) 1467 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1468 else 1469 m_final = m_gethdr(how, MT_DATA); 1470 if (m_final == NULL) 1471 goto nospace; 1472 m_final->m_data += align; 1473 m_new = m_final; 1474 while (progress < total) { 1475 length = total - progress; 1476 if (length > MCLBYTES) 1477 length = MCLBYTES; 1478 if (m_new == NULL) { 1479 if (length > MLEN) 1480 m_new = m_getcl(how, MT_DATA, 0); 1481 else 1482 m_new = m_get(how, MT_DATA); 1483 if (m_new == NULL) 1484 goto nospace; 1485 } 1486 error = uiomove(mtod(m_new, void *), length, uio); 1487 if (error) 1488 goto nospace; 1489 progress += length; 1490 m_new->m_len = length; 1491 if (m_new != m_final) 1492 m_cat(m_final, m_new); 1493 m_new = NULL; 1494 } 1495 m_fixhdr(m_final); 1496 return (m_final); 1497nospace: 1498 if (m_new) 1499 m_free(m_new); 1500 if (m_final) 1501 m_freem(m_final); 1502 return (NULL); 1503} 1504 1505/* 1506 * Set the m_data pointer of a newly-allocated mbuf 1507 * to place an object of the specified size at the 1508 * end of the mbuf, longword aligned. 1509 */ 1510void 1511m_align(struct mbuf *m, int len) 1512{ 1513 int adjust; 1514 1515 if (m->m_flags & M_EXT) 1516 adjust = m->m_ext.ext_size - len; 1517 else if (m->m_flags & M_PKTHDR) 1518 adjust = MHLEN - len; 1519 else 1520 adjust = MLEN - len; 1521 m->m_data += adjust &~ (sizeof(long)-1); 1522} 1523