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 <linux/bpf.h>
18 #include <net/netlink.h>
19 #include <net/pkt_sched.h>
21 #include <linux/tc_act/tc_bpf.h>
22 #include <net/tc_act/tc_bpf.h>
24 #define BPF_TAB_MASK 15
25 #define ACT_BPF_NAME_LEN 256
28 struct bpf_prog
*filter
;
29 struct sock_filter
*bpf_ops
;
36 static int tcf_bpf(struct sk_buff
*skb
, const struct tc_action
*act
,
37 struct tcf_result
*res
)
39 struct tcf_bpf
*prog
= act
->priv
;
40 int action
, filter_res
;
41 bool at_ingress
= G_TC_AT(skb
->tc_verd
) & AT_INGRESS
;
43 if (unlikely(!skb_mac_header_was_set(skb
)))
46 spin_lock(&prog
->tcf_lock
);
48 prog
->tcf_tm
.lastuse
= jiffies
;
49 bstats_update(&prog
->tcf_bstats
, skb
);
51 /* Needed here for accessing maps. */
54 __skb_push(skb
, skb
->mac_len
);
55 filter_res
= BPF_PROG_RUN(prog
->filter
, skb
);
56 __skb_pull(skb
, skb
->mac_len
);
58 filter_res
= BPF_PROG_RUN(prog
->filter
, skb
);
62 /* A BPF program may overwrite the default action opcode.
63 * Similarly as in cls_bpf, if filter_res == -1 we use the
64 * default action specified from tc.
66 * In case a different well-known TC_ACT opcode has been
67 * returned, it will overwrite the default one.
69 * For everything else that is unkown, TC_ACT_UNSPEC is
74 case TC_ACT_RECLASSIFY
:
80 prog
->tcf_qstats
.drops
++;
83 action
= prog
->tcf_action
;
86 action
= TC_ACT_UNSPEC
;
90 spin_unlock(&prog
->tcf_lock
);
94 static bool tcf_bpf_is_ebpf(const struct tcf_bpf
*prog
)
96 return !prog
->bpf_ops
;
99 static int tcf_bpf_dump_bpf_info(const struct tcf_bpf
*prog
,
104 if (nla_put_u16(skb
, TCA_ACT_BPF_OPS_LEN
, prog
->bpf_num_ops
))
107 nla
= nla_reserve(skb
, TCA_ACT_BPF_OPS
, prog
->bpf_num_ops
*
108 sizeof(struct sock_filter
));
112 memcpy(nla_data(nla
), prog
->bpf_ops
, nla_len(nla
));
117 static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf
*prog
,
120 if (nla_put_u32(skb
, TCA_ACT_BPF_FD
, prog
->bpf_fd
))
123 if (prog
->bpf_name
&&
124 nla_put_string(skb
, TCA_ACT_BPF_NAME
, prog
->bpf_name
))
130 static int tcf_bpf_dump(struct sk_buff
*skb
, struct tc_action
*act
,
133 unsigned char *tp
= skb_tail_pointer(skb
);
134 struct tcf_bpf
*prog
= act
->priv
;
135 struct tc_act_bpf opt
= {
136 .index
= prog
->tcf_index
,
137 .refcnt
= prog
->tcf_refcnt
- ref
,
138 .bindcnt
= prog
->tcf_bindcnt
- bind
,
139 .action
= prog
->tcf_action
,
144 if (nla_put(skb
, TCA_ACT_BPF_PARMS
, sizeof(opt
), &opt
))
145 goto nla_put_failure
;
147 if (tcf_bpf_is_ebpf(prog
))
148 ret
= tcf_bpf_dump_ebpf_info(prog
, skb
);
150 ret
= tcf_bpf_dump_bpf_info(prog
, skb
);
152 goto nla_put_failure
;
154 tm
.install
= jiffies_to_clock_t(jiffies
- prog
->tcf_tm
.install
);
155 tm
.lastuse
= jiffies_to_clock_t(jiffies
- prog
->tcf_tm
.lastuse
);
156 tm
.expires
= jiffies_to_clock_t(prog
->tcf_tm
.expires
);
158 if (nla_put(skb
, TCA_ACT_BPF_TM
, sizeof(tm
), &tm
))
159 goto nla_put_failure
;
168 static const struct nla_policy act_bpf_policy
[TCA_ACT_BPF_MAX
+ 1] = {
169 [TCA_ACT_BPF_PARMS
] = { .len
= sizeof(struct tc_act_bpf
) },
170 [TCA_ACT_BPF_FD
] = { .type
= NLA_U32
},
171 [TCA_ACT_BPF_NAME
] = { .type
= NLA_NUL_STRING
, .len
= ACT_BPF_NAME_LEN
},
172 [TCA_ACT_BPF_OPS_LEN
] = { .type
= NLA_U16
},
173 [TCA_ACT_BPF_OPS
] = { .type
= NLA_BINARY
,
174 .len
= sizeof(struct sock_filter
) * BPF_MAXINSNS
},
177 static int tcf_bpf_init_from_ops(struct nlattr
**tb
, struct tcf_bpf_cfg
*cfg
)
179 struct sock_filter
*bpf_ops
;
180 struct sock_fprog_kern fprog_tmp
;
182 u16 bpf_size
, bpf_num_ops
;
185 bpf_num_ops
= nla_get_u16(tb
[TCA_ACT_BPF_OPS_LEN
]);
186 if (bpf_num_ops
> BPF_MAXINSNS
|| bpf_num_ops
== 0)
189 bpf_size
= bpf_num_ops
* sizeof(*bpf_ops
);
190 if (bpf_size
!= nla_len(tb
[TCA_ACT_BPF_OPS
]))
193 bpf_ops
= kzalloc(bpf_size
, GFP_KERNEL
);
197 memcpy(bpf_ops
, nla_data(tb
[TCA_ACT_BPF_OPS
]), bpf_size
);
199 fprog_tmp
.len
= bpf_num_ops
;
200 fprog_tmp
.filter
= bpf_ops
;
202 ret
= bpf_prog_create(&fp
, &fprog_tmp
);
208 cfg
->bpf_ops
= bpf_ops
;
209 cfg
->bpf_num_ops
= bpf_num_ops
;
211 cfg
->is_ebpf
= false;
216 static int tcf_bpf_init_from_efd(struct nlattr
**tb
, struct tcf_bpf_cfg
*cfg
)
222 bpf_fd
= nla_get_u32(tb
[TCA_ACT_BPF_FD
]);
224 fp
= bpf_prog_get(bpf_fd
);
228 if (fp
->type
!= BPF_PROG_TYPE_SCHED_ACT
) {
233 if (tb
[TCA_ACT_BPF_NAME
]) {
234 name
= kmemdup(nla_data(tb
[TCA_ACT_BPF_NAME
]),
235 nla_len(tb
[TCA_ACT_BPF_NAME
]),
243 cfg
->bpf_fd
= bpf_fd
;
244 cfg
->bpf_name
= name
;
251 static void tcf_bpf_cfg_cleanup(const struct tcf_bpf_cfg
*cfg
)
254 bpf_prog_put(cfg
->filter
);
256 bpf_prog_destroy(cfg
->filter
);
259 kfree(cfg
->bpf_name
);
262 static void tcf_bpf_prog_fill_cfg(const struct tcf_bpf
*prog
,
263 struct tcf_bpf_cfg
*cfg
)
265 cfg
->is_ebpf
= tcf_bpf_is_ebpf(prog
);
266 cfg
->filter
= prog
->filter
;
268 cfg
->bpf_ops
= prog
->bpf_ops
;
269 cfg
->bpf_name
= prog
->bpf_name
;
272 static int tcf_bpf_init(struct net
*net
, struct nlattr
*nla
,
273 struct nlattr
*est
, struct tc_action
*act
,
274 int replace
, int bind
)
276 struct nlattr
*tb
[TCA_ACT_BPF_MAX
+ 1];
277 struct tcf_bpf_cfg cfg
, old
;
278 struct tc_act_bpf
*parm
;
279 struct tcf_bpf
*prog
;
280 bool is_bpf
, is_ebpf
;
286 ret
= nla_parse_nested(tb
, TCA_ACT_BPF_MAX
, nla
, act_bpf_policy
);
290 is_bpf
= tb
[TCA_ACT_BPF_OPS_LEN
] && tb
[TCA_ACT_BPF_OPS
];
291 is_ebpf
= tb
[TCA_ACT_BPF_FD
];
293 if ((!is_bpf
&& !is_ebpf
) || (is_bpf
&& is_ebpf
) ||
294 !tb
[TCA_ACT_BPF_PARMS
])
297 parm
= nla_data(tb
[TCA_ACT_BPF_PARMS
]);
299 memset(&cfg
, 0, sizeof(cfg
));
301 ret
= is_bpf
? tcf_bpf_init_from_ops(tb
, &cfg
) :
302 tcf_bpf_init_from_efd(tb
, &cfg
);
306 if (!tcf_hash_check(parm
->index
, act
, bind
)) {
307 ret
= tcf_hash_create(parm
->index
, est
, act
,
308 sizeof(*prog
), bind
);
314 /* Don't override defaults. */
318 tcf_hash_release(act
, bind
);
326 spin_lock_bh(&prog
->tcf_lock
);
328 if (ret
!= ACT_P_CREATED
)
329 tcf_bpf_prog_fill_cfg(prog
, &old
);
331 prog
->bpf_ops
= cfg
.bpf_ops
;
332 prog
->bpf_name
= cfg
.bpf_name
;
335 prog
->bpf_num_ops
= cfg
.bpf_num_ops
;
337 prog
->bpf_fd
= cfg
.bpf_fd
;
339 prog
->tcf_action
= parm
->action
;
340 prog
->filter
= cfg
.filter
;
342 spin_unlock_bh(&prog
->tcf_lock
);
344 if (ret
== ACT_P_CREATED
)
345 tcf_hash_insert(act
);
347 tcf_bpf_cfg_cleanup(&old
);
352 tcf_bpf_cfg_cleanup(&cfg
);
356 static void tcf_bpf_cleanup(struct tc_action
*act
, int bind
)
358 struct tcf_bpf_cfg tmp
;
360 tcf_bpf_prog_fill_cfg(act
->priv
, &tmp
);
361 tcf_bpf_cfg_cleanup(&tmp
);
364 static struct tc_action_ops act_bpf_ops __read_mostly
= {
367 .owner
= THIS_MODULE
,
369 .dump
= tcf_bpf_dump
,
370 .cleanup
= tcf_bpf_cleanup
,
371 .init
= tcf_bpf_init
,
374 static int __init
bpf_init_module(void)
376 return tcf_register_action(&act_bpf_ops
, BPF_TAB_MASK
);
379 static void __exit
bpf_cleanup_module(void)
381 tcf_unregister_action(&act_bpf_ops
);
384 module_init(bpf_init_module
);
385 module_exit(bpf_cleanup_module
);
387 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
388 MODULE_DESCRIPTION("TC BPF based action");
389 MODULE_LICENSE("GPL v2");