sch_fq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 <linux/prefetch.h>
  49. #include <net/netlink.h>
  50. #include <net/pkt_sched.h>
  51. #include <net/sock.h>
  52. #include <net/tcp_states.h>
  53. /*
  54. * Per flow structure, dynamically allocated
  55. */
  56. struct fq_flow {
  57. struct sk_buff *head; /* list of skbs for this flow : first skb */
  58. union {
  59. struct sk_buff *tail; /* last skb in the list */
  60. unsigned long age; /* jiffies when flow was emptied, for gc */
  61. };
  62. struct rb_node fq_node; /* anchor in fq_root[] trees */
  63. struct sock *sk;
  64. int qlen; /* number of packets in flow queue */
  65. int credit;
  66. u32 socket_hash; /* sk_hash */
  67. struct fq_flow *next; /* next pointer in RR lists, or &detached */
  68. struct rb_node rate_node; /* anchor in q->delayed tree */
  69. u64 time_next_packet;
  70. };
  71. struct fq_flow_head {
  72. struct fq_flow *first;
  73. struct fq_flow *last;
  74. };
  75. struct fq_sched_data {
  76. struct fq_flow_head new_flows;
  77. struct fq_flow_head old_flows;
  78. struct rb_root delayed; /* for rate limited flows */
  79. u64 time_next_delayed_flow;
  80. struct fq_flow internal; /* for non classified or high prio packets */
  81. u32 quantum;
  82. u32 initial_quantum;
  83. u32 flow_refill_delay;
  84. u32 flow_max_rate; /* optional max rate per flow */
  85. u32 flow_plimit; /* max packets per flow */
  86. struct rb_root *fq_root;
  87. u8 rate_enable;
  88. u8 fq_trees_log;
  89. u32 flows;
  90. u32 inactive_flows;
  91. u32 throttled_flows;
  92. u64 stat_gc_flows;
  93. u64 stat_internal_packets;
  94. u64 stat_tcp_retrans;
  95. u64 stat_throttled;
  96. u64 stat_flows_plimit;
  97. u64 stat_pkts_too_long;
  98. u64 stat_allocation_errors;
  99. struct qdisc_watchdog watchdog;
  100. };
  101. /* special value to mark a detached flow (not on old/new list) */
  102. static struct fq_flow detached, throttled;
  103. static void fq_flow_set_detached(struct fq_flow *f)
  104. {
  105. f->next = &detached;
  106. f->age = jiffies;
  107. }
  108. static bool fq_flow_is_detached(const struct fq_flow *f)
  109. {
  110. return f->next == &detached;
  111. }
  112. static void fq_flow_set_throttled(struct fq_sched_data *q, struct fq_flow *f)
  113. {
  114. struct rb_node **p = &q->delayed.rb_node, *parent = NULL;
  115. while (*p) {
  116. struct fq_flow *aux;
  117. parent = *p;
  118. aux = container_of(parent, struct fq_flow, rate_node);
  119. if (f->time_next_packet >= aux->time_next_packet)
  120. p = &parent->rb_right;
  121. else
  122. p = &parent->rb_left;
  123. }
  124. rb_link_node(&f->rate_node, parent, p);
  125. rb_insert_color(&f->rate_node, &q->delayed);
  126. q->throttled_flows++;
  127. q->stat_throttled++;
  128. f->next = &throttled;
  129. if (q->time_next_delayed_flow > f->time_next_packet)
  130. q->time_next_delayed_flow = f->time_next_packet;
  131. }
  132. static struct kmem_cache *fq_flow_cachep __read_mostly;
  133. static void fq_flow_add_tail(struct fq_flow_head *head, struct fq_flow *flow)
  134. {
  135. if (head->first)
  136. head->last->next = flow;
  137. else
  138. head->first = flow;
  139. head->last = flow;
  140. flow->next = NULL;
  141. }
  142. /* limit number of collected flows per round */
  143. #define FQ_GC_MAX 8
  144. #define FQ_GC_AGE (3*HZ)
  145. static bool fq_gc_candidate(const struct fq_flow *f)
  146. {
  147. return fq_flow_is_detached(f) &&
  148. time_after(jiffies, f->age + FQ_GC_AGE);
  149. }
  150. static void fq_gc(struct fq_sched_data *q,
  151. struct rb_root *root,
  152. struct sock *sk)
  153. {
  154. struct fq_flow *f, *tofree[FQ_GC_MAX];
  155. struct rb_node **p, *parent;
  156. int fcnt = 0;
  157. p = &root->rb_node;
  158. parent = NULL;
  159. while (*p) {
  160. parent = *p;
  161. f = container_of(parent, struct fq_flow, fq_node);
  162. if (f->sk == sk)
  163. break;
  164. if (fq_gc_candidate(f)) {
  165. tofree[fcnt++] = f;
  166. if (fcnt == FQ_GC_MAX)
  167. break;
  168. }
  169. if (f->sk > sk)
  170. p = &parent->rb_right;
  171. else
  172. p = &parent->rb_left;
  173. }
  174. q->flows -= fcnt;
  175. q->inactive_flows -= fcnt;
  176. q->stat_gc_flows += fcnt;
  177. while (fcnt) {
  178. struct fq_flow *f = tofree[--fcnt];
  179. rb_erase(&f->fq_node, root);
  180. kmem_cache_free(fq_flow_cachep, f);
  181. }
  182. }
  183. static struct fq_flow *fq_classify(struct sk_buff *skb, struct fq_sched_data *q)
  184. {
  185. struct rb_node **p, *parent;
  186. struct sock *sk = skb->sk;
  187. struct rb_root *root;
  188. struct fq_flow *f;
  189. /* warning: no starvation prevention... */
  190. if (unlikely((skb->priority & TC_PRIO_MAX) == TC_PRIO_CONTROL))
  191. return &q->internal;
  192. if (unlikely(!sk)) {
  193. /* By forcing low order bit to 1, we make sure to not
  194. * collide with a local flow (socket pointers are word aligned)
  195. */
  196. sk = (struct sock *)(skb_get_rxhash(skb) | 1L);
  197. }
  198. root = &q->fq_root[hash_32((u32)(long)sk, q->fq_trees_log)];
  199. if (q->flows >= (2U << q->fq_trees_log) &&
  200. q->inactive_flows > q->flows/2)
  201. fq_gc(q, root, sk);
  202. p = &root->rb_node;
  203. parent = NULL;
  204. while (*p) {
  205. parent = *p;
  206. f = container_of(parent, struct fq_flow, fq_node);
  207. if (f->sk == sk) {
  208. /* socket might have been reallocated, so check
  209. * if its sk_hash is the same.
  210. * It not, we need to refill credit with
  211. * initial quantum
  212. */
  213. if (unlikely(skb->sk &&
  214. f->socket_hash != sk->sk_hash)) {
  215. f->credit = q->initial_quantum;
  216. f->socket_hash = sk->sk_hash;
  217. f->time_next_packet = 0ULL;
  218. }
  219. return f;
  220. }
  221. if (f->sk > sk)
  222. p = &parent->rb_right;
  223. else
  224. p = &parent->rb_left;
  225. }
  226. f = kmem_cache_zalloc(fq_flow_cachep, GFP_ATOMIC | __GFP_NOWARN);
  227. if (unlikely(!f)) {
  228. q->stat_allocation_errors++;
  229. return &q->internal;
  230. }
  231. fq_flow_set_detached(f);
  232. f->sk = sk;
  233. if (skb->sk)
  234. f->socket_hash = sk->sk_hash;
  235. f->credit = q->initial_quantum;
  236. rb_link_node(&f->fq_node, parent, p);
  237. rb_insert_color(&f->fq_node, root);
  238. q->flows++;
  239. q->inactive_flows++;
  240. return f;
  241. }
  242. /* remove one skb from head of flow queue */
  243. static struct sk_buff *fq_dequeue_head(struct Qdisc *sch, struct fq_flow *flow)
  244. {
  245. struct sk_buff *skb = flow->head;
  246. if (skb) {
  247. flow->head = skb->next;
  248. skb->next = NULL;
  249. flow->qlen--;
  250. sch->qstats.backlog -= qdisc_pkt_len(skb);
  251. sch->q.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. if (skb_is_retransmit(skb))
  322. q->stat_tcp_retrans++;
  323. sch->qstats.backlog += qdisc_pkt_len(skb);
  324. if (fq_flow_is_detached(f)) {
  325. fq_flow_add_tail(&q->new_flows, f);
  326. if (time_after(jiffies, f->age + q->flow_refill_delay))
  327. f->credit = max_t(u32, f->credit, q->quantum);
  328. q->inactive_flows--;
  329. qdisc_unthrottled(sch);
  330. }
  331. /* Note: this overwrites f->age */
  332. flow_queue_add(f, skb);
  333. if (unlikely(f == &q->internal)) {
  334. q->stat_internal_packets++;
  335. qdisc_unthrottled(sch);
  336. }
  337. sch->q.qlen++;
  338. return NET_XMIT_SUCCESS;
  339. }
  340. static void fq_check_throttled(struct fq_sched_data *q, u64 now)
  341. {
  342. struct rb_node *p;
  343. if (q->time_next_delayed_flow > now)
  344. return;
  345. q->time_next_delayed_flow = ~0ULL;
  346. while ((p = rb_first(&q->delayed)) != NULL) {
  347. struct fq_flow *f = container_of(p, struct fq_flow, rate_node);
  348. if (f->time_next_packet > now) {
  349. q->time_next_delayed_flow = f->time_next_packet;
  350. break;
  351. }
  352. rb_erase(p, &q->delayed);
  353. q->throttled_flows--;
  354. fq_flow_add_tail(&q->old_flows, f);
  355. }
  356. }
  357. static struct sk_buff *fq_dequeue(struct Qdisc *sch)
  358. {
  359. struct fq_sched_data *q = qdisc_priv(sch);
  360. u64 now = ktime_to_ns(ktime_get());
  361. struct fq_flow_head *head;
  362. struct sk_buff *skb;
  363. struct fq_flow *f;
  364. u32 rate;
  365. skb = fq_dequeue_head(sch, &q->internal);
  366. if (skb)
  367. goto out;
  368. fq_check_throttled(q, now);
  369. begin:
  370. head = &q->new_flows;
  371. if (!head->first) {
  372. head = &q->old_flows;
  373. if (!head->first) {
  374. if (q->time_next_delayed_flow != ~0ULL)
  375. qdisc_watchdog_schedule_ns(&q->watchdog,
  376. q->time_next_delayed_flow);
  377. return NULL;
  378. }
  379. }
  380. f = head->first;
  381. if (f->credit <= 0) {
  382. f->credit += q->quantum;
  383. head->first = f->next;
  384. fq_flow_add_tail(&q->old_flows, f);
  385. goto begin;
  386. }
  387. if (unlikely(f->head && now < f->time_next_packet)) {
  388. head->first = f->next;
  389. fq_flow_set_throttled(q, f);
  390. goto begin;
  391. }
  392. skb = fq_dequeue_head(sch, f);
  393. if (!skb) {
  394. head->first = f->next;
  395. /* force a pass through old_flows to prevent starvation */
  396. if ((head == &q->new_flows) && q->old_flows.first) {
  397. fq_flow_add_tail(&q->old_flows, f);
  398. } else {
  399. fq_flow_set_detached(f);
  400. q->inactive_flows++;
  401. }
  402. goto begin;
  403. }
  404. prefetch(&skb->end);
  405. f->time_next_packet = now;
  406. f->credit -= qdisc_pkt_len(skb);
  407. if (f->credit > 0 || !q->rate_enable)
  408. goto out;
  409. rate = q->flow_max_rate;
  410. if (skb->sk && skb->sk->sk_state != TCP_TIME_WAIT)
  411. rate = min(skb->sk->sk_pacing_rate, rate);
  412. if (rate != ~0U) {
  413. u32 plen = max(qdisc_pkt_len(skb), q->quantum);
  414. u64 len = (u64)plen * NSEC_PER_SEC;
  415. if (likely(rate))
  416. do_div(len, rate);
  417. /* Since socket rate can change later,
  418. * clamp the delay to 125 ms.
  419. * TODO: maybe segment the too big skb, as in commit
  420. * e43ac79a4bc ("sch_tbf: segment too big GSO packets")
  421. */
  422. if (unlikely(len > 125 * NSEC_PER_MSEC)) {
  423. len = 125 * NSEC_PER_MSEC;
  424. q->stat_pkts_too_long++;
  425. }
  426. f->time_next_packet = now + len;
  427. }
  428. out:
  429. qdisc_bstats_update(sch, skb);
  430. qdisc_unthrottled(sch);
  431. return skb;
  432. }
  433. static void fq_reset(struct Qdisc *sch)
  434. {
  435. struct fq_sched_data *q = qdisc_priv(sch);
  436. struct rb_root *root;
  437. struct sk_buff *skb;
  438. struct rb_node *p;
  439. struct fq_flow *f;
  440. unsigned int idx;
  441. while ((skb = fq_dequeue_head(sch, &q->internal)) != NULL)
  442. kfree_skb(skb);
  443. if (!q->fq_root)
  444. return;
  445. for (idx = 0; idx < (1U << q->fq_trees_log); idx++) {
  446. root = &q->fq_root[idx];
  447. while ((p = rb_first(root)) != NULL) {
  448. f = container_of(p, struct fq_flow, fq_node);
  449. rb_erase(p, root);
  450. while ((skb = fq_dequeue_head(sch, f)) != NULL)
  451. kfree_skb(skb);
  452. kmem_cache_free(fq_flow_cachep, f);
  453. }
  454. }
  455. q->new_flows.first = NULL;
  456. q->old_flows.first = NULL;
  457. q->delayed = RB_ROOT;
  458. q->flows = 0;
  459. q->inactive_flows = 0;
  460. q->throttled_flows = 0;
  461. }
  462. static void fq_rehash(struct fq_sched_data *q,
  463. struct rb_root *old_array, u32 old_log,
  464. struct rb_root *new_array, u32 new_log)
  465. {
  466. struct rb_node *op, **np, *parent;
  467. struct rb_root *oroot, *nroot;
  468. struct fq_flow *of, *nf;
  469. int fcnt = 0;
  470. u32 idx;
  471. for (idx = 0; idx < (1U << old_log); idx++) {
  472. oroot = &old_array[idx];
  473. while ((op = rb_first(oroot)) != NULL) {
  474. rb_erase(op, oroot);
  475. of = container_of(op, struct fq_flow, fq_node);
  476. if (fq_gc_candidate(of)) {
  477. fcnt++;
  478. kmem_cache_free(fq_flow_cachep, of);
  479. continue;
  480. }
  481. nroot = &new_array[hash_32((u32)(long)of->sk, new_log)];
  482. np = &nroot->rb_node;
  483. parent = NULL;
  484. while (*np) {
  485. parent = *np;
  486. nf = container_of(parent, struct fq_flow, fq_node);
  487. BUG_ON(nf->sk == of->sk);
  488. if (nf->sk > of->sk)
  489. np = &parent->rb_right;
  490. else
  491. np = &parent->rb_left;
  492. }
  493. rb_link_node(&of->fq_node, parent, np);
  494. rb_insert_color(&of->fq_node, nroot);
  495. }
  496. }
  497. q->flows -= fcnt;
  498. q->inactive_flows -= fcnt;
  499. q->stat_gc_flows += fcnt;
  500. }
  501. static int fq_resize(struct fq_sched_data *q, u32 log)
  502. {
  503. struct rb_root *array;
  504. u32 idx;
  505. if (q->fq_root && log == q->fq_trees_log)
  506. return 0;
  507. array = kmalloc(sizeof(struct rb_root) << log, GFP_KERNEL);
  508. if (!array)
  509. return -ENOMEM;
  510. for (idx = 0; idx < (1U << log); idx++)
  511. array[idx] = RB_ROOT;
  512. if (q->fq_root) {
  513. fq_rehash(q, q->fq_root, q->fq_trees_log, array, log);
  514. kfree(q->fq_root);
  515. }
  516. q->fq_root = array;
  517. q->fq_trees_log = log;
  518. return 0;
  519. }
  520. static const struct nla_policy fq_policy[TCA_FQ_MAX + 1] = {
  521. [TCA_FQ_PLIMIT] = { .type = NLA_U32 },
  522. [TCA_FQ_FLOW_PLIMIT] = { .type = NLA_U32 },
  523. [TCA_FQ_QUANTUM] = { .type = NLA_U32 },
  524. [TCA_FQ_INITIAL_QUANTUM] = { .type = NLA_U32 },
  525. [TCA_FQ_RATE_ENABLE] = { .type = NLA_U32 },
  526. [TCA_FQ_FLOW_DEFAULT_RATE] = { .type = NLA_U32 },
  527. [TCA_FQ_FLOW_MAX_RATE] = { .type = NLA_U32 },
  528. [TCA_FQ_BUCKETS_LOG] = { .type = NLA_U32 },
  529. [TCA_FQ_FLOW_REFILL_DELAY] = { .type = NLA_U32 },
  530. };
  531. static int fq_change(struct Qdisc *sch, struct nlattr *opt)
  532. {
  533. struct fq_sched_data *q = qdisc_priv(sch);
  534. struct nlattr *tb[TCA_FQ_MAX + 1];
  535. int err, drop_count = 0;
  536. u32 fq_log;
  537. if (!opt)
  538. return -EINVAL;
  539. err = nla_parse_nested(tb, TCA_FQ_MAX, opt, fq_policy);
  540. if (err < 0)
  541. return err;
  542. sch_tree_lock(sch);
  543. fq_log = q->fq_trees_log;
  544. if (tb[TCA_FQ_BUCKETS_LOG]) {
  545. u32 nval = nla_get_u32(tb[TCA_FQ_BUCKETS_LOG]);
  546. if (nval >= 1 && nval <= ilog2(256*1024))
  547. fq_log = nval;
  548. else
  549. err = -EINVAL;
  550. }
  551. if (tb[TCA_FQ_PLIMIT])
  552. sch->limit = nla_get_u32(tb[TCA_FQ_PLIMIT]);
  553. if (tb[TCA_FQ_FLOW_PLIMIT])
  554. q->flow_plimit = nla_get_u32(tb[TCA_FQ_FLOW_PLIMIT]);
  555. if (tb[TCA_FQ_QUANTUM])
  556. q->quantum = nla_get_u32(tb[TCA_FQ_QUANTUM]);
  557. if (tb[TCA_FQ_INITIAL_QUANTUM])
  558. q->initial_quantum = nla_get_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
  559. if (tb[TCA_FQ_FLOW_DEFAULT_RATE])
  560. pr_warn_ratelimited("sch_fq: defrate %u ignored.\n",
  561. nla_get_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]));
  562. if (tb[TCA_FQ_FLOW_MAX_RATE])
  563. q->flow_max_rate = nla_get_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
  564. if (tb[TCA_FQ_RATE_ENABLE]) {
  565. u32 enable = nla_get_u32(tb[TCA_FQ_RATE_ENABLE]);
  566. if (enable <= 1)
  567. q->rate_enable = enable;
  568. else
  569. err = -EINVAL;
  570. }
  571. if (tb[TCA_FQ_FLOW_REFILL_DELAY]) {
  572. u32 usecs_delay = nla_get_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]) ;
  573. q->flow_refill_delay = usecs_to_jiffies(usecs_delay);
  574. }
  575. if (!err)
  576. err = fq_resize(q, fq_log);
  577. while (sch->q.qlen > sch->limit) {
  578. struct sk_buff *skb = fq_dequeue(sch);
  579. if (!skb)
  580. break;
  581. kfree_skb(skb);
  582. drop_count++;
  583. }
  584. qdisc_tree_decrease_qlen(sch, drop_count);
  585. sch_tree_unlock(sch);
  586. return err;
  587. }
  588. static void fq_destroy(struct Qdisc *sch)
  589. {
  590. struct fq_sched_data *q = qdisc_priv(sch);
  591. fq_reset(sch);
  592. kfree(q->fq_root);
  593. qdisc_watchdog_cancel(&q->watchdog);
  594. }
  595. static int fq_init(struct Qdisc *sch, struct nlattr *opt)
  596. {
  597. struct fq_sched_data *q = qdisc_priv(sch);
  598. int err;
  599. sch->limit = 10000;
  600. q->flow_plimit = 100;
  601. q->quantum = 2 * psched_mtu(qdisc_dev(sch));
  602. q->initial_quantum = 10 * psched_mtu(qdisc_dev(sch));
  603. q->flow_refill_delay = msecs_to_jiffies(40);
  604. q->flow_max_rate = ~0U;
  605. q->rate_enable = 1;
  606. q->new_flows.first = NULL;
  607. q->old_flows.first = NULL;
  608. q->delayed = RB_ROOT;
  609. q->fq_root = NULL;
  610. q->fq_trees_log = ilog2(1024);
  611. qdisc_watchdog_init(&q->watchdog, sch);
  612. if (opt)
  613. err = fq_change(sch, opt);
  614. else
  615. err = fq_resize(q, q->fq_trees_log);
  616. return err;
  617. }
  618. static int fq_dump(struct Qdisc *sch, struct sk_buff *skb)
  619. {
  620. struct fq_sched_data *q = qdisc_priv(sch);
  621. struct nlattr *opts;
  622. opts = nla_nest_start(skb, TCA_OPTIONS);
  623. if (opts == NULL)
  624. goto nla_put_failure;
  625. /* TCA_FQ_FLOW_DEFAULT_RATE is not used anymore */
  626. if (nla_put_u32(skb, TCA_FQ_PLIMIT, sch->limit) ||
  627. nla_put_u32(skb, TCA_FQ_FLOW_PLIMIT, q->flow_plimit) ||
  628. nla_put_u32(skb, TCA_FQ_QUANTUM, q->quantum) ||
  629. nla_put_u32(skb, TCA_FQ_INITIAL_QUANTUM, q->initial_quantum) ||
  630. nla_put_u32(skb, TCA_FQ_RATE_ENABLE, q->rate_enable) ||
  631. nla_put_u32(skb, TCA_FQ_FLOW_MAX_RATE, q->flow_max_rate) ||
  632. nla_put_u32(skb, TCA_FQ_FLOW_REFILL_DELAY,
  633. jiffies_to_usecs(q->flow_refill_delay)) ||
  634. nla_put_u32(skb, TCA_FQ_BUCKETS_LOG, q->fq_trees_log))
  635. goto nla_put_failure;
  636. nla_nest_end(skb, opts);
  637. return skb->len;
  638. nla_put_failure:
  639. return -1;
  640. }
  641. static int fq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  642. {
  643. struct fq_sched_data *q = qdisc_priv(sch);
  644. u64 now = ktime_to_ns(ktime_get());
  645. struct tc_fq_qd_stats st = {
  646. .gc_flows = q->stat_gc_flows,
  647. .highprio_packets = q->stat_internal_packets,
  648. .tcp_retrans = q->stat_tcp_retrans,
  649. .throttled = q->stat_throttled,
  650. .flows_plimit = q->stat_flows_plimit,
  651. .pkts_too_long = q->stat_pkts_too_long,
  652. .allocation_errors = q->stat_allocation_errors,
  653. .flows = q->flows,
  654. .inactive_flows = q->inactive_flows,
  655. .throttled_flows = q->throttled_flows,
  656. .time_next_delayed_flow = q->time_next_delayed_flow - now,
  657. };
  658. return gnet_stats_copy_app(d, &st, sizeof(st));
  659. }
  660. static struct Qdisc_ops fq_qdisc_ops __read_mostly = {
  661. .id = "fq",
  662. .priv_size = sizeof(struct fq_sched_data),
  663. .enqueue = fq_enqueue,
  664. .dequeue = fq_dequeue,
  665. .peek = qdisc_peek_dequeued,
  666. .init = fq_init,
  667. .reset = fq_reset,
  668. .destroy = fq_destroy,
  669. .change = fq_change,
  670. .dump = fq_dump,
  671. .dump_stats = fq_dump_stats,
  672. .owner = THIS_MODULE,
  673. };
  674. static int __init fq_module_init(void)
  675. {
  676. int ret;
  677. fq_flow_cachep = kmem_cache_create("fq_flow_cache",
  678. sizeof(struct fq_flow),
  679. 0, 0, NULL);
  680. if (!fq_flow_cachep)
  681. return -ENOMEM;
  682. ret = register_qdisc(&fq_qdisc_ops);
  683. if (ret)
  684. kmem_cache_destroy(fq_flow_cachep);
  685. return ret;
  686. }
  687. static void __exit fq_module_exit(void)
  688. {
  689. unregister_qdisc(&fq_qdisc_ops);
  690. kmem_cache_destroy(fq_flow_cachep);
  691. }
  692. module_init(fq_module_init)
  693. module_exit(fq_module_exit)
  694. MODULE_AUTHOR("Eric Dumazet");
  695. MODULE_LICENSE("GPL");