main.c 4.4 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 37 38 39 40
#include "../nfpcore/nfp_cpp.h"
#include "../nfp_app.h"
#include "../nfp_main.h"
#include "../nfp_net.h"
#include "../nfp_port.h"
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include "main.h"

static bool nfp_net_ebpf_capable(struct nfp_net *nn)
{
	if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
	    nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
		return true;
	return false;
}

static int
nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
		    struct bpf_prog *prog)
{
	struct tc_cls_bpf_offload cmd = {
		.prog = prog,
	};
	int ret;

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

	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
		if (!nn->dp.bpf_offload_xdp)
			return prog ? -EBUSY : 0;
		cmd.command = prog ? TC_CLSBPF_REPLACE : TC_CLSBPF_DESTROY;
	} else {
		if (!prog)
			return 0;
		cmd.command = TC_CLSBPF_ADD;
	}

	ret = nfp_net_bpf_offload(nn, &cmd);
	/* Stop offload if replace not possible */
	if (ret && cmd.command == TC_CLSBPF_REPLACE)
		nfp_bpf_xdp_offload(app, nn, NULL);
	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 已提交
85 86 87 88

static int
nfp_bpf_vnic_init(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
{
89 90 91
	struct nfp_net_bpf_priv *priv;
	int ret;

J
Jakub Kicinski 已提交
92 93 94 95 96 97 98 99
	/* Limit to single port, otherwise it's just a NIC */
	if (id > 0) {
		nfp_warn(app->cpp,
			 "BPF NIC doesn't support more than one port right now\n");
		nn->port = nfp_port_alloc(app, NFP_PORT_INVALID, nn->dp.netdev);
		return PTR_ERR_OR_ZERO(nn->port);
	}

100 101 102 103 104 105 106 107 108 109 110 111 112 113
	priv = kmalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	nn->app_priv = priv;
	spin_lock_init(&priv->rx_filter_lock);
	setup_timer(&priv->rx_filter_stats_timer,
		    nfp_net_filter_stats_timer, (unsigned long)nn);

	ret = nfp_app_nic_vnic_init(app, nn, id);
	if (ret)
		kfree(priv);

	return ret;
J
Jakub Kicinski 已提交
114 115
}

116 117 118 119
static void nfp_bpf_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
{
	if (nn->dp.bpf_offload_xdp)
		nfp_bpf_xdp_offload(app, nn, NULL);
120
	kfree(nn->app_priv);
121 122 123
}

static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
124
			    enum tc_setup_type type,
125
			    struct tc_to_netdev *tc)
126
{
127
	struct tc_cls_bpf_offload *cls_bpf = tc->cls_bpf;
128 129
	struct nfp_net *nn = netdev_priv(netdev);

130
	if (type != TC_SETUP_CLSBPF || !nfp_net_ebpf_capable(nn) ||
131 132 133
	    TC_H_MAJ(cls_bpf->common.handle) != TC_H_MAJ(TC_H_INGRESS) ||
	    cls_bpf->common.protocol != htons(ETH_P_ALL) ||
	    cls_bpf->common.chain_index)
134 135
		return -EOPNOTSUPP;

136 137
	if (nn->dp.bpf_offload_xdp)
		return -EBUSY;
138

139
	return nfp_net_bpf_offload(nn, cls_bpf);
140 141 142 143 144 145 146
}

static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
{
	return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
}

J
Jakub Kicinski 已提交
147 148
const struct nfp_app_type app_bpf = {
	.id		= NFP_APP_BPF_NIC,
149
	.name		= "ebpf",
J
Jakub Kicinski 已提交
150

151 152
	.extra_cap	= nfp_bpf_extra_cap,

J
Jakub Kicinski 已提交
153
	.vnic_init	= nfp_bpf_vnic_init,
154 155 156 157 158
	.vnic_clean	= nfp_bpf_vnic_clean,

	.setup_tc	= nfp_bpf_setup_tc,
	.tc_busy	= nfp_bpf_tc_busy,
	.xdp_offload	= nfp_bpf_xdp_offload,
J
Jakub Kicinski 已提交
159
};