act_police.c 9.6 KB

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