sch_fq_codel.c 19 KB

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