route.cpp 53 KB

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