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