blob: 152a7abb58ed086439b785a845d9665491b47c7f [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"
37#include "../nfp_app.h"
38#include "../nfp_main.h"
39#include "../nfp_net.h"
40#include "../nfp_port.h"
Jakub Kicinskibb45e512017-05-31 08:06:49 -070041#include "main.h"
42
43static bool nfp_net_ebpf_capable(struct nfp_net *nn)
44{
45 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
46 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
47 return true;
48 return false;
49}
50
51static int
52nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
53 struct bpf_prog *prog)
54{
55 struct tc_cls_bpf_offload cmd = {
56 .prog = prog,
57 };
58 int ret;
59
60 if (!nfp_net_ebpf_capable(nn))
61 return -EINVAL;
62
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;
67 } else {
68 if (!prog)
69 return 0;
70 cmd.command = TC_CLSBPF_ADD;
71 }
72
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;
78 return ret;
79}
80
81static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
82{
83 return nfp_net_ebpf_capable(nn) ? "BPF" : "";
84}
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070085
86static int
87nfp_bpf_vnic_init(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
88{
Jakub Kicinskic66a9cf2017-05-31 08:06:50 -070089 struct nfp_net_bpf_priv *priv;
90 int ret;
91
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070092 /* Limit to single port, otherwise it's just a NIC */
93 if (id > 0) {
94 nfp_warn(app->cpp,
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);
98 }
99
Jakub Kicinskic66a9cf2017-05-31 08:06:50 -0700100 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
101 if (!priv)
102 return -ENOMEM;
103
104 nn->app_priv = priv;
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);
108
109 ret = nfp_app_nic_vnic_init(app, nn, id);
110 if (ret)
111 kfree(priv);
112
113 return ret;
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700114}
115
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700116static void nfp_bpf_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
117{
118 if (nn->dp.bpf_offload_xdp)
119 nfp_bpf_xdp_offload(app, nn, NULL);
Jakub Kicinskic66a9cf2017-05-31 08:06:50 -0700120 kfree(nn->app_priv);
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700121}
122
123static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200124 enum tc_setup_type type,
Jiri Pirko2572ac52017-08-07 10:15:17 +0200125 struct tc_to_netdev *tc)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700126{
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200127 struct tc_cls_bpf_offload *cls_bpf = tc->cls_bpf;
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700128 struct nfp_net *nn = netdev_priv(netdev);
129
Jiri Pirko37cba6b2017-08-07 10:15:27 +0200130 if (type != TC_SETUP_CLSBPF || !nfp_net_ebpf_capable(nn) ||
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200131 TC_H_MAJ(cls_bpf->common.handle) != TC_H_MAJ(TC_H_INGRESS) ||
132 cls_bpf->common.protocol != htons(ETH_P_ALL) ||
133 cls_bpf->common.chain_index)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700134 return -EOPNOTSUPP;
135
Jiri Pirko37cba6b2017-08-07 10:15:27 +0200136 if (nn->dp.bpf_offload_xdp)
137 return -EBUSY;
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700138
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200139 return nfp_net_bpf_offload(nn, cls_bpf);
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700140}
141
142static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
143{
144 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
145}
146
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700147const struct nfp_app_type app_bpf = {
148 .id = NFP_APP_BPF_NIC,
Jakub Kicinski2707d6f2017-05-31 08:06:47 -0700149 .name = "ebpf",
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700150
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700151 .extra_cap = nfp_bpf_extra_cap,
152
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700153 .vnic_init = nfp_bpf_vnic_init,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700154 .vnic_clean = nfp_bpf_vnic_clean,
155
156 .setup_tc = nfp_bpf_setup_tc,
157 .tc_busy = nfp_bpf_tc_busy,
158 .xdp_offload = nfp_bpf_xdp_offload,
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700159};