act_gact.c 7.3 KB

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