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