2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1988, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
65 * 10/15/97 Annette DeSchon (deschon@apple.com)
66 * Fixed bug in which all cluster mbufs were broken up
67 * into regular mbufs: Some clusters are now reserved.
68 * When a cluster is needed, regular mbufs are no longer
69 * used. (Radar 1683621)
70 * 20-May-95 Mac Gillon (mgillon) at NeXT
71 * New version based on 4.4
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/syslog.h>
81 #include <sys/protosw.h>
82 #include <sys/domain.h>
84 #include <kern/queue.h>
85 #include <kern/kern_types.h>
86 #include <kern/sched_prim.h>
88 #include <IOKit/IOMapper.h>
90 extern vm_offset_t
kmem_mb_alloc(vm_map_t
, int );
91 extern boolean_t
PE_parse_boot_arg(const char *, void *);
93 #define _MCLREF(p) (++mclrefcnt[mtocl(p)])
94 #define _MCLUNREF(p) (--mclrefcnt[mtocl(p)] == 0)
95 #define _M_CLEAR_PKTHDR(mbuf_ptr) (mbuf_ptr)->m_pkthdr.rcvif = NULL; \
96 (mbuf_ptr)->m_pkthdr.len = 0; \
97 (mbuf_ptr)->m_pkthdr.header = NULL; \
98 (mbuf_ptr)->m_pkthdr.csum_flags = 0; \
99 (mbuf_ptr)->m_pkthdr.csum_data = 0; \
100 (mbuf_ptr)->m_pkthdr.aux = (struct mbuf*)NULL; \
101 (mbuf_ptr)->m_pkthdr.vlan_tag = 0; \
102 (mbuf_ptr)->m_pkthdr.socket_id = 0; \
103 SLIST_INIT(&(mbuf_ptr)->m_pkthdr.tags);
105 /* kernel translater */
106 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
108 lck_mtx_t
* mbuf_mlock
;
109 lck_grp_t
* mbuf_mlock_grp
;
110 lck_grp_attr_t
* mbuf_mlock_grp_attr
;
111 lck_attr_t
* mbuf_mlock_attr
;
112 extern lck_mtx_t
*domain_proto_mtx
;
114 struct mbuf
*mfree
; /* mbuf free list */
115 struct mbuf
*mfreelater
; /* mbuf deallocation list */
116 extern vm_map_t mb_map
; /* special map */
117 int m_want
; /* sleepers on mbufs */
118 short *mclrefcnt
; /* mapped cluster reference counts */
120 static ppnum_t mcl_paddr_base
; /* Handle returned by IOMapper::iovmAlloc() */
121 union mcluster
*mclfree
; /* mapped cluster free list */
122 union mbigcluster
*mbigfree
; /* mapped cluster free list */
123 int max_linkhdr
; /* largest link-level header */
124 int max_protohdr
; /* largest protocol header */
125 int max_hdr
; /* largest link+protocol header */
126 int max_datalen
; /* MHLEN - max_hdr */
127 struct mbstat mbstat
; /* statistics */
128 union mcluster
*mbutl
; /* first mapped cluster address */
129 union mcluster
*embutl
; /* ending virtual address of mclusters */
131 static int nclpp
; /* # clusters per physical page */
133 static int m_howmany(int, size_t );
134 void m_reclaim(void);
135 static int m_clalloc(const int , const int, const size_t, int);
138 #define MF_NOWAIT 0x1
141 /* The number of cluster mbufs that are allocated, to start. */
142 #define MINCL max(16, 2)
144 static int mbuf_expand_thread_wakeup
= 0;
145 static int mbuf_expand_mcl
= 0;
146 static int mbuf_expand_big
= 0;
147 static int mbuf_expand_thread_initialized
= 0;
149 static void mbuf_expand_thread_init(void);
150 static void mbuf_expand_thread(void);
151 static int m_expand(int );
152 static caddr_t
m_bigalloc(int );
153 static void m_bigfree(caddr_t
, u_int
, caddr_t
);
154 __private_extern__
struct mbuf
* m_mbigget(struct mbuf
*, int );
156 static void m_range_check(void *addr
);
160 static int mfree_munge
= 0;
162 #define _MFREE_MUNGE(m) { \
165 vm_offset_t *element = (vm_offset_t *)(m); \
167 i < sizeof(struct mbuf)/sizeof(vm_offset_t); \
169 (element)[i] = 0xdeadbeef; \
174 munge_mbuf(struct mbuf
*m
)
177 vm_offset_t
*element
= (vm_offset_t
*)(m
);
179 i
< sizeof(struct mbuf
)/sizeof(vm_offset_t
);
181 (element
)[i
] = 0xdeadbeef;
183 #define _MFREE_MUNGE(m) { \
189 #define _MFREE_MUNGE(m)
193 #define _MINTGET(m, type) { \
195 if (((m) = mfree) != 0) { \
197 ++mclrefcnt[mtocl(m)]; \
198 mbstat.m_mtypes[MT_FREE]--; \
199 mbstat.m_mtypes[(type)]++; \
200 mfree = (m)->m_next; \
207 m_range_check(void *addr
)
209 if (addr
&& (addr
< (void *)mbutl
|| addr
>= (void *)embutl
))
210 panic("mbuf address out of range 0x%x", addr
);
213 __private_extern__
void
222 nclpp
= round_page_32(MCLBYTES
) / MCLBYTES
; /* see mbufgc() */
223 if (nclpp
< 1) nclpp
= 1;
224 mbuf_mlock_grp_attr
= lck_grp_attr_alloc_init();
226 mbuf_mlock_grp
= lck_grp_alloc_init("mbuf", mbuf_mlock_grp_attr
);
227 mbuf_mlock_attr
= lck_attr_alloc_init();
229 mbuf_mlock
= lck_mtx_alloc_init(mbuf_mlock_grp
, mbuf_mlock_attr
);
231 mbstat
.m_msize
= MSIZE
;
232 mbstat
.m_mclbytes
= MCLBYTES
;
233 mbstat
.m_minclsize
= MINCLSIZE
;
234 mbstat
.m_mlen
= MLEN
;
235 mbstat
.m_mhlen
= MHLEN
;
236 mbstat
.m_bigmclbytes
= NBPG
;
238 if (nmbclusters
== 0)
239 nmbclusters
= NMBCLUSTERS
;
240 MALLOC(mclrefcnt
, short *, nmbclusters
* sizeof (short),
244 for (m
= 0; m
< nmbclusters
; m
++)
247 /* Calculate the number of pages assigned to the cluster pool */
248 mcl_pages
= nmbclusters
/(NBPG
/CLBYTES
);
249 MALLOC(mcl_paddr
, int *, mcl_pages
* sizeof(int), M_TEMP
, M_WAITOK
);
252 /* Register with the I/O Bus mapper */
253 mcl_paddr_base
= IOMapperIOVMAlloc(mcl_pages
);
254 bzero((char *)mcl_paddr
, mcl_pages
* sizeof(int));
256 embutl
= (union mcluster
*)((unsigned char *)mbutl
+ (nmbclusters
* MCLBYTES
));
258 PE_parse_boot_arg("initmcl", &initmcl
);
260 if (m_clalloc(max(NBPG
/CLBYTES
, 1) * initmcl
, M_WAIT
, MCLBYTES
, 0) == 0)
264 (void) kernel_thread(kernel_task
, mbuf_expand_thread_init
);
272 * Allocate some number of mbuf clusters
273 * and place on cluster free list.
274 * Take the mbuf lock (if not already locked) and do not release it
281 const size_t bufsize
,
287 vm_offset_t page
= 0;
292 * Honor the caller's wish to block or not block.
293 * We have a way to grow the pool asynchronously,
294 * by kicking the dlil_input_thread.
296 i
= m_howmany(num
, bufsize
);
297 if (i
== 0 || nowait
== M_DONTWAIT
)
301 size
= round_page_32(i
* bufsize
);
302 page
= kmem_mb_alloc(mb_map
, size
);
305 size
= NBPG
; /* Try for 1 if failed */
306 page
= kmem_mb_alloc(mb_map
, size
);
311 numpages
= size
/ NBPG
;
312 for (i
= 0; i
< numpages
; i
++, page
+= NBPG
) {
313 if (((int)page
& PGOFSET
) == 0) {
314 ppnum_t offset
= ((char *)page
- (char *)mbutl
)/NBPG
;
315 ppnum_t new_page
= pmap_find_phys(kernel_pmap
, (vm_address_t
) page
);
318 * In the case of no mapper being available
319 * the following code nops and returns the
320 * input page, if there is a mapper the I/O
321 * page appropriate is returned.
323 new_page
= IOMapperInsertPage(mcl_paddr_base
, offset
, new_page
);
324 mcl_paddr
[offset
] = new_page
<< 12;
326 if (bufsize
== MCLBYTES
) {
327 union mcluster
*mcl
= (union mcluster
*)page
;
329 if (++mclrefcnt
[mtocl(mcl
)] != 0)
330 panic("m_clalloc already there");
331 mcl
->mcl_next
= mclfree
;
333 if (++mclrefcnt
[mtocl(mcl
)] != 0)
334 panic("m_clalloc already there");
335 mcl
->mcl_next
= mclfree
;
338 union mbigcluster
*mbc
= (union mbigcluster
*)page
;
340 if (++mclrefcnt
[mtocl(mbc
)] != 0)
341 panic("m_clalloc already there");
342 if (++mclrefcnt
[mtocl(mbc
) + 1] != 0)
343 panic("m_clalloc already there");
345 mbc
->mbc_next
= mbigfree
;
349 if (bufsize
== MCLBYTES
) {
350 int numcl
= numpages
<< 1;
351 mbstat
.m_clfree
+= numcl
;
352 mbstat
.m_clusters
+= numcl
;
355 mbstat
.m_bigclfree
+= numpages
;
356 mbstat
.m_bigclusters
+= numpages
;
362 * When non-blocking we kick a thread if we havve to grow the
363 * pool or if the number of free clusters is less than requested.
365 if (bufsize
== MCLBYTES
) {
367 /* Remember total number of clusters needed at this time */
368 i
+= mbstat
.m_clusters
;
369 if (i
> mbuf_expand_mcl
) {
371 if (mbuf_expand_thread_initialized
)
372 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
376 if (mbstat
.m_clfree
>= num
)
380 /* Remember total number of 4KB clusters needed at this time */
381 i
+= mbstat
.m_bigclusters
;
382 if (i
> mbuf_expand_big
) {
384 if (mbuf_expand_thread_initialized
)
385 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
389 if (mbstat
.m_bigclfree
>= num
)
396 * Add more free mbufs by cutting up a cluster.
399 m_expand(int canwait
)
403 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4)) {
405 * 1/16th of the total number of cluster mbufs allocated is
406 * reserved for large packets. The number reserved must
407 * always be < 1/2, or future allocation will be prevented.
409 (void)m_clalloc(1, canwait
, MCLBYTES
, 0);
411 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4))
415 MCLALLOC(mcl
, canwait
);
417 struct mbuf
*m
= (struct mbuf
*)mcl
;
420 mbstat
.m_mtypes
[MT_FREE
] += i
;
431 if (i
) wakeup((caddr_t
)&mfree
);
438 * When MGET failes, ask protocols to free space when short of memory,
439 * then re-attempt to allocate an mbuf.
450 (void) m_expand(canwait
);
453 (m
)->m_next
= (m
)->m_nextpkt
= 0;
454 (m
)->m_type
= (type
);
455 (m
)->m_data
= (m
)->m_dat
;
459 if (m
|| canwait
== M_DONTWAIT
)
470 if (mbuf_expand_thread_initialized
)
471 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
479 (void) msleep((caddr_t
)&mfree
, 0, (PZERO
-1) | PDROP
, "m_retry", &ts
);
488 * As above; retry an MGETHDR.
497 if ((m
= m_retry(canwait
, type
))) {
498 m
->m_next
= m
->m_nextpkt
= 0;
499 m
->m_flags
|= M_PKTHDR
;
500 m
->m_data
= m
->m_pktdat
;
509 do_reclaim
= 1; /* drain is performed in pfslowtimo(), to avoid deadlocks */
514 * Space allocation routines.
515 * These are also available as macros
516 * for critical paths.
525 m_range_check(mfree
);
526 m_range_check(mclfree
);
527 m_range_check(mbigfree
);
531 m
->m_next
= m
->m_nextpkt
= 0;
533 m
->m_data
= m
->m_dat
;
537 (m
) = m_retry(nowait
, type
);
539 m_range_check(mfree
);
540 m_range_check(mclfree
);
541 m_range_check(mbigfree
);
554 m_range_check(mfree
);
555 m_range_check(mclfree
);
556 m_range_check(mbigfree
);
561 m
->m_next
= m
->m_nextpkt
= 0;
563 m
->m_data
= m
->m_pktdat
;
564 m
->m_flags
= M_PKTHDR
;
568 m
= m_retryhdr(nowait
, type
);
570 m_range_check(mfree
);
571 m_range_check(mclfree
);
572 m_range_check(mbigfree
);
585 MGET(m
, nowait
, type
);
588 bzero(mtod(m
, caddr_t
), MLEN
);
596 struct mbuf
*n
= m
->m_next
;
600 m_range_check(mfree
);
601 m_range_check(mclfree
);
603 if (m
->m_type
== MT_FREE
)
604 panic("freeing free mbuf");
606 /* Free the aux data if there is any */
607 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
)
609 m_freem(m
->m_pkthdr
.aux
);
611 if ((m
->m_flags
& M_PKTHDR
) != 0)
612 m_tag_delete_chain(m
, NULL
);
615 if ((m
->m_flags
& M_EXT
))
617 if (MCLHASREFERENCE(m
)) {
618 remque((queue_t
)&m
->m_ext
.ext_refs
);
619 } else if (m
->m_ext
.ext_free
== NULL
) {
620 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
624 if (_MCLUNREF(mcl
)) {
625 mcl
->mcl_next
= mclfree
;
630 /* *** Since m_split() increments "mclrefcnt[mtocl(m->m_ext.ext_buf)]",
631 and AppleTalk ADSP uses m_split(), this incorrect sanity check
634 else /* sanity check - not referenced this way */
635 panic("m_free m_ext cluster not free");
638 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
639 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
642 mbstat
.m_mtypes
[m
->m_type
]--;
646 mbstat
.m_mtypes
[m
->m_type
]++;
654 if (i
) wakeup((caddr_t
)&mfree
);
658 /* m_mclget() add an mbuf cluster to a normal mbuf */
664 MCLALLOC(m
->m_ext
.ext_buf
, nowait
);
665 if (m
->m_ext
.ext_buf
) {
666 m
->m_data
= m
->m_ext
.ext_buf
;
668 m
->m_ext
.ext_size
= MCLBYTES
;
669 m
->m_ext
.ext_free
= 0;
670 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
677 /* m_mclalloc() allocate an mbuf cluster */
684 (void)m_clalloc(1, nowait
, MCLBYTES
, 0);
685 if ((p
= (caddr_t
)mclfree
)) {
686 ++mclrefcnt
[mtocl(p
)];
688 mclfree
= ((union mcluster
*)p
)->mcl_next
;
697 /* m_mclfree() releases a reference to a cluster allocated by MCLALLOC,
698 * freeing the cluster if the reference count has reached 0. */
707 if (--mclrefcnt
[mtocl(p
)] == 0) {
708 ((union mcluster
*)(p
))->mcl_next
= mclfree
;
709 mclfree
= (union mcluster
*)(p
);
715 /* mcl_hasreference() checks if a cluster of an mbuf is referenced by another mbuf */
720 return (m
->m_ext
.ext_refs
.forward
!= &(m
->m_ext
.ext_refs
));
723 __private_extern__ caddr_t
724 m_bigalloc(int nowait
)
728 (void)m_clalloc(1, nowait
, NBPG
, 0);
729 if ((p
= (caddr_t
)mbigfree
)) {
730 if (mclrefcnt
[mtocl(p
)] != mclrefcnt
[mtocl(p
) + 1])
731 panic("m_bigalloc mclrefcnt %x mismatch %d != %d",
732 p
, mclrefcnt
[mtocl(p
)], mclrefcnt
[mtocl(p
) + 1]);
733 if (mclrefcnt
[mtocl(p
)] || mclrefcnt
[mtocl(p
) + 1])
734 panic("m_bigalloc mclrefcnt %x not null %d != %d",
735 p
, mclrefcnt
[mtocl(p
)], mclrefcnt
[mtocl(p
) + 1]);
736 ++mclrefcnt
[mtocl(p
)];
737 ++mclrefcnt
[mtocl(p
) + 1];
738 mbstat
.m_bigclfree
--;
739 mbigfree
= ((union mbigcluster
*)p
)->mbc_next
;
747 __private_extern__
void
748 m_bigfree(caddr_t p
, __unused u_int size
, __unused caddr_t arg
)
752 if (mclrefcnt
[mtocl(p
)] != mclrefcnt
[mtocl(p
) + 1])
753 panic("m_bigfree mclrefcnt %x mismatch %d != %d",
754 p
, mclrefcnt
[mtocl(p
)], mclrefcnt
[mtocl(p
) + 1]);
755 --mclrefcnt
[mtocl(p
)];
756 --mclrefcnt
[mtocl(p
) + 1];
757 if (mclrefcnt
[mtocl(p
)] == 0) {
758 ((union mbigcluster
*)(p
))->mbc_next
= mbigfree
;
759 mbigfree
= (union mbigcluster
*)(p
);
760 mbstat
.m_bigclfree
++;
764 /* m_mbigget() add an 4KB mbuf cluster to a normal mbuf */
765 __private_extern__
struct mbuf
*
766 m_mbigget(struct mbuf
*m
, int nowait
)
768 m
->m_ext
.ext_buf
= m_bigalloc(nowait
);
769 if (m
->m_ext
.ext_buf
) {
770 m
->m_data
= m
->m_ext
.ext_buf
;
772 m
->m_ext
.ext_size
= NBPG
;
773 m
->m_ext
.ext_free
= m_bigfree
;
774 m
->m_ext
.ext_arg
= 0;
775 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
789 to
->m_pkthdr
= from
->m_pkthdr
;
790 from
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
791 SLIST_INIT(&from
->m_pkthdr
.tags
); /* purge tags from src */
792 to
->m_flags
= from
->m_flags
& M_COPYFLAGS
;
793 to
->m_data
= (to
)->m_pktdat
;
797 * "Move" mbuf pkthdr from "from" to "to".
798 * "from" must have M_PKTHDR set, and "to" must be empty.
802 m_move_pkthdr(struct mbuf
*to
, struct mbuf
*from
)
804 KASSERT((to
->m_flags
& M_EXT
) == 0, ("m_move_pkthdr: to has cluster"));
806 to
->m_flags
= from
->m_flags
& M_COPYFLAGS
;
807 to
->m_data
= to
->m_pktdat
;
808 to
->m_pkthdr
= from
->m_pkthdr
; /* especially tags */
809 SLIST_INIT(&from
->m_pkthdr
.tags
); /* purge tags from src */
810 from
->m_flags
&= ~M_PKTHDR
;
815 * Duplicate "from"'s mbuf pkthdr in "to".
816 * "from" must have M_PKTHDR set, and "to" must be empty.
817 * In particular, this does a deep copy of the packet tags.
820 m_dup_pkthdr(struct mbuf
*to
, struct mbuf
*from
, int how
)
822 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) | (to
->m_flags
& M_EXT
);
823 if ((to
->m_flags
& M_EXT
) == 0)
824 to
->m_data
= to
->m_pktdat
;
825 if (to
->m_pkthdr
.aux
!= NULL
)
826 m_freem(to
->m_pkthdr
.aux
);
827 to
->m_pkthdr
= from
->m_pkthdr
;
828 to
->m_pkthdr
.aux
= NULL
;
829 (void) m_aux_copy(to
, from
);
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
, "");