sch_hhf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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/jiffies.h>
  7. #include <linux/module.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/siphash.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. siphash_key_t 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, struct sk_buff **to_free)
  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_backlog_dec(sch, skb);
  325. qdisc_drop(skb, sch, to_free);
  326. }
  327. /* Return id of the bucket from which the packet was dropped. */
  328. return bucket - q->buckets;
  329. }
  330. static int hhf_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  331. struct sk_buff **to_free)
  332. {
  333. struct hhf_sched_data *q = qdisc_priv(sch);
  334. enum wdrr_bucket_idx idx;
  335. struct wdrr_bucket *bucket;
  336. unsigned int prev_backlog;
  337. idx = hhf_classify(skb, sch);
  338. bucket = &q->buckets[idx];
  339. bucket_add(bucket, skb);
  340. qdisc_qstats_backlog_inc(sch, skb);
  341. if (list_empty(&bucket->bucketchain)) {
  342. unsigned int weight;
  343. /* The logic of new_buckets vs. old_buckets is the same as
  344. * new_flows vs. old_flows in the implementation of fq_codel,
  345. * i.e., short bursts of non-HHs should have strict priority.
  346. */
  347. if (idx == WDRR_BUCKET_FOR_HH) {
  348. /* Always move heavy-hitters to old bucket. */
  349. weight = 1;
  350. list_add_tail(&bucket->bucketchain, &q->old_buckets);
  351. } else {
  352. weight = q->hhf_non_hh_weight;
  353. list_add_tail(&bucket->bucketchain, &q->new_buckets);
  354. }
  355. bucket->deficit = weight * q->quantum;
  356. }
  357. if (++sch->q.qlen <= sch->limit)
  358. return NET_XMIT_SUCCESS;
  359. prev_backlog = sch->qstats.backlog;
  360. q->drop_overlimit++;
  361. /* Return Congestion Notification only if we dropped a packet from this
  362. * bucket.
  363. */
  364. if (hhf_drop(sch, to_free) == idx)
  365. return NET_XMIT_CN;
  366. /* As we dropped a packet, better let upper stack know this. */
  367. qdisc_tree_reduce_backlog(sch, 1, prev_backlog - sch->qstats.backlog);
  368. return NET_XMIT_SUCCESS;
  369. }
  370. static struct sk_buff *hhf_dequeue(struct Qdisc *sch)
  371. {
  372. struct hhf_sched_data *q = qdisc_priv(sch);
  373. struct sk_buff *skb = NULL;
  374. struct wdrr_bucket *bucket;
  375. struct list_head *head;
  376. begin:
  377. head = &q->new_buckets;
  378. if (list_empty(head)) {
  379. head = &q->old_buckets;
  380. if (list_empty(head))
  381. return NULL;
  382. }
  383. bucket = list_first_entry(head, struct wdrr_bucket, bucketchain);
  384. if (bucket->deficit <= 0) {
  385. int weight = (bucket - q->buckets == WDRR_BUCKET_FOR_HH) ?
  386. 1 : q->hhf_non_hh_weight;
  387. bucket->deficit += weight * q->quantum;
  388. list_move_tail(&bucket->bucketchain, &q->old_buckets);
  389. goto begin;
  390. }
  391. if (bucket->head) {
  392. skb = dequeue_head(bucket);
  393. sch->q.qlen--;
  394. qdisc_qstats_backlog_dec(sch, skb);
  395. }
  396. if (!skb) {
  397. /* Force a pass through old_buckets to prevent starvation. */
  398. if ((head == &q->new_buckets) && !list_empty(&q->old_buckets))
  399. list_move_tail(&bucket->bucketchain, &q->old_buckets);
  400. else
  401. list_del_init(&bucket->bucketchain);
  402. goto begin;
  403. }
  404. qdisc_bstats_update(sch, skb);
  405. bucket->deficit -= qdisc_pkt_len(skb);
  406. return skb;
  407. }
  408. static void hhf_reset(struct Qdisc *sch)
  409. {
  410. struct sk_buff *skb;
  411. while ((skb = hhf_dequeue(sch)) != NULL)
  412. rtnl_kfree_skbs(skb, skb);
  413. }
  414. static void hhf_destroy(struct Qdisc *sch)
  415. {
  416. int i;
  417. struct hhf_sched_data *q = qdisc_priv(sch);
  418. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  419. kvfree(q->hhf_arrays[i]);
  420. kvfree(q->hhf_valid_bits[i]);
  421. }
  422. if (!q->hh_flows)
  423. return;
  424. for (i = 0; i < HH_FLOWS_CNT; i++) {
  425. struct hh_flow_state *flow, *next;
  426. struct list_head *head = &q->hh_flows[i];
  427. if (list_empty(head))
  428. continue;
  429. list_for_each_entry_safe(flow, next, head, flowchain) {
  430. list_del(&flow->flowchain);
  431. kfree(flow);
  432. }
  433. }
  434. kvfree(q->hh_flows);
  435. }
  436. static const struct nla_policy hhf_policy[TCA_HHF_MAX + 1] = {
  437. [TCA_HHF_BACKLOG_LIMIT] = { .type = NLA_U32 },
  438. [TCA_HHF_QUANTUM] = { .type = NLA_U32 },
  439. [TCA_HHF_HH_FLOWS_LIMIT] = { .type = NLA_U32 },
  440. [TCA_HHF_RESET_TIMEOUT] = { .type = NLA_U32 },
  441. [TCA_HHF_ADMIT_BYTES] = { .type = NLA_U32 },
  442. [TCA_HHF_EVICT_TIMEOUT] = { .type = NLA_U32 },
  443. [TCA_HHF_NON_HH_WEIGHT] = { .type = NLA_U32 },
  444. };
  445. static int hhf_change(struct Qdisc *sch, struct nlattr *opt)
  446. {
  447. struct hhf_sched_data *q = qdisc_priv(sch);
  448. struct nlattr *tb[TCA_HHF_MAX + 1];
  449. unsigned int qlen, prev_backlog;
  450. int err;
  451. u64 non_hh_quantum;
  452. u32 new_quantum = q->quantum;
  453. u32 new_hhf_non_hh_weight = q->hhf_non_hh_weight;
  454. if (!opt)
  455. return -EINVAL;
  456. err = nla_parse_nested(tb, TCA_HHF_MAX, opt, hhf_policy, NULL);
  457. if (err < 0)
  458. return err;
  459. if (tb[TCA_HHF_QUANTUM])
  460. new_quantum = nla_get_u32(tb[TCA_HHF_QUANTUM]);
  461. if (tb[TCA_HHF_NON_HH_WEIGHT])
  462. new_hhf_non_hh_weight = nla_get_u32(tb[TCA_HHF_NON_HH_WEIGHT]);
  463. non_hh_quantum = (u64)new_quantum * new_hhf_non_hh_weight;
  464. if (non_hh_quantum == 0 || non_hh_quantum > INT_MAX)
  465. return -EINVAL;
  466. sch_tree_lock(sch);
  467. if (tb[TCA_HHF_BACKLOG_LIMIT])
  468. sch->limit = nla_get_u32(tb[TCA_HHF_BACKLOG_LIMIT]);
  469. q->quantum = new_quantum;
  470. q->hhf_non_hh_weight = new_hhf_non_hh_weight;
  471. if (tb[TCA_HHF_HH_FLOWS_LIMIT])
  472. q->hh_flows_limit = nla_get_u32(tb[TCA_HHF_HH_FLOWS_LIMIT]);
  473. if (tb[TCA_HHF_RESET_TIMEOUT]) {
  474. u32 us = nla_get_u32(tb[TCA_HHF_RESET_TIMEOUT]);
  475. q->hhf_reset_timeout = usecs_to_jiffies(us);
  476. }
  477. if (tb[TCA_HHF_ADMIT_BYTES])
  478. q->hhf_admit_bytes = nla_get_u32(tb[TCA_HHF_ADMIT_BYTES]);
  479. if (tb[TCA_HHF_EVICT_TIMEOUT]) {
  480. u32 us = nla_get_u32(tb[TCA_HHF_EVICT_TIMEOUT]);
  481. q->hhf_evict_timeout = usecs_to_jiffies(us);
  482. }
  483. qlen = sch->q.qlen;
  484. prev_backlog = sch->qstats.backlog;
  485. while (sch->q.qlen > sch->limit) {
  486. struct sk_buff *skb = hhf_dequeue(sch);
  487. rtnl_kfree_skbs(skb, skb);
  488. }
  489. qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen,
  490. prev_backlog - sch->qstats.backlog);
  491. sch_tree_unlock(sch);
  492. return 0;
  493. }
  494. static int hhf_init(struct Qdisc *sch, struct nlattr *opt)
  495. {
  496. struct hhf_sched_data *q = qdisc_priv(sch);
  497. int i;
  498. sch->limit = 1000;
  499. q->quantum = psched_mtu(qdisc_dev(sch));
  500. get_random_bytes(&q->perturbation, sizeof(q->perturbation));
  501. INIT_LIST_HEAD(&q->new_buckets);
  502. INIT_LIST_HEAD(&q->old_buckets);
  503. /* Configurable HHF parameters */
  504. q->hhf_reset_timeout = HZ / 25; /* 40 ms */
  505. q->hhf_admit_bytes = 131072; /* 128 KB */
  506. q->hhf_evict_timeout = HZ; /* 1 sec */
  507. q->hhf_non_hh_weight = 2;
  508. if (opt) {
  509. int err = hhf_change(sch, opt);
  510. if (err)
  511. return err;
  512. }
  513. if (!q->hh_flows) {
  514. /* Initialize heavy-hitter flow table. */
  515. q->hh_flows = kvzalloc(HH_FLOWS_CNT *
  516. sizeof(struct list_head), GFP_KERNEL);
  517. if (!q->hh_flows)
  518. return -ENOMEM;
  519. for (i = 0; i < HH_FLOWS_CNT; i++)
  520. INIT_LIST_HEAD(&q->hh_flows[i]);
  521. /* Cap max active HHs at twice len of hh_flows table. */
  522. q->hh_flows_limit = 2 * HH_FLOWS_CNT;
  523. q->hh_flows_overlimit = 0;
  524. q->hh_flows_total_cnt = 0;
  525. q->hh_flows_current_cnt = 0;
  526. /* Initialize heavy-hitter filter arrays. */
  527. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  528. q->hhf_arrays[i] = kvzalloc(HHF_ARRAYS_LEN *
  529. sizeof(u32), GFP_KERNEL);
  530. if (!q->hhf_arrays[i]) {
  531. /* Note: hhf_destroy() will be called
  532. * by our caller.
  533. */
  534. return -ENOMEM;
  535. }
  536. }
  537. q->hhf_arrays_reset_timestamp = hhf_time_stamp();
  538. /* Initialize valid bits of heavy-hitter filter arrays. */
  539. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  540. q->hhf_valid_bits[i] = kvzalloc(HHF_ARRAYS_LEN /
  541. BITS_PER_BYTE, GFP_KERNEL);
  542. if (!q->hhf_valid_bits[i]) {
  543. /* Note: hhf_destroy() will be called
  544. * by our caller.
  545. */
  546. return -ENOMEM;
  547. }
  548. }
  549. /* Initialize Weighted DRR buckets. */
  550. for (i = 0; i < WDRR_BUCKET_CNT; i++) {
  551. struct wdrr_bucket *bucket = q->buckets + i;
  552. INIT_LIST_HEAD(&bucket->bucketchain);
  553. }
  554. }
  555. return 0;
  556. }
  557. static int hhf_dump(struct Qdisc *sch, struct sk_buff *skb)
  558. {
  559. struct hhf_sched_data *q = qdisc_priv(sch);
  560. struct nlattr *opts;
  561. opts = nla_nest_start(skb, TCA_OPTIONS);
  562. if (opts == NULL)
  563. goto nla_put_failure;
  564. if (nla_put_u32(skb, TCA_HHF_BACKLOG_LIMIT, sch->limit) ||
  565. nla_put_u32(skb, TCA_HHF_QUANTUM, q->quantum) ||
  566. nla_put_u32(skb, TCA_HHF_HH_FLOWS_LIMIT, q->hh_flows_limit) ||
  567. nla_put_u32(skb, TCA_HHF_RESET_TIMEOUT,
  568. jiffies_to_usecs(q->hhf_reset_timeout)) ||
  569. nla_put_u32(skb, TCA_HHF_ADMIT_BYTES, q->hhf_admit_bytes) ||
  570. nla_put_u32(skb, TCA_HHF_EVICT_TIMEOUT,
  571. jiffies_to_usecs(q->hhf_evict_timeout)) ||
  572. nla_put_u32(skb, TCA_HHF_NON_HH_WEIGHT, q->hhf_non_hh_weight))
  573. goto nla_put_failure;
  574. return nla_nest_end(skb, opts);
  575. nla_put_failure:
  576. return -1;
  577. }
  578. static int hhf_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  579. {
  580. struct hhf_sched_data *q = qdisc_priv(sch);
  581. struct tc_hhf_xstats st = {
  582. .drop_overlimit = q->drop_overlimit,
  583. .hh_overlimit = q->hh_flows_overlimit,
  584. .hh_tot_count = q->hh_flows_total_cnt,
  585. .hh_cur_count = q->hh_flows_current_cnt,
  586. };
  587. return gnet_stats_copy_app(d, &st, sizeof(st));
  588. }
  589. static struct Qdisc_ops hhf_qdisc_ops __read_mostly = {
  590. .id = "hhf",
  591. .priv_size = sizeof(struct hhf_sched_data),
  592. .enqueue = hhf_enqueue,
  593. .dequeue = hhf_dequeue,
  594. .peek = qdisc_peek_dequeued,
  595. .init = hhf_init,
  596. .reset = hhf_reset,
  597. .destroy = hhf_destroy,
  598. .change = hhf_change,
  599. .dump = hhf_dump,
  600. .dump_stats = hhf_dump_stats,
  601. .owner = THIS_MODULE,
  602. };
  603. static int __init hhf_module_init(void)
  604. {
  605. return register_qdisc(&hhf_qdisc_ops);
  606. }
  607. static void __exit hhf_module_exit(void)
  608. {
  609. unregister_qdisc(&hhf_qdisc_ops);
  610. }
  611. module_init(hhf_module_init)
  612. module_exit(hhf_module_exit)
  613. MODULE_AUTHOR("Terry Lam");
  614. MODULE_AUTHOR("Nandita Dukkipati");
  615. MODULE_LICENSE("GPL");