act_police.c 9.2 KB

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