]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/classq/classq_sfb.c
xnu-3789.1.32.tar.gz
[apple/xnu.git] / bsd / net / classq / classq_sfb.c
CommitLineData
316670eb 1/*
39236c6e 2 * Copyright (c) 2011-2013 Apple Inc. All rights reserved.
316670eb
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <sys/cdefs.h>
30#include <sys/param.h>
316670eb
A
31#include <sys/mbuf.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>
37#include <sys/proc.h>
38#include <sys/errno.h>
39#include <sys/kernel.h>
40#include <sys/kauth.h>
41
42#include <kern/zalloc.h>
43
44#include <net/if.h>
45#include <net/if_var.h>
46#include <net/if_types.h>
47#include <net/dlil.h>
39236c6e 48#include <net/flowadv.h>
316670eb
A
49
50#include <netinet/in.h>
51#include <netinet/in_systm.h>
52#include <netinet/ip.h>
53#if INET6
54#include <netinet/ip6.h>
55#endif
56
57#include <net/classq/classq_sfb.h>
58#include <net/flowhash.h>
59#include <net/net_osdep.h>
39236c6e 60#include <dev/random/randomdev.h>
316670eb
A
61
62/*
63 * Stochastic Fair Blue
64 *
65 * Wu-chang Feng, Dilip D. Kandlur, Debanjan Saha, Kang G. Shin
66 * http://www.thefengs.com/wuchang/blue/CSE-TR-387-99.pdf
67 *
68 * Based on the NS code with the following parameters:
69 *
70 * bytes: false
71 * decrement: 0.001
72 * increment: 0.005
73 * hold-time: 10ms-50ms (randomized)
74 * algorithm: 0
75 * pbox: 1
76 * pbox-time: 50-100ms (randomized)
77 * hinterval: 11-23 (randomized)
78 *
79 * This implementation uses L = 2 and N = 32 for 2 sets of:
80 *
81 * B[L][N]: L x N array of bins (L levels, N bins per level)
82 *
83 * Each set effectively creates 32^2 virtual buckets (bin combinations)
84 * while using only O(32*2) states.
85 *
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.)
91 */
92
93/*
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.
97 */
98#define SFB_HASH net_flowhash_mh3_x86_32
99#define SFB_HASHMASK HASHMASK(16)
100
101#define SFB_BINMASK(_x) \
102 ((_x) & HASHMASK(SFB_BINS_SHIFT))
103
104#define SFB_BINST(_sp, _l, _n, _c) \
105 (&(*(_sp)->sfb_bins)[_c].stats[_l][_n])
106
107#define SFB_BINFT(_sp, _l, _n, _c) \
108 (&(*(_sp)->sfb_bins)[_c].freezetime[_l][_n])
109
110#define SFB_FC_LIST(_sp, _n) \
111 (&(*(_sp)->sfb_fc_lists)[_n])
112
113/*
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.
118 */
119#define HOLDTIME_BASE (100ULL * 1000 * 1000) /* 100ms */
120#define HOLDTIME_MIN (10ULL * 1000 * 1000) /* 10ms */
121#define HOLDTIME_MAX (100ULL * 1000 * 1000) /* 100ms */
122
123/*
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.
128 */
129#define PBOXTIME_BASE (300ULL * 1000 * 1000) /* 300ms */
130#define PBOXTIME_MIN (30ULL * 1000 * 1000) /* 30ms */
131#define PBOXTIME_MAX (300ULL * 1000 * 1000) /* 300ms */
132
fe8ab488
A
133/*
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.
137 */
138#define TARGET_QDELAY_BASE (10ULL * 1000 * 1000) /* 10ms */
39037602
A
139#define TARGET_QDELAY_MIN (10ULL * 1000) /* 10us */
140#define TARGET_QDELAY_MAX (20ULL * 1000 * 1000 * 1000) /* 20s */
fe8ab488
A
141
142/*
143 * Update interval for checking the extra delay added by the queue. This
39037602 144 * should be 90-95 percentile of RTT experienced by any TCP connection
fe8ab488
A
145 * so that it will take care of the burst traffic.
146 */
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 */
150
316670eb
A
151#define SFB_RANDOM(sp, tmin, tmax) ((sfb_random(sp) % (tmax)) + (tmin))
152
39236c6e 153#define SFB_PKT_PBOX 0x1 /* in penalty box */
316670eb
A
154
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 */
157
158/*
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
166 * traffic load.
167 */
168#define SFB_INCREMENT 82 /* Q14 representation of 0.005 */
169#define SFB_DECREMENT 16 /* Q14 representation of 0.001 */
170
171#define SFB_PMARK_TH 16056 /* Q14 representation of 0.98 */
172#define SFB_PMARK_WARM 3276 /* Q14 representation of 0.2 */
173
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; \
178} while (0)
179
180#define SFB_PMARK_DEC(_bin) do { \
181 if ((_bin)->pmark > 0) { \
182 (_bin)->pmark -= sfb_decrement; \
183 if ((_bin)->pmark < 0) \
184 (_bin)->pmark = 0; \
185 } \
186} while (0)
187
fe8ab488
A
188/* Minimum nuber of bytes in queue to get flow controlled */
189#define SFB_MIN_FC_THRESHOLD_BYTES 7500
190
39037602 191#define SFB_SET_DELAY_HIGH(_sp_, _q_) do { \
fe8ab488
A
192 (_sp_)->sfb_flags |= SFBF_DELAYHIGH; \
193 (_sp_)->sfb_fc_threshold = max(SFB_MIN_FC_THRESHOLD_BYTES, \
194 (qsize((_q_)) >> 3)); \
195} while (0)
196
197#define SFB_QUEUE_DELAYBASED(_sp_) ((_sp_)->sfb_flags & SFBF_DELAYBASED)
39037602 198#define SFB_IS_DELAYHIGH(_sp_) ((_sp_)->sfb_flags & SFBF_DELAYHIGH)
fe8ab488
A
199#define SFB_QUEUE_DELAYBASED_MAXSIZE 2048 /* max pkts */
200
316670eb
A
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)
204
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))
208
209#define ABS(v) (((v) > 0) ? (v) : -(v))
210
39236c6e
A
211#define SFB_ZONE_MAX 32 /* maximum elements in zone */
212#define SFB_ZONE_NAME "classq_sfb" /* zone name */
213
214#define SFB_BINS_ZONE_MAX 32 /* maximum elements in zone */
215#define SFB_BINS_ZONE_NAME "classq_sfb_bins" /* zone name */
216
217#define SFB_FCL_ZONE_MAX 32 /* maximum elements in zone */
218#define SFB_FCL_ZONE_NAME "classq_sfb_fcl" /* zone name */
316670eb
A
219
220/* Place the flow control entries in current bin on level 0 */
221#define SFB_FC_LEVEL 0
222
39236c6e
A
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
228
316670eb
A
229static unsigned int sfb_size; /* size of zone element */
230static struct zone *sfb_zone; /* zone for sfb */
231
39236c6e
A
232static unsigned int sfb_bins_size; /* size of zone element */
233static struct zone *sfb_bins_zone; /* zone for sfb_bins */
234
235static unsigned int sfb_fcl_size; /* size of zone element */
236static struct zone *sfb_fcl_zone; /* zone for sfb_fc_lists */
237
316670eb
A
238/* internal function prototypes */
239static u_int32_t sfb_random(struct sfb *);
240static struct mbuf *sfb_getq_flow(struct sfb *, class_queue_t *, u_int32_t,
241 boolean_t);
242static void sfb_resetq(struct sfb *, cqev_t);
243static void sfb_calc_holdtime(struct sfb *, u_int64_t);
244static void sfb_calc_pboxtime(struct sfb *, u_int64_t);
245static void sfb_calc_hinterval(struct sfb *, u_int64_t *);
fe8ab488 246static void sfb_calc_update_interval(struct sfb *, u_int64_t);
316670eb 247static void sfb_swap_bins(struct sfb *, u_int32_t);
39236c6e
A
248static inline int sfb_pcheck(struct sfb *, struct pkthdr *);
249static int sfb_penalize(struct sfb *, struct pkthdr *, struct timespec *);
316670eb
A
250static void sfb_adjust_bin(struct sfb *, struct sfbbinstats *,
251 struct timespec *, struct timespec *, boolean_t);
252static void sfb_decrement_bin(struct sfb *, struct sfbbinstats *,
253 struct timespec *, struct timespec *);
254static void sfb_increment_bin(struct sfb *, struct sfbbinstats *,
255 struct timespec *, struct timespec *);
39236c6e 256static inline void sfb_dq_update_bins(struct sfb *, struct pkthdr *,
fe8ab488 257 struct timespec *, u_int32_t qsize);
39236c6e
A
258static inline void sfb_eq_update_bins(struct sfb *, struct pkthdr *);
259static int sfb_drop_early(struct sfb *, struct pkthdr *, u_int16_t *,
316670eb 260 struct timespec *);
39236c6e
A
261static boolean_t sfb_bin_addfcentry(struct sfb *, struct pkthdr *);
262static void sfb_fclist_append(struct sfb *, struct sfb_fcl *);
316670eb 263static void sfb_fclists_clean(struct sfb *sp);
fe8ab488
A
264static int sfb_bin_mark_or_drop(struct sfb *sp, struct sfbbinstats *bin);
265static void sfb_detect_dequeue_stall(struct sfb *sp, class_queue_t *,
266 struct timespec *);
316670eb
A
267
268SYSCTL_NODE(_net_classq, OID_AUTO, sfb, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "SFB");
269
270static u_int64_t sfb_holdtime = 0; /* 0 indicates "automatic" */
271SYSCTL_QUAD(_net_classq_sfb, OID_AUTO, holdtime, CTLFLAG_RW|CTLFLAG_LOCKED,
272 &sfb_holdtime, "SFB freeze time in nanoseconds");
273
274static u_int64_t sfb_pboxtime = 0; /* 0 indicates "automatic" */
275SYSCTL_QUAD(_net_classq_sfb, OID_AUTO, pboxtime, CTLFLAG_RW|CTLFLAG_LOCKED,
276 &sfb_pboxtime, "SFB penalty box time in nanoseconds");
277
278static u_int64_t sfb_hinterval;
279SYSCTL_QUAD(_net_classq_sfb, OID_AUTO, hinterval, CTLFLAG_RW|CTLFLAG_LOCKED,
280 &sfb_hinterval, "SFB hash interval in nanoseconds");
281
282static u_int32_t sfb_increment = SFB_INCREMENT;
283SYSCTL_UINT(_net_classq_sfb, OID_AUTO, increment, CTLFLAG_RW|CTLFLAG_LOCKED,
284 &sfb_increment, SFB_INCREMENT, "SFB increment [d1]");
285
286static u_int32_t sfb_decrement = SFB_DECREMENT;
287SYSCTL_UINT(_net_classq_sfb, OID_AUTO, decrement, CTLFLAG_RW|CTLFLAG_LOCKED,
288 &sfb_decrement, SFB_DECREMENT, "SFB decrement [d2]");
289
290static u_int32_t sfb_allocation = 0; /* 0 means "automatic" */
291SYSCTL_UINT(_net_classq_sfb, OID_AUTO, allocation, CTLFLAG_RW|CTLFLAG_LOCKED,
292 &sfb_allocation, 0, "SFB bin allocation");
293
294static u_int32_t sfb_ratelimit = 0;
295SYSCTL_UINT(_net_classq_sfb, OID_AUTO, ratelimit, CTLFLAG_RW|CTLFLAG_LOCKED,
296 &sfb_ratelimit, 0, "SFB rate limit");
297
fe8ab488
A
298#define KBPS (1ULL * 1000) /* 1 Kbits per second */
299#define MBPS (1ULL * 1000 * 1000) /* 1 Mbits per second */
300#define GBPS (MBPS * 1000) /* 1 Gbits per second */
316670eb
A
301
302struct sfb_time_tbl {
303 u_int64_t speed; /* uplink speed */
304 u_int64_t holdtime; /* hold time */
305 u_int64_t pboxtime; /* penalty box time */
306};
307
308static struct sfb_time_tbl sfb_ttbl[] = {
309 { 1 * MBPS, HOLDTIME_BASE * 1000, PBOXTIME_BASE * 1000 },
310 { 10 * MBPS, HOLDTIME_BASE * 100, PBOXTIME_BASE * 100 },
311 { 100 * MBPS, HOLDTIME_BASE * 10, PBOXTIME_BASE * 10 },
312 { 1 * GBPS, HOLDTIME_BASE, PBOXTIME_BASE },
313 { 10 * GBPS, HOLDTIME_BASE / 10, PBOXTIME_BASE / 10 },
314 { 100 * GBPS, HOLDTIME_BASE / 100, PBOXTIME_BASE / 100 },
315 { 0, 0, 0 }
316};
317
318void
319sfb_init(void)
320{
321 _CASSERT(SFBF_ECN4 == CLASSQF_ECN4);
322 _CASSERT(SFBF_ECN6 == CLASSQF_ECN6);
323
324 sfb_size = sizeof (struct sfb);
325 sfb_zone = zinit(sfb_size, SFB_ZONE_MAX * sfb_size,
326 0, SFB_ZONE_NAME);
327 if (sfb_zone == NULL) {
328 panic("%s: failed allocating %s", __func__, SFB_ZONE_NAME);
329 /* NOTREACHED */
330 }
331 zone_change(sfb_zone, Z_EXPAND, TRUE);
332 zone_change(sfb_zone, Z_CALLERACCT, TRUE);
39236c6e
A
333
334 sfb_bins_size = sizeof (*((struct sfb *)0)->sfb_bins);
335 sfb_bins_zone = zinit(sfb_bins_size, SFB_BINS_ZONE_MAX * sfb_bins_size,
336 0, SFB_BINS_ZONE_NAME);
337 if (sfb_bins_zone == NULL) {
338 panic("%s: failed allocating %s", __func__, SFB_BINS_ZONE_NAME);
339 /* NOTREACHED */
340 }
341 zone_change(sfb_bins_zone, Z_EXPAND, TRUE);
342 zone_change(sfb_bins_zone, Z_CALLERACCT, TRUE);
343
344 sfb_fcl_size = sizeof (*((struct sfb *)0)->sfb_fc_lists);
345 sfb_fcl_zone = zinit(sfb_fcl_size, SFB_FCL_ZONE_MAX * sfb_fcl_size,
346 0, SFB_FCL_ZONE_NAME);
347 if (sfb_fcl_zone == NULL) {
348 panic("%s: failed allocating %s", __func__, SFB_FCL_ZONE_NAME);
349 /* NOTREACHED */
350 }
351 zone_change(sfb_fcl_zone, Z_EXPAND, TRUE);
352 zone_change(sfb_fcl_zone, Z_CALLERACCT, TRUE);
316670eb
A
353}
354
355static u_int32_t
356sfb_random(struct sfb *sp)
357{
358 IFCQ_CONVERT_LOCK(&sp->sfb_ifp->if_snd);
39236c6e 359 return (RandomULong());
316670eb
A
360}
361
362static void
363sfb_calc_holdtime(struct sfb *sp, u_int64_t outbw)
364{
365 u_int64_t holdtime;
366
367 if (sfb_holdtime != 0) {
368 holdtime = sfb_holdtime;
369 } else if (outbw == 0) {
370 holdtime = SFB_RANDOM(sp, HOLDTIME_MIN, HOLDTIME_MAX);
371 } else {
372 unsigned int n, i;
373
374 n = sfb_ttbl[0].holdtime;
375 for (i = 0; sfb_ttbl[i].speed != 0; i++) {
376 if (outbw < sfb_ttbl[i].speed)
377 break;
378 n = sfb_ttbl[i].holdtime;
379 }
380 holdtime = n;
381 }
382 net_nsectimer(&holdtime, &sp->sfb_holdtime);
383}
384
385static void
386sfb_calc_pboxtime(struct sfb *sp, u_int64_t outbw)
387{
388 u_int64_t pboxtime;
389
390 if (sfb_pboxtime != 0) {
391 pboxtime = sfb_pboxtime;
392 } else if (outbw == 0) {
393 pboxtime = SFB_RANDOM(sp, PBOXTIME_MIN, PBOXTIME_MAX);
394 } else {
395 unsigned int n, i;
396
397 n = sfb_ttbl[0].pboxtime;
398 for (i = 0; sfb_ttbl[i].speed != 0; i++) {
399 if (outbw < sfb_ttbl[i].speed)
400 break;
401 n = sfb_ttbl[i].pboxtime;
402 }
403 pboxtime = n;
404 }
405 net_nsectimer(&pboxtime, &sp->sfb_pboxtime);
406 net_timerclear(&sp->sfb_pboxfreeze);
407}
408
409static void
410sfb_calc_hinterval(struct sfb *sp, u_int64_t *t)
411{
412 u_int64_t hinterval;
413 struct timespec now;
414
415 if (t != NULL) {
416 /*
417 * TODO adi@apple.com: use dq_avg to derive hinterval.
418 */
419 hinterval = *t;
420 }
421
422 if (sfb_hinterval != 0)
423 hinterval = sfb_hinterval;
424 else if (t == NULL || hinterval == 0)
425 hinterval = ((u_int64_t)SFB_HINTERVAL(sp) * NSEC_PER_SEC);
426
427 net_nsectimer(&hinterval, &sp->sfb_hinterval);
428
429 nanouptime(&now);
430 net_timeradd(&now, &sp->sfb_hinterval, &sp->sfb_nextreset);
431}
432
fe8ab488
A
433static void
434sfb_calc_update_interval(struct sfb *sp, u_int64_t out_bw)
435{
436#pragma unused(out_bw)
437 u_int64_t update_interval = 0;
39037602 438 ifclassq_calc_update_interval(&update_interval);
fe8ab488
A
439 net_nsectimer(&update_interval, &sp->sfb_update_interval);
440}
441
316670eb
A
442/*
443 * sfb support routines
444 */
445struct sfb *
446sfb_alloc(struct ifnet *ifp, u_int32_t qid, u_int32_t qlim, u_int32_t flags)
447{
448 struct sfb *sp;
39236c6e 449 int i;
316670eb
A
450
451 VERIFY(ifp != NULL && qlim > 0);
452
453 sp = zalloc(sfb_zone);
454 if (sp == NULL) {
455 log(LOG_ERR, "%s: SFB unable to allocate\n", if_name(ifp));
456 return (NULL);
457 }
316670eb 458 bzero(sp, sfb_size);
39236c6e
A
459
460 if ((sp->sfb_bins = zalloc(sfb_bins_zone)) == NULL) {
316670eb
A
461 log(LOG_ERR, "%s: SFB unable to allocate bins\n", if_name(ifp));
462 sfb_destroy(sp);
463 return (NULL);
464 }
39236c6e 465 bzero(sp->sfb_bins, sfb_bins_size);
316670eb 466
39236c6e 467 if ((sp->sfb_fc_lists = zalloc(sfb_fcl_zone)) == NULL) {
316670eb
A
468 log(LOG_ERR, "%s: SFB unable to allocate flow control lists\n",
469 if_name(ifp));
470 sfb_destroy(sp);
39037602 471 return (NULL);
316670eb 472 }
39236c6e
A
473 bzero(sp->sfb_fc_lists, sfb_fcl_size);
474
475 for (i = 0; i < SFB_BINS; ++i)
476 STAILQ_INIT(&SFB_FC_LIST(sp, i)->fclist);
316670eb 477
316670eb
A
478 sp->sfb_ifp = ifp;
479 sp->sfb_qlim = qlim;
480 sp->sfb_qid = qid;
39236c6e
A
481 sp->sfb_flags = (flags & SFBF_USERFLAGS);
482#if !PF_ECN
483 if (sp->sfb_flags & SFBF_ECN) {
484 sp->sfb_flags &= ~SFBF_ECN;
485 log(LOG_ERR, "%s: SFB qid=%d, ECN not available; ignoring "
486 "SFBF_ECN flag!\n", if_name(ifp), sp->sfb_qid);
487 }
488#endif /* !PF_ECN */
316670eb
A
489
490 sfb_resetq(sp, -1);
491
492 return (sp);
493}
494
495static void
39236c6e 496sfb_fclist_append(struct sfb *sp, struct sfb_fcl *fcl)
316670eb
A
497{
498 IFCQ_CONVERT_LOCK(&sp->sfb_ifp->if_snd);
39236c6e
A
499
500 VERIFY(STAILQ_EMPTY(&fcl->fclist) || fcl->cnt > 0);
501 sp->sfb_stats.flow_feedback += fcl->cnt;
502 fcl->cnt = 0;
503
504 flowadv_add(&fcl->fclist);
505 VERIFY(fcl->cnt == 0 && STAILQ_EMPTY(&fcl->fclist));
316670eb
A
506}
507
508static void
509sfb_fclists_clean(struct sfb *sp)
510{
511 int i;
512
39236c6e 513 /* Move all the flow control entries to the flowadv list */
316670eb 514 for (i = 0; i < SFB_BINS; ++i) {
39236c6e
A
515 struct sfb_fcl *fcl = SFB_FC_LIST(sp, i);
516 if (!STAILQ_EMPTY(&fcl->fclist))
316670eb
A
517 sfb_fclist_append(sp, fcl);
518 }
519}
520
521void
522sfb_destroy(struct sfb *sp)
523{
524 sfb_fclists_clean(sp);
525 if (sp->sfb_bins != NULL) {
39236c6e 526 zfree(sfb_bins_zone, sp->sfb_bins);
316670eb
A
527 sp->sfb_bins = NULL;
528 }
529 if (sp->sfb_fc_lists != NULL) {
39236c6e 530 zfree(sfb_fcl_zone, sp->sfb_fc_lists);
316670eb
A
531 sp->sfb_fc_lists = NULL;
532 }
533 zfree(sfb_zone, sp);
534}
535
536static void
537sfb_resetq(struct sfb *sp, cqev_t ev)
538{
539 struct ifnet *ifp = sp->sfb_ifp;
540 u_int64_t eff_rate;
541
542 VERIFY(ifp != NULL);
543
544 if (ev != CLASSQ_EV_LINK_DOWN) {
545 (*sp->sfb_bins)[0].fudge = sfb_random(sp);
546 (*sp->sfb_bins)[1].fudge = sfb_random(sp);
547 sp->sfb_allocation = ((sfb_allocation == 0) ?
548 (sp->sfb_qlim / 3) : sfb_allocation);
549 sp->sfb_drop_thresh = sp->sfb_allocation +
550 (sp->sfb_allocation >> 1);
551 }
552
553 sp->sfb_clearpkts = 0;
554 sp->sfb_current = 0;
555
556 eff_rate = ifnet_output_linkrate(ifp);
557 sp->sfb_eff_rate = eff_rate;
558
559 sfb_calc_holdtime(sp, eff_rate);
560 sfb_calc_pboxtime(sp, eff_rate);
561 sfb_calc_hinterval(sp, NULL);
39037602 562 ifclassq_calc_target_qdelay(ifp, &sp->sfb_target_qdelay);
fe8ab488 563 sfb_calc_update_interval(sp, eff_rate);
316670eb
A
564
565 if (ev == CLASSQ_EV_LINK_DOWN ||
39037602 566 ev == CLASSQ_EV_LINK_UP)
316670eb
A
567 sfb_fclists_clean(sp);
568
569 bzero(sp->sfb_bins, sizeof (*sp->sfb_bins));
570 bzero(&sp->sfb_stats, sizeof (sp->sfb_stats));
571
572 if (ev == CLASSQ_EV_LINK_DOWN || !classq_verbose)
573 return;
574
575 log(LOG_DEBUG, "%s: SFB qid=%d, holdtime=%llu nsec, "
576 "pboxtime=%llu nsec, allocation=%d, drop_thresh=%d, "
fe8ab488
A
577 "hinterval=%d sec, sfb_bins=%d bytes, eff_rate=%llu bps"
578 "target_qdelay= %llu nsec "
579 "update_interval=%llu sec %llu nsec flags=0x%x\n",
316670eb
A
580 if_name(ifp), sp->sfb_qid, (u_int64_t)sp->sfb_holdtime.tv_nsec,
581 (u_int64_t)sp->sfb_pboxtime.tv_nsec,
582 (u_int32_t)sp->sfb_allocation, (u_int32_t)sp->sfb_drop_thresh,
583 (int)sp->sfb_hinterval.tv_sec, (int)sizeof (*sp->sfb_bins),
fe8ab488
A
584 eff_rate, (u_int64_t)sp->sfb_target_qdelay,
585 (u_int64_t)sp->sfb_update_interval.tv_sec,
586 (u_int64_t)sp->sfb_update_interval.tv_nsec, sp->sfb_flags);
316670eb
A
587}
588
589void
590sfb_getstats(struct sfb *sp, struct sfb_stats *sps)
591{
592 sps->allocation = sp->sfb_allocation;
593 sps->dropthresh = sp->sfb_drop_thresh;
594 sps->clearpkts = sp->sfb_clearpkts;
595 sps->current = sp->sfb_current;
fe8ab488
A
596 sps->target_qdelay = sp->sfb_target_qdelay;
597 sps->min_estdelay = sp->sfb_min_qdelay;
598 sps->delay_fcthreshold = sp->sfb_fc_threshold;
599 sps->flags = sp->sfb_flags;
316670eb
A
600
601 net_timernsec(&sp->sfb_holdtime, &sp->sfb_stats.hold_time);
602 net_timernsec(&sp->sfb_pboxtime, &sp->sfb_stats.pbox_time);
603 net_timernsec(&sp->sfb_hinterval, &sp->sfb_stats.rehash_intval);
fe8ab488 604 net_timernsec(&sp->sfb_update_interval, &sps->update_interval);
316670eb
A
605 *(&(sps->sfbstats)) = *(&(sp->sfb_stats));
606
607 _CASSERT(sizeof ((*sp->sfb_bins)[0].stats) ==
608 sizeof (sps->binstats[0].stats));
609
610 bcopy(&(*sp->sfb_bins)[0].stats, &sps->binstats[0].stats,
611 sizeof (sps->binstats[0].stats));
612 bcopy(&(*sp->sfb_bins)[1].stats, &sps->binstats[1].stats,
613 sizeof (sps->binstats[1].stats));
614}
615
616static void
617sfb_swap_bins(struct sfb *sp, u_int32_t len)
618{
619 int i, j, s;
620
621 if (sp->sfb_flags & SFBF_SUSPENDED)
622 return;
623
624 s = sp->sfb_current;
625 VERIFY((s + (s ^ 1)) == 1);
626
627 (*sp->sfb_bins)[s].fudge = sfb_random(sp); /* recompute perturbation */
628 sp->sfb_clearpkts = len;
629 sp->sfb_stats.num_rehash++;
630
631 s = (sp->sfb_current ^= 1); /* flip the bit (swap current) */
632
633 if (classq_verbose) {
634 log(LOG_DEBUG, "%s: SFB qid=%d, set %d is now current, "
635 "qlen=%d\n", if_name(sp->sfb_ifp), sp->sfb_qid, s, len);
636 }
637
638 /* clear freezetime for all current bins */
639 bzero(&(*sp->sfb_bins)[s].freezetime,
640 sizeof ((*sp->sfb_bins)[s].freezetime));
641
642 /* clear/adjust bin statistics and flow control lists */
643 for (i = 0; i < SFB_BINS; i++) {
39236c6e 644 struct sfb_fcl *fcl = SFB_FC_LIST(sp, i);
316670eb 645
39236c6e 646 if (!STAILQ_EMPTY(&fcl->fclist))
316670eb
A
647 sfb_fclist_append(sp, fcl);
648
649 for (j = 0; j < SFB_LEVELS; j++) {
650 struct sfbbinstats *cbin, *wbin;
651
652 cbin = SFB_BINST(sp, j, i, s); /* current */
653 wbin = SFB_BINST(sp, j, i, s ^ 1); /* warm-up */
654
655 cbin->pkts = 0;
fe8ab488 656 cbin->bytes = 0;
316670eb
A
657 if (cbin->pmark > SFB_MAX_PMARK)
658 cbin->pmark = SFB_MAX_PMARK;
659 if (cbin->pmark < 0)
660 cbin->pmark = 0;
661
662 /*
663 * Keep pmark from before to identify
664 * non-responsives immediately.
665 */
666 if (wbin->pmark > SFB_PMARK_WARM)
667 wbin->pmark = SFB_PMARK_WARM;
668 }
669 }
670}
671
672static inline int
39236c6e 673sfb_pcheck(struct sfb *sp, struct pkthdr *pkt)
316670eb
A
674{
675#if SFB_LEVELS != 2
676 int i, n;
677#endif /* SFB_LEVELS != 2 */
678 int s;
679
680 s = sp->sfb_current;
681 VERIFY((s + (s ^ 1)) == 1);
682
683 /*
684 * For current bins, returns 1 if all pmark >= SFB_PMARK_TH,
685 * 0 otherwise; optimize for SFB_LEVELS=2.
686 */
687#if SFB_LEVELS == 2
688 /*
689 * Level 0: bin index at [0] for set 0; [2] for set 1
690 * Level 1: bin index at [1] for set 0; [3] for set 1
691 */
39236c6e 692 if (SFB_BINST(sp, 0, SFB_BINMASK(pkt->pkt_sfb_hash8[(s << 1)]),
316670eb 693 s)->pmark < SFB_PMARK_TH ||
39236c6e 694 SFB_BINST(sp, 1, SFB_BINMASK(pkt->pkt_sfb_hash8[(s << 1) + 1]),
316670eb
A
695 s)->pmark < SFB_PMARK_TH)
696 return (0);
697#else /* SFB_LEVELS != 2 */
698 for (i = 0; i < SFB_LEVELS; i++) {
699 if (s == 0) /* set 0, bin index [0,1] */
39236c6e 700 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i]);
316670eb 701 else /* set 1, bin index [2,3] */
39236c6e 702 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i + 2]);
316670eb
A
703
704 if (SFB_BINST(sp, i, n, s)->pmark < SFB_PMARK_TH)
705 return (0);
706 }
707#endif /* SFB_LEVELS != 2 */
708 return (1);
709}
710
711static int
39236c6e 712sfb_penalize(struct sfb *sp, struct pkthdr *pkt, struct timespec *now)
316670eb
A
713{
714 struct timespec delta = { 0, 0 };
715
716 /* If minimum pmark of current bins is < SFB_PMARK_TH, we're done */
39236c6e 717 if (!sfb_ratelimit || !sfb_pcheck(sp, pkt))
316670eb
A
718 return (0);
719
720 net_timersub(now, &sp->sfb_pboxfreeze, &delta);
721 if (net_timercmp(&delta, &sp->sfb_pboxtime, <)) {
722#if SFB_LEVELS != 2
723 int i;
724#endif /* SFB_LEVELS != 2 */
725 struct sfbbinstats *bin;
726 int n, w;
727
728 w = sp->sfb_current ^ 1;
729 VERIFY((w + (w ^ 1)) == 1);
730
731 /*
732 * Update warm-up bins; optimize for SFB_LEVELS=2
733 */
734#if SFB_LEVELS == 2
735 /* Level 0: bin index at [0] for set 0; [2] for set 1 */
39236c6e 736 n = SFB_BINMASK(pkt->pkt_sfb_hash8[(w << 1)]);
316670eb
A
737 bin = SFB_BINST(sp, 0, n, w);
738 if (bin->pkts >= sp->sfb_allocation)
739 sfb_increment_bin(sp, bin, SFB_BINFT(sp, 0, n, w), now);
740
741 /* Level 0: bin index at [1] for set 0; [3] for set 1 */
39236c6e 742 n = SFB_BINMASK(pkt->pkt_sfb_hash8[(w << 1) + 1]);
316670eb
A
743 bin = SFB_BINST(sp, 1, n, w);
744 if (bin->pkts >= sp->sfb_allocation)
745 sfb_increment_bin(sp, bin, SFB_BINFT(sp, 1, n, w), now);
746#else /* SFB_LEVELS != 2 */
747 for (i = 0; i < SFB_LEVELS; i++) {
748 if (w == 0) /* set 0, bin index [0,1] */
39236c6e 749 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i]);
316670eb 750 else /* set 1, bin index [2,3] */
39236c6e 751 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i + 2]);
316670eb
A
752
753 bin = SFB_BINST(sp, i, n, w);
754 if (bin->pkts >= sp->sfb_allocation) {
755 sfb_increment_bin(sp, bin,
756 SFB_BINFT(sp, i, n, w), now);
757 }
758 }
759#endif /* SFB_LEVELS != 2 */
760 return (1);
761 }
762
763 /* non-conformant or else misclassified flow; queue it anyway */
39236c6e 764 pkt->pkt_sfb_flags |= SFB_PKT_PBOX;
316670eb
A
765 *(&sp->sfb_pboxfreeze) = *now;
766
767 return (0);
768}
769
770static void
771sfb_adjust_bin(struct sfb *sp, struct sfbbinstats *bin, struct timespec *ft,
772 struct timespec *now, boolean_t inc)
773{
774 struct timespec delta;
775
776 net_timersub(now, ft, &delta);
777 if (net_timercmp(&delta, &sp->sfb_holdtime, <)) {
778 if (classq_verbose > 1) {
779 log(LOG_DEBUG, "%s: SFB qid=%d, %s update frozen "
780 "(delta=%llu nsec)\n", if_name(sp->sfb_ifp),
781 sp->sfb_qid, inc ? "increment" : "decrement",
782 (u_int64_t)delta.tv_nsec);
783 }
784 return;
785 }
786
787 /* increment/decrement marking probability */
788 *ft = *now;
789 if (inc)
790 SFB_PMARK_INC(bin);
791 else
792 SFB_PMARK_DEC(bin);
793}
794
795static void
796sfb_decrement_bin(struct sfb *sp, struct sfbbinstats *bin, struct timespec *ft,
797 struct timespec *now)
798{
799 return (sfb_adjust_bin(sp, bin, ft, now, FALSE));
800}
801
802static void
803sfb_increment_bin(struct sfb *sp, struct sfbbinstats *bin, struct timespec *ft,
804 struct timespec *now)
805{
806 return (sfb_adjust_bin(sp, bin, ft, now, TRUE));
807}
808
809static inline void
fe8ab488
A
810sfb_dq_update_bins(struct sfb *sp, struct pkthdr *pkt,
811 struct timespec *now, u_int32_t qsize)
316670eb
A
812{
813#if SFB_LEVELS != 2 || SFB_FC_LEVEL != 0
814 int i;
815#endif /* SFB_LEVELS != 2 || SFB_FC_LEVEL != 0 */
816 struct sfbbinstats *bin;
817 int s, n;
39236c6e 818 struct sfb_fcl *fcl = NULL;
316670eb
A
819
820 s = sp->sfb_current;
821 VERIFY((s + (s ^ 1)) == 1);
822
823 /*
824 * Update current bins; optimize for SFB_LEVELS=2 and SFB_FC_LEVEL=0
825 */
826#if SFB_LEVELS == 2 && SFB_FC_LEVEL == 0
827 /* Level 0: bin index at [0] for set 0; [2] for set 1 */
39236c6e 828 n = SFB_BINMASK(pkt->pkt_sfb_hash8[(s << 1)]);
316670eb
A
829 bin = SFB_BINST(sp, 0, n, s);
830
fe8ab488
A
831 VERIFY(bin->pkts > 0 && bin->bytes >= (u_int32_t)pkt->len);
832 bin->pkts--;
833 bin->bytes -= pkt->len;
834
835 if (bin->pkts == 0)
316670eb 836 sfb_decrement_bin(sp, bin, SFB_BINFT(sp, 0, n, s), now);
fe8ab488
A
837
838 /* Deliver flow control feedback to the sockets */
839 if (SFB_QUEUE_DELAYBASED(sp)) {
840 if (!(SFB_IS_DELAYHIGH(sp)) ||
841 bin->bytes <= sp->sfb_fc_threshold ||
842 bin->pkts == 0 || qsize == 0)
843 fcl = SFB_FC_LIST(sp, n);
844 } else if (bin->pkts <= (sp->sfb_allocation >> 2)) {
845 fcl = SFB_FC_LIST(sp, n);
316670eb 846 }
fe8ab488
A
847
848 if (fcl != NULL && !STAILQ_EMPTY(&fcl->fclist))
849 sfb_fclist_append(sp, fcl);
850 fcl = NULL;
316670eb
A
851
852 /* Level 1: bin index at [1] for set 0; [3] for set 1 */
39236c6e 853 n = SFB_BINMASK(pkt->pkt_sfb_hash8[(s << 1) + 1]);
316670eb
A
854 bin = SFB_BINST(sp, 1, n, s);
855
fe8ab488
A
856 VERIFY(bin->pkts > 0 && bin->bytes >= (u_int64_t)pkt->len);
857 bin->pkts--;
858 bin->bytes -= pkt->len;
859 if (bin->pkts == 0)
316670eb
A
860 sfb_decrement_bin(sp, bin, SFB_BINFT(sp, 1, n, s), now);
861#else /* SFB_LEVELS != 2 || SFB_FC_LEVEL != 0 */
862 for (i = 0; i < SFB_LEVELS; i++) {
863 if (s == 0) /* set 0, bin index [0,1] */
39236c6e 864 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i]);
316670eb 865 else /* set 1, bin index [2,3] */
39236c6e 866 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i + 2]);
316670eb
A
867
868 bin = SFB_BINST(sp, i, n, s);
869
fe8ab488
A
870 VERIFY(bin->pkts > 0 && bin->bytes >= pkt->len);
871 bin->pkts--;
872 bin->bytes -= pkt->len;
873 if (bin->pkts == 0)
316670eb
A
874 sfb_decrement_bin(sp, bin,
875 SFB_BINFT(sp, i, n, s), now);
fe8ab488
A
876 if (i != SFB_FC_LEVEL)
877 continue;
878 if (SFB_QUEUE_DELAYBASED(sp)) {
879 if (!(SFB_IS_DELAYHIGH(sp)) ||
880 bin->bytes <= sp->sfb_fc_threshold)
316670eb 881 fcl = SFB_FC_LIST(sp, n);
fe8ab488
A
882 } else if (bin->pkts <= (sp->sfb_allocation >> 2)) {
883 fcl = SFB_FC_LIST(sp, n);
316670eb 884 }
fe8ab488
A
885 if (fcl != NULL && !STAILQ_EMPTY(&fcl->fclist))
886 sfb_fclist_append(sp, fcl);
887 fcl = NULL;
316670eb
A
888 }
889#endif /* SFB_LEVELS != 2 || SFB_FC_LEVEL != 0 */
890}
891
892static inline void
39236c6e 893sfb_eq_update_bins(struct sfb *sp, struct pkthdr *pkt)
316670eb
A
894{
895#if SFB_LEVELS != 2
896 int i, n;
897#endif /* SFB_LEVELS != 2 */
898 int s;
fe8ab488 899 struct sfbbinstats *bin;
316670eb
A
900 s = sp->sfb_current;
901 VERIFY((s + (s ^ 1)) == 1);
902
903 /*
904 * Update current bins; optimize for SFB_LEVELS=2
905 */
906#if SFB_LEVELS == 2
907 /* Level 0: bin index at [0] for set 0; [2] for set 1 */
fe8ab488
A
908 bin = SFB_BINST(sp, 0,
909 SFB_BINMASK(pkt->pkt_sfb_hash8[(s << 1)]), s);
910 bin->pkts++;
911 bin->bytes += pkt->len;
316670eb
A
912
913 /* Level 1: bin index at [1] for set 0; [3] for set 1 */
fe8ab488
A
914 bin = SFB_BINST(sp, 1,
915 SFB_BINMASK(pkt->pkt_sfb_hash8[(s << 1) + 1]), s);
916 bin->pkts++;
917 bin->bytes += pkt->len;
918
316670eb
A
919#else /* SFB_LEVELS != 2 */
920 for (i = 0; i < SFB_LEVELS; i++) {
921 if (s == 0) /* set 0, bin index [0,1] */
39236c6e 922 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i]);
316670eb 923 else /* set 1, bin index [2,3] */
39236c6e 924 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i + 2]);
316670eb 925
fe8ab488
A
926 bin = SFB_BINST(sp, i, n, s);
927 bin->pkts++;
928 bin->bytes += pkt->len;
316670eb
A
929 }
930#endif /* SFB_LEVELS != 2 */
931}
932
933static boolean_t
39236c6e 934sfb_bin_addfcentry(struct sfb *sp, struct pkthdr *pkt)
316670eb 935{
39236c6e
A
936 struct flowadv_fcentry *fce;
937 u_int32_t flowsrc, flowid;
938 struct sfb_fcl *fcl;
316670eb
A
939 int s;
940
941 s = sp->sfb_current;
942 VERIFY((s + (s ^ 1)) == 1);
943
39236c6e
A
944 flowsrc = pkt->pkt_flowsrc;
945 flowid = pkt->pkt_flowid;
316670eb 946
39236c6e
A
947 if (flowid == 0) {
948 sp->sfb_stats.null_flowid++;
316670eb
A
949 return (FALSE);
950 }
951
952 /*
953 * Use value at index 0 for set 0 and
954 * value at index 2 for set 1
955 */
39236c6e
A
956 fcl = SFB_FC_LIST(sp, SFB_BINMASK(pkt->pkt_sfb_hash8[(s << 1)]));
957 STAILQ_FOREACH(fce, &fcl->fclist, fce_link) {
958 if (fce->fce_flowsrc == flowsrc &&
959 fce->fce_flowid == flowid) {
316670eb
A
960 /* Already on flow control list; just return */
961 return (TRUE);
962 }
963 }
964
965 IFCQ_CONVERT_LOCK(&sp->sfb_ifp->if_snd);
39236c6e 966 fce = flowadv_alloc_entry(M_WAITOK);
316670eb 967 if (fce != NULL) {
39236c6e
A
968 fce->fce_flowsrc = flowsrc;
969 fce->fce_flowid = flowid;
970 STAILQ_INSERT_TAIL(&fcl->fclist, fce, fce_link);
971 fcl->cnt++;
316670eb
A
972 sp->sfb_stats.flow_controlled++;
973 }
974
975 return (fce != NULL);
976}
977
fe8ab488
A
978/*
979 * check if this flow needs to be flow-controlled or if this
980 * packet needs to be dropped.
981 */
982static int
983sfb_bin_mark_or_drop(struct sfb *sp, struct sfbbinstats *bin)
984{
985 int ret = 0;
986 if (SFB_QUEUE_DELAYBASED(sp)) {
987 /*
988 * Mark or drop if this bin has more
989 * bytes than the flowcontrol threshold.
990 */
991 if (SFB_IS_DELAYHIGH(sp) &&
992 bin->bytes >= (sp->sfb_fc_threshold << 1))
993 ret = 1;
994 } else {
995 if (bin->pkts >= sp->sfb_allocation &&
996 bin->pkts >= sp->sfb_drop_thresh)
997 ret = 1; /* drop or mark */
998 }
999 return (ret);
1000}
1001
316670eb
A
1002/*
1003 * early-drop probability is kept in pmark of each bin of the flow
1004 */
1005static int
39236c6e 1006sfb_drop_early(struct sfb *sp, struct pkthdr *pkt, u_int16_t *pmin,
316670eb
A
1007 struct timespec *now)
1008{
1009#if SFB_LEVELS != 2
1010 int i;
1011#endif /* SFB_LEVELS != 2 */
1012 struct sfbbinstats *bin;
1013 int s, n, ret = 0;
1014
1015 s = sp->sfb_current;
1016 VERIFY((s + (s ^ 1)) == 1);
1017
1018 *pmin = (u_int16_t)-1;
1019
1020 /*
1021 * Update current bins; optimize for SFB_LEVELS=2
1022 */
1023#if SFB_LEVELS == 2
1024 /* Level 0: bin index at [0] for set 0; [2] for set 1 */
39236c6e 1025 n = SFB_BINMASK(pkt->pkt_sfb_hash8[(s << 1)]);
316670eb
A
1026 bin = SFB_BINST(sp, 0, n, s);
1027 if (*pmin > (u_int16_t)bin->pmark)
1028 *pmin = (u_int16_t)bin->pmark;
1029
fe8ab488
A
1030
1031 /* Update SFB probability */
1032 if (bin->pkts >= sp->sfb_allocation)
316670eb 1033 sfb_increment_bin(sp, bin, SFB_BINFT(sp, 0, n, s), now);
fe8ab488
A
1034
1035 ret = sfb_bin_mark_or_drop(sp, bin);
316670eb
A
1036
1037 /* Level 1: bin index at [1] for set 0; [3] for set 1 */
39236c6e 1038 n = SFB_BINMASK(pkt->pkt_sfb_hash8[(s << 1) + 1]);
316670eb
A
1039 bin = SFB_BINST(sp, 1, n, s);
1040 if (*pmin > (u_int16_t)bin->pmark)
1041 *pmin = (u_int16_t)bin->pmark;
1042
fe8ab488 1043 if (bin->pkts >= sp->sfb_allocation)
316670eb 1044 sfb_increment_bin(sp, bin, SFB_BINFT(sp, 1, n, s), now);
316670eb
A
1045#else /* SFB_LEVELS != 2 */
1046 for (i = 0; i < SFB_LEVELS; i++) {
1047 if (s == 0) /* set 0, bin index [0,1] */
39236c6e 1048 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i]);
316670eb 1049 else /* set 1, bin index [2,3] */
39236c6e 1050 n = SFB_BINMASK(pkt->pkt_sfb_hash8[i + 2]);
316670eb
A
1051
1052 bin = SFB_BINST(sp, i, n, s);
1053 if (*pmin > (u_int16_t)bin->pmark)
1054 *pmin = (u_int16_t)bin->pmark;
1055
fe8ab488 1056 if (bin->pkts >= sp->sfb_allocation)
316670eb
A
1057 sfb_increment_bin(sp, bin,
1058 SFB_BINFT(sp, i, n, s), now);
fe8ab488
A
1059 if (i == SFB_FC_LEVEL)
1060 ret = sfb_bin_mark_or_drop(sp, bin);
316670eb
A
1061 }
1062#endif /* SFB_LEVELS != 2 */
1063
1064 if (sp->sfb_flags & SFBF_SUSPENDED)
1065 ret = 1; /* drop or mark */
1066
1067 return (ret);
1068}
1069
fe8ab488
A
1070void
1071sfb_detect_dequeue_stall(struct sfb *sp, class_queue_t *q,
1072 struct timespec *now)
1073{
1074 struct timespec max_getqtime;
1075
1076 if (!SFB_QUEUE_DELAYBASED(sp) || SFB_IS_DELAYHIGH(sp) ||
1077 qsize(q) <= SFB_MIN_FC_THRESHOLD_BYTES ||
1078 !net_timerisset(&sp->sfb_getqtime))
1079 return;
1080
1081 net_timeradd(&sp->sfb_getqtime, &sp->sfb_update_interval,
1082 &max_getqtime);
1083 if (net_timercmp(now, &max_getqtime, >)) {
1084 /*
1085 * No packets have been dequeued in an update interval
1086 * worth of time. It means that the queue is stalled
1087 */
1088 SFB_SET_DELAY_HIGH(sp, q);
1089 sp->sfb_stats.dequeue_stall++;
1090 }
1091}
1092
316670eb
A
1093#define DTYPE_NODROP 0 /* no drop */
1094#define DTYPE_FORCED 1 /* a "forced" drop */
1095#define DTYPE_EARLY 2 /* an "unforced" (early) drop */
1096
1097int
1098sfb_addq(struct sfb *sp, class_queue_t *q, struct mbuf *m, struct pf_mtag *t)
1099{
39236c6e
A
1100#if !PF_ECN
1101#pragma unused(t)
1102#endif /* !PF_ECN */
1103 struct pkthdr *pkt = &m->m_pkthdr;
316670eb
A
1104 struct timespec now;
1105 int droptype, s;
1106 u_int16_t pmin;
1107 int fc_adv = 0;
1108 int ret = CLASSQEQ_SUCCESS;
3e170ce0 1109 u_int32_t maxqsize = 0;
316670eb
A
1110
1111 s = sp->sfb_current;
1112 VERIFY((s + (s ^ 1)) == 1);
1113
fe8ab488
A
1114 /* See comments in <rdar://problem/14040693> */
1115 VERIFY(!(pkt->pkt_flags & PKTF_PRIV_GUARDED));
1116 pkt->pkt_flags |= PKTF_PRIV_GUARDED;
1117
39037602
A
1118 if (pkt->pkt_timestamp > 0) {
1119 net_nsectimer(&pkt->pkt_timestamp, &now);
3e170ce0
A
1120 } else {
1121 nanouptime(&now);
39037602 1122 net_timernsec(&now, &pkt->pkt_timestamp);
3e170ce0
A
1123 }
1124
316670eb
A
1125 /* time to swap the bins? */
1126 if (net_timercmp(&now, &sp->sfb_nextreset, >=)) {
1127 net_timeradd(&now, &sp->sfb_hinterval, &sp->sfb_nextreset);
1128 sfb_swap_bins(sp, qlen(q));
1129 s = sp->sfb_current;
1130 VERIFY((s + (s ^ 1)) == 1);
1131 }
1132
fe8ab488
A
1133 if (!net_timerisset(&sp->sfb_update_time)) {
1134 net_timeradd(&now, &sp->sfb_update_interval,
1135 &sp->sfb_update_time);
1136 }
1137
3e170ce0
A
1138 /*
1139 * If getq time is not set because this is the first packet
1140 * or after idle time, set it now so that we can detect a stall.
1141 */
1142 if (qsize(q) == 0 && !net_timerisset(&sp->sfb_getqtime))
1143 *(&sp->sfb_getqtime) = *(&now);
1144
39236c6e
A
1145 pkt->pkt_sfb_flags = 0;
1146 pkt->pkt_sfb_hash16[s] =
1147 (SFB_HASH(&pkt->pkt_flowid, sizeof (pkt->pkt_flowid),
316670eb 1148 (*sp->sfb_bins)[s].fudge) & SFB_HASHMASK);
39236c6e
A
1149 pkt->pkt_sfb_hash16[s ^ 1] =
1150 (SFB_HASH(&pkt->pkt_flowid, sizeof (pkt->pkt_flowid),
316670eb
A
1151 (*sp->sfb_bins)[s ^ 1].fudge) & SFB_HASHMASK);
1152
fe8ab488
A
1153 /* check if the queue has been stalled */
1154 sfb_detect_dequeue_stall(sp, q, &now);
1155
316670eb
A
1156 /* see if we drop early */
1157 droptype = DTYPE_NODROP;
39236c6e 1158 if (sfb_drop_early(sp, pkt, &pmin, &now)) {
316670eb
A
1159 /* flow control, mark or drop by sfb */
1160 if ((sp->sfb_flags & SFBF_FLOWCTL) &&
39236c6e 1161 (pkt->pkt_flags & PKTF_FLOW_ADV)) {
316670eb
A
1162 fc_adv = 1;
1163 /* drop all during suspension or for non-TCP */
1164 if ((sp->sfb_flags & SFBF_SUSPENDED) ||
39236c6e 1165 pkt->pkt_proto != IPPROTO_TCP) {
316670eb
A
1166 droptype = DTYPE_EARLY;
1167 sp->sfb_stats.drop_early++;
1168 }
39236c6e
A
1169 }
1170#if PF_ECN
1171 else if ((sp->sfb_flags & SFBF_ECN) &&
1172 (pkt->pkt_proto == IPPROTO_TCP) && /* only for TCP */
316670eb
A
1173 ((sfb_random(sp) & SFB_MAX_PMARK) <= pmin) &&
1174 mark_ecn(m, t, sp->sfb_flags) &&
1175 !(sp->sfb_flags & SFBF_SUSPENDED)) {
1176 /* successfully marked; do not drop. */
1177 sp->sfb_stats.marked_packets++;
39236c6e
A
1178 }
1179#endif /* PF_ECN */
1180 else {
316670eb
A
1181 /* unforced drop by sfb */
1182 droptype = DTYPE_EARLY;
1183 sp->sfb_stats.drop_early++;
1184 }
1185 }
1186
1187 /* non-responsive flow penalty? */
39236c6e 1188 if (droptype == DTYPE_NODROP && sfb_penalize(sp, pkt, &now)) {
316670eb
A
1189 droptype = DTYPE_FORCED;
1190 sp->sfb_stats.drop_pbox++;
1191 }
1192
3e170ce0
A
1193 if (SFB_QUEUE_DELAYBASED(sp))
1194 maxqsize = SFB_QUEUE_DELAYBASED_MAXSIZE;
1195 else
1196 maxqsize = qlimit(q);
fe8ab488
A
1197
1198 /*
3e170ce0
A
1199 * When the queue length hits the queue limit, make it a forced
1200 * drop
fe8ab488 1201 */
3e170ce0
A
1202 if (droptype == DTYPE_NODROP && qlen(q) >= maxqsize) {
1203 if (pkt->pkt_proto == IPPROTO_TCP &&
4bd07ac2 1204 qlen(q) < (maxqsize + (maxqsize >> 1)) &&
3e170ce0
A
1205 ((pkt->pkt_flags & PKTF_TCP_REXMT) ||
1206 (sp->sfb_flags & SFBF_LAST_PKT_DROPPED))) {
1207 /*
1208 * At some level, dropping packets will make the
1209 * flows backoff and will keep memory requirements
1210 * under control. But we should not cause a tail
1211 * drop because it can take a long time for a
1212 * TCP flow to recover. We should try to drop
1213 * alternate packets instead.
1214 */
1215 sp->sfb_flags &= ~SFBF_LAST_PKT_DROPPED;
1216 } else {
1217 droptype = DTYPE_FORCED;
1218 sp->sfb_stats.drop_queue++;
1219 sp->sfb_flags |= SFBF_LAST_PKT_DROPPED;
1220 }
316670eb
A
1221 }
1222
1223 if (fc_adv == 1 && droptype != DTYPE_FORCED &&
39236c6e 1224 sfb_bin_addfcentry(sp, pkt)) {
316670eb
A
1225 /* deliver flow control advisory error */
1226 if (droptype == DTYPE_NODROP) {
1227 ret = CLASSQEQ_SUCCESS_FC;
1228 VERIFY(!(sp->sfb_flags & SFBF_SUSPENDED));
1229 } else if (sp->sfb_flags & SFBF_SUSPENDED) {
1230 /* dropped due to suspension */
1231 ret = CLASSQEQ_DROPPED_SP;
1232 } else {
1233 /* dropped due to flow-control */
1234 ret = CLASSQEQ_DROPPED_FC;
1235 }
1236 }
316670eb
A
1237 /* if successful enqueue this packet, else drop it */
1238 if (droptype == DTYPE_NODROP) {
1239 _addq(q, m);
1240 } else {
1241 IFCQ_CONVERT_LOCK(&sp->sfb_ifp->if_snd);
1242 m_freem(m);
1243 return ((ret != CLASSQEQ_SUCCESS) ? ret : CLASSQEQ_DROPPED);
1244 }
1245
39236c6e
A
1246 if (!(pkt->pkt_sfb_flags & SFB_PKT_PBOX))
1247 sfb_eq_update_bins(sp, pkt);
316670eb
A
1248 else
1249 sp->sfb_stats.pbox_packets++;
1250
1251 /* successfully queued */
1252 return (ret);
1253}
1254
1255static struct mbuf *
1256sfb_getq_flow(struct sfb *sp, class_queue_t *q, u_int32_t flow, boolean_t purge)
1257{
1258 struct timespec now;
1259 struct mbuf *m;
39236c6e 1260 struct pkthdr *pkt;
316670eb
A
1261
1262 if (!purge && (sp->sfb_flags & SFBF_SUSPENDED))
1263 return (NULL);
1264
1265 nanouptime(&now);
1266
1267 /* flow of 0 means head of queue */
1268 if ((m = ((flow == 0) ? _getq(q) : _getq_flow(q, flow))) == NULL) {
1269 if (!purge)
1270 net_timerclear(&sp->sfb_getqtime);
1271 return (NULL);
1272 }
1273
1274 VERIFY(m->m_flags & M_PKTHDR);
1275
39236c6e 1276 pkt = &m->m_pkthdr;
fe8ab488 1277 VERIFY(pkt->pkt_flags & PKTF_PRIV_GUARDED);
316670eb
A
1278
1279 if (!purge) {
1280 /* calculate EWMA of dequeues */
1281 if (net_timerisset(&sp->sfb_getqtime)) {
1282 struct timespec delta;
1283 u_int64_t avg, new;
316670eb
A
1284 net_timersub(&now, &sp->sfb_getqtime, &delta);
1285 net_timernsec(&delta, &new);
1286 avg = sp->sfb_stats.dequeue_avg;
1287 if (avg > 0) {
1288 int decay = DEQUEUE_DECAY;
1289 /*
1290 * If the time since last dequeue is
1291 * significantly greater than the current
fe8ab488 1292 * average, weigh the average more against
316670eb
A
1293 * the old value.
1294 */
1295 if (DEQUEUE_SPIKE(new, avg))
1296 decay += 5;
1297 avg = (((avg << decay) - avg) + new) >> decay;
1298 } else {
1299 avg = new;
1300 }
1301 sp->sfb_stats.dequeue_avg = avg;
1302 }
1303 *(&sp->sfb_getqtime) = *(&now);
1304 }
1305
fe8ab488
A
1306 if (!purge && SFB_QUEUE_DELAYBASED(sp)) {
1307 u_int64_t dequeue_ns, queue_delay = 0;
1308 net_timernsec(&now, &dequeue_ns);
39037602
A
1309 if (dequeue_ns > pkt->pkt_timestamp)
1310 queue_delay = dequeue_ns - pkt->pkt_timestamp;
fe8ab488
A
1311
1312 if (sp->sfb_min_qdelay == 0 ||
1313 (queue_delay > 0 && queue_delay < sp->sfb_min_qdelay))
1314 sp->sfb_min_qdelay = queue_delay;
1315 if (net_timercmp(&now, &sp->sfb_update_time, >=)) {
1316 if (sp->sfb_min_qdelay > sp->sfb_target_qdelay) {
1317 if (!SFB_IS_DELAYHIGH(sp))
1318 SFB_SET_DELAY_HIGH(sp, q);
1319 } else {
1320 sp->sfb_flags &= ~(SFBF_DELAYHIGH);
1321 sp->sfb_fc_threshold = 0;
39037602 1322
fe8ab488
A
1323 }
1324 net_timeradd(&now, &sp->sfb_update_interval,
1325 &sp->sfb_update_time);
1326 sp->sfb_min_qdelay = 0;
1327 }
1328 }
39037602 1329 pkt->pkt_timestamp = 0;
fe8ab488 1330
316670eb
A
1331 /*
1332 * Clearpkts are the ones which were in the queue when the hash
1333 * function was perturbed. Since the perturbation value (fudge),
1334 * and thus bin information for these packets is not known, we do
1335 * not change accounting information while dequeuing these packets.
1336 * It is important not to set the hash interval too small due to
1337 * this reason. A rule of thumb is to set it to K*D, where D is
1338 * the time taken to drain queue.
1339 */
39236c6e
A
1340 if (pkt->pkt_sfb_flags & SFB_PKT_PBOX) {
1341 pkt->pkt_sfb_flags &= ~SFB_PKT_PBOX;
316670eb
A
1342 if (sp->sfb_clearpkts > 0)
1343 sp->sfb_clearpkts--;
1344 } else if (sp->sfb_clearpkts > 0) {
1345 sp->sfb_clearpkts--;
1346 } else {
fe8ab488
A
1347 sfb_dq_update_bins(sp, pkt, &now, qsize(q));
1348 }
1349
1350 /* See comments in <rdar://problem/14040693> */
1351 pkt->pkt_flags &= ~PKTF_PRIV_GUARDED;
1352
1353 /*
1354 * If the queue becomes empty before the update interval, reset
1355 * the flow control threshold
1356 */
1357 if (qsize(q) == 0) {
1358 sp->sfb_flags &= ~SFBF_DELAYHIGH;
1359 sp->sfb_min_qdelay = 0;
1360 sp->sfb_fc_threshold = 0;
1361 net_timerclear(&sp->sfb_update_time);
3e170ce0 1362 net_timerclear(&sp->sfb_getqtime);
316670eb
A
1363 }
1364
1365 return (m);
1366}
1367
1368struct mbuf *
1369sfb_getq(struct sfb *sp, class_queue_t *q)
1370{
1371 return (sfb_getq_flow(sp, q, 0, FALSE));
1372}
1373
1374void
1375sfb_purgeq(struct sfb *sp, class_queue_t *q, u_int32_t flow, u_int32_t *packets,
1376 u_int32_t *bytes)
1377{
1378 u_int32_t cnt = 0, len = 0;
1379 struct mbuf *m;
1380
1381 IFCQ_CONVERT_LOCK(&sp->sfb_ifp->if_snd);
1382
1383 while ((m = sfb_getq_flow(sp, q, flow, TRUE)) != NULL) {
1384 cnt++;
1385 len += m_pktlen(m);
1386 m_freem(m);
1387 }
1388
1389 if (packets != NULL)
1390 *packets = cnt;
1391 if (bytes != NULL)
1392 *bytes = len;
1393}
1394
1395void
1396sfb_updateq(struct sfb *sp, cqev_t ev)
1397{
1398 struct ifnet *ifp = sp->sfb_ifp;
1399
1400 VERIFY(ifp != NULL);
1401
1402 switch (ev) {
39236c6e 1403 case CLASSQ_EV_LINK_BANDWIDTH: {
316670eb
A
1404 u_int64_t eff_rate = ifnet_output_linkrate(ifp);
1405
1406 /* update parameters only if rate has changed */
1407 if (eff_rate == sp->sfb_eff_rate)
1408 break;
1409
1410 if (classq_verbose) {
1411 log(LOG_DEBUG, "%s: SFB qid=%d, adapting to new "
1412 "eff_rate=%llu bps\n", if_name(ifp), sp->sfb_qid,
1413 eff_rate);
1414 }
1415 sfb_calc_holdtime(sp, eff_rate);
1416 sfb_calc_pboxtime(sp, eff_rate);
39037602 1417 ifclassq_calc_target_qdelay(ifp, &sp->sfb_target_qdelay);
fe8ab488 1418 sfb_calc_update_interval(sp, eff_rate);
316670eb
A
1419 break;
1420 }
1421
1422 case CLASSQ_EV_LINK_UP:
1423 case CLASSQ_EV_LINK_DOWN:
1424 if (classq_verbose) {
1425 log(LOG_DEBUG, "%s: SFB qid=%d, resetting due to "
1426 "link %s\n", if_name(ifp), sp->sfb_qid,
1427 (ev == CLASSQ_EV_LINK_UP) ? "UP" : "DOWN");
1428 }
1429 sfb_resetq(sp, ev);
1430 break;
1431
39236c6e 1432 case CLASSQ_EV_LINK_LATENCY:
316670eb
A
1433 case CLASSQ_EV_LINK_MTU:
1434 default:
1435 break;
1436 }
1437}
1438
1439int
1440sfb_suspendq(struct sfb *sp, class_queue_t *q, boolean_t on)
1441{
1442#pragma unused(q)
1443 struct ifnet *ifp = sp->sfb_ifp;
1444
1445 VERIFY(ifp != NULL);
1446
1447 if ((on && (sp->sfb_flags & SFBF_SUSPENDED)) ||
1448 (!on && !(sp->sfb_flags & SFBF_SUSPENDED)))
1449 return (0);
1450
1451 if (!(sp->sfb_flags & SFBF_FLOWCTL)) {
1452 log(LOG_ERR, "%s: SFB qid=%d, unable to %s queue since "
1453 "flow-control is not enabled", if_name(ifp), sp->sfb_qid,
1454 (on ? "suspend" : "resume"));
1455 return (ENOTSUP);
1456 }
1457
1458 if (classq_verbose) {
1459 log(LOG_DEBUG, "%s: SFB qid=%d, setting state to %s",
1460 if_name(ifp), sp->sfb_qid, (on ? "SUSPENDED" : "RUNNING"));
1461 }
1462
1463 if (on) {
1464 sp->sfb_flags |= SFBF_SUSPENDED;
1465 } else {
1466 sp->sfb_flags &= ~SFBF_SUSPENDED;
1467 sfb_swap_bins(sp, qlen(q));
1468 }
1469
1470 return (0);
1471}