2 * Copyright (c) 2009-2016 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@
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/types.h>
32 #include <sys/filedesc.h>
33 #include <sys/file_internal.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/errno.h>
38 #include <sys/protosw.h>
39 #include <sys/domain.h>
41 #include <sys/queue.h>
42 #include <sys/sysctl.h>
45 #include <net/if_var.h>
46 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/in_pcb.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/ip6_var.h>
55 #include <netinet/udp.h>
56 #include <netinet/udp_var.h>
57 #include <netinet/tcp.h>
58 #include <netinet/tcp_var.h>
59 #include <netinet/tcp_cc.h>
60 #include <netinet/lro_ext.h>
61 #include <netinet/in_tclass.h>
67 static inline int so_throttle_best_effort(struct socket
*, struct ifnet
*);
68 static void set_dscp_to_wifi_ac_map(const struct dcsp_msc_map
*, int);
69 static errno_t
dscp_msc_map_from_netsvctype_dscp_map(struct netsvctype_dscp_map
*, size_t,
70 struct dcsp_msc_map
*);
72 static lck_grp_attr_t
*tclass_lck_grp_attr
= NULL
; /* mutex group attributes */
73 static lck_grp_t
*tclass_lck_grp
= NULL
; /* mutex group definition */
74 static lck_attr_t
*tclass_lck_attr
= NULL
; /* mutex attributes */
75 decl_lck_mtx_data(static, tclass_lock_data
);
76 static lck_mtx_t
*tclass_lock
= &tclass_lock_data
;
78 SYSCTL_NODE(_net
, OID_AUTO
, qos
,
79 CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "QoS");
81 static int sysctl_default_netsvctype_to_dscp_map SYSCTL_HANDLER_ARGS
;
82 SYSCTL_PROC(_net_qos
, OID_AUTO
, default_netsvctype_to_dscp_map
,
83 CTLTYPE_STRUCT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
84 0, 0, sysctl_default_netsvctype_to_dscp_map
, "S", "");
86 static int sysctl_dscp_to_wifi_ac_map SYSCTL_HANDLER_ARGS
;
87 SYSCTL_PROC(_net_qos
, OID_AUTO
, dscp_to_wifi_ac_map
,
88 CTLTYPE_STRUCT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
89 0, 0, sysctl_dscp_to_wifi_ac_map
, "S", "");
91 static int sysctl_reset_dscp_to_wifi_ac_map SYSCTL_HANDLER_ARGS
;
92 SYSCTL_PROC(_net_qos
, OID_AUTO
, reset_dscp_to_wifi_ac_map
,
93 CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_LOCKED
,
94 0, 0, sysctl_reset_dscp_to_wifi_ac_map
, "I", "");
96 int net_qos_verbose
= 0;
97 SYSCTL_INT(_net_qos
, OID_AUTO
, verbose
,
98 CTLFLAG_RW
| CTLFLAG_LOCKED
, &net_qos_verbose
, 0, "");
101 * Fastlane QoS policy:
102 * By Default allow all apps to get traffic class to DSCP mapping
104 SYSCTL_NODE(_net_qos
, OID_AUTO
, policy
,
105 CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "");
107 int net_qos_policy_restricted
= 0;
108 SYSCTL_INT(_net_qos_policy
, OID_AUTO
, restricted
,
109 CTLFLAG_RW
| CTLFLAG_LOCKED
, &net_qos_policy_restricted
, 0, "");
111 int net_qos_policy_restrict_avapps
= 0;
112 SYSCTL_INT(_net_qos_policy
, OID_AUTO
, restrict_avapps
,
113 CTLFLAG_RW
| CTLFLAG_LOCKED
, &net_qos_policy_restrict_avapps
, 0, "");
115 int net_qos_policy_wifi_enabled
= 0;
116 SYSCTL_INT(_net_qos_policy
, OID_AUTO
, wifi_enabled
,
117 CTLFLAG_RW
| CTLFLAG_LOCKED
, &net_qos_policy_wifi_enabled
, 0, "");
119 int net_qos_policy_capable_enabled
= 0;
120 SYSCTL_INT(_net_qos_policy
, OID_AUTO
, capable_enabled
,
121 CTLFLAG_RW
| CTLFLAG_LOCKED
, &net_qos_policy_capable_enabled
, 0, "");
124 * Socket traffic class from network service type
126 const int sotc_by_netservicetype
[_NET_SERVICE_TYPE_COUNT
] = {
127 SO_TC_BE
, /* NET_SERVICE_TYPE_BE */
128 SO_TC_BK_SYS
, /* NET_SERVICE_TYPE_BK */
129 SO_TC_VI
, /* NET_SERVICE_TYPE_SIG */
130 SO_TC_VI
, /* NET_SERVICE_TYPE_VI */
131 SO_TC_VO
, /* NET_SERVICE_TYPE_VO */
132 SO_TC_RV
, /* NET_SERVICE_TYPE_RV */
133 SO_TC_AV
, /* NET_SERVICE_TYPE_AV */
134 SO_TC_OAM
, /* NET_SERVICE_TYPE_OAM */
135 SO_TC_RD
/* NET_SERVICE_TYPE_RD */
139 * DSCP mappings for QoS Fastlane as based on network service types
142 struct netsvctype_dscp_map fastlane_netsvctype_dscp_map
[_NET_SERVICE_TYPE_COUNT
] = {
143 { NET_SERVICE_TYPE_BE
, _DSCP_DF
},
144 { NET_SERVICE_TYPE_BK
, _DSCP_AF11
},
145 { NET_SERVICE_TYPE_SIG
, _DSCP_CS3
},
146 { NET_SERVICE_TYPE_VI
, _DSCP_AF41
},
147 { NET_SERVICE_TYPE_VO
, _DSCP_EF
},
148 { NET_SERVICE_TYPE_RV
, _DSCP_CS4
},
149 { NET_SERVICE_TYPE_AV
, _DSCP_AF31
},
150 { NET_SERVICE_TYPE_OAM
, _DSCP_CS2
},
151 { NET_SERVICE_TYPE_RD
, _DSCP_AF21
},
154 static struct net_qos_dscp_map default_net_qos_dscp_map
;
157 * The size is one more than the max because DSCP start at zero
159 #define DSCP_ARRAY_SIZE (_MAX_DSCP + 1)
162 * The DSCP to UP mapping (via mbuf service class) for WiFi follows is the mapping
163 * that implemented at the 802.11 driver level when the mbuf service class is
166 * This clashes with the recommended mapping documented by the IETF document
167 * draft-szigeti-tsvwg-ieee-802-11e-01.txt but we keep the mapping to maintain
168 * binary compatibility. Applications should use the network service type socket
169 * option instead to select L2 QoS marking instead of IP_TOS or IPV6_TCLASS.
171 static const struct dcsp_msc_map default_dscp_to_wifi_ac_map
[] = {
172 { _DSCP_DF
, MBUF_SC_BE
}, /* RFC 2474 Standard */
173 { 1, MBUF_SC_BE
}, /* */
174 { 2, MBUF_SC_BE
}, /* */
175 { 3, MBUF_SC_BE
}, /* */
176 { 4, MBUF_SC_BE
}, /* */
177 { 5, MBUF_SC_BE
}, /* */
178 { 6, MBUF_SC_BE
}, /* */
179 { 7, MBUF_SC_BE
}, /* */
181 { _DSCP_CS1
, MBUF_SC_BK
}, /* RFC 3662 Low-Priority Data */
182 { 9, MBUF_SC_BK
}, /* */
183 { _DSCP_AF11
, MBUF_SC_BK
}, /* RFC 2597 High-Throughput Data */
184 { 11, MBUF_SC_BK
}, /* */
185 { _DSCP_AF12
, MBUF_SC_BK
}, /* RFC 2597 High-Throughput Data */
186 { 13, MBUF_SC_BK
}, /* */
187 { _DSCP_AF13
, MBUF_SC_BK
}, /* RFC 2597 High-Throughput Data */
188 { 15, MBUF_SC_BK
}, /* */
190 { _DSCP_CS2
, MBUF_SC_BK
}, /* RFC 4594 OAM */
191 { 17, MBUF_SC_BK
}, /* */
192 { _DSCP_AF21
, MBUF_SC_BK
}, /* RFC 2597 Low-Latency Data */
193 { 19, MBUF_SC_BK
}, /* */
194 { _DSCP_AF22
, MBUF_SC_BK
}, /* RFC 2597 Low-Latency Data */
195 { 21, MBUF_SC_BK
}, /* */
196 { _DSCP_AF23
, MBUF_SC_BK
}, /* RFC 2597 Low-Latency Data */
197 { 23, MBUF_SC_BK
}, /* */
199 { _DSCP_CS3
, MBUF_SC_BE
}, /* RFC 2474 Broadcast Video */
200 { 25, MBUF_SC_BE
}, /* */
201 { _DSCP_AF31
, MBUF_SC_BE
}, /* RFC 2597 Multimedia Streaming */
202 { 27, MBUF_SC_BE
}, /* */
203 { _DSCP_AF32
, MBUF_SC_BE
}, /* RFC 2597 Multimedia Streaming */
204 { 29, MBUF_SC_BE
}, /* */
205 { _DSCP_AF33
, MBUF_SC_BE
}, /* RFC 2597 Multimedia Streaming */
206 { 31, MBUF_SC_BE
}, /* */
208 { _DSCP_CS4
, MBUF_SC_VI
}, /* RFC 2474 Real-Time Interactive */
209 { 33, MBUF_SC_VI
}, /* */
210 { _DSCP_AF41
, MBUF_SC_VI
}, /* RFC 2597 Multimedia Conferencing */
211 { 35, MBUF_SC_VI
}, /* */
212 { _DSCP_AF42
, MBUF_SC_VI
}, /* RFC 2597 Multimedia Conferencing */
213 { 37, MBUF_SC_VI
}, /* */
214 { _DSCP_AF43
, MBUF_SC_VI
}, /* RFC 2597 Multimedia Conferencing */
215 { 39, MBUF_SC_VI
}, /* */
217 { _DSCP_CS5
, MBUF_SC_VI
}, /* RFC 2474 Signaling */
218 { 41, MBUF_SC_VI
}, /* */
219 { 42, MBUF_SC_VI
}, /* */
220 { 43, MBUF_SC_VI
}, /* */
221 { _DSCP_VA
, MBUF_SC_VI
}, /* RFC 5865 VOICE-ADMIT */
222 { 45, MBUF_SC_VI
}, /* */
223 { _DSCP_EF
, MBUF_SC_VI
}, /* RFC 3246 Telephony */
224 { 47, MBUF_SC_VI
}, /* */
226 { _DSCP_CS6
, MBUF_SC_VO
}, /* Wi-Fi WMM Certification: Chariot */
227 { 49, MBUF_SC_VO
}, /* */
228 { 50, MBUF_SC_VO
}, /* */
229 { 51, MBUF_SC_VO
}, /* */
230 { 52, MBUF_SC_VO
}, /* Wi-Fi WMM Certification: Sigma */
231 { 53, MBUF_SC_VO
}, /* */
232 { 54, MBUF_SC_VO
}, /* */
233 { 55, MBUF_SC_VO
}, /* */
235 { _DSCP_CS7
, MBUF_SC_VO
}, /* Wi-Fi WMM Certification: Chariot */
236 { 57, MBUF_SC_VO
}, /* */
237 { 58, MBUF_SC_VO
}, /* */
238 { 59, MBUF_SC_VO
}, /* */
239 { 60, MBUF_SC_VO
}, /* */
240 { 61, MBUF_SC_VO
}, /* */
241 { 62, MBUF_SC_VO
}, /* */
242 { 63, MBUF_SC_VO
}, /* */
244 { 255, MBUF_SC_UNSPEC
} /* invalid DSCP to mark last entry */
247 mbuf_svc_class_t wifi_dscp_to_msc_array
[DSCP_ARRAY_SIZE
];
250 * If there is no foreground activity on the interface for bg_switch_time
251 * seconds, the background connections can switch to foreground TCP
252 * congestion control.
254 #define TCP_BG_SWITCH_TIME 2 /* seconds */
256 #if (DEVELOPMENT || DEBUG)
258 extern char *proc_best_name(proc_t p
);
260 static int tfp_count
= 0;
262 static TAILQ_HEAD(, tclass_for_proc
) tfp_head
=
263 TAILQ_HEAD_INITIALIZER(tfp_head
);
265 struct tclass_for_proc
{
266 TAILQ_ENTRY(tclass_for_proc
) tfp_link
;
269 char tfp_pname
[(2 * MAXCOMLEN
) + 1];
270 u_int32_t tfp_qos_mode
;
273 static int get_pid_tclass(struct so_tcdbg
*);
274 static int get_pname_tclass(struct so_tcdbg
*);
275 static int set_pid_tclass(struct so_tcdbg
*);
276 static int set_pname_tclass(struct so_tcdbg
*);
277 static int flush_pid_tclass(struct so_tcdbg
*);
278 static int purge_tclass_for_proc(void);
279 static int flush_tclass_for_proc(void);
280 static void set_tclass_for_curr_proc(struct socket
*);
283 * Must be called with tclass_lock held
285 static struct tclass_for_proc
*
286 find_tfp_by_pid(pid_t pid
)
288 struct tclass_for_proc
*tfp
;
290 TAILQ_FOREACH(tfp
, &tfp_head
, tfp_link
) {
291 if (tfp
->tfp_pid
== pid
)
298 * Must be called with tclass_lock held
300 static struct tclass_for_proc
*
301 find_tfp_by_pname(const char *pname
)
303 struct tclass_for_proc
*tfp
;
305 TAILQ_FOREACH(tfp
, &tfp_head
, tfp_link
) {
306 if (strncmp(pname
, tfp
->tfp_pname
,
307 sizeof (tfp
->tfp_pname
)) == 0)
313 __private_extern__
void
314 set_tclass_for_curr_proc(struct socket
*so
)
316 struct tclass_for_proc
*tfp
= NULL
;
317 proc_t p
= current_proc(); /* Not ref counted */
318 pid_t pid
= proc_pid(p
);
319 char *pname
= proc_best_name(p
);
321 lck_mtx_lock(tclass_lock
);
323 TAILQ_FOREACH(tfp
, &tfp_head
, tfp_link
) {
324 if ((tfp
->tfp_pid
== pid
) || (tfp
->tfp_pid
== -1 &&
325 strncmp(pname
, tfp
->tfp_pname
,
326 sizeof (tfp
->tfp_pname
)) == 0)) {
327 if (tfp
->tfp_class
!= SO_TC_UNSPEC
)
328 so
->so_traffic_class
= tfp
->tfp_class
;
330 if (tfp
->tfp_qos_mode
== QOS_MODE_MARKING_POLICY_ENABLE
)
331 so
->so_flags1
|= SOF1_QOSMARKING_ALLOWED
;
332 else if (tfp
->tfp_qos_mode
== QOS_MODE_MARKING_POLICY_DISABLE
)
333 so
->so_flags1
&= ~SOF1_QOSMARKING_ALLOWED
;
338 lck_mtx_unlock(tclass_lock
);
342 * Purge entries with PIDs of exited processes
345 purge_tclass_for_proc(void)
348 struct tclass_for_proc
*tfp
, *tvar
;
350 lck_mtx_lock(tclass_lock
);
352 TAILQ_FOREACH_SAFE(tfp
, &tfp_head
, tfp_link
, tvar
) {
355 if (tfp
->tfp_pid
== -1)
357 if ((p
= proc_find(tfp
->tfp_pid
)) == NULL
) {
359 TAILQ_REMOVE(&tfp_head
, tfp
, tfp_link
);
367 lck_mtx_unlock(tclass_lock
);
374 * Must be called with tclass_lock held
377 free_tclass_for_proc(struct tclass_for_proc
*tfp
)
382 TAILQ_REMOVE(&tfp_head
, tfp
, tfp_link
);
390 flush_tclass_for_proc(void)
393 struct tclass_for_proc
*tfp
, *tvar
;
395 lck_mtx_lock(tclass_lock
);
397 TAILQ_FOREACH_SAFE(tfp
, &tfp_head
, tfp_link
, tvar
) {
398 free_tclass_for_proc(tfp
);
401 lck_mtx_unlock(tclass_lock
);
408 * Must be called with tclass_lock held
410 static struct tclass_for_proc
*
411 alloc_tclass_for_proc(pid_t pid
, const char *pname
)
413 struct tclass_for_proc
*tfp
;
415 if (pid
== -1 && pname
== NULL
)
418 tfp
= _MALLOC(sizeof (struct tclass_for_proc
), M_TEMP
, M_NOWAIT
|M_ZERO
);
424 * Add per pid entries before per proc name so we can find
425 * a specific instance of a process before the general name base entry.
428 TAILQ_INSERT_HEAD(&tfp_head
, tfp
, tfp_link
);
430 strlcpy(tfp
->tfp_pname
, pname
, sizeof (tfp
->tfp_pname
));
431 TAILQ_INSERT_TAIL(&tfp_head
, tfp
, tfp_link
);
440 * SO_TC_UNSPEC for tclass means to remove the entry
443 set_pid_tclass(struct so_tcdbg
*so_tcdbg
)
447 struct filedesc
*fdp
;
449 struct tclass_for_proc
*tfp
;
451 pid_t pid
= so_tcdbg
->so_tcdbg_pid
;
452 int tclass
= so_tcdbg
->so_tcdbg_tclass
;
453 int netsvctype
= so_tcdbg
->so_tcdbg_netsvctype
;
457 printf("%s proc_find(%d) failed\n", __func__
, pid
);
462 lck_mtx_lock(tclass_lock
);
464 tfp
= find_tfp_by_pid(pid
);
466 tfp
= alloc_tclass_for_proc(pid
, NULL
);
468 lck_mtx_unlock(tclass_lock
);
473 tfp
->tfp_class
= tclass
;
474 tfp
->tfp_qos_mode
= so_tcdbg
->so_tcbbg_qos_mode
;
476 lck_mtx_unlock(tclass_lock
);
482 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
485 fp
= fdp
->fd_ofiles
[i
];
487 (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0 ||
488 FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_SOCKET
)
491 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
492 if (SOCK_DOM(so
) != PF_INET
&& SOCK_DOM(so
) != PF_INET6
)
496 if (tfp
->tfp_qos_mode
== QOS_MODE_MARKING_POLICY_ENABLE
)
497 so
->so_flags1
|= SOF1_QOSMARKING_ALLOWED
;
498 else if (tfp
->tfp_qos_mode
== QOS_MODE_MARKING_POLICY_DISABLE
)
499 so
->so_flags1
&= ~SOF1_QOSMARKING_ALLOWED
;
500 socket_unlock(so
, 1);
502 if (netsvctype
!= _NET_SERVICE_TYPE_UNSPEC
)
503 error
= sock_setsockopt(so
, SOL_SOCKET
,
504 SO_NET_SERVICE_TYPE
, &netsvctype
, sizeof(int));
505 if (tclass
!= SO_TC_UNSPEC
)
506 error
= sock_setsockopt(so
, SOL_SOCKET
,
507 SO_TRAFFIC_CLASS
, &tclass
, sizeof(int));
523 set_pname_tclass(struct so_tcdbg
*so_tcdbg
)
526 struct tclass_for_proc
*tfp
;
528 lck_mtx_lock(tclass_lock
);
530 tfp
= find_tfp_by_pname(so_tcdbg
->so_tcdbg_pname
);
532 tfp
= alloc_tclass_for_proc(-1, so_tcdbg
->so_tcdbg_pname
);
534 lck_mtx_unlock(tclass_lock
);
539 tfp
->tfp_class
= so_tcdbg
->so_tcdbg_tclass
;
540 tfp
->tfp_qos_mode
= so_tcdbg
->so_tcbbg_qos_mode
;
542 lck_mtx_unlock(tclass_lock
);
551 flush_pid_tclass(struct so_tcdbg
*so_tcdbg
)
553 pid_t pid
= so_tcdbg
->so_tcdbg_pid
;
554 int tclass
= so_tcdbg
->so_tcdbg_tclass
;
555 struct filedesc
*fdp
;
561 if (p
== PROC_NULL
) {
562 printf("%s proc_find(%d) failed\n", __func__
, pid
);
568 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
572 fp
= fdp
->fd_ofiles
[i
];
574 (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0 ||
575 FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_SOCKET
)
578 so
= (struct socket
*)fp
->f_fglob
->fg_data
;
579 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_FLUSH
, &tclass
,
582 printf("%s: setsockopt(SO_FLUSH) (so=0x%llx, fd=%d, "
583 "tclass=%d) failed %d\n", __func__
,
584 (uint64_t)VM_KERNEL_ADDRPERM(so
), i
, tclass
,
600 get_pid_tclass(struct so_tcdbg
*so_tcdbg
)
604 struct tclass_for_proc
*tfp
;
605 pid_t pid
= so_tcdbg
->so_tcdbg_pid
;
607 so_tcdbg
->so_tcdbg_tclass
= SO_TC_UNSPEC
; /* Means not set */
611 printf("%s proc_find(%d) failed\n", __func__
, pid
);
616 lck_mtx_lock(tclass_lock
);
618 tfp
= find_tfp_by_pid(pid
);
620 so_tcdbg
->so_tcdbg_tclass
= tfp
->tfp_class
;
621 so_tcdbg
->so_tcbbg_qos_mode
= tfp
->tfp_qos_mode
;
624 lck_mtx_unlock(tclass_lock
);
633 get_pname_tclass(struct so_tcdbg
*so_tcdbg
)
636 struct tclass_for_proc
*tfp
;
638 so_tcdbg
->so_tcdbg_tclass
= SO_TC_UNSPEC
; /* Means not set */
641 lck_mtx_lock(tclass_lock
);
643 tfp
= find_tfp_by_pname(so_tcdbg
->so_tcdbg_pname
);
645 so_tcdbg
->so_tcdbg_tclass
= tfp
->tfp_class
;
646 so_tcdbg
->so_tcbbg_qos_mode
= tfp
->tfp_qos_mode
;
649 lck_mtx_unlock(tclass_lock
);
655 delete_tclass_for_pid_pname(struct so_tcdbg
*so_tcdbg
)
658 pid_t pid
= so_tcdbg
->so_tcdbg_pid
;
659 struct tclass_for_proc
*tfp
= NULL
;
661 lck_mtx_lock(tclass_lock
);
664 tfp
= find_tfp_by_pid(pid
);
666 tfp
= find_tfp_by_pname(so_tcdbg
->so_tcdbg_pname
);
669 free_tclass_for_proc(tfp
);
673 lck_mtx_unlock(tclass_lock
);
679 * Setting options requires privileges
681 __private_extern__
int
682 so_set_tcdbg(struct socket
*so
, struct so_tcdbg
*so_tcdbg
)
686 if ((so
->so_state
& SS_PRIV
) == 0)
689 socket_unlock(so
, 0);
691 switch (so_tcdbg
->so_tcdbg_cmd
) {
693 error
= set_pid_tclass(so_tcdbg
);
697 error
= set_pname_tclass(so_tcdbg
);
701 error
= purge_tclass_for_proc();
705 error
= flush_tclass_for_proc();
708 case SO_TCDBG_DELETE
:
709 error
= delete_tclass_for_pid_pname(so_tcdbg
);
712 case SO_TCDBG_TCFLUSH_PID
:
713 error
= flush_pid_tclass(so_tcdbg
);
727 * Not required to be privileged to get
729 __private_extern__
int
730 sogetopt_tcdbg(struct socket
*so
, struct sockopt
*sopt
)
733 struct so_tcdbg so_tcdbg
;
735 size_t len
= sopt
->sopt_valsize
;
737 error
= sooptcopyin(sopt
, &so_tcdbg
, sizeof (struct so_tcdbg
),
738 sizeof (struct so_tcdbg
));
742 sopt
->sopt_valsize
= len
;
744 socket_unlock(so
, 0);
746 switch (so_tcdbg
.so_tcdbg_cmd
) {
748 error
= get_pid_tclass(&so_tcdbg
);
752 error
= get_pname_tclass(&so_tcdbg
);
756 lck_mtx_lock(tclass_lock
);
757 so_tcdbg
.so_tcdbg_count
= tfp_count
;
758 lck_mtx_unlock(tclass_lock
);
761 case SO_TCDBG_LIST
: {
762 struct tclass_for_proc
*tfp
;
764 struct so_tcdbg
*ptr
;
766 lck_mtx_lock(tclass_lock
);
767 if ((alloc_count
= tfp_count
) == 0) {
768 lck_mtx_unlock(tclass_lock
);
772 len
= alloc_count
* sizeof (struct so_tcdbg
);
773 lck_mtx_unlock(tclass_lock
);
775 buf
= _MALLOC(len
, M_TEMP
, M_WAITOK
| M_ZERO
);
781 lck_mtx_lock(tclass_lock
);
783 ptr
= (struct so_tcdbg
*)buf
;
784 TAILQ_FOREACH(tfp
, &tfp_head
, tfp_link
) {
785 if (++n
> alloc_count
)
787 if (tfp
->tfp_pid
!= -1) {
788 ptr
->so_tcdbg_cmd
= SO_TCDBG_PID
;
789 ptr
->so_tcdbg_pid
= tfp
->tfp_pid
;
791 ptr
->so_tcdbg_cmd
= SO_TCDBG_PNAME
;
792 ptr
->so_tcdbg_pid
= -1;
793 strlcpy(ptr
->so_tcdbg_pname
,
795 sizeof (ptr
->so_tcdbg_pname
));
797 ptr
->so_tcdbg_tclass
= tfp
->tfp_class
;
798 ptr
->so_tcbbg_qos_mode
= tfp
->tfp_qos_mode
;
802 lck_mtx_unlock(tclass_lock
);
815 error
= sooptcopyout(sopt
, &so_tcdbg
,
816 sizeof (struct so_tcdbg
));
818 error
= sooptcopyout(sopt
, buf
, len
);
825 #endif /* (DEVELOPMENT || DEBUG) */
828 so_get_netsvc_marking_level(struct socket
*so
)
830 int marking_level
= NETSVC_MRKNG_UNKNOWN
;
831 struct ifnet
*ifp
= NULL
;
833 switch (SOCK_DOM(so
)) {
835 struct inpcb
*inp
= sotoinpcb(so
);
838 ifp
= inp
->inp_last_outifp
;
842 struct in6pcb
*in6p
= sotoin6pcb(so
);
845 ifp
= in6p
->in6p_last_outifp
;
852 if ((ifp
->if_eflags
&
853 (IFEF_QOSMARKING_ENABLED
| IFEF_QOSMARKING_CAPABLE
)) ==
854 (IFEF_QOSMARKING_ENABLED
| IFEF_QOSMARKING_CAPABLE
)) {
855 if ((so
->so_flags1
& SOF1_QOSMARKING_ALLOWED
))
856 marking_level
= NETSVC_MRKNG_LVL_L3L2_ALL
;
858 marking_level
= NETSVC_MRKNG_LVL_L3L2_BK
;
860 marking_level
= NETSVC_MRKNG_LVL_L2
;
863 return (marking_level
);
866 __private_extern__
int
867 so_set_traffic_class(struct socket
*so
, int optval
)
871 if (optval
< SO_TC_BE
|| optval
> SO_TC_CTL
) {
885 if (!SO_VALID_TC(optval
))
891 int oldval
= so
->so_traffic_class
;
893 VERIFY(SO_VALID_TC(optval
));
894 so
->so_traffic_class
= optval
;
896 if ((SOCK_DOM(so
) == PF_INET
||
897 SOCK_DOM(so
) == PF_INET6
) &&
898 SOCK_TYPE(so
) == SOCK_STREAM
)
899 set_tcp_stream_priority(so
);
901 if ((SOCK_DOM(so
) == PF_INET
||
902 SOCK_DOM(so
) == PF_INET6
) &&
903 optval
!= oldval
&& (optval
== SO_TC_BK_SYS
||
904 oldval
== SO_TC_BK_SYS
)) {
906 * If the app switches from BK_SYS to something
907 * else, resume the socket if it was suspended.
909 if (oldval
== SO_TC_BK_SYS
)
910 inp_reset_fc_state(so
->so_pcb
);
912 SOTHROTTLELOG("throttle[%d]: so 0x%llx "
913 "[%d,%d] opportunistic %s\n", so
->last_pid
,
914 (uint64_t)VM_KERNEL_ADDRPERM(so
),
915 SOCK_DOM(so
), SOCK_TYPE(so
),
916 (optval
== SO_TC_BK_SYS
) ? "ON" : "OFF");
923 __private_extern__
int
924 so_set_net_service_type(struct socket
*so
, int netsvctype
)
929 if (!IS_VALID_NET_SERVICE_TYPE(netsvctype
))
932 sotc
= sotc_by_netservicetype
[netsvctype
];
933 error
= so_set_traffic_class(so
, sotc
);
936 so
->so_netsvctype
= netsvctype
;
937 so
->so_flags1
|= SOF1_TC_NET_SERV_TYPE
;
942 __private_extern__
void
943 so_set_default_traffic_class(struct socket
*so
)
945 so
->so_traffic_class
= SO_TC_BE
;
947 if ((SOCK_DOM(so
) == PF_INET
|| SOCK_DOM(so
) == PF_INET6
)) {
948 if (net_qos_policy_restricted
== 0)
949 so
->so_flags1
|= SOF1_QOSMARKING_ALLOWED
;
950 #if (DEVELOPMENT || DEBUG)
952 set_tclass_for_curr_proc(so
);
953 #endif /* (DEVELOPMENT || DEBUG) */
957 __private_extern__
int
958 so_set_opportunistic(struct socket
*so
, int optval
)
960 return (so_set_traffic_class(so
, (optval
== 0) ?
961 SO_TC_BE
: SO_TC_BK_SYS
));
964 __private_extern__
int
965 so_get_opportunistic(struct socket
*so
)
967 return (so
->so_traffic_class
== SO_TC_BK_SYS
);
970 __private_extern__
int
971 so_tc_from_control(struct mbuf
*control
, int *out_netsvctype
)
974 int sotc
= SO_TC_UNSPEC
;
976 *out_netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
978 for (cm
= M_FIRST_CMSGHDR(control
); cm
!= NULL
;
979 cm
= M_NXT_CMSGHDR(control
, cm
)) {
982 if (cm
->cmsg_len
< sizeof (struct cmsghdr
))
984 if (cm
->cmsg_level
!= SOL_SOCKET
||
985 cm
->cmsg_len
!= CMSG_LEN(sizeof(int)))
987 val
= *(int *)(void *)CMSG_DATA(cm
);
989 * The first valid option wins
991 switch (cm
->cmsg_type
) {
992 case SO_TRAFFIC_CLASS
:
993 if (SO_VALID_TC(val
)) {
997 } else if (val
< SO_TC_NET_SERVICE_OFFSET
) {
1001 * Handle the case SO_NET_SERVICE_TYPE values are
1002 * passed using SO_TRAFFIC_CLASS
1004 val
= val
- SO_TC_NET_SERVICE_OFFSET
;
1006 case SO_NET_SERVICE_TYPE
:
1007 if (!IS_VALID_NET_SERVICE_TYPE(val
))
1009 *out_netsvctype
= val
;
1010 sotc
= sotc_by_netservicetype
[val
];
1021 __private_extern__
void
1022 so_recv_data_stat(struct socket
*so
, struct mbuf
*m
, size_t off
)
1024 uint32_t mtc
= m_get_traffic_class(m
);
1026 if (mtc
>= SO_TC_STATS_MAX
)
1029 so
->so_tc_stats
[mtc
].rxpackets
+= 1;
1030 so
->so_tc_stats
[mtc
].rxbytes
+=
1031 ((m
->m_flags
& M_PKTHDR
) ? m
->m_pkthdr
.len
: 0) + off
;
1034 __private_extern__
void
1035 so_inc_recv_data_stat(struct socket
*so
, size_t pkts
, size_t bytes
,
1038 if (mtc
>= SO_TC_STATS_MAX
)
1041 so
->so_tc_stats
[mtc
].rxpackets
+= pkts
;
1042 so
->so_tc_stats
[mtc
].rxbytes
+= bytes
;
1046 so_throttle_best_effort(struct socket
*so
, struct ifnet
*ifp
)
1048 u_int32_t uptime
= net_uptime();
1049 return (soissrcbesteffort(so
) &&
1050 net_io_policy_throttle_best_effort
== 1 &&
1051 ifp
->if_rt_sendts
> 0 &&
1052 (int)(uptime
- ifp
->if_rt_sendts
) <= TCP_BG_SWITCH_TIME
);
1055 __private_extern__
void
1056 set_tcp_stream_priority(struct socket
*so
)
1058 struct inpcb
*inp
= sotoinpcb(so
);
1059 struct tcpcb
*tp
= intotcpcb(inp
);
1060 struct ifnet
*outifp
;
1061 u_char old_cc
= tp
->tcp_cc_index
;
1062 int recvbg
= IS_TCP_RECV_BG(so
);
1063 bool is_local
= false, fg_active
= false;
1066 VERIFY((SOCK_CHECK_DOM(so
, PF_INET
) ||
1067 SOCK_CHECK_DOM(so
, PF_INET6
)) &&
1068 SOCK_CHECK_TYPE(so
, SOCK_STREAM
) &&
1069 SOCK_CHECK_PROTO(so
, IPPROTO_TCP
));
1071 /* Return if the socket is in a terminal state */
1072 if (inp
->inp_state
== INPCB_STATE_DEAD
)
1075 outifp
= inp
->inp_last_outifp
;
1076 uptime
= net_uptime();
1079 * If the socket was marked as a background socket or if the
1080 * traffic class is set to background with traffic class socket
1081 * option then make both send and recv side of the stream to be
1082 * background. The variable sotcdb which can be set with sysctl
1083 * is used to disable these settings for testing.
1085 if (outifp
== NULL
|| (outifp
->if_flags
& IFF_LOOPBACK
))
1088 /* Check if there has been recent foreground activity */
1089 if (outifp
!= NULL
) {
1091 * If the traffic source is background, check if
1092 * if it can be switched to foreground. This can
1093 * happen when there is no indication of foreground
1096 if (soissrcbackground(so
) &&
1097 ((outifp
->if_fg_sendts
> 0 &&
1098 (int)(uptime
- outifp
->if_fg_sendts
) <=
1099 TCP_BG_SWITCH_TIME
) || net_io_policy_throttled
))
1103 * The traffic source is best-effort -- check if
1104 * the policy to throttle best effort is enabled
1105 * and there was realtime activity on this
1106 * interface recently. If this is true, enable
1107 * algorithms that respond to increased latency
1108 * on best-effort traffic.
1110 if (so_throttle_best_effort(so
, outifp
))
1115 * System initiated background traffic like cloud uploads should
1116 * always use background delay sensitive algorithms. This will
1117 * make the stream more responsive to other streams on the user's
1118 * network and it will minimize latency induced.
1120 if (fg_active
|| IS_SO_TC_BACKGROUNDSYSTEM(so
->so_traffic_class
)) {
1122 * If the interface that the connection is using is
1123 * loopback, do not use background congestion
1124 * control algorithm.
1126 * If there has been recent foreground activity or if
1127 * there was an indication that a foreground application
1128 * is going to use networking (net_io_policy_throttled),
1129 * switch the backgroung streams to use background
1130 * congestion control algorithm. Otherwise, even background
1131 * flows can move into foreground.
1133 if ((sotcdb
& SOTCDB_NO_SENDTCPBG
) != 0 || is_local
||
1134 !IS_SO_TC_BACKGROUNDSYSTEM(so
->so_traffic_class
)) {
1135 if (old_cc
== TCP_CC_ALGO_BACKGROUND_INDEX
)
1136 tcp_set_foreground_cc(so
);
1138 if (old_cc
!= TCP_CC_ALGO_BACKGROUND_INDEX
)
1139 tcp_set_background_cc(so
);
1142 /* Set receive side background flags */
1143 if ((sotcdb
& SOTCDB_NO_RECVTCPBG
) != 0 || is_local
||
1144 !IS_SO_TC_BACKGROUNDSYSTEM(so
->so_traffic_class
)) {
1145 tcp_clear_recv_bg(so
);
1147 tcp_set_recv_bg(so
);
1150 tcp_clear_recv_bg(so
);
1151 if (old_cc
== TCP_CC_ALGO_BACKGROUND_INDEX
)
1152 tcp_set_foreground_cc(so
);
1155 if (old_cc
!= tp
->tcp_cc_index
|| recvbg
!= IS_TCP_RECV_BG(so
)) {
1156 SOTHROTTLELOG("throttle[%d]: so 0x%llx [%d,%d] TCP %s send; "
1157 "%s recv\n", so
->last_pid
,
1158 (uint64_t)VM_KERNEL_ADDRPERM(so
),
1159 SOCK_DOM(so
), SOCK_TYPE(so
),
1160 (tp
->tcp_cc_index
== TCP_CC_ALGO_BACKGROUND_INDEX
) ?
1161 "background" : "foreground",
1162 IS_TCP_RECV_BG(so
) ? "background" : "foreground");
1167 * Set traffic class to an IPv4 or IPv6 packet
1169 * - set the DSCP code following the WMM mapping
1171 __private_extern__
void
1172 set_packet_service_class(struct mbuf
*m
, struct socket
*so
,
1173 int sotc
, u_int32_t flags
)
1175 mbuf_svc_class_t msc
= MBUF_SC_BE
; /* Best effort by default */
1176 struct inpcb
*inp
= sotoinpcb(so
); /* in6pcb and inpcb are the same */
1178 if (!(m
->m_flags
& M_PKTHDR
))
1182 * Here is the precedence:
1183 * 1) TRAFFIC_MGT_SO_BACKGROUND trumps all
1184 * 2) Traffic class passed via ancillary data to sendmsdg(2)
1185 * 3) Traffic class socket option last
1187 if (sotc
!= SO_TC_UNSPEC
) {
1188 VERIFY(SO_VALID_TC(sotc
));
1189 msc
= so_tc2msc(sotc
);
1190 /* Assert because tc must have been valid */
1191 VERIFY(MBUF_VALID_SC(msc
));
1195 * If TRAFFIC_MGT_SO_BACKGROUND is set or policy to throttle
1196 * best effort is set, depress the priority.
1198 if (!IS_MBUF_SC_BACKGROUND(msc
) && soisthrottled(so
))
1201 if (IS_MBUF_SC_BESTEFFORT(msc
) && inp
->inp_last_outifp
!= NULL
&&
1202 so_throttle_best_effort(so
, inp
->inp_last_outifp
))
1205 if (soissrcbackground(so
))
1206 m
->m_pkthdr
.pkt_flags
|= PKTF_SO_BACKGROUND
;
1208 if (soissrcrealtime(so
) || IS_MBUF_SC_REALTIME(msc
))
1209 m
->m_pkthdr
.pkt_flags
|= PKTF_SO_REALTIME
;
1211 * Set the traffic class in the mbuf packet header svc field
1213 if (sotcdb
& SOTCDB_NO_MTC
)
1217 * Elevate service class if the packet is a pure TCP ACK.
1218 * We can do this only when the flow is not a background
1219 * flow and the outgoing interface supports
1220 * transmit-start model.
1222 if (!IS_MBUF_SC_BACKGROUND(msc
) &&
1223 (flags
& (PKT_SCF_TCP_ACK
| PKT_SCF_TCP_SYN
)) != 0)
1226 (void) m_set_service_class(m
, msc
);
1229 * Set the privileged traffic auxiliary flag if applicable,
1232 if (!(sotcdb
& SOTCDB_NO_PRIVILEGED
) && soisprivilegedtraffic(so
) &&
1233 msc
!= MBUF_SC_UNSPEC
)
1234 m
->m_pkthdr
.pkt_flags
|= PKTF_PRIO_PRIVILEGED
;
1236 m
->m_pkthdr
.pkt_flags
&= ~PKTF_PRIO_PRIVILEGED
;
1240 * For TCP with background traffic class switch CC algo based on sysctl
1242 if (so
->so_type
== SOCK_STREAM
)
1243 set_tcp_stream_priority(so
);
1245 so_tc_update_stats(m
, so
, msc
);
1248 __private_extern__
void
1249 so_tc_update_stats(struct mbuf
*m
, struct socket
*so
, mbuf_svc_class_t msc
)
1251 mbuf_traffic_class_t mtc
;
1254 * Assume socket and mbuf traffic class values are the same
1255 * Also assume the socket lock is held. Note that the stats
1256 * at the socket layer are reduced down to the legacy traffic
1257 * classes; we could/should potentially expand so_tc_stats[].
1259 mtc
= MBUF_SC2TC(msc
);
1260 VERIFY(mtc
< SO_TC_STATS_MAX
);
1261 so
->so_tc_stats
[mtc
].txpackets
+= 1;
1262 so
->so_tc_stats
[mtc
].txbytes
+= m
->m_pkthdr
.len
;
1265 __private_extern__
void
1266 socket_tclass_init(void)
1268 _CASSERT(_SO_TC_MAX
== SO_TC_STATS_MAX
);
1270 tclass_lck_grp_attr
= lck_grp_attr_alloc_init();
1271 tclass_lck_grp
= lck_grp_alloc_init("tclass", tclass_lck_grp_attr
);
1272 tclass_lck_attr
= lck_attr_alloc_init();
1273 lck_mtx_init(tclass_lock
, tclass_lck_grp
, tclass_lck_attr
);
1276 __private_extern__ mbuf_svc_class_t
1279 mbuf_svc_class_t msc
;
1283 msc
= MBUF_SC_BK_SYS
;
1317 msc
= MBUF_SC_UNSPEC
;
1324 __private_extern__
int
1325 so_svc2tc(mbuf_svc_class_t svc
)
1328 case MBUF_SC_BK_SYS
:
1329 return (SO_TC_BK_SYS
);
1348 case MBUF_SC_UNSPEC
:
1355 * LRO is turned on for AV streaming class.
1358 so_set_lro(struct socket
*so
, int optval
)
1360 if (optval
== SO_TC_AV
) {
1361 so
->so_flags
|= SOF_USELRO
;
1363 if (so
->so_flags
& SOF_USELRO
) {
1364 /* transition to non LRO class */
1365 so
->so_flags
&= ~SOF_USELRO
;
1366 struct inpcb
*inp
= sotoinpcb(so
);
1367 struct tcpcb
*tp
= NULL
;
1369 tp
= intotcpcb(inp
);
1370 if (tp
&& (tp
->t_flagsext
& TF_LRO_OFFLOADED
)) {
1371 tcp_lro_remove_state(inp
->inp_laddr
,
1375 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
1383 sotc_index(int sotc
)
1387 return (SOTCIX_BK_SYS
);
1397 return (SOTCIX_OAM
);
1411 return (SOTCIX_CTL
);
1417 * Unknown traffic class value
1419 return (SIZE_T_MAX
);
1423 * Pass NULL ifp for default map
1426 set_netsvctype_dscp_map(size_t in_count
,
1427 const struct netsvctype_dscp_map
*netsvctype_dscp_map
)
1430 struct net_qos_dscp_map
*net_qos_dscp_map
= NULL
;
1434 * Do not accept more that max number of distinct DSCPs
1436 if (in_count
> _MAX_DSCP
|| netsvctype_dscp_map
== NULL
)
1440 * Validate input parameters
1442 for (i
= 0; i
< in_count
; i
++) {
1443 if (!IS_VALID_NET_SERVICE_TYPE(netsvctype_dscp_map
[i
].netsvctype
))
1445 if (netsvctype_dscp_map
[i
].dscp
> _MAX_DSCP
)
1449 net_qos_dscp_map
= &default_net_qos_dscp_map
;
1451 for (i
= 0; i
< in_count
; i
++) {
1452 netsvctype
= netsvctype_dscp_map
[i
].netsvctype
;
1454 net_qos_dscp_map
->netsvctype_to_dscp
[netsvctype
] =
1455 netsvctype_dscp_map
[i
].dscp
;
1457 for (netsvctype
= 0; netsvctype
< _NET_SERVICE_TYPE_COUNT
; netsvctype
++) {
1458 switch (netsvctype
) {
1459 case NET_SERVICE_TYPE_BE
:
1460 case NET_SERVICE_TYPE_BK
:
1461 case NET_SERVICE_TYPE_VI
:
1462 case NET_SERVICE_TYPE_VO
:
1463 case NET_SERVICE_TYPE_RV
:
1464 case NET_SERVICE_TYPE_AV
:
1465 case NET_SERVICE_TYPE_OAM
:
1466 case NET_SERVICE_TYPE_RD
: {
1469 sotcix
= sotc_index(sotc_by_netservicetype
[netsvctype
]);
1470 net_qos_dscp_map
->sotc_to_dscp
[sotcix
] =
1471 netsvctype_dscp_map
[netsvctype
].dscp
;
1474 case NET_SERVICE_TYPE_SIG
:
1475 /* Signaling does not have its own traffic class */
1478 /* We should not be here */
1482 /* Network control socket traffic class is always best effort */
1483 net_qos_dscp_map
->sotc_to_dscp
[SOTCIX_CTL
] = _DSCP_DF
;
1485 /* Backround socket traffic class DSCP same as backround system */
1486 net_qos_dscp_map
->sotc_to_dscp
[SOTCIX_BK
] =
1487 net_qos_dscp_map
->sotc_to_dscp
[SOTCIX_BK_SYS
];
1493 * out_count is an input/ouput parameter
1496 get_netsvctype_dscp_map(size_t *out_count
,
1497 struct netsvctype_dscp_map
*netsvctype_dscp_map
)
1500 struct net_qos_dscp_map
*net_qos_dscp_map
= NULL
;
1503 * Do not accept more that max number of distinct DSCPs
1505 if (out_count
== NULL
|| netsvctype_dscp_map
== NULL
)
1507 if (*out_count
> _MAX_DSCP
)
1510 net_qos_dscp_map
= &default_net_qos_dscp_map
;
1512 for (i
= 0; i
< MIN(_NET_SERVICE_TYPE_COUNT
, *out_count
); i
++) {
1513 netsvctype_dscp_map
[i
].netsvctype
= i
;
1514 netsvctype_dscp_map
[i
].dscp
= net_qos_dscp_map
->netsvctype_to_dscp
[i
];
1528 * By default use the Fastlane DSCP mappngs
1530 error
= set_netsvctype_dscp_map(_NET_SERVICE_TYPE_COUNT
,
1531 fastlane_netsvctype_dscp_map
);
1535 * No DSCP mapping for network control
1537 default_net_qos_dscp_map
.sotc_to_dscp
[SOTCIX_CTL
] = _DSCP_DF
;
1539 set_dscp_to_wifi_ac_map(default_dscp_to_wifi_ac_map
, 1);
1543 sysctl_default_netsvctype_to_dscp_map SYSCTL_HANDLER_ARGS
1545 #pragma unused(oidp, arg1, arg2)
1547 const size_t max_netsvctype_to_dscp_map_len
=
1548 _NET_SERVICE_TYPE_COUNT
* sizeof(struct netsvctype_dscp_map
);
1550 struct netsvctype_dscp_map netsvctype_dscp_map
[_NET_SERVICE_TYPE_COUNT
];
1553 if (req
->oldptr
== USER_ADDR_NULL
) {
1555 _NET_SERVICE_TYPE_COUNT
* sizeof(struct netsvctype_dscp_map
);
1556 } else if (req
->oldlen
> 0) {
1557 count
= _NET_SERVICE_TYPE_COUNT
;
1558 error
= get_netsvctype_dscp_map(&count
, netsvctype_dscp_map
);
1561 len
= count
* sizeof(struct netsvctype_dscp_map
);
1562 error
= SYSCTL_OUT(req
, netsvctype_dscp_map
,
1563 MIN(len
, req
->oldlen
));
1568 if (req
->newptr
== USER_ADDR_NULL
)
1571 error
= proc_suser(current_proc());
1576 * Check input length
1578 if (req
->newlen
> max_netsvctype_to_dscp_map_len
) {
1583 * Cap the number of entries to copy from input buffer
1585 error
= SYSCTL_IN(req
, netsvctype_dscp_map
, req
->newlen
);
1589 count
= req
->newlen
/ sizeof(struct netsvctype_dscp_map
);
1590 error
= set_netsvctype_dscp_map(count
, netsvctype_dscp_map
);
1595 __private_extern__ errno_t
1596 set_packet_qos(struct mbuf
*m
, struct ifnet
*ifp
, boolean_t qos_allowed
,
1597 int sotc
, int netsvctype
, u_int8_t
*dscp_inout
)
1599 if (ifp
== NULL
|| dscp_inout
== NULL
)
1602 if ((ifp
->if_eflags
&
1603 (IFEF_QOSMARKING_ENABLED
| IFEF_QOSMARKING_CAPABLE
)) ==
1604 (IFEF_QOSMARKING_ENABLED
| IFEF_QOSMARKING_CAPABLE
)) {
1608 * When on a Fastlane network, IP_TOS/IPV6_TCLASS are no-ops
1613 * For DSCP use the network service type is specified, otherwise
1614 * use the socket traffic class
1616 * When not whitelisted by the policy, set DSCP only for best
1617 * effort and background, and set the mbuf service class to
1618 * best effort as well so the packet will be queued and
1619 * scheduled at a lower priority.
1620 * We still want to prioritize control traffic on the interface
1621 * so we do not change the mbuf service class for SO_TC_CTL
1623 if (netsvctype
!= _NET_SERVICE_TYPE_UNSPEC
&&
1624 netsvctype
!= NET_SERVICE_TYPE_BE
) {
1625 dscp
= default_net_qos_dscp_map
.netsvctype_to_dscp
[netsvctype
];
1627 if (qos_allowed
== FALSE
&&
1628 netsvctype
!= NET_SERVICE_TYPE_BE
&&
1629 netsvctype
!= NET_SERVICE_TYPE_BK
) {
1631 if (sotc
!= SO_TC_CTL
)
1632 m_set_service_class(m
, MBUF_SC_BE
);
1635 size_t sotcix
= sotc_index(sotc
);
1637 dscp
= default_net_qos_dscp_map
.sotc_to_dscp
[sotcix
];
1639 if (qos_allowed
== FALSE
&& sotc
!= SO_TC_BE
&&
1640 sotc
!= SO_TC_BK
&& sotc
!= SO_TC_BK_SYS
&&
1641 sotc
!= SO_TC_CTL
) {
1643 if (sotc
!= SO_TC_CTL
)
1644 m_set_service_class(m
, MBUF_SC_BE
);
1647 if (net_qos_verbose
!= 0)
1648 printf("%s qos_allowed %d sotc %u netsvctype %u dscp %u\n",
1649 __func__
, qos_allowed
, sotc
, netsvctype
, dscp
);
1651 if (*dscp_inout
!= dscp
) {
1654 } else if (*dscp_inout
!= _DSCP_DF
&& IFNET_IS_WIFI_INFRA(ifp
)) {
1655 mbuf_svc_class_t msc
= m_get_service_class(m
);
1658 * For WiFi infra, when the mbuf service class is best effort
1659 * and the DSCP is not default, set the service class based
1662 if (msc
== MBUF_SC_BE
) {
1663 msc
= wifi_dscp_to_msc_array
[*dscp_inout
];
1665 if (msc
!= MBUF_SC_BE
) {
1666 m_set_service_class(m
, msc
);
1668 if (net_qos_verbose
!= 0)
1669 printf("%s set msc %u for dscp %u\n",
1670 __func__
, msc
, *dscp_inout
);
1679 set_dscp_to_wifi_ac_map(const struct dcsp_msc_map
*map
, int clear
)
1684 bzero(wifi_dscp_to_msc_array
, sizeof(wifi_dscp_to_msc_array
));
1686 for (i
= 0; i
< DSCP_ARRAY_SIZE
; i
++) {
1687 const struct dcsp_msc_map
*elem
= map
+ i
;
1689 if (elem
->dscp
> _MAX_DSCP
|| elem
->msc
== MBUF_SC_UNSPEC
)
1691 switch (elem
->msc
) {
1692 case MBUF_SC_BK_SYS
:
1694 wifi_dscp_to_msc_array
[elem
->dscp
] = MBUF_SC_BK
;
1700 wifi_dscp_to_msc_array
[elem
->dscp
] = MBUF_SC_BE
;
1705 wifi_dscp_to_msc_array
[elem
->dscp
] = MBUF_SC_VI
;
1709 wifi_dscp_to_msc_array
[elem
->dscp
] = MBUF_SC_VO
;
1716 dscp_msc_map_from_netsvctype_dscp_map(struct netsvctype_dscp_map
*netsvctype_dscp_map
,
1717 size_t count
, struct dcsp_msc_map
*dcsp_msc_map
)
1723 * Validate input parameters
1725 for (i
= 0; i
< count
; i
++) {
1726 if (!SO_VALID_TC(netsvctype_dscp_map
[i
].netsvctype
)) {
1730 if (netsvctype_dscp_map
[i
].dscp
> _MAX_DSCP
) {
1736 bzero(dcsp_msc_map
, DSCP_ARRAY_SIZE
* sizeof(struct dcsp_msc_map
));
1738 for (i
= 0; i
< count
; i
++) {
1739 dcsp_msc_map
[i
].dscp
= netsvctype_dscp_map
[i
].dscp
;
1740 dcsp_msc_map
[i
].msc
= so_tc2msc(netsvctype_dscp_map
[i
].netsvctype
);
1747 sysctl_dscp_to_wifi_ac_map SYSCTL_HANDLER_ARGS
1749 #pragma unused(oidp, arg1, arg2)
1751 size_t len
= DSCP_ARRAY_SIZE
* sizeof(struct netsvctype_dscp_map
);
1752 struct netsvctype_dscp_map netsvctype_dscp_map
[DSCP_ARRAY_SIZE
];
1753 struct dcsp_msc_map dcsp_msc_map
[DSCP_ARRAY_SIZE
];
1757 if (req
->oldptr
== USER_ADDR_NULL
) {
1759 } else if (req
->oldlen
> 0) {
1760 for (i
= 0; i
< DSCP_ARRAY_SIZE
; i
++) {
1761 netsvctype_dscp_map
[i
].dscp
= i
;
1762 netsvctype_dscp_map
[i
].netsvctype
=
1763 so_svc2tc(wifi_dscp_to_msc_array
[i
]);
1765 error
= SYSCTL_OUT(req
, netsvctype_dscp_map
,
1766 MIN(len
, req
->oldlen
));
1771 if (req
->newptr
== USER_ADDR_NULL
)
1774 error
= proc_suser(current_proc());
1779 * Check input length
1781 if (req
->newlen
> len
) {
1786 * Cap the number of entries to copy from input buffer
1788 if (len
> req
->newlen
)
1790 error
= SYSCTL_IN(req
, netsvctype_dscp_map
, len
);
1794 count
= len
/ sizeof(struct netsvctype_dscp_map
);
1795 bzero(dcsp_msc_map
, sizeof(dcsp_msc_map
));
1796 error
= dscp_msc_map_from_netsvctype_dscp_map(netsvctype_dscp_map
, count
,
1801 set_dscp_to_wifi_ac_map(dcsp_msc_map
, 0);
1807 sysctl_reset_dscp_to_wifi_ac_map SYSCTL_HANDLER_ARGS
1809 #pragma unused(oidp, arg1, arg2)
1813 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
1814 if (error
|| !req
->newptr
)
1817 set_dscp_to_wifi_ac_map(default_dscp_to_wifi_ac_map
, 1);