act_gact.c 4.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * net/sched/gact.c	Generic actions
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * copyright 	Jamal Hadi Salim (2002-4)
 *
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/module.h>
#include <linux/init.h>
21
#include <net/netlink.h>
L
Linus Torvalds 已提交
22 23 24 25
#include <net/pkt_sched.h>
#include <linux/tc_act/tc_gact.h>
#include <net/tc_act/tc_gact.h>

26
#define GACT_TAB_MASK	15
L
Linus Torvalds 已提交
27 28

#ifdef CONFIG_GACT_PROB
29
static int gact_net_rand(struct tcf_gact *gact)
L
Linus Torvalds 已提交
30
{
31
	if (!gact->tcfg_pval || prandom_u32() % gact->tcfg_pval)
32 33
		return gact->tcf_action;
	return gact->tcfg_paction;
L
Linus Torvalds 已提交
34 35
}

36
static int gact_determ(struct tcf_gact *gact)
L
Linus Torvalds 已提交
37
{
38
	if (!gact->tcfg_pval || gact->tcf_bstats.packets % gact->tcfg_pval)
39 40
		return gact->tcf_action;
	return gact->tcfg_paction;
L
Linus Torvalds 已提交
41 42
}

43
typedef int (*g_rand)(struct tcf_gact *gact);
E
Eric Dumazet 已提交
44
static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
45
#endif /* CONFIG_GACT_PROB */
L
Linus Torvalds 已提交
46

47 48 49 50 51
static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
	[TCA_GACT_PARMS]	= { .len = sizeof(struct tc_gact) },
	[TCA_GACT_PROB]		= { .len = sizeof(struct tc_gact_p) },
};

52 53 54
static int tcf_gact_init(struct net *net, struct nlattr *nla,
			 struct nlattr *est, struct tc_action *a,
			 int ovr, int bind)
L
Linus Torvalds 已提交
55
{
56
	struct nlattr *tb[TCA_GACT_MAX + 1];
L
Linus Torvalds 已提交
57
	struct tc_gact *parm;
58
	struct tcf_gact *gact;
L
Linus Torvalds 已提交
59
	int ret = 0;
60
	int err;
61 62 63
#ifdef CONFIG_GACT_PROB
	struct tc_gact_p *p_parm = NULL;
#endif
L
Linus Torvalds 已提交
64

65
	if (nla == NULL)
L
Linus Torvalds 已提交
66 67
		return -EINVAL;

68
	err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy);
69 70 71
	if (err < 0)
		return err;

72
	if (tb[TCA_GACT_PARMS] == NULL)
L
Linus Torvalds 已提交
73
		return -EINVAL;
74
	parm = nla_data(tb[TCA_GACT_PARMS]);
L
Linus Torvalds 已提交
75

76
#ifndef CONFIG_GACT_PROB
77
	if (tb[TCA_GACT_PROB] != NULL)
L
Linus Torvalds 已提交
78
		return -EOPNOTSUPP;
79 80 81 82 83 84
#else
	if (tb[TCA_GACT_PROB]) {
		p_parm = nla_data(tb[TCA_GACT_PROB]);
		if (p_parm->ptype >= MAX_RAND)
			return -EINVAL;
	}
L
Linus Torvalds 已提交
85 86
#endif

87 88 89 90
	if (!tcf_hash_check(parm->index, a, bind)) {
		ret = tcf_hash_create(parm->index, est, a, sizeof(*gact), bind);
		if (ret)
			return ret;
L
Linus Torvalds 已提交
91 92
		ret = ACT_P_CREATED;
	} else {
93 94
		if (bind)/* dont override defaults */
			return 0;
95
		tcf_hash_release(a, bind);
96
		if (!ovr)
L
Linus Torvalds 已提交
97 98 99
			return -EEXIST;
	}

100
	gact = to_gact(a);
101 102 103

	spin_lock_bh(&gact->tcf_lock);
	gact->tcf_action = parm->action;
L
Linus Torvalds 已提交
104
#ifdef CONFIG_GACT_PROB
105
	if (p_parm) {
106 107 108
		gact->tcfg_paction = p_parm->paction;
		gact->tcfg_pval    = p_parm->pval;
		gact->tcfg_ptype   = p_parm->ptype;
L
Linus Torvalds 已提交
109 110
	}
