route.cpp 61 KB

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