FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_mbuf.c
1 /*-
2 * Copyright (c) 1982, 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: releng/7.1/sys/kern/uipc_mbuf.c 209964 2010-07-13 02:45:17Z cperciva $");
34
35 #include "opt_mac.h"
36 #include "opt_param.h"
37 #include "opt_mbuf_stress_test.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/sysctl.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/uio.h>
50
51 #include <security/mac/mac_framework.h>
52
53 int max_linkhdr;
54 int max_protohdr;
55 int max_hdr;
56 int max_datalen;
57 #ifdef MBUF_STRESS_TEST
58 int m_defragpackets;
59 int m_defragbytes;
60 int m_defraguseless;
61 int m_defragfailure;
62 int m_defragrandomfailures;
63 #endif
64
65 /*
66 * sysctl(8) exported objects
67 */
68 SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RD,
69 &max_linkhdr, 0, "Size of largest link layer header");
70 SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RD,
71 &max_protohdr, 0, "Size of largest protocol layer header");
72 SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RD,
73 &max_hdr, 0, "Size of largest link plus protocol header");
74 SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RD,
75 &max_datalen, 0, "Minimum space left in mbuf after max_hdr");
76 #ifdef MBUF_STRESS_TEST
77 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
78 &m_defragpackets, 0, "");
79 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
80 &m_defragbytes, 0, "");
81 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
82 &m_defraguseless, 0, "");
83 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
84 &m_defragfailure, 0, "");
85 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
86 &m_defragrandomfailures, 0, "");
87 #endif
88
89 /*
90 * Allocate a given length worth of mbufs and/or clusters (whatever fits
91 * best) and return a pointer to the top of the allocated chain. If an
92 * existing mbuf chain is provided, then we will append the new chain
93 * to the existing one but still return the top of the newly allocated
94 * chain.
95 */
96 struct mbuf *
97 m_getm2(struct mbuf *m, int len, int how, short type, int flags)
98 {
99 struct mbuf *mb, *nm = NULL, *mtail = NULL;
100
101 KASSERT(len >= 0, ("%s: len is < 0", __func__));
102
103 /* Validate flags. */
104 flags &= (M_PKTHDR | M_EOR);
105
106 /* Packet header mbuf must be first in chain. */
107 if ((flags & M_PKTHDR) && m != NULL)
108 flags &= ~M_PKTHDR;
109
110 /* Loop and append maximum sized mbufs to the chain tail. */
111 while (len > 0) {
112 if (len > MCLBYTES)
113 mb = m_getjcl(how, type, (flags & M_PKTHDR),
114 MJUMPAGESIZE);
115 else if (len >= MINCLSIZE)
116 mb = m_getcl(how, type, (flags & M_PKTHDR));
117 else if (flags & M_PKTHDR)
118 mb = m_gethdr(how, type);
119 else
120 mb = m_get(how, type);
121
122 /* Fail the whole operation if one mbuf can't be allocated. */
123 if (mb == NULL) {
124 if (nm != NULL)
125 m_freem(nm);
126 return (NULL);
127 }
128
129 /* Book keeping. */
130 len -= (mb->m_flags & M_EXT) ? mb->m_ext.ext_size :
131 ((mb->m_flags & M_PKTHDR) ? MHLEN : MLEN);
132 if (mtail != NULL)
133 mtail->m_next = mb;
134 else
135 nm = mb;
136 mtail = mb;
137 flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */
138 }
139 if (flags & M_EOR)
140 mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */
141
142 /* If mbuf was supplied, append new chain to the end of it. */
143 if (m != NULL) {
144 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
145 ;
146 mtail->m_next = nm;
147 mtail->m_flags &= ~M_EOR;
148 } else
149 m = nm;
150
151 return (m);
152 }
153
154 /*
155 * Free an entire chain of mbufs and associated external buffers, if
156 * applicable.
157 */
158 void
159 m_freem(struct mbuf *mb)
160 {
161
162 while (mb != NULL)
163 mb = m_free(mb);
164 }
165
166 /*-
167 * Configure a provided mbuf to refer to the provided external storage
168 * buffer and setup a reference count for said buffer. If the setting
169 * up of the reference count fails, the M_EXT bit will not be set. If
170 * successfull, the M_EXT bit is set in the mbuf's flags.
171 *
172 * Arguments:
173 * mb The existing mbuf to which to attach the provided buffer.
174 * buf The address of the provided external storage buffer.
175 * size The size of the provided buffer.
176 * freef A pointer to a routine that is responsible for freeing the
177 * provided external storage buffer.
178 * args A pointer to an argument structure (of any type) to be passed
179 * to the provided freef routine (may be NULL).
180 * flags Any other flags to be passed to the provided mbuf.
181 * type The type that the external storage buffer should be
182 * labeled with.
183 *
184 * Returns:
185 * Nothing.
186 */
187 void
188 m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
189 void (*freef)(void *, void *), void *args, int flags, int type)
190 {
191 KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
192
193 if (type != EXT_EXTREF)
194 mb->m_ext.ref_cnt = (u_int *)uma_zalloc(zone_ext_refcnt, M_NOWAIT);
195 if (mb->m_ext.ref_cnt != NULL) {
196 *(mb->m_ext.ref_cnt) = 1;
197 mb->m_flags |= (M_EXT | flags);
198 mb->m_ext.ext_buf = buf;
199 mb->m_data = mb->m_ext.ext_buf;
200 mb->m_ext.ext_size = size;
201 mb->m_ext.ext_free = freef;
202 mb->m_ext.ext_args = args;
203 mb->m_ext.ext_type = type;
204 }
205 }
206
207 /*
208 * Non-directly-exported function to clean up after mbufs with M_EXT
209 * storage attached to them if the reference count hits 1.
210 */
211 void
212 mb_free_ext(struct mbuf *m)
213 {
214 int skipmbuf;
215
216 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
217 KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
218
219
220 /*
221 * check if the header is embedded in the cluster
222 */
223 skipmbuf = (m->m_flags & M_NOFREE);
224
225 /* Free attached storage if this mbuf is the only reference to it. */
226 if (*(m->m_ext.ref_cnt) == 1 ||
227 atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 1) {
228 switch (m->m_ext.ext_type) {
229 case EXT_PACKET: /* The packet zone is special. */
230 if (*(m->m_ext.ref_cnt) == 0)
231 *(m->m_ext.ref_cnt) = 1;
232 uma_zfree(zone_pack, m);
233 return; /* Job done. */
234 case EXT_CLUSTER:
235 uma_zfree(zone_clust, m->m_ext.ext_buf);
236 break;
237 case EXT_JUMBOP:
238 uma_zfree(zone_jumbop, m->m_ext.ext_buf);
239 break;
240 case EXT_JUMBO9:
241 uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
242 break;
243 case EXT_JUMBO16:
244 uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
245 break;
246 case EXT_SFBUF:
247 case EXT_NET_DRV:
248 case EXT_MOD_TYPE:
249 case EXT_DISPOSABLE:
250 *(m->m_ext.ref_cnt) = 0;
251 uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *,
252 m->m_ext.ref_cnt));
253 /* FALLTHROUGH */
254 case EXT_EXTREF:
255 KASSERT(m->m_ext.ext_free != NULL,
256 ("%s: ext_free not set", __func__));
257 (*(m->m_ext.ext_free))(m->m_ext.ext_buf,
258 m->m_ext.ext_args);
259 break;
260 default:
261 KASSERT(m->m_ext.ext_type == 0,
262 ("%s: unknown ext_type", __func__));
263 }
264 }
265 if (skipmbuf)
266 return;
267
268 /*
269 * Free this mbuf back to the mbuf zone with all m_ext
270 * information purged.
271 */
272 m->m_ext.ext_buf = NULL;
273 m->m_ext.ext_free = NULL;
274 m->m_ext.ext_args = NULL;
275 m->m_ext.ref_cnt = NULL;
276 m->m_ext.ext_size = 0;
277 m->m_ext.ext_type = 0;
278 m->m_flags &= ~M_EXT;
279 uma_zfree(zone_mbuf, m);
280 }
281
282 /*
283 * Attach the the cluster from *m to *n, set up m_ext in *n
284 * and bump the refcount of the cluster.
285 */
286 static void
287 mb_dupcl(struct mbuf *n, struct mbuf *m)
288 {
289 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
290 KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
291 KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
292
293 if (*(m->m_ext.ref_cnt) == 1)
294 *(m->m_ext.ref_cnt) += 1;
295 else
296 atomic_add_int(m->m_ext.ref_cnt, 1);
297 n->m_ext.ext_buf = m->m_ext.ext_buf;
298 n->m_ext.ext_free = m->m_ext.ext_free;
299 n->m_ext.ext_args = m->m_ext.ext_args;
300 n->m_ext.ext_size = m->m_ext.ext_size;
301 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
302 n->m_ext.ext_type = m->m_ext.ext_type;
303 n->m_flags |= M_EXT;
304 n->m_flags |= m->m_flags & M_RDONLY;
305 }
306
307 /*
308 * Clean up mbuf (chain) from any tags and packet headers.
309 * If "all" is set then the first mbuf in the chain will be
310 * cleaned too.
311 */
312 void
313 m_demote(struct mbuf *m0, int all)
314 {
315 struct mbuf *m;
316
317 for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) {
318 if (m->m_flags & M_PKTHDR) {
319 m_tag_delete_chain(m, NULL);
320 m->m_flags &= ~M_PKTHDR;
321 bzero(&m->m_pkthdr, sizeof(struct pkthdr));
322 }
323 if (m->m_type == MT_HEADER)
324 m->m_type = MT_DATA;
325 if (m != m0 && m->m_nextpkt != NULL)
326 m->m_nextpkt = NULL;
327 m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST);
328 }
329 }
330
331 /*
332 * Sanity checks on mbuf (chain) for use in KASSERT() and general
333 * debugging.
334 * Returns 0 or panics when bad and 1 on all tests passed.
335 * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they
336 * blow up later.
337 */
338 int
339 m_sanity(struct mbuf *m0, int sanitize)
340 {
341 struct mbuf *m;
342 caddr_t a, b;
343 int pktlen = 0;
344
345 #ifdef INVARIANTS
346 #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m)
347 #else
348 #define M_SANITY_ACTION(s) printf("mbuf %p: " s, m)
349 #endif
350
351 for (m = m0; m != NULL; m = m->m_next) {
352 /*
353 * Basic pointer checks. If any of these fails then some
354 * unrelated kernel memory before or after us is trashed.
355 * No way to recover from that.
356 */
357 a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf :
358 ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) :
359 (caddr_t)(&m->m_dat)) );
360 b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size :
361 ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN)));
362 if ((caddr_t)m->m_data < a)
363 M_SANITY_ACTION("m_data outside mbuf data range left");
364 if ((caddr_t)m->m_data > b)
365 M_SANITY_ACTION("m_data outside mbuf data range right");
366 if ((caddr_t)m->m_data + m->m_len > b)
367 M_SANITY_ACTION("m_data + m_len exeeds mbuf space");
368 if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) {
369 if ((caddr_t)m->m_pkthdr.header < a ||
370 (caddr_t)m->m_pkthdr.header > b)
371 M_SANITY_ACTION("m_pkthdr.header outside mbuf data range");
372 }
373
374 /* m->m_nextpkt may only be set on first mbuf in chain. */
375 if (m != m0 && m->m_nextpkt != NULL) {
376 if (sanitize) {
377 m_freem(m->m_nextpkt);
378 m->m_nextpkt = (struct mbuf *)0xDEADC0DE;
379 } else
380 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf");
381 }
382
383 /* packet length (not mbuf length!) calculation */
384 if (m0->m_flags & M_PKTHDR)
385 pktlen += m->m_len;
386
387 /* m_tags may only be attached to first mbuf in chain. */
388 if (m != m0 && m->m_flags & M_PKTHDR &&
389 !SLIST_EMPTY(&m->m_pkthdr.tags)) {
390 if (sanitize) {
391 m_tag_delete_chain(m, NULL);
392 /* put in 0xDEADC0DE perhaps? */
393 } else
394 M_SANITY_ACTION("m_tags on in-chain mbuf");
395 }
396
397 /* M_PKTHDR may only be set on first mbuf in chain */
398 if (m != m0 && m->m_flags & M_PKTHDR) {
399 if (sanitize) {
400 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
401 m->m_flags &= ~M_PKTHDR;
402 /* put in 0xDEADCODE and leave hdr flag in */
403 } else
404 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf");
405 }
406 }
407 m = m0;
408 if (pktlen && pktlen != m->m_pkthdr.len) {
409 if (sanitize)
410 m->m_pkthdr.len = 0;
411 else
412 M_SANITY_ACTION("m_pkthdr.len != mbuf chain length");
413 }
414 return 1;
415
416 #undef M_SANITY_ACTION
417 }
418
419
420 /*
421 * "Move" mbuf pkthdr from "from" to "to".
422 * "from" must have M_PKTHDR set, and "to" must be empty.
423 */
424 void
425 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
426 {
427
428 #if 0
429 /* see below for why these are not enabled */
430 M_ASSERTPKTHDR(to);
431 /* Note: with MAC, this may not be a good assertion. */
432 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
433 ("m_move_pkthdr: to has tags"));
434 #endif
435 #ifdef MAC
436 /*
437 * XXXMAC: It could be this should also occur for non-MAC?
438 */
439 if (to->m_flags & M_PKTHDR)
440 m_tag_delete_chain(to, NULL);
441 #endif
442 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
443 if ((to->m_flags & M_EXT) == 0)
444 to->m_data = to->m_pktdat;
445 to->m_pkthdr = from->m_pkthdr; /* especially tags */
446 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */
447 from->m_flags &= ~M_PKTHDR;
448 }
449
450 /*
451 * Duplicate "from"'s mbuf pkthdr in "to".
452 * "from" must have M_PKTHDR set, and "to" must be empty.
453 * In particular, this does a deep copy of the packet tags.
454 */
455 int
456 m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
457 {
458
459 #if 0
460 /*
461 * The mbuf allocator only initializes the pkthdr
462 * when the mbuf is allocated with MGETHDR. Many users
463 * (e.g. m_copy*, m_prepend) use MGET and then
464 * smash the pkthdr as needed causing these
465 * assertions to trip. For now just disable them.
466 */
467 M_ASSERTPKTHDR(to);
468 /* Note: with MAC, this may not be a good assertion. */
469 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
470 #endif
471 MBUF_CHECKSLEEP(how);
472 #ifdef MAC
473 if (to->m_flags & M_PKTHDR)
474 m_tag_delete_chain(to, NULL);
475 #endif
476 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
477 if ((to->m_flags & M_EXT) == 0)
478 to->m_data = to->m_pktdat;
479 to->m_pkthdr = from->m_pkthdr;
480 SLIST_INIT(&to->m_pkthdr.tags);
481 return (m_tag_copy_chain(to, from, MBTOM(how)));
482 }
483
484 /*
485 * Lesser-used path for M_PREPEND:
486 * allocate new mbuf to prepend to chain,
487 * copy junk along.
488 */
489 struct mbuf *
490 m_prepend(struct mbuf *m, int len, int how)
491 {
492 struct mbuf *mn;
493
494 if (m->m_flags & M_PKTHDR)
495 MGETHDR(mn, how, m->m_type);
496 else
497 MGET(mn, how, m->m_type);
498 if (mn == NULL) {
499 m_freem(m);
500 return (NULL);
501 }
502 if (m->m_flags & M_PKTHDR)
503 M_MOVE_PKTHDR(mn, m);
504 mn->m_next = m;
505 m = mn;
506 if(m->m_flags & M_PKTHDR) {
507 if (len < MHLEN)
508 MH_ALIGN(m, len);
509 } else {
510 if (len < MLEN)
511 M_ALIGN(m, len);
512 }
513 m->m_len = len;
514 return (m);
515 }
516
517 /*
518 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
519 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
520 * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
521 * Note that the copy is read-only, because clusters are not copied,
522 * only their reference counts are incremented.
523 */
524 struct mbuf *
525 m_copym(struct mbuf *m, int off0, int len, int wait)
526 {
527 struct mbuf *n, **np;
528 int off = off0;
529 struct mbuf *top;
530 int copyhdr = 0;
531
532 KASSERT(off >= 0, ("m_copym, negative off %d", off));
533 KASSERT(len >= 0, ("m_copym, negative len %d", len));
534 MBUF_CHECKSLEEP(wait);
535 if (off == 0 && m->m_flags & M_PKTHDR)
536 copyhdr = 1;
537 while (off > 0) {
538 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
539 if (off < m->m_len)
540 break;
541 off -= m->m_len;
542 m = m->m_next;
543 }
544 np = ⊤
545 top = 0;
546 while (len > 0) {
547 if (m == NULL) {
548 KASSERT(len == M_COPYALL,
549 ("m_copym, length > size of mbuf chain"));
550 break;
551 }
552 if (copyhdr)
553 MGETHDR(n, wait, m->m_type);
554 else
555 MGET(n, wait, m->m_type);
556 *np = n;
557 if (n == NULL)
558 goto nospace;
559 if (copyhdr) {
560 if (!m_dup_pkthdr(n, m, wait))
561 goto nospace;
562 if (len == M_COPYALL)
563 n->m_pkthdr.len -= off0;
564 else
565 n->m_pkthdr.len = len;
566 copyhdr = 0;
567 }
568 n->m_len = min(len, m->m_len - off);
569 if (m->m_flags & M_EXT) {
570 n->m_data = m->m_data + off;
571 mb_dupcl(n, m);
572 } else
573 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
574 (u_int)n->m_len);
575 if (len != M_COPYALL)
576 len -= n->m_len;
577 off = 0;
578 m = m->m_next;
579 np = &n->m_next;
580 }
581 if (top == NULL)
582 mbstat.m_mcfail++; /* XXX: No consistency. */
583
584 return (top);
585 nospace:
586 m_freem(top);
587 mbstat.m_mcfail++; /* XXX: No consistency. */
588 return (NULL);
589 }
590
591 /*
592 * Returns mbuf chain with new head for the prepending case.
593 * Copies from mbuf (chain) n from off for len to mbuf (chain) m
594 * either prepending or appending the data.
595 * The resulting mbuf (chain) m is fully writeable.
596 * m is destination (is made writeable)
597 * n is source, off is offset in source, len is len from offset
598 * dir, 0 append, 1 prepend
599 * how, wait or nowait
600 */
601
602 static int
603 m_bcopyxxx(void *s, void *t, u_int len)
604 {
605 bcopy(s, t, (size_t)len);
606 return 0;
607 }
608
609 struct mbuf *
610 m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len,
611 int prep, int how)
612 {
613 struct mbuf *mm, *x, *z, *prev = NULL;
614 caddr_t p;
615 int i, nlen = 0;
616 caddr_t buf[MLEN];
617
618 KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source"));
619 KASSERT(off >= 0, ("m_copymdata, negative off %d", off));
620 KASSERT(len >= 0, ("m_copymdata, negative len %d", len));
621 KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep));
622
623 mm = m;
624 if (!prep) {
625 while(mm->m_next) {
626 prev = mm;
627 mm = mm->m_next;
628 }
629 }
630 for (z = n; z != NULL; z = z->m_next)
631 nlen += z->m_len;
632 if (len == M_COPYALL)
633 len = nlen - off;
634 if (off + len > nlen || len < 1)
635 return NULL;
636
637 if (!M_WRITABLE(mm)) {
638 /* XXX: Use proper m_xxx function instead. */
639 x = m_getcl(how, MT_DATA, mm->m_flags);
640 if (x == NULL)
641 return NULL;
642 bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size);
643 p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf);
644 x->m_data = p;
645 mm->m_next = NULL;
646 if (mm != m)
647 prev->m_next = x;
648 m_free(mm);
649 mm = x;
650 }
651
652 /*
653 * Append/prepend the data. Allocating mbufs as necessary.
654 */
655 /* Shortcut if enough free space in first/last mbuf. */
656 if (!prep && M_TRAILINGSPACE(mm) >= len) {
657 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) +
658 mm->m_len);
659 mm->m_len += len;
660 mm->m_pkthdr.len += len;
661 return m;
662 }
663 if (prep && M_LEADINGSPACE(mm) >= len) {
664 mm->m_data = mtod(mm, caddr_t) - len;
665 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t));
666 mm->m_len += len;
667 mm->m_pkthdr.len += len;
668 return mm;
669 }
670
671 /* Expand first/last mbuf to cluster if possible. */
672 if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) {
673 bcopy(mm->m_data, &buf, mm->m_len);
674 m_clget(mm, how);
675 if (!(mm->m_flags & M_EXT))
676 return NULL;
677 bcopy(&buf, mm->m_ext.ext_buf, mm->m_len);
678 mm->m_data = mm->m_ext.ext_buf;
679 mm->m_pkthdr.header = NULL;
680 }
681 if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) {
682 bcopy(mm->m_data, &buf, mm->m_len);
683 m_clget(mm, how);
684 if (!(mm->m_flags & M_EXT))
685 return NULL;
686 bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf +
687 mm->m_ext.ext_size - mm->m_len, mm->m_len);
688 mm->m_data = (caddr_t)mm->m_ext.ext_buf +
689 mm->m_ext.ext_size - mm->m_len;
690 mm->m_pkthdr.header = NULL;
691 }
692
693 /* Append/prepend as many mbuf (clusters) as necessary to fit len. */
694 if (!prep && len > M_TRAILINGSPACE(mm)) {
695 if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA))
696 return NULL;
697 }
698 if (prep && len > M_LEADINGSPACE(mm)) {
699 if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA)))
700 return NULL;
701 i = 0;
702 for (x = z; x != NULL; x = x->m_next) {
703 i += x->m_flags & M_EXT ? x->m_ext.ext_size :
704 (x->m_flags & M_PKTHDR ? MHLEN : MLEN);
705 if (!x->m_next)
706 break;
707 }
708 z->m_data += i - len;
709 m_move_pkthdr(mm, z);
710 x->m_next = mm;
711 mm = z;
712 }
713
714 /* Seek to start position in source mbuf. Optimization for long chains. */
715 while (off > 0) {
716 if (off < n->m_len)
717 break;
718 off -= n->m_len;
719 n = n->m_next;
720 }
721
722 /* Copy data into target mbuf. */
723 z = mm;
724 while (len > 0) {
725 KASSERT(z != NULL, ("m_copymdata, falling off target edge"));
726 i = M_TRAILINGSPACE(z);
727 m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len);
728 z->m_len += i;
729 /* fixup pkthdr.len if necessary */
730 if ((prep ? mm : m)->m_flags & M_PKTHDR)
731 (prep ? mm : m)->m_pkthdr.len += i;
732 off += i;
733 len -= i;
734 z = z->m_next;
735 }
736 return (prep ? mm : m);
737 }
738
739 /*
740 * Copy an entire packet, including header (which must be present).
741 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
742 * Note that the copy is read-only, because clusters are not copied,
743 * only their reference counts are incremented.
744 * Preserve alignment of the first mbuf so if the creator has left
745 * some room at the beginning (e.g. for inserting protocol headers)
746 * the copies still have the room available.
747 */
748 struct mbuf *
749 m_copypacket(struct mbuf *m, int how)
750 {
751 struct mbuf *top, *n, *o;
752
753 MBUF_CHECKSLEEP(how);
754 MGET(n, how, m->m_type);
755 top = n;
756 if (n == NULL)
757 goto nospace;
758
759 if (!m_dup_pkthdr(n, m, how))
760 goto nospace;
761 n->m_len = m->m_len;
762 if (m->m_flags & M_EXT) {
763 n->m_data = m->m_data;
764 mb_dupcl(n, m);
765 } else {
766 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
767 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
768 }
769
770 m = m->m_next;
771 while (m) {
772 MGET(o, how, m->m_type);
773 if (o == NULL)
774 goto nospace;
775
776 n->m_next = o;
777 n = n->m_next;
778
779 n->m_len = m->m_len;
780 if (m->m_flags & M_EXT) {
781 n->m_data = m->m_data;
782 mb_dupcl(n, m);
783 } else {
784 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
785 }
786
787 m = m->m_next;
788 }
789 return top;
790 nospace:
791 m_freem(top);
792 mbstat.m_mcfail++; /* XXX: No consistency. */
793 return (NULL);
794 }
795
796 /*
797 * Copy data from an mbuf chain starting "off" bytes from the beginning,
798 * continuing for "len" bytes, into the indicated buffer.
799 */
800 void
801 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
802 {
803 u_int count;
804
805 KASSERT(off >= 0, ("m_copydata, negative off %d", off));
806 KASSERT(len >= 0, ("m_copydata, negative len %d", len));
807 while (off > 0) {
808 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
809 if (off < m->m_len)
810 break;
811 off -= m->m_len;
812 m = m->m_next;
813 }
814 while (len > 0) {
815 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
816 count = min(m->m_len - off, len);
817 bcopy(mtod(m, caddr_t) + off, cp, count);
818 len -= count;
819 cp += count;
820 off = 0;
821 m = m->m_next;
822 }
823 }
824
825 /*
826 * Copy a packet header mbuf chain into a completely new chain, including
827 * copying any mbuf clusters. Use this instead of m_copypacket() when
828 * you need a writable copy of an mbuf chain.
829 */
830 struct mbuf *
831 m_dup(struct mbuf *m, int how)
832 {
833 struct mbuf **p, *top = NULL;
834 int remain, moff, nsize;
835
836 MBUF_CHECKSLEEP(how);
837 /* Sanity check */
838 if (m == NULL)
839 return (NULL);
840 M_ASSERTPKTHDR(m);
841
842 /* While there's more data, get a new mbuf, tack it on, and fill it */
843 remain = m->m_pkthdr.len;
844 moff = 0;
845 p = ⊤
846 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */
847 struct mbuf *n;
848
849 /* Get the next new mbuf */
850 if (remain >= MINCLSIZE) {
851 n = m_getcl(how, m->m_type, 0);
852 nsize = MCLBYTES;
853 } else {
854 n = m_get(how, m->m_type);
855 nsize = MLEN;
856 }
857 if (n == NULL)
858 goto nospace;
859
860 if (top == NULL) { /* First one, must be PKTHDR */
861 if (!m_dup_pkthdr(n, m, how)) {
862 m_free(n);
863 goto nospace;
864 }
865 if ((n->m_flags & M_EXT) == 0)
866 nsize = MHLEN;
867 }
868 n->m_len = 0;
869
870 /* Link it into the new chain */
871 *p = n;
872 p = &n->m_next;
873
874 /* Copy data from original mbuf(s) into new mbuf */
875 while (n->m_len < nsize && m != NULL) {
876 int chunk = min(nsize - n->m_len, m->m_len - moff);
877
878 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
879 moff += chunk;
880 n->m_len += chunk;
881 remain -= chunk;
882 if (moff == m->m_len) {
883 m = m->m_next;
884 moff = 0;
885 }
886 }
887
888 /* Check correct total mbuf length */
889 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
890 ("%s: bogus m_pkthdr.len", __func__));
891 }
892 return (top);
893
894 nospace:
895 m_freem(top);
896 mbstat.m_mcfail++; /* XXX: No consistency. */
897 return (NULL);
898 }
899
900 /*
901 * Concatenate mbuf chain n to m.
902 * Both chains must be of the same type (e.g. MT_DATA).
903 * Any m_pkthdr is not updated.
904 */
905 void
906 m_cat(struct mbuf *m, struct mbuf *n)
907 {
908 while (m->m_next)
909 m = m->m_next;
910 while (n) {
911 if (m->m_flags & M_EXT ||
912 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
913 /* just join the two chains */
914 m->m_next = n;
915 return;
916 }
917 /* splat the data from one into the other */
918 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
919 (u_int)n->m_len);
920 m->m_len += n->m_len;
921 n = m_free(n);
922 }
923 }
924
925 void
926 m_adj(struct mbuf *mp, int req_len)
927 {
928 int len = req_len;
929 struct mbuf *m;
930 int count;
931
932 if ((m = mp) == NULL)
933 return;
934 if (len >= 0) {
935 /*
936 * Trim from head.
937 */
938 while (m != NULL && len > 0) {
939 if (m->m_len <= len) {
940 len -= m->m_len;
941 m->m_len = 0;
942 m = m->m_next;
943 } else {
944 m->m_len -= len;
945 m->m_data += len;
946 len = 0;
947 }
948 }
949 m = mp;
950 if (mp->m_flags & M_PKTHDR)
951 m->m_pkthdr.len -= (req_len - len);
952 } else {
953 /*
954 * Trim from tail. Scan the mbuf chain,
955 * calculating its length and finding the last mbuf.
956 * If the adjustment only affects this mbuf, then just
957 * adjust and return. Otherwise, rescan and truncate
958 * after the remaining size.
959 */
960 len = -len;
961 count = 0;
962 for (;;) {
963 count += m->m_len;
964 if (m->m_next == (struct mbuf *)0)
965 break;
966 m = m->m_next;
967 }
968 if (m->m_len >= len) {
969 m->m_len -= len;
970 if (mp->m_flags & M_PKTHDR)
971 mp->m_pkthdr.len -= len;
972 return;
973 }
974 count -= len;
975 if (count < 0)
976 count = 0;
977 /*
978 * Correct length for chain is "count".
979 * Find the mbuf with last data, adjust its length,
980 * and toss data from remaining mbufs on chain.
981 */
982 m = mp;
983 if (m->m_flags & M_PKTHDR)
984 m->m_pkthdr.len = count;
985 for (; m; m = m->m_next) {
986 if (m->m_len >= count) {
987 m->m_len = count;
988 if (m->m_next != NULL) {
989 m_freem(m->m_next);
990 m->m_next = NULL;
991 }
992 break;
993 }
994 count -= m->m_len;
995 }
996 }
997 }
998
999 /*
1000 * Rearange an mbuf chain so that len bytes are contiguous
1001 * and in the data area of an mbuf (so that mtod and dtom
1002 * will work for a structure of size len). Returns the resulting
1003 * mbuf chain on success, frees it and returns null on failure.
1004 * If there is room, it will add up to max_protohdr-len extra bytes to the
1005 * contiguous region in an attempt to avoid being called next time.
1006 */
1007 struct mbuf *
1008 m_pullup(struct mbuf *n, int len)
1009 {
1010 struct mbuf *m;
1011 int count;
1012 int space;
1013
1014 /*
1015 * If first mbuf has no cluster, and has room for len bytes
1016 * without shifting current data, pullup into it,
1017 * otherwise allocate a new mbuf to prepend to the chain.
1018 */
1019 if ((n->m_flags & M_EXT) == 0 &&
1020 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
1021 if (n->m_len >= len)
1022 return (n);
1023 m = n;
1024 n = n->m_next;
1025 len -= m->m_len;
1026 } else {
1027 if (len > MHLEN)
1028 goto bad;
1029 MGET(m, M_DONTWAIT, n->m_type);
1030 if (m == NULL)
1031 goto bad;
1032 m->m_len = 0;
1033 if (n->m_flags & M_PKTHDR)
1034 M_MOVE_PKTHDR(m, n);
1035 }
1036 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1037 do {
1038 count = min(min(max(len, max_protohdr), space), n->m_len);
1039 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1040 (u_int)count);
1041 len -= count;
1042 m->m_len += count;
1043 n->m_len -= count;
1044 space -= count;
1045 if (n->m_len)
1046 n->m_data += count;
1047 else
1048 n = m_free(n);
1049 } while (len > 0 && n);
1050 if (len > 0) {
1051 (void) m_free(m);
1052 goto bad;
1053 }
1054 m->m_next = n;
1055 return (m);
1056 bad:
1057 m_freem(n);
1058 mbstat.m_mpfail++; /* XXX: No consistency. */
1059 return (NULL);
1060 }
1061
1062 /*
1063 * Like m_pullup(), except a new mbuf is always allocated, and we allow
1064 * the amount of empty space before the data in the new mbuf to be specified
1065 * (in the event that the caller expects to prepend later).
1066 */
1067 int MSFail;
1068
1069 struct mbuf *
1070 m_copyup(struct mbuf *n, int len, int dstoff)
1071 {
1072 struct mbuf *m;
1073 int count, space;
1074
1075 if (len > (MHLEN - dstoff))
1076 goto bad;
1077 MGET(m, M_DONTWAIT, n->m_type);
1078 if (m == NULL)
1079 goto bad;
1080 m->m_len = 0;
1081 if (n->m_flags & M_PKTHDR)
1082 M_MOVE_PKTHDR(m, n);
1083 m->m_data += dstoff;
1084 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1085 do {
1086 count = min(min(max(len, max_protohdr), space), n->m_len);
1087 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
1088 (unsigned)count);
1089 len -= count;
1090 m->m_len += count;
1091 n->m_len -= count;
1092 space -= count;
1093 if (n->m_len)
1094 n->m_data += count;
1095 else
1096 n = m_free(n);
1097 } while (len > 0 && n);
1098 if (len > 0) {
1099 (void) m_free(m);
1100 goto bad;
1101 }
1102 m->m_next = n;
1103 return (m);
1104 bad:
1105 m_freem(n);
1106 MSFail++;
1107 return (NULL);
1108 }
1109
1110 /*
1111 * Partition an mbuf chain in two pieces, returning the tail --
1112 * all but the first len0 bytes. In case of failure, it returns NULL and
1113 * attempts to restore the chain to its original state.
1114 *
1115 * Note that the resulting mbufs might be read-only, because the new
1116 * mbuf can end up sharing an mbuf cluster with the original mbuf if
1117 * the "breaking point" happens to lie within a cluster mbuf. Use the
1118 * M_WRITABLE() macro to check for this case.
1119 */
1120 struct mbuf *
1121 m_split(struct mbuf *m0, int len0, int wait)
1122 {
1123 struct mbuf *m, *n;
1124 u_int len = len0, remain;
1125
1126 MBUF_CHECKSLEEP(wait);
1127 for (m = m0; m && len > m->m_len; m = m->m_next)
1128 len -= m->m_len;
1129 if (m == NULL)
1130 return (NULL);
1131 remain = m->m_len - len;
1132 if (m0->m_flags & M_PKTHDR) {
1133 MGETHDR(n, wait, m0->m_type);
1134 if (n == NULL)
1135 return (NULL);
1136 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1137 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1138 m0->m_pkthdr.len = len0;
1139 if (m->m_flags & M_EXT)
1140 goto extpacket;
1141 if (remain > MHLEN) {
1142 /* m can't be the lead packet */
1143 MH_ALIGN(n, 0);
1144 n->m_next = m_split(m, len, wait);
1145 if (n->m_next == NULL) {
1146 (void) m_free(n);
1147 return (NULL);
1148 } else {
1149 n->m_len = 0;
1150 return (n);
1151 }
1152 } else
1153 MH_ALIGN(n, remain);
1154 } else if (remain == 0) {
1155 n = m->m_next;
1156 m->m_next = NULL;
1157 return (n);
1158 } else {
1159 MGET(n, wait, m->m_type);
1160 if (n == NULL)
1161 return (NULL);
1162 M_ALIGN(n, remain);
1163 }
1164 extpacket:
1165 if (m->m_flags & M_EXT) {
1166 n->m_data = m->m_data + len;
1167 mb_dupcl(n, m);
1168 } else {
1169 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1170 }
1171 n->m_len = remain;
1172 m->m_len = len;
1173 n->m_next = m->m_next;
1174 m->m_next = NULL;
1175 return (n);
1176 }
1177 /*
1178 * Routine to copy from device local memory into mbufs.
1179 * Note that `off' argument is offset into first mbuf of target chain from
1180 * which to begin copying the data to.
1181 */
1182 struct mbuf *
1183 m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
1184 void (*copy)(char *from, caddr_t to, u_int len))
1185 {
1186 struct mbuf *m;
1187 struct mbuf *top = NULL, **mp = ⊤
1188 int len;
1189
1190 if (off < 0 || off > MHLEN)
1191 return (NULL);
1192
1193 while (totlen > 0) {
1194 if (top == NULL) { /* First one, must be PKTHDR */
1195 if (totlen + off >= MINCLSIZE) {
1196 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1197 len = MCLBYTES;
1198 } else {
1199 m = m_gethdr(M_DONTWAIT, MT_DATA);
1200 len = MHLEN;
1201
1202 /* Place initial small packet/header at end of mbuf */
1203 if (m && totlen + off + max_linkhdr <= MLEN) {
1204 m->m_data += max_linkhdr;
1205 len -= max_linkhdr;
1206 }
1207 }
1208 if (m == NULL)
1209 return NULL;
1210 m->m_pkthdr.rcvif = ifp;
1211 m->m_pkthdr.len = totlen;
1212 } else {
1213 if (totlen + off >= MINCLSIZE) {
1214 m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1215 len = MCLBYTES;
1216 } else {
1217 m = m_get(M_DONTWAIT, MT_DATA);
1218 len = MLEN;
1219 }
1220 if (m == NULL) {
1221 m_freem(top);
1222 return NULL;
1223 }
1224 }
1225 if (off) {
1226 m->m_data += off;
1227 len -= off;
1228 off = 0;
1229 }
1230 m->m_len = len = min(totlen, len);
1231 if (copy)
1232 copy(buf, mtod(m, caddr_t), (u_int)len);
1233 else
1234 bcopy(buf, mtod(m, caddr_t), (u_int)len);
1235 buf += len;
1236 *mp = m;
1237 mp = &m->m_next;
1238 totlen -= len;
1239 }
1240 return (top);
1241 }
1242
1243 /*
1244 * Copy data from a buffer back into the indicated mbuf chain,
1245 * starting "off" bytes from the beginning, extending the mbuf
1246 * chain if necessary.
1247 */
1248 void
1249 m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
1250 {
1251 int mlen;
1252 struct mbuf *m = m0, *n;
1253 int totlen = 0;
1254
1255 if (m0 == NULL)
1256 return;
1257 while (off > (mlen = m->m_len)) {
1258 off -= mlen;
1259 totlen += mlen;
1260 if (m->m_next == NULL) {
1261 n = m_get(M_DONTWAIT, m->m_type);
1262 if (n == NULL)
1263 goto out;
1264 bzero(mtod(n, caddr_t), MLEN);
1265 n->m_len = min(MLEN, len + off);
1266 m->m_next = n;
1267 }
1268 m = m->m_next;
1269 }
1270 while (len > 0) {
1271 mlen = min (m->m_len - off, len);
1272 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
1273 cp += mlen;
1274 len -= mlen;
1275 mlen += off;
1276 off = 0;
1277 totlen += mlen;
1278 if (len == 0)
1279 break;
1280 if (m->m_next == NULL) {
1281 n = m_get(M_DONTWAIT, m->m_type);
1282 if (n == NULL)
1283 break;
1284 n->m_len = min(MLEN, len);
1285 m->m_next = n;
1286 }
1287 m = m->m_next;
1288 }
1289 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1290 m->m_pkthdr.len = totlen;
1291 }
1292
1293 /*
1294 * Append the specified data to the indicated mbuf chain,
1295 * Extend the mbuf chain if the new data does not fit in
1296 * existing space.
1297 *
1298 * Return 1 if able to complete the job; otherwise 0.
1299 */
1300 int
1301 m_append(struct mbuf *m0, int len, c_caddr_t cp)
1302 {
1303 struct mbuf *m, *n;
1304 int remainder, space;
1305
1306 for (m = m0; m->m_next != NULL; m = m->m_next)
1307 ;
1308 remainder = len;
1309 space = M_TRAILINGSPACE(m);
1310 if (space > 0) {
1311 /*
1312 * Copy into available space.
1313 */
1314 if (space > remainder)
1315 space = remainder;
1316 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1317 m->m_len += space;
1318 cp += space, remainder -= space;
1319 }
1320 while (remainder > 0) {
1321 /*
1322 * Allocate a new mbuf; could check space
1323 * and allocate a cluster instead.
1324 */
1325 n = m_get(M_DONTWAIT, m->m_type);
1326 if (n == NULL)
1327 break;
1328 n->m_len = min(MLEN, remainder);
1329 bcopy(cp, mtod(n, caddr_t), n->m_len);
1330 cp += n->m_len, remainder -= n->m_len;
1331 m->m_next = n;
1332 m = n;
1333 }
1334 if (m0->m_flags & M_PKTHDR)
1335 m0->m_pkthdr.len += len - remainder;
1336 return (remainder == 0);
1337 }
1338
1339 /*
1340 * Apply function f to the data in an mbuf chain starting "off" bytes from
1341 * the beginning, continuing for "len" bytes.
1342 */
1343 int
1344 m_apply(struct mbuf *m, int off, int len,
1345 int (*f)(void *, void *, u_int), void *arg)
1346 {
1347 u_int count;
1348 int rval;
1349
1350 KASSERT(off >= 0, ("m_apply, negative off %d", off));
1351 KASSERT(len >= 0, ("m_apply, negative len %d", len));
1352 while (off > 0) {
1353 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1354 if (off < m->m_len)
1355 break;
1356 off -= m->m_len;
1357 m = m->m_next;
1358 }
1359 while (len > 0) {
1360 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1361 count = min(m->m_len - off, len);
1362 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1363 if (rval)
1364 return (rval);
1365 len -= count;
1366 off = 0;
1367 m = m->m_next;
1368 }
1369 return (0);
1370 }
1371
1372 /*
1373 * Return a pointer to mbuf/offset of location in mbuf chain.
1374 */
1375 struct mbuf *
1376 m_getptr(struct mbuf *m, int loc, int *off)
1377 {
1378
1379 while (loc >= 0) {
1380 /* Normal end of search. */
1381 if (m->m_len > loc) {
1382 *off = loc;
1383 return (m);
1384 } else {
1385 loc -= m->m_len;
1386 if (m->m_next == NULL) {
1387 if (loc == 0) {
1388 /* Point at the end of valid data. */
1389 *off = m->m_len;
1390 return (m);
1391 }
1392 return (NULL);
1393 }
1394 m = m->m_next;
1395 }
1396 }
1397 return (NULL);
1398 }
1399
1400 void
1401 m_print(const struct mbuf *m, int maxlen)
1402 {
1403 int len;
1404 int pdata;
1405 const struct mbuf *m2;
1406
1407 if (m->m_flags & M_PKTHDR)
1408 len = m->m_pkthdr.len;
1409 else
1410 len = -1;
1411 m2 = m;
1412 while (m2 != NULL && (len == -1 || len)) {
1413 pdata = m2->m_len;
1414 if (maxlen != -1 && pdata > maxlen)
1415 pdata = maxlen;
1416 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len,
1417 m2->m_next, m2->m_flags, "\2\20freelist\17skipfw"
1418 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly"
1419 "\3eor\2pkthdr\1ext", pdata ? "" : "\n");
1420 if (pdata)
1421 printf(", %*D\n", pdata, (u_char *)m2->m_data, "-");
1422 if (len != -1)
1423 len -= m2->m_len;
1424 m2 = m2->m_next;
1425 }
1426 if (len > 0)
1427 printf("%d bytes unaccounted for.\n", len);
1428 return;
1429 }
1430
1431 u_int
1432 m_fixhdr(struct mbuf *m0)
1433 {
1434 u_int len;
1435
1436 len = m_length(m0, NULL);
1437 m0->m_pkthdr.len = len;
1438 return (len);
1439 }
1440
1441 u_int
1442 m_length(struct mbuf *m0, struct mbuf **last)
1443 {
1444 struct mbuf *m;
1445 u_int len;
1446
1447 len = 0;
1448 for (m = m0; m != NULL; m = m->m_next) {
1449 len += m->m_len;
1450 if (m->m_next == NULL)
1451 break;
1452 }
1453 if (last != NULL)
1454 *last = m;
1455 return (len);
1456 }
1457
1458 /*
1459 * Defragment a mbuf chain, returning the shortest possible
1460 * chain of mbufs and clusters. If allocation fails and
1461 * this cannot be completed, NULL will be returned, but
1462 * the passed in chain will be unchanged. Upon success,
1463 * the original chain will be freed, and the new chain
1464 * will be returned.
1465 *
1466 * If a non-packet header is passed in, the original
1467 * mbuf (chain?) will be returned unharmed.
1468 */
1469 struct mbuf *
1470 m_defrag(struct mbuf *m0, int how)
1471 {
1472 struct mbuf *m_new = NULL, *m_final = NULL;
1473 int progress = 0, length;
1474
1475 MBUF_CHECKSLEEP(how);
1476 if (!(m0->m_flags & M_PKTHDR))
1477 return (m0);
1478
1479 m_fixhdr(m0); /* Needed sanity check */
1480
1481 #ifdef MBUF_STRESS_TEST
1482 if (m_defragrandomfailures) {
1483 int temp = arc4random() & 0xff;
1484 if (temp == 0xba)
1485 goto nospace;
1486 }
1487 #endif
1488
1489 if (m0->m_pkthdr.len > MHLEN)
1490 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1491 else
1492 m_final = m_gethdr(how, MT_DATA);
1493
1494 if (m_final == NULL)
1495 goto nospace;
1496
1497 if (m_dup_pkthdr(m_final, m0, how) == 0)
1498 goto nospace;
1499
1500 m_new = m_final;
1501
1502 while (progress < m0->m_pkthdr.len) {
1503 length = m0->m_pkthdr.len - progress;
1504 if (length > MCLBYTES)
1505 length = MCLBYTES;
1506
1507 if (m_new == NULL) {
1508 if (length > MLEN)
1509 m_new = m_getcl(how, MT_DATA, 0);
1510 else
1511 m_new = m_get(how, MT_DATA);
1512 if (m_new == NULL)
1513 goto nospace;
1514 }
1515
1516 m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1517 progress += length;
1518 m_new->m_len = length;
1519 if (m_new != m_final)
1520 m_cat(m_final, m_new);
1521 m_new = NULL;
1522 }
1523 #ifdef MBUF_STRESS_TEST
1524 if (m0->m_next == NULL)
1525 m_defraguseless++;
1526 #endif
1527 m_freem(m0);
1528 m0 = m_final;
1529 #ifdef MBUF_STRESS_TEST
1530 m_defragpackets++;
1531 m_defragbytes += m0->m_pkthdr.len;
1532 #endif
1533 return (m0);
1534 nospace:
1535 #ifdef MBUF_STRESS_TEST
1536 m_defragfailure++;
1537 #endif
1538 if (m_final)
1539 m_freem(m_final);
1540 return (NULL);
1541 }
1542
1543 /*
1544 * Defragment an mbuf chain, returning at most maxfrags separate
1545 * mbufs+clusters. If this is not possible NULL is returned and
1546 * the original mbuf chain is left in it's present (potentially
1547 * modified) state. We use two techniques: collapsing consecutive
1548 * mbufs and replacing consecutive mbufs by a cluster.
1549 *
1550 * NB: this should really be named m_defrag but that name is taken
1551 */
1552 struct mbuf *
1553 m_collapse(struct mbuf *m0, int how, int maxfrags)
1554 {
1555 struct mbuf *m, *n, *n2, **prev;
1556 u_int curfrags;
1557
1558 /*
1559 * Calculate the current number of frags.
1560 */
1561 curfrags = 0;
1562 for (m = m0; m != NULL; m = m->m_next)
1563 curfrags++;
1564 /*
1565 * First, try to collapse mbufs. Note that we always collapse
1566 * towards the front so we don't need to deal with moving the
1567 * pkthdr. This may be suboptimal if the first mbuf has much
1568 * less data than the following.
1569 */
1570 m = m0;
1571 again:
1572 for (;;) {
1573 n = m->m_next;
1574 if (n == NULL)
1575 break;
1576 if ((m->m_flags & M_RDONLY) == 0 &&
1577 n->m_len < M_TRAILINGSPACE(m)) {
1578 bcopy(mtod(n, void *), mtod(m, char *) + m->m_len,
1579 n->m_len);
1580 m->m_len += n->m_len;
1581 m->m_next = n->m_next;
1582 m_free(n);
1583 if (--curfrags <= maxfrags)
1584 return m0;
1585 } else
1586 m = n;
1587 }
1588 KASSERT(maxfrags > 1,
1589 ("maxfrags %u, but normal collapse failed", maxfrags));
1590 /*
1591 * Collapse consecutive mbufs to a cluster.
1592 */
1593 prev = &m0->m_next; /* NB: not the first mbuf */
1594 while ((n = *prev) != NULL) {
1595 if ((n2 = n->m_next) != NULL &&
1596 n->m_len + n2->m_len < MCLBYTES) {
1597 m = m_getcl(how, MT_DATA, 0);
1598 if (m == NULL)
1599 goto bad;
1600 bcopy(mtod(n, void *), mtod(m, void *), n->m_len);
1601 bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len,
1602 n2->m_len);
1603 m->m_len = n->m_len + n2->m_len;
1604 m->m_next = n2->m_next;
1605 *prev = m;
1606 m_free(n);
1607 m_free(n2);
1608 if (--curfrags <= maxfrags) /* +1 cl -2 mbufs */
1609 return m0;
1610 /*
1611 * Still not there, try the normal collapse
1612 * again before we allocate another cluster.
1613 */
1614 goto again;
1615 }
1616 prev = &n->m_next;
1617 }
1618 /*
1619 * No place where we can collapse to a cluster; punt.
1620 * This can occur if, for example, you request 2 frags
1621 * but the packet requires that both be clusters (we
1622 * never reallocate the first mbuf to avoid moving the
1623 * packet header).
1624 */
1625 bad:
1626 return NULL;
1627 }
1628
1629 #ifdef MBUF_STRESS_TEST
1630
1631 /*
1632 * Fragment an mbuf chain. There's no reason you'd ever want to do
1633 * this in normal usage, but it's great for stress testing various
1634 * mbuf consumers.
1635 *
1636 * If fragmentation is not possible, the original chain will be
1637 * returned.
1638 *
1639 * Possible length values:
1640 * 0 no fragmentation will occur
1641 * > 0 each fragment will be of the specified length
1642 * -1 each fragment will be the same random value in length
1643 * -2 each fragment's length will be entirely random
1644 * (Random values range from 1 to 256)
1645 */
1646 struct mbuf *
1647 m_fragment(struct mbuf *m0, int how, int length)
1648 {
1649 struct mbuf *m_new = NULL, *m_final = NULL;
1650 int progress = 0;
1651
1652 if (!(m0->m_flags & M_PKTHDR))
1653 return (m0);
1654
1655 if ((length == 0) || (length < -2))
1656 return (m0);
1657
1658 m_fixhdr(m0); /* Needed sanity check */
1659
1660 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1661
1662 if (m_final == NULL)
1663 goto nospace;
1664
1665 if (m_dup_pkthdr(m_final, m0, how) == 0)
1666 goto nospace;
1667
1668 m_new = m_final;
1669
1670 if (length == -1)
1671 length = 1 + (arc4random() & 255);
1672
1673 while (progress < m0->m_pkthdr.len) {
1674 int fraglen;
1675
1676 if (length > 0)
1677 fraglen = length;
1678 else
1679 fraglen = 1 + (arc4random() & 255);
1680 if (fraglen > m0->m_pkthdr.len - progress)
1681 fraglen = m0->m_pkthdr.len - progress;
1682
1683 if (fraglen > MCLBYTES)
1684 fraglen = MCLBYTES;
1685
1686 if (m_new == NULL) {
1687 m_new = m_getcl(how, MT_DATA, 0);
1688 if (m_new == NULL)
1689 goto nospace;
1690 }
1691
1692 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
1693 progress += fraglen;
1694 m_new->m_len = fraglen;
1695 if (m_new != m_final)
1696 m_cat(m_final, m_new);
1697 m_new = NULL;
1698 }
1699 m_freem(m0);
1700 m0 = m_final;
1701 return (m0);
1702 nospace:
1703 if (m_final)
1704 m_freem(m_final);
1705 /* Return the original chain on failure */
1706 return (m0);
1707 }
1708
1709 #endif
1710
1711 /*
1712 * Copy the contents of uio into a properly sized mbuf chain.
1713 */
1714 struct mbuf *
1715 m_uiotombuf(struct uio *uio, int how, int len, int align, int flags)
1716 {
1717 struct mbuf *m, *mb;
1718 int error, length, total;
1719 int progress = 0;
1720
1721 /*
1722 * len can be zero or an arbitrary large value bound by
1723 * the total data supplied by the uio.
1724 */
1725 if (len > 0)
1726 total = min(uio->uio_resid, len);
1727 else
1728 total = uio->uio_resid;
1729
1730 /*
1731 * The smallest unit returned by m_getm2() is a single mbuf
1732 * with pkthdr. We can't align past it.
1733 */
1734 if (align >= MHLEN)
1735 return (NULL);
1736
1737 /*
1738 * Give us the full allocation or nothing.
1739 * If len is zero return the smallest empty mbuf.
1740 */
1741 m = m_getm2(NULL, max(total + align, 1), how, MT_DATA, flags);
1742 if (m == NULL)
1743 return (NULL);
1744 m->m_data += align;
1745
1746 /* Fill all mbufs with uio data and update header information. */
1747 for (mb = m; mb != NULL; mb = mb->m_next) {
1748 length = min(M_TRAILINGSPACE(mb), total - progress);
1749
1750 error = uiomove(mtod(mb, void *), length, uio);
1751 if (error) {
1752 m_freem(m);
1753 return (NULL);
1754 }
1755
1756 mb->m_len = length;
1757 progress += length;
1758 if (flags & M_PKTHDR)
1759 m->m_pkthdr.len += length;
1760 }
1761 KASSERT(progress == total, ("%s: progress != total", __func__));
1762
1763 return (m);
1764 }
1765
1766 /*
1767 * Set the m_data pointer of a newly-allocated mbuf
1768 * to place an object of the specified size at the
1769 * end of the mbuf, longword aligned.
1770 */
1771 void
1772 m_align(struct mbuf *m, int len)
1773 {
1774 int adjust;
1775
1776 if (m->m_flags & M_EXT)
1777 adjust = m->m_ext.ext_size - len;
1778 else if (m->m_flags & M_PKTHDR)
1779 adjust = MHLEN - len;
1780 else
1781 adjust = MLEN - len;
1782 m->m_data += adjust &~ (sizeof(long)-1);
1783 }
1784
1785 /*
1786 * Create a writable copy of the mbuf chain. While doing this
1787 * we compact the chain with a goal of producing a chain with
1788 * at most two mbufs. The second mbuf in this chain is likely
1789 * to be a cluster. The primary purpose of this work is to create
1790 * a writable packet for encryption, compression, etc. The
1791 * secondary goal is to linearize the data so the data can be
1792 * passed to crypto hardware in the most efficient manner possible.
1793 */
1794 struct mbuf *
1795 m_unshare(struct mbuf *m0, int how)
1796 {
1797 struct mbuf *m, *mprev;
1798 struct mbuf *n, *mfirst, *mlast;
1799 int len, off;
1800
1801 mprev = NULL;
1802 for (m = m0; m != NULL; m = mprev->m_next) {
1803 /*
1804 * Regular mbufs are ignored unless there's a cluster
1805 * in front of it that we can use to coalesce. We do
1806 * the latter mainly so later clusters can be coalesced
1807 * also w/o having to handle them specially (i.e. convert
1808 * mbuf+cluster -> cluster). This optimization is heavily
1809 * influenced by the assumption that we're running over
1810 * Ethernet where MCLBYTES is large enough that the max
1811 * packet size will permit lots of coalescing into a
1812 * single cluster. This in turn permits efficient
1813 * crypto operations, especially when using hardware.
1814 */
1815 if ((m->m_flags & M_EXT) == 0) {
1816 if (mprev && (mprev->m_flags & M_EXT) &&
1817 m->m_len <= M_TRAILINGSPACE(mprev)) {
1818 /* XXX: this ignores mbuf types */
1819 memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1820 mtod(m, caddr_t), m->m_len);
1821 mprev->m_len += m->m_len;
1822 mprev->m_next = m->m_next; /* unlink from chain */
1823 m_free(m); /* reclaim mbuf */
1824 #if 0
1825 newipsecstat.ips_mbcoalesced++;
1826 #endif
1827 } else {
1828 mprev = m;
1829 }
1830 continue;
1831 }
1832 /*
1833 * Writable mbufs are left alone (for now).
1834 */
1835 if (M_WRITABLE(m)) {
1836 mprev = m;
1837 continue;
1838 }
1839
1840 /*
1841 * Not writable, replace with a copy or coalesce with
1842 * the previous mbuf if possible (since we have to copy
1843 * it anyway, we try to reduce the number of mbufs and
1844 * clusters so that future work is easier).
1845 */
1846 KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags));
1847 /* NB: we only coalesce into a cluster or larger */
1848 if (mprev != NULL && (mprev->m_flags & M_EXT) &&
1849 m->m_len <= M_TRAILINGSPACE(mprev)) {
1850 /* XXX: this ignores mbuf types */
1851 memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1852 mtod(m, caddr_t), m->m_len);
1853 mprev->m_len += m->m_len;
1854 mprev->m_next = m->m_next; /* unlink from chain */
1855 m_free(m); /* reclaim mbuf */
1856 #if 0
1857 newipsecstat.ips_clcoalesced++;
1858 #endif
1859 continue;
1860 }
1861
1862 /*
1863 * Allocate new space to hold the copy...
1864 */
1865 /* XXX why can M_PKTHDR be set past the first mbuf? */
1866 if (mprev == NULL && (m->m_flags & M_PKTHDR)) {
1867 /*
1868 * NB: if a packet header is present we must
1869 * allocate the mbuf separately from any cluster
1870 * because M_MOVE_PKTHDR will smash the data
1871 * pointer and drop the M_EXT marker.
1872 */
1873 MGETHDR(n, how, m->m_type);
1874 if (n == NULL) {
1875 m_freem(m0);
1876 return (NULL);
1877 }
1878 M_MOVE_PKTHDR(n, m);
1879 MCLGET(n, how);
1880 if ((n->m_flags & M_EXT) == 0) {
1881 m_free(n);
1882 m_freem(m0);
1883 return (NULL);
1884 }
1885 } else {
1886 n = m_getcl(how, m->m_type, m->m_flags);
1887 if (n == NULL) {
1888 m_freem(m0);
1889 return (NULL);
1890 }
1891 }
1892 /*
1893 * ... and copy the data. We deal with jumbo mbufs
1894 * (i.e. m_len > MCLBYTES) by splitting them into
1895 * clusters. We could just malloc a buffer and make
1896 * it external but too many device drivers don't know
1897 * how to break up the non-contiguous memory when
1898 * doing DMA.
1899 */
1900 len = m->m_len;
1901 off = 0;
1902 mfirst = n;
1903 mlast = NULL;
1904 for (;;) {
1905 int cc = min(len, MCLBYTES);
1906 memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc);
1907 n->m_len = cc;
1908 if (mlast != NULL)
1909 mlast->m_next = n;
1910 mlast = n;
1911 #if 0
1912 newipsecstat.ips_clcopied++;
1913 #endif
1914
1915 len -= cc;
1916 if (len <= 0)
1917 break;
1918 off += cc;
1919
1920 n = m_getcl(how, m->m_type, m->m_flags);
1921 if (n == NULL) {
1922 m_freem(mfirst);
1923 m_freem(m0);
1924 return (NULL);
1925 }
1926 }
1927 n->m_next = m->m_next;
1928 if (mprev == NULL)
1929 m0 = mfirst; /* new head of chain */
1930 else
1931 mprev->m_next = mfirst; /* replace old mbuf */
1932 m_free(m); /* release old mbuf */
1933 mprev = mfirst;
1934 }
1935 return (m0);
1936 }
Cache object: 54faba59df387954a364b96e12b9cc4e
|