2 * Copyright (c) 1982, 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_param.h"
36 #include "opt_mbuf_stress_test.h"
37 #include "opt_mbuf_profiling.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
44 #include <sys/malloc.h>
46 #include <sys/sysctl.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
55 #ifdef MBUF_STRESS_TEST
60 int m_defragrandomfailures;
64 * sysctl(8) exported objects
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, "");
88 * Ensure the correct size of various mbuf parameters. It could be off due
89 * to compiler-induced padding and alignment artifacts.
91 CTASSERT(MSIZE - offsetof(struct mbuf, m_dat) == MLEN);
92 CTASSERT(MSIZE - offsetof(struct mbuf, m_pktdat) == MHLEN);
95 * mbuf data storage should be 64-bit aligned regardless of architectural
96 * pointer size; check this is the case with and without a packet header.
98 CTASSERT(offsetof(struct mbuf, m_dat) % 8 == 0);
99 CTASSERT(offsetof(struct mbuf, m_pktdat) % 8 == 0);
102 * While the specific values here don't matter too much (i.e., +/- a few
103 * words), we do want to ensure that changes to these values are carefully
104 * reasoned about and properly documented. This is especially the case as
105 * network-protocol and device-driver modules encode these layouts, and must
106 * be recompiled if the structures change. Check these values at compile time
107 * against the ones documented in comments in mbuf.h.
109 * NB: Possibly they should be documented there via #define's and not just
112 #if defined(__LP64__)
113 CTASSERT(offsetof(struct mbuf, m_dat) == 32);
114 CTASSERT(sizeof(struct pkthdr) == 56);
115 CTASSERT(sizeof(struct m_ext) == 48);
117 CTASSERT(offsetof(struct mbuf, m_dat) == 24);
118 CTASSERT(sizeof(struct pkthdr) == 48);
119 CTASSERT(sizeof(struct m_ext) == 28);
123 * Assert that the queue(3) macros produce code of the same size as an old
124 * plain pointer does.
127 static struct mbuf m_assertbuf;
128 CTASSERT(sizeof(m_assertbuf.m_slist) == sizeof(m_assertbuf.m_next));
129 CTASSERT(sizeof(m_assertbuf.m_stailq) == sizeof(m_assertbuf.m_next));
130 CTASSERT(sizeof(m_assertbuf.m_slistpkt) == sizeof(m_assertbuf.m_nextpkt));
131 CTASSERT(sizeof(m_assertbuf.m_stailqpkt) == sizeof(m_assertbuf.m_nextpkt));
135 * m_get2() allocates minimum mbuf that would fit "size" argument.
138 m_get2(int size, int how, short type, int flags)
146 if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0))
147 return (uma_zalloc_arg(zone_mbuf, &args, how));
148 if (size <= MCLBYTES)
149 return (uma_zalloc_arg(zone_pack, &args, how));
151 if (size > MJUMPAGESIZE)
154 m = uma_zalloc_arg(zone_mbuf, &args, how);
158 n = uma_zalloc_arg(zone_jumbop, m, how);
160 uma_zfree(zone_mbuf, m);
168 * m_getjcl() returns an mbuf with a cluster of the specified size attached.
169 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
172 m_getjcl(int how, short type, int flags, int size)
178 if (size == MCLBYTES)
179 return m_getcl(how, type, flags);
184 m = uma_zalloc_arg(zone_mbuf, &args, how);
188 zone = m_getzone(size);
189 n = uma_zalloc_arg(zone, m, how);
191 uma_zfree(zone_mbuf, m);
198 * Allocate a given length worth of mbufs and/or clusters (whatever fits
199 * best) and return a pointer to the top of the allocated chain. If an
200 * existing mbuf chain is provided, then we will append the new chain
201 * to the existing one but still return the top of the newly allocated
205 m_getm2(struct mbuf *m, int len, int how, short type, int flags)
207 struct mbuf *mb, *nm = NULL, *mtail = NULL;
209 KASSERT(len >= 0, ("%s: len is < 0", __func__));
211 /* Validate flags. */
212 flags &= (M_PKTHDR | M_EOR);
214 /* Packet header mbuf must be first in chain. */
215 if ((flags & M_PKTHDR) && m != NULL)
218 /* Loop and append maximum sized mbufs to the chain tail. */
221 mb = m_getjcl(how, type, (flags & M_PKTHDR),
223 else if (len >= MINCLSIZE)
224 mb = m_getcl(how, type, (flags & M_PKTHDR));
225 else if (flags & M_PKTHDR)
226 mb = m_gethdr(how, type);
228 mb = m_get(how, type);
230 /* Fail the whole operation if one mbuf can't be allocated. */
244 flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */
247 mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */
249 /* If mbuf was supplied, append new chain to the end of it. */
251 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
254 mtail->m_flags &= ~M_EOR;
262 * Free an entire chain of mbufs and associated external buffers, if
266 m_freem(struct mbuf *mb)
274 * Configure a provided mbuf to refer to the provided external storage
275 * buffer and setup a reference count for said buffer. If the setting
276 * up of the reference count fails, the M_EXT bit will not be set. If
277 * successfull, the M_EXT bit is set in the mbuf's flags.
280 * mb The existing mbuf to which to attach the provided buffer.
281 * buf The address of the provided external storage buffer.
282 * size The size of the provided buffer.
283 * freef A pointer to a routine that is responsible for freeing the
284 * provided external storage buffer.
285 * args A pointer to an argument structure (of any type) to be passed
286 * to the provided freef routine (may be NULL).
287 * flags Any other flags to be passed to the provided mbuf.
288 * type The type that the external storage buffer should be
295 m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
296 void (*freef)(struct mbuf *, void *, void *), void *arg1, void *arg2,
297 int flags, int type, int wait)
299 KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
301 if (type != EXT_EXTREF)
302 mb->m_ext.ext_cnt = uma_zalloc(zone_ext_refcnt, wait);
304 if (mb->m_ext.ext_cnt == NULL)
307 *(mb->m_ext.ext_cnt) = 1;
308 mb->m_flags |= (M_EXT | flags);
309 mb->m_ext.ext_buf = buf;
310 mb->m_data = mb->m_ext.ext_buf;
311 mb->m_ext.ext_size = size;
312 mb->m_ext.ext_free = freef;
313 mb->m_ext.ext_arg1 = arg1;
314 mb->m_ext.ext_arg2 = arg2;
315 mb->m_ext.ext_type = type;
316 mb->m_ext.ext_flags = 0;
322 * Non-directly-exported function to clean up after mbufs with M_EXT
323 * storage attached to them if the reference count hits 1.
326 mb_free_ext(struct mbuf *m)
330 KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m));
333 * Check if the header is embedded in the cluster.
335 freembuf = (m->m_flags & M_NOFREE) ? 0 : 1;
337 switch (m->m_ext.ext_type) {
339 sf_ext_free(m->m_ext.ext_arg1, m->m_ext.ext_arg2);
341 case EXT_SFBUF_NOCACHE:
342 sf_ext_free_nocache(m->m_ext.ext_arg1, m->m_ext.ext_arg2);
345 KASSERT(m->m_ext.ext_cnt != NULL,
346 ("%s: no refcounting pointer on %p", __func__, m));
348 * Free attached storage if this mbuf is the only
351 if (*(m->m_ext.ext_cnt) != 1) {
352 if (atomic_fetchadd_int(m->m_ext.ext_cnt, -1) != 1)
356 switch (m->m_ext.ext_type) {
357 case EXT_PACKET: /* The packet zone is special. */
358 if (*(m->m_ext.ext_cnt) == 0)
359 *(m->m_ext.ext_cnt) = 1;
360 uma_zfree(zone_pack, m);
361 return; /* Job done. */
363 uma_zfree(zone_clust, m->m_ext.ext_buf);
366 uma_zfree(zone_jumbop, m->m_ext.ext_buf);
369 uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
372 uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
377 *(m->m_ext.ext_cnt) = 0;
378 uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *,
382 KASSERT(m->m_ext.ext_free != NULL,
383 ("%s: ext_free not set", __func__));
384 (*(m->m_ext.ext_free))(m, m->m_ext.ext_arg1,
388 KASSERT(m->m_ext.ext_type == 0,
389 ("%s: unknown ext_type", __func__));
394 uma_zfree(zone_mbuf, m);
398 * Attach the cluster from *m to *n, set up m_ext in *n
399 * and bump the refcount of the cluster.
402 mb_dupcl(struct mbuf *n, const struct mbuf *m)
405 KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m));
406 KASSERT(!(n->m_flags & M_EXT), ("%s: M_EXT set on %p", __func__, n));
408 switch (m->m_ext.ext_type) {
410 case EXT_SFBUF_NOCACHE:
411 sf_ext_ref(m->m_ext.ext_arg1, m->m_ext.ext_arg2);
414 KASSERT(m->m_ext.ext_cnt != NULL,
415 ("%s: no refcounting pointer on %p", __func__, m));
416 if (*(m->m_ext.ext_cnt) == 1)
417 *(m->m_ext.ext_cnt) += 1;
419 atomic_add_int(m->m_ext.ext_cnt, 1);
424 n->m_flags |= m->m_flags & M_RDONLY;
428 m_demote_pkthdr(struct mbuf *m)
433 m_tag_delete_chain(m, NULL);
434 m->m_flags &= ~M_PKTHDR;
435 bzero(&m->m_pkthdr, sizeof(struct pkthdr));
439 * Clean up mbuf (chain) from any tags and packet headers.
440 * If "all" is set then the first mbuf in the chain will be
444 m_demote(struct mbuf *m0, int all, int flags)
448 for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) {
449 KASSERT(m->m_nextpkt == NULL, ("%s: m_nextpkt in m %p, m0 %p",
451 if (m->m_flags & M_PKTHDR)
453 m->m_flags = m->m_flags & (M_EXT | M_RDONLY | M_NOFREE | flags);
458 * Sanity checks on mbuf (chain) for use in KASSERT() and general
460 * Returns 0 or panics when bad and 1 on all tests passed.
461 * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they
465 m_sanity(struct mbuf *m0, int sanitize)
472 #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m)
474 #define M_SANITY_ACTION(s) printf("mbuf %p: " s, m)
477 for (m = m0; m != NULL; m = m->m_next) {
479 * Basic pointer checks. If any of these fails then some
480 * unrelated kernel memory before or after us is trashed.
481 * No way to recover from that.
485 if ((caddr_t)m->m_data < a)
486 M_SANITY_ACTION("m_data outside mbuf data range left");
487 if ((caddr_t)m->m_data > b)
488 M_SANITY_ACTION("m_data outside mbuf data range right");
489 if ((caddr_t)m->m_data + m->m_len > b)
490 M_SANITY_ACTION("m_data + m_len exeeds mbuf space");
492 /* m->m_nextpkt may only be set on first mbuf in chain. */
493 if (m != m0 && m->m_nextpkt != NULL) {
495 m_freem(m->m_nextpkt);
496 m->m_nextpkt = (struct mbuf *)0xDEADC0DE;
498 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf");
501 /* packet length (not mbuf length!) calculation */
502 if (m0->m_flags & M_PKTHDR)
505 /* m_tags may only be attached to first mbuf in chain. */
506 if (m != m0 && m->m_flags & M_PKTHDR &&
507 !SLIST_EMPTY(&m->m_pkthdr.tags)) {
509 m_tag_delete_chain(m, NULL);
510 /* put in 0xDEADC0DE perhaps? */
512 M_SANITY_ACTION("m_tags on in-chain mbuf");
515 /* M_PKTHDR may only be set on first mbuf in chain */
516 if (m != m0 && m->m_flags & M_PKTHDR) {
518 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
519 m->m_flags &= ~M_PKTHDR;
520 /* put in 0xDEADCODE and leave hdr flag in */
522 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf");
526 if (pktlen && pktlen != m->m_pkthdr.len) {
530 M_SANITY_ACTION("m_pkthdr.len != mbuf chain length");
534 #undef M_SANITY_ACTION
539 * "Move" mbuf pkthdr from "from" to "to".
540 * "from" must have M_PKTHDR set, and "to" must be empty.
543 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
547 /* see below for why these are not enabled */
549 /* Note: with MAC, this may not be a good assertion. */
550 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
551 ("m_move_pkthdr: to has tags"));
555 * XXXMAC: It could be this should also occur for non-MAC?
557 if (to->m_flags & M_PKTHDR)
558 m_tag_delete_chain(to, NULL);
560 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
561 if ((to->m_flags & M_EXT) == 0)
562 to->m_data = to->m_pktdat;
563 to->m_pkthdr = from->m_pkthdr; /* especially tags */
564 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */
565 from->m_flags &= ~M_PKTHDR;
569 * Duplicate "from"'s mbuf pkthdr in "to".
570 * "from" must have M_PKTHDR set, and "to" must be empty.
571 * In particular, this does a deep copy of the packet tags.
574 m_dup_pkthdr(struct mbuf *to, const struct mbuf *from, int how)
579 * The mbuf allocator only initializes the pkthdr
580 * when the mbuf is allocated with m_gethdr(). Many users
581 * (e.g. m_copy*, m_prepend) use m_get() and then
582 * smash the pkthdr as needed causing these
583 * assertions to trip. For now just disable them.
586 /* Note: with MAC, this may not be a good assertion. */
587 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
589 MBUF_CHECKSLEEP(how);
591 if (to->m_flags & M_PKTHDR)
592 m_tag_delete_chain(to, NULL);
594 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
595 if ((to->m_flags & M_EXT) == 0)
596 to->m_data = to->m_pktdat;
597 to->m_pkthdr = from->m_pkthdr;
598 SLIST_INIT(&to->m_pkthdr.tags);
599 return (m_tag_copy_chain(to, from, how));
603 * Lesser-used path for M_PREPEND:
604 * allocate new mbuf to prepend to chain,
608 m_prepend(struct mbuf *m, int len, int how)
612 if (m->m_flags & M_PKTHDR)
613 mn = m_gethdr(how, m->m_type);
615 mn = m_get(how, m->m_type);
620 if (m->m_flags & M_PKTHDR)
621 m_move_pkthdr(mn, m);
631 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
632 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
633 * The wait parameter is a choice of M_WAITOK/M_NOWAIT from caller.
634 * Note that the copy is read-only, because clusters are not copied,
635 * only their reference counts are incremented.
638 m_copym(const struct mbuf *m, int off0, int len, int wait)
640 struct mbuf *n, **np;
645 KASSERT(off >= 0, ("m_copym, negative off %d", off));
646 KASSERT(len >= 0, ("m_copym, negative len %d", len));
647 MBUF_CHECKSLEEP(wait);
648 if (off == 0 && m->m_flags & M_PKTHDR)
651 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
661 KASSERT(len == M_COPYALL,
662 ("m_copym, length > size of mbuf chain"));
666 n = m_gethdr(wait, m->m_type);
668 n = m_get(wait, m->m_type);
673 if (!m_dup_pkthdr(n, m, wait))
675 if (len == M_COPYALL)
676 n->m_pkthdr.len -= off0;
678 n->m_pkthdr.len = len;
681 n->m_len = min(len, m->m_len - off);
682 if (m->m_flags & M_EXT) {
683 n->m_data = m->m_data + off;
686 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
688 if (len != M_COPYALL)
702 * Copy an entire packet, including header (which must be present).
703 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
704 * Note that the copy is read-only, because clusters are not copied,
705 * only their reference counts are incremented.
706 * Preserve alignment of the first mbuf so if the creator has left
707 * some room at the beginning (e.g. for inserting protocol headers)
708 * the copies still have the room available.
711 m_copypacket(struct mbuf *m, int how)
713 struct mbuf *top, *n, *o;
715 MBUF_CHECKSLEEP(how);
716 n = m_get(how, m->m_type);
721 if (!m_dup_pkthdr(n, m, how))
724 if (m->m_flags & M_EXT) {
725 n->m_data = m->m_data;
728 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
729 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
734 o = m_get(how, m->m_type);
742 if (m->m_flags & M_EXT) {
743 n->m_data = m->m_data;
746 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
758 * Copy data from an mbuf chain starting "off" bytes from the beginning,
759 * continuing for "len" bytes, into the indicated buffer.
762 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
766 KASSERT(off >= 0, ("m_copydata, negative off %d", off));
767 KASSERT(len >= 0, ("m_copydata, negative len %d", len));
769 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
776 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
777 count = min(m->m_len - off, len);
778 bcopy(mtod(m, caddr_t) + off, cp, count);
787 * Copy a packet header mbuf chain into a completely new chain, including
788 * copying any mbuf clusters. Use this instead of m_copypacket() when
789 * you need a writable copy of an mbuf chain.
792 m_dup(const struct mbuf *m, int how)
794 struct mbuf **p, *top = NULL;
795 int remain, moff, nsize;
797 MBUF_CHECKSLEEP(how);
803 /* While there's more data, get a new mbuf, tack it on, and fill it */
804 remain = m->m_pkthdr.len;
807 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */
810 /* Get the next new mbuf */
811 if (remain >= MINCLSIZE) {
812 n = m_getcl(how, m->m_type, 0);
815 n = m_get(how, m->m_type);
821 if (top == NULL) { /* First one, must be PKTHDR */
822 if (!m_dup_pkthdr(n, m, how)) {
826 if ((n->m_flags & M_EXT) == 0)
828 n->m_flags &= ~M_RDONLY;
832 /* Link it into the new chain */
836 /* Copy data from original mbuf(s) into new mbuf */
837 while (n->m_len < nsize && m != NULL) {
838 int chunk = min(nsize - n->m_len, m->m_len - moff);
840 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
844 if (moff == m->m_len) {
850 /* Check correct total mbuf length */
851 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
852 ("%s: bogus m_pkthdr.len", __func__));
862 * Concatenate mbuf chain n to m.
863 * Both chains must be of the same type (e.g. MT_DATA).
864 * Any m_pkthdr is not updated.
867 m_cat(struct mbuf *m, struct mbuf *n)
872 if (!M_WRITABLE(m) ||
873 M_TRAILINGSPACE(m) < n->m_len) {
874 /* just join the two chains */
878 /* splat the data from one into the other */
879 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
881 m->m_len += n->m_len;
887 * Concatenate two pkthdr mbuf chains.
890 m_catpkt(struct mbuf *m, struct mbuf *n)
896 m->m_pkthdr.len += n->m_pkthdr.len;
903 m_adj(struct mbuf *mp, int req_len)
909 if ((m = mp) == NULL)
915 while (m != NULL && len > 0) {
916 if (m->m_len <= len) {
926 if (mp->m_flags & M_PKTHDR)
927 mp->m_pkthdr.len -= (req_len - len);
930 * Trim from tail. Scan the mbuf chain,
931 * calculating its length and finding the last mbuf.
932 * If the adjustment only affects this mbuf, then just
933 * adjust and return. Otherwise, rescan and truncate
934 * after the remaining size.
940 if (m->m_next == (struct mbuf *)0)
944 if (m->m_len >= len) {
946 if (mp->m_flags & M_PKTHDR)
947 mp->m_pkthdr.len -= len;
954 * Correct length for chain is "count".
955 * Find the mbuf with last data, adjust its length,
956 * and toss data from remaining mbufs on chain.
959 if (m->m_flags & M_PKTHDR)
960 m->m_pkthdr.len = count;
961 for (; m; m = m->m_next) {
962 if (m->m_len >= count) {
964 if (m->m_next != NULL) {
976 * Rearange an mbuf chain so that len bytes are contiguous
977 * and in the data area of an mbuf (so that mtod will work
978 * for a structure of size len). Returns the resulting
979 * mbuf chain on success, frees it and returns null on failure.
980 * If there is room, it will add up to max_protohdr-len extra bytes to the
981 * contiguous region in an attempt to avoid being called next time.
984 m_pullup(struct mbuf *n, int len)
991 * If first mbuf has no cluster, and has room for len bytes
992 * without shifting current data, pullup into it,
993 * otherwise allocate a new mbuf to prepend to the chain.
995 if ((n->m_flags & M_EXT) == 0 &&
996 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
1005 m = m_get(M_NOWAIT, n->m_type);
1008 if (n->m_flags & M_PKTHDR)
1009 m_move_pkthdr(m, n);
1011 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1013 count = min(min(max(len, max_protohdr), space), n->m_len);
1014 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1024 } while (len > 0 && n);
1037 * Like m_pullup(), except a new mbuf is always allocated, and we allow
1038 * the amount of empty space before the data in the new mbuf to be specified
1039 * (in the event that the caller expects to prepend later).
1042 m_copyup(struct mbuf *n, int len, int dstoff)
1047 if (len > (MHLEN - dstoff))
1049 m = m_get(M_NOWAIT, n->m_type);
1052 if (n->m_flags & M_PKTHDR)
1053 m_move_pkthdr(m, n);
1054 m->m_data += dstoff;
1055 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1057 count = min(min(max(len, max_protohdr), space), n->m_len);
1058 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
1068 } while (len > 0 && n);
1081 * Partition an mbuf chain in two pieces, returning the tail --
1082 * all but the first len0 bytes. In case of failure, it returns NULL and
1083 * attempts to restore the chain to its original state.
1085 * Note that the resulting mbufs might be read-only, because the new
1086 * mbuf can end up sharing an mbuf cluster with the original mbuf if
1087 * the "breaking point" happens to lie within a cluster mbuf. Use the
1088 * M_WRITABLE() macro to check for this case.
1091 m_split(struct mbuf *m0, int len0, int wait)
1094 u_int len = len0, remain;
1096 MBUF_CHECKSLEEP(wait);
1097 for (m = m0; m && len > m->m_len; m = m->m_next)
1101 remain = m->m_len - len;
1102 if (m0->m_flags & M_PKTHDR && remain == 0) {
1103 n = m_gethdr(wait, m0->m_type);
1106 n->m_next = m->m_next;
1108 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1109 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1110 m0->m_pkthdr.len = len0;
1112 } else if (m0->m_flags & M_PKTHDR) {
1113 n = m_gethdr(wait, m0->m_type);
1116 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1117 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1118 m0->m_pkthdr.len = len0;
1119 if (m->m_flags & M_EXT)
1121 if (remain > MHLEN) {
1122 /* m can't be the lead packet */
1124 n->m_next = m_split(m, len, wait);
1125 if (n->m_next == NULL) {
1134 } else if (remain == 0) {
1139 n = m_get(wait, m->m_type);
1145 if (m->m_flags & M_EXT) {
1146 n->m_data = m->m_data + len;
1149 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1153 n->m_next = m->m_next;
1158 * Routine to copy from device local memory into mbufs.
1159 * Note that `off' argument is offset into first mbuf of target chain from
1160 * which to begin copying the data to.
1163 m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
1164 void (*copy)(char *from, caddr_t to, u_int len))
1167 struct mbuf *top = NULL, **mp = ⊤
1170 if (off < 0 || off > MHLEN)
1173 while (totlen > 0) {
1174 if (top == NULL) { /* First one, must be PKTHDR */
1175 if (totlen + off >= MINCLSIZE) {
1176 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1179 m = m_gethdr(M_NOWAIT, MT_DATA);
1182 /* Place initial small packet/header at end of mbuf */
1183 if (m && totlen + off + max_linkhdr <= MLEN) {
1184 m->m_data += max_linkhdr;
1190 m->m_pkthdr.rcvif = ifp;
1191 m->m_pkthdr.len = totlen;
1193 if (totlen + off >= MINCLSIZE) {
1194 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1197 m = m_get(M_NOWAIT, MT_DATA);
1210 m->m_len = len = min(totlen, len);
1212 copy(buf, mtod(m, caddr_t), (u_int)len);
1214 bcopy(buf, mtod(m, caddr_t), (u_int)len);
1224 * Copy data from a buffer back into the indicated mbuf chain,
1225 * starting "off" bytes from the beginning, extending the mbuf
1226 * chain if necessary.
1229 m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
1232 struct mbuf *m = m0, *n;
1237 while (off > (mlen = m->m_len)) {
1240 if (m->m_next == NULL) {
1241 n = m_get(M_NOWAIT, m->m_type);
1244 bzero(mtod(n, caddr_t), MLEN);
1245 n->m_len = min(MLEN, len + off);
1251 if (m->m_next == NULL && (len > m->m_len - off)) {
1252 m->m_len += min(len - (m->m_len - off),
1253 M_TRAILINGSPACE(m));
1255 mlen = min (m->m_len - off, len);
1256 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
1264 if (m->m_next == NULL) {
1265 n = m_get(M_NOWAIT, m->m_type);
1268 n->m_len = min(MLEN, len);
1273 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1274 m->m_pkthdr.len = totlen;
1278 * Append the specified data to the indicated mbuf chain,
1279 * Extend the mbuf chain if the new data does not fit in
1282 * Return 1 if able to complete the job; otherwise 0.
1285 m_append(struct mbuf *m0, int len, c_caddr_t cp)
1288 int remainder, space;
1290 for (m = m0; m->m_next != NULL; m = m->m_next)
1293 space = M_TRAILINGSPACE(m);
1296 * Copy into available space.
1298 if (space > remainder)
1300 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1302 cp += space, remainder -= space;
1304 while (remainder > 0) {
1306 * Allocate a new mbuf; could check space
1307 * and allocate a cluster instead.
1309 n = m_get(M_NOWAIT, m->m_type);
1312 n->m_len = min(MLEN, remainder);
1313 bcopy(cp, mtod(n, caddr_t), n->m_len);
1314 cp += n->m_len, remainder -= n->m_len;
1318 if (m0->m_flags & M_PKTHDR)
1319 m0->m_pkthdr.len += len - remainder;
1320 return (remainder == 0);
1324 * Apply function f to the data in an mbuf chain starting "off" bytes from
1325 * the beginning, continuing for "len" bytes.
1328 m_apply(struct mbuf *m, int off, int len,
1329 int (*f)(void *, void *, u_int), void *arg)
1334 KASSERT(off >= 0, ("m_apply, negative off %d", off));
1335 KASSERT(len >= 0, ("m_apply, negative len %d", len));
1337 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1344 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1345 count = min(m->m_len - off, len);
1346 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1357 * Return a pointer to mbuf/offset of location in mbuf chain.
1360 m_getptr(struct mbuf *m, int loc, int *off)
1364 /* Normal end of search. */
1365 if (m->m_len > loc) {
1370 if (m->m_next == NULL) {
1372 /* Point at the end of valid data. */
1385 m_print(const struct mbuf *m, int maxlen)
1389 const struct mbuf *m2;
1392 printf("mbuf: %p\n", m);
1396 if (m->m_flags & M_PKTHDR)
1397 len = m->m_pkthdr.len;
1401 while (m2 != NULL && (len == -1 || len)) {
1403 if (maxlen != -1 && pdata > maxlen)
1405 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len,
1406 m2->m_next, m2->m_flags, "\20\20freelist\17skipfw"
1407 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly"
1408 "\3eor\2pkthdr\1ext", pdata ? "" : "\n");
1410 printf(", %*D\n", pdata, (u_char *)m2->m_data, "-");
1416 printf("%d bytes unaccounted for.\n", len);
1421 m_fixhdr(struct mbuf *m0)
1425 len = m_length(m0, NULL);
1426 m0->m_pkthdr.len = len;
1431 m_length(struct mbuf *m0, struct mbuf **last)
1437 for (m = m0; m != NULL; m = m->m_next) {
1439 if (m->m_next == NULL)
1448 * Defragment a mbuf chain, returning the shortest possible
1449 * chain of mbufs and clusters. If allocation fails and
1450 * this cannot be completed, NULL will be returned, but
1451 * the passed in chain will be unchanged. Upon success,
1452 * the original chain will be freed, and the new chain
1455 * If a non-packet header is passed in, the original
1456 * mbuf (chain?) will be returned unharmed.
1459 m_defrag(struct mbuf *m0, int how)
1461 struct mbuf *m_new = NULL, *m_final = NULL;
1462 int progress = 0, length;
1464 MBUF_CHECKSLEEP(how);
1465 if (!(m0->m_flags & M_PKTHDR))
1468 m_fixhdr(m0); /* Needed sanity check */
1470 #ifdef MBUF_STRESS_TEST
1471 if (m_defragrandomfailures) {
1472 int temp = arc4random() & 0xff;
1478 if (m0->m_pkthdr.len > MHLEN)
1479 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1481 m_final = m_gethdr(how, MT_DATA);
1483 if (m_final == NULL)
1486 if (m_dup_pkthdr(m_final, m0, how) == 0)
1491 while (progress < m0->m_pkthdr.len) {
1492 length = m0->m_pkthdr.len - progress;
1493 if (length > MCLBYTES)
1496 if (m_new == NULL) {
1498 m_new = m_getcl(how, MT_DATA, 0);
1500 m_new = m_get(how, MT_DATA);
1505 m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1507 m_new->m_len = length;
1508 if (m_new != m_final)
1509 m_cat(m_final, m_new);
1512 #ifdef MBUF_STRESS_TEST
1513 if (m0->m_next == NULL)
1518 #ifdef MBUF_STRESS_TEST
1520 m_defragbytes += m0->m_pkthdr.len;
1524 #ifdef MBUF_STRESS_TEST
1533 * Defragment an mbuf chain, returning at most maxfrags separate
1534 * mbufs+clusters. If this is not possible NULL is returned and
1535 * the original mbuf chain is left in it's present (potentially
1536 * modified) state. We use two techniques: collapsing consecutive
1537 * mbufs and replacing consecutive mbufs by a cluster.
1539 * NB: this should really be named m_defrag but that name is taken
1542 m_collapse(struct mbuf *m0, int how, int maxfrags)
1544 struct mbuf *m, *n, *n2, **prev;
1548 * Calculate the current number of frags.
1551 for (m = m0; m != NULL; m = m->m_next)
1554 * First, try to collapse mbufs. Note that we always collapse
1555 * towards the front so we don't need to deal with moving the
1556 * pkthdr. This may be suboptimal if the first mbuf has much
1557 * less data than the following.
1565 if (M_WRITABLE(m) &&
1566 n->m_len < M_TRAILINGSPACE(m)) {
1567 bcopy(mtod(n, void *), mtod(m, char *) + m->m_len,
1569 m->m_len += n->m_len;
1570 m->m_next = n->m_next;
1572 if (--curfrags <= maxfrags)
1577 KASSERT(maxfrags > 1,
1578 ("maxfrags %u, but normal collapse failed", maxfrags));
1580 * Collapse consecutive mbufs to a cluster.
1582 prev = &m0->m_next; /* NB: not the first mbuf */
1583 while ((n = *prev) != NULL) {
1584 if ((n2 = n->m_next) != NULL &&
1585 n->m_len + n2->m_len < MCLBYTES) {
1586 m = m_getcl(how, MT_DATA, 0);
1589 bcopy(mtod(n, void *), mtod(m, void *), n->m_len);
1590 bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len,
1592 m->m_len = n->m_len + n2->m_len;
1593 m->m_next = n2->m_next;
1597 if (--curfrags <= maxfrags) /* +1 cl -2 mbufs */
1600 * Still not there, try the normal collapse
1601 * again before we allocate another cluster.
1608 * No place where we can collapse to a cluster; punt.
1609 * This can occur if, for example, you request 2 frags
1610 * but the packet requires that both be clusters (we
1611 * never reallocate the first mbuf to avoid moving the
1618 #ifdef MBUF_STRESS_TEST
1621 * Fragment an mbuf chain. There's no reason you'd ever want to do
1622 * this in normal usage, but it's great for stress testing various
1625 * If fragmentation is not possible, the original chain will be
1628 * Possible length values:
1629 * 0 no fragmentation will occur
1630 * > 0 each fragment will be of the specified length
1631 * -1 each fragment will be the same random value in length
1632 * -2 each fragment's length will be entirely random
1633 * (Random values range from 1 to 256)
1636 m_fragment(struct mbuf *m0, int how, int length)
1638 struct mbuf *m_new = NULL, *m_final = NULL;
1641 if (!(m0->m_flags & M_PKTHDR))
1644 if ((length == 0) || (length < -2))
1647 m_fixhdr(m0); /* Needed sanity check */
1649 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1651 if (m_final == NULL)
1654 if (m_dup_pkthdr(m_final, m0, how) == 0)
1660 length = 1 + (arc4random() & 255);
1662 while (progress < m0->m_pkthdr.len) {
1668 fraglen = 1 + (arc4random() & 255);
1669 if (fraglen > m0->m_pkthdr.len - progress)
1670 fraglen = m0->m_pkthdr.len - progress;
1672 if (fraglen > MCLBYTES)
1675 if (m_new == NULL) {
1676 m_new = m_getcl(how, MT_DATA, 0);
1681 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
1682 progress += fraglen;
1683 m_new->m_len = fraglen;
1684 if (m_new != m_final)
1685 m_cat(m_final, m_new);
1694 /* Return the original chain on failure */
1701 * Copy the contents of uio into a properly sized mbuf chain.
1704 m_uiotombuf(struct uio *uio, int how, int len, int align, int flags)
1706 struct mbuf *m, *mb;
1712 * len can be zero or an arbitrary large value bound by
1713 * the total data supplied by the uio.
1716 total = min(uio->uio_resid, len);
1718 total = uio->uio_resid;
1721 * The smallest unit returned by m_getm2() is a single mbuf
1722 * with pkthdr. We can't align past it.
1728 * Give us the full allocation or nothing.
1729 * If len is zero return the smallest empty mbuf.
1731 m = m_getm2(NULL, max(total + align, 1), how, MT_DATA, flags);
1736 /* Fill all mbufs with uio data and update header information. */
1737 for (mb = m; mb != NULL; mb = mb->m_next) {
1738 length = min(M_TRAILINGSPACE(mb), total - progress);
1740 error = uiomove(mtod(mb, void *), length, uio);
1748 if (flags & M_PKTHDR)
1749 m->m_pkthdr.len += length;
1751 KASSERT(progress == total, ("%s: progress != total", __func__));
1757 * Copy an mbuf chain into a uio limited by len if set.
1760 m_mbuftouio(struct uio *uio, struct mbuf *m, int len)
1762 int error, length, total;
1766 total = min(uio->uio_resid, len);
1768 total = uio->uio_resid;
1770 /* Fill the uio with data from the mbufs. */
1771 for (; m != NULL; m = m->m_next) {
1772 length = min(m->m_len, total - progress);
1774 error = uiomove(mtod(m, void *), length, uio);
1785 * Create a writable copy of the mbuf chain. While doing this
1786 * we compact the chain with a goal of producing a chain with
1787 * at most two mbufs. The second mbuf in this chain is likely
1788 * to be a cluster. The primary purpose of this work is to create
1789 * a writable packet for encryption, compression, etc. The
1790 * secondary goal is to linearize the data so the data can be
1791 * passed to crypto hardware in the most efficient manner possible.
1794 m_unshare(struct mbuf *m0, int how)
1796 struct mbuf *m, *mprev;
1797 struct mbuf *n, *mfirst, *mlast;
1801 for (m = m0; m != NULL; m = mprev->m_next) {
1803 * Regular mbufs are ignored unless there's a cluster
1804 * in front of it that we can use to coalesce. We do
1805 * the latter mainly so later clusters can be coalesced
1806 * also w/o having to handle them specially (i.e. convert
1807 * mbuf+cluster -> cluster). This optimization is heavily
1808 * influenced by the assumption that we're running over
1809 * Ethernet where MCLBYTES is large enough that the max
1810 * packet size will permit lots of coalescing into a
1811 * single cluster. This in turn permits efficient
1812 * crypto operations, especially when using hardware.
1814 if ((m->m_flags & M_EXT) == 0) {
1815 if (mprev && (mprev->m_flags & M_EXT) &&
1816 m->m_len <= M_TRAILINGSPACE(mprev)) {
1817 /* XXX: this ignores mbuf types */
1818 memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1819 mtod(m, caddr_t), m->m_len);
1820 mprev->m_len += m->m_len;
1821 mprev->m_next = m->m_next; /* unlink from chain */
1822 m_free(m); /* reclaim mbuf */
1824 newipsecstat.ips_mbcoalesced++;
1832 * Writable mbufs are left alone (for now).
1834 if (M_WRITABLE(m)) {
1840 * Not writable, replace with a copy or coalesce with
1841 * the previous mbuf if possible (since we have to copy
1842 * it anyway, we try to reduce the number of mbufs and
1843 * clusters so that future work is easier).
1845 KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags));
1846 /* NB: we only coalesce into a cluster or larger */
1847 if (mprev != NULL && (mprev->m_flags & M_EXT) &&
1848 m->m_len <= M_TRAILINGSPACE(mprev)) {
1849 /* XXX: this ignores mbuf types */
1850 memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1851 mtod(m, caddr_t), m->m_len);
1852 mprev->m_len += m->m_len;
1853 mprev->m_next = m->m_next; /* unlink from chain */
1854 m_free(m); /* reclaim mbuf */
1856 newipsecstat.ips_clcoalesced++;
1862 * Allocate new space to hold the copy and copy the data.
1863 * We deal with jumbo mbufs (i.e. m_len > MCLBYTES) by
1864 * splitting them into clusters. We could just malloc a
1865 * buffer and make it external but too many device drivers
1866 * don't know how to break up the non-contiguous memory when
1869 n = m_getcl(how, m->m_type, m->m_flags);
1874 if (m->m_flags & M_PKTHDR) {
1875 KASSERT(mprev == NULL, ("%s: m0 %p, m %p has M_PKTHDR",
1877 m_move_pkthdr(n, m);
1884 int cc = min(len, MCLBYTES);
1885 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc);
1891 newipsecstat.ips_clcopied++;
1899 n = m_getcl(how, m->m_type, m->m_flags);
1906 n->m_next = m->m_next;
1908 m0 = mfirst; /* new head of chain */
1910 mprev->m_next = mfirst; /* replace old mbuf */
1911 m_free(m); /* release old mbuf */
1917 #ifdef MBUF_PROFILING
1919 #define MP_BUCKETS 32 /* don't just change this as things may overflow.*/
1920 struct mbufprofile {
1921 uintmax_t wasted[MP_BUCKETS];
1922 uintmax_t used[MP_BUCKETS];
1923 uintmax_t segments[MP_BUCKETS];
1926 #define MP_MAXDIGITS 21 /* strlen("16,000,000,000,000,000,000") == 21 */
1927 #define MP_NUMLINES 6
1928 #define MP_NUMSPERLINE 16
1929 #define MP_EXTRABYTES 64 /* > strlen("used:\nwasted:\nsegments:\n") */
1930 /* work out max space needed and add a bit of spare space too */
1931 #define MP_MAXLINE ((MP_MAXDIGITS+1) * MP_NUMSPERLINE)
1932 #define MP_BUFSIZE ((MP_MAXLINE * MP_NUMLINES) + 1 + MP_EXTRABYTES)
1934 char mbprofbuf[MP_BUFSIZE];
1937 m_profile(struct mbuf *m)
1946 if (m->m_flags & M_EXT) {
1947 wasted += MHLEN - sizeof(m->m_ext) +
1948 m->m_ext.ext_size - m->m_len;
1950 if (m->m_flags & M_PKTHDR)
1951 wasted += MHLEN - m->m_len;
1953 wasted += MLEN - m->m_len;
1957 /* be paranoid.. it helps */
1958 if (segments > MP_BUCKETS - 1)
1959 segments = MP_BUCKETS - 1;
1962 if (wasted > 100000)
1964 /* store in the appropriate bucket */
1965 /* don't bother locking. if it's slightly off, so what? */
1966 mbprof.segments[segments]++;
1967 mbprof.used[fls(used)]++;
1968 mbprof.wasted[fls(wasted)]++;
1972 mbprof_textify(void)
1978 p = &mbprof.wasted[0];
1980 offset = snprintf(c, MP_MAXLINE + 10,
1982 "%ju %ju %ju %ju %ju %ju %ju %ju "
1983 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
1984 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
1985 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
1987 p = &mbprof.wasted[16];
1989 offset = snprintf(c, MP_MAXLINE,
1990 "%ju %ju %ju %ju %ju %ju %ju %ju "
1991 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
1992 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
1993 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
1995 p = &mbprof.used[0];
1997 offset = snprintf(c, MP_MAXLINE + 10,
1999 "%ju %ju %ju %ju %ju %ju %ju %ju "
2000 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2001 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2002 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2004 p = &mbprof.used[16];
2006 offset = snprintf(c, MP_MAXLINE,
2007 "%ju %ju %ju %ju %ju %ju %ju %ju "
2008 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2009 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2010 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2012 p = &mbprof.segments[0];
2014 offset = snprintf(c, MP_MAXLINE + 10,
2016 "%ju %ju %ju %ju %ju %ju %ju %ju "
2017 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2018 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2019 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2021 p = &mbprof.segments[16];
2023 offset = snprintf(c, MP_MAXLINE,
2024 "%ju %ju %ju %ju %ju %ju %ju %ju "
2025 "%ju %ju %ju %ju %ju %ju %ju %jju",
2026 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2027 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2032 mbprof_handler(SYSCTL_HANDLER_ARGS)
2037 error = SYSCTL_OUT(req, mbprofbuf, strlen(mbprofbuf) + 1);
2042 mbprof_clr_handler(SYSCTL_HANDLER_ARGS)
2047 error = sysctl_handle_int(oidp, &clear, 0, req);
2048 if (error || !req->newptr)
2052 bzero(&mbprof, sizeof(mbprof));
2059 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofile, CTLTYPE_STRING|CTLFLAG_RD,
2060 NULL, 0, mbprof_handler, "A", "mbuf profiling statistics");
2062 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofileclr, CTLTYPE_INT|CTLFLAG_RW,
2063 NULL, 0, mbprof_clr_handler, "I", "clear mbuf profiling statistics");