]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_mbuf2.c
2 * Copyright (c) 2000-2018 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@
28 /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */
31 * Copyright (C) 1999 WIDE Project.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. Neither the name of the project nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * Copyright (c) 1982, 1986, 1988, 1991, 1993
61 * The Regents of the University of California. All rights reserved.
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
71 * 3. All advertising materials mentioning features or use of this software
72 * must display the following acknowledgement:
73 * This product includes software developed by the University of
74 * California, Berkeley and its contributors.
75 * 4. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
94 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
95 * support for mandatory and extensible security protections. This notice
96 * is included in support of clause 2.2 (b) of the Apple Public License,
101 /*#define PULLDOWN_DEBUG*/
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/proc_internal.h>
106 #include <sys/malloc.h>
107 #include <sys/mbuf.h>
108 #include <sys/mcache.h>
109 #include <netinet/in.h>
110 #include <netinet/ip_var.h>
112 #include <netinet/ip6.h>
113 #include <netinet6/ip6_var.h>
117 #include <security/mac_framework.h>
121 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
122 * packet chain before "off" is kept untouched.
123 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
124 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
126 * on error return (NULL return value), original "m" will be freed.
128 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
131 m_pulldown(struct mbuf
*m
, int off
, int len
, int *offp
)
133 struct mbuf
*n
= NULL
, *o
= NULL
;
134 int hlen
= 0, tlen
= 0, olen
= 0;
135 int sharedcluster
= 0;
136 #if defined(PULLDOWN_STAT) && INET6
137 static struct mbuf
*prev
= NULL
;
138 int prevlen
= 0, prevmlen
= 0;
141 /* check invalid arguments. */
143 panic("m == NULL in m_pulldown()");
144 if (len
> MCLBYTES
) {
146 return NULL
; /* impossible */
149 #if defined(PULLDOWN_STAT) && INET6
150 ip6stat
.ip6s_pulldown
++;
153 #if defined(PULLDOWN_STAT) && INET6
154 /* statistics for m_pullup */
155 ip6stat
.ip6s_pullup
++;
156 if (off
+ len
> MHLEN
)
157 ip6stat
.ip6s_pullup_fail
++;
161 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
162 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
164 if (dlen
>= off
+ len
)
165 ip6stat
.ip6s_pullup
--; /* call will not be made! */
166 else if ((m
->m_flags
& M_EXT
) != 0) {
167 ip6stat
.ip6s_pullup_alloc
++;
168 ip6stat
.ip6s_pullup_copy
++;
170 if (mlen
>= off
+ len
)
171 ip6stat
.ip6s_pullup_copy
++;
173 ip6stat
.ip6s_pullup_alloc
++;
174 ip6stat
.ip6s_pullup_copy
++;
182 /* statistics for m_pullup2 */
183 ip6stat
.ip6s_pullup2
++;
184 if (off
+ len
> MCLBYTES
)
185 ip6stat
.ip6s_pullup2_fail
++;
189 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
190 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
194 if (dlen
>= off
+ len
)
195 ip6stat
.ip6s_pullup2
--; /* call will not be made! */
196 else if ((m
->m_flags
& M_EXT
) != 0) {
197 ip6stat
.ip6s_pullup2_alloc
++;
198 ip6stat
.ip6s_pullup2_copy
++;
199 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
: MHLEN
;
201 if (mlen
>= off
+ len
)
202 ip6stat
.ip6s_pullup2_copy
++;
204 ip6stat
.ip6s_pullup2_alloc
++;
205 ip6stat
.ip6s_pullup2_copy
++;
206 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
215 #ifdef PULLDOWN_DEBUG
219 for (t
= m
; t
; t
= t
->m_next
)
220 printf(" %d", t
->m_len
);
227 * Iterate and make n point to the mbuf
228 * within which the first byte at length
229 * offset is contained from the start of
232 while (n
!= NULL
&& off
> 0) {
239 /* be sure to point non-empty mbuf */
240 while (n
!= NULL
&& n
->m_len
== 0)
245 return NULL
; /* mbuf chain too short */
249 * the target data is on <n, off>.
250 * if we got enough data on the mbuf "n", we're done.
252 * It should be noted, that we should only do this either
253 * when offset is 0, i.e. data is pointing to the start
254 * or when the caller specifies an out argument to get
255 * the offset value in the mbuf to work with data pointer
258 * If offset is not 0 and caller did not provide out-argument
259 * to get offset, we should split the mbuf even when the length
260 * is contained in current mbuf.
262 if ((off
== 0 || offp
) && len
<= n
->m_len
- off
)
265 #if defined(PULLDOWN_STAT) && INET6
266 ip6stat
.ip6s_pulldown_copy
++;
270 * when len <= n->m_len - off and off != 0, it is a special case.
271 * len bytes from <n, off> sits in single mbuf, but the caller does
272 * not like the starting position (off).
273 * chop the current mbuf into two pieces, set off to 0.
275 if (len
<= n
->m_len
- off
) {
276 o
= m_copym(n
, off
, n
->m_len
- off
, M_DONTWAIT
);
279 return NULL
; /* ENOBUFS */
282 o
->m_next
= n
->m_next
;
290 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
291 * and construct contiguous mbuf with m_len == len.
292 * note that hlen + tlen == len, and tlen > 0.
294 * Read these variables as head length and tail length
296 hlen
= n
->m_len
- off
;
300 * ensure that we have enough trailing data on mbuf chain.
301 * if not, we can do nothing about the chain.
304 for (o
= n
->m_next
; o
!= NULL
; o
= o
->m_next
)
306 if (hlen
+ olen
< len
) {
308 return NULL
; /* mbuf chain too short */
313 * we need to use m_copydata() to get data from <n->m_next, 0>.
315 if ((n
->m_flags
& M_EXT
) == 0)
318 if (m_get_ext_free(n
) != NULL
)
320 else if (m_mclhasreference(n
))
327 * If we have enough space left in current mbuf to accomodate
328 * tail length, copy tail length worth of data starting with next mbuf
329 * and adjust the length of next one accordingly.
331 if ((off
== 0 || offp
) && M_TRAILINGSPACE(n
) >= tlen
333 m_copydata(n
->m_next
, 0, tlen
, mtod(n
, caddr_t
) + n
->m_len
);
335 m_adj(n
->m_next
, tlen
);
340 * If have enough leading space in next mbuf to accomodate head length
341 * of current mbuf, and total resulting length of next mbuf is greater
342 * than or equal to requested len bytes, then just copy hlen from
343 * current to the next one and adjust sizes accordingly.
345 if ((off
== 0 || offp
) && M_LEADINGSPACE(n
->m_next
) >= hlen
&&
346 (n
->m_next
->m_len
+ hlen
) >= len
&& !sharedcluster
) {
347 n
->m_next
->m_data
-= hlen
;
348 n
->m_next
->m_len
+= hlen
;
349 bcopy(mtod(n
, caddr_t
) + off
, mtod(n
->m_next
, caddr_t
), hlen
);
357 * now, we need to do the hard way. don't m_copy as there's no room
360 #if defined(PULLDOWN_STAT) && INET6
361 ip6stat
.ip6s_pulldown_alloc
++;
363 MGET(o
, M_DONTWAIT
, m
->m_type
);
366 return NULL
; /* ENOBUFS */
368 if (len
> MHLEN
) { /* use MHLEN just for safety */
369 MCLGET(o
, M_DONTWAIT
);
370 if ((o
->m_flags
& M_EXT
) == 0) {
373 return NULL
; /* ENOBUFS */
376 /* get hlen from <n, off> into <o, 0> */
378 bcopy(mtod(n
, caddr_t
) + off
, mtod(o
, caddr_t
), hlen
);
380 /* get tlen from <n->m_next, 0> into <o, hlen> */
381 m_copydata(n
->m_next
, 0, tlen
, mtod(o
, caddr_t
) + o
->m_len
);
383 m_adj(n
->m_next
, tlen
);
384 o
->m_next
= n
->m_next
;
390 #ifdef PULLDOWN_DEBUG
394 for (t
= m
; t
; t
= t
->m_next
)
395 printf("%c%d", t
== n
? '*' : ' ', t
->m_len
);
396 printf(" (off=%d)\n", off
);
405 * Create and return an m_tag, either by re-using space in a previous tag
406 * or by allocating a new mbuf/cluster
409 m_tag_create(u_int32_t id
, u_int16_t type
, int len
, int wait
, struct mbuf
*buf
)
411 struct m_tag
*t
= NULL
;
417 if (len
+ sizeof (struct m_tag
) + sizeof (struct m_taghdr
) > MLEN
)
418 return (m_tag_alloc(id
, type
, len
, wait
));
421 * We've exhausted all external cases. Now, go through the m_tag
422 * chain and see if we can fit it in any of them.
423 * If not (t == NULL), call m_tag_alloc to store it in a new mbuf.
425 p
= SLIST_FIRST(&buf
->m_pkthdr
.tags
);
428 if (M_TAG_ALIGN(p
->m_tag_len
) +
429 sizeof (struct m_taghdr
) > MLEN
) {
430 p
= SLIST_NEXT(p
, m_tag_link
);
434 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
436 struct mbuf
*m
= m_dtom(p
);
437 struct m_taghdr
*hdr
= (struct m_taghdr
*)(void *)m
->m_data
;
439 VERIFY(IS_P2ALIGNED(hdr
+ 1, sizeof (u_int64_t
)));
440 VERIFY(m
->m_flags
& M_TAGHDR
&& !(m
->m_flags
& M_EXT
));
442 /* The mbuf can store this m_tag */
443 if (M_TAG_ALIGN(len
) <= MLEN
- m
->m_len
) {
444 t
= (struct m_tag
*)(void *)(m
->m_data
+ m
->m_len
);
445 VERIFY(IS_P2ALIGNED(t
, sizeof (u_int64_t
)));
447 m
->m_len
+= M_TAG_ALIGN(len
);
448 VERIFY(m
->m_len
<= MLEN
);
452 p
= SLIST_NEXT(p
, m_tag_link
);
456 return (m_tag_alloc(id
, type
, len
, wait
));
458 t
->m_tag_cookie
= M_TAG_VALID_PATTERN
;
459 t
->m_tag_type
= type
;
467 /* Get a packet tag structure along with specified data following. */
469 m_tag_alloc(u_int32_t id
, u_int16_t type
, int len
, int wait
)
476 if (M_TAG_ALIGN(len
) + sizeof (struct m_taghdr
) <= MLEN
) {
477 struct mbuf
*m
= m_get(wait
, MT_TAG
);
478 struct m_taghdr
*hdr
;
483 m
->m_flags
|= M_TAGHDR
;
485 hdr
= (struct m_taghdr
*)(void *)m
->m_data
;
486 VERIFY(IS_P2ALIGNED(hdr
+ 1, sizeof (u_int64_t
)));
488 m
->m_len
+= sizeof (struct m_taghdr
);
489 t
= (struct m_tag
*)(void *)(m
->m_data
+ m
->m_len
);
490 VERIFY(IS_P2ALIGNED(t
, sizeof (u_int64_t
)));
491 m
->m_len
+= M_TAG_ALIGN(len
);
492 VERIFY(m
->m_len
<= MLEN
);
493 } else if (len
+ sizeof (struct m_tag
) <= MCLBYTES
) {
494 t
= (struct m_tag
*)(void *)m_mclalloc(wait
);
502 VERIFY(IS_P2ALIGNED(t
, sizeof (u_int64_t
)));
503 t
->m_tag_cookie
= M_TAG_VALID_PATTERN
;
504 t
->m_tag_type
= type
;
513 /* Free a packet tag. */
515 m_tag_free(struct m_tag
*t
)
519 t
->m_tag_id
== KERNEL_MODULE_TAG_ID
&&
520 t
->m_tag_type
== KERNEL_TAG_TYPE_MACLABEL
)
521 mac_mbuf_tag_destroy(t
);
526 VERIFY(t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
528 if (M_TAG_ALIGN(t
->m_tag_len
) + sizeof (struct m_taghdr
) <= MLEN
) {
529 struct mbuf
* m
= m_dtom(t
);
530 VERIFY(m
->m_flags
& M_TAGHDR
);
531 struct m_taghdr
*hdr
= (struct m_taghdr
*)(void *)m
->m_data
;
533 VERIFY(IS_P2ALIGNED(hdr
+ 1, sizeof (u_int64_t
)));
535 /* No other tags in this mbuf */
536 if(--hdr
->refcnt
== 0) {
541 /* Pattern-fill the header */
542 u_int64_t
*fill_ptr
= (u_int64_t
*)t
;
543 u_int64_t
*end_ptr
= (u_int64_t
*)(t
+ 1);
544 while (fill_ptr
< end_ptr
) {
545 *fill_ptr
= M_TAG_FREE_PATTERN
;
549 m_mclfree((caddr_t
)t
);
553 /* Prepend a packet tag. */
555 m_tag_prepend(struct mbuf
*m
, struct m_tag
*t
)
557 VERIFY(m
!= NULL
&& t
!= NULL
);
559 SLIST_INSERT_HEAD(&m
->m_pkthdr
.tags
, t
, m_tag_link
);
562 /* Unlink a packet tag. */
564 m_tag_unlink(struct mbuf
*m
, struct m_tag
*t
)
566 VERIFY(m
->m_flags
& M_PKTHDR
);
567 VERIFY(t
!= NULL
&& t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
569 SLIST_REMOVE(&m
->m_pkthdr
.tags
, t
, m_tag
, m_tag_link
);
572 /* Unlink and free a packet tag. */
574 m_tag_delete(struct mbuf
*m
, struct m_tag
*t
)
580 /* Unlink and free a packet tag chain, starting from given tag. */
582 m_tag_delete_chain(struct mbuf
*m
, struct m_tag
*t
)
586 VERIFY(m
->m_flags
& M_PKTHDR
);
591 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
596 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
597 while ((q
= SLIST_NEXT(p
, m_tag_link
)) != NULL
) {
598 VERIFY(q
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
604 /* Find a tag, starting from a given position. */
606 m_tag_locate(struct mbuf
*m
, u_int32_t id
, u_int16_t type
, struct m_tag
*t
)
610 VERIFY(m
->m_flags
& M_PKTHDR
);
613 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
615 VERIFY(t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
616 p
= SLIST_NEXT(t
, m_tag_link
);
619 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
620 if (p
->m_tag_id
== id
&& p
->m_tag_type
== type
)
622 p
= SLIST_NEXT(p
, m_tag_link
);
627 /* Copy a single tag. */
629 m_tag_copy(struct m_tag
*t
, int how
)
635 p
= m_tag_alloc(t
->m_tag_id
, t
->m_tag_type
, t
->m_tag_len
, how
);
640 * XXXMAC: we should probably pass off the initialization, and
641 * copying here? can we hid that KERNEL_TAG_TYPE_MACLABEL is
642 * special from the mbuf code?
645 t
->m_tag_id
== KERNEL_MODULE_TAG_ID
&&
646 t
->m_tag_type
== KERNEL_TAG_TYPE_MACLABEL
) {
647 if (mac_mbuf_tag_init(p
, how
) != 0) {
651 mac_mbuf_tag_copy(t
, p
);
654 bcopy(t
+ 1, p
+ 1, t
->m_tag_len
); /* Copy the data */
659 * Copy two tag chains. The destination mbuf (to) loses any attached
660 * tags even if the operation fails. This should not be a problem, as
661 * m_tag_copy_chain() is typically called with a newly-allocated
665 m_tag_copy_chain(struct mbuf
*to
, struct mbuf
*from
, int how
)
667 struct m_tag
*p
, *t
, *tprev
= NULL
;
669 VERIFY((to
->m_flags
& M_PKTHDR
) && (from
->m_flags
& M_PKTHDR
));
671 m_tag_delete_chain(to
, NULL
);
672 SLIST_FOREACH(p
, &from
->m_pkthdr
.tags
, m_tag_link
) {
673 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
674 t
= m_tag_copy(p
, how
);
676 m_tag_delete_chain(to
, NULL
);
680 SLIST_INSERT_HEAD(&to
->m_pkthdr
.tags
, t
, m_tag_link
);
682 SLIST_INSERT_AFTER(tprev
, t
, m_tag_link
);
689 /* Initialize dynamic and static tags on an mbuf. */
691 m_tag_init(struct mbuf
*m
, int all
)
693 VERIFY(m
->m_flags
& M_PKTHDR
);
695 SLIST_INIT(&m
->m_pkthdr
.tags
);
697 * If the caller wants to preserve static mbuf tags
698 * (e.g. m_dup_pkthdr), don't zero them out.
701 bzero(&m
->m_pkthdr
.builtin_mtag
._net_mtag
,
702 sizeof (m
->m_pkthdr
.builtin_mtag
._net_mtag
));
706 /* Get first tag in chain. */
708 m_tag_first(struct mbuf
*m
)
710 VERIFY(m
->m_flags
& M_PKTHDR
);
712 return (SLIST_FIRST(&m
->m_pkthdr
.tags
));
715 /* Get next tag in chain. */
717 m_tag_next(struct mbuf
*m
, struct m_tag
*t
)
721 VERIFY(t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
723 return (SLIST_NEXT(t
, m_tag_link
));
727 m_set_traffic_class(struct mbuf
*m
, mbuf_traffic_class_t tc
)
729 u_int32_t val
= MBUF_TC2SCVAL(tc
); /* just the val portion */
731 return (m_set_service_class(m
, m_service_class_from_val(val
)));
735 m_get_traffic_class(struct mbuf
*m
)
737 return (MBUF_SC2TC(m_get_service_class(m
)));
741 m_set_service_class(struct mbuf
*m
, mbuf_svc_class_t sc
)
745 VERIFY(m
->m_flags
& M_PKTHDR
);
747 if (MBUF_VALID_SC(sc
))
748 m
->m_pkthdr
.pkt_svc
= sc
;
756 m_get_service_class(struct mbuf
*m
)
760 VERIFY(m
->m_flags
& M_PKTHDR
);
762 if (MBUF_VALID_SC(m
->m_pkthdr
.pkt_svc
))
763 sc
= m
->m_pkthdr
.pkt_svc
;
771 m_service_class_from_idx(u_int32_t i
)
773 mbuf_svc_class_t sc
= MBUF_SC_BE
;
777 return (MBUF_SC_BK_SYS
);
789 return (MBUF_SC_OAM
);
804 return (MBUF_SC_CTL
);
816 m_service_class_from_val(u_int32_t v
)
818 mbuf_svc_class_t sc
= MBUF_SC_BE
;
822 return (MBUF_SC_BK_SYS
);
834 return (MBUF_SC_OAM
);
849 return (MBUF_SC_CTL
);
861 m_adj_sum16(struct mbuf
*m
, uint32_t start
, uint32_t dataoff
,
862 uint32_t datalen
, uint32_t sum
)
864 uint32_t total_sub
= 0; /* total to subtract */
865 uint32_t mlen
= m_pktlen(m
); /* frame length */
866 uint32_t bytes
= (dataoff
+ datalen
); /* bytes covered by sum */
869 ASSERT(bytes
<= mlen
);
872 * Take care of excluding (len > 0) or including (len < 0)
873 * extraneous octets at the beginning of the packet, taking
874 * into account the start offset.
876 len
= (dataoff
- start
);
878 total_sub
= m_sum16(m
, start
, len
);
880 sum
+= m_sum16(m
, dataoff
, -len
);
883 * Take care of excluding any postpended extraneous octets.
885 len
= (mlen
- bytes
);
888 uint32_t extra
= m_sum16(m
, bytes
, len
);
889 uint32_t off
= bytes
, off0
= off
;
892 if (__improbable(m
== NULL
)) {
893 panic("%s: invalid mbuf chain %p [off %u, "
894 "len %u]", __func__
, m0
, off0
, len
);
903 /* if we started on odd-alignment, swap the value */
904 if ((uintptr_t)(mtod(m
, uint8_t *) + off
) & 1)
905 total_sub
+= ((extra
<< 8) & 0xffff) | (extra
>> 8);
909 total_sub
= (total_sub
>> 16) + (total_sub
& 0xffff);
913 * 1's complement subtract any extraneous octets.
915 if (total_sub
!= 0) {
916 if (total_sub
>= sum
)
917 sum
= ~(total_sub
- sum
) & 0xffff;
922 /* fold 32-bit to 16-bit */
923 sum
= (sum
>> 16) + (sum
& 0xffff); /* 17-bit */
924 sum
= (sum
>> 16) + (sum
& 0xffff); /* 16-bit + carry */
925 sum
= (sum
>> 16) + (sum
& 0xffff); /* final carry */
927 return (sum
& 0xffff);
931 m_sum16(struct mbuf
*m
, uint32_t off
, uint32_t len
)
938 * Use m_length2() instead of m_length(), as we cannot rely on
939 * the caller setting m_pkthdr.len correctly, if the mbuf is
942 if ((mlen
= m_length2(m
, NULL
)) < (off
+ len
)) {
943 panic("%s: mbuf %p len (%d) < off+len (%d+%d)\n", __func__
,
948 return (os_cpu_in_cksum_mbuf(m
, len
, off
, 0));