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