sch_choke.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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/reciprocal_div.h>
  17. #include <linux/vmalloc.h>
  18. #include <net/pkt_sched.h>
  19. #include <net/inet_ecn.h>
  20. #include <net/red.h>
  21. #include <linux/ip.h>
  22. #include <net/ip.h>
  23. #include <linux/ipv6.h>
  24. #include <net/ipv6.h>
  25. /*
  26. CHOKe stateless AQM for fair bandwidth allocation
  27. =================================================
  28. CHOKe (CHOose and Keep for responsive flows, CHOose and Kill for
  29. unresponsive flows) is a variant of RED that penalizes misbehaving flows but
  30. maintains no flow state. The difference from RED is an additional step
  31. during the enqueuing process. If average queue size is over the
  32. low threshold (qmin), a packet is chosen at random from the queue.
  33. If both the new and chosen packet are from the same flow, both
  34. are dropped. Unlike RED, CHOKe is not really a "classful" qdisc because it
  35. needs to access packets in queue randomly. It has a minimal class
  36. interface to allow overriding the builtin flow classifier with
  37. filters.
  38. Source:
  39. R. Pan, B. Prabhakar, and K. Psounis, "CHOKe, A Stateless
  40. Active Queue Management Scheme for Approximating Fair Bandwidth Allocation",
  41. IEEE INFOCOM, 2000.
  42. A. Tang, J. Wang, S. Low, "Understanding CHOKe: Throughput and Spatial
  43. Characteristics", IEEE/ACM Transactions on Networking, 2004
  44. */
  45. /* Upper bound on size of sk_buff table (packets) */
  46. #define CHOKE_MAX_QUEUE (128*1024 - 1)
  47. struct choke_sched_data {
  48. /* Parameters */
  49. u32 limit;
  50. unsigned char flags;
  51. struct red_parms parms;
  52. /* Variables */
  53. struct tcf_proto *filter_list;
  54. struct {
  55. u32 prob_drop; /* Early probability drops */
  56. u32 prob_mark; /* Early probability marks */
  57. u32 forced_drop; /* Forced drops, qavg > max_thresh */
  58. u32 forced_mark; /* Forced marks, qavg > max_thresh */
  59. u32 pdrop; /* Drops due to queue limits */
  60. u32 other; /* Drops due to drop() calls */
  61. u32 matched; /* Drops to flow match */
  62. } stats;
  63. unsigned int head;
  64. unsigned int tail;
  65. unsigned int tab_mask; /* size - 1 */
  66. struct sk_buff **tab;
  67. };
  68. /* deliver a random number between 0 and N - 1 */
  69. static u32 random_N(unsigned int N)
  70. {
  71. return reciprocal_divide(random32(), N);
  72. }
  73. /* number of elements in queue including holes */
  74. static unsigned int choke_len(const struct choke_sched_data *q)
  75. {
  76. return (q->tail - q->head) & q->tab_mask;
  77. }
  78. /* Is ECN parameter configured */
  79. static int use_ecn(const struct choke_sched_data *q)
  80. {
  81. return q->flags & TC_RED_ECN;
  82. }
  83. /* Should packets over max just be dropped (versus marked) */
  84. static int use_harddrop(const struct choke_sched_data *q)
  85. {
  86. return q->flags & TC_RED_HARDDROP;
  87. }
  88. /* Move head pointer forward to skip over holes */
  89. static void choke_zap_head_holes(struct choke_sched_data *q)
  90. {
  91. do {
  92. q->head = (q->head + 1) & q->tab_mask;
  93. if (q->head == q->tail)
  94. break;
  95. } while (q->tab[q->head] == NULL);
  96. }
  97. /* Move tail pointer backwards to reuse holes */
  98. static void choke_zap_tail_holes(struct choke_sched_data *q)
  99. {
  100. do {
  101. q->tail = (q->tail - 1) & q->tab_mask;
  102. if (q->head == q->tail)
  103. break;
  104. } while (q->tab[q->tail] == NULL);
  105. }
  106. /* Drop packet from queue array by creating a "hole" */
  107. static void choke_drop_by_idx(struct Qdisc *sch, unsigned int idx)
  108. {
  109. struct choke_sched_data *q = qdisc_priv(sch);
  110. struct sk_buff *skb = q->tab[idx];
  111. q->tab[idx] = NULL;
  112. if (idx == q->head)
  113. choke_zap_head_holes(q);
  114. if (idx == q->tail)
  115. choke_zap_tail_holes(q);
  116. sch->qstats.backlog -= qdisc_pkt_len(skb);
  117. qdisc_drop(skb, sch);
  118. qdisc_tree_decrease_qlen(sch, 1);
  119. --sch->q.qlen;
  120. }
  121. /*
  122. * Compare flow of two packets
  123. * Returns true only if source and destination address and port match.
  124. * false for special cases
  125. */
  126. static bool choke_match_flow(struct sk_buff *skb1,
  127. struct sk_buff *skb2)
  128. {
  129. int off1, off2, poff;
  130. const u32 *ports1, *ports2;
  131. u8 ip_proto;
  132. __u32 hash1;
  133. if (skb1->protocol != skb2->protocol)
  134. return false;
  135. /* Use hash value as quick check
  136. * Assumes that __skb_get_rxhash makes IP header and ports linear
  137. */
  138. hash1 = skb_get_rxhash(skb1);
  139. if (!hash1 || hash1 != skb_get_rxhash(skb2))
  140. return false;
  141. /* Probably match, but be sure to avoid hash collisions */
  142. off1 = skb_network_offset(skb1);
  143. off2 = skb_network_offset(skb2);
  144. switch (skb1->protocol) {
  145. case __constant_htons(ETH_P_IP): {
  146. const struct iphdr *ip1, *ip2;
  147. ip1 = (const struct iphdr *) (skb1->data + off1);
  148. ip2 = (const struct iphdr *) (skb2->data + off2);
  149. ip_proto = ip1->protocol;
  150. if (ip_proto != ip2->protocol ||
  151. ip1->saddr != ip2->saddr || ip1->daddr != ip2->daddr)
  152. return false;
  153. if ((ip1->frag_off | ip2->frag_off) & htons(IP_MF | IP_OFFSET))
  154. ip_proto = 0;
  155. off1 += ip1->ihl * 4;
  156. off2 += ip2->ihl * 4;
  157. break;
  158. }
  159. case __constant_htons(ETH_P_IPV6): {
  160. const struct ipv6hdr *ip1, *ip2;
  161. ip1 = (const struct ipv6hdr *) (skb1->data + off1);
  162. ip2 = (const struct ipv6hdr *) (skb2->data + off2);
  163. ip_proto = ip1->nexthdr;
  164. if (ip_proto != ip2->nexthdr ||
  165. ipv6_addr_cmp(&ip1->saddr, &ip2->saddr) ||
  166. ipv6_addr_cmp(&ip1->daddr, &ip2->daddr))
  167. return false;
  168. off1 += 40;
  169. off2 += 40;
  170. }
  171. default: /* Maybe compare MAC header here? */
  172. return false;
  173. }
  174. poff = proto_ports_offset(ip_proto);
  175. if (poff < 0)
  176. return true;
  177. off1 += poff;
  178. off2 += poff;
  179. ports1 = (__force u32 *)(skb1->data + off1);
  180. ports2 = (__force u32 *)(skb2->data + off2);
  181. return *ports1 == *ports2;
  182. }
  183. struct choke_skb_cb {
  184. u16 classid;
  185. };
  186. static inline struct choke_skb_cb *choke_skb_cb(const struct sk_buff *skb)
  187. {
  188. qdisc_cb_private_validate(skb, sizeof(struct choke_skb_cb));
  189. return (struct choke_skb_cb *)qdisc_skb_cb(skb)->data;
  190. }
  191. static inline void choke_set_classid(struct sk_buff *skb, u16 classid)
  192. {
  193. choke_skb_cb(skb)->classid = classid;
  194. }
  195. static u16 choke_get_classid(const struct sk_buff *skb)
  196. {
  197. return choke_skb_cb(skb)->classid;
  198. }
  199. /*
  200. * Classify flow using either:
  201. * 1. pre-existing classification result in skb
  202. * 2. fast internal classification
  203. * 3. use TC filter based classification
  204. */
  205. static bool choke_classify(struct sk_buff *skb,
  206. struct Qdisc *sch, int *qerr)
  207. {
  208. struct choke_sched_data *q = qdisc_priv(sch);
  209. struct tcf_result res;
  210. int result;
  211. result = tc_classify(skb, q->filter_list, &res);
  212. if (result >= 0) {
  213. #ifdef CONFIG_NET_CLS_ACT
  214. switch (result) {
  215. case TC_ACT_STOLEN:
  216. case TC_ACT_QUEUED:
  217. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  218. case TC_ACT_SHOT:
  219. return false;
  220. }
  221. #endif
  222. choke_set_classid(skb, TC_H_MIN(res.classid));
  223. return true;
  224. }
  225. return false;
  226. }
  227. /*
  228. * Select a packet at random from queue
  229. * HACK: since queue can have holes from previous deletion; retry several
  230. * times to find a random skb but then just give up and return the head
  231. * Will return NULL if queue is empty (q->head == q->tail)
  232. */
  233. static struct sk_buff *choke_peek_random(const struct choke_sched_data *q,
  234. unsigned int *pidx)
  235. {
  236. struct sk_buff *skb;
  237. int retrys = 3;
  238. do {
  239. *pidx = (q->head + random_N(choke_len(q))) & q->tab_mask;
  240. skb = q->tab[*pidx];
  241. if (skb)
  242. return skb;
  243. } while (--retrys > 0);
  244. return q->tab[*pidx = q->head];
  245. }
  246. /*
  247. * Compare new packet with random packet in queue
  248. * returns true if matched and sets *pidx
  249. */
  250. static bool choke_match_random(const struct choke_sched_data *q,
  251. struct sk_buff *nskb,
  252. unsigned int *pidx)
  253. {
  254. struct sk_buff *oskb;
  255. if (q->head == q->tail)
  256. return false;
  257. oskb = choke_peek_random(q, pidx);
  258. if (q->filter_list)
  259. return choke_get_classid(nskb) == choke_get_classid(oskb);
  260. return choke_match_flow(oskb, nskb);
  261. }
  262. static int choke_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  263. {
  264. struct choke_sched_data *q = qdisc_priv(sch);
  265. struct red_parms *p = &q->parms;
  266. int ret = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  267. if (q->filter_list) {
  268. /* If using external classifiers, get result and record it. */
  269. if (!choke_classify(skb, sch, &ret))
  270. goto other_drop; /* Packet was eaten by filter */
  271. }
  272. /* Compute average queue usage (see RED) */
  273. p->qavg = red_calc_qavg(p, sch->q.qlen);
  274. if (red_is_idling(p))
  275. red_end_of_idle_period(p);
  276. /* Is queue small? */
  277. if (p->qavg <= p->qth_min)
  278. p->qcount = -1;
  279. else {
  280. unsigned int idx;
  281. /* Draw a packet at random from queue and compare flow */
  282. if (choke_match_random(q, skb, &idx)) {
  283. q->stats.matched++;
  284. choke_drop_by_idx(sch, idx);
  285. goto congestion_drop;
  286. }
  287. /* Queue is large, always mark/drop */
  288. if (p->qavg > p->qth_max) {
  289. p->qcount = -1;
  290. sch->qstats.overlimits++;
  291. if (use_harddrop(q) || !use_ecn(q) ||
  292. !INET_ECN_set_ce(skb)) {
  293. q->stats.forced_drop++;
  294. goto congestion_drop;
  295. }
  296. q->stats.forced_mark++;
  297. } else if (++p->qcount) {
  298. if (red_mark_probability(p, p->qavg)) {
  299. p->qcount = 0;
  300. p->qR = red_random(p);
  301. sch->qstats.overlimits++;
  302. if (!use_ecn(q) || !INET_ECN_set_ce(skb)) {
  303. q->stats.prob_drop++;
  304. goto congestion_drop;
  305. }
  306. q->stats.prob_mark++;
  307. }
  308. } else
  309. p->qR = red_random(p);
  310. }
  311. /* Admit new packet */
  312. if (sch->q.qlen < q->limit) {
  313. q->tab[q->tail] = skb;
  314. q->tail = (q->tail + 1) & q->tab_mask;
  315. ++sch->q.qlen;
  316. sch->qstats.backlog += qdisc_pkt_len(skb);
  317. return NET_XMIT_SUCCESS;
  318. }
  319. q->stats.pdrop++;
  320. sch->qstats.drops++;
  321. kfree_skb(skb);
  322. return NET_XMIT_DROP;
  323. congestion_drop:
  324. qdisc_drop(skb, sch);
  325. return NET_XMIT_CN;
  326. other_drop:
  327. if (ret & __NET_XMIT_BYPASS)
  328. sch->qstats.drops++;
  329. kfree_skb(skb);
  330. return ret;
  331. }
  332. static struct sk_buff *choke_dequeue(struct Qdisc *sch)
  333. {
  334. struct choke_sched_data *q = qdisc_priv(sch);
  335. struct sk_buff *skb;
  336. if (q->head == q->tail) {
  337. if (!red_is_idling(&q->parms))
  338. red_start_of_idle_period(&q->parms);
  339. return NULL;
  340. }
  341. skb = q->tab[q->head];
  342. q->tab[q->head] = NULL;
  343. choke_zap_head_holes(q);
  344. --sch->q.qlen;
  345. sch->qstats.backlog -= qdisc_pkt_len(skb);
  346. qdisc_bstats_update(sch, skb);
  347. return skb;
  348. }
  349. static unsigned int choke_drop(struct Qdisc *sch)
  350. {
  351. struct choke_sched_data *q = qdisc_priv(sch);
  352. unsigned int len;
  353. len = qdisc_queue_drop(sch);
  354. if (len > 0)
  355. q->stats.other++;
  356. else {
  357. if (!red_is_idling(&q->parms))
  358. red_start_of_idle_period(&q->parms);
  359. }
  360. return len;
  361. }
  362. static void choke_reset(struct Qdisc *sch)
  363. {
  364. struct choke_sched_data *q = qdisc_priv(sch);
  365. red_restart(&q->parms);
  366. }
  367. static const struct nla_policy choke_policy[TCA_CHOKE_MAX + 1] = {
  368. [TCA_CHOKE_PARMS] = { .len = sizeof(struct tc_red_qopt) },
  369. [TCA_CHOKE_STAB] = { .len = RED_STAB_SIZE },
  370. };
  371. static void choke_free(void *addr)
  372. {
  373. if (addr) {
  374. if (is_vmalloc_addr(addr))
  375. vfree(addr);
  376. else
  377. kfree(addr);
  378. }
  379. }
  380. static int choke_change(struct Qdisc *sch, struct nlattr *opt)
  381. {
  382. struct choke_sched_data *q = qdisc_priv(sch);
  383. struct nlattr *tb[TCA_CHOKE_MAX + 1];
  384. const struct tc_red_qopt *ctl;
  385. int err;
  386. struct sk_buff **old = NULL;
  387. unsigned int mask;
  388. if (opt == NULL)
  389. return -EINVAL;
  390. err = nla_parse_nested(tb, TCA_CHOKE_MAX, opt, choke_policy);
  391. if (err < 0)
  392. return err;
  393. if (tb[TCA_CHOKE_PARMS] == NULL ||
  394. tb[TCA_CHOKE_STAB] == NULL)
  395. return -EINVAL;
  396. ctl = nla_data(tb[TCA_CHOKE_PARMS]);
  397. if (ctl->limit > CHOKE_MAX_QUEUE)
  398. return -EINVAL;
  399. mask = roundup_pow_of_two(ctl->limit + 1) - 1;
  400. if (mask != q->tab_mask) {
  401. struct sk_buff **ntab;
  402. ntab = kcalloc(mask + 1, sizeof(struct sk_buff *), GFP_KERNEL);
  403. if (!ntab)
  404. ntab = vzalloc((mask + 1) * sizeof(struct sk_buff *));
  405. if (!ntab)
  406. return -ENOMEM;
  407. sch_tree_lock(sch);
  408. old = q->tab;
  409. if (old) {
  410. unsigned int oqlen = sch->q.qlen, tail = 0;
  411. while (q->head != q->tail) {
  412. struct sk_buff *skb = q->tab[q->head];
  413. q->head = (q->head + 1) & q->tab_mask;
  414. if (!skb)
  415. continue;
  416. if (tail < mask) {
  417. ntab[tail++] = skb;
  418. continue;
  419. }
  420. sch->qstats.backlog -= qdisc_pkt_len(skb);
  421. --sch->q.qlen;
  422. qdisc_drop(skb, sch);
  423. }
  424. qdisc_tree_decrease_qlen(sch, oqlen - sch->q.qlen);
  425. q->head = 0;
  426. q->tail = tail;
  427. }
  428. q->tab_mask = mask;
  429. q->tab = ntab;
  430. } else
  431. sch_tree_lock(sch);
  432. q->flags = ctl->flags;
  433. q->limit = ctl->limit;
  434. red_set_parms(&q->parms, ctl->qth_min, ctl->qth_max, ctl->Wlog,
  435. ctl->Plog, ctl->Scell_log,
  436. nla_data(tb[TCA_CHOKE_STAB]));
  437. if (q->head == q->tail)
  438. red_end_of_idle_period(&q->parms);
  439. sch_tree_unlock(sch);
  440. choke_free(old);
  441. return 0;
  442. }
  443. static int choke_init(struct Qdisc *sch, struct nlattr *opt)
  444. {
  445. return choke_change(sch, opt);
  446. }
  447. static int choke_dump(struct Qdisc *sch, struct sk_buff *skb)
  448. {
  449. struct choke_sched_data *q = qdisc_priv(sch);
  450. struct nlattr *opts = NULL;
  451. struct tc_red_qopt opt = {
  452. .limit = q->limit,
  453. .flags = q->flags,
  454. .qth_min = q->parms.qth_min >> q->parms.Wlog,
  455. .qth_max = q->parms.qth_max >> q->parms.Wlog,
  456. .Wlog = q->parms.Wlog,
  457. .Plog = q->parms.Plog,
  458. .Scell_log = q->parms.Scell_log,
  459. };
  460. opts = nla_nest_start(skb, TCA_OPTIONS);
  461. if (opts == NULL)
  462. goto nla_put_failure;
  463. NLA_PUT(skb, TCA_CHOKE_PARMS, sizeof(opt), &opt);
  464. return nla_nest_end(skb, opts);
  465. nla_put_failure:
  466. nla_nest_cancel(skb, opts);
  467. return -EMSGSIZE;
  468. }
  469. static int choke_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  470. {
  471. struct choke_sched_data *q = qdisc_priv(sch);
  472. struct tc_choke_xstats st = {
  473. .early = q->stats.prob_drop + q->stats.forced_drop,
  474. .marked = q->stats.prob_mark + q->stats.forced_mark,
  475. .pdrop = q->stats.pdrop,
  476. .other = q->stats.other,
  477. .matched = q->stats.matched,
  478. };
  479. return gnet_stats_copy_app(d, &st, sizeof(st));
  480. }
  481. static void choke_destroy(struct Qdisc *sch)
  482. {
  483. struct choke_sched_data *q = qdisc_priv(sch);
  484. tcf_destroy_chain(&q->filter_list);
  485. choke_free(q->tab);
  486. }
  487. static struct Qdisc *choke_leaf(struct Qdisc *sch, unsigned long arg)
  488. {
  489. return NULL;
  490. }
  491. static unsigned long choke_get(struct Qdisc *sch, u32 classid)
  492. {
  493. return 0;
  494. }
  495. static void choke_put(struct Qdisc *q, unsigned long cl)
  496. {
  497. }
  498. static unsigned long choke_bind(struct Qdisc *sch, unsigned long parent,
  499. u32 classid)
  500. {
  501. return 0;
  502. }
  503. static struct tcf_proto **choke_find_tcf(struct Qdisc *sch, unsigned long cl)
  504. {
  505. struct choke_sched_data *q = qdisc_priv(sch);
  506. if (cl)
  507. return NULL;
  508. return &q->filter_list;
  509. }
  510. static int choke_dump_class(struct Qdisc *sch, unsigned long cl,
  511. struct sk_buff *skb, struct tcmsg *tcm)
  512. {
  513. tcm->tcm_handle |= TC_H_MIN(cl);
  514. return 0;
  515. }
  516. static void choke_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  517. {
  518. if (!arg->stop) {
  519. if (arg->fn(sch, 1, arg) < 0) {
  520. arg->stop = 1;
  521. return;
  522. }
  523. arg->count++;
  524. }
  525. }
  526. static const struct Qdisc_class_ops choke_class_ops = {
  527. .leaf = choke_leaf,
  528. .get = choke_get,
  529. .put = choke_put,
  530. .tcf_chain = choke_find_tcf,
  531. .bind_tcf = choke_bind,
  532. .unbind_tcf = choke_put,
  533. .dump = choke_dump_class,
  534. .walk = choke_walk,
  535. };
  536. static struct sk_buff *choke_peek_head(struct Qdisc *sch)
  537. {
  538. struct choke_sched_data *q = qdisc_priv(sch);
  539. return (q->head != q->tail) ? q->tab[q->head] : NULL;
  540. }
  541. static struct Qdisc_ops choke_qdisc_ops __read_mostly = {
  542. .id = "choke",
  543. .priv_size = sizeof(struct choke_sched_data),
  544. .enqueue = choke_enqueue,
  545. .dequeue = choke_dequeue,
  546. .peek = choke_peek_head,
  547. .drop = choke_drop,
  548. .init = choke_init,
  549. .destroy = choke_destroy,
  550. .reset = choke_reset,
  551. .change = choke_change,
  552. .dump = choke_dump,
  553. .dump_stats = choke_dump_stats,
  554. .owner = THIS_MODULE,
  555. };
  556. static int __init choke_module_init(void)
  557. {
  558. return register_qdisc(&choke_qdisc_ops);
  559. }
  560. static void __exit choke_module_exit(void)
  561. {
  562. unregister_qdisc(&choke_qdisc_ops);
  563. }
  564. module_init(choke_module_init)
  565. module_exit(choke_module_exit)
  566. MODULE_LICENSE("GPL");