blob: 73c3926358a066bb383197cc56d2a75af82e66df [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 return ret;
Davide Caratti0da2dbd2019-03-20 15:00:02 +0100144release_idr:
145 tcf_idr_release(*a, bind);
146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Jamal Hadi Salim17400052018-08-12 09:34:52 -0400149static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
150 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
WANG Conga85a9702016-07-25 16:09:41 -0700152 struct tcf_gact *gact = to_gact(a);
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700153 int action = READ_ONCE(gact->tcf_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#ifdef CONFIG_GACT_PROB
Eric Dumazet8f2ae965b2015-07-06 05:18:07 -0700156 {
157 u32 ptype = READ_ONCE(gact->tcfg_ptype);
158
159 if (ptype)
160 action = gact_rand[ptype](gact);
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#endif
Vlad Buslov5e1ad952019-10-30 16:09:01 +0200163 tcf_action_update_bstats(&gact->common, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (action == TC_ACT_SHOT)
Vlad Buslov26b537a2019-10-30 16:09:02 +0200165 tcf_action_inc_drop_qstats(&gact->common);
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700166
167 tcf_lastuse_update(&gact->tcf_tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return action;
170}
171
Po Liu4b61d3e2020-06-19 14:01:07 +0800172static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u64 packets,
173 u64 drops, u64 lastuse, bool hw)
Amir Vadai9fea47d2016-05-13 12:55:36 +0000174{
WANG Conga85a9702016-07-25 16:09:41 -0700175 struct tcf_gact *gact = to_gact(a);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000176 int action = READ_ONCE(gact->tcf_action);
177 struct tcf_t *tm = &gact->tcf_tm;
178
Po Liu4b61d3e2020-06-19 14:01:07 +0800179 tcf_action_update_stats(a, bytes, packets,
180 action == TC_ACT_SHOT ? packets : drops, hw);
Roi Dayan3bb23422017-12-26 07:48:51 +0200181 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000182}
183
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400184static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
185 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700187 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700188 struct tcf_gact *gact = to_gact(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000189 struct tc_gact opt = {
190 .index = gact->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300191 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
192 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000193 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct tcf_t t;
195
Vlad Buslov653cd282018-08-14 21:46:16 +0300196 spin_lock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300197 opt.action = gact->tcf_action;
David S. Miller1b34ec42012-03-29 05:11:39 -0400198 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
199 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#ifdef CONFIG_GACT_PROB
David S. Millere9ce1cd2006-08-21 23:54:55 -0700201 if (gact->tcfg_ptype) {
Eric Dumazet1c40be12010-08-16 20:04:22 +0000202 struct tc_gact_p p_opt = {
203 .paction = gact->tcfg_paction,
204 .pval = gact->tcfg_pval,
205 .ptype = gact->tcfg_ptype,
206 };
207
David S. Miller1b34ec42012-03-29 05:11:39 -0400208 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
209 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211#endif
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400212 tcf_tm_dump(&t, &gact->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200213 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400214 goto nla_put_failure;
Vlad Buslov653cd282018-08-14 21:46:16 +0300215 spin_unlock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return skb->len;
218
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800219nla_put_failure:
Vlad Buslov653cd282018-08-14 21:46:16 +0300220 spin_unlock_bh(&gact->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700221 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -1;
223}
224
WANG Congddf97cc2016-02-22 15:57:53 -0800225static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
226 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500227 const struct tc_action_ops *ops,
228 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800229{
230 struct tc_action_net *tn = net_generic(net, gact_net_id);
231
Alexander Aringb3620142018-02-15 10:54:59 -0500232 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800233}
234
Cong Wangf061b482018-08-29 10:15:35 -0700235static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800236{
237 struct tc_action_net *tn = net_generic(net, gact_net_id);
238
Chris Mi65a206c2017-08-30 02:31:59 -0400239 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800240}
241
Roman Mashak9c5c9c52018-03-08 16:59:20 -0500242static size_t tcf_gact_get_fill_size(const struct tc_action *act)
243{
244 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
245
246#ifdef CONFIG_GACT_PROB
247 if (to_gact(act)->tcfg_ptype)
248 /* TCA_GACT_PROB */
249 sz += nla_total_size(sizeof(struct tc_gact_p));
250#endif
251
252 return sz;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static struct tc_action_ops act_gact_ops = {
256 .kind = "gact",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200257 .id = TCA_ID_GACT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .owner = THIS_MODULE,
Jamal Hadi Salim17400052018-08-12 09:34:52 -0400259 .act = tcf_gact_act,
Amir Vadai9fea47d2016-05-13 12:55:36 +0000260 .stats_update = tcf_gact_stats_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .dump = tcf_gact_dump,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .init = tcf_gact_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800263 .walk = tcf_gact_walker,
264 .lookup = tcf_gact_search,
Roman Mashak9c5c9c52018-03-08 16:59:20 -0500265 .get_fill_size = tcf_gact_get_fill_size,
WANG Conga85a9702016-07-25 16:09:41 -0700266 .size = sizeof(struct tcf_gact),
WANG Congddf97cc2016-02-22 15:57:53 -0800267};
268
269static __net_init int gact_init_net(struct net *net)
270{
271 struct tc_action_net *tn = net_generic(net, gact_net_id);
272
Cong Wang981471b2019-08-25 10:01:32 -0700273 return tc_action_net_init(net, tn, &act_gact_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800274}
275
Cong Wang039af9c2017-12-11 15:35:03 -0800276static void __net_exit gact_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800277{
Cong Wang039af9c2017-12-11 15:35:03 -0800278 tc_action_net_exit(net_list, gact_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800279}
280
281static struct pernet_operations gact_net_ops = {
282 .init = gact_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800283 .exit_batch = gact_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800284 .id = &gact_net_id,
285 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
288MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
289MODULE_DESCRIPTION("Generic Classifier actions");
290MODULE_LICENSE("GPL");
291
David S. Millere9ce1cd2006-08-21 23:54:55 -0700292static int __init gact_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294#ifdef CONFIG_GACT_PROB
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000295 pr_info("GACT probability on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296#else
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000297 pr_info("GACT probability NOT on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif
WANG Congddf97cc2016-02-22 15:57:53 -0800299
300 return tcf_register_action(&act_gact_ops, &gact_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
David S. Millere9ce1cd2006-08-21 23:54:55 -0700303static void __exit gact_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
WANG Congddf97cc2016-02-22 15:57:53 -0800305 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308module_init(gact_init_module);
309module_exit(gact_cleanup_module);