blob: 978086580ca018032ca5298fa233ec5640c59b32 [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{
90 int err;
91
92 nn->app_priv = kzalloc(sizeof(struct nfp_bpf_vnic), GFP_KERNEL);
93 if (!nn->app_priv)
94 return -ENOMEM;
95
96 err = nfp_app_nic_vnic_alloc(app, nn, id);
97 if (err)
98 goto err_free_priv;
99
100 return 0;
101err_free_priv:
102 kfree(nn->app_priv);
103 return err;
104}
105
106static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
107{
108 struct nfp_bpf_vnic *bv = nn->app_priv;
109
110 WARN_ON(bv->tc_prog);
111 kfree(bv);
112}
113
Jiri Pirko90d97312017-10-19 15:50:44 +0200114static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
115 void *type_data, void *cb_priv)
116{
117 struct tc_cls_bpf_offload *cls_bpf = type_data;
118 struct nfp_net *nn = cb_priv;
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800119 struct bpf_prog *oldprog;
120 struct nfp_bpf_vnic *bv;
121 int err;
Jiri Pirko90d97312017-10-19 15:50:44 +0200122
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700123 if (type != TC_SETUP_CLSBPF ||
124 !tc_can_offload(nn->dp.netdev) ||
125 !nfp_net_ebpf_capable(nn) ||
126 cls_bpf->common.protocol != htons(ETH_P_ALL) ||
127 cls_bpf->common.chain_index)
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100128 return -EOPNOTSUPP;
129
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700130 /* Only support TC direct action */
131 if (!cls_bpf->exts_integrated ||
132 tcf_exts_has_actions(cls_bpf->exts)) {
133 nn_err(nn, "only direct action with no legacy actions supported\n");
134 return -EOPNOTSUPP;
135 }
Jakub Kicinskif4496572017-11-02 01:31:31 -0700136
Jakub Kicinski102740b2017-12-19 13:32:13 -0800137 if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
Jiri Pirko90d97312017-10-19 15:50:44 +0200138 return -EOPNOTSUPP;
Jakub Kicinski102740b2017-12-19 13:32:13 -0800139
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800140 bv = nn->app_priv;
141 oldprog = cls_bpf->oldprog;
142
143 /* Don't remove if oldprog doesn't match driver's state */
144 if (bv->tc_prog != oldprog) {
145 oldprog = NULL;
146 if (!cls_bpf->prog)
147 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200148 }
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800149
150 err = nfp_net_bpf_offload(nn, cls_bpf->prog, oldprog);
151 if (err)
152 return err;
153
154 bv->tc_prog = cls_bpf->prog;
155 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200156}
157
158static int nfp_bpf_setup_tc_block(struct net_device *netdev,
159 struct tc_block_offload *f)
160{
161 struct nfp_net *nn = netdev_priv(netdev);
162
163 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
164 return -EOPNOTSUPP;
165
166 switch (f->command) {
167 case TC_BLOCK_BIND:
168 return tcf_block_cb_register(f->block,
169 nfp_bpf_setup_tc_block_cb,
170 nn, nn);
171 case TC_BLOCK_UNBIND:
172 tcf_block_cb_unregister(f->block,
173 nfp_bpf_setup_tc_block_cb,
174 nn);
175 return 0;
176 default:
177 return -EOPNOTSUPP;
178 }
179}
180
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700181static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200182 enum tc_setup_type type, void *type_data)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700183{
Jiri Pirko90d97312017-10-19 15:50:44 +0200184 switch (type) {
Jiri Pirko90d97312017-10-19 15:50:44 +0200185 case TC_SETUP_BLOCK:
186 return nfp_bpf_setup_tc_block(netdev, type_data);
187 default:
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700188 return -EOPNOTSUPP;
Jiri Pirko90d97312017-10-19 15:50:44 +0200189 }
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700190}
191
192static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
193{
194 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
195}
196
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800197static int
Jakub Kicinskiccbdc592018-01-10 12:25:57 +0000198nfp_bpf_change_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
199{
200 struct nfp_net *nn = netdev_priv(netdev);
201 unsigned int max_mtu;
202
203 if (~nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
204 return 0;
205
206 max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
207 if (new_mtu > max_mtu) {
208 nn_info(nn, "BPF offload active, MTU over %u not supported\n",
209 max_mtu);
210 return -EBUSY;
211 }
212 return 0;
213}
214
215static int
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800216nfp_bpf_parse_cap_adjust_head(struct nfp_app_bpf *bpf, void __iomem *value,
217 u32 length)
218{
219 struct nfp_bpf_cap_tlv_adjust_head __iomem *cap = value;
220 struct nfp_cpp *cpp = bpf->app->pf->cpp;
221
222 if (length < sizeof(*cap)) {
223 nfp_err(cpp, "truncated adjust_head TLV: %d\n", length);
224 return -EINVAL;
225 }
226
227 bpf->adjust_head.flags = readl(&cap->flags);
228 bpf->adjust_head.off_min = readl(&cap->off_min);
229 bpf->adjust_head.off_max = readl(&cap->off_max);
Jakub Kicinski8231f842017-12-14 21:29:19 -0800230 bpf->adjust_head.guaranteed_sub = readl(&cap->guaranteed_sub);
231 bpf->adjust_head.guaranteed_add = readl(&cap->guaranteed_add);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800232
233 if (bpf->adjust_head.off_min > bpf->adjust_head.off_max) {
234 nfp_err(cpp, "invalid adjust_head TLV: min > max\n");
235 return -EINVAL;
236 }
237 if (!FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_min) ||
238 !FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_max)) {
239 nfp_warn(cpp, "disabling adjust_head - driver expects min/max to fit in as immediates\n");
240 memset(&bpf->adjust_head, 0, sizeof(bpf->adjust_head));
241 return 0;
242 }
243
244 return 0;
245}
246
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800247static int nfp_bpf_parse_capabilities(struct nfp_app *app)
248{
249 struct nfp_cpp *cpp = app->pf->cpp;
250 struct nfp_cpp_area *area;
251 u8 __iomem *mem, *start;
252
253 mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
254 8, &area);
255 if (IS_ERR(mem))
256 return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
257
258 start = mem;
259 while (mem - start + 8 < nfp_cpp_area_size(area)) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800260 u8 __iomem *value;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800261 u32 type, length;
262
263 type = readl(mem);
264 length = readl(mem + 4);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800265 value = mem + 8;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800266
267 mem += 8 + length;
268 if (mem - start > nfp_cpp_area_size(area))
269 goto err_release_free;
270
271 switch (type) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800272 case NFP_BPF_CAP_TYPE_ADJUST_HEAD:
273 if (nfp_bpf_parse_cap_adjust_head(app->priv, value,
274 length))
275 goto err_release_free;
276 break;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800277 default:
278 nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
279 break;
280 }
281 }
282 if (mem - start != nfp_cpp_area_size(area)) {
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800283 nfp_err(cpp, "BPF capabilities left after parsing, parsed:%zd total length:%zu\n",
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800284 mem - start, nfp_cpp_area_size(area));
285 goto err_release_free;
286 }
287
288 nfp_cpp_area_release_free(area);
289
290 return 0;
291
292err_release_free:
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800293 nfp_err(cpp, "invalid BPF capabilities at offset:%zd\n", mem - start);
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800294 nfp_cpp_area_release_free(area);
295 return -EINVAL;
296}
297
298static int nfp_bpf_init(struct nfp_app *app)
299{
300 struct nfp_app_bpf *bpf;
301 int err;
302
303 bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
304 if (!bpf)
305 return -ENOMEM;
306 bpf->app = app;
307 app->priv = bpf;
308
309 err = nfp_bpf_parse_capabilities(app);
310 if (err)
311 goto err_free_bpf;
312
313 return 0;
314
315err_free_bpf:
316 kfree(bpf);
317 return err;
318}
319
320static void nfp_bpf_clean(struct nfp_app *app)
321{
322 kfree(app->priv);
323}
324
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700325const struct nfp_app_type app_bpf = {
326 .id = NFP_APP_BPF_NIC,
Jakub Kicinski2707d6f2017-05-31 08:06:47 -0700327 .name = "ebpf",
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700328
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800329 .init = nfp_bpf_init,
330 .clean = nfp_bpf_clean,
331
Jakub Kicinskiccbdc592018-01-10 12:25:57 +0000332 .change_mtu = nfp_bpf_change_mtu,
333
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700334 .extra_cap = nfp_bpf_extra_cap,
335
Jakub Kicinski4f834352017-12-27 15:36:49 -0800336 .vnic_alloc = nfp_bpf_vnic_alloc,
337 .vnic_free = nfp_bpf_vnic_free,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700338
339 .setup_tc = nfp_bpf_setup_tc,
340 .tc_busy = nfp_bpf_tc_busy,
341 .xdp_offload = nfp_bpf_xdp_offload,
Jakub Kicinskic6c580d2017-11-03 13:56:29 -0700342
343 .bpf_verifier_prep = nfp_bpf_verifier_prep,
344 .bpf_translate = nfp_bpf_translate,
345 .bpf_destroy = nfp_bpf_destroy,
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700346};