blob: 842cbe6307d024392a494a562c9503edb6acf7ab [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) :
30 hostSocket(ioc),
31 conn(connIn)
32 {}
33
34 ~ConsoleHandler() = default;
35
36 ConsoleHandler(const ConsoleHandler&) = delete;
37 ConsoleHandler(ConsoleHandler&&) = delete;
38 ConsoleHandler& operator=(const ConsoleHandler&) = delete;
39 ConsoleHandler& operator=(ConsoleHandler&&) = delete;
40
41 void doWrite()
Ed Tanous2daf6722018-08-23 11:27:23 -070042 {
Ninad Palsulef948d812023-05-25 16:53:01 -050043 if (doingWrite)
Ed Tanous002d39b2022-05-31 08:59:27 -070044 {
Ed Tanous62598e32023-07-17 17:06:25 -070045 BMCWEB_LOG_DEBUG("Already writing. Bailing out");
Ed Tanous002d39b2022-05-31 08:59:27 -070046 return;
47 }
Ninad Palsulef948d812023-05-25 16:53:01 -050048
49 if (inputBuffer.empty())
50 {
Ed Tanous62598e32023-07-17 17:06:25 -070051 BMCWEB_LOG_DEBUG("Outbuffer empty. Bailing out");
Ninad Palsulef948d812023-05-25 16:53:01 -050052 return;
53 }
54
55 doingWrite = true;
56 hostSocket.async_write_some(
57 boost::asio::buffer(inputBuffer.data(), inputBuffer.size()),
58 [weak(weak_from_this())](const boost::beast::error_code& ec,
59 std::size_t bytesWritten) {
60 std::shared_ptr<ConsoleHandler> self = weak.lock();
61 if (self == nullptr)
62 {
63 return;
64 }
65
66 self->doingWrite = false;
67 self->inputBuffer.erase(0, bytesWritten);
68
69 if (ec == boost::asio::error::eof)
70 {
71 self->conn.close("Error in reading to host port");
72 return;
73 }
74 if (ec)
75 {
Ed Tanous62598e32023-07-17 17:06:25 -070076 BMCWEB_LOG_ERROR("Error in host serial write {}", ec.message());
Ninad Palsulef948d812023-05-25 16:53:01 -050077 return;
78 }
79 self->doWrite();
Patrick Williams5a39f772023-10-20 11:20:21 -050080 });
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) {
Ed Tanous62598e32023-07-17 17:06:25 -0700100 BMCWEB_LOG_DEBUG("read done. Read {} bytes", bytesRead);
Ninad Palsulef948d812023-05-25 16:53:01 -0500101 std::shared_ptr<ConsoleHandler> self = weakSelf.lock();
102 if (self == nullptr)
103 {
104 return;
105 }
106 if (ec)
107 {
Ed Tanous62598e32023-07-17 17:06:25 -0700108 BMCWEB_LOG_ERROR("Couldn't read from host serial port: {}",
109 ec.message());
Ninad Palsulef948d812023-05-25 16:53:01 -0500110 conn.close("Error connecting to host port");
111 return;
112 }
Ed Tanous099793f2023-05-31 08:54:26 -0700113 std::string_view payload(outputBuffer.data(), bytesRead);
114 self->conn.sendEx(crow::websocket::MessageType::Binary, payload,
115 std::bind_front(afterSendEx, weak_from_this()));
Patrick Williams5a39f772023-10-20 11:20:21 -0500116 });
Ninad Palsulef948d812023-05-25 16:53:01 -0500117 }
118
119 bool connect(int fd)
120 {
121 boost::system::error_code ec;
122 boost::asio::local::stream_protocol proto;
123
124 hostSocket.assign(proto, fd, ec);
125
Ed Tanous002d39b2022-05-31 08:59:27 -0700126 if (ec)
127 {
Ed Tanous62598e32023-07-17 17:06:25 -0700128 BMCWEB_LOG_ERROR(
129 "Failed to assign the DBUS socket Socket assign error: {}",
130 ec.message());
Ninad Palsulef948d812023-05-25 16:53:01 -0500131 return false;
Ed Tanous002d39b2022-05-31 08:59:27 -0700132 }
Ninad Palsulef948d812023-05-25 16:53:01 -0500133
134 conn.resumeRead();
Ed Tanous002d39b2022-05-31 08:59:27 -0700135 doWrite();
Ninad Palsulef948d812023-05-25 16:53:01 -0500136 doRead();
137 return true;
138 }
139
140 boost::asio::local::stream_protocol::socket hostSocket;
141
Ed Tanous099793f2023-05-31 08:54:26 -0700142 std::array<char, 4096> outputBuffer{};
Ninad Palsulef948d812023-05-25 16:53:01 -0500143
144 std::string inputBuffer;
145 bool doingWrite = false;
146 crow::websocket::Connection& conn;
147};
148
149using ObmcConsoleMap = boost::container::flat_map<
150 crow::websocket::Connection*, std::shared_ptr<ConsoleHandler>, std::less<>,
151 std::vector<std::pair<crow::websocket::Connection*,
152 std::shared_ptr<ConsoleHandler>>>>;
153
154inline ObmcConsoleMap& getConsoleHandlerMap()
155{
156 static ObmcConsoleMap map;
157 return map;
Ed Tanous2daf6722018-08-23 11:27:23 -0700158}
159
Ninad Palsulef948d812023-05-25 16:53:01 -0500160// Remove connection from the connection map and if connection map is empty
161// then remove the handler from handlers map.
162inline void onClose(crow::websocket::Connection& conn, const std::string& err)
Ed Tanous2daf6722018-08-23 11:27:23 -0700163{
Ed Tanous62598e32023-07-17 17:06:25 -0700164 BMCWEB_LOG_INFO("Closing websocket. Reason: {}", err);
Ninad Palsulef948d812023-05-25 16:53:01 -0500165
166 auto iter = getConsoleHandlerMap().find(&conn);
167 if (iter == getConsoleHandlerMap().end())
AppaRao Puli5238bd32020-11-17 11:03:11 +0530168 {
Ed Tanous62598e32023-07-17 17:06:25 -0700169 BMCWEB_LOG_CRITICAL("Unable to find connection {}", logPtr(&conn));
AppaRao Puli5238bd32020-11-17 11:03:11 +0530170 return;
171 }
Ed Tanous62598e32023-07-17 17:06:25 -0700172 BMCWEB_LOG_DEBUG("Remove connection {} from obmc console", logPtr(&conn));
AppaRao Puli5238bd32020-11-17 11:03:11 +0530173
Ninad Palsulef948d812023-05-25 16:53:01 -0500174 // Removed last connection so remove the path
175 getConsoleHandlerMap().erase(iter);
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500176}
177
178inline void connectConsoleSocket(crow::websocket::Connection& conn,
179 const boost::system::error_code& ec,
180 const sdbusplus::message::unix_fd& unixfd)
181{
Ed Tanous2daf6722018-08-23 11:27:23 -0700182 if (ec)
183 {
Ed Tanous62598e32023-07-17 17:06:25 -0700184 BMCWEB_LOG_ERROR(
185 "Failed to call console Connect() method DBUS error: {}",
186 ec.message());
Ninad Palsulef948d812023-05-25 16:53:01 -0500187 conn.close("Failed to connect");
Ed Tanous2daf6722018-08-23 11:27:23 -0700188 return;
189 }
190
Ninad Palsulef948d812023-05-25 16:53:01 -0500191 // Look up the handler
192 auto iter = getConsoleHandlerMap().find(&conn);
193 if (iter == getConsoleHandlerMap().end())
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500194 {
Ed Tanous62598e32023-07-17 17:06:25 -0700195 BMCWEB_LOG_ERROR("Connection was already closed");
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500196 return;
197 }
198
Ninad Palsulef948d812023-05-25 16:53:01 -0500199 int fd = dup(unixfd);
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500200 if (fd == -1)
201 {
Ed Tanous62598e32023-07-17 17:06:25 -0700202 BMCWEB_LOG_ERROR("Failed to dup the DBUS unixfd error: {}",
203 strerror(errno));
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
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500217inline void
218 processConsoleObject(crow::websocket::Connection& conn,
219 const std::string& consoleObjPath,
220 const boost::system::error_code& ec,
221 const ::dbus::utility::MapperGetObject& objInfo)
222{
223 // Look up the handler
224 auto iter = getConsoleHandlerMap().find(&conn);
225 if (iter == getConsoleHandlerMap().end())
226 {
Ed Tanous62598e32023-07-17 17:06:25 -0700227 BMCWEB_LOG_ERROR("Connection was already closed");
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500228 return;
229 }
230
231 if (ec)
232 {
Ed Tanous62598e32023-07-17 17:06:25 -0700233 BMCWEB_LOG_WARNING("getDbusObject() for consoles failed. DBUS error:{}",
234 ec.message());
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500235 conn.close("getDbusObject() for consoles failed.");
236 return;
237 }
238
239 const auto valueIface = objInfo.begin();
240 if (valueIface == objInfo.end())
241 {
Ed Tanous62598e32023-07-17 17:06:25 -0700242 BMCWEB_LOG_WARNING("getDbusObject() returned unexpected size: {}",
243 objInfo.size());
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500244 conn.close("getDbusObject() returned unexpected size");
245 return;
246 }
247
248 const std::string& consoleService = valueIface->first;
Ed Tanous62598e32023-07-17 17:06:25 -0700249 BMCWEB_LOG_DEBUG("Looking up unixFD for Service {} Path {}", consoleService,
250 consoleObjPath);
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500251 // Call Connect() method to get the unix FD
252 crow::connections::systemBus->async_method_call(
253 [&conn](const boost::system::error_code& ec1,
254 const sdbusplus::message::unix_fd& unixfd) {
255 connectConsoleSocket(conn, ec1, unixfd);
Patrick Williams5a39f772023-10-20 11:20:21 -0500256 },
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500257 consoleService, consoleObjPath, "xyz.openbmc_project.Console.Access",
258 "Connect");
259}
260
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500261// Query consoles from DBUS and find the matching to the
262// rules string.
263inline void onOpen(crow::websocket::Connection& conn)
264{
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500265 std::string consoleLeaf;
266
Ed Tanous62598e32023-07-17 17:06:25 -0700267 BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn));
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500268
Ninad Palsulef948d812023-05-25 16:53:01 -0500269 if (getConsoleHandlerMap().size() >= maxSessions)
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500270 {
Ninad Palsulef948d812023-05-25 16:53:01 -0500271 conn.close("Max sessions are already connected");
272 return;
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500273 }
274
Ninad Palsulef948d812023-05-25 16:53:01 -0500275 std::shared_ptr<ConsoleHandler> handler =
276 std::make_shared<ConsoleHandler>(conn.getIoContext(), conn);
277 getConsoleHandlerMap().emplace(&conn, handler);
278
279 conn.deferRead();
280
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500281 // Keep old path for backward compatibility
282 if (conn.url().path() == "/console0")
283 {
284 consoleLeaf = "default";
285 }
286 else
287 {
288 // Get the console id from console router path and prepare the console
289 // object path and console service.
290 consoleLeaf = conn.url().segments().back();
291 }
292 std::string consolePath =
293 sdbusplus::message::object_path("/xyz/openbmc_project/console") /
294 consoleLeaf;
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500295
Ed Tanous62598e32023-07-17 17:06:25 -0700296 BMCWEB_LOG_DEBUG("Console Object path = {} Request target = {}",
297 consolePath, conn.url().path());
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500298
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500299 // mapper call lambda
300 constexpr std::array<std::string_view, 1> interfaces = {
301 "xyz.openbmc_project.Console.Access"};
302
303 dbus::utility::getDbusObject(
304 consolePath, interfaces,
305 [&conn, consolePath](const boost::system::error_code& ec,
306 const ::dbus::utility::MapperGetObject& objInfo) {
307 processConsoleObject(conn, consolePath, ec, objInfo);
Patrick Williams5a39f772023-10-20 11:20:21 -0500308 });
Ed Tanous2daf6722018-08-23 11:27:23 -0700309}
310
Ninad Palsulef948d812023-05-25 16:53:01 -0500311inline void onMessage(crow::websocket::Connection& conn,
312 const std::string& data, bool /*isBinary*/)
313{
314 auto handler = getConsoleHandlerMap().find(&conn);
315 if (handler == getConsoleHandlerMap().end())
316 {
Ed Tanous62598e32023-07-17 17:06:25 -0700317 BMCWEB_LOG_CRITICAL("Unable to find connection {}", logPtr(&conn));
Ninad Palsulef948d812023-05-25 16:53:01 -0500318 return;
319 }
320 handler->second->inputBuffer += data;
321 handler->second->doWrite();
322}
323
Ed Tanous23a21a12020-07-25 04:45:05 +0000324inline void requestRoutes(App& app)
Ed Tanous2daf6722018-08-23 11:27:23 -0700325{
326 BMCWEB_ROUTE(app, "/console0")
Ninad Palsule3e72c202023-03-27 17:19:55 -0500327 .privileges({{"OpenBMCHostConsole"}})
Ed Tanous2daf6722018-08-23 11:27:23 -0700328 .websocket()
Ninad Palsulea8d4b8e2023-04-10 16:40:00 -0500329 .onopen(onOpen)
Ninad Palsulef948d812023-05-25 16:53:01 -0500330 .onclose(onClose)
331 .onmessage(onMessage);
Ninad Palsule052bcbf2023-05-30 11:10:58 -0500332
333 BMCWEB_ROUTE(app, "/console/<str>")
334 .privileges({{"OpenBMCHostConsole"}})
335 .websocket()
336 .onopen(onOpen)
337 .onclose(onClose)
338 .onmessage(onMessage);
Ed Tanous2daf6722018-08-23 11:27:23 -0700339}
340} // namespace obmc_console
341} // namespace crow