route.cpp 28 KB

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