act_police.c 9.5 KB

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