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 * Allocate a given length worth of mbufs and/or clusters (whatever fits
89 * best) and return a pointer to the top of the allocated chain. If an
90 * existing mbuf chain is provided, then we will append the new chain
91 * to the existing one but still return the top of the newly allocated
95 m_getm2(struct mbuf *m, int len, int how, short type, int flags)
97 struct mbuf *mb, *nm = NULL, *mtail = NULL;
99 KASSERT(len >= 0, ("%s: len is < 0", __func__));
101 /* Validate flags. */
102 flags &= (M_PKTHDR | M_EOR);
104 /* Packet header mbuf must be first in chain. */
105 if ((flags & M_PKTHDR) && m != NULL)
108 /* Loop and append maximum sized mbufs to the chain tail. */
111 mb = m_getjcl(how, type, (flags & M_PKTHDR),
113 else if (len >= MINCLSIZE)
114 mb = m_getcl(how, type, (flags & M_PKTHDR));
115 else if (flags & M_PKTHDR)
116 mb = m_gethdr(how, type);
118 mb = m_get(how, type);
120 /* Fail the whole operation if one mbuf can't be allocated. */
128 len -= (mb->m_flags & M_EXT) ? mb->m_ext.ext_size :
129 ((mb->m_flags & M_PKTHDR) ? MHLEN : MLEN);
135 flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */
138 mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */
140 /* If mbuf was supplied, append new chain to the end of it. */
142 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
145 mtail->m_flags &= ~M_EOR;
153 * Free an entire chain of mbufs and associated external buffers, if
157 m_freem(struct mbuf *mb)
165 * Configure a provided mbuf to refer to the provided external storage
166 * buffer and setup a reference count for said buffer. If the setting
167 * up of the reference count fails, the M_EXT bit will not be set. If
168 * successfull, the M_EXT bit is set in the mbuf's flags.
171 * mb The existing mbuf to which to attach the provided buffer.
172 * buf The address of the provided external storage buffer.
173 * size The size of the provided buffer.
174 * freef A pointer to a routine that is responsible for freeing the
175 * provided external storage buffer.
176 * args A pointer to an argument structure (of any type) to be passed
177 * to the provided freef routine (may be NULL).
178 * flags Any other flags to be passed to the provided mbuf.
179 * type The type that the external storage buffer should be
186 m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
187 void (*freef)(void *, void *), void *arg1, void *arg2, int flags, int type)
189 KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
191 if (type != EXT_EXTREF)
192 mb->m_ext.ref_cnt = (u_int *)uma_zalloc(zone_ext_refcnt, M_NOWAIT);
193 if (mb->m_ext.ref_cnt != NULL) {
194 *(mb->m_ext.ref_cnt) = 1;
195 mb->m_flags |= (M_EXT | flags);
196 mb->m_ext.ext_buf = buf;
197 mb->m_data = mb->m_ext.ext_buf;
198 mb->m_ext.ext_size = size;
199 mb->m_ext.ext_free = freef;
200 mb->m_ext.ext_arg1 = arg1;
201 mb->m_ext.ext_arg2 = arg2;
202 mb->m_ext.ext_type = type;
207 * Non-directly-exported function to clean up after mbufs with M_EXT
208 * storage attached to them if the reference count hits 1.
211 mb_free_ext(struct mbuf *m)
215 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
216 KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
220 * check if the header is embedded in the cluster
222 skipmbuf = (m->m_flags & M_NOFREE);
224 /* Free attached storage if this mbuf is the only reference to it. */
225 if (*(m->m_ext.ref_cnt) == 1 ||
226 atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 1) {
227 switch (m->m_ext.ext_type) {
228 case EXT_PACKET: /* The packet zone is special. */
229 if (*(m->m_ext.ref_cnt) == 0)
230 *(m->m_ext.ref_cnt) = 1;
231 uma_zfree(zone_pack, m);
232 return; /* Job done. */
234 uma_zfree(zone_clust, m->m_ext.ext_buf);
237 uma_zfree(zone_jumbop, m->m_ext.ext_buf);
240 uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
243 uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
249 *(m->m_ext.ref_cnt) = 0;
250 uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *,
254 KASSERT(m->m_ext.ext_free != NULL,
255 ("%s: ext_free not set", __func__));
256 (*(m->m_ext.ext_free))(m->m_ext.ext_arg1,
260 KASSERT(m->m_ext.ext_type == 0,
261 ("%s: unknown ext_type", __func__));
268 * Free this mbuf back to the mbuf zone with all m_ext
269 * information purged.
271 m->m_ext.ext_buf = NULL;
272 m->m_ext.ext_free = NULL;
273 m->m_ext.ext_arg1 = NULL;
274 m->m_ext.ext_arg2 = NULL;
275 m->m_ext.ref_cnt = NULL;
276 m->m_ext.ext_size = 0;
277 m->m_ext.ext_type = 0;
278 m->m_flags &= ~M_EXT;
279 uma_zfree(zone_mbuf, m);
283 * Attach the cluster from *m to *n, set up m_ext in *n
284 * and bump the refcount of the cluster.
287 mb_dupcl(struct mbuf *n, struct mbuf *m)
289 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
290 KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
291 KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
293 if (*(m->m_ext.ref_cnt) == 1)
294 *(m->m_ext.ref_cnt) += 1;
296 atomic_add_int(m->m_ext.ref_cnt, 1);
297 n->m_ext.ext_buf = m->m_ext.ext_buf;
298 n->m_ext.ext_free = m->m_ext.ext_free;
299 n->m_ext.ext_arg1 = m->m_ext.ext_arg1;
300 n->m_ext.ext_arg2 = m->m_ext.ext_arg2;
301 n->m_ext.ext_size = m->m_ext.ext_size;
302 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
303 n->m_ext.ext_type = m->m_ext.ext_type;
305 n->m_flags |= m->m_flags & M_RDONLY;
309 * Clean up mbuf (chain) from any tags and packet headers.
310 * If "all" is set then the first mbuf in the chain will be
314 m_demote(struct mbuf *m0, int all)
318 for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) {
319 if (m->m_flags & M_PKTHDR) {
320 m_tag_delete_chain(m, NULL);
321 m->m_flags &= ~M_PKTHDR;
322 bzero(&m->m_pkthdr, sizeof(struct pkthdr));
324 if (m != m0 && m->m_nextpkt != NULL) {
325 KASSERT(m->m_nextpkt == NULL,
326 ("%s: m_nextpkt not NULL", __func__));
327 m_freem(m->m_nextpkt);
330 m->m_flags = m->m_flags & (M_EXT|M_RDONLY|M_FREELIST|M_NOFREE);
335 * Sanity checks on mbuf (chain) for use in KASSERT() and general
337 * Returns 0 or panics when bad and 1 on all tests passed.
338 * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they
342 m_sanity(struct mbuf *m0, int sanitize)
349 #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m)
351 #define M_SANITY_ACTION(s) printf("mbuf %p: " s, m)
354 for (m = m0; m != NULL; m = m->m_next) {
356 * Basic pointer checks. If any of these fails then some
357 * unrelated kernel memory before or after us is trashed.
358 * No way to recover from that.
360 a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf :
361 ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) :
362 (caddr_t)(&m->m_dat)) );
363 b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size :
364 ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN)));
365 if ((caddr_t)m->m_data < a)
366 M_SANITY_ACTION("m_data outside mbuf data range left");
367 if ((caddr_t)m->m_data > b)
368 M_SANITY_ACTION("m_data outside mbuf data range right");
369 if ((caddr_t)m->m_data + m->m_len > b)
370 M_SANITY_ACTION("m_data + m_len exeeds mbuf space");
371 if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) {
372 if ((caddr_t)m->m_pkthdr.header < a ||
373 (caddr_t)m->m_pkthdr.header > b)
374 M_SANITY_ACTION("m_pkthdr.header outside mbuf data range");
377 /* m->m_nextpkt may only be set on first mbuf in chain. */
378 if (m != m0 && m->m_nextpkt != NULL) {
380 m_freem(m->m_nextpkt);
381 m->m_nextpkt = (struct mbuf *)0xDEADC0DE;
383 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf");
386 /* packet length (not mbuf length!) calculation */
387 if (m0->m_flags & M_PKTHDR)
390 /* m_tags may only be attached to first mbuf in chain. */
391 if (m != m0 && m->m_flags & M_PKTHDR &&
392 !SLIST_EMPTY(&m->m_pkthdr.tags)) {
394 m_tag_delete_chain(m, NULL);
395 /* put in 0xDEADC0DE perhaps? */
397 M_SANITY_ACTION("m_tags on in-chain mbuf");
400 /* M_PKTHDR may only be set on first mbuf in chain */
401 if (m != m0 && m->m_flags & M_PKTHDR) {
403 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
404 m->m_flags &= ~M_PKTHDR;
405 /* put in 0xDEADCODE and leave hdr flag in */
407 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf");
411 if (pktlen && pktlen != m->m_pkthdr.len) {
415 M_SANITY_ACTION("m_pkthdr.len != mbuf chain length");
419 #undef M_SANITY_ACTION
424 * "Move" mbuf pkthdr from "from" to "to".
425 * "from" must have M_PKTHDR set, and "to" must be empty.
428 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
432 /* see below for why these are not enabled */
434 /* Note: with MAC, this may not be a good assertion. */
435 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
436 ("m_move_pkthdr: to has tags"));
440 * XXXMAC: It could be this should also occur for non-MAC?
442 if (to->m_flags & M_PKTHDR)
443 m_tag_delete_chain(to, NULL);
445 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
446 if ((to->m_flags & M_EXT) == 0)
447 to->m_data = to->m_pktdat;
448 to->m_pkthdr = from->m_pkthdr; /* especially tags */
449 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */
450 from->m_flags &= ~M_PKTHDR;
454 * Duplicate "from"'s mbuf pkthdr in "to".
455 * "from" must have M_PKTHDR set, and "to" must be empty.
456 * In particular, this does a deep copy of the packet tags.
459 m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
464 * The mbuf allocator only initializes the pkthdr
465 * when the mbuf is allocated with MGETHDR. Many users
466 * (e.g. m_copy*, m_prepend) use MGET and then
467 * smash the pkthdr as needed causing these
468 * assertions to trip. For now just disable them.
471 /* Note: with MAC, this may not be a good assertion. */
472 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
474 MBUF_CHECKSLEEP(how);
476 if (to->m_flags & M_PKTHDR)
477 m_tag_delete_chain(to, NULL);
479 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
480 if ((to->m_flags & M_EXT) == 0)
481 to->m_data = to->m_pktdat;
482 to->m_pkthdr = from->m_pkthdr;
483 SLIST_INIT(&to->m_pkthdr.tags);
484 return (m_tag_copy_chain(to, from, MBTOM(how)));
488 * Lesser-used path for M_PREPEND:
489 * allocate new mbuf to prepend to chain,
493 m_prepend(struct mbuf *m, int len, int how)
497 if (m->m_flags & M_PKTHDR)
498 MGETHDR(mn, how, m->m_type);
500 MGET(mn, how, m->m_type);
505 if (m->m_flags & M_PKTHDR)
506 M_MOVE_PKTHDR(mn, m);
509 if(m->m_flags & M_PKTHDR) {
521 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
522 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
523 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
524 * Note that the copy is read-only, because clusters are not copied,
525 * only their reference counts are incremented.
528 m_copym(struct mbuf *m, int off0, int len, int wait)
530 struct mbuf *n, **np;
535 KASSERT(off >= 0, ("m_copym, negative off %d", off));
536 KASSERT(len >= 0, ("m_copym, negative len %d", len));
537 MBUF_CHECKSLEEP(wait);
538 if (off == 0 && m->m_flags & M_PKTHDR)
541 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
551 KASSERT(len == M_COPYALL,
552 ("m_copym, length > size of mbuf chain"));
556 MGETHDR(n, wait, m->m_type);
558 MGET(n, wait, m->m_type);
563 if (!m_dup_pkthdr(n, m, wait))
565 if (len == M_COPYALL)
566 n->m_pkthdr.len -= off0;
568 n->m_pkthdr.len = len;
571 n->m_len = min(len, m->m_len - off);
572 if (m->m_flags & M_EXT) {
573 n->m_data = m->m_data + off;
576 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
578 if (len != M_COPYALL)
585 mbstat.m_mcfail++; /* XXX: No consistency. */
590 mbstat.m_mcfail++; /* XXX: No consistency. */
595 * Returns mbuf chain with new head for the prepending case.
596 * Copies from mbuf (chain) n from off for len to mbuf (chain) m
597 * either prepending or appending the data.
598 * The resulting mbuf (chain) m is fully writeable.
599 * m is destination (is made writeable)
600 * n is source, off is offset in source, len is len from offset
601 * dir, 0 append, 1 prepend
602 * how, wait or nowait
606 m_bcopyxxx(void *s, void *t, u_int len)
608 bcopy(s, t, (size_t)len);
613 m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len,
616 struct mbuf *mm, *x, *z, *prev = NULL;
621 KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source"));
622 KASSERT(off >= 0, ("m_copymdata, negative off %d", off));
623 KASSERT(len >= 0, ("m_copymdata, negative len %d", len));
624 KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep));
633 for (z = n; z != NULL; z = z->m_next)
635 if (len == M_COPYALL)
637 if (off + len > nlen || len < 1)
640 if (!M_WRITABLE(mm)) {
641 /* XXX: Use proper m_xxx function instead. */
642 x = m_getcl(how, MT_DATA, mm->m_flags);
645 bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size);
646 p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf);
656 * Append/prepend the data. Allocating mbufs as necessary.
658 /* Shortcut if enough free space in first/last mbuf. */
659 if (!prep && M_TRAILINGSPACE(mm) >= len) {
660 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) +
663 mm->m_pkthdr.len += len;
666 if (prep && M_LEADINGSPACE(mm) >= len) {
667 mm->m_data = mtod(mm, caddr_t) - len;
668 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t));
670 mm->m_pkthdr.len += len;
674 /* Expand first/last mbuf to cluster if possible. */
675 if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) {
676 bcopy(mm->m_data, &buf, mm->m_len);
678 if (!(mm->m_flags & M_EXT))
680 bcopy(&buf, mm->m_ext.ext_buf, mm->m_len);
681 mm->m_data = mm->m_ext.ext_buf;
682 mm->m_pkthdr.header = NULL;
684 if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) {
685 bcopy(mm->m_data, &buf, mm->m_len);
687 if (!(mm->m_flags & M_EXT))
689 bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf +
690 mm->m_ext.ext_size - mm->m_len, mm->m_len);
691 mm->m_data = (caddr_t)mm->m_ext.ext_buf +
692 mm->m_ext.ext_size - mm->m_len;
693 mm->m_pkthdr.header = NULL;
696 /* Append/prepend as many mbuf (clusters) as necessary to fit len. */
697 if (!prep && len > M_TRAILINGSPACE(mm)) {
698 if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA))
701 if (prep && len > M_LEADINGSPACE(mm)) {
702 if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA)))
705 for (x = z; x != NULL; x = x->m_next) {
706 i += x->m_flags & M_EXT ? x->m_ext.ext_size :
707 (x->m_flags & M_PKTHDR ? MHLEN : MLEN);
711 z->m_data += i - len;
712 m_move_pkthdr(mm, z);
717 /* Seek to start position in source mbuf. Optimization for long chains. */
725 /* Copy data into target mbuf. */
728 KASSERT(z != NULL, ("m_copymdata, falling off target edge"));
729 i = M_TRAILINGSPACE(z);
730 m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len);
732 /* fixup pkthdr.len if necessary */
733 if ((prep ? mm : m)->m_flags & M_PKTHDR)
734 (prep ? mm : m)->m_pkthdr.len += i;
739 return (prep ? mm : m);
743 * Copy an entire packet, including header (which must be present).
744 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
745 * Note that the copy is read-only, because clusters are not copied,
746 * only their reference counts are incremented.
747 * Preserve alignment of the first mbuf so if the creator has left
748 * some room at the beginning (e.g. for inserting protocol headers)
749 * the copies still have the room available.
752 m_copypacket(struct mbuf *m, int how)
754 struct mbuf *top, *n, *o;
756 MBUF_CHECKSLEEP(how);
757 MGET(n, how, m->m_type);
762 if (!m_dup_pkthdr(n, m, how))
765 if (m->m_flags & M_EXT) {
766 n->m_data = m->m_data;
769 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
770 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
775 MGET(o, how, m->m_type);
783 if (m->m_flags & M_EXT) {
784 n->m_data = m->m_data;
787 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
795 mbstat.m_mcfail++; /* XXX: No consistency. */
800 * Copy data from an mbuf chain starting "off" bytes from the beginning,
801 * continuing for "len" bytes, into the indicated buffer.
804 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
808 KASSERT(off >= 0, ("m_copydata, negative off %d", off));
809 KASSERT(len >= 0, ("m_copydata, negative len %d", len));
811 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
818 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
819 count = min(m->m_len - off, len);
820 bcopy(mtod(m, caddr_t) + off, cp, count);
829 * Copy a packet header mbuf chain into a completely new chain, including
830 * copying any mbuf clusters. Use this instead of m_copypacket() when
831 * you need a writable copy of an mbuf chain.
834 m_dup(struct mbuf *m, int how)
836 struct mbuf **p, *top = NULL;
837 int remain, moff, nsize;
839 MBUF_CHECKSLEEP(how);
845 /* While there's more data, get a new mbuf, tack it on, and fill it */
846 remain = m->m_pkthdr.len;
849 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */
852 /* Get the next new mbuf */
853 if (remain >= MINCLSIZE) {
854 n = m_getcl(how, m->m_type, 0);
857 n = m_get(how, m->m_type);
863 if (top == NULL) { /* First one, must be PKTHDR */
864 if (!m_dup_pkthdr(n, m, how)) {
868 if ((n->m_flags & M_EXT) == 0)
870 n->m_flags &= ~M_RDONLY;
874 /* Link it into the new chain */
878 /* Copy data from original mbuf(s) into new mbuf */
879 while (n->m_len < nsize && m != NULL) {
880 int chunk = min(nsize - n->m_len, m->m_len - moff);
882 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
886 if (moff == m->m_len) {
892 /* Check correct total mbuf length */
893 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
894 ("%s: bogus m_pkthdr.len", __func__));
900 mbstat.m_mcfail++; /* XXX: No consistency. */
905 * Concatenate mbuf chain n to m.
906 * Both chains must be of the same type (e.g. MT_DATA).
907 * Any m_pkthdr is not updated.
910 m_cat(struct mbuf *m, struct mbuf *n)
915 if (!M_WRITABLE(m) ||
916 M_TRAILINGSPACE(m) < n->m_len) {
917 /* just join the two chains */
921 /* splat the data from one into the other */
922 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
924 m->m_len += n->m_len;
930 m_adj(struct mbuf *mp, int req_len)
936 if ((m = mp) == NULL)
942 while (m != NULL && len > 0) {
943 if (m->m_len <= len) {
953 if (mp->m_flags & M_PKTHDR)
954 mp->m_pkthdr.len -= (req_len - len);
957 * Trim from tail. Scan the mbuf chain,
958 * calculating its length and finding the last mbuf.
959 * If the adjustment only affects this mbuf, then just
960 * adjust and return. Otherwise, rescan and truncate
961 * after the remaining size.
967 if (m->m_next == (struct mbuf *)0)
971 if (m->m_len >= len) {
973 if (mp->m_flags & M_PKTHDR)
974 mp->m_pkthdr.len -= len;
981 * Correct length for chain is "count".
982 * Find the mbuf with last data, adjust its length,
983 * and toss data from remaining mbufs on chain.
986 if (m->m_flags & M_PKTHDR)
987 m->m_pkthdr.len = count;
988 for (; m; m = m->m_next) {
989 if (m->m_len >= count) {
991 if (m->m_next != NULL) {
1003 * Rearange an mbuf chain so that len bytes are contiguous
1004 * and in the data area of an mbuf (so that mtod and dtom
1005 * will work for a structure of size len). Returns the resulting
1006 * mbuf chain on success, frees it and returns null on failure.
1007 * If there is room, it will add up to max_protohdr-len extra bytes to the
1008 * contiguous region in an attempt to avoid being called next time.
1011 m_pullup(struct mbuf *n, int len)
1018 * If first mbuf has no cluster, and has room for len bytes
1019 * without shifting current data, pullup into it,
1020 * otherwise allocate a new mbuf to prepend to the chain.
1022 if ((n->m_flags & M_EXT) == 0 &&
1023 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
1024 if (n->m_len >= len)
1032 MGET(m, M_DONTWAIT, n->m_type);
1036 if (n->m_flags & M_PKTHDR)
1037 M_MOVE_PKTHDR(m, n);
1039 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1041 count = min(min(max(len, max_protohdr), space), n->m_len);
1042 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1052 } while (len > 0 && n);
1061 mbstat.m_mpfail++; /* XXX: No consistency. */
1066 * Like m_pullup(), except a new mbuf is always allocated, and we allow
1067 * the amount of empty space before the data in the new mbuf to be specified
1068 * (in the event that the caller expects to prepend later).
1073 m_copyup(struct mbuf *n, int len, int dstoff)
1078 if (len > (MHLEN - dstoff))
1080 MGET(m, M_DONTWAIT, n->m_type);
1084 if (n->m_flags & M_PKTHDR)
1085 M_MOVE_PKTHDR(m, n);
1086 m->m_data += dstoff;
1087 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1089 count = min(min(max(len, max_protohdr), space), n->m_len);
1090 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
1100 } while (len > 0 && n);
1114 * Partition an mbuf chain in two pieces, returning the tail --
1115 * all but the first len0 bytes. In case of failure, it returns NULL and
1116 * attempts to restore the chain to its original state.
1118 * Note that the resulting mbufs might be read-only, because the new
1119 * mbuf can end up sharing an mbuf cluster with the original mbuf if
1120 * the "breaking point" happens to lie within a cluster mbuf. Use the
1121 * M_WRITABLE() macro to check for this case.
1124 m_split(struct mbuf *m0, int len0, int wait)
1127 u_int len = len0, remain;
1129 MBUF_CHECKSLEEP(wait);
1130 for (m = m0; m && len > m->m_len; m = m->m_next)
1134 remain = m->m_len - len;
1135 if (m0->m_flags & M_PKTHDR) {
1136 MGETHDR(n, wait, m0->m_type);
1139 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1140 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1141 m0->m_pkthdr.len = len0;
1142 if (m->m_flags & M_EXT)
1144 if (remain > MHLEN) {
1145 /* m can't be the lead packet */
1147 n->m_next = m_split(m, len, wait);
1148 if (n->m_next == NULL) {
1156 MH_ALIGN(n, remain);
1157 } else if (remain == 0) {
1162 MGET(n, wait, m->m_type);
1168 if (m->m_flags & M_EXT) {
1169 n->m_data = m->m_data + len;
1172 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1176 n->m_next = m->m_next;
1181 * Routine to copy from device local memory into mbufs.
1182 * Note that `off' argument is offset into first mbuf of target chain from
1183 * which to begin copying the data to.
1186 m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
1187 void (*copy)(char *from, caddr_t to, u_int len))
1190 struct mbuf *top = NULL, **mp = ⊤
1193 if (off < 0 || off > MHLEN)
1196 while (totlen > 0) {
1197 if (top == NULL) { /* First one, must be PKTHDR */
1198 if (totlen + off >= MINCLSIZE) {
1199 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1202 m = m_gethdr(M_DONTWAIT, MT_DATA);
1205 /* Place initial small packet/header at end of mbuf */
1206 if (m && totlen + off + max_linkhdr <= MLEN) {
1207 m->m_data += max_linkhdr;
1213 m->m_pkthdr.rcvif = ifp;
1214 m->m_pkthdr.len = totlen;
1216 if (totlen + off >= MINCLSIZE) {
1217 m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1220 m = m_get(M_DONTWAIT, MT_DATA);
1233 m->m_len = len = min(totlen, len);
1235 copy(buf, mtod(m, caddr_t), (u_int)len);
1237 bcopy(buf, mtod(m, caddr_t), (u_int)len);
1247 * Copy data from a buffer back into the indicated mbuf chain,
1248 * starting "off" bytes from the beginning, extending the mbuf
1249 * chain if necessary.
1252 m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
1255 struct mbuf *m = m0, *n;
1260 while (off > (mlen = m->m_len)) {
1263 if (m->m_next == NULL) {
1264 n = m_get(M_DONTWAIT, m->m_type);
1267 bzero(mtod(n, caddr_t), MLEN);
1268 n->m_len = min(MLEN, len + off);
1274 if (m->m_next == NULL && (len > m->m_len - off)) {
1275 m->m_len += min(len - (m->m_len - off),
1276 M_TRAILINGSPACE(m));
1278 mlen = min (m->m_len - off, len);
1279 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
1287 if (m->m_next == NULL) {
1288 n = m_get(M_DONTWAIT, m->m_type);
1291 n->m_len = min(MLEN, len);
1296 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1297 m->m_pkthdr.len = totlen;
1301 * Append the specified data to the indicated mbuf chain,
1302 * Extend the mbuf chain if the new data does not fit in
1305 * Return 1 if able to complete the job; otherwise 0.
1308 m_append(struct mbuf *m0, int len, c_caddr_t cp)
1311 int remainder, space;
1313 for (m = m0; m->m_next != NULL; m = m->m_next)
1316 space = M_TRAILINGSPACE(m);
1319 * Copy into available space.
1321 if (space > remainder)
1323 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1325 cp += space, remainder -= space;
1327 while (remainder > 0) {
1329 * Allocate a new mbuf; could check space
1330 * and allocate a cluster instead.
1332 n = m_get(M_DONTWAIT, m->m_type);
1335 n->m_len = min(MLEN, remainder);
1336 bcopy(cp, mtod(n, caddr_t), n->m_len);
1337 cp += n->m_len, remainder -= n->m_len;
1341 if (m0->m_flags & M_PKTHDR)
1342 m0->m_pkthdr.len += len - remainder;
1343 return (remainder == 0);
1347 * Apply function f to the data in an mbuf chain starting "off" bytes from
1348 * the beginning, continuing for "len" bytes.
1351 m_apply(struct mbuf *m, int off, int len,
1352 int (*f)(void *, void *, u_int), void *arg)
1357 KASSERT(off >= 0, ("m_apply, negative off %d", off));
1358 KASSERT(len >= 0, ("m_apply, negative len %d", len));
1360 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1367 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1368 count = min(m->m_len - off, len);
1369 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1380 * Return a pointer to mbuf/offset of location in mbuf chain.
1383 m_getptr(struct mbuf *m, int loc, int *off)
1387 /* Normal end of search. */
1388 if (m->m_len > loc) {
1393 if (m->m_next == NULL) {
1395 /* Point at the end of valid data. */
1408 m_print(const struct mbuf *m, int maxlen)
1412 const struct mbuf *m2;
1415 printf("mbuf: %p\n", m);
1419 if (m->m_flags & M_PKTHDR)
1420 len = m->m_pkthdr.len;
1424 while (m2 != NULL && (len == -1 || len)) {
1426 if (maxlen != -1 && pdata > maxlen)
1428 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len,
1429 m2->m_next, m2->m_flags, "\20\20freelist\17skipfw"
1430 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly"
1431 "\3eor\2pkthdr\1ext", pdata ? "" : "\n");
1433 printf(", %*D\n", pdata, (u_char *)m2->m_data, "-");
1439 printf("%d bytes unaccounted for.\n", len);
1444 m_fixhdr(struct mbuf *m0)
1448 len = m_length(m0, NULL);
1449 m0->m_pkthdr.len = len;
1454 m_length(struct mbuf *m0, struct mbuf **last)
1460 for (m = m0; m != NULL; m = m->m_next) {
1462 if (m->m_next == NULL)
1471 * Defragment a mbuf chain, returning the shortest possible
1472 * chain of mbufs and clusters. If allocation fails and
1473 * this cannot be completed, NULL will be returned, but
1474 * the passed in chain will be unchanged. Upon success,
1475 * the original chain will be freed, and the new chain
1478 * If a non-packet header is passed in, the original
1479 * mbuf (chain?) will be returned unharmed.
1482 m_defrag(struct mbuf *m0, int how)
1484 struct mbuf *m_new = NULL, *m_final = NULL;
1485 int progress = 0, length;
1487 MBUF_CHECKSLEEP(how);
1488 if (!(m0->m_flags & M_PKTHDR))
1491 m_fixhdr(m0); /* Needed sanity check */
1493 #ifdef MBUF_STRESS_TEST
1494 if (m_defragrandomfailures) {
1495 int temp = arc4random() & 0xff;
1501 if (m0->m_pkthdr.len > MHLEN)
1502 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1504 m_final = m_gethdr(how, MT_DATA);
1506 if (m_final == NULL)
1509 if (m_dup_pkthdr(m_final, m0, how) == 0)
1514 while (progress < m0->m_pkthdr.len) {
1515 length = m0->m_pkthdr.len - progress;
1516 if (length > MCLBYTES)
1519 if (m_new == NULL) {
1521 m_new = m_getcl(how, MT_DATA, 0);
1523 m_new = m_get(how, MT_DATA);
1528 m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1530 m_new->m_len = length;
1531 if (m_new != m_final)
1532 m_cat(m_final, m_new);
1535 #ifdef MBUF_STRESS_TEST
1536 if (m0->m_next == NULL)
1541 #ifdef MBUF_STRESS_TEST
1543 m_defragbytes += m0->m_pkthdr.len;
1547 #ifdef MBUF_STRESS_TEST
1556 * Defragment an mbuf chain, returning at most maxfrags separate
1557 * mbufs+clusters. If this is not possible NULL is returned and
1558 * the original mbuf chain is left in it's present (potentially
1559 * modified) state. We use two techniques: collapsing consecutive
1560 * mbufs and replacing consecutive mbufs by a cluster.
1562 * NB: this should really be named m_defrag but that name is taken
1565 m_collapse(struct mbuf *m0, int how, int maxfrags)
1567 struct mbuf *m, *n, *n2, **prev;
1571 * Calculate the current number of frags.
1574 for (m = m0; m != NULL; m = m->m_next)
1577 * First, try to collapse mbufs. Note that we always collapse
1578 * towards the front so we don't need to deal with moving the
1579 * pkthdr. This may be suboptimal if the first mbuf has much
1580 * less data than the following.
1588 if (M_WRITABLE(m) &&
1589 n->m_len < M_TRAILINGSPACE(m)) {
1590 bcopy(mtod(n, void *), mtod(m, char *) + m->m_len,
1592 m->m_len += n->m_len;
1593 m->m_next = n->m_next;
1595 if (--curfrags <= maxfrags)
1600 KASSERT(maxfrags > 1,
1601 ("maxfrags %u, but normal collapse failed", maxfrags));
1603 * Collapse consecutive mbufs to a cluster.
1605 prev = &m0->m_next; /* NB: not the first mbuf */
1606 while ((n = *prev) != NULL) {
1607 if ((n2 = n->m_next) != NULL &&
1608 n->m_len + n2->m_len < MCLBYTES) {
1609 m = m_getcl(how, MT_DATA, 0);
1612 bcopy(mtod(n, void *), mtod(m, void *), n->m_len);
1613 bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len,
1615 m->m_len = n->m_len + n2->m_len;
1616 m->m_next = n2->m_next;
1620 if (--curfrags <= maxfrags) /* +1 cl -2 mbufs */
1623 * Still not there, try the normal collapse
1624 * again before we allocate another cluster.
1631 * No place where we can collapse to a cluster; punt.
1632 * This can occur if, for example, you request 2 frags
1633 * but the packet requires that both be clusters (we
1634 * never reallocate the first mbuf to avoid moving the
1641 #ifdef MBUF_STRESS_TEST
1644 * Fragment an mbuf chain. There's no reason you'd ever want to do
1645 * this in normal usage, but it's great for stress testing various
1648 * If fragmentation is not possible, the original chain will be
1651 * Possible length values:
1652 * 0 no fragmentation will occur
1653 * > 0 each fragment will be of the specified length
1654 * -1 each fragment will be the same random value in length
1655 * -2 each fragment's length will be entirely random
1656 * (Random values range from 1 to 256)
1659 m_fragment(struct mbuf *m0, int how, int length)
1661 struct mbuf *m_new = NULL, *m_final = NULL;
1664 if (!(m0->m_flags & M_PKTHDR))
1667 if ((length == 0) || (length < -2))
1670 m_fixhdr(m0); /* Needed sanity check */
1672 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1674 if (m_final == NULL)
1677 if (m_dup_pkthdr(m_final, m0, how) == 0)
1683 length = 1 + (arc4random() & 255);
1685 while (progress < m0->m_pkthdr.len) {
1691 fraglen = 1 + (arc4random() & 255);
1692 if (fraglen > m0->m_pkthdr.len - progress)
1693 fraglen = m0->m_pkthdr.len - progress;
1695 if (fraglen > MCLBYTES)
1698 if (m_new == NULL) {
1699 m_new = m_getcl(how, MT_DATA, 0);
1704 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
1705 progress += fraglen;
1706 m_new->m_len = fraglen;
1707 if (m_new != m_final)
1708 m_cat(m_final, m_new);
1717 /* Return the original chain on failure */
1724 * Copy the contents of uio into a properly sized mbuf chain.
1727 m_uiotombuf(struct uio *uio, int how, int len, int align, int flags)
1729 struct mbuf *m, *mb;
1735 * len can be zero or an arbitrary large value bound by
1736 * the total data supplied by the uio.
1739 total = min(uio->uio_resid, len);
1741 total = uio->uio_resid;
1744 * The smallest unit returned by m_getm2() is a single mbuf
1745 * with pkthdr. We can't align past it.
1751 * Give us the full allocation or nothing.
1752 * If len is zero return the smallest empty mbuf.
1754 m = m_getm2(NULL, max(total + align, 1), how, MT_DATA, flags);
1759 /* Fill all mbufs with uio data and update header information. */
1760 for (mb = m; mb != NULL; mb = mb->m_next) {
1761 length = min(M_TRAILINGSPACE(mb), total - progress);
1763 error = uiomove(mtod(mb, void *), length, uio);
1771 if (flags & M_PKTHDR)
1772 m->m_pkthdr.len += length;
1774 KASSERT(progress == total, ("%s: progress != total", __func__));
1780 * Copy an mbuf chain into a uio limited by len if set.
1783 m_mbuftouio(struct uio *uio, struct mbuf *m, int len)
1785 int error, length, total;
1789 total = min(uio->uio_resid, len);
1791 total = uio->uio_resid;
1793 /* Fill the uio with data from the mbufs. */
1794 for (; m != NULL; m = m->m_next) {
1795 length = min(m->m_len, total - progress);
1797 error = uiomove(mtod(m, void *), length, uio);
1808 * Set the m_data pointer of a newly-allocated mbuf
1809 * to place an object of the specified size at the
1810 * end of the mbuf, longword aligned.
1813 m_align(struct mbuf *m, int len)
1817 if (m->m_flags & M_EXT)
1818 adjust = m->m_ext.ext_size - len;
1819 else if (m->m_flags & M_PKTHDR)
1820 adjust = MHLEN - len;
1822 adjust = MLEN - len;
1823 m->m_data += adjust &~ (sizeof(long)-1);
1827 * Create a writable copy of the mbuf chain. While doing this
1828 * we compact the chain with a goal of producing a chain with
1829 * at most two mbufs. The second mbuf in this chain is likely
1830 * to be a cluster. The primary purpose of this work is to create
1831 * a writable packet for encryption, compression, etc. The
1832 * secondary goal is to linearize the data so the data can be
1833 * passed to crypto hardware in the most efficient manner possible.
1836 m_unshare(struct mbuf *m0, int how)
1838 struct mbuf *m, *mprev;
1839 struct mbuf *n, *mfirst, *mlast;
1843 for (m = m0; m != NULL; m = mprev->m_next) {
1845 * Regular mbufs are ignored unless there's a cluster
1846 * in front of it that we can use to coalesce. We do
1847 * the latter mainly so later clusters can be coalesced
1848 * also w/o having to handle them specially (i.e. convert
1849 * mbuf+cluster -> cluster). This optimization is heavily
1850 * influenced by the assumption that we're running over
1851 * Ethernet where MCLBYTES is large enough that the max
1852 * packet size will permit lots of coalescing into a
1853 * single cluster. This in turn permits efficient
1854 * crypto operations, especially when using hardware.
1856 if ((m->m_flags & M_EXT) == 0) {
1857 if (mprev && (mprev->m_flags & M_EXT) &&
1858 m->m_len <= M_TRAILINGSPACE(mprev)) {
1859 /* XXX: this ignores mbuf types */
1860 memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1861 mtod(m, caddr_t), m->m_len);
1862 mprev->m_len += m->m_len;
1863 mprev->m_next = m->m_next; /* unlink from chain */
1864 m_free(m); /* reclaim mbuf */
1866 newipsecstat.ips_mbcoalesced++;
1874 * Writable mbufs are left alone (for now).
1876 if (M_WRITABLE(m)) {
1882 * Not writable, replace with a copy or coalesce with
1883 * the previous mbuf if possible (since we have to copy
1884 * it anyway, we try to reduce the number of mbufs and
1885 * clusters so that future work is easier).
1887 KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags));
1888 /* NB: we only coalesce into a cluster or larger */
1889 if (mprev != NULL && (mprev->m_flags & M_EXT) &&
1890 m->m_len <= M_TRAILINGSPACE(mprev)) {
1891 /* XXX: this ignores mbuf types */
1892 memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1893 mtod(m, caddr_t), m->m_len);
1894 mprev->m_len += m->m_len;
1895 mprev->m_next = m->m_next; /* unlink from chain */
1896 m_free(m); /* reclaim mbuf */
1898 newipsecstat.ips_clcoalesced++;
1904 * Allocate new space to hold the copy...
1906 /* XXX why can M_PKTHDR be set past the first mbuf? */
1907 if (mprev == NULL && (m->m_flags & M_PKTHDR)) {
1909 * NB: if a packet header is present we must
1910 * allocate the mbuf separately from any cluster
1911 * because M_MOVE_PKTHDR will smash the data
1912 * pointer and drop the M_EXT marker.
1914 MGETHDR(n, how, m->m_type);
1919 M_MOVE_PKTHDR(n, m);
1921 if ((n->m_flags & M_EXT) == 0) {
1927 n = m_getcl(how, m->m_type, m->m_flags);
1934 * ... and copy the data. We deal with jumbo mbufs
1935 * (i.e. m_len > MCLBYTES) by splitting them into
1936 * clusters. We could just malloc a buffer and make
1937 * it external but too many device drivers don't know
1938 * how to break up the non-contiguous memory when
1946 int cc = min(len, MCLBYTES);
1947 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc);
1953 newipsecstat.ips_clcopied++;
1961 n = m_getcl(how, m->m_type, m->m_flags);
1968 n->m_next = m->m_next;
1970 m0 = mfirst; /* new head of chain */
1972 mprev->m_next = mfirst; /* replace old mbuf */
1973 m_free(m); /* release old mbuf */
1979 #ifdef MBUF_PROFILING
1981 #define MP_BUCKETS 32 /* don't just change this as things may overflow.*/
1982 struct mbufprofile {
1983 uintmax_t wasted[MP_BUCKETS];
1984 uintmax_t used[MP_BUCKETS];
1985 uintmax_t segments[MP_BUCKETS];
1988 #define MP_MAXDIGITS 21 /* strlen("16,000,000,000,000,000,000") == 21 */
1989 #define MP_NUMLINES 6
1990 #define MP_NUMSPERLINE 16
1991 #define MP_EXTRABYTES 64 /* > strlen("used:\nwasted:\nsegments:\n") */
1992 /* work out max space needed and add a bit of spare space too */
1993 #define MP_MAXLINE ((MP_MAXDIGITS+1) * MP_NUMSPERLINE)
1994 #define MP_BUFSIZE ((MP_MAXLINE * MP_NUMLINES) + 1 + MP_EXTRABYTES)
1996 char mbprofbuf[MP_BUFSIZE];
1999 m_profile(struct mbuf *m)
2008 if (m->m_flags & M_EXT) {
2009 wasted += MHLEN - sizeof(m->m_ext) +
2010 m->m_ext.ext_size - m->m_len;
2012 if (m->m_flags & M_PKTHDR)
2013 wasted += MHLEN - m->m_len;
2015 wasted += MLEN - m->m_len;
2019 /* be paranoid.. it helps */
2020 if (segments > MP_BUCKETS - 1)
2021 segments = MP_BUCKETS - 1;
2024 if (wasted > 100000)
2026 /* store in the appropriate bucket */
2027 /* don't bother locking. if it's slightly off, so what? */
2028 mbprof.segments[segments]++;
2029 mbprof.used[fls(used)]++;
2030 mbprof.wasted[fls(wasted)]++;
2034 mbprof_textify(void)
2041 p = &mbprof.wasted[0];
2043 offset = snprintf(c, MP_MAXLINE + 10,
2045 "%ju %ju %ju %ju %ju %ju %ju %ju "
2046 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2047 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2048 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2050 p = &mbprof.wasted[16];
2052 offset = snprintf(c, MP_MAXLINE,
2053 "%ju %ju %ju %ju %ju %ju %ju %ju "
2054 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2055 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2056 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2058 p = &mbprof.used[0];
2060 offset = snprintf(c, MP_MAXLINE + 10,
2062 "%ju %ju %ju %ju %ju %ju %ju %ju "
2063 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2064 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2065 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2067 p = &mbprof.used[16];
2069 offset = snprintf(c, MP_MAXLINE,
2070 "%ju %ju %ju %ju %ju %ju %ju %ju "
2071 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2072 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2073 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2075 p = &mbprof.segments[0];
2077 offset = snprintf(c, MP_MAXLINE + 10,
2079 "%ju %ju %ju %ju %ju %ju %ju %ju "
2080 "%ju %ju %ju %ju %ju %ju %ju %ju\n",
2081 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2082 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2084 p = &mbprof.segments[16];
2086 offset = snprintf(c, MP_MAXLINE,
2087 "%ju %ju %ju %ju %ju %ju %ju %ju "
2088 "%ju %ju %ju %ju %ju %ju %ju %jju",
2089 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
2090 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
2095 mbprof_handler(SYSCTL_HANDLER_ARGS)
2100 error = SYSCTL_OUT(req, mbprofbuf, strlen(mbprofbuf) + 1);
2105 mbprof_clr_handler(SYSCTL_HANDLER_ARGS)
2110 error = sysctl_handle_int(oidp, &clear, 0, req);
2111 if (error || !req->newptr)
2115 bzero(&mbprof, sizeof(mbprof));
2122 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofile, CTLTYPE_STRING|CTLFLAG_RD,
2123 NULL, 0, mbprof_handler, "A", "mbuf profiling statistics");
2125 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbufprofileclr, CTLTYPE_INT|CTLFLAG_RW,
2126 NULL, 0, mbprof_clr_handler, "I", "clear mbuf profiling statistics");