act_police.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * net/sched/act_police.c Input police filter
  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. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. * J Hadi Salim (action changes)
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <net/act_api.h>
  22. #include <net/netlink.h>
  23. struct tcf_police {
  24. struct tcf_common common;
  25. int tcfp_result;
  26. u32 tcfp_ewma_rate;
  27. s64 tcfp_burst;
  28. u32 tcfp_mtu;
  29. s64 tcfp_toks;
  30. s64 tcfp_ptoks;
  31. s64 tcfp_mtu_ptoks;
  32. s64 tcfp_t_c;
  33. struct psched_ratecfg rate;
  34. bool rate_present;
  35. struct psched_ratecfg peak;
  36. bool peak_present;
  37. };
  38. #define to_police(pc) \
  39. container_of(pc, struct tcf_police, common)
  40. #define POL_TAB_MASK 15
  41. /* old policer structure from before tc actions */
  42. struct tc_police_compat {
  43. u32 index;
  44. int action;
  45. u32 limit;
  46. u32 burst;
  47. u32 mtu;
  48. struct tc_ratespec rate;
  49. struct tc_ratespec peakrate;
  50. };
  51. /* Each policer is serialized by its individual spinlock */
  52. static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
  53. int type, struct tc_action *a)
  54. {
  55. struct tcf_hashinfo *hinfo = a->ops->hinfo;
  56. struct hlist_head *head;
  57. struct tcf_common *p;
  58. int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
  59. struct nlattr *nest;
  60. spin_lock_bh(&hinfo->lock);
  61. s_i = cb->args[0];
  62. for (i = 0; i < (POL_TAB_MASK + 1); i++) {
  63. head = &hinfo->htab[tcf_hash(i, POL_TAB_MASK)];
  64. hlist_for_each_entry_rcu(p, head, tcfc_head) {
  65. index++;
  66. if (index < s_i)
  67. continue;
  68. a->priv = p;
  69. a->order = index;
  70. nest = nla_nest_start(skb, a->order);
  71. if (nest == NULL)
  72. goto nla_put_failure;
  73. if (type == RTM_DELACTION)
  74. err = tcf_action_dump_1(skb, a, 0, 1);
  75. else
  76. err = tcf_action_dump_1(skb, a, 0, 0);
  77. if (err < 0) {
  78. index--;
  79. nla_nest_cancel(skb, nest);
  80. goto done;
  81. }
  82. nla_nest_end(skb, nest);
  83. n_i++;
  84. }
  85. }
  86. done:
  87. spin_unlock_bh(&hinfo->lock);
  88. if (n_i)
  89. cb->args[0] += n_i;
  90. return n_i;
  91. nla_put_failure:
  92. nla_nest_cancel(skb, nest);
  93. goto done;
  94. }
  95. static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
  96. [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
  97. [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
  98. [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
  99. [TCA_POLICE_RESULT] = { .type = NLA_U32 },
  100. };
  101. static int tcf_act_police_locate(struct net *net, struct nlattr *nla,
  102. struct nlattr *est, struct tc_action *a,
  103. int ovr, int bind)
  104. {
  105. unsigned int h;
  106. int ret = 0, err;
  107. struct nlattr *tb[TCA_POLICE_MAX + 1];
  108. struct tc_police *parm;
  109. struct tcf_police *police;
  110. struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
  111. struct tcf_hashinfo *hinfo = a->ops->hinfo;
  112. int size;
  113. if (nla == NULL)
  114. return -EINVAL;
  115. err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy);
  116. if (err < 0)
  117. return err;
  118. if (tb[TCA_POLICE_TBF] == NULL)
  119. return -EINVAL;
  120. size = nla_len(tb[TCA_POLICE_TBF]);
  121. if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
  122. return -EINVAL;
  123. parm = nla_data(tb[TCA_POLICE_TBF]);
  124. if (parm->index) {
  125. if (tcf_hash_search(a, parm->index)) {
  126. police = to_police(a->priv);
  127. if (bind) {
  128. police->tcf_bindcnt += 1;
  129. police->tcf_refcnt += 1;
  130. return 0;
  131. }
  132. if (ovr)
  133. goto override;
  134. /* not replacing */
  135. return -EEXIST;
  136. }
  137. }
  138. police = kzalloc(sizeof(*police), GFP_KERNEL);
  139. if (police == NULL)
  140. return -ENOMEM;
  141. ret = ACT_P_CREATED;
  142. police->tcf_refcnt = 1;
  143. spin_lock_init(&police->tcf_lock);
  144. if (bind)
  145. police->tcf_bindcnt = 1;
  146. override:
  147. if (parm->rate.rate) {
  148. err = -ENOMEM;
  149. R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
  150. if (R_tab == NULL)
  151. goto failure;
  152. if (parm->peakrate.rate) {
  153. P_tab = qdisc_get_rtab(&parm->peakrate,
  154. tb[TCA_POLICE_PEAKRATE]);
  155. if (P_tab == NULL)
  156. goto failure;
  157. }
  158. }
  159. spin_lock_bh(&police->tcf_lock);
  160. if (est) {
  161. err = gen_replace_estimator(&police->tcf_bstats, NULL,
  162. &police->tcf_rate_est,
  163. &police->tcf_lock, est);
  164. if (err)
  165. goto failure_unlock;
  166. } else if (tb[TCA_POLICE_AVRATE] &&
  167. (ret == ACT_P_CREATED ||
  168. !gen_estimator_active(&police->tcf_bstats,
  169. &police->tcf_rate_est))) {
  170. err = -EINVAL;
  171. goto failure_unlock;
  172. }
  173. /* No failure allowed after this point */
  174. police->tcfp_mtu = parm->mtu;
  175. if (police->tcfp_mtu == 0) {
  176. police->tcfp_mtu = ~0;
  177. if (R_tab)
  178. police->tcfp_mtu = 255 << R_tab->rate.cell_log;
  179. }
  180. if (R_tab) {
  181. police->rate_present = true;
  182. psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
  183. qdisc_put_rtab(R_tab);
  184. } else {
  185. police->rate_present = false;
  186. }
  187. if (P_tab) {
  188. police->peak_present = true;
  189. psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
  190. qdisc_put_rtab(P_tab);
  191. } else {
  192. police->peak_present = false;
  193. }
  194. if (tb[TCA_POLICE_RESULT])
  195. police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
  196. police->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
  197. police->tcfp_toks = police->tcfp_burst;
  198. if (police->peak_present) {
  199. police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak,
  200. police->tcfp_mtu);
  201. police->tcfp_ptoks = police->tcfp_mtu_ptoks;
  202. }
  203. police->tcf_action = parm->action;
  204. if (tb[TCA_POLICE_AVRATE])
  205. police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
  206. spin_unlock_bh(&police->tcf_lock);
  207. if (ret != ACT_P_CREATED)
  208. return ret;
  209. police->tcfp_t_c = ktime_get_ns();
  210. police->tcf_index = parm->index ? parm->index :
  211. tcf_hash_new_index(hinfo);
  212. h = tcf_hash(police->tcf_index, POL_TAB_MASK);
  213. spin_lock_bh(&hinfo->lock);
  214. hlist_add_head(&police->tcf_head, &hinfo->htab[h]);
  215. spin_unlock_bh(&hinfo->lock);
  216. a->priv = police;
  217. return ret;
  218. failure_unlock:
  219. spin_unlock_bh(&police->tcf_lock);
  220. failure:
  221. qdisc_put_rtab(P_tab);
  222. qdisc_put_rtab(R_tab);
  223. if (ret == ACT_P_CREATED)
  224. kfree(police);
  225. return err;
  226. }
  227. static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
  228. struct tcf_result *res)
  229. {
  230. struct tcf_police *police = a->priv;
  231. s64 now;
  232. s64 toks;
  233. s64 ptoks = 0;
  234. spin_lock(&police->tcf_lock);
  235. bstats_update(&police->tcf_bstats, skb);
  236. if (police->tcfp_ewma_rate &&
  237. police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
  238. police->tcf_qstats.overlimits++;
  239. if (police->tcf_action == TC_ACT_SHOT)
  240. police->tcf_qstats.drops++;
  241. spin_unlock(&police->tcf_lock);
  242. return police->tcf_action;
  243. }
  244. if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
  245. if (!police->rate_present) {
  246. spin_unlock(&police->tcf_lock);
  247. return police->tcfp_result;
  248. }
  249. now = ktime_get_ns();
  250. toks = min_t(s64, now - police->tcfp_t_c,
  251. police->tcfp_burst);
  252. if (police->peak_present) {
  253. ptoks = toks + police->tcfp_ptoks;
  254. if (ptoks > police->tcfp_mtu_ptoks)
  255. ptoks = police->tcfp_mtu_ptoks;
  256. ptoks -= (s64) psched_l2t_ns(&police->peak,
  257. qdisc_pkt_len(skb));
  258. }
  259. toks += police->tcfp_toks;
  260. if (toks > police->tcfp_burst)
  261. toks = police->tcfp_burst;
  262. toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
  263. if ((toks|ptoks) >= 0) {
  264. police->tcfp_t_c = now;
  265. police->tcfp_toks = toks;
  266. police->tcfp_ptoks = ptoks;
  267. spin_unlock(&police->tcf_lock);
  268. return police->tcfp_result;
  269. }
  270. }
  271. police->tcf_qstats.overlimits++;
  272. if (police->tcf_action == TC_ACT_SHOT)
  273. police->tcf_qstats.drops++;
  274. spin_unlock(&police->tcf_lock);
  275. return police->tcf_action;
  276. }
  277. static int
  278. tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  279. {
  280. unsigned char *b = skb_tail_pointer(skb);
  281. struct tcf_police *police = a->priv;
  282. struct tc_police opt = {
  283. .index = police->tcf_index,
  284. .action = police->tcf_action,
  285. .mtu = police->tcfp_mtu,
  286. .burst = PSCHED_NS2TICKS(police->tcfp_burst),
  287. .refcnt = police->tcf_refcnt - ref,
  288. .bindcnt = police->tcf_bindcnt - bind,
  289. };
  290. if (police->rate_present)
  291. psched_ratecfg_getrate(&opt.rate, &police->rate);
  292. if (police->peak_present)
  293. psched_ratecfg_getrate(&opt.peakrate, &police->peak);
  294. if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
  295. goto nla_put_failure;
  296. if (police->tcfp_result &&
  297. nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
  298. goto nla_put_failure;
  299. if (police->tcfp_ewma_rate &&
  300. nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
  301. goto nla_put_failure;
  302. return skb->len;
  303. nla_put_failure:
  304. nlmsg_trim(skb, b);
  305. return -1;
  306. }
  307. MODULE_AUTHOR("Alexey Kuznetsov");
  308. MODULE_DESCRIPTION("Policing actions");
  309. MODULE_LICENSE("GPL");
  310. static struct tc_action_ops act_police_ops = {
  311. .kind = "police",
  312. .type = TCA_ID_POLICE,
  313. .owner = THIS_MODULE,
  314. .act = tcf_act_police,
  315. .dump = tcf_act_police_dump,
  316. .init = tcf_act_police_locate,
  317. .walk = tcf_act_police_walker
  318. };
  319. static int __init
  320. police_init_module(void)
  321. {
  322. return tcf_register_action(&act_police_ops, POL_TAB_MASK);
  323. }
  324. static void __exit
  325. police_cleanup_module(void)
  326. {
  327. tcf_unregister_action(&act_police_ops);
  328. }
  329. module_init(police_init_module);
  330. module_exit(police_cleanup_module);