]>
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()");
145 if (len
> MCLBYTES
) {
147 return NULL
; /* impossible */
150 #if defined(PULLDOWN_STAT) && INET6
151 ip6stat
.ip6s_pulldown
++;
154 #if defined(PULLDOWN_STAT) && INET6
155 /* statistics for m_pullup */
156 ip6stat
.ip6s_pullup
++;
157 if (off
+ len
> MHLEN
) {
158 ip6stat
.ip6s_pullup_fail
++;
162 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
163 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
165 if (dlen
>= off
+ len
) {
166 ip6stat
.ip6s_pullup
--; /* call will not be made! */
167 } else if ((m
->m_flags
& M_EXT
) != 0) {
168 ip6stat
.ip6s_pullup_alloc
++;
169 ip6stat
.ip6s_pullup_copy
++;
171 if (mlen
>= off
+ len
) {
172 ip6stat
.ip6s_pullup_copy
++;
174 ip6stat
.ip6s_pullup_alloc
++;
175 ip6stat
.ip6s_pullup_copy
++;
183 /* statistics for m_pullup2 */
184 ip6stat
.ip6s_pullup2
++;
185 if (off
+ len
> MCLBYTES
) {
186 ip6stat
.ip6s_pullup2_fail
++;
190 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
191 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
195 if (dlen
>= off
+ len
) {
196 ip6stat
.ip6s_pullup2
--; /* call will not be made! */
197 } else if ((m
->m_flags
& M_EXT
) != 0) {
198 ip6stat
.ip6s_pullup2_alloc
++;
199 ip6stat
.ip6s_pullup2_copy
++;
200 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
: MHLEN
;
202 if (mlen
>= off
+ len
) {
203 ip6stat
.ip6s_pullup2_copy
++;
205 ip6stat
.ip6s_pullup2_alloc
++;
206 ip6stat
.ip6s_pullup2_copy
++;
207 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
216 #ifdef PULLDOWN_DEBUG
220 for (t
= m
; t
; t
= t
->m_next
) {
221 printf(" %d", t
->m_len
);
229 * Iterate and make n point to the mbuf
230 * within which the first byte at length
231 * offset is contained from the start of
234 while (n
!= NULL
&& off
> 0) {
235 if (n
->m_len
> off
) {
242 /* be sure to point non-empty mbuf */
243 while (n
!= NULL
&& n
->m_len
== 0) {
249 return NULL
; /* mbuf chain too short */
253 * the target data is on <n, off>.
254 * if we got enough data on the mbuf "n", we're done.
256 * It should be noted, that we should only do this either
257 * when offset is 0, i.e. data is pointing to the start
258 * or when the caller specifies an out argument to get
259 * the offset value in the mbuf to work with data pointer
262 * If offset is not 0 and caller did not provide out-argument
263 * to get offset, we should split the mbuf even when the length
264 * is contained in current mbuf.
266 if ((off
== 0 || offp
) && len
<= n
->m_len
- off
) {
270 #if defined(PULLDOWN_STAT) && INET6
271 ip6stat
.ip6s_pulldown_copy
++;
275 * when len <= n->m_len - off and off != 0, it is a special case.
276 * len bytes from <n, off> sits in single mbuf, but the caller does
277 * not like the starting position (off).
278 * chop the current mbuf into two pieces, set off to 0.
280 if (len
<= n
->m_len
- off
) {
281 o
= m_copym(n
, off
, n
->m_len
- off
, M_DONTWAIT
);
284 return NULL
; /* ENOBUFS */
287 o
->m_next
= n
->m_next
;
295 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
296 * and construct contiguous mbuf with m_len == len.
297 * note that hlen + tlen == len, and tlen > 0.
299 * Read these variables as head length and tail length
301 hlen
= n
->m_len
- off
;
305 * ensure that we have enough trailing data on mbuf chain.
306 * if not, we can do nothing about the chain.
309 for (o
= n
->m_next
; o
!= NULL
; o
= o
->m_next
) {
312 if (hlen
+ olen
< len
) {
314 return NULL
; /* mbuf chain too short */
319 * we need to use m_copydata() to get data from <n->m_next, 0>.
321 if ((n
->m_flags
& M_EXT
) == 0) {
324 if (m_get_ext_free(n
) != NULL
) {
326 } else if (m_mclhasreference(n
)) {
334 * If we have enough space left in current mbuf to accomodate
335 * tail length, copy tail length worth of data starting with next mbuf
336 * and adjust the length of next one accordingly.
338 if ((off
== 0 || offp
) && M_TRAILINGSPACE(n
) >= tlen
340 m_copydata(n
->m_next
, 0, tlen
, mtod(n
, caddr_t
) + n
->m_len
);
342 m_adj(n
->m_next
, tlen
);
347 * If have enough leading space in next mbuf to accomodate head length
348 * of current mbuf, and total resulting length of next mbuf is greater
349 * than or equal to requested len bytes, then just copy hlen from
350 * current to the next one and adjust sizes accordingly.
352 if ((off
== 0 || offp
) && M_LEADINGSPACE(n
->m_next
) >= hlen
&&
353 (n
->m_next
->m_len
+ hlen
) >= len
&& !sharedcluster
) {
354 n
->m_next
->m_data
-= hlen
;
355 n
->m_next
->m_len
+= hlen
;
356 bcopy(mtod(n
, caddr_t
) + off
, mtod(n
->m_next
, caddr_t
), hlen
);
364 * now, we need to do the hard way. don't m_copy as there's no room
367 #if defined(PULLDOWN_STAT) && INET6
368 ip6stat
.ip6s_pulldown_alloc
++;
370 MGET(o
, M_DONTWAIT
, m
->m_type
);
373 return NULL
; /* ENOBUFS */
375 if (len
> MHLEN
) { /* use MHLEN just for safety */
376 MCLGET(o
, M_DONTWAIT
);
377 if ((o
->m_flags
& M_EXT
) == 0) {
380 return NULL
; /* ENOBUFS */
383 /* get hlen from <n, off> into <o, 0> */
385 bcopy(mtod(n
, caddr_t
) + off
, mtod(o
, caddr_t
), hlen
);
387 /* get tlen from <n->m_next, 0> into <o, hlen> */
388 m_copydata(n
->m_next
, 0, tlen
, mtod(o
, caddr_t
) + o
->m_len
);
390 m_adj(n
->m_next
, tlen
);
391 o
->m_next
= n
->m_next
;
397 #ifdef PULLDOWN_DEBUG
401 for (t
= m
; t
; t
= t
->m_next
) {
402 printf("%c%d", t
== n
? '*' : ' ', t
->m_len
);
404 printf(" (off=%d)\n", off
);
414 * Create and return an m_tag, either by re-using space in a previous tag
415 * or by allocating a new mbuf/cluster
418 m_tag_create(u_int32_t id
, u_int16_t type
, int len
, int wait
, struct mbuf
*buf
)
420 struct m_tag
*t
= NULL
;
427 if (len
+ sizeof(struct m_tag
) + sizeof(struct m_taghdr
) > MLEN
) {
428 return m_tag_alloc(id
, type
, len
, wait
);
432 * We've exhausted all external cases. Now, go through the m_tag
433 * chain and see if we can fit it in any of them.
434 * If not (t == NULL), call m_tag_alloc to store it in a new mbuf.
436 p
= SLIST_FIRST(&buf
->m_pkthdr
.tags
);
439 if (M_TAG_ALIGN(p
->m_tag_len
) +
440 sizeof(struct m_taghdr
) > MLEN
) {
441 p
= SLIST_NEXT(p
, m_tag_link
);
445 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
447 struct mbuf
*m
= m_dtom(p
);
448 struct m_taghdr
*hdr
= (struct m_taghdr
*)(void *)m
->m_data
;
450 VERIFY(IS_P2ALIGNED(hdr
+ 1, sizeof(u_int64_t
)));
451 VERIFY(m
->m_flags
& M_TAGHDR
&& !(m
->m_flags
& M_EXT
));
453 /* The mbuf can store this m_tag */
454 if (M_TAG_ALIGN(len
) <= MLEN
- m
->m_len
) {
455 t
= (struct m_tag
*)(void *)(m
->m_data
+ m
->m_len
);
456 VERIFY(IS_P2ALIGNED(t
, sizeof(u_int64_t
)));
458 m
->m_len
+= M_TAG_ALIGN(len
);
459 VERIFY(m
->m_len
<= MLEN
);
463 p
= SLIST_NEXT(p
, m_tag_link
);
467 return m_tag_alloc(id
, type
, len
, wait
);
470 t
->m_tag_cookie
= M_TAG_VALID_PATTERN
;
471 t
->m_tag_type
= type
;
480 /* Get a packet tag structure along with specified data following. */
482 m_tag_alloc(u_int32_t id
, u_int16_t type
, int len
, int wait
)
490 if (M_TAG_ALIGN(len
) + sizeof(struct m_taghdr
) <= MLEN
) {
491 struct mbuf
*m
= m_get(wait
, MT_TAG
);
492 struct m_taghdr
*hdr
;
498 m
->m_flags
|= M_TAGHDR
;
500 hdr
= (struct m_taghdr
*)(void *)m
->m_data
;
501 VERIFY(IS_P2ALIGNED(hdr
+ 1, sizeof(u_int64_t
)));
503 m
->m_len
+= sizeof(struct m_taghdr
);
504 t
= (struct m_tag
*)(void *)(m
->m_data
+ m
->m_len
);
505 VERIFY(IS_P2ALIGNED(t
, sizeof(u_int64_t
)));
506 m
->m_len
+= M_TAG_ALIGN(len
);
507 VERIFY(m
->m_len
<= MLEN
);
508 } else if (len
+ sizeof(struct m_tag
) <= MCLBYTES
) {
509 t
= (struct m_tag
*)(void *)m_mclalloc(wait
);
518 VERIFY(IS_P2ALIGNED(t
, sizeof(u_int64_t
)));
519 t
->m_tag_cookie
= M_TAG_VALID_PATTERN
;
520 t
->m_tag_type
= type
;
530 /* Free a packet tag. */
532 m_tag_free(struct m_tag
*t
)
536 t
->m_tag_id
== KERNEL_MODULE_TAG_ID
&&
537 t
->m_tag_type
== KERNEL_TAG_TYPE_MACLABEL
) {
538 mac_mbuf_tag_destroy(t
);
545 VERIFY(t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
547 if (M_TAG_ALIGN(t
->m_tag_len
) + sizeof(struct m_taghdr
) <= MLEN
) {
548 struct mbuf
* m
= m_dtom(t
);
549 VERIFY(m
->m_flags
& M_TAGHDR
);
550 struct m_taghdr
*hdr
= (struct m_taghdr
*)(void *)m
->m_data
;
552 VERIFY(IS_P2ALIGNED(hdr
+ 1, sizeof(u_int64_t
)));
554 /* No other tags in this mbuf */
555 if (--hdr
->refcnt
== 0) {
560 /* Pattern-fill the header */
561 u_int64_t
*fill_ptr
= (u_int64_t
*)t
;
562 u_int64_t
*end_ptr
= (u_int64_t
*)(t
+ 1);
563 while (fill_ptr
< end_ptr
) {
564 *fill_ptr
= M_TAG_FREE_PATTERN
;
568 m_mclfree((caddr_t
)t
);
572 /* Prepend a packet tag. */
574 m_tag_prepend(struct mbuf
*m
, struct m_tag
*t
)
576 VERIFY(m
!= NULL
&& t
!= NULL
);
578 SLIST_INSERT_HEAD(&m
->m_pkthdr
.tags
, t
, m_tag_link
);
581 /* Unlink a packet tag. */
583 m_tag_unlink(struct mbuf
*m
, struct m_tag
*t
)
585 VERIFY(m
->m_flags
& M_PKTHDR
);
586 VERIFY(t
!= NULL
&& t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
588 SLIST_REMOVE(&m
->m_pkthdr
.tags
, t
, m_tag
, m_tag_link
);
591 /* Unlink and free a packet tag. */
593 m_tag_delete(struct mbuf
*m
, struct m_tag
*t
)
599 /* Unlink and free a packet tag chain, starting from given tag. */
601 m_tag_delete_chain(struct mbuf
*m
, struct m_tag
*t
)
605 VERIFY(m
->m_flags
& M_PKTHDR
);
610 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
616 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
617 while ((q
= SLIST_NEXT(p
, m_tag_link
)) != NULL
) {
618 VERIFY(q
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
624 /* Find a tag, starting from a given position. */
626 m_tag_locate(struct mbuf
*m
, u_int32_t id
, u_int16_t type
, struct m_tag
*t
)
630 VERIFY(m
->m_flags
& M_PKTHDR
);
633 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
635 VERIFY(t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
636 p
= SLIST_NEXT(t
, m_tag_link
);
639 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
640 if (p
->m_tag_id
== id
&& p
->m_tag_type
== type
) {
643 p
= SLIST_NEXT(p
, m_tag_link
);
648 /* Copy a single tag. */
650 m_tag_copy(struct m_tag
*t
, int how
)
656 p
= m_tag_alloc(t
->m_tag_id
, t
->m_tag_type
, t
->m_tag_len
, how
);
662 * XXXMAC: we should probably pass off the initialization, and
663 * copying here? can we hid that KERNEL_TAG_TYPE_MACLABEL is
664 * special from the mbuf code?
667 t
->m_tag_id
== KERNEL_MODULE_TAG_ID
&&
668 t
->m_tag_type
== KERNEL_TAG_TYPE_MACLABEL
) {
669 if (mac_mbuf_tag_init(p
, how
) != 0) {
673 mac_mbuf_tag_copy(t
, p
);
676 bcopy(t
+ 1, p
+ 1, t
->m_tag_len
); /* Copy the data */
681 * Copy two tag chains. The destination mbuf (to) loses any attached
682 * tags even if the operation fails. This should not be a problem, as
683 * m_tag_copy_chain() is typically called with a newly-allocated
687 m_tag_copy_chain(struct mbuf
*to
, struct mbuf
*from
, int how
)
689 struct m_tag
*p
, *t
, *tprev
= NULL
;
691 VERIFY((to
->m_flags
& M_PKTHDR
) && (from
->m_flags
& M_PKTHDR
));
693 m_tag_delete_chain(to
, NULL
);
694 SLIST_FOREACH(p
, &from
->m_pkthdr
.tags
, m_tag_link
) {
695 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
696 t
= m_tag_copy(p
, how
);
698 m_tag_delete_chain(to
, NULL
);
702 SLIST_INSERT_HEAD(&to
->m_pkthdr
.tags
, t
, m_tag_link
);
704 SLIST_INSERT_AFTER(tprev
, t
, m_tag_link
);
711 /* Initialize dynamic and static tags on an mbuf. */
713 m_tag_init(struct mbuf
*m
, int all
)
715 VERIFY(m
->m_flags
& M_PKTHDR
);
717 SLIST_INIT(&m
->m_pkthdr
.tags
);
719 * If the caller wants to preserve static mbuf tags
720 * (e.g. m_dup_pkthdr), don't zero them out.
723 bzero(&m
->m_pkthdr
.builtin_mtag
._net_mtag
,
724 sizeof(m
->m_pkthdr
.builtin_mtag
._net_mtag
));
728 /* Get first tag in chain. */
730 m_tag_first(struct mbuf
*m
)
732 VERIFY(m
->m_flags
& M_PKTHDR
);
734 return SLIST_FIRST(&m
->m_pkthdr
.tags
);
737 /* Get next tag in chain. */
739 m_tag_next(struct mbuf
*m
, struct m_tag
*t
)
743 VERIFY(t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
745 return SLIST_NEXT(t
, m_tag_link
);
749 m_set_traffic_class(struct mbuf
*m
, mbuf_traffic_class_t tc
)
751 u_int32_t val
= MBUF_TC2SCVAL(tc
); /* just the val portion */
753 return m_set_service_class(m
, m_service_class_from_val(val
));
757 m_get_traffic_class(struct mbuf
*m
)
759 return MBUF_SC2TC(m_get_service_class(m
));
763 m_set_service_class(struct mbuf
*m
, mbuf_svc_class_t sc
)
767 VERIFY(m
->m_flags
& M_PKTHDR
);
769 if (MBUF_VALID_SC(sc
)) {
770 m
->m_pkthdr
.pkt_svc
= sc
;
779 m_get_service_class(struct mbuf
*m
)
783 VERIFY(m
->m_flags
& M_PKTHDR
);
785 if (MBUF_VALID_SC(m
->m_pkthdr
.pkt_svc
)) {
786 sc
= m
->m_pkthdr
.pkt_svc
;
795 m_service_class_from_idx(u_int32_t i
)
797 mbuf_svc_class_t sc
= MBUF_SC_BE
;
801 return MBUF_SC_BK_SYS
;
840 m_service_class_from_val(u_int32_t v
)
842 mbuf_svc_class_t sc
= MBUF_SC_BE
;
846 return MBUF_SC_BK_SYS
;
885 m_adj_sum16(struct mbuf
*m
, uint32_t start
, uint32_t dataoff
,
886 uint32_t datalen
, uint32_t sum
)
888 uint32_t total_sub
= 0; /* total to subtract */
889 uint32_t mlen
= m_pktlen(m
); /* frame length */
890 uint32_t bytes
= (dataoff
+ datalen
); /* bytes covered by sum */
893 ASSERT(bytes
<= mlen
);
896 * Take care of excluding (len > 0) or including (len < 0)
897 * extraneous octets at the beginning of the packet, taking
898 * into account the start offset.
900 len
= (dataoff
- start
);
902 total_sub
= m_sum16(m
, start
, len
);
903 } else if (len
< 0) {
904 sum
+= m_sum16(m
, dataoff
, -len
);
908 * Take care of excluding any postpended extraneous octets.
910 len
= (mlen
- bytes
);
913 uint32_t extra
= m_sum16(m
, bytes
, len
);
914 uint32_t off
= bytes
, off0
= off
;
917 if (__improbable(m
== NULL
)) {
918 panic("%s: invalid mbuf chain %p [off %u, "
919 "len %u]", __func__
, m0
, off0
, len
);
922 if (off
< m
->m_len
) {
929 /* if we started on odd-alignment, swap the value */
930 if ((uintptr_t)(mtod(m
, uint8_t *) + off
) & 1) {
931 total_sub
+= ((extra
<< 8) & 0xffff) | (extra
>> 8);
936 total_sub
= (total_sub
>> 16) + (total_sub
& 0xffff);
940 * 1's complement subtract any extraneous octets.
942 if (total_sub
!= 0) {
943 if (total_sub
>= sum
) {
944 sum
= ~(total_sub
- sum
) & 0xffff;
950 /* fold 32-bit to 16-bit */
951 sum
= (sum
>> 16) + (sum
& 0xffff); /* 17-bit */
952 sum
= (sum
>> 16) + (sum
& 0xffff); /* 16-bit + carry */
953 sum
= (sum
>> 16) + (sum
& 0xffff); /* final carry */
959 m_sum16(struct mbuf
*m
, uint32_t off
, uint32_t len
)
966 * Use m_length2() instead of m_length(), as we cannot rely on
967 * the caller setting m_pkthdr.len correctly, if the mbuf is
970 if ((mlen
= m_length2(m
, NULL
)) < (off
+ len
)) {
971 panic("%s: mbuf %p len (%d) < off+len (%d+%d)\n", __func__
,
976 return os_cpu_in_cksum_mbuf(m
, len
, off
, 0);