sch_hhf.c 21 KB

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