sch_fq.c 20 KB

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