mbuf: Staticize mbupdatelimits
[dragonfly.git] / sys / kern / uipc_mbuf.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 2004 Jeffrey M. Hsu.  All rights reserved.
5  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
6  * 
7  * This code is derived from software contributed to The DragonFly Project
8  * by Jeffrey M. Hsu.
9  * 
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  * 
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 /*
37  * Copyright (c) 1982, 1986, 1988, 1991, 1993
38  *      The Regents of the University of California.  All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  * @(#)uipc_mbuf.c      8.2 (Berkeley) 1/4/94
65  * $FreeBSD: src/sys/kern/uipc_mbuf.c,v 1.51.2.24 2003/04/15 06:59:29 silby Exp $
66  */
67
68 #include "opt_param.h"
69 #include "opt_mbuf_stress_test.h"
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/file.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/kernel.h>
76 #include <sys/sysctl.h>
77 #include <sys/domain.h>
78 #include <sys/objcache.h>
79 #include <sys/tree.h>
80 #include <sys/protosw.h>
81 #include <sys/uio.h>
82 #include <sys/thread.h>
83 #include <sys/globaldata.h>
84
85 #include <sys/thread2.h>
86 #include <sys/spinlock2.h>
87
88 #include <machine/atomic.h>
89 #include <machine/limits.h>
90
91 #include <vm/vm.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_extern.h>
94
95 #ifdef INVARIANTS
96 #include <machine/cpu.h>
97 #endif
98
99 /*
100  * mbuf cluster meta-data
101  */
102 struct mbcluster {
103         int32_t mcl_refs;
104         void    *mcl_data;
105 };
106
107 /*
108  * mbuf tracking for debugging purposes
109  */
110 #ifdef MBUF_DEBUG
111
112 static MALLOC_DEFINE(M_MTRACK, "mtrack", "mtrack");
113
114 struct mbctrack;
115 RB_HEAD(mbuf_rb_tree, mbtrack);
116 RB_PROTOTYPE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *);
117
118 struct mbtrack {
119         RB_ENTRY(mbtrack) rb_node;
120         int trackid;
121         struct mbuf *m;
122 };
123
124 static int
125 mbtrack_cmp(struct mbtrack *mb1, struct mbtrack *mb2)
126 {
127         if (mb1->m < mb2->m)
128                 return(-1);
129         if (mb1->m > mb2->m)
130                 return(1);
131         return(0);
132 }
133
134 RB_GENERATE2(mbuf_rb_tree, mbtrack, rb_node, mbtrack_cmp, struct mbuf *, m);
135
136 struct mbuf_rb_tree     mbuf_track_root;
137 static struct spinlock  mbuf_track_spin = SPINLOCK_INITIALIZER(mbuf_track_spin, "mbuf_track_spin");
138
139 static void
140 mbuftrack(struct mbuf *m)
141 {
142         struct mbtrack *mbt;
143
144         mbt = kmalloc(sizeof(*mbt), M_MTRACK, M_INTWAIT|M_ZERO);
145         spin_lock(&mbuf_track_spin);
146         mbt->m = m;
147         if (mbuf_rb_tree_RB_INSERT(&mbuf_track_root, mbt)) {
148                 spin_unlock(&mbuf_track_spin);
149                 panic("mbuftrack: mbuf %p already being tracked", m);
150         }
151         spin_unlock(&mbuf_track_spin);
152 }
153
154 static void
155 mbufuntrack(struct mbuf *m)
156 {
157         struct mbtrack *mbt;
158
159         spin_lock(&mbuf_track_spin);
160         mbt = mbuf_rb_tree_RB_LOOKUP(&mbuf_track_root, m);
161         if (mbt == NULL) {
162                 spin_unlock(&mbuf_track_spin);
163                 panic("mbufuntrack: mbuf %p was not tracked", m);
164         } else {
165                 mbuf_rb_tree_RB_REMOVE(&mbuf_track_root, mbt);
166                 spin_unlock(&mbuf_track_spin);
167                 kfree(mbt, M_MTRACK);
168         }
169 }
170
171 void
172 mbuftrackid(struct mbuf *m, int trackid)
173 {
174         struct mbtrack *mbt;
175         struct mbuf *n;
176
177         spin_lock(&mbuf_track_spin);
178         while (m) { 
179                 n = m->m_nextpkt;
180                 while (m) {
181                         mbt = mbuf_rb_tree_RB_LOOKUP(&mbuf_track_root, m);
182                         if (mbt == NULL) {
183                                 spin_unlock(&mbuf_track_spin);
184                                 panic("mbuftrackid: mbuf %p not tracked", m);
185                         }
186                         mbt->trackid = trackid;
187                         m = m->m_next;
188                 }
189                 m = n;
190         }
191         spin_unlock(&mbuf_track_spin);
192 }
193
194 static int
195 mbuftrack_callback(struct mbtrack *mbt, void *arg)
196 {
197         struct sysctl_req *req = arg;
198         char buf[64];
199         int error;
200
201         ksnprintf(buf, sizeof(buf), "mbuf %p track %d\n", mbt->m, mbt->trackid);
202
203         spin_unlock(&mbuf_track_spin);
204         error = SYSCTL_OUT(req, buf, strlen(buf));
205         spin_lock(&mbuf_track_spin);
206         if (error)      
207                 return(-error);
208         return(0);
209 }
210
211 static int
212 mbuftrack_show(SYSCTL_HANDLER_ARGS)
213 {
214         int error;
215
216         spin_lock(&mbuf_track_spin);
217         error = mbuf_rb_tree_RB_SCAN(&mbuf_track_root, NULL,
218                                      mbuftrack_callback, req);
219         spin_unlock(&mbuf_track_spin);
220         return (-error);
221 }
222 SYSCTL_PROC(_kern_ipc, OID_AUTO, showmbufs, CTLFLAG_RD|CTLTYPE_STRING,
223             0, 0, mbuftrack_show, "A", "Show all in-use mbufs");
224
225 #else
226
227 #define mbuftrack(m)
228 #define mbufuntrack(m)
229
230 #endif
231
232 static void mbinit(void *);
233 SYSINIT(mbuf, SI_BOOT2_MACHDEP, SI_ORDER_FIRST, mbinit, NULL);
234
235 struct mbtypes_stat {
236         u_long  stats[MT_NTYPES];
237 } __cachealign;
238
239 static struct mbtypes_stat      mbtypes[SMP_MAXCPU];
240
241 static struct mbstat mbstat[SMP_MAXCPU] __cachealign;
242 int     max_linkhdr;
243 int     max_protohdr;
244 int     max_hdr;
245 int     max_datalen;
246 int     m_defragpackets;
247 int     m_defragbytes;
248 int     m_defraguseless;
249 int     m_defragfailure;
250 #ifdef MBUF_STRESS_TEST
251 int     m_defragrandomfailures;
252 #endif
253
254 struct objcache *mbuf_cache, *mbufphdr_cache;
255 struct objcache *mclmeta_cache, *mjclmeta_cache;
256 struct objcache *mbufcluster_cache, *mbufphdrcluster_cache;
257 struct objcache *mbufjcluster_cache, *mbufphdrjcluster_cache;
258
259 struct lock     mbupdate_lk = LOCK_INITIALIZER("mbupdate", 0, 0);
260
261 int             nmbclusters;
262 static int      nmbjclusters;
263 int             nmbufs;
264
265 static int      mjclph_cachefrac;
266 static int      mjcl_cachefrac;
267 static int      mclph_cachefrac;
268 static int      mcl_cachefrac;
269
270 SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
271         &max_linkhdr, 0, "Max size of a link-level header");
272 SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
273         &max_protohdr, 0, "Max size of a protocol header");
274 SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0,
275         "Max size of link+protocol headers");
276 SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
277         &max_datalen, 0, "Max data payload size without headers");
278 SYSCTL_INT(_kern_ipc, OID_AUTO, mbuf_wait, CTLFLAG_RW,
279         &mbuf_wait, 0, "Time in ticks to sleep after failed mbuf allocations");
280 static int do_mbstat(SYSCTL_HANDLER_ARGS);
281
282 SYSCTL_PROC(_kern_ipc, KIPC_MBSTAT, mbstat, CTLTYPE_STRUCT|CTLFLAG_RD,
283         0, 0, do_mbstat, "S,mbstat", "mbuf usage statistics");
284
285 static int do_mbtypes(SYSCTL_HANDLER_ARGS);
286
287 SYSCTL_PROC(_kern_ipc, OID_AUTO, mbtypes, CTLTYPE_ULONG|CTLFLAG_RD,
288         0, 0, do_mbtypes, "LU", "");
289
290 static int
291 do_mbstat(SYSCTL_HANDLER_ARGS)
292 {
293         struct mbstat mbstat_total;
294         struct mbstat *mbstat_totalp;
295         int i;
296
297         bzero(&mbstat_total, sizeof(mbstat_total));
298         mbstat_totalp = &mbstat_total;
299
300         for (i = 0; i < ncpus; i++)
301         {
302                 mbstat_total.m_mbufs += mbstat[i].m_mbufs;      
303                 mbstat_total.m_clusters += mbstat[i].m_clusters;        
304                 mbstat_total.m_jclusters += mbstat[i].m_jclusters;      
305                 mbstat_total.m_clfree += mbstat[i].m_clfree;    
306                 mbstat_total.m_drops += mbstat[i].m_drops;      
307                 mbstat_total.m_wait += mbstat[i].m_wait;        
308                 mbstat_total.m_drain += mbstat[i].m_drain;      
309                 mbstat_total.m_mcfail += mbstat[i].m_mcfail;    
310                 mbstat_total.m_mpfail += mbstat[i].m_mpfail;    
311
312         }
313         /*
314          * The following fields are not cumulative fields so just
315          * get their values once.
316          */
317         mbstat_total.m_msize = mbstat[0].m_msize;       
318         mbstat_total.m_mclbytes = mbstat[0].m_mclbytes; 
319         mbstat_total.m_minclsize = mbstat[0].m_minclsize;       
320         mbstat_total.m_mlen = mbstat[0].m_mlen; 
321         mbstat_total.m_mhlen = mbstat[0].m_mhlen;       
322
323         return(sysctl_handle_opaque(oidp, mbstat_totalp, sizeof(mbstat_total), req));
324 }
325
326 static int
327 do_mbtypes(SYSCTL_HANDLER_ARGS)
328 {
329         u_long totals[MT_NTYPES];
330         int i, j;
331
332         for (i = 0; i < MT_NTYPES; i++)
333                 totals[i] = 0;
334
335         for (i = 0; i < ncpus; i++)
336         {
337                 for (j = 0; j < MT_NTYPES; j++)
338                         totals[j] += mbtypes[i].stats[j];
339         }
340
341         return(sysctl_handle_opaque(oidp, totals, sizeof(totals), req));
342 }
343
344 /*
345  * The variables may be set as boot-time tunables or live.  Setting these
346  * values too low can deadlock your network.  Network interfaces may also
347  * adjust nmbclusters and/or nmbjclusters to account for preloading the
348  * hardware rings.
349  */
350 static int sysctl_nmbclusters(SYSCTL_HANDLER_ARGS);
351 static int sysctl_nmbjclusters(SYSCTL_HANDLER_ARGS);
352 static int sysctl_nmbufs(SYSCTL_HANDLER_ARGS);
353 SYSCTL_PROC(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLTYPE_INT | CTLFLAG_RW,
354            0, 0, sysctl_nmbclusters, "I",
355            "Maximum number of mbuf clusters available");
356 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjclusters, CTLTYPE_INT | CTLFLAG_RW,
357            0, 0, sysctl_nmbjclusters, "I",
358            "Maximum number of mbuf jclusters available");
359 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbufs, CTLTYPE_INT | CTLFLAG_RW,
360            0, 0, sysctl_nmbufs, "I",
361            "Maximum number of mbufs available");
362
363 SYSCTL_INT(_kern_ipc, OID_AUTO, mjclph_cachefrac, CTLFLAG_RD,
364            &mjclph_cachefrac, 0,
365            "Fraction of cacheable mbuf jclusters w/ pkthdr");
366 SYSCTL_INT(_kern_ipc, OID_AUTO, mjcl_cachefrac, CTLFLAG_RD,
367            &mjcl_cachefrac, 0,
368            "Fraction of cacheable mbuf jclusters");
369 SYSCTL_INT(_kern_ipc, OID_AUTO, mclph_cachefrac, CTLFLAG_RD,
370            &mclph_cachefrac, 0,
371            "Fraction of cacheable mbuf clusters w/ pkthdr");
372 SYSCTL_INT(_kern_ipc, OID_AUTO, mcl_cachefrac, CTLFLAG_RD,
373            &mcl_cachefrac, 0, "Fraction of cacheable mbuf clusters");
374
375 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
376            &m_defragpackets, 0, "Number of defragment packets");
377 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
378            &m_defragbytes, 0, "Number of defragment bytes");
379 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
380            &m_defraguseless, 0, "Number of useless defragment mbuf chain operations");
381 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
382            &m_defragfailure, 0, "Number of failed defragment mbuf chain operations");
383 #ifdef MBUF_STRESS_TEST
384 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
385            &m_defragrandomfailures, 0, "");
386 #endif
387
388 static MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
389 static MALLOC_DEFINE(M_MBUFCL, "mbufcl", "mbufcl");
390 static MALLOC_DEFINE(M_MCLMETA, "mclmeta", "mclmeta");
391
392 static void m_reclaim (void);
393 static void m_mclref(void *arg);
394 static void m_mclfree(void *arg);
395 static void m_mjclfree(void *arg);
396
397 static void mbupdatelimits(void);
398
399 /*
400  * NOTE: Default NMBUFS must take into account a possible DOS attack
401  *       using fd passing on unix domain sockets.
402  */
403 #ifndef NMBCLUSTERS
404 #define NMBCLUSTERS     (512 + maxusers * 16)
405 #endif
406 #ifndef MJCLPH_CACHEFRAC
407 #define MJCLPH_CACHEFRAC 16
408 #endif
409 #ifndef MJCL_CACHEFRAC
410 #define MJCL_CACHEFRAC  4
411 #endif
412 #ifndef MCLPH_CACHEFRAC
413 #define MCLPH_CACHEFRAC 16
414 #endif
415 #ifndef MCL_CACHEFRAC
416 #define MCL_CACHEFRAC   4
417 #endif
418 #ifndef NMBJCLUSTERS
419 #define NMBJCLUSTERS    (NMBCLUSTERS / 2)
420 #endif
421 #ifndef NMBUFS
422 #define NMBUFS          (nmbclusters * 2 + maxfiles)
423 #endif
424
425 #define NMBCLUSTERS_MIN (NMBCLUSTERS / 2)
426 #define NMBJCLUSTERS_MIN (NMBJCLUSTERS / 2)
427 #define NMBUFS_MIN      ((NMBCLUSTERS * 2 + maxfiles) / 2)
428
429 /*
430  * Perform sanity checks of tunables declared above.
431  */
432 static void
433 tunable_mbinit(void *dummy)
434 {
435         /*
436          * This has to be done before VM init.
437          */
438         nmbclusters = NMBCLUSTERS;
439         TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
440         mjclph_cachefrac = MJCLPH_CACHEFRAC;
441         TUNABLE_INT_FETCH("kern.ipc.mjclph_cachefrac", &mjclph_cachefrac);
442         mjcl_cachefrac = MJCL_CACHEFRAC;
443         TUNABLE_INT_FETCH("kern.ipc.mjcl_cachefrac", &mjcl_cachefrac);
444         mclph_cachefrac = MCLPH_CACHEFRAC;
445         TUNABLE_INT_FETCH("kern.ipc.mclph_cachefrac", &mclph_cachefrac);
446         mcl_cachefrac = MCL_CACHEFRAC;
447         TUNABLE_INT_FETCH("kern.ipc.mcl_cachefrac", &mcl_cachefrac);
448
449         /*
450          * WARNING! each mcl cache feeds two mbuf caches, so the minimum
451          *          cachefrac is 2.  For safety, use 3.
452          */
453         if (mjclph_cachefrac < 3)
454                 mjclph_cachefrac = 3;
455         if (mjcl_cachefrac < 3)
456                 mjcl_cachefrac = 3;
457         if (mclph_cachefrac < 3)
458                 mclph_cachefrac = 3;
459         if (mcl_cachefrac < 3)
460                 mcl_cachefrac = 3;
461
462         nmbjclusters = NMBJCLUSTERS;
463         TUNABLE_INT_FETCH("kern.ipc.nmbjclusters", &nmbjclusters);
464
465         nmbufs = NMBUFS;
466         TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
467
468         /* Sanity checks */
469         if (nmbufs < nmbclusters * 2)
470                 nmbufs = nmbclusters * 2;
471 }
472 SYSINIT(tunable_mbinit, SI_BOOT1_TUNABLES, SI_ORDER_ANY,
473         tunable_mbinit, NULL);
474
475 /*
476  * Sysctl support to update nmbclusters, nmbjclusters, and nmbufs.
477  */
478 static int
479 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
480 {
481         int error;
482         int value;
483
484         value = nmbclusters;
485         error = sysctl_handle_int(oidp, &value, 0, req);
486         if (error || req->newptr == NULL)
487                 return error;
488
489         if (value < NMBCLUSTERS_MIN)
490                 return EINVAL;
491
492         lockmgr(&mbupdate_lk, LK_EXCLUSIVE);
493         if (nmbclusters != value) {
494                 nmbclusters = value;
495                 mbupdatelimits();
496         }
497         lockmgr(&mbupdate_lk, LK_RELEASE);
498         return 0;
499 }
500
501 static int
502 sysctl_nmbjclusters(SYSCTL_HANDLER_ARGS)
503 {
504         int error;
505         int value;
506
507         value = nmbjclusters;
508         error = sysctl_handle_int(oidp, &value, 0, req);
509         if (error || req->newptr == NULL)
510                 return error;
511
512         if (value < NMBJCLUSTERS_MIN)
513                 return EINVAL;
514
515         lockmgr(&mbupdate_lk, LK_EXCLUSIVE);
516         if (nmbjclusters != value) {
517                 nmbjclusters = value;
518                 mbupdatelimits();
519         }
520         lockmgr(&mbupdate_lk, LK_RELEASE);
521         return 0;
522 }
523
524 static int
525 sysctl_nmbufs(SYSCTL_HANDLER_ARGS)
526 {
527         int error;
528         int value;
529
530         value = nmbufs;
531         error = sysctl_handle_int(oidp, &value, 0, req);
532         if (error || req->newptr == NULL)
533                 return error;
534
535         if (value < NMBUFS_MIN)
536                 return EINVAL;
537
538         lockmgr(&mbupdate_lk, LK_EXCLUSIVE);
539         if (nmbufs != value) {
540                 nmbufs = value;
541                 mbupdatelimits();
542         }
543         lockmgr(&mbupdate_lk, LK_RELEASE);
544         return 0;
545 }
546
547 /* "number of clusters of pages" */
548 #define NCL_INIT        1
549
550 #define NMB_INIT        16
551
552 /*
553  * The mbuf object cache only guarantees that m_next and m_nextpkt are
554  * NULL and that m_data points to the beginning of the data area.  In
555  * particular, m_len and m_pkthdr.len are uninitialized.  It is the
556  * responsibility of the caller to initialize those fields before use.
557  */
558 static __inline boolean_t
559 mbuf_ctor(void *obj, void *private, int ocflags)
560 {
561         struct mbuf *m = obj;
562
563         m->m_next = NULL;
564         m->m_nextpkt = NULL;
565         m->m_data = m->m_dat;
566         m->m_flags = 0;
567
568         return (TRUE);
569 }
570
571 /*
572  * Initialize the mbuf and the packet header fields.
573  */
574 static boolean_t
575 mbufphdr_ctor(void *obj, void *private, int ocflags)
576 {
577         struct mbuf *m = obj;
578
579         m->m_next = NULL;
580         m->m_nextpkt = NULL;
581         m->m_data = m->m_pktdat;
582         m->m_flags = M_PKTHDR | M_PHCACHE;
583
584         m->m_pkthdr.rcvif = NULL;       /* eliminate XXX JH */
585         SLIST_INIT(&m->m_pkthdr.tags);
586         m->m_pkthdr.csum_flags = 0;     /* eliminate XXX JH */
587         m->m_pkthdr.fw_flags = 0;       /* eliminate XXX JH */
588
589         return (TRUE);
590 }
591
592 /*
593  * A mbcluster object consists of 2K (MCLBYTES) cluster and a refcount.
594  */
595 static boolean_t
596 mclmeta_ctor(void *obj, void *private, int ocflags)
597 {
598         struct mbcluster *cl = obj;
599         void *buf;
600
601         if (ocflags & M_NOWAIT)
602                 buf = kmalloc(MCLBYTES, M_MBUFCL, M_NOWAIT | M_ZERO);
603         else
604                 buf = kmalloc(MCLBYTES, M_MBUFCL, M_INTWAIT | M_ZERO);
605         if (buf == NULL)
606                 return (FALSE);
607         cl->mcl_refs = 0;
608         cl->mcl_data = buf;
609         return (TRUE);
610 }
611
612 static boolean_t
613 mjclmeta_ctor(void *obj, void *private, int ocflags)
614 {
615         struct mbcluster *cl = obj;
616         void *buf;
617
618         if (ocflags & M_NOWAIT)
619                 buf = kmalloc(MJUMPAGESIZE, M_MBUFCL, M_NOWAIT | M_ZERO);
620         else
621                 buf = kmalloc(MJUMPAGESIZE, M_MBUFCL, M_INTWAIT | M_ZERO);
622         if (buf == NULL)
623                 return (FALSE);
624         cl->mcl_refs = 0;
625         cl->mcl_data = buf;
626         return (TRUE);
627 }
628
629 static void
630 mclmeta_dtor(void *obj, void *private)
631 {
632         struct mbcluster *mcl = obj;
633
634         KKASSERT(mcl->mcl_refs == 0);
635         kfree(mcl->mcl_data, M_MBUFCL);
636 }
637
638 static void
639 linkjcluster(struct mbuf *m, struct mbcluster *cl, uint size)
640 {
641         /*
642          * Add the cluster to the mbuf.  The caller will detect that the
643          * mbuf now has an attached cluster.
644          */
645         m->m_ext.ext_arg = cl;
646         m->m_ext.ext_buf = cl->mcl_data;
647         m->m_ext.ext_ref = m_mclref;
648         if (size != MCLBYTES)
649                 m->m_ext.ext_free = m_mjclfree;
650         else
651                 m->m_ext.ext_free = m_mclfree;
652         m->m_ext.ext_size = size;
653         atomic_add_int(&cl->mcl_refs, 1);
654
655         m->m_data = m->m_ext.ext_buf;
656         m->m_flags |= M_EXT | M_EXT_CLUSTER;
657 }
658
659 static void
660 linkcluster(struct mbuf *m, struct mbcluster *cl)
661 {
662         linkjcluster(m, cl, MCLBYTES);
663 }
664
665 static boolean_t
666 mbufphdrcluster_ctor(void *obj, void *private, int ocflags)
667 {
668         struct mbuf *m = obj;
669         struct mbcluster *cl;
670
671         mbufphdr_ctor(obj, private, ocflags);
672         cl = objcache_get(mclmeta_cache, ocflags);
673         if (cl == NULL) {
674                 ++mbstat[mycpu->gd_cpuid].m_drops;
675                 return (FALSE);
676         }
677         m->m_flags |= M_CLCACHE;
678         linkcluster(m, cl);
679         return (TRUE);
680 }
681
682 static boolean_t
683 mbufphdrjcluster_ctor(void *obj, void *private, int ocflags)
684 {
685         struct mbuf *m = obj;
686         struct mbcluster *cl;
687
688         mbufphdr_ctor(obj, private, ocflags);
689         cl = objcache_get(mjclmeta_cache, ocflags);
690         if (cl == NULL) {
691                 ++mbstat[mycpu->gd_cpuid].m_drops;
692                 return (FALSE);
693         }
694         m->m_flags |= M_CLCACHE;
695         linkjcluster(m, cl, MJUMPAGESIZE);
696         return (TRUE);
697 }
698
699 static boolean_t
700 mbufcluster_ctor(void *obj, void *private, int ocflags)
701 {
702         struct mbuf *m = obj;
703         struct mbcluster *cl;
704
705         mbuf_ctor(obj, private, ocflags);
706         cl = objcache_get(mclmeta_cache, ocflags);
707         if (cl == NULL) {
708                 ++mbstat[mycpu->gd_cpuid].m_drops;
709                 return (FALSE);
710         }
711         m->m_flags |= M_CLCACHE;
712         linkcluster(m, cl);
713         return (TRUE);
714 }
715
716 static boolean_t
717 mbufjcluster_ctor(void *obj, void *private, int ocflags)
718 {
719         struct mbuf *m = obj;
720         struct mbcluster *cl;
721
722         mbuf_ctor(obj, private, ocflags);
723         cl = objcache_get(mjclmeta_cache, ocflags);
724         if (cl == NULL) {
725                 ++mbstat[mycpu->gd_cpuid].m_drops;
726                 return (FALSE);
727         }
728         m->m_flags |= M_CLCACHE;
729         linkjcluster(m, cl, MJUMPAGESIZE);
730         return (TRUE);
731 }
732
733 /*
734  * Used for both the cluster and cluster PHDR caches.
735  *
736  * The mbuf may have lost its cluster due to sharing, deal
737  * with the situation by checking M_EXT.
738  */
739 static void
740 mbufcluster_dtor(void *obj, void *private)
741 {
742         struct mbuf *m = obj;
743         struct mbcluster *mcl;
744
745         if (m->m_flags & M_EXT) {
746                 KKASSERT((m->m_flags & M_EXT_CLUSTER) != 0);
747                 mcl = m->m_ext.ext_arg;
748                 KKASSERT(mcl->mcl_refs == 1);
749                 mcl->mcl_refs = 0;
750                 if (m->m_flags & M_EXT && m->m_ext.ext_size != MCLBYTES)
751                         objcache_put(mjclmeta_cache, mcl);
752                 else
753                         objcache_put(mclmeta_cache, mcl);
754         }
755 }
756
757 struct objcache_malloc_args mbuf_malloc_args = { MSIZE, M_MBUF };
758 struct objcache_malloc_args mclmeta_malloc_args =
759         { sizeof(struct mbcluster), M_MCLMETA };
760
761 /* ARGSUSED*/
762 static void
763 mbinit(void *dummy)
764 {
765         int mb_limit, cl_limit, ncl_limit, jcl_limit;
766         int limit;
767         int i;
768
769         /*
770          * Initialize statistics
771          */
772         for (i = 0; i < ncpus; i++) {
773                 mbstat[i].m_msize = MSIZE;
774                 mbstat[i].m_mclbytes = MCLBYTES;
775                 mbstat[i].m_mjumpagesize = MJUMPAGESIZE;
776                 mbstat[i].m_minclsize = MINCLSIZE;
777                 mbstat[i].m_mlen = MLEN;
778                 mbstat[i].m_mhlen = MHLEN;
779         }
780
781         /*
782          * Create object caches and save cluster limits, which will
783          * be used to adjust backing kmalloc pools' limit later.
784          */
785
786         mb_limit = cl_limit = 0;
787
788         limit = nmbufs;
789         mbuf_cache = objcache_create("mbuf",
790             limit, nmbufs / 4,
791             mbuf_ctor, NULL, NULL,
792             objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
793         mb_limit += limit;
794
795         limit = nmbufs;
796         mbufphdr_cache = objcache_create("mbuf pkt hdr",
797             limit, nmbufs / 4,
798             mbufphdr_ctor, NULL, NULL,
799             objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
800         mb_limit += limit;
801
802         ncl_limit = nmbclusters;
803         mclmeta_cache = objcache_create("cluster mbuf",
804             ncl_limit, nmbclusters / 4,
805             mclmeta_ctor, mclmeta_dtor, NULL,
806             objcache_malloc_alloc, objcache_malloc_free, &mclmeta_malloc_args);
807         cl_limit += ncl_limit;
808
809         jcl_limit = nmbjclusters;
810         mjclmeta_cache = objcache_create("jcluster mbuf",
811             jcl_limit, nmbjclusters / 4,
812             mjclmeta_ctor, mclmeta_dtor, NULL,
813             objcache_malloc_alloc, objcache_malloc_free, &mclmeta_malloc_args);
814         cl_limit += jcl_limit;
815
816         limit = nmbclusters;
817         mbufcluster_cache = objcache_create("mbuf + cluster",
818             limit, nmbclusters / mcl_cachefrac,
819             mbufcluster_ctor, mbufcluster_dtor, NULL,
820             objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
821         mb_limit += limit;
822
823         limit = nmbclusters;
824         mbufphdrcluster_cache = objcache_create("mbuf pkt hdr + cluster",
825             limit, nmbclusters / mclph_cachefrac,
826             mbufphdrcluster_ctor, mbufcluster_dtor, NULL,
827             objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
828         mb_limit += limit;
829
830         limit = nmbjclusters;
831         mbufjcluster_cache = objcache_create("mbuf + jcluster",
832             limit, nmbjclusters / mjcl_cachefrac,
833             mbufjcluster_ctor, mbufcluster_dtor, NULL,
834             objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
835         mb_limit += limit;
836
837         limit = nmbjclusters;
838         mbufphdrjcluster_cache = objcache_create("mbuf pkt hdr + jcluster",
839             limit, nmbjclusters / mjclph_cachefrac,
840             mbufphdrjcluster_ctor, mbufcluster_dtor, NULL,
841             objcache_malloc_alloc, objcache_malloc_free, &mbuf_malloc_args);
842         mb_limit += limit;
843
844         /*
845          * Adjust backing kmalloc pools' limit
846          *
847          * NOTE: We raise the limit by another 1/8 to take the effect
848          * of loosememuse into account.
849          */
850         cl_limit += cl_limit / 8;
851         kmalloc_raise_limit(mclmeta_malloc_args.mtype,
852                             mclmeta_malloc_args.objsize * (size_t)cl_limit);
853         kmalloc_raise_limit(M_MBUFCL,
854                             (MCLBYTES * (size_t)ncl_limit) +
855                             (MJUMPAGESIZE * (size_t)jcl_limit));
856
857         mb_limit += mb_limit / 8;
858         kmalloc_raise_limit(mbuf_malloc_args.mtype,
859                             mbuf_malloc_args.objsize * (size_t)mb_limit);
860 }
861
862 /*
863  * Adjust mbuf limits after changes have been made
864  *
865  * Caller must hold mbupdate_lk
866  */
867 static void
868 mbupdatelimits(void)
869 {
870         int mb_limit, cl_limit, ncl_limit, jcl_limit;
871         int limit;
872
873         KASSERT(lockstatus(&mbupdate_lk, curthread) != 0,
874             ("mbupdate_lk is not held"));
875
876         /*
877          * Figure out adjustments to object caches after nmbufs, nmbclusters,
878          * or nmbjclusters has been modified.
879          */
880         mb_limit = cl_limit = 0;
881
882         limit = nmbufs;
883         objcache_set_cluster_limit(mbuf_cache, limit);
884         mb_limit += limit;
885
886         limit = nmbufs;
887         objcache_set_cluster_limit(mbufphdr_cache, limit);
888         mb_limit += limit;
889
890         ncl_limit = nmbclusters;
891         objcache_set_cluster_limit(mclmeta_cache, ncl_limit);
892         cl_limit += ncl_limit;
893
894         jcl_limit = nmbjclusters;
895         objcache_set_cluster_limit(mjclmeta_cache, jcl_limit);
896         cl_limit += jcl_limit;
897
898         limit = nmbclusters;
899         objcache_set_cluster_limit(mbufcluster_cache, limit);
900         mb_limit += limit;
901
902         limit = nmbclusters;
903         objcache_set_cluster_limit(mbufphdrcluster_cache, limit);
904         mb_limit += limit;
905
906         limit = nmbjclusters;
907         objcache_set_cluster_limit(mbufjcluster_cache, limit);
908         mb_limit += limit;
909
910         limit = nmbjclusters;
911         objcache_set_cluster_limit(mbufphdrjcluster_cache, limit);
912         mb_limit += limit;
913
914         /*
915          * Adjust backing kmalloc pools' limit
916          *
917          * NOTE: We raise the limit by another 1/8 to take the effect
918          * of loosememuse into account.
919          */
920         cl_limit += cl_limit / 8;
921         kmalloc_raise_limit(mclmeta_malloc_args.mtype,
922                             mclmeta_malloc_args.objsize * (size_t)cl_limit);
923         kmalloc_raise_limit(M_MBUFCL,
924                             (MCLBYTES * (size_t)ncl_limit) +
925                             (MJUMPAGESIZE * (size_t)jcl_limit));
926         mb_limit += mb_limit / 8;
927         kmalloc_raise_limit(mbuf_malloc_args.mtype,
928                             mbuf_malloc_args.objsize * (size_t)mb_limit);
929 }
930
931 /*
932  * Return the number of references to this mbuf's data.  0 is returned
933  * if the mbuf is not M_EXT, a reference count is returned if it is
934  * M_EXT | M_EXT_CLUSTER, and 99 is returned if it is a special M_EXT.
935  */
936 int
937 m_sharecount(struct mbuf *m)
938 {
939         switch (m->m_flags & (M_EXT | M_EXT_CLUSTER)) {
940         case 0:
941                 return (0);
942         case M_EXT:
943                 return (99);
944         case M_EXT | M_EXT_CLUSTER:
945                 return (((struct mbcluster *)m->m_ext.ext_arg)->mcl_refs);
946         }
947         /* NOTREACHED */
948         return (0);             /* to shut up compiler */
949 }
950
951 /*
952  * change mbuf to new type
953  */
954 void
955 m_chtype(struct mbuf *m, int type)
956 {
957         struct globaldata *gd = mycpu;
958
959         ++mbtypes[gd->gd_cpuid].stats[type];
960         --mbtypes[gd->gd_cpuid].stats[m->m_type];
961         m->m_type = type;
962 }
963
964 static void
965 m_reclaim(void)
966 {
967         struct domain *dp;
968         struct protosw *pr;
969
970         kprintf("Debug: m_reclaim() called\n");
971
972         SLIST_FOREACH(dp, &domains, dom_next) {
973                 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
974                         if (pr->pr_drain)
975                                 (*pr->pr_drain)();
976                 }
977         }
978         ++mbstat[mycpu->gd_cpuid].m_drain;
979 }
980
981 static __inline void
982 updatestats(struct mbuf *m, int type)
983 {
984         struct globaldata *gd = mycpu;
985
986         m->m_type = type;
987         mbuftrack(m);
988 #ifdef MBUF_DEBUG
989         KASSERT(m->m_next == NULL, ("mbuf %p: bad m_next in get", m));
990         KASSERT(m->m_nextpkt == NULL, ("mbuf %p: bad m_nextpkt in get", m));
991 #endif
992
993         ++mbtypes[gd->gd_cpuid].stats[type];
994         ++mbstat[gd->gd_cpuid].m_mbufs;
995
996 }
997
998 /*
999  * Allocate an mbuf.
1000  */
1001 struct mbuf *
1002 m_get(int how, int type)
1003 {
1004         struct mbuf *m;
1005         int ntries = 0;
1006         int ocf = MB_OCFLAG(how);
1007
1008 retryonce:
1009
1010         m = objcache_get(mbuf_cache, ocf);
1011
1012         if (m == NULL) {
1013                 if ((ocf & M_WAITOK) && ntries++ == 0) {
1014                         struct objcache *reclaimlist[] = {
1015                                 mbufphdr_cache,
1016                                 mbufcluster_cache,
1017                                 mbufphdrcluster_cache,
1018                                 mbufjcluster_cache,
1019                                 mbufphdrjcluster_cache
1020                         };
1021                         const int nreclaims = NELEM(reclaimlist);
1022
1023                         if (!objcache_reclaimlist(reclaimlist, nreclaims, ocf))
1024                                 m_reclaim();
1025                         goto retryonce;
1026                 }
1027                 ++mbstat[mycpu->gd_cpuid].m_drops;
1028                 return (NULL);
1029         }
1030 #ifdef MBUF_DEBUG
1031         KASSERT(m->m_data == m->m_dat, ("mbuf %p: bad m_data in get", m));
1032 #endif
1033         m->m_len = 0;
1034
1035         updatestats(m, type);
1036         return (m);
1037 }
1038
1039 struct mbuf *
1040 m_gethdr(int how, int type)
1041 {
1042         struct mbuf *m;
1043         int ocf = MB_OCFLAG(how);
1044         int ntries = 0;
1045
1046 retryonce:
1047
1048         m = objcache_get(mbufphdr_cache, ocf);
1049
1050         if (m == NULL) {
1051                 if ((ocf & M_WAITOK) && ntries++ == 0) {
1052                         struct objcache *reclaimlist[] = {
1053                                 mbuf_cache,
1054                                 mbufcluster_cache, mbufphdrcluster_cache,
1055                                 mbufjcluster_cache, mbufphdrjcluster_cache
1056                         };
1057                         const int nreclaims = NELEM(reclaimlist);
1058
1059                         if (!objcache_reclaimlist(reclaimlist, nreclaims, ocf))
1060                                 m_reclaim();
1061                         goto retryonce;
1062                 }
1063                 ++mbstat[mycpu->gd_cpuid].m_drops;
1064                 return (NULL);
1065         }
1066 #ifdef MBUF_DEBUG
1067         KASSERT(m->m_data == m->m_pktdat, ("mbuf %p: bad m_data in get", m));
1068 #endif
1069         m->m_len = 0;
1070         m->m_pkthdr.len = 0;
1071
1072         updatestats(m, type);
1073         return (m);
1074 }
1075
1076 /*
1077  * Get a mbuf (not a mbuf cluster!) and zero it.
1078  * Deprecated.
1079  */
1080 struct mbuf *
1081 m_getclr(int how, int type)
1082 {
1083         struct mbuf *m;
1084
1085         m = m_get(how, type);
1086         if (m != NULL)
1087                 bzero(m->m_data, MLEN);
1088         return (m);
1089 }
1090
1091 static struct mbuf *
1092 m_getcl_cache(int how, short type, int flags, struct objcache *mbclc,
1093     struct objcache *mbphclc, u_long *cl_stats)
1094 {
1095         struct mbuf *m = NULL;
1096         int ocflags = MB_OCFLAG(how);
1097         int ntries = 0;
1098
1099 retryonce:
1100
1101         if (flags & M_PKTHDR)
1102                 m = objcache_get(mbphclc, ocflags);
1103         else
1104                 m = objcache_get(mbclc, ocflags);
1105
1106         if (m == NULL) {
1107                 if ((ocflags & M_WAITOK) && ntries++ == 0) {
1108                         struct objcache *reclaimlist[1];
1109
1110                         if (flags & M_PKTHDR)
1111                                 reclaimlist[0] = mbclc;
1112                         else
1113                                 reclaimlist[0] = mbphclc;
1114                         if (!objcache_reclaimlist(reclaimlist, 1, ocflags))
1115                                 m_reclaim();
1116                         goto retryonce;
1117                 }
1118                 ++mbstat[mycpu->gd_cpuid].m_drops;
1119                 return (NULL);
1120         }
1121
1122 #ifdef MBUF_DEBUG
1123         KASSERT(m->m_data == m->m_ext.ext_buf,
1124                 ("mbuf %p: bad m_data in get", m));
1125 #endif
1126         m->m_type = type;
1127         m->m_len = 0;
1128         m->m_pkthdr.len = 0;    /* just do it unconditonally */
1129
1130         mbuftrack(m);
1131
1132         ++mbtypes[mycpu->gd_cpuid].stats[type];
1133         ++(*cl_stats);
1134         return (m);
1135 }
1136
1137 struct mbuf *
1138 m_getjcl(int how, short type, int flags, size_t size)
1139 {
1140         struct objcache *mbclc, *mbphclc;
1141         u_long *cl_stats;
1142
1143         switch (size) {
1144         case MCLBYTES:
1145                 mbclc = mbufcluster_cache;
1146                 mbphclc = mbufphdrcluster_cache;
1147                 cl_stats = &mbstat[mycpu->gd_cpuid].m_clusters;
1148                 break;
1149
1150         default:
1151                 mbclc = mbufjcluster_cache;
1152                 mbphclc = mbufphdrjcluster_cache;
1153                 cl_stats = &mbstat[mycpu->gd_cpuid].m_jclusters;
1154                 break;
1155         }
1156         return m_getcl_cache(how, type, flags, mbclc, mbphclc, cl_stats);
1157 }
1158
1159 /*
1160  * Returns an mbuf with an attached cluster.
1161  * Because many network drivers use this kind of buffers a lot, it is
1162  * convenient to keep a small pool of free buffers of this kind.
1163  * Even a small size such as 10 gives about 10% improvement in the
1164  * forwarding rate in a bridge or router.
1165  */
1166 struct mbuf *
1167 m_getcl(int how, short type, int flags)
1168 {
1169         return m_getcl_cache(how, type, flags,
1170             mbufcluster_cache, mbufphdrcluster_cache,
1171             &mbstat[mycpu->gd_cpuid].m_clusters);
1172 }
1173
1174 /*
1175  * Allocate chain of requested length.
1176  */
1177 struct mbuf *
1178 m_getc(int len, int how, int type)
1179 {
1180         struct mbuf *n, *nfirst = NULL, **ntail = &nfirst;
1181         int nsize;
1182
1183         while (len > 0) {
1184                 n = m_getl(len, how, type, 0, &nsize);
1185                 if (n == NULL)
1186                         goto failed;
1187                 n->m_len = 0;
1188                 *ntail = n;
1189                 ntail = &n->m_next;
1190                 len -= nsize;
1191         }
1192         return (nfirst);
1193
1194 failed:
1195         m_freem(nfirst);
1196         return (NULL);
1197 }
1198
1199 /*
1200  * Allocate len-worth of mbufs and/or mbuf clusters (whatever fits best)
1201  * and return a pointer to the head of the allocated chain. If m0 is
1202  * non-null, then we assume that it is a single mbuf or an mbuf chain to
1203  * which we want len bytes worth of mbufs and/or clusters attached, and so
1204  * if we succeed in allocating it, we will just return a pointer to m0.
1205  *
1206  * If we happen to fail at any point during the allocation, we will free
1207  * up everything we have already allocated and return NULL.
1208  *
1209  * Deprecated.  Use m_getc() and m_cat() instead.
1210  */
1211 struct mbuf *
1212 m_getm(struct mbuf *m0, int len, int type, int how)
1213 {
1214         struct mbuf *nfirst;
1215
1216         nfirst = m_getc(len, how, type);
1217
1218         if (m0 != NULL) {
1219                 m_last(m0)->m_next = nfirst;
1220                 return (m0);
1221         }
1222
1223         return (nfirst);
1224 }
1225
1226 /*
1227  * Adds a cluster to a normal mbuf, M_EXT is set on success.
1228  * Deprecated.  Use m_getcl() instead.
1229  */
1230 void
1231 m_mclget(struct mbuf *m, int how)
1232 {
1233         struct mbcluster *mcl;
1234
1235         KKASSERT((m->m_flags & M_EXT) == 0);
1236         mcl = objcache_get(mclmeta_cache, MB_OCFLAG(how));
1237         if (mcl != NULL) {
1238                 linkcluster(m, mcl);
1239                 ++mbstat[mycpu->gd_cpuid].m_clusters;
1240         } else {
1241                 ++mbstat[mycpu->gd_cpuid].m_drops;
1242         }
1243 }
1244
1245 /*
1246  * Updates to mbcluster must be MPSAFE.  Only an entity which already has
1247  * a reference to the cluster can ref it, so we are in no danger of 
1248  * racing an add with a subtract.  But the operation must still be atomic
1249  * since multiple entities may have a reference on the cluster.
1250  *
1251  * m_mclfree() is almost the same but it must contend with two entities
1252  * freeing the cluster at the same time.
1253  */
1254 static void
1255 m_mclref(void *arg)
1256 {
1257         struct mbcluster *mcl = arg;
1258
1259         atomic_add_int(&mcl->mcl_refs, 1);
1260 }
1261
1262 /*
1263  * When dereferencing a cluster we have to deal with a N->0 race, where
1264  * N entities free their references simultaniously.  To do this we use
1265  * atomic_fetchadd_int().
1266  */
1267 static void
1268 m_mclfree(void *arg)
1269 {
1270         struct mbcluster *mcl = arg;
1271
1272         if (atomic_fetchadd_int(&mcl->mcl_refs, -1) == 1) {
1273                 --mbstat[mycpu->gd_cpuid].m_clusters;
1274                 objcache_put(mclmeta_cache, mcl);
1275         }
1276 }
1277
1278 static void
1279 m_mjclfree(void *arg)
1280 {
1281         struct mbcluster *mcl = arg;
1282
1283         if (atomic_fetchadd_int(&mcl->mcl_refs, -1) == 1) {
1284                 --mbstat[mycpu->gd_cpuid].m_jclusters;
1285                 objcache_put(mjclmeta_cache, mcl);
1286         }
1287 }
1288
1289 /*
1290  * Free a single mbuf and any associated external storage.  The successor,
1291  * if any, is returned.
1292  *
1293  * We do need to check non-first mbuf for m_aux, since some of existing
1294  * code does not call M_PREPEND properly.
1295  * (example: call to bpf_mtap from drivers)
1296  */
1297
1298 #ifdef MBUF_DEBUG
1299
1300 struct mbuf  *
1301 _m_free(struct mbuf *m, const char *func)
1302
1303 #else
1304
1305 struct mbuf *
1306 m_free(struct mbuf *m)
1307
1308 #endif
1309 {
1310         struct mbuf *n;
1311         struct globaldata *gd = mycpu;
1312
1313         KASSERT(m->m_type != MT_FREE, ("freeing free mbuf %p", m));
1314         KASSERT(M_TRAILINGSPACE(m) >= 0, ("overflowed mbuf %p", m));
1315         --mbtypes[gd->gd_cpuid].stats[m->m_type];
1316
1317         n = m->m_next;
1318
1319         /*
1320          * Make sure the mbuf is in constructed state before returning it
1321          * to the objcache.
1322          */
1323         m->m_next = NULL;
1324         mbufuntrack(m);
1325 #ifdef MBUF_DEBUG
1326         m->m_hdr.mh_lastfunc = func;
1327 #endif
1328 #ifdef notyet
1329         KKASSERT(m->m_nextpkt == NULL);
1330 #else
1331         if (m->m_nextpkt != NULL) {
1332                 static int afewtimes = 10;
1333
1334                 if (afewtimes-- > 0) {
1335                         kprintf("mfree: m->m_nextpkt != NULL\n");
1336                         print_backtrace(-1);
1337                 }
1338                 m->m_nextpkt = NULL;
1339         }
1340 #endif
1341         if (m->m_flags & M_PKTHDR) {
1342                 m_tag_delete_chain(m);          /* eliminate XXX JH */
1343         }
1344
1345         m->m_flags &= (M_EXT | M_EXT_CLUSTER | M_CLCACHE | M_PHCACHE);
1346
1347         /*
1348          * Clean the M_PKTHDR state so we can return the mbuf to its original
1349          * cache.  This is based on the PHCACHE flag which tells us whether
1350          * the mbuf was originally allocated out of a packet-header cache
1351          * or a non-packet-header cache.
1352          */
1353         if (m->m_flags & M_PHCACHE) {
1354                 m->m_flags |= M_PKTHDR;
1355                 m->m_pkthdr.rcvif = NULL;       /* eliminate XXX JH */
1356                 m->m_pkthdr.csum_flags = 0;     /* eliminate XXX JH */
1357                 m->m_pkthdr.fw_flags = 0;       /* eliminate XXX JH */
1358                 SLIST_INIT(&m->m_pkthdr.tags);
1359         }
1360
1361         /*
1362          * Handle remaining flags combinations.  M_CLCACHE tells us whether
1363          * the mbuf was originally allocated from a cluster cache or not,
1364          * and is totally separate from whether the mbuf is currently
1365          * associated with a cluster.
1366          */
1367         switch(m->m_flags & (M_CLCACHE | M_EXT | M_EXT_CLUSTER)) {
1368         case M_CLCACHE | M_EXT | M_EXT_CLUSTER:
1369                 /*
1370                  * mbuf+cluster cache case.  The mbuf was allocated from the
1371                  * combined mbuf_cluster cache and can be returned to the
1372                  * cache if the cluster hasn't been shared.
1373                  */
1374                 if (m_sharecount(m) == 1) {
1375                         /*
1376                          * The cluster has not been shared, we can just
1377                          * reset the data pointer and return the mbuf
1378                          * to the cluster cache.  Note that the reference
1379                          * count is left intact (it is still associated with
1380                          * an mbuf).
1381                          */
1382                         m->m_data = m->m_ext.ext_buf;
1383                         if (m->m_flags & M_EXT && m->m_ext.ext_size != MCLBYTES) {
1384                                 if (m->m_flags & M_PHCACHE)
1385                                         objcache_put(mbufphdrjcluster_cache, m);
1386                                 else
1387                                         objcache_put(mbufjcluster_cache, m);
1388                                 --mbstat[mycpu->gd_cpuid].m_jclusters;
1389                         } else {
1390                                 if (m->m_flags & M_PHCACHE)
1391                                         objcache_put(mbufphdrcluster_cache, m);
1392                                 else
1393                                         objcache_put(mbufcluster_cache, m);
1394                                 --mbstat[mycpu->gd_cpuid].m_clusters;
1395                         }
1396                 } else {
1397                         /*
1398                          * Hell.  Someone else has a ref on this cluster,
1399                          * we have to disconnect it which means we can't
1400                          * put it back into the mbufcluster_cache, we
1401                          * have to destroy the mbuf.
1402                          *
1403                          * Other mbuf references to the cluster will typically
1404                          * be M_EXT | M_EXT_CLUSTER but without M_CLCACHE.
1405                          *
1406                          * XXX we could try to connect another cluster to
1407                          * it.
1408                          */
1409                         m->m_ext.ext_free(m->m_ext.ext_arg); 
1410                         m->m_flags &= ~(M_EXT | M_EXT_CLUSTER);
1411                         if (m->m_ext.ext_size == MCLBYTES) {
1412                                 if (m->m_flags & M_PHCACHE)
1413                                         objcache_dtor(mbufphdrcluster_cache, m);
1414                                 else
1415                                         objcache_dtor(mbufcluster_cache, m);
1416                         } else {
1417                                 if (m->m_flags & M_PHCACHE)
1418                                         objcache_dtor(mbufphdrjcluster_cache, m);
1419                                 else
1420                                         objcache_dtor(mbufjcluster_cache, m);
1421                         }
1422                 }
1423                 break;
1424         case M_EXT | M_EXT_CLUSTER:
1425         case M_EXT:
1426                 /*
1427                  * Normal cluster association case, disconnect the cluster from
1428                  * the mbuf.  The cluster may or may not be custom.
1429                  */
1430                 m->m_ext.ext_free(m->m_ext.ext_arg); 
1431                 m->m_flags &= ~(M_EXT | M_EXT_CLUSTER);
1432                 /* fall through */
1433         case 0:
1434                 /*
1435                  * return the mbuf to the mbuf cache.
1436                  */
1437                 if (m->m_flags & M_PHCACHE) {
1438                         m->m_data = m->m_pktdat;
1439                         objcache_put(mbufphdr_cache, m);
1440                 } else {
1441                         m->m_data = m->m_dat;
1442                         objcache_put(mbuf_cache, m);
1443                 }
1444                 --mbstat[mycpu->gd_cpuid].m_mbufs;
1445                 break;
1446         default:
1447                 if (!panicstr)
1448                         panic("bad mbuf flags %p %08x", m, m->m_flags);
1449                 break;
1450         }
1451         return (n);
1452 }
1453
1454 #ifdef MBUF_DEBUG
1455
1456 void
1457 _m_freem(struct mbuf *m, const char *func)
1458 {
1459         while (m)
1460                 m = _m_free(m, func);
1461 }
1462
1463 #else
1464
1465 void
1466 m_freem(struct mbuf *m)
1467 {
1468         while (m)
1469                 m = m_free(m);
1470 }
1471
1472 #endif
1473
1474 void
1475 m_extadd(struct mbuf *m, caddr_t buf, u_int size,  void (*reff)(void *),
1476     void (*freef)(void *), void *arg)
1477 {
1478         m->m_ext.ext_arg = arg;
1479         m->m_ext.ext_buf = buf;
1480         m->m_ext.ext_ref = reff;
1481         m->m_ext.ext_free = freef;
1482         m->m_ext.ext_size = size;
1483         reff(arg);
1484         m->m_data = buf;
1485         m->m_flags |= M_EXT;
1486 }
1487
1488 /*
1489  * mbuf utility routines
1490  */
1491
1492 /*
1493  * Lesser-used path for M_PREPEND: allocate new mbuf to prepend to chain and
1494  * copy junk along.
1495  */
1496 struct mbuf *
1497 m_prepend(struct mbuf *m, int len, int how)
1498 {
1499         struct mbuf *mn;
1500
1501         if (m->m_flags & M_PKTHDR)
1502             mn = m_gethdr(how, m->m_type);
1503         else
1504             mn = m_get(how, m->m_type);
1505         if (mn == NULL) {
1506                 m_freem(m);
1507                 return (NULL);
1508         }
1509         if (m->m_flags & M_PKTHDR)
1510                 M_MOVE_PKTHDR(mn, m);
1511         mn->m_next = m;
1512         m = mn;
1513         if (len < MHLEN)
1514                 MH_ALIGN(m, len);
1515         m->m_len = len;
1516         return (m);
1517 }
1518
1519 /*
1520  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
1521  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
1522  * The wait parameter is a choice of M_WAITOK/M_NOWAIT from caller.
1523  * Note that the copy is read-only, because clusters are not copied,
1524  * only their reference counts are incremented.
1525  */
1526 struct mbuf *
1527 m_copym(const struct mbuf *m, int off0, int len, int wait)
1528 {
1529         struct mbuf *n, **np;
1530         int off = off0;
1531         struct mbuf *top;
1532         int copyhdr = 0;
1533
1534         KASSERT(off >= 0, ("m_copym, negative off %d", off));
1535         KASSERT(len >= 0, ("m_copym, negative len %d", len));
1536         if (off == 0 && (m->m_flags & M_PKTHDR))
1537                 copyhdr = 1;
1538         while (off > 0) {
1539                 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
1540                 if (off < m->m_len)
1541                         break;
1542                 off -= m->m_len;
1543                 m = m->m_next;
1544         }
1545         np = &top;
1546         top = NULL;
1547         while (len > 0) {
1548                 if (m == NULL) {
1549                         KASSERT(len == M_COPYALL, 
1550                             ("m_copym, length > size of mbuf chain"));
1551                         break;
1552                 }
1553                 /*
1554                  * Because we are sharing any cluster attachment below,
1555                  * be sure to get an mbuf that does not have a cluster
1556                  * associated with it.
1557                  */
1558                 if (copyhdr)
1559                         n = m_gethdr(wait, m->m_type);
1560                 else
1561                         n = m_get(wait, m->m_type);
1562                 *np = n;
1563                 if (n == NULL)
1564                         goto nospace;
1565                 if (copyhdr) {
1566                         if (!m_dup_pkthdr(n, m, wait))
1567                                 goto nospace;
1568                         if (len == M_COPYALL)
1569                                 n->m_pkthdr.len -= off0;
1570                         else
1571                                 n->m_pkthdr.len = len;
1572                         copyhdr = 0;
1573                 }
1574                 n->m_len = min(len, m->m_len - off);
1575                 if (m->m_flags & M_EXT) {
1576                         KKASSERT((n->m_flags & M_EXT) == 0);
1577                         n->m_data = m->m_data + off;
1578                         m->m_ext.ext_ref(m->m_ext.ext_arg); 
1579                         n->m_ext = m->m_ext;
1580                         n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1581                 } else {
1582                         bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
1583                             (unsigned)n->m_len);
1584                 }
1585                 if (len != M_COPYALL)
1586                         len -= n->m_len;
1587                 off = 0;
1588                 m = m->m_next;
1589                 np = &n->m_next;
1590         }
1591         if (top == NULL)
1592                 ++mbstat[mycpu->gd_cpuid].m_mcfail;
1593         return (top);
1594 nospace:
1595         m_freem(top);
1596         ++mbstat[mycpu->gd_cpuid].m_mcfail;
1597         return (NULL);
1598 }
1599
1600 /*
1601  * Copy an entire packet, including header (which must be present).
1602  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
1603  * Note that the copy is read-only, because clusters are not copied,
1604  * only their reference counts are incremented.
1605  * Preserve alignment of the first mbuf so if the creator has left
1606  * some room at the beginning (e.g. for inserting protocol headers)
1607  * the copies also have the room available.
1608  */
1609 struct mbuf *
1610 m_copypacket(struct mbuf *m, int how)
1611 {
1612         struct mbuf *top, *n, *o;
1613
1614         n = m_gethdr(how, m->m_type);
1615         top = n;
1616         if (!n)
1617                 goto nospace;
1618
1619         if (!m_dup_pkthdr(n, m, how))
1620                 goto nospace;
1621         n->m_len = m->m_len;
1622         if (m->m_flags & M_EXT) {
1623                 KKASSERT((n->m_flags & M_EXT) == 0);
1624                 n->m_data = m->m_data;
1625                 m->m_ext.ext_ref(m->m_ext.ext_arg); 
1626                 n->m_ext = m->m_ext;
1627                 n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1628         } else {
1629                 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
1630                 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
1631         }
1632
1633         m = m->m_next;
1634         while (m) {
1635                 o = m_get(how, m->m_type);
1636                 if (!o)
1637                         goto nospace;
1638
1639                 n->m_next = o;
1640                 n = n->m_next;
1641
1642                 n->m_len = m->m_len;
1643                 if (m->m_flags & M_EXT) {
1644                         KKASSERT((n->m_flags & M_EXT) == 0);
1645                         n->m_data = m->m_data;
1646                         m->m_ext.ext_ref(m->m_ext.ext_arg); 
1647                         n->m_ext = m->m_ext;
1648                         n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
1649                 } else {
1650                         bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
1651                 }
1652
1653                 m = m->m_next;
1654         }
1655         return top;
1656 nospace:
1657         m_freem(top);
1658         ++mbstat[mycpu->gd_cpuid].m_mcfail;
1659         return (NULL);
1660 }
1661
1662 /*
1663  * Copy data from an mbuf chain starting "off" bytes from the beginning,
1664  * continuing for "len" bytes, into the indicated buffer.
1665  */
1666 void
1667 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
1668 {
1669         unsigned count;
1670
1671         KASSERT(off >= 0, ("m_copydata, negative off %d", off));
1672         KASSERT(len >= 0, ("m_copydata, negative len %d", len));
1673         while (off > 0) {
1674                 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
1675                 if (off < m->m_len)
1676                         break;
1677                 off -= m->m_len;
1678                 m = m->m_next;
1679         }
1680         while (len > 0) {
1681                 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
1682                 count = min(m->m_len - off, len);
1683                 bcopy(mtod(m, caddr_t) + off, cp, count);
1684                 len -= count;
1685                 cp += count;
1686                 off = 0;
1687                 m = m->m_next;
1688         }
1689 }
1690
1691 /*
1692  * Copy a packet header mbuf chain into a completely new chain, including
1693  * copying any mbuf clusters.  Use this instead of m_copypacket() when
1694  * you need a writable copy of an mbuf chain.
1695  */
1696 struct mbuf *
1697 m_dup(struct mbuf *m, int how)
1698 {
1699         struct mbuf **p, *top = NULL;
1700         int remain, moff, nsize;
1701
1702         /* Sanity check */
1703         if (m == NULL)
1704                 return (NULL);
1705         KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __func__));
1706
1707         /* While there's more data, get a new mbuf, tack it on, and fill it */
1708         remain = m->m_pkthdr.len;
1709         moff = 0;
1710         p = &top;
1711         while (remain > 0 || top == NULL) {     /* allow m->m_pkthdr.len == 0 */
1712                 struct mbuf *n;
1713
1714                 /* Get the next new mbuf */
1715                 n = m_getl(remain, how, m->m_type, top == NULL ? M_PKTHDR : 0,
1716                            &nsize);
1717                 if (n == NULL)
1718                         goto nospace;
1719                 if (top == NULL)
1720                         if (!m_dup_pkthdr(n, m, how))
1721                                 goto nospace0;
1722
1723                 /* Link it into the new chain */
1724                 *p = n;
1725                 p = &n->m_next;
1726
1727                 /* Copy data from original mbuf(s) into new mbuf */
1728                 n->m_len = 0;
1729                 while (n->m_len < nsize && m != NULL) {
1730                         int chunk = min(nsize - n->m_len, m->m_len - moff);
1731
1732                         bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
1733                         moff += chunk;
1734                         n->m_len += chunk;
1735                         remain -= chunk;
1736                         if (moff == m->m_len) {
1737                                 m = m->m_next;
1738                                 moff = 0;
1739                         }
1740                 }
1741
1742                 /* Check correct total mbuf length */
1743                 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
1744                         ("%s: bogus m_pkthdr.len", __func__));
1745         }
1746         return (top);
1747
1748 nospace:
1749         m_freem(top);
1750 nospace0:
1751         ++mbstat[mycpu->gd_cpuid].m_mcfail;
1752         return (NULL);
1753 }
1754
1755 /*
1756  * Copy the non-packet mbuf data chain into a new set of mbufs, including
1757  * copying any mbuf clusters.  This is typically used to realign a data
1758  * chain by nfs_realign().
1759  *
1760  * The original chain is left intact.  how should be M_WAITOK or M_NOWAIT
1761  * and NULL can be returned if M_NOWAIT is passed.
1762  *
1763  * Be careful to use cluster mbufs, a large mbuf chain converted to non
1764  * cluster mbufs can exhaust our supply of mbufs.
1765  */
1766 struct mbuf *
1767 m_dup_data(struct mbuf *m, int how)
1768 {
1769         struct mbuf **p, *n, *top = NULL;
1770         int mlen, moff, chunk, gsize, nsize;
1771
1772         /*
1773          * Degenerate case
1774          */
1775         if (m == NULL)
1776                 return (NULL);
1777
1778         /*
1779          * Optimize the mbuf allocation but do not get too carried away.
1780          */
1781         if (m->m_next || m->m_len > MLEN)
1782                 if (m->m_flags & M_EXT && m->m_ext.ext_size == MCLBYTES)
1783                         gsize = MCLBYTES;
1784                 else
1785                         gsize = MJUMPAGESIZE;
1786         else
1787                 gsize = MLEN;
1788
1789         /* Chain control */
1790         p = &top;
1791         n = NULL;
1792         nsize = 0;
1793
1794         /*
1795          * Scan the mbuf chain until nothing is left, the new mbuf chain
1796          * will be allocated on the fly as needed.
1797          */
1798         while (m) {
1799                 mlen = m->m_len;
1800                 moff = 0;
1801
1802                 while (mlen) {
1803                         KKASSERT(m->m_type == MT_DATA);
1804                         if (n == NULL) {
1805                                 n = m_getl(gsize, how, MT_DATA, 0, &nsize);
1806                                 n->m_len = 0;
1807                                 if (n == NULL)
1808                                         goto nospace;
1809                                 *p = n;
1810                                 p = &n->m_next;
1811                         }
1812                         chunk = imin(mlen, nsize);
1813                         bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
1814                         mlen -= chunk;
1815                         moff += chunk;
1816                         n->m_len += chunk;
1817                         nsize -= chunk;
1818                         if (nsize == 0)
1819                                 n = NULL;
1820                 }
1821                 m = m->m_next;
1822         }
1823         *p = NULL;
1824         return(top);
1825 nospace:
1826         *p = NULL;
1827         m_freem(top);
1828         ++mbstat[mycpu->gd_cpuid].m_mcfail;
1829         return (NULL);
1830 }
1831
1832 /*
1833  * Concatenate mbuf chain n to m.
1834  * Both chains must be of the same type (e.g. MT_DATA).
1835  * Any m_pkthdr is not updated.
1836  */
1837 void
1838 m_cat(struct mbuf *m, struct mbuf *n)
1839 {
1840         m = m_last(m);
1841         while (n) {
1842                 if (m->m_flags & M_EXT ||
1843                     m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
1844                         /* just join the two chains */
1845                         m->m_next = n;
1846                         return;
1847                 }
1848                 /* splat the data from one into the other */
1849                 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1850                     (u_int)n->m_len);
1851                 m->m_len += n->m_len;
1852                 n = m_free(n);
1853         }
1854 }
1855
1856 void
1857 m_adj(struct mbuf *mp, int req_len)
1858 {
1859         int len = req_len;
1860         struct mbuf *m;
1861         int count;
1862
1863         if ((m = mp) == NULL)
1864                 return;
1865         if (len >= 0) {
1866                 /*
1867                  * Trim from head.
1868                  */
1869                 while (m != NULL && len > 0) {
1870                         if (m->m_len <= len) {
1871                                 len -= m->m_len;
1872                                 m->m_len = 0;
1873                                 m = m->m_next;
1874                         } else {
1875                                 m->m_len -= len;
1876                                 m->m_data += len;
1877                                 len = 0;
1878                         }
1879                 }
1880                 m = mp;
1881                 if (mp->m_flags & M_PKTHDR)
1882                         m->m_pkthdr.len -= (req_len - len);
1883         } else {
1884                 /*
1885                  * Trim from tail.  Scan the mbuf chain,
1886                  * calculating its length and finding the last mbuf.
1887                  * If the adjustment only affects this mbuf, then just
1888                  * adjust and return.  Otherwise, rescan and truncate
1889                  * after the remaining size.
1890                  */
1891                 len = -len;
1892                 count = 0;
1893                 for (;;) {
1894                         count += m->m_len;
1895                         if (m->m_next == NULL)
1896                                 break;
1897                         m = m->m_next;
1898                 }
1899                 if (m->m_len >= len) {
1900                         m->m_len -= len;
1901                         if (mp->m_flags & M_PKTHDR)
1902                                 mp->m_pkthdr.len -= len;
1903                         return;
1904                 }
1905                 count -= len;
1906                 if (count < 0)
1907                         count = 0;
1908                 /*
1909                  * Correct length for chain is "count".
1910                  * Find the mbuf with last data, adjust its length,
1911                  * and toss data from remaining mbufs on chain.
1912                  */
1913                 m = mp;
1914                 if (m->m_flags & M_PKTHDR)
1915                         m->m_pkthdr.len = count;
1916                 for (; m; m = m->m_next) {
1917                         if (m->m_len >= count) {
1918                                 m->m_len = count;
1919                                 break;
1920                         }
1921                         count -= m->m_len;
1922                 }
1923                 while (m->m_next)
1924                         (m = m->m_next) ->m_len = 0;
1925         }
1926 }
1927
1928 /*
1929  * Set the m_data pointer of a newly-allocated mbuf
1930  * to place an object of the specified size at the
1931  * end of the mbuf, longword aligned.
1932  */
1933 void
1934 m_align(struct mbuf *m, int len)
1935 {
1936         int adjust;
1937
1938         if (m->m_flags & M_EXT)
1939                 adjust = m->m_ext.ext_size - len;
1940         else if (m->m_flags & M_PKTHDR)
1941                 adjust = MHLEN - len;
1942         else
1943                 adjust = MLEN - len;
1944         m->m_data += adjust &~ (sizeof(long)-1);
1945 }
1946
1947 /*
1948  * Create a writable copy of the mbuf chain.  While doing this
1949  * we compact the chain with a goal of producing a chain with
1950  * at most two mbufs.  The second mbuf in this chain is likely
1951  * to be a cluster.  The primary purpose of this work is to create
1952  * a writable packet for encryption, compression, etc.  The
1953  * secondary goal is to linearize the data so the data can be
1954  * passed to crypto hardware in the most efficient manner possible.
1955  */
1956 struct mbuf *
1957 m_unshare(struct mbuf *m0, int how)
1958 {
1959         struct mbuf *m, *mprev;
1960         struct mbuf *n, *mfirst, *mlast;
1961         int len, off;
1962
1963         mprev = NULL;
1964         for (m = m0; m != NULL; m = mprev->m_next) {
1965                 /*
1966                  * Regular mbufs are ignored unless there's a cluster
1967                  * in front of it that we can use to coalesce.  We do
1968                  * the latter mainly so later clusters can be coalesced
1969                  * also w/o having to handle them specially (i.e. convert
1970                  * mbuf+cluster -> cluster).  This optimization is heavily
1971                  * influenced by the assumption that we're running over
1972                  * Ethernet where MCLBYTES is large enough that the max
1973                  * packet size will permit lots of coalescing into a
1974                  * single cluster.  This in turn permits efficient
1975                  * crypto operations, especially when using hardware.
1976                  */
1977                 if ((m->m_flags & M_EXT) == 0) {
1978                         if (mprev && (mprev->m_flags & M_EXT) &&
1979                             m->m_len <= M_TRAILINGSPACE(mprev)) {
1980                                 /* XXX: this ignores mbuf types */
1981                                 memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1982                                        mtod(m, caddr_t), m->m_len);
1983                                 mprev->m_len += m->m_len;
1984                                 mprev->m_next = m->m_next;      /* unlink from chain */
1985                                 m_free(m);                      /* reclaim mbuf */
1986                         } else {
1987                                 mprev = m;
1988                         }
1989                         continue;
1990                 }
1991                 /*
1992                  * Writable mbufs are left alone (for now).
1993                  */
1994                 if (M_WRITABLE(m)) {
1995                         mprev = m;
1996                         continue;
1997                 }
1998
1999                 /*
2000                  * Not writable, replace with a copy or coalesce with
2001                  * the previous mbuf if possible (since we have to copy
2002                  * it anyway, we try to reduce the number of mbufs and
2003                  * clusters so that future work is easier).
2004                  */
2005                 KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags));
2006                 /* NB: we only coalesce into a cluster or larger */
2007                 if (mprev != NULL && (mprev->m_flags & M_EXT) &&
2008                     m->m_len <= M_TRAILINGSPACE(mprev)) {
2009                         /* XXX: this ignores mbuf types */
2010                         memcpy(mtod(mprev, caddr_t) + mprev->m_len,
2011                                mtod(m, caddr_t), m->m_len);
2012                         mprev->m_len += m->m_len;
2013                         mprev->m_next = m->m_next;      /* unlink from chain */
2014                         m_free(m);                      /* reclaim mbuf */
2015                         continue;
2016                 }
2017
2018                 /*
2019                  * Allocate new space to hold the copy...
2020                  */
2021                 /* XXX why can M_PKTHDR be set past the first mbuf? */
2022                 if (mprev == NULL && (m->m_flags & M_PKTHDR)) {
2023                         /*
2024                          * NB: if a packet header is present we must
2025                          * allocate the mbuf separately from any cluster
2026                          * because M_MOVE_PKTHDR will smash the data
2027                          * pointer and drop the M_EXT marker.
2028                          */
2029                         MGETHDR(n, how, m->m_type);
2030                         if (n == NULL) {
2031                                 m_freem(m0);
2032                                 return (NULL);
2033                         }
2034                         M_MOVE_PKTHDR(n, m);
2035                         MCLGET(n, how);
2036                         if ((n->m_flags & M_EXT) == 0) {
2037                                 m_free(n);
2038                                 m_freem(m0);
2039                                 return (NULL);
2040                         }
2041                 } else {
2042                         n = m_getcl(how, m->m_type, m->m_flags);
2043                         if (n == NULL) {
2044                                 m_freem(m0);
2045                                 return (NULL);
2046                         }
2047                 }
2048                 /*
2049                  * ... and copy the data.  We deal with jumbo mbufs
2050                  * (i.e. m_len > MCLBYTES) by splitting them into
2051                  * clusters.  We could just malloc a buffer and make
2052                  * it external but too many device drivers don't know
2053                  * how to break up the non-contiguous memory when
2054                  * doing DMA.
2055                  */
2056                 len = m->m_len;
2057                 off = 0;
2058                 mfirst = n;
2059                 mlast = NULL;
2060                 for (;;) {
2061                         int cc = min(len, MCLBYTES);
2062                         memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc);
2063                         n->m_len = cc;
2064                         if (mlast != NULL)
2065                                 mlast->m_next = n;
2066                         mlast = n;      
2067
2068                         len -= cc;
2069                         if (len <= 0)
2070                                 break;
2071                         off += cc;
2072
2073                         n = m_getcl(how, m->m_type, m->m_flags);
2074                         if (n == NULL) {
2075                                 m_freem(mfirst);
2076                                 m_freem(m0);
2077                                 return (NULL);
2078                         }
2079                 }
2080                 n->m_next = m->m_next; 
2081                 if (mprev == NULL)
2082                         m0 = mfirst;            /* new head of chain */
2083                 else
2084                         mprev->m_next = mfirst; /* replace old mbuf */
2085                 m_free(m);                      /* release old mbuf */
2086                 mprev = mfirst;
2087         }
2088         return (m0);
2089 }
2090
2091 /*
2092  * Rearrange an mbuf chain so that len bytes are contiguous
2093  * and in the data area of an mbuf (so that mtod will work for a structure
2094  * of size len).  Returns the resulting mbuf chain on success, frees it and
2095  * returns null on failure.  If there is room, it will add up to
2096  * max_protohdr-len extra bytes to the contiguous region in an attempt to
2097  * avoid being called next time.
2098  */
2099 struct mbuf *
2100 m_pullup(struct mbuf *n, int len)
2101 {
2102         struct mbuf *m;
2103         int count;
2104         int space;
2105
2106         /*
2107          * If first mbuf has no cluster, and has room for len bytes
2108          * without shifting current data, pullup into it,
2109          * otherwise allocate a new mbuf to prepend to the chain.
2110          */
2111         if (!(n->m_flags & M_EXT) &&
2112             n->m_data + len < &n->m_dat[MLEN] &&
2113             n->m_next) {
2114                 if (n->m_len >= len)
2115                         return (n);
2116                 m = n;
2117                 n = n->m_next;
2118                 len -= m->m_len;
2119         } else {
2120                 if (len > MHLEN)
2121                         goto bad;
2122                 if (n->m_flags & M_PKTHDR)
2123                         m = m_gethdr(M_NOWAIT, n->m_type);
2124                 else
2125                         m = m_get(M_NOWAIT, n->m_type);
2126                 if (m == NULL)
2127                         goto bad;
2128                 m->m_len = 0;
2129                 if (n->m_flags & M_PKTHDR)
2130                         M_MOVE_PKTHDR(m, n);
2131         }
2132         space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
2133         do {
2134                 count = min(min(max(len, max_protohdr), space), n->m_len);
2135                 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
2136                   (unsigned)count);
2137                 len -= count;
2138                 m->m_len += count;
2139                 n->m_len -= count;
2140                 space -= count;
2141                 if (n->m_len)
2142                         n->m_data += count;
2143                 else
2144                         n = m_free(n);
2145         } while (len > 0 && n);
2146         if (len > 0) {
2147                 m_free(m);
2148                 goto bad;
2149         }
2150         m->m_next = n;
2151         return (m);
2152 bad:
2153         m_freem(n);
2154         ++mbstat[mycpu->gd_cpuid].m_mcfail;
2155         return (NULL);
2156 }
2157
2158 /*
2159  * Partition an mbuf chain in two pieces, returning the tail --
2160  * all but the first len0 bytes.  In case of failure, it returns NULL and
2161  * attempts to restore the chain to its original state.
2162  *
2163  * Note that the resulting mbufs might be read-only, because the new
2164  * mbuf can end up sharing an mbuf cluster with the original mbuf if
2165  * the "breaking point" happens to lie within a cluster mbuf. Use the
2166  * M_WRITABLE() macro to check for this case.
2167  */
2168 struct mbuf *
2169 m_split(struct mbuf *m0, int len0, int wait)
2170 {
2171         struct mbuf *m, *n;
2172         unsigned len = len0, remain;
2173
2174         for (m = m0; m && len > m->m_len; m = m->m_next)
2175                 len -= m->m_len;
2176         if (m == NULL)
2177                 return (NULL);
2178         remain = m->m_len - len;
2179         if (m0->m_flags & M_PKTHDR) {
2180                 n = m_gethdr(wait, m0->m_type);
2181                 if (n == NULL)
2182                         return (NULL);
2183                 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
2184                 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
2185                 m0->m_pkthdr.len = len0;
2186                 if (m->m_flags & M_EXT)
2187                         goto extpacket;
2188                 if (remain > MHLEN) {
2189                         /* m can't be the lead packet */
2190                         MH_ALIGN(n, 0);
2191                         n->m_next = m_split(m, len, wait);
2192                         if (n->m_next == NULL) {
2193                                 m_free(n);
2194                                 return (NULL);
2195                         } else {
2196                                 n->m_len = 0;
2197                                 return (n);
2198                         }
2199                 } else
2200                         MH_ALIGN(n, remain);
2201         } else if (remain == 0) {
2202                 n = m->m_next;
2203                 m->m_next = NULL;
2204                 return (n);
2205         } else {
2206                 n = m_get(wait, m->m_type);
2207                 if (n == NULL)
2208                         return (NULL);
2209                 M_ALIGN(n, remain);
2210         }
2211 extpacket:
2212         if (m->m_flags & M_EXT) {
2213                 KKASSERT((n->m_flags & M_EXT) == 0);
2214                 n->m_data = m->m_data + len;
2215                 m->m_ext.ext_ref(m->m_ext.ext_arg); 
2216                 n->m_ext = m->m_ext;
2217                 n->m_flags |= m->m_flags & (M_EXT | M_EXT_CLUSTER);
2218         } else {
2219                 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
2220         }
2221         n->m_len = remain;
2222         m->m_len = len;
2223         n->m_next = m->m_next;
2224         m->m_next = NULL;
2225         return (n);
2226 }
2227
2228 /*
2229  * Routine to copy from device local memory into mbufs.
2230  * Note: "offset" is ill-defined and always called as 0, so ignore it.
2231  */
2232 struct mbuf *
2233 m_devget(char *buf, int len, int offset, struct ifnet *ifp,
2234     void (*copy)(volatile const void *from, volatile void *to, size_t length))
2235 {
2236         struct mbuf *m, *mfirst = NULL, **mtail;
2237         int nsize, flags;
2238
2239         if (copy == NULL)
2240                 copy = bcopy;
2241         mtail = &mfirst;
2242         flags = M_PKTHDR;
2243
2244         while (len > 0) {
2245                 m = m_getl(len, M_NOWAIT, MT_DATA, flags, &nsize);
2246                 if (m == NULL) {
2247                         m_freem(mfirst);
2248                         return (NULL);
2249                 }
2250                 m->m_len = min(len, nsize);
2251
2252                 if (flags & M_PKTHDR) {
2253                         if (len + max_linkhdr <= nsize)
2254                                 m->m_data += max_linkhdr;
2255                         m->m_pkthdr.rcvif = ifp;
2256                         m->m_pkthdr.len = len;
2257                         flags = 0;
2258                 }
2259
2260                 copy(buf, m->m_data, (unsigned)m->m_len);
2261                 buf += m->m_len;
2262                 len -= m->m_len;
2263                 *mtail = m;
2264                 mtail = &m->m_next;
2265         }
2266
2267         return (mfirst);
2268 }
2269
2270 /*
2271  * Routine to pad mbuf to the specified length 'padto'.
2272  */
2273 int
2274 m_devpad(struct mbuf *m, int padto)
2275 {
2276         struct mbuf *last = NULL;
2277         int padlen;
2278
2279         if (padto <= m->m_pkthdr.len)
2280                 return 0;
2281
2282         padlen = padto - m->m_pkthdr.len;
2283
2284         /* if there's only the packet-header and we can pad there, use it. */
2285         if (m->m_pkthdr.len == m->m_len && M_TRAILINGSPACE(m) >= padlen) {
2286                 last = m;
2287         } else {
2288                 /*
2289                  * Walk packet chain to find last mbuf. We will either
2290                  * pad there, or append a new mbuf and pad it
2291                  */
2292                 for (last = m; last->m_next != NULL; last = last->m_next)
2293                         ; /* EMPTY */
2294
2295                 /* `last' now points to last in chain. */
2296                 if (M_TRAILINGSPACE(last) < padlen) {
2297                         struct mbuf *n;
2298
2299                         /* Allocate new empty mbuf, pad it.  Compact later. */
2300                         MGET(n, M_NOWAIT, MT_DATA);
2301                         if (n == NULL)
2302                                 return ENOBUFS;
2303                         n->m_len = 0;
2304                         last->m_next = n;
2305                         last = n;
2306                 }
2307         }
2308         KKASSERT(M_TRAILINGSPACE(last) >= padlen);
2309         KKASSERT(M_WRITABLE(last));
2310
2311         /* Now zero the pad area */
2312         bzero(mtod(last, char *) + last->m_len, padlen);
2313         last->m_len += padlen;
2314         m->m_pkthdr.len += padlen;
2315         return 0;
2316 }
2317
2318 /*
2319  * Copy data from a buffer back into the indicated mbuf chain,
2320  * starting "off" bytes from the beginning, extending the mbuf
2321  * chain if necessary.
2322  */
2323 void
2324 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
2325 {
2326         int mlen;
2327         struct mbuf *m = m0, *n;
2328         int totlen = 0;
2329
2330         if (m0 == NULL)
2331                 return;
2332         while (off > (mlen = m->m_len)) {
2333                 off -= mlen;
2334                 totlen += mlen;
2335                 if (m->m_next == NULL) {
2336                         n = m_getclr(M_NOWAIT, m->m_type);
2337                         if (n == NULL)
2338                                 goto out;
2339                         n->m_len = min(MLEN, len + off);
2340                         m->m_next = n;
2341                 }
2342                 m = m->m_next;
2343         }
2344         while (len > 0) {
2345                 mlen = min (m->m_len - off, len);
2346                 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
2347                 cp += mlen;
2348                 len -= mlen;
2349                 mlen += off;
2350                 off = 0;
2351                 totlen += mlen;
2352                 if (len == 0)
2353                         break;
2354                 if (m->m_next == NULL) {
2355                         n = m_get(M_NOWAIT, m->m_type);
2356                         if (n == NULL)
2357                                 break;
2358                         n->m_len = min(MLEN, len);
2359                         m->m_next = n;
2360                 }
2361                 m = m->m_next;
2362         }
2363 out:    if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
2364                 m->m_pkthdr.len = totlen;
2365 }
2366
2367 /*
2368  * Append the specified data to the indicated mbuf chain,
2369  * Extend the mbuf chain if the new data does not fit in
2370  * existing space.
2371  *
2372  * Return 1 if able to complete the job; otherwise 0.
2373  */
2374 int
2375 m_append(struct mbuf *m0, int len, c_caddr_t cp)
2376 {
2377         struct mbuf *m, *n;
2378         int remainder, space;
2379
2380         for (m = m0; m->m_next != NULL; m = m->m_next)
2381                 ;
2382         remainder = len;
2383         space = M_TRAILINGSPACE(m);
2384         if (space > 0) {
2385                 /*
2386                  * Copy into available space.
2387                  */
2388                 if (space > remainder)
2389                         space = remainder;
2390                 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
2391                 m->m_len += space;
2392                 cp += space, remainder -= space;
2393         }
2394         while (remainder > 0) {
2395                 /*
2396                  * Allocate a new mbuf; could check space
2397                  * and allocate a cluster instead.
2398                  */
2399                 n = m_get(M_NOWAIT, m->m_type);
2400                 if (n == NULL)
2401                         break;
2402                 n->m_len = min(MLEN, remainder);
2403                 bcopy(cp, mtod(n, caddr_t), n->m_len);
2404                 cp += n->m_len, remainder -= n->m_len;
2405                 m->m_next = n;
2406                 m = n;
2407         }
2408         if (m0->m_flags & M_PKTHDR)
2409                 m0->m_pkthdr.len += len - remainder;
2410         return (remainder == 0);
2411 }
2412
2413 /*
2414  * Apply function f to the data in an mbuf chain starting "off" bytes from
2415  * the beginning, continuing for "len" bytes.
2416  */
2417 int
2418 m_apply(struct mbuf *m, int off, int len,
2419     int (*f)(void *, void *, u_int), void *arg)
2420 {
2421         u_int count;
2422         int rval;
2423
2424         KASSERT(off >= 0, ("m_apply, negative off %d", off));
2425         KASSERT(len >= 0, ("m_apply, negative len %d", len));
2426         while (off > 0) {
2427                 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
2428                 if (off < m->m_len)
2429                         break;
2430                 off -= m->m_len;
2431                 m = m->m_next;
2432         }
2433         while (len > 0) {
2434                 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
2435                 count = min(m->m_len - off, len);
2436                 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
2437                 if (rval)
2438                         return (rval);
2439                 len -= count;
2440                 off = 0;
2441                 m = m->m_next;
2442         }
2443         return (0);
2444 }
2445
2446 /*
2447  * Return a pointer to mbuf/offset of location in mbuf chain.
2448  */
2449 struct mbuf *
2450 m_getptr(struct mbuf *m, int loc, int *off)
2451 {
2452
2453         while (loc >= 0) {
2454                 /* Normal end of search. */
2455                 if (m->m_len > loc) {
2456                         *off = loc;
2457                         return (m);
2458                 } else {
2459                         loc -= m->m_len;
2460                         if (m->m_next == NULL) {
2461                                 if (loc == 0) {
2462                                         /* Point at the end of valid data. */
2463                                         *off = m->m_len;
2464                                         return (m);
2465                                 }
2466                                 return (NULL);
2467                         }
2468                         m = m->m_next;
2469                 }
2470         }
2471         return (NULL);
2472 }
2473
2474 void
2475 m_print(const struct mbuf *m)
2476 {
2477         int len;
2478         const struct mbuf *m2;
2479         char *hexstr;
2480
2481         len = m->m_pkthdr.len;
2482         m2 = m;
2483         hexstr = kmalloc(HEX_NCPYLEN(len), M_TEMP, M_ZERO | M_WAITOK);
2484         while (len) {
2485                 kprintf("%p %s\n", m2, hexncpy(m2->m_data, m2->m_len, hexstr,
2486                         HEX_NCPYLEN(m2->m_len), "-"));
2487                 len -= m2->m_len;
2488                 m2 = m2->m_next;
2489         }
2490         kfree(hexstr, M_TEMP);
2491         return;
2492 }
2493
2494 /*
2495  * "Move" mbuf pkthdr from "from" to "to".
2496  * "from" must have M_PKTHDR set, and "to" must be empty.
2497  */
2498 void
2499 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
2500 {
2501         KASSERT((to->m_flags & M_PKTHDR), ("m_move_pkthdr: not packet header"));
2502
2503         to->m_flags |= from->m_flags & M_COPYFLAGS;
2504         to->m_pkthdr = from->m_pkthdr;          /* especially tags */
2505         SLIST_INIT(&from->m_pkthdr.tags);       /* purge tags from src */
2506 }
2507
2508 /*
2509  * Duplicate "from"'s mbuf pkthdr in "to".
2510  * "from" must have M_PKTHDR set, and "to" must be empty.
2511  * In particular, this does a deep copy of the packet tags.
2512  */
2513 int
2514 m_dup_pkthdr(struct mbuf *to, const struct mbuf *from, int how)
2515 {
2516         KASSERT((to->m_flags & M_PKTHDR), ("m_dup_pkthdr: not packet header"));
2517
2518         to->m_flags = (from->m_flags & M_COPYFLAGS) |
2519                       (to->m_flags & ~M_COPYFLAGS);
2520         to->m_pkthdr = from->m_pkthdr;
2521         SLIST_INIT(&to->m_pkthdr.tags);
2522         return (m_tag_copy_chain(to, from, how));
2523 }
2524
2525 /*
2526  * Defragment a mbuf chain, returning the shortest possible
2527  * chain of mbufs and clusters.  If allocation fails and
2528  * this cannot be completed, NULL will be returned, but
2529  * the passed in chain will be unchanged.  Upon success,
2530  * the original chain will be freed, and the new chain
2531  * will be returned.
2532  *
2533  * If a non-packet header is passed in, the original
2534  * mbuf (chain?) will be returned unharmed.
2535  *
2536  * m_defrag_nofree doesn't free the passed in mbuf.
2537  */
2538 struct mbuf *
2539 m_defrag(struct mbuf *m0, int how)
2540 {
2541         struct mbuf *m_new;
2542
2543         if ((m_new = m_defrag_nofree(m0, how)) == NULL)
2544                 return (NULL);
2545         if (m_new != m0)
2546                 m_freem(m0);
2547         return (m_new);
2548 }
2549
2550 struct mbuf *
2551 m_defrag_nofree(struct mbuf *m0, int how)
2552 {
2553         struct mbuf     *m_new = NULL, *m_final = NULL;
2554         int             progress = 0, length, nsize;
2555
2556         if (!(m0->m_flags & M_PKTHDR))
2557                 return (m0);
2558
2559 #ifdef MBUF_STRESS_TEST
2560         if (m_defragrandomfailures) {
2561                 int temp = karc4random() & 0xff;
2562                 if (temp == 0xba)
2563                         goto nospace;
2564         }
2565 #endif
2566         
2567         m_final = m_getl(m0->m_pkthdr.len, how, MT_DATA, M_PKTHDR, &nsize);
2568         if (m_final == NULL)
2569                 goto nospace;
2570         m_final->m_len = 0;     /* in case m0->m_pkthdr.len is zero */
2571
2572         if (m_dup_pkthdr(m_final, m0, how) == 0)
2573                 goto nospace;
2574
2575         m_new = m_final;
2576
2577         while (progress < m0->m_pkthdr.len) {
2578                 length = m0->m_pkthdr.len - progress;
2579                 if (length > MCLBYTES)
2580                         length = MCLBYTES;
2581
2582                 if (m_new == NULL) {
2583                         m_new = m_getl(length, how, MT_DATA, 0, &nsize);
2584                         if (m_new == NULL)
2585                                 goto nospace;
2586                 }
2587
2588                 m_copydata(m0, progress, length, mtod(m_new, caddr_t));
2589                 progress += length;
2590                 m_new->m_len = length;
2591                 if (m_new != m_final)
2592                         m_cat(m_final, m_new);
2593                 m_new = NULL;
2594         }
2595         if (m0->m_next == NULL)
2596                 m_defraguseless++;
2597         m_defragpackets++;
2598         m_defragbytes += m_final->m_pkthdr.len;
2599         return (m_final);
2600 nospace:
2601         m_defragfailure++;
2602         if (m_new)
2603                 m_free(m_new);
2604         m_freem(m_final);
2605         return (NULL);
2606 }
2607
2608 /*
2609  * Move data from uio into mbufs.
2610  */
2611 struct mbuf *
2612 m_uiomove(struct uio *uio)
2613 {
2614         struct mbuf *m;                 /* current working mbuf */
2615         struct mbuf *head = NULL;       /* result mbuf chain */
2616         struct mbuf **mp = &head;
2617         int flags = M_PKTHDR;
2618         int nsize;
2619         int error;
2620         int resid;
2621
2622         do {
2623                 if (uio->uio_resid > INT_MAX)
2624                         resid = INT_MAX;
2625                 else
2626                         resid = (int)uio->uio_resid;
2627                 m = m_getl(resid, M_WAITOK, MT_DATA, flags, &nsize);
2628                 if (flags) {
2629                         m->m_pkthdr.len = 0;
2630                         /* Leave room for protocol headers. */
2631                         if (resid < MHLEN)
2632                                 MH_ALIGN(m, resid);
2633                         flags = 0;
2634                 }
2635                 m->m_len = imin(nsize, resid);
2636                 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
2637                 if (error) {
2638                         m_free(m);
2639                         goto failed;
2640                 }
2641                 *mp = m;
2642                 mp = &m->m_next;
2643                 head->m_pkthdr.len += m->m_len;
2644         } while (uio->uio_resid > 0);
2645
2646         return (head);
2647
2648 failed:
2649         m_freem(head);
2650         return (NULL);
2651 }
2652
2653 struct mbuf *
2654 m_last(struct mbuf *m)
2655 {
2656         while (m->m_next)
2657                 m = m->m_next;
2658         return (m);
2659 }
2660
2661 /*
2662  * Return the number of bytes in an mbuf chain.
2663  * If lastm is not NULL, also return the last mbuf.
2664  */
2665 u_int
2666 m_lengthm(struct mbuf *m, struct mbuf **lastm)
2667 {
2668         u_int len = 0;
2669         struct mbuf *prev = m;
2670
2671         while (m) {
2672                 len += m->m_len;
2673                 prev = m;
2674                 m = m->m_next;
2675         }
2676         if (lastm != NULL)
2677                 *lastm = prev;
2678         return (len);
2679 }
2680
2681 /*
2682  * Like m_lengthm(), except also keep track of mbuf usage.
2683  */
2684 u_int
2685 m_countm(struct mbuf *m, struct mbuf **lastm, u_int *pmbcnt)
2686 {
2687         u_int len = 0, mbcnt = 0;
2688         struct mbuf *prev = m;
2689
2690         while (m) {
2691                 len += m->m_len;
2692                 mbcnt += MSIZE;
2693                 if (m->m_flags & M_EXT)
2694                         mbcnt += m->m_ext.ext_size;
2695                 prev = m;
2696                 m = m->m_next;
2697         }
2698         if (lastm != NULL)
2699                 *lastm = prev;
2700         *pmbcnt = mbcnt;
2701         return (len);
2702 }