1 /* 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 34 #include <net/pkt_cls.h> 35 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" 41 #include "main.h" 42 43 static 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 51 static int 52 nfp_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 81 static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn) 82 { 83 return nfp_net_ebpf_capable(nn) ? "BPF" : ""; 84 } 85 86 static int 87 nfp_bpf_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id) 88 { 89 struct nfp_net_bpf_priv *priv; 90 int ret; 91 92 /* 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 100 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_alloc(app, nn, id); 110 if (ret) 111 kfree(priv); 112 113 return ret; 114 } 115 116 static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn) 117 { 118 if (nn->dp.bpf_offload_xdp) 119 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, void *type_data) 125 { 126 struct tc_cls_bpf_offload *cls_bpf = type_data; 127 struct nfp_net *nn = netdev_priv(netdev); 128 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) 133 return -EOPNOTSUPP; 134 135 if (nn->dp.bpf_offload_xdp) 136 return -EBUSY; 137 138 return nfp_net_bpf_offload(nn, cls_bpf); 139 } 140 141 static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn) 142 { 143 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF; 144 } 145 146 const struct nfp_app_type app_bpf = { 147 .id = NFP_APP_BPF_NIC, 148 .name = "ebpf", 149 150 .extra_cap = nfp_bpf_extra_cap, 151 152 .vnic_alloc = nfp_bpf_vnic_alloc, 153 .vnic_free = nfp_bpf_vnic_free, 154 155 .setup_tc = nfp_bpf_setup_tc, 156 .tc_busy = nfp_bpf_tc_busy, 157 .xdp_offload = nfp_bpf_xdp_offload, 158 }; 159