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