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