2 * Copyright (c) 2016-2017 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/cdefs.h>
30 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
36 #include <sys/syslog.h>
38 #include <sys/errno.h>
39 #include <sys/kernel.h>
40 #include <sys/kauth.h>
41 #include <kern/zalloc.h>
42 #include <netinet/in.h>
44 #include <net/classq/classq.h>
45 #include <net/classq/if_classq.h>
46 #include <net/pktsched/pktsched.h>
47 #include <net/pktsched/pktsched_fq_codel.h>
48 #include <net/classq/classq_fq_codel.h>
50 static struct zone
*flowq_zone
= NULL
;
52 #define FQ_ZONE_MAX (32 * 1024) /* across all interfaces */
54 #define DTYPE_NODROP 0 /* no drop */
55 #define DTYPE_FORCED 1 /* a "forced" drop */
56 #define DTYPE_EARLY 2 /* an "unforced" (early) drop */
61 if (flowq_zone
!= NULL
)
64 flowq_zone
= zinit(sizeof (struct flowq
),
65 FQ_ZONE_MAX
* sizeof (struct flowq
), 0, "flowq_zone");
66 if (flowq_zone
== NULL
) {
67 panic("%s: failed to allocate flowq_zone", __func__
);
70 zone_change(flowq_zone
, Z_EXPAND
, TRUE
);
71 zone_change(flowq_zone
, Z_CALLERACCT
, TRUE
);
75 fq_alloc(classq_pkt_type_t ptype
)
78 fq
= zalloc(flowq_zone
);
80 log(LOG_ERR
, "%s: unable to allocate from flowq_zone\n");
84 bzero(fq
, sizeof (*fq
));
86 if (ptype
== QP_MBUF
) {
87 MBUFQ_INIT(&fq
->fq_mbufq
);
96 VERIFY(!(fq
->fq_flags
& (FQF_NEW_FLOW
| FQF_OLD_FLOW
)));
97 VERIFY(fq
->fq_bytes
== 0);
98 zfree(flowq_zone
, fq
);
102 fq_detect_dequeue_stall(fq_if_t
*fqs
, fq_t
*flowq
, fq_if_classq_t
*fq_cl
,
105 u_int64_t maxgetqtime
;
106 if (FQ_IS_DELAYHIGH(flowq
) || flowq
->fq_getqtime
== 0 ||
108 flowq
->fq_bytes
< FQ_MIN_FC_THRESHOLD_BYTES
)
110 maxgetqtime
= flowq
->fq_getqtime
+ fqs
->fqs_update_interval
;
111 if ((*now
) > maxgetqtime
) {
113 * there was no dequeue in an update interval worth of
114 * time. It means that the queue is stalled.
116 FQ_SET_DELAY_HIGH(flowq
);
117 fq_cl
->fcl_stat
.fcl_dequeue_stall
++;
122 fq_head_drop(fq_if_t
*fqs
, fq_t
*fq
)
126 uint64_t *pkt_timestamp
;
127 struct ifclassq
*ifq
= fqs
->fqs_ifq
;
129 _PKTSCHED_PKT_INIT(&pkt
);
130 if (fq_getq_flow_internal(fqs
, fq
, &pkt
) == NULL
)
133 pktsched_get_pkt_vars(&pkt
, &pkt_flags
, &pkt_timestamp
, NULL
, NULL
,
137 if (pkt
.pktsched_ptype
== QP_MBUF
)
138 *pkt_flags
&= ~PKTF_PRIV_GUARDED
;
140 IFCQ_DROP_ADD(ifq
, 1, pktsched_get_pkt_len(&pkt
));
141 IFCQ_CONVERT_LOCK(ifq
);
142 pktsched_free_pkt(&pkt
);
146 fq_addq(fq_if_t
*fqs
, pktsched_pkt_t
*pkt
, fq_if_classq_t
*fq_cl
)
148 int droptype
= DTYPE_NODROP
, fc_adv
= 0, ret
= CLASSQEQ_SUCCESS
;
151 uint64_t *pkt_timestamp
;
153 uint32_t pkt_flowid
, pkt_tx_start_seq
;
154 uint8_t pkt_proto
, pkt_flowsrc
;
156 pktsched_get_pkt_vars(pkt
, &pkt_flags
, &pkt_timestamp
, &pkt_flowid
,
157 &pkt_flowsrc
, &pkt_proto
, &pkt_tx_start_seq
);
159 if (pkt
->pktsched_ptype
== QP_MBUF
) {
160 /* See comments in <rdar://problem/14040693> */
161 VERIFY(!(*pkt_flags
& PKTF_PRIV_GUARDED
));
162 *pkt_flags
|= PKTF_PRIV_GUARDED
;
165 if (*pkt_timestamp
> 0) {
166 now
= *pkt_timestamp
;
168 struct timespec now_ts
;
170 now
= (now_ts
.tv_sec
* NSEC_PER_SEC
) + now_ts
.tv_nsec
;
171 *pkt_timestamp
= now
;
174 /* find the flowq for this packet */
175 fq
= fq_if_hash_pkt(fqs
, pkt_flowid
, pktsched_get_pkt_svc(pkt
),
176 now
, TRUE
, pkt
->pktsched_ptype
);
178 /* drop the packet if we could not allocate a flow queue */
179 fq_cl
->fcl_stat
.fcl_drop_memfailure
++;
180 IFCQ_CONVERT_LOCK(fqs
->fqs_ifq
);
181 return (CLASSQEQ_DROP
);
183 VERIFY(fq
->fq_ptype
== pkt
->pktsched_ptype
);
185 fq_detect_dequeue_stall(fqs
, fq
, fq_cl
, &now
);
187 if (FQ_IS_DELAYHIGH(fq
)) {
188 if ((fq
->fq_flags
& FQF_FLOWCTL_CAPABLE
) &&
189 (*pkt_flags
& PKTF_FLOW_ADV
)) {
192 * If the flow is suspended or it is not
193 * TCP, drop the packet
195 if (pkt_proto
!= IPPROTO_TCP
) {
196 droptype
= DTYPE_EARLY
;
197 fq_cl
->fcl_stat
.fcl_drop_early
++;
201 * Need to drop a packet, instead of dropping this
202 * one, try to drop from the head of the queue
205 fq_head_drop(fqs
, fq
);
206 droptype
= DTYPE_NODROP
;
208 droptype
= DTYPE_EARLY
;
210 fq_cl
->fcl_stat
.fcl_drop_early
++;
215 /* Set the return code correctly */
216 if (fc_adv
== 1 && droptype
!= DTYPE_FORCED
) {
217 if (fq_if_add_fcentry(fqs
, pkt
, pkt_flowid
, pkt_flowsrc
,
219 fq
->fq_flags
|= FQF_FLOWCTL_ON
;
220 /* deliver flow control advisory error */
221 if (droptype
== DTYPE_NODROP
) {
222 ret
= CLASSQEQ_SUCCESS_FC
;
224 /* dropped due to flow control */
225 ret
= CLASSQEQ_DROP_FC
;
229 * if we could not flow control the flow, it is
232 droptype
= DTYPE_FORCED
;
233 ret
= CLASSQEQ_DROP_FC
;
234 fq_cl
->fcl_stat
.fcl_flow_control_fail
++;
239 * If the queue length hits the queue limit, drop a packet from the
240 * front of the queue for a flow with maximum number of bytes. This
241 * will penalize heavy and unresponsive flows. It will also avoid a
244 if (droptype
== DTYPE_NODROP
&& fq_if_at_drop_limit(fqs
)) {
245 if (fqs
->fqs_large_flow
== fq
) {
247 * Drop from the head of the current fq. Since a
248 * new packet will be added to the tail, it is ok
249 * to leave fq in place.
251 fq_head_drop(fqs
, fq
);
253 if (fqs
->fqs_large_flow
== NULL
) {
254 droptype
= DTYPE_FORCED
;
255 fq_cl
->fcl_stat
.fcl_drop_overflow
++;
259 * if this fq was freshly created and there
260 * is nothing to enqueue, free it
262 if (fq_empty(fq
) && !(fq
->fq_flags
&
263 (FQF_NEW_FLOW
| FQF_OLD_FLOW
))) {
264 fq_if_destroy_flow(fqs
, fq_cl
, fq
);
268 fq_if_drop_packet(fqs
);
273 if (droptype
== DTYPE_NODROP
) {
274 uint32_t pkt_len
= pktsched_get_pkt_len(pkt
);
275 fq_enqueue(fq
, pkt
->pktsched_pkt
);
276 fq
->fq_bytes
+= pkt_len
;
277 fq_cl
->fcl_stat
.fcl_byte_cnt
+= pkt_len
;
278 fq_cl
->fcl_stat
.fcl_pkt_cnt
++;
281 * check if this queue will qualify to be the next
284 fq_if_is_flow_heavy(fqs
, fq
);
286 IFCQ_CONVERT_LOCK(fqs
->fqs_ifq
);
287 return ((ret
!= CLASSQEQ_SUCCESS
) ? ret
: CLASSQEQ_DROP
);
291 * If the queue is not currently active, add it to the end of new
292 * flows list for that service class.
294 if ((fq
->fq_flags
& (FQF_NEW_FLOW
|FQF_OLD_FLOW
)) == 0) {
295 VERIFY(STAILQ_NEXT(fq
, fq_actlink
) == NULL
);
296 STAILQ_INSERT_TAIL(&fq_cl
->fcl_new_flows
, fq
, fq_actlink
);
297 fq
->fq_flags
|= FQF_NEW_FLOW
;
299 fq_cl
->fcl_stat
.fcl_newflows_cnt
++;
301 fq
->fq_deficit
= fq_cl
->fcl_quantum
;
307 fq_getq_flow_internal(fq_if_t
*fqs
, fq_t
*fq
, pktsched_pkt_t
*pkt
)
311 fq_if_classq_t
*fq_cl
;
312 struct ifclassq
*ifq
= fqs
->fqs_ifq
;
318 pktsched_pkt_encap(pkt
, fq
->fq_ptype
, p
);
319 plen
= pktsched_get_pkt_len(pkt
);
321 VERIFY(fq
->fq_bytes
>= plen
);
322 fq
->fq_bytes
-= plen
;
324 fq_cl
= &fqs
->fqs_classq
[fq
->fq_sc_index
];
325 fq_cl
->fcl_stat
.fcl_byte_cnt
-= plen
;
326 fq_cl
->fcl_stat
.fcl_pkt_cnt
--;
328 IFCQ_DEC_BYTES(ifq
, plen
);
330 /* Reset getqtime so that we don't count idle times */
338 fq_getq_flow(fq_if_t
*fqs
, fq_t
*fq
, pktsched_pkt_t
*pkt
)
341 fq_if_classq_t
*fq_cl
;
344 struct timespec now_ts
;
345 uint32_t *pkt_flags
, pkt_tx_start_seq
;
346 uint64_t *pkt_timestamp
;
348 p
= fq_getq_flow_internal(fqs
, fq
, pkt
);
352 pktsched_get_pkt_vars(pkt
, &pkt_flags
, &pkt_timestamp
, NULL
, NULL
,
353 NULL
, &pkt_tx_start_seq
);
356 now
= (now_ts
.tv_sec
* NSEC_PER_SEC
) + now_ts
.tv_nsec
;
358 /* this will compute qdelay in nanoseconds */
359 if (now
> *pkt_timestamp
)
360 qdelay
= now
- *pkt_timestamp
;
361 fq_cl
= &fqs
->fqs_classq
[fq
->fq_sc_index
];
363 if (fq
->fq_min_qdelay
== 0 ||
364 (qdelay
> 0 && (u_int64_t
)qdelay
< fq
->fq_min_qdelay
))
365 fq
->fq_min_qdelay
= qdelay
;
366 if (now
>= fq
->fq_updatetime
) {
367 if (fq
->fq_min_qdelay
> fqs
->fqs_target_qdelay
) {
368 if (!FQ_IS_DELAYHIGH(fq
))
369 FQ_SET_DELAY_HIGH(fq
);
371 FQ_CLEAR_DELAY_HIGH(fq
);
375 /* Reset measured queue delay and update time */
376 fq
->fq_updatetime
= now
+ fqs
->fqs_update_interval
;
377 fq
->fq_min_qdelay
= 0;
379 if (!FQ_IS_DELAYHIGH(fq
) || fq_empty(fq
)) {
380 FQ_CLEAR_DELAY_HIGH(fq
);
381 if (fq
->fq_flags
& FQF_FLOWCTL_ON
) {
382 fq_if_flow_feedback(fqs
, fq
, fq_cl
);
387 /* Reset getqtime so that we don't count idle times */
390 fq
->fq_getqtime
= now
;
392 fq_if_is_flow_heavy(fqs
, fq
);
395 if (pkt
->pktsched_ptype
== QP_MBUF
)
396 *pkt_flags
&= ~PKTF_PRIV_GUARDED
;