net.hpp 6.9 KB

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