act_gact.c 5.5 KB

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