blob: 41606577271968c7dd03ecd99f040bec63199297 [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,
Vlad Buslovabbb0d32019-10-30 16:09:05 +020056 struct tcf_proto *tp, u32 flags,
57 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
WANG Congddf97cc2016-02-22 15:57:53 -080059 struct tc_action_net *tn = net_generic(net, gact_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080060 struct nlattr *tb[TCA_GACT_MAX + 1];
Davide Caratti0da2dbd2019-03-20 15:00:02 +010061 struct tcf_chain *goto_ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct tc_gact *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070063 struct tcf_gact *gact;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int ret = 0;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000065 u32 index;
Patrick McHardycee63722008-01-23 20:33:32 -080066 int err;
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +090067#ifdef CONFIG_GACT_PROB
68 struct tc_gact_p *p_parm = NULL;
69#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardycee63722008-01-23 20:33:32 -080071 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -EINVAL;
73
Johannes Berg8cb08172019-04-26 14:07:28 +020074 err = nla_parse_nested_deprecated(tb, TCA_GACT_MAX, nla, gact_policy,
75 NULL);
Patrick McHardycee63722008-01-23 20:33:32 -080076 if (err < 0)
77 return err;
78
Patrick McHardy53b2bf32008-01-23 20:36:30 -080079 if (tb[TCA_GACT_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080081 parm = nla_data(tb[TCA_GACT_PARMS]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000082 index = parm->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Patrick McHardy53b2bf32008-01-23 20:36:30 -080084#ifndef CONFIG_GACT_PROB
Patrick McHardy7ba699c2008-01-22 22:11:50 -080085 if (tb[TCA_GACT_PROB] != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return -EOPNOTSUPP;
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +090087#else
88 if (tb[TCA_GACT_PROB]) {
89 p_parm = nla_data(tb[TCA_GACT_PROB]);
90 if (p_parm->ptype >= MAX_RAND)
91 return -EINVAL;
Davide Caratti9469f372018-10-20 23:33:07 +020092 if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) {
93 NL_SET_ERR_MSG(extack,
94 "goto chain not allowed on fallback");
95 return -EINVAL;
96 }
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +090097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#endif
99
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000100 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300101 if (!err) {
Vlad Buslove3822672019-10-30 16:09:06 +0200102 ret = tcf_idr_create_from_flags(tn, index, est, a,
103 &act_gact_ops, bind, flags);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300104 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000105 tcf_idr_cleanup(tn, index);
WANG Cong86062032014-02-11 17:07:31 -0800106 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 ret = ACT_P_CREATED;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300109 } else if (err > 0) {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500110 if (bind)/* dont override defaults */
111 return 0;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300112 if (!ovr) {
113 tcf_idr_release(*a, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -EEXIST;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300115 }
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300116 } else {
117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119
Davide Caratti0da2dbd2019-03-20 15:00:02 +0100120 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
121 if (err < 0)
122 goto release_idr;
WANG Conga85a9702016-07-25 16:09:41 -0700123 gact = to_gact(*a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700124
Vlad Buslov653cd282018-08-14 21:46:16 +0300125 spin_lock_bh(&gact->tcf_lock);
Davide Caratti0da2dbd2019-03-20 15:00:02 +0100126 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#ifdef CONFIG_GACT_PROB
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +0900128 if (p_parm) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700129 gact->tcfg_paction = p_parm->paction;
Eric Dumazetcef5ecf2015-07-06 05:18:05 -0700130 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
131 /* Make sure tcfg_pval is written before tcfg_ptype
132 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
133 */
134 smp_wmb();
David S. Millere9ce1cd2006-08-21 23:54:55 -0700135 gact->tcfg_ptype = p_parm->ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137#endif
Vlad Buslov653cd282018-08-14 21:46:16 +0300138 spin_unlock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300139
Davide Caratti0da2dbd2019-03-20 15:00:02 +0100140 if (goto_ch)
141 tcf_chain_put_by_act(goto_ch);
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400144 tcf_idr_insert(tn, *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return ret;
Davide Caratti0da2dbd2019-03-20 15:00:02 +0100146release_idr:
147 tcf_idr_release(*a, bind);
148 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Jamal Hadi Salim17400052018-08-12 09:34:52 -0400151static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
152 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
WANG Conga85a9702016-07-25 16:09:41 -0700154 struct tcf_gact *gact = to_gact(a);
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700155 int action = READ_ONCE(gact->tcf_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#ifdef CONFIG_GACT_PROB
Eric Dumazet8f2ae965b2015-07-06 05:18:07 -0700158 {
159 u32 ptype = READ_ONCE(gact->tcfg_ptype);
160
161 if (ptype)
162 action = gact_rand[ptype](gact);
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#endif
Vlad Buslov5e1ad952019-10-30 16:09:01 +0200165 tcf_action_update_bstats(&gact->common, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (action == TC_ACT_SHOT)
Vlad Buslov26b537a2019-10-30 16:09:02 +0200167 tcf_action_inc_drop_qstats(&gact->common);
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700168
169 tcf_lastuse_update(&gact->tcf_tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return action;
172}
173
Amir Vadai9fea47d2016-05-13 12:55:36 +0000174static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
Eelco Chaudron28169ab2018-09-21 07:14:02 -0400175 u64 lastuse, bool hw)
Amir Vadai9fea47d2016-05-13 12:55:36 +0000176{
WANG Conga85a9702016-07-25 16:09:41 -0700177 struct tcf_gact *gact = to_gact(a);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000178 int action = READ_ONCE(gact->tcf_action);
179 struct tcf_t *tm = &gact->tcf_tm;
180
Vlad Buslovc8ecebd2019-10-30 16:09:00 +0200181 tcf_action_update_stats(a, bytes, packets, action == TC_ACT_SHOT, hw);
Roi Dayan3bb23422017-12-26 07:48:51 +0200182 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000183}
184
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400185static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
186 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700188 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700189 struct tcf_gact *gact = to_gact(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000190 struct tc_gact opt = {
191 .index = gact->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300192 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
193 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000194 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 struct tcf_t t;
196
Vlad Buslov653cd282018-08-14 21:46:16 +0300197 spin_lock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300198 opt.action = gact->tcf_action;
David S. Miller1b34ec42012-03-29 05:11:39 -0400199 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
200 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#ifdef CONFIG_GACT_PROB
David S. Millere9ce1cd2006-08-21 23:54:55 -0700202 if (gact->tcfg_ptype) {
Eric Dumazet1c40be12010-08-16 20:04:22 +0000203 struct tc_gact_p p_opt = {
204 .paction = gact->tcfg_paction,
205 .pval = gact->tcfg_pval,
206 .ptype = gact->tcfg_ptype,
207 };
208
David S. Miller1b34ec42012-03-29 05:11:39 -0400209 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
210 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212#endif
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400213 tcf_tm_dump(&t, &gact->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200214 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400215 goto nla_put_failure;
Vlad Buslov653cd282018-08-14 21:46:16 +0300216 spin_unlock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return skb->len;
219
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800220nla_put_failure:
Vlad Buslov653cd282018-08-14 21:46:16 +0300221 spin_unlock_bh(&gact->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700222 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -1;
224}
225
WANG Congddf97cc2016-02-22 15:57:53 -0800226static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
227 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500228 const struct tc_action_ops *ops,
229 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800230{
231 struct tc_action_net *tn = net_generic(net, gact_net_id);
232
Alexander Aringb3620142018-02-15 10:54:59 -0500233 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800234}
235
Cong Wangf061b482018-08-29 10:15:35 -0700236static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800237{
238 struct tc_action_net *tn = net_generic(net, gact_net_id);
239
Chris Mi65a206c2017-08-30 02:31:59 -0400240 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800241}
242
Roman Mashak9c5c9c52018-03-08 16:59:20 -0500243static size_t tcf_gact_get_fill_size(const struct tc_action *act)
244{
245 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
246
247#ifdef CONFIG_GACT_PROB
248 if (to_gact(act)->tcfg_ptype)
249 /* TCA_GACT_PROB */
250 sz += nla_total_size(sizeof(struct tc_gact_p));
251#endif
252
253 return sz;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static struct tc_action_ops act_gact_ops = {
257 .kind = "gact",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200258 .id = TCA_ID_GACT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .owner = THIS_MODULE,
Jamal Hadi Salim17400052018-08-12 09:34:52 -0400260 .act = tcf_gact_act,
Amir Vadai9fea47d2016-05-13 12:55:36 +0000261 .stats_update = tcf_gact_stats_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .dump = tcf_gact_dump,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 .init = tcf_gact_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800264 .walk = tcf_gact_walker,
265 .lookup = tcf_gact_search,
Roman Mashak9c5c9c52018-03-08 16:59:20 -0500266 .get_fill_size = tcf_gact_get_fill_size,
WANG Conga85a9702016-07-25 16:09:41 -0700267 .size = sizeof(struct tcf_gact),
WANG Congddf97cc2016-02-22 15:57:53 -0800268};
269
270static __net_init int gact_init_net(struct net *net)
271{
272 struct tc_action_net *tn = net_generic(net, gact_net_id);
273
Cong Wang981471b2019-08-25 10:01:32 -0700274 return tc_action_net_init(net, tn, &act_gact_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800275}
276
Cong Wang039af9c2017-12-11 15:35:03 -0800277static void __net_exit gact_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800278{
Cong Wang039af9c2017-12-11 15:35:03 -0800279 tc_action_net_exit(net_list, gact_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800280}
281
282static struct pernet_operations gact_net_ops = {
283 .init = gact_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800284 .exit_batch = gact_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800285 .id = &gact_net_id,
286 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
288
289MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
290MODULE_DESCRIPTION("Generic Classifier actions");
291MODULE_LICENSE("GPL");
292
David S. Millere9ce1cd2006-08-21 23:54:55 -0700293static int __init gact_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295#ifdef CONFIG_GACT_PROB
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000296 pr_info("GACT probability on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#else
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000298 pr_info("GACT probability NOT on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#endif
WANG Congddf97cc2016-02-22 15:57:53 -0800300
301 return tcf_register_action(&act_gact_ops, &gact_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
David S. Millere9ce1cd2006-08-21 23:54:55 -0700304static void __exit gact_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
WANG Congddf97cc2016-02-22 15:57:53 -0800306 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309module_init(gact_init_module);
310module_exit(gact_cleanup_module);