sch_hhf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /* net/sched/sch_hhf.c Heavy-Hitter Filter (HHF)
  2. *
  3. * Copyright (C) 2013 Terry Lam <vtlam@google.com>
  4. * Copyright (C) 2013 Nandita Dukkipati <nanditad@google.com>
  5. */
  6. #include <linux/jhash.h>
  7. #include <linux/jiffies.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/vmalloc.h>
  11. #include <net/pkt_sched.h>
  12. #include <net/sock.h>
  13. /* Heavy-Hitter Filter (HHF)
  14. *
  15. * Principles :
  16. * Flows are classified into two buckets: non-heavy-hitter and heavy-hitter
  17. * buckets. Initially, a new flow starts as non-heavy-hitter. Once classified
  18. * as heavy-hitter, it is immediately switched to the heavy-hitter bucket.
  19. * The buckets are dequeued by a Weighted Deficit Round Robin (WDRR) scheduler,
  20. * in which the heavy-hitter bucket is served with less weight.
  21. * In other words, non-heavy-hitters (e.g., short bursts of critical traffic)
  22. * are isolated from heavy-hitters (e.g., persistent bulk traffic) and also have
  23. * higher share of bandwidth.
  24. *
  25. * To capture heavy-hitters, we use the "multi-stage filter" algorithm in the
  26. * following paper:
  27. * [EV02] C. Estan and G. Varghese, "New Directions in Traffic Measurement and
  28. * Accounting", in ACM SIGCOMM, 2002.
  29. *
  30. * Conceptually, a multi-stage filter comprises k independent hash functions
  31. * and k counter arrays. Packets are indexed into k counter arrays by k hash
  32. * functions, respectively. The counters are then increased by the packet sizes.
  33. * Therefore,
  34. * - For a heavy-hitter flow: *all* of its k array counters must be large.
  35. * - For a non-heavy-hitter flow: some of its k array counters can be large
  36. * due to hash collision with other small flows; however, with high
  37. * probability, not *all* k counters are large.
  38. *
  39. * By the design of the multi-stage filter algorithm, the false negative rate
  40. * (heavy-hitters getting away uncaptured) is zero. However, the algorithm is
  41. * susceptible to false positives (non-heavy-hitters mistakenly classified as
  42. * heavy-hitters).
  43. * Therefore, we also implement the following optimizations to reduce false
  44. * positives by avoiding unnecessary increment of the counter values:
  45. * - Optimization O1: once a heavy-hitter is identified, its bytes are not
  46. * accounted in the array counters. This technique is called "shielding"
  47. * in Section 3.3.1 of [EV02].
  48. * - Optimization O2: conservative update of counters
  49. * (Section 3.3.2 of [EV02]),
  50. * New counter value = max {old counter value,
  51. * smallest counter value + packet bytes}
  52. *
  53. * Finally, we refresh the counters periodically since otherwise the counter
  54. * values will keep accumulating.
  55. *
  56. * Once a flow is classified as heavy-hitter, we also save its per-flow state
  57. * in an exact-matching flow table so that its subsequent packets can be
  58. * dispatched to the heavy-hitter bucket accordingly.
  59. *
  60. *
  61. * At a high level, this qdisc works as follows:
  62. * Given a packet p:
  63. * - If the flow-id of p (e.g., TCP 5-tuple) is already in the exact-matching
  64. * heavy-hitter flow table, denoted table T, then send p to the heavy-hitter
  65. * bucket.
  66. * - Otherwise, forward p to the multi-stage filter, denoted filter F
  67. * + If F decides that p belongs to a non-heavy-hitter flow, then send p
  68. * to the non-heavy-hitter bucket.
  69. * + Otherwise, if F decides that p belongs to a new heavy-hitter flow,
  70. * then set up a new flow entry for the flow-id of p in the table T and
  71. * send p to the heavy-hitter bucket.
  72. *
  73. * In this implementation:
  74. * - T is a fixed-size hash-table with 1024 entries. Hash collision is
  75. * resolved by linked-list chaining.
  76. * - F has four counter arrays, each array containing 1024 32-bit counters.
  77. * That means 4 * 1024 * 32 bits = 16KB of memory.
  78. * - Since each array in F contains 1024 counters, 10 bits are sufficient to
  79. * index into each array.
  80. * Hence, instead of having four hash functions, we chop the 32-bit
  81. * skb-hash into three 10-bit chunks, and the remaining 10-bit chunk is
  82. * computed as XOR sum of those three chunks.
  83. * - We need to clear the counter arrays periodically; however, directly
  84. * memsetting 16KB of memory can lead to cache eviction and unwanted delay.
  85. * So by representing each counter by a valid bit, we only need to reset
  86. * 4K of 1 bit (i.e. 512 bytes) instead of 16KB of memory.
  87. * - The Deficit Round Robin engine is taken from fq_codel implementation
  88. * (net/sched/sch_fq_codel.c). Note that wdrr_bucket corresponds to
  89. * fq_codel_flow in fq_codel implementation.
  90. *
  91. */
  92. /* Non-configurable parameters */
  93. #define HH_FLOWS_CNT 1024 /* number of entries in exact-matching table T */
  94. #define HHF_ARRAYS_CNT 4 /* number of arrays in multi-stage filter F */
  95. #define HHF_ARRAYS_LEN 1024 /* number of counters in each array of F */
  96. #define HHF_BIT_MASK_LEN 10 /* masking 10 bits */
  97. #define HHF_BIT_MASK 0x3FF /* bitmask of 10 bits */
  98. #define WDRR_BUCKET_CNT 2 /* two buckets for Weighted DRR */
  99. enum wdrr_bucket_idx {
  100. WDRR_BUCKET_FOR_HH = 0, /* bucket id for heavy-hitters */
  101. WDRR_BUCKET_FOR_NON_HH = 1 /* bucket id for non-heavy-hitters */
  102. };
  103. #define hhf_time_before(a, b) \
  104. (typecheck(u32, a) && typecheck(u32, b) && ((s32)((a) - (b)) < 0))
  105. /* Heavy-hitter per-flow state */
  106. struct hh_flow_state {
  107. u32 hash_id; /* hash of flow-id (e.g. TCP 5-tuple) */
  108. u32 hit_timestamp; /* last time heavy-hitter was seen */
  109. struct list_head flowchain; /* chaining under hash collision */
  110. };
  111. /* Weighted Deficit Round Robin (WDRR) scheduler */
  112. struct wdrr_bucket {
  113. struct sk_buff *head;
  114. struct sk_buff *tail;
  115. struct list_head bucketchain;
  116. int deficit;
  117. };
  118. struct hhf_sched_data {
  119. struct wdrr_bucket buckets[WDRR_BUCKET_CNT];
  120. u32 perturbation; /* hash perturbation */
  121. u32 quantum; /* psched_mtu(qdisc_dev(sch)); */
  122. u32 drop_overlimit; /* number of times max qdisc packet
  123. * limit was hit
  124. */
  125. struct list_head *hh_flows; /* table T (currently active HHs) */
  126. u32 hh_flows_limit; /* max active HH allocs */
  127. u32 hh_flows_overlimit; /* num of disallowed HH allocs */
  128. u32 hh_flows_total_cnt; /* total admitted HHs */
  129. u32 hh_flows_current_cnt; /* total current HHs */
  130. u32 *hhf_arrays[HHF_ARRAYS_CNT]; /* HH filter F */
  131. u32 hhf_arrays_reset_timestamp; /* last time hhf_arrays
  132. * was reset
  133. */
  134. unsigned long *hhf_valid_bits[HHF_ARRAYS_CNT]; /* shadow valid bits
  135. * of hhf_arrays
  136. */
  137. /* Similar to the "new_flows" vs. "old_flows" concept in fq_codel DRR */
  138. struct list_head new_buckets; /* list of new buckets */
  139. struct list_head old_buckets; /* list of old buckets */
  140. /* Configurable HHF parameters */
  141. u32 hhf_reset_timeout; /* interval to reset counter
  142. * arrays in filter F
  143. * (default 40ms)
  144. */
  145. u32 hhf_admit_bytes; /* counter thresh to classify as
  146. * HH (default 128KB).
  147. * With these default values,
  148. * 128KB / 40ms = 25 Mbps
  149. * i.e., we expect to capture HHs
  150. * sending > 25 Mbps.
  151. */
  152. u32 hhf_evict_timeout; /* aging threshold to evict idle
  153. * HHs out of table T. This should
  154. * be large enough to avoid
  155. * reordering during HH eviction.
  156. * (default 1s)
  157. */
  158. u32 hhf_non_hh_weight; /* WDRR weight for non-HHs
  159. * (default 2,
  160. * i.e., non-HH : HH = 2 : 1)
  161. */
  162. };
  163. static u32 hhf_time_stamp(void)
  164. {
  165. return jiffies;
  166. }
  167. /* Looks up a heavy-hitter flow in a chaining list of table T. */
  168. static struct hh_flow_state *seek_list(const u32 hash,
  169. struct list_head *head,
  170. struct hhf_sched_data *q)
  171. {
  172. struct hh_flow_state *flow, *next;
  173. u32 now = hhf_time_stamp();
  174. if (list_empty(head))
  175. return NULL;
  176. list_for_each_entry_safe(flow, next, head, flowchain) {
  177. u32 prev = flow->hit_timestamp + q->hhf_evict_timeout;
  178. if (hhf_time_before(prev, now)) {
  179. /* Delete expired heavy-hitters, but preserve one entry
  180. * to avoid kzalloc() when next time this slot is hit.
  181. */
  182. if (list_is_last(&flow->flowchain, head))
  183. return NULL;
  184. list_del(&flow->flowchain);
  185. kfree(flow);
  186. q->hh_flows_current_cnt--;
  187. } else if (flow->hash_id == hash) {
  188. return flow;
  189. }
  190. }
  191. return NULL;
  192. }
  193. /* Returns a flow state entry for a new heavy-hitter. Either reuses an expired
  194. * entry or dynamically alloc a new entry.
  195. */
  196. static struct hh_flow_state *alloc_new_hh(struct list_head *head,
  197. struct hhf_sched_data *q)
  198. {
  199. struct hh_flow_state *flow;
  200. u32 now = hhf_time_stamp();
  201. if (!list_empty(head)) {
  202. /* Find an expired heavy-hitter flow entry. */
  203. list_for_each_entry(flow, head, flowchain) {
  204. u32 prev = flow->hit_timestamp + q->hhf_evict_timeout;
  205. if (hhf_time_before(prev, now))
  206. return flow;
  207. }
  208. }
  209. if (q->hh_flows_current_cnt >= q->hh_flows_limit) {
  210. q->hh_flows_overlimit++;
  211. return NULL;
  212. }
  213. /* Create new entry. */
  214. flow = kzalloc(sizeof(struct hh_flow_state), GFP_ATOMIC);
  215. if (!flow)
  216. return NULL;
  217. q->hh_flows_current_cnt++;
  218. INIT_LIST_HEAD(&flow->flowchain);
  219. list_add_tail(&flow->flowchain, head);
  220. return flow;
  221. }
  222. /* Assigns packets to WDRR buckets. Implements a multi-stage filter to
  223. * classify heavy-hitters.
  224. */
  225. static enum wdrr_bucket_idx hhf_classify(struct sk_buff *skb, struct Qdisc *sch)
  226. {
  227. struct hhf_sched_data *q = qdisc_priv(sch);
  228. u32 tmp_hash, hash;
  229. u32 xorsum, filter_pos[HHF_ARRAYS_CNT], flow_pos;
  230. struct hh_flow_state *flow;
  231. u32 pkt_len, min_hhf_val;
  232. int i;
  233. u32 prev;
  234. u32 now = hhf_time_stamp();
  235. /* Reset the HHF counter arrays if this is the right time. */
  236. prev = q->hhf_arrays_reset_timestamp + q->hhf_reset_timeout;
  237. if (hhf_time_before(prev, now)) {
  238. for (i = 0; i < HHF_ARRAYS_CNT; i++)
  239. bitmap_zero(q->hhf_valid_bits[i], HHF_ARRAYS_LEN);
  240. q->hhf_arrays_reset_timestamp = now;
  241. }
  242. /* Get hashed flow-id of the skb. */
  243. hash = skb_get_hash_perturb(skb, q->perturbation);
  244. /* Check if this packet belongs to an already established HH flow. */
  245. flow_pos = hash & HHF_BIT_MASK;
  246. flow = seek_list(hash, &q->hh_flows[flow_pos], q);
  247. if (flow) { /* found its HH flow */
  248. flow->hit_timestamp = now;
  249. return WDRR_BUCKET_FOR_HH;
  250. }
  251. /* Now pass the packet through the multi-stage filter. */
  252. tmp_hash = hash;
  253. xorsum = 0;
  254. for (i = 0; i < HHF_ARRAYS_CNT - 1; i++) {
  255. /* Split the skb_hash into three 10-bit chunks. */
  256. filter_pos[i] = tmp_hash & HHF_BIT_MASK;
  257. xorsum ^= filter_pos[i];
  258. tmp_hash >>= HHF_BIT_MASK_LEN;
  259. }
  260. /* The last chunk is computed as XOR sum of other chunks. */
  261. filter_pos[HHF_ARRAYS_CNT - 1] = xorsum ^ tmp_hash;
  262. pkt_len = qdisc_pkt_len(skb);
  263. min_hhf_val = ~0U;
  264. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  265. u32 val;
  266. if (!test_bit(filter_pos[i], q->hhf_valid_bits[i])) {
  267. q->hhf_arrays[i][filter_pos[i]] = 0;
  268. __set_bit(filter_pos[i], q->hhf_valid_bits[i]);
  269. }
  270. val = q->hhf_arrays[i][filter_pos[i]] + pkt_len;
  271. if (min_hhf_val > val)
  272. min_hhf_val = val;
  273. }
  274. /* Found a new HH iff all counter values > HH admit threshold. */
  275. if (min_hhf_val > q->hhf_admit_bytes) {
  276. /* Just captured a new heavy-hitter. */
  277. flow = alloc_new_hh(&q->hh_flows[flow_pos], q);
  278. if (!flow) /* memory alloc problem */
  279. return WDRR_BUCKET_FOR_NON_HH;
  280. flow->hash_id = hash;
  281. flow->hit_timestamp = now;
  282. q->hh_flows_total_cnt++;
  283. /* By returning without updating counters in q->hhf_arrays,
  284. * we implicitly implement "shielding" (see Optimization O1).
  285. */
  286. return WDRR_BUCKET_FOR_HH;
  287. }
  288. /* Conservative update of HHF arrays (see Optimization O2). */
  289. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  290. if (q->hhf_arrays[i][filter_pos[i]] < min_hhf_val)
  291. q->hhf_arrays[i][filter_pos[i]] = min_hhf_val;
  292. }
  293. return WDRR_BUCKET_FOR_NON_HH;
  294. }
  295. /* Removes one skb from head of bucket. */
  296. static struct sk_buff *dequeue_head(struct wdrr_bucket *bucket)
  297. {
  298. struct sk_buff *skb = bucket->head;
  299. bucket->head = skb->next;
  300. skb->next = NULL;
  301. return skb;
  302. }
  303. /* Tail-adds skb to bucket. */
  304. static void bucket_add(struct wdrr_bucket *bucket, struct sk_buff *skb)
  305. {
  306. if (bucket->head == NULL)
  307. bucket->head = skb;
  308. else
  309. bucket->tail->next = skb;
  310. bucket->tail = skb;
  311. skb->next = NULL;
  312. }
  313. static unsigned int hhf_drop(struct Qdisc *sch)
  314. {
  315. struct hhf_sched_data *q = qdisc_priv(sch);
  316. struct wdrr_bucket *bucket;
  317. /* Always try to drop from heavy-hitters first. */
  318. bucket = &q->buckets[WDRR_BUCKET_FOR_HH];
  319. if (!bucket->head)
  320. bucket = &q->buckets[WDRR_BUCKET_FOR_NON_HH];
  321. if (bucket->head) {
  322. struct sk_buff *skb = dequeue_head(bucket);
  323. sch->q.qlen--;
  324. qdisc_qstats_drop(sch);
  325. qdisc_qstats_backlog_dec(sch, skb);
  326. kfree_skb(skb);
  327. }
  328. /* Return id of the bucket from which the packet was dropped. */
  329. return bucket - q->buckets;
  330. }
  331. static unsigned int hhf_qdisc_drop(struct Qdisc *sch)
  332. {
  333. unsigned int prev_backlog;
  334. prev_backlog = sch->qstats.backlog;
  335. hhf_drop(sch);
  336. return prev_backlog - sch->qstats.backlog;
  337. }
  338. static int hhf_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  339. {
  340. struct hhf_sched_data *q = qdisc_priv(sch);
  341. enum wdrr_bucket_idx idx;
  342. struct wdrr_bucket *bucket;
  343. unsigned int prev_backlog;
  344. idx = hhf_classify(skb, sch);
  345. bucket = &q->buckets[idx];
  346. bucket_add(bucket, skb);
  347. qdisc_qstats_backlog_inc(sch, skb);
  348. if (list_empty(&bucket->bucketchain)) {
  349. unsigned int weight;
  350. /* The logic of new_buckets vs. old_buckets is the same as
  351. * new_flows vs. old_flows in the implementation of fq_codel,
  352. * i.e., short bursts of non-HHs should have strict priority.
  353. */
  354. if (idx == WDRR_BUCKET_FOR_HH) {
  355. /* Always move heavy-hitters to old bucket. */
  356. weight = 1;
  357. list_add_tail(&bucket->bucketchain, &q->old_buckets);
  358. } else {
  359. weight = q->hhf_non_hh_weight;
  360. list_add_tail(&bucket->bucketchain, &q->new_buckets);
  361. }
  362. bucket->deficit = weight * q->quantum;
  363. }
  364. if (++sch->q.qlen <= sch->limit)
  365. return NET_XMIT_SUCCESS;
  366. prev_backlog = sch->qstats.backlog;
  367. q->drop_overlimit++;
  368. /* Return Congestion Notification only if we dropped a packet from this
  369. * bucket.
  370. */
  371. if (hhf_drop(sch) == idx)
  372. return NET_XMIT_CN;
  373. /* As we dropped a packet, better let upper stack know this. */
  374. qdisc_tree_reduce_backlog(sch, 1, prev_backlog - sch->qstats.backlog);
  375. return NET_XMIT_SUCCESS;
  376. }
  377. static struct sk_buff *hhf_dequeue(struct Qdisc *sch)
  378. {
  379. struct hhf_sched_data *q = qdisc_priv(sch);
  380. struct sk_buff *skb = NULL;
  381. struct wdrr_bucket *bucket;
  382. struct list_head *head;
  383. begin:
  384. head = &q->new_buckets;
  385. if (list_empty(head)) {
  386. head = &q->old_buckets;
  387. if (list_empty(head))
  388. return NULL;
  389. }
  390. bucket = list_first_entry(head, struct wdrr_bucket, bucketchain);
  391. if (bucket->deficit <= 0) {
  392. int weight = (bucket - q->buckets == WDRR_BUCKET_FOR_HH) ?
  393. 1 : q->hhf_non_hh_weight;
  394. bucket->deficit += weight * q->quantum;
  395. list_move_tail(&bucket->bucketchain, &q->old_buckets);
  396. goto begin;
  397. }
  398. if (bucket->head) {
  399. skb = dequeue_head(bucket);
  400. sch->q.qlen--;
  401. qdisc_qstats_backlog_dec(sch, skb);
  402. }
  403. if (!skb) {
  404. /* Force a pass through old_buckets to prevent starvation. */
  405. if ((head == &q->new_buckets) && !list_empty(&q->old_buckets))
  406. list_move_tail(&bucket->bucketchain, &q->old_buckets);
  407. else
  408. list_del_init(&bucket->bucketchain);
  409. goto begin;
  410. }
  411. qdisc_bstats_update(sch, skb);
  412. bucket->deficit -= qdisc_pkt_len(skb);
  413. return skb;
  414. }
  415. static void hhf_reset(struct Qdisc *sch)
  416. {
  417. struct sk_buff *skb;
  418. while ((skb = hhf_dequeue(sch)) != NULL)
  419. kfree_skb(skb);
  420. }
  421. static void *hhf_zalloc(size_t sz)
  422. {
  423. void *ptr = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN);
  424. if (!ptr)
  425. ptr = vzalloc(sz);
  426. return ptr;
  427. }
  428. static void hhf_free(void *addr)
  429. {
  430. kvfree(addr);
  431. }
  432. static void hhf_destroy(struct Qdisc *sch)
  433. {
  434. int i;
  435. struct hhf_sched_data *q = qdisc_priv(sch);
  436. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  437. hhf_free(q->hhf_arrays[i]);
  438. hhf_free(q->hhf_valid_bits[i]);
  439. }
  440. if (!q->hh_flows)
  441. return;
  442. for (i = 0; i < HH_FLOWS_CNT; i++) {
  443. struct hh_flow_state *flow, *next;
  444. struct list_head *head = &q->hh_flows[i];
  445. if (list_empty(head))
  446. continue;
  447. list_for_each_entry_safe(flow, next, head, flowchain) {
  448. list_del(&flow->flowchain);
  449. kfree(flow);
  450. }
  451. }
  452. hhf_free(q->hh_flows);
  453. }
  454. static const struct nla_policy hhf_policy[TCA_HHF_MAX + 1] = {
  455. [TCA_HHF_BACKLOG_LIMIT] = { .type = NLA_U32 },
  456. [TCA_HHF_QUANTUM] = { .type = NLA_U32 },
  457. [TCA_HHF_HH_FLOWS_LIMIT] = { .type = NLA_U32 },
  458. [TCA_HHF_RESET_TIMEOUT] = { .type = NLA_U32 },
  459. [TCA_HHF_ADMIT_BYTES] = { .type = NLA_U32 },
  460. [TCA_HHF_EVICT_TIMEOUT] = { .type = NLA_U32 },
  461. [TCA_HHF_NON_HH_WEIGHT] = { .type = NLA_U32 },
  462. };
  463. static int hhf_change(struct Qdisc *sch, struct nlattr *opt)
  464. {
  465. struct hhf_sched_data *q = qdisc_priv(sch);
  466. struct nlattr *tb[TCA_HHF_MAX + 1];
  467. unsigned int qlen, prev_backlog;
  468. int err;
  469. u64 non_hh_quantum;
  470. u32 new_quantum = q->quantum;
  471. u32 new_hhf_non_hh_weight = q->hhf_non_hh_weight;
  472. if (!opt)
  473. return -EINVAL;
  474. err = nla_parse_nested(tb, TCA_HHF_MAX, opt, hhf_policy);
  475. if (err < 0)
  476. return err;
  477. if (tb[TCA_HHF_QUANTUM])
  478. new_quantum = nla_get_u32(tb[TCA_HHF_QUANTUM]);
  479. if (tb[TCA_HHF_NON_HH_WEIGHT])
  480. new_hhf_non_hh_weight = nla_get_u32(tb[TCA_HHF_NON_HH_WEIGHT]);
  481. non_hh_quantum = (u64)new_quantum * new_hhf_non_hh_weight;
  482. if (non_hh_quantum > INT_MAX)
  483. return -EINVAL;
  484. sch_tree_lock(sch);
  485. if (tb[TCA_HHF_BACKLOG_LIMIT])
  486. sch->limit = nla_get_u32(tb[TCA_HHF_BACKLOG_LIMIT]);
  487. q->quantum = new_quantum;
  488. q->hhf_non_hh_weight = new_hhf_non_hh_weight;
  489. if (tb[TCA_HHF_HH_FLOWS_LIMIT])
  490. q->hh_flows_limit = nla_get_u32(tb[TCA_HHF_HH_FLOWS_LIMIT]);
  491. if (tb[TCA_HHF_RESET_TIMEOUT]) {
  492. u32 us = nla_get_u32(tb[TCA_HHF_RESET_TIMEOUT]);
  493. q->hhf_reset_timeout = usecs_to_jiffies(us);
  494. }
  495. if (tb[TCA_HHF_ADMIT_BYTES])
  496. q->hhf_admit_bytes = nla_get_u32(tb[TCA_HHF_ADMIT_BYTES]);
  497. if (tb[TCA_HHF_EVICT_TIMEOUT]) {
  498. u32 us = nla_get_u32(tb[TCA_HHF_EVICT_TIMEOUT]);
  499. q->hhf_evict_timeout = usecs_to_jiffies(us);
  500. }
  501. qlen = sch->q.qlen;
  502. prev_backlog = sch->qstats.backlog;
  503. while (sch->q.qlen > sch->limit) {
  504. struct sk_buff *skb = hhf_dequeue(sch);
  505. kfree_skb(skb);
  506. }
  507. qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen,
  508. prev_backlog - sch->qstats.backlog);
  509. sch_tree_unlock(sch);
  510. return 0;
  511. }
  512. static int hhf_init(struct Qdisc *sch, struct nlattr *opt)
  513. {
  514. struct hhf_sched_data *q = qdisc_priv(sch);
  515. int i;
  516. sch->limit = 1000;
  517. q->quantum = psched_mtu(qdisc_dev(sch));
  518. q->perturbation = prandom_u32();
  519. INIT_LIST_HEAD(&q->new_buckets);
  520. INIT_LIST_HEAD(&q->old_buckets);
  521. /* Configurable HHF parameters */
  522. q->hhf_reset_timeout = HZ / 25; /* 40 ms */
  523. q->hhf_admit_bytes = 131072; /* 128 KB */
  524. q->hhf_evict_timeout = HZ; /* 1 sec */
  525. q->hhf_non_hh_weight = 2;
  526. if (opt) {
  527. int err = hhf_change(sch, opt);
  528. if (err)
  529. return err;
  530. }
  531. if (!q->hh_flows) {
  532. /* Initialize heavy-hitter flow table. */
  533. q->hh_flows = hhf_zalloc(HH_FLOWS_CNT *
  534. sizeof(struct list_head));
  535. if (!q->hh_flows)
  536. return -ENOMEM;
  537. for (i = 0; i < HH_FLOWS_CNT; i++)
  538. INIT_LIST_HEAD(&q->hh_flows[i]);
  539. /* Cap max active HHs at twice len of hh_flows table. */
  540. q->hh_flows_limit = 2 * HH_FLOWS_CNT;
  541. q->hh_flows_overlimit = 0;
  542. q->hh_flows_total_cnt = 0;
  543. q->hh_flows_current_cnt = 0;
  544. /* Initialize heavy-hitter filter arrays. */
  545. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  546. q->hhf_arrays[i] = hhf_zalloc(HHF_ARRAYS_LEN *
  547. sizeof(u32));
  548. if (!q->hhf_arrays[i]) {
  549. /* Note: hhf_destroy() will be called
  550. * by our caller.
  551. */
  552. return -ENOMEM;
  553. }
  554. }
  555. q->hhf_arrays_reset_timestamp = hhf_time_stamp();
  556. /* Initialize valid bits of heavy-hitter filter arrays. */
  557. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  558. q->hhf_valid_bits[i] = hhf_zalloc(HHF_ARRAYS_LEN /
  559. BITS_PER_BYTE);
  560. if (!q->hhf_valid_bits[i]) {
  561. /* Note: hhf_destroy() will be called
  562. * by our caller.
  563. */
  564. return -ENOMEM;
  565. }
  566. }
  567. /* Initialize Weighted DRR buckets. */
  568. for (i = 0; i < WDRR_BUCKET_CNT; i++) {
  569. struct wdrr_bucket *bucket = q->buckets + i;
  570. INIT_LIST_HEAD(&bucket->bucketchain);
  571. }
  572. }
  573. return 0;
  574. }
  575. static int hhf_dump(struct Qdisc *sch, struct sk_buff *skb)
  576. {
  577. struct hhf_sched_data *q = qdisc_priv(sch);
  578. struct nlattr *opts;
  579. opts = nla_nest_start(skb, TCA_OPTIONS);
  580. if (opts == NULL)
  581. goto nla_put_failure;
  582. if (nla_put_u32(skb, TCA_HHF_BACKLOG_LIMIT, sch->limit) ||
  583. nla_put_u32(skb, TCA_HHF_QUANTUM, q->quantum) ||
  584. nla_put_u32(skb, TCA_HHF_HH_FLOWS_LIMIT, q->hh_flows_limit) ||
  585. nla_put_u32(skb, TCA_HHF_RESET_TIMEOUT,
  586. jiffies_to_usecs(q->hhf_reset_timeout)) ||
  587. nla_put_u32(skb, TCA_HHF_ADMIT_BYTES, q->hhf_admit_bytes) ||
  588. nla_put_u32(skb, TCA_HHF_EVICT_TIMEOUT,
  589. jiffies_to_usecs(q->hhf_evict_timeout)) ||
  590. nla_put_u32(skb, TCA_HHF_NON_HH_WEIGHT, q->hhf_non_hh_weight))
  591. goto nla_put_failure;
  592. return nla_nest_end(skb, opts);
  593. nla_put_failure:
  594. return -1;
  595. }
  596. static int hhf_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  597. {
  598. struct hhf_sched_data *q = qdisc_priv(sch);
  599. struct tc_hhf_xstats st = {
  600. .drop_overlimit = q->drop_overlimit,
  601. .hh_overlimit = q->hh_flows_overlimit,
  602. .hh_tot_count = q->hh_flows_total_cnt,
  603. .hh_cur_count = q->hh_flows_current_cnt,
  604. };
  605. return gnet_stats_copy_app(d, &st, sizeof(st));
  606. }
  607. static struct Qdisc_ops hhf_qdisc_ops __read_mostly = {
  608. .id = "hhf",
  609. .priv_size = sizeof(struct hhf_sched_data),
  610. .enqueue = hhf_enqueue,
  611. .dequeue = hhf_dequeue,
  612. .peek = qdisc_peek_dequeued,
  613. .drop = hhf_qdisc_drop,
  614. .init = hhf_init,
  615. .reset = hhf_reset,
  616. .destroy = hhf_destroy,
  617. .change = hhf_change,
  618. .dump = hhf_dump,
  619. .dump_stats = hhf_dump_stats,
  620. .owner = THIS_MODULE,
  621. };
  622. static int __init hhf_module_init(void)
  623. {
  624. return register_qdisc(&hhf_qdisc_ops);
  625. }
  626. static void __exit hhf_module_exit(void)
  627. {
  628. unregister_qdisc(&hhf_qdisc_ops);
  629. }
  630. module_init(hhf_module_init)
  631. module_exit(hhf_module_exit)
  632. MODULE_AUTHOR("Terry Lam");
  633. MODULE_AUTHOR("Nandita Dukkipati");
  634. MODULE_LICENSE("GPL");