sch_fq.c 22 KB

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