route.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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 (nodes_received == adjacent_nodes && completed_prev_round) {
  357. route_state.step = ROUTE_ROUND_1B;
  358. void *cbpointer = route_state.cbpointer;
  359. route_state.cbpointer = NULL;
  360. ocall_routing_round_complete(cbpointer, ROUND_1B);
  361. }
  362. }
  363. // Message received in round 1c
  364. static void round1c_received(NodeCommState &nodest, uint8_t *data,
  365. uint32_t plaintext_len, uint32_t)
  366. {
  367. uint16_t msg_size = g_teems_config.msg_size;
  368. assert((plaintext_len % uint32_t(msg_size)) == 0);
  369. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  370. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  371. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  372. pthread_mutex_lock(&route_state.round1c.mutex);
  373. route_state.round1c.inserted += num_msgs;
  374. route_state.round1c.nodes_received += 1;
  375. nodenum_t nodes_received = route_state.round1c.nodes_received;
  376. bool completed_prev_round = route_state.round1c.completed_prev_round;
  377. pthread_mutex_unlock(&route_state.round1c.mutex);
  378. // What is the next message we expect from this node?
  379. if (our_roles & ROLE_STORAGE) {
  380. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  381. uint32_t tot_enc_chunk_size) {
  382. return msgbuffer_get_buf(route_state.round2, commst,
  383. tot_enc_chunk_size);
  384. };
  385. nodest.in_msg_received = round2_received;
  386. } else if (their_roles & ROLE_INGESTION) {
  387. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  388. uint32_t tot_enc_chunk_size) {
  389. return msgbuffer_get_buf(route_state.round1, commst,
  390. tot_enc_chunk_size);
  391. };
  392. nodest.in_msg_received = round1_received;
  393. } else {
  394. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  395. uint32_t tot_enc_chunk_size) {
  396. return msgbuffer_get_buf(route_state.round1a, commst,
  397. tot_enc_chunk_size);
  398. };
  399. nodest.in_msg_received = round1a_received;
  400. }
  401. if (nodes_received == g_teems_config.num_routing_nodes &&
  402. completed_prev_round) {
  403. route_state.step = ROUTE_ROUND_1C;
  404. void *cbpointer = route_state.cbpointer;
  405. route_state.cbpointer = NULL;
  406. ocall_routing_round_complete(cbpointer, ROUND_1C);
  407. }
  408. }
  409. // A round 2 message was received by a storage node from a routing node
  410. static void round2_received(NodeCommState &nodest,
  411. uint8_t *data, uint32_t plaintext_len, uint32_t)
  412. {
  413. uint16_t msg_size = g_teems_config.msg_size;
  414. assert((plaintext_len % uint32_t(msg_size)) == 0);
  415. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  416. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  417. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  418. pthread_mutex_lock(&route_state.round2.mutex);
  419. route_state.round2.inserted += num_msgs;
  420. route_state.round2.nodes_received += 1;
  421. nodenum_t nodes_received = route_state.round2.nodes_received;
  422. bool completed_prev_round = route_state.round2.completed_prev_round;
  423. pthread_mutex_unlock(&route_state.round2.mutex);
  424. // What is the next message we expect from this node?
  425. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  426. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  427. uint32_t tot_enc_chunk_size) {
  428. return msgbuffer_get_buf(route_state.round1, commst,
  429. tot_enc_chunk_size);
  430. };
  431. nodest.in_msg_received = round1_received;
  432. }
  433. // Otherwise, it's just the next round 2 message, so don't change
  434. // the handlers.
  435. if (nodes_received == g_teems_config.num_routing_nodes &&
  436. completed_prev_round) {
  437. route_state.step = ROUTE_ROUND_2;
  438. void *cbpointer = route_state.cbpointer;
  439. route_state.cbpointer = NULL;
  440. ocall_routing_round_complete(cbpointer, 2);
  441. }
  442. }
  443. // For a given other node, set the received message handler to the first
  444. // message we would expect from them, given their roles and our roles.
  445. void route_init_msg_handler(nodenum_t node_num)
  446. {
  447. // Our roles and their roles
  448. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  449. uint8_t their_roles = g_teems_config.roles[node_num];
  450. // The node communication state
  451. NodeCommState &nodest = g_commstates[node_num];
  452. // If we are a routing node (possibly among other roles) and they
  453. // are an ingestion node (possibly among other roles), a round 1
  454. // routing message is the first thing we expect from them. We put
  455. // these messages into the round1 buffer for processing.
  456. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  457. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  458. uint32_t tot_enc_chunk_size) {
  459. return msgbuffer_get_buf(route_state.round1, commst,
  460. tot_enc_chunk_size);
  461. };
  462. nodest.in_msg_received = round1_received;
  463. }
  464. // Otherwise, if we are a storage node (possibly among other roles)
  465. // and they are a routing node (possibly among other roles), a round
  466. // 2 routing message is the first thing we expect from them
  467. else if ((our_roles & ROLE_STORAGE) && (their_roles & ROLE_ROUTING)) {
  468. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  469. uint32_t tot_enc_chunk_size) {
  470. return msgbuffer_get_buf(route_state.round2, commst,
  471. tot_enc_chunk_size);
  472. };
  473. nodest.in_msg_received = round2_received;
  474. }
  475. // Otherwise, we don't expect a message from this node. Set the
  476. // unknown message handler.
  477. else {
  478. nodest.in_msg_get_buf = default_in_msg_get_buf;
  479. nodest.in_msg_received = unknown_in_msg_received;
  480. }
  481. }
  482. // Directly ingest a buffer of num_msgs messages into the ingbuf buffer.
  483. // Return true on success, false on failure.
  484. bool ecall_ingest_raw(uint8_t *msgs, uint32_t num_msgs)
  485. {
  486. uint16_t msg_size = g_teems_config.msg_size;
  487. MsgBuffer &ingbuf = route_state.ingbuf;
  488. pthread_mutex_lock(&ingbuf.mutex);
  489. uint32_t start = ingbuf.reserved;
  490. if (start + num_msgs > route_state.tot_msg_per_ing) {
  491. pthread_mutex_unlock(&ingbuf.mutex);
  492. printf("Max %u messages exceeded\n",
  493. route_state.tot_msg_per_ing);
  494. return false;
  495. }
  496. ingbuf.reserved += num_msgs;
  497. pthread_mutex_unlock(&ingbuf.mutex);
  498. memmove(ingbuf.buf + start * msg_size,
  499. msgs, num_msgs * msg_size);
  500. pthread_mutex_lock(&ingbuf.mutex);
  501. ingbuf.inserted += num_msgs;
  502. pthread_mutex_unlock(&ingbuf.mutex);
  503. return true;
  504. }
  505. // Send the round 1 messages. Note that N here is not private.
  506. template<typename T>
  507. static void send_round1_msgs(const uint8_t *msgs, const T *indices,
  508. uint32_t N)
  509. {
  510. uint16_t msg_size = g_teems_config.msg_size;
  511. uint16_t tot_weight;
  512. tot_weight = g_teems_config.tot_weight;
  513. nodenum_t my_node_num = g_teems_config.my_node_num;
  514. uint32_t full_rows;
  515. uint32_t last_row;
  516. full_rows = N / uint32_t(tot_weight);
  517. last_row = N % uint32_t(tot_weight);
  518. for (auto &routing_node: g_teems_config.routing_nodes) {
  519. uint8_t weight = g_teems_config.weights[routing_node].weight;
  520. if (weight == 0) {
  521. // This shouldn't happen, but just in case
  522. continue;
  523. }
  524. uint16_t start_weight =
  525. g_teems_config.weights[routing_node].startweight;
  526. // The number of messages headed for this routing node from the
  527. // full rows
  528. uint32_t num_msgs_full_rows = full_rows * uint32_t(weight);
  529. // The number of messages headed for this routing node from the
  530. // incomplete last row is:
  531. // 0 if last_row < start_weight
  532. // last_row-start_weight if start_weight <= last_row < start_weight + weight
  533. // weight if start_weight + weight <= last_row
  534. uint32_t num_msgs_last_row = 0;
  535. if (start_weight <= last_row && last_row < start_weight + weight) {
  536. num_msgs_last_row = last_row-start_weight;
  537. } else if (start_weight + weight <= last_row) {
  538. num_msgs_last_row = weight;
  539. }
  540. // The total number of messages headed for this routing node
  541. uint32_t num_msgs = num_msgs_full_rows + num_msgs_last_row;
  542. if (routing_node == my_node_num) {
  543. // Special case: we're sending to ourselves; just put the
  544. // messages in our own round1 buffer
  545. MsgBuffer &round1 = route_state.round1;
  546. pthread_mutex_lock(&round1.mutex);
  547. uint32_t start = round1.reserved;
  548. if (start + num_msgs > round1.bufsize) {
  549. pthread_mutex_unlock(&round1.mutex);
  550. printf("Max %u messages exceeded\n", round1.bufsize);
  551. return;
  552. }
  553. round1.reserved += num_msgs;
  554. pthread_mutex_unlock(&round1.mutex);
  555. uint8_t *buf = round1.buf + start * msg_size;
  556. for (uint32_t i=0; i<full_rows; ++i) {
  557. const T *idxp = indices + i*tot_weight + start_weight;
  558. for (uint32_t j=0; j<weight; ++j) {
  559. memmove(buf, msgs + idxp[j].index()*msg_size, msg_size);
  560. buf += msg_size;
  561. }
  562. }
  563. const T *idxp = indices + full_rows*tot_weight + start_weight;
  564. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  565. memmove(buf, msgs + idxp[j].index()*msg_size, msg_size);
  566. buf += msg_size;
  567. }
  568. pthread_mutex_lock(&round1.mutex);
  569. round1.inserted += num_msgs;
  570. round1.nodes_received += 1;
  571. pthread_mutex_unlock(&round1.mutex);
  572. } else {
  573. NodeCommState &nodecom = g_commstates[routing_node];
  574. nodecom.message_start(num_msgs * msg_size);
  575. for (uint32_t i=0; i<full_rows; ++i) {
  576. const T *idxp = indices + i*tot_weight + start_weight;
  577. for (uint32_t j=0; j<weight; ++j) {
  578. nodecom.message_data(msgs + idxp[j].index()*msg_size, msg_size);
  579. }
  580. }
  581. const T *idxp = indices + full_rows*tot_weight + start_weight;
  582. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  583. nodecom.message_data(msgs + idxp[j].index()*msg_size, msg_size);
  584. }
  585. }
  586. }
  587. }
  588. // Send the round 1a messages from the round 1 buffer, which only occurs in public-channel routing.
  589. // msgs points to the message buffer, indices points to the the sorted indices, and N is the number
  590. // of non-padding items.
  591. static void send_round1a_msgs(const uint8_t *msgs, const UidPriorityKey *indices, uint32_t N) {
  592. uint16_t msg_size = g_teems_config.msg_size;
  593. nodenum_t my_node_num = g_teems_config.my_node_num;
  594. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  595. uint32_t min_msgs_per_node = route_state.max_round1a_msgs / num_routing_nodes;
  596. uint32_t extra_msgs = route_state.max_round1a_msgs % num_routing_nodes;
  597. for (auto &routing_node: g_teems_config.routing_nodes) {
  598. // In this unweighted setting, start_weight represents the position among routing nodes
  599. uint16_t prev_nodes = g_teems_config.weights[routing_node].startweight;
  600. uint32_t start_msg, num_msgs;
  601. if (prev_nodes >= extra_msgs) {
  602. start_msg = min_msgs_per_node * prev_nodes + extra_msgs;
  603. num_msgs = min_msgs_per_node;
  604. } else {
  605. start_msg = min_msgs_per_node * prev_nodes + prev_nodes;
  606. num_msgs = min_msgs_per_node + 1;
  607. }
  608. // take number of messages into account
  609. if (start_msg >= N) {
  610. num_msgs = 0;
  611. } else if (start_msg + num_msgs > N) {
  612. num_msgs = N - start_msg;
  613. }
  614. if (routing_node == my_node_num) {
  615. // Special case: we're sending to ourselves; just put the
  616. // messages in our own buffer
  617. MsgBuffer &round1a = route_state.round1a;
  618. pthread_mutex_lock(&round1a.mutex);
  619. uint32_t start = round1a.reserved;
  620. if (start + num_msgs > round1a.bufsize) {
  621. pthread_mutex_unlock(&round1a.mutex);
  622. printf("Max %u messages exceeded in round 1a\n", round1a.bufsize);
  623. return;
  624. }
  625. round1a.reserved += num_msgs;
  626. pthread_mutex_unlock(&round1a.mutex);
  627. uint8_t *buf = round1a.buf + start * msg_size;
  628. for (uint32_t i=0; i<num_msgs; ++i) {
  629. const UidPriorityKey *idxp = indices + start_msg + i;
  630. memmove(buf, msgs + idxp->index()*msg_size, msg_size);
  631. buf += msg_size;
  632. }
  633. pthread_mutex_lock(&round1a.mutex);
  634. round1a.inserted += num_msgs;
  635. round1a.nodes_received += 1;
  636. pthread_mutex_unlock(&round1a.mutex);
  637. } else {
  638. NodeCommState &nodecom = g_commstates[routing_node];
  639. nodecom.message_start(num_msgs * msg_size);
  640. for (uint32_t i=0; i<num_msgs; ++i) {
  641. const UidPriorityKey *idxp = indices + start_msg + i;
  642. nodecom.message_data(msgs + idxp->index()*msg_size, msg_size);
  643. }
  644. }
  645. }
  646. }
  647. // Send the round 1b messages from the round 1a buffer, which only occurs in public-channel routing.
  648. // msgs points to the message buffer, and N is the number of non-padding items.
  649. static void send_round1b_msgs(const uint8_t *msgs, uint32_t N) {
  650. uint16_t msg_size = g_teems_config.msg_size;
  651. nodenum_t my_node_num = g_teems_config.my_node_num;
  652. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  653. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  654. // send to previous node
  655. if (prev_nodes > 0) {
  656. nodenum_t prev_node = g_teems_config.routing_nodes[num_routing_nodes-1];
  657. NodeCommState &nodecom = g_commstates[prev_node];
  658. uint32_t num_msgs = min(N, route_state.max_round1b_msgs_to_adj_rtr);
  659. nodecom.message_start(num_msgs * msg_size);
  660. for (uint32_t i=0; i<num_msgs; ++i) {
  661. nodecom.message_data(msgs + i*msg_size, msg_size);
  662. }
  663. }
  664. // send to next node
  665. if (prev_nodes < num_routing_nodes-1) {
  666. nodenum_t next_node = g_teems_config.routing_nodes[1];
  667. NodeCommState &nodecom = g_commstates[next_node];
  668. if (N <= route_state.max_round1a_msgs - route_state.max_round1b_msgs_to_adj_rtr) {
  669. // No messages to exchange with next node
  670. nodecom.message_start(0);
  671. // No need to call message_data()
  672. } else {
  673. uint32_t start_msg =
  674. route_state.max_round1a_msgs - route_state.max_round1b_msgs_to_adj_rtr;
  675. uint32_t num_msgs = N - start_msg;
  676. nodecom.message_start(num_msgs * msg_size);
  677. for (uint32_t i=0; i<num_msgs; ++i) {
  678. nodecom.message_data(msgs + i*msg_size, msg_size);
  679. }
  680. }
  681. }
  682. }
  683. // Send the round 1c messages. Note that N here is not private.
  684. // FIX: combine with send_round1_msgs(), which is similar
  685. static void send_round1c_msgs(const uint8_t *msgs, const UidPriorityKey *indices,
  686. uint32_t N)
  687. {
  688. uint16_t msg_size = g_teems_config.msg_size;
  689. uint16_t tot_weight;
  690. tot_weight = g_teems_config.tot_weight;
  691. nodenum_t my_node_num = g_teems_config.my_node_num;
  692. uint32_t full_rows;
  693. uint32_t last_row;
  694. full_rows = N / uint32_t(tot_weight);
  695. last_row = N % uint32_t(tot_weight);
  696. for (auto &routing_node: g_teems_config.routing_nodes) {
  697. uint8_t weight = g_teems_config.weights[routing_node].weight;
  698. if (weight == 0) {
  699. // This shouldn't happen, but just in case
  700. continue;
  701. }
  702. uint16_t start_weight =
  703. g_teems_config.weights[routing_node].startweight;
  704. // The number of messages headed for this routing node from the
  705. // full rows
  706. uint32_t num_msgs_full_rows = full_rows * uint32_t(weight);
  707. // The number of messages headed for this routing node from the
  708. // incomplete last row is:
  709. // 0 if last_row < start_weight
  710. // last_row-start_weight if start_weight <= last_row < start_weight + weight
  711. // weight if start_weight + weight <= last_row
  712. uint32_t num_msgs_last_row = 0;
  713. if (start_weight <= last_row && last_row < start_weight + weight) {
  714. num_msgs_last_row = last_row-start_weight;
  715. } else if (start_weight + weight <= last_row) {
  716. num_msgs_last_row = weight;
  717. }
  718. // The total number of messages headed for this routing node
  719. uint32_t num_msgs = num_msgs_full_rows + num_msgs_last_row;
  720. if (routing_node == my_node_num) {
  721. // Special case: we're sending to ourselves; just put the
  722. // messages in our own round1 buffer
  723. MsgBuffer &round1c = route_state.round1c;
  724. pthread_mutex_lock(&round1c.mutex);
  725. uint32_t start = round1c.reserved;
  726. if (start + num_msgs > round1c.bufsize) {
  727. pthread_mutex_unlock(&round1c.mutex);
  728. printf("Max %u messages exceeded\n", round1c.bufsize);
  729. return;
  730. }
  731. round1c.reserved += num_msgs;
  732. pthread_mutex_unlock(&round1c.mutex);
  733. uint8_t *buf = round1c.buf + start * msg_size;
  734. for (uint32_t i=0; i<full_rows; ++i) {
  735. const UidPriorityKey *idxp = indices + i*tot_weight + start_weight;
  736. for (uint32_t j=0; j<weight; ++j) {
  737. memmove(buf, msgs + idxp[j].index()*msg_size, msg_size);
  738. buf += msg_size;
  739. }
  740. }
  741. const UidPriorityKey *idxp = indices + full_rows*tot_weight + start_weight;
  742. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  743. memmove(buf, msgs + idxp[j].index()*msg_size, msg_size);
  744. buf += msg_size;
  745. }
  746. pthread_mutex_lock(&round1c.mutex);
  747. round1c.inserted += num_msgs;
  748. round1c.nodes_received += 1;
  749. pthread_mutex_unlock(&round1c.mutex);
  750. } else {
  751. NodeCommState &nodecom = g_commstates[routing_node];
  752. nodecom.message_start(num_msgs * msg_size);
  753. for (uint32_t i=0; i<full_rows; ++i) {
  754. const UidPriorityKey *idxp = indices + i*tot_weight + start_weight;
  755. for (uint32_t j=0; j<weight; ++j) {
  756. nodecom.message_data(msgs + idxp[j].index()*msg_size, msg_size);
  757. }
  758. }
  759. const UidPriorityKey *idxp = indices + full_rows*tot_weight + start_weight;
  760. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  761. nodecom.message_data(msgs + idxp[j].index()*msg_size, msg_size);
  762. }
  763. }
  764. }
  765. }
  766. // Send the round 2 messages from the previous-round buffer, which are already
  767. // padded and shuffled, so this can be done non-obliviously. tot_msgs
  768. // is the total number of messages in the input buffer, which may
  769. // include padding messages added by the shuffle. Those messages are
  770. // not sent anywhere. There are num_msgs_per_stg messages for each
  771. // storage node labelled for that node.
  772. static void send_round2_msgs(uint32_t tot_msgs, uint32_t num_msgs_per_stg, MsgBuffer &prevround)
  773. {
  774. uint16_t msg_size = g_teems_config.msg_size;
  775. const uint8_t* buf = prevround.buf;
  776. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  777. nodenum_t my_node_num = g_teems_config.my_node_num;
  778. uint8_t *myself_buf = NULL;
  779. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  780. nodenum_t node = g_teems_config.storage_nodes[i];
  781. if (node != my_node_num) {
  782. g_commstates[node].message_start(msg_size * num_msgs_per_stg);
  783. } else {
  784. MsgBuffer &round2 = route_state.round2;
  785. pthread_mutex_lock(&round2.mutex);
  786. uint32_t start = round2.reserved;
  787. if (start + num_msgs_per_stg > round2.bufsize) {
  788. pthread_mutex_unlock(&round2.mutex);
  789. printf("Max %u messages exceeded\n", round2.bufsize);
  790. return;
  791. }
  792. round2.reserved += num_msgs_per_stg;
  793. pthread_mutex_unlock(&round2.mutex);
  794. myself_buf = round2.buf + start * msg_size;
  795. }
  796. }
  797. while (tot_msgs) {
  798. nodenum_t storage_node_id =
  799. nodenum_t((*(const uint32_t *)buf)>>DEST_UID_BITS);
  800. if (storage_node_id < num_storage_nodes) {
  801. nodenum_t node = g_teems_config.storage_map[storage_node_id];
  802. if (node == my_node_num) {
  803. memmove(myself_buf, buf, msg_size);
  804. myself_buf += msg_size;
  805. } else {
  806. g_commstates[node].message_data(buf, msg_size);
  807. }
  808. }
  809. buf += msg_size;
  810. --tot_msgs;
  811. }
  812. if (myself_buf) {
  813. MsgBuffer &round2 = route_state.round2;
  814. pthread_mutex_lock(&round2.mutex);
  815. round2.inserted += num_msgs_per_stg;
  816. round2.nodes_received += 1;
  817. pthread_mutex_unlock(&round2.mutex);
  818. }
  819. }
  820. static void round1a_processing(void *cbpointer) {
  821. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  822. MsgBuffer &round1 = route_state.round1;
  823. if (my_roles & ROLE_ROUTING) {
  824. route_state.cbpointer = cbpointer;
  825. pthread_mutex_lock(&round1.mutex);
  826. // Ensure there are no pending messages currently being inserted
  827. // into the buffer
  828. while (round1.reserved != round1.inserted) {
  829. pthread_mutex_unlock(&round1.mutex);
  830. pthread_mutex_lock(&round1.mutex);
  831. }
  832. #ifdef PROFILE_ROUTING
  833. uint32_t inserted = round1.inserted;
  834. unsigned long start_round1a = printf_with_rtclock("begin round1a processing (%u)\n", inserted);
  835. // Sort the messages we've received
  836. unsigned long start_sort = printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted, route_state.max_round1_msgs);
  837. #endif
  838. // Sort received messages by increasing user ID and
  839. // priority. Smaller priority number indicates higher priority.
  840. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, round1.buf,
  841. g_teems_config.msg_size, round1.inserted, route_state.max_round1_msgs,
  842. send_round1a_msgs);
  843. #ifdef PROFILE_ROUTING
  844. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n", inserted, route_state.max_round1_msgs);
  845. printf_with_rtclock_diff(start_round1a, "end round1a processing (%u)\n", inserted);
  846. #endif
  847. round1.reset();
  848. pthread_mutex_unlock(&round1.mutex);
  849. MsgBuffer &round1a = route_state.round1a;
  850. pthread_mutex_lock(&round1a.mutex);
  851. round1a.completed_prev_round = true;
  852. nodenum_t nodes_received = round1a.nodes_received;
  853. pthread_mutex_unlock(&round1a.mutex);
  854. if (nodes_received == g_teems_config.num_routing_nodes) {
  855. route_state.step = ROUTE_ROUND_1A;
  856. route_state.cbpointer = NULL;
  857. ocall_routing_round_complete(cbpointer, ROUND_1A);
  858. }
  859. } else {
  860. route_state.step = ROUTE_ROUND_1A;
  861. route_state.round1a.completed_prev_round = true;
  862. ocall_routing_round_complete(cbpointer, ROUND_1A);
  863. }
  864. }
  865. static void round1b_processing(void *cbpointer) {
  866. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  867. nodenum_t my_node_num = g_teems_config.my_node_num;
  868. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  869. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  870. MsgBuffer &round1a = route_state.round1a;
  871. MsgBuffer &round1a_sorted = route_state.round1a_sorted;
  872. if (my_roles & ROLE_ROUTING) {
  873. route_state.cbpointer = cbpointer;
  874. pthread_mutex_lock(&round1a.mutex);
  875. // Ensure there are no pending messages currently being inserted
  876. // into the buffer
  877. while (round1a.reserved != round1a.inserted) {
  878. pthread_mutex_unlock(&round1a.mutex);
  879. pthread_mutex_lock(&round1a.mutex);
  880. }
  881. pthread_mutex_lock(&round1a_sorted.mutex);
  882. #ifdef PROFILE_ROUTING
  883. uint32_t inserted = round1a.inserted;
  884. unsigned long start_round1b = printf_with_rtclock("begin round1b processing (%u)\n", inserted);
  885. // Sort the messages we've received
  886. unsigned long start_sort = printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted, route_state.max_round1a_msgs);
  887. #endif
  888. // Sort received messages by increasing user ID and
  889. // priority. Smaller priority number indicates higher priority.
  890. if (inserted > 0) {
  891. // copy items in sorted order into round1a_sorted
  892. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, round1a.buf,
  893. g_teems_config.msg_size, round1a.inserted, route_state.max_round1a_msgs,
  894. route_state.round1a_sorted.buf);
  895. send_round1b_msgs(round1a.buf, round1a.inserted);
  896. } else {
  897. send_round1b_msgs(NULL, 0);
  898. }
  899. #ifdef PROFILE_ROUTING
  900. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n", inserted, route_state.max_round1a_msgs);
  901. printf_with_rtclock_diff(start_round1b, "end round1b processing (%u)\n", inserted);
  902. #endif
  903. pthread_mutex_unlock(&round1a_sorted.mutex);
  904. pthread_mutex_unlock(&round1a.mutex);
  905. MsgBuffer &round1b = route_state.round1b;
  906. pthread_mutex_lock(&round1b.mutex);
  907. round1b.completed_prev_round = true;
  908. nodenum_t nodes_received = round1b.nodes_received;
  909. nodenum_t adjacent_nodes =
  910. (((prev_nodes == 0) || (prev_nodes == num_routing_nodes-1)) ? 1 : 2);
  911. pthread_mutex_unlock(&round1b.mutex);
  912. if (nodes_received == adjacent_nodes) {
  913. route_state.step = ROUTE_ROUND_1B;
  914. route_state.cbpointer = NULL;
  915. ocall_routing_round_complete(cbpointer, ROUND_1B);
  916. }
  917. } else {
  918. route_state.step = ROUTE_ROUND_1B;
  919. route_state.round1b.completed_prev_round = true;
  920. ocall_routing_round_complete(cbpointer, ROUND_1B);
  921. }
  922. }
  923. //FIX: adjust the message totals based on the sorts
  924. static void round1c_processing(void *cbpointer) {
  925. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  926. nodenum_t my_node_num = g_teems_config.my_node_num;
  927. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  928. MsgBuffer &round1a = route_state.round1a;
  929. MsgBuffer &round1a_sorted = route_state.round1a_sorted;
  930. MsgBuffer &round1b = route_state.round1b;
  931. if (my_roles & ROLE_ROUTING) {
  932. route_state.cbpointer = cbpointer;
  933. pthread_mutex_lock(&round1b.mutex);
  934. // Ensure there are no pending messages currently being inserted
  935. // into the buffer
  936. while (round1b.reserved != round1b.inserted) {
  937. pthread_mutex_unlock(&round1b.mutex);
  938. pthread_mutex_lock(&round1b.mutex);
  939. }
  940. pthread_mutex_lock(&round1a.mutex);
  941. pthread_mutex_lock(&round1a_sorted.mutex);
  942. #ifdef PROFILE_ROUTING
  943. uint32_t inserted = round1a.inserted;
  944. unsigned long start_round1c = printf_with_rtclock("begin round1c processing (%u)\n", inserted);
  945. // Sort the messages we've received
  946. unsigned long start_sort = printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted, route_state.max_round1a_msgs);
  947. #endif
  948. // Sort received messages by increasing user ID and
  949. // priority. Smaller priority number indicates higher priority.
  950. if (inserted > 0) {
  951. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, round1a_sorted.buf,
  952. g_teems_config.msg_size, round1a.inserted, route_state.max_round1a_msgs,
  953. send_round1c_msgs);
  954. } else {
  955. send_round1c_msgs(NULL, NULL, 0);
  956. }
  957. #ifdef PROFILE_ROUTING
  958. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n", inserted, route_state.max_round1a_msgs);
  959. printf_with_rtclock_diff(start_round1c, "end round1c processing (%u)\n", inserted);
  960. #endif
  961. round1a.reset();
  962. round1a_sorted.reset();
  963. round1b.reset();
  964. pthread_mutex_unlock(&round1a_sorted.mutex);
  965. pthread_mutex_unlock(&round1a.mutex);
  966. pthread_mutex_unlock(&round1b.mutex);
  967. MsgBuffer &round1c = route_state.round1c;
  968. pthread_mutex_lock(&round1c.mutex);
  969. round1c.completed_prev_round = true;
  970. nodenum_t nodes_received = round1c.nodes_received;
  971. pthread_mutex_unlock(&round1c.mutex);
  972. if (nodes_received == num_routing_nodes) {
  973. route_state.step = ROUTE_ROUND_1C;
  974. route_state.cbpointer = NULL;
  975. ocall_routing_round_complete(cbpointer, ROUND_1C);
  976. }
  977. } else {
  978. route_state.step = ROUTE_ROUND_1C;
  979. route_state.round1b.completed_prev_round = true;
  980. ocall_routing_round_complete(cbpointer, ROUND_1C);
  981. }
  982. }
  983. // Process messages in round 2
  984. static void round2_processing(uint8_t my_roles, void *cbpointer, MsgBuffer &prevround) {
  985. if (my_roles & ROLE_ROUTING) {
  986. route_state.cbpointer = cbpointer;
  987. pthread_mutex_lock(&prevround.mutex);
  988. // Ensure there are no pending messages currently being inserted
  989. // into the buffer
  990. while (prevround.reserved != prevround.inserted) {
  991. pthread_mutex_unlock(&prevround.mutex);
  992. pthread_mutex_lock(&prevround.mutex);
  993. }
  994. // If the _total_ number of messages we received in round 1
  995. // is less than the max number of messages we could send to
  996. // _each_ storage node, then cap the number of messages we
  997. // will send to each storage node to that number.
  998. uint32_t msgs_per_stg = route_state.max_msg_to_each_stg;
  999. if (prevround.inserted < msgs_per_stg) {
  1000. msgs_per_stg = prevround.inserted;
  1001. }
  1002. // Note: at this point, it is required that each message in
  1003. // the prevround buffer have a _valid_ storage node id field.
  1004. // Obliviously tally the number of messages we received in
  1005. // the previous round destined for each storage node
  1006. #ifdef PROFILE_ROUTING
  1007. unsigned long start_round2 = printf_with_rtclock("begin round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1008. unsigned long start_tally = printf_with_rtclock("begin tally (%u)\n", prevround.inserted);
  1009. #endif
  1010. uint16_t msg_size = g_teems_config.msg_size;
  1011. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  1012. std::vector<uint32_t> tally = obliv_tally_stg(
  1013. prevround.buf, msg_size, prevround.inserted, num_storage_nodes);
  1014. #ifdef PROFILE_ROUTING
  1015. printf_with_rtclock_diff(start_tally, "end tally (%u)\n", prevround.inserted);
  1016. #endif
  1017. // Note: tally contains private values! It's OK to
  1018. // non-obliviously check for an error condition, though.
  1019. // While we're at it, obliviously change the tally of
  1020. // messages received to a tally of padding messages
  1021. // required.
  1022. uint32_t tot_padding = 0;
  1023. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  1024. if (tally[i] > msgs_per_stg) {
  1025. printf("Received too many messages for storage node %u\n", i);
  1026. assert(tally[i] <= msgs_per_stg);
  1027. }
  1028. tally[i] = msgs_per_stg - tally[i];
  1029. tot_padding += tally[i];
  1030. }
  1031. prevround.reserved += tot_padding;
  1032. assert(prevround.reserved <= prevround.bufsize);
  1033. // Obliviously add padding for each storage node according
  1034. // to the (private) padding tally.
  1035. #ifdef PROFILE_ROUTING
  1036. unsigned long start_pad = printf_with_rtclock("begin pad (%u)\n", tot_padding);
  1037. #endif
  1038. obliv_pad_stg(prevround.buf + prevround.inserted * msg_size,
  1039. msg_size, tally, tot_padding);
  1040. #ifdef PROFILE_ROUTING
  1041. printf_with_rtclock_diff(start_pad, "end pad (%u)\n", tot_padding);
  1042. #endif
  1043. prevround.inserted += tot_padding;
  1044. // Obliviously shuffle the messages
  1045. #ifdef PROFILE_ROUTING
  1046. unsigned long start_shuffle = printf_with_rtclock("begin shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1047. #endif
  1048. uint32_t num_shuffled = shuffle_mtobliv(g_teems_config.nthreads,
  1049. prevround.buf, msg_size, prevround.inserted, prevround.bufsize);
  1050. #ifdef PROFILE_ROUTING
  1051. printf_with_rtclock_diff(start_shuffle, "end shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1052. printf_with_rtclock_diff(start_round2, "end round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1053. #endif
  1054. // Now we can handle the messages non-obliviously, since we
  1055. // know there will be exactly msgs_per_stg messages to each
  1056. // storage node, and the oblivious shuffle broke the
  1057. // connection between where each message came from and where
  1058. // it's going.
  1059. send_round2_msgs(num_shuffled, msgs_per_stg, prevround);
  1060. prevround.reset();
  1061. pthread_mutex_unlock(&prevround.mutex);
  1062. }
  1063. if (my_roles & ROLE_STORAGE) {
  1064. route_state.cbpointer = cbpointer;
  1065. MsgBuffer &round2 = route_state.round2;
  1066. pthread_mutex_lock(&round2.mutex);
  1067. round2.completed_prev_round = true;
  1068. nodenum_t nodes_received = round2.nodes_received;
  1069. pthread_mutex_unlock(&round2.mutex);
  1070. if (nodes_received == g_teems_config.num_routing_nodes) {
  1071. route_state.step = ROUTE_ROUND_2;
  1072. route_state.cbpointer = NULL;
  1073. ocall_routing_round_complete(cbpointer, 2);
  1074. }
  1075. } else {
  1076. route_state.step = ROUTE_ROUND_2;
  1077. route_state.round2.completed_prev_round = true;
  1078. ocall_routing_round_complete(cbpointer, 2);
  1079. }
  1080. }
  1081. // Perform the next round of routing. The callback pointer will be
  1082. // passed to ocall_routing_round_complete when the round is complete.
  1083. void ecall_routing_proceed(void *cbpointer)
  1084. {
  1085. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  1086. if (route_state.step == ROUTE_NOT_STARTED) {
  1087. if (my_roles & ROLE_INGESTION) {
  1088. ingestion_epoch++;
  1089. route_state.cbpointer = cbpointer;
  1090. MsgBuffer &ingbuf = route_state.ingbuf;
  1091. pthread_mutex_lock(&ingbuf.mutex);
  1092. // Ensure there are no pending messages currently being inserted
  1093. // into the buffer
  1094. while (ingbuf.reserved != ingbuf.inserted) {
  1095. pthread_mutex_unlock(&ingbuf.mutex);
  1096. pthread_mutex_lock(&ingbuf.mutex);
  1097. }
  1098. // Sort the messages we've received
  1099. #ifdef PROFILE_ROUTING
  1100. uint32_t inserted = ingbuf.inserted;
  1101. unsigned long start_round1 = printf_with_rtclock("begin round1 processing (%u)\n", inserted);
  1102. unsigned long start_sort = printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  1103. #endif
  1104. if (g_teems_config.private_routing) {
  1105. sort_mtobliv<UidKey>(g_teems_config.nthreads, ingbuf.buf,
  1106. g_teems_config.msg_size, ingbuf.inserted,
  1107. route_state.tot_msg_per_ing, send_round1_msgs<UidKey>);
  1108. } else {
  1109. // Sort received messages by increasing user ID and
  1110. // priority. Smaller priority number indicates higher priority.
  1111. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, ingbuf.buf,
  1112. g_teems_config.msg_size, ingbuf.inserted, route_state.tot_msg_per_ing,
  1113. send_round1_msgs<UidPriorityKey>);
  1114. }
  1115. #ifdef PROFILE_ROUTING
  1116. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  1117. printf_with_rtclock_diff(start_round1, "end round1 processing (%u)\n", inserted);
  1118. #endif
  1119. ingbuf.reset();
  1120. pthread_mutex_unlock(&ingbuf.mutex);
  1121. }
  1122. if (my_roles & ROLE_ROUTING) {
  1123. MsgBuffer &round1 = route_state.round1;
  1124. pthread_mutex_lock(&round1.mutex);
  1125. round1.completed_prev_round = true;
  1126. nodenum_t nodes_received = round1.nodes_received;
  1127. pthread_mutex_unlock(&round1.mutex);
  1128. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  1129. route_state.step = ROUTE_ROUND_1;
  1130. route_state.cbpointer = NULL;
  1131. ocall_routing_round_complete(cbpointer, 1);
  1132. }
  1133. } else {
  1134. route_state.step = ROUTE_ROUND_1;
  1135. route_state.round1.completed_prev_round = true;
  1136. ocall_routing_round_complete(cbpointer, 1);
  1137. }
  1138. } else if (route_state.step == ROUTE_ROUND_1) {
  1139. if (g_teems_config.private_routing) { // private routing next round
  1140. round2_processing(my_roles, cbpointer, route_state.round1);
  1141. } else { // public routing next round
  1142. round1a_processing(cbpointer);
  1143. }
  1144. } else if (route_state.step == ROUTE_ROUND_1A) {
  1145. round1b_processing(cbpointer);
  1146. } else if (route_state.step == ROUTE_ROUND_1B) {
  1147. round1c_processing(cbpointer);
  1148. } else if (route_state.step == ROUTE_ROUND_1C) {
  1149. round2_processing(my_roles, cbpointer, route_state.round1c);
  1150. } else if (route_state.step == ROUTE_ROUND_2) {
  1151. if (my_roles & ROLE_STORAGE) {
  1152. MsgBuffer &round2 = route_state.round2;
  1153. pthread_mutex_lock(&round2.mutex);
  1154. // Ensure there are no pending messages currently being inserted
  1155. // into the buffer
  1156. while (round2.reserved != round2.inserted) {
  1157. pthread_mutex_unlock(&round2.mutex);
  1158. pthread_mutex_lock(&round2.mutex);
  1159. }
  1160. unsigned long start = printf_with_rtclock("begin storage processing (%u)\n", round2.inserted);
  1161. storage_received(round2);
  1162. printf_with_rtclock_diff(start, "end storage processing (%u)\n", round2.inserted);
  1163. // We're done
  1164. route_state.step = ROUTE_NOT_STARTED;
  1165. ocall_routing_round_complete(cbpointer, 0);
  1166. } else {
  1167. // We're done
  1168. route_state.step = ROUTE_NOT_STARTED;
  1169. ocall_routing_round_complete(cbpointer, 0);
  1170. }
  1171. }
  1172. }