act_police.c 9.2 KB

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