act_police.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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_params {
  24. int tcfp_result;
  25. u32 tcfp_ewma_rate;
  26. s64 tcfp_burst;
  27. u32 tcfp_mtu;
  28. s64 tcfp_mtu_ptoks;
  29. struct psched_ratecfg rate;
  30. bool rate_present;
  31. struct psched_ratecfg peak;
  32. bool peak_present;
  33. struct rcu_head rcu;
  34. };
  35. struct tcf_police {
  36. struct tc_action common;
  37. struct tcf_police_params __rcu *params;
  38. spinlock_t tcfp_lock ____cacheline_aligned_in_smp;
  39. s64 tcfp_toks;
  40. s64 tcfp_ptoks;
  41. s64 tcfp_t_c;
  42. };
  43. #define to_police(pc) ((struct tcf_police *)pc)
  44. /* old policer structure from before tc actions */
  45. struct tc_police_compat {
  46. u32 index;
  47. int action;
  48. u32 limit;
  49. u32 burst;
  50. u32 mtu;
  51. struct tc_ratespec rate;
  52. struct tc_ratespec peakrate;
  53. };
  54. /* Each policer is serialized by its individual spinlock */
  55. static unsigned int police_net_id;
  56. static struct tc_action_ops act_police_ops;
  57. static int tcf_police_walker(struct net *net, struct sk_buff *skb,
  58. struct netlink_callback *cb, int type,
  59. const struct tc_action_ops *ops,
  60. struct netlink_ext_ack *extack)
  61. {
  62. struct tc_action_net *tn = net_generic(net, police_net_id);
  63. return tcf_generic_walker(tn, skb, cb, type, ops, extack);
  64. }
  65. static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
  66. [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
  67. [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
  68. [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
  69. [TCA_POLICE_RESULT] = { .type = NLA_U32 },
  70. };
  71. static int tcf_police_init(struct net *net, struct nlattr *nla,
  72. struct nlattr *est, struct tc_action **a,
  73. int ovr, int bind, bool rtnl_held,
  74. struct netlink_ext_ack *extack)
  75. {
  76. int ret = 0, tcfp_result = TC_ACT_OK, err, size;
  77. struct nlattr *tb[TCA_POLICE_MAX + 1];
  78. struct tc_police *parm;
  79. struct tcf_police *police;
  80. struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
  81. struct tc_action_net *tn = net_generic(net, police_net_id);
  82. struct tcf_police_params *new;
  83. bool exists = false;
  84. if (nla == NULL)
  85. return -EINVAL;
  86. err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy, NULL);
  87. if (err < 0)
  88. return err;
  89. if (tb[TCA_POLICE_TBF] == NULL)
  90. return -EINVAL;
  91. size = nla_len(tb[TCA_POLICE_TBF]);
  92. if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
  93. return -EINVAL;
  94. parm = nla_data(tb[TCA_POLICE_TBF]);
  95. err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
  96. if (err < 0)
  97. return err;
  98. exists = err;
  99. if (exists && bind)
  100. return 0;
  101. if (!exists) {
  102. ret = tcf_idr_create(tn, parm->index, NULL, a,
  103. &act_police_ops, bind, true);
  104. if (ret) {
  105. tcf_idr_cleanup(tn, parm->index);
  106. return ret;
  107. }
  108. ret = ACT_P_CREATED;
  109. spin_lock_init(&(to_police(*a)->tcfp_lock));
  110. } else if (!ovr) {
  111. tcf_idr_release(*a, bind);
  112. return -EEXIST;
  113. }
  114. police = to_police(*a);
  115. if (parm->rate.rate) {
  116. err = -ENOMEM;
  117. R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
  118. if (R_tab == NULL)
  119. goto failure;
  120. if (parm->peakrate.rate) {
  121. P_tab = qdisc_get_rtab(&parm->peakrate,
  122. tb[TCA_POLICE_PEAKRATE], NULL);
  123. if (P_tab == NULL)
  124. goto failure;
  125. }
  126. }
  127. if (est) {
  128. err = gen_replace_estimator(&police->tcf_bstats,
  129. police->common.cpu_bstats,
  130. &police->tcf_rate_est,
  131. &police->tcf_lock,
  132. NULL, est);
  133. if (err)
  134. goto failure;
  135. } else if (tb[TCA_POLICE_AVRATE] &&
  136. (ret == ACT_P_CREATED ||
  137. !gen_estimator_active(&police->tcf_rate_est))) {
  138. err = -EINVAL;
  139. goto failure;
  140. }
  141. if (tb[TCA_POLICE_RESULT]) {
  142. tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
  143. if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
  144. NL_SET_ERR_MSG(extack,
  145. "goto chain not allowed on fallback");
  146. err = -EINVAL;
  147. goto failure;
  148. }
  149. }
  150. new = kzalloc(sizeof(*new), GFP_KERNEL);
  151. if (unlikely(!new)) {
  152. err = -ENOMEM;
  153. goto failure;
  154. }
  155. /* No failure allowed after this point */
  156. new->tcfp_result = tcfp_result;
  157. new->tcfp_mtu = parm->mtu;
  158. if (!new->tcfp_mtu) {
  159. new->tcfp_mtu = ~0;
  160. if (R_tab)
  161. new->tcfp_mtu = 255 << R_tab->rate.cell_log;
  162. }
  163. if (R_tab) {
  164. new->rate_present = true;
  165. psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
  166. qdisc_put_rtab(R_tab);
  167. } else {
  168. new->rate_present = false;
  169. }
  170. if (P_tab) {
  171. new->peak_present = true;
  172. psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
  173. qdisc_put_rtab(P_tab);
  174. } else {
  175. new->peak_present = false;
  176. }
  177. new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
  178. if (new->peak_present)
  179. new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
  180. new->tcfp_mtu);
  181. if (tb[TCA_POLICE_AVRATE])
  182. new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
  183. spin_lock_bh(&police->tcf_lock);
  184. spin_lock_bh(&police->tcfp_lock);
  185. police->tcfp_t_c = ktime_get_ns();
  186. police->tcfp_toks = new->tcfp_burst;
  187. if (new->peak_present)
  188. police->tcfp_ptoks = new->tcfp_mtu_ptoks;
  189. spin_unlock_bh(&police->tcfp_lock);
  190. police->tcf_action = parm->action;
  191. rcu_swap_protected(police->params,
  192. new,
  193. lockdep_is_held(&police->tcf_lock));
  194. spin_unlock_bh(&police->tcf_lock);
  195. if (new)
  196. kfree_rcu(new, rcu);
  197. if (ret == ACT_P_CREATED)
  198. tcf_idr_insert(tn, *a);
  199. return ret;
  200. failure:
  201. qdisc_put_rtab(P_tab);
  202. qdisc_put_rtab(R_tab);
  203. tcf_idr_release(*a, bind);
  204. return err;
  205. }
  206. static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
  207. struct tcf_result *res)
  208. {
  209. struct tcf_police *police = to_police(a);
  210. struct tcf_police_params *p;
  211. s64 now, toks, ptoks = 0;
  212. int ret;
  213. tcf_lastuse_update(&police->tcf_tm);
  214. bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
  215. ret = READ_ONCE(police->tcf_action);
  216. p = rcu_dereference_bh(police->params);
  217. if (p->tcfp_ewma_rate) {
  218. struct gnet_stats_rate_est64 sample;
  219. if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
  220. sample.bps >= p->tcfp_ewma_rate)
  221. goto inc_overlimits;
  222. }
  223. if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
  224. if (!p->rate_present) {
  225. ret = p->tcfp_result;
  226. goto end;
  227. }
  228. now = ktime_get_ns();
  229. spin_lock_bh(&police->tcfp_lock);
  230. toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
  231. if (p->peak_present) {
  232. ptoks = toks + police->tcfp_ptoks;
  233. if (ptoks > p->tcfp_mtu_ptoks)
  234. ptoks = p->tcfp_mtu_ptoks;
  235. ptoks -= (s64)psched_l2t_ns(&p->peak,
  236. qdisc_pkt_len(skb));
  237. }
  238. toks += police->tcfp_toks;
  239. if (toks > p->tcfp_burst)
  240. toks = p->tcfp_burst;
  241. toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
  242. if ((toks|ptoks) >= 0) {
  243. police->tcfp_t_c = now;
  244. police->tcfp_toks = toks;
  245. police->tcfp_ptoks = ptoks;
  246. spin_unlock_bh(&police->tcfp_lock);
  247. ret = p->tcfp_result;
  248. goto inc_drops;
  249. }
  250. spin_unlock_bh(&police->tcfp_lock);
  251. }
  252. inc_overlimits:
  253. qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
  254. inc_drops:
  255. if (ret == TC_ACT_SHOT)
  256. qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
  257. end:
  258. return ret;
  259. }
  260. static void tcf_police_cleanup(struct tc_action *a)
  261. {
  262. struct tcf_police *police = to_police(a);
  263. struct tcf_police_params *p;
  264. p = rcu_dereference_protected(police->params, 1);
  265. if (p)
  266. kfree_rcu(p, rcu);
  267. }
  268. static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
  269. int bind, int ref)
  270. {
  271. unsigned char *b = skb_tail_pointer(skb);
  272. struct tcf_police *police = to_police(a);
  273. struct tcf_police_params *p;
  274. struct tc_police opt = {
  275. .index = police->tcf_index,
  276. .refcnt = refcount_read(&police->tcf_refcnt) - ref,
  277. .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
  278. };
  279. struct tcf_t t;
  280. spin_lock_bh(&police->tcf_lock);
  281. opt.action = police->tcf_action;
  282. p = rcu_dereference_protected(police->params,
  283. lockdep_is_held(&police->tcf_lock));
  284. opt.mtu = p->tcfp_mtu;
  285. opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
  286. if (p->rate_present)
  287. psched_ratecfg_getrate(&opt.rate, &p->rate);
  288. if (p->peak_present)
  289. psched_ratecfg_getrate(&opt.peakrate, &p->peak);
  290. if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
  291. goto nla_put_failure;
  292. if (p->tcfp_result &&
  293. nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
  294. goto nla_put_failure;
  295. if (p->tcfp_ewma_rate &&
  296. nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
  297. goto nla_put_failure;
  298. t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
  299. t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
  300. t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
  301. t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
  302. if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
  303. goto nla_put_failure;
  304. spin_unlock_bh(&police->tcf_lock);
  305. return skb->len;
  306. nla_put_failure:
  307. spin_unlock_bh(&police->tcf_lock);
  308. nlmsg_trim(skb, b);
  309. return -1;
  310. }
  311. static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
  312. {
  313. struct tc_action_net *tn = net_generic(net, police_net_id);
  314. return tcf_idr_search(tn, a, index);
  315. }
  316. MODULE_AUTHOR("Alexey Kuznetsov");
  317. MODULE_DESCRIPTION("Policing actions");
  318. MODULE_LICENSE("GPL");
  319. static struct tc_action_ops act_police_ops = {
  320. .kind = "police",
  321. .type = TCA_ID_POLICE,
  322. .owner = THIS_MODULE,
  323. .act = tcf_police_act,
  324. .dump = tcf_police_dump,
  325. .init = tcf_police_init,
  326. .walk = tcf_police_walker,
  327. .lookup = tcf_police_search,
  328. .cleanup = tcf_police_cleanup,
  329. .size = sizeof(struct tcf_police),
  330. };
  331. static __net_init int police_init_net(struct net *net)
  332. {
  333. struct tc_action_net *tn = net_generic(net, police_net_id);
  334. return tc_action_net_init(tn, &act_police_ops);
  335. }
  336. static void __net_exit police_exit_net(struct list_head *net_list)
  337. {
  338. tc_action_net_exit(net_list, police_net_id);
  339. }
  340. static struct pernet_operations police_net_ops = {
  341. .init = police_init_net,
  342. .exit_batch = police_exit_net,
  343. .id = &police_net_id,
  344. .size = sizeof(struct tc_action_net),
  345. };
  346. static int __init police_init_module(void)
  347. {
  348. return tcf_register_action(&act_police_ops, &police_net_ops);
  349. }
  350. static void __exit police_cleanup_module(void)
  351. {
  352. tcf_unregister_action(&act_police_ops, &police_net_ops);
  353. }
  354. module_init(police_init_module);
  355. module_exit(police_cleanup_module);