act_police.c 9.1 KB

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