act_police.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 <asm/uaccess.h>
  13. #include <asm/system.h>
  14. #include <linux/bitops.h>
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/socket.h>
  22. #include <linux/sockios.h>
  23. #include <linux/in.h>
  24. #include <linux/errno.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/module.h>
  29. #include <linux/rtnetlink.h>
  30. #include <linux/init.h>
  31. #include <net/sock.h>
  32. #include <net/act_api.h>
  33. #define L2T(p,L) ((p)->R_tab->data[(L)>>(p)->R_tab->rate.cell_log])
  34. #define L2T_P(p,L) ((p)->P_tab->data[(L)>>(p)->P_tab->rate.cell_log])
  35. #define PRIV(a) ((struct tcf_police *) (a)->priv)
  36. /* use generic hash table */
  37. #define MY_TAB_SIZE 16
  38. #define MY_TAB_MASK 15
  39. static u32 idx_gen;
  40. static struct tcf_police *tcf_police_ht[MY_TAB_SIZE];
  41. /* Policer hash table lock */
  42. static DEFINE_RWLOCK(police_lock);
  43. /* Each policer is serialized by its individual spinlock */
  44. static __inline__ unsigned tcf_police_hash(u32 index)
  45. {
  46. return index&0xF;
  47. }
  48. static __inline__ struct tcf_police * tcf_police_lookup(u32 index)
  49. {
  50. struct tcf_police *p;
  51. read_lock(&police_lock);
  52. for (p = tcf_police_ht[tcf_police_hash(index)]; p; p = p->next) {
  53. if (p->index == index)
  54. break;
  55. }
  56. read_unlock(&police_lock);
  57. return p;
  58. }
  59. #ifdef CONFIG_NET_CLS_ACT
  60. static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
  61. int type, struct tc_action *a)
  62. {
  63. struct tcf_police *p;
  64. int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
  65. struct rtattr *r;
  66. read_lock(&police_lock);
  67. s_i = cb->args[0];
  68. for (i = 0; i < MY_TAB_SIZE; i++) {
  69. p = tcf_police_ht[tcf_police_hash(i)];
  70. for (; p; p = p->next) {
  71. index++;
  72. if (index < s_i)
  73. continue;
  74. a->priv = p;
  75. a->order = index;
  76. r = (struct rtattr*) skb->tail;
  77. RTA_PUT(skb, a->order, 0, NULL);
  78. if (type == RTM_DELACTION)
  79. err = tcf_action_dump_1(skb, a, 0, 1);
  80. else
  81. err = tcf_action_dump_1(skb, a, 0, 0);
  82. if (err < 0) {
  83. index--;
  84. skb_trim(skb, (u8*)r - skb->data);
  85. goto done;
  86. }
  87. r->rta_len = skb->tail - (u8*)r;
  88. n_i++;
  89. }
  90. }
  91. done:
  92. read_unlock(&police_lock);
  93. if (n_i)
  94. cb->args[0] += n_i;
  95. return n_i;
  96. rtattr_failure:
  97. skb_trim(skb, (u8*)r - skb->data);
  98. goto done;
  99. }
  100. static inline int
  101. tcf_act_police_hash_search(struct tc_action *a, u32 index)
  102. {
  103. struct tcf_police *p = tcf_police_lookup(index);
  104. if (p != NULL) {
  105. a->priv = p;
  106. return 1;
  107. } else {
  108. return 0;
  109. }
  110. }
  111. #endif
  112. static inline u32 tcf_police_new_index(void)
  113. {
  114. do {
  115. if (++idx_gen == 0)
  116. idx_gen = 1;
  117. } while (tcf_police_lookup(idx_gen));
  118. return idx_gen;
  119. }
  120. void tcf_police_destroy(struct tcf_police *p)
  121. {
  122. unsigned h = tcf_police_hash(p->index);
  123. struct tcf_police **p1p;
  124. for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->next) {
  125. if (*p1p == p) {
  126. write_lock_bh(&police_lock);
  127. *p1p = p->next;
  128. write_unlock_bh(&police_lock);
  129. #ifdef CONFIG_NET_ESTIMATOR
  130. gen_kill_estimator(&p->bstats, &p->rate_est);
  131. #endif
  132. if (p->R_tab)
  133. qdisc_put_rtab(p->R_tab);
  134. if (p->P_tab)
  135. qdisc_put_rtab(p->P_tab);
  136. kfree(p);
  137. return;
  138. }
  139. }
  140. BUG_TRAP(0);
  141. }
  142. #ifdef CONFIG_NET_CLS_ACT
  143. static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
  144. struct tc_action *a, int ovr, int bind)
  145. {
  146. unsigned h;
  147. int ret = 0, err;
  148. struct rtattr *tb[TCA_POLICE_MAX];
  149. struct tc_police *parm;
  150. struct tcf_police *p;
  151. struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
  152. if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
  153. return -EINVAL;
  154. if (tb[TCA_POLICE_TBF-1] == NULL ||
  155. RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]) != sizeof(*parm))
  156. return -EINVAL;
  157. parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
  158. if (tb[TCA_POLICE_RESULT-1] != NULL &&
  159. RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
  160. return -EINVAL;
  161. if (tb[TCA_POLICE_RESULT-1] != NULL &&
  162. RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
  163. return -EINVAL;
  164. if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
  165. a->priv = p;
  166. if (bind) {
  167. p->bindcnt += 1;
  168. p->refcnt += 1;
  169. }
  170. if (ovr)
  171. goto override;
  172. return ret;
  173. }
  174. p = kzalloc(sizeof(*p), GFP_KERNEL);
  175. if (p == NULL)
  176. return -ENOMEM;
  177. ret = ACT_P_CREATED;
  178. p->refcnt = 1;
  179. spin_lock_init(&p->lock);
  180. p->stats_lock = &p->lock;
  181. if (bind)
  182. p->bindcnt = 1;
  183. override:
  184. if (parm->rate.rate) {
  185. err = -ENOMEM;
  186. R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
  187. if (R_tab == NULL)
  188. goto failure;
  189. if (parm->peakrate.rate) {
  190. P_tab = qdisc_get_rtab(&parm->peakrate,
  191. tb[TCA_POLICE_PEAKRATE-1]);
  192. if (p->P_tab == NULL) {
  193. qdisc_put_rtab(R_tab);
  194. goto failure;
  195. }
  196. }
  197. }
  198. /* No failure allowed after this point */
  199. spin_lock_bh(&p->lock);
  200. if (R_tab != NULL) {
  201. qdisc_put_rtab(p->R_tab);
  202. p->R_tab = R_tab;
  203. }
  204. if (P_tab != NULL) {
  205. qdisc_put_rtab(p->P_tab);
  206. p->P_tab = P_tab;
  207. }
  208. if (tb[TCA_POLICE_RESULT-1])
  209. p->result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
  210. p->toks = p->burst = parm->burst;
  211. p->mtu = parm->mtu;
  212. if (p->mtu == 0) {
  213. p->mtu = ~0;
  214. if (p->R_tab)
  215. p->mtu = 255<<p->R_tab->rate.cell_log;
  216. }
  217. if (p->P_tab)
  218. p->ptoks = L2T_P(p, p->mtu);
  219. p->action = parm->action;
  220. #ifdef CONFIG_NET_ESTIMATOR
  221. if (tb[TCA_POLICE_AVRATE-1])
  222. p->ewma_rate = *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
  223. if (est)
  224. gen_replace_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
  225. #endif
  226. spin_unlock_bh(&p->lock);
  227. if (ret != ACT_P_CREATED)
  228. return ret;
  229. PSCHED_GET_TIME(p->t_c);
  230. p->index = parm->index ? : tcf_police_new_index();
  231. h = tcf_police_hash(p->index);
  232. write_lock_bh(&police_lock);
  233. p->next = tcf_police_ht[h];
  234. tcf_police_ht[h] = p;
  235. write_unlock_bh(&police_lock);
  236. a->priv = p;
  237. return ret;
  238. failure:
  239. if (ret == ACT_P_CREATED)
  240. kfree(p);
  241. return err;
  242. }
  243. static int tcf_act_police_cleanup(struct tc_action *a, int bind)
  244. {
  245. struct tcf_police *p = PRIV(a);
  246. if (p != NULL)
  247. return tcf_police_release(p, bind);
  248. return 0;
  249. }
  250. static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
  251. struct tcf_result *res)
  252. {
  253. psched_time_t now;
  254. struct tcf_police *p = PRIV(a);
  255. long toks;
  256. long ptoks = 0;
  257. spin_lock(&p->lock);
  258. p->bstats.bytes += skb->len;
  259. p->bstats.packets++;
  260. #ifdef CONFIG_NET_ESTIMATOR
  261. if (p->ewma_rate && p->rate_est.bps >= p->ewma_rate) {
  262. p->qstats.overlimits++;
  263. spin_unlock(&p->lock);
  264. return p->action;
  265. }
  266. #endif
  267. if (skb->len <= p->mtu) {
  268. if (p->R_tab == NULL) {
  269. spin_unlock(&p->lock);
  270. return p->result;
  271. }
  272. PSCHED_GET_TIME(now);
  273. toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
  274. if (p->P_tab) {
  275. ptoks = toks + p->ptoks;
  276. if (ptoks > (long)L2T_P(p, p->mtu))
  277. ptoks = (long)L2T_P(p, p->mtu);
  278. ptoks -= L2T_P(p, skb->len);
  279. }
  280. toks += p->toks;
  281. if (toks > (long)p->burst)
  282. toks = p->burst;
  283. toks -= L2T(p, skb->len);
  284. if ((toks|ptoks) >= 0) {
  285. p->t_c = now;
  286. p->toks = toks;
  287. p->ptoks = ptoks;
  288. spin_unlock(&p->lock);
  289. return p->result;
  290. }
  291. }
  292. p->qstats.overlimits++;
  293. spin_unlock(&p->lock);
  294. return p->action;
  295. }
  296. static int
  297. tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  298. {
  299. unsigned char *b = skb->tail;
  300. struct tc_police opt;
  301. struct tcf_police *p = PRIV(a);
  302. opt.index = p->index;
  303. opt.action = p->action;
  304. opt.mtu = p->mtu;
  305. opt.burst = p->burst;
  306. opt.refcnt = p->refcnt - ref;
  307. opt.bindcnt = p->bindcnt - bind;
  308. if (p->R_tab)
  309. opt.rate = p->R_tab->rate;
  310. else
  311. memset(&opt.rate, 0, sizeof(opt.rate));
  312. if (p->P_tab)
  313. opt.peakrate = p->P_tab->rate;
  314. else
  315. memset(&opt.peakrate, 0, sizeof(opt.peakrate));
  316. RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
  317. if (p->result)
  318. RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
  319. #ifdef CONFIG_NET_ESTIMATOR
  320. if (p->ewma_rate)
  321. RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
  322. #endif
  323. return skb->len;
  324. rtattr_failure:
  325. skb_trim(skb, b - skb->data);
  326. return -1;
  327. }
  328. MODULE_AUTHOR("Alexey Kuznetsov");
  329. MODULE_DESCRIPTION("Policing actions");
  330. MODULE_LICENSE("GPL");
  331. static struct tc_action_ops act_police_ops = {
  332. .kind = "police",
  333. .type = TCA_ID_POLICE,
  334. .capab = TCA_CAP_NONE,
  335. .owner = THIS_MODULE,
  336. .act = tcf_act_police,
  337. .dump = tcf_act_police_dump,
  338. .cleanup = tcf_act_police_cleanup,
  339. .lookup = tcf_act_police_hash_search,
  340. .init = tcf_act_police_locate,
  341. .walk = tcf_act_police_walker
  342. };
  343. static int __init
  344. police_init_module(void)
  345. {
  346. return tcf_register_action(&act_police_ops);
  347. }
  348. static void __exit
  349. police_cleanup_module(void)
  350. {
  351. tcf_unregister_action(&act_police_ops);
  352. }
  353. module_init(police_init_module);
  354. module_exit(police_cleanup_module);
  355. #else /* CONFIG_NET_CLS_ACT */
  356. struct tcf_police * tcf_police_locate(struct rtattr *rta, struct rtattr *est)
  357. {
  358. unsigned h;
  359. struct tcf_police *p;
  360. struct rtattr *tb[TCA_POLICE_MAX];
  361. struct tc_police *parm;
  362. if (rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
  363. return NULL;
  364. if (tb[TCA_POLICE_TBF-1] == NULL ||
  365. RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]) != sizeof(*parm))
  366. return NULL;
  367. parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
  368. if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
  369. p->refcnt++;
  370. return p;
  371. }
  372. p = kzalloc(sizeof(*p), GFP_KERNEL);
  373. if (p == NULL)
  374. return NULL;
  375. p->refcnt = 1;
  376. spin_lock_init(&p->lock);
  377. p->stats_lock = &p->lock;
  378. if (parm->rate.rate) {
  379. p->R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
  380. if (p->R_tab == NULL)
  381. goto failure;
  382. if (parm->peakrate.rate) {
  383. p->P_tab = qdisc_get_rtab(&parm->peakrate,
  384. tb[TCA_POLICE_PEAKRATE-1]);
  385. if (p->P_tab == NULL)
  386. goto failure;
  387. }
  388. }
  389. if (tb[TCA_POLICE_RESULT-1]) {
  390. if (RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
  391. goto failure;
  392. p->result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
  393. }
  394. #ifdef CONFIG_NET_ESTIMATOR
  395. if (tb[TCA_POLICE_AVRATE-1]) {
  396. if (RTA_PAYLOAD(tb[TCA_POLICE_AVRATE-1]) != sizeof(u32))
  397. goto failure;
  398. p->ewma_rate = *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
  399. }
  400. #endif
  401. p->toks = p->burst = parm->burst;
  402. p->mtu = parm->mtu;
  403. if (p->mtu == 0) {
  404. p->mtu = ~0;
  405. if (p->R_tab)
  406. p->mtu = 255<<p->R_tab->rate.cell_log;
  407. }
  408. if (p->P_tab)
  409. p->ptoks = L2T_P(p, p->mtu);
  410. PSCHED_GET_TIME(p->t_c);
  411. p->index = parm->index ? : tcf_police_new_index();
  412. p->action = parm->action;
  413. #ifdef CONFIG_NET_ESTIMATOR
  414. if (est)
  415. gen_new_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
  416. #endif
  417. h = tcf_police_hash(p->index);
  418. write_lock_bh(&police_lock);
  419. p->next = tcf_police_ht[h];
  420. tcf_police_ht[h] = p;
  421. write_unlock_bh(&police_lock);
  422. return p;
  423. failure:
  424. if (p->R_tab)
  425. qdisc_put_rtab(p->R_tab);
  426. kfree(p);
  427. return NULL;
  428. }
  429. int tcf_police(struct sk_buff *skb, struct tcf_police *p)
  430. {
  431. psched_time_t now;
  432. long toks;
  433. long ptoks = 0;
  434. spin_lock(&p->lock);
  435. p->bstats.bytes += skb->len;
  436. p->bstats.packets++;
  437. #ifdef CONFIG_NET_ESTIMATOR
  438. if (p->ewma_rate && p->rate_est.bps >= p->ewma_rate) {
  439. p->qstats.overlimits++;
  440. spin_unlock(&p->lock);
  441. return p->action;
  442. }
  443. #endif
  444. if (skb->len <= p->mtu) {
  445. if (p->R_tab == NULL) {
  446. spin_unlock(&p->lock);
  447. return p->result;
  448. }
  449. PSCHED_GET_TIME(now);
  450. toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
  451. if (p->P_tab) {
  452. ptoks = toks + p->ptoks;
  453. if (ptoks > (long)L2T_P(p, p->mtu))
  454. ptoks = (long)L2T_P(p, p->mtu);
  455. ptoks -= L2T_P(p, skb->len);
  456. }
  457. toks += p->toks;
  458. if (toks > (long)p->burst)
  459. toks = p->burst;
  460. toks -= L2T(p, skb->len);
  461. if ((toks|ptoks) >= 0) {
  462. p->t_c = now;
  463. p->toks = toks;
  464. p->ptoks = ptoks;
  465. spin_unlock(&p->lock);
  466. return p->result;
  467. }
  468. }
  469. p->qstats.overlimits++;
  470. spin_unlock(&p->lock);
  471. return p->action;
  472. }
  473. EXPORT_SYMBOL(tcf_police);
  474. int tcf_police_dump(struct sk_buff *skb, struct tcf_police *p)
  475. {
  476. unsigned char *b = skb->tail;
  477. struct tc_police opt;
  478. opt.index = p->index;
  479. opt.action = p->action;
  480. opt.mtu = p->mtu;
  481. opt.burst = p->burst;
  482. if (p->R_tab)
  483. opt.rate = p->R_tab->rate;
  484. else
  485. memset(&opt.rate, 0, sizeof(opt.rate));
  486. if (p->P_tab)
  487. opt.peakrate = p->P_tab->rate;
  488. else
  489. memset(&opt.peakrate, 0, sizeof(opt.peakrate));
  490. RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
  491. if (p->result)
  492. RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
  493. #ifdef CONFIG_NET_ESTIMATOR
  494. if (p->ewma_rate)
  495. RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
  496. #endif
  497. return skb->len;
  498. rtattr_failure:
  499. skb_trim(skb, b - skb->data);
  500. return -1;
  501. }
  502. int tcf_police_dump_stats(struct sk_buff *skb, struct tcf_police *p)
  503. {
  504. struct gnet_dump d;
  505. if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
  506. TCA_XSTATS, p->stats_lock, &d) < 0)
  507. goto errout;
  508. if (gnet_stats_copy_basic(&d, &p->bstats) < 0 ||
  509. #ifdef CONFIG_NET_ESTIMATOR
  510. gnet_stats_copy_rate_est(&d, &p->rate_est) < 0 ||
  511. #endif
  512. gnet_stats_copy_queue(&d, &p->qstats) < 0)
  513. goto errout;
  514. if (gnet_stats_finish_copy(&d) < 0)
  515. goto errout;
  516. return 0;
  517. errout:
  518. return -1;
  519. }
  520. #endif /* CONFIG_NET_CLS_ACT */