Skip to content

Commit 33a4892

Browse files
jpirkodavem330
authored andcommitted
sched: push TC filter protocol creation into a separate function
Make the long function tc_ctl_tfilter a little bit shorter and easier to read. Also make the creation of filter proto symmetric to destruction. Signed-off-by: Jiri Pirko <[email protected]> Acked-by: Jamal Hadi Salim <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent cf1facd commit 33a4892

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

net/sched/cls_api.c

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/kernel.h>
2020
#include <linux/string.h>
2121
#include <linux/errno.h>
22+
#include <linux/err.h>
2223
#include <linux/skbuff.h>
2324
#include <linux/init.h>
2425
#include <linux/kmod.h>
@@ -38,14 +39,14 @@ static DEFINE_RWLOCK(cls_mod_lock);
3839

3940
/* Find classifier type by string name */
4041

41-
static const struct tcf_proto_ops *tcf_proto_lookup_ops(struct nlattr *kind)
42+
static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
4243
{
4344
const struct tcf_proto_ops *t, *res = NULL;
4445

4546
if (kind) {
4647
read_lock(&cls_mod_lock);
4748
list_for_each_entry(t, &tcf_proto_base, head) {
48-
if (nla_strcmp(kind, t->kind) == 0) {
49+
if (strcmp(kind, t->kind) == 0) {
4950
if (try_module_get(t->owner))
5051
res = t;
5152
break;
@@ -127,6 +128,56 @@ static inline u32 tcf_auto_prio(struct tcf_proto *tp)
127128
return first;
128129
}
129130

131+
static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
132+
u32 prio, u32 parent, struct Qdisc *q)
133+
{
134+
struct tcf_proto *tp;
135+
int err;
136+
137+
tp = kzalloc(sizeof(*tp), GFP_KERNEL);
138+
if (!tp)
139+
return ERR_PTR(-ENOBUFS);
140+
141+
err = -ENOENT;
142+
tp->ops = tcf_proto_lookup_ops(kind);
143+
if (!tp->ops) {
144+
#ifdef CONFIG_MODULES
145+
rtnl_unlock();
146+
request_module("cls_%s", kind);
147+
rtnl_lock();
148+
tp->ops = tcf_proto_lookup_ops(kind);
149+
/* We dropped the RTNL semaphore in order to perform
150+
* the module load. So, even if we succeeded in loading
151+
* the module we have to replay the request. We indicate
152+
* this using -EAGAIN.
153+
*/
154+
if (tp->ops) {
155+
module_put(tp->ops->owner);
156+
err = -EAGAIN;
157+
} else {
158+
err = -ENOENT;
159+
}
160+
goto errout;
161+
#endif
162+
}
163+
tp->classify = tp->ops->classify;
164+
tp->protocol = protocol;
165+
tp->prio = prio;
166+
tp->classid = parent;
167+
tp->q = q;
168+
169+
err = tp->ops->init(tp);
170+
if (err) {
171+
module_put(tp->ops->owner);
172+
goto errout;
173+
}
174+
return tp;
175+
176+
errout:
177+
kfree(tp);
178+
return ERR_PTR(err);
179+
}
180+
130181
static bool tcf_proto_destroy(struct tcf_proto *tp, bool force)
131182
{
132183
if (tp->ops->destroy(tp, force)) {
@@ -164,7 +215,6 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
164215
struct tcf_proto __rcu **back;
165216
struct tcf_proto __rcu **chain;
166217
struct tcf_proto *tp;
167-
const struct tcf_proto_ops *tp_ops;
168218
const struct Qdisc_class_ops *cops;
169219
unsigned long cl;
170220
unsigned long fh;
@@ -279,58 +329,16 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n)
279329
!(n->nlmsg_flags & NLM_F_CREATE))
280330
goto errout;
281331

332+
if (!nprio)
333+
nprio = TC_H_MAJ(tcf_auto_prio(rtnl_dereference(*back)));
282334

283-
/* Create new proto tcf */
284-
285-
err = -ENOBUFS;
286-
tp = kzalloc(sizeof(*tp), GFP_KERNEL);
287-
if (tp == NULL)
288-
goto errout;
289-
err = -ENOENT;
290-
tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
291-
if (tp_ops == NULL) {
292-
#ifdef CONFIG_MODULES
293-
struct nlattr *kind = tca[TCA_KIND];
294-
char name[IFNAMSIZ];
295-
296-
if (kind != NULL &&
297-
nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
298-
rtnl_unlock();
299-
request_module("cls_%s", name);
300-
rtnl_lock();
301-
tp_ops = tcf_proto_lookup_ops(kind);
302-
/* We dropped the RTNL semaphore in order to
303-
* perform the module load. So, even if we
304-
* succeeded in loading the module we have to
305-
* replay the request. We indicate this using
306-
* -EAGAIN.
307-
*/
308-
if (tp_ops != NULL) {
309-
module_put(tp_ops->owner);
310-
err = -EAGAIN;
311-
}
312-
}
313-
#endif
314-
kfree(tp);
335+
tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
336+
protocol, nprio, parent, q);
337+
if (IS_ERR(tp)) {
338+
err = PTR_ERR(tp);
315339
goto errout;
316340
}
317-
tp->ops = tp_ops;
318-
tp->protocol = protocol;
319-
tp->prio = nprio ? :
320-
TC_H_MAJ(tcf_auto_prio(rtnl_dereference(*back)));
321-
tp->q = q;
322-
tp->classify = tp_ops->classify;
323-
tp->classid = parent;
324-
325-
err = tp_ops->init(tp);
326-
if (err != 0) {
327-
module_put(tp_ops->owner);
328-
kfree(tp);
329-
goto errout;
330-
}
331-
332341
tp_created = 1;
333-
334342
} else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
335343
goto errout;
336344

0 commit comments

Comments
 (0)