sch_fq_codel.c 19 KB

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