Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
0c6965dd | 2 | * net/sched/act_gact.c Generic actions |
1da177e4 LT |
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 | ||
1da177e4 LT |
13 | #include <linux/types.h> |
14 | #include <linux/kernel.h> | |
1da177e4 | 15 | #include <linux/string.h> |
1da177e4 | 16 | #include <linux/errno.h> |
1da177e4 LT |
17 | #include <linux/skbuff.h> |
18 | #include <linux/rtnetlink.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/init.h> | |
dc5fc579 | 21 | #include <net/netlink.h> |
1da177e4 LT |
22 | #include <net/pkt_sched.h> |
23 | #include <linux/tc_act/tc_gact.h> | |
24 | #include <net/tc_act/tc_gact.h> | |
25 | ||
e9ce1cd3 | 26 | #define GACT_TAB_MASK 15 |
1da177e4 LT |
27 | |
28 | #ifdef CONFIG_GACT_PROB | |
e9ce1cd3 | 29 | static int gact_net_rand(struct tcf_gact *gact) |
1da177e4 | 30 | { |
cef5ecf9 ED |
31 | smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */ |
32 | if (prandom_u32() % gact->tcfg_pval) | |
e9ce1cd3 DM |
33 | return gact->tcf_action; |
34 | return gact->tcfg_paction; | |
1da177e4 LT |
35 | } |
36 | ||
e9ce1cd3 | 37 | static int gact_determ(struct tcf_gact *gact) |
1da177e4 | 38 | { |
cc6510a9 ED |
39 | u32 pack = atomic_inc_return(&gact->packets); |
40 | ||
cef5ecf9 | 41 | smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */ |
cc6510a9 | 42 | if (pack % gact->tcfg_pval) |
e9ce1cd3 DM |
43 | return gact->tcf_action; |
44 | return gact->tcfg_paction; | |
1da177e4 LT |
45 | } |
46 | ||
e9ce1cd3 | 47 | typedef int (*g_rand)(struct tcf_gact *gact); |
cc7ec456 | 48 | static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ }; |
e9ce1cd3 | 49 | #endif /* CONFIG_GACT_PROB */ |
1da177e4 | 50 | |
53b2bf3f PM |
51 | static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = { |
52 | [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) }, | |
53 | [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) }, | |
54 | }; | |
55 | ||
c1b52739 BL |
56 | static int tcf_gact_init(struct net *net, struct nlattr *nla, |
57 | struct nlattr *est, struct tc_action *a, | |
58 | int ovr, int bind) | |
1da177e4 | 59 | { |
7ba699c6 | 60 | struct nlattr *tb[TCA_GACT_MAX + 1]; |
1da177e4 | 61 | struct tc_gact *parm; |
e9ce1cd3 | 62 | struct tcf_gact *gact; |
1da177e4 | 63 | int ret = 0; |
cee63723 | 64 | int err; |
696ecdc1 HS |
65 | #ifdef CONFIG_GACT_PROB |
66 | struct tc_gact_p *p_parm = NULL; | |
67 | #endif | |
1da177e4 | 68 | |
cee63723 | 69 | if (nla == NULL) |
1da177e4 LT |
70 | return -EINVAL; |
71 | ||
53b2bf3f | 72 | err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy); |
cee63723 PM |
73 | if (err < 0) |
74 | return err; | |
75 | ||
53b2bf3f | 76 | if (tb[TCA_GACT_PARMS] == NULL) |
1da177e4 | 77 | return -EINVAL; |
7ba699c6 | 78 | parm = nla_data(tb[TCA_GACT_PARMS]); |
1da177e4 | 79 | |
53b2bf3f | 80 | #ifndef CONFIG_GACT_PROB |
7ba699c6 | 81 | if (tb[TCA_GACT_PROB] != NULL) |
1da177e4 | 82 | return -EOPNOTSUPP; |
696ecdc1 HS |
83 | #else |
84 | if (tb[TCA_GACT_PROB]) { | |
85 | p_parm = nla_data(tb[TCA_GACT_PROB]); | |
86 | if (p_parm->ptype >= MAX_RAND) | |
87 | return -EINVAL; | |
88 | } | |
1da177e4 LT |
89 | #endif |
90 | ||
86062033 | 91 | if (!tcf_hash_check(parm->index, a, bind)) { |
519c818e ED |
92 | ret = tcf_hash_create(parm->index, est, a, sizeof(*gact), |
93 | bind, false); | |
86062033 WC |
94 | if (ret) |
95 | return ret; | |
1da177e4 LT |
96 | ret = ACT_P_CREATED; |
97 | } else { | |
1a29321e JHS |
98 | if (bind)/* dont override defaults */ |
99 | return 0; | |
86062033 | 100 | tcf_hash_release(a, bind); |
1a29321e | 101 | if (!ovr) |
1da177e4 | 102 | return -EEXIST; |
1da177e4 LT |
103 | } |
104 | ||
86062033 | 105 | gact = to_gact(a); |
e9ce1cd3 DM |
106 | |
107 | spin_lock_bh(&gact->tcf_lock); | |
108 | gact->tcf_action = parm->action; | |
1da177e4 | 109 | #ifdef CONFIG_GACT_PROB |
696ecdc1 | 110 | if (p_parm) { |
e9ce1cd3 | 111 | gact->tcfg_paction = p_parm->paction; |
cef5ecf9 ED |
112 | gact->tcfg_pval = max_t(u16, 1, p_parm->pval); |
113 | /* Make sure tcfg_pval is written before tcfg_ptype | |
114 | * coupled with smp_rmb() in gact_net_rand() & gact_determ() | |
115 | */ | |
116 | smp_wmb(); | |
e9ce1cd3 | 117 | gact->tcfg_ptype = p_parm->ptype; |
1da177e4 LT |
118 | } |
119 | #endif | |
e9ce1cd3 | 120 | spin_unlock_bh(&gact->tcf_lock); |
1da177e4 | 121 | if (ret == ACT_P_CREATED) |
86062033 | 122 | tcf_hash_insert(a); |
1da177e4 LT |
123 | return ret; |
124 | } | |
125 | ||
dc7f9f6e ED |
126 | static int tcf_gact(struct sk_buff *skb, const struct tc_action *a, |
127 | struct tcf_result *res) | |
1da177e4 | 128 | { |
e9ce1cd3 | 129 | struct tcf_gact *gact = a->priv; |
8f2ae965 | 130 | int action = gact->tcf_action; |
1da177e4 | 131 | |
e9ce1cd3 | 132 | spin_lock(&gact->tcf_lock); |
1da177e4 | 133 | #ifdef CONFIG_GACT_PROB |
8f2ae965 ED |
134 | { |
135 | u32 ptype = READ_ONCE(gact->tcfg_ptype); | |
136 | ||
137 | if (ptype) | |
138 | action = gact_rand[ptype](gact); | |
139 | } | |
1da177e4 | 140 | #endif |
0abf77e5 | 141 | gact->tcf_bstats.bytes += qdisc_pkt_len(skb); |
e9ce1cd3 | 142 | gact->tcf_bstats.packets++; |
1da177e4 | 143 | if (action == TC_ACT_SHOT) |
e9ce1cd3 DM |
144 | gact->tcf_qstats.drops++; |
145 | gact->tcf_tm.lastuse = jiffies; | |
146 | spin_unlock(&gact->tcf_lock); | |
1da177e4 LT |
147 | |
148 | return action; | |
149 | } | |
150 | ||
e9ce1cd3 | 151 | static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
1da177e4 | 152 | { |
27a884dc | 153 | unsigned char *b = skb_tail_pointer(skb); |
e9ce1cd3 | 154 | struct tcf_gact *gact = a->priv; |
1c40be12 ED |
155 | struct tc_gact opt = { |
156 | .index = gact->tcf_index, | |
157 | .refcnt = gact->tcf_refcnt - ref, | |
158 | .bindcnt = gact->tcf_bindcnt - bind, | |
159 | .action = gact->tcf_action, | |
160 | }; | |
1da177e4 LT |
161 | struct tcf_t t; |
162 | ||
1b34ec43 DM |
163 | if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt)) |
164 | goto nla_put_failure; | |
1da177e4 | 165 | #ifdef CONFIG_GACT_PROB |
e9ce1cd3 | 166 | if (gact->tcfg_ptype) { |
1c40be12 ED |
167 | struct tc_gact_p p_opt = { |
168 | .paction = gact->tcfg_paction, | |
169 | .pval = gact->tcfg_pval, | |
170 | .ptype = gact->tcfg_ptype, | |
171 | }; | |
172 | ||
1b34ec43 DM |
173 | if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt)) |
174 | goto nla_put_failure; | |
1da177e4 LT |
175 | } |
176 | #endif | |
e9ce1cd3 DM |
177 | t.install = jiffies_to_clock_t(jiffies - gact->tcf_tm.install); |
178 | t.lastuse = jiffies_to_clock_t(jiffies - gact->tcf_tm.lastuse); | |
179 | t.expires = jiffies_to_clock_t(gact->tcf_tm.expires); | |
1b34ec43 DM |
180 | if (nla_put(skb, TCA_GACT_TM, sizeof(t), &t)) |
181 | goto nla_put_failure; | |
1da177e4 LT |
182 | return skb->len; |
183 | ||
7ba699c6 | 184 | nla_put_failure: |
dc5fc579 | 185 | nlmsg_trim(skb, b); |
1da177e4 LT |
186 | return -1; |
187 | } | |
188 | ||
189 | static struct tc_action_ops act_gact_ops = { | |
190 | .kind = "gact", | |
191 | .type = TCA_ACT_GACT, | |
1da177e4 LT |
192 | .owner = THIS_MODULE, |
193 | .act = tcf_gact, | |
194 | .dump = tcf_gact_dump, | |
1da177e4 | 195 | .init = tcf_gact_init, |
1da177e4 LT |
196 | }; |
197 | ||
198 | MODULE_AUTHOR("Jamal Hadi Salim(2002-4)"); | |
199 | MODULE_DESCRIPTION("Generic Classifier actions"); | |
200 | MODULE_LICENSE("GPL"); | |
201 | ||
e9ce1cd3 | 202 | static int __init gact_init_module(void) |
1da177e4 LT |
203 | { |
204 | #ifdef CONFIG_GACT_PROB | |
cc7ec456 | 205 | pr_info("GACT probability on\n"); |
1da177e4 | 206 | #else |
cc7ec456 | 207 | pr_info("GACT probability NOT on\n"); |
1da177e4 | 208 | #endif |
4f1e9d89 | 209 | return tcf_register_action(&act_gact_ops, GACT_TAB_MASK); |
1da177e4 LT |
210 | } |
211 | ||
e9ce1cd3 | 212 | static void __exit gact_cleanup_module(void) |
1da177e4 LT |
213 | { |
214 | tcf_unregister_action(&act_gact_ops); | |
215 | } | |
216 | ||
217 | module_init(gact_init_module); | |
218 | module_exit(gact_cleanup_module); |