PageRenderTime 95ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/linux-2.6.11.12-hmwk6/net/sched/police.c

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