route.cpp 30 KB

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