uipc_mbuf.c revision 1.158.4.3 1 /* $NetBSD: uipc_mbuf.c,v 1.158.4.3 2018/04/17 08:27:18 martin Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1988, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.158.4.3 2018/04/17 08:27:18 martin Exp $");
66
67 #include "opt_mbuftrace.h"
68 #include "opt_nmbclusters.h"
69 #include "opt_ddb.h"
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/atomic.h>
74 #include <sys/cpu.h>
75 #include <sys/proc.h>
76 #include <sys/mbuf.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
79 #include <sys/domain.h>
80 #include <sys/protosw.h>
81 #include <sys/percpu.h>
82 #include <sys/pool.h>
83 #include <sys/socket.h>
84 #include <sys/sysctl.h>
85
86 #include <net/if.h>
87
88 pool_cache_t mb_cache; /* mbuf cache */
89 pool_cache_t mcl_cache; /* mbuf cluster cache */
90
91 struct mbstat mbstat;
92 int max_linkhdr;
93 int max_protohdr;
94 int max_hdr;
95 int max_datalen;
96
97 static int mb_ctor(void *, void *, int);
98
99 static void sysctl_kern_mbuf_setup(void);
100
101 static struct sysctllog *mbuf_sysctllog;
102
103 static struct mbuf *m_copym0(struct mbuf *, int, int, int, int);
104 static struct mbuf *m_split0(struct mbuf *, int, int, int);
105 static int m_copyback0(struct mbuf **, int, int, const void *, int, int);
106
107 /* flags for m_copyback0 */
108 #define M_COPYBACK0_COPYBACK 0x0001 /* copyback from cp */
109 #define M_COPYBACK0_PRESERVE 0x0002 /* preserve original data */
110 #define M_COPYBACK0_COW 0x0004 /* do copy-on-write */
111 #define M_COPYBACK0_EXTEND 0x0008 /* extend chain */
112
113 static const char mclpool_warnmsg[] =
114 "WARNING: mclpool limit reached; increase kern.mbuf.nmbclusters";
115
116 MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
117
118 static percpu_t *mbstat_percpu;
119
120 #ifdef MBUFTRACE
121 struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
122 struct mowner unknown_mowners[] = {
123 MOWNER_INIT("unknown", "free"),
124 MOWNER_INIT("unknown", "data"),
125 MOWNER_INIT("unknown", "header"),
126 MOWNER_INIT("unknown", "soname"),
127 MOWNER_INIT("unknown", "soopts"),
128 MOWNER_INIT("unknown", "ftable"),
129 MOWNER_INIT("unknown", "control"),
130 MOWNER_INIT("unknown", "oobdata"),
131 };
132 struct mowner revoked_mowner = MOWNER_INIT("revoked", "");
133 #endif
134
135 #define MEXT_ISEMBEDDED(m) ((m)->m_ext_ref == (m))
136
137 #define MCLADDREFERENCE(o, n) \
138 do { \
139 KASSERT(((o)->m_flags & M_EXT) != 0); \
140 KASSERT(((n)->m_flags & M_EXT) == 0); \
141 KASSERT((o)->m_ext.ext_refcnt >= 1); \
142 (n)->m_flags |= ((o)->m_flags & M_EXTCOPYFLAGS); \
143 atomic_inc_uint(&(o)->m_ext.ext_refcnt); \
144 (n)->m_ext_ref = (o)->m_ext_ref; \
145 mowner_ref((n), (n)->m_flags); \
146 MCLREFDEBUGN((n), __FILE__, __LINE__); \
147 } while (/* CONSTCOND */ 0)
148
149 static int
150 nmbclusters_limit(void)
151 {
152 #if defined(PMAP_MAP_POOLPAGE)
153 /* direct mapping, doesn't use space in kmem_arena */
154 vsize_t max_size = physmem / 4;
155 #else
156 vsize_t max_size = MIN(physmem / 4, nkmempages / 4);
157 #endif
158
159 max_size = max_size * PAGE_SIZE / MCLBYTES;
160 #ifdef NMBCLUSTERS_MAX
161 max_size = MIN(max_size, NMBCLUSTERS_MAX);
162 #endif
163
164 #ifdef NMBCLUSTERS
165 return MIN(max_size, NMBCLUSTERS);
166 #else
167 return max_size;
168 #endif
169 }
170
171 /*
172 * Initialize the mbuf allocator.
173 */
174 void
175 mbinit(void)
176 {
177
178 CTASSERT(sizeof(struct _m_ext) <= MHLEN);
179 CTASSERT(sizeof(struct mbuf) == MSIZE);
180
181 sysctl_kern_mbuf_setup();
182
183 mb_cache = pool_cache_init(msize, 0, 0, 0, "mbpl",
184 NULL, IPL_VM, mb_ctor, NULL, NULL);
185 KASSERT(mb_cache != NULL);
186
187 mcl_cache = pool_cache_init(mclbytes, 0, 0, 0, "mclpl", NULL,
188 IPL_VM, NULL, NULL, NULL);
189 KASSERT(mcl_cache != NULL);
190
191 pool_cache_set_drain_hook(mb_cache, m_reclaim, NULL);
192 pool_cache_set_drain_hook(mcl_cache, m_reclaim, NULL);
193
194 /*
195 * Set an arbitrary default limit on the number of mbuf clusters.
196 */
197 #ifdef NMBCLUSTERS
198 nmbclusters = nmbclusters_limit();
199 #else
200 nmbclusters = MAX(1024,
201 (vsize_t)physmem * PAGE_SIZE / MCLBYTES / 16);
202 nmbclusters = MIN(nmbclusters, nmbclusters_limit());
203 #endif
204
205 /*
206 * Set the hard limit on the mclpool to the number of
207 * mbuf clusters the kernel is to support. Log the limit
208 * reached message max once a minute.
209 */
210 pool_cache_sethardlimit(mcl_cache, nmbclusters, mclpool_warnmsg, 60);
211
212 mbstat_percpu = percpu_alloc(sizeof(struct mbstat_cpu));
213
214 /*
215 * Set a low water mark for both mbufs and clusters. This should
216 * help ensure that they can be allocated in a memory starvation
217 * situation. This is important for e.g. diskless systems which
218 * must allocate mbufs in order for the pagedaemon to clean pages.
219 */
220 pool_cache_setlowat(mb_cache, mblowat);
221 pool_cache_setlowat(mcl_cache, mcllowat);
222
223 #ifdef MBUFTRACE
224 {
225 /*
226 * Attach the unknown mowners.
227 */
228 int i;
229 MOWNER_ATTACH(&revoked_mowner);
230 for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
231 i-- > 0; )
232 MOWNER_ATTACH(&unknown_mowners[i]);
233 }
234 #endif
235 }
236
237 /*
238 * sysctl helper routine for the kern.mbuf subtree.
239 * nmbclusters, mblowat and mcllowat need range
240 * checking and pool tweaking after being reset.
241 */
242 static int
243 sysctl_kern_mbuf(SYSCTLFN_ARGS)
244 {
245 int error, newval;
246 struct sysctlnode node;
247
248 node = *rnode;
249 node.sysctl_data = &newval;
250 switch (rnode->sysctl_num) {
251 case MBUF_NMBCLUSTERS:
252 case MBUF_MBLOWAT:
253 case MBUF_MCLLOWAT:
254 newval = *(int*)rnode->sysctl_data;
255 break;
256 default:
257 return (EOPNOTSUPP);
258 }
259
260 error = sysctl_lookup(SYSCTLFN_CALL(&node));
261 if (error || newp == NULL)
262 return (error);
263 if (newval < 0)
264 return (EINVAL);
265
266 switch (node.sysctl_num) {
267 case MBUF_NMBCLUSTERS:
268 if (newval < nmbclusters)
269 return (EINVAL);
270 if (newval > nmbclusters_limit())
271 return (EINVAL);
272 nmbclusters = newval;
273 pool_cache_sethardlimit(mcl_cache, nmbclusters,
274 mclpool_warnmsg, 60);
275 break;
276 case MBUF_MBLOWAT:
277 mblowat = newval;
278 pool_cache_setlowat(mb_cache, mblowat);
279 break;
280 case MBUF_MCLLOWAT:
281 mcllowat = newval;
282 pool_cache_setlowat(mcl_cache, mcllowat);
283 break;
284 }
285
286 return (0);
287 }
288
289 #ifdef MBUFTRACE
290 static void
291 mowner_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
292 {
293 struct mowner_counter *mc = v1;
294 struct mowner_user *mo_user = v2;
295 int i;
296
297 for (i = 0; i < MOWNER_COUNTER_NCOUNTERS; i++) {
298 mo_user->mo_counter[i] += mc->mc_counter[i];
299 }
300 }
301
302 static void
303 mowner_convert_to_user(struct mowner *mo, struct mowner_user *mo_user)
304 {
305
306 memset(mo_user, 0, sizeof(*mo_user));
307 CTASSERT(sizeof(mo_user->mo_name) == sizeof(mo->mo_name));
308 CTASSERT(sizeof(mo_user->mo_descr) == sizeof(mo->mo_descr));
309 memcpy(mo_user->mo_name, mo->mo_name, sizeof(mo->mo_name));
310 memcpy(mo_user->mo_descr, mo->mo_descr, sizeof(mo->mo_descr));
311 percpu_foreach(mo->mo_counters, mowner_conver_to_user_cb, mo_user);
312 }
313
314 static int
315 sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
316 {
317 struct mowner *mo;
318 size_t len = 0;
319 int error = 0;
320
321 if (namelen != 0)
322 return (EINVAL);
323 if (newp != NULL)
324 return (EPERM);
325
326 LIST_FOREACH(mo, &mowners, mo_link) {
327 struct mowner_user mo_user;
328
329 mowner_convert_to_user(mo, &mo_user);
330
331 if (oldp != NULL) {
332 if (*oldlenp - len < sizeof(mo_user)) {
333 error = ENOMEM;
334 break;
335 }
336 error = copyout(&mo_user, (char *)oldp + len,
337 sizeof(mo_user));
338 if (error)
339 break;
340 }
341 len += sizeof(mo_user);
342 }
343
344 if (error == 0)
345 *oldlenp = len;
346
347 return (error);
348 }
349 #endif /* MBUFTRACE */
350
351 static void
352 mbstat_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
353 {
354 struct mbstat_cpu *mbsc = v1;
355 struct mbstat *mbs = v2;
356 int i;
357
358 for (i = 0; i < __arraycount(mbs->m_mtypes); i++) {
359 mbs->m_mtypes[i] += mbsc->m_mtypes[i];
360 }
361 }
362
363 static void
364 mbstat_convert_to_user(struct mbstat *mbs)
365 {
366
367 memset(mbs, 0, sizeof(*mbs));
368 mbs->m_drain = mbstat.m_drain;
369 percpu_foreach(mbstat_percpu, mbstat_conver_to_user_cb, mbs);
370 }
371
372 static int
373 sysctl_kern_mbuf_stats(SYSCTLFN_ARGS)
374 {
375 struct sysctlnode node;
376 struct mbstat mbs;
377
378 mbstat_convert_to_user(&mbs);
379 node = *rnode;
380 node.sysctl_data = &mbs;
381 node.sysctl_size = sizeof(mbs);
382 return sysctl_lookup(SYSCTLFN_CALL(&node));
383 }
384
385 static void
386 sysctl_kern_mbuf_setup(void)
387 {
388
389 KASSERT(mbuf_sysctllog == NULL);
390 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
391 CTLFLAG_PERMANENT,
392 CTLTYPE_NODE, "mbuf",
393 SYSCTL_DESCR("mbuf control variables"),
394 NULL, 0, NULL, 0,
395 CTL_KERN, KERN_MBUF, CTL_EOL);
396
397 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
398 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
399 CTLTYPE_INT, "msize",
400 SYSCTL_DESCR("mbuf base size"),
401 NULL, msize, NULL, 0,
402 CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
403 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
404 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
405 CTLTYPE_INT, "mclbytes",
406 SYSCTL_DESCR("mbuf cluster size"),
407 NULL, mclbytes, NULL, 0,
408 CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
409 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
410 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
411 CTLTYPE_INT, "nmbclusters",
412 SYSCTL_DESCR("Limit on the number of mbuf clusters"),
413 sysctl_kern_mbuf, 0, &nmbclusters, 0,
414 CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
415 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
416 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
417 CTLTYPE_INT, "mblowat",
418 SYSCTL_DESCR("mbuf low water mark"),
419 sysctl_kern_mbuf, 0, &mblowat, 0,
420 CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
421 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
422 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
423 CTLTYPE_INT, "mcllowat",
424 SYSCTL_DESCR("mbuf cluster low water mark"),
425 sysctl_kern_mbuf, 0, &mcllowat, 0,
426 CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
427 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
428 CTLFLAG_PERMANENT,
429 CTLTYPE_STRUCT, "stats",
430 SYSCTL_DESCR("mbuf allocation statistics"),
431 sysctl_kern_mbuf_stats, 0, NULL, 0,
432 CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
433 #ifdef MBUFTRACE
434 sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
435 CTLFLAG_PERMANENT,
436 CTLTYPE_STRUCT, "mowners",
437 SYSCTL_DESCR("Information about mbuf owners"),
438 sysctl_kern_mbuf_mowners, 0, NULL, 0,
439 CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
440 #endif /* MBUFTRACE */
441 }
442
443 static int
444 mb_ctor(void *arg, void *object, int flags)
445 {
446 struct mbuf *m = object;
447
448 #ifdef POOL_VTOPHYS
449 m->m_paddr = POOL_VTOPHYS(m);
450 #else
451 m->m_paddr = M_PADDR_INVALID;
452 #endif
453 return (0);
454 }
455
456 void
457 m_pkthdr_remove(struct mbuf *m)
458 {
459 KASSERT(m->m_flags & M_PKTHDR);
460
461 if (M_READONLY(m)) {
462 /* Nothing we can do. */
463 return;
464 }
465
466 m_tag_delete_chain(m, NULL);
467 m->m_flags &= ~M_PKTHDR;
468 memset(&m->m_pkthdr, 0, sizeof(m->m_pkthdr));
469 }
470
471 /*
472 * Add mbuf to the end of a chain
473 */
474 struct mbuf *
475 m_add(struct mbuf *c, struct mbuf *m) {
476 struct mbuf *n;
477
478 if (c == NULL)
479 return m;
480
481 for (n = c; n->m_next != NULL; n = n->m_next)
482 continue;
483 n->m_next = m;
484 return c;
485 }
486
487 /*
488 * Set the m_data pointer of a newly-allocated mbuf
489 * to place an object of the specified size at the
490 * end of the mbuf, longword aligned.
491 */
492 void
493 m_align(struct mbuf *m, int len)
494 {
495 int adjust;
496
497 KASSERT(len != M_COPYALL);
498
499 if (m->m_flags & M_EXT)
500 adjust = m->m_ext.ext_size - len;
501 else if (m->m_flags & M_PKTHDR)
502 adjust = MHLEN - len;
503 else
504 adjust = MLEN - len;
505 m->m_data += adjust &~ (sizeof(long)-1);
506 }
507
508 /*
509 * Append the specified data to the indicated mbuf chain,
510 * Extend the mbuf chain if the new data does not fit in
511 * existing space.
512 *
513 * Return 1 if able to complete the job; otherwise 0.
514 */
515 int
516 m_append(struct mbuf *m0, int len, const void *cpv)
517 {
518 struct mbuf *m, *n;
519 int remainder, space;
520 const char *cp = cpv;
521
522 KASSERT(len != M_COPYALL);
523 for (m = m0; m->m_next != NULL; m = m->m_next)
524 continue;
525 remainder = len;
526 space = M_TRAILINGSPACE(m);
527 if (space > 0) {
528 /*
529 * Copy into available space.
530 */
531 if (space > remainder)
532 space = remainder;
533 memmove(mtod(m, char *) + m->m_len, cp, space);
534 m->m_len += space;
535 cp = cp + space, remainder -= space;
536 }
537 while (remainder > 0) {
538 /*
539 * Allocate a new mbuf; could check space
540 * and allocate a cluster instead.
541 */
542 n = m_get(M_DONTWAIT, m->m_type);
543 if (n == NULL)
544 break;
545 n->m_len = min(MLEN, remainder);
546 memmove(mtod(n, void *), cp, n->m_len);
547 cp += n->m_len, remainder -= n->m_len;
548 m->m_next = n;
549 m = n;
550 }
551 if (m0->m_flags & M_PKTHDR)
552 m0->m_pkthdr.len += len - remainder;
553 return (remainder == 0);
554 }
555
556 void
557 m_reclaim(void *arg, int flags)
558 {
559 struct domain *dp;
560 const struct protosw *pr;
561 struct ifnet *ifp;
562 int s;
563
564 KERNEL_LOCK(1, NULL);
565 s = splvm();
566 DOMAIN_FOREACH(dp) {
567 for (pr = dp->dom_protosw;
568 pr < dp->dom_protoswNPROTOSW; pr++)
569 if (pr->pr_drain)
570 (*pr->pr_drain)();
571 }
572 IFNET_FOREACH(ifp) {
573 if (ifp->if_drain)
574 (*ifp->if_drain)(ifp);
575 }
576 splx(s);
577 mbstat.m_drain++;
578 KERNEL_UNLOCK_ONE(NULL);
579 }
580
581 /*
582 * Space allocation routines.
583 * These are also available as macros
584 * for critical paths.
585 */
586 struct mbuf *
587 m_get(int nowait, int type)
588 {
589 struct mbuf *m;
590
591 KASSERT(type != MT_FREE);
592
593 m = pool_cache_get(mb_cache,
594 nowait == M_WAIT ? PR_WAITOK|PR_LIMITFAIL : 0);
595 if (m == NULL)
596 return NULL;
597
598 mbstat_type_add(type, 1);
599 mowner_init(m, type);
600 m->m_ext_ref = m;
601 m->m_type = type;
602 m->m_len = 0;
603 m->m_next = NULL;
604 m->m_nextpkt = NULL;
605 m->m_data = m->m_dat;
606 m->m_flags = 0;
607
608 return m;
609 }
610
611 struct mbuf *
612 m_gethdr(int nowait, int type)
613 {
614 struct mbuf *m;
615
616 m = m_get(nowait, type);
617 if (m == NULL)
618 return NULL;
619
620 m->m_data = m->m_pktdat;
621 m->m_flags = M_PKTHDR;
622 m->m_pkthdr.rcvif = NULL;
623 m->m_pkthdr.len = 0;
624 m->m_pkthdr.csum_flags = 0;
625 m->m_pkthdr.csum_data = 0;
626 SLIST_INIT(&m->m_pkthdr.tags);
627
628 return m;
629 }
630
631 struct mbuf *
632 m_getclr(int nowait, int type)
633 {
634 struct mbuf *m;
635
636 m = m_get(nowait, type);
637 if (m == 0)
638 return (NULL);
639 memset(mtod(m, void *), 0, MLEN);
640 return (m);
641 }
642
643 void
644 m_clget(struct mbuf *m, int nowait)
645 {
646
647 MCLGET(m, nowait);
648 }
649
650 struct mbuf *
651 m_free(struct mbuf *m)
652 {
653 struct mbuf *n;
654
655 MFREE(m, n);
656 return (n);
657 }
658
659 void
660 m_freem(struct mbuf *m)
661 {
662 struct mbuf *n;
663
664 if (m == NULL)
665 return;
666 do {
667 MFREE(m, n);
668 m = n;
669 } while (m);
670 }
671
672 #ifdef MBUFTRACE
673 /*
674 * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
675 */
676 void
677 m_claimm(struct mbuf *m, struct mowner *mo)
678 {
679
680 for (; m != NULL; m = m->m_next)
681 MCLAIM(m, mo);
682 }
683 #endif
684
685 /*
686 * Mbuffer utility routines.
687 */
688
689 /*
690 * Lesser-used path for M_PREPEND:
691 * allocate new mbuf to prepend to chain,
692 * copy junk along.
693 */
694 struct mbuf *
695 m_prepend(struct mbuf *m, int len, int how)
696 {
697 struct mbuf *mn;
698
699 KASSERT(len != M_COPYALL);
700 mn = m_get(how, m->m_type);
701 if (mn == NULL) {
702 m_freem(m);
703 return (NULL);
704 }
705 if (m->m_flags & M_PKTHDR) {
706 M_MOVE_PKTHDR(mn, m);
707 } else {
708 MCLAIM(mn, m->m_owner);
709 }
710 mn->m_next = m;
711 m = mn;
712 if (len < MHLEN)
713 MH_ALIGN(m, len);
714 m->m_len = len;
715 return (m);
716 }
717
718 /*
719 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
720 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
721 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
722 */
723 int MCFail;
724
725 struct mbuf *
726 m_copym(struct mbuf *m, int off0, int len, int wait)
727 {
728
729 return m_copym0(m, off0, len, wait, 0); /* shallow copy on M_EXT */
730 }
731
732 struct mbuf *
733 m_dup(struct mbuf *m, int off0, int len, int wait)
734 {
735
736 return m_copym0(m, off0, len, wait, 1); /* deep copy */
737 }
738
739 static inline int
740 m_copylen(int len, int copylen) {
741 return len == M_COPYALL ? copylen : min(len, copylen);
742 }
743
744 static struct mbuf *
745 m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
746 {
747 struct mbuf *n, **np;
748 int off = off0;
749 struct mbuf *top;
750 int copyhdr = 0;
751
752 if (off < 0 || (len != M_COPYALL && len < 0))
753 panic("m_copym: off %d, len %d", off, len);
754 if (off == 0 && m->m_flags & M_PKTHDR)
755 copyhdr = 1;
756 while (off > 0) {
757 if (m == 0)
758 panic("m_copym: m == 0, off %d", off);
759 if (off < m->m_len)
760 break;
761 off -= m->m_len;
762 m = m->m_next;
763 }
764 np = ⊤
765 top = 0;
766 while (len == M_COPYALL || len > 0) {
767 if (m == 0) {
768 if (len != M_COPYALL)
769 panic("m_copym: m == 0, len %d [!COPYALL]",
770 len);
771 break;
772 }
773 n = m_get(wait, m->m_type);
774 *np = n;
775 if (n == 0)
776 goto nospace;
777 MCLAIM(n, m->m_owner);
778 if (copyhdr) {
779 M_COPY_PKTHDR(n, m);
780 if (len == M_COPYALL)
781 n->m_pkthdr.len -= off0;
782 else
783 n->m_pkthdr.len = len;
784 copyhdr = 0;
785 }
786 n->m_len = m_copylen(len, m->m_len - off);
787 if (m->m_flags & M_EXT) {
788 if (!deep) {
789 n->m_data = m->m_data + off;
790 MCLADDREFERENCE(m, n);
791 } else {
792 /*
793 * we are unsure about the way m was allocated.
794 * copy into multiple MCLBYTES cluster mbufs.
795 *
796 * recompute m_len, it is no longer valid if MCLGET()
797 * fails to allocate a cluster. Then we try to split
798 * the source into normal sized mbufs.
799 */
800 MCLGET(n, wait);
801 n->m_len = 0;
802 n->m_len = M_TRAILINGSPACE(n);
803 n->m_len = m_copylen(len, n->m_len);
804 n->m_len = min(n->m_len, m->m_len - off);
805 memcpy(mtod(n, void *), mtod(m, char *) + off,
806 (unsigned)n->m_len);
807 }
808 } else
809 memcpy(mtod(n, void *), mtod(m, char *) + off,
810 (unsigned)n->m_len);
811 if (len != M_COPYALL)
812 len -= n->m_len;
813 off += n->m_len;
814 #ifdef DIAGNOSTIC
815 if (off > m->m_len)
816 panic("m_copym0 overrun %d %d", off, m->m_len);
817 #endif
818 if (off == m->m_len) {
819 m = m->m_next;
820 off = 0;
821 }
822 np = &n->m_next;
823 }
824 if (top == 0)
825 MCFail++;
826 return (top);
827 nospace:
828 m_freem(top);
829 MCFail++;
830 return (NULL);
831 }
832
833 /*
834 * Copy an entire packet, including header (which must be present).
835 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
836 */
837 struct mbuf *
838 m_copypacket(struct mbuf *m, int how)
839 {
840 struct mbuf *top, *n, *o;
841
842 n = m_get(how, m->m_type);
843 top = n;
844 if (!n)
845 goto nospace;
846
847 MCLAIM(n, m->m_owner);
848 M_COPY_PKTHDR(n, m);
849 n->m_len = m->m_len;
850 if (m->m_flags & M_EXT) {
851 n->m_data = m->m_data;
852 MCLADDREFERENCE(m, n);
853 } else {
854 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
855 }
856
857 m = m->m_next;
858 while (m) {
859 o = m_get(how, m->m_type);
860 if (!o)
861 goto nospace;
862
863 MCLAIM(o, m->m_owner);
864 n->m_next = o;
865 n = n->m_next;
866
867 n->m_len = m->m_len;
868 if (m->m_flags & M_EXT) {
869 n->m_data = m->m_data;
870 MCLADDREFERENCE(m, n);
871 } else {
872 memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
873 }
874
875 m = m->m_next;
876 }
877 return top;
878 nospace:
879 m_freem(top);
880 MCFail++;
881 return NULL;
882 }
883
884 /*
885 * Copy data from an mbuf chain starting "off" bytes from the beginning,
886 * continuing for "len" bytes, into the indicated buffer.
887 */
888 void
889 m_copydata(struct mbuf *m, int off, int len, void *vp)
890 {
891 unsigned count;
892 void * cp = vp;
893 struct mbuf *m0 = m;
894 int len0 = len;
895 int off0 = off;
896 void *vp0 = vp;
897
898 KASSERT(len != M_COPYALL);
899 if (off < 0 || len < 0)
900 panic("m_copydata: off %d, len %d", off, len);
901 while (off > 0) {
902 if (m == NULL)
903 panic("m_copydata(%p,%d,%d,%p): m=NULL, off=%d (%d)",
904 m0, len0, off0, vp0, off, off0 - off);
905 if (off < m->m_len)
906 break;
907 off -= m->m_len;
908 m = m->m_next;
909 }
910 while (len > 0) {
911 if (m == NULL)
912 panic("m_copydata(%p,%d,%d,%p): "
913 "m=NULL, off=%d (%d), len=%d (%d)",
914 m0, len0, off0, vp0,
915 off, off0 - off, len, len0 - len);
916 count = min(m->m_len - off, len);
917 memcpy(cp, mtod(m, char *) + off, count);
918 len -= count;
919 cp = (char *)cp + count;
920 off = 0;
921 m = m->m_next;
922 }
923 }
924
925 /*
926 * Concatenate mbuf chain n to m.
927 * n might be copied into m (when n->m_len is small), therefore data portion of
928 * n could be copied into an mbuf of different mbuf type.
929 * Any m_pkthdr is not updated.
930 */
931 void
932 m_cat(struct mbuf *m, struct mbuf *n)
933 {
934
935 while (m->m_next)
936 m = m->m_next;
937 while (n) {
938 if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
939 /* just join the two chains */
940 m->m_next = n;
941 return;
942 }
943 /* splat the data from one into the other */
944 memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
945 (u_int)n->m_len);
946 m->m_len += n->m_len;
947 n = m_free(n);
948 }
949 }
950
951 void
952 m_adj(struct mbuf *mp, int req_len)
953 {
954 int len = req_len;
955 struct mbuf *m;
956 int count;
957
958 if ((m = mp) == NULL)
959 return;
960 if (len >= 0) {
961 /*
962 * Trim from head.
963 */
964 while (m != NULL && len > 0) {
965 if (m->m_len <= len) {
966 len -= m->m_len;
967 m->m_len = 0;
968 m = m->m_next;
969 } else {
970 m->m_len -= len;
971 m->m_data += len;
972 len = 0;
973 }
974 }
975 m = mp;
976 if (mp->m_flags & M_PKTHDR)
977 m->m_pkthdr.len -= (req_len - len);
978 } else {
979 /*
980 * Trim from tail. Scan the mbuf chain,
981 * calculating its length and finding the last mbuf.
982 * If the adjustment only affects this mbuf, then just
983 * adjust and return. Otherwise, rescan and truncate
984 * after the remaining size.
985 */
986 len = -len;
987 count = 0;
988 for (;;) {
989 count += m->m_len;
990 if (m->m_next == (struct mbuf *)0)
991 break;
992 m = m->m_next;
993 }
994 if (m->m_len >= len) {
995 m->m_len -= len;
996 if (mp->m_flags & M_PKTHDR)
997 mp->m_pkthdr.len -= len;
998 return;
999 }
1000 count -= len;
1001 if (count < 0)
1002 count = 0;
1003 /*
1004 * Correct length for chain is "count".
1005 * Find the mbuf with last data, adjust its length,
1006 * and toss data from remaining mbufs on chain.
1007 */
1008 m = mp;
1009 if (m->m_flags & M_PKTHDR)
1010 m->m_pkthdr.len = count;
1011 for (; m; m = m->m_next) {
1012 if (m->m_len >= count) {
1013 m->m_len = count;
1014 break;
1015 }
1016 count -= m->m_len;
1017 }
1018 if (m)
1019 while (m->m_next)
1020 (m = m->m_next)->m_len = 0;
1021 }
1022 }
1023
1024 /*
1025 * m_ensure_contig: rearrange an mbuf chain that given length of bytes
1026 * would be contiguous and in the data area of an mbuf (therefore, mtod()
1027 * would work for a structure of given length).
1028 *
1029 * => On success, returns true and the resulting mbuf chain; false otherwise.
1030 * => The mbuf chain may change, but is always preserved valid.
1031 */
1032 bool
1033 m_ensure_contig(struct mbuf **m0, int len)
1034 {
1035 struct mbuf *n = *m0, *m;
1036 size_t count, space;
1037
1038 KASSERT(len != M_COPYALL);
1039 /*
1040 * If first mbuf has no cluster, and has room for len bytes
1041 * without shifting current data, pullup into it,
1042 * otherwise allocate a new mbuf to prepend to the chain.
1043 */
1044 if ((n->m_flags & M_EXT) == 0 &&
1045 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
1046 if (n->m_len >= len) {
1047 return true;
1048 }
1049 m = n;
1050 n = n->m_next;
1051 len -= m->m_len;
1052 } else {
1053 if (len > MHLEN) {
1054 return false;
1055 }
1056 m = m_get(M_DONTWAIT, n->m_type);
1057 if (m == NULL) {
1058 return false;
1059 }
1060 MCLAIM(m, n->m_owner);
1061 if (n->m_flags & M_PKTHDR) {
1062 M_MOVE_PKTHDR(m, n);
1063 }
1064 }
1065 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1066 do {
1067 count = MIN(MIN(MAX(len, max_protohdr), space), n->m_len);
1068 memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
1069 (unsigned)count);
1070 len -= count;
1071 m->m_len += count;
1072 n->m_len -= count;
1073 space -= count;
1074 if (n->m_len)
1075 n->m_data += count;
1076 else
1077 n = m_free(n);
1078 } while (len > 0 && n);
1079
1080 m->m_next = n;
1081 *m0 = m;
1082
1083 return len <= 0;
1084 }
1085
1086 /*
1087 * m_pullup: same as m_ensure_contig(), but destroys mbuf chain on error.
1088 */
1089 int MPFail;
1090
1091 struct mbuf *
1092 m_pullup(struct mbuf *n, int len)
1093 {
1094 struct mbuf *m = n;
1095
1096 KASSERT(len != M_COPYALL);
1097 if (!m_ensure_contig(&m, len)) {
1098 KASSERT(m != NULL);
1099 m_freem(m);
1100 MPFail++;
1101 m = NULL;
1102 }
1103 return m;
1104 }
1105
1106 /*
1107 * Like m_pullup(), except a new mbuf is always allocated, and we allow
1108 * the amount of empty space before the data in the new mbuf to be specified
1109 * (in the event that the caller expects to prepend later).
1110 */
1111 int MSFail;
1112
1113 struct mbuf *
1114 m_copyup(struct mbuf *n, int len, int dstoff)
1115 {
1116 struct mbuf *m;
1117 int count, space;
1118
1119 KASSERT(len != M_COPYALL);
1120 if (len > (MHLEN - dstoff))
1121 goto bad;
1122 m = m_get(M_DONTWAIT, n->m_type);
1123 if (m == NULL)
1124 goto bad;
1125 MCLAIM(m, n->m_owner);
1126 if (n->m_flags & M_PKTHDR) {
1127 M_MOVE_PKTHDR(m, n);
1128 }
1129 m->m_data += dstoff;
1130 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1131 do {
1132 count = min(min(max(len, max_protohdr), space), n->m_len);
1133 memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
1134 (unsigned)count);
1135 len -= count;
1136 m->m_len += count;
1137 n->m_len -= count;
1138 space -= count;
1139 if (n->m_len)
1140 n->m_data += count;
1141 else
1142 n = m_free(n);
1143 } while (len > 0 && n);
1144 if (len > 0) {
1145 (void) m_free(m);
1146 goto bad;
1147 }
1148 m->m_next = n;
1149 return (m);
1150 bad:
1151 m_freem(n);
1152 MSFail++;
1153 return (NULL);
1154 }
1155
1156 /*
1157 * Partition an mbuf chain in two pieces, returning the tail --
1158 * all but the first len0 bytes. In case of failure, it returns NULL and
1159 * attempts to restore the chain to its original state.
1160 */
1161 struct mbuf *
1162 m_split(struct mbuf *m0, int len0, int wait)
1163 {
1164
1165 return m_split0(m0, len0, wait, 1);
1166 }
1167
1168 static struct mbuf *
1169 m_split0(struct mbuf *m0, int len0, int wait, int copyhdr)
1170 {
1171 struct mbuf *m, *n;
1172 unsigned len = len0, remain, len_save;
1173
1174 KASSERT(len0 != M_COPYALL);
1175 for (m = m0; m && len > m->m_len; m = m->m_next)
1176 len -= m->m_len;
1177 if (m == 0)
1178 return (NULL);
1179 remain = m->m_len - len;
1180 if (copyhdr && (m0->m_flags & M_PKTHDR)) {
1181 n = m_gethdr(wait, m0->m_type);
1182 if (n == NULL)
1183 return NULL;
1184 MCLAIM(n, m0->m_owner);
1185 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1186 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1187 len_save = m0->m_pkthdr.len;
1188 m0->m_pkthdr.len = len0;
1189 if (m->m_flags & M_EXT)
1190 goto extpacket;
1191 if (remain > MHLEN) {
1192 /* m can't be the lead packet */
1193 MH_ALIGN(n, 0);
1194 n->m_len = 0;
1195 n->m_next = m_split(m, len, wait);
1196 if (n->m_next == 0) {
1197 (void) m_free(n);
1198 m0->m_pkthdr.len = len_save;
1199 return (NULL);
1200 } else
1201 return (n);
1202 } else
1203 MH_ALIGN(n, remain);
1204 } else if (remain == 0) {
1205 n = m->m_next;
1206 m->m_next = 0;
1207 return (n);
1208 } else {
1209 n = m_get(wait, m->m_type);
1210 if (n == 0)
1211 return (NULL);
1212 MCLAIM(n, m->m_owner);
1213 M_ALIGN(n, remain);
1214 }
1215 extpacket:
1216 if (m->m_flags & M_EXT) {
1217 n->m_data = m->m_data + len;
1218 MCLADDREFERENCE(m, n);
1219 } else {
1220 memcpy(mtod(n, void *), mtod(m, char *) + len, remain);
1221 }
1222 n->m_len = remain;
1223 m->m_len = len;
1224 n->m_next = m->m_next;
1225 m->m_next = 0;
1226 return (n);
1227 }
1228 /*
1229 * Routine to copy from device local memory into mbufs.
1230 */
1231 struct mbuf *
1232 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
1233 void (*copy)(const void *from, void *to, size_t len))
1234 {
1235 struct mbuf *m;
1236 struct mbuf *top = 0, **mp = ⊤
1237 int off = off0, len;
1238 char *cp;
1239 char *epkt;
1240
1241 cp = buf;
1242 epkt = cp + totlen;
1243 if (off) {
1244 /*
1245 * If 'off' is non-zero, packet is trailer-encapsulated,
1246 * so we have to skip the type and length fields.
1247 */
1248 cp += off + 2 * sizeof(uint16_t);
1249 totlen -= 2 * sizeof(uint16_t);
1250 }
1251 m = m_gethdr(M_DONTWAIT, MT_DATA);
1252 if (m == NULL)
1253 return NULL;
1254 m->m_pkthdr.rcvif = ifp;
1255 m->m_pkthdr.len = totlen;
1256 m->m_len = MHLEN;
1257
1258 while (totlen > 0) {
1259 if (top) {
1260 m = m_get(M_DONTWAIT, MT_DATA);
1261 if (m == 0) {
1262 m_freem(top);
1263 return (NULL);
1264 }
1265 m->m_len = MLEN;
1266 }
1267 len = min(totlen, epkt - cp);
1268 if (len >= MINCLSIZE) {
1269 MCLGET(m, M_DONTWAIT);
1270 if ((m->m_flags & M_EXT) == 0) {
1271 m_free(m);
1272 m_freem(top);
1273 return (NULL);
1274 }
1275 m->m_len = len = min(len, MCLBYTES);
1276 } else {
1277 /*
1278 * Place initial small packet/header at end of mbuf.
1279 */
1280 if (len < m->m_len) {
1281 if (top == 0 && len + max_linkhdr <= m->m_len)
1282 m->m_data += max_linkhdr;
1283 m->m_len = len;
1284 } else
1285 len = m->m_len;
1286 }
1287 if (copy)
1288 copy(cp, mtod(m, void *), (size_t)len);
1289 else
1290 memcpy(mtod(m, void *), cp, (size_t)len);
1291 cp += len;
1292 *mp = m;
1293 mp = &m->m_next;
1294 totlen -= len;
1295 if (cp == epkt)
1296 cp = buf;
1297 }
1298 return (top);
1299 }
1300
1301 /*
1302 * Copy data from a buffer back into the indicated mbuf chain,
1303 * starting "off" bytes from the beginning, extending the mbuf
1304 * chain if necessary.
1305 */
1306 void
1307 m_copyback(struct mbuf *m0, int off, int len, const void *cp)
1308 {
1309 #if defined(DEBUG)
1310 struct mbuf *origm = m0;
1311 int error;
1312 #endif /* defined(DEBUG) */
1313
1314 if (m0 == NULL)
1315 return;
1316
1317 #if defined(DEBUG)
1318 error =
1319 #endif /* defined(DEBUG) */
1320 m_copyback0(&m0, off, len, cp,
1321 M_COPYBACK0_COPYBACK|M_COPYBACK0_EXTEND, M_DONTWAIT);
1322
1323 #if defined(DEBUG)
1324 if (error != 0 || (m0 != NULL && origm != m0))
1325 panic("m_copyback");
1326 #endif /* defined(DEBUG) */
1327 }
1328
1329 struct mbuf *
1330 m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
1331 {
1332 int error;
1333
1334 /* don't support chain expansion */
1335 KASSERT(len != M_COPYALL);
1336 KDASSERT(off + len <= m_length(m0));
1337
1338 error = m_copyback0(&m0, off, len, cp,
1339 M_COPYBACK0_COPYBACK|M_COPYBACK0_COW, how);
1340 if (error) {
1341 /*
1342 * no way to recover from partial success.
1343 * just free the chain.
1344 */
1345 m_freem(m0);
1346 return NULL;
1347 }
1348 return m0;
1349 }
1350
1351 /*
1352 * m_makewritable: ensure the specified range writable.
1353 */
1354 int
1355 m_makewritable(struct mbuf **mp, int off, int len, int how)
1356 {
1357 int error;
1358 #if defined(DEBUG)
1359 int origlen = m_length(*mp);
1360 #endif /* defined(DEBUG) */
1361
1362 error = m_copyback0(mp, off, len, NULL,
1363 M_COPYBACK0_PRESERVE|M_COPYBACK0_COW, how);
1364
1365 #if defined(DEBUG)
1366 int reslen = 0;
1367 for (struct mbuf *n = *mp; n; n = n->m_next)
1368 reslen += n->m_len;
1369 if (origlen != reslen)
1370 panic("m_makewritable: length changed");
1371 if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
1372 panic("m_makewritable: inconsist");
1373 #endif /* defined(DEBUG) */
1374
1375 return error;
1376 }
1377
1378 /*
1379 * Copy the mbuf chain to a new mbuf chain that is as short as possible.
1380 * Return the new mbuf chain on success, NULL on failure. On success,
1381 * free the old mbuf chain.
1382 */
1383 struct mbuf *
1384 m_defrag(struct mbuf *mold, int flags)
1385 {
1386 struct mbuf *m0, *mn, *n;
1387 size_t sz = mold->m_pkthdr.len;
1388
1389 #ifdef DIAGNOSTIC
1390 if ((mold->m_flags & M_PKTHDR) == 0)
1391 panic("m_defrag: not a mbuf chain header");
1392 #endif
1393
1394 m0 = m_gethdr(flags, MT_DATA);
1395 if (m0 == NULL)
1396 return NULL;
1397 M_COPY_PKTHDR(m0, mold);
1398 mn = m0;
1399
1400 do {
1401 if (sz > MHLEN) {
1402 MCLGET(mn, M_DONTWAIT);
1403 if ((mn->m_flags & M_EXT) == 0) {
1404 m_freem(m0);
1405 return NULL;
1406 }
1407 }
1408
1409 mn->m_len = MIN(sz, MCLBYTES);
1410
1411 m_copydata(mold, mold->m_pkthdr.len - sz, mn->m_len,
1412 mtod(mn, void *));
1413
1414 sz -= mn->m_len;
1415
1416 if (sz > 0) {
1417 /* need more mbufs */
1418 n = m_get(M_NOWAIT, MT_DATA);
1419 if (n == NULL) {
1420 m_freem(m0);
1421 return NULL;
1422 }
1423
1424 mn->m_next = n;
1425 mn = n;
1426 }
1427 } while (sz > 0);
1428
1429 m_freem(mold);
1430
1431 return m0;
1432 }
1433
1434 int
1435 m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags,
1436 int how)
1437 {
1438 int mlen;
1439 struct mbuf *m, *n;
1440 struct mbuf **mp;
1441 int totlen = 0;
1442 const char *cp = vp;
1443
1444 KASSERT(mp0 != NULL);
1445 KASSERT(*mp0 != NULL);
1446 KASSERT((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL);
1447 KASSERT((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL);
1448
1449 if (len == M_COPYALL)
1450 len = m_length(*mp0) - off;
1451
1452 /*
1453 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
1454 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
1455 */
1456
1457 KASSERT((~flags & (M_COPYBACK0_EXTEND|M_COPYBACK0_COW)) != 0);
1458
1459 mp = mp0;
1460 m = *mp;
1461 while (off > (mlen = m->m_len)) {
1462 off -= mlen;
1463 totlen += mlen;
1464 if (m->m_next == NULL) {
1465 int tspace;
1466 extend:
1467 if ((flags & M_COPYBACK0_EXTEND) == 0)
1468 goto out;
1469
1470 /*
1471 * try to make some space at the end of "m".
1472 */
1473
1474 mlen = m->m_len;
1475 if (off + len >= MINCLSIZE &&
1476 (m->m_flags & M_EXT) == 0 && m->m_len == 0) {
1477 MCLGET(m, how);
1478 }
1479 tspace = M_TRAILINGSPACE(m);
1480 if (tspace > 0) {
1481 tspace = min(tspace, off + len);
1482 KASSERT(tspace > 0);
1483 memset(mtod(m, char *) + m->m_len, 0,
1484 min(off, tspace));
1485 m->m_len += tspace;
1486 off += mlen;
1487 totlen -= mlen;
1488 continue;
1489 }
1490
1491 /*
1492 * need to allocate an mbuf.
1493 */
1494
1495 if (off + len >= MINCLSIZE) {
1496 n = m_getcl(how, m->m_type, 0);
1497 } else {
1498 n = m_get(how, m->m_type);
1499 }
1500 if (n == NULL) {
1501 goto out;
1502 }
1503 n->m_len = min(M_TRAILINGSPACE(n), off + len);
1504 memset(mtod(n, char *), 0, min(n->m_len, off));
1505 m->m_next = n;
1506 }
1507 mp = &m->m_next;
1508 m = m->m_next;
1509 }
1510 while (len > 0) {
1511 mlen = m->m_len - off;
1512 if (mlen != 0 && M_READONLY(m)) {
1513 char *datap;
1514 int eatlen;
1515
1516 /*
1517 * this mbuf is read-only.
1518 * allocate a new writable mbuf and try again.
1519 */
1520
1521 #if defined(DIAGNOSTIC)
1522 if ((flags & M_COPYBACK0_COW) == 0)
1523 panic("m_copyback0: read-only");
1524 #endif /* defined(DIAGNOSTIC) */
1525
1526 /*
1527 * if we're going to write into the middle of
1528 * a mbuf, split it first.
1529 */
1530 if (off > 0) {
1531 n = m_split0(m, off, how, 0);
1532 if (n == NULL)
1533 goto enobufs;
1534 m->m_next = n;
1535 mp = &m->m_next;
1536 m = n;
1537 off = 0;
1538 continue;
1539 }
1540
1541 /*
1542 * XXX TODO coalesce into the trailingspace of
1543 * the previous mbuf when possible.
1544 */
1545
1546 /*
1547 * allocate a new mbuf. copy packet header if needed.
1548 */
1549 n = m_get(how, m->m_type);
1550 if (n == NULL)
1551 goto enobufs;
1552 MCLAIM(n, m->m_owner);
1553 if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
1554 M_MOVE_PKTHDR(n, m);
1555 n->m_len = MHLEN;
1556 } else {
1557 if (len >= MINCLSIZE)
1558 MCLGET(n, M_DONTWAIT);
1559 n->m_len =
1560 (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
1561 }
1562 if (n->m_len > len)
1563 n->m_len = len;
1564
1565 /*
1566 * free the region which has been overwritten.
1567 * copying data from old mbufs if requested.
1568 */
1569 if (flags & M_COPYBACK0_PRESERVE)
1570 datap = mtod(n, char *);
1571 else
1572 datap = NULL;
1573 eatlen = n->m_len;
1574 while (m != NULL && M_READONLY(m) &&
1575 n->m_type == m->m_type && eatlen > 0) {
1576 mlen = min(eatlen, m->m_len);
1577 if (datap) {
1578 m_copydata(m, 0, mlen, datap);
1579 datap += mlen;
1580 }
1581 m->m_data += mlen;
1582 m->m_len -= mlen;
1583 eatlen -= mlen;
1584 if (m->m_len == 0)
1585 *mp = m = m_free(m);
1586 }
1587 if (eatlen > 0)
1588 n->m_len -= eatlen;
1589 n->m_next = m;
1590 *mp = m = n;
1591 continue;
1592 }
1593 mlen = min(mlen, len);
1594 if (flags & M_COPYBACK0_COPYBACK) {
1595 memcpy(mtod(m, char *) + off, cp, (unsigned)mlen);
1596 cp += mlen;
1597 }
1598 len -= mlen;
1599 mlen += off;
1600 off = 0;
1601 totlen += mlen;
1602 if (len == 0)
1603 break;
1604 if (m->m_next == NULL) {
1605 goto extend;
1606 }
1607 mp = &m->m_next;
1608 m = m->m_next;
1609 }
1610 out: if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) {
1611 KASSERT((flags & M_COPYBACK0_EXTEND) != 0);
1612 m->m_pkthdr.len = totlen;
1613 }
1614
1615 return 0;
1616
1617 enobufs:
1618 return ENOBUFS;
1619 }
1620
1621 void
1622 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
1623 {
1624
1625 KASSERT((to->m_flags & M_EXT) == 0);
1626 KASSERT((to->m_flags & M_PKTHDR) == 0 || m_tag_first(to) == NULL);
1627 KASSERT((from->m_flags & M_PKTHDR) != 0);
1628
1629 to->m_pkthdr = from->m_pkthdr;
1630 to->m_flags = from->m_flags & M_COPYFLAGS;
1631 to->m_data = to->m_pktdat;
1632
1633 from->m_flags &= ~M_PKTHDR;
1634 }
1635
1636 /*
1637 * Apply function f to the data in an mbuf chain starting "off" bytes from the
1638 * beginning, continuing for "len" bytes.
1639 */
1640 int
1641 m_apply(struct mbuf *m, int off, int len,
1642 int (*f)(void *, void *, unsigned int), void *arg)
1643 {
1644 unsigned int count;
1645 int rval;
1646
1647 KASSERT(len != M_COPYALL);
1648 KASSERT(len >= 0);
1649 KASSERT(off >= 0);
1650
1651 while (off > 0) {
1652 KASSERT(m != NULL);
1653 if (off < m->m_len)
1654 break;
1655 off -= m->m_len;
1656 m = m->m_next;
1657 }
1658 while (len > 0) {
1659 KASSERT(m != NULL);
1660 count = min(m->m_len - off, len);
1661
1662 rval = (*f)(arg, mtod(m, char *) + off, count);
1663 if (rval)
1664 return (rval);
1665
1666 len -= count;
1667 off = 0;
1668 m = m->m_next;
1669 }
1670
1671 return (0);
1672 }
1673
1674 /*
1675 * Return a pointer to mbuf/offset of location in mbuf chain.
1676 */
1677 struct mbuf *
1678 m_getptr(struct mbuf *m, int loc, int *off)
1679 {
1680
1681 while (loc >= 0) {
1682 /* Normal end of search */
1683 if (m->m_len > loc) {
1684 *off = loc;
1685 return (m);
1686 } else {
1687 loc -= m->m_len;
1688
1689 if (m->m_next == NULL) {
1690 if (loc == 0) {
1691 /* Point at the end of valid data */
1692 *off = m->m_len;
1693 return (m);
1694 } else
1695 return (NULL);
1696 } else
1697 m = m->m_next;
1698 }
1699 }
1700
1701 return (NULL);
1702 }
1703
1704 /*
1705 * m_ext_free: release a reference to the mbuf external storage.
1706 *
1707 * => free the mbuf m itsself as well.
1708 */
1709
1710 void
1711 m_ext_free(struct mbuf *m)
1712 {
1713 bool embedded = MEXT_ISEMBEDDED(m);
1714 bool dofree = true;
1715 u_int refcnt;
1716
1717 KASSERT((m->m_flags & M_EXT) != 0);
1718 KASSERT(MEXT_ISEMBEDDED(m->m_ext_ref));
1719 KASSERT((m->m_ext_ref->m_flags & M_EXT) != 0);
1720 KASSERT((m->m_flags & M_EXT_CLUSTER) ==
1721 (m->m_ext_ref->m_flags & M_EXT_CLUSTER));
1722
1723 if (__predict_true(m->m_ext.ext_refcnt == 1)) {
1724 refcnt = m->m_ext.ext_refcnt = 0;
1725 } else {
1726 refcnt = atomic_dec_uint_nv(&m->m_ext.ext_refcnt);
1727 }
1728 if (refcnt > 0) {
1729 if (embedded) {
1730 /*
1731 * other mbuf's m_ext_ref still points to us.
1732 */
1733 dofree = false;
1734 } else {
1735 m->m_ext_ref = m;
1736 }
1737 } else {
1738 /*
1739 * dropping the last reference
1740 */
1741 if (!embedded) {
1742 m->m_ext.ext_refcnt++; /* XXX */
1743 m_ext_free(m->m_ext_ref);
1744 m->m_ext_ref = m;
1745 } else if ((m->m_flags & M_EXT_CLUSTER) != 0) {
1746 pool_cache_put_paddr((struct pool_cache *)
1747 m->m_ext.ext_arg,
1748 m->m_ext.ext_buf, m->m_ext.ext_paddr);
1749 } else if (m->m_ext.ext_free) {
1750 (*m->m_ext.ext_free)(m,
1751 m->m_ext.ext_buf, m->m_ext.ext_size,
1752 m->m_ext.ext_arg);
1753 /*
1754 * 'm' is already freed by the ext_free callback.
1755 */
1756 dofree = false;
1757 } else {
1758 free(m->m_ext.ext_buf, m->m_ext.ext_type);
1759 }
1760 }
1761 if (dofree) {
1762 m->m_type = MT_FREE;
1763 pool_cache_put(mb_cache, m);
1764 }
1765 }
1766
1767 #if defined(DDB)
1768 void
1769 m_print(const struct mbuf *m, const char *modif, void (*pr)(const char *, ...))
1770 {
1771 char ch;
1772 bool opt_c = false;
1773 char buf[512];
1774
1775 while ((ch = *(modif++)) != '\0') {
1776 switch (ch) {
1777 case 'c':
1778 opt_c = true;
1779 break;
1780 }
1781 }
1782
1783 nextchain:
1784 (*pr)("MBUF %p\n", m);
1785 snprintb(buf, sizeof(buf), M_FLAGS_BITS, (u_int)m->m_flags);
1786 (*pr)(" data=%p, len=%d, type=%d, flags=%s\n",
1787 m->m_data, m->m_len, m->m_type, buf);
1788 (*pr)(" owner=%p, next=%p, nextpkt=%p\n", m->m_owner, m->m_next,
1789 m->m_nextpkt);
1790 (*pr)(" leadingspace=%u, trailingspace=%u, readonly=%u\n",
1791 (int)M_LEADINGSPACE(m), (int)M_TRAILINGSPACE(m),
1792 (int)M_READONLY(m));
1793 if ((m->m_flags & M_PKTHDR) != 0) {
1794 snprintb(buf, sizeof(buf), M_CSUM_BITS, m->m_pkthdr.csum_flags);
1795 (*pr)(" pktlen=%d, rcvif=%p, csum_flags=0x%s, csum_data=0x%"
1796 PRIx32 ", segsz=%u\n",
1797 m->m_pkthdr.len, m->m_pkthdr.rcvif,
1798 buf, m->m_pkthdr.csum_data, m->m_pkthdr.segsz);
1799 }
1800 if ((m->m_flags & M_EXT)) {
1801 (*pr)(" ext_refcnt=%u, ext_buf=%p, ext_size=%zd, "
1802 "ext_free=%p, ext_arg=%p\n",
1803 m->m_ext.ext_refcnt,
1804 m->m_ext.ext_buf, m->m_ext.ext_size,
1805 m->m_ext.ext_free, m->m_ext.ext_arg);
1806 }
1807 if ((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0) {
1808 vaddr_t sva = (vaddr_t)m->m_ext.ext_buf;
1809 vaddr_t eva = sva + m->m_ext.ext_size;
1810 int n = (round_page(eva) - trunc_page(sva)) >> PAGE_SHIFT;
1811 int i;
1812
1813 (*pr)(" pages:");
1814 for (i = 0; i < n; i ++) {
1815 (*pr)(" %p", m->m_ext.ext_pgs[i]);
1816 }
1817 (*pr)("\n");
1818 }
1819
1820 if (opt_c) {
1821 m = m->m_next;
1822 if (m != NULL) {
1823 goto nextchain;
1824 }
1825 }
1826 }
1827 #endif /* defined(DDB) */
1828
1829 void
1830 mbstat_type_add(int type, int diff)
1831 {
1832 struct mbstat_cpu *mb;
1833 int s;
1834
1835 s = splvm();
1836 mb = percpu_getref(mbstat_percpu);
1837 mb->m_mtypes[type] += diff;
1838 percpu_putref(mbstat_percpu);
1839 splx(s);
1840 }
1841
1842 #if defined(MBUFTRACE)
1843 void
1844 mowner_attach(struct mowner *mo)
1845 {
1846
1847 KASSERT(mo->mo_counters == NULL);
1848 mo->mo_counters = percpu_alloc(sizeof(struct mowner_counter));
1849
1850 /* XXX lock */
1851 LIST_INSERT_HEAD(&mowners, mo, mo_link);
1852 }
1853
1854 void
1855 mowner_detach(struct mowner *mo)
1856 {
1857
1858 KASSERT(mo->mo_counters != NULL);
1859
1860 /* XXX lock */
1861 LIST_REMOVE(mo, mo_link);
1862
1863 percpu_free(mo->mo_counters, sizeof(struct mowner_counter));
1864 mo->mo_counters = NULL;
1865 }
1866
1867 void
1868 mowner_init(struct mbuf *m, int type)
1869 {
1870 struct mowner_counter *mc;
1871 struct mowner *mo;
1872 int s;
1873
1874 m->m_owner = mo = &unknown_mowners[type];
1875 s = splvm();
1876 mc = percpu_getref(mo->mo_counters);
1877 mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
1878 percpu_putref(mo->mo_counters);
1879 splx(s);
1880 }
1881
1882 void
1883 mowner_ref(struct mbuf *m, int flags)
1884 {
1885 struct mowner *mo = m->m_owner;
1886 struct mowner_counter *mc;
1887 int s;
1888
1889 s = splvm();
1890 mc = percpu_getref(mo->mo_counters);
1891 if ((flags & M_EXT) != 0)
1892 mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
1893 if ((flags & M_CLUSTER) != 0)
1894 mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
1895 percpu_putref(mo->mo_counters);
1896 splx(s);
1897 }
1898
1899 void
1900 mowner_revoke(struct mbuf *m, bool all, int flags)
1901 {
1902 struct mowner *mo = m->m_owner;
1903 struct mowner_counter *mc;
1904 int s;
1905
1906 s = splvm();
1907 mc = percpu_getref(mo->mo_counters);
1908 if ((flags & M_EXT) != 0)
1909 mc->mc_counter[MOWNER_COUNTER_EXT_RELEASES]++;
1910 if ((flags & M_CLUSTER) != 0)
1911 mc->mc_counter[MOWNER_COUNTER_CLUSTER_RELEASES]++;
1912 if (all)
1913 mc->mc_counter[MOWNER_COUNTER_RELEASES]++;
1914 percpu_putref(mo->mo_counters);
1915 splx(s);
1916 if (all)
1917 m->m_owner = &revoked_mowner;
1918 }
1919
1920 static void
1921 mowner_claim(struct mbuf *m, struct mowner *mo)
1922 {
1923 struct mowner_counter *mc;
1924 int flags = m->m_flags;
1925 int s;
1926
1927 s = splvm();
1928 mc = percpu_getref(mo->mo_counters);
1929 mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
1930 if ((flags & M_EXT) != 0)
1931 mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
1932 if ((flags & M_CLUSTER) != 0)
1933 mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
1934 percpu_putref(mo->mo_counters);
1935 splx(s);
1936 m->m_owner = mo;
1937 }
1938
1939 void
1940 m_claim(struct mbuf *m, struct mowner *mo)
1941 {
1942
1943 if (m->m_owner == mo || mo == NULL)
1944 return;
1945
1946 mowner_revoke(m, true, m->m_flags);
1947 mowner_claim(m, mo);
1948 }
1949 #endif /* defined(MBUFTRACE) */
1950