sch_fq.c 22 KB

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