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 | { |
Jakub Kicinski | 0f6cf4d | 2017-10-12 10:34:13 -0700 | [diff] [blame] | 45 | #ifdef __LITTLE_ENDIAN |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 46 | if (nn->cap & NFP_NET_CFG_CTRL_BPF && |
| 47 | nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI) |
| 48 | return true; |
Jakub Kicinski | 0f6cf4d | 2017-10-12 10:34:13 -0700 | [diff] [blame] | 49 | #endif |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | |
| 53 | static int |
| 54 | nfp_bpf_xdp_offload(struct nfp_app *app, struct nfp_net *nn, |
| 55 | struct bpf_prog *prog) |
| 56 | { |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 57 | bool running, xdp_running; |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 58 | int ret; |
| 59 | |
| 60 | if (!nfp_net_ebpf_capable(nn)) |
| 61 | return -EINVAL; |
| 62 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 63 | running = nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF; |
| 64 | xdp_running = running && nn->dp.bpf_offload_xdp; |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 65 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 66 | if (!prog && !xdp_running) |
| 67 | return 0; |
| 68 | if (prog && running && !xdp_running) |
| 69 | return -EBUSY; |
| 70 | |
Jakub Kicinski | e4a91cd | 2017-11-03 13:56:26 -0700 | [diff] [blame] | 71 | ret = nfp_net_bpf_offload(nn, prog, running); |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 72 | /* Stop offload if replace not possible */ |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 73 | if (ret && prog) |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 74 | nfp_bpf_xdp_offload(app, nn, NULL); |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 75 | |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 76 | nn->dp.bpf_offload_xdp = prog && !ret; |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | static const char *nfp_bpf_extra_cap(struct nfp_app *app, struct nfp_net *nn) |
| 81 | { |
| 82 | return nfp_net_ebpf_capable(nn) ? "BPF" : ""; |
| 83 | } |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 84 | |
Jakub Kicinski | c496291 | 2017-09-02 18:26:00 -0700 | [diff] [blame] | 85 | static void nfp_bpf_vnic_free(struct nfp_app *app, struct nfp_net *nn) |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 86 | { |
| 87 | if (nn->dp.bpf_offload_xdp) |
| 88 | nfp_bpf_xdp_offload(app, nn, NULL); |
| 89 | } |
| 90 | |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 91 | static int nfp_bpf_setup_tc_block_cb(enum tc_setup_type type, |
| 92 | void *type_data, void *cb_priv) |
| 93 | { |
| 94 | struct tc_cls_bpf_offload *cls_bpf = type_data; |
| 95 | struct nfp_net *nn = cb_priv; |
| 96 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 97 | if (type != TC_SETUP_CLSBPF || |
| 98 | !tc_can_offload(nn->dp.netdev) || |
| 99 | !nfp_net_ebpf_capable(nn) || |
| 100 | cls_bpf->common.protocol != htons(ETH_P_ALL) || |
| 101 | cls_bpf->common.chain_index) |
Jiri Pirko | 44ae12a | 2017-11-01 11:47:39 +0100 | [diff] [blame] | 102 | return -EOPNOTSUPP; |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 103 | if (nn->dp.bpf_offload_xdp) |
| 104 | return -EBUSY; |
Jiri Pirko | 44ae12a | 2017-11-01 11:47:39 +0100 | [diff] [blame] | 105 | |
Jakub Kicinski | 9ce7a95 | 2017-11-03 13:56:25 -0700 | [diff] [blame] | 106 | /* Only support TC direct action */ |
| 107 | if (!cls_bpf->exts_integrated || |
| 108 | tcf_exts_has_actions(cls_bpf->exts)) { |
| 109 | nn_err(nn, "only direct action with no legacy actions supported\n"); |
| 110 | return -EOPNOTSUPP; |
| 111 | } |
Jakub Kicinski | f449657 | 2017-11-02 01:31:31 -0700 | [diff] [blame] | 112 | |
Jakub Kicinski | 102740b | 2017-12-19 13:32:13 -0800 | [diff] [blame^] | 113 | if (cls_bpf->command != TC_CLSBPF_OFFLOAD) |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 114 | return -EOPNOTSUPP; |
Jakub Kicinski | 102740b | 2017-12-19 13:32:13 -0800 | [diff] [blame^] | 115 | |
| 116 | return nfp_net_bpf_offload(nn, cls_bpf->prog, cls_bpf->oldprog); |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int nfp_bpf_setup_tc_block(struct net_device *netdev, |
| 120 | struct tc_block_offload *f) |
| 121 | { |
| 122 | struct nfp_net *nn = netdev_priv(netdev); |
| 123 | |
| 124 | if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) |
| 125 | return -EOPNOTSUPP; |
| 126 | |
| 127 | switch (f->command) { |
| 128 | case TC_BLOCK_BIND: |
| 129 | return tcf_block_cb_register(f->block, |
| 130 | nfp_bpf_setup_tc_block_cb, |
| 131 | nn, nn); |
| 132 | case TC_BLOCK_UNBIND: |
| 133 | tcf_block_cb_unregister(f->block, |
| 134 | nfp_bpf_setup_tc_block_cb, |
| 135 | nn); |
| 136 | return 0; |
| 137 | default: |
| 138 | return -EOPNOTSUPP; |
| 139 | } |
| 140 | } |
| 141 | |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 142 | 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] | 143 | enum tc_setup_type type, void *type_data) |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 144 | { |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 145 | switch (type) { |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 146 | case TC_SETUP_BLOCK: |
| 147 | return nfp_bpf_setup_tc_block(netdev, type_data); |
| 148 | default: |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 149 | return -EOPNOTSUPP; |
Jiri Pirko | 90d9731 | 2017-10-19 15:50:44 +0200 | [diff] [blame] | 150 | } |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static bool nfp_bpf_tc_busy(struct nfp_app *app, struct nfp_net *nn) |
| 154 | { |
| 155 | return nn->dp.ctrl & NFP_NET_CFG_CTRL_BPF; |
| 156 | } |
| 157 | |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 158 | const struct nfp_app_type app_bpf = { |
| 159 | .id = NFP_APP_BPF_NIC, |
Jakub Kicinski | 2707d6f | 2017-05-31 08:06:47 -0700 | [diff] [blame] | 160 | .name = "ebpf", |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 161 | |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 162 | .extra_cap = nfp_bpf_extra_cap, |
| 163 | |
Jakub Kicinski | 012bb8a | 2017-11-03 13:56:22 -0700 | [diff] [blame] | 164 | .vnic_alloc = nfp_app_nic_vnic_alloc, |
Jakub Kicinski | c496291 | 2017-09-02 18:26:00 -0700 | [diff] [blame] | 165 | .vnic_free = nfp_bpf_vnic_free, |
Jakub Kicinski | bb45e51 | 2017-05-31 08:06:49 -0700 | [diff] [blame] | 166 | |
| 167 | .setup_tc = nfp_bpf_setup_tc, |
| 168 | .tc_busy = nfp_bpf_tc_busy, |
| 169 | .xdp_offload = nfp_bpf_xdp_offload, |
Jakub Kicinski | c6c580d | 2017-11-03 13:56:29 -0700 | [diff] [blame] | 170 | |
| 171 | .bpf_verifier_prep = nfp_bpf_verifier_prep, |
| 172 | .bpf_translate = nfp_bpf_translate, |
| 173 | .bpf_destroy = nfp_bpf_destroy, |
Jakub Kicinski | 8aa0cb0 | 2017-05-31 08:06:46 -0700 | [diff] [blame] | 174 | }; |