route.cpp 27 KB

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