blob: a0eed30d58111ab49e11f9cc97c4dc0d30e93c9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/gact.c Generic actions
3 *
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
David S. Millere9ce1cd2006-08-21 23:54:55 -070026#define GACT_TAB_MASK 15
WANG Cong369ba562013-12-15 20:15:08 -080027static struct tcf_hashinfo gact_hash_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#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{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -050032 if (!gact->tcfg_pval || prandom_u32() % gact->tcfg_pval)
David S. Millere9ce1cd2006-08-21 23:54:55 -070033 return gact->tcf_action;
34 return gact->tcfg_paction;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
David S. Millere9ce1cd2006-08-21 23:54:55 -070037static int gact_determ(struct tcf_gact *gact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Kim Nordlunda1631482006-12-01 20:21:44 -080039 if (!gact->tcfg_pval || gact->tcf_bstats.packets % 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,
54 struct nlattr *est, struct tc_action *a,
55 int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Patrick McHardy7ba699c2008-01-22 22:11:50 -080057 struct nlattr *tb[TCA_GACT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct tc_gact *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -070059 struct tcf_gact *gact;
60 struct tcf_common *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int ret = 0;
Patrick McHardycee63722008-01-23 20:33:32 -080062 int err;
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +090063#ifdef CONFIG_GACT_PROB
64 struct tc_gact_p *p_parm = NULL;
65#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Patrick McHardycee63722008-01-23 20:33:32 -080067 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return -EINVAL;
69
Patrick McHardy53b2bf32008-01-23 20:36:30 -080070 err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy);
Patrick McHardycee63722008-01-23 20:33:32 -080071 if (err < 0)
72 return err;
73
Patrick McHardy53b2bf32008-01-23 20:36:30 -080074 if (tb[TCA_GACT_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080076 parm = nla_data(tb[TCA_GACT_PARMS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Patrick McHardy53b2bf32008-01-23 20:36:30 -080078#ifndef CONFIG_GACT_PROB
Patrick McHardy7ba699c2008-01-22 22:11:50 -080079 if (tb[TCA_GACT_PROB] != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EOPNOTSUPP;
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +090081#else
82 if (tb[TCA_GACT_PROB]) {
83 p_parm = nla_data(tb[TCA_GACT_PROB]);
84 if (p_parm->ptype >= MAX_RAND)
85 return -EINVAL;
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#endif
88
WANG Congc779f7a2014-01-17 11:37:02 -080089 pc = tcf_hash_check(parm->index, a, bind);
David S. Millere9ce1cd2006-08-21 23:54:55 -070090 if (!pc) {
WANG Congc779f7a2014-01-17 11:37:02 -080091 pc = tcf_hash_create(parm->index, est, a, sizeof(*gact), bind);
Stephen Hemminger0e991ec2008-11-25 21:12:32 -080092 if (IS_ERR(pc))
Eric Dumazetcc7ec452011-01-19 19:26:56 +000093 return PTR_ERR(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 ret = ACT_P_CREATED;
95 } else {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050096 if (bind)/* dont override defaults */
97 return 0;
WANG Congc779f7a2014-01-17 11:37:02 -080098 tcf_hash_release(pc, bind, a->ops->hinfo);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050099 if (!ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 gact = to_gact(pc);
104
105 spin_lock_bh(&gact->tcf_lock);
106 gact->tcf_action = parm->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#ifdef CONFIG_GACT_PROB
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +0900108 if (p_parm) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700109 gact->tcfg_paction = p_parm->paction;
110 gact->tcfg_pval = p_parm->pval;
111 gact->tcfg_ptype = p_parm->ptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113#endif
David S. Millere9ce1cd2006-08-21 23:54:55 -0700114 spin_unlock_bh(&gact->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (ret == ACT_P_CREATED)
WANG Congc779f7a2014-01-17 11:37:02 -0800116 tcf_hash_insert(pc, a->ops->hinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ret;
118}
119
David S. Millere9ce1cd2006-08-21 23:54:55 -0700120static int tcf_gact_cleanup(struct tc_action *a, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700122 struct tcf_gact *gact = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
David S. Millere9ce1cd2006-08-21 23:54:55 -0700124 if (gact)
WANG Congc779f7a2014-01-17 11:37:02 -0800125 return tcf_hash_release(&gact->common, bind, a->ops->hinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 0;
127}
128
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000129static int tcf_gact(struct sk_buff *skb, const struct tc_action *a,
130 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700132 struct tcf_gact *gact = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int action = TC_ACT_SHOT;
134
David S. Millere9ce1cd2006-08-21 23:54:55 -0700135 spin_lock(&gact->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#ifdef CONFIG_GACT_PROB
Hiroaki SHIMODA696ecdc2012-08-03 19:57:52 +0900137 if (gact->tcfg_ptype)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700138 action = gact_rand[gact->tcfg_ptype](gact);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 else
David S. Millere9ce1cd2006-08-21 23:54:55 -0700140 action = gact->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#else
David S. Millere9ce1cd2006-08-21 23:54:55 -0700142 action = gact->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#endif
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700144 gact->tcf_bstats.bytes += qdisc_pkt_len(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700145 gact->tcf_bstats.packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (action == TC_ACT_SHOT)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700147 gact->tcf_qstats.drops++;
148 gact->tcf_tm.lastuse = jiffies;
149 spin_unlock(&gact->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return action;
152}
153
David S. Millere9ce1cd2006-08-21 23:54:55 -0700154static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700156 unsigned char *b = skb_tail_pointer(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700157 struct tcf_gact *gact = a->priv;
Eric Dumazet1c40be12010-08-16 20:04:22 +0000158 struct tc_gact opt = {
159 .index = gact->tcf_index,
160 .refcnt = gact->tcf_refcnt - ref,
161 .bindcnt = gact->tcf_bindcnt - bind,
162 .action = gact->tcf_action,
163 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct tcf_t t;
165
David S. Miller1b34ec42012-03-29 05:11:39 -0400166 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
167 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#ifdef CONFIG_GACT_PROB
David S. Millere9ce1cd2006-08-21 23:54:55 -0700169 if (gact->tcfg_ptype) {
Eric Dumazet1c40be12010-08-16 20:04:22 +0000170 struct tc_gact_p p_opt = {
171 .paction = gact->tcfg_paction,
172 .pval = gact->tcfg_pval,
173 .ptype = gact->tcfg_ptype,
174 };
175
David S. Miller1b34ec42012-03-29 05:11:39 -0400176 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
177 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179#endif
David S. Millere9ce1cd2006-08-21 23:54:55 -0700180 t.install = jiffies_to_clock_t(jiffies - gact->tcf_tm.install);
181 t.lastuse = jiffies_to_clock_t(jiffies - gact->tcf_tm.lastuse);
182 t.expires = jiffies_to_clock_t(gact->tcf_tm.expires);
David S. Miller1b34ec42012-03-29 05:11:39 -0400183 if (nla_put(skb, TCA_GACT_TM, sizeof(t), &t))
184 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return skb->len;
186
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800187nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700188 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -1;
190}
191
192static struct tc_action_ops act_gact_ops = {
193 .kind = "gact",
David S. Millere9ce1cd2006-08-21 23:54:55 -0700194 .hinfo = &gact_hash_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .type = TCA_ACT_GACT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .owner = THIS_MODULE,
197 .act = tcf_gact,
198 .dump = tcf_gact_dump,
199 .cleanup = tcf_gact_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 .init = tcf_gact_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
203MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
204MODULE_DESCRIPTION("Generic Classifier actions");
205MODULE_LICENSE("GPL");
206
David S. Millere9ce1cd2006-08-21 23:54:55 -0700207static int __init gact_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
WANG Cong568a1532013-12-20 00:08:51 -0800209 int err = tcf_hashinfo_init(&gact_hash_info, GACT_TAB_MASK);
WANG Cong369ba562013-12-15 20:15:08 -0800210 if (err)
211 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#ifdef CONFIG_GACT_PROB
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000213 pr_info("GACT probability on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#else
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000215 pr_info("GACT probability NOT on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#endif
217 return tcf_register_action(&act_gact_ops);
218}
219
David S. Millere9ce1cd2006-08-21 23:54:55 -0700220static void __exit gact_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 tcf_unregister_action(&act_gact_ops);
WANG Cong369ba562013-12-15 20:15:08 -0800223 tcf_hashinfo_destroy(&gact_hash_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226module_init(gact_init_module);
227module_exit(gact_cleanup_module);