sch_fq_codel.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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/codel.h>
  26. #include <net/codel_impl.h>
  27. #include <net/codel_qdisc.h>
  28. /* Fair Queue CoDel.
  29. *
  30. * Principles :
  31. * Packets are classified (internal classifier or external) on flows.
  32. * This is a Stochastic model (as we use a hash, several flows
  33. * might be hashed on same slot)
  34. * Each flow has a CoDel managed queue.
  35. * Flows are linked onto two (Round Robin) lists,
  36. * so that new flows have priority on old ones.
  37. *
  38. * For a given flow, packets are not reordered (CoDel uses a FIFO)
  39. * head drops only.
  40. * ECN capability is on by default.
  41. * Low memory footprint (64 bytes per flow)
  42. */
  43. struct fq_codel_flow {
  44. struct sk_buff *head;
  45. struct sk_buff *tail;
  46. struct list_head flowchain;
  47. int deficit;
  48. u32 dropped; /* number of drops (or ECN marks) on this flow */
  49. struct codel_vars cvars;
  50. }; /* please try to keep this structure <= 64 bytes */
  51. struct fq_codel_sched_data {
  52. struct tcf_proto __rcu *filter_list; /* optional external classifier */
  53. struct fq_codel_flow *flows; /* Flows table [flows_cnt] */
  54. u32 *backlogs; /* backlog table [flows_cnt] */
  55. u32 flows_cnt; /* number of flows */
  56. u32 perturbation; /* hash perturbation */
  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. u32 hash = skb_get_hash_perturb(skb, q->perturbation);
  73. return reciprocal_scale(hash, 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 = tc_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. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  97. case TC_ACT_SHOT:
  98. return 0;
  99. }
  100. #endif
  101. if (TC_H_MIN(res.classid) <= q->flows_cnt)
  102. return TC_H_MIN(res.classid);
  103. }
  104. return 0;
  105. }
  106. /* helper functions : might be changed when/if skb use a standard list_head */
  107. /* remove one skb from head of slot queue */
  108. static inline struct sk_buff *dequeue_head(struct fq_codel_flow *flow)
  109. {
  110. struct sk_buff *skb = flow->head;
  111. flow->head = skb->next;
  112. skb->next = NULL;
  113. return skb;
  114. }
  115. /* add skb to flow queue (tail add) */
  116. static inline void flow_queue_add(struct fq_codel_flow *flow,
  117. struct sk_buff *skb)
  118. {
  119. if (flow->head == NULL)
  120. flow->head = skb;
  121. else
  122. flow->tail->next = skb;
  123. flow->tail = skb;
  124. skb->next = NULL;
  125. }
  126. static unsigned int fq_codel_drop(struct Qdisc *sch, unsigned int max_packets,
  127. struct sk_buff **to_free)
  128. {
  129. struct fq_codel_sched_data *q = qdisc_priv(sch);
  130. struct sk_buff *skb;
  131. unsigned int maxbacklog = 0, idx = 0, i, len;
  132. struct fq_codel_flow *flow;
  133. unsigned int threshold;
  134. unsigned int mem = 0;
  135. /* Queue is full! Find the fat flow and drop packet(s) from it.
  136. * This might sound expensive, but with 1024 flows, we scan
  137. * 4KB of memory, and we dont need to handle a complex tree
  138. * in fast path (packet queue/enqueue) with many cache misses.
  139. * In stress mode, we'll try to drop 64 packets from the flow,
  140. * amortizing this linear lookup to one cache line per drop.
  141. */
  142. for (i = 0; i < q->flows_cnt; i++) {
  143. if (q->backlogs[i] > maxbacklog) {
  144. maxbacklog = q->backlogs[i];
  145. idx = i;
  146. }
  147. }
  148. /* Our goal is to drop half of this fat flow backlog */
  149. threshold = maxbacklog >> 1;
  150. flow = &q->flows[idx];
  151. len = 0;
  152. i = 0;
  153. do {
  154. skb = dequeue_head(flow);
  155. len += qdisc_pkt_len(skb);
  156. mem += get_codel_cb(skb)->mem_usage;
  157. __qdisc_drop(skb, to_free);
  158. } while (++i < max_packets && len < threshold);
  159. flow->dropped += i;
  160. q->backlogs[idx] -= len;
  161. q->memory_usage -= mem;
  162. sch->qstats.drops += i;
  163. sch->qstats.backlog -= len;
  164. sch->q.qlen -= i;
  165. return idx;
  166. }
  167. static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  168. struct sk_buff **to_free)
  169. {
  170. struct fq_codel_sched_data *q = qdisc_priv(sch);
  171. unsigned int idx, prev_backlog, prev_qlen;
  172. struct fq_codel_flow *flow;
  173. int uninitialized_var(ret);
  174. unsigned int pkt_len;
  175. bool memory_limited;
  176. idx = fq_codel_classify(skb, sch, &ret);
  177. if (idx == 0) {
  178. if (ret & __NET_XMIT_BYPASS)
  179. qdisc_qstats_drop(sch);
  180. __qdisc_drop(skb, to_free);
  181. return ret;
  182. }
  183. idx--;
  184. codel_set_enqueue_time(skb);
  185. flow = &q->flows[idx];
  186. flow_queue_add(flow, skb);
  187. q->backlogs[idx] += qdisc_pkt_len(skb);
  188. qdisc_qstats_backlog_inc(sch, skb);
  189. if (list_empty(&flow->flowchain)) {
  190. list_add_tail(&flow->flowchain, &q->new_flows);
  191. q->new_flow_count++;
  192. flow->deficit = q->quantum;
  193. flow->dropped = 0;
  194. }
  195. get_codel_cb(skb)->mem_usage = skb->truesize;
  196. q->memory_usage += get_codel_cb(skb)->mem_usage;
  197. memory_limited = q->memory_usage > q->memory_limit;
  198. if (++sch->q.qlen <= sch->limit && !memory_limited)
  199. return NET_XMIT_SUCCESS;
  200. prev_backlog = sch->qstats.backlog;
  201. prev_qlen = sch->q.qlen;
  202. /* save this packet length as it might be dropped by fq_codel_drop() */
  203. pkt_len = qdisc_pkt_len(skb);
  204. /* fq_codel_drop() is quite expensive, as it performs a linear search
  205. * in q->backlogs[] to find a fat flow.
  206. * So instead of dropping a single packet, drop half of its backlog
  207. * with a 64 packets limit to not add a too big cpu spike here.
  208. */
  209. ret = fq_codel_drop(sch, q->drop_batch_size, to_free);
  210. prev_qlen -= sch->q.qlen;
  211. prev_backlog -= sch->qstats.backlog;
  212. q->drop_overlimit += prev_qlen;
  213. if (memory_limited)
  214. q->drop_overmemory += prev_qlen;
  215. /* As we dropped packet(s), better let upper stack know this.
  216. * If we dropped a packet for this flow, return NET_XMIT_CN,
  217. * but in this case, our parents wont increase their backlogs.
  218. */
  219. if (ret == idx) {
  220. qdisc_tree_reduce_backlog(sch, prev_qlen - 1,
  221. prev_backlog - pkt_len);
  222. return NET_XMIT_CN;
  223. }
  224. qdisc_tree_reduce_backlog(sch, prev_qlen, prev_backlog);
  225. return NET_XMIT_SUCCESS;
  226. }
  227. /* This is the specific function called from codel_dequeue()
  228. * to dequeue a packet from queue. Note: backlog is handled in
  229. * codel, we dont need to reduce it here.
  230. */
  231. static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
  232. {
  233. struct Qdisc *sch = ctx;
  234. struct fq_codel_sched_data *q = qdisc_priv(sch);
  235. struct fq_codel_flow *flow;
  236. struct sk_buff *skb = NULL;
  237. flow = container_of(vars, struct fq_codel_flow, cvars);
  238. if (flow->head) {
  239. skb = dequeue_head(flow);
  240. q->backlogs[flow - q->flows] -= qdisc_pkt_len(skb);
  241. q->memory_usage -= get_codel_cb(skb)->mem_usage;
  242. sch->q.qlen--;
  243. sch->qstats.backlog -= qdisc_pkt_len(skb);
  244. }
  245. return skb;
  246. }
  247. static void drop_func(struct sk_buff *skb, void *ctx)
  248. {
  249. struct Qdisc *sch = ctx;
  250. kfree_skb(skb);
  251. qdisc_qstats_drop(sch);
  252. }
  253. static struct sk_buff *fq_codel_dequeue(struct Qdisc *sch)
  254. {
  255. struct fq_codel_sched_data *q = qdisc_priv(sch);
  256. struct sk_buff *skb;
  257. struct fq_codel_flow *flow;
  258. struct list_head *head;
  259. u32 prev_drop_count, prev_ecn_mark;
  260. unsigned int prev_backlog;
  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. prev_backlog = sch->qstats.backlog;
  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. {
  338. struct fq_codel_sched_data *q = qdisc_priv(sch);
  339. struct nlattr *tb[TCA_FQ_CODEL_MAX + 1];
  340. int err;
  341. if (!opt)
  342. return -EINVAL;
  343. err = nla_parse_nested(tb, TCA_FQ_CODEL_MAX, opt, fq_codel_policy);
  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 = min(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_zalloc(size_t sz)
  391. {
  392. void *ptr = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN);
  393. if (!ptr)
  394. ptr = vzalloc(sz);
  395. return ptr;
  396. }
  397. static void fq_codel_free(void *addr)
  398. {
  399. kvfree(addr);
  400. }
  401. static void fq_codel_destroy(struct Qdisc *sch)
  402. {
  403. struct fq_codel_sched_data *q = qdisc_priv(sch);
  404. tcf_destroy_chain(&q->filter_list);
  405. fq_codel_free(q->backlogs);
  406. fq_codel_free(q->flows);
  407. }
  408. static int fq_codel_init(struct Qdisc *sch, struct nlattr *opt)
  409. {
  410. struct fq_codel_sched_data *q = qdisc_priv(sch);
  411. int i;
  412. sch->limit = 10*1024;
  413. q->flows_cnt = 1024;
  414. q->memory_limit = 32 << 20; /* 32 MBytes */
  415. q->drop_batch_size = 64;
  416. q->quantum = psched_mtu(qdisc_dev(sch));
  417. q->perturbation = prandom_u32();
  418. INIT_LIST_HEAD(&q->new_flows);
  419. INIT_LIST_HEAD(&q->old_flows);
  420. codel_params_init(&q->cparams);
  421. codel_stats_init(&q->cstats);
  422. q->cparams.ecn = true;
  423. q->cparams.mtu = psched_mtu(qdisc_dev(sch));
  424. if (opt) {
  425. int err = fq_codel_change(sch, opt);
  426. if (err)
  427. return err;
  428. }
  429. if (!q->flows) {
  430. q->flows = fq_codel_zalloc(q->flows_cnt *
  431. sizeof(struct fq_codel_flow));
  432. if (!q->flows)
  433. return -ENOMEM;
  434. q->backlogs = fq_codel_zalloc(q->flows_cnt * sizeof(u32));
  435. if (!q->backlogs) {
  436. fq_codel_free(q->flows);
  437. return -ENOMEM;
  438. }
  439. for (i = 0; i < q->flows_cnt; i++) {
  440. struct fq_codel_flow *flow = q->flows + i;
  441. INIT_LIST_HEAD(&flow->flowchain);
  442. codel_vars_init(&flow->cvars);
  443. }
  444. }
  445. if (sch->limit >= 1)
  446. sch->flags |= TCQ_F_CAN_BYPASS;
  447. else
  448. sch->flags &= ~TCQ_F_CAN_BYPASS;
  449. return 0;
  450. }
  451. static int fq_codel_dump(struct Qdisc *sch, struct sk_buff *skb)
  452. {
  453. struct fq_codel_sched_data *q = qdisc_priv(sch);
  454. struct nlattr *opts;
  455. opts = nla_nest_start(skb, TCA_OPTIONS);
  456. if (opts == NULL)
  457. goto nla_put_failure;
  458. if (nla_put_u32(skb, TCA_FQ_CODEL_TARGET,
  459. codel_time_to_us(q->cparams.target)) ||
  460. nla_put_u32(skb, TCA_FQ_CODEL_LIMIT,
  461. sch->limit) ||
  462. nla_put_u32(skb, TCA_FQ_CODEL_INTERVAL,
  463. codel_time_to_us(q->cparams.interval)) ||
  464. nla_put_u32(skb, TCA_FQ_CODEL_ECN,
  465. q->cparams.ecn) ||
  466. nla_put_u32(skb, TCA_FQ_CODEL_QUANTUM,
  467. q->quantum) ||
  468. nla_put_u32(skb, TCA_FQ_CODEL_DROP_BATCH_SIZE,
  469. q->drop_batch_size) ||
  470. nla_put_u32(skb, TCA_FQ_CODEL_MEMORY_LIMIT,
  471. q->memory_limit) ||
  472. nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
  473. q->flows_cnt))
  474. goto nla_put_failure;
  475. if (q->cparams.ce_threshold != CODEL_DISABLED_THRESHOLD &&
  476. nla_put_u32(skb, TCA_FQ_CODEL_CE_THRESHOLD,
  477. codel_time_to_us(q->cparams.ce_threshold)))
  478. goto nla_put_failure;
  479. return nla_nest_end(skb, opts);
  480. nla_put_failure:
  481. return -1;
  482. }
  483. static int fq_codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  484. {
  485. struct fq_codel_sched_data *q = qdisc_priv(sch);
  486. struct tc_fq_codel_xstats st = {
  487. .type = TCA_FQ_CODEL_XSTATS_QDISC,
  488. };
  489. struct list_head *pos;
  490. st.qdisc_stats.maxpacket = q->cstats.maxpacket;
  491. st.qdisc_stats.drop_overlimit = q->drop_overlimit;
  492. st.qdisc_stats.ecn_mark = q->cstats.ecn_mark;
  493. st.qdisc_stats.new_flow_count = q->new_flow_count;
  494. st.qdisc_stats.ce_mark = q->cstats.ce_mark;
  495. st.qdisc_stats.memory_usage = q->memory_usage;
  496. st.qdisc_stats.drop_overmemory = q->drop_overmemory;
  497. sch_tree_lock(sch);
  498. list_for_each(pos, &q->new_flows)
  499. st.qdisc_stats.new_flows_len++;
  500. list_for_each(pos, &q->old_flows)
  501. st.qdisc_stats.old_flows_len++;
  502. sch_tree_unlock(sch);
  503. return gnet_stats_copy_app(d, &st, sizeof(st));
  504. }
  505. static struct Qdisc *fq_codel_leaf(struct Qdisc *sch, unsigned long arg)
  506. {
  507. return NULL;
  508. }
  509. static unsigned long fq_codel_get(struct Qdisc *sch, u32 classid)
  510. {
  511. return 0;
  512. }
  513. static unsigned long fq_codel_bind(struct Qdisc *sch, unsigned long parent,
  514. u32 classid)
  515. {
  516. /* we cannot bypass queue discipline anymore */
  517. sch->flags &= ~TCQ_F_CAN_BYPASS;
  518. return 0;
  519. }
  520. static void fq_codel_put(struct Qdisc *q, unsigned long cl)
  521. {
  522. }
  523. static struct tcf_proto __rcu **fq_codel_find_tcf(struct Qdisc *sch,
  524. unsigned long cl)
  525. {
  526. struct fq_codel_sched_data *q = qdisc_priv(sch);
  527. if (cl)
  528. return NULL;
  529. return &q->filter_list;
  530. }
  531. static int fq_codel_dump_class(struct Qdisc *sch, unsigned long cl,
  532. struct sk_buff *skb, struct tcmsg *tcm)
  533. {
  534. tcm->tcm_handle |= TC_H_MIN(cl);
  535. return 0;
  536. }
  537. static int fq_codel_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  538. struct gnet_dump *d)
  539. {
  540. struct fq_codel_sched_data *q = qdisc_priv(sch);
  541. u32 idx = cl - 1;
  542. struct gnet_stats_queue qs = { 0 };
  543. struct tc_fq_codel_xstats xstats;
  544. if (idx < q->flows_cnt) {
  545. const struct fq_codel_flow *flow = &q->flows[idx];
  546. const struct sk_buff *skb;
  547. memset(&xstats, 0, sizeof(xstats));
  548. xstats.type = TCA_FQ_CODEL_XSTATS_CLASS;
  549. xstats.class_stats.deficit = flow->deficit;
  550. xstats.class_stats.ldelay =
  551. codel_time_to_us(flow->cvars.ldelay);
  552. xstats.class_stats.count = flow->cvars.count;
  553. xstats.class_stats.lastcount = flow->cvars.lastcount;
  554. xstats.class_stats.dropping = flow->cvars.dropping;
  555. if (flow->cvars.dropping) {
  556. codel_tdiff_t delta = flow->cvars.drop_next -
  557. codel_get_time();
  558. xstats.class_stats.drop_next = (delta >= 0) ?
  559. codel_time_to_us(delta) :
  560. -codel_time_to_us(-delta);
  561. }
  562. if (flow->head) {
  563. sch_tree_lock(sch);
  564. skb = flow->head;
  565. while (skb) {
  566. qs.qlen++;
  567. skb = skb->next;
  568. }
  569. sch_tree_unlock(sch);
  570. }
  571. qs.backlog = q->backlogs[idx];
  572. qs.drops = flow->dropped;
  573. }
  574. if (gnet_stats_copy_queue(d, NULL, &qs, qs.qlen) < 0)
  575. return -1;
  576. if (idx < q->flows_cnt)
  577. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  578. return 0;
  579. }
  580. static void fq_codel_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  581. {
  582. struct fq_codel_sched_data *q = qdisc_priv(sch);
  583. unsigned int i;
  584. if (arg->stop)
  585. return;
  586. for (i = 0; i < q->flows_cnt; i++) {
  587. if (list_empty(&q->flows[i].flowchain) ||
  588. arg->count < arg->skip) {
  589. arg->count++;
  590. continue;
  591. }
  592. if (arg->fn(sch, i + 1, arg) < 0) {
  593. arg->stop = 1;
  594. break;
  595. }
  596. arg->count++;
  597. }
  598. }
  599. static const struct Qdisc_class_ops fq_codel_class_ops = {
  600. .leaf = fq_codel_leaf,
  601. .get = fq_codel_get,
  602. .put = fq_codel_put,
  603. .tcf_chain = fq_codel_find_tcf,
  604. .bind_tcf = fq_codel_bind,
  605. .unbind_tcf = fq_codel_put,
  606. .dump = fq_codel_dump_class,
  607. .dump_stats = fq_codel_dump_class_stats,
  608. .walk = fq_codel_walk,
  609. };
  610. static struct Qdisc_ops fq_codel_qdisc_ops __read_mostly = {
  611. .cl_ops = &fq_codel_class_ops,
  612. .id = "fq_codel",
  613. .priv_size = sizeof(struct fq_codel_sched_data),
  614. .enqueue = fq_codel_enqueue,
  615. .dequeue = fq_codel_dequeue,
  616. .peek = qdisc_peek_dequeued,
  617. .init = fq_codel_init,
  618. .reset = fq_codel_reset,
  619. .destroy = fq_codel_destroy,
  620. .change = fq_codel_change,
  621. .dump = fq_codel_dump,
  622. .dump_stats = fq_codel_dump_stats,
  623. .owner = THIS_MODULE,
  624. };
  625. static int __init fq_codel_module_init(void)
  626. {
  627. return register_qdisc(&fq_codel_qdisc_ops);
  628. }
  629. static void __exit fq_codel_module_exit(void)
  630. {
  631. unregister_qdisc(&fq_codel_qdisc_ops);
  632. }
  633. module_init(fq_codel_module_init)
  634. module_exit(fq_codel_module_exit)
  635. MODULE_AUTHOR("Eric Dumazet");
  636. MODULE_LICENSE("GPL");