2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1988, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
67 * 10/15/97 Annette DeSchon (deschon@apple.com)
68 * Fixed bug in which all cluster mbufs were broken up
69 * into regular mbufs: Some clusters are now reserved.
70 * When a cluster is needed, regular mbufs are no longer
71 * used. (Radar 1683621)
72 * 20-May-95 Mac Gillon (mgillon) at NeXT
73 * New version based on 4.4
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/malloc.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
82 #include <sys/syslog.h>
83 #include <sys/protosw.h>
84 #include <sys/domain.h>
86 #include <kern/queue.h>
87 #include <kern/kern_types.h>
88 #include <kern/sched_prim.h>
90 #include <IOKit/IOMapper.h>
92 extern vm_offset_t
kmem_mb_alloc(vm_map_t
, int );
93 extern boolean_t
PE_parse_boot_arg(const char *, void *);
95 #define _MCLREF(p) (++mclrefcnt[mtocl(p)])
96 #define _MCLUNREF(p) (--mclrefcnt[mtocl(p)] == 0)
97 #define _M_CLEAR_PKTHDR(mbuf_ptr) (mbuf_ptr)->m_pkthdr.rcvif = NULL; \
98 (mbuf_ptr)->m_pkthdr.len = 0; \
99 (mbuf_ptr)->m_pkthdr.header = NULL; \
100 (mbuf_ptr)->m_pkthdr.csum_flags = 0; \
101 (mbuf_ptr)->m_pkthdr.csum_data = 0; \
102 (mbuf_ptr)->m_pkthdr.aux = (struct mbuf*)NULL; \
103 (mbuf_ptr)->m_pkthdr.vlan_tag = 0; \
104 (mbuf_ptr)->m_pkthdr.socket_id = 0; \
105 SLIST_INIT(&(mbuf_ptr)->m_pkthdr.tags);
107 /* kernel translater */
108 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
110 lck_mtx_t
* mbuf_mlock
;
111 lck_grp_t
* mbuf_mlock_grp
;
112 lck_grp_attr_t
* mbuf_mlock_grp_attr
;
113 lck_attr_t
* mbuf_mlock_attr
;
114 extern lck_mtx_t
*domain_proto_mtx
;
116 struct mbuf
*mfree
; /* mbuf free list */
117 struct mbuf
*mfreelater
; /* mbuf deallocation list */
118 extern vm_map_t mb_map
; /* special map */
119 int m_want
; /* sleepers on mbufs */
120 short *mclrefcnt
; /* mapped cluster reference counts */
122 static ppnum_t mcl_paddr_base
; /* Handle returned by IOMapper::iovmAlloc() */
123 union mcluster
*mclfree
; /* mapped cluster free list */
124 union mbigcluster
*mbigfree
; /* mapped cluster free list */
125 int max_linkhdr
; /* largest link-level header */
126 int max_protohdr
; /* largest protocol header */
127 int max_hdr
; /* largest link+protocol header */
128 int max_datalen
; /* MHLEN - max_hdr */
129 struct mbstat mbstat
; /* statistics */
130 union mcluster
*mbutl
; /* first mapped cluster address */
131 union mcluster
*embutl
; /* ending virtual address of mclusters */
133 static int nclpp
; /* # clusters per physical page */
135 static int m_howmany(int, size_t );
136 void m_reclaim(void);
137 static int m_clalloc(const int , const int, const size_t, int);
140 #define MF_NOWAIT 0x1
143 /* The number of cluster mbufs that are allocated, to start. */
144 #define MINCL max(16, 2)
146 static int mbuf_expand_thread_wakeup
= 0;
147 static int mbuf_expand_mcl
= 0;
148 static int mbuf_expand_big
= 0;
149 static int mbuf_expand_thread_initialized
= 0;
151 static void mbuf_expand_thread_init(void);
152 static void mbuf_expand_thread(void);
153 static int m_expand(int );
154 static caddr_t
m_bigalloc(int );
155 static void m_bigfree(caddr_t
, u_int
, caddr_t
);
156 static struct mbuf
* m_mbigget(struct mbuf
*, int );
158 static void m_range_check(void *addr
);
162 static int mfree_munge
= 0;
164 #define _MFREE_MUNGE(m) { \
167 vm_offset_t *element = (vm_offset_t *)(m); \
169 i < sizeof(struct mbuf)/sizeof(vm_offset_t); \
171 (element)[i] = 0xdeadbeef; \
176 munge_mbuf(struct mbuf
*m
)
179 vm_offset_t
*element
= (vm_offset_t
*)(m
);
181 i
< sizeof(struct mbuf
)/sizeof(vm_offset_t
);
183 (element
)[i
] = 0xdeadbeef;
185 #define _MFREE_MUNGE(m) { \
191 #define _MFREE_MUNGE(m)
195 #define _MINTGET(m, type) { \
197 if (((m) = mfree) != 0) { \
199 ++mclrefcnt[mtocl(m)]; \
200 mbstat.m_mtypes[MT_FREE]--; \
201 mbstat.m_mtypes[(type)]++; \
202 mfree = (m)->m_next; \
209 m_range_check(void *addr
)
211 if (addr
&& (addr
< (void *)mbutl
|| addr
>= (void *)embutl
))
212 panic("mbuf address out of range 0x%x", addr
);
215 __private_extern__
void
224 nclpp
= round_page_32(MCLBYTES
) / MCLBYTES
; /* see mbufgc() */
225 if (nclpp
< 1) nclpp
= 1;
226 mbuf_mlock_grp_attr
= lck_grp_attr_alloc_init();
227 lck_grp_attr_setdefault(mbuf_mlock_grp_attr
);
229 mbuf_mlock_grp
= lck_grp_alloc_init("mbuf", mbuf_mlock_grp_attr
);
230 mbuf_mlock_attr
= lck_attr_alloc_init();
231 lck_attr_setdefault(mbuf_mlock_attr
);
233 mbuf_mlock
= lck_mtx_alloc_init(mbuf_mlock_grp
, mbuf_mlock_attr
);
235 mbstat
.m_msize
= MSIZE
;
236 mbstat
.m_mclbytes
= MCLBYTES
;
237 mbstat
.m_minclsize
= MINCLSIZE
;
238 mbstat
.m_mlen
= MLEN
;
239 mbstat
.m_mhlen
= MHLEN
;
240 mbstat
.m_bigmclbytes
= NBPG
;
242 if (nmbclusters
== 0)
243 nmbclusters
= NMBCLUSTERS
;
244 MALLOC(mclrefcnt
, short *, nmbclusters
* sizeof (short),
248 for (m
= 0; m
< nmbclusters
; m
++)
251 /* Calculate the number of pages assigned to the cluster pool */
252 mcl_pages
= nmbclusters
/(NBPG
/CLBYTES
);
253 MALLOC(mcl_paddr
, int *, mcl_pages
* sizeof(int), M_TEMP
, M_WAITOK
);
256 /* Register with the I/O Bus mapper */
257 mcl_paddr_base
= IOMapperIOVMAlloc(mcl_pages
);
258 bzero((char *)mcl_paddr
, mcl_pages
* sizeof(int));
260 embutl
= (union mcluster
*)((unsigned char *)mbutl
+ (nmbclusters
* MCLBYTES
));
262 PE_parse_boot_arg("initmcl", &initmcl
);
264 if (m_clalloc(max(NBPG
/CLBYTES
, 1) * initmcl
, M_WAIT
, MCLBYTES
, 0) == 0)
268 (void) kernel_thread(kernel_task
, mbuf_expand_thread_init
);
276 * Allocate some number of mbuf clusters
277 * and place on cluster free list.
278 * Take the mbuf lock (if not already locked) and do not release it
285 const size_t bufsize
,
291 vm_offset_t page
= 0;
296 * Honor the caller's wish to block or not block.
297 * We have a way to grow the pool asynchronously,
298 * by kicking the dlil_input_thread.
300 i
= m_howmany(num
, bufsize
);
301 if (i
== 0 || nowait
== M_DONTWAIT
)
305 size
= round_page_32(i
* bufsize
);
306 page
= kmem_mb_alloc(mb_map
, size
);
309 size
= NBPG
; /* Try for 1 if failed */
310 page
= kmem_mb_alloc(mb_map
, size
);
315 numpages
= size
/ NBPG
;
316 for (i
= 0; i
< numpages
; i
++, page
+= NBPG
) {
317 if (((int)page
& PGOFSET
) == 0) {
318 ppnum_t offset
= ((char *)page
- (char *)mbutl
)/NBPG
;
319 ppnum_t new_page
= pmap_find_phys(kernel_pmap
, (vm_address_t
) page
);
322 * In the case of no mapper being available
323 * the following code nops and returns the
324 * input page, if there is a mapper the I/O
325 * page appropriate is returned.
327 new_page
= IOMapperInsertPage(mcl_paddr_base
, offset
, new_page
);
328 mcl_paddr
[offset
] = new_page
<< 12;
330 if (bufsize
== MCLBYTES
) {
331 union mcluster
*mcl
= (union mcluster
*)page
;
333 if (++mclrefcnt
[mtocl(mcl
)] != 0)
334 panic("m_clalloc already there");
335 mcl
->mcl_next
= mclfree
;
337 if (++mclrefcnt
[mtocl(mcl
)] != 0)
338 panic("m_clalloc already there");
339 mcl
->mcl_next
= mclfree
;
342 union mbigcluster
*mbc
= (union mbigcluster
*)page
;
344 if (++mclrefcnt
[mtocl(mbc
)] != 0)
345 panic("m_clalloc already there");
346 if (++mclrefcnt
[mtocl(mbc
) + 1] != 0)
347 panic("m_clalloc already there");
349 mbc
->mbc_next
= mbigfree
;
353 if (bufsize
== MCLBYTES
) {
354 int numcl
= numpages
<< 1;
355 mbstat
.m_clfree
+= numcl
;
356 mbstat
.m_clusters
+= numcl
;
359 mbstat
.m_bigclfree
+= numpages
;
360 mbstat
.m_bigclusters
+= numpages
;
366 * When non-blocking we kick a thread if we havve to grow the
367 * pool or if the number of free clusters is less than requested.
369 if (bufsize
== MCLBYTES
) {
371 /* Remember total number of clusters needed at this time */
372 i
+= mbstat
.m_clusters
;
373 if (i
> mbuf_expand_mcl
) {
375 if (mbuf_expand_thread_initialized
)
376 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
380 if (mbstat
.m_clfree
>= num
)
384 /* Remember total number of 4KB clusters needed at this time */
385 i
+= mbstat
.m_bigclusters
;
386 if (i
> mbuf_expand_big
) {
388 if (mbuf_expand_thread_initialized
)
389 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
393 if (mbstat
.m_bigclfree
>= num
)
400 * Add more free mbufs by cutting up a cluster.
403 m_expand(int canwait
)
407 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4)) {
409 * 1/16th of the total number of cluster mbufs allocated is
410 * reserved for large packets. The number reserved must
411 * always be < 1/2, or future allocation will be prevented.
413 (void)m_clalloc(1, canwait
, MCLBYTES
, 0);
415 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4))
419 MCLALLOC(mcl
, canwait
);
421 struct mbuf
*m
= (struct mbuf
*)mcl
;
424 mbstat
.m_mtypes
[MT_FREE
] += i
;
435 if (i
) wakeup((caddr_t
)&mfree
);
442 * When MGET failes, ask protocols to free space when short of memory,
443 * then re-attempt to allocate an mbuf.
454 (void) m_expand(canwait
);
457 (m
)->m_next
= (m
)->m_nextpkt
= 0;
458 (m
)->m_type
= (type
);
459 (m
)->m_data
= (m
)->m_dat
;
463 if (m
|| canwait
== M_DONTWAIT
)
474 if (mbuf_expand_thread_initialized
)
475 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
483 (void) msleep((caddr_t
)&mfree
, 0, (PZERO
-1) | PDROP
, "m_retry", &ts
);
492 * As above; retry an MGETHDR.
501 if ((m
= m_retry(canwait
, type
))) {
502 m
->m_next
= m
->m_nextpkt
= 0;
503 m
->m_flags
|= M_PKTHDR
;
504 m
->m_data
= m
->m_pktdat
;
513 do_reclaim
= 1; /* drain is performed in pfslowtimo(), to avoid deadlocks */
518 * Space allocation routines.
519 * These are also available as macros
520 * for critical paths.
529 m_range_check(mfree
);
530 m_range_check(mclfree
);
531 m_range_check(mbigfree
);
535 m
->m_next
= m
->m_nextpkt
= 0;
537 m
->m_data
= m
->m_dat
;
541 (m
) = m_retry(nowait
, type
);
543 m_range_check(mfree
);
544 m_range_check(mclfree
);
545 m_range_check(mbigfree
);
558 m_range_check(mfree
);
559 m_range_check(mclfree
);
560 m_range_check(mbigfree
);
565 m
->m_next
= m
->m_nextpkt
= 0;
567 m
->m_data
= m
->m_pktdat
;
568 m
->m_flags
= M_PKTHDR
;
572 m
= m_retryhdr(nowait
, type
);
574 m_range_check(mfree
);
575 m_range_check(mclfree
);
576 m_range_check(mbigfree
);
589 MGET(m
, nowait
, type
);
592 bzero(mtod(m
, caddr_t
), MLEN
);
600 struct mbuf
*n
= m
->m_next
;
604 m_range_check(mfree
);
605 m_range_check(mclfree
);
607 if (m
->m_type
== MT_FREE
)
608 panic("freeing free mbuf");
610 /* Free the aux data if there is any */
611 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
)
613 m_freem(m
->m_pkthdr
.aux
);
615 if ((m
->m_flags
& M_PKTHDR
) != 0)
616 m_tag_delete_chain(m
, NULL
);
619 if ((m
->m_flags
& M_EXT
))
621 if (MCLHASREFERENCE(m
)) {
622 remque((queue_t
)&m
->m_ext
.ext_refs
);
623 } else if (m
->m_ext
.ext_free
== NULL
) {
624 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
628 if (_MCLUNREF(mcl
)) {
629 mcl
->mcl_next
= mclfree
;
634 /* *** Since m_split() increments "mclrefcnt[mtocl(m->m_ext.ext_buf)]",
635 and AppleTalk ADSP uses m_split(), this incorrect sanity check
638 else /* sanity check - not referenced this way */
639 panic("m_free m_ext cluster not free");
642 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
643 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
646 mbstat
.m_mtypes
[m
->m_type
]--;
650 mbstat
.m_mtypes
[m
->m_type
]++;
658 if (i
) wakeup((caddr_t
)&mfree
);
662 /* m_mclget() add an mbuf cluster to a normal mbuf */
668 MCLALLOC(m
->m_ext
.ext_buf
, nowait
);
669 if (m
->m_ext
.ext_buf
) {
670 m
->m_data
= m
->m_ext
.ext_buf
;
672 m
->m_ext
.ext_size
= MCLBYTES
;
673 m
->m_ext
.ext_free
= 0;
674 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
681 /* m_mclalloc() allocate an mbuf cluster */
688 (void)m_clalloc(1, nowait
, MCLBYTES
, 0);
689 if ((p
= (caddr_t
)mclfree
)) {
690 ++mclrefcnt
[mtocl(p
)];
692 mclfree
= ((union mcluster
*)p
)->mcl_next
;
701 /* m_mclfree() releases a reference to a cluster allocated by MCLALLOC,
702 * freeing the cluster if the reference count has reached 0. */
711 if (--mclrefcnt
[mtocl(p
)] == 0) {
712 ((union mcluster
*)(p
))->mcl_next
= mclfree
;
713 mclfree
= (union mcluster
*)(p
);
719 /* mcl_hasreference() checks if a cluster of an mbuf is referenced by another mbuf */
724 return (m
->m_ext
.ext_refs
.forward
!= &(m
->m_ext
.ext_refs
));
727 __private_extern__ caddr_t
728 m_bigalloc(int nowait
)
732 (void)m_clalloc(1, nowait
, NBPG
, 0);
733 if ((p
= (caddr_t
)mbigfree
)) {
734 if (mclrefcnt
[mtocl(p
)] != mclrefcnt
[mtocl(p
) + 1])
735 panic("m_bigalloc mclrefcnt %x mismatch %d != %d",
736 p
, mclrefcnt
[mtocl(p
)], mclrefcnt
[mtocl(p
) + 1]);
737 if (mclrefcnt
[mtocl(p
)] || mclrefcnt
[mtocl(p
) + 1])
738 panic("m_bigalloc mclrefcnt %x not null %d != %d",
739 p
, mclrefcnt
[mtocl(p
)], mclrefcnt
[mtocl(p
) + 1]);
740 ++mclrefcnt
[mtocl(p
)];
741 ++mclrefcnt
[mtocl(p
) + 1];
742 mbstat
.m_bigclfree
--;
743 mbigfree
= ((union mbigcluster
*)p
)->mbc_next
;
751 __private_extern__
void
752 m_bigfree(caddr_t p
, __unused u_int size
, __unused caddr_t arg
)
756 if (mclrefcnt
[mtocl(p
)] != mclrefcnt
[mtocl(p
) + 1])
757 panic("m_bigfree mclrefcnt %x mismatch %d != %d",
758 p
, mclrefcnt
[mtocl(p
)], mclrefcnt
[mtocl(p
) + 1]);
759 --mclrefcnt
[mtocl(p
)];
760 --mclrefcnt
[mtocl(p
) + 1];
761 if (mclrefcnt
[mtocl(p
)] == 0) {
762 ((union mbigcluster
*)(p
))->mbc_next
= mbigfree
;
763 mbigfree
= (union mbigcluster
*)(p
);
764 mbstat
.m_bigclfree
++;
768 /* m_mbigget() add an 4KB mbuf cluster to a normal mbuf */
769 __private_extern__
struct mbuf
*
770 m_mbigget(struct mbuf
*m
, int nowait
)
772 m
->m_ext
.ext_buf
= m_bigalloc(nowait
);
773 if (m
->m_ext
.ext_buf
) {
774 m
->m_data
= m
->m_ext
.ext_buf
;
776 m
->m_ext
.ext_size
= NBPG
;
777 m
->m_ext
.ext_free
= m_bigfree
;
778 m
->m_ext
.ext_arg
= 0;
779 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
793 to
->m_pkthdr
= from
->m_pkthdr
;
794 from
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
795 SLIST_INIT(&from
->m_pkthdr
.tags
); /* purge tags from src */
796 to
->m_flags
= from
->m_flags
& M_COPYFLAGS
;
797 to
->m_data
= (to
)->m_pktdat
;
801 * "Move" mbuf pkthdr from "from" to "to".
802 * "from" must have M_PKTHDR set, and "to" must be empty.
806 m_move_pkthdr(struct mbuf
*to
, struct mbuf
*from
)
808 KASSERT((to
->m_flags
& M_EXT
) == 0, ("m_move_pkthdr: to has cluster"));
810 to
->m_flags
= from
->m_flags
& M_COPYFLAGS
;
811 to
->m_data
= to
->m_pktdat
;
812 to
->m_pkthdr
= from
->m_pkthdr
; /* especially tags */
813 SLIST_INIT(&from
->m_pkthdr
.tags
); /* purge tags from src */
814 from
->m_flags
&= ~M_PKTHDR
;
819 * Duplicate "from"'s mbuf pkthdr in "to".
820 * "from" must have M_PKTHDR set, and "to" must be empty.
821 * In particular, this does a deep copy of the packet tags.
824 m_dup_pkthdr(struct mbuf
*to
, struct mbuf
*from
, int how
)
826 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) | (to
->m_flags
& M_EXT
);
827 if ((to
->m_flags
& M_EXT
) == 0)
828 to
->m_data
= to
->m_pktdat
;
829 to
->m_pkthdr
= from
->m_pkthdr
;
830 SLIST_INIT(&to
->m_pkthdr
.tags
);
831 return (m_tag_copy_chain(to
, from
, how
));
835 * return a list of mbuf hdrs that point to clusters...
836 * try for num_needed, if wantall is not set, return whatever
837 * number were available... set up the first num_with_pkthdrs
838 * with mbuf hdrs configured as packet headers... these are
839 * chained on the m_nextpkt field... any packets requested beyond
840 * this are chained onto the last packet header's m_next field.
841 * The size of the cluster is controlled by the paramter bufsize.
843 __private_extern__
struct mbuf
*
844 m_getpackets_internal(unsigned int *num_needed
, int num_with_pkthdrs
, int how
, int wantall
, size_t bufsize
)
847 struct mbuf
**np
, *top
;
848 unsigned int num
, needed
= *num_needed
;
850 if (bufsize
!= MCLBYTES
&& bufsize
!= NBPG
)
856 (void)m_clalloc(needed
, how
, bufsize
, 0); /* takes the MBUF_LOCK, but doesn't release it... */
858 for (num
= 0; num
< needed
; num
++) {
859 m_range_check(mfree
);
860 m_range_check(mclfree
);
861 m_range_check(mbigfree
);
863 if (mfree
&& ((bufsize
== NBPG
&& mbigfree
) || (bufsize
== MCLBYTES
&& mclfree
))) {
864 /* mbuf + cluster are available */
868 ++mclrefcnt
[mtocl(m
)];
869 mbstat
.m_mtypes
[MT_FREE
]--;
870 mbstat
.m_mtypes
[MT_DATA
]++;
871 if (bufsize
== NBPG
) {
872 m
->m_ext
.ext_buf
= (caddr_t
)mbigfree
; /* get the big cluster */
873 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
874 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
) + 1];
875 mbstat
.m_bigclfree
--;
876 mbigfree
= ((union mbigcluster
*)(m
->m_ext
.ext_buf
))->mbc_next
;
877 m
->m_ext
.ext_free
= m_bigfree
;
878 m
->m_ext
.ext_size
= NBPG
;
880 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
881 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
883 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
884 m
->m_ext
.ext_free
= 0;
885 m
->m_ext
.ext_size
= MCLBYTES
;
887 m
->m_ext
.ext_arg
= 0;
888 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
889 m
->m_next
= m
->m_nextpkt
= 0;
891 m
->m_data
= m
->m_ext
.ext_buf
;
894 if (num_with_pkthdrs
== 0)
897 m
->m_flags
= M_PKTHDR
| M_EXT
;
905 if (num_with_pkthdrs
== 0) {
906 MGET(m
, how
, MT_DATA
);
908 MGETHDR(m
, how
, MT_DATA
);
916 m
= m_mbigget(m
, how
);
918 m
= m_mclget(m
, how
);
919 if ((m
->m_flags
& M_EXT
) == 0) {
927 if (num_with_pkthdrs
)
937 if (wantall
&& top
) {
946 * Return list of mbuf linked by m_nextpkt
947 * Try for num_needed, and if wantall is not set, return whatever
948 * number were available
949 * The size of each mbuf in the list is controlled by the parameter packetlen.
950 * Each mbuf of the list may have a chain of mbufs linked by m_next. Each mbuf in
951 * the chain is called a segment.
952 * If maxsegments is not null and the value pointed to is not null, this specify
953 * the maximum number of segments for a chain of mbufs.
954 * If maxsegments is zero or the value pointed to is zero the
955 * caller does not have any restriction on the number of segments.
956 * The actual number of segments of a mbuf chain is return in the value pointed
958 * When possible the allocation is done under a single lock.
961 __private_extern__
struct mbuf
*
962 m_allocpacket_internal(unsigned int *num_needed
, size_t packetlen
, unsigned int * maxsegments
,
963 int how
, int wantall
, size_t wantsize
)
965 struct mbuf
**np
, *top
;
968 unsigned int numchunks
= 0;
974 if (packetlen
<= MINCLSIZE
)
976 else if (packetlen
> MCLBYTES
)
980 } else if (wantsize
== MCLBYTES
|| wantsize
== NBPG
)
985 if (bufsize
<= MHLEN
) {
987 } else if (bufsize
<= MINCLSIZE
) {
988 if (maxsegments
!= NULL
&& *maxsegments
== 1) {
994 } else if (bufsize
== NBPG
) {
995 numchunks
= ((packetlen
- 1) >> PGSHIFT
) + 1;
997 numchunks
= ((packetlen
- 1) >> MCLSHIFT
) + 1;
999 if (maxsegments
!= NULL
) {
1000 if (*maxsegments
&& numchunks
> *maxsegments
) {
1001 *maxsegments
= numchunks
;
1004 *maxsegments
= numchunks
;
1006 /* m_clalloc takes the MBUF_LOCK, but do not release it */
1007 (void)m_clalloc(numchunks
, how
, (bufsize
== NBPG
) ? NBPG
: MCLBYTES
, 0);
1008 for (num
= 0; num
< *num_needed
; num
++) {
1009 struct mbuf
**nm
, *pkt
= 0;
1014 m_range_check(mfree
);
1015 m_range_check(mclfree
);
1016 m_range_check(mbigfree
);
1018 for (len
= 0; len
< packetlen
; ) {
1019 struct mbuf
*m
= NULL
;
1021 if (wantsize
== 0 && packetlen
> MINCLSIZE
) {
1022 if (packetlen
- len
> MCLBYTES
)
1029 if (mfree
&& ((bufsize
== NBPG
&& mbigfree
) || (bufsize
== MCLBYTES
&& mclfree
))) {
1030 /* mbuf + cluster are available */
1034 ++mclrefcnt
[mtocl(m
)];
1035 mbstat
.m_mtypes
[MT_FREE
]--;
1036 mbstat
.m_mtypes
[MT_DATA
]++;
1037 if (bufsize
== NBPG
) {
1038 m
->m_ext
.ext_buf
= (caddr_t
)mbigfree
; /* get the big cluster */
1039 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
1040 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
) + 1];
1041 mbstat
.m_bigclfree
--;
1042 mbigfree
= ((union mbigcluster
*)(m
->m_ext
.ext_buf
))->mbc_next
;
1043 m
->m_ext
.ext_free
= m_bigfree
;
1044 m
->m_ext
.ext_size
= NBPG
;
1046 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
1047 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
1049 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
1050 m
->m_ext
.ext_free
= 0;
1051 m
->m_ext
.ext_size
= MCLBYTES
;
1053 m
->m_ext
.ext_arg
= 0;
1054 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
1055 m
->m_next
= m
->m_nextpkt
= 0;
1056 m
->m_type
= MT_DATA
;
1057 m
->m_data
= m
->m_ext
.ext_buf
;
1062 m
->m_flags
= M_PKTHDR
| M_EXT
;
1071 MGETHDR(m
, how
, MT_DATA
);
1073 MGET(m
, how
, MT_DATA
);
1079 if (bufsize
<= MINCLSIZE
) {
1080 if (bufsize
> MHLEN
) {
1081 MGET(m
->m_next
, how
, MT_DATA
);
1082 if (m
->m_next
== 0) {
1089 if (bufsize
== NBPG
)
1090 m
= m_mbigget(m
, how
);
1092 m
= m_mclget(m
, how
);
1093 if ((m
->m_flags
& M_EXT
) == 0) {
1105 np
= &pkt
->m_nextpkt
;
1112 if (wantall
&& top
) {
1122 /* Best effort to get a mbuf cluster + pkthdr under one lock.
1123 * If we don't have them avail, just bail out and use the regular
1125 * Used by drivers to allocated packets on receive ring.
1127 __private_extern__
struct mbuf
*
1128 m_getpacket_how(int how
)
1130 unsigned int num_needed
= 1;
1132 return m_getpackets_internal(&num_needed
, 1, how
, 1, MCLBYTES
);
1135 /* Best effort to get a mbuf cluster + pkthdr under one lock.
1136 * If we don't have them avail, just bail out and use the regular
1138 * Used by drivers to allocated packets on receive ring.
1143 unsigned int num_needed
= 1;
1145 return m_getpackets_internal(&num_needed
, 1, M_WAITOK
, 1, MCLBYTES
);
1150 * return a list of mbuf hdrs that point to clusters...
1151 * try for num_needed, if this can't be met, return whatever
1152 * number were available... set up the first num_with_pkthdrs
1153 * with mbuf hdrs configured as packet headers... these are
1154 * chained on the m_nextpkt field... any packets requested beyond
1155 * this are chained onto the last packet header's m_next field.
1158 m_getpackets(int num_needed
, int num_with_pkthdrs
, int how
)
1160 unsigned int n
= num_needed
;
1162 return m_getpackets_internal(&n
, num_with_pkthdrs
, how
, 0, MCLBYTES
);
1167 * return a list of mbuf hdrs set up as packet hdrs
1168 * chained together on the m_nextpkt field
1171 m_getpackethdrs(int num_needed
, int how
)
1174 struct mbuf
**np
, *top
;
1181 while (num_needed
--) {
1182 m_range_check(mfree
);
1183 m_range_check(mclfree
);
1184 m_range_check(mbigfree
);
1186 if ((m
= mfree
)) { /* mbufs are available */
1189 ++mclrefcnt
[mtocl(m
)];
1190 mbstat
.m_mtypes
[MT_FREE
]--;
1191 mbstat
.m_mtypes
[MT_DATA
]++;
1193 m
->m_next
= m
->m_nextpkt
= 0;
1194 m
->m_type
= MT_DATA
;
1195 m
->m_flags
= M_PKTHDR
;
1197 m
->m_data
= m
->m_pktdat
;
1203 m
= m_retryhdr(how
, MT_DATA
);
1217 /* free and mbuf list (m_nextpkt) while following m_next under one lock.
1218 * returns the count for mbufs packets freed. Used by the drivers.
1224 struct mbuf
*nextpkt
;
1231 nextpkt
= m
->m_nextpkt
; /* chain of linked mbufs from driver */
1237 while (m
) { /* free the mbuf chain (like mfreem) */
1242 m_range_check(mfree
);
1243 m_range_check(mclfree
);
1244 m_range_check(mbigfree
);
1247 /* Free the aux data if there is any */
1248 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
) {
1250 * Treat the current m as the nextpkt and set m
1251 * to the aux data. Preserve nextpkt in m->m_nextpkt.
1252 * This lets us free the aux data in this loop
1253 * without having to call m_freem recursively,
1254 * which wouldn't work because we've still got
1257 m
->m_nextpkt
= nextpkt
;
1259 m
= nextpkt
->m_pkthdr
.aux
;
1260 nextpkt
->m_pkthdr
.aux
= NULL
;
1263 if ((m
->m_flags
& M_PKTHDR
) != 0 && !SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
1264 /* A quick (albeit inefficient) expedient */
1266 m_tag_delete_chain(m
, NULL
);
1272 if (n
&& n
->m_nextpkt
)
1273 panic("m_freem_list: m_nextpkt of m_next != NULL");
1274 if (m
->m_type
== MT_FREE
)
1275 panic("freeing free mbuf");
1277 if (m
->m_flags
& M_EXT
) {
1278 if (MCLHASREFERENCE(m
)) {
1279 remque((queue_t
)&m
->m_ext
.ext_refs
);
1280 } else if (m
->m_ext
.ext_free
== NULL
) {
1281 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
1285 if (_MCLUNREF(mcl
)) {
1286 mcl
->mcl_next
= mclfree
;
1291 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
1292 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
1295 mbstat
.m_mtypes
[m
->m_type
]--;
1296 (void) _MCLUNREF(m
);
1298 mbstat
.m_mtypes
[MT_FREE
]++;
1299 m
->m_type
= MT_FREE
;
1306 m
= nextpkt
; /* bump m with saved nextpkt if any */
1314 wakeup((caddr_t
)&mfree
);
1328 * Mbuffer utility routines.
1331 * Compute the amount of space available
1332 * before the current start of data in an mbuf.
1338 if (m
->m_flags
& M_EXT
) {
1339 if (MCLHASREFERENCE(m
))
1341 return (m
->m_data
- m
->m_ext
.ext_buf
);
1343 if (m
->m_flags
& M_PKTHDR
)
1344 return (m
->m_data
- m
->m_pktdat
);
1345 return (m
->m_data
- m
->m_dat
);
1349 * Compute the amount of space available
1350 * after the end of data in an mbuf.
1356 if (m
->m_flags
& M_EXT
) {
1357 if (MCLHASREFERENCE(m
))
1359 return (m
->m_ext
.ext_buf
+ m
->m_ext
.ext_size
-
1360 (m
->m_data
+ m
->m_len
));
1362 return (&m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
));
1366 * Lesser-used path for M_PREPEND:
1367 * allocate new mbuf to prepend to chain,
1369 * Does not adjust packet header length.
1379 MGET(mn
, how
, m
->m_type
);
1380 if (mn
== (struct mbuf
*)NULL
) {
1382 return ((struct mbuf
*)NULL
);
1384 if (m
->m_flags
& M_PKTHDR
) {
1385 M_COPY_PKTHDR(mn
, m
);
1386 m
->m_flags
&= ~M_PKTHDR
;
1397 * Replacement for old M_PREPEND macro:
1398 * allocate new mbuf to prepend to chain,
1399 * copy junk along, and adjust length.
1408 if (M_LEADINGSPACE(m
) >= len
) {
1412 m
= m_prepend(m
, len
, how
);
1414 if ((m
) && (m
->m_flags
& M_PKTHDR
))
1415 m
->m_pkthdr
.len
+= len
;
1420 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
1421 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
1422 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
1433 struct mbuf
*n
, **np
;
1438 if (off
< 0 || len
< 0)
1440 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1443 while (off
>= m
->m_len
) {
1455 m_range_check(mfree
);
1456 m_range_check(mclfree
);
1457 m_range_check(mbigfree
);
1460 if (len
!= M_COPYALL
)
1466 ++mclrefcnt
[mtocl(n
)];
1467 mbstat
.m_mtypes
[MT_FREE
]--;
1468 mbstat
.m_mtypes
[m
->m_type
]++;
1470 n
->m_next
= n
->m_nextpkt
= 0;
1471 n
->m_type
= m
->m_type
;
1472 n
->m_data
= n
->m_dat
;
1476 n
= m_retry(wait
, m
->m_type
);
1484 M_COPY_PKTHDR(n
, m
);
1485 if (len
== M_COPYALL
)
1486 n
->m_pkthdr
.len
-= off0
;
1488 n
->m_pkthdr
.len
= len
;
1491 if (len
== M_COPYALL
) {
1492 if (min(len
, (m
->m_len
- off
)) == len
) {
1493 printf("m->m_len %d - off %d = %d, %d\n",
1494 m
->m_len
, off
, m
->m_len
- off
,
1495 min(len
, (m
->m_len
- off
)));
1498 n
->m_len
= min(len
, (m
->m_len
- off
));
1499 if (n
->m_len
== M_COPYALL
) {
1500 printf("n->m_len == M_COPYALL, fixing\n");
1503 if (m
->m_flags
& M_EXT
) {
1504 n
->m_ext
= m
->m_ext
;
1505 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1506 n
->m_data
= m
->m_data
+ off
;
1507 n
->m_flags
|= M_EXT
;
1509 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1510 (unsigned)n
->m_len
);
1512 if (len
!= M_COPYALL
)
1534 * equivilent to m_copym except that all necessary
1535 * mbuf hdrs are allocated within this routine
1536 * also, the last mbuf and offset accessed are passed
1537 * out and can be passed back in to avoid having to
1538 * rescan the entire mbuf list (normally hung off of the socket)
1546 struct mbuf
**m_last
,
1549 struct mbuf
*n
, **np
= 0;
1551 struct mbuf
*top
= 0;
1555 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1562 while (off
>= m
->m_len
) {
1571 m_range_check(mfree
);
1572 m_range_check(mclfree
);
1573 m_range_check(mbigfree
);
1579 panic("m_gethdr_and_copym");
1584 ++mclrefcnt
[mtocl(n
)];
1585 mbstat
.m_mtypes
[MT_FREE
]--;
1586 mbstat
.m_mtypes
[type
]++;
1588 n
->m_next
= n
->m_nextpkt
= 0;
1592 n
->m_data
= n
->m_dat
;
1595 n
->m_data
= n
->m_pktdat
;
1596 n
->m_flags
= M_PKTHDR
;
1602 n
= m_retry(wait
, type
);
1604 n
= m_retryhdr(wait
, type
);
1617 M_COPY_PKTHDR(n
, m
);
1618 n
->m_pkthdr
.len
= len
;
1621 n
->m_len
= min(len
, (m
->m_len
- off
));
1623 if (m
->m_flags
& M_EXT
) {
1624 n
->m_ext
= m
->m_ext
;
1625 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1626 n
->m_data
= m
->m_data
+ off
;
1627 n
->m_flags
|= M_EXT
;
1629 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1630 (unsigned)n
->m_len
);
1635 if ((off
+ n
->m_len
) == m
->m_len
) {
1636 *m_last
= m
->m_next
;
1640 *m_off
= off
+ n
->m_len
;
1662 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1663 * continuing for "len" bytes, into the indicated buffer.
1673 if (off
< 0 || len
< 0)
1674 panic("m_copydata");
1677 panic("m_copydata");
1685 panic("m_copydata");
1686 count
= min(m
->m_len
- off
, len
);
1687 bcopy(mtod(m
, caddr_t
) + off
, cp
, count
);
1696 * Concatenate mbuf chain n to m.
1697 * Both chains must be of the same type (e.g. MT_DATA).
1698 * Any m_pkthdr is not updated.
1701 struct mbuf
*m
, struct mbuf
*n
)
1706 if (m
->m_flags
& M_EXT
||
1707 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
1708 /* just join the two chains */
1712 /* splat the data from one into the other */
1713 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1715 m
->m_len
+= n
->m_len
;
1729 if ((m
= mp
) == NULL
)
1735 while (m
!= NULL
&& len
> 0) {
1736 if (m
->m_len
<= len
) {
1747 if (m
->m_flags
& M_PKTHDR
)
1748 m
->m_pkthdr
.len
-= (req_len
- len
);
1751 * Trim from tail. Scan the mbuf chain,
1752 * calculating its length and finding the last mbuf.
1753 * If the adjustment only affects this mbuf, then just
1754 * adjust and return. Otherwise, rescan and truncate
1755 * after the remaining size.
1761 if (m
->m_next
== (struct mbuf
*)0)
1765 if (m
->m_len
>= len
) {
1768 if (m
->m_flags
& M_PKTHDR
)
1769 m
->m_pkthdr
.len
-= len
;
1776 * Correct length for chain is "count".
1777 * Find the mbuf with last data, adjust its length,
1778 * and toss data from remaining mbufs on chain.
1781 if (m
->m_flags
& M_PKTHDR
)
1782 m
->m_pkthdr
.len
= count
;
1783 for (; m
; m
= m
->m_next
) {
1784 if (m
->m_len
>= count
) {
1790 while ((m
= m
->m_next
))
1796 * Rearange an mbuf chain so that len bytes are contiguous
1797 * and in the data area of an mbuf (so that mtod and dtom
1798 * will work for a structure of size len). Returns the resulting
1799 * mbuf chain on success, frees it and returns null on failure.
1800 * If there is room, it will add up to max_protohdr-len extra bytes to the
1801 * contiguous region in an attempt to avoid being called next time.
1815 * If first mbuf has no cluster, and has room for len bytes
1816 * without shifting current data, pullup into it,
1817 * otherwise allocate a new mbuf to prepend to the chain.
1819 if ((n
->m_flags
& M_EXT
) == 0 &&
1820 n
->m_data
+ len
< &n
->m_dat
[MLEN
] && n
->m_next
) {
1821 if (n
->m_len
>= len
)
1829 MGET(m
, M_DONTWAIT
, n
->m_type
);
1833 if (n
->m_flags
& M_PKTHDR
) {
1834 M_COPY_PKTHDR(m
, n
);
1835 n
->m_flags
&= ~M_PKTHDR
;
1838 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
1840 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
1841 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1851 } while (len
> 0 && n
);
1865 * Partition an mbuf chain in two pieces, returning the tail --
1866 * all but the first len0 bytes. In case of failure, it returns NULL and
1867 * attempts to restore the chain to its original state.
1876 unsigned len
= len0
, remain
;
1878 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
1882 remain
= m
->m_len
- len
;
1883 if (m0
->m_flags
& M_PKTHDR
) {
1884 MGETHDR(n
, wait
, m0
->m_type
);
1887 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
1888 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
1889 m0
->m_pkthdr
.len
= len0
;
1890 if (m
->m_flags
& M_EXT
)
1892 if (remain
> MHLEN
) {
1893 /* m can't be the lead packet */
1895 n
->m_next
= m_split(m
, len
, wait
);
1896 if (n
->m_next
== 0) {
1902 MH_ALIGN(n
, remain
);
1903 } else if (remain
== 0) {
1908 MGET(n
, wait
, m
->m_type
);
1914 if (m
->m_flags
& M_EXT
) {
1915 n
->m_flags
|= M_EXT
;
1917 n
->m_ext
= m
->m_ext
;
1918 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1920 n
->m_data
= m
->m_data
+ len
;
1922 bcopy(mtod(m
, caddr_t
) + len
, mtod(n
, caddr_t
), remain
);
1926 n
->m_next
= m
->m_next
;
1931 * Routine to copy from device local memory into mbufs.
1939 void (*copy
)(const void *, void *, size_t))
1942 struct mbuf
*top
= 0, **mp
= &top
;
1943 int off
= off0
, len
;
1951 * If 'off' is non-zero, packet is trailer-encapsulated,
1952 * so we have to skip the type and length fields.
1954 cp
+= off
+ 2 * sizeof(u_int16_t
);
1955 totlen
-= 2 * sizeof(u_int16_t
);
1957 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1960 m
->m_pkthdr
.rcvif
= ifp
;
1961 m
->m_pkthdr
.len
= totlen
;
1964 while (totlen
> 0) {
1966 MGET(m
, M_DONTWAIT
, MT_DATA
);
1973 len
= min(totlen
, epkt
- cp
);
1974 if (len
>= MINCLSIZE
) {
1975 MCLGET(m
, M_DONTWAIT
);
1976 if (m
->m_flags
& M_EXT
)
1977 m
->m_len
= len
= min(len
, MCLBYTES
);
1979 /* give up when it's out of cluster mbufs */
1987 * Place initial small packet/header at end of mbuf.
1989 if (len
< m
->m_len
) {
1990 if (top
== 0 && len
+ max_linkhdr
<= m
->m_len
)
1991 m
->m_data
+= max_linkhdr
;
1997 copy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
1999 bcopy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
2011 * Cluster freelist allocation check. The mbuf lock must be held.
2012 * Ensure hysteresis between hi/lo.
2015 m_howmany(int num
, size_t bufsize
)
2019 /* Bail if we've maxed out the mbuf memory map */
2020 if (mbstat
.m_clusters
+ (mbstat
.m_bigclusters
<< 1) < nmbclusters
) {
2023 if (bufsize
== MCLBYTES
) {
2025 if (mbstat
.m_clusters
< MINCL
)
2026 return (MINCL
- mbstat
.m_clusters
);
2027 /* Too few (free < 1/2 total) and not over maximum */
2028 if (mbstat
.m_clusters
< (nmbclusters
>> 1)) {
2029 if (num
>= mbstat
.m_clfree
)
2030 i
= num
- mbstat
.m_clfree
;
2031 if (((mbstat
.m_clusters
+ num
) >> 1) > mbstat
.m_clfree
)
2032 j
= ((mbstat
.m_clusters
+ num
) >> 1) - mbstat
.m_clfree
;
2034 if (i
+ mbstat
.m_clusters
>= (nmbclusters
>> 1))
2035 i
= (nmbclusters
>> 1) - mbstat
.m_clusters
;
2039 if (mbstat
.m_bigclusters
< MINCL
)
2040 return (MINCL
- mbstat
.m_bigclusters
);
2041 /* Too few (free < 1/2 total) and not over maximum */
2042 if (mbstat
.m_bigclusters
< (nmbclusters
>> 2)) {
2043 if (num
>= mbstat
.m_bigclfree
)
2044 i
= num
- mbstat
.m_bigclfree
;
2045 if (((mbstat
.m_bigclusters
+ num
) >> 1) > mbstat
.m_bigclfree
)
2046 j
= ((mbstat
.m_bigclusters
+ num
) >> 1) - mbstat
.m_bigclfree
;
2048 if (i
+ mbstat
.m_bigclusters
>= (nmbclusters
>> 2))
2049 i
= (nmbclusters
>> 2) - mbstat
.m_bigclusters
;
2057 * Copy data from a buffer back into the indicated mbuf chain,
2058 * starting "off" bytes from the beginning, extending the mbuf
2059 * chain if necessary.
2069 struct mbuf
*m
= m0
, *n
;
2074 while (off
> (mlen
= m
->m_len
)) {
2077 if (m
->m_next
== 0) {
2078 n
= m_getclr(M_DONTWAIT
, m
->m_type
);
2081 n
->m_len
= min(MLEN
, len
+ off
);
2087 mlen
= min (m
->m_len
- off
, len
);
2088 bcopy(cp
, off
+ mtod(m
, caddr_t
), (unsigned)mlen
);
2096 if (m
->m_next
== 0) {
2097 n
= m_get(M_DONTWAIT
, m
->m_type
);
2100 n
->m_len
= min(MLEN
, len
);
2105 out
: if (((m
= m0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
))
2106 m
->m_pkthdr
.len
= totlen
;
2110 char *mcl_to_paddr(char *addr
) {
2113 if (addr
< (char *)mbutl
|| addr
>= (char *)embutl
)
2115 base_phys
= mcl_paddr
[(addr
- (char *)mbutl
) >> PGSHIFT
];
2119 return ((char *)((int)base_phys
| ((int)addr
& PGOFSET
)));
2123 * Dup the mbuf chain passed in. The whole thing. No cute additional cruft.
2124 * And really copy the thing. That way, we don't "precompute" checksums
2125 * for unsuspecting consumers.
2126 * Assumption: m->m_nextpkt == 0.
2127 * Trick: for small packets, don't dup into a cluster. That way received
2128 * packets don't take up too much room in the sockbuf (cf. sbspace()).
2133 m_dup(struct mbuf
*m
, int how
)
2135 struct mbuf
*n
, **np
;
2141 if (m
->m_flags
& M_PKTHDR
)
2145 * Quick check: if we have one mbuf and its data fits in an
2146 * mbuf with packet header, just copy and go.
2148 if (m
->m_next
== NULL
)
2149 { /* Then just move the data into an mbuf and be done... */
2151 { if (m
->m_pkthdr
.len
<= MHLEN
)
2152 { if ((n
= m_gethdr(how
, m
->m_type
)) == NULL
)
2154 n
->m_len
= m
->m_len
;
2155 m_dup_pkthdr(n
, m
, how
);
2156 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
2159 } else if (m
->m_len
<= MLEN
)
2160 { if ((n
= m_get(how
, m
->m_type
)) == NULL
)
2162 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
2163 n
->m_len
= m
->m_len
;
2170 kprintf("<%x: %x, %x, %x\n", m
, m
->m_flags
, m
->m_len
,
2174 n
= m_gethdr(how
, m
->m_type
);
2176 n
= m_get(how
, m
->m_type
);
2179 if (m
->m_flags
& M_EXT
)
2181 if ((n
->m_flags
& M_EXT
) == 0)
2186 { /* Don't use M_COPY_PKTHDR: preserve m_data */
2187 m_dup_pkthdr(n
, m
, how
);
2189 if ((n
->m_flags
& M_EXT
) == 0)
2190 n
->m_data
= n
->m_pktdat
;
2192 n
->m_len
= m
->m_len
;
2194 * Get the dup on the same bdry as the original
2195 * Assume that the two mbufs have the same offset to data area
2196 * (up to word bdries)
2198 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), (unsigned)n
->m_len
);
2202 kprintf(">%x: %x, %x, %x\n", n
, n
->m_flags
, n
->m_len
,
2217 m_mclref(struct mbuf
*p
)
2219 return (_MCLREF(p
));
2223 m_mclunref(struct mbuf
*p
)
2225 return (_MCLUNREF(p
));
2228 /* change mbuf to new type */
2230 m_mchtype(struct mbuf
*m
, int t
)
2233 mbstat
.m_mtypes
[(m
)->m_type
]--;
2234 mbstat
.m_mtypes
[t
]++;
2239 void *m_mtod(struct mbuf
*m
)
2241 return ((m
)->m_data
);
2244 struct mbuf
*m_dtom(void *x
)
2246 return ((struct mbuf
*)((u_long
)(x
) & ~(MSIZE
-1)));
2249 int m_mtocl(void *x
)
2251 return (((char *)(x
) - (char *)mbutl
) / sizeof(union mcluster
));
2254 union mcluster
*m_cltom(int x
)
2256 return ((union mcluster
*)(mbutl
+ (x
)));
2260 void m_mcheck(struct mbuf
*m
)
2262 if (m
->m_type
!= MT_FREE
)
2263 panic("mget MCHECK: m_type=%x m=%x", m
->m_type
, m
);
2267 mbuf_expand_thread(void)
2271 if (mbuf_expand_mcl
) {
2274 /* Adjust to the current number of cluster in use */
2275 n
= mbuf_expand_mcl
- (mbstat
.m_clusters
- mbstat
.m_clfree
);
2276 mbuf_expand_mcl
= 0;
2279 (void)m_clalloc(n
, M_WAIT
, MCLBYTES
, 1);
2281 if (mbuf_expand_big
) {
2284 /* Adjust to the current number of 4 KB cluster in use */
2285 n
= mbuf_expand_big
- (mbstat
.m_bigclusters
- mbstat
.m_bigclfree
);
2286 mbuf_expand_big
= 0;
2289 (void)m_clalloc(n
, M_WAIT
, NBPG
, 1);
2293 * Because we can run out of memory before filling the mbuf map, we
2294 * should not allocate more clusters than they are mbufs -- otherwise
2295 * we could have a large number of useless clusters allocated.
2297 while (mbstat
.m_mbufs
< mbstat
.m_bigclusters
+ mbstat
.m_clusters
) {
2298 if (m_expand(M_WAIT
) == 0)
2302 assert_wait(&mbuf_expand_thread_wakeup
, THREAD_UNINT
);
2303 (void) thread_block((thread_continue_t
)mbuf_expand_thread
);
2308 mbuf_expand_thread_init(void)
2310 mbuf_expand_thread_initialized
++;
2311 mbuf_expand_thread();
2314 SYSCTL_DECL(_kern_ipc
);
2315 SYSCTL_STRUCT(_kern_ipc
, KIPC_MBSTAT
, mbstat
, CTLFLAG_RW
, &mbstat
, mbstat
, "");