xref: /freebsd/sys/kern/kern_mbuf.c (revision eec189c70ba5bed05b2831426bb04e8a0b38fdb2)
1099a0e58SBosko Milekic /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
48076cb52SBosko Milekic  * Copyright (c) 2004, 2005,
58076cb52SBosko Milekic  *	Bosko Milekic <bmilekic@FreeBSD.org>.  All rights reserved.
6099a0e58SBosko Milekic  *
7099a0e58SBosko Milekic  * Redistribution and use in source and binary forms, with or without
8099a0e58SBosko Milekic  * modification, are permitted provided that the following conditions
9099a0e58SBosko Milekic  * are met:
10099a0e58SBosko Milekic  * 1. Redistributions of source code must retain the above copyright
11099a0e58SBosko Milekic  *    notice unmodified, this list of conditions and the following
12099a0e58SBosko Milekic  *    disclaimer.
13099a0e58SBosko Milekic  * 2. Redistributions in binary form must reproduce the above copyright
14099a0e58SBosko Milekic  *    notice, this list of conditions and the following disclaimer in the
15099a0e58SBosko Milekic  *    documentation and/or other materials provided with the distribution.
16099a0e58SBosko Milekic  *
17099a0e58SBosko Milekic  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18099a0e58SBosko Milekic  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19099a0e58SBosko Milekic  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20099a0e58SBosko Milekic  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21099a0e58SBosko Milekic  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22099a0e58SBosko Milekic  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23099a0e58SBosko Milekic  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24099a0e58SBosko Milekic  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25099a0e58SBosko Milekic  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26099a0e58SBosko Milekic  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27099a0e58SBosko Milekic  * SUCH DAMAGE.
28099a0e58SBosko Milekic  */
29099a0e58SBosko Milekic 
30099a0e58SBosko Milekic #include <sys/cdefs.h>
31099a0e58SBosko Milekic __FBSDID("$FreeBSD$");
32099a0e58SBosko Milekic 
33099a0e58SBosko Milekic #include "opt_param.h"
34099a0e58SBosko Milekic 
35099a0e58SBosko Milekic #include <sys/param.h>
363937ee75SConrad Meyer #include <sys/conf.h>
379978bd99SMark Johnston #include <sys/domainset.h>
38099a0e58SBosko Milekic #include <sys/malloc.h>
39099a0e58SBosko Milekic #include <sys/systm.h>
40099a0e58SBosko Milekic #include <sys/mbuf.h>
41099a0e58SBosko Milekic #include <sys/domain.h>
42099a0e58SBosko Milekic #include <sys/eventhandler.h>
43099a0e58SBosko Milekic #include <sys/kernel.h>
445475ca5aSMark Johnston #include <sys/limits.h>
4554503a13SJonathan T. Looney #include <sys/lock.h>
4654503a13SJonathan T. Looney #include <sys/mutex.h>
47099a0e58SBosko Milekic #include <sys/protosw.h>
48099a0e58SBosko Milekic #include <sys/smp.h>
49099a0e58SBosko Milekic #include <sys/sysctl.h>
50099a0e58SBosko Milekic 
51099a0e58SBosko Milekic #include <vm/vm.h>
52c45c0034SAlan Cox #include <vm/vm_extern.h>
53c45c0034SAlan Cox #include <vm/vm_kern.h>
54099a0e58SBosko Milekic #include <vm/vm_page.h>
5537140716SAndre Oppermann #include <vm/vm_map.h>
56099a0e58SBosko Milekic #include <vm/uma.h>
57121f0509SMike Silbersack #include <vm/uma_dbg.h>
58099a0e58SBosko Milekic 
59099a0e58SBosko Milekic /*
60099a0e58SBosko Milekic  * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
61099a0e58SBosko Milekic  * Zones.
62099a0e58SBosko Milekic  *
63099a0e58SBosko Milekic  * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
64099a0e58SBosko Milekic  * Zone.  The Zone can be capped at kern.ipc.nmbclusters, if the
65099a0e58SBosko Milekic  * administrator so desires.
66099a0e58SBosko Milekic  *
67099a0e58SBosko Milekic  * Mbufs are allocated from a UMA Master Zone called the Mbuf
68099a0e58SBosko Milekic  * Zone.
69099a0e58SBosko Milekic  *
70099a0e58SBosko Milekic  * Additionally, FreeBSD provides a Packet Zone, which it
71099a0e58SBosko Milekic  * configures as a Secondary Zone to the Mbuf Master Zone,
72099a0e58SBosko Milekic  * thus sharing backend Slab kegs with the Mbuf Master Zone.
73099a0e58SBosko Milekic  *
74099a0e58SBosko Milekic  * Thus common-case allocations and locking are simplified:
75099a0e58SBosko Milekic  *
76099a0e58SBosko Milekic  *  m_clget()                m_getcl()
77099a0e58SBosko Milekic  *    |                         |
78099a0e58SBosko Milekic  *    |   .------------>[(Packet Cache)]    m_get(), m_gethdr()
79099a0e58SBosko Milekic  *    |   |             [     Packet   ]            |
80099a0e58SBosko Milekic  *  [(Cluster Cache)]   [    Secondary ]   [ (Mbuf Cache)     ]
81099a0e58SBosko Milekic  *  [ Cluster Zone  ]   [     Zone     ]   [ Mbuf Master Zone ]
82099a0e58SBosko Milekic  *        |                       \________         |
83099a0e58SBosko Milekic  *  [ Cluster Keg   ]                      \       /
84099a0e58SBosko Milekic  *        |	                         [ Mbuf Keg   ]
85099a0e58SBosko Milekic  *  [ Cluster Slabs ]                         |
86099a0e58SBosko Milekic  *        |                              [ Mbuf Slabs ]
87099a0e58SBosko Milekic  *         \____________(VM)_________________/
8856a4e45aSAndre Oppermann  *
8956a4e45aSAndre Oppermann  *
90fcf90618SGleb Smirnoff  * Whenever an object is allocated with uma_zalloc() out of
9156a4e45aSAndre Oppermann  * one of the Zones its _ctor_ function is executed.  The same
92fcf90618SGleb Smirnoff  * for any deallocation through uma_zfree() the _dtor_ function
9356a4e45aSAndre Oppermann  * is executed.
9456a4e45aSAndre Oppermann  *
9556a4e45aSAndre Oppermann  * Caches are per-CPU and are filled from the Master Zone.
9656a4e45aSAndre Oppermann  *
97fcf90618SGleb Smirnoff  * Whenever an object is allocated from the underlying global
9856a4e45aSAndre Oppermann  * memory pool it gets pre-initialized with the _zinit_ functions.
99e3043798SPedro F. Giffuni  * When the Keg's are overfull objects get decommissioned with
10056a4e45aSAndre Oppermann  * _zfini_ functions and free'd back to the global memory pool.
10156a4e45aSAndre Oppermann  *
102099a0e58SBosko Milekic  */
103099a0e58SBosko Milekic 
104ead46972SAndre Oppermann int nmbufs;			/* limits number of mbufs */
10556a4e45aSAndre Oppermann int nmbclusters;		/* limits number of mbuf clusters */
106ec63cb90SAndre Oppermann int nmbjumbop;			/* limits number of page size jumbo clusters */
10756a4e45aSAndre Oppermann int nmbjumbo9;			/* limits number of 9k jumbo clusters */
10856a4e45aSAndre Oppermann int nmbjumbo16;			/* limits number of 16k jumbo clusters */
109099a0e58SBosko Milekic 
110e0c00addSAndre Oppermann static quad_t maxmbufmem;	/* overall real memory limit for all mbufs */
111e0c00addSAndre Oppermann 
112af3b2549SHans Petter Selasky SYSCTL_QUAD(_kern_ipc, OID_AUTO, maxmbufmem, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &maxmbufmem, 0,
113b6f49c23SHiren Panchasara     "Maximum real memory allocatable to various mbuf types");
114e0c00addSAndre Oppermann 
11562938659SBjoern A. Zeeb /*
11637140716SAndre Oppermann  * tunable_mbinit() has to be run before any mbuf allocations are done.
11762938659SBjoern A. Zeeb  */
118099a0e58SBosko Milekic static void
119099a0e58SBosko Milekic tunable_mbinit(void *dummy)
120099a0e58SBosko Milekic {
121e0c00addSAndre Oppermann 	quad_t realmem;
12237140716SAndre Oppermann 
12337140716SAndre Oppermann 	/*
12437140716SAndre Oppermann 	 * The default limit for all mbuf related memory is 1/2 of all
12537140716SAndre Oppermann 	 * available kernel memory (physical or kmem).
12637140716SAndre Oppermann 	 * At most it can be 3/4 of available kernel memory.
12737140716SAndre Oppermann 	 */
1285df87b21SJeff Roberson 	realmem = qmin((quad_t)physmem * PAGE_SIZE, vm_kmem_size);
12937140716SAndre Oppermann 	maxmbufmem = realmem / 2;
130e0c00addSAndre Oppermann 	TUNABLE_QUAD_FETCH("kern.ipc.maxmbufmem", &maxmbufmem);
13137140716SAndre Oppermann 	if (maxmbufmem > realmem / 4 * 3)
13237140716SAndre Oppermann 		maxmbufmem = realmem / 4 * 3;
133099a0e58SBosko Milekic 
134812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
135416a434cSAndre Oppermann 	if (nmbclusters == 0)
136416a434cSAndre Oppermann 		nmbclusters = maxmbufmem / MCLBYTES / 4;
137812302c3SNavdeep Parhar 
138812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop);
139812302c3SNavdeep Parhar 	if (nmbjumbop == 0)
140416a434cSAndre Oppermann 		nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4;
141812302c3SNavdeep Parhar 
142812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbjumbo9", &nmbjumbo9);
143812302c3SNavdeep Parhar 	if (nmbjumbo9 == 0)
144416a434cSAndre Oppermann 		nmbjumbo9 = maxmbufmem / MJUM9BYTES / 6;
145812302c3SNavdeep Parhar 
146812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbjumbo16", &nmbjumbo16);
147812302c3SNavdeep Parhar 	if (nmbjumbo16 == 0)
148416a434cSAndre Oppermann 		nmbjumbo16 = maxmbufmem / MJUM16BYTES / 6;
149416a434cSAndre Oppermann 
150416a434cSAndre Oppermann 	/*
151416a434cSAndre Oppermann 	 * We need at least as many mbufs as we have clusters of
152416a434cSAndre Oppermann 	 * the various types added together.
153416a434cSAndre Oppermann 	 */
154416a434cSAndre Oppermann 	TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
155416a434cSAndre Oppermann 	if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16)
156416a434cSAndre Oppermann 		nmbufs = lmax(maxmbufmem / MSIZE / 5,
157416a434cSAndre Oppermann 		    nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16);
158099a0e58SBosko Milekic }
15937140716SAndre Oppermann SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
160099a0e58SBosko Milekic 
1614f590175SPaul Saab static int
1624f590175SPaul Saab sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
1634f590175SPaul Saab {
1644f590175SPaul Saab 	int error, newnmbclusters;
1654f590175SPaul Saab 
1664f590175SPaul Saab 	newnmbclusters = nmbclusters;
167041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
168d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbclusters != nmbclusters) {
169ead46972SAndre Oppermann 		if (newnmbclusters > nmbclusters &&
170ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
1714f590175SPaul Saab 			nmbclusters = newnmbclusters;
172bc4a1b8cSAndre Oppermann 			nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
1734f590175SPaul Saab 			EVENTHANDLER_INVOKE(nmbclusters_change);
1744f590175SPaul Saab 		} else
1754f590175SPaul Saab 			error = EINVAL;
1764f590175SPaul Saab 	}
1774f590175SPaul Saab 	return (error);
1784f590175SPaul Saab }
1794f590175SPaul Saab SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
1804f590175SPaul Saab &nmbclusters, 0, sysctl_nmbclusters, "IU",
181099a0e58SBosko Milekic     "Maximum number of mbuf clusters allowed");
182cf70a46bSRandall Stewart 
183cf70a46bSRandall Stewart static int
184cf70a46bSRandall Stewart sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS)
185cf70a46bSRandall Stewart {
186cf70a46bSRandall Stewart 	int error, newnmbjumbop;
187cf70a46bSRandall Stewart 
188cf70a46bSRandall Stewart 	newnmbjumbop = nmbjumbop;
189cf70a46bSRandall Stewart 	error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req);
190d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbjumbop != nmbjumbop) {
191ead46972SAndre Oppermann 		if (newnmbjumbop > nmbjumbop &&
192ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
193cf70a46bSRandall Stewart 			nmbjumbop = newnmbjumbop;
194bc4a1b8cSAndre Oppermann 			nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
195cf70a46bSRandall Stewart 		} else
196cf70a46bSRandall Stewart 			error = EINVAL;
197cf70a46bSRandall Stewart 	}
198cf70a46bSRandall Stewart 	return (error);
199cf70a46bSRandall Stewart }
200cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW,
201cf70a46bSRandall Stewart &nmbjumbop, 0, sysctl_nmbjumbop, "IU",
202ec63cb90SAndre Oppermann     "Maximum number of mbuf page size jumbo clusters allowed");
203cf70a46bSRandall Stewart 
204cf70a46bSRandall Stewart static int
205cf70a46bSRandall Stewart sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS)
206cf70a46bSRandall Stewart {
207cf70a46bSRandall Stewart 	int error, newnmbjumbo9;
208cf70a46bSRandall Stewart 
209cf70a46bSRandall Stewart 	newnmbjumbo9 = nmbjumbo9;
210cf70a46bSRandall Stewart 	error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req);
211d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbjumbo9 != nmbjumbo9) {
212ead46972SAndre Oppermann 		if (newnmbjumbo9 > nmbjumbo9 &&
213ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
214cf70a46bSRandall Stewart 			nmbjumbo9 = newnmbjumbo9;
215bc4a1b8cSAndre Oppermann 			nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
216cf70a46bSRandall Stewart 		} else
217cf70a46bSRandall Stewart 			error = EINVAL;
218cf70a46bSRandall Stewart 	}
219cf70a46bSRandall Stewart 	return (error);
220cf70a46bSRandall Stewart }
221cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW,
222cf70a46bSRandall Stewart &nmbjumbo9, 0, sysctl_nmbjumbo9, "IU",
22356a4e45aSAndre Oppermann     "Maximum number of mbuf 9k jumbo clusters allowed");
224cf70a46bSRandall Stewart 
225cf70a46bSRandall Stewart static int
226cf70a46bSRandall Stewart sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS)
227cf70a46bSRandall Stewart {
228cf70a46bSRandall Stewart 	int error, newnmbjumbo16;
229cf70a46bSRandall Stewart 
230cf70a46bSRandall Stewart 	newnmbjumbo16 = nmbjumbo16;
231cf70a46bSRandall Stewart 	error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req);
232d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbjumbo16 != nmbjumbo16) {
233ead46972SAndre Oppermann 		if (newnmbjumbo16 > nmbjumbo16 &&
234ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
235cf70a46bSRandall Stewart 			nmbjumbo16 = newnmbjumbo16;
236bc4a1b8cSAndre Oppermann 			nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
237cf70a46bSRandall Stewart 		} else
238cf70a46bSRandall Stewart 			error = EINVAL;
239cf70a46bSRandall Stewart 	}
240cf70a46bSRandall Stewart 	return (error);
241cf70a46bSRandall Stewart }
242cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW,
243cf70a46bSRandall Stewart &nmbjumbo16, 0, sysctl_nmbjumbo16, "IU",
24456a4e45aSAndre Oppermann     "Maximum number of mbuf 16k jumbo clusters allowed");
245cf70a46bSRandall Stewart 
246ead46972SAndre Oppermann static int
247ead46972SAndre Oppermann sysctl_nmbufs(SYSCTL_HANDLER_ARGS)
248ead46972SAndre Oppermann {
249ead46972SAndre Oppermann 	int error, newnmbufs;
250ead46972SAndre Oppermann 
251ead46972SAndre Oppermann 	newnmbufs = nmbufs;
252ead46972SAndre Oppermann 	error = sysctl_handle_int(oidp, &newnmbufs, 0, req);
253d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbufs != nmbufs) {
254ead46972SAndre Oppermann 		if (newnmbufs > nmbufs) {
255ead46972SAndre Oppermann 			nmbufs = newnmbufs;
256bc4a1b8cSAndre Oppermann 			nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
257ead46972SAndre Oppermann 			EVENTHANDLER_INVOKE(nmbufs_change);
258ead46972SAndre Oppermann 		} else
259ead46972SAndre Oppermann 			error = EINVAL;
260ead46972SAndre Oppermann 	}
261ead46972SAndre Oppermann 	return (error);
262ead46972SAndre Oppermann }
263e0c00addSAndre Oppermann SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbufs, CTLTYPE_INT|CTLFLAG_RW,
264ead46972SAndre Oppermann &nmbufs, 0, sysctl_nmbufs, "IU",
265ead46972SAndre Oppermann     "Maximum number of mbufs allowed");
266cf70a46bSRandall Stewart 
267099a0e58SBosko Milekic /*
268099a0e58SBosko Milekic  * Zones from which we allocate.
269099a0e58SBosko Milekic  */
270099a0e58SBosko Milekic uma_zone_t	zone_mbuf;
271099a0e58SBosko Milekic uma_zone_t	zone_clust;
272099a0e58SBosko Milekic uma_zone_t	zone_pack;
273ec63cb90SAndre Oppermann uma_zone_t	zone_jumbop;
27456a4e45aSAndre Oppermann uma_zone_t	zone_jumbo9;
27556a4e45aSAndre Oppermann uma_zone_t	zone_jumbo16;
276099a0e58SBosko Milekic 
277099a0e58SBosko Milekic /*
278099a0e58SBosko Milekic  * Local prototypes.
279099a0e58SBosko Milekic  */
280b23f72e9SBrian Feldman static int	mb_ctor_mbuf(void *, int, void *, int);
281b23f72e9SBrian Feldman static int	mb_ctor_clust(void *, int, void *, int);
282b23f72e9SBrian Feldman static int	mb_ctor_pack(void *, int, void *, int);
283099a0e58SBosko Milekic static void	mb_dtor_mbuf(void *, int, void *);
28456a4e45aSAndre Oppermann static void	mb_dtor_pack(void *, int, void *);
28556a4e45aSAndre Oppermann static int	mb_zinit_pack(void *, int, int);
28656a4e45aSAndre Oppermann static void	mb_zfini_pack(void *, int);
287e60b2fcbSGleb Smirnoff static void	mb_reclaim(uma_zone_t, int);
288ab3185d1SJeff Roberson static void    *mbuf_jumbo_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
289099a0e58SBosko Milekic 
29037140716SAndre Oppermann /* Ensure that MSIZE is a power of 2. */
291a04946cfSBrian Somers CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
292a04946cfSBrian Somers 
293099a0e58SBosko Milekic /*
294099a0e58SBosko Milekic  * Initialize FreeBSD Network buffer allocation.
295099a0e58SBosko Milekic  */
296099a0e58SBosko Milekic static void
297099a0e58SBosko Milekic mbuf_init(void *dummy)
298099a0e58SBosko Milekic {
299099a0e58SBosko Milekic 
300099a0e58SBosko Milekic 	/*
301099a0e58SBosko Milekic 	 * Configure UMA zones for Mbufs, Clusters, and Packets.
302099a0e58SBosko Milekic 	 */
30356a4e45aSAndre Oppermann 	zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
30456a4e45aSAndre Oppermann 	    mb_ctor_mbuf, mb_dtor_mbuf,
305121f0509SMike Silbersack #ifdef INVARIANTS
30656a4e45aSAndre Oppermann 	    trash_init, trash_fini,
307121f0509SMike Silbersack #else
30856a4e45aSAndre Oppermann 	    NULL, NULL,
309121f0509SMike Silbersack #endif
31056a4e45aSAndre Oppermann 	    MSIZE - 1, UMA_ZONE_MAXBUCKET);
31145fe0bf7SPawel Jakub Dawidek 	if (nmbufs > 0)
31245fe0bf7SPawel Jakub Dawidek 		nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
3136e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_mbuf, "kern.ipc.nmbufs limit reached");
314e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_mbuf, mb_reclaim);
31556a4e45aSAndre Oppermann 
31668352adfSRobert Watson 	zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
31756a5f52eSGleb Smirnoff 	    mb_ctor_clust,
318121f0509SMike Silbersack #ifdef INVARIANTS
31956a5f52eSGleb Smirnoff 	    trash_dtor, trash_init, trash_fini,
320121f0509SMike Silbersack #else
32156a5f52eSGleb Smirnoff 	    NULL, NULL, NULL,
322121f0509SMike Silbersack #endif
32356a5f52eSGleb Smirnoff 	    UMA_ALIGN_PTR, 0);
32445fe0bf7SPawel Jakub Dawidek 	if (nmbclusters > 0)
32545fe0bf7SPawel Jakub Dawidek 		nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
3266e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_clust, "kern.ipc.nmbclusters limit reached");
327e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_clust, mb_reclaim);
328099a0e58SBosko Milekic 
32956a4e45aSAndre Oppermann 	zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
33056a4e45aSAndre Oppermann 	    mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
33156a4e45aSAndre Oppermann 
332fcf90618SGleb Smirnoff 	/* Make jumbo frame zone too. Page size, 9k and 16k. */
333ec63cb90SAndre Oppermann 	zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
33456a5f52eSGleb Smirnoff 	    mb_ctor_clust,
335d5269a63SAndre Oppermann #ifdef INVARIANTS
33656a5f52eSGleb Smirnoff 	    trash_dtor, trash_init, trash_fini,
337d5269a63SAndre Oppermann #else
33856a5f52eSGleb Smirnoff 	    NULL, NULL, NULL,
339d5269a63SAndre Oppermann #endif
34056a5f52eSGleb Smirnoff 	    UMA_ALIGN_PTR, 0);
34145fe0bf7SPawel Jakub Dawidek 	if (nmbjumbop > 0)
34245fe0bf7SPawel Jakub Dawidek 		nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
3436e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_jumbop, "kern.ipc.nmbjumbop limit reached");
344e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_jumbop, mb_reclaim);
345d5269a63SAndre Oppermann 
34656a4e45aSAndre Oppermann 	zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
34756a5f52eSGleb Smirnoff 	    mb_ctor_clust,
34856a4e45aSAndre Oppermann #ifdef INVARIANTS
34956a5f52eSGleb Smirnoff 	    trash_dtor, trash_init, trash_fini,
35056a4e45aSAndre Oppermann #else
35156a5f52eSGleb Smirnoff 	    NULL, NULL, NULL,
35256a4e45aSAndre Oppermann #endif
35356a5f52eSGleb Smirnoff 	    UMA_ALIGN_PTR, 0);
354ba63339aSAlan Cox 	uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc);
35545fe0bf7SPawel Jakub Dawidek 	if (nmbjumbo9 > 0)
35645fe0bf7SPawel Jakub Dawidek 		nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
3576e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_jumbo9, "kern.ipc.nmbjumbo9 limit reached");
358e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_jumbo9, mb_reclaim);
35956a4e45aSAndre Oppermann 
36056a4e45aSAndre Oppermann 	zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
36156a5f52eSGleb Smirnoff 	    mb_ctor_clust,
36256a4e45aSAndre Oppermann #ifdef INVARIANTS
36356a5f52eSGleb Smirnoff 	    trash_dtor, trash_init, trash_fini,
36456a4e45aSAndre Oppermann #else
36556a5f52eSGleb Smirnoff 	    NULL, NULL, NULL,
36656a4e45aSAndre Oppermann #endif
36756a5f52eSGleb Smirnoff 	    UMA_ALIGN_PTR, 0);
368ba63339aSAlan Cox 	uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc);
36945fe0bf7SPawel Jakub Dawidek 	if (nmbjumbo16 > 0)
37045fe0bf7SPawel Jakub Dawidek 		nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
3716e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_jumbo16, "kern.ipc.nmbjumbo16 limit reached");
372e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_jumbo16, mb_reclaim);
37356a4e45aSAndre Oppermann 
374099a0e58SBosko Milekic 	/*
375099a0e58SBosko Milekic 	 * Hook event handler for low-memory situation, used to
376099a0e58SBosko Milekic 	 * drain protocols and push data back to the caches (UMA
377099a0e58SBosko Milekic 	 * later pushes it back to VM).
378099a0e58SBosko Milekic 	 */
379099a0e58SBosko Milekic 	EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
380099a0e58SBosko Milekic 	    EVENTHANDLER_PRI_FIRST);
381099a0e58SBosko Milekic }
38237140716SAndre Oppermann SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
383099a0e58SBosko Milekic 
3845475ca5aSMark Johnston #ifdef NETDUMP
3855475ca5aSMark Johnston /*
3865475ca5aSMark Johnston  * netdump makes use of a pre-allocated pool of mbufs and clusters.  When
3875475ca5aSMark Johnston  * netdump is configured, we initialize a set of UMA cache zones which return
3885475ca5aSMark Johnston  * items from this pool.  At panic-time, the regular UMA zone pointers are
3895475ca5aSMark Johnston  * overwritten with those of the cache zones so that drivers may allocate and
3905475ca5aSMark Johnston  * free mbufs and clusters without attempting to allocate physical memory.
3915475ca5aSMark Johnston  *
3925475ca5aSMark Johnston  * We keep mbufs and clusters in a pair of mbuf queues.  In particular, for
3935475ca5aSMark Johnston  * the purpose of caching clusters, we treat them as mbufs.
3945475ca5aSMark Johnston  */
3955475ca5aSMark Johnston static struct mbufq nd_mbufq =
3965475ca5aSMark Johnston     { STAILQ_HEAD_INITIALIZER(nd_mbufq.mq_head), 0, INT_MAX };
3975475ca5aSMark Johnston static struct mbufq nd_clustq =
3985475ca5aSMark Johnston     { STAILQ_HEAD_INITIALIZER(nd_clustq.mq_head), 0, INT_MAX };
3995475ca5aSMark Johnston 
4005475ca5aSMark Johnston static int nd_clsize;
4015475ca5aSMark Johnston static uma_zone_t nd_zone_mbuf;
4025475ca5aSMark Johnston static uma_zone_t nd_zone_clust;
4035475ca5aSMark Johnston static uma_zone_t nd_zone_pack;
4045475ca5aSMark Johnston 
4055475ca5aSMark Johnston static int
4065475ca5aSMark Johnston nd_buf_import(void *arg, void **store, int count, int domain __unused,
4075475ca5aSMark Johnston     int flags)
4085475ca5aSMark Johnston {
4095475ca5aSMark Johnston 	struct mbufq *q;
4105475ca5aSMark Johnston 	struct mbuf *m;
4115475ca5aSMark Johnston 	int i;
4125475ca5aSMark Johnston 
4135475ca5aSMark Johnston 	q = arg;
4145475ca5aSMark Johnston 
4155475ca5aSMark Johnston 	for (i = 0; i < count; i++) {
4165475ca5aSMark Johnston 		m = mbufq_dequeue(q);
4175475ca5aSMark Johnston 		if (m == NULL)
4185475ca5aSMark Johnston 			break;
4195475ca5aSMark Johnston 		trash_init(m, q == &nd_mbufq ? MSIZE : nd_clsize, flags);
4205475ca5aSMark Johnston 		store[i] = m;
4215475ca5aSMark Johnston 	}
4220d1467b1SConrad Meyer 	KASSERT((flags & M_WAITOK) == 0 || i == count,
4230d1467b1SConrad Meyer 	    ("%s: ran out of pre-allocated mbufs", __func__));
4245475ca5aSMark Johnston 	return (i);
4255475ca5aSMark Johnston }
4265475ca5aSMark Johnston 
4275475ca5aSMark Johnston static void
4285475ca5aSMark Johnston nd_buf_release(void *arg, void **store, int count)
4295475ca5aSMark Johnston {
4305475ca5aSMark Johnston 	struct mbufq *q;
4315475ca5aSMark Johnston 	struct mbuf *m;
4325475ca5aSMark Johnston 	int i;
4335475ca5aSMark Johnston 
4345475ca5aSMark Johnston 	q = arg;
4355475ca5aSMark Johnston 
4365475ca5aSMark Johnston 	for (i = 0; i < count; i++) {
4375475ca5aSMark Johnston 		m = store[i];
4385475ca5aSMark Johnston 		(void)mbufq_enqueue(q, m);
4395475ca5aSMark Johnston 	}
4405475ca5aSMark Johnston }
4415475ca5aSMark Johnston 
4425475ca5aSMark Johnston static int
4435475ca5aSMark Johnston nd_pack_import(void *arg __unused, void **store, int count, int domain __unused,
4445475ca5aSMark Johnston     int flags __unused)
4455475ca5aSMark Johnston {
4465475ca5aSMark Johnston 	struct mbuf *m;
4475475ca5aSMark Johnston 	void *clust;
4485475ca5aSMark Johnston 	int i;
4495475ca5aSMark Johnston 
4505475ca5aSMark Johnston 	for (i = 0; i < count; i++) {
4515475ca5aSMark Johnston 		m = m_get(MT_DATA, M_NOWAIT);
4525475ca5aSMark Johnston 		if (m == NULL)
4535475ca5aSMark Johnston 			break;
4545475ca5aSMark Johnston 		clust = uma_zalloc(nd_zone_clust, M_NOWAIT);
4555475ca5aSMark Johnston 		if (clust == NULL) {
4565475ca5aSMark Johnston 			m_free(m);
4575475ca5aSMark Johnston 			break;
4585475ca5aSMark Johnston 		}
4595475ca5aSMark Johnston 		mb_ctor_clust(clust, nd_clsize, m, 0);
4605475ca5aSMark Johnston 		store[i] = m;
4615475ca5aSMark Johnston 	}
4620d1467b1SConrad Meyer 	KASSERT((flags & M_WAITOK) == 0 || i == count,
4630d1467b1SConrad Meyer 	    ("%s: ran out of pre-allocated mbufs", __func__));
4645475ca5aSMark Johnston 	return (i);
4655475ca5aSMark Johnston }
4665475ca5aSMark Johnston 
4675475ca5aSMark Johnston static void
4685475ca5aSMark Johnston nd_pack_release(void *arg __unused, void **store, int count)
4695475ca5aSMark Johnston {
4705475ca5aSMark Johnston 	struct mbuf *m;
4715475ca5aSMark Johnston 	void *clust;
4725475ca5aSMark Johnston 	int i;
4735475ca5aSMark Johnston 
4745475ca5aSMark Johnston 	for (i = 0; i < count; i++) {
4755475ca5aSMark Johnston 		m = store[i];
4765475ca5aSMark Johnston 		clust = m->m_ext.ext_buf;
4775475ca5aSMark Johnston 		uma_zfree(nd_zone_clust, clust);
4785475ca5aSMark Johnston 		uma_zfree(nd_zone_mbuf, m);
4795475ca5aSMark Johnston 	}
4805475ca5aSMark Johnston }
4815475ca5aSMark Johnston 
4825475ca5aSMark Johnston /*
4835475ca5aSMark Johnston  * Free the pre-allocated mbufs and clusters reserved for netdump, and destroy
4845475ca5aSMark Johnston  * the corresponding UMA cache zones.
4855475ca5aSMark Johnston  */
4865475ca5aSMark Johnston void
4875475ca5aSMark Johnston netdump_mbuf_drain(void)
4885475ca5aSMark Johnston {
4895475ca5aSMark Johnston 	struct mbuf *m;
4905475ca5aSMark Johnston 	void *item;
4915475ca5aSMark Johnston 
4925475ca5aSMark Johnston 	if (nd_zone_mbuf != NULL) {
4935475ca5aSMark Johnston 		uma_zdestroy(nd_zone_mbuf);
4945475ca5aSMark Johnston 		nd_zone_mbuf = NULL;
4955475ca5aSMark Johnston 	}
4965475ca5aSMark Johnston 	if (nd_zone_clust != NULL) {
4975475ca5aSMark Johnston 		uma_zdestroy(nd_zone_clust);
4985475ca5aSMark Johnston 		nd_zone_clust = NULL;
4995475ca5aSMark Johnston 	}
5005475ca5aSMark Johnston 	if (nd_zone_pack != NULL) {
5015475ca5aSMark Johnston 		uma_zdestroy(nd_zone_pack);
5025475ca5aSMark Johnston 		nd_zone_pack = NULL;
5035475ca5aSMark Johnston 	}
5045475ca5aSMark Johnston 
5055475ca5aSMark Johnston 	while ((m = mbufq_dequeue(&nd_mbufq)) != NULL)
5065475ca5aSMark Johnston 		m_free(m);
5075475ca5aSMark Johnston 	while ((item = mbufq_dequeue(&nd_clustq)) != NULL)
5085475ca5aSMark Johnston 		uma_zfree(m_getzone(nd_clsize), item);
5095475ca5aSMark Johnston }
5105475ca5aSMark Johnston 
5115475ca5aSMark Johnston /*
5125475ca5aSMark Johnston  * Callback invoked immediately prior to starting a netdump.
5135475ca5aSMark Johnston  */
5145475ca5aSMark Johnston void
5155475ca5aSMark Johnston netdump_mbuf_dump(void)
5165475ca5aSMark Johnston {
5175475ca5aSMark Johnston 
5185475ca5aSMark Johnston 	/*
5195475ca5aSMark Johnston 	 * All cluster zones return buffers of the size requested by the
5205475ca5aSMark Johnston 	 * drivers.  It's up to the driver to reinitialize the zones if the
5215475ca5aSMark Johnston 	 * MTU of a netdump-enabled interface changes.
5225475ca5aSMark Johnston 	 */
5235475ca5aSMark Johnston 	printf("netdump: overwriting mbuf zone pointers\n");
5245475ca5aSMark Johnston 	zone_mbuf = nd_zone_mbuf;
5255475ca5aSMark Johnston 	zone_clust = nd_zone_clust;
5265475ca5aSMark Johnston 	zone_pack = nd_zone_pack;
5275475ca5aSMark Johnston 	zone_jumbop = nd_zone_clust;
5285475ca5aSMark Johnston 	zone_jumbo9 = nd_zone_clust;
5295475ca5aSMark Johnston 	zone_jumbo16 = nd_zone_clust;
5305475ca5aSMark Johnston }
5315475ca5aSMark Johnston 
5325475ca5aSMark Johnston /*
5335475ca5aSMark Johnston  * Reinitialize the netdump mbuf+cluster pool and cache zones.
5345475ca5aSMark Johnston  */
5355475ca5aSMark Johnston void
5365475ca5aSMark Johnston netdump_mbuf_reinit(int nmbuf, int nclust, int clsize)
5375475ca5aSMark Johnston {
5385475ca5aSMark Johnston 	struct mbuf *m;
5395475ca5aSMark Johnston 	void *item;
5405475ca5aSMark Johnston 
5415475ca5aSMark Johnston 	netdump_mbuf_drain();
5425475ca5aSMark Johnston 
5435475ca5aSMark Johnston 	nd_clsize = clsize;
5445475ca5aSMark Johnston 
5455475ca5aSMark Johnston 	nd_zone_mbuf = uma_zcache_create("netdump_" MBUF_MEM_NAME,
5465475ca5aSMark Johnston 	    MSIZE, mb_ctor_mbuf, mb_dtor_mbuf,
5475475ca5aSMark Johnston #ifdef INVARIANTS
5485475ca5aSMark Johnston 	    trash_init, trash_fini,
5495475ca5aSMark Johnston #else
5505475ca5aSMark Johnston 	    NULL, NULL,
5515475ca5aSMark Johnston #endif
5525475ca5aSMark Johnston 	    nd_buf_import, nd_buf_release,
5535475ca5aSMark Johnston 	    &nd_mbufq, UMA_ZONE_NOBUCKET);
5545475ca5aSMark Johnston 
5555475ca5aSMark Johnston 	nd_zone_clust = uma_zcache_create("netdump_" MBUF_CLUSTER_MEM_NAME,
5565475ca5aSMark Johnston 	    clsize, mb_ctor_clust,
5575475ca5aSMark Johnston #ifdef INVARIANTS
5585475ca5aSMark Johnston 	    trash_dtor, trash_init, trash_fini,
5595475ca5aSMark Johnston #else
5605475ca5aSMark Johnston 	    NULL, NULL, NULL,
5615475ca5aSMark Johnston #endif
5625475ca5aSMark Johnston 	    nd_buf_import, nd_buf_release,
5635475ca5aSMark Johnston 	    &nd_clustq, UMA_ZONE_NOBUCKET);
5645475ca5aSMark Johnston 
5655475ca5aSMark Johnston 	nd_zone_pack = uma_zcache_create("netdump_" MBUF_PACKET_MEM_NAME,
5665475ca5aSMark Johnston 	    MCLBYTES, mb_ctor_pack, mb_dtor_pack, NULL, NULL,
5675475ca5aSMark Johnston 	    nd_pack_import, nd_pack_release,
5685475ca5aSMark Johnston 	    NULL, UMA_ZONE_NOBUCKET);
5695475ca5aSMark Johnston 
5705475ca5aSMark Johnston 	while (nmbuf-- > 0) {
5715475ca5aSMark Johnston 		m = m_get(MT_DATA, M_WAITOK);
5725475ca5aSMark Johnston 		uma_zfree(nd_zone_mbuf, m);
5735475ca5aSMark Johnston 	}
5745475ca5aSMark Johnston 	while (nclust-- > 0) {
5755475ca5aSMark Johnston 		item = uma_zalloc(m_getzone(nd_clsize), M_WAITOK);
5765475ca5aSMark Johnston 		uma_zfree(nd_zone_clust, item);
5775475ca5aSMark Johnston 	}
5785475ca5aSMark Johnston }
5795475ca5aSMark Johnston #endif /* NETDUMP */
5805475ca5aSMark Johnston 
581099a0e58SBosko Milekic /*
582ba63339aSAlan Cox  * UMA backend page allocator for the jumbo frame zones.
583ba63339aSAlan Cox  *
584ba63339aSAlan Cox  * Allocates kernel virtual memory that is backed by contiguous physical
585ba63339aSAlan Cox  * pages.
586ba63339aSAlan Cox  */
587ba63339aSAlan Cox static void *
588ab3185d1SJeff Roberson mbuf_jumbo_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
589ab3185d1SJeff Roberson     int wait)
590ba63339aSAlan Cox {
591ba63339aSAlan Cox 
5927630c265SAlan Cox 	/* Inform UMA that this allocator uses kernel_map/object. */
5937630c265SAlan Cox 	*flags = UMA_SLAB_KERNEL;
5949978bd99SMark Johnston 	return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
5959978bd99SMark Johnston 	    bytes, wait, (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0,
5969978bd99SMark Johnston 	    VM_MEMATTR_DEFAULT));
597ba63339aSAlan Cox }
598ba63339aSAlan Cox 
599ba63339aSAlan Cox /*
600099a0e58SBosko Milekic  * Constructor for Mbuf master zone.
601099a0e58SBosko Milekic  *
602099a0e58SBosko Milekic  * The 'arg' pointer points to a mb_args structure which
603099a0e58SBosko Milekic  * contains call-specific information required to support the
60456a4e45aSAndre Oppermann  * mbuf allocation API.  See mbuf.h.
605099a0e58SBosko Milekic  */
606b23f72e9SBrian Feldman static int
607b23f72e9SBrian Feldman mb_ctor_mbuf(void *mem, int size, void *arg, int how)
608099a0e58SBosko Milekic {
609099a0e58SBosko Milekic 	struct mbuf *m;
610099a0e58SBosko Milekic 	struct mb_args *args;
611b23f72e9SBrian Feldman 	int error;
612099a0e58SBosko Milekic 	int flags;
613099a0e58SBosko Milekic 	short type;
614099a0e58SBosko Milekic 
615121f0509SMike Silbersack #ifdef INVARIANTS
616121f0509SMike Silbersack 	trash_ctor(mem, size, arg, how);
617121f0509SMike Silbersack #endif
618099a0e58SBosko Milekic 	args = (struct mb_args *)arg;
619099a0e58SBosko Milekic 	type = args->type;
620099a0e58SBosko Milekic 
62156a4e45aSAndre Oppermann 	/*
62256a4e45aSAndre Oppermann 	 * The mbuf is initialized later.  The caller has the
623fcf90618SGleb Smirnoff 	 * responsibility to set up any MAC labels too.
62456a4e45aSAndre Oppermann 	 */
62556a4e45aSAndre Oppermann 	if (type == MT_NOINIT)
62656a4e45aSAndre Oppermann 		return (0);
62756a4e45aSAndre Oppermann 
628afb295ccSAndre Oppermann 	m = (struct mbuf *)mem;
629afb295ccSAndre Oppermann 	flags = args->flags;
630fddd4f62SNavdeep Parhar 	MPASS((flags & M_NOFREE) == 0);
631afb295ccSAndre Oppermann 
632b4b12e52SGleb Smirnoff 	error = m_init(m, how, type, flags);
633afb295ccSAndre Oppermann 
634b23f72e9SBrian Feldman 	return (error);
635099a0e58SBosko Milekic }
636099a0e58SBosko Milekic 
637099a0e58SBosko Milekic /*
63856a4e45aSAndre Oppermann  * The Mbuf master zone destructor.
639099a0e58SBosko Milekic  */
640099a0e58SBosko Milekic static void
641099a0e58SBosko Milekic mb_dtor_mbuf(void *mem, int size, void *arg)
642099a0e58SBosko Milekic {
643099a0e58SBosko Milekic 	struct mbuf *m;
644629b9e08SKip Macy 	unsigned long flags;
645099a0e58SBosko Milekic 
646099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
647629b9e08SKip Macy 	flags = (unsigned long)arg;
648629b9e08SKip Macy 
649a9fa76f2SNavdeep Parhar 	KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__));
6504c7070dbSScott Long 	if (!(flags & MB_DTOR_SKIP) && (m->m_flags & M_PKTHDR) && !SLIST_EMPTY(&m->m_pkthdr.tags))
651099a0e58SBosko Milekic 		m_tag_delete_chain(m, NULL);
652121f0509SMike Silbersack #ifdef INVARIANTS
653121f0509SMike Silbersack 	trash_dtor(mem, size, arg);
654121f0509SMike Silbersack #endif
655099a0e58SBosko Milekic }
656099a0e58SBosko Milekic 
65756a4e45aSAndre Oppermann /*
65856a4e45aSAndre Oppermann  * The Mbuf Packet zone destructor.
65956a4e45aSAndre Oppermann  */
660099a0e58SBosko Milekic static void
661099a0e58SBosko Milekic mb_dtor_pack(void *mem, int size, void *arg)
662099a0e58SBosko Milekic {
663099a0e58SBosko Milekic 	struct mbuf *m;
664099a0e58SBosko Milekic 
665099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
666099a0e58SBosko Milekic 	if ((m->m_flags & M_PKTHDR) != 0)
667099a0e58SBosko Milekic 		m_tag_delete_chain(m, NULL);
66856a4e45aSAndre Oppermann 
66956a4e45aSAndre Oppermann 	/* Make sure we've got a clean cluster back. */
67056a4e45aSAndre Oppermann 	KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
67156a4e45aSAndre Oppermann 	KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
67256a4e45aSAndre Oppermann 	KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
673cf827063SPoul-Henning Kamp 	KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__));
674cf827063SPoul-Henning Kamp 	KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__));
67556a4e45aSAndre Oppermann 	KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
67649d46b61SGleb Smirnoff 	KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
677121f0509SMike Silbersack #ifdef INVARIANTS
678121f0509SMike Silbersack 	trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
679121f0509SMike Silbersack #endif
6806c125b8dSMohan Srinivasan 	/*
681ef44c8d2SDavid E. O'Brien 	 * If there are processes blocked on zone_clust, waiting for pages
682ef44c8d2SDavid E. O'Brien 	 * to be freed up, * cause them to be woken up by draining the
683ef44c8d2SDavid E. O'Brien 	 * packet zone.  We are exposed to a race here * (in the check for
684ef44c8d2SDavid E. O'Brien 	 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
685ef44c8d2SDavid E. O'Brien 	 * is deliberate. We don't want to acquire the zone lock for every
686ef44c8d2SDavid E. O'Brien 	 * mbuf free.
6876c125b8dSMohan Srinivasan 	 */
6886c125b8dSMohan Srinivasan 	if (uma_zone_exhausted_nolock(zone_clust))
6896c125b8dSMohan Srinivasan 		zone_drain(zone_pack);
690099a0e58SBosko Milekic }
691099a0e58SBosko Milekic 
692099a0e58SBosko Milekic /*
693ec63cb90SAndre Oppermann  * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
694099a0e58SBosko Milekic  *
695099a0e58SBosko Milekic  * Here the 'arg' pointer points to the Mbuf which we
69656a4e45aSAndre Oppermann  * are configuring cluster storage for.  If 'arg' is
69756a4e45aSAndre Oppermann  * empty we allocate just the cluster without setting
69856a4e45aSAndre Oppermann  * the mbuf to it.  See mbuf.h.
699099a0e58SBosko Milekic  */
700b23f72e9SBrian Feldman static int
701b23f72e9SBrian Feldman mb_ctor_clust(void *mem, int size, void *arg, int how)
702099a0e58SBosko Milekic {
703099a0e58SBosko Milekic 	struct mbuf *m;
704099a0e58SBosko Milekic 
705121f0509SMike Silbersack #ifdef INVARIANTS
706121f0509SMike Silbersack 	trash_ctor(mem, size, arg, how);
707121f0509SMike Silbersack #endif
7080f4d9d04SKip Macy 	m = (struct mbuf *)arg;
7090f4d9d04SKip Macy 	if (m != NULL) {
710e8fd18f3SGleb Smirnoff 		m->m_ext.ext_buf = (char *)mem;
711099a0e58SBosko Milekic 		m->m_data = m->m_ext.ext_buf;
712099a0e58SBosko Milekic 		m->m_flags |= M_EXT;
713099a0e58SBosko Milekic 		m->m_ext.ext_free = NULL;
714cf827063SPoul-Henning Kamp 		m->m_ext.ext_arg1 = NULL;
715cf827063SPoul-Henning Kamp 		m->m_ext.ext_arg2 = NULL;
71656a4e45aSAndre Oppermann 		m->m_ext.ext_size = size;
71756a5f52eSGleb Smirnoff 		m->m_ext.ext_type = m_gettype(size);
71856a5f52eSGleb Smirnoff 		m->m_ext.ext_flags = EXT_FLAG_EMBREF;
71956a5f52eSGleb Smirnoff 		m->m_ext.ext_count = 1;
72056a4e45aSAndre Oppermann 	}
7210f4d9d04SKip Macy 
722b23f72e9SBrian Feldman 	return (0);
723099a0e58SBosko Milekic }
724099a0e58SBosko Milekic 
72556a4e45aSAndre Oppermann /*
726099a0e58SBosko Milekic  * The Packet secondary zone's init routine, executed on the
72756a4e45aSAndre Oppermann  * object's transition from mbuf keg slab to zone cache.
728099a0e58SBosko Milekic  */
729b23f72e9SBrian Feldman static int
73056a4e45aSAndre Oppermann mb_zinit_pack(void *mem, int size, int how)
731099a0e58SBosko Milekic {
732099a0e58SBosko Milekic 	struct mbuf *m;
733099a0e58SBosko Milekic 
73456a4e45aSAndre Oppermann 	m = (struct mbuf *)mem;		/* m is virgin. */
735a7bd90efSAndre Oppermann 	if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
736a7bd90efSAndre Oppermann 	    m->m_ext.ext_buf == NULL)
737b23f72e9SBrian Feldman 		return (ENOMEM);
738cd5bb63bSAndre Oppermann 	m->m_ext.ext_type = EXT_PACKET;	/* Override. */
739121f0509SMike Silbersack #ifdef INVARIANTS
740121f0509SMike Silbersack 	trash_init(m->m_ext.ext_buf, MCLBYTES, how);
741121f0509SMike Silbersack #endif
742b23f72e9SBrian Feldman 	return (0);
743099a0e58SBosko Milekic }
744099a0e58SBosko Milekic 
745099a0e58SBosko Milekic /*
746099a0e58SBosko Milekic  * The Packet secondary zone's fini routine, executed on the
747099a0e58SBosko Milekic  * object's transition from zone cache to keg slab.
748099a0e58SBosko Milekic  */
749099a0e58SBosko Milekic static void
75056a4e45aSAndre Oppermann mb_zfini_pack(void *mem, int size)
751099a0e58SBosko Milekic {
752099a0e58SBosko Milekic 	struct mbuf *m;
753099a0e58SBosko Milekic 
754099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
755121f0509SMike Silbersack #ifdef INVARIANTS
756121f0509SMike Silbersack 	trash_fini(m->m_ext.ext_buf, MCLBYTES);
757121f0509SMike Silbersack #endif
758099a0e58SBosko Milekic 	uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
759a7b844d2SMike Silbersack #ifdef INVARIANTS
760a7b844d2SMike Silbersack 	trash_dtor(mem, size, NULL);
761a7b844d2SMike Silbersack #endif
762099a0e58SBosko Milekic }
763099a0e58SBosko Milekic 
764099a0e58SBosko Milekic /*
765099a0e58SBosko Milekic  * The "packet" keg constructor.
766099a0e58SBosko Milekic  */
767b23f72e9SBrian Feldman static int
768b23f72e9SBrian Feldman mb_ctor_pack(void *mem, int size, void *arg, int how)
769099a0e58SBosko Milekic {
770099a0e58SBosko Milekic 	struct mbuf *m;
771099a0e58SBosko Milekic 	struct mb_args *args;
772ce28636bSAndre Oppermann 	int error, flags;
773099a0e58SBosko Milekic 	short type;
774099a0e58SBosko Milekic 
775099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
776099a0e58SBosko Milekic 	args = (struct mb_args *)arg;
777099a0e58SBosko Milekic 	flags = args->flags;
778099a0e58SBosko Milekic 	type = args->type;
779fddd4f62SNavdeep Parhar 	MPASS((flags & M_NOFREE) == 0);
780099a0e58SBosko Milekic 
781121f0509SMike Silbersack #ifdef INVARIANTS
782121f0509SMike Silbersack 	trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
783121f0509SMike Silbersack #endif
784099a0e58SBosko Milekic 
785b4b12e52SGleb Smirnoff 	error = m_init(m, how, type, flags);
786afb295ccSAndre Oppermann 
78756a4e45aSAndre Oppermann 	/* m_ext is already initialized. */
788afb295ccSAndre Oppermann 	m->m_data = m->m_ext.ext_buf;
789afb295ccSAndre Oppermann  	m->m_flags = (flags | M_EXT);
79056a4e45aSAndre Oppermann 
791afb295ccSAndre Oppermann 	return (error);
792099a0e58SBosko Milekic }
793099a0e58SBosko Milekic 
794099a0e58SBosko Milekic /*
795e60b2fcbSGleb Smirnoff  * This is the protocol drain routine.  Called by UMA whenever any of the
796e60b2fcbSGleb Smirnoff  * mbuf zones is closed to its limit.
797099a0e58SBosko Milekic  *
798099a0e58SBosko Milekic  * No locks should be held when this is called.  The drain routines have to
799099a0e58SBosko Milekic  * presently acquire some locks which raises the possibility of lock order
800099a0e58SBosko Milekic  * reversal.
801099a0e58SBosko Milekic  */
802099a0e58SBosko Milekic static void
803e60b2fcbSGleb Smirnoff mb_reclaim(uma_zone_t zone __unused, int pending __unused)
804099a0e58SBosko Milekic {
805099a0e58SBosko Milekic 	struct domain *dp;
806099a0e58SBosko Milekic 	struct protosw *pr;
807099a0e58SBosko Milekic 
808e60b2fcbSGleb Smirnoff 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, __func__);
809099a0e58SBosko Milekic 
810099a0e58SBosko Milekic 	for (dp = domains; dp != NULL; dp = dp->dom_next)
811099a0e58SBosko Milekic 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
812099a0e58SBosko Milekic 			if (pr->pr_drain != NULL)
813099a0e58SBosko Milekic 				(*pr->pr_drain)();
814099a0e58SBosko Milekic }
8155e4bc63bSGleb Smirnoff 
8165e4bc63bSGleb Smirnoff /*
8175e4bc63bSGleb Smirnoff  * Clean up after mbufs with M_EXT storage attached to them if the
8185e4bc63bSGleb Smirnoff  * reference count hits 1.
8195e4bc63bSGleb Smirnoff  */
8205e4bc63bSGleb Smirnoff void
8215e4bc63bSGleb Smirnoff mb_free_ext(struct mbuf *m)
8225e4bc63bSGleb Smirnoff {
82356a5f52eSGleb Smirnoff 	volatile u_int *refcnt;
82456a5f52eSGleb Smirnoff 	struct mbuf *mref;
8255e4bc63bSGleb Smirnoff 	int freembuf;
8265e4bc63bSGleb Smirnoff 
8275e4bc63bSGleb Smirnoff 	KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m));
8285e4bc63bSGleb Smirnoff 
82956a5f52eSGleb Smirnoff 	/* See if this is the mbuf that holds the embedded refcount. */
83056a5f52eSGleb Smirnoff 	if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
83156a5f52eSGleb Smirnoff 		refcnt = &m->m_ext.ext_count;
83256a5f52eSGleb Smirnoff 		mref = m;
83356a5f52eSGleb Smirnoff 	} else {
83456a5f52eSGleb Smirnoff 		KASSERT(m->m_ext.ext_cnt != NULL,
83556a5f52eSGleb Smirnoff 		    ("%s: no refcounting pointer on %p", __func__, m));
83656a5f52eSGleb Smirnoff 		refcnt = m->m_ext.ext_cnt;
83756a5f52eSGleb Smirnoff 		mref = __containerof(refcnt, struct mbuf, m_ext.ext_count);
83856a5f52eSGleb Smirnoff 	}
83956a5f52eSGleb Smirnoff 
8405e4bc63bSGleb Smirnoff 	/*
84156a5f52eSGleb Smirnoff 	 * Check if the header is embedded in the cluster.  It is
84256a5f52eSGleb Smirnoff 	 * important that we can't touch any of the mbuf fields
84356a5f52eSGleb Smirnoff 	 * after we have freed the external storage, since mbuf
84417cd649fSGleb Smirnoff 	 * could have been embedded in it.  For now, the mbufs
84517cd649fSGleb Smirnoff 	 * embedded into the cluster are always of type EXT_EXTREF,
84617cd649fSGleb Smirnoff 	 * and for this type we won't free the mref.
8475e4bc63bSGleb Smirnoff 	 */
84817cd649fSGleb Smirnoff 	if (m->m_flags & M_NOFREE) {
84917cd649fSGleb Smirnoff 		freembuf = 0;
850*eec189c7SGleb Smirnoff 		KASSERT(m->m_ext.ext_type == EXT_EXTREF ||
851*eec189c7SGleb Smirnoff 		    m->m_ext.ext_type == EXT_RXRING,
85217cd649fSGleb Smirnoff 		    ("%s: no-free mbuf %p has wrong type", __func__, m));
85317cd649fSGleb Smirnoff 	} else
85417cd649fSGleb Smirnoff 		freembuf = 1;
8555e4bc63bSGleb Smirnoff 
85656a5f52eSGleb Smirnoff 	/* Free attached storage if this mbuf is the only reference to it. */
85756a5f52eSGleb Smirnoff 	if (*refcnt == 1 || atomic_fetchadd_int(refcnt, -1) == 1) {
8585e4bc63bSGleb Smirnoff 		switch (m->m_ext.ext_type) {
85956a5f52eSGleb Smirnoff 		case EXT_PACKET:
86056a5f52eSGleb Smirnoff 			/* The packet zone is special. */
86156a5f52eSGleb Smirnoff 			if (*refcnt == 0)
86256a5f52eSGleb Smirnoff 				*refcnt = 1;
86356a5f52eSGleb Smirnoff 			uma_zfree(zone_pack, mref);
8645e4bc63bSGleb Smirnoff 			break;
8655e4bc63bSGleb Smirnoff 		case EXT_CLUSTER:
8665e4bc63bSGleb Smirnoff 			uma_zfree(zone_clust, m->m_ext.ext_buf);
86756a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
8685e4bc63bSGleb Smirnoff 			break;
8695e4bc63bSGleb Smirnoff 		case EXT_JUMBOP:
8705e4bc63bSGleb Smirnoff 			uma_zfree(zone_jumbop, m->m_ext.ext_buf);
87156a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
8725e4bc63bSGleb Smirnoff 			break;
8735e4bc63bSGleb Smirnoff 		case EXT_JUMBO9:
8745e4bc63bSGleb Smirnoff 			uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
87556a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
8765e4bc63bSGleb Smirnoff 			break;
8775e4bc63bSGleb Smirnoff 		case EXT_JUMBO16:
8785e4bc63bSGleb Smirnoff 			uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
87956a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
88056a5f52eSGleb Smirnoff 			break;
88156a5f52eSGleb Smirnoff 		case EXT_SFBUF:
8825e4bc63bSGleb Smirnoff 		case EXT_NET_DRV:
8835e4bc63bSGleb Smirnoff 		case EXT_MOD_TYPE:
8845e4bc63bSGleb Smirnoff 		case EXT_DISPOSABLE:
88507e87a1dSGleb Smirnoff 			KASSERT(mref->m_ext.ext_free != NULL,
8860ea37a86SGleb Smirnoff 			    ("%s: ext_free not set", __func__));
88707e87a1dSGleb Smirnoff 			mref->m_ext.ext_free(mref);
88856a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
8890ea37a86SGleb Smirnoff 			break;
8905e4bc63bSGleb Smirnoff 		case EXT_EXTREF:
8915e4bc63bSGleb Smirnoff 			KASSERT(m->m_ext.ext_free != NULL,
8925e4bc63bSGleb Smirnoff 			    ("%s: ext_free not set", __func__));
893e8fd18f3SGleb Smirnoff 			m->m_ext.ext_free(m);
8945e4bc63bSGleb Smirnoff 			break;
895*eec189c7SGleb Smirnoff 		case EXT_RXRING:
896*eec189c7SGleb Smirnoff 			KASSERT(m->m_ext.ext_free == NULL,
897*eec189c7SGleb Smirnoff 			    ("%s: ext_free is set", __func__));
898*eec189c7SGleb Smirnoff 			break;
8995e4bc63bSGleb Smirnoff 		default:
9005e4bc63bSGleb Smirnoff 			KASSERT(m->m_ext.ext_type == 0,
9015e4bc63bSGleb Smirnoff 			    ("%s: unknown ext_type", __func__));
9025e4bc63bSGleb Smirnoff 		}
9035e4bc63bSGleb Smirnoff 	}
9045e4bc63bSGleb Smirnoff 
90556a5f52eSGleb Smirnoff 	if (freembuf && m != mref)
9065e4bc63bSGleb Smirnoff 		uma_zfree(zone_mbuf, m);
9075e4bc63bSGleb Smirnoff }
9085e4bc63bSGleb Smirnoff 
9095e4bc63bSGleb Smirnoff /*
9105e4bc63bSGleb Smirnoff  * Official mbuf(9) allocation KPI for stack and drivers:
9115e4bc63bSGleb Smirnoff  *
9125e4bc63bSGleb Smirnoff  * m_get()	- a single mbuf without any attachments, sys/mbuf.h.
9135e4bc63bSGleb Smirnoff  * m_gethdr()	- a single mbuf initialized as M_PKTHDR, sys/mbuf.h.
9145e4bc63bSGleb Smirnoff  * m_getcl()	- an mbuf + 2k cluster, sys/mbuf.h.
9155e4bc63bSGleb Smirnoff  * m_clget()	- attach cluster to already allocated mbuf.
9165e4bc63bSGleb Smirnoff  * m_cljget()	- attach jumbo cluster to already allocated mbuf.
9175e4bc63bSGleb Smirnoff  * m_get2()	- allocate minimum mbuf that would fit size argument.
9185e4bc63bSGleb Smirnoff  * m_getm2()	- allocate a chain of mbufs/clusters.
9195e4bc63bSGleb Smirnoff  * m_extadd()	- attach external cluster to mbuf.
9205e4bc63bSGleb Smirnoff  *
9215e4bc63bSGleb Smirnoff  * m_free()	- free single mbuf with its tags and ext, sys/mbuf.h.
9225e4bc63bSGleb Smirnoff  * m_freem()	- free chain of mbufs.
9235e4bc63bSGleb Smirnoff  */
9245e4bc63bSGleb Smirnoff 
9255e4bc63bSGleb Smirnoff int
9265e4bc63bSGleb Smirnoff m_clget(struct mbuf *m, int how)
9275e4bc63bSGleb Smirnoff {
9285e4bc63bSGleb Smirnoff 
9295e4bc63bSGleb Smirnoff 	KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
9305e4bc63bSGleb Smirnoff 	    __func__, m));
9315e4bc63bSGleb Smirnoff 	m->m_ext.ext_buf = (char *)NULL;
9325e4bc63bSGleb Smirnoff 	uma_zalloc_arg(zone_clust, m, how);
9335e4bc63bSGleb Smirnoff 	/*
9345e4bc63bSGleb Smirnoff 	 * On a cluster allocation failure, drain the packet zone and retry,
9355e4bc63bSGleb Smirnoff 	 * we might be able to loosen a few clusters up on the drain.
9365e4bc63bSGleb Smirnoff 	 */
9375e4bc63bSGleb Smirnoff 	if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) {
9385e4bc63bSGleb Smirnoff 		zone_drain(zone_pack);
9395e4bc63bSGleb Smirnoff 		uma_zalloc_arg(zone_clust, m, how);
9405e4bc63bSGleb Smirnoff 	}
941480f4e94SGeorge V. Neville-Neil 	MBUF_PROBE2(m__clget, m, how);
9425e4bc63bSGleb Smirnoff 	return (m->m_flags & M_EXT);
9435e4bc63bSGleb Smirnoff }
9445e4bc63bSGleb Smirnoff 
9455e4bc63bSGleb Smirnoff /*
9465e4bc63bSGleb Smirnoff  * m_cljget() is different from m_clget() as it can allocate clusters without
9475e4bc63bSGleb Smirnoff  * attaching them to an mbuf.  In that case the return value is the pointer
9485e4bc63bSGleb Smirnoff  * to the cluster of the requested size.  If an mbuf was specified, it gets
9495e4bc63bSGleb Smirnoff  * the cluster attached to it and the return value can be safely ignored.
9505e4bc63bSGleb Smirnoff  * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
9515e4bc63bSGleb Smirnoff  */
9525e4bc63bSGleb Smirnoff void *
9535e4bc63bSGleb Smirnoff m_cljget(struct mbuf *m, int how, int size)
9545e4bc63bSGleb Smirnoff {
9555e4bc63bSGleb Smirnoff 	uma_zone_t zone;
956480f4e94SGeorge V. Neville-Neil 	void *retval;
9575e4bc63bSGleb Smirnoff 
9585e4bc63bSGleb Smirnoff 	if (m != NULL) {
9595e4bc63bSGleb Smirnoff 		KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
9605e4bc63bSGleb Smirnoff 		    __func__, m));
9615e4bc63bSGleb Smirnoff 		m->m_ext.ext_buf = NULL;
9625e4bc63bSGleb Smirnoff 	}
9635e4bc63bSGleb Smirnoff 
9645e4bc63bSGleb Smirnoff 	zone = m_getzone(size);
965480f4e94SGeorge V. Neville-Neil 	retval = uma_zalloc_arg(zone, m, how);
966480f4e94SGeorge V. Neville-Neil 
967480f4e94SGeorge V. Neville-Neil 	MBUF_PROBE4(m__cljget, m, how, size, retval);
968480f4e94SGeorge V. Neville-Neil 
969480f4e94SGeorge V. Neville-Neil 	return (retval);
9705e4bc63bSGleb Smirnoff }
9715e4bc63bSGleb Smirnoff 
9725e4bc63bSGleb Smirnoff /*
9735e4bc63bSGleb Smirnoff  * m_get2() allocates minimum mbuf that would fit "size" argument.
9745e4bc63bSGleb Smirnoff  */
9755e4bc63bSGleb Smirnoff struct mbuf *
9765e4bc63bSGleb Smirnoff m_get2(int size, int how, short type, int flags)
9775e4bc63bSGleb Smirnoff {
9785e4bc63bSGleb Smirnoff 	struct mb_args args;
9795e4bc63bSGleb Smirnoff 	struct mbuf *m, *n;
9805e4bc63bSGleb Smirnoff 
9815e4bc63bSGleb Smirnoff 	args.flags = flags;
9825e4bc63bSGleb Smirnoff 	args.type = type;
9835e4bc63bSGleb Smirnoff 
9845e4bc63bSGleb Smirnoff 	if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0))
9855e4bc63bSGleb Smirnoff 		return (uma_zalloc_arg(zone_mbuf, &args, how));
9865e4bc63bSGleb Smirnoff 	if (size <= MCLBYTES)
9875e4bc63bSGleb Smirnoff 		return (uma_zalloc_arg(zone_pack, &args, how));
9885e4bc63bSGleb Smirnoff 
9895e4bc63bSGleb Smirnoff 	if (size > MJUMPAGESIZE)
9905e4bc63bSGleb Smirnoff 		return (NULL);
9915e4bc63bSGleb Smirnoff 
9925e4bc63bSGleb Smirnoff 	m = uma_zalloc_arg(zone_mbuf, &args, how);
9935e4bc63bSGleb Smirnoff 	if (m == NULL)
9945e4bc63bSGleb Smirnoff 		return (NULL);
9955e4bc63bSGleb Smirnoff 
9965e4bc63bSGleb Smirnoff 	n = uma_zalloc_arg(zone_jumbop, m, how);
9975e4bc63bSGleb Smirnoff 	if (n == NULL) {
9985e4bc63bSGleb Smirnoff 		uma_zfree(zone_mbuf, m);
9995e4bc63bSGleb Smirnoff 		return (NULL);
10005e4bc63bSGleb Smirnoff 	}
10015e4bc63bSGleb Smirnoff 
10025e4bc63bSGleb Smirnoff 	return (m);
10035e4bc63bSGleb Smirnoff }
10045e4bc63bSGleb Smirnoff 
10055e4bc63bSGleb Smirnoff /*
10065e4bc63bSGleb Smirnoff  * m_getjcl() returns an mbuf with a cluster of the specified size attached.
10075e4bc63bSGleb Smirnoff  * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
10085e4bc63bSGleb Smirnoff  */
10095e4bc63bSGleb Smirnoff struct mbuf *
10105e4bc63bSGleb Smirnoff m_getjcl(int how, short type, int flags, int size)
10115e4bc63bSGleb Smirnoff {
10125e4bc63bSGleb Smirnoff 	struct mb_args args;
10135e4bc63bSGleb Smirnoff 	struct mbuf *m, *n;
10145e4bc63bSGleb Smirnoff 	uma_zone_t zone;
10155e4bc63bSGleb Smirnoff 
10165e4bc63bSGleb Smirnoff 	if (size == MCLBYTES)
10175e4bc63bSGleb Smirnoff 		return m_getcl(how, type, flags);
10185e4bc63bSGleb Smirnoff 
10195e4bc63bSGleb Smirnoff 	args.flags = flags;
10205e4bc63bSGleb Smirnoff 	args.type = type;
10215e4bc63bSGleb Smirnoff 
10225e4bc63bSGleb Smirnoff 	m = uma_zalloc_arg(zone_mbuf, &args, how);
10235e4bc63bSGleb Smirnoff 	if (m == NULL)
10245e4bc63bSGleb Smirnoff 		return (NULL);
10255e4bc63bSGleb Smirnoff 
10265e4bc63bSGleb Smirnoff 	zone = m_getzone(size);
10275e4bc63bSGleb Smirnoff 	n = uma_zalloc_arg(zone, m, how);
10285e4bc63bSGleb Smirnoff 	if (n == NULL) {
10295e4bc63bSGleb Smirnoff 		uma_zfree(zone_mbuf, m);
10305e4bc63bSGleb Smirnoff 		return (NULL);
10315e4bc63bSGleb Smirnoff 	}
10325e4bc63bSGleb Smirnoff 	return (m);
10335e4bc63bSGleb Smirnoff }
10345e4bc63bSGleb Smirnoff 
10355e4bc63bSGleb Smirnoff /*
10365e4bc63bSGleb Smirnoff  * Allocate a given length worth of mbufs and/or clusters (whatever fits
10375e4bc63bSGleb Smirnoff  * best) and return a pointer to the top of the allocated chain.  If an
10385e4bc63bSGleb Smirnoff  * existing mbuf chain is provided, then we will append the new chain
103958c43838SAndriy Voskoboinyk  * to the existing one and return a pointer to the provided mbuf.
10405e4bc63bSGleb Smirnoff  */
10415e4bc63bSGleb Smirnoff struct mbuf *
10425e4bc63bSGleb Smirnoff m_getm2(struct mbuf *m, int len, int how, short type, int flags)
10435e4bc63bSGleb Smirnoff {
10445e4bc63bSGleb Smirnoff 	struct mbuf *mb, *nm = NULL, *mtail = NULL;
10455e4bc63bSGleb Smirnoff 
10465e4bc63bSGleb Smirnoff 	KASSERT(len >= 0, ("%s: len is < 0", __func__));
10475e4bc63bSGleb Smirnoff 
10485e4bc63bSGleb Smirnoff 	/* Validate flags. */
10495e4bc63bSGleb Smirnoff 	flags &= (M_PKTHDR | M_EOR);
10505e4bc63bSGleb Smirnoff 
10515e4bc63bSGleb Smirnoff 	/* Packet header mbuf must be first in chain. */
10525e4bc63bSGleb Smirnoff 	if ((flags & M_PKTHDR) && m != NULL)
10535e4bc63bSGleb Smirnoff 		flags &= ~M_PKTHDR;
10545e4bc63bSGleb Smirnoff 
10555e4bc63bSGleb Smirnoff 	/* Loop and append maximum sized mbufs to the chain tail. */
10565e4bc63bSGleb Smirnoff 	while (len > 0) {
10575e4bc63bSGleb Smirnoff 		if (len > MCLBYTES)
10585e4bc63bSGleb Smirnoff 			mb = m_getjcl(how, type, (flags & M_PKTHDR),
10595e4bc63bSGleb Smirnoff 			    MJUMPAGESIZE);
10605e4bc63bSGleb Smirnoff 		else if (len >= MINCLSIZE)
10615e4bc63bSGleb Smirnoff 			mb = m_getcl(how, type, (flags & M_PKTHDR));
10625e4bc63bSGleb Smirnoff 		else if (flags & M_PKTHDR)
10635e4bc63bSGleb Smirnoff 			mb = m_gethdr(how, type);
10645e4bc63bSGleb Smirnoff 		else
10655e4bc63bSGleb Smirnoff 			mb = m_get(how, type);
10665e4bc63bSGleb Smirnoff 
10675e4bc63bSGleb Smirnoff 		/* Fail the whole operation if one mbuf can't be allocated. */
10685e4bc63bSGleb Smirnoff 		if (mb == NULL) {
10695e4bc63bSGleb Smirnoff 			if (nm != NULL)
10705e4bc63bSGleb Smirnoff 				m_freem(nm);
10715e4bc63bSGleb Smirnoff 			return (NULL);
10725e4bc63bSGleb Smirnoff 		}
10735e4bc63bSGleb Smirnoff 
10745e4bc63bSGleb Smirnoff 		/* Book keeping. */
10755e4bc63bSGleb Smirnoff 		len -= M_SIZE(mb);
10765e4bc63bSGleb Smirnoff 		if (mtail != NULL)
10775e4bc63bSGleb Smirnoff 			mtail->m_next = mb;
10785e4bc63bSGleb Smirnoff 		else
10795e4bc63bSGleb Smirnoff 			nm = mb;
10805e4bc63bSGleb Smirnoff 		mtail = mb;
10815e4bc63bSGleb Smirnoff 		flags &= ~M_PKTHDR;	/* Only valid on the first mbuf. */
10825e4bc63bSGleb Smirnoff 	}
10835e4bc63bSGleb Smirnoff 	if (flags & M_EOR)
10845e4bc63bSGleb Smirnoff 		mtail->m_flags |= M_EOR;  /* Only valid on the last mbuf. */
10855e4bc63bSGleb Smirnoff 
10865e4bc63bSGleb Smirnoff 	/* If mbuf was supplied, append new chain to the end of it. */
10875e4bc63bSGleb Smirnoff 	if (m != NULL) {
10885e4bc63bSGleb Smirnoff 		for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
10895e4bc63bSGleb Smirnoff 			;
10905e4bc63bSGleb Smirnoff 		mtail->m_next = nm;
10915e4bc63bSGleb Smirnoff 		mtail->m_flags &= ~M_EOR;
10925e4bc63bSGleb Smirnoff 	} else
10935e4bc63bSGleb Smirnoff 		m = nm;
10945e4bc63bSGleb Smirnoff 
10955e4bc63bSGleb Smirnoff 	return (m);
10965e4bc63bSGleb Smirnoff }
10975e4bc63bSGleb Smirnoff 
10985e4bc63bSGleb Smirnoff /*-
10995e4bc63bSGleb Smirnoff  * Configure a provided mbuf to refer to the provided external storage
110056a5f52eSGleb Smirnoff  * buffer and setup a reference count for said buffer.
11015e4bc63bSGleb Smirnoff  *
11025e4bc63bSGleb Smirnoff  * Arguments:
11035e4bc63bSGleb Smirnoff  *    mb     The existing mbuf to which to attach the provided buffer.
11045e4bc63bSGleb Smirnoff  *    buf    The address of the provided external storage buffer.
11055e4bc63bSGleb Smirnoff  *    size   The size of the provided buffer.
11065e4bc63bSGleb Smirnoff  *    freef  A pointer to a routine that is responsible for freeing the
11075e4bc63bSGleb Smirnoff  *           provided external storage buffer.
11085e4bc63bSGleb Smirnoff  *    args   A pointer to an argument structure (of any type) to be passed
11095e4bc63bSGleb Smirnoff  *           to the provided freef routine (may be NULL).
11105e4bc63bSGleb Smirnoff  *    flags  Any other flags to be passed to the provided mbuf.
11115e4bc63bSGleb Smirnoff  *    type   The type that the external storage buffer should be
11125e4bc63bSGleb Smirnoff  *           labeled with.
11135e4bc63bSGleb Smirnoff  *
11145e4bc63bSGleb Smirnoff  * Returns:
11155e4bc63bSGleb Smirnoff  *    Nothing.
11165e4bc63bSGleb Smirnoff  */
111756a5f52eSGleb Smirnoff void
1118e8fd18f3SGleb Smirnoff m_extadd(struct mbuf *mb, char *buf, u_int size, m_ext_free_t freef,
1119e8fd18f3SGleb Smirnoff     void *arg1, void *arg2, int flags, int type)
11205e4bc63bSGleb Smirnoff {
112156a5f52eSGleb Smirnoff 
11225e4bc63bSGleb Smirnoff 	KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
11235e4bc63bSGleb Smirnoff 
11245e4bc63bSGleb Smirnoff 	mb->m_flags |= (M_EXT | flags);
11255e4bc63bSGleb Smirnoff 	mb->m_ext.ext_buf = buf;
11265e4bc63bSGleb Smirnoff 	mb->m_data = mb->m_ext.ext_buf;
11275e4bc63bSGleb Smirnoff 	mb->m_ext.ext_size = size;
11285e4bc63bSGleb Smirnoff 	mb->m_ext.ext_free = freef;
11295e4bc63bSGleb Smirnoff 	mb->m_ext.ext_arg1 = arg1;
11305e4bc63bSGleb Smirnoff 	mb->m_ext.ext_arg2 = arg2;
11315e4bc63bSGleb Smirnoff 	mb->m_ext.ext_type = type;
11325e4bc63bSGleb Smirnoff 
113356a5f52eSGleb Smirnoff 	if (type != EXT_EXTREF) {
113456a5f52eSGleb Smirnoff 		mb->m_ext.ext_count = 1;
113556a5f52eSGleb Smirnoff 		mb->m_ext.ext_flags = EXT_FLAG_EMBREF;
113656a5f52eSGleb Smirnoff 	} else
113756a5f52eSGleb Smirnoff 		mb->m_ext.ext_flags = 0;
11385e4bc63bSGleb Smirnoff }
11395e4bc63bSGleb Smirnoff 
11405e4bc63bSGleb Smirnoff /*
11415e4bc63bSGleb Smirnoff  * Free an entire chain of mbufs and associated external buffers, if
11425e4bc63bSGleb Smirnoff  * applicable.
11435e4bc63bSGleb Smirnoff  */
11445e4bc63bSGleb Smirnoff void
11455e4bc63bSGleb Smirnoff m_freem(struct mbuf *mb)
11465e4bc63bSGleb Smirnoff {
11475e4bc63bSGleb Smirnoff 
1148c8f59118SGleb Smirnoff 	MBUF_PROBE1(m__freem, mb);
11495e4bc63bSGleb Smirnoff 	while (mb != NULL)
11505e4bc63bSGleb Smirnoff 		mb = m_free(mb);
11515e4bc63bSGleb Smirnoff }
1152