route.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #include <pthread.h>
  2. #include "Enclave_t.h"
  3. #include "config.hpp"
  4. #include "utils.hpp"
  5. #include "sort.hpp"
  6. #include "comms.hpp"
  7. #include "route.hpp"
  8. #define PROFILE_ROUTING
  9. struct MsgBuffer {
  10. pthread_mutex_t mutex;
  11. uint8_t *buf;
  12. // The number of messages (not bytes) in, or on their way into, the
  13. // buffer
  14. uint32_t reserved;
  15. // The number of messages definitely in the buffer
  16. uint32_t inserted;
  17. // The number of messages that can fit in buf
  18. uint32_t bufsize;
  19. // The number of nodes we've heard from
  20. nodenum_t nodes_received;
  21. MsgBuffer() : buf(NULL), reserved(0), inserted(0), bufsize(0),
  22. nodes_received(0) {
  23. pthread_mutex_init(&mutex, NULL);
  24. }
  25. ~MsgBuffer() {
  26. delete[] buf;
  27. }
  28. // The number passed is messages, not bytes
  29. void alloc(uint32_t msgs) {
  30. delete[] buf;
  31. buf = NULL;
  32. reserved = 0;
  33. inserted = 0;
  34. // This may throw bad_alloc, but we'll catch it higher up
  35. buf = new uint8_t[size_t(msgs) * g_teems_config.msg_size];
  36. memset(buf, 0, size_t(msgs) * g_teems_config.msg_size);
  37. bufsize = msgs;
  38. nodes_received = 0;
  39. }
  40. // Reset the contents of the buffer
  41. void reset() {
  42. memset(buf, 0, bufsize * g_teems_config.msg_size);
  43. reserved = 0;
  44. inserted = 0;
  45. nodes_received = 0;
  46. }
  47. // You can't copy a MsgBuffer
  48. MsgBuffer(const MsgBuffer&) = delete;
  49. MsgBuffer &operator=(const MsgBuffer&) = delete;
  50. };
  51. enum RouteStep {
  52. ROUTE_NOT_STARTED,
  53. ROUTE_ROUND_1,
  54. ROUTE_ROUND_2
  55. };
  56. // The ingbuf MsgBuffer stores messages an ingestion node ingests while
  57. // waiting for round 1 to start, which will be sorted and sent out in
  58. // round 1. The round1 MsgBuffer stores messages a routing node
  59. // receives in round 1, which will be padded, sorted, and sent out in
  60. // round 2. The round2 MsgBuffer stores messages a storage node
  61. // receives in round 2.
  62. static struct RouteState {
  63. MsgBuffer ingbuf;
  64. MsgBuffer round1;
  65. MsgBuffer round2;
  66. RouteStep step;
  67. uint32_t tot_msg_per_ing;
  68. uint32_t max_msg_to_each_str;
  69. uint32_t max_round2_msgs;
  70. void *cbpointer;
  71. } route_state;
  72. // Computes ceil(x/y) where x and y are integers, x>=0, y>0.
  73. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  74. // Call this near the end of ecall_config_load, but before
  75. // comms_init_nodestate. Returns true on success, false on failure.
  76. bool route_init()
  77. {
  78. // Compute the maximum number of messages we could receive by direct
  79. // ingestion
  80. // Each ingestion node will have at most
  81. // ceil(user_count/num_ingestion_nodes) users, and each user will
  82. // send at most m_priv_out messages.
  83. uint32_t users_per_ing = CEILDIV(g_teems_config.user_count,
  84. g_teems_config.num_ingestion_nodes);
  85. uint32_t tot_msg_per_ing = users_per_ing * g_teems_config.m_priv_out;
  86. // Compute the maximum number of messages we could receive in round 1
  87. // Each ingestion node will send us an our_weight/tot_weight
  88. // fraction of the messages they hold
  89. uint32_t max_msg_from_each_ing = CEILDIV(tot_msg_per_ing,
  90. g_teems_config.tot_weight) * g_teems_config.my_weight;
  91. // And the maximum number we can receive in total is that times the
  92. // number of ingestion nodes
  93. uint32_t max_round1_msgs = max_msg_from_each_ing *
  94. g_teems_config.num_ingestion_nodes;
  95. // Compute the maximum number of messages we could send in round 2
  96. // Each storage node has at most this many users
  97. uint32_t users_per_str = CEILDIV(g_teems_config.user_count,
  98. g_teems_config.num_storage_nodes);
  99. // And so can receive at most this many messages
  100. uint32_t tot_msg_per_str = users_per_str *
  101. g_teems_config.m_priv_in;
  102. // Which will be at most this many from us
  103. uint32_t max_msg_to_each_str = CEILDIV(tot_msg_per_str,
  104. g_teems_config.tot_weight) * g_teems_config.my_weight;
  105. // But we can't send more messages to each storage server than we
  106. // could receive in total
  107. if (max_msg_to_each_str > max_round1_msgs) {
  108. max_msg_to_each_str = max_round1_msgs;
  109. }
  110. // And the max total number of outgoing messages in round 2 is then
  111. uint32_t max_round2_msgs = max_msg_to_each_str *
  112. g_teems_config.num_storage_nodes;
  113. // In case we have a weird configuration where users can send more
  114. // messages per epoch than they can receive, ensure the round 2
  115. // buffer is large enough to hold the incoming messages as well
  116. if (max_round2_msgs < max_round1_msgs) {
  117. max_round2_msgs = max_round1_msgs;
  118. }
  119. /*
  120. printf("round1_msgs = %u, round2_msgs = %u\n",
  121. max_round1_msgs, max_round2_msgs);
  122. */
  123. // Create the route state
  124. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  125. try {
  126. if (my_roles & ROLE_INGESTION) {
  127. route_state.ingbuf.alloc(tot_msg_per_ing);
  128. }
  129. if (my_roles & ROLE_ROUTING) {
  130. route_state.round1.alloc(max_round2_msgs);
  131. }
  132. if (my_roles & ROLE_STORAGE) {
  133. route_state.round2.alloc(tot_msg_per_str +
  134. g_teems_config.tot_weight);
  135. }
  136. } catch (std::bad_alloc&) {
  137. printf("Memory allocation failed in route_init\n");
  138. return false;
  139. }
  140. route_state.step = ROUTE_NOT_STARTED;
  141. route_state.tot_msg_per_ing = tot_msg_per_ing;
  142. route_state.max_msg_to_each_str = max_msg_to_each_str;
  143. route_state.max_round2_msgs = max_round2_msgs;
  144. route_state.cbpointer = NULL;
  145. threadid_t nthreads = g_teems_config.nthreads;
  146. #ifdef PROFILE_ROUTING
  147. unsigned long start = printf_with_rtclock("begin precompute evalplans (%u,%hu) (%u,%hu)\n", tot_msg_per_ing, nthreads, max_round2_msgs, nthreads);
  148. #endif
  149. sort_precompute_evalplan(tot_msg_per_ing, nthreads);
  150. sort_precompute_evalplan(max_round2_msgs, nthreads);
  151. #ifdef PROFILE_ROUTING
  152. printf_with_rtclock_diff(start, "end precompute evalplans\n");
  153. #endif
  154. return true;
  155. }
  156. // Precompute the WaksmanNetworks needed for the sorts. If you pass -1,
  157. // it will return the number of different sizes it needs. If you pass
  158. // [0,sizes-1], it will compute one WaksmanNetwork with that size index
  159. // and return the number of available WaksmanNetworks of that size.
  160. size_t ecall_precompute_sort(int sizeidx)
  161. {
  162. size_t ret = 0;
  163. switch(sizeidx) {
  164. case 0:
  165. #ifdef PROFILE_ROUTING
  166. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.tot_msg_per_ing);
  167. #endif
  168. ret = sort_precompute(route_state.tot_msg_per_ing);
  169. #ifdef PROFILE_ROUTING
  170. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.tot_msg_per_ing);}
  171. #endif
  172. break;
  173. case 1:
  174. #ifdef PROFILE_ROUTING
  175. {unsigned long start = printf_with_rtclock("begin precompute WaksmanNetwork (%u)\n", route_state.max_round2_msgs);
  176. #endif
  177. ret = sort_precompute(route_state.max_round2_msgs);
  178. #ifdef PROFILE_ROUTING
  179. printf_with_rtclock_diff(start, "end precompute Waksman Network (%u)\n", route_state.max_round2_msgs);}
  180. #endif
  181. break;
  182. default:
  183. ret = 2;
  184. break;
  185. }
  186. return ret;
  187. }
  188. static uint8_t* msgbuffer_get_buf(MsgBuffer &msgbuf,
  189. NodeCommState &, uint32_t tot_enc_chunk_size)
  190. {
  191. uint16_t msg_size = g_teems_config.msg_size;
  192. // Chunks will be encrypted and have a MAC tag attached which will
  193. // not correspond to plaintext bytes, so we can trim them.
  194. // The minimum number of chunks needed to transmit this message
  195. uint32_t min_num_chunks =
  196. (tot_enc_chunk_size + (FRAME_SIZE-1)) / FRAME_SIZE;
  197. // The number of plaintext bytes this message could contain
  198. uint32_t plaintext_bytes = tot_enc_chunk_size -
  199. SGX_AESGCM_MAC_SIZE * min_num_chunks;
  200. assert ((plaintext_bytes % uint32_t(msg_size)) == 0);
  201. uint32_t num_msgs = plaintext_bytes/uint32_t(msg_size);
  202. pthread_mutex_lock(&msgbuf.mutex);
  203. uint32_t start = msgbuf.reserved;
  204. if (start + num_msgs > msgbuf.bufsize) {
  205. pthread_mutex_unlock(&msgbuf.mutex);
  206. printf("Max %u messages exceeded\n", msgbuf.bufsize);
  207. return NULL;
  208. }
  209. msgbuf.reserved += num_msgs;
  210. pthread_mutex_unlock(&msgbuf.mutex);
  211. return msgbuf.buf + start * msg_size;
  212. }
  213. // A round 1 message was received by a routing node from an ingestion
  214. // node; we put it into the round 2 buffer for processing in round 2
  215. static void round1_received(NodeCommState &nodest,
  216. uint8_t *data, uint32_t plaintext_len, uint32_t)
  217. {
  218. uint16_t msg_size = g_teems_config.msg_size;
  219. assert((plaintext_len % uint32_t(msg_size)) == 0);
  220. uint32_t num_msgs = plaintext_len / uint32_t(msg_size);
  221. pthread_mutex_lock(&route_state.round1.mutex);
  222. route_state.round1.inserted += num_msgs;
  223. route_state.round1.nodes_received += 1;
  224. nodenum_t nodes_received = route_state.round1.nodes_received;
  225. pthread_mutex_unlock(&route_state.round1.mutex);
  226. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  227. route_state.step = ROUTE_ROUND_1;
  228. void *cbpointer = route_state.cbpointer;
  229. route_state.cbpointer = NULL;
  230. ocall_routing_round_complete(cbpointer, 1);
  231. }
  232. }
  233. // A round 2 message was received by a storage node from a routing node
  234. static void round2_received(NodeCommState &nodest,
  235. uint8_t *data, uint32_t plaintext_len, uint32_t)
  236. {
  237. uint16_t msg_size = g_teems_config.msg_size;
  238. assert((plaintext_len % uint32_t(msg_size)) == 0);
  239. }
  240. // For a given other node, set the received message handler to the first
  241. // message we would expect from them, given their roles and our roles.
  242. void route_init_msg_handler(nodenum_t node_num)
  243. {
  244. // Our roles and their roles
  245. uint8_t our_roles = g_teems_config.roles[g_teems_config.my_node_num];
  246. uint8_t their_roles = g_teems_config.roles[node_num];
  247. // The node communication state
  248. NodeCommState &nodest = g_commstates[node_num];
  249. // If we are a routing node (possibly among other roles) and they
  250. // are an ingestion node (possibly among other roles), a round 1
  251. // routing message is the first thing we expect from them. We put
  252. // these messages into the round1 buffer for processing.
  253. if ((our_roles & ROLE_ROUTING) && (their_roles & ROLE_INGESTION)) {
  254. nodest.in_msg_get_buf = [&](NodeCommState &commst,
  255. uint32_t tot_enc_chunk_size) {
  256. return msgbuffer_get_buf(route_state.round1, commst,
  257. tot_enc_chunk_size);
  258. };
  259. nodest.in_msg_received = round1_received;
  260. }
  261. // Otherwise, if we are a storage node (possibly among other roles)
  262. // and they are a routing node (possibly among other roles), a round
  263. // 2 routing message is the first thing we expect from them
  264. else if ((our_roles & ROLE_STORAGE) && (their_roles & ROLE_ROUTING)) {
  265. nodest.in_msg_get_buf = default_in_msg_get_buf;
  266. nodest.in_msg_received = round2_received;
  267. }
  268. // Otherwise, we don't expect a message from this node. Set the
  269. // unknown message handler.
  270. else {
  271. nodest.in_msg_get_buf = default_in_msg_get_buf;
  272. nodest.in_msg_received = unknown_in_msg_received;
  273. }
  274. }
  275. // Directly ingest a buffer of num_msgs messages into the ingbuf buffer.
  276. // Return true on success, false on failure.
  277. bool ecall_ingest_raw(uint8_t *msgs, uint32_t num_msgs)
  278. {
  279. uint16_t msg_size = g_teems_config.msg_size;
  280. MsgBuffer &ingbuf = route_state.ingbuf;
  281. pthread_mutex_lock(&ingbuf.mutex);
  282. uint32_t start = ingbuf.reserved;
  283. if (start + num_msgs > route_state.tot_msg_per_ing) {
  284. pthread_mutex_unlock(&ingbuf.mutex);
  285. printf("Max %u messages exceeded\n",
  286. route_state.tot_msg_per_ing);
  287. return false;
  288. }
  289. ingbuf.reserved += num_msgs;
  290. pthread_mutex_unlock(&ingbuf.mutex);
  291. memmove(ingbuf.buf + start * msg_size,
  292. msgs, num_msgs * msg_size);
  293. pthread_mutex_lock(&ingbuf.mutex);
  294. ingbuf.inserted += num_msgs;
  295. pthread_mutex_unlock(&ingbuf.mutex);
  296. return true;
  297. }
  298. // Send the round 1 messages. Note that N here is not private.
  299. static void send_round1_msgs(const uint8_t *msgs, const uint64_t *indices,
  300. uint32_t N)
  301. {
  302. uint16_t msg_size = g_teems_config.msg_size;
  303. uint16_t tot_weight = g_teems_config.tot_weight;
  304. nodenum_t my_node_num = g_teems_config.my_node_num;
  305. uint32_t full_rows = N / uint32_t(tot_weight);
  306. uint32_t last_row = N % uint32_t(tot_weight);
  307. for (auto &routing_node: g_teems_config.routing_nodes) {
  308. uint8_t weight =
  309. g_teems_config.weights[routing_node].weight;
  310. if (weight == 0) {
  311. // This shouldn't happen, but just in case
  312. continue;
  313. }
  314. uint16_t start_weight =
  315. g_teems_config.weights[routing_node].startweight;
  316. // The number of messages headed for this routing node from the
  317. // full rows
  318. uint32_t num_msgs_full_rows = full_rows * uint32_t(weight);
  319. // The number of messages headed for this routing node from the
  320. // incomplete last row is:
  321. // 0 if last_row < start_weight
  322. // last_row-start_weight if start_weight <= last_row < start_weight + weight
  323. // weight if start_weight + weight <= last_row
  324. uint32_t num_msgs_last_row = 0;
  325. if (start_weight <= last_row && last_row < start_weight + weight) {
  326. num_msgs_last_row = last_row-start_weight;
  327. } else if (start_weight + weight <= last_row) {
  328. num_msgs_last_row = weight;
  329. }
  330. // The total number of messages headed for this routing node
  331. uint32_t num_msgs = num_msgs_full_rows + num_msgs_last_row;
  332. if (routing_node == my_node_num) {
  333. // Special case: we're sending to ourselves; just put the
  334. // messages in our own round1 buffer
  335. MsgBuffer &round1 = route_state.round1;
  336. pthread_mutex_lock(&round1.mutex);
  337. uint32_t start = round1.reserved;
  338. if (start + num_msgs > round1.bufsize) {
  339. pthread_mutex_unlock(&round1.mutex);
  340. printf("Max %u messages exceeded\n", round1.bufsize);
  341. return;
  342. }
  343. round1.reserved += num_msgs;
  344. pthread_mutex_unlock(&round1.mutex);
  345. uint8_t *buf = round1.buf + start * msg_size;
  346. for (uint32_t i=0; i<full_rows; ++i) {
  347. const uint64_t *idxp = indices + i*tot_weight + start_weight;
  348. for (uint32_t j=0; j<weight; ++j) {
  349. memmove(buf, msgs + idxp[j]*msg_size, msg_size);
  350. buf += msg_size;
  351. }
  352. }
  353. const uint64_t *idxp = indices + full_rows*tot_weight + start_weight;
  354. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  355. memmove(buf, msgs + idxp[j]*msg_size, msg_size);
  356. buf += msg_size;
  357. }
  358. pthread_mutex_lock(&round1.mutex);
  359. round1.inserted += num_msgs;
  360. round1.nodes_received += 1;
  361. nodenum_t nodes_received = round1.nodes_received;
  362. pthread_mutex_unlock(&round1.mutex);
  363. if (nodes_received == g_teems_config.num_ingestion_nodes) {
  364. route_state.step = ROUTE_ROUND_1;
  365. void *cbpointer = route_state.cbpointer;
  366. route_state.cbpointer = NULL;
  367. ocall_routing_round_complete(cbpointer, 1);
  368. }
  369. } else {
  370. NodeCommState &nodecom = g_commstates[routing_node];
  371. nodecom.message_start(num_msgs * msg_size);
  372. for (uint32_t i=0; i<full_rows; ++i) {
  373. const uint64_t *idxp = indices + i*tot_weight + start_weight;
  374. for (uint32_t j=0; j<weight; ++j) {
  375. nodecom.message_data(msgs + idxp[j]*msg_size, msg_size);
  376. }
  377. }
  378. const uint64_t *idxp = indices + full_rows*tot_weight + start_weight;
  379. for (uint32_t j=0; j<num_msgs_last_row; ++j) {
  380. nodecom.message_data(msgs + idxp[j]*msg_size, msg_size);
  381. }
  382. }
  383. }
  384. }
  385. // Perform the next round of routing. The callback pointer will be
  386. // passed to ocall_routing_round_complete when the round is complete.
  387. void ecall_routing_proceed(void *cbpointer)
  388. {
  389. uint8_t my_roles = g_teems_config.roles[g_teems_config.my_node_num];
  390. if (route_state.step == ROUTE_NOT_STARTED) {
  391. if (my_roles & ROLE_INGESTION) {
  392. route_state.cbpointer = cbpointer;
  393. MsgBuffer &ingbuf = route_state.ingbuf;
  394. pthread_mutex_lock(&ingbuf.mutex);
  395. // Ensure there are no pending messages currently being inserted
  396. // into the buffer
  397. while (ingbuf.reserved != ingbuf.inserted) {
  398. pthread_mutex_unlock(&ingbuf.mutex);
  399. pthread_mutex_lock(&ingbuf.mutex);
  400. }
  401. // Sort the messages we've received
  402. #ifdef PROFILE_ROUTING
  403. uint32_t inserted = ingbuf.inserted;
  404. unsigned long start = printf_with_rtclock("begin oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  405. #endif
  406. sort_mtobliv(g_teems_config.nthreads, ingbuf.buf,
  407. g_teems_config.msg_size, ingbuf.inserted,
  408. route_state.tot_msg_per_ing, send_round1_msgs);
  409. #ifdef PROFILE_ROUTING
  410. printf_with_rtclock_diff(start, "end oblivious sort (%u,%u)\n", inserted, route_state.tot_msg_per_ing);
  411. #endif
  412. ingbuf.reset();
  413. pthread_mutex_unlock(&ingbuf.mutex);
  414. } else {
  415. route_state.step = ROUTE_ROUND_1;
  416. ocall_routing_round_complete(cbpointer, 1);
  417. }
  418. } else if (route_state.step == ROUTE_ROUND_1) {
  419. if (my_roles & ROLE_ROUTING) {
  420. route_state.cbpointer = cbpointer;
  421. MsgBuffer &round1 = route_state.round1;
  422. pthread_mutex_lock(&round1.mutex);
  423. // Ensure there are no pending messages currently being inserted
  424. // into the buffer
  425. while (round1.reserved != round1.inserted) {
  426. pthread_mutex_unlock(&round1.mutex);
  427. pthread_mutex_lock(&round1.mutex);
  428. }
  429. uint32_t msg_size = g_teems_config.msg_size;
  430. for(uint32_t i=0;i<round1.inserted;++i) {
  431. uint32_t destaddr = *(uint32_t*)(round1.buf+i*msg_size);
  432. printf("%08x\n", destaddr);
  433. }
  434. round1.reset();
  435. pthread_mutex_unlock(&round1.mutex);
  436. } else {
  437. // We're done
  438. route_state.step = ROUTE_NOT_STARTED;
  439. ocall_routing_round_complete(cbpointer, 2);
  440. }
  441. }
  442. }