Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 2 | #include <sys/socket.h> |
| 3 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 4 | #include <app.hpp> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 5 | #include <async_resp.hpp> |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 6 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 7 | #include <websocket.hpp> |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 8 | |
| 9 | namespace crow |
| 10 | { |
| 11 | namespace obmc_kvm |
| 12 | { |
| 13 | |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 14 | static constexpr const uint maxSessions = 4; |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 15 | |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 16 | class KvmSession |
Jae Hyun Yoo | c68604b | 2019-02-26 15:46:07 -0800 | [diff] [blame] | 17 | { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 18 | public: |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 19 | explicit KvmSession(crow::websocket::Connection& connIn) : |
Ed Tanous | f5b191a | 2022-02-15 11:30:39 -0800 | [diff] [blame] | 20 | conn(connIn), hostSocket(conn.getIoContext()) |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 21 | { |
| 22 | boost::asio::ip::tcp::endpoint endpoint( |
Johnathan Mantey | b8c7eb2 | 2020-02-19 12:44:19 -0800 | [diff] [blame] | 23 | boost::asio::ip::make_address("127.0.0.1"), 5900); |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 24 | hostSocket.async_connect( |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 25 | endpoint, [this, &connIn](const boost::system::error_code& ec) { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 26 | if (ec) |
| 27 | { |
| 28 | BMCWEB_LOG_ERROR |
| 29 | << "conn:" << &conn |
| 30 | << ", Couldn't connect to KVM socket port: " << ec; |
| 31 | if (ec != boost::asio::error::operation_aborted) |
| 32 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 33 | connIn.close("Error in connecting to KVM port"); |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 34 | } |
| 35 | return; |
| 36 | } |
Jae Hyun Yoo | c68604b | 2019-02-26 15:46:07 -0800 | [diff] [blame] | 37 | |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 38 | doRead(); |
| 39 | }); |
Jae Hyun Yoo | c68604b | 2019-02-26 15:46:07 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 42 | void onMessage(const std::string& data) |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 43 | { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 44 | if (data.length() > inputBuffer.capacity()) |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 45 | { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 46 | BMCWEB_LOG_ERROR << "conn:" << &conn |
| 47 | << ", Buffer overrun when writing " |
| 48 | << data.length() << " bytes"; |
| 49 | conn.close("Buffer overrun"); |
| 50 | return; |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 51 | } |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 52 | |
| 53 | BMCWEB_LOG_DEBUG << "conn:" << &conn << ", Read " << data.size() |
| 54 | << " bytes from websocket"; |
| 55 | boost::asio::buffer_copy(inputBuffer.prepare(data.size()), |
| 56 | boost::asio::buffer(data)); |
Gunnar Mills | caa3ce3 | 2020-07-08 14:46:53 -0500 | [diff] [blame] | 57 | BMCWEB_LOG_DEBUG << "conn:" << &conn << ", Committing " << data.size() |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 58 | << " bytes from websocket"; |
| 59 | inputBuffer.commit(data.size()); |
| 60 | |
| 61 | BMCWEB_LOG_DEBUG << "conn:" << &conn << ", inputbuffer size " |
| 62 | << inputBuffer.size(); |
| 63 | doWrite(); |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 66 | protected: |
| 67 | void doRead() |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 68 | { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 69 | std::size_t bytes = outputBuffer.capacity() - outputBuffer.size(); |
| 70 | BMCWEB_LOG_DEBUG << "conn:" << &conn << ", Reading " << bytes |
| 71 | << " from kvm socket"; |
| 72 | hostSocket.async_read_some( |
| 73 | outputBuffer.prepare(outputBuffer.capacity() - outputBuffer.size()), |
| 74 | [this](const boost::system::error_code& ec, std::size_t bytesRead) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 75 | BMCWEB_LOG_DEBUG << "conn:" << &conn << ", read done. Read " |
| 76 | << bytesRead << " bytes"; |
| 77 | if (ec) |
| 78 | { |
| 79 | BMCWEB_LOG_ERROR |
| 80 | << "conn:" << &conn |
| 81 | << ", Couldn't read from KVM socket port: " << ec; |
| 82 | if (ec != boost::asio::error::operation_aborted) |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 83 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 84 | conn.close("Error in connecting to KVM port"); |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 85 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 88 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 89 | outputBuffer.commit(bytesRead); |
| 90 | std::string_view payload( |
| 91 | static_cast<const char*>(outputBuffer.data().data()), |
| 92 | bytesRead); |
| 93 | BMCWEB_LOG_DEBUG << "conn:" << &conn << ", Sending payload size " |
| 94 | << payload.size(); |
| 95 | conn.sendBinary(payload); |
| 96 | outputBuffer.consume(bytesRead); |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 97 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 98 | doRead(); |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 99 | }); |
| 100 | } |
| 101 | |
| 102 | void doWrite() |
| 103 | { |
| 104 | if (doingWrite) |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 105 | { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 106 | BMCWEB_LOG_DEBUG << "conn:" << &conn |
| 107 | << ", Already writing. Bailing out"; |
| 108 | return; |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 109 | } |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 110 | if (inputBuffer.size() == 0) |
| 111 | { |
| 112 | BMCWEB_LOG_DEBUG << "conn:" << &conn |
| 113 | << ", inputBuffer empty. Bailing out"; |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | doingWrite = true; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 118 | hostSocket.async_write_some(inputBuffer.data(), |
| 119 | [this](const boost::system::error_code& ec, |
| 120 | std::size_t bytesWritten) { |
| 121 | BMCWEB_LOG_DEBUG << "conn:" << &conn << ", Wrote " << bytesWritten |
| 122 | << "bytes"; |
| 123 | doingWrite = false; |
| 124 | inputBuffer.consume(bytesWritten); |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 125 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 126 | if (ec == boost::asio::error::eof) |
| 127 | { |
| 128 | conn.close("KVM socket port closed"); |
| 129 | return; |
| 130 | } |
| 131 | if (ec) |
| 132 | { |
| 133 | BMCWEB_LOG_ERROR << "conn:" << &conn |
| 134 | << ", Error in KVM socket write " << ec; |
| 135 | if (ec != boost::asio::error::operation_aborted) |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 136 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 137 | conn.close("Error in reading to host port"); |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 138 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 139 | return; |
| 140 | } |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 141 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 142 | doWrite(); |
| 143 | }); |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 146 | crow::websocket::Connection& conn; |
| 147 | boost::asio::ip::tcp::socket hostSocket; |
Ed Tanous | 6de264c | 2022-01-06 12:47:59 -0800 | [diff] [blame] | 148 | boost::beast::flat_static_buffer<1024UL * 50UL> outputBuffer; |
| 149 | boost::beast::flat_static_buffer<1024UL> inputBuffer; |
Ed Tanous | f5b191a | 2022-02-15 11:30:39 -0800 | [diff] [blame] | 150 | bool doingWrite{false}; |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | static boost::container::flat_map<crow::websocket::Connection*, |
| 154 | std::unique_ptr<KvmSession>> |
| 155 | sessions; |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 156 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 157 | inline void requestRoutes(App& app) |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 158 | { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 159 | sessions.reserve(maxSessions); |
| 160 | |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 161 | BMCWEB_ROUTE(app, "/kvm/0") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 162 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 163 | .websocket() |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 164 | .onopen([](crow::websocket::Connection& conn) { |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 165 | BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened"; |
| 166 | |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 167 | if (sessions.size() == maxSessions) |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 168 | { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 169 | conn.close("Max sessions are already connected"); |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 173 | sessions[&conn] = std::make_unique<KvmSession>(conn); |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 174 | }) |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 175 | .onclose([](crow::websocket::Connection& conn, const std::string&) { |
| 176 | sessions.erase(&conn); |
| 177 | }) |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 178 | .onmessage([](crow::websocket::Connection& conn, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 179 | const std::string& data, bool) { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 180 | if (sessions[&conn]) |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 181 | { |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 182 | sessions[&conn]->onMessage(data); |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 183 | } |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 184 | }); |
| 185 | } |
Jae Hyun Yoo | a133b29 | 2019-09-16 10:38:47 -0700 | [diff] [blame] | 186 | |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 187 | } // namespace obmc_kvm |
| 188 | } // namespace crow |