sch_hhf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* net/sched/sch_hhf.c Heavy-Hitter Filter (HHF)
  3. *
  4. * Copyright (C) 2013 Terry Lam <[email protected]>
  5. * Copyright (C) 2013 Nandita Dukkipati <[email protected]>
  6. */
  7. #include <linux/jiffies.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/siphash.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. siphash_key_t 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. /* Looks up a heavy-hitter flow in a chaining list of table T. */
  169. static struct hh_flow_state *seek_list(const u32 hash,
  170. struct list_head *head,
  171. struct hhf_sched_data *q)
  172. {
  173. struct hh_flow_state *flow, *next;
  174. u32 now = hhf_time_stamp();
  175. if (list_empty(head))
  176. return NULL;
  177. list_for_each_entry_safe(flow, next, head, flowchain) {
  178. u32 prev = flow->hit_timestamp + q->hhf_evict_timeout;
  179. if (hhf_time_before(prev, now)) {
  180. /* Delete expired heavy-hitters, but preserve one entry
  181. * to avoid kzalloc() when next time this slot is hit.
  182. */
  183. if (list_is_last(&flow->flowchain, head))
  184. return NULL;
  185. list_del(&flow->flowchain);
  186. kfree(flow);
  187. q->hh_flows_current_cnt--;
  188. } else if (flow->hash_id == hash) {
  189. return flow;
  190. }
  191. }
  192. return NULL;
  193. }
  194. /* Returns a flow state entry for a new heavy-hitter. Either reuses an expired
  195. * entry or dynamically alloc a new entry.
  196. */
  197. static struct hh_flow_state *alloc_new_hh(struct list_head *head,
  198. struct hhf_sched_data *q)
  199. {
  200. struct hh_flow_state *flow;
  201. u32 now = hhf_time_stamp();
  202. if (!list_empty(head)) {
  203. /* Find an expired heavy-hitter flow entry. */
  204. list_for_each_entry(flow, head, flowchain) {
  205. u32 prev = flow->hit_timestamp + q->hhf_evict_timeout;
  206. if (hhf_time_before(prev, now))
  207. return flow;
  208. }
  209. }
  210. if (q->hh_flows_current_cnt >= q->hh_flows_limit) {
  211. q->hh_flows_overlimit++;
  212. return NULL;
  213. }
  214. /* Create new entry. */
  215. flow = kzalloc(sizeof(struct hh_flow_state), GFP_ATOMIC);
  216. if (!flow)
  217. return NULL;
  218. q->hh_flows_current_cnt++;
  219. INIT_LIST_HEAD(&flow->flowchain);
  220. list_add_tail(&flow->flowchain, head);
  221. return flow;
  222. }
  223. /* Assigns packets to WDRR buckets. Implements a multi-stage filter to
  224. * classify heavy-hitters.
  225. */
  226. static enum wdrr_bucket_idx hhf_classify(struct sk_buff *skb, struct Qdisc *sch)
  227. {
  228. struct hhf_sched_data *q = qdisc_priv(sch);
  229. u32 tmp_hash, hash;
  230. u32 xorsum, filter_pos[HHF_ARRAYS_CNT], flow_pos;
  231. struct hh_flow_state *flow;
  232. u32 pkt_len, min_hhf_val;
  233. int i;
  234. u32 prev;
  235. u32 now = hhf_time_stamp();
  236. /* Reset the HHF counter arrays if this is the right time. */
  237. prev = q->hhf_arrays_reset_timestamp + q->hhf_reset_timeout;
  238. if (hhf_time_before(prev, now)) {
  239. for (i = 0; i < HHF_ARRAYS_CNT; i++)
  240. bitmap_zero(q->hhf_valid_bits[i], HHF_ARRAYS_LEN);
  241. q->hhf_arrays_reset_timestamp = now;
  242. }
  243. /* Get hashed flow-id of the skb. */
  244. hash = skb_get_hash_perturb(skb, &q->perturbation);
  245. /* Check if this packet belongs to an already established HH flow. */
  246. flow_pos = hash & HHF_BIT_MASK;
  247. flow = seek_list(hash, &q->hh_flows[flow_pos], q);
  248. if (flow) { /* found its HH flow */
  249. flow->hit_timestamp = now;
  250. return WDRR_BUCKET_FOR_HH;
  251. }
  252. /* Now pass the packet through the multi-stage filter. */
  253. tmp_hash = hash;
  254. xorsum = 0;
  255. for (i = 0; i < HHF_ARRAYS_CNT - 1; i++) {
  256. /* Split the skb_hash into three 10-bit chunks. */
  257. filter_pos[i] = tmp_hash & HHF_BIT_MASK;
  258. xorsum ^= filter_pos[i];
  259. tmp_hash >>= HHF_BIT_MASK_LEN;
  260. }
  261. /* The last chunk is computed as XOR sum of other chunks. */
  262. filter_pos[HHF_ARRAYS_CNT - 1] = xorsum ^ tmp_hash;
  263. pkt_len = qdisc_pkt_len(skb);
  264. min_hhf_val = ~0U;
  265. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  266. u32 val;
  267. if (!test_bit(filter_pos[i], q->hhf_valid_bits[i])) {
  268. q->hhf_arrays[i][filter_pos[i]] = 0;
  269. __set_bit(filter_pos[i], q->hhf_valid_bits[i]);
  270. }
  271. val = q->hhf_arrays[i][filter_pos[i]] + pkt_len;
  272. if (min_hhf_val > val)
  273. min_hhf_val = val;
  274. }
  275. /* Found a new HH iff all counter values > HH admit threshold. */
  276. if (min_hhf_val > q->hhf_admit_bytes) {
  277. /* Just captured a new heavy-hitter. */
  278. flow = alloc_new_hh(&q->hh_flows[flow_pos], q);
  279. if (!flow) /* memory alloc problem */
  280. return WDRR_BUCKET_FOR_NON_HH;
  281. flow->hash_id = hash;
  282. flow->hit_timestamp = now;
  283. q->hh_flows_total_cnt++;
  284. /* By returning without updating counters in q->hhf_arrays,
  285. * we implicitly implement "shielding" (see Optimization O1).
  286. */
  287. return WDRR_BUCKET_FOR_HH;
  288. }
  289. /* Conservative update of HHF arrays (see Optimization O2). */
  290. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  291. if (q->hhf_arrays[i][filter_pos[i]] < min_hhf_val)
  292. q->hhf_arrays[i][filter_pos[i]] = min_hhf_val;
  293. }
  294. return WDRR_BUCKET_FOR_NON_HH;
  295. }
  296. /* Removes one skb from head of bucket. */
  297. static struct sk_buff *dequeue_head(struct wdrr_bucket *bucket)
  298. {
  299. struct sk_buff *skb = bucket->head;
  300. bucket->head = skb->next;
  301. skb_mark_not_on_list(skb);
  302. return skb;
  303. }
  304. /* Tail-adds skb to bucket. */
  305. static void bucket_add(struct wdrr_bucket *bucket, struct sk_buff *skb)
  306. {
  307. if (bucket->head == NULL)
  308. bucket->head = skb;
  309. else
  310. bucket->tail->next = skb;
  311. bucket->tail = skb;
  312. skb->next = NULL;
  313. }
  314. static unsigned int hhf_drop(struct Qdisc *sch, struct sk_buff **to_free)
  315. {
  316. struct hhf_sched_data *q = qdisc_priv(sch);
  317. struct wdrr_bucket *bucket;
  318. /* Always try to drop from heavy-hitters first. */
  319. bucket = &q->buckets[WDRR_BUCKET_FOR_HH];
  320. if (!bucket->head)
  321. bucket = &q->buckets[WDRR_BUCKET_FOR_NON_HH];
  322. if (bucket->head) {
  323. struct sk_buff *skb = dequeue_head(bucket);
  324. sch->q.qlen--;
  325. qdisc_qstats_backlog_dec(sch, skb);
  326. qdisc_drop(skb, sch, to_free);
  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. struct sk_buff **to_free)
  333. {
  334. struct hhf_sched_data *q = qdisc_priv(sch);
  335. enum wdrr_bucket_idx idx;
  336. struct wdrr_bucket *bucket;
  337. unsigned int prev_backlog;
  338. idx = hhf_classify(skb, sch);
  339. bucket = &q->buckets[idx];
  340. bucket_add(bucket, skb);
  341. qdisc_qstats_backlog_inc(sch, skb);
  342. if (list_empty(&bucket->bucketchain)) {
  343. unsigned int weight;
  344. /* The logic of new_buckets vs. old_buckets is the same as
  345. * new_flows vs. old_flows in the implementation of fq_codel,
  346. * i.e., short bursts of non-HHs should have strict priority.
  347. */
  348. if (idx == WDRR_BUCKET_FOR_HH) {
  349. /* Always move heavy-hitters to old bucket. */
  350. weight = 1;
  351. list_add_tail(&bucket->bucketchain, &q->old_buckets);
  352. } else {
  353. weight = q->hhf_non_hh_weight;
  354. list_add_tail(&bucket->bucketchain, &q->new_buckets);
  355. }
  356. bucket->deficit = weight * q->quantum;
  357. }
  358. if (++sch->q.qlen <= sch->limit)
  359. return NET_XMIT_SUCCESS;
  360. prev_backlog = sch->qstats.backlog;
  361. q->drop_overlimit++;
  362. /* Return Congestion Notification only if we dropped a packet from this
  363. * bucket.
  364. */
  365. if (hhf_drop(sch, to_free) == idx)
  366. return NET_XMIT_CN;
  367. /* As we dropped a packet, better let upper stack know this. */
  368. qdisc_tree_reduce_backlog(sch, 1, prev_backlog - sch->qstats.backlog);
  369. return NET_XMIT_SUCCESS;
  370. }
  371. static struct sk_buff *hhf_dequeue(struct Qdisc *sch)
  372. {
  373. struct hhf_sched_data *q = qdisc_priv(sch);
  374. struct sk_buff *skb = NULL;
  375. struct wdrr_bucket *bucket;
  376. struct list_head *head;
  377. begin:
  378. head = &q->new_buckets;
  379. if (list_empty(head)) {
  380. head = &q->old_buckets;
  381. if (list_empty(head))
  382. return NULL;
  383. }
  384. bucket = list_first_entry(head, struct wdrr_bucket, bucketchain);
  385. if (bucket->deficit <= 0) {
  386. int weight = (bucket - q->buckets == WDRR_BUCKET_FOR_HH) ?
  387. 1 : q->hhf_non_hh_weight;
  388. bucket->deficit += weight * q->quantum;
  389. list_move_tail(&bucket->bucketchain, &q->old_buckets);
  390. goto begin;
  391. }
  392. if (bucket->head) {
  393. skb = dequeue_head(bucket);
  394. sch->q.qlen--;
  395. qdisc_qstats_backlog_dec(sch, skb);
  396. }
  397. if (!skb) {
  398. /* Force a pass through old_buckets to prevent starvation. */
  399. if ((head == &q->new_buckets) && !list_empty(&q->old_buckets))
  400. list_move_tail(&bucket->bucketchain, &q->old_buckets);
  401. else
  402. list_del_init(&bucket->bucketchain);
  403. goto begin;
  404. }
  405. qdisc_bstats_update(sch, skb);
  406. bucket->deficit -= qdisc_pkt_len(skb);
  407. return skb;
  408. }
  409. static void hhf_reset(struct Qdisc *sch)
  410. {
  411. struct sk_buff *skb;
  412. while ((skb = hhf_dequeue(sch)) != NULL)
  413. rtnl_kfree_skbs(skb, skb);
  414. }
  415. static void hhf_destroy(struct Qdisc *sch)
  416. {
  417. int i;
  418. struct hhf_sched_data *q = qdisc_priv(sch);
  419. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  420. kvfree(q->hhf_arrays[i]);
  421. kvfree(q->hhf_valid_bits[i]);
  422. }
  423. if (!q->hh_flows)
  424. return;
  425. for (i = 0; i < HH_FLOWS_CNT; i++) {
  426. struct hh_flow_state *flow, *next;
  427. struct list_head *head = &q->hh_flows[i];
  428. if (list_empty(head))
  429. continue;
  430. list_for_each_entry_safe(flow, next, head, flowchain) {
  431. list_del(&flow->flowchain);
  432. kfree(flow);
  433. }
  434. }
  435. kvfree(q->hh_flows);
  436. }
  437. static const struct nla_policy hhf_policy[TCA_HHF_MAX + 1] = {
  438. [TCA_HHF_BACKLOG_LIMIT] = { .type = NLA_U32 },
  439. [TCA_HHF_QUANTUM] = { .type = NLA_U32 },
  440. [TCA_HHF_HH_FLOWS_LIMIT] = { .type = NLA_U32 },
  441. [TCA_HHF_RESET_TIMEOUT] = { .type = NLA_U32 },
  442. [TCA_HHF_ADMIT_BYTES] = { .type = NLA_U32 },
  443. [TCA_HHF_EVICT_TIMEOUT] = { .type = NLA_U32 },
  444. [TCA_HHF_NON_HH_WEIGHT] = { .type = NLA_U32 },
  445. };
  446. static int hhf_change(struct Qdisc *sch, struct nlattr *opt,
  447. struct netlink_ext_ack *extack)
  448. {
  449. struct hhf_sched_data *q = qdisc_priv(sch);
  450. struct nlattr *tb[TCA_HHF_MAX + 1];
  451. unsigned int qlen, prev_backlog;
  452. int err;
  453. u64 non_hh_quantum;
  454. u32 new_quantum = q->quantum;
  455. u32 new_hhf_non_hh_weight = q->hhf_non_hh_weight;
  456. err = nla_parse_nested_deprecated(tb, TCA_HHF_MAX, opt, hhf_policy,
  457. NULL);
  458. if (err < 0)
  459. return err;
  460. if (tb[TCA_HHF_QUANTUM])
  461. new_quantum = nla_get_u32(tb[TCA_HHF_QUANTUM]);
  462. if (tb[TCA_HHF_NON_HH_WEIGHT])
  463. new_hhf_non_hh_weight = nla_get_u32(tb[TCA_HHF_NON_HH_WEIGHT]);
  464. non_hh_quantum = (u64)new_quantum * new_hhf_non_hh_weight;
  465. if (non_hh_quantum == 0 || non_hh_quantum > INT_MAX)
  466. return -EINVAL;
  467. sch_tree_lock(sch);
  468. if (tb[TCA_HHF_BACKLOG_LIMIT])
  469. sch->limit = nla_get_u32(tb[TCA_HHF_BACKLOG_LIMIT]);
  470. q->quantum = new_quantum;
  471. q->hhf_non_hh_weight = new_hhf_non_hh_weight;
  472. if (tb[TCA_HHF_HH_FLOWS_LIMIT])
  473. q->hh_flows_limit = nla_get_u32(tb[TCA_HHF_HH_FLOWS_LIMIT]);
  474. if (tb[TCA_HHF_RESET_TIMEOUT]) {
  475. u32 us = nla_get_u32(tb[TCA_HHF_RESET_TIMEOUT]);
  476. q->hhf_reset_timeout = usecs_to_jiffies(us);
  477. }
  478. if (tb[TCA_HHF_ADMIT_BYTES])
  479. q->hhf_admit_bytes = nla_get_u32(tb[TCA_HHF_ADMIT_BYTES]);
  480. if (tb[TCA_HHF_EVICT_TIMEOUT]) {
  481. u32 us = nla_get_u32(tb[TCA_HHF_EVICT_TIMEOUT]);
  482. q->hhf_evict_timeout = usecs_to_jiffies(us);
  483. }
  484. qlen = sch->q.qlen;
  485. prev_backlog = sch->qstats.backlog;
  486. while (sch->q.qlen > sch->limit) {
  487. struct sk_buff *skb = hhf_dequeue(sch);
  488. rtnl_kfree_skbs(skb, skb);
  489. }
  490. qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen,
  491. prev_backlog - sch->qstats.backlog);
  492. sch_tree_unlock(sch);
  493. return 0;
  494. }
  495. static int hhf_init(struct Qdisc *sch, struct nlattr *opt,
  496. struct netlink_ext_ack *extack)
  497. {
  498. struct hhf_sched_data *q = qdisc_priv(sch);
  499. int i;
  500. sch->limit = 1000;
  501. q->quantum = psched_mtu(qdisc_dev(sch));
  502. get_random_bytes(&q->perturbation, sizeof(q->perturbation));
  503. INIT_LIST_HEAD(&q->new_buckets);
  504. INIT_LIST_HEAD(&q->old_buckets);
  505. /* Configurable HHF parameters */
  506. q->hhf_reset_timeout = HZ / 25; /* 40 ms */
  507. q->hhf_admit_bytes = 131072; /* 128 KB */
  508. q->hhf_evict_timeout = HZ; /* 1 sec */
  509. q->hhf_non_hh_weight = 2;
  510. if (opt) {
  511. int err = hhf_change(sch, opt, extack);
  512. if (err)
  513. return err;
  514. }
  515. if (!q->hh_flows) {
  516. /* Initialize heavy-hitter flow table. */
  517. q->hh_flows = kvcalloc(HH_FLOWS_CNT, sizeof(struct list_head),
  518. GFP_KERNEL);
  519. if (!q->hh_flows)
  520. return -ENOMEM;
  521. for (i = 0; i < HH_FLOWS_CNT; i++)
  522. INIT_LIST_HEAD(&q->hh_flows[i]);
  523. /* Cap max active HHs at twice len of hh_flows table. */
  524. q->hh_flows_limit = 2 * HH_FLOWS_CNT;
  525. q->hh_flows_overlimit = 0;
  526. q->hh_flows_total_cnt = 0;
  527. q->hh_flows_current_cnt = 0;
  528. /* Initialize heavy-hitter filter arrays. */
  529. for (i = 0; i < HHF_ARRAYS_CNT; i++) {
  530. q->hhf_arrays[i] = kvcalloc(HHF_ARRAYS_LEN,
  531. sizeof(u32),
  532. GFP_KERNEL);
  533. if (!q->hhf_arrays[i]) {
  534. /* Note: hhf_destroy() will be called
  535. * by our caller.
  536. */
  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] = kvzalloc(HHF_ARRAYS_LEN /
  544. BITS_PER_BYTE, GFP_KERNEL);
  545. if (!q->hhf_valid_bits[i]) {
  546. /* Note: hhf_destroy() will be called
  547. * by our caller.
  548. */
  549. return -ENOMEM;
  550. }
  551. }
  552. /* Initialize Weighted DRR buckets. */
  553. for (i = 0; i < WDRR_BUCKET_CNT; i++) {
  554. struct wdrr_bucket *bucket = q->buckets + i;
  555. INIT_LIST_HEAD(&bucket->bucketchain);
  556. }
  557. }
  558. return 0;
  559. }
  560. static int hhf_dump(struct Qdisc *sch, struct sk_buff *skb)
  561. {
  562. struct hhf_sched_data *q = qdisc_priv(sch);
  563. struct nlattr *opts;
  564. opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
  565. if (opts == NULL)
  566. goto nla_put_failure;
  567. if (nla_put_u32(skb, TCA_HHF_BACKLOG_LIMIT, sch->limit) ||
  568. nla_put_u32(skb, TCA_HHF_QUANTUM, q->quantum) ||
  569. nla_put_u32(skb, TCA_HHF_HH_FLOWS_LIMIT, q->hh_flows_limit) ||
  570. nla_put_u32(skb, TCA_HHF_RESET_TIMEOUT,
  571. jiffies_to_usecs(q->hhf_reset_timeout)) ||
  572. nla_put_u32(skb, TCA_HHF_ADMIT_BYTES, q->hhf_admit_bytes) ||
  573. nla_put_u32(skb, TCA_HHF_EVICT_TIMEOUT,
  574. jiffies_to_usecs(q->hhf_evict_timeout)) ||
  575. nla_put_u32(skb, TCA_HHF_NON_HH_WEIGHT, q->hhf_non_hh_weight))
  576. goto nla_put_failure;
  577. return nla_nest_end(skb, opts);
  578. nla_put_failure:
  579. return -1;
  580. }
  581. static int hhf_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  582. {
  583. struct hhf_sched_data *q = qdisc_priv(sch);
  584. struct tc_hhf_xstats st = {
  585. .drop_overlimit = q->drop_overlimit,
  586. .hh_overlimit = q->hh_flows_overlimit,
  587. .hh_tot_count = q->hh_flows_total_cnt,
  588. .hh_cur_count = q->hh_flows_current_cnt,
  589. };
  590. return gnet_stats_copy_app(d, &st, sizeof(st));
  591. }
  592. static struct Qdisc_ops hhf_qdisc_ops __read_mostly = {
  593. .id = "hhf",
  594. .priv_size = sizeof(struct hhf_sched_data),
  595. .enqueue = hhf_enqueue,
  596. .dequeue = hhf_dequeue,
  597. .peek = qdisc_peek_dequeued,
  598. .init = hhf_init,
  599. .reset = hhf_reset,
  600. .destroy = hhf_destroy,
  601. .change = hhf_change,
  602. .dump = hhf_dump,
  603. .dump_stats = hhf_dump_stats,
  604. .owner = THIS_MODULE,
  605. };
  606. static int __init hhf_module_init(void)
  607. {
  608. return register_qdisc(&hhf_qdisc_ops);
  609. }
  610. static void __exit hhf_module_exit(void)
  611. {
  612. unregister_qdisc(&hhf_qdisc_ops);
  613. }
  614. module_init(hhf_module_init)
  615. module_exit(hhf_module_exit)
  616. MODULE_AUTHOR("Terry Lam");
  617. MODULE_AUTHOR("Nandita Dukkipati");
  618. MODULE_LICENSE("GPL");
  619. MODULE_DESCRIPTION("Heavy-Hitter Filter (HHF)");