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>
80 extern kernel_pmap
; /* The kernel's pmap */
82 decl_simple_lock_data(, mbuf_slock
);
83 struct mbuf
*mfree
; /* mbuf free list */
84 struct mbuf
*mfreelater
; /* mbuf deallocation list */
85 extern vm_map_t mb_map
; /* special map */
86 int m_want
; /* sleepers on mbufs */
87 extern int nmbclusters
; /* max number of mapped clusters */
88 short *mclrefcnt
; /* mapped cluster reference counts */
90 union mcluster
*mclfree
; /* mapped cluster free list */
91 int max_linkhdr
; /* largest link-level header */
92 int max_protohdr
; /* largest protocol header */
93 int max_hdr
; /* largest link+protocol header */
94 int max_datalen
; /* MHLEN - max_hdr */
95 struct mbstat mbstat
; /* statistics */
96 union mcluster
*mbutl
; /* first mapped cluster address */
97 union mcluster
*embutl
; /* ending virtual address of mclusters */
99 static int nclpp
; /* # clusters per physical page */
100 static char mbfail
[] = "mbuf not mapped";
102 static int m_howmany();
104 /* The number of cluster mbufs that are allocated, to start. */
105 #define MINCL max(16, 2)
107 extern int dlil_input_thread_wakeup
;
108 extern int dlil_expand_mcl
;
109 extern int dlil_initialized
;
120 nclpp
= round_page(MCLBYTES
) / MCLBYTES
; /* see mbufgc() */
121 if (nclpp
< 1) nclpp
= 1;
123 // NETISR_LOCKINIT();
125 mbstat
.m_msize
= MSIZE
;
126 mbstat
.m_mclbytes
= MCLBYTES
;
127 mbstat
.m_minclsize
= MINCLSIZE
;
128 mbstat
.m_mlen
= MLEN
;
129 mbstat
.m_mhlen
= MHLEN
;
131 if (nmbclusters
== 0)
132 nmbclusters
= NMBCLUSTERS
;
133 MALLOC(mclrefcnt
, short *, nmbclusters
* sizeof (short),
137 for (m
= 0; m
< nmbclusters
; m
++)
140 MALLOC(mcl_paddr
, int *, (nmbclusters
/(PAGE_SIZE
/CLBYTES
)) * sizeof (int),
144 bzero((char *)mcl_paddr
, (nmbclusters
/(PAGE_SIZE
/CLBYTES
)) * sizeof (int));
146 embutl
= (union mcluster
*)((unsigned char *)mbutl
+ (nmbclusters
* MCLBYTES
));
148 PE_parse_boot_arg("initmcl", &initmcl
);
150 if (m_clalloc(max(PAGE_SIZE
/CLBYTES
, 1) * initmcl
, M_WAIT
) == 0)
159 * Allocate some number of mbuf clusters
160 * and place on cluster free list.
163 m_clalloc(ncl
, nowait
)
167 register union mcluster
*mcl
;
170 static char doing_alloc
;
173 * Honor the caller's wish to block or not block.
174 * We have a way to grow the pool asynchronously,
175 * by kicking the dlil_input_thread.
177 if ((i
= m_howmany()) <= 0)
180 if ((nowait
== M_DONTWAIT
))
185 size
= round_page(ncl
* MCLBYTES
);
186 mcl
= (union mcluster
*)kmem_mb_alloc(mb_map
, size
);
188 if (mcl
== 0 && ncl
> 1) {
189 size
= round_page(MCLBYTES
); /* Try for 1 if failed */
190 mcl
= (union mcluster
*)kmem_mb_alloc(mb_map
, size
);
195 ncl
= size
/ MCLBYTES
;
196 for (i
= 0; i
< ncl
; i
++) {
197 if (++mclrefcnt
[mtocl(mcl
)] != 0)
198 panic("m_clalloc already there");
199 if (((int)mcl
& PAGE_MASK
) == 0)
200 mcl_paddr
[((char *)mcl
- (char *)mbutl
)/PAGE_SIZE
] = pmap_extract(kernel_pmap
, (char *)mcl
);
202 mcl
->mcl_next
= mclfree
;
205 mbstat
.m_clfree
+= ncl
;
206 mbstat
.m_clusters
+= ncl
;
213 * When non-blocking we kick the dlil thread if we havve to grow the
214 * pool or if the number of free clusters is less than requested.
216 if ((nowait
== M_DONTWAIT
) && (i
> 0 || ncl
>= mbstat
.m_clfree
)) {
218 if (dlil_initialized
)
219 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
222 if (mbstat
.m_clfree
>= ncl
)
231 * Add more free mbufs by cutting up a cluster.
236 register caddr_t mcl
;
238 if (mbstat
.m_clfree
< (mbstat
.m_clusters
>> 4))
239 /* 1/16th of the total number of cluster mbufs allocated is
240 reserved for large packets. The number reserved must
241 always be < 1/2, or future allocation will be prevented.
245 MCLALLOC(mcl
, canwait
);
247 register struct mbuf
*m
= (struct mbuf
*)mcl
;
248 register int i
= NMBPCL
;
250 mbstat
.m_mtypes
[MT_FREE
] += i
;
260 if (i
) wakeup((caddr_t
)&mfree
);
267 * When MGET failes, ask protocols to free space when short of memory,
268 * then re-attempt to allocate an mbuf.
271 m_retry(canwait
, type
)
274 #define m_retry(h, t) 0
275 register struct mbuf
*m
;
279 boolean_t funnel_state
;
282 (void) m_expand(canwait
);
284 if (m
|| canwait
== M_DONTWAIT
)
292 if (dlil_initialized
)
293 wakeup((caddr_t
)&dlil_input_thread_wakeup
);
299 assert_wait((caddr_t
)&mfree
, THREAD_UNINT
);
304 * Grab network funnel because m_reclaim calls into the
305 * socket domains and tsleep end-up calling splhigh
307 fnl
= thread_funnel_get();
308 if (fnl
&& (fnl
== kernel_flock
)) {
310 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
312 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
316 /* Sleep with a small timeout as insurance */
317 (void) tsleep((caddr_t
)0, PZERO
-1, "m_retry", hz
);
320 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
322 thread_funnel_set(network_flock
, funnel_state
);
329 * As above; retry an MGETHDR.
332 m_retryhdr(canwait
, type
)
335 register struct mbuf
*m
;
337 if (m
= m_retry(canwait
, type
)) {
338 m
->m_flags
|= M_PKTHDR
;
339 m
->m_data
= m
->m_pktdat
;
340 m
->m_pkthdr
.rcvif
= NULL
;
342 m
->m_pkthdr
.header
= NULL
;
343 m
->m_pkthdr
.csum_flags
= 0;
344 m
->m_pkthdr
.csum_data
= 0;
345 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
346 m
->m_pkthdr
.reserved1
= NULL
;
347 m
->m_pkthdr
.reserved2
= NULL
;
354 register struct domain
*dp
;
355 register struct protosw
*pr
;
357 for (dp
= domains
; dp
; dp
= dp
->dom_next
)
358 for (pr
= dp
->dom_protosw
; pr
; pr
= pr
->pr_next
)
365 * Space allocation routines.
366 * These are also available as macros
367 * for critical paths.
373 register struct mbuf
*m
;
375 MGET(m
, nowait
, type
);
380 m_gethdr(nowait
, type
)
383 register struct mbuf
*m
;
385 MGETHDR(m
, nowait
, type
);
390 m_getclr(nowait
, type
)
393 register struct mbuf
*m
;
395 MGET(m
, nowait
, type
);
398 bzero(mtod(m
, caddr_t
), MLEN
);
406 struct mbuf
*n
= m
->m_next
;
409 if (m
->m_type
== MT_FREE
)
410 panic("freeing free mbuf");
413 if (m
->m_flags
& M_EXT
) {
414 if (MCLHASREFERENCE(m
)) {
415 remque((queue_t
)&m
->m_ext
.ext_refs
);
416 } else if (m
->m_ext
.ext_free
== NULL
) {
417 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
419 mcl
->mcl_next
= mclfree
;
424 /* *** Since m_split() increments "mclrefcnt[mtocl(m->m_ext.ext_buf)]",
425 and AppleTalk ADSP uses m_split(), this incorrect sanity check
428 else /* sanity check - not referenced this way */
429 panic("m_free m_ext cluster not free");
432 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
433 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
436 mbstat
.m_mtypes
[m
->m_type
]--;
439 mbstat
.m_mtypes
[m
->m_type
]++;
447 if (i
) wakeup((caddr_t
)&mfree
);
451 /* Best effort to get a mbuf cluster + pkthdr under one lock.
452 * If we don't have them avail, just bail out and use the regular
454 * Used by drivers to allocated packets on receive ring.
460 m_clalloc(1, M_DONTWAIT
); /* takes the MBUF_LOCK, but doesn't release it... */
461 if ((mfree
!= 0) && (mclfree
!= 0)) { /* mbuf + cluster are available */
465 ++mclrefcnt
[mtocl(m
)];
466 mbstat
.m_mtypes
[MT_FREE
]--;
467 mbstat
.m_mtypes
[MT_DATA
]++;
468 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
469 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
471 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
473 m
->m_next
= m
->m_nextpkt
= 0;
475 m
->m_data
= m
->m_ext
.ext_buf
;
476 m
->m_flags
= M_PKTHDR
| M_EXT
;
478 m
->m_pkthdr
.rcvif
= NULL
;
479 m
->m_pkthdr
.header
= NULL
;
480 m
->m_pkthdr
.csum_data
= 0;
481 m
->m_pkthdr
.csum_flags
= 0;
482 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
483 m
->m_pkthdr
.reserved1
= 0;
484 m
->m_pkthdr
.reserved2
= 0;
485 m
->m_ext
.ext_free
= 0;
486 m
->m_ext
.ext_size
= MCLBYTES
;
487 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
=
491 else { /* slow path: either mbuf or cluster need to be allocated anyway */
494 MGETHDR(m
, M_WAITOK
, MT_DATA
);
499 MCLGET( m
, M_WAITOK
);
500 if ( ( m
->m_flags
& M_EXT
) == 0 )
510 m_getpackets(int num_needed
, int num_with_pkthdrs
, int how
)
513 struct mbuf
**np
, *top
;
518 m_clalloc(num_needed
, how
); /* takes the MBUF_LOCK, but doesn't release it... */
520 while (num_needed
--) {
521 if (mfree
&& mclfree
) { /* mbuf + cluster are available */
525 ++mclrefcnt
[mtocl(m
)];
526 mbstat
.m_mtypes
[MT_FREE
]--;
527 mbstat
.m_mtypes
[MT_DATA
]++;
528 m
->m_ext
.ext_buf
= (caddr_t
)mclfree
; /* get the cluster */
529 ++mclrefcnt
[mtocl(m
->m_ext
.ext_buf
)];
531 mclfree
= ((union mcluster
*)(m
->m_ext
.ext_buf
))->mcl_next
;
533 m
->m_next
= m
->m_nextpkt
= 0;
535 m
->m_data
= m
->m_ext
.ext_buf
;
536 m
->m_ext
.ext_free
= 0;
537 m
->m_ext
.ext_size
= MCLBYTES
;
538 m
->m_ext
.ext_refs
.forward
= m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
540 if (num_with_pkthdrs
== 0)
543 m
->m_flags
= M_PKTHDR
| M_EXT
;
545 m
->m_pkthdr
.rcvif
= NULL
;
546 m
->m_pkthdr
.header
= NULL
;
547 m
->m_pkthdr
.csum_flags
= 0;
548 m
->m_pkthdr
.csum_data
= 0;
549 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
550 m
->m_pkthdr
.reserved1
= NULL
;
551 m
->m_pkthdr
.reserved2
= NULL
;
560 if (num_with_pkthdrs
== 0) {
561 MGET(m
, how
, MT_DATA
);
563 MGETHDR(m
, how
, MT_DATA
);
573 if ((m
->m_flags
& M_EXT
) == 0) {
581 if (num_with_pkthdrs
)
593 m_getpackethdrs(int num_needed
, int how
)
596 struct mbuf
**np
, *top
;
603 while (num_needed
--) {
604 if (m
= mfree
) { /* mbufs are available */
607 ++mclrefcnt
[mtocl(m
)];
608 mbstat
.m_mtypes
[MT_FREE
]--;
609 mbstat
.m_mtypes
[MT_DATA
]++;
611 m
->m_next
= m
->m_nextpkt
= 0;
613 m
->m_flags
= M_PKTHDR
;
614 m
->m_data
= m
->m_pktdat
;
616 m
->m_pkthdr
.rcvif
= NULL
;
617 m
->m_pkthdr
.header
= NULL
;
618 m
->m_pkthdr
.csum_flags
= 0;
619 m
->m_pkthdr
.csum_data
= 0;
620 m
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
621 m
->m_pkthdr
.reserved1
= NULL
;
622 m
->m_pkthdr
.reserved2
= NULL
;
628 m
= m_retryhdr(how
, MT_DATA
);
644 /* free and mbuf list (m_nextpkt) while following m_next under one lock.
645 * returns the count for mbufs packets freed. Used by the drivers.
651 struct mbuf
*nextpkt
;
658 nextpkt
= m
->m_nextpkt
; /* chain of linked mbufs from driver */
663 while (m
) { /* free the mbuf chain (like mfreem) */
664 struct mbuf
*n
= m
->m_next
;
666 if (n
&& n
->m_nextpkt
)
667 panic("m_freem_list: m_nextpkt of m_next != NULL");
668 if (m
->m_type
== MT_FREE
)
669 panic("freeing free mbuf");
671 if (m
->m_flags
& M_EXT
) {
672 if (MCLHASREFERENCE(m
)) {
673 remque((queue_t
)&m
->m_ext
.ext_refs
);
674 } else if (m
->m_ext
.ext_free
== NULL
) {
675 union mcluster
*mcl
= (union mcluster
*)m
->m_ext
.ext_buf
;
677 mcl
->mcl_next
= mclfree
;
682 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
683 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
686 mbstat
.m_mtypes
[m
->m_type
]--;
688 mbstat
.m_mtypes
[MT_FREE
]++;
696 m
= nextpkt
; /* bump m with saved nextpkt if any */
704 wakeup((caddr_t
)&mfree
);
711 register struct mbuf
*m
;
718 * Mbuffer utility routines.
721 * Compute the amount of space available
722 * before the current start of data in an mbuf.
725 register struct mbuf
*m
;
727 if (m
->m_flags
& M_EXT
) {
728 if (MCLHASREFERENCE(m
))
730 return (m
->m_data
- m
->m_ext
.ext_buf
);
732 if (m
->m_flags
& M_PKTHDR
)
733 return (m
->m_data
- m
->m_pktdat
);
734 return (m
->m_data
- m
->m_dat
);
738 * Compute the amount of space available
739 * after the end of data in an mbuf.
742 register struct mbuf
*m
;
744 if (m
->m_flags
& M_EXT
) {
745 if (MCLHASREFERENCE(m
))
747 return (m
->m_ext
.ext_buf
+ m
->m_ext
.ext_size
-
748 (m
->m_data
+ m
->m_len
));
750 return (&m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
));
754 * Lesser-used path for M_PREPEND:
755 * allocate new mbuf to prepend to chain,
759 m_prepend(m
, len
, how
)
760 register struct mbuf
*m
;
765 MGET(mn
, how
, m
->m_type
);
766 if (mn
== (struct mbuf
*)NULL
) {
768 return ((struct mbuf
*)NULL
);
770 if (m
->m_flags
& M_PKTHDR
) {
771 M_COPY_PKTHDR(mn
, m
);
772 m
->m_flags
&= ~M_PKTHDR
;
783 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
784 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
785 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
790 m_copym(m
, off0
, len
, wait
)
791 register struct mbuf
*m
;
795 register struct mbuf
*n
, **np
;
796 register int off
= off0
;
800 if (off
< 0 || len
< 0)
802 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
805 while (off
>= m
->m_len
) {
818 if (len
!= M_COPYALL
)
824 ++mclrefcnt
[mtocl(n
)];
825 mbstat
.m_mtypes
[MT_FREE
]--;
826 mbstat
.m_mtypes
[m
->m_type
]++;
828 n
->m_next
= n
->m_nextpkt
= 0;
829 n
->m_type
= m
->m_type
;
830 n
->m_data
= n
->m_dat
;
834 n
= m_retry(wait
, m
->m_type
);
843 if (len
== M_COPYALL
)
844 n
->m_pkthdr
.len
-= off0
;
846 n
->m_pkthdr
.len
= len
;
849 if (len
== M_COPYALL
) {
850 if (min(len
, (m
->m_len
- off
)) == len
) {
851 printf("m->m_len %d - off %d = %d, %d\n",
852 m
->m_len
, off
, m
->m_len
- off
,
853 min(len
, (m
->m_len
- off
)));
856 n
->m_len
= min(len
, (m
->m_len
- off
));
857 if (n
->m_len
== M_COPYALL
) {
858 printf("n->m_len == M_COPYALL, fixing\n");
861 if (m
->m_flags
& M_EXT
) {
863 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
864 n
->m_data
= m
->m_data
+ off
;
867 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
870 if (len
!= M_COPYALL
)
893 m_copym_with_hdrs(m
, off0
, len
, wait
, m_last
, m_off
)
894 register struct mbuf
*m
;
897 struct mbuf
**m_last
;
900 register struct mbuf
*n
, **np
;
901 register int off
= off0
;
902 struct mbuf
*top
= 0;
906 if (off
== 0 && m
->m_flags
& M_PKTHDR
)
913 while (off
>= m
->m_len
) {
925 panic("m_gethdr_and_copym");
930 ++mclrefcnt
[mtocl(n
)];
931 mbstat
.m_mtypes
[MT_FREE
]--;
932 mbstat
.m_mtypes
[type
]++;
934 n
->m_next
= n
->m_nextpkt
= 0;
938 n
->m_data
= n
->m_dat
;
941 n
->m_data
= n
->m_pktdat
;
942 n
->m_flags
= M_PKTHDR
;
944 n
->m_pkthdr
.rcvif
= NULL
;
945 n
->m_pkthdr
.header
= NULL
;
946 n
->m_pkthdr
.csum_flags
= 0;
947 n
->m_pkthdr
.csum_data
= 0;
948 n
->m_pkthdr
.aux
= (struct mbuf
*)NULL
;
949 n
->m_pkthdr
.reserved1
= NULL
;
950 n
->m_pkthdr
.reserved2
= NULL
;
955 n
= m_retry(wait
, type
);
957 n
= m_retryhdr(wait
, type
);
971 n
->m_pkthdr
.len
= len
;
974 n
->m_len
= min(len
, (m
->m_len
- off
));
976 if (m
->m_flags
& M_EXT
) {
978 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
979 n
->m_data
= m
->m_data
+ off
;
982 bcopy(mtod(m
, caddr_t
)+off
, mtod(n
, caddr_t
),
988 if ((off
+ n
->m_len
) == m
->m_len
) {
993 *m_off
= off
+ n
->m_len
;
1015 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1016 * continuing for "len" bytes, into the indicated buffer.
1018 void m_copydata(m
, off
, len
, cp
)
1019 register struct mbuf
*m
;
1024 register unsigned count
;
1026 if (off
< 0 || len
< 0)
1027 panic("m_copydata");
1030 panic("m_copydata");
1038 panic("m_copydata");
1039 count
= min(m
->m_len
- off
, len
);
1040 bcopy(mtod(m
, caddr_t
) + off
, cp
, count
);
1049 * Concatenate mbuf chain n to m.
1050 * Both chains must be of the same type (e.g. MT_DATA).
1051 * Any m_pkthdr is not updated.
1054 register struct mbuf
*m
, *n
;
1059 if (m
->m_flags
& M_EXT
||
1060 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
1061 /* just join the two chains */
1065 /* splat the data from one into the other */
1066 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1068 m
->m_len
+= n
->m_len
;
1078 register int len
= req_len
;
1079 register struct mbuf
*m
;
1082 if ((m
= mp
) == NULL
)
1088 while (m
!= NULL
&& len
> 0) {
1089 if (m
->m_len
<= len
) {
1100 if (m
->m_flags
& M_PKTHDR
)
1101 m
->m_pkthdr
.len
-= (req_len
- len
);
1104 * Trim from tail. Scan the mbuf chain,
1105 * calculating its length and finding the last mbuf.
1106 * If the adjustment only affects this mbuf, then just
1107 * adjust and return. Otherwise, rescan and truncate
1108 * after the remaining size.
1114 if (m
->m_next
== (struct mbuf
*)0)
1118 if (m
->m_len
>= len
) {
1121 if (m
->m_flags
& M_PKTHDR
)
1122 m
->m_pkthdr
.len
-= len
;
1129 * Correct length for chain is "count".
1130 * Find the mbuf with last data, adjust its length,
1131 * and toss data from remaining mbufs on chain.
1134 if (m
->m_flags
& M_PKTHDR
)
1135 m
->m_pkthdr
.len
= count
;
1136 for (; m
; m
= m
->m_next
) {
1137 if (m
->m_len
>= count
) {
1143 while (m
= m
->m_next
)
1149 * Rearange an mbuf chain so that len bytes are contiguous
1150 * and in the data area of an mbuf (so that mtod and dtom
1151 * will work for a structure of size len). Returns the resulting
1152 * mbuf chain on success, frees it and returns null on failure.
1153 * If there is room, it will add up to max_protohdr-len extra bytes to the
1154 * contiguous region in an attempt to avoid being called next time.
1160 register struct mbuf
*n
;
1163 register struct mbuf
*m
;
1168 * If first mbuf has no cluster, and has room for len bytes
1169 * without shifting current data, pullup into it,
1170 * otherwise allocate a new mbuf to prepend to the chain.
1172 if ((n
->m_flags
& M_EXT
) == 0 &&
1173 n
->m_data
+ len
< &n
->m_dat
[MLEN
] && n
->m_next
) {
1174 if (n
->m_len
>= len
)
1182 MGET(m
, M_DONTWAIT
, n
->m_type
);
1186 if (n
->m_flags
& M_PKTHDR
) {
1187 M_COPY_PKTHDR(m
, n
);
1188 n
->m_flags
&= ~M_PKTHDR
;
1191 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
1193 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
1194 bcopy(mtod(n
, caddr_t
), mtod(m
, caddr_t
) + m
->m_len
,
1204 } while (len
> 0 && n
);
1218 * Partition an mbuf chain in two pieces, returning the tail --
1219 * all but the first len0 bytes. In case of failure, it returns NULL and
1220 * attempts to restore the chain to its original state.
1223 m_split(m0
, len0
, wait
)
1224 register struct mbuf
*m0
;
1227 register struct mbuf
*m
, *n
;
1228 unsigned len
= len0
, remain
;
1230 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
1234 remain
= m
->m_len
- len
;
1235 if (m0
->m_flags
& M_PKTHDR
) {
1236 MGETHDR(n
, wait
, m0
->m_type
);
1239 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
1240 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
1241 m0
->m_pkthdr
.len
= len0
;
1242 if (m
->m_flags
& M_EXT
)
1244 if (remain
> MHLEN
) {
1245 /* m can't be the lead packet */
1247 n
->m_next
= m_split(m
, len
, wait
);
1248 if (n
->m_next
== 0) {
1254 MH_ALIGN(n
, remain
);
1255 } else if (remain
== 0) {
1260 MGET(n
, wait
, m
->m_type
);
1266 if (m
->m_flags
& M_EXT
) {
1267 n
->m_flags
|= M_EXT
;
1269 n
->m_ext
= m
->m_ext
;
1270 insque((queue_t
)&n
->m_ext
.ext_refs
, (queue_t
)&m
->m_ext
.ext_refs
);
1272 n
->m_data
= m
->m_data
+ len
;
1274 bcopy(mtod(m
, caddr_t
) + len
, mtod(n
, caddr_t
), remain
);
1278 n
->m_next
= m
->m_next
;
1283 * Routine to copy from device local memory into mbufs.
1286 m_devget(buf
, totlen
, off0
, ifp
, copy
)
1292 register struct mbuf
*m
;
1293 struct mbuf
*top
= 0, **mp
= &top
;
1294 register int off
= off0
, len
;
1302 * If 'off' is non-zero, packet is trailer-encapsulated,
1303 * so we have to skip the type and length fields.
1305 cp
+= off
+ 2 * sizeof(u_int16_t
);
1306 totlen
-= 2 * sizeof(u_int16_t
);
1308 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1311 m
->m_pkthdr
.rcvif
= ifp
;
1312 m
->m_pkthdr
.len
= totlen
;
1315 while (totlen
> 0) {
1317 MGET(m
, M_DONTWAIT
, MT_DATA
);
1324 len
= min(totlen
, epkt
- cp
);
1325 if (len
>= MINCLSIZE
) {
1326 MCLGET(m
, M_DONTWAIT
);
1327 if (m
->m_flags
& M_EXT
)
1328 m
->m_len
= len
= min(len
, MCLBYTES
);
1330 /* give up when it's out of cluster mbufs */
1338 * Place initial small packet/header at end of mbuf.
1340 if (len
< m
->m_len
) {
1341 if (top
== 0 && len
+ max_linkhdr
<= m
->m_len
)
1342 m
->m_data
+= max_linkhdr
;
1348 copy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
1350 bcopy(cp
, mtod(m
, caddr_t
), (unsigned)len
);
1362 * Cluster freelist allocation check. The mbuf lock must be held.
1363 * Ensure hysteresis between hi/lo.
1371 if (mbstat
.m_clusters
< MINCL
)
1372 return (MINCL
- mbstat
.m_clusters
);
1373 /* Too few (free < 1/2 total) and not over maximum */
1374 if (mbstat
.m_clusters
< nmbclusters
&&
1375 (i
= ((mbstat
.m_clusters
>> 1) - mbstat
.m_clfree
)) > 0)
1382 * Copy data from a buffer back into the indicated mbuf chain,
1383 * starting "off" bytes from the beginning, extending the mbuf
1384 * chain if necessary.
1387 m_copyback(m0
, off
, len
, cp
)
1394 register struct mbuf
*m
= m0
, *n
;
1399 while (off
> (mlen
= m
->m_len
)) {
1402 if (m
->m_next
== 0) {
1403 n
= m_getclr(M_DONTWAIT
, m
->m_type
);
1406 n
->m_len
= min(MLEN
, len
+ off
);
1412 mlen
= min (m
->m_len
- off
, len
);
1413 bcopy(cp
, off
+ mtod(m
, caddr_t
), (unsigned)mlen
);
1421 if (m
->m_next
== 0) {
1422 n
= m_get(M_DONTWAIT
, m
->m_type
);
1425 n
->m_len
= min(MLEN
, len
);
1430 out
: if (((m
= m0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
))
1431 m
->m_pkthdr
.len
= totlen
;
1435 char *mcl_to_paddr(register char *addr
) {
1436 register int base_phys
;
1438 if (addr
< (char *)mbutl
|| addr
>= (char *)embutl
)
1440 base_phys
= mcl_paddr
[(addr
- (char *)mbutl
) >> PAGE_SHIFT
];
1444 return ((char *)((int)base_phys
| ((int)addr
& PAGE_MASK
)));
1448 * Dup the mbuf chain passed in. The whole thing. No cute additional cruft.
1449 * And really copy the thing. That way, we don't "precompute" checksums
1450 * for unsuspecting consumers.
1451 * Assumption: m->m_nextpkt == 0.
1452 * Trick: for small packets, don't dup into a cluster. That way received
1453 * packets don't take up too much room in the sockbuf (cf. sbspace()).
1458 m_dup(register struct mbuf
*m
, int how
)
1459 { register struct mbuf
*n
, **np
;
1465 if (m
->m_flags
& M_PKTHDR
)
1469 * Quick check: if we have one mbuf and its data fits in an
1470 * mbuf with packet header, just copy and go.
1472 if (m
->m_next
== NULL
)
1473 { /* Then just move the data into an mbuf and be done... */
1475 { if (m
->m_pkthdr
.len
<= MHLEN
)
1476 { if ((n
= m_gethdr(how
, m
->m_type
)) == NULL
)
1478 n
->m_len
= m
->m_len
;
1479 n
->m_flags
|= (m
->m_flags
& M_COPYFLAGS
);
1480 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
;
1481 n
->m_pkthdr
.rcvif
= m
->m_pkthdr
.rcvif
;
1482 n
->m_pkthdr
.header
= NULL
;
1483 n
->m_pkthdr
.csum_flags
= 0;
1484 n
->m_pkthdr
.csum_data
= 0;
1485 n
->m_pkthdr
.aux
= NULL
;
1486 n
->m_pkthdr
.reserved1
= 0;
1487 n
->m_pkthdr
.reserved2
= 0;
1488 bcopy(m
->m_data
, n
->m_data
, m
->m_pkthdr
.len
);
1491 } else if (m
->m_len
<= MLEN
)
1492 { if ((n
= m_get(how
, m
->m_type
)) == NULL
)
1494 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
1495 n
->m_len
= m
->m_len
;
1502 kprintf("<%x: %x, %x, %x\n", m
, m
->m_flags
, m
->m_len
,
1506 n
= m_gethdr(how
, m
->m_type
);
1508 n
= m_get(how
, m
->m_type
);
1511 if (m
->m_flags
& M_EXT
)
1513 if ((n
->m_flags
& M_EXT
) == 0)
1518 { /* Don't use M_COPY_PKTHDR: preserve m_data */
1519 n
->m_pkthdr
= m
->m_pkthdr
;
1520 n
->m_flags
|= (m
->m_flags
& M_COPYFLAGS
);
1522 if ((n
->m_flags
& M_EXT
) == 0)
1523 n
->m_data
= n
->m_pktdat
;
1525 n
->m_len
= m
->m_len
;
1527 * Get the dup on the same bdry as the original
1528 * Assume that the two mbufs have the same offset to data area
1529 * (up to word bdries)
1531 bcopy(mtod(m
, caddr_t
), mtod(n
, caddr_t
), (unsigned)n
->m_len
);
1535 kprintf(">%x: %x, %x, %x\n", n
, n
->m_flags
, n
->m_len
,
1550 #include <sys/sysctl.h>
1552 static int mhog_num
= 0;
1553 static struct mbuf
*mhog_chain
= 0;
1554 static int mhog_wait
= 1;
1557 sysctl_mhog_num SYSCTL_HANDLER_ARGS
1562 error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
1563 if (!error
&& req
->newptr
) {
1568 m_freem(mhog_chain
);
1572 for (i
= 0; i
< mhog_num
; i
++) {
1573 MGETHDR(m
, mhog_wait
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
1577 MCLGET(m
, mhog_wait
? M_WAIT
: M_DONTWAIT
);
1578 if ((m
->m_flags
& M_EXT
) == 0) {
1583 m
->m_next
= mhog_chain
;
1592 SYSCTL_NODE(_kern_ipc
, OID_AUTO
, mhog
, CTLFLAG_RW
, 0, "mbuf hog");
1594 SYSCTL_PROC(_kern_ipc_mhog
, OID_AUTO
, cluster
, CTLTYPE_INT
|CTLFLAG_RW
,
1595 &mhog_num
, 0, &sysctl_mhog_num
, "I", "");
1596 SYSCTL_INT(_kern_ipc_mhog
, OID_AUTO
, wait
, CTLFLAG_RW
, &mhog_wait
,