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