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; /* use M_EXTPG mbufs for sendfile & TLS */
120 SYSCTL_BOOL(_kern_ipc, OID_AUTO, mb_use_ext_pgs, CTLFLAG_RWTUN,
122 "Use unmapped mbufs for sendfile(2) and TLS offload");
124 static quad_t maxmbufmem; /* overall real memory limit for all mbufs */
126 SYSCTL_QUAD(_kern_ipc, OID_AUTO, maxmbufmem, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &maxmbufmem, 0,
127 "Maximum real memory allocatable to various mbuf types");
129 static counter_u64_t snd_tag_count;
130 SYSCTL_COUNTER_U64(_kern_ipc, OID_AUTO, num_snd_tags, CTLFLAG_RW,
131 &snd_tag_count, "# of active mbuf send tags");
134 * tunable_mbinit() has to be run before any mbuf allocations are done.
137 tunable_mbinit(void *dummy)
142 * The default limit for all mbuf related memory is 1/2 of all
143 * available kernel memory (physical or kmem).
144 * At most it can be 3/4 of available kernel memory.
146 realmem = qmin((quad_t)physmem * PAGE_SIZE, vm_kmem_size);
147 maxmbufmem = realmem / 2;
148 TUNABLE_QUAD_FETCH("kern.ipc.maxmbufmem", &maxmbufmem);
149 if (maxmbufmem > realmem / 4 * 3)
150 maxmbufmem = realmem / 4 * 3;
152 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
153 if (nmbclusters == 0)
154 nmbclusters = maxmbufmem / MCLBYTES / 4;
156 TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop);
158 nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4;
160 TUNABLE_INT_FETCH("kern.ipc.nmbjumbo9", &nmbjumbo9);
162 nmbjumbo9 = maxmbufmem / MJUM9BYTES / 6;
164 TUNABLE_INT_FETCH("kern.ipc.nmbjumbo16", &nmbjumbo16);
166 nmbjumbo16 = maxmbufmem / MJUM16BYTES / 6;
169 * We need at least as many mbufs as we have clusters of
170 * the various types added together.
172 TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
173 if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16)
174 nmbufs = lmax(maxmbufmem / MSIZE / 5,
175 nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16);
177 SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
180 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
182 int error, newnmbclusters;
184 newnmbclusters = nmbclusters;
185 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
186 if (error == 0 && req->newptr && newnmbclusters != nmbclusters) {
187 if (newnmbclusters > nmbclusters &&
188 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
189 nmbclusters = newnmbclusters;
190 nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
191 EVENTHANDLER_INVOKE(nmbclusters_change);
197 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters,
198 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &nmbclusters, 0,
199 sysctl_nmbclusters, "IU",
200 "Maximum number of mbuf clusters allowed");
203 sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS)
205 int error, newnmbjumbop;
207 newnmbjumbop = nmbjumbop;
208 error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req);
209 if (error == 0 && req->newptr && newnmbjumbop != nmbjumbop) {
210 if (newnmbjumbop > nmbjumbop &&
211 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
212 nmbjumbop = newnmbjumbop;
213 nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
219 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop,
220 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &nmbjumbop, 0,
221 sysctl_nmbjumbop, "IU",
222 "Maximum number of mbuf page size jumbo clusters allowed");
225 sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS)
227 int error, newnmbjumbo9;
229 newnmbjumbo9 = nmbjumbo9;
230 error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req);
231 if (error == 0 && req->newptr && newnmbjumbo9 != nmbjumbo9) {
232 if (newnmbjumbo9 > nmbjumbo9 &&
233 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
234 nmbjumbo9 = newnmbjumbo9;
235 nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
241 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9,
242 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &nmbjumbo9, 0,
243 sysctl_nmbjumbo9, "IU",
244 "Maximum number of mbuf 9k jumbo clusters allowed");
247 sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS)
249 int error, newnmbjumbo16;
251 newnmbjumbo16 = nmbjumbo16;
252 error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req);
253 if (error == 0 && req->newptr && newnmbjumbo16 != nmbjumbo16) {
254 if (newnmbjumbo16 > nmbjumbo16 &&
255 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
256 nmbjumbo16 = newnmbjumbo16;
257 nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
263 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16,
264 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &nmbjumbo16, 0,
265 sysctl_nmbjumbo16, "IU",
266 "Maximum number of mbuf 16k jumbo clusters allowed");
269 sysctl_nmbufs(SYSCTL_HANDLER_ARGS)
271 int error, newnmbufs;
274 error = sysctl_handle_int(oidp, &newnmbufs, 0, req);
275 if (error == 0 && req->newptr && newnmbufs != nmbufs) {
276 if (newnmbufs > nmbufs) {
278 nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
279 EVENTHANDLER_INVOKE(nmbufs_change);
285 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbufs,
286 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
287 &nmbufs, 0, sysctl_nmbufs, "IU",
288 "Maximum number of mbufs allowed");
291 * Zones from which we allocate.
293 uma_zone_t zone_mbuf;
294 uma_zone_t zone_clust;
295 uma_zone_t zone_pack;
296 uma_zone_t zone_jumbop;
297 uma_zone_t zone_jumbo9;
298 uma_zone_t zone_jumbo16;
303 static int mb_ctor_mbuf(void *, int, void *, int);
304 static int mb_ctor_clust(void *, int, void *, int);
305 static int mb_ctor_pack(void *, int, void *, int);
306 static void mb_dtor_mbuf(void *, int, void *);
307 static void mb_dtor_pack(void *, int, void *);
308 static int mb_zinit_pack(void *, int, int);
309 static void mb_zfini_pack(void *, int);
310 static void mb_reclaim(uma_zone_t, int);
312 /* Ensure that MSIZE is a power of 2. */
313 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
315 _Static_assert(sizeof(struct mbuf) <= MSIZE,
316 "size of mbuf exceeds MSIZE");
318 * Initialize FreeBSD Network buffer allocation.
321 mbuf_init(void *dummy)
325 * Configure UMA zones for Mbufs, Clusters, and Packets.
327 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
328 mb_ctor_mbuf, mb_dtor_mbuf, NULL, NULL,
329 MSIZE - 1, UMA_ZONE_CONTIG | UMA_ZONE_MAXBUCKET);
331 nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
332 uma_zone_set_warning(zone_mbuf, "kern.ipc.nmbufs limit reached");
333 uma_zone_set_maxaction(zone_mbuf, mb_reclaim);
335 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
336 mb_ctor_clust, NULL, NULL, NULL,
337 UMA_ALIGN_PTR, UMA_ZONE_CONTIG);
339 nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
340 uma_zone_set_warning(zone_clust, "kern.ipc.nmbclusters limit reached");
341 uma_zone_set_maxaction(zone_clust, mb_reclaim);
343 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
344 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
346 /* Make jumbo frame zone too. Page size, 9k and 16k. */
347 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
348 mb_ctor_clust, NULL, NULL, NULL,
349 UMA_ALIGN_PTR, UMA_ZONE_CONTIG);
351 nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
352 uma_zone_set_warning(zone_jumbop, "kern.ipc.nmbjumbop limit reached");
353 uma_zone_set_maxaction(zone_jumbop, mb_reclaim);
355 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
356 mb_ctor_clust, NULL, NULL, NULL,
357 UMA_ALIGN_PTR, UMA_ZONE_CONTIG);
359 nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
360 uma_zone_set_warning(zone_jumbo9, "kern.ipc.nmbjumbo9 limit reached");
361 uma_zone_set_maxaction(zone_jumbo9, mb_reclaim);
363 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
364 mb_ctor_clust, NULL, NULL, NULL,
365 UMA_ALIGN_PTR, UMA_ZONE_CONTIG);
367 nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
368 uma_zone_set_warning(zone_jumbo16, "kern.ipc.nmbjumbo16 limit reached");
369 uma_zone_set_maxaction(zone_jumbo16, mb_reclaim);
372 * Hook event handler for low-memory situation, used to
373 * drain protocols and push data back to the caches (UMA
374 * later pushes it back to VM).
376 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
377 EVENTHANDLER_PRI_FIRST);
379 snd_tag_count = counter_u64_alloc(M_WAITOK);
381 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
385 * debugnet makes use of a pre-allocated pool of mbufs and clusters. When
386 * debugnet is configured, we initialize a set of UMA cache zones which return
387 * items from this pool. At panic-time, the regular UMA zone pointers are
388 * overwritten with those of the cache zones so that drivers may allocate and
389 * free mbufs and clusters without attempting to allocate physical memory.
391 * We keep mbufs and clusters in a pair of mbuf queues. In particular, for
392 * the purpose of caching clusters, we treat them as mbufs.
394 static struct mbufq dn_mbufq =
395 { STAILQ_HEAD_INITIALIZER(dn_mbufq.mq_head), 0, INT_MAX };
396 static struct mbufq dn_clustq =
397 { STAILQ_HEAD_INITIALIZER(dn_clustq.mq_head), 0, INT_MAX };
399 static int dn_clsize;
400 static uma_zone_t dn_zone_mbuf;
401 static uma_zone_t dn_zone_clust;
402 static uma_zone_t dn_zone_pack;
404 static struct debugnet_saved_zones {
406 uma_zone_t dsz_clust;
408 uma_zone_t dsz_jumbop;
409 uma_zone_t dsz_jumbo9;
410 uma_zone_t dsz_jumbo16;
411 bool dsz_debugnet_zones_enabled;
415 dn_buf_import(void *arg, void **store, int count, int domain __unused,
424 for (i = 0; i < count; i++) {
425 m = mbufq_dequeue(q);
428 trash_init(m, q == &dn_mbufq ? MSIZE : dn_clsize, flags);
431 KASSERT((flags & M_WAITOK) == 0 || i == count,
432 ("%s: ran out of pre-allocated mbufs", __func__));
437 dn_buf_release(void *arg, void **store, int count)
445 for (i = 0; i < count; i++) {
447 (void)mbufq_enqueue(q, m);
452 dn_pack_import(void *arg __unused, void **store, int count, int domain __unused,
459 for (i = 0; i < count; i++) {
460 m = m_get(MT_DATA, M_NOWAIT);
463 clust = uma_zalloc(dn_zone_clust, M_NOWAIT);
468 mb_ctor_clust(clust, dn_clsize, m, 0);
471 KASSERT((flags & M_WAITOK) == 0 || i == count,
472 ("%s: ran out of pre-allocated mbufs", __func__));
477 dn_pack_release(void *arg __unused, void **store, int count)
483 for (i = 0; i < count; i++) {
485 clust = m->m_ext.ext_buf;
486 uma_zfree(dn_zone_clust, clust);
487 uma_zfree(dn_zone_mbuf, m);
492 * Free the pre-allocated mbufs and clusters reserved for debugnet, and destroy
493 * the corresponding UMA cache zones.
496 debugnet_mbuf_drain(void)
501 if (dn_zone_mbuf != NULL) {
502 uma_zdestroy(dn_zone_mbuf);
505 if (dn_zone_clust != NULL) {
506 uma_zdestroy(dn_zone_clust);
507 dn_zone_clust = NULL;
509 if (dn_zone_pack != NULL) {
510 uma_zdestroy(dn_zone_pack);
514 while ((m = mbufq_dequeue(&dn_mbufq)) != NULL)
516 while ((item = mbufq_dequeue(&dn_clustq)) != NULL)
517 uma_zfree(m_getzone(dn_clsize), item);
521 * Callback invoked immediately prior to starting a debugnet connection.
524 debugnet_mbuf_start(void)
527 MPASS(!dn_saved_zones.dsz_debugnet_zones_enabled);
529 /* Save the old zone pointers to restore when debugnet is closed. */
530 dn_saved_zones = (struct debugnet_saved_zones) {
531 .dsz_debugnet_zones_enabled = true,
532 .dsz_mbuf = zone_mbuf,
533 .dsz_clust = zone_clust,
534 .dsz_pack = zone_pack,
535 .dsz_jumbop = zone_jumbop,
536 .dsz_jumbo9 = zone_jumbo9,
537 .dsz_jumbo16 = zone_jumbo16,
541 * All cluster zones return buffers of the size requested by the
542 * drivers. It's up to the driver to reinitialize the zones if the
543 * MTU of a debugnet-enabled interface changes.
545 printf("debugnet: overwriting mbuf zone pointers\n");
546 zone_mbuf = dn_zone_mbuf;
547 zone_clust = dn_zone_clust;
548 zone_pack = dn_zone_pack;
549 zone_jumbop = dn_zone_clust;
550 zone_jumbo9 = dn_zone_clust;
551 zone_jumbo16 = dn_zone_clust;
555 * Callback invoked when a debugnet connection is closed/finished.
558 debugnet_mbuf_finish(void)
561 MPASS(dn_saved_zones.dsz_debugnet_zones_enabled);
563 printf("debugnet: restoring mbuf zone pointers\n");
564 zone_mbuf = dn_saved_zones.dsz_mbuf;
565 zone_clust = dn_saved_zones.dsz_clust;
566 zone_pack = dn_saved_zones.dsz_pack;
567 zone_jumbop = dn_saved_zones.dsz_jumbop;
568 zone_jumbo9 = dn_saved_zones.dsz_jumbo9;
569 zone_jumbo16 = dn_saved_zones.dsz_jumbo16;
571 memset(&dn_saved_zones, 0, sizeof(dn_saved_zones));
575 * Reinitialize the debugnet mbuf+cluster pool and cache zones.
578 debugnet_mbuf_reinit(int nmbuf, int nclust, int clsize)
583 debugnet_mbuf_drain();
587 dn_zone_mbuf = uma_zcache_create("debugnet_" MBUF_MEM_NAME,
588 MSIZE, mb_ctor_mbuf, mb_dtor_mbuf, NULL, NULL,
589 dn_buf_import, dn_buf_release,
590 &dn_mbufq, UMA_ZONE_NOBUCKET);
592 dn_zone_clust = uma_zcache_create("debugnet_" MBUF_CLUSTER_MEM_NAME,
593 clsize, mb_ctor_clust, NULL, NULL, NULL,
594 dn_buf_import, dn_buf_release,
595 &dn_clustq, UMA_ZONE_NOBUCKET);
597 dn_zone_pack = uma_zcache_create("debugnet_" MBUF_PACKET_MEM_NAME,
598 MCLBYTES, mb_ctor_pack, mb_dtor_pack, NULL, NULL,
599 dn_pack_import, dn_pack_release,
600 NULL, UMA_ZONE_NOBUCKET);
602 while (nmbuf-- > 0) {
603 m = m_get(MT_DATA, M_WAITOK);
604 uma_zfree(dn_zone_mbuf, m);
606 while (nclust-- > 0) {
607 item = uma_zalloc(m_getzone(dn_clsize), M_WAITOK);
608 uma_zfree(dn_zone_clust, item);
611 #endif /* DEBUGNET */
614 * Constructor for Mbuf primary zone.
616 * The 'arg' pointer points to a mb_args structure which
617 * contains call-specific information required to support the
618 * mbuf allocation API. See mbuf.h.
621 mb_ctor_mbuf(void *mem, int size, void *arg, int how)
624 struct mb_args *args;
629 args = (struct mb_args *)arg;
633 * The mbuf is initialized later. The caller has the
634 * responsibility to set up any MAC labels too.
636 if (type == MT_NOINIT)
639 m = (struct mbuf *)mem;
641 MPASS((flags & M_NOFREE) == 0);
643 error = m_init(m, how, type, flags);
649 * The Mbuf primary zone destructor.
652 mb_dtor_mbuf(void *mem, int size, void *arg)
657 m = (struct mbuf *)mem;
658 flags = (unsigned long)arg;
660 KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__));
661 if (!(flags & MB_DTOR_SKIP) && (m->m_flags & M_PKTHDR) && !SLIST_EMPTY(&m->m_pkthdr.tags))
662 m_tag_delete_chain(m, NULL);
666 * The Mbuf Packet zone destructor.
669 mb_dtor_pack(void *mem, int size, void *arg)
673 m = (struct mbuf *)mem;
674 if ((m->m_flags & M_PKTHDR) != 0)
675 m_tag_delete_chain(m, NULL);
677 /* Make sure we've got a clean cluster back. */
678 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
679 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
680 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
681 KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__));
682 KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__));
683 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
684 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
686 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
689 * If there are processes blocked on zone_clust, waiting for pages
690 * to be freed up, cause them to be woken up by draining the
691 * packet zone. We are exposed to a race here (in the check for
692 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
693 * is deliberate. We don't want to acquire the zone lock for every
696 if (uma_zone_exhausted(zone_clust))
697 uma_zone_reclaim(zone_pack, UMA_RECLAIM_DRAIN);
701 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
703 * Here the 'arg' pointer points to the Mbuf which we
704 * are configuring cluster storage for. If 'arg' is
705 * empty we allocate just the cluster without setting
706 * the mbuf to it. See mbuf.h.
709 mb_ctor_clust(void *mem, int size, void *arg, int how)
713 m = (struct mbuf *)arg;
715 m->m_ext.ext_buf = (char *)mem;
716 m->m_data = m->m_ext.ext_buf;
718 m->m_ext.ext_free = NULL;
719 m->m_ext.ext_arg1 = NULL;
720 m->m_ext.ext_arg2 = NULL;
721 m->m_ext.ext_size = size;
722 m->m_ext.ext_type = m_gettype(size);
723 m->m_ext.ext_flags = EXT_FLAG_EMBREF;
724 m->m_ext.ext_count = 1;
731 * The Packet secondary zone's init routine, executed on the
732 * object's transition from mbuf keg slab to zone cache.
735 mb_zinit_pack(void *mem, int size, int how)
739 m = (struct mbuf *)mem; /* m is virgin. */
740 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
741 m->m_ext.ext_buf == NULL)
743 m->m_ext.ext_type = EXT_PACKET; /* Override. */
745 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
751 * The Packet secondary zone's fini routine, executed on the
752 * object's transition from zone cache to keg slab.
755 mb_zfini_pack(void *mem, int size)
759 m = (struct mbuf *)mem;
761 trash_fini(m->m_ext.ext_buf, MCLBYTES);
763 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
765 trash_dtor(mem, size, NULL);
770 * The "packet" keg constructor.
773 mb_ctor_pack(void *mem, int size, void *arg, int how)
776 struct mb_args *args;
780 m = (struct mbuf *)mem;
781 args = (struct mb_args *)arg;
784 MPASS((flags & M_NOFREE) == 0);
787 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
790 error = m_init(m, how, type, flags);
792 /* m_ext is already initialized. */
793 m->m_data = m->m_ext.ext_buf;
794 m->m_flags = (flags | M_EXT);
800 * This is the protocol drain routine. Called by UMA whenever any of the
801 * mbuf zones is closed to its limit.
803 * No locks should be held when this is called. The drain routines have to
804 * presently acquire some locks which raises the possibility of lock order
808 mb_reclaim(uma_zone_t zone __unused, int pending __unused)
810 struct epoch_tracker et;
814 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, __func__);
817 for (dp = domains; dp != NULL; dp = dp->dom_next)
818 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
819 if (pr->pr_drain != NULL)
825 * Free "count" units of I/O from an mbuf chain. They could be held
826 * in M_EXTPG or just as a normal mbuf. This code is intended to be
827 * called in an error path (I/O error, closed connection, etc).
830 mb_free_notready(struct mbuf *m, int count)
834 for (i = 0; i < count && m != NULL; i++) {
835 if ((m->m_flags & M_EXTPG) != 0) {
837 if (m->m_epg_nrdy != 0)
842 KASSERT(i == count, ("Removed only %d items from %p", i, m));
846 * Compress an unmapped mbuf into a simple mbuf when it holds a small
847 * amount of data. This is used as a DOS defense to avoid having
848 * small packets tie up wired pages, an ext_pgs structure, and an
849 * mbuf. Since this converts the existing mbuf in place, it can only
850 * be used if there are no other references to 'm'.
853 mb_unmapped_compress(struct mbuf *m)
855 volatile u_int *refcnt;
859 * Assert that 'm' does not have a packet header. If 'm' had
860 * a packet header, it would only be able to hold MHLEN bytes
861 * and m_data would have to be initialized differently.
863 KASSERT((m->m_flags & M_PKTHDR) == 0 && (m->m_flags & M_EXTPG),
864 ("%s: m %p !M_EXTPG or M_PKTHDR", __func__, m));
865 KASSERT(m->m_len <= MLEN, ("m_len too large %p", m));
867 if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
868 refcnt = &m->m_ext.ext_count;
870 KASSERT(m->m_ext.ext_cnt != NULL,
871 ("%s: no refcounting pointer on %p", __func__, m));
872 refcnt = m->m_ext.ext_cnt;
878 m_copydata(m, 0, m->m_len, buf);
880 /* Free the backing pages. */
881 m->m_ext.ext_free(m);
883 /* Turn 'm' into a "normal" mbuf. */
884 m->m_flags &= ~(M_EXT | M_RDONLY | M_EXTPG);
885 m->m_data = m->m_dat;
887 /* Copy data back into m. */
888 bcopy(buf, mtod(m, char *), m->m_len);
894 * These next few routines are used to permit downgrading an unmapped
895 * mbuf to a chain of mapped mbufs. This is used when an interface
896 * doesn't supported unmapped mbufs or if checksums need to be
897 * computed in software.
899 * Each unmapped mbuf is converted to a chain of mbufs. First, any
900 * TLS header data is stored in a regular mbuf. Second, each page of
901 * unmapped data is stored in an mbuf with an EXT_SFBUF external
902 * cluster. These mbufs use an sf_buf to provide a valid KVA for the
903 * associated physical page. They also hold a reference on the
904 * original M_EXTPG mbuf to ensure the physical page doesn't go away.
905 * Finally, any TLS trailer data is stored in a regular mbuf.
907 * mb_unmapped_free_mext() is the ext_free handler for the EXT_SFBUF
908 * mbufs. It frees the associated sf_buf and releases its reference
909 * on the original M_EXTPG mbuf.
911 * _mb_unmapped_to_ext() is a helper function that converts a single
912 * unmapped mbuf into a chain of mbufs.
914 * mb_unmapped_to_ext() is the public function that walks an mbuf
915 * chain converting any unmapped mbufs to mapped mbufs. It returns
916 * the new chain of unmapped mbufs on success. On failure it frees
917 * the original mbuf chain and returns NULL.
920 mb_unmapped_free_mext(struct mbuf *m)
925 sf = m->m_ext.ext_arg1;
928 /* Drop the reference on the backing M_EXTPG mbuf. */
929 old_m = m->m_ext.ext_arg2;
930 mb_free_extpg(old_m);
934 _mb_unmapped_to_ext(struct mbuf *m)
936 struct mbuf *m_new, *top, *prev, *mref;
939 int i, len, off, pglen, pgoff, seglen, segoff;
940 volatile u_int *refcnt;
945 KASSERT(m->m_epg_tls == NULL, ("%s: can't convert TLS mbuf %p",
948 /* See if this is the mbuf that holds the embedded refcount. */
949 if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
950 refcnt = &m->m_ext.ext_count;
953 KASSERT(m->m_ext.ext_cnt != NULL,
954 ("%s: no refcounting pointer on %p", __func__, m));
955 refcnt = m->m_ext.ext_cnt;
956 mref = __containerof(refcnt, struct mbuf, m_ext.ext_count);
959 /* Skip over any data removed from the front. */
960 off = mtod(m, vm_offset_t);
963 if (m->m_epg_hdrlen != 0) {
964 if (off >= m->m_epg_hdrlen) {
965 off -= m->m_epg_hdrlen;
967 seglen = m->m_epg_hdrlen - off;
969 seglen = min(seglen, len);
972 m_new = m_get(M_NOWAIT, MT_DATA);
975 m_new->m_len = seglen;
977 memcpy(mtod(m_new, void *), &m->m_epg_hdr[segoff],
981 pgoff = m->m_epg_1st_off;
982 for (i = 0; i < m->m_epg_npgs && len > 0; i++) {
983 pglen = m_epg_pagelen(m, i, pgoff);
989 seglen = pglen - off;
990 segoff = pgoff + off;
992 seglen = min(seglen, len);
995 pg = PHYS_TO_VM_PAGE(m->m_epg_pa[i]);
996 m_new = m_get(M_NOWAIT, MT_DATA);
1002 prev->m_next = m_new;
1005 sf = sf_buf_alloc(pg, SFB_NOWAIT);
1010 m_extadd(m_new, (char *)sf_buf_kva(sf), PAGE_SIZE,
1011 mb_unmapped_free_mext, sf, mref, M_RDONLY, EXT_SFBUF);
1012 m_new->m_data += segoff;
1013 m_new->m_len = seglen;
1018 KASSERT((off + len) <= m->m_epg_trllen,
1019 ("off + len > trail (%d + %d > %d)", off, len,
1021 m_new = m_get(M_NOWAIT, MT_DATA);
1027 prev->m_next = m_new;
1029 memcpy(mtod(m_new, void *), &m->m_epg_trail[off], len);
1034 * Obtain an additional reference on the old mbuf for
1035 * each created EXT_SFBUF mbuf. They will be dropped
1036 * in mb_unmapped_free_mext().
1041 atomic_add_int(refcnt, ref_inc);
1049 * Obtain an additional reference on the old mbuf for
1050 * each created EXT_SFBUF mbuf. They will be
1051 * immediately dropped when these mbufs are freed
1057 atomic_add_int(refcnt, ref_inc);
1065 mb_unmapped_to_ext(struct mbuf *top)
1067 struct mbuf *m, *next, *prev = NULL;
1070 for (m = top; m != NULL; m = next) {
1071 /* m might be freed, so cache the next pointer. */
1073 if (m->m_flags & M_EXTPG) {
1076 * Remove 'm' from the new chain so
1077 * that the 'top' chain terminates
1078 * before 'm' in case 'top' is freed
1081 prev->m_next = NULL;
1083 m = _mb_unmapped_to_ext(m);
1096 * Replaced one mbuf with a chain, so we must
1097 * find the end of chain.
1111 * Allocate an empty M_EXTPG mbuf. The ext_free routine is
1112 * responsible for freeing any pages backing this mbuf when it is
1116 mb_alloc_ext_pgs(int how, m_ext_free_t ext_free)
1120 m = m_get(how, MT_DATA);
1126 m->m_epg_1st_off = 0;
1127 m->m_epg_last_len = 0;
1129 m->m_epg_hdrlen = 0;
1130 m->m_epg_trllen = 0;
1131 m->m_epg_tls = NULL;
1134 m->m_flags |= (M_EXT | M_RDONLY | M_EXTPG);
1135 m->m_ext.ext_flags = EXT_FLAG_EMBREF;
1136 m->m_ext.ext_count = 1;
1137 m->m_ext.ext_size = 0;
1138 m->m_ext.ext_free = ext_free;
1143 * Clean up after mbufs with M_EXT storage attached to them if the
1144 * reference count hits 1.
1147 mb_free_ext(struct mbuf *m)
1149 volatile u_int *refcnt;
1153 KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m));
1155 /* See if this is the mbuf that holds the embedded refcount. */
1156 if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
1157 refcnt = &m->m_ext.ext_count;
1160 KASSERT(m->m_ext.ext_cnt != NULL,
1161 ("%s: no refcounting pointer on %p", __func__, m));
1162 refcnt = m->m_ext.ext_cnt;
1163 mref = __containerof(refcnt, struct mbuf, m_ext.ext_count);
1167 * Check if the header is embedded in the cluster. It is
1168 * important that we can't touch any of the mbuf fields
1169 * after we have freed the external storage, since mbuf
1170 * could have been embedded in it. For now, the mbufs
1171 * embedded into the cluster are always of type EXT_EXTREF,
1172 * and for this type we won't free the mref.
1174 if (m->m_flags & M_NOFREE) {
1176 KASSERT(m->m_ext.ext_type == EXT_EXTREF ||
1177 m->m_ext.ext_type == EXT_RXRING,
1178 ("%s: no-free mbuf %p has wrong type", __func__, m));
1182 /* Free attached storage if this mbuf is the only reference to it. */
1183 if (*refcnt == 1 || atomic_fetchadd_int(refcnt, -1) == 1) {
1184 switch (m->m_ext.ext_type) {
1186 /* The packet zone is special. */
1189 uma_zfree(zone_pack, mref);
1192 uma_zfree(zone_clust, m->m_ext.ext_buf);
1193 uma_zfree(zone_mbuf, mref);
1196 uma_zfree(zone_jumbop, m->m_ext.ext_buf);
1197 uma_zfree(zone_mbuf, mref);
1200 uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
1201 uma_zfree(zone_mbuf, mref);
1204 uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
1205 uma_zfree(zone_mbuf, mref);
1210 case EXT_DISPOSABLE:
1211 KASSERT(mref->m_ext.ext_free != NULL,
1212 ("%s: ext_free not set", __func__));
1213 mref->m_ext.ext_free(mref);
1214 uma_zfree(zone_mbuf, mref);
1217 KASSERT(m->m_ext.ext_free != NULL,
1218 ("%s: ext_free not set", __func__));
1219 m->m_ext.ext_free(m);
1222 KASSERT(m->m_ext.ext_free == NULL,
1223 ("%s: ext_free is set", __func__));
1226 KASSERT(m->m_ext.ext_type == 0,
1227 ("%s: unknown ext_type", __func__));
1231 if (freembuf && m != mref)
1232 uma_zfree(zone_mbuf, m);
1236 * Clean up after mbufs with M_EXTPG storage attached to them if the
1237 * reference count hits 1.
1240 mb_free_extpg(struct mbuf *m)
1242 volatile u_int *refcnt;
1247 /* See if this is the mbuf that holds the embedded refcount. */
1248 if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
1249 refcnt = &m->m_ext.ext_count;
1252 KASSERT(m->m_ext.ext_cnt != NULL,
1253 ("%s: no refcounting pointer on %p", __func__, m));
1254 refcnt = m->m_ext.ext_cnt;
1255 mref = __containerof(refcnt, struct mbuf, m_ext.ext_count);
1258 /* Free attached storage if this mbuf is the only reference to it. */
1259 if (*refcnt == 1 || atomic_fetchadd_int(refcnt, -1) == 1) {
1260 KASSERT(mref->m_ext.ext_free != NULL,
1261 ("%s: ext_free not set", __func__));
1263 mref->m_ext.ext_free(mref);
1265 if (mref->m_epg_tls != NULL &&
1266 !refcount_release_if_not_last(&mref->m_epg_tls->refcount))
1267 ktls_enqueue_to_free(mref);
1270 uma_zfree(zone_mbuf, mref);
1274 uma_zfree(zone_mbuf, m);
1278 * Official mbuf(9) allocation KPI for stack and drivers:
1280 * m_get() - a single mbuf without any attachments, sys/mbuf.h.
1281 * m_gethdr() - a single mbuf initialized as M_PKTHDR, sys/mbuf.h.
1282 * m_getcl() - an mbuf + 2k cluster, sys/mbuf.h.
1283 * m_clget() - attach cluster to already allocated mbuf.
1284 * m_cljget() - attach jumbo cluster to already allocated mbuf.
1285 * m_get2() - allocate minimum mbuf that would fit size argument.
1286 * m_getm2() - allocate a chain of mbufs/clusters.
1287 * m_extadd() - attach external cluster to mbuf.
1289 * m_free() - free single mbuf with its tags and ext, sys/mbuf.h.
1290 * m_freem() - free chain of mbufs.
1294 m_clget(struct mbuf *m, int how)
1297 KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
1299 m->m_ext.ext_buf = (char *)NULL;
1300 uma_zalloc_arg(zone_clust, m, how);
1302 * On a cluster allocation failure, drain the packet zone and retry,
1303 * we might be able to loosen a few clusters up on the drain.
1305 if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) {
1306 uma_zone_reclaim(zone_pack, UMA_RECLAIM_DRAIN);
1307 uma_zalloc_arg(zone_clust, m, how);
1309 MBUF_PROBE2(m__clget, m, how);
1310 return (m->m_flags & M_EXT);
1314 * m_cljget() is different from m_clget() as it can allocate clusters without
1315 * attaching them to an mbuf. In that case the return value is the pointer
1316 * to the cluster of the requested size. If an mbuf was specified, it gets
1317 * the cluster attached to it and the return value can be safely ignored.
1318 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
1321 m_cljget(struct mbuf *m, int how, int size)
1327 KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
1329 m->m_ext.ext_buf = NULL;
1332 zone = m_getzone(size);
1333 retval = uma_zalloc_arg(zone, m, how);
1335 MBUF_PROBE4(m__cljget, m, how, size, retval);
1341 * m_get2() allocates minimum mbuf that would fit "size" argument.
1344 m_get2(int size, int how, short type, int flags)
1346 struct mb_args args;
1352 if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0))
1353 return (uma_zalloc_arg(zone_mbuf, &args, how));
1354 if (size <= MCLBYTES)
1355 return (uma_zalloc_arg(zone_pack, &args, how));
1357 if (size > MJUMPAGESIZE)
1360 m = uma_zalloc_arg(zone_mbuf, &args, how);
1364 n = uma_zalloc_arg(zone_jumbop, m, how);
1366 uma_zfree(zone_mbuf, m);
1374 * m_getjcl() returns an mbuf with a cluster of the specified size attached.
1375 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
1378 m_getjcl(int how, short type, int flags, int size)
1380 struct mb_args args;
1384 if (size == MCLBYTES)
1385 return m_getcl(how, type, flags);
1390 m = uma_zalloc_arg(zone_mbuf, &args, how);
1394 zone = m_getzone(size);
1395 n = uma_zalloc_arg(zone, m, how);
1397 uma_zfree(zone_mbuf, m);
1400 MBUF_PROBE5(m__getjcl, how, type, flags, size, m);
1405 * Allocate a given length worth of mbufs and/or clusters (whatever fits
1406 * best) and return a pointer to the top of the allocated chain. If an
1407 * existing mbuf chain is provided, then we will append the new chain
1408 * to the existing one and return a pointer to the provided mbuf.
1411 m_getm2(struct mbuf *m, int len, int how, short type, int flags)
1413 struct mbuf *mb, *nm = NULL, *mtail = NULL;
1415 KASSERT(len >= 0, ("%s: len is < 0", __func__));
1417 /* Validate flags. */
1418 flags &= (M_PKTHDR | M_EOR);
1420 /* Packet header mbuf must be first in chain. */
1421 if ((flags & M_PKTHDR) && m != NULL)
1424 /* Loop and append maximum sized mbufs to the chain tail. */
1427 if (len > MCLBYTES) {
1428 mb = m_getjcl(M_NOWAIT, type, (flags & M_PKTHDR),
1432 if (len >= MINCLSIZE)
1433 mb = m_getcl(how, type, (flags & M_PKTHDR));
1434 else if (flags & M_PKTHDR)
1435 mb = m_gethdr(how, type);
1437 mb = m_get(how, type);
1440 * Fail the whole operation if one mbuf can't be
1456 flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */
1459 mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */
1461 /* If mbuf was supplied, append new chain to the end of it. */
1463 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
1466 mtail->m_flags &= ~M_EOR;
1474 * Configure a provided mbuf to refer to the provided external storage
1475 * buffer and setup a reference count for said buffer.
1478 * mb The existing mbuf to which to attach the provided buffer.
1479 * buf The address of the provided external storage buffer.
1480 * size The size of the provided buffer.
1481 * freef A pointer to a routine that is responsible for freeing the
1482 * provided external storage buffer.
1483 * args A pointer to an argument structure (of any type) to be passed
1484 * to the provided freef routine (may be NULL).
1485 * flags Any other flags to be passed to the provided mbuf.
1486 * type The type that the external storage buffer should be
1493 m_extadd(struct mbuf *mb, char *buf, u_int size, m_ext_free_t freef,
1494 void *arg1, void *arg2, int flags, int type)
1497 KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
1499 mb->m_flags |= (M_EXT | flags);
1500 mb->m_ext.ext_buf = buf;
1501 mb->m_data = mb->m_ext.ext_buf;
1502 mb->m_ext.ext_size = size;
1503 mb->m_ext.ext_free = freef;
1504 mb->m_ext.ext_arg1 = arg1;
1505 mb->m_ext.ext_arg2 = arg2;
1506 mb->m_ext.ext_type = type;
1508 if (type != EXT_EXTREF) {
1509 mb->m_ext.ext_count = 1;
1510 mb->m_ext.ext_flags = EXT_FLAG_EMBREF;
1512 mb->m_ext.ext_flags = 0;
1516 * Free an entire chain of mbufs and associated external buffers, if
1520 m_freem(struct mbuf *mb)
1523 MBUF_PROBE1(m__freem, mb);
1529 m_snd_tag_alloc(struct ifnet *ifp, union if_snd_tag_alloc_params *params,
1530 struct m_snd_tag **mstp)
1533 if (ifp->if_snd_tag_alloc == NULL)
1534 return (EOPNOTSUPP);
1535 return (ifp->if_snd_tag_alloc(ifp, params, mstp));
1539 m_snd_tag_init(struct m_snd_tag *mst, struct ifnet *ifp, u_int type)
1544 refcount_init(&mst->refcount, 1);
1546 counter_u64_add(snd_tag_count, 1);
1550 m_snd_tag_destroy(struct m_snd_tag *mst)
1555 ifp->if_snd_tag_free(mst);
1557 counter_u64_add(snd_tag_count, -1);
1561 * Allocate an mbuf with anonymous external pages.
1564 mb_alloc_ext_plus_pages(int len, int how)
1570 m = mb_alloc_ext_pgs(how, mb_free_mext_pgs);
1573 m->m_epg_flags |= EPG_FLAG_ANON;
1574 npgs = howmany(len, PAGE_SIZE);
1575 for (i = 0; i < npgs; i++) {
1577 pg = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
1578 VM_ALLOC_NOOBJ | VM_ALLOC_NODUMP | VM_ALLOC_WIRED);
1580 if (how == M_NOWAIT) {
1587 } while (pg == NULL);
1588 m->m_epg_pa[i] = VM_PAGE_TO_PHYS(pg);
1590 m->m_epg_npgs = npgs;
1595 * Copy the data in the mbuf chain to a chain of mbufs with anonymous external
1597 * len is the length of data in the input mbuf chain.
1598 * mlen is the maximum number of bytes put into each ext_page mbuf.
1601 mb_mapped_to_unmapped(struct mbuf *mp, int len, int mlen, int how,
1602 struct mbuf **mlast)
1604 struct mbuf *m, *mout;
1605 char *pgpos, *mbpos;
1606 int i, mblen, mbufsiz, pglen, xfer;
1610 mbufsiz = min(mlen, len);
1611 m = mout = mb_alloc_ext_plus_pages(mbufsiz, how);
1614 pgpos = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[0]);
1620 if (++i == m->m_epg_npgs) {
1621 m->m_epg_last_len = PAGE_SIZE;
1622 mbufsiz = min(mlen, len);
1623 m->m_next = mb_alloc_ext_plus_pages(mbufsiz,
1632 pgpos = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[i]);
1635 while (mblen == 0) {
1640 KASSERT((mp->m_flags & M_EXTPG) == 0,
1641 ("mb_copym_ext_pgs: ext_pgs input mbuf"));
1642 mbpos = mtod(mp, char *);
1646 xfer = min(mblen, pglen);
1647 memcpy(pgpos, mbpos, xfer);
1655 m->m_epg_last_len = PAGE_SIZE - pglen;