2 * Copyright (c) 2011-2013 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>
42 #include <kern/zalloc.h>
45 #include <net/if_var.h>
46 #include <net/if_types.h>
48 #include <net/flowadv.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
54 #include <netinet/ip6.h>
57 #include <net/classq/classq_sfb.h>
58 #include <net/flowhash.h>
59 #include <net/net_osdep.h>
60 #include <dev/random/randomdev.h>
63 * Stochastic Fair Blue
65 * Wu-chang Feng, Dilip D. Kandlur, Debanjan Saha, Kang G. Shin
66 * http://www.thefengs.com/wuchang/blue/CSE-TR-387-99.pdf
68 * Based on the NS code with the following parameters:
73 * hold-time: 10ms-50ms (randomized)
76 * pbox-time: 50-100ms (randomized)
77 * hinterval: 11-23 (randomized)
79 * This implementation uses L = 2 and N = 32 for 2 sets of:
81 * B[L][N]: L x N array of bins (L levels, N bins per level)
83 * Each set effectively creates 32^2 virtual buckets (bin combinations)
84 * while using only O(32*2) states.
86 * Given a 32-bit hash value, we divide it such that octets [0,1,2,3] are
87 * used as index for the bins across the 2 levels, where level 1 uses [0,2]
88 * and level 2 uses [1,3]. The 2 values per level correspond to the indices
89 * for the current and warm-up sets (section 4.4. in the SFB paper regarding
90 * Moving Hash Functions explains the purposes of these 2 sets.)
94 * Use Murmur3A_x86_32 for hash function. It seems to perform consistently
95 * across platforms for 1-word key (32-bit flowhash value). See flowhash.h
96 * for other alternatives. We only need 16-bit hash output.
98 #define SFB_HASH net_flowhash_mh3_x86_32
99 #define SFB_HASHMASK HASHMASK(16)
101 #define SFB_BINMASK(_x) \
102 ((_x) & HASHMASK(SFB_BINS_SHIFT))
104 #define SFB_BINST(_sp, _l, _n, _c) \
105 (&(*(_sp)->sfb_bins)[_c].stats[_l][_n])
107 #define SFB_BINFT(_sp, _l, _n, _c) \
108 (&(*(_sp)->sfb_bins)[_c].freezetime[_l][_n])
110 #define SFB_FC_LIST(_sp, _n) \
111 (&(*(_sp)->sfb_fc_lists)[_n])
114 * The holdtime parameter determines the minimum time interval between
115 * two successive updates of the marking probability. In the event the
116 * uplink speed is not known, a default value is chosen and is randomized
117 * to be within the following range.
119 #define HOLDTIME_BASE (100ULL * 1000 * 1000) /* 100ms */
120 #define HOLDTIME_MIN (10ULL * 1000 * 1000) /* 10ms */
121 #define HOLDTIME_MAX (100ULL * 1000 * 1000) /* 100ms */
124 * The pboxtime parameter determines the bandwidth allocated for rogue
125 * flows, i.e. the rate limiting bandwidth. In the event the uplink speed
126 * is not known, a default value is chosen and is randomized to be within
127 * the following range.
129 #define PBOXTIME_BASE (300ULL * 1000 * 1000) /* 300ms */
130 #define PBOXTIME_MIN (30ULL * 1000 * 1000) /* 30ms */
131 #define PBOXTIME_MAX (300ULL * 1000 * 1000) /* 300ms */
134 * Target queueing delay is the amount of extra delay that can be added
135 * to accommodate variations in the link bandwidth. The queue should be
136 * large enough to induce this much delay and nothing more than that.
138 #define TARGET_QDELAY_BASE (10ULL * 1000 * 1000) /* 10ms */
139 #define TARGET_QDELAY_MIN (10ULL * 1000) /* 10us */
140 #define TARGET_QDELAY_MAX (20ULL * 1000 * 1000 * 1000) /* 20s */
143 * Update interval for checking the extra delay added by the queue. This
144 * should be 90-95 percentile of RTT experienced by any TCP connection
145 * so that it will take care of the burst traffic.
147 #define UPDATE_INTERVAL_BASE (100ULL * 1000 * 1000) /* 100ms */
148 #define UPDATE_INTERVAL_MIN (100ULL * 1000 * 1000) /* 100ms */
149 #define UPDATE_INTERVAL_MAX (10ULL * 1000 * 1000 * 1000) /* 10s */
151 #define SFB_RANDOM(sp, tmin, tmax) ((sfb_random(sp) % (tmax)) + (tmin))
153 #define SFB_PKT_PBOX 0x1 /* in penalty box */
155 /* The following mantissa values are in SFB_FP_SHIFT Q format */
156 #define SFB_MAX_PMARK (1 << SFB_FP_SHIFT) /* Q14 representation of 1.00 */
159 * These are d1 (increment) and d2 (decrement) parameters, used to determine
160 * the amount by which the marking probability is incremented when the queue
161 * overflows, or is decremented when the link is idle. d1 is set higher than
162 * d2, because link underutilization can occur when congestion management is
163 * either too conservative or too aggressive, but packet loss occurs only
164 * when congestion management is too conservative. By weighing heavily
165 * against packet loss, it can quickly reach to a substantial increase in
168 #define SFB_INCREMENT 82 /* Q14 representation of 0.005 */
169 #define SFB_DECREMENT 16 /* Q14 representation of 0.001 */
171 #define SFB_PMARK_TH 16056 /* Q14 representation of 0.98 */
172 #define SFB_PMARK_WARM 3276 /* Q14 representation of 0.2 */
174 #define SFB_PMARK_INC(_bin) do { \
175 (_bin)->pmark += sfb_increment; \
176 if ((_bin)->pmark > SFB_MAX_PMARK) \
177 (_bin)->pmark = SFB_MAX_PMARK; \
180 #define SFB_PMARK_DEC(_bin) do { \
181 if ((_bin)->pmark > 0) { \
182 (_bin)->pmark -= sfb_decrement; \
183 if ((_bin)->pmark < 0) \
188 /* Minimum nuber of bytes in queue to get flow controlled */
189 #define SFB_MIN_FC_THRESHOLD_BYTES 7500
191 #define SFB_SET_DELAY_HIGH(_sp_, _q_) do { \
192 (_sp_)->sfb_flags |= SFBF_DELAYHIGH; \
193 (_sp_)->sfb_fc_threshold = max(SFB_MIN_FC_THRESHOLD_BYTES, \
194 (qsize((_q_)) >> 3)); \
197 #define SFB_QUEUE_DELAYBASED(_sp_) ((_sp_)->sfb_flags & SFBF_DELAYBASED)
198 #define SFB_IS_DELAYHIGH(_sp_) ((_sp_)->sfb_flags & SFBF_DELAYHIGH)
199 #define SFB_QUEUE_DELAYBASED_MAXSIZE 2048 /* max pkts */
201 #define HINTERVAL_MIN (10) /* 10 seconds */
202 #define HINTERVAL_MAX (20) /* 20 seconds */
203 #define SFB_HINTERVAL(sp) ((sfb_random(sp) % HINTERVAL_MAX) + HINTERVAL_MIN)
205 #define DEQUEUE_DECAY 7 /* ilog2 of EWMA decay rate, (128) */
206 #define DEQUEUE_SPIKE(_new, _old) \
207 ((u_int64_t)ABS((int64_t)(_new) - (int64_t)(_old)) > ((_old) << 11))
209 #define ABS(v) (((v) > 0) ? (v) : -(v))
211 #define SFB_ZONE_MAX 32 /* maximum elements in zone */
212 #define SFB_ZONE_NAME "classq_sfb" /* zone name */
214 #define SFB_BINS_ZONE_MAX 32 /* maximum elements in zone */
215 #define SFB_BINS_ZONE_NAME "classq_sfb_bins" /* zone name */
217 #define SFB_FCL_ZONE_MAX 32 /* maximum elements in zone */
218 #define SFB_FCL_ZONE_NAME "classq_sfb_fcl" /* zone name */
220 /* Place the flow control entries in current bin on level 0 */
221 #define SFB_FC_LEVEL 0
223 /* Store SFB hash and flags in the module private scratch space */
224 #define pkt_sfb_hash8 pkt_mpriv.__mpriv_u.__mpriv32[0].__mpriv32_u.__val8
225 #define pkt_sfb_hash16 pkt_mpriv.__mpriv_u.__mpriv32[0].__mpriv32_u.__val16
226 #define pkt_sfb_hash32 pkt_mpriv.__mpriv_u.__mpriv32[0].__mpriv32_u.__val32
227 #define pkt_sfb_flags pkt_mpriv.__mpriv_u.__mpriv32[1].__mpriv32_u.__val32
229 static unsigned int sfb_size
; /* size of zone element */
230 static struct zone
*sfb_zone
; /* zone for sfb */
232 static unsigned int sfb_bins_size
; /* size of zone element */
233 static struct zone
*sfb_bins_zone
; /* zone for sfb_bins */
235 static unsigned int sfb_fcl_size
; /* size of zone element */
236 static struct zone
*sfb_fcl_zone
; /* zone for sfb_fc_lists */
238 /* internal function prototypes */
239 static u_int32_t
sfb_random(struct sfb
*);
240 static struct mbuf
*sfb_getq_flow(struct sfb
*, class_queue_t
*, u_int32_t
,
242 static void sfb_resetq(struct sfb
*, cqev_t
);
243 static void sfb_calc_holdtime(struct sfb
*, u_int64_t
);
244 static void sfb_calc_pboxtime(struct sfb
*, u_int64_t
);
245 static void sfb_calc_hinterval(struct sfb
*, u_int64_t
*);
246 static void sfb_calc_target_qdelay(struct sfb
*, u_int64_t
);
247 static void sfb_calc_update_interval(struct sfb
*, u_int64_t
);
248 static void sfb_swap_bins(struct sfb
*, u_int32_t
);
249 static inline int sfb_pcheck(struct sfb
*, struct pkthdr
*);
250 static int sfb_penalize(struct sfb
*, struct pkthdr
*, struct timespec
*);
251 static void sfb_adjust_bin(struct sfb
*, struct sfbbinstats
*,
252 struct timespec
*, struct timespec
*, boolean_t
);
253 static void sfb_decrement_bin(struct sfb
*, struct sfbbinstats
*,
254 struct timespec
*, struct timespec
*);
255 static void sfb_increment_bin(struct sfb
*, struct sfbbinstats
*,
256 struct timespec
*, struct timespec
*);
257 static inline void sfb_dq_update_bins(struct sfb
*, struct pkthdr
*,
258 struct timespec
*, u_int32_t qsize
);
259 static inline void sfb_eq_update_bins(struct sfb
*, struct pkthdr
*);
260 static int sfb_drop_early(struct sfb
*, struct pkthdr
*, u_int16_t
*,
262 static boolean_t
sfb_bin_addfcentry(struct sfb
*, struct pkthdr
*);
263 static void sfb_fclist_append(struct sfb
*, struct sfb_fcl
*);
264 static void sfb_fclists_clean(struct sfb
*sp
);
265 static int sfb_bin_mark_or_drop(struct sfb
*sp
, struct sfbbinstats
*bin
);
266 static void sfb_detect_dequeue_stall(struct sfb
*sp
, class_queue_t
*,
269 SYSCTL_NODE(_net_classq
, OID_AUTO
, sfb
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "SFB");
271 static u_int64_t sfb_holdtime
= 0; /* 0 indicates "automatic" */
272 SYSCTL_QUAD(_net_classq_sfb
, OID_AUTO
, holdtime
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
273 &sfb_holdtime
, "SFB freeze time in nanoseconds");
275 static u_int64_t sfb_pboxtime
= 0; /* 0 indicates "automatic" */
276 SYSCTL_QUAD(_net_classq_sfb
, OID_AUTO
, pboxtime
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
277 &sfb_pboxtime
, "SFB penalty box time in nanoseconds");
279 static u_int64_t sfb_hinterval
;
280 SYSCTL_QUAD(_net_classq_sfb
, OID_AUTO
, hinterval
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
281 &sfb_hinterval
, "SFB hash interval in nanoseconds");
283 static u_int64_t sfb_target_qdelay
;
284 SYSCTL_QUAD(_net_classq_sfb
, OID_AUTO
, target_qdelay
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
285 &sfb_target_qdelay
, "SFB target queue delay in milliseconds");
287 static u_int64_t sfb_update_interval
;
288 SYSCTL_QUAD(_net_classq_sfb
, OID_AUTO
, update_interval
,
289 CTLFLAG_RW
|CTLFLAG_LOCKED
, &sfb_update_interval
, "SFB update interval");
291 static u_int32_t sfb_increment
= SFB_INCREMENT
;
292 SYSCTL_UINT(_net_classq_sfb
, OID_AUTO
, increment
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
293 &sfb_increment
, SFB_INCREMENT
, "SFB increment [d1]");
295 static u_int32_t sfb_decrement
= SFB_DECREMENT
;
296 SYSCTL_UINT(_net_classq_sfb
, OID_AUTO
, decrement
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
297 &sfb_decrement
, SFB_DECREMENT
, "SFB decrement [d2]");
299 static u_int32_t sfb_allocation
= 0; /* 0 means "automatic" */
300 SYSCTL_UINT(_net_classq_sfb
, OID_AUTO
, allocation
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
301 &sfb_allocation
, 0, "SFB bin allocation");
303 static u_int32_t sfb_ratelimit
= 0;
304 SYSCTL_UINT(_net_classq_sfb
, OID_AUTO
, ratelimit
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
305 &sfb_ratelimit
, 0, "SFB rate limit");
307 #define KBPS (1ULL * 1000) /* 1 Kbits per second */
308 #define MBPS (1ULL * 1000 * 1000) /* 1 Mbits per second */
309 #define GBPS (MBPS * 1000) /* 1 Gbits per second */
311 struct sfb_time_tbl
{
312 u_int64_t speed
; /* uplink speed */
313 u_int64_t holdtime
; /* hold time */
314 u_int64_t pboxtime
; /* penalty box time */
317 static struct sfb_time_tbl sfb_ttbl
[] = {
318 { 1 * MBPS
, HOLDTIME_BASE
* 1000, PBOXTIME_BASE
* 1000 },
319 { 10 * MBPS
, HOLDTIME_BASE
* 100, PBOXTIME_BASE
* 100 },
320 { 100 * MBPS
, HOLDTIME_BASE
* 10, PBOXTIME_BASE
* 10 },
321 { 1 * GBPS
, HOLDTIME_BASE
, PBOXTIME_BASE
},
322 { 10 * GBPS
, HOLDTIME_BASE
/ 10, PBOXTIME_BASE
/ 10 },
323 { 100 * GBPS
, HOLDTIME_BASE
/ 100, PBOXTIME_BASE
/ 100 },
330 _CASSERT(SFBF_ECN4
== CLASSQF_ECN4
);
331 _CASSERT(SFBF_ECN6
== CLASSQF_ECN6
);
333 sfb_size
= sizeof (struct sfb
);
334 sfb_zone
= zinit(sfb_size
, SFB_ZONE_MAX
* sfb_size
,
336 if (sfb_zone
== NULL
) {
337 panic("%s: failed allocating %s", __func__
, SFB_ZONE_NAME
);
340 zone_change(sfb_zone
, Z_EXPAND
, TRUE
);
341 zone_change(sfb_zone
, Z_CALLERACCT
, TRUE
);
343 sfb_bins_size
= sizeof (*((struct sfb
*)0)->sfb_bins
);
344 sfb_bins_zone
= zinit(sfb_bins_size
, SFB_BINS_ZONE_MAX
* sfb_bins_size
,
345 0, SFB_BINS_ZONE_NAME
);
346 if (sfb_bins_zone
== NULL
) {
347 panic("%s: failed allocating %s", __func__
, SFB_BINS_ZONE_NAME
);
350 zone_change(sfb_bins_zone
, Z_EXPAND
, TRUE
);
351 zone_change(sfb_bins_zone
, Z_CALLERACCT
, TRUE
);
353 sfb_fcl_size
= sizeof (*((struct sfb
*)0)->sfb_fc_lists
);
354 sfb_fcl_zone
= zinit(sfb_fcl_size
, SFB_FCL_ZONE_MAX
* sfb_fcl_size
,
355 0, SFB_FCL_ZONE_NAME
);
356 if (sfb_fcl_zone
== NULL
) {
357 panic("%s: failed allocating %s", __func__
, SFB_FCL_ZONE_NAME
);
360 zone_change(sfb_fcl_zone
, Z_EXPAND
, TRUE
);
361 zone_change(sfb_fcl_zone
, Z_CALLERACCT
, TRUE
);
365 sfb_random(struct sfb
*sp
)
367 IFCQ_CONVERT_LOCK(&sp
->sfb_ifp
->if_snd
);
368 return (RandomULong());
372 sfb_calc_holdtime(struct sfb
*sp
, u_int64_t outbw
)
376 if (sfb_holdtime
!= 0) {
377 holdtime
= sfb_holdtime
;
378 } else if (outbw
== 0) {
379 holdtime
= SFB_RANDOM(sp
, HOLDTIME_MIN
, HOLDTIME_MAX
);
383 n
= sfb_ttbl
[0].holdtime
;
384 for (i
= 0; sfb_ttbl
[i
].speed
!= 0; i
++) {
385 if (outbw
< sfb_ttbl
[i
].speed
)
387 n
= sfb_ttbl
[i
].holdtime
;
391 net_nsectimer(&holdtime
, &sp
->sfb_holdtime
);
395 sfb_calc_pboxtime(struct sfb
*sp
, u_int64_t outbw
)
399 if (sfb_pboxtime
!= 0) {
400 pboxtime
= sfb_pboxtime
;
401 } else if (outbw
== 0) {
402 pboxtime
= SFB_RANDOM(sp
, PBOXTIME_MIN
, PBOXTIME_MAX
);
406 n
= sfb_ttbl
[0].pboxtime
;
407 for (i
= 0; sfb_ttbl
[i
].speed
!= 0; i
++) {
408 if (outbw
< sfb_ttbl
[i
].speed
)
410 n
= sfb_ttbl
[i
].pboxtime
;
414 net_nsectimer(&pboxtime
, &sp
->sfb_pboxtime
);
415 net_timerclear(&sp
->sfb_pboxfreeze
);
419 sfb_calc_hinterval(struct sfb
*sp
, u_int64_t
*t
)
426 * TODO adi@apple.com: use dq_avg to derive hinterval.
431 if (sfb_hinterval
!= 0)
432 hinterval
= sfb_hinterval
;
433 else if (t
== NULL
|| hinterval
== 0)
434 hinterval
= ((u_int64_t
)SFB_HINTERVAL(sp
) * NSEC_PER_SEC
);
436 net_nsectimer(&hinterval
, &sp
->sfb_hinterval
);
439 net_timeradd(&now
, &sp
->sfb_hinterval
, &sp
->sfb_nextreset
);
443 sfb_calc_target_qdelay(struct sfb
*sp
, u_int64_t out_bw
)
445 #pragma unused(out_bw)
446 u_int64_t target_qdelay
= 0;
447 struct ifnet
*ifp
= sp
->sfb_ifp
;
449 target_qdelay
= IFCQ_TARGET_QDELAY(&ifp
->if_snd
);
451 if (sfb_target_qdelay
!= 0)
452 target_qdelay
= sfb_target_qdelay
;
455 * If we do not know the effective bandwidth, use the default
456 * target queue delay.
458 if (target_qdelay
== 0)
459 target_qdelay
= IFQ_TARGET_DELAY
;
461 sp
->sfb_target_qdelay
= target_qdelay
;
465 sfb_calc_update_interval(struct sfb
*sp
, u_int64_t out_bw
)
467 #pragma unused(out_bw)
468 u_int64_t update_interval
= 0;
470 /* If the system-level override is set, use it */
471 if (sfb_update_interval
!= 0)
472 update_interval
= sfb_update_interval
;
474 * If we do not know the effective bandwidth, use the default
477 if (update_interval
== 0)
478 update_interval
= IFQ_UPDATE_INTERVAL
;
480 net_nsectimer(&update_interval
, &sp
->sfb_update_interval
);
484 * sfb support routines
487 sfb_alloc(struct ifnet
*ifp
, u_int32_t qid
, u_int32_t qlim
, u_int32_t flags
)
492 VERIFY(ifp
!= NULL
&& qlim
> 0);
494 sp
= zalloc(sfb_zone
);
496 log(LOG_ERR
, "%s: SFB unable to allocate\n", if_name(ifp
));
501 if ((sp
->sfb_bins
= zalloc(sfb_bins_zone
)) == NULL
) {
502 log(LOG_ERR
, "%s: SFB unable to allocate bins\n", if_name(ifp
));
506 bzero(sp
->sfb_bins
, sfb_bins_size
);
508 if ((sp
->sfb_fc_lists
= zalloc(sfb_fcl_zone
)) == NULL
) {
509 log(LOG_ERR
, "%s: SFB unable to allocate flow control lists\n",
514 bzero(sp
->sfb_fc_lists
, sfb_fcl_size
);
516 for (i
= 0; i
< SFB_BINS
; ++i
)
517 STAILQ_INIT(&SFB_FC_LIST(sp
, i
)->fclist
);
522 sp
->sfb_flags
= (flags
& SFBF_USERFLAGS
);
524 if (sp
->sfb_flags
& SFBF_ECN
) {
525 sp
->sfb_flags
&= ~SFBF_ECN
;
526 log(LOG_ERR
, "%s: SFB qid=%d, ECN not available; ignoring "
527 "SFBF_ECN flag!\n", if_name(ifp
), sp
->sfb_qid
);
537 sfb_fclist_append(struct sfb
*sp
, struct sfb_fcl
*fcl
)
539 IFCQ_CONVERT_LOCK(&sp
->sfb_ifp
->if_snd
);
541 VERIFY(STAILQ_EMPTY(&fcl
->fclist
) || fcl
->cnt
> 0);
542 sp
->sfb_stats
.flow_feedback
+= fcl
->cnt
;
545 flowadv_add(&fcl
->fclist
);
546 VERIFY(fcl
->cnt
== 0 && STAILQ_EMPTY(&fcl
->fclist
));
550 sfb_fclists_clean(struct sfb
*sp
)
554 /* Move all the flow control entries to the flowadv list */
555 for (i
= 0; i
< SFB_BINS
; ++i
) {
556 struct sfb_fcl
*fcl
= SFB_FC_LIST(sp
, i
);
557 if (!STAILQ_EMPTY(&fcl
->fclist
))
558 sfb_fclist_append(sp
, fcl
);
563 sfb_destroy(struct sfb
*sp
)
565 sfb_fclists_clean(sp
);
566 if (sp
->sfb_bins
!= NULL
) {
567 zfree(sfb_bins_zone
, sp
->sfb_bins
);
570 if (sp
->sfb_fc_lists
!= NULL
) {
571 zfree(sfb_fcl_zone
, sp
->sfb_fc_lists
);
572 sp
->sfb_fc_lists
= NULL
;
578 sfb_resetq(struct sfb
*sp
, cqev_t ev
)
580 struct ifnet
*ifp
= sp
->sfb_ifp
;
585 if (ev
!= CLASSQ_EV_LINK_DOWN
) {
586 (*sp
->sfb_bins
)[0].fudge
= sfb_random(sp
);
587 (*sp
->sfb_bins
)[1].fudge
= sfb_random(sp
);
588 sp
->sfb_allocation
= ((sfb_allocation
== 0) ?
589 (sp
->sfb_qlim
/ 3) : sfb_allocation
);
590 sp
->sfb_drop_thresh
= sp
->sfb_allocation
+
591 (sp
->sfb_allocation
>> 1);
594 sp
->sfb_clearpkts
= 0;
597 eff_rate
= ifnet_output_linkrate(ifp
);
598 sp
->sfb_eff_rate
= eff_rate
;
600 sfb_calc_holdtime(sp
, eff_rate
);
601 sfb_calc_pboxtime(sp
, eff_rate
);
602 sfb_calc_hinterval(sp
, NULL
);
603 sfb_calc_target_qdelay(sp
, eff_rate
);
604 sfb_calc_update_interval(sp
, eff_rate
);
606 if (ev
== CLASSQ_EV_LINK_DOWN
||
607 ev
== CLASSQ_EV_LINK_UP
)
608 sfb_fclists_clean(sp
);
610 bzero(sp
->sfb_bins
, sizeof (*sp
->sfb_bins
));
611 bzero(&sp
->sfb_stats
, sizeof (sp
->sfb_stats
));
613 if (ev
== CLASSQ_EV_LINK_DOWN
|| !classq_verbose
)
616 log(LOG_DEBUG
, "%s: SFB qid=%d, holdtime=%llu nsec, "
617 "pboxtime=%llu nsec, allocation=%d, drop_thresh=%d, "
618 "hinterval=%d sec, sfb_bins=%d bytes, eff_rate=%llu bps"
619 "target_qdelay= %llu nsec "
620 "update_interval=%llu sec %llu nsec flags=0x%x\n",
621 if_name(ifp
), sp
->sfb_qid
, (u_int64_t
)sp
->sfb_holdtime
.tv_nsec
,
622 (u_int64_t
)sp
->sfb_pboxtime
.tv_nsec
,
623 (u_int32_t
)sp
->sfb_allocation
, (u_int32_t
)sp
->sfb_drop_thresh
,
624 (int)sp
->sfb_hinterval
.tv_sec
, (int)sizeof (*sp
->sfb_bins
),
625 eff_rate
, (u_int64_t
)sp
->sfb_target_qdelay
,
626 (u_int64_t
)sp
->sfb_update_interval
.tv_sec
,
627 (u_int64_t
)sp
->sfb_update_interval
.tv_nsec
, sp
->sfb_flags
);
631 sfb_getstats(struct sfb
*sp
, struct sfb_stats
*sps
)
633 sps
->allocation
= sp
->sfb_allocation
;
634 sps
->dropthresh
= sp
->sfb_drop_thresh
;
635 sps
->clearpkts
= sp
->sfb_clearpkts
;
636 sps
->current
= sp
->sfb_current
;
637 sps
->target_qdelay
= sp
->sfb_target_qdelay
;
638 sps
->min_estdelay
= sp
->sfb_min_qdelay
;
639 sps
->delay_fcthreshold
= sp
->sfb_fc_threshold
;
640 sps
->flags
= sp
->sfb_flags
;
642 net_timernsec(&sp
->sfb_holdtime
, &sp
->sfb_stats
.hold_time
);
643 net_timernsec(&sp
->sfb_pboxtime
, &sp
->sfb_stats
.pbox_time
);
644 net_timernsec(&sp
->sfb_hinterval
, &sp
->sfb_stats
.rehash_intval
);
645 net_timernsec(&sp
->sfb_update_interval
, &sps
->update_interval
);
646 *(&(sps
->sfbstats
)) = *(&(sp
->sfb_stats
));
648 _CASSERT(sizeof ((*sp
->sfb_bins
)[0].stats
) ==
649 sizeof (sps
->binstats
[0].stats
));
651 bcopy(&(*sp
->sfb_bins
)[0].stats
, &sps
->binstats
[0].stats
,
652 sizeof (sps
->binstats
[0].stats
));
653 bcopy(&(*sp
->sfb_bins
)[1].stats
, &sps
->binstats
[1].stats
,
654 sizeof (sps
->binstats
[1].stats
));
658 sfb_swap_bins(struct sfb
*sp
, u_int32_t len
)
662 if (sp
->sfb_flags
& SFBF_SUSPENDED
)
666 VERIFY((s
+ (s
^ 1)) == 1);
668 (*sp
->sfb_bins
)[s
].fudge
= sfb_random(sp
); /* recompute perturbation */
669 sp
->sfb_clearpkts
= len
;
670 sp
->sfb_stats
.num_rehash
++;
672 s
= (sp
->sfb_current
^= 1); /* flip the bit (swap current) */
674 if (classq_verbose
) {
675 log(LOG_DEBUG
, "%s: SFB qid=%d, set %d is now current, "
676 "qlen=%d\n", if_name(sp
->sfb_ifp
), sp
->sfb_qid
, s
, len
);
679 /* clear freezetime for all current bins */
680 bzero(&(*sp
->sfb_bins
)[s
].freezetime
,
681 sizeof ((*sp
->sfb_bins
)[s
].freezetime
));
683 /* clear/adjust bin statistics and flow control lists */
684 for (i
= 0; i
< SFB_BINS
; i
++) {
685 struct sfb_fcl
*fcl
= SFB_FC_LIST(sp
, i
);
687 if (!STAILQ_EMPTY(&fcl
->fclist
))
688 sfb_fclist_append(sp
, fcl
);
690 for (j
= 0; j
< SFB_LEVELS
; j
++) {
691 struct sfbbinstats
*cbin
, *wbin
;
693 cbin
= SFB_BINST(sp
, j
, i
, s
); /* current */
694 wbin
= SFB_BINST(sp
, j
, i
, s
^ 1); /* warm-up */
698 if (cbin
->pmark
> SFB_MAX_PMARK
)
699 cbin
->pmark
= SFB_MAX_PMARK
;
704 * Keep pmark from before to identify
705 * non-responsives immediately.
707 if (wbin
->pmark
> SFB_PMARK_WARM
)
708 wbin
->pmark
= SFB_PMARK_WARM
;
714 sfb_pcheck(struct sfb
*sp
, struct pkthdr
*pkt
)
718 #endif /* SFB_LEVELS != 2 */
722 VERIFY((s
+ (s
^ 1)) == 1);
725 * For current bins, returns 1 if all pmark >= SFB_PMARK_TH,
726 * 0 otherwise; optimize for SFB_LEVELS=2.
730 * Level 0: bin index at [0] for set 0; [2] for set 1
731 * Level 1: bin index at [1] for set 0; [3] for set 1
733 if (SFB_BINST(sp
, 0, SFB_BINMASK(pkt
->pkt_sfb_hash8
[(s
<< 1)]),
734 s
)->pmark
< SFB_PMARK_TH
||
735 SFB_BINST(sp
, 1, SFB_BINMASK(pkt
->pkt_sfb_hash8
[(s
<< 1) + 1]),
736 s
)->pmark
< SFB_PMARK_TH
)
738 #else /* SFB_LEVELS != 2 */
739 for (i
= 0; i
< SFB_LEVELS
; i
++) {
740 if (s
== 0) /* set 0, bin index [0,1] */
741 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
]);
742 else /* set 1, bin index [2,3] */
743 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
+ 2]);
745 if (SFB_BINST(sp
, i
, n
, s
)->pmark
< SFB_PMARK_TH
)
748 #endif /* SFB_LEVELS != 2 */
753 sfb_penalize(struct sfb
*sp
, struct pkthdr
*pkt
, struct timespec
*now
)
755 struct timespec delta
= { 0, 0 };
757 /* If minimum pmark of current bins is < SFB_PMARK_TH, we're done */
758 if (!sfb_ratelimit
|| !sfb_pcheck(sp
, pkt
))
761 net_timersub(now
, &sp
->sfb_pboxfreeze
, &delta
);
762 if (net_timercmp(&delta
, &sp
->sfb_pboxtime
, <)) {
765 #endif /* SFB_LEVELS != 2 */
766 struct sfbbinstats
*bin
;
769 w
= sp
->sfb_current
^ 1;
770 VERIFY((w
+ (w
^ 1)) == 1);
773 * Update warm-up bins; optimize for SFB_LEVELS=2
776 /* Level 0: bin index at [0] for set 0; [2] for set 1 */
777 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[(w
<< 1)]);
778 bin
= SFB_BINST(sp
, 0, n
, w
);
779 if (bin
->pkts
>= sp
->sfb_allocation
)
780 sfb_increment_bin(sp
, bin
, SFB_BINFT(sp
, 0, n
, w
), now
);
782 /* Level 0: bin index at [1] for set 0; [3] for set 1 */
783 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[(w
<< 1) + 1]);
784 bin
= SFB_BINST(sp
, 1, n
, w
);
785 if (bin
->pkts
>= sp
->sfb_allocation
)
786 sfb_increment_bin(sp
, bin
, SFB_BINFT(sp
, 1, n
, w
), now
);
787 #else /* SFB_LEVELS != 2 */
788 for (i
= 0; i
< SFB_LEVELS
; i
++) {
789 if (w
== 0) /* set 0, bin index [0,1] */
790 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
]);
791 else /* set 1, bin index [2,3] */
792 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
+ 2]);
794 bin
= SFB_BINST(sp
, i
, n
, w
);
795 if (bin
->pkts
>= sp
->sfb_allocation
) {
796 sfb_increment_bin(sp
, bin
,
797 SFB_BINFT(sp
, i
, n
, w
), now
);
800 #endif /* SFB_LEVELS != 2 */
804 /* non-conformant or else misclassified flow; queue it anyway */
805 pkt
->pkt_sfb_flags
|= SFB_PKT_PBOX
;
806 *(&sp
->sfb_pboxfreeze
) = *now
;
812 sfb_adjust_bin(struct sfb
*sp
, struct sfbbinstats
*bin
, struct timespec
*ft
,
813 struct timespec
*now
, boolean_t inc
)
815 struct timespec delta
;
817 net_timersub(now
, ft
, &delta
);
818 if (net_timercmp(&delta
, &sp
->sfb_holdtime
, <)) {
819 if (classq_verbose
> 1) {
820 log(LOG_DEBUG
, "%s: SFB qid=%d, %s update frozen "
821 "(delta=%llu nsec)\n", if_name(sp
->sfb_ifp
),
822 sp
->sfb_qid
, inc
? "increment" : "decrement",
823 (u_int64_t
)delta
.tv_nsec
);
828 /* increment/decrement marking probability */
837 sfb_decrement_bin(struct sfb
*sp
, struct sfbbinstats
*bin
, struct timespec
*ft
,
838 struct timespec
*now
)
840 return (sfb_adjust_bin(sp
, bin
, ft
, now
, FALSE
));
844 sfb_increment_bin(struct sfb
*sp
, struct sfbbinstats
*bin
, struct timespec
*ft
,
845 struct timespec
*now
)
847 return (sfb_adjust_bin(sp
, bin
, ft
, now
, TRUE
));
851 sfb_dq_update_bins(struct sfb
*sp
, struct pkthdr
*pkt
,
852 struct timespec
*now
, u_int32_t qsize
)
854 #if SFB_LEVELS != 2 || SFB_FC_LEVEL != 0
856 #endif /* SFB_LEVELS != 2 || SFB_FC_LEVEL != 0 */
857 struct sfbbinstats
*bin
;
859 struct sfb_fcl
*fcl
= NULL
;
862 VERIFY((s
+ (s
^ 1)) == 1);
865 * Update current bins; optimize for SFB_LEVELS=2 and SFB_FC_LEVEL=0
867 #if SFB_LEVELS == 2 && SFB_FC_LEVEL == 0
868 /* Level 0: bin index at [0] for set 0; [2] for set 1 */
869 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[(s
<< 1)]);
870 bin
= SFB_BINST(sp
, 0, n
, s
);
872 VERIFY(bin
->pkts
> 0 && bin
->bytes
>= (u_int32_t
)pkt
->len
);
874 bin
->bytes
-= pkt
->len
;
877 sfb_decrement_bin(sp
, bin
, SFB_BINFT(sp
, 0, n
, s
), now
);
879 /* Deliver flow control feedback to the sockets */
880 if (SFB_QUEUE_DELAYBASED(sp
)) {
881 if (!(SFB_IS_DELAYHIGH(sp
)) ||
882 bin
->bytes
<= sp
->sfb_fc_threshold
||
883 bin
->pkts
== 0 || qsize
== 0)
884 fcl
= SFB_FC_LIST(sp
, n
);
885 } else if (bin
->pkts
<= (sp
->sfb_allocation
>> 2)) {
886 fcl
= SFB_FC_LIST(sp
, n
);
889 if (fcl
!= NULL
&& !STAILQ_EMPTY(&fcl
->fclist
))
890 sfb_fclist_append(sp
, fcl
);
893 /* Level 1: bin index at [1] for set 0; [3] for set 1 */
894 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[(s
<< 1) + 1]);
895 bin
= SFB_BINST(sp
, 1, n
, s
);
897 VERIFY(bin
->pkts
> 0 && bin
->bytes
>= (u_int64_t
)pkt
->len
);
899 bin
->bytes
-= pkt
->len
;
901 sfb_decrement_bin(sp
, bin
, SFB_BINFT(sp
, 1, n
, s
), now
);
902 #else /* SFB_LEVELS != 2 || SFB_FC_LEVEL != 0 */
903 for (i
= 0; i
< SFB_LEVELS
; i
++) {
904 if (s
== 0) /* set 0, bin index [0,1] */
905 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
]);
906 else /* set 1, bin index [2,3] */
907 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
+ 2]);
909 bin
= SFB_BINST(sp
, i
, n
, s
);
911 VERIFY(bin
->pkts
> 0 && bin
->bytes
>= pkt
->len
);
913 bin
->bytes
-= pkt
->len
;
915 sfb_decrement_bin(sp
, bin
,
916 SFB_BINFT(sp
, i
, n
, s
), now
);
917 if (i
!= SFB_FC_LEVEL
)
919 if (SFB_QUEUE_DELAYBASED(sp
)) {
920 if (!(SFB_IS_DELAYHIGH(sp
)) ||
921 bin
->bytes
<= sp
->sfb_fc_threshold
)
922 fcl
= SFB_FC_LIST(sp
, n
);
923 } else if (bin
->pkts
<= (sp
->sfb_allocation
>> 2)) {
924 fcl
= SFB_FC_LIST(sp
, n
);
926 if (fcl
!= NULL
&& !STAILQ_EMPTY(&fcl
->fclist
))
927 sfb_fclist_append(sp
, fcl
);
930 #endif /* SFB_LEVELS != 2 || SFB_FC_LEVEL != 0 */
934 sfb_eq_update_bins(struct sfb
*sp
, struct pkthdr
*pkt
)
938 #endif /* SFB_LEVELS != 2 */
940 struct sfbbinstats
*bin
;
942 VERIFY((s
+ (s
^ 1)) == 1);
945 * Update current bins; optimize for SFB_LEVELS=2
948 /* Level 0: bin index at [0] for set 0; [2] for set 1 */
949 bin
= SFB_BINST(sp
, 0,
950 SFB_BINMASK(pkt
->pkt_sfb_hash8
[(s
<< 1)]), s
);
952 bin
->bytes
+= pkt
->len
;
954 /* Level 1: bin index at [1] for set 0; [3] for set 1 */
955 bin
= SFB_BINST(sp
, 1,
956 SFB_BINMASK(pkt
->pkt_sfb_hash8
[(s
<< 1) + 1]), s
);
958 bin
->bytes
+= pkt
->len
;
960 #else /* SFB_LEVELS != 2 */
961 for (i
= 0; i
< SFB_LEVELS
; i
++) {
962 if (s
== 0) /* set 0, bin index [0,1] */
963 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
]);
964 else /* set 1, bin index [2,3] */
965 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
+ 2]);
967 bin
= SFB_BINST(sp
, i
, n
, s
);
969 bin
->bytes
+= pkt
->len
;
971 #endif /* SFB_LEVELS != 2 */
975 sfb_bin_addfcentry(struct sfb
*sp
, struct pkthdr
*pkt
)
977 struct flowadv_fcentry
*fce
;
978 u_int32_t flowsrc
, flowid
;
983 VERIFY((s
+ (s
^ 1)) == 1);
985 flowsrc
= pkt
->pkt_flowsrc
;
986 flowid
= pkt
->pkt_flowid
;
989 sp
->sfb_stats
.null_flowid
++;
994 * Use value at index 0 for set 0 and
995 * value at index 2 for set 1
997 fcl
= SFB_FC_LIST(sp
, SFB_BINMASK(pkt
->pkt_sfb_hash8
[(s
<< 1)]));
998 STAILQ_FOREACH(fce
, &fcl
->fclist
, fce_link
) {
999 if (fce
->fce_flowsrc
== flowsrc
&&
1000 fce
->fce_flowid
== flowid
) {
1001 /* Already on flow control list; just return */
1006 IFCQ_CONVERT_LOCK(&sp
->sfb_ifp
->if_snd
);
1007 fce
= flowadv_alloc_entry(M_WAITOK
);
1009 fce
->fce_flowsrc
= flowsrc
;
1010 fce
->fce_flowid
= flowid
;
1011 STAILQ_INSERT_TAIL(&fcl
->fclist
, fce
, fce_link
);
1013 sp
->sfb_stats
.flow_controlled
++;
1016 return (fce
!= NULL
);
1020 * check if this flow needs to be flow-controlled or if this
1021 * packet needs to be dropped.
1024 sfb_bin_mark_or_drop(struct sfb
*sp
, struct sfbbinstats
*bin
)
1027 if (SFB_QUEUE_DELAYBASED(sp
)) {
1029 * Mark or drop if this bin has more
1030 * bytes than the flowcontrol threshold.
1032 if (SFB_IS_DELAYHIGH(sp
) &&
1033 bin
->bytes
>= (sp
->sfb_fc_threshold
<< 1))
1036 if (bin
->pkts
>= sp
->sfb_allocation
&&
1037 bin
->pkts
>= sp
->sfb_drop_thresh
)
1038 ret
= 1; /* drop or mark */
1044 * early-drop probability is kept in pmark of each bin of the flow
1047 sfb_drop_early(struct sfb
*sp
, struct pkthdr
*pkt
, u_int16_t
*pmin
,
1048 struct timespec
*now
)
1052 #endif /* SFB_LEVELS != 2 */
1053 struct sfbbinstats
*bin
;
1056 s
= sp
->sfb_current
;
1057 VERIFY((s
+ (s
^ 1)) == 1);
1059 *pmin
= (u_int16_t
)-1;
1062 * Update current bins; optimize for SFB_LEVELS=2
1065 /* Level 0: bin index at [0] for set 0; [2] for set 1 */
1066 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[(s
<< 1)]);
1067 bin
= SFB_BINST(sp
, 0, n
, s
);
1068 if (*pmin
> (u_int16_t
)bin
->pmark
)
1069 *pmin
= (u_int16_t
)bin
->pmark
;
1072 /* Update SFB probability */
1073 if (bin
->pkts
>= sp
->sfb_allocation
)
1074 sfb_increment_bin(sp
, bin
, SFB_BINFT(sp
, 0, n
, s
), now
);
1076 ret
= sfb_bin_mark_or_drop(sp
, bin
);
1078 /* Level 1: bin index at [1] for set 0; [3] for set 1 */
1079 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[(s
<< 1) + 1]);
1080 bin
= SFB_BINST(sp
, 1, n
, s
);
1081 if (*pmin
> (u_int16_t
)bin
->pmark
)
1082 *pmin
= (u_int16_t
)bin
->pmark
;
1084 if (bin
->pkts
>= sp
->sfb_allocation
)
1085 sfb_increment_bin(sp
, bin
, SFB_BINFT(sp
, 1, n
, s
), now
);
1086 #else /* SFB_LEVELS != 2 */
1087 for (i
= 0; i
< SFB_LEVELS
; i
++) {
1088 if (s
== 0) /* set 0, bin index [0,1] */
1089 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
]);
1090 else /* set 1, bin index [2,3] */
1091 n
= SFB_BINMASK(pkt
->pkt_sfb_hash8
[i
+ 2]);
1093 bin
= SFB_BINST(sp
, i
, n
, s
);
1094 if (*pmin
> (u_int16_t
)bin
->pmark
)
1095 *pmin
= (u_int16_t
)bin
->pmark
;
1097 if (bin
->pkts
>= sp
->sfb_allocation
)
1098 sfb_increment_bin(sp
, bin
,
1099 SFB_BINFT(sp
, i
, n
, s
), now
);
1100 if (i
== SFB_FC_LEVEL
)
1101 ret
= sfb_bin_mark_or_drop(sp
, bin
);
1103 #endif /* SFB_LEVELS != 2 */
1105 if (sp
->sfb_flags
& SFBF_SUSPENDED
)
1106 ret
= 1; /* drop or mark */
1112 sfb_detect_dequeue_stall(struct sfb
*sp
, class_queue_t
*q
,
1113 struct timespec
*now
)
1115 struct timespec max_getqtime
;
1117 if (!SFB_QUEUE_DELAYBASED(sp
) || SFB_IS_DELAYHIGH(sp
) ||
1118 qsize(q
) <= SFB_MIN_FC_THRESHOLD_BYTES
||
1119 !net_timerisset(&sp
->sfb_getqtime
))
1122 net_timeradd(&sp
->sfb_getqtime
, &sp
->sfb_update_interval
,
1124 if (net_timercmp(now
, &max_getqtime
, >)) {
1126 * No packets have been dequeued in an update interval
1127 * worth of time. It means that the queue is stalled
1129 SFB_SET_DELAY_HIGH(sp
, q
);
1130 sp
->sfb_stats
.dequeue_stall
++;
1134 #define DTYPE_NODROP 0 /* no drop */
1135 #define DTYPE_FORCED 1 /* a "forced" drop */
1136 #define DTYPE_EARLY 2 /* an "unforced" (early) drop */
1139 sfb_addq(struct sfb
*sp
, class_queue_t
*q
, struct mbuf
*m
, struct pf_mtag
*t
)
1143 #endif /* !PF_ECN */
1144 struct pkthdr
*pkt
= &m
->m_pkthdr
;
1145 struct timespec now
;
1149 int ret
= CLASSQEQ_SUCCESS
;
1153 s
= sp
->sfb_current
;
1154 VERIFY((s
+ (s
^ 1)) == 1);
1156 /* See comments in <rdar://problem/14040693> */
1157 VERIFY(!(pkt
->pkt_flags
& PKTF_PRIV_GUARDED
));
1158 pkt
->pkt_flags
|= PKTF_PRIV_GUARDED
;
1160 /* time to swap the bins? */
1161 if (net_timercmp(&now
, &sp
->sfb_nextreset
, >=)) {
1162 net_timeradd(&now
, &sp
->sfb_hinterval
, &sp
->sfb_nextreset
);
1163 sfb_swap_bins(sp
, qlen(q
));
1164 s
= sp
->sfb_current
;
1165 VERIFY((s
+ (s
^ 1)) == 1);
1168 if (!net_timerisset(&sp
->sfb_update_time
)) {
1169 net_timeradd(&now
, &sp
->sfb_update_interval
,
1170 &sp
->sfb_update_time
);
1173 pkt
->pkt_sfb_flags
= 0;
1174 pkt
->pkt_sfb_hash16
[s
] =
1175 (SFB_HASH(&pkt
->pkt_flowid
, sizeof (pkt
->pkt_flowid
),
1176 (*sp
->sfb_bins
)[s
].fudge
) & SFB_HASHMASK
);
1177 pkt
->pkt_sfb_hash16
[s
^ 1] =
1178 (SFB_HASH(&pkt
->pkt_flowid
, sizeof (pkt
->pkt_flowid
),
1179 (*sp
->sfb_bins
)[s
^ 1].fudge
) & SFB_HASHMASK
);
1181 /* check if the queue has been stalled */
1182 sfb_detect_dequeue_stall(sp
, q
, &now
);
1184 /* see if we drop early */
1185 droptype
= DTYPE_NODROP
;
1186 if (sfb_drop_early(sp
, pkt
, &pmin
, &now
)) {
1187 /* flow control, mark or drop by sfb */
1188 if ((sp
->sfb_flags
& SFBF_FLOWCTL
) &&
1189 (pkt
->pkt_flags
& PKTF_FLOW_ADV
)) {
1191 /* drop all during suspension or for non-TCP */
1192 if ((sp
->sfb_flags
& SFBF_SUSPENDED
) ||
1193 pkt
->pkt_proto
!= IPPROTO_TCP
) {
1194 droptype
= DTYPE_EARLY
;
1195 sp
->sfb_stats
.drop_early
++;
1199 else if ((sp
->sfb_flags
& SFBF_ECN
) &&
1200 (pkt
->pkt_proto
== IPPROTO_TCP
) && /* only for TCP */
1201 ((sfb_random(sp
) & SFB_MAX_PMARK
) <= pmin
) &&
1202 mark_ecn(m
, t
, sp
->sfb_flags
) &&
1203 !(sp
->sfb_flags
& SFBF_SUSPENDED
)) {
1204 /* successfully marked; do not drop. */
1205 sp
->sfb_stats
.marked_packets
++;
1209 /* unforced drop by sfb */
1210 droptype
= DTYPE_EARLY
;
1211 sp
->sfb_stats
.drop_early
++;
1215 /* non-responsive flow penalty? */
1216 if (droptype
== DTYPE_NODROP
&& sfb_penalize(sp
, pkt
, &now
)) {
1217 droptype
= DTYPE_FORCED
;
1218 sp
->sfb_stats
.drop_pbox
++;
1222 * if max queue size is static, make it a forced drop
1223 * when the queue length hits the queue limit
1225 if (!(SFB_QUEUE_DELAYBASED(sp
)) &&
1226 droptype
== DTYPE_NODROP
&& qlen(q
) >= qlimit(q
)) {
1227 droptype
= DTYPE_FORCED
;
1228 sp
->sfb_stats
.drop_queue
++;
1232 * delay based queues have a larger maximum size to
1235 if (SFB_QUEUE_DELAYBASED(sp
) &&
1236 droptype
== DTYPE_NODROP
&&
1237 qlen(q
) >= SFB_QUEUE_DELAYBASED_MAXSIZE
) {
1238 droptype
= DTYPE_FORCED
;
1239 sp
->sfb_stats
.drop_queue
++;
1242 if (fc_adv
== 1 && droptype
!= DTYPE_FORCED
&&
1243 sfb_bin_addfcentry(sp
, pkt
)) {
1244 /* deliver flow control advisory error */
1245 if (droptype
== DTYPE_NODROP
) {
1246 ret
= CLASSQEQ_SUCCESS_FC
;
1247 VERIFY(!(sp
->sfb_flags
& SFBF_SUSPENDED
));
1248 } else if (sp
->sfb_flags
& SFBF_SUSPENDED
) {
1249 /* dropped due to suspension */
1250 ret
= CLASSQEQ_DROPPED_SP
;
1252 /* dropped due to flow-control */
1253 ret
= CLASSQEQ_DROPPED_FC
;
1256 /* if successful enqueue this packet, else drop it */
1257 if (droptype
== DTYPE_NODROP
) {
1258 net_timernsec(&now
, &pkt
->pkt_enqueue_ts
);
1261 IFCQ_CONVERT_LOCK(&sp
->sfb_ifp
->if_snd
);
1263 return ((ret
!= CLASSQEQ_SUCCESS
) ? ret
: CLASSQEQ_DROPPED
);
1266 if (!(pkt
->pkt_sfb_flags
& SFB_PKT_PBOX
))
1267 sfb_eq_update_bins(sp
, pkt
);
1269 sp
->sfb_stats
.pbox_packets
++;
1271 /* successfully queued */
1275 static struct mbuf
*
1276 sfb_getq_flow(struct sfb
*sp
, class_queue_t
*q
, u_int32_t flow
, boolean_t purge
)
1278 struct timespec now
;
1282 if (!purge
&& (sp
->sfb_flags
& SFBF_SUSPENDED
))
1287 /* flow of 0 means head of queue */
1288 if ((m
= ((flow
== 0) ? _getq(q
) : _getq_flow(q
, flow
))) == NULL
) {
1290 net_timerclear(&sp
->sfb_getqtime
);
1294 VERIFY(m
->m_flags
& M_PKTHDR
);
1297 VERIFY(pkt
->pkt_flags
& PKTF_PRIV_GUARDED
);
1300 /* calculate EWMA of dequeues */
1301 if (net_timerisset(&sp
->sfb_getqtime
)) {
1302 struct timespec delta
;
1304 net_timersub(&now
, &sp
->sfb_getqtime
, &delta
);
1305 net_timernsec(&delta
, &new);
1306 avg
= sp
->sfb_stats
.dequeue_avg
;
1308 int decay
= DEQUEUE_DECAY
;
1310 * If the time since last dequeue is
1311 * significantly greater than the current
1312 * average, weigh the average more against
1315 if (DEQUEUE_SPIKE(new, avg
))
1317 avg
= (((avg
<< decay
) - avg
) + new) >> decay
;
1321 sp
->sfb_stats
.dequeue_avg
= avg
;
1323 *(&sp
->sfb_getqtime
) = *(&now
);
1326 if (!purge
&& SFB_QUEUE_DELAYBASED(sp
)) {
1327 u_int64_t dequeue_ns
, queue_delay
= 0;
1328 net_timernsec(&now
, &dequeue_ns
);
1329 if (dequeue_ns
> pkt
->pkt_enqueue_ts
)
1330 queue_delay
= dequeue_ns
- pkt
->pkt_enqueue_ts
;
1332 if (sp
->sfb_min_qdelay
== 0 ||
1333 (queue_delay
> 0 && queue_delay
< sp
->sfb_min_qdelay
))
1334 sp
->sfb_min_qdelay
= queue_delay
;
1335 if (net_timercmp(&now
, &sp
->sfb_update_time
, >=)) {
1336 if (sp
->sfb_min_qdelay
> sp
->sfb_target_qdelay
) {
1337 if (!SFB_IS_DELAYHIGH(sp
))
1338 SFB_SET_DELAY_HIGH(sp
, q
);
1340 sp
->sfb_flags
&= ~(SFBF_DELAYHIGH
);
1341 sp
->sfb_fc_threshold
= 0;
1344 net_timeradd(&now
, &sp
->sfb_update_interval
,
1345 &sp
->sfb_update_time
);
1346 sp
->sfb_min_qdelay
= 0;
1351 * Clearpkts are the ones which were in the queue when the hash
1352 * function was perturbed. Since the perturbation value (fudge),
1353 * and thus bin information for these packets is not known, we do
1354 * not change accounting information while dequeuing these packets.
1355 * It is important not to set the hash interval too small due to
1356 * this reason. A rule of thumb is to set it to K*D, where D is
1357 * the time taken to drain queue.
1359 if (pkt
->pkt_sfb_flags
& SFB_PKT_PBOX
) {
1360 pkt
->pkt_sfb_flags
&= ~SFB_PKT_PBOX
;
1361 if (sp
->sfb_clearpkts
> 0)
1362 sp
->sfb_clearpkts
--;
1363 } else if (sp
->sfb_clearpkts
> 0) {
1364 sp
->sfb_clearpkts
--;
1366 sfb_dq_update_bins(sp
, pkt
, &now
, qsize(q
));
1369 /* See comments in <rdar://problem/14040693> */
1370 pkt
->pkt_flags
&= ~PKTF_PRIV_GUARDED
;
1373 * If the queue becomes empty before the update interval, reset
1374 * the flow control threshold
1376 if (qsize(q
) == 0) {
1377 sp
->sfb_flags
&= ~SFBF_DELAYHIGH
;
1378 sp
->sfb_min_qdelay
= 0;
1379 sp
->sfb_fc_threshold
= 0;
1380 net_timerclear(&sp
->sfb_update_time
);
1387 sfb_getq(struct sfb
*sp
, class_queue_t
*q
)
1389 return (sfb_getq_flow(sp
, q
, 0, FALSE
));
1393 sfb_purgeq(struct sfb
*sp
, class_queue_t
*q
, u_int32_t flow
, u_int32_t
*packets
,
1396 u_int32_t cnt
= 0, len
= 0;
1399 IFCQ_CONVERT_LOCK(&sp
->sfb_ifp
->if_snd
);
1401 while ((m
= sfb_getq_flow(sp
, q
, flow
, TRUE
)) != NULL
) {
1407 if (packets
!= NULL
)
1414 sfb_updateq(struct sfb
*sp
, cqev_t ev
)
1416 struct ifnet
*ifp
= sp
->sfb_ifp
;
1418 VERIFY(ifp
!= NULL
);
1421 case CLASSQ_EV_LINK_BANDWIDTH
: {
1422 u_int64_t eff_rate
= ifnet_output_linkrate(ifp
);
1424 /* update parameters only if rate has changed */
1425 if (eff_rate
== sp
->sfb_eff_rate
)
1428 if (classq_verbose
) {
1429 log(LOG_DEBUG
, "%s: SFB qid=%d, adapting to new "
1430 "eff_rate=%llu bps\n", if_name(ifp
), sp
->sfb_qid
,
1433 sfb_calc_holdtime(sp
, eff_rate
);
1434 sfb_calc_pboxtime(sp
, eff_rate
);
1435 sfb_calc_target_qdelay(sp
, eff_rate
);
1436 sfb_calc_update_interval(sp
, eff_rate
);
1440 case CLASSQ_EV_LINK_UP
:
1441 case CLASSQ_EV_LINK_DOWN
:
1442 if (classq_verbose
) {
1443 log(LOG_DEBUG
, "%s: SFB qid=%d, resetting due to "
1444 "link %s\n", if_name(ifp
), sp
->sfb_qid
,
1445 (ev
== CLASSQ_EV_LINK_UP
) ? "UP" : "DOWN");
1450 case CLASSQ_EV_LINK_LATENCY
:
1451 case CLASSQ_EV_LINK_MTU
:
1458 sfb_suspendq(struct sfb
*sp
, class_queue_t
*q
, boolean_t on
)
1461 struct ifnet
*ifp
= sp
->sfb_ifp
;
1463 VERIFY(ifp
!= NULL
);
1465 if ((on
&& (sp
->sfb_flags
& SFBF_SUSPENDED
)) ||
1466 (!on
&& !(sp
->sfb_flags
& SFBF_SUSPENDED
)))
1469 if (!(sp
->sfb_flags
& SFBF_FLOWCTL
)) {
1470 log(LOG_ERR
, "%s: SFB qid=%d, unable to %s queue since "
1471 "flow-control is not enabled", if_name(ifp
), sp
->sfb_qid
,
1472 (on
? "suspend" : "resume"));
1476 if (classq_verbose
) {
1477 log(LOG_DEBUG
, "%s: SFB qid=%d, setting state to %s",
1478 if_name(ifp
), sp
->sfb_qid
, (on
? "SUSPENDED" : "RUNNING"));
1482 sp
->sfb_flags
|= SFBF_SUSPENDED
;
1484 sp
->sfb_flags
&= ~SFBF_SUSPENDED
;
1485 sfb_swap_bins(sp
, qlen(q
));