act_gact.c 8.5 KB

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