sch_fq.c 21 KB

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