blob: 6a3f25a8ffb30b8998fc2a7c37aaccc2c29c466d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jiri Pirko0c6965d2014-11-05 20:51:51 +01002 * net/sched/act_gact.c Generic actions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * copyright Jamal Hadi Salim (2002-4)
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/skbuff.h>
18#include <linux/rtnetlink.h>
19#include <linux/module.h>
20#include <linux/init.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070021#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/pkt_sched.h>
23#include <linux/tc_act/tc_gact.h>
24#include <net/tc_act/tc_gact.h>
25
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030026static unsigned int gact_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070027static struct tc_action_ops act_gact_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#ifdef CONFIG_GACT_PROB
David S. Millere9ce1cd2006-08-21 23:54:55 -070030static int gact_net_rand(struct tcf_gact *gact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Eric Dumazetcef5ecf2015-07-06 05:18:05 -070032 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
33 if (prandom_u32() % gact->tcfg_pval)
David S. Millere9ce1cd2006-08-21 23:54:55 -070034 return gact->tcf_action;
35 return gact->tcfg_paction;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
David S. Millere9ce1cd2006-08-21 23:54:55 -070038static int gact_determ(struct tcf_gact *gact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Eric Dumazetcc6510a2015-07-06 05:18:06 -070040 u32 pack = atomic_inc_return(&gact->packets);
41
Eric Dumazetcef5ecf2015-07-06 05:18:05 -070042 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
Eric Dumazetcc6510a2015-07-06 05:18:06 -070043 if (pack % gact->tcfg_pval)
David S. Millere9ce1cd2006-08-21 23:54:55 -070044 return gact->tcf_action;
45 return gact->tcfg_paction;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
David S. Millere9ce1cd2006-08-21 23:54:55 -070048typedef int (*g_rand)(struct tcf_gact *gact);
Eric Dumazetcc7ec452011-01-19 19:26:56 +000049static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
David S. Millere9ce1cd2006-08-21 23:54:55 -070050#endif /* CONFIG_GACT_PROB */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Patrick McHardy53b2bf32008-01-23 20:36:30 -080052static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
53 [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) },
54 [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) },
55};
56
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000057static int tcf_gact_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070058 struct nlattr *est, struct tc_action **a,
Vlad Buslov789871b2018-07-05 17:24:25 +030059 int ovr, int bind, bool rtnl_held,
60 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
WANG Congddf97cc2016-02-22 15:57:53 -080062 struct tc_action_net *tn = net_generic(net, gact_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080063 struct nlattr *tb[TCA_GACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct tc_gact *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070065 struct tcf_gact *gact;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 int ret = 0;
Patrick McHardycee63722008-01-23 20:33:32 -080067 int err;
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +090068#ifdef CONFIG_GACT_PROB
69 struct tc_gact_p *p_parm = NULL;
70#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Patrick McHardycee63722008-01-23 20:33:32 -080072 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -EINVAL;
74
Johannes Bergfceb6432017-04-12 14:34:07 +020075 err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy, 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]);
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;
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif
93
Vlad Buslov0190c1d2018-07-05 17:24:32 +030094 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
95 if (!err) {
Chris Mi65a206c2017-08-30 02:31:59 -040096 ret = tcf_idr_create(tn, parm->index, est, a,
97 &act_gact_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030098 if (ret) {
99 tcf_idr_cleanup(tn, parm->index);
WANG Cong86062032014-02-11 17:07:31 -0800100 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 ret = ACT_P_CREATED;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300103 } else if (err > 0) {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -0500104 if (bind)/* dont override defaults */
105 return 0;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300106 if (!ovr) {
107 tcf_idr_release(*a, bind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -EEXIST;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300109 }
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300110 } else {
111 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
WANG Conga85a9702016-07-25 16:09:41 -0700114 gact = to_gact(*a);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700115
Vlad Buslov653cd282018-08-14 21:46:16 +0300116 spin_lock_bh(&gact->tcf_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700117 gact->tcf_action = parm->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#ifdef CONFIG_GACT_PROB
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +0900119 if (p_parm) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120 gact->tcfg_paction = p_parm->paction;
Eric Dumazetcef5ecf2015-07-06 05:18:05 -0700121 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
122 /* Make sure tcfg_pval is written before tcfg_ptype
123 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
124 */
125 smp_wmb();
David S. Millere9ce1cd2006-08-21 23:54:55 -0700126 gact->tcfg_ptype = p_parm->ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128#endif
Vlad Buslov653cd282018-08-14 21:46:16 +0300129 spin_unlock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400132 tcf_idr_insert(tn, *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return ret;
134}
135
Jamal Hadi Salim17400052018-08-12 09:34:52 -0400136static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
137 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
WANG Conga85a9702016-07-25 16:09:41 -0700139 struct tcf_gact *gact = to_gact(a);
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700140 int action = READ_ONCE(gact->tcf_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#ifdef CONFIG_GACT_PROB
Eric Dumazet8f2ae965b2015-07-06 05:18:07 -0700143 {
144 u32 ptype = READ_ONCE(gact->tcfg_ptype);
145
146 if (ptype)
147 action = gact_rand[ptype](gact);
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700150 bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (action == TC_ACT_SHOT)
Eric Dumazet56e5d1c2015-07-06 05:18:08 -0700152 qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
153
154 tcf_lastuse_update(&gact->tcf_tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return action;
157}
158
Amir Vadai9fea47d2016-05-13 12:55:36 +0000159static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
160 u64 lastuse)
161{
WANG Conga85a9702016-07-25 16:09:41 -0700162 struct tcf_gact *gact = to_gact(a);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000163 int action = READ_ONCE(gact->tcf_action);
164 struct tcf_t *tm = &gact->tcf_tm;
165
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400166 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes,
167 packets);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000168 if (action == TC_ACT_SHOT)
169 this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
170
Roi Dayan3bb23422017-12-26 07:48:51 +0200171 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
Amir Vadai9fea47d2016-05-13 12:55:36 +0000172}
173
Jamal Hadi Salim0b0f43f2016-06-05 10:41:32 -0400174static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
175 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700177 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700178 struct tcf_gact *gact = to_gact(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000179 struct tc_gact opt = {
180 .index = gact->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300181 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
182 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000183 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct tcf_t t;
185
Vlad Buslov653cd282018-08-14 21:46:16 +0300186 spin_lock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300187 opt.action = gact->tcf_action;
David S. Miller1b34ec42012-03-29 05:11:39 -0400188 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
189 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#ifdef CONFIG_GACT_PROB
David S. Millere9ce1cd2006-08-21 23:54:55 -0700191 if (gact->tcfg_ptype) {
Eric Dumazet1c40be12010-08-16 20:04:22 +0000192 struct tc_gact_p p_opt = {
193 .paction = gact->tcfg_paction,
194 .pval = gact->tcfg_pval,
195 .ptype = gact->tcfg_ptype,
196 };
197
David S. Miller1b34ec42012-03-29 05:11:39 -0400198 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
199 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201#endif
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400202 tcf_tm_dump(&t, &gact->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200203 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400204 goto nla_put_failure;
Vlad Buslov653cd282018-08-14 21:46:16 +0300205 spin_unlock_bh(&gact->tcf_lock);
Vlad Buslove8917f42018-08-10 20:51:43 +0300206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return skb->len;
208
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800209nla_put_failure:
Vlad Buslov653cd282018-08-14 21:46:16 +0300210 spin_unlock_bh(&gact->tcf_lock);
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700211 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -1;
213}
214
WANG Congddf97cc2016-02-22 15:57:53 -0800215static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
216 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500217 const struct tc_action_ops *ops,
218 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800219{
220 struct tc_action_net *tn = net_generic(net, gact_net_id);
221
Alexander Aringb3620142018-02-15 10:54:59 -0500222 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800223}
224
Alexander Aring331a9292018-02-15 10:54:57 -0500225static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index,
226 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800227{
228 struct tc_action_net *tn = net_generic(net, gact_net_id);
229
Chris Mi65a206c2017-08-30 02:31:59 -0400230 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800231}
232
Roman Mashak9c5c9c52018-03-08 16:59:20 -0500233static size_t tcf_gact_get_fill_size(const struct tc_action *act)
234{
235 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
236
237#ifdef CONFIG_GACT_PROB
238 if (to_gact(act)->tcfg_ptype)
239 /* TCA_GACT_PROB */
240 sz += nla_total_size(sizeof(struct tc_gact_p));
241#endif
242
243 return sz;
244}
245
Vlad Buslovb4090742018-07-05 17:24:28 +0300246static int tcf_gact_delete(struct net *net, u32 index)
247{
248 struct tc_action_net *tn = net_generic(net, gact_net_id);
249
250 return tcf_idr_delete_index(tn, index);
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static struct tc_action_ops act_gact_ops = {
254 .kind = "gact",
255 .type = TCA_ACT_GACT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 .owner = THIS_MODULE,
Jamal Hadi Salim17400052018-08-12 09:34:52 -0400257 .act = tcf_gact_act,
Amir Vadai9fea47d2016-05-13 12:55:36 +0000258 .stats_update = tcf_gact_stats_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .dump = tcf_gact_dump,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .init = tcf_gact_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800261 .walk = tcf_gact_walker,
262 .lookup = tcf_gact_search,
Roman Mashak9c5c9c52018-03-08 16:59:20 -0500263 .get_fill_size = tcf_gact_get_fill_size,
Vlad Buslovb4090742018-07-05 17:24:28 +0300264 .delete = tcf_gact_delete,
WANG Conga85a9702016-07-25 16:09:41 -0700265 .size = sizeof(struct tcf_gact),
WANG Congddf97cc2016-02-22 15:57:53 -0800266};
267
268static __net_init int gact_init_net(struct net *net)
269{
270 struct tc_action_net *tn = net_generic(net, gact_net_id);
271
Cong Wangc7e460c2017-11-06 13:47:18 -0800272 return tc_action_net_init(tn, &act_gact_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800273}
274
Cong Wang039af9c2017-12-11 15:35:03 -0800275static void __net_exit gact_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800276{
Cong Wang039af9c2017-12-11 15:35:03 -0800277 tc_action_net_exit(net_list, gact_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800278}
279
280static struct pernet_operations gact_net_ops = {
281 .init = gact_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800282 .exit_batch = gact_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800283 .id = &gact_net_id,
284 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
287MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
288MODULE_DESCRIPTION("Generic Classifier actions");
289MODULE_LICENSE("GPL");
290
David S. Millere9ce1cd2006-08-21 23:54:55 -0700291static int __init gact_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293#ifdef CONFIG_GACT_PROB
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000294 pr_info("GACT probability on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#else
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000296 pr_info("GACT probability NOT on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#endif
WANG Congddf97cc2016-02-22 15:57:53 -0800298
299 return tcf_register_action(&act_gact_ops, &gact_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
David S. Millere9ce1cd2006-08-21 23:54:55 -0700302static void __exit gact_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
WANG Congddf97cc2016-02-22 15:57:53 -0800304 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307module_init(gact_init_module);
308module_exit(gact_cleanup_module);