act_gact.c 5.6 KB

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