route.cpp 54 KB

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