| 1 | /* |
| 2 | * eBPF RSS loader |
| 3 | * |
| 4 | * Developed by Daynix Computing LTD (http://www.daynix.com) |
| 5 | * |
| 6 | * Authors: |
| 7 | * Andrew Melnychenko <andrew@daynix.com> |
| 8 | * Yuri Benditovich <yuri.benditovich@daynix.com> |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 11 | * the COPYING file in the top-level directory. |
| 12 | */ |
| 13 | |
| 14 | #include "qemu/osdep.h" |
| 15 | #include "qemu/error-report.h" |
| 16 | #include "qapi/qapi-types-misc.h" |
| 17 | #include "qapi/qapi-commands-ebpf.h" |
| 18 | |
| 19 | #include <bpf/libbpf.h> |
| 20 | #include <bpf/bpf.h> |
| 21 | |
| 22 | #include "hw/virtio/virtio-net.h" /* VIRTIO_NET_RSS_MAX_TABLE_LEN */ |
| 23 | |
| 24 | #include "ebpf/ebpf_rss.h" |
| 25 | #include "ebpf/rss.bpf.skeleton.h" |
| 26 | #include "ebpf/ebpf.h" |
| 27 | |
| 28 | #include "trace.h" |
| 29 | |
| 30 | void ebpf_rss_init(struct EBPFRSSContext *ctx) |
| 31 | { |
| 32 | if (ctx != NULL) { |
| 33 | ctx->obj = NULL; |
| 34 | ctx->program_fd = -1; |
| 35 | ctx->map_configuration = -1; |
| 36 | ctx->map_toeplitz_key = -1; |
| 37 | ctx->map_indirections_table = -1; |
| 38 | |
| 39 | ctx->mmap_configuration = NULL; |
| 40 | ctx->mmap_toeplitz_key = NULL; |
| 41 | ctx->mmap_indirections_table = NULL; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | bool ebpf_rss_is_loaded(struct EBPFRSSContext *ctx) |
| 46 | { |
| 47 | return ctx != NULL && (ctx->obj != NULL || ctx->program_fd != -1); |
| 48 | } |
| 49 | |
| 50 | static bool ebpf_rss_mmap(struct EBPFRSSContext *ctx, Error **errp) |
| 51 | { |
| 52 | ctx->mmap_configuration = mmap(NULL, qemu_real_host_page_size(), |
| 53 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 54 | ctx->map_configuration, 0); |
| 55 | if (ctx->mmap_configuration == MAP_FAILED) { |
| 56 | trace_ebpf_rss_mmap_error(ctx, "configuration"); |
| 57 | error_setg(errp, "Unable to map eBPF configuration array"); |
| 58 | return false; |
| 59 | } |
| 60 | ctx->mmap_toeplitz_key = mmap(NULL, qemu_real_host_page_size(), |
| 61 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 62 | ctx->map_toeplitz_key, 0); |
| 63 | if (ctx->mmap_toeplitz_key == MAP_FAILED) { |
| 64 | trace_ebpf_rss_mmap_error(ctx, "toeplitz key"); |
| 65 | error_setg(errp, "Unable to map eBPF toeplitz array"); |
| 66 | goto toeplitz_fail; |
| 67 | } |
| 68 | ctx->mmap_indirections_table = mmap(NULL, qemu_real_host_page_size(), |
| 69 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 70 | ctx->map_indirections_table, 0); |
| 71 | if (ctx->mmap_indirections_table == MAP_FAILED) { |
| 72 | trace_ebpf_rss_mmap_error(ctx, "indirections table"); |
| 73 | error_setg(errp, "Unable to map eBPF indirection array"); |
| 74 | goto indirection_fail; |
| 75 | } |
| 76 | |
| 77 | trace_ebpf_rss_mmap(ctx, |
| 78 | ctx->mmap_configuration, |
| 79 | ctx->mmap_toeplitz_key, |
| 80 | ctx->mmap_indirections_table); |
| 81 | return true; |
| 82 | |
| 83 | indirection_fail: |
| 84 | munmap(ctx->mmap_toeplitz_key, qemu_real_host_page_size()); |
| 85 | ctx->mmap_toeplitz_key = NULL; |
| 86 | toeplitz_fail: |
| 87 | munmap(ctx->mmap_configuration, qemu_real_host_page_size()); |
| 88 | ctx->mmap_configuration = NULL; |
| 89 | |
| 90 | ctx->mmap_indirections_table = NULL; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | static void ebpf_rss_munmap(struct EBPFRSSContext *ctx) |
| 95 | { |
| 96 | munmap(ctx->mmap_indirections_table, qemu_real_host_page_size()); |
| 97 | munmap(ctx->mmap_toeplitz_key, qemu_real_host_page_size()); |
| 98 | munmap(ctx->mmap_configuration, qemu_real_host_page_size()); |
| 99 | |
| 100 | ctx->mmap_configuration = NULL; |
| 101 | ctx->mmap_toeplitz_key = NULL; |
| 102 | ctx->mmap_indirections_table = NULL; |
| 103 | } |
| 104 | |
| 105 | bool ebpf_rss_load(struct EBPFRSSContext *ctx, Error **errp) |
| 106 | { |
| 107 | struct rss_bpf *rss_bpf_ctx; |
| 108 | |
| 109 | g_assert(!ebpf_rss_is_loaded(ctx)); |
| 110 | |
| 111 | rss_bpf_ctx = rss_bpf__open(); |
| 112 | if (rss_bpf_ctx == NULL) { |
| 113 | trace_ebpf_rss_open_error(ctx); |
| 114 | error_setg(errp, "Unable to open eBPF RSS object"); |
| 115 | goto error; |
| 116 | } |
| 117 | |
| 118 | bpf_program__set_type(rss_bpf_ctx->progs.tun_rss_steering_prog, BPF_PROG_TYPE_SOCKET_FILTER); |
| 119 | |
| 120 | if (rss_bpf__load(rss_bpf_ctx)) { |
| 121 | trace_ebpf_rss_load_error(ctx); |
| 122 | error_setg(errp, "Unable to load eBPF program"); |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | ctx->obj = rss_bpf_ctx; |
| 127 | ctx->program_fd = bpf_program__fd( |
| 128 | rss_bpf_ctx->progs.tun_rss_steering_prog); |
| 129 | ctx->map_configuration = bpf_map__fd( |
| 130 | rss_bpf_ctx->maps.tap_rss_map_configurations); |
| 131 | ctx->map_indirections_table = bpf_map__fd( |
| 132 | rss_bpf_ctx->maps.tap_rss_map_indirection_table); |
| 133 | ctx->map_toeplitz_key = bpf_map__fd( |
| 134 | rss_bpf_ctx->maps.tap_rss_map_toeplitz_key); |
| 135 | |
| 136 | trace_ebpf_rss_load(ctx, |
| 137 | ctx->program_fd, |
| 138 | ctx->map_configuration, |
| 139 | ctx->map_indirections_table, |
| 140 | ctx->map_toeplitz_key); |
| 141 | if (!ebpf_rss_mmap(ctx, errp)) { |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | error: |
| 147 | rss_bpf__destroy(rss_bpf_ctx); |
| 148 | ctx->obj = NULL; |
| 149 | ctx->program_fd = -1; |
| 150 | ctx->map_configuration = -1; |
| 151 | ctx->map_toeplitz_key = -1; |
| 152 | ctx->map_indirections_table = -1; |
| 153 | |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | bool ebpf_rss_load_fds(struct EBPFRSSContext *ctx, int program_fd, |
| 158 | int config_fd, int toeplitz_fd, int table_fd, |
| 159 | Error **errp) |
| 160 | { |
| 161 | if (ebpf_rss_is_loaded(ctx)) { |
| 162 | error_setg(errp, "eBPF program is already loaded"); |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | if (program_fd < 0) { |
| 167 | error_setg(errp, "eBPF program FD is not open"); |
| 168 | return false; |
| 169 | } |
| 170 | if (config_fd < 0) { |
| 171 | error_setg(errp, "eBPF config FD is not open"); |
| 172 | return false; |
| 173 | } |
| 174 | if (toeplitz_fd < 0) { |
| 175 | error_setg(errp, "eBPF toeplitz FD is not open"); |
| 176 | return false; |
| 177 | } |
| 178 | if (table_fd < 0) { |
| 179 | error_setg(errp, "eBPF indirection FD is not open"); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | ctx->program_fd = program_fd; |
| 184 | ctx->map_configuration = config_fd; |
| 185 | ctx->map_toeplitz_key = toeplitz_fd; |
| 186 | ctx->map_indirections_table = table_fd; |
| 187 | |
| 188 | trace_ebpf_rss_load(ctx, |
| 189 | ctx->program_fd, |
| 190 | ctx->map_configuration, |
| 191 | ctx->map_indirections_table, |
| 192 | ctx->map_toeplitz_key); |
| 193 | |
| 194 | if (!ebpf_rss_mmap(ctx, errp)) { |
| 195 | ctx->program_fd = -1; |
| 196 | ctx->map_configuration = -1; |
| 197 | ctx->map_toeplitz_key = -1; |
| 198 | ctx->map_indirections_table = -1; |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | static void ebpf_rss_set_config(struct EBPFRSSContext *ctx, |
| 206 | struct EBPFRSSConfig *config) |
| 207 | { |
| 208 | memcpy(ctx->mmap_configuration, config, sizeof(*config)); |
| 209 | } |
| 210 | |
| 211 | static bool ebpf_rss_set_indirections_table(struct EBPFRSSContext *ctx, |
| 212 | uint16_t *indirections_table, |
| 213 | size_t len, |
| 214 | Error **errp) |
| 215 | { |
| 216 | char *cursor = ctx->mmap_indirections_table; |
| 217 | |
| 218 | if (len > VIRTIO_NET_RSS_MAX_TABLE_LEN) { |
| 219 | error_setg(errp, "Indirections table length %zu exceeds limit %d", |
| 220 | len, VIRTIO_NET_RSS_MAX_TABLE_LEN); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | for (size_t i = 0; i < len; i++) { |
| 225 | *(uint16_t *)cursor = indirections_table[i]; |
| 226 | cursor += 8; |
| 227 | } |
| 228 | |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | static void ebpf_rss_set_toepliz_key(struct EBPFRSSContext *ctx, |
| 233 | uint8_t *toeplitz_key) |
| 234 | { |
| 235 | /* prepare toeplitz key */ |
| 236 | uint8_t toe[VIRTIO_NET_RSS_MAX_KEY_SIZE] = {}; |
| 237 | |
| 238 | memcpy(toe, toeplitz_key, VIRTIO_NET_RSS_MAX_KEY_SIZE); |
| 239 | *(uint32_t *)toe = ntohl(*(uint32_t *)toe); |
| 240 | |
| 241 | memcpy(ctx->mmap_toeplitz_key, toe, VIRTIO_NET_RSS_MAX_KEY_SIZE); |
| 242 | } |
| 243 | |
| 244 | bool ebpf_rss_set_all(struct EBPFRSSContext *ctx, struct EBPFRSSConfig *config, |
| 245 | uint16_t *indirections_table, uint8_t *toeplitz_key, |
| 246 | Error **errp) |
| 247 | { |
| 248 | g_assert(ebpf_rss_is_loaded(ctx)); |
| 249 | |
| 250 | if (config == NULL) { |
| 251 | error_setg(errp, "eBPF config table is NULL"); |
| 252 | return false; |
| 253 | } |
| 254 | if (indirections_table == NULL) { |
| 255 | error_setg(errp, "eBPF indirections table is NULL"); |
| 256 | return false; |
| 257 | } |
| 258 | if (toeplitz_key == NULL) { |
| 259 | error_setg(errp, "eBPF toeplitz key is NULL"); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | ebpf_rss_set_config(ctx, config); |
| 264 | |
| 265 | if (!ebpf_rss_set_indirections_table(ctx, indirections_table, |
| 266 | config->indirections_len, |
| 267 | errp)) { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | ebpf_rss_set_toepliz_key(ctx, toeplitz_key); |
| 272 | |
| 273 | trace_ebpf_rss_set_data(ctx, config, toeplitz_key, indirections_table); |
| 274 | |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | void ebpf_rss_unload(struct EBPFRSSContext *ctx) |
| 279 | { |
| 280 | if (!ebpf_rss_is_loaded(ctx)) { |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | trace_ebpf_rss_unload(ctx); |
| 285 | |
| 286 | ebpf_rss_munmap(ctx); |
| 287 | |
| 288 | if (ctx->obj) { |
| 289 | rss_bpf__destroy(ctx->obj); |
| 290 | } else { |
| 291 | close(ctx->program_fd); |
| 292 | close(ctx->map_configuration); |
| 293 | close(ctx->map_toeplitz_key); |
| 294 | close(ctx->map_indirections_table); |
| 295 | } |
| 296 | |
| 297 | ctx->obj = NULL; |
| 298 | ctx->program_fd = -1; |
| 299 | ctx->map_configuration = -1; |
| 300 | ctx->map_toeplitz_key = -1; |
| 301 | ctx->map_indirections_table = -1; |
| 302 | } |
| 303 | |
| 304 | ebpf_binary_init(EBPF_PROGRAM_ID_RSS, rss_bpf__elf_bytes) |