xref: /freebsd/sys/kern/kern_mbuf.c (revision 6e0b674628eaf76ab20933e9df494d4059426d89)
1099a0e58SBosko Milekic /*-
28076cb52SBosko Milekic  * Copyright (c) 2004, 2005,
38076cb52SBosko Milekic  *	Bosko Milekic <bmilekic@FreeBSD.org>.  All rights reserved.
4099a0e58SBosko Milekic  *
5099a0e58SBosko Milekic  * Redistribution and use in source and binary forms, with or without
6099a0e58SBosko Milekic  * modification, are permitted provided that the following conditions
7099a0e58SBosko Milekic  * are met:
8099a0e58SBosko Milekic  * 1. Redistributions of source code must retain the above copyright
9099a0e58SBosko Milekic  *    notice unmodified, this list of conditions and the following
10099a0e58SBosko Milekic  *    disclaimer.
11099a0e58SBosko Milekic  * 2. Redistributions in binary form must reproduce the above copyright
12099a0e58SBosko Milekic  *    notice, this list of conditions and the following disclaimer in the
13099a0e58SBosko Milekic  *    documentation and/or other materials provided with the distribution.
14099a0e58SBosko Milekic  *
15099a0e58SBosko Milekic  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16099a0e58SBosko Milekic  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17099a0e58SBosko Milekic  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18099a0e58SBosko Milekic  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19099a0e58SBosko Milekic  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20099a0e58SBosko Milekic  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21099a0e58SBosko Milekic  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22099a0e58SBosko Milekic  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23099a0e58SBosko Milekic  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24099a0e58SBosko Milekic  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25099a0e58SBosko Milekic  * SUCH DAMAGE.
26099a0e58SBosko Milekic  */
27099a0e58SBosko Milekic 
28099a0e58SBosko Milekic #include <sys/cdefs.h>
29099a0e58SBosko Milekic __FBSDID("$FreeBSD$");
30099a0e58SBosko Milekic 
31099a0e58SBosko Milekic #include "opt_param.h"
32099a0e58SBosko Milekic 
33099a0e58SBosko Milekic #include <sys/param.h>
34099a0e58SBosko Milekic #include <sys/malloc.h>
35099a0e58SBosko Milekic #include <sys/systm.h>
36099a0e58SBosko Milekic #include <sys/mbuf.h>
37099a0e58SBosko Milekic #include <sys/domain.h>
38099a0e58SBosko Milekic #include <sys/eventhandler.h>
39099a0e58SBosko Milekic #include <sys/kernel.h>
40099a0e58SBosko Milekic #include <sys/protosw.h>
41099a0e58SBosko Milekic #include <sys/smp.h>
42099a0e58SBosko Milekic #include <sys/sysctl.h>
43099a0e58SBosko Milekic 
44aed55708SRobert Watson #include <security/mac/mac_framework.h>
45aed55708SRobert Watson 
46099a0e58SBosko Milekic #include <vm/vm.h>
47c45c0034SAlan Cox #include <vm/vm_extern.h>
48c45c0034SAlan Cox #include <vm/vm_kern.h>
49099a0e58SBosko Milekic #include <vm/vm_page.h>
50099a0e58SBosko Milekic #include <vm/uma.h>
51121f0509SMike Silbersack #include <vm/uma_int.h>
52121f0509SMike Silbersack #include <vm/uma_dbg.h>
53099a0e58SBosko Milekic 
54099a0e58SBosko Milekic /*
55099a0e58SBosko Milekic  * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
56099a0e58SBosko Milekic  * Zones.
57099a0e58SBosko Milekic  *
58099a0e58SBosko Milekic  * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
59099a0e58SBosko Milekic  * Zone.  The Zone can be capped at kern.ipc.nmbclusters, if the
60099a0e58SBosko Milekic  * administrator so desires.
61099a0e58SBosko Milekic  *
62099a0e58SBosko Milekic  * Mbufs are allocated from a UMA Master Zone called the Mbuf
63099a0e58SBosko Milekic  * Zone.
64099a0e58SBosko Milekic  *
65099a0e58SBosko Milekic  * Additionally, FreeBSD provides a Packet Zone, which it
66099a0e58SBosko Milekic  * configures as a Secondary Zone to the Mbuf Master Zone,
67099a0e58SBosko Milekic  * thus sharing backend Slab kegs with the Mbuf Master Zone.
68099a0e58SBosko Milekic  *
69099a0e58SBosko Milekic  * Thus common-case allocations and locking are simplified:
70099a0e58SBosko Milekic  *
71099a0e58SBosko Milekic  *  m_clget()                m_getcl()
72099a0e58SBosko Milekic  *    |                         |
73099a0e58SBosko Milekic  *    |   .------------>[(Packet Cache)]    m_get(), m_gethdr()
74099a0e58SBosko Milekic  *    |   |             [     Packet   ]            |
75099a0e58SBosko Milekic  *  [(Cluster Cache)]   [    Secondary ]   [ (Mbuf Cache)     ]
76099a0e58SBosko Milekic  *  [ Cluster Zone  ]   [     Zone     ]   [ Mbuf Master Zone ]
77099a0e58SBosko Milekic  *        |                       \________         |
78099a0e58SBosko Milekic  *  [ Cluster Keg   ]                      \       /
79099a0e58SBosko Milekic  *        |	                         [ Mbuf Keg   ]
80099a0e58SBosko Milekic  *  [ Cluster Slabs ]                         |
81099a0e58SBosko Milekic  *        |                              [ Mbuf Slabs ]
82099a0e58SBosko Milekic  *         \____________(VM)_________________/
8356a4e45aSAndre Oppermann  *
8456a4e45aSAndre Oppermann  *
85fcf90618SGleb Smirnoff  * Whenever an object is allocated with uma_zalloc() out of
8656a4e45aSAndre Oppermann  * one of the Zones its _ctor_ function is executed.  The same
87fcf90618SGleb Smirnoff  * for any deallocation through uma_zfree() the _dtor_ function
8856a4e45aSAndre Oppermann  * is executed.
8956a4e45aSAndre Oppermann  *
9056a4e45aSAndre Oppermann  * Caches are per-CPU and are filled from the Master Zone.
9156a4e45aSAndre Oppermann  *
92fcf90618SGleb Smirnoff  * Whenever an object is allocated from the underlying global
9356a4e45aSAndre Oppermann  * memory pool it gets pre-initialized with the _zinit_ functions.
9456a4e45aSAndre Oppermann  * When the Keg's are overfull objects get decomissioned with
9556a4e45aSAndre Oppermann  * _zfini_ functions and free'd back to the global memory pool.
9656a4e45aSAndre Oppermann  *
97099a0e58SBosko Milekic  */
98099a0e58SBosko Milekic 
99ead46972SAndre Oppermann int nmbufs;			/* limits number of mbufs */
10056a4e45aSAndre Oppermann int nmbclusters;		/* limits number of mbuf clusters */
101ec63cb90SAndre Oppermann int nmbjumbop;			/* limits number of page size jumbo clusters */
10256a4e45aSAndre Oppermann int nmbjumbo9;			/* limits number of 9k jumbo clusters */
10356a4e45aSAndre Oppermann int nmbjumbo16;			/* limits number of 16k jumbo clusters */
104099a0e58SBosko Milekic struct mbstat mbstat;
105099a0e58SBosko Milekic 
10662938659SBjoern A. Zeeb /*
10762938659SBjoern A. Zeeb  * tunable_mbinit() has to be run before init_maxsockets() thus
10862938659SBjoern A. Zeeb  * the SYSINIT order below is SI_ORDER_MIDDLE while init_maxsockets()
10962938659SBjoern A. Zeeb  * runs at SI_ORDER_ANY.
110416a434cSAndre Oppermann  *
111416a434cSAndre Oppermann  * NB: This has to be done before VM init.
11262938659SBjoern A. Zeeb  */
113099a0e58SBosko Milekic static void
114099a0e58SBosko Milekic tunable_mbinit(void *dummy)
115099a0e58SBosko Milekic {
116099a0e58SBosko Milekic 
117812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
118416a434cSAndre Oppermann 	if (nmbclusters == 0)
119416a434cSAndre Oppermann 		nmbclusters = maxmbufmem / MCLBYTES / 4;
120812302c3SNavdeep Parhar 
121812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop);
122812302c3SNavdeep Parhar 	if (nmbjumbop == 0)
123416a434cSAndre Oppermann 		nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4;
124812302c3SNavdeep Parhar 
125812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbjumbo9", &nmbjumbo9);
126812302c3SNavdeep Parhar 	if (nmbjumbo9 == 0)
127416a434cSAndre Oppermann 		nmbjumbo9 = maxmbufmem / MJUM9BYTES / 6;
128812302c3SNavdeep Parhar 
129812302c3SNavdeep Parhar 	TUNABLE_INT_FETCH("kern.ipc.nmbjumbo16", &nmbjumbo16);
130812302c3SNavdeep Parhar 	if (nmbjumbo16 == 0)
131416a434cSAndre Oppermann 		nmbjumbo16 = maxmbufmem / MJUM16BYTES / 6;
132416a434cSAndre Oppermann 
133416a434cSAndre Oppermann 	/*
134416a434cSAndre Oppermann 	 * We need at least as many mbufs as we have clusters of
135416a434cSAndre Oppermann 	 * the various types added together.
136416a434cSAndre Oppermann 	 */
137416a434cSAndre Oppermann 	TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
138416a434cSAndre Oppermann 	if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16)
139416a434cSAndre Oppermann 		nmbufs = lmax(maxmbufmem / MSIZE / 5,
140416a434cSAndre Oppermann 		    nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16);
141099a0e58SBosko Milekic }
14262938659SBjoern A. Zeeb SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
143099a0e58SBosko Milekic 
1444f590175SPaul Saab static int
1454f590175SPaul Saab sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
1464f590175SPaul Saab {
1474f590175SPaul Saab 	int error, newnmbclusters;
1484f590175SPaul Saab 
1494f590175SPaul Saab 	newnmbclusters = nmbclusters;
150041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
1514f590175SPaul Saab 	if (error == 0 && req->newptr) {
152ead46972SAndre Oppermann 		if (newnmbclusters > nmbclusters &&
153ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
1544f590175SPaul Saab 			nmbclusters = newnmbclusters;
1554f590175SPaul Saab 			uma_zone_set_max(zone_clust, nmbclusters);
156ead46972SAndre Oppermann 			nmbclusters = uma_zone_get_max(zone_clust);
1574f590175SPaul Saab 			EVENTHANDLER_INVOKE(nmbclusters_change);
1584f590175SPaul Saab 		} else
1594f590175SPaul Saab 			error = EINVAL;
1604f590175SPaul Saab 	}
1614f590175SPaul Saab 	return (error);
1624f590175SPaul Saab }
1634f590175SPaul Saab SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
1644f590175SPaul Saab &nmbclusters, 0, sysctl_nmbclusters, "IU",
165099a0e58SBosko Milekic     "Maximum number of mbuf clusters allowed");
166cf70a46bSRandall Stewart 
167cf70a46bSRandall Stewart static int
168cf70a46bSRandall Stewart sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS)
169cf70a46bSRandall Stewart {
170cf70a46bSRandall Stewart 	int error, newnmbjumbop;
171cf70a46bSRandall Stewart 
172cf70a46bSRandall Stewart 	newnmbjumbop = nmbjumbop;
173cf70a46bSRandall Stewart 	error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req);
174cf70a46bSRandall Stewart 	if (error == 0 && req->newptr) {
175ead46972SAndre Oppermann 		if (newnmbjumbop > nmbjumbop &&
176ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
177cf70a46bSRandall Stewart 			nmbjumbop = newnmbjumbop;
178cf70a46bSRandall Stewart 			uma_zone_set_max(zone_jumbop, nmbjumbop);
179ead46972SAndre Oppermann 			nmbjumbop = uma_zone_get_max(zone_jumbop);
180cf70a46bSRandall Stewart 		} else
181cf70a46bSRandall Stewart 			error = EINVAL;
182cf70a46bSRandall Stewart 	}
183cf70a46bSRandall Stewart 	return (error);
184cf70a46bSRandall Stewart }
185cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW,
186cf70a46bSRandall Stewart &nmbjumbop, 0, sysctl_nmbjumbop, "IU",
187ec63cb90SAndre Oppermann     "Maximum number of mbuf page size jumbo clusters allowed");
188cf70a46bSRandall Stewart 
189cf70a46bSRandall Stewart static int
190cf70a46bSRandall Stewart sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS)
191cf70a46bSRandall Stewart {
192cf70a46bSRandall Stewart 	int error, newnmbjumbo9;
193cf70a46bSRandall Stewart 
194cf70a46bSRandall Stewart 	newnmbjumbo9 = nmbjumbo9;
195cf70a46bSRandall Stewart 	error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req);
196cf70a46bSRandall Stewart 	if (error == 0 && req->newptr) {
197ead46972SAndre Oppermann 		if (newnmbjumbo9 > nmbjumbo9&&
198ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
199cf70a46bSRandall Stewart 			nmbjumbo9 = newnmbjumbo9;
200cf70a46bSRandall Stewart 			uma_zone_set_max(zone_jumbo9, nmbjumbo9);
201ead46972SAndre Oppermann 			nmbjumbo9 = uma_zone_get_max(zone_jumbo9);
202cf70a46bSRandall Stewart 		} else
203cf70a46bSRandall Stewart 			error = EINVAL;
204cf70a46bSRandall Stewart 	}
205cf70a46bSRandall Stewart 	return (error);
206cf70a46bSRandall Stewart }
207cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW,
208cf70a46bSRandall Stewart &nmbjumbo9, 0, sysctl_nmbjumbo9, "IU",
20956a4e45aSAndre Oppermann     "Maximum number of mbuf 9k jumbo clusters allowed");
210cf70a46bSRandall Stewart 
211cf70a46bSRandall Stewart static int
212cf70a46bSRandall Stewart sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS)
213cf70a46bSRandall Stewart {
214cf70a46bSRandall Stewart 	int error, newnmbjumbo16;
215cf70a46bSRandall Stewart 
216cf70a46bSRandall Stewart 	newnmbjumbo16 = nmbjumbo16;
217cf70a46bSRandall Stewart 	error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req);
218cf70a46bSRandall Stewart 	if (error == 0 && req->newptr) {
219ead46972SAndre Oppermann 		if (newnmbjumbo16 > nmbjumbo16 &&
220ead46972SAndre Oppermann 		    nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
221cf70a46bSRandall Stewart 			nmbjumbo16 = newnmbjumbo16;
222cf70a46bSRandall Stewart 			uma_zone_set_max(zone_jumbo16, nmbjumbo16);
223ead46972SAndre Oppermann 			nmbjumbo16 = uma_zone_get_max(zone_jumbo16);
224cf70a46bSRandall Stewart 		} else
225cf70a46bSRandall Stewart 			error = EINVAL;
226cf70a46bSRandall Stewart 	}
227cf70a46bSRandall Stewart 	return (error);
228cf70a46bSRandall Stewart }
229cf70a46bSRandall Stewart SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW,
230cf70a46bSRandall Stewart &nmbjumbo16, 0, sysctl_nmbjumbo16, "IU",
23156a4e45aSAndre Oppermann     "Maximum number of mbuf 16k jumbo clusters allowed");
232cf70a46bSRandall Stewart 
233ead46972SAndre Oppermann static int
234ead46972SAndre Oppermann sysctl_nmbufs(SYSCTL_HANDLER_ARGS)
235ead46972SAndre Oppermann {
236ead46972SAndre Oppermann 	int error, newnmbufs;
237ead46972SAndre Oppermann 
238ead46972SAndre Oppermann 	newnmbufs = nmbufs;
239ead46972SAndre Oppermann 	error = sysctl_handle_int(oidp, &newnmbufs, 0, req);
240ead46972SAndre Oppermann 	if (error == 0 && req->newptr) {
241ead46972SAndre Oppermann 		if (newnmbufs > nmbufs) {
242ead46972SAndre Oppermann 			nmbufs = newnmbufs;
243ead46972SAndre Oppermann 			uma_zone_set_max(zone_mbuf, nmbufs);
244416a434cSAndre Oppermann 			nmbufs = uma_zone_get_max(zone_mbuf);
245ead46972SAndre Oppermann 			EVENTHANDLER_INVOKE(nmbufs_change);
246ead46972SAndre Oppermann 		} else
247ead46972SAndre Oppermann 			error = EINVAL;
248ead46972SAndre Oppermann 	}
249ead46972SAndre Oppermann 	return (error);
250ead46972SAndre Oppermann }
251ead46972SAndre Oppermann SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbuf, CTLTYPE_INT|CTLFLAG_RW,
252ead46972SAndre Oppermann &nmbufs, 0, sysctl_nmbufs, "IU",
253ead46972SAndre Oppermann     "Maximum number of mbufs allowed");
254cf70a46bSRandall Stewart 
255099a0e58SBosko Milekic SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
256099a0e58SBosko Milekic     "Mbuf general information and statistics");
257099a0e58SBosko Milekic 
258099a0e58SBosko Milekic /*
259099a0e58SBosko Milekic  * Zones from which we allocate.
260099a0e58SBosko Milekic  */
261099a0e58SBosko Milekic uma_zone_t	zone_mbuf;
262099a0e58SBosko Milekic uma_zone_t	zone_clust;
263099a0e58SBosko Milekic uma_zone_t	zone_pack;
264ec63cb90SAndre Oppermann uma_zone_t	zone_jumbop;
26556a4e45aSAndre Oppermann uma_zone_t	zone_jumbo9;
26656a4e45aSAndre Oppermann uma_zone_t	zone_jumbo16;
26756a4e45aSAndre Oppermann uma_zone_t	zone_ext_refcnt;
268099a0e58SBosko Milekic 
269099a0e58SBosko Milekic /*
270099a0e58SBosko Milekic  * Local prototypes.
271099a0e58SBosko Milekic  */
272b23f72e9SBrian Feldman static int	mb_ctor_mbuf(void *, int, void *, int);
273b23f72e9SBrian Feldman static int	mb_ctor_clust(void *, int, void *, int);
274b23f72e9SBrian Feldman static int	mb_ctor_pack(void *, int, void *, int);
275099a0e58SBosko Milekic static void	mb_dtor_mbuf(void *, int, void *);
27656a4e45aSAndre Oppermann static void	mb_dtor_clust(void *, int, void *);
27756a4e45aSAndre Oppermann static void	mb_dtor_pack(void *, int, void *);
27856a4e45aSAndre Oppermann static int	mb_zinit_pack(void *, int, int);
27956a4e45aSAndre Oppermann static void	mb_zfini_pack(void *, int);
280099a0e58SBosko Milekic 
281099a0e58SBosko Milekic static void	mb_reclaim(void *);
282099a0e58SBosko Milekic static void	mbuf_init(void *);
28360ae52f7SEd Schouten static void    *mbuf_jumbo_alloc(uma_zone_t, int, uint8_t *, int);
284099a0e58SBosko Milekic 
285a2c36a02SKevin Lo /* Ensure that MSIZE must be a power of 2. */
286a04946cfSBrian Somers CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
287a04946cfSBrian Somers 
288099a0e58SBosko Milekic /*
289099a0e58SBosko Milekic  * Initialize FreeBSD Network buffer allocation.
290099a0e58SBosko Milekic  */
291237fdd78SRobert Watson SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
292099a0e58SBosko Milekic static void
293099a0e58SBosko Milekic mbuf_init(void *dummy)
294099a0e58SBosko Milekic {
295099a0e58SBosko Milekic 
296099a0e58SBosko Milekic 	/*
297099a0e58SBosko Milekic 	 * Configure UMA zones for Mbufs, Clusters, and Packets.
298099a0e58SBosko Milekic 	 */
29956a4e45aSAndre Oppermann 	zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
30056a4e45aSAndre Oppermann 	    mb_ctor_mbuf, mb_dtor_mbuf,
301121f0509SMike Silbersack #ifdef INVARIANTS
30256a4e45aSAndre Oppermann 	    trash_init, trash_fini,
303121f0509SMike Silbersack #else
30456a4e45aSAndre Oppermann 	    NULL, NULL,
305121f0509SMike Silbersack #endif
30656a4e45aSAndre Oppermann 	    MSIZE - 1, UMA_ZONE_MAXBUCKET);
30745fe0bf7SPawel Jakub Dawidek 	if (nmbufs > 0)
30845fe0bf7SPawel Jakub Dawidek 		nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
309*6e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_mbuf, "kern.ipc.nmbufs limit reached");
31056a4e45aSAndre Oppermann 
31168352adfSRobert Watson 	zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
31256a4e45aSAndre Oppermann 	    mb_ctor_clust, mb_dtor_clust,
313121f0509SMike Silbersack #ifdef INVARIANTS
31456a4e45aSAndre Oppermann 	    trash_init, trash_fini,
315121f0509SMike Silbersack #else
31656a4e45aSAndre Oppermann 	    NULL, NULL,
317121f0509SMike Silbersack #endif
31856a4e45aSAndre Oppermann 	    UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
31945fe0bf7SPawel Jakub Dawidek 	if (nmbclusters > 0)
32045fe0bf7SPawel Jakub Dawidek 		nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
321*6e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_clust, "kern.ipc.nmbclusters limit reached");
322099a0e58SBosko Milekic 
32356a4e45aSAndre Oppermann 	zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
32456a4e45aSAndre Oppermann 	    mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
32556a4e45aSAndre Oppermann 
326fcf90618SGleb Smirnoff 	/* Make jumbo frame zone too. Page size, 9k and 16k. */
327ec63cb90SAndre Oppermann 	zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
328d5269a63SAndre Oppermann 	    mb_ctor_clust, mb_dtor_clust,
329d5269a63SAndre Oppermann #ifdef INVARIANTS
330d5269a63SAndre Oppermann 	    trash_init, trash_fini,
331d5269a63SAndre Oppermann #else
332d5269a63SAndre Oppermann 	    NULL, NULL,
333d5269a63SAndre Oppermann #endif
334d5269a63SAndre Oppermann 	    UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
33545fe0bf7SPawel Jakub Dawidek 	if (nmbjumbop > 0)
33645fe0bf7SPawel Jakub Dawidek 		nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
337*6e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_jumbop, "kern.ipc.nmbjumbop limit reached");
338d5269a63SAndre Oppermann 
33956a4e45aSAndre Oppermann 	zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
34056a4e45aSAndre Oppermann 	    mb_ctor_clust, mb_dtor_clust,
34156a4e45aSAndre Oppermann #ifdef INVARIANTS
34256a4e45aSAndre Oppermann 	    trash_init, trash_fini,
34356a4e45aSAndre Oppermann #else
34456a4e45aSAndre Oppermann 	    NULL, NULL,
34556a4e45aSAndre Oppermann #endif
34656a4e45aSAndre Oppermann 	    UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
347ba63339aSAlan Cox 	uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc);
34845fe0bf7SPawel Jakub Dawidek 	if (nmbjumbo9 > 0)
34945fe0bf7SPawel Jakub Dawidek 		nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
350*6e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_jumbo9, "kern.ipc.nmbjumbo9 limit reached");
35156a4e45aSAndre Oppermann 
35256a4e45aSAndre Oppermann 	zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
35356a4e45aSAndre Oppermann 	    mb_ctor_clust, mb_dtor_clust,
35456a4e45aSAndre Oppermann #ifdef INVARIANTS
35556a4e45aSAndre Oppermann 	    trash_init, trash_fini,
35656a4e45aSAndre Oppermann #else
35756a4e45aSAndre Oppermann 	    NULL, NULL,
35856a4e45aSAndre Oppermann #endif
35956a4e45aSAndre Oppermann 	    UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
360ba63339aSAlan Cox 	uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc);
36145fe0bf7SPawel Jakub Dawidek 	if (nmbjumbo16 > 0)
36245fe0bf7SPawel Jakub Dawidek 		nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
363*6e0b6746SPawel Jakub Dawidek 	uma_zone_set_warning(zone_jumbo16, "kern.ipc.nmbjumbo16 limit reached");
36456a4e45aSAndre Oppermann 
36556a4e45aSAndre Oppermann 	zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int),
36656a4e45aSAndre Oppermann 	    NULL, NULL,
36756a4e45aSAndre Oppermann 	    NULL, NULL,
36856a4e45aSAndre Oppermann 	    UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
36956a4e45aSAndre Oppermann 
37056a4e45aSAndre Oppermann 	/* uma_prealloc() goes here... */
371099a0e58SBosko Milekic 
372099a0e58SBosko Milekic 	/*
373099a0e58SBosko Milekic 	 * Hook event handler for low-memory situation, used to
374099a0e58SBosko Milekic 	 * drain protocols and push data back to the caches (UMA
375099a0e58SBosko Milekic 	 * later pushes it back to VM).
376099a0e58SBosko Milekic 	 */
377099a0e58SBosko Milekic 	EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
378099a0e58SBosko Milekic 	    EVENTHANDLER_PRI_FIRST);
379099a0e58SBosko Milekic 
380099a0e58SBosko Milekic 	/*
381099a0e58SBosko Milekic 	 * [Re]set counters and local statistics knobs.
382099a0e58SBosko Milekic 	 * XXX Some of these should go and be replaced, but UMA stat
383099a0e58SBosko Milekic 	 * gathering needs to be revised.
384099a0e58SBosko Milekic 	 */
385099a0e58SBosko Milekic 	mbstat.m_mbufs = 0;
386099a0e58SBosko Milekic 	mbstat.m_mclusts = 0;
387099a0e58SBosko Milekic 	mbstat.m_drain = 0;
388099a0e58SBosko Milekic 	mbstat.m_msize = MSIZE;
389099a0e58SBosko Milekic 	mbstat.m_mclbytes = MCLBYTES;
390099a0e58SBosko Milekic 	mbstat.m_minclsize = MINCLSIZE;
391099a0e58SBosko Milekic 	mbstat.m_mlen = MLEN;
392099a0e58SBosko Milekic 	mbstat.m_mhlen = MHLEN;
393099a0e58SBosko Milekic 	mbstat.m_numtypes = MT_NTYPES;
394099a0e58SBosko Milekic 
395099a0e58SBosko Milekic 	mbstat.m_mcfail = mbstat.m_mpfail = 0;
396099a0e58SBosko Milekic 	mbstat.sf_iocnt = 0;
397099a0e58SBosko Milekic 	mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
398099a0e58SBosko Milekic }
399099a0e58SBosko Milekic 
400099a0e58SBosko Milekic /*
401ba63339aSAlan Cox  * UMA backend page allocator for the jumbo frame zones.
402ba63339aSAlan Cox  *
403ba63339aSAlan Cox  * Allocates kernel virtual memory that is backed by contiguous physical
404ba63339aSAlan Cox  * pages.
405ba63339aSAlan Cox  */
406ba63339aSAlan Cox static void *
40760ae52f7SEd Schouten mbuf_jumbo_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait)
408ba63339aSAlan Cox {
409ba63339aSAlan Cox 
4107630c265SAlan Cox 	/* Inform UMA that this allocator uses kernel_map/object. */
4117630c265SAlan Cox 	*flags = UMA_SLAB_KERNEL;
412c45c0034SAlan Cox 	return ((void *)kmem_alloc_contig(kernel_map, bytes, wait,
4133153e878SAlan Cox 	    (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
414ba63339aSAlan Cox }
415ba63339aSAlan Cox 
416ba63339aSAlan Cox /*
417099a0e58SBosko Milekic  * Constructor for Mbuf master zone.
418099a0e58SBosko Milekic  *
419099a0e58SBosko Milekic  * The 'arg' pointer points to a mb_args structure which
420099a0e58SBosko Milekic  * contains call-specific information required to support the
42156a4e45aSAndre Oppermann  * mbuf allocation API.  See mbuf.h.
422099a0e58SBosko Milekic  */
423b23f72e9SBrian Feldman static int
424b23f72e9SBrian Feldman mb_ctor_mbuf(void *mem, int size, void *arg, int how)
425099a0e58SBosko Milekic {
426099a0e58SBosko Milekic 	struct mbuf *m;
427099a0e58SBosko Milekic 	struct mb_args *args;
428b23f72e9SBrian Feldman #ifdef MAC
429b23f72e9SBrian Feldman 	int error;
430b23f72e9SBrian Feldman #endif
431099a0e58SBosko Milekic 	int flags;
432099a0e58SBosko Milekic 	short type;
433099a0e58SBosko Milekic 
434121f0509SMike Silbersack #ifdef INVARIANTS
435121f0509SMike Silbersack 	trash_ctor(mem, size, arg, how);
436121f0509SMike Silbersack #endif
437099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
438099a0e58SBosko Milekic 	args = (struct mb_args *)arg;
439099a0e58SBosko Milekic 	flags = args->flags;
440099a0e58SBosko Milekic 	type = args->type;
441099a0e58SBosko Milekic 
44256a4e45aSAndre Oppermann 	/*
44356a4e45aSAndre Oppermann 	 * The mbuf is initialized later.  The caller has the
444fcf90618SGleb Smirnoff 	 * responsibility to set up any MAC labels too.
44556a4e45aSAndre Oppermann 	 */
44656a4e45aSAndre Oppermann 	if (type == MT_NOINIT)
44756a4e45aSAndre Oppermann 		return (0);
44856a4e45aSAndre Oppermann 
449099a0e58SBosko Milekic 	m->m_next = NULL;
450099a0e58SBosko Milekic 	m->m_nextpkt = NULL;
45156a4e45aSAndre Oppermann 	m->m_len = 0;
4526bc72ab9SBosko Milekic 	m->m_flags = flags;
45356a4e45aSAndre Oppermann 	m->m_type = type;
454099a0e58SBosko Milekic 	if (flags & M_PKTHDR) {
455099a0e58SBosko Milekic 		m->m_data = m->m_pktdat;
456099a0e58SBosko Milekic 		m->m_pkthdr.rcvif = NULL;
45756a4e45aSAndre Oppermann 		m->m_pkthdr.header = NULL;
4588aa7a581SKip Macy 		m->m_pkthdr.len = 0;
459099a0e58SBosko Milekic 		m->m_pkthdr.csum_flags = 0;
46056a4e45aSAndre Oppermann 		m->m_pkthdr.csum_data = 0;
461a855e2b4SAndre Oppermann 		m->m_pkthdr.tso_segsz = 0;
462a855e2b4SAndre Oppermann 		m->m_pkthdr.ether_vtag = 0;
463877e8812SRobert Watson 		m->m_pkthdr.flowid = 0;
464099a0e58SBosko Milekic 		SLIST_INIT(&m->m_pkthdr.tags);
465099a0e58SBosko Milekic #ifdef MAC
466099a0e58SBosko Milekic 		/* If the label init fails, fail the alloc */
46730d239bcSRobert Watson 		error = mac_mbuf_init(m, how);
468b23f72e9SBrian Feldman 		if (error)
469b23f72e9SBrian Feldman 			return (error);
470099a0e58SBosko Milekic #endif
4716bc72ab9SBosko Milekic 	} else
472099a0e58SBosko Milekic 		m->m_data = m->m_dat;
473b23f72e9SBrian Feldman 	return (0);
474099a0e58SBosko Milekic }
475099a0e58SBosko Milekic 
476099a0e58SBosko Milekic /*
47756a4e45aSAndre Oppermann  * The Mbuf master zone destructor.
478099a0e58SBosko Milekic  */
479099a0e58SBosko Milekic static void
480099a0e58SBosko Milekic mb_dtor_mbuf(void *mem, int size, void *arg)
481099a0e58SBosko Milekic {
482099a0e58SBosko Milekic 	struct mbuf *m;
483629b9e08SKip Macy 	unsigned long flags;
484099a0e58SBosko Milekic 
485099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
486629b9e08SKip Macy 	flags = (unsigned long)arg;
487629b9e08SKip Macy 
488629b9e08SKip Macy 	if ((flags & MB_NOTAGS) == 0 && (m->m_flags & M_PKTHDR) != 0)
489099a0e58SBosko Milekic 		m_tag_delete_chain(m, NULL);
49056a4e45aSAndre Oppermann 	KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
491457869b9SKip Macy 	KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__));
492121f0509SMike Silbersack #ifdef INVARIANTS
493121f0509SMike Silbersack 	trash_dtor(mem, size, arg);
494121f0509SMike Silbersack #endif
495099a0e58SBosko Milekic }
496099a0e58SBosko Milekic 
49756a4e45aSAndre Oppermann /*
49856a4e45aSAndre Oppermann  * The Mbuf Packet zone destructor.
49956a4e45aSAndre Oppermann  */
500099a0e58SBosko Milekic static void
501099a0e58SBosko Milekic mb_dtor_pack(void *mem, int size, void *arg)
502099a0e58SBosko Milekic {
503099a0e58SBosko Milekic 	struct mbuf *m;
504099a0e58SBosko Milekic 
505099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
506099a0e58SBosko Milekic 	if ((m->m_flags & M_PKTHDR) != 0)
507099a0e58SBosko Milekic 		m_tag_delete_chain(m, NULL);
50856a4e45aSAndre Oppermann 
50956a4e45aSAndre Oppermann 	/* Make sure we've got a clean cluster back. */
51056a4e45aSAndre Oppermann 	KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
51156a4e45aSAndre Oppermann 	KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
51256a4e45aSAndre Oppermann 	KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
513cf827063SPoul-Henning Kamp 	KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__));
514cf827063SPoul-Henning Kamp 	KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__));
51556a4e45aSAndre Oppermann 	KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
51649d46b61SGleb Smirnoff 	KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
51756a4e45aSAndre Oppermann 	KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__));
518121f0509SMike Silbersack #ifdef INVARIANTS
519121f0509SMike Silbersack 	trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
520121f0509SMike Silbersack #endif
5216c125b8dSMohan Srinivasan 	/*
522ef44c8d2SDavid E. O'Brien 	 * If there are processes blocked on zone_clust, waiting for pages
523ef44c8d2SDavid E. O'Brien 	 * to be freed up, * cause them to be woken up by draining the
524ef44c8d2SDavid E. O'Brien 	 * packet zone.  We are exposed to a race here * (in the check for
525ef44c8d2SDavid E. O'Brien 	 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
526ef44c8d2SDavid E. O'Brien 	 * is deliberate. We don't want to acquire the zone lock for every
527ef44c8d2SDavid E. O'Brien 	 * mbuf free.
5286c125b8dSMohan Srinivasan 	 */
5296c125b8dSMohan Srinivasan 	if (uma_zone_exhausted_nolock(zone_clust))
5306c125b8dSMohan Srinivasan 		zone_drain(zone_pack);
531099a0e58SBosko Milekic }
532099a0e58SBosko Milekic 
533099a0e58SBosko Milekic /*
534ec63cb90SAndre Oppermann  * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
535099a0e58SBosko Milekic  *
536099a0e58SBosko Milekic  * Here the 'arg' pointer points to the Mbuf which we
53756a4e45aSAndre Oppermann  * are configuring cluster storage for.  If 'arg' is
53856a4e45aSAndre Oppermann  * empty we allocate just the cluster without setting
53956a4e45aSAndre Oppermann  * the mbuf to it.  See mbuf.h.
540099a0e58SBosko Milekic  */
541b23f72e9SBrian Feldman static int
542b23f72e9SBrian Feldman mb_ctor_clust(void *mem, int size, void *arg, int how)
543099a0e58SBosko Milekic {
544099a0e58SBosko Milekic 	struct mbuf *m;
54556a4e45aSAndre Oppermann 	u_int *refcnt;
5460f4d9d04SKip Macy 	int type;
5470f4d9d04SKip Macy 	uma_zone_t zone;
548099a0e58SBosko Milekic 
549121f0509SMike Silbersack #ifdef INVARIANTS
550121f0509SMike Silbersack 	trash_ctor(mem, size, arg, how);
551121f0509SMike Silbersack #endif
55256a4e45aSAndre Oppermann 	switch (size) {
55356a4e45aSAndre Oppermann 	case MCLBYTES:
55456a4e45aSAndre Oppermann 		type = EXT_CLUSTER;
5550f4d9d04SKip Macy 		zone = zone_clust;
55656a4e45aSAndre Oppermann 		break;
557ec63cb90SAndre Oppermann #if MJUMPAGESIZE != MCLBYTES
558ec63cb90SAndre Oppermann 	case MJUMPAGESIZE:
559ec63cb90SAndre Oppermann 		type = EXT_JUMBOP;
5600f4d9d04SKip Macy 		zone = zone_jumbop;
561d5269a63SAndre Oppermann 		break;
56236ae3fd3SAndre Oppermann #endif
56356a4e45aSAndre Oppermann 	case MJUM9BYTES:
56456a4e45aSAndre Oppermann 		type = EXT_JUMBO9;
5650f4d9d04SKip Macy 		zone = zone_jumbo9;
56656a4e45aSAndre Oppermann 		break;
56756a4e45aSAndre Oppermann 	case MJUM16BYTES:
56856a4e45aSAndre Oppermann 		type = EXT_JUMBO16;
5690f4d9d04SKip Macy 		zone = zone_jumbo16;
57056a4e45aSAndre Oppermann 		break;
57156a4e45aSAndre Oppermann 	default:
57256a4e45aSAndre Oppermann 		panic("unknown cluster size");
57356a4e45aSAndre Oppermann 		break;
57456a4e45aSAndre Oppermann 	}
5750f4d9d04SKip Macy 
5760f4d9d04SKip Macy 	m = (struct mbuf *)arg;
5770f4d9d04SKip Macy 	refcnt = uma_find_refcnt(zone, mem);
5780f4d9d04SKip Macy 	*refcnt = 1;
5790f4d9d04SKip Macy 	if (m != NULL) {
580099a0e58SBosko Milekic 		m->m_ext.ext_buf = (caddr_t)mem;
581099a0e58SBosko Milekic 		m->m_data = m->m_ext.ext_buf;
582099a0e58SBosko Milekic 		m->m_flags |= M_EXT;
583099a0e58SBosko Milekic 		m->m_ext.ext_free = NULL;
584cf827063SPoul-Henning Kamp 		m->m_ext.ext_arg1 = NULL;
585cf827063SPoul-Henning Kamp 		m->m_ext.ext_arg2 = NULL;
58656a4e45aSAndre Oppermann 		m->m_ext.ext_size = size;
58756a4e45aSAndre Oppermann 		m->m_ext.ext_type = type;
5880f4d9d04SKip Macy 		m->m_ext.ref_cnt = refcnt;
58956a4e45aSAndre Oppermann 	}
5900f4d9d04SKip Macy 
591b23f72e9SBrian Feldman 	return (0);
592099a0e58SBosko Milekic }
593099a0e58SBosko Milekic 
59456a4e45aSAndre Oppermann /*
59556a4e45aSAndre Oppermann  * The Mbuf Cluster zone destructor.
59656a4e45aSAndre Oppermann  */
597099a0e58SBosko Milekic static void
598099a0e58SBosko Milekic mb_dtor_clust(void *mem, int size, void *arg)
599099a0e58SBosko Milekic {
600121f0509SMike Silbersack #ifdef INVARIANTS
6010f4d9d04SKip Macy 	uma_zone_t zone;
6020f4d9d04SKip Macy 
6030f4d9d04SKip Macy 	zone = m_getzone(size);
6040f4d9d04SKip Macy 	KASSERT(*(uma_find_refcnt(zone, mem)) <= 1,
6050f4d9d04SKip Macy 		("%s: refcnt incorrect %u", __func__,
6060f4d9d04SKip Macy 		 *(uma_find_refcnt(zone, mem))) );
6070f4d9d04SKip Macy 
608121f0509SMike Silbersack 	trash_dtor(mem, size, arg);
609121f0509SMike Silbersack #endif
610099a0e58SBosko Milekic }
611099a0e58SBosko Milekic 
612099a0e58SBosko Milekic /*
613099a0e58SBosko Milekic  * The Packet secondary zone's init routine, executed on the
61456a4e45aSAndre Oppermann  * object's transition from mbuf keg slab to zone cache.
615099a0e58SBosko Milekic  */
616b23f72e9SBrian Feldman static int
61756a4e45aSAndre Oppermann mb_zinit_pack(void *mem, int size, int how)
618099a0e58SBosko Milekic {
619099a0e58SBosko Milekic 	struct mbuf *m;
620099a0e58SBosko Milekic 
62156a4e45aSAndre Oppermann 	m = (struct mbuf *)mem;		/* m is virgin. */
622a7bd90efSAndre Oppermann 	if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
623a7bd90efSAndre Oppermann 	    m->m_ext.ext_buf == NULL)
624b23f72e9SBrian Feldman 		return (ENOMEM);
625cd5bb63bSAndre Oppermann 	m->m_ext.ext_type = EXT_PACKET;	/* Override. */
626121f0509SMike Silbersack #ifdef INVARIANTS
627121f0509SMike Silbersack 	trash_init(m->m_ext.ext_buf, MCLBYTES, how);
628121f0509SMike Silbersack #endif
629b23f72e9SBrian Feldman 	return (0);
630099a0e58SBosko Milekic }
631099a0e58SBosko Milekic 
632099a0e58SBosko Milekic /*
633099a0e58SBosko Milekic  * The Packet secondary zone's fini routine, executed on the
634099a0e58SBosko Milekic  * object's transition from zone cache to keg slab.
635099a0e58SBosko Milekic  */
636099a0e58SBosko Milekic static void
63756a4e45aSAndre Oppermann mb_zfini_pack(void *mem, int size)
638099a0e58SBosko Milekic {
639099a0e58SBosko Milekic 	struct mbuf *m;
640099a0e58SBosko Milekic 
641099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
642121f0509SMike Silbersack #ifdef INVARIANTS
643121f0509SMike Silbersack 	trash_fini(m->m_ext.ext_buf, MCLBYTES);
644121f0509SMike Silbersack #endif
645099a0e58SBosko Milekic 	uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
646a7b844d2SMike Silbersack #ifdef INVARIANTS
647a7b844d2SMike Silbersack 	trash_dtor(mem, size, NULL);
648a7b844d2SMike Silbersack #endif
649099a0e58SBosko Milekic }
650099a0e58SBosko Milekic 
651099a0e58SBosko Milekic /*
652099a0e58SBosko Milekic  * The "packet" keg constructor.
653099a0e58SBosko Milekic  */
654b23f72e9SBrian Feldman static int
655b23f72e9SBrian Feldman mb_ctor_pack(void *mem, int size, void *arg, int how)
656099a0e58SBosko Milekic {
657099a0e58SBosko Milekic 	struct mbuf *m;
658099a0e58SBosko Milekic 	struct mb_args *args;
659b23f72e9SBrian Feldman #ifdef MAC
660b23f72e9SBrian Feldman 	int error;
661b23f72e9SBrian Feldman #endif
662b23f72e9SBrian Feldman 	int flags;
663099a0e58SBosko Milekic 	short type;
664099a0e58SBosko Milekic 
665099a0e58SBosko Milekic 	m = (struct mbuf *)mem;
666099a0e58SBosko Milekic 	args = (struct mb_args *)arg;
667099a0e58SBosko Milekic 	flags = args->flags;
668099a0e58SBosko Milekic 	type = args->type;
669099a0e58SBosko Milekic 
670121f0509SMike Silbersack #ifdef INVARIANTS
671121f0509SMike Silbersack 	trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
672121f0509SMike Silbersack #endif
673099a0e58SBosko Milekic 	m->m_next = NULL;
6746bc72ab9SBosko Milekic 	m->m_nextpkt = NULL;
675099a0e58SBosko Milekic 	m->m_data = m->m_ext.ext_buf;
67656a4e45aSAndre Oppermann 	m->m_len = 0;
67756a4e45aSAndre Oppermann 	m->m_flags = (flags | M_EXT);
67856a4e45aSAndre Oppermann 	m->m_type = type;
679099a0e58SBosko Milekic 
680099a0e58SBosko Milekic 	if (flags & M_PKTHDR) {
681099a0e58SBosko Milekic 		m->m_pkthdr.rcvif = NULL;
68256a4e45aSAndre Oppermann 		m->m_pkthdr.len = 0;
68356a4e45aSAndre Oppermann 		m->m_pkthdr.header = NULL;
684099a0e58SBosko Milekic 		m->m_pkthdr.csum_flags = 0;
68556a4e45aSAndre Oppermann 		m->m_pkthdr.csum_data = 0;
686a855e2b4SAndre Oppermann 		m->m_pkthdr.tso_segsz = 0;
687a855e2b4SAndre Oppermann 		m->m_pkthdr.ether_vtag = 0;
688877e8812SRobert Watson 		m->m_pkthdr.flowid = 0;
689099a0e58SBosko Milekic 		SLIST_INIT(&m->m_pkthdr.tags);
690099a0e58SBosko Milekic #ifdef MAC
691099a0e58SBosko Milekic 		/* If the label init fails, fail the alloc */
69230d239bcSRobert Watson 		error = mac_mbuf_init(m, how);
693b23f72e9SBrian Feldman 		if (error)
694b23f72e9SBrian Feldman 			return (error);
695099a0e58SBosko Milekic #endif
696099a0e58SBosko Milekic 	}
69756a4e45aSAndre Oppermann 	/* m_ext is already initialized. */
69856a4e45aSAndre Oppermann 
699b23f72e9SBrian Feldman 	return (0);
700099a0e58SBosko Milekic }
701099a0e58SBosko Milekic 
7025b204a11SKip Macy int
7035b204a11SKip Macy m_pkthdr_init(struct mbuf *m, int how)
7045b204a11SKip Macy {
7055b204a11SKip Macy #ifdef MAC
7065b204a11SKip Macy 	int error;
7075b204a11SKip Macy #endif
7085b204a11SKip Macy 	m->m_data = m->m_pktdat;
7095b204a11SKip Macy 	SLIST_INIT(&m->m_pkthdr.tags);
7105b204a11SKip Macy 	m->m_pkthdr.rcvif = NULL;
7115b204a11SKip Macy 	m->m_pkthdr.header = NULL;
7125b204a11SKip Macy 	m->m_pkthdr.len = 0;
7135b204a11SKip Macy 	m->m_pkthdr.flowid = 0;
7145b204a11SKip Macy 	m->m_pkthdr.csum_flags = 0;
7155b204a11SKip Macy 	m->m_pkthdr.csum_data = 0;
7165b204a11SKip Macy 	m->m_pkthdr.tso_segsz = 0;
7175b204a11SKip Macy 	m->m_pkthdr.ether_vtag = 0;
7185b204a11SKip Macy #ifdef MAC
7195b204a11SKip Macy 	/* If the label init fails, fail the alloc */
7205b204a11SKip Macy 	error = mac_mbuf_init(m, how);
7215b204a11SKip Macy 	if (error)
7225b204a11SKip Macy 		return (error);
7235b204a11SKip Macy #endif
7245b204a11SKip Macy 
7255b204a11SKip Macy 	return (0);
7265b204a11SKip Macy }
7275b204a11SKip Macy 
728099a0e58SBosko Milekic /*
729099a0e58SBosko Milekic  * This is the protocol drain routine.
730099a0e58SBosko Milekic  *
731099a0e58SBosko Milekic  * No locks should be held when this is called.  The drain routines have to
732099a0e58SBosko Milekic  * presently acquire some locks which raises the possibility of lock order
733099a0e58SBosko Milekic  * reversal.
734099a0e58SBosko Milekic  */
735099a0e58SBosko Milekic static void
736099a0e58SBosko Milekic mb_reclaim(void *junk)
737099a0e58SBosko Milekic {
738099a0e58SBosko Milekic 	struct domain *dp;
739099a0e58SBosko Milekic 	struct protosw *pr;
740099a0e58SBosko Milekic 
741099a0e58SBosko Milekic 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
742099a0e58SBosko Milekic 	    "mb_reclaim()");
743099a0e58SBosko Milekic 
744099a0e58SBosko Milekic 	for (dp = domains; dp != NULL; dp = dp->dom_next)
745099a0e58SBosko Milekic 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
746099a0e58SBosko Milekic 			if (pr->pr_drain != NULL)
747099a0e58SBosko Milekic 				(*pr->pr_drain)();
748099a0e58SBosko Milekic }
749