act_police.c 9.9 KB

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