main.c 10.0 KB
Newer Older
J
Jakub Kicinski 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Copyright (C) 2017 Netronome Systems, Inc.
 *
 * This software is dual licensed under the GNU General License Version 2,
 * June 1991 as shown in the file COPYING in the top-level directory of this
 * source tree or the BSD 2-Clause License provided below.  You have the
 * option to license this software under the complete terms of either license.
 *
 * The BSD 2-Clause License:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      1. Redistributions of source code must retain the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer.
 *
 *      2. Redistributions in binary form must reproduce the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer in the documentation and/or other materials
 *         provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

34 35
#include <net/pkt_cls.h>

J
Jakub Kicinski 已提交
36
#include "../nfpcore/nfp_cpp.h"
37
#include "../nfpcore/nfp_nffw.h"
J
Jakub Kicinski 已提交
38 39 40 41
#include "../nfp_app.h"
#include "../nfp_main.h"
#include "../nfp_net.h"
#include "../nfp_port.h"
42
#include "fw.h"
43 44 45 46
#include "main.h"

static bool nfp_net_ebpf_capable(struct nfp_net *nn)
{
47
#ifdef __LITTLE_ENDIAN
48 49 50
	if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
	    nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
		return true;
51
#endif
52 53 54 55 56
	return false;
}

static int
nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
57
		    struct bpf_prog *prog, struct netlink_ext_ack *extack)
58
{
J
Jakub Kicinski 已提交
59
	bool running, xdp_running;
60 61 62 63 64
	int ret;

	if (!nfp_net_ebpf_capable(nn))
		return -EINVAL;

J
Jakub Kicinski 已提交
65 66 67 68 69 70 71
	running = nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
	xdp_running = running && nn->dp.bpf_offload_xdp;

	if (!prog && !xdp_running)
		return 0;
	if (prog && running && !xdp_running)
		return -EBUSY;
72

73
	ret = nfp_net_bpf_offload(nn, prog, running);
74
	/* Stop offload if replace not possible */
J
Jakub Kicinski 已提交
75
	if (ret && prog)
76
		nfp_bpf_xdp_offload(app, nn, NULL, extack);
J
Jakub Kicinski 已提交
77

78 79 80 81 82 83 84 85
	nn->dp.bpf_offload_xdp = prog && !ret;
	return ret;
}

static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
{
	return nfp_net_ebpf_capable(nn) ? "BPF" : "";
}
J
Jakub Kicinski 已提交
86

87 88 89
static int
nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
{
90
	struct nfp_bpf_vnic *bv;
91 92
	int err;

93 94
	bv = kzalloc(sizeof(*bv), GFP_KERNEL);
	if (!bv)
95
		return -ENOMEM;
96
	nn->app_priv = bv;
97 98 99 100 101

	err = nfp_app_nic_vnic_alloc(app, nn, id);
	if (err)
		goto err_free_priv;

102 103 104
	bv->start_off = nn_readw(nn, NFP_NET_CFG_BPF_START);
	bv->tgt_done = nn_readw(nn, NFP_NET_CFG_BPF_DONE);

105 106 107 108 109 110 111 112 113 114 115 116 117 118
	return 0;
err_free_priv:
	kfree(nn->app_priv);
	return err;
}

static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
{
	struct nfp_bpf_vnic *bv = nn->app_priv;

	WARN_ON(bv->tc_prog);
	kfree(bv);
}

119 120
static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
				     void *type_data, void *cb_priv)
121
{
122
	struct tc_cls_bpf_offload *cls_bpf = type_data;
123
	struct nfp_net *nn = cb_priv;
124 125 126
	struct bpf_prog *oldprog;
	struct nfp_bpf_vnic *bv;
	int err;
J
Jakub Kicinski 已提交
127 128 129 130 131 132 133

	if (type != TC_SETUP_CLSBPF ||
	    !tc_can_offload(nn->dp.netdev) ||
	    !nfp_net_ebpf_capable(nn) ||
	    cls_bpf->common.protocol != htons(ETH_P_ALL) ||
	    cls_bpf->common.chain_index)
		return -EOPNOTSUPP;
134

J
Jakub Kicinski 已提交
135 136 137 138
	/* Only support TC direct action */
	if (!cls_bpf->exts_integrated ||
	    tcf_exts_has_actions(cls_bpf->exts)) {
		nn_err(nn, "only direct action with no legacy actions supported\n");
139
		return -EOPNOTSUPP;
J
Jakub Kicinski 已提交
140 141
	}

142
	if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
143
		return -EOPNOTSUPP;
144

145 146 147 148 149 150 151 152
	bv = nn->app_priv;
	oldprog = cls_bpf->oldprog;

	/* Don't remove if oldprog doesn't match driver's state */
	if (bv->tc_prog != oldprog) {
		oldprog = NULL;
		if (!cls_bpf->prog)
			return 0;
153
	}
154 155 156 157 158 159 160

	err = nfp_net_bpf_offload(nn, cls_bpf->prog, oldprog);
	if (err)
		return err;

	bv->tc_prog = cls_bpf->prog;
	return 0;
161 162 163 164 165
}

