2 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/filter.h>
16 #include <net/netlink.h>
17 #include <net/pkt_sched.h>
19 #include <linux/tc_act/tc_bpf.h>
20 #include <net/tc_act/tc_bpf.h>
22 #define BPF_TAB_MASK 15
24 static int tcf_bpf(struct sk_buff
*skb
, const struct tc_action
*a
,
25 struct tcf_result
*res
)
27 struct tcf_bpf
*b
= a
->priv
;
31 spin_lock(&b
->tcf_lock
);
32 b
->tcf_tm
.lastuse
= jiffies
;
33 bstats_update(&b
->tcf_bstats
, skb
);
34 action
= b
->tcf_action
;
36 filter_res
= BPF_PROG_RUN(b
->filter
, skb
);
37 if (filter_res
== 0) {
38 /* Return code 0 from the BPF program
39 * is being interpreted as a drop here.
42 b
->tcf_qstats
.drops
++;
45 spin_unlock(&b
->tcf_lock
);
49 static int tcf_bpf_dump(struct sk_buff
*skb
, struct tc_action
*a
,
52 unsigned char *tp
= skb_tail_pointer(skb
);
53 struct tcf_bpf
*b
= a
->priv
;
54 struct tc_act_bpf opt
= {
55 .index
= b
->tcf_index
,
56 .refcnt
= b
->tcf_refcnt
- ref
,
57 .bindcnt
= b
->tcf_bindcnt
- bind
,
58 .action
= b
->tcf_action
,
63 if (nla_put(skb
, TCA_ACT_BPF_PARMS
, sizeof(opt
), &opt
))
66 if (nla_put_u16(skb
, TCA_ACT_BPF_OPS_LEN
, b
->bpf_num_ops
))
69 nla
= nla_reserve(skb
, TCA_ACT_BPF_OPS
, b
->bpf_num_ops
*
70 sizeof(struct sock_filter
));
74 memcpy(nla_data(nla
), b
->bpf_ops
, nla_len(nla
));
76 t
.install
= jiffies_to_clock_t(jiffies
- b
->tcf_tm
.install
);
77 t
.lastuse
= jiffies_to_clock_t(jiffies
- b
->tcf_tm
.lastuse
);
78 t
.expires
= jiffies_to_clock_t(b
->tcf_tm
.expires
);
79 if (nla_put(skb
, TCA_ACT_BPF_TM
, sizeof(t
), &t
))
88 static const struct nla_policy act_bpf_policy
[TCA_ACT_BPF_MAX
+ 1] = {
89 [TCA_ACT_BPF_PARMS
] = { .len
= sizeof(struct tc_act_bpf
) },
90 [TCA_ACT_BPF_OPS_LEN
] = { .type
= NLA_U16
},
91 [TCA_ACT_BPF_OPS
] = { .type
= NLA_BINARY
,
92 .len
= sizeof(struct sock_filter
) * BPF_MAXINSNS
},
95 static int tcf_bpf_init(struct net
*net
, struct nlattr
*nla
,
96 struct nlattr
*est
, struct tc_action
*a
,
99 struct nlattr
*tb
[TCA_ACT_BPF_MAX
+ 1];
100 struct tc_act_bpf
*parm
;
102 u16 bpf_size
, bpf_num_ops
;
103 struct sock_filter
*bpf_ops
;
104 struct sock_fprog_kern tmp
;
111 ret
= nla_parse_nested(tb
, TCA_ACT_BPF_MAX
, nla
, act_bpf_policy
);
115 if (!tb
[TCA_ACT_BPF_PARMS
] ||
116 !tb
[TCA_ACT_BPF_OPS_LEN
] || !tb
[TCA_ACT_BPF_OPS
])
118 parm
= nla_data(tb
[TCA_ACT_BPF_PARMS
]);
120 bpf_num_ops
= nla_get_u16(tb
[TCA_ACT_BPF_OPS_LEN
]);
121 if (bpf_num_ops
> BPF_MAXINSNS
|| bpf_num_ops
== 0)
124 bpf_size
= bpf_num_ops
* sizeof(*bpf_ops
);
125 if (bpf_size
!= nla_len(tb
[TCA_ACT_BPF_OPS
]))
128 bpf_ops
= kzalloc(bpf_size
, GFP_KERNEL
);
132 memcpy(bpf_ops
, nla_data(tb
[TCA_ACT_BPF_OPS
]), bpf_size
);
134 tmp
.len
= bpf_num_ops
;
135 tmp
.filter
= bpf_ops
;
137 ret
= bpf_prog_create(&fp
, &tmp
);
141 if (!tcf_hash_check(parm
->index
, a
, bind
)) {
142 ret
= tcf_hash_create(parm
->index
, est
, a
, sizeof(*b
), bind
);
150 tcf_hash_release(a
, bind
);
158 spin_lock_bh(&b
->tcf_lock
);
159 b
->tcf_action
= parm
->action
;
160 b
->bpf_num_ops
= bpf_num_ops
;
161 b
->bpf_ops
= bpf_ops
;
163 spin_unlock_bh(&b
->tcf_lock
);
165 if (ret
== ACT_P_CREATED
)
170 bpf_prog_destroy(fp
);
176 static void tcf_bpf_cleanup(struct tc_action
*a
, int bind
)
178 struct tcf_bpf
*b
= a
->priv
;
180 bpf_prog_destroy(b
->filter
);
183 static struct tc_action_ops act_bpf_ops
= {
186 .owner
= THIS_MODULE
,
188 .dump
= tcf_bpf_dump
,
189 .cleanup
= tcf_bpf_cleanup
,
190 .init
= tcf_bpf_init
,
193 static int __init
bpf_init_module(void)
195 return tcf_register_action(&act_bpf_ops
, BPF_TAB_MASK
);
198 static void __exit
bpf_cleanup_module(void)
200 tcf_unregister_action(&act_bpf_ops
);
203 module_init(bpf_init_module
);
204 module_exit(bpf_cleanup_module
);
206 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
207 MODULE_DESCRIPTION("TC BPF based action");
208 MODULE_LICENSE("GPL v2");