route.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <pthread.h>
  2. #include "Enclave_t.h"
  3. #include "config.hpp"
  4. #include "utils.hpp"
  5. #include "route.hpp"
  6. struct MsgBuffer {
  7. pthread_mutex_t mutex;
  8. uint8_t *buf;
  9. // The number of messages (not bytes) in, or on their way into, the
  10. // buffer
  11. uint32_t reserved;
  12. // The number of messages definitely in the buffer
  13. uint32_t inserted;
  14. MsgBuffer() : buf(NULL), reserved(0), inserted(0) {}
  15. ~MsgBuffer() {
  16. delete[] buf;
  17. }
  18. // The number passed is messages, not bytes
  19. void alloc(uint32_t msgs) {
  20. delete[] buf;
  21. buf = NULL;
  22. reserved = 0;
  23. inserted = 0;
  24. // This may throw bad_alloc, but we'll catch it higher up
  25. buf = new uint8_t[size_t(msgs) * g_teems_config.msg_size];
  26. }
  27. // You can't copy a MsgBuffer
  28. MsgBuffer(const MsgBuffer&) = delete;
  29. MsgBuffer &operator=(const MsgBuffer&) = delete;
  30. };
  31. enum RouteStep {
  32. ROUTE_NOT_STARTED,
  33. ROUTE_ROUND_1,
  34. ROUTE_ROUND_2
  35. };
  36. // The round1 MsgBuffer stores messages we ingest while waiting for
  37. // round 1 to start, which will be sorted and sent out in round 1. The
  38. // round2 MsgBuffer stores messages we receive in round 1, which will be
  39. // padded, sorted, and sent out in round 2.
  40. static struct RouteState {
  41. MsgBuffer round1;
  42. MsgBuffer round2;
  43. RouteStep step;
  44. uint32_t tot_msg_per_ing;
  45. uint32_t max_msg_to_each_str;
  46. uint32_t max_round2_msgs;
  47. } route_state;
  48. // Computes ceil(x/y) where x and y are integers, x>=0, y>0.
  49. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  50. // Call this near the end of ecall_config_load, but before
  51. // comms_init_nodestate. Returns true on success, false on failure.
  52. bool route_init()
  53. {
  54. // Compute the maximum number of messages we could receive by direct
  55. // ingestion
  56. // Each ingestion node will have at most
  57. // ceil(user_count/num_ingestion_nodes) users, and each user will
  58. // send at most m_priv_out messages.
  59. uint32_t users_per_ing = CEILDIV(g_teems_config.user_count,
  60. g_teems_config.num_ingestion_nodes);
  61. uint32_t tot_msg_per_ing = users_per_ing * g_teems_config.m_priv_out;
  62. // Compute the maximum number of messages we could receive in round 1
  63. // Each ingestion node will send us an our_weight/tot_weight
  64. // fraction of the messages they hold
  65. uint32_t max_msg_from_each_ing = CEILDIV(tot_msg_per_ing,
  66. g_teems_config.tot_weight) * g_teems_config.my_weight;
  67. // And the maximum number we can receive in total is that times the
  68. // number of ingestion nodes
  69. uint32_t max_round1_msgs = max_msg_from_each_ing *
  70. g_teems_config.num_ingestion_nodes;
  71. // Compute the maximum number of messages we could send in round 2
  72. // Each storage node has at most this many users
  73. uint32_t users_per_str = CEILDIV(g_teems_config.user_count,
  74. g_teems_config.num_storage_nodes);
  75. // And so can receive at most this many messages
  76. uint32_t tot_msg_per_str = users_per_str *
  77. g_teems_config.m_priv_in;
  78. // Which will be at most this many from us
  79. uint32_t max_msg_to_each_str = CEILDIV(tot_msg_per_str,
  80. g_teems_config.tot_weight) * g_teems_config.my_weight;
  81. // But we can't send more messages to each storage server than we
  82. // could receive in total
  83. if (max_msg_to_each_str > max_round1_msgs) {
  84. max_msg_to_each_str = max_round1_msgs;
  85. }
  86. // And the max total number of outgoing messages in round 2 is then
  87. uint32_t max_round2_msgs = max_msg_to_each_str *
  88. g_teems_config.num_storage_nodes;
  89. // In case we have a weird configuration where users can send more
  90. // messages per epoch than they can receive, ensure the round 2
  91. // buffer is large enough to hold the incoming messages as well
  92. if (max_round2_msgs < max_round1_msgs) {
  93. max_round2_msgs = max_round1_msgs;
  94. }
  95. /*
  96. printf("round1_msgs = %u, round2_msgs = %u\n",
  97. max_round1_msgs, max_round2_msgs);
  98. */
  99. // Create the route state
  100. try {
  101. route_state.round1.alloc(tot_msg_per_ing);
  102. route_state.round2.alloc(max_round2_msgs);
  103. } catch (std::bad_alloc&) {
  104. printf("Memory allocation failed in route_init\n");
  105. return false;
  106. }
  107. route_state.step = ROUTE_NOT_STARTED;
  108. route_state.tot_msg_per_ing = tot_msg_per_ing;
  109. route_state.max_msg_to_each_str = max_msg_to_each_str;
  110. route_state.max_round2_msgs = max_round2_msgs;
  111. return true;
  112. }
  113. // Directly ingest a buffer of num_msgs messages into the round1 buffer.
  114. // Return true on success, false on failure.
  115. bool ecall_ingest_raw(uint8_t *msgs, uint32_t num_msgs)
  116. {
  117. uint16_t msg_size = g_teems_config.msg_size;
  118. MsgBuffer &round1 = route_state.round1;
  119. pthread_mutex_lock(&round1.mutex);
  120. uint32_t start = round1.reserved;
  121. if (start + num_msgs > route_state.tot_msg_per_ing) {
  122. pthread_mutex_unlock(&round1.mutex);
  123. return false;
  124. }
  125. round1.reserved += num_msgs;
  126. pthread_mutex_unlock(&round1.mutex);
  127. memmove(round1.buf + start * msg_size,
  128. msgs, num_msgs * msg_size);
  129. pthread_mutex_lock(&round1.mutex);
  130. round1.inserted += num_msgs;
  131. pthread_mutex_unlock(&round1.mutex);
  132. return true;
  133. }