route.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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. #define TRACE_ROUTING
  11. RouteState route_state;
  12. // Computes ceil(x/y) where x and y are integers, x>=0, y>0.
  13. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  14. #ifdef TRACE_ROUTING
  15. // Show (the first 300 of, if there are more) the headers and the first
  16. // few bytes of the body of each message in the buffer
  17. static void show_messages(const char *label, const unsigned char *buffer,
  18. size_t num)
  19. {
  20. if (label) {
  21. printf("%s\n", label);
  22. }
  23. for (size_t i=0; i<num && i<300; ++i) {
  24. const uint32_t *ibuf = (const uint32_t *)buffer;
  25. if (g_teems_config.private_routing) {
  26. printf("%3d R:%08x S:%08x [%08x]\n", i, ibuf[0], ibuf[1],
  27. ibuf[2]);
  28. } else {
  29. printf("%3d R:%08x P:%08x S:%08x [%08x]\n", i, ibuf[0], ibuf[1],
  30. ibuf[2], ibuf[3]);
  31. }
  32. buffer += g_teems_config.msg_size;
  33. }
  34. printf("\n");
  35. }
  36. #endif
  37. // Call this near the end of ecall_config_load, but before
  38. // comms_init_nodestate. Returns true on success, false on failure.
  39. bool route_init()
  40. {
  41. // Compute the maximum number of messages we could receive by direct
  42. // ingestion
  43. // Each ingestion node will have at most
  44. // ceil(user_count/num_ingestion_nodes) users, and each user will
  45. // send at most m_priv_out messages.
  46. uint32_t users_per_ing = CEILDIV(g_teems_config.user_count,
  47. g_teems_config.num_ingestion_nodes);
  48. uint32_t tot_msg_per_ing;
  49. if (g_teems_config.private_routing) {
  50. tot_msg_per_ing = users_per_ing * g_teems_config.m_priv_out;
  51. } else {
  52. tot_msg_per_ing = users_per_ing * g_teems_config.m_pub_out;
  53. }
  54. // Compute the maximum number of messages we could receive in round 1
  55. // In private routing, each ingestion node will send us an
  56. // our_weight/tot_weight fraction of the messages they hold
  57. uint32_t max_msg_from_each_ing;
  58. max_msg_from_each_ing = CEILDIV(tot_msg_per_ing, g_teems_config.tot_weight) *
  59. g_teems_config.my_weight;
  60. // And the maximum number we can receive in total is that times the
  61. // number of ingestion nodes
  62. uint32_t max_round1_msgs = max_msg_from_each_ing *
  63. g_teems_config.num_ingestion_nodes;
  64. // Compute the maximum number of messages we could send in round 2
  65. // Each storage node has at most this many users
  66. uint32_t users_per_stg = CEILDIV(g_teems_config.user_count,
  67. g_teems_config.num_storage_nodes);
  68. // And so can receive at most this many messages
  69. uint32_t tot_msg_per_stg;
  70. if (g_teems_config.private_routing) {
  71. tot_msg_per_stg = users_per_stg * g_teems_config.m_priv_in;
  72. } else {
  73. tot_msg_per_stg = users_per_stg * g_teems_config.m_pub_in;
  74. }
  75. // Which will be at most this many from us
  76. uint32_t max_msg_to_each_stg;
  77. max_msg_to_each_stg = (tot_msg_per_stg/g_teems_config.tot_weight
  78. + g_teems_config.tot_weight) * g_teems_config.my_weight;
  79. // But we can't send more messages to each storage server than we
  80. // could receive in total
  81. if (max_msg_to_each_stg > max_round1_msgs) {
  82. max_msg_to_each_stg = max_round1_msgs;
  83. }
  84. // And the max total number of outgoing messages in round 2 is then
  85. uint32_t max_round2_msgs = max_msg_to_each_stg *
  86. g_teems_config.num_storage_nodes;
  87. // In case we have a weird configuration where users can send more
  88. // messages per epoch than they can receive, ensure the round 2
  89. // buffer is large enough to hold the incoming messages as well
  90. if (max_round2_msgs < max_round1_msgs) {
  91. max_round2_msgs = max_round1_msgs;
  92. }
  93. // The max number of messages that can arrive at a storage server
  94. uint32_t max_stg_msgs;
  95. max_stg_msgs = (tot_msg_per_stg/g_teems_config.tot_weight
  96. + g_teems_config.tot_weight) * g_teems_config.tot_weight;
  97. // Calculating public-routing buffer sizes
  98. // Weights are not used in public routing
  99. // Round up to a multiple of num_routing_nodes
  100. uint32_t max_round1b_msgs_to_adj_rtr = CEILDIV(
  101. (g_teems_config.num_routing_nodes-1)*(g_teems_config.num_routing_nodes-1),
  102. g_teems_config.num_routing_nodes) *
  103. g_teems_config.num_routing_nodes;
  104. // Ensure columnroute constraint that column height is >= 2*(num_routing_nodes-1)^2
  105. // and a multiple of num_routing_nodes
  106. uint32_t max_round1a_msgs = CEILDIV(
  107. std::max(max_round1_msgs, 2*max_round1b_msgs_to_adj_rtr),
  108. g_teems_config.num_routing_nodes) *
  109. g_teems_config.num_routing_nodes;
  110. uint32_t max_round1c_msgs = std::max(max_round1a_msgs,
  111. max_round2_msgs);
  112. /*
  113. 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);
  114. */
  115. #ifdef TRACK_HEAP_USAGE
  116. printf("route_init H1 heap %u\n", g_peak_heap_used);
  117. #endif
  118. // Create the route state
  119. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  120. try {
  121. if (my_roles & ROLE_INGESTION) {
  122. route_state.ingbuf.alloc(tot_msg_per_ing);
  123. }
  124. #ifdef TRACK_HEAP_USAGE
  125. printf("route_init alloc %u msgs\n", tot_msg_per_ing);
  126. printf("route_init H2 heap %u\n", g_peak_heap_used);
  127. #endif
  128. if (my_roles & ROLE_ROUTING) {
  129. route_state.round1.alloc(max_round2_msgs);
  130. #ifdef TRACK_HEAP_USAGE
  131. printf("route_init alloc %u msgs\n", max_round2_msgs);
  132. printf("route_init H3 heap %u\n", g_peak_heap_used);
  133. #endif
  134. if (!g_teems_config.private_routing) {
  135. route_state.round1a.alloc(max_round1a_msgs);
  136. route_state.round1a_sorted.alloc(max_round1a_msgs +
  137. max_round1b_msgs_to_adj_rtr);
  138. // double round 1b buffers to sort with some round 1a messages
  139. route_state.round1b_next.alloc(2*max_round1b_msgs_to_adj_rtr);
  140. route_state.round1c.alloc(max_round1c_msgs);
  141. #ifdef TRACK_HEAP_USAGE
  142. printf("route_init alloc %u msgs\n", max_round1a_msgs);
  143. printf("route_init alloc %u msgs\n", max_round1a_msgs +
  144. max_round1b_msgs_to_adj_rtr);
  145. printf("route_init alloc %u msgs\n", 2*max_round1b_msgs_to_adj_rtr);
  146. printf("route_init alloc %u msgs\n", max_round1c_msgs);
  147. printf("route_init H4 heap %u\n", g_peak_heap_used);
  148. #endif
  149. }
  150. }
  151. if (my_roles & ROLE_STORAGE) {
  152. route_state.round2.alloc(max_stg_msgs);
  153. #ifdef TRACK_HEAP_USAGE
  154. printf("route_init alloc %u msgs\n", max_stg_msgs);
  155. printf("route_init H5 heap %u\n", g_peak_heap_used);
  156. #endif
  157. if (!storage_init(users_per_stg, max_stg_msgs)) {
  158. return false;
  159. }
  160. }
  161. } catch (std::bad_alloc&) {
  162. printf("Memory allocation failed in route_init\n");
  163. return false;
  164. }
  165. route_state.step = ROUTE_NOT_STARTED;
  166. route_state.tot_msg_per_ing = tot_msg_per_ing;
  167. route_state.max_round1_msgs = max_round1_msgs;
  168. route_state.max_round1a_msgs = max_round1a_msgs;
  169. route_state.max_round1b_msgs_to_adj_rtr = max_round1b_msgs_to_adj_rtr;
  170. route_state.max_round1c_msgs = max_round1c_msgs;
  171. route_state.max_msg_to_each_stg = max_msg_to_each_stg;
  172. route_state.max_round2_msgs = max_round2_msgs;
  173. route_state.max_stg_msgs = max_stg_msgs;
  174. route_state.cbpointer = NULL;
  175. threadid_t nthreads = g_teems_config.nthreads;
  176. #ifdef PROFILE_ROUTING
  177. unsigned long start = printf_with_rtclock("begin precompute evalplans (%u,%hu) (%u,%hu)\n", tot_msg_per_ing, nthreads, max_round2_msgs, nthreads);
  178. #endif
  179. if (my_roles & ROLE_INGESTION) {
  180. sort_precompute_evalplan(tot_msg_per_ing, nthreads);
  181. }
  182. if (my_roles & ROLE_ROUTING) {
  183. sort_precompute_evalplan(max_round2_msgs, nthreads);
  184. if(!g_teems_config.private_routing) {
  185. sort_precompute_evalplan(max_round1a_msgs, nthreads);
  186. sort_precompute_evalplan(2*max_round1b_msgs_to_adj_rtr, nthreads);
  187. }
  188. }
  189. if (my_roles & ROLE_STORAGE) {
  190. sort_precompute_evalplan(max_stg_msgs, nthreads);
  191. }
  192. #ifdef PROFILE_ROUTING
  193. printf_with_rtclock_diff(start, "end precompute evalplans\n");
  194. #endif
  195. #ifdef TRACK_HEAP_USAGE
  196. printf("route_init end heap %u\n", g_peak_heap_used);
  197. #endif
  198. return true;
  199. }
  200. // Call when shutting system down to deallocate routing state
  201. void route_close() {
  202. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  203. if (my_roles & ROLE_STORAGE) {
  204. storage_close();
  205. }
  206. }
  207. // Precompute the WaksmanNetworks needed for the sorts. If you pass -1,
  208. // it will return the number of different sizes it needs to regenerate.
  209. // If you pass [0,sizes-1], it will compute one WaksmanNetwork with that
  210. // size index and return the number of available WaksmanNetworks of that
  211. // size. If you pass anything else, it will return the number of
  212. // different sizes it needs at all.
  213. // The list of sizes that need refilling, updated when you pass -1
  214. static std::vector<uint32_t> used_sizes;
  215. size_t ecall_precompute_sort(int sizeidx)
  216. {
  217. size_t ret = 0;
  218. if (sizeidx == -1) {
  219. used_sizes = sort_get_used();
  220. ret = used_sizes.size();
  221. } else if (sizeidx >= 0 && sizeidx < used_sizes.size()) {
  222. uint32_t size = used_sizes[sizeidx];
  223. #ifdef PROFILE_ROUTING
  224. unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", size);
  225. #endif
  226. ret = sort_precompute(size);
  227. #ifdef PROFILE_ROUTING
  228. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", size);
  229. #endif
  230. } else {
  231. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  232. if (my_roles & ROLE_INGESTION) {
  233. used_sizes.push_back(route_state.tot_msg_per_ing);
  234. }
  235. if (my_roles & ROLE_ROUTING) {
  236. used_sizes.push_back(route_state.max_round2_msgs);
  237. if(!g_teems_config.private_routing) {
  238. used_sizes.push_back(route_state.max_round1a_msgs);
  239. used_sizes.push_back(2*route_state.max_round1b_msgs_to_adj_rtr);
  240. }
  241. }
  242. if (my_roles & ROLE_STORAGE) {
  243. used_sizes.push_back(route_state.max_stg_msgs);
  244. if(!g_teems_config.private_routing) {
  245. used_sizes.push_back(route_state.max_stg_msgs);
  246. }
  247. }
  248. ret = used_sizes.size();
  249. }
  250. return ret;
  251. }
  252. static uint8_t* msgbuffer_get_buf(MsgBuffer &msgbuf,
  253. NodeCommState &, uint32_t tot_enc_chunk_size)
  254. {
  255. uint16_t msg_size = g_teems_config.msg_size;
  256. // Chunks will be encrypted and have a MAC tag attached which will
  257. // not correspond to plaintext bytes, so we can trim them.
  258. // The minimum number of chunks needed to transmit this message
  259. uint32_t min_num_chunks =
  260. (tot_enc_chunk_size + (FRAME_SIZE-1)) / FRAME_SIZE;
  261. // The number of plaintext bytes this message could contain
  262. uint32_t plaintext_bytes = tot_enc_chunk_size -
  263. SGX_AESGCM_MAC_SIZE * min_num_chunks;
  264. uint32_t num_msgs = CEILDIV(plaintext_bytes, uint32_t(msg_size));
  265. pthread_mutex_lock(&msgbuf.mutex);
  266. uint32_t start = msgbuf.reserved;
  267. if (start + num_msgs > msgbuf.bufsize) {
  268. pthread_mutex_unlock(&msgbuf.mutex);
  269. printf("Max %u messages exceeded (msgbuffer_get_buf)\n",
  270. msgbuf.bufsize);
  271. return NULL;
  272. }
  273. msgbuf.reserved += num_msgs;
  274. pthread_mutex_unlock(&msgbuf.mutex);
  275. return msgbuf.buf + start * msg_size;
  276. }
  277. static void round1a_received(NodeCommState &nodest,
  278. uint8_t *data, uint32_t plaintext_len, uint32_t);
  279. static void round1b_next_received(NodeCommState &nodest,
  280. uint8_t *data, uint32_t plaintext_len, uint32_t);
  281. static void round1c_received(NodeCommState &nodest, uint8_t *data,
  282. uint32_t plaintext_len, uint32_t);
  283. static void round2_received(NodeCommState &nodest,
  284. uint8_t *data, uint32_t plaintext_len, uint32_t);
  285. // A round 1 message was received by a routing node from an ingestion
  286. // node; we put it into the round 2 buffer for processing in round 2
  287. static void round1_received(NodeCommState &nodest,
  288. uint8_t *data, uint32_t plaintext_len, uint32_t)
  289. {
  290. uint16_t msg_size = g_teems_config.msg_size;
  291. assert((plaintext_len % uint32_t(msg_size)) == 0);
  292. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  293. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  294. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  295. pthread_mutex_lock(&route_state.round1.mutex);
  296. route_state.round1.inserted += num_msgs;
  297. route_state.round1.nodes_received += 1;
  298. nodenum_t nodes_received = route_state.round1.nodes_received;
  299. bool completed_prev_round = route_state.round1.completed_prev_round;
  300. pthread_mutex_unlock(&route_state.round1.mutex);
  301. // What is the next message we expect from this node?
  302. if (g_teems_config.private_routing) {
  303. if ((our_roles & ROLE_STORAGE) && (their_roles & ROLE_ROUTING)) {
  304. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  305. uint32_t tot_enc_chunk_size) {
  306. return msgbuffer_get_buf(route_state.round2, commst,
  307. tot_enc_chunk_size);
  308. };
  309. nodest.in_msg_received = round2_received;
  310. }
  311. // Otherwise, it's just the next round 1 message, so don't change
  312. // the handlers.
  313. } else {
  314. if (their_roles & ROLE_ROUTING) {
  315. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  316. uint32_t tot_enc_chunk_size) {
  317. return msgbuffer_get_buf(route_state.round1a, commst,
  318. tot_enc_chunk_size);
  319. };
  320. nodest.in_msg_received = round1a_received;
  321. }
  322. // Otherwise, it's just the next round 1 message, so don't change
  323. // the handlers.
  324. }
  325. if (nodes_received == g_teems_config.num_ingestion_nodes &&
  326. completed_prev_round) {
  327. route_state.step = ROUTE_ROUND_1;
  328. void *cbpointer = route_state.cbpointer;
  329. route_state.cbpointer = NULL;
  330. ocall_routing_round_complete(cbpointer, 1);
  331. }
  332. }
  333. // A round 1a message was received by a routing node
  334. static void round1a_received(NodeCommState &nodest,
  335. uint8_t *data, uint32_t plaintext_len, uint32_t)
  336. {
  337. uint16_t msg_size = g_teems_config.msg_size;
  338. assert((plaintext_len % uint32_t(msg_size)) == 0);
  339. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  340. pthread_mutex_lock(&route_state.round1a.mutex);
  341. route_state.round1a.inserted += num_msgs;
  342. route_state.round1a.nodes_received += 1;
  343. nodenum_t nodes_received = route_state.round1a.nodes_received;
  344. bool completed_prev_round = route_state.round1a.completed_prev_round;
  345. pthread_mutex_unlock(&route_state.round1a.mutex);
  346. // Both are routing nodes
  347. // We only expect a message from the next node (if it exists)
  348. nodenum_t my_node_num = g_teems_config.my_node_num;
  349. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  350. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  351. if ((prev_nodes < num_routing_nodes-1) &&
  352. (nodest.node_num == g_teems_config.routing_nodes[1])) {
  353. // Node is next routing node
  354. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  355. uint32_t tot_enc_chunk_size) {
  356. return msgbuffer_get_buf(route_state.round1b_next, commst,
  357. tot_enc_chunk_size);
  358. };
  359. nodest.in_msg_received = round1b_next_received;
  360. } else {
  361. // other routing nodes will not send to this node until round 1c
  362. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  363. uint32_t tot_enc_chunk_size) {
  364. return msgbuffer_get_buf(route_state.round1c, commst,
  365. tot_enc_chunk_size);
  366. };
  367. nodest.in_msg_received = round1c_received;
  368. }
  369. if (nodes_received == g_teems_config.num_routing_nodes &&
  370. completed_prev_round) {
  371. route_state.step = ROUTE_ROUND_1A;
  372. void *cbpointer = route_state.cbpointer;
  373. route_state.cbpointer = NULL;
  374. ocall_routing_round_complete(cbpointer, ROUND_1A);
  375. }
  376. }
  377. // A round 1b message was received from the next routing node
  378. static void round1b_next_received(NodeCommState &nodest,
  379. uint8_t *data, uint32_t plaintext_len, uint32_t)
  380. {
  381. uint16_t msg_size = g_teems_config.msg_size;
  382. // There are an extra 5 bytes at the end of this message, containing
  383. // the next receiver id (4 bytes) and the count of messages with
  384. // that receiver id (1 byte)
  385. assert((plaintext_len % uint32_t(msg_size)) == 5);
  386. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  387. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  388. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  389. pthread_mutex_lock(&route_state.round1b_next.mutex);
  390. // Add an extra 1 for the message space taken up by the above 5
  391. // bytes
  392. route_state.round1b_next.inserted += num_msgs + 1;
  393. route_state.round1b_next.nodes_received += 1;
  394. nodenum_t nodes_received = route_state.round1b_next.nodes_received;
  395. bool completed_prev_round = route_state.round1b_next.completed_prev_round;
  396. pthread_mutex_unlock(&route_state.round1b_next.mutex);
  397. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  398. uint32_t tot_enc_chunk_size) {
  399. return msgbuffer_get_buf(route_state.round1c, commst,
  400. tot_enc_chunk_size);
  401. };
  402. nodest.in_msg_received = round1c_received;
  403. nodenum_t my_node_num = g_teems_config.my_node_num;
  404. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  405. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  406. nodenum_t adjacent_nodes;
  407. if (num_routing_nodes == 1) {
  408. adjacent_nodes = 0;
  409. } else if (prev_nodes == num_routing_nodes-1) {
  410. adjacent_nodes = 0;
  411. } else {
  412. adjacent_nodes = 1;
  413. }
  414. if (nodes_received == adjacent_nodes && completed_prev_round) {
  415. route_state.step = ROUTE_ROUND_1B;
  416. void *cbpointer = route_state.cbpointer;
  417. route_state.cbpointer = NULL;
  418. ocall_routing_round_complete(cbpointer, ROUND_1B);
  419. }
  420. }
  421. // Message received in round 1c
  422. static void round1c_received(NodeCommState &nodest, uint8_t *data,
  423. uint32_t plaintext_len, uint32_t)
  424. {
  425. uint16_t msg_size = g_teems_config.msg_size;
  426. assert((plaintext_len % uint32_t(msg_size)) == 0);
  427. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  428. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  429. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  430. pthread_mutex_lock(&route_state.round1c.mutex);
  431. route_state.round1c.inserted += num_msgs;
  432. route_state.round1c.nodes_received += 1;
  433. nodenum_t nodes_received = route_state.round1c.nodes_received;
  434. bool completed_prev_round = route_state.round1c.completed_prev_round;
  435. pthread_mutex_unlock(&route_state.round1c.mutex);
  436. // What is the next message we expect from this node?
  437. if (our_roles & ROLE_STORAGE) {
  438. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  439. uint32_t tot_enc_chunk_size) {
  440. return msgbuffer_get_buf(route_state.round2, commst,
  441. tot_enc_chunk_size);
  442. };
  443. nodest.in_msg_received = round2_received;
  444. } else if (their_roles & ROLE_INGESTION) {
  445. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  446. uint32_t tot_enc_chunk_size) {
  447. return msgbuffer_get_buf(route_state.round1, commst,
  448. tot_enc_chunk_size);
  449. };
  450. nodest.in_msg_received = round1_received;
  451. } else {
  452. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  453. uint32_t tot_enc_chunk_size) {
  454. return msgbuffer_get_buf(route_state.round1a, commst,
  455. tot_enc_chunk_size);
  456. };
  457. nodest.in_msg_received = round1a_received;
  458. }
  459. if (nodes_received == g_teems_config.num_routing_nodes &&
  460. completed_prev_round) {
  461. route_state.step = ROUTE_ROUND_1C;
  462. void *cbpointer = route_state.cbpointer;
  463. route_state.cbpointer = NULL;
  464. ocall_routing_round_complete(cbpointer, ROUND_1C);
  465. }
  466. }
  467. // A round 2 message was received by a storage node from a routing node
  468. static void round2_received(NodeCommState &nodest,
  469. uint8_t *data, uint32_t plaintext_len, uint32_t)
  470. {
  471. uint16_t msg_size = g_teems_config.msg_size;
  472. assert((plaintext_len % uint32_t(msg_size)) == 0);
  473. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  474. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  475. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  476. pthread_mutex_lock(&route_state.round2.mutex);
  477. route_state.round2.inserted += num_msgs;
  478. route_state.round2.nodes_received += 1;
  479. nodenum_t nodes_received = route_state.round2.nodes_received;
  480. bool completed_prev_round = route_state.round2.completed_prev_round;
  481. pthread_mutex_unlock(&route_state.round2.mutex);
  482. // What is the next message we expect from this node?
  483. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  484. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  485. uint32_t tot_enc_chunk_size) {
  486. return msgbuffer_get_buf(route_state.round1, commst,
  487. tot_enc_chunk_size);
  488. };
  489. nodest.in_msg_received = round1_received;
  490. }
  491. // Otherwise, it's just the next round 2 message, so don't change
  492. // the handlers.
  493. if (nodes_received == g_teems_config.num_routing_nodes &&
  494. completed_prev_round) {
  495. route_state.step = ROUTE_ROUND_2;
  496. void *cbpointer = route_state.cbpointer;
  497. route_state.cbpointer = NULL;
  498. ocall_routing_round_complete(cbpointer, 2);
  499. }
  500. }
  501. // For a given other node, set the received message handler to the first
  502. // message we would expect from them, given their roles and our roles.
  503. void route_init_msg_handler(nodenum_t node_num)
  504. {
  505. // Our roles and their roles
  506. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  507. uint8_t their_roles = g_teems_config.roles[node_num];
  508. // The node communication state
  509. NodeCommState &nodest = g_commstates[node_num];
  510. // If we are a routing node (possibly among other roles) and they
  511. // are an ingestion node (possibly among other roles), a round 1
  512. // routing message is the first thing we expect from them. We put
  513. // these messages into the round1 buffer for processing.
  514. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  515. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  516. uint32_t tot_enc_chunk_size) {
  517. return msgbuffer_get_buf(route_state.round1, commst,
  518. tot_enc_chunk_size);
  519. };
  520. nodest.in_msg_received = round1_received;
  521. }
  522. // Otherwise, if we are a storage node (possibly among other roles)
  523. // and they are a routing node (possibly among other roles), a round
  524. // 2 routing message is the first thing we expect from them
  525. else if ((our_roles & ROLE_STORAGE) && (their_roles & ROLE_ROUTING)) {
  526. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  527. uint32_t tot_enc_chunk_size) {
  528. return msgbuffer_get_buf(route_state.round2, commst,
  529. tot_enc_chunk_size);
  530. };
  531. nodest.in_msg_received = round2_received;
  532. }
  533. // Otherwise, we don't expect a message from this node. Set the
  534. // unknown message handler.
  535. else {
  536. nodest.in_msg_get_buf = default_in_msg_get_buf;
  537. nodest.in_msg_received = unknown_in_msg_received;
  538. }
  539. }
  540. // Directly ingest a buffer of num_msgs messages into the ingbuf buffer.
  541. // Return true on success, false on failure.
  542. bool ecall_ingest_raw(uint8_t *msgs, uint32_t num_msgs)
  543. {
  544. uint16_t msg_size = g_teems_config.msg_size;
  545. MsgBuffer &ingbuf = route_state.ingbuf;
  546. pthread_mutex_lock(&ingbuf.mutex);
  547. uint32_t start = ingbuf.reserved;
  548. if (start + num_msgs > route_state.tot_msg_per_ing) {
  549. pthread_mutex_unlock(&ingbuf.mutex);
  550. printf("Max %u messages exceeded (ecall_ingest_raw)\n",
  551. route_state.tot_msg_per_ing);
  552. return false;
  553. }
  554. ingbuf.reserved += num_msgs;
  555. pthread_mutex_unlock(&ingbuf.mutex);
  556. memmove(ingbuf.buf + start * msg_size,
  557. msgs, num_msgs * msg_size);
  558. pthread_mutex_lock(&ingbuf.mutex);
  559. ingbuf.inserted += num_msgs;
  560. pthread_mutex_unlock(&ingbuf.mutex);
  561. return true;
  562. }
  563. // Send messages round-robin, used in rounds 1 and 1c. Note that N here
  564. // is not private. Pass indices = nullptr to just send the messages in
  565. // the order they appear in the msgs buffer.
  566. template<typename T>
  567. static void send_round_robin_msgs(MsgBuffer &round, const uint8_t *msgs,
  568. const T *indices, uint32_t N)
  569. {
  570. uint16_t msg_size = g_teems_config.msg_size;
  571. uint16_t tot_weight;
  572. tot_weight = g_teems_config.tot_weight;
  573. nodenum_t my_node_num = g_teems_config.my_node_num;
  574. uint32_t full_rows;
  575. uint32_t last_row;
  576. full_rows = N / uint32_t(tot_weight);
  577. last_row = N % uint32_t(tot_weight);
  578. for (auto &routing_node: g_teems_config.routing_nodes) {
  579. uint8_t weight = g_teems_config.weights[routing_node].weight;
  580. if (weight == 0) {
  581. // This shouldn't happen, but just in case
  582. continue;
  583. }
  584. uint16_t start_weight =
  585. g_teems_config.weights[routing_node].startweight;
  586. // The number of messages headed for this routing node from the
  587. // full rows
  588. uint32_t num_msgs_full_rows = full_rows * uint32_t(weight);
  589. // The number of messages headed for this routing node from the
  590. // incomplete last row is:
  591. // 0 if last_row < start_weight
  592. // last_row-start_weight if start_weight <= last_row < start_weight + weight
  593. // weight if start_weight + weight <= last_row
  594. uint32_t num_msgs_last_row = 0;
  595. if (start_weight <= last_row && last_row < start_weight + weight) {
  596. num_msgs_last_row = last_row-start_weight;
  597. } else if (start_weight + weight <= last_row) {
  598. num_msgs_last_row = weight;
  599. }
  600. // The total number of messages headed for this routing node
  601. uint32_t num_msgs = num_msgs_full_rows + num_msgs_last_row;
  602. if (routing_node == my_node_num) {
  603. // Special case: we're sending to ourselves; just put the
  604. // messages in our own buffer
  605. pthread_mutex_lock(&round.mutex);
  606. uint32_t start = round.reserved;
  607. if (start + num_msgs > round.bufsize) {
  608. pthread_mutex_unlock(&round.mutex);
  609. printf("Max %u messages exceeded (send_round_robin_msgs)\n",
  610. round.bufsize);
  611. return;
  612. }
  613. round.reserved += num_msgs;
  614. pthread_mutex_unlock(&round.mutex);
  615. uint8_t *buf = round.buf + start * msg_size;
  616. for (uint32_t i=0; i<full_rows; ++i) {
  617. if (indices) {
  618. const T *idxp = indices + i*tot_weight + start_weight;
  619. for (uint32_t j=0; j<weight; ++j) {
  620. memmove(buf, msgs + idxp[j].index()*msg_size, msg_size);
  621. buf += msg_size;
  622. }
  623. } else {
  624. size_t idx = i*tot_weight + start_weight;
  625. memmove(buf, msgs + idx*msg_size, weight*msg_size);
  626. buf += weight*msg_size;
  627. }
  628. }
  629. if (indices) {
  630. const T *idxp = indices + full_rows*tot_weight + start_weight;
  631. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  632. memmove(buf, msgs + idxp[j].index()*msg_size, msg_size);
  633. buf += msg_size;
  634. }
  635. } else {
  636. size_t idx = full_rows*tot_weight + start_weight;
  637. memmove(buf, msgs + idx*msg_size, num_msgs_last_row*msg_size);
  638. buf += num_msgs_last_row*msg_size;
  639. }
  640. pthread_mutex_lock(&round.mutex);
  641. round.inserted += num_msgs;
  642. round.nodes_received += 1;
  643. pthread_mutex_unlock(&round.mutex);
  644. } else {
  645. NodeCommState &nodecom = g_commstates[routing_node];
  646. nodecom.message_start(num_msgs * msg_size);
  647. for (uint32_t i=0; i<full_rows; ++i) {
  648. if (indices) {
  649. const T *idxp = indices + i*tot_weight + start_weight;
  650. for (uint32_t j=0; j<weight; ++j) {
  651. nodecom.message_data(msgs + idxp[j].index()*msg_size,
  652. msg_size);
  653. }
  654. } else {
  655. size_t idx = i*tot_weight + start_weight;
  656. nodecom.message_data(msgs + idx*msg_size, weight*msg_size);
  657. }
  658. }
  659. if (indices) {
  660. const T *idxp = indices + full_rows*tot_weight + start_weight;
  661. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  662. nodecom.message_data(msgs + idxp[j].index()*msg_size,
  663. msg_size);
  664. }
  665. } else {
  666. size_t idx = full_rows*tot_weight + start_weight;
  667. nodecom.message_data(msgs + idx*msg_size,
  668. num_msgs_last_row*msg_size);
  669. }
  670. }
  671. }
  672. }
  673. // Send the round 1a messages from the round 1 buffer, which only occurs in public-channel routing.
  674. // msgs points to the message buffer, indices points to the the sorted indices, and N is the number
  675. // of non-padding items.
  676. static void send_round1a_msgs(const uint8_t *msgs, const UidPriorityKey *indices, uint32_t N) {
  677. uint16_t msg_size = g_teems_config.msg_size;
  678. nodenum_t my_node_num = g_teems_config.my_node_num;
  679. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  680. uint32_t min_msgs_per_node = route_state.max_round1a_msgs / num_routing_nodes;
  681. uint32_t extra_msgs = route_state.max_round1a_msgs % num_routing_nodes;
  682. for (auto &routing_node: g_teems_config.routing_nodes) {
  683. // In this unweighted setting, start_weight represents the position among routing nodes
  684. uint16_t prev_nodes = g_teems_config.weights[routing_node].startweight;
  685. uint32_t start_msg, num_msgs;
  686. if (prev_nodes >= extra_msgs) {
  687. start_msg = min_msgs_per_node * prev_nodes + extra_msgs;
  688. num_msgs = min_msgs_per_node;
  689. } else {
  690. start_msg = min_msgs_per_node * prev_nodes + prev_nodes;
  691. num_msgs = min_msgs_per_node + 1;
  692. }
  693. // take number of messages into account
  694. if (start_msg >= N) {
  695. num_msgs = 0;
  696. } else if (start_msg + num_msgs > N) {
  697. num_msgs = N - start_msg;
  698. }
  699. if (routing_node == my_node_num) {
  700. // Special case: we're sending to ourselves; just put the
  701. // messages in our own buffer
  702. MsgBuffer &round1a = route_state.round1a;
  703. pthread_mutex_lock(&round1a.mutex);
  704. uint32_t start = round1a.reserved;
  705. if (start + num_msgs > round1a.bufsize) {
  706. pthread_mutex_unlock(&round1a.mutex);
  707. printf("Max %u messages exceeded in round 1a\n", round1a.bufsize);
  708. return;
  709. }
  710. round1a.reserved += num_msgs;
  711. pthread_mutex_unlock(&round1a.mutex);
  712. uint8_t *buf = round1a.buf + start * msg_size;
  713. for (uint32_t i=0; i<num_msgs; ++i) {
  714. const UidPriorityKey *idxp = indices + start_msg + i;
  715. memmove(buf, msgs + idxp->index()*msg_size, msg_size);
  716. buf += msg_size;
  717. }
  718. pthread_mutex_lock(&round1a.mutex);
  719. round1a.inserted += num_msgs;
  720. round1a.nodes_received += 1;
  721. pthread_mutex_unlock(&round1a.mutex);
  722. } else {
  723. NodeCommState &nodecom = g_commstates[routing_node];
  724. nodecom.message_start(num_msgs * msg_size);
  725. for (uint32_t i=0; i<num_msgs; ++i) {
  726. const UidPriorityKey *idxp = indices + start_msg + i;
  727. nodecom.message_data(msgs + idxp->index()*msg_size, msg_size);
  728. }
  729. }
  730. }
  731. }
  732. // Send the round 1b messages from the round 1a buffer, which only occurs in public-channel routing.
  733. // msgs points to the message buffer, and N is the number of non-padding items.
  734. // Return the number of messages sent
  735. static uint32_t send_round1b_msgs(const uint8_t *msgs, uint32_t N) {
  736. uint16_t msg_size = g_teems_config.msg_size;
  737. nodenum_t my_node_num = g_teems_config.my_node_num;
  738. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  739. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  740. // send to previous node
  741. if (prev_nodes > 0) {
  742. nodenum_t prev_node = g_teems_config.routing_nodes[num_routing_nodes-1];
  743. NodeCommState &nodecom = g_commstates[prev_node];
  744. uint32_t num_msgs = min(N, route_state.max_round1b_msgs_to_adj_rtr);
  745. // There are an extra 5 bytes at the end of this message: 4
  746. // bytes for the receiver id in the next message we _didn't_
  747. // send, and 1 byte for the number of messages we have at the
  748. // beginning of the buffer of messages we didn't send (max
  749. // pub_in) with the same receiver id
  750. nodecom.message_start(num_msgs * msg_size + 5);
  751. nodecom.message_data(msgs, num_msgs * msg_size);
  752. uint32_t next_receiver_id = 0xffffffff;
  753. uint8_t next_rid_count = 0;
  754. // num_msgs and N are not private, but the contents of the
  755. // buffer are.
  756. if (num_msgs < N) {
  757. next_receiver_id = *(const uint32_t *)(msgs +
  758. num_msgs * msg_size);
  759. next_rid_count = 1;
  760. // If pub_in > 1, obliviously scan messages num_msgs+1 ..
  761. // num_msgs+(pub_in-1) and as long as they have the same
  762. // receiver id as next_receiver_id, add 1 to next_rid_count (but
  763. // don't go past message N of course)
  764. // This count _includes_ the first message already scanned
  765. // above. It is not private.
  766. uint8_t num_to_scan = uint8_t(std::min(N - num_msgs,
  767. uint32_t(g_teems_config.m_pub_in)));
  768. const unsigned char *scan_msg = msgs +
  769. (num_msgs + 1) * msg_size;
  770. for (uint8_t i=1; i<num_to_scan; ++i) {
  771. uint32_t receiver_id = *(const uint32_t *)scan_msg;
  772. next_rid_count += (receiver_id == next_receiver_id);
  773. scan_msg += msg_size;
  774. }
  775. }
  776. nodecom.message_data((const unsigned char *)&next_receiver_id, 4);
  777. nodecom.message_data(&next_rid_count, 1);
  778. return num_msgs;
  779. }
  780. return 0;
  781. }
  782. // Send the round 2 messages from the previous-round buffer, which are already
  783. // padded and shuffled, so this can be done non-obliviously. tot_msgs
  784. // is the total number of messages in the input buffer, which may
  785. // include padding messages added by the shuffle. Those messages are
  786. // not sent anywhere. There are num_msgs_per_stg messages for each
  787. // storage node labelled for that node.
  788. static void send_round2_msgs(uint32_t tot_msgs, uint32_t num_msgs_per_stg, MsgBuffer &prevround)
  789. {
  790. uint16_t msg_size = g_teems_config.msg_size;
  791. const uint8_t* buf = prevround.buf;
  792. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  793. nodenum_t my_node_num = g_teems_config.my_node_num;
  794. uint8_t *myself_buf = NULL;
  795. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  796. nodenum_t node = g_teems_config.storage_nodes[i];
  797. if (node != my_node_num) {
  798. g_commstates[node].message_start(msg_size * num_msgs_per_stg);
  799. } else {
  800. MsgBuffer &round2 = route_state.round2;
  801. pthread_mutex_lock(&round2.mutex);
  802. uint32_t start = round2.reserved;
  803. if (start + num_msgs_per_stg > round2.bufsize) {
  804. pthread_mutex_unlock(&round2.mutex);
  805. printf("Max %u messages exceeded (send_round2_msgs)\n",
  806. round2.bufsize);
  807. return;
  808. }
  809. round2.reserved += num_msgs_per_stg;
  810. pthread_mutex_unlock(&round2.mutex);
  811. myself_buf = round2.buf + start * msg_size;
  812. }
  813. }
  814. while (tot_msgs) {
  815. nodenum_t storage_node_id =
  816. nodenum_t((*(const uint32_t *)buf)>>DEST_UID_BITS);
  817. if (storage_node_id < num_storage_nodes) {
  818. nodenum_t node = g_teems_config.storage_map[storage_node_id];
  819. if (node == my_node_num) {
  820. memmove(myself_buf, buf, msg_size);
  821. myself_buf += msg_size;
  822. } else {
  823. g_commstates[node].message_data(buf, msg_size);
  824. }
  825. }
  826. buf += msg_size;
  827. --tot_msgs;
  828. }
  829. if (myself_buf) {
  830. MsgBuffer &round2 = route_state.round2;
  831. pthread_mutex_lock(&round2.mutex);
  832. round2.inserted += num_msgs_per_stg;
  833. round2.nodes_received += 1;
  834. pthread_mutex_unlock(&round2.mutex);
  835. }
  836. }
  837. static void round1a_processing(void *cbpointer) {
  838. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  839. MsgBuffer &round1 = route_state.round1;
  840. if (my_roles & ROLE_ROUTING) {
  841. route_state.cbpointer = cbpointer;
  842. pthread_mutex_lock(&round1.mutex);
  843. // Ensure there are no pending messages currently being inserted
  844. // into the buffer
  845. while (round1.reserved != round1.inserted) {
  846. pthread_mutex_unlock(&round1.mutex);
  847. pthread_mutex_lock(&round1.mutex);
  848. }
  849. #ifdef TRACE_ROUTING
  850. show_messages("Start of round 1a", round1.buf, round1.inserted);
  851. #endif
  852. #ifdef PROFILE_ROUTING
  853. uint32_t inserted = round1.inserted;
  854. unsigned long start_round1a =
  855. printf_with_rtclock("begin round1a processing (%u)\n", inserted);
  856. // Sort the messages we've received
  857. unsigned long start_sort =
  858. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  859. route_state.max_round1_msgs);
  860. #endif
  861. // Sort received messages by increasing user ID and
  862. // priority. Larger priority number indicates higher priority.
  863. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, round1.buf,
  864. g_teems_config.msg_size, round1.inserted, route_state.max_round1_msgs,
  865. send_round1a_msgs);
  866. #ifdef PROFILE_ROUTING
  867. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  868. inserted, route_state.max_round1_msgs);
  869. printf_with_rtclock_diff(start_round1a, "end round1a processing (%u)\n",
  870. inserted);
  871. #endif
  872. round1.reset();
  873. pthread_mutex_unlock(&round1.mutex);
  874. MsgBuffer &round1a = route_state.round1a;
  875. pthread_mutex_lock(&round1a.mutex);
  876. round1a.completed_prev_round = true;
  877. nodenum_t nodes_received = round1a.nodes_received;
  878. pthread_mutex_unlock(&round1a.mutex);
  879. if (nodes_received == g_teems_config.num_routing_nodes) {
  880. route_state.step = ROUTE_ROUND_1A;
  881. route_state.cbpointer = NULL;
  882. ocall_routing_round_complete(cbpointer, ROUND_1A);
  883. }
  884. } else {
  885. route_state.step = ROUTE_ROUND_1A;
  886. route_state.round1a.completed_prev_round = true;
  887. ocall_routing_round_complete(cbpointer, ROUND_1A);
  888. }
  889. }
  890. static void round1b_processing(void *cbpointer) {
  891. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  892. nodenum_t my_node_num = g_teems_config.my_node_num;
  893. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  894. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  895. MsgBuffer &round1a = route_state.round1a;
  896. MsgBuffer &round1a_sorted = route_state.round1a_sorted;
  897. if (my_roles & ROLE_ROUTING) {
  898. route_state.cbpointer = cbpointer;
  899. pthread_mutex_lock(&round1a.mutex);
  900. // Ensure there are no pending messages currently being inserted
  901. // into the buffer
  902. while (round1a.reserved != round1a.inserted) {
  903. pthread_mutex_unlock(&round1a.mutex);
  904. pthread_mutex_lock(&round1a.mutex);
  905. }
  906. pthread_mutex_lock(&round1a_sorted.mutex);
  907. #ifdef TRACE_ROUTING
  908. show_messages("Start of round 1b", round1a.buf, round1a.inserted);
  909. #endif
  910. #ifdef PROFILE_ROUTING
  911. uint32_t inserted = round1a.inserted;
  912. unsigned long start_round1b =
  913. printf_with_rtclock("begin round1b processing (%u)\n", inserted);
  914. #endif
  915. // Sort the messages we've received
  916. // Sort received messages by increasing user ID and
  917. // priority. Larger priority number indicates higher priority.
  918. if (inserted > 0) {
  919. // copy items in sorted order into round1a_sorted
  920. #ifdef PROFILE_ROUTING
  921. unsigned long start_sort =
  922. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  923. route_state.max_round1a_msgs);
  924. #endif
  925. route_state.round1a_sorted.reserved = round1a.inserted;
  926. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, round1a.buf,
  927. g_teems_config.msg_size, round1a.inserted,
  928. route_state.max_round1a_msgs,
  929. route_state.round1a_sorted.buf);
  930. route_state.round1a_sorted.inserted = round1a.inserted;
  931. #ifdef PROFILE_ROUTING
  932. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  933. inserted, route_state.max_round1a_msgs);
  934. #endif
  935. #ifdef TRACE_ROUTING
  936. show_messages("In round 1b (sorted)", round1a_sorted.buf,
  937. round1a_sorted.inserted);
  938. #endif
  939. uint32_t num_sent = send_round1b_msgs(round1a_sorted.buf,
  940. round1a_sorted.inserted);
  941. // Remove the sent messages from the buffer
  942. memmove(round1a_sorted.buf,
  943. round1a_sorted.buf + num_sent * g_teems_config.msg_size,
  944. (round1a_sorted.inserted - num_sent) * g_teems_config.msg_size);
  945. round1a_sorted.inserted -= num_sent;
  946. round1a_sorted.reserved -= num_sent;
  947. #ifdef TRACE_ROUTING
  948. show_messages("In round 1b (after sending initial block)",
  949. round1a_sorted.buf, round1a_sorted.inserted);
  950. #endif
  951. } else {
  952. send_round1b_msgs(NULL, 0);
  953. }
  954. #ifdef PROFILE_ROUTING
  955. printf_with_rtclock_diff(start_round1b, "end round1b processing (%u)\n",
  956. inserted);
  957. #endif
  958. pthread_mutex_unlock(&round1a_sorted.mutex);
  959. pthread_mutex_unlock(&round1a.mutex);
  960. MsgBuffer &round1b_next = route_state.round1b_next;
  961. pthread_mutex_lock(&round1b_next.mutex);
  962. round1b_next.completed_prev_round = true;
  963. nodenum_t nodes_received = round1b_next.nodes_received;
  964. pthread_mutex_unlock(&round1b_next.mutex);
  965. nodenum_t adjacent_nodes;
  966. if (num_routing_nodes == 1) {
  967. adjacent_nodes = 0;
  968. } else if (prev_nodes == num_routing_nodes-1) {
  969. adjacent_nodes = 0;
  970. } else {
  971. adjacent_nodes = 1;
  972. }
  973. if (nodes_received == adjacent_nodes) {
  974. route_state.step = ROUTE_ROUND_1B;
  975. route_state.cbpointer = NULL;
  976. ocall_routing_round_complete(cbpointer, ROUND_1B);
  977. }
  978. } else {
  979. route_state.step = ROUTE_ROUND_1B;
  980. route_state.round1b_next.completed_prev_round = true;
  981. ocall_routing_round_complete(cbpointer, ROUND_1B);
  982. }
  983. }
  984. static void copy_msgs(uint8_t *dst, uint32_t start_msg, uint32_t num_copy, const uint8_t *src,
  985. const UidPriorityKey *indices)
  986. {
  987. uint16_t msg_size = g_teems_config.msg_size;
  988. const UidPriorityKey *idxp = indices + start_msg;
  989. uint8_t *buf = dst;
  990. for (uint32_t i=0; i<num_copy; i++) {
  991. memmove(buf, src + idxp[i].index()*msg_size, msg_size);
  992. buf += msg_size;
  993. }
  994. }
  995. static void round1c_processing(void *cbpointer) {
  996. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  997. nodenum_t my_node_num = g_teems_config.my_node_num;
  998. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  999. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  1000. uint16_t msg_size = g_teems_config.msg_size;
  1001. uint32_t max_round1b_msgs_to_adj_rtr = route_state.max_round1b_msgs_to_adj_rtr;
  1002. uint32_t max_round1a_msgs = route_state.max_round1a_msgs;
  1003. MsgBuffer &round1a = route_state.round1a;
  1004. MsgBuffer &round1a_sorted = route_state.round1a_sorted;
  1005. MsgBuffer &round1b_next = route_state.round1b_next;
  1006. if (my_roles & ROLE_ROUTING) {
  1007. route_state.cbpointer = cbpointer;
  1008. pthread_mutex_lock(&round1b_next.mutex);
  1009. // Ensure there are no pending messages currently being inserted
  1010. // into the round 1b buffer
  1011. while (round1b_next.reserved != round1b_next.inserted) {
  1012. pthread_mutex_unlock(&round1b_next.mutex);
  1013. pthread_mutex_lock(&round1b_next.mutex);
  1014. }
  1015. uint32_t next_receiver_id = 0xffffffff;
  1016. uint8_t next_rid_count = 0;
  1017. // Extract the trailing 5 bytes if we received a round1b message
  1018. if (round1b_next.inserted >= 1) {
  1019. next_receiver_id = *(uint32_t *)(round1b_next.buf +
  1020. (round1b_next.inserted-1)*msg_size);
  1021. next_rid_count = *(round1b_next.buf +
  1022. (round1b_next.inserted-1)*msg_size + 4);
  1023. round1b_next.inserted -= 1;
  1024. round1b_next.reserved -= 1;
  1025. }
  1026. pthread_mutex_lock(&round1a.mutex);
  1027. pthread_mutex_lock(&round1a_sorted.mutex);
  1028. #ifdef TRACE_ROUTING
  1029. show_messages("Start of round 1c", round1a_sorted.buf,
  1030. round1a_sorted.inserted);
  1031. #endif
  1032. #ifdef PROFILE_ROUTING
  1033. unsigned long start_round1c = printf_with_rtclock("begin round1c processing (%u)\n", round1a.inserted);
  1034. #endif
  1035. // sort round1b_next msgs with final msgs in round1a_sorted
  1036. if ((prev_nodes < num_routing_nodes-1) && (round1b_next.inserted > 0)) {
  1037. // Copy final msgs in round1a_sorted to round1b_next buffer for sorting
  1038. // Note that all inserted values and buffer sizes are non-secret
  1039. uint32_t num_final_round1a = std::min(
  1040. round1a_sorted.inserted, max_round1b_msgs_to_adj_rtr);
  1041. uint32_t round1a_msg_start =
  1042. round1a_sorted.inserted-num_final_round1a;
  1043. uint32_t num_round1b_next = round1b_next.inserted;
  1044. round1b_next.reserved += num_final_round1a;
  1045. memmove(round1b_next.buf+num_round1b_next*msg_size,
  1046. round1a_sorted.buf + round1a_msg_start*msg_size,
  1047. num_final_round1a*msg_size);
  1048. round1b_next.inserted += num_final_round1a;
  1049. round1a_sorted.reserved -= num_final_round1a;
  1050. round1a_sorted.inserted -= num_final_round1a;
  1051. #ifdef TRACE_ROUTING
  1052. show_messages("In round 1c (after setting aside)",
  1053. round1a_sorted.buf, round1a_sorted.inserted);
  1054. show_messages("In round 1c (the aside)", round1b_next.buf,
  1055. round1b_next.inserted);
  1056. #endif
  1057. // sort and append to round1a msgs
  1058. uint32_t num_copy = round1b_next.inserted;
  1059. #ifdef PROFILE_ROUTING
  1060. unsigned long start_sort =
  1061. printf_with_rtclock("begin round1b_next oblivious sort (%u,%u)\n",
  1062. num_copy, 2*max_round1b_msgs_to_adj_rtr);
  1063. #endif
  1064. round1a_sorted.reserved += num_copy;
  1065. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads,
  1066. round1b_next.buf, msg_size, num_copy,
  1067. 2*max_round1b_msgs_to_adj_rtr,
  1068. [&](const uint8_t *src, const UidPriorityKey *indices, uint32_t Nr) {
  1069. return copy_msgs(round1a_sorted.buf +
  1070. round1a_sorted.inserted * msg_size, 0, num_copy,
  1071. src, indices);
  1072. }
  1073. );
  1074. round1a_sorted.inserted += num_copy;
  1075. #ifdef PROFILE_ROUTING
  1076. printf_with_rtclock_diff(start_sort,
  1077. "end round1b_next oblivious sort (%u,%u)\n",
  1078. num_copy, 2*max_round1b_msgs_to_adj_rtr);
  1079. #endif
  1080. }
  1081. #ifdef TRACE_ROUTING
  1082. printf("round1a_sorted.inserted = %lu, reserved = %lu, bufsize = %lu\n",
  1083. round1a_sorted.inserted, round1a_sorted.reserved,
  1084. round1a_sorted.bufsize);
  1085. show_messages("In round 1c before padding pass", round1a_sorted.buf,
  1086. round1a_sorted.inserted);
  1087. #endif
  1088. // The round1a_sorted buffer is now sorted by receiver uid and
  1089. // priority. Going from the end of the buffer to the beginning
  1090. // (so as to encounter and keep the highest-priority messages
  1091. // for any given receiver first), obliviously turn any messages
  1092. // over the limit of pub_in for any given receiver into padding.
  1093. // Also keep track of which messages are not padding for use in
  1094. // later compaction.
  1095. bool *is_not_padding = new bool[round1a_sorted.inserted];
  1096. for (uint32_t i=0; i<round1a_sorted.inserted; ++i) {
  1097. uint8_t *header = round1a_sorted.buf +
  1098. msg_size * (round1a_sorted.inserted - 1 - i);
  1099. uint32_t receiver_id = *(uint32_t*)header;
  1100. uint32_t pub_in = uint32_t(g_teems_config.m_pub_in);
  1101. // These are the possible cases and what we need to do in
  1102. // each case, but we have to evaluate them obliviously
  1103. // receiver_id != next_receiver_id:
  1104. // next_receiver_id = receiver_id
  1105. // next_rid_count = 1
  1106. // become_padding = 0
  1107. // receiver_id == next_receiver_id && next_rid_count < pub_in:
  1108. // next_receiver_id = receiver_id
  1109. // next_rid_count = next_rid_count + 1
  1110. // become_padding = 0
  1111. // receiver_id == next_receiver_id && next_rid_count >= pub_in:
  1112. // next_receiver_id = receiver_id
  1113. // next_rid_count = next_rid_count
  1114. // become_padding = 1
  1115. bool same_receiver_id = (receiver_id == next_receiver_id);
  1116. // If same_receiver_id is 0, reset next_rid_count to 0.
  1117. // If same_receiver_id is 1, don't change next_rid_count.
  1118. // This method (AND with -same_receiver_id) is more likely
  1119. // to be constant time than multiplying by same_receiver_id.
  1120. next_rid_count &= (-(uint32_t(same_receiver_id)));
  1121. bool become_padding = (next_rid_count >= pub_in);
  1122. next_rid_count += !become_padding;
  1123. next_receiver_id = receiver_id;
  1124. // Obliviously change the receiver id to 0xffffffff
  1125. // (padding) if become-padding is 1
  1126. receiver_id |= (-(uint32_t(become_padding)));
  1127. *(uint32_t*)header = receiver_id;
  1128. is_not_padding[round1a_sorted.inserted - 1 - i] =
  1129. (receiver_id != 0xffffffff);
  1130. }
  1131. #ifdef TRACE_ROUTING
  1132. show_messages("In round 1c after padding pass", round1a_sorted.buf,
  1133. round1a_sorted.inserted);
  1134. #endif
  1135. // Oblivious compaction to move the padding messages to the end,
  1136. // preserving the (already-sorted) order of the non-padding
  1137. // messages
  1138. TightCompact_parallel<OSWAP_16X>(
  1139. (unsigned char *) round1a_sorted.buf,
  1140. round1a_sorted.inserted, msg_size, is_not_padding,
  1141. g_teems_config.nthreads);
  1142. delete[] is_not_padding;
  1143. #ifdef TRACE_ROUTING
  1144. show_messages("In round 1c after compaction", round1a_sorted.buf,
  1145. round1a_sorted.inserted);
  1146. #endif
  1147. send_round_robin_msgs<UidPriorityKey>(route_state.round1c,
  1148. round1a_sorted.buf, NULL, round1a_sorted.inserted);
  1149. #ifdef PROFILE_ROUTING
  1150. printf_with_rtclock_diff(start_round1c, "end round1c processing (%u)\n",
  1151. round1a.inserted);
  1152. #endif
  1153. round1a.reset();
  1154. round1a_sorted.reset();
  1155. round1b_next.reset();
  1156. pthread_mutex_unlock(&round1a_sorted.mutex);
  1157. pthread_mutex_unlock(&round1a.mutex);
  1158. pthread_mutex_unlock(&round1b_next.mutex);
  1159. MsgBuffer &round1c = route_state.round1c;
  1160. pthread_mutex_lock(&round1c.mutex);
  1161. round1c.completed_prev_round = true;
  1162. nodenum_t nodes_received = round1c.nodes_received;
  1163. pthread_mutex_unlock(&round1c.mutex);
  1164. if (nodes_received == num_routing_nodes) {
  1165. route_state.step = ROUTE_ROUND_1C;
  1166. route_state.cbpointer = NULL;
  1167. ocall_routing_round_complete(cbpointer, ROUND_1C);
  1168. }
  1169. } else {
  1170. route_state.step = ROUTE_ROUND_1C;
  1171. route_state.round1c.completed_prev_round = true;
  1172. ocall_routing_round_complete(cbpointer, ROUND_1C);
  1173. }
  1174. }
  1175. // Process messages in round 2
  1176. static void round2_processing(uint8_t my_roles, void *cbpointer, MsgBuffer &prevround) {
  1177. if (my_roles & ROLE_ROUTING) {
  1178. route_state.cbpointer = cbpointer;
  1179. pthread_mutex_lock(&prevround.mutex);
  1180. // Ensure there are no pending messages currently being inserted
  1181. // into the buffer
  1182. while (prevround.reserved != prevround.inserted) {
  1183. pthread_mutex_unlock(&prevround.mutex);
  1184. pthread_mutex_lock(&prevround.mutex);
  1185. }
  1186. // If the _total_ number of messages we received in round 1
  1187. // is less than the max number of messages we could send to
  1188. // _each_ storage node, then cap the number of messages we
  1189. // will send to each storage node to that number.
  1190. uint32_t msgs_per_stg = route_state.max_msg_to_each_stg;
  1191. if (prevround.inserted < msgs_per_stg) {
  1192. msgs_per_stg = prevround.inserted;
  1193. }
  1194. // Note: at this point, it is required that each message in
  1195. // the prevround buffer have a _valid_ storage node id field.
  1196. // Obliviously tally the number of messages we received in
  1197. // the previous round destined for each storage node
  1198. #ifdef TRACE_ROUTING
  1199. show_messages("Start of round 2", prevround.buf, prevround.inserted);
  1200. #endif
  1201. #ifdef PROFILE_ROUTING
  1202. unsigned long start_round2 = printf_with_rtclock("begin round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1203. unsigned long start_tally = printf_with_rtclock("begin tally (%u)\n", prevround.inserted);
  1204. #endif
  1205. uint16_t msg_size = g_teems_config.msg_size;
  1206. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  1207. std::vector<uint32_t> tally = obliv_tally_stg(
  1208. prevround.buf, msg_size, prevround.inserted, num_storage_nodes);
  1209. #ifdef PROFILE_ROUTING
  1210. printf_with_rtclock_diff(start_tally, "end tally (%u)\n", prevround.inserted);
  1211. #endif
  1212. // Note: tally contains private values! It's OK to
  1213. // non-obliviously check for an error condition, though.
  1214. // While we're at it, obliviously change the tally of
  1215. // messages received to a tally of padding messages
  1216. // required.
  1217. uint32_t tot_messages = msgs_per_stg * num_storage_nodes;
  1218. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  1219. if (tally[i] > msgs_per_stg) {
  1220. printf("Received too many messages for storage node "
  1221. "%u (%u > %u)\n", i, tally[i], msgs_per_stg);
  1222. assert(tally[i] <= msgs_per_stg);
  1223. }
  1224. tally[i] = msgs_per_stg - tally[i];
  1225. }
  1226. // Allocate extra padding messages (not yet destined for a
  1227. // particular storage node)
  1228. assert(prevround.inserted <= tot_messages &&
  1229. tot_messages <= prevround.bufsize);
  1230. prevround.reserved = tot_messages;
  1231. for (uint32_t i=prevround.inserted; i<tot_messages; ++i) {
  1232. uint8_t *header = prevround.buf + i*msg_size;
  1233. *(uint32_t*)header = 0xffffffff;
  1234. }
  1235. // Obliviously convert global padding (receiver id 0xffffffff)
  1236. // into padding for each storage node according to the (private)
  1237. // padding tally.
  1238. #ifdef PROFILE_ROUTING
  1239. unsigned long start_pad =
  1240. printf_with_rtclock("begin pad (%u)\n", tot_messages);
  1241. #endif
  1242. obliv_stg_padding(prevround.buf, msg_size, tally, tot_messages);
  1243. #ifdef PROFILE_ROUTING
  1244. printf_with_rtclock_diff(start_pad, "end pad (%u)\n",
  1245. tot_messages);
  1246. #endif
  1247. prevround.inserted = tot_messages;
  1248. #ifdef TRACE_ROUTING
  1249. show_messages("In round 2 after padding", prevround.buf, prevround.inserted);
  1250. #endif
  1251. // Obliviously shuffle the messages
  1252. #ifdef PROFILE_ROUTING
  1253. unsigned long start_shuffle = printf_with_rtclock("begin shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1254. #endif
  1255. uint32_t num_shuffled = shuffle_mtobliv(g_teems_config.nthreads,
  1256. prevround.buf, msg_size, prevround.inserted, prevround.bufsize);
  1257. #ifdef PROFILE_ROUTING
  1258. printf_with_rtclock_diff(start_shuffle, "end shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1259. printf_with_rtclock_diff(start_round2, "end round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1260. #endif
  1261. // Now we can handle the messages non-obliviously, since we
  1262. // know there will be exactly msgs_per_stg messages to each
  1263. // storage node, and the oblivious shuffle broke the
  1264. // connection between where each message came from and where
  1265. // it's going.
  1266. send_round2_msgs(num_shuffled, msgs_per_stg, prevround);
  1267. prevround.reset();
  1268. pthread_mutex_unlock(&prevround.mutex);
  1269. }
  1270. if (my_roles & ROLE_STORAGE) {
  1271. route_state.cbpointer = cbpointer;
  1272. MsgBuffer &round2 = route_state.round2;
  1273. pthread_mutex_lock(&round2.mutex);
  1274. round2.completed_prev_round = true;
  1275. nodenum_t nodes_received = round2.nodes_received;
  1276. pthread_mutex_unlock(&round2.mutex);
  1277. if (nodes_received == g_teems_config.num_routing_nodes) {
  1278. route_state.step = ROUTE_ROUND_2;
  1279. route_state.cbpointer = NULL;
  1280. ocall_routing_round_complete(cbpointer, 2);
  1281. }
  1282. } else {
  1283. route_state.step = ROUTE_ROUND_2;
  1284. route_state.round2.completed_prev_round = true;
  1285. ocall_routing_round_complete(cbpointer, 2);
  1286. }
  1287. }
  1288. // Perform the next round of routing. The callback pointer will be
  1289. // passed to ocall_routing_round_complete when the round is complete.
  1290. void ecall_routing_proceed(void *cbpointer)
  1291. {
  1292. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  1293. if (route_state.step == ROUTE_NOT_STARTED) {
  1294. if (my_roles & ROLE_INGESTION) {
  1295. ingestion_epoch++;
  1296. route_state.cbpointer = cbpointer;
  1297. MsgBuffer &ingbuf = route_state.ingbuf;
  1298. pthread_mutex_lock(&ingbuf.mutex);
  1299. // Ensure there are no pending messages currently being inserted
  1300. // into the buffer
  1301. while (ingbuf.reserved != ingbuf.inserted) {
  1302. pthread_mutex_unlock(&ingbuf.mutex);
  1303. pthread_mutex_lock(&ingbuf.mutex);
  1304. }
  1305. // Sort the messages we've received
  1306. #ifdef TRACE_ROUTING
  1307. show_messages("Start of round 1", ingbuf.buf, ingbuf.inserted);
  1308. #endif
  1309. #ifdef PROFILE_ROUTING
  1310. uint32_t inserted = ingbuf.inserted;
  1311. unsigned long start_round1 =
  1312. printf_with_rtclock("begin round1 processing (%u)\n", inserted);
  1313. unsigned long start_sort =
  1314. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  1315. route_state.tot_msg_per_ing);
  1316. #endif
  1317. if (g_teems_config.private_routing) {
  1318. sort_mtobliv<UidKey>(g_teems_config.nthreads, ingbuf.buf,
  1319. g_teems_config.msg_size, ingbuf.inserted,
  1320. route_state.tot_msg_per_ing,
  1321. [&](const uint8_t *msgs, const UidKey *indices, uint32_t N) {
  1322. send_round_robin_msgs<UidKey>(route_state.round1,
  1323. msgs, indices, N);
  1324. });
  1325. } else {
  1326. // Sort received messages by increasing user ID and
  1327. // priority. Larger priority number indicates higher priority.
  1328. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, ingbuf.buf,
  1329. g_teems_config.msg_size, ingbuf.inserted,
  1330. route_state.tot_msg_per_ing,
  1331. [&](const uint8_t *msgs,
  1332. const UidPriorityKey *indices, uint32_t N) {
  1333. send_round_robin_msgs<UidPriorityKey>(route_state.round1,
  1334. msgs, indices, N);
  1335. });
  1336. }
  1337. #ifdef PROFILE_ROUTING
  1338. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  1339. inserted, route_state.tot_msg_per_ing);
  1340. printf_with_rtclock_diff(start_round1, "end round1 processing (%u)\n",
  1341. inserted);
  1342. #endif
  1343. ingbuf.reset();
  1344. pthread_mutex_unlock(&ingbuf.mutex);
  1345. }
  1346. if (my_roles & ROLE_ROUTING) {
  1347. MsgBuffer &round1 = route_state.round1;
  1348. pthread_mutex_lock(&round1.mutex);
  1349. round1.completed_prev_round = true;
  1350. nodenum_t nodes_received = round1.nodes_received;
  1351. pthread_mutex_unlock(&round1.mutex);
  1352. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  1353. route_state.step = ROUTE_ROUND_1;
  1354. route_state.cbpointer = NULL;
  1355. ocall_routing_round_complete(cbpointer, 1);
  1356. }
  1357. } else {
  1358. route_state.step = ROUTE_ROUND_1;
  1359. route_state.round1.completed_prev_round = true;
  1360. ocall_routing_round_complete(cbpointer, 1);
  1361. }
  1362. } else if (route_state.step == ROUTE_ROUND_1) {
  1363. if (g_teems_config.private_routing) { // private routing next round
  1364. round2_processing(my_roles, cbpointer, route_state.round1);
  1365. } else { // public routing next round
  1366. round1a_processing(cbpointer);
  1367. }
  1368. } else if (route_state.step == ROUTE_ROUND_1A) {
  1369. round1b_processing(cbpointer);
  1370. } else if (route_state.step == ROUTE_ROUND_1B) {
  1371. round1c_processing(cbpointer);
  1372. } else if (route_state.step == ROUTE_ROUND_1C) {
  1373. round2_processing(my_roles, cbpointer, route_state.round1c);
  1374. } else if (route_state.step == ROUTE_ROUND_2) {
  1375. if (my_roles & ROLE_STORAGE) {
  1376. MsgBuffer &round2 = route_state.round2;
  1377. pthread_mutex_lock(&round2.mutex);
  1378. // Ensure there are no pending messages currently being inserted
  1379. // into the buffer
  1380. while (round2.reserved != round2.inserted) {
  1381. pthread_mutex_unlock(&round2.mutex);
  1382. pthread_mutex_lock(&round2.mutex);
  1383. }
  1384. unsigned long start = printf_with_rtclock("begin storage processing (%u)\n", round2.inserted);
  1385. storage_received(round2);
  1386. printf_with_rtclock_diff(start, "end storage processing (%u)\n", round2.inserted);
  1387. // We're done
  1388. route_state.step = ROUTE_NOT_STARTED;
  1389. ocall_routing_round_complete(cbpointer, 0);
  1390. } else {
  1391. // We're done
  1392. route_state.step = ROUTE_NOT_STARTED;
  1393. ocall_routing_round_complete(cbpointer, 0);
  1394. }
  1395. }
  1396. }