Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / ebpf / ebpf_rss.c
blobd102f3dd0929ba17452f7ad654a7ae62b0bc65a9
1 /*
2 * eBPF RSS loader
4 * Developed by Daynix Computing LTD (http://www.daynix.com)
6 * Authors:
7 * Andrew Melnychenko <andrew@daynix.com>
8 * Yuri Benditovich <yuri.benditovich@daynix.com>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
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"
19 #include <bpf/libbpf.h>
20 #include <bpf/bpf.h>
22 #include "hw/virtio/virtio-net.h" /* VIRTIO_NET_RSS_MAX_TABLE_LEN */
24 #include "ebpf/ebpf_rss.h"
25 #include "ebpf/rss.bpf.skeleton.h"
26 #include "ebpf/ebpf.h"
28 void ebpf_rss_init(struct EBPFRSSContext *ctx)
30 if (ctx != NULL) {
31 ctx->obj = NULL;
32 ctx->program_fd = -1;
33 ctx->map_configuration = -1;
34 ctx->map_toeplitz_key = -1;
35 ctx->map_indirections_table = -1;
37 ctx->mmap_configuration = NULL;
38 ctx->mmap_toeplitz_key = NULL;
39 ctx->mmap_indirections_table = NULL;
43 bool ebpf_rss_is_loaded(struct EBPFRSSContext *ctx)
45 return ctx != NULL && (ctx->obj != NULL || ctx->program_fd != -1);
48 static bool ebpf_rss_mmap(struct EBPFRSSContext *ctx)
50 if (!ebpf_rss_is_loaded(ctx)) {
51 return false;
54 ctx->mmap_configuration = mmap(NULL, qemu_real_host_page_size(),
55 PROT_READ | PROT_WRITE, MAP_SHARED,
56 ctx->map_configuration, 0);
57 if (ctx->mmap_configuration == MAP_FAILED) {
58 return false;
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 goto toeplitz_fail;
66 ctx->mmap_indirections_table = mmap(NULL, qemu_real_host_page_size(),
67 PROT_READ | PROT_WRITE, MAP_SHARED,
68 ctx->map_indirections_table, 0);
69 if (ctx->mmap_indirections_table == MAP_FAILED) {
70 goto indirection_fail;
73 return true;
75 indirection_fail:
76 munmap(ctx->mmap_toeplitz_key, qemu_real_host_page_size());
77 ctx->mmap_toeplitz_key = NULL;
78 toeplitz_fail:
79 munmap(ctx->mmap_configuration, qemu_real_host_page_size());
80 ctx->mmap_configuration = NULL;
82 ctx->mmap_indirections_table = NULL;
83 return false;
86 static void ebpf_rss_munmap(struct EBPFRSSContext *ctx)
88 if (!ebpf_rss_is_loaded(ctx)) {
89 return;
92 munmap(ctx->mmap_indirections_table, qemu_real_host_page_size());
93 munmap(ctx->mmap_toeplitz_key, qemu_real_host_page_size());
94 munmap(ctx->mmap_configuration, qemu_real_host_page_size());
96 ctx->mmap_configuration = NULL;
97 ctx->mmap_toeplitz_key = NULL;
98 ctx->mmap_indirections_table = NULL;
101 bool ebpf_rss_load(struct EBPFRSSContext *ctx)
103 struct rss_bpf *rss_bpf_ctx;
105 if (ebpf_rss_is_loaded(ctx)) {
106 return false;
109 rss_bpf_ctx = rss_bpf__open();
110 if (rss_bpf_ctx == NULL) {
111 goto error;
114 bpf_program__set_type(rss_bpf_ctx->progs.tun_rss_steering_prog, BPF_PROG_TYPE_SOCKET_FILTER);
116 if (rss_bpf__load(rss_bpf_ctx)) {
117 goto error;
120 ctx->obj = rss_bpf_ctx;
121 ctx->program_fd = bpf_program__fd(
122 rss_bpf_ctx->progs.tun_rss_steering_prog);
123 ctx->map_configuration = bpf_map__fd(
124 rss_bpf_ctx->maps.tap_rss_map_configurations);
125 ctx->map_indirections_table = bpf_map__fd(
126 rss_bpf_ctx->maps.tap_rss_map_indirection_table);
127 ctx->map_toeplitz_key = bpf_map__fd(
128 rss_bpf_ctx->maps.tap_rss_map_toeplitz_key);
130 if (!ebpf_rss_mmap(ctx)) {
131 goto error;
134 return true;
135 error:
136 rss_bpf__destroy(rss_bpf_ctx);
137 ctx->obj = NULL;
138 ctx->program_fd = -1;
139 ctx->map_configuration = -1;
140 ctx->map_toeplitz_key = -1;
141 ctx->map_indirections_table = -1;
143 return false;
146 bool ebpf_rss_load_fds(struct EBPFRSSContext *ctx, int program_fd,
147 int config_fd, int toeplitz_fd, int table_fd)
149 if (ebpf_rss_is_loaded(ctx)) {
150 return false;
153 if (program_fd < 0 || config_fd < 0 || toeplitz_fd < 0 || table_fd < 0) {
154 return false;
157 ctx->program_fd = program_fd;
158 ctx->map_configuration = config_fd;
159 ctx->map_toeplitz_key = toeplitz_fd;
160 ctx->map_indirections_table = table_fd;
162 if (!ebpf_rss_mmap(ctx)) {
163 ctx->program_fd = -1;
164 ctx->map_configuration = -1;
165 ctx->map_toeplitz_key = -1;
166 ctx->map_indirections_table = -1;
167 return false;
170 return true;
173 static bool ebpf_rss_set_config(struct EBPFRSSContext *ctx,
174 struct EBPFRSSConfig *config)
176 if (!ebpf_rss_is_loaded(ctx)) {
177 return false;
180 memcpy(ctx->mmap_configuration, config, sizeof(*config));
181 return true;
184 static bool ebpf_rss_set_indirections_table(struct EBPFRSSContext *ctx,
185 uint16_t *indirections_table,
186 size_t len)
188 char *cursor = ctx->mmap_indirections_table;
190 if (!ebpf_rss_is_loaded(ctx) || indirections_table == NULL ||
191 len > VIRTIO_NET_RSS_MAX_TABLE_LEN) {
192 return false;
195 for (size_t i = 0; i < len; i++) {
196 *(uint16_t *)cursor = indirections_table[i];
197 cursor += 8;
200 return true;
203 static bool ebpf_rss_set_toepliz_key(struct EBPFRSSContext *ctx,
204 uint8_t *toeplitz_key)
206 /* prepare toeplitz key */
207 uint8_t toe[VIRTIO_NET_RSS_MAX_KEY_SIZE] = {};
209 if (!ebpf_rss_is_loaded(ctx) || toeplitz_key == NULL) {
210 return false;
212 memcpy(toe, toeplitz_key, VIRTIO_NET_RSS_MAX_KEY_SIZE);
213 *(uint32_t *)toe = ntohl(*(uint32_t *)toe);
215 memcpy(ctx->mmap_toeplitz_key, toe, VIRTIO_NET_RSS_MAX_KEY_SIZE);
216 return true;
219 bool ebpf_rss_set_all(struct EBPFRSSContext *ctx, struct EBPFRSSConfig *config,
220 uint16_t *indirections_table, uint8_t *toeplitz_key)
222 if (!ebpf_rss_is_loaded(ctx) || config == NULL ||
223 indirections_table == NULL || toeplitz_key == NULL) {
224 return false;
227 if (!ebpf_rss_set_config(ctx, config)) {
228 return false;
231 if (!ebpf_rss_set_indirections_table(ctx, indirections_table,
232 config->indirections_len)) {
233 return false;
236 if (!ebpf_rss_set_toepliz_key(ctx, toeplitz_key)) {
237 return false;
240 return true;
243 void ebpf_rss_unload(struct EBPFRSSContext *ctx)
245 if (!ebpf_rss_is_loaded(ctx)) {
246 return;
249 ebpf_rss_munmap(ctx);
251 if (ctx->obj) {
252 rss_bpf__destroy(ctx->obj);
253 } else {
254 close(ctx->program_fd);
255 close(ctx->map_configuration);
256 close(ctx->map_toeplitz_key);
257 close(ctx->map_indirections_table);
260 ctx->obj = NULL;
261 ctx->program_fd = -1;
262 ctx->map_configuration = -1;
263 ctx->map_toeplitz_key = -1;
264 ctx->map_indirections_table = -1;
267 ebpf_binary_init(EBPF_PROGRAMID_RSS, rss_bpf__elf_bytes)