sch_fq.c 21 KB

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