uipc_mbuf.c revision 1.68 1 1.68 simonb /* $NetBSD: uipc_mbuf.c,v 1.68 2003/05/27 09:03:46 simonb Exp $ */
2 1.42 thorpej
3 1.42 thorpej /*-
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 * 3. All advertising materials mentioning features or use of this software
20 1.42 thorpej * must display the following acknowledgement:
21 1.42 thorpej * This product includes software developed by the NetBSD
22 1.42 thorpej * Foundation, Inc. and its contributors.
23 1.42 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.42 thorpej * contributors may be used to endorse or promote products derived
25 1.42 thorpej * from this software without specific prior written permission.
26 1.42 thorpej *
27 1.42 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.42 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.42 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.42 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.42 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.42 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.42 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.42 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.42 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.42 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.42 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.42 thorpej */
39 1.10 cgd
40 1.1 cgd /*
41 1.9 mycroft * Copyright (c) 1982, 1986, 1988, 1991, 1993
42 1.9 mycroft * The Regents of the University of California. All rights reserved.
43 1.1 cgd *
44 1.1 cgd * Redistribution and use in source and binary forms, with or without
45 1.1 cgd * modification, are permitted provided that the following conditions
46 1.1 cgd * are met:
47 1.1 cgd * 1. Redistributions of source code must retain the above copyright
48 1.1 cgd * notice, this list of conditions and the following disclaimer.
49 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 cgd * notice, this list of conditions and the following disclaimer in the
51 1.1 cgd * documentation and/or other materials provided with the distribution.
52 1.1 cgd * 3. All advertising materials mentioning features or use of this software
53 1.1 cgd * must display the following acknowledgement:
54 1.1 cgd * This product includes software developed by the University of
55 1.1 cgd * California, Berkeley and its contributors.
56 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
57 1.1 cgd * may be used to endorse or promote products derived from this software
58 1.1 cgd * without specific prior written permission.
59 1.1 cgd *
60 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.1 cgd * SUCH DAMAGE.
71 1.1 cgd *
72 1.26 fvdl * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
73 1.1 cgd */
74 1.56 lukem
75 1.56 lukem #include <sys/cdefs.h>
76 1.68 simonb __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.68 2003/05/27 09:03:46 simonb Exp $");
77 1.24 mrg
78 1.6 mycroft #include <sys/param.h>
79 1.6 mycroft #include <sys/systm.h>
80 1.6 mycroft #include <sys/proc.h>
81 1.6 mycroft #include <sys/malloc.h>
82 1.1 cgd #define MBTYPES
83 1.6 mycroft #include <sys/mbuf.h>
84 1.6 mycroft #include <sys/kernel.h>
85 1.6 mycroft #include <sys/syslog.h>
86 1.6 mycroft #include <sys/domain.h>
87 1.6 mycroft #include <sys/protosw.h>
88 1.28 thorpej #include <sys/pool.h>
89 1.27 matt #include <sys/socket.h>
90 1.55 simonb #include <sys/sysctl.h>
91 1.55 simonb
92 1.27 matt #include <net/if.h>
93 1.14 christos
94 1.65 thorpej #include <uvm/uvm.h>
95 1.23 mrg
96 1.42 thorpej
97 1.28 thorpej struct pool mbpool; /* mbuf pool */
98 1.28 thorpej struct pool mclpool; /* mbuf cluster pool */
99 1.28 thorpej
100 1.53 thorpej struct pool_cache mbpool_cache;
101 1.53 thorpej struct pool_cache mclpool_cache;
102 1.53 thorpej
103 1.18 thorpej struct mbstat mbstat;
104 1.18 thorpej int max_linkhdr;
105 1.18 thorpej int max_protohdr;
106 1.18 thorpej int max_hdr;
107 1.18 thorpej int max_datalen;
108 1.18 thorpej
109 1.65 thorpej static int mb_ctor(void *, void *, int);
110 1.65 thorpej
111 1.62 thorpej void *mclpool_alloc(struct pool *, int);
112 1.62 thorpej void mclpool_release(struct pool *, void *);
113 1.58 thorpej
114 1.58 thorpej struct pool_allocator mclpool_allocator = {
115 1.58 thorpej mclpool_alloc, mclpool_release, 0,
116 1.58 thorpej };
117 1.58 thorpej
118 1.44 itojun static struct mbuf *m_copym0 __P((struct mbuf *, int, int, int, int));
119 1.28 thorpej
120 1.57 thorpej const char mclpool_warnmsg[] =
121 1.42 thorpej "WARNING: mclpool limit reached; increase NMBCLUSTERS";
122 1.63 thorpej
123 1.63 thorpej MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
124 1.42 thorpej
125 1.64 matt #ifdef MBUFTRACE
126 1.64 matt struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
127 1.64 matt struct mowner unknown_mowners[] = {
128 1.64 matt { "unknown", "free" },
129 1.64 matt { "unknown", "data" },
130 1.64 matt { "unknown", "header" },
131 1.64 matt { "unknown", "soname" },
132 1.64 matt { "unknown", "soopts" },
133 1.64 matt { "unknown", "ftable" },
134 1.64 matt { "unknown", "control" },
135 1.64 matt { "unknown", "oobdata" },
136 1.64 matt };
137 1.64 matt struct mowner revoked_mowner = { "revoked", "" };
138 1.64 matt #endif
139 1.64 matt
140 1.28 thorpej /*
141 1.68 simonb * Initialize the mbuf allocator.
142 1.28 thorpej */
143 1.4 jtc void
144 1.62 thorpej mbinit(void)
145 1.1 cgd {
146 1.65 thorpej
147 1.65 thorpej KASSERT(sizeof(struct _m_ext) <= MHLEN);
148 1.67 simonb KASSERT(sizeof(struct mbuf) == MSIZE);
149 1.65 thorpej
150 1.58 thorpej pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL);
151 1.58 thorpej pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator);
152 1.53 thorpej
153 1.59 thorpej pool_set_drain_hook(&mbpool, m_reclaim, NULL);
154 1.59 thorpej pool_set_drain_hook(&mclpool, m_reclaim, NULL);
155 1.59 thorpej
156 1.65 thorpej pool_cache_init(&mbpool_cache, &mbpool, mb_ctor, NULL, NULL);
157 1.53 thorpej pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
158 1.37 thorpej
159 1.37 thorpej /*
160 1.39 thorpej * Set the hard limit on the mclpool to the number of
161 1.39 thorpej * mbuf clusters the kernel is to support. Log the limit
162 1.39 thorpej * reached message max once a minute.
163 1.39 thorpej */
164 1.42 thorpej pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
165 1.42 thorpej
166 1.39 thorpej /*
167 1.42 thorpej * Set a low water mark for both mbufs and clusters. This should
168 1.42 thorpej * help ensure that they can be allocated in a memory starvation
169 1.42 thorpej * situation. This is important for e.g. diskless systems which
170 1.42 thorpej * must allocate mbufs in order for the pagedaemon to clean pages.
171 1.37 thorpej */
172 1.42 thorpej pool_setlowat(&mbpool, mblowat);
173 1.42 thorpej pool_setlowat(&mclpool, mcllowat);
174 1.64 matt
175 1.64 matt #ifdef MBUFTRACE
176 1.64 matt {
177 1.64 matt /*
178 1.64 matt * Attach the unknown mowners.
179 1.64 matt */
180 1.64 matt int i;
181 1.64 matt MOWNER_ATTACH(&revoked_mowner);
182 1.64 matt for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
183 1.64 matt i-- > 0; )
184 1.64 matt MOWNER_ATTACH(&unknown_mowners[i]);
185 1.64 matt }
186 1.64 matt #endif
187 1.42 thorpej }
188 1.42 thorpej
189 1.42 thorpej int
190 1.62 thorpej sysctl_dombuf(int *name, u_int namelen, void *oldp, size_t *oldlenp,
191 1.62 thorpej void *newp, size_t newlen)
192 1.42 thorpej {
193 1.42 thorpej int error, newval;
194 1.42 thorpej
195 1.42 thorpej /* All sysctl names at this level are terminal. */
196 1.42 thorpej if (namelen != 1)
197 1.42 thorpej return (ENOTDIR); /* overloaded */
198 1.42 thorpej
199 1.42 thorpej switch (name[0]) {
200 1.42 thorpej case MBUF_MSIZE:
201 1.42 thorpej return (sysctl_rdint(oldp, oldlenp, newp, msize));
202 1.42 thorpej case MBUF_MCLBYTES:
203 1.42 thorpej return (sysctl_rdint(oldp, oldlenp, newp, mclbytes));
204 1.42 thorpej case MBUF_NMBCLUSTERS:
205 1.42 thorpej /*
206 1.42 thorpej * If we have direct-mapped pool pages, we can adjust this
207 1.42 thorpej * number on the fly. If not, we're limited by the size
208 1.42 thorpej * of mb_map, and cannot change this value.
209 1.42 thorpej *
210 1.42 thorpej * Note: we only allow the value to be increased, never
211 1.42 thorpej * decreased.
212 1.42 thorpej */
213 1.42 thorpej if (mb_map == NULL) {
214 1.42 thorpej newval = nmbclusters;
215 1.42 thorpej error = sysctl_int(oldp, oldlenp, newp, newlen,
216 1.42 thorpej &newval);
217 1.42 thorpej if (error != 0)
218 1.42 thorpej return (error);
219 1.42 thorpej if (newp != NULL) {
220 1.42 thorpej if (newval >= nmbclusters) {
221 1.42 thorpej nmbclusters = newval;
222 1.42 thorpej pool_sethardlimit(&mclpool,
223 1.42 thorpej nmbclusters, mclpool_warnmsg, 60);
224 1.42 thorpej } else
225 1.42 thorpej error = EINVAL;
226 1.42 thorpej }
227 1.42 thorpej return (error);
228 1.42 thorpej } else
229 1.42 thorpej return (sysctl_rdint(oldp, oldlenp, newp, nmbclusters));
230 1.42 thorpej case MBUF_MBLOWAT:
231 1.42 thorpej case MBUF_MCLLOWAT:
232 1.42 thorpej /* New value must be >= 0. */
233 1.42 thorpej newval = (name[0] == MBUF_MBLOWAT) ? mblowat : mcllowat;
234 1.42 thorpej error = sysctl_int(oldp, oldlenp, newp, newlen, &newval);
235 1.42 thorpej if (error != 0)
236 1.42 thorpej return (error);
237 1.42 thorpej if (newp != NULL) {
238 1.42 thorpej if (newval >= 0) {
239 1.42 thorpej if (name[0] == MBUF_MBLOWAT) {
240 1.42 thorpej mblowat = newval;
241 1.42 thorpej pool_setlowat(&mbpool, newval);
242 1.42 thorpej } else {
243 1.42 thorpej mcllowat = newval;
244 1.42 thorpej pool_setlowat(&mclpool, newval);
245 1.42 thorpej }
246 1.42 thorpej } else
247 1.42 thorpej error = EINVAL;
248 1.42 thorpej }
249 1.42 thorpej return (error);
250 1.64 matt case MBUF_STATS:
251 1.64 matt return (sysctl_rdstruct(oldp, oldlenp, newp,
252 1.64 matt &mbstat, sizeof(mbstat)));
253 1.64 matt #ifdef MBUFTRACE
254 1.64 matt case MBUF_MOWNERS: {
255 1.64 matt struct mowner *mo;
256 1.64 matt size_t len = 0;
257 1.64 matt if (newp != NULL)
258 1.64 matt return (EPERM);
259 1.64 matt error = 0;
260 1.64 matt LIST_FOREACH(mo, &mowners, mo_link) {
261 1.64 matt if (oldp != NULL) {
262 1.64 matt if (*oldlenp - len < sizeof(*mo)) {
263 1.64 matt error = ENOMEM;
264 1.64 matt break;
265 1.64 matt }
266 1.64 matt error = copyout(mo, (caddr_t) oldp + len,
267 1.64 matt sizeof(*mo));
268 1.64 matt if (error)
269 1.64 matt break;
270 1.64 matt }
271 1.64 matt len += sizeof(*mo);
272 1.64 matt }
273 1.64 matt *oldlenp = len;
274 1.64 matt return (error);
275 1.64 matt }
276 1.64 matt #endif
277 1.42 thorpej default:
278 1.42 thorpej return (EOPNOTSUPP);
279 1.42 thorpej }
280 1.42 thorpej /* NOTREACHED */
281 1.28 thorpej }
282 1.28 thorpej
283 1.28 thorpej void *
284 1.62 thorpej mclpool_alloc(struct pool *pp, int flags)
285 1.28 thorpej {
286 1.32 thorpej boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
287 1.28 thorpej
288 1.54 chs return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
289 1.1 cgd }
290 1.1 cgd
291 1.28 thorpej void
292 1.62 thorpej mclpool_release(struct pool *pp, void *v)
293 1.1 cgd {
294 1.1 cgd
295 1.31 thorpej uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
296 1.65 thorpej }
297 1.65 thorpej
298 1.65 thorpej /*ARGSUSED*/
299 1.65 thorpej static int
300 1.65 thorpej mb_ctor(void *arg, void *object, int flags)
301 1.65 thorpej {
302 1.65 thorpej struct mbuf *m = object;
303 1.65 thorpej
304 1.65 thorpej #ifdef POOL_VTOPHYS
305 1.65 thorpej m->m_paddr = POOL_VTOPHYS(m);
306 1.65 thorpej #else
307 1.65 thorpej m->m_paddr = M_PADDR_INVALID;
308 1.65 thorpej #endif
309 1.65 thorpej return (0);
310 1.1 cgd }
311 1.1 cgd
312 1.14 christos void
313 1.59 thorpej m_reclaim(void *arg, int flags)
314 1.1 cgd {
315 1.27 matt struct domain *dp;
316 1.27 matt struct protosw *pr;
317 1.27 matt struct ifnet *ifp;
318 1.52 thorpej int s = splvm();
319 1.1 cgd
320 1.33 thorpej for (dp = domains; dp; dp = dp->dom_next)
321 1.33 thorpej for (pr = dp->dom_protosw;
322 1.33 thorpej pr < dp->dom_protoswNPROTOSW; pr++)
323 1.33 thorpej if (pr->pr_drain)
324 1.33 thorpej (*pr->pr_drain)();
325 1.27 matt for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
326 1.27 matt if (ifp->if_drain)
327 1.27 matt (*ifp->if_drain)(ifp);
328 1.1 cgd splx(s);
329 1.1 cgd mbstat.m_drain++;
330 1.1 cgd }
331 1.1 cgd
332 1.1 cgd /*
333 1.1 cgd * Space allocation routines.
334 1.1 cgd * These are also available as macros
335 1.1 cgd * for critical paths.
336 1.1 cgd */
337 1.1 cgd struct mbuf *
338 1.62 thorpej m_get(int nowait, int type)
339 1.1 cgd {
340 1.27 matt struct mbuf *m;
341 1.1 cgd
342 1.5 cgd MGET(m, nowait, type);
343 1.1 cgd return (m);
344 1.1 cgd }
345 1.1 cgd
346 1.1 cgd struct mbuf *
347 1.62 thorpej m_gethdr(int nowait, int type)
348 1.1 cgd {
349 1.27 matt struct mbuf *m;
350 1.1 cgd
351 1.5 cgd MGETHDR(m, nowait, type);
352 1.1 cgd return (m);
353 1.1 cgd }
354 1.1 cgd
355 1.1 cgd struct mbuf *
356 1.62 thorpej m_getclr(int nowait, int type)
357 1.1 cgd {
358 1.27 matt struct mbuf *m;
359 1.1 cgd
360 1.5 cgd MGET(m, nowait, type);
361 1.1 cgd if (m == 0)
362 1.1 cgd return (0);
363 1.30 perry memset(mtod(m, caddr_t), 0, MLEN);
364 1.1 cgd return (m);
365 1.1 cgd }
366 1.1 cgd
367 1.64 matt void
368 1.64 matt m_clget(struct mbuf *m, int nowait)
369 1.64 matt {
370 1.64 matt MCLGET(m, nowait);
371 1.64 matt }
372 1.64 matt
373 1.1 cgd struct mbuf *
374 1.62 thorpej m_free(struct mbuf *m)
375 1.1 cgd {
376 1.27 matt struct mbuf *n;
377 1.1 cgd
378 1.1 cgd MFREE(m, n);
379 1.1 cgd return (n);
380 1.1 cgd }
381 1.1 cgd
382 1.9 mycroft void
383 1.62 thorpej m_freem(struct mbuf *m)
384 1.1 cgd {
385 1.27 matt struct mbuf *n;
386 1.1 cgd
387 1.1 cgd if (m == NULL)
388 1.1 cgd return;
389 1.1 cgd do {
390 1.1 cgd MFREE(m, n);
391 1.18 thorpej m = n;
392 1.18 thorpej } while (m);
393 1.1 cgd }
394 1.1 cgd
395 1.64 matt #ifdef MBUFTRACE
396 1.64 matt void
397 1.64 matt m_claim(struct mbuf *m, struct mowner *mo)
398 1.64 matt {
399 1.64 matt for (; m != NULL; m = m->m_next)
400 1.64 matt MCLAIM(m, mo);
401 1.64 matt }
402 1.64 matt #endif
403 1.64 matt
404 1.1 cgd /*
405 1.1 cgd * Mbuffer utility routines.
406 1.1 cgd */
407 1.1 cgd
408 1.1 cgd /*
409 1.1 cgd * Lesser-used path for M_PREPEND:
410 1.1 cgd * allocate new mbuf to prepend to chain,
411 1.1 cgd * copy junk along.
412 1.1 cgd */
413 1.1 cgd struct mbuf *
414 1.62 thorpej m_prepend(struct mbuf *m, int len, int how)
415 1.1 cgd {
416 1.1 cgd struct mbuf *mn;
417 1.1 cgd
418 1.9 mycroft MGET(mn, how, m->m_type);
419 1.1 cgd if (mn == (struct mbuf *)NULL) {
420 1.1 cgd m_freem(m);
421 1.1 cgd return ((struct mbuf *)NULL);
422 1.1 cgd }
423 1.1 cgd if (m->m_flags & M_PKTHDR) {
424 1.1 cgd M_COPY_PKTHDR(mn, m);
425 1.1 cgd m->m_flags &= ~M_PKTHDR;
426 1.64 matt } else {
427 1.64 matt MCLAIM(mn, m->m_owner);
428 1.1 cgd }
429 1.1 cgd mn->m_next = m;
430 1.1 cgd m = mn;
431 1.1 cgd if (len < MHLEN)
432 1.1 cgd MH_ALIGN(m, len);
433 1.1 cgd m->m_len = len;
434 1.1 cgd return (m);
435 1.1 cgd }
436 1.1 cgd
437 1.1 cgd /*
438 1.1 cgd * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
439 1.1 cgd * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
440 1.1 cgd * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
441 1.1 cgd */
442 1.1 cgd int MCFail;
443 1.1 cgd
444 1.1 cgd struct mbuf *
445 1.62 thorpej m_copym(struct mbuf *m, int off0, int len, int wait)
446 1.1 cgd {
447 1.44 itojun return m_copym0(m, off0, len, wait, 0); /* shallow copy on M_EXT */
448 1.44 itojun }
449 1.44 itojun
450 1.44 itojun struct mbuf *
451 1.62 thorpej m_dup(struct mbuf *m, int off0, int len, int wait)
452 1.44 itojun {
453 1.44 itojun return m_copym0(m, off0, len, wait, 1); /* deep copy */
454 1.44 itojun }
455 1.44 itojun
456 1.44 itojun static struct mbuf *
457 1.62 thorpej m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
458 1.44 itojun {
459 1.27 matt struct mbuf *n, **np;
460 1.27 matt int off = off0;
461 1.1 cgd struct mbuf *top;
462 1.1 cgd int copyhdr = 0;
463 1.1 cgd
464 1.1 cgd if (off < 0 || len < 0)
465 1.43 thorpej panic("m_copym: off %d, len %d", off, len);
466 1.1 cgd if (off == 0 && m->m_flags & M_PKTHDR)
467 1.1 cgd copyhdr = 1;
468 1.1 cgd while (off > 0) {
469 1.1 cgd if (m == 0)
470 1.43 thorpej panic("m_copym: m == 0");
471 1.1 cgd if (off < m->m_len)
472 1.1 cgd break;
473 1.1 cgd off -= m->m_len;
474 1.1 cgd m = m->m_next;
475 1.1 cgd }
476 1.1 cgd np = ⊤
477 1.1 cgd top = 0;
478 1.1 cgd while (len > 0) {
479 1.1 cgd if (m == 0) {
480 1.1 cgd if (len != M_COPYALL)
481 1.43 thorpej panic("m_copym: m == 0 and not COPYALL");
482 1.1 cgd break;
483 1.1 cgd }
484 1.1 cgd MGET(n, wait, m->m_type);
485 1.1 cgd *np = n;
486 1.1 cgd if (n == 0)
487 1.1 cgd goto nospace;
488 1.64 matt MCLAIM(n, m->m_owner);
489 1.1 cgd if (copyhdr) {
490 1.1 cgd M_COPY_PKTHDR(n, m);
491 1.1 cgd if (len == M_COPYALL)
492 1.1 cgd n->m_pkthdr.len -= off0;
493 1.1 cgd else
494 1.1 cgd n->m_pkthdr.len = len;
495 1.1 cgd copyhdr = 0;
496 1.1 cgd }
497 1.9 mycroft n->m_len = min(len, m->m_len - off);
498 1.1 cgd if (m->m_flags & M_EXT) {
499 1.44 itojun if (!deep) {
500 1.44 itojun n->m_data = m->m_data + off;
501 1.44 itojun n->m_ext = m->m_ext;
502 1.44 itojun MCLADDREFERENCE(m, n);
503 1.44 itojun } else {
504 1.48 itojun /*
505 1.50 itojun * we are unsure about the way m was allocated.
506 1.50 itojun * copy into multiple MCLBYTES cluster mbufs.
507 1.48 itojun */
508 1.44 itojun MCLGET(n, wait);
509 1.50 itojun n->m_len = 0;
510 1.50 itojun n->m_len = M_TRAILINGSPACE(n);
511 1.50 itojun n->m_len = min(n->m_len, len);
512 1.50 itojun n->m_len = min(n->m_len, m->m_len - off);
513 1.50 itojun memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
514 1.44 itojun (unsigned)n->m_len);
515 1.44 itojun }
516 1.1 cgd } else
517 1.30 perry memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
518 1.1 cgd (unsigned)n->m_len);
519 1.1 cgd if (len != M_COPYALL)
520 1.1 cgd len -= n->m_len;
521 1.50 itojun off += n->m_len;
522 1.50 itojun #ifdef DIAGNOSTIC
523 1.50 itojun if (off > m->m_len)
524 1.50 itojun panic("m_copym0 overrun");
525 1.50 itojun #endif
526 1.50 itojun if (off == m->m_len) {
527 1.50 itojun m = m->m_next;
528 1.50 itojun off = 0;
529 1.50 itojun }
530 1.1 cgd np = &n->m_next;
531 1.1 cgd }
532 1.1 cgd if (top == 0)
533 1.1 cgd MCFail++;
534 1.1 cgd return (top);
535 1.1 cgd nospace:
536 1.1 cgd m_freem(top);
537 1.1 cgd MCFail++;
538 1.1 cgd return (0);
539 1.1 cgd }
540 1.1 cgd
541 1.1 cgd /*
542 1.18 thorpej * Copy an entire packet, including header (which must be present).
543 1.18 thorpej * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
544 1.18 thorpej */
545 1.18 thorpej struct mbuf *
546 1.62 thorpej m_copypacket(struct mbuf *m, int how)
547 1.18 thorpej {
548 1.18 thorpej struct mbuf *top, *n, *o;
549 1.18 thorpej
550 1.18 thorpej MGET(n, how, m->m_type);
551 1.18 thorpej top = n;
552 1.18 thorpej if (!n)
553 1.18 thorpej goto nospace;
554 1.18 thorpej
555 1.64 matt MCLAIM(n, m->m_owner);
556 1.18 thorpej M_COPY_PKTHDR(n, m);
557 1.18 thorpej n->m_len = m->m_len;
558 1.18 thorpej if (m->m_flags & M_EXT) {
559 1.18 thorpej n->m_data = m->m_data;
560 1.18 thorpej n->m_ext = m->m_ext;
561 1.18 thorpej MCLADDREFERENCE(m, n);
562 1.18 thorpej } else {
563 1.30 perry memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
564 1.18 thorpej }
565 1.18 thorpej
566 1.18 thorpej m = m->m_next;
567 1.18 thorpej while (m) {
568 1.18 thorpej MGET(o, how, m->m_type);
569 1.18 thorpej if (!o)
570 1.18 thorpej goto nospace;
571 1.18 thorpej
572 1.64 matt MCLAIM(o, m->m_owner);
573 1.18 thorpej n->m_next = o;
574 1.18 thorpej n = n->m_next;
575 1.18 thorpej
576 1.18 thorpej n->m_len = m->m_len;
577 1.18 thorpej if (m->m_flags & M_EXT) {
578 1.18 thorpej n->m_data = m->m_data;
579 1.18 thorpej n->m_ext = m->m_ext;
580 1.18 thorpej MCLADDREFERENCE(m, n);
581 1.18 thorpej } else {
582 1.30 perry memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
583 1.18 thorpej }
584 1.18 thorpej
585 1.18 thorpej m = m->m_next;
586 1.18 thorpej }
587 1.18 thorpej return top;
588 1.18 thorpej nospace:
589 1.18 thorpej m_freem(top);
590 1.18 thorpej MCFail++;
591 1.18 thorpej return 0;
592 1.18 thorpej }
593 1.18 thorpej
594 1.18 thorpej /*
595 1.1 cgd * Copy data from an mbuf chain starting "off" bytes from the beginning,
596 1.1 cgd * continuing for "len" bytes, into the indicated buffer.
597 1.1 cgd */
598 1.14 christos void
599 1.62 thorpej m_copydata(struct mbuf *m, int off, int len, caddr_t cp)
600 1.1 cgd {
601 1.27 matt unsigned count;
602 1.1 cgd
603 1.1 cgd if (off < 0 || len < 0)
604 1.1 cgd panic("m_copydata");
605 1.1 cgd while (off > 0) {
606 1.1 cgd if (m == 0)
607 1.1 cgd panic("m_copydata");
608 1.1 cgd if (off < m->m_len)
609 1.1 cgd break;
610 1.1 cgd off -= m->m_len;
611 1.1 cgd m = m->m_next;
612 1.1 cgd }
613 1.1 cgd while (len > 0) {
614 1.1 cgd if (m == 0)
615 1.1 cgd panic("m_copydata");
616 1.9 mycroft count = min(m->m_len - off, len);
617 1.30 perry memcpy(cp, mtod(m, caddr_t) + off, count);
618 1.1 cgd len -= count;
619 1.1 cgd cp += count;
620 1.1 cgd off = 0;
621 1.1 cgd m = m->m_next;
622 1.1 cgd }
623 1.1 cgd }
624 1.1 cgd
625 1.1 cgd /*
626 1.1 cgd * Concatenate mbuf chain n to m.
627 1.1 cgd * Both chains must be of the same type (e.g. MT_DATA).
628 1.1 cgd * Any m_pkthdr is not updated.
629 1.1 cgd */
630 1.14 christos void
631 1.62 thorpej m_cat(struct mbuf *m, struct mbuf *n)
632 1.1 cgd {
633 1.1 cgd while (m->m_next)
634 1.1 cgd m = m->m_next;
635 1.1 cgd while (n) {
636 1.1 cgd if (m->m_flags & M_EXT ||
637 1.1 cgd m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
638 1.1 cgd /* just join the two chains */
639 1.1 cgd m->m_next = n;
640 1.1 cgd return;
641 1.1 cgd }
642 1.1 cgd /* splat the data from one into the other */
643 1.30 perry memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
644 1.1 cgd (u_int)n->m_len);
645 1.1 cgd m->m_len += n->m_len;
646 1.1 cgd n = m_free(n);
647 1.1 cgd }
648 1.1 cgd }
649 1.1 cgd
650 1.11 mycroft void
651 1.62 thorpej m_adj(struct mbuf *mp, int req_len)
652 1.1 cgd {
653 1.27 matt int len = req_len;
654 1.27 matt struct mbuf *m;
655 1.27 matt int count;
656 1.1 cgd
657 1.1 cgd if ((m = mp) == NULL)
658 1.1 cgd return;
659 1.1 cgd if (len >= 0) {
660 1.1 cgd /*
661 1.1 cgd * Trim from head.
662 1.1 cgd */
663 1.1 cgd while (m != NULL && len > 0) {
664 1.1 cgd if (m->m_len <= len) {
665 1.1 cgd len -= m->m_len;
666 1.1 cgd m->m_len = 0;
667 1.1 cgd m = m->m_next;
668 1.1 cgd } else {
669 1.1 cgd m->m_len -= len;
670 1.1 cgd m->m_data += len;
671 1.1 cgd len = 0;
672 1.1 cgd }
673 1.1 cgd }
674 1.1 cgd m = mp;
675 1.1 cgd if (mp->m_flags & M_PKTHDR)
676 1.1 cgd m->m_pkthdr.len -= (req_len - len);
677 1.1 cgd } else {
678 1.1 cgd /*
679 1.1 cgd * Trim from tail. Scan the mbuf chain,
680 1.1 cgd * calculating its length and finding the last mbuf.
681 1.1 cgd * If the adjustment only affects this mbuf, then just
682 1.1 cgd * adjust and return. Otherwise, rescan and truncate
683 1.1 cgd * after the remaining size.
684 1.1 cgd */
685 1.1 cgd len = -len;
686 1.1 cgd count = 0;
687 1.1 cgd for (;;) {
688 1.1 cgd count += m->m_len;
689 1.1 cgd if (m->m_next == (struct mbuf *)0)
690 1.1 cgd break;
691 1.1 cgd m = m->m_next;
692 1.1 cgd }
693 1.1 cgd if (m->m_len >= len) {
694 1.1 cgd m->m_len -= len;
695 1.8 deraadt if (mp->m_flags & M_PKTHDR)
696 1.8 deraadt mp->m_pkthdr.len -= len;
697 1.1 cgd return;
698 1.1 cgd }
699 1.1 cgd count -= len;
700 1.1 cgd if (count < 0)
701 1.1 cgd count = 0;
702 1.1 cgd /*
703 1.1 cgd * Correct length for chain is "count".
704 1.1 cgd * Find the mbuf with last data, adjust its length,
705 1.1 cgd * and toss data from remaining mbufs on chain.
706 1.1 cgd */
707 1.1 cgd m = mp;
708 1.1 cgd if (m->m_flags & M_PKTHDR)
709 1.1 cgd m->m_pkthdr.len = count;
710 1.1 cgd for (; m; m = m->m_next) {
711 1.1 cgd if (m->m_len >= count) {
712 1.1 cgd m->m_len = count;
713 1.1 cgd break;
714 1.1 cgd }
715 1.1 cgd count -= m->m_len;
716 1.1 cgd }
717 1.18 thorpej while (m->m_next)
718 1.18 thorpej (m = m->m_next) ->m_len = 0;
719 1.1 cgd }
720 1.1 cgd }
721 1.1 cgd
722 1.1 cgd /*
723 1.1 cgd * Rearange an mbuf chain so that len bytes are contiguous
724 1.1 cgd * and in the data area of an mbuf (so that mtod and dtom
725 1.1 cgd * will work for a structure of size len). Returns the resulting
726 1.1 cgd * mbuf chain on success, frees it and returns null on failure.
727 1.1 cgd * If there is room, it will add up to max_protohdr-len extra bytes to the
728 1.1 cgd * contiguous region in an attempt to avoid being called next time.
729 1.1 cgd */
730 1.1 cgd int MPFail;
731 1.1 cgd
732 1.1 cgd struct mbuf *
733 1.62 thorpej m_pullup(struct mbuf *n, int len)
734 1.1 cgd {
735 1.27 matt struct mbuf *m;
736 1.27 matt int count;
737 1.1 cgd int space;
738 1.1 cgd
739 1.1 cgd /*
740 1.1 cgd * If first mbuf has no cluster, and has room for len bytes
741 1.1 cgd * without shifting current data, pullup into it,
742 1.1 cgd * otherwise allocate a new mbuf to prepend to the chain.
743 1.1 cgd */
744 1.1 cgd if ((n->m_flags & M_EXT) == 0 &&
745 1.1 cgd n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
746 1.1 cgd if (n->m_len >= len)
747 1.1 cgd return (n);
748 1.1 cgd m = n;
749 1.1 cgd n = n->m_next;
750 1.1 cgd len -= m->m_len;
751 1.1 cgd } else {
752 1.1 cgd if (len > MHLEN)
753 1.1 cgd goto bad;
754 1.1 cgd MGET(m, M_DONTWAIT, n->m_type);
755 1.1 cgd if (m == 0)
756 1.1 cgd goto bad;
757 1.64 matt MCLAIM(m, n->m_owner);
758 1.1 cgd m->m_len = 0;
759 1.1 cgd if (n->m_flags & M_PKTHDR) {
760 1.1 cgd M_COPY_PKTHDR(m, n);
761 1.1 cgd n->m_flags &= ~M_PKTHDR;
762 1.1 cgd }
763 1.1 cgd }
764 1.1 cgd space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
765 1.1 cgd do {
766 1.1 cgd count = min(min(max(len, max_protohdr), space), n->m_len);
767 1.30 perry memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
768 1.1 cgd (unsigned)count);
769 1.1 cgd len -= count;
770 1.1 cgd m->m_len += count;
771 1.1 cgd n->m_len -= count;
772 1.1 cgd space -= count;
773 1.1 cgd if (n->m_len)
774 1.1 cgd n->m_data += count;
775 1.1 cgd else
776 1.1 cgd n = m_free(n);
777 1.1 cgd } while (len > 0 && n);
778 1.1 cgd if (len > 0) {
779 1.1 cgd (void) m_free(m);
780 1.1 cgd goto bad;
781 1.1 cgd }
782 1.1 cgd m->m_next = n;
783 1.1 cgd return (m);
784 1.1 cgd bad:
785 1.1 cgd m_freem(n);
786 1.1 cgd MPFail++;
787 1.1 cgd return (0);
788 1.60 thorpej }
789 1.60 thorpej
790 1.60 thorpej /*
791 1.60 thorpej * Like m_pullup(), except a new mbuf is always allocated, and we allow
792 1.60 thorpej * the amount of empty space before the data in the new mbuf to be specified
793 1.60 thorpej * (in the event that the caller expects to prepend later).
794 1.60 thorpej */
795 1.60 thorpej int MSFail;
796 1.60 thorpej
797 1.60 thorpej struct mbuf *
798 1.60 thorpej m_copyup(struct mbuf *n, int len, int dstoff)
799 1.60 thorpej {
800 1.60 thorpej struct mbuf *m;
801 1.60 thorpej int count, space;
802 1.60 thorpej
803 1.60 thorpej if (len > (MHLEN - dstoff))
804 1.60 thorpej goto bad;
805 1.60 thorpej MGET(m, M_DONTWAIT, n->m_type);
806 1.60 thorpej if (m == NULL)
807 1.60 thorpej goto bad;
808 1.64 matt MCLAIM(m, n->m_owner);
809 1.60 thorpej m->m_len = 0;
810 1.60 thorpej if (n->m_flags & M_PKTHDR) {
811 1.60 thorpej M_COPY_PKTHDR(m, n);
812 1.60 thorpej n->m_flags &= ~M_PKTHDR;
813 1.60 thorpej }
814 1.60 thorpej m->m_data += dstoff;
815 1.60 thorpej space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
816 1.60 thorpej do {
817 1.60 thorpej count = min(min(max(len, max_protohdr), space), n->m_len);
818 1.60 thorpej memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
819 1.60 thorpej (unsigned)count);
820 1.60 thorpej len -= count;
821 1.60 thorpej m->m_len += count;
822 1.60 thorpej n->m_len -= count;
823 1.60 thorpej space -= count;
824 1.60 thorpej if (n->m_len)
825 1.60 thorpej n->m_data += count;
826 1.60 thorpej else
827 1.60 thorpej n = m_free(n);
828 1.60 thorpej } while (len > 0 && n);
829 1.60 thorpej if (len > 0) {
830 1.60 thorpej (void) m_free(m);
831 1.60 thorpej goto bad;
832 1.60 thorpej }
833 1.60 thorpej m->m_next = n;
834 1.60 thorpej return (m);
835 1.60 thorpej bad:
836 1.60 thorpej m_freem(n);
837 1.60 thorpej MSFail++;
838 1.60 thorpej return (NULL);
839 1.9 mycroft }
840 1.9 mycroft
841 1.9 mycroft /*
842 1.9 mycroft * Partition an mbuf chain in two pieces, returning the tail --
843 1.9 mycroft * all but the first len0 bytes. In case of failure, it returns NULL and
844 1.9 mycroft * attempts to restore the chain to its original state.
845 1.9 mycroft */
846 1.9 mycroft struct mbuf *
847 1.62 thorpej m_split(struct mbuf *m0, int len0, int wait)
848 1.9 mycroft {
849 1.27 matt struct mbuf *m, *n;
850 1.22 thorpej unsigned len = len0, remain, len_save;
851 1.9 mycroft
852 1.9 mycroft for (m = m0; m && len > m->m_len; m = m->m_next)
853 1.9 mycroft len -= m->m_len;
854 1.9 mycroft if (m == 0)
855 1.9 mycroft return (0);
856 1.9 mycroft remain = m->m_len - len;
857 1.9 mycroft if (m0->m_flags & M_PKTHDR) {
858 1.9 mycroft MGETHDR(n, wait, m0->m_type);
859 1.9 mycroft if (n == 0)
860 1.9 mycroft return (0);
861 1.64 matt MCLAIM(m, m0->m_owner);
862 1.9 mycroft n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
863 1.9 mycroft n->m_pkthdr.len = m0->m_pkthdr.len - len0;
864 1.22 thorpej len_save = m0->m_pkthdr.len;
865 1.9 mycroft m0->m_pkthdr.len = len0;
866 1.9 mycroft if (m->m_flags & M_EXT)
867 1.9 mycroft goto extpacket;
868 1.9 mycroft if (remain > MHLEN) {
869 1.9 mycroft /* m can't be the lead packet */
870 1.9 mycroft MH_ALIGN(n, 0);
871 1.9 mycroft n->m_next = m_split(m, len, wait);
872 1.9 mycroft if (n->m_next == 0) {
873 1.9 mycroft (void) m_free(n);
874 1.22 thorpej m0->m_pkthdr.len = len_save;
875 1.9 mycroft return (0);
876 1.9 mycroft } else
877 1.9 mycroft return (n);
878 1.9 mycroft } else
879 1.9 mycroft MH_ALIGN(n, remain);
880 1.9 mycroft } else if (remain == 0) {
881 1.9 mycroft n = m->m_next;
882 1.9 mycroft m->m_next = 0;
883 1.9 mycroft return (n);
884 1.9 mycroft } else {
885 1.9 mycroft MGET(n, wait, m->m_type);
886 1.9 mycroft if (n == 0)
887 1.9 mycroft return (0);
888 1.64 matt MCLAIM(n, m->m_owner);
889 1.9 mycroft M_ALIGN(n, remain);
890 1.9 mycroft }
891 1.9 mycroft extpacket:
892 1.9 mycroft if (m->m_flags & M_EXT) {
893 1.9 mycroft n->m_ext = m->m_ext;
894 1.18 thorpej MCLADDREFERENCE(m, n);
895 1.9 mycroft n->m_data = m->m_data + len;
896 1.9 mycroft } else {
897 1.30 perry memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
898 1.9 mycroft }
899 1.9 mycroft n->m_len = remain;
900 1.9 mycroft m->m_len = len;
901 1.9 mycroft n->m_next = m->m_next;
902 1.9 mycroft m->m_next = 0;
903 1.9 mycroft return (n);
904 1.9 mycroft }
905 1.9 mycroft /*
906 1.9 mycroft * Routine to copy from device local memory into mbufs.
907 1.9 mycroft */
908 1.9 mycroft struct mbuf *
909 1.62 thorpej m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
910 1.62 thorpej void (*copy)(const void *from, void *to, size_t len))
911 1.9 mycroft {
912 1.27 matt struct mbuf *m;
913 1.9 mycroft struct mbuf *top = 0, **mp = ⊤
914 1.27 matt int off = off0, len;
915 1.27 matt char *cp;
916 1.9 mycroft char *epkt;
917 1.9 mycroft
918 1.9 mycroft cp = buf;
919 1.9 mycroft epkt = cp + totlen;
920 1.9 mycroft if (off) {
921 1.13 cgd /*
922 1.13 cgd * If 'off' is non-zero, packet is trailer-encapsulated,
923 1.13 cgd * so we have to skip the type and length fields.
924 1.13 cgd */
925 1.13 cgd cp += off + 2 * sizeof(u_int16_t);
926 1.13 cgd totlen -= 2 * sizeof(u_int16_t);
927 1.9 mycroft }
928 1.9 mycroft MGETHDR(m, M_DONTWAIT, MT_DATA);
929 1.9 mycroft if (m == 0)
930 1.9 mycroft return (0);
931 1.9 mycroft m->m_pkthdr.rcvif = ifp;
932 1.9 mycroft m->m_pkthdr.len = totlen;
933 1.9 mycroft m->m_len = MHLEN;
934 1.9 mycroft
935 1.9 mycroft while (totlen > 0) {
936 1.9 mycroft if (top) {
937 1.9 mycroft MGET(m, M_DONTWAIT, MT_DATA);
938 1.9 mycroft if (m == 0) {
939 1.9 mycroft m_freem(top);
940 1.9 mycroft return (0);
941 1.9 mycroft }
942 1.9 mycroft m->m_len = MLEN;
943 1.9 mycroft }
944 1.9 mycroft len = min(totlen, epkt - cp);
945 1.9 mycroft if (len >= MINCLSIZE) {
946 1.9 mycroft MCLGET(m, M_DONTWAIT);
947 1.19 mycroft if ((m->m_flags & M_EXT) == 0) {
948 1.20 mycroft m_free(m);
949 1.19 mycroft m_freem(top);
950 1.19 mycroft return (0);
951 1.19 mycroft }
952 1.19 mycroft m->m_len = len = min(len, MCLBYTES);
953 1.9 mycroft } else {
954 1.9 mycroft /*
955 1.9 mycroft * Place initial small packet/header at end of mbuf.
956 1.9 mycroft */
957 1.9 mycroft if (len < m->m_len) {
958 1.9 mycroft if (top == 0 && len + max_linkhdr <= m->m_len)
959 1.9 mycroft m->m_data += max_linkhdr;
960 1.9 mycroft m->m_len = len;
961 1.9 mycroft } else
962 1.9 mycroft len = m->m_len;
963 1.9 mycroft }
964 1.9 mycroft if (copy)
965 1.14 christos copy(cp, mtod(m, caddr_t), (size_t)len);
966 1.9 mycroft else
967 1.30 perry memcpy(mtod(m, caddr_t), cp, (size_t)len);
968 1.9 mycroft cp += len;
969 1.9 mycroft *mp = m;
970 1.9 mycroft mp = &m->m_next;
971 1.9 mycroft totlen -= len;
972 1.9 mycroft if (cp == epkt)
973 1.9 mycroft cp = buf;
974 1.9 mycroft }
975 1.9 mycroft return (top);
976 1.18 thorpej }
977 1.18 thorpej
978 1.18 thorpej /*
979 1.18 thorpej * Copy data from a buffer back into the indicated mbuf chain,
980 1.18 thorpej * starting "off" bytes from the beginning, extending the mbuf
981 1.18 thorpej * chain if necessary.
982 1.18 thorpej */
983 1.18 thorpej void
984 1.62 thorpej m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
985 1.18 thorpej {
986 1.27 matt int mlen;
987 1.27 matt struct mbuf *m = m0, *n;
988 1.18 thorpej int totlen = 0;
989 1.18 thorpej
990 1.18 thorpej if (m0 == 0)
991 1.18 thorpej return;
992 1.18 thorpej while (off > (mlen = m->m_len)) {
993 1.18 thorpej off -= mlen;
994 1.18 thorpej totlen += mlen;
995 1.18 thorpej if (m->m_next == 0) {
996 1.18 thorpej n = m_getclr(M_DONTWAIT, m->m_type);
997 1.18 thorpej if (n == 0)
998 1.18 thorpej goto out;
999 1.18 thorpej n->m_len = min(MLEN, len + off);
1000 1.18 thorpej m->m_next = n;
1001 1.18 thorpej }
1002 1.18 thorpej m = m->m_next;
1003 1.18 thorpej }
1004 1.18 thorpej while (len > 0) {
1005 1.18 thorpej mlen = min (m->m_len - off, len);
1006 1.30 perry memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
1007 1.18 thorpej cp += mlen;
1008 1.18 thorpej len -= mlen;
1009 1.18 thorpej mlen += off;
1010 1.18 thorpej off = 0;
1011 1.18 thorpej totlen += mlen;
1012 1.18 thorpej if (len == 0)
1013 1.18 thorpej break;
1014 1.18 thorpej if (m->m_next == 0) {
1015 1.18 thorpej n = m_get(M_DONTWAIT, m->m_type);
1016 1.18 thorpej if (n == 0)
1017 1.18 thorpej break;
1018 1.18 thorpej n->m_len = min(MLEN, len);
1019 1.18 thorpej m->m_next = n;
1020 1.18 thorpej }
1021 1.18 thorpej m = m->m_next;
1022 1.18 thorpej }
1023 1.18 thorpej out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1024 1.18 thorpej m->m_pkthdr.len = totlen;
1025 1.66 thorpej }
1026 1.66 thorpej
1027 1.66 thorpej /*
1028 1.66 thorpej * Apply function f to the data in an mbuf chain starting "off" bytes from the
1029 1.66 thorpej * beginning, continuing for "len" bytes.
1030 1.66 thorpej */
1031 1.66 thorpej int
1032 1.66 thorpej m_apply(struct mbuf *m, int off, int len,
1033 1.66 thorpej int (*f)(void *, caddr_t, unsigned int), void *arg)
1034 1.66 thorpej {
1035 1.66 thorpej unsigned int count;
1036 1.66 thorpej int rval;
1037 1.66 thorpej
1038 1.66 thorpej KASSERT(len >= 0);
1039 1.66 thorpej KASSERT(off >= 0);
1040 1.66 thorpej
1041 1.66 thorpej while (off > 0) {
1042 1.66 thorpej KASSERT(m != NULL);
1043 1.66 thorpej if (off < m->m_len)
1044 1.66 thorpej break;
1045 1.66 thorpej off -= m->m_len;
1046 1.66 thorpej m = m->m_next;
1047 1.66 thorpej }
1048 1.66 thorpej while (len > 0) {
1049 1.66 thorpej KASSERT(m != NULL);
1050 1.66 thorpej count = min(m->m_len - off, len);
1051 1.66 thorpej
1052 1.66 thorpej rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1053 1.66 thorpej if (rval)
1054 1.66 thorpej return (rval);
1055 1.66 thorpej
1056 1.66 thorpej len -= count;
1057 1.66 thorpej off = 0;
1058 1.66 thorpej m = m->m_next;
1059 1.66 thorpej }
1060 1.66 thorpej
1061 1.66 thorpej return (0);
1062 1.66 thorpej }
1063 1.66 thorpej
1064 1.66 thorpej /*
1065 1.66 thorpej * Return a pointer to mbuf/offset of location in mbuf chain.
1066 1.66 thorpej */
1067 1.66 thorpej struct mbuf *
1068 1.66 thorpej m_getptr(struct mbuf *m, int loc, int *off)
1069 1.66 thorpej {
1070 1.66 thorpej
1071 1.66 thorpej while (loc >= 0) {
1072 1.66 thorpej /* Normal end of search */
1073 1.66 thorpej if (m->m_len > loc) {
1074 1.66 thorpej *off = loc;
1075 1.66 thorpej return (m);
1076 1.66 thorpej } else {
1077 1.66 thorpej loc -= m->m_len;
1078 1.66 thorpej
1079 1.66 thorpej if (m->m_next == NULL) {
1080 1.66 thorpej if (loc == 0) {
1081 1.66 thorpej /* Point at the end of valid data */
1082 1.66 thorpej *off = m->m_len;
1083 1.66 thorpej return (m);
1084 1.66 thorpej } else
1085 1.66 thorpej return (NULL);
1086 1.66 thorpej } else
1087 1.66 thorpej m = m->m_next;
1088 1.66 thorpej }
1089 1.66 thorpej }
1090 1.66 thorpej
1091 1.66 thorpej return (NULL);
1092 1.1 cgd }
1093