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