ebpf_rss.c 9.0 KB

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