route.cpp 60 KB

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