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" |
Jakub Kicinski | 77a844e | 2017-12-14 21:29:16 -0800 | [diff] [blame] | 37 | #include "../nfpcore/nfp_nffw.h" |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 38 | #include "../nfp_app.h" |
| 39 | #include "../nfp_main.h" |
| 40 | #include "../nfp_net.h" |
| 41 | #include "../nfp_port.h" |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 42 | #include "main.h" |
| 43 | |
| 44 | static bool nfp_net_ebpf_capable(struct nfp_net *nn) |
| 45 | { |
Jakub Kicinski | 0f6cf4d | 2017-10-12 10:34:13 -0700 | [diff] [blame] | 46 | #ifdef __LITTLE_ENDIAN |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 47 | if (nn->cap & NFP_NET_CFG_CTRL_BPF && |
| 48 | nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI) |
| 49 | return true; |
Jakub Kicinski | 0f6cf4d | 2017-10-12 10:34:13 -0700 | [diff] [blame] | 50 | #endif |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn, |
| 56 | struct bpf_prog *prog) |
| 57 | { |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 58 | bool running, xdp_running; |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 59 | int ret; |
| 60 | |
| 61 | if (!nfp_net_ebpf_capable(nn)) |
| 62 | return -EINVAL; |
| 63 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 64 | running = nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF; |
| 65 | xdp_running = running && nn->dp.bpf_offload_xdp; |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 66 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 67 | if (!prog && !xdp_running) |
| 68 | return 0; |
| 69 | if (prog && running && !xdp_running) |
| 70 | return -EBUSY; |
| 71 | |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame] | 72 | ret = nfp_net_bpf_offload(nn, prog, running); |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 73 | /* Stop offload if replace not possible */ |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 74 | if (ret && prog) |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 75 | nfp_bpf_xdp_offload(app, nn, NULL); |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 76 | |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 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 | |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 86 | static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type, |
| 87 | void *type_data, void *cb_priv) |
| 88 | { |
| 89 | struct tc_cls_bpf_offload *cls_bpf = type_data; |
| 90 | struct nfp_net *nn = cb_priv; |
| 91 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 92 | if (type != TC_SETUP_CLSBPF || |
| 93 | !tc_can_offload(nn->dp.netdev) || |
| 94 | !nfp_net_ebpf_capable(nn) || |
| 95 | cls_bpf->common.protocol != htons(ETH_P_ALL) || |
| 96 | cls_bpf->common.chain_index) |
Jiri Pirko | 44ae12a | 2017-11-01 11:47:39 +0100 | [diff] [blame] | 97 | return -EOPNOTSUPP; |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 98 | if (nn->dp.bpf_offload_xdp) |
| 99 | return -EBUSY; |
Jiri Pirko | 44ae12a | 2017-11-01 11:47:39 +0100 | [diff] [blame] | 100 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 101 | /* Only support TC direct action */ |
| 102 | if (!cls_bpf->exts_integrated || |
| 103 | tcf_exts_has_actions(cls_bpf->exts)) { |
| 104 | nn_err(nn, "only direct action with no legacy actions supported\n"); |
| 105 | return -EOPNOTSUPP; |
| 106 | } |
Jakub Kicinski | f449657 | 2017-11-02 01:31:31 -0700 | [diff] [blame] | 107 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 108 | switch (cls_bpf->command) { |
| 109 | case TC_CLSBPF_REPLACE: |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame] | 110 | return nfp_net_bpf_offload(nn, cls_bpf->prog, true); |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 111 | case TC_CLSBPF_ADD: |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame] | 112 | return nfp_net_bpf_offload(nn, cls_bpf->prog, false); |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 113 | case TC_CLSBPF_DESTROY: |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame] | 114 | return nfp_net_bpf_offload(nn, NULL, true); |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 115 | default: |
| 116 | return -EOPNOTSUPP; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | static int nfp_bpf_setup_tc_block(struct net_device *netdev, |
| 121 | struct tc_block_offload *f) |
| 122 | { |
| 123 | struct nfp_net *nn = netdev_priv(netdev); |
| 124 | |
| 125 | if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) |
| 126 | return -EOPNOTSUPP; |
| 127 | |
| 128 | switch (f->command) { |
| 129 | case TC_BLOCK_BIND: |
| 130 | return tcf_block_cb_register(f->block, |
| 131 | nfp_bpf_setup_tc_block_cb, |
| 132 | nn, nn); |
| 133 | case TC_BLOCK_UNBIND: |
| 134 | tcf_block_cb_unregister(f->block, |
| 135 | nfp_bpf_setup_tc_block_cb, |
| 136 | nn); |
| 137 | return 0; |
| 138 | default: |
| 139 | return -EOPNOTSUPP; |
| 140 | } |
| 141 | } |
| 142 | |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 143 | static int nfp_bpf_setup_tc(struct nfp_app *app, struct net_device *netdev, |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 144 | enum tc_setup_type type, void *type_data) |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 145 | { |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 146 | switch (type) { |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 147 | case TC_SETUP_BLOCK: |
| 148 | return nfp_bpf_setup_tc_block(netdev, type_data); |
| 149 | default: |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 150 | return -EOPNOTSUPP; |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 151 | } |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn) |
| 155 | { |
| 156 | return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF; |
| 157 | } |
| 158 | |
Jakub Kicinski | 77a844e | 2017-12-14 21:29:16 -0800 | [diff] [blame] | 159 | static int nfp_bpf_parse_capabilities(struct nfp_app *app) |
| 160 | { |
| 161 | struct nfp_cpp *cpp = app->pf->cpp; |
| 162 | struct nfp_cpp_area *area; |
| 163 | u8 __iomem *mem, *start; |
| 164 | |
| 165 | mem = nfp_rtsym_map(app->pf->rtbl, "_abi_bpf_capabilities", "bpf.cap", |
| 166 | 8, &area); |
| 167 | if (IS_ERR(mem)) |
| 168 | return PTR_ERR(mem) == -ENOENT ? 0 : PTR_ERR(mem); |
| 169 | |
| 170 | start = mem; |
| 171 | while (mem - start + 8 < nfp_cpp_area_size(area)) { |
| 172 | u32 type, length; |
| 173 | |
| 174 | type = readl(mem); |
| 175 | length = readl(mem + 4); |
| 176 | |
| 177 | mem += 8 + length; |
| 178 | if (mem - start > nfp_cpp_area_size(area)) |
| 179 | goto err_release_free; |
| 180 | |
| 181 | switch (type) { |
| 182 | default: |
| 183 | nfp_dbg(cpp, "unknown BPF capability: %d\n", type); |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | if (mem - start != nfp_cpp_area_size(area)) { |
| 188 | nfp_err(cpp, "BPF capabilities left after parsing, parsed:%lu total length:%lu\n", |
| 189 | mem - start, nfp_cpp_area_size(area)); |
| 190 | goto err_release_free; |
| 191 | } |
| 192 | |
| 193 | nfp_cpp_area_release_free(area); |
| 194 | |
| 195 | return 0; |
| 196 | |
| 197 | err_release_free: |
| 198 | nfp_err(cpp, "invalid BPF capabilities at offset:%ld\n", mem - start); |
| 199 | nfp_cpp_area_release_free(area); |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
| 203 | static int nfp_bpf_init(struct nfp_app *app) |
| 204 | { |
| 205 | struct nfp_app_bpf *bpf; |
| 206 | int err; |
| 207 | |
| 208 | bpf = kzalloc(sizeof(*bpf), GFP_KERNEL); |
| 209 | if (!bpf) |
| 210 | return -ENOMEM; |
| 211 | bpf->app = app; |
| 212 | app->priv = bpf; |
| 213 | |
| 214 | err = nfp_bpf_parse_capabilities(app); |
| 215 | if (err) |
| 216 | goto err_free_bpf; |
| 217 | |
| 218 | return 0; |
| 219 | |
| 220 | err_free_bpf: |
| 221 | kfree(bpf); |
| 222 | return err; |
| 223 | } |
| 224 | |
| 225 | static void nfp_bpf_clean(struct nfp_app *app) |
| 226 | { |
| 227 | kfree(app->priv); |
| 228 | } |
| 229 | |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 230 | const struct nfp_app_type app_bpf = { |
| 231 | .id = NFP_APP_BPF_NIC, |
Jakub Kicinski | 2707d6f | 2017-05-31 08:06:47 -0700 | [diff] [blame] | 232 | .name = "ebpf", |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 233 | |
Jakub Kicinski | 77a844e | 2017-12-14 21:29:16 -0800 | [diff] [blame] | 234 | .init = nfp_bpf_init, |
| 235 | .clean = nfp_bpf_clean, |
| 236 | |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 237 | .extra_cap = nfp_bpf_extra_cap, |
| 238 | |
Jakub Kicinski | 012bb8a | 2017-11-03 13:56:22 -0700 | [diff] [blame] | 239 | .vnic_alloc = nfp_app_nic_vnic_alloc, |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 240 | |
| 241 | .setup_tc = nfp_bpf_setup_tc, |
| 242 | .tc_busy = nfp_bpf_tc_busy, |
| 243 | .xdp_offload = nfp_bpf_xdp_offload, |
Jakub Kicinski | c6c580d | 2017-11-03 13:56:29 -0700 | [diff] [blame] | 244 | |
| 245 | .bpf_verifier_prep = nfp_bpf_verifier_prep, |
| 246 | .bpf_translate = nfp_bpf_translate, |
| 247 | .bpf_destroy = nfp_bpf_destroy, |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 248 | }; |