Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include <app.hpp> |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 4 | #include <boost/beast/core/flat_static_buffer.hpp> |
Ed Tanous | 5edbe94 | 2021-10-09 14:49:05 -0700 | [diff] [blame] | 5 | #include <boost/process/async_pipe.hpp> |
| 6 | #include <boost/process/child.hpp> |
| 7 | #include <boost/process/io.hpp> |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 8 | #include <websocket.hpp> |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 9 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 10 | #include <csignal> |
| 11 | |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 12 | namespace crow |
| 13 | { |
| 14 | namespace obmc_vm |
| 15 | { |
| 16 | |
| 17 | static crow::websocket::Connection* session = nullptr; |
| 18 | |
| 19 | // The max network block device buffer size is 128kb plus 16bytes |
| 20 | // for the message header: |
| 21 | // https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md#simple-reply-message |
Troy Lee | 4ee8f0b | 2021-08-02 11:08:26 +0800 | [diff] [blame] | 22 | static constexpr auto nbdBufferSize = (128 * 1024 + 16) * 4; |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 23 | |
| 24 | class Handler : public std::enable_shared_from_this<Handler> |
| 25 | { |
| 26 | public: |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 27 | Handler(const std::string& mediaIn, boost::asio::io_context& ios) : |
Ed Tanous | f5b191a | 2022-02-15 11:30:39 -0800 | [diff] [blame] | 28 | pipeOut(ios), pipeIn(ios), media(mediaIn), |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 29 | outputBuffer(new boost::beast::flat_static_buffer<nbdBufferSize>), |
| 30 | inputBuffer(new boost::beast::flat_static_buffer<nbdBufferSize>) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 31 | {} |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 32 | |
Ed Tanous | 0c0084a | 2019-10-24 15:57:51 -0700 | [diff] [blame] | 33 | ~Handler() = default; |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 34 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 35 | Handler(const Handler&) = delete; |
| 36 | Handler(Handler&&) = delete; |
| 37 | Handler& operator=(const Handler&) = delete; |
| 38 | Handler& operator=(Handler&&) = delete; |
| 39 | |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 40 | void doClose() |
| 41 | { |
| 42 | // boost::process::child::terminate uses SIGKILL, need to send SIGTERM |
| 43 | // to allow the proxy to stop nbd-client and the USB device gadget. |
| 44 | int rc = kill(proxy.id(), SIGTERM); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 45 | if (rc != 0) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 46 | { |
| 47 | return; |
| 48 | } |
| 49 | proxy.wait(); |
| 50 | } |
| 51 | |
| 52 | void connect() |
| 53 | { |
| 54 | std::error_code ec; |
| 55 | proxy = boost::process::child("/usr/sbin/nbd-proxy", media, |
| 56 | boost::process::std_out > pipeOut, |
| 57 | boost::process::std_in < pipeIn, ec); |
| 58 | if (ec) |
| 59 | { |
| 60 | BMCWEB_LOG_ERROR << "Couldn't connect to nbd-proxy: " |
| 61 | << ec.message(); |
| 62 | if (session != nullptr) |
| 63 | { |
| 64 | session->close("Error connecting to nbd-proxy"); |
| 65 | } |
| 66 | return; |
| 67 | } |
| 68 | doWrite(); |
| 69 | doRead(); |
| 70 | } |
| 71 | |
| 72 | void doWrite() |
| 73 | { |
| 74 | if (doingWrite) |
| 75 | { |
| 76 | BMCWEB_LOG_DEBUG << "Already writing. Bailing out"; |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if (inputBuffer->size() == 0) |
| 81 | { |
| 82 | BMCWEB_LOG_DEBUG << "inputBuffer empty. Bailing out"; |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | doingWrite = true; |
| 87 | pipeIn.async_write_some( |
| 88 | inputBuffer->data(), |
| 89 | [this, self(shared_from_this())](boost::beast::error_code ec, |
| 90 | std::size_t bytesWritten) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 91 | BMCWEB_LOG_DEBUG << "Wrote " << bytesWritten << "bytes"; |
| 92 | doingWrite = false; |
| 93 | inputBuffer->consume(bytesWritten); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 94 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 95 | if (session == nullptr) |
| 96 | { |
| 97 | return; |
| 98 | } |
| 99 | if (ec == boost::asio::error::eof) |
| 100 | { |
| 101 | session->close("VM socket port closed"); |
| 102 | return; |
| 103 | } |
| 104 | if (ec) |
| 105 | { |
| 106 | session->close("Error in writing to proxy port"); |
| 107 | BMCWEB_LOG_ERROR << "Error in VM socket write " << ec; |
| 108 | return; |
| 109 | } |
| 110 | doWrite(); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 111 | }); |
| 112 | } |
| 113 | |
| 114 | void doRead() |
| 115 | { |
| 116 | std::size_t bytes = outputBuffer->capacity() - outputBuffer->size(); |
| 117 | |
| 118 | pipeOut.async_read_some( |
| 119 | outputBuffer->prepare(bytes), |
| 120 | [this, self(shared_from_this())]( |
| 121 | const boost::system::error_code& ec, std::size_t bytesRead) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 122 | BMCWEB_LOG_DEBUG << "Read done. Read " << bytesRead << " bytes"; |
| 123 | if (ec) |
| 124 | { |
| 125 | BMCWEB_LOG_ERROR << "Couldn't read from VM port: " << ec; |
| 126 | if (session != nullptr) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 127 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 128 | session->close("Error in connecting to VM port"); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 129 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | if (session == nullptr) |
| 133 | { |
| 134 | return; |
| 135 | } |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 136 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 137 | outputBuffer->commit(bytesRead); |
| 138 | std::string_view payload( |
| 139 | static_cast<const char*>(outputBuffer->data().data()), |
| 140 | bytesRead); |
| 141 | session->sendBinary(payload); |
| 142 | outputBuffer->consume(bytesRead); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 143 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 144 | doRead(); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 145 | }); |
| 146 | } |
| 147 | |
| 148 | boost::process::async_pipe pipeOut; |
| 149 | boost::process::async_pipe pipeIn; |
| 150 | boost::process::child proxy; |
| 151 | std::string media; |
Ed Tanous | f5b191a | 2022-02-15 11:30:39 -0800 | [diff] [blame] | 152 | bool doingWrite{false}; |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 153 | |
| 154 | std::unique_ptr<boost::beast::flat_static_buffer<nbdBufferSize>> |
| 155 | outputBuffer; |
| 156 | std::unique_ptr<boost::beast::flat_static_buffer<nbdBufferSize>> |
| 157 | inputBuffer; |
| 158 | }; |
| 159 | |
| 160 | static std::shared_ptr<Handler> handler; |
| 161 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 162 | inline void requestRoutes(App& app) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 163 | { |
| 164 | BMCWEB_ROUTE(app, "/vm/0/0") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 165 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 166 | .websocket() |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 167 | .onopen([](crow::websocket::Connection& conn) { |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 168 | BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened"; |
| 169 | |
| 170 | if (session != nullptr) |
| 171 | { |
| 172 | conn.close("Session already connected"); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (handler != nullptr) |
| 177 | { |
| 178 | conn.close("Handler already running"); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | session = &conn; |
| 183 | |
| 184 | // media is the last digit of the endpoint /vm/0/0. A future |
| 185 | // enhancement can include supporting different endpoint values. |
| 186 | const char* media = "0"; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 187 | handler = std::make_shared<Handler>(media, conn.getIoContext()); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 188 | handler->connect(); |
| 189 | }) |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 190 | .onclose([](crow::websocket::Connection& conn, |
| 191 | const std::string& /*reason*/) { |
| 192 | if (&conn != session) |
| 193 | { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | session = nullptr; |
| 198 | handler->doClose(); |
| 199 | handler->inputBuffer->clear(); |
| 200 | handler->outputBuffer->clear(); |
| 201 | handler.reset(); |
| 202 | }) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 203 | .onmessage([](crow::websocket::Connection& conn, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 204 | const std::string& data, bool) { |
Troy Lee | 4ee8f0b | 2021-08-02 11:08:26 +0800 | [diff] [blame] | 205 | if (data.length() > |
| 206 | handler->inputBuffer->capacity() - handler->inputBuffer->size()) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 207 | { |
| 208 | BMCWEB_LOG_ERROR << "Buffer overrun when writing " |
| 209 | << data.length() << " bytes"; |
| 210 | conn.close("Buffer overrun"); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | boost::asio::buffer_copy(handler->inputBuffer->prepare(data.size()), |
| 215 | boost::asio::buffer(data)); |
| 216 | handler->inputBuffer->commit(data.size()); |
| 217 | handler->doWrite(); |
| 218 | }); |
| 219 | } |
| 220 | |
| 221 | } // namespace obmc_vm |
| 222 | } // namespace crow |