blob: 15c44de748172bfe5c2662f4ec90a125da0f850a [file] [log] [blame]
Ed Tanous2daf6722018-08-23 11:27:23 -07001#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 Tanous2daf6722018-08-23 11:27:23 -07006#include <sys/socket.h>
7
Ed Tanousd4d77e32020-08-18 00:07:28 -07008#include <boost/asio/local/stream_protocol.hpp>
Ninad Palsulef948d812023-05-25 16:53:01 -05009#include <boost/container/flat_map.hpp>
Gunnar Mills8b901d72024-05-03 16:58:32 -050010#include <boost/system/error_code.hpp>
11
12#include <array>
13#include <memory>
14#include <string>
15#include <string_view>
Ed Tanous2daf6722018-08-23 11:27:23 -070016
17namespace crow
18{
19namespace obmc_console
20{
21
Ninad Palsulef948d812023-05-25 16:53:01 -050022// Update this value each time we add new console route.
23static constexpr const uint maxSessions = 32;
Ed Tanous2daf6722018-08-23 11:27:23 -070024
Ninad Palsulef948d812023-05-25 16:53:01 -050025class ConsoleHandler : public std::enable_shared_from_this<ConsoleHandler>
Ed Tanous2daf6722018-08-23 11:27:23 -070026{
Ninad Palsulef948d812023-05-25 16:53:01 -050027 public:
28 ConsoleHandler(boost::asio::io_context& ioc,
29 crow::websocket::Connection& connIn) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -040030 hostSocket(ioc), conn(connIn)
Ninad Palsulef948d812023-05-25 16:53:01 -050031 {}
32
33 ~ConsoleHandler() = default;
34
35 ConsoleHandler(const ConsoleHandler&) = delete;
36 ConsoleHandler(ConsoleHandler&&) = delete;
37 ConsoleHandler& operator=(const ConsoleHandler&) = delete;
38 ConsoleHandler& operator=(ConsoleHandler&&) = delete;
39
40 void doWrite()
Ed Tanous2daf6722018-08-23 11:27:23 -070041 {
Ninad Palsulef948d812023-05-25 16:53:01 -050042 if (doingWrite)
Ed Tanous002d39b2022-05-31 08:59:27 -070043 {
Ed Tanous62598e32023-07-17 17:06:25 -070044 BMCWEB_LOG_DEBUG("Already writing. Bailing out");
Ed Tanous002d39b2022-05-31 08:59:27 -070045 return;
46 }
Ninad Palsulef948d812023-05-25 16:53:01 -050047
48 if (inputBuffer.empty())
49 {
Ed Tanous62598e32023-07-17 17:06:25 -070050 BMCWEB_LOG_DEBUG("Outbuffer empty. Bailing out");
Ninad Palsulef948d812023-05-25 16:53:01 -050051 return;
52 }
53
54 doingWrite = true;
55 hostSocket.async_write_some(
56 boost::asio::buffer(inputBuffer.data(), inputBuffer.size()),
57 [weak(weak_from_this())](const boost::beast::error_code& ec,
58 std::size_t bytesWritten) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040059 std::shared_ptr<ConsoleHandler> self = weak.lock();
60 if (self == nullptr)
61 {
62 return;
63 }
Ninad Palsulef948d812023-05-25 16:53:01 -050064
Patrick Williamsbd79bce2024-08-16 15:22:20 -040065 self->doingWrite = false;
66 self->inputBuffer.erase(0, bytesWritten);
Ninad Palsulef948d812023-05-25 16:53:01 -050067
Patrick Williamsbd79bce2024-08-16 15:22:20 -040068 if (ec == boost::asio::error::eof)
69 {
70 self->conn.close("Error in reading to host port");
71 return;
72 }
73 if (ec)
74 {
75 BMCWEB_LOG_ERROR("Error in host serial write {}",
76 ec.message());
77 return;
78 }
79 self->doWrite();
80 });
Ninad Palsulef948d812023-05-25 16:53:01 -050081 }
82
Ed Tanous099793f2023-05-31 08:54:26 -070083 static void afterSendEx(const std::weak_ptr<ConsoleHandler>& weak)
84 {
85 std::shared_ptr<ConsoleHandler> self = weak.lock();
86 if (self == nullptr)
87 {
88 return;
89 }
90 self->doRead();
91 }
92
Ninad Palsulef948d812023-05-25 16:53:01 -050093 void doRead()
94 {
Ed Tanous62598e32023-07-17 17:06:25 -070095 BMCWEB_LOG_DEBUG("Reading from socket");
Ninad Palsulef948d812023-05-25 16:53:01 -050096 hostSocket.async_read_some(
Ed Tanous099793f2023-05-31 08:54:26 -070097 boost::asio::buffer(outputBuffer),
Ninad Palsulef948d812023-05-25 16:53:01 -050098 [this, weakSelf(weak_from_this())](
99 const boost::system::error_code& ec, std::size_t bytesRead) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400100 BMCWEB_LOG_DEBUG("read done. Read {} bytes", bytesRead);
101 std::shared_ptr<ConsoleHandler> self = weakSelf.lock();
102 if (self == nullptr)
103 {
104 return;
105 }
106 if (ec)
107 {
108 BMCWEB_LOG_ERROR("Couldn't read from host serial port: {}",
109 ec.message());
110 conn.close("Error connecting to host port");
111 return;
112 }
113 std::string_view payload(outputBuffer.data(), bytesRead);
114 self->conn.sendEx(
115 crow::websocket::MessageType::Binary, payload,
116 std::bind_front(afterSendEx, weak_from_this()));
117 });
Ninad Palsulef948d812023-05-25 16:53:01 -0500118 }
119
120 bool connect(int fd)
121 {
122 boost::system::error_code ec;
123 boost::asio::local::stream_protocol proto;
124
125 hostSocket.assign(proto, fd, ec);
126
Ed Tanous002d39b2022-05-31 08:59:27 -0700127 if (ec)
128 {
Ed Tanous62598e32023-07-17 17:06:25 -0700129 BMCWEB_LOG_ERROR(
130 "Failed to assign the DBUS socket Socket assign error: {}",
131 ec.message());
Ninad Palsulef948d812023-05-25 16:53:01 -0500132 return false;
Ed Tanous002d39b2022-05-31 08:59:27 -0700133 }
Ninad Palsulef948d812023-05-25 16:53:01 -0500134
135 conn.resumeRead();
Ed Tanous002d39b2022-05-31 08:59:27 -0700136 doWrite();
Ninad Palsulef948d812023-05-25 16:53:01 -0500137 doRead();
138 return true;
139 }
140
141 boost::asio::local::stream_protocol::socket hostSocket;
142
Ed Tanous099793f2023-05-31 08:54:26 -0700143 std::array<char, 4096> outputBuffer{};
Ninad Palsulef948d812023-05-25 16:53:01 -0500144
145 std::string inputBuffer;
146 bool doingWrite = false;
147 crow::websocket::Connection& conn;
148};
149
150using ObmcConsoleMap = boost::container::flat_map<
151 crow::websocket::Connection*, std::shared_ptr<ConsoleHandler>, std::less<>,
152 std::vector<std::pair<crow::websocket::Connection*,
153 std::shared_ptr<ConsoleHandler>>>>;
154
155inline ObmcConsoleMap& getConsoleHandlerMap()
156{
157 static ObmcConsoleMap map;
158 return map;
Ed Tanous2daf6722018-08-23 11:27:23 -0700159}
160
Ninad Palsulef948d812023-05-25 16:53:01 -0500161// Remove connection from the connection map and if connection map is empty
162// then remove the handler from handlers map.
163inline void onClose(crow::websocket::Connection& conn, const std::string& err)
Ed Tanous2daf6722018-08-23 11:27:23 -0700164{
Ed Tanous62598e32023-07-17 17:06:25 -0700165 BMCWEB_LOG_INFO("Closing websocket. Reason: {}", err);
Ninad Palsulef948d812023-05-25 16:53:01 -0500166
167 auto iter = getConsoleHandlerMap().find(&conn);
168 if (iter == getConsoleHandlerMap().end())
AppaRao Puli5238bd32020-11-17 11:03:11 +0530169 {
Ed Tanous62598e32023-07-17 17:06:25 -0700170 BMCWEB_LOG_CRITICAL("Unable to find connection {}", logPtr(&conn));
AppaRao Puli5238bd32020-11-17 11:03:11 +0530171 return;
172 }
Ed Tanous62598e32023-07-17 17:06:25 -0700173 BMCWEB_LOG_DEBUG("Remove connection {} from obmc console", logPtr(&conn));
AppaRao Puli5238bd32020-11-17 11:03:11 +0530174
Ninad Palsulef948d812023-05-25 16:53:01 -0500175 // Removed last connection so remove the path
176 getConsoleHandlerMap().erase(iter);
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500177}
178
179inline void connectConsoleSocket(crow::websocket::Connection& conn,
180 const boost::system::error_code& ec,
181 const sdbusplus::message::unix_fd& unixfd)
182{
Ed Tanous2daf6722018-08-23 11:27:23 -0700183 if (ec)
184 {
Ed Tanous62598e32023-07-17 17:06:25 -0700185 BMCWEB_LOG_ERROR(
186 "Failed to call console Connect() method DBUS error: {}",
187 ec.message());
Ninad Palsulef948d812023-05-25 16:53:01 -0500188 conn.close("Failed to connect");
Ed Tanous2daf6722018-08-23 11:27:23 -0700189 return;
190 }
191
Ninad Palsulef948d812023-05-25 16:53:01 -0500192 // Look up the handler
193 auto iter = getConsoleHandlerMap().find(&conn);
194 if (iter == getConsoleHandlerMap().end())
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500195 {
Ed Tanous62598e32023-07-17 17:06:25 -0700196 BMCWEB_LOG_ERROR("Connection was already closed");
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500197 return;
198 }
199
Ninad Palsulef948d812023-05-25 16:53:01 -0500200 int fd = dup(unixfd);
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500201 if (fd == -1)
202 {
Ed Tanous7da633f2024-12-02 08:25:38 -0800203 BMCWEB_LOG_ERROR("Failed to dup the DBUS unixfd error");
Ninad Palsulef948d812023-05-25 16:53:01 -0500204 conn.close("Internal error");
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500205 return;
206 }
207
Ed Tanous62598e32023-07-17 17:06:25 -0700208 BMCWEB_LOG_DEBUG("Console duped FD: {}", fd);
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500209
Ninad Palsulef948d812023-05-25 16:53:01 -0500210 if (!iter->second->connect(fd))
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500211 {
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500212 close(fd);
Ninad Palsulef948d812023-05-25 16:53:01 -0500213 conn.close("Internal Error");
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500214 }
215}
216
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400217inline void processConsoleObject(
218 crow::websocket::Connection& conn, const std::string& consoleObjPath,
219 const boost::system::error_code& ec,
220 const ::dbus::utility::MapperGetObject& objInfo)
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500221{
222 // Look up the handler
223 auto iter = getConsoleHandlerMap().find(&conn);
224 if (iter == getConsoleHandlerMap().end())
225 {
Ed Tanous62598e32023-07-17 17:06:25 -0700226 BMCWEB_LOG_ERROR("Connection was already closed");
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500227 return;
228 }
229
230 if (ec)
231 {
Ed Tanous62598e32023-07-17 17:06:25 -0700232 BMCWEB_LOG_WARNING("getDbusObject() for consoles failed. DBUS error:{}",
233 ec.message());
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500234 conn.close("getDbusObject() for consoles failed.");
235 return;
236 }
237
238 const auto valueIface = objInfo.begin();
239 if (valueIface == objInfo.end())
240 {
Ed Tanous62598e32023-07-17 17:06:25 -0700241 BMCWEB_LOG_WARNING("getDbusObject() returned unexpected size: {}",
242 objInfo.size());
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500243 conn.close("getDbusObject() returned unexpected size");
244 return;
245 }
246
247 const std::string& consoleService = valueIface->first;
Ed Tanous62598e32023-07-17 17:06:25 -0700248 BMCWEB_LOG_DEBUG("Looking up unixFD for Service {} Path {}", consoleService,
249 consoleObjPath);
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500250 // Call Connect() method to get the unix FD
251 crow::connections::systemBus->async_method_call(
252 [&conn](const boost::system::error_code& ec1,
253 const sdbusplus::message::unix_fd& unixfd) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400254 connectConsoleSocket(conn, ec1, unixfd);
255 },
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500256 consoleService, consoleObjPath, "xyz.openbmc_project.Console.Access",
257 "Connect");
258}
259
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500260// Query consoles from DBUS and find the matching to the
261// rules string.
262inline void onOpen(crow::websocket::Connection& conn)
263{
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500264 std::string consoleLeaf;
265
Ed Tanous62598e32023-07-17 17:06:25 -0700266 BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn));
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500267
Ninad Palsulef948d812023-05-25 16:53:01 -0500268 if (getConsoleHandlerMap().size() >= maxSessions)
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500269 {
Ninad Palsulef948d812023-05-25 16:53:01 -0500270 conn.close("Max sessions are already connected");
271 return;
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500272 }
273
Ninad Palsulef948d812023-05-25 16:53:01 -0500274 std::shared_ptr<ConsoleHandler> handler =
275 std::make_shared<ConsoleHandler>(conn.getIoContext(), conn);
276 getConsoleHandlerMap().emplace(&conn, handler);
277
278 conn.deferRead();
279
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500280 // Keep old path for backward compatibility
281 if (conn.url().path() == "/console0")
282 {
283 consoleLeaf = "default";
284 }
285 else
286 {
287 // Get the console id from console router path and prepare the console
288 // object path and console service.
289 consoleLeaf = conn.url().segments().back();
290 }
291 std::string consolePath =
292 sdbusplus::message::object_path("/xyz/openbmc_project/console") /
293 consoleLeaf;
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500294
Ed Tanous62598e32023-07-17 17:06:25 -0700295 BMCWEB_LOG_DEBUG("Console Object path = {} Request target = {}",
296 consolePath, conn.url().path());
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500297
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500298 // mapper call lambda
299 constexpr std::array<std::string_view, 1> interfaces = {
300 "xyz.openbmc_project.Console.Access"};
301
302 dbus::utility::getDbusObject(
303 consolePath, interfaces,
304 [&conn, consolePath](const boost::system::error_code& ec,
305 const ::dbus::utility::MapperGetObject& objInfo) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400306 processConsoleObject(conn, consolePath, ec, objInfo);
307 });
Ed Tanous2daf6722018-08-23 11:27:23 -0700308}
309
Ninad Palsulef948d812023-05-25 16:53:01 -0500310inline void onMessage(crow::websocket::Connection& conn,
311 const std::string& data, bool /*isBinary*/)
312{
313 auto handler = getConsoleHandlerMap().find(&conn);
314 if (handler == getConsoleHandlerMap().end())
315 {
Ed Tanous62598e32023-07-17 17:06:25 -0700316 BMCWEB_LOG_CRITICAL("Unable to find connection {}", logPtr(&conn));
Ninad Palsulef948d812023-05-25 16:53:01 -0500317 return;
318 }
319 handler->second->inputBuffer += data;
320 handler->second->doWrite();
321}
322
Ed Tanous23a21a12020-07-25 04:45:05 +0000323inline void requestRoutes(App& app)
Ed Tanous2daf6722018-08-23 11:27:23 -0700324{
325 BMCWEB_ROUTE(app, "/console0")
Ninad Palsule3e72c202023-03-27 17:19:55 -0500326 .privileges({{"OpenBMCHostConsole"}})
Ed Tanous2daf6722018-08-23 11:27:23 -0700327 .websocket()
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500328 .onopen(onOpen)
Ninad Palsulef948d812023-05-25 16:53:01 -0500329 .onclose(onClose)
330 .onmessage(onMessage);
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500331
332 BMCWEB_ROUTE(app, "/console/<str>")
333 .privileges({{"OpenBMCHostConsole"}})
334 .websocket()
335 .onopen(onOpen)
336 .onclose(onClose)
337 .onmessage(onMessage);
Ed Tanous2daf6722018-08-23 11:27:23 -0700338}
339} // namespace obmc_console
340} // namespace crow