act_police.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 unsigned 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_rate_est))) {
  127. err = -EINVAL;
  128. goto failure_unlock;
  129. }
  130. /* No failure allowed after this point */
  131. police->tcfp_mtu = parm->mtu;
  132. if (police->tcfp_mtu == 0) {
  133. police->tcfp_mtu = ~0;
  134. if (R_tab)
  135. police->tcfp_mtu = 255 << R_tab->rate.cell_log;
  136. }
  137. if (R_tab) {
  138. police->rate_present = true;
  139. psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
  140. qdisc_put_rtab(R_tab);
  141. } else {
  142. police->rate_present = false;
  143. }
  144. if (P_tab) {
  145. police->peak_present = true;
  146. psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
  147. qdisc_put_rtab(P_tab);
  148. } else {
  149. police->peak_present = false;
  150. }
  151. if (tb[TCA_POLICE_RESULT])
  152. police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
  153. police->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
  154. police->tcfp_toks = police->tcfp_burst;
  155. if (police->peak_present) {
  156. police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak,
  157. police->tcfp_mtu);
  158. police->tcfp_ptoks = police->tcfp_mtu_ptoks;
  159. }
  160. police->tcf_action = parm->action;
  161. if (tb[TCA_POLICE_AVRATE])
  162. police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
  163. spin_unlock_bh(&police->tcf_lock);
  164. if (ret != ACT_P_CREATED)
  165. return ret;
  166. police->tcfp_t_c = ktime_get_ns();
  167. tcf_hash_insert(tn, *a);
  168. return ret;
  169. failure_unlock:
  170. spin_unlock_bh(&police->tcf_lock);
  171. failure:
  172. qdisc_put_rtab(P_tab);
  173. qdisc_put_rtab(R_tab);
  174. if (ret == ACT_P_CREATED)
  175. tcf_hash_cleanup(*a, est);
  176. return err;
  177. }
  178. static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
  179. struct tcf_result *res)
  180. {
  181. struct tcf_police *police = to_police(a);
  182. s64 now;
  183. s64 toks;
  184. s64 ptoks = 0;
  185. spin_lock(&police->tcf_lock);
  186. bstats_update(&police->tcf_bstats, skb);
  187. tcf_lastuse_update(&police->tcf_tm);
  188. if (police->tcfp_ewma_rate) {
  189. struct gnet_stats_rate_est64 sample;
  190. if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
  191. sample.bps >= police->tcfp_ewma_rate) {
  192. police->tcf_qstats.overlimits++;
  193. if (police->tcf_action == TC_ACT_SHOT)
  194. police->tcf_qstats.drops++;
  195. spin_unlock(&police->tcf_lock);
  196. return police->tcf_action;
  197. }
  198. }
  199. if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
  200. if (!police->rate_present) {
  201. spin_unlock(&police->tcf_lock);
  202. return police->tcfp_result;
  203. }
  204. now = ktime_get_ns();
  205. toks = min_t(s64, now - police->tcfp_t_c,
  206. police->tcfp_burst);
  207. if (police->peak_present) {
  208. ptoks = toks + police->tcfp_ptoks;
  209. if (ptoks > police->tcfp_mtu_ptoks)
  210. ptoks = police->tcfp_mtu_ptoks;
  211. ptoks -= (s64) psched_l2t_ns(&police->peak,
  212. qdisc_pkt_len(skb));
  213. }
  214. toks += police->tcfp_toks;
  215. if (toks > police->tcfp_burst)
  216. toks = police->tcfp_burst;
  217. toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
  218. if ((toks|ptoks) >= 0) {
  219. police->tcfp_t_c = now;
  220. police->tcfp_toks = toks;
  221. police->tcfp_ptoks = ptoks;
  222. if (police->tcfp_result == TC_ACT_SHOT)
  223. police->tcf_qstats.drops++;
  224. spin_unlock(&police->tcf_lock);
  225. return police->tcfp_result;
  226. }
  227. }
  228. police->tcf_qstats.overlimits++;
  229. if (police->tcf_action == TC_ACT_SHOT)
  230. police->tcf_qstats.drops++;
  231. spin_unlock(&police->tcf_lock);
  232. return police->tcf_action;
  233. }
  234. static int tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a,
  235. int bind, int ref)
  236. {
  237. unsigned char *b = skb_tail_pointer(skb);
  238. struct tcf_police *police = to_police(a);
  239. struct tc_police opt = {
  240. .index = police->tcf_index,
  241. .action = police->tcf_action,
  242. .mtu = police->tcfp_mtu,
  243. .burst = PSCHED_NS2TICKS(police->tcfp_burst),
  244. .refcnt = police->tcf_refcnt - ref,
  245. .bindcnt = police->tcf_bindcnt - bind,
  246. };
  247. struct tcf_t t;
  248. if (police->rate_present)
  249. psched_ratecfg_getrate(&opt.rate, &police->rate);
  250. if (police->peak_present)
  251. psched_ratecfg_getrate(&opt.peakrate, &police->peak);
  252. if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
  253. goto nla_put_failure;
  254. if (police->tcfp_result &&
  255. nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
  256. goto nla_put_failure;
  257. if (police->tcfp_ewma_rate &&
  258. nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
  259. goto nla_put_failure;
  260. t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
  261. t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
  262. t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
  263. t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
  264. if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
  265. goto nla_put_failure;
  266. return skb->len;
  267. nla_put_failure:
  268. nlmsg_trim(skb, b);
  269. return -1;
  270. }
  271. static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
  272. {
  273. struct tc_action_net *tn = net_generic(net, police_net_id);
  274. return tcf_hash_search(tn, a, index);
  275. }
  276. MODULE_AUTHOR("Alexey Kuznetsov");
  277. MODULE_DESCRIPTION("Policing actions");
  278. MODULE_LICENSE("GPL");
  279. static struct tc_action_ops act_police_ops = {
  280. .kind = "police",
  281. .type = TCA_ID_POLICE,
  282. .owner = THIS_MODULE,
  283. .act = tcf_act_police,
  284. .dump = tcf_act_police_dump,
  285. .init = tcf_act_police_init,
  286. .walk = tcf_act_police_walker,
  287. .lookup = tcf_police_search,
  288. .size = sizeof(struct tcf_police),
  289. };
  290. static __net_init int police_init_net(struct net *net)
  291. {
  292. struct tc_action_net *tn = net_generic(net, police_net_id);
  293. return tc_action_net_init(tn, &act_police_ops, POL_TAB_MASK);
  294. }
  295. static void __net_exit police_exit_net(struct net *net)
  296. {
  297. struct tc_action_net *tn = net_generic(net, police_net_id);
  298. tc_action_net_exit(tn);
  299. }
  300. static struct pernet_operations police_net_ops = {
  301. .init = police_init_net,
  302. .exit = police_exit_net,
  303. .id = &police_net_id,
  304. .size = sizeof(struct tc_action_net),
  305. };
  306. static int __init police_init_module(void)
  307. {
  308. return tcf_register_action(&act_police_ops, &police_net_ops);
  309. }
  310. static void __exit police_cleanup_module(void)
  311. {
  312. tcf_unregister_action(&act_police_ops, &police_net_ops);
  313. }
  314. module_init(police_init_module);
  315. module_exit(police_cleanup_module);