act_police.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 tc_action 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) ((struct tcf_police *)pc)
  39. #define POL_TAB_MASK 15
  40. /* old policer structure from before tc actions */
  41. struct tc_police_compat {
  42. u32 index;
  43. int action;
  44. u32 limit;
  45. u32 burst;
  46. u32 mtu;
  47. struct tc_ratespec rate;
  48. struct tc_ratespec peakrate;
  49. };
  50. /* Each policer is serialized by its individual spinlock */
  51. static int police_net_id;
  52. static struct tc_action_ops act_police_ops;
  53. static int tcf_act_police_walker(struct net *net, struct sk_buff *skb,
  54. struct netlink_callback *cb, int type,
  55. const struct tc_action_ops *ops)
  56. {
  57. struct tc_action_net *tn = net_generic(net, police_net_id);
  58. return tcf_generic_walker(tn, skb, cb, type, ops);
  59. }
  60. static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
  61. [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
  62. [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
  63. [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
  64. [TCA_POLICE_RESULT] = { .type = NLA_U32 },
  65. };
  66. static int tcf_act_police_init(struct net *net, struct nlattr *nla,
  67. struct nlattr *est, struct tc_action **a,
  68. int ovr, int bind)
  69. {
  70. int ret = 0, err;
  71. struct nlattr *tb[TCA_POLICE_MAX + 1];
  72. struct tc_police *parm;
  73. struct tcf_police *police;
  74. struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
  75. struct tc_action_net *tn = net_generic(net, police_net_id);
  76. bool exists = false;
  77. int size;
  78. if (nla == NULL)
  79. return -EINVAL;
  80. err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy);
  81. if (err < 0)
  82. return err;
  83. if (tb[TCA_POLICE_TBF] == NULL)
  84. return -EINVAL;
  85. size = nla_len(tb[TCA_POLICE_TBF]);
  86. if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
  87. return -EINVAL;
  88. parm = nla_data(tb[TCA_POLICE_TBF]);
  89. exists = tcf_hash_check(tn, parm->index, a, bind);
  90. if (exists && bind)
  91. return 0;
  92. if (!exists) {
  93. ret = tcf_hash_create(tn, parm->index, NULL, a,
  94. &act_police_ops, bind, false);
  95. if (ret)
  96. return ret;
  97. ret = ACT_P_CREATED;
  98. } else {
  99. tcf_hash_release(*a, bind);
  100. if (!ovr)
  101. return -EEXIST;
  102. }
  103. police = to_police(*a);
  104. if (parm->rate.rate) {
  105. err = -ENOMEM;
  106. R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
  107. if (R_tab == NULL)
  108. goto failure;
  109. if (parm->peakrate.rate) {
  110. P_tab = qdisc_get_rtab(&parm->peakrate,
  111. tb[TCA_POLICE_PEAKRATE]);
  112. if (P_tab == NULL)
  113. goto failure;
  114. }
  115. }
  116. spin_lock_bh(&police->tcf_lock);
  117. if (est) {
  118. err = gen_replace_estimator(&police->tcf_bstats, NULL,
  119. &police->tcf_rate_est,
  120. &police->tcf_lock,
  121. NULL, est);
  122. if (err)
  123. goto failure_unlock;
  124. } else if (tb[TCA_POLICE_AVRATE] &&
  125. (ret == ACT_P_CREATED ||
  126. !gen_estimator_active(&police->tcf_bstats,
  127. &police->tcf_rate_est))) {
  128. err = -EINVAL;
  129. goto failure_unlock;
  130. }
  131. /* No failure allowed after this point */
  132. police->tcfp_mtu = parm->mtu;
  133. if (police->tcfp_mtu == 0) {
  134. police->tcfp_mtu = ~0;
  135. if (R_tab)
  136. police->tcfp_mtu = 255 << R_tab->rate.cell_log;
  137. }
  138. if (R_tab) {
  139. police->rate_present = true;
  140. psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
  141. qdisc_put_rtab(R_tab);
  142. } else {
  143. police->rate_present = false;
  144. }
  145. if (P_tab) {
  146. police->peak_present = true;
  147. psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
  148. qdisc_put_rtab(P_tab);
  149. } else {
  150. police->peak_present = false;
  151. }
  152. if (tb[TCA_POLICE_RESULT])
  153. police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
  154. police->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
  155. police->tcfp_toks = police->tcfp_burst;
  156. if (police->peak_present) {
  157. police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak,
  158. police->tcfp_mtu);
  159. police->tcfp_ptoks = police->tcfp_mtu_ptoks;
  160. }
  161. police->tcf_action = parm->action;
  162. if (tb[TCA_POLICE_AVRATE])
  163. police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
  164. spin_unlock_bh(&police->tcf_lock);
  165. if (ret != ACT_P_CREATED)
  166. return ret;
  167. police->tcfp_t_c = ktime_get_ns();
  168. tcf_hash_insert(tn, *a);
  169. return ret;
  170. failure_unlock:
  171. spin_unlock_bh(&police->tcf_lock);
  172. failure:
  173. qdisc_put_rtab(P_tab);
  174. qdisc_put_rtab(R_tab);
  175. if (ret == ACT_P_CREATED)
  176. tcf_hash_cleanup(*a, est);
  177. return err;
  178. }
  179. static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
  180. struct tcf_result *res)
  181. {
  182. struct tcf_police *police = to_police(a);
  183. s64 now;
  184. s64 toks;
  185. s64 ptoks = 0;
  186. spin_lock(&police->tcf_lock);
  187. bstats_update(&police->tcf_bstats, skb);
  188. tcf_lastuse_update(&police->tcf_tm);
  189. if (police->tcfp_ewma_rate &&
  190. police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
  191. police->tcf_qstats.overlimits++;
  192. if (police->tcf_action == TC_ACT_SHOT)
  193. police->tcf_qstats.drops++;
  194. spin_unlock(&police->tcf_lock);
  195. return police->tcf_action;
  196. }
  197. if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
  198. if (!police->rate_present) {
  199. spin_unlock(&police->tcf_lock);
  200. return police->tcfp_result;
  201. }
  202. now = ktime_get_ns();
  203. toks = min_t(s64, now - police->tcfp_t_c,
  204. police->tcfp_burst);
  205. if (police->peak_present) {
  206. ptoks = toks + police->tcfp_ptoks;
  207. if (ptoks > police->tcfp_mtu_ptoks)
  208. ptoks = police->tcfp_mtu_ptoks;
  209. ptoks -= (s64) psched_l2t_ns(&police->peak,
  210. qdisc_pkt_len(skb));
  211. }
  212. toks += police->tcfp_toks;
  213. if (toks > police->tcfp_burst)
  214. toks = police->tcfp_burst;
  215. toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
  216. if ((toks|ptoks) >= 0) {
  217. police->tcfp_t_c = now;
  218. police->tcfp_toks = toks;
  219. police->tcfp_ptoks = ptoks;
  220. if (police->tcfp_result == TC_ACT_SHOT)
  221. police->tcf_qstats.drops++;
  222. spin_unlock(&police->tcf_lock);
  223. return police->tcfp_result;
  224. }
  225. }
  226. police->tcf_qstats.overlimits++;
  227. if (police->tcf_action == TC_ACT_SHOT)
  228. police->tcf_qstats.drops++;
  229. spin_unlock(&police->tcf_lock);
  230. return police->tcf_action;
  231. }
  232. static int tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a,
  233. int bind, int ref)
  234. {
  235. unsigned char *b = skb_tail_pointer(skb);
  236. struct tcf_police *police = to_police(a);
  237. struct tc_police opt = {
  238. .index = police->tcf_index,
  239. .action = police->tcf_action,
  240. .mtu = police->tcfp_mtu,
  241. .burst = PSCHED_NS2TICKS(police->tcfp_burst),
  242. .refcnt = police->tcf_refcnt - ref,
  243. .bindcnt = police->tcf_bindcnt - bind,
  244. };
  245. struct tcf_t t;
  246. if (police->rate_present)
  247. psched_ratecfg_getrate(&opt.rate, &police->rate);
  248. if (police->peak_present)
  249. psched_ratecfg_getrate(&opt.peakrate, &police->peak);
  250. if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
  251. goto nla_put_failure;
  252. if (police->tcfp_result &&
  253. nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
  254. goto nla_put_failure;
  255. if (police->tcfp_ewma_rate &&
  256. nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
  257. goto nla_put_failure;
  258. t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
  259. t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
  260. t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
  261. t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
  262. if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
  263. goto nla_put_failure;
  264. return skb->len;
  265. nla_put_failure:
  266. nlmsg_trim(skb, b);
  267. return -1;
  268. }
  269. static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
  270. {
  271. struct tc_action_net *tn = net_generic(net, police_net_id);
  272. return tcf_hash_search(tn, a, index);
  273. }
  274. MODULE_AUTHOR("Alexey Kuznetsov");
  275. MODULE_DESCRIPTION("Policing actions");
  276. MODULE_LICENSE("GPL");
  277. static struct tc_action_ops act_police_ops = {
  278. .kind = "police",
  279. .type = TCA_ID_POLICE,
  280. .owner = THIS_MODULE,
  281. .act = tcf_act_police,
  282. .dump = tcf_act_police_dump,
  283. .init = tcf_act_police_init,
  284. .walk = tcf_act_police_walker,
  285. .lookup = tcf_police_search,
  286. .size = sizeof(struct tcf_police),
  287. };
  288. static __net_init int police_init_net(struct net *net)
  289. {
  290. struct tc_action_net *tn = net_generic(net, police_net_id);
  291. return tc_action_net_init(tn, &act_police_ops, POL_TAB_MASK);
  292. }
  293. static void __net_exit police_exit_net(struct net *net)
  294. {
  295. struct tc_action_net *tn = net_generic(net, police_net_id);
  296. tc_action_net_exit(tn);
  297. }
  298. static struct pernet_operations police_net_ops = {
  299. .init = police_init_net,
  300. .exit = police_exit_net,
  301. .id = &police_net_id,
  302. .size = sizeof(struct tc_action_net),
  303. };
  304. static int __init police_init_module(void)
  305. {
  306. return tcf_register_action(&act_police_ops, &police_net_ops);
  307. }
  308. static void __exit police_cleanup_module(void)
  309. {
  310. tcf_unregister_action(&act_police_ops, &police_net_ops);
  311. }
  312. module_init(police_init_module);
  313. module_exit(police_cleanup_module);