blob: 7678e687a2b1463a3e56f8522b53a903b2b4da43 [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;
92
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070093 if (type != TC_SETUP_CLSBPF ||
94 !tc_can_offload(nn->dp.netdev) ||
95 !nfp_net_ebpf_capable(nn) ||
96 cls_bpf->common.protocol != htons(ETH_P_ALL) ||
97 cls_bpf->common.chain_index)
Jiri Pirko44ae12a2017-11-01 11:47:39 +010098 return -EOPNOTSUPP;
Jakub Kicinski9ce7a952017-11-03 13:56:25 -070099 if (nn->dp.bpf_offload_xdp)
100 return -EBUSY;
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100101
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700102 /* Only support TC direct action */
103 if (!cls_bpf->exts_integrated ||
104 tcf_exts_has_actions(cls_bpf->exts)) {
105 nn_err(nn, "only direct action with no legacy actions supported\n");
106 return -EOPNOTSUPP;
107 }
Jakub Kicinskif4496572017-11-02 01:31:31 -0700108
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700109 switch (cls_bpf->command) {
110 case TC_CLSBPF_REPLACE:
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700111 return nfp_net_bpf_offload(nn, cls_bpf->prog, true);
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700112 case TC_CLSBPF_ADD:
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700113 return nfp_net_bpf_offload(nn, cls_bpf->prog, false);
Jakub Kicinski9ce7a952017-11-03 13:56:25 -0700114 case TC_CLSBPF_DESTROY:
Jakub Kicinskie4a91cd2017-11-03 13:56:26 -0700115 return nfp_net_bpf_offload(nn, NULL, true);
Jiri Pirko90d97312017-10-19 15:50:44 +0200116 default:
117 return -EOPNOTSUPP;
118 }
119}
120
121static int nfp_bpf_setup_tc_block(struct net_device *netdev,
122 struct tc_block_offload *f)
123{
124 struct nfp_net *nn = netdev_priv(netdev);
125
126 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
127 return -EOPNOTSUPP;
128
129 switch (f->command) {
130 case TC_BLOCK_BIND:
131 return tcf_block_cb_register(f->block,
132 nfp_bpf_setup_tc_block_cb,
133 nn, nn);
134 case TC_BLOCK_UNBIND:
135 tcf_block_cb_unregister(f->block,
136 nfp_bpf_setup_tc_block_cb,
137 nn);
138 return 0;
139 default:
140 return -EOPNOTSUPP;
141 }
142}
143
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700144static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200145 enum tc_setup_type type, void *type_data)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700146{
Jiri Pirko90d97312017-10-19 15:50:44 +0200147 switch (type) {
Jiri Pirko90d97312017-10-19 15:50:44 +0200148 case TC_SETUP_BLOCK:
149 return nfp_bpf_setup_tc_block(netdev, type_data);
150 default:
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700151 return -EOPNOTSUPP;
Jiri Pirko90d97312017-10-19 15:50:44 +0200152 }
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700153}
154
155static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
156{
157 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
158}
159
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800160static int
161nfp_bpf_parse_cap_adjust_head(struct nfp_app_bpf *bpf, void __iomem *value,
162 u32 length)
163{
164 struct nfp_bpf_cap_tlv_adjust_head __iomem *cap = value;
165 struct nfp_cpp *cpp = bpf->app->pf->cpp;
166
167 if (length < sizeof(*cap)) {
168 nfp_err(cpp, "truncated adjust_head TLV: %d\n", length);
169 return -EINVAL;
170 }
171
172 bpf->adjust_head.flags = readl(&cap->flags);
173 bpf->adjust_head.off_min = readl(&cap->off_min);
174 bpf->adjust_head.off_max = readl(&cap->off_max);
Jakub Kicinski8231f842017-12-14 21:29:19 -0800175 bpf->adjust_head.guaranteed_sub = readl(&cap->guaranteed_sub);
176 bpf->adjust_head.guaranteed_add = readl(&cap->guaranteed_add);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800177
178 if (bpf->adjust_head.off_min > bpf->adjust_head.off_max) {
179 nfp_err(cpp, "invalid adjust_head TLV: min > max\n");
180 return -EINVAL;
181 }
182 if (!FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_min) ||
183 !FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_max)) {
184 nfp_warn(cpp, "disabling adjust_head - driver expects min/max to fit in as immediates\n");
185 memset(&bpf->adjust_head, 0, sizeof(bpf->adjust_head));
186 return 0;
187 }
188
189 return 0;
190}
191
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800192static int nfp_bpf_parse_capabilities(struct nfp_app *app)
193{
194 struct nfp_cpp *cpp = app->pf->cpp;
195 struct nfp_cpp_area *area;
196 u8 __iomem *mem, *start;
197
198 mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
199 8, &area);
200 if (IS_ERR(mem))
201 return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
202
203 start = mem;
204 while (mem - start + 8 < nfp_cpp_area_size(area)) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800205 u8 __iomem *value;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800206 u32 type, length;
207
208 type = readl(mem);
209 length = readl(mem + 4);
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800210 value = mem + 8;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800211
212 mem += 8 + length;
213 if (mem - start > nfp_cpp_area_size(area))
214 goto err_release_free;
215
216 switch (type) {
Jakub Kicinski0d49eaf2017-12-14 21:29:18 -0800217 case NFP_BPF_CAP_TYPE_ADJUST_HEAD:
218 if (nfp_bpf_parse_cap_adjust_head(app->priv, value,
219 length))
220 goto err_release_free;
221 break;
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800222 default:
223 nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
224 break;
225 }
226 }
227 if (mem - start != nfp_cpp_area_size(area)) {
228 nfp_err(cpp, "BPF capabilities left after parsing, parsed:%lu total length:%lu\n",
229 mem - start, nfp_cpp_area_size(area));
230 goto err_release_free;
231 }
232
233 nfp_cpp_area_release_free(area);
234
235 return 0;
236
237err_release_free:
238 nfp_err(cpp, "invalid BPF capabilities at offset:%ld\n", mem - start);
239 nfp_cpp_area_release_free(area);
240 return -EINVAL;
241}
242
243static int nfp_bpf_init(struct nfp_app *app)
244{
245 struct nfp_app_bpf *bpf;
246 int err;
247
248 bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
249 if (!bpf)
250 return -ENOMEM;
251 bpf->app = app;
252 app->priv = bpf;
253
254 err = nfp_bpf_parse_capabilities(app);
255 if (err)
256 goto err_free_bpf;
257
258 return 0;
259
260err_free_bpf:
261 kfree(bpf);
262 return err;
263}
264
265static void nfp_bpf_clean(struct nfp_app *app)
266{
267 kfree(app->priv);
268}
269
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700270const struct nfp_app_type app_bpf = {
271 .id = NFP_APP_BPF_NIC,
Jakub Kicinski2707d6f2017-05-31 08:06:47 -0700272 .name = "ebpf",
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700273
Jakub Kicinski77a844e2017-12-14 21:29:16 -0800274 .init = nfp_bpf_init,
275 .clean = nfp_bpf_clean,
276
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700277 .extra_cap = nfp_bpf_extra_cap,
278
Jakub Kicinski012bb8a2017-11-03 13:56:22 -0700279 .vnic_alloc = nfp_app_nic_vnic_alloc,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700280
281 .setup_tc = nfp_bpf_setup_tc,
282 .tc_busy = nfp_bpf_tc_busy,
283 .xdp_offload = nfp_bpf_xdp_offload,
Jakub Kicinskic6c580d2017-11-03 13:56:29 -0700284
285 .bpf_verifier_prep = nfp_bpf_verifier_prep,
286 .bpf_translate = nfp_bpf_translate,
287 .bpf_destroy = nfp_bpf_destroy,
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700288};