act_police.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * net/sched/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 tcf_common 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) \
  39. container_of(pc, struct tcf_police, common)
  40. #define POL_TAB_MASK 15
  41. static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1];
  42. static u32 police_idx_gen;
  43. static DEFINE_RWLOCK(police_lock);
  44. static struct tcf_hashinfo police_hash_info = {
  45. .htab = tcf_police_ht,
  46. .hmask = POL_TAB_MASK,
  47. .lock = &police_lock,
  48. };
  49. /* old policer structure from before tc actions */
  50. struct tc_police_compat {
  51. u32 index;
  52. int action;
  53. u32 limit;
  54. u32 burst;
  55. u32 mtu;
  56. struct tc_ratespec rate;
  57. struct tc_ratespec peakrate;
  58. };
  59. /* Each policer is serialized by its individual spinlock */
  60. static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
  61. int type, struct tc_action *a)
  62. {
  63. struct tcf_common *p;
  64. int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
  65. struct nlattr *nest;
  66. read_lock_bh(&police_lock);
  67. s_i = cb->args[0];
  68. for (i = 0; i < (POL_TAB_MASK + 1); i++) {
  69. p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)];
  70. for (; p; p = p->tcfc_next) {
  71. index++;
  72. if (index < s_i)
  73. continue;
  74. a->priv = p;
  75. a->order = index;
  76. nest = nla_nest_start(skb, a->order);
  77. if (nest == NULL)
  78. goto nla_put_failure;
  79. if (type == RTM_DELACTION)
  80. err = tcf_action_dump_1(skb, a, 0, 1);
  81. else
  82. err = tcf_action_dump_1(skb, a, 0, 0);
  83. if (err < 0) {
  84. index--;
  85. nla_nest_cancel(skb, nest);
  86. goto done;
  87. }
  88. nla_nest_end(skb, nest);
  89. n_i++;
  90. }
  91. }
  92. done:
  93. read_unlock_bh(&police_lock);
  94. if (n_i)
  95. cb->args[0] += n_i;
  96. return n_i;
  97. nla_put_failure:
  98. nla_nest_cancel(skb, nest);
  99. goto done;
  100. }
  101. static void tcf_police_destroy(struct tcf_police *p)
  102. {
  103. unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK);
  104. struct tcf_common **p1p;
  105. for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
  106. if (*p1p == &p->common) {
  107. write_lock_bh(&police_lock);
  108. *p1p = p->tcf_next;
  109. write_unlock_bh(&police_lock);
  110. gen_kill_estimator(&p->tcf_bstats,
  111. &p->tcf_rate_est);
  112. /*
  113. * gen_estimator est_timer() might access p->tcf_lock
  114. * or bstats, wait a RCU grace period before freeing p
  115. */
  116. kfree_rcu(p, tcf_rcu);
  117. return;
  118. }
  119. }
  120. WARN_ON(1);
  121. }
  122. static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
  123. [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
  124. [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
  125. [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
  126. [TCA_POLICE_RESULT] = { .type = NLA_U32 },
  127. };
  128. static int tcf_act_police_locate(struct net *net, struct nlattr *nla,
  129. struct nlattr *est, struct tc_action *a,
  130. int ovr, int bind)
  131. {
  132. unsigned int h;
  133. int ret = 0, err;
  134. struct nlattr *tb[TCA_POLICE_MAX + 1];
  135. struct tc_police *parm;
  136. struct tcf_police *police;
  137. struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
  138. int size;
  139. if (nla == NULL)
  140. return -EINVAL;
  141. err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy);
  142. if (err < 0)
  143. return err;
  144. if (tb[TCA_POLICE_TBF] == NULL)
  145. return -EINVAL;
  146. size = nla_len(tb[TCA_POLICE_TBF]);
  147. if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
  148. return -EINVAL;
  149. parm = nla_data(tb[TCA_POLICE_TBF]);
  150. if (parm->index) {
  151. struct tcf_common *pc;
  152. pc = tcf_hash_lookup(parm->index, &police_hash_info);
  153. if (pc != NULL) {
  154. a->priv = pc;
  155. police = to_police(pc);
  156. if (bind) {
  157. police->tcf_bindcnt += 1;
  158. police->tcf_refcnt += 1;
  159. }
  160. if (ovr)
  161. goto override;
  162. return ret;
  163. }
  164. }
  165. police = kzalloc(sizeof(*police), GFP_KERNEL);
  166. if (police == NULL)
  167. return -ENOMEM;
  168. ret = ACT_P_CREATED;
  169. police->tcf_refcnt = 1;
  170. spin_lock_init(&police->tcf_lock);
  171. if (bind)
  172. police->tcf_bindcnt = 1;
  173. override:
  174. if (parm->rate.rate) {
  175. err = -ENOMEM;
  176. R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
  177. if (R_tab == NULL)
  178. goto failure;
  179. if (parm->peakrate.rate) {
  180. P_tab = qdisc_get_rtab(&parm->peakrate,
  181. tb[TCA_POLICE_PEAKRATE]);
  182. if (P_tab == NULL)
  183. goto failure;
  184. }
  185. }
  186. spin_lock_bh(&police->tcf_lock);
  187. if (est) {
  188. err = gen_replace_estimator(&police->tcf_bstats,
  189. &police->tcf_rate_est,
  190. &police->tcf_lock, est);
  191. if (err)
  192. goto failure_unlock;
  193. } else if (tb[TCA_POLICE_AVRATE] &&
  194. (ret == ACT_P_CREATED ||
  195. !gen_estimator_active(&police->tcf_bstats,
  196. &police->tcf_rate_est))) {
  197. err = -EINVAL;
  198. goto failure_unlock;
  199. }
  200. /* No failure allowed after this point */
  201. police->tcfp_mtu = parm->mtu;
  202. if (police->tcfp_mtu == 0) {
  203. police->tcfp_mtu = ~0;
  204. if (R_tab)
  205. police->tcfp_mtu = 255 << R_tab->rate.cell_log;
  206. }
  207. if (R_tab) {
  208. police->rate_present = true;
  209. psched_ratecfg_precompute(&police->rate, R_tab->rate.rate);
  210. qdisc_put_rtab(R_tab);
  211. } else {
  212. police->rate_present = false;
  213. }
  214. if (P_tab) {
  215. police->peak_present = true;
  216. psched_ratecfg_precompute(&police->peak, P_tab->rate.rate);
  217. qdisc_put_rtab(P_tab);
  218. } else {
  219. police->peak_present = false;
  220. }
  221. if (tb[TCA_POLICE_RESULT])
  222. police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
  223. police->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
  224. police->tcfp_toks = police->tcfp_burst;
  225. if (police->peak_present) {
  226. police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak,
  227. police->tcfp_mtu);
  228. police->tcfp_ptoks = police->tcfp_mtu_ptoks;
  229. }
  230. police->tcf_action = parm->action;
  231. if (tb[TCA_POLICE_AVRATE])
  232. police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
  233. spin_unlock_bh(&police->tcf_lock);
  234. if (ret != ACT_P_CREATED)
  235. return ret;
  236. police->tcfp_t_c = ktime_to_ns(ktime_get());
  237. police->tcf_index = parm->index ? parm->index :
  238. tcf_hash_new_index(&police_idx_gen, &police_hash_info);
  239. h = tcf_hash(police->tcf_index, POL_TAB_MASK);
  240. write_lock_bh(&police_lock);
  241. police->tcf_next = tcf_police_ht[h];
  242. tcf_police_ht[h] = &police->common;
  243. write_unlock_bh(&police_lock);
  244. a->priv = police;
  245. return ret;
  246. failure_unlock:
  247. spin_unlock_bh(&police->tcf_lock);
  248. failure:
  249. if (P_tab)
  250. qdisc_put_rtab(P_tab);
  251. if (R_tab)
  252. qdisc_put_rtab(R_tab);
  253. if (ret == ACT_P_CREATED)
  254. kfree(police);
  255. return err;
  256. }
  257. static int tcf_act_police_cleanup(struct tc_action *a, int bind)
  258. {
  259. struct tcf_police *p = a->priv;
  260. int ret = 0;
  261. if (p != NULL) {
  262. if (bind)
  263. p->tcf_bindcnt--;
  264. p->tcf_refcnt--;
  265. if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
  266. tcf_police_destroy(p);
  267. ret = 1;
  268. }
  269. }
  270. return ret;
  271. }
  272. static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
  273. struct tcf_result *res)
  274. {
  275. struct tcf_police *police = a->priv;
  276. s64 now;
  277. s64 toks;
  278. s64 ptoks = 0;
  279. spin_lock(&police->tcf_lock);
  280. bstats_update(&police->tcf_bstats, skb);
  281. if (police->tcfp_ewma_rate &&
  282. police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
  283. police->tcf_qstats.overlimits++;
  284. if (police->tcf_action == TC_ACT_SHOT)
  285. police->tcf_qstats.drops++;
  286. spin_unlock(&police->tcf_lock);
  287. return police->tcf_action;
  288. }
  289. if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
  290. if (!police->rate_present) {
  291. spin_unlock(&police->tcf_lock);
  292. return police->tcfp_result;
  293. }
  294. now = ktime_to_ns(ktime_get());
  295. toks = min_t(s64, now - police->tcfp_t_c,
  296. police->tcfp_burst);
  297. if (police->peak_present) {
  298. ptoks = toks + police->tcfp_ptoks;
  299. if (ptoks > police->tcfp_mtu_ptoks)
  300. ptoks = police->tcfp_mtu_ptoks;
  301. ptoks -= (s64) psched_l2t_ns(&police->peak,
  302. qdisc_pkt_len(skb));
  303. }
  304. toks += police->tcfp_toks;
  305. if (toks > police->tcfp_burst)
  306. toks = police->tcfp_burst;
  307. toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
  308. if ((toks|ptoks) >= 0) {
  309. police->tcfp_t_c = now;
  310. police->tcfp_toks = toks;
  311. police->tcfp_ptoks = ptoks;
  312. spin_unlock(&police->tcf_lock);
  313. return police->tcfp_result;
  314. }
  315. }
  316. police->tcf_qstats.overlimits++;
  317. if (police->tcf_action == TC_ACT_SHOT)
  318. police->tcf_qstats.drops++;
  319. spin_unlock(&police->tcf_lock);
  320. return police->tcf_action;
  321. }
  322. static int
  323. tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  324. {
  325. unsigned char *b = skb_tail_pointer(skb);
  326. struct tcf_police *police = a->priv;
  327. struct tc_police opt = {
  328. .index = police->tcf_index,
  329. .action = police->tcf_action,
  330. .mtu = police->tcfp_mtu,
  331. .burst = PSCHED_NS2TICKS(police->tcfp_burst),
  332. .refcnt = police->tcf_refcnt - ref,
  333. .bindcnt = police->tcf_bindcnt - bind,
  334. };
  335. if (police->rate_present)
  336. opt.rate.rate = psched_ratecfg_getrate(&police->rate);
  337. if (police->peak_present)
  338. opt.peakrate.rate = psched_ratecfg_getrate(&police->peak);
  339. if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
  340. goto nla_put_failure;
  341. if (police->tcfp_result &&
  342. nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
  343. goto nla_put_failure;
  344. if (police->tcfp_ewma_rate &&
  345. nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
  346. goto nla_put_failure;
  347. return skb->len;
  348. nla_put_failure:
  349. nlmsg_trim(skb, b);
  350. return -1;
  351. }
  352. MODULE_AUTHOR("Alexey Kuznetsov");
  353. MODULE_DESCRIPTION("Policing actions");
  354. MODULE_LICENSE("GPL");
  355. static struct tc_action_ops act_police_ops = {
  356. .kind = "police",
  357. .hinfo = &police_hash_info,
  358. .type = TCA_ID_POLICE,
  359. .capab = TCA_CAP_NONE,
  360. .owner = THIS_MODULE,
  361. .act = tcf_act_police,
  362. .dump = tcf_act_police_dump,
  363. .cleanup = tcf_act_police_cleanup,
  364. .lookup = tcf_hash_search,
  365. .init = tcf_act_police_locate,
  366. .walk = tcf_act_police_walker
  367. };
  368. static int __init
  369. police_init_module(void)
  370. {
  371. return tcf_register_action(&act_police_ops);
  372. }
  373. static void __exit
  374. police_cleanup_module(void)
  375. {
  376. tcf_unregister_action(&act_police_ops);
  377. }
  378. module_init(police_init_module);
  379. module_exit(police_cleanup_module);