blob: c9fd7d417d1aaff9b6124e0cc261ed673d5dfcf5 [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,
57 struct bpf_prog *prog)
58{
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
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -070073 ret = nfp_net_bpf_offload(nn, prog, running);
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)
Jakub Kicinskibb45e512017-05-31 08:06:49 -070076 nfp_bpf_xdp_offload(app, nn, NULL);
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
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700128 if (type != TC_SETUP_CLSBPF ||
129 !tc_can_offload(nn->dp.netdev) ||
130 !nfp_net_ebpf_capable(nn) ||
131 cls_bpf->common.protocol != htons(ETH_P_ALL) ||
132 cls_bpf->common.chain_index)
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100133 return -EOPNOTSUPP;
134
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700135 /* Only support TC direct action */
136 if (!cls_bpf->exts_integrated ||
137 tcf_exts_has_actions(cls_bpf->exts)) {
138 nn_err(nn, "only direct action with no legacy actions supported\n");
139 return -EOPNOTSUPP;
140 }
Jakub Kicinskif4496572017-11-02 01:31:31 -0700141
Jakub Kicinski102740b2017-12-19 13:32:13 -0800142 if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
Jiri Pirko90d97312017-10-19 15:50:44 +0200143 return -EOPNOTSUPP;
Jakub Kicinski102740b2017-12-19 13:32:13 -0800144
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800145 bv = nn->app_priv;
146 oldprog = cls_bpf->oldprog;
147
148 /* Don't remove if oldprog doesn't match driver's state */
149 if (bv->tc_prog != oldprog) {
150 oldprog = NULL;
151 if (!cls_bpf->prog)
152 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200153 }
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800154
155 err = nfp_net_bpf_offload(nn, cls_bpf->prog, oldprog);
156 if (err)
157 return err;
158
159 bv->tc_prog = cls_bpf->prog;
160 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200161}
162
163static int nfp_bpf_setup_tc_block(struct net_device *netdev,
164 struct tc_block_offload *f)
165{
166 struct nfp_net *nn = netdev_priv(netdev);
167
168 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
169 return -EOPNOTSUPP;
170
171 switch (f->command) {
172 case TC_BLOCK_BIND:
173 return tcf_block_cb_register(f->block,
174 nfp_bpf_setup_tc_block_cb,
175 nn, nn);
176 case TC_BLOCK_UNBIND:
177 tcf_block_cb_unregister(f->block,
178 nfp_bpf_setup_tc_block_cb,
179 nn);
180 return 0;
181 default:
182 return -EOPNOTSUPP;
183 }
184}
185
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700186static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200187 enum tc_setup_type type, void *type_data)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700188{
Jiri Pirko90d97312017-10-19 15:50:44 +0200189 switch (type) {
Jiri Pirko90d97312017-10-19 15:50:44 +0200190 case TC_SETUP_BLOCK:
191 return nfp_bpf_setup_tc_block(netdev, type_data);
192 default:
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700193 return -EOPNOTSUPP;
Jiri Pirko90d97312017-10-19 15:50:44 +0200194 }
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700195}
196
197static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
198{
Jakub Kicinskia0f30c92018-01-10 12:25:58 +0000199 struct nfp_bpf_vnic *bv = nn->app_priv;
200
201 return !!bv->tc_prog;
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700202}
203
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800204static int
Jakub Kicinskiccbdc592018-01-10 12:25:57 +0000205nfp_bpf_change_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
206{
207 struct nfp_net *nn = netdev_priv(netdev);
208 unsigned int max_mtu;
209
210 if (~nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
211 return 0;
212
213 max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
214 if (new_mtu > max_mtu) {
215 nn_info(nn, "BPF offload active, MTU over %u not supported\n",
216 max_mtu);
217 return -EBUSY;
218 }
219 return 0;
220}
221
222static int
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800223nfp_bpf_parse_cap_adjust_head(struct nfp_app_bpf *bpf, void __iomem *value,
224 u32 length)
225{
226 struct nfp_bpf_cap_tlv_adjust_head __iomem *cap = value;
227 struct nfp_cpp *cpp = bpf->app->pf->cpp;
228
229 if (length < sizeof(*cap)) {
230 nfp_err(cpp, "truncated adjust_head TLV: %d\n", length);
231 return -EINVAL;
232 }
233
234 bpf->adjust_head.flags = readl(&cap->flags);
235 bpf->adjust_head.off_min = readl(&cap->off_min);
236 bpf->adjust_head.off_max = readl(&cap->off_max);
Jakub Kicinski8231f842017-12-14 21:29:19 -0800237 bpf->adjust_head.guaranteed_sub = readl(&cap->guaranteed_sub);
238 bpf->adjust_head.guaranteed_add = readl(&cap->guaranteed_add);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800239
240 if (bpf->adjust_head.off_min > bpf->adjust_head.off_max) {
241 nfp_err(cpp, "invalid adjust_head TLV: min > max\n");
242 return -EINVAL;
243 }
244 if (!FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_min) ||
245 !FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_max)) {
246 nfp_warn(cpp, "disabling adjust_head - driver expects min/max to fit in as immediates\n");
247 memset(&bpf->adjust_head, 0, sizeof(bpf->adjust_head));
248 return 0;
249 }
250
251 return 0;
252}
253
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800254static int nfp_bpf_parse_capabilities(struct nfp_app *app)
255{
256 struct nfp_cpp *cpp = app->pf->cpp;
257 struct nfp_cpp_area *area;
258 u8 __iomem *mem, *start;
259
260 mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
261 8, &area);
262 if (IS_ERR(mem))
263 return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
264
265 start = mem;
266 while (mem - start + 8 < nfp_cpp_area_size(area)) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800267 u8 __iomem *value;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800268 u32 type, length;
269
270 type = readl(mem);
271 length = readl(mem + 4);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800272 value = mem + 8;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800273
274 mem += 8 + length;
275 if (mem - start > nfp_cpp_area_size(area))
276 goto err_release_free;
277
278 switch (type) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800279 case NFP_BPF_CAP_TYPE_ADJUST_HEAD:
280 if (nfp_bpf_parse_cap_adjust_head(app->priv, value,
281 length))
282 goto err_release_free;
283 break;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800284 default:
285 nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
286 break;
287 }
288 }
289 if (mem - start != nfp_cpp_area_size(area)) {
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800290 nfp_err(cpp, "BPF capabilities left after parsing, parsed:%zd total length:%zu\n",
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800291 mem - start, nfp_cpp_area_size(area));
292 goto err_release_free;
293 }
294
295 nfp_cpp_area_release_free(area);
296
297 return 0;
298
299err_release_free:
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800300 nfp_err(cpp, "invalid BPF capabilities at offset:%zd\n", mem - start);
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800301 nfp_cpp_area_release_free(area);
302 return -EINVAL;
303}
304
305static int nfp_bpf_init(struct nfp_app *app)
306{
307 struct nfp_app_bpf *bpf;
308 int err;
309
310 bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
311 if (!bpf)
312 return -ENOMEM;
313 bpf->app = app;
314 app->priv = bpf;
315
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800316 INIT_LIST_HEAD(&bpf->map_list);
317
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800318 err = nfp_bpf_parse_capabilities(app);
319 if (err)
320 goto err_free_bpf;
321
322 return 0;
323
324err_free_bpf:
325 kfree(bpf);
326 return err;
327}
328
329static void nfp_bpf_clean(struct nfp_app *app)
330{
Jakub Kicinski4da98ee2018-01-11 20:29:10 -0800331 struct nfp_app_bpf *bpf = app->priv;
332
333 WARN_ON(!list_empty(&bpf->map_list));
334 kfree(bpf);
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800335}
336
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700337const struct nfp_app_type app_bpf = {
338 .id = NFP_APP_BPF_NIC,
Jakub Kicinski2707d6f2017-05-31 08:06:47 -0700339 .name = "ebpf",
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700340
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800341 .init = nfp_bpf_init,
342 .clean = nfp_bpf_clean,
343
Jakub Kicinskiccbdc592018-01-10 12:25:57 +0000344 .change_mtu = nfp_bpf_change_mtu,
345
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700346 .extra_cap = nfp_bpf_extra_cap,
347
Jakub Kicinski4f834352017-12-27 15:36:49 -0800348 .vnic_alloc = nfp_bpf_vnic_alloc,
349 .vnic_free = nfp_bpf_vnic_free,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700350
351 .setup_tc = nfp_bpf_setup_tc,
352 .tc_busy = nfp_bpf_tc_busy,
Jakub Kicinskiaf93d152018-01-10 12:26:04 +0000353 .bpf = nfp_ndo_bpf,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700354 .xdp_offload = nfp_bpf_xdp_offload,
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700355};