route.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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 before padding pass", round1a_sorted.buf,
  1085. round1a_sorted.inserted);
  1086. #endif
  1087. // The round1a_sorted buffer is now sorted by receiver uid and
  1088. // priority. Going from the end of the buffer to the beginning
  1089. // (so as to encounter and keep the highest-priority messages
  1090. // for any given receiver first), obliviously turn any messages
  1091. // over the limit of pub_in for any given receiver into padding.
  1092. // Also keep track of which messages are not padding for use in
  1093. // later compaction.
  1094. bool *is_not_padding = new bool[round1a_sorted.inserted];
  1095. for (uint32_t i=0; i<round1a_sorted.inserted; ++i) {
  1096. uint8_t *header = round1a_sorted.buf +
  1097. msg_size * (round1a_sorted.inserted - 1 - i);
  1098. uint32_t receiver_id = *(uint32_t*)header;
  1099. uint32_t pub_in = uint32_t(g_teems_config.m_pub_in);
  1100. // These are the possible cases and what we need to do in
  1101. // each case, but we have to evaluate them obliviously
  1102. // receiver_id != next_receiver_id:
  1103. // next_receiver_id = receiver_id
  1104. // next_rid_count = 1
  1105. // become_padding = 0
  1106. // receiver_id == next_receiver_id && next_rid_count < pub_in:
  1107. // next_receiver_id = receiver_id
  1108. // next_rid_count = next_rid_count + 1
  1109. // become_padding = 0
  1110. // receiver_id == next_receiver_id && next_rid_count >= pub_in:
  1111. // next_receiver_id = receiver_id
  1112. // next_rid_count = next_rid_count
  1113. // become_padding = 1
  1114. bool same_receiver_id = (receiver_id == next_receiver_id);
  1115. // If same_receiver_id is 0, reset next_rid_count to 0.
  1116. // If same_receiver_id is 1, don't change next_rid_count.
  1117. // This method (AND with -same_receiver_id) is more likely
  1118. // to be constant time than multiplying by same_receiver_id.
  1119. next_rid_count &= (-(uint32_t(same_receiver_id)));
  1120. bool become_padding = (next_rid_count >= pub_in);
  1121. next_rid_count += !become_padding;
  1122. next_receiver_id = receiver_id;
  1123. // Obliviously change the receiver id to 0xffffffff
  1124. // (padding) if become-padding is 1
  1125. receiver_id |= (-(uint32_t(become_padding)));
  1126. *(uint32_t*)header = receiver_id;
  1127. is_not_padding[round1a_sorted.inserted - 1 - i] =
  1128. (receiver_id != 0xffffffff);
  1129. }
  1130. #ifdef TRACE_ROUTING
  1131. show_messages("In round 1c after padding pass", round1a_sorted.buf,
  1132. round1a_sorted.inserted);
  1133. #endif
  1134. // Oblivious compaction to move the padding messages to the end,
  1135. // preserving the (already-sorted) order of the non-padding
  1136. // messages
  1137. TightCompact_parallel<OSWAP_16X>(
  1138. (unsigned char *) round1a_sorted.buf,
  1139. round1a_sorted.inserted, msg_size, is_not_padding,
  1140. g_teems_config.nthreads);
  1141. delete[] is_not_padding;
  1142. #ifdef TRACE_ROUTING
  1143. show_messages("In round 1c after compaction", round1a_sorted.buf,
  1144. round1a_sorted.inserted);
  1145. #endif
  1146. send_round_robin_msgs<UidPriorityKey>(route_state.round1c,
  1147. round1a_sorted.buf, NULL, round1a_sorted.inserted);
  1148. #ifdef PROFILE_ROUTING
  1149. printf_with_rtclock_diff(start_round1c, "end round1c processing (%u)\n",
  1150. round1a.inserted);
  1151. #endif
  1152. round1a.reset();
  1153. round1a_sorted.reset();
  1154. round1b_next.reset();
  1155. pthread_mutex_unlock(&round1a_sorted.mutex);
  1156. pthread_mutex_unlock(&round1a.mutex);
  1157. pthread_mutex_unlock(&round1b_next.mutex);
  1158. MsgBuffer &round1c = route_state.round1c;
  1159. pthread_mutex_lock(&round1c.mutex);
  1160. round1c.completed_prev_round = true;
  1161. nodenum_t nodes_received = round1c.nodes_received;
  1162. pthread_mutex_unlock(&round1c.mutex);
  1163. if (nodes_received == num_routing_nodes) {
  1164. route_state.step = ROUTE_ROUND_1C;
  1165. route_state.cbpointer = NULL;
  1166. ocall_routing_round_complete(cbpointer, ROUND_1C);
  1167. }
  1168. } else {
  1169. route_state.step = ROUTE_ROUND_1C;
  1170. route_state.round1c.completed_prev_round = true;
  1171. ocall_routing_round_complete(cbpointer, ROUND_1C);
  1172. }
  1173. }
  1174. // Process messages in round 2
  1175. static void round2_processing(uint8_t my_roles, void *cbpointer, MsgBuffer &prevround) {
  1176. if (my_roles & ROLE_ROUTING) {
  1177. route_state.cbpointer = cbpointer;
  1178. pthread_mutex_lock(&prevround.mutex);
  1179. // Ensure there are no pending messages currently being inserted
  1180. // into the buffer
  1181. while (prevround.reserved != prevround.inserted) {
  1182. pthread_mutex_unlock(&prevround.mutex);
  1183. pthread_mutex_lock(&prevround.mutex);
  1184. }
  1185. // If the _total_ number of messages we received in round 1
  1186. // is less than the max number of messages we could send to
  1187. // _each_ storage node, then cap the number of messages we
  1188. // will send to each storage node to that number.
  1189. uint32_t msgs_per_stg = route_state.max_msg_to_each_stg;
  1190. if (prevround.inserted < msgs_per_stg) {
  1191. msgs_per_stg = prevround.inserted;
  1192. }
  1193. // Note: at this point, it is required that each message in
  1194. // the prevround buffer have a _valid_ storage node id field.
  1195. // Obliviously tally the number of messages we received in
  1196. // the previous round destined for each storage node
  1197. #ifdef TRACE_ROUTING
  1198. show_messages("Start of round 2", prevround.buf, prevround.inserted);
  1199. #endif
  1200. #ifdef PROFILE_ROUTING
  1201. unsigned long start_round2 = printf_with_rtclock("begin round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1202. unsigned long start_tally = printf_with_rtclock("begin tally (%u)\n", prevround.inserted);
  1203. #endif
  1204. uint16_t msg_size = g_teems_config.msg_size;
  1205. nodenum_t num_storage_nodes = g_teems_config.num_storage_nodes;
  1206. std::vector<uint32_t> tally = obliv_tally_stg(
  1207. prevround.buf, msg_size, prevround.inserted, num_storage_nodes);
  1208. #ifdef PROFILE_ROUTING
  1209. printf_with_rtclock_diff(start_tally, "end tally (%u)\n", prevround.inserted);
  1210. #endif
  1211. // Note: tally contains private values! It's OK to
  1212. // non-obliviously check for an error condition, though.
  1213. // While we're at it, obliviously change the tally of
  1214. // messages received to a tally of padding messages
  1215. // required.
  1216. uint32_t tot_padding = 0;
  1217. for (nodenum_t i=0; i<num_storage_nodes; ++i) {
  1218. if (tally[i] > msgs_per_stg) {
  1219. printf("Received too many messages for storage node %u\n", i);
  1220. assert(tally[i] <= msgs_per_stg);
  1221. }
  1222. tally[i] = msgs_per_stg - tally[i];
  1223. tot_padding += tally[i];
  1224. }
  1225. prevround.reserved += tot_padding;
  1226. assert(prevround.reserved <= prevround.bufsize);
  1227. // Obliviously add padding for each storage node according
  1228. // to the (private) padding tally.
  1229. #ifdef PROFILE_ROUTING
  1230. unsigned long start_pad = printf_with_rtclock("begin pad (%u)\n", tot_padding);
  1231. #endif
  1232. obliv_pad_stg(prevround.buf + prevround.inserted * msg_size,
  1233. msg_size, tally, tot_padding);
  1234. #ifdef PROFILE_ROUTING
  1235. printf_with_rtclock_diff(start_pad, "end pad (%u)\n", tot_padding);
  1236. #endif
  1237. prevround.inserted += tot_padding;
  1238. // Obliviously shuffle the messages
  1239. #ifdef PROFILE_ROUTING
  1240. unsigned long start_shuffle = printf_with_rtclock("begin shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1241. #endif
  1242. uint32_t num_shuffled = shuffle_mtobliv(g_teems_config.nthreads,
  1243. prevround.buf, msg_size, prevround.inserted, prevround.bufsize);
  1244. #ifdef PROFILE_ROUTING
  1245. printf_with_rtclock_diff(start_shuffle, "end shuffle (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1246. printf_with_rtclock_diff(start_round2, "end round2 processing (%u,%u)\n", prevround.inserted, prevround.bufsize);
  1247. #endif
  1248. // Now we can handle the messages non-obliviously, since we
  1249. // know there will be exactly msgs_per_stg messages to each
  1250. // storage node, and the oblivious shuffle broke the
  1251. // connection between where each message came from and where
  1252. // it's going.
  1253. send_round2_msgs(num_shuffled, msgs_per_stg, prevround);
  1254. prevround.reset();
  1255. pthread_mutex_unlock(&prevround.mutex);
  1256. }
  1257. if (my_roles & ROLE_STORAGE) {
  1258. route_state.cbpointer = cbpointer;
  1259. MsgBuffer &round2 = route_state.round2;
  1260. pthread_mutex_lock(&round2.mutex);
  1261. round2.completed_prev_round = true;
  1262. nodenum_t nodes_received = round2.nodes_received;
  1263. pthread_mutex_unlock(&round2.mutex);
  1264. if (nodes_received == g_teems_config.num_routing_nodes) {
  1265. route_state.step = ROUTE_ROUND_2;
  1266. route_state.cbpointer = NULL;
  1267. ocall_routing_round_complete(cbpointer, 2);
  1268. }
  1269. } else {
  1270. route_state.step = ROUTE_ROUND_2;
  1271. route_state.round2.completed_prev_round = true;
  1272. ocall_routing_round_complete(cbpointer, 2);
  1273. }
  1274. }
  1275. // Perform the next round of routing. The callback pointer will be
  1276. // passed to ocall_routing_round_complete when the round is complete.
  1277. void ecall_routing_proceed(void *cbpointer)
  1278. {
  1279. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  1280. if (route_state.step == ROUTE_NOT_STARTED) {
  1281. if (my_roles & ROLE_INGESTION) {
  1282. ingestion_epoch++;
  1283. route_state.cbpointer = cbpointer;
  1284. MsgBuffer &ingbuf = route_state.ingbuf;
  1285. pthread_mutex_lock(&ingbuf.mutex);
  1286. // Ensure there are no pending messages currently being inserted
  1287. // into the buffer
  1288. while (ingbuf.reserved != ingbuf.inserted) {
  1289. pthread_mutex_unlock(&ingbuf.mutex);
  1290. pthread_mutex_lock(&ingbuf.mutex);
  1291. }
  1292. // Sort the messages we've received
  1293. #ifdef TRACE_ROUTING
  1294. show_messages("Start of round 1", ingbuf.buf, ingbuf.inserted);
  1295. #endif
  1296. #ifdef PROFILE_ROUTING
  1297. uint32_t inserted = ingbuf.inserted;
  1298. unsigned long start_round1 =
  1299. printf_with_rtclock("begin round1 processing (%u)\n", inserted);
  1300. unsigned long start_sort =
  1301. printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted,
  1302. route_state.tot_msg_per_ing);
  1303. #endif
  1304. if (g_teems_config.private_routing) {
  1305. sort_mtobliv<UidKey>(g_teems_config.nthreads, ingbuf.buf,
  1306. g_teems_config.msg_size, ingbuf.inserted,
  1307. route_state.tot_msg_per_ing,
  1308. [&](const uint8_t *msgs, const UidKey *indices, uint32_t N) {
  1309. send_round_robin_msgs<UidKey>(route_state.round1,
  1310. msgs, indices, N);
  1311. });
  1312. } else {
  1313. // Sort received messages by increasing user ID and
  1314. // priority. Larger priority number indicates higher priority.
  1315. sort_mtobliv<UidPriorityKey>(g_teems_config.nthreads, ingbuf.buf,
  1316. g_teems_config.msg_size, ingbuf.inserted,
  1317. route_state.tot_msg_per_ing,
  1318. [&](const uint8_t *msgs,
  1319. const UidPriorityKey *indices, uint32_t N) {
  1320. send_round_robin_msgs<UidPriorityKey>(route_state.round1,
  1321. msgs, indices, N);
  1322. });
  1323. }
  1324. #ifdef PROFILE_ROUTING
  1325. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u,%u)\n",
  1326. inserted, route_state.tot_msg_per_ing);
  1327. printf_with_rtclock_diff(start_round1, "end round1 processing (%u)\n",
  1328. inserted);
  1329. #endif
  1330. ingbuf.reset();
  1331. pthread_mutex_unlock(&ingbuf.mutex);
  1332. }
  1333. if (my_roles & ROLE_ROUTING) {
  1334. MsgBuffer &round1 = route_state.round1;
  1335. pthread_mutex_lock(&round1.mutex);
  1336. round1.completed_prev_round = true;
  1337. nodenum_t nodes_received = round1.nodes_received;
  1338. pthread_mutex_unlock(&round1.mutex);
  1339. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  1340. route_state.step = ROUTE_ROUND_1;
  1341. route_state.cbpointer = NULL;
  1342. ocall_routing_round_complete(cbpointer, 1);
  1343. }
  1344. } else {
  1345. route_state.step = ROUTE_ROUND_1;
  1346. route_state.round1.completed_prev_round = true;
  1347. ocall_routing_round_complete(cbpointer, 1);
  1348. }
  1349. } else if (route_state.step == ROUTE_ROUND_1) {
  1350. if (g_teems_config.private_routing) { // private routing next round
  1351. round2_processing(my_roles, cbpointer, route_state.round1);
  1352. } else { // public routing next round
  1353. round1a_processing(cbpointer);
  1354. }
  1355. } else if (route_state.step == ROUTE_ROUND_1A) {
  1356. round1b_processing(cbpointer);
  1357. } else if (route_state.step == ROUTE_ROUND_1B) {
  1358. round1c_processing(cbpointer);
  1359. } else if (route_state.step == ROUTE_ROUND_1C) {
  1360. round2_processing(my_roles, cbpointer, route_state.round1c);
  1361. } else if (route_state.step == ROUTE_ROUND_2) {
  1362. if (my_roles & ROLE_STORAGE) {
  1363. MsgBuffer &round2 = route_state.round2;
  1364. pthread_mutex_lock(&round2.mutex);
  1365. // Ensure there are no pending messages currently being inserted
  1366. // into the buffer
  1367. while (round2.reserved != round2.inserted) {
  1368. pthread_mutex_unlock(&round2.mutex);
  1369. pthread_mutex_lock(&round2.mutex);
  1370. }
  1371. unsigned long start = printf_with_rtclock("begin storage processing (%u)\n", round2.inserted);
  1372. storage_received(round2);
  1373. printf_with_rtclock_diff(start, "end storage processing (%u)\n", round2.inserted);
  1374. // We're done
  1375. route_state.step = ROUTE_NOT_STARTED;
  1376. ocall_routing_round_complete(cbpointer, 0);
  1377. } else {
  1378. // We're done
  1379. route_state.step = ROUTE_NOT_STARTED;
  1380. ocall_routing_round_complete(cbpointer, 0);
  1381. }
  1382. }
  1383. }