Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 5 | #include "bmcweb_config.h" |
| 6 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 7 | #include "app.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 8 | #include "dbus_singleton.hpp" |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 9 | #include "dbus_utility.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 10 | #include "logging.hpp" |
Ed Tanous | faf100f | 2023-05-25 10:03:14 -0700 | [diff] [blame] | 11 | #include "websocket.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 12 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 13 | // NOLINTNEXTLINE(modernize-deprecated-headers) |
| 14 | #include <signal.h> |
| 15 | |
| 16 | #include <boost/asio/buffer.hpp> |
| 17 | #include <boost/asio/error.hpp> |
| 18 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 19 | #include <boost/asio/local/stream_protocol.hpp> |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 20 | #include <boost/asio/readable_pipe.hpp> |
| 21 | #include <boost/asio/writable_pipe.hpp> |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 22 | #include <boost/beast/core/buffers_to_string.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 23 | #include <boost/beast/core/error.hpp> |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 24 | #include <boost/beast/core/flat_static_buffer.hpp> |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 25 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 26 | #include <boost/process/v2/process.hpp> |
| 27 | #include <boost/process/v2/stdio.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 28 | #include <sdbusplus/message/native_types.hpp> |
| 29 | #include <sdbusplus/unpack_properties.hpp> |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 30 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 31 | #include <cerrno> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 32 | #include <csignal> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 33 | #include <cstddef> |
| 34 | #include <filesystem> |
| 35 | #include <format> |
| 36 | #include <functional> |
| 37 | #include <memory> |
| 38 | #include <string> |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 39 | #include <string_view> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 40 | #include <system_error> |
| 41 | #include <utility> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 42 | |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 43 | namespace crow |
| 44 | { |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 45 | |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 46 | namespace obmc_vm |
| 47 | { |
| 48 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 49 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 50 | static crow::websocket::Connection* session = nullptr; |
| 51 | |
| 52 | // The max network block device buffer size is 128kb plus 16bytes |
| 53 | // for the message header: |
| 54 | // https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md#simple-reply-message |
Troy Lee | 4ee8f0b | 2021-08-02 11:08:26 +0800 | [diff] [blame] | 55 | static constexpr auto nbdBufferSize = (128 * 1024 + 16) * 4; |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 56 | |
| 57 | class Handler : public std::enable_shared_from_this<Handler> |
| 58 | { |
| 59 | public: |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 60 | Handler(const std::string& media, boost::asio::io_context& ios) : |
| 61 | pipeOut(ios), pipeIn(ios), |
| 62 | proxy(ios, "/usr/bin/nbd-proxy", {media}, |
| 63 | boost::process::v2::process_stdio{ |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 64 | .in = pipeIn, .out = pipeOut, .err = nullptr}) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 65 | {} |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 66 | |
Ed Tanous | 0c0084a | 2019-10-24 15:57:51 -0700 | [diff] [blame] | 67 | ~Handler() = default; |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 68 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 69 | Handler(const Handler&) = delete; |
| 70 | Handler(Handler&&) = delete; |
| 71 | Handler& operator=(const Handler&) = delete; |
| 72 | Handler& operator=(Handler&&) = delete; |
| 73 | |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 74 | void doClose() |
| 75 | { |
| 76 | // boost::process::child::terminate uses SIGKILL, need to send SIGTERM |
| 77 | // to allow the proxy to stop nbd-client and the USB device gadget. |
| 78 | int rc = kill(proxy.id(), SIGTERM); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 79 | if (rc != 0) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 80 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 81 | BMCWEB_LOG_ERROR("Failed to terminate nbd-proxy: {}", errno); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 82 | return; |
| 83 | } |
Troy Lee | 36ecbf3 | 2021-08-17 18:15:28 +0800 | [diff] [blame] | 84 | |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 85 | proxy.wait(); |
| 86 | } |
| 87 | |
| 88 | void connect() |
| 89 | { |
| 90 | std::error_code ec; |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 91 | if (ec) |
| 92 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 93 | BMCWEB_LOG_ERROR("Couldn't connect to nbd-proxy: {}", ec.message()); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 94 | if (session != nullptr) |
| 95 | { |
| 96 | session->close("Error connecting to nbd-proxy"); |
| 97 | } |
| 98 | return; |
| 99 | } |
| 100 | doWrite(); |
| 101 | doRead(); |
| 102 | } |
| 103 | |
| 104 | void doWrite() |
| 105 | { |
| 106 | if (doingWrite) |
| 107 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 108 | BMCWEB_LOG_DEBUG("Already writing. Bailing out"); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 112 | if (inputBuffer.size() == 0) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 113 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 114 | BMCWEB_LOG_DEBUG("inputBuffer empty. Bailing out"); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | doingWrite = true; |
| 119 | pipeIn.async_write_some( |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 120 | inputBuffer.data(), |
Ed Tanous | 81c4e33 | 2023-05-18 10:30:34 -0700 | [diff] [blame] | 121 | [this, self(shared_from_this())](const boost::beast::error_code& ec, |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 122 | std::size_t bytesWritten) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 123 | BMCWEB_LOG_DEBUG("Wrote {}bytes", bytesWritten); |
| 124 | doingWrite = false; |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 125 | inputBuffer.consume(bytesWritten); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 126 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 127 | if (session == nullptr) |
| 128 | { |
| 129 | return; |
| 130 | } |
| 131 | if (ec == boost::asio::error::eof) |
| 132 | { |
| 133 | session->close("VM socket port closed"); |
| 134 | return; |
| 135 | } |
| 136 | if (ec) |
| 137 | { |
| 138 | session->close("Error in writing to proxy port"); |
| 139 | BMCWEB_LOG_ERROR("Error in VM socket write {}", ec); |
| 140 | return; |
| 141 | } |
| 142 | doWrite(); |
| 143 | }); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void doRead() |
| 147 | { |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 148 | std::size_t bytes = outputBuffer.capacity() - outputBuffer.size(); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 149 | |
| 150 | pipeOut.async_read_some( |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 151 | outputBuffer.prepare(bytes), |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 152 | [this, self(shared_from_this())]( |
| 153 | const boost::system::error_code& ec, std::size_t bytesRead) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 154 | BMCWEB_LOG_DEBUG("Read done. Read {} bytes", bytesRead); |
| 155 | if (ec) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 156 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 157 | BMCWEB_LOG_ERROR("Couldn't read from VM port: {}", ec); |
| 158 | if (session != nullptr) |
| 159 | { |
| 160 | session->close("Error in connecting to VM port"); |
| 161 | } |
| 162 | return; |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 163 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 164 | if (session == nullptr) |
| 165 | { |
| 166 | return; |
| 167 | } |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 168 | |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 169 | outputBuffer.commit(bytesRead); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 170 | std::string_view payload( |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 171 | static_cast<const char*>(outputBuffer.data().data()), |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 172 | bytesRead); |
| 173 | session->sendBinary(payload); |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 174 | outputBuffer.consume(bytesRead); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 175 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 176 | doRead(); |
| 177 | }); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 178 | } |
| 179 | |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 180 | boost::asio::readable_pipe pipeOut; |
| 181 | boost::asio::writable_pipe pipeIn; |
| 182 | boost::process::v2::process proxy; |
Ed Tanous | f5b191a | 2022-02-15 11:30:39 -0800 | [diff] [blame] | 183 | bool doingWrite{false}; |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 184 | |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 185 | boost::beast::flat_static_buffer<nbdBufferSize> outputBuffer; |
| 186 | boost::beast::flat_static_buffer<nbdBufferSize> inputBuffer; |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 187 | }; |
| 188 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 189 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 190 | static std::shared_ptr<Handler> handler; |
| 191 | |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 192 | } // namespace obmc_vm |
| 193 | |
| 194 | namespace nbd_proxy |
| 195 | { |
| 196 | using boost::asio::local::stream_protocol; |
| 197 | |
| 198 | // The max network block device buffer size is 128kb plus 16bytes |
| 199 | // for the message header: |
| 200 | // https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md#simple-reply-message |
| 201 | static constexpr auto nbdBufferSize = (128 * 1024 + 16) * 4; |
| 202 | |
| 203 | struct NbdProxyServer : std::enable_shared_from_this<NbdProxyServer> |
| 204 | { |
| 205 | NbdProxyServer(crow::websocket::Connection& connIn, |
| 206 | const std::string& socketIdIn, |
| 207 | const std::string& endpointIdIn, const std::string& pathIn) : |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 208 | socketId(socketIdIn), endpointId(endpointIdIn), path(pathIn), |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 209 | |
| 210 | peerSocket(connIn.getIoContext()), |
| 211 | acceptor(connIn.getIoContext(), stream_protocol::endpoint(socketId)), |
| 212 | connection(connIn) |
| 213 | {} |
| 214 | |
| 215 | NbdProxyServer(const NbdProxyServer&) = delete; |
| 216 | NbdProxyServer(NbdProxyServer&&) = delete; |
| 217 | NbdProxyServer& operator=(const NbdProxyServer&) = delete; |
| 218 | NbdProxyServer& operator=(NbdProxyServer&&) = delete; |
| 219 | |
| 220 | ~NbdProxyServer() |
| 221 | { |
| 222 | BMCWEB_LOG_DEBUG("NbdProxyServer destructor"); |
| 223 | |
| 224 | BMCWEB_LOG_DEBUG("peerSocket->close()"); |
| 225 | boost::system::error_code ec; |
| 226 | peerSocket.close(ec); |
| 227 | |
| 228 | BMCWEB_LOG_DEBUG("std::filesystem::remove({})", socketId); |
| 229 | std::error_code ec2; |
| 230 | std::filesystem::remove(socketId.c_str(), ec2); |
| 231 | if (ec2) |
| 232 | { |
| 233 | BMCWEB_LOG_DEBUG("Failed to remove file, ignoring"); |
| 234 | } |
| 235 | |
| 236 | crow::connections::systemBus->async_method_call( |
| 237 | dbus::utility::logError, "xyz.openbmc_project.VirtualMedia", path, |
| 238 | "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount"); |
| 239 | } |
| 240 | |
| 241 | std::string getEndpointId() const |
| 242 | { |
| 243 | return endpointId; |
| 244 | } |
| 245 | |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 246 | static void afterMount(const std::weak_ptr<NbdProxyServer>& weak, |
| 247 | const boost::system::error_code& ec, |
| 248 | bool /*isBinary*/) |
| 249 | { |
| 250 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 251 | if (self == nullptr) |
| 252 | { |
| 253 | return; |
| 254 | } |
| 255 | if (ec) |
| 256 | { |
| 257 | BMCWEB_LOG_ERROR("DBus error: cannot call mount method = {}", |
| 258 | ec.message()); |
| 259 | |
| 260 | self->connection.close("Failed to mount media"); |
| 261 | return; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | static void afterAccept(const std::weak_ptr<NbdProxyServer>& weak, |
| 266 | const boost::system::error_code& ec, |
| 267 | stream_protocol::socket socket) |
| 268 | { |
| 269 | if (ec) |
| 270 | { |
| 271 | BMCWEB_LOG_ERROR("UNIX socket: async_accept error = {}", |
| 272 | ec.message()); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | BMCWEB_LOG_DEBUG("Connection opened"); |
| 277 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 278 | if (self == nullptr) |
| 279 | { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | self->connection.resumeRead(); |
| 284 | self->peerSocket = std::move(socket); |
| 285 | // Start reading from socket |
| 286 | self->doRead(); |
| 287 | } |
| 288 | |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 289 | void run() |
| 290 | { |
| 291 | acceptor.async_accept( |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 292 | std::bind_front(&NbdProxyServer::afterAccept, weak_from_this())); |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 293 | |
| 294 | crow::connections::systemBus->async_method_call( |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 295 | [weak{weak_from_this()}](const boost::system::error_code& ec, |
| 296 | bool isBinary) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 297 | afterMount(weak, ec, isBinary); |
| 298 | }, |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 299 | "xyz.openbmc_project.VirtualMedia", path, |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 300 | "xyz.openbmc_project.VirtualMedia.Proxy", "Mount"); |
| 301 | } |
| 302 | |
| 303 | void send(std::string_view buffer, std::function<void()>&& onDone) |
| 304 | { |
| 305 | size_t copied = boost::asio::buffer_copy( |
| 306 | ws2uxBuf.prepare(buffer.size()), boost::asio::buffer(buffer)); |
| 307 | ws2uxBuf.commit(copied); |
| 308 | |
| 309 | doWrite(std::move(onDone)); |
| 310 | } |
| 311 | |
| 312 | private: |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 313 | static void afterSendEx(const std::weak_ptr<NbdProxyServer>& weak) |
| 314 | { |
| 315 | std::shared_ptr<NbdProxyServer> self2 = weak.lock(); |
| 316 | if (self2 != nullptr) |
| 317 | { |
| 318 | self2->ux2wsBuf.consume(self2->ux2wsBuf.size()); |
| 319 | self2->doRead(); |
| 320 | } |
| 321 | } |
| 322 | |
Ed Tanous | e43512a | 2024-08-06 10:50:33 -0700 | [diff] [blame] | 323 | void afterRead(const std::weak_ptr<NbdProxyServer>& weak, |
| 324 | const boost::system::error_code& ec, size_t bytesRead) |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 325 | { |
| 326 | if (ec) |
| 327 | { |
| 328 | BMCWEB_LOG_ERROR("UNIX socket: async_read_some error = {}", |
| 329 | ec.message()); |
| 330 | return; |
| 331 | } |
| 332 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 333 | if (self == nullptr) |
| 334 | { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | // Send to websocket |
| 339 | self->ux2wsBuf.commit(bytesRead); |
| 340 | self->connection.sendEx( |
| 341 | crow::websocket::MessageType::Binary, |
| 342 | boost::beast::buffers_to_string(self->ux2wsBuf.data()), |
| 343 | std::bind_front(&NbdProxyServer::afterSendEx, weak_from_this())); |
| 344 | } |
| 345 | |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 346 | void doRead() |
| 347 | { |
| 348 | // Trigger async read |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 349 | peerSocket.async_read_some(ux2wsBuf.prepare(nbdBufferSize), |
| 350 | std::bind_front(&NbdProxyServer::afterRead, |
| 351 | this, weak_from_this())); |
| 352 | } |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 353 | |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 354 | static void afterWrite(const std::weak_ptr<NbdProxyServer>& weak, |
| 355 | std::function<void()>&& onDone, |
| 356 | const boost::system::error_code& ec, |
| 357 | size_t bytesWritten) |
| 358 | { |
| 359 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 360 | if (self == nullptr) |
| 361 | { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | self->ws2uxBuf.consume(bytesWritten); |
| 366 | self->uxWriteInProgress = false; |
| 367 | |
| 368 | if (ec) |
| 369 | { |
| 370 | BMCWEB_LOG_ERROR("UNIX: async_write error = {}", ec.message()); |
| 371 | self->connection.close("Internal error"); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | // Retrigger doWrite if there is something in buffer |
| 376 | if (self->ws2uxBuf.size() > 0) |
| 377 | { |
| 378 | self->doWrite(std::move(onDone)); |
| 379 | return; |
| 380 | } |
| 381 | onDone(); |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void doWrite(std::function<void()>&& onDone) |
| 385 | { |
| 386 | if (uxWriteInProgress) |
| 387 | { |
| 388 | BMCWEB_LOG_ERROR("Write in progress"); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | if (ws2uxBuf.size() == 0) |
| 393 | { |
| 394 | BMCWEB_LOG_ERROR("No data to write to UNIX socket"); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | uxWriteInProgress = true; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 399 | peerSocket.async_write_some( |
| 400 | ws2uxBuf.data(), |
| 401 | std::bind_front(&NbdProxyServer::afterWrite, weak_from_this(), |
| 402 | std::move(onDone))); |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | // Keeps UNIX socket endpoint file path |
| 406 | const std::string socketId; |
| 407 | const std::string endpointId; |
| 408 | const std::string path; |
| 409 | |
| 410 | bool uxWriteInProgress = false; |
| 411 | |
| 412 | // UNIX => WebSocket buffer |
| 413 | boost::beast::flat_static_buffer<nbdBufferSize> ux2wsBuf; |
| 414 | |
| 415 | // WebSocket => UNIX buffer |
| 416 | boost::beast::flat_static_buffer<nbdBufferSize> ws2uxBuf; |
| 417 | |
| 418 | // The socket used to communicate with the client. |
| 419 | stream_protocol::socket peerSocket; |
| 420 | |
| 421 | // Default acceptor for UNIX socket |
| 422 | stream_protocol::acceptor acceptor; |
| 423 | |
| 424 | crow::websocket::Connection& connection; |
| 425 | }; |
| 426 | |
| 427 | using SessionMap = boost::container::flat_map<crow::websocket::Connection*, |
| 428 | std::shared_ptr<NbdProxyServer>>; |
| 429 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 430 | static SessionMap sessions; |
| 431 | |
| 432 | inline void |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 433 | afterGetSocket(crow::websocket::Connection& conn, |
| 434 | const sdbusplus::message::object_path& path, |
| 435 | const boost::system::error_code& ec, |
| 436 | const dbus::utility::DBusPropertiesMap& propertiesList) |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 437 | { |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 438 | if (ec) |
| 439 | { |
Boleslaw Ogonczyk Makowski | 5ec2284 | 2024-05-16 14:23:43 +0200 | [diff] [blame] | 440 | BMCWEB_LOG_ERROR("DBus getAllProperties error: {}", ec.message()); |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 441 | conn.close("Internal Error"); |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 442 | return; |
| 443 | } |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 444 | std::string endpointId; |
| 445 | std::string socket; |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 446 | |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 447 | bool success = sdbusplus::unpackPropertiesNoThrow( |
| 448 | redfish::dbus_utils::UnpackErrorPrinter(), propertiesList, "EndpointId", |
| 449 | endpointId, "Socket", socket); |
| 450 | |
| 451 | if (!success) |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 452 | { |
Boleslaw Ogonczyk Makowski | 5ec2284 | 2024-05-16 14:23:43 +0200 | [diff] [blame] | 453 | BMCWEB_LOG_ERROR("Failed to unpack properties"); |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 454 | conn.close("Internal Error"); |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
| 458 | for (const auto& session : sessions) |
| 459 | { |
| 460 | if (session.second->getEndpointId() == conn.url().path()) |
| 461 | { |
| 462 | BMCWEB_LOG_ERROR("Cannot open new connection - socket is in use"); |
| 463 | conn.close("Slot is in use"); |
| 464 | return; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // If the socket file exists (i.e. after bmcweb crash), |
| 469 | // we cannot reuse it. |
Ed Tanous | 80ba22f | 2024-05-06 15:37:17 -0700 | [diff] [blame] | 470 | std::error_code ec2; |
| 471 | std::filesystem::remove(socket.c_str(), ec2); |
| 472 | // Ignore failures. File might not exist. |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 473 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 474 | sessions[&conn] = |
| 475 | std::make_shared<NbdProxyServer>(conn, socket, endpointId, path); |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 476 | sessions[&conn]->run(); |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 477 | } |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 478 | |
| 479 | inline void onOpen(crow::websocket::Connection& conn) |
| 480 | { |
| 481 | BMCWEB_LOG_DEBUG("nbd-proxy.onopen({})", logPtr(&conn)); |
| 482 | |
Boleslaw Ogonczyk Makowski | 5ec2284 | 2024-05-16 14:23:43 +0200 | [diff] [blame] | 483 | if (conn.url().segments().size() < 2) |
| 484 | { |
| 485 | BMCWEB_LOG_ERROR("Invalid path - \"{}\"", conn.url().path()); |
| 486 | conn.close("Internal error"); |
| 487 | return; |
| 488 | } |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 489 | |
Boleslaw Ogonczyk Makowski | 5ec2284 | 2024-05-16 14:23:43 +0200 | [diff] [blame] | 490 | std::string index = conn.url().segments().back(); |
| 491 | std::string path = |
| 492 | std::format("/xyz/openbmc_project/VirtualMedia/Proxy/Slot_{}", index); |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 493 | |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 494 | dbus::utility::getAllProperties( |
| 495 | "xyz.openbmc_project.VirtualMedia", path, |
Boleslaw Ogonczyk Makowski | 5ec2284 | 2024-05-16 14:23:43 +0200 | [diff] [blame] | 496 | "xyz.openbmc_project.VirtualMedia.MountPoint", |
Ed Tanous | e4b3275 | 2024-02-09 18:56:29 -0800 | [diff] [blame] | 497 | [&conn, path](const boost::system::error_code& ec, |
| 498 | const dbus::utility::DBusPropertiesMap& propertiesList) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 499 | afterGetSocket(conn, path, ec, propertiesList); |
| 500 | }); |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 501 | |
| 502 | // We need to wait for dbus and the websockets to hook up before data is |
| 503 | // sent/received. Tell the core to hold off messages until the sockets are |
| 504 | // up |
| 505 | conn.deferRead(); |
| 506 | } |
| 507 | |
| 508 | inline void onClose(crow::websocket::Connection& conn, |
| 509 | const std::string& reason) |
| 510 | { |
| 511 | BMCWEB_LOG_DEBUG("nbd-proxy.onclose(reason = '{}')", reason); |
| 512 | auto session = sessions.find(&conn); |
| 513 | if (session == sessions.end()) |
| 514 | { |
| 515 | BMCWEB_LOG_DEBUG("No session to close"); |
| 516 | return; |
| 517 | } |
| 518 | // Remove reference to session in global map |
| 519 | sessions.erase(session); |
| 520 | } |
| 521 | |
| 522 | inline void onMessage(crow::websocket::Connection& conn, std::string_view data, |
| 523 | crow::websocket::MessageType /*type*/, |
| 524 | std::function<void()>&& whenComplete) |
| 525 | { |
| 526 | BMCWEB_LOG_DEBUG("nbd-proxy.onMessage(len = {})", data.size()); |
| 527 | |
| 528 | // Acquire proxy from sessions |
| 529 | auto session = sessions.find(&conn); |
| 530 | if (session == sessions.end() || session->second == nullptr) |
| 531 | { |
| 532 | whenComplete(); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | session->second->send(data, std::move(whenComplete)); |
| 537 | } |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 538 | } // namespace nbd_proxy |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 539 | |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 540 | namespace obmc_vm |
| 541 | { |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 542 | |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 543 | inline void requestRoutes(App& app) |
| 544 | { |
| 545 | static_assert( |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 546 | !(BMCWEB_VM_WEBSOCKET && BMCWEB_VM_NBDPROXY), |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 547 | "nbd proxy cannot be turned on at the same time as vm websocket."); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 548 | |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 549 | if constexpr (BMCWEB_VM_NBDPROXY) |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 550 | { |
| 551 | BMCWEB_ROUTE(app, "/nbd/<str>") |
| 552 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
| 553 | .websocket() |
| 554 | .onopen(nbd_proxy::onOpen) |
| 555 | .onclose(nbd_proxy::onClose) |
| 556 | .onmessageex(nbd_proxy::onMessage); |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 557 | |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 558 | BMCWEB_ROUTE(app, "/vm/0/0") |
| 559 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
| 560 | .websocket() |
| 561 | .onopen(nbd_proxy::onOpen) |
| 562 | .onclose(nbd_proxy::onClose) |
| 563 | .onmessageex(nbd_proxy::onMessage); |
| 564 | } |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 565 | if constexpr (BMCWEB_VM_WEBSOCKET) |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 566 | { |
| 567 | BMCWEB_ROUTE(app, "/vm/0/0") |
| 568 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
| 569 | .websocket() |
| 570 | .onopen([](crow::websocket::Connection& conn) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 571 | BMCWEB_LOG_DEBUG("Connection {} opened", logPtr(&conn)); |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 572 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 573 | if (session != nullptr) |
| 574 | { |
| 575 | conn.close("Session already connected"); |
| 576 | return; |
| 577 | } |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 578 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 579 | if (handler != nullptr) |
| 580 | { |
| 581 | conn.close("Handler already running"); |
| 582 | return; |
| 583 | } |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 584 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 585 | session = &conn; |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 586 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 587 | // media is the last digit of the endpoint /vm/0/0. A future |
| 588 | // enhancement can include supporting different endpoint values. |
| 589 | const char* media = "0"; |
| 590 | handler = std::make_shared<Handler>(media, conn.getIoContext()); |
| 591 | handler->connect(); |
| 592 | }) |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 593 | .onclose([](crow::websocket::Connection& conn, |
| 594 | const std::string& /*reason*/) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 595 | if (&conn != session) |
| 596 | { |
| 597 | return; |
| 598 | } |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 599 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 600 | session = nullptr; |
| 601 | handler->doClose(); |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 602 | handler->inputBuffer.clear(); |
| 603 | handler->outputBuffer.clear(); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 604 | handler.reset(); |
| 605 | }) |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 606 | .onmessage([](crow::websocket::Connection& conn, |
| 607 | const std::string& data, bool) { |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 608 | if (data.length() > handler->inputBuffer.capacity() - |
| 609 | handler->inputBuffer.size()) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 610 | { |
| 611 | BMCWEB_LOG_ERROR("Buffer overrun when writing {} bytes", |
| 612 | data.length()); |
| 613 | conn.close("Buffer overrun"); |
| 614 | return; |
| 615 | } |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 616 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 617 | size_t copied = boost::asio::buffer_copy( |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 618 | handler->inputBuffer.prepare(data.size()), |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 619 | boost::asio::buffer(data)); |
Ed Tanous | 3515fd1 | 2024-12-11 23:34:32 -0800 | [diff] [blame] | 620 | handler->inputBuffer.commit(copied); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 621 | handler->doWrite(); |
| 622 | }); |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 623 | } |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | } // namespace obmc_vm |
Ed Tanous | 36c0f2a | 2024-02-09 13:50:26 -0800 | [diff] [blame] | 627 | |
Adriana Kobylak | 1bfbe0e | 2019-01-17 12:08:38 -0600 | [diff] [blame] | 628 | } // namespace crow |