route.cpp 57 KB

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