ebpf_rss.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <bpf/libbpf.h>
  16. #include <bpf/bpf.h>
  17. #include "hw/virtio/virtio-net.h" /* VIRTIO_NET_RSS_MAX_TABLE_LEN */
  18. #include "ebpf/ebpf_rss.h"
  19. #include "ebpf/rss.bpf.skeleton.h"
  20. #include "trace.h"
  21. void ebpf_rss_init(struct EBPFRSSContext *ctx)
  22. {
  23. if (ctx != NULL) {
  24. ctx->obj = NULL;
  25. }
  26. }
  27. bool ebpf_rss_is_loaded(struct EBPFRSSContext *ctx)
  28. {
  29. return ctx != NULL && ctx->obj != NULL;
  30. }
  31. bool ebpf_rss_load(struct EBPFRSSContext *ctx)
  32. {
  33. struct rss_bpf *rss_bpf_ctx;
  34. if (ctx == NULL) {
  35. return false;
  36. }
  37. rss_bpf_ctx = rss_bpf__open();
  38. if (rss_bpf_ctx == NULL) {
  39. trace_ebpf_error("eBPF RSS", "can not open eBPF RSS object");
  40. goto error;
  41. }
  42. bpf_program__set_type(rss_bpf_ctx->progs.tun_rss_steering_prog, BPF_PROG_TYPE_SOCKET_FILTER);
  43. if (rss_bpf__load(rss_bpf_ctx)) {
  44. trace_ebpf_error("eBPF RSS", "can not load RSS program");
  45. goto error;
  46. }
  47. ctx->obj = rss_bpf_ctx;
  48. ctx->program_fd = bpf_program__fd(
  49. rss_bpf_ctx->progs.tun_rss_steering_prog);
  50. ctx->map_configuration = bpf_map__fd(
  51. rss_bpf_ctx->maps.tap_rss_map_configurations);
  52. ctx->map_indirections_table = bpf_map__fd(
  53. rss_bpf_ctx->maps.tap_rss_map_indirection_table);
  54. ctx->map_toeplitz_key = bpf_map__fd(
  55. rss_bpf_ctx->maps.tap_rss_map_toeplitz_key);
  56. return true;
  57. error:
  58. rss_bpf__destroy(rss_bpf_ctx);
  59. ctx->obj = NULL;
  60. return false;
  61. }
  62. static bool ebpf_rss_set_config(struct EBPFRSSContext *ctx,
  63. struct EBPFRSSConfig *config)
  64. {
  65. uint32_t map_key = 0;
  66. if (!ebpf_rss_is_loaded(ctx)) {
  67. return false;
  68. }
  69. if (bpf_map_update_elem(ctx->map_configuration,
  70. &map_key, config, 0) < 0) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. static bool ebpf_rss_set_indirections_table(struct EBPFRSSContext *ctx,
  76. uint16_t *indirections_table,
  77. size_t len)
  78. {
  79. uint32_t i = 0;
  80. if (!ebpf_rss_is_loaded(ctx) || indirections_table == NULL ||
  81. len > VIRTIO_NET_RSS_MAX_TABLE_LEN) {
  82. return false;
  83. }
  84. for (; i < len; ++i) {
  85. if (bpf_map_update_elem(ctx->map_indirections_table, &i,
  86. indirections_table + i, 0) < 0) {
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. static bool ebpf_rss_set_toepliz_key(struct EBPFRSSContext *ctx,
  93. uint8_t *toeplitz_key)
  94. {
  95. uint32_t map_key = 0;
  96. /* prepare toeplitz key */
  97. uint8_t toe[VIRTIO_NET_RSS_MAX_KEY_SIZE] = {};
  98. if (!ebpf_rss_is_loaded(ctx) || toeplitz_key == NULL) {
  99. return false;
  100. }
  101. memcpy(toe, toeplitz_key, VIRTIO_NET_RSS_MAX_KEY_SIZE);
  102. *(uint32_t *)toe = ntohl(*(uint32_t *)toe);
  103. if (bpf_map_update_elem(ctx->map_toeplitz_key, &map_key, toe,
  104. 0) < 0) {
  105. return false;
  106. }
  107. return true;
  108. }
  109. bool ebpf_rss_set_all(struct EBPFRSSContext *ctx, struct EBPFRSSConfig *config,
  110. uint16_t *indirections_table, uint8_t *toeplitz_key)
  111. {
  112. if (!ebpf_rss_is_loaded(ctx) || config == NULL ||
  113. indirections_table == NULL || toeplitz_key == NULL) {
  114. return false;
  115. }
  116. if (!ebpf_rss_set_config(ctx, config)) {
  117. return false;
  118. }
  119. if (!ebpf_rss_set_indirections_table(ctx, indirections_table,
  120. config->indirections_len)) {
  121. return false;
  122. }
  123. if (!ebpf_rss_set_toepliz_key(ctx, toeplitz_key)) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. void ebpf_rss_unload(struct EBPFRSSContext *ctx)
  129. {
  130. if (!ebpf_rss_is_loaded(ctx)) {
  131. return;
  132. }
  133. rss_bpf__destroy(ctx->obj);
  134. ctx->obj = NULL;
  135. }