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