route.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. #include "Enclave_t.h"
  2. #include "config.hpp"
  3. #include "utils.hpp"
  4. #include "sort.hpp"
  5. #include "comms.hpp"
  6. #include "obliv.hpp"
  7. #include "storage.hpp"
  8. #include "route.hpp"
  9. #define PROFILE_ROUTING
  10. RouteState route_state;
  11. // Computes ceil(x/y) where x and y are integers, x>=0, y>0.
  12. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  13. // Call this near the end of ecall_config_load, but before
  14. // comms_init_nodestate. Returns true on success, false on failure.
  15. bool route_init()
  16. {
  17. // Compute the maximum number of messages we could receive by direct
  18. // ingestion
  19. // Each ingestion node will have at most
  20. // ceil(user_count/num_ingestion_nodes) users, and each user will
  21. // send at most m_priv_out messages.
  22. uint32_t users_per_ing = CEILDIV(g_teems_config.user_count,
  23. g_teems_config.num_ingestion_nodes);
  24. uint32_t tot_msg_per_ing = users_per_ing * g_teems_config.m_priv_out;
  25. // Compute the maximum number of messages we could receive in round 1
  26. // Each ingestion node will send us an our_weight/tot_weight
  27. // fraction of the messages they hold
  28. uint32_t max_msg_from_each_ing = CEILDIV(tot_msg_per_ing,
  29. g_teems_config.tot_weight) * g_teems_config.my_weight;
  30. // And the maximum number we can receive in total is that times the
  31. // number of ingestion nodes
  32. uint32_t max_round1_msgs = max_msg_from_each_ing *
  33. g_teems_config.num_ingestion_nodes;
  34. // Compute the maximum number of messages we could send in round 2
  35. // Each storage node has at most this many users
  36. uint32_t users_per_stg = CEILDIV(g_teems_config.user_count,
  37. g_teems_config.num_storage_nodes);
  38. // And so can receive at most this many messages
  39. uint32_t tot_msg_per_stg = users_per_stg *
  40. g_teems_config.m_priv_in;
  41. // Which will be at most this many from us
  42. uint32_t max_msg_to_each_stg = CEILDIV(tot_msg_per_stg,
  43. g_teems_config.tot_weight) * g_teems_config.my_weight;
  44. // But we can't send more messages to each storage server than we
  45. // could receive in total
  46. if (max_msg_to_each_stg > max_round1_msgs) {
  47. max_msg_to_each_stg = max_round1_msgs;
  48. }
  49. // And the max total number of outgoing messages in round 2 is then
  50. uint32_t max_round2_msgs = max_msg_to_each_stg *
  51. g_teems_config.num_storage_nodes;
  52. // In case we have a weird configuration where users can send more
  53. // messages per epoch than they can receive, ensure the round 2
  54. // buffer is large enough to hold the incoming messages as well
  55. if (max_round2_msgs < max_round1_msgs) {
  56. max_round2_msgs = max_round1_msgs;
  57. }
  58. /*
  59. printf("round1_msgs = %u, round2_msgs = %u\n",
  60. max_round1_msgs, max_round2_msgs);
  61. */
  62. // Create the route state
  63. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  64. try {
  65. if (my_roles & ROLE_INGESTION) {
  66. route_state.ingbuf.alloc(tot_msg_per_ing);
  67. }
  68. if (my_roles & ROLE_ROUTING) {
  69. route_state.round1.alloc(max_round2_msgs);
  70. }
  71. if (my_roles & ROLE_STORAGE) {
  72. route_state.round2.alloc(tot_msg_per_stg +
  73. g_teems_config.tot_weight);
  74. }
  75. } catch (std::bad_alloc&) {
  76. printf("Memory allocation failed in route_init\n");
  77. return false;
  78. }
  79. route_state.step = ROUTE_NOT_STARTED;
  80. route_state.tot_msg_per_ing = tot_msg_per_ing;
  81. route_state.max_msg_to_each_stg = max_msg_to_each_stg;
  82. route_state.max_round2_msgs = max_round2_msgs;
  83. route_state.cbpointer = NULL;
  84. threadid_t nthreads = g_teems_config.nthreads;
  85. #ifdef PROFILE_ROUTING
  86. unsigned long start = printf_with_rtclock("begin precompute evalplans (%u,%hu) (%u,%hu)\n", tot_msg_per_ing, nthreads, max_round2_msgs, nthreads);
  87. #endif
  88. sort_precompute_evalplan(tot_msg_per_ing, nthreads);
  89. sort_precompute_evalplan(max_round2_msgs, nthreads);
  90. #ifdef PROFILE_ROUTING
  91. printf_with_rtclock_diff(start, "end precompute evalplans\n");
  92. #endif
  93. return true;
  94. }
  95. // Precompute the WaksmanNetworks needed for the sorts. If you pass -1,
  96. // it will return the number of different sizes it needs. If you pass
  97. // [0,sizes-1], it will compute one WaksmanNetwork with that size index
  98. // and return the number of available WaksmanNetworks of that size.
  99. size_t ecall_precompute_sort(int sizeidx)
  100. {
  101. size_t ret = 0;
  102. switch(sizeidx) {
  103. case 0:
  104. #ifdef PROFILE_ROUTING
  105. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.tot_msg_per_ing);
  106. #endif
  107. ret = sort_precompute(route_state.tot_msg_per_ing);
  108. #ifdef PROFILE_ROUTING
  109. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.tot_msg_per_ing);}
  110. #endif
  111. break;
  112. case 1:
  113. #ifdef PROFILE_ROUTING
  114. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.max_round2_msgs);
  115. #endif
  116. ret = sort_precompute(route_state.max_round2_msgs);
  117. #ifdef PROFILE_ROUTING
  118. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.max_round2_msgs);}
  119. #endif
  120. break;
  121. default:
  122. ret = 2;
  123. break;
  124. }
  125. return ret;
  126. }
  127. static uint8_t* msgbuffer_get_buf(MsgBuffer &msgbuf,
  128. NodeCommState &, uint32_t tot_enc_chunk_size)
  129. {
  130. uint16_t msg_size = g_teems_config.msg_size;
  131. // Chunks will be encrypted and have a MAC tag attached which will
  132. // not correspond to plaintext bytes, so we can trim them.
  133. // The minimum number of chunks needed to transmit this message
  134. uint32_t min_num_chunks =
  135. (tot_enc_chunk_size + (FRAME_SIZE-1)) / FRAME_SIZE;
  136. // The number of plaintext bytes this message could contain
  137. uint32_t plaintext_bytes = tot_enc_chunk_size -
  138. SGX_AESGCM_MAC_SIZE * min_num_chunks;
  139. assert ((plaintext_bytes % uint32_t(msg_size)) == 0);
  140. uint32_t num_msgs = plaintext_bytes/uint32_t(msg_size);
  141. pthread_mutex_lock(&msgbuf.mutex);
  142. uint32_t start = msgbuf.reserved;
  143. if (start + num_msgs > msgbuf.bufsize) {
  144. pthread_mutex_unlock(&msgbuf.mutex);
  145. printf("Max %u messages exceeded\n", msgbuf.bufsize);
  146. return NULL;
  147. }
  148. msgbuf.reserved += num_msgs;
  149. pthread_mutex_unlock(&msgbuf.mutex);
  150. return msgbuf.buf + start * msg_size;
  151. }
  152. static void round2_received(NodeCommState &nodest,
  153. uint8_t *data, uint32_t plaintext_len, uint32_t);
  154. // A round 1 message was received by a routing node from an ingestion
  155. // node; we put it into the round 2 buffer for processing in round 2
  156. static void round1_received(NodeCommState &nodest,
  157. uint8_t *data, uint32_t plaintext_len, uint32_t)
  158. {
  159. uint16_t msg_size = g_teems_config.msg_size;
  160. assert((plaintext_len % uint32_t(msg_size)) == 0);
  161. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  162. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  163. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  164. pthread_mutex_lock(&route_state.round1.mutex);
  165. route_state.round1.inserted += num_msgs;
  166. route_state.round1.nodes_received += 1;
  167. nodenum_t nodes_received = route_state.round1.nodes_received;
  168. bool completed_prev_round = route_state.round1.completed_prev_round;
  169. pthread_mutex_unlock(&route_state.round1.mutex);
  170. // What is the next message we expect from this node?
  171. if ((our_roles & ROLE_STORAGE) && (their_roles & ROLE_ROUTING)) {
  172. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  173. uint32_t tot_enc_chunk_size) {
  174. return msgbuffer_get_buf(route_state.round2, commst,
  175. tot_enc_chunk_size);
  176. };
  177. nodest.in_msg_received = round2_received;
  178. }
  179. // Otherwise, it's just the next round 1 message, so don't change
  180. // the handlers.
  181. if (nodes_received == g_teems_config.num_ingestion_nodes &&
  182. completed_prev_round) {
  183. route_state.step = ROUTE_ROUND_1;
  184. void *cbpointer = route_state.cbpointer;
  185. route_state.cbpointer = NULL;
  186. ocall_routing_round_complete(cbpointer, 1);
  187. }
  188. }
  189. // A round 2 message was received by a storage node from a routing node
  190. static void round2_received(NodeCommState &nodest,
  191. uint8_t *data, uint32_t plaintext_len, uint32_t)
  192. {
  193. uint16_t msg_size = g_teems_config.msg_size;
  194. assert((plaintext_len % uint32_t(msg_size)) == 0);
  195. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  196. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  197. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  198. pthread_mutex_lock(&route_state.round2.mutex);
  199. route_state.round2.inserted += num_msgs;
  200. route_state.round2.nodes_received += 1;
  201. nodenum_t nodes_received = route_state.round2.nodes_received;
  202. bool completed_prev_round = route_state.round2.completed_prev_round;
  203. pthread_mutex_unlock(&route_state.round2.mutex);
  204. // What is the next message we expect from this node?
  205. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  206. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  207. uint32_t tot_enc_chunk_size) {
  208. return msgbuffer_get_buf(route_state.round1, commst,
  209. tot_enc_chunk_size);
  210. };
  211. nodest.in_msg_received = round1_received;
  212. }
  213. // Otherwise, it's just the next round 2 message, so don't change
  214. // the handlers.
  215. if (nodes_received == g_teems_config.num_routing_nodes &&
  216. completed_prev_round) {
  217. route_state.step = ROUTE_ROUND_2;
  218. void *cbpointer = route_state.cbpointer;
  219. route_state.cbpointer = NULL;
  220. ocall_routing_round_complete(cbpointer, 2);
  221. }
  222. }
  223. // For a given other node, set the received message handler to the first
  224. // message we would expect from them, given their roles and our roles.
  225. void route_init_msg_handler(nodenum_t node_num)
  226. {
  227. // Our roles and their roles
  228. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  229. uint8_t their_roles = g_teems_config.roles[node_num];
  230. // The node communication state
  231. NodeCommState &nodest = g_commstates[node_num];
  232. // If we are a routing node (possibly among other roles) and they
  233. // are an ingestion node (possibly among other roles), a round 1
  234. // routing message is the first thing we expect from them. We put
  235. // these messages into the round1 buffer for processing.
  236. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  237. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  238. uint32_t tot_enc_chunk_size) {
  239. return msgbuffer_get_buf(route_state.round1, commst,
  240. tot_enc_chunk_size);
  241. };
  242. nodest.in_msg_received = round1_received;
  243. }
  244. // Otherwise, if we are a storage node (possibly among other roles)
  245. // and they are a routing node (possibly among other roles), a round
  246. // 2 routing message is the first thing we expect from them
  247. else if ((our_roles & ROLE_STORAGE) && (their_roles & ROLE_ROUTING)) {
  248. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  249. uint32_t tot_enc_chunk_size) {
  250. return msgbuffer_get_buf(route_state.round2, commst,
  251. tot_enc_chunk_size);
  252. };
  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 ingbuf 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 &ingbuf = route_state.ingbuf;
  268. pthread_mutex_lock(&ingbuf.mutex);
  269. uint32_t start = ingbuf.reserved;
  270. if (start + num_msgs > route_state.tot_msg_per_ing) {
  271. pthread_mutex_unlock(&ingbuf.mutex);
  272. printf("Max %u messages exceeded\n",
  273. route_state.tot_msg_per_ing);
  274. return false;
  275. }
  276. ingbuf.reserved += num_msgs;
  277. pthread_mutex_unlock(&ingbuf.mutex);
  278. memmove(ingbuf.buf + start * msg_size,
  279. msgs, num_msgs * msg_size);
  280. pthread_mutex_lock(&ingbuf.mutex);
  281. ingbuf.inserted += num_msgs;
  282. pthread_mutex_unlock(&ingbuf.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 round1 buffer
  322. MsgBuffer &round1 = route_state.round1;
  323. pthread_mutex_lock(&round1.mutex);
  324. uint32_t start = round1.reserved;
  325. if (start + num_msgs > round1.bufsize) {
  326. pthread_mutex_unlock(&round1.mutex);
  327. printf("Max %u messages exceeded\n", round1.bufsize);
  328. return;
  329. }
  330. round1.reserved += num_msgs;
  331. pthread_mutex_unlock(&round1.mutex);
  332. uint8_t *buf = round1.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(&round1.mutex);
  346. round1.inserted += num_msgs;
  347. round1.nodes_received += 1;
  348. pthread_mutex_unlock(&round1.mutex);
  349. } else {
  350. NodeCommState &nodecom = g_commstates[routing_node];
  351. nodecom.message_start(num_msgs * msg_size);
  352. for (uint32_t i=0; i<full_rows; ++i) {
  353. const uint64_t *idxp = indices + i*tot_weight + start_weight;
  354. for (uint32_t j=0; j<weight; ++j) {
  355. nodecom.message_data(msgs + idxp[j]*msg_size, msg_size);
  356. }
  357. }
  358. const uint64_t *idxp = indices + full_rows*tot_weight + start_weight;
  359. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  360. nodecom.message_data(msgs + idxp[j]*msg_size, msg_size);
  361. }
  362. }
  363. }
  364. }
  365. // Send the round 2 messages from the round 1 buffer, which are already
  366. // padded and shuffled, so this can be done non-obliviously. tot_msgs
  367. // is the total number of messages in the input buffer, which may
  368. // include padding messages added by the shuffle. Those messages are
  369. // not sent anywhere. There are num_msgs_per_stg messages for each
  370. // storage node labelled for that node.
  371. static void send_round2_msgs(uint32_t tot_msgs, uint32_t num_msgs_per_stg)
  372. {
  373. uint16_t msg_size = g_teems_config.msg_size;
  374. MsgBuffer &round1 = route_state.round1;
  375. const uint8_t* buf = round1.buf;
  376. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  377. nodenum_t my_node_num = g_teems_config.my_node_num;
  378. uint8_t *myself_buf = NULL;
  379. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  380. nodenum_t node = g_teems_config.storage_nodes[i];
  381. if (node != my_node_num) {
  382. g_commstates[node].message_start(msg_size * num_msgs_per_stg);
  383. } else {
  384. MsgBuffer &round2 = route_state.round2;
  385. pthread_mutex_lock(&round2.mutex);
  386. uint32_t start = round2.reserved;
  387. if (start + num_msgs_per_stg > round2.bufsize) {
  388. pthread_mutex_unlock(&round2.mutex);
  389. printf("Max %u messages exceeded\n", round2.bufsize);
  390. return;
  391. }
  392. round2.reserved += num_msgs_per_stg;
  393. pthread_mutex_unlock(&round2.mutex);
  394. myself_buf = round2.buf + start * msg_size;
  395. }
  396. }
  397. while (tot_msgs) {
  398. nodenum_t storage_node_id =
  399. nodenum_t((*(const uint32_t *)buf)>>DEST_UID_BITS);
  400. if (storage_node_id < num_storage_nodes) {
  401. nodenum_t node = g_teems_config.storage_map[storage_node_id];
  402. if (node == my_node_num) {
  403. memmove(myself_buf, buf, msg_size);
  404. myself_buf += msg_size;
  405. } else {
  406. g_commstates[node].message_data(buf, msg_size);
  407. }
  408. }
  409. buf += msg_size;
  410. --tot_msgs;
  411. }
  412. if (myself_buf) {
  413. MsgBuffer &round2 = route_state.round2;
  414. pthread_mutex_lock(&round2.mutex);
  415. round2.inserted += num_msgs_per_stg;
  416. round2.nodes_received += 1;
  417. pthread_mutex_unlock(&round2.mutex);
  418. }
  419. }
  420. // Perform the next round of routing. The callback pointer will be
  421. // passed to ocall_routing_round_complete when the round is complete.
  422. void ecall_routing_proceed(void *cbpointer)
  423. {
  424. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  425. if (route_state.step == ROUTE_NOT_STARTED) {
  426. if (my_roles & ROLE_INGESTION) {
  427. route_state.cbpointer = cbpointer;
  428. MsgBuffer &ingbuf = route_state.ingbuf;
  429. pthread_mutex_lock(&ingbuf.mutex);
  430. // Ensure there are no pending messages currently being inserted
  431. // into the buffer
  432. while (ingbuf.reserved != ingbuf.inserted) {
  433. pthread_mutex_unlock(&ingbuf.mutex);
  434. pthread_mutex_lock(&ingbuf.mutex);
  435. }
  436. // Sort the messages we've received
  437. #ifdef PROFILE_ROUTING
  438. uint32_t inserted = ingbuf.inserted;
  439. unsigned long start_round1 = printf_with_rtclock("begin round1 processing (%u)\n", inserted);
  440. unsigned long start_sort = printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  441. #endif
  442. sort_mtobliv(g_teems_config.nthreads, ingbuf.buf,
  443. g_teems_config.msg_size, ingbuf.inserted,
  444. route_state.tot_msg_per_ing, send_round1_msgs);
  445. #ifdef PROFILE_ROUTING
  446. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  447. printf_with_rtclock_diff(start_round1, "end round1 processing (%u)\n", inserted);
  448. #endif
  449. ingbuf.reset();
  450. pthread_mutex_unlock(&ingbuf.mutex);
  451. }
  452. if (my_roles & ROLE_ROUTING) {
  453. MsgBuffer &round1 = route_state.round1;
  454. pthread_mutex_lock(&round1.mutex);
  455. round1.completed_prev_round = true;
  456. nodenum_t nodes_received = round1.nodes_received;
  457. pthread_mutex_unlock(&round1.mutex);
  458. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  459. route_state.step = ROUTE_ROUND_1;
  460. route_state.cbpointer = NULL;
  461. ocall_routing_round_complete(cbpointer, 1);
  462. }
  463. } else {
  464. route_state.step = ROUTE_ROUND_1;
  465. route_state.round1.completed_prev_round = true;
  466. ocall_routing_round_complete(cbpointer, 1);
  467. }
  468. } else if (route_state.step == ROUTE_ROUND_1) {
  469. if (my_roles & ROLE_ROUTING) {
  470. route_state.cbpointer = cbpointer;
  471. MsgBuffer &round1 = route_state.round1;
  472. pthread_mutex_lock(&round1.mutex);
  473. // Ensure there are no pending messages currently being inserted
  474. // into the buffer
  475. while (round1.reserved != round1.inserted) {
  476. pthread_mutex_unlock(&round1.mutex);
  477. pthread_mutex_lock(&round1.mutex);
  478. }
  479. // If the _total_ number of messages we received in round 1
  480. // is less than the max number of messages we could send to
  481. // _each_ storage node, then cap the number of messages we
  482. // will send to each storage node to that number.
  483. uint32_t msgs_per_stg = route_state.max_msg_to_each_stg;
  484. if (round1.inserted < msgs_per_stg) {
  485. msgs_per_stg = round1.inserted;
  486. }
  487. // Note: at this point, it is required that each message in
  488. // the round1 buffer have a _valid_ storage node id field.
  489. // Obliviously tally the number of messages we received in
  490. // round1 destined for each storage node
  491. #ifdef PROFILE_ROUTING
  492. uint32_t inserted = round1.inserted;
  493. unsigned long start_round2 = printf_with_rtclock("begin round2 processing (%u,%u)\n", inserted, round1.bufsize);
  494. unsigned long start_tally = printf_with_rtclock("begin tally (%u)\n", inserted);
  495. #endif
  496. uint16_t msg_size = g_teems_config.msg_size;
  497. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  498. std::vector<uint32_t> tally = obliv_tally_stg(
  499. round1.buf, msg_size, round1.inserted, num_storage_nodes);
  500. #ifdef PROFILE_ROUTING
  501. printf_with_rtclock_diff(start_tally, "end tally (%u)\n", inserted);
  502. #endif
  503. // Note: tally contains private values! It's OK to
  504. // non-obliviously check for an error condition, though.
  505. // While we're at it, obliviously change the tally of
  506. // messages received to a tally of padding messages
  507. // required.
  508. uint32_t tot_padding = 0;
  509. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  510. if (tally[i] > msgs_per_stg) {
  511. printf("Received too many messages for storage node %u\n", i);
  512. assert(tally[i] <= msgs_per_stg);
  513. }
  514. tally[i] = msgs_per_stg - tally[i];
  515. tot_padding += tally[i];
  516. }
  517. round1.reserved += tot_padding;
  518. assert(round1.reserved <= round1.bufsize);
  519. // Obliviously add padding for each storage node according
  520. // to the (private) padding tally.
  521. #ifdef PROFILE_ROUTING
  522. unsigned long start_pad = printf_with_rtclock("begin pad (%u)\n", tot_padding);
  523. #endif
  524. obliv_pad_stg(round1.buf + round1.inserted * msg_size,
  525. msg_size, tally, tot_padding);
  526. #ifdef PROFILE_ROUTING
  527. printf_with_rtclock_diff(start_pad, "end pad (%u)\n", tot_padding);
  528. #endif
  529. round1.inserted += tot_padding;
  530. // Obliviously shuffle the messages
  531. #ifdef PROFILE_ROUTING
  532. unsigned long start_shuffle = printf_with_rtclock("begin shuffle (%u,%u)\n", round1.inserted, round1.bufsize);
  533. #endif
  534. uint32_t num_shuffled = shuffle_mtobliv(g_teems_config.nthreads,
  535. round1.buf, msg_size, round1.inserted, round1.bufsize);
  536. #ifdef PROFILE_ROUTING
  537. printf_with_rtclock_diff(start_shuffle, "end shuffle (%u,%u)\n", round1.inserted, round1.bufsize);
  538. printf_with_rtclock_diff(start_round2, "end round2 processing (%u,%u)\n", inserted, round1.bufsize);
  539. #endif
  540. // Now we can handle the messages non-obliviously, since we
  541. // know there will be exactly msgs_per_stg messages to each
  542. // storage node, and the oblivious shuffle broke the
  543. // connection between where each message came from and where
  544. // it's going.
  545. send_round2_msgs(num_shuffled, msgs_per_stg);
  546. round1.reset();
  547. pthread_mutex_unlock(&round1.mutex);
  548. }
  549. if (my_roles & ROLE_STORAGE) {
  550. route_state.cbpointer = cbpointer;
  551. MsgBuffer &round2 = route_state.round2;
  552. pthread_mutex_lock(&round2.mutex);
  553. round2.completed_prev_round = true;
  554. nodenum_t nodes_received = round2.nodes_received;
  555. pthread_mutex_unlock(&round2.mutex);
  556. if (nodes_received == g_teems_config.num_routing_nodes) {
  557. route_state.step = ROUTE_ROUND_2;
  558. route_state.cbpointer = NULL;
  559. ocall_routing_round_complete(cbpointer, 2);
  560. }
  561. } else {
  562. route_state.step = ROUTE_ROUND_2;
  563. route_state.round2.completed_prev_round = true;
  564. ocall_routing_round_complete(cbpointer, 2);
  565. }
  566. } else if (route_state.step == ROUTE_ROUND_2) {
  567. if (my_roles & ROLE_STORAGE) {
  568. MsgBuffer &round2 = route_state.round2;
  569. pthread_mutex_lock(&round2.mutex);
  570. // Ensure there are no pending messages currently being inserted
  571. // into the buffer
  572. while (round2.reserved != round2.inserted) {
  573. pthread_mutex_unlock(&round2.mutex);
  574. pthread_mutex_lock(&round2.mutex);
  575. }
  576. #ifdef PROFILE_ROUTING
  577. unsigned long start = printf_with_rtclock("begin storage processing (%u)\n", round2.inserted);
  578. #endif
  579. storage_received(round2.buf, round2.inserted);
  580. #ifdef PROFILE_ROUTING
  581. printf_with_rtclock_diff(start, "end storage processing (%u)\n", round2.inserted);
  582. #endif
  583. round2.reset();
  584. pthread_mutex_unlock(&round2.mutex);
  585. // We're done
  586. route_state.step = ROUTE_NOT_STARTED;
  587. ocall_routing_round_complete(cbpointer, 0);
  588. } else {
  589. // We're done
  590. route_state.step = ROUTE_NOT_STARTED;
  591. ocall_routing_round_complete(cbpointer, 0);
  592. }
  593. }
  594. }