blob: 61898dda11cf5a8077fa7603c8de48a24f6cf2da [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 Kicinskie3ac6c02018-02-07 20:55:22 -080038#include "../nfpcore/nfp_nsp.h"
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070039#include "../nfp_app.h"
40#include "../nfp_main.h"
41#include "../nfp_net.h"
42#include "../nfp_port.h"
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -080043#include "fw.h"
Jakub Kicinskibb45e512017-05-31 08:06:49 -070044#include "main.h"
45
46static bool nfp_net_ebpf_capable(struct nfp_net *nn)
47{
Jakub Kicinski0f6cf4d2017-10-12 10:34:13 -070048#ifdef __LITTLE_ENDIAN
Jakub Kicinskibb45e512017-05-31 08:06:49 -070049 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
50 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
51 return true;
Jakub Kicinski0f6cf4d2017-10-12 10:34:13 -070052#endif
Jakub Kicinskibb45e512017-05-31 08:06:49 -070053 return false;
54}
55
56static int
57nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
Quentin Monnetacc2abbb2018-01-19 17:44:49 -080058 struct bpf_prog *prog, struct netlink_ext_ack *extack)
Jakub Kicinskibb45e512017-05-31 08:06:49 -070059{
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070060 bool running, xdp_running;
Jakub Kicinskibb45e512017-05-31 08:06:49 -070061 int ret;
62
63 if (!nfp_net_ebpf_capable(nn))
64 return -EINVAL;
65
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070066 running = nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
67 xdp_running = running && nn->dp.bpf_offload_xdp;
Jakub Kicinskibb45e512017-05-31 08:06:49 -070068
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070069 if (!prog && !xdp_running)
70 return 0;
71 if (prog && running && !xdp_running)
72 return -EBUSY;
73
Quentin Monnet52be9a72018-01-19 17:44:50 -080074 ret = nfp_net_bpf_offload(nn, prog, running, extack);
Jakub Kicinskibb45e512017-05-31 08:06:49 -070075 /* Stop offload if replace not possible */
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070076 if (ret && prog)
Quentin Monnetacc2abbb2018-01-19 17:44:49 -080077 nfp_bpf_xdp_offload(app, nn, NULL, extack);
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070078
Jakub Kicinskibb45e512017-05-31 08:06:49 -070079 nn->dp.bpf_offload_xdp = prog && !ret;
80 return ret;
81}
82
83static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
84{
85 return nfp_net_ebpf_capable(nn) ? "BPF" : "";
86}
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070087
Jakub Kicinski4f834352017-12-27 15:36:49 -080088static int
89nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
90{
Jakub Kicinskie3ac6c02018-02-07 20:55:22 -080091 struct nfp_pf *pf = app->pf;
Jakub Kicinski2314fe92018-01-10 12:26:01 +000092 struct nfp_bpf_vnic *bv;
Jakub Kicinski4f834352017-12-27 15:36:49 -080093 int err;
94
Jakub Kicinskie3ac6c02018-02-07 20:55:22 -080095 if (!pf->eth_tbl) {
96 nfp_err(pf->cpp, "No ETH table\n");
97 return -EINVAL;
98 }
99 if (pf->max_data_vnics != pf->eth_tbl->count) {
100 nfp_err(pf->cpp, "ETH entries don't match vNICs (%d vs %d)\n",
101 pf->max_data_vnics, pf->eth_tbl->count);
102 return -EINVAL;
103 }
104
Jakub Kicinski2314fe92018-01-10 12:26:01 +0000105 bv = kzalloc(sizeof(*bv), GFP_KERNEL);
106 if (!bv)
Jakub Kicinski4f834352017-12-27 15:36:49 -0800107 return -ENOMEM;
Jakub Kicinski2314fe92018-01-10 12:26:01 +0000108 nn->app_priv = bv;
Jakub Kicinski4f834352017-12-27 15:36:49 -0800109
110 err = nfp_app_nic_vnic_alloc(app, nn, id);
111 if (err)
112 goto err_free_priv;
113
Jakub Kicinski2314fe92018-01-10 12:26:01 +0000114 bv->start_off = nn_readw(nn, NFP_NET_CFG_BPF_START);
115 bv->tgt_done = nn_readw(nn, NFP_NET_CFG_BPF_DONE);
116
Jakub Kicinski4f834352017-12-27 15:36:49 -0800117 return 0;
118err_free_priv:
119 kfree(nn->app_priv);
120 return err;
121}
122
123static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
124{
125 struct nfp_bpf_vnic *bv = nn->app_priv;
126
127 WARN_ON(bv->tc_prog);
128 kfree(bv);
129}
130
Jiri Pirko90d97312017-10-19 15:50:44 +0200131static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
132 void *type_data, void *cb_priv)
133{
134 struct tc_cls_bpf_offload *cls_bpf = type_data;
135 struct nfp_net *nn = cb_priv;
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800136 struct bpf_prog *oldprog;
137 struct nfp_bpf_vnic *bv;
138 int err;
Jiri Pirko90d97312017-10-19 15:50:44 +0200139
Quentin Monnet52be9a72018-01-19 17:44:50 -0800140 if (type != TC_SETUP_CLSBPF) {
141 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
142 "only offload of BPF classifiers supported");
143 return -EOPNOTSUPP;
144 }
Jakub Kicinski3107fdc2018-01-25 14:00:45 -0800145 if (!tc_cls_can_offload_and_chain0(nn->dp.netdev, &cls_bpf->common))
Quentin Monnet52be9a72018-01-19 17:44:50 -0800146 return -EOPNOTSUPP;
147 if (!nfp_net_ebpf_capable(nn)) {
148 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
149 "NFP firmware does not support eBPF offload");
150 return -EOPNOTSUPP;
151 }
152 if (cls_bpf->common.protocol != htons(ETH_P_ALL)) {
153 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
154 "only ETH_P_ALL supported as filter protocol");
155 return -EOPNOTSUPP;
156 }
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100157
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700158 /* Only support TC direct action */
159 if (!cls_bpf->exts_integrated ||
160 tcf_exts_has_actions(cls_bpf->exts)) {
Quentin Monnet52be9a72018-01-19 17:44:50 -0800161 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
162 "only direct action with no legacy actions supported");
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700163 return -EOPNOTSUPP;
164 }
Jakub Kicinskif4496572017-11-02 01:31:31 -0700165
Jakub Kicinski102740b2017-12-19 13:32:13 -0800166 if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
Jiri Pirko90d97312017-10-19 15:50:44 +0200167 return -EOPNOTSUPP;
Jakub Kicinski102740b2017-12-19 13:32:13 -0800168
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800169 bv = nn->app_priv;
170 oldprog = cls_bpf->oldprog;
171
172 /* Don't remove if oldprog doesn't match driver's state */
173 if (bv->tc_prog != oldprog) {
174 oldprog = NULL;
175 if (!cls_bpf->prog)
176 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200177 }
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800178
Quentin Monnet52be9a72018-01-19 17:44:50 -0800179 err = nfp_net_bpf_offload(nn, cls_bpf->prog, oldprog,
180 cls_bpf->common.extack);
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800181 if (err)
182 return err;
183
184 bv->tc_prog = cls_bpf->prog;
185 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200186}
187
188static int nfp_bpf_setup_tc_block(struct net_device *netdev,
189 struct tc_block_offload *f)
190{
191 struct nfp_net *nn = netdev_priv(netdev);
192
193 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
194 return -EOPNOTSUPP;
195
196 switch (f->command) {
197 case TC_BLOCK_BIND:
198 return tcf_block_cb_register(f->block,
199 nfp_bpf_setup_tc_block_cb,
200 nn, nn);
201 case TC_BLOCK_UNBIND:
202 tcf_block_cb_unregister(f->block,
203 nfp_bpf_setup_tc_block_cb,
204 nn);
205 return 0;
206 default:
207 return -EOPNOTSUPP;
208 }
209}
210
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700211static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200212 enum tc_setup_type type, void *type_data)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700213{
Jiri Pirko90d97312017-10-19 15:50:44 +0200214 switch (type) {
Jiri Pirko90d97312017-10-19 15:50:44 +0200215 case TC_SETUP_BLOCK:
216 return nfp_bpf_setup_tc_block(netdev, type_data);
217 default:
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700218 return -EOPNOTSUPP;
Jiri Pirko90d97312017-10-19 15:50:44 +0200219 }
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700220}
221
222static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
223{
Jakub Kicinskia0f30c92018-01-10 12:25:58 +0000224 struct nfp_bpf_vnic *bv = nn->app_priv;
225
226 return !!bv->tc_prog;
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700227}
228
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800229static int
Jakub Kicinskiccbdc592018-01-10 12:25:57 +0000230nfp_bpf_change_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
231{
232 struct nfp_net *nn = netdev_priv(netdev);
233 unsigned int max_mtu;
234
235 if (~nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
236 return 0;
237
238 max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
239 if (new_mtu > max_mtu) {
240 nn_info(nn, "BPF offload active, MTU over %u not supported\n",
241 max_mtu);
242 return -EBUSY;
243 }
244 return 0;
245}
246
247static int
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800248nfp_bpf_parse_cap_adjust_head(struct nfp_app_bpf *bpf, void __iomem *value,
249 u32 length)
250{
251 struct nfp_bpf_cap_tlv_adjust_head __iomem *cap = value;
252 struct nfp_cpp *cpp = bpf->app->pf->cpp;
253
254 if (length < sizeof(*cap)) {
255 nfp_err(cpp, "truncated adjust_head TLV: %d\n", length);
256 return -EINVAL;
257 }
258
259 bpf->adjust_head.flags = readl(&cap->flags);
260 bpf->adjust_head.off_min = readl(&cap->off_min);
261 bpf->adjust_head.off_max = readl(&cap->off_max);
Jakub Kicinski8231f842017-12-14 21:29:19 -0800262 bpf->adjust_head.guaranteed_sub = readl(&cap->guaranteed_sub);
263 bpf->adjust_head.guaranteed_add = readl(&cap->guaranteed_add);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800264
265 if (bpf->adjust_head.off_min > bpf->adjust_head.off_max) {
266 nfp_err(cpp, "invalid adjust_head TLV: min > max\n");
267 return -EINVAL;
268 }
269 if (!FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_min) ||
270 !FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_max)) {
271 nfp_warn(cpp, "disabling adjust_head - driver expects min/max to fit in as immediates\n");
272 memset(&bpf->adjust_head, 0, sizeof(bpf->adjust_head));
273 return 0;
274 }
275
276 return 0;
277}
278
Jakub Kicinski9d080d52018-01-11 20:29:13 -0800279static int
280nfp_bpf_parse_cap_func(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
281{
282 struct nfp_bpf_cap_tlv_func __iomem *cap = value;
283
284 if (length < sizeof(*cap)) {
285 nfp_err(bpf->app->cpp, "truncated function TLV: %d\n", length);
286 return -EINVAL;
287 }
288
289 switch (readl(&cap->func_id)) {
290 case BPF_FUNC_map_lookup_elem:
291 bpf->helpers.map_lookup = readl(&cap->func_addr);
292 break;
293 }
294
295 return 0;
296}
297
298static int
299nfp_bpf_parse_cap_maps(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
300{
301 struct nfp_bpf_cap_tlv_maps __iomem *cap = value;
302
303 if (length < sizeof(*cap)) {
304 nfp_err(bpf->app->cpp, "truncated maps TLV: %d\n", length);
305 return -EINVAL;
306 }
307
308 bpf->maps.types = readl(&cap->types);
309 bpf->maps.max_maps = readl(&cap->max_maps);
310 bpf->maps.max_elems = readl(&cap->max_elems);
311 bpf->maps.max_key_sz = readl(&cap->max_key_sz);
312 bpf->maps.max_val_sz = readl(&cap->max_val_sz);
313 bpf->maps.max_elem_sz = readl(&cap->max_elem_sz);
314
315 return 0;
316}
317
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800318static int nfp_bpf_parse_capabilities(struct nfp_app *app)
319{
320 struct nfp_cpp *cpp = app->pf->cpp;
321 struct nfp_cpp_area *area;
322 u8 __iomem *mem, *start;
323
324 mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
325 8, &area);
326 if (IS_ERR(mem))
327 return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
328
329 start = mem;
330 while (mem - start + 8 < nfp_cpp_area_size(area)) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800331 u8 __iomem *value;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800332 u32 type, length;
333
334 type = readl(mem);
335 length = readl(mem + 4);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800336 value = mem + 8;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800337
338 mem += 8 + length;
339 if (mem - start > nfp_cpp_area_size(area))
340 goto err_release_free;
341
342 switch (type) {
Jakub Kicinski9d080d52018-01-11 20:29:13 -0800343 case NFP_BPF_CAP_TYPE_FUNC:
344 if (nfp_bpf_parse_cap_func(app->priv, value, length))
345 goto err_release_free;
346 break;
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800347 case NFP_BPF_CAP_TYPE_ADJUST_HEAD:
348 if (nfp_bpf_parse_cap_adjust_head(app->priv, value,
349 length))
350 goto err_release_free;
351 break;
Jakub Kicinski9d080d52018-01-11 20:29:13 -0800352 case NFP_BPF_CAP_TYPE_MAPS:
353 if (nfp_bpf_parse_cap_maps(app->priv, value, length))
354 goto err_release_free;
355 break;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800356 default:
357 nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
358 break;
359 }
360 }
361 if (mem - start != nfp_cpp_area_size(area)) {
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800362 nfp_err(cpp, "BPF capabilities left after parsing, parsed:%zd total length:%zu\n",
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800363 mem - start, nfp_cpp_area_size(area));
364 goto err_release_free;
365 }
366
367 nfp_cpp_area_release_free(area);
368
369 return 0;
370
371err_release_free:
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800372 nfp_err(cpp, "invalid BPF capabilities at offset:%zd\n", mem - start);
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800373 nfp_cpp_area_release_free(area);
374 return -EINVAL;
375}
376
377static int nfp_bpf_init(struct nfp_app *app)
378{
379 struct nfp_app_bpf *bpf;
380 int err;
381
382 bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
383 if (!bpf)
384 return -ENOMEM;
385 bpf->app = app;
386 app->priv = bpf;
387
Jakub Kicinskid48ae232018-01-11 20:29:11 -0800388 skb_queue_head_init(&bpf->cmsg_replies);
389 init_waitqueue_head(&bpf->cmsg_wq);
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800390 INIT_LIST_HEAD(&bpf->map_list);
391
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800392 err = nfp_bpf_parse_capabilities(app);
393 if (err)
394 goto err_free_bpf;
395
396 return 0;
397
398err_free_bpf:
399 kfree(bpf);
400 return err;
401}
402
403static void nfp_bpf_clean(struct nfp_app *app)
404{
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800405 struct nfp_app_bpf *bpf = app->priv;
406
Jakub Kicinskid48ae232018-01-11 20:29:11 -0800407 WARN_ON(!skb_queue_empty(&bpf->cmsg_replies));
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800408 WARN_ON(!list_empty(&bpf->map_list));
Jakub Kicinski1bba4c42018-01-11 20:29:17 -0800409 WARN_ON(bpf->maps_in_use || bpf->map_elems_in_use);
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800410 kfree(bpf);
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800411}
412
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700413const struct nfp_app_type app_bpf = {
414 .id = NFP_APP_BPF_NIC,
Jakub Kicinski2707d6f2017-05-31 08:06:47 -0700415 .name = "ebpf",
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700416
Jakub Kicinski81bd5ded2018-01-17 18:51:06 -0800417 .ctrl_cap_mask = 0,
Jakub Kicinski78a0a652018-01-17 18:51:05 -0800418
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800419 .init = nfp_bpf_init,
420 .clean = nfp_bpf_clean,
421
Jakub Kicinskiccbdc592018-01-10 12:25:57 +0000422 .change_mtu = nfp_bpf_change_mtu,
423
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700424 .extra_cap = nfp_bpf_extra_cap,
425
Jakub Kicinski4f834352017-12-27 15:36:49 -0800426 .vnic_alloc = nfp_bpf_vnic_alloc,
427 .vnic_free = nfp_bpf_vnic_free,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700428
Jakub Kicinskid48ae232018-01-11 20:29:11 -0800429 .ctrl_msg_rx = nfp_bpf_ctrl_msg_rx,
430
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700431 .setup_tc = nfp_bpf_setup_tc,
432 .tc_busy = nfp_bpf_tc_busy,
Jakub Kicinskiaf93d152018-01-10 12:26:04 +0000433 .bpf = nfp_ndo_bpf,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700434 .xdp_offload = nfp_bpf_xdp_offload,
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700435};