net.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef __NET_HPP__
  2. #define __NET_HPP__
  3. #include <vector>
  4. #include <deque>
  5. #include <optional>
  6. #include <boost/asio.hpp>
  7. #include <boost/thread.hpp>
  8. #include "config.hpp"
  9. #define MAXCHUNKSIZE (65536+16)
  10. // The inter-node (untrusted node to untrusted node) communication
  11. // protocol is as follows. Nodes are numbered 0 through num_nodes-1.
  12. // At startup time, each pair of nodes establishes a TCP connection by
  13. // having the lower-numbered node connect to the higher-numbered node,
  14. // and send a two-byte value of its (the sender's) node number. Once
  15. // all the connections are established, commands consist of a 5-byte
  16. // header, followed optionally by some data. The commands are listed
  17. // below. If a socket closes, we interpret that to mean the experiment
  18. // is over, and the node shuts down (which will close its own sockets,
  19. // its peers will shut down, etc.). [This isn't the best idea for a
  20. // robust long-lived deployment, of course.]
  21. //
  22. // The commands are:
  23. //
  24. // EPOCH: 0x00 + 4-byte epoch number (little-endian)
  25. //
  26. // This command is sent by the leader (typically node 0) to each other
  27. // node at the start of each epoch.
  28. //
  29. // MESSAGE: 0x01 + 4-byte total message length (little-endian)
  30. //
  31. // This command says that a number of CHUNKs comprising a single
  32. // enclave-to-enclave message will follow, whose total size will be the
  33. // given value. Note that the data itself is sent following a CHUNK
  34. // header, not a MESSAGE header, even if it's small.
  35. //
  36. // CHUNK: 0x02 + 4-byte chunk length (little-endian)
  37. // + that many bytes of data
  38. //
  39. // This command transmits the enclave-to-enclave data. The data in the
  40. // chunk will be (after the enclace-to-enclave handshake, anyway)
  41. // AES-GCM encrypted to a key known to the receiving enclave (but not
  42. // the receiving untrusted node). The chunk number (starting from 0 and
  43. // not reset between messages) will be the IV, which is not transmitted.
  44. // The 16-byte GCM tag will be the last 16 bytes of the chunk (and
  45. // included in the length in the chunk header). The sum of the chunk
  46. // lengths since the last MESSAGE command may not exceed the length in
  47. // that MESSAGE command.
  48. // Data for chunks are stored in frames. The frames are pre-allocated
  49. // to be MAXCHUNKSIZE bytes each, and reused as much as possible by the
  50. // NodeIO class. A node will request a frame from the NodeIO, which
  51. // will return a pointer. The node will pass that pointer to the
  52. // enclave, which will write data into it, and also return to the node
  53. // how much data it wrote. The node will async_write the chunk header
  54. // and the chunk data. The async write completion handler will return
  55. // the frame to the NodeIO when the write completes.
  56. //
  57. // Headers are stored as the low 5 bytes of a uint64_t. Note that means
  58. // for headers containing sizes, the value of this uint64_t will be (for
  59. // example for the CHUNK header) (chunk_len << 8) + 0x02.
  60. using boost::asio::ip::tcp;
  61. class NodeIO {
  62. tcp::socket sock;
  63. std::deque<uint64_t> headers_inflight;
  64. std::deque<uint8_t *> frames_available;
  65. // The frames and headers are used and returned by different
  66. // threads, so we protect them with a mutex each
  67. boost::mutex frame_deque_lock, header_deque_lock;
  68. // The claimed size of the message currently being sent in chunks
  69. uint32_t msgsize_inflight;
  70. // The total size of the chunks so far we've sent for this message
  71. uint32_t chunksize_inflight;
  72. // The static frame used to _receive_ data
  73. uint8_t receive_frame[MAXCHUNKSIZE];
  74. void send_header_data(uint64_t header, uint8_t *data, size_t len);
  75. public:
  76. NodeIO(tcp::socket &&socket);
  77. uint8_t *request_frame();
  78. void return_frame(uint8_t* frame);
  79. void send_epoch(uint32_t epoch_num);
  80. void send_message_header(uint32_t tot_message_len);
  81. void send_chunk(uint8_t *data, uint32_t chunk_len);
  82. // These functions return true for success, false for failure
  83. bool recv_header(uint64_t &header);
  84. // This function puts the received data into a _static_ frame that's
  85. // only used for receiving. Be sure to do whatever you need to do
  86. // with the contents (typically, pass it to the enclave) before
  87. // calling this function again. Pass *in* the header you got from
  88. // recv_header.
  89. bool recv_chunk(uint64_t header, uint8_t *&data, size_t &len);
  90. };
  91. class NetIO {
  92. const Config &conf;
  93. const NodeConfig &myconf;
  94. std::deque<std::optional<NodeIO>> nodeios;
  95. public:
  96. NetIO(boost::asio::io_context &io_context, const Config &config);
  97. size_t num_nodes;
  98. size_t me;
  99. NodeIO &node(size_t node_num) { return nodeios[node_num].value(); }
  100. };
  101. #endif