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 "../nfpcore/nfp_nffw.h"
38 #include "../nfpcore/nfp_nsp.h"
39 #include "../nfp_app.h"
40 #include "../nfp_main.h"
41 #include "../nfp_net.h"
42 #include "../nfp_port.h"
46 static bool nfp_net_ebpf_capable(struct nfp_net *nn)
48 #ifdef __LITTLE_ENDIAN
49 if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
50 nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
57 nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
58 struct bpf_prog *prog, struct netlink_ext_ack *extack)
60 bool running, xdp_running;
63 if (!nfp_net_ebpf_capable(nn))
66 running = nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF;
67 xdp_running = running && nn->dp.bpf_offload_xdp;
69 if (!prog && !xdp_running)
71 if (prog && running && !xdp_running)
74 ret = nfp_net_bpf_offload(nn, prog, running, extack);
75 /* Stop offload if replace not possible */
77 nfp_bpf_xdp_offload(app, nn, NULL, extack);
79 nn->dp.bpf_offload_xdp = prog && !ret;
83 static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn)
85 return nfp_net_ebpf_capable(nn) ? "BPF" : "";
89 nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
91 struct nfp_pf *pf = app->pf;
92 struct nfp_bpf_vnic *bv;
96 nfp_err(pf->cpp, "No ETH table\n");
99 if (pf->max_data_vnics != pf->eth_tbl->count) {
100 nfp_err(pf->cpp, "ETH entries don't match vNICs (%d vs %d)\n",
101 pf->max_data_vnics, pf->eth_tbl->count);
105 bv = kzalloc(sizeof(*bv), GFP_KERNEL);
110 err = nfp_app_nic_vnic_alloc(app, nn, id);
114 bv->start_off = nn_readw(nn, NFP_NET_CFG_BPF_START);
115 bv->tgt_done = nn_readw(nn, NFP_NET_CFG_BPF_DONE);
123 static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn)
125 struct nfp_bpf_vnic *bv = nn->app_priv;
127 WARN_ON(bv->tc_prog);
131 static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type,
132 void *type_data, void *cb_priv)
134 struct tc_cls_bpf_offload *cls_bpf = type_data;
135 struct nfp_net *nn = cb_priv;
136 struct bpf_prog *oldprog;
137 struct nfp_bpf_vnic *bv;
140 if (type != TC_SETUP_CLSBPF) {
141 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
142 "only offload of BPF classifiers supported");
145 if (!tc_cls_can_offload_and_chain0(nn->dp.netdev, &cls_bpf->common))
147 if (!nfp_net_ebpf_capable(nn)) {
148 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
149 "NFP firmware does not support eBPF offload");
152 if (cls_bpf->common.protocol != htons(ETH_P_ALL)) {
153 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
154 "only ETH_P_ALL supported as filter protocol");
158 /* Only support TC direct action */
159 if (!cls_bpf->exts_integrated ||
160 tcf_exts_has_actions(cls_bpf->exts)) {
161 NL_SET_ERR_MSG_MOD(cls_bpf->common.extack,
162 "only direct action with no legacy actions supported");
166 if (cls_bpf->command != TC_CLSBPF_OFFLOAD)
170 oldprog = cls_bpf->oldprog;
172 /* Don't remove if oldprog doesn't match driver's state */
173 if (bv->tc_prog != oldprog) {
179 err = nfp_net_bpf_offload(nn, cls_bpf->prog, oldprog,
180 cls_bpf->common.extack);
184 bv->tc_prog = cls_bpf->prog;
185 nn->port->tc_offload_cnt = !!bv->tc_prog;
189 static int nfp_bpf_setup_tc_block(struct net_device *netdev,
190 struct tc_block_offload *f)
192 struct nfp_net *nn = netdev_priv(netdev);
194 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
197 switch (f->command) {
199 return tcf_block_cb_register(f->block,
200 nfp_bpf_setup_tc_block_cb,
202 case TC_BLOCK_UNBIND:
203 tcf_block_cb_unregister(f->block,
204 nfp_bpf_setup_tc_block_cb,
212 static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev,
213 enum tc_setup_type type, void *type_data)
217 return nfp_bpf_setup_tc_block(netdev, type_data);
224 nfp_bpf_change_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
226 struct nfp_net *nn = netdev_priv(netdev);
227 unsigned int max_mtu;
229 if (~nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF)
232 max_mtu = nn_readb(nn, NFP_NET_CFG_BPF_INL_MTU) * 64 - 32;
233 if (new_mtu > max_mtu) {
234 nn_info(nn, "BPF offload active, MTU over %u not supported\n",
242 nfp_bpf_parse_cap_adjust_head(struct nfp_app_bpf *bpf, void __iomem *value,
245 struct nfp_bpf_cap_tlv_adjust_head __iomem *cap = value;
246 struct nfp_cpp *cpp = bpf->app->pf->cpp;
248 if (length < sizeof(*cap)) {
249 nfp_err(cpp, "truncated adjust_head TLV: %d\n", length);
253 bpf->adjust_head.flags = readl(&cap->flags);
254 bpf->adjust_head.off_min = readl(&cap->off_min);
255 bpf->adjust_head.off_max = readl(&cap->off_max);
256 bpf->adjust_head.guaranteed_sub = readl(&cap->guaranteed_sub);
257 bpf->adjust_head.guaranteed_add = readl(&cap->guaranteed_add);
259 if (bpf->adjust_head.off_min > bpf->adjust_head.off_max) {
260 nfp_err(cpp, "invalid adjust_head TLV: min > max\n");
263 if (!FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_min) ||
264 !FIELD_FIT(UR_REG_IMM_MAX, bpf->adjust_head.off_max)) {
265 nfp_warn(cpp, "disabling adjust_head - driver expects min/max to fit in as immediates\n");
266 memset(&bpf->adjust_head, 0, sizeof(bpf->adjust_head));
274 nfp_bpf_parse_cap_func(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
276 struct nfp_bpf_cap_tlv_func __iomem *cap = value;
278 if (length < sizeof(*cap)) {
279 nfp_err(bpf->app->cpp, "truncated function TLV: %d\n", length);
283 switch (readl(&cap->func_id)) {
284 case BPF_FUNC_map_lookup_elem:
285 bpf->helpers.map_lookup = readl(&cap->func_addr);
293 nfp_bpf_parse_cap_maps(struct nfp_app_bpf *bpf, void __iomem *value, u32 length)
295 struct nfp_bpf_cap_tlv_maps __iomem *cap = value;
297 if (length < sizeof(*cap)) {
298 nfp_err(bpf->app->cpp, "truncated maps TLV: %d\n", length);
302 bpf->maps.types = readl(&cap->types);
303 bpf->maps.max_maps = readl(&cap->max_maps);
304 bpf->maps.max_elems = readl(&cap->max_elems);
305 bpf->maps.max_key_sz = readl(&cap->max_key_sz);
306 bpf->maps.max_val_sz = readl(&cap->max_val_sz);
307 bpf->maps.max_elem_sz = readl(&cap->max_elem_sz);
312 static int nfp_bpf_parse_capabilities(struct nfp_app *app)
314 struct nfp_cpp *cpp = app->pf->cpp;
315 struct nfp_cpp_area *area;
316 u8 __iomem *mem, *start;
318 mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap",
321 return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem);
324 while (mem - start + 8 < nfp_cpp_area_size(area)) {
329 length = readl(mem + 4);
333 if (mem - start > nfp_cpp_area_size(area))
334 goto err_release_free;
337 case NFP_BPF_CAP_TYPE_FUNC:
338 if (nfp_bpf_parse_cap_func(app->priv, value, length))
339 goto err_release_free;
341 case NFP_BPF_CAP_TYPE_ADJUST_HEAD:
342 if (nfp_bpf_parse_cap_adjust_head(app->priv, value,
344 goto err_release_free;
346 case NFP_BPF_CAP_TYPE_MAPS:
347 if (nfp_bpf_parse_cap_maps(app->priv, value, length))
348 goto err_release_free;
351 nfp_dbg(cpp, "unknown BPF capability: %d\n", type);
355 if (mem - start != nfp_cpp_area_size(area)) {
356 nfp_err(cpp, "BPF capabilities left after parsing, parsed:%zd total length:%zu\n",
357 mem - start, nfp_cpp_area_size(area));
358 goto err_release_free;
361 nfp_cpp_area_release_free(area);
366 nfp_err(cpp, "invalid BPF capabilities at offset:%zd\n", mem - start);
367 nfp_cpp_area_release_free(area);
371 static int nfp_bpf_init(struct nfp_app *app)
373 struct nfp_app_bpf *bpf;
376 bpf = kzalloc(sizeof(*bpf), GFP_KERNEL);
382 skb_queue_head_init(&bpf->cmsg_replies);
383 init_waitqueue_head(&bpf->cmsg_wq);
384 INIT_LIST_HEAD(&bpf->map_list);
386 err = nfp_bpf_parse_capabilities(app);
397 static void nfp_bpf_clean(struct nfp_app *app)
399 struct nfp_app_bpf *bpf = app->priv;
401 WARN_ON(!skb_queue_empty(&bpf->cmsg_replies));
402 WARN_ON(!list_empty(&bpf->map_list));
403 WARN_ON(bpf->maps_in_use || bpf->map_elems_in_use);
407 const struct nfp_app_type app_bpf = {
408 .id = NFP_APP_BPF_NIC,
413 .init = nfp_bpf_init,
414 .clean = nfp_bpf_clean,
416 .change_mtu = nfp_bpf_change_mtu,
418 .extra_cap = nfp_bpf_extra_cap,
420 .vnic_alloc = nfp_bpf_vnic_alloc,
421 .vnic_free = nfp_bpf_vnic_free,
423 .ctrl_msg_rx = nfp_bpf_ctrl_msg_rx,
425 .setup_tc = nfp_bpf_setup_tc,
427 .xdp_offload = nfp_bpf_xdp_offload,