1/*-
2 * Copyright (c) 2004
3 * Bosko Milekic <bmilekic@FreeBSD.org>.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions and the following
11 * disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of contributors may be
16 * used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/kern_mbuf.c 132987 2004-08-02 00:18:36Z green $");
34
35#include "opt_mac.h"
36#include "opt_param.h"
37
38#include <sys/param.h>
39#include <sys/mac.h>
40#include <sys/malloc.h>
41#include <sys/systm.h>
42#include <sys/mbuf.h>
43#include <sys/domain.h>
44#include <sys/eventhandler.h>
45#include <sys/kernel.h>
46#include <sys/protosw.h>
47#include <sys/smp.h>
48#include <sys/sysctl.h>
49
50#include <vm/vm.h>
51#include <vm/vm_page.h>
52#include <vm/uma.h>
53
54/*
55 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
56 * Zones.
57 *
58 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
59 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
60 * administrator so desires.
61 *
62 * Mbufs are allocated from a UMA Master Zone called the Mbuf
63 * Zone.
64 *
65 * Additionally, FreeBSD provides a Packet Zone, which it
66 * configures as a Secondary Zone to the Mbuf Master Zone,
67 * thus sharing backend Slab kegs with the Mbuf Master Zone.
68 *
69 * Thus common-case allocations and locking are simplified:
70 *
71 * m_clget() m_getcl()
72 * | |
73 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
74 * | | [ Packet ] |
75 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
76 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
77 * | \________ |
78 * [ Cluster Keg ] \ /
79 * | [ Mbuf Keg ]
80 * [ Cluster Slabs ] |
81 * | [ Mbuf Slabs ]
82 * \____________(VM)_________________/
83 */
84
85int nmbclusters;
86struct mbstat mbstat;
87
88static void
89tunable_mbinit(void *dummy)
90{
91
92 /* This has to be done before VM init. */
93 nmbclusters = 1024 + maxusers * 64;
94 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
95}
96SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL);
97
98SYSCTL_DECL(_kern_ipc);
99SYSCTL_INT(_kern_ipc, OID_AUTO, nmbclusters, CTLFLAG_RW, &nmbclusters, 0,
100 "Maximum number of mbuf clusters allowed");
101SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
102 "Mbuf general information and statistics");
103
104/*
105 * Zones from which we allocate.
106 */
107uma_zone_t zone_mbuf;
108uma_zone_t zone_clust;
109uma_zone_t zone_pack;
110
111/*
112 * Local prototypes.
113 */
114static int mb_ctor_mbuf(void *, int, void *, int);
115static int mb_ctor_clust(void *, int, void *, int);
116static int mb_ctor_pack(void *, int, void *, int);
117static void mb_dtor_mbuf(void *, int, void *);
118static void mb_dtor_clust(void *, int, void *); /* XXX */
119static void mb_dtor_pack(void *, int, void *); /* XXX */
120static int mb_init_pack(void *, int, int);
121static void mb_fini_pack(void *, int);
122
123static void mb_reclaim(void *);
124static void mbuf_init(void *);
125
126/*
127 * Initialize FreeBSD Network buffer allocation.
128 */
129SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL)
130static void
131mbuf_init(void *dummy)
132{
133
134 /*
135 * Configure UMA zones for Mbufs, Clusters, and Packets.
136 */
137 zone_mbuf = uma_zcreate("Mbuf", MSIZE, mb_ctor_mbuf, mb_dtor_mbuf,
138 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_MAXBUCKET);
139 zone_clust = uma_zcreate("MbufClust", MCLBYTES, mb_ctor_clust,
140 mb_dtor_clust, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
141 if (nmbclusters > 0)
142 uma_zone_set_max(zone_clust, nmbclusters);
143 zone_pack = uma_zsecond_create("Packet", mb_ctor_pack, mb_dtor_pack,
144 mb_init_pack, mb_fini_pack, zone_mbuf);
145
146 /* uma_prealloc() goes here */
147
148 /*
149 * Hook event handler for low-memory situation, used to
150 * drain protocols and push data back to the caches (UMA
151 * later pushes it back to VM).
152 */
153 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
154 EVENTHANDLER_PRI_FIRST);
155
156 /*
157 * [Re]set counters and local statistics knobs.
158 * XXX Some of these should go and be replaced, but UMA stat
159 * gathering needs to be revised.
160 */
161 mbstat.m_mbufs = 0;
162 mbstat.m_mclusts = 0;
163 mbstat.m_drain = 0;
164 mbstat.m_msize = MSIZE;
165 mbstat.m_mclbytes = MCLBYTES;
166 mbstat.m_minclsize = MINCLSIZE;
167 mbstat.m_mlen = MLEN;
168 mbstat.m_mhlen = MHLEN;
169 mbstat.m_numtypes = MT_NTYPES;
170
171 mbstat.m_mcfail = mbstat.m_mpfail = 0;
172 mbstat.sf_iocnt = 0;
173 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
174}
175
176/*
177 * Constructor for Mbuf master zone.
178 *
179 * The 'arg' pointer points to a mb_args structure which
180 * contains call-specific information required to support the
181 * mbuf allocation API.
182 */
183static int
184mb_ctor_mbuf(void *mem, int size, void *arg, int how)
185{
186 struct mbuf *m;
187 struct mb_args *args;
188#ifdef MAC
189 int error;
190#endif
191 int flags;
192 short type;
193
194 m = (struct mbuf *)mem;
195 args = (struct mb_args *)arg;
196 flags = args->flags;
197 type = args->type;
198
199 m->m_type = type;
200 m->m_next = NULL;
201 m->m_nextpkt = NULL;
202 m->m_flags = flags;
203 if (flags & M_PKTHDR) {
204 m->m_data = m->m_pktdat;
205 m->m_pkthdr.rcvif = NULL;
206 m->m_pkthdr.csum_flags = 0;
207 SLIST_INIT(&m->m_pkthdr.tags);
208#ifdef MAC
209 /* If the label init fails, fail the alloc */
210 error = mac_init_mbuf(m, how);
211 if (error)
212 return (error);
213#endif
214 } else
215 m->m_data = m->m_dat;
216 mbstat.m_mbufs += 1; /* XXX */
217 return (0);
218}
219
220/*
221 * The Mbuf master zone and Packet secondary zone destructor.
222 */
223static void
224mb_dtor_mbuf(void *mem, int size, void *arg)
225{
226 struct mbuf *m;
227
228 m = (struct mbuf *)mem;
229 if ((m->m_flags & M_PKTHDR) != 0)
230 m_tag_delete_chain(m, NULL);
231 mbstat.m_mbufs -= 1; /* XXX */
232}
233
234/* XXX Only because of stats */
235static void
236mb_dtor_pack(void *mem, int size, void *arg)
237{
238 struct mbuf *m;
239
240 m = (struct mbuf *)mem;
241 if ((m->m_flags & M_PKTHDR) != 0)
242 m_tag_delete_chain(m, NULL);
243 mbstat.m_mbufs -= 1; /* XXX */
244 mbstat.m_mclusts -= 1; /* XXX */
245}
246
247/*
248 * The Cluster zone constructor.
249 *
250 * Here the 'arg' pointer points to the Mbuf which we
251 * are configuring cluster storage for.
252 */
253static int
254mb_ctor_clust(void *mem, int size, void *arg, int how)
255{
256 struct mbuf *m;
257
258 m = (struct mbuf *)arg;
259 m->m_ext.ext_buf = (caddr_t)mem;
260 m->m_data = m->m_ext.ext_buf;
261 m->m_flags |= M_EXT;
262 m->m_ext.ext_free = NULL;
263 m->m_ext.ext_args = NULL;
264 m->m_ext.ext_size = MCLBYTES;
265 m->m_ext.ext_type = EXT_CLUSTER;
266 m->m_ext.ref_cnt = (u_int *)uma_find_refcnt(zone_clust,
267 m->m_ext.ext_buf);
268 *(m->m_ext.ref_cnt) = 1;
269 mbstat.m_mclusts += 1; /* XXX */
270 return (0);
271}
272
273/* XXX */
274static void
275mb_dtor_clust(void *mem, int size, void *arg)
276{
277 mbstat.m_mclusts -= 1; /* XXX */
278}
279
280/*
281 * The Packet secondary zone's init routine, executed on the
282 * object's transition from keg slab to zone cache.
283 */
284static int
285mb_init_pack(void *mem, int size, int how)
286{
287 struct mbuf *m;
288
289 m = (struct mbuf *)mem;
290 m->m_ext.ext_buf = NULL;
291 uma_zalloc_arg(zone_clust, m, how);
292 if (m->m_ext.ext_buf == NULL)
293 return (ENOMEM);
294 mbstat.m_mclusts -= 1; /* XXX */
295 return (0);
296}
297
298/*
299 * The Packet secondary zone's fini routine, executed on the
300 * object's transition from zone cache to keg slab.
301 */
302static void
303mb_fini_pack(void *mem, int size)
304{
305 struct mbuf *m;
306
307 m = (struct mbuf *)mem;
308 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
309 m->m_ext.ext_buf = NULL;
310 mbstat.m_mclusts += 1; /* XXX */
311}
312
313/*
314 * The "packet" keg constructor.
315 */
316static int
317mb_ctor_pack(void *mem, int size, void *arg, int how)
318{
319 struct mbuf *m;
320 struct mb_args *args;
321#ifdef MAC
322 int error;
323#endif
324 int flags;
325 short type;
326
327 m = (struct mbuf *)mem;
328 args = (struct mb_args *)arg;
329 flags = args->flags;
330 type = args->type;
331
332 m->m_type = type;
333 m->m_next = NULL;
334 m->m_nextpkt = NULL;
335 m->m_data = m->m_ext.ext_buf;
336 m->m_flags = flags|M_EXT;
337 m->m_ext.ext_free = NULL;
338 m->m_ext.ext_args = NULL;
339 m->m_ext.ext_size = MCLBYTES;
340 m->m_ext.ext_type = EXT_PACKET;
341 *(m->m_ext.ref_cnt) = 1;
342
343 if (flags & M_PKTHDR) {
344 m->m_pkthdr.rcvif = NULL;
345 m->m_pkthdr.csum_flags = 0;
346 SLIST_INIT(&m->m_pkthdr.tags);
347#ifdef MAC
348 /* If the label init fails, fail the alloc */
349 error = mac_init_mbuf(m, how);
350 if (error)
351 return (error);
352#endif
353 }
354 mbstat.m_mbufs += 1; /* XXX */
355 mbstat.m_mclusts += 1; /* XXX */
356 return (0);
357}
358
359/*
360 * This is the protocol drain routine.
361 *
362 * No locks should be held when this is called. The drain routines have to
363 * presently acquire some locks which raises the possibility of lock order
364 * reversal.
365 */
366static void
367mb_reclaim(void *junk)
368{
369 struct domain *dp;
370 struct protosw *pr;
371
372 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
373 "mb_reclaim()");
374
375 mbstat.m_drain++;
376 for (dp = domains; dp != NULL; dp = dp->dom_next)
377 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
378 if (pr->pr_drain != NULL)
379 (*pr->pr_drain)();
380}
2 * Copyright (c) 2004
3 * Bosko Milekic <bmilekic@FreeBSD.org>.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions and the following
11 * disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of contributors may be
16 * used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/kern_mbuf.c 132987 2004-08-02 00:18:36Z green $");
34
35#include "opt_mac.h"
36#include "opt_param.h"
37
38#include <sys/param.h>
39#include <sys/mac.h>
40#include <sys/malloc.h>
41#include <sys/systm.h>
42#include <sys/mbuf.h>
43#include <sys/domain.h>
44#include <sys/eventhandler.h>
45#include <sys/kernel.h>
46#include <sys/protosw.h>
47#include <sys/smp.h>
48#include <sys/sysctl.h>
49
50#include <vm/vm.h>
51#include <vm/vm_page.h>
52#include <vm/uma.h>
53
54/*
55 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
56 * Zones.
57 *
58 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
59 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
60 * administrator so desires.
61 *
62 * Mbufs are allocated from a UMA Master Zone called the Mbuf
63 * Zone.
64 *
65 * Additionally, FreeBSD provides a Packet Zone, which it
66 * configures as a Secondary Zone to the Mbuf Master Zone,
67 * thus sharing backend Slab kegs with the Mbuf Master Zone.
68 *
69 * Thus common-case allocations and locking are simplified:
70 *
71 * m_clget() m_getcl()
72 * | |
73 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
74 * | | [ Packet ] |
75 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
76 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
77 * | \________ |
78 * [ Cluster Keg ] \ /
79 * | [ Mbuf Keg ]
80 * [ Cluster Slabs ] |
81 * | [ Mbuf Slabs ]
82 * \____________(VM)_________________/
83 */
84
85int nmbclusters;
86struct mbstat mbstat;
87
88static void
89tunable_mbinit(void *dummy)
90{
91
92 /* This has to be done before VM init. */
93 nmbclusters = 1024 + maxusers * 64;
94 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
95}
96SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL);
97
98SYSCTL_DECL(_kern_ipc);
99SYSCTL_INT(_kern_ipc, OID_AUTO, nmbclusters, CTLFLAG_RW, &nmbclusters, 0,
100 "Maximum number of mbuf clusters allowed");
101SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
102 "Mbuf general information and statistics");
103
104/*
105 * Zones from which we allocate.
106 */
107uma_zone_t zone_mbuf;
108uma_zone_t zone_clust;
109uma_zone_t zone_pack;
110
111/*
112 * Local prototypes.
113 */
114static int mb_ctor_mbuf(void *, int, void *, int);
115static int mb_ctor_clust(void *, int, void *, int);
116static int mb_ctor_pack(void *, int, void *, int);
117static void mb_dtor_mbuf(void *, int, void *);
118static void mb_dtor_clust(void *, int, void *); /* XXX */
119static void mb_dtor_pack(void *, int, void *); /* XXX */
120static int mb_init_pack(void *, int, int);
121static void mb_fini_pack(void *, int);
122
123static void mb_reclaim(void *);
124static void mbuf_init(void *);
125
126/*
127 * Initialize FreeBSD Network buffer allocation.
128 */
129SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL)
130static void
131mbuf_init(void *dummy)
132{
133
134 /*
135 * Configure UMA zones for Mbufs, Clusters, and Packets.
136 */
137 zone_mbuf = uma_zcreate("Mbuf", MSIZE, mb_ctor_mbuf, mb_dtor_mbuf,
138 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_MAXBUCKET);
139 zone_clust = uma_zcreate("MbufClust", MCLBYTES, mb_ctor_clust,
140 mb_dtor_clust, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
141 if (nmbclusters > 0)
142 uma_zone_set_max(zone_clust, nmbclusters);
143 zone_pack = uma_zsecond_create("Packet", mb_ctor_pack, mb_dtor_pack,
144 mb_init_pack, mb_fini_pack, zone_mbuf);
145
146 /* uma_prealloc() goes here */
147
148 /*
149 * Hook event handler for low-memory situation, used to
150 * drain protocols and push data back to the caches (UMA
151 * later pushes it back to VM).
152 */
153 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
154 EVENTHANDLER_PRI_FIRST);
155
156 /*
157 * [Re]set counters and local statistics knobs.
158 * XXX Some of these should go and be replaced, but UMA stat
159 * gathering needs to be revised.
160 */
161 mbstat.m_mbufs = 0;
162 mbstat.m_mclusts = 0;
163 mbstat.m_drain = 0;
164 mbstat.m_msize = MSIZE;
165 mbstat.m_mclbytes = MCLBYTES;
166 mbstat.m_minclsize = MINCLSIZE;
167 mbstat.m_mlen = MLEN;
168 mbstat.m_mhlen = MHLEN;
169 mbstat.m_numtypes = MT_NTYPES;
170
171 mbstat.m_mcfail = mbstat.m_mpfail = 0;
172 mbstat.sf_iocnt = 0;
173 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
174}
175
176/*
177 * Constructor for Mbuf master zone.
178 *
179 * The 'arg' pointer points to a mb_args structure which
180 * contains call-specific information required to support the
181 * mbuf allocation API.
182 */
183static int
184mb_ctor_mbuf(void *mem, int size, void *arg, int how)
185{
186 struct mbuf *m;
187 struct mb_args *args;
188#ifdef MAC
189 int error;
190#endif
191 int flags;
192 short type;
193
194 m = (struct mbuf *)mem;
195 args = (struct mb_args *)arg;
196 flags = args->flags;
197 type = args->type;
198
199 m->m_type = type;
200 m->m_next = NULL;
201 m->m_nextpkt = NULL;
202 m->m_flags = flags;
203 if (flags & M_PKTHDR) {
204 m->m_data = m->m_pktdat;
205 m->m_pkthdr.rcvif = NULL;
206 m->m_pkthdr.csum_flags = 0;
207 SLIST_INIT(&m->m_pkthdr.tags);
208#ifdef MAC
209 /* If the label init fails, fail the alloc */
210 error = mac_init_mbuf(m, how);
211 if (error)
212 return (error);
213#endif
214 } else
215 m->m_data = m->m_dat;
216 mbstat.m_mbufs += 1; /* XXX */
217 return (0);
218}
219
220/*
221 * The Mbuf master zone and Packet secondary zone destructor.
222 */
223static void
224mb_dtor_mbuf(void *mem, int size, void *arg)
225{
226 struct mbuf *m;
227
228 m = (struct mbuf *)mem;
229 if ((m->m_flags & M_PKTHDR) != 0)
230 m_tag_delete_chain(m, NULL);
231 mbstat.m_mbufs -= 1; /* XXX */
232}
233
234/* XXX Only because of stats */
235static void
236mb_dtor_pack(void *mem, int size, void *arg)
237{
238 struct mbuf *m;
239
240 m = (struct mbuf *)mem;
241 if ((m->m_flags & M_PKTHDR) != 0)
242 m_tag_delete_chain(m, NULL);
243 mbstat.m_mbufs -= 1; /* XXX */
244 mbstat.m_mclusts -= 1; /* XXX */
245}
246
247/*
248 * The Cluster zone constructor.
249 *
250 * Here the 'arg' pointer points to the Mbuf which we
251 * are configuring cluster storage for.
252 */
253static int
254mb_ctor_clust(void *mem, int size, void *arg, int how)
255{
256 struct mbuf *m;
257
258 m = (struct mbuf *)arg;
259 m->m_ext.ext_buf = (caddr_t)mem;
260 m->m_data = m->m_ext.ext_buf;
261 m->m_flags |= M_EXT;
262 m->m_ext.ext_free = NULL;
263 m->m_ext.ext_args = NULL;
264 m->m_ext.ext_size = MCLBYTES;
265 m->m_ext.ext_type = EXT_CLUSTER;
266 m->m_ext.ref_cnt = (u_int *)uma_find_refcnt(zone_clust,
267 m->m_ext.ext_buf);
268 *(m->m_ext.ref_cnt) = 1;
269 mbstat.m_mclusts += 1; /* XXX */
270 return (0);
271}
272
273/* XXX */
274static void
275mb_dtor_clust(void *mem, int size, void *arg)
276{
277 mbstat.m_mclusts -= 1; /* XXX */
278}
279
280/*
281 * The Packet secondary zone's init routine, executed on the
282 * object's transition from keg slab to zone cache.
283 */
284static int
285mb_init_pack(void *mem, int size, int how)
286{
287 struct mbuf *m;
288
289 m = (struct mbuf *)mem;
290 m->m_ext.ext_buf = NULL;
291 uma_zalloc_arg(zone_clust, m, how);
292 if (m->m_ext.ext_buf == NULL)
293 return (ENOMEM);
294 mbstat.m_mclusts -= 1; /* XXX */
295 return (0);
296}
297
298/*
299 * The Packet secondary zone's fini routine, executed on the
300 * object's transition from zone cache to keg slab.
301 */
302static void
303mb_fini_pack(void *mem, int size)
304{
305 struct mbuf *m;
306
307 m = (struct mbuf *)mem;
308 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
309 m->m_ext.ext_buf = NULL;
310 mbstat.m_mclusts += 1; /* XXX */
311}
312
313/*
314 * The "packet" keg constructor.
315 */
316static int
317mb_ctor_pack(void *mem, int size, void *arg, int how)
318{
319 struct mbuf *m;
320 struct mb_args *args;
321#ifdef MAC
322 int error;
323#endif
324 int flags;
325 short type;
326
327 m = (struct mbuf *)mem;
328 args = (struct mb_args *)arg;
329 flags = args->flags;
330 type = args->type;
331
332 m->m_type = type;
333 m->m_next = NULL;
334 m->m_nextpkt = NULL;
335 m->m_data = m->m_ext.ext_buf;
336 m->m_flags = flags|M_EXT;
337 m->m_ext.ext_free = NULL;
338 m->m_ext.ext_args = NULL;
339 m->m_ext.ext_size = MCLBYTES;
340 m->m_ext.ext_type = EXT_PACKET;
341 *(m->m_ext.ref_cnt) = 1;
342
343 if (flags & M_PKTHDR) {
344 m->m_pkthdr.rcvif = NULL;
345 m->m_pkthdr.csum_flags = 0;
346 SLIST_INIT(&m->m_pkthdr.tags);
347#ifdef MAC
348 /* If the label init fails, fail the alloc */
349 error = mac_init_mbuf(m, how);
350 if (error)
351 return (error);
352#endif
353 }
354 mbstat.m_mbufs += 1; /* XXX */
355 mbstat.m_mclusts += 1; /* XXX */
356 return (0);
357}
358
359/*
360 * This is the protocol drain routine.
361 *
362 * No locks should be held when this is called. The drain routines have to
363 * presently acquire some locks which raises the possibility of lock order
364 * reversal.
365 */
366static void
367mb_reclaim(void *junk)
368{
369 struct domain *dp;
370 struct protosw *pr;
371
372 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
373 "mb_reclaim()");
374
375 mbstat.m_drain++;
376 for (dp = domains; dp != NULL; dp = dp->dom_next)
377 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
378 if (pr->pr_drain != NULL)
379 (*pr->pr_drain)();
380}