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