blob: 64f97b3f89492e06171de0d0c1df9309a27beede [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{
Jakub Kicinski0f6cf4d2017-10-12 10:34:13 -070045#ifdef __LITTLE_ENDIAN
Jakub Kicinskibb45e512017-05-31 08:06:49 -070046 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
47 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
48 return true;
Jakub Kicinski0f6cf4d2017-10-12 10:34:13 -070049#endif
Jakub Kicinskibb45e512017-05-31 08:06:49 -070050 return false;
51}
52
53static int
54nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
55 struct bpf_prog *prog)
56{
57 struct tc_cls_bpf_offload cmd = {
58 .prog = prog,
59 };
60 int ret;
61
62 if (!nfp_net_ebpf_capable(nn))
63 return -EINVAL;
64
65 if (nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF) {
66 if (!nn->dp.bpf_offload_xdp)
67 return prog ? -EBUSY : 0;
68 cmd.command = prog ? TC_CLSBPF_REPLACE : TC_CLSBPF_DESTROY;
69 } else {
70 if (!prog)
71 return 0;
72 cmd.command = TC_CLSBPF_ADD;
73 }
74
75 ret = nfp_net_bpf_offload(nn, &cmd);
76 /* Stop offload if replace not possible */
77 if (ret && cmd.command == TC_CLSBPF_REPLACE)
78 nfp_bpf_xdp_offload(app, nn, NULL);
79 nn->dp.bpf_offload_xdp = prog && !ret;
80 return ret;
81}
82
83static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
84{
85 return nfp_net_ebpf_capable(nn) ? "BPF" : "";
86}
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070087
88static int
Jakub Kicinskic4962912017-09-02 18:26:00 -070089nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -070090{
Jakub Kicinskic66a9cf2017-05-31 08:06:50 -070091 struct nfp_net_bpf_priv *priv;
92 int ret;
93
Jakub Kicinskic66a9cf2017-05-31 08:06:50 -070094 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
95 if (!priv)
96 return -ENOMEM;
97
98 nn->app_priv = priv;
99 spin_lock_init(&priv->rx_filter_lock);
100 setup_timer(&priv->rx_filter_stats_timer,
101 nfp_net_filter_stats_timer, (unsigned long)nn);
102
Jakub Kicinskic4962912017-09-02 18:26:00 -0700103 ret = nfp_app_nic_vnic_alloc(app, nn, id);
Jakub Kicinskic66a9cf2017-05-31 08:06:50 -0700104 if (ret)
105 kfree(priv);
106
107 return ret;
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700108}
109
Jakub Kicinskic4962912017-09-02 18:26:00 -0700110static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700111{
112 if (nn->dp.bpf_offload_xdp)
113 nfp_bpf_xdp_offload(app, nn, NULL);
Jakub Kicinskic66a9cf2017-05-31 08:06:50 -0700114 kfree(nn->app_priv);
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700115}
116
Jiri Pirko90d97312017-10-19 15:50:44 +0200117static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
118 void *type_data, void *cb_priv)
119{
120 struct tc_cls_bpf_offload *cls_bpf = type_data;
121 struct nfp_net *nn = cb_priv;
122
123 switch (type) {
124 case TC_SETUP_CLSBPF:
125 if (!nfp_net_ebpf_capable(nn) ||
126 cls_bpf->common.protocol != htons(ETH_P_ALL) ||
127 cls_bpf->common.chain_index)
128 return -EOPNOTSUPP;
129 return nfp_net_bpf_offload(nn, cls_bpf);
130 default:
131 return -EOPNOTSUPP;
132 }
133}
134
135static int nfp_bpf_setup_tc_block(struct net_device *netdev,
136 struct tc_block_offload *f)
137{
138 struct nfp_net *nn = netdev_priv(netdev);
139
140 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
141 return -EOPNOTSUPP;
142
143 switch (f->command) {
144 case TC_BLOCK_BIND:
145 return tcf_block_cb_register(f->block,
146 nfp_bpf_setup_tc_block_cb,
147 nn, nn);
148 case TC_BLOCK_UNBIND:
149 tcf_block_cb_unregister(f->block,
150 nfp_bpf_setup_tc_block_cb,
151 nn);
152 return 0;
153 default:
154 return -EOPNOTSUPP;
155 }
156}
157
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700158static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200159 enum tc_setup_type type, void *type_data)
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700160{
Jiri Pirko90d97312017-10-19 15:50:44 +0200161 switch (type) {
162 case TC_SETUP_CLSBPF:
163 return 0; /* will be removed after conversion from ndo */
164 case TC_SETUP_BLOCK:
165 return nfp_bpf_setup_tc_block(netdev, type_data);
166 default:
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700167 return -EOPNOTSUPP;
Jiri Pirko90d97312017-10-19 15:50:44 +0200168 }
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700169}
170
171static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn)
172{
173 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
174}
175
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700176const struct nfp_app_type app_bpf = {
177 .id = NFP_APP_BPF_NIC,
Jakub Kicinski2707d6f2017-05-31 08:06:47 -0700178 .name = "ebpf",
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700179
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700180 .extra_cap = nfp_bpf_extra_cap,
181
Jakub Kicinskic4962912017-09-02 18:26:00 -0700182 .vnic_alloc = nfp_bpf_vnic_alloc,
183 .vnic_free = nfp_bpf_vnic_free,
Jakub Kicinskibb45e512017-05-31 08:06:49 -0700184
185 .setup_tc = nfp_bpf_setup_tc,
186 .tc_busy = nfp_bpf_tc_busy,
187 .xdp_offload = nfp_bpf_xdp_offload,
Jakub Kicinski8aa0cb02017-05-31 08:06:46 -0700188};