Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 17 | #include "app.hpp" |
| 18 | #include "dbus_utility.hpp" |
| 19 | #include "privileges.hpp" |
Ed Tanous | faf100f | 2023-05-25 10:03:14 -0700 | [diff] [blame] | 20 | #include "websocket.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 21 | |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 22 | #include <boost/asio/local/stream_protocol.hpp> |
| 23 | #include <boost/asio/write.hpp> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 24 | #include <boost/beast/core/buffers_to_string.hpp> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 25 | #include <boost/container/flat_map.hpp> |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 26 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 27 | #include <string_view> |
| 28 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 29 | namespace crow |
| 30 | { |
| 31 | |
| 32 | namespace nbd_proxy |
| 33 | { |
| 34 | |
| 35 | using boost::asio::local::stream_protocol; |
| 36 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 37 | static constexpr size_t nbdBufferSize = 131088; |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 38 | constexpr const char* requiredPrivilegeString = "ConfigureManager"; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 39 | |
| 40 | struct NbdProxyServer : std::enable_shared_from_this<NbdProxyServer> |
| 41 | { |
| 42 | NbdProxyServer(crow::websocket::Connection& connIn, |
| 43 | const std::string& socketIdIn, |
| 44 | const std::string& endpointIdIn, const std::string& pathIn) : |
| 45 | socketId(socketIdIn), |
| 46 | endpointId(endpointIdIn), path(pathIn), |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 47 | |
| 48 | peerSocket(connIn.getIoContext()), |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 49 | acceptor(connIn.getIoContext(), stream_protocol::endpoint(socketId)), |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 50 | connection(connIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 51 | {} |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 52 | |
Ed Tanous | 08e9fa9 | 2022-02-16 16:18:08 -0800 | [diff] [blame] | 53 | NbdProxyServer(const NbdProxyServer&) = delete; |
| 54 | NbdProxyServer(NbdProxyServer&&) = delete; |
| 55 | NbdProxyServer& operator=(const NbdProxyServer&) = delete; |
| 56 | NbdProxyServer& operator=(NbdProxyServer&&) = delete; |
| 57 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 58 | ~NbdProxyServer() |
| 59 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 60 | BMCWEB_LOG_DEBUG("NbdProxyServer destructor"); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 61 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 62 | BMCWEB_LOG_DEBUG("peerSocket->close()"); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 63 | boost::system::error_code ec; |
| 64 | peerSocket.close(ec); |
| 65 | |
Ed Tanous | 4c521c3 | 2024-04-07 13:47:06 -0700 | [diff] [blame] | 66 | BMCWEB_LOG_DEBUG("std::filesystem::remove({})", socketId); |
| 67 | std::error_code ec2; |
| 68 | std::filesystem::remove(socketId.c_str(), ec2); |
| 69 | if (ec2) |
| 70 | { |
| 71 | BMCWEB_LOG_DEBUG("Failed to remove file, ignoring"); |
| 72 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 73 | |
| 74 | crow::connections::systemBus->async_method_call( |
| 75 | dbus::utility::logError, "xyz.openbmc_project.VirtualMedia", path, |
| 76 | "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | std::string getEndpointId() const |
| 80 | { |
| 81 | return endpointId; |
| 82 | } |
| 83 | |
| 84 | void run() |
| 85 | { |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 86 | acceptor.async_accept( |
| 87 | [weak(weak_from_this())](const boost::system::error_code& ec, |
| 88 | stream_protocol::socket socket) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 89 | if (ec) |
| 90 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 91 | BMCWEB_LOG_ERROR("UNIX socket: async_accept error = {}", |
| 92 | ec.message()); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 93 | return; |
| 94 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 95 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 96 | BMCWEB_LOG_DEBUG("Connection opened"); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 97 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 98 | if (self == nullptr) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 99 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 100 | return; |
| 101 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 102 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 103 | self->connection.resumeRead(); |
| 104 | self->peerSocket = std::move(socket); |
| 105 | // Start reading from socket |
| 106 | self->doRead(); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 107 | }); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 108 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 109 | auto mountHandler = [weak(weak_from_this())]( |
| 110 | const boost::system::error_code& ec, bool) { |
| 111 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 112 | if (self == nullptr) |
| 113 | { |
| 114 | return; |
| 115 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 116 | if (ec) |
| 117 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 118 | BMCWEB_LOG_ERROR("DBus error: cannot call mount method = {}", |
| 119 | ec.message()); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 120 | |
| 121 | self->connection.close("Failed to mount media"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | crow::connections::systemBus->async_method_call( |
| 127 | std::move(mountHandler), "xyz.openbmc_project.VirtualMedia", path, |
| 128 | "xyz.openbmc_project.VirtualMedia.Proxy", "Mount"); |
| 129 | } |
| 130 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 131 | void send(std::string_view buffer, std::function<void()>&& onDone) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 132 | { |
Ed Tanous | 44106f3 | 2024-04-06 13:48:50 -0700 | [diff] [blame] | 133 | size_t copied = boost::asio::buffer_copy( |
| 134 | ws2uxBuf.prepare(buffer.size()), boost::asio::buffer(buffer)); |
| 135 | ws2uxBuf.commit(copied); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 136 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 137 | doWrite(std::move(onDone)); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | private: |
| 141 | void doRead() |
| 142 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 143 | // Trigger async read |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 144 | peerSocket.async_read_some( |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 145 | ux2wsBuf.prepare(nbdBufferSize), |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 146 | [weak(weak_from_this())](const boost::system::error_code& ec, |
| 147 | size_t bytesRead) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 148 | if (ec) |
| 149 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 150 | BMCWEB_LOG_ERROR("UNIX socket: async_read_some error = {}", |
| 151 | ec.message()); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 155 | if (self == nullptr) |
| 156 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 157 | return; |
| 158 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 159 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 160 | // Send to websocket |
| 161 | self->ux2wsBuf.commit(bytesRead); |
| 162 | self->connection.sendEx( |
| 163 | crow::websocket::MessageType::Binary, |
| 164 | boost::beast::buffers_to_string(self->ux2wsBuf.data()), |
| 165 | [weak(self->weak_from_this())]() { |
| 166 | std::shared_ptr<NbdProxyServer> self2 = weak.lock(); |
| 167 | if (self2 != nullptr) |
| 168 | { |
| 169 | self2->ux2wsBuf.consume(self2->ux2wsBuf.size()); |
| 170 | self2->doRead(); |
| 171 | } |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 172 | }); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 173 | }); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 176 | void doWrite(std::function<void()>&& onDone) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 177 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 178 | if (uxWriteInProgress) |
| 179 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 180 | BMCWEB_LOG_ERROR("Write in progress"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | |
Jason M. Bills | 9c0b4e5 | 2022-02-14 07:23:30 -0800 | [diff] [blame] | 184 | if (ws2uxBuf.size() == 0) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 185 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 186 | BMCWEB_LOG_ERROR("No data to write to UNIX socket"); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
| 190 | uxWriteInProgress = true; |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 191 | peerSocket.async_write_some( |
| 192 | ws2uxBuf.data(), |
| 193 | [weak(weak_from_this()), |
| 194 | onDone(std::move(onDone))](const boost::system::error_code& ec, |
| 195 | size_t bytesWritten) mutable { |
| 196 | std::shared_ptr<NbdProxyServer> self = weak.lock(); |
| 197 | if (self == nullptr) |
| 198 | { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | self->ws2uxBuf.consume(bytesWritten); |
| 203 | self->uxWriteInProgress = false; |
| 204 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 205 | if (ec) |
| 206 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 207 | BMCWEB_LOG_ERROR("UNIX: async_write error = {}", ec.message()); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 208 | self->connection.close("Internal error"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 209 | return; |
| 210 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 211 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 212 | // Retrigger doWrite if there is something in buffer |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 213 | if (self->ws2uxBuf.size() > 0) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 214 | { |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 215 | self->doWrite(std::move(onDone)); |
| 216 | return; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 217 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 218 | onDone(); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 219 | }); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // Keeps UNIX socket endpoint file path |
| 223 | const std::string socketId; |
| 224 | const std::string endpointId; |
| 225 | const std::string path; |
| 226 | |
| 227 | bool uxWriteInProgress = false; |
| 228 | |
| 229 | // UNIX => WebSocket buffer |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 230 | boost::beast::flat_static_buffer<nbdBufferSize> ux2wsBuf; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 231 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 232 | // WebSocket => UNIX buffer |
| 233 | boost::beast::flat_static_buffer<nbdBufferSize> ws2uxBuf; |
| 234 | |
| 235 | // The socket used to communicate with the client. |
| 236 | stream_protocol::socket peerSocket; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 237 | |
| 238 | // Default acceptor for UNIX socket |
| 239 | stream_protocol::acceptor acceptor; |
| 240 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 241 | crow::websocket::Connection& connection; |
| 242 | }; |
| 243 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 244 | using SessionMap = boost::container::flat_map<crow::websocket::Connection*, |
| 245 | std::shared_ptr<NbdProxyServer>>; |
| 246 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 247 | static SessionMap sessions; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 248 | |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 249 | inline void |
| 250 | afterGetManagedObjects(crow::websocket::Connection& conn, |
| 251 | const boost::system::error_code& ec, |
| 252 | const dbus::utility::ManagedObjectType& objects) |
| 253 | { |
| 254 | const std::string* socketValue = nullptr; |
| 255 | const std::string* endpointValue = nullptr; |
| 256 | const std::string* endpointObjectPath = nullptr; |
| 257 | |
| 258 | if (ec) |
| 259 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 260 | BMCWEB_LOG_ERROR("DBus error: {}", ec.message()); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 261 | conn.close("Failed to create mount point"); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | for (const auto& [objectPath, interfaces] : objects) |
| 266 | { |
| 267 | for (const auto& [interface, properties] : interfaces) |
| 268 | { |
| 269 | if (interface != "xyz.openbmc_project.VirtualMedia.MountPoint") |
| 270 | { |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | for (const auto& [name, value] : properties) |
| 275 | { |
| 276 | if (name == "EndpointId") |
| 277 | { |
| 278 | endpointValue = std::get_if<std::string>(&value); |
| 279 | |
| 280 | if (endpointValue == nullptr) |
| 281 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 282 | BMCWEB_LOG_ERROR("EndpointId property value is null"); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | if (name == "Socket") |
| 286 | { |
| 287 | socketValue = std::get_if<std::string>(&value); |
| 288 | if (socketValue == nullptr) |
| 289 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 290 | BMCWEB_LOG_ERROR("Socket property value is null"); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if ((endpointValue != nullptr) && (socketValue != nullptr) && |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 297 | *endpointValue == conn.url().path()) |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 298 | { |
| 299 | endpointObjectPath = &objectPath.str; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (objects.empty() || endpointObjectPath == nullptr) |
| 305 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 306 | BMCWEB_LOG_ERROR("Cannot find requested EndpointId"); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 307 | conn.close("Failed to match EndpointId"); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | for (const auto& session : sessions) |
| 312 | { |
Ed Tanous | 5ebb9d3 | 2023-02-27 18:20:47 -0800 | [diff] [blame] | 313 | if (session.second->getEndpointId() == conn.url().path()) |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 314 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 315 | BMCWEB_LOG_ERROR("Cannot open new connection - socket is in use"); |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 316 | conn.close("Slot is in use"); |
| 317 | return; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // If the socket file exists (i.e. after bmcweb crash), |
| 322 | // we cannot reuse it. |
| 323 | std::remove((*socketValue).c_str()); |
| 324 | |
| 325 | sessions[&conn] = std::make_shared<NbdProxyServer>( |
| 326 | conn, *socketValue, *endpointValue, *endpointObjectPath); |
| 327 | |
| 328 | sessions[&conn]->run(); |
| 329 | }; |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 330 | inline void onOpen(crow::websocket::Connection& conn) |
| 331 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 332 | BMCWEB_LOG_DEBUG("nbd-proxy.onopen({})", logPtr(&conn)); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 333 | |
George Liu | 5eb468d | 2023-06-20 17:03:24 +0800 | [diff] [blame] | 334 | sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia"); |
| 335 | dbus::utility::getManagedObjects( |
| 336 | "xyz.openbmc_project.VirtualMedia", path, |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 337 | [&conn](const boost::system::error_code& ec, |
Ed Tanous | e551b5f | 2023-02-27 14:19:07 -0800 | [diff] [blame] | 338 | const dbus::utility::ManagedObjectType& objects) { |
Ed Tanous | 2da6b8c | 2023-02-27 14:25:10 -0800 | [diff] [blame] | 339 | afterGetManagedObjects(conn, ec, objects); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 340 | }); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 341 | |
| 342 | // We need to wait for dbus and the websockets to hook up before data is |
| 343 | // sent/received. Tell the core to hold off messages until the sockets are |
| 344 | // up |
| 345 | conn.deferRead(); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | inline void onClose(crow::websocket::Connection& conn, |
| 349 | const std::string& reason) |
| 350 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 351 | BMCWEB_LOG_DEBUG("nbd-proxy.onclose(reason = '{}')", reason); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 352 | auto session = sessions.find(&conn); |
| 353 | if (session == sessions.end()) |
| 354 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 355 | BMCWEB_LOG_DEBUG("No session to close"); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 356 | return; |
| 357 | } |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 358 | // Remove reference to session in global map |
| 359 | sessions.erase(session); |
| 360 | } |
| 361 | |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 362 | inline void onMessage(crow::websocket::Connection& conn, std::string_view data, |
| 363 | crow::websocket::MessageType /*type*/, |
| 364 | std::function<void()>&& whenComplete) |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 365 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 366 | BMCWEB_LOG_DEBUG("nbd-proxy.onMessage(len = {})", data.size()); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 367 | |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 368 | // Acquire proxy from sessions |
| 369 | auto session = sessions.find(&conn); |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 370 | if (session == sessions.end() || session->second == nullptr) |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 371 | { |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 372 | whenComplete(); |
| 373 | return; |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 374 | } |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 375 | |
| 376 | session->second->send(data, std::move(whenComplete)); |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 379 | inline void requestRoutes(App& app) |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 380 | { |
| 381 | BMCWEB_ROUTE(app, "/nbd/<str>") |
| 382 | .websocket() |
Ed Tanous | 8108fd3 | 2023-02-27 13:58:03 -0800 | [diff] [blame] | 383 | .onopen(onOpen) |
| 384 | .onclose(onClose) |
Ed Tanous | 863c1c2 | 2022-02-21 21:33:06 -0800 | [diff] [blame] | 385 | .onmessageex(onMessage); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 386 | } |
| 387 | } // namespace nbd_proxy |
| 388 | } // namespace crow |