route.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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. // If pub_in > 1, obliviously scan messages num_msgs+1 ..
  760. // num_msgs+(pub_in-1) and as long as they have the same
  761. // receiver id as next_receiver_id, add 1 to next_rid_count (but
  762. // don't go past message N of course)
  763. // This count _includes_ the first message already scanned
  764. // above. It is not private.
  765. uint8_t num_to_scan = uint8_t(std::min(N - num_msgs,
  766. uint32_t(g_teems_config.m_pub_in)));
  767. const unsigned char *scan_msg = msgs +
  768. (num_msgs + 1) * msg_size;
  769. for (uint8_t i=1; i<num_to_scan; ++i) {
  770. uint32_t receiver_id = *(const uint32_t *)scan_msg;
  771. next_rid_count += (receiver_id == next_receiver_id);
  772. scan_msg += msg_size;
  773. }
  774. }
  775. nodecom.message_data((const unsigned char *)&next_receiver_id, 4);
  776. nodecom.message_data(&next_rid_count, 1);
  777. return num_msgs;
  778. }
  779. return 0;
  780. }
  781. // Send the round 2 messages from the previous-round buffer, which are already
  782. // padded and shuffled, so this can be done non-obliviously. tot_msgs
  783. // is the total number of messages in the input buffer, which may
  784. // include padding messages added by the shuffle. Those messages are
  785. // not sent anywhere. There are num_msgs_per_stg messages for each
  786. // storage node labelled for that node.
  787. static void send_round2_msgs(uint32_t tot_msgs, uint32_t num_msgs_per_stg, MsgBuffer &prevround)
  788. {
  789. uint16_t msg_size = g_teems_config.msg_size;
  790. const uint8_t* buf = prevround.buf;
  791. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  792. nodenum_t my_node_num = g_teems_config.my_node_num;
  793. uint8_t *myself_buf = NULL;
  794. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  795. nodenum_t node = g_teems_config.storage_nodes[i];
  796. if (node != my_node_num) {
  797. g_commstates[node].message_start(msg_size * num_msgs_per_stg);
  798. } else {
  799. MsgBuffer &round2 = route_state.round2;
  800. pthread_mutex_lock(&round2.mutex);
  801. uint32_t start = round2.reserved;
  802. if (start + num_msgs_per_stg > round2.bufsize) {
  803. pthread_mutex_unlock(&round2.mutex);
  804. printf("Max %u messages exceeded (send_round2_msgs)\n",
  805. round2.bufsize);
  806. return;
  807. }
  808. round2.reserved += num_msgs_per_stg;
  809. pthread_mutex_unlock(&round2.mutex);
  810. myself_buf = round2.buf + start * msg_size;
  811. }
  812. }
  813. while (tot_msgs) {
  814. nodenum_t storage_node_id =
  815. nodenum_t((*(const uint32_t *)buf)>>DEST_UID_BITS);
  816. if (storage_node_id < num_storage_nodes) {
  817. nodenum_t node = g_teems_config.storage_map[storage_node_id];
  818. if (node == my_node_num) {
  819. memmove(myself_buf, buf, msg_size);
  820. myself_buf += msg_size;
  821. } else {
  822. g_commstates[node].message_data(buf, msg_size);
  823. }
  824. }
  825. buf += msg_size;
  826. --tot_msgs;
  827. }
  828. if (myself_buf) {
  829. MsgBuffer &round2 = route_state.round2;
  830. pthread_mutex_lock(&round2.mutex);
  831. round2.inserted += num_msgs_per_stg;
  832. round2.nodes_received += 1;
  833. pthread_mutex_unlock(&round2.mutex);
  834. }
  835. }
  836. static void round1a_processing(void *cbpointer) {
  837. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  838. MsgBuffer &round1 = route_state.round1;
  839. if (my_roles & ROLE_ROUTING) {
  840. route_state.cbpointer = cbpointer;
  841. pthread_mutex_lock(&round1.mutex);
  842. // Ensure there are no pending messages currently being inserted
  843. // into the buffer
  844. while (round1.reserved != round1.inserted) {
  845. pthread_mutex_unlock(&round1.mutex);
  846. pthread_mutex_lock(&round1.mutex);
  847. }
  848. #ifdef TRACE_ROUTING
  849. show_messages("Start of round 1a", round1.buf, round1.inserted);
  850. #endif
  851. #ifdef PROFILE_ROUTING
  852. uint32_t inserted = round1.inserted;
  853. unsigned long start_round1a =
  854. printf_with_rtclock("begin round1a processing (%u)\n", inserted);
  855. // Sort the messages we've received
  856. unsigned long start_sort =
  857. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  858. route_state.max_round1_msgs);
  859. #endif
  860. // Sort received messages by increasing user ID and
  861. // priority. Larger priority number indicates higher priority.
  862. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, round1.buf,
  863. g_teems_config.msg_size, round1.inserted, route_state.max_round1_msgs,
  864. send_round1a_msgs);
  865. #ifdef PROFILE_ROUTING
  866. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  867. inserted, route_state.max_round1_msgs);
  868. printf_with_rtclock_diff(start_round1a, "end round1a processing (%u)\n",
  869. inserted);
  870. #endif
  871. round1.reset();
  872. pthread_mutex_unlock(&round1.mutex);
  873. MsgBuffer &round1a = route_state.round1a;
  874. pthread_mutex_lock(&round1a.mutex);
  875. round1a.completed_prev_round = true;
  876. nodenum_t nodes_received = round1a.nodes_received;
  877. pthread_mutex_unlock(&round1a.mutex);
  878. if (nodes_received == g_teems_config.num_routing_nodes) {
  879. route_state.step = ROUTE_ROUND_1A;
  880. route_state.cbpointer = NULL;
  881. ocall_routing_round_complete(cbpointer, ROUND_1A);
  882. }
  883. } else {
  884. route_state.step = ROUTE_ROUND_1A;
  885. route_state.round1a.completed_prev_round = true;
  886. ocall_routing_round_complete(cbpointer, ROUND_1A);
  887. }
  888. }
  889. static void round1b_processing(void *cbpointer) {
  890. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  891. nodenum_t my_node_num = g_teems_config.my_node_num;
  892. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  893. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  894. MsgBuffer &round1a = route_state.round1a;
  895. MsgBuffer &round1a_sorted = route_state.round1a_sorted;
  896. if (my_roles & ROLE_ROUTING) {
  897. route_state.cbpointer = cbpointer;
  898. pthread_mutex_lock(&round1a.mutex);
  899. // Ensure there are no pending messages currently being inserted
  900. // into the buffer
  901. while (round1a.reserved != round1a.inserted) {
  902. pthread_mutex_unlock(&round1a.mutex);
  903. pthread_mutex_lock(&round1a.mutex);
  904. }
  905. pthread_mutex_lock(&round1a_sorted.mutex);
  906. #ifdef TRACE_ROUTING
  907. show_messages("Start of round 1b", round1a.buf, round1a.inserted);
  908. #endif
  909. #ifdef PROFILE_ROUTING
  910. uint32_t inserted = round1a.inserted;
  911. unsigned long start_round1b =
  912. printf_with_rtclock("begin round1b processing (%u)\n", inserted);
  913. #endif
  914. // Sort the messages we've received
  915. // Sort received messages by increasing user ID and
  916. // priority. Larger priority number indicates higher priority.
  917. if (inserted > 0) {
  918. // copy items in sorted order into round1a_sorted
  919. #ifdef PROFILE_ROUTING
  920. unsigned long start_sort =
  921. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  922. route_state.max_round1a_msgs);
  923. #endif
  924. route_state.round1a_sorted.reserved = round1a.inserted;
  925. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, round1a.buf,
  926. g_teems_config.msg_size, round1a.inserted,
  927. route_state.max_round1a_msgs,
  928. route_state.round1a_sorted.buf);
  929. route_state.round1a_sorted.inserted = round1a.inserted;
  930. #ifdef PROFILE_ROUTING
  931. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  932. inserted, route_state.max_round1a_msgs);
  933. #endif
  934. #ifdef TRACE_ROUTING
  935. show_messages("In round 1b (sorted)", round1a_sorted.buf,
  936. round1a_sorted.inserted);
  937. #endif
  938. uint32_t num_sent = send_round1b_msgs(round1a_sorted.buf,
  939. round1a_sorted.inserted);
  940. // Remove the sent messages from the buffer
  941. memmove(round1a_sorted.buf,
  942. round1a_sorted.buf + num_sent * g_teems_config.msg_size,
  943. (round1a_sorted.inserted - num_sent) * g_teems_config.msg_size);
  944. round1a_sorted.inserted -= num_sent;
  945. round1a_sorted.reserved -= num_sent;
  946. #ifdef TRACE_ROUTING
  947. show_messages("In round 1b (after sending initial block)",
  948. round1a_sorted.buf, round1a_sorted.inserted);
  949. #endif
  950. } else {
  951. send_round1b_msgs(NULL, 0);
  952. }
  953. #ifdef PROFILE_ROUTING
  954. printf_with_rtclock_diff(start_round1b, "end round1b processing (%u)\n",
  955. inserted);
  956. #endif
  957. pthread_mutex_unlock(&round1a_sorted.mutex);
  958. pthread_mutex_unlock(&round1a.mutex);
  959. MsgBuffer &round1b_next = route_state.round1b_next;
  960. pthread_mutex_lock(&round1b_next.mutex);
  961. round1b_next.completed_prev_round = true;
  962. nodenum_t nodes_received = round1b_next.nodes_received;
  963. pthread_mutex_unlock(&round1b_next.mutex);
  964. nodenum_t adjacent_nodes;
  965. if (num_routing_nodes == 1) {
  966. adjacent_nodes = 0;
  967. } else if (prev_nodes == num_routing_nodes-1) {
  968. adjacent_nodes = 0;
  969. } else {
  970. adjacent_nodes = 1;
  971. }
  972. if (nodes_received == adjacent_nodes) {
  973. route_state.step = ROUTE_ROUND_1B;
  974. route_state.cbpointer = NULL;
  975. ocall_routing_round_complete(cbpointer, ROUND_1B);
  976. }
  977. } else {
  978. route_state.step = ROUTE_ROUND_1B;
  979. route_state.round1b_next.completed_prev_round = true;
  980. ocall_routing_round_complete(cbpointer, ROUND_1B);
  981. }
  982. }
  983. static void copy_msgs(uint8_t *dst, uint32_t start_msg, uint32_t num_copy, const uint8_t *src,
  984. const UidPriorityKey *indices)
  985. {
  986. uint16_t msg_size = g_teems_config.msg_size;
  987. const UidPriorityKey *idxp = indices + start_msg;
  988. uint8_t *buf = dst;
  989. for (uint32_t i=0; i<num_copy; i++) {
  990. memmove(buf, src + idxp[i].index()*msg_size, msg_size);
  991. buf += msg_size;
  992. }
  993. }
  994. static void round1c_processing(void *cbpointer) {
  995. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  996. nodenum_t my_node_num = g_teems_config.my_node_num;
  997. nodenum_t num_routing_nodes = g_teems_config.num_routing_nodes;
  998. uint16_t prev_nodes = g_teems_config.weights[my_node_num].startweight;
  999. uint16_t msg_size = g_teems_config.msg_size;
  1000. uint32_t max_round1b_msgs_to_adj_rtr = route_state.max_round1b_msgs_to_adj_rtr;
  1001. uint32_t max_round1a_msgs = route_state.max_round1a_msgs;
  1002. MsgBuffer &round1a = route_state.round1a;
  1003. MsgBuffer &round1a_sorted = route_state.round1a_sorted;
  1004. MsgBuffer &round1b_next = route_state.round1b_next;
  1005. if (my_roles & ROLE_ROUTING) {
  1006. route_state.cbpointer = cbpointer;
  1007. pthread_mutex_lock(&round1b_next.mutex);
  1008. // Ensure there are no pending messages currently being inserted
  1009. // into the round 1b buffer
  1010. while (round1b_next.reserved != round1b_next.inserted) {
  1011. pthread_mutex_unlock(&round1b_next.mutex);
  1012. pthread_mutex_lock(&round1b_next.mutex);
  1013. }
  1014. uint32_t next_receiver_id = 0xffffffff;
  1015. uint8_t next_rid_count = 0;
  1016. // Extract the trailing 5 bytes if we received a round1b message
  1017. if (round1b_next.inserted >= 1) {
  1018. next_receiver_id = *(uint32_t *)(round1b_next.buf +
  1019. (round1b_next.inserted-1)*msg_size);
  1020. next_rid_count = *(round1b_next.buf +
  1021. (round1b_next.inserted-1)*msg_size + 4);
  1022. round1b_next.inserted -= 1;
  1023. round1b_next.reserved -= 1;
  1024. }
  1025. pthread_mutex_lock(&round1a.mutex);
  1026. pthread_mutex_lock(&round1a_sorted.mutex);
  1027. #ifdef TRACE_ROUTING
  1028. show_messages("Start of round 1c", round1a_sorted.buf,
  1029. round1a_sorted.inserted);
  1030. #endif
  1031. #ifdef PROFILE_ROUTING
  1032. unsigned long start_round1c = printf_with_rtclock("begin round1c processing (%u)\n", round1a.inserted);
  1033. #endif
  1034. // sort round1b_next msgs with final msgs in round1a_sorted
  1035. if ((prev_nodes < num_routing_nodes-1) && (round1b_next.inserted > 0)) {
  1036. // Copy final msgs in round1a_sorted to round1b_next buffer for sorting
  1037. // Note that all inserted values and buffer sizes are non-secret
  1038. uint32_t num_final_round1a = std::min(
  1039. round1a_sorted.inserted, max_round1b_msgs_to_adj_rtr);
  1040. uint32_t round1a_msg_start =
  1041. round1a_sorted.inserted-num_final_round1a;
  1042. uint32_t num_round1b_next = round1b_next.inserted;
  1043. round1b_next.reserved += num_final_round1a;
  1044. memmove(round1b_next.buf+num_round1b_next*msg_size,
  1045. round1a_sorted.buf + round1a_msg_start*msg_size,
  1046. num_final_round1a*msg_size);
  1047. round1b_next.inserted += num_final_round1a;
  1048. round1a_sorted.reserved -= num_final_round1a;
  1049. round1a_sorted.inserted -= num_final_round1a;
  1050. #ifdef TRACE_ROUTING
  1051. show_messages("In round 1c (after setting aside)",
  1052. round1a_sorted.buf, round1a_sorted.inserted);
  1053. show_messages("In round 1c (the aside)", round1b_next.buf,
  1054. round1b_next.inserted);
  1055. #endif
  1056. // sort and append to round1a msgs
  1057. uint32_t num_copy = round1b_next.inserted;
  1058. #ifdef PROFILE_ROUTING
  1059. unsigned long start_sort =
  1060. printf_with_rtclock("begin round1b_next oblivious sort (%u,%u)\n",
  1061. num_copy, 2*max_round1b_msgs_to_adj_rtr);
  1062. #endif
  1063. round1a_sorted.reserved += num_copy;
  1064. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads,
  1065. round1b_next.buf, msg_size, num_copy,
  1066. 2*max_round1b_msgs_to_adj_rtr,
  1067. [&](const uint8_t *src, const UidPriorityKey *indices, uint32_t Nr) {
  1068. return copy_msgs(round1a_sorted.buf +
  1069. round1a_sorted.inserted * msg_size, 0, num_copy,
  1070. src, indices);
  1071. }
  1072. );
  1073. round1a_sorted.inserted += num_copy;
  1074. #ifdef PROFILE_ROUTING
  1075. printf_with_rtclock_diff(start_sort,
  1076. "end round1b_next oblivious sort (%u,%u)\n",
  1077. num_copy, 2*max_round1b_msgs_to_adj_rtr);
  1078. #endif
  1079. }
  1080. #ifdef TRACE_ROUTING
  1081. printf("round1a_sorted.inserted = %lu, reserved = %lu, bufsize = %lu\n",
  1082. round1a_sorted.inserted, round1a_sorted.reserved,
  1083. round1a_sorted.bufsize);
  1084. show_messages("In round 1c", round1a_sorted.buf,
  1085. round1a_sorted.inserted);
  1086. #endif
  1087. send_round_robin_msgs<UidPriorityKey>(route_state.round1c,
  1088. round1a_sorted.buf, NULL, round1a_sorted.inserted);
  1089. #ifdef PROFILE_ROUTING
  1090. printf_with_rtclock_diff(start_round1c, "end round1c processing (%u)\n",
  1091. round1a.inserted);
  1092. #endif
  1093. round1a.reset();
  1094. round1a_sorted.reset();
  1095. round1b_next.reset();
  1096. pthread_mutex_unlock(&round1a_sorted.mutex);
  1097. pthread_mutex_unlock(&round1a.mutex);
  1098. pthread_mutex_unlock(&round1b_next.mutex);
  1099. MsgBuffer &round1c = route_state.round1c;
  1100. pthread_mutex_lock(&round1c.mutex);
  1101. round1c.completed_prev_round = true;
  1102. nodenum_t nodes_received = round1c.nodes_received;
  1103. pthread_mutex_unlock(&round1c.mutex);
  1104. if (nodes_received == num_routing_nodes) {
  1105. route_state.step = ROUTE_ROUND_1C;
  1106. route_state.cbpointer = NULL;
  1107. ocall_routing_round_complete(cbpointer, ROUND_1C);
  1108. }
  1109. } else {
  1110. route_state.step = ROUTE_ROUND_1C;
  1111. route_state.round1c.completed_prev_round = true;
  1112. ocall_routing_round_complete(cbpointer, ROUND_1C);
  1113. }
  1114. }
  1115. // Process messages in round 2
  1116. static void round2_processing(uint8_t my_roles, void *cbpointer, MsgBuffer &prevround) {
  1117. if (my_roles & ROLE_ROUTING) {
  1118. route_state.cbpointer = cbpointer;
  1119. pthread_mutex_lock(&prevround.mutex);
  1120. // Ensure there are no pending messages currently being inserted
  1121. // into the buffer
  1122. while (prevround.reserved != prevround.inserted) {
  1123. pthread_mutex_unlock(&prevround.mutex);
  1124. pthread_mutex_lock(&prevround.mutex);
  1125. }
  1126. // If the _total_ number of messages we received in round 1
  1127. // is less than the max number of messages we could send to
  1128. // _each_ storage node, then cap the number of messages we
  1129. // will send to each storage node to that number.
  1130. uint32_t msgs_per_stg = route_state.max_msg_to_each_stg;
  1131. if (prevround.inserted < msgs_per_stg) {
  1132. msgs_per_stg = prevround.inserted;
  1133. }
  1134. // Note: at this point, it is required that each message in
  1135. // the prevround buffer have a _valid_ storage node id field.
  1136. // Obliviously tally the number of messages we received in
  1137. // the previous round destined for each storage node
  1138. #ifdef TRACE_ROUTING
  1139. show_messages("Start of round 2", prevround.buf, prevround.inserted);
  1140. #endif
  1141. #ifdef PROFILE_ROUTING
  1142. unsigned long start_round2 = printf_with_rtclock("begin round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1143. unsigned long start_tally = printf_with_rtclock("begin tally (%u)\n", prevround.inserted);
  1144. #endif
  1145. uint16_t msg_size = g_teems_config.msg_size;
  1146. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  1147. std::vector<uint32_t> tally = obliv_tally_stg(
  1148. prevround.buf, msg_size, prevround.inserted, num_storage_nodes);
  1149. #ifdef PROFILE_ROUTING
  1150. printf_with_rtclock_diff(start_tally, "end tally (%u)\n", prevround.inserted);
  1151. #endif
  1152. // Note: tally contains private values! It's OK to
  1153. // non-obliviously check for an error condition, though.
  1154. // While we're at it, obliviously change the tally of
  1155. // messages received to a tally of padding messages
  1156. // required.
  1157. uint32_t tot_padding = 0;
  1158. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  1159. if (tally[i] > msgs_per_stg) {
  1160. printf("Received too many messages for storage node %u\n", i);
  1161. assert(tally[i] <= msgs_per_stg);
  1162. }
  1163. tally[i] = msgs_per_stg - tally[i];
  1164. tot_padding += tally[i];
  1165. }
  1166. prevround.reserved += tot_padding;
  1167. assert(prevround.reserved <= prevround.bufsize);
  1168. // Obliviously add padding for each storage node according
  1169. // to the (private) padding tally.
  1170. #ifdef PROFILE_ROUTING
  1171. unsigned long start_pad = printf_with_rtclock("begin pad (%u)\n", tot_padding);
  1172. #endif
  1173. obliv_pad_stg(prevround.buf + prevround.inserted * msg_size,
  1174. msg_size, tally, tot_padding);
  1175. #ifdef PROFILE_ROUTING
  1176. printf_with_rtclock_diff(start_pad, "end pad (%u)\n", tot_padding);
  1177. #endif
  1178. prevround.inserted += tot_padding;
  1179. // Obliviously shuffle the messages
  1180. #ifdef PROFILE_ROUTING
  1181. unsigned long start_shuffle = printf_with_rtclock("begin shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1182. #endif
  1183. uint32_t num_shuffled = shuffle_mtobliv(g_teems_config.nthreads,
  1184. prevround.buf, msg_size, prevround.inserted, prevround.bufsize);
  1185. #ifdef PROFILE_ROUTING
  1186. printf_with_rtclock_diff(start_shuffle, "end shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1187. printf_with_rtclock_diff(start_round2, "end round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1188. #endif
  1189. // Now we can handle the messages non-obliviously, since we
  1190. // know there will be exactly msgs_per_stg messages to each
  1191. // storage node, and the oblivious shuffle broke the
  1192. // connection between where each message came from and where
  1193. // it's going.
  1194. send_round2_msgs(num_shuffled, msgs_per_stg, prevround);
  1195. prevround.reset();
  1196. pthread_mutex_unlock(&prevround.mutex);
  1197. }
  1198. if (my_roles & ROLE_STORAGE) {
  1199. route_state.cbpointer = cbpointer;
  1200. MsgBuffer &round2 = route_state.round2;
  1201. pthread_mutex_lock(&round2.mutex);
  1202. round2.completed_prev_round = true;
  1203. nodenum_t nodes_received = round2.nodes_received;
  1204. pthread_mutex_unlock(&round2.mutex);
  1205. if (nodes_received == g_teems_config.num_routing_nodes) {
  1206. route_state.step = ROUTE_ROUND_2;
  1207. route_state.cbpointer = NULL;
  1208. ocall_routing_round_complete(cbpointer, 2);
  1209. }
  1210. } else {
  1211. route_state.step = ROUTE_ROUND_2;
  1212. route_state.round2.completed_prev_round = true;
  1213. ocall_routing_round_complete(cbpointer, 2);
  1214. }
  1215. }
  1216. // Perform the next round of routing. The callback pointer will be
  1217. // passed to ocall_routing_round_complete when the round is complete.
  1218. void ecall_routing_proceed(void *cbpointer)
  1219. {
  1220. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  1221. if (route_state.step == ROUTE_NOT_STARTED) {
  1222. if (my_roles & ROLE_INGESTION) {
  1223. ingestion_epoch++;
  1224. route_state.cbpointer = cbpointer;
  1225. MsgBuffer &ingbuf = route_state.ingbuf;
  1226. pthread_mutex_lock(&ingbuf.mutex);
  1227. // Ensure there are no pending messages currently being inserted
  1228. // into the buffer
  1229. while (ingbuf.reserved != ingbuf.inserted) {
  1230. pthread_mutex_unlock(&ingbuf.mutex);
  1231. pthread_mutex_lock(&ingbuf.mutex);
  1232. }
  1233. // Sort the messages we've received
  1234. #ifdef TRACE_ROUTING
  1235. show_messages("Start of round 1", ingbuf.buf, ingbuf.inserted);
  1236. #endif
  1237. #ifdef PROFILE_ROUTING
  1238. uint32_t inserted = ingbuf.inserted;
  1239. unsigned long start_round1 =
  1240. printf_with_rtclock("begin round1 processing (%u)\n", inserted);
  1241. unsigned long start_sort =
  1242. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  1243. route_state.tot_msg_per_ing);
  1244. #endif
  1245. if (g_teems_config.private_routing) {
  1246. sort_mtobliv<UidKey>(g_teems_config.nthreads, ingbuf.buf,
  1247. g_teems_config.msg_size, ingbuf.inserted,
  1248. route_state.tot_msg_per_ing,
  1249. [&](const uint8_t *msgs, const UidKey *indices, uint32_t N) {
  1250. send_round_robin_msgs<UidKey>(route_state.round1,
  1251. msgs, indices, N);
  1252. });
  1253. } else {
  1254. // Sort received messages by increasing user ID and
  1255. // priority. Larger priority number indicates higher priority.
  1256. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, ingbuf.buf,
  1257. g_teems_config.msg_size, ingbuf.inserted,
  1258. route_state.tot_msg_per_ing,
  1259. [&](const uint8_t *msgs,
  1260. const UidPriorityKey *indices, uint32_t N) {
  1261. send_round_robin_msgs<UidPriorityKey>(route_state.round1,
  1262. msgs, indices, N);
  1263. });
  1264. }
  1265. #ifdef PROFILE_ROUTING
  1266. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  1267. inserted, route_state.tot_msg_per_ing);
  1268. printf_with_rtclock_diff(start_round1, "end round1 processing (%u)\n",
  1269. inserted);
  1270. #endif
  1271. ingbuf.reset();
  1272. pthread_mutex_unlock(&ingbuf.mutex);
  1273. }
  1274. if (my_roles & ROLE_ROUTING) {
  1275. MsgBuffer &round1 = route_state.round1;
  1276. pthread_mutex_lock(&round1.mutex);
  1277. round1.completed_prev_round = true;
  1278. nodenum_t nodes_received = round1.nodes_received;
  1279. pthread_mutex_unlock(&round1.mutex);
  1280. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  1281. route_state.step = ROUTE_ROUND_1;
  1282. route_state.cbpointer = NULL;
  1283. ocall_routing_round_complete(cbpointer, 1);
  1284. }
  1285. } else {
  1286. route_state.step = ROUTE_ROUND_1;
  1287. route_state.round1.completed_prev_round = true;
  1288. ocall_routing_round_complete(cbpointer, 1);
  1289. }
  1290. } else if (route_state.step == ROUTE_ROUND_1) {
  1291. if (g_teems_config.private_routing) { // private routing next round
  1292. round2_processing(my_roles, cbpointer, route_state.round1);
  1293. } else { // public routing next round
  1294. round1a_processing(cbpointer);
  1295. }
  1296. } else if (route_state.step == ROUTE_ROUND_1A) {
  1297. round1b_processing(cbpointer);
  1298. } else if (route_state.step == ROUTE_ROUND_1B) {
  1299. round1c_processing(cbpointer);
  1300. } else if (route_state.step == ROUTE_ROUND_1C) {
  1301. round2_processing(my_roles, cbpointer, route_state.round1c);
  1302. } else if (route_state.step == ROUTE_ROUND_2) {
  1303. if (my_roles & ROLE_STORAGE) {
  1304. MsgBuffer &round2 = route_state.round2;
  1305. pthread_mutex_lock(&round2.mutex);
  1306. // Ensure there are no pending messages currently being inserted
  1307. // into the buffer
  1308. while (round2.reserved != round2.inserted) {
  1309. pthread_mutex_unlock(&round2.mutex);
  1310. pthread_mutex_lock(&round2.mutex);
  1311. }
  1312. unsigned long start = printf_with_rtclock("begin storage processing (%u)\n", round2.inserted);
  1313. storage_received(round2);
  1314. printf_with_rtclock_diff(start, "end storage processing (%u)\n", round2.inserted);
  1315. // We're done
  1316. route_state.step = ROUTE_NOT_STARTED;
  1317. ocall_routing_round_complete(cbpointer, 0);
  1318. } else {
  1319. // We're done
  1320. route_state.step = ROUTE_NOT_STARTED;
  1321. ocall_routing_round_complete(cbpointer, 0);
  1322. }
  1323. }
  1324. }