sch_fq.c 21 KB

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