route.cpp 28 KB

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