2 * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 //#include <sys/kpi_interface.h>
32 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <kern/debug.h>
36 #include <libkern/OSAtomic.h>
37 #include <kern/kalloc.h>
39 #include <netinet/in.h>
40 #include "kpi_mbuf_internal.h"
42 static const mbuf_flags_t mbuf_flags_mask
= MBUF_EXT
| MBUF_PKTHDR
| MBUF_EOR
|
43 MBUF_BCAST
| MBUF_MCAST
| MBUF_FRAG
| MBUF_FIRSTFRAG
|
44 MBUF_LASTFRAG
| MBUF_PROMISC
;
46 void* mbuf_data(mbuf_t mbuf
)
51 void* mbuf_datastart(mbuf_t mbuf
)
53 if (mbuf
->m_flags
& M_EXT
)
54 return mbuf
->m_ext
.ext_buf
;
55 if (mbuf
->m_flags
& M_PKTHDR
)
56 return mbuf
->m_pktdat
;
60 errno_t
mbuf_setdata(mbuf_t mbuf
, void* data
, size_t len
)
62 size_t start
= (size_t)((char*)mbuf_datastart(mbuf
));
63 size_t maxlen
= mbuf_maxlen(mbuf
);
65 if ((size_t)data
< start
|| ((size_t)data
) + len
> start
+ maxlen
)
73 errno_t
mbuf_align_32(mbuf_t mbuf
, size_t len
)
75 if ((mbuf
->m_flags
& M_EXT
) != 0 && m_mclhasreference(mbuf
))
77 mbuf
->m_data
= mbuf_datastart(mbuf
);
78 mbuf
->m_data
+= ((mbuf_trailingspace(mbuf
) - len
) &~ (sizeof(u_int32_t
) - 1));
83 addr64_t
mbuf_data_to_physical(void* ptr
)
85 return (addr64_t
)(intptr_t)mcl_to_paddr(ptr
);
88 errno_t
mbuf_get(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
)
90 /* Must set *mbuf to NULL in failure case */
91 *mbuf
= m_get(how
, type
);
93 return (*mbuf
== NULL
) ? ENOMEM
: 0;
96 errno_t
mbuf_gethdr(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
)
98 /* Must set *mbuf to NULL in failure case */
99 *mbuf
= m_gethdr(how
, type
);
101 return (*mbuf
== NULL
) ? ENOMEM
: 0;
105 mbuf_attachcluster(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
,
106 caddr_t extbuf
, void (*extfree
)(caddr_t
, u_int
, caddr_t
),
107 size_t extsize
, caddr_t extarg
)
109 if (extbuf
== NULL
|| extfree
== NULL
|| extsize
== 0)
112 if ((*mbuf
= m_clattach(mbuf
!= NULL
? *mbuf
: NULL
, type
, extbuf
,
113 extfree
, extsize
, extarg
, how
)) == NULL
)
120 mbuf_alloccluster(mbuf_how_t how
, size_t *size
, caddr_t
*addr
)
122 if (size
== NULL
|| *size
== 0 || addr
== NULL
)
127 /* Jumbo cluster pool not available? */
128 if (*size
> NBPG
&& njcl
== 0)
131 if (*size
<= MCLBYTES
&& (*addr
= m_mclalloc(how
)) != NULL
)
133 else if (*size
> MCLBYTES
&& *size
<= NBPG
&&
134 (*addr
= m_bigalloc(how
)) != NULL
)
136 else if (*size
> NBPG
&& *size
<= M16KCLBYTES
&&
137 (*addr
= m_16kalloc(how
)) != NULL
)
149 mbuf_freecluster(caddr_t addr
, size_t size
)
151 if (size
!= MCLBYTES
&& size
!= NBPG
&& size
!= M16KCLBYTES
)
152 panic("%s: invalid size (%ld) for cluster %p", __func__
,
155 if (size
== MCLBYTES
)
157 else if (size
== NBPG
)
158 m_bigfree(addr
, NBPG
, NULL
);
160 m_16kfree(addr
, M16KCLBYTES
, NULL
);
162 panic("%s: freeing jumbo cluster to an empty pool", __func__
);
166 mbuf_getcluster(mbuf_how_t how
, mbuf_type_t type
, size_t size
, mbuf_t
* mbuf
)
168 /* Must set *mbuf to NULL in failure case */
175 *mbuf
= m_get(how
, type
);
181 * At the time this code was written, m_{mclget,mbigget,m16kget}
182 * would always return the same value that was passed in to it.
184 if (size
== MCLBYTES
) {
185 *mbuf
= m_mclget(*mbuf
, how
);
186 } else if (size
== NBPG
) {
187 *mbuf
= m_mbigget(*mbuf
, how
);
188 } else if (size
== M16KCLBYTES
) {
190 *mbuf
= m_m16kget(*mbuf
, how
);
192 /* Jumbo cluster pool not available? */
200 if (*mbuf
== NULL
|| ((*mbuf
)->m_flags
& M_EXT
) == 0)
203 if (created
&& error
!= 0) {
210 errno_t
mbuf_mclget(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
)
212 /* Must set *mbuf to NULL in failure case */
215 if (mbuf
== NULL
) return EINVAL
;
217 error
= mbuf_get(how
, type
, mbuf
);
224 * At the time this code was written, m_mclget would always
225 * return the same value that was passed in to it.
227 *mbuf
= m_mclget(*mbuf
, how
);
229 if (created
&& ((*mbuf
)->m_flags
& M_EXT
) == 0) {
233 if (*mbuf
== NULL
|| ((*mbuf
)->m_flags
& M_EXT
) == 0)
239 errno_t
mbuf_getpacket(mbuf_how_t how
, mbuf_t
*mbuf
)
241 /* Must set *mbuf to NULL in failure case */
244 *mbuf
= m_getpacket_how(how
);
247 if (how
== MBUF_WAITOK
)
256 mbuf_t
mbuf_free(mbuf_t mbuf
)
261 void mbuf_freem(mbuf_t mbuf
)
266 int mbuf_freem_list(mbuf_t mbuf
)
268 return m_freem_list(mbuf
);
271 size_t mbuf_leadingspace(const mbuf_t mbuf
)
273 return m_leadingspace(mbuf
);
276 size_t mbuf_trailingspace(const mbuf_t mbuf
)
278 return m_trailingspace(mbuf
);
282 errno_t
mbuf_copym(const mbuf_t src
, size_t offset
, size_t len
,
283 mbuf_how_t how
, mbuf_t
*new_mbuf
)
285 /* Must set *mbuf to NULL in failure case */
286 *new_mbuf
= m_copym(src
, offset
, len
, how
);
288 return (*new_mbuf
== NULL
) ? ENOMEM
: 0;
291 errno_t
mbuf_dup(const mbuf_t src
, mbuf_how_t how
, mbuf_t
*new_mbuf
)
293 /* Must set *new_mbuf to NULL in failure case */
294 *new_mbuf
= m_dup(src
, how
);
296 return (*new_mbuf
== NULL
) ? ENOMEM
: 0;
299 errno_t
mbuf_prepend(mbuf_t
*orig
, size_t len
, mbuf_how_t how
)
301 /* Must set *orig to NULL in failure case */
302 *orig
= m_prepend_2(*orig
, len
, how
);
304 return (*orig
== NULL
) ? ENOMEM
: 0;
307 errno_t
mbuf_split(mbuf_t src
, size_t offset
,
308 mbuf_how_t how
, mbuf_t
*new_mbuf
)
310 /* Must set *new_mbuf to NULL in failure case */
311 *new_mbuf
= m_split(src
, offset
, how
);
313 return (*new_mbuf
== NULL
) ? ENOMEM
: 0;
316 errno_t
mbuf_pullup(mbuf_t
*mbuf
, size_t len
)
318 /* Must set *mbuf to NULL in failure case */
319 *mbuf
= m_pullup(*mbuf
, len
);
321 return (*mbuf
== NULL
) ? ENOMEM
: 0;
324 errno_t
mbuf_pulldown(mbuf_t src
, size_t *offset
, size_t len
, mbuf_t
*location
)
326 /* Must set *location to NULL in failure case */
328 *location
= m_pulldown(src
, *offset
, len
, &new_offset
);
329 *offset
= new_offset
;
331 return (*location
== NULL
) ? ENOMEM
: 0;
334 void mbuf_adj(mbuf_t mbuf
, int len
)
339 errno_t
mbuf_adjustlen(mbuf_t m
, int amount
)
341 /* Verify m_len will be valid after adding amount */
343 int used
= (size_t)mbuf_data(m
) - (size_t)mbuf_datastart(m
) +
346 if ((size_t)(amount
+ used
) > mbuf_maxlen(m
))
349 else if (-amount
> m
->m_len
) {
357 errno_t
mbuf_copydata(const mbuf_t m0
, size_t off
, size_t len
, void* out_data
)
359 /* Copied m_copydata, added error handling (don't just panic) */
366 if (off
< (size_t)m
->m_len
)
374 count
= m
->m_len
- off
> len
? len
: m
->m_len
- off
;
375 bcopy(mtod(m
, caddr_t
) + off
, out_data
, count
);
377 out_data
= ((char*)out_data
) + count
;
385 int mbuf_mclhasreference(mbuf_t mbuf
)
387 if ((mbuf
->m_flags
& M_EXT
))
388 return m_mclhasreference(mbuf
);
395 mbuf_t
mbuf_next(const mbuf_t mbuf
)
400 errno_t
mbuf_setnext(mbuf_t mbuf
, mbuf_t next
)
402 if (next
&& ((next
)->m_nextpkt
!= NULL
||
403 (next
)->m_type
== MT_FREE
)) return EINVAL
;
409 mbuf_t
mbuf_nextpkt(const mbuf_t mbuf
)
411 return mbuf
->m_nextpkt
;
414 void mbuf_setnextpkt(mbuf_t mbuf
, mbuf_t nextpkt
)
416 mbuf
->m_nextpkt
= nextpkt
;
419 size_t mbuf_len(const mbuf_t mbuf
)
424 void mbuf_setlen(mbuf_t mbuf
, size_t len
)
429 size_t mbuf_maxlen(const mbuf_t mbuf
)
431 if (mbuf
->m_flags
& M_EXT
)
432 return mbuf
->m_ext
.ext_size
;
433 return &mbuf
->m_dat
[MLEN
] - ((char*)mbuf_datastart(mbuf
));
436 mbuf_type_t
mbuf_type(const mbuf_t mbuf
)
441 errno_t
mbuf_settype(mbuf_t mbuf
, mbuf_type_t new_type
)
443 if (new_type
== MBUF_TYPE_FREE
) return EINVAL
;
445 m_mchtype(mbuf
, new_type
);
450 mbuf_flags_t
mbuf_flags(const mbuf_t mbuf
)
452 return mbuf
->m_flags
& mbuf_flags_mask
;
455 errno_t
mbuf_setflags(mbuf_t mbuf
, mbuf_flags_t flags
)
457 if ((flags
& ~mbuf_flags_mask
) != 0) return EINVAL
;
458 mbuf
->m_flags
= flags
|
459 (mbuf
->m_flags
& ~mbuf_flags_mask
);
464 errno_t
mbuf_setflags_mask(mbuf_t mbuf
, mbuf_flags_t flags
, mbuf_flags_t mask
)
466 if (((flags
| mask
) & ~mbuf_flags_mask
) != 0) return EINVAL
;
468 mbuf
->m_flags
= (flags
& mask
) | (mbuf
->m_flags
& ~mask
);
473 errno_t
mbuf_copy_pkthdr(mbuf_t dest
, const mbuf_t src
)
475 if (((src
)->m_flags
& M_PKTHDR
) == 0)
478 m_copy_pkthdr(dest
, src
);
483 size_t mbuf_pkthdr_len(const mbuf_t mbuf
)
485 return mbuf
->m_pkthdr
.len
;
488 void mbuf_pkthdr_setlen(mbuf_t mbuf
, size_t len
)
490 mbuf
->m_pkthdr
.len
= len
;
493 void mbuf_pkthdr_adjustlen(mbuf_t mbuf
, int amount
)
495 mbuf
->m_pkthdr
.len
+= amount
;
498 ifnet_t
mbuf_pkthdr_rcvif(const mbuf_t mbuf
)
500 // If we reference count ifnets, we should take a reference here before returning
501 return mbuf
->m_pkthdr
.rcvif
;
504 errno_t
mbuf_pkthdr_setrcvif(mbuf_t mbuf
, ifnet_t ifnet
)
506 /* May want to walk ifnet list to determine if interface is valid */
507 mbuf
->m_pkthdr
.rcvif
= (struct ifnet
*)ifnet
;
511 void* mbuf_pkthdr_header(const mbuf_t mbuf
)
513 return mbuf
->m_pkthdr
.header
;
516 void mbuf_pkthdr_setheader(mbuf_t mbuf
, void *header
)
518 mbuf
->m_pkthdr
.header
= (void*)header
;
522 mbuf_inbound_modified(mbuf_t mbuf
)
524 /* Invalidate hardware generated checksum flags */
525 mbuf
->m_pkthdr
.csum_flags
= 0;
528 extern void in_cksum_offset(struct mbuf
* m
, size_t ip_offset
);
529 extern void in_delayed_cksum_offset(struct mbuf
*m
, int ip_offset
);
532 mbuf_outbound_finalize(mbuf_t mbuf
, u_long protocol_family
, size_t protocol_offset
)
534 if ((mbuf
->m_pkthdr
.csum_flags
&
535 (CSUM_DELAY_DATA
| CSUM_DELAY_IP
| CSUM_TCP_SUM16
)) == 0)
538 /* Generate the packet in software, client needs it */
539 switch (protocol_family
) {
541 if (mbuf
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) {
543 * If you're wondering where this lovely code comes
544 * from, we're trying to undo what happens in ip_output.
545 * Look for CSUM_TCP_SUM16 in ip_output.
547 u_int16_t first
, second
;
548 mbuf
->m_pkthdr
.csum_flags
&= ~CSUM_TCP_SUM16
;
549 mbuf
->m_pkthdr
.csum_flags
|= CSUM_TCP
;
550 first
= mbuf
->m_pkthdr
.csum_data
>> 16;
551 second
= mbuf
->m_pkthdr
.csum_data
& 0xffff;
552 mbuf
->m_pkthdr
.csum_data
= first
- second
;
554 if (mbuf
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
555 in_delayed_cksum_offset(mbuf
, protocol_offset
);
558 if (mbuf
->m_pkthdr
.csum_flags
& CSUM_DELAY_IP
) {
559 in_cksum_offset(mbuf
, protocol_offset
);
562 mbuf
->m_pkthdr
.csum_flags
&= ~(CSUM_DELAY_DATA
| CSUM_DELAY_IP
);
567 * Not sure what to do here if anything.
568 * Hardware checksum code looked pretty IPv4 specific.
570 if ((mbuf
->m_pkthdr
.csum_flags
& (CSUM_DELAY_DATA
| CSUM_DELAY_IP
)) != 0)
571 panic("mbuf_outbound_finalize - CSUM flags set for non-IPv4 packet (%lu)!\n", protocol_family
);
580 mbuf
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
581 mbuf
->m_pkthdr
.vlan_tag
= vlan
;
591 if ((mbuf
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) == 0)
592 return ENXIO
; // No vlan tag set
594 *vlan
= mbuf
->m_pkthdr
.vlan_tag
;
603 mbuf
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
604 mbuf
->m_pkthdr
.vlan_tag
= 0;
609 static const mbuf_csum_request_flags_t mbuf_valid_csum_request_flags
=
610 MBUF_CSUM_REQ_IP
| MBUF_CSUM_REQ_TCP
| MBUF_CSUM_REQ_UDP
| MBUF_CSUM_REQ_SUM16
;
613 mbuf_set_csum_requested(
615 mbuf_csum_request_flags_t request
,
618 request
&= mbuf_valid_csum_request_flags
;
619 mbuf
->m_pkthdr
.csum_flags
= (mbuf
->m_pkthdr
.csum_flags
& 0xffff0000) | request
;
620 mbuf
->m_pkthdr
.csum_data
= value
;
626 mbuf_get_csum_requested(
628 mbuf_csum_request_flags_t
*request
,
631 *request
= mbuf
->m_pkthdr
.csum_flags
;
632 *request
&= mbuf_valid_csum_request_flags
;
634 *value
= mbuf
->m_pkthdr
.csum_data
;
641 mbuf_clear_csum_requested(
644 mbuf
->m_pkthdr
.csum_flags
&= 0xffff0000;
645 mbuf
->m_pkthdr
.csum_data
= 0;
650 static const mbuf_csum_performed_flags_t mbuf_valid_csum_performed_flags
=
651 MBUF_CSUM_DID_IP
| MBUF_CSUM_IP_GOOD
| MBUF_CSUM_DID_DATA
|
652 MBUF_CSUM_PSEUDO_HDR
| MBUF_CSUM_TCP_SUM16
;
655 mbuf_set_csum_performed(
657 mbuf_csum_performed_flags_t performed
,
660 performed
&= mbuf_valid_csum_performed_flags
;
661 mbuf
->m_pkthdr
.csum_flags
= (mbuf
->m_pkthdr
.csum_flags
& 0xffff0000) | performed
;
662 mbuf
->m_pkthdr
.csum_data
= value
;
668 mbuf_get_csum_performed(
670 mbuf_csum_performed_flags_t
*performed
,
673 *performed
= mbuf
->m_pkthdr
.csum_flags
& mbuf_valid_csum_performed_flags
;
674 *value
= mbuf
->m_pkthdr
.csum_data
;
680 mbuf_clear_csum_performed(
683 mbuf
->m_pkthdr
.csum_flags
&= 0xffff0000;
684 mbuf
->m_pkthdr
.csum_data
= 0;
690 mbuf_inet_cksum(mbuf_t mbuf
, int protocol
, u_int32_t offset
, u_int32_t length
,
693 if (mbuf
== NULL
|| length
== 0 || csum
== NULL
||
694 (u_int32_t
)mbuf
->m_pkthdr
.len
< (offset
+ length
))
697 *csum
= inet_cksum(mbuf
, protocol
, offset
, length
);
703 mbuf_inet6_cksum(mbuf_t mbuf
, int protocol
, u_int32_t offset
, u_int32_t length
,
706 if (mbuf
== NULL
|| length
== 0 || csum
== NULL
||
707 (u_int32_t
)mbuf
->m_pkthdr
.len
< (offset
+ length
))
710 *csum
= inet6_cksum(mbuf
, protocol
, offset
, length
);
715 mbuf_inet6_cksum(__unused mbuf_t mbuf
, __unused
int protocol
,
716 __unused u_int32_t offset
, __unused u_int32_t length
,
717 __unused u_int16_t
*csum
)
719 panic("mbuf_inet6_cksum() doesn't exist on this platform\n");
724 inet6_cksum(__unused
struct mbuf
*m
, __unused
unsigned int nxt
,
725 __unused
unsigned int off
, __unused
unsigned int len
)
727 panic("inet6_cksum() doesn't exist on this platform\n");
731 void nd6_lookup_ipv6(void);
733 nd6_lookup_ipv6(void)
735 panic("nd6_lookup_ipv6() doesn't exist on this platform\n");
739 in6addr_local(__unused
struct in6_addr
*a
)
741 panic("in6addr_local() doesn't exist on this platform\n");
745 void nd6_storelladdr(void);
747 nd6_storelladdr(void)
749 panic("nd6_storelladdr() doesn't exist on this platform\n");
757 struct mbuf_tag_id_entry
{
758 SLIST_ENTRY(mbuf_tag_id_entry
) next
;
763 #define MBUF_TAG_ID_ENTRY_SIZE(__str) \
764 ((size_t)&(((struct mbuf_tag_id_entry*)0)->string[0]) + \
767 #define MTAG_FIRST_ID 1000
768 static mbuf_tag_id_t mtag_id_next
= MTAG_FIRST_ID
;
769 static SLIST_HEAD(,mbuf_tag_id_entry
) mtag_id_list
= {NULL
};
770 static lck_mtx_t
*mtag_id_lock
= NULL
;
772 __private_extern__
void
773 mbuf_tag_id_first_last(
774 mbuf_tag_id_t
* first
,
775 mbuf_tag_id_t
* last
)
777 *first
= MTAG_FIRST_ID
;
778 *last
= mtag_id_next
- 1;
781 __private_extern__ errno_t
782 mbuf_tag_id_find_internal(
784 mbuf_tag_id_t
*out_id
,
787 struct mbuf_tag_id_entry
*entry
= NULL
;
792 if (string
== NULL
|| out_id
== NULL
) {
796 /* Don't bother allocating the lock if we're only doing a lookup */
797 if (create
== 0 && mtag_id_lock
== NULL
)
800 /* Allocate lock if necessary */
801 if (mtag_id_lock
== NULL
) {
802 lck_grp_attr_t
*grp_attrib
= NULL
;
803 lck_attr_t
*lck_attrb
= NULL
;
804 lck_grp_t
*lck_group
= NULL
;
805 lck_mtx_t
*new_lock
= NULL
;
807 grp_attrib
= lck_grp_attr_alloc_init();
808 lck_group
= lck_grp_alloc_init("mbuf_tag_allocate_id", grp_attrib
);
809 lck_grp_attr_free(grp_attrib
);
810 lck_attrb
= lck_attr_alloc_init();
812 new_lock
= lck_mtx_alloc_init(lck_group
, lck_attrb
);
813 if (!OSCompareAndSwap((UInt32
)0, (UInt32
)new_lock
, (UInt32
*)&mtag_id_lock
)) {
815 * If the atomic swap fails, someone else has already
816 * done this work. We can free the stuff we allocated.
818 lck_mtx_free(new_lock
, lck_group
);
819 lck_grp_free(lck_group
);
821 lck_attr_free(lck_attrb
);
824 /* Look for an existing entry */
825 lck_mtx_lock(mtag_id_lock
);
826 SLIST_FOREACH(entry
, &mtag_id_list
, next
) {
827 if (strncmp(string
, entry
->string
, strlen(string
) + 1) == 0) {
834 lck_mtx_unlock(mtag_id_lock
);
838 entry
= kalloc(MBUF_TAG_ID_ENTRY_SIZE(string
));
840 lck_mtx_unlock(mtag_id_lock
);
844 strlcpy(entry
->string
, string
, strlen(string
)+1);
845 entry
->id
= mtag_id_next
;
847 SLIST_INSERT_HEAD(&mtag_id_list
, entry
, next
);
849 lck_mtx_unlock(mtag_id_lock
);
859 mbuf_tag_id_t
*out_id
)
861 return mbuf_tag_id_find_internal(string
, out_id
, 1);
868 mbuf_tag_type_t type
,
878 /* Sanity check parameters */
879 if (mbuf
== NULL
|| (mbuf
->m_flags
& M_PKTHDR
) == 0 || id
< MTAG_FIRST_ID
||
880 id
>= mtag_id_next
|| length
< 1 || (length
& 0xffff0000) != 0 ||
885 /* Make sure this mtag hasn't already been allocated */
886 tag
= m_tag_locate(mbuf
, id
, type
, NULL
);
891 /* Allocate an mtag */
892 tag
= m_tag_alloc(id
, type
, length
, how
);
894 return how
== M_WAITOK
? ENOMEM
: EWOULDBLOCK
;
897 /* Attach the mtag and set *data_p */
898 m_tag_prepend(mbuf
, tag
);
908 mbuf_tag_type_t type
,
919 /* Sanity check parameters */
920 if (mbuf
== NULL
|| (mbuf
->m_flags
& M_PKTHDR
) == 0 || id
< MTAG_FIRST_ID
||
921 id
>= mtag_id_next
|| length
== NULL
|| data_p
== NULL
) {
926 tag
= m_tag_locate(mbuf
, id
, type
, NULL
);
931 /* Copy out the pointer to the data and the lenght value */
932 *length
= tag
->m_tag_len
;
942 mbuf_tag_type_t type
)
946 if (mbuf
== NULL
|| (mbuf
->m_flags
& M_PKTHDR
) == 0 || id
< MTAG_FIRST_ID
||
950 tag
= m_tag_locate(mbuf
, id
, type
, NULL
);
955 m_tag_delete(mbuf
, tag
);
960 void mbuf_stats(struct mbuf_stat
*stats
)
962 stats
->mbufs
= mbstat
.m_mbufs
;
963 stats
->clusters
= mbstat
.m_clusters
;
964 stats
->clfree
= mbstat
.m_clfree
;
965 stats
->drops
= mbstat
.m_drops
;
966 stats
->wait
= mbstat
.m_wait
;
967 stats
->drain
= mbstat
.m_drain
;
968 __builtin_memcpy(stats
->mtypes
, mbstat
.m_mtypes
, sizeof(stats
->mtypes
));
969 stats
->mcfail
= mbstat
.m_mcfail
;
970 stats
->mpfail
= mbstat
.m_mpfail
;
971 stats
->msize
= mbstat
.m_msize
;
972 stats
->mclbytes
= mbstat
.m_mclbytes
;
973 stats
->minclsize
= mbstat
.m_minclsize
;
974 stats
->mlen
= mbstat
.m_mlen
;
975 stats
->mhlen
= mbstat
.m_mhlen
;
976 stats
->bigclusters
= mbstat
.m_bigclusters
;
977 stats
->bigclfree
= mbstat
.m_bigclfree
;
978 stats
->bigmclbytes
= mbstat
.m_bigmclbytes
;
982 mbuf_allocpacket(mbuf_how_t how
, size_t packetlen
, unsigned int *maxchunks
, mbuf_t
*mbuf
)
986 unsigned int numpkts
= 1;
987 unsigned int numchunks
= maxchunks
? *maxchunks
: 0;
989 if (packetlen
== 0) {
993 m
= m_allocpacket_internal(&numpkts
, packetlen
, maxchunks
? &numchunks
: NULL
, how
, 1, 0);
995 if (maxchunks
&& *maxchunks
&& numchunks
> *maxchunks
)
1001 *maxchunks
= numchunks
;
1010 mbuf_allocpacket_list(unsigned int numpkts
, mbuf_how_t how
, size_t packetlen
, unsigned int *maxchunks
, mbuf_t
*mbuf
)
1014 unsigned int numchunks
= maxchunks
? *maxchunks
: 0;
1020 if (packetlen
== 0) {
1024 m
= m_allocpacket_internal(&numpkts
, packetlen
, maxchunks
? &numchunks
: NULL
, how
, 1, 0);
1026 if (maxchunks
&& *maxchunks
&& numchunks
> *maxchunks
)
1032 *maxchunks
= numchunks
;
1043 * mbuf_copyback differs from m_copyback in a few ways:
1044 * 1) mbuf_copyback will allocate clusters for new mbufs we append
1045 * 2) mbuf_copyback will grow the last mbuf in the chain if possible
1046 * 3) mbuf_copyback reports whether or not the operation succeeded
1047 * 4) mbuf_copyback allows the caller to specify M_WAITOK or M_NOWAIT
1062 const char *cp
= data
;
1064 if (m
== NULL
|| len
== 0 || data
== NULL
)
1067 while (off
> (mlen
= m
->m_len
)) {
1070 if (m
->m_next
== 0) {
1071 n
= m_getclr(how
, m
->m_type
);
1076 n
->m_len
= MIN(MLEN
, len
+ off
);
1083 mlen
= MIN(m
->m_len
- off
, len
);
1084 if (mlen
< len
&& m
->m_next
== NULL
&& mbuf_trailingspace(m
) > 0) {
1085 size_t grow
= MIN(mbuf_trailingspace(m
), len
- mlen
);
1089 bcopy(cp
, off
+ (char*)mbuf_data(m
), (unsigned)mlen
);
1097 if (m
->m_next
== 0) {
1098 n
= m_get(how
, m
->m_type
);
1103 if (len
> MINCLSIZE
) {
1104 /* cluter allocation failure is okay, we can grow chain */
1105 mbuf_mclget(how
, m
->m_type
, &n
);
1107 n
->m_len
= MIN(mbuf_maxlen(n
), len
);
1114 if ((m_start
->m_flags
& M_PKTHDR
) && (m_start
->m_pkthdr
.len
< totlen
))
1115 m_start
->m_pkthdr
.len
= totlen
;
1121 void inet6_unsupported(void);
1123 void inet6_unsupported(void)