blob: f76bb40c20bc698362cf153b80c76ac293d1e4ba [file] [log] [blame]
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -07001/*
2 * Copyright (C) 2017 Netronome Systems, Inc.
3 *
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.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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.
32 */
33
Jakub Kicinskibb45e512017-05-31 08:06:49 -070034#include <net/pkt_cls.h>
35
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070036#include "../nfpcore/nfp_cpp.h"
Jakub Kicinski77a844e2017-12-14 21:29:16 -080037#include "../nfpcore/nfp_nffw.h"
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070038#include "../nfp_app.h"
39#include "../nfp_main.h"
40#include "../nfp_net.h"
41#include "../nfp_port.h"
Jakub Kicinskibb45e512017-05-31 08:06:49 -070042#include "main.h"
43
44static bool nfp_net_ebpf_capable(struct nfp_net *nn)
45{
Jakub Kicinski0f6cf4d2017-10-12 10:34:13 -070046#ifdef __LITTLE_ENDIAN
Jakub Kicinskibb45e512017-05-31 08:06:49 -070047 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
48 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
49 return true;
Jakub Kicinski0f6cf4d2017-10-12 10:34:13 -070050#endif
Jakub Kicinskibb45e512017-05-31 08:06:49 -070051 return false;
52}
53
54static int
55nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
56 struct bpf_prog *prog)
57{
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070058 bool running, xdp_running;
Jakub Kicinskibb45e512017-05-31 08:06:49 -070059 int ret;
60
61 if (!nfp_net_ebpf_capable(nn))
62 return -EINVAL;
63
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070064 running = nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
65 xdp_running = running && nn->dp.bpf_offload_xdp;
Jakub Kicinskibb45e512017-05-31 08:06:49 -070066
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070067 if (!prog && !xdp_running)
68 return 0;
69 if (prog && running && !xdp_running)
70 return -EBUSY;
71
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -070072 ret = nfp_net_bpf_offload(nn, prog, running);
Jakub Kicinskibb45e512017-05-31 08:06:49 -070073 /* Stop offload if replace not possible */
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070074 if (ret && prog)
Jakub Kicinskibb45e512017-05-31 08:06:49 -070075 nfp_bpf_xdp_offload(app, nn, NULL);
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070076
Jakub Kicinskibb45e512017-05-31 08:06:49 -070077 nn->dp.bpf_offload_xdp = prog && !ret;
78 return ret;
79}
80
81static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
82{
83 return nfp_net_ebpf_capable(nn) ? "BPF" : "";
84}
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070085
Jiri Pirko90d97312017-10-19 15:50:44 +020086static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
87 void *type_data, void *cb_priv)
88{
89 struct tc_cls_bpf_offload *cls_bpf = type_data;
90 struct nfp_net *nn = cb_priv;
91
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070092 if (type != TC_SETUP_CLSBPF ||
93 !tc_can_offload(nn->dp.netdev) ||
94 !nfp_net_ebpf_capable(nn) ||
95 cls_bpf->common.protocol != htons(ETH_P_ALL) ||
96 cls_bpf->common.chain_index)
Jiri Pirko44ae12a2017-11-01 11:47:39 +010097 return -EOPNOTSUPP;
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070098 if (nn->dp.bpf_offload_xdp)
99 return -EBUSY;
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100100
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700101 /* Only support TC direct action */
102 if (!cls_bpf->exts_integrated ||
103 tcf_exts_has_actions(cls_bpf->exts)) {
104 nn_err(nn, "only direct action with no legacy actions supported\n");
105 return -EOPNOTSUPP;
106 }
Jakub Kicinskif4496572017-11-02 01:31:31 -0700107
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700108 switch (cls_bpf->command) {
109 case TC_CLSBPF_REPLACE:
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700110 return nfp_net_bpf_offload(nn, cls_bpf->prog, true);
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700111 case TC_CLSBPF_ADD:
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700112 return nfp_net_bpf_offload(nn, cls_bpf->prog, false);
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700113 case TC_CLSBPF_DESTROY:
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700114 return nfp_net_bpf_offload(nn, NULL, true);
Jiri Pirko90d97312017-10-19 15:50:44 +0200115 default:
116 return -EOPNOTSUPP;
117 }
118}
119
120static int nfp_bpf_setup_tc_block(struct net_device *netdev,
121 struct tc_block_offload *f)
122{
123 struct nfp_net *nn = netdev_priv(netdev);
124
125 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
126 return -EOPNOTSUPP;
127
128 switch (f->command) {
129 case TC_BLOCK_BIND:
130 return tcf_block_cb_register(f->block,
131 nfp_bpf_setup_tc_block_cb,
132 nn, nn);
133 case TC_BLOCK_UNBIND:
134 tcf_block_cb_unregister(f->block,
135 nfp_bpf_setup_tc_block_cb,
136 nn);
137 return 0;
138 default:
139 return -EOPNOTSUPP;
140 }
141}
142
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700143static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200144 enum tc_setup_type type, void *type_data)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700145{
Jiri Pirko90d97312017-10-19 15:50:44 +0200146 switch (type) {
Jiri Pirko90d97312017-10-19 15:50:44 +0200147 case TC_SETUP_BLOCK:
148 return nfp_bpf_setup_tc_block(netdev, type_data);
149 default:
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700150 return -EOPNOTSUPP;
Jiri Pirko90d97312017-10-19 15:50:44 +0200151 }
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700152}
153
154static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
155{
156 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
157}
158
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800159static int nfp_bpf_parse_capabilities(struct nfp_app *app)
160{
161 struct nfp_cpp *cpp = app->pf->cpp;
162 struct nfp_cpp_area *area;
163 u8 __iomem *mem, *start;
164
165 mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
166 8, &area);
167 if (IS_ERR(mem))
168 return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
169
170 start = mem;
171 while (mem - start + 8 < nfp_cpp_area_size(area)) {
172 u32 type, length;
173
174 type = readl(mem);
175 length = readl(mem + 4);
176
177 mem += 8 + length;
178 if (mem - start > nfp_cpp_area_size(area))
179 goto err_release_free;
180
181 switch (type) {
182 default:
183 nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
184 break;
185 }
186 }
187 if (mem - start != nfp_cpp_area_size(area)) {
188 nfp_err(cpp, "BPF capabilities left after parsing, parsed:%lu total length:%lu\n",
189 mem - start, nfp_cpp_area_size(area));
190 goto err_release_free;
191 }
192
193 nfp_cpp_area_release_free(area);
194
195 return 0;
196
197err_release_free:
198 nfp_err(cpp, "invalid BPF capabilities at offset:%ld\n", mem - start);
199 nfp_cpp_area_release_free(area);
200 return -EINVAL;
201}
202
203static int nfp_bpf_init(struct nfp_app *app)
204{
205 struct nfp_app_bpf *bpf;
206 int err;
207
208 bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
209 if (!bpf)
210 return -ENOMEM;
211 bpf->app = app;
212 app->priv = bpf;
213
214 err = nfp_bpf_parse_capabilities(app);
215 if (err)
216 goto err_free_bpf;
217
218 return 0;
219
220err_free_bpf:
221 kfree(bpf);
222 return err;
223}
224
225static void nfp_bpf_clean(struct nfp_app *app)
226{
227 kfree(app->priv);
228}
229
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700230const struct nfp_app_type app_bpf = {
231 .id = NFP_APP_BPF_NIC,
Jakub Kicinski2707d6f2017-05-31 08:06:47 -0700232 .name = "ebpf",
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700233
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800234 .init = nfp_bpf_init,
235 .clean = nfp_bpf_clean,
236
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700237 .extra_cap = nfp_bpf_extra_cap,
238
Jakub Kicinski012bb8a2017-11-03 13:56:22 -0700239 .vnic_alloc = nfp_app_nic_vnic_alloc,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700240
241 .setup_tc = nfp_bpf_setup_tc,
242 .tc_busy = nfp_bpf_tc_busy,
243 .xdp_offload = nfp_bpf_xdp_offload,
Jakub Kicinskic6c580d2017-11-03 13:56:29 -0700244
245 .bpf_verifier_prep = nfp_bpf_verifier_prep,
246 .bpf_translate = nfp_bpf_translate,
247 .bpf_destroy = nfp_bpf_destroy,
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700248};