act_police.c 9.8 KB

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