2 * Copyright (C) 2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <net/pkt_cls.h>
36 #include "../nfpcore/nfp_cpp.h"
37 #include "../nfp_app.h"
38 #include "../nfp_main.h"
39 #include "../nfp_net.h"
40 #include "../nfp_port.h"
43 static bool nfp_net_ebpf_capable(struct nfp_net *nn)
45 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
46 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
52 nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
53 struct bpf_prog *prog)
55 struct tc_cls_bpf_offload cmd = {
60 if (!nfp_net_ebpf_capable(nn))
63 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
64 if (!nn->dp.bpf_offload_xdp)
65 return prog ? -EBUSY : 0;
66 cmd.command = prog ? TC_CLSBPF_REPLACE : TC_CLSBPF_DESTROY;
70 cmd.command = TC_CLSBPF_ADD;
73 ret = nfp_net_bpf_offload(nn, &cmd);
74 /* Stop offload if replace not possible */
75 if (ret && cmd.command == TC_CLSBPF_REPLACE)
76 nfp_bpf_xdp_offload(app, nn, NULL);
77 nn->dp.bpf_offload_xdp = prog && !ret;
81 static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
83 return nfp_net_ebpf_capable(nn) ? "BPF" : "";
87 nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
89 struct nfp_net_bpf_priv *priv;
92 /* Limit to single port, otherwise it's just a NIC */
95 "BPF NIC doesn't support more than one port right now\n");
96 nn->port = nfp_port_alloc(app, NFP_PORT_INVALID, nn->dp.netdev);
97 return PTR_ERR_OR_ZERO(nn->port);
100 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
105 spin_lock_init(&priv->rx_filter_lock);
106 setup_timer(&priv->rx_filter_stats_timer,
107 nfp_net_filter_stats_timer, (unsigned long)nn);
109 ret = nfp_app_nic_vnic_alloc(app, nn, id);
116 static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
118 if (nn->dp.bpf_offload_xdp)
119 nfp_bpf_xdp_offload(app, nn, NULL);
123 static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
124 enum tc_setup_type type, void *type_data)
126 struct tc_cls_bpf_offload *cls_bpf = type_data;
127 struct nfp_net *nn = netdev_priv(netdev);
129 if (type != TC_SETUP_CLSBPF || !nfp_net_ebpf_capable(nn) ||
130 !is_classid_clsact_ingress(cls_bpf->common.classid) ||
131 cls_bpf->common.protocol != htons(ETH_P_ALL) ||
132 cls_bpf->common.chain_index)
135 if (nn->dp.bpf_offload_xdp)
138 return nfp_net_bpf_offload(nn, cls_bpf);
141 static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
143 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
146 const struct nfp_app_type app_bpf = {
147 .id = NFP_APP_BPF_NIC,
150 .extra_cap = nfp_bpf_extra_cap,
152 .vnic_alloc = nfp_bpf_vnic_alloc,
153 .vnic_free = nfp_bpf_vnic_free,
155 .setup_tc = nfp_bpf_setup_tc,
156 .tc_busy = nfp_bpf_tc_busy,
157 .xdp_offload = nfp_bpf_xdp_offload,