sch_choke.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * net/sched/sch_choke.c CHOKE scheduler
  3. *
  4. * Copyright (c) 2011 Stephen Hemminger <shemminger@vyatta.com>
  5. * Copyright (c) 2011 Eric Dumazet <eric.dumazet@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/vmalloc.h>
  17. #include <net/pkt_sched.h>
  18. #include <net/inet_ecn.h>
  19. #include <net/red.h>
  20. #include <net/flow_dissector.h>
  21. /*
  22. CHOKe stateless AQM for fair bandwidth allocation
  23. =================================================
  24. CHOKe (CHOose and Keep for responsive flows, CHOose and Kill for
  25. unresponsive flows) is a variant of RED that penalizes misbehaving flows but
  26. maintains no flow state. The difference from RED is an additional step
  27. during the enqueuing process. If average queue size is over the
  28. low threshold (qmin), a packet is chosen at random from the queue.
  29. If both the new and chosen packet are from the same flow, both
  30. are dropped. Unlike RED, CHOKe is not really a "classful" qdisc because it
  31. needs to access packets in queue randomly. It has a minimal class
  32. interface to allow overriding the builtin flow classifier with
  33. filters.
  34. Source:
  35. R. Pan, B. Prabhakar, and K. Psounis, "CHOKe, A Stateless
  36. Active Queue Management Scheme for Approximating Fair Bandwidth Allocation",
  37. IEEE INFOCOM, 2000.
  38. A. Tang, J. Wang, S. Low, "Understanding CHOKe: Throughput and Spatial
  39. Characteristics", IEEE/ACM Transactions on Networking, 2004
  40. */
  41. /* Upper bound on size of sk_buff table (packets) */
  42. #define CHOKE_MAX_QUEUE (128*1024 - 1)
  43. struct choke_sched_data {
  44. /* Parameters */
  45. u32 limit;
  46. unsigned char flags;
  47. struct red_parms parms;
  48. /* Variables */
  49. struct red_vars vars;
  50. struct tcf_proto __rcu *filter_list;
  51. struct {
  52. u32 prob_drop; /* Early probability drops */
  53. u32 prob_mark; /* Early probability marks */
  54. u32 forced_drop; /* Forced drops, qavg > max_thresh */
  55. u32 forced_mark; /* Forced marks, qavg > max_thresh */
  56. u32 pdrop; /* Drops due to queue limits */
  57. u32 other; /* Drops due to drop() calls */
  58. u32 matched; /* Drops to flow match */
  59. } stats;
  60. unsigned int head;
  61. unsigned int tail;
  62. unsigned int tab_mask; /* size - 1 */
  63. struct sk_buff **tab;
  64. };
  65. /* number of elements in queue including holes */
  66. static unsigned int choke_len(const struct choke_sched_data *q)
  67. {
  68. return (q->tail - q->head) & q->tab_mask;
  69. }
  70. /* Is ECN parameter configured */
  71. static int use_ecn(const struct choke_sched_data *q)
  72. {
  73. return q->flags & TC_RED_ECN;
  74. }
  75. /* Should packets over max just be dropped (versus marked) */
  76. static int use_harddrop(const struct choke_sched_data *q)
  77. {
  78. return q->flags & TC_RED_HARDDROP;
  79. }
  80. /* Move head pointer forward to skip over holes */
  81. static void choke_zap_head_holes(struct choke_sched_data *q)
  82. {
  83. do {
  84. q->head = (q->head + 1) & q->tab_mask;
  85. if (q->head == q->tail)
  86. break;
  87. } while (q->tab[q->head] == NULL);
  88. }
  89. /* Move tail pointer backwards to reuse holes */
  90. static void choke_zap_tail_holes(struct choke_sched_data *q)
  91. {
  92. do {
  93. q->tail = (q->tail - 1) & q->tab_mask;
  94. if (q->head == q->tail)
  95. break;
  96. } while (q->tab[q->tail] == NULL);
  97. }
  98. /* Drop packet from queue array by creating a "hole" */
  99. static void choke_drop_by_idx(struct Qdisc *sch, unsigned int idx)
  100. {
  101. struct choke_sched_data *q = qdisc_priv(sch);
  102. struct sk_buff *skb = q->tab[idx];
  103. q->tab[idx] = NULL;
  104. if (idx == q->head)
  105. choke_zap_head_holes(q);
  106. if (idx == q->tail)
  107. choke_zap_tail_holes(q);
  108. qdisc_qstats_backlog_dec(sch, skb);
  109. qdisc_tree_reduce_backlog(sch, 1, qdisc_pkt_len(skb));
  110. qdisc_drop(skb, sch);
  111. --sch->q.qlen;
  112. }
  113. struct choke_skb_cb {
  114. u16 classid;
  115. u8 keys_valid;
  116. struct flow_keys_digest keys;
  117. };
  118. static inline struct choke_skb_cb *choke_skb_cb(const struct sk_buff *skb)
  119. {
  120. qdisc_cb_private_validate(skb, sizeof(struct choke_skb_cb));
  121. return (struct choke_skb_cb *)qdisc_skb_cb(skb)->data;
  122. }
  123. static inline void choke_set_classid(struct sk_buff *skb, u16 classid)
  124. {
  125. choke_skb_cb(skb)->classid = classid;
  126. }
  127. static u16 choke_get_classid(const struct sk_buff *skb)
  128. {
  129. return choke_skb_cb(skb)->classid;
  130. }
  131. /*
  132. * Compare flow of two packets
  133. * Returns true only if source and destination address and port match.
  134. * false for special cases
  135. */
  136. static bool choke_match_flow(struct sk_buff *skb1,
  137. struct sk_buff *skb2)
  138. {
  139. struct flow_keys temp;
  140. if (skb1->protocol != skb2->protocol)
  141. return false;
  142. if (!choke_skb_cb(skb1)->keys_valid) {
  143. choke_skb_cb(skb1)->keys_valid = 1;
  144. skb_flow_dissect_flow_keys(skb1, &temp, 0);
  145. make_flow_keys_digest(&choke_skb_cb(skb1)->keys, &temp);
  146. }
  147. if (!choke_skb_cb(skb2)->keys_valid) {
  148. choke_skb_cb(skb2)->keys_valid = 1;
  149. skb_flow_dissect_flow_keys(skb2, &temp, 0);
  150. make_flow_keys_digest(&choke_skb_cb(skb2)->keys, &temp);
  151. }
  152. return !memcmp(&choke_skb_cb(skb1)->keys,
  153. &choke_skb_cb(skb2)->keys,
  154. sizeof(choke_skb_cb(skb1)->keys));
  155. }
  156. /*
  157. * Classify flow using either:
  158. * 1. pre-existing classification result in skb
  159. * 2. fast internal classification
  160. * 3. use TC filter based classification
  161. */
  162. static bool choke_classify(struct sk_buff *skb,
  163. struct Qdisc *sch, int *qerr)
  164. {
  165. struct choke_sched_data *q = qdisc_priv(sch);
  166. struct tcf_result res;
  167. struct tcf_proto *fl;
  168. int result;
  169. fl = rcu_dereference_bh(q->filter_list);
  170. result = tc_classify(skb, fl, &res, false);
  171. if (result >= 0) {
  172. #ifdef CONFIG_NET_CLS_ACT
  173. switch (result) {
  174. case TC_ACT_STOLEN:
  175. case TC_ACT_QUEUED:
  176. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  177. case TC_ACT_SHOT:
  178. return false;
  179. }
  180. #endif
  181. choke_set_classid(skb, TC_H_MIN(res.classid));
  182. return true;
  183. }
  184. return false;
  185. }
  186. /*
  187. * Select a packet at random from queue
  188. * HACK: since queue can have holes from previous deletion; retry several
  189. * times to find a random skb but then just give up and return the head
  190. * Will return NULL if queue is empty (q->head == q->tail)
  191. */
  192. static struct sk_buff *choke_peek_random(const struct choke_sched_data *q,
  193. unsigned int *pidx)
  194. {
  195. struct sk_buff *skb;
  196. int retrys = 3;
  197. do {
  198. *pidx = (q->head + prandom_u32_max(choke_len(q))) & q->tab_mask;
  199. skb = q->tab[*pidx];
  200. if (skb)
  201. return skb;
  202. } while (--retrys > 0);
  203. return q->tab[*pidx = q->head];
  204. }
  205. /*
  206. * Compare new packet with random packet in queue
  207. * returns true if matched and sets *pidx
  208. */
  209. static bool choke_match_random(const struct choke_sched_data *q,
  210. struct sk_buff *nskb,
  211. unsigned int *pidx)
  212. {
  213. struct sk_buff *oskb;
  214. if (q->head == q->tail)
  215. return false;
  216. oskb = choke_peek_random(q, pidx);
  217. if (rcu_access_pointer(q->filter_list))
  218. return choke_get_classid(nskb) == choke_get_classid(oskb);
  219. return choke_match_flow(oskb, nskb);
  220. }
  221. static int choke_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  222. {
  223. int ret = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  224. struct choke_sched_data *q = qdisc_priv(sch);
  225. const struct red_parms *p = &q->parms;
  226. if (rcu_access_pointer(q->filter_list)) {
  227. /* If using external classifiers, get result and record it. */
  228. if (!choke_classify(skb, sch, &ret))
  229. goto other_drop; /* Packet was eaten by filter */
  230. }
  231. choke_skb_cb(skb)->keys_valid = 0;
  232. /* Compute average queue usage (see RED) */
  233. q->vars.qavg = red_calc_qavg(p, &q->vars, sch->q.qlen);
  234. if (red_is_idling(&q->vars))
  235. red_end_of_idle_period(&q->vars);
  236. /* Is queue small? */
  237. if (q->vars.qavg <= p->qth_min)
  238. q->vars.qcount = -1;
  239. else {
  240. unsigned int idx;
  241. /* Draw a packet at random from queue and compare flow */
  242. if (choke_match_random(q, skb, &idx)) {
  243. q->stats.matched++;
  244. choke_drop_by_idx(sch, idx);
  245. goto congestion_drop;
  246. }
  247. /* Queue is large, always mark/drop */
  248. if (q->vars.qavg > p->qth_max) {
  249. q->vars.qcount = -1;
  250. qdisc_qstats_overlimit(sch);
  251. if (use_harddrop(q) || !use_ecn(q) ||
  252. !INET_ECN_set_ce(skb)) {
  253. q->stats.forced_drop++;
  254. goto congestion_drop;
  255. }
  256. q->stats.forced_mark++;
  257. } else if (++q->vars.qcount) {
  258. if (red_mark_probability(p, &q->vars, q->vars.qavg)) {
  259. q->vars.qcount = 0;
  260. q->vars.qR = red_random(p);
  261. qdisc_qstats_overlimit(sch);
  262. if (!use_ecn(q) || !INET_ECN_set_ce(skb)) {
  263. q->stats.prob_drop++;
  264. goto congestion_drop;
  265. }
  266. q->stats.prob_mark++;
  267. }
  268. } else
  269. q->vars.qR = red_random(p);
  270. }
  271. /* Admit new packet */
  272. if (sch->q.qlen < q->limit) {
  273. q->tab[q->tail] = skb;
  274. q->tail = (q->tail + 1) & q->tab_mask;
  275. ++sch->q.qlen;
  276. qdisc_qstats_backlog_inc(sch, skb);
  277. return NET_XMIT_SUCCESS;
  278. }
  279. q->stats.pdrop++;
  280. return qdisc_drop(skb, sch);
  281. congestion_drop:
  282. qdisc_drop(skb, sch);
  283. return NET_XMIT_CN;
  284. other_drop:
  285. if (ret & __NET_XMIT_BYPASS)
  286. qdisc_qstats_drop(sch);
  287. kfree_skb(skb);
  288. return ret;
  289. }
  290. static struct sk_buff *choke_dequeue(struct Qdisc *sch)
  291. {
  292. struct choke_sched_data *q = qdisc_priv(sch);
  293. struct sk_buff *skb;
  294. if (q->head == q->tail) {
  295. if (!red_is_idling(&q->vars))
  296. red_start_of_idle_period(&q->vars);
  297. return NULL;
  298. }
  299. skb = q->tab[q->head];
  300. q->tab[q->head] = NULL;
  301. choke_zap_head_holes(q);
  302. --sch->q.qlen;
  303. qdisc_qstats_backlog_dec(sch, skb);
  304. qdisc_bstats_update(sch, skb);
  305. return skb;
  306. }
  307. static unsigned int choke_drop(struct Qdisc *sch)
  308. {
  309. struct choke_sched_data *q = qdisc_priv(sch);
  310. unsigned int len;
  311. len = qdisc_queue_drop(sch);
  312. if (len > 0)
  313. q->stats.other++;
  314. else {
  315. if (!red_is_idling(&q->vars))
  316. red_start_of_idle_period(&q->vars);
  317. }
  318. return len;
  319. }
  320. static void choke_reset(struct Qdisc *sch)
  321. {
  322. struct choke_sched_data *q = qdisc_priv(sch);
  323. while (q->head != q->tail) {
  324. struct sk_buff *skb = q->tab[q->head];
  325. q->head = (q->head + 1) & q->tab_mask;
  326. if (!skb)
  327. continue;
  328. qdisc_qstats_backlog_dec(sch, skb);
  329. --sch->q.qlen;
  330. qdisc_drop(skb, sch);
  331. }
  332. memset(q->tab, 0, (q->tab_mask + 1) * sizeof(struct sk_buff *));
  333. q->head = q->tail = 0;
  334. red_restart(&q->vars);
  335. }
  336. static const struct nla_policy choke_policy[TCA_CHOKE_MAX + 1] = {
  337. [TCA_CHOKE_PARMS] = { .len = sizeof(struct tc_red_qopt) },
  338. [TCA_CHOKE_STAB] = { .len = RED_STAB_SIZE },
  339. [TCA_CHOKE_MAX_P] = { .type = NLA_U32 },
  340. };
  341. static void choke_free(void *addr)
  342. {
  343. kvfree(addr);
  344. }
  345. static int choke_change(struct Qdisc *sch, struct nlattr *opt)
  346. {
  347. struct choke_sched_data *q = qdisc_priv(sch);
  348. struct nlattr *tb[TCA_CHOKE_MAX + 1];
  349. const struct tc_red_qopt *ctl;
  350. int err;
  351. struct sk_buff **old = NULL;
  352. unsigned int mask;
  353. u32 max_P;
  354. if (opt == NULL)
  355. return -EINVAL;
  356. err = nla_parse_nested(tb, TCA_CHOKE_MAX, opt, choke_policy);
  357. if (err < 0)
  358. return err;
  359. if (tb[TCA_CHOKE_PARMS] == NULL ||
  360. tb[TCA_CHOKE_STAB] == NULL)
  361. return -EINVAL;
  362. max_P = tb[TCA_CHOKE_MAX_P] ? nla_get_u32(tb[TCA_CHOKE_MAX_P]) : 0;
  363. ctl = nla_data(tb[TCA_CHOKE_PARMS]);
  364. if (!red_check_params(ctl->qth_min, ctl->qth_max, ctl->Wlog))
  365. return -EINVAL;
  366. if (ctl->limit > CHOKE_MAX_QUEUE)
  367. return -EINVAL;
  368. mask = roundup_pow_of_two(ctl->limit + 1) - 1;
  369. if (mask != q->tab_mask) {
  370. struct sk_buff **ntab;
  371. ntab = kcalloc(mask + 1, sizeof(struct sk_buff *),
  372. GFP_KERNEL | __GFP_NOWARN);
  373. if (!ntab)
  374. ntab = vzalloc((mask + 1) * sizeof(struct sk_buff *));
  375. if (!ntab)
  376. return -ENOMEM;
  377. sch_tree_lock(sch);
  378. old = q->tab;
  379. if (old) {
  380. unsigned int oqlen = sch->q.qlen, tail = 0;
  381. unsigned dropped = 0;
  382. while (q->head != q->tail) {
  383. struct sk_buff *skb = q->tab[q->head];
  384. q->head = (q->head + 1) & q->tab_mask;
  385. if (!skb)
  386. continue;
  387. if (tail < mask) {
  388. ntab[tail++] = skb;
  389. continue;
  390. }
  391. dropped += qdisc_pkt_len(skb);
  392. qdisc_qstats_backlog_dec(sch, skb);
  393. --sch->q.qlen;
  394. qdisc_drop(skb, sch);
  395. }
  396. qdisc_tree_reduce_backlog(sch, oqlen - sch->q.qlen, dropped);
  397. q->head = 0;
  398. q->tail = tail;
  399. }
  400. q->tab_mask = mask;
  401. q->tab = ntab;
  402. } else
  403. sch_tree_lock(sch);
  404. q->flags = ctl->flags;
  405. q->limit = ctl->limit;
  406. red_set_parms(&q->parms, ctl->qth_min, ctl->qth_max, ctl->Wlog,
  407. ctl->Plog, ctl->Scell_log,
  408. nla_data(tb[TCA_CHOKE_STAB]),
  409. max_P);
  410. red_set_vars(&q->vars);
  411. if (q->head == q->tail)
  412. red_end_of_idle_period(&q->vars);
  413. sch_tree_unlock(sch);
  414. choke_free(old);
  415. return 0;
  416. }
  417. static int choke_init(struct Qdisc *sch, struct nlattr *opt)
  418. {
  419. return choke_change(sch, opt);
  420. }
  421. static int choke_dump(struct Qdisc *sch, struct sk_buff *skb)
  422. {
  423. struct choke_sched_data *q = qdisc_priv(sch);
  424. struct nlattr *opts = NULL;
  425. struct tc_red_qopt opt = {
  426. .limit = q->limit,
  427. .flags = q->flags,
  428. .qth_min = q->parms.qth_min >> q->parms.Wlog,
  429. .qth_max = q->parms.qth_max >> q->parms.Wlog,
  430. .Wlog = q->parms.Wlog,
  431. .Plog = q->parms.Plog,
  432. .Scell_log = q->parms.Scell_log,
  433. };
  434. opts = nla_nest_start(skb, TCA_OPTIONS);
  435. if (opts == NULL)
  436. goto nla_put_failure;
  437. if (nla_put(skb, TCA_CHOKE_PARMS, sizeof(opt), &opt) ||
  438. nla_put_u32(skb, TCA_CHOKE_MAX_P, q->parms.max_P))
  439. goto nla_put_failure;
  440. return nla_nest_end(skb, opts);
  441. nla_put_failure:
  442. nla_nest_cancel(skb, opts);
  443. return -EMSGSIZE;
  444. }
  445. static int choke_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  446. {
  447. struct choke_sched_data *q = qdisc_priv(sch);
  448. struct tc_choke_xstats st = {
  449. .early = q->stats.prob_drop + q->stats.forced_drop,
  450. .marked = q->stats.prob_mark + q->stats.forced_mark,
  451. .pdrop = q->stats.pdrop,
  452. .other = q->stats.other,
  453. .matched = q->stats.matched,
  454. };
  455. return gnet_stats_copy_app(d, &st, sizeof(st));
  456. }
  457. static void choke_destroy(struct Qdisc *sch)
  458. {
  459. struct choke_sched_data *q = qdisc_priv(sch);
  460. tcf_destroy_chain(&q->filter_list);
  461. choke_free(q->tab);
  462. }
  463. static struct sk_buff *choke_peek_head(struct Qdisc *sch)
  464. {
  465. struct choke_sched_data *q = qdisc_priv(sch);
  466. return (q->head != q->tail) ? q->tab[q->head] : NULL;
  467. }
  468. static struct Qdisc_ops choke_qdisc_ops __read_mostly = {
  469. .id = "choke",
  470. .priv_size = sizeof(struct choke_sched_data),
  471. .enqueue = choke_enqueue,
  472. .dequeue = choke_dequeue,
  473. .peek = choke_peek_head,
  474. .drop = choke_drop,
  475. .init = choke_init,
  476. .destroy = choke_destroy,
  477. .reset = choke_reset,
  478. .change = choke_change,
  479. .dump = choke_dump,
  480. .dump_stats = choke_dump_stats,
  481. .owner = THIS_MODULE,
  482. };
  483. static int __init choke_module_init(void)
  484. {
  485. return register_qdisc(&choke_qdisc_ops);
  486. }
  487. static void __exit choke_module_exit(void)
  488. {
  489. unregister_qdisc(&choke_qdisc_ops);
  490. }
  491. module_init(choke_module_init)
  492. module_exit(choke_module_exit)
  493. MODULE_LICENSE("GPL");