act_gact.c 6.6 KB

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