net.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef __NET_HPP__
  2. #define __NET_HPP__
  3. #include <vector>
  4. #include <deque>
  5. #include <optional>
  6. #include <functional>
  7. #include <tuple>
  8. #include <boost/asio.hpp>
  9. #include <boost/thread.hpp>
  10. #include "appconfig.hpp"
  11. #include "../Enclave/enclave_api.h"
  12. // #define DEBUG_NET_CLIENTS
  13. #define PROFILE_NET_CLIENTS
  14. // The inter-node (untrusted node to untrusted node) communication
  15. // protocol is as follows. Nodes are numbered 0 through num_nodes-1.
  16. // At startup time, each pair of nodes establishes a TCP connection by
  17. // having the lower-numbered node connect to the higher-numbered node,
  18. // and send a two-byte value of its (the sender's) node number. Once
  19. // all the connections are established, commands consist of a 5-byte
  20. // header, followed optionally by some data. The commands are listed
  21. // below. If a socket closes, we interpret that to mean the experiment
  22. // is over, and the node shuts down (which will close its own sockets,
  23. // its peers will shut down, etc.). [This isn't the best idea for a
  24. // robust long-lived deployment, of course.]
  25. //
  26. // The commands are:
  27. //
  28. // EPOCH: 0x00 + 4-byte epoch number (little-endian)
  29. //
  30. // This command is sent by the leader (typically node 0) to each other
  31. // node at the start of each epoch.
  32. //
  33. // MESSAGE: 0x01 + 4-byte total message length (little-endian)
  34. //
  35. // This command says that a number of CHUNKs comprising a single
  36. // enclave-to-enclave message will follow, whose total size will be the
  37. // given value. Note that the data itself is sent following a CHUNK
  38. // header, not a MESSAGE header, even if it's small.
  39. //
  40. // CHUNK: 0x02 + 4-byte chunk length (little-endian)
  41. // + that many bytes of data
  42. //
  43. // This command transmits the enclave-to-enclave data. The data in the
  44. // chunk will be (after the enclave-to-enclave handshake, anyway)
  45. // AES-GCM encrypted to a key known to the receiving enclave (but not
  46. // the receiving untrusted node). The chunk number (starting from 0 and
  47. // not reset between messages) will be the IV, which is not transmitted.
  48. // The 16-byte GCM tag will be the last 16 bytes of the chunk (and
  49. // included in the length in the chunk header). The sum of the chunk
  50. // lengths since the last MESSAGE command may not exceed the length in
  51. // that MESSAGE command.
  52. // Data for chunks are stored in frames. The frames are pre-allocated
  53. // to be FRAME_SIZE bytes each, and reused as much as possible by the
  54. // NodeIO class. A node will request a frame from the NodeIO, which
  55. // will return a pointer. The node will pass that pointer to the
  56. // enclave, which will write data into it, and also return to the node
  57. // how much data it wrote. The node will async_write the chunk header
  58. // and the chunk data. The async write completion handler will return
  59. // the frame to the NodeIO when the write completes.
  60. //
  61. // Headers are stored as the low 5 bytes of a uint64_t. Note that means
  62. // for headers containing sizes, the value of this uint64_t will be (for
  63. // example for the CHUNK header) (chunk_len << 8) + 0x02.
  64. using boost::asio::ip::tcp;
  65. class NodeIO {
  66. tcp::socket sock;
  67. nodenum_t node_num;
  68. using CommandTuple = std::tuple<uint64_t,uint8_t*,size_t>;
  69. std::deque<CommandTuple> commands_inflight;
  70. std::deque<uint8_t *> frames_available;
  71. // The frames and commands are used and returned by different
  72. // threads, so we protect them with a mutex each
  73. boost::mutex frame_deque_lock, commands_deque_lock;
  74. // The claimed size of the message currently being sent in chunks
  75. uint32_t msgsize_inflight;
  76. // The total size of the chunks so far we've sent for this message
  77. uint32_t chunksize_inflight;
  78. // As above, but for incoming messages and chunks
  79. uint32_t recv_msgsize_inflight;
  80. uint32_t recv_chunksize_inflight;
  81. // The static uint64_t used to receive a header
  82. uint64_t receive_header;
  83. // The static frame used to receive a chunk
  84. uint8_t receive_frame[FRAME_SIZE];
  85. uint64_t bytes_sent; // count bytes sent
  86. void send_header_data(uint64_t header, uint8_t *data, size_t len);
  87. // Asynchronously send the first message from the command queue.
  88. // * The command_deque_lock must be held when this is called! *
  89. // This method may be called from either thread (the work thread or
  90. // the async_write handler thread).
  91. void async_send_commands();
  92. public:
  93. NodeIO(tcp::socket &&socket, nodenum_t node_num);
  94. uint8_t *request_frame();
  95. void return_frame(uint8_t* frame);
  96. void send_epoch(uint32_t epoch_num);
  97. void send_message_header(uint32_t tot_message_len);
  98. // Returns true if there are more chunks to send in this message,
  99. // false if not.
  100. bool send_chunk(uint8_t *data, uint32_t chunk_len);
  101. // Asynchronously receive commands from this socket. Depending on
  102. // what they are, one of the three callbacks will be called. The
  103. // callbacks may be called from a different thread. The data
  104. // pointer in chunk_cb is to a _static_ frame that's only used for
  105. // receiving. Be sure to do whatever you need to do with the
  106. // contents (typically, pass it to the enclave) before calling this
  107. // function again.
  108. void recv_commands(
  109. std::function<void(boost::system::error_code)> error_cb,
  110. std::function<void(uint32_t)> epoch_cb);
  111. // Close the socket
  112. void close() { sock.close(); }
  113. uint64_t reset_bytes_sent();
  114. };
  115. class NetIO {
  116. boost::asio::io_context &context;
  117. const Config &conf;
  118. const NodeConfig &myconf;
  119. std::deque<std::optional<NodeIO>> nodeios;
  120. std::shared_ptr<tcp::acceptor> ingestion_acceptor;
  121. std::shared_ptr<tcp::acceptor> storage_acceptor;
  122. size_t auth_size, msgbundle_size;
  123. void ing_receive_msgbundle(tcp::socket* socket, clientid_t c_simid);
  124. void ing_authenticate_new_client(tcp::socket* socket,
  125. const boost::system::error_code& error);
  126. void ing_start_accept();
  127. std::vector<tcp::socket*> client_sockets;
  128. uint32_t num_clients_per_stg;
  129. unsigned char *epoch_tokens;
  130. unsigned char *epoch_mailboxes;
  131. uint16_t num_stg_nodes;
  132. uint32_t token_bundle_size;
  133. uint32_t mailbox_size;
  134. void stg_authenticate_new_client(tcp::socket* socket,
  135. const boost::system::error_code& error);
  136. void stg_start_accept();
  137. public:
  138. NetIO(boost::asio::io_context &io_context, const Config &config);
  139. nodenum_t num_nodes;
  140. nodenum_t me;
  141. NodeIO &node(nodenum_t node_num) {
  142. assert(node_num < num_nodes);
  143. return nodeios[node_num].value();
  144. }
  145. const Config &config() { return conf; }
  146. const NodeConfig &myconfig() { return myconf; }
  147. boost::asio::io_context &io_context() { return context; }
  148. // Call recv_commands with these arguments on each of the nodes (not
  149. // including ourselves)
  150. void recv_commands(
  151. std::function<void(boost::system::error_code)> error_cb,
  152. std::function<void(uint32_t)> epoch_cb);
  153. void send_client_mailbox();
  154. // Close all the sockets
  155. void close();
  156. uint64_t reset_bytes_sent();
  157. };
  158. extern NetIO *g_netio;
  159. extern size_t client_count;
  160. #endif