uipc_mbuf.c revision 1.203 1 1.203 maxv /* $NetBSD: uipc_mbuf.c,v 1.203 2018/04/27 07:41:58 maxv Exp $ */
2 1.42 thorpej
3 1.177 maxv /*
4 1.53 thorpej * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
5 1.42 thorpej * All rights reserved.
6 1.42 thorpej *
7 1.42 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.42 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.42 thorpej * NASA Ames Research Center.
10 1.42 thorpej *
11 1.42 thorpej * Redistribution and use in source and binary forms, with or without
12 1.42 thorpej * modification, are permitted provided that the following conditions
13 1.42 thorpej * are met:
14 1.42 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.42 thorpej * notice, this list of conditions and the following disclaimer.
16 1.42 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.42 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.42 thorpej * documentation and/or other materials provided with the distribution.
19 1.42 thorpej *
20 1.42 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.42 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.42 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.42 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.42 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.42 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.42 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.42 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.42 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.42 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.42 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.42 thorpej */
32 1.10 cgd
33 1.1 cgd /*
34 1.9 mycroft * Copyright (c) 1982, 1986, 1988, 1991, 1993
35 1.9 mycroft * The Regents of the University of California. All rights reserved.
36 1.1 cgd *
37 1.1 cgd * Redistribution and use in source and binary forms, with or without
38 1.1 cgd * modification, are permitted provided that the following conditions
39 1.1 cgd * are met:
40 1.1 cgd * 1. Redistributions of source code must retain the above copyright
41 1.1 cgd * notice, this list of conditions and the following disclaimer.
42 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 cgd * notice, this list of conditions and the following disclaimer in the
44 1.1 cgd * documentation and/or other materials provided with the distribution.
45 1.70 agc * 3. Neither the name of the University nor the names of its contributors
46 1.1 cgd * may be used to endorse or promote products derived from this software
47 1.1 cgd * without specific prior written permission.
48 1.1 cgd *
49 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.1 cgd * SUCH DAMAGE.
60 1.1 cgd *
61 1.26 fvdl * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
62 1.1 cgd */
63 1.56 lukem
64 1.56 lukem #include <sys/cdefs.h>
65 1.203 maxv __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.203 2018/04/27 07:41:58 maxv Exp $");
66 1.69 martin
67 1.163 pooka #ifdef _KERNEL_OPT
68 1.69 martin #include "opt_mbuftrace.h"
69 1.133 joerg #include "opt_nmbclusters.h"
70 1.105 yamt #include "opt_ddb.h"
71 1.163 pooka #endif
72 1.24 mrg
73 1.6 mycroft #include <sys/param.h>
74 1.6 mycroft #include <sys/systm.h>
75 1.125 yamt #include <sys/atomic.h>
76 1.124 yamt #include <sys/cpu.h>
77 1.6 mycroft #include <sys/proc.h>
78 1.6 mycroft #include <sys/mbuf.h>
79 1.6 mycroft #include <sys/kernel.h>
80 1.6 mycroft #include <sys/syslog.h>
81 1.6 mycroft #include <sys/domain.h>
82 1.6 mycroft #include <sys/protosw.h>
83 1.124 yamt #include <sys/percpu.h>
84 1.28 thorpej #include <sys/pool.h>
85 1.27 matt #include <sys/socket.h>
86 1.55 simonb #include <sys/sysctl.h>
87 1.55 simonb
88 1.27 matt #include <net/if.h>
89 1.14 christos
90 1.122 ad pool_cache_t mb_cache; /* mbuf cache */
91 1.122 ad pool_cache_t mcl_cache; /* mbuf cluster cache */
92 1.53 thorpej
93 1.18 thorpej struct mbstat mbstat;
94 1.199 maxv int max_linkhdr;
95 1.199 maxv int max_protohdr;
96 1.199 maxv int max_hdr;
97 1.199 maxv int max_datalen;
98 1.18 thorpej
99 1.203 maxv static void mb_drain(void *, int);
100 1.65 thorpej static int mb_ctor(void *, void *, int);
101 1.65 thorpej
102 1.199 maxv static void sysctl_kern_mbuf_setup(void);
103 1.129 pooka
104 1.129 pooka static struct sysctllog *mbuf_sysctllog;
105 1.129 pooka
106 1.195 maxv static struct mbuf *m_copy_internal(struct mbuf *, int, int, int, bool);
107 1.195 maxv static struct mbuf *m_split_internal(struct mbuf *, int, int, bool);
108 1.196 maxv static int m_copyback_internal(struct mbuf **, int, int, const void *,
109 1.196 maxv int, int);
110 1.85 yamt
111 1.196 maxv /* Flags for m_copyback_internal. */
112 1.196 maxv #define CB_COPYBACK 0x0001 /* copyback from cp */
113 1.196 maxv #define CB_PRESERVE 0x0002 /* preserve original data */
114 1.196 maxv #define CB_COW 0x0004 /* do copy-on-write */
115 1.196 maxv #define CB_EXTEND 0x0008 /* extend chain */
116 1.28 thorpej
117 1.103 thorpej static const char mclpool_warnmsg[] =
118 1.133 joerg "WARNING: mclpool limit reached; increase kern.mbuf.nmbclusters";
119 1.63 thorpej
120 1.63 thorpej MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
121 1.42 thorpej
122 1.124 yamt static percpu_t *mbstat_percpu;
123 1.124 yamt
124 1.64 matt #ifdef MBUFTRACE
125 1.64 matt struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
126 1.64 matt struct mowner unknown_mowners[] = {
127 1.114 dogcow MOWNER_INIT("unknown", "free"),
128 1.114 dogcow MOWNER_INIT("unknown", "data"),
129 1.114 dogcow MOWNER_INIT("unknown", "header"),
130 1.114 dogcow MOWNER_INIT("unknown", "soname"),
131 1.114 dogcow MOWNER_INIT("unknown", "soopts"),
132 1.114 dogcow MOWNER_INIT("unknown", "ftable"),
133 1.114 dogcow MOWNER_INIT("unknown", "control"),
134 1.114 dogcow MOWNER_INIT("unknown", "oobdata"),
135 1.64 matt };
136 1.114 dogcow struct mowner revoked_mowner = MOWNER_INIT("revoked", "");
137 1.64 matt #endif
138 1.64 matt
139 1.125 yamt #define MEXT_ISEMBEDDED(m) ((m)->m_ext_ref == (m))
140 1.125 yamt
141 1.125 yamt #define MCLADDREFERENCE(o, n) \
142 1.125 yamt do { \
143 1.125 yamt KASSERT(((o)->m_flags & M_EXT) != 0); \
144 1.125 yamt KASSERT(((n)->m_flags & M_EXT) == 0); \
145 1.125 yamt KASSERT((o)->m_ext.ext_refcnt >= 1); \
146 1.125 yamt (n)->m_flags |= ((o)->m_flags & M_EXTCOPYFLAGS); \
147 1.125 yamt atomic_inc_uint(&(o)->m_ext.ext_refcnt); \
148 1.125 yamt (n)->m_ext_ref = (o)->m_ext_ref; \
149 1.125 yamt mowner_ref((n), (n)->m_flags); \
150 1.125 yamt MCLREFDEBUGN((n), __FILE__, __LINE__); \
151 1.125 yamt } while (/* CONSTCOND */ 0)
152 1.125 yamt
153 1.133 joerg static int
154 1.133 joerg nmbclusters_limit(void)
155 1.133 joerg {
156 1.136 pooka #if defined(PMAP_MAP_POOLPAGE)
157 1.147 para /* direct mapping, doesn't use space in kmem_arena */
158 1.133 joerg vsize_t max_size = physmem / 4;
159 1.133 joerg #else
160 1.145 para vsize_t max_size = MIN(physmem / 4, nkmempages / 4);
161 1.133 joerg #endif
162 1.133 joerg
163 1.133 joerg max_size = max_size * PAGE_SIZE / MCLBYTES;
164 1.133 joerg #ifdef NMBCLUSTERS_MAX
165 1.133 joerg max_size = MIN(max_size, NMBCLUSTERS_MAX);
166 1.133 joerg #endif
167 1.133 joerg
168 1.133 joerg #ifdef NMBCLUSTERS
169 1.133 joerg return MIN(max_size, NMBCLUSTERS);
170 1.133 joerg #else
171 1.133 joerg return max_size;
172 1.133 joerg #endif
173 1.133 joerg }
174 1.133 joerg
175 1.28 thorpej /*
176 1.68 simonb * Initialize the mbuf allocator.
177 1.28 thorpej */
178 1.4 jtc void
179 1.62 thorpej mbinit(void)
180 1.1 cgd {
181 1.65 thorpej
182 1.128 matt CTASSERT(sizeof(struct _m_ext) <= MHLEN);
183 1.128 matt CTASSERT(sizeof(struct mbuf) == MSIZE);
184 1.65 thorpej
185 1.129 pooka sysctl_kern_mbuf_setup();
186 1.129 pooka
187 1.122 ad mb_cache = pool_cache_init(msize, 0, 0, 0, "mbpl",
188 1.122 ad NULL, IPL_VM, mb_ctor, NULL, NULL);
189 1.122 ad KASSERT(mb_cache != NULL);
190 1.122 ad
191 1.135 rmind mcl_cache = pool_cache_init(mclbytes, 0, 0, 0, "mclpl", NULL,
192 1.135 rmind IPL_VM, NULL, NULL, NULL);
193 1.122 ad KASSERT(mcl_cache != NULL);
194 1.59 thorpej
195 1.203 maxv pool_cache_set_drain_hook(mb_cache, mb_drain, NULL);
196 1.203 maxv pool_cache_set_drain_hook(mcl_cache, mb_drain, NULL);
197 1.37 thorpej
198 1.37 thorpej /*
199 1.133 joerg * Set an arbitrary default limit on the number of mbuf clusters.
200 1.133 joerg */
201 1.133 joerg #ifdef NMBCLUSTERS
202 1.133 joerg nmbclusters = nmbclusters_limit();
203 1.133 joerg #else
204 1.133 joerg nmbclusters = MAX(1024,
205 1.133 joerg (vsize_t)physmem * PAGE_SIZE / MCLBYTES / 16);
206 1.133 joerg nmbclusters = MIN(nmbclusters, nmbclusters_limit());
207 1.133 joerg #endif
208 1.133 joerg
209 1.133 joerg /*
210 1.39 thorpej * Set the hard limit on the mclpool to the number of
211 1.39 thorpej * mbuf clusters the kernel is to support. Log the limit
212 1.39 thorpej * reached message max once a minute.
213 1.39 thorpej */
214 1.122 ad pool_cache_sethardlimit(mcl_cache, nmbclusters, mclpool_warnmsg, 60);
215 1.42 thorpej
216 1.124 yamt mbstat_percpu = percpu_alloc(sizeof(struct mbstat_cpu));
217 1.124 yamt
218 1.39 thorpej /*
219 1.42 thorpej * Set a low water mark for both mbufs and clusters. This should
220 1.42 thorpej * help ensure that they can be allocated in a memory starvation
221 1.42 thorpej * situation. This is important for e.g. diskless systems which
222 1.42 thorpej * must allocate mbufs in order for the pagedaemon to clean pages.
223 1.37 thorpej */
224 1.122 ad pool_cache_setlowat(mb_cache, mblowat);
225 1.122 ad pool_cache_setlowat(mcl_cache, mcllowat);
226 1.64 matt
227 1.64 matt #ifdef MBUFTRACE
228 1.64 matt {
229 1.64 matt /*
230 1.64 matt * Attach the unknown mowners.
231 1.64 matt */
232 1.64 matt int i;
233 1.64 matt MOWNER_ATTACH(&revoked_mowner);
234 1.64 matt for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
235 1.64 matt i-- > 0; )
236 1.64 matt MOWNER_ATTACH(&unknown_mowners[i]);
237 1.64 matt }
238 1.64 matt #endif
239 1.42 thorpej }
240 1.42 thorpej
241 1.203 maxv static void
242 1.203 maxv mb_drain(void *arg, int flags)
243 1.203 maxv {
244 1.203 maxv struct domain *dp;
245 1.203 maxv const struct protosw *pr;
246 1.203 maxv struct ifnet *ifp;
247 1.203 maxv int s;
248 1.203 maxv
249 1.203 maxv KERNEL_LOCK(1, NULL);
250 1.203 maxv s = splvm();
251 1.203 maxv DOMAIN_FOREACH(dp) {
252 1.203 maxv for (pr = dp->dom_protosw;
253 1.203 maxv pr < dp->dom_protoswNPROTOSW; pr++)
254 1.203 maxv if (pr->pr_drain)
255 1.203 maxv (*pr->pr_drain)();
256 1.203 maxv }
257 1.203 maxv /* XXX we cannot use psref in H/W interrupt */
258 1.203 maxv if (!cpu_intr_p()) {
259 1.203 maxv int bound = curlwp_bind();
260 1.203 maxv IFNET_READER_FOREACH(ifp) {
261 1.203 maxv struct psref psref;
262 1.203 maxv
263 1.203 maxv if_acquire(ifp, &psref);
264 1.203 maxv
265 1.203 maxv if (ifp->if_drain)
266 1.203 maxv (*ifp->if_drain)(ifp);
267 1.203 maxv
268 1.203 maxv if_release(ifp, &psref);
269 1.203 maxv }
270 1.203 maxv curlwp_bindx(bound);
271 1.203 maxv }
272 1.203 maxv splx(s);
273 1.203 maxv mbstat.m_drain++;
274 1.203 maxv KERNEL_UNLOCK_ONE(NULL);
275 1.203 maxv }
276 1.203 maxv
277 1.75 atatat /*
278 1.133 joerg * sysctl helper routine for the kern.mbuf subtree.
279 1.133 joerg * nmbclusters, mblowat and mcllowat need range
280 1.75 atatat * checking and pool tweaking after being reset.
281 1.75 atatat */
282 1.75 atatat static int
283 1.75 atatat sysctl_kern_mbuf(SYSCTLFN_ARGS)
284 1.42 thorpej {
285 1.42 thorpej int error, newval;
286 1.75 atatat struct sysctlnode node;
287 1.42 thorpej
288 1.75 atatat node = *rnode;
289 1.75 atatat node.sysctl_data = &newval;
290 1.75 atatat switch (rnode->sysctl_num) {
291 1.42 thorpej case MBUF_NMBCLUSTERS:
292 1.42 thorpej case MBUF_MBLOWAT:
293 1.42 thorpej case MBUF_MCLLOWAT:
294 1.75 atatat newval = *(int*)rnode->sysctl_data;
295 1.75 atatat break;
296 1.75 atatat default:
297 1.199 maxv return EOPNOTSUPP;
298 1.75 atatat }
299 1.75 atatat
300 1.75 atatat error = sysctl_lookup(SYSCTLFN_CALL(&node));
301 1.75 atatat if (error || newp == NULL)
302 1.199 maxv return error;
303 1.75 atatat if (newval < 0)
304 1.199 maxv return EINVAL;
305 1.75 atatat
306 1.75 atatat switch (node.sysctl_num) {
307 1.75 atatat case MBUF_NMBCLUSTERS:
308 1.75 atatat if (newval < nmbclusters)
309 1.199 maxv return EINVAL;
310 1.133 joerg if (newval > nmbclusters_limit())
311 1.199 maxv return EINVAL;
312 1.75 atatat nmbclusters = newval;
313 1.122 ad pool_cache_sethardlimit(mcl_cache, nmbclusters,
314 1.122 ad mclpool_warnmsg, 60);
315 1.75 atatat break;
316 1.75 atatat case MBUF_MBLOWAT:
317 1.75 atatat mblowat = newval;
318 1.122 ad pool_cache_setlowat(mb_cache, mblowat);
319 1.75 atatat break;
320 1.75 atatat case MBUF_MCLLOWAT:
321 1.76 atatat mcllowat = newval;
322 1.122 ad pool_cache_setlowat(mcl_cache, mcllowat);
323 1.75 atatat break;
324 1.75 atatat }
325 1.75 atatat
326 1.199 maxv return 0;
327 1.75 atatat }
328 1.75 atatat
329 1.64 matt #ifdef MBUFTRACE
330 1.124 yamt static void
331 1.124 yamt mowner_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
332 1.124 yamt {
333 1.124 yamt struct mowner_counter *mc = v1;
334 1.124 yamt struct mowner_user *mo_user = v2;
335 1.124 yamt int i;
336 1.124 yamt
337 1.124 yamt for (i = 0; i < MOWNER_COUNTER_NCOUNTERS; i++) {
338 1.124 yamt mo_user->mo_counter[i] += mc->mc_counter[i];
339 1.124 yamt }
340 1.124 yamt }
341 1.124 yamt
342 1.124 yamt static void
343 1.124 yamt mowner_convert_to_user(struct mowner *mo, struct mowner_user *mo_user)
344 1.124 yamt {
345 1.124 yamt
346 1.124 yamt memset(mo_user, 0, sizeof(*mo_user));
347 1.128 matt CTASSERT(sizeof(mo_user->mo_name) == sizeof(mo->mo_name));
348 1.128 matt CTASSERT(sizeof(mo_user->mo_descr) == sizeof(mo->mo_descr));
349 1.124 yamt memcpy(mo_user->mo_name, mo->mo_name, sizeof(mo->mo_name));
350 1.124 yamt memcpy(mo_user->mo_descr, mo->mo_descr, sizeof(mo->mo_descr));
351 1.124 yamt percpu_foreach(mo->mo_counters, mowner_conver_to_user_cb, mo_user);
352 1.124 yamt }
353 1.124 yamt
354 1.75 atatat static int
355 1.75 atatat sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
356 1.75 atatat {
357 1.75 atatat struct mowner *mo;
358 1.75 atatat size_t len = 0;
359 1.75 atatat int error = 0;
360 1.75 atatat
361 1.75 atatat if (namelen != 0)
362 1.199 maxv return EINVAL;
363 1.75 atatat if (newp != NULL)
364 1.199 maxv return EPERM;
365 1.75 atatat
366 1.75 atatat LIST_FOREACH(mo, &mowners, mo_link) {
367 1.124 yamt struct mowner_user mo_user;
368 1.124 yamt
369 1.124 yamt mowner_convert_to_user(mo, &mo_user);
370 1.124 yamt
371 1.75 atatat if (oldp != NULL) {
372 1.124 yamt if (*oldlenp - len < sizeof(mo_user)) {
373 1.75 atatat error = ENOMEM;
374 1.75 atatat break;
375 1.75 atatat }
376 1.124 yamt error = copyout(&mo_user, (char *)oldp + len,
377 1.124 yamt sizeof(mo_user));
378 1.75 atatat if (error)
379 1.75 atatat break;
380 1.64 matt }
381 1.124 yamt len += sizeof(mo_user);
382 1.75 atatat }
383 1.75 atatat
384 1.75 atatat if (error == 0)
385 1.64 matt *oldlenp = len;
386 1.75 atatat
387 1.199 maxv return error;
388 1.75 atatat }
389 1.75 atatat #endif /* MBUFTRACE */
390 1.75 atatat
391 1.124 yamt static void
392 1.124 yamt mbstat_conver_to_user_cb(void *v1, void *v2, struct cpu_info *ci)
393 1.124 yamt {
394 1.124 yamt struct mbstat_cpu *mbsc = v1;
395 1.124 yamt struct mbstat *mbs = v2;
396 1.124 yamt int i;
397 1.124 yamt
398 1.124 yamt for (i = 0; i < __arraycount(mbs->m_mtypes); i++) {
399 1.124 yamt mbs->m_mtypes[i] += mbsc->m_mtypes[i];
400 1.124 yamt }
401 1.124 yamt }
402 1.124 yamt
403 1.124 yamt static void
404 1.124 yamt mbstat_convert_to_user(struct mbstat *mbs)
405 1.124 yamt {
406 1.124 yamt
407 1.124 yamt memset(mbs, 0, sizeof(*mbs));
408 1.124 yamt mbs->m_drain = mbstat.m_drain;
409 1.124 yamt percpu_foreach(mbstat_percpu, mbstat_conver_to_user_cb, mbs);
410 1.124 yamt }
411 1.124 yamt
412 1.124 yamt static int
413 1.124 yamt sysctl_kern_mbuf_stats(SYSCTLFN_ARGS)
414 1.124 yamt {
415 1.124 yamt struct sysctlnode node;
416 1.124 yamt struct mbstat mbs;
417 1.124 yamt
418 1.124 yamt mbstat_convert_to_user(&mbs);
419 1.124 yamt node = *rnode;
420 1.124 yamt node.sysctl_data = &mbs;
421 1.124 yamt node.sysctl_size = sizeof(mbs);
422 1.124 yamt return sysctl_lookup(SYSCTLFN_CALL(&node));
423 1.124 yamt }
424 1.124 yamt
425 1.129 pooka static void
426 1.131 cegger sysctl_kern_mbuf_setup(void)
427 1.75 atatat {
428 1.75 atatat
429 1.129 pooka KASSERT(mbuf_sysctllog == NULL);
430 1.129 pooka sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
431 1.80 atatat CTLFLAG_PERMANENT,
432 1.82 atatat CTLTYPE_NODE, "mbuf",
433 1.82 atatat SYSCTL_DESCR("mbuf control variables"),
434 1.75 atatat NULL, 0, NULL, 0,
435 1.75 atatat CTL_KERN, KERN_MBUF, CTL_EOL);
436 1.75 atatat
437 1.129 pooka sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
438 1.80 atatat CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
439 1.82 atatat CTLTYPE_INT, "msize",
440 1.82 atatat SYSCTL_DESCR("mbuf base size"),
441 1.75 atatat NULL, msize, NULL, 0,
442 1.75 atatat CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
443 1.129 pooka sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
444 1.80 atatat CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
445 1.82 atatat CTLTYPE_INT, "mclbytes",
446 1.82 atatat SYSCTL_DESCR("mbuf cluster size"),
447 1.75 atatat NULL, mclbytes, NULL, 0,
448 1.75 atatat CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
449 1.129 pooka sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
450 1.80 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
451 1.82 atatat CTLTYPE_INT, "nmbclusters",
452 1.82 atatat SYSCTL_DESCR("Limit on the number of mbuf clusters"),
453 1.75 atatat sysctl_kern_mbuf, 0, &nmbclusters, 0,
454 1.75 atatat CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
455 1.129 pooka sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
456 1.80 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
457 1.82 atatat CTLTYPE_INT, "mblowat",
458 1.82 atatat SYSCTL_DESCR("mbuf low water mark"),
459 1.75 atatat sysctl_kern_mbuf, 0, &mblowat, 0,
460 1.75 atatat CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
461 1.129 pooka sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
462 1.80 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
463 1.82 atatat CTLTYPE_INT, "mcllowat",
464 1.82 atatat SYSCTL_DESCR("mbuf cluster low water mark"),
465 1.75 atatat sysctl_kern_mbuf, 0, &mcllowat, 0,
466 1.75 atatat CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
467 1.129 pooka sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
468 1.80 atatat CTLFLAG_PERMANENT,
469 1.82 atatat CTLTYPE_STRUCT, "stats",
470 1.82 atatat SYSCTL_DESCR("mbuf allocation statistics"),
471 1.124 yamt sysctl_kern_mbuf_stats, 0, NULL, 0,
472 1.75 atatat CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
473 1.75 atatat #ifdef MBUFTRACE
474 1.129 pooka sysctl_createv(&mbuf_sysctllog, 0, NULL, NULL,
475 1.80 atatat CTLFLAG_PERMANENT,
476 1.82 atatat CTLTYPE_STRUCT, "mowners",
477 1.82 atatat SYSCTL_DESCR("Information about mbuf owners"),
478 1.75 atatat sysctl_kern_mbuf_mowners, 0, NULL, 0,
479 1.75 atatat CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
480 1.199 maxv #endif
481 1.28 thorpej }
482 1.28 thorpej
483 1.65 thorpej static int
484 1.116 yamt mb_ctor(void *arg, void *object, int flags)
485 1.65 thorpej {
486 1.65 thorpej struct mbuf *m = object;
487 1.65 thorpej
488 1.65 thorpej #ifdef POOL_VTOPHYS
489 1.65 thorpej m->m_paddr = POOL_VTOPHYS(m);
490 1.65 thorpej #else
491 1.65 thorpej m->m_paddr = M_PADDR_INVALID;
492 1.65 thorpej #endif
493 1.199 maxv return 0;
494 1.1 cgd }
495 1.1 cgd
496 1.182 maxv void
497 1.182 maxv m_pkthdr_remove(struct mbuf *m)
498 1.182 maxv {
499 1.182 maxv KASSERT(m->m_flags & M_PKTHDR);
500 1.182 maxv
501 1.190 maxv if (M_READONLY(m)) {
502 1.190 maxv /* Nothing we can do. */
503 1.190 maxv return;
504 1.190 maxv }
505 1.190 maxv
506 1.182 maxv m_tag_delete_chain(m, NULL);
507 1.182 maxv m->m_flags &= ~M_PKTHDR;
508 1.182 maxv memset(&m->m_pkthdr, 0, sizeof(m->m_pkthdr));
509 1.182 maxv }
510 1.182 maxv
511 1.150 christos /*
512 1.150 christos * Add mbuf to the end of a chain
513 1.150 christos */
514 1.150 christos struct mbuf *
515 1.179 maxv m_add(struct mbuf *c, struct mbuf *m)
516 1.179 maxv {
517 1.150 christos struct mbuf *n;
518 1.150 christos
519 1.150 christos if (c == NULL)
520 1.150 christos return m;
521 1.150 christos
522 1.150 christos for (n = c; n->m_next != NULL; n = n->m_next)
523 1.150 christos continue;
524 1.150 christos n->m_next = m;
525 1.150 christos return c;
526 1.150 christos }
527 1.150 christos
528 1.1 cgd struct mbuf *
529 1.62 thorpej m_get(int nowait, int type)
530 1.1 cgd {
531 1.27 matt struct mbuf *m;
532 1.1 cgd
533 1.142 dyoung KASSERT(type != MT_FREE);
534 1.142 dyoung
535 1.124 yamt m = pool_cache_get(mb_cache,
536 1.173 christos nowait == M_WAIT ? PR_WAITOK|PR_LIMITFAIL : PR_NOWAIT);
537 1.124 yamt if (m == NULL)
538 1.124 yamt return NULL;
539 1.124 yamt
540 1.124 yamt mbstat_type_add(type, 1);
541 1.164 knakahar
542 1.184 maxv mowner_init(m, type);
543 1.184 maxv m->m_ext_ref = m; /* default */
544 1.184 maxv m->m_type = type;
545 1.184 maxv m->m_len = 0;
546 1.184 maxv m->m_next = NULL;
547 1.184 maxv m->m_nextpkt = NULL; /* default */
548 1.184 maxv m->m_data = m->m_dat;
549 1.184 maxv m->m_flags = 0; /* default */
550 1.124 yamt
551 1.124 yamt return m;
552 1.1 cgd }
553 1.1 cgd
554 1.1 cgd struct mbuf *
555 1.62 thorpej m_gethdr(int nowait, int type)
556 1.1 cgd {
557 1.27 matt struct mbuf *m;
558 1.1 cgd
559 1.124 yamt m = m_get(nowait, type);
560 1.124 yamt if (m == NULL)
561 1.124 yamt return NULL;
562 1.124 yamt
563 1.184 maxv m->m_data = m->m_pktdat;
564 1.184 maxv m->m_flags = M_PKTHDR;
565 1.184 maxv
566 1.184 maxv m_reset_rcvif(m);
567 1.184 maxv m->m_pkthdr.len = 0;
568 1.184 maxv m->m_pkthdr.csum_flags = 0;
569 1.184 maxv m->m_pkthdr.csum_data = 0;
570 1.184 maxv SLIST_INIT(&m->m_pkthdr.tags);
571 1.184 maxv
572 1.184 maxv m->m_pkthdr.pattr_class = NULL;
573 1.184 maxv m->m_pkthdr.pattr_af = AF_UNSPEC;
574 1.184 maxv m->m_pkthdr.pattr_hdr = NULL;
575 1.124 yamt
576 1.124 yamt return m;
577 1.1 cgd }
578 1.1 cgd
579 1.64 matt void
580 1.64 matt m_clget(struct mbuf *m, int nowait)
581 1.64 matt {
582 1.71 simonb
583 1.197 maxv _MCLGET(m, mcl_cache, MCLBYTES, nowait);
584 1.64 matt }
585 1.64 matt
586 1.64 matt #ifdef MBUFTRACE
587 1.83 jonathan /*
588 1.83 jonathan * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
589 1.83 jonathan */
590 1.64 matt void
591 1.83 jonathan m_claimm(struct mbuf *m, struct mowner *mo)
592 1.64 matt {
593 1.71 simonb
594 1.64 matt for (; m != NULL; m = m->m_next)
595 1.64 matt MCLAIM(m, mo);
596 1.64 matt }
597 1.64 matt #endif
598 1.64 matt
599 1.1 cgd /*
600 1.194 maxv * Utility function for M_PREPEND. Do *NOT* use it directly.
601 1.1 cgd */
602 1.1 cgd struct mbuf *
603 1.62 thorpej m_prepend(struct mbuf *m, int len, int how)
604 1.1 cgd {
605 1.1 cgd struct mbuf *mn;
606 1.1 cgd
607 1.180 maxv if (__predict_false(len > MHLEN)) {
608 1.180 maxv panic("%s: len > MHLEN", __func__);
609 1.180 maxv }
610 1.180 maxv
611 1.156 christos KASSERT(len != M_COPYALL);
612 1.153 christos mn = m_get(how, m->m_type);
613 1.143 plunky if (mn == NULL) {
614 1.1 cgd m_freem(m);
615 1.179 maxv return NULL;
616 1.1 cgd }
617 1.178 maxv
618 1.1 cgd if (m->m_flags & M_PKTHDR) {
619 1.101 yamt M_MOVE_PKTHDR(mn, m);
620 1.64 matt } else {
621 1.64 matt MCLAIM(mn, m->m_owner);
622 1.1 cgd }
623 1.1 cgd mn->m_next = m;
624 1.1 cgd m = mn;
625 1.178 maxv
626 1.178 maxv if (m->m_flags & M_PKTHDR) {
627 1.178 maxv if (len < MHLEN)
628 1.178 maxv MH_ALIGN(m, len);
629 1.178 maxv } else {
630 1.178 maxv if (len < MLEN)
631 1.178 maxv M_ALIGN(m, len);
632 1.178 maxv }
633 1.178 maxv
634 1.1 cgd m->m_len = len;
635 1.179 maxv return m;
636 1.1 cgd }
637 1.1 cgd
638 1.1 cgd struct mbuf *
639 1.195 maxv m_copym(struct mbuf *m, int off, int len, int wait)
640 1.1 cgd {
641 1.195 maxv /* Shallow copy on M_EXT. */
642 1.195 maxv return m_copy_internal(m, off, len, wait, false);
643 1.44 itojun }
644 1.44 itojun
645 1.44 itojun struct mbuf *
646 1.195 maxv m_dup(struct mbuf *m, int off, int len, int wait)
647 1.44 itojun {
648 1.195 maxv /* Deep copy. */
649 1.195 maxv return m_copy_internal(m, off, len, wait, true);
650 1.44 itojun }
651 1.44 itojun
652 1.154 christos static inline int
653 1.179 maxv m_copylen(int len, int copylen)
654 1.179 maxv {
655 1.179 maxv return (len == M_COPYALL) ? copylen : min(len, copylen);
656 1.154 christos }
657 1.154 christos
658 1.44 itojun static struct mbuf *
659 1.195 maxv m_copy_internal(struct mbuf *m, int off0, int len, int wait, bool deep)
660 1.44 itojun {
661 1.27 matt struct mbuf *n, **np;
662 1.27 matt int off = off0;
663 1.1 cgd struct mbuf *top;
664 1.1 cgd int copyhdr = 0;
665 1.1 cgd
666 1.154 christos if (off < 0 || (len != M_COPYALL && len < 0))
667 1.196 maxv panic("%s: off %d, len %d", __func__, off, len);
668 1.1 cgd if (off == 0 && m->m_flags & M_PKTHDR)
669 1.1 cgd copyhdr = 1;
670 1.1 cgd while (off > 0) {
671 1.179 maxv if (m == NULL)
672 1.199 maxv panic("%s: m == NULL, off %d", __func__, off);
673 1.1 cgd if (off < m->m_len)
674 1.1 cgd break;
675 1.1 cgd off -= m->m_len;
676 1.1 cgd m = m->m_next;
677 1.1 cgd }
678 1.179 maxv
679 1.1 cgd np = ⊤
680 1.179 maxv top = NULL;
681 1.155 skrll while (len == M_COPYALL || len > 0) {
682 1.179 maxv if (m == NULL) {
683 1.1 cgd if (len != M_COPYALL)
684 1.196 maxv panic("%s: m == NULL, len %d [!COPYALL]",
685 1.196 maxv __func__, len);
686 1.1 cgd break;
687 1.1 cgd }
688 1.179 maxv
689 1.153 christos n = m_get(wait, m->m_type);
690 1.1 cgd *np = n;
691 1.179 maxv if (n == NULL)
692 1.1 cgd goto nospace;
693 1.64 matt MCLAIM(n, m->m_owner);
694 1.179 maxv
695 1.1 cgd if (copyhdr) {
696 1.1 cgd M_COPY_PKTHDR(n, m);
697 1.1 cgd if (len == M_COPYALL)
698 1.1 cgd n->m_pkthdr.len -= off0;
699 1.1 cgd else
700 1.1 cgd n->m_pkthdr.len = len;
701 1.1 cgd copyhdr = 0;
702 1.1 cgd }
703 1.154 christos n->m_len = m_copylen(len, m->m_len - off);
704 1.179 maxv
705 1.1 cgd if (m->m_flags & M_EXT) {
706 1.44 itojun if (!deep) {
707 1.44 itojun n->m_data = m->m_data + off;
708 1.44 itojun MCLADDREFERENCE(m, n);
709 1.44 itojun } else {
710 1.48 itojun /*
711 1.181 maxv * We don't care if MCLGET fails. n->m_len is
712 1.181 maxv * recomputed and handles that.
713 1.48 itojun */
714 1.44 itojun MCLGET(n, wait);
715 1.161 mlelstv n->m_len = 0;
716 1.50 itojun n->m_len = M_TRAILINGSPACE(n);
717 1.154 christos n->m_len = m_copylen(len, n->m_len);
718 1.50 itojun n->m_len = min(n->m_len, m->m_len - off);
719 1.119 christos memcpy(mtod(n, void *), mtod(m, char *) + off,
720 1.44 itojun (unsigned)n->m_len);
721 1.44 itojun }
722 1.179 maxv } else {
723 1.119 christos memcpy(mtod(n, void *), mtod(m, char *) + off,
724 1.1 cgd (unsigned)n->m_len);
725 1.179 maxv }
726 1.179 maxv
727 1.1 cgd if (len != M_COPYALL)
728 1.1 cgd len -= n->m_len;
729 1.50 itojun off += n->m_len;
730 1.199 maxv
731 1.199 maxv KASSERT(off <= m->m_len);
732 1.199 maxv
733 1.50 itojun if (off == m->m_len) {
734 1.50 itojun m = m->m_next;
735 1.50 itojun off = 0;
736 1.50 itojun }
737 1.1 cgd np = &n->m_next;
738 1.1 cgd }
739 1.179 maxv
740 1.179 maxv return top;
741 1.179 maxv
742 1.1 cgd nospace:
743 1.1 cgd m_freem(top);
744 1.179 maxv return NULL;
745 1.1 cgd }
746 1.1 cgd
747 1.1 cgd /*
748 1.18 thorpej * Copy an entire packet, including header (which must be present).
749 1.181 maxv * An optimization of the common case 'm_copym(m, 0, M_COPYALL, how)'.
750 1.18 thorpej */
751 1.18 thorpej struct mbuf *
752 1.62 thorpej m_copypacket(struct mbuf *m, int how)
753 1.18 thorpej {
754 1.18 thorpej struct mbuf *top, *n, *o;
755 1.18 thorpej
756 1.198 maxv if (__predict_false((m->m_flags & M_PKTHDR) == 0)) {
757 1.198 maxv panic("%s: no header (m = %p)", __func__, m);
758 1.198 maxv }
759 1.198 maxv
760 1.153 christos n = m_get(how, m->m_type);
761 1.18 thorpej top = n;
762 1.18 thorpej if (!n)
763 1.18 thorpej goto nospace;
764 1.18 thorpej
765 1.64 matt MCLAIM(n, m->m_owner);
766 1.18 thorpej M_COPY_PKTHDR(n, m);
767 1.18 thorpej n->m_len = m->m_len;
768 1.18 thorpej if (m->m_flags & M_EXT) {
769 1.18 thorpej n->m_data = m->m_data;
770 1.18 thorpej MCLADDREFERENCE(m, n);
771 1.18 thorpej } else {
772 1.30 perry memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
773 1.18 thorpej }
774 1.18 thorpej
775 1.18 thorpej m = m->m_next;
776 1.18 thorpej while (m) {
777 1.153 christos o = m_get(how, m->m_type);
778 1.18 thorpej if (!o)
779 1.18 thorpej goto nospace;
780 1.18 thorpej
781 1.64 matt MCLAIM(o, m->m_owner);
782 1.18 thorpej n->m_next = o;
783 1.18 thorpej n = n->m_next;
784 1.18 thorpej
785 1.18 thorpej n->m_len = m->m_len;
786 1.18 thorpej if (m->m_flags & M_EXT) {
787 1.18 thorpej n->m_data = m->m_data;
788 1.18 thorpej MCLADDREFERENCE(m, n);
789 1.18 thorpej } else {
790 1.30 perry memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
791 1.18 thorpej }
792 1.18 thorpej
793 1.18 thorpej m = m->m_next;
794 1.18 thorpej }
795 1.18 thorpej return top;
796 1.181 maxv
797 1.18 thorpej nospace:
798 1.18 thorpej m_freem(top);
799 1.71 simonb return NULL;
800 1.18 thorpej }
801 1.18 thorpej
802 1.14 christos void
803 1.200 maxv m_copydata(struct mbuf *m, int off, int len, void *cp)
804 1.1 cgd {
805 1.200 maxv unsigned int count;
806 1.179 maxv struct mbuf *m0 = m;
807 1.179 maxv int len0 = len;
808 1.179 maxv int off0 = off;
809 1.200 maxv void *cp0 = cp;
810 1.1 cgd
811 1.156 christos KASSERT(len != M_COPYALL);
812 1.1 cgd if (off < 0 || len < 0)
813 1.90 matt panic("m_copydata: off %d, len %d", off, len);
814 1.1 cgd while (off > 0) {
815 1.94 tron if (m == NULL)
816 1.151 matt panic("m_copydata(%p,%d,%d,%p): m=NULL, off=%d (%d)",
817 1.200 maxv m0, len0, off0, cp0, off, off0 - off);
818 1.1 cgd if (off < m->m_len)
819 1.1 cgd break;
820 1.1 cgd off -= m->m_len;
821 1.1 cgd m = m->m_next;
822 1.1 cgd }
823 1.1 cgd while (len > 0) {
824 1.94 tron if (m == NULL)
825 1.151 matt panic("m_copydata(%p,%d,%d,%p): "
826 1.151 matt "m=NULL, off=%d (%d), len=%d (%d)",
827 1.200 maxv m0, len0, off0, cp0,
828 1.151 matt off, off0 - off, len, len0 - len);
829 1.9 mycroft count = min(m->m_len - off, len);
830 1.119 christos memcpy(cp, mtod(m, char *) + off, count);
831 1.1 cgd len -= count;
832 1.119 christos cp = (char *)cp + count;
833 1.1 cgd off = 0;
834 1.1 cgd m = m->m_next;
835 1.1 cgd }
836 1.1 cgd }
837 1.1 cgd
838 1.1 cgd /*
839 1.1 cgd * Concatenate mbuf chain n to m.
840 1.72 itojun * n might be copied into m (when n->m_len is small), therefore data portion of
841 1.72 itojun * n could be copied into an mbuf of different mbuf type.
842 1.1 cgd * Any m_pkthdr is not updated.
843 1.1 cgd */
844 1.14 christos void
845 1.62 thorpej m_cat(struct mbuf *m, struct mbuf *n)
846 1.1 cgd {
847 1.73 yamt
848 1.1 cgd while (m->m_next)
849 1.1 cgd m = m->m_next;
850 1.1 cgd while (n) {
851 1.77 itojun if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
852 1.1 cgd /* just join the two chains */
853 1.1 cgd m->m_next = n;
854 1.1 cgd return;
855 1.1 cgd }
856 1.1 cgd /* splat the data from one into the other */
857 1.119 christos memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
858 1.1 cgd (u_int)n->m_len);
859 1.1 cgd m->m_len += n->m_len;
860 1.1 cgd n = m_free(n);
861 1.1 cgd }
862 1.1 cgd }
863 1.1 cgd
864 1.11 mycroft void
865 1.62 thorpej m_adj(struct mbuf *mp, int req_len)
866 1.1 cgd {
867 1.27 matt int len = req_len;
868 1.27 matt struct mbuf *m;
869 1.27 matt int count;
870 1.1 cgd
871 1.1 cgd if ((m = mp) == NULL)
872 1.1 cgd return;
873 1.1 cgd if (len >= 0) {
874 1.1 cgd /*
875 1.1 cgd * Trim from head.
876 1.1 cgd */
877 1.1 cgd while (m != NULL && len > 0) {
878 1.1 cgd if (m->m_len <= len) {
879 1.1 cgd len -= m->m_len;
880 1.1 cgd m->m_len = 0;
881 1.1 cgd m = m->m_next;
882 1.1 cgd } else {
883 1.1 cgd m->m_len -= len;
884 1.1 cgd m->m_data += len;
885 1.1 cgd len = 0;
886 1.1 cgd }
887 1.1 cgd }
888 1.1 cgd if (mp->m_flags & M_PKTHDR)
889 1.181 maxv mp->m_pkthdr.len -= (req_len - len);
890 1.1 cgd } else {
891 1.1 cgd /*
892 1.1 cgd * Trim from tail. Scan the mbuf chain,
893 1.1 cgd * calculating its length and finding the last mbuf.
894 1.1 cgd * If the adjustment only affects this mbuf, then just
895 1.1 cgd * adjust and return. Otherwise, rescan and truncate
896 1.1 cgd * after the remaining size.
897 1.1 cgd */
898 1.1 cgd len = -len;
899 1.1 cgd count = 0;
900 1.1 cgd for (;;) {
901 1.1 cgd count += m->m_len;
902 1.181 maxv if (m->m_next == NULL)
903 1.1 cgd break;
904 1.1 cgd m = m->m_next;
905 1.1 cgd }
906 1.1 cgd if (m->m_len >= len) {
907 1.1 cgd m->m_len -= len;
908 1.8 deraadt if (mp->m_flags & M_PKTHDR)
909 1.8 deraadt mp->m_pkthdr.len -= len;
910 1.1 cgd return;
911 1.1 cgd }
912 1.181 maxv
913 1.1 cgd count -= len;
914 1.1 cgd if (count < 0)
915 1.1 cgd count = 0;
916 1.181 maxv
917 1.1 cgd /*
918 1.1 cgd * Correct length for chain is "count".
919 1.1 cgd * Find the mbuf with last data, adjust its length,
920 1.1 cgd * and toss data from remaining mbufs on chain.
921 1.1 cgd */
922 1.1 cgd m = mp;
923 1.1 cgd if (m->m_flags & M_PKTHDR)
924 1.1 cgd m->m_pkthdr.len = count;
925 1.1 cgd for (; m; m = m->m_next) {
926 1.1 cgd if (m->m_len >= count) {
927 1.1 cgd m->m_len = count;
928 1.1 cgd break;
929 1.1 cgd }
930 1.1 cgd count -= m->m_len;
931 1.1 cgd }
932 1.181 maxv if (m) {
933 1.110 christos while (m->m_next)
934 1.110 christos (m = m->m_next)->m_len = 0;
935 1.181 maxv }
936 1.1 cgd }
937 1.1 cgd }
938 1.1 cgd
939 1.1 cgd /*
940 1.148 rmind * m_ensure_contig: rearrange an mbuf chain that given length of bytes
941 1.148 rmind * would be contiguous and in the data area of an mbuf (therefore, mtod()
942 1.148 rmind * would work for a structure of given length).
943 1.148 rmind *
944 1.148 rmind * => On success, returns true and the resulting mbuf chain; false otherwise.
945 1.148 rmind * => The mbuf chain may change, but is always preserved valid.
946 1.1 cgd */
947 1.148 rmind bool
948 1.148 rmind m_ensure_contig(struct mbuf **m0, int len)
949 1.1 cgd {
950 1.148 rmind struct mbuf *n = *m0, *m;
951 1.148 rmind size_t count, space;
952 1.1 cgd
953 1.156 christos KASSERT(len != M_COPYALL);
954 1.1 cgd /*
955 1.1 cgd * If first mbuf has no cluster, and has room for len bytes
956 1.1 cgd * without shifting current data, pullup into it,
957 1.1 cgd * otherwise allocate a new mbuf to prepend to the chain.
958 1.1 cgd */
959 1.1 cgd if ((n->m_flags & M_EXT) == 0 &&
960 1.1 cgd n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
961 1.148 rmind if (n->m_len >= len) {
962 1.148 rmind return true;
963 1.148 rmind }
964 1.1 cgd m = n;
965 1.1 cgd n = n->m_next;
966 1.1 cgd len -= m->m_len;
967 1.1 cgd } else {
968 1.148 rmind if (len > MHLEN) {
969 1.148 rmind return false;
970 1.148 rmind }
971 1.153 christos m = m_get(M_DONTWAIT, n->m_type);
972 1.148 rmind if (m == NULL) {
973 1.148 rmind return false;
974 1.148 rmind }
975 1.64 matt MCLAIM(m, n->m_owner);
976 1.1 cgd if (n->m_flags & M_PKTHDR) {
977 1.101 yamt M_MOVE_PKTHDR(m, n);
978 1.1 cgd }
979 1.1 cgd }
980 1.1 cgd space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
981 1.1 cgd do {
982 1.148 rmind count = MIN(MIN(MAX(len, max_protohdr), space), n->m_len);
983 1.119 christos memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
984 1.1 cgd (unsigned)count);
985 1.1 cgd len -= count;
986 1.1 cgd m->m_len += count;
987 1.1 cgd n->m_len -= count;
988 1.1 cgd space -= count;
989 1.1 cgd if (n->m_len)
990 1.1 cgd n->m_data += count;
991 1.1 cgd else
992 1.1 cgd n = m_free(n);
993 1.1 cgd } while (len > 0 && n);
994 1.148 rmind
995 1.148 rmind m->m_next = n;
996 1.148 rmind *m0 = m;
997 1.148 rmind
998 1.148 rmind return len <= 0;
999 1.148 rmind }
1000 1.148 rmind
1001 1.148 rmind /*
1002 1.148 rmind * m_pullup: same as m_ensure_contig(), but destroys mbuf chain on error.
1003 1.148 rmind */
1004 1.148 rmind struct mbuf *
1005 1.148 rmind m_pullup(struct mbuf *n, int len)
1006 1.148 rmind {
1007 1.148 rmind struct mbuf *m = n;
1008 1.148 rmind
1009 1.156 christos KASSERT(len != M_COPYALL);
1010 1.148 rmind if (!m_ensure_contig(&m, len)) {
1011 1.148 rmind KASSERT(m != NULL);
1012 1.148 rmind m_freem(m);
1013 1.148 rmind m = NULL;
1014 1.1 cgd }
1015 1.148 rmind return m;
1016 1.60 thorpej }
1017 1.60 thorpej
1018 1.60 thorpej /*
1019 1.60 thorpej * Like m_pullup(), except a new mbuf is always allocated, and we allow
1020 1.60 thorpej * the amount of empty space before the data in the new mbuf to be specified
1021 1.60 thorpej * (in the event that the caller expects to prepend later).
1022 1.60 thorpej */
1023 1.60 thorpej struct mbuf *
1024 1.60 thorpej m_copyup(struct mbuf *n, int len, int dstoff)
1025 1.60 thorpej {
1026 1.60 thorpej struct mbuf *m;
1027 1.60 thorpej int count, space;
1028 1.60 thorpej
1029 1.156 christos KASSERT(len != M_COPYALL);
1030 1.193 maxv if (len > ((int)MHLEN - dstoff))
1031 1.60 thorpej goto bad;
1032 1.153 christos m = m_get(M_DONTWAIT, n->m_type);
1033 1.60 thorpej if (m == NULL)
1034 1.60 thorpej goto bad;
1035 1.64 matt MCLAIM(m, n->m_owner);
1036 1.60 thorpej if (n->m_flags & M_PKTHDR) {
1037 1.101 yamt M_MOVE_PKTHDR(m, n);
1038 1.60 thorpej }
1039 1.60 thorpej m->m_data += dstoff;
1040 1.60 thorpej space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1041 1.60 thorpej do {
1042 1.60 thorpej count = min(min(max(len, max_protohdr), space), n->m_len);
1043 1.119 christos memcpy(mtod(m, char *) + m->m_len, mtod(n, void *),
1044 1.60 thorpej (unsigned)count);
1045 1.60 thorpej len -= count;
1046 1.60 thorpej m->m_len += count;
1047 1.60 thorpej n->m_len -= count;
1048 1.60 thorpej space -= count;
1049 1.60 thorpej if (n->m_len)
1050 1.60 thorpej n->m_data += count;
1051 1.60 thorpej else
1052 1.60 thorpej n = m_free(n);
1053 1.60 thorpej } while (len > 0 && n);
1054 1.60 thorpej if (len > 0) {
1055 1.60 thorpej (void) m_free(m);
1056 1.60 thorpej goto bad;
1057 1.60 thorpej }
1058 1.60 thorpej m->m_next = n;
1059 1.199 maxv return m;
1060 1.60 thorpej bad:
1061 1.60 thorpej m_freem(n);
1062 1.199 maxv return NULL;
1063 1.9 mycroft }
1064 1.9 mycroft
1065 1.9 mycroft struct mbuf *
1066 1.195 maxv m_split(struct mbuf *m0, int len, int wait)
1067 1.9 mycroft {
1068 1.195 maxv return m_split_internal(m0, len, wait, true);
1069 1.85 yamt }
1070 1.85 yamt
1071 1.85 yamt static struct mbuf *
1072 1.195 maxv m_split_internal(struct mbuf *m0, int len0, int wait, bool copyhdr)
1073 1.85 yamt {
1074 1.27 matt struct mbuf *m, *n;
1075 1.22 thorpej unsigned len = len0, remain, len_save;
1076 1.9 mycroft
1077 1.156 christos KASSERT(len0 != M_COPYALL);
1078 1.9 mycroft for (m = m0; m && len > m->m_len; m = m->m_next)
1079 1.9 mycroft len -= m->m_len;
1080 1.181 maxv if (m == NULL)
1081 1.181 maxv return NULL;
1082 1.181 maxv
1083 1.9 mycroft remain = m->m_len - len;
1084 1.85 yamt if (copyhdr && (m0->m_flags & M_PKTHDR)) {
1085 1.153 christos n = m_gethdr(wait, m0->m_type);
1086 1.153 christos if (n == NULL)
1087 1.153 christos return NULL;
1088 1.181 maxv
1089 1.112 pavel MCLAIM(n, m0->m_owner);
1090 1.167 ozaki m_copy_rcvif(n, m0);
1091 1.9 mycroft n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1092 1.22 thorpej len_save = m0->m_pkthdr.len;
1093 1.9 mycroft m0->m_pkthdr.len = len0;
1094 1.181 maxv
1095 1.9 mycroft if (m->m_flags & M_EXT)
1096 1.9 mycroft goto extpacket;
1097 1.181 maxv
1098 1.9 mycroft if (remain > MHLEN) {
1099 1.9 mycroft /* m can't be the lead packet */
1100 1.9 mycroft MH_ALIGN(n, 0);
1101 1.132 bouyer n->m_len = 0;
1102 1.9 mycroft n->m_next = m_split(m, len, wait);
1103 1.181 maxv if (n->m_next == NULL) {
1104 1.181 maxv (void)m_free(n);
1105 1.22 thorpej m0->m_pkthdr.len = len_save;
1106 1.181 maxv return NULL;
1107 1.181 maxv }
1108 1.181 maxv return n;
1109 1.181 maxv } else {
1110 1.9 mycroft MH_ALIGN(n, remain);
1111 1.181 maxv }
1112 1.9 mycroft } else if (remain == 0) {
1113 1.9 mycroft n = m->m_next;
1114 1.181 maxv m->m_next = NULL;
1115 1.181 maxv return n;
1116 1.9 mycroft } else {
1117 1.153 christos n = m_get(wait, m->m_type);
1118 1.181 maxv if (n == NULL)
1119 1.181 maxv return NULL;
1120 1.64 matt MCLAIM(n, m->m_owner);
1121 1.9 mycroft M_ALIGN(n, remain);
1122 1.9 mycroft }
1123 1.181 maxv
1124 1.9 mycroft extpacket:
1125 1.9 mycroft if (m->m_flags & M_EXT) {
1126 1.125 yamt n->m_data = m->m_data + len;
1127 1.18 thorpej MCLADDREFERENCE(m, n);
1128 1.9 mycroft } else {
1129 1.119 christos memcpy(mtod(n, void *), mtod(m, char *) + len, remain);
1130 1.9 mycroft }
1131 1.181 maxv
1132 1.9 mycroft n->m_len = remain;
1133 1.9 mycroft m->m_len = len;
1134 1.9 mycroft n->m_next = m->m_next;
1135 1.181 maxv m->m_next = NULL;
1136 1.181 maxv return n;
1137 1.9 mycroft }
1138 1.181 maxv
1139 1.9 mycroft /*
1140 1.9 mycroft * Routine to copy from device local memory into mbufs.
1141 1.9 mycroft */
1142 1.9 mycroft struct mbuf *
1143 1.62 thorpej m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
1144 1.62 thorpej void (*copy)(const void *from, void *to, size_t len))
1145 1.9 mycroft {
1146 1.27 matt struct mbuf *m;
1147 1.181 maxv struct mbuf *top = NULL, **mp = ⊤
1148 1.27 matt int off = off0, len;
1149 1.181 maxv char *cp, *epkt;
1150 1.9 mycroft
1151 1.9 mycroft cp = buf;
1152 1.9 mycroft epkt = cp + totlen;
1153 1.9 mycroft if (off) {
1154 1.13 cgd /*
1155 1.13 cgd * If 'off' is non-zero, packet is trailer-encapsulated,
1156 1.13 cgd * so we have to skip the type and length fields.
1157 1.13 cgd */
1158 1.104 perry cp += off + 2 * sizeof(uint16_t);
1159 1.104 perry totlen -= 2 * sizeof(uint16_t);
1160 1.9 mycroft }
1161 1.181 maxv
1162 1.153 christos m = m_gethdr(M_DONTWAIT, MT_DATA);
1163 1.153 christos if (m == NULL)
1164 1.153 christos return NULL;
1165 1.166 ozaki m_set_rcvif(m, ifp);
1166 1.9 mycroft m->m_pkthdr.len = totlen;
1167 1.9 mycroft m->m_len = MHLEN;
1168 1.9 mycroft
1169 1.9 mycroft while (totlen > 0) {
1170 1.9 mycroft if (top) {
1171 1.153 christos m = m_get(M_DONTWAIT, MT_DATA);
1172 1.181 maxv if (m == NULL) {
1173 1.9 mycroft m_freem(top);
1174 1.181 maxv return NULL;
1175 1.9 mycroft }
1176 1.9 mycroft m->m_len = MLEN;
1177 1.9 mycroft }
1178 1.181 maxv
1179 1.9 mycroft len = min(totlen, epkt - cp);
1180 1.181 maxv
1181 1.9 mycroft if (len >= MINCLSIZE) {
1182 1.9 mycroft MCLGET(m, M_DONTWAIT);
1183 1.19 mycroft if ((m->m_flags & M_EXT) == 0) {
1184 1.20 mycroft m_free(m);
1185 1.19 mycroft m_freem(top);
1186 1.181 maxv return NULL;
1187 1.19 mycroft }
1188 1.19 mycroft m->m_len = len = min(len, MCLBYTES);
1189 1.9 mycroft } else {
1190 1.9 mycroft /*
1191 1.9 mycroft * Place initial small packet/header at end of mbuf.
1192 1.9 mycroft */
1193 1.9 mycroft if (len < m->m_len) {
1194 1.9 mycroft if (top == 0 && len + max_linkhdr <= m->m_len)
1195 1.9 mycroft m->m_data += max_linkhdr;
1196 1.9 mycroft m->m_len = len;
1197 1.9 mycroft } else
1198 1.9 mycroft len = m->m_len;
1199 1.9 mycroft }
1200 1.181 maxv
1201 1.9 mycroft if (copy)
1202 1.119 christos copy(cp, mtod(m, void *), (size_t)len);
1203 1.9 mycroft else
1204 1.119 christos memcpy(mtod(m, void *), cp, (size_t)len);
1205 1.181 maxv
1206 1.9 mycroft cp += len;
1207 1.9 mycroft *mp = m;
1208 1.9 mycroft mp = &m->m_next;
1209 1.9 mycroft totlen -= len;
1210 1.9 mycroft if (cp == epkt)
1211 1.9 mycroft cp = buf;
1212 1.9 mycroft }
1213 1.181 maxv
1214 1.181 maxv return top;
1215 1.18 thorpej }
1216 1.18 thorpej
1217 1.18 thorpej /*
1218 1.18 thorpej * Copy data from a buffer back into the indicated mbuf chain,
1219 1.18 thorpej * starting "off" bytes from the beginning, extending the mbuf
1220 1.18 thorpej * chain if necessary.
1221 1.18 thorpej */
1222 1.18 thorpej void
1223 1.86 yamt m_copyback(struct mbuf *m0, int off, int len, const void *cp)
1224 1.18 thorpej {
1225 1.85 yamt #if defined(DEBUG)
1226 1.85 yamt struct mbuf *origm = m0;
1227 1.85 yamt int error;
1228 1.181 maxv #endif
1229 1.85 yamt
1230 1.85 yamt if (m0 == NULL)
1231 1.85 yamt return;
1232 1.85 yamt
1233 1.85 yamt #if defined(DEBUG)
1234 1.85 yamt error =
1235 1.181 maxv #endif
1236 1.196 maxv m_copyback_internal(&m0, off, len, cp, CB_COPYBACK|CB_EXTEND,
1237 1.196 maxv M_DONTWAIT);
1238 1.85 yamt
1239 1.85 yamt #if defined(DEBUG)
1240 1.85 yamt if (error != 0 || (m0 != NULL && origm != m0))
1241 1.85 yamt panic("m_copyback");
1242 1.181 maxv #endif
1243 1.85 yamt }
1244 1.85 yamt
1245 1.85 yamt struct mbuf *
1246 1.86 yamt m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
1247 1.85 yamt {
1248 1.85 yamt int error;
1249 1.85 yamt
1250 1.85 yamt /* don't support chain expansion */
1251 1.156 christos KASSERT(len != M_COPYALL);
1252 1.85 yamt KDASSERT(off + len <= m_length(m0));
1253 1.85 yamt
1254 1.196 maxv error = m_copyback_internal(&m0, off, len, cp, CB_COPYBACK|CB_COW,
1255 1.196 maxv how);
1256 1.85 yamt if (error) {
1257 1.85 yamt /*
1258 1.85 yamt * no way to recover from partial success.
1259 1.85 yamt * just free the chain.
1260 1.85 yamt */
1261 1.85 yamt m_freem(m0);
1262 1.85 yamt return NULL;
1263 1.85 yamt }
1264 1.85 yamt return m0;
1265 1.85 yamt }
1266 1.85 yamt
1267 1.85 yamt int
1268 1.85 yamt m_makewritable(struct mbuf **mp, int off, int len, int how)
1269 1.85 yamt {
1270 1.85 yamt int error;
1271 1.85 yamt #if defined(DEBUG)
1272 1.156 christos int origlen = m_length(*mp);
1273 1.181 maxv #endif
1274 1.85 yamt
1275 1.196 maxv error = m_copyback_internal(mp, off, len, NULL, CB_PRESERVE|CB_COW,
1276 1.196 maxv how);
1277 1.170 christos if (error)
1278 1.170 christos return error;
1279 1.170 christos
1280 1.85 yamt #if defined(DEBUG)
1281 1.156 christos int reslen = 0;
1282 1.156 christos for (struct mbuf *n = *mp; n; n = n->m_next)
1283 1.85 yamt reslen += n->m_len;
1284 1.85 yamt if (origlen != reslen)
1285 1.85 yamt panic("m_makewritable: length changed");
1286 1.85 yamt if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
1287 1.85 yamt panic("m_makewritable: inconsist");
1288 1.181 maxv #endif
1289 1.85 yamt
1290 1.170 christos return 0;
1291 1.85 yamt }
1292 1.85 yamt
1293 1.142 dyoung /*
1294 1.142 dyoung * Copy the mbuf chain to a new mbuf chain that is as short as possible.
1295 1.142 dyoung * Return the new mbuf chain on success, NULL on failure. On success,
1296 1.142 dyoung * free the old mbuf chain.
1297 1.142 dyoung */
1298 1.142 dyoung struct mbuf *
1299 1.142 dyoung m_defrag(struct mbuf *mold, int flags)
1300 1.142 dyoung {
1301 1.142 dyoung struct mbuf *m0, *mn, *n;
1302 1.142 dyoung size_t sz = mold->m_pkthdr.len;
1303 1.142 dyoung
1304 1.181 maxv KASSERT((mold->m_flags & M_PKTHDR) != 0);
1305 1.142 dyoung
1306 1.153 christos m0 = m_gethdr(flags, MT_DATA);
1307 1.142 dyoung if (m0 == NULL)
1308 1.142 dyoung return NULL;
1309 1.142 dyoung M_COPY_PKTHDR(m0, mold);
1310 1.142 dyoung mn = m0;
1311 1.142 dyoung
1312 1.142 dyoung do {
1313 1.142 dyoung if (sz > MHLEN) {
1314 1.142 dyoung MCLGET(mn, M_DONTWAIT);
1315 1.142 dyoung if ((mn->m_flags & M_EXT) == 0) {
1316 1.142 dyoung m_freem(m0);
1317 1.142 dyoung return NULL;
1318 1.142 dyoung }
1319 1.142 dyoung }
1320 1.142 dyoung
1321 1.142 dyoung mn->m_len = MIN(sz, MCLBYTES);
1322 1.142 dyoung
1323 1.142 dyoung m_copydata(mold, mold->m_pkthdr.len - sz, mn->m_len,
1324 1.142 dyoung mtod(mn, void *));
1325 1.142 dyoung
1326 1.142 dyoung sz -= mn->m_len;
1327 1.142 dyoung
1328 1.142 dyoung if (sz > 0) {
1329 1.142 dyoung /* need more mbufs */
1330 1.153 christos n = m_get(M_NOWAIT, MT_DATA);
1331 1.142 dyoung if (n == NULL) {
1332 1.142 dyoung m_freem(m0);
1333 1.142 dyoung return NULL;
1334 1.142 dyoung }
1335 1.142 dyoung
1336 1.142 dyoung mn->m_next = n;
1337 1.142 dyoung mn = n;
1338 1.142 dyoung }
1339 1.142 dyoung } while (sz > 0);
1340 1.142 dyoung
1341 1.142 dyoung m_freem(mold);
1342 1.142 dyoung
1343 1.142 dyoung return m0;
1344 1.142 dyoung }
1345 1.142 dyoung
1346 1.196 maxv static int
1347 1.196 maxv m_copyback_internal(struct mbuf **mp0, int off, int len, const void *vp,
1348 1.196 maxv int flags, int how)
1349 1.85 yamt {
1350 1.27 matt int mlen;
1351 1.85 yamt struct mbuf *m, *n;
1352 1.85 yamt struct mbuf **mp;
1353 1.18 thorpej int totlen = 0;
1354 1.86 yamt const char *cp = vp;
1355 1.18 thorpej
1356 1.85 yamt KASSERT(mp0 != NULL);
1357 1.85 yamt KASSERT(*mp0 != NULL);
1358 1.196 maxv KASSERT((flags & CB_PRESERVE) == 0 || cp == NULL);
1359 1.196 maxv KASSERT((flags & CB_COPYBACK) == 0 || cp != NULL);
1360 1.85 yamt
1361 1.156 christos if (len == M_COPYALL)
1362 1.156 christos len = m_length(*mp0) - off;
1363 1.156 christos
1364 1.106 yamt /*
1365 1.196 maxv * we don't bother to update "totlen" in the case of CB_COW,
1366 1.196 maxv * assuming that CB_EXTEND and CB_COW are exclusive.
1367 1.106 yamt */
1368 1.106 yamt
1369 1.196 maxv KASSERT((~flags & (CB_EXTEND|CB_COW)) != 0);
1370 1.106 yamt
1371 1.85 yamt mp = mp0;
1372 1.85 yamt m = *mp;
1373 1.18 thorpej while (off > (mlen = m->m_len)) {
1374 1.18 thorpej off -= mlen;
1375 1.18 thorpej totlen += mlen;
1376 1.109 yamt if (m->m_next == NULL) {
1377 1.109 yamt int tspace;
1378 1.109 yamt extend:
1379 1.196 maxv if ((flags & CB_EXTEND) == 0)
1380 1.85 yamt goto out;
1381 1.109 yamt
1382 1.109 yamt /*
1383 1.109 yamt * try to make some space at the end of "m".
1384 1.109 yamt */
1385 1.109 yamt
1386 1.109 yamt mlen = m->m_len;
1387 1.109 yamt if (off + len >= MINCLSIZE &&
1388 1.109 yamt (m->m_flags & M_EXT) == 0 && m->m_len == 0) {
1389 1.109 yamt MCLGET(m, how);
1390 1.109 yamt }
1391 1.109 yamt tspace = M_TRAILINGSPACE(m);
1392 1.109 yamt if (tspace > 0) {
1393 1.109 yamt tspace = min(tspace, off + len);
1394 1.109 yamt KASSERT(tspace > 0);
1395 1.109 yamt memset(mtod(m, char *) + m->m_len, 0,
1396 1.109 yamt min(off, tspace));
1397 1.109 yamt m->m_len += tspace;
1398 1.109 yamt off += mlen;
1399 1.109 yamt totlen -= mlen;
1400 1.109 yamt continue;
1401 1.109 yamt }
1402 1.109 yamt
1403 1.109 yamt /*
1404 1.109 yamt * need to allocate an mbuf.
1405 1.109 yamt */
1406 1.109 yamt
1407 1.109 yamt if (off + len >= MINCLSIZE) {
1408 1.109 yamt n = m_getcl(how, m->m_type, 0);
1409 1.109 yamt } else {
1410 1.109 yamt n = m_get(how, m->m_type);
1411 1.109 yamt }
1412 1.109 yamt if (n == NULL) {
1413 1.18 thorpej goto out;
1414 1.109 yamt }
1415 1.109 yamt n->m_len = min(M_TRAILINGSPACE(n), off + len);
1416 1.109 yamt memset(mtod(n, char *), 0, min(n->m_len, off));
1417 1.18 thorpej m->m_next = n;
1418 1.18 thorpej }
1419 1.85 yamt mp = &m->m_next;
1420 1.18 thorpej m = m->m_next;
1421 1.18 thorpej }
1422 1.18 thorpej while (len > 0) {
1423 1.85 yamt mlen = m->m_len - off;
1424 1.85 yamt if (mlen != 0 && M_READONLY(m)) {
1425 1.196 maxv /*
1426 1.196 maxv * This mbuf is read-only. Allocate a new writable
1427 1.196 maxv * mbuf and try again.
1428 1.196 maxv */
1429 1.85 yamt char *datap;
1430 1.85 yamt int eatlen;
1431 1.85 yamt
1432 1.196 maxv KASSERT((flags & CB_COW) != 0);
1433 1.85 yamt
1434 1.85 yamt /*
1435 1.85 yamt * if we're going to write into the middle of
1436 1.85 yamt * a mbuf, split it first.
1437 1.85 yamt */
1438 1.137 seanb if (off > 0) {
1439 1.195 maxv n = m_split_internal(m, off, how, false);
1440 1.85 yamt if (n == NULL)
1441 1.85 yamt goto enobufs;
1442 1.85 yamt m->m_next = n;
1443 1.85 yamt mp = &m->m_next;
1444 1.85 yamt m = n;
1445 1.85 yamt off = 0;
1446 1.85 yamt continue;
1447 1.85 yamt }
1448 1.85 yamt
1449 1.85 yamt /*
1450 1.85 yamt * XXX TODO coalesce into the trailingspace of
1451 1.85 yamt * the previous mbuf when possible.
1452 1.85 yamt */
1453 1.85 yamt
1454 1.85 yamt /*
1455 1.85 yamt * allocate a new mbuf. copy packet header if needed.
1456 1.85 yamt */
1457 1.153 christos n = m_get(how, m->m_type);
1458 1.85 yamt if (n == NULL)
1459 1.85 yamt goto enobufs;
1460 1.85 yamt MCLAIM(n, m->m_owner);
1461 1.85 yamt if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
1462 1.101 yamt M_MOVE_PKTHDR(n, m);
1463 1.85 yamt n->m_len = MHLEN;
1464 1.85 yamt } else {
1465 1.85 yamt if (len >= MINCLSIZE)
1466 1.85 yamt MCLGET(n, M_DONTWAIT);
1467 1.85 yamt n->m_len =
1468 1.85 yamt (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
1469 1.85 yamt }
1470 1.85 yamt if (n->m_len > len)
1471 1.85 yamt n->m_len = len;
1472 1.85 yamt
1473 1.85 yamt /*
1474 1.85 yamt * free the region which has been overwritten.
1475 1.85 yamt * copying data from old mbufs if requested.
1476 1.85 yamt */
1477 1.196 maxv if (flags & CB_PRESERVE)
1478 1.85 yamt datap = mtod(n, char *);
1479 1.85 yamt else
1480 1.85 yamt datap = NULL;
1481 1.85 yamt eatlen = n->m_len;
1482 1.85 yamt while (m != NULL && M_READONLY(m) &&
1483 1.85 yamt n->m_type == m->m_type && eatlen > 0) {
1484 1.85 yamt mlen = min(eatlen, m->m_len);
1485 1.85 yamt if (datap) {
1486 1.85 yamt m_copydata(m, 0, mlen, datap);
1487 1.85 yamt datap += mlen;
1488 1.85 yamt }
1489 1.85 yamt m->m_data += mlen;
1490 1.85 yamt m->m_len -= mlen;
1491 1.85 yamt eatlen -= mlen;
1492 1.85 yamt if (m->m_len == 0)
1493 1.85 yamt *mp = m = m_free(m);
1494 1.85 yamt }
1495 1.85 yamt if (eatlen > 0)
1496 1.85 yamt n->m_len -= eatlen;
1497 1.85 yamt n->m_next = m;
1498 1.85 yamt *mp = m = n;
1499 1.85 yamt continue;
1500 1.85 yamt }
1501 1.85 yamt mlen = min(mlen, len);
1502 1.196 maxv if (flags & CB_COPYBACK) {
1503 1.119 christos memcpy(mtod(m, char *) + off, cp, (unsigned)mlen);
1504 1.85 yamt cp += mlen;
1505 1.85 yamt }
1506 1.18 thorpej len -= mlen;
1507 1.18 thorpej mlen += off;
1508 1.18 thorpej off = 0;
1509 1.18 thorpej totlen += mlen;
1510 1.18 thorpej if (len == 0)
1511 1.18 thorpej break;
1512 1.109 yamt if (m->m_next == NULL) {
1513 1.109 yamt goto extend;
1514 1.18 thorpej }
1515 1.85 yamt mp = &m->m_next;
1516 1.18 thorpej m = m->m_next;
1517 1.18 thorpej }
1518 1.199 maxv
1519 1.199 maxv out:
1520 1.199 maxv if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) {
1521 1.196 maxv KASSERT((flags & CB_EXTEND) != 0);
1522 1.18 thorpej m->m_pkthdr.len = totlen;
1523 1.106 yamt }
1524 1.85 yamt
1525 1.85 yamt return 0;
1526 1.85 yamt
1527 1.85 yamt enobufs:
1528 1.85 yamt return ENOBUFS;
1529 1.66 thorpej }
1530 1.66 thorpej
1531 1.101 yamt void
1532 1.202 maxv m_copy_pkthdr(struct mbuf *to, struct mbuf *from)
1533 1.202 maxv {
1534 1.202 maxv KASSERT((from->m_flags & M_PKTHDR) != 0);
1535 1.202 maxv
1536 1.202 maxv to->m_pkthdr = from->m_pkthdr;
1537 1.202 maxv to->m_flags = from->m_flags & M_COPYFLAGS;
1538 1.202 maxv SLIST_INIT(&to->m_pkthdr.tags);
1539 1.202 maxv m_tag_copy_chain(to, from);
1540 1.202 maxv to->m_data = to->m_pktdat;
1541 1.202 maxv }
1542 1.202 maxv
1543 1.202 maxv void
1544 1.101 yamt m_move_pkthdr(struct mbuf *to, struct mbuf *from)
1545 1.101 yamt {
1546 1.101 yamt
1547 1.101 yamt KASSERT((to->m_flags & M_EXT) == 0);
1548 1.101 yamt KASSERT((to->m_flags & M_PKTHDR) == 0 || m_tag_first(to) == NULL);
1549 1.101 yamt KASSERT((from->m_flags & M_PKTHDR) != 0);
1550 1.101 yamt
1551 1.101 yamt to->m_pkthdr = from->m_pkthdr;
1552 1.101 yamt to->m_flags = from->m_flags & M_COPYFLAGS;
1553 1.101 yamt to->m_data = to->m_pktdat;
1554 1.101 yamt
1555 1.101 yamt from->m_flags &= ~M_PKTHDR;
1556 1.101 yamt }
1557 1.101 yamt
1558 1.66 thorpej /*
1559 1.66 thorpej * Apply function f to the data in an mbuf chain starting "off" bytes from the
1560 1.66 thorpej * beginning, continuing for "len" bytes.
1561 1.66 thorpej */
1562 1.66 thorpej int
1563 1.66 thorpej m_apply(struct mbuf *m, int off, int len,
1564 1.119 christos int (*f)(void *, void *, unsigned int), void *arg)
1565 1.66 thorpej {
1566 1.66 thorpej unsigned int count;
1567 1.66 thorpej int rval;
1568 1.66 thorpej
1569 1.156 christos KASSERT(len != M_COPYALL);
1570 1.66 thorpej KASSERT(len >= 0);
1571 1.66 thorpej KASSERT(off >= 0);
1572 1.66 thorpej
1573 1.66 thorpej while (off > 0) {
1574 1.66 thorpej KASSERT(m != NULL);
1575 1.66 thorpej if (off < m->m_len)
1576 1.66 thorpej break;
1577 1.66 thorpej off -= m->m_len;
1578 1.66 thorpej m = m->m_next;
1579 1.66 thorpej }
1580 1.66 thorpej while (len > 0) {
1581 1.66 thorpej KASSERT(m != NULL);
1582 1.66 thorpej count = min(m->m_len - off, len);
1583 1.66 thorpej
1584 1.119 christos rval = (*f)(arg, mtod(m, char *) + off, count);
1585 1.66 thorpej if (rval)
1586 1.181 maxv return rval;
1587 1.66 thorpej
1588 1.66 thorpej len -= count;
1589 1.66 thorpej off = 0;
1590 1.66 thorpej m = m->m_next;
1591 1.66 thorpej }
1592 1.66 thorpej
1593 1.181 maxv return 0;
1594 1.66 thorpej }
1595 1.66 thorpej
1596 1.66 thorpej /*
1597 1.66 thorpej * Return a pointer to mbuf/offset of location in mbuf chain.
1598 1.66 thorpej */
1599 1.66 thorpej struct mbuf *
1600 1.66 thorpej m_getptr(struct mbuf *m, int loc, int *off)
1601 1.66 thorpej {
1602 1.66 thorpej
1603 1.66 thorpej while (loc >= 0) {
1604 1.66 thorpej /* Normal end of search */
1605 1.66 thorpej if (m->m_len > loc) {
1606 1.177 maxv *off = loc;
1607 1.181 maxv return m;
1608 1.181 maxv }
1609 1.181 maxv
1610 1.181 maxv loc -= m->m_len;
1611 1.181 maxv
1612 1.181 maxv if (m->m_next == NULL) {
1613 1.181 maxv if (loc == 0) {
1614 1.181 maxv /* Point at the end of valid data */
1615 1.181 maxv *off = m->m_len;
1616 1.181 maxv return m;
1617 1.181 maxv }
1618 1.181 maxv return NULL;
1619 1.66 thorpej } else {
1620 1.181 maxv m = m->m_next;
1621 1.66 thorpej }
1622 1.177 maxv }
1623 1.66 thorpej
1624 1.181 maxv return NULL;
1625 1.1 cgd }
1626 1.105 yamt
1627 1.105 yamt #if defined(DDB)
1628 1.105 yamt void
1629 1.105 yamt m_print(const struct mbuf *m, const char *modif, void (*pr)(const char *, ...))
1630 1.105 yamt {
1631 1.105 yamt char ch;
1632 1.118 thorpej bool opt_c = false;
1633 1.105 yamt char buf[512];
1634 1.105 yamt
1635 1.105 yamt while ((ch = *(modif++)) != '\0') {
1636 1.105 yamt switch (ch) {
1637 1.105 yamt case 'c':
1638 1.118 thorpej opt_c = true;
1639 1.105 yamt break;
1640 1.105 yamt }
1641 1.105 yamt }
1642 1.105 yamt
1643 1.105 yamt nextchain:
1644 1.105 yamt (*pr)("MBUF %p\n", m);
1645 1.130 christos snprintb(buf, sizeof(buf), M_FLAGS_BITS, (u_int)m->m_flags);
1646 1.138 cegger (*pr)(" data=%p, len=%d, type=%d, flags=%s\n",
1647 1.105 yamt m->m_data, m->m_len, m->m_type, buf);
1648 1.105 yamt (*pr)(" owner=%p, next=%p, nextpkt=%p\n", m->m_owner, m->m_next,
1649 1.105 yamt m->m_nextpkt);
1650 1.105 yamt (*pr)(" leadingspace=%u, trailingspace=%u, readonly=%u\n",
1651 1.105 yamt (int)M_LEADINGSPACE(m), (int)M_TRAILINGSPACE(m),
1652 1.105 yamt (int)M_READONLY(m));
1653 1.105 yamt if ((m->m_flags & M_PKTHDR) != 0) {
1654 1.130 christos snprintb(buf, sizeof(buf), M_CSUM_BITS, m->m_pkthdr.csum_flags);
1655 1.172 msaitoh (*pr)(" pktlen=%d, rcvif=%p, csum_flags=%s, csum_data=0x%"
1656 1.105 yamt PRIx32 ", segsz=%u\n",
1657 1.167 ozaki m->m_pkthdr.len, m_get_rcvif_NOMPSAFE(m),
1658 1.105 yamt buf, m->m_pkthdr.csum_data, m->m_pkthdr.segsz);
1659 1.105 yamt }
1660 1.105 yamt if ((m->m_flags & M_EXT)) {
1661 1.125 yamt (*pr)(" ext_refcnt=%u, ext_buf=%p, ext_size=%zd, "
1662 1.105 yamt "ext_free=%p, ext_arg=%p\n",
1663 1.125 yamt m->m_ext.ext_refcnt,
1664 1.105 yamt m->m_ext.ext_buf, m->m_ext.ext_size,
1665 1.105 yamt m->m_ext.ext_free, m->m_ext.ext_arg);
1666 1.105 yamt }
1667 1.105 yamt if ((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0) {
1668 1.108 yamt vaddr_t sva = (vaddr_t)m->m_ext.ext_buf;
1669 1.108 yamt vaddr_t eva = sva + m->m_ext.ext_size;
1670 1.108 yamt int n = (round_page(eva) - trunc_page(sva)) >> PAGE_SHIFT;
1671 1.108 yamt int i;
1672 1.105 yamt
1673 1.105 yamt (*pr)(" pages:");
1674 1.108 yamt for (i = 0; i < n; i ++) {
1675 1.108 yamt (*pr)(" %p", m->m_ext.ext_pgs[i]);
1676 1.105 yamt }
1677 1.105 yamt (*pr)("\n");
1678 1.105 yamt }
1679 1.105 yamt
1680 1.105 yamt if (opt_c) {
1681 1.105 yamt m = m->m_next;
1682 1.105 yamt if (m != NULL) {
1683 1.105 yamt goto nextchain;
1684 1.105 yamt }
1685 1.105 yamt }
1686 1.105 yamt }
1687 1.105 yamt #endif /* defined(DDB) */
1688 1.124 yamt
1689 1.124 yamt void
1690 1.124 yamt mbstat_type_add(int type, int diff)
1691 1.124 yamt {
1692 1.124 yamt struct mbstat_cpu *mb;
1693 1.124 yamt int s;
1694 1.124 yamt
1695 1.124 yamt s = splvm();
1696 1.126 thorpej mb = percpu_getref(mbstat_percpu);
1697 1.124 yamt mb->m_mtypes[type] += diff;
1698 1.126 thorpej percpu_putref(mbstat_percpu);
1699 1.124 yamt splx(s);
1700 1.124 yamt }
1701 1.124 yamt
1702 1.124 yamt #if defined(MBUFTRACE)
1703 1.124 yamt void
1704 1.124 yamt mowner_attach(struct mowner *mo)
1705 1.124 yamt {
1706 1.124 yamt
1707 1.124 yamt KASSERT(mo->mo_counters == NULL);
1708 1.124 yamt mo->mo_counters = percpu_alloc(sizeof(struct mowner_counter));
1709 1.124 yamt
1710 1.124 yamt /* XXX lock */
1711 1.124 yamt LIST_INSERT_HEAD(&mowners, mo, mo_link);
1712 1.124 yamt }
1713 1.124 yamt
1714 1.124 yamt void
1715 1.124 yamt mowner_detach(struct mowner *mo)
1716 1.124 yamt {
1717 1.124 yamt
1718 1.124 yamt KASSERT(mo->mo_counters != NULL);
1719 1.124 yamt
1720 1.124 yamt /* XXX lock */
1721 1.124 yamt LIST_REMOVE(mo, mo_link);
1722 1.124 yamt
1723 1.124 yamt percpu_free(mo->mo_counters, sizeof(struct mowner_counter));
1724 1.124 yamt mo->mo_counters = NULL;
1725 1.124 yamt }
1726 1.124 yamt
1727 1.124 yamt void
1728 1.124 yamt mowner_init(struct mbuf *m, int type)
1729 1.124 yamt {
1730 1.124 yamt struct mowner_counter *mc;
1731 1.124 yamt struct mowner *mo;
1732 1.124 yamt int s;
1733 1.124 yamt
1734 1.124 yamt m->m_owner = mo = &unknown_mowners[type];
1735 1.124 yamt s = splvm();
1736 1.126 thorpej mc = percpu_getref(mo->mo_counters);
1737 1.124 yamt mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
1738 1.126 thorpej percpu_putref(mo->mo_counters);
1739 1.124 yamt splx(s);
1740 1.124 yamt }
1741 1.124 yamt
1742 1.124 yamt void
1743 1.124 yamt mowner_ref(struct mbuf *m, int flags)
1744 1.124 yamt {
1745 1.124 yamt struct mowner *mo = m->m_owner;
1746 1.124 yamt struct mowner_counter *mc;
1747 1.124 yamt int s;
1748 1.124 yamt
1749 1.124 yamt s = splvm();
1750 1.126 thorpej mc = percpu_getref(mo->mo_counters);
1751 1.124 yamt if ((flags & M_EXT) != 0)
1752 1.124 yamt mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
1753 1.124 yamt if ((flags & M_CLUSTER) != 0)
1754 1.124 yamt mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
1755 1.126 thorpej percpu_putref(mo->mo_counters);
1756 1.124 yamt splx(s);
1757 1.124 yamt }
1758 1.124 yamt
1759 1.124 yamt void
1760 1.124 yamt mowner_revoke(struct mbuf *m, bool all, int flags)
1761 1.124 yamt {
1762 1.124 yamt struct mowner *mo = m->m_owner;
1763 1.124 yamt struct mowner_counter *mc;
1764 1.124 yamt int s;
1765 1.124 yamt
1766 1.124 yamt s = splvm();
1767 1.126 thorpej mc = percpu_getref(mo->mo_counters);
1768 1.124 yamt if ((flags & M_EXT) != 0)
1769 1.124 yamt mc->mc_counter[MOWNER_COUNTER_EXT_RELEASES]++;
1770 1.124 yamt if ((flags & M_CLUSTER) != 0)
1771 1.124 yamt mc->mc_counter[MOWNER_COUNTER_CLUSTER_RELEASES]++;
1772 1.124 yamt if (all)
1773 1.124 yamt mc->mc_counter[MOWNER_COUNTER_RELEASES]++;
1774 1.126 thorpej percpu_putref(mo->mo_counters);
1775 1.124 yamt splx(s);
1776 1.124 yamt if (all)
1777 1.124 yamt m->m_owner = &revoked_mowner;
1778 1.124 yamt }
1779 1.124 yamt
1780 1.124 yamt static void
1781 1.124 yamt mowner_claim(struct mbuf *m, struct mowner *mo)
1782 1.124 yamt {
1783 1.124 yamt struct mowner_counter *mc;
1784 1.124 yamt int flags = m->m_flags;
1785 1.124 yamt int s;
1786 1.124 yamt
1787 1.124 yamt s = splvm();
1788 1.126 thorpej mc = percpu_getref(mo->mo_counters);
1789 1.124 yamt mc->mc_counter[MOWNER_COUNTER_CLAIMS]++;
1790 1.124 yamt if ((flags & M_EXT) != 0)
1791 1.124 yamt mc->mc_counter[MOWNER_COUNTER_EXT_CLAIMS]++;
1792 1.124 yamt if ((flags & M_CLUSTER) != 0)
1793 1.124 yamt mc->mc_counter[MOWNER_COUNTER_CLUSTER_CLAIMS]++;
1794 1.126 thorpej percpu_putref(mo->mo_counters);
1795 1.124 yamt splx(s);
1796 1.124 yamt m->m_owner = mo;
1797 1.124 yamt }
1798 1.124 yamt
1799 1.124 yamt void
1800 1.124 yamt m_claim(struct mbuf *m, struct mowner *mo)
1801 1.124 yamt {
1802 1.124 yamt
1803 1.124 yamt if (m->m_owner == mo || mo == NULL)
1804 1.124 yamt return;
1805 1.124 yamt
1806 1.124 yamt mowner_revoke(m, true, m->m_flags);
1807 1.124 yamt mowner_claim(m, mo);
1808 1.124 yamt }
1809 1.124 yamt #endif /* defined(MBUFTRACE) */
1810 1.169 christos
1811 1.188 maxv #ifdef DIAGNOSTIC
1812 1.188 maxv /*
1813 1.188 maxv * Verify that the mbuf chain is not malformed. Used only for diagnostic.
1814 1.188 maxv * Panics on error.
1815 1.188 maxv */
1816 1.188 maxv void
1817 1.188 maxv m_verify_packet(struct mbuf *m)
1818 1.188 maxv {
1819 1.188 maxv struct mbuf *n = m;
1820 1.188 maxv char *low, *high, *dat;
1821 1.188 maxv int totlen = 0, len;
1822 1.188 maxv
1823 1.188 maxv if (__predict_false((m->m_flags & M_PKTHDR) == 0)) {
1824 1.188 maxv panic("%s: mbuf doesn't have M_PKTHDR", __func__);
1825 1.188 maxv }
1826 1.188 maxv
1827 1.188 maxv while (n != NULL) {
1828 1.188 maxv if (__predict_false(n->m_type == MT_FREE)) {
1829 1.188 maxv panic("%s: mbuf already freed (n = %p)", __func__, n);
1830 1.188 maxv }
1831 1.191 maxv #if 0
1832 1.191 maxv /*
1833 1.191 maxv * This ought to be a rule of the mbuf API. Unfortunately,
1834 1.191 maxv * many places don't respect that rule.
1835 1.191 maxv */
1836 1.188 maxv if (__predict_false((n != m) && (n->m_flags & M_PKTHDR) != 0)) {
1837 1.188 maxv panic("%s: M_PKTHDR set on secondary mbuf", __func__);
1838 1.188 maxv }
1839 1.189 maxv #endif
1840 1.188 maxv if (__predict_false(n->m_nextpkt != NULL)) {
1841 1.188 maxv panic("%s: m_nextpkt not null (m_nextpkt = %p)",
1842 1.188 maxv __func__, n->m_nextpkt);
1843 1.188 maxv }
1844 1.188 maxv
1845 1.188 maxv dat = n->m_data;
1846 1.188 maxv len = n->m_len;
1847 1.188 maxv
1848 1.188 maxv if (n->m_flags & M_EXT) {
1849 1.188 maxv low = n->m_ext.ext_buf;
1850 1.188 maxv high = low + n->m_ext.ext_size;
1851 1.188 maxv } else if (n->m_flags & M_PKTHDR) {
1852 1.188 maxv low = n->m_pktdat;
1853 1.188 maxv high = low + MHLEN;
1854 1.188 maxv } else {
1855 1.188 maxv low = n->m_dat;
1856 1.188 maxv high = low + MLEN;
1857 1.188 maxv }
1858 1.192 maxv if (__predict_false(dat + len < dat)) {
1859 1.188 maxv panic("%s: incorrect length (len = %d)", __func__, len);
1860 1.188 maxv }
1861 1.188 maxv if (__predict_false((dat < low) || (dat + len > high))) {
1862 1.188 maxv panic("%s: m_data not in packet"
1863 1.188 maxv "(dat = %p, len = %d, low = %p, high = %p)",
1864 1.188 maxv __func__, dat, len, low, high);
1865 1.188 maxv }
1866 1.188 maxv
1867 1.188 maxv totlen += len;
1868 1.188 maxv n = n->m_next;
1869 1.188 maxv }
1870 1.188 maxv
1871 1.188 maxv if (__predict_false(totlen != m->m_pkthdr.len)) {
1872 1.188 maxv panic("%s: inconsistent mbuf length (%d != %d)", __func__,
1873 1.188 maxv totlen, m->m_pkthdr.len);
1874 1.188 maxv }
1875 1.188 maxv }
1876 1.188 maxv #endif
1877 1.188 maxv
1878 1.169 christos /*
1879 1.186 maxv * Release a reference to the mbuf external storage.
1880 1.186 maxv *
1881 1.186 maxv * => free the mbuf m itself as well.
1882 1.186 maxv */
1883 1.186 maxv static void
1884 1.186 maxv m_ext_free(struct mbuf *m)
1885 1.186 maxv {
1886 1.186 maxv const bool embedded = MEXT_ISEMBEDDED(m);
1887 1.186 maxv bool dofree = true;
1888 1.186 maxv u_int refcnt;
1889 1.186 maxv
1890 1.186 maxv KASSERT((m->m_flags & M_EXT) != 0);
1891 1.186 maxv KASSERT(MEXT_ISEMBEDDED(m->m_ext_ref));
1892 1.186 maxv KASSERT((m->m_ext_ref->m_flags & M_EXT) != 0);
1893 1.186 maxv KASSERT((m->m_flags & M_EXT_CLUSTER) ==
1894 1.186 maxv (m->m_ext_ref->m_flags & M_EXT_CLUSTER));
1895 1.186 maxv
1896 1.186 maxv if (__predict_false(m->m_type == MT_FREE)) {
1897 1.186 maxv panic("mbuf %p already freed", m);
1898 1.186 maxv }
1899 1.186 maxv
1900 1.186 maxv if (__predict_true(m->m_ext.ext_refcnt == 1)) {
1901 1.186 maxv refcnt = m->m_ext.ext_refcnt = 0;
1902 1.186 maxv } else {
1903 1.186 maxv refcnt = atomic_dec_uint_nv(&m->m_ext.ext_refcnt);
1904 1.186 maxv }
1905 1.186 maxv
1906 1.186 maxv if (refcnt > 0) {
1907 1.186 maxv if (embedded) {
1908 1.186 maxv /*
1909 1.186 maxv * other mbuf's m_ext_ref still points to us.
1910 1.186 maxv */
1911 1.186 maxv dofree = false;
1912 1.186 maxv } else {
1913 1.186 maxv m->m_ext_ref = m;
1914 1.186 maxv }
1915 1.186 maxv } else {
1916 1.186 maxv /*
1917 1.186 maxv * dropping the last reference
1918 1.186 maxv */
1919 1.186 maxv if (!embedded) {
1920 1.186 maxv m->m_ext.ext_refcnt++; /* XXX */
1921 1.186 maxv m_ext_free(m->m_ext_ref);
1922 1.186 maxv m->m_ext_ref = m;
1923 1.186 maxv } else if ((m->m_flags & M_EXT_CLUSTER) != 0) {
1924 1.186 maxv pool_cache_put_paddr((struct pool_cache *)
1925 1.186 maxv m->m_ext.ext_arg,
1926 1.186 maxv m->m_ext.ext_buf, m->m_ext.ext_paddr);
1927 1.186 maxv } else if (m->m_ext.ext_free) {
1928 1.186 maxv (*m->m_ext.ext_free)(m,
1929 1.186 maxv m->m_ext.ext_buf, m->m_ext.ext_size,
1930 1.186 maxv m->m_ext.ext_arg);
1931 1.186 maxv /*
1932 1.186 maxv * 'm' is already freed by the ext_free callback.
1933 1.186 maxv */
1934 1.186 maxv dofree = false;
1935 1.186 maxv } else {
1936 1.186 maxv free(m->m_ext.ext_buf, m->m_ext.ext_type);
1937 1.186 maxv }
1938 1.186 maxv }
1939 1.186 maxv
1940 1.186 maxv if (dofree) {
1941 1.186 maxv m->m_type = MT_FREE;
1942 1.186 maxv m->m_data = NULL;
1943 1.186 maxv pool_cache_put(mb_cache, m);
1944 1.186 maxv }
1945 1.186 maxv }
1946 1.186 maxv
1947 1.186 maxv /*
1948 1.175 maxv * Free a single mbuf and associated external storage. Return the
1949 1.175 maxv * successor, if any.
1950 1.175 maxv */
1951 1.169 christos struct mbuf *
1952 1.175 maxv m_free(struct mbuf *m)
1953 1.169 christos {
1954 1.169 christos struct mbuf *n;
1955 1.169 christos
1956 1.175 maxv mowner_revoke(m, 1, m->m_flags);
1957 1.175 maxv mbstat_type_add(m->m_type, -1);
1958 1.175 maxv
1959 1.175 maxv if (m->m_flags & M_PKTHDR)
1960 1.175 maxv m_tag_delete_chain(m, NULL);
1961 1.175 maxv
1962 1.175 maxv n = m->m_next;
1963 1.175 maxv
1964 1.175 maxv if (m->m_flags & M_EXT) {
1965 1.175 maxv m_ext_free(m);
1966 1.175 maxv } else {
1967 1.175 maxv if (__predict_false(m->m_type == MT_FREE)) {
1968 1.175 maxv panic("mbuf %p already freed", m);
1969 1.175 maxv }
1970 1.175 maxv m->m_type = MT_FREE;
1971 1.175 maxv m->m_data = NULL;
1972 1.175 maxv pool_cache_put(mb_cache, m);
1973 1.175 maxv }
1974 1.175 maxv
1975 1.175 maxv return n;
1976 1.169 christos }
1977 1.169 christos
1978 1.169 christos void
1979 1.175 maxv m_freem(struct mbuf *m)
1980 1.169 christos {
1981 1.169 christos if (m == NULL)
1982 1.169 christos return;
1983 1.169 christos do {
1984 1.175 maxv m = m_free(m);
1985 1.169 christos } while (m);
1986 1.169 christos }
1987