route.cpp 17 KB

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