net: sched: move the can_offload check from binding phase to rule insertion phase
[linux-2.6/btrfs-unstable.git] / drivers / net / ethernet / netronome / nfp / bpf / main.c
blob04424db24b80ce0831ad5caade363a1df9be3ff3
1 /*
2 * Copyright (C) 2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
34 #include <net/pkt_cls.h>
36 #include "../nfpcore/nfp_cpp.h"
37 #include "../nfp_app.h"
38 #include "../nfp_main.h"
39 #include "../nfp_net.h"
40 #include "../nfp_port.h"
41 #include "main.h"
43 static bool nfp_net_ebpf_capable(struct nfp_net *nn)
45 #ifdef __LITTLE_ENDIAN
46 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
47 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
48 return true;
49 #endif
50 return false;
53 static int
54 nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
55 struct bpf_prog *prog)
57 struct tc_cls_bpf_offload cmd = {
58 .prog = prog,
60 int ret;
62 if (!nfp_net_ebpf_capable(nn))
63 return -EINVAL;
65 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
66 if (!nn->dp.bpf_offload_xdp)
67 return prog ? -EBUSY : 0;
68 cmd.command = prog ? TC_CLSBPF_REPLACE : TC_CLSBPF_DESTROY;
69 } else {
70 if (!prog)
71 return 0;
72 cmd.command = TC_CLSBPF_ADD;
75 ret = nfp_net_bpf_offload(nn, &cmd);
76 /* Stop offload if replace not possible */
77 if (ret && cmd.command == TC_CLSBPF_REPLACE)
78 nfp_bpf_xdp_offload(app, nn, NULL);
79 nn->dp.bpf_offload_xdp = prog && !ret;
80 return ret;
83 static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
85 return nfp_net_ebpf_capable(nn) ? "BPF" : "";
88 static int
89 nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
91 struct nfp_net_bpf_priv *priv;
92 int ret;
94 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
95 if (!priv)
96 return -ENOMEM;
98 nn->app_priv = priv;
99 spin_lock_init(&priv->rx_filter_lock);
100 priv->nn = nn;
101 timer_setup(&priv->rx_filter_stats_timer,
102 nfp_net_filter_stats_timer, 0);
104 ret = nfp_app_nic_vnic_alloc(app, nn, id);
105 if (ret)
106 kfree(priv);
108 return ret;
111 static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
113 if (nn->dp.bpf_offload_xdp)
114 nfp_bpf_xdp_offload(app, nn, NULL);
115 kfree(nn->app_priv);
118 static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
119 void *type_data, void *cb_priv)
121 struct tc_cls_bpf_offload *cls_bpf = type_data;
122 struct nfp_net *nn = cb_priv;
124 if (!tc_can_offload(nn->dp.netdev))
125 return -EOPNOTSUPP;
127 switch (type) {
128 case TC_SETUP_CLSBPF:
129 if (!nfp_net_ebpf_capable(nn) ||
130 cls_bpf->common.protocol != htons(ETH_P_ALL) ||
131 cls_bpf->common.chain_index)
132 return -EOPNOTSUPP;
133 return nfp_net_bpf_offload(nn, cls_bpf);
134 default:
135 return -EOPNOTSUPP;
139 static int nfp_bpf_setup_tc_block(struct net_device *netdev,
140 struct tc_block_offload *f)
142 struct nfp_net *nn = netdev_priv(netdev);
144 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
145 return -EOPNOTSUPP;
147 switch (f->command) {
148 case TC_BLOCK_BIND:
149 return tcf_block_cb_register(f->block,
150 nfp_bpf_setup_tc_block_cb,
151 nn, nn);
152 case TC_BLOCK_UNBIND:
153 tcf_block_cb_unregister(f->block,
154 nfp_bpf_setup_tc_block_cb,
155 nn);
156 return 0;
157 default:
158 return -EOPNOTSUPP;
162 static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
163 enum tc_setup_type type, void *type_data)
165 switch (type) {
166 case TC_SETUP_BLOCK:
167 return nfp_bpf_setup_tc_block(netdev, type_data);
168 default:
169 return -EOPNOTSUPP;
173 static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
175 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
178 const struct nfp_app_type app_bpf = {
179 .id = NFP_APP_BPF_NIC,
180 .name = "ebpf",
182 .extra_cap = nfp_bpf_extra_cap,
184 .vnic_alloc = nfp_bpf_vnic_alloc,
185 .vnic_free = nfp_bpf_vnic_free,
187 .setup_tc = nfp_bpf_setup_tc,
188 .tc_busy = nfp_bpf_tc_busy,
189 .xdp_offload = nfp_bpf_xdp_offload,