2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004, 2005,
5 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_param.h"
34 #include "opt_kern_tls.h"
36 #include <sys/param.h>
38 #include <sys/domainset.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
42 #include <sys/domain.h>
43 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
46 #include <sys/limits.h>
48 #include <sys/mutex.h>
49 #include <sys/protosw.h>
50 #include <sys/refcount.h>
51 #include <sys/sf_buf.h>
53 #include <sys/socket.h>
54 #include <sys/sysctl.h>
57 #include <net/if_var.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_pageout.h>
64 #include <vm/vm_map.h>
66 #include <vm/uma_dbg.h>
69 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
72 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
73 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
74 * administrator so desires.
76 * Mbufs are allocated from a UMA Primary Zone called the Mbuf
79 * Additionally, FreeBSD provides a Packet Zone, which it
80 * configures as a Secondary Zone to the Mbuf Primary Zone,
81 * thus sharing backend Slab kegs with the Mbuf Primary Zone.
83 * Thus common-case allocations and locking are simplified:
87 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
89 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
90 * [ Cluster Zone ] [ Zone ] [ Mbuf Primary Zone ]
96 * \____________(VM)_________________/
99 * Whenever an object is allocated with uma_zalloc() out of
100 * one of the Zones its _ctor_ function is executed. The same
101 * for any deallocation through uma_zfree() the _dtor_ function
104 * Caches are per-CPU and are filled from the Primary Zone.
106 * Whenever an object is allocated from the underlying global
107 * memory pool it gets pre-initialized with the _zinit_ functions.
108 * When the Keg's are overfull objects get decommissioned with
109 * _zfini_ functions and free'd back to the global memory pool.
113 int nmbufs; /* limits number of mbufs */
114 int nmbclusters; /* limits number of mbuf clusters */
115 int nmbjumbop; /* limits number of page size jumbo clusters */
116 int nmbjumbo9; /* limits number of 9k jumbo clusters */
117 int nmbjumbo16; /* limits number of 16k jumbo clusters */
119 bool mb_use_ext_pgs = false; /* use M_EXTPG mbufs for sendfile & TLS */
122 sysctl_mb_use_ext_pgs(SYSCTL_HANDLER_ARGS)
126 extpg = mb_use_ext_pgs;
127 error = sysctl_handle_int(oidp, &extpg, 0, req);
128 if (error == 0 && req->newptr != NULL) {
129 if (extpg != 0 && !PMAP_HAS_DMAP)
132 mb_use_ext_pgs = extpg != 0;
136 SYSCTL_PROC(_kern_ipc, OID_AUTO, mb_use_ext_pgs, CTLTYPE_INT | CTLFLAG_RW,
138 sysctl_mb_use_ext_pgs, "IU",
139 "Use unmapped mbufs for sendfile(2) and TLS offload");
141 static quad_t maxmbufmem; /* overall real memory limit for all mbufs */
143 SYSCTL_QUAD(_kern_ipc, OID_AUTO, maxmbufmem, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &maxmbufmem, 0,
144 "Maximum real memory allocatable to various mbuf types");
146 static counter_u64_t snd_tag_count;
147 SYSCTL_COUNTER_U64(_kern_ipc, OID_AUTO, num_snd_tags, CTLFLAG_RW,
148 &snd_tag_count, "# of active mbuf send tags");
151 * tunable_mbinit() has to be run before any mbuf allocations are done.
154 tunable_mbinit(void *dummy)
160 * The default limit for all mbuf related memory is 1/2 of all
161 * available kernel memory (physical or kmem).
162 * At most it can be 3/4 of available kernel memory.
164 realmem = qmin((quad_t)physmem * PAGE_SIZE, vm_kmem_size);
165 maxmbufmem = realmem / 2;
166 TUNABLE_QUAD_FETCH("kern.ipc.maxmbufmem", &maxmbufmem);
167 if (maxmbufmem > realmem / 4 * 3)
168 maxmbufmem = realmem / 4 * 3;
170 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
171 if (nmbclusters == 0)
172 nmbclusters = maxmbufmem / MCLBYTES / 4;
174 TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop);
176 nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4;
178 TUNABLE_INT_FETCH("kern.ipc.nmbjumbo9", &nmbjumbo9);
180 nmbjumbo9 = maxmbufmem / MJUM9BYTES / 6;
182 TUNABLE_INT_FETCH("kern.ipc.nmbjumbo16", &nmbjumbo16);
184 nmbjumbo16 = maxmbufmem / MJUM16BYTES / 6;
187 * We need at least as many mbufs as we have clusters of
188 * the various types added together.
190 TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
191 if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16)
192 nmbufs = lmax(maxmbufmem / MSIZE / 5,
193 nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16);
196 * Unmapped mbufs can only safely be used on platforms with a direct
201 TUNABLE_INT_FETCH("kern.ipc.mb_use_ext_pgs", &extpg);
202 mb_use_ext_pgs = extpg != 0;
205 SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
208 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
210 int error, newnmbclusters;
212 newnmbclusters = nmbclusters;
213 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
214 if (error == 0 && req->newptr && newnmbclusters != nmbclusters) {
215 if (newnmbclusters > nmbclusters &&
216 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
217 nmbclusters = newnmbclusters;
218 nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
219 EVENTHANDLER_INVOKE(nmbclusters_change);
225 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters,
226 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &nmbclusters, 0,
227 sysctl_nmbclusters, "IU",
228 "Maximum number of mbuf clusters allowed");
231 sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS)
233 int error, newnmbjumbop;
235 newnmbjumbop = nmbjumbop;
236 error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req);
237 if (error == 0 && req->newptr && newnmbjumbop != nmbjumbop) {
238 if (newnmbjumbop > nmbjumbop &&
239 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
240 nmbjumbop = newnmbjumbop;
241 nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
247 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop,
248 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &nmbjumbop, 0,
249 sysctl_nmbjumbop, "IU",
250 "Maximum number of mbuf page size jumbo clusters allowed");
253 sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS)
255 int error, newnmbjumbo9;
257 newnmbjumbo9 = nmbjumbo9;
258 error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req);
259 if (error == 0 && req->newptr && newnmbjumbo9 != nmbjumbo9) {
260 if (newnmbjumbo9 > nmbjumbo9 &&
261 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
262 nmbjumbo9 = newnmbjumbo9;
263 nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
269 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9,
270 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &nmbjumbo9, 0,
271 sysctl_nmbjumbo9, "IU",
272 "Maximum number of mbuf 9k jumbo clusters allowed");
275 sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS)
277 int error, newnmbjumbo16;
279 newnmbjumbo16 = nmbjumbo16;
280 error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req);
281 if (error == 0 && req->newptr && newnmbjumbo16 != nmbjumbo16) {
282 if (newnmbjumbo16 > nmbjumbo16 &&
283 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
284 nmbjumbo16 = newnmbjumbo16;
285 nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
291 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16,
292 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &nmbjumbo16, 0,
293 sysctl_nmbjumbo16, "IU",
294 "Maximum number of mbuf 16k jumbo clusters allowed");
297 sysctl_nmbufs(SYSCTL_HANDLER_ARGS)
299 int error, newnmbufs;
302 error = sysctl_handle_int(oidp, &newnmbufs, 0, req);
303 if (error == 0 && req->newptr && newnmbufs != nmbufs) {
304 if (newnmbufs > nmbufs) {
306 nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
307 EVENTHANDLER_INVOKE(nmbufs_change);
313 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbufs,
314 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
315 &nmbufs, 0, sysctl_nmbufs, "IU",
316 "Maximum number of mbufs allowed");
319 * Zones from which we allocate.
321 uma_zone_t zone_mbuf;
322 uma_zone_t zone_clust;
323 uma_zone_t zone_pack;
324 uma_zone_t zone_jumbop;
325 uma_zone_t zone_jumbo9;
326 uma_zone_t zone_jumbo16;
331 static int mb_ctor_mbuf(void *, int, void *, int);
332 static int mb_ctor_clust(void *, int, void *, int);
333 static int mb_ctor_pack(void *, int, void *, int);
334 static void mb_dtor_mbuf(void *, int, void *);
335 static void mb_dtor_pack(void *, int, void *);
336 static int mb_zinit_pack(void *, int, int);
337 static void mb_zfini_pack(void *, int);
338 static void mb_reclaim(uma_zone_t, int);
340 /* Ensure that MSIZE is a power of 2. */
341 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
343 _Static_assert(sizeof(struct mbuf) <= MSIZE,
344 "size of mbuf exceeds MSIZE");
346 * Initialize FreeBSD Network buffer allocation.
349 mbuf_init(void *dummy)
353 * Configure UMA zones for Mbufs, Clusters, and Packets.
355 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
356 mb_ctor_mbuf, mb_dtor_mbuf, NULL, NULL,
357 MSIZE - 1, UMA_ZONE_CONTIG | UMA_ZONE_MAXBUCKET);
359 nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
360 uma_zone_set_warning(zone_mbuf, "kern.ipc.nmbufs limit reached");
361 uma_zone_set_maxaction(zone_mbuf, mb_reclaim);
363 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
364 mb_ctor_clust, NULL, NULL, NULL,
365 UMA_ALIGN_PTR, UMA_ZONE_CONTIG);
367 nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
368 uma_zone_set_warning(zone_clust, "kern.ipc.nmbclusters limit reached");
369 uma_zone_set_maxaction(zone_clust, mb_reclaim);
371 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
372 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
374 /* Make jumbo frame zone too. Page size, 9k and 16k. */
375 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
376 mb_ctor_clust, NULL, NULL, NULL,
377 UMA_ALIGN_PTR, UMA_ZONE_CONTIG);
379 nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
380 uma_zone_set_warning(zone_jumbop, "kern.ipc.nmbjumbop limit reached");
381 uma_zone_set_maxaction(zone_jumbop, mb_reclaim);
383 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
384 mb_ctor_clust, NULL, NULL, NULL,
385 UMA_ALIGN_PTR, UMA_ZONE_CONTIG);
387 nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
388 uma_zone_set_warning(zone_jumbo9, "kern.ipc.nmbjumbo9 limit reached");
389 uma_zone_set_maxaction(zone_jumbo9, mb_reclaim);
391 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
392 mb_ctor_clust, NULL, NULL, NULL,
393 UMA_ALIGN_PTR, UMA_ZONE_CONTIG);
395 nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
396 uma_zone_set_warning(zone_jumbo16, "kern.ipc.nmbjumbo16 limit reached");
397 uma_zone_set_maxaction(zone_jumbo16, mb_reclaim);
400 * Hook event handler for low-memory situation, used to
401 * drain protocols and push data back to the caches (UMA
402 * later pushes it back to VM).
404 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
405 EVENTHANDLER_PRI_FIRST);
407 snd_tag_count = counter_u64_alloc(M_WAITOK);
409 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
413 * debugnet makes use of a pre-allocated pool of mbufs and clusters. When
414 * debugnet is configured, we initialize a set of UMA cache zones which return
415 * items from this pool. At panic-time, the regular UMA zone pointers are
416 * overwritten with those of the cache zones so that drivers may allocate and
417 * free mbufs and clusters without attempting to allocate physical memory.
419 * We keep mbufs and clusters in a pair of mbuf queues. In particular, for
420 * the purpose of caching clusters, we treat them as mbufs.
422 static struct mbufq dn_mbufq =
423 { STAILQ_HEAD_INITIALIZER(dn_mbufq.mq_head), 0, INT_MAX };
424 static struct mbufq dn_clustq =
425 { STAILQ_HEAD_INITIALIZER(dn_clustq.mq_head), 0, INT_MAX };
427 static int dn_clsize;
428 static uma_zone_t dn_zone_mbuf;
429 static uma_zone_t dn_zone_clust;
430 static uma_zone_t dn_zone_pack;
432 static struct debugnet_saved_zones {
434 uma_zone_t dsz_clust;
436 uma_zone_t dsz_jumbop;
437 uma_zone_t dsz_jumbo9;
438 uma_zone_t dsz_jumbo16;
439 bool dsz_debugnet_zones_enabled;
443 dn_buf_import(void *arg, void **store, int count, int domain __unused,
452 for (i = 0; i < count; i++) {
453 m = mbufq_dequeue(q);
456 trash_init(m, q == &dn_mbufq ? MSIZE : dn_clsize, flags);
459 KASSERT((flags & M_WAITOK) == 0 || i == count,
460 ("%s: ran out of pre-allocated mbufs", __func__));
465 dn_buf_release(void *arg, void **store, int count)
473 for (i = 0; i < count; i++) {
475 (void)mbufq_enqueue(q, m);
480 dn_pack_import(void *arg __unused, void **store, int count, int domain __unused,
487 for (i = 0; i < count; i++) {
488 m = m_get(MT_DATA, M_NOWAIT);
491 clust = uma_zalloc(dn_zone_clust, M_NOWAIT);
496 mb_ctor_clust(clust, dn_clsize, m, 0);
499 KASSERT((flags & M_WAITOK) == 0 || i == count,
500 ("%s: ran out of pre-allocated mbufs", __func__));
505 dn_pack_release(void *arg __unused, void **store, int count)
511 for (i = 0; i < count; i++) {
513 clust = m->m_ext.ext_buf;
514 uma_zfree(dn_zone_clust, clust);
515 uma_zfree(dn_zone_mbuf, m);
520 * Free the pre-allocated mbufs and clusters reserved for debugnet, and destroy
521 * the corresponding UMA cache zones.
524 debugnet_mbuf_drain(void)
529 if (dn_zone_mbuf != NULL) {
530 uma_zdestroy(dn_zone_mbuf);
533 if (dn_zone_clust != NULL) {
534 uma_zdestroy(dn_zone_clust);
535 dn_zone_clust = NULL;
537 if (dn_zone_pack != NULL) {
538 uma_zdestroy(dn_zone_pack);
542 while ((m = mbufq_dequeue(&dn_mbufq)) != NULL)
544 while ((item = mbufq_dequeue(&dn_clustq)) != NULL)
545 uma_zfree(m_getzone(dn_clsize), item);
549 * Callback invoked immediately prior to starting a debugnet connection.
552 debugnet_mbuf_start(void)
555 MPASS(!dn_saved_zones.dsz_debugnet_zones_enabled);
557 /* Save the old zone pointers to restore when debugnet is closed. */
558 dn_saved_zones = (struct debugnet_saved_zones) {
559 .dsz_debugnet_zones_enabled = true,
560 .dsz_mbuf = zone_mbuf,
561 .dsz_clust = zone_clust,
562 .dsz_pack = zone_pack,
563 .dsz_jumbop = zone_jumbop,
564 .dsz_jumbo9 = zone_jumbo9,
565 .dsz_jumbo16 = zone_jumbo16,
569 * All cluster zones return buffers of the size requested by the
570 * drivers. It's up to the driver to reinitialize the zones if the
571 * MTU of a debugnet-enabled interface changes.
573 printf("debugnet: overwriting mbuf zone pointers\n");
574 zone_mbuf = dn_zone_mbuf;
575 zone_clust = dn_zone_clust;
576 zone_pack = dn_zone_pack;
577 zone_jumbop = dn_zone_clust;
578 zone_jumbo9 = dn_zone_clust;
579 zone_jumbo16 = dn_zone_clust;
583 * Callback invoked when a debugnet connection is closed/finished.
586 debugnet_mbuf_finish(void)
589 MPASS(dn_saved_zones.dsz_debugnet_zones_enabled);
591 printf("debugnet: restoring mbuf zone pointers\n");
592 zone_mbuf = dn_saved_zones.dsz_mbuf;
593 zone_clust = dn_saved_zones.dsz_clust;
594 zone_pack = dn_saved_zones.dsz_pack;
595 zone_jumbop = dn_saved_zones.dsz_jumbop;
596 zone_jumbo9 = dn_saved_zones.dsz_jumbo9;
597 zone_jumbo16 = dn_saved_zones.dsz_jumbo16;
599 memset(&dn_saved_zones, 0, sizeof(dn_saved_zones));
603 * Reinitialize the debugnet mbuf+cluster pool and cache zones.
606 debugnet_mbuf_reinit(int nmbuf, int nclust, int clsize)
611 debugnet_mbuf_drain();
615 dn_zone_mbuf = uma_zcache_create("debugnet_" MBUF_MEM_NAME,
616 MSIZE, mb_ctor_mbuf, mb_dtor_mbuf, NULL, NULL,
617 dn_buf_import, dn_buf_release,
618 &dn_mbufq, UMA_ZONE_NOBUCKET);
620 dn_zone_clust = uma_zcache_create("debugnet_" MBUF_CLUSTER_MEM_NAME,
621 clsize, mb_ctor_clust, NULL, NULL, NULL,
622 dn_buf_import, dn_buf_release,
623 &dn_clustq, UMA_ZONE_NOBUCKET);
625 dn_zone_pack = uma_zcache_create("debugnet_" MBUF_PACKET_MEM_NAME,
626 MCLBYTES, mb_ctor_pack, mb_dtor_pack, NULL, NULL,
627 dn_pack_import, dn_pack_release,
628 NULL, UMA_ZONE_NOBUCKET);
630 while (nmbuf-- > 0) {
631 m = m_get(MT_DATA, M_WAITOK);
632 uma_zfree(dn_zone_mbuf, m);
634 while (nclust-- > 0) {
635 item = uma_zalloc(m_getzone(dn_clsize), M_WAITOK);
636 uma_zfree(dn_zone_clust, item);
639 #endif /* DEBUGNET */
642 * Constructor for Mbuf primary zone.
644 * The 'arg' pointer points to a mb_args structure which
645 * contains call-specific information required to support the
646 * mbuf allocation API. See mbuf.h.
649 mb_ctor_mbuf(void *mem, int size, void *arg, int how)
652 struct mb_args *args;
657 args = (struct mb_args *)arg;
661 * The mbuf is initialized later. The caller has the
662 * responsibility to set up any MAC labels too.
664 if (type == MT_NOINIT)
667 m = (struct mbuf *)mem;
669 MPASS((flags & M_NOFREE) == 0);
671 error = m_init(m, how, type, flags);
677 * The Mbuf primary zone destructor.
680 mb_dtor_mbuf(void *mem, int size, void *arg)
685 m = (struct mbuf *)mem;
686 flags = (unsigned long)arg;
688 KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__));
689 KASSERT((flags & 0x1) == 0, ("%s: obsolete MB_DTOR_SKIP passed", __func__));
690 if ((m->m_flags & M_PKTHDR) && !SLIST_EMPTY(&m->m_pkthdr.tags))
691 m_tag_delete_chain(m, NULL);
695 * The Mbuf Packet zone destructor.
698 mb_dtor_pack(void *mem, int size, void *arg)
702 m = (struct mbuf *)mem;
703 if ((m->m_flags & M_PKTHDR) != 0)
704 m_tag_delete_chain(m, NULL);
706 /* Make sure we've got a clean cluster back. */
707 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
708 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
709 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
710 KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__));
711 KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__));
712 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
713 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
714 #if defined(INVARIANTS) && !defined(KMSAN)
715 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
718 * If there are processes blocked on zone_clust, waiting for pages
719 * to be freed up, cause them to be woken up by draining the
720 * packet zone. We are exposed to a race here (in the check for
721 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
722 * is deliberate. We don't want to acquire the zone lock for every
725 if (uma_zone_exhausted(zone_clust))
726 uma_zone_reclaim(zone_pack, UMA_RECLAIM_DRAIN);
730 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
732 * Here the 'arg' pointer points to the Mbuf which we
733 * are configuring cluster storage for. If 'arg' is
734 * empty we allocate just the cluster without setting
735 * the mbuf to it. See mbuf.h.
738 mb_ctor_clust(void *mem, int size, void *arg, int how)
742 m = (struct mbuf *)arg;
744 m->m_ext.ext_buf = (char *)mem;
745 m->m_data = m->m_ext.ext_buf;
747 m->m_ext.ext_free = NULL;
748 m->m_ext.ext_arg1 = NULL;
749 m->m_ext.ext_arg2 = NULL;
750 m->m_ext.ext_size = size;
751 m->m_ext.ext_type = m_gettype(size);
752 m->m_ext.ext_flags = EXT_FLAG_EMBREF;
753 m->m_ext.ext_count = 1;
760 * The Packet secondary zone's init routine, executed on the
761 * object's transition from mbuf keg slab to zone cache.
764 mb_zinit_pack(void *mem, int size, int how)
768 m = (struct mbuf *)mem; /* m is virgin. */
769 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
770 m->m_ext.ext_buf == NULL)
772 m->m_ext.ext_type = EXT_PACKET; /* Override. */
773 #if defined(INVARIANTS) && !defined(KMSAN)
774 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
780 * The Packet secondary zone's fini routine, executed on the
781 * object's transition from zone cache to keg slab.
784 mb_zfini_pack(void *mem, int size)
788 m = (struct mbuf *)mem;
789 #if defined(INVARIANTS) && !defined(KMSAN)
790 trash_fini(m->m_ext.ext_buf, MCLBYTES);
792 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
793 #if defined(INVARIANTS) && !defined(KMSAN)
794 trash_dtor(mem, size, NULL);
799 * The "packet" keg constructor.
802 mb_ctor_pack(void *mem, int size, void *arg, int how)
805 struct mb_args *args;
809 m = (struct mbuf *)mem;
810 args = (struct mb_args *)arg;
813 MPASS((flags & M_NOFREE) == 0);
815 #if defined(INVARIANTS) && !defined(KMSAN)
816 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
819 error = m_init(m, how, type, flags);
821 /* m_ext is already initialized. */
822 m->m_data = m->m_ext.ext_buf;
823 m->m_flags = (flags | M_EXT);
829 * This is the protocol drain routine. Called by UMA whenever any of the
830 * mbuf zones is closed to its limit.
832 * No locks should be held when this is called. The drain routines have to
833 * presently acquire some locks which raises the possibility of lock order
837 mb_reclaim(uma_zone_t zone __unused, int pending __unused)
839 struct epoch_tracker et;
843 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, __func__);
846 for (dp = domains; dp != NULL; dp = dp->dom_next)
847 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
848 if (pr->pr_drain != NULL)
854 * Free "count" units of I/O from an mbuf chain. They could be held
855 * in M_EXTPG or just as a normal mbuf. This code is intended to be
856 * called in an error path (I/O error, closed connection, etc).
859 mb_free_notready(struct mbuf *m, int count)
863 for (i = 0; i < count && m != NULL; i++) {
864 if ((m->m_flags & M_EXTPG) != 0) {
866 if (m->m_epg_nrdy != 0)
871 KASSERT(i == count, ("Removed only %d items from %p", i, m));
875 * Compress an unmapped mbuf into a simple mbuf when it holds a small
876 * amount of data. This is used as a DOS defense to avoid having
877 * small packets tie up wired pages, an ext_pgs structure, and an
878 * mbuf. Since this converts the existing mbuf in place, it can only
879 * be used if there are no other references to 'm'.
882 mb_unmapped_compress(struct mbuf *m)
884 volatile u_int *refcnt;
888 * Assert that 'm' does not have a packet header. If 'm' had
889 * a packet header, it would only be able to hold MHLEN bytes
890 * and m_data would have to be initialized differently.
892 KASSERT((m->m_flags & M_PKTHDR) == 0 && (m->m_flags & M_EXTPG),
893 ("%s: m %p !M_EXTPG or M_PKTHDR", __func__, m));
894 KASSERT(m->m_len <= MLEN, ("m_len too large %p", m));
896 if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
897 refcnt = &m->m_ext.ext_count;
899 KASSERT(m->m_ext.ext_cnt != NULL,
900 ("%s: no refcounting pointer on %p", __func__, m));
901 refcnt = m->m_ext.ext_cnt;
907 m_copydata(m, 0, m->m_len, buf);
909 /* Free the backing pages. */
910 m->m_ext.ext_free(m);
912 /* Turn 'm' into a "normal" mbuf. */
913 m->m_flags &= ~(M_EXT | M_RDONLY | M_EXTPG);
914 m->m_data = m->m_dat;
916 /* Copy data back into m. */
917 bcopy(buf, mtod(m, char *), m->m_len);
923 * These next few routines are used to permit downgrading an unmapped
924 * mbuf to a chain of mapped mbufs. This is used when an interface
925 * doesn't supported unmapped mbufs or if checksums need to be
926 * computed in software.
928 * Each unmapped mbuf is converted to a chain of mbufs. First, any
929 * TLS header data is stored in a regular mbuf. Second, each page of
930 * unmapped data is stored in an mbuf with an EXT_SFBUF external
931 * cluster. These mbufs use an sf_buf to provide a valid KVA for the
932 * associated physical page. They also hold a reference on the
933 * original M_EXTPG mbuf to ensure the physical page doesn't go away.
934 * Finally, any TLS trailer data is stored in a regular mbuf.
936 * mb_unmapped_free_mext() is the ext_free handler for the EXT_SFBUF
937 * mbufs. It frees the associated sf_buf and releases its reference
938 * on the original M_EXTPG mbuf.
940 * _mb_unmapped_to_ext() is a helper function that converts a single
941 * unmapped mbuf into a chain of mbufs.
943 * mb_unmapped_to_ext() is the public function that walks an mbuf
944 * chain converting any unmapped mbufs to mapped mbufs. It returns
945 * the new chain of unmapped mbufs on success. On failure it frees
946 * the original mbuf chain and returns NULL.
949 mb_unmapped_free_mext(struct mbuf *m)
954 sf = m->m_ext.ext_arg1;
957 /* Drop the reference on the backing M_EXTPG mbuf. */
958 old_m = m->m_ext.ext_arg2;
959 mb_free_extpg(old_m);
963 _mb_unmapped_to_ext(struct mbuf *m)
965 struct mbuf *m_new, *top, *prev, *mref;
968 int i, len, off, pglen, pgoff, seglen, segoff;
969 volatile u_int *refcnt;
974 KASSERT(m->m_epg_tls == NULL, ("%s: can't convert TLS mbuf %p",
977 /* See if this is the mbuf that holds the embedded refcount. */
978 if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
979 refcnt = &m->m_ext.ext_count;
982 KASSERT(m->m_ext.ext_cnt != NULL,
983 ("%s: no refcounting pointer on %p", __func__, m));
984 refcnt = m->m_ext.ext_cnt;
985 mref = __containerof(refcnt, struct mbuf, m_ext.ext_count);
988 /* Skip over any data removed from the front. */
989 off = mtod(m, vm_offset_t);
992 if (m->m_epg_hdrlen != 0) {
993 if (off >= m->m_epg_hdrlen) {
994 off -= m->m_epg_hdrlen;
996 seglen = m->m_epg_hdrlen - off;
998 seglen = min(seglen, len);
1001 m_new = m_get(M_NOWAIT, MT_DATA);
1004 m_new->m_len = seglen;
1006 memcpy(mtod(m_new, void *), &m->m_epg_hdr[segoff],
1010 pgoff = m->m_epg_1st_off;
1011 for (i = 0; i < m->m_epg_npgs && len > 0; i++) {
1012 pglen = m_epg_pagelen(m, i, pgoff);
1018 seglen = pglen - off;
1019 segoff = pgoff + off;
1021 seglen = min(seglen, len);
1024 pg = PHYS_TO_VM_PAGE(m->m_epg_pa[i]);
1025 m_new = m_get(M_NOWAIT, MT_DATA);
1031 prev->m_next = m_new;
1034 sf = sf_buf_alloc(pg, SFB_NOWAIT);
1039 m_extadd(m_new, (char *)sf_buf_kva(sf), PAGE_SIZE,
1040 mb_unmapped_free_mext, sf, mref, M_RDONLY, EXT_SFBUF);
1041 m_new->m_data += segoff;
1042 m_new->m_len = seglen;
1047 KASSERT((off + len) <= m->m_epg_trllen,
1048 ("off + len > trail (%d + %d > %d)", off, len,
1050 m_new = m_get(M_NOWAIT, MT_DATA);
1056 prev->m_next = m_new;
1058 memcpy(mtod(m_new, void *), &m->m_epg_trail[off], len);
1063 * Obtain an additional reference on the old mbuf for
1064 * each created EXT_SFBUF mbuf. They will be dropped
1065 * in mb_unmapped_free_mext().
1070 atomic_add_int(refcnt, ref_inc);
1078 * Obtain an additional reference on the old mbuf for
1079 * each created EXT_SFBUF mbuf. They will be
1080 * immediately dropped when these mbufs are freed
1086 atomic_add_int(refcnt, ref_inc);
1094 mb_unmapped_to_ext(struct mbuf *top)
1096 struct mbuf *m, *next, *prev = NULL;
1099 for (m = top; m != NULL; m = next) {
1100 /* m might be freed, so cache the next pointer. */
1102 if (m->m_flags & M_EXTPG) {
1105 * Remove 'm' from the new chain so
1106 * that the 'top' chain terminates
1107 * before 'm' in case 'top' is freed
1110 prev->m_next = NULL;
1112 m = _mb_unmapped_to_ext(m);
1125 * Replaced one mbuf with a chain, so we must
1126 * find the end of chain.
1140 * Allocate an empty M_EXTPG mbuf. The ext_free routine is
1141 * responsible for freeing any pages backing this mbuf when it is
1145 mb_alloc_ext_pgs(int how, m_ext_free_t ext_free)
1149 m = m_get(how, MT_DATA);
1155 m->m_epg_1st_off = 0;
1156 m->m_epg_last_len = 0;
1158 m->m_epg_hdrlen = 0;
1159 m->m_epg_trllen = 0;
1160 m->m_epg_tls = NULL;
1163 m->m_flags |= (M_EXT | M_RDONLY | M_EXTPG);
1164 m->m_ext.ext_flags = EXT_FLAG_EMBREF;
1165 m->m_ext.ext_count = 1;
1166 m->m_ext.ext_size = 0;
1167 m->m_ext.ext_free = ext_free;
1172 * Clean up after mbufs with M_EXT storage attached to them if the
1173 * reference count hits 1.
1176 mb_free_ext(struct mbuf *m)
1178 volatile u_int *refcnt;
1182 KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m));
1184 /* See if this is the mbuf that holds the embedded refcount. */
1185 if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
1186 refcnt = &m->m_ext.ext_count;
1189 KASSERT(m->m_ext.ext_cnt != NULL,
1190 ("%s: no refcounting pointer on %p", __func__, m));
1191 refcnt = m->m_ext.ext_cnt;
1192 mref = __containerof(refcnt, struct mbuf, m_ext.ext_count);
1196 * Check if the header is embedded in the cluster. It is
1197 * important that we can't touch any of the mbuf fields
1198 * after we have freed the external storage, since mbuf
1199 * could have been embedded in it. For now, the mbufs
1200 * embedded into the cluster are always of type EXT_EXTREF,
1201 * and for this type we won't free the mref.
1203 if (m->m_flags & M_NOFREE) {
1205 KASSERT(m->m_ext.ext_type == EXT_EXTREF ||
1206 m->m_ext.ext_type == EXT_RXRING,
1207 ("%s: no-free mbuf %p has wrong type", __func__, m));
1211 /* Free attached storage if this mbuf is the only reference to it. */
1212 if (*refcnt == 1 || atomic_fetchadd_int(refcnt, -1) == 1) {
1213 switch (m->m_ext.ext_type) {
1215 /* The packet zone is special. */
1218 uma_zfree(zone_pack, mref);
1221 uma_zfree(zone_clust, m->m_ext.ext_buf);
1225 uma_zfree(zone_jumbop, m->m_ext.ext_buf);
1229 uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
1233 uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
1239 case EXT_DISPOSABLE:
1240 KASSERT(mref->m_ext.ext_free != NULL,
1241 ("%s: ext_free not set", __func__));
1242 mref->m_ext.ext_free(mref);
1246 KASSERT(m->m_ext.ext_free != NULL,
1247 ("%s: ext_free not set", __func__));
1248 m->m_ext.ext_free(m);
1251 KASSERT(m->m_ext.ext_free == NULL,
1252 ("%s: ext_free is set", __func__));
1255 KASSERT(m->m_ext.ext_type == 0,
1256 ("%s: unknown ext_type", __func__));
1260 if (freembuf && m != mref)
1265 * Clean up after mbufs with M_EXTPG storage attached to them if the
1266 * reference count hits 1.
1269 mb_free_extpg(struct mbuf *m)
1271 volatile u_int *refcnt;
1276 /* See if this is the mbuf that holds the embedded refcount. */
1277 if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
1278 refcnt = &m->m_ext.ext_count;
1281 KASSERT(m->m_ext.ext_cnt != NULL,
1282 ("%s: no refcounting pointer on %p", __func__, m));
1283 refcnt = m->m_ext.ext_cnt;
1284 mref = __containerof(refcnt, struct mbuf, m_ext.ext_count);
1287 /* Free attached storage if this mbuf is the only reference to it. */
1288 if (*refcnt == 1 || atomic_fetchadd_int(refcnt, -1) == 1) {
1289 KASSERT(mref->m_ext.ext_free != NULL,
1290 ("%s: ext_free not set", __func__));
1292 mref->m_ext.ext_free(mref);
1294 if (mref->m_epg_tls != NULL &&
1295 !refcount_release_if_not_last(&mref->m_epg_tls->refcount))
1296 ktls_enqueue_to_free(mref);
1307 * Official mbuf(9) allocation KPI for stack and drivers:
1309 * m_get() - a single mbuf without any attachments, sys/mbuf.h.
1310 * m_gethdr() - a single mbuf initialized as M_PKTHDR, sys/mbuf.h.
1311 * m_getcl() - an mbuf + 2k cluster, sys/mbuf.h.
1312 * m_clget() - attach cluster to already allocated mbuf.
1313 * m_cljget() - attach jumbo cluster to already allocated mbuf.
1314 * m_get2() - allocate minimum mbuf that would fit size argument.
1315 * m_getm2() - allocate a chain of mbufs/clusters.
1316 * m_extadd() - attach external cluster to mbuf.
1318 * m_free() - free single mbuf with its tags and ext, sys/mbuf.h.
1319 * m_freem() - free chain of mbufs.
1323 m_clget(struct mbuf *m, int how)
1326 KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
1328 m->m_ext.ext_buf = (char *)NULL;
1329 uma_zalloc_arg(zone_clust, m, how);
1331 * On a cluster allocation failure, drain the packet zone and retry,
1332 * we might be able to loosen a few clusters up on the drain.
1334 if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) {
1335 uma_zone_reclaim(zone_pack, UMA_RECLAIM_DRAIN);
1336 uma_zalloc_arg(zone_clust, m, how);
1338 MBUF_PROBE2(m__clget, m, how);
1339 return (m->m_flags & M_EXT);
1343 * m_cljget() is different from m_clget() as it can allocate clusters without
1344 * attaching them to an mbuf. In that case the return value is the pointer
1345 * to the cluster of the requested size. If an mbuf was specified, it gets
1346 * the cluster attached to it and the return value can be safely ignored.
1347 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
1350 m_cljget(struct mbuf *m, int how, int size)
1356 KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
1358 m->m_ext.ext_buf = NULL;
1361 zone = m_getzone(size);
1362 retval = uma_zalloc_arg(zone, m, how);
1364 MBUF_PROBE4(m__cljget, m, how, size, retval);
1370 * m_get2() allocates minimum mbuf that would fit "size" argument.
1373 m_get2(int size, int how, short type, int flags)
1375 struct mb_args args;
1381 if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0))
1382 return (uma_zalloc_arg(zone_mbuf, &args, how));
1383 if (size <= MCLBYTES)
1384 return (uma_zalloc_arg(zone_pack, &args, how));
1386 if (size > MJUMPAGESIZE)
1389 m = uma_zalloc_arg(zone_mbuf, &args, how);
1393 n = uma_zalloc_arg(zone_jumbop, m, how);
1403 * m_get3() allocates minimum mbuf that would fit "size" argument.
1404 * Unlike m_get2() it can allocate clusters up to MJUM16BYTES.
1407 m_get3(int size, int how, short type, int flags)
1409 struct mb_args args;
1413 if (size <= MJUMPAGESIZE)
1414 return (m_get2(size, how, type, flags));
1416 if (size > MJUM16BYTES)
1422 m = uma_zalloc_arg(zone_mbuf, &args, how);
1426 if (size <= MJUM9BYTES)
1429 zone = zone_jumbo16;
1431 n = uma_zalloc_arg(zone, m, how);
1441 * m_getjcl() returns an mbuf with a cluster of the specified size attached.
1442 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
1445 m_getjcl(int how, short type, int flags, int size)
1447 struct mb_args args;
1451 if (size == MCLBYTES)
1452 return m_getcl(how, type, flags);
1457 m = uma_zalloc_arg(zone_mbuf, &args, how);
1461 zone = m_getzone(size);
1462 n = uma_zalloc_arg(zone, m, how);
1467 MBUF_PROBE5(m__getjcl, how, type, flags, size, m);
1472 * Allocate a given length worth of mbufs and/or clusters (whatever fits
1473 * best) and return a pointer to the top of the allocated chain. If an
1474 * existing mbuf chain is provided, then we will append the new chain
1475 * to the existing one and return a pointer to the provided mbuf.
1478 m_getm2(struct mbuf *m, int len, int how, short type, int flags)
1480 struct mbuf *mb, *nm = NULL, *mtail = NULL;
1482 KASSERT(len >= 0, ("%s: len is < 0", __func__));
1484 /* Validate flags. */
1485 flags &= (M_PKTHDR | M_EOR);
1487 /* Packet header mbuf must be first in chain. */
1488 if ((flags & M_PKTHDR) && m != NULL)
1491 /* Loop and append maximum sized mbufs to the chain tail. */
1494 if (len > MCLBYTES) {
1495 mb = m_getjcl(M_NOWAIT, type, (flags & M_PKTHDR),
1499 if (len >= MINCLSIZE)
1500 mb = m_getcl(how, type, (flags & M_PKTHDR));
1501 else if (flags & M_PKTHDR)
1502 mb = m_gethdr(how, type);
1504 mb = m_get(how, type);
1507 * Fail the whole operation if one mbuf can't be
1523 flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */
1526 mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */
1528 /* If mbuf was supplied, append new chain to the end of it. */
1530 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
1533 mtail->m_flags &= ~M_EOR;
1541 * Configure a provided mbuf to refer to the provided external storage
1542 * buffer and setup a reference count for said buffer.
1545 * mb The existing mbuf to which to attach the provided buffer.
1546 * buf The address of the provided external storage buffer.
1547 * size The size of the provided buffer.
1548 * freef A pointer to a routine that is responsible for freeing the
1549 * provided external storage buffer.
1550 * args A pointer to an argument structure (of any type) to be passed
1551 * to the provided freef routine (may be NULL).
1552 * flags Any other flags to be passed to the provided mbuf.
1553 * type The type that the external storage buffer should be
1560 m_extadd(struct mbuf *mb, char *buf, u_int size, m_ext_free_t freef,
1561 void *arg1, void *arg2, int flags, int type)
1564 KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
1566 mb->m_flags |= (M_EXT | flags);
1567 mb->m_ext.ext_buf = buf;
1568 mb->m_data = mb->m_ext.ext_buf;
1569 mb->m_ext.ext_size = size;
1570 mb->m_ext.ext_free = freef;
1571 mb->m_ext.ext_arg1 = arg1;
1572 mb->m_ext.ext_arg2 = arg2;
1573 mb->m_ext.ext_type = type;
1575 if (type != EXT_EXTREF) {
1576 mb->m_ext.ext_count = 1;
1577 mb->m_ext.ext_flags = EXT_FLAG_EMBREF;
1579 mb->m_ext.ext_flags = 0;
1583 * Free an entire chain of mbufs and associated external buffers, if
1587 m_freem(struct mbuf *mb)
1590 MBUF_PROBE1(m__freem, mb);
1596 * Temporary primitive to allow freeing without going through m_free.
1599 m_free_raw(struct mbuf *mb)
1602 uma_zfree(zone_mbuf, mb);
1606 m_snd_tag_alloc(struct ifnet *ifp, union if_snd_tag_alloc_params *params,
1607 struct m_snd_tag **mstp)
1610 if (ifp->if_snd_tag_alloc == NULL)
1611 return (EOPNOTSUPP);
1612 return (ifp->if_snd_tag_alloc(ifp, params, mstp));
1616 m_snd_tag_init(struct m_snd_tag *mst, struct ifnet *ifp,
1617 const struct if_snd_tag_sw *sw)
1622 refcount_init(&mst->refcount, 1);
1624 counter_u64_add(snd_tag_count, 1);
1628 m_snd_tag_destroy(struct m_snd_tag *mst)
1633 mst->sw->snd_tag_free(mst);
1635 counter_u64_add(snd_tag_count, -1);
1639 * Allocate an mbuf with anonymous external pages.
1642 mb_alloc_ext_plus_pages(int len, int how)
1648 m = mb_alloc_ext_pgs(how, mb_free_mext_pgs);
1651 m->m_epg_flags |= EPG_FLAG_ANON;
1652 npgs = howmany(len, PAGE_SIZE);
1653 for (i = 0; i < npgs; i++) {
1655 pg = vm_page_alloc_noobj(VM_ALLOC_NODUMP |
1658 if (how == M_NOWAIT) {
1665 } while (pg == NULL);
1666 m->m_epg_pa[i] = VM_PAGE_TO_PHYS(pg);
1668 m->m_epg_npgs = npgs;
1673 * Copy the data in the mbuf chain to a chain of mbufs with anonymous external
1675 * len is the length of data in the input mbuf chain.
1676 * mlen is the maximum number of bytes put into each ext_page mbuf.
1679 mb_mapped_to_unmapped(struct mbuf *mp, int len, int mlen, int how,
1680 struct mbuf **mlast)
1682 struct mbuf *m, *mout;
1683 char *pgpos, *mbpos;
1684 int i, mblen, mbufsiz, pglen, xfer;
1688 mbufsiz = min(mlen, len);
1689 m = mout = mb_alloc_ext_plus_pages(mbufsiz, how);
1692 pgpos = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[0]);
1698 if (++i == m->m_epg_npgs) {
1699 m->m_epg_last_len = PAGE_SIZE;
1700 mbufsiz = min(mlen, len);
1701 m->m_next = mb_alloc_ext_plus_pages(mbufsiz,
1710 pgpos = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[i]);
1713 while (mblen == 0) {
1718 KASSERT((mp->m_flags & M_EXTPG) == 0,
1719 ("mb_copym_ext_pgs: ext_pgs input mbuf"));
1720 mbpos = mtod(mp, char *);
1724 xfer = min(mblen, pglen);
1725 memcpy(pgpos, mbpos, xfer);
1733 m->m_epg_last_len = PAGE_SIZE - pglen;