sch_hhf.c 21 KB

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