sch_fq.c 33 KB

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