sch_fq_codel.c 19 KB

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