act_gact.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/act_gact.c Generic actions
  4. *
  5. * copyright Jamal Hadi Salim (2002-4)
  6. */
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/string.h>
  10. #include <linux/errno.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <net/netlink.h>
  16. #include <net/pkt_sched.h>
  17. #include <net/pkt_cls.h>
  18. #include <linux/tc_act/tc_gact.h>
  19. #include <net/tc_act/tc_gact.h>
  20. #include <net/tc_wrapper.h>
  21. static struct tc_action_ops act_gact_ops;
  22. #ifdef CONFIG_GACT_PROB
  23. static int gact_net_rand(struct tcf_gact *gact)
  24. {
  25. smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
  26. if (get_random_u32_below(gact->tcfg_pval))
  27. return gact->tcf_action;
  28. return gact->tcfg_paction;
  29. }
  30. static int gact_determ(struct tcf_gact *gact)
  31. {
  32. u32 pack = atomic_inc_return(&gact->packets);
  33. smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
  34. if (pack % gact->tcfg_pval)
  35. return gact->tcf_action;
  36. return gact->tcfg_paction;
  37. }
  38. typedef int (*g_rand)(struct tcf_gact *gact);
  39. static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
  40. #endif /* CONFIG_GACT_PROB */
  41. static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
  42. [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) },
  43. [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) },
  44. };
  45. static int tcf_gact_init(struct net *net, struct nlattr *nla,
  46. struct nlattr *est, struct tc_action **a,
  47. struct tcf_proto *tp, u32 flags,
  48. struct netlink_ext_ack *extack)
  49. {
  50. struct tc_action_net *tn = net_generic(net, act_gact_ops.net_id);
  51. bool bind = flags & TCA_ACT_FLAGS_BIND;
  52. struct nlattr *tb[TCA_GACT_MAX + 1];
  53. struct tcf_chain *goto_ch = NULL;
  54. struct tc_gact *parm;
  55. struct tcf_gact *gact;
  56. int ret = 0;
  57. u32 index;
  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_deprecated(tb, TCA_GACT_MAX, nla, gact_policy,
  65. 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. index = parm->index;
  72. #ifndef CONFIG_GACT_PROB
  73. if (tb[TCA_GACT_PROB] != NULL)
  74. return -EOPNOTSUPP;
  75. #else
  76. if (tb[TCA_GACT_PROB]) {
  77. p_parm = nla_data(tb[TCA_GACT_PROB]);
  78. if (p_parm->ptype >= MAX_RAND)
  79. return -EINVAL;
  80. if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) {
  81. NL_SET_ERR_MSG(extack,
  82. "goto chain not allowed on fallback");
  83. return -EINVAL;
  84. }
  85. }
  86. #endif
  87. err = tcf_idr_check_alloc(tn, &index, a, bind);
  88. if (!err) {
  89. ret = tcf_idr_create_from_flags(tn, index, est, a,
  90. &act_gact_ops, bind, flags);
  91. if (ret) {
  92. tcf_idr_cleanup(tn, index);
  93. return ret;
  94. }
  95. ret = ACT_P_CREATED;
  96. } else if (err > 0) {
  97. if (bind)/* dont override defaults */
  98. return ACT_P_BOUND;
  99. if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  100. tcf_idr_release(*a, bind);
  101. return -EEXIST;
  102. }
  103. } else {
  104. return err;
  105. }
  106. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  107. if (err < 0)
  108. goto release_idr;
  109. gact = to_gact(*a);
  110. spin_lock_bh(&gact->tcf_lock);
  111. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  112. #ifdef CONFIG_GACT_PROB
  113. if (p_parm) {
  114. gact->tcfg_paction = p_parm->paction;
  115. gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
  116. /* Make sure tcfg_pval is written before tcfg_ptype
  117. * coupled with smp_rmb() in gact_net_rand() & gact_determ()
  118. */
  119. smp_wmb();
  120. gact->tcfg_ptype = p_parm->ptype;
  121. }
  122. #endif
  123. spin_unlock_bh(&gact->tcf_lock);
  124. if (goto_ch)
  125. tcf_chain_put_by_act(goto_ch);
  126. return ret;
  127. release_idr:
  128. tcf_idr_release(*a, bind);
  129. return err;
  130. }
  131. TC_INDIRECT_SCOPE int tcf_gact_act(struct sk_buff *skb,
  132. const struct tc_action *a,
  133. struct tcf_result *res)
  134. {
  135. struct tcf_gact *gact = to_gact(a);
  136. int action = READ_ONCE(gact->tcf_action);
  137. #ifdef CONFIG_GACT_PROB
  138. {
  139. u32 ptype = READ_ONCE(gact->tcfg_ptype);
  140. if (ptype)
  141. action = gact_rand[ptype](gact);
  142. }
  143. #endif
  144. tcf_action_update_bstats(&gact->common, skb);
  145. if (action == TC_ACT_SHOT)
  146. tcf_action_inc_drop_qstats(&gact->common);
  147. tcf_lastuse_update(&gact->tcf_tm);
  148. return action;
  149. }
  150. static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u64 packets,
  151. u64 drops, u64 lastuse, bool hw)
  152. {
  153. struct tcf_gact *gact = to_gact(a);
  154. int action = READ_ONCE(gact->tcf_action);
  155. struct tcf_t *tm = &gact->tcf_tm;
  156. tcf_action_update_stats(a, bytes, packets,
  157. action == TC_ACT_SHOT ? packets : drops, hw);
  158. tm->lastuse = max_t(u64, tm->lastuse, lastuse);
  159. }
  160. static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
  161. int bind, int ref)
  162. {
  163. unsigned char *b = skb_tail_pointer(skb);
  164. struct tcf_gact *gact = to_gact(a);
  165. struct tc_gact opt = {
  166. .index = gact->tcf_index,
  167. .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
  168. .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
  169. };
  170. struct tcf_t t;
  171. spin_lock_bh(&gact->tcf_lock);
  172. opt.action = gact->tcf_action;
  173. if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
  174. goto nla_put_failure;
  175. #ifdef CONFIG_GACT_PROB
  176. if (gact->tcfg_ptype) {
  177. struct tc_gact_p p_opt = {
  178. .paction = gact->tcfg_paction,
  179. .pval = gact->tcfg_pval,
  180. .ptype = gact->tcfg_ptype,
  181. };
  182. if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
  183. goto nla_put_failure;
  184. }
  185. #endif
  186. tcf_tm_dump(&t, &gact->tcf_tm);
  187. if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
  188. goto nla_put_failure;
  189. spin_unlock_bh(&gact->tcf_lock);
  190. return skb->len;
  191. nla_put_failure:
  192. spin_unlock_bh(&gact->tcf_lock);
  193. nlmsg_trim(skb, b);
  194. return -1;
  195. }
  196. static size_t tcf_gact_get_fill_size(const struct tc_action *act)
  197. {
  198. size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
  199. #ifdef CONFIG_GACT_PROB
  200. if (to_gact(act)->tcfg_ptype)
  201. /* TCA_GACT_PROB */
  202. sz += nla_total_size(sizeof(struct tc_gact_p));
  203. #endif
  204. return sz;
  205. }
  206. static int tcf_gact_offload_act_setup(struct tc_action *act, void *entry_data,
  207. u32 *index_inc, bool bind,
  208. struct netlink_ext_ack *extack)
  209. {
  210. if (bind) {
  211. struct flow_action_entry *entry = entry_data;
  212. if (is_tcf_gact_ok(act)) {
  213. entry->id = FLOW_ACTION_ACCEPT;
  214. } else if (is_tcf_gact_shot(act)) {
  215. entry->id = FLOW_ACTION_DROP;
  216. } else if (is_tcf_gact_trap(act)) {
  217. entry->id = FLOW_ACTION_TRAP;
  218. } else if (is_tcf_gact_goto_chain(act)) {
  219. entry->id = FLOW_ACTION_GOTO;
  220. entry->chain_index = tcf_gact_goto_chain_index(act);
  221. } else if (is_tcf_gact_continue(act)) {
  222. NL_SET_ERR_MSG_MOD(extack, "Offload of \"continue\" action is not supported");
  223. return -EOPNOTSUPP;
  224. } else if (is_tcf_gact_reclassify(act)) {
  225. NL_SET_ERR_MSG_MOD(extack, "Offload of \"reclassify\" action is not supported");
  226. return -EOPNOTSUPP;
  227. } else if (is_tcf_gact_pipe(act)) {
  228. NL_SET_ERR_MSG_MOD(extack, "Offload of \"pipe\" action is not supported");
  229. return -EOPNOTSUPP;
  230. } else {
  231. NL_SET_ERR_MSG_MOD(extack, "Unsupported generic action offload");
  232. return -EOPNOTSUPP;
  233. }
  234. *index_inc = 1;
  235. } else {
  236. struct flow_offload_action *fl_action = entry_data;
  237. if (is_tcf_gact_ok(act))
  238. fl_action->id = FLOW_ACTION_ACCEPT;
  239. else if (is_tcf_gact_shot(act))
  240. fl_action->id = FLOW_ACTION_DROP;
  241. else if (is_tcf_gact_trap(act))
  242. fl_action->id = FLOW_ACTION_TRAP;
  243. else if (is_tcf_gact_goto_chain(act))
  244. fl_action->id = FLOW_ACTION_GOTO;
  245. else
  246. return -EOPNOTSUPP;
  247. }
  248. return 0;
  249. }
  250. static struct tc_action_ops act_gact_ops = {
  251. .kind = "gact",
  252. .id = TCA_ID_GACT,
  253. .owner = THIS_MODULE,
  254. .act = tcf_gact_act,
  255. .stats_update = tcf_gact_stats_update,
  256. .dump = tcf_gact_dump,
  257. .init = tcf_gact_init,
  258. .get_fill_size = tcf_gact_get_fill_size,
  259. .offload_act_setup = tcf_gact_offload_act_setup,
  260. .size = sizeof(struct tcf_gact),
  261. };
  262. MODULE_ALIAS_NET_ACT("gact");
  263. static __net_init int gact_init_net(struct net *net)
  264. {
  265. struct tc_action_net *tn = net_generic(net, act_gact_ops.net_id);
  266. return tc_action_net_init(net, tn, &act_gact_ops);
  267. }
  268. static void __net_exit gact_exit_net(struct list_head *net_list)
  269. {
  270. tc_action_net_exit(net_list, act_gact_ops.net_id);
  271. }
  272. static struct pernet_operations gact_net_ops = {
  273. .init = gact_init_net,
  274. .exit_batch = gact_exit_net,
  275. .id = &act_gact_ops.net_id,
  276. .size = sizeof(struct tc_action_net),
  277. };
  278. MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
  279. MODULE_DESCRIPTION("Generic Classifier actions");
  280. MODULE_LICENSE("GPL");
  281. static int __init gact_init_module(void)
  282. {
  283. #ifdef CONFIG_GACT_PROB
  284. pr_info("GACT probability on\n");
  285. #else
  286. pr_info("GACT probability NOT on\n");
  287. #endif
  288. return tcf_register_action(&act_gact_ops, &gact_net_ops);
  289. }
  290. static void __exit gact_cleanup_module(void)
  291. {
  292. tcf_unregister_action(&act_gact_ops, &gact_net_ops);
  293. }
  294. module_init(gact_init_module);
  295. module_exit(gact_cleanup_module);