sch_fq_codel.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Fair Queue CoDel discipline
  4. *
  5. * Copyright (C) 2012,2015 Eric Dumazet <[email protected]>
  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. __qdisc_drop(skb, to_free);
  154. } while (++i < max_packets && len < threshold);
  155. /* Tell codel to increase its signal strength also */
  156. flow->cvars.count += i;
  157. q->backlogs[idx] -= len;
  158. q->memory_usage -= mem;
  159. sch->qstats.drops += i;
  160. sch->qstats.backlog -= len;
  161. sch->q.qlen -= i;
  162. return idx;
  163. }
  164. static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  165. struct sk_buff **to_free)
  166. {
  167. struct fq_codel_sched_data *q = qdisc_priv(sch);
  168. unsigned int idx, prev_backlog, prev_qlen;
  169. struct fq_codel_flow *flow;
  170. int ret;
  171. unsigned int pkt_len;
  172. bool memory_limited;
  173. idx = fq_codel_classify(skb, sch, &ret);
  174. if (idx == 0) {
  175. if (ret & __NET_XMIT_BYPASS)
  176. qdisc_qstats_drop(sch);
  177. __qdisc_drop(skb, to_free);
  178. return ret;
  179. }
  180. idx--;
  181. codel_set_enqueue_time(skb);
  182. flow = &q->flows[idx];
  183. flow_queue_add(flow, skb);
  184. q->backlogs[idx] += qdisc_pkt_len(skb);
  185. qdisc_qstats_backlog_inc(sch, skb);
  186. if (list_empty(&flow->flowchain)) {
  187. list_add_tail(&flow->flowchain, &q->new_flows);
  188. q->new_flow_count++;
  189. flow->deficit = q->quantum;
  190. }
  191. get_codel_cb(skb)->mem_usage = skb->truesize;
  192. q->memory_usage += get_codel_cb(skb)->mem_usage;
  193. memory_limited = q->memory_usage > q->memory_limit;
  194. if (++sch->q.qlen <= sch->limit && !memory_limited)
  195. return NET_XMIT_SUCCESS;
  196. prev_backlog = sch->qstats.backlog;
  197. prev_qlen = sch->q.qlen;
  198. /* save this packet length as it might be dropped by fq_codel_drop() */
  199. pkt_len = qdisc_pkt_len(skb);
  200. /* fq_codel_drop() is quite expensive, as it performs a linear search
  201. * in q->backlogs[] to find a fat flow.
  202. * So instead of dropping a single packet, drop half of its backlog
  203. * with a 64 packets limit to not add a too big cpu spike here.
  204. */
  205. ret = fq_codel_drop(sch, q->drop_batch_size, to_free);
  206. prev_qlen -= sch->q.qlen;
  207. prev_backlog -= sch->qstats.backlog;
  208. q->drop_overlimit += prev_qlen;
  209. if (memory_limited)
  210. q->drop_overmemory += prev_qlen;
  211. /* As we dropped packet(s), better let upper stack know this.
  212. * If we dropped a packet for this flow, return NET_XMIT_CN,
  213. * but in this case, our parents wont increase their backlogs.
  214. */
  215. if (ret == idx) {
  216. qdisc_tree_reduce_backlog(sch, prev_qlen - 1,
  217. prev_backlog - pkt_len);
  218. return NET_XMIT_CN;
  219. }
  220. qdisc_tree_reduce_backlog(sch, prev_qlen, prev_backlog);
  221. return NET_XMIT_SUCCESS;
  222. }
  223. /* This is the specific function called from codel_dequeue()
  224. * to dequeue a packet from queue. Note: backlog is handled in
  225. * codel, we dont need to reduce it here.
  226. */
  227. static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
  228. {
  229. struct Qdisc *sch = ctx;
  230. struct fq_codel_sched_data *q = qdisc_priv(sch);
  231. struct fq_codel_flow *flow;
  232. struct sk_buff *skb = NULL;
  233. flow = container_of(vars, struct fq_codel_flow, cvars);
  234. if (flow->head) {
  235. skb = dequeue_head(flow);
  236. q->backlogs[flow - q->flows] -= qdisc_pkt_len(skb);
  237. q->memory_usage -= get_codel_cb(skb)->mem_usage;
  238. sch->q.qlen--;
  239. sch->qstats.backlog -= qdisc_pkt_len(skb);
  240. }
  241. return skb;
  242. }
  243. static void drop_func(struct sk_buff *skb, void *ctx)
  244. {
  245. struct Qdisc *sch = ctx;
  246. kfree_skb(skb);
  247. qdisc_qstats_drop(sch);
  248. }
  249. static struct sk_buff *fq_codel_dequeue(struct Qdisc *sch)
  250. {
  251. struct fq_codel_sched_data *q = qdisc_priv(sch);
  252. struct sk_buff *skb;
  253. struct fq_codel_flow *flow;
  254. struct list_head *head;
  255. begin:
  256. head = &q->new_flows;
  257. if (list_empty(head)) {
  258. head = &q->old_flows;
  259. if (list_empty(head))
  260. return NULL;
  261. }
  262. flow = list_first_entry(head, struct fq_codel_flow, flowchain);
  263. if (flow->deficit <= 0) {
  264. flow->deficit += q->quantum;
  265. list_move_tail(&flow->flowchain, &q->old_flows);
  266. goto begin;
  267. }
  268. skb = codel_dequeue(sch, &sch->qstats.backlog, &q->cparams,
  269. &flow->cvars, &q->cstats, qdisc_pkt_len,
  270. codel_get_enqueue_time, drop_func, dequeue_func);
  271. if (!skb) {
  272. /* force a pass through old_flows to prevent starvation */
  273. if ((head == &q->new_flows) && !list_empty(&q->old_flows))
  274. list_move_tail(&flow->flowchain, &q->old_flows);
  275. else
  276. list_del_init(&flow->flowchain);
  277. goto begin;
  278. }
  279. qdisc_bstats_update(sch, skb);
  280. flow->deficit -= qdisc_pkt_len(skb);
  281. /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
  282. * or HTB crashes. Defer it for next round.
  283. */
  284. if (q->cstats.drop_count && sch->q.qlen) {
  285. qdisc_tree_reduce_backlog(sch, q->cstats.drop_count,
  286. q->cstats.drop_len);
  287. q->cstats.drop_count = 0;
  288. q->cstats.drop_len = 0;
  289. }
  290. return skb;
  291. }
  292. static void fq_codel_flow_purge(struct fq_codel_flow *flow)
  293. {
  294. rtnl_kfree_skbs(flow->head, flow->tail);
  295. flow->head = NULL;
  296. }
  297. static void fq_codel_reset(struct Qdisc *sch)
  298. {
  299. struct fq_codel_sched_data *q = qdisc_priv(sch);
  300. int i;
  301. INIT_LIST_HEAD(&q->new_flows);
  302. INIT_LIST_HEAD(&q->old_flows);
  303. for (i = 0; i < q->flows_cnt; i++) {
  304. struct fq_codel_flow *flow = q->flows + i;
  305. fq_codel_flow_purge(flow);
  306. INIT_LIST_HEAD(&flow->flowchain);
  307. codel_vars_init(&flow->cvars);
  308. }
  309. memset(q->backlogs, 0, q->flows_cnt * sizeof(u32));
  310. q->memory_usage = 0;
  311. }
  312. static const struct nla_policy fq_codel_policy[TCA_FQ_CODEL_MAX + 1] = {
  313. [TCA_FQ_CODEL_TARGET] = { .type = NLA_U32 },
  314. [TCA_FQ_CODEL_LIMIT] = { .type = NLA_U32 },
  315. [TCA_FQ_CODEL_INTERVAL] = { .type = NLA_U32 },
  316. [TCA_FQ_CODEL_ECN] = { .type = NLA_U32 },
  317. [TCA_FQ_CODEL_FLOWS] = { .type = NLA_U32 },
  318. [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 },
  319. [TCA_FQ_CODEL_CE_THRESHOLD] = { .type = NLA_U32 },
  320. [TCA_FQ_CODEL_DROP_BATCH_SIZE] = { .type = NLA_U32 },
  321. [TCA_FQ_CODEL_MEMORY_LIMIT] = { .type = NLA_U32 },
  322. [TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR] = { .type = NLA_U8 },
  323. [TCA_FQ_CODEL_CE_THRESHOLD_MASK] = { .type = NLA_U8 },
  324. };
  325. static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt,
  326. struct netlink_ext_ack *extack)
  327. {
  328. struct fq_codel_sched_data *q = qdisc_priv(sch);
  329. struct nlattr *tb[TCA_FQ_CODEL_MAX + 1];
  330. u32 quantum = 0;
  331. int err;
  332. err = nla_parse_nested_deprecated(tb, TCA_FQ_CODEL_MAX, opt,
  333. fq_codel_policy, NULL);
  334. if (err < 0)
  335. return err;
  336. if (tb[TCA_FQ_CODEL_FLOWS]) {
  337. if (q->flows)
  338. return -EINVAL;
  339. q->flows_cnt = nla_get_u32(tb[TCA_FQ_CODEL_FLOWS]);
  340. if (!q->flows_cnt ||
  341. q->flows_cnt > 65536)
  342. return -EINVAL;
  343. }
  344. if (tb[TCA_FQ_CODEL_QUANTUM]) {
  345. quantum = max(256U, nla_get_u32(tb[TCA_FQ_CODEL_QUANTUM]));
  346. if (quantum > FQ_CODEL_QUANTUM_MAX) {
  347. NL_SET_ERR_MSG(extack, "Invalid quantum");
  348. return -EINVAL;
  349. }
  350. }
  351. sch_tree_lock(sch);
  352. if (tb[TCA_FQ_CODEL_TARGET]) {
  353. u64 target = nla_get_u32(tb[TCA_FQ_CODEL_TARGET]);
  354. q->cparams.target = (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. q->cparams.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT;
  359. }
  360. if (tb[TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR])
  361. q->cparams.ce_threshold_selector = nla_get_u8(tb[TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR]);
  362. if (tb[TCA_FQ_CODEL_CE_THRESHOLD_MASK])
  363. q->cparams.ce_threshold_mask = nla_get_u8(tb[TCA_FQ_CODEL_CE_THRESHOLD_MASK]);
  364. if (tb[TCA_FQ_CODEL_INTERVAL]) {
  365. u64 interval = nla_get_u32(tb[TCA_FQ_CODEL_INTERVAL]);
  366. q->cparams.interval = (interval * NSEC_PER_USEC) >> CODEL_SHIFT;
  367. }
  368. if (tb[TCA_FQ_CODEL_LIMIT])
  369. sch->limit = nla_get_u32(tb[TCA_FQ_CODEL_LIMIT]);
  370. if (tb[TCA_FQ_CODEL_ECN])
  371. q->cparams.ecn = !!nla_get_u32(tb[TCA_FQ_CODEL_ECN]);
  372. if (quantum)
  373. q->quantum = quantum;
  374. if (tb[TCA_FQ_CODEL_DROP_BATCH_SIZE])
  375. q->drop_batch_size = max(1U, nla_get_u32(tb[TCA_FQ_CODEL_DROP_BATCH_SIZE]));
  376. if (tb[TCA_FQ_CODEL_MEMORY_LIMIT])
  377. q->memory_limit = min(1U << 31, nla_get_u32(tb[TCA_FQ_CODEL_MEMORY_LIMIT]));
  378. while (sch->q.qlen > sch->limit ||
  379. q->memory_usage > q->memory_limit) {
  380. struct sk_buff *skb = fq_codel_dequeue(sch);
  381. q->cstats.drop_len += qdisc_pkt_len(skb);
  382. rtnl_kfree_skbs(skb, skb);
  383. q->cstats.drop_count++;
  384. }
  385. qdisc_tree_reduce_backlog(sch, q->cstats.drop_count, q->cstats.drop_len);
  386. q->cstats.drop_count = 0;
  387. q->cstats.drop_len = 0;
  388. sch_tree_unlock(sch);
  389. return 0;
  390. }
  391. static void fq_codel_destroy(struct Qdisc *sch)
  392. {
  393. struct fq_codel_sched_data *q = qdisc_priv(sch);
  394. tcf_block_put(q->block);
  395. kvfree(q->backlogs);
  396. kvfree(q->flows);
  397. }
  398. static int fq_codel_init(struct Qdisc *sch, struct nlattr *opt,
  399. struct netlink_ext_ack *extack)
  400. {
  401. struct fq_codel_sched_data *q = qdisc_priv(sch);
  402. int i;
  403. int err;
  404. sch->limit = 10*1024;
  405. q->flows_cnt = 1024;
  406. q->memory_limit = 32 << 20; /* 32 MBytes */
  407. q->drop_batch_size = 64;
  408. q->quantum = psched_mtu(qdisc_dev(sch));
  409. INIT_LIST_HEAD(&q->new_flows);
  410. INIT_LIST_HEAD(&q->old_flows);
  411. codel_params_init(&q->cparams);
  412. codel_stats_init(&q->cstats);
  413. q->cparams.ecn = true;
  414. q->cparams.mtu = psched_mtu(qdisc_dev(sch));
  415. if (opt) {
  416. err = fq_codel_change(sch, opt, extack);
  417. if (err)
  418. goto init_failure;
  419. }
  420. err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
  421. if (err)
  422. goto init_failure;
  423. if (!q->flows) {
  424. q->flows = kvcalloc(q->flows_cnt,
  425. sizeof(struct fq_codel_flow),
  426. GFP_KERNEL);
  427. if (!q->flows) {
  428. err = -ENOMEM;
  429. goto init_failure;
  430. }
  431. q->backlogs = kvcalloc(q->flows_cnt, sizeof(u32), GFP_KERNEL);
  432. if (!q->backlogs) {
  433. err = -ENOMEM;
  434. goto alloc_failure;
  435. }
  436. for (i = 0; i < q->flows_cnt; i++) {
  437. struct fq_codel_flow *flow = q->flows + i;
  438. INIT_LIST_HEAD(&flow->flowchain);
  439. codel_vars_init(&flow->cvars);
  440. }
  441. }
  442. if (sch->limit >= 1)
  443. sch->flags |= TCQ_F_CAN_BYPASS;
  444. else
  445. sch->flags &= ~TCQ_F_CAN_BYPASS;
  446. return 0;
  447. alloc_failure:
  448. kvfree(q->flows);
  449. q->flows = NULL;
  450. init_failure:
  451. q->flows_cnt = 0;
  452. return err;
  453. }
  454. static int fq_codel_dump(struct Qdisc *sch, struct sk_buff *skb)
  455. {
  456. struct fq_codel_sched_data *q = qdisc_priv(sch);
  457. struct nlattr *opts;
  458. opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
  459. if (opts == NULL)
  460. goto nla_put_failure;
  461. if (nla_put_u32(skb, TCA_FQ_CODEL_TARGET,
  462. codel_time_to_us(q->cparams.target)) ||
  463. nla_put_u32(skb, TCA_FQ_CODEL_LIMIT,
  464. sch->limit) ||
  465. nla_put_u32(skb, TCA_FQ_CODEL_INTERVAL,
  466. codel_time_to_us(q->cparams.interval)) ||
  467. nla_put_u32(skb, TCA_FQ_CODEL_ECN,
  468. q->cparams.ecn) ||
  469. nla_put_u32(skb, TCA_FQ_CODEL_QUANTUM,
  470. q->quantum) ||
  471. nla_put_u32(skb, TCA_FQ_CODEL_DROP_BATCH_SIZE,
  472. q->drop_batch_size) ||
  473. nla_put_u32(skb, TCA_FQ_CODEL_MEMORY_LIMIT,
  474. q->memory_limit) ||
  475. nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
  476. q->flows_cnt))
  477. goto nla_put_failure;
  478. if (q->cparams.ce_threshold != CODEL_DISABLED_THRESHOLD) {
  479. if (nla_put_u32(skb, TCA_FQ_CODEL_CE_THRESHOLD,
  480. codel_time_to_us(q->cparams.ce_threshold)))
  481. goto nla_put_failure;
  482. if (nla_put_u8(skb, TCA_FQ_CODEL_CE_THRESHOLD_SELECTOR, q->cparams.ce_threshold_selector))
  483. goto nla_put_failure;
  484. if (nla_put_u8(skb, TCA_FQ_CODEL_CE_THRESHOLD_MASK, q->cparams.ce_threshold_mask))
  485. goto nla_put_failure;
  486. }
  487. return nla_nest_end(skb, opts);
  488. nla_put_failure:
  489. return -1;
  490. }
  491. static int fq_codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  492. {
  493. struct fq_codel_sched_data *q = qdisc_priv(sch);
  494. struct tc_fq_codel_xstats st = {
  495. .type = TCA_FQ_CODEL_XSTATS_QDISC,
  496. };
  497. struct list_head *pos;
  498. st.qdisc_stats.maxpacket = q->cstats.maxpacket;
  499. st.qdisc_stats.drop_overlimit = q->drop_overlimit;
  500. st.qdisc_stats.ecn_mark = q->cstats.ecn_mark;
  501. st.qdisc_stats.new_flow_count = q->new_flow_count;
  502. st.qdisc_stats.ce_mark = q->cstats.ce_mark;
  503. st.qdisc_stats.memory_usage = q->memory_usage;
  504. st.qdisc_stats.drop_overmemory = q->drop_overmemory;
  505. sch_tree_lock(sch);
  506. list_for_each(pos, &q->new_flows)
  507. st.qdisc_stats.new_flows_len++;
  508. list_for_each(pos, &q->old_flows)
  509. st.qdisc_stats.old_flows_len++;
  510. sch_tree_unlock(sch);
  511. return gnet_stats_copy_app(d, &st, sizeof(st));
  512. }
  513. static struct Qdisc *fq_codel_leaf(struct Qdisc *sch, unsigned long arg)
  514. {
  515. return NULL;
  516. }
  517. static unsigned long fq_codel_find(struct Qdisc *sch, u32 classid)
  518. {
  519. return 0;
  520. }
  521. static unsigned long fq_codel_bind(struct Qdisc *sch, unsigned long parent,
  522. u32 classid)
  523. {
  524. return 0;
  525. }
  526. static void fq_codel_unbind(struct Qdisc *q, unsigned long cl)
  527. {
  528. }
  529. static struct tcf_block *fq_codel_tcf_block(struct Qdisc *sch, unsigned long cl,
  530. struct netlink_ext_ack *extack)
  531. {
  532. struct fq_codel_sched_data *q = qdisc_priv(sch);
  533. if (cl)
  534. return NULL;
  535. return q->block;
  536. }
  537. static int fq_codel_dump_class(struct Qdisc *sch, unsigned long cl,
  538. struct sk_buff *skb, struct tcmsg *tcm)
  539. {
  540. tcm->tcm_handle |= TC_H_MIN(cl);
  541. return 0;
  542. }
  543. static int fq_codel_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  544. struct gnet_dump *d)
  545. {
  546. struct fq_codel_sched_data *q = qdisc_priv(sch);
  547. u32 idx = cl - 1;
  548. struct gnet_stats_queue qs = { 0 };
  549. struct tc_fq_codel_xstats xstats;
  550. if (idx < q->flows_cnt) {
  551. const struct fq_codel_flow *flow = &q->flows[idx];
  552. const struct sk_buff *skb;
  553. memset(&xstats, 0, sizeof(xstats));
  554. xstats.type = TCA_FQ_CODEL_XSTATS_CLASS;
  555. xstats.class_stats.deficit = flow->deficit;
  556. xstats.class_stats.ldelay =
  557. codel_time_to_us(flow->cvars.ldelay);
  558. xstats.class_stats.count = flow->cvars.count;
  559. xstats.class_stats.lastcount = flow->cvars.lastcount;
  560. xstats.class_stats.dropping = flow->cvars.dropping;
  561. if (flow->cvars.dropping) {
  562. codel_tdiff_t delta = flow->cvars.drop_next -
  563. codel_get_time();
  564. xstats.class_stats.drop_next = (delta >= 0) ?
  565. codel_time_to_us(delta) :
  566. -codel_time_to_us(-delta);
  567. }
  568. if (flow->head) {
  569. sch_tree_lock(sch);
  570. skb = flow->head;
  571. while (skb) {
  572. qs.qlen++;
  573. skb = skb->next;
  574. }
  575. sch_tree_unlock(sch);
  576. }
  577. qs.backlog = q->backlogs[idx];
  578. qs.drops = 0;
  579. }
  580. if (gnet_stats_copy_queue(d, NULL, &qs, qs.qlen) < 0)
  581. return -1;
  582. if (idx < q->flows_cnt)
  583. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  584. return 0;
  585. }
  586. static void fq_codel_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  587. {
  588. struct fq_codel_sched_data *q = qdisc_priv(sch);
  589. unsigned int i;
  590. if (arg->stop)
  591. return;
  592. for (i = 0; i < q->flows_cnt; i++) {
  593. if (list_empty(&q->flows[i].flowchain)) {
  594. arg->count++;
  595. continue;
  596. }
  597. if (!tc_qdisc_stats_dump(sch, i + 1, arg))
  598. break;
  599. }
  600. }
  601. static const struct Qdisc_class_ops fq_codel_class_ops = {
  602. .leaf = fq_codel_leaf,
  603. .find = fq_codel_find,
  604. .tcf_block = fq_codel_tcf_block,
  605. .bind_tcf = fq_codel_bind,
  606. .unbind_tcf = fq_codel_unbind,
  607. .dump = fq_codel_dump_class,
  608. .dump_stats = fq_codel_dump_class_stats,
  609. .walk = fq_codel_walk,
  610. };
  611. static struct Qdisc_ops fq_codel_qdisc_ops __read_mostly = {
  612. .cl_ops = &fq_codel_class_ops,
  613. .id = "fq_codel",
  614. .priv_size = sizeof(struct fq_codel_sched_data),
  615. .enqueue = fq_codel_enqueue,
  616. .dequeue = fq_codel_dequeue,
  617. .peek = qdisc_peek_dequeued,
  618. .init = fq_codel_init,
  619. .reset = fq_codel_reset,
  620. .destroy = fq_codel_destroy,
  621. .change = fq_codel_change,
  622. .dump = fq_codel_dump,
  623. .dump_stats = fq_codel_dump_stats,
  624. .owner = THIS_MODULE,
  625. };
  626. static int __init fq_codel_module_init(void)
  627. {
  628. return register_qdisc(&fq_codel_qdisc_ops);
  629. }
  630. static void __exit fq_codel_module_exit(void)
  631. {
  632. unregister_qdisc(&fq_codel_qdisc_ops);
  633. }
  634. module_init(fq_codel_module_init)
  635. module_exit(fq_codel_module_exit)
  636. MODULE_AUTHOR("Eric Dumazet");
  637. MODULE_LICENSE("GPL");
  638. MODULE_DESCRIPTION("Fair Queue CoDel discipline");