act_gact.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. static int gact_net_id;
  26. static struct tc_action_ops act_gact_ops;
  27. #ifdef CONFIG_GACT_PROB
  28. static int gact_net_rand(struct tcf_gact *gact)
  29. {
  30. smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
  31. if (prandom_u32() % gact->tcfg_pval)
  32. return gact->tcf_action;
  33. return gact->tcfg_paction;
  34. }
  35. static int gact_determ(struct tcf_gact *gact)
  36. {
  37. u32 pack = atomic_inc_return(&gact->packets);
  38. smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
  39. if (pack % gact->tcfg_pval)
  40. return gact->tcf_action;
  41. return gact->tcfg_paction;
  42. }
  43. typedef int (*g_rand)(struct tcf_gact *gact);
  44. static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
  45. #endif /* CONFIG_GACT_PROB */
  46. static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
  47. [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) },
  48. [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) },
  49. };
  50. static int tcf_gact_init(struct net *net, struct nlattr *nla,
  51. struct nlattr *est, struct tc_action **a,
  52. int ovr, int bind)
  53. {
  54. struct tc_action_net *tn = net_generic(net, gact_net_id);
  55. struct nlattr *tb[TCA_GACT_MAX + 1];
  56. struct tc_gact *parm;
  57. struct tcf_gact *gact;
  58. int ret = 0;
  59. int err;
  60. #ifdef CONFIG_GACT_PROB
  61. struct tc_gact_p *p_parm = NULL;
  62. #endif
  63. if (nla == NULL)
  64. return -EINVAL;
  65. err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy);
  66. if (err < 0)
  67. return err;
  68. if (tb[TCA_GACT_PARMS] == NULL)
  69. return -EINVAL;
  70. parm = nla_data(tb[TCA_GACT_PARMS]);
  71. #ifndef CONFIG_GACT_PROB
  72. if (tb[TCA_GACT_PROB] != NULL)
  73. return -EOPNOTSUPP;
  74. #else
  75. if (tb[TCA_GACT_PROB]) {
  76. p_parm = nla_data(tb[TCA_GACT_PROB]);
  77. if (p_parm->ptype >= MAX_RAND)
  78. return -EINVAL;
  79. }
  80. #endif
  81. if (!tcf_hash_check(tn, parm->index, a, bind)) {
  82. ret = tcf_hash_create(tn, parm->index, est, a,
  83. &act_gact_ops, bind, true);
  84. if (ret)
  85. return ret;
  86. ret = ACT_P_CREATED;
  87. } else {
  88. if (bind)/* dont override defaults */
  89. return 0;
  90. tcf_hash_release(*a, bind);
  91. if (!ovr)
  92. return -EEXIST;
  93. }
  94. gact = to_gact(*a);
  95. ASSERT_RTNL();
  96. gact->tcf_action = parm->action;
  97. #ifdef CONFIG_GACT_PROB
  98. if (p_parm) {
  99. gact->tcfg_paction = p_parm->paction;
  100. gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
  101. /* Make sure tcfg_pval is written before tcfg_ptype
  102. * coupled with smp_rmb() in gact_net_rand() & gact_determ()
  103. */
  104. smp_wmb();
  105. gact->tcfg_ptype = p_parm->ptype;
  106. }
  107. #endif
  108. if (ret == ACT_P_CREATED)
  109. tcf_hash_insert(tn, *a);
  110. return ret;
  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 = to_gact(a);
  116. int action = READ_ONCE(gact->tcf_action);
  117. #ifdef CONFIG_GACT_PROB
  118. {
  119. u32 ptype = READ_ONCE(gact->tcfg_ptype);
  120. if (ptype)
  121. action = gact_rand[ptype](gact);
  122. }
  123. #endif
  124. bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
  125. if (action == TC_ACT_SHOT)
  126. qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
  127. tcf_lastuse_update(&gact->tcf_tm);
  128. return action;
  129. }
  130. static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
  131. u64 lastuse)
  132. {
  133. struct tcf_gact *gact = to_gact(a);
  134. int action = READ_ONCE(gact->tcf_action);
  135. struct tcf_t *tm = &gact->tcf_tm;
  136. _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes,
  137. packets);
  138. if (action == TC_ACT_SHOT)
  139. this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
  140. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  141. }
  142. static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
  143. int bind, int ref)
  144. {
  145. unsigned char *b = skb_tail_pointer(skb);
  146. struct tcf_gact *gact = to_gact(a);
  147. struct tc_gact opt = {
  148. .index = gact->tcf_index,
  149. .refcnt = gact->tcf_refcnt - ref,
  150. .bindcnt = gact->tcf_bindcnt - bind,
  151. .action = gact->tcf_action,
  152. };
  153. struct tcf_t t;
  154. if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
  155. goto nla_put_failure;
  156. #ifdef CONFIG_GACT_PROB
  157. if (gact->tcfg_ptype) {
  158. struct tc_gact_p p_opt = {
  159. .paction = gact->tcfg_paction,
  160. .pval = gact->tcfg_pval,
  161. .ptype = gact->tcfg_ptype,
  162. };
  163. if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
  164. goto nla_put_failure;
  165. }
  166. #endif
  167. tcf_tm_dump(&t, &gact->tcf_tm);
  168. if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
  169. goto nla_put_failure;
  170. return skb->len;
  171. nla_put_failure:
  172. nlmsg_trim(skb, b);
  173. return -1;
  174. }
  175. static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
  176. struct netlink_callback *cb, int type,
  177. const struct tc_action_ops *ops)
  178. {
  179. struct tc_action_net *tn = net_generic(net, gact_net_id);
  180. return tcf_generic_walker(tn, skb, cb, type, ops);
  181. }
  182. static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
  183. {
  184. struct tc_action_net *tn = net_generic(net, gact_net_id);
  185. return tcf_hash_search(tn, a, index);
  186. }
  187. static struct tc_action_ops act_gact_ops = {
  188. .kind = "gact",
  189. .type = TCA_ACT_GACT,
  190. .owner = THIS_MODULE,
  191. .act = tcf_gact,
  192. .stats_update = tcf_gact_stats_update,
  193. .dump = tcf_gact_dump,
  194. .init = tcf_gact_init,
  195. .walk = tcf_gact_walker,
  196. .lookup = tcf_gact_search,
  197. .size = sizeof(struct tcf_gact),
  198. };
  199. static __net_init int gact_init_net(struct net *net)
  200. {
  201. struct tc_action_net *tn = net_generic(net, gact_net_id);
  202. return tc_action_net_init(tn, &act_gact_ops, GACT_TAB_MASK);
  203. }
  204. static void __net_exit gact_exit_net(struct net *net)
  205. {
  206. struct tc_action_net *tn = net_generic(net, gact_net_id);
  207. tc_action_net_exit(tn);
  208. }
  209. static struct pernet_operations gact_net_ops = {
  210. .init = gact_init_net,
  211. .exit = gact_exit_net,
  212. .id = &gact_net_id,
  213. .size = sizeof(struct tc_action_net),
  214. };
  215. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  216. MODULE_DESCRIPTION("Generic Classifier actions");
  217. MODULE_LICENSE("GPL");
  218. static int __init gact_init_module(void)
  219. {
  220. #ifdef CONFIG_GACT_PROB
  221. pr_info("GACT probability on\n");
  222. #else
  223. pr_info("GACT probability NOT on\n");
  224. #endif
  225. return tcf_register_action(&act_gact_ops, &gact_net_ops);
  226. }
  227. static void __exit gact_cleanup_module(void)
  228. {
  229. tcf_unregister_action(&act_gact_ops, &gact_net_ops);
  230. }
  231. module_init(gact_init_module);
  232. module_exit(gact_cleanup_module);