route.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #include <pthread.h>
  2. #include "Enclave_t.h"
  3. #include "config.hpp"
  4. #include "utils.hpp"
  5. #include "sort.hpp"
  6. #include "comms.hpp"
  7. #include "route.hpp"
  8. #define PROFILE_ROUTING
  9. struct MsgBuffer {
  10. pthread_mutex_t mutex;
  11. uint8_t *buf;
  12. // The number of messages (not bytes) in, or on their way into, the
  13. // buffer
  14. uint32_t reserved;
  15. // The number of messages definitely in the buffer
  16. uint32_t inserted;
  17. // The number of messages that can fit in buf
  18. uint32_t bufsize;
  19. // The number of nodes we've heard from
  20. nodenum_t nodes_received;
  21. MsgBuffer() : buf(NULL), reserved(0), inserted(0), bufsize(0),
  22. nodes_received(0) {
  23. pthread_mutex_init(&mutex, NULL);
  24. }
  25. ~MsgBuffer() {
  26. delete[] buf;
  27. }
  28. // The number passed is messages, not bytes
  29. void alloc(uint32_t msgs) {
  30. delete[] buf;
  31. buf = NULL;
  32. reserved = 0;
  33. inserted = 0;
  34. // This may throw bad_alloc, but we'll catch it higher up
  35. buf = new uint8_t[size_t(msgs) * g_teems_config.msg_size];
  36. bufsize = msgs;
  37. nodes_received = 0;
  38. }
  39. // Reset the contents of the buffer
  40. void reset() {
  41. memset(buf, 0, bufsize * g_teems_config.msg_size);
  42. reserved = 0;
  43. inserted = 0;
  44. nodes_received = 0;
  45. }
  46. // You can't copy a MsgBuffer
  47. MsgBuffer(const MsgBuffer&) = delete;
  48. MsgBuffer &operator=(const MsgBuffer&) = delete;
  49. };
  50. enum RouteStep {
  51. ROUTE_NOT_STARTED,
  52. ROUTE_ROUND_1,
  53. ROUTE_ROUND_2
  54. };
  55. // The round1 MsgBuffer stores messages we ingest while waiting for
  56. // round 1 to start, which will be sorted and sent out in round 1. The
  57. // round2 MsgBuffer stores messages we receive in round 1, which will be
  58. // padded, sorted, and sent out in round 2.
  59. static struct RouteState {
  60. MsgBuffer round1;
  61. MsgBuffer round2;
  62. RouteStep step;
  63. uint32_t tot_msg_per_ing;
  64. uint32_t max_msg_to_each_str;
  65. uint32_t max_round2_msgs;
  66. void *cbpointer;
  67. } route_state;
  68. // Computes ceil(x/y) where x and y are integers, x>=0, y>0.
  69. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  70. // Call this near the end of ecall_config_load, but before
  71. // comms_init_nodestate. Returns true on success, false on failure.
  72. bool route_init()
  73. {
  74. // Compute the maximum number of messages we could receive by direct
  75. // ingestion
  76. // Each ingestion node will have at most
  77. // ceil(user_count/num_ingestion_nodes) users, and each user will
  78. // send at most m_priv_out messages.
  79. uint32_t users_per_ing = CEILDIV(g_teems_config.user_count,
  80. g_teems_config.num_ingestion_nodes);
  81. uint32_t tot_msg_per_ing = users_per_ing * g_teems_config.m_priv_out;
  82. // Compute the maximum number of messages we could receive in round 1
  83. // Each ingestion node will send us an our_weight/tot_weight
  84. // fraction of the messages they hold
  85. uint32_t max_msg_from_each_ing = CEILDIV(tot_msg_per_ing,
  86. g_teems_config.tot_weight) * g_teems_config.my_weight;
  87. // And the maximum number we can receive in total is that times the
  88. // number of ingestion nodes
  89. uint32_t max_round1_msgs = max_msg_from_each_ing *
  90. g_teems_config.num_ingestion_nodes;
  91. // Compute the maximum number of messages we could send in round 2
  92. // Each storage node has at most this many users
  93. uint32_t users_per_str = CEILDIV(g_teems_config.user_count,
  94. g_teems_config.num_storage_nodes);
  95. // And so can receive at most this many messages
  96. uint32_t tot_msg_per_str = users_per_str *
  97. g_teems_config.m_priv_in;
  98. // Which will be at most this many from us
  99. uint32_t max_msg_to_each_str = CEILDIV(tot_msg_per_str,
  100. g_teems_config.tot_weight) * g_teems_config.my_weight;
  101. // But we can't send more messages to each storage server than we
  102. // could receive in total
  103. if (max_msg_to_each_str > max_round1_msgs) {
  104. max_msg_to_each_str = max_round1_msgs;
  105. }
  106. // And the max total number of outgoing messages in round 2 is then
  107. uint32_t max_round2_msgs = max_msg_to_each_str *
  108. g_teems_config.num_storage_nodes;
  109. // In case we have a weird configuration where users can send more
  110. // messages per epoch than they can receive, ensure the round 2
  111. // buffer is large enough to hold the incoming messages as well
  112. if (max_round2_msgs < max_round1_msgs) {
  113. max_round2_msgs = max_round1_msgs;
  114. }
  115. /*
  116. printf("round1_msgs = %u, round2_msgs = %u\n",
  117. max_round1_msgs, max_round2_msgs);
  118. */
  119. // Create the route state
  120. try {
  121. route_state.round1.alloc(tot_msg_per_ing);
  122. route_state.round2.alloc(max_round2_msgs);
  123. } catch (std::bad_alloc&) {
  124. printf("Memory allocation failed in route_init\n");
  125. return false;
  126. }
  127. route_state.step = ROUTE_NOT_STARTED;
  128. route_state.tot_msg_per_ing = tot_msg_per_ing;
  129. route_state.max_msg_to_each_str = max_msg_to_each_str;
  130. route_state.max_round2_msgs = max_round2_msgs;
  131. route_state.cbpointer = NULL;
  132. threadid_t nthreads = g_teems_config.nthreads;
  133. #ifdef PROFILE_ROUTING
  134. unsigned long start = printf_with_rtclock("begin precompute evalplans (%u,%hu) (%u,%hu)\n", tot_msg_per_ing, nthreads, max_round2_msgs, nthreads);
  135. #endif
  136. sort_precompute_evalplan(tot_msg_per_ing, nthreads);
  137. sort_precompute_evalplan(max_round2_msgs, nthreads);
  138. #ifdef PROFILE_ROUTING
  139. printf_with_rtclock_diff(start, "end precompute evalplans\n");
  140. #endif
  141. return true;
  142. }
  143. // Precompute the WaksmanNetworks needed for the sorts. If you pass -1,
  144. // it will return the number of different sizes it needs. If you pass
  145. // [0,sizes-1], it will compute one WaksmanNetwork with that size index
  146. // and return the number of available WaksmanNetworks of that size.
  147. size_t ecall_precompute_sort(int sizeidx)
  148. {
  149. size_t ret = 0;
  150. switch(sizeidx) {
  151. case 0:
  152. #ifdef PROFILE_ROUTING
  153. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.tot_msg_per_ing);
  154. #endif
  155. ret = sort_precompute(route_state.tot_msg_per_ing);
  156. #ifdef PROFILE_ROUTING
  157. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.tot_msg_per_ing);}
  158. #endif
  159. break;
  160. case 1:
  161. #ifdef PROFILE_ROUTING
  162. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.max_round2_msgs);
  163. #endif
  164. ret = sort_precompute(route_state.max_round2_msgs);
  165. #ifdef PROFILE_ROUTING
  166. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.max_round2_msgs);}
  167. #endif
  168. break;
  169. default:
  170. ret = 2;
  171. break;
  172. }
  173. return ret;
  174. }
  175. static uint8_t* msgbuffer_get_buf(MsgBuffer &msgbuf,
  176. NodeCommState &, uint32_t tot_enc_chunk_size)
  177. {
  178. uint16_t msg_size = g_teems_config.msg_size;
  179. // Chunks will be encrypted and have a MAC tag attached which will
  180. // not correspond to plaintext bytes, so we can trim them.
  181. // The minimum number of chunks needed to transmit this message
  182. uint32_t min_num_chunks =
  183. (tot_enc_chunk_size + (FRAME_SIZE-1)) / FRAME_SIZE;
  184. // The number of plaintext bytes this message could contain
  185. uint32_t plaintext_bytes = tot_enc_chunk_size -
  186. SGX_AESGCM_MAC_SIZE * min_num_chunks;
  187. assert ((plaintext_bytes % uint32_t(msg_size)) == 0);
  188. uint32_t num_msgs = plaintext_bytes/uint32_t(msg_size);
  189. pthread_mutex_lock(&msgbuf.mutex);
  190. uint32_t start = msgbuf.reserved;
  191. if (start + num_msgs > msgbuf.bufsize) {
  192. pthread_mutex_unlock(&msgbuf.mutex);
  193. printf("Max %u messages exceeded\n", msgbuf.bufsize);
  194. return NULL;
  195. }
  196. msgbuf.reserved += num_msgs;
  197. pthread_mutex_unlock(&msgbuf.mutex);
  198. return msgbuf.buf + start * msg_size;
  199. }
  200. // A round 1 message was received by a routing node from an ingestion
  201. // node; we put it into the round 2 buffer for processing in round 2
  202. static void round1_received(NodeCommState &nodest,
  203. uint8_t *data, uint32_t plaintext_len, uint32_t)
  204. {
  205. uint16_t msg_size = g_teems_config.msg_size;
  206. assert((plaintext_len % uint32_t(msg_size)) == 0);
  207. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  208. pthread_mutex_lock(&route_state.round2.mutex);
  209. route_state.round2.inserted += num_msgs;
  210. route_state.round2.nodes_received += 1;
  211. nodenum_t nodes_received = route_state.round2.nodes_received;
  212. pthread_mutex_unlock(&route_state.round2.mutex);
  213. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  214. route_state.step = ROUTE_ROUND_1;
  215. void *cbpointer = route_state.cbpointer;
  216. route_state.cbpointer = NULL;
  217. ocall_routing_round_complete(cbpointer, 1);
  218. }
  219. }
  220. // A round 2 message was received by a storage node from a routing node
  221. static void round2_received(NodeCommState &nodest,
  222. uint8_t *data, uint32_t plaintext_len, uint32_t)
  223. {
  224. uint16_t msg_size = g_teems_config.msg_size;
  225. assert((plaintext_len % uint32_t(msg_size)) == 0);
  226. }
  227. // For a given other node, set the received message handler to the first
  228. // message we would expect from them, given their roles and our roles.
  229. void route_init_msg_handler(nodenum_t node_num)
  230. {
  231. // Our roles and their roles
  232. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  233. uint8_t their_roles = g_teems_config.roles[node_num];
  234. // The node communication state
  235. NodeCommState &nodest = g_commstates[node_num];
  236. // If we are a routing node (possibly among other roles) and they
  237. // are an ingestion node (possibly among other roles), a round 1
  238. // routing message is the first thing we expect from them. We put
  239. // these messages into the round2 buffer for processing.
  240. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  241. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  242. uint32_t tot_enc_chunk_size) {
  243. return msgbuffer_get_buf(route_state.round2, commst,
  244. tot_enc_chunk_size);
  245. };
  246. nodest.in_msg_received = round1_received;
  247. }
  248. // Otherwise, if we are a storage node (possibly among other roles)
  249. // and they are a routing node (possibly among other roles), a round
  250. // 2 routing message is the first thing we expect form them
  251. else if ((our_roles & ROLE_STORAGE) && (their_roles & ROLE_ROUTING)) {
  252. nodest.in_msg_get_buf = default_in_msg_get_buf;
  253. nodest.in_msg_received = round2_received;
  254. }
  255. // Otherwise, we don't expect a message from this node. Set the
  256. // unknown message handler.
  257. else {
  258. nodest.in_msg_get_buf = default_in_msg_get_buf;
  259. nodest.in_msg_received = unknown_in_msg_received;
  260. }
  261. }
  262. // Directly ingest a buffer of num_msgs messages into the round1 buffer.
  263. // Return true on success, false on failure.
  264. bool ecall_ingest_raw(uint8_t *msgs, uint32_t num_msgs)
  265. {
  266. uint16_t msg_size = g_teems_config.msg_size;
  267. MsgBuffer &round1 = route_state.round1;
  268. pthread_mutex_lock(&round1.mutex);
  269. uint32_t start = round1.reserved;
  270. if (start + num_msgs > route_state.tot_msg_per_ing) {
  271. pthread_mutex_unlock(&round1.mutex);
  272. printf("Max %u messages exceeded\n",
  273. route_state.tot_msg_per_ing);
  274. return false;
  275. }
  276. round1.reserved += num_msgs;
  277. pthread_mutex_unlock(&round1.mutex);
  278. memmove(round1.buf + start * msg_size,
  279. msgs, num_msgs * msg_size);
  280. pthread_mutex_lock(&round1.mutex);
  281. round1.inserted += num_msgs;
  282. pthread_mutex_unlock(&round1.mutex);
  283. return true;
  284. }
  285. // Send the round 1 messages. Note that N here is not private.
  286. static void send_round1_msgs(const uint8_t *msgs, const uint64_t *indices,
  287. uint32_t N)
  288. {
  289. uint16_t msg_size = g_teems_config.msg_size;
  290. uint16_t tot_weight = g_teems_config.tot_weight;
  291. nodenum_t my_node_num = g_teems_config.my_node_num;
  292. uint32_t full_rows = N / uint32_t(tot_weight);
  293. uint32_t last_row = N % uint32_t(tot_weight);
  294. for (auto &routing_node: g_teems_config.routing_nodes) {
  295. uint8_t weight =
  296. g_teems_config.weights[routing_node].weight;
  297. if (weight == 0) {
  298. // This shouldn't happen, but just in case
  299. continue;
  300. }
  301. uint16_t start_weight =
  302. g_teems_config.weights[routing_node].startweight;
  303. // The number of messages headed for this routing node from the
  304. // full rows
  305. uint32_t num_msgs_full_rows = full_rows * uint32_t(weight);
  306. // The number of messages headed for this routing node from the
  307. // incomplete last row is:
  308. // 0 if last_row < start_weight
  309. // last_row-start_weight if start_weight <= last_row < start_weight + weight
  310. // weight if start_weight + weight <= last_row
  311. uint32_t num_msgs_last_row = 0;
  312. if (start_weight <= last_row && last_row < start_weight + weight) {
  313. num_msgs_last_row = last_row-start_weight;
  314. } else if (start_weight + weight <= last_row) {
  315. num_msgs_last_row = weight;
  316. }
  317. // The total number of messages headed for this routing node
  318. uint32_t num_msgs = num_msgs_full_rows + num_msgs_last_row;
  319. if (routing_node == my_node_num) {
  320. // Special case: we're sending to ourselves; just put the
  321. // messages in our own round2 buffer
  322. MsgBuffer &round2 = route_state.round2;
  323. pthread_mutex_lock(&round2.mutex);
  324. uint32_t start = round2.reserved;
  325. if (start + num_msgs > round2.bufsize) {
  326. pthread_mutex_unlock(&round2.mutex);
  327. printf("Max %u messages exceeded\n", round2.bufsize);
  328. return;
  329. }
  330. round2.reserved += num_msgs;
  331. pthread_mutex_unlock(&round2.mutex);
  332. uint8_t *buf = round2.buf + start * msg_size;
  333. for (uint32_t i=0; i<full_rows; ++i) {
  334. const uint64_t *idxp = indices + i*tot_weight + start_weight;
  335. for (uint32_t j=0; j<weight; ++j) {
  336. memmove(buf, msgs + idxp[j]*msg_size, msg_size);
  337. buf += msg_size;
  338. }
  339. }
  340. const uint64_t *idxp = indices + full_rows*tot_weight + start_weight;
  341. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  342. memmove(buf, msgs + idxp[j]*msg_size, msg_size);
  343. buf += msg_size;
  344. }
  345. pthread_mutex_lock(&round2.mutex);
  346. round2.inserted += num_msgs;
  347. round2.nodes_received += 1;
  348. nodenum_t nodes_received = round2.nodes_received;
  349. pthread_mutex_unlock(&round2.mutex);
  350. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  351. route_state.step = ROUTE_ROUND_1;
  352. void *cbpointer = route_state.cbpointer;
  353. route_state.cbpointer = NULL;
  354. ocall_routing_round_complete(cbpointer, 1);
  355. }
  356. } else {
  357. NodeCommState &nodecom = g_commstates[routing_node];
  358. nodecom.message_start(num_msgs * msg_size);
  359. for (uint32_t i=0; i<full_rows; ++i) {
  360. const uint64_t *idxp = indices + i*tot_weight + start_weight;
  361. for (uint32_t j=0; j<weight; ++j) {
  362. nodecom.message_data(msgs + idxp[j]*msg_size, msg_size);
  363. }
  364. }
  365. const uint64_t *idxp = indices + full_rows*tot_weight + start_weight;
  366. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  367. nodecom.message_data(msgs + idxp[j]*msg_size, msg_size);
  368. }
  369. }
  370. }
  371. }
  372. // Perform the next round of routing. The callback pointer will be
  373. // passed to ocall_routing_round_complete when the round is complete.
  374. void ecall_routing_proceed(void *cbpointer)
  375. {
  376. if (route_state.step == ROUTE_NOT_STARTED) {
  377. route_state.cbpointer = cbpointer;
  378. MsgBuffer &round1 = route_state.round1;
  379. pthread_mutex_lock(&round1.mutex);
  380. // Ensure there are no pending messages currently being inserted
  381. // into the buffer
  382. while (round1.reserved != round1.inserted) {
  383. pthread_mutex_unlock(&round1.mutex);
  384. pthread_mutex_lock(&round1.mutex);
  385. }
  386. // Sort the messages we've received
  387. #ifdef PROFILE_ROUTING
  388. uint32_t inserted = round1.inserted;
  389. unsigned long start = printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  390. #endif
  391. sort_mtobliv(g_teems_config.nthreads, round1.buf,
  392. g_teems_config.msg_size, round1.inserted,
  393. route_state.tot_msg_per_ing, send_round1_msgs);
  394. #ifdef PROFILE_ROUTING
  395. printf_with_rtclock_diff(start, "end oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  396. #endif
  397. round1.reset();
  398. pthread_mutex_unlock(&round1.mutex);
  399. } else if (route_state.step == ROUTE_ROUND_1) {
  400. route_state.cbpointer = cbpointer;
  401. MsgBuffer &round2 = route_state.round2;
  402. pthread_mutex_lock(&round2.mutex);
  403. // Ensure there are no pending messages currently being inserted
  404. // into the buffer
  405. while (round2.reserved != round2.inserted) {
  406. pthread_mutex_unlock(&round2.mutex);
  407. pthread_mutex_lock(&round2.mutex);
  408. }
  409. uint32_t msg_size = g_teems_config.msg_size;
  410. for(uint32_t i=0;i<round2.inserted;++i) {
  411. uint32_t destaddr = *(uint32_t*)(round2.buf+i*msg_size);
  412. printf("%08x\n", destaddr);
  413. }
  414. round2.reset();
  415. pthread_mutex_unlock(&round2.mutex);
  416. }
  417. }