2 * Copyright (c) 2000-2005 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 __private_extern__
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();
228 mbuf_mlock_grp
= lck_grp_alloc_init("mbuf", mbuf_mlock_grp_attr
);
229 mbuf_mlock_attr
= lck_attr_alloc_init();
231 mbuf_mlock
= lck_mtx_alloc_init(mbuf_mlock_grp
, mbuf_mlock_attr
);
233 mbstat
.m_msize
= MSIZE
;
234 mbstat
.m_mclbytes
= MCLBYTES
;
235 mbstat
.m_minclsize
= MINCLSIZE
;
236 mbstat
.m_mlen
= MLEN
;
237 mbstat
.m_mhlen
= MHLEN
;
238 mbstat
.m_bigmclbytes
= NBPG
;
240 if (nmbclusters
== 0)
241 nmbclusters
= NMBCLUSTERS
;
242 MALLOC(mclrefcnt
, short *, nmbclusters
* sizeof (short),
246 for (m
= 0; m
< nmbclusters
; m
++)
249 /* Calculate the number of pages assigned to the cluster pool */
250 mcl_pages
= nmbclusters
/(NBPG
/CLBYTES
);
251 MALLOC(mcl_paddr
, int *, mcl_pages
* sizeof(int), M_TEMP
, M_WAITOK
);
254 /* Register with the I/O Bus mapper */
255 mcl_paddr_base
= IOMapperIOVMAlloc(mcl_pages
);
256 bzero((char *)mcl_paddr
, mcl_pages
* sizeof(int));
258 embutl
= (union mcluster
*)((unsigned char *)mbutl
+ (nmbclusters
* MCLBYTES
));
260 PE_parse_boot_arg("initmcl", &initmcl
);
262 if (m_clalloc(max(NBPG
/CLBYTES
, 1) * initmcl
, M_WAIT
, MCLBYTES
, 0) == 0)
266 (void) kernel_thread(kernel_task
, mbuf_expand_thread_init
);
274 * Allocate some number of mbuf clusters
275 * and place on cluster free list.
276 * Take the mbuf lock (if not already locked) and do not release it
283 const size_t bufsize
,
289 vm_offset_t page
= 0;
294 * Honor the caller's wish to block or not block.
295 * We have a way to grow the pool asynchronously,
296 * by kicking the dlil_input_thread.
298 i
= m_howmany(num
, bufsize
);
299 if (i
== 0 || nowait
== M_DONTWAIT
)
303 size
= round_page_32(i
* bufsize
);
304 page
= kmem_mb_alloc(mb_map
, size
);
307 size
= NBPG
; /* Try for 1 if failed */
308 page
= kmem_mb_alloc(mb_map
, size
);
313 numpages
= size
/ NBPG
;
314 for (i
= 0; i
< numpages
; i
++, page
+= NBPG
) {
315 if (((int)page
& PGOFSET
) == 0) {
316 ppnum_t offset
= ((char *)page
- (char *)mbutl
)/NBPG
;
317 ppnum_t new_page
= pmap_find_phys(kernel_pmap
, (vm_address_t
) page
);
320 * In the case of no mapper being available
321 * the following code nops and returns the
322 * input page, if there is a mapper the I/O
323 * page appropriate is returned.
325 new_page
= IOMapperInsertPage(mcl_paddr_base
, offset
, new_page
);
326 mcl_paddr
[offset
] = new_page
<< 12;
328 if (bufsize
== MCLBYTES
) {
329 union mcluster
*mcl
= (union mcluster
*)page
;
331 if (++mclrefcnt
[mtocl(mcl
)] != 0)
332 panic("m_clalloc already there");
333 mcl
->mcl_next
= mclfree
;
335 if (++mclrefcnt
[mtocl(mcl
)] != 0)
336 panic("m_clalloc already there");
337 mcl
->mcl_next
= mclfree
;
340 union mbigcluster
*mbc
= (union mbigcluster
*)page
;
342 if (++mclrefcnt
[mtocl(mbc
)] != 0)
343 panic("m_clalloc already there");
344 if (++mclrefcnt
[mtocl(mbc
) + 1] != 0)
345 panic("m_clalloc already there");
347 mbc
->mbc_next
= mbigfree
;
351 if (bufsize
== MCLBYTES
) {
352 int numcl
= numpages
<< 1;
353 mbstat
.m_clfree
+= numcl
;
354 mbstat
.m_clusters
+= numcl
;
357 mbstat
.m_bigclfree
+= numpages
;
358 mbstat
.m_bigclusters
+= numpages
;
364 * When non-blocking we kick a thread if we havve to grow the
365 * pool or if the number of free clusters is less than requested.
367 if (bufsize
== MCLBYTES
) {
369 /* Remember total number of clusters needed at this time */
370 i
+= mbstat
.m_clusters
;
371 if (i
> mbuf_expand_mcl
) {
373 if (mbuf_expand_thread_initialized
)
374 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
378 if (mbstat
.m_clfree
>= num
)
382 /* Remember total number of 4KB clusters needed at this time */
383 i
+= mbstat
.m_bigclusters
;
384 if (i
> mbuf_expand_big
) {
386 if (mbuf_expand_thread_initialized
)
387 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
391 if (mbstat
.m_bigclfree
>= num
)
398 * Add more free mbufs by cutting up a cluster.
401 m_expand(int canwait
)
405 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4)) {
407 * 1/16th of the total number of cluster mbufs allocated is
408 * reserved for large packets. The number reserved must
409 * always be < 1/2, or future allocation will be prevented.
411 (void)m_clalloc(1, canwait
, MCLBYTES
, 0);
413 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4))
417 MCLALLOC(mcl
, canwait
);
419 struct mbuf
*m
= (struct mbuf
*)mcl
;
422 mbstat
.m_mtypes
[MT_FREE
] += i
;
433 if (i
) wakeup((caddr_t
)&mfree
);
440 * When MGET failes, ask protocols to free space when short of memory,
441 * then re-attempt to allocate an mbuf.
452 (void) m_expand(canwait
);
455 (m
)->m_next
= (m
)->m_nextpkt
= 0;
456 (m
)->m_type
= (type
);
457 (m
)->m_data
= (m
)->m_dat
;
461 if (m
|| canwait
== M_DONTWAIT
)
472 if (mbuf_expand_thread_initialized
)
473 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
481 (void) msleep((caddr_t
)&mfree
, 0, (PZERO
-1) | PDROP
, "m_retry", &ts
);
490 * As above; retry an MGETHDR.
499 if ((m
= m_retry(canwait
, type
))) {
500 m
->m_next
= m
->m_nextpkt
= 0;
501 m
->m_flags
|= M_PKTHDR
;
502 m
->m_data
= m
->m_pktdat
;
511 do_reclaim
= 1; /* drain is performed in pfslowtimo(), to avoid deadlocks */
516 * Space allocation routines.
517 * These are also available as macros
518 * for critical paths.
527 m_range_check(mfree
);
528 m_range_check(mclfree
);
529 m_range_check(mbigfree
);
533 m
->m_next
= m
->m_nextpkt
= 0;
535 m
->m_data
= m
->m_dat
;
539 (m
) = m_retry(nowait
, type
);
541 m_range_check(mfree
);
542 m_range_check(mclfree
);
543 m_range_check(mbigfree
);
556 m_range_check(mfree
);
557 m_range_check(mclfree
);
558 m_range_check(mbigfree
);
563 m
->m_next
= m
->m_nextpkt
= 0;
565 m
->m_data
= m
->m_pktdat
;
566 m
->m_flags
= M_PKTHDR
;
570 m
= m_retryhdr(nowait
, type
);
572 m_range_check(mfree
);
573 m_range_check(mclfree
);
574 m_range_check(mbigfree
);
587 MGET(m
, nowait
, type
);
590 bzero(mtod(m
, caddr_t
), MLEN
);
598 struct mbuf
*n
= m
->m_next
;
602 m_range_check(mfree
);
603 m_range_check(mclfree
);
605 if (m
->m_type
== MT_FREE
)
606 panic("freeing free mbuf");
608 /* Free the aux data if there is any */
609 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
)
611 m_freem(m
->m_pkthdr
.aux
);
613 if ((m
->m_flags
& M_PKTHDR
) != 0)
614 m_tag_delete_chain(m
, NULL
);
617 if ((m
->m_flags
& M_EXT
))
619 if (MCLHASREFERENCE(m
)) {
620 remque((queue_t
)&m
->m_ext
.ext_refs
);
621 } else if (m
->m_ext
.ext_free
== NULL
) {
622 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
626 if (_MCLUNREF(mcl
)) {
627 mcl
->mcl_next
= mclfree
;
632 /* *** Since m_split() increments "mclrefcnt[mtocl(m->m_ext.ext_buf)]",
633 and AppleTalk ADSP uses m_split(), this incorrect sanity check
636 else /* sanity check - not referenced this way */
637 panic("m_free m_ext cluster not free");
640 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
641 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
644 mbstat
.m_mtypes
[m
->m_type
]--;
648 mbstat
.m_mtypes
[m
->m_type
]++;
656 if (i
) wakeup((caddr_t
)&mfree
);
660 /* m_mclget() add an mbuf cluster to a normal mbuf */
666 MCLALLOC(m
->m_ext
.ext_buf
, nowait
);
667 if (m
->m_ext
.ext_buf
) {
668 m
->m_data
= m
->m_ext
.ext_buf
;
670 m
->m_ext
.ext_size
= MCLBYTES
;
671 m
->m_ext
.ext_free
= 0;
672 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
679 /* m_mclalloc() allocate an mbuf cluster */
686 (void)m_clalloc(1, nowait
, MCLBYTES
, 0);
687 if ((p
= (caddr_t
)mclfree
)) {
688 ++mclrefcnt
[mtocl(p
)];
690 mclfree
= ((union mcluster
*)p
)->mcl_next
;
699 /* m_mclfree() releases a reference to a cluster allocated by MCLALLOC,
700 * freeing the cluster if the reference count has reached 0. */
709 if (--mclrefcnt
[mtocl(p
)] == 0) {
710 ((union mcluster
*)(p
))->mcl_next
= mclfree
;
711 mclfree
= (union mcluster
*)(p
);
717 /* mcl_hasreference() checks if a cluster of an mbuf is referenced by another mbuf */
722 return (m
->m_ext
.ext_refs
.forward
!= &(m
->m_ext
.ext_refs
));
725 __private_extern__ caddr_t
726 m_bigalloc(int nowait
)
730 (void)m_clalloc(1, nowait
, NBPG
, 0);
731 if ((p
= (caddr_t
)mbigfree
)) {
732 if (mclrefcnt
[mtocl(p
)] != mclrefcnt
[mtocl(p
) + 1])
733 panic("m_bigalloc mclrefcnt %x mismatch %d != %d",
734 p
, mclrefcnt
[mtocl(p
)], mclrefcnt
[mtocl(p
) + 1]);
735 if (mclrefcnt
[mtocl(p
)] || mclrefcnt
[mtocl(p
) + 1])
736 panic("m_bigalloc mclrefcnt %x not null %d != %d",
737 p
, mclrefcnt
[mtocl(p
)], mclrefcnt
[mtocl(p
) + 1]);
738 ++mclrefcnt
[mtocl(p
)];
739 ++mclrefcnt
[mtocl(p
) + 1];
740 mbstat
.m_bigclfree
--;
741 mbigfree
= ((union mbigcluster
*)p
)->mbc_next
;
749 __private_extern__
void
750 m_bigfree(caddr_t p
, __unused u_int size
, __unused caddr_t arg
)
754 if (mclrefcnt
[mtocl(p
)] != mclrefcnt
[mtocl(p
) + 1])
755 panic("m_bigfree mclrefcnt %x mismatch %d != %d",
756 p
, mclrefcnt
[mtocl(p
)], mclrefcnt
[mtocl(p
) + 1]);
757 --mclrefcnt
[mtocl(p
)];
758 --mclrefcnt
[mtocl(p
) + 1];
759 if (mclrefcnt
[mtocl(p
)] == 0) {
760 ((union mbigcluster
*)(p
))->mbc_next
= mbigfree
;
761 mbigfree
= (union mbigcluster
*)(p
);
762 mbstat
.m_bigclfree
++;
766 /* m_mbigget() add an 4KB mbuf cluster to a normal mbuf */
767 __private_extern__
struct mbuf
*
768 m_mbigget(struct mbuf
*m
, int nowait
)
770 m
->m_ext
.ext_buf
= m_bigalloc(nowait
);
771 if (m
->m_ext
.ext_buf
) {
772 m
->m_data
= m
->m_ext
.ext_buf
;
774 m
->m_ext
.ext_size
= NBPG
;
775 m
->m_ext
.ext_free
= m_bigfree
;
776 m
->m_ext
.ext_arg
= 0;
777 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
791 to
->m_pkthdr
= from
->m_pkthdr
;
792 from
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
793 SLIST_INIT(&from
->m_pkthdr
.tags
); /* purge tags from src */
794 to
->m_flags
= from
->m_flags
& M_COPYFLAGS
;
795 to
->m_data
= (to
)->m_pktdat
;
799 * "Move" mbuf pkthdr from "from" to "to".
800 * "from" must have M_PKTHDR set, and "to" must be empty.
804 m_move_pkthdr(struct mbuf
*to
, struct mbuf
*from
)
806 KASSERT((to
->m_flags
& M_EXT
) == 0, ("m_move_pkthdr: to has cluster"));
808 to
->m_flags
= from
->m_flags
& M_COPYFLAGS
;
809 to
->m_data
= to
->m_pktdat
;
810 to
->m_pkthdr
= from
->m_pkthdr
; /* especially tags */
811 SLIST_INIT(&from
->m_pkthdr
.tags
); /* purge tags from src */
812 from
->m_flags
&= ~M_PKTHDR
;
817 * Duplicate "from"'s mbuf pkthdr in "to".
818 * "from" must have M_PKTHDR set, and "to" must be empty.
819 * In particular, this does a deep copy of the packet tags.
822 m_dup_pkthdr(struct mbuf
*to
, struct mbuf
*from
, int how
)
824 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) | (to
->m_flags
& M_EXT
);
825 if ((to
->m_flags
& M_EXT
) == 0)
826 to
->m_data
= to
->m_pktdat
;
827 to
->m_pkthdr
= from
->m_pkthdr
;
828 SLIST_INIT(&to
->m_pkthdr
.tags
);
829 return (m_tag_copy_chain(to
, from
, how
));
833 * return a list of mbuf hdrs that point to clusters...
834 * try for num_needed, if wantall is not set, return whatever
835 * number were available... set up the first num_with_pkthdrs
836 * with mbuf hdrs configured as packet headers... these are
837 * chained on the m_nextpkt field... any packets requested beyond
838 * this are chained onto the last packet header's m_next field.
839 * The size of the cluster is controlled by the paramter bufsize.
841 __private_extern__
struct mbuf
*
842 m_getpackets_internal(unsigned int *num_needed
, int num_with_pkthdrs
, int how
, int wantall
, size_t bufsize
)
845 struct mbuf
**np
, *top
;
846 unsigned int num
, needed
= *num_needed
;
848 if (bufsize
!= MCLBYTES
&& bufsize
!= NBPG
)
854 (void)m_clalloc(needed
, how
, bufsize
, 0); /* takes the MBUF_LOCK, but doesn't release it... */
856 for (num
= 0; num
< needed
; num
++) {
857 m_range_check(mfree
);
858 m_range_check(mclfree
);
859 m_range_check(mbigfree
);
861 if (mfree
&& ((bufsize
== NBPG
&& mbigfree
) || (bufsize
== MCLBYTES
&& mclfree
))) {
862 /* mbuf + cluster are available */
866 ++mclrefcnt
[mtocl(m
)];
867 mbstat
.m_mtypes
[MT_FREE
]--;
868 mbstat
.m_mtypes
[MT_DATA
]++;
869 if (bufsize
== NBPG
) {
870 m
->m_ext
.ext_buf
= (caddr_t
)mbigfree
; /* get the big cluster */
871 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
872 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
) + 1];
873 mbstat
.m_bigclfree
--;
874 mbigfree
= ((union mbigcluster
*)(m
->m_ext
.ext_buf
))->mbc_next
;
875 m
->m_ext
.ext_free
= m_bigfree
;
876 m
->m_ext
.ext_size
= NBPG
;
878 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
879 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
881 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
882 m
->m_ext
.ext_free
= 0;
883 m
->m_ext
.ext_size
= MCLBYTES
;
885 m
->m_ext
.ext_arg
= 0;
886 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
887 m
->m_next
= m
->m_nextpkt
= 0;
889 m
->m_data
= m
->m_ext
.ext_buf
;
892 if (num_with_pkthdrs
== 0)
895 m
->m_flags
= M_PKTHDR
| M_EXT
;
903 if (num_with_pkthdrs
== 0) {
904 MGET(m
, how
, MT_DATA
);
906 MGETHDR(m
, how
, MT_DATA
);
914 m
= m_mbigget(m
, how
);
916 m
= m_mclget(m
, how
);
917 if ((m
->m_flags
& M_EXT
) == 0) {
925 if (num_with_pkthdrs
)
935 if (wantall
&& top
) {
944 * Return list of mbuf linked by m_nextpkt
945 * Try for num_needed, and if wantall is not set, return whatever
946 * number were available
947 * The size of each mbuf in the list is controlled by the parameter packetlen.
948 * Each mbuf of the list may have a chain of mbufs linked by m_next. Each mbuf in
949 * the chain is called a segment.
950 * If maxsegments is not null and the value pointed to is not null, this specify
951 * the maximum number of segments for a chain of mbufs.
952 * If maxsegments is zero or the value pointed to is zero the
953 * caller does not have any restriction on the number of segments.
954 * The actual number of segments of a mbuf chain is return in the value pointed
956 * When possible the allocation is done under a single lock.
959 __private_extern__
struct mbuf
*
960 m_allocpacket_internal(unsigned int *num_needed
, size_t packetlen
, unsigned int * maxsegments
,
961 int how
, int wantall
, size_t wantsize
)
963 struct mbuf
**np
, *top
;
966 unsigned int numchunks
= 0;
972 if (packetlen
<= MINCLSIZE
)
974 else if (packetlen
> MCLBYTES
)
978 } else if (wantsize
== MCLBYTES
|| wantsize
== NBPG
)
983 if (bufsize
<= MHLEN
) {
985 } else if (bufsize
<= MINCLSIZE
) {
986 if (maxsegments
!= NULL
&& *maxsegments
== 1) {
992 } else if (bufsize
== NBPG
) {
993 numchunks
= ((packetlen
- 1) >> PGSHIFT
) + 1;
995 numchunks
= ((packetlen
- 1) >> MCLSHIFT
) + 1;
997 if (maxsegments
!= NULL
) {
998 if (*maxsegments
&& numchunks
> *maxsegments
) {
999 *maxsegments
= numchunks
;
1002 *maxsegments
= numchunks
;
1004 /* m_clalloc takes the MBUF_LOCK, but do not release it */
1005 (void)m_clalloc(numchunks
, how
, (bufsize
== NBPG
) ? NBPG
: MCLBYTES
, 0);
1006 for (num
= 0; num
< *num_needed
; num
++) {
1007 struct mbuf
**nm
, *pkt
= 0;
1012 m_range_check(mfree
);
1013 m_range_check(mclfree
);
1014 m_range_check(mbigfree
);
1016 for (len
= 0; len
< packetlen
; ) {
1017 struct mbuf
*m
= NULL
;
1019 if (wantsize
== 0 && packetlen
> MINCLSIZE
) {
1020 if (packetlen
- len
> MCLBYTES
)
1027 if (mfree
&& ((bufsize
== NBPG
&& mbigfree
) || (bufsize
== MCLBYTES
&& mclfree
))) {
1028 /* mbuf + cluster are available */
1032 ++mclrefcnt
[mtocl(m
)];
1033 mbstat
.m_mtypes
[MT_FREE
]--;
1034 mbstat
.m_mtypes
[MT_DATA
]++;
1035 if (bufsize
== NBPG
) {
1036 m
->m_ext
.ext_buf
= (caddr_t
)mbigfree
; /* get the big cluster */
1037 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
1038 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
) + 1];
1039 mbstat
.m_bigclfree
--;
1040 mbigfree
= ((union mbigcluster
*)(m
->m_ext
.ext_buf
))->mbc_next
;
1041 m
->m_ext
.ext_free
= m_bigfree
;
1042 m
->m_ext
.ext_size
= NBPG
;
1044 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
1045 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
1047 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
1048 m
->m_ext
.ext_free
= 0;
1049 m
->m_ext
.ext_size
= MCLBYTES
;
1051 m
->m_ext
.ext_arg
= 0;
1052 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
1053 m
->m_next
= m
->m_nextpkt
= 0;
1054 m
->m_type
= MT_DATA
;
1055 m
->m_data
= m
->m_ext
.ext_buf
;
1060 m
->m_flags
= M_PKTHDR
| M_EXT
;
1069 MGETHDR(m
, how
, MT_DATA
);
1071 MGET(m
, how
, MT_DATA
);
1077 if (bufsize
<= MINCLSIZE
) {
1078 if (bufsize
> MHLEN
) {
1079 MGET(m
->m_next
, how
, MT_DATA
);
1080 if (m
->m_next
== 0) {
1087 if (bufsize
== NBPG
)
1088 m
= m_mbigget(m
, how
);
1090 m
= m_mclget(m
, how
);
1091 if ((m
->m_flags
& M_EXT
) == 0) {
1103 np
= &pkt
->m_nextpkt
;
1110 if (wantall
&& top
) {
1120 /* Best effort to get a mbuf cluster + pkthdr under one lock.
1121 * If we don't have them avail, just bail out and use the regular
1123 * Used by drivers to allocated packets on receive ring.
1125 __private_extern__
struct mbuf
*
1126 m_getpacket_how(int how
)
1128 unsigned int num_needed
= 1;
1130 return m_getpackets_internal(&num_needed
, 1, how
, 1, MCLBYTES
);
1133 /* Best effort to get a mbuf cluster + pkthdr under one lock.
1134 * If we don't have them avail, just bail out and use the regular
1136 * Used by drivers to allocated packets on receive ring.
1141 unsigned int num_needed
= 1;
1143 return m_getpackets_internal(&num_needed
, 1, M_WAITOK
, 1, MCLBYTES
);
1148 * return a list of mbuf hdrs that point to clusters...
1149 * try for num_needed, if this can't be met, return whatever
1150 * number were available... set up the first num_with_pkthdrs
1151 * with mbuf hdrs configured as packet headers... these are
1152 * chained on the m_nextpkt field... any packets requested beyond
1153 * this are chained onto the last packet header's m_next field.
1156 m_getpackets(int num_needed
, int num_with_pkthdrs
, int how
)
1158 unsigned int n
= num_needed
;
1160 return m_getpackets_internal(&n
, num_with_pkthdrs
, how
, 0, MCLBYTES
);
1165 * return a list of mbuf hdrs set up as packet hdrs
1166 * chained together on the m_nextpkt field
1169 m_getpackethdrs(int num_needed
, int how
)
1172 struct mbuf
**np
, *top
;
1179 while (num_needed
--) {
1180 m_range_check(mfree
);
1181 m_range_check(mclfree
);
1182 m_range_check(mbigfree
);
1184 if ((m
= mfree
)) { /* mbufs are available */
1187 ++mclrefcnt
[mtocl(m
)];
1188 mbstat
.m_mtypes
[MT_FREE
]--;
1189 mbstat
.m_mtypes
[MT_DATA
]++;
1191 m
->m_next
= m
->m_nextpkt
= 0;
1192 m
->m_type
= MT_DATA
;
1193 m
->m_flags
= M_PKTHDR
;
1195 m
->m_data
= m
->m_pktdat
;
1201 m
= m_retryhdr(how
, MT_DATA
);
1215 /* free and mbuf list (m_nextpkt) while following m_next under one lock.
1216 * returns the count for mbufs packets freed. Used by the drivers.
1222 struct mbuf
*nextpkt
;
1229 nextpkt
= m
->m_nextpkt
; /* chain of linked mbufs from driver */
1235 while (m
) { /* free the mbuf chain (like mfreem) */
1240 m_range_check(mfree
);
1241 m_range_check(mclfree
);
1242 m_range_check(mbigfree
);
1245 /* Free the aux data if there is any */
1246 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
) {
1248 * Treat the current m as the nextpkt and set m
1249 * to the aux data. Preserve nextpkt in m->m_nextpkt.
1250 * This lets us free the aux data in this loop
1251 * without having to call m_freem recursively,
1252 * which wouldn't work because we've still got
1255 m
->m_nextpkt
= nextpkt
;
1257 m
= nextpkt
->m_pkthdr
.aux
;
1258 nextpkt
->m_pkthdr
.aux
= NULL
;
1261 if ((m
->m_flags
& M_PKTHDR
) != 0 && !SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
1262 /* A quick (albeit inefficient) expedient */
1264 m_tag_delete_chain(m
, NULL
);
1270 if (n
&& n
->m_nextpkt
)
1271 panic("m_freem_list: m_nextpkt of m_next != NULL");
1272 if (m
->m_type
== MT_FREE
)
1273 panic("freeing free mbuf");
1275 if (m
->m_flags
& M_EXT
) {
1276 if (MCLHASREFERENCE(m
)) {
1277 remque((queue_t
)&m
->m_ext
.ext_refs
);
1278 } else if (m
->m_ext
.ext_free
== NULL
) {
1279 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
1283 if (_MCLUNREF(mcl
)) {
1284 mcl
->mcl_next
= mclfree
;
1289 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
1290 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
1293 mbstat
.m_mtypes
[m
->m_type
]--;
1294 (void) _MCLUNREF(m
);
1296 mbstat
.m_mtypes
[MT_FREE
]++;
1297 m
->m_type
= MT_FREE
;
1304 m
= nextpkt
; /* bump m with saved nextpkt if any */
1312 wakeup((caddr_t
)&mfree
);
1326 * Mbuffer utility routines.
1329 * Compute the amount of space available
1330 * before the current start of data in an mbuf.
1336 if (m
->m_flags
& M_EXT
) {
1337 if (MCLHASREFERENCE(m
))
1339 return (m
->m_data
- m
->m_ext
.ext_buf
);
1341 if (m
->m_flags
& M_PKTHDR
)
1342 return (m
->m_data
- m
->m_pktdat
);
1343 return (m
->m_data
- m
->m_dat
);
1347 * Compute the amount of space available
1348 * after the end of data in an mbuf.
1354 if (m
->m_flags
& M_EXT
) {
1355 if (MCLHASREFERENCE(m
))
1357 return (m
->m_ext
.ext_buf
+ m
->m_ext
.ext_size
-
1358 (m
->m_data
+ m
->m_len
));
1360 return (&m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
));
1364 * Lesser-used path for M_PREPEND:
1365 * allocate new mbuf to prepend to chain,
1367 * Does not adjust packet header length.
1377 MGET(mn
, how
, m
->m_type
);
1378 if (mn
== (struct mbuf
*)NULL
) {
1380 return ((struct mbuf
*)NULL
);
1382 if (m
->m_flags
& M_PKTHDR
) {
1383 M_COPY_PKTHDR(mn
, m
);
1384 m
->m_flags
&= ~M_PKTHDR
;
1395 * Replacement for old M_PREPEND macro:
1396 * allocate new mbuf to prepend to chain,
1397 * copy junk along, and adjust length.
1406 if (M_LEADINGSPACE(m
) >= len
) {
1410 m
= m_prepend(m
, len
, how
);
1412 if ((m
) && (m
->m_flags
& M_PKTHDR
))
1413 m
->m_pkthdr
.len
+= len
;
1418 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
1419 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
1420 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
1431 struct mbuf
*n
, **np
;
1436 if (off
< 0 || len
< 0)
1438 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1441 while (off
>= m
->m_len
) {
1453 m_range_check(mfree
);
1454 m_range_check(mclfree
);
1455 m_range_check(mbigfree
);
1458 if (len
!= M_COPYALL
)
1464 ++mclrefcnt
[mtocl(n
)];
1465 mbstat
.m_mtypes
[MT_FREE
]--;
1466 mbstat
.m_mtypes
[m
->m_type
]++;
1468 n
->m_next
= n
->m_nextpkt
= 0;
1469 n
->m_type
= m
->m_type
;
1470 n
->m_data
= n
->m_dat
;
1474 n
= m_retry(wait
, m
->m_type
);
1482 M_COPY_PKTHDR(n
, m
);
1483 if (len
== M_COPYALL
)
1484 n
->m_pkthdr
.len
-= off0
;
1486 n
->m_pkthdr
.len
= len
;
1489 if (len
== M_COPYALL
) {
1490 if (min(len
, (m
->m_len
- off
)) == len
) {
1491 printf("m->m_len %d - off %d = %d, %d\n",
1492 m
->m_len
, off
, m
->m_len
- off
,
1493 min(len
, (m
->m_len
- off
)));
1496 n
->m_len
= min(len
, (m
->m_len
- off
));
1497 if (n
->m_len
== M_COPYALL
) {
1498 printf("n->m_len == M_COPYALL, fixing\n");
1501 if (m
->m_flags
& M_EXT
) {
1502 n
->m_ext
= m
->m_ext
;
1503 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1504 n
->m_data
= m
->m_data
+ off
;
1505 n
->m_flags
|= M_EXT
;
1507 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1508 (unsigned)n
->m_len
);
1510 if (len
!= M_COPYALL
)
1532 * equivilent to m_copym except that all necessary
1533 * mbuf hdrs are allocated within this routine
1534 * also, the last mbuf and offset accessed are passed
1535 * out and can be passed back in to avoid having to
1536 * rescan the entire mbuf list (normally hung off of the socket)
1544 struct mbuf
**m_last
,
1547 struct mbuf
*n
, **np
= 0;
1549 struct mbuf
*top
= 0;
1553 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1560 while (off
>= m
->m_len
) {
1569 m_range_check(mfree
);
1570 m_range_check(mclfree
);
1571 m_range_check(mbigfree
);
1577 panic("m_gethdr_and_copym");
1582 ++mclrefcnt
[mtocl(n
)];
1583 mbstat
.m_mtypes
[MT_FREE
]--;
1584 mbstat
.m_mtypes
[type
]++;
1586 n
->m_next
= n
->m_nextpkt
= 0;
1590 n
->m_data
= n
->m_dat
;
1593 n
->m_data
= n
->m_pktdat
;
1594 n
->m_flags
= M_PKTHDR
;
1600 n
= m_retry(wait
, type
);
1602 n
= m_retryhdr(wait
, type
);
1615 M_COPY_PKTHDR(n
, m
);
1616 n
->m_pkthdr
.len
= len
;
1619 n
->m_len
= min(len
, (m
->m_len
- off
));
1621 if (m
->m_flags
& M_EXT
) {
1622 n
->m_ext
= m
->m_ext
;
1623 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1624 n
->m_data
= m
->m_data
+ off
;
1625 n
->m_flags
|= M_EXT
;
1627 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1628 (unsigned)n
->m_len
);
1633 if ((off
+ n
->m_len
) == m
->m_len
) {
1634 *m_last
= m
->m_next
;
1638 *m_off
= off
+ n
->m_len
;
1660 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1661 * continuing for "len" bytes, into the indicated buffer.
1671 if (off
< 0 || len
< 0)
1672 panic("m_copydata");
1675 panic("m_copydata");
1683 panic("m_copydata");
1684 count
= min(m
->m_len
- off
, len
);
1685 bcopy(mtod(m
, caddr_t
) + off
, cp
, count
);
1694 * Concatenate mbuf chain n to m.
1695 * Both chains must be of the same type (e.g. MT_DATA).
1696 * Any m_pkthdr is not updated.
1699 struct mbuf
*m
, struct mbuf
*n
)
1704 if (m
->m_flags
& M_EXT
||
1705 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
1706 /* just join the two chains */
1710 /* splat the data from one into the other */
1711 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1713 m
->m_len
+= n
->m_len
;
1727 if ((m
= mp
) == NULL
)
1733 while (m
!= NULL
&& len
> 0) {
1734 if (m
->m_len
<= len
) {
1745 if (m
->m_flags
& M_PKTHDR
)
1746 m
->m_pkthdr
.len
-= (req_len
- len
);
1749 * Trim from tail. Scan the mbuf chain,
1750 * calculating its length and finding the last mbuf.
1751 * If the adjustment only affects this mbuf, then just
1752 * adjust and return. Otherwise, rescan and truncate
1753 * after the remaining size.
1759 if (m
->m_next
== (struct mbuf
*)0)
1763 if (m
->m_len
>= len
) {
1766 if (m
->m_flags
& M_PKTHDR
)
1767 m
->m_pkthdr
.len
-= len
;
1774 * Correct length for chain is "count".
1775 * Find the mbuf with last data, adjust its length,
1776 * and toss data from remaining mbufs on chain.
1779 if (m
->m_flags
& M_PKTHDR
)
1780 m
->m_pkthdr
.len
= count
;
1781 for (; m
; m
= m
->m_next
) {
1782 if (m
->m_len
>= count
) {
1788 while ((m
= m
->m_next
))
1794 * Rearange an mbuf chain so that len bytes are contiguous
1795 * and in the data area of an mbuf (so that mtod and dtom
1796 * will work for a structure of size len). Returns the resulting
1797 * mbuf chain on success, frees it and returns null on failure.
1798 * If there is room, it will add up to max_protohdr-len extra bytes to the
1799 * contiguous region in an attempt to avoid being called next time.
1813 * If first mbuf has no cluster, and has room for len bytes
1814 * without shifting current data, pullup into it,
1815 * otherwise allocate a new mbuf to prepend to the chain.
1817 if ((n
->m_flags
& M_EXT
) == 0 &&
1818 n
->m_data
+ len
< &n
->m_dat
[MLEN
] && n
->m_next
) {
1819 if (n
->m_len
>= len
)
1827 MGET(m
, M_DONTWAIT
, n
->m_type
);
1831 if (n
->m_flags
& M_PKTHDR
) {
1832 M_COPY_PKTHDR(m
, n
);
1833 n
->m_flags
&= ~M_PKTHDR
;
1836 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
1838 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
1839 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1849 } while (len
> 0 && n
);
1863 * Partition an mbuf chain in two pieces, returning the tail --
1864 * all but the first len0 bytes. In case of failure, it returns NULL and
1865 * attempts to restore the chain to its original state.
1874 unsigned len
= len0
, remain
;
1876 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
1880 remain
= m
->m_len
- len
;
1881 if (m0
->m_flags
& M_PKTHDR
) {
1882 MGETHDR(n
, wait
, m0
->m_type
);
1885 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
1886 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
1887 m0
->m_pkthdr
.len
= len0
;
1888 if (m
->m_flags
& M_EXT
)
1890 if (remain
> MHLEN
) {
1891 /* m can't be the lead packet */
1893 n
->m_next
= m_split(m
, len
, wait
);
1894 if (n
->m_next
== 0) {
1900 MH_ALIGN(n
, remain
);
1901 } else if (remain
== 0) {
1906 MGET(n
, wait
, m
->m_type
);
1912 if (m
->m_flags
& M_EXT
) {
1913 n
->m_flags
|= M_EXT
;
1915 n
->m_ext
= m
->m_ext
;
1916 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1918 n
->m_data
= m
->m_data
+ len
;
1920 bcopy(mtod(m
, caddr_t
) + len
, mtod(n
, caddr_t
), remain
);
1924 n
->m_next
= m
->m_next
;
1929 * Routine to copy from device local memory into mbufs.
1937 void (*copy
)(const void *, void *, size_t))
1940 struct mbuf
*top
= 0, **mp
= &top
;
1941 int off
= off0
, len
;
1949 * If 'off' is non-zero, packet is trailer-encapsulated,
1950 * so we have to skip the type and length fields.
1952 cp
+= off
+ 2 * sizeof(u_int16_t
);
1953 totlen
-= 2 * sizeof(u_int16_t
);
1955 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1958 m
->m_pkthdr
.rcvif
= ifp
;
1959 m
->m_pkthdr
.len
= totlen
;
1962 while (totlen
> 0) {
1964 MGET(m
, M_DONTWAIT
, MT_DATA
);
1971 len
= min(totlen
, epkt
- cp
);
1972 if (len
>= MINCLSIZE
) {
1973 MCLGET(m
, M_DONTWAIT
);
1974 if (m
->m_flags
& M_EXT
)
1975 m
->m_len
= len
= min(len
, MCLBYTES
);
1977 /* give up when it's out of cluster mbufs */
1985 * Place initial small packet/header at end of mbuf.
1987 if (len
< m
->m_len
) {
1988 if (top
== 0 && len
+ max_linkhdr
<= m
->m_len
)
1989 m
->m_data
+= max_linkhdr
;
1995 copy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
1997 bcopy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
2009 * Cluster freelist allocation check. The mbuf lock must be held.
2010 * Ensure hysteresis between hi/lo.
2013 m_howmany(int num
, size_t bufsize
)
2017 /* Bail if we've maxed out the mbuf memory map */
2018 if (mbstat
.m_clusters
+ (mbstat
.m_bigclusters
<< 1) < nmbclusters
) {
2021 if (bufsize
== MCLBYTES
) {
2023 if (mbstat
.m_clusters
< MINCL
)
2024 return (MINCL
- mbstat
.m_clusters
);
2025 /* Too few (free < 1/2 total) and not over maximum */
2026 if (mbstat
.m_clusters
< (nmbclusters
>> 1)) {
2027 if (num
>= mbstat
.m_clfree
)
2028 i
= num
- mbstat
.m_clfree
;
2029 if (((mbstat
.m_clusters
+ num
) >> 1) > mbstat
.m_clfree
)
2030 j
= ((mbstat
.m_clusters
+ num
) >> 1) - mbstat
.m_clfree
;
2032 if (i
+ mbstat
.m_clusters
>= (nmbclusters
>> 1))
2033 i
= (nmbclusters
>> 1) - mbstat
.m_clusters
;
2037 if (mbstat
.m_bigclusters
< MINCL
)
2038 return (MINCL
- mbstat
.m_bigclusters
);
2039 /* Too few (free < 1/2 total) and not over maximum */
2040 if (mbstat
.m_bigclusters
< (nmbclusters
>> 2)) {
2041 if (num
>= mbstat
.m_bigclfree
)
2042 i
= num
- mbstat
.m_bigclfree
;
2043 if (((mbstat
.m_bigclusters
+ num
) >> 1) > mbstat
.m_bigclfree
)
2044 j
= ((mbstat
.m_bigclusters
+ num
) >> 1) - mbstat
.m_bigclfree
;
2046 if (i
+ mbstat
.m_bigclusters
>= (nmbclusters
>> 2))
2047 i
= (nmbclusters
>> 2) - mbstat
.m_bigclusters
;
2055 * Copy data from a buffer back into the indicated mbuf chain,
2056 * starting "off" bytes from the beginning, extending the mbuf
2057 * chain if necessary.
2067 struct mbuf
*m
= m0
, *n
;
2072 while (off
> (mlen
= m
->m_len
)) {
2075 if (m
->m_next
== 0) {
2076 n
= m_getclr(M_DONTWAIT
, m
->m_type
);
2079 n
->m_len
= min(MLEN
, len
+ off
);
2085 mlen
= min (m
->m_len
- off
, len
);
2086 bcopy(cp
, off
+ mtod(m
, caddr_t
), (unsigned)mlen
);
2094 if (m
->m_next
== 0) {
2095 n
= m_get(M_DONTWAIT
, m
->m_type
);
2098 n
->m_len
= min(MLEN
, len
);
2103 out
: if (((m
= m0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
))
2104 m
->m_pkthdr
.len
= totlen
;
2108 char *mcl_to_paddr(char *addr
) {
2111 if (addr
< (char *)mbutl
|| addr
>= (char *)embutl
)
2113 base_phys
= mcl_paddr
[(addr
- (char *)mbutl
) >> PGSHIFT
];
2117 return ((char *)((int)base_phys
| ((int)addr
& PGOFSET
)));
2121 * Dup the mbuf chain passed in. The whole thing. No cute additional cruft.
2122 * And really copy the thing. That way, we don't "precompute" checksums
2123 * for unsuspecting consumers.
2124 * Assumption: m->m_nextpkt == 0.
2125 * Trick: for small packets, don't dup into a cluster. That way received
2126 * packets don't take up too much room in the sockbuf (cf. sbspace()).
2131 m_dup(struct mbuf
*m
, int how
)
2133 struct mbuf
*n
, **np
;
2139 if (m
->m_flags
& M_PKTHDR
)
2143 * Quick check: if we have one mbuf and its data fits in an
2144 * mbuf with packet header, just copy and go.
2146 if (m
->m_next
== NULL
)
2147 { /* Then just move the data into an mbuf and be done... */
2149 { if (m
->m_pkthdr
.len
<= MHLEN
)
2150 { if ((n
= m_gethdr(how
, m
->m_type
)) == NULL
)
2152 n
->m_len
= m
->m_len
;
2153 m_dup_pkthdr(n
, m
, how
);
2154 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
2157 } else if (m
->m_len
<= MLEN
)
2158 { if ((n
= m_get(how
, m
->m_type
)) == NULL
)
2160 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
2161 n
->m_len
= m
->m_len
;
2168 kprintf("<%x: %x, %x, %x\n", m
, m
->m_flags
, m
->m_len
,
2172 n
= m_gethdr(how
, m
->m_type
);
2174 n
= m_get(how
, m
->m_type
);
2177 if (m
->m_flags
& M_EXT
)
2179 if ((n
->m_flags
& M_EXT
) == 0)
2184 { /* Don't use M_COPY_PKTHDR: preserve m_data */
2185 m_dup_pkthdr(n
, m
, how
);
2187 if ((n
->m_flags
& M_EXT
) == 0)
2188 n
->m_data
= n
->m_pktdat
;
2190 n
->m_len
= m
->m_len
;
2192 * Get the dup on the same bdry as the original
2193 * Assume that the two mbufs have the same offset to data area
2194 * (up to word bdries)
2196 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), (unsigned)n
->m_len
);
2200 kprintf(">%x: %x, %x, %x\n", n
, n
->m_flags
, n
->m_len
,
2215 m_mclref(struct mbuf
*p
)
2217 return (_MCLREF(p
));
2221 m_mclunref(struct mbuf
*p
)
2223 return (_MCLUNREF(p
));
2226 /* change mbuf to new type */
2228 m_mchtype(struct mbuf
*m
, int t
)
2231 mbstat
.m_mtypes
[(m
)->m_type
]--;
2232 mbstat
.m_mtypes
[t
]++;
2237 void *m_mtod(struct mbuf
*m
)
2239 return ((m
)->m_data
);
2242 struct mbuf
*m_dtom(void *x
)
2244 return ((struct mbuf
*)((u_long
)(x
) & ~(MSIZE
-1)));
2247 int m_mtocl(void *x
)
2249 return (((char *)(x
) - (char *)mbutl
) / sizeof(union mcluster
));
2252 union mcluster
*m_cltom(int x
)
2254 return ((union mcluster
*)(mbutl
+ (x
)));
2258 void m_mcheck(struct mbuf
*m
)
2260 if (m
->m_type
!= MT_FREE
)
2261 panic("mget MCHECK: m_type=%x m=%x", m
->m_type
, m
);
2265 mbuf_expand_thread(void)
2269 if (mbuf_expand_mcl
) {
2272 /* Adjust to the current number of cluster in use */
2273 n
= mbuf_expand_mcl
- (mbstat
.m_clusters
- mbstat
.m_clfree
);
2274 mbuf_expand_mcl
= 0;
2277 (void)m_clalloc(n
, M_WAIT
, MCLBYTES
, 1);
2279 if (mbuf_expand_big
) {
2282 /* Adjust to the current number of 4 KB cluster in use */
2283 n
= mbuf_expand_big
- (mbstat
.m_bigclusters
- mbstat
.m_bigclfree
);
2284 mbuf_expand_big
= 0;
2287 (void)m_clalloc(n
, M_WAIT
, NBPG
, 1);
2291 * Because we can run out of memory before filling the mbuf map, we
2292 * should not allocate more clusters than they are mbufs -- otherwise
2293 * we could have a large number of useless clusters allocated.
2295 while (mbstat
.m_mbufs
< mbstat
.m_bigclusters
+ mbstat
.m_clusters
) {
2296 if (m_expand(M_WAIT
) == 0)
2300 assert_wait(&mbuf_expand_thread_wakeup
, THREAD_UNINT
);
2301 (void) thread_block((thread_continue_t
)mbuf_expand_thread
);
2306 mbuf_expand_thread_init(void)
2308 mbuf_expand_thread_initialized
++;
2309 mbuf_expand_thread();
2312 SYSCTL_DECL(_kern_ipc
);
2313 SYSCTL_STRUCT(_kern_ipc
, KIPC_MBSTAT
, mbstat
, CTLFLAG_RW
, &mbstat
, mbstat
, "");