route.cpp 29 KB

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