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_init(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_init(app, nn, id); 110 if (ret) 111 kfree(priv); 112 113 return ret; 114 } 115 116 static 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); 120 kfree(nn->app_priv); 121 } 122 123 static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev, 124 u32 handle, __be16 proto, struct tc_to_netdev *tc) 125 { 126 struct nfp_net *nn = netdev_priv(netdev); 127 128 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS)) 129 return -EOPNOTSUPP; 130 if (proto != htons(ETH_P_ALL)) 131 return -EOPNOTSUPP; 132 133 if (tc->type == TC_SETUP_CLSBPF && nfp_net_ebpf_capable(nn)) { 134 if (!nn->dp.bpf_offload_xdp) 135 return nfp_net_bpf_offload(nn, tc->cls_bpf); 136 else 137 return -EBUSY; 138 } 139 140 return -EINVAL; 141 } 142 143 static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn) 144 { 145 return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF; 146 } 147 148 const struct nfp_app_type app_bpf = { 149 .id = NFP_APP_BPF_NIC, 150 .name = "ebpf", 151 152 .extra_cap = nfp_bpf_extra_cap, 153 154 .vnic_init = nfp_bpf_vnic_init, 155 .vnic_clean = nfp_bpf_vnic_clean, 156 157 .setup_tc = nfp_bpf_setup_tc, 158 .tc_busy = nfp_bpf_tc_busy, 159 .xdp_offload = nfp_bpf_xdp_offload, 160 }; 161