2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1988, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
62 * 10/15/97 Annette DeSchon (deschon@apple.com)
63 * Fixed bug in which all cluster mbufs were broken up
64 * into regular mbufs: Some clusters are now reserved.
65 * When a cluster is needed, regular mbufs are no longer
66 * used. (Radar 1683621)
67 * 20-May-95 Mac Gillon (mgillon) at NeXT
68 * New version based on 4.4
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
75 #include <sys/kernel.h>
76 #include <sys/syslog.h>
77 #include <sys/protosw.h>
78 #include <sys/domain.h>
79 #include <net/netisr.h>
81 #include <kern/queue.h>
82 #include <kern/kern_types.h>
83 #include <kern/sched_prim.h>
85 #include <IOKit/IOMapper.h>
87 #define _MCLREF(p) (++mclrefcnt[mtocl(p)])
88 #define _MCLUNREF(p) (--mclrefcnt[mtocl(p)] == 0)
90 extern pmap_t kernel_pmap
; /* The kernel's pmap */
91 /* kernel translater */
92 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
94 decl_simple_lock_data(, mbuf_slock
);
95 struct mbuf
*mfree
; /* mbuf free list */
96 struct mbuf
*mfreelater
; /* mbuf deallocation list */
97 extern vm_map_t mb_map
; /* special map */
98 int m_want
; /* sleepers on mbufs */
99 extern int nmbclusters
; /* max number of mapped clusters */
100 short *mclrefcnt
; /* mapped cluster reference counts */
102 static ppnum_t mcl_paddr_base
; /* Handle returned by IOMapper::iovmAlloc() */
103 union mcluster
*mclfree
; /* mapped cluster free list */
104 int max_linkhdr
; /* largest link-level header */
105 int max_protohdr
; /* largest protocol header */
106 int max_hdr
; /* largest link+protocol header */
107 int max_datalen
; /* MHLEN - max_hdr */
108 struct mbstat mbstat
; /* statistics */
109 union mcluster
*mbutl
; /* first mapped cluster address */
110 union mcluster
*embutl
; /* ending virtual address of mclusters */
112 static int nclpp
; /* # clusters per physical page */
113 static char mbfail
[] = "mbuf not mapped";
115 static int m_howmany();
117 /* The number of cluster mbufs that are allocated, to start. */
118 #define MINCL max(16, 2)
120 extern int dlil_input_thread_wakeup
;
121 extern int dlil_expand_mcl
;
122 extern int dlil_initialized
;
125 static int mfree_munge
= 0;
127 #define _MFREE_MUNGE(m) { \
130 vm_offset_t *element = (vm_offset_t *)(m); \
132 i < sizeof(struct mbuf)/sizeof(vm_offset_t); \
134 (element)[i] = 0xdeadbeef; \
139 munge_mbuf(struct mbuf
*m
)
142 vm_offset_t
*element
= (vm_offset_t
*)(m
);
144 i
< sizeof(struct mbuf
)/sizeof(vm_offset_t
);
146 (element
)[i
] = 0xdeadbeef;
148 #define _MFREE_MUNGE(m) { \
154 #define _MFREE_MUNGE(m)
158 #define _MINTGET(m, type) { \
160 if (((m) = mfree) != 0) { \
162 ++mclrefcnt[mtocl(m)]; \
163 mbstat.m_mtypes[MT_FREE]--; \
164 mbstat.m_mtypes[(type)]++; \
165 mfree = (m)->m_next; \
180 nclpp
= round_page_32(MCLBYTES
) / MCLBYTES
; /* see mbufgc() */
181 if (nclpp
< 1) nclpp
= 1;
183 // NETISR_LOCKINIT();
185 mbstat
.m_msize
= MSIZE
;
186 mbstat
.m_mclbytes
= MCLBYTES
;
187 mbstat
.m_minclsize
= MINCLSIZE
;
188 mbstat
.m_mlen
= MLEN
;
189 mbstat
.m_mhlen
= MHLEN
;
191 if (nmbclusters
== 0)
192 nmbclusters
= NMBCLUSTERS
;
193 MALLOC(mclrefcnt
, short *, nmbclusters
* sizeof (short),
197 for (m
= 0; m
< nmbclusters
; m
++)
200 /* Calculate the number of pages assigned to the cluster pool */
201 mcl_pages
= nmbclusters
/(PAGE_SIZE
/CLBYTES
);
202 MALLOC(mcl_paddr
, int *, mcl_pages
* sizeof(int), M_TEMP
, M_WAITOK
);
205 /* Register with the I/O Bus mapper */
206 mcl_paddr_base
= IOMapperIOVMAlloc(mcl_pages
);
207 bzero((char *)mcl_paddr
, mcl_pages
* sizeof(int));
209 embutl
= (union mcluster
*)((unsigned char *)mbutl
+ (nmbclusters
* MCLBYTES
));
211 PE_parse_boot_arg("initmcl", &initmcl
);
213 if (m_clalloc(max(PAGE_SIZE
/CLBYTES
, 1) * initmcl
, M_WAIT
) == 0)
222 * Allocate some number of mbuf clusters
223 * and place on cluster free list.
226 m_clalloc(ncl
, nowait
)
230 register union mcluster
*mcl
;
233 static char doing_alloc
;
236 * Honor the caller's wish to block or not block.
237 * We have a way to grow the pool asynchronously,
238 * by kicking the dlil_input_thread.
240 if ((i
= m_howmany()) <= 0)
243 if ((nowait
== M_DONTWAIT
))
248 size
= round_page_32(ncl
* MCLBYTES
);
249 mcl
= (union mcluster
*)kmem_mb_alloc(mb_map
, size
);
251 if (mcl
== 0 && ncl
> 1) {
252 size
= round_page_32(MCLBYTES
); /* Try for 1 if failed */
253 mcl
= (union mcluster
*)kmem_mb_alloc(mb_map
, size
);
258 ncl
= size
/ MCLBYTES
;
259 for (i
= 0; i
< ncl
; i
++) {
260 if (++mclrefcnt
[mtocl(mcl
)] != 0)
261 panic("m_clalloc already there");
262 if (((int)mcl
& PAGE_MASK
) == 0) {
263 ppnum_t offset
= ((char *)mcl
- (char *)mbutl
)/PAGE_SIZE
;
264 ppnum_t new_page
= pmap_find_phys(kernel_pmap
, (vm_address_t
) mcl
);
267 * In the case of no mapper being available
268 * the following code nops and returns the
269 * input page, if there is a mapper the I/O
270 * page appropriate is returned.
272 new_page
= IOMapperInsertPage(mcl_paddr_base
, offset
, new_page
);
273 mcl_paddr
[offset
] = new_page
<< 12;
276 mcl
->mcl_next
= mclfree
;
279 mbstat
.m_clfree
+= ncl
;
280 mbstat
.m_clusters
+= ncl
;
287 * When non-blocking we kick the dlil thread if we havve to grow the
288 * pool or if the number of free clusters is less than requested.
290 if ((nowait
== M_DONTWAIT
) && (i
> 0 || ncl
>= mbstat
.m_clfree
)) {
292 if (dlil_initialized
)
293 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
296 if (mbstat
.m_clfree
>= ncl
)
305 * Add more free mbufs by cutting up a cluster.
310 register caddr_t mcl
;
312 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4))
313 /* 1/16th of the total number of cluster mbufs allocated is
314 reserved for large packets. The number reserved must
315 always be < 1/2, or future allocation will be prevented.
319 MCLALLOC(mcl
, canwait
);
321 register struct mbuf
*m
= (struct mbuf
*)mcl
;
322 register int i
= NMBPCL
;
324 mbstat
.m_mtypes
[MT_FREE
] += i
;
335 if (i
) wakeup((caddr_t
)&mfree
);
342 * When MGET failes, ask protocols to free space when short of memory,
343 * then re-attempt to allocate an mbuf.
346 m_retry(canwait
, type
)
349 register struct mbuf
*m
;
353 boolean_t funnel_state
;
356 (void) m_expand(canwait
);
359 (m
)->m_next
= (m
)->m_nextpkt
= 0;
360 (m
)->m_type
= (type
);
361 (m
)->m_data
= (m
)->m_dat
;
364 if (m
|| canwait
== M_DONTWAIT
)
375 if (dlil_initialized
)
376 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
379 * Grab network funnel because m_reclaim calls into the
380 * socket domains and tsleep end-up calling splhigh
382 fnl
= thread_funnel_get();
383 if (fnl
&& (fnl
== kernel_flock
)) {
385 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
387 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
391 /* Sleep with a small timeout as insurance */
392 (void) tsleep((caddr_t
)&mfree
, PZERO
-1, "m_retry", hz
);
395 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
397 thread_funnel_set(network_flock
, funnel_state
);
403 * As above; retry an MGETHDR.
406 m_retryhdr(canwait
, type
)
409 register struct mbuf
*m
;
411 if (m
= m_retry(canwait
, type
)) {
412 m
->m_flags
|= M_PKTHDR
;
413 m
->m_data
= m
->m_pktdat
;
414 m
->m_pkthdr
.rcvif
= NULL
;
416 m
->m_pkthdr
.header
= NULL
;
417 m
->m_pkthdr
.csum_flags
= 0;
418 m
->m_pkthdr
.csum_data
= 0;
419 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
420 m
->m_pkthdr
.reserved1
= NULL
;
421 m
->m_pkthdr
.reserved2
= NULL
;
428 register struct domain
*dp
;
429 register struct protosw
*pr
;
431 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
432 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
)
439 * Space allocation routines.
440 * These are also available as macros
441 * for critical paths.
447 register struct mbuf
*m
;
451 m
->m_next
= m
->m_nextpkt
= 0;
453 m
->m_data
= m
->m_dat
;
456 (m
) = m_retry(nowait
, type
);
462 m_gethdr(nowait
, type
)
465 register struct mbuf
*m
;
469 m
->m_next
= m
->m_nextpkt
= 0;
471 m
->m_data
= m
->m_pktdat
;
472 m
->m_flags
= M_PKTHDR
;
473 m
->m_pkthdr
.rcvif
= NULL
;
474 m
->m_pkthdr
.header
= NULL
;
475 m
->m_pkthdr
.csum_flags
= 0;
476 m
->m_pkthdr
.csum_data
= 0;
477 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
478 m
->m_pkthdr
.reserved1
= NULL
;
479 m
->m_pkthdr
.reserved2
= NULL
;
481 m
= m_retryhdr(nowait
, type
);
487 m_getclr(nowait
, type
)
490 register struct mbuf
*m
;
492 MGET(m
, nowait
, type
);
495 bzero(mtod(m
, caddr_t
), MLEN
);
503 struct mbuf
*n
= m
->m_next
;
506 if (m
->m_type
== MT_FREE
)
507 panic("freeing free mbuf");
509 /* Free the aux data if there is any */
510 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
)
512 m_freem(m
->m_pkthdr
.aux
);
516 if ((m
->m_flags
& M_EXT
))
518 if (MCLHASREFERENCE(m
)) {
519 remque((queue_t
)&m
->m_ext
.ext_refs
);
520 } else if (m
->m_ext
.ext_free
== NULL
) {
521 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
522 if (_MCLUNREF(mcl
)) {
523 mcl
->mcl_next
= mclfree
;
528 /* *** Since m_split() increments "mclrefcnt[mtocl(m->m_ext.ext_buf)]",
529 and AppleTalk ADSP uses m_split(), this incorrect sanity check
532 else /* sanity check - not referenced this way */
533 panic("m_free m_ext cluster not free");
536 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
537 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
540 mbstat
.m_mtypes
[m
->m_type
]--;
544 mbstat
.m_mtypes
[m
->m_type
]++;
552 if (i
) wakeup((caddr_t
)&mfree
);
556 /* m_mclget() add an mbuf cluster to a normal mbuf */
562 MCLALLOC(m
->m_ext
.ext_buf
, nowait
);
563 if (m
->m_ext
.ext_buf
) {
564 m
->m_data
= m
->m_ext
.ext_buf
;
566 m
->m_ext
.ext_size
= MCLBYTES
;
567 m
->m_ext
.ext_free
= 0;
568 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
575 /* m_mclalloc() allocate an mbuf cluster */
582 (void)m_clalloc(1, nowait
);
583 if ((p
= (caddr_t
)mclfree
)) {
584 ++mclrefcnt
[mtocl(p
)];
586 mclfree
= ((union mcluster
*)p
)->mcl_next
;
593 /* m_mclfree() releases a reference to a cluster allocated by MCLALLOC,
594 * freeing the cluster if the reference count has reached 0. */
600 if (--mclrefcnt
[mtocl(p
)] == 0) {
601 ((union mcluster
*)(p
))->mcl_next
= mclfree
;
602 mclfree
= (union mcluster
*)(p
);
608 /* mcl_hasreference() checks if a cluster of an mbuf is referenced by another mbuf */
613 return (m
->m_ext
.ext_refs
.forward
!= &(m
->m_ext
.ext_refs
));
618 m_copy_pkthdr(to
, from
)
619 struct mbuf
*to
, *from
;
621 to
->m_pkthdr
= from
->m_pkthdr
;
622 from
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
623 to
->m_flags
= from
->m_flags
& M_COPYFLAGS
;
624 to
->m_data
= (to
)->m_pktdat
;
627 /* Best effort to get a mbuf cluster + pkthdr under one lock.
628 * If we don't have them avail, just bail out and use the regular
630 * Used by drivers to allocated packets on receive ring.
636 m_clalloc(1, M_DONTWAIT
); /* takes the MBUF_LOCK, but doesn't release it... */
637 if ((mfree
!= 0) && (mclfree
!= 0)) { /* mbuf + cluster are available */
641 ++mclrefcnt
[mtocl(m
)];
642 mbstat
.m_mtypes
[MT_FREE
]--;
643 mbstat
.m_mtypes
[MT_DATA
]++;
644 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
645 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
647 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
649 m
->m_next
= m
->m_nextpkt
= 0;
651 m
->m_data
= m
->m_ext
.ext_buf
;
652 m
->m_flags
= M_PKTHDR
| M_EXT
;
654 m
->m_pkthdr
.rcvif
= NULL
;
655 m
->m_pkthdr
.header
= NULL
;
656 m
->m_pkthdr
.csum_data
= 0;
657 m
->m_pkthdr
.csum_flags
= 0;
658 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
659 m
->m_pkthdr
.reserved1
= 0;
660 m
->m_pkthdr
.reserved2
= 0;
661 m
->m_ext
.ext_free
= 0;
662 m
->m_ext
.ext_size
= MCLBYTES
;
663 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
667 else { /* slow path: either mbuf or cluster need to be allocated anyway */
670 MGETHDR(m
, M_WAITOK
, MT_DATA
);
675 MCLGET( m
, M_WAITOK
);
676 if ( ( m
->m_flags
& M_EXT
) == 0 )
686 * return a list of mbuf hdrs that point to clusters...
687 * try for num_needed, if this can't be met, return whatever
688 * number were available... set up the first num_with_pkthdrs
689 * with mbuf hdrs configured as packet headers... these are
690 * chained on the m_nextpkt field... any packets requested beyond
691 * this are chained onto the last packet header's m_next field.
694 m_getpackets(int num_needed
, int num_with_pkthdrs
, int how
)
697 struct mbuf
**np
, *top
;
702 m_clalloc(num_needed
, how
); /* takes the MBUF_LOCK, but doesn't release it... */
704 while (num_needed
--) {
705 if (mfree
&& mclfree
) { /* mbuf + cluster are available */
709 ++mclrefcnt
[mtocl(m
)];
710 mbstat
.m_mtypes
[MT_FREE
]--;
711 mbstat
.m_mtypes
[MT_DATA
]++;
712 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
713 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
715 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
717 m
->m_next
= m
->m_nextpkt
= 0;
719 m
->m_data
= m
->m_ext
.ext_buf
;
720 m
->m_ext
.ext_free
= 0;
721 m
->m_ext
.ext_size
= MCLBYTES
;
722 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
724 if (num_with_pkthdrs
== 0)
727 m
->m_flags
= M_PKTHDR
| M_EXT
;
729 m
->m_pkthdr
.rcvif
= NULL
;
730 m
->m_pkthdr
.header
= NULL
;
731 m
->m_pkthdr
.csum_flags
= 0;
732 m
->m_pkthdr
.csum_data
= 0;
733 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
734 m
->m_pkthdr
.reserved1
= NULL
;
735 m
->m_pkthdr
.reserved2
= NULL
;
744 if (num_with_pkthdrs
== 0) {
745 MGET(m
, how
, MT_DATA
);
747 MGETHDR(m
, how
, MT_DATA
);
755 if ((m
->m_flags
& M_EXT
) == 0) {
763 if (num_with_pkthdrs
)
775 * return a list of mbuf hdrs set up as packet hdrs
776 * chained together on the m_nextpkt field
779 m_getpackethdrs(int num_needed
, int how
)
782 struct mbuf
**np
, *top
;
789 while (num_needed
--) {
790 if (m
= mfree
) { /* mbufs are available */
793 ++mclrefcnt
[mtocl(m
)];
794 mbstat
.m_mtypes
[MT_FREE
]--;
795 mbstat
.m_mtypes
[MT_DATA
]++;
797 m
->m_next
= m
->m_nextpkt
= 0;
799 m
->m_flags
= M_PKTHDR
;
800 m
->m_data
= m
->m_pktdat
;
802 m
->m_pkthdr
.rcvif
= NULL
;
803 m
->m_pkthdr
.header
= NULL
;
804 m
->m_pkthdr
.csum_flags
= 0;
805 m
->m_pkthdr
.csum_data
= 0;
806 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
807 m
->m_pkthdr
.reserved1
= NULL
;
808 m
->m_pkthdr
.reserved2
= NULL
;
814 m
= m_retryhdr(how
, MT_DATA
);
830 /* free and mbuf list (m_nextpkt) while following m_next under one lock.
831 * returns the count for mbufs packets freed. Used by the drivers.
837 struct mbuf
*nextpkt
;
844 nextpkt
= m
->m_nextpkt
; /* chain of linked mbufs from driver */
850 while (m
) { /* free the mbuf chain (like mfreem) */
854 /* Free the aux data if there is any */
855 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
) {
857 * Treat the current m as the nextpkt and set m
858 * to the aux data. This lets us free the aux
859 * data in this loop without having to call
860 * m_freem recursively, which wouldn't work
861 * because we've still got the lock.
864 m
= nextpkt
->m_pkthdr
.aux
;
865 nextpkt
->m_pkthdr
.aux
= NULL
;
870 if (n
&& n
->m_nextpkt
)
871 panic("m_freem_list: m_nextpkt of m_next != NULL");
872 if (m
->m_type
== MT_FREE
)
873 panic("freeing free mbuf");
875 if (m
->m_flags
& M_EXT
) {
876 if (MCLHASREFERENCE(m
)) {
877 remque((queue_t
)&m
->m_ext
.ext_refs
);
878 } else if (m
->m_ext
.ext_free
== NULL
) {
879 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
880 if (_MCLUNREF(mcl
)) {
881 mcl
->mcl_next
= mclfree
;
886 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
887 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
890 mbstat
.m_mtypes
[m
->m_type
]--;
893 mbstat
.m_mtypes
[MT_FREE
]++;
901 m
= nextpkt
; /* bump m with saved nextpkt if any */
909 wakeup((caddr_t
)&mfree
);
916 register struct mbuf
*m
;
923 * Mbuffer utility routines.
926 * Compute the amount of space available
927 * before the current start of data in an mbuf.
930 register struct mbuf
*m
;
932 if (m
->m_flags
& M_EXT
) {
933 if (MCLHASREFERENCE(m
))
935 return (m
->m_data
- m
->m_ext
.ext_buf
);
937 if (m
->m_flags
& M_PKTHDR
)
938 return (m
->m_data
- m
->m_pktdat
);
939 return (m
->m_data
- m
->m_dat
);
943 * Compute the amount of space available
944 * after the end of data in an mbuf.
947 register struct mbuf
*m
;
949 if (m
->m_flags
& M_EXT
) {
950 if (MCLHASREFERENCE(m
))
952 return (m
->m_ext
.ext_buf
+ m
->m_ext
.ext_size
-
953 (m
->m_data
+ m
->m_len
));
955 return (&m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
));
959 * Lesser-used path for M_PREPEND:
960 * allocate new mbuf to prepend to chain,
962 * Does not adjust packet header length.
965 m_prepend(m
, len
, how
)
966 register struct mbuf
*m
;
971 MGET(mn
, how
, m
->m_type
);
972 if (mn
== (struct mbuf
*)NULL
) {
974 return ((struct mbuf
*)NULL
);
976 if (m
->m_flags
& M_PKTHDR
) {
977 M_COPY_PKTHDR(mn
, m
);
978 m
->m_flags
&= ~M_PKTHDR
;
989 * Replacement for old M_PREPEND macro:
990 * allocate new mbuf to prepend to chain,
991 * copy junk along, and adjust length.
995 m_prepend_2(m
, len
, how
)
996 register struct mbuf
*m
;
999 if (M_LEADINGSPACE(m
) >= len
) {
1003 m
= m_prepend(m
, len
, how
);
1005 if ((m
) && (m
->m_flags
& M_PKTHDR
))
1006 m
->m_pkthdr
.len
+= len
;
1011 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
1012 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
1013 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
1018 m_copym(m
, off0
, len
, wait
)
1019 register struct mbuf
*m
;
1023 register struct mbuf
*n
, **np
;
1024 register int off
= off0
;
1028 if (off
< 0 || len
< 0)
1030 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1033 while (off
>= m
->m_len
) {
1046 if (len
!= M_COPYALL
)
1052 ++mclrefcnt
[mtocl(n
)];
1053 mbstat
.m_mtypes
[MT_FREE
]--;
1054 mbstat
.m_mtypes
[m
->m_type
]++;
1056 n
->m_next
= n
->m_nextpkt
= 0;
1057 n
->m_type
= m
->m_type
;
1058 n
->m_data
= n
->m_dat
;
1062 n
= m_retry(wait
, m
->m_type
);
1070 M_COPY_PKTHDR(n
, m
);
1071 if (len
== M_COPYALL
)
1072 n
->m_pkthdr
.len
-= off0
;
1074 n
->m_pkthdr
.len
= len
;
1077 if (len
== M_COPYALL
) {
1078 if (min(len
, (m
->m_len
- off
)) == len
) {
1079 printf("m->m_len %d - off %d = %d, %d\n",
1080 m
->m_len
, off
, m
->m_len
- off
,
1081 min(len
, (m
->m_len
- off
)));
1084 n
->m_len
= min(len
, (m
->m_len
- off
));
1085 if (n
->m_len
== M_COPYALL
) {
1086 printf("n->m_len == M_COPYALL, fixing\n");
1089 if (m
->m_flags
& M_EXT
) {
1090 n
->m_ext
= m
->m_ext
;
1091 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1092 n
->m_data
= m
->m_data
+ off
;
1093 n
->m_flags
|= M_EXT
;
1095 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1096 (unsigned)n
->m_len
);
1098 if (len
!= M_COPYALL
)
1120 * equivilent to m_copym except that all necessary
1121 * mbuf hdrs are allocated within this routine
1122 * also, the last mbuf and offset accessed are passed
1123 * out and can be passed back in to avoid having to
1124 * rescan the entire mbuf list (normally hung off of the socket)
1127 m_copym_with_hdrs(m
, off0
, len
, wait
, m_last
, m_off
)
1128 register struct mbuf
*m
;
1131 struct mbuf
**m_last
;
1134 register struct mbuf
*n
, **np
;
1135 register int off
= off0
;
1136 struct mbuf
*top
= 0;
1140 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1147 while (off
>= m
->m_len
) {
1159 panic("m_gethdr_and_copym");
1164 ++mclrefcnt
[mtocl(n
)];
1165 mbstat
.m_mtypes
[MT_FREE
]--;
1166 mbstat
.m_mtypes
[type
]++;
1168 n
->m_next
= n
->m_nextpkt
= 0;
1172 n
->m_data
= n
->m_dat
;
1175 n
->m_data
= n
->m_pktdat
;
1176 n
->m_flags
= M_PKTHDR
;
1177 n
->m_pkthdr
.len
= 0;
1178 n
->m_pkthdr
.rcvif
= NULL
;
1179 n
->m_pkthdr
.header
= NULL
;
1180 n
->m_pkthdr
.csum_flags
= 0;
1181 n
->m_pkthdr
.csum_data
= 0;
1182 n
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
1183 n
->m_pkthdr
.reserved1
= NULL
;
1184 n
->m_pkthdr
.reserved2
= NULL
;
1189 n
= m_retry(wait
, type
);
1191 n
= m_retryhdr(wait
, type
);
1204 M_COPY_PKTHDR(n
, m
);
1205 n
->m_pkthdr
.len
= len
;
1208 n
->m_len
= min(len
, (m
->m_len
- off
));
1210 if (m
->m_flags
& M_EXT
) {
1211 n
->m_ext
= m
->m_ext
;
1212 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1213 n
->m_data
= m
->m_data
+ off
;
1214 n
->m_flags
|= M_EXT
;
1216 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1217 (unsigned)n
->m_len
);
1222 if ((off
+ n
->m_len
) == m
->m_len
) {
1223 *m_last
= m
->m_next
;
1227 *m_off
= off
+ n
->m_len
;
1249 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1250 * continuing for "len" bytes, into the indicated buffer.
1252 void m_copydata(m
, off
, len
, cp
)
1253 register struct mbuf
*m
;
1258 register unsigned count
;
1260 if (off
< 0 || len
< 0)
1261 panic("m_copydata");
1264 panic("m_copydata");
1272 panic("m_copydata");
1273 count
= min(m
->m_len
- off
, len
);
1274 bcopy(mtod(m
, caddr_t
) + off
, cp
, count
);
1283 * Concatenate mbuf chain n to m.
1284 * Both chains must be of the same type (e.g. MT_DATA).
1285 * Any m_pkthdr is not updated.
1288 register struct mbuf
*m
, *n
;
1293 if (m
->m_flags
& M_EXT
||
1294 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
1295 /* just join the two chains */
1299 /* splat the data from one into the other */
1300 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1302 m
->m_len
+= n
->m_len
;
1312 register int len
= req_len
;
1313 register struct mbuf
*m
;
1316 if ((m
= mp
) == NULL
)
1322 while (m
!= NULL
&& len
> 0) {
1323 if (m
->m_len
<= len
) {
1334 if (m
->m_flags
& M_PKTHDR
)
1335 m
->m_pkthdr
.len
-= (req_len
- len
);
1338 * Trim from tail. Scan the mbuf chain,
1339 * calculating its length and finding the last mbuf.
1340 * If the adjustment only affects this mbuf, then just
1341 * adjust and return. Otherwise, rescan and truncate
1342 * after the remaining size.
1348 if (m
->m_next
== (struct mbuf
*)0)
1352 if (m
->m_len
>= len
) {
1355 if (m
->m_flags
& M_PKTHDR
)
1356 m
->m_pkthdr
.len
-= len
;
1363 * Correct length for chain is "count".
1364 * Find the mbuf with last data, adjust its length,
1365 * and toss data from remaining mbufs on chain.
1368 if (m
->m_flags
& M_PKTHDR
)
1369 m
->m_pkthdr
.len
= count
;
1370 for (; m
; m
= m
->m_next
) {
1371 if (m
->m_len
>= count
) {
1377 while (m
= m
->m_next
)
1383 * Rearange an mbuf chain so that len bytes are contiguous
1384 * and in the data area of an mbuf (so that mtod and dtom
1385 * will work for a structure of size len). Returns the resulting
1386 * mbuf chain on success, frees it and returns null on failure.
1387 * If there is room, it will add up to max_protohdr-len extra bytes to the
1388 * contiguous region in an attempt to avoid being called next time.
1394 register struct mbuf
*n
;
1397 register struct mbuf
*m
;
1402 * If first mbuf has no cluster, and has room for len bytes
1403 * without shifting current data, pullup into it,
1404 * otherwise allocate a new mbuf to prepend to the chain.
1406 if ((n
->m_flags
& M_EXT
) == 0 &&
1407 n
->m_data
+ len
< &n
->m_dat
[MLEN
] && n
->m_next
) {
1408 if (n
->m_len
>= len
)
1416 MGET(m
, M_DONTWAIT
, n
->m_type
);
1420 if (n
->m_flags
& M_PKTHDR
) {
1421 M_COPY_PKTHDR(m
, n
);
1422 n
->m_flags
&= ~M_PKTHDR
;
1425 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
1427 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
1428 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1438 } while (len
> 0 && n
);
1452 * Partition an mbuf chain in two pieces, returning the tail --
1453 * all but the first len0 bytes. In case of failure, it returns NULL and
1454 * attempts to restore the chain to its original state.
1457 m_split(m0
, len0
, wait
)
1458 register struct mbuf
*m0
;
1461 register struct mbuf
*m
, *n
;
1462 unsigned len
= len0
, remain
;
1464 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
1468 remain
= m
->m_len
- len
;
1469 if (m0
->m_flags
& M_PKTHDR
) {
1470 MGETHDR(n
, wait
, m0
->m_type
);
1473 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
1474 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
1475 m0
->m_pkthdr
.len
= len0
;
1476 if (m
->m_flags
& M_EXT
)
1478 if (remain
> MHLEN
) {
1479 /* m can't be the lead packet */
1481 n
->m_next
= m_split(m
, len
, wait
);
1482 if (n
->m_next
== 0) {
1488 MH_ALIGN(n
, remain
);
1489 } else if (remain
== 0) {
1494 MGET(n
, wait
, m
->m_type
);
1500 if (m
->m_flags
& M_EXT
) {
1501 n
->m_flags
|= M_EXT
;
1503 n
->m_ext
= m
->m_ext
;
1504 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1506 n
->m_data
= m
->m_data
+ len
;
1508 bcopy(mtod(m
, caddr_t
) + len
, mtod(n
, caddr_t
), remain
);
1512 n
->m_next
= m
->m_next
;
1517 * Routine to copy from device local memory into mbufs.
1520 m_devget(buf
, totlen
, off0
, ifp
, copy
)
1526 register struct mbuf
*m
;
1527 struct mbuf
*top
= 0, **mp
= &top
;
1528 register int off
= off0
, len
;
1536 * If 'off' is non-zero, packet is trailer-encapsulated,
1537 * so we have to skip the type and length fields.
1539 cp
+= off
+ 2 * sizeof(u_int16_t
);
1540 totlen
-= 2 * sizeof(u_int16_t
);
1542 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1545 m
->m_pkthdr
.rcvif
= ifp
;
1546 m
->m_pkthdr
.len
= totlen
;
1549 while (totlen
> 0) {
1551 MGET(m
, M_DONTWAIT
, MT_DATA
);
1558 len
= min(totlen
, epkt
- cp
);
1559 if (len
>= MINCLSIZE
) {
1560 MCLGET(m
, M_DONTWAIT
);
1561 if (m
->m_flags
& M_EXT
)
1562 m
->m_len
= len
= min(len
, MCLBYTES
);
1564 /* give up when it's out of cluster mbufs */
1572 * Place initial small packet/header at end of mbuf.
1574 if (len
< m
->m_len
) {
1575 if (top
== 0 && len
+ max_linkhdr
<= m
->m_len
)
1576 m
->m_data
+= max_linkhdr
;
1582 copy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
1584 bcopy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
1596 * Cluster freelist allocation check. The mbuf lock must be held.
1597 * Ensure hysteresis between hi/lo.
1605 if (mbstat
.m_clusters
< MINCL
)
1606 return (MINCL
- mbstat
.m_clusters
);
1607 /* Too few (free < 1/2 total) and not over maximum */
1608 if (mbstat
.m_clusters
< nmbclusters
&&
1609 (i
= ((mbstat
.m_clusters
>> 1) - mbstat
.m_clfree
)) > 0)
1616 * Copy data from a buffer back into the indicated mbuf chain,
1617 * starting "off" bytes from the beginning, extending the mbuf
1618 * chain if necessary.
1621 m_copyback(m0
, off
, len
, cp
)
1628 register struct mbuf
*m
= m0
, *n
;
1633 while (off
> (mlen
= m
->m_len
)) {
1636 if (m
->m_next
== 0) {
1637 n
= m_getclr(M_DONTWAIT
, m
->m_type
);
1640 n
->m_len
= min(MLEN
, len
+ off
);
1646 mlen
= min (m
->m_len
- off
, len
);
1647 bcopy(cp
, off
+ mtod(m
, caddr_t
), (unsigned)mlen
);
1655 if (m
->m_next
== 0) {
1656 n
= m_get(M_DONTWAIT
, m
->m_type
);
1659 n
->m_len
= min(MLEN
, len
);
1664 out
: if (((m
= m0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
))
1665 m
->m_pkthdr
.len
= totlen
;
1669 char *mcl_to_paddr(register char *addr
) {
1670 register int base_phys
;
1672 if (addr
< (char *)mbutl
|| addr
>= (char *)embutl
)
1674 base_phys
= mcl_paddr
[(addr
- (char *)mbutl
) >> PAGE_SHIFT
];
1678 return ((char *)((int)base_phys
| ((int)addr
& PAGE_MASK
)));
1682 * Dup the mbuf chain passed in. The whole thing. No cute additional cruft.
1683 * And really copy the thing. That way, we don't "precompute" checksums
1684 * for unsuspecting consumers.
1685 * Assumption: m->m_nextpkt == 0.
1686 * Trick: for small packets, don't dup into a cluster. That way received
1687 * packets don't take up too much room in the sockbuf (cf. sbspace()).
1692 m_dup(register struct mbuf
*m
, int how
)
1693 { register struct mbuf
*n
, **np
;
1699 if (m
->m_flags
& M_PKTHDR
)
1703 * Quick check: if we have one mbuf and its data fits in an
1704 * mbuf with packet header, just copy and go.
1706 if (m
->m_next
== NULL
)
1707 { /* Then just move the data into an mbuf and be done... */
1709 { if (m
->m_pkthdr
.len
<= MHLEN
)
1710 { if ((n
= m_gethdr(how
, m
->m_type
)) == NULL
)
1712 n
->m_len
= m
->m_len
;
1713 n
->m_flags
|= (m
->m_flags
& M_COPYFLAGS
);
1714 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1715 n
->m_pkthdr
.rcvif
= m
->m_pkthdr
.rcvif
;
1716 n
->m_pkthdr
.header
= NULL
;
1717 n
->m_pkthdr
.csum_flags
= 0;
1718 n
->m_pkthdr
.csum_data
= 0;
1719 n
->m_pkthdr
.aux
= NULL
;
1720 n
->m_pkthdr
.reserved1
= 0;
1721 n
->m_pkthdr
.reserved2
= 0;
1722 bcopy(m
->m_data
, n
->m_data
, m
->m_pkthdr
.len
);
1725 } else if (m
->m_len
<= MLEN
)
1726 { if ((n
= m_get(how
, m
->m_type
)) == NULL
)
1728 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
1729 n
->m_len
= m
->m_len
;
1736 kprintf("<%x: %x, %x, %x\n", m
, m
->m_flags
, m
->m_len
,
1740 n
= m_gethdr(how
, m
->m_type
);
1742 n
= m_get(how
, m
->m_type
);
1745 if (m
->m_flags
& M_EXT
)
1747 if ((n
->m_flags
& M_EXT
) == 0)
1752 { /* Don't use M_COPY_PKTHDR: preserve m_data */
1753 n
->m_pkthdr
= m
->m_pkthdr
;
1754 n
->m_flags
|= (m
->m_flags
& M_COPYFLAGS
);
1756 if ((n
->m_flags
& M_EXT
) == 0)
1757 n
->m_data
= n
->m_pktdat
;
1759 n
->m_len
= m
->m_len
;
1761 * Get the dup on the same bdry as the original
1762 * Assume that the two mbufs have the same offset to data area
1763 * (up to word bdries)
1765 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), (unsigned)n
->m_len
);
1769 kprintf(">%x: %x, %x, %x\n", n
, n
->m_flags
, n
->m_len
,
1784 m_mclref(struct mbuf
*p
)
1786 return (_MCLREF(p
));
1790 m_mclunref(struct mbuf
*p
)
1792 return (_MCLUNREF(p
));
1795 /* change mbuf to new type */
1797 m_mchtype(struct mbuf
*m
, int t
)
1800 mbstat
.m_mtypes
[(m
)->m_type
]--;
1801 mbstat
.m_mtypes
[t
]++;
1806 void *m_mtod(struct mbuf
*m
)
1808 return ((m
)->m_data
);
1811 struct mbuf
*m_dtom(void *x
)
1813 return ((struct mbuf
*)((u_long
)(x
) & ~(MSIZE
-1)));
1816 int m_mtocl(void *x
)
1818 return (((char *)(x
) - (char *)mbutl
) / sizeof(union mcluster
));
1821 union mcluster
*m_cltom(int x
)
1823 return ((union mcluster
*)(mbutl
+ (x
)));
1827 void m_mcheck(struct mbuf
*m
)
1829 if (m
->m_type
!= MT_FREE
)
1830 panic("mget MCHECK: m_type=%x m=%x", m
->m_type
, m
);
1834 #include <sys/sysctl.h>
1836 static int mhog_num
= 0;
1837 static struct mbuf
*mhog_chain
= 0;
1838 static int mhog_wait
= 1;
1841 sysctl_mhog_num SYSCTL_HANDLER_ARGS
1846 error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
1847 if (!error
&& req
->newptr
) {
1852 m_freem(mhog_chain
);
1856 for (i
= 0; i
< mhog_num
; i
++) {
1857 MGETHDR(m
, mhog_wait
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
1861 MCLGET(m
, mhog_wait
? M_WAIT
: M_DONTWAIT
);
1862 if ((m
->m_flags
& M_EXT
) == 0) {
1867 m
->m_next
= mhog_chain
;
1876 SYSCTL_NODE(_kern_ipc
, OID_AUTO
, mhog
, CTLFLAG_RW
, 0, "mbuf hog");
1878 SYSCTL_PROC(_kern_ipc_mhog
, OID_AUTO
, cluster
, CTLTYPE_INT
|CTLFLAG_RW
,
1879 &mhog_num
, 0, &sysctl_mhog_num
, "I", "");
1880 SYSCTL_INT(_kern_ipc_mhog
, OID_AUTO
, wait
, CTLFLAG_RW
, &mhog_wait
,