]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/uipc_mbuf.c
This commit was generated by cvs2svn to compensate for changes in r143731,
[FreeBSD/FreeBSD.git] / 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$");
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/mac.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/sysctl.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/uio.h>
51
52 int     max_linkhdr;
53 int     max_protohdr;
54 int     max_hdr;
55 int     max_datalen;
56 #ifdef MBUF_STRESS_TEST
57 int     m_defragpackets;
58 int     m_defragbytes;
59 int     m_defraguseless;
60 int     m_defragfailure;
61 int     m_defragrandomfailures;
62 #endif
63
64 /*
65  * sysctl(8) exported objects
66  */
67 SYSCTL_DECL(_kern_ipc);
68 SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
69            &max_linkhdr, 0, "");
70 SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
71            &max_protohdr, 0, "");
72 SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
73 SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
74            &max_datalen, 0, "");
75 #ifdef MBUF_STRESS_TEST
76 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
77            &m_defragpackets, 0, "");
78 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
79            &m_defragbytes, 0, "");
80 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
81            &m_defraguseless, 0, "");
82 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
83            &m_defragfailure, 0, "");
84 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
85            &m_defragrandomfailures, 0, "");
86 #endif
87
88 /*
89  * Malloc-type for external ext_buf ref counts.
90  */
91 static MALLOC_DEFINE(M_MBUF, "mbextcnt", "mbuf external ref counts");
92
93 /*
94  * Allocate a given length worth of mbufs and/or clusters (whatever fits
95  * best) and return a pointer to the top of the allocated chain.  If an
96  * existing mbuf chain is provided, then we will append the new chain
97  * to the existing one but still return the top of the newly allocated
98  * chain.
99  */
100 struct mbuf *
101 m_getm(struct mbuf *m, int len, int how, short type)
102 {
103         struct mbuf *mb, *top, *cur, *mtail;
104         int num, rem;
105         int i;
106
107         KASSERT(len >= 0, ("m_getm(): len is < 0"));
108
109         /* If m != NULL, we will append to the end of that chain. */
110         if (m != NULL)
111                 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
112         else
113                 mtail = NULL;
114
115         /*
116          * Calculate how many mbufs+clusters ("packets") we need and how much
117          * leftover there is after that and allocate the first mbuf+cluster
118          * if required.
119          */
120         num = len / MCLBYTES;
121         rem = len % MCLBYTES;
122         top = cur = NULL;
123         if (num > 0) {
124                 if ((top = cur = m_getcl(how, type, 0)) == NULL)
125                         goto failed;
126                 top->m_len = 0;
127         }
128         num--;
129
130         for (i = 0; i < num; i++) {
131                 mb = m_getcl(how, type, 0);
132                 if (mb == NULL)
133                         goto failed;
134                 mb->m_len = 0;
135                 cur = (cur->m_next = mb);
136         }
137         if (rem > 0) {
138                 mb = (rem > MINCLSIZE) ?
139                     m_getcl(how, type, 0) : m_get(how, type);
140                 if (mb == NULL)
141                         goto failed;
142                 mb->m_len = 0;
143                 if (cur == NULL)
144                         top = mb;
145                 else
146                         cur->m_next = mb;
147         }
148
149         if (mtail != NULL)
150                 mtail->m_next = top;
151         return top;
152 failed:
153         if (top != NULL)
154                 m_freem(top);
155         return NULL;
156 }
157
158 /*
159  * Free an entire chain of mbufs and associated external buffers, if
160  * applicable.
161  */
162 void
163 m_freem(struct mbuf *mb)
164 {
165
166         while (mb != NULL)
167                 mb = m_free(mb);
168 }
169
170 /*-
171  * Configure a provided mbuf to refer to the provided external storage
172  * buffer and setup a reference count for said buffer.  If the setting
173  * up of the reference count fails, the M_EXT bit will not be set.  If
174  * successfull, the M_EXT bit is set in the mbuf's flags.
175  *
176  * Arguments:
177  *    mb     The existing mbuf to which to attach the provided buffer.
178  *    buf    The address of the provided external storage buffer.
179  *    size   The size of the provided buffer.
180  *    freef  A pointer to a routine that is responsible for freeing the
181  *           provided external storage buffer.
182  *    args   A pointer to an argument structure (of any type) to be passed
183  *           to the provided freef routine (may be NULL).
184  *    flags  Any other flags to be passed to the provided mbuf.
185  *    type   The type that the external storage buffer should be
186  *           labeled with.
187  *
188  * Returns:
189  *    Nothing.
190  */
191 void
192 m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
193     void (*freef)(void *, void *), void *args, int flags, int type)
194 {
195         u_int *ref_cnt = NULL;
196
197         /* XXX Shouldn't be adding EXT_CLUSTER with this API */
198         if (type == EXT_CLUSTER)
199                 ref_cnt = (u_int *)uma_find_refcnt(zone_clust,
200                     mb->m_ext.ext_buf);
201         else if (type == EXT_EXTREF)
202                 ref_cnt = __DEVOLATILE(u_int *, mb->m_ext.ref_cnt);
203         mb->m_ext.ref_cnt = (ref_cnt == NULL) ?
204             malloc(sizeof(u_int), M_MBUF, M_NOWAIT) : (u_int *)ref_cnt;
205         if (mb->m_ext.ref_cnt != NULL) {
206                 *(mb->m_ext.ref_cnt) = 1;
207                 mb->m_flags |= (M_EXT | flags);
208                 mb->m_ext.ext_buf = buf;
209                 mb->m_data = mb->m_ext.ext_buf;
210                 mb->m_ext.ext_size = size;
211                 mb->m_ext.ext_free = freef;
212                 mb->m_ext.ext_args = args;
213                 mb->m_ext.ext_type = type;
214         }
215 }
216
217 /*
218  * Non-directly-exported function to clean up after mbufs with M_EXT
219  * storage attached to them if the reference count hits 0.
220  */
221 void
222 mb_free_ext(struct mbuf *m)
223 {
224         u_int cnt;
225         int dofree;
226
227         /* Account for lazy ref count assign. */
228         if (m->m_ext.ref_cnt == NULL)
229                 dofree = 1;
230         else
231                 dofree = 0;
232
233         /*
234          * This is tricky.  We need to make sure to decrement the
235          * refcount in a safe way but to also clean up if we're the
236          * last reference.  This method seems to do it without race.
237          */
238         while (dofree == 0) {
239                 cnt = *(m->m_ext.ref_cnt);
240                 if (atomic_cmpset_int(m->m_ext.ref_cnt, cnt, cnt - 1)) {
241                         if (cnt == 1)
242                                 dofree = 1;
243                         break;
244                 }
245         }
246
247         if (dofree) {
248                 /*
249                  * Do the free, should be safe.
250                  */
251                 if (m->m_ext.ext_type == EXT_PACKET) {
252                         uma_zfree(zone_pack, m);
253                         return;
254                 } else if (m->m_ext.ext_type == EXT_CLUSTER) {
255                         uma_zfree(zone_clust, m->m_ext.ext_buf);
256                         m->m_ext.ext_buf = NULL;
257                 } else {
258                         (*(m->m_ext.ext_free))(m->m_ext.ext_buf,
259                             m->m_ext.ext_args);
260                         if (m->m_ext.ext_type != EXT_EXTREF) {
261                                 if (m->m_ext.ref_cnt != NULL)
262                                         free(__DEVOLATILE(u_int *,
263                                             m->m_ext.ref_cnt), M_MBUF);
264                                 m->m_ext.ref_cnt = NULL;
265                         }
266                         m->m_ext.ext_buf = NULL;
267                 }
268         }
269         uma_zfree(zone_mbuf, m);
270 }
271
272 /*
273  * "Move" mbuf pkthdr from "from" to "to".
274  * "from" must have M_PKTHDR set, and "to" must be empty.
275  */
276 void
277 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
278 {
279
280 #if 0
281         /* see below for why these are not enabled */
282         M_ASSERTPKTHDR(to);
283         /* Note: with MAC, this may not be a good assertion. */
284         KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
285             ("m_move_pkthdr: to has tags"));
286 #endif
287 #ifdef MAC
288         /*
289          * XXXMAC: It could be this should also occur for non-MAC?
290          */
291         if (to->m_flags & M_PKTHDR)
292                 m_tag_delete_chain(to, NULL);
293 #endif
294         to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
295         if ((to->m_flags & M_EXT) == 0)
296                 to->m_data = to->m_pktdat;
297         to->m_pkthdr = from->m_pkthdr;          /* especially tags */
298         SLIST_INIT(&from->m_pkthdr.tags);       /* purge tags from src */
299         from->m_flags &= ~M_PKTHDR;
300 }
301
302 /*
303  * Duplicate "from"'s mbuf pkthdr in "to".
304  * "from" must have M_PKTHDR set, and "to" must be empty.
305  * In particular, this does a deep copy of the packet tags.
306  */
307 int
308 m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
309 {
310
311 #if 0
312         /*
313          * The mbuf allocator only initializes the pkthdr
314          * when the mbuf is allocated with MGETHDR. Many users
315          * (e.g. m_copy*, m_prepend) use MGET and then
316          * smash the pkthdr as needed causing these
317          * assertions to trip.  For now just disable them.
318          */
319         M_ASSERTPKTHDR(to);
320         /* Note: with MAC, this may not be a good assertion. */
321         KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
322 #endif
323         MBUF_CHECKSLEEP(how);
324 #ifdef MAC
325         if (to->m_flags & M_PKTHDR)
326                 m_tag_delete_chain(to, NULL);
327 #endif
328         to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
329         if ((to->m_flags & M_EXT) == 0)
330                 to->m_data = to->m_pktdat;
331         to->m_pkthdr = from->m_pkthdr;
332         SLIST_INIT(&to->m_pkthdr.tags);
333         return (m_tag_copy_chain(to, from, MBTOM(how)));
334 }
335
336 /*
337  * Lesser-used path for M_PREPEND:
338  * allocate new mbuf to prepend to chain,
339  * copy junk along.
340  */
341 struct mbuf *
342 m_prepend(struct mbuf *m, int len, int how)
343 {
344         struct mbuf *mn;
345
346         if (m->m_flags & M_PKTHDR)
347                 MGETHDR(mn, how, m->m_type);
348         else
349                 MGET(mn, how, m->m_type);
350         if (mn == NULL) {
351                 m_freem(m);
352                 return (NULL);
353         }
354         if (m->m_flags & M_PKTHDR)
355                 M_MOVE_PKTHDR(mn, m);
356         mn->m_next = m;
357         m = mn;
358         if (len < MHLEN)
359                 MH_ALIGN(m, len);
360         m->m_len = len;
361         return (m);
362 }
363
364 /*
365  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
366  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
367  * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
368  * Note that the copy is read-only, because clusters are not copied,
369  * only their reference counts are incremented.
370  */
371 struct mbuf *
372 m_copym(struct mbuf *m, int off0, int len, int wait)
373 {
374         struct mbuf *n, **np;
375         int off = off0;
376         struct mbuf *top;
377         int copyhdr = 0;
378
379         KASSERT(off >= 0, ("m_copym, negative off %d", off));
380         KASSERT(len >= 0, ("m_copym, negative len %d", len));
381         MBUF_CHECKSLEEP(wait);
382         if (off == 0 && m->m_flags & M_PKTHDR)
383                 copyhdr = 1;
384         while (off > 0) {
385                 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
386                 if (off < m->m_len)
387                         break;
388                 off -= m->m_len;
389                 m = m->m_next;
390         }
391         np = &top;
392         top = 0;
393         while (len > 0) {
394                 if (m == NULL) {
395                         KASSERT(len == M_COPYALL, 
396                             ("m_copym, length > size of mbuf chain"));
397                         break;
398                 }
399                 if (copyhdr)
400                         MGETHDR(n, wait, m->m_type);
401                 else
402                         MGET(n, wait, m->m_type);
403                 *np = n;
404                 if (n == NULL)
405                         goto nospace;
406                 if (copyhdr) {
407                         if (!m_dup_pkthdr(n, m, wait))
408                                 goto nospace;
409                         if (len == M_COPYALL)
410                                 n->m_pkthdr.len -= off0;
411                         else
412                                 n->m_pkthdr.len = len;
413                         copyhdr = 0;
414                 }
415                 n->m_len = min(len, m->m_len - off);
416                 if (m->m_flags & M_EXT) {
417                         n->m_data = m->m_data + off;
418                         n->m_ext = m->m_ext;
419                         n->m_flags |= M_EXT;
420                         MEXT_ADD_REF(m);
421                         n->m_ext.ref_cnt = m->m_ext.ref_cnt;
422                 } else
423                         bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
424                             (u_int)n->m_len);
425                 if (len != M_COPYALL)
426                         len -= n->m_len;
427                 off = 0;
428                 m = m->m_next;
429                 np = &n->m_next;
430         }
431         if (top == NULL)
432                 mbstat.m_mcfail++;      /* XXX: No consistency. */
433
434         return (top);
435 nospace:
436         m_freem(top);
437         mbstat.m_mcfail++;      /* XXX: No consistency. */
438         return (NULL);
439 }
440
441 /*
442  * Copy an entire packet, including header (which must be present).
443  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
444  * Note that the copy is read-only, because clusters are not copied,
445  * only their reference counts are incremented.
446  * Preserve alignment of the first mbuf so if the creator has left
447  * some room at the beginning (e.g. for inserting protocol headers)
448  * the copies still have the room available.
449  */
450 struct mbuf *
451 m_copypacket(struct mbuf *m, int how)
452 {
453         struct mbuf *top, *n, *o;
454
455         MBUF_CHECKSLEEP(how);
456         MGET(n, how, m->m_type);
457         top = n;
458         if (n == NULL)
459                 goto nospace;
460
461         if (!m_dup_pkthdr(n, m, how))
462                 goto nospace;
463         n->m_len = m->m_len;
464         if (m->m_flags & M_EXT) {
465                 n->m_data = m->m_data;
466                 n->m_ext = m->m_ext;
467                 n->m_flags |= M_EXT;
468                 MEXT_ADD_REF(m);
469                 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
470         } else {
471                 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
472                 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
473         }
474
475         m = m->m_next;
476         while (m) {
477                 MGET(o, how, m->m_type);
478                 if (o == NULL)
479                         goto nospace;
480
481                 n->m_next = o;
482                 n = n->m_next;
483
484                 n->m_len = m->m_len;
485                 if (m->m_flags & M_EXT) {
486                         n->m_data = m->m_data;
487                         n->m_ext = m->m_ext;
488                         n->m_flags |= M_EXT;
489                         MEXT_ADD_REF(m);
490                         n->m_ext.ref_cnt = m->m_ext.ref_cnt;
491                 } else {
492                         bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
493                 }
494
495                 m = m->m_next;
496         }
497         return top;
498 nospace:
499         m_freem(top);
500         mbstat.m_mcfail++;      /* XXX: No consistency. */ 
501         return (NULL);
502 }
503
504 /*
505  * Copy data from an mbuf chain starting "off" bytes from the beginning,
506  * continuing for "len" bytes, into the indicated buffer.
507  */
508 void
509 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
510 {
511         u_int count;
512
513         KASSERT(off >= 0, ("m_copydata, negative off %d", off));
514         KASSERT(len >= 0, ("m_copydata, negative len %d", len));
515         while (off > 0) {
516                 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
517                 if (off < m->m_len)
518                         break;
519                 off -= m->m_len;
520                 m = m->m_next;
521         }
522         while (len > 0) {
523                 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
524                 count = min(m->m_len - off, len);
525                 bcopy(mtod(m, caddr_t) + off, cp, count);
526                 len -= count;
527                 cp += count;
528                 off = 0;
529                 m = m->m_next;
530         }
531 }
532
533 /*
534  * Copy a packet header mbuf chain into a completely new chain, including
535  * copying any mbuf clusters.  Use this instead of m_copypacket() when
536  * you need a writable copy of an mbuf chain.
537  */
538 struct mbuf *
539 m_dup(struct mbuf *m, int how)
540 {
541         struct mbuf **p, *top = NULL;
542         int remain, moff, nsize;
543
544         MBUF_CHECKSLEEP(how);
545         /* Sanity check */
546         if (m == NULL)
547                 return (NULL);
548         M_ASSERTPKTHDR(m);
549
550         /* While there's more data, get a new mbuf, tack it on, and fill it */
551         remain = m->m_pkthdr.len;
552         moff = 0;
553         p = &top;
554         while (remain > 0 || top == NULL) {     /* allow m->m_pkthdr.len == 0 */
555                 struct mbuf *n;
556
557                 /* Get the next new mbuf */
558                 if (remain >= MINCLSIZE) {
559                         n = m_getcl(how, m->m_type, 0);
560                         nsize = MCLBYTES;
561                 } else {
562                         n = m_get(how, m->m_type);
563                         nsize = MLEN;
564                 }
565                 if (n == NULL)
566                         goto nospace;
567
568                 if (top == NULL) {              /* First one, must be PKTHDR */
569                         if (!m_dup_pkthdr(n, m, how)) {
570                                 m_free(n);
571                                 goto nospace;
572                         }
573                         nsize = MHLEN;
574                 }
575                 n->m_len = 0;
576
577                 /* Link it into the new chain */
578                 *p = n;
579                 p = &n->m_next;
580
581                 /* Copy data from original mbuf(s) into new mbuf */
582                 while (n->m_len < nsize && m != NULL) {
583                         int chunk = min(nsize - n->m_len, m->m_len - moff);
584
585                         bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
586                         moff += chunk;
587                         n->m_len += chunk;
588                         remain -= chunk;
589                         if (moff == m->m_len) {
590                                 m = m->m_next;
591                                 moff = 0;
592                         }
593                 }
594
595                 /* Check correct total mbuf length */
596                 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
597                         ("%s: bogus m_pkthdr.len", __func__));
598         }
599         return (top);
600
601 nospace:
602         m_freem(top);
603         mbstat.m_mcfail++;      /* XXX: No consistency. */
604         return (NULL);
605 }
606
607 /*
608  * Concatenate mbuf chain n to m.
609  * Both chains must be of the same type (e.g. MT_DATA).
610  * Any m_pkthdr is not updated.
611  */
612 void
613 m_cat(struct mbuf *m, struct mbuf *n)
614 {
615         while (m->m_next)
616                 m = m->m_next;
617         while (n) {
618                 if (m->m_flags & M_EXT ||
619                     m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
620                         /* just join the two chains */
621                         m->m_next = n;
622                         return;
623                 }
624                 /* splat the data from one into the other */
625                 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
626                     (u_int)n->m_len);
627                 m->m_len += n->m_len;
628                 n = m_free(n);
629         }
630 }
631
632 void
633 m_adj(struct mbuf *mp, int req_len)
634 {
635         int len = req_len;
636         struct mbuf *m;
637         int count;
638
639         if ((m = mp) == NULL)
640                 return;
641         if (len >= 0) {
642                 /*
643                  * Trim from head.
644                  */
645                 while (m != NULL && len > 0) {
646                         if (m->m_len <= len) {
647                                 len -= m->m_len;
648                                 m->m_len = 0;
649                                 m = m->m_next;
650                         } else {
651                                 m->m_len -= len;
652                                 m->m_data += len;
653                                 len = 0;
654                         }
655                 }
656                 m = mp;
657                 if (mp->m_flags & M_PKTHDR)
658                         m->m_pkthdr.len -= (req_len - len);
659         } else {
660                 /*
661                  * Trim from tail.  Scan the mbuf chain,
662                  * calculating its length and finding the last mbuf.
663                  * If the adjustment only affects this mbuf, then just
664                  * adjust and return.  Otherwise, rescan and truncate
665                  * after the remaining size.
666                  */
667                 len = -len;
668                 count = 0;
669                 for (;;) {
670                         count += m->m_len;
671                         if (m->m_next == (struct mbuf *)0)
672                                 break;
673                         m = m->m_next;
674                 }
675                 if (m->m_len >= len) {
676                         m->m_len -= len;
677                         if (mp->m_flags & M_PKTHDR)
678                                 mp->m_pkthdr.len -= len;
679                         return;
680                 }
681                 count -= len;
682                 if (count < 0)
683                         count = 0;
684                 /*
685                  * Correct length for chain is "count".
686                  * Find the mbuf with last data, adjust its length,
687                  * and toss data from remaining mbufs on chain.
688                  */
689                 m = mp;
690                 if (m->m_flags & M_PKTHDR)
691                         m->m_pkthdr.len = count;
692                 for (; m; m = m->m_next) {
693                         if (m->m_len >= count) {
694                                 m->m_len = count;
695                                 if (m->m_next != NULL) {
696                                         m_freem(m->m_next);
697                                         m->m_next = NULL;
698                                 }
699                                 break;
700                         }
701                         count -= m->m_len;
702                 }
703         }
704 }
705
706 /*
707  * Rearange an mbuf chain so that len bytes are contiguous
708  * and in the data area of an mbuf (so that mtod and dtom
709  * will work for a structure of size len).  Returns the resulting
710  * mbuf chain on success, frees it and returns null on failure.
711  * If there is room, it will add up to max_protohdr-len extra bytes to the
712  * contiguous region in an attempt to avoid being called next time.
713  */
714 struct mbuf *
715 m_pullup(struct mbuf *n, int len)
716 {
717         struct mbuf *m;
718         int count;
719         int space;
720
721         /*
722          * If first mbuf has no cluster, and has room for len bytes
723          * without shifting current data, pullup into it,
724          * otherwise allocate a new mbuf to prepend to the chain.
725          */
726         if ((n->m_flags & M_EXT) == 0 &&
727             n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
728                 if (n->m_len >= len)
729                         return (n);
730                 m = n;
731                 n = n->m_next;
732                 len -= m->m_len;
733         } else {
734                 if (len > MHLEN)
735                         goto bad;
736                 MGET(m, M_DONTWAIT, n->m_type);
737                 if (m == NULL)
738                         goto bad;
739                 m->m_len = 0;
740                 if (n->m_flags & M_PKTHDR)
741                         M_MOVE_PKTHDR(m, n);
742         }
743         space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
744         do {
745                 count = min(min(max(len, max_protohdr), space), n->m_len);
746                 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
747                   (u_int)count);
748                 len -= count;
749                 m->m_len += count;
750                 n->m_len -= count;
751                 space -= count;
752                 if (n->m_len)
753                         n->m_data += count;
754                 else
755                         n = m_free(n);
756         } while (len > 0 && n);
757         if (len > 0) {
758                 (void) m_free(m);
759                 goto bad;
760         }
761         m->m_next = n;
762         return (m);
763 bad:
764         m_freem(n);
765         mbstat.m_mpfail++;      /* XXX: No consistency. */
766         return (NULL);
767 }
768
769 /*
770  * Partition an mbuf chain in two pieces, returning the tail --
771  * all but the first len0 bytes.  In case of failure, it returns NULL and
772  * attempts to restore the chain to its original state.
773  *
774  * Note that the resulting mbufs might be read-only, because the new
775  * mbuf can end up sharing an mbuf cluster with the original mbuf if
776  * the "breaking point" happens to lie within a cluster mbuf. Use the
777  * M_WRITABLE() macro to check for this case.
778  */
779 struct mbuf *
780 m_split(struct mbuf *m0, int len0, int wait)
781 {
782         struct mbuf *m, *n;
783         u_int len = len0, remain;
784
785         MBUF_CHECKSLEEP(wait);
786         for (m = m0; m && len > m->m_len; m = m->m_next)
787                 len -= m->m_len;
788         if (m == NULL)
789                 return (NULL);
790         remain = m->m_len - len;
791         if (m0->m_flags & M_PKTHDR) {
792                 MGETHDR(n, wait, m0->m_type);
793                 if (n == NULL)
794                         return (NULL);
795                 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
796                 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
797                 m0->m_pkthdr.len = len0;
798                 if (m->m_flags & M_EXT)
799                         goto extpacket;
800                 if (remain > MHLEN) {
801                         /* m can't be the lead packet */
802                         MH_ALIGN(n, 0);
803                         n->m_next = m_split(m, len, wait);
804                         if (n->m_next == NULL) {
805                                 (void) m_free(n);
806                                 return (NULL);
807                         } else {
808                                 n->m_len = 0;
809                                 return (n);
810                         }
811                 } else
812                         MH_ALIGN(n, remain);
813         } else if (remain == 0) {
814                 n = m->m_next;
815                 m->m_next = NULL;
816                 return (n);
817         } else {
818                 MGET(n, wait, m->m_type);
819                 if (n == NULL)
820                         return (NULL);
821                 M_ALIGN(n, remain);
822         }
823 extpacket:
824         if (m->m_flags & M_EXT) {
825                 n->m_flags |= M_EXT;
826                 n->m_ext = m->m_ext;
827                 MEXT_ADD_REF(m);
828                 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
829                 n->m_data = m->m_data + len;
830         } else {
831                 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
832         }
833         n->m_len = remain;
834         m->m_len = len;
835         n->m_next = m->m_next;
836         m->m_next = NULL;
837         return (n);
838 }
839 /*
840  * Routine to copy from device local memory into mbufs.
841  * Note that `off' argument is offset into first mbuf of target chain from
842  * which to begin copying the data to.
843  */
844 struct mbuf *
845 m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
846          void (*copy)(char *from, caddr_t to, u_int len))
847 {
848         struct mbuf *m;
849         struct mbuf *top = NULL, **mp = &top;
850         int len;
851
852         if (off < 0 || off > MHLEN)
853                 return (NULL);
854
855         while (totlen > 0) {
856                 if (top == NULL) {      /* First one, must be PKTHDR */
857                         if (totlen + off >= MINCLSIZE) {
858                                 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
859                                 len = MCLBYTES;
860                         } else {
861                                 m = m_gethdr(M_DONTWAIT, MT_DATA);
862                                 len = MHLEN;
863
864                                 /* Place initial small packet/header at end of mbuf */
865                                 if (m && totlen + off + max_linkhdr <= MLEN) {
866                                         m->m_data += max_linkhdr;
867                                         len -= max_linkhdr;
868                                 }
869                         }
870                         if (m == NULL)
871                                 return NULL;
872                         m->m_pkthdr.rcvif = ifp;
873                         m->m_pkthdr.len = totlen;
874                 } else {
875                         if (totlen + off >= MINCLSIZE) {
876                                 m = m_getcl(M_DONTWAIT, MT_DATA, 0);
877                                 len = MCLBYTES;
878                         } else {
879                                 m = m_get(M_DONTWAIT, MT_DATA);
880                                 len = MLEN;
881                         }
882                         if (m == NULL) {
883                                 m_freem(top);
884                                 return NULL;
885                         }
886                 }
887                 if (off) {
888                         m->m_data += off;
889                         len -= off;
890                         off = 0;
891                 }
892                 m->m_len = len = min(totlen, len);
893                 if (copy)
894                         copy(buf, mtod(m, caddr_t), (u_int)len);
895                 else
896                         bcopy(buf, mtod(m, caddr_t), (u_int)len);
897                 buf += len;
898                 *mp = m;
899                 mp = &m->m_next;
900                 totlen -= len;
901         }
902         return (top);
903 }
904
905 /*
906  * Copy data from a buffer back into the indicated mbuf chain,
907  * starting "off" bytes from the beginning, extending the mbuf
908  * chain if necessary.
909  */
910 void
911 m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
912 {
913         int mlen;
914         struct mbuf *m = m0, *n;
915         int totlen = 0;
916
917         if (m0 == NULL)
918                 return;
919         while (off > (mlen = m->m_len)) {
920                 off -= mlen;
921                 totlen += mlen;
922                 if (m->m_next == NULL) {
923                         n = m_get(M_DONTWAIT, m->m_type);
924                         if (n == NULL)
925                                 goto out;
926                         bzero(mtod(n, caddr_t), MLEN);
927                         n->m_len = min(MLEN, len + off);
928                         m->m_next = n;
929                 }
930                 m = m->m_next;
931         }
932         while (len > 0) {
933                 mlen = min (m->m_len - off, len);
934                 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
935                 cp += mlen;
936                 len -= mlen;
937                 mlen += off;
938                 off = 0;
939                 totlen += mlen;
940                 if (len == 0)
941                         break;
942                 if (m->m_next == NULL) {
943                         n = m_get(M_DONTWAIT, m->m_type);
944                         if (n == NULL)
945                                 break;
946                         n->m_len = min(MLEN, len);
947                         m->m_next = n;
948                 }
949                 m = m->m_next;
950         }
951 out:    if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
952                 m->m_pkthdr.len = totlen;
953 }
954
955 /*
956  * Append the specified data to the indicated mbuf chain,
957  * Extend the mbuf chain if the new data does not fit in
958  * existing space.
959  *
960  * Return 1 if able to complete the job; otherwise 0.
961  */
962 int
963 m_append(struct mbuf *m0, int len, c_caddr_t cp)
964 {
965         struct mbuf *m, *n;
966         int remainder, space;
967
968         for (m = m0; m->m_next != NULL; m = m->m_next)
969                 ;
970         remainder = len;
971         space = M_TRAILINGSPACE(m);
972         if (space > 0) {
973                 /*
974                  * Copy into available space.
975                  */
976                 if (space > remainder)
977                         space = remainder;
978                 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
979                 m->m_len += space;
980                 cp += space, remainder -= space;
981         }
982         while (remainder > 0) {
983                 /*
984                  * Allocate a new mbuf; could check space
985                  * and allocate a cluster instead.
986                  */
987                 n = m_get(M_DONTWAIT, m->m_type);
988                 if (n == NULL)
989                         break;
990                 n->m_len = min(MLEN, remainder);
991                 bcopy(cp, mtod(n, caddr_t), n->m_len);
992                 cp += n->m_len, remainder -= n->m_len;
993                 m->m_next = n;
994                 m = n;
995         }
996         if (m0->m_flags & M_PKTHDR)
997                 m0->m_pkthdr.len += len - remainder;
998         return (remainder == 0);
999 }
1000
1001 /*
1002  * Apply function f to the data in an mbuf chain starting "off" bytes from
1003  * the beginning, continuing for "len" bytes.
1004  */
1005 int
1006 m_apply(struct mbuf *m, int off, int len,
1007     int (*f)(void *, void *, u_int), void *arg)
1008 {
1009         u_int count;
1010         int rval;
1011
1012         KASSERT(off >= 0, ("m_apply, negative off %d", off));
1013         KASSERT(len >= 0, ("m_apply, negative len %d", len));
1014         while (off > 0) {
1015                 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1016                 if (off < m->m_len)
1017                         break;
1018                 off -= m->m_len;
1019                 m = m->m_next;
1020         }
1021         while (len > 0) {
1022                 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1023                 count = min(m->m_len - off, len);
1024                 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1025                 if (rval)
1026                         return (rval);
1027                 len -= count;
1028                 off = 0;
1029                 m = m->m_next;
1030         }
1031         return (0);
1032 }
1033
1034 /*
1035  * Return a pointer to mbuf/offset of location in mbuf chain.
1036  */
1037 struct mbuf *
1038 m_getptr(struct mbuf *m, int loc, int *off)
1039 {
1040
1041         while (loc >= 0) {
1042                 /* Normal end of search. */
1043                 if (m->m_len > loc) {
1044                         *off = loc;
1045                         return (m);
1046                 } else {
1047                         loc -= m->m_len;
1048                         if (m->m_next == NULL) {
1049                                 if (loc == 0) {
1050                                         /* Point at the end of valid data. */
1051                                         *off = m->m_len;
1052                                         return (m);
1053                                 }
1054                                 return (NULL);
1055                         }
1056                         m = m->m_next;
1057                 }
1058         }
1059         return (NULL);
1060 }
1061
1062 void
1063 m_print(const struct mbuf *m, int maxlen)
1064 {
1065         int len;
1066         int pdata;
1067         const struct mbuf *m2;
1068
1069         if (m->m_flags & M_PKTHDR)
1070                 len = m->m_pkthdr.len;
1071         else
1072                 len = -1;
1073         m2 = m;
1074         while (m2 != NULL && (len == -1 || len)) {
1075                 pdata = m2->m_len;
1076                 if (maxlen != -1 && pdata > maxlen)
1077                         pdata = maxlen;
1078                 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len,
1079                     m2->m_next, m2->m_flags, "\20\20freelist\17skipfw"
1080                     "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly"
1081                     "\3eor\2pkthdr\1ext", pdata ? "" : "\n");
1082                 if (pdata)
1083                         printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-");
1084                 if (len != -1)
1085                         len -= m2->m_len;
1086                 m2 = m2->m_next;
1087         }
1088         if (len > 0)
1089                 printf("%d bytes unaccounted for.\n", len);
1090         return;
1091 }
1092
1093 u_int
1094 m_fixhdr(struct mbuf *m0)
1095 {
1096         u_int len;
1097
1098         len = m_length(m0, NULL);
1099         m0->m_pkthdr.len = len;
1100         return (len);
1101 }
1102
1103 u_int
1104 m_length(struct mbuf *m0, struct mbuf **last)
1105 {
1106         struct mbuf *m;
1107         u_int len;
1108
1109         len = 0;
1110         for (m = m0; m != NULL; m = m->m_next) {
1111                 len += m->m_len;
1112                 if (m->m_next == NULL)
1113                         break;
1114         }
1115         if (last != NULL)
1116                 *last = m;
1117         return (len);
1118 }
1119
1120 /*
1121  * Defragment a mbuf chain, returning the shortest possible
1122  * chain of mbufs and clusters.  If allocation fails and
1123  * this cannot be completed, NULL will be returned, but
1124  * the passed in chain will be unchanged.  Upon success,
1125  * the original chain will be freed, and the new chain
1126  * will be returned.
1127  *
1128  * If a non-packet header is passed in, the original
1129  * mbuf (chain?) will be returned unharmed.
1130  */
1131 struct mbuf *
1132 m_defrag(struct mbuf *m0, int how)
1133 {
1134         struct mbuf *m_new = NULL, *m_final = NULL;
1135         int progress = 0, length;
1136
1137         MBUF_CHECKSLEEP(how);
1138         if (!(m0->m_flags & M_PKTHDR))
1139                 return (m0);
1140
1141         m_fixhdr(m0); /* Needed sanity check */
1142
1143 #ifdef MBUF_STRESS_TEST
1144         if (m_defragrandomfailures) {
1145                 int temp = arc4random() & 0xff;
1146                 if (temp == 0xba)
1147                         goto nospace;
1148         }
1149 #endif
1150         
1151         if (m0->m_pkthdr.len > MHLEN)
1152                 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1153         else
1154                 m_final = m_gethdr(how, MT_DATA);
1155
1156         if (m_final == NULL)
1157                 goto nospace;
1158
1159         if (m_dup_pkthdr(m_final, m0, how) == 0)
1160                 goto nospace;
1161
1162         m_new = m_final;
1163
1164         while (progress < m0->m_pkthdr.len) {
1165                 length = m0->m_pkthdr.len - progress;
1166                 if (length > MCLBYTES)
1167                         length = MCLBYTES;
1168
1169                 if (m_new == NULL) {
1170                         if (length > MLEN)
1171                                 m_new = m_getcl(how, MT_DATA, 0);
1172                         else
1173                                 m_new = m_get(how, MT_DATA);
1174                         if (m_new == NULL)
1175                                 goto nospace;
1176                 }
1177
1178                 m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1179                 progress += length;
1180                 m_new->m_len = length;
1181                 if (m_new != m_final)
1182                         m_cat(m_final, m_new);
1183                 m_new = NULL;
1184         }
1185 #ifdef MBUF_STRESS_TEST
1186         if (m0->m_next == NULL)
1187                 m_defraguseless++;
1188 #endif
1189         m_freem(m0);
1190         m0 = m_final;
1191 #ifdef MBUF_STRESS_TEST
1192         m_defragpackets++;
1193         m_defragbytes += m0->m_pkthdr.len;
1194 #endif
1195         return (m0);
1196 nospace:
1197 #ifdef MBUF_STRESS_TEST
1198         m_defragfailure++;
1199 #endif
1200         if (m_final)
1201                 m_freem(m_final);
1202         return (NULL);
1203 }
1204
1205 #ifdef MBUF_STRESS_TEST
1206
1207 /*
1208  * Fragment an mbuf chain.  There's no reason you'd ever want to do
1209  * this in normal usage, but it's great for stress testing various
1210  * mbuf consumers.
1211  *
1212  * If fragmentation is not possible, the original chain will be
1213  * returned.
1214  *
1215  * Possible length values:
1216  * 0     no fragmentation will occur
1217  * > 0  each fragment will be of the specified length
1218  * -1   each fragment will be the same random value in length
1219  * -2   each fragment's length will be entirely random
1220  * (Random values range from 1 to 256)
1221  */
1222 struct mbuf *
1223 m_fragment(struct mbuf *m0, int how, int length)
1224 {
1225         struct mbuf *m_new = NULL, *m_final = NULL;
1226         int progress = 0;
1227
1228         if (!(m0->m_flags & M_PKTHDR))
1229                 return (m0);
1230         
1231         if ((length == 0) || (length < -2))
1232                 return (m0);
1233
1234         m_fixhdr(m0); /* Needed sanity check */
1235
1236         m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1237
1238         if (m_final == NULL)
1239                 goto nospace;
1240
1241         if (m_dup_pkthdr(m_final, m0, how) == 0)
1242                 goto nospace;
1243
1244         m_new = m_final;
1245
1246         if (length == -1)
1247                 length = 1 + (arc4random() & 255);
1248
1249         while (progress < m0->m_pkthdr.len) {
1250                 int fraglen;
1251
1252                 if (length > 0)
1253                         fraglen = length;
1254                 else
1255                         fraglen = 1 + (arc4random() & 255);
1256                 if (fraglen > m0->m_pkthdr.len - progress)
1257                         fraglen = m0->m_pkthdr.len - progress;
1258
1259                 if (fraglen > MCLBYTES)
1260                         fraglen = MCLBYTES;
1261
1262                 if (m_new == NULL) {
1263                         m_new = m_getcl(how, MT_DATA, 0);
1264                         if (m_new == NULL)
1265                                 goto nospace;
1266                 }
1267
1268                 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
1269                 progress += fraglen;
1270                 m_new->m_len = fraglen;
1271                 if (m_new != m_final)
1272                         m_cat(m_final, m_new);
1273                 m_new = NULL;
1274         }
1275         m_freem(m0);
1276         m0 = m_final;
1277         return (m0);
1278 nospace:
1279         if (m_final)
1280                 m_freem(m_final);
1281         /* Return the original chain on failure */
1282         return (m0);
1283 }
1284
1285 #endif
1286
1287 struct mbuf *
1288 m_uiotombuf(struct uio *uio, int how, int len)
1289 {
1290         struct mbuf *m_new = NULL, *m_final = NULL;
1291         int progress = 0, error = 0, length, total;
1292
1293         if (len > 0)
1294                 total = min(uio->uio_resid, len);
1295         else
1296                 total = uio->uio_resid;
1297         if (total > MHLEN)
1298                 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1299         else
1300                 m_final = m_gethdr(how, MT_DATA);
1301         if (m_final == NULL)
1302                 goto nospace;
1303         m_new = m_final;
1304         while (progress < total) {
1305                 length = total - progress;
1306                 if (length > MCLBYTES)
1307                         length = MCLBYTES;
1308                 if (m_new == NULL) {
1309                         if (length > MLEN)
1310                                 m_new = m_getcl(how, MT_DATA, 0);
1311                         else
1312                                 m_new = m_get(how, MT_DATA);
1313                         if (m_new == NULL)
1314                                 goto nospace;
1315                 }
1316                 error = uiomove(mtod(m_new, void *), length, uio);
1317                 if (error)
1318                         goto nospace;
1319                 progress += length;
1320                 m_new->m_len = length;
1321                 if (m_new != m_final)
1322                         m_cat(m_final, m_new);
1323                 m_new = NULL;
1324         }
1325         m_fixhdr(m_final);
1326         return (m_final);
1327 nospace:
1328         if (m_new)
1329                 m_free(m_new);
1330         if (m_final)
1331                 m_freem(m_final);
1332         return (NULL);
1333 }