xref: /freebsd/sys/kern/kern_mbuf.c (revision 5475ca5acae384a2242b62b90f459f6521c6d72a)
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>
36099a0e58SBosko Milekic #include <sys/malloc.h>
3754503a13SJonathan T. Looney #include <sys/types.h>
38099a0e58SBosko Milekic #include <sys/systm.h>
39099a0e58SBosko Milekic #include <sys/mbuf.h>
40099a0e58SBosko Milekic #include <sys/domain.h>
41099a0e58SBosko Milekic #include <sys/eventhandler.h>
42099a0e58SBosko Milekic #include <sys/kernel.h>
43*5475ca5aSMark Johnston #include <sys/limits.h>
4454503a13SJonathan T. Looney #include <sys/lock.h>
4554503a13SJonathan T. Looney #include <sys/mutex.h>
46099a0e58SBosko Milekic #include <sys/protosw.h>
47099a0e58SBosko Milekic #include <sys/smp.h>
48099a0e58SBosko Milekic #include <sys/sysctl.h>
49099a0e58SBosko Milekic 
50099a0e58SBosko Milekic #include <vm/vm.h>
51c45c0034SAlan Cox #include <vm/vm_extern.h>
52c45c0034SAlan Cox #include <vm/vm_kern.h>
53099a0e58SBosko Milekic #include <vm/vm_page.h>
5437140716SAndre Oppermann #include <vm/vm_map.h>
55099a0e58SBosko Milekic #include <vm/uma.h>
56121f0509SMike Silbersack #include <vm/uma_dbg.h>
57099a0e58SBosko Milekic 
58099a0e58SBosko Milekic /*
59099a0e58SBosko Milekic  * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
60099a0e58SBosko Milekic  * Zones.
61099a0e58SBosko Milekic  *
62099a0e58SBosko Milekic  * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
63099a0e58SBosko Milekic  * Zone.  The Zone can be capped at kern.ipc.nmbclusters, if the
64099a0e58SBosko Milekic  * administrator so desires.
65099a0e58SBosko Milekic  *
66099a0e58SBosko Milekic  * Mbufs are allocated from a UMA Master Zone called the Mbuf
67099a0e58SBosko Milekic  * Zone.
68099a0e58SBosko Milekic  *
69099a0e58SBosko Milekic  * Additionally, FreeBSD provides a Packet Zone, which it
70099a0e58SBosko Milekic  * configures as a Secondary Zone to the Mbuf Master Zone,
71099a0e58SBosko Milekic  * thus sharing backend Slab kegs with the Mbuf Master Zone.
72099a0e58SBosko Milekic  *
73099a0e58SBosko Milekic  * Thus common-case allocations and locking are simplified:
74099a0e58SBosko Milekic  *
75099a0e58SBosko Milekic  *  m_clget()                m_getcl()
76099a0e58SBosko Milekic  *    |                         |
77099a0e58SBosko Milekic  *    |   .------------>[(Packet Cache)]    m_get(), m_gethdr()
78099a0e58SBosko Milekic  *    |   |             [     Packet   ]            |
79099a0e58SBosko Milekic  *  [(Cluster Cache)]   [    Secondary ]   [ (Mbuf Cache)     ]
80099a0e58SBosko Milekic  *  [ Cluster Zone  ]   [     Zone     ]   [ Mbuf Master Zone ]
81099a0e58SBosko Milekic  *        |                       \________         |
82099a0e58SBosko Milekic  *  [ Cluster Keg   ]                      \       /
83099a0e58SBosko Milekic  *        |	                         [ Mbuf Keg   ]
84099a0e58SBosko Milekic  *  [ Cluster Slabs ]                         |
85099a0e58SBosko Milekic  *        |                              [ Mbuf Slabs ]
86099a0e58SBosko Milekic  *         \____________(VM)_________________/
8756a4e45aSAndre Oppermann  *
8856a4e45aSAndre Oppermann  *
89fcf90618SGleb Smirnoff  * Whenever an object is allocated with uma_zalloc() out of
9056a4e45aSAndre Oppermann  * one of the Zones its _ctor_ function is executed.  The same
91fcf90618SGleb Smirnoff  * for any deallocation through uma_zfree() the _dtor_ function
9256a4e45aSAndre Oppermann  * is executed.
9356a4e45aSAndre Oppermann  *
9456a4e45aSAndre Oppermann  * Caches are per-CPU and are filled from the Master Zone.
9556a4e45aSAndre Oppermann  *
96fcf90618SGleb Smirnoff  * Whenever an object is allocated from the underlying global
9756a4e45aSAndre Oppermann  * memory pool it gets pre-initialized with the _zinit_ functions.
98e3043798SPedro F. Giffuni  * When the Keg's are overfull objects get decommissioned with
9956a4e45aSAndre Oppermann  * _zfini_ functions and free'd back to the global memory pool.
10056a4e45aSAndre Oppermann  *
101099a0e58SBosko Milekic  */
102099a0e58SBosko Milekic 
103ead46972SAndre Oppermann int nmbufs;			/* limits number of mbufs */
10456a4e45aSAndre Oppermann int nmbclusters;		/* limits number of mbuf clusters */
105ec63cb90SAndre Oppermann int nmbjumbop;			/* limits number of page size jumbo clusters */
10656a4e45aSAndre Oppermann int nmbjumbo9;			/* limits number of 9k jumbo clusters */
10756a4e45aSAndre Oppermann int nmbjumbo16;			/* limits number of 16k jumbo clusters */
108099a0e58SBosko Milekic 
109e0c00addSAndre Oppermann static quad_t maxmbufmem;	/* overall real memory limit for all mbufs */
110e0c00addSAndre Oppermann 
111af3b2549SHans Petter Selasky SYSCTL_QUAD(_kern_ipc, OID_AUTO, maxmbufmem, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &maxmbufmem, 0,
112b6f49c23SHiren Panchasara     "Maximum real memory allocatable to various mbuf types");
113e0c00addSAndre Oppermann 
11462938659SBjoern A. Zeeb /*
11537140716SAndre Oppermann  * tunable_mbinit() has to be run before any mbuf allocations are done.
11662938659SBjoern A. Zeeb  */
117099a0e58SBosko Milekic static void
118099a0e58SBosko Milekic tunable_mbinit(void *dummy)
119099a0e58SBosko Milekic {
120e0c00addSAndre Oppermann 	quad_t realmem;
12137140716SAndre Oppermann 
12237140716SAndre Oppermann 	/*
12337140716SAndre Oppermann 	 * The default limit for all mbuf related memory is 1/2 of all
12437140716SAndre Oppermann 	 * available kernel memory (physical or kmem).
12537140716SAndre Oppermann 	 * At most it can be 3/4 of available kernel memory.
12637140716SAndre Oppermann 	 */
1275df87b21SJeff Roberson 	realmem = qmin((quad_t)physmem * PAGE_SIZE, vm_kmem_size);
12837140716SAndre Oppermann 	maxmbufmem = realmem / 2;
129e0c00addSAndre Oppermann 	TUNABLE_QUAD_FETCH("kern.ipc.maxmbufmem", &maxmbufmem);
13037140716SAndre Oppermann 	if (maxmbufmem > realmem / 4 * 3)
13137140716SAndre Oppermann 		maxmbufmem = realmem / 4 * 3;
132099a0e58SBosko Milekic 
133812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
134416a434cSAndre Oppermann 	if (nmbclusters == 0)
135416a434cSAndre Oppermann 		nmbclusters = maxmbufmem / MCLBYTES / 4;
136812302c3SNavdeep Parhar 
137812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop);
138812302c3SNavdeep Parhar 	if (nmbjumbop == 0)
139416a434cSAndre Oppermann 		nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4;
140812302c3SNavdeep Parhar 
141812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbjumbo9", &nmbjumbo9);
142812302c3SNavdeep Parhar 	if (nmbjumbo9 == 0)
143416a434cSAndre Oppermann 		nmbjumbo9 = maxmbufmem / MJUM9BYTES / 6;
144812302c3SNavdeep Parhar 
145812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbjumbo16", &nmbjumbo16);
146812302c3SNavdeep Parhar 	if (nmbjumbo16 == 0)
147416a434cSAndre Oppermann 		nmbjumbo16 = maxmbufmem / MJUM16BYTES / 6;
148416a434cSAndre Oppermann 
149416a434cSAndre Oppermann 	/*
150416a434cSAndre Oppermann 	 * We need at least as many mbufs as we have clusters of
151416a434cSAndre Oppermann 	 * the various types added together.
152416a434cSAndre Oppermann 	 */
153416a434cSAndre Oppermann 	TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
154416a434cSAndre Oppermann 	if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16)
155416a434cSAndre Oppermann 		nmbufs = lmax(maxmbufmem / MSIZE / 5,
156416a434cSAndre Oppermann 		    nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16);
157099a0e58SBosko Milekic }
15837140716SAndre Oppermann SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
159099a0e58SBosko Milekic 
1604f590175SPaul Saab static int
1614f590175SPaul Saab sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
1624f590175SPaul Saab {
1634f590175SPaul Saab 	int error, newnmbclusters;
1644f590175SPaul Saab 
1654f590175SPaul Saab 	newnmbclusters = nmbclusters;
166041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
167d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbclusters != nmbclusters) {
168ead46972SAndre Oppermann 		if (newnmbclusters > nmbclusters &&
169ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
1704f590175SPaul Saab 			nmbclusters = newnmbclusters;
171bc4a1b8cSAndre Oppermann 			nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
1724f590175SPaul Saab 			EVENTHANDLER_INVOKE(nmbclusters_change);
1734f590175SPaul Saab 		} else
1744f590175SPaul Saab 			error = EINVAL;
1754f590175SPaul Saab 	}
1764f590175SPaul Saab 	return (error);
1774f590175SPaul Saab }
1784f590175SPaul Saab SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
1794f590175SPaul Saab &nmbclusters, 0, sysctl_nmbclusters, "IU",
180099a0e58SBosko Milekic     "Maximum number of mbuf clusters allowed");
181cf70a46bSRandall Stewart 
182cf70a46bSRandall Stewart static int
183cf70a46bSRandall Stewart sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS)
184cf70a46bSRandall Stewart {
185cf70a46bSRandall Stewart 	int error, newnmbjumbop;
186cf70a46bSRandall Stewart 
187cf70a46bSRandall Stewart 	newnmbjumbop = nmbjumbop;
188cf70a46bSRandall Stewart 	error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req);
189d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbjumbop != nmbjumbop) {
190ead46972SAndre Oppermann 		if (newnmbjumbop > nmbjumbop &&
191ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
192cf70a46bSRandall Stewart 			nmbjumbop = newnmbjumbop;
193bc4a1b8cSAndre Oppermann 			nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
194cf70a46bSRandall Stewart 		} else
195cf70a46bSRandall Stewart 			error = EINVAL;
196cf70a46bSRandall Stewart 	}
197cf70a46bSRandall Stewart 	return (error);
198cf70a46bSRandall Stewart }
199cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW,
200cf70a46bSRandall Stewart &nmbjumbop, 0, sysctl_nmbjumbop, "IU",
201ec63cb90SAndre Oppermann     "Maximum number of mbuf page size jumbo clusters allowed");
202cf70a46bSRandall Stewart 
203cf70a46bSRandall Stewart static int
204cf70a46bSRandall Stewart sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS)
205cf70a46bSRandall Stewart {
206cf70a46bSRandall Stewart 	int error, newnmbjumbo9;
207cf70a46bSRandall Stewart 
208cf70a46bSRandall Stewart 	newnmbjumbo9 = nmbjumbo9;
209cf70a46bSRandall Stewart 	error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req);
210d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbjumbo9 != nmbjumbo9) {
211ead46972SAndre Oppermann 		if (newnmbjumbo9 > nmbjumbo9 &&
212ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
213cf70a46bSRandall Stewart 			nmbjumbo9 = newnmbjumbo9;
214bc4a1b8cSAndre Oppermann 			nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
215cf70a46bSRandall Stewart 		} else
216cf70a46bSRandall Stewart 			error = EINVAL;
217cf70a46bSRandall Stewart 	}
218cf70a46bSRandall Stewart 	return (error);
219cf70a46bSRandall Stewart }
220cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW,
221cf70a46bSRandall Stewart &nmbjumbo9, 0, sysctl_nmbjumbo9, "IU",
22256a4e45aSAndre Oppermann     "Maximum number of mbuf 9k jumbo clusters allowed");
223cf70a46bSRandall Stewart 
224cf70a46bSRandall Stewart static int
225cf70a46bSRandall Stewart sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS)
226cf70a46bSRandall Stewart {
227cf70a46bSRandall Stewart 	int error, newnmbjumbo16;
228cf70a46bSRandall Stewart 
229cf70a46bSRandall Stewart 	newnmbjumbo16 = nmbjumbo16;
230cf70a46bSRandall Stewart 	error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req);
231d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbjumbo16 != nmbjumbo16) {
232ead46972SAndre Oppermann 		if (newnmbjumbo16 > nmbjumbo16 &&
233ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
234cf70a46bSRandall Stewart 			nmbjumbo16 = newnmbjumbo16;
235bc4a1b8cSAndre Oppermann 			nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
236cf70a46bSRandall Stewart 		} else
237cf70a46bSRandall Stewart 			error = EINVAL;
238cf70a46bSRandall Stewart 	}
239cf70a46bSRandall Stewart 	return (error);
240cf70a46bSRandall Stewart }
241cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW,
242cf70a46bSRandall Stewart &nmbjumbo16, 0, sysctl_nmbjumbo16, "IU",
24356a4e45aSAndre Oppermann     "Maximum number of mbuf 16k jumbo clusters allowed");
244cf70a46bSRandall Stewart 
245ead46972SAndre Oppermann static int
246ead46972SAndre Oppermann sysctl_nmbufs(SYSCTL_HANDLER_ARGS)
247ead46972SAndre Oppermann {
248ead46972SAndre Oppermann 	int error, newnmbufs;
249ead46972SAndre Oppermann 
250ead46972SAndre Oppermann 	newnmbufs = nmbufs;
251ead46972SAndre Oppermann 	error = sysctl_handle_int(oidp, &newnmbufs, 0, req);
252d251e700SJohn Baldwin 	if (error == 0 && req->newptr && newnmbufs != nmbufs) {
253ead46972SAndre Oppermann 		if (newnmbufs > nmbufs) {
254ead46972SAndre Oppermann 			nmbufs = newnmbufs;
255bc4a1b8cSAndre Oppermann 			nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
256ead46972SAndre Oppermann 			EVENTHANDLER_INVOKE(nmbufs_change);
257ead46972SAndre Oppermann 		} else
258ead46972SAndre Oppermann 			error = EINVAL;
259ead46972SAndre Oppermann 	}
260ead46972SAndre Oppermann 	return (error);
261ead46972SAndre Oppermann }
262e0c00addSAndre Oppermann SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbufs, CTLTYPE_INT|CTLFLAG_RW,
263ead46972SAndre Oppermann &nmbufs, 0, sysctl_nmbufs, "IU",
264ead46972SAndre Oppermann     "Maximum number of mbufs allowed");
265cf70a46bSRandall Stewart 
266099a0e58SBosko Milekic /*
267099a0e58SBosko Milekic  * Zones from which we allocate.
268099a0e58SBosko Milekic  */
269099a0e58SBosko Milekic uma_zone_t	zone_mbuf;
270099a0e58SBosko Milekic uma_zone_t	zone_clust;
271099a0e58SBosko Milekic uma_zone_t	zone_pack;
272ec63cb90SAndre Oppermann uma_zone_t	zone_jumbop;
27356a4e45aSAndre Oppermann uma_zone_t	zone_jumbo9;
27456a4e45aSAndre Oppermann uma_zone_t	zone_jumbo16;
275099a0e58SBosko Milekic 
276099a0e58SBosko Milekic /*
277099a0e58SBosko Milekic  * Local prototypes.
278099a0e58SBosko Milekic  */
279b23f72e9SBrian Feldman static int	mb_ctor_mbuf(void *, int, void *, int);
280b23f72e9SBrian Feldman static int	mb_ctor_clust(void *, int, void *, int);
281b23f72e9SBrian Feldman static int	mb_ctor_pack(void *, int, void *, int);
282099a0e58SBosko Milekic static void	mb_dtor_mbuf(void *, int, void *);
28356a4e45aSAndre Oppermann static void	mb_dtor_pack(void *, int, void *);
28456a4e45aSAndre Oppermann static int	mb_zinit_pack(void *, int, int);
28556a4e45aSAndre Oppermann static void	mb_zfini_pack(void *, int);
286e60b2fcbSGleb Smirnoff static void	mb_reclaim(uma_zone_t, int);
287ab3185d1SJeff Roberson static void    *mbuf_jumbo_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
288099a0e58SBosko Milekic 
28937140716SAndre Oppermann /* Ensure that MSIZE is a power of 2. */
290a04946cfSBrian Somers CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
291a04946cfSBrian Somers 
292099a0e58SBosko Milekic /*
293099a0e58SBosko Milekic  * Initialize FreeBSD Network buffer allocation.
294099a0e58SBosko Milekic  */
295099a0e58SBosko Milekic static void
296099a0e58SBosko Milekic mbuf_init(void *dummy)
297099a0e58SBosko Milekic {
298099a0e58SBosko Milekic 
299099a0e58SBosko Milekic 	/*
300099a0e58SBosko Milekic 	 * Configure UMA zones for Mbufs, Clusters, and Packets.
301099a0e58SBosko Milekic 	 */
30256a4e45aSAndre Oppermann 	zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
30356a4e45aSAndre Oppermann 	    mb_ctor_mbuf, mb_dtor_mbuf,
304121f0509SMike Silbersack #ifdef INVARIANTS
30556a4e45aSAndre Oppermann 	    trash_init, trash_fini,
306121f0509SMike Silbersack #else
30756a4e45aSAndre Oppermann 	    NULL, NULL,
308121f0509SMike Silbersack #endif
30956a4e45aSAndre Oppermann 	    MSIZE - 1, UMA_ZONE_MAXBUCKET);
31045fe0bf7SPawel Jakub Dawidek 	if (nmbufs > 0)
31145fe0bf7SPawel Jakub Dawidek 		nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
3126e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_mbuf, "kern.ipc.nmbufs limit reached");
313e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_mbuf, mb_reclaim);
31456a4e45aSAndre Oppermann 
31568352adfSRobert Watson 	zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
31656a5f52eSGleb Smirnoff 	    mb_ctor_clust,
317121f0509SMike Silbersack #ifdef INVARIANTS
31856a5f52eSGleb Smirnoff 	    trash_dtor, trash_init, trash_fini,
319121f0509SMike Silbersack #else
32056a5f52eSGleb Smirnoff 	    NULL, NULL, NULL,
321121f0509SMike Silbersack #endif
32256a5f52eSGleb Smirnoff 	    UMA_ALIGN_PTR, 0);
32345fe0bf7SPawel Jakub Dawidek 	if (nmbclusters > 0)
32445fe0bf7SPawel Jakub Dawidek 		nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
3256e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_clust, "kern.ipc.nmbclusters limit reached");
326e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_clust, mb_reclaim);
327099a0e58SBosko Milekic 
32856a4e45aSAndre Oppermann 	zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
32956a4e45aSAndre Oppermann 	    mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
33056a4e45aSAndre Oppermann 
331fcf90618SGleb Smirnoff 	/* Make jumbo frame zone too. Page size, 9k and 16k. */
332ec63cb90SAndre Oppermann 	zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
33356a5f52eSGleb Smirnoff 	    mb_ctor_clust,
334d5269a63SAndre Oppermann #ifdef INVARIANTS
33556a5f52eSGleb Smirnoff 	    trash_dtor, trash_init, trash_fini,
336d5269a63SAndre Oppermann #else
33756a5f52eSGleb Smirnoff 	    NULL, NULL, NULL,
338d5269a63SAndre Oppermann #endif
33956a5f52eSGleb Smirnoff 	    UMA_ALIGN_PTR, 0);
34045fe0bf7SPawel Jakub Dawidek 	if (nmbjumbop > 0)
34145fe0bf7SPawel Jakub Dawidek 		nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
3426e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_jumbop, "kern.ipc.nmbjumbop limit reached");
343e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_jumbop, mb_reclaim);
344d5269a63SAndre Oppermann 
34556a4e45aSAndre Oppermann 	zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
34656a5f52eSGleb Smirnoff 	    mb_ctor_clust,
34756a4e45aSAndre Oppermann #ifdef INVARIANTS
34856a5f52eSGleb Smirnoff 	    trash_dtor, trash_init, trash_fini,
34956a4e45aSAndre Oppermann #else
35056a5f52eSGleb Smirnoff 	    NULL, NULL, NULL,
35156a4e45aSAndre Oppermann #endif
35256a5f52eSGleb Smirnoff 	    UMA_ALIGN_PTR, 0);
353ba63339aSAlan Cox 	uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc);
35445fe0bf7SPawel Jakub Dawidek 	if (nmbjumbo9 > 0)
35545fe0bf7SPawel Jakub Dawidek 		nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
3566e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_jumbo9, "kern.ipc.nmbjumbo9 limit reached");
357e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_jumbo9, mb_reclaim);
35856a4e45aSAndre Oppermann 
35956a4e45aSAndre Oppermann 	zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
36056a5f52eSGleb Smirnoff 	    mb_ctor_clust,
36156a4e45aSAndre Oppermann #ifdef INVARIANTS
36256a5f52eSGleb Smirnoff 	    trash_dtor, trash_init, trash_fini,
36356a4e45aSAndre Oppermann #else
36456a5f52eSGleb Smirnoff 	    NULL, NULL, NULL,
36556a4e45aSAndre Oppermann #endif
36656a5f52eSGleb Smirnoff 	    UMA_ALIGN_PTR, 0);
367ba63339aSAlan Cox 	uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc);
36845fe0bf7SPawel Jakub Dawidek 	if (nmbjumbo16 > 0)
36945fe0bf7SPawel Jakub Dawidek 		nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
3706e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_jumbo16, "kern.ipc.nmbjumbo16 limit reached");
371e60b2fcbSGleb Smirnoff 	uma_zone_set_maxaction(zone_jumbo16, mb_reclaim);
37256a4e45aSAndre Oppermann 
373099a0e58SBosko Milekic 	/*
374099a0e58SBosko Milekic 	 * Hook event handler for low-memory situation, used to
375099a0e58SBosko Milekic 	 * drain protocols and push data back to the caches (UMA
376099a0e58SBosko Milekic 	 * later pushes it back to VM).
377099a0e58SBosko Milekic 	 */
378099a0e58SBosko Milekic 	EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
379099a0e58SBosko Milekic 	    EVENTHANDLER_PRI_FIRST);
380099a0e58SBosko Milekic }
38137140716SAndre Oppermann SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
382099a0e58SBosko Milekic 
383*5475ca5aSMark Johnston #ifdef NETDUMP
384*5475ca5aSMark Johnston /*
385*5475ca5aSMark Johnston  * netdump makes use of a pre-allocated pool of mbufs and clusters.  When
386*5475ca5aSMark Johnston  * netdump is configured, we initialize a set of UMA cache zones which return
387*5475ca5aSMark Johnston  * items from this pool.  At panic-time, the regular UMA zone pointers are
388*5475ca5aSMark Johnston  * overwritten with those of the cache zones so that drivers may allocate and
389*5475ca5aSMark Johnston  * free mbufs and clusters without attempting to allocate physical memory.
390*5475ca5aSMark Johnston  *
391*5475ca5aSMark Johnston  * We keep mbufs and clusters in a pair of mbuf queues.  In particular, for
392*5475ca5aSMark Johnston  * the purpose of caching clusters, we treat them as mbufs.
393*5475ca5aSMark Johnston  */
394*5475ca5aSMark Johnston static struct mbufq nd_mbufq =
395*5475ca5aSMark Johnston     { STAILQ_HEAD_INITIALIZER(nd_mbufq.mq_head), 0, INT_MAX };
396*5475ca5aSMark Johnston static struct mbufq nd_clustq =
397*5475ca5aSMark Johnston     { STAILQ_HEAD_INITIALIZER(nd_clustq.mq_head), 0, INT_MAX };
398*5475ca5aSMark Johnston 
399*5475ca5aSMark Johnston static int nd_clsize;
400*5475ca5aSMark Johnston static uma_zone_t nd_zone_mbuf;
401*5475ca5aSMark Johnston static uma_zone_t nd_zone_clust;
402*5475ca5aSMark Johnston static uma_zone_t nd_zone_pack;
403*5475ca5aSMark Johnston 
404*5475ca5aSMark Johnston static int
405*5475ca5aSMark Johnston nd_buf_import(void *arg, void **store, int count, int domain __unused,
406*5475ca5aSMark Johnston     int flags)
407*5475ca5aSMark Johnston {
408*5475ca5aSMark Johnston 	struct mbufq *q;
409*5475ca5aSMark Johnston 	struct mbuf *m;
410*5475ca5aSMark Johnston 	int i;
411*5475ca5aSMark Johnston 
412*5475ca5aSMark Johnston 	q = arg;
413*5475ca5aSMark Johnston 
414*5475ca5aSMark Johnston 	for (i = 0; i < count; i++) {
415*5475ca5aSMark Johnston 		m = mbufq_dequeue(q);
416*5475ca5aSMark Johnston 		if (m == NULL)
417*5475ca5aSMark Johnston 			break;
418*5475ca5aSMark Johnston 		trash_init(m, q == &nd_mbufq ? MSIZE : nd_clsize, flags);
419*5475ca5aSMark Johnston 		store[i] = m;
420*5475ca5aSMark Johnston 	}
421*5475ca5aSMark Johnston 	return (i);
422*5475ca5aSMark Johnston }
423*5475ca5aSMark Johnston 
424*5475ca5aSMark Johnston static void
425*5475ca5aSMark Johnston nd_buf_release(void *arg, void **store, int count)
426*5475ca5aSMark Johnston {
427*5475ca5aSMark Johnston 	struct mbufq *q;
428*5475ca5aSMark Johnston 	struct mbuf *m;
429*5475ca5aSMark Johnston 	int i;
430*5475ca5aSMark Johnston 
431*5475ca5aSMark Johnston 	q = arg;
432*5475ca5aSMark Johnston 
433*5475ca5aSMark Johnston 	for (i = 0; i < count; i++) {
434*5475ca5aSMark Johnston 		m = store[i];
435*5475ca5aSMark Johnston 		(void)mbufq_enqueue(q, m);
436*5475ca5aSMark Johnston 	}
437*5475ca5aSMark Johnston }
438*5475ca5aSMark Johnston 
439*5475ca5aSMark Johnston static int
440*5475ca5aSMark Johnston nd_pack_import(void *arg __unused, void **store, int count, int domain __unused,
441*5475ca5aSMark Johnston     int flags __unused)
442*5475ca5aSMark Johnston {
443*5475ca5aSMark Johnston 	struct mbuf *m;
444*5475ca5aSMark Johnston 	void *clust;
445*5475ca5aSMark Johnston 	int i;
446*5475ca5aSMark Johnston 
447*5475ca5aSMark Johnston 	for (i = 0; i < count; i++) {
448*5475ca5aSMark Johnston 		m = m_get(MT_DATA, M_NOWAIT);
449*5475ca5aSMark Johnston 		if (m == NULL)
450*5475ca5aSMark Johnston 			break;
451*5475ca5aSMark Johnston 		clust = uma_zalloc(nd_zone_clust, M_NOWAIT);
452*5475ca5aSMark Johnston 		if (clust == NULL) {
453*5475ca5aSMark Johnston 			m_free(m);
454*5475ca5aSMark Johnston 			break;
455*5475ca5aSMark Johnston 		}
456*5475ca5aSMark Johnston 		mb_ctor_clust(clust, nd_clsize, m, 0);
457*5475ca5aSMark Johnston 		store[i] = m;
458*5475ca5aSMark Johnston 	}
459*5475ca5aSMark Johnston 	return (i);
460*5475ca5aSMark Johnston }
461*5475ca5aSMark Johnston 
462*5475ca5aSMark Johnston static void
463*5475ca5aSMark Johnston nd_pack_release(void *arg __unused, void **store, int count)
464*5475ca5aSMark Johnston {
465*5475ca5aSMark Johnston 	struct mbuf *m;
466*5475ca5aSMark Johnston 	void *clust;
467*5475ca5aSMark Johnston 	int i;
468*5475ca5aSMark Johnston 
469*5475ca5aSMark Johnston 	for (i = 0; i < count; i++) {
470*5475ca5aSMark Johnston 		m = store[i];
471*5475ca5aSMark Johnston 		clust = m->m_ext.ext_buf;
472*5475ca5aSMark Johnston 		uma_zfree(nd_zone_clust, clust);
473*5475ca5aSMark Johnston 		uma_zfree(nd_zone_mbuf, m);
474*5475ca5aSMark Johnston 	}
475*5475ca5aSMark Johnston }
476*5475ca5aSMark Johnston 
477*5475ca5aSMark Johnston /*
478*5475ca5aSMark Johnston  * Free the pre-allocated mbufs and clusters reserved for netdump, and destroy
479*5475ca5aSMark Johnston  * the corresponding UMA cache zones.
480*5475ca5aSMark Johnston  */
481*5475ca5aSMark Johnston void
482*5475ca5aSMark Johnston netdump_mbuf_drain(void)
483*5475ca5aSMark Johnston {
484*5475ca5aSMark Johnston 	struct mbuf *m;
485*5475ca5aSMark Johnston 	void *item;
486*5475ca5aSMark Johnston 
487*5475ca5aSMark Johnston 	if (nd_zone_mbuf != NULL) {
488*5475ca5aSMark Johnston 		uma_zdestroy(nd_zone_mbuf);
489*5475ca5aSMark Johnston 		nd_zone_mbuf = NULL;
490*5475ca5aSMark Johnston 	}
491*5475ca5aSMark Johnston 	if (nd_zone_clust != NULL) {
492*5475ca5aSMark Johnston 		uma_zdestroy(nd_zone_clust);
493*5475ca5aSMark Johnston 		nd_zone_clust = NULL;
494*5475ca5aSMark Johnston 	}
495*5475ca5aSMark Johnston 	if (nd_zone_pack != NULL) {
496*5475ca5aSMark Johnston 		uma_zdestroy(nd_zone_pack);
497*5475ca5aSMark Johnston 		nd_zone_pack = NULL;
498*5475ca5aSMark Johnston 	}
499*5475ca5aSMark Johnston 
500*5475ca5aSMark Johnston 	while ((m = mbufq_dequeue(&nd_mbufq)) != NULL)
501*5475ca5aSMark Johnston 		m_free(m);
502*5475ca5aSMark Johnston 	while ((item = mbufq_dequeue(&nd_clustq)) != NULL)
503*5475ca5aSMark Johnston 		uma_zfree(m_getzone(nd_clsize), item);
504*5475ca5aSMark Johnston }
505*5475ca5aSMark Johnston 
506*5475ca5aSMark Johnston /*
507*5475ca5aSMark Johnston  * Callback invoked immediately prior to starting a netdump.
508*5475ca5aSMark Johnston  */
509*5475ca5aSMark Johnston void
510*5475ca5aSMark Johnston netdump_mbuf_dump(void)
511*5475ca5aSMark Johnston {
512*5475ca5aSMark Johnston 
513*5475ca5aSMark Johnston 	/*
514*5475ca5aSMark Johnston 	 * All cluster zones return buffers of the size requested by the
515*5475ca5aSMark Johnston 	 * drivers.  It's up to the driver to reinitialize the zones if the
516*5475ca5aSMark Johnston 	 * MTU of a netdump-enabled interface changes.
517*5475ca5aSMark Johnston 	 */
518*5475ca5aSMark Johnston 	printf("netdump: overwriting mbuf zone pointers\n");
519*5475ca5aSMark Johnston 	zone_mbuf = nd_zone_mbuf;
520*5475ca5aSMark Johnston 	zone_clust = nd_zone_clust;
521*5475ca5aSMark Johnston 	zone_pack = nd_zone_pack;
522*5475ca5aSMark Johnston 	zone_jumbop = nd_zone_clust;
523*5475ca5aSMark Johnston 	zone_jumbo9 = nd_zone_clust;
524*5475ca5aSMark Johnston 	zone_jumbo16 = nd_zone_clust;
525*5475ca5aSMark Johnston }
526*5475ca5aSMark Johnston 
527*5475ca5aSMark Johnston /*
528*5475ca5aSMark Johnston  * Reinitialize the netdump mbuf+cluster pool and cache zones.
529*5475ca5aSMark Johnston  */
530*5475ca5aSMark Johnston void
531*5475ca5aSMark Johnston netdump_mbuf_reinit(int nmbuf, int nclust, int clsize)
532*5475ca5aSMark Johnston {
533*5475ca5aSMark Johnston 	struct mbuf *m;
534*5475ca5aSMark Johnston 	void *item;
535*5475ca5aSMark Johnston 
536*5475ca5aSMark Johnston 	netdump_mbuf_drain();
537*5475ca5aSMark Johnston 
538*5475ca5aSMark Johnston 	nd_clsize = clsize;
539*5475ca5aSMark Johnston 
540*5475ca5aSMark Johnston 	nd_zone_mbuf = uma_zcache_create("netdump_" MBUF_MEM_NAME,
541*5475ca5aSMark Johnston 	    MSIZE, mb_ctor_mbuf, mb_dtor_mbuf,
542*5475ca5aSMark Johnston #ifdef INVARIANTS
543*5475ca5aSMark Johnston 	    trash_init, trash_fini,
544*5475ca5aSMark Johnston #else
545*5475ca5aSMark Johnston 	    NULL, NULL,
546*5475ca5aSMark Johnston #endif
547*5475ca5aSMark Johnston 	    nd_buf_import, nd_buf_release,
548*5475ca5aSMark Johnston 	    &nd_mbufq, UMA_ZONE_NOBUCKET);
549*5475ca5aSMark Johnston 
550*5475ca5aSMark Johnston 	nd_zone_clust = uma_zcache_create("netdump_" MBUF_CLUSTER_MEM_NAME,
551*5475ca5aSMark Johnston 	    clsize, mb_ctor_clust,
552*5475ca5aSMark Johnston #ifdef INVARIANTS
553*5475ca5aSMark Johnston 	    trash_dtor, trash_init, trash_fini,
554*5475ca5aSMark Johnston #else
555*5475ca5aSMark Johnston 	    NULL, NULL, NULL,
556*5475ca5aSMark Johnston #endif
557*5475ca5aSMark Johnston 	    nd_buf_import, nd_buf_release,
558*5475ca5aSMark Johnston 	    &nd_clustq, UMA_ZONE_NOBUCKET);
559*5475ca5aSMark Johnston 
560*5475ca5aSMark Johnston 	nd_zone_pack = uma_zcache_create("netdump_" MBUF_PACKET_MEM_NAME,
561*5475ca5aSMark Johnston 	    MCLBYTES, mb_ctor_pack, mb_dtor_pack, NULL, NULL,
562*5475ca5aSMark Johnston 	    nd_pack_import, nd_pack_release,
563*5475ca5aSMark Johnston 	    NULL, UMA_ZONE_NOBUCKET);
564*5475ca5aSMark Johnston 
565*5475ca5aSMark Johnston 	while (nmbuf-- > 0) {
566*5475ca5aSMark Johnston 		m = m_get(MT_DATA, M_WAITOK);
567*5475ca5aSMark Johnston 		uma_zfree(nd_zone_mbuf, m);
568*5475ca5aSMark Johnston 	}
569*5475ca5aSMark Johnston 	while (nclust-- > 0) {
570*5475ca5aSMark Johnston 		item = uma_zalloc(m_getzone(nd_clsize), M_WAITOK);
571*5475ca5aSMark Johnston 		uma_zfree(nd_zone_clust, item);
572*5475ca5aSMark Johnston 	}
573*5475ca5aSMark Johnston }
574*5475ca5aSMark Johnston #endif /* NETDUMP */
575*5475ca5aSMark Johnston 
576099a0e58SBosko Milekic /*
577ba63339aSAlan Cox  * UMA backend page allocator for the jumbo frame zones.
578ba63339aSAlan Cox  *
579ba63339aSAlan Cox  * Allocates kernel virtual memory that is backed by contiguous physical
580ba63339aSAlan Cox  * pages.
581ba63339aSAlan Cox  */
582ba63339aSAlan Cox static void *
583ab3185d1SJeff Roberson mbuf_jumbo_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
584ab3185d1SJeff Roberson     int wait)
585ba63339aSAlan Cox {
586ba63339aSAlan Cox 
5877630c265SAlan Cox 	/* Inform UMA that this allocator uses kernel_map/object. */
5887630c265SAlan Cox 	*flags = UMA_SLAB_KERNEL;
589ab3185d1SJeff Roberson 	return ((void *)kmem_alloc_contig_domain(domain, bytes, wait,
5903153e878SAlan Cox 	    (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
591ba63339aSAlan Cox }
592ba63339aSAlan Cox 
593ba63339aSAlan Cox /*
594099a0e58SBosko Milekic  * Constructor for Mbuf master zone.
595099a0e58SBosko Milekic  *
596099a0e58SBosko Milekic  * The 'arg' pointer points to a mb_args structure which
597099a0e58SBosko Milekic  * contains call-specific information required to support the
59856a4e45aSAndre Oppermann  * mbuf allocation API.  See mbuf.h.
599099a0e58SBosko Milekic  */
600b23f72e9SBrian Feldman static int
601b23f72e9SBrian Feldman mb_ctor_mbuf(void *mem, int size, void *arg, int how)
602099a0e58SBosko Milekic {
603099a0e58SBosko Milekic 	struct mbuf *m;
604099a0e58SBosko Milekic 	struct mb_args *args;
605b23f72e9SBrian Feldman 	int error;
606099a0e58SBosko Milekic 	int flags;
607099a0e58SBosko Milekic 	short type;
608099a0e58SBosko Milekic 
609121f0509SMike Silbersack #ifdef INVARIANTS
610121f0509SMike Silbersack 	trash_ctor(mem, size, arg, how);
611121f0509SMike Silbersack #endif
612099a0e58SBosko Milekic 	args = (struct mb_args *)arg;
613099a0e58SBosko Milekic 	type = args->type;
614099a0e58SBosko Milekic 
61556a4e45aSAndre Oppermann 	/*
61656a4e45aSAndre Oppermann 	 * The mbuf is initialized later.  The caller has the
617fcf90618SGleb Smirnoff 	 * responsibility to set up any MAC labels too.
61856a4e45aSAndre Oppermann 	 */
61956a4e45aSAndre Oppermann 	if (type == MT_NOINIT)
62056a4e45aSAndre Oppermann 		return (0);
62156a4e45aSAndre Oppermann 
622afb295ccSAndre Oppermann 	m = (struct mbuf *)mem;
623afb295ccSAndre Oppermann 	flags = args->flags;
624fddd4f62SNavdeep Parhar 	MPASS((flags & M_NOFREE) == 0);
625afb295ccSAndre Oppermann 
626b4b12e52SGleb Smirnoff 	error = m_init(m, how, type, flags);
627afb295ccSAndre Oppermann 
628b23f72e9SBrian Feldman 	return (error);
629099a0e58SBosko Milekic }
630099a0e58SBosko Milekic 
631099a0e58SBosko Milekic /*
63256a4e45aSAndre Oppermann  * The Mbuf master zone destructor.
633099a0e58SBosko Milekic  */
634099a0e58SBosko Milekic static void
635099a0e58SBosko Milekic mb_dtor_mbuf(void *mem, int size, void *arg)
636099a0e58SBosko Milekic {
637099a0e58SBosko Milekic 	struct mbuf *m;
638629b9e08SKip Macy 	unsigned long flags;
639099a0e58SBosko Milekic 
640099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
641629b9e08SKip Macy 	flags = (unsigned long)arg;
642629b9e08SKip Macy 
643a9fa76f2SNavdeep Parhar 	KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__));
6444c7070dbSScott Long 	if (!(flags & MB_DTOR_SKIP) && (m->m_flags & M_PKTHDR) && !SLIST_EMPTY(&m->m_pkthdr.tags))
645099a0e58SBosko Milekic 		m_tag_delete_chain(m, NULL);
646121f0509SMike Silbersack #ifdef INVARIANTS
647121f0509SMike Silbersack 	trash_dtor(mem, size, arg);
648121f0509SMike Silbersack #endif
649099a0e58SBosko Milekic }
650099a0e58SBosko Milekic 
65156a4e45aSAndre Oppermann /*
65256a4e45aSAndre Oppermann  * The Mbuf Packet zone destructor.
65356a4e45aSAndre Oppermann  */
654099a0e58SBosko Milekic static void
655099a0e58SBosko Milekic mb_dtor_pack(void *mem, int size, void *arg)
656099a0e58SBosko Milekic {
657099a0e58SBosko Milekic 	struct mbuf *m;
658099a0e58SBosko Milekic 
659099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
660099a0e58SBosko Milekic 	if ((m->m_flags & M_PKTHDR) != 0)
661099a0e58SBosko Milekic 		m_tag_delete_chain(m, NULL);
66256a4e45aSAndre Oppermann 
66356a4e45aSAndre Oppermann 	/* Make sure we've got a clean cluster back. */
66456a4e45aSAndre Oppermann 	KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
66556a4e45aSAndre Oppermann 	KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
66656a4e45aSAndre Oppermann 	KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
667cf827063SPoul-Henning Kamp 	KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__));
668cf827063SPoul-Henning Kamp 	KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__));
66956a4e45aSAndre Oppermann 	KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
67049d46b61SGleb Smirnoff 	KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
671121f0509SMike Silbersack #ifdef INVARIANTS
672121f0509SMike Silbersack 	trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
673121f0509SMike Silbersack #endif
6746c125b8dSMohan Srinivasan 	/*
675ef44c8d2SDavid E. O'Brien 	 * If there are processes blocked on zone_clust, waiting for pages
676ef44c8d2SDavid E. O'Brien 	 * to be freed up, * cause them to be woken up by draining the
677ef44c8d2SDavid E. O'Brien 	 * packet zone.  We are exposed to a race here * (in the check for
678ef44c8d2SDavid E. O'Brien 	 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
679ef44c8d2SDavid E. O'Brien 	 * is deliberate. We don't want to acquire the zone lock for every
680ef44c8d2SDavid E. O'Brien 	 * mbuf free.
6816c125b8dSMohan Srinivasan 	 */
6826c125b8dSMohan Srinivasan 	if (uma_zone_exhausted_nolock(zone_clust))
6836c125b8dSMohan Srinivasan 		zone_drain(zone_pack);
684099a0e58SBosko Milekic }
685099a0e58SBosko Milekic 
686099a0e58SBosko Milekic /*
687ec63cb90SAndre Oppermann  * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
688099a0e58SBosko Milekic  *
689099a0e58SBosko Milekic  * Here the 'arg' pointer points to the Mbuf which we
69056a4e45aSAndre Oppermann  * are configuring cluster storage for.  If 'arg' is
69156a4e45aSAndre Oppermann  * empty we allocate just the cluster without setting
69256a4e45aSAndre Oppermann  * the mbuf to it.  See mbuf.h.
693099a0e58SBosko Milekic  */
694b23f72e9SBrian Feldman static int
695b23f72e9SBrian Feldman mb_ctor_clust(void *mem, int size, void *arg, int how)
696099a0e58SBosko Milekic {
697099a0e58SBosko Milekic 	struct mbuf *m;
698099a0e58SBosko Milekic 
699121f0509SMike Silbersack #ifdef INVARIANTS
700121f0509SMike Silbersack 	trash_ctor(mem, size, arg, how);
701121f0509SMike Silbersack #endif
7020f4d9d04SKip Macy 	m = (struct mbuf *)arg;
7030f4d9d04SKip Macy 	if (m != NULL) {
704e8fd18f3SGleb Smirnoff 		m->m_ext.ext_buf = (char *)mem;
705099a0e58SBosko Milekic 		m->m_data = m->m_ext.ext_buf;
706099a0e58SBosko Milekic 		m->m_flags |= M_EXT;
707099a0e58SBosko Milekic 		m->m_ext.ext_free = NULL;
708cf827063SPoul-Henning Kamp 		m->m_ext.ext_arg1 = NULL;
709cf827063SPoul-Henning Kamp 		m->m_ext.ext_arg2 = NULL;
71056a4e45aSAndre Oppermann 		m->m_ext.ext_size = size;
71156a5f52eSGleb Smirnoff 		m->m_ext.ext_type = m_gettype(size);
71256a5f52eSGleb Smirnoff 		m->m_ext.ext_flags = EXT_FLAG_EMBREF;
71356a5f52eSGleb Smirnoff 		m->m_ext.ext_count = 1;
71456a4e45aSAndre Oppermann 	}
7150f4d9d04SKip Macy 
716b23f72e9SBrian Feldman 	return (0);
717099a0e58SBosko Milekic }
718099a0e58SBosko Milekic 
71956a4e45aSAndre Oppermann /*
720099a0e58SBosko Milekic  * The Packet secondary zone's init routine, executed on the
72156a4e45aSAndre Oppermann  * object's transition from mbuf keg slab to zone cache.
722099a0e58SBosko Milekic  */
723b23f72e9SBrian Feldman static int
72456a4e45aSAndre Oppermann mb_zinit_pack(void *mem, int size, int how)
725099a0e58SBosko Milekic {
726099a0e58SBosko Milekic 	struct mbuf *m;
727099a0e58SBosko Milekic 
72856a4e45aSAndre Oppermann 	m = (struct mbuf *)mem;		/* m is virgin. */
729a7bd90efSAndre Oppermann 	if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
730a7bd90efSAndre Oppermann 	    m->m_ext.ext_buf == NULL)
731b23f72e9SBrian Feldman 		return (ENOMEM);
732cd5bb63bSAndre Oppermann 	m->m_ext.ext_type = EXT_PACKET;	/* Override. */
733121f0509SMike Silbersack #ifdef INVARIANTS
734121f0509SMike Silbersack 	trash_init(m->m_ext.ext_buf, MCLBYTES, how);
735121f0509SMike Silbersack #endif
736b23f72e9SBrian Feldman 	return (0);
737099a0e58SBosko Milekic }
738099a0e58SBosko Milekic 
739099a0e58SBosko Milekic /*
740099a0e58SBosko Milekic  * The Packet secondary zone's fini routine, executed on the
741099a0e58SBosko Milekic  * object's transition from zone cache to keg slab.
742099a0e58SBosko Milekic  */
743099a0e58SBosko Milekic static void
74456a4e45aSAndre Oppermann mb_zfini_pack(void *mem, int size)
745099a0e58SBosko Milekic {
746099a0e58SBosko Milekic 	struct mbuf *m;
747099a0e58SBosko Milekic 
748099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
749121f0509SMike Silbersack #ifdef INVARIANTS
750121f0509SMike Silbersack 	trash_fini(m->m_ext.ext_buf, MCLBYTES);
751121f0509SMike Silbersack #endif
752099a0e58SBosko Milekic 	uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
753a7b844d2SMike Silbersack #ifdef INVARIANTS
754a7b844d2SMike Silbersack 	trash_dtor(mem, size, NULL);
755a7b844d2SMike Silbersack #endif
756099a0e58SBosko Milekic }
757099a0e58SBosko Milekic 
758099a0e58SBosko Milekic /*
759099a0e58SBosko Milekic  * The "packet" keg constructor.
760099a0e58SBosko Milekic  */
761b23f72e9SBrian Feldman static int
762b23f72e9SBrian Feldman mb_ctor_pack(void *mem, int size, void *arg, int how)
763099a0e58SBosko Milekic {
764099a0e58SBosko Milekic 	struct mbuf *m;
765099a0e58SBosko Milekic 	struct mb_args *args;
766ce28636bSAndre Oppermann 	int error, flags;
767099a0e58SBosko Milekic 	short type;
768099a0e58SBosko Milekic 
769099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
770099a0e58SBosko Milekic 	args = (struct mb_args *)arg;
771099a0e58SBosko Milekic 	flags = args->flags;
772099a0e58SBosko Milekic 	type = args->type;
773fddd4f62SNavdeep Parhar 	MPASS((flags & M_NOFREE) == 0);
774099a0e58SBosko Milekic 
775121f0509SMike Silbersack #ifdef INVARIANTS
776121f0509SMike Silbersack 	trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
777121f0509SMike Silbersack #endif
778099a0e58SBosko Milekic 
779b4b12e52SGleb Smirnoff 	error = m_init(m, how, type, flags);
780afb295ccSAndre Oppermann 
78156a4e45aSAndre Oppermann 	/* m_ext is already initialized. */
782afb295ccSAndre Oppermann 	m->m_data = m->m_ext.ext_buf;
783afb295ccSAndre Oppermann  	m->m_flags = (flags | M_EXT);
78456a4e45aSAndre Oppermann 
785afb295ccSAndre Oppermann 	return (error);
786099a0e58SBosko Milekic }
787099a0e58SBosko Milekic 
788099a0e58SBosko Milekic /*
789e60b2fcbSGleb Smirnoff  * This is the protocol drain routine.  Called by UMA whenever any of the
790e60b2fcbSGleb Smirnoff  * mbuf zones is closed to its limit.
791099a0e58SBosko Milekic  *
792099a0e58SBosko Milekic  * No locks should be held when this is called.  The drain routines have to
793099a0e58SBosko Milekic  * presently acquire some locks which raises the possibility of lock order
794099a0e58SBosko Milekic  * reversal.
795099a0e58SBosko Milekic  */
796099a0e58SBosko Milekic static void
797e60b2fcbSGleb Smirnoff mb_reclaim(uma_zone_t zone __unused, int pending __unused)
798099a0e58SBosko Milekic {
799099a0e58SBosko Milekic 	struct domain *dp;
800099a0e58SBosko Milekic 	struct protosw *pr;
801099a0e58SBosko Milekic 
802e60b2fcbSGleb Smirnoff 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, __func__);
803099a0e58SBosko Milekic 
804099a0e58SBosko Milekic 	for (dp = domains; dp != NULL; dp = dp->dom_next)
805099a0e58SBosko Milekic 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
806099a0e58SBosko Milekic 			if (pr->pr_drain != NULL)
807099a0e58SBosko Milekic 				(*pr->pr_drain)();
808099a0e58SBosko Milekic }
8095e4bc63bSGleb Smirnoff 
8105e4bc63bSGleb Smirnoff /*
8115e4bc63bSGleb Smirnoff  * Clean up after mbufs with M_EXT storage attached to them if the
8125e4bc63bSGleb Smirnoff  * reference count hits 1.
8135e4bc63bSGleb Smirnoff  */
8145e4bc63bSGleb Smirnoff void
8155e4bc63bSGleb Smirnoff mb_free_ext(struct mbuf *m)
8165e4bc63bSGleb Smirnoff {
81756a5f52eSGleb Smirnoff 	volatile u_int *refcnt;
81856a5f52eSGleb Smirnoff 	struct mbuf *mref;
8195e4bc63bSGleb Smirnoff 	int freembuf;
8205e4bc63bSGleb Smirnoff 
8215e4bc63bSGleb Smirnoff 	KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m));
8225e4bc63bSGleb Smirnoff 
82356a5f52eSGleb Smirnoff 	/* See if this is the mbuf that holds the embedded refcount. */
82456a5f52eSGleb Smirnoff 	if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
82556a5f52eSGleb Smirnoff 		refcnt = &m->m_ext.ext_count;
82656a5f52eSGleb Smirnoff 		mref = m;
82756a5f52eSGleb Smirnoff 	} else {
82856a5f52eSGleb Smirnoff 		KASSERT(m->m_ext.ext_cnt != NULL,
82956a5f52eSGleb Smirnoff 		    ("%s: no refcounting pointer on %p", __func__, m));
83056a5f52eSGleb Smirnoff 		refcnt = m->m_ext.ext_cnt;
83156a5f52eSGleb Smirnoff 		mref = __containerof(refcnt, struct mbuf, m_ext.ext_count);
83256a5f52eSGleb Smirnoff 	}
83356a5f52eSGleb Smirnoff 
8345e4bc63bSGleb Smirnoff 	/*
83556a5f52eSGleb Smirnoff 	 * Check if the header is embedded in the cluster.  It is
83656a5f52eSGleb Smirnoff 	 * important that we can't touch any of the mbuf fields
83756a5f52eSGleb Smirnoff 	 * after we have freed the external storage, since mbuf
83817cd649fSGleb Smirnoff 	 * could have been embedded in it.  For now, the mbufs
83917cd649fSGleb Smirnoff 	 * embedded into the cluster are always of type EXT_EXTREF,
84017cd649fSGleb Smirnoff 	 * and for this type we won't free the mref.
8415e4bc63bSGleb Smirnoff 	 */
84217cd649fSGleb Smirnoff 	if (m->m_flags & M_NOFREE) {
84317cd649fSGleb Smirnoff 		freembuf = 0;
84417cd649fSGleb Smirnoff 		KASSERT(m->m_ext.ext_type == EXT_EXTREF,
84517cd649fSGleb Smirnoff 		    ("%s: no-free mbuf %p has wrong type", __func__, m));
84617cd649fSGleb Smirnoff 	} else
84717cd649fSGleb Smirnoff 		freembuf = 1;
8485e4bc63bSGleb Smirnoff 
84956a5f52eSGleb Smirnoff 	/* Free attached storage if this mbuf is the only reference to it. */
85056a5f52eSGleb Smirnoff 	if (*refcnt == 1 || atomic_fetchadd_int(refcnt, -1) == 1) {
8515e4bc63bSGleb Smirnoff 		switch (m->m_ext.ext_type) {
85256a5f52eSGleb Smirnoff 		case EXT_PACKET:
85356a5f52eSGleb Smirnoff 			/* The packet zone is special. */
85456a5f52eSGleb Smirnoff 			if (*refcnt == 0)
85556a5f52eSGleb Smirnoff 				*refcnt = 1;
85656a5f52eSGleb Smirnoff 			uma_zfree(zone_pack, mref);
8575e4bc63bSGleb Smirnoff 			break;
8585e4bc63bSGleb Smirnoff 		case EXT_CLUSTER:
8595e4bc63bSGleb Smirnoff 			uma_zfree(zone_clust, m->m_ext.ext_buf);
86056a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
8615e4bc63bSGleb Smirnoff 			break;
8625e4bc63bSGleb Smirnoff 		case EXT_JUMBOP:
8635e4bc63bSGleb Smirnoff 			uma_zfree(zone_jumbop, m->m_ext.ext_buf);
86456a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
8655e4bc63bSGleb Smirnoff 			break;
8665e4bc63bSGleb Smirnoff 		case EXT_JUMBO9:
8675e4bc63bSGleb Smirnoff 			uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
86856a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
8695e4bc63bSGleb Smirnoff 			break;
8705e4bc63bSGleb Smirnoff 		case EXT_JUMBO16:
8715e4bc63bSGleb Smirnoff 			uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
87256a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
87356a5f52eSGleb Smirnoff 			break;
87456a5f52eSGleb Smirnoff 		case EXT_SFBUF:
8755e4bc63bSGleb Smirnoff 		case EXT_NET_DRV:
8765e4bc63bSGleb Smirnoff 		case EXT_MOD_TYPE:
8775e4bc63bSGleb Smirnoff 		case EXT_DISPOSABLE:
87807e87a1dSGleb Smirnoff 			KASSERT(mref->m_ext.ext_free != NULL,
8790ea37a86SGleb Smirnoff 			    ("%s: ext_free not set", __func__));
88007e87a1dSGleb Smirnoff 			mref->m_ext.ext_free(mref);
88156a5f52eSGleb Smirnoff 			uma_zfree(zone_mbuf, mref);
8820ea37a86SGleb Smirnoff 			break;
8835e4bc63bSGleb Smirnoff 		case EXT_EXTREF:
8845e4bc63bSGleb Smirnoff 			KASSERT(m->m_ext.ext_free != NULL,
8855e4bc63bSGleb Smirnoff 			    ("%s: ext_free not set", __func__));
886e8fd18f3SGleb Smirnoff 			m->m_ext.ext_free(m);
8875e4bc63bSGleb Smirnoff 			break;
8885e4bc63bSGleb Smirnoff 		default:
8895e4bc63bSGleb Smirnoff 			KASSERT(m->m_ext.ext_type == 0,
8905e4bc63bSGleb Smirnoff 			    ("%s: unknown ext_type", __func__));
8915e4bc63bSGleb Smirnoff 		}
8925e4bc63bSGleb Smirnoff 	}
8935e4bc63bSGleb Smirnoff 
89456a5f52eSGleb Smirnoff 	if (freembuf && m != mref)
8955e4bc63bSGleb Smirnoff 		uma_zfree(zone_mbuf, m);
8965e4bc63bSGleb Smirnoff }
8975e4bc63bSGleb Smirnoff 
8985e4bc63bSGleb Smirnoff /*
8995e4bc63bSGleb Smirnoff  * Official mbuf(9) allocation KPI for stack and drivers:
9005e4bc63bSGleb Smirnoff  *
9015e4bc63bSGleb Smirnoff  * m_get()	- a single mbuf without any attachments, sys/mbuf.h.
9025e4bc63bSGleb Smirnoff  * m_gethdr()	- a single mbuf initialized as M_PKTHDR, sys/mbuf.h.
9035e4bc63bSGleb Smirnoff  * m_getcl()	- an mbuf + 2k cluster, sys/mbuf.h.
9045e4bc63bSGleb Smirnoff  * m_clget()	- attach cluster to already allocated mbuf.
9055e4bc63bSGleb Smirnoff  * m_cljget()	- attach jumbo cluster to already allocated mbuf.
9065e4bc63bSGleb Smirnoff  * m_get2()	- allocate minimum mbuf that would fit size argument.
9075e4bc63bSGleb Smirnoff  * m_getm2()	- allocate a chain of mbufs/clusters.
9085e4bc63bSGleb Smirnoff  * m_extadd()	- attach external cluster to mbuf.
9095e4bc63bSGleb Smirnoff  *
9105e4bc63bSGleb Smirnoff  * m_free()	- free single mbuf with its tags and ext, sys/mbuf.h.
9115e4bc63bSGleb Smirnoff  * m_freem()	- free chain of mbufs.
9125e4bc63bSGleb Smirnoff  */
9135e4bc63bSGleb Smirnoff 
9145e4bc63bSGleb Smirnoff int
9155e4bc63bSGleb Smirnoff m_clget(struct mbuf *m, int how)
9165e4bc63bSGleb Smirnoff {
9175e4bc63bSGleb Smirnoff 
9185e4bc63bSGleb Smirnoff 	KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
9195e4bc63bSGleb Smirnoff 	    __func__, m));
9205e4bc63bSGleb Smirnoff 	m->m_ext.ext_buf = (char *)NULL;
9215e4bc63bSGleb Smirnoff 	uma_zalloc_arg(zone_clust, m, how);
9225e4bc63bSGleb Smirnoff 	/*
9235e4bc63bSGleb Smirnoff 	 * On a cluster allocation failure, drain the packet zone and retry,
9245e4bc63bSGleb Smirnoff 	 * we might be able to loosen a few clusters up on the drain.
9255e4bc63bSGleb Smirnoff 	 */
9265e4bc63bSGleb Smirnoff 	if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) {
9275e4bc63bSGleb Smirnoff 		zone_drain(zone_pack);
9285e4bc63bSGleb Smirnoff 		uma_zalloc_arg(zone_clust, m, how);
9295e4bc63bSGleb Smirnoff 	}
930480f4e94SGeorge V. Neville-Neil 	MBUF_PROBE2(m__clget, m, how);
9315e4bc63bSGleb Smirnoff 	return (m->m_flags & M_EXT);
9325e4bc63bSGleb Smirnoff }
9335e4bc63bSGleb Smirnoff 
9345e4bc63bSGleb Smirnoff /*
9355e4bc63bSGleb Smirnoff  * m_cljget() is different from m_clget() as it can allocate clusters without
9365e4bc63bSGleb Smirnoff  * attaching them to an mbuf.  In that case the return value is the pointer
9375e4bc63bSGleb Smirnoff  * to the cluster of the requested size.  If an mbuf was specified, it gets
9385e4bc63bSGleb Smirnoff  * the cluster attached to it and the return value can be safely ignored.
9395e4bc63bSGleb Smirnoff  * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
9405e4bc63bSGleb Smirnoff  */
9415e4bc63bSGleb Smirnoff void *
9425e4bc63bSGleb Smirnoff m_cljget(struct mbuf *m, int how, int size)
9435e4bc63bSGleb Smirnoff {
9445e4bc63bSGleb Smirnoff 	uma_zone_t zone;
945480f4e94SGeorge V. Neville-Neil 	void *retval;
9465e4bc63bSGleb Smirnoff 
9475e4bc63bSGleb Smirnoff 	if (m != NULL) {
9485e4bc63bSGleb Smirnoff 		KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
9495e4bc63bSGleb Smirnoff 		    __func__, m));
9505e4bc63bSGleb Smirnoff 		m->m_ext.ext_buf = NULL;
9515e4bc63bSGleb Smirnoff 	}
9525e4bc63bSGleb Smirnoff 
9535e4bc63bSGleb Smirnoff 	zone = m_getzone(size);
954480f4e94SGeorge V. Neville-Neil 	retval = uma_zalloc_arg(zone, m, how);
955480f4e94SGeorge V. Neville-Neil 
956480f4e94SGeorge V. Neville-Neil 	MBUF_PROBE4(m__cljget, m, how, size, retval);
957480f4e94SGeorge V. Neville-Neil 
958480f4e94SGeorge V. Neville-Neil 	return (retval);
9595e4bc63bSGleb Smirnoff }
9605e4bc63bSGleb Smirnoff 
9615e4bc63bSGleb Smirnoff /*
9625e4bc63bSGleb Smirnoff  * m_get2() allocates minimum mbuf that would fit "size" argument.
9635e4bc63bSGleb Smirnoff  */
9645e4bc63bSGleb Smirnoff struct mbuf *
9655e4bc63bSGleb Smirnoff m_get2(int size, int how, short type, int flags)
9665e4bc63bSGleb Smirnoff {
9675e4bc63bSGleb Smirnoff 	struct mb_args args;
9685e4bc63bSGleb Smirnoff 	struct mbuf *m, *n;
9695e4bc63bSGleb Smirnoff 
9705e4bc63bSGleb Smirnoff 	args.flags = flags;
9715e4bc63bSGleb Smirnoff 	args.type = type;
9725e4bc63bSGleb Smirnoff 
9735e4bc63bSGleb Smirnoff 	if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0))
9745e4bc63bSGleb Smirnoff 		return (uma_zalloc_arg(zone_mbuf, &args, how));
9755e4bc63bSGleb Smirnoff 	if (size <= MCLBYTES)
9765e4bc63bSGleb Smirnoff 		return (uma_zalloc_arg(zone_pack, &args, how));
9775e4bc63bSGleb Smirnoff 
9785e4bc63bSGleb Smirnoff 	if (size > MJUMPAGESIZE)
9795e4bc63bSGleb Smirnoff 		return (NULL);
9805e4bc63bSGleb Smirnoff 
9815e4bc63bSGleb Smirnoff 	m = uma_zalloc_arg(zone_mbuf, &args, how);
9825e4bc63bSGleb Smirnoff 	if (m == NULL)
9835e4bc63bSGleb Smirnoff 		return (NULL);
9845e4bc63bSGleb Smirnoff 
9855e4bc63bSGleb Smirnoff 	n = uma_zalloc_arg(zone_jumbop, m, how);
9865e4bc63bSGleb Smirnoff 	if (n == NULL) {
9875e4bc63bSGleb Smirnoff 		uma_zfree(zone_mbuf, m);
9885e4bc63bSGleb Smirnoff 		return (NULL);
9895e4bc63bSGleb Smirnoff 	}
9905e4bc63bSGleb Smirnoff 
9915e4bc63bSGleb Smirnoff 	return (m);
9925e4bc63bSGleb Smirnoff }
9935e4bc63bSGleb Smirnoff 
9945e4bc63bSGleb Smirnoff /*
9955e4bc63bSGleb Smirnoff  * m_getjcl() returns an mbuf with a cluster of the specified size attached.
9965e4bc63bSGleb Smirnoff  * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
9975e4bc63bSGleb Smirnoff  */
9985e4bc63bSGleb Smirnoff struct mbuf *
9995e4bc63bSGleb Smirnoff m_getjcl(int how, short type, int flags, int size)
10005e4bc63bSGleb Smirnoff {
10015e4bc63bSGleb Smirnoff 	struct mb_args args;
10025e4bc63bSGleb Smirnoff 	struct mbuf *m, *n;
10035e4bc63bSGleb Smirnoff 	uma_zone_t zone;
10045e4bc63bSGleb Smirnoff 
10055e4bc63bSGleb Smirnoff 	if (size == MCLBYTES)
10065e4bc63bSGleb Smirnoff 		return m_getcl(how, type, flags);
10075e4bc63bSGleb Smirnoff 
10085e4bc63bSGleb Smirnoff 	args.flags = flags;
10095e4bc63bSGleb Smirnoff 	args.type = type;
10105e4bc63bSGleb Smirnoff 
10115e4bc63bSGleb Smirnoff 	m = uma_zalloc_arg(zone_mbuf, &args, how);
10125e4bc63bSGleb Smirnoff 	if (m == NULL)
10135e4bc63bSGleb Smirnoff 		return (NULL);
10145e4bc63bSGleb Smirnoff 
10155e4bc63bSGleb Smirnoff 	zone = m_getzone(size);
10165e4bc63bSGleb Smirnoff 	n = uma_zalloc_arg(zone, m, how);
10175e4bc63bSGleb Smirnoff 	if (n == NULL) {
10185e4bc63bSGleb Smirnoff 		uma_zfree(zone_mbuf, m);
10195e4bc63bSGleb Smirnoff 		return (NULL);
10205e4bc63bSGleb Smirnoff 	}
10215e4bc63bSGleb Smirnoff 	return (m);
10225e4bc63bSGleb Smirnoff }
10235e4bc63bSGleb Smirnoff 
10245e4bc63bSGleb Smirnoff /*
10255e4bc63bSGleb Smirnoff  * Allocate a given length worth of mbufs and/or clusters (whatever fits
10265e4bc63bSGleb Smirnoff  * best) and return a pointer to the top of the allocated chain.  If an
10275e4bc63bSGleb Smirnoff  * existing mbuf chain is provided, then we will append the new chain
10285e4bc63bSGleb Smirnoff  * to the existing one but still return the top of the newly allocated
10295e4bc63bSGleb Smirnoff  * chain.
10305e4bc63bSGleb Smirnoff  */
10315e4bc63bSGleb Smirnoff struct mbuf *
10325e4bc63bSGleb Smirnoff m_getm2(struct mbuf *m, int len, int how, short type, int flags)
10335e4bc63bSGleb Smirnoff {
10345e4bc63bSGleb Smirnoff 	struct mbuf *mb, *nm = NULL, *mtail = NULL;
10355e4bc63bSGleb Smirnoff 
10365e4bc63bSGleb Smirnoff 	KASSERT(len >= 0, ("%s: len is < 0", __func__));
10375e4bc63bSGleb Smirnoff 
10385e4bc63bSGleb Smirnoff 	/* Validate flags. */
10395e4bc63bSGleb Smirnoff 	flags &= (M_PKTHDR | M_EOR);
10405e4bc63bSGleb Smirnoff 
10415e4bc63bSGleb Smirnoff 	/* Packet header mbuf must be first in chain. */
10425e4bc63bSGleb Smirnoff 	if ((flags & M_PKTHDR) && m != NULL)
10435e4bc63bSGleb Smirnoff 		flags &= ~M_PKTHDR;
10445e4bc63bSGleb Smirnoff 
10455e4bc63bSGleb Smirnoff 	/* Loop and append maximum sized mbufs to the chain tail. */
10465e4bc63bSGleb Smirnoff 	while (len > 0) {
10475e4bc63bSGleb Smirnoff 		if (len > MCLBYTES)
10485e4bc63bSGleb Smirnoff 			mb = m_getjcl(how, type, (flags & M_PKTHDR),
10495e4bc63bSGleb Smirnoff 			    MJUMPAGESIZE);
10505e4bc63bSGleb Smirnoff 		else if (len >= MINCLSIZE)
10515e4bc63bSGleb Smirnoff 			mb = m_getcl(how, type, (flags & M_PKTHDR));
10525e4bc63bSGleb Smirnoff 		else if (flags & M_PKTHDR)
10535e4bc63bSGleb Smirnoff 			mb = m_gethdr(how, type);
10545e4bc63bSGleb Smirnoff 		else
10555e4bc63bSGleb Smirnoff 			mb = m_get(how, type);
10565e4bc63bSGleb Smirnoff 
10575e4bc63bSGleb Smirnoff 		/* Fail the whole operation if one mbuf can't be allocated. */
10585e4bc63bSGleb Smirnoff 		if (mb == NULL) {
10595e4bc63bSGleb Smirnoff 			if (nm != NULL)
10605e4bc63bSGleb Smirnoff 				m_freem(nm);
10615e4bc63bSGleb Smirnoff 			return (NULL);
10625e4bc63bSGleb Smirnoff 		}
10635e4bc63bSGleb Smirnoff 
10645e4bc63bSGleb Smirnoff 		/* Book keeping. */
10655e4bc63bSGleb Smirnoff 		len -= M_SIZE(mb);
10665e4bc63bSGleb Smirnoff 		if (mtail != NULL)
10675e4bc63bSGleb Smirnoff 			mtail->m_next = mb;
10685e4bc63bSGleb Smirnoff 		else
10695e4bc63bSGleb Smirnoff 			nm = mb;
10705e4bc63bSGleb Smirnoff 		mtail = mb;
10715e4bc63bSGleb Smirnoff 		flags &= ~M_PKTHDR;	/* Only valid on the first mbuf. */
10725e4bc63bSGleb Smirnoff 	}
10735e4bc63bSGleb Smirnoff 	if (flags & M_EOR)
10745e4bc63bSGleb Smirnoff 		mtail->m_flags |= M_EOR;  /* Only valid on the last mbuf. */
10755e4bc63bSGleb Smirnoff 
10765e4bc63bSGleb Smirnoff 	/* If mbuf was supplied, append new chain to the end of it. */
10775e4bc63bSGleb Smirnoff 	if (m != NULL) {
10785e4bc63bSGleb Smirnoff 		for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
10795e4bc63bSGleb Smirnoff 			;
10805e4bc63bSGleb Smirnoff 		mtail->m_next = nm;
10815e4bc63bSGleb Smirnoff 		mtail->m_flags &= ~M_EOR;
10825e4bc63bSGleb Smirnoff 	} else
10835e4bc63bSGleb Smirnoff 		m = nm;
10845e4bc63bSGleb Smirnoff 
10855e4bc63bSGleb Smirnoff 	return (m);
10865e4bc63bSGleb Smirnoff }
10875e4bc63bSGleb Smirnoff 
10885e4bc63bSGleb Smirnoff /*-
10895e4bc63bSGleb Smirnoff  * Configure a provided mbuf to refer to the provided external storage
109056a5f52eSGleb Smirnoff  * buffer and setup a reference count for said buffer.
10915e4bc63bSGleb Smirnoff  *
10925e4bc63bSGleb Smirnoff  * Arguments:
10935e4bc63bSGleb Smirnoff  *    mb     The existing mbuf to which to attach the provided buffer.
10945e4bc63bSGleb Smirnoff  *    buf    The address of the provided external storage buffer.
10955e4bc63bSGleb Smirnoff  *    size   The size of the provided buffer.
10965e4bc63bSGleb Smirnoff  *    freef  A pointer to a routine that is responsible for freeing the
10975e4bc63bSGleb Smirnoff  *           provided external storage buffer.
10985e4bc63bSGleb Smirnoff  *    args   A pointer to an argument structure (of any type) to be passed
10995e4bc63bSGleb Smirnoff  *           to the provided freef routine (may be NULL).
11005e4bc63bSGleb Smirnoff  *    flags  Any other flags to be passed to the provided mbuf.
11015e4bc63bSGleb Smirnoff  *    type   The type that the external storage buffer should be
11025e4bc63bSGleb Smirnoff  *           labeled with.
11035e4bc63bSGleb Smirnoff  *
11045e4bc63bSGleb Smirnoff  * Returns:
11055e4bc63bSGleb Smirnoff  *    Nothing.
11065e4bc63bSGleb Smirnoff  */
110756a5f52eSGleb Smirnoff void
1108e8fd18f3SGleb Smirnoff m_extadd(struct mbuf *mb, char *buf, u_int size, m_ext_free_t freef,
1109e8fd18f3SGleb Smirnoff     void *arg1, void *arg2, int flags, int type)
11105e4bc63bSGleb Smirnoff {
111156a5f52eSGleb Smirnoff 
11125e4bc63bSGleb Smirnoff 	KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
11135e4bc63bSGleb Smirnoff 
11145e4bc63bSGleb Smirnoff 	mb->m_flags |= (M_EXT | flags);
11155e4bc63bSGleb Smirnoff 	mb->m_ext.ext_buf = buf;
11165e4bc63bSGleb Smirnoff 	mb->m_data = mb->m_ext.ext_buf;
11175e4bc63bSGleb Smirnoff 	mb->m_ext.ext_size = size;
11185e4bc63bSGleb Smirnoff 	mb->m_ext.ext_free = freef;
11195e4bc63bSGleb Smirnoff 	mb->m_ext.ext_arg1 = arg1;
11205e4bc63bSGleb Smirnoff 	mb->m_ext.ext_arg2 = arg2;
11215e4bc63bSGleb Smirnoff 	mb->m_ext.ext_type = type;
11225e4bc63bSGleb Smirnoff 
112356a5f52eSGleb Smirnoff 	if (type != EXT_EXTREF) {
112456a5f52eSGleb Smirnoff 		mb->m_ext.ext_count = 1;
112556a5f52eSGleb Smirnoff 		mb->m_ext.ext_flags = EXT_FLAG_EMBREF;
112656a5f52eSGleb Smirnoff 	} else
112756a5f52eSGleb Smirnoff 		mb->m_ext.ext_flags = 0;
11285e4bc63bSGleb Smirnoff }
11295e4bc63bSGleb Smirnoff 
11305e4bc63bSGleb Smirnoff /*
11315e4bc63bSGleb Smirnoff  * Free an entire chain of mbufs and associated external buffers, if
11325e4bc63bSGleb Smirnoff  * applicable.
11335e4bc63bSGleb Smirnoff  */
11345e4bc63bSGleb Smirnoff void
11355e4bc63bSGleb Smirnoff m_freem(struct mbuf *mb)
11365e4bc63bSGleb Smirnoff {
11375e4bc63bSGleb Smirnoff 
1138c8f59118SGleb Smirnoff 	MBUF_PROBE1(m__freem, mb);
11395e4bc63bSGleb Smirnoff 	while (mb != NULL)
11405e4bc63bSGleb Smirnoff 		mb = m_free(mb);
11415e4bc63bSGleb Smirnoff }
1142