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)
89 #define _M_CLEAR_PKTHDR(mbuf_ptr) (mbuf_ptr)->m_pkthdr.rcvif = NULL; \
90 (mbuf_ptr)->m_pkthdr.len = 0; \
91 (mbuf_ptr)->m_pkthdr.header = NULL; \
92 (mbuf_ptr)->m_pkthdr.csum_flags = 0; \
93 (mbuf_ptr)->m_pkthdr.csum_data = 0; \
94 (mbuf_ptr)->m_pkthdr.aux = (struct mbuf*)NULL; \
95 (mbuf_ptr)->m_pkthdr.reserved_1 = 0; \
96 (mbuf_ptr)->m_pkthdr.vlan_tag = 0; \
97 (mbuf_ptr)->m_pkthdr.reserved2 = NULL;
99 extern pmap_t kernel_pmap
; /* The kernel's pmap */
100 /* kernel translater */
101 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
103 decl_simple_lock_data(, mbuf_slock
);
104 struct mbuf
*mfree
; /* mbuf free list */
105 struct mbuf
*mfreelater
; /* mbuf deallocation list */
106 extern vm_map_t mb_map
; /* special map */
107 int m_want
; /* sleepers on mbufs */
108 extern int nmbclusters
; /* max number of mapped clusters */
109 short *mclrefcnt
; /* mapped cluster reference counts */
111 static ppnum_t mcl_paddr_base
; /* Handle returned by IOMapper::iovmAlloc() */
112 union mcluster
*mclfree
; /* mapped cluster free list */
113 int max_linkhdr
; /* largest link-level header */
114 int max_protohdr
; /* largest protocol header */
115 int max_hdr
; /* largest link+protocol header */
116 int max_datalen
; /* MHLEN - max_hdr */
117 struct mbstat mbstat
; /* statistics */
118 union mcluster
*mbutl
; /* first mapped cluster address */
119 union mcluster
*embutl
; /* ending virtual address of mclusters */
121 static int nclpp
; /* # clusters per physical page */
122 static char mbfail
[] = "mbuf not mapped";
124 static int m_howmany();
126 /* The number of cluster mbufs that are allocated, to start. */
127 #define MINCL max(16, 2)
129 static int mbuf_expand_thread_wakeup
= 0;
130 static int mbuf_expand_mcl
= 0;
131 static int mbuf_expand_thread_initialized
= 0;
133 static void mbuf_expand_thread_init(void);
136 static int mfree_munge
= 0;
138 #define _MFREE_MUNGE(m) { \
141 vm_offset_t *element = (vm_offset_t *)(m); \
143 i < sizeof(struct mbuf)/sizeof(vm_offset_t); \
145 (element)[i] = 0xdeadbeef; \
150 munge_mbuf(struct mbuf
*m
)
153 vm_offset_t
*element
= (vm_offset_t
*)(m
);
155 i
< sizeof(struct mbuf
)/sizeof(vm_offset_t
);
157 (element
)[i
] = 0xdeadbeef;
159 #define _MFREE_MUNGE(m) { \
165 #define _MFREE_MUNGE(m)
169 #define _MINTGET(m, type) { \
171 if (((m) = mfree) != 0) { \
173 ++mclrefcnt[mtocl(m)]; \
174 mbstat.m_mtypes[MT_FREE]--; \
175 mbstat.m_mtypes[(type)]++; \
176 mfree = (m)->m_next; \
191 nclpp
= round_page_32(MCLBYTES
) / MCLBYTES
; /* see mbufgc() */
192 if (nclpp
< 1) nclpp
= 1;
194 // NETISR_LOCKINIT();
196 mbstat
.m_msize
= MSIZE
;
197 mbstat
.m_mclbytes
= MCLBYTES
;
198 mbstat
.m_minclsize
= MINCLSIZE
;
199 mbstat
.m_mlen
= MLEN
;
200 mbstat
.m_mhlen
= MHLEN
;
202 if (nmbclusters
== 0)
203 nmbclusters
= NMBCLUSTERS
;
204 MALLOC(mclrefcnt
, short *, nmbclusters
* sizeof (short),
208 for (m
= 0; m
< nmbclusters
; m
++)
211 /* Calculate the number of pages assigned to the cluster pool */
212 mcl_pages
= nmbclusters
/(PAGE_SIZE
/CLBYTES
);
213 MALLOC(mcl_paddr
, int *, mcl_pages
* sizeof(int), M_TEMP
, M_WAITOK
);
216 /* Register with the I/O Bus mapper */
217 mcl_paddr_base
= IOMapperIOVMAlloc(mcl_pages
);
218 bzero((char *)mcl_paddr
, mcl_pages
* sizeof(int));
220 embutl
= (union mcluster
*)((unsigned char *)mbutl
+ (nmbclusters
* MCLBYTES
));
222 PE_parse_boot_arg("initmcl", &initmcl
);
224 if (m_clalloc(max(PAGE_SIZE
/CLBYTES
, 1) * initmcl
, M_WAIT
) == 0)
228 (void) kernel_thread(kernel_task
, mbuf_expand_thread_init
);
236 * Allocate some number of mbuf clusters
237 * and place on cluster free list.
240 m_clalloc(ncl
, nowait
)
244 register union mcluster
*mcl
;
247 static char doing_alloc
;
250 * Honor the caller's wish to block or not block.
251 * We have a way to grow the pool asynchronously,
252 * by kicking the dlil_input_thread.
254 if ((i
= m_howmany()) <= 0)
257 if ((nowait
== M_DONTWAIT
))
262 size
= round_page_32(ncl
* MCLBYTES
);
263 mcl
= (union mcluster
*)kmem_mb_alloc(mb_map
, size
);
265 if (mcl
== 0 && ncl
> 1) {
266 size
= round_page_32(MCLBYTES
); /* Try for 1 if failed */
267 mcl
= (union mcluster
*)kmem_mb_alloc(mb_map
, size
);
272 ncl
= size
/ MCLBYTES
;
273 for (i
= 0; i
< ncl
; i
++) {
274 if (++mclrefcnt
[mtocl(mcl
)] != 0)
275 panic("m_clalloc already there");
276 if (((int)mcl
& PAGE_MASK
) == 0) {
277 ppnum_t offset
= ((char *)mcl
- (char *)mbutl
)/PAGE_SIZE
;
278 ppnum_t new_page
= pmap_find_phys(kernel_pmap
, (vm_address_t
) mcl
);
281 * In the case of no mapper being available
282 * the following code nops and returns the
283 * input page, if there is a mapper the I/O
284 * page appropriate is returned.
286 new_page
= IOMapperInsertPage(mcl_paddr_base
, offset
, new_page
);
287 mcl_paddr
[offset
] = new_page
<< 12;
290 mcl
->mcl_next
= mclfree
;
293 mbstat
.m_clfree
+= ncl
;
294 mbstat
.m_clusters
+= ncl
;
301 * When non-blocking we kick the dlil thread if we havve to grow the
302 * pool or if the number of free clusters is less than requested.
304 if ((nowait
== M_DONTWAIT
) && (i
> 0 || ncl
>= mbstat
.m_clfree
)) {
306 if (mbuf_expand_thread_initialized
)
307 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
310 if (mbstat
.m_clfree
>= ncl
)
317 * Add more free mbufs by cutting up a cluster.
322 register caddr_t mcl
;
324 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4))
325 /* 1/16th of the total number of cluster mbufs allocated is
326 reserved for large packets. The number reserved must
327 always be < 1/2, or future allocation will be prevented.
331 MCLALLOC(mcl
, canwait
);
333 register struct mbuf
*m
= (struct mbuf
*)mcl
;
334 register int i
= NMBPCL
;
336 mbstat
.m_mtypes
[MT_FREE
] += i
;
347 if (i
) wakeup((caddr_t
)&mfree
);
354 * When MGET failes, ask protocols to free space when short of memory,
355 * then re-attempt to allocate an mbuf.
358 m_retry(canwait
, type
)
361 register struct mbuf
*m
;
365 boolean_t funnel_state
;
368 (void) m_expand(canwait
);
371 (m
)->m_next
= (m
)->m_nextpkt
= 0;
372 (m
)->m_type
= (type
);
373 (m
)->m_data
= (m
)->m_dat
;
376 if (m
|| canwait
== M_DONTWAIT
)
387 if (mbuf_expand_thread_initialized
)
388 wakeup((caddr_t
)&mbuf_expand_thread_wakeup
);
391 * Need to be inside network funnel for m_reclaim because it calls into the
392 * socket domains and tsleep end-up calling splhigh
394 fnl
= thread_funnel_get();
395 if (wait
== 0 && fnl
== network_flock
) {
397 } else if (fnl
!= THR_FUNNEL_NULL
) {
398 /* Sleep with a small timeout as insurance */
399 (void) tsleep((caddr_t
)&mfree
, PZERO
-1, "m_retry", hz
);
401 /* We are called from a non-BSD context: use mach primitives */
402 u_int64_t abstime
= 0;
404 assert_wait((event_t
)&mfree
, THREAD_UNINT
);
405 clock_interval_to_deadline(hz
, NSEC_PER_SEC
/ hz
, &abstime
);
406 thread_set_timer_deadline(abstime
);
407 if (thread_block(THREAD_CONTINUE_NULL
) != THREAD_TIMED_OUT
)
408 thread_cancel_timer();
417 * As above; retry an MGETHDR.
420 m_retryhdr(canwait
, type
)
423 register struct mbuf
*m
;
425 if (m
= m_retry(canwait
, type
)) {
426 m
->m_flags
|= M_PKTHDR
;
427 m
->m_data
= m
->m_pktdat
;
435 register struct domain
*dp
;
436 register struct protosw
*pr
;
438 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
439 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
)
446 * Space allocation routines.
447 * These are also available as macros
448 * for critical paths.
454 register struct mbuf
*m
;
458 m
->m_next
= m
->m_nextpkt
= 0;
460 m
->m_data
= m
->m_dat
;
463 (m
) = m_retry(nowait
, type
);
469 m_gethdr(nowait
, type
)
472 register struct mbuf
*m
;
476 m
->m_next
= m
->m_nextpkt
= 0;
478 m
->m_data
= m
->m_pktdat
;
479 m
->m_flags
= M_PKTHDR
;
482 m
= m_retryhdr(nowait
, type
);
488 m_getclr(nowait
, type
)
491 register struct mbuf
*m
;
493 MGET(m
, nowait
, type
);
496 bzero(mtod(m
, caddr_t
), MLEN
);
504 struct mbuf
*n
= m
->m_next
;
507 if (m
->m_type
== MT_FREE
)
508 panic("freeing free mbuf");
510 /* Free the aux data if there is any */
511 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
)
513 m_freem(m
->m_pkthdr
.aux
);
517 if ((m
->m_flags
& M_EXT
))
519 if (MCLHASREFERENCE(m
)) {
520 remque((queue_t
)&m
->m_ext
.ext_refs
);
521 } else if (m
->m_ext
.ext_free
== NULL
) {
522 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
523 if (_MCLUNREF(mcl
)) {
524 mcl
->mcl_next
= mclfree
;
529 /* *** Since m_split() increments "mclrefcnt[mtocl(m->m_ext.ext_buf)]",
530 and AppleTalk ADSP uses m_split(), this incorrect sanity check
533 else /* sanity check - not referenced this way */
534 panic("m_free m_ext cluster not free");
537 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
538 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
541 mbstat
.m_mtypes
[m
->m_type
]--;
545 mbstat
.m_mtypes
[m
->m_type
]++;
553 if (i
) wakeup((caddr_t
)&mfree
);
557 /* m_mclget() add an mbuf cluster to a normal mbuf */
563 MCLALLOC(m
->m_ext
.ext_buf
, nowait
);
564 if (m
->m_ext
.ext_buf
) {
565 m
->m_data
= m
->m_ext
.ext_buf
;
567 m
->m_ext
.ext_size
= MCLBYTES
;
568 m
->m_ext
.ext_free
= 0;
569 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
576 /* m_mclalloc() allocate an mbuf cluster */
583 (void)m_clalloc(1, nowait
);
584 if ((p
= (caddr_t
)mclfree
)) {
585 ++mclrefcnt
[mtocl(p
)];
587 mclfree
= ((union mcluster
*)p
)->mcl_next
;
596 /* m_mclfree() releases a reference to a cluster allocated by MCLALLOC,
597 * freeing the cluster if the reference count has reached 0. */
603 if (--mclrefcnt
[mtocl(p
)] == 0) {
604 ((union mcluster
*)(p
))->mcl_next
= mclfree
;
605 mclfree
= (union mcluster
*)(p
);
611 /* mcl_hasreference() checks if a cluster of an mbuf is referenced by another mbuf */
616 return (m
->m_ext
.ext_refs
.forward
!= &(m
->m_ext
.ext_refs
));
621 m_copy_pkthdr(to
, from
)
622 struct mbuf
*to
, *from
;
624 to
->m_pkthdr
= from
->m_pkthdr
;
625 from
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
626 to
->m_flags
= from
->m_flags
& M_COPYFLAGS
;
627 to
->m_data
= (to
)->m_pktdat
;
630 /* Best effort to get a mbuf cluster + pkthdr under one lock.
631 * If we don't have them avail, just bail out and use the regular
633 * Used by drivers to allocated packets on receive ring.
639 m_clalloc(1, M_DONTWAIT
); /* takes the MBUF_LOCK, but doesn't release it... */
640 if ((mfree
!= 0) && (mclfree
!= 0)) { /* mbuf + cluster are available */
644 ++mclrefcnt
[mtocl(m
)];
645 mbstat
.m_mtypes
[MT_FREE
]--;
646 mbstat
.m_mtypes
[MT_DATA
]++;
647 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
648 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
650 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
652 m
->m_next
= m
->m_nextpkt
= 0;
654 m
->m_data
= m
->m_ext
.ext_buf
;
655 m
->m_flags
= M_PKTHDR
| M_EXT
;
657 m
->m_ext
.ext_free
= 0;
658 m
->m_ext
.ext_size
= MCLBYTES
;
659 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
663 else { /* slow path: either mbuf or cluster need to be allocated anyway */
666 MGETHDR(m
, M_WAITOK
, MT_DATA
);
671 MCLGET( m
, M_WAITOK
);
672 if ( ( m
->m_flags
& M_EXT
) == 0 )
682 * return a list of mbuf hdrs that point to clusters...
683 * try for num_needed, if this can't be met, return whatever
684 * number were available... set up the first num_with_pkthdrs
685 * with mbuf hdrs configured as packet headers... these are
686 * chained on the m_nextpkt field... any packets requested beyond
687 * this are chained onto the last packet header's m_next field.
690 m_getpackets(int num_needed
, int num_with_pkthdrs
, int how
)
693 struct mbuf
**np
, *top
;
698 m_clalloc(num_needed
, how
); /* takes the MBUF_LOCK, but doesn't release it... */
700 while (num_needed
--) {
701 if (mfree
&& mclfree
) { /* mbuf + cluster are available */
705 ++mclrefcnt
[mtocl(m
)];
706 mbstat
.m_mtypes
[MT_FREE
]--;
707 mbstat
.m_mtypes
[MT_DATA
]++;
708 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
709 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
711 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
713 m
->m_next
= m
->m_nextpkt
= 0;
715 m
->m_data
= m
->m_ext
.ext_buf
;
716 m
->m_ext
.ext_free
= 0;
717 m
->m_ext
.ext_size
= MCLBYTES
;
718 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
720 if (num_with_pkthdrs
== 0)
723 m
->m_flags
= M_PKTHDR
| M_EXT
;
733 if (num_with_pkthdrs
== 0) {
734 MGET(m
, how
, MT_DATA
);
736 MGETHDR(m
, how
, MT_DATA
);
744 if ((m
->m_flags
& M_EXT
) == 0) {
752 if (num_with_pkthdrs
)
764 * return a list of mbuf hdrs set up as packet hdrs
765 * chained together on the m_nextpkt field
768 m_getpackethdrs(int num_needed
, int how
)
771 struct mbuf
**np
, *top
;
778 while (num_needed
--) {
779 if (m
= mfree
) { /* mbufs are available */
782 ++mclrefcnt
[mtocl(m
)];
783 mbstat
.m_mtypes
[MT_FREE
]--;
784 mbstat
.m_mtypes
[MT_DATA
]++;
786 m
->m_next
= m
->m_nextpkt
= 0;
788 m
->m_flags
= M_PKTHDR
;
789 m
->m_data
= m
->m_pktdat
;
796 m
= m_retryhdr(how
, MT_DATA
);
812 /* free and mbuf list (m_nextpkt) while following m_next under one lock.
813 * returns the count for mbufs packets freed. Used by the drivers.
819 struct mbuf
*nextpkt
;
826 nextpkt
= m
->m_nextpkt
; /* chain of linked mbufs from driver */
832 while (m
) { /* free the mbuf chain (like mfreem) */
836 /* Free the aux data if there is any */
837 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
) {
839 * Treat the current m as the nextpkt and set m
840 * to the aux data. Preserve nextpkt in m->m_nextpkt.
841 * This lets us free the aux data in this loop
842 * without having to call m_freem recursively,
843 * which wouldn't work because we've still got
846 m
->m_nextpkt
= nextpkt
;
848 m
= nextpkt
->m_pkthdr
.aux
;
849 nextpkt
->m_pkthdr
.aux
= NULL
;
854 if (n
&& n
->m_nextpkt
)
855 panic("m_freem_list: m_nextpkt of m_next != NULL");
856 if (m
->m_type
== MT_FREE
)
857 panic("freeing free mbuf");
859 if (m
->m_flags
& M_EXT
) {
860 if (MCLHASREFERENCE(m
)) {
861 remque((queue_t
)&m
->m_ext
.ext_refs
);
862 } else if (m
->m_ext
.ext_free
== NULL
) {
863 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
864 if (_MCLUNREF(mcl
)) {
865 mcl
->mcl_next
= mclfree
;
870 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
871 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
874 mbstat
.m_mtypes
[m
->m_type
]--;
877 mbstat
.m_mtypes
[MT_FREE
]++;
885 m
= nextpkt
; /* bump m with saved nextpkt if any */
893 wakeup((caddr_t
)&mfree
);
900 register struct mbuf
*m
;
907 * Mbuffer utility routines.
910 * Compute the amount of space available
911 * before the current start of data in an mbuf.
914 register struct mbuf
*m
;
916 if (m
->m_flags
& M_EXT
) {
917 if (MCLHASREFERENCE(m
))
919 return (m
->m_data
- m
->m_ext
.ext_buf
);
921 if (m
->m_flags
& M_PKTHDR
)
922 return (m
->m_data
- m
->m_pktdat
);
923 return (m
->m_data
- m
->m_dat
);
927 * Compute the amount of space available
928 * after the end of data in an mbuf.
931 register struct mbuf
*m
;
933 if (m
->m_flags
& M_EXT
) {
934 if (MCLHASREFERENCE(m
))
936 return (m
->m_ext
.ext_buf
+ m
->m_ext
.ext_size
-
937 (m
->m_data
+ m
->m_len
));
939 return (&m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
));
943 * Lesser-used path for M_PREPEND:
944 * allocate new mbuf to prepend to chain,
946 * Does not adjust packet header length.
949 m_prepend(m
, len
, how
)
950 register struct mbuf
*m
;
955 MGET(mn
, how
, m
->m_type
);
956 if (mn
== (struct mbuf
*)NULL
) {
958 return ((struct mbuf
*)NULL
);
960 if (m
->m_flags
& M_PKTHDR
) {
961 M_COPY_PKTHDR(mn
, m
);
962 m
->m_flags
&= ~M_PKTHDR
;
973 * Replacement for old M_PREPEND macro:
974 * allocate new mbuf to prepend to chain,
975 * copy junk along, and adjust length.
979 m_prepend_2(m
, len
, how
)
980 register struct mbuf
*m
;
983 if (M_LEADINGSPACE(m
) >= len
) {
987 m
= m_prepend(m
, len
, how
);
989 if ((m
) && (m
->m_flags
& M_PKTHDR
))
990 m
->m_pkthdr
.len
+= len
;
995 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
996 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
997 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
1002 m_copym(m
, off0
, len
, wait
)
1003 register struct mbuf
*m
;
1007 register struct mbuf
*n
, **np
;
1008 register int off
= off0
;
1012 if (off
< 0 || len
< 0)
1014 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1017 while (off
>= m
->m_len
) {
1030 if (len
!= M_COPYALL
)
1036 ++mclrefcnt
[mtocl(n
)];
1037 mbstat
.m_mtypes
[MT_FREE
]--;
1038 mbstat
.m_mtypes
[m
->m_type
]++;
1040 n
->m_next
= n
->m_nextpkt
= 0;
1041 n
->m_type
= m
->m_type
;
1042 n
->m_data
= n
->m_dat
;
1046 n
= m_retry(wait
, m
->m_type
);
1054 M_COPY_PKTHDR(n
, m
);
1055 if (len
== M_COPYALL
)
1056 n
->m_pkthdr
.len
-= off0
;
1058 n
->m_pkthdr
.len
= len
;
1061 if (len
== M_COPYALL
) {
1062 if (min(len
, (m
->m_len
- off
)) == len
) {
1063 printf("m->m_len %d - off %d = %d, %d\n",
1064 m
->m_len
, off
, m
->m_len
- off
,
1065 min(len
, (m
->m_len
- off
)));
1068 n
->m_len
= min(len
, (m
->m_len
- off
));
1069 if (n
->m_len
== M_COPYALL
) {
1070 printf("n->m_len == M_COPYALL, fixing\n");
1073 if (m
->m_flags
& M_EXT
) {
1074 n
->m_ext
= m
->m_ext
;
1075 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1076 n
->m_data
= m
->m_data
+ off
;
1077 n
->m_flags
|= M_EXT
;
1079 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1080 (unsigned)n
->m_len
);
1082 if (len
!= M_COPYALL
)
1104 * equivilent to m_copym except that all necessary
1105 * mbuf hdrs are allocated within this routine
1106 * also, the last mbuf and offset accessed are passed
1107 * out and can be passed back in to avoid having to
1108 * rescan the entire mbuf list (normally hung off of the socket)
1111 m_copym_with_hdrs(m
, off0
, len
, wait
, m_last
, m_off
)
1112 register struct mbuf
*m
;
1115 struct mbuf
**m_last
;
1118 register struct mbuf
*n
, **np
;
1119 register int off
= off0
;
1120 struct mbuf
*top
= 0;
1124 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1131 while (off
>= m
->m_len
) {
1143 panic("m_gethdr_and_copym");
1148 ++mclrefcnt
[mtocl(n
)];
1149 mbstat
.m_mtypes
[MT_FREE
]--;
1150 mbstat
.m_mtypes
[type
]++;
1152 n
->m_next
= n
->m_nextpkt
= 0;
1156 n
->m_data
= n
->m_dat
;
1159 n
->m_data
= n
->m_pktdat
;
1160 n
->m_flags
= M_PKTHDR
;
1166 n
= m_retry(wait
, type
);
1168 n
= m_retryhdr(wait
, type
);
1181 M_COPY_PKTHDR(n
, m
);
1182 n
->m_pkthdr
.len
= len
;
1185 n
->m_len
= min(len
, (m
->m_len
- off
));
1187 if (m
->m_flags
& M_EXT
) {
1188 n
->m_ext
= m
->m_ext
;
1189 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1190 n
->m_data
= m
->m_data
+ off
;
1191 n
->m_flags
|= M_EXT
;
1193 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1194 (unsigned)n
->m_len
);
1199 if ((off
+ n
->m_len
) == m
->m_len
) {
1200 *m_last
= m
->m_next
;
1204 *m_off
= off
+ n
->m_len
;
1226 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1227 * continuing for "len" bytes, into the indicated buffer.
1229 void m_copydata(m
, off
, len
, cp
)
1230 register struct mbuf
*m
;
1235 register unsigned count
;
1237 if (off
< 0 || len
< 0)
1238 panic("m_copydata");
1241 panic("m_copydata");
1249 panic("m_copydata");
1250 count
= min(m
->m_len
- off
, len
);
1251 bcopy(mtod(m
, caddr_t
) + off
, cp
, count
);
1260 * Concatenate mbuf chain n to m.
1261 * Both chains must be of the same type (e.g. MT_DATA).
1262 * Any m_pkthdr is not updated.
1265 register struct mbuf
*m
, *n
;
1270 if (m
->m_flags
& M_EXT
||
1271 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
1272 /* just join the two chains */
1276 /* splat the data from one into the other */
1277 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1279 m
->m_len
+= n
->m_len
;
1289 register int len
= req_len
;
1290 register struct mbuf
*m
;
1293 if ((m
= mp
) == NULL
)
1299 while (m
!= NULL
&& len
> 0) {
1300 if (m
->m_len
<= len
) {
1311 if (m
->m_flags
& M_PKTHDR
)
1312 m
->m_pkthdr
.len
-= (req_len
- len
);
1315 * Trim from tail. Scan the mbuf chain,
1316 * calculating its length and finding the last mbuf.
1317 * If the adjustment only affects this mbuf, then just
1318 * adjust and return. Otherwise, rescan and truncate
1319 * after the remaining size.
1325 if (m
->m_next
== (struct mbuf
*)0)
1329 if (m
->m_len
>= len
) {
1332 if (m
->m_flags
& M_PKTHDR
)
1333 m
->m_pkthdr
.len
-= len
;
1340 * Correct length for chain is "count".
1341 * Find the mbuf with last data, adjust its length,
1342 * and toss data from remaining mbufs on chain.
1345 if (m
->m_flags
& M_PKTHDR
)
1346 m
->m_pkthdr
.len
= count
;
1347 for (; m
; m
= m
->m_next
) {
1348 if (m
->m_len
>= count
) {
1354 while (m
= m
->m_next
)
1360 * Rearange an mbuf chain so that len bytes are contiguous
1361 * and in the data area of an mbuf (so that mtod and dtom
1362 * will work for a structure of size len). Returns the resulting
1363 * mbuf chain on success, frees it and returns null on failure.
1364 * If there is room, it will add up to max_protohdr-len extra bytes to the
1365 * contiguous region in an attempt to avoid being called next time.
1371 register struct mbuf
*n
;
1374 register struct mbuf
*m
;
1379 * If first mbuf has no cluster, and has room for len bytes
1380 * without shifting current data, pullup into it,
1381 * otherwise allocate a new mbuf to prepend to the chain.
1383 if ((n
->m_flags
& M_EXT
) == 0 &&
1384 n
->m_data
+ len
< &n
->m_dat
[MLEN
] && n
->m_next
) {
1385 if (n
->m_len
>= len
)
1393 MGET(m
, M_DONTWAIT
, n
->m_type
);
1397 if (n
->m_flags
& M_PKTHDR
) {
1398 M_COPY_PKTHDR(m
, n
);
1399 n
->m_flags
&= ~M_PKTHDR
;
1402 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
1404 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
1405 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1415 } while (len
> 0 && n
);
1429 * Partition an mbuf chain in two pieces, returning the tail --
1430 * all but the first len0 bytes. In case of failure, it returns NULL and
1431 * attempts to restore the chain to its original state.
1434 m_split(m0
, len0
, wait
)
1435 register struct mbuf
*m0
;
1438 register struct mbuf
*m
, *n
;
1439 unsigned len
= len0
, remain
;
1441 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
1445 remain
= m
->m_len
- len
;
1446 if (m0
->m_flags
& M_PKTHDR
) {
1447 MGETHDR(n
, wait
, m0
->m_type
);
1450 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
1451 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
1452 m0
->m_pkthdr
.len
= len0
;
1453 if (m
->m_flags
& M_EXT
)
1455 if (remain
> MHLEN
) {
1456 /* m can't be the lead packet */
1458 n
->m_next
= m_split(m
, len
, wait
);
1459 if (n
->m_next
== 0) {
1465 MH_ALIGN(n
, remain
);
1466 } else if (remain
== 0) {
1471 MGET(n
, wait
, m
->m_type
);
1477 if (m
->m_flags
& M_EXT
) {
1478 n
->m_flags
|= M_EXT
;
1480 n
->m_ext
= m
->m_ext
;
1481 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1483 n
->m_data
= m
->m_data
+ len
;
1485 bcopy(mtod(m
, caddr_t
) + len
, mtod(n
, caddr_t
), remain
);
1489 n
->m_next
= m
->m_next
;
1494 * Routine to copy from device local memory into mbufs.
1497 m_devget(buf
, totlen
, off0
, ifp
, copy
)
1503 register struct mbuf
*m
;
1504 struct mbuf
*top
= 0, **mp
= &top
;
1505 register int off
= off0
, len
;
1513 * If 'off' is non-zero, packet is trailer-encapsulated,
1514 * so we have to skip the type and length fields.
1516 cp
+= off
+ 2 * sizeof(u_int16_t
);
1517 totlen
-= 2 * sizeof(u_int16_t
);
1519 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1522 m
->m_pkthdr
.rcvif
= ifp
;
1523 m
->m_pkthdr
.len
= totlen
;
1526 while (totlen
> 0) {
1528 MGET(m
, M_DONTWAIT
, MT_DATA
);
1535 len
= min(totlen
, epkt
- cp
);
1536 if (len
>= MINCLSIZE
) {
1537 MCLGET(m
, M_DONTWAIT
);
1538 if (m
->m_flags
& M_EXT
)
1539 m
->m_len
= len
= min(len
, MCLBYTES
);
1541 /* give up when it's out of cluster mbufs */
1549 * Place initial small packet/header at end of mbuf.
1551 if (len
< m
->m_len
) {
1552 if (top
== 0 && len
+ max_linkhdr
<= m
->m_len
)
1553 m
->m_data
+= max_linkhdr
;
1559 copy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
1561 bcopy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
1573 * Cluster freelist allocation check. The mbuf lock must be held.
1574 * Ensure hysteresis between hi/lo.
1582 if (mbstat
.m_clusters
< MINCL
)
1583 return (MINCL
- mbstat
.m_clusters
);
1584 /* Too few (free < 1/2 total) and not over maximum */
1585 if (mbstat
.m_clusters
< nmbclusters
&&
1586 (i
= ((mbstat
.m_clusters
>> 1) - mbstat
.m_clfree
)) > 0)
1593 * Copy data from a buffer back into the indicated mbuf chain,
1594 * starting "off" bytes from the beginning, extending the mbuf
1595 * chain if necessary.
1598 m_copyback(m0
, off
, len
, cp
)
1605 register struct mbuf
*m
= m0
, *n
;
1610 while (off
> (mlen
= m
->m_len
)) {
1613 if (m
->m_next
== 0) {
1614 n
= m_getclr(M_DONTWAIT
, m
->m_type
);
1617 n
->m_len
= min(MLEN
, len
+ off
);
1623 mlen
= min (m
->m_len
- off
, len
);
1624 bcopy(cp
, off
+ mtod(m
, caddr_t
), (unsigned)mlen
);
1632 if (m
->m_next
== 0) {
1633 n
= m_get(M_DONTWAIT
, m
->m_type
);
1636 n
->m_len
= min(MLEN
, len
);
1641 out
: if (((m
= m0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
))
1642 m
->m_pkthdr
.len
= totlen
;
1646 char *mcl_to_paddr(register char *addr
) {
1647 register int base_phys
;
1649 if (addr
< (char *)mbutl
|| addr
>= (char *)embutl
)
1651 base_phys
= mcl_paddr
[(addr
- (char *)mbutl
) >> PAGE_SHIFT
];
1655 return ((char *)((int)base_phys
| ((int)addr
& PAGE_MASK
)));
1659 * Dup the mbuf chain passed in. The whole thing. No cute additional cruft.
1660 * And really copy the thing. That way, we don't "precompute" checksums
1661 * for unsuspecting consumers.
1662 * Assumption: m->m_nextpkt == 0.
1663 * Trick: for small packets, don't dup into a cluster. That way received
1664 * packets don't take up too much room in the sockbuf (cf. sbspace()).
1669 m_dup(register struct mbuf
*m
, int how
)
1670 { register struct mbuf
*n
, **np
;
1676 if (m
->m_flags
& M_PKTHDR
)
1680 * Quick check: if we have one mbuf and its data fits in an
1681 * mbuf with packet header, just copy and go.
1683 if (m
->m_next
== NULL
)
1684 { /* Then just move the data into an mbuf and be done... */
1686 { if (m
->m_pkthdr
.len
<= MHLEN
)
1687 { if ((n
= m_gethdr(how
, m
->m_type
)) == NULL
)
1689 n
->m_len
= m
->m_len
;
1690 n
->m_flags
|= (m
->m_flags
& M_COPYFLAGS
);
1691 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1692 n
->m_pkthdr
.rcvif
= m
->m_pkthdr
.rcvif
;
1693 n
->m_pkthdr
.header
= NULL
;
1694 n
->m_pkthdr
.csum_flags
= 0;
1695 n
->m_pkthdr
.csum_data
= 0;
1696 n
->m_pkthdr
.aux
= NULL
;
1697 n
->m_pkthdr
.vlan_tag
= 0;
1698 n
->m_pkthdr
.reserved_1
= 0;
1699 n
->m_pkthdr
.reserved2
= 0;
1700 bcopy(m
->m_data
, n
->m_data
, m
->m_pkthdr
.len
);
1703 } else if (m
->m_len
<= MLEN
)
1704 { if ((n
= m_get(how
, m
->m_type
)) == NULL
)
1706 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
1707 n
->m_len
= m
->m_len
;
1714 kprintf("<%x: %x, %x, %x\n", m
, m
->m_flags
, m
->m_len
,
1718 n
= m_gethdr(how
, m
->m_type
);
1720 n
= m_get(how
, m
->m_type
);
1723 if (m
->m_flags
& M_EXT
)
1725 if ((n
->m_flags
& M_EXT
) == 0)
1730 { /* Don't use M_COPY_PKTHDR: preserve m_data */
1731 n
->m_pkthdr
= m
->m_pkthdr
;
1732 n
->m_flags
|= (m
->m_flags
& M_COPYFLAGS
);
1734 if ((n
->m_flags
& M_EXT
) == 0)
1735 n
->m_data
= n
->m_pktdat
;
1737 n
->m_len
= m
->m_len
;
1739 * Get the dup on the same bdry as the original
1740 * Assume that the two mbufs have the same offset to data area
1741 * (up to word bdries)
1743 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), (unsigned)n
->m_len
);
1747 kprintf(">%x: %x, %x, %x\n", n
, n
->m_flags
, n
->m_len
,
1762 m_mclref(struct mbuf
*p
)
1764 return (_MCLREF(p
));
1768 m_mclunref(struct mbuf
*p
)
1770 return (_MCLUNREF(p
));
1773 /* change mbuf to new type */
1775 m_mchtype(struct mbuf
*m
, int t
)
1778 mbstat
.m_mtypes
[(m
)->m_type
]--;
1779 mbstat
.m_mtypes
[t
]++;
1784 void *m_mtod(struct mbuf
*m
)
1786 return ((m
)->m_data
);
1789 struct mbuf
*m_dtom(void *x
)
1791 return ((struct mbuf
*)((u_long
)(x
) & ~(MSIZE
-1)));
1794 int m_mtocl(void *x
)
1796 return (((char *)(x
) - (char *)mbutl
) / sizeof(union mcluster
));
1799 union mcluster
*m_cltom(int x
)
1801 return ((union mcluster
*)(mbutl
+ (x
)));
1805 void m_mcheck(struct mbuf
*m
)
1807 if (m
->m_type
!= MT_FREE
)
1808 panic("mget MCHECK: m_type=%x m=%x", m
->m_type
, m
);
1812 mbuf_expand_thread(void)
1817 expand_mcl
= mbuf_expand_mcl
;
1818 mbuf_expand_mcl
= 0;
1822 MCLALLOC(p
, M_WAIT
);
1825 assert_wait(&mbuf_expand_thread_wakeup
, THREAD_UNINT
);
1826 (void) thread_block(mbuf_expand_thread
);
1831 mbuf_expand_thread_init(void)
1833 mbuf_expand_thread_initialized
++;
1834 mbuf_expand_thread();