sch_fq.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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-2023 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. u8 band;
  54. };
  55. static inline struct fq_skb_cb *fq_skb_cb(struct sk_buff *skb)
  56. {
  57. qdisc_cb_private_validate(skb, sizeof(struct fq_skb_cb));
  58. return (struct fq_skb_cb *)qdisc_skb_cb(skb)->data;
  59. }
  60. /*
  61. * Per flow structure, dynamically allocated.
  62. * If packets have monotically increasing time_to_send, they are placed in O(1)
  63. * in linear list (head,tail), otherwise are placed in a rbtree (t_root).
  64. */
  65. struct fq_flow {
  66. /* First cache line : used in fq_gc(), fq_enqueue(), fq_dequeue() */
  67. struct rb_root t_root;
  68. struct sk_buff *head; /* list of skbs for this flow : first skb */
  69. union {
  70. struct sk_buff *tail; /* last skb in the list */
  71. unsigned long age; /* (jiffies | 1UL) when flow was emptied, for gc */
  72. };
  73. union {
  74. struct rb_node fq_node; /* anchor in fq_root[] trees */
  75. /* Following field is only used for q->internal,
  76. * because q->internal is not hashed in fq_root[]
  77. */
  78. u64 stat_fastpath_packets;
  79. };
  80. struct sock *sk;
  81. u32 socket_hash; /* sk_hash */
  82. int qlen; /* number of packets in flow queue */
  83. /* Second cache line */
  84. int credit;
  85. int band;
  86. struct fq_flow *next; /* next pointer in RR lists */
  87. struct rb_node rate_node; /* anchor in q->delayed tree */
  88. u64 time_next_packet;
  89. };
  90. struct fq_flow_head {
  91. struct fq_flow *first;
  92. struct fq_flow *last;
  93. };
  94. struct fq_perband_flows {
  95. struct fq_flow_head new_flows;
  96. struct fq_flow_head old_flows;
  97. int credit;
  98. int quantum; /* based on band nr : 576KB, 192KB, 64KB */
  99. };
  100. #define FQ_PRIO2BAND_CRUMB_SIZE ((TC_PRIO_MAX + 1) >> 2)
  101. struct fq_sched_data {
  102. /* Read mostly cache line */
  103. u64 offload_horizon;
  104. u32 quantum;
  105. u32 initial_quantum;
  106. u32 flow_refill_delay;
  107. u32 flow_plimit; /* max packets per flow */
  108. unsigned long flow_max_rate; /* optional max rate per flow */
  109. u64 ce_threshold;
  110. u64 horizon; /* horizon in ns */
  111. u32 orphan_mask; /* mask for orphaned skb */
  112. u32 low_rate_threshold;
  113. struct rb_root *fq_root;
  114. u8 rate_enable;
  115. u8 fq_trees_log;
  116. u8 horizon_drop;
  117. u8 prio2band[FQ_PRIO2BAND_CRUMB_SIZE];
  118. u32 timer_slack; /* hrtimer slack in ns */
  119. /* Read/Write fields. */
  120. unsigned int band_nr; /* band being serviced in fq_dequeue() */
  121. struct fq_perband_flows band_flows[FQ_BANDS];
  122. struct fq_flow internal; /* fastpath queue. */
  123. struct rb_root delayed; /* for rate limited flows */
  124. u64 time_next_delayed_flow;
  125. unsigned long unthrottle_latency_ns;
  126. u32 band_pkt_count[FQ_BANDS];
  127. u32 flows;
  128. u32 inactive_flows; /* Flows with no packet to send. */
  129. u32 throttled_flows;
  130. u64 stat_throttled;
  131. struct qdisc_watchdog watchdog;
  132. u64 stat_gc_flows;
  133. /* Seldom used fields. */
  134. u64 stat_band_drops[FQ_BANDS];
  135. u64 stat_ce_mark;
  136. u64 stat_horizon_drops;
  137. u64 stat_horizon_caps;
  138. u64 stat_flows_plimit;
  139. u64 stat_pkts_too_long;
  140. u64 stat_allocation_errors;
  141. };
  142. /* return the i-th 2-bit value ("crumb") */
  143. static u8 fq_prio2band(const u8 *prio2band, unsigned int prio)
  144. {
  145. return (READ_ONCE(prio2band[prio / 4]) >> (2 * (prio & 0x3))) & 0x3;
  146. }
  147. /*
  148. * f->tail and f->age share the same location.
  149. * We can use the low order bit to differentiate if this location points
  150. * to a sk_buff or contains a jiffies value, if we force this value to be odd.
  151. * This assumes f->tail low order bit must be 0 since alignof(struct sk_buff) >= 2
  152. */
  153. static void fq_flow_set_detached(struct fq_flow *f)
  154. {
  155. f->age = jiffies | 1UL;
  156. }
  157. static bool fq_flow_is_detached(const struct fq_flow *f)
  158. {
  159. return !!(f->age & 1UL);
  160. }
  161. /* special value to mark a throttled flow (not on old/new list) */
  162. static struct fq_flow throttled;
  163. static bool fq_flow_is_throttled(const struct fq_flow *f)
  164. {
  165. return f->next == &throttled;
  166. }
  167. enum new_flow {
  168. NEW_FLOW,
  169. OLD_FLOW
  170. };
  171. static void fq_flow_add_tail(struct fq_sched_data *q, struct fq_flow *flow,
  172. enum new_flow list_sel)
  173. {
  174. struct fq_perband_flows *pband = &q->band_flows[flow->band];
  175. struct fq_flow_head *head = (list_sel == NEW_FLOW) ?
  176. &pband->new_flows :
  177. &pband->old_flows;
  178. if (head->first)
  179. head->last->next = flow;
  180. else
  181. head->first = flow;
  182. head->last = flow;
  183. flow->next = NULL;
  184. }
  185. static void fq_flow_unset_throttled(struct fq_sched_data *q, struct fq_flow *f)
  186. {
  187. rb_erase(&f->rate_node, &q->delayed);
  188. q->throttled_flows--;
  189. fq_flow_add_tail(q, f, OLD_FLOW);
  190. }
  191. static void fq_flow_set_throttled(struct fq_sched_data *q, struct fq_flow *f)
  192. {
  193. struct rb_node **p = &q->delayed.rb_node, *parent = NULL;
  194. while (*p) {
  195. struct fq_flow *aux;
  196. parent = *p;
  197. aux = rb_entry(parent, struct fq_flow, rate_node);
  198. if (f->time_next_packet >= aux->time_next_packet)
  199. p = &parent->rb_right;
  200. else
  201. p = &parent->rb_left;
  202. }
  203. rb_link_node(&f->rate_node, parent, p);
  204. rb_insert_color(&f->rate_node, &q->delayed);
  205. q->throttled_flows++;
  206. q->stat_throttled++;
  207. f->next = &throttled;
  208. if (q->time_next_delayed_flow > f->time_next_packet)
  209. q->time_next_delayed_flow = f->time_next_packet;
  210. }
  211. static struct kmem_cache *fq_flow_cachep __read_mostly;
  212. #define FQ_GC_AGE (3*HZ)
  213. static bool fq_gc_candidate(const struct fq_flow *f)
  214. {
  215. return fq_flow_is_detached(f) &&
  216. time_after(jiffies, f->age + FQ_GC_AGE);
  217. }
  218. static void fq_gc(struct fq_sched_data *q,
  219. struct rb_root *root,
  220. struct sock *sk)
  221. {
  222. struct fq_flow *f, *tofree = NULL;
  223. struct rb_node **p, *parent;
  224. int fcnt;
  225. p = &root->rb_node;
  226. parent = NULL;
  227. while (*p) {
  228. parent = *p;
  229. f = rb_entry(parent, struct fq_flow, fq_node);
  230. if (f->sk == sk)
  231. break;
  232. if (fq_gc_candidate(f)) {
  233. f->next = tofree;
  234. tofree = f;
  235. }
  236. if (f->sk > sk)
  237. p = &parent->rb_right;
  238. else
  239. p = &parent->rb_left;
  240. }
  241. if (!tofree)
  242. return;
  243. fcnt = 0;
  244. while (tofree) {
  245. f = tofree;
  246. tofree = f->next;
  247. rb_erase(&f->fq_node, root);
  248. kmem_cache_free(fq_flow_cachep, f);
  249. fcnt++;
  250. }
  251. q->flows -= fcnt;
  252. q->inactive_flows -= fcnt;
  253. q->stat_gc_flows += fcnt;
  254. }
  255. /* Fast path can be used if :
  256. * 1) Packet tstamp is in the past, or within the pacing offload horizon.
  257. * 2) FQ qlen == 0 OR
  258. * (no flow is currently eligible for transmit,
  259. * AND fast path queue has less than 8 packets)
  260. * 3) No SO_MAX_PACING_RATE on the socket (if any).
  261. * 4) No @maxrate attribute on this qdisc,
  262. *
  263. * FQ can not use generic TCQ_F_CAN_BYPASS infrastructure.
  264. */
  265. static bool fq_fastpath_check(const struct Qdisc *sch, struct sk_buff *skb,
  266. u64 now)
  267. {
  268. const struct fq_sched_data *q = qdisc_priv(sch);
  269. const struct sock *sk;
  270. if (fq_skb_cb(skb)->time_to_send > now + q->offload_horizon)
  271. return false;
  272. if (sch->q.qlen != 0) {
  273. /* Even if some packets are stored in this qdisc,
  274. * we can still enable fast path if all of them are
  275. * scheduled in the future (ie no flows are eligible)
  276. * or in the fast path queue.
  277. */
  278. if (q->flows != q->inactive_flows + q->throttled_flows)
  279. return false;
  280. /* Do not allow fast path queue to explode, we want Fair Queue mode
  281. * under pressure.
  282. */
  283. if (q->internal.qlen >= 8)
  284. return false;
  285. /* Ordering invariants fall apart if some delayed flows
  286. * are ready but we haven't serviced them, yet.
  287. */
  288. if (q->time_next_delayed_flow <= now + q->offload_horizon)
  289. return false;
  290. }
  291. sk = skb->sk;
  292. if (sk && sk_fullsock(sk) && !sk_is_tcp(sk) &&
  293. sk->sk_max_pacing_rate != ~0UL)
  294. return false;
  295. if (q->flow_max_rate != ~0UL)
  296. return false;
  297. return true;
  298. }
  299. static struct fq_flow *fq_classify(struct Qdisc *sch, struct sk_buff *skb,
  300. u64 now)
  301. {
  302. struct fq_sched_data *q = qdisc_priv(sch);
  303. struct rb_node **p, *parent;
  304. struct sock *sk = skb->sk;
  305. struct rb_root *root;
  306. struct fq_flow *f;
  307. /* SYNACK messages are attached to a TCP_NEW_SYN_RECV request socket
  308. * or a listener (SYNCOOKIE mode)
  309. * 1) request sockets are not full blown,
  310. * they do not contain sk_pacing_rate
  311. * 2) They are not part of a 'flow' yet
  312. * 3) We do not want to rate limit them (eg SYNFLOOD attack),
  313. * especially if the listener set SO_MAX_PACING_RATE
  314. * 4) We pretend they are orphaned
  315. * TCP can also associate TIME_WAIT sockets with RST or ACK packets.
  316. */
  317. if (!sk || sk_listener_or_tw(sk)) {
  318. unsigned long hash = skb_get_hash(skb) & q->orphan_mask;
  319. /* By forcing low order bit to 1, we make sure to not
  320. * collide with a local flow (socket pointers are word aligned)
  321. */
  322. sk = (struct sock *)((hash << 1) | 1UL);
  323. skb_orphan(skb);
  324. } else if (sk->sk_state == TCP_CLOSE) {
  325. unsigned long hash = skb_get_hash(skb) & q->orphan_mask;
  326. /*
  327. * Sockets in TCP_CLOSE are non connected.
  328. * Typical use case is UDP sockets, they can send packets
  329. * with sendto() to many different destinations.
  330. * We probably could use a generic bit advertising
  331. * non connected sockets, instead of sk_state == TCP_CLOSE,
  332. * if we care enough.
  333. */
  334. sk = (struct sock *)((hash << 1) | 1UL);
  335. }
  336. if (fq_fastpath_check(sch, skb, now)) {
  337. q->internal.stat_fastpath_packets++;
  338. if (skb->sk == sk && q->rate_enable &&
  339. READ_ONCE(sk->sk_pacing_status) != SK_PACING_FQ)
  340. smp_store_release(&sk->sk_pacing_status,
  341. SK_PACING_FQ);
  342. return &q->internal;
  343. }
  344. root = &q->fq_root[hash_ptr(sk, q->fq_trees_log)];
  345. fq_gc(q, root, sk);
  346. p = &root->rb_node;
  347. parent = NULL;
  348. while (*p) {
  349. parent = *p;
  350. f = rb_entry(parent, struct fq_flow, fq_node);
  351. if (f->sk == sk) {
  352. /* socket might have been reallocated, so check
  353. * if its sk_hash is the same.
  354. * It not, we need to refill credit with
  355. * initial quantum
  356. */
  357. if (unlikely(skb->sk == sk &&
  358. f->socket_hash != sk->sk_hash)) {
  359. f->credit = q->initial_quantum;
  360. f->socket_hash = sk->sk_hash;
  361. if (q->rate_enable)
  362. smp_store_release(&sk->sk_pacing_status,
  363. SK_PACING_FQ);
  364. if (fq_flow_is_throttled(f))
  365. fq_flow_unset_throttled(q, f);
  366. f->time_next_packet = 0ULL;
  367. }
  368. return f;
  369. }
  370. if (f->sk > sk)
  371. p = &parent->rb_right;
  372. else
  373. p = &parent->rb_left;
  374. }
  375. f = kmem_cache_zalloc(fq_flow_cachep, GFP_ATOMIC | __GFP_NOWARN);
  376. if (unlikely(!f)) {
  377. q->stat_allocation_errors++;
  378. return &q->internal;
  379. }
  380. /* f->t_root is already zeroed after kmem_cache_zalloc() */
  381. fq_flow_set_detached(f);
  382. f->sk = sk;
  383. if (skb->sk == sk) {
  384. f->socket_hash = sk->sk_hash;
  385. if (q->rate_enable)
  386. smp_store_release(&sk->sk_pacing_status,
  387. SK_PACING_FQ);
  388. }
  389. f->credit = q->initial_quantum;
  390. rb_link_node(&f->fq_node, parent, p);
  391. rb_insert_color(&f->fq_node, root);
  392. q->flows++;
  393. q->inactive_flows++;
  394. return f;
  395. }
  396. static struct sk_buff *fq_peek(struct fq_flow *flow)
  397. {
  398. struct sk_buff *skb = skb_rb_first(&flow->t_root);
  399. struct sk_buff *head = flow->head;
  400. if (!skb)
  401. return head;
  402. if (!head)
  403. return skb;
  404. if (fq_skb_cb(skb)->time_to_send < fq_skb_cb(head)->time_to_send)
  405. return skb;
  406. return head;
  407. }
  408. static void fq_erase_head(struct Qdisc *sch, struct fq_flow *flow,
  409. struct sk_buff *skb)
  410. {
  411. if (skb == flow->head) {
  412. struct sk_buff *next = skb->next;
  413. prefetch(next);
  414. flow->head = next;
  415. } else {
  416. rb_erase(&skb->rbnode, &flow->t_root);
  417. skb->dev = qdisc_dev(sch);
  418. }
  419. }
  420. /* Remove one skb from flow queue.
  421. * This skb must be the return value of prior fq_peek().
  422. */
  423. static void fq_dequeue_skb(struct Qdisc *sch, struct fq_flow *flow,
  424. struct sk_buff *skb)
  425. {
  426. fq_erase_head(sch, flow, skb);
  427. skb_mark_not_on_list(skb);
  428. qdisc_qstats_backlog_dec(sch, skb);
  429. sch->q.qlen--;
  430. qdisc_bstats_update(sch, skb);
  431. }
  432. static void flow_queue_add(struct fq_flow *flow, struct sk_buff *skb)
  433. {
  434. struct rb_node **p, *parent;
  435. struct sk_buff *head, *aux;
  436. head = flow->head;
  437. if (!head ||
  438. fq_skb_cb(skb)->time_to_send >= fq_skb_cb(flow->tail)->time_to_send) {
  439. if (!head)
  440. flow->head = skb;
  441. else
  442. flow->tail->next = skb;
  443. flow->tail = skb;
  444. skb->next = NULL;
  445. return;
  446. }
  447. p = &flow->t_root.rb_node;
  448. parent = NULL;
  449. while (*p) {
  450. parent = *p;
  451. aux = rb_to_skb(parent);
  452. if (fq_skb_cb(skb)->time_to_send >= fq_skb_cb(aux)->time_to_send)
  453. p = &parent->rb_right;
  454. else
  455. p = &parent->rb_left;
  456. }
  457. rb_link_node(&skb->rbnode, parent, p);
  458. rb_insert_color(&skb->rbnode, &flow->t_root);
  459. }
  460. static bool fq_packet_beyond_horizon(const struct sk_buff *skb,
  461. const struct fq_sched_data *q, u64 now)
  462. {
  463. return unlikely((s64)skb->tstamp > (s64)(now + q->horizon));
  464. }
  465. #define FQDR(reason) SKB_DROP_REASON_FQ_##reason
  466. static int fq_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  467. struct sk_buff **to_free)
  468. {
  469. struct fq_sched_data *q = qdisc_priv(sch);
  470. struct fq_flow *f;
  471. u64 now;
  472. u8 band;
  473. band = fq_prio2band(q->prio2band, skb->priority & TC_PRIO_MAX);
  474. if (unlikely(q->band_pkt_count[band] >= sch->limit)) {
  475. q->stat_band_drops[band]++;
  476. return qdisc_drop_reason(skb, sch, to_free,
  477. FQDR(BAND_LIMIT));
  478. }
  479. now = ktime_get_ns();
  480. if (!skb->tstamp) {
  481. fq_skb_cb(skb)->time_to_send = now;
  482. } else {
  483. /* Check if packet timestamp is too far in the future. */
  484. if (fq_packet_beyond_horizon(skb, q, now)) {
  485. if (q->horizon_drop) {
  486. q->stat_horizon_drops++;
  487. return qdisc_drop_reason(skb, sch, to_free,
  488. FQDR(HORIZON_LIMIT));
  489. }
  490. q->stat_horizon_caps++;
  491. skb->tstamp = now + q->horizon;
  492. }
  493. fq_skb_cb(skb)->time_to_send = skb->tstamp;
  494. }
  495. f = fq_classify(sch, skb, now);
  496. if (f != &q->internal) {
  497. if (unlikely(f->qlen >= q->flow_plimit)) {
  498. q->stat_flows_plimit++;
  499. return qdisc_drop_reason(skb, sch, to_free,
  500. FQDR(FLOW_LIMIT));
  501. }
  502. if (fq_flow_is_detached(f)) {
  503. fq_flow_add_tail(q, f, NEW_FLOW);
  504. if (time_after(jiffies, f->age + q->flow_refill_delay))
  505. f->credit = max_t(u32, f->credit, q->quantum);
  506. }
  507. f->band = band;
  508. q->band_pkt_count[band]++;
  509. fq_skb_cb(skb)->band = band;
  510. if (f->qlen == 0)
  511. q->inactive_flows--;
  512. }
  513. f->qlen++;
  514. /* Note: this overwrites f->age */
  515. flow_queue_add(f, skb);
  516. qdisc_qstats_backlog_inc(sch, skb);
  517. sch->q.qlen++;
  518. return NET_XMIT_SUCCESS;
  519. }
  520. #undef FQDR
  521. static void fq_check_throttled(struct fq_sched_data *q, u64 now)
  522. {
  523. unsigned long sample;
  524. struct rb_node *p;
  525. if (q->time_next_delayed_flow > now + q->offload_horizon)
  526. return;
  527. /* Update unthrottle latency EWMA.
  528. * This is cheap and can help diagnosing timer/latency problems.
  529. */
  530. sample = (unsigned long)(now - q->time_next_delayed_flow);
  531. if ((long)sample > 0) {
  532. q->unthrottle_latency_ns -= q->unthrottle_latency_ns >> 3;
  533. q->unthrottle_latency_ns += sample >> 3;
  534. }
  535. now += q->offload_horizon;
  536. q->time_next_delayed_flow = ~0ULL;
  537. while ((p = rb_first(&q->delayed)) != NULL) {
  538. struct fq_flow *f = rb_entry(p, struct fq_flow, rate_node);
  539. if (f->time_next_packet > now) {
  540. q->time_next_delayed_flow = f->time_next_packet;
  541. break;
  542. }
  543. fq_flow_unset_throttled(q, f);
  544. }
  545. }
  546. static struct fq_flow_head *fq_pband_head_select(struct fq_perband_flows *pband)
  547. {
  548. if (pband->credit <= 0)
  549. return NULL;
  550. if (pband->new_flows.first)
  551. return &pband->new_flows;
  552. return pband->old_flows.first ? &pband->old_flows : NULL;
  553. }
  554. static struct sk_buff *fq_dequeue(struct Qdisc *sch)
  555. {
  556. struct fq_sched_data *q = qdisc_priv(sch);
  557. struct fq_perband_flows *pband;
  558. struct fq_flow_head *head;
  559. struct sk_buff *skb;
  560. struct fq_flow *f;
  561. unsigned long rate;
  562. int retry;
  563. u32 plen;
  564. u64 now;
  565. if (!sch->q.qlen)
  566. return NULL;
  567. skb = fq_peek(&q->internal);
  568. if (skb) {
  569. q->internal.qlen--;
  570. fq_dequeue_skb(sch, &q->internal, skb);
  571. goto out;
  572. }
  573. now = ktime_get_ns();
  574. fq_check_throttled(q, now);
  575. retry = 0;
  576. pband = &q->band_flows[q->band_nr];
  577. begin:
  578. head = fq_pband_head_select(pband);
  579. if (!head) {
  580. while (++retry <= FQ_BANDS) {
  581. if (++q->band_nr == FQ_BANDS)
  582. q->band_nr = 0;
  583. pband = &q->band_flows[q->band_nr];
  584. pband->credit = min(pband->credit + pband->quantum,
  585. pband->quantum);
  586. if (pband->credit > 0)
  587. goto begin;
  588. retry = 0;
  589. }
  590. if (q->time_next_delayed_flow != ~0ULL)
  591. qdisc_watchdog_schedule_range_ns(&q->watchdog,
  592. q->time_next_delayed_flow,
  593. q->timer_slack);
  594. return NULL;
  595. }
  596. f = head->first;
  597. retry = 0;
  598. if (f->credit <= 0) {
  599. f->credit += q->quantum;
  600. head->first = f->next;
  601. fq_flow_add_tail(q, f, OLD_FLOW);
  602. goto begin;
  603. }
  604. skb = fq_peek(f);
  605. if (skb) {
  606. u64 time_next_packet = max_t(u64, fq_skb_cb(skb)->time_to_send,
  607. f->time_next_packet);
  608. if (now + q->offload_horizon < time_next_packet) {
  609. head->first = f->next;
  610. f->time_next_packet = time_next_packet;
  611. fq_flow_set_throttled(q, f);
  612. goto begin;
  613. }
  614. prefetch(&skb->end);
  615. fq_dequeue_skb(sch, f, skb);
  616. if (unlikely((s64)(now - time_next_packet - q->ce_threshold) > 0)) {
  617. INET_ECN_set_ce(skb);
  618. q->stat_ce_mark++;
  619. }
  620. if (--f->qlen == 0)
  621. q->inactive_flows++;
  622. q->band_pkt_count[fq_skb_cb(skb)->band]--;
  623. } else {
  624. head->first = f->next;
  625. /* force a pass through old_flows to prevent starvation */
  626. if (head == &pband->new_flows) {
  627. fq_flow_add_tail(q, f, OLD_FLOW);
  628. } else {
  629. fq_flow_set_detached(f);
  630. }
  631. goto begin;
  632. }
  633. plen = qdisc_pkt_len(skb);
  634. f->credit -= plen;
  635. pband->credit -= plen;
  636. if (!q->rate_enable)
  637. goto out;
  638. rate = q->flow_max_rate;
  639. /* If EDT time was provided for this skb, we need to
  640. * update f->time_next_packet only if this qdisc enforces
  641. * a flow max rate.
  642. */
  643. if (!skb->tstamp) {
  644. if (skb->sk)
  645. rate = min(READ_ONCE(skb->sk->sk_pacing_rate), rate);
  646. if (rate <= q->low_rate_threshold) {
  647. f->credit = 0;
  648. } else {
  649. plen = max(plen, q->quantum);
  650. if (f->credit > 0)
  651. goto out;
  652. }
  653. }
  654. if (rate != ~0UL) {
  655. u64 len = (u64)plen * NSEC_PER_SEC;
  656. if (likely(rate))
  657. len = div64_ul(len, rate);
  658. /* Since socket rate can change later,
  659. * clamp the delay to 1 second.
  660. * Really, providers of too big packets should be fixed !
  661. */
  662. if (unlikely(len > NSEC_PER_SEC)) {
  663. len = NSEC_PER_SEC;
  664. q->stat_pkts_too_long++;
  665. }
  666. /* Account for schedule/timers drifts.
  667. * f->time_next_packet was set when prior packet was sent,
  668. * and current time (@now) can be too late by tens of us.
  669. */
  670. if (f->time_next_packet)
  671. len -= min(len/2, now - f->time_next_packet);
  672. f->time_next_packet = now + len;
  673. }
  674. out:
  675. return skb;
  676. }
  677. static void fq_flow_purge(struct fq_flow *flow)
  678. {
  679. struct rb_node *p = rb_first(&flow->t_root);
  680. while (p) {
  681. struct sk_buff *skb = rb_to_skb(p);
  682. p = rb_next(p);
  683. rb_erase(&skb->rbnode, &flow->t_root);
  684. rtnl_kfree_skbs(skb, skb);
  685. }
  686. rtnl_kfree_skbs(flow->head, flow->tail);
  687. flow->head = NULL;
  688. flow->qlen = 0;
  689. }
  690. static void fq_reset(struct Qdisc *sch)
  691. {
  692. struct fq_sched_data *q = qdisc_priv(sch);
  693. struct rb_root *root;
  694. struct rb_node *p;
  695. struct fq_flow *f;
  696. unsigned int idx;
  697. sch->q.qlen = 0;
  698. sch->qstats.backlog = 0;
  699. fq_flow_purge(&q->internal);
  700. if (!q->fq_root)
  701. return;
  702. for (idx = 0; idx < (1U << q->fq_trees_log); idx++) {
  703. root = &q->fq_root[idx];
  704. while ((p = rb_first(root)) != NULL) {
  705. f = rb_entry(p, struct fq_flow, fq_node);
  706. rb_erase(p, root);
  707. fq_flow_purge(f);
  708. kmem_cache_free(fq_flow_cachep, f);
  709. }
  710. }
  711. for (idx = 0; idx < FQ_BANDS; idx++) {
  712. q->band_flows[idx].new_flows.first = NULL;
  713. q->band_flows[idx].old_flows.first = NULL;
  714. q->band_pkt_count[idx] = 0;
  715. }
  716. q->delayed = RB_ROOT;
  717. q->flows = 0;
  718. q->inactive_flows = 0;
  719. q->throttled_flows = 0;
  720. }
  721. static void fq_rehash(struct fq_sched_data *q,
  722. struct rb_root *old_array, u32 old_log,
  723. struct rb_root *new_array, u32 new_log)
  724. {
  725. struct rb_node *op, **np, *parent;
  726. struct rb_root *oroot, *nroot;
  727. struct fq_flow *of, *nf;
  728. int fcnt = 0;
  729. u32 idx;
  730. for (idx = 0; idx < (1U << old_log); idx++) {
  731. oroot = &old_array[idx];
  732. while ((op = rb_first(oroot)) != NULL) {
  733. rb_erase(op, oroot);
  734. of = rb_entry(op, struct fq_flow, fq_node);
  735. if (fq_gc_candidate(of)) {
  736. fcnt++;
  737. kmem_cache_free(fq_flow_cachep, of);
  738. continue;
  739. }
  740. nroot = &new_array[hash_ptr(of->sk, new_log)];
  741. np = &nroot->rb_node;
  742. parent = NULL;
  743. while (*np) {
  744. parent = *np;
  745. nf = rb_entry(parent, struct fq_flow, fq_node);
  746. BUG_ON(nf->sk == of->sk);
  747. if (nf->sk > of->sk)
  748. np = &parent->rb_right;
  749. else
  750. np = &parent->rb_left;
  751. }
  752. rb_link_node(&of->fq_node, parent, np);
  753. rb_insert_color(&of->fq_node, nroot);
  754. }
  755. }
  756. q->flows -= fcnt;
  757. q->inactive_flows -= fcnt;
  758. q->stat_gc_flows += fcnt;
  759. }
  760. static void fq_free(void *addr)
  761. {
  762. kvfree(addr);
  763. }
  764. static int fq_resize(struct Qdisc *sch, u32 log)
  765. {
  766. struct fq_sched_data *q = qdisc_priv(sch);
  767. struct rb_root *array;
  768. void *old_fq_root;
  769. u32 idx;
  770. if (q->fq_root && log == q->fq_trees_log)
  771. return 0;
  772. /* If XPS was setup, we can allocate memory on right NUMA node */
  773. array = kvmalloc_node(sizeof(struct rb_root) << log, GFP_KERNEL | __GFP_RETRY_MAYFAIL,
  774. netdev_queue_numa_node_read(sch->dev_queue));
  775. if (!array)
  776. return -ENOMEM;
  777. for (idx = 0; idx < (1U << log); idx++)
  778. array[idx] = RB_ROOT;
  779. sch_tree_lock(sch);
  780. old_fq_root = q->fq_root;
  781. if (old_fq_root)
  782. fq_rehash(q, old_fq_root, q->fq_trees_log, array, log);
  783. q->fq_root = array;
  784. WRITE_ONCE(q->fq_trees_log, log);
  785. sch_tree_unlock(sch);
  786. fq_free(old_fq_root);
  787. return 0;
  788. }
  789. static const struct netlink_range_validation iq_range = {
  790. .max = INT_MAX,
  791. };
  792. static const struct nla_policy fq_policy[TCA_FQ_MAX + 1] = {
  793. [TCA_FQ_UNSPEC] = { .strict_start_type = TCA_FQ_TIMER_SLACK },
  794. [TCA_FQ_PLIMIT] = { .type = NLA_U32 },
  795. [TCA_FQ_FLOW_PLIMIT] = { .type = NLA_U32 },
  796. [TCA_FQ_QUANTUM] = { .type = NLA_U32 },
  797. [TCA_FQ_INITIAL_QUANTUM] = NLA_POLICY_FULL_RANGE(NLA_U32, &iq_range),
  798. [TCA_FQ_RATE_ENABLE] = { .type = NLA_U32 },
  799. [TCA_FQ_FLOW_DEFAULT_RATE] = { .type = NLA_U32 },
  800. [TCA_FQ_FLOW_MAX_RATE] = { .type = NLA_U32 },
  801. [TCA_FQ_BUCKETS_LOG] = { .type = NLA_U32 },
  802. [TCA_FQ_FLOW_REFILL_DELAY] = { .type = NLA_U32 },
  803. [TCA_FQ_ORPHAN_MASK] = { .type = NLA_U32 },
  804. [TCA_FQ_LOW_RATE_THRESHOLD] = { .type = NLA_U32 },
  805. [TCA_FQ_CE_THRESHOLD] = { .type = NLA_U32 },
  806. [TCA_FQ_TIMER_SLACK] = { .type = NLA_U32 },
  807. [TCA_FQ_HORIZON] = { .type = NLA_U32 },
  808. [TCA_FQ_HORIZON_DROP] = { .type = NLA_U8 },
  809. [TCA_FQ_PRIOMAP] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_prio_qopt)),
  810. [TCA_FQ_WEIGHTS] = NLA_POLICY_EXACT_LEN(FQ_BANDS * sizeof(s32)),
  811. [TCA_FQ_OFFLOAD_HORIZON] = { .type = NLA_U32 },
  812. };
  813. /* compress a u8 array with all elems <= 3 to an array of 2-bit fields */
  814. static void fq_prio2band_compress_crumb(const u8 *in, u8 *out)
  815. {
  816. const int num_elems = TC_PRIO_MAX + 1;
  817. u8 tmp[FQ_PRIO2BAND_CRUMB_SIZE];
  818. int i;
  819. memset(tmp, 0, sizeof(tmp));
  820. for (i = 0; i < num_elems; i++)
  821. tmp[i / 4] |= in[i] << (2 * (i & 0x3));
  822. for (i = 0; i < FQ_PRIO2BAND_CRUMB_SIZE; i++)
  823. WRITE_ONCE(out[i], tmp[i]);
  824. }
  825. static void fq_prio2band_decompress_crumb(const u8 *in, u8 *out)
  826. {
  827. const int num_elems = TC_PRIO_MAX + 1;
  828. int i;
  829. for (i = 0; i < num_elems; i++)
  830. out[i] = fq_prio2band(in, i);
  831. }
  832. static int fq_load_weights(struct fq_sched_data *q,
  833. const struct nlattr *attr,
  834. struct netlink_ext_ack *extack)
  835. {
  836. s32 *weights = nla_data(attr);
  837. int i;
  838. for (i = 0; i < FQ_BANDS; i++) {
  839. if (weights[i] < FQ_MIN_WEIGHT) {
  840. NL_SET_ERR_MSG_FMT_MOD(extack, "Weight %d less that minimum allowed %d",
  841. weights[i], FQ_MIN_WEIGHT);
  842. return -EINVAL;
  843. }
  844. }
  845. for (i = 0; i < FQ_BANDS; i++)
  846. WRITE_ONCE(q->band_flows[i].quantum, weights[i]);
  847. return 0;
  848. }
  849. static int fq_load_priomap(struct fq_sched_data *q,
  850. const struct nlattr *attr,
  851. struct netlink_ext_ack *extack)
  852. {
  853. const struct tc_prio_qopt *map = nla_data(attr);
  854. int i;
  855. if (map->bands != FQ_BANDS) {
  856. NL_SET_ERR_MSG_MOD(extack, "FQ only supports 3 bands");
  857. return -EINVAL;
  858. }
  859. for (i = 0; i < TC_PRIO_MAX + 1; i++) {
  860. if (map->priomap[i] >= FQ_BANDS) {
  861. NL_SET_ERR_MSG_FMT_MOD(extack, "FQ priomap field %d maps to a too high band %d",
  862. i, map->priomap[i]);
  863. return -EINVAL;
  864. }
  865. }
  866. fq_prio2band_compress_crumb(map->priomap, q->prio2band);
  867. return 0;
  868. }
  869. static int fq_change(struct Qdisc *sch, struct nlattr *opt,
  870. struct netlink_ext_ack *extack)
  871. {
  872. unsigned int dropped_pkts = 0, dropped_bytes = 0;
  873. struct fq_sched_data *q = qdisc_priv(sch);
  874. struct nlattr *tb[TCA_FQ_MAX + 1];
  875. u32 fq_log;
  876. int err;
  877. err = nla_parse_nested_deprecated(tb, TCA_FQ_MAX, opt, fq_policy,
  878. NULL);
  879. if (err < 0)
  880. return err;
  881. sch_tree_lock(sch);
  882. fq_log = q->fq_trees_log;
  883. if (tb[TCA_FQ_BUCKETS_LOG]) {
  884. u32 nval = nla_get_u32(tb[TCA_FQ_BUCKETS_LOG]);
  885. if (nval >= 1 && nval <= ilog2(256*1024))
  886. fq_log = nval;
  887. else
  888. err = -EINVAL;
  889. }
  890. if (tb[TCA_FQ_PLIMIT])
  891. WRITE_ONCE(sch->limit,
  892. nla_get_u32(tb[TCA_FQ_PLIMIT]));
  893. if (tb[TCA_FQ_FLOW_PLIMIT])
  894. WRITE_ONCE(q->flow_plimit,
  895. nla_get_u32(tb[TCA_FQ_FLOW_PLIMIT]));
  896. if (tb[TCA_FQ_QUANTUM]) {
  897. u32 quantum = nla_get_u32(tb[TCA_FQ_QUANTUM]);
  898. if (quantum > 0 && quantum <= (1 << 20)) {
  899. WRITE_ONCE(q->quantum, quantum);
  900. } else {
  901. NL_SET_ERR_MSG_MOD(extack, "invalid quantum");
  902. err = -EINVAL;
  903. }
  904. }
  905. if (tb[TCA_FQ_INITIAL_QUANTUM])
  906. WRITE_ONCE(q->initial_quantum,
  907. nla_get_u32(tb[TCA_FQ_INITIAL_QUANTUM]));
  908. if (tb[TCA_FQ_FLOW_DEFAULT_RATE])
  909. pr_warn_ratelimited("sch_fq: defrate %u ignored.\n",
  910. nla_get_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]));
  911. if (tb[TCA_FQ_FLOW_MAX_RATE]) {
  912. u32 rate = nla_get_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
  913. WRITE_ONCE(q->flow_max_rate,
  914. (rate == ~0U) ? ~0UL : rate);
  915. }
  916. if (tb[TCA_FQ_LOW_RATE_THRESHOLD])
  917. WRITE_ONCE(q->low_rate_threshold,
  918. nla_get_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]));
  919. if (tb[TCA_FQ_RATE_ENABLE]) {
  920. u32 enable = nla_get_u32(tb[TCA_FQ_RATE_ENABLE]);
  921. if (enable <= 1)
  922. WRITE_ONCE(q->rate_enable,
  923. enable);
  924. else
  925. err = -EINVAL;
  926. }
  927. if (tb[TCA_FQ_FLOW_REFILL_DELAY]) {
  928. u32 usecs_delay = nla_get_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]) ;
  929. WRITE_ONCE(q->flow_refill_delay,
  930. usecs_to_jiffies(usecs_delay));
  931. }
  932. if (!err && tb[TCA_FQ_PRIOMAP])
  933. err = fq_load_priomap(q, tb[TCA_FQ_PRIOMAP], extack);
  934. if (!err && tb[TCA_FQ_WEIGHTS])
  935. err = fq_load_weights(q, tb[TCA_FQ_WEIGHTS], extack);
  936. if (tb[TCA_FQ_ORPHAN_MASK])
  937. WRITE_ONCE(q->orphan_mask,
  938. nla_get_u32(tb[TCA_FQ_ORPHAN_MASK]));
  939. if (tb[TCA_FQ_CE_THRESHOLD])
  940. WRITE_ONCE(q->ce_threshold,
  941. (u64)NSEC_PER_USEC *
  942. nla_get_u32(tb[TCA_FQ_CE_THRESHOLD]));
  943. if (tb[TCA_FQ_TIMER_SLACK])
  944. WRITE_ONCE(q->timer_slack,
  945. nla_get_u32(tb[TCA_FQ_TIMER_SLACK]));
  946. if (tb[TCA_FQ_HORIZON])
  947. WRITE_ONCE(q->horizon,
  948. (u64)NSEC_PER_USEC *
  949. nla_get_u32(tb[TCA_FQ_HORIZON]));
  950. if (tb[TCA_FQ_HORIZON_DROP])
  951. WRITE_ONCE(q->horizon_drop,
  952. nla_get_u8(tb[TCA_FQ_HORIZON_DROP]));
  953. if (tb[TCA_FQ_OFFLOAD_HORIZON]) {
  954. u64 offload_horizon = (u64)NSEC_PER_USEC *
  955. nla_get_u32(tb[TCA_FQ_OFFLOAD_HORIZON]);
  956. if (offload_horizon <= qdisc_dev(sch)->max_pacing_offload_horizon) {
  957. WRITE_ONCE(q->offload_horizon, offload_horizon);
  958. } else {
  959. NL_SET_ERR_MSG_MOD(extack, "invalid offload_horizon");
  960. err = -EINVAL;
  961. }
  962. }
  963. if (!err) {
  964. sch_tree_unlock(sch);
  965. err = fq_resize(sch, fq_log);
  966. sch_tree_lock(sch);
  967. }
  968. while (sch->q.qlen > sch->limit) {
  969. struct sk_buff *skb = qdisc_dequeue_internal(sch, false);
  970. if (!skb)
  971. break;
  972. dropped_pkts++;
  973. dropped_bytes += qdisc_pkt_len(skb);
  974. rtnl_kfree_skbs(skb, skb);
  975. }
  976. qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
  977. sch_tree_unlock(sch);
  978. return err;
  979. }
  980. static void fq_destroy(struct Qdisc *sch)
  981. {
  982. struct fq_sched_data *q = qdisc_priv(sch);
  983. fq_reset(sch);
  984. fq_free(q->fq_root);
  985. qdisc_watchdog_cancel(&q->watchdog);
  986. }
  987. static int fq_init(struct Qdisc *sch, struct nlattr *opt,
  988. struct netlink_ext_ack *extack)
  989. {
  990. struct fq_sched_data *q = qdisc_priv(sch);
  991. int i, err;
  992. sch->limit = 10000;
  993. q->flow_plimit = 100;
  994. q->quantum = 2 * psched_mtu(qdisc_dev(sch));
  995. q->initial_quantum = 10 * psched_mtu(qdisc_dev(sch));
  996. q->flow_refill_delay = msecs_to_jiffies(40);
  997. q->flow_max_rate = ~0UL;
  998. q->time_next_delayed_flow = ~0ULL;
  999. q->rate_enable = 1;
  1000. for (i = 0; i < FQ_BANDS; i++) {
  1001. q->band_flows[i].new_flows.first = NULL;
  1002. q->band_flows[i].old_flows.first = NULL;
  1003. }
  1004. q->band_flows[0].quantum = 9 << 16;
  1005. q->band_flows[1].quantum = 3 << 16;
  1006. q->band_flows[2].quantum = 1 << 16;
  1007. q->delayed = RB_ROOT;
  1008. q->fq_root = NULL;
  1009. q->fq_trees_log = ilog2(1024);
  1010. q->orphan_mask = 1024 - 1;
  1011. q->low_rate_threshold = 550000 / 8;
  1012. q->timer_slack = 10 * NSEC_PER_USEC; /* 10 usec of hrtimer slack */
  1013. q->horizon = 10ULL * NSEC_PER_SEC; /* 10 seconds */
  1014. q->horizon_drop = 1; /* by default, drop packets beyond horizon */
  1015. /* Default ce_threshold of 4294 seconds */
  1016. q->ce_threshold = (u64)NSEC_PER_USEC * ~0U;
  1017. fq_prio2band_compress_crumb(sch_default_prio2band, q->prio2band);
  1018. qdisc_watchdog_init_clockid(&q->watchdog, sch, CLOCK_MONOTONIC);
  1019. if (opt)
  1020. err = fq_change(sch, opt, extack);
  1021. else
  1022. err = fq_resize(sch, q->fq_trees_log);
  1023. return err;
  1024. }
  1025. static int fq_dump(struct Qdisc *sch, struct sk_buff *skb)
  1026. {
  1027. struct fq_sched_data *q = qdisc_priv(sch);
  1028. struct tc_prio_qopt prio = {
  1029. .bands = FQ_BANDS,
  1030. };
  1031. struct nlattr *opts;
  1032. u64 offload_horizon;
  1033. u64 ce_threshold;
  1034. s32 weights[3];
  1035. u64 horizon;
  1036. opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
  1037. if (opts == NULL)
  1038. goto nla_put_failure;
  1039. /* TCA_FQ_FLOW_DEFAULT_RATE is not used anymore */
  1040. ce_threshold = READ_ONCE(q->ce_threshold);
  1041. do_div(ce_threshold, NSEC_PER_USEC);
  1042. horizon = READ_ONCE(q->horizon);
  1043. do_div(horizon, NSEC_PER_USEC);
  1044. offload_horizon = READ_ONCE(q->offload_horizon);
  1045. do_div(offload_horizon, NSEC_PER_USEC);
  1046. if (nla_put_u32(skb, TCA_FQ_PLIMIT,
  1047. READ_ONCE(sch->limit)) ||
  1048. nla_put_u32(skb, TCA_FQ_FLOW_PLIMIT,
  1049. READ_ONCE(q->flow_plimit)) ||
  1050. nla_put_u32(skb, TCA_FQ_QUANTUM,
  1051. READ_ONCE(q->quantum)) ||
  1052. nla_put_u32(skb, TCA_FQ_INITIAL_QUANTUM,
  1053. READ_ONCE(q->initial_quantum)) ||
  1054. nla_put_u32(skb, TCA_FQ_RATE_ENABLE,
  1055. READ_ONCE(q->rate_enable)) ||
  1056. nla_put_u32(skb, TCA_FQ_FLOW_MAX_RATE,
  1057. min_t(unsigned long,
  1058. READ_ONCE(q->flow_max_rate), ~0U)) ||
  1059. nla_put_u32(skb, TCA_FQ_FLOW_REFILL_DELAY,
  1060. jiffies_to_usecs(READ_ONCE(q->flow_refill_delay))) ||
  1061. nla_put_u32(skb, TCA_FQ_ORPHAN_MASK,
  1062. READ_ONCE(q->orphan_mask)) ||
  1063. nla_put_u32(skb, TCA_FQ_LOW_RATE_THRESHOLD,
  1064. READ_ONCE(q->low_rate_threshold)) ||
  1065. nla_put_u32(skb, TCA_FQ_CE_THRESHOLD, (u32)ce_threshold) ||
  1066. nla_put_u32(skb, TCA_FQ_BUCKETS_LOG,
  1067. READ_ONCE(q->fq_trees_log)) ||
  1068. nla_put_u32(skb, TCA_FQ_TIMER_SLACK,
  1069. READ_ONCE(q->timer_slack)) ||
  1070. nla_put_u32(skb, TCA_FQ_HORIZON, (u32)horizon) ||
  1071. nla_put_u32(skb, TCA_FQ_OFFLOAD_HORIZON, (u32)offload_horizon) ||
  1072. nla_put_u8(skb, TCA_FQ_HORIZON_DROP,
  1073. READ_ONCE(q->horizon_drop)))
  1074. goto nla_put_failure;
  1075. fq_prio2band_decompress_crumb(q->prio2band, prio.priomap);
  1076. if (nla_put(skb, TCA_FQ_PRIOMAP, sizeof(prio), &prio))
  1077. goto nla_put_failure;
  1078. weights[0] = READ_ONCE(q->band_flows[0].quantum);
  1079. weights[1] = READ_ONCE(q->band_flows[1].quantum);
  1080. weights[2] = READ_ONCE(q->band_flows[2].quantum);
  1081. if (nla_put(skb, TCA_FQ_WEIGHTS, sizeof(weights), &weights))
  1082. goto nla_put_failure;
  1083. return nla_nest_end(skb, opts);
  1084. nla_put_failure:
  1085. return -1;
  1086. }
  1087. static int fq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  1088. {
  1089. struct fq_sched_data *q = qdisc_priv(sch);
  1090. struct tc_fq_qd_stats st;
  1091. int i;
  1092. st.pad = 0;
  1093. sch_tree_lock(sch);
  1094. st.gc_flows = q->stat_gc_flows;
  1095. st.highprio_packets = 0;
  1096. st.fastpath_packets = q->internal.stat_fastpath_packets;
  1097. st.tcp_retrans = 0;
  1098. st.throttled = q->stat_throttled;
  1099. st.flows_plimit = q->stat_flows_plimit;
  1100. st.pkts_too_long = q->stat_pkts_too_long;
  1101. st.allocation_errors = q->stat_allocation_errors;
  1102. st.time_next_delayed_flow = q->time_next_delayed_flow + q->timer_slack -
  1103. ktime_get_ns();
  1104. st.flows = q->flows;
  1105. st.inactive_flows = q->inactive_flows;
  1106. st.throttled_flows = q->throttled_flows;
  1107. st.unthrottle_latency_ns = min_t(unsigned long,
  1108. q->unthrottle_latency_ns, ~0U);
  1109. st.ce_mark = q->stat_ce_mark;
  1110. st.horizon_drops = q->stat_horizon_drops;
  1111. st.horizon_caps = q->stat_horizon_caps;
  1112. for (i = 0; i < FQ_BANDS; i++) {
  1113. st.band_drops[i] = q->stat_band_drops[i];
  1114. st.band_pkt_count[i] = q->band_pkt_count[i];
  1115. }
  1116. sch_tree_unlock(sch);
  1117. return gnet_stats_copy_app(d, &st, sizeof(st));
  1118. }
  1119. static struct Qdisc_ops fq_qdisc_ops __read_mostly = {
  1120. .id = "fq",
  1121. .priv_size = sizeof(struct fq_sched_data),
  1122. .enqueue = fq_enqueue,
  1123. .dequeue = fq_dequeue,
  1124. .peek = qdisc_peek_dequeued,
  1125. .init = fq_init,
  1126. .reset = fq_reset,
  1127. .destroy = fq_destroy,
  1128. .change = fq_change,
  1129. .dump = fq_dump,
  1130. .dump_stats = fq_dump_stats,
  1131. .owner = THIS_MODULE,
  1132. };
  1133. MODULE_ALIAS_NET_SCH("fq");
  1134. static int __init fq_module_init(void)
  1135. {
  1136. int ret;
  1137. fq_flow_cachep = kmem_cache_create("fq_flow_cache",
  1138. sizeof(struct fq_flow),
  1139. 0, SLAB_HWCACHE_ALIGN, NULL);
  1140. if (!fq_flow_cachep)
  1141. return -ENOMEM;
  1142. ret = register_qdisc(&fq_qdisc_ops);
  1143. if (ret)
  1144. kmem_cache_destroy(fq_flow_cachep);
  1145. return ret;
  1146. }
  1147. static void __exit fq_module_exit(void)
  1148. {
  1149. unregister_qdisc(&fq_qdisc_ops);
  1150. kmem_cache_destroy(fq_flow_cachep);
  1151. }
  1152. module_init(fq_module_init)
  1153. module_exit(fq_module_exit)
  1154. MODULE_AUTHOR("Eric Dumazet");
  1155. MODULE_LICENSE("GPL");
  1156. MODULE_DESCRIPTION("Fair Queue Packet Scheduler");