act_police.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <net/act_api.h>
  21. #include <net/netlink.h>
  22. #define L2T(p,L) qdisc_l2t((p)->tcfp_R_tab, L)
  23. #define L2T_P(p,L) qdisc_l2t((p)->tcfp_P_tab, L)
  24. #define POL_TAB_MASK 15
  25. static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1];
  26. static u32 police_idx_gen;
  27. static DEFINE_RWLOCK(police_lock);
  28. static struct tcf_hashinfo police_hash_info = {
  29. .htab = tcf_police_ht,
  30. .hmask = POL_TAB_MASK,
  31. .lock = &police_lock,
  32. };
  33. /* old policer structure from before tc actions */
  34. struct tc_police_compat
  35. {
  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 rtattr *r;
  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. r = (struct rtattr *)skb_tail_pointer(skb);
  62. RTA_PUT(skb, a->order, 0, NULL);
  63. if (type == RTM_DELACTION)
  64. err = tcf_action_dump_1(skb, a, 0, 1);
  65. else
  66. err = tcf_action_dump_1(skb, a, 0, 0);
  67. if (err < 0) {
  68. index--;
  69. nlmsg_trim(skb, r);
  70. goto done;
  71. }
  72. r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
  73. n_i++;
  74. }
  75. }
  76. done:
  77. read_unlock_bh(&police_lock);
  78. if (n_i)
  79. cb->args[0] += n_i;
  80. return n_i;
  81. rtattr_failure:
  82. nlmsg_trim(skb, r);
  83. goto done;
  84. }
  85. static void tcf_police_destroy(struct tcf_police *p)
  86. {
  87. unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK);
  88. struct tcf_common **p1p;
  89. for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
  90. if (*p1p == &p->common) {
  91. write_lock_bh(&police_lock);
  92. *p1p = p->tcf_next;
  93. write_unlock_bh(&police_lock);
  94. gen_kill_estimator(&p->tcf_bstats,
  95. &p->tcf_rate_est);
  96. if (p->tcfp_R_tab)
  97. qdisc_put_rtab(p->tcfp_R_tab);
  98. if (p->tcfp_P_tab)
  99. qdisc_put_rtab(p->tcfp_P_tab);
  100. kfree(p);
  101. return;
  102. }
  103. }
  104. BUG_TRAP(0);
  105. }
  106. static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
  107. struct tc_action *a, int ovr, int bind)
  108. {
  109. unsigned h;
  110. int ret = 0, err;
  111. struct rtattr *tb[TCA_POLICE_MAX];
  112. struct tc_police *parm;
  113. struct tcf_police *police;
  114. struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
  115. int size;
  116. if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
  117. return -EINVAL;
  118. if (tb[TCA_POLICE_TBF-1] == NULL)
  119. return -EINVAL;
  120. size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]);
  121. if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
  122. return -EINVAL;
  123. parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
  124. if (tb[TCA_POLICE_RESULT-1] != NULL &&
  125. RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
  126. return -EINVAL;
  127. if (tb[TCA_POLICE_RESULT-1] != NULL &&
  128. RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
  129. return -EINVAL;
  130. if (parm->index) {
  131. struct tcf_common *pc;
  132. pc = tcf_hash_lookup(parm->index, &police_hash_info);
  133. if (pc != NULL) {
  134. a->priv = pc;
  135. police = to_police(pc);
  136. if (bind) {
  137. police->tcf_bindcnt += 1;
  138. police->tcf_refcnt += 1;
  139. }
  140. if (ovr)
  141. goto override;
  142. return ret;
  143. }
  144. }
  145. police = kzalloc(sizeof(*police), GFP_KERNEL);
  146. if (police == NULL)
  147. return -ENOMEM;
  148. ret = ACT_P_CREATED;
  149. police->tcf_refcnt = 1;
  150. spin_lock_init(&police->tcf_lock);
  151. if (bind)
  152. police->tcf_bindcnt = 1;
  153. override:
  154. if (parm->rate.rate) {
  155. err = -ENOMEM;
  156. R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
  157. if (R_tab == NULL)
  158. goto failure;
  159. if (parm->peakrate.rate) {
  160. P_tab = qdisc_get_rtab(&parm->peakrate,
  161. tb[TCA_POLICE_PEAKRATE-1]);
  162. if (P_tab == NULL) {
  163. qdisc_put_rtab(R_tab);
  164. goto failure;
  165. }
  166. }
  167. }
  168. /* No failure allowed after this point */
  169. spin_lock_bh(&police->tcf_lock);
  170. if (R_tab != NULL) {
  171. qdisc_put_rtab(police->tcfp_R_tab);
  172. police->tcfp_R_tab = R_tab;
  173. }
  174. if (P_tab != NULL) {
  175. qdisc_put_rtab(police->tcfp_P_tab);
  176. police->tcfp_P_tab = P_tab;
  177. }
  178. if (tb[TCA_POLICE_RESULT-1])
  179. police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
  180. police->tcfp_toks = police->tcfp_burst = parm->burst;
  181. police->tcfp_mtu = parm->mtu;
  182. if (police->tcfp_mtu == 0) {
  183. police->tcfp_mtu = ~0;
  184. if (police->tcfp_R_tab)
  185. police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log;
  186. }
  187. if (police->tcfp_P_tab)
  188. police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
  189. police->tcf_action = parm->action;
  190. if (tb[TCA_POLICE_AVRATE-1])
  191. police->tcfp_ewma_rate =
  192. *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
  193. if (est)
  194. gen_replace_estimator(&police->tcf_bstats,
  195. &police->tcf_rate_est,
  196. &police->tcf_lock, est);
  197. spin_unlock_bh(&police->tcf_lock);
  198. if (ret != ACT_P_CREATED)
  199. return ret;
  200. police->tcfp_t_c = psched_get_time();
  201. police->tcf_index = parm->index ? parm->index :
  202. tcf_hash_new_index(&police_idx_gen, &police_hash_info);
  203. h = tcf_hash(police->tcf_index, POL_TAB_MASK);
  204. write_lock_bh(&police_lock);
  205. police->tcf_next = tcf_police_ht[h];
  206. tcf_police_ht[h] = &police->common;
  207. write_unlock_bh(&police_lock);
  208. a->priv = police;
  209. return ret;
  210. failure:
  211. if (ret == ACT_P_CREATED)
  212. kfree(police);
  213. return err;
  214. }
  215. static int tcf_act_police_cleanup(struct tc_action *a, int bind)
  216. {
  217. struct tcf_police *p = a->priv;
  218. int ret = 0;
  219. if (p != NULL) {
  220. if (bind)
  221. p->tcf_bindcnt--;
  222. p->tcf_refcnt--;
  223. if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
  224. tcf_police_destroy(p);
  225. ret = 1;
  226. }
  227. }
  228. return ret;
  229. }
  230. static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
  231. struct tcf_result *res)
  232. {
  233. struct tcf_police *police = a->priv;
  234. psched_time_t now;
  235. long toks;
  236. long ptoks = 0;
  237. spin_lock(&police->tcf_lock);
  238. police->tcf_bstats.bytes += skb->len;
  239. police->tcf_bstats.packets++;
  240. if (police->tcfp_ewma_rate &&
  241. police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
  242. police->tcf_qstats.overlimits++;
  243. spin_unlock(&police->tcf_lock);
  244. return police->tcf_action;
  245. }
  246. if (skb->len <= police->tcfp_mtu) {
  247. if (police->tcfp_R_tab == NULL) {
  248. spin_unlock(&police->tcf_lock);
  249. return police->tcfp_result;
  250. }
  251. now = psched_get_time();
  252. toks = psched_tdiff_bounded(now, police->tcfp_t_c,
  253. police->tcfp_burst);
  254. if (police->tcfp_P_tab) {
  255. ptoks = toks + police->tcfp_ptoks;
  256. if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
  257. ptoks = (long)L2T_P(police, police->tcfp_mtu);
  258. ptoks -= L2T_P(police, skb->len);
  259. }
  260. toks += police->tcfp_toks;
  261. if (toks > (long)police->tcfp_burst)
  262. toks = police->tcfp_burst;
  263. toks -= L2T(police, skb->len);
  264. if ((toks|ptoks) >= 0) {
  265. police->tcfp_t_c = now;
  266. police->tcfp_toks = toks;
  267. police->tcfp_ptoks = ptoks;
  268. spin_unlock(&police->tcf_lock);
  269. return police->tcfp_result;
  270. }
  271. }
  272. police->tcf_qstats.overlimits++;
  273. spin_unlock(&police->tcf_lock);
  274. return police->tcf_action;
  275. }
  276. static int
  277. tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  278. {
  279. unsigned char *b = skb_tail_pointer(skb);
  280. struct tcf_police *police = a->priv;
  281. struct tc_police opt;
  282. opt.index = police->tcf_index;
  283. opt.action = police->tcf_action;
  284. opt.mtu = police->tcfp_mtu;
  285. opt.burst = police->tcfp_burst;
  286. opt.refcnt = police->tcf_refcnt - ref;
  287. opt.bindcnt = police->tcf_bindcnt - bind;
  288. if (police->tcfp_R_tab)
  289. opt.rate = police->tcfp_R_tab->rate;
  290. else
  291. memset(&opt.rate, 0, sizeof(opt.rate));
  292. if (police->tcfp_P_tab)
  293. opt.peakrate = police->tcfp_P_tab->rate;
  294. else
  295. memset(&opt.peakrate, 0, sizeof(opt.peakrate));
  296. RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
  297. if (police->tcfp_result)
  298. RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
  299. &police->tcfp_result);
  300. if (police->tcfp_ewma_rate)
  301. RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
  302. return skb->len;
  303. rtattr_failure:
  304. nlmsg_trim(skb, b);
  305. return -1;
  306. }
  307. MODULE_AUTHOR("Alexey Kuznetsov");
  308. MODULE_DESCRIPTION("Policing actions");
  309. MODULE_LICENSE("GPL");
  310. static struct tc_action_ops act_police_ops = {
  311. .kind = "police",
  312. .hinfo = &police_hash_info,
  313. .type = TCA_ID_POLICE,
  314. .capab = TCA_CAP_NONE,
  315. .owner = THIS_MODULE,
  316. .act = tcf_act_police,
  317. .dump = tcf_act_police_dump,
  318. .cleanup = tcf_act_police_cleanup,
  319. .lookup = tcf_hash_search,
  320. .init = tcf_act_police_locate,
  321. .walk = tcf_act_police_walker
  322. };
  323. static int __init
  324. police_init_module(void)
  325. {
  326. return tcf_register_action(&act_police_ops);
  327. }
  328. static void __exit
  329. police_cleanup_module(void)
  330. {
  331. tcf_unregister_action(&act_police_ops);
  332. }
  333. module_init(police_init_module);
  334. module_exit(police_cleanup_module);