2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1988, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
59 * 10/15/97 Annette DeSchon (deschon@apple.com)
60 * Fixed bug in which all cluster mbufs were broken up
61 * into regular mbufs: Some clusters are now reserved.
62 * When a cluster is needed, regular mbufs are no longer
63 * used. (Radar 1683621)
64 * 20-May-95 Mac Gillon (mgillon) at NeXT
65 * New version based on 4.4
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/malloc.h>
72 #include <sys/kernel.h>
73 #include <sys/syslog.h>
74 #include <sys/protosw.h>
75 #include <sys/domain.h>
76 #include <net/netisr.h>
78 #include <kern/queue.h>
79 #include <kern/kern_types.h>
80 #include <kern/sched_prim.h>
82 #define _MCLREF(p) (++mclrefcnt[mtocl(p)])
83 #define _MCLUNREF(p) (--mclrefcnt[mtocl(p)] == 0)
85 extern kernel_pmap
; /* The kernel's pmap */
87 decl_simple_lock_data(, mbuf_slock
);
88 struct mbuf
*mfree
; /* mbuf free list */
89 struct mbuf
*mfreelater
; /* mbuf deallocation list */
90 extern vm_map_t mb_map
; /* special map */
91 int m_want
; /* sleepers on mbufs */
92 extern int nmbclusters
; /* max number of mapped clusters */
93 short *mclrefcnt
; /* mapped cluster reference counts */
95 union mcluster
*mclfree
; /* mapped cluster free list */
96 int max_linkhdr
; /* largest link-level header */
97 int max_protohdr
; /* largest protocol header */
98 int max_hdr
; /* largest link+protocol header */
99 int max_datalen
; /* MHLEN - max_hdr */
100 struct mbstat mbstat
; /* statistics */
101 union mcluster
*mbutl
; /* first mapped cluster address */
102 union mcluster
*embutl
; /* ending virtual address of mclusters */
104 static int nclpp
; /* # clusters per physical page */
105 static char mbfail
[] = "mbuf not mapped";
107 static int m_howmany();
109 /* The number of cluster mbufs that are allocated, to start. */
110 #define MINCL max(16, 2)
112 extern int dlil_input_thread_wakeup
;
113 extern int dlil_expand_mcl
;
114 extern int dlil_initialized
;
117 static int mfree_munge
= 0;
119 #define _MFREE_MUNGE(m) { \
122 vm_offset_t *element = (vm_offset_t *)(m); \
124 i < sizeof(struct mbuf)/sizeof(vm_offset_t); \
126 (element)[i] = 0xdeadbeef; \
131 munge_mbuf(struct mbuf
*m
)
134 vm_offset_t
*element
= (vm_offset_t
*)(m
);
136 i
< sizeof(struct mbuf
)/sizeof(vm_offset_t
);
138 (element
)[i
] = 0xdeadbeef;
140 #define _MFREE_MUNGE(m) { \
146 #define _MFREE_MUNGE(m)
150 #define _MINTGET(m, type) { \
152 if (((m) = mfree) != 0) { \
154 ++mclrefcnt[mtocl(m)]; \
155 mbstat.m_mtypes[MT_FREE]--; \
156 mbstat.m_mtypes[(type)]++; \
157 mfree = (m)->m_next; \
171 nclpp
= round_page(MCLBYTES
) / MCLBYTES
; /* see mbufgc() */
172 if (nclpp
< 1) nclpp
= 1;
174 // NETISR_LOCKINIT();
176 mbstat
.m_msize
= MSIZE
;
177 mbstat
.m_mclbytes
= MCLBYTES
;
178 mbstat
.m_minclsize
= MINCLSIZE
;
179 mbstat
.m_mlen
= MLEN
;
180 mbstat
.m_mhlen
= MHLEN
;
182 if (nmbclusters
== 0)
183 nmbclusters
= NMBCLUSTERS
;
184 MALLOC(mclrefcnt
, short *, nmbclusters
* sizeof (short),
188 for (m
= 0; m
< nmbclusters
; m
++)
191 MALLOC(mcl_paddr
, int *, (nmbclusters
/(PAGE_SIZE
/CLBYTES
)) * sizeof (int),
195 bzero((char *)mcl_paddr
, (nmbclusters
/(PAGE_SIZE
/CLBYTES
)) * sizeof (int));
197 embutl
= (union mcluster
*)((unsigned char *)mbutl
+ (nmbclusters
* MCLBYTES
));
199 PE_parse_boot_arg("initmcl", &initmcl
);
201 if (m_clalloc(max(PAGE_SIZE
/CLBYTES
, 1) * initmcl
, M_WAIT
) == 0)
210 * Allocate some number of mbuf clusters
211 * and place on cluster free list.
214 m_clalloc(ncl
, nowait
)
218 register union mcluster
*mcl
;
221 static char doing_alloc
;
224 * Honor the caller's wish to block or not block.
225 * We have a way to grow the pool asynchronously,
226 * by kicking the dlil_input_thread.
228 if ((i
= m_howmany()) <= 0)
231 if ((nowait
== M_DONTWAIT
))
236 size
= round_page(ncl
* MCLBYTES
);
237 mcl
= (union mcluster
*)kmem_mb_alloc(mb_map
, size
);
239 if (mcl
== 0 && ncl
> 1) {
240 size
= round_page(MCLBYTES
); /* Try for 1 if failed */
241 mcl
= (union mcluster
*)kmem_mb_alloc(mb_map
, size
);
246 ncl
= size
/ MCLBYTES
;
247 for (i
= 0; i
< ncl
; i
++) {
248 if (++mclrefcnt
[mtocl(mcl
)] != 0)
249 panic("m_clalloc already there");
250 if (((int)mcl
& PAGE_MASK
) == 0)
251 mcl_paddr
[((char *)mcl
- (char *)mbutl
)/PAGE_SIZE
] = pmap_extract(kernel_pmap
, (char *)mcl
);
253 mcl
->mcl_next
= mclfree
;
256 mbstat
.m_clfree
+= ncl
;
257 mbstat
.m_clusters
+= ncl
;
264 * When non-blocking we kick the dlil thread if we havve to grow the
265 * pool or if the number of free clusters is less than requested.
267 if ((nowait
== M_DONTWAIT
) && (i
> 0 || ncl
>= mbstat
.m_clfree
)) {
269 if (dlil_initialized
)
270 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
273 if (mbstat
.m_clfree
>= ncl
)
282 * Add more free mbufs by cutting up a cluster.
287 register caddr_t mcl
;
289 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4))
290 /* 1/16th of the total number of cluster mbufs allocated is
291 reserved for large packets. The number reserved must
292 always be < 1/2, or future allocation will be prevented.
296 MCLALLOC(mcl
, canwait
);
298 register struct mbuf
*m
= (struct mbuf
*)mcl
;
299 register int i
= NMBPCL
;
301 mbstat
.m_mtypes
[MT_FREE
] += i
;
312 if (i
) wakeup((caddr_t
)&mfree
);
319 * When MGET failes, ask protocols to free space when short of memory,
320 * then re-attempt to allocate an mbuf.
323 m_retry(canwait
, type
)
326 register struct mbuf
*m
;
330 boolean_t funnel_state
;
333 (void) m_expand(canwait
);
336 (m
)->m_next
= (m
)->m_nextpkt
= 0;
337 (m
)->m_type
= (type
);
338 (m
)->m_data
= (m
)->m_dat
;
341 if (m
|| canwait
== M_DONTWAIT
)
352 if (dlil_initialized
)
353 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
356 * Grab network funnel because m_reclaim calls into the
357 * socket domains and tsleep end-up calling splhigh
359 fnl
= thread_funnel_get();
360 if (fnl
&& (fnl
== kernel_flock
)) {
362 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
364 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
368 /* Sleep with a small timeout as insurance */
369 (void) tsleep((caddr_t
)&mfree
, PZERO
-1, "m_retry", hz
);
372 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
374 thread_funnel_set(network_flock
, funnel_state
);
380 * As above; retry an MGETHDR.
383 m_retryhdr(canwait
, type
)
386 register struct mbuf
*m
;
388 if (m
= m_retry(canwait
, type
)) {
389 m
->m_flags
|= M_PKTHDR
;
390 m
->m_data
= m
->m_pktdat
;
391 m
->m_pkthdr
.rcvif
= NULL
;
393 m
->m_pkthdr
.header
= NULL
;
394 m
->m_pkthdr
.csum_flags
= 0;
395 m
->m_pkthdr
.csum_data
= 0;
396 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
397 m
->m_pkthdr
.reserved1
= NULL
;
398 m
->m_pkthdr
.reserved2
= NULL
;
405 register struct domain
*dp
;
406 register struct protosw
*pr
;
408 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
409 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
)
416 * Space allocation routines.
417 * These are also available as macros
418 * for critical paths.
424 register struct mbuf
*m
;
428 m
->m_next
= m
->m_nextpkt
= 0;
430 m
->m_data
= m
->m_dat
;
433 (m
) = m_retry(nowait
, type
);
439 m_gethdr(nowait
, type
)
442 register struct mbuf
*m
;
446 m
->m_next
= m
->m_nextpkt
= 0;
448 m
->m_data
= m
->m_pktdat
;
449 m
->m_flags
= M_PKTHDR
;
450 m
->m_pkthdr
.rcvif
= NULL
;
451 m
->m_pkthdr
.header
= NULL
;
452 m
->m_pkthdr
.csum_flags
= 0;
453 m
->m_pkthdr
.csum_data
= 0;
454 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
455 m
->m_pkthdr
.reserved1
= NULL
;
456 m
->m_pkthdr
.reserved2
= NULL
;
458 m
= m_retryhdr(nowait
, type
);
464 m_getclr(nowait
, type
)
467 register struct mbuf
*m
;
469 MGET(m
, nowait
, type
);
472 bzero(mtod(m
, caddr_t
), MLEN
);
480 struct mbuf
*n
= m
->m_next
;
483 if (m
->m_type
== MT_FREE
)
484 panic("freeing free mbuf");
486 /* Free the aux data if there is any */
487 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
)
489 m_freem(m
->m_pkthdr
.aux
);
493 if ((m
->m_flags
& M_EXT
))
495 if (MCLHASREFERENCE(m
)) {
496 remque((queue_t
)&m
->m_ext
.ext_refs
);
497 } else if (m
->m_ext
.ext_free
== NULL
) {
498 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
499 if (_MCLUNREF(mcl
)) {
500 mcl
->mcl_next
= mclfree
;
505 /* *** Since m_split() increments "mclrefcnt[mtocl(m->m_ext.ext_buf)]",
506 and AppleTalk ADSP uses m_split(), this incorrect sanity check
509 else /* sanity check - not referenced this way */
510 panic("m_free m_ext cluster not free");
513 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
514 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
517 mbstat
.m_mtypes
[m
->m_type
]--;
521 mbstat
.m_mtypes
[m
->m_type
]++;
529 if (i
) wakeup((caddr_t
)&mfree
);
533 /* m_mclget() add an mbuf cluster to a normal mbuf */
539 MCLALLOC(m
->m_ext
.ext_buf
, nowait
);
540 if (m
->m_ext
.ext_buf
) {
541 m
->m_data
= m
->m_ext
.ext_buf
;
543 m
->m_ext
.ext_size
= MCLBYTES
;
544 m
->m_ext
.ext_free
= 0;
545 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
552 /* m_mclalloc() allocate an mbuf cluster */
559 (void)m_clalloc(1, nowait
);
560 if ((p
= (caddr_t
)mclfree
)) {
561 ++mclrefcnt
[mtocl(p
)];
563 mclfree
= ((union mcluster
*)p
)->mcl_next
;
570 /* m_mclfree() releases a reference to a cluster allocated by MCLALLOC,
571 * freeing the cluster if the reference count has reached 0. */
577 if (--mclrefcnt
[mtocl(p
)] == 0) {
578 ((union mcluster
*)(p
))->mcl_next
= mclfree
;
579 mclfree
= (union mcluster
*)(p
);
585 /* mcl_hasreference() checks if a cluster of an mbuf is referenced by another mbuf */
590 return (m
->m_ext
.ext_refs
.forward
!= &(m
->m_ext
.ext_refs
));
595 m_copy_pkthdr(to
, from
)
596 struct mbuf
*to
, *from
;
598 to
->m_pkthdr
= from
->m_pkthdr
;
599 from
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
600 to
->m_flags
= from
->m_flags
& M_COPYFLAGS
;
601 to
->m_data
= (to
)->m_pktdat
;
604 /* Best effort to get a mbuf cluster + pkthdr under one lock.
605 * If we don't have them avail, just bail out and use the regular
607 * Used by drivers to allocated packets on receive ring.
613 m_clalloc(1, M_DONTWAIT
); /* takes the MBUF_LOCK, but doesn't release it... */
614 if ((mfree
!= 0) && (mclfree
!= 0)) { /* mbuf + cluster are available */
618 ++mclrefcnt
[mtocl(m
)];
619 mbstat
.m_mtypes
[MT_FREE
]--;
620 mbstat
.m_mtypes
[MT_DATA
]++;
621 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
622 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
624 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
626 m
->m_next
= m
->m_nextpkt
= 0;
628 m
->m_data
= m
->m_ext
.ext_buf
;
629 m
->m_flags
= M_PKTHDR
| M_EXT
;
631 m
->m_pkthdr
.rcvif
= NULL
;
632 m
->m_pkthdr
.header
= NULL
;
633 m
->m_pkthdr
.csum_data
= 0;
634 m
->m_pkthdr
.csum_flags
= 0;
635 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
636 m
->m_pkthdr
.reserved1
= 0;
637 m
->m_pkthdr
.reserved2
= 0;
638 m
->m_ext
.ext_free
= 0;
639 m
->m_ext
.ext_size
= MCLBYTES
;
640 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
644 else { /* slow path: either mbuf or cluster need to be allocated anyway */
647 MGETHDR(m
, M_WAITOK
, MT_DATA
);
652 MCLGET( m
, M_WAITOK
);
653 if ( ( m
->m_flags
& M_EXT
) == 0 )
663 * return a list of mbuf hdrs that point to clusters...
664 * try for num_needed, if this can't be met, return whatever
665 * number were available... set up the first num_with_pkthdrs
666 * with mbuf hdrs configured as packet headers... these are
667 * chained on the m_nextpkt field... any packets requested beyond
668 * this are chained onto the last packet header's m_next field.
671 m_getpackets(int num_needed
, int num_with_pkthdrs
, int how
)
674 struct mbuf
**np
, *top
;
679 m_clalloc(num_needed
, how
); /* takes the MBUF_LOCK, but doesn't release it... */
681 while (num_needed
--) {
682 if (mfree
&& mclfree
) { /* mbuf + cluster are available */
686 ++mclrefcnt
[mtocl(m
)];
687 mbstat
.m_mtypes
[MT_FREE
]--;
688 mbstat
.m_mtypes
[MT_DATA
]++;
689 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
690 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
692 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
694 m
->m_next
= m
->m_nextpkt
= 0;
696 m
->m_data
= m
->m_ext
.ext_buf
;
697 m
->m_ext
.ext_free
= 0;
698 m
->m_ext
.ext_size
= MCLBYTES
;
699 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
701 if (num_with_pkthdrs
== 0)
704 m
->m_flags
= M_PKTHDR
| M_EXT
;
706 m
->m_pkthdr
.rcvif
= NULL
;
707 m
->m_pkthdr
.header
= NULL
;
708 m
->m_pkthdr
.csum_flags
= 0;
709 m
->m_pkthdr
.csum_data
= 0;
710 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
711 m
->m_pkthdr
.reserved1
= NULL
;
712 m
->m_pkthdr
.reserved2
= NULL
;
721 if (num_with_pkthdrs
== 0) {
722 MGET(m
, how
, MT_DATA
);
724 MGETHDR(m
, how
, MT_DATA
);
732 if ((m
->m_flags
& M_EXT
) == 0) {
740 if (num_with_pkthdrs
)
752 * return a list of mbuf hdrs set up as packet hdrs
753 * chained together on the m_nextpkt field
756 m_getpackethdrs(int num_needed
, int how
)
759 struct mbuf
**np
, *top
;
766 while (num_needed
--) {
767 if (m
= mfree
) { /* mbufs are available */
770 ++mclrefcnt
[mtocl(m
)];
771 mbstat
.m_mtypes
[MT_FREE
]--;
772 mbstat
.m_mtypes
[MT_DATA
]++;
774 m
->m_next
= m
->m_nextpkt
= 0;
776 m
->m_flags
= M_PKTHDR
;
777 m
->m_data
= m
->m_pktdat
;
779 m
->m_pkthdr
.rcvif
= NULL
;
780 m
->m_pkthdr
.header
= NULL
;
781 m
->m_pkthdr
.csum_flags
= 0;
782 m
->m_pkthdr
.csum_data
= 0;
783 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
784 m
->m_pkthdr
.reserved1
= NULL
;
785 m
->m_pkthdr
.reserved2
= NULL
;
791 m
= m_retryhdr(how
, MT_DATA
);
807 /* free and mbuf list (m_nextpkt) while following m_next under one lock.
808 * returns the count for mbufs packets freed. Used by the drivers.
814 struct mbuf
*nextpkt
;
821 nextpkt
= m
->m_nextpkt
; /* chain of linked mbufs from driver */
827 while (m
) { /* free the mbuf chain (like mfreem) */
831 /* Free the aux data if there is any */
832 if ((m
->m_flags
& M_PKTHDR
) && m
->m_pkthdr
.aux
) {
834 * Treat the current m as the nextpkt and set m
835 * to the aux data. This lets us free the aux
836 * data in this loop without having to call
837 * m_freem recursively, which wouldn't work
838 * because we've still got the lock.
841 m
= nextpkt
->m_pkthdr
.aux
;
842 nextpkt
->m_pkthdr
.aux
= NULL
;
847 if (n
&& n
->m_nextpkt
)
848 panic("m_freem_list: m_nextpkt of m_next != NULL");
849 if (m
->m_type
== MT_FREE
)
850 panic("freeing free mbuf");
852 if (m
->m_flags
& M_EXT
) {
853 if (MCLHASREFERENCE(m
)) {
854 remque((queue_t
)&m
->m_ext
.ext_refs
);
855 } else if (m
->m_ext
.ext_free
== NULL
) {
856 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
857 if (_MCLUNREF(mcl
)) {
858 mcl
->mcl_next
= mclfree
;
863 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
864 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
867 mbstat
.m_mtypes
[m
->m_type
]--;
870 mbstat
.m_mtypes
[MT_FREE
]++;
878 m
= nextpkt
; /* bump m with saved nextpkt if any */
886 wakeup((caddr_t
)&mfree
);
893 register struct mbuf
*m
;
900 * Mbuffer utility routines.
903 * Compute the amount of space available
904 * before the current start of data in an mbuf.
907 register struct mbuf
*m
;
909 if (m
->m_flags
& M_EXT
) {
910 if (MCLHASREFERENCE(m
))
912 return (m
->m_data
- m
->m_ext
.ext_buf
);
914 if (m
->m_flags
& M_PKTHDR
)
915 return (m
->m_data
- m
->m_pktdat
);
916 return (m
->m_data
- m
->m_dat
);
920 * Compute the amount of space available
921 * after the end of data in an mbuf.
924 register struct mbuf
*m
;
926 if (m
->m_flags
& M_EXT
) {
927 if (MCLHASREFERENCE(m
))
929 return (m
->m_ext
.ext_buf
+ m
->m_ext
.ext_size
-
930 (m
->m_data
+ m
->m_len
));
932 return (&m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
));
936 * Lesser-used path for M_PREPEND:
937 * allocate new mbuf to prepend to chain,
939 * Does not adjust packet header length.
942 m_prepend(m
, len
, how
)
943 register struct mbuf
*m
;
948 MGET(mn
, how
, m
->m_type
);
949 if (mn
== (struct mbuf
*)NULL
) {
951 return ((struct mbuf
*)NULL
);
953 if (m
->m_flags
& M_PKTHDR
) {
954 M_COPY_PKTHDR(mn
, m
);
955 m
->m_flags
&= ~M_PKTHDR
;
966 * Replacement for old M_PREPEND macro:
967 * allocate new mbuf to prepend to chain,
968 * copy junk along, and adjust length.
972 m_prepend_2(m
, len
, how
)
973 register struct mbuf
*m
;
976 if (M_LEADINGSPACE(m
) >= len
) {
980 m
= m_prepend(m
, len
, how
);
982 if ((m
) && (m
->m_flags
& M_PKTHDR
))
983 m
->m_pkthdr
.len
+= len
;
988 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
989 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
990 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
995 m_copym(m
, off0
, len
, wait
)
996 register struct mbuf
*m
;
1000 register struct mbuf
*n
, **np
;
1001 register int off
= off0
;
1005 if (off
< 0 || len
< 0)
1007 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1010 while (off
>= m
->m_len
) {
1023 if (len
!= M_COPYALL
)
1029 ++mclrefcnt
[mtocl(n
)];
1030 mbstat
.m_mtypes
[MT_FREE
]--;
1031 mbstat
.m_mtypes
[m
->m_type
]++;
1033 n
->m_next
= n
->m_nextpkt
= 0;
1034 n
->m_type
= m
->m_type
;
1035 n
->m_data
= n
->m_dat
;
1039 n
= m_retry(wait
, m
->m_type
);
1047 M_COPY_PKTHDR(n
, m
);
1048 if (len
== M_COPYALL
)
1049 n
->m_pkthdr
.len
-= off0
;
1051 n
->m_pkthdr
.len
= len
;
1054 if (len
== M_COPYALL
) {
1055 if (min(len
, (m
->m_len
- off
)) == len
) {
1056 printf("m->m_len %d - off %d = %d, %d\n",
1057 m
->m_len
, off
, m
->m_len
- off
,
1058 min(len
, (m
->m_len
- off
)));
1061 n
->m_len
= min(len
, (m
->m_len
- off
));
1062 if (n
->m_len
== M_COPYALL
) {
1063 printf("n->m_len == M_COPYALL, fixing\n");
1066 if (m
->m_flags
& M_EXT
) {
1067 n
->m_ext
= m
->m_ext
;
1068 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1069 n
->m_data
= m
->m_data
+ off
;
1070 n
->m_flags
|= M_EXT
;
1072 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
1073 (unsigned)n
->m_len
);
1075 if (len
!= M_COPYALL
)
1097 * equivilent to m_copym except that all necessary
1098 * mbuf hdrs are allocated within this routine
1099 * also, the last mbuf and offset accessed are passed
1100 * out and can be passed back in to avoid having to
1101 * rescan the entire mbuf list (normally hung off of the socket)
1104 m_copym_with_hdrs(m
, off0
, len
, wait
, m_last
, m_off
)
1105 register struct mbuf
*m
;
1108 struct mbuf
**m_last
;
1111 register struct mbuf
*n
, **np
;
1112 register int off
= off0
;
1113 struct mbuf
*top
= 0;
1117 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
1124 while (off
>= m
->m_len
) {
1136 panic("m_gethdr_and_copym");
1141 ++mclrefcnt
[mtocl(n
)];
1142 mbstat
.m_mtypes
[MT_FREE
]--;
1143 mbstat
.m_mtypes
[type
]++;
1145 n
->m_next
= n
->m_nextpkt
= 0;
1149 n
->m_data
= n
->m_dat
;
1152 n
->m_data
= n
->m_pktdat
;
1153 n
->m_flags
= M_PKTHDR
;
1154 n
->m_pkthdr
.len
= 0;
1155 n
->m_pkthdr
.rcvif
= NULL
;
1156 n
->m_pkthdr
.header
= NULL
;
1157 n
->m_pkthdr
.csum_flags
= 0;
1158 n
->m_pkthdr
.csum_data
= 0;
1159 n
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
1160 n
->m_pkthdr
.reserved1
= NULL
;
1161 n
->m_pkthdr
.reserved2
= NULL
;
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
.reserved1
= 0;
1698 n
->m_pkthdr
.reserved2
= 0;
1699 bcopy(m
->m_data
, n
->m_data
, m
->m_pkthdr
.len
);
1702 } else if (m
->m_len
<= MLEN
)
1703 { if ((n
= m_get(how
, m
->m_type
)) == NULL
)
1705 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
1706 n
->m_len
= m
->m_len
;
1713 kprintf("<%x: %x, %x, %x\n", m
, m
->m_flags
, m
->m_len
,
1717 n
= m_gethdr(how
, m
->m_type
);
1719 n
= m_get(how
, m
->m_type
);
1722 if (m
->m_flags
& M_EXT
)
1724 if ((n
->m_flags
& M_EXT
) == 0)
1729 { /* Don't use M_COPY_PKTHDR: preserve m_data */
1730 n
->m_pkthdr
= m
->m_pkthdr
;
1731 n
->m_flags
|= (m
->m_flags
& M_COPYFLAGS
);
1733 if ((n
->m_flags
& M_EXT
) == 0)
1734 n
->m_data
= n
->m_pktdat
;
1736 n
->m_len
= m
->m_len
;
1738 * Get the dup on the same bdry as the original
1739 * Assume that the two mbufs have the same offset to data area
1740 * (up to word bdries)
1742 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), (unsigned)n
->m_len
);
1746 kprintf(">%x: %x, %x, %x\n", n
, n
->m_flags
, n
->m_len
,
1761 m_mclref(struct mbuf
*p
)
1763 return (_MCLREF(p
));
1767 m_mclunref(struct mbuf
*p
)
1769 return (_MCLUNREF(p
));
1772 /* change mbuf to new type */
1774 m_mchtype(struct mbuf
*m
, int t
)
1777 mbstat
.m_mtypes
[(m
)->m_type
]--;
1778 mbstat
.m_mtypes
[t
]++;
1783 void *m_mtod(struct mbuf
*m
)
1785 return ((m
)->m_data
);
1788 struct mbuf
*m_dtom(void *x
)
1790 return ((struct mbuf
*)((u_long
)(x
) & ~(MSIZE
-1)));
1793 int m_mtocl(void *x
)
1795 return (((char *)(x
) - (char *)mbutl
) / sizeof(union mcluster
));
1798 union mcluster
*m_cltom(int x
)
1800 return ((union mcluster
*)(mbutl
+ (x
)));
1804 void m_mcheck(struct mbuf
*m
)
1806 if (m
->m_type
!= MT_FREE
)
1807 panic("mget MCHECK: m_type=%x m=%x", m
->m_type
, m
);
1811 #include <sys/sysctl.h>
1813 static int mhog_num
= 0;
1814 static struct mbuf
*mhog_chain
= 0;
1815 static int mhog_wait
= 1;
1818 sysctl_mhog_num SYSCTL_HANDLER_ARGS
1823 error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
1824 if (!error
&& req
->newptr
) {
1829 m_freem(mhog_chain
);
1833 for (i
= 0; i
< mhog_num
; i
++) {
1834 MGETHDR(m
, mhog_wait
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
1838 MCLGET(m
, mhog_wait
? M_WAIT
: M_DONTWAIT
);
1839 if ((m
->m_flags
& M_EXT
) == 0) {
1844 m
->m_next
= mhog_chain
;
1853 SYSCTL_NODE(_kern_ipc
, OID_AUTO
, mhog
, CTLFLAG_RW
, 0, "mbuf hog");
1855 SYSCTL_PROC(_kern_ipc_mhog
, OID_AUTO
, cluster
, CTLTYPE_INT
|CTLFLAG_RW
,
1856 &mhog_num
, 0, &sysctl_mhog_num
, "I", "");
1857 SYSCTL_INT(_kern_ipc_mhog
, OID_AUTO
, wait
, CTLFLAG_RW
, &mhog_wait
,