blob: 214b02a3acdd20b077881759d527b9c793a4e026 [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
Jiri Pirko90d97312017-10-19 15:50:44 +020087static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
88 void *type_data, void *cb_priv)
89{
90 struct tc_cls_bpf_offload *cls_bpf = type_data;
91 struct nfp_net *nn = cb_priv;
Jakub Kicinskid3f89b92017-12-19 13:32:14 -080092 struct bpf_prog *oldprog;
93 struct nfp_bpf_vnic *bv;
94 int err;
Jiri Pirko90d97312017-10-19 15:50:44 +020095
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070096 if (type != TC_SETUP_CLSBPF ||
97 !tc_can_offload(nn->dp.netdev) ||
98 !nfp_net_ebpf_capable(nn) ||
99 cls_bpf->common.protocol != htons(ETH_P_ALL) ||
100 cls_bpf->common.chain_index)
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100101 return -EOPNOTSUPP;
102
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700103 /* Only support TC direct action */
104 if (!cls_bpf->exts_integrated ||
105 tcf_exts_has_actions(cls_bpf->exts)) {
106 nn_err(nn, "only direct action with no legacy actions supported\n");
107 return -EOPNOTSUPP;
108 }
Jakub Kicinskif4496572017-11-02 01:31:31 -0700109
Jakub Kicinski102740b2017-12-19 13:32:13 -0800110 if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
Jiri Pirko90d97312017-10-19 15:50:44 +0200111 return -EOPNOTSUPP;
Jakub Kicinski102740b2017-12-19 13:32:13 -0800112
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800113 bv = nn->app_priv;
114 oldprog = cls_bpf->oldprog;
115
116 /* Don't remove if oldprog doesn't match driver's state */
117 if (bv->tc_prog != oldprog) {
118 oldprog = NULL;
119 if (!cls_bpf->prog)
120 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200121 }
Jakub Kicinskid3f89b92017-12-19 13:32:14 -0800122
123 err = nfp_net_bpf_offload(nn, cls_bpf->prog, oldprog);
124 if (err)
125 return err;
126
127 bv->tc_prog = cls_bpf->prog;
128 return 0;
Jiri Pirko90d97312017-10-19 15:50:44 +0200129}
130
131static int nfp_bpf_setup_tc_block(struct net_device *netdev,
132 struct tc_block_offload *f)
133{
134 struct nfp_net *nn = netdev_priv(netdev);
135
136 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
137 return -EOPNOTSUPP;
138
139 switch (f->command) {
140 case TC_BLOCK_BIND:
141 return tcf_block_cb_register(f->block,
142 nfp_bpf_setup_tc_block_cb,
143 nn, nn);
144 case TC_BLOCK_UNBIND:
145 tcf_block_cb_unregister(f->block,
146 nfp_bpf_setup_tc_block_cb,
147 nn);
148 return 0;
149 default:
150 return -EOPNOTSUPP;
151 }
152}
153
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700154static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200155 enum tc_setup_type type, void *type_data)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700156{
Jiri Pirko90d97312017-10-19 15:50:44 +0200157 switch (type) {
Jiri Pirko90d97312017-10-19 15:50:44 +0200158 case TC_SETUP_BLOCK:
159 return nfp_bpf_setup_tc_block(netdev, type_data);
160 default:
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700161 return -EOPNOTSUPP;
Jiri Pirko90d97312017-10-19 15:50:44 +0200162 }
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700163}
164
165static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
166{
167 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
168}
169
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800170static int
171nfp_bpf_parse_cap_adjust_head(struct nfp_app_bpf *bpf, void __iomem *value,
172 u32 length)
173{
174 struct nfp_bpf_cap_tlv_adjust_head __iomem *cap = value;
175 struct nfp_cpp *cpp = bpf->app->pf->cpp;
176
177 if (length < sizeof(*cap)) {
178 nfp_err(cpp, "truncated adjust_head TLV: %d\n", length);
179 return -EINVAL;
180 }
181
182 bpf->adjust_head.flags = readl(&cap->flags);
183 bpf->adjust_head.off_min = readl(&cap->off_min);
184 bpf->adjust_head.off_max = readl(&cap->off_max);
Jakub Kicinski8231f842017-12-14 21:29:19 -0800185 bpf->adjust_head.guaranteed_sub = readl(&cap->guaranteed_sub);
186 bpf->adjust_head.guaranteed_add = readl(&cap->guaranteed_add);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800187
188 if (bpf->adjust_head.off_min > bpf->adjust_head.off_max) {
189 nfp_err(cpp, "invalid adjust_head TLV: min > max\n");
190 return -EINVAL;
191 }
192 if (!FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_min) ||
193 !FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_max)) {
194 nfp_warn(cpp, "disabling adjust_head - driver expects min/max to fit in as immediates\n");
195 memset(&bpf->adjust_head, 0, sizeof(bpf->adjust_head));
196 return 0;
197 }
198
199 return 0;
200}
201
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800202static int nfp_bpf_parse_capabilities(struct nfp_app *app)
203{
204 struct nfp_cpp *cpp = app->pf->cpp;
205 struct nfp_cpp_area *area;
206 u8 __iomem *mem, *start;
207
208 mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
209 8, &area);
210 if (IS_ERR(mem))
211 return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
212
213 start = mem;
214 while (mem - start + 8 < nfp_cpp_area_size(area)) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800215 u8 __iomem *value;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800216 u32 type, length;
217
218 type = readl(mem);
219 length = readl(mem + 4);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800220 value = mem + 8;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800221
222 mem += 8 + length;
223 if (mem - start > nfp_cpp_area_size(area))
224 goto err_release_free;
225
226 switch (type) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800227 case NFP_BPF_CAP_TYPE_ADJUST_HEAD:
228 if (nfp_bpf_parse_cap_adjust_head(app->priv, value,
229 length))
230 goto err_release_free;
231 break;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800232 default:
233 nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
234 break;
235 }
236 }
237 if (mem - start != nfp_cpp_area_size(area)) {
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800238 nfp_err(cpp, "BPF capabilities left after parsing, parsed:%zd total length:%zu\n",
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800239 mem - start, nfp_cpp_area_size(area));
240 goto err_release_free;
241 }
242
243 nfp_cpp_area_release_free(area);
244
245 return 0;
246
247err_release_free:
Jakub Kicinski0bce7c92017-12-15 10:39:31 -0800248 nfp_err(cpp, "invalid BPF capabilities at offset:%zd\n", mem - start);
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800249 nfp_cpp_area_release_free(area);
250 return -EINVAL;
251}
252
253static int nfp_bpf_init(struct nfp_app *app)
254{
255 struct nfp_app_bpf *bpf;
256 int err;
257
258 bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
259 if (!bpf)
260 return -ENOMEM;
261 bpf->app = app;
262 app->priv = bpf;
263
264 err = nfp_bpf_parse_capabilities(app);
265 if (err)
266 goto err_free_bpf;
267
268 return 0;
269
270err_free_bpf:
271 kfree(bpf);
272 return err;
273}
274
275static void nfp_bpf_clean(struct nfp_app *app)
276{
277 kfree(app->priv);
278}
279
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700280const struct nfp_app_type app_bpf = {
281 .id = NFP_APP_BPF_NIC,
Jakub Kicinski2707d6f2017-05-31 08:06:47 -0700282 .name = "ebpf",
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700283
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800284 .init = nfp_bpf_init,
285 .clean = nfp_bpf_clean,
286
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700287 .extra_cap = nfp_bpf_extra_cap,
288
Jakub Kicinski012bb8a2017-11-03 13:56:22 -0700289 .vnic_alloc = nfp_app_nic_vnic_alloc,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700290
291 .setup_tc = nfp_bpf_setup_tc,
292 .tc_busy = nfp_bpf_tc_busy,
293 .xdp_offload = nfp_bpf_xdp_offload,
Jakub Kicinskic6c580d2017-11-03 13:56:29 -0700294
295 .bpf_verifier_prep = nfp_bpf_verifier_prep,
296 .bpf_translate = nfp_bpf_translate,
297 .bpf_destroy = nfp_bpf_destroy,
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700298};