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;
42 if (unlikely(!skb_mac_header_was_set(skb)))
45 spin_lock(&prog->tcf_lock);
47 prog->tcf_tm.lastuse = jiffies;
48 bstats_update(&prog->tcf_bstats, skb);
50 /* Needed here for accessing maps. */
52 filter_res = BPF_PROG_RUN(prog->filter, skb);
55 /* A BPF program may overwrite the default action opcode.
56 * Similarly as in cls_bpf, if filter_res == -1 we use the
57 * default action specified from tc.
59 * In case a different well-known TC_ACT opcode has been
60 * returned, it will overwrite the default one.
62 * For everything else that is unkown, TC_ACT_UNSPEC is
67 case TC_ACT_RECLASSIFY:
73 prog->tcf_qstats.drops++;
76 action = prog->tcf_action;
79 action = TC_ACT_UNSPEC;
83 spin_unlock(&prog->tcf_lock);
87 static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
89 return !prog->bpf_ops;
92 static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog,
97 if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops))
100 nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops *
101 sizeof(struct sock_filter));
105 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
110 static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog,
113 if (nla_put_u32(skb, TCA_ACT_BPF_FD, prog->bpf_fd))
116 if (prog->bpf_name &&
117 nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name))
123 static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
126 unsigned char *tp = skb_tail_pointer(skb);
127 struct tcf_bpf *prog = act->priv;
128 struct tc_act_bpf opt = {
129 .index = prog->tcf_index,
130 .refcnt = prog->tcf_refcnt - ref,
131 .bindcnt = prog->tcf_bindcnt - bind,
132 .action = prog->tcf_action,
137 if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
138 goto nla_put_failure;
140 if (tcf_bpf_is_ebpf(prog))
141 ret = tcf_bpf_dump_ebpf_info(prog, skb);
143 ret = tcf_bpf_dump_bpf_info(prog, skb);
145 goto nla_put_failure;
147 tm.install = jiffies_to_clock_t(jiffies - prog->tcf_tm.install);
148 tm.lastuse = jiffies_to_clock_t(jiffies - prog->tcf_tm.lastuse);
149 tm.expires = jiffies_to_clock_t(prog->tcf_tm.expires);
151 if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm))
152 goto nla_put_failure;
161 static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
162 [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) },
163 [TCA_ACT_BPF_FD] = { .type = NLA_U32 },
164 [TCA_ACT_BPF_NAME] = { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN },
165 [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 },
166 [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY,
167 .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
170 static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
172 struct sock_filter *bpf_ops;
173 struct sock_fprog_kern fprog_tmp;
175 u16 bpf_size, bpf_num_ops;
178 bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
179 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
182 bpf_size = bpf_num_ops * sizeof(*bpf_ops);
183 if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
186 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
190 memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
192 fprog_tmp.len = bpf_num_ops;
193 fprog_tmp.filter = bpf_ops;
195 ret = bpf_prog_create(&fp, &fprog_tmp);
201 cfg->bpf_ops = bpf_ops;
202 cfg->bpf_num_ops = bpf_num_ops;
204 cfg->is_ebpf = false;
209 static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
215 bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]);
217 fp = bpf_prog_get(bpf_fd);
221 if (fp->type != BPF_PROG_TYPE_SCHED_ACT) {
226 if (tb[TCA_ACT_BPF_NAME]) {
227 name = kmemdup(nla_data(tb[TCA_ACT_BPF_NAME]),
228 nla_len(tb[TCA_ACT_BPF_NAME]),
236 cfg->bpf_fd = bpf_fd;
237 cfg->bpf_name = name;
244 static void tcf_bpf_cfg_cleanup(const struct tcf_bpf_cfg *cfg)
247 bpf_prog_put(cfg->filter);
249 bpf_prog_destroy(cfg->filter);
252 kfree(cfg->bpf_name);
255 static void tcf_bpf_prog_fill_cfg(const struct tcf_bpf *prog,
256 struct tcf_bpf_cfg *cfg)
258 cfg->is_ebpf = tcf_bpf_is_ebpf(prog);
259 cfg->filter = prog->filter;
261 cfg->bpf_ops = prog->bpf_ops;
262 cfg->bpf_name = prog->bpf_name;
265 static int tcf_bpf_init(struct net *net, struct nlattr *nla,
266 struct nlattr *est, struct tc_action *act,
267 int replace, int bind)
269 struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
270 struct tcf_bpf_cfg cfg, old;
271 struct tc_act_bpf *parm;
272 struct tcf_bpf *prog;
273 bool is_bpf, is_ebpf;
279 ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
283 is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
284 is_ebpf = tb[TCA_ACT_BPF_FD];
286 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) ||
287 !tb[TCA_ACT_BPF_PARMS])
290 parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
292 memset(&cfg, 0, sizeof(cfg));
294 ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
295 tcf_bpf_init_from_efd(tb, &cfg);
299 if (!tcf_hash_check(parm->index, act, bind)) {
300 ret = tcf_hash_create(parm->index, est, act,
301 sizeof(*prog), bind);
307 /* Don't override defaults. */
311 tcf_hash_release(act, bind);
319 spin_lock_bh(&prog->tcf_lock);
321 if (ret != ACT_P_CREATED)
322 tcf_bpf_prog_fill_cfg(prog, &old);
324 prog->bpf_ops = cfg.bpf_ops;
325 prog->bpf_name = cfg.bpf_name;
328 prog->bpf_num_ops = cfg.bpf_num_ops;
330 prog->bpf_fd = cfg.bpf_fd;
332 prog->tcf_action = parm->action;
333 prog->filter = cfg.filter;
335 spin_unlock_bh(&prog->tcf_lock);
337 if (ret == ACT_P_CREATED)
338 tcf_hash_insert(act);
340 tcf_bpf_cfg_cleanup(&old);
345 tcf_bpf_cfg_cleanup(&cfg);
349 static void tcf_bpf_cleanup(struct tc_action *act, int bind)
351 struct tcf_bpf_cfg tmp;
353 tcf_bpf_prog_fill_cfg(act->priv, &tmp);
354 tcf_bpf_cfg_cleanup(&tmp);
357 static struct tc_action_ops act_bpf_ops __read_mostly = {
360 .owner = THIS_MODULE,
362 .dump = tcf_bpf_dump,
363 .cleanup = tcf_bpf_cleanup,
364 .init = tcf_bpf_init,
367 static int __init bpf_init_module(void)
369 return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
372 static void __exit bpf_cleanup_module(void)
374 tcf_unregister_action(&act_bpf_ops);
377 module_init(bpf_init_module);
378 module_exit(bpf_cleanup_module);
380 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
381 MODULE_DESCRIPTION("TC BPF based action");
382 MODULE_LICENSE("GPL v2");