Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 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 | |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 34 | #include <net/pkt_cls.h> |
| 35 | |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 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" |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 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 | } |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 85 | |
| 86 | static int |
| 87 | nfp_bpf_vnic_init(struct nfp_app *app, struct nfp_net *nn, unsigned int id) |
| 88 | { |
Jakub Kicinski | c66a9cf | 2017-05-31 08:06:50 -0700 | [diff] [blame] | 89 | struct nfp_net_bpf_priv *priv; |
| 90 | int ret; |
| 91 | |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 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 | |
Jakub Kicinski | c66a9cf | 2017-05-31 08:06:50 -0700 | [diff] [blame] | 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; |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 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); |
Jakub Kicinski | c66a9cf | 2017-05-31 08:06:50 -0700 | [diff] [blame] | 120 | kfree(nn->app_priv); |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev, |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame^] | 124 | enum tc_setup_type type, |
Jiri Pirko | 2572ac5 | 2017-08-07 10:15:17 +0200 | [diff] [blame] | 125 | struct tc_to_netdev *tc) |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 126 | { |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame^] | 127 | struct tc_cls_bpf_offload *cls_bpf = tc->cls_bpf; |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 128 | struct nfp_net *nn = netdev_priv(netdev); |
| 129 | |
Jiri Pirko | 37cba6b | 2017-08-07 10:15:27 +0200 | [diff] [blame] | 130 | if (type != TC_SETUP_CLSBPF || !nfp_net_ebpf_capable(nn) || |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame^] | 131 | 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 Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 134 | return -EOPNOTSUPP; |
| 135 | |
Jiri Pirko | 37cba6b | 2017-08-07 10:15:27 +0200 | [diff] [blame] | 136 | if (nn->dp.bpf_offload_xdp) |
| 137 | return -EBUSY; |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 138 | |
Jiri Pirko | 5fd9fc4 | 2017-08-07 10:15:29 +0200 | [diff] [blame^] | 139 | return nfp_net_bpf_offload(nn, cls_bpf); |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static 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 Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 147 | const struct nfp_app_type app_bpf = { |
| 148 | .id = NFP_APP_BPF_NIC, |
Jakub Kicinski | 2707d6f | 2017-05-31 08:06:47 -0700 | [diff] [blame] | 149 | .name = "ebpf", |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 150 | |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 151 | .extra_cap = nfp_bpf_extra_cap, |
| 152 | |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 153 | .vnic_init = nfp_bpf_vnic_init, |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 154 | .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 Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 159 | }; |