config.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "Enclave_t.h"
  2. #include "comms.hpp"
  3. #include "config.hpp"
  4. #include "utils.hpp"
  5. #include "route.hpp"
  6. #include "ingest.hpp"
  7. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  8. unsigned long ingestion_epoch;
  9. unsigned long storage_epoch;
  10. Config g_teems_config;
  11. static int generateMasterKeys(sgx_aes_gcm_128bit_key_t master_secret,
  12. sgx_aes_gcm_128bit_key_t &ESK, sgx_aes_gcm_128bit_key_t &TSK)
  13. {
  14. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  15. unsigned char iv[SGX_AESGCM_IV_SIZE];
  16. sgx_aes_gcm_128bit_tag_t mac;
  17. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  18. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  19. memcpy(iv, "Encryption", sizeof("Encryption"));
  20. sgx_status_t ret = SGX_SUCCESS;
  21. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *)
  22. master_secret, zeroes, SGX_AESGCM_KEY_SIZE,
  23. (uint8_t*) ESK, iv, SGX_AESGCM_IV_SIZE, NULL, 0, &mac);
  24. if(ret!=SGX_SUCCESS) {
  25. return -1;
  26. }
  27. printf("Encryption Master Key: ");
  28. for(int i=0;i<SGX_AESGCM_KEY_SIZE;i++) {
  29. printf("%02x", ESK[i]);
  30. }
  31. printf("\n");
  32. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  33. memcpy(iv, "Token", sizeof("Token"));
  34. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *)
  35. master_secret, zeroes, SGX_AESGCM_KEY_SIZE,
  36. (uint8_t*) TSK, iv, SGX_AESGCM_IV_SIZE, NULL, 0, &mac);
  37. if(ret!=SGX_SUCCESS) {
  38. return -1;
  39. }
  40. printf("Token Master Key: ");
  41. for(int i=0;i<SGX_AESGCM_KEY_SIZE;i++) {
  42. printf("%02x", TSK[i]);
  43. }
  44. printf("\n");
  45. return 1;
  46. }
  47. bool ecall_config_load(threadid_t nthreads,
  48. EnclaveAPIParams *apiparams,
  49. EnclaveAPINodeConfig *apinodeconfigs,
  50. nodenum_t num_nodes, nodenum_t my_node_num)
  51. {
  52. #ifdef TRACK_HEAP_USAGE
  53. printf("ecall_config_load begin heap %u\n", g_peak_heap_used);
  54. #endif
  55. g_teems_config.nthreads = nthreads;
  56. g_teems_config.num_nodes = num_nodes;
  57. g_teems_config.num_ingestion_nodes = 0;
  58. g_teems_config.num_routing_nodes = 0;
  59. g_teems_config.num_storage_nodes = 0;
  60. g_teems_config.my_node_num = my_node_num;
  61. g_teems_config.user_count = apiparams->user_count;
  62. g_teems_config.msg_size = apiparams->msg_size;
  63. g_teems_config.m_token_out = apiparams->m_token_out;
  64. g_teems_config.m_token_in = apiparams->m_token_in;
  65. g_teems_config.m_id_out = apiparams->m_id_out;
  66. g_teems_config.m_id_in = apiparams->m_id_in;
  67. memcpy(g_teems_config.master_secret, apiparams->master_secret,
  68. SGX_AESGCM_KEY_SIZE);
  69. g_teems_config.token_channel = apiparams->token_channel;
  70. // Temporary vectors to store node numbers for nodes of different
  71. // types, where the node numbers are smaller than our own node
  72. // number
  73. std::vector<nodenum_t> ing_smaller, rte_smaller, str_smaller;
  74. uint16_t cumul_weight = 0;
  75. g_teems_config.weights.clear();
  76. g_teems_config.ingestion_nodes.clear();
  77. g_teems_config.routing_nodes.clear();
  78. g_teems_config.storage_nodes.clear();
  79. g_teems_config.storage_map.clear();
  80. for (nodenum_t i=0; i<num_nodes; ++i) {
  81. NodeWeight nw;
  82. nw.startweight = cumul_weight;
  83. // Weights only matter for routing nodes
  84. nw.weight = 0;
  85. if (apinodeconfigs[i].roles & ROLE_INGESTION) {
  86. g_teems_config.num_ingestion_nodes += 1;
  87. if (i < my_node_num) {
  88. ing_smaller.push_back(i);
  89. } else {
  90. g_teems_config.ingestion_nodes.push_back(i);
  91. }
  92. }
  93. if (apinodeconfigs[i].roles & ROLE_ROUTING) {
  94. // Only use weights in token channel routing
  95. if (g_teems_config.token_channel) {
  96. nw.weight = apinodeconfigs[i].weight;
  97. } else {
  98. nw.weight = 1;
  99. }
  100. g_teems_config.num_routing_nodes += 1;
  101. if (i < my_node_num) {
  102. rte_smaller.push_back(i);
  103. } else {
  104. g_teems_config.routing_nodes.push_back(i);
  105. }
  106. }
  107. if (apinodeconfigs[i].roles & ROLE_STORAGE) {
  108. g_teems_config.num_storage_nodes += 1;
  109. if (i < my_node_num) {
  110. str_smaller.push_back(i);
  111. } else {
  112. g_teems_config.storage_nodes.push_back(i);
  113. }
  114. g_teems_config.storage_map.push_back(i);
  115. }
  116. cumul_weight += nw.weight;
  117. g_teems_config.weights.push_back(nw);
  118. g_teems_config.roles.push_back(apinodeconfigs[i].roles);
  119. if (i == my_node_num) {
  120. g_teems_config.my_weight = nw.weight;
  121. }
  122. }
  123. g_teems_config.tot_weight = cumul_weight;
  124. // Concatenate the *_smaller vectors to the ends of the
  125. // g_teems_config.*_nodes vectors. This way, each node has a list
  126. // of nodes of each role starting with itself and "looping around".
  127. // This should make the communication pattern have less of a
  128. // bottleneck.
  129. g_teems_config.ingestion_nodes.insert(
  130. g_teems_config.ingestion_nodes.end(),
  131. ing_smaller.begin(),
  132. ing_smaller.end());
  133. g_teems_config.routing_nodes.insert(
  134. g_teems_config.routing_nodes.end(),
  135. rte_smaller.begin(),
  136. rte_smaller.end());
  137. g_teems_config.storage_nodes.insert(
  138. g_teems_config.storage_nodes.end(),
  139. str_smaller.begin(),
  140. str_smaller.end());
  141. // Initialize the threadpool and the pseudorandom bytes pools
  142. threadpool_init(nthreads);
  143. uint8_t my_role = apinodeconfigs[my_node_num].roles;
  144. if ( (my_role & ROLE_INGESTION) || (my_role & ROLE_STORAGE) ) {
  145. generateMasterKeys(g_teems_config.master_secret,
  146. g_teems_config.ESK, g_teems_config.TSK);
  147. uint32_t num_clients_total = g_teems_config.user_count;
  148. if (my_role & ROLE_INGESTION) {
  149. uint32_t num_ing_nodes = g_teems_config.num_ingestion_nodes;
  150. uint32_t clients_per_server =
  151. CEILDIV(num_clients_total, num_ing_nodes);
  152. uint32_t num_clients_this_ing = clients_per_server;
  153. uint32_t client_start = ing_smaller.size();
  154. g_ing.initialize(num_clients_this_ing, client_start,
  155. g_teems_config.ESK);
  156. }
  157. }
  158. ingestion_epoch = 0;
  159. storage_epoch = 0;
  160. #ifdef TRACK_HEAP_USAGE
  161. printf("ecall_config_load H1 heap %u\n", g_peak_heap_used);
  162. #endif
  163. if (!route_init()) {
  164. return false;
  165. }
  166. #ifdef TRACK_HEAP_USAGE
  167. printf("ecall_config_load H2 heap %u\n", g_peak_heap_used);
  168. #endif
  169. bool ret = comms_init_nodestate(apinodeconfigs, num_nodes, my_node_num);
  170. #ifdef TRACK_HEAP_USAGE
  171. printf("ecall_config_load end heap %u\n", g_peak_heap_used);
  172. #endif
  173. return ret;
  174. }
  175. void ecall_close()
  176. {
  177. route_close();
  178. threadpool_shutdown();
  179. }