act_gact.c 7.5 KB

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