act_gact.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * net/sched/act_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. #ifdef CONFIG_GACT_PROB
  26. static int gact_net_rand(struct tcf_gact *gact)
  27. {
  28. smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
  29. if (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. u32 pack = atomic_inc_return(&gact->packets);
  36. smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
  37. if (pack % gact->tcfg_pval)
  38. return gact->tcf_action;
  39. return gact->tcfg_paction;
  40. }
  41. typedef int (*g_rand)(struct tcf_gact *gact);
  42. static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
  43. #endif /* CONFIG_GACT_PROB */
  44. static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
  45. [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) },
  46. [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) },
  47. };
  48. static int tcf_gact_init(struct net *net, struct nlattr *nla,
  49. struct nlattr *est, struct tc_action *a,
  50. int ovr, int bind)
  51. {
  52. struct nlattr *tb[TCA_GACT_MAX + 1];
  53. struct tc_gact *parm;
  54. struct tcf_gact *gact;
  55. int ret = 0;
  56. int err;
  57. #ifdef CONFIG_GACT_PROB
  58. struct tc_gact_p *p_parm = NULL;
  59. #endif
  60. if (nla == NULL)
  61. return -EINVAL;
  62. err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy);
  63. if (err < 0)
  64. return err;
  65. if (tb[TCA_GACT_PARMS] == NULL)
  66. return -EINVAL;
  67. parm = nla_data(tb[TCA_GACT_PARMS]);
  68. #ifndef CONFIG_GACT_PROB
  69. if (tb[TCA_GACT_PROB] != NULL)
  70. return -EOPNOTSUPP;
  71. #else
  72. if (tb[TCA_GACT_PROB]) {
  73. p_parm = nla_data(tb[TCA_GACT_PROB]);
  74. if (p_parm->ptype >= MAX_RAND)
  75. return -EINVAL;
  76. }
  77. #endif
  78. if (!tcf_hash_check(parm->index, a, bind)) {
  79. ret = tcf_hash_create(parm->index, est, a, sizeof(*gact),
  80. bind, true);
  81. if (ret)
  82. return ret;
  83. ret = ACT_P_CREATED;
  84. } else {
  85. if (bind)/* dont override defaults */
  86. return 0;
  87. tcf_hash_release(a, bind);
  88. if (!ovr)
  89. return -EEXIST;
  90. }
  91. gact = to_gact(a);
  92. ASSERT_RTNL();
  93. gact->tcf_action = parm->action;
  94. #ifdef CONFIG_GACT_PROB
  95. if (p_parm) {
  96. gact->tcfg_paction = p_parm->paction;
  97. gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
  98. /* Make sure tcfg_pval is written before tcfg_ptype
  99. * coupled with smp_rmb() in gact_net_rand() & gact_determ()
  100. */
  101. smp_wmb();
  102. gact->tcfg_ptype = p_parm->ptype;
  103. }
  104. #endif
  105. if (ret == ACT_P_CREATED)
  106. tcf_hash_insert(a);
  107. return ret;
  108. }
  109. static int tcf_gact(struct sk_buff *skb, const struct tc_action *a,
  110. struct tcf_result *res)
  111. {
  112. struct tcf_gact *gact = a->priv;
  113. int action = READ_ONCE(gact->tcf_action);
  114. #ifdef CONFIG_GACT_PROB
  115. {
  116. u32 ptype = READ_ONCE(gact->tcfg_ptype);
  117. if (ptype)
  118. action = gact_rand[ptype](gact);
  119. }
  120. #endif
  121. bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
  122. if (action == TC_ACT_SHOT)
  123. qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
  124. tcf_lastuse_update(&gact->tcf_tm);
  125. return action;
  126. }
  127. static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  128. {
  129. unsigned char *b = skb_tail_pointer(skb);
  130. struct tcf_gact *gact = a->priv;
  131. struct tc_gact opt = {
  132. .index = gact->tcf_index,
  133. .refcnt = gact->tcf_refcnt - ref,
  134. .bindcnt = gact->tcf_bindcnt - bind,
  135. .action = gact->tcf_action,
  136. };
  137. struct tcf_t t;
  138. if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
  139. goto nla_put_failure;
  140. #ifdef CONFIG_GACT_PROB
  141. if (gact->tcfg_ptype) {
  142. struct tc_gact_p p_opt = {
  143. .paction = gact->tcfg_paction,
  144. .pval = gact->tcfg_pval,
  145. .ptype = gact->tcfg_ptype,
  146. };
  147. if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
  148. goto nla_put_failure;
  149. }
  150. #endif
  151. t.install = jiffies_to_clock_t(jiffies - gact->tcf_tm.install);
  152. t.lastuse = jiffies_to_clock_t(jiffies - gact->tcf_tm.lastuse);
  153. t.expires = jiffies_to_clock_t(gact->tcf_tm.expires);
  154. if (nla_put(skb, TCA_GACT_TM, sizeof(t), &t))
  155. goto nla_put_failure;
  156. return skb->len;
  157. nla_put_failure:
  158. nlmsg_trim(skb, b);
  159. return -1;
  160. }
  161. static struct tc_action_ops act_gact_ops = {
  162. .kind = "gact",
  163. .type = TCA_ACT_GACT,
  164. .owner = THIS_MODULE,
  165. .act = tcf_gact,
  166. .dump = tcf_gact_dump,
  167. .init = tcf_gact_init,
  168. };
  169. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  170. MODULE_DESCRIPTION("Generic Classifier actions");
  171. MODULE_LICENSE("GPL");
  172. static int __init gact_init_module(void)
  173. {
  174. #ifdef CONFIG_GACT_PROB
  175. pr_info("GACT probability on\n");
  176. #else
  177. pr_info("GACT probability NOT on\n");
  178. #endif
  179. return tcf_register_action(&act_gact_ops, GACT_TAB_MASK);
  180. }
  181. static void __exit gact_cleanup_module(void)
  182. {
  183. tcf_unregister_action(&act_gact_ops);
  184. }
  185. module_init(gact_init_module);
  186. module_exit(gact_cleanup_module);