static int nfp_bpf_setup_tc_block(struct net_device *netdev,
				  struct tc_block_offload *f)
{
166 167
	struct nfp_net *nn = netdev_priv(netdev);

168
	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
169 170
		return -EOPNOTSUPP;

171 172 173 174 175 176 177 178 179 180 181 182 183 184
	switch (f->command) {
	case TC_BLOCK_BIND:
		return tcf_block_cb_register(f->block,
					     nfp_bpf_setup_tc_block_cb,
					     nn, nn);
	case TC_BLOCK_UNBIND:
		tcf_block_cb_unregister(f->block,
					nfp_bpf_setup_tc_block_cb,
					nn);
		return 0;
	default:
		return -EOPNOTSUPP;
	}
}
185

186 187 188 189 190 191 192 193 194
static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
			    enum tc_setup_type type, void *type_data)
{
	switch (type) {
	case TC_SETUP_BLOCK:
		return nfp_bpf_setup_tc_block(netdev, type_data);
	default:
		return -EOPNOTSUPP;
	}
195 196 197 198
}

static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
{
199 200 201
	struct nfp_bpf_vnic *bv = nn->app_priv;

	return !!bv->tc_prog;
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
static int
nfp_bpf_change_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
{
	struct nfp_net *nn = netdev_priv(netdev);
	unsigned int max_mtu;

	if (~nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
		return 0;

	max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
	if (new_mtu > max_mtu) {
		nn_info(nn, "BPF offload active, MTU over %u not supported\n",
			max_mtu);
		return -EBUSY;
	}
	return 0;
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static int
nfp_bpf_parse_cap_adjust_head(struct nfp_app_bpf *bpf, void __iomem *value,
			      u32 length)
{
	struct nfp_bpf_cap_tlv_adjust_head __iomem *cap = value;
	struct nfp_cpp *cpp = bpf->app->pf->cpp;

	if (length < sizeof(*cap)) {
		nfp_err(cpp, "truncated adjust_head TLV: %d\n", length);
		return -EINVAL;
	}

	bpf->adjust_head.flags = readl(&cap->flags);
	bpf->adjust_head.off_min = readl(&cap->off_min);
	bpf->adjust_head.off_max = readl(&cap->off_max);
237 238
	bpf->adjust_head.guaranteed_sub = readl(&cap->guaranteed_sub);
	bpf->adjust_head.guaranteed_add = readl(&cap->guaranteed_add);
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

	if (bpf->adjust_head.off_min > bpf->adjust_head.off_max) {
		nfp_err(cpp, "invalid adjust_head TLV: min > max\n");
		return -EINVAL;
	}
	if (!FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_min) ||
	    !FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_max)) {
		nfp_warn(cpp, "disabling adjust_head - driver expects min/max to fit in as immediates\n");
		memset(&bpf->adjust_head, 0, sizeof(bpf->adjust_head));
		return 0;
	}

	return 0;
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static int
nfp_bpf_parse_cap_func(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
{
	struct nfp_bpf_cap_tlv_func __iomem *cap = value;

	if (length < sizeof(*cap)) {
		nfp_err(bpf->app->cpp, "truncated function TLV: %d\n", length);
		return -EINVAL;
	}

	switch (readl(&cap->func_id)) {
	case BPF_FUNC_map_lookup_elem:
		bpf->helpers.map_lookup = readl(&cap->func_addr);
		break;
	}

	return 0;
}

static int
nfp_bpf_parse_cap_maps(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
{
	struct nfp_bpf_cap_tlv_maps __iomem *cap = value;

	if (length < sizeof(*cap)) {
		nfp_err(bpf->app->cpp, "truncated maps TLV: %d\n", length);
		return -EINVAL;
	}

	bpf->maps.types = readl(&cap->types);
	bpf->maps.max_maps = readl(&cap->max_maps);
	bpf->maps.max_elems = readl(&cap->max_elems);
	bpf->maps.max_key_sz = readl(&cap->max_key_sz);
	bpf->maps.max_val_sz = readl(&cap->max_val_sz);
	bpf->maps.max_elem_sz = readl(&cap->max_elem_sz);

	return 0;
}

293 294 295 296 297 298 299 300 301 302 303 304 305
static int nfp_bpf_parse_capabilities(struct nfp_app *app)
{
	struct nfp_cpp *cpp = app->pf->cpp;
	struct nfp_cpp_area *area;
	u8 __iomem *mem, *start;

	mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
			    8, &area);
	if (IS_ERR(mem))
		return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);

	start = mem;
	while (mem - start + 8 < nfp_cpp_area_size(area)) {
306
		u8 __iomem *value;
307 308 309 310
		u32 type, length;

		type = readl(mem);
		length = readl(mem + 4);
311
		value = mem + 8;
312 313 314 315 316 317

		mem += 8 + length;
		if (mem - start > nfp_cpp_area_size(area))
			goto err_release_free;

		switch (type) {
318 319 320 321
		case NFP_BPF_CAP_TYPE_FUNC:
			if (nfp_bpf_parse_cap_func(app->priv, value, length))
				goto err_release_free;
			break;
322 323 324 325 326
		case NFP_BPF_CAP_TYPE_ADJUST_HEAD:
			if (nfp_bpf_parse_cap_adjust_head(app->priv, value,
							  length))
				goto err_release_free;
			break;
327 328 329 330
		case NFP_BPF_CAP_TYPE_MAPS:
			if (nfp_bpf_parse_cap_maps(app->priv, value, length))
				goto err_release_free;
			break;
331 332 333 334 335 336
		default:
			nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
			break;
		}
	}
	if (mem - start != nfp_cpp_area_size(area)) {
337
		nfp_err(cpp, "BPF capabilities left after parsing, parsed:%zd total length:%zu\n",
338 339 340 341 342 343 344 345 346
			mem - start, nfp_cpp_area_size(area));
		goto err_release_free;
	}

	nfp_cpp_area_release_free(area);

	return 0;

err_release_free:
347
	nfp_err(cpp, "invalid BPF capabilities at offset:%zd\n", mem - start);
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	nfp_cpp_area_release_free(area);
	return -EINVAL;
}

static int nfp_bpf_init(struct nfp_app *app)
{
	struct nfp_app_bpf *bpf;
	int err;

	bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
	if (!bpf)
		return -ENOMEM;
	bpf->app = app;
	app->priv = bpf;

363 364
	skb_queue_head_init(&bpf->cmsg_replies);
	init_waitqueue_head(&bpf->cmsg_wq);
365 366
	INIT_LIST_HEAD(&bpf->map_list);

367 368 369 370 371 372 373 374 375 376 377 378 379
	err = nfp_bpf_parse_capabilities(app);
	if (err)
		goto err_free_bpf;

	return 0;

err_free_bpf:
	kfree(bpf);
	return err;
}

static void nfp_bpf_clean(struct nfp_app *app)
{
380 381
	struct nfp_app_bpf *bpf = app->priv;

382
	WARN_ON(!skb_queue_empty(&bpf->cmsg_replies));
383
	WARN_ON(!list_empty(&bpf->map_list));
384
	WARN_ON(bpf->maps_in_use || bpf->map_elems_in_use);
385
	kfree(bpf);
386 387
}

J
Jakub Kicinski 已提交
388 389
const struct nfp_app_type app_bpf = {
	.id		= NFP_APP_BPF_NIC,
390
	.name		= "ebpf",
J
Jakub Kicinski 已提交
391

392
	.ctrl_cap_mask	= 0,
393

394 395 396
	.init		= nfp_bpf_init,
	.clean		= nfp_bpf_clean,

397 398
	.change_mtu	= nfp_bpf_change_mtu,

399 400
	.extra_cap	= nfp_bpf_extra_cap,

401 402
	.vnic_alloc	= nfp_bpf_vnic_alloc,
	.vnic_free	= nfp_bpf_vnic_free,
403

404 405
	.ctrl_msg_rx	= nfp_bpf_ctrl_msg_rx,

406 407
	.setup_tc	= nfp_bpf_setup_tc,
	.tc_busy	= nfp_bpf_tc_busy,
408
	.bpf		= nfp_ndo_bpf,
409
	.xdp_offload	= nfp_bpf_xdp_offload,
J
Jakub Kicinski 已提交
410
};