act_police.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. #ifdef CONFIG_NET_CLS_ACT
  47. static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
  48. int type, struct tc_action *a)
  49. {
  50. struct tcf_common *p;
  51. int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
  52. struct rtattr *r;
  53. read_lock(&police_lock);
  54. s_i = cb->args[0];
  55. for (i = 0; i < (POL_TAB_MASK + 1); i++) {
  56. p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)];
  57. for (; p; p = p->tcfc_next) {
  58. index++;
  59. if (index < s_i)
  60. continue;
  61. a->priv = p;
  62. a->order = index;
  63. r = (struct rtattr *)skb_tail_pointer(skb);
  64. RTA_PUT(skb, a->order, 0, NULL);
  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. nlmsg_trim(skb, r);
  72. goto done;
  73. }
  74. r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
  75. n_i++;
  76. }
  77. }
  78. done:
  79. read_unlock(&police_lock);
  80. if (n_i)
  81. cb->args[0] += n_i;
  82. return n_i;
  83. rtattr_failure:
  84. nlmsg_trim(skb, r);
  85. goto done;
  86. }
  87. #endif
  88. void tcf_police_destroy(struct tcf_police *p)
  89. {
  90. unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK);
  91. struct tcf_common **p1p;
  92. for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
  93. if (*p1p == &p->common) {
  94. write_lock_bh(&police_lock);
  95. *p1p = p->tcf_next;
  96. write_unlock_bh(&police_lock);
  97. gen_kill_estimator(&p->tcf_bstats,
  98. &p->tcf_rate_est);
  99. if (p->tcfp_R_tab)
  100. qdisc_put_rtab(p->tcfp_R_tab);
  101. if (p->tcfp_P_tab)
  102. qdisc_put_rtab(p->tcfp_P_tab);
  103. kfree(p);
  104. return;
  105. }
  106. }
  107. BUG_TRAP(0);
  108. }
  109. #ifdef CONFIG_NET_CLS_ACT
  110. static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
  111. struct tc_action *a, int ovr, int bind)
  112. {
  113. unsigned h;
  114. int ret = 0, err;
  115. struct rtattr *tb[TCA_POLICE_MAX];
  116. struct tc_police *parm;
  117. struct tcf_police *police;
  118. struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
  119. int size;
  120. if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
  121. return -EINVAL;
  122. if (tb[TCA_POLICE_TBF-1] == NULL)
  123. return -EINVAL;
  124. size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]);
  125. if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
  126. return -EINVAL;
  127. parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
  128. if (tb[TCA_POLICE_RESULT-1] != NULL &&
  129. RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
  130. return -EINVAL;
  131. if (tb[TCA_POLICE_RESULT-1] != NULL &&
  132. RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
  133. return -EINVAL;
  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-1]);
  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-1]);
  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-1])
  183. police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
  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-1])
  195. police->tcfp_ewma_rate =
  196. *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
  197. if (est)
  198. gen_replace_estimator(&police->tcf_bstats,
  199. &police->tcf_rate_est,
  200. &police->tcf_lock, est);
  201. spin_unlock_bh(&police->tcf_lock);
  202. if (ret != ACT_P_CREATED)
  203. return ret;
  204. police->tcfp_t_c = psched_get_time();
  205. police->tcf_index = parm->index ? parm->index :
  206. tcf_hash_new_index(&police_idx_gen, &police_hash_info);
  207. h = tcf_hash(police->tcf_index, POL_TAB_MASK);
  208. write_lock_bh(&police_lock);
  209. police->tcf_next = tcf_police_ht[h];
  210. tcf_police_ht[h] = &police->common;
  211. write_unlock_bh(&police_lock);
  212. a->priv = police;
  213. return ret;
  214. failure:
  215. if (ret == ACT_P_CREATED)
  216. kfree(police);
  217. return err;
  218. }
  219. static int tcf_act_police_cleanup(struct tc_action *a, int bind)
  220. {
  221. struct tcf_police *p = a->priv;
  222. if (p != NULL)
  223. return tcf_police_release(p, bind);
  224. return 0;
  225. }
  226. static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
  227. struct tcf_result *res)
  228. {
  229. struct tcf_police *police = a->priv;
  230. psched_time_t now;
  231. long toks;
  232. long ptoks = 0;
  233. spin_lock(&police->tcf_lock);
  234. police->tcf_bstats.bytes += skb->len;
  235. police->tcf_bstats.packets++;
  236. if (police->tcfp_ewma_rate &&
  237. police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
  238. police->tcf_qstats.overlimits++;
  239. spin_unlock(&police->tcf_lock);
  240. return police->tcf_action;
  241. }
  242. if (skb->len <= police->tcfp_mtu) {
  243. if (police->tcfp_R_tab == NULL) {
  244. spin_unlock(&police->tcf_lock);
  245. return police->tcfp_result;
  246. }
  247. now = psched_get_time();
  248. toks = psched_tdiff_bounded(now, police->tcfp_t_c,
  249. police->tcfp_burst);
  250. if (police->tcfp_P_tab) {
  251. ptoks = toks + police->tcfp_ptoks;
  252. if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
  253. ptoks = (long)L2T_P(police, police->tcfp_mtu);
  254. ptoks -= L2T_P(police, skb->len);
  255. }
  256. toks += police->tcfp_toks;
  257. if (toks > (long)police->tcfp_burst)
  258. toks = police->tcfp_burst;
  259. toks -= L2T(police, skb->len);
  260. if ((toks|ptoks) >= 0) {
  261. police->tcfp_t_c = now;
  262. police->tcfp_toks = toks;
  263. police->tcfp_ptoks = ptoks;
  264. spin_unlock(&police->tcf_lock);
  265. return police->tcfp_result;
  266. }
  267. }
  268. police->tcf_qstats.overlimits++;
  269. spin_unlock(&police->tcf_lock);
  270. return police->tcf_action;
  271. }
  272. static int
  273. tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  274. {
  275. unsigned char *b = skb_tail_pointer(skb);
  276. struct tcf_police *police = a->priv;
  277. struct tc_police opt;
  278. opt.index = police->tcf_index;
  279. opt.action = police->tcf_action;
  280. opt.mtu = police->tcfp_mtu;
  281. opt.burst = police->tcfp_burst;
  282. opt.refcnt = police->tcf_refcnt - ref;
  283. opt.bindcnt = police->tcf_bindcnt - bind;
  284. if (police->tcfp_R_tab)
  285. opt.rate = police->tcfp_R_tab->rate;
  286. else
  287. memset(&opt.rate, 0, sizeof(opt.rate));
  288. if (police->tcfp_P_tab)
  289. opt.peakrate = police->tcfp_P_tab->rate;
  290. else
  291. memset(&opt.peakrate, 0, sizeof(opt.peakrate));
  292. RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
  293. if (police->tcfp_result)
  294. RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
  295. &police->tcfp_result);
  296. if (police->tcfp_ewma_rate)
  297. RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
  298. return skb->len;
  299. rtattr_failure:
  300. nlmsg_trim(skb, b);
  301. return -1;
  302. }
  303. MODULE_AUTHOR("Alexey Kuznetsov");
  304. MODULE_DESCRIPTION("Policing actions");
  305. MODULE_LICENSE("GPL");
  306. static struct tc_action_ops act_police_ops = {
  307. .kind = "police",
  308. .hinfo = &police_hash_info,
  309. .type = TCA_ID_POLICE,
  310. .capab = TCA_CAP_NONE,
  311. .owner = THIS_MODULE,
  312. .act = tcf_act_police,
  313. .dump = tcf_act_police_dump,
  314. .cleanup = tcf_act_police_cleanup,
  315. .lookup = tcf_hash_search,
  316. .init = tcf_act_police_locate,
  317. .walk = tcf_act_police_walker
  318. };
  319. static int __init
  320. police_init_module(void)
  321. {
  322. return tcf_register_action(&act_police_ops);
  323. }
  324. static void __exit
  325. police_cleanup_module(void)
  326. {
  327. tcf_unregister_action(&act_police_ops);
  328. }
  329. module_init(police_init_module);
  330. module_exit(police_cleanup_module);
  331. #else /* CONFIG_NET_CLS_ACT */
  332. static struct tcf_common *tcf_police_lookup(u32 index)
  333. {
  334. struct tcf_hashinfo *hinfo = &police_hash_info;
  335. struct tcf_common *p;
  336. read_lock(hinfo->lock);
  337. for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
  338. p = p->tcfc_next) {
  339. if (p->tcfc_index == index)
  340. break;
  341. }
  342. read_unlock(hinfo->lock);
  343. return p;
  344. }
  345. static u32 tcf_police_new_index(void)
  346. {
  347. u32 *idx_gen = &police_idx_gen;
  348. u32 val = *idx_gen;
  349. do {
  350. if (++val == 0)
  351. val = 1;
  352. } while (tcf_police_lookup(val));
  353. return (*idx_gen = val);
  354. }
  355. struct tcf_police *tcf_police_locate(struct rtattr *rta, struct rtattr *est)
  356. {
  357. unsigned int h;
  358. struct tcf_police *police;
  359. struct rtattr *tb[TCA_POLICE_MAX];
  360. struct tc_police *parm;
  361. int size;
  362. if (rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
  363. return NULL;
  364. if (tb[TCA_POLICE_TBF-1] == NULL)
  365. return NULL;
  366. size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]);
  367. if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
  368. return NULL;
  369. parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
  370. if (parm->index) {
  371. struct tcf_common *pc;
  372. pc = tcf_police_lookup(parm->index);
  373. if (pc) {
  374. police = to_police(pc);
  375. police->tcf_refcnt++;
  376. return police;
  377. }
  378. }
  379. police = kzalloc(sizeof(*police), GFP_KERNEL);
  380. if (unlikely(!police))
  381. return NULL;
  382. police->tcf_refcnt = 1;
  383. spin_lock_init(&police->tcf_lock);
  384. if (parm->rate.rate) {
  385. police->tcfp_R_tab =
  386. qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
  387. if (police->tcfp_R_tab == NULL)
  388. goto failure;
  389. if (parm->peakrate.rate) {
  390. police->tcfp_P_tab =
  391. qdisc_get_rtab(&parm->peakrate,
  392. tb[TCA_POLICE_PEAKRATE-1]);
  393. if (police->tcfp_P_tab == NULL)
  394. goto failure;
  395. }
  396. }
  397. if (tb[TCA_POLICE_RESULT-1]) {
  398. if (RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
  399. goto failure;
  400. police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
  401. }
  402. if (tb[TCA_POLICE_AVRATE-1]) {
  403. if (RTA_PAYLOAD(tb[TCA_POLICE_AVRATE-1]) != sizeof(u32))
  404. goto failure;
  405. police->tcfp_ewma_rate =
  406. *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
  407. }
  408. police->tcfp_toks = police->tcfp_burst = parm->burst;
  409. police->tcfp_mtu = parm->mtu;
  410. if (police->tcfp_mtu == 0) {
  411. police->tcfp_mtu = ~0;
  412. if (police->tcfp_R_tab)
  413. police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log;
  414. }
  415. if (police->tcfp_P_tab)
  416. police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
  417. police->tcfp_t_c = psched_get_time();
  418. police->tcf_index = parm->index ? parm->index :
  419. tcf_police_new_index();
  420. police->tcf_action = parm->action;
  421. if (est)
  422. gen_new_estimator(&police->tcf_bstats, &police->tcf_rate_est,
  423. &police->tcf_lock, est);
  424. h = tcf_hash(police->tcf_index, POL_TAB_MASK);
  425. write_lock_bh(&police_lock);
  426. police->tcf_next = tcf_police_ht[h];
  427. tcf_police_ht[h] = &police->common;
  428. write_unlock_bh(&police_lock);
  429. return police;
  430. failure:
  431. if (police->tcfp_R_tab)
  432. qdisc_put_rtab(police->tcfp_R_tab);
  433. kfree(police);
  434. return NULL;
  435. }
  436. int tcf_police(struct sk_buff *skb, struct tcf_police *police)
  437. {
  438. psched_time_t now;
  439. long toks;
  440. long ptoks = 0;
  441. spin_lock(&police->tcf_lock);
  442. police->tcf_bstats.bytes += skb->len;
  443. police->tcf_bstats.packets++;
  444. if (police->tcfp_ewma_rate &&
  445. police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
  446. police->tcf_qstats.overlimits++;
  447. spin_unlock(&police->tcf_lock);
  448. return police->tcf_action;
  449. }
  450. if (skb->len <= police->tcfp_mtu) {
  451. if (police->tcfp_R_tab == NULL) {
  452. spin_unlock(&police->tcf_lock);
  453. return police->tcfp_result;
  454. }
  455. now = psched_get_time();
  456. toks = psched_tdiff_bounded(now, police->tcfp_t_c,
  457. police->tcfp_burst);
  458. if (police->tcfp_P_tab) {
  459. ptoks = toks + police->tcfp_ptoks;
  460. if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
  461. ptoks = (long)L2T_P(police, police->tcfp_mtu);
  462. ptoks -= L2T_P(police, skb->len);
  463. }
  464. toks += police->tcfp_toks;
  465. if (toks > (long)police->tcfp_burst)
  466. toks = police->tcfp_burst;
  467. toks -= L2T(police, skb->len);
  468. if ((toks|ptoks) >= 0) {
  469. police->tcfp_t_c = now;
  470. police->tcfp_toks = toks;
  471. police->tcfp_ptoks = ptoks;
  472. spin_unlock(&police->tcf_lock);
  473. return police->tcfp_result;
  474. }
  475. }
  476. police->tcf_qstats.overlimits++;
  477. spin_unlock(&police->tcf_lock);
  478. return police->tcf_action;
  479. }
  480. EXPORT_SYMBOL(tcf_police);
  481. int tcf_police_dump(struct sk_buff *skb, struct tcf_police *police)
  482. {
  483. unsigned char *b = skb_tail_pointer(skb);
  484. struct tc_police opt;
  485. opt.index = police->tcf_index;
  486. opt.action = police->tcf_action;
  487. opt.mtu = police->tcfp_mtu;
  488. opt.burst = police->tcfp_burst;
  489. if (police->tcfp_R_tab)
  490. opt.rate = police->tcfp_R_tab->rate;
  491. else
  492. memset(&opt.rate, 0, sizeof(opt.rate));
  493. if (police->tcfp_P_tab)
  494. opt.peakrate = police->tcfp_P_tab->rate;
  495. else
  496. memset(&opt.peakrate, 0, sizeof(opt.peakrate));
  497. RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
  498. if (police->tcfp_result)
  499. RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
  500. &police->tcfp_result);
  501. if (police->tcfp_ewma_rate)
  502. RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
  503. return skb->len;
  504. rtattr_failure:
  505. nlmsg_trim(skb, b);
  506. return -1;
  507. }
  508. int tcf_police_dump_stats(struct sk_buff *skb, struct tcf_police *police)
  509. {
  510. struct gnet_dump d;
  511. if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
  512. TCA_XSTATS, &police->tcf_lock,
  513. &d) < 0)
  514. goto errout;
  515. if (gnet_stats_copy_basic(&d, &police->tcf_bstats) < 0 ||
  516. gnet_stats_copy_rate_est(&d, &police->tcf_rate_est) < 0 ||
  517. gnet_stats_copy_queue(&d, &police->tcf_qstats) < 0)
  518. goto errout;
  519. if (gnet_stats_finish_copy(&d) < 0)
  520. goto errout;
  521. return 0;
  522. errout:
  523. return -1;
  524. }
  525. #endif /* CONFIG_NET_CLS_ACT */