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$"); 34 35 #include "opt_param.h" 36 #include "opt_mbuf_stress_test.h" 37 #include "opt_mbuf_profiling.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/malloc.h> 45 #include <sys/mbuf.h> 46 #include <sys/sysctl.h> 47 #include <sys/domain.h> 48 #include <sys/protosw.h> 49 #include <sys/uio.h> 50 51 int max_linkhdr; 52 int max_protohdr; 53 int max_hdr; 54 int max_datalen; 55 #ifdef MBUF_STRESS_TEST 56 int m_defragpackets; 57 int m_defragbytes; 58 int m_defraguseless; 59 int m_defragfailure; 60 int m_defragrandomfailures; 61 #endif 62 63 /* 64 * sysctl(8) exported objects 65 */ 66 SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RD, 67 &max_linkhdr, 0, "Size of largest link layer header"); 68 SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RD, 69 &max_protohdr, 0, "Size of largest protocol layer header"); 70 SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RD, 71 &max_hdr, 0, "Size of largest link plus protocol header"); 72 SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RD, 73 &max_datalen, 0, "Minimum space left in mbuf after max_hdr"); 74 #ifdef MBUF_STRESS_TEST 75 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD, 76 &m_defragpackets, 0, ""); 77 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD, 78 &m_defragbytes, 0, ""); 79 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD, 80 &m_defraguseless, 0, ""); 81 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD, 82 &m_defragfailure, 0, ""); 83 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW, 84 &m_defragrandomfailures, 0, ""); 85 #endif 86 87 /* 88 * Ensure the correct size of various mbuf parameters. It could be off due 89 * to compiler-induced padding and alignment artifacts. 90 */ 91 CTASSERT(sizeof(struct mbuf) == MSIZE); 92 CTASSERT(MSIZE - offsetof(struct mbuf, m_dat) == MLEN); 93 CTASSERT(MSIZE - offsetof(struct mbuf, m_pktdat) == MHLEN); 94 95 /* 96 * m_get2() allocates minimum mbuf that would fit "size" argument. 97 */ 98 struct mbuf * 99 m_get2(int size, int how, short type, int flags) 100 { 101 struct mb_args args; 102 struct mbuf *m, *n; 103 104 args.flags = flags; 105 args.type = type; 106 107 if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0)) 108 return (uma_zalloc_arg(zone_mbuf, &args, how)); 109 if (size <= MCLBYTES) 110 return (uma_zalloc_arg(zone_pack, &args, how)); 111 112 if (size > MJUMPAGESIZE) 113 return (NULL); 114 115 m = uma_zalloc_arg(zone_mbuf, &args, how); 116 if (m == NULL) 117 return (NULL); 118 119 n = uma_zalloc_arg(zone_jumbop, m, how); 120 if (n == NULL) { 121 uma_zfree(zone_mbuf, m); 122 return (NULL); 123 } 124 125 return (m); 126 } 127 128 /* 129 * m_getjcl() returns an mbuf with a cluster of the specified size attached. 130 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES. 131 */ 132 struct mbuf * 133 m_getjcl(int how, short type, int flags, int size) 134 { 135 struct mb_args args; 136 struct mbuf *m, *n; 137 uma_zone_t zone; 138 139 if (size == MCLBYTES) 140 return m_getcl(how, type, flags); 141 142 args.flags = flags; 143 args.type = type; 144 145 m = uma_zalloc_arg(zone_mbuf, &args, how); 146 if (m == NULL) 147 return (NULL); 148 149 zone = m_getzone(size); 150 n = uma_zalloc_arg(zone, m, how); 151 if (n == NULL) { 152 uma_zfree(zone_mbuf, m); 153 return (NULL); 154 } 155 return (m); 156 } 157 158 /* 159 * Allocate a given length worth of mbufs and/or clusters (whatever fits 160 * best) and return a pointer to the top of the allocated chain. If an 161 * existing mbuf chain is provided, then we will append the new chain 162 * to the existing one but still return the top of the newly allocated 163 * chain. 164 */ 165 struct mbuf * 166 m_getm2(struct mbuf *m, int len, int how, short type, int flags) 167 { 168 struct mbuf *mb, *nm = NULL, *mtail = NULL; 169 170 KASSERT(len >= 0, ("%s: len is < 0", __func__)); 171 172 /* Validate flags. */ 173 flags &= (M_PKTHDR | M_EOR); 174 175 /* Packet header mbuf must be first in chain. */ 176 if ((flags & M_PKTHDR) && m != NULL) 177 flags &= ~M_PKTHDR; 178 179 /* Loop and append maximum sized mbufs to the chain tail. */ 180 while (len > 0) { 181 if (len > MCLBYTES) 182 mb = m_getjcl(how, type, (flags & M_PKTHDR), 183 MJUMPAGESIZE); 184 else if (len >= MINCLSIZE) 185 mb = m_getcl(how, type, (flags & M_PKTHDR)); 186 else if (flags & M_PKTHDR) 187 mb = m_gethdr(how, type); 188 else 189 mb = m_get(how, type); 190 191 /* Fail the whole operation if one mbuf can't be allocated. */ 192 if (mb == NULL) { 193 if (nm != NULL) 194 m_freem(nm); 195 return (NULL); 196 } 197 198 /* Book keeping. */ 199 len -= (mb->m_flags & M_EXT) ? mb->m_ext.ext_size : 200 ((mb->m_flags & M_PKTHDR) ? MHLEN : MLEN); 201 if (mtail != NULL) 202 mtail->m_next = mb; 203 else 204 nm = mb; 205 mtail = mb; 206 flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */ 207 } 208 if (flags & M_EOR) 209 mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */ 210 211 /* If mbuf was supplied, append new chain to the end of it. */ 212 if (m != NULL) { 213 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next) 214 ; 215 mtail->m_next = nm; 216 mtail->m_flags &= ~M_EOR; 217 } else 218 m = nm; 219 220 return (m); 221 } 222 223 /* 224 * Free an entire chain of mbufs and associated external buffers, if 225 * applicable. 226 */ 227 void 228 m_freem(struct mbuf *mb) 229 { 230 231 while (mb != NULL) 232 mb = m_free(mb); 233 } 234 235 /*- 236 * Configure a provided mbuf to refer to the provided external storage 237 * buffer and setup a reference count for said buffer. If the setting 238 * up of the reference count fails, the M_EXT bit will not be set. If 239 * successfull, the M_EXT bit is set in the mbuf's flags. 240 * 241 * Arguments: 242 * mb The existing mbuf to which to attach the provided buffer. 243 * buf The address of the provided external storage buffer. 244 * size The size of the provided buffer. 245 * freef A pointer to a routine that is responsible for freeing the 246 * provided external storage buffer. 247 * args A pointer to an argument structure (of any type) to be passed 248 * to the provided freef routine (may be NULL). 249 * flags Any other flags to be passed to the provided mbuf. 250 * type The type that the external storage buffer should be 251 * labeled with. 252 * 253 * Returns: 254 * Nothing. 255 */ 256 int 257 m_extadd(struct mbuf *mb, caddr_t buf, u_int size, 258 void (*freef)(struct mbuf *, void *, void *), void *arg1, void *arg2, 259 int flags, int type, int wait) 260 { 261 KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__)); 262 263 if (type != EXT_EXTREF) 264 mb->m_ext.ext_cnt = uma_zalloc(zone_ext_refcnt, wait); 265 266 if (mb->m_ext.ext_cnt == NULL) 267 return (ENOMEM); 268 269 *(mb->m_ext.ext_cnt) = 1; 270 mb->m_flags |= (M_EXT | flags); 271 mb->m_ext.ext_buf = buf; 272 mb->m_data = mb->m_ext.ext_buf; 273 mb->m_ext.ext_size = size; 274 mb->m_ext.ext_free = freef; 275 mb->m_ext.ext_arg1 = arg1; 276 mb->m_ext.ext_arg2 = arg2; 277 mb->m_ext.ext_type = type; 278 mb->m_ext.ext_flags = 0; 279 280 return (0); 281 } 282 283 /* 284 * Non-directly-exported function to clean up after mbufs with M_EXT 285 * storage attached to them if the reference count hits 1. 286 */ 287 void 288 mb_free_ext(struct mbuf *m) 289 { 290 int freembuf; 291 292 KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m)); 293 294 /* 295 * Check if the header is embedded in the cluster. 296 */ 297 freembuf = (m->m_flags & M_NOFREE) ? 0 : 1; 298 299 switch (m->m_ext.ext_type) { 300 case EXT_SFBUF: 301 sf_ext_free(m->m_ext.ext_arg1, m->m_ext.ext_arg2); 302 break; 303 default: 304 KASSERT(m->m_ext.ext_cnt != NULL, 305 ("%s: no refcounting pointer on %p", __func__, m)); 306 /* 307 * Free attached storage if this mbuf is the only 308 * reference to it. 309 */ 310 if (*(m->m_ext.ext_cnt) != 1) { 311 if (atomic_fetchadd_int(m->m_ext.ext_cnt, -1) != 1) 312 break; 313 } 314 315 switch (m->m_ext.ext_type) { 316 case EXT_PACKET: /* The packet zone is special. */ 317 if (*(m->m_ext.ext_cnt) == 0) 318 *(m->m_ext.ext_cnt) = 1; 319 uma_zfree(zone_pack, m); 320 return; /* Job done. */ 321 case EXT_CLUSTER: 322 uma_zfree(zone_clust, m->m_ext.ext_buf); 323 break; 324 case EXT_JUMBOP: 325 uma_zfree(zone_jumbop, m->m_ext.ext_buf); 326 break; 327 case EXT_JUMBO9: 328 uma_zfree(zone_jumbo9, m->m_ext.ext_buf); 329 break; 330 case EXT_JUMBO16: 331 uma_zfree(zone_jumbo16, m->m_ext.ext_buf); 332 break; 333 case EXT_NET_DRV: 334 case EXT_MOD_TYPE: 335 case EXT_DISPOSABLE: 336 *(m->m_ext.ext_cnt) = 0; 337 uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *, 338 m->m_ext.ext_cnt)); 339 /* FALLTHROUGH */ 340 case EXT_EXTREF: 341 KASSERT(m->m_ext.ext_free != NULL, 342 ("%s: ext_free not set", __func__)); 343 (*(m->m_ext.ext_free))(m, m->m_ext.ext_arg1, 344 m->m_ext.ext_arg2); 345 break; 346 default: 347 KASSERT(m->m_ext.ext_type == 0, 348 ("%s: unknown ext_type", __func__)); 349 } 350 } 351 352 if (freembuf) 353 uma_zfree(zone_mbuf, m); 354 } 355 356 /* 357 * Attach the cluster from *m to *n, set up m_ext in *n 358 * and bump the refcount of the cluster. 359 */ 360 static void 361 mb_dupcl(struct mbuf *n, struct mbuf *m) 362 { 363 364 KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m)); 365 KASSERT(!(n->m_flags & M_EXT), ("%s: M_EXT set on %p", __func__, n)); 366 367 switch (m->m_ext.ext_type) { 368 case EXT_SFBUF: 369 sf_ext_ref(m->m_ext.ext_arg1, m->m_ext.ext_arg2); 370 break; 371 default: 372 KASSERT(m->m_ext.ext_cnt != NULL, 373 ("%s: no refcounting pointer on %p", __func__, m)); 374 if (*(m->m_ext.ext_cnt) == 1) 375 *(m->m_ext.ext_cnt) += 1; 376 else 377 atomic_add_int(m->m_ext.ext_cnt, 1); 378 } 379 380 n->m_ext = m->m_ext; 381 n->m_flags |= M_EXT; 382 n->m_flags |= m->m_flags & M_RDONLY; 383 } 384 385 /* 386 * Clean up mbuf (chain) from any tags and packet headers. 387 * If "all" is set then the first mbuf in the chain will be 388 * cleaned too. 389 */ 390 void 391 m_demote(struct mbuf *m0, int all) 392 { 393 struct mbuf *m; 394 395 for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) { 396 if (m->m_flags & M_PKTHDR) { 397 m_tag_delete_chain(m, NULL); 398 m->m_flags &= ~M_PKTHDR; 399 bzero(&m->m_pkthdr, sizeof(struct pkthdr)); 400 } 401 if (m != m0 && m->m_nextpkt != NULL) { 402 KASSERT(m->m_nextpkt == NULL, 403 ("%s: m_nextpkt not NULL", __func__)); 404 m_freem(m->m_nextpkt); 405 m->m_nextpkt = NULL; 406 } 407 m->m_flags = m->m_flags & (M_EXT|M_RDONLY|M_NOFREE); 408 } 409 } 410 411 /* 412 * Sanity checks on mbuf (chain) for use in KASSERT() and general 413 * debugging. 414 * Returns 0 or panics when bad and 1 on all tests passed. 415 * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they 416 * blow up later. 417 */ 418 int 419 m_sanity(struct mbuf *m0, int sanitize) 420 { 421 struct mbuf *m; 422 caddr_t a, b; 423 int pktlen = 0; 424 425 #ifdef INVARIANTS 426 #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m) 427 #else 428 #define M_SANITY_ACTION(s) printf("mbuf %p: " s, m) 429 #endif 430 431 for (m = m0; m != NULL; m = m->m_next) { 432 /* 433 * Basic pointer checks. If any of these fails then some 434 * unrelated kernel memory before or after us is trashed. 435 * No way to recover from that. 436 */ 437 a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf : 438 ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) : 439 (caddr_t)(&m->m_dat)) ); 440 b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size : 441 ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN))); 442 if ((caddr_t)m->m_data < a) 443 M_SANITY_ACTION("m_data outside mbuf data range left"); 444 if ((caddr_t)m->m_data > b) 445 M_SANITY_ACTION("m_data outside mbuf data range right"); 446 if ((caddr_t)m->m_data + m->m_len > b) 447 M_SANITY_ACTION("m_data + m_len exeeds mbuf space"); 448 449 /* m->m_nextpkt may only be set on first mbuf in chain. */ 450 if (m != m0 && m->m_nextpkt != NULL) { 451 if (sanitize) { 452 m_freem(m->m_nextpkt); 453 m->m_nextpkt = (struct mbuf *)0xDEADC0DE; 454 } else 455 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf"); 456 } 457 458 /* packet length (not mbuf length!) calculation */ 459 if (m0->m_flags & M_PKTHDR) 460 pktlen += m->m_len; 461 462 /* m_tags may only be attached to first mbuf in chain. */ 463 if (m != m0 && m->m_flags & M_PKTHDR && 464 !SLIST_EMPTY(&m->m_pkthdr.tags)) { 465 if (sanitize) { 466 m_tag_delete_chain(m, NULL); 467 /* put in 0xDEADC0DE perhaps? */ 468 } else 469 M_SANITY_ACTION("m_tags on in-chain mbuf"); 470 } 471 472 /* M_PKTHDR may only be set on first mbuf in chain */ 473 if (m != m0 && m->m_flags & M_PKTHDR) { 474 if (sanitize) { 475 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr)); 476 m->m_flags &= ~M_PKTHDR; 477 /* put in 0xDEADCODE and leave hdr flag in */ 478 } else 479 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf"); 480 } 481 } 482 m = m0; 483 if (pktlen && pktlen != m->m_pkthdr.len) { 484 if (sanitize) 485 m->m_pkthdr.len = 0; 486 else 487 M_SANITY_ACTION("m_pkthdr.len != mbuf chain length"); 488 } 489 return 1; 490 491 #undef M_SANITY_ACTION 492 } 493 494 495 /* 496 * "Move" mbuf pkthdr from "from" to "to". 497 * "from" must have M_PKTHDR set, and "to" must be empty. 498 */ 499 void 500 m_move_pkthdr(struct mbuf *to, struct mbuf *from) 501 { 502 503 #if 0 504 /* see below for why these are not enabled */ 505 M_ASSERTPKTHDR(to); 506 /* Note: with MAC, this may not be a good assertion. */ 507 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 508 ("m_move_pkthdr: to has tags")); 509 #endif 510 #ifdef MAC 511 /* 512 * XXXMAC: It could be this should also occur for non-MAC? 513 */ 514 if (to->m_flags & M_PKTHDR) 515 m_tag_delete_chain(to, NULL); 516 #endif 517 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 518 if ((to->m_flags & M_EXT) == 0) 519 to->m_data = to->m_pktdat; 520 to->m_pkthdr = from->m_pkthdr; /* especially tags */ 521 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 522 from->m_flags &= ~M_PKTHDR; 523 } 524 525 /* 526 * Duplicate "from"'s mbuf pkthdr in "to". 527 * "from" must have M_PKTHDR set, and "to" must be empty. 528 * In particular, this does a deep copy of the packet tags. 529 */ 530 int 531 m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 532 { 533 534 #if 0 535 /* 536 * The mbuf allocator only initializes the pkthdr 537 * when the mbuf is allocated with m_gethdr(). Many users 538 * (e.g. m_copy*, m_prepend) use m_get() and then 539 * smash the pkthdr as needed causing these 540 * assertions to trip. For now just disable them. 541 */ 542 M_ASSERTPKTHDR(to); 543 /* Note: with MAC, this may not be a good assertion. */ 544 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 545 #endif 546 MBUF_CHECKSLEEP(how); 547 #ifdef MAC 548 if (to->m_flags & M_PKTHDR) 549 m_tag_delete_chain(to, NULL); 550 #endif 551 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 552 if ((to->m_flags & M_EXT) == 0) 553 to->m_data = to->m_pktdat; 554 to->m_pkthdr = from->m_pkthdr; 555 SLIST_INIT(&to->m_pkthdr.tags); 556 return (m_tag_copy_chain(to, from, how)); 557 } 558 559 /* 560 * Lesser-used path for M_PREPEND: 561 * allocate new mbuf to prepend to chain, 562 * copy junk along. 563 */ 564 struct mbuf * 565 m_prepend(struct mbuf *m, int len, int how) 566 { 567 struct mbuf *mn; 568 569 if (m->m_flags & M_PKTHDR) 570 mn = m_gethdr(how, m->m_type); 571 else 572 mn = m_get(how, m->m_type); 573 if (mn == NULL) { 574 m_freem(m); 575 return (NULL); 576 } 577 if (m->m_flags & M_PKTHDR) 578 m_move_pkthdr(mn, m); 579 mn->m_next = m; 580 m = mn; 581 if(m->m_flags & M_PKTHDR) { 582 if (len < MHLEN) 583 MH_ALIGN(m, len); 584 } else { 585 if (len < MLEN) 586 M_ALIGN(m, len); 587 } 588 m->m_len = len; 589 return (m); 590 } 591 592 /* 593 * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 594 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 595 * The wait parameter is a choice of M_WAITOK/M_NOWAIT from caller. 596 * Note that the copy is read-only, because clusters are not copied, 597 * only their reference counts are incremented. 598 */ 599 struct mbuf * 600 m_copym(struct mbuf *m, int off0, int len, int wait) 601 { 602 struct mbuf *n, **np; 603 int off = off0; 604 struct mbuf *top; 605 int copyhdr = 0; 606 607 KASSERT(off >= 0, ("m_copym, negative off %d", off)); 608 KASSERT(len >= 0, ("m_copym, negative len %d", len)); 609 MBUF_CHECKSLEEP(wait); 610 if (off == 0 && m->m_flags & M_PKTHDR) 611 copyhdr = 1; 612 while (off > 0) { 613 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 614 if (off < m->m_len) 615 break; 616 off -= m->m_len; 617 m = m->m_next; 618 } 619 np = ⊤ 620 top = 0; 621 while (len > 0) { 622 if (m == NULL) { 623 KASSERT(len == M_COPYALL, 624 ("m_copym, length > size of mbuf chain")); 625 break; 626 } 627 if (copyhdr) 628 n = m_gethdr(wait, m->m_type); 629 else 630 n = m_get(wait, m->m_type); 631 *np = n; 632 if (n == NULL) 633 goto nospace; 634 if (copyhdr) { 635 if (!m_dup_pkthdr(n, m, wait)) 636 goto nospace; 637 if (len == M_COPYALL) 638 n->m_pkthdr.len -= off0; 639 else 640 n->m_pkthdr.len = len; 641 copyhdr = 0; 642 } 643 n->m_len = min(len, m->m_len - off); 644 if (m->m_flags & M_EXT) { 645 n->m_data = m->m_data + off; 646 mb_dupcl(n, m); 647 } else 648 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 649 (u_int)n->m_len); 650 if (len != M_COPYALL) 651 len -= n->m_len; 652 off = 0; 653 m = m->m_next; 654 np = &n->m_next; 655 } 656 657 return (top); 658 nospace: 659 m_freem(top); 660 return (NULL); 661 } 662 663 /* 664 * Returns mbuf chain with new head for the prepending case. 665 * Copies from mbuf (chain) n from off for len to mbuf (chain) m 666 * either prepending or appending the data. 667 * The resulting mbuf (chain) m is fully writeable. 668 * m is destination (is made writeable) 669 * n is source, off is offset in source, len is len from offset 670 * dir, 0 append, 1 prepend 671 * how, wait or nowait 672 */ 673 674 static int 675 m_bcopyxxx(void *s, void *t, u_int len) 676 { 677 bcopy(s, t, (size_t)len); 678 return 0; 679 } 680 681 struct mbuf * 682 m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len, 683 int prep, int how) 684 { 685 struct mbuf *mm, *x, *z, *prev = NULL; 686 caddr_t p; 687 int i, nlen = 0; 688 caddr_t buf[MLEN]; 689 690 KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source")); 691 KASSERT(off >= 0, ("m_copymdata, negative off %d", off)); 692 KASSERT(len >= 0, ("m_copymdata, negative len %d", len)); 693 KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep)); 694 695 mm = m; 696 if (!prep) { 697 while(mm->m_next) { 698 prev = mm; 699 mm = mm->m_next; 700 } 701 } 702 for (z = n; z != NULL; z = z->m_next) 703 nlen += z->m_len; 704 if (len == M_COPYALL) 705 len = nlen - off; 706 if (off + len > nlen || len < 1) 707 return NULL; 708 709 if (!M_WRITABLE(mm)) { 710 /* XXX: Use proper m_xxx function instead. */ 711 x = m_getcl(how, MT_DATA, mm->m_flags); 712 if (x == NULL) 713 return NULL; 714 bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size); 715 p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf); 716 x->m_data = p; 717 mm->m_next = NULL; 718 if (mm != m) 719 prev->m_next = x; 720 m_free(mm); 721 mm = x; 722 } 723 724 /* 725 * Append/prepend the data. Allocating mbufs as necessary. 726 */ 727 /* Shortcut if enough free space in first/last mbuf. */ 728 if (!prep && M_TRAILINGSPACE(mm) >= len) { 729 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) + 730 mm->m_len); 731 mm->m_len += len; 732 mm->m_pkthdr.len += len; 733 return m; 734 } 735 if (prep && M_LEADINGSPACE(mm) >= len) { 736 mm->m_data = mtod(mm, caddr_t) - len; 737 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t)); 738 mm->m_len += len; 739 mm->m_pkthdr.len += len; 740 return mm; 741 } 742 743 /* Expand first/last mbuf to cluster if possible. */ 744 if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) { 745 bcopy(mm->m_data, &buf, mm->m_len); 746 m_clget(mm, how); 747 if (!(mm->m_flags & M_EXT)) 748 return NULL; 749 bcopy(&buf, mm->m_ext.ext_buf, mm->m_len); 750 mm->m_data = mm->m_ext.ext_buf; 751 } 752 if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) { 753 bcopy(mm->m_data, &buf, mm->m_len); 754 m_clget(mm, how); 755 if (!(mm->m_flags & M_EXT)) 756 return NULL; 757 bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf + 758 mm->m_ext.ext_size - mm->m_len, mm->m_len); 759 mm->m_data = (caddr_t)mm->m_ext.ext_buf + 760 mm->m_ext.ext_size - mm->m_len; 761 } 762 763 /* Append/prepend as many mbuf (clusters) as necessary to fit len. */ 764 if (!prep && len > M_TRAILINGSPACE(mm)) { 765 if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA)) 766 return NULL; 767 } 768 if (prep && len > M_LEADINGSPACE(mm)) { 769 if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA))) 770 return NULL; 771 i = 0; 772 for (x = z; x != NULL; x = x->m_next) { 773 i += x->m_flags & M_EXT ? x->m_ext.ext_size : 774 (x->m_flags & M_PKTHDR ? MHLEN : MLEN); 775 if (!x->m_next) 776 break; 777 } 778 z->m_data += i - len; 779 m_move_pkthdr(mm, z); 780 x->m_next = mm; 781 mm = z; 782 } 783 784 /* Seek to start position in source mbuf. Optimization for long chains. */ 785 while (off > 0) { 786 if (off < n->m_len) 787 break; 788 off -= n->m_len; 789 n = n->m_next; 790 } 791 792 /* Copy data into target mbuf. */ 793 z = mm; 794 while (len > 0) { 795 KASSERT(z != NULL, ("m_copymdata, falling off target edge")); 796 i = M_TRAILINGSPACE(z); 797 m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len); 798 z->m_len += i; 799 /* fixup pkthdr.len if necessary */ 800 if ((prep ? mm : m)->m_flags & M_PKTHDR) 801 (prep ? mm : m)->m_pkthdr.len += i; 802 off += i; 803 len -= i; 804 z = z->m_next; 805 } 806 return (prep ? mm : m); 807 } 808 809 /* 810 * Copy an entire packet, including header (which must be present). 811 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 812 * Note that the copy is read-only, because clusters are not copied, 813 * only their reference counts are incremented. 814 * Preserve alignment of the first mbuf so if the creator has left 815 * some room at the beginning (e.g. for inserting protocol headers) 816 * the copies still have the room available. 817 */ 818 struct mbuf * 819 m_copypacket(struct mbuf *m, int how) 820 { 821 struct mbuf *top, *n, *o; 822 823 MBUF_CHECKSLEEP(how); 824 n = m_get(how, m->m_type); 825 top = n; 826 if (n == NULL) 827 goto nospace; 828 829 if (!m_dup_pkthdr(n, m, how)) 830 goto nospace; 831 n->m_len = m->m_len; 832 if (m->m_flags & M_EXT) { 833 n->m_data = m->m_data; 834 mb_dupcl(n, m); 835 } else { 836 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 837 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 838 } 839 840 m = m->m_next; 841 while (m) { 842 o = m_get(how, m->m_type); 843 if (o == NULL) 844 goto nospace; 845 846 n->m_next = o; 847 n = n->m_next; 848 849 n->m_len = m->m_len; 850 if (m->m_flags & M_EXT) { 851 n->m_data = m->m_data; 852 mb_dupcl(n, m); 853 } else { 854 bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 855 } 856 857 m = m->m_next; 858 } 859 return top; 860 nospace: 861 m_freem(top); 862 return (NULL); 863 } 864 865 /* 866 * Copy data from an mbuf chain starting "off" bytes from the beginning, 867 * continuing for "len" bytes, into the indicated buffer. 868 */ 869 void 870 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 871 { 872 u_int count; 873 874 KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 875 KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 876 while (off > 0) { 877 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 878 if (off < m->m_len) 879 break; 880 off -= m->m_len; 881 m = m->m_next; 882 } 883 while (len > 0) { 884 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 885 count = min(m->m_len - off, len); 886 bcopy(mtod(m, caddr_t) + off, cp, count); 887 len -= count; 888 cp += count; 889 off = 0; 890 m = m->m_next; 891 } 892 } 893 894 /* 895 * Copy a packet header mbuf chain into a completely new chain, including 896 * copying any mbuf clusters. Use this instead of m_copypacket() when 897 * you need a writable copy of an mbuf chain. 898 */ 899 struct mbuf * 900 m_dup(struct mbuf *m, int how) 901 { 902 struct mbuf **p, *top = NULL; 903 int remain, moff, nsize; 904 905 MBUF_CHECKSLEEP(how); 906 /* Sanity check */ 907 if (m == NULL) 908 return (NULL); 909 M_ASSERTPKTHDR(m); 910 911 /* While there's more data, get a new mbuf, tack it on, and fill it */ 912 remain = m->m_pkthdr.len; 913 moff = 0; 914 p = ⊤ 915 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 916 struct mbuf *n; 917 918 /* Get the next new mbuf */ 919 if (remain >= MINCLSIZE) { 920 n = m_getcl(how, m->m_type, 0); 921 nsize = MCLBYTES; 922 } else { 923 n = m_get(how, m->m_type); 924 nsize = MLEN; 925 } 926 if (n == NULL) 927 goto nospace; 928 929 if (top == NULL) { /* First one, must be PKTHDR */ 930 if (!m_dup_pkthdr(n, m, how)) { 931 m_free(n); 932 goto nospace; 933 } 934 if ((n->m_flags & M_EXT) == 0) 935 nsize = MHLEN; 936 } 937 n->m_len = 0; 938 939 /* Link it into the new chain */ 940 *p = n; 941 p = &n->m_next; 942 943 /* Copy data from original mbuf(s) into new mbuf */ 944 while (n->m_len < nsize && m != NULL) { 945 int chunk = min(nsize - n->m_len, m->m_len - moff); 946 947 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 948 moff += chunk; 949 n->m_len += chunk; 950 remain -= chunk; 951 if (moff == m->m_len) { 952 m = m->m_next; 953 moff = 0; 954 } 955 } 956 957 /* Check correct total mbuf length */ 958 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 959 ("%s: bogus m_pkthdr.len", __func__)); 960 } 961 return (top); 962 963 nospace: 964 m_freem(top); 965 return (NULL); 966 } 967 968 /* 969 * Concatenate mbuf chain n to m. 970 * Both chains must be of the same type (e.g. MT_DATA). 971 * Any m_pkthdr is not updated. 972 */ 973 void 974 m_cat(struct mbuf *m, struct mbuf *n) 975 { 976 while (m->m_next) 977 m = m->m_next; 978 while (n) { 979 if (!M_WRITABLE(m) || 980 M_TRAILINGSPACE(m) < n->m_len) { 981 /* just join the two chains */ 982 m->m_next = n; 983 return; 984 } 985 /* splat the data from one into the other */ 986 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 987 (u_int)n->m_len); 988 m->m_len += n->m_len; 989 n = m_free(n); 990 } 991 } 992 993 void 994 m_adj(struct mbuf *mp, int req_len) 995 { 996 int len = req_len; 997 struct mbuf *m; 998 int count; 999 1000 if ((m = mp) == NULL) 1001 return; 1002 if (len >= 0) { 1003 /* 1004 * Trim from head. 1005 */ 1006 while (m != NULL && len > 0) { 1007 if (m->m_len <= len) { 1008 len -= m->m_len; 1009 m->m_len = 0; 1010 m = m->m_next; 1011 } else { 1012 m->m_len -= len; 1013 m->m_data += len; 1014 len = 0; 1015 } 1016 } 1017 if (mp->m_flags & M_PKTHDR) 1018 mp->m_pkthdr.len -= (req_len - len); 1019 } else { 1020 /* 1021 * Trim from tail. Scan the mbuf chain, 1022 * calculating its length and finding the last mbuf. 1023 * If the adjustment only affects this mbuf, then just 1024 * adjust and return. Otherwise, rescan and truncate 1025 * after the remaining size. 1026 */ 1027 len = -len; 1028 count = 0; 1029 for (;;) { 1030 count += m->m_len; 1031 if (m->m_next == (struct mbuf *)0) 1032 break; 1033 m = m->m_next; 1034 } 1035 if (m->m_len >= len) { 1036 m->m_len -= len; 1037 if (mp->m_flags & M_PKTHDR) 1038 mp->m_pkthdr.len -= len; 1039 return; 1040 } 1041 count -= len; 1042 if (count < 0) 1043 count = 0; 1044 /* 1045 * Correct length for chain is "count". 1046 * Find the mbuf with last data, adjust its length, 1047 * and toss data from remaining mbufs on chain. 1048 */ 1049 m = mp; 1050 if (m->m_flags & M_PKTHDR) 1051 m->m_pkthdr.len = count; 1052 for (; m; m = m->m_next) { 1053 if (m->m_len >= count) { 1054 m->m_len = count; 1055 if (m->m_next != NULL) { 1056 m_freem(m->m_next); 1057 m->m_next = NULL; 1058 } 1059 break; 1060 } 1061 count -= m->m_len; 1062 } 1063 } 1064 } 1065 1066 /* 1067 * Rearange an mbuf chain so that len bytes are contiguous 1068 * and in the data area of an mbuf (so that mtod will work 1069 * for a structure of size len). Returns the resulting 1070 * mbuf chain on success, frees it and returns null on failure. 1071 * If there is room, it will add up to max_protohdr-len extra bytes to the 1072 * contiguous region in an attempt to avoid being called next time. 1073 */ 1074 struct mbuf * 1075 m_pullup(struct mbuf *n, int len) 1076 { 1077 struct mbuf *m; 1078 int count; 1079 int space; 1080 1081 /* 1082 * If first mbuf has no cluster, and has room for len bytes 1083 * without shifting current data, pullup into it, 1084 * otherwise allocate a new mbuf to prepend to the chain. 1085 */ 1086 if ((n->m_flags & M_EXT) == 0 && 1087 n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 1088 if (n->m_len >= len) 1089 return (n); 1090 m = n; 1091 n = n->m_next; 1092 len -= m->m_len; 1093 } else { 1094 if (len > MHLEN) 1095 goto bad; 1096 m = m_get(M_NOWAIT, n->m_type); 1097 if (m == NULL) 1098 goto bad; 1099 if (n->m_flags & M_PKTHDR) 1100 m_move_pkthdr(m, n); 1101 } 1102 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1103 do { 1104 count = min(min(max(len, max_protohdr), space), n->m_len); 1105 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 1106 (u_int)count); 1107 len -= count; 1108 m->m_len += count; 1109 n->m_len -= count; 1110 space -= count; 1111 if (n->m_len) 1112 n->m_data += count; 1113 else 1114 n = m_free(n); 1115 } while (len > 0 && n); 1116 if (len > 0) { 1117 (void) m_free(m); 1118 goto bad; 1119 } 1120 m->m_next = n; 1121 return (m); 1122 bad: 1123 m_freem(n); 1124 return (NULL); 1125 } 1126 1127 /* 1128 * Like m_pullup(), except a new mbuf is always allocated, and we allow 1129 * the amount of empty space before the data in the new mbuf to be specified 1130 * (in the event that the caller expects to prepend later). 1131 */ 1132 int MSFail; 1133 1134 struct mbuf * 1135 m_copyup(struct mbuf *n, int len, int dstoff) 1136 { 1137 struct mbuf *m; 1138 int count, space; 1139 1140 if (len > (MHLEN - dstoff)) 1141 goto bad; 1142 m = m_get(M_NOWAIT, n->m_type); 1143 if (m == NULL) 1144 goto bad; 1145 if (n->m_flags & M_PKTHDR) 1146 m_move_pkthdr(m, n); 1147 m->m_data += dstoff; 1148 space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1149 do { 1150 count = min(min(max(len, max_protohdr), space), n->m_len); 1151 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), 1152 (unsigned)count); 1153 len -= count; 1154 m->m_len += count; 1155 n->m_len -= count; 1156 space -= count; 1157 if (n->m_len) 1158 n->m_data += count; 1159 else 1160 n = m_free(n); 1161 } while (len > 0 && n); 1162 if (len > 0) { 1163 (void) m_free(m); 1164 goto bad; 1165 } 1166 m->m_next = n; 1167 return (m); 1168 bad: 1169 m_freem(n); 1170 MSFail++; 1171 return (NULL); 1172 } 1173 1174 /* 1175 * Partition an mbuf chain in two pieces, returning the tail -- 1176 * all but the first len0 bytes. In case of failure, it returns NULL and 1177 * attempts to restore the chain to its original state. 1178 * 1179 * Note that the resulting mbufs might be read-only, because the new 1180 * mbuf can end up sharing an mbuf cluster with the original mbuf if 1181 * the "breaking point" happens to lie within a cluster mbuf. Use the 1182 * M_WRITABLE() macro to check for this case. 1183 */ 1184 struct mbuf * 1185 m_split(struct mbuf *m0, int len0, int wait) 1186 { 1187 struct mbuf *m, *n; 1188 u_int len = len0, remain; 1189 1190 MBUF_CHECKSLEEP(wait); 1191 for (m = m0; m && len > m->m_len; m = m->m_next) 1192 len -= m->m_len; 1193 if (m == NULL) 1194 return (NULL); 1195 remain = m->m_len - len; 1196 if (m0->m_flags & M_PKTHDR && remain == 0) { 1197 n = m_gethdr(wait, m0->m_type); 1198 if (n == NULL) 1199 return (NULL); 1200 n->m_next = m->m_next; 1201 m->m_next = NULL; 1202 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 1203 n->m_pkthdr.len = m0->m_pkthdr.len - len0; 1204 m0->m_pkthdr.len = len0; 1205 return (n); 1206 } else if (m0->m_flags & M_PKTHDR) { 1207 n = m_gethdr(wait, m0->m_type); 1208 if (n == NULL) 1209 return (NULL); 1210 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 1211 n->m_pkthdr.len = m0->m_pkthdr.len - len0; 1212 m0->m_pkthdr.len = len0; 1213 if (m->m_flags & M_EXT) 1214 goto extpacket; 1215 if (remain > MHLEN) { 1216 /* m can't be the lead packet */ 1217 MH_ALIGN(n, 0); 1218 n->m_next = m_split(m, len, wait); 1219 if (n->m_next == NULL) { 1220 (void) m_free(n); 1221 return (NULL); 1222 } else { 1223 n->m_len = 0; 1224 return (n); 1225 } 1226 } else 1227 MH_ALIGN(n, remain); 1228 } else if (remain == 0) { 1229 n = m->m_next; 1230 m->m_next = NULL; 1231 return (n); 1232 } else { 1233 n = m_get(wait, m->m_type); 1234 if (n == NULL) 1235 return (NULL); 1236 M_ALIGN(n, remain); 1237 } 1238 extpacket: 1239 if (m->m_flags & M_EXT) { 1240 n->m_data = m->m_data + len; 1241 mb_dupcl(n, m); 1242 } else { 1243 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 1244 } 1245 n->m_len = remain; 1246 m->m_len = len; 1247 n->m_next = m->m_next; 1248 m->m_next = NULL; 1249 return (n); 1250 } 1251 /* 1252 * Routine to copy from device local memory into mbufs. 1253 * Note that `off' argument is offset into first mbuf of target chain from 1254 * which to begin copying the data to. 1255 */ 1256 struct mbuf * 1257 m_devget(char *buf, int totlen, int off, struct ifnet *ifp, 1258 void (*copy)(char *from, caddr_t to, u_int len)) 1259 { 1260 struct mbuf *m; 1261 struct mbuf *top = NULL, **mp = ⊤ 1262 int len; 1263 1264 if (off < 0 || off > MHLEN) 1265 return (NULL); 1266 1267 while (totlen > 0) { 1268 if (top == NULL) { /* First one, must be PKTHDR */ 1269 if (totlen + off >= MINCLSIZE) { 1270 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1271 len = MCLBYTES; 1272 } else { 1273 m = m_gethdr(M_NOWAIT, MT_DATA); 1274 len = MHLEN; 1275 1276 /* Place initial small packet/header at end of mbuf */ 1277 if (m && totlen + off + max_linkhdr <= MLEN) { 1278 m->m_data += max_linkhdr; 1279 len -= max_linkhdr; 1280 } 1281 } 1282 if (m == NULL) 1283 return NULL; 1284 m->m_pkthdr.rcvif = ifp; 1285 m->m_pkthdr.len = totlen; 1286 } else { 1287 if (totlen + off >= MINCLSIZE) { 1288 m = m_getcl(M_NOWAIT, MT_DATA, 0); 1289 len = MCLBYTES; 1290 } else { 1291 m = m_get(M_NOWAIT, MT_DATA); 1292 len = MLEN; 1293 } 1294 if (m == NULL) { 1295 m_freem(top); 1296 return NULL; 1297 } 1298 } 1299 if (off) { 1300 m->m_data += off; 1301 len -= off; 1302 off = 0; 1303 } 1304 m->m_len = len = min(totlen, len); 1305 if (copy) 1306 copy(buf, mtod(m, caddr_t), (u_int)len); 1307 else 1308 bcopy(buf, mtod(m, caddr_t), (u_int)len); 1309 buf += len; 1310 *mp = m; 1311 mp = &m->m_next; 1312 totlen -= len; 1313 } 1314 return (top); 1315 } 1316 1317 /* 1318 * Copy data from a buffer back into the indicated mbuf chain, 1319 * starting "off" bytes from the beginning, extending the mbuf 1320 * chain if necessary. 1321 */ 1322 void 1323 m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp) 1324 { 1325 int mlen; 1326 struct mbuf *m = m0, *n; 1327 int totlen = 0; 1328 1329 if (m0 == NULL) 1330 return; 1331 while (off > (mlen = m->m_len)) { 1332 off -= mlen; 1333 totlen += mlen; 1334 if (m->m_next == NULL) { 1335 n = m_get(M_NOWAIT, m->m_type); 1336 if (n == NULL) 1337 goto out; 1338 bzero(mtod(n, caddr_t), MLEN); 1339 n->m_len = min(MLEN, len + off); 1340 m->m_next = n; 1341 } 1342 m = m->m_next; 1343 } 1344 while (len > 0) { 1345 if (m->m_next == NULL && (len > m->m_len - off)) { 1346 m->m_len += min(len - (m->m_len - off), 1347 M_TRAILINGSPACE(m)); 1348 } 1349 mlen = min (m->m_len - off, len); 1350 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 1351 cp += mlen; 1352 len -= mlen; 1353 mlen += off; 1354 off = 0; 1355 totlen += mlen; 1356 if (len == 0) 1357 break; 1358 if (m->m_next == NULL) { 1359 n = m_get(M_NOWAIT, m->m_type); 1360 if (n == NULL) 1361 break; 1362 n->m_len = min(MLEN, len); 1363 m->m_next = n; 1364 } 1365 m = m->m_next; 1366 } 1367 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 1368 m->m_pkthdr.len = totlen; 1369 } 1370 1371 /* 1372 * Append the specified data to the indicated mbuf chain, 1373 * Extend the mbuf chain if the new data does not fit in 1374 * existing space. 1375 * 1376 * Return 1 if able to complete the job; otherwise 0. 1377 */ 1378 int 1379 m_append(struct mbuf *m0, int len, c_caddr_t cp) 1380 { 1381 struct mbuf *m, *n; 1382 int remainder, space; 1383 1384 for (m = m0; m->m_next != NULL; m = m->m_next) 1385 ; 1386 remainder = len; 1387 space = M_TRAILINGSPACE(m); 1388 if (space > 0) { 1389 /* 1390 * Copy into available space. 1391 */ 1392 if (space > remainder) 1393 space = remainder; 1394 bcopy(cp, mtod(m, caddr_t) + m->m_len, space); 1395 m->m_len += space; 1396 cp += space, remainder -= space; 1397 } 1398 while (remainder > 0) { 1399 /* 1400 * Allocate a new mbuf; could check space 1401 * and allocate a cluster instead. 1402 */ 1403 n = m_get(M_NOWAIT, m->m_type); 1404 if (n == NULL) 1405 break; 1406 n->m_len = min(MLEN, remainder); 1407 bcopy(cp, mtod(n, caddr_t), n->m_len); 1408 cp += n->m_len, remainder -= n->m_len; 1409 m->m_next = n; 1410 m = n; 1411 } 1412 if (m0->m_flags & M_PKTHDR) 1413 m0->m_pkthdr.len += len - remainder; 1414 return (remainder == 0); 1415 } 1416 1417 /* 1418 * Apply function f to the data in an mbuf chain starting "off" bytes from 1419 * the beginning, continuing for "len" bytes. 1420 */ 1421 int 1422 m_apply(struct mbuf *m, int off, int len, 1423 int (*f)(void *, void *, u_int), void *arg) 1424 { 1425 u_int count; 1426 int rval; 1427 1428 KASSERT(off >= 0, ("m_apply, negative off %d", off)); 1429 KASSERT(len >= 0, ("m_apply, negative len %d", len)); 1430 while (off > 0) { 1431 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1432 if (off < m->m_len) 1433 break; 1434 off -= m->m_len; 1435 m = m->m_next; 1436 } 1437 while (len > 0) { 1438 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1439 count = min(m->m_len - off, len); 1440 rval = (*f)(arg, mtod(m, caddr_t) + off, count); 1441 if (rval) 1442 return (rval); 1443 len -= count; 1444 off = 0; 1445 m = m->m_next; 1446 } 1447 return (0); 1448 } 1449 1450 /* 1451 * Return a pointer to mbuf/offset of location in mbuf chain. 1452 */ 1453 struct mbuf * 1454 m_getptr(struct mbuf *m, int loc, int *off) 1455 { 1456 1457 while (loc >= 0) { 1458 /* Normal end of search. */ 1459 if (m->m_len > loc) { 1460 *off = loc; 1461 return (m); 1462 } else { 1463 loc -= m->m_len; 1464 if (m->m_next == NULL) { 1465 if (loc == 0) { 1466 /* Point at the end of valid data. */ 1467 *off = m->m_len; 1468 return (m); 1469 } 1470 return (NULL); 1471 } 1472 m = m->m_next; 1473 } 1474 } 1475 return (NULL); 1476 } 1477 1478 void 1479 m_print(const struct mbuf *m, int maxlen) 1480 { 1481 int len; 1482 int pdata; 1483 const struct mbuf *m2; 1484 1485 if (m == NULL) { 1486 printf("mbuf: %p\n", m); 1487 return; 1488 } 1489 1490 if (m->m_flags & M_PKTHDR) 1491 len = m->m_pkthdr.len; 1492 else 1493 len = -1; 1494 m2 = m; 1495 while (m2 != NULL && (len == -1 || len)) { 1496 pdata = m2->m_len; 1497 if (maxlen != -1 && pdata > maxlen) 1498 pdata = maxlen; 1499 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len, 1500 m2->m_next, m2->m_flags, "\20\20freelist\17skipfw" 1501 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly" 1502 "\3eor\2pkthdr\1ext", pdata ? "" : "\n"); 1503 if (pdata) 1504 printf(", %*D\n", pdata, (u_char *)m2->m_data, "-"); 1505 if (len != -1) 1506 len -= m2->m_len; 1507 m2 = m2->m_next; 1508 } 1509 if (len > 0) 1510 printf("%d bytes unaccounted for.\n", len); 1511 return; 1512 } 1513 1514 u_int 1515 m_fixhdr(struct mbuf *m0) 1516 { 1517 u_int len; 1518 1519 len = m_length(m0, NULL); 1520 m0->m_pkthdr.len = len; 1521 return (len); 1522 } 1523 1524 u_int 1525 m_length(struct mbuf *m0, struct mbuf **last) 1526 { 1527 struct mbuf *m; 1528 u_int len; 1529 1530 len = 0; 1531 for (m = m0; m != NULL; m = m->m_next) { 1532 len += m->m_len; 1533 if (m->m_next == NULL) 1534 break; 1535 } 1536 if (last != NULL) 1537 *last = m; 1538 return (len); 1539 } 1540 1541 /* 1542 * Defragment a mbuf chain, returning the shortest possible 1543 * chain of mbufs and clusters. If allocation fails and 1544 * this cannot be completed, NULL will be returned, but 1545 * the passed in chain will be unchanged. Upon success, 1546 * the original chain will be freed, and the new chain 1547 * will be returned. 1548 * 1549 * If a non-packet header is passed in, the original 1550 * mbuf (chain?) will be returned unharmed. 1551 */ 1552 struct mbuf * 1553 m_defrag(struct mbuf *m0, int how) 1554 { 1555 struct mbuf *m_new = NULL, *m_final = NULL; 1556 int progress = 0, length; 1557 1558 MBUF_CHECKSLEEP(how); 1559 if (!(m0->m_flags & M_PKTHDR)) 1560 return (m0); 1561 1562 m_fixhdr(m0); /* Needed sanity check */ 1563 1564 #ifdef MBUF_STRESS_TEST 1565 if (m_defragrandomfailures) { 1566 int temp = arc4random() & 0xff; 1567 if (temp == 0xba) 1568 goto nospace; 1569 } 1570 #endif 1571 1572 if (m0->m_pkthdr.len > MHLEN) 1573 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1574 else 1575 m_final = m_gethdr(how, MT_DATA); 1576 1577 if (m_final == NULL) 1578 goto nospace; 1579 1580 if (m_dup_pkthdr(m_final, m0, how) == 0) 1581 goto nospace; 1582 1583 m_new = m_final; 1584 1585 while (progress < m0->m_pkthdr.len) { 1586 length = m0->m_pkthdr.len - progress; 1587 if (length > MCLBYTES) 1588 length = MCLBYTES; 1589 1590 if (m_new == NULL) { 1591 if (length > MLEN) 1592 m_new = m_getcl(how, MT_DATA, 0); 1593 else 1594 m_new = m_get(how, MT_DATA); 1595 if (m_new == NULL) 1596 goto nospace; 1597 } 1598 1599 m_copydata(m0, progress, length, mtod(m_new, caddr_t)); 1600 progress += length; 1601 m_new->m_len = length; 1602 if (m_new != m_final) 1603 m_cat(m_final, m_new); 1604 m_new = NULL; 1605 } 1606 #ifdef MBUF_STRESS_TEST 1607 if (m0->m_next == NULL) 1608 m_defraguseless++; 1609 #endif 1610 m_freem(m0); 1611 m0 = m_final; 1612 #ifdef MBUF_STRESS_TEST 1613 m_defragpackets++; 1614 m_defragbytes += m0->m_pkthdr.len; 1615 #endif 1616 return (m0); 1617 nospace: 1618 #ifdef MBUF_STRESS_TEST 1619 m_defragfailure++; 1620 #endif 1621 if (m_final) 1622 m_freem(m_final); 1623 return (NULL); 1624 } 1625 1626 /* 1627 * Defragment an mbuf chain, returning at most maxfrags separate 1628 * mbufs+clusters. If this is not possible NULL is returned and 1629 * the original mbuf chain is left in it's present (potentially 1630 * modified) state. We use two techniques: collapsing consecutive 1631 * mbufs and replacing consecutive mbufs by a cluster. 1632 * 1633 * NB: this should really be named m_defrag but that name is taken 1634 */ 1635 struct mbuf * 1636 m_collapse(struct mbuf *m0, int how, int maxfrags) 1637 { 1638 struct mbuf *m, *n, *n2, **prev; 1639 u_int curfrags; 1640 1641 /* 1642 * Calculate the current number of frags. 1643 */ 1644 curfrags = 0; 1645 for (m = m0; m != NULL; m = m->m_next) 1646 curfrags++; 1647 /* 1648 * First, try to collapse mbufs. Note that we always collapse 1649 * towards the front so we don't need to deal with moving the 1650 * pkthdr. This may be suboptimal if the first mbuf has much 1651 * less data than the following. 1652 */ 1653 m = m0; 1654 again: 1655 for (;;) { 1656 n = m->m_next; 1657 if (n == NULL) 1658 break; 1659 if (M_WRITABLE(m) && 1660 n->m_len < M_TRAILINGSPACE(m)) { 1661 bcopy(mtod(n, void *), mtod(m, char *) + m->m_len, 1662 n->m_len); 1663 m->m_len += n->m_len; 1664 m->m_next = n->m_next; 1665 m_free(n); 1666 if (--curfrags <= maxfrags) 1667 return m0; 1668 } else 1669 m = n; 1670 } 1671 KASSERT(maxfrags > 1, 1672 ("maxfrags %u, but normal collapse failed", maxfrags)); 1673 /* 1674 * Collapse consecutive mbufs to a cluster. 1675 */ 1676 prev = &m0->m_next; /* NB: not the first mbuf */ 1677 while ((n = *prev) != NULL) { 1678 if ((n2 = n->m_next) != NULL && 1679 n->m_len + n2->m_len < MCLBYTES) { 1680 m = m_getcl(how, MT_DATA, 0); 1681 if (m == NULL) 1682 goto bad; 1683 bcopy(mtod(n, void *), mtod(m, void *), n->m_len); 1684 bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len, 1685 n2->m_len); 1686 m->m_len = n->m_len + n2->m_len; 1687 m->m_next = n2->m_next; 1688 *prev = m; 1689 m_free(n); 1690 m_free(n2); 1691 if (--curfrags <= maxfrags) /* +1 cl -2 mbufs */ 1692 return m0; 1693 /* 1694 * Still not there, try the normal collapse 1695 * again before we allocate another cluster. 1696 */ 1697 goto again; 1698 } 1699 prev = &n->m_next; 1700 } 1701 /* 1702 * No place where we can collapse to a cluster; punt. 1703 * This can occur if, for example, you request 2 frags 1704 * but the packet requires that both be clusters (we 1705 * never reallocate the first mbuf to avoid moving the 1706 * packet header). 1707 */ 1708 bad: 1709 return NULL; 1710 } 1711 1712 #ifdef MBUF_STRESS_TEST 1713 1714 /* 1715 * Fragment an mbuf chain. There's no reason you'd ever want to do 1716 * this in normal usage, but it's great for stress testing various 1717 * mbuf consumers. 1718 * 1719 * If fragmentation is not possible, the original chain will be 1720 * returned. 1721 * 1722 * Possible length values: 1723 * 0 no fragmentation will occur 1724 * > 0 each fragment will be of the specified length 1725 * -1 each fragment will be the same random value in length 1726 * -2 each fragment's length will be entirely random 1727 * (Random values range from 1 to 256) 1728 */ 1729 struct mbuf * 1730 m_fragment(struct mbuf *m0, int how, int length) 1731 { 1732 struct mbuf *m_new = NULL, *m_final = NULL; 1733 int progress = 0; 1734 1735 if (!(m0->m_flags & M_PKTHDR)) 1736 return (m0); 1737 1738 if ((length == 0) || (length < -2)) 1739 return (m0); 1740 1741 m_fixhdr(m0); /* Needed sanity check */ 1742 1743 m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1744 1745 if (m_final == NULL) 1746 goto nospace; 1747 1748 if (m_dup_pkthdr(m_final, m0, how) == 0) 1749 goto nospace; 1750 1751 m_new = m_final; 1752 1753 if (length == -1) 1754 length = 1 + (arc4random() & 255); 1755 1756 while (progress < m0->m_pkthdr.len) { 1757 int fraglen; 1758 1759 if (length > 0) 1760 fraglen = length; 1761 else 1762 fraglen = 1 + (arc4random() & 255); 1763 if (fraglen > m0->m_pkthdr.len - progress) 1764 fraglen = m0->m_pkthdr.len - progress; 1765 1766 if (fraglen > MCLBYTES) 1767 fraglen = MCLBYTES; 1768 1769 if (m_new == NULL) { 1770 m_new = m_getcl(how, MT_DATA, 0); 1771 if (m_new == NULL) 1772 goto nospace; 1773 } 1774 1775 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t)); 1776 progress += fraglen; 1777 m_new->m_len = fraglen; 1778 if (m_new != m_final) 1779 m_cat(m_final, m_new); 1780 m_new = NULL; 1781 } 1782 m_freem(m0); 1783 m0 = m_final; 1784 return (m0); 1785 nospace: 1786 if (m_final) 1787 m_freem(m_final); 1788 /* Return the original chain on failure */ 1789 return (m0); 1790 } 1791 1792 #endif 1793 1794 /* 1795 * Copy the contents of uio into a properly sized mbuf chain. 1796 */ 1797 struct mbuf * 1798 m_uiotombuf(struct uio *uio, int how, int len, int align, int flags) 1799 { 1800 struct mbuf *m, *mb; 1801 int error, length; 1802 ssize_t total; 1803 int progress = 0; 1804 1805 /* 1806 * len can be zero or an arbitrary large value bound by 1807 * the total data supplied by the uio. 1808 */ 1809 if (len > 0) 1810 total = min(uio->uio_resid, len); 1811 else 1812 total = uio->uio_resid; 1813 1814 /* 1815 * The smallest unit returned by m_getm2() is a single mbuf 1816 * with pkthdr. We can't align past it. 1817 */ 1818 if (align >= MHLEN) 1819 return (NULL); 1820 1821 /* 1822 * Give us the full allocation or nothing. 1823 * If len is zero return the smallest empty mbuf. 1824 */ 1825 m = m_getm2(NULL, max(total + align, 1), how, MT_DATA, flags); 1826 if (m == NULL) 1827 return (NULL); 1828 m->m_data += align; 1829 1830 /* Fill all mbufs with uio data and update header information. */ 1831 for (mb = m; mb != NULL; mb = mb->m_next) { 1832 length = min(M_TRAILINGSPACE(mb), total - progress); 1833 1834 error = uiomove(mtod(mb, void *), length, uio); 1835 if (error) { 1836 m_freem(m); 1837 return (NULL); 1838 } 1839 1840 mb->m_len = length; 1841 progress += length; 1842 if (flags & M_PKTHDR) 1843 m->m_pkthdr.len += length; 1844 } 1845 KASSERT(progress == total, ("%s: progress != total", __func__)); 1846 1847 return (m); 1848 } 1849 1850 /* 1851 * Copy an mbuf chain into a uio limited by len if set. 1852 */ 1853 int 1854 m_mbuftouio(struct uio *uio, struct mbuf *m, int len) 1855 { 1856 int error, length, total; 1857 int progress = 0; 1858 1859 if (len > 0) 1860 total = min(uio->uio_resid, len); 1861 else 1862 total = uio->uio_resid; 1863 1864 /* Fill the uio with data from the mbufs. */ 1865 for (; m != NULL; m = m->m_next) { 1866 length = min(m->m_len, total - progress); 1867 1868 error = uiomove(mtod(m, void *), length, uio); 1869 if (error) 1870 return (error); 1871 1872 progress += length; 1873 } 1874 1875 return (0); 1876 } 1877 1878 /* 1879 * Set the m_data pointer of a newly-allocated mbuf 1880 * to place an object of the specified size at the 1881 * end of the mbuf, longword aligned. 1882 */ 1883 void 1884 m_align(struct mbuf *m, int len) 1885 { 1886 #ifdef INVARIANTS 1887 const char *msg = "%s: not a virgin mbuf"; 1888 #endif 1889 int adjust; 1890 1891 if (m->m_flags & M_EXT) { 1892 KASSERT(m->m_data == m->m_ext.ext_buf, (msg, __func__)); 1893 adjust = m->m_ext.ext_size - len; 1894 } else if (m->m_flags & M_PKTHDR) { 1895 KASSERT(m->m_data == m->m_pktdat, (msg, __func__)); 1896 adjust = MHLEN - len; 1897 } else { 1898 KASSERT(m->m_data == m->m_dat, (msg, __func__)); 1899 adjust = MLEN - len; 1900 } 1901 1902 m->m_data += adjust &~ (sizeof(long)-1); 1903 } 1904 1905 /* 1906 * Create a writable copy of the mbuf chain. While doing this 1907 * we compact the chain with a goal of producing a chain with 1908 * at most two mbufs. The second mbuf in this chain is likely 1909 * to be a cluster. The primary purpose of this work is to create 1910 * a writable packet for encryption, compression, etc. The 1911 * secondary goal is to linearize the data so the data can be 1912 * passed to crypto hardware in the most efficient manner possible. 1913 */ 1914 struct mbuf * 1915 m_unshare(struct mbuf *m0, int how) 1916 { 1917 struct mbuf *m, *mprev; 1918 struct mbuf *n, *mfirst, *mlast; 1919 int len, off; 1920 1921 mprev = NULL; 1922 for (m = m0; m != NULL; m = mprev->m_next) { 1923 /* 1924 * Regular mbufs are ignored unless there's a cluster 1925 * in front of it that we can use to coalesce. We do 1926 * the latter mainly so later clusters can be coalesced 1927 * also w/o having to handle them specially (i.e. convert 1928 * mbuf+cluster -> cluster). This optimization is heavily 1929 * influenced by the assumption that we're running over 1930 * Ethernet where MCLBYTES is large enough that the max 1931 * packet size will permit lots of coalescing into a 1932 * single cluster. This in turn permits efficient 1933 * crypto operations, especially when using hardware. 1934 */ 1935 if ((m->m_flags & M_EXT) == 0) { 1936 if (mprev && (mprev->m_flags & M_EXT) && 1937 m->m_len <= M_TRAILINGSPACE(mprev)) { 1938 /* XXX: this ignores mbuf types */ 1939 memcpy(mtod(mprev, caddr_t) + mprev->m_len, 1940 mtod(m, caddr_t), m->m_len); 1941 mprev->m_len += m->m_len; 1942 mprev->m_next = m->m_next; /* unlink from chain */ 1943 m_free(m); /* reclaim mbuf */ 1944 #if 0 1945 newipsecstat.ips_mbcoalesced++; 1946 #endif 1947 } else { 1948 mprev = m; 1949 } 1950 continue; 1951 } 1952 /* 1953 * Writable mbufs are left alone (for now). 1954 */ 1955 if (M_WRITABLE(m)) { 1956 mprev = m; 1957 continue; 1958 } 1959 1960 /* 1961 * Not writable, replace with a copy or coalesce with 1962 * the previous mbuf if possible (since we have to copy 1963 * it anyway, we try to reduce the number of mbufs and 1964 * clusters so that future work is easier). 1965 */ 1966 KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags)); 1967 /* NB: we only coalesce into a cluster or larger */ 1968 if (mprev != NULL && (mprev->m_flags & M_EXT) && 1969 m->m_len <= M_TRAILINGSPACE(mprev)) { 1970 /* XXX: this ignores mbuf types */ 1971 memcpy(mtod(mprev, caddr_t) + mprev->m_len, 1972 mtod(m, caddr_t), m->m_len); 1973 mprev->m_len += m->m_len; 1974 mprev->m_next = m->m_next; /* unlink from chain */ 1975 m_free(m); /* reclaim mbuf */ 1976 #if 0 1977 newipsecstat.ips_clcoalesced++; 1978 #endif 1979 continue; 1980 } 1981 1982 /* 1983 * Allocate new space to hold the copy and copy the data. 1984 * We deal with jumbo mbufs (i.e. m_len > MCLBYTES) by 1985 * splitting them into clusters. We could just malloc a 1986 * buffer and make it external but too many device drivers 1987 * don't know how to break up the non-contiguous memory when 1988 * doing DMA. 1989 */ 1990 n = m_getcl(how, m->m_type, m->m_flags); 1991 if (n == NULL) { 1992 m_freem(m0); 1993 return (NULL); 1994 } 1995 len = m->m_len; 1996 off = 0; 1997 mfirst = n; 1998 mlast = NULL; 1999 for (;;) { 2000 int cc = min(len, MCLBYTES); 2001 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc); 2002 n->m_len = cc; 2003 if (mlast != NULL) 2004 mlast->m_next = n; 2005 mlast = n; 2006 #if 0 2007 newipsecstat.ips_clcopied++; 2008 #endif 2009 2010 len -= cc; 2011 if (len <= 0) 2012 break; 2013 off += cc; 2014 2015 n = m_getcl(how, m->m_type, m->m_flags); 2016 if (n == NULL) { 2017 m_freem(mfirst); 2018 m_freem(m0); 2019 return (NULL); 2020 } 2021 } 2022 n->m_next = m->m_next; 2023 if (mprev == NULL) 2024 m0 = mfirst; /* new head of chain */ 2025 else 2026 mprev->m_next = mfirst; /* replace old mbuf */ 2027 m_free(m); /* release old mbuf */ 2028 mprev = mfirst; 2029 } 2030 return (m0); 2031 } 2032 2033 #ifdef MBUF_PROFILING 2034 2035 #define MP_BUCKETS 32 /* don't just change this as things may overflow.*/ 2036 struct mbufprofile { 2037 uintmax_t wasted[MP_BUCKETS]; 2038 uintmax_t used[MP_BUCKETS]; 2039 uintmax_t segments[MP_BUCKETS]; 2040 } mbprof; 2041 2042 #define MP_MAXDIGITS 21 /* strlen("16,000,000,000,000,000,000") == 21 */ 2043 #define MP_NUMLINES 6 2044 #define MP_NUMSPERLINE 16 2045 #define MP_EXTRABYTES 64 /* > strlen("used:\nwasted:\nsegments:\n") */ 2046 /* work out max space needed and add a bit of spare space too */ 2047 #define MP_MAXLINE ((MP_MAXDIGITS+1) * MP_NUMSPERLINE) 2048 #define MP_BUFSIZE ((MP_MAXLINE * MP_NUMLINES) + 1 + MP_EXTRABYTES) 2049 2050 char mbprofbuf[MP_BUFSIZE]; 2051 2052 void 2053 m_profile(struct mbuf *m) 2054 { 2055 int segments = 0; 2056 int used = 0; 2057 int wasted = 0; 2058 2059 while (m) { 2060 segments++; 2061 used += m->m_len; 2062 if (m->m_flags & M_EXT) { 2063 wasted += MHLEN - sizeof(m->m_ext) + 2064 m->m_ext.ext_size - m->m_len; 2065 } else { 2066 if (m->m_flags & M_PKTHDR) 2067 wasted += MHLEN - m->m_len; 2068 else 2069 wasted += MLEN - m->m_len; 2070 } 2071 m = m->m_next; 2072 } 2073 /* be paranoid.. it helps */ 2074 if (segments > MP_BUCKETS - 1) 2075 segments = MP_BUCKETS - 1; 2076 if (used > 100000) 2077 used = 100000; 2078 if (wasted > 100000) 2079 wasted = 100000; 2080 /* store in the appropriate bucket */ 2081 /* don't bother locking. if it's slightly off, so what? */ 2082 mbprof.segments[segments]++; 2083 mbprof.used[fls(used)]++; 2084 mbprof.wasted[fls(wasted)]++; 2085 } 2086 2087 static void 2088 mbprof_textify(void) 2089 { 2090 int offset; 2091 char *c; 2092 uint64_t *p; 2093 2094 p = &mbprof.wasted[0]; 2095 c = mbprofbuf; 2096 offset = snprintf(c, MP_MAXLINE + 10, 2097 "wasted:\n" 2098 "%ju %ju %ju %ju %ju %ju %ju %ju " 2099 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2100 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2101 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2102 #ifdef BIG_ARRAY 2103 p = &mbprof.wasted[16]; 2104 c += offset; 2105 offset = snprintf(c, MP_MAXLINE, 2106 "%ju %ju %ju %ju %ju %ju %ju %ju " 2107 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2108 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2109 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2110 #endif 2111 p = &mbprof.used[0]; 2112 c += offset; 2113 offset = snprintf(c, MP_MAXLINE + 10, 2114 "used:\n" 2115 "%ju %ju %ju %ju %ju %ju %ju %ju " 2116 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2117 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2118 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2119 #ifdef BIG_ARRAY 2120 p = &mbprof.used[16]; 2121 c += offset; 2122 offset = snprintf(c, MP_MAXLINE, 2123 "%ju %ju %ju %ju %ju %ju %ju %ju " 2124 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2125 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2126 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2127 #endif 2128 p = &mbprof.segments[0]; 2129 c += offset; 2130 offset = snprintf(c, MP_MAXLINE + 10, 2131 "segments:\n" 2132 "%ju %ju %ju %ju %ju %ju %ju %ju " 2133 "%ju %ju %ju %ju %ju %ju %ju %ju\n", 2134 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2135 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2136 #ifdef BIG_ARRAY 2137 p = &mbprof.segments[16]; 2138 c += offset; 2139 offset = snprintf(c, MP_MAXLINE, 2140 "%ju %ju %ju %ju %ju %ju %ju %ju " 2141 "%ju %ju %ju %ju %ju %ju %ju %jju", 2142 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 2143 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]); 2144 #endif 2145 } 2146 2147 static int 2148 mbprof_handler(SYSCTL_HANDLER_ARGS) 2149 { 2150 int error; 2151 2152 mbprof_textify(); 2153 error = SYSCTL_OUT(req, mbprofbuf, strlen(mbprofbuf) + 1); 2154 return (error); 2155 } 2156 2157 static int 2158 mbprof_clr_handler(SYSCTL_HANDLER_ARGS) 2159 { 2160 int clear, error; 2161 2162 clear = 0; 2163 error = sysctl_handle_int(oidp, &clear, 0, req); 2164 if (error || !req->newptr) 2165 return (error); 2166 2167 if (clear) { 2168 bzero(&mbprof, sizeof(mbprof)); 2169 } 2170 2171 return (error); 2172 } 2173 2174 2175 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofile, CTLTYPE_STRING|CTLFLAG_RD, 2176 NULL, 0, mbprof_handler, "A", "mbuf profiling statistics"); 2177 2178 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofileclr, CTLTYPE_INT|CTLFLAG_RW, 2179 NULL, 0, mbprof_clr_handler, "I", "clear mbuf profiling statistics"); 2180 #endif 2181 2182