sch_fq_codel.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Fair Queue CoDel discipline
  4. *
  5. * Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/string.h>
  12. #include <linux/in.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <net/netlink.h>
  19. #include <net/pkt_sched.h>
  20. #include <net/pkt_cls.h>
  21. #include <net/codel.h>
  22. #include <net/codel_impl.h>
  23. #include <net/codel_qdisc.h>
  24. /* Fair Queue CoDel.
  25. *
  26. * Principles :
  27. * Packets are classified (internal classifier or external) on flows.
  28. * This is a Stochastic model (as we use a hash, several flows
  29. * might be hashed on same slot)
  30. * Each flow has a CoDel managed queue.
  31. * Flows are linked onto two (Round Robin) lists,
  32. * so that new flows have priority on old ones.
  33. *
  34. * For a given flow, packets are not reordered (CoDel uses a FIFO)
  35. * head drops only.
  36. * ECN capability is on by default.
  37. * Low memory footprint (64 bytes per flow)
  38. */
  39. struct fq_codel_flow {
  40. struct sk_buff *head;
  41. struct sk_buff *tail;
  42. struct list_head flowchain;
  43. int deficit;
  44. struct codel_vars cvars;
  45. }; /* please try to keep this structure <= 64 bytes */
  46. struct fq_codel_sched_data {
  47. struct tcf_proto __rcu *filter_list; /* optional external classifier */
  48. struct tcf_block *block;
  49. struct fq_codel_flow *flows; /* Flows table [flows_cnt] */
  50. u32 *backlogs; /* backlog table [flows_cnt] */
  51. u32 flows_cnt; /* number of flows */
  52. u32 quantum; /* psched_mtu(qdisc_dev(sch)); */
  53. u32 drop_batch_size;
  54. u32 memory_limit;
  55. struct codel_params cparams;
  56. struct codel_stats cstats;
  57. u32 memory_usage;
  58. u32 drop_overmemory;
  59. u32 drop_overlimit;
  60. u32 new_flow_count;
  61. struct list_head new_flows; /* list of new flows */
  62. struct list_head old_flows; /* list of old flows */
  63. };
  64. static unsigned int fq_codel_hash(const struct fq_codel_sched_data *q,
  65. struct sk_buff *skb)
  66. {
  67. return reciprocal_scale(skb_get_hash(skb), q->flows_cnt);
  68. }
  69. static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,
  70. int *qerr)
  71. {
  72. struct fq_codel_sched_data *q = qdisc_priv(sch);
  73. struct tcf_proto *filter;
  74. struct tcf_result res;
  75. int result;
  76. if (TC_H_MAJ(skb->priority) == sch->handle &&
  77. TC_H_MIN(skb->priority) > 0 &&
  78. TC_H_MIN(skb->priority) <= q->flows_cnt)
  79. return TC_H_MIN(skb->priority);
  80. filter = rcu_dereference_bh(q->filter_list);
  81. if (!filter)
  82. return fq_codel_hash(q, skb) + 1;
  83. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  84. result = tcf_classify(skb, NULL, filter, &res, false);
  85. if (result >= 0) {
  86. #ifdef CONFIG_NET_CLS_ACT
  87. switch (result) {
  88. case TC_ACT_STOLEN:
  89. case TC_ACT_QUEUED:
  90. case TC_ACT_TRAP:
  91. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  92. fallthrough;
  93. case TC_ACT_SHOT:
  94. return 0;
  95. }
  96. #endif
  97. if (TC_H_MIN(res.classid) <= q->flows_cnt)
  98. return TC_H_MIN(res.classid);
  99. }
  100. return 0;
  101. }
  102. /* helper functions : might be changed when/if skb use a standard list_head */
  103. /* remove one skb from head of slot queue */
  104. static inline struct sk_buff *dequeue_head(struct fq_codel_flow *flow)
  105. {
  106. struct sk_buff *skb = flow->head;
  107. flow->head = skb->next;
  108. skb_mark_not_on_list(skb);
  109. return skb;
  110. }
  111. /* add skb to flow queue (tail add) */
  112. static inline void flow_queue_add(struct fq_codel_flow *flow,
  113. struct sk_buff *skb)
  114. {
  115. if (flow->head == NULL)
  116. flow->head = skb;
  117. else
  118. flow->tail->next = skb;
  119. flow->tail = skb;
  120. skb->next = NULL;
  121. }
  122. static unsigned int fq_codel_drop(struct Qdisc *sch, unsigned int max_packets,
  123. struct sk_buff **to_free)
  124. {
  125. struct fq_codel_sched_data *q = qdisc_priv(sch);
  126. struct sk_buff *skb;
  127. unsigned int maxbacklog = 0, idx = 0, i, len;
  128. struct fq_codel_flow *flow;
  129. unsigned int threshold;
  130. unsigned int mem = 0;
  131. /* Queue is full! Find the fat flow and drop packet(s) from it.
  132. * This might sound expensive, but with 1024 flows, we scan
  133. * 4KB of memory, and we dont need to handle a complex tree
  134. * in fast path (packet queue/enqueue) with many cache misses.
  135. * In stress mode, we'll try to drop 64 packets from the flow,
  136. * amortizing this linear lookup to one cache line per drop.
  137. */
  138. for (i = 0; i < q->flows_cnt; i++) {
  139. if (q->backlogs[i] > maxbacklog) {
  140. maxbacklog = q->backlogs[i];
  141. idx = i;
  142. }
  143. }
  144. /* Our goal is to drop half of this fat flow backlog */
  145. threshold = maxbacklog >> 1;
  146. flow = &q->flows[idx];
  147. len = 0;
  148. i = 0;
  149. do {
  150. skb = dequeue_head(flow);
  151. len += qdisc_pkt_len(skb);
  152. mem += get_codel_cb(skb)->mem_usage;
  153. tcf_set_drop_reason(skb, SKB_DROP_REASON_QDISC_OVERLIMIT);
  154. __qdisc_drop(skb, to_free);
  155. } while (++i < max_packets && len < threshold);
  156. /* Tell codel to increase its signal strength also */
  157. flow->cvars.count += i;
  158. q->backlogs[idx] -= len;
  159. q->memory_usage -= mem;
  160. sch->qstats.drops += i;
  161. sch->qstats.backlog -= len;
  162. sch->q.qlen -= i;
  163. return idx;
  164. }
  165. static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  166. struct sk_buff **to_free)
  167. {
  168. struct fq_codel_sched_data *q = qdisc_priv(sch);
  169. unsigned int idx, prev_backlog, prev_qlen;
  170. struct fq_codel_flow *flow;
  171. int ret;
  172. unsigned int pkt_len;
  173. bool memory_limited;
  174. idx = fq_codel_classify(skb, sch, &ret);
  175. if (idx == 0) {
  176. if (ret & __NET_XMIT_BYPASS)
  177. qdisc_qstats_drop(sch);
  178. __qdisc_drop(skb, to_free);
  179. return ret;
  180. }
  181. idx--;
  182. codel_set_enqueue_time(skb);
  183. flow = &q->flows[idx];
  184. flow_queue_add(flow, skb);
  185. q->backlogs[idx] += qdisc_pkt_len(skb);
  186. qdisc_qstats_backlog_inc(sch, skb);
  187. if (list_empty(&flow->flowchain)) {
  188. list_add_tail(&flow->flowchain, &q->new_flows);
  189. q->new_flow_count++;
  190. flow->deficit = q->quantum;
  191. }
  192. get_codel_cb(skb)->mem_usage = skb->truesize;
  193. q->memory_usage += get_codel_cb(skb)->mem_usage;
  194. memory_limited = q->memory_usage > q->memory_limit;
  195. if (++sch->q.qlen <= sch->limit && !memory_limited)
  196. return NET_XMIT_SUCCESS;
  197. prev_backlog = sch->qstats.backlog;
  198. prev_qlen = sch->q.qlen;
  199. /* save this packet length as it might be dropped by fq_codel_drop() */
  200. pkt_len = qdisc_pkt_len(skb);
  201. /* fq_codel_drop() is quite expensive, as it performs a linear search
  202. * in q->backlogs[] to find a fat flow.
  203. * So instead of dropping a single packet, drop half of its backlog
  204. * with a 64 packets limit to not add a too big cpu spike here.
  205. */
  206. ret = fq_codel_drop(sch, q->drop_batch_size, to_free);
  207. prev_qlen -= sch->q.qlen;
  208. prev_backlog -= sch->qstats.backlog;
  209. q->drop_overlimit += prev_qlen;
  210. if (memory_limited)
  211. q->drop_overmemory += prev_qlen;
  212. /* As we dropped packet(s), better let upper stack know this.
  213. * If we dropped a packet for this flow, return NET_XMIT_CN,
  214. * but in this case, our parents wont increase their backlogs.
  215. */
  216. if (ret == idx) {
  217. qdisc_tree_reduce_backlog(sch, prev_qlen - 1,
  218. prev_backlog - pkt_len);
  219. return NET_XMIT_CN;
  220. }
  221. qdisc_tree_reduce_backlog(sch, prev_qlen, prev_backlog);
  222. return NET_XMIT_SUCCESS;
  223. }
  224. /* This is the specific function called from codel_dequeue()
  225. * to dequeue a packet from queue. Note: backlog is handled in
  226. * codel, we dont need to reduce it here.
  227. */
  228. static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
  229. {
  230. struct Qdisc *sch = ctx;
  231. struct fq_codel_sched_data *q = qdisc_priv(sch);
  232. struct fq_codel_flow *flow;
  233. struct sk_buff *skb = NULL;
  234. flow = container_of(vars, struct fq_codel_flow, cvars);
  235. if (flow->head) {
  236. skb = dequeue_head(flow);
  237. q->backlogs[flow - q->flows] -= qdisc_pkt_len(skb);
  238. q->memory_usage -= get_codel_cb(skb)->mem_usage;
  239. sch->q.qlen--;
  240. sch->qstats.backlog -= qdisc_pkt_len(skb);
  241. }
  242. return skb;
  243. }
  244. static void drop_func(struct sk_buff *skb, void *ctx)
  245. {
  246. struct Qdisc *sch = ctx;
  247. qdisc_dequeue_drop(sch, skb, SKB_DROP_REASON_QDISC_CONGESTED);
  248. qdisc_qstats_drop(sch);
  249. }
  250. static struct sk_buff *fq_codel_dequeue(struct Qdisc *sch)
  251. {
  252. struct fq_codel_sched_data *q = qdisc_priv(sch);
  253. struct sk_buff *skb;
  254. struct fq_codel_flow *flow;
  255. struct list_head *head;
  256. begin:
  257. head = &q->new_flows;
  258. if (list_empty(head)) {
  259. head = &q->old_flows;
  260. if (list_empty(head))
  261. return NULL;
  262. }
  263. flow = list_first_entry(head, struct fq_codel_flow, flowchain);
  264. if (flow->deficit <= 0) {
  265. flow->deficit += q->quantum;
  266. list_move_tail(&flow->flowchain, &q->old_flows);
  267. goto begin;
  268. }
  269. skb = codel_dequeue(sch, &sch->qstats.backlog, &q->cparams,
  270. &flow->cvars, &q->cstats, qdisc_pkt_len,
  271. codel_get_enqueue_time, drop_func, dequeue_func);
  272. if (!skb) {
  273. /* force a pass through old_flows to prevent starvation */
  274. if ((head == &q->new_flows) && !list_empty(&q->old_flows))
  275. list_move_tail(&flow->flowchain, &q->old_flows);
  276. else
  277. list_del_init(&flow->flowchain);
  278. goto begin;
  279. }
  280. qdisc_bstats_update(sch, skb);
  281. flow->deficit -= qdisc_pkt_len(skb);
  282. if (q->cstats.drop_count) {
  283. qdisc_tree_reduce_backlog(sch, q->cstats.drop_count,
  284. q->cstats.drop_len);
  285. q->cstats.drop_count = 0;
  286. q->cstats.drop_len = 0;
  287. }
  288. return skb;
  289. }
  290. static void fq_codel_flow_purge(struct fq_codel_flow *flow)
  291. {
  292. rtnl_kfree_skbs(flow->head, flow->tail);
  293. flow->head = NULL;
  294. }
  295. static void fq_codel_reset(struct Qdisc *sch)
  296. {
  297. struct fq_codel_sched_data *q = qdisc_priv(sch);
  298. int i;
  299. INIT_LIST_HEAD(&q->new_flows);
  300. INIT_LIST_HEAD(&q->old_flows);
  301. for (i = 0; i < q->flows_cnt; i++) {
  302. struct fq_codel_flow *flow = q->flows + i;
  303. fq_codel_flow_purge(flow);
  304. INIT_LIST_HEAD(&flow->flowchain);
  305. codel_vars_init(&flow->cvars);
  306. }
  307. memset(q->backlogs, 0, q->flows_cnt * sizeof(u32));
  308. q->memory_usage = 0;
  309. }
  310. static const struct nla_policy fq_codel_policy[TCA_FQ_CODEL_MAX + 1] = {
  311. [TCA_FQ_CODEL_TARGET] = { .type = NLA_U32 },
  312. [TCA_FQ_CODEL_LIMIT] = { .type = NLA_U32 },
  313. [TCA_FQ_CODEL_INTERVAL] = { .type = NLA_U32 },
  314. [TCA_FQ_CODEL_ECN] = { .type = NLA_U32 },
  315. [TCA_FQ_CODEL_FLOWS] = { .type = NLA_U32 },
  316. [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 },
  317. [TCA_FQ_CODEL_CE_THRESHOLD] = { .type = NLA_U32 },
  318. [TCA_FQ_CODEL_DROP_BATCH_SIZE] = { .type = NLA_U32 },
  319. [TCA_FQ_CODEL_MEMORY_LIMIT] = { .type = NLA_U32 },
  320. [TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR] = { .type = NLA_U8 },
  321. [TCA_FQ_CODEL_CE_THRESHOLD_MASK] = { .type = NLA_U8 },
  322. };
  323. static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt,
  324. struct netlink_ext_ack *extack)
  325. {
  326. unsigned int dropped_pkts = 0, dropped_bytes = 0;
  327. struct fq_codel_sched_data *q = qdisc_priv(sch);
  328. struct nlattr *tb[TCA_FQ_CODEL_MAX + 1];
  329. u32 quantum = 0;
  330. int err;
  331. err = nla_parse_nested_deprecated(tb, TCA_FQ_CODEL_MAX, opt,
  332. fq_codel_policy, NULL);
  333. if (err < 0)
  334. return err;
  335. if (tb[TCA_FQ_CODEL_FLOWS]) {
  336. if (q->flows)
  337. return -EINVAL;
  338. q->flows_cnt = nla_get_u32(tb[TCA_FQ_CODEL_FLOWS]);
  339. if (!q->flows_cnt ||
  340. q->flows_cnt > 65536)
  341. return -EINVAL;
  342. }
  343. if (tb[TCA_FQ_CODEL_QUANTUM]) {
  344. quantum = max(256U, nla_get_u32(tb[TCA_FQ_CODEL_QUANTUM]));
  345. if (quantum > FQ_CODEL_QUANTUM_MAX) {
  346. NL_SET_ERR_MSG(extack, "Invalid quantum");
  347. return -EINVAL;
  348. }
  349. }
  350. sch_tree_lock(sch);
  351. if (tb[TCA_FQ_CODEL_TARGET]) {
  352. u64 target = nla_get_u32(tb[TCA_FQ_CODEL_TARGET]);
  353. WRITE_ONCE(q->cparams.target,
  354. (target * NSEC_PER_USEC) >> CODEL_SHIFT);
  355. }
  356. if (tb[TCA_FQ_CODEL_CE_THRESHOLD]) {
  357. u64 val = nla_get_u32(tb[TCA_FQ_CODEL_CE_THRESHOLD]);
  358. WRITE_ONCE(q->cparams.ce_threshold,
  359. (val * NSEC_PER_USEC) >> CODEL_SHIFT);
  360. }
  361. if (tb[TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR])
  362. WRITE_ONCE(q->cparams.ce_threshold_selector,
  363. nla_get_u8(tb[TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR]));
  364. if (tb[TCA_FQ_CODEL_CE_THRESHOLD_MASK])
  365. WRITE_ONCE(q->cparams.ce_threshold_mask,
  366. nla_get_u8(tb[TCA_FQ_CODEL_CE_THRESHOLD_MASK]));
  367. if (tb[TCA_FQ_CODEL_INTERVAL]) {
  368. u64 interval = nla_get_u32(tb[TCA_FQ_CODEL_INTERVAL]);
  369. WRITE_ONCE(q->cparams.interval,
  370. (interval * NSEC_PER_USEC) >> CODEL_SHIFT);
  371. }
  372. if (tb[TCA_FQ_CODEL_LIMIT])
  373. WRITE_ONCE(sch->limit,
  374. nla_get_u32(tb[TCA_FQ_CODEL_LIMIT]));
  375. if (tb[TCA_FQ_CODEL_ECN])
  376. WRITE_ONCE(q->cparams.ecn,
  377. !!nla_get_u32(tb[TCA_FQ_CODEL_ECN]));
  378. if (quantum)
  379. WRITE_ONCE(q->quantum, quantum);
  380. if (tb[TCA_FQ_CODEL_DROP_BATCH_SIZE])
  381. WRITE_ONCE(q->drop_batch_size,
  382. max(1U, nla_get_u32(tb[TCA_FQ_CODEL_DROP_BATCH_SIZE])));
  383. if (tb[TCA_FQ_CODEL_MEMORY_LIMIT])
  384. WRITE_ONCE(q->memory_limit,
  385. min(1U << 31, nla_get_u32(tb[TCA_FQ_CODEL_MEMORY_LIMIT])));
  386. while (sch->q.qlen > sch->limit ||
  387. q->memory_usage > q->memory_limit) {
  388. struct sk_buff *skb = qdisc_dequeue_internal(sch, false);
  389. if (!skb)
  390. break;
  391. dropped_pkts++;
  392. dropped_bytes += qdisc_pkt_len(skb);
  393. rtnl_kfree_skbs(skb, skb);
  394. }
  395. qdisc_tree_reduce_backlog(sch, dropped_pkts, dropped_bytes);
  396. sch_tree_unlock(sch);
  397. return 0;
  398. }
  399. static void fq_codel_destroy(struct Qdisc *sch)
  400. {
  401. struct fq_codel_sched_data *q = qdisc_priv(sch);
  402. tcf_block_put(q->block);
  403. kvfree(q->backlogs);
  404. kvfree(q->flows);
  405. }
  406. static int fq_codel_init(struct Qdisc *sch, struct nlattr *opt,
  407. struct netlink_ext_ack *extack)
  408. {
  409. struct fq_codel_sched_data *q = qdisc_priv(sch);
  410. int i;
  411. int err;
  412. sch->limit = 10*1024;
  413. q->flows_cnt = 1024;
  414. q->memory_limit = 32 << 20; /* 32 MBytes */
  415. q->drop_batch_size = 64;
  416. q->quantum = psched_mtu(qdisc_dev(sch));
  417. INIT_LIST_HEAD(&q->new_flows);
  418. INIT_LIST_HEAD(&q->old_flows);
  419. codel_params_init(&q->cparams);
  420. codel_stats_init(&q->cstats);
  421. q->cparams.ecn = true;
  422. q->cparams.mtu = psched_mtu(qdisc_dev(sch));
  423. if (opt) {
  424. err = fq_codel_change(sch, opt, extack);
  425. if (err)
  426. goto init_failure;
  427. }
  428. err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
  429. if (err)
  430. goto init_failure;
  431. if (!q->flows) {
  432. q->flows = kvzalloc_objs(struct fq_codel_flow, q->flows_cnt);
  433. if (!q->flows) {
  434. err = -ENOMEM;
  435. goto init_failure;
  436. }
  437. q->backlogs = kvcalloc(q->flows_cnt, sizeof(u32), GFP_KERNEL);
  438. if (!q->backlogs) {
  439. err = -ENOMEM;
  440. goto alloc_failure;
  441. }
  442. for (i = 0; i < q->flows_cnt; i++) {
  443. struct fq_codel_flow *flow = q->flows + i;
  444. INIT_LIST_HEAD(&flow->flowchain);
  445. codel_vars_init(&flow->cvars);
  446. }
  447. }
  448. if (sch->limit >= 1)
  449. sch->flags |= TCQ_F_CAN_BYPASS;
  450. else
  451. sch->flags &= ~TCQ_F_CAN_BYPASS;
  452. sch->flags |= TCQ_F_DEQUEUE_DROPS;
  453. return 0;
  454. alloc_failure:
  455. kvfree(q->flows);
  456. q->flows = NULL;
  457. init_failure:
  458. q->flows_cnt = 0;
  459. return err;
  460. }
  461. static int fq_codel_dump(struct Qdisc *sch, struct sk_buff *skb)
  462. {
  463. struct fq_codel_sched_data *q = qdisc_priv(sch);
  464. codel_time_t ce_threshold;
  465. struct nlattr *opts;
  466. opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
  467. if (opts == NULL)
  468. goto nla_put_failure;
  469. if (nla_put_u32(skb, TCA_FQ_CODEL_TARGET,
  470. codel_time_to_us(READ_ONCE(q->cparams.target))) ||
  471. nla_put_u32(skb, TCA_FQ_CODEL_LIMIT,
  472. READ_ONCE(sch->limit)) ||
  473. nla_put_u32(skb, TCA_FQ_CODEL_INTERVAL,
  474. codel_time_to_us(READ_ONCE(q->cparams.interval))) ||
  475. nla_put_u32(skb, TCA_FQ_CODEL_ECN,
  476. READ_ONCE(q->cparams.ecn)) ||
  477. nla_put_u32(skb, TCA_FQ_CODEL_QUANTUM,
  478. READ_ONCE(q->quantum)) ||
  479. nla_put_u32(skb, TCA_FQ_CODEL_DROP_BATCH_SIZE,
  480. READ_ONCE(q->drop_batch_size)) ||
  481. nla_put_u32(skb, TCA_FQ_CODEL_MEMORY_LIMIT,
  482. READ_ONCE(q->memory_limit)) ||
  483. nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
  484. READ_ONCE(q->flows_cnt)))
  485. goto nla_put_failure;
  486. ce_threshold = READ_ONCE(q->cparams.ce_threshold);
  487. if (ce_threshold != CODEL_DISABLED_THRESHOLD) {
  488. if (nla_put_u32(skb, TCA_FQ_CODEL_CE_THRESHOLD,
  489. codel_time_to_us(ce_threshold)))
  490. goto nla_put_failure;
  491. if (nla_put_u8(skb, TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR,
  492. READ_ONCE(q->cparams.ce_threshold_selector)))
  493. goto nla_put_failure;
  494. if (nla_put_u8(skb, TCA_FQ_CODEL_CE_THRESHOLD_MASK,
  495. READ_ONCE(q->cparams.ce_threshold_mask)))
  496. goto nla_put_failure;
  497. }
  498. return nla_nest_end(skb, opts);
  499. nla_put_failure:
  500. return -1;
  501. }
  502. static int fq_codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  503. {
  504. struct fq_codel_sched_data *q = qdisc_priv(sch);
  505. struct tc_fq_codel_xstats st = {
  506. .type = TCA_FQ_CODEL_XSTATS_QDISC,
  507. };
  508. struct list_head *pos;
  509. st.qdisc_stats.maxpacket = q->cstats.maxpacket;
  510. st.qdisc_stats.drop_overlimit = q->drop_overlimit;
  511. st.qdisc_stats.ecn_mark = q->cstats.ecn_mark;
  512. st.qdisc_stats.new_flow_count = q->new_flow_count;
  513. st.qdisc_stats.ce_mark = q->cstats.ce_mark;
  514. st.qdisc_stats.memory_usage = q->memory_usage;
  515. st.qdisc_stats.drop_overmemory = q->drop_overmemory;
  516. sch_tree_lock(sch);
  517. list_for_each(pos, &q->new_flows)
  518. st.qdisc_stats.new_flows_len++;
  519. list_for_each(pos, &q->old_flows)
  520. st.qdisc_stats.old_flows_len++;
  521. sch_tree_unlock(sch);
  522. return gnet_stats_copy_app(d, &st, sizeof(st));
  523. }
  524. static struct Qdisc *fq_codel_leaf(struct Qdisc *sch, unsigned long arg)
  525. {
  526. return NULL;
  527. }
  528. static unsigned long fq_codel_find(struct Qdisc *sch, u32 classid)
  529. {
  530. return 0;
  531. }
  532. static unsigned long fq_codel_bind(struct Qdisc *sch, unsigned long parent,
  533. u32 classid)
  534. {
  535. return 0;
  536. }
  537. static void fq_codel_unbind(struct Qdisc *q, unsigned long cl)
  538. {
  539. }
  540. static struct tcf_block *fq_codel_tcf_block(struct Qdisc *sch, unsigned long cl,
  541. struct netlink_ext_ack *extack)
  542. {
  543. struct fq_codel_sched_data *q = qdisc_priv(sch);
  544. if (cl)
  545. return NULL;
  546. return q->block;
  547. }
  548. static int fq_codel_dump_class(struct Qdisc *sch, unsigned long cl,
  549. struct sk_buff *skb, struct tcmsg *tcm)
  550. {
  551. tcm->tcm_handle |= TC_H_MIN(cl);
  552. return 0;
  553. }
  554. static int fq_codel_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  555. struct gnet_dump *d)
  556. {
  557. struct fq_codel_sched_data *q = qdisc_priv(sch);
  558. u32 idx = cl - 1;
  559. struct gnet_stats_queue qs = { 0 };
  560. struct tc_fq_codel_xstats xstats;
  561. if (idx < q->flows_cnt) {
  562. const struct fq_codel_flow *flow = &q->flows[idx];
  563. const struct sk_buff *skb;
  564. memset(&xstats, 0, sizeof(xstats));
  565. xstats.type = TCA_FQ_CODEL_XSTATS_CLASS;
  566. xstats.class_stats.deficit = flow->deficit;
  567. xstats.class_stats.ldelay =
  568. codel_time_to_us(flow->cvars.ldelay);
  569. xstats.class_stats.count = flow->cvars.count;
  570. xstats.class_stats.lastcount = flow->cvars.lastcount;
  571. xstats.class_stats.dropping = flow->cvars.dropping;
  572. if (flow->cvars.dropping) {
  573. codel_tdiff_t delta = flow->cvars.drop_next -
  574. codel_get_time();
  575. xstats.class_stats.drop_next = (delta >= 0) ?
  576. codel_time_to_us(delta) :
  577. -codel_time_to_us(-delta);
  578. }
  579. if (flow->head) {
  580. sch_tree_lock(sch);
  581. skb = flow->head;
  582. while (skb) {
  583. qs.qlen++;
  584. skb = skb->next;
  585. }
  586. sch_tree_unlock(sch);
  587. }
  588. qs.backlog = q->backlogs[idx];
  589. qs.drops = 0;
  590. }
  591. if (gnet_stats_copy_queue(d, NULL, &qs, qs.qlen) < 0)
  592. return -1;
  593. if (idx < q->flows_cnt)
  594. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  595. return 0;
  596. }
  597. static void fq_codel_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  598. {
  599. struct fq_codel_sched_data *q = qdisc_priv(sch);
  600. unsigned int i;
  601. if (arg->stop)
  602. return;
  603. for (i = 0; i < q->flows_cnt; i++) {
  604. if (list_empty(&q->flows[i].flowchain)) {
  605. arg->count++;
  606. continue;
  607. }
  608. if (!tc_qdisc_stats_dump(sch, i + 1, arg))
  609. break;
  610. }
  611. }
  612. static const struct Qdisc_class_ops fq_codel_class_ops = {
  613. .leaf = fq_codel_leaf,
  614. .find = fq_codel_find,
  615. .tcf_block = fq_codel_tcf_block,
  616. .bind_tcf = fq_codel_bind,
  617. .unbind_tcf = fq_codel_unbind,
  618. .dump = fq_codel_dump_class,
  619. .dump_stats = fq_codel_dump_class_stats,
  620. .walk = fq_codel_walk,
  621. };
  622. static struct Qdisc_ops fq_codel_qdisc_ops __read_mostly = {
  623. .cl_ops = &fq_codel_class_ops,
  624. .id = "fq_codel",
  625. .priv_size = sizeof(struct fq_codel_sched_data),
  626. .enqueue = fq_codel_enqueue,
  627. .dequeue = fq_codel_dequeue,
  628. .peek = qdisc_peek_dequeued,
  629. .init = fq_codel_init,
  630. .reset = fq_codel_reset,
  631. .destroy = fq_codel_destroy,
  632. .change = fq_codel_change,
  633. .dump = fq_codel_dump,
  634. .dump_stats = fq_codel_dump_stats,
  635. .owner = THIS_MODULE,
  636. };
  637. MODULE_ALIAS_NET_SCH("fq_codel");
  638. static int __init fq_codel_module_init(void)
  639. {
  640. return register_qdisc(&fq_codel_qdisc_ops);
  641. }
  642. static void __exit fq_codel_module_exit(void)
  643. {
  644. unregister_qdisc(&fq_codel_qdisc_ops);
  645. }
  646. module_init(fq_codel_module_init)
  647. module_exit(fq_codel_module_exit)
  648. MODULE_AUTHOR("Eric Dumazet");
  649. MODULE_LICENSE("GPL");
  650. MODULE_DESCRIPTION("Fair Queue CoDel discipline");