act_gact.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * net/sched/gact.c Generic actions
  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. * copyright Jamal Hadi Salim (2002-4)
  10. *
  11. */
  12. #include <asm/uaccess.h>
  13. #include <asm/system.h>
  14. #include <linux/bitops.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/socket.h>
  21. #include <linux/sockios.h>
  22. #include <linux/in.h>
  23. #include <linux/errno.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/rtnetlink.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/proc_fs.h>
  31. #include <net/sock.h>
  32. #include <net/pkt_sched.h>
  33. #include <linux/tc_act/tc_gact.h>
  34. #include <net/tc_act/tc_gact.h>
  35. #define GACT_TAB_MASK 15
  36. static struct tcf_common *tcf_gact_ht[GACT_TAB_MASK + 1];
  37. static u32 gact_idx_gen;
  38. static DEFINE_RWLOCK(gact_lock);
  39. static struct tcf_hashinfo gact_hash_info = {
  40. .htab = tcf_gact_ht,
  41. .hmask = GACT_TAB_MASK,
  42. .lock = &gact_lock,
  43. };
  44. #ifdef CONFIG_GACT_PROB
  45. static int gact_net_rand(struct tcf_gact *gact)
  46. {
  47. if (net_random() % gact->tcfg_pval)
  48. return gact->tcf_action;
  49. return gact->tcfg_paction;
  50. }
  51. static int gact_determ(struct tcf_gact *gact)
  52. {
  53. if (gact->tcf_bstats.packets % gact->tcfg_pval)
  54. return gact->tcf_action;
  55. return gact->tcfg_paction;
  56. }
  57. typedef int (*g_rand)(struct tcf_gact *gact);
  58. static g_rand gact_rand[MAX_RAND]= { NULL, gact_net_rand, gact_determ };
  59. #endif /* CONFIG_GACT_PROB */
  60. static int tcf_gact_init(struct rtattr *rta, struct rtattr *est,
  61. struct tc_action *a, int ovr, int bind)
  62. {
  63. struct rtattr *tb[TCA_GACT_MAX];
  64. struct tc_gact *parm;
  65. struct tcf_gact *gact;
  66. struct tcf_common *pc;
  67. int ret = 0;
  68. if (rta == NULL || rtattr_parse_nested(tb, TCA_GACT_MAX, rta) < 0)
  69. return -EINVAL;
  70. if (tb[TCA_GACT_PARMS - 1] == NULL ||
  71. RTA_PAYLOAD(tb[TCA_GACT_PARMS - 1]) < sizeof(*parm))
  72. return -EINVAL;
  73. parm = RTA_DATA(tb[TCA_GACT_PARMS - 1]);
  74. if (tb[TCA_GACT_PROB-1] != NULL)
  75. #ifdef CONFIG_GACT_PROB
  76. if (RTA_PAYLOAD(tb[TCA_GACT_PROB-1]) < sizeof(struct tc_gact_p))
  77. return -EINVAL;
  78. #else
  79. return -EOPNOTSUPP;
  80. #endif
  81. pc = tcf_hash_check(parm->index, a, bind, &gact_hash_info);
  82. if (!pc) {
  83. pc = tcf_hash_create(parm->index, est, a, sizeof(*gact),
  84. bind, &gact_idx_gen, &gact_hash_info);
  85. if (unlikely(!pc))
  86. return -ENOMEM;
  87. ret = ACT_P_CREATED;
  88. } else {
  89. if (!ovr) {
  90. tcf_hash_release(pc, bind, &gact_hash_info);
  91. return -EEXIST;
  92. }
  93. }
  94. gact = to_gact(pc);
  95. spin_lock_bh(&gact->tcf_lock);
  96. gact->tcf_action = parm->action;
  97. #ifdef CONFIG_GACT_PROB
  98. if (tb[TCA_GACT_PROB-1] != NULL) {
  99. struct tc_gact_p *p_parm = RTA_DATA(tb[TCA_GACT_PROB-1]);
  100. gact->tcfg_paction = p_parm->paction;
  101. gact->tcfg_pval = p_parm->pval;
  102. gact->tcfg_ptype = p_parm->ptype;
  103. }
  104. #endif
  105. spin_unlock_bh(&gact->tcf_lock);
  106. if (ret == ACT_P_CREATED)
  107. tcf_hash_insert(pc, &gact_hash_info);
  108. return ret;
  109. }
  110. static int tcf_gact_cleanup(struct tc_action *a, int bind)
  111. {
  112. struct tcf_gact *gact = a->priv;
  113. if (gact)
  114. return tcf_hash_release(&gact->common, bind, &gact_hash_info);
  115. return 0;
  116. }
  117. static int tcf_gact(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res)
  118. {
  119. struct tcf_gact *gact = a->priv;
  120. int action = TC_ACT_SHOT;
  121. spin_lock(&gact->tcf_lock);
  122. #ifdef CONFIG_GACT_PROB
  123. if (gact->tcfg_ptype && gact_rand[gact->tcfg_ptype] != NULL)
  124. action = gact_rand[gact->tcfg_ptype](gact);
  125. else
  126. action = gact->tcf_action;
  127. #else
  128. action = gact->tcf_action;
  129. #endif
  130. gact->tcf_bstats.bytes += skb->len;
  131. gact->tcf_bstats.packets++;
  132. if (action == TC_ACT_SHOT)
  133. gact->tcf_qstats.drops++;
  134. gact->tcf_tm.lastuse = jiffies;
  135. spin_unlock(&gact->tcf_lock);
  136. return action;
  137. }
  138. static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  139. {
  140. unsigned char *b = skb->tail;
  141. struct tc_gact opt;
  142. struct tcf_gact *gact = a->priv;
  143. struct tcf_t t;
  144. opt.index = gact->tcf_index;
  145. opt.refcnt = gact->tcf_refcnt - ref;
  146. opt.bindcnt = gact->tcf_bindcnt - bind;
  147. opt.action = gact->tcf_action;
  148. RTA_PUT(skb, TCA_GACT_PARMS, sizeof(opt), &opt);
  149. #ifdef CONFIG_GACT_PROB
  150. if (gact->tcfg_ptype) {
  151. struct tc_gact_p p_opt;
  152. p_opt.paction = gact->tcfg_paction;
  153. p_opt.pval = gact->tcfg_pval;
  154. p_opt.ptype = gact->tcfg_ptype;
  155. RTA_PUT(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt);
  156. }
  157. #endif
  158. t.install = jiffies_to_clock_t(jiffies - gact->tcf_tm.install);
  159. t.lastuse = jiffies_to_clock_t(jiffies - gact->tcf_tm.lastuse);
  160. t.expires = jiffies_to_clock_t(gact->tcf_tm.expires);
  161. RTA_PUT(skb, TCA_GACT_TM, sizeof(t), &t);
  162. return skb->len;
  163. rtattr_failure:
  164. skb_trim(skb, b - skb->data);
  165. return -1;
  166. }
  167. static struct tc_action_ops act_gact_ops = {
  168. .kind = "gact",
  169. .hinfo = &gact_hash_info,
  170. .type = TCA_ACT_GACT,
  171. .capab = TCA_CAP_NONE,
  172. .owner = THIS_MODULE,
  173. .act = tcf_gact,
  174. .dump = tcf_gact_dump,
  175. .cleanup = tcf_gact_cleanup,
  176. .lookup = tcf_hash_search,
  177. .init = tcf_gact_init,
  178. .walk = tcf_generic_walker
  179. };
  180. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  181. MODULE_DESCRIPTION("Generic Classifier actions");
  182. MODULE_LICENSE("GPL");
  183. static int __init gact_init_module(void)
  184. {
  185. #ifdef CONFIG_GACT_PROB
  186. printk("GACT probability on\n");
  187. #else
  188. printk("GACT probability NOT on\n");
  189. #endif
  190. return tcf_register_action(&act_gact_ops);
  191. }
  192. static void __exit gact_cleanup_module(void)
  193. {
  194. tcf_unregister_action(&act_gact_ops);
  195. }
  196. module_init(gact_init_module);
  197. module_exit(gact_cleanup_module);