blob: 324f1d1f6d477973596fe334ce682587610a30a7 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jiri Pirko0c6965d2014-11-05 20:51:51 +01003 * net/sched/act_gact.c Generic actions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * copyright Jamal Hadi Salim (2002-4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/types.h>
9#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/skbuff.h>
13#include <linux/rtnetlink.h>
14#include <linux/module.h>
15#include <linux/init.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070016#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/pkt_sched.h>
Davide Caratti0da2dbd2019-03-20 15:00:02 +010018#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/tc_act/tc_gact.h>
20#include <net/tc_act/tc_gact.h>
21
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030022static unsigned int gact_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070023static struct tc_action_ops act_gact_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#ifdef CONFIG_GACT_PROB
David S. Millere9ce1cd2006-08-21 23:54:55 -070026static int gact_net_rand(struct tcf_gact *gact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Eric Dumazetcef5ecf2015-07-06 05:18:05 -070028 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
29 if (prandom_u32() % gact->tcfg_pval)
David S. Millere9ce1cd2006-08-21 23:54:55 -070030 return gact->tcf_action;
31 return gact->tcfg_paction;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
David S. Millere9ce1cd2006-08-21 23:54:55 -070034static int gact_determ(struct tcf_gact *gact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Eric Dumazetcc6510a2015-07-06 05:18:06 -070036 u32 pack = atomic_inc_return(&gact->packets);
37
Eric Dumazetcef5ecf2015-07-06 05:18:05 -070038 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
Eric Dumazetcc6510a2015-07-06 05:18:06 -070039 if (pack % gact->tcfg_pval)
David S. Millere9ce1cd2006-08-21 23:54:55 -070040 return gact->tcf_action;
41 return gact->tcfg_paction;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
David S. Millere9ce1cd2006-08-21 23:54:55 -070044typedef int (*g_rand)(struct tcf_gact *gact);
Eric Dumazetcc7ec452011-01-19 19:26:56 +000045static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
David S. Millere9ce1cd2006-08-21 23:54:55 -070046#endif /* CONFIG_GACT_PROB */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Patrick McHardy53b2bf32008-01-23 20:36:30 -080048static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
49 [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) },
50 [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) },
51};
52
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000053static int tcf_gact_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070054 struct nlattr *est, struct tc_action **a,
Vlad Buslov789871b2018-07-05 17:24:25 +030055 int ovr, int bind, bool rtnl_held,
Davide Caratti85d09662019-03-20 14:59:59 +010056 struct tcf_proto *tp, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
WANG Congddf97cc2016-02-22 15:57:53 -080058 struct tc_action_net *tn = net_generic(net, gact_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080059 struct nlattr *tb[TCA_GACT_MAX + 1];
Davide Caratti0da2dbd2019-03-20 15:00:02 +010060 struct tcf_chain *goto_ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct tc_gact *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070062 struct tcf_gact *gact;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int ret = 0;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000064 u32 index;
Patrick McHardycee63722008-01-23 20:33:32 -080065 int err;
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +090066#ifdef CONFIG_GACT_PROB
67 struct tc_gact_p *p_parm = NULL;
68#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Patrick McHardycee63722008-01-23 20:33:32 -080070 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return -EINVAL;
72
Johannes Berg8cb08172019-04-26 14:07:28 +020073 err = nla_parse_nested_deprecated(tb, TCA_GACT_MAX, nla, gact_policy,
74 NULL);
Patrick McHardycee63722008-01-23 20:33:32 -080075 if (err < 0)
76 return err;
77
Patrick McHardy53b2bf32008-01-23 20:36:30 -080078 if (tb[TCA_GACT_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080080 parm = nla_data(tb[TCA_GACT_PARMS]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000081 index = parm->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Patrick McHardy53b2bf32008-01-23 20:36:30 -080083#ifndef CONFIG_GACT_PROB
Patrick McHardy7ba699c2008-01-22 22:11:50 -080084 if (tb[TCA_GACT_PROB] != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return -EOPNOTSUPP;
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +090086#else
87 if (tb[TCA_GACT_PROB]) {
88 p_parm = nla_data(tb[TCA_GACT_PROB]);
89 if (p_parm->ptype >= MAX_RAND)
90 return -EINVAL;
Davide Caratti9469f372018-10-20 23:33:07 +020091 if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) {
92 NL_SET_ERR_MSG(extack,
93 "goto chain not allowed on fallback");
94 return -EINVAL;
95 }
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +090096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#endif
98
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000099 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300100 if (!err) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000101 ret = tcf_idr_create(tn, index, est, a,
Chris Mi65a206c2017-08-30 02:31:59 -0400102 &act_gact_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300103 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000104 tcf_idr_cleanup(tn, index);
WANG Cong86062032014-02-11 17:07:31 -0800105 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ret = ACT_P_CREATED;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300108 } else if (err > 0) {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500109 if (bind)/* dont override defaults */
110 return 0;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300111 if (!ovr) {
112 tcf_idr_release(*a, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -EEXIST;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300114 }
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300115 } else {
116 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118
Davide Caratti0da2dbd2019-03-20 15:00:02 +0100119 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
120 if (err < 0)
121 goto release_idr;
WANG Conga85a9702016-07-25 16:09:41 -0700122 gact = to_gact(*a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700123
Vlad Buslov653cd282018-08-14 21:46:16 +0300124 spin_lock_bh(&gact->tcf_lock);
Davide Caratti0da2dbd2019-03-20 15:00:02 +0100125 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#ifdef CONFIG_GACT_PROB
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +0900127 if (p_parm) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700128 gact->tcfg_paction = p_parm->paction;
Eric Dumazetcef5ecf2015-07-06 05:18:05 -0700129 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
130 /* Make sure tcfg_pval is written before tcfg_ptype
131 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
132 */
133 smp_wmb();
David S. Millere9ce1cd2006-08-21 23:54:55 -0700134 gact->tcfg_ptype = p_parm->ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136#endif
Vlad Buslov653cd282018-08-14 21:46:16 +0300137 spin_unlock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300138
Davide Caratti0da2dbd2019-03-20 15:00:02 +0100139 if (goto_ch)
140 tcf_chain_put_by_act(goto_ch);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400143 tcf_idr_insert(tn, *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return ret;
Davide Caratti0da2dbd2019-03-20 15:00:02 +0100145release_idr:
146 tcf_idr_release(*a, bind);
147 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Jamal Hadi Salim17400052018-08-12 09:34:52 -0400150static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
151 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
WANG Conga85a9702016-07-25 16:09:41 -0700153 struct tcf_gact *gact = to_gact(a);
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700154 int action = READ_ONCE(gact->tcf_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#ifdef CONFIG_GACT_PROB
Eric Dumazet8f2ae965b2015-07-06 05:18:07 -0700157 {
158 u32 ptype = READ_ONCE(gact->tcfg_ptype);
159
160 if (ptype)
161 action = gact_rand[ptype](gact);
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#endif
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700164 bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (action == TC_ACT_SHOT)
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700166 qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
167
168 tcf_lastuse_update(&gact->tcf_tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 return action;
171}
172
Amir Vadai9fea47d2016-05-13 12:55:36 +0000173static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400174 u64 lastuse, bool hw)
Amir Vadai9fea47d2016-05-13 12:55:36 +0000175{
WANG Conga85a9702016-07-25 16:09:41 -0700176 struct tcf_gact *gact = to_gact(a);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000177 int action = READ_ONCE(gact->tcf_action);
178 struct tcf_t *tm = &gact->tcf_tm;
179
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400180 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes,
181 packets);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000182 if (action == TC_ACT_SHOT)
183 this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
184
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400185 if (hw)
186 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats_hw),
187 bytes, packets);
188
Roi Dayan3bb23422017-12-26 07:48:51 +0200189 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000190}
191
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400192static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
193 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700195 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700196 struct tcf_gact *gact = to_gact(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000197 struct tc_gact opt = {
198 .index = gact->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300199 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
200 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000201 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct tcf_t t;
203
Vlad Buslov653cd282018-08-14 21:46:16 +0300204 spin_lock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300205 opt.action = gact->tcf_action;
David S. Miller1b34ec42012-03-29 05:11:39 -0400206 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
207 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#ifdef CONFIG_GACT_PROB
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209 if (gact->tcfg_ptype) {
Eric Dumazet1c40be12010-08-16 20:04:22 +0000210 struct tc_gact_p p_opt = {
211 .paction = gact->tcfg_paction,
212 .pval = gact->tcfg_pval,
213 .ptype = gact->tcfg_ptype,
214 };
215
David S. Miller1b34ec42012-03-29 05:11:39 -0400216 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
217 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219#endif
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400220 tcf_tm_dump(&t, &gact->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200221 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400222 goto nla_put_failure;
Vlad Buslov653cd282018-08-14 21:46:16 +0300223 spin_unlock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return skb->len;
226
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800227nla_put_failure:
Vlad Buslov653cd282018-08-14 21:46:16 +0300228 spin_unlock_bh(&gact->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700229 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -1;
231}
232
WANG Congddf97cc2016-02-22 15:57:53 -0800233static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
234 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500235 const struct tc_action_ops *ops,
236 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800237{
238 struct tc_action_net *tn = net_generic(net, gact_net_id);
239
Alexander Aringb3620142018-02-15 10:54:59 -0500240 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800241}
242
Cong Wangf061b482018-08-29 10:15:35 -0700243static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800244{
245 struct tc_action_net *tn = net_generic(net, gact_net_id);
246
Chris Mi65a206c2017-08-30 02:31:59 -0400247 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800248}
249
Roman Mashak9c5c9c52018-03-08 16:59:20 -0500250static size_t tcf_gact_get_fill_size(const struct tc_action *act)
251{
252 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
253
254#ifdef CONFIG_GACT_PROB
255 if (to_gact(act)->tcfg_ptype)
256 /* TCA_GACT_PROB */
257 sz += nla_total_size(sizeof(struct tc_gact_p));
258#endif
259
260 return sz;
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static struct tc_action_ops act_gact_ops = {
264 .kind = "gact",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200265 .id = TCA_ID_GACT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .owner = THIS_MODULE,
Jamal Hadi Salim17400052018-08-12 09:34:52 -0400267 .act = tcf_gact_act,
Amir Vadai9fea47d2016-05-13 12:55:36 +0000268 .stats_update = tcf_gact_stats_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 .dump = tcf_gact_dump,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .init = tcf_gact_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800271 .walk = tcf_gact_walker,
272 .lookup = tcf_gact_search,
Roman Mashak9c5c9c52018-03-08 16:59:20 -0500273 .get_fill_size = tcf_gact_get_fill_size,
WANG Conga85a9702016-07-25 16:09:41 -0700274 .size = sizeof(struct tcf_gact),
WANG Congddf97cc2016-02-22 15:57:53 -0800275};
276
277static __net_init int gact_init_net(struct net *net)
278{
279 struct tc_action_net *tn = net_generic(net, gact_net_id);
280
Cong Wang981471bd2019-08-25 10:01:32 -0700281 return tc_action_net_init(net, tn, &act_gact_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800282}
283
Cong Wang039af9c2017-12-11 15:35:03 -0800284static void __net_exit gact_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800285{
Cong Wang039af9c2017-12-11 15:35:03 -0800286 tc_action_net_exit(net_list, gact_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800287}
288
289static struct pernet_operations gact_net_ops = {
290 .init = gact_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800291 .exit_batch = gact_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800292 .id = &gact_net_id,
293 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294};
295
296MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
297MODULE_DESCRIPTION("Generic Classifier actions");
298MODULE_LICENSE("GPL");
299
David S. Millere9ce1cd2006-08-21 23:54:55 -0700300static int __init gact_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302#ifdef CONFIG_GACT_PROB
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000303 pr_info("GACT probability on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#else
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000305 pr_info("GACT probability NOT on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#endif
WANG Congddf97cc2016-02-22 15:57:53 -0800307
308 return tcf_register_action(&act_gact_ops, &gact_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
David S. Millere9ce1cd2006-08-21 23:54:55 -0700311static void __exit gact_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
WANG Congddf97cc2016-02-22 15:57:53 -0800313 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316module_init(gact_init_module);
317module_exit(gact_cleanup_module);