act_gact.c 5.4 KB

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