sch_fq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * net/sched/sch_fq.c Fair Queue Packet Scheduler (per flow pacing)
  3. *
  4. * Copyright (C) 2013 Eric Dumazet <edumazet@google.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Meant to be mostly used for localy generated traffic :
  12. * Fast classification depends on skb->sk being set before reaching us.
  13. * If not, (router workload), we use rxhash as fallback, with 32 bits wide hash.
  14. * All packets belonging to a socket are considered as a 'flow'.
  15. *
  16. * Flows are dynamically allocated and stored in a hash table of RB trees
  17. * They are also part of one Round Robin 'queues' (new or old flows)
  18. *
  19. * Burst avoidance (aka pacing) capability :
  20. *
  21. * Transport (eg TCP) can set in sk->sk_pacing_rate a rate, enqueue a
  22. * bunch of packets, and this packet scheduler adds delay between
  23. * packets to respect rate limitation.
  24. *
  25. * enqueue() :
  26. * - lookup one RB tree (out of 1024 or more) to find the flow.
  27. * If non existent flow, create it, add it to the tree.
  28. * Add skb to the per flow list of skb (fifo).
  29. * - Use a special fifo for high prio packets
  30. *
  31. * dequeue() : serves flows in Round Robin
  32. * Note : When a flow becomes empty, we do not immediately remove it from
  33. * rb trees, for performance reasons (its expected to send additional packets,
  34. * or SLAB cache will reuse socket for another flow)
  35. */
  36. #include <linux/module.h>
  37. #include <linux/types.h>
  38. #include <linux/kernel.h>
  39. #include <linux/jiffies.h>
  40. #include <linux/string.h>
  41. #include <linux/in.h>
  42. #include <linux/errno.h>
  43. #include <linux/init.h>
  44. #include <linux/skbuff.h>
  45. #include <linux/slab.h>
  46. #include <linux/rbtree.h>
  47. #include <linux/hash.h>
  48. #include <net/netlink.h>
  49. #include <net/pkt_sched.h>
  50. #include <net/sock.h>
  51. #include <net/tcp_states.h>
  52. /*
  53. * Per flow structure, dynamically allocated
  54. */
  55. struct fq_flow {
  56. struct sk_buff *head; /* list of skbs for this flow : first skb */
  57. union {
  58. struct sk_buff *tail; /* last skb in the list */
  59. unsigned long age; /* jiffies when flow was emptied, for gc */
  60. };
  61. struct rb_node fq_node; /* anchor in fq_root[] trees */
  62. struct sock *sk;
  63. int qlen; /* number of packets in flow queue */
  64. int credit;
  65. u32 socket_hash; /* sk_hash */
  66. struct fq_flow *next; /* next pointer in RR lists, or &detached */
  67. struct rb_node rate_node; /* anchor in q->delayed tree */
  68. u64 time_next_packet;
  69. };
  70. struct fq_flow_head {
  71. struct fq_flow *first;
  72. struct fq_flow *last;
  73. };
  74. struct fq_sched_data {
  75. struct fq_flow_head new_flows;
  76. struct fq_flow_head old_flows;
  77. struct rb_root delayed; /* for rate limited flows */
  78. u64 time_next_delayed_flow;
  79. struct fq_flow internal; /* for non classified or high prio packets */
  80. u32 quantum;
  81. u32 initial_quantum;
  82. u32 flow_default_rate;/* rate per flow : bytes per second */
  83. u32 flow_max_rate; /* optional max rate per flow */
  84. u32 flow_plimit; /* max packets per flow */
  85. struct rb_root *fq_root;
  86. u8 rate_enable;
  87. u8 fq_trees_log;
  88. u32 flows;
  89. u32 inactive_flows;
  90. u32 throttled_flows;
  91. u64 stat_gc_flows;
  92. u64 stat_internal_packets;
  93. u64 stat_tcp_retrans;
  94. u64 stat_throttled;
  95. u64 stat_flows_plimit;
  96. u64 stat_pkts_too_long;
  97. u64 stat_allocation_errors;
  98. struct qdisc_watchdog watchdog;
  99. };
  100. /* special value to mark a detached flow (not on old/new list) */
  101. static struct fq_flow detached, throttled;
  102. static void fq_flow_set_detached(struct fq_flow *f)
  103. {
  104. f->next = &detached;
  105. }
  106. static bool fq_flow_is_detached(const struct fq_flow *f)
  107. {
  108. return f->next == &detached;
  109. }
  110. static void fq_flow_set_throttled(struct fq_sched_data *q, struct fq_flow *f)
  111. {
  112. struct rb_node **p = &q->delayed.rb_node, *parent = NULL;
  113. while (*p) {
  114. struct fq_flow *aux;
  115. parent = *p;
  116. aux = container_of(parent, struct fq_flow, rate_node);
  117. if (f->time_next_packet >= aux->time_next_packet)
  118. p = &parent->rb_right;
  119. else
  120. p = &parent->rb_left;
  121. }
  122. rb_link_node(&f->rate_node, parent, p);
  123. rb_insert_color(&f->rate_node, &q->delayed);
  124. q->throttled_flows++;
  125. q->stat_throttled++;
  126. f->next = &throttled;
  127. if (q->time_next_delayed_flow > f->time_next_packet)
  128. q->time_next_delayed_flow = f->time_next_packet;
  129. }
  130. static struct kmem_cache *fq_flow_cachep __read_mostly;
  131. static void fq_flow_add_tail(struct fq_flow_head *head, struct fq_flow *flow)
  132. {
  133. if (head->first)
  134. head->last->next = flow;
  135. else
  136. head->first = flow;
  137. head->last = flow;
  138. flow->next = NULL;
  139. }
  140. /* limit number of collected flows per round */
  141. #define FQ_GC_MAX 8
  142. #define FQ_GC_AGE (3*HZ)
  143. static bool fq_gc_candidate(const struct fq_flow *f)
  144. {
  145. return fq_flow_is_detached(f) &&
  146. time_after(jiffies, f->age + FQ_GC_AGE);
  147. }
  148. static void fq_gc(struct fq_sched_data *q,
  149. struct rb_root *root,
  150. struct sock *sk)
  151. {
  152. struct fq_flow *f, *tofree[FQ_GC_MAX];
  153. struct rb_node **p, *parent;
  154. int fcnt = 0;
  155. p = &root->rb_node;
  156. parent = NULL;
  157. while (*p) {
  158. parent = *p;
  159. f = container_of(parent, struct fq_flow, fq_node);
  160. if (f->sk == sk)
  161. break;
  162. if (fq_gc_candidate(f)) {
  163. tofree[fcnt++] = f;
  164. if (fcnt == FQ_GC_MAX)
  165. break;
  166. }
  167. if (f->sk > sk)
  168. p = &parent->rb_right;
  169. else
  170. p = &parent->rb_left;
  171. }
  172. q->flows -= fcnt;
  173. q->inactive_flows -= fcnt;
  174. q->stat_gc_flows += fcnt;
  175. while (fcnt) {
  176. struct fq_flow *f = tofree[--fcnt];
  177. rb_erase(&f->fq_node, root);
  178. kmem_cache_free(fq_flow_cachep, f);
  179. }
  180. }
  181. static const u8 prio2band[TC_PRIO_MAX + 1] = {
  182. 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
  183. };
  184. static struct fq_flow *fq_classify(struct sk_buff *skb, struct fq_sched_data *q)
  185. {
  186. struct rb_node **p, *parent;
  187. struct sock *sk = skb->sk;
  188. struct rb_root *root;
  189. struct fq_flow *f;
  190. int band;
  191. /* warning: no starvation prevention... */
  192. band = prio2band[skb->priority & TC_PRIO_MAX];
  193. if (unlikely(band == 0))
  194. return &q->internal;
  195. if (unlikely(!sk)) {
  196. /* By forcing low order bit to 1, we make sure to not
  197. * collide with a local flow (socket pointers are word aligned)
  198. */
  199. sk = (struct sock *)(skb_get_rxhash(skb) | 1L);
  200. }
  201. root = &q->fq_root[hash_32((u32)(long)sk, q->fq_trees_log)];
  202. if (q->flows >= (2U << q->fq_trees_log) &&
  203. q->inactive_flows > q->flows/2)
  204. fq_gc(q, root, sk);
  205. p = &root->rb_node;
  206. parent = NULL;
  207. while (*p) {
  208. parent = *p;
  209. f = container_of(parent, struct fq_flow, fq_node);
  210. if (f->sk == sk) {
  211. /* socket might have been reallocated, so check
  212. * if its sk_hash is the same.
  213. * It not, we need to refill credit with
  214. * initial quantum
  215. */
  216. if (unlikely(skb->sk &&
  217. f->socket_hash != sk->sk_hash)) {
  218. f->credit = q->initial_quantum;
  219. f->socket_hash = sk->sk_hash;
  220. }
  221. return f;
  222. }
  223. if (f->sk > sk)
  224. p = &parent->rb_right;
  225. else
  226. p = &parent->rb_left;
  227. }
  228. f = kmem_cache_zalloc(fq_flow_cachep, GFP_ATOMIC | __GFP_NOWARN);
  229. if (unlikely(!f)) {
  230. q->stat_allocation_errors++;
  231. return &q->internal;
  232. }
  233. fq_flow_set_detached(f);
  234. f->sk = sk;
  235. if (skb->sk)
  236. f->socket_hash = sk->sk_hash;
  237. f->credit = q->initial_quantum;
  238. rb_link_node(&f->fq_node, parent, p);
  239. rb_insert_color(&f->fq_node, root);
  240. q->flows++;
  241. q->inactive_flows++;
  242. return f;
  243. }
  244. /* remove one skb from head of flow queue */
  245. static struct sk_buff *fq_dequeue_head(struct fq_flow *flow)
  246. {
  247. struct sk_buff *skb = flow->head;
  248. if (skb) {
  249. flow->head = skb->next;
  250. skb->next = NULL;
  251. flow->qlen--;
  252. }
  253. return skb;
  254. }
  255. /* We might add in the future detection of retransmits
  256. * For the time being, just return false
  257. */
  258. static bool skb_is_retransmit(struct sk_buff *skb)
  259. {
  260. return false;
  261. }
  262. /* add skb to flow queue
  263. * flow queue is a linked list, kind of FIFO, except for TCP retransmits
  264. * We special case tcp retransmits to be transmitted before other packets.
  265. * We rely on fact that TCP retransmits are unlikely, so we do not waste
  266. * a separate queue or a pointer.
  267. * head-> [retrans pkt 1]
  268. * [retrans pkt 2]
  269. * [ normal pkt 1]
  270. * [ normal pkt 2]
  271. * [ normal pkt 3]
  272. * tail-> [ normal pkt 4]
  273. */
  274. static void flow_queue_add(struct fq_flow *flow, struct sk_buff *skb)
  275. {
  276. struct sk_buff *prev, *head = flow->head;
  277. skb->next = NULL;
  278. if (!head) {
  279. flow->head = skb;
  280. flow->tail = skb;
  281. return;
  282. }
  283. if (likely(!skb_is_retransmit(skb))) {
  284. flow->tail->next = skb;
  285. flow->tail = skb;
  286. return;
  287. }
  288. /* This skb is a tcp retransmit,
  289. * find the last retrans packet in the queue
  290. */
  291. prev = NULL;
  292. while (skb_is_retransmit(head)) {
  293. prev = head;
  294. head = head->next;
  295. if (!head)
  296. break;
  297. }
  298. if (!prev) { /* no rtx packet in queue, become the new head */
  299. skb->next = flow->head;
  300. flow->head = skb;
  301. } else {
  302. if (prev == flow->tail)
  303. flow->tail = skb;
  304. else
  305. skb->next = prev->next;
  306. prev->next = skb;
  307. }
  308. }
  309. static int fq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  310. {
  311. struct fq_sched_data *q = qdisc_priv(sch);
  312. struct fq_flow *f;
  313. if (unlikely(sch->q.qlen >= sch->limit))
  314. return qdisc_drop(skb, sch);
  315. f = fq_classify(skb, q);
  316. if (unlikely(f->qlen >= q->flow_plimit && f != &q->internal)) {
  317. q->stat_flows_plimit++;
  318. return qdisc_drop(skb, sch);
  319. }
  320. f->qlen++;
  321. flow_queue_add(f, skb);
  322. if (skb_is_retransmit(skb))
  323. q->stat_tcp_retrans++;
  324. sch->qstats.backlog += qdisc_pkt_len(skb);
  325. if (fq_flow_is_detached(f)) {
  326. fq_flow_add_tail(&q->new_flows, f);
  327. if (q->quantum > f->credit)
  328. f->credit = q->quantum;
  329. q->inactive_flows--;
  330. qdisc_unthrottled(sch);
  331. }
  332. if (unlikely(f == &q->internal)) {
  333. q->stat_internal_packets++;
  334. qdisc_unthrottled(sch);
  335. }
  336. sch->q.qlen++;
  337. return NET_XMIT_SUCCESS;
  338. }
  339. static void fq_check_throttled(struct fq_sched_data *q, u64 now)
  340. {
  341. struct rb_node *p;
  342. if (q->time_next_delayed_flow > now)
  343. return;
  344. q->time_next_delayed_flow = ~0ULL;
  345. while ((p = rb_first(&q->delayed)) != NULL) {
  346. struct fq_flow *f = container_of(p, struct fq_flow, rate_node);
  347. if (f->time_next_packet > now) {
  348. q->time_next_delayed_flow = f->time_next_packet;
  349. break;
  350. }
  351. rb_erase(p, &q->delayed);
  352. q->throttled_flows--;
  353. fq_flow_add_tail(&q->old_flows, f);
  354. }
  355. }
  356. static struct sk_buff *fq_dequeue(struct Qdisc *sch)
  357. {
  358. struct fq_sched_data *q = qdisc_priv(sch);
  359. u64 now = ktime_to_ns(ktime_get());
  360. struct fq_flow_head *head;
  361. struct sk_buff *skb;
  362. struct fq_flow *f;
  363. skb = fq_dequeue_head(&q->internal);
  364. if (skb)
  365. goto out;
  366. fq_check_throttled(q, now);
  367. begin:
  368. head = &q->new_flows;
  369. if (!head->first) {
  370. head = &q->old_flows;
  371. if (!head->first) {
  372. if (q->time_next_delayed_flow != ~0ULL)
  373. qdisc_watchdog_schedule_ns(&q->watchdog,
  374. q->time_next_delayed_flow);
  375. return NULL;
  376. }
  377. }
  378. f = head->first;
  379. if (f->credit <= 0) {
  380. f->credit += q->quantum;
  381. head->first = f->next;
  382. fq_flow_add_tail(&q->old_flows, f);
  383. goto begin;
  384. }
  385. if (unlikely(f->head && now < f->time_next_packet)) {
  386. head->first = f->next;
  387. fq_flow_set_throttled(q, f);
  388. goto begin;
  389. }
  390. skb = fq_dequeue_head(f);
  391. if (!skb) {
  392. head->first = f->next;
  393. /* force a pass through old_flows to prevent starvation */
  394. if ((head == &q->new_flows) && q->old_flows.first) {
  395. fq_flow_add_tail(&q->old_flows, f);
  396. } else {
  397. fq_flow_set_detached(f);
  398. f->age = jiffies;
  399. q->inactive_flows++;
  400. }
  401. goto begin;
  402. }
  403. f->time_next_packet = now;
  404. f->credit -= qdisc_pkt_len(skb);
  405. if (f->credit <= 0 &&
  406. q->rate_enable &&
  407. skb->sk && skb->sk->sk_state != TCP_TIME_WAIT) {
  408. u32 rate = skb->sk->sk_pacing_rate ?: q->flow_default_rate;
  409. rate = min(rate, q->flow_max_rate);
  410. if (rate) {
  411. u64 len = (u64)qdisc_pkt_len(skb) * NSEC_PER_SEC;
  412. do_div(len, rate);
  413. /* Since socket rate can change later,
  414. * clamp the delay to 125 ms.
  415. * TODO: maybe segment the too big skb, as in commit
  416. * e43ac79a4bc ("sch_tbf: segment too big GSO packets")
  417. */
  418. if (unlikely(len > 125 * NSEC_PER_MSEC)) {
  419. len = 125 * NSEC_PER_MSEC;
  420. q->stat_pkts_too_long++;
  421. }
  422. f->time_next_packet = now + len;
  423. }
  424. }
  425. out:
  426. prefetch(&skb->end);
  427. sch->qstats.backlog -= qdisc_pkt_len(skb);
  428. qdisc_bstats_update(sch, skb);
  429. sch->q.qlen--;
  430. qdisc_unthrottled(sch);
  431. return skb;
  432. }
  433. static void fq_reset(struct Qdisc *sch)
  434. {
  435. struct sk_buff *skb;
  436. while ((skb = fq_dequeue(sch)) != NULL)
  437. kfree_skb(skb);
  438. }
  439. static void fq_rehash(struct fq_sched_data *q,
  440. struct rb_root *old_array, u32 old_log,
  441. struct rb_root *new_array, u32 new_log)
  442. {
  443. struct rb_node *op, **np, *parent;
  444. struct rb_root *oroot, *nroot;
  445. struct fq_flow *of, *nf;
  446. int fcnt = 0;
  447. u32 idx;
  448. for (idx = 0; idx < (1U << old_log); idx++) {
  449. oroot = &old_array[idx];
  450. while ((op = rb_first(oroot)) != NULL) {
  451. rb_erase(op, oroot);
  452. of = container_of(op, struct fq_flow, fq_node);
  453. if (fq_gc_candidate(of)) {
  454. fcnt++;
  455. kmem_cache_free(fq_flow_cachep, of);
  456. continue;
  457. }
  458. nroot = &new_array[hash_32((u32)(long)of->sk, new_log)];
  459. np = &nroot->rb_node;
  460. parent = NULL;
  461. while (*np) {
  462. parent = *np;
  463. nf = container_of(parent, struct fq_flow, fq_node);
  464. BUG_ON(nf->sk == of->sk);
  465. if (nf->sk > of->sk)
  466. np = &parent->rb_right;
  467. else
  468. np = &parent->rb_left;
  469. }
  470. rb_link_node(&of->fq_node, parent, np);
  471. rb_insert_color(&of->fq_node, nroot);
  472. }
  473. }
  474. q->flows -= fcnt;
  475. q->inactive_flows -= fcnt;
  476. q->stat_gc_flows += fcnt;
  477. }
  478. static int fq_resize(struct fq_sched_data *q, u32 log)
  479. {
  480. struct rb_root *array;
  481. u32 idx;
  482. if (q->fq_root && log == q->fq_trees_log)
  483. return 0;
  484. array = kmalloc(sizeof(struct rb_root) << log, GFP_KERNEL);
  485. if (!array)
  486. return -ENOMEM;
  487. for (idx = 0; idx < (1U << log); idx++)
  488. array[idx] = RB_ROOT;
  489. if (q->fq_root) {
  490. fq_rehash(q, q->fq_root, q->fq_trees_log, array, log);
  491. kfree(q->fq_root);
  492. }
  493. q->fq_root = array;
  494. q->fq_trees_log = log;
  495. return 0;
  496. }
  497. static const struct nla_policy fq_policy[TCA_FQ_MAX + 1] = {
  498. [TCA_FQ_PLIMIT] = { .type = NLA_U32 },
  499. [TCA_FQ_FLOW_PLIMIT] = { .type = NLA_U32 },
  500. [TCA_FQ_QUANTUM] = { .type = NLA_U32 },
  501. [TCA_FQ_INITIAL_QUANTUM] = { .type = NLA_U32 },
  502. [TCA_FQ_RATE_ENABLE] = { .type = NLA_U32 },
  503. [TCA_FQ_FLOW_DEFAULT_RATE] = { .type = NLA_U32 },
  504. [TCA_FQ_FLOW_MAX_RATE] = { .type = NLA_U32 },
  505. [TCA_FQ_BUCKETS_LOG] = { .type = NLA_U32 },
  506. };
  507. static int fq_change(struct Qdisc *sch, struct nlattr *opt)
  508. {
  509. struct fq_sched_data *q = qdisc_priv(sch);
  510. struct nlattr *tb[TCA_FQ_MAX + 1];
  511. int err, drop_count = 0;
  512. u32 fq_log;
  513. if (!opt)
  514. return -EINVAL;
  515. err = nla_parse_nested(tb, TCA_FQ_MAX, opt, fq_policy);
  516. if (err < 0)
  517. return err;
  518. sch_tree_lock(sch);
  519. fq_log = q->fq_trees_log;
  520. if (tb[TCA_FQ_BUCKETS_LOG]) {
  521. u32 nval = nla_get_u32(tb[TCA_FQ_BUCKETS_LOG]);
  522. if (nval >= 1 && nval <= ilog2(256*1024))
  523. fq_log = nval;
  524. else
  525. err = -EINVAL;
  526. }
  527. if (tb[TCA_FQ_PLIMIT])
  528. sch->limit = nla_get_u32(tb[TCA_FQ_PLIMIT]);
  529. if (tb[TCA_FQ_FLOW_PLIMIT])
  530. q->flow_plimit = nla_get_u32(tb[TCA_FQ_FLOW_PLIMIT]);
  531. if (tb[TCA_FQ_QUANTUM])
  532. q->quantum = nla_get_u32(tb[TCA_FQ_QUANTUM]);
  533. if (tb[TCA_FQ_INITIAL_QUANTUM])
  534. q->quantum = nla_get_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
  535. if (tb[TCA_FQ_FLOW_DEFAULT_RATE])
  536. q->flow_default_rate = nla_get_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]);
  537. if (tb[TCA_FQ_FLOW_MAX_RATE])
  538. q->flow_max_rate = nla_get_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
  539. if (tb[TCA_FQ_RATE_ENABLE]) {
  540. u32 enable = nla_get_u32(tb[TCA_FQ_RATE_ENABLE]);
  541. if (enable <= 1)
  542. q->rate_enable = enable;
  543. else
  544. err = -EINVAL;
  545. }
  546. if (!err)
  547. err = fq_resize(q, fq_log);
  548. while (sch->q.qlen > sch->limit) {
  549. struct sk_buff *skb = fq_dequeue(sch);
  550. kfree_skb(skb);
  551. drop_count++;
  552. }
  553. qdisc_tree_decrease_qlen(sch, drop_count);
  554. sch_tree_unlock(sch);
  555. return err;
  556. }
  557. static void fq_destroy(struct Qdisc *sch)
  558. {
  559. struct fq_sched_data *q = qdisc_priv(sch);
  560. struct rb_root *root;
  561. struct rb_node *p;
  562. unsigned int idx;
  563. if (q->fq_root) {
  564. for (idx = 0; idx < (1U << q->fq_trees_log); idx++) {
  565. root = &q->fq_root[idx];
  566. while ((p = rb_first(root)) != NULL) {
  567. rb_erase(p, root);
  568. kmem_cache_free(fq_flow_cachep,
  569. container_of(p, struct fq_flow, fq_node));
  570. }
  571. }
  572. kfree(q->fq_root);
  573. }
  574. qdisc_watchdog_cancel(&q->watchdog);
  575. }
  576. static int fq_init(struct Qdisc *sch, struct nlattr *opt)
  577. {
  578. struct fq_sched_data *q = qdisc_priv(sch);
  579. int err;
  580. sch->limit = 10000;
  581. q->flow_plimit = 100;
  582. q->quantum = 2 * psched_mtu(qdisc_dev(sch));
  583. q->initial_quantum = 10 * psched_mtu(qdisc_dev(sch));
  584. q->flow_default_rate = 0;
  585. q->flow_max_rate = ~0U;
  586. q->rate_enable = 1;
  587. q->new_flows.first = NULL;
  588. q->old_flows.first = NULL;
  589. q->delayed = RB_ROOT;
  590. q->fq_root = NULL;
  591. q->fq_trees_log = ilog2(1024);
  592. qdisc_watchdog_init(&q->watchdog, sch);
  593. if (opt)
  594. err = fq_change(sch, opt);
  595. else
  596. err = fq_resize(q, q->fq_trees_log);
  597. return err;
  598. }
  599. static int fq_dump(struct Qdisc *sch, struct sk_buff *skb)
  600. {
  601. struct fq_sched_data *q = qdisc_priv(sch);
  602. struct nlattr *opts;
  603. opts = nla_nest_start(skb, TCA_OPTIONS);
  604. if (opts == NULL)
  605. goto nla_put_failure;
  606. if (nla_put_u32(skb, TCA_FQ_PLIMIT, sch->limit) ||
  607. nla_put_u32(skb, TCA_FQ_FLOW_PLIMIT, q->flow_plimit) ||
  608. nla_put_u32(skb, TCA_FQ_QUANTUM, q->quantum) ||
  609. nla_put_u32(skb, TCA_FQ_INITIAL_QUANTUM, q->initial_quantum) ||
  610. nla_put_u32(skb, TCA_FQ_RATE_ENABLE, q->rate_enable) ||
  611. nla_put_u32(skb, TCA_FQ_FLOW_DEFAULT_RATE, q->flow_default_rate) ||
  612. nla_put_u32(skb, TCA_FQ_FLOW_MAX_RATE, q->flow_max_rate) ||
  613. nla_put_u32(skb, TCA_FQ_BUCKETS_LOG, q->fq_trees_log))
  614. goto nla_put_failure;
  615. nla_nest_end(skb, opts);
  616. return skb->len;
  617. nla_put_failure:
  618. return -1;
  619. }
  620. static int fq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  621. {
  622. struct fq_sched_data *q = qdisc_priv(sch);
  623. u64 now = ktime_to_ns(ktime_get());
  624. struct tc_fq_qd_stats st = {
  625. .gc_flows = q->stat_gc_flows,
  626. .highprio_packets = q->stat_internal_packets,
  627. .tcp_retrans = q->stat_tcp_retrans,
  628. .throttled = q->stat_throttled,
  629. .flows_plimit = q->stat_flows_plimit,
  630. .pkts_too_long = q->stat_pkts_too_long,
  631. .allocation_errors = q->stat_allocation_errors,
  632. .flows = q->flows,
  633. .inactive_flows = q->inactive_flows,
  634. .throttled_flows = q->throttled_flows,
  635. .time_next_delayed_flow = q->time_next_delayed_flow - now,
  636. };
  637. return gnet_stats_copy_app(d, &st, sizeof(st));
  638. }
  639. static struct Qdisc_ops fq_qdisc_ops __read_mostly = {
  640. .id = "fq",
  641. .priv_size = sizeof(struct fq_sched_data),
  642. .enqueue = fq_enqueue,
  643. .dequeue = fq_dequeue,
  644. .peek = qdisc_peek_dequeued,
  645. .init = fq_init,
  646. .reset = fq_reset,
  647. .destroy = fq_destroy,
  648. .change = fq_change,
  649. .dump = fq_dump,
  650. .dump_stats = fq_dump_stats,
  651. .owner = THIS_MODULE,
  652. };
  653. static int __init fq_module_init(void)
  654. {
  655. int ret;
  656. fq_flow_cachep = kmem_cache_create("fq_flow_cache",
  657. sizeof(struct fq_flow),
  658. 0, 0, NULL);
  659. if (!fq_flow_cachep)
  660. return -ENOMEM;
  661. ret = register_qdisc(&fq_qdisc_ops);
  662. if (ret)
  663. kmem_cache_destroy(fq_flow_cachep);
  664. return ret;
  665. }
  666. static void __exit fq_module_exit(void)
  667. {
  668. unregister_qdisc(&fq_qdisc_ops);
  669. kmem_cache_destroy(fq_flow_cachep);
  670. }
  671. module_init(fq_module_init)
  672. module_exit(fq_module_exit)
  673. MODULE_AUTHOR("Eric Dumazet");
  674. MODULE_LICENSE("GPL");