sch_fq.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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 <[email protected]>
  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 struct netlink_range_validation iq_range = {
  670. .max = INT_MAX,
  671. };
  672. static const struct nla_policy fq_policy[TCA_FQ_MAX + 1] = {
  673. [TCA_FQ_UNSPEC] = { .strict_start_type = TCA_FQ_TIMER_SLACK },
  674. [TCA_FQ_PLIMIT] = { .type = NLA_U32 },
  675. [TCA_FQ_FLOW_PLIMIT] = { .type = NLA_U32 },
  676. [TCA_FQ_QUANTUM] = { .type = NLA_U32 },
  677. [TCA_FQ_INITIAL_QUANTUM] = NLA_POLICY_FULL_RANGE(NLA_U32, &iq_range),
  678. [TCA_FQ_RATE_ENABLE] = { .type = NLA_U32 },
  679. [TCA_FQ_FLOW_DEFAULT_RATE] = { .type = NLA_U32 },
  680. [TCA_FQ_FLOW_MAX_RATE] = { .type = NLA_U32 },
  681. [TCA_FQ_BUCKETS_LOG] = { .type = NLA_U32 },
  682. [TCA_FQ_FLOW_REFILL_DELAY] = { .type = NLA_U32 },
  683. [TCA_FQ_ORPHAN_MASK] = { .type = NLA_U32 },
  684. [TCA_FQ_LOW_RATE_THRESHOLD] = { .type = NLA_U32 },
  685. [TCA_FQ_CE_THRESHOLD] = { .type = NLA_U32 },
  686. [TCA_FQ_TIMER_SLACK] = { .type = NLA_U32 },
  687. [TCA_FQ_HORIZON] = { .type = NLA_U32 },
  688. [TCA_FQ_HORIZON_DROP] = { .type = NLA_U8 },
  689. };
  690. static int fq_change(struct Qdisc *sch, struct nlattr *opt,
  691. struct netlink_ext_ack *extack)
  692. {
  693. struct fq_sched_data *q = qdisc_priv(sch);
  694. struct nlattr *tb[TCA_FQ_MAX + 1];
  695. int err, drop_count = 0;
  696. unsigned drop_len = 0;
  697. u32 fq_log;
  698. err = nla_parse_nested_deprecated(tb, TCA_FQ_MAX, opt, fq_policy,
  699. NULL);
  700. if (err < 0)
  701. return err;
  702. sch_tree_lock(sch);
  703. fq_log = q->fq_trees_log;
  704. if (tb[TCA_FQ_BUCKETS_LOG]) {
  705. u32 nval = nla_get_u32(tb[TCA_FQ_BUCKETS_LOG]);
  706. if (nval >= 1 && nval <= ilog2(256*1024))
  707. fq_log = nval;
  708. else
  709. err = -EINVAL;
  710. }
  711. if (tb[TCA_FQ_PLIMIT])
  712. sch->limit = nla_get_u32(tb[TCA_FQ_PLIMIT]);
  713. if (tb[TCA_FQ_FLOW_PLIMIT])
  714. q->flow_plimit = nla_get_u32(tb[TCA_FQ_FLOW_PLIMIT]);
  715. if (tb[TCA_FQ_QUANTUM]) {
  716. u32 quantum = nla_get_u32(tb[TCA_FQ_QUANTUM]);
  717. if (quantum > 0 && quantum <= (1 << 20)) {
  718. q->quantum = quantum;
  719. } else {
  720. NL_SET_ERR_MSG_MOD(extack, "invalid quantum");
  721. err = -EINVAL;
  722. }
  723. }
  724. if (tb[TCA_FQ_INITIAL_QUANTUM])
  725. q->initial_quantum = nla_get_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
  726. if (tb[TCA_FQ_FLOW_DEFAULT_RATE])
  727. pr_warn_ratelimited("sch_fq: defrate %u ignored.\n",
  728. nla_get_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]));
  729. if (tb[TCA_FQ_FLOW_MAX_RATE]) {
  730. u32 rate = nla_get_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
  731. q->flow_max_rate = (rate == ~0U) ? ~0UL : rate;
  732. }
  733. if (tb[TCA_FQ_LOW_RATE_THRESHOLD])
  734. q->low_rate_threshold =
  735. nla_get_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]);
  736. if (tb[TCA_FQ_RATE_ENABLE]) {
  737. u32 enable = nla_get_u32(tb[TCA_FQ_RATE_ENABLE]);
  738. if (enable <= 1)
  739. q->rate_enable = enable;
  740. else
  741. err = -EINVAL;
  742. }
  743. if (tb[TCA_FQ_FLOW_REFILL_DELAY]) {
  744. u32 usecs_delay = nla_get_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]) ;
  745. q->flow_refill_delay = usecs_to_jiffies(usecs_delay);
  746. }
  747. if (tb[TCA_FQ_ORPHAN_MASK])
  748. q->orphan_mask = nla_get_u32(tb[TCA_FQ_ORPHAN_MASK]);
  749. if (tb[TCA_FQ_CE_THRESHOLD])
  750. q->ce_threshold = (u64)NSEC_PER_USEC *
  751. nla_get_u32(tb[TCA_FQ_CE_THRESHOLD]);
  752. if (tb[TCA_FQ_TIMER_SLACK])
  753. q->timer_slack = nla_get_u32(tb[TCA_FQ_TIMER_SLACK]);
  754. if (tb[TCA_FQ_HORIZON])
  755. q->horizon = (u64)NSEC_PER_USEC *
  756. nla_get_u32(tb[TCA_FQ_HORIZON]);
  757. if (tb[TCA_FQ_HORIZON_DROP])
  758. q->horizon_drop = nla_get_u8(tb[TCA_FQ_HORIZON_DROP]);
  759. if (!err) {
  760. sch_tree_unlock(sch);
  761. err = fq_resize(sch, fq_log);
  762. sch_tree_lock(sch);
  763. }
  764. while (sch->q.qlen > sch->limit) {
  765. struct sk_buff *skb = fq_dequeue(sch);
  766. if (!skb)
  767. break;
  768. drop_len += qdisc_pkt_len(skb);
  769. rtnl_kfree_skbs(skb, skb);
  770. drop_count++;
  771. }
  772. qdisc_tree_reduce_backlog(sch, drop_count, drop_len);
  773. sch_tree_unlock(sch);
  774. return err;
  775. }
  776. static void fq_destroy(struct Qdisc *sch)
  777. {
  778. struct fq_sched_data *q = qdisc_priv(sch);
  779. fq_reset(sch);
  780. fq_free(q->fq_root);
  781. qdisc_watchdog_cancel(&q->watchdog);
  782. }
  783. static int fq_init(struct Qdisc *sch, struct nlattr *opt,
  784. struct netlink_ext_ack *extack)
  785. {
  786. struct fq_sched_data *q = qdisc_priv(sch);
  787. int err;
  788. sch->limit = 10000;
  789. q->flow_plimit = 100;
  790. q->quantum = 2 * psched_mtu(qdisc_dev(sch));
  791. q->initial_quantum = 10 * psched_mtu(qdisc_dev(sch));
  792. q->flow_refill_delay = msecs_to_jiffies(40);
  793. q->flow_max_rate = ~0UL;
  794. q->time_next_delayed_flow = ~0ULL;
  795. q->rate_enable = 1;
  796. q->new_flows.first = NULL;
  797. q->old_flows.first = NULL;
  798. q->delayed = RB_ROOT;
  799. q->fq_root = NULL;
  800. q->fq_trees_log = ilog2(1024);
  801. q->orphan_mask = 1024 - 1;
  802. q->low_rate_threshold = 550000 / 8;
  803. q->timer_slack = 10 * NSEC_PER_USEC; /* 10 usec of hrtimer slack */
  804. q->horizon = 10ULL * NSEC_PER_SEC; /* 10 seconds */
  805. q->horizon_drop = 1; /* by default, drop packets beyond horizon */
  806. /* Default ce_threshold of 4294 seconds */
  807. q->ce_threshold = (u64)NSEC_PER_USEC * ~0U;
  808. qdisc_watchdog_init_clockid(&q->watchdog, sch, CLOCK_MONOTONIC);
  809. if (opt)
  810. err = fq_change(sch, opt, extack);
  811. else
  812. err = fq_resize(sch, q->fq_trees_log);
  813. return err;
  814. }
  815. static int fq_dump(struct Qdisc *sch, struct sk_buff *skb)
  816. {
  817. struct fq_sched_data *q = qdisc_priv(sch);
  818. u64 ce_threshold = q->ce_threshold;
  819. u64 horizon = q->horizon;
  820. struct nlattr *opts;
  821. opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
  822. if (opts == NULL)
  823. goto nla_put_failure;
  824. /* TCA_FQ_FLOW_DEFAULT_RATE is not used anymore */
  825. do_div(ce_threshold, NSEC_PER_USEC);
  826. do_div(horizon, NSEC_PER_USEC);
  827. if (nla_put_u32(skb, TCA_FQ_PLIMIT, sch->limit) ||
  828. nla_put_u32(skb, TCA_FQ_FLOW_PLIMIT, q->flow_plimit) ||
  829. nla_put_u32(skb, TCA_FQ_QUANTUM, q->quantum) ||
  830. nla_put_u32(skb, TCA_FQ_INITIAL_QUANTUM, q->initial_quantum) ||
  831. nla_put_u32(skb, TCA_FQ_RATE_ENABLE, q->rate_enable) ||
  832. nla_put_u32(skb, TCA_FQ_FLOW_MAX_RATE,
  833. min_t(unsigned long, q->flow_max_rate, ~0U)) ||
  834. nla_put_u32(skb, TCA_FQ_FLOW_REFILL_DELAY,
  835. jiffies_to_usecs(q->flow_refill_delay)) ||
  836. nla_put_u32(skb, TCA_FQ_ORPHAN_MASK, q->orphan_mask) ||
  837. nla_put_u32(skb, TCA_FQ_LOW_RATE_THRESHOLD,
  838. q->low_rate_threshold) ||
  839. nla_put_u32(skb, TCA_FQ_CE_THRESHOLD, (u32)ce_threshold) ||
  840. nla_put_u32(skb, TCA_FQ_BUCKETS_LOG, q->fq_trees_log) ||
  841. nla_put_u32(skb, TCA_FQ_TIMER_SLACK, q->timer_slack) ||
  842. nla_put_u32(skb, TCA_FQ_HORIZON, (u32)horizon) ||
  843. nla_put_u8(skb, TCA_FQ_HORIZON_DROP, q->horizon_drop))
  844. goto nla_put_failure;
  845. return nla_nest_end(skb, opts);
  846. nla_put_failure:
  847. return -1;
  848. }
  849. static int fq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  850. {
  851. struct fq_sched_data *q = qdisc_priv(sch);
  852. struct tc_fq_qd_stats st;
  853. sch_tree_lock(sch);
  854. st.gc_flows = q->stat_gc_flows;
  855. st.highprio_packets = q->stat_internal_packets;
  856. st.tcp_retrans = 0;
  857. st.throttled = q->stat_throttled;
  858. st.flows_plimit = q->stat_flows_plimit;
  859. st.pkts_too_long = q->stat_pkts_too_long;
  860. st.allocation_errors = q->stat_allocation_errors;
  861. st.time_next_delayed_flow = q->time_next_delayed_flow + q->timer_slack -
  862. ktime_get_ns();
  863. st.flows = q->flows;
  864. st.inactive_flows = q->inactive_flows;
  865. st.throttled_flows = q->throttled_flows;
  866. st.unthrottle_latency_ns = min_t(unsigned long,
  867. q->unthrottle_latency_ns, ~0U);
  868. st.ce_mark = q->stat_ce_mark;
  869. st.horizon_drops = q->stat_horizon_drops;
  870. st.horizon_caps = q->stat_horizon_caps;
  871. sch_tree_unlock(sch);
  872. return gnet_stats_copy_app(d, &st, sizeof(st));
  873. }
  874. static struct Qdisc_ops fq_qdisc_ops __read_mostly = {
  875. .id = "fq",
  876. .priv_size = sizeof(struct fq_sched_data),
  877. .enqueue = fq_enqueue,
  878. .dequeue = fq_dequeue,
  879. .peek = qdisc_peek_dequeued,
  880. .init = fq_init,
  881. .reset = fq_reset,
  882. .destroy = fq_destroy,
  883. .change = fq_change,
  884. .dump = fq_dump,
  885. .dump_stats = fq_dump_stats,
  886. .owner = THIS_MODULE,
  887. };
  888. static int __init fq_module_init(void)
  889. {
  890. int ret;
  891. fq_flow_cachep = kmem_cache_create("fq_flow_cache",
  892. sizeof(struct fq_flow),
  893. 0, 0, NULL);
  894. if (!fq_flow_cachep)
  895. return -ENOMEM;
  896. ret = register_qdisc(&fq_qdisc_ops);
  897. if (ret)
  898. kmem_cache_destroy(fq_flow_cachep);
  899. return ret;
  900. }
  901. static void __exit fq_module_exit(void)
  902. {
  903. unregister_qdisc(&fq_qdisc_ops);
  904. kmem_cache_destroy(fq_flow_cachep);
  905. }
  906. module_init(fq_module_init)
  907. module_exit(fq_module_exit)
  908. MODULE_AUTHOR("Eric Dumazet");
  909. MODULE_LICENSE("GPL");
  910. MODULE_DESCRIPTION("Fair Queue Packet Scheduler");