blob: b3206855535a8210bd7bc8d1549db07d98536646 [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 Kicinski0d49eaf2017-12-14 21:29:18 -080042#include "fw.h"
Jakub Kicinskibb45e512017-05-31 08:06:49 -070043#include "main.h"
44
45static bool nfp_net_ebpf_capable(struct nfp_net *nn)
46{
Jakub Kicinski0f6cf4d2017-10-12 10:34:13 -070047#ifdef __LITTLE_ENDIAN
Jakub Kicinskibb45e512017-05-31 08:06:49 -070048 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
49 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
50 return true;
Jakub Kicinski0f6cf4d2017-10-12 10:34:13 -070051#endif
Jakub Kicinskibb45e512017-05-31 08:06:49 -070052 return false;
53}
54
55static int
56nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
Quentin Monnetacc2abbb2018-01-19 17:44:49 -080057 struct bpf_prog *prog, struct netlink_ext_ack *extack)
Jakub Kicinskibb45e512017-05-31 08:06:49 -070058{
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070059 bool running, xdp_running;
Jakub Kicinskibb45e512017-05-31 08:06:49 -070060 int ret;
61
62 if (!nfp_net_ebpf_capable(nn))
63 return -EINVAL;
64
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070065 running = nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
66 xdp_running = running && nn->dp.bpf_offload_xdp;
Jakub Kicinskibb45e512017-05-31 08:06:49 -070067
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070068 if (!prog && !xdp_running)
69 return 0;
70 if (prog && running && !xdp_running)
71 return -EBUSY;
72
Quentin Monnet52be9a72018-01-19 17:44:50 -080073 ret = nfp_net_bpf_offload(nn, prog, running, extack);
Jakub Kicinskibb45e512017-05-31 08:06:49 -070074 /* Stop offload if replace not possible */
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070075 if (ret && prog)
Quentin Monnetacc2abbb2018-01-19 17:44:49 -080076 nfp_bpf_xdp_offload(app, nn, NULL, extack);
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070077
Jakub Kicinskibb45e512017-05-31 08:06:49 -070078 nn->dp.bpf_offload_xdp = prog && !ret;
79 return ret;
80}
81
82static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
83{
84 return nfp_net_ebpf_capable(nn) ? "BPF" : "";
85}
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070086
Jakub Kicinski4f834352017-12-27 15:36:49 -080087static int
88nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
89{
Jakub Kicinski2314fe92018-01-10 12:26:01 +000090 struct nfp_bpf_vnic *bv;
Jakub Kicinski4f834352017-12-27 15:36:49 -080091 int err;
92
Jakub Kicinski2314fe92018-01-10 12:26:01 +000093 bv = kzalloc(sizeof(*bv), GFP_KERNEL);
94 if (!bv)
Jakub Kicinski4f834352017-12-27 15:36:49 -080095 return -ENOMEM;
Jakub Kicinski2314fe92018-01-10 12:26:01 +000096 nn->app_priv = bv;
Jakub Kicinski4f834352017-12-27 15:36:49 -080097
98 err = nfp_app_nic_vnic_alloc(app, nn, id);
99 if (err)
100 goto err_free_priv;
101
Jakub Kicinski2314fe92018-01-10 12:26:01 +0000102 bv->start_off = nn_readw(nn, NFP_NET_CFG_BPF_START);
103 bv->tgt_done = nn_readw(nn, NFP_NET_CFG_BPF_DONE);
104
Jakub Kicinski4f834352017-12-27 15:36:49 -0800105 return 0;
106err_free_priv:
107 kfree(nn->app_priv);
108 return err;
109}
110
111static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
112{
113 struct nfp_bpf_vnic *bv = nn->app_priv;
114
115 WARN_ON(bv->tc_prog);
116 kfree(bv);
117}
118
Jiri Pirko90d97312017-10-19 15:50:44 +0200119static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
120 void *type_data, void *cb_priv)
121{
122 struct tc_cls_bpf_offload *cls_bpf = type_data;
123 struct nfp_net *nn = cb_priv;
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800124 struct bpf_prog *oldprog;
125 struct nfp_bpf_vnic *bv;
126 int err;
Jiri Pirko90d97312017-10-19 15:50:44 +0200127
Quentin Monnet52be9a72018-01-19 17:44:50 -0800128 if (type != TC_SETUP_CLSBPF) {
129 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
130 "only offload of BPF classifiers supported");
131 return -EOPNOTSUPP;
132 }
133 if (!tc_can_offload_extack(nn->dp.netdev, cls_bpf->common.extack))
134 return -EOPNOTSUPP;
135 if (!nfp_net_ebpf_capable(nn)) {
136 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
137 "NFP firmware does not support eBPF offload");
138 return -EOPNOTSUPP;
139 }
140 if (cls_bpf->common.protocol != htons(ETH_P_ALL)) {
141 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
142 "only ETH_P_ALL supported as filter protocol");
143 return -EOPNOTSUPP;
144 }
145 if (cls_bpf->common.chain_index)
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100146 return -EOPNOTSUPP;
147
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700148 /* Only support TC direct action */
149 if (!cls_bpf->exts_integrated ||
150 tcf_exts_has_actions(cls_bpf->exts)) {
Quentin Monnet52be9a72018-01-19 17:44:50 -0800151 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
152 "only direct action with no legacy actions supported");
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700153 return -EOPNOTSUPP;
154 }
Jakub Kicinskif4496572017-11-02 01:31:31 -0700155
Jakub Kicinski102740b2017-12-19 13:32:13 -0800156 if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
Jiri Pirko90d97312017-10-19 15:50:44 +0200157 return -EOPNOTSUPP;
Jakub Kicinski102740b2017-12-19 13:32:13 -0800158
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800159 bv = nn->app_priv;
160 oldprog = cls_bpf->oldprog;
161
162 /* Don't remove if oldprog doesn't match driver's state */
163 if (bv->tc_prog != oldprog) {
164 oldprog = NULL;
165 if (!cls_bpf->prog)
166 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200167 }
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800168
Quentin Monnet52be9a72018-01-19 17:44:50 -0800169 err = nfp_net_bpf_offload(nn, cls_bpf->prog, oldprog,
170 cls_bpf->common.extack);
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800171 if (err)
172 return err;
173
174 bv->tc_prog = cls_bpf->prog;
175 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200176}
177
178static int nfp_bpf_setup_tc_block(struct net_device *netdev,
179 struct tc_block_offload *f)
180{
181 struct nfp_net *nn = netdev_priv(netdev);
182
183 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
184 return -EOPNOTSUPP;
185
186 switch (f->command) {
187 case TC_BLOCK_BIND:
188 return tcf_block_cb_register(f->block,
189 nfp_bpf_setup_tc_block_cb,
190 nn, nn);
191 case TC_BLOCK_UNBIND:
192 tcf_block_cb_unregister(f->block,
193 nfp_bpf_setup_tc_block_cb,
194 nn);
195 return 0;
196 default:
197 return -EOPNOTSUPP;
198 }
199}
200
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700201static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200202 enum tc_setup_type type, void *type_data)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700203{
Jiri Pirko90d97312017-10-19 15:50:44 +0200204 switch (type) {
Jiri Pirko90d97312017-10-19 15:50:44 +0200205 case TC_SETUP_BLOCK:
206 return nfp_bpf_setup_tc_block(netdev, type_data);
207 default:
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700208 return -EOPNOTSUPP;
Jiri Pirko90d97312017-10-19 15:50:44 +0200209 }
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700210}
211
212static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
213{
Jakub Kicinskia0f30c92018-01-10 12:25:58 +0000214 struct nfp_bpf_vnic *bv = nn->app_priv;
215
216 return !!bv->tc_prog;
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700217}
218
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800219static int
Jakub Kicinskiccbdc592018-01-10 12:25:57 +0000220nfp_bpf_change_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
221{
222 struct nfp_net *nn = netdev_priv(netdev);
223 unsigned int max_mtu;
224
225 if (~nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
226 return 0;
227
228 max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
229 if (new_mtu > max_mtu) {
230 nn_info(nn, "BPF offload active, MTU over %u not supported\n",
231 max_mtu);
232 return -EBUSY;
233 }
234 return 0;
235}
236
237static int
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800238nfp_bpf_parse_cap_adjust_head(struct nfp_app_bpf *bpf, void __iomem *value,
239 u32 length)
240{
241 struct nfp_bpf_cap_tlv_adjust_head __iomem *cap = value;
242 struct nfp_cpp *cpp = bpf->app->pf->cpp;
243
244 if (length < sizeof(*cap)) {
245 nfp_err(cpp, "truncated adjust_head TLV: %d\n", length);
246 return -EINVAL;
247 }
248
249 bpf->adjust_head.flags = readl(&cap->flags);
250 bpf->adjust_head.off_min = readl(&cap->off_min);
251 bpf->adjust_head.off_max = readl(&cap->off_max);
Jakub Kicinski8231f842017-12-14 21:29:19 -0800252 bpf->adjust_head.guaranteed_sub = readl(&cap->guaranteed_sub);
253 bpf->adjust_head.guaranteed_add = readl(&cap->guaranteed_add);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800254
255 if (bpf->adjust_head.off_min > bpf->adjust_head.off_max) {
256 nfp_err(cpp, "invalid adjust_head TLV: min > max\n");
257 return -EINVAL;
258 }
259 if (!FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_min) ||
260 !FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_max)) {
261 nfp_warn(cpp, "disabling adjust_head - driver expects min/max to fit in as immediates\n");
262 memset(&bpf->adjust_head, 0, sizeof(bpf->adjust_head));
263 return 0;
264 }
265
266 return 0;
267}
268
Jakub Kicinski9d080d52018-01-11 20:29:13 -0800269static int
270nfp_bpf_parse_cap_func(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
271{
272 struct nfp_bpf_cap_tlv_func __iomem *cap = value;
273
274 if (length < sizeof(*cap)) {
275 nfp_err(bpf->app->cpp, "truncated function TLV: %d\n", length);
276 return -EINVAL;
277 }
278
279 switch (readl(&cap->func_id)) {
280 case BPF_FUNC_map_lookup_elem:
281 bpf->helpers.map_lookup = readl(&cap->func_addr);
282 break;
283 }
284
285 return 0;
286}
287
288static int
289nfp_bpf_parse_cap_maps(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
290{
291 struct nfp_bpf_cap_tlv_maps __iomem *cap = value;
292
293 if (length < sizeof(*cap)) {
294 nfp_err(bpf->app->cpp, "truncated maps TLV: %d\n", length);
295 return -EINVAL;
296 }
297
298 bpf->maps.types = readl(&cap->types);
299 bpf->maps.max_maps = readl(&cap->max_maps);
300 bpf->maps.max_elems = readl(&cap->max_elems);
301 bpf->maps.max_key_sz = readl(&cap->max_key_sz);
302 bpf->maps.max_val_sz = readl(&cap->max_val_sz);
303 bpf->maps.max_elem_sz = readl(&cap->max_elem_sz);
304
305 return 0;
306}
307
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800308static int nfp_bpf_parse_capabilities(struct nfp_app *app)
309{
310 struct nfp_cpp *cpp = app->pf->cpp;
311 struct nfp_cpp_area *area;
312 u8 __iomem *mem, *start;
313
314 mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
315 8, &area);
316 if (IS_ERR(mem))
317 return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
318
319 start = mem;
320 while (mem - start + 8 < nfp_cpp_area_size(area)) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800321 u8 __iomem *value;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800322 u32 type, length;
323
324 type = readl(mem);
325 length = readl(mem + 4);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800326 value = mem + 8;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800327
328 mem += 8 + length;
329 if (mem - start > nfp_cpp_area_size(area))
330 goto err_release_free;
331
332 switch (type) {
Jakub Kicinski9d080d52018-01-11 20:29:13 -0800333 case NFP_BPF_CAP_TYPE_FUNC:
334 if (nfp_bpf_parse_cap_func(app->priv, value, length))
335 goto err_release_free;
336 break;
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800337 case NFP_BPF_CAP_TYPE_ADJUST_HEAD:
338 if (nfp_bpf_parse_cap_adjust_head(app->priv, value,
339 length))
340 goto err_release_free;
341 break;
Jakub Kicinski9d080d52018-01-11 20:29:13 -0800342 case NFP_BPF_CAP_TYPE_MAPS:
343 if (nfp_bpf_parse_cap_maps(app->priv, value, length))
344 goto err_release_free;
345 break;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800346 default:
347 nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
348 break;
349 }
350 }
351 if (mem - start != nfp_cpp_area_size(area)) {
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800352 nfp_err(cpp, "BPF capabilities left after parsing, parsed:%zd total length:%zu\n",
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800353 mem - start, nfp_cpp_area_size(area));
354 goto err_release_free;
355 }
356
357 nfp_cpp_area_release_free(area);
358
359 return 0;
360
361err_release_free:
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800362 nfp_err(cpp, "invalid BPF capabilities at offset:%zd\n", mem - start);
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800363 nfp_cpp_area_release_free(area);
364 return -EINVAL;
365}
366
367static int nfp_bpf_init(struct nfp_app *app)
368{
369 struct nfp_app_bpf *bpf;
370 int err;
371
372 bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
373 if (!bpf)
374 return -ENOMEM;
375 bpf->app = app;
376 app->priv = bpf;
377
Jakub Kicinskid48ae232018-01-11 20:29:11 -0800378 skb_queue_head_init(&bpf->cmsg_replies);
379 init_waitqueue_head(&bpf->cmsg_wq);
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800380 INIT_LIST_HEAD(&bpf->map_list);
381
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800382 err = nfp_bpf_parse_capabilities(app);
383 if (err)
384 goto err_free_bpf;
385
386 return 0;
387
388err_free_bpf:
389 kfree(bpf);
390 return err;
391}
392
393static void nfp_bpf_clean(struct nfp_app *app)
394{
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800395 struct nfp_app_bpf *bpf = app->priv;
396
Jakub Kicinskid48ae232018-01-11 20:29:11 -0800397 WARN_ON(!skb_queue_empty(&bpf->cmsg_replies));
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800398 WARN_ON(!list_empty(&bpf->map_list));
Jakub Kicinski1bba4c42018-01-11 20:29:17 -0800399 WARN_ON(bpf->maps_in_use || bpf->map_elems_in_use);
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800400 kfree(bpf);
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800401}
402
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700403const struct nfp_app_type app_bpf = {
404 .id = NFP_APP_BPF_NIC,
Jakub Kicinski2707d6f2017-05-31 08:06:47 -0700405 .name = "ebpf",
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700406
Jakub Kicinski81bd5ded2018-01-17 18:51:06 -0800407 .ctrl_cap_mask = 0,
Jakub Kicinski78a0a652018-01-17 18:51:05 -0800408
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800409 .init = nfp_bpf_init,
410 .clean = nfp_bpf_clean,
411
Jakub Kicinskiccbdc592018-01-10 12:25:57 +0000412 .change_mtu = nfp_bpf_change_mtu,
413
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700414 .extra_cap = nfp_bpf_extra_cap,
415
Jakub Kicinski4f834352017-12-27 15:36:49 -0800416 .vnic_alloc = nfp_bpf_vnic_alloc,
417 .vnic_free = nfp_bpf_vnic_free,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700418
Jakub Kicinskid48ae232018-01-11 20:29:11 -0800419 .ctrl_msg_rx = nfp_bpf_ctrl_msg_rx,
420
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700421 .setup_tc = nfp_bpf_setup_tc,
422 .tc_busy = nfp_bpf_tc_busy,
Jakub Kicinskiaf93d152018-01-10 12:26:04 +0000423 .bpf = nfp_ndo_bpf,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700424 .xdp_offload = nfp_bpf_xdp_offload,
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700425};