blob: e3131bb3c280c2a16fc16afe1c8992445b36140d [file] [log] [blame]
Ed Tanous3eb2f352018-12-20 12:30:45 -08001#pragma once
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08002#include "app.hpp"
3#include "async_resp.hpp"
Ed Tanousfaf100f2023-05-25 10:03:14 -07004#include "websocket.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005
Ed Tanous3eb2f352018-12-20 12:30:45 -08006#include <sys/socket.h>
7
8#include <boost/container/flat_map.hpp>
Ed Tanous3eb2f352018-12-20 12:30:45 -08009
10namespace crow
11{
12namespace obmc_kvm
13{
14
Jae Hyun Yooa133b292019-09-16 10:38:47 -070015static constexpr const uint maxSessions = 4;
Ed Tanous3eb2f352018-12-20 12:30:45 -080016
Jae Hyun Yooa133b292019-09-16 10:38:47 -070017class KvmSession
Jae Hyun Yooc68604b2019-02-26 15:46:07 -080018{
Jae Hyun Yooa133b292019-09-16 10:38:47 -070019 public:
Ed Tanous23a21a12020-07-25 04:45:05 +000020 explicit KvmSession(crow::websocket::Connection& connIn) :
Ed Tanousf5b191a2022-02-15 11:30:39 -080021 conn(connIn), hostSocket(conn.getIoContext())
Jae Hyun Yooa133b292019-09-16 10:38:47 -070022 {
23 boost::asio::ip::tcp::endpoint endpoint(
Johnathan Manteyb8c7eb22020-02-19 12:44:19 -080024 boost::asio::ip::make_address("127.0.0.1"), 5900);
Jae Hyun Yooa133b292019-09-16 10:38:47 -070025 hostSocket.async_connect(
Ed Tanous23a21a12020-07-25 04:45:05 +000026 endpoint, [this, &connIn](const boost::system::error_code& ec) {
Jae Hyun Yooa133b292019-09-16 10:38:47 -070027 if (ec)
28 {
Ed Tanous62598e32023-07-17 17:06:25 -070029 BMCWEB_LOG_ERROR(
30 "conn:{}, Couldn't connect to KVM socket port: {}",
31 logPtr(&conn), ec);
Jae Hyun Yooa133b292019-09-16 10:38:47 -070032 if (ec != boost::asio::error::operation_aborted)
33 {
Ed Tanous23a21a12020-07-25 04:45:05 +000034 connIn.close("Error in connecting to KVM port");
Jae Hyun Yooa133b292019-09-16 10:38:47 -070035 }
36 return;
37 }
Jae Hyun Yooc68604b2019-02-26 15:46:07 -080038
Jae Hyun Yooa133b292019-09-16 10:38:47 -070039 doRead();
40 });
Jae Hyun Yooc68604b2019-02-26 15:46:07 -080041 }
42
Jae Hyun Yooa133b292019-09-16 10:38:47 -070043 void onMessage(const std::string& data)
Ed Tanous3eb2f352018-12-20 12:30:45 -080044 {
Jae Hyun Yooa133b292019-09-16 10:38:47 -070045 if (data.length() > inputBuffer.capacity())
Ed Tanous3eb2f352018-12-20 12:30:45 -080046 {
Ed Tanous62598e32023-07-17 17:06:25 -070047 BMCWEB_LOG_ERROR("conn:{}, Buffer overrun when writing {} bytes",
48 logPtr(&conn), data.length());
Jae Hyun Yooa133b292019-09-16 10:38:47 -070049 conn.close("Buffer overrun");
50 return;
Ed Tanous3eb2f352018-12-20 12:30:45 -080051 }
Jae Hyun Yooa133b292019-09-16 10:38:47 -070052
Ed Tanous62598e32023-07-17 17:06:25 -070053 BMCWEB_LOG_DEBUG("conn:{}, Read {} bytes from websocket", logPtr(&conn),
54 data.size());
Jae Hyun Yooa133b292019-09-16 10:38:47 -070055 boost::asio::buffer_copy(inputBuffer.prepare(data.size()),
56 boost::asio::buffer(data));
Ed Tanous62598e32023-07-17 17:06:25 -070057 BMCWEB_LOG_DEBUG("conn:{}, Committing {} bytes from websocket",
58 logPtr(&conn), data.size());
Jae Hyun Yooa133b292019-09-16 10:38:47 -070059 inputBuffer.commit(data.size());
60
Ed Tanous62598e32023-07-17 17:06:25 -070061 BMCWEB_LOG_DEBUG("conn:{}, inputbuffer size {}", logPtr(&conn),
62 inputBuffer.size());
Jae Hyun Yooa133b292019-09-16 10:38:47 -070063 doWrite();
Ed Tanous3eb2f352018-12-20 12:30:45 -080064 }
65
Jae Hyun Yooa133b292019-09-16 10:38:47 -070066 protected:
67 void doRead()
Ed Tanous3eb2f352018-12-20 12:30:45 -080068 {
Jae Hyun Yooa133b292019-09-16 10:38:47 -070069 std::size_t bytes = outputBuffer.capacity() - outputBuffer.size();
Ed Tanous62598e32023-07-17 17:06:25 -070070 BMCWEB_LOG_DEBUG("conn:{}, Reading {} from kvm socket", logPtr(&conn),
71 bytes);
Jae Hyun Yooa133b292019-09-16 10:38:47 -070072 hostSocket.async_read_some(
73 outputBuffer.prepare(outputBuffer.capacity() - outputBuffer.size()),
74 [this](const boost::system::error_code& ec, std::size_t bytesRead) {
Ed Tanous62598e32023-07-17 17:06:25 -070075 BMCWEB_LOG_DEBUG("conn:{}, read done. Read {} bytes",
76 logPtr(&conn), bytesRead);
Ed Tanous002d39b2022-05-31 08:59:27 -070077 if (ec)
78 {
Ed Tanous62598e32023-07-17 17:06:25 -070079 BMCWEB_LOG_ERROR(
80 "conn:{}, Couldn't read from KVM socket port: {}",
81 logPtr(&conn), ec);
Ed Tanous002d39b2022-05-31 08:59:27 -070082 if (ec != boost::asio::error::operation_aborted)
Jae Hyun Yooa133b292019-09-16 10:38:47 -070083 {
Ed Tanous002d39b2022-05-31 08:59:27 -070084 conn.close("Error in connecting to KVM port");
Jae Hyun Yooa133b292019-09-16 10:38:47 -070085 }
Ed Tanous002d39b2022-05-31 08:59:27 -070086 return;
87 }
Jae Hyun Yooa133b292019-09-16 10:38:47 -070088
Ed Tanous002d39b2022-05-31 08:59:27 -070089 outputBuffer.commit(bytesRead);
90 std::string_view payload(
91 static_cast<const char*>(outputBuffer.data().data()),
92 bytesRead);
Ed Tanous62598e32023-07-17 17:06:25 -070093 BMCWEB_LOG_DEBUG("conn:{}, Sending payload size {}", logPtr(&conn),
94 payload.size());
Ed Tanous002d39b2022-05-31 08:59:27 -070095 conn.sendBinary(payload);
96 outputBuffer.consume(bytesRead);
Jae Hyun Yooa133b292019-09-16 10:38:47 -070097
Ed Tanous002d39b2022-05-31 08:59:27 -070098 doRead();
Jae Hyun Yooa133b292019-09-16 10:38:47 -070099 });
100 }
101
102 void doWrite()
103 {
104 if (doingWrite)
Ed Tanous3eb2f352018-12-20 12:30:45 -0800105 {
Ed Tanous62598e32023-07-17 17:06:25 -0700106 BMCWEB_LOG_DEBUG("conn:{}, Already writing. Bailing out",
107 logPtr(&conn));
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700108 return;
Ed Tanous3eb2f352018-12-20 12:30:45 -0800109 }
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700110 if (inputBuffer.size() == 0)
111 {
Ed Tanous62598e32023-07-17 17:06:25 -0700112 BMCWEB_LOG_DEBUG("conn:{}, inputBuffer empty. Bailing out",
113 logPtr(&conn));
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700114 return;
115 }
116
117 doingWrite = true;
Ed Tanous002d39b2022-05-31 08:59:27 -0700118 hostSocket.async_write_some(inputBuffer.data(),
119 [this](const boost::system::error_code& ec,
120 std::size_t bytesWritten) {
Ed Tanous62598e32023-07-17 17:06:25 -0700121 BMCWEB_LOG_DEBUG("conn:{}, Wrote {}bytes", logPtr(&conn),
122 bytesWritten);
Ed Tanous002d39b2022-05-31 08:59:27 -0700123 doingWrite = false;
124 inputBuffer.consume(bytesWritten);
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700125
Ed Tanous002d39b2022-05-31 08:59:27 -0700126 if (ec == boost::asio::error::eof)
127 {
128 conn.close("KVM socket port closed");
129 return;
130 }
131 if (ec)
132 {
Ed Tanous62598e32023-07-17 17:06:25 -0700133 BMCWEB_LOG_ERROR("conn:{}, Error in KVM socket write {}",
134 logPtr(&conn), ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700135 if (ec != boost::asio::error::operation_aborted)
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700136 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700137 conn.close("Error in reading to host port");
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700138 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700139 return;
140 }
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700141
Ed Tanous002d39b2022-05-31 08:59:27 -0700142 doWrite();
143 });
Ed Tanous3eb2f352018-12-20 12:30:45 -0800144 }
145
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700146 crow::websocket::Connection& conn;
147 boost::asio::ip::tcp::socket hostSocket;
Ed Tanous6de264c2022-01-06 12:47:59 -0800148 boost::beast::flat_static_buffer<1024UL * 50UL> outputBuffer;
149 boost::beast::flat_static_buffer<1024UL> inputBuffer;
Ed Tanousf5b191a2022-02-15 11:30:39 -0800150 bool doingWrite{false};
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700151};
152
Ed Tanouscf9e4172022-12-21 09:30:16 -0800153using SessionMap = boost::container::flat_map<crow::websocket::Connection*,
154 std::unique_ptr<KvmSession>>;
155// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
156static SessionMap sessions;
Ed Tanous3eb2f352018-12-20 12:30:45 -0800157
Ed Tanous52cc1122020-07-18 13:51:21 -0700158inline void requestRoutes(App& app)
Ed Tanous3eb2f352018-12-20 12:30:45 -0800159{
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700160 sessions.reserve(maxSessions);
161
Ed Tanous3eb2f352018-12-20 12:30:45 -0800162 BMCWEB_ROUTE(app, "/kvm/0")
Ed Tanous432a8902021-06-14 15:28:56 -0700163 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanous3eb2f352018-12-20 12:30:45 -0800164 .websocket()
zhanghch0577726382021-10-21 14:07:57 +0800165 .onopen([](crow::websocket::Connection& conn) {
Ed Tanous62598e32023-07-17 17:06:25 -0700166 BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn));
Ed Tanous3eb2f352018-12-20 12:30:45 -0800167
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700168 if (sessions.size() == maxSessions)
Ed Tanous3eb2f352018-12-20 12:30:45 -0800169 {
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700170 conn.close("Max sessions are already connected");
Ed Tanous3eb2f352018-12-20 12:30:45 -0800171 return;
172 }
173
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700174 sessions[&conn] = std::make_unique<KvmSession>(conn);
Ed Tanous3eb2f352018-12-20 12:30:45 -0800175 })
Ed Tanouscb13a392020-07-25 19:02:03 +0000176 .onclose([](crow::websocket::Connection& conn, const std::string&) {
177 sessions.erase(&conn);
178 })
Ed Tanous3eb2f352018-12-20 12:30:45 -0800179 .onmessage([](crow::websocket::Connection& conn,
Ed Tanouscb13a392020-07-25 19:02:03 +0000180 const std::string& data, bool) {
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700181 if (sessions[&conn])
Ed Tanous3eb2f352018-12-20 12:30:45 -0800182 {
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700183 sessions[&conn]->onMessage(data);
Ed Tanous3eb2f352018-12-20 12:30:45 -0800184 }
Ed Tanous3eb2f352018-12-20 12:30:45 -0800185 });
186}
Jae Hyun Yooa133b292019-09-16 10:38:47 -0700187
Ed Tanous3eb2f352018-12-20 12:30:45 -0800188} // namespace obmc_kvm
189} // namespace crow