Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [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 | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 6 | #include <sys/socket.h> |
| 7 | |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 8 | #include <boost/asio/local/stream_protocol.hpp> |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 9 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 10 | |
| 11 | namespace crow |
| 12 | { |
| 13 | namespace obmc_console |
| 14 | { |
| 15 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 16 | // Update this value each time we add new console route. |
| 17 | static constexpr const uint maxSessions = 32; |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 18 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 19 | class ConsoleHandler : public std::enable_shared_from_this<ConsoleHandler> |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 20 | { |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 21 | public: |
| 22 | ConsoleHandler(boost::asio::io_context& ioc, |
| 23 | crow::websocket::Connection& connIn) : |
| 24 | hostSocket(ioc), |
| 25 | conn(connIn) |
| 26 | {} |
| 27 | |
| 28 | ~ConsoleHandler() = default; |
| 29 | |
| 30 | ConsoleHandler(const ConsoleHandler&) = delete; |
| 31 | ConsoleHandler(ConsoleHandler&&) = delete; |
| 32 | ConsoleHandler& operator=(const ConsoleHandler&) = delete; |
| 33 | ConsoleHandler& operator=(ConsoleHandler&&) = delete; |
| 34 | |
| 35 | void doWrite() |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 36 | { |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 37 | if (doingWrite) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 38 | { |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 39 | BMCWEB_LOG_DEBUG << "Already writing. Bailing out"; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 40 | return; |
| 41 | } |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 42 | |
| 43 | if (inputBuffer.empty()) |
| 44 | { |
| 45 | BMCWEB_LOG_DEBUG << "Outbuffer empty. Bailing out"; |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | doingWrite = true; |
| 50 | hostSocket.async_write_some( |
| 51 | boost::asio::buffer(inputBuffer.data(), inputBuffer.size()), |
| 52 | [weak(weak_from_this())](const boost::beast::error_code& ec, |
| 53 | std::size_t bytesWritten) { |
| 54 | std::shared_ptr<ConsoleHandler> self = weak.lock(); |
| 55 | if (self == nullptr) |
| 56 | { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | self->doingWrite = false; |
| 61 | self->inputBuffer.erase(0, bytesWritten); |
| 62 | |
| 63 | if (ec == boost::asio::error::eof) |
| 64 | { |
| 65 | self->conn.close("Error in reading to host port"); |
| 66 | return; |
| 67 | } |
| 68 | if (ec) |
| 69 | { |
| 70 | BMCWEB_LOG_ERROR << "Error in host serial write " |
| 71 | << ec.message(); |
| 72 | return; |
| 73 | } |
| 74 | self->doWrite(); |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | void doRead() |
| 79 | { |
| 80 | std::size_t bytes = outputBuffer.capacity() - outputBuffer.size(); |
| 81 | |
| 82 | BMCWEB_LOG_DEBUG << "Reading from socket"; |
| 83 | hostSocket.async_read_some( |
| 84 | outputBuffer.prepare(bytes), |
| 85 | [this, weakSelf(weak_from_this())]( |
| 86 | const boost::system::error_code& ec, std::size_t bytesRead) { |
| 87 | BMCWEB_LOG_DEBUG << "read done. Read " << bytesRead << " bytes"; |
| 88 | std::shared_ptr<ConsoleHandler> self = weakSelf.lock(); |
| 89 | if (self == nullptr) |
| 90 | { |
| 91 | return; |
| 92 | } |
| 93 | if (ec) |
| 94 | { |
| 95 | BMCWEB_LOG_ERROR << "Couldn't read from host serial port: " |
| 96 | << ec.message(); |
| 97 | conn.close("Error connecting to host port"); |
| 98 | return; |
| 99 | } |
| 100 | outputBuffer.commit(bytesRead); |
| 101 | std::string_view payload( |
| 102 | static_cast<const char*>(outputBuffer.data().data()), |
| 103 | bytesRead); |
| 104 | conn.sendBinary(payload); |
| 105 | outputBuffer.consume(bytesRead); |
| 106 | doRead(); |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | bool connect(int fd) |
| 111 | { |
| 112 | boost::system::error_code ec; |
| 113 | boost::asio::local::stream_protocol proto; |
| 114 | |
| 115 | hostSocket.assign(proto, fd, ec); |
| 116 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 117 | if (ec) |
| 118 | { |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 119 | BMCWEB_LOG_ERROR << "Failed to assign the DBUS socket" |
| 120 | << " Socket assign error: " << ec.message(); |
| 121 | return false; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 122 | } |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 123 | |
| 124 | conn.resumeRead(); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 125 | doWrite(); |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 126 | doRead(); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | boost::asio::local::stream_protocol::socket hostSocket; |
| 131 | |
| 132 | boost::beast::flat_static_buffer<4096> outputBuffer; |
| 133 | |
| 134 | std::string inputBuffer; |
| 135 | bool doingWrite = false; |
| 136 | crow::websocket::Connection& conn; |
| 137 | }; |
| 138 | |
| 139 | using ObmcConsoleMap = boost::container::flat_map< |
| 140 | crow::websocket::Connection*, std::shared_ptr<ConsoleHandler>, std::less<>, |
| 141 | std::vector<std::pair<crow::websocket::Connection*, |
| 142 | std::shared_ptr<ConsoleHandler>>>>; |
| 143 | |
| 144 | inline ObmcConsoleMap& getConsoleHandlerMap() |
| 145 | { |
| 146 | static ObmcConsoleMap map; |
| 147 | return map; |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 150 | // Remove connection from the connection map and if connection map is empty |
| 151 | // then remove the handler from handlers map. |
| 152 | inline void onClose(crow::websocket::Connection& conn, const std::string& err) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 153 | { |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 154 | BMCWEB_LOG_INFO << "Closing websocket. Reason: " << err; |
| 155 | |
| 156 | auto iter = getConsoleHandlerMap().find(&conn); |
| 157 | if (iter == getConsoleHandlerMap().end()) |
AppaRao Puli | 5238bd3 | 2020-11-17 11:03:11 +0530 | [diff] [blame] | 158 | { |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 159 | BMCWEB_LOG_CRITICAL << "Unable to find connection " << &conn; |
AppaRao Puli | 5238bd3 | 2020-11-17 11:03:11 +0530 | [diff] [blame] | 160 | return; |
| 161 | } |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 162 | BMCWEB_LOG_DEBUG << "Remove connection " << &conn << " from obmc console"; |
AppaRao Puli | 5238bd3 | 2020-11-17 11:03:11 +0530 | [diff] [blame] | 163 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 164 | // Removed last connection so remove the path |
| 165 | getConsoleHandlerMap().erase(iter); |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | inline void connectConsoleSocket(crow::websocket::Connection& conn, |
| 169 | const boost::system::error_code& ec, |
| 170 | const sdbusplus::message::unix_fd& unixfd) |
| 171 | { |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 172 | if (ec) |
| 173 | { |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 174 | BMCWEB_LOG_ERROR << "Failed to call console Connect() method" |
| 175 | << " DBUS error: " << ec.message(); |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 176 | conn.close("Failed to connect"); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 180 | // Look up the handler |
| 181 | auto iter = getConsoleHandlerMap().find(&conn); |
| 182 | if (iter == getConsoleHandlerMap().end()) |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 183 | { |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 184 | BMCWEB_LOG_ERROR << "Failed to find the handler"; |
| 185 | conn.close("Internal error"); |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 189 | int fd = dup(unixfd); |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 190 | if (fd == -1) |
| 191 | { |
| 192 | BMCWEB_LOG_ERROR << "Failed to dup the DBUS unixfd" |
| 193 | << " error: " << strerror(errno); |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 194 | conn.close("Internal error"); |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
| 198 | BMCWEB_LOG_DEBUG << "Console web socket path: " << conn.req.target() |
| 199 | << " Console unix FD: " << unixfd << " duped FD: " << fd; |
| 200 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 201 | if (!iter->second->connect(fd)) |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 202 | { |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 203 | close(fd); |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 204 | conn.close("Internal Error"); |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | // Query consoles from DBUS and find the matching to the |
| 209 | // rules string. |
| 210 | inline void onOpen(crow::websocket::Connection& conn) |
| 211 | { |
| 212 | BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened"; |
| 213 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 214 | if (getConsoleHandlerMap().size() >= maxSessions) |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 215 | { |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 216 | conn.close("Max sessions are already connected"); |
| 217 | return; |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 220 | std::shared_ptr<ConsoleHandler> handler = |
| 221 | std::make_shared<ConsoleHandler>(conn.getIoContext(), conn); |
| 222 | getConsoleHandlerMap().emplace(&conn, handler); |
| 223 | |
| 224 | conn.deferRead(); |
| 225 | |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 226 | // The console id 'default' is used for the console0 |
| 227 | // We need to change it when we provide full multi-console support. |
| 228 | const std::string consolePath = "/xyz/openbmc_project/console/default"; |
| 229 | const std::string consoleService = "xyz.openbmc_project.Console.default"; |
| 230 | |
| 231 | BMCWEB_LOG_DEBUG << "Console Object path = " << consolePath |
| 232 | << " service = " << consoleService |
| 233 | << " Request target = " << conn.req.target(); |
| 234 | |
| 235 | // Call Connect() method to get the unix FD |
| 236 | crow::connections::systemBus->async_method_call( |
| 237 | [&conn](const boost::system::error_code& ec, |
| 238 | const sdbusplus::message::unix_fd& unixfd) { |
| 239 | connectConsoleSocket(conn, ec, unixfd); |
| 240 | }, |
| 241 | consoleService, consolePath, "xyz.openbmc_project.Console.Access", |
| 242 | "Connect"); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 245 | inline void onMessage(crow::websocket::Connection& conn, |
| 246 | const std::string& data, bool /*isBinary*/) |
| 247 | { |
| 248 | auto handler = getConsoleHandlerMap().find(&conn); |
| 249 | if (handler == getConsoleHandlerMap().end()) |
| 250 | { |
| 251 | BMCWEB_LOG_CRITICAL << "Unable to find connection " << &conn; |
| 252 | return; |
| 253 | } |
| 254 | handler->second->inputBuffer += data; |
| 255 | handler->second->doWrite(); |
| 256 | } |
| 257 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 258 | inline void requestRoutes(App& app) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 259 | { |
| 260 | BMCWEB_ROUTE(app, "/console0") |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 261 | .privileges({{"OpenBMCHostConsole"}}) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 262 | .websocket() |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 263 | .onopen(onOpen) |
Ninad Palsule | f948d81 | 2023-05-25 16:53:01 -0500 | [diff] [blame] | 264 | .onclose(onClose) |
| 265 | .onmessage(onMessage); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 266 | } |
| 267 | } // namespace obmc_console |
| 268 | } // namespace crow |