#endif
111
	spin_unlock_bh(&gact->tcf_lock);
L
Linus Torvalds 已提交
112
	if (ret == ACT_P_CREATED)
113
		tcf_hash_insert(a);
L
Linus Torvalds 已提交
114 115 116
	return ret;
}

117 118
static int tcf_gact(struct sk_buff *skb, const struct tc_action *a,
		    struct tcf_result *res)
L
Linus Torvalds 已提交
119
{
120
	struct tcf_gact *gact = a->priv;
L
Linus Torvalds 已提交
121 122
	int action = TC_ACT_SHOT;

123
	spin_lock(&gact->tcf_lock);
L
Linus Torvalds 已提交
124
#ifdef CONFIG_GACT_PROB
125
	if (gact->tcfg_ptype)
126
		action = gact_rand[gact->tcfg_ptype](gact);
L
Linus Torvalds 已提交
127
	else
128
		action = gact->tcf_action;
L
Linus Torvalds 已提交
129
#else
130
	action = gact->tcf_action;
L
Linus Torvalds 已提交
131
#endif
132
	gact->tcf_bstats.bytes += qdisc_pkt_len(skb);
133
	gact->tcf_bstats.packets++;
L
Linus Torvalds 已提交
134
	if (action == TC_ACT_SHOT)
135 136 137
		gact->tcf_qstats.drops++;
	gact->tcf_tm.lastuse = jiffies;
	spin_unlock(&gact->tcf_lock);
L
Linus Torvalds 已提交
138 139 140 141

	return action;
}

142
static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
L
Linus Torvalds 已提交
143
{
144
	unsigned char *b = skb_tail_pointer(skb);
145
	struct tcf_gact *gact = a->priv;
146 147 148 149 150 151
	struct tc_gact opt = {
		.index   = gact->tcf_index,
		.refcnt  = gact->tcf_refcnt - ref,
		.bindcnt = gact->tcf_bindcnt - bind,
		.action  = gact->tcf_action,
	};
L
Linus Torvalds 已提交
152 153
	struct tcf_t t;

154 155
	if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
		goto nla_put_failure;
L
Linus Torvalds 已提交
156
#ifdef CONFIG_GACT_PROB
157
	if (gact->tcfg_ptype) {
158 159 160 161 162 163
		struct tc_gact_p p_opt = {
			.paction = gact->tcfg_paction,
			.pval    = gact->tcfg_pval,
			.ptype   = gact->tcfg_ptype,
		};

164 165
		if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
			goto nla_put_failure;
L
Linus Torvalds 已提交
166 167
	}
#endif
168 169 170
	t.install = jiffies_to_clock_t(jiffies - gact->tcf_tm.install);
	t.lastuse = jiffies_to_clock_t(jiffies - gact->tcf_tm.lastuse);
	t.expires = jiffies_to_clock_t(gact->tcf_tm.expires);
171 172
	if (nla_put(skb, TCA_GACT_TM, sizeof(t), &t))
		goto nla_put_failure;
L
Linus Torvalds 已提交
173 174
	return skb->len;

175
nla_put_failure:
176
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	return -1;
}

static struct tc_action_ops act_gact_ops = {
	.kind		=	"gact",
	.type		=	TCA_ACT_GACT,
	.owner		=	THIS_MODULE,
	.act		=	tcf_gact,
	.dump		=	tcf_gact_dump,
	.init		=	tcf_gact_init,
};

MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
MODULE_DESCRIPTION("Generic Classifier actions");
MODULE_LICENSE("GPL");

193
static int __init gact_init_module(void)
L
Linus Torvalds 已提交
194 195
{
#ifdef CONFIG_GACT_PROB
E
Eric Dumazet 已提交
196
	pr_info("GACT probability on\n");
L
Linus Torvalds 已提交
197
#else
E
Eric Dumazet 已提交
198
	pr_info("GACT probability NOT on\n");
L
Linus Torvalds 已提交
199
#endif
200
	return tcf_register_action(&act_gact_ops, GACT_TAB_MASK);
L
Linus Torvalds 已提交
201 202
}

203
static void __exit gact_cleanup_module(void)
L
Linus Torvalds 已提交
204 205 206 207 208 209
{
	tcf_unregister_action(&act_gact_ops);
}

module_init(gact_init_module);
module_exit(gact_cleanup_module);