route.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. uint32_t num_msgs = CEILDIV(plaintext_bytes, uint32_t(msg_size));
  264. pthread_mutex_lock(&msgbuf.mutex);
  265. uint32_t start = msgbuf.reserved;
  266. if (start + num_msgs > msgbuf.bufsize) {
  267. pthread_mutex_unlock(&msgbuf.mutex);
  268. printf("Max %u messages exceeded (msgbuffer_get_buf)\n",
  269. msgbuf.bufsize);
  270. return NULL;
  271. }
  272. msgbuf.reserved += num_msgs;
  273. pthread_mutex_unlock(&msgbuf.mutex);
  274. return msgbuf.buf + start * msg_size;
  275. }
  276. static void round1a_received(NodeCommState &nodest,
  277. uint8_t *data, uint32_t plaintext_len, uint32_t);
  278. static void round1b_next_received(NodeCommState &nodest,
  279. uint8_t *data, uint32_t plaintext_len, uint32_t);
  280. static void round1c_received(NodeCommState &nodest, uint8_t *data,
  281. uint32_t plaintext_len, uint32_t);
  282. static void round2_received(NodeCommState &nodest,
  283. uint8_t *data, uint32_t plaintext_len, uint32_t);
  284. // A round 1 message was received by a routing node from an ingestion
  285. // node; we put it into the round 2 buffer for processing in round 2
  286. static void round1_received(NodeCommState &nodest,
  287. uint8_t *data, uint32_t plaintext_len, uint32_t)
  288. {
  289. uint16_t msg_size = g_teems_config.msg_size;
  290. assert((plaintext_len % uint32_t(msg_size)) == 0);
  291. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  292. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  293. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  294. pthread_mutex_lock(&route_state.round1.mutex);
  295. route_state.round1.inserted += num_msgs;
  296. route_state.round1.nodes_received += 1;
  297. nodenum_t nodes_received = route_state.round1.nodes_received;
  298. bool completed_prev_round = route_state.round1.completed_prev_round;
  299. pthread_mutex_unlock(&route_state.round1.mutex);
  300. // What is the next message we expect from this node?
  301. if (g_teems_config.private_routing) {
  302. if ((our_roles & ROLE_STORAGE) && (their_roles & ROLE_ROUTING)) {
  303. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  304. uint32_t tot_enc_chunk_size) {
  305. return msgbuffer_get_buf(route_state.round2, commst,
  306. tot_enc_chunk_size);
  307. };
  308. nodest.in_msg_received = round2_received;
  309. }
  310. // Otherwise, it's just the next round 1 message, so don't change
  311. // the handlers.
  312. } else {
  313. if (their_roles & ROLE_ROUTING) {
  314. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  315. uint32_t tot_enc_chunk_size) {
  316. return msgbuffer_get_buf(route_state.round1a, commst,
  317. tot_enc_chunk_size);
  318. };
  319. nodest.in_msg_received = round1a_received;
  320. }
  321. // Otherwise, it's just the next round 1 message, so don't change
  322. // the handlers.
  323. }
  324. if (nodes_received == g_teems_config.num_ingestion_nodes &&
  325. completed_prev_round) {
  326. route_state.step = ROUTE_ROUND_1;
  327. void *cbpointer = route_state.cbpointer;
  328. route_state.cbpointer = NULL;
  329. ocall_routing_round_complete(cbpointer, 1);
  330. }
  331. }
  332. // A round 1a message was received by a routing node
  333. static void round1a_received(NodeCommState &nodest,
  334. uint8_t *data, uint32_t plaintext_len, uint32_t)
  335. {
  336. uint16_t msg_size = g_teems_config.msg_size;
  337. assert((plaintext_len % uint32_t(msg_size)) == 0);
  338. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  339. pthread_mutex_lock(&route_state.round1a.mutex);
  340. route_state.round1a.inserted += num_msgs;
  341. route_state.round1a.nodes_received += 1;
  342. nodenum_t nodes_received = route_state.round1a.nodes_received;
  343. bool completed_prev_round = route_state.round1a.completed_prev_round;
  344. pthread_mutex_unlock(&route_state.round1a.mutex);
  345. // Both are routing nodes
  346. // We only expect a message from the next node (if it exists)
  347. nodenum_t my_node_num = g_teems_config.my_node_num;
  348. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  349. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  350. if ((prev_nodes < num_routing_nodes-1) &&
  351. (nodest.node_num == g_teems_config.routing_nodes[1])) {
  352. // Node is next routing node
  353. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  354. uint32_t tot_enc_chunk_size) {
  355. return msgbuffer_get_buf(route_state.round1b_next, commst,
  356. tot_enc_chunk_size);
  357. };
  358. nodest.in_msg_received = round1b_next_received;
  359. } else {
  360. // other routing nodes will not send to this node until round 1c
  361. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  362. uint32_t tot_enc_chunk_size) {
  363. return msgbuffer_get_buf(route_state.round1c, commst,
  364. tot_enc_chunk_size);
  365. };
  366. nodest.in_msg_received = round1c_received;
  367. }
  368. if (nodes_received == g_teems_config.num_routing_nodes &&
  369. completed_prev_round) {
  370. route_state.step = ROUTE_ROUND_1A;
  371. void *cbpointer = route_state.cbpointer;
  372. route_state.cbpointer = NULL;
  373. ocall_routing_round_complete(cbpointer, ROUND_1A);
  374. }
  375. }
  376. // A round 1b message was received from the next routing node
  377. static void round1b_next_received(NodeCommState &nodest,
  378. uint8_t *data, uint32_t plaintext_len, uint32_t)
  379. {
  380. uint16_t msg_size = g_teems_config.msg_size;
  381. // There are an extra 5 bytes at the end of this message, containing
  382. // the next receiver id (4 bytes) and the count of messages with
  383. // that receiver id (1 byte)
  384. assert((plaintext_len % uint32_t(msg_size)) == 5);
  385. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  386. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  387. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  388. pthread_mutex_lock(&route_state.round1b_next.mutex);
  389. // Add an extra 1 for the message space taken up by the above 5
  390. // bytes
  391. route_state.round1b_next.inserted += num_msgs + 1;
  392. route_state.round1b_next.nodes_received += 1;
  393. nodenum_t nodes_received = route_state.round1b_next.nodes_received;
  394. bool completed_prev_round = route_state.round1b_next.completed_prev_round;
  395. pthread_mutex_unlock(&route_state.round1b_next.mutex);
  396. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  397. uint32_t tot_enc_chunk_size) {
  398. return msgbuffer_get_buf(route_state.round1c, commst,
  399. tot_enc_chunk_size);
  400. };
  401. nodest.in_msg_received = round1c_received;
  402. nodenum_t my_node_num = g_teems_config.my_node_num;
  403. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  404. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  405. nodenum_t adjacent_nodes;
  406. if (num_routing_nodes == 1) {
  407. adjacent_nodes = 0;
  408. } else if (prev_nodes == num_routing_nodes-1) {
  409. adjacent_nodes = 0;
  410. } else {
  411. adjacent_nodes = 1;
  412. }
  413. if (nodes_received == adjacent_nodes && completed_prev_round) {
  414. route_state.step = ROUTE_ROUND_1B;
  415. void *cbpointer = route_state.cbpointer;
  416. route_state.cbpointer = NULL;
  417. ocall_routing_round_complete(cbpointer, ROUND_1B);
  418. }
  419. }
  420. // Message received in round 1c
  421. static void round1c_received(NodeCommState &nodest, uint8_t *data,
  422. uint32_t plaintext_len, uint32_t)
  423. {
  424. uint16_t msg_size = g_teems_config.msg_size;
  425. assert((plaintext_len % uint32_t(msg_size)) == 0);
  426. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  427. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  428. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  429. pthread_mutex_lock(&route_state.round1c.mutex);
  430. route_state.round1c.inserted += num_msgs;
  431. route_state.round1c.nodes_received += 1;
  432. nodenum_t nodes_received = route_state.round1c.nodes_received;
  433. bool completed_prev_round = route_state.round1c.completed_prev_round;
  434. pthread_mutex_unlock(&route_state.round1c.mutex);
  435. // What is the next message we expect from this node?
  436. if (our_roles & ROLE_STORAGE) {
  437. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  438. uint32_t tot_enc_chunk_size) {
  439. return msgbuffer_get_buf(route_state.round2, commst,
  440. tot_enc_chunk_size);
  441. };
  442. nodest.in_msg_received = round2_received;
  443. } else if (their_roles & ROLE_INGESTION) {
  444. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  445. uint32_t tot_enc_chunk_size) {
  446. return msgbuffer_get_buf(route_state.round1, commst,
  447. tot_enc_chunk_size);
  448. };
  449. nodest.in_msg_received = round1_received;
  450. } else {
  451. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  452. uint32_t tot_enc_chunk_size) {
  453. return msgbuffer_get_buf(route_state.round1a, commst,
  454. tot_enc_chunk_size);
  455. };
  456. nodest.in_msg_received = round1a_received;
  457. }
  458. if (nodes_received == g_teems_config.num_routing_nodes &&
  459. completed_prev_round) {
  460. route_state.step = ROUTE_ROUND_1C;
  461. void *cbpointer = route_state.cbpointer;
  462. route_state.cbpointer = NULL;
  463. ocall_routing_round_complete(cbpointer, ROUND_1C);
  464. }
  465. }
  466. // A round 2 message was received by a storage node from a routing node
  467. static void round2_received(NodeCommState &nodest,
  468. uint8_t *data, uint32_t plaintext_len, uint32_t)
  469. {
  470. uint16_t msg_size = g_teems_config.msg_size;
  471. assert((plaintext_len % uint32_t(msg_size)) == 0);
  472. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  473. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  474. uint8_t their_roles = g_teems_config.roles[nodest.node_num];
  475. pthread_mutex_lock(&route_state.round2.mutex);
  476. route_state.round2.inserted += num_msgs;
  477. route_state.round2.nodes_received += 1;
  478. nodenum_t nodes_received = route_state.round2.nodes_received;
  479. bool completed_prev_round = route_state.round2.completed_prev_round;
  480. pthread_mutex_unlock(&route_state.round2.mutex);
  481. // What is the next message we expect from this node?
  482. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  483. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  484. uint32_t tot_enc_chunk_size) {
  485. return msgbuffer_get_buf(route_state.round1, commst,
  486. tot_enc_chunk_size);
  487. };
  488. nodest.in_msg_received = round1_received;
  489. }
  490. // Otherwise, it's just the next round 2 message, so don't change
  491. // the handlers.
  492. if (nodes_received == g_teems_config.num_routing_nodes &&
  493. completed_prev_round) {
  494. route_state.step = ROUTE_ROUND_2;
  495. void *cbpointer = route_state.cbpointer;
  496. route_state.cbpointer = NULL;
  497. ocall_routing_round_complete(cbpointer, 2);
  498. }
  499. }
  500. // For a given other node, set the received message handler to the first
  501. // message we would expect from them, given their roles and our roles.
  502. void route_init_msg_handler(nodenum_t node_num)
  503. {
  504. // Our roles and their roles
  505. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  506. uint8_t their_roles = g_teems_config.roles[node_num];
  507. // The node communication state
  508. NodeCommState &nodest = g_commstates[node_num];
  509. // If we are a routing node (possibly among other roles) and they
  510. // are an ingestion node (possibly among other roles), a round 1
  511. // routing message is the first thing we expect from them. We put
  512. // these messages into the round1 buffer for processing.
  513. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  514. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  515. uint32_t tot_enc_chunk_size) {
  516. return msgbuffer_get_buf(route_state.round1, commst,
  517. tot_enc_chunk_size);
  518. };
  519. nodest.in_msg_received = round1_received;
  520. }
  521. // Otherwise, if we are a storage node (possibly among other roles)
  522. // and they are a routing node (possibly among other roles), a round
  523. // 2 routing message is the first thing we expect from them
  524. else if ((our_roles & ROLE_STORAGE) && (their_roles & ROLE_ROUTING)) {
  525. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  526. uint32_t tot_enc_chunk_size) {
  527. return msgbuffer_get_buf(route_state.round2, commst,
  528. tot_enc_chunk_size);
  529. };
  530. nodest.in_msg_received = round2_received;
  531. }
  532. // Otherwise, we don't expect a message from this node. Set the
  533. // unknown message handler.
  534. else {
  535. nodest.in_msg_get_buf = default_in_msg_get_buf;
  536. nodest.in_msg_received = unknown_in_msg_received;
  537. }
  538. }
  539. // Directly ingest a buffer of num_msgs messages into the ingbuf buffer.
  540. // Return true on success, false on failure.
  541. bool ecall_ingest_raw(uint8_t *msgs, uint32_t num_msgs)
  542. {
  543. uint16_t msg_size = g_teems_config.msg_size;
  544. MsgBuffer &ingbuf = route_state.ingbuf;
  545. pthread_mutex_lock(&ingbuf.mutex);
  546. uint32_t start = ingbuf.reserved;
  547. if (start + num_msgs > route_state.tot_msg_per_ing) {
  548. pthread_mutex_unlock(&ingbuf.mutex);
  549. printf("Max %u messages exceeded (ecall_ingest_raw)\n",
  550. route_state.tot_msg_per_ing);
  551. return false;
  552. }
  553. ingbuf.reserved += num_msgs;
  554. pthread_mutex_unlock(&ingbuf.mutex);
  555. memmove(ingbuf.buf + start * msg_size,
  556. msgs, num_msgs * msg_size);
  557. pthread_mutex_lock(&ingbuf.mutex);
  558. ingbuf.inserted += num_msgs;
  559. pthread_mutex_unlock(&ingbuf.mutex);
  560. return true;
  561. }
  562. // Send messages round-robin, used in rounds 1 and 1c. Note that N here
  563. // is not private. Pass indices = nullptr to just send the messages in
  564. // the order they appear in the msgs buffer.
  565. template<typename T>
  566. static void send_round_robin_msgs(MsgBuffer &round, const uint8_t *msgs,
  567. const T *indices, uint32_t N)
  568. {
  569. uint16_t msg_size = g_teems_config.msg_size;
  570. uint16_t tot_weight;
  571. tot_weight = g_teems_config.tot_weight;
  572. nodenum_t my_node_num = g_teems_config.my_node_num;
  573. uint32_t full_rows;
  574. uint32_t last_row;
  575. full_rows = N / uint32_t(tot_weight);
  576. last_row = N % uint32_t(tot_weight);
  577. for (auto &routing_node: g_teems_config.routing_nodes) {
  578. uint8_t weight = g_teems_config.weights[routing_node].weight;
  579. if (weight == 0) {
  580. // This shouldn't happen, but just in case
  581. continue;
  582. }
  583. uint16_t start_weight =
  584. g_teems_config.weights[routing_node].startweight;
  585. // The number of messages headed for this routing node from the
  586. // full rows
  587. uint32_t num_msgs_full_rows = full_rows * uint32_t(weight);
  588. // The number of messages headed for this routing node from the
  589. // incomplete last row is:
  590. // 0 if last_row < start_weight
  591. // last_row-start_weight if start_weight <= last_row < start_weight + weight
  592. // weight if start_weight + weight <= last_row
  593. uint32_t num_msgs_last_row = 0;
  594. if (start_weight <= last_row && last_row < start_weight + weight) {
  595. num_msgs_last_row = last_row-start_weight;
  596. } else if (start_weight + weight <= last_row) {
  597. num_msgs_last_row = weight;
  598. }
  599. // The total number of messages headed for this routing node
  600. uint32_t num_msgs = num_msgs_full_rows + num_msgs_last_row;
  601. if (routing_node == my_node_num) {
  602. // Special case: we're sending to ourselves; just put the
  603. // messages in our own buffer
  604. pthread_mutex_lock(&round.mutex);
  605. uint32_t start = round.reserved;
  606. if (start + num_msgs > round.bufsize) {
  607. pthread_mutex_unlock(&round.mutex);
  608. printf("Max %u messages exceeded (send_round_robin_msgs)\n",
  609. round.bufsize);
  610. return;
  611. }
  612. round.reserved += num_msgs;
  613. pthread_mutex_unlock(&round.mutex);
  614. uint8_t *buf = round.buf + start * msg_size;
  615. for (uint32_t i=0; i<full_rows; ++i) {
  616. if (indices) {
  617. const T *idxp = indices + i*tot_weight + start_weight;
  618. for (uint32_t j=0; j<weight; ++j) {
  619. memmove(buf, msgs + idxp[j].index()*msg_size, msg_size);
  620. buf += msg_size;
  621. }
  622. } else {
  623. size_t idx = i*tot_weight + start_weight;
  624. memmove(buf, msgs + idx*msg_size, weight*msg_size);
  625. buf += weight*msg_size;
  626. }
  627. }
  628. if (indices) {
  629. const T *idxp = indices + full_rows*tot_weight + start_weight;
  630. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  631. memmove(buf, msgs + idxp[j].index()*msg_size, msg_size);
  632. buf += msg_size;
  633. }
  634. } else {
  635. size_t idx = full_rows*tot_weight + start_weight;
  636. memmove(buf, msgs + idx*msg_size, num_msgs_last_row*msg_size);
  637. buf += num_msgs_last_row*msg_size;
  638. }
  639. pthread_mutex_lock(&round.mutex);
  640. round.inserted += num_msgs;
  641. round.nodes_received += 1;
  642. pthread_mutex_unlock(&round.mutex);
  643. } else {
  644. NodeCommState &nodecom = g_commstates[routing_node];
  645. nodecom.message_start(num_msgs * msg_size);
  646. for (uint32_t i=0; i<full_rows; ++i) {
  647. if (indices) {
  648. const T *idxp = indices + i*tot_weight + start_weight;
  649. for (uint32_t j=0; j<weight; ++j) {
  650. nodecom.message_data(msgs + idxp[j].index()*msg_size,
  651. msg_size);
  652. }
  653. } else {
  654. size_t idx = i*tot_weight + start_weight;
  655. nodecom.message_data(msgs + idx*msg_size, weight*msg_size);
  656. }
  657. }
  658. if (indices) {
  659. const T *idxp = indices + full_rows*tot_weight + start_weight;
  660. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  661. nodecom.message_data(msgs + idxp[j].index()*msg_size,
  662. msg_size);
  663. }
  664. } else {
  665. size_t idx = full_rows*tot_weight + start_weight;
  666. nodecom.message_data(msgs + idx*msg_size,
  667. num_msgs_last_row*msg_size);
  668. }
  669. }
  670. }
  671. }
  672. // Send the round 1a messages from the round 1 buffer, which only occurs in public-channel routing.
  673. // msgs points to the message buffer, indices points to the the sorted indices, and N is the number
  674. // of non-padding items.
  675. static void send_round1a_msgs(const uint8_t *msgs, const UidPriorityKey *indices, uint32_t N) {
  676. uint16_t msg_size = g_teems_config.msg_size;
  677. nodenum_t my_node_num = g_teems_config.my_node_num;
  678. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  679. uint32_t min_msgs_per_node = route_state.max_round1a_msgs / num_routing_nodes;
  680. uint32_t extra_msgs = route_state.max_round1a_msgs % num_routing_nodes;
  681. for (auto &routing_node: g_teems_config.routing_nodes) {
  682. // In this unweighted setting, start_weight represents the position among routing nodes
  683. uint16_t prev_nodes = g_teems_config.weights[routing_node].startweight;
  684. uint32_t start_msg, num_msgs;
  685. if (prev_nodes >= extra_msgs) {
  686. start_msg = min_msgs_per_node * prev_nodes + extra_msgs;
  687. num_msgs = min_msgs_per_node;
  688. } else {
  689. start_msg = min_msgs_per_node * prev_nodes + prev_nodes;
  690. num_msgs = min_msgs_per_node + 1;
  691. }
  692. // take number of messages into account
  693. if (start_msg >= N) {
  694. num_msgs = 0;
  695. } else if (start_msg + num_msgs > N) {
  696. num_msgs = N - start_msg;
  697. }
  698. if (routing_node == my_node_num) {
  699. // Special case: we're sending to ourselves; just put the
  700. // messages in our own buffer
  701. MsgBuffer &round1a = route_state.round1a;
  702. pthread_mutex_lock(&round1a.mutex);
  703. uint32_t start = round1a.reserved;
  704. if (start + num_msgs > round1a.bufsize) {
  705. pthread_mutex_unlock(&round1a.mutex);
  706. printf("Max %u messages exceeded in round 1a\n", round1a.bufsize);
  707. return;
  708. }
  709. round1a.reserved += num_msgs;
  710. pthread_mutex_unlock(&round1a.mutex);
  711. uint8_t *buf = round1a.buf + start * msg_size;
  712. for (uint32_t i=0; i<num_msgs; ++i) {
  713. const UidPriorityKey *idxp = indices + start_msg + i;
  714. memmove(buf, msgs + idxp->index()*msg_size, msg_size);
  715. buf += msg_size;
  716. }
  717. pthread_mutex_lock(&round1a.mutex);
  718. round1a.inserted += num_msgs;
  719. round1a.nodes_received += 1;
  720. pthread_mutex_unlock(&round1a.mutex);
  721. } else {
  722. NodeCommState &nodecom = g_commstates[routing_node];
  723. nodecom.message_start(num_msgs * msg_size);
  724. for (uint32_t i=0; i<num_msgs; ++i) {
  725. const UidPriorityKey *idxp = indices + start_msg + i;
  726. nodecom.message_data(msgs + idxp->index()*msg_size, msg_size);
  727. }
  728. }
  729. }
  730. }
  731. // Send the round 1b messages from the round 1a buffer, which only occurs in public-channel routing.
  732. // msgs points to the message buffer, and N is the number of non-padding items.
  733. // Return the number of messages sent
  734. static uint32_t send_round1b_msgs(const uint8_t *msgs, uint32_t N) {
  735. uint16_t msg_size = g_teems_config.msg_size;
  736. nodenum_t my_node_num = g_teems_config.my_node_num;
  737. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  738. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  739. // send to previous node
  740. if (prev_nodes > 0) {
  741. nodenum_t prev_node = g_teems_config.routing_nodes[num_routing_nodes-1];
  742. NodeCommState &nodecom = g_commstates[prev_node];
  743. uint32_t num_msgs = min(N, route_state.max_round1b_msgs_to_adj_rtr);
  744. // There are an extra 5 bytes at the end of this message: 4
  745. // bytes for the receiver id in the next message we _didn't_
  746. // send, and 1 byte for the number of messages we have at the
  747. // beginning of the buffer of messages we didn't send (max
  748. // pub_in) with the same receiver id
  749. nodecom.message_start(num_msgs * msg_size + 5);
  750. nodecom.message_data(msgs, num_msgs * msg_size);
  751. uint32_t next_receiver_id = 0xffffffff;
  752. uint8_t next_rid_count = 0;
  753. // num_msgs and N are not private, but the contents of the
  754. // buffer are.
  755. if (num_msgs < N) {
  756. next_receiver_id = *(const uint32_t *)(msgs +
  757. num_msgs * msg_size);
  758. next_rid_count = 1;
  759. }
  760. // TODO: If pub_in > 1, obliviously scan messages num_msgs+1 ..
  761. // num_msgs+(pub_in-1) and as long as they have the same
  762. // receiver id as next_receiver_id, add 1 to next_rid_count (but
  763. // don't go past message N of course)
  764. nodecom.message_data((const unsigned char *)&next_receiver_id, 4);
  765. nodecom.message_data(&next_rid_count, 1);
  766. return num_msgs;
  767. }
  768. return 0;
  769. }
  770. // Send the round 2 messages from the previous-round buffer, which are already
  771. // padded and shuffled, so this can be done non-obliviously. tot_msgs
  772. // is the total number of messages in the input buffer, which may
  773. // include padding messages added by the shuffle. Those messages are
  774. // not sent anywhere. There are num_msgs_per_stg messages for each
  775. // storage node labelled for that node.
  776. static void send_round2_msgs(uint32_t tot_msgs, uint32_t num_msgs_per_stg, MsgBuffer &prevround)
  777. {
  778. uint16_t msg_size = g_teems_config.msg_size;
  779. const uint8_t* buf = prevround.buf;
  780. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  781. nodenum_t my_node_num = g_teems_config.my_node_num;
  782. uint8_t *myself_buf = NULL;
  783. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  784. nodenum_t node = g_teems_config.storage_nodes[i];
  785. if (node != my_node_num) {
  786. g_commstates[node].message_start(msg_size * num_msgs_per_stg);
  787. } else {
  788. MsgBuffer &round2 = route_state.round2;
  789. pthread_mutex_lock(&round2.mutex);
  790. uint32_t start = round2.reserved;
  791. if (start + num_msgs_per_stg > round2.bufsize) {
  792. pthread_mutex_unlock(&round2.mutex);
  793. printf("Max %u messages exceeded (send_round2_msgs)\n",
  794. round2.bufsize);
  795. return;
  796. }
  797. round2.reserved += num_msgs_per_stg;
  798. pthread_mutex_unlock(&round2.mutex);
  799. myself_buf = round2.buf + start * msg_size;
  800. }
  801. }
  802. while (tot_msgs) {
  803. nodenum_t storage_node_id =
  804. nodenum_t((*(const uint32_t *)buf)>>DEST_UID_BITS);
  805. if (storage_node_id < num_storage_nodes) {
  806. nodenum_t node = g_teems_config.storage_map[storage_node_id];
  807. if (node == my_node_num) {
  808. memmove(myself_buf, buf, msg_size);
  809. myself_buf += msg_size;
  810. } else {
  811. g_commstates[node].message_data(buf, msg_size);
  812. }
  813. }
  814. buf += msg_size;
  815. --tot_msgs;
  816. }
  817. if (myself_buf) {
  818. MsgBuffer &round2 = route_state.round2;
  819. pthread_mutex_lock(&round2.mutex);
  820. round2.inserted += num_msgs_per_stg;
  821. round2.nodes_received += 1;
  822. pthread_mutex_unlock(&round2.mutex);
  823. }
  824. }
  825. static void round1a_processing(void *cbpointer) {
  826. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  827. MsgBuffer &round1 = route_state.round1;
  828. if (my_roles & ROLE_ROUTING) {
  829. route_state.cbpointer = cbpointer;
  830. pthread_mutex_lock(&round1.mutex);
  831. // Ensure there are no pending messages currently being inserted
  832. // into the buffer
  833. while (round1.reserved != round1.inserted) {
  834. pthread_mutex_unlock(&round1.mutex);
  835. pthread_mutex_lock(&round1.mutex);
  836. }
  837. #ifdef TRACE_ROUTING
  838. show_messages("Start of round 1a", round1.buf, round1.inserted);
  839. #endif
  840. #ifdef PROFILE_ROUTING
  841. uint32_t inserted = round1.inserted;
  842. unsigned long start_round1a =
  843. printf_with_rtclock("begin round1a processing (%u)\n", inserted);
  844. // Sort the messages we've received
  845. unsigned long start_sort =
  846. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  847. route_state.max_round1_msgs);
  848. #endif
  849. // Sort received messages by increasing user ID and
  850. // priority. Larger priority number indicates higher priority.
  851. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, round1.buf,
  852. g_teems_config.msg_size, round1.inserted, route_state.max_round1_msgs,
  853. send_round1a_msgs);
  854. #ifdef PROFILE_ROUTING
  855. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  856. inserted, route_state.max_round1_msgs);
  857. printf_with_rtclock_diff(start_round1a, "end round1a processing (%u)\n",
  858. inserted);
  859. #endif
  860. round1.reset();
  861. pthread_mutex_unlock(&round1.mutex);
  862. MsgBuffer &round1a = route_state.round1a;
  863. pthread_mutex_lock(&round1a.mutex);
  864. round1a.completed_prev_round = true;
  865. nodenum_t nodes_received = round1a.nodes_received;
  866. pthread_mutex_unlock(&round1a.mutex);
  867. if (nodes_received == g_teems_config.num_routing_nodes) {
  868. route_state.step = ROUTE_ROUND_1A;
  869. route_state.cbpointer = NULL;
  870. ocall_routing_round_complete(cbpointer, ROUND_1A);
  871. }
  872. } else {
  873. route_state.step = ROUTE_ROUND_1A;
  874. route_state.round1a.completed_prev_round = true;
  875. ocall_routing_round_complete(cbpointer, ROUND_1A);
  876. }
  877. }
  878. static void round1b_processing(void *cbpointer) {
  879. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  880. nodenum_t my_node_num = g_teems_config.my_node_num;
  881. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  882. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  883. MsgBuffer &round1a = route_state.round1a;
  884. MsgBuffer &round1a_sorted = route_state.round1a_sorted;
  885. if (my_roles & ROLE_ROUTING) {
  886. route_state.cbpointer = cbpointer;
  887. pthread_mutex_lock(&round1a.mutex);
  888. // Ensure there are no pending messages currently being inserted
  889. // into the buffer
  890. while (round1a.reserved != round1a.inserted) {
  891. pthread_mutex_unlock(&round1a.mutex);
  892. pthread_mutex_lock(&round1a.mutex);
  893. }
  894. pthread_mutex_lock(&round1a_sorted.mutex);
  895. #ifdef TRACE_ROUTING
  896. show_messages("Start of round 1b", round1a.buf, round1a.inserted);
  897. #endif
  898. #ifdef PROFILE_ROUTING
  899. uint32_t inserted = round1a.inserted;
  900. unsigned long start_round1b =
  901. printf_with_rtclock("begin round1b processing (%u)\n", inserted);
  902. #endif
  903. // Sort the messages we've received
  904. // Sort received messages by increasing user ID and
  905. // priority. Larger priority number indicates higher priority.
  906. if (inserted > 0) {
  907. // copy items in sorted order into round1a_sorted
  908. #ifdef PROFILE_ROUTING
  909. unsigned long start_sort =
  910. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  911. route_state.max_round1a_msgs);
  912. #endif
  913. route_state.round1a_sorted.reserved = round1a.inserted;
  914. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, round1a.buf,
  915. g_teems_config.msg_size, round1a.inserted,
  916. route_state.max_round1a_msgs,
  917. route_state.round1a_sorted.buf);
  918. route_state.round1a_sorted.inserted = round1a.inserted;
  919. #ifdef PROFILE_ROUTING
  920. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  921. inserted, route_state.max_round1a_msgs);
  922. #endif
  923. #ifdef TRACE_ROUTING
  924. show_messages("In round 1b (sorted)", round1a_sorted.buf,
  925. round1a_sorted.inserted);
  926. #endif
  927. uint32_t num_sent = send_round1b_msgs(round1a_sorted.buf,
  928. round1a_sorted.inserted);
  929. // Remove the sent messages from the buffer
  930. memmove(round1a_sorted.buf,
  931. round1a_sorted.buf + num_sent * g_teems_config.msg_size,
  932. (round1a_sorted.inserted - num_sent) * g_teems_config.msg_size);
  933. round1a_sorted.inserted -= num_sent;
  934. round1a_sorted.reserved -= num_sent;
  935. #ifdef TRACE_ROUTING
  936. show_messages("In round 1b (after sending initial block)",
  937. round1a_sorted.buf, round1a_sorted.inserted);
  938. #endif
  939. } else {
  940. send_round1b_msgs(NULL, 0);
  941. }
  942. #ifdef PROFILE_ROUTING
  943. printf_with_rtclock_diff(start_round1b, "end round1b processing (%u)\n",
  944. inserted);
  945. #endif
  946. pthread_mutex_unlock(&round1a_sorted.mutex);
  947. pthread_mutex_unlock(&round1a.mutex);
  948. MsgBuffer &round1b_next = route_state.round1b_next;
  949. pthread_mutex_lock(&round1b_next.mutex);
  950. round1b_next.completed_prev_round = true;
  951. nodenum_t nodes_received = round1b_next.nodes_received;
  952. pthread_mutex_unlock(&round1b_next.mutex);
  953. nodenum_t adjacent_nodes;
  954. if (num_routing_nodes == 1) {
  955. adjacent_nodes = 0;
  956. } else if (prev_nodes == num_routing_nodes-1) {
  957. adjacent_nodes = 0;
  958. } else {
  959. adjacent_nodes = 1;
  960. }
  961. if (nodes_received == adjacent_nodes) {
  962. route_state.step = ROUTE_ROUND_1B;
  963. route_state.cbpointer = NULL;
  964. ocall_routing_round_complete(cbpointer, ROUND_1B);
  965. }
  966. } else {
  967. route_state.step = ROUTE_ROUND_1B;
  968. route_state.round1b_next.completed_prev_round = true;
  969. ocall_routing_round_complete(cbpointer, ROUND_1B);
  970. }
  971. }
  972. static void copy_msgs(uint8_t *dst, uint32_t start_msg, uint32_t num_copy, const uint8_t *src,
  973. const UidPriorityKey *indices)
  974. {
  975. uint16_t msg_size = g_teems_config.msg_size;
  976. const UidPriorityKey *idxp = indices + start_msg;
  977. uint8_t *buf = dst;
  978. for (uint32_t i=0; i<num_copy; i++) {
  979. memmove(buf, src + idxp[i].index()*msg_size, msg_size);
  980. buf += msg_size;
  981. }
  982. }
  983. static void round1c_processing(void *cbpointer) {
  984. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  985. nodenum_t my_node_num = g_teems_config.my_node_num;
  986. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  987. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  988. uint16_t msg_size = g_teems_config.msg_size;
  989. uint32_t max_round1b_msgs_to_adj_rtr = route_state.max_round1b_msgs_to_adj_rtr;
  990. uint32_t max_round1a_msgs = route_state.max_round1a_msgs;
  991. MsgBuffer &round1a = route_state.round1a;
  992. MsgBuffer &round1a_sorted = route_state.round1a_sorted;
  993. MsgBuffer &round1b_next = route_state.round1b_next;
  994. if (my_roles & ROLE_ROUTING) {
  995. route_state.cbpointer = cbpointer;
  996. pthread_mutex_lock(&round1b_next.mutex);
  997. // Ensure there are no pending messages currently being inserted
  998. // into the round 1b buffer
  999. while (round1b_next.reserved != round1b_next.inserted) {
  1000. pthread_mutex_unlock(&round1b_next.mutex);
  1001. pthread_mutex_lock(&round1b_next.mutex);
  1002. }
  1003. uint32_t next_receiver_id = 0xffffffff;
  1004. uint8_t next_rid_count = 0;
  1005. // Extract the trailing 5 bytes if we received a round1b message
  1006. if (round1b_next.inserted >= 1) {
  1007. next_receiver_id = *(uint32_t *)(round1b_next.buf +
  1008. (round1b_next.inserted-1)*msg_size);
  1009. next_rid_count = *(round1b_next.buf +
  1010. (round1b_next.inserted-1)*msg_size + 4);
  1011. round1b_next.inserted -= 1;
  1012. round1b_next.reserved -= 1;
  1013. }
  1014. pthread_mutex_lock(&round1a.mutex);
  1015. pthread_mutex_lock(&round1a_sorted.mutex);
  1016. #ifdef TRACE_ROUTING
  1017. show_messages("Start of round 1c", round1a_sorted.buf,
  1018. round1a_sorted.inserted);
  1019. #endif
  1020. #ifdef PROFILE_ROUTING
  1021. unsigned long start_round1c = printf_with_rtclock("begin round1c processing (%u)\n", round1a.inserted);
  1022. #endif
  1023. // sort round1b_next msgs with final msgs in round1a_sorted
  1024. if ((prev_nodes < num_routing_nodes-1) && (round1b_next.inserted > 0)) {
  1025. // Copy final msgs in round1a_sorted to round1b_next buffer for sorting
  1026. // Note that all inserted values and buffer sizes are non-secret
  1027. uint32_t num_final_round1a = std::min(
  1028. round1a_sorted.inserted, max_round1b_msgs_to_adj_rtr);
  1029. uint32_t round1a_msg_start =
  1030. round1a_sorted.inserted-num_final_round1a;
  1031. uint32_t num_round1b_next = round1b_next.inserted;
  1032. round1b_next.reserved += num_final_round1a;
  1033. memmove(round1b_next.buf+num_round1b_next*msg_size,
  1034. round1a_sorted.buf + round1a_msg_start*msg_size,
  1035. num_final_round1a*msg_size);
  1036. round1b_next.inserted += num_final_round1a;
  1037. round1a_sorted.reserved -= num_final_round1a;
  1038. round1a_sorted.inserted -= num_final_round1a;
  1039. #ifdef TRACE_ROUTING
  1040. show_messages("In round 1c (after setting aside)",
  1041. round1a_sorted.buf, round1a_sorted.inserted);
  1042. show_messages("In round 1c (the aside)", round1b_next.buf,
  1043. round1b_next.inserted);
  1044. #endif
  1045. // sort and append to round1a msgs
  1046. uint32_t num_copy = round1b_next.inserted;
  1047. #ifdef PROFILE_ROUTING
  1048. unsigned long start_sort =
  1049. printf_with_rtclock("begin round1b_next oblivious sort (%u,%u)\n",
  1050. num_copy, 2*max_round1b_msgs_to_adj_rtr);
  1051. #endif
  1052. round1a_sorted.reserved += num_copy;
  1053. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads,
  1054. round1b_next.buf, msg_size, num_copy,
  1055. 2*max_round1b_msgs_to_adj_rtr,
  1056. [&](const uint8_t *src, const UidPriorityKey *indices, uint32_t Nr) {
  1057. return copy_msgs(round1a_sorted.buf +
  1058. round1a_sorted.inserted * msg_size, 0, num_copy,
  1059. src, indices);
  1060. }
  1061. );
  1062. round1a_sorted.inserted += num_copy;
  1063. #ifdef PROFILE_ROUTING
  1064. printf_with_rtclock_diff(start_sort,
  1065. "end round1b_next oblivious sort (%u,%u)\n",
  1066. num_copy, 2*max_round1b_msgs_to_adj_rtr);
  1067. #endif
  1068. }
  1069. #ifdef TRACE_ROUTING
  1070. printf("round1a_sorted.inserted = %lu, reserved = %lu, bufsize = %lu\n",
  1071. round1a_sorted.inserted, round1a_sorted.reserved,
  1072. round1a_sorted.bufsize);
  1073. show_messages("In round 1c", round1a_sorted.buf,
  1074. round1a_sorted.inserted);
  1075. #endif
  1076. send_round_robin_msgs<UidPriorityKey>(route_state.round1c,
  1077. round1a_sorted.buf, NULL, round1a_sorted.inserted);
  1078. #ifdef PROFILE_ROUTING
  1079. printf_with_rtclock_diff(start_round1c, "end round1c processing (%u)\n",
  1080. round1a.inserted);
  1081. #endif
  1082. round1a.reset();
  1083. round1a_sorted.reset();
  1084. round1b_next.reset();
  1085. pthread_mutex_unlock(&round1a_sorted.mutex);
  1086. pthread_mutex_unlock(&round1a.mutex);
  1087. pthread_mutex_unlock(&round1b_next.mutex);
  1088. MsgBuffer &round1c = route_state.round1c;
  1089. pthread_mutex_lock(&round1c.mutex);
  1090. round1c.completed_prev_round = true;
  1091. nodenum_t nodes_received = round1c.nodes_received;
  1092. pthread_mutex_unlock(&round1c.mutex);
  1093. if (nodes_received == num_routing_nodes) {
  1094. route_state.step = ROUTE_ROUND_1C;
  1095. route_state.cbpointer = NULL;
  1096. ocall_routing_round_complete(cbpointer, ROUND_1C);
  1097. }
  1098. } else {
  1099. route_state.step = ROUTE_ROUND_1C;
  1100. route_state.round1c.completed_prev_round = true;
  1101. ocall_routing_round_complete(cbpointer, ROUND_1C);
  1102. }
  1103. }
  1104. // Process messages in round 2
  1105. static void round2_processing(uint8_t my_roles, void *cbpointer, MsgBuffer &prevround) {
  1106. if (my_roles & ROLE_ROUTING) {
  1107. route_state.cbpointer = cbpointer;
  1108. pthread_mutex_lock(&prevround.mutex);
  1109. // Ensure there are no pending messages currently being inserted
  1110. // into the buffer
  1111. while (prevround.reserved != prevround.inserted) {
  1112. pthread_mutex_unlock(&prevround.mutex);
  1113. pthread_mutex_lock(&prevround.mutex);
  1114. }
  1115. // If the _total_ number of messages we received in round 1
  1116. // is less than the max number of messages we could send to
  1117. // _each_ storage node, then cap the number of messages we
  1118. // will send to each storage node to that number.
  1119. uint32_t msgs_per_stg = route_state.max_msg_to_each_stg;
  1120. if (prevround.inserted < msgs_per_stg) {
  1121. msgs_per_stg = prevround.inserted;
  1122. }
  1123. // Note: at this point, it is required that each message in
  1124. // the prevround buffer have a _valid_ storage node id field.
  1125. // Obliviously tally the number of messages we received in
  1126. // the previous round destined for each storage node
  1127. #ifdef TRACE_ROUTING
  1128. show_messages("Start of round 2", prevround.buf, prevround.inserted);
  1129. #endif
  1130. #ifdef PROFILE_ROUTING
  1131. unsigned long start_round2 = printf_with_rtclock("begin round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1132. unsigned long start_tally = printf_with_rtclock("begin tally (%u)\n", prevround.inserted);
  1133. #endif
  1134. uint16_t msg_size = g_teems_config.msg_size;
  1135. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  1136. std::vector<uint32_t> tally = obliv_tally_stg(
  1137. prevround.buf, msg_size, prevround.inserted, num_storage_nodes);
  1138. #ifdef PROFILE_ROUTING
  1139. printf_with_rtclock_diff(start_tally, "end tally (%u)\n", prevround.inserted);
  1140. #endif
  1141. // Note: tally contains private values! It's OK to
  1142. // non-obliviously check for an error condition, though.
  1143. // While we're at it, obliviously change the tally of
  1144. // messages received to a tally of padding messages
  1145. // required.
  1146. uint32_t tot_padding = 0;
  1147. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  1148. if (tally[i] > msgs_per_stg) {
  1149. printf("Received too many messages for storage node %u\n", i);
  1150. assert(tally[i] <= msgs_per_stg);
  1151. }
  1152. tally[i] = msgs_per_stg - tally[i];
  1153. tot_padding += tally[i];
  1154. }
  1155. prevround.reserved += tot_padding;
  1156. assert(prevround.reserved <= prevround.bufsize);
  1157. // Obliviously add padding for each storage node according
  1158. // to the (private) padding tally.
  1159. #ifdef PROFILE_ROUTING
  1160. unsigned long start_pad = printf_with_rtclock("begin pad (%u)\n", tot_padding);
  1161. #endif
  1162. obliv_pad_stg(prevround.buf + prevround.inserted * msg_size,
  1163. msg_size, tally, tot_padding);
  1164. #ifdef PROFILE_ROUTING
  1165. printf_with_rtclock_diff(start_pad, "end pad (%u)\n", tot_padding);
  1166. #endif
  1167. prevround.inserted += tot_padding;
  1168. // Obliviously shuffle the messages
  1169. #ifdef PROFILE_ROUTING
  1170. unsigned long start_shuffle = printf_with_rtclock("begin shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1171. #endif
  1172. uint32_t num_shuffled = shuffle_mtobliv(g_teems_config.nthreads,
  1173. prevround.buf, msg_size, prevround.inserted, prevround.bufsize);
  1174. #ifdef PROFILE_ROUTING
  1175. printf_with_rtclock_diff(start_shuffle, "end shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1176. printf_with_rtclock_diff(start_round2, "end round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1177. #endif
  1178. // Now we can handle the messages non-obliviously, since we
  1179. // know there will be exactly msgs_per_stg messages to each
  1180. // storage node, and the oblivious shuffle broke the
  1181. // connection between where each message came from and where
  1182. // it's going.
  1183. send_round2_msgs(num_shuffled, msgs_per_stg, prevround);
  1184. prevround.reset();
  1185. pthread_mutex_unlock(&prevround.mutex);
  1186. }
  1187. if (my_roles & ROLE_STORAGE) {
  1188. route_state.cbpointer = cbpointer;
  1189. MsgBuffer &round2 = route_state.round2;
  1190. pthread_mutex_lock(&round2.mutex);
  1191. round2.completed_prev_round = true;
  1192. nodenum_t nodes_received = round2.nodes_received;
  1193. pthread_mutex_unlock(&round2.mutex);
  1194. if (nodes_received == g_teems_config.num_routing_nodes) {
  1195. route_state.step = ROUTE_ROUND_2;
  1196. route_state.cbpointer = NULL;
  1197. ocall_routing_round_complete(cbpointer, 2);
  1198. }
  1199. } else {
  1200. route_state.step = ROUTE_ROUND_2;
  1201. route_state.round2.completed_prev_round = true;
  1202. ocall_routing_round_complete(cbpointer, 2);
  1203. }
  1204. }
  1205. // Perform the next round of routing. The callback pointer will be
  1206. // passed to ocall_routing_round_complete when the round is complete.
  1207. void ecall_routing_proceed(void *cbpointer)
  1208. {
  1209. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  1210. if (route_state.step == ROUTE_NOT_STARTED) {
  1211. if (my_roles & ROLE_INGESTION) {
  1212. ingestion_epoch++;
  1213. route_state.cbpointer = cbpointer;
  1214. MsgBuffer &ingbuf = route_state.ingbuf;
  1215. pthread_mutex_lock(&ingbuf.mutex);
  1216. // Ensure there are no pending messages currently being inserted
  1217. // into the buffer
  1218. while (ingbuf.reserved != ingbuf.inserted) {
  1219. pthread_mutex_unlock(&ingbuf.mutex);
  1220. pthread_mutex_lock(&ingbuf.mutex);
  1221. }
  1222. // Sort the messages we've received
  1223. #ifdef TRACE_ROUTING
  1224. show_messages("Start of round 1", ingbuf.buf, ingbuf.inserted);
  1225. #endif
  1226. #ifdef PROFILE_ROUTING
  1227. uint32_t inserted = ingbuf.inserted;
  1228. unsigned long start_round1 =
  1229. printf_with_rtclock("begin round1 processing (%u)\n", inserted);
  1230. unsigned long start_sort =
  1231. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  1232. route_state.tot_msg_per_ing);
  1233. #endif
  1234. if (g_teems_config.private_routing) {
  1235. sort_mtobliv<UidKey>(g_teems_config.nthreads, ingbuf.buf,
  1236. g_teems_config.msg_size, ingbuf.inserted,
  1237. route_state.tot_msg_per_ing,
  1238. [&](const uint8_t *msgs, const UidKey *indices, uint32_t N) {
  1239. send_round_robin_msgs<UidKey>(route_state.round1,
  1240. msgs, indices, N);
  1241. });
  1242. } else {
  1243. // Sort received messages by increasing user ID and
  1244. // priority. Larger priority number indicates higher priority.
  1245. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, ingbuf.buf,
  1246. g_teems_config.msg_size, ingbuf.inserted,
  1247. route_state.tot_msg_per_ing,
  1248. [&](const uint8_t *msgs,
  1249. const UidPriorityKey *indices, uint32_t N) {
  1250. send_round_robin_msgs<UidPriorityKey>(route_state.round1,
  1251. msgs, indices, N);
  1252. });
  1253. }
  1254. #ifdef PROFILE_ROUTING
  1255. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  1256. inserted, route_state.tot_msg_per_ing);
  1257. printf_with_rtclock_diff(start_round1, "end round1 processing (%u)\n",
  1258. inserted);
  1259. #endif
  1260. ingbuf.reset();
  1261. pthread_mutex_unlock(&ingbuf.mutex);
  1262. }
  1263. if (my_roles & ROLE_ROUTING) {
  1264. MsgBuffer &round1 = route_state.round1;
  1265. pthread_mutex_lock(&round1.mutex);
  1266. round1.completed_prev_round = true;
  1267. nodenum_t nodes_received = round1.nodes_received;
  1268. pthread_mutex_unlock(&round1.mutex);
  1269. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  1270. route_state.step = ROUTE_ROUND_1;
  1271. route_state.cbpointer = NULL;
  1272. ocall_routing_round_complete(cbpointer, 1);
  1273. }
  1274. } else {
  1275. route_state.step = ROUTE_ROUND_1;
  1276. route_state.round1.completed_prev_round = true;
  1277. ocall_routing_round_complete(cbpointer, 1);
  1278. }
  1279. } else if (route_state.step == ROUTE_ROUND_1) {
  1280. if (g_teems_config.private_routing) { // private routing next round
  1281. round2_processing(my_roles, cbpointer, route_state.round1);
  1282. } else { // public routing next round
  1283. round1a_processing(cbpointer);
  1284. }
  1285. } else if (route_state.step == ROUTE_ROUND_1A) {
  1286. round1b_processing(cbpointer);
  1287. } else if (route_state.step == ROUTE_ROUND_1B) {
  1288. round1c_processing(cbpointer);
  1289. } else if (route_state.step == ROUTE_ROUND_1C) {
  1290. round2_processing(my_roles, cbpointer, route_state.round1c);
  1291. } else if (route_state.step == ROUTE_ROUND_2) {
  1292. if (my_roles & ROLE_STORAGE) {
  1293. MsgBuffer &round2 = route_state.round2;
  1294. pthread_mutex_lock(&round2.mutex);
  1295. // Ensure there are no pending messages currently being inserted
  1296. // into the buffer
  1297. while (round2.reserved != round2.inserted) {
  1298. pthread_mutex_unlock(&round2.mutex);
  1299. pthread_mutex_lock(&round2.mutex);
  1300. }
  1301. unsigned long start = printf_with_rtclock("begin storage processing (%u)\n", round2.inserted);
  1302. storage_received(round2);
  1303. printf_with_rtclock_diff(start, "end storage processing (%u)\n", round2.inserted);
  1304. // We're done
  1305. route_state.step = ROUTE_NOT_STARTED;
  1306. ocall_routing_round_complete(cbpointer, 0);
  1307. } else {
  1308. // We're done
  1309. route_state.step = ROUTE_NOT_STARTED;
  1310. ocall_routing_round_complete(cbpointer, 0);
  1311. }
  1312. }
  1313. }