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> |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 9 | #include <boost/container/flat_set.hpp> |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 10 | |
| 11 | namespace crow |
| 12 | { |
| 13 | namespace obmc_console |
| 14 | { |
| 15 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 16 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 17 | static std::unique_ptr<boost::asio::local::stream_protocol::socket> hostSocket; |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 18 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 19 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 20 | static std::array<char, 4096> outputBuffer; |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 21 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 22 | static std::string inputBuffer; |
| 23 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 24 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 25 | static boost::container::flat_set<crow::websocket::Connection*> sessions; |
| 26 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 27 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 28 | static bool doingWrite = false; |
| 29 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 30 | inline void doWrite() |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 31 | { |
| 32 | if (doingWrite) |
| 33 | { |
| 34 | BMCWEB_LOG_DEBUG << "Already writing. Bailing out"; |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (inputBuffer.empty()) |
| 39 | { |
| 40 | BMCWEB_LOG_DEBUG << "Outbuffer empty. Bailing out"; |
| 41 | return; |
| 42 | } |
| 43 | |
AppaRao Puli | 5238bd3 | 2020-11-17 11:03:11 +0530 | [diff] [blame] | 44 | if (!hostSocket) |
| 45 | { |
| 46 | BMCWEB_LOG_ERROR << "doWrite(): Socket closed."; |
| 47 | return; |
| 48 | } |
| 49 | |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 50 | doingWrite = true; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 51 | hostSocket->async_write_some( |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 52 | boost::asio::buffer(inputBuffer.data(), inputBuffer.size()), |
Ed Tanous | 81c4e33 | 2023-05-18 10:30:34 -0700 | [diff] [blame] | 53 | [](const boost::beast::error_code& ec, std::size_t bytesWritten) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 54 | doingWrite = false; |
| 55 | inputBuffer.erase(0, bytesWritten); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 56 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 57 | if (ec == boost::asio::error::eof) |
| 58 | { |
| 59 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 60 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 61 | session->close("Error in reading to host port"); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 62 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | if (ec) |
| 66 | { |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 67 | BMCWEB_LOG_ERROR << "Error in host serial write " << ec.message(); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | doWrite(); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 71 | }); |
| 72 | } |
| 73 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 74 | inline void doRead() |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 75 | { |
AppaRao Puli | 5238bd3 | 2020-11-17 11:03:11 +0530 | [diff] [blame] | 76 | if (!hostSocket) |
| 77 | { |
| 78 | BMCWEB_LOG_ERROR << "doRead(): Socket closed."; |
| 79 | return; |
| 80 | } |
| 81 | |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 82 | BMCWEB_LOG_DEBUG << "Reading from socket"; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 83 | hostSocket->async_read_some( |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 84 | boost::asio::buffer(outputBuffer.data(), outputBuffer.size()), |
| 85 | [](const boost::system::error_code& ec, std::size_t bytesRead) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 86 | BMCWEB_LOG_DEBUG << "read done. Read " << bytesRead << " bytes"; |
| 87 | if (ec) |
| 88 | { |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 89 | BMCWEB_LOG_ERROR << "Couldn't read from host serial port: " |
| 90 | << ec.message(); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 91 | for (crow::websocket::Connection* session : sessions) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 92 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 93 | session->close("Error in connecting to host port"); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 94 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | std::string_view payload(outputBuffer.data(), bytesRead); |
| 98 | for (crow::websocket::Connection* session : sessions) |
| 99 | { |
| 100 | session->sendBinary(payload); |
| 101 | } |
| 102 | doRead(); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 103 | }); |
| 104 | } |
| 105 | |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 106 | // If connection is active then remove it from the connection map |
| 107 | inline bool removeConnection(crow::websocket::Connection& conn) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 108 | { |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 109 | bool ret = false; |
| 110 | |
| 111 | if (sessions.erase(&conn) != 0U) |
| 112 | { |
| 113 | ret = true; |
| 114 | } |
| 115 | |
| 116 | if (sessions.empty()) |
| 117 | { |
| 118 | hostSocket = nullptr; |
| 119 | inputBuffer.clear(); |
| 120 | inputBuffer.shrink_to_fit(); |
| 121 | } |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | inline void connectConsoleSocket(crow::websocket::Connection& conn, |
| 126 | const boost::system::error_code& ec, |
| 127 | const sdbusplus::message::unix_fd& unixfd) |
| 128 | { |
| 129 | int fd = -1; |
| 130 | |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 131 | if (ec) |
| 132 | { |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 133 | BMCWEB_LOG_ERROR << "Failed to call console Connect() method" |
| 134 | << " DBUS error: " << ec.message(); |
| 135 | if (removeConnection(conn)) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 136 | { |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 137 | conn.close("Failed to call console Connect() method"); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 138 | } |
| 139 | return; |
| 140 | } |
| 141 | |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 142 | // Make sure that connection is still open. |
| 143 | if (!sessions.contains(&conn)) |
| 144 | { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | fd = dup(unixfd); |
| 149 | if (fd == -1) |
| 150 | { |
| 151 | BMCWEB_LOG_ERROR << "Failed to dup the DBUS unixfd" |
| 152 | << " error: " << strerror(errno); |
| 153 | if (removeConnection(conn)) |
| 154 | { |
| 155 | conn.close("Failed to dup the DBUS unixfd"); |
| 156 | } |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | BMCWEB_LOG_DEBUG << "Console web socket path: " << conn.req.target() |
| 161 | << " Console unix FD: " << unixfd << " duped FD: " << fd; |
| 162 | |
| 163 | if (hostSocket == nullptr) |
| 164 | { |
| 165 | boost::system::error_code ec1; |
| 166 | boost::asio::local::stream_protocol proto; |
| 167 | hostSocket = |
| 168 | std::make_unique<boost::asio::local::stream_protocol::socket>( |
| 169 | conn.getIoContext()); |
| 170 | |
| 171 | hostSocket->assign(proto, fd, ec1); |
| 172 | |
| 173 | if (ec1) |
| 174 | { |
| 175 | close(fd); |
| 176 | BMCWEB_LOG_ERROR << "Failed to assign the DBUS socket" |
| 177 | << " Socket assign error: " << ec1.message(); |
| 178 | if (removeConnection(conn)) |
| 179 | { |
| 180 | conn.close("Failed to assign the DBUS socket"); |
| 181 | } |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | conn.resumeRead(); |
| 186 | doWrite(); |
| 187 | doRead(); |
| 188 | } |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | BMCWEB_LOG_DEBUG << "Socket already exist so close the new fd: " << fd; |
| 193 | close(fd); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Query consoles from DBUS and find the matching to the |
| 198 | // rules string. |
| 199 | inline void onOpen(crow::websocket::Connection& conn) |
| 200 | { |
| 201 | BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened"; |
| 202 | |
| 203 | // Save the connection in the map |
| 204 | sessions.insert(&conn); |
| 205 | |
| 206 | // We need to wait for dbus and the websockets to hook up before data is |
| 207 | // sent/received. Tell the core to hold off messages until the sockets are |
| 208 | // up |
| 209 | if (hostSocket == nullptr) |
| 210 | { |
| 211 | conn.deferRead(); |
| 212 | } |
| 213 | |
| 214 | // The console id 'default' is used for the console0 |
| 215 | // We need to change it when we provide full multi-console support. |
| 216 | const std::string consolePath = "/xyz/openbmc_project/console/default"; |
| 217 | const std::string consoleService = "xyz.openbmc_project.Console.default"; |
| 218 | |
| 219 | BMCWEB_LOG_DEBUG << "Console Object path = " << consolePath |
| 220 | << " service = " << consoleService |
| 221 | << " Request target = " << conn.req.target(); |
| 222 | |
| 223 | // Call Connect() method to get the unix FD |
| 224 | crow::connections::systemBus->async_method_call( |
| 225 | [&conn](const boost::system::error_code& ec, |
| 226 | const sdbusplus::message::unix_fd& unixfd) { |
| 227 | connectConsoleSocket(conn, ec, unixfd); |
| 228 | }, |
| 229 | consoleService, consolePath, "xyz.openbmc_project.Console.Access", |
| 230 | "Connect"); |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 233 | inline void requestRoutes(App& app) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 234 | { |
| 235 | BMCWEB_ROUTE(app, "/console0") |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 236 | .privileges({{"OpenBMCHostConsole"}}) |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 237 | .websocket() |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 238 | .onopen(onOpen) |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 239 | .onclose([](crow::websocket::Connection& conn, |
| 240 | [[maybe_unused]] const std::string& reason) { |
AppaRao Puli | 5238bd3 | 2020-11-17 11:03:11 +0530 | [diff] [blame] | 241 | BMCWEB_LOG_INFO << "Closing websocket. Reason: " << reason; |
| 242 | |
Ninad Palsule | a8d4b8e | 2023-04-10 16:40:00 -0500 | [diff] [blame] | 243 | removeConnection(conn); |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 244 | }) |
| 245 | .onmessage([]([[maybe_unused]] crow::websocket::Connection& conn, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 246 | const std::string& data, [[maybe_unused]] bool isBinary) { |
Ed Tanous | 2daf672 | 2018-08-23 11:27:23 -0700 | [diff] [blame] | 247 | inputBuffer += data; |
| 248 | doWrite(); |
| 249 | }); |
| 250 | } |
| 251 | } // namespace obmc_console |
| 252 | } // namespace crow |