sch_fq.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/sch_fq.c Fair Queue Packet Scheduler (per flow pacing)
  4. *
  5. * Copyright (C) 2013-2015 Eric Dumazet <edumazet@google.com>
  6. *
  7. * Meant to be mostly used for locally generated traffic :
  8. * Fast classification depends on skb->sk being set before reaching us.
  9. * If not, (router workload), we use rxhash as fallback, with 32 bits wide hash.
  10. * All packets belonging to a socket are considered as a 'flow'.
  11. *
  12. * Flows are dynamically allocated and stored in a hash table of RB trees
  13. * They are also part of one Round Robin 'queues' (new or old flows)
  14. *
  15. * Burst avoidance (aka pacing) capability :
  16. *
  17. * Transport (eg TCP) can set in sk->sk_pacing_rate a rate, enqueue a
  18. * bunch of packets, and this packet scheduler adds delay between
  19. * packets to respect rate limitation.
  20. *
  21. * enqueue() :
  22. * - lookup one RB tree (out of 1024 or more) to find the flow.
  23. * If non existent flow, create it, add it to the tree.
  24. * Add skb to the per flow list of skb (fifo).
  25. * - Use a special fifo for high prio packets
  26. *
  27. * dequeue() : serves flows in Round Robin
  28. * Note : When a flow becomes empty, we do not immediately remove it from
  29. * rb trees, for performance reasons (its expected to send additional packets,
  30. * or SLAB cache will reuse socket for another flow)
  31. */
  32. #include <linux/module.h>
  33. #include <linux/types.h>
  34. #include <linux/kernel.h>
  35. #include <linux/jiffies.h>
  36. #include <linux/string.h>
  37. #include <linux/in.h>
  38. #include <linux/errno.h>
  39. #include <linux/init.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/slab.h>
  42. #include <linux/rbtree.h>
  43. #include <linux/hash.h>
  44. #include <linux/prefetch.h>
  45. #include <linux/vmalloc.h>
  46. #include <net/netlink.h>
  47. #include <net/pkt_sched.h>
  48. #include <net/sock.h>
  49. #include <net/tcp_states.h>
  50. #include <net/tcp.h>
  51. struct fq_skb_cb {
  52. u64 time_to_send;
  53. };
  54. static inline struct fq_skb_cb *fq_skb_cb(struct sk_buff *skb)
  55. {
  56. qdisc_cb_private_validate(skb, sizeof(struct fq_skb_cb));
  57. return (struct fq_skb_cb *)qdisc_skb_cb(skb)->data;
  58. }
  59. /*
  60. * Per flow structure, dynamically allocated.
  61. * If packets have monotically increasing time_to_send, they are placed in O(1)
  62. * in linear list (head,tail), otherwise are placed in a rbtree (t_root).
  63. */
  64. struct fq_flow {
  65. /* First cache line : used in fq_gc(), fq_enqueue(), fq_dequeue() */
  66. struct rb_root t_root;
  67. struct sk_buff *head; /* list of skbs for this flow : first skb */
  68. union {
  69. struct sk_buff *tail; /* last skb in the list */
  70. unsigned long age; /* (jiffies | 1UL) when flow was emptied, for gc */
  71. };
  72. struct rb_node fq_node; /* anchor in fq_root[] trees */
  73. struct sock *sk;
  74. u32 socket_hash; /* sk_hash */
  75. int qlen; /* number of packets in flow queue */
  76. /* Second cache line, used in fq_dequeue() */
  77. int credit;
  78. /* 32bit hole on 64bit arches */
  79. struct fq_flow *next; /* next pointer in RR lists */
  80. struct rb_node rate_node; /* anchor in q->delayed tree */
  81. u64 time_next_packet;
  82. } ____cacheline_aligned_in_smp;
  83. struct fq_flow_head {
  84. struct fq_flow *first;
  85. struct fq_flow *last;
  86. };
  87. struct fq_sched_data {
  88. struct fq_flow_head new_flows;
  89. struct fq_flow_head old_flows;
  90. struct rb_root delayed; /* for rate limited flows */
  91. u64 time_next_delayed_flow;
  92. u64 ktime_cache; /* copy of last ktime_get_ns() */
  93. unsigned long unthrottle_latency_ns;
  94. struct fq_flow internal; /* for non classified or high prio packets */
  95. u32 quantum;
  96. u32 initial_quantum;
  97. u32 flow_refill_delay;
  98. u32 flow_plimit; /* max packets per flow */
  99. unsigned long flow_max_rate; /* optional max rate per flow */
  100. u64 ce_threshold;
  101. u64 horizon; /* horizon in ns */
  102. u32 orphan_mask; /* mask for orphaned skb */
  103. u32 low_rate_threshold;
  104. struct rb_root *fq_root;
  105. u8 rate_enable;
  106. u8 fq_trees_log;
  107. u8 horizon_drop;
  108. u32 flows;
  109. u32 inactive_flows;
  110. u32 throttled_flows;
  111. u64 stat_gc_flows;
  112. u64 stat_internal_packets;
  113. u64 stat_throttled;
  114. u64 stat_ce_mark;
  115. u64 stat_horizon_drops;
  116. u64 stat_horizon_caps;
  117. u64 stat_flows_plimit;
  118. u64 stat_pkts_too_long;
  119. u64 stat_allocation_errors;
  120. u32 timer_slack; /* hrtimer slack in ns */
  121. struct qdisc_watchdog watchdog;
  122. };
  123. /*
  124. * f->tail and f->age share the same location.
  125. * We can use the low order bit to differentiate if this location points
  126. * to a sk_buff or contains a jiffies value, if we force this value to be odd.
  127. * This assumes f->tail low order bit must be 0 since alignof(struct sk_buff) >= 2
  128. */
  129. static void fq_flow_set_detached(struct fq_flow *f)
  130. {
  131. f->age = jiffies | 1UL;
  132. }
  133. static bool fq_flow_is_detached(const struct fq_flow *f)
  134. {
  135. return !!(f->age & 1UL);
  136. }
  137. /* special value to mark a throttled flow (not on old/new list) */
  138. static struct fq_flow throttled;
  139. static bool fq_flow_is_throttled(const struct fq_flow *f)
  140. {
  141. return f->next == &throttled;
  142. }
  143. static void fq_flow_add_tail(struct fq_flow_head *head, struct fq_flow *flow)
  144. {
  145. if (head->first)
  146. head->last->next = flow;
  147. else
  148. head->first = flow;
  149. head->last = flow;
  150. flow->next = NULL;
  151. }
  152. static void fq_flow_unset_throttled(struct fq_sched_data *q, struct fq_flow *f)
  153. {
  154. rb_erase(&f->rate_node, &q->delayed);
  155. q->throttled_flows--;
  156. fq_flow_add_tail(&q->old_flows, f);
  157. }
  158. static void fq_flow_set_throttled(struct fq_sched_data *q, struct fq_flow *f)
  159. {
  160. struct rb_node **p = &q->delayed.rb_node, *parent = NULL;
  161. while (*p) {
  162. struct fq_flow *aux;
  163. parent = *p;
  164. aux = rb_entry(parent, struct fq_flow, rate_node);
  165. if (f->time_next_packet >= aux->time_next_packet)
  166. p = &parent->rb_right;
  167. else
  168. p = &parent->rb_left;
  169. }
  170. rb_link_node(&f->rate_node, parent, p);
  171. rb_insert_color(&f->rate_node, &q->delayed);
  172. q->throttled_flows++;
  173. q->stat_throttled++;
  174. f->next = &throttled;
  175. if (q->time_next_delayed_flow > f->time_next_packet)
  176. q->time_next_delayed_flow = f->time_next_packet;
  177. }
  178. static struct kmem_cache *fq_flow_cachep __read_mostly;
  179. /* limit number of collected flows per round */
  180. #define FQ_GC_MAX 8
  181. #define FQ_GC_AGE (3*HZ)
  182. static bool fq_gc_candidate(const struct fq_flow *f)
  183. {
  184. return fq_flow_is_detached(f) &&
  185. time_after(jiffies, f->age + FQ_GC_AGE);
  186. }
  187. static void fq_gc(struct fq_sched_data *q,
  188. struct rb_root *root,
  189. struct sock *sk)
  190. {
  191. struct rb_node **p, *parent;
  192. void *tofree[FQ_GC_MAX];
  193. struct fq_flow *f;
  194. int i, fcnt = 0;
  195. p = &root->rb_node;
  196. parent = NULL;
  197. while (*p) {
  198. parent = *p;
  199. f = rb_entry(parent, struct fq_flow, fq_node);
  200. if (f->sk == sk)
  201. break;
  202. if (fq_gc_candidate(f)) {
  203. tofree[fcnt++] = f;
  204. if (fcnt == FQ_GC_MAX)
  205. break;
  206. }
  207. if (f->sk > sk)
  208. p = &parent->rb_right;
  209. else
  210. p = &parent->rb_left;
  211. }
  212. if (!fcnt)
  213. return;
  214. for (i = fcnt; i > 0; ) {
  215. f = tofree[--i];
  216. rb_erase(&f->fq_node, root);
  217. }
  218. q->flows -= fcnt;
  219. q->inactive_flows -= fcnt;
  220. q->stat_gc_flows += fcnt;
  221. kmem_cache_free_bulk(fq_flow_cachep, fcnt, tofree);
  222. }
  223. static struct fq_flow *fq_classify(struct sk_buff *skb, struct fq_sched_data *q)
  224. {
  225. struct rb_node **p, *parent;
  226. struct sock *sk = skb->sk;
  227. struct rb_root *root;
  228. struct fq_flow *f;
  229. /* warning: no starvation prevention... */
  230. if (unlikely((skb->priority & TC_PRIO_MAX) == TC_PRIO_CONTROL))
  231. return &q->internal;
  232. /* SYNACK messages are attached to a TCP_NEW_SYN_RECV request socket
  233. * or a listener (SYNCOOKIE mode)
  234. * 1) request sockets are not full blown,
  235. * they do not contain sk_pacing_rate
  236. * 2) They are not part of a 'flow' yet
  237. * 3) We do not want to rate limit them (eg SYNFLOOD attack),
  238. * especially if the listener set SO_MAX_PACING_RATE
  239. * 4) We pretend they are orphaned
  240. */
  241. if (!sk || sk_listener(sk)) {
  242. unsigned long hash = skb_get_hash(skb) & q->orphan_mask;
  243. /* By forcing low order bit to 1, we make sure to not
  244. * collide with a local flow (socket pointers are word aligned)
  245. */
  246. sk = (struct sock *)((hash << 1) | 1UL);
  247. skb_orphan(skb);
  248. } else if (sk->sk_state == TCP_CLOSE) {
  249. unsigned long hash = skb_get_hash(skb) & q->orphan_mask;
  250. /*
  251. * Sockets in TCP_CLOSE are non connected.
  252. * Typical use case is UDP sockets, they can send packets
  253. * with sendto() to many different destinations.
  254. * We probably could use a generic bit advertising
  255. * non connected sockets, instead of sk_state == TCP_CLOSE,
  256. * if we care enough.
  257. */
  258. sk = (struct sock *)((hash << 1) | 1UL);
  259. }
  260. root = &q->fq_root[hash_ptr(sk, q->fq_trees_log)];
  261. if (q->flows >= (2U << q->fq_trees_log) &&
  262. q->inactive_flows > q->flows/2)
  263. fq_gc(q, root, sk);
  264. p = &root->rb_node;
  265. parent = NULL;
  266. while (*p) {
  267. parent = *p;
  268. f = rb_entry(parent, struct fq_flow, fq_node);
  269. if (f->sk == sk) {
  270. /* socket might have been reallocated, so check
  271. * if its sk_hash is the same.
  272. * It not, we need to refill credit with
  273. * initial quantum
  274. */
  275. if (unlikely(skb->sk == sk &&
  276. f->socket_hash != sk->sk_hash)) {
  277. f->credit = q->initial_quantum;
  278. f->socket_hash = sk->sk_hash;
  279. if (q->rate_enable)
  280. smp_store_release(&sk->sk_pacing_status,
  281. SK_PACING_FQ);
  282. if (fq_flow_is_throttled(f))
  283. fq_flow_unset_throttled(q, f);
  284. f->time_next_packet = 0ULL;
  285. }
  286. return f;
  287. }
  288. if (f->sk > sk)
  289. p = &parent->rb_right;
  290. else
  291. p = &parent->rb_left;
  292. }
  293. f = kmem_cache_zalloc(fq_flow_cachep, GFP_ATOMIC | __GFP_NOWARN);
  294. if (unlikely(!f)) {
  295. q->stat_allocation_errors++;
  296. return &q->internal;
  297. }
  298. /* f->t_root is already zeroed after kmem_cache_zalloc() */
  299. fq_flow_set_detached(f);
  300. f->sk = sk;
  301. if (skb->sk == sk) {
  302. f->socket_hash = sk->sk_hash;
  303. if (q->rate_enable)
  304. smp_store_release(&sk->sk_pacing_status,
  305. SK_PACING_FQ);
  306. }
  307. f->credit = q->initial_quantum;
  308. rb_link_node(&f->fq_node, parent, p);
  309. rb_insert_color(&f->fq_node, root);
  310. q->flows++;
  311. q->inactive_flows++;
  312. return f;
  313. }
  314. static struct sk_buff *fq_peek(struct fq_flow *flow)
  315. {
  316. struct sk_buff *skb = skb_rb_first(&flow->t_root);
  317. struct sk_buff *head = flow->head;
  318. if (!skb)
  319. return head;
  320. if (!head)
  321. return skb;
  322. if (fq_skb_cb(skb)->time_to_send < fq_skb_cb(head)->time_to_send)
  323. return skb;
  324. return head;
  325. }
  326. static void fq_erase_head(struct Qdisc *sch, struct fq_flow *flow,
  327. struct sk_buff *skb)
  328. {
  329. if (skb == flow->head) {
  330. flow->head = skb->next;
  331. } else {
  332. rb_erase(&skb->rbnode, &flow->t_root);
  333. skb->dev = qdisc_dev(sch);
  334. }
  335. }
  336. /* Remove one skb from flow queue.
  337. * This skb must be the return value of prior fq_peek().
  338. */
  339. static void fq_dequeue_skb(struct Qdisc *sch, struct fq_flow *flow,
  340. struct sk_buff *skb)
  341. {
  342. fq_erase_head(sch, flow, skb);
  343. skb_mark_not_on_list(skb);
  344. flow->qlen--;
  345. qdisc_qstats_backlog_dec(sch, skb);
  346. sch->q.qlen--;
  347. }
  348. static void flow_queue_add(struct fq_flow *flow, struct sk_buff *skb)
  349. {
  350. struct rb_node **p, *parent;
  351. struct sk_buff *head, *aux;
  352. head = flow->head;
  353. if (!head ||
  354. fq_skb_cb(skb)->time_to_send >= fq_skb_cb(flow->tail)->time_to_send) {
  355. if (!head)
  356. flow->head = skb;
  357. else
  358. flow->tail->next = skb;
  359. flow->tail = skb;
  360. skb->next = NULL;
  361. return;
  362. }
  363. p = &flow->t_root.rb_node;
  364. parent = NULL;
  365. while (*p) {
  366. parent = *p;
  367. aux = rb_to_skb(parent);
  368. if (fq_skb_cb(skb)->time_to_send >= fq_skb_cb(aux)->time_to_send)
  369. p = &parent->rb_right;
  370. else
  371. p = &parent->rb_left;
  372. }
  373. rb_link_node(&skb->rbnode, parent, p);
  374. rb_insert_color(&skb->rbnode, &flow->t_root);
  375. }
  376. static bool fq_packet_beyond_horizon(const struct sk_buff *skb,
  377. const struct fq_sched_data *q)
  378. {
  379. return unlikely((s64)skb->tstamp > (s64)(q->ktime_cache + q->horizon));
  380. }
  381. static int fq_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  382. struct sk_buff **to_free)
  383. {
  384. struct fq_sched_data *q = qdisc_priv(sch);
  385. struct fq_flow *f;
  386. if (unlikely(sch->q.qlen >= sch->limit))
  387. return qdisc_drop(skb, sch, to_free);
  388. if (!skb->tstamp) {
  389. fq_skb_cb(skb)->time_to_send = q->ktime_cache = ktime_get_ns();
  390. } else {
  391. /* Check if packet timestamp is too far in the future.
  392. * Try first if our cached value, to avoid ktime_get_ns()
  393. * cost in most cases.
  394. */
  395. if (fq_packet_beyond_horizon(skb, q)) {
  396. /* Refresh our cache and check another time */
  397. q->ktime_cache = ktime_get_ns();
  398. if (fq_packet_beyond_horizon(skb, q)) {
  399. if (q->horizon_drop) {
  400. q->stat_horizon_drops++;
  401. return qdisc_drop(skb, sch, to_free);
  402. }
  403. q->stat_horizon_caps++;
  404. skb->tstamp = q->ktime_cache + q->horizon;
  405. }
  406. }
  407. fq_skb_cb(skb)->time_to_send = skb->tstamp;
  408. }
  409. f = fq_classify(skb, q);
  410. if (unlikely(f->qlen >= q->flow_plimit && f != &q->internal)) {
  411. q->stat_flows_plimit++;
  412. return qdisc_drop(skb, sch, to_free);
  413. }
  414. f->qlen++;
  415. qdisc_qstats_backlog_inc(sch, skb);
  416. if (fq_flow_is_detached(f)) {
  417. fq_flow_add_tail(&q->new_flows, f);
  418. if (time_after(jiffies, f->age + q->flow_refill_delay))
  419. f->credit = max_t(u32, f->credit, q->quantum);
  420. q->inactive_flows--;
  421. }
  422. /* Note: this overwrites f->age */
  423. flow_queue_add(f, skb);
  424. if (unlikely(f == &q->internal)) {
  425. q->stat_internal_packets++;
  426. }
  427. sch->q.qlen++;
  428. return NET_XMIT_SUCCESS;
  429. }
  430. static void fq_check_throttled(struct fq_sched_data *q, u64 now)
  431. {
  432. unsigned long sample;
  433. struct rb_node *p;
  434. if (q->time_next_delayed_flow > now)
  435. return;
  436. /* Update unthrottle latency EWMA.
  437. * This is cheap and can help diagnosing timer/latency problems.
  438. */
  439. sample = (unsigned long)(now - q->time_next_delayed_flow);
  440. q->unthrottle_latency_ns -= q->unthrottle_latency_ns >> 3;
  441. q->unthrottle_latency_ns += sample >> 3;
  442. q->time_next_delayed_flow = ~0ULL;
  443. while ((p = rb_first(&q->delayed)) != NULL) {
  444. struct fq_flow *f = rb_entry(p, struct fq_flow, rate_node);
  445. if (f->time_next_packet > now) {
  446. q->time_next_delayed_flow = f->time_next_packet;
  447. break;
  448. }
  449. fq_flow_unset_throttled(q, f);
  450. }
  451. }
  452. static struct sk_buff *fq_dequeue(struct Qdisc *sch)
  453. {
  454. struct fq_sched_data *q = qdisc_priv(sch);
  455. struct fq_flow_head *head;
  456. struct sk_buff *skb;
  457. struct fq_flow *f;
  458. unsigned long rate;
  459. u32 plen;
  460. u64 now;
  461. if (!sch->q.qlen)
  462. return NULL;
  463. skb = fq_peek(&q->internal);
  464. if (unlikely(skb)) {
  465. fq_dequeue_skb(sch, &q->internal, skb);
  466. goto out;
  467. }
  468. q->ktime_cache = now = ktime_get_ns();
  469. fq_check_throttled(q, now);
  470. begin:
  471. head = &q->new_flows;
  472. if (!head->first) {
  473. head = &q->old_flows;
  474. if (!head->first) {
  475. if (q->time_next_delayed_flow != ~0ULL)
  476. qdisc_watchdog_schedule_range_ns(&q->watchdog,
  477. q->time_next_delayed_flow,
  478. q->timer_slack);
  479. return NULL;
  480. }
  481. }
  482. f = head->first;
  483. if (f->credit <= 0) {
  484. f->credit += q->quantum;
  485. head->first = f->next;
  486. fq_flow_add_tail(&q->old_flows, f);
  487. goto begin;
  488. }
  489. skb = fq_peek(f);
  490. if (skb) {
  491. u64 time_next_packet = max_t(u64, fq_skb_cb(skb)->time_to_send,
  492. f->time_next_packet);
  493. if (now < time_next_packet) {
  494. head->first = f->next;
  495. f->time_next_packet = time_next_packet;
  496. fq_flow_set_throttled(q, f);
  497. goto begin;
  498. }
  499. prefetch(&skb->end);
  500. if ((s64)(now - time_next_packet - q->ce_threshold) > 0) {
  501. INET_ECN_set_ce(skb);
  502. q->stat_ce_mark++;
  503. }
  504. fq_dequeue_skb(sch, f, skb);
  505. } else {
  506. head->first = f->next;
  507. /* force a pass through old_flows to prevent starvation */
  508. if ((head == &q->new_flows) && q->old_flows.first) {
  509. fq_flow_add_tail(&q->old_flows, f);
  510. } else {
  511. fq_flow_set_detached(f);
  512. q->inactive_flows++;
  513. }
  514. goto begin;
  515. }
  516. plen = qdisc_pkt_len(skb);
  517. f->credit -= plen;
  518. if (!q->rate_enable)
  519. goto out;
  520. rate = q->flow_max_rate;
  521. /* If EDT time was provided for this skb, we need to
  522. * update f->time_next_packet only if this qdisc enforces
  523. * a flow max rate.
  524. */
  525. if (!skb->tstamp) {
  526. if (skb->sk)
  527. rate = min(skb->sk->sk_pacing_rate, rate);
  528. if (rate <= q->low_rate_threshold) {
  529. f->credit = 0;
  530. } else {
  531. plen = max(plen, q->quantum);
  532. if (f->credit > 0)
  533. goto out;
  534. }
  535. }
  536. if (rate != ~0UL) {
  537. u64 len = (u64)plen * NSEC_PER_SEC;
  538. if (likely(rate))
  539. len = div64_ul(len, rate);
  540. /* Since socket rate can change later,
  541. * clamp the delay to 1 second.
  542. * Really, providers of too big packets should be fixed !
  543. */
  544. if (unlikely(len > NSEC_PER_SEC)) {
  545. len = NSEC_PER_SEC;
  546. q->stat_pkts_too_long++;
  547. }
  548. /* Account for schedule/timers drifts.
  549. * f->time_next_packet was set when prior packet was sent,
  550. * and current time (@now) can be too late by tens of us.
  551. */
  552. if (f->time_next_packet)
  553. len -= min(len/2, now - f->time_next_packet);
  554. f->time_next_packet = now + len;
  555. }
  556. out:
  557. qdisc_bstats_update(sch, skb);
  558. return skb;
  559. }
  560. static void fq_flow_purge(struct fq_flow *flow)
  561. {
  562. struct rb_node *p = rb_first(&flow->t_root);
  563. while (p) {
  564. struct sk_buff *skb = rb_to_skb(p);
  565. p = rb_next(p);
  566. rb_erase(&skb->rbnode, &flow->t_root);
  567. rtnl_kfree_skbs(skb, skb);
  568. }
  569. rtnl_kfree_skbs(flow->head, flow->tail);
  570. flow->head = NULL;
  571. flow->qlen = 0;
  572. }
  573. static void fq_reset(struct Qdisc *sch)
  574. {
  575. struct fq_sched_data *q = qdisc_priv(sch);
  576. struct rb_root *root;
  577. struct rb_node *p;
  578. struct fq_flow *f;
  579. unsigned int idx;
  580. sch->q.qlen = 0;
  581. sch->qstats.backlog = 0;
  582. fq_flow_purge(&q->internal);
  583. if (!q->fq_root)
  584. return;
  585. for (idx = 0; idx < (1U << q->fq_trees_log); idx++) {
  586. root = &q->fq_root[idx];
  587. while ((p = rb_first(root)) != NULL) {
  588. f = rb_entry(p, struct fq_flow, fq_node);
  589. rb_erase(p, root);
  590. fq_flow_purge(f);
  591. kmem_cache_free(fq_flow_cachep, f);
  592. }
  593. }
  594. q->new_flows.first = NULL;
  595. q->old_flows.first = NULL;
  596. q->delayed = RB_ROOT;
  597. q->flows = 0;
  598. q->inactive_flows = 0;
  599. q->throttled_flows = 0;
  600. }
  601. static void fq_rehash(struct fq_sched_data *q,
  602. struct rb_root *old_array, u32 old_log,
  603. struct rb_root *new_array, u32 new_log)
  604. {
  605. struct rb_node *op, **np, *parent;
  606. struct rb_root *oroot, *nroot;
  607. struct fq_flow *of, *nf;
  608. int fcnt = 0;
  609. u32 idx;
  610. for (idx = 0; idx < (1U << old_log); idx++) {
  611. oroot = &old_array[idx];
  612. while ((op = rb_first(oroot)) != NULL) {
  613. rb_erase(op, oroot);
  614. of = rb_entry(op, struct fq_flow, fq_node);
  615. if (fq_gc_candidate(of)) {
  616. fcnt++;
  617. kmem_cache_free(fq_flow_cachep, of);
  618. continue;
  619. }
  620. nroot = &new_array[hash_ptr(of->sk, new_log)];
  621. np = &nroot->rb_node;
  622. parent = NULL;
  623. while (*np) {
  624. parent = *np;
  625. nf = rb_entry(parent, struct fq_flow, fq_node);
  626. BUG_ON(nf->sk == of->sk);
  627. if (nf->sk > of->sk)
  628. np = &parent->rb_right;
  629. else
  630. np = &parent->rb_left;
  631. }
  632. rb_link_node(&of->fq_node, parent, np);
  633. rb_insert_color(&of->fq_node, nroot);
  634. }
  635. }
  636. q->flows -= fcnt;
  637. q->inactive_flows -= fcnt;
  638. q->stat_gc_flows += fcnt;
  639. }
  640. static void fq_free(void *addr)
  641. {
  642. kvfree(addr);
  643. }
  644. static int fq_resize(struct Qdisc *sch, u32 log)
  645. {
  646. struct fq_sched_data *q = qdisc_priv(sch);
  647. struct rb_root *array;
  648. void *old_fq_root;
  649. u32 idx;
  650. if (q->fq_root && log == q->fq_trees_log)
  651. return 0;
  652. /* If XPS was setup, we can allocate memory on right NUMA node */
  653. array = kvmalloc_node(sizeof(struct rb_root) << log, GFP_KERNEL | __GFP_RETRY_MAYFAIL,
  654. netdev_queue_numa_node_read(sch->dev_queue));
  655. if (!array)
  656. return -ENOMEM;
  657. for (idx = 0; idx < (1U << log); idx++)
  658. array[idx] = RB_ROOT;
  659. sch_tree_lock(sch);
  660. old_fq_root = q->fq_root;
  661. if (old_fq_root)
  662. fq_rehash(q, old_fq_root, q->fq_trees_log, array, log);
  663. q->fq_root = array;
  664. q->fq_trees_log = log;
  665. sch_tree_unlock(sch);
  666. fq_free(old_fq_root);
  667. return 0;
  668. }
  669. static const struct nla_policy fq_policy[TCA_FQ_MAX + 1] = {
  670. [TCA_FQ_UNSPEC] = { .strict_start_type = TCA_FQ_TIMER_SLACK },
  671. [TCA_FQ_PLIMIT] = { .type = NLA_U32 },
  672. [TCA_FQ_FLOW_PLIMIT] = { .type = NLA_U32 },
  673. [TCA_FQ_QUANTUM] = { .type = NLA_U32 },
  674. [TCA_FQ_INITIAL_QUANTUM] = { .type = NLA_U32 },
  675. [TCA_FQ_RATE_ENABLE] = { .type = NLA_U32 },
  676. [TCA_FQ_FLOW_DEFAULT_RATE] = { .type = NLA_U32 },
  677. [TCA_FQ_FLOW_MAX_RATE] = { .type = NLA_U32 },
  678. [TCA_FQ_BUCKETS_LOG] = { .type = NLA_U32 },
  679. [TCA_FQ_FLOW_REFILL_DELAY] = { .type = NLA_U32 },
  680. [TCA_FQ_ORPHAN_MASK] = { .type = NLA_U32 },
  681. [TCA_FQ_LOW_RATE_THRESHOLD] = { .type = NLA_U32 },
  682. [TCA_FQ_CE_THRESHOLD] = { .type = NLA_U32 },
  683. [TCA_FQ_TIMER_SLACK] = { .type = NLA_U32 },
  684. [TCA_FQ_HORIZON] = { .type = NLA_U32 },
  685. [TCA_FQ_HORIZON_DROP] = { .type = NLA_U8 },
  686. };
  687. static int fq_change(struct Qdisc *sch, struct nlattr *opt,
  688. struct netlink_ext_ack *extack)
  689. {
  690. struct fq_sched_data *q = qdisc_priv(sch);
  691. struct nlattr *tb[TCA_FQ_MAX + 1];
  692. int err, drop_count = 0;
  693. unsigned drop_len = 0;
  694. u32 fq_log;
  695. if (!opt)
  696. return -EINVAL;
  697. err = nla_parse_nested_deprecated(tb, TCA_FQ_MAX, opt, fq_policy,
  698. NULL);
  699. if (err < 0)
  700. return err;
  701. sch_tree_lock(sch);
  702. fq_log = q->fq_trees_log;
  703. if (tb[TCA_FQ_BUCKETS_LOG]) {
  704. u32 nval = nla_get_u32(tb[TCA_FQ_BUCKETS_LOG]);
  705. if (nval >= 1 && nval <= ilog2(256*1024))
  706. fq_log = nval;
  707. else
  708. err = -EINVAL;
  709. }
  710. if (tb[TCA_FQ_PLIMIT])
  711. sch->limit = nla_get_u32(tb[TCA_FQ_PLIMIT]);
  712. if (tb[TCA_FQ_FLOW_PLIMIT])
  713. q->flow_plimit = nla_get_u32(tb[TCA_FQ_FLOW_PLIMIT]);
  714. if (tb[TCA_FQ_QUANTUM]) {
  715. u32 quantum = nla_get_u32(tb[TCA_FQ_QUANTUM]);
  716. if (quantum > 0 && quantum <= (1 << 20)) {
  717. q->quantum = quantum;
  718. } else {
  719. NL_SET_ERR_MSG_MOD(extack, "invalid quantum");
  720. err = -EINVAL;
  721. }
  722. }
  723. if (tb[TCA_FQ_INITIAL_QUANTUM])
  724. q->initial_quantum = nla_get_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
  725. if (tb[TCA_FQ_FLOW_DEFAULT_RATE])
  726. pr_warn_ratelimited("sch_fq: defrate %u ignored.\n",
  727. nla_get_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]));
  728. if (tb[TCA_FQ_FLOW_MAX_RATE]) {
  729. u32 rate = nla_get_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
  730. q->flow_max_rate = (rate == ~0U) ? ~0UL : rate;
  731. }
  732. if (tb[TCA_FQ_LOW_RATE_THRESHOLD])
  733. q->low_rate_threshold =
  734. nla_get_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]);
  735. if (tb[TCA_FQ_RATE_ENABLE]) {
  736. u32 enable = nla_get_u32(tb[TCA_FQ_RATE_ENABLE]);
  737. if (enable <= 1)
  738. q->rate_enable = enable;
  739. else
  740. err = -EINVAL;
  741. }
  742. if (tb[TCA_FQ_FLOW_REFILL_DELAY]) {
  743. u32 usecs_delay = nla_get_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]) ;
  744. q->flow_refill_delay = usecs_to_jiffies(usecs_delay);
  745. }
  746. if (tb[TCA_FQ_ORPHAN_MASK])
  747. q->orphan_mask = nla_get_u32(tb[TCA_FQ_ORPHAN_MASK]);
  748. if (tb[TCA_FQ_CE_THRESHOLD])
  749. q->ce_threshold = (u64)NSEC_PER_USEC *
  750. nla_get_u32(tb[TCA_FQ_CE_THRESHOLD]);
  751. if (tb[TCA_FQ_TIMER_SLACK])
  752. q->timer_slack = nla_get_u32(tb[TCA_FQ_TIMER_SLACK]);
  753. if (tb[TCA_FQ_HORIZON])
  754. q->horizon = (u64)NSEC_PER_USEC *
  755. nla_get_u32(tb[TCA_FQ_HORIZON]);
  756. if (tb[TCA_FQ_HORIZON_DROP])
  757. q->horizon_drop = nla_get_u8(tb[TCA_FQ_HORIZON_DROP]);
  758. if (!err) {
  759. sch_tree_unlock(sch);
  760. err = fq_resize(sch, fq_log);
  761. sch_tree_lock(sch);
  762. }
  763. while (sch->q.qlen > sch->limit) {
  764. struct sk_buff *skb = fq_dequeue(sch);
  765. if (!skb)
  766. break;
  767. drop_len += qdisc_pkt_len(skb);
  768. rtnl_kfree_skbs(skb, skb);
  769. drop_count++;
  770. }
  771. qdisc_tree_reduce_backlog(sch, drop_count, drop_len);
  772. sch_tree_unlock(sch);
  773. return err;
  774. }
  775. static void fq_destroy(struct Qdisc *sch)
  776. {
  777. struct fq_sched_data *q = qdisc_priv(sch);
  778. fq_reset(sch);
  779. fq_free(q->fq_root);
  780. qdisc_watchdog_cancel(&q->watchdog);
  781. }
  782. static int fq_init(struct Qdisc *sch, struct nlattr *opt,
  783. struct netlink_ext_ack *extack)
  784. {
  785. struct fq_sched_data *q = qdisc_priv(sch);
  786. int err;
  787. sch->limit = 10000;
  788. q->flow_plimit = 100;
  789. q->quantum = 2 * psched_mtu(qdisc_dev(sch));
  790. q->initial_quantum = 10 * psched_mtu(qdisc_dev(sch));
  791. q->flow_refill_delay = msecs_to_jiffies(40);
  792. q->flow_max_rate = ~0UL;
  793. q->time_next_delayed_flow = ~0ULL;
  794. q->rate_enable = 1;
  795. q->new_flows.first = NULL;
  796. q->old_flows.first = NULL;
  797. q->delayed = RB_ROOT;
  798. q->fq_root = NULL;
  799. q->fq_trees_log = ilog2(1024);
  800. q->orphan_mask = 1024 - 1;
  801. q->low_rate_threshold = 550000 / 8;
  802. q->timer_slack = 10 * NSEC_PER_USEC; /* 10 usec of hrtimer slack */
  803. q->horizon = 10ULL * NSEC_PER_SEC; /* 10 seconds */
  804. q->horizon_drop = 1; /* by default, drop packets beyond horizon */
  805. /* Default ce_threshold of 4294 seconds */
  806. q->ce_threshold = (u64)NSEC_PER_USEC * ~0U;
  807. qdisc_watchdog_init_clockid(&q->watchdog, sch, CLOCK_MONOTONIC);
  808. if (opt)
  809. err = fq_change(sch, opt, extack);
  810. else
  811. err = fq_resize(sch, q->fq_trees_log);
  812. return err;
  813. }
  814. static int fq_dump(struct Qdisc *sch, struct sk_buff *skb)
  815. {
  816. struct fq_sched_data *q = qdisc_priv(sch);
  817. u64 ce_threshold = q->ce_threshold;
  818. u64 horizon = q->horizon;
  819. struct nlattr *opts;
  820. opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
  821. if (opts == NULL)
  822. goto nla_put_failure;
  823. /* TCA_FQ_FLOW_DEFAULT_RATE is not used anymore */
  824. do_div(ce_threshold, NSEC_PER_USEC);
  825. do_div(horizon, NSEC_PER_USEC);
  826. if (nla_put_u32(skb, TCA_FQ_PLIMIT, sch->limit) ||
  827. nla_put_u32(skb, TCA_FQ_FLOW_PLIMIT, q->flow_plimit) ||
  828. nla_put_u32(skb, TCA_FQ_QUANTUM, q->quantum) ||
  829. nla_put_u32(skb, TCA_FQ_INITIAL_QUANTUM, q->initial_quantum) ||
  830. nla_put_u32(skb, TCA_FQ_RATE_ENABLE, q->rate_enable) ||
  831. nla_put_u32(skb, TCA_FQ_FLOW_MAX_RATE,
  832. min_t(unsigned long, q->flow_max_rate, ~0U)) ||
  833. nla_put_u32(skb, TCA_FQ_FLOW_REFILL_DELAY,
  834. jiffies_to_usecs(q->flow_refill_delay)) ||
  835. nla_put_u32(skb, TCA_FQ_ORPHAN_MASK, q->orphan_mask) ||
  836. nla_put_u32(skb, TCA_FQ_LOW_RATE_THRESHOLD,
  837. q->low_rate_threshold) ||
  838. nla_put_u32(skb, TCA_FQ_CE_THRESHOLD, (u32)ce_threshold) ||
  839. nla_put_u32(skb, TCA_FQ_BUCKETS_LOG, q->fq_trees_log) ||
  840. nla_put_u32(skb, TCA_FQ_TIMER_SLACK, q->timer_slack) ||
  841. nla_put_u32(skb, TCA_FQ_HORIZON, (u32)horizon) ||
  842. nla_put_u8(skb, TCA_FQ_HORIZON_DROP, q->horizon_drop))
  843. goto nla_put_failure;
  844. return nla_nest_end(skb, opts);
  845. nla_put_failure:
  846. return -1;
  847. }
  848. static int fq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  849. {
  850. struct fq_sched_data *q = qdisc_priv(sch);
  851. struct tc_fq_qd_stats st;
  852. sch_tree_lock(sch);
  853. st.gc_flows = q->stat_gc_flows;
  854. st.highprio_packets = q->stat_internal_packets;
  855. st.tcp_retrans = 0;
  856. st.throttled = q->stat_throttled;
  857. st.flows_plimit = q->stat_flows_plimit;
  858. st.pkts_too_long = q->stat_pkts_too_long;
  859. st.allocation_errors = q->stat_allocation_errors;
  860. st.time_next_delayed_flow = q->time_next_delayed_flow + q->timer_slack -
  861. ktime_get_ns();
  862. st.flows = q->flows;
  863. st.inactive_flows = q->inactive_flows;
  864. st.throttled_flows = q->throttled_flows;
  865. st.unthrottle_latency_ns = min_t(unsigned long,
  866. q->unthrottle_latency_ns, ~0U);
  867. st.ce_mark = q->stat_ce_mark;
  868. st.horizon_drops = q->stat_horizon_drops;
  869. st.horizon_caps = q->stat_horizon_caps;
  870. sch_tree_unlock(sch);
  871. return gnet_stats_copy_app(d, &st, sizeof(st));
  872. }
  873. static struct Qdisc_ops fq_qdisc_ops __read_mostly = {
  874. .id = "fq",
  875. .priv_size = sizeof(struct fq_sched_data),
  876. .enqueue = fq_enqueue,
  877. .dequeue = fq_dequeue,
  878. .peek = qdisc_peek_dequeued,
  879. .init = fq_init,
  880. .reset = fq_reset,
  881. .destroy = fq_destroy,
  882. .change = fq_change,
  883. .dump = fq_dump,
  884. .dump_stats = fq_dump_stats,
  885. .owner = THIS_MODULE,
  886. };
  887. static int __init fq_module_init(void)
  888. {
  889. int ret;
  890. fq_flow_cachep = kmem_cache_create("fq_flow_cache",
  891. sizeof(struct fq_flow),
  892. 0, 0, NULL);
  893. if (!fq_flow_cachep)
  894. return -ENOMEM;
  895. ret = register_qdisc(&fq_qdisc_ops);
  896. if (ret)
  897. kmem_cache_destroy(fq_flow_cachep);
  898. return ret;
  899. }
  900. static void __exit fq_module_exit(void)
  901. {
  902. unregister_qdisc(&fq_qdisc_ops);
  903. kmem_cache_destroy(fq_flow_cachep);
  904. }
  905. module_init(fq_module_init)
  906. module_exit(fq_module_exit)
  907. MODULE_AUTHOR("Eric Dumazet");
  908. MODULE_LICENSE("GPL");
  909. MODULE_DESCRIPTION("Fair Queue Packet Scheduler");