2 * Copyright (c) 2004-2011 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/mcache.h>
35 #include <sys/socket.h>
36 #include <kern/debug.h>
37 #include <libkern/OSAtomic.h>
38 #include <kern/kalloc.h>
40 #include <netinet/in.h>
42 #include "net/net_str_id.h"
44 static const mbuf_flags_t mbuf_flags_mask
= (MBUF_EXT
| MBUF_PKTHDR
| MBUF_EOR
|
45 MBUF_LOOP
| MBUF_BCAST
| MBUF_MCAST
| MBUF_FRAG
| MBUF_FIRSTFRAG
|
46 MBUF_LASTFRAG
| MBUF_PROMISC
| MBUF_HASFCS
);
48 #define MBUF_PKTAUXF_MASK \
49 (MBUF_PKTAUXF_INET_RESOLVE_RTR | MBUF_PKTAUXF_INET6_RESOLVE_RTR)
51 void* mbuf_data(mbuf_t mbuf
)
56 void* mbuf_datastart(mbuf_t mbuf
)
58 if (mbuf
->m_flags
& M_EXT
)
59 return mbuf
->m_ext
.ext_buf
;
60 if (mbuf
->m_flags
& M_PKTHDR
)
61 return mbuf
->m_pktdat
;
65 errno_t
mbuf_setdata(mbuf_t mbuf
, void* data
, size_t len
)
67 size_t start
= (size_t)((char*)mbuf_datastart(mbuf
));
68 size_t maxlen
= mbuf_maxlen(mbuf
);
70 if ((size_t)data
< start
|| ((size_t)data
) + len
> start
+ maxlen
)
78 errno_t
mbuf_align_32(mbuf_t mbuf
, size_t len
)
80 if ((mbuf
->m_flags
& M_EXT
) != 0 && m_mclhasreference(mbuf
))
82 mbuf
->m_data
= mbuf_datastart(mbuf
);
83 mbuf
->m_data
+= ((mbuf_trailingspace(mbuf
) - len
) &~ (sizeof(u_int32_t
) - 1));
88 /* This function is used to provide mcl_to_paddr via symbol indirection,
89 * please avoid any change in behavior or remove the indirection in
92 addr64_t
mbuf_data_to_physical(void* ptr
)
94 return (addr64_t
)(uintptr_t)mcl_to_paddr(ptr
);
97 errno_t
mbuf_get(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
)
99 /* Must set *mbuf to NULL in failure case */
100 *mbuf
= m_get(how
, type
);
102 return (*mbuf
== NULL
) ? ENOMEM
: 0;
105 errno_t
mbuf_gethdr(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
)
107 /* Must set *mbuf to NULL in failure case */
108 *mbuf
= m_gethdr(how
, type
);
110 return (*mbuf
== NULL
) ? ENOMEM
: 0;
114 mbuf_attachcluster(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
,
115 caddr_t extbuf
, void (*extfree
)(caddr_t
, u_int
, caddr_t
),
116 size_t extsize
, caddr_t extarg
)
118 if (mbuf
== NULL
|| extbuf
== NULL
|| extfree
== NULL
|| extsize
== 0)
121 if ((*mbuf
= m_clattach(*mbuf
, type
, extbuf
,
122 extfree
, extsize
, extarg
, how
)) == NULL
)
129 mbuf_alloccluster(mbuf_how_t how
, size_t *size
, caddr_t
*addr
)
131 if (size
== NULL
|| *size
== 0 || addr
== NULL
)
136 /* Jumbo cluster pool not available? */
137 if (*size
> MBIGCLBYTES
&& njcl
== 0)
140 if (*size
<= MCLBYTES
&& (*addr
= m_mclalloc(how
)) != NULL
)
142 else if (*size
> MCLBYTES
&& *size
<= MBIGCLBYTES
&&
143 (*addr
= m_bigalloc(how
)) != NULL
)
145 else if (*size
> MBIGCLBYTES
&& *size
<= M16KCLBYTES
&&
146 (*addr
= m_16kalloc(how
)) != NULL
)
158 mbuf_freecluster(caddr_t addr
, size_t size
)
160 if (size
!= MCLBYTES
&& size
!= MBIGCLBYTES
&& size
!= M16KCLBYTES
)
161 panic("%s: invalid size (%ld) for cluster %p", __func__
,
164 if (size
== MCLBYTES
)
166 else if (size
== MBIGCLBYTES
)
167 m_bigfree(addr
, MBIGCLBYTES
, NULL
);
169 m_16kfree(addr
, M16KCLBYTES
, NULL
);
171 panic("%s: freeing jumbo cluster to an empty pool", __func__
);
175 mbuf_getcluster(mbuf_how_t how
, mbuf_type_t type
, size_t size
, mbuf_t
* mbuf
)
177 /* Must set *mbuf to NULL in failure case */
184 *mbuf
= m_get(how
, type
);
190 * At the time this code was written, m_{mclget,mbigget,m16kget}
191 * would always return the same value that was passed in to it.
193 if (size
== MCLBYTES
) {
194 *mbuf
= m_mclget(*mbuf
, how
);
195 } else if (size
== MBIGCLBYTES
) {
196 *mbuf
= m_mbigget(*mbuf
, how
);
197 } else if (size
== M16KCLBYTES
) {
199 *mbuf
= m_m16kget(*mbuf
, how
);
201 /* Jumbo cluster pool not available? */
209 if (*mbuf
== NULL
|| ((*mbuf
)->m_flags
& M_EXT
) == 0)
212 if (created
&& error
!= 0) {
219 errno_t
mbuf_mclget(mbuf_how_t how
, mbuf_type_t type
, mbuf_t
*mbuf
)
221 /* Must set *mbuf to NULL in failure case */
224 if (mbuf
== NULL
) return EINVAL
;
226 error
= mbuf_get(how
, type
, mbuf
);
233 * At the time this code was written, m_mclget would always
234 * return the same value that was passed in to it.
236 *mbuf
= m_mclget(*mbuf
, how
);
238 if (created
&& ((*mbuf
)->m_flags
& M_EXT
) == 0) {
242 if (*mbuf
== NULL
|| ((*mbuf
)->m_flags
& M_EXT
) == 0)
248 errno_t
mbuf_getpacket(mbuf_how_t how
, mbuf_t
*mbuf
)
250 /* Must set *mbuf to NULL in failure case */
253 *mbuf
= m_getpacket_how(how
);
256 if (how
== MBUF_WAITOK
)
265 /* This function is used to provide m_free via symbol indirection, please avoid
266 * any change in behavior or remove the indirection in config/Unsupported*
268 mbuf_t
mbuf_free(mbuf_t mbuf
)
273 /* This function is used to provide m_freem via symbol indirection, please avoid
274 * any change in behavior or remove the indirection in config/Unsupported*
276 void mbuf_freem(mbuf_t mbuf
)
281 int mbuf_freem_list(mbuf_t mbuf
)
283 return m_freem_list(mbuf
);
286 size_t mbuf_leadingspace(const mbuf_t mbuf
)
288 return m_leadingspace(mbuf
);
291 /* This function is used to provide m_trailingspace via symbol indirection,
292 * please avoid any change in behavior or remove the indirection in
293 * config/Unsupported*
295 size_t mbuf_trailingspace(const mbuf_t mbuf
)
297 return m_trailingspace(mbuf
);
301 errno_t
mbuf_copym(const mbuf_t src
, size_t offset
, size_t len
,
302 mbuf_how_t how
, mbuf_t
*new_mbuf
)
304 /* Must set *mbuf to NULL in failure case */
305 *new_mbuf
= m_copym(src
, offset
, len
, how
);
307 return (*new_mbuf
== NULL
) ? ENOMEM
: 0;
310 errno_t
mbuf_dup(const mbuf_t src
, mbuf_how_t how
, mbuf_t
*new_mbuf
)
312 /* Must set *new_mbuf to NULL in failure case */
313 *new_mbuf
= m_dup(src
, how
);
315 return (*new_mbuf
== NULL
) ? ENOMEM
: 0;
318 errno_t
mbuf_prepend(mbuf_t
*orig
, size_t len
, mbuf_how_t how
)
320 /* Must set *orig to NULL in failure case */
321 *orig
= m_prepend_2(*orig
, len
, how
);
323 return (*orig
== NULL
) ? ENOMEM
: 0;
326 errno_t
mbuf_split(mbuf_t src
, size_t offset
,
327 mbuf_how_t how
, mbuf_t
*new_mbuf
)
329 /* Must set *new_mbuf to NULL in failure case */
330 *new_mbuf
= m_split(src
, offset
, how
);
332 return (*new_mbuf
== NULL
) ? ENOMEM
: 0;
335 errno_t
mbuf_pullup(mbuf_t
*mbuf
, size_t len
)
337 /* Must set *mbuf to NULL in failure case */
338 *mbuf
= m_pullup(*mbuf
, len
);
340 return (*mbuf
== NULL
) ? ENOMEM
: 0;
343 errno_t
mbuf_pulldown(mbuf_t src
, size_t *offset
, size_t len
, mbuf_t
*location
)
345 /* Must set *location to NULL in failure case */
347 *location
= m_pulldown(src
, *offset
, len
, &new_offset
);
348 *offset
= new_offset
;
350 return (*location
== NULL
) ? ENOMEM
: 0;
353 /* This function is used to provide m_adj via symbol indirection, please avoid
354 * any change in behavior or remove the indirection in config/Unsupported*
356 void mbuf_adj(mbuf_t mbuf
, int len
)
361 errno_t
mbuf_adjustlen(mbuf_t m
, int amount
)
363 /* Verify m_len will be valid after adding amount */
365 int used
= (size_t)mbuf_data(m
) - (size_t)mbuf_datastart(m
) +
368 if ((size_t)(amount
+ used
) > mbuf_maxlen(m
))
371 else if (-amount
> m
->m_len
) {
380 mbuf_concatenate(mbuf_t dst
, mbuf_t src
)
387 /* return dst as is in the current implementation */
390 errno_t
mbuf_copydata(const mbuf_t m0
, size_t off
, size_t len
, void* out_data
)
392 /* Copied m_copydata, added error handling (don't just panic) */
399 if (off
< (size_t)m
->m_len
)
407 count
= m
->m_len
- off
> len
? len
: m
->m_len
- off
;
408 bcopy(mtod(m
, caddr_t
) + off
, out_data
, count
);
410 out_data
= ((char*)out_data
) + count
;
418 int mbuf_mclhasreference(mbuf_t mbuf
)
420 if ((mbuf
->m_flags
& M_EXT
))
421 return m_mclhasreference(mbuf
);
428 mbuf_t
mbuf_next(const mbuf_t mbuf
)
433 errno_t
mbuf_setnext(mbuf_t mbuf
, mbuf_t next
)
435 if (next
&& ((next
)->m_nextpkt
!= NULL
||
436 (next
)->m_type
== MT_FREE
)) return EINVAL
;
442 mbuf_t
mbuf_nextpkt(const mbuf_t mbuf
)
444 return mbuf
->m_nextpkt
;
447 void mbuf_setnextpkt(mbuf_t mbuf
, mbuf_t nextpkt
)
449 mbuf
->m_nextpkt
= nextpkt
;
452 size_t mbuf_len(const mbuf_t mbuf
)
457 void mbuf_setlen(mbuf_t mbuf
, size_t len
)
462 size_t mbuf_maxlen(const mbuf_t mbuf
)
464 if (mbuf
->m_flags
& M_EXT
)
465 return mbuf
->m_ext
.ext_size
;
466 return &mbuf
->m_dat
[MLEN
] - ((char*)mbuf_datastart(mbuf
));
469 mbuf_type_t
mbuf_type(const mbuf_t mbuf
)
474 errno_t
mbuf_settype(mbuf_t mbuf
, mbuf_type_t new_type
)
476 if (new_type
== MBUF_TYPE_FREE
) return EINVAL
;
478 m_mchtype(mbuf
, new_type
);
483 mbuf_flags_t
mbuf_flags(const mbuf_t mbuf
)
485 return mbuf
->m_flags
& mbuf_flags_mask
;
488 errno_t
mbuf_setflags(mbuf_t mbuf
, mbuf_flags_t flags
)
490 if ((flags
& ~mbuf_flags_mask
) != 0) return EINVAL
;
491 mbuf
->m_flags
= flags
|
492 (mbuf
->m_flags
& ~mbuf_flags_mask
);
497 errno_t
mbuf_setflags_mask(mbuf_t mbuf
, mbuf_flags_t flags
, mbuf_flags_t mask
)
499 if (((flags
| mask
) & ~mbuf_flags_mask
) != 0) return EINVAL
;
501 mbuf
->m_flags
= (flags
& mask
) | (mbuf
->m_flags
& ~mask
);
506 errno_t
mbuf_copy_pkthdr(mbuf_t dest
, const mbuf_t src
)
508 if (((src
)->m_flags
& M_PKTHDR
) == 0)
511 m_copy_pkthdr(dest
, src
);
516 size_t mbuf_pkthdr_len(const mbuf_t mbuf
)
518 return mbuf
->m_pkthdr
.len
;
521 void mbuf_pkthdr_setlen(mbuf_t mbuf
, size_t len
)
523 mbuf
->m_pkthdr
.len
= len
;
526 void mbuf_pkthdr_adjustlen(mbuf_t mbuf
, int amount
)
528 mbuf
->m_pkthdr
.len
+= amount
;
531 ifnet_t
mbuf_pkthdr_rcvif(const mbuf_t mbuf
)
533 // If we reference count ifnets, we should take a reference here before returning
534 return mbuf
->m_pkthdr
.rcvif
;
537 errno_t
mbuf_pkthdr_setrcvif(mbuf_t mbuf
, ifnet_t ifnet
)
539 /* May want to walk ifnet list to determine if interface is valid */
540 mbuf
->m_pkthdr
.rcvif
= (struct ifnet
*)ifnet
;
544 void* mbuf_pkthdr_header(const mbuf_t mbuf
)
546 return mbuf
->m_pkthdr
.header
;
549 void mbuf_pkthdr_setheader(mbuf_t mbuf
, void *header
)
551 mbuf
->m_pkthdr
.header
= (void*)header
;
555 mbuf_inbound_modified(mbuf_t mbuf
)
557 /* Invalidate hardware generated checksum flags */
558 mbuf
->m_pkthdr
.csum_flags
= 0;
561 extern void in_cksum_offset(struct mbuf
* m
, size_t ip_offset
);
562 extern void in_delayed_cksum_offset(struct mbuf
*m
, int ip_offset
);
565 mbuf_outbound_finalize(mbuf_t mbuf
, u_int32_t protocol_family
, size_t protocol_offset
)
567 if ((mbuf
->m_pkthdr
.csum_flags
&
568 (CSUM_DELAY_DATA
| CSUM_DELAY_IP
| CSUM_TCP_SUM16
| CSUM_DELAY_IPV6_DATA
)) == 0)
571 /* Generate the packet in software, client needs it */
572 switch (protocol_family
) {
574 if (mbuf
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) {
576 * If you're wondering where this lovely code comes
577 * from, we're trying to undo what happens in ip_output.
578 * Look for CSUM_TCP_SUM16 in ip_output.
580 u_int16_t first
, second
;
581 mbuf
->m_pkthdr
.csum_flags
&= ~CSUM_TCP_SUM16
;
582 mbuf
->m_pkthdr
.csum_flags
|= CSUM_TCP
;
583 first
= mbuf
->m_pkthdr
.csum_data
>> 16;
584 second
= mbuf
->m_pkthdr
.csum_data
& 0xffff;
585 mbuf
->m_pkthdr
.csum_data
= first
- second
;
587 if (mbuf
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
588 in_delayed_cksum_offset(mbuf
, protocol_offset
);
591 if (mbuf
->m_pkthdr
.csum_flags
& CSUM_DELAY_IP
) {
592 in_cksum_offset(mbuf
, protocol_offset
);
595 mbuf
->m_pkthdr
.csum_flags
&= ~(CSUM_DELAY_DATA
| CSUM_DELAY_IP
);
600 if (mbuf
->m_pkthdr
.csum_flags
& CSUM_DELAY_IPV6_DATA
) {
601 in_delayed_cksum_offset(mbuf
, protocol_offset
);
603 mbuf
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_IPV6_DATA
;
609 * Not sure what to do here if anything.
610 * Hardware checksum code looked pretty IPv4/IPv6 specific.
612 if ((mbuf
->m_pkthdr
.csum_flags
& (CSUM_DELAY_DATA
| CSUM_DELAY_IP
| CSUM_DELAY_IPV6_DATA
)) != 0)
613 panic("mbuf_outbound_finalize - CSUM flags set for non-IPv4 or IPv6 packet (%u)!\n", protocol_family
);
622 mbuf
->m_pkthdr
.csum_flags
|= CSUM_VLAN_TAG_VALID
;
623 mbuf
->m_pkthdr
.vlan_tag
= vlan
;
633 if ((mbuf
->m_pkthdr
.csum_flags
& CSUM_VLAN_TAG_VALID
) == 0)
634 return ENXIO
; // No vlan tag set
636 *vlan
= mbuf
->m_pkthdr
.vlan_tag
;
645 mbuf
->m_pkthdr
.csum_flags
&= ~CSUM_VLAN_TAG_VALID
;
646 mbuf
->m_pkthdr
.vlan_tag
= 0;
651 static const mbuf_csum_request_flags_t mbuf_valid_csum_request_flags
=
652 MBUF_CSUM_REQ_IP
| MBUF_CSUM_REQ_TCP
| MBUF_CSUM_REQ_UDP
|
653 MBUF_CSUM_REQ_SUM16
| MBUF_CSUM_REQ_TCPIPV6
| MBUF_CSUM_REQ_UDPIPV6
;
656 mbuf_set_csum_requested(
658 mbuf_csum_request_flags_t request
,
661 request
&= mbuf_valid_csum_request_flags
;
662 mbuf
->m_pkthdr
.csum_flags
= (mbuf
->m_pkthdr
.csum_flags
& 0xffff0000) | request
;
663 mbuf
->m_pkthdr
.csum_data
= value
;
668 static const mbuf_tso_request_flags_t mbuf_valid_tso_request_flags
=
669 MBUF_TSO_IPV4
| MBUF_TSO_IPV6
;
672 mbuf_get_tso_requested(
674 mbuf_tso_request_flags_t
*request
,
677 if (mbuf
== NULL
|| (mbuf
->m_flags
& M_PKTHDR
) == 0 ||
678 request
== NULL
|| value
== NULL
)
681 *request
= mbuf
->m_pkthdr
.csum_flags
;
682 *request
&= mbuf_valid_tso_request_flags
;
683 if (*request
&& value
!= NULL
)
684 *value
= mbuf
->m_pkthdr
.tso_segsz
;
690 mbuf_get_csum_requested(
692 mbuf_csum_request_flags_t
*request
,
695 *request
= mbuf
->m_pkthdr
.csum_flags
;
696 *request
&= mbuf_valid_csum_request_flags
;
698 *value
= mbuf
->m_pkthdr
.csum_data
;
705 mbuf_clear_csum_requested(
708 mbuf
->m_pkthdr
.csum_flags
&= 0xffff0000;
709 mbuf
->m_pkthdr
.csum_data
= 0;
714 static const mbuf_csum_performed_flags_t mbuf_valid_csum_performed_flags
=
715 MBUF_CSUM_DID_IP
| MBUF_CSUM_IP_GOOD
| MBUF_CSUM_DID_DATA
|
716 MBUF_CSUM_PSEUDO_HDR
| MBUF_CSUM_TCP_SUM16
;
719 mbuf_set_csum_performed(
721 mbuf_csum_performed_flags_t performed
,
724 performed
&= mbuf_valid_csum_performed_flags
;
725 mbuf
->m_pkthdr
.csum_flags
= (mbuf
->m_pkthdr
.csum_flags
& 0xffff0000) | performed
;
726 mbuf
->m_pkthdr
.csum_data
= value
;
732 mbuf_get_csum_performed(
734 mbuf_csum_performed_flags_t
*performed
,
737 *performed
= mbuf
->m_pkthdr
.csum_flags
& mbuf_valid_csum_performed_flags
;
738 *value
= mbuf
->m_pkthdr
.csum_data
;
744 mbuf_clear_csum_performed(
747 mbuf
->m_pkthdr
.csum_flags
&= 0xffff0000;
748 mbuf
->m_pkthdr
.csum_data
= 0;
754 mbuf_inet_cksum(mbuf_t mbuf
, int protocol
, u_int32_t offset
, u_int32_t length
,
757 if (mbuf
== NULL
|| length
== 0 || csum
== NULL
||
758 (u_int32_t
)mbuf
->m_pkthdr
.len
< (offset
+ length
))
761 *csum
= inet_cksum(mbuf
, protocol
, offset
, length
);
767 mbuf_inet6_cksum(mbuf_t mbuf
, int protocol
, u_int32_t offset
, u_int32_t length
,
770 if (mbuf
== NULL
|| length
== 0 || csum
== NULL
||
771 (u_int32_t
)mbuf
->m_pkthdr
.len
< (offset
+ length
))
774 *csum
= inet6_cksum(mbuf
, protocol
, offset
, length
);
779 mbuf_inet6_cksum(__unused mbuf_t mbuf
, __unused
int protocol
,
780 __unused u_int32_t offset
, __unused u_int32_t length
,
781 __unused u_int16_t
*csum
)
783 panic("mbuf_inet6_cksum() doesn't exist on this platform\n");
788 inet6_cksum(__unused
struct mbuf
*m
, __unused
unsigned int nxt
,
789 __unused
unsigned int off
, __unused
unsigned int len
)
791 panic("inet6_cksum() doesn't exist on this platform\n");
795 void nd6_lookup_ipv6(void);
797 nd6_lookup_ipv6(void)
799 panic("nd6_lookup_ipv6() doesn't exist on this platform\n");
803 in6addr_local(__unused
struct in6_addr
*a
)
805 panic("in6addr_local() doesn't exist on this platform\n");
809 void nd6_storelladdr(void);
811 nd6_storelladdr(void)
813 panic("nd6_storelladdr() doesn't exist on this platform\n");
821 #define MTAG_FIRST_ID FIRST_KPI_STR_ID
826 mbuf_tag_id_t
*out_id
)
828 return net_str_id_find_internal(string
, out_id
, NSI_MBUF_TAG
, 1);
835 mbuf_tag_type_t type
,
841 u_int32_t mtag_id_first
, mtag_id_last
;
846 /* Sanity check parameters */
847 (void) net_str_id_first_last(&mtag_id_first
, &mtag_id_last
, NSI_MBUF_TAG
);
848 if (mbuf
== NULL
|| (mbuf
->m_flags
& M_PKTHDR
) == 0 || id
< mtag_id_first
||
849 id
> mtag_id_last
|| length
< 1 || (length
& 0xffff0000) != 0 ||
854 /* Make sure this mtag hasn't already been allocated */
855 tag
= m_tag_locate(mbuf
, id
, type
, NULL
);
860 /* Allocate an mtag */
861 tag
= m_tag_create(id
, type
, length
, how
, mbuf
);
863 return how
== M_WAITOK
? ENOMEM
: EWOULDBLOCK
;
866 /* Attach the mtag and set *data_p */
867 m_tag_prepend(mbuf
, tag
);
877 mbuf_tag_type_t type
,
882 u_int32_t mtag_id_first
, mtag_id_last
;
889 /* Sanity check parameters */
890 (void) net_str_id_first_last(&mtag_id_first
, &mtag_id_last
, NSI_MBUF_TAG
);
891 if (mbuf
== NULL
|| (mbuf
->m_flags
& M_PKTHDR
) == 0 || id
< mtag_id_first
||
892 id
> mtag_id_last
|| length
== NULL
|| data_p
== NULL
) {
897 tag
= m_tag_locate(mbuf
, id
, type
, NULL
);
902 /* Copy out the pointer to the data and the lenght value */
903 *length
= tag
->m_tag_len
;
913 mbuf_tag_type_t type
)
916 u_int32_t mtag_id_first
, mtag_id_last
;
918 /* Sanity check parameters */
919 (void) net_str_id_first_last(&mtag_id_first
, &mtag_id_last
, NSI_MBUF_TAG
);
920 if (mbuf
== NULL
|| (mbuf
->m_flags
& M_PKTHDR
) == 0 || id
< mtag_id_first
||
924 tag
= m_tag_locate(mbuf
, id
, type
, NULL
);
929 m_tag_delete(mbuf
, tag
);
934 void mbuf_stats(struct mbuf_stat
*stats
)
936 stats
->mbufs
= mbstat
.m_mbufs
;
937 stats
->clusters
= mbstat
.m_clusters
;
938 stats
->clfree
= mbstat
.m_clfree
;
939 stats
->drops
= mbstat
.m_drops
;
940 stats
->wait
= mbstat
.m_wait
;
941 stats
->drain
= mbstat
.m_drain
;
942 __builtin_memcpy(stats
->mtypes
, mbstat
.m_mtypes
, sizeof(stats
->mtypes
));
943 stats
->mcfail
= mbstat
.m_mcfail
;
944 stats
->mpfail
= mbstat
.m_mpfail
;
945 stats
->msize
= mbstat
.m_msize
;
946 stats
->mclbytes
= mbstat
.m_mclbytes
;
947 stats
->minclsize
= mbstat
.m_minclsize
;
948 stats
->mlen
= mbstat
.m_mlen
;
949 stats
->mhlen
= mbstat
.m_mhlen
;
950 stats
->bigclusters
= mbstat
.m_bigclusters
;
951 stats
->bigclfree
= mbstat
.m_bigclfree
;
952 stats
->bigmclbytes
= mbstat
.m_bigmclbytes
;
956 mbuf_allocpacket(mbuf_how_t how
, size_t packetlen
, unsigned int *maxchunks
, mbuf_t
*mbuf
)
960 unsigned int numpkts
= 1;
961 unsigned int numchunks
= maxchunks
? *maxchunks
: 0;
963 if (packetlen
== 0) {
967 m
= m_allocpacket_internal(&numpkts
, packetlen
, maxchunks
? &numchunks
: NULL
, how
, 1, 0);
969 if (maxchunks
&& *maxchunks
&& numchunks
> *maxchunks
)
975 *maxchunks
= numchunks
;
984 mbuf_allocpacket_list(unsigned int numpkts
, mbuf_how_t how
, size_t packetlen
, unsigned int *maxchunks
, mbuf_t
*mbuf
)
988 unsigned int numchunks
= maxchunks
? *maxchunks
: 0;
994 if (packetlen
== 0) {
998 m
= m_allocpacket_internal(&numpkts
, packetlen
, maxchunks
? &numchunks
: NULL
, how
, 1, 0);
1000 if (maxchunks
&& *maxchunks
&& numchunks
> *maxchunks
)
1006 *maxchunks
= numchunks
;
1017 * mbuf_copyback differs from m_copyback in a few ways:
1018 * 1) mbuf_copyback will allocate clusters for new mbufs we append
1019 * 2) mbuf_copyback will grow the last mbuf in the chain if possible
1020 * 3) mbuf_copyback reports whether or not the operation succeeded
1021 * 4) mbuf_copyback allows the caller to specify M_WAITOK or M_NOWAIT
1036 const char *cp
= data
;
1038 if (m
== NULL
|| len
== 0 || data
== NULL
)
1041 while (off
> (mlen
= m
->m_len
)) {
1044 if (m
->m_next
== 0) {
1045 n
= m_getclr(how
, m
->m_type
);
1050 n
->m_len
= MIN(MLEN
, len
+ off
);
1057 mlen
= MIN(m
->m_len
- off
, len
);
1058 if (mlen
< len
&& m
->m_next
== NULL
&& mbuf_trailingspace(m
) > 0) {
1059 size_t grow
= MIN(mbuf_trailingspace(m
), len
- mlen
);
1063 bcopy(cp
, off
+ (char*)mbuf_data(m
), (unsigned)mlen
);
1071 if (m
->m_next
== 0) {
1072 n
= m_get(how
, m
->m_type
);
1077 if (len
> MINCLSIZE
) {
1078 /* cluter allocation failure is okay, we can grow chain */
1079 mbuf_mclget(how
, m
->m_type
, &n
);
1081 n
->m_len
= MIN(mbuf_maxlen(n
), len
);
1088 if ((m_start
->m_flags
& M_PKTHDR
) && (m_start
->m_pkthdr
.len
< totlen
))
1089 m_start
->m_pkthdr
.len
= totlen
;
1101 mbuf_get_mhlen(void)
1107 mbuf_get_minclsize(void)
1109 return (MHLEN
+ MLEN
);
1112 mbuf_traffic_class_t
1113 mbuf_get_traffic_class(mbuf_t m
)
1115 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
))
1116 return (MBUF_TC_BE
);
1118 return (m_get_traffic_class(m
));
1122 mbuf_set_traffic_class(mbuf_t m
, mbuf_traffic_class_t tc
)
1124 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
) ||
1125 ((u_int32_t
)tc
>= MBUF_TC_MAX
))
1128 return (m_set_traffic_class(m
, tc
));
1132 mbuf_is_traffic_class_privileged(mbuf_t m
)
1134 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
) ||
1135 !MBUF_VALID_SC(m
->m_pkthdr
.svc
))
1138 return (m
->m_pkthdr
.aux_flags
& MAUXF_PRIO_PRIVILEGED
);
1142 mbuf_get_service_class(mbuf_t m
)
1144 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
))
1145 return (MBUF_SC_BE
);
1147 return (m_get_service_class(m
));
1151 mbuf_set_service_class(mbuf_t m
, mbuf_svc_class_t sc
)
1153 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
))
1156 return (m_set_service_class(m
, sc
));
1160 mbuf_pkthdr_aux_flags(mbuf_t m
, mbuf_pkthdr_aux_flags_t
*flagsp
)
1163 if (m
== NULL
|| !(m
->m_flags
& M_PKTHDR
) || flagsp
== NULL
)
1166 flags
= m
->m_pkthdr
.aux_flags
& MBUF_PKTAUXF_MASK
;
1168 /* These 2 flags are mutually exclusive */
1170 (MBUF_PKTAUXF_INET_RESOLVE_RTR
| MBUF_PKTAUXF_INET6_RESOLVE_RTR
)) !=
1171 (MBUF_PKTAUXF_INET_RESOLVE_RTR
| MBUF_PKTAUXF_INET6_RESOLVE_RTR
));