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