AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2020 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 |
Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 17 | |
| 18 | #include "async_resolve.hpp" |
| 19 | #include "http_response.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 20 | #include "logging.hpp" |
| 21 | #include "ssl_key_handler.hpp" |
Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 22 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 23 | #include <boost/asio/connect.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 24 | #include <boost/asio/io_context.hpp> |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 25 | #include <boost/asio/ip/address.hpp> |
| 26 | #include <boost/asio/ip/basic_endpoint.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 27 | #include <boost/asio/ip/tcp.hpp> |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 28 | #include <boost/asio/ssl/context.hpp> |
| 29 | #include <boost/asio/ssl/error.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 30 | #include <boost/asio/steady_timer.hpp> |
| 31 | #include <boost/beast/core/flat_buffer.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 32 | #include <boost/beast/core/flat_static_buffer.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 33 | #include <boost/beast/http/message.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 34 | #include <boost/beast/http/parser.hpp> |
| 35 | #include <boost/beast/http/read.hpp> |
| 36 | #include <boost/beast/http/string_body.hpp> |
| 37 | #include <boost/beast/http/write.hpp> |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 38 | #include <boost/beast/ssl/ssl_stream.hpp> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 39 | #include <boost/beast/version.hpp> |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 40 | #include <boost/container/devector.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 41 | #include <boost/system/error_code.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 42 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 43 | #include <cstdlib> |
| 44 | #include <functional> |
| 45 | #include <iostream> |
| 46 | #include <memory> |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 47 | #include <queue> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 48 | #include <string> |
| 49 | |
| 50 | namespace crow |
| 51 | { |
| 52 | |
Carson Labrado | 66d90c2 | 2022-12-07 22:34:33 +0000 | [diff] [blame] | 53 | // With Redfish Aggregation it is assumed we will connect to another instance |
| 54 | // of BMCWeb which can handle 100 simultaneous connections. |
| 55 | constexpr size_t maxPoolSize = 20; |
| 56 | constexpr size_t maxRequestQueueSize = 500; |
Carson Labrado | 17dcc31 | 2022-07-28 22:17:28 +0000 | [diff] [blame] | 57 | constexpr unsigned int httpReadBodyLimit = 131072; |
Carson Labrado | 4d94272 | 2022-06-22 22:16:10 +0000 | [diff] [blame] | 58 | constexpr unsigned int httpReadBufferSize = 4096; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 59 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 60 | enum class ConnState |
| 61 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 62 | initialized, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 63 | resolveInProgress, |
| 64 | resolveFailed, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 65 | connectInProgress, |
| 66 | connectFailed, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 67 | connected, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 68 | handshakeInProgress, |
| 69 | handshakeFailed, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 70 | sendInProgress, |
| 71 | sendFailed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 72 | recvInProgress, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 73 | recvFailed, |
| 74 | idle, |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 75 | closed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 76 | suspended, |
| 77 | terminated, |
| 78 | abortConnection, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 79 | sslInitFailed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 80 | retry |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 81 | }; |
| 82 | |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 83 | static inline boost::system::error_code |
| 84 | defaultRetryHandler(unsigned int respCode) |
| 85 | { |
| 86 | // As a default, assume 200X is alright |
| 87 | BMCWEB_LOG_DEBUG << "Using default check for response code validity"; |
| 88 | if ((respCode < 200) || (respCode >= 300)) |
| 89 | { |
| 90 | return boost::system::errc::make_error_code( |
| 91 | boost::system::errc::result_out_of_range); |
| 92 | } |
| 93 | |
| 94 | // Return 0 if the response code is valid |
| 95 | return boost::system::errc::make_error_code(boost::system::errc::success); |
| 96 | }; |
| 97 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 98 | // We need to allow retry information to be set before a message has been sent |
| 99 | // and a connection pool has been created |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 100 | struct ConnectionPolicy |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 101 | { |
| 102 | uint32_t maxRetryAttempts = 5; |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 103 | |
| 104 | // the max size of requests in bytes. 0 for unlimited |
| 105 | boost::optional<uint64_t> requestByteLimit = httpReadBodyLimit; |
| 106 | |
| 107 | size_t maxConnections = 1; |
| 108 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 109 | std::string retryPolicyAction = "TerminateAfterRetries"; |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 110 | |
| 111 | std::chrono::seconds retryIntervalSecs = std::chrono::seconds(0); |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 112 | std::function<boost::system::error_code(unsigned int respCode)> |
| 113 | invalidResp = defaultRetryHandler; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | struct PendingRequest |
| 117 | { |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 118 | boost::beast::http::request<boost::beast::http::string_body> req; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 119 | std::function<void(bool, uint32_t, Response&)> callback; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 120 | PendingRequest( |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 121 | boost::beast::http::request<boost::beast::http::string_body>&& reqIn, |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 122 | const std::function<void(bool, uint32_t, Response&)>& callbackIn) : |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 123 | req(std::move(reqIn)), |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 124 | callback(callbackIn) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 125 | {} |
| 126 | }; |
| 127 | |
| 128 | class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 129 | { |
| 130 | private: |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 131 | ConnState state = ConnState::initialized; |
| 132 | uint32_t retryCount = 0; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 133 | std::string subId; |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 134 | std::shared_ptr<ConnectionPolicy> connPolicy; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 135 | std::string host; |
| 136 | uint16_t port; |
| 137 | uint32_t connId; |
| 138 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 139 | // Data buffers |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 140 | boost::beast::http::request<boost::beast::http::string_body> req; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 141 | std::optional< |
| 142 | boost::beast::http::response_parser<boost::beast::http::string_body>> |
| 143 | parser; |
Carson Labrado | 4d94272 | 2022-06-22 22:16:10 +0000 | [diff] [blame] | 144 | boost::beast::flat_static_buffer<httpReadBufferSize> buffer; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 145 | Response res; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 146 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 147 | // Ascync callables |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 148 | std::function<void(bool, uint32_t, Response&)> callback; |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 149 | |
| 150 | #ifdef BMCWEB_DBUS_DNS_RESOLVER |
Ed Tanous | e1452be | 2021-10-04 17:03:52 -0700 | [diff] [blame] | 151 | using Resolver = async_resolve::Resolver; |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 152 | #else |
| 153 | using Resolver = boost::asio::ip::tcp::resolver; |
| 154 | #endif |
| 155 | Resolver resolver; |
| 156 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 157 | boost::asio::ip::tcp::socket conn; |
| 158 | std::optional<boost::beast::ssl_stream<boost::asio::ip::tcp::socket&>> |
| 159 | sslConn; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 160 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 161 | boost::asio::steady_timer timer; |
Ed Tanous | 84b3560 | 2021-09-08 20:06:32 -0700 | [diff] [blame] | 162 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 163 | friend class ConnectionPool; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 164 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 165 | void doResolve() |
| 166 | { |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 167 | state = ConnState::resolveInProgress; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 168 | BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":" |
| 169 | << std::to_string(port) |
| 170 | << ", id: " << std::to_string(connId); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 171 | |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 172 | resolver.async_resolve(host, std::to_string(port), |
| 173 | std::bind_front(&ConnectionInfo::afterResolve, |
| 174 | this, shared_from_this())); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 175 | } |
| 176 | |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 177 | void afterResolve(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 178 | const boost::system::error_code& ec, |
| 179 | const Resolver::results_type& endpointList) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 180 | { |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 181 | if (ec || (endpointList.empty())) |
| 182 | { |
Sunitha Harish | d7043b3 | 2023-07-11 04:49:03 -0500 | [diff] [blame] | 183 | BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message() << " " |
| 184 | << host << ":" << std::to_string(port); |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 185 | state = ConnState::resolveFailed; |
| 186 | waitAndRetry(); |
| 187 | return; |
| 188 | } |
| 189 | BMCWEB_LOG_DEBUG << "Resolved " << host << ":" << std::to_string(port) |
| 190 | << ", id: " << std::to_string(connId); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 191 | state = ConnState::connectInProgress; |
| 192 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 193 | BMCWEB_LOG_DEBUG << "Trying to connect to: " << host << ":" |
| 194 | << std::to_string(port) |
| 195 | << ", id: " << std::to_string(connId); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 196 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 197 | timer.expires_after(std::chrono::seconds(30)); |
| 198 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
| 199 | |
| 200 | boost::asio::async_connect( |
| 201 | conn, endpointList, |
| 202 | std::bind_front(&ConnectionInfo::afterConnect, this, |
| 203 | shared_from_this())); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void afterConnect(const std::shared_ptr<ConnectionInfo>& /*self*/, |
Ed Tanous | 81c4e33 | 2023-05-18 10:30:34 -0700 | [diff] [blame] | 207 | const boost::beast::error_code& ec, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 208 | const boost::asio::ip::tcp::endpoint& endpoint) |
| 209 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 210 | // The operation already timed out. We don't want do continue down |
| 211 | // this branch |
| 212 | if (ec && ec == boost::asio::error::operation_aborted) |
| 213 | { |
| 214 | return; |
| 215 | } |
| 216 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 217 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 218 | if (ec) |
| 219 | { |
| 220 | BMCWEB_LOG_ERROR << "Connect " << endpoint.address().to_string() |
| 221 | << ":" << std::to_string(endpoint.port()) |
| 222 | << ", id: " << std::to_string(connId) |
| 223 | << " failed: " << ec.message(); |
| 224 | state = ConnState::connectFailed; |
| 225 | waitAndRetry(); |
| 226 | return; |
| 227 | } |
| 228 | BMCWEB_LOG_DEBUG << "Connected to: " << endpoint.address().to_string() |
| 229 | << ":" << std::to_string(endpoint.port()) |
| 230 | << ", id: " << std::to_string(connId); |
| 231 | if (sslConn) |
| 232 | { |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 233 | doSslHandshake(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | state = ConnState::connected; |
| 237 | sendMessage(); |
| 238 | } |
| 239 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 240 | void doSslHandshake() |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 241 | { |
| 242 | if (!sslConn) |
| 243 | { |
| 244 | return; |
| 245 | } |
| 246 | state = ConnState::handshakeInProgress; |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 247 | timer.expires_after(std::chrono::seconds(30)); |
| 248 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 249 | sslConn->async_handshake( |
| 250 | boost::asio::ssl::stream_base::client, |
| 251 | std::bind_front(&ConnectionInfo::afterSslHandshake, this, |
| 252 | shared_from_this())); |
| 253 | } |
| 254 | |
| 255 | void afterSslHandshake(const std::shared_ptr<ConnectionInfo>& /*self*/, |
Ed Tanous | 81c4e33 | 2023-05-18 10:30:34 -0700 | [diff] [blame] | 256 | const boost::beast::error_code& ec) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 257 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 258 | // The operation already timed out. We don't want do continue down |
| 259 | // this branch |
| 260 | if (ec && ec == boost::asio::error::operation_aborted) |
| 261 | { |
| 262 | return; |
| 263 | } |
| 264 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 265 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 266 | if (ec) |
| 267 | { |
| 268 | BMCWEB_LOG_ERROR << "SSL Handshake failed -" |
| 269 | << " id: " << std::to_string(connId) |
| 270 | << " error: " << ec.message(); |
| 271 | state = ConnState::handshakeFailed; |
| 272 | waitAndRetry(); |
| 273 | return; |
| 274 | } |
| 275 | BMCWEB_LOG_DEBUG << "SSL Handshake successful -" |
| 276 | << " id: " << std::to_string(connId); |
| 277 | state = ConnState::connected; |
| 278 | sendMessage(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 279 | } |
| 280 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 281 | void sendMessage() |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 282 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 283 | state = ConnState::sendInProgress; |
| 284 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 285 | // Set a timeout on the operation |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 286 | timer.expires_after(std::chrono::seconds(30)); |
| 287 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 288 | |
| 289 | // Send the HTTP request to the remote host |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 290 | if (sslConn) |
| 291 | { |
| 292 | boost::beast::http::async_write( |
| 293 | *sslConn, req, |
| 294 | std::bind_front(&ConnectionInfo::afterWrite, this, |
| 295 | shared_from_this())); |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | boost::beast::http::async_write( |
| 300 | conn, req, |
| 301 | std::bind_front(&ConnectionInfo::afterWrite, this, |
| 302 | shared_from_this())); |
| 303 | } |
| 304 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 305 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 306 | void afterWrite(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 307 | const boost::beast::error_code& ec, size_t bytesTransferred) |
| 308 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 309 | // The operation already timed out. We don't want do continue down |
| 310 | // this branch |
| 311 | if (ec && ec == boost::asio::error::operation_aborted) |
| 312 | { |
| 313 | return; |
| 314 | } |
| 315 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 316 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 317 | if (ec) |
| 318 | { |
Sunitha Harish | d7043b3 | 2023-07-11 04:49:03 -0500 | [diff] [blame] | 319 | BMCWEB_LOG_ERROR << "sendMessage() failed: " << ec.message() << " " |
| 320 | << host << ":" << std::to_string(port); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 321 | state = ConnState::sendFailed; |
| 322 | waitAndRetry(); |
| 323 | return; |
| 324 | } |
| 325 | BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: " |
| 326 | << bytesTransferred; |
| 327 | |
| 328 | recvMessage(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void recvMessage() |
| 332 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 333 | state = ConnState::recvInProgress; |
| 334 | |
| 335 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 336 | |
| 337 | parser->body_limit(connPolicy->requestByteLimit); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 338 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 339 | timer.expires_after(std::chrono::seconds(30)); |
| 340 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
| 341 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 342 | // Receive the HTTP response |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 343 | if (sslConn) |
| 344 | { |
| 345 | boost::beast::http::async_read( |
| 346 | *sslConn, buffer, *parser, |
| 347 | std::bind_front(&ConnectionInfo::afterRead, this, |
| 348 | shared_from_this())); |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | boost::beast::http::async_read( |
| 353 | conn, buffer, *parser, |
| 354 | std::bind_front(&ConnectionInfo::afterRead, this, |
| 355 | shared_from_this())); |
| 356 | } |
| 357 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 358 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 359 | void afterRead(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 360 | const boost::beast::error_code& ec, |
| 361 | const std::size_t& bytesTransferred) |
| 362 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 363 | // The operation already timed out. We don't want do continue down |
| 364 | // this branch |
| 365 | if (ec && ec == boost::asio::error::operation_aborted) |
| 366 | { |
| 367 | return; |
| 368 | } |
| 369 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 370 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 371 | if (ec && ec != boost::asio::ssl::error::stream_truncated) |
| 372 | { |
Sunitha Harish | d7043b3 | 2023-07-11 04:49:03 -0500 | [diff] [blame] | 373 | BMCWEB_LOG_ERROR << "recvMessage() failed: " << ec.message() |
| 374 | << " from " << host << ":" << std::to_string(port); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 375 | state = ConnState::recvFailed; |
| 376 | waitAndRetry(); |
| 377 | return; |
| 378 | } |
| 379 | BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: " |
| 380 | << bytesTransferred; |
| 381 | BMCWEB_LOG_DEBUG << "recvMessage() data: " << parser->get().body(); |
| 382 | |
| 383 | unsigned int respCode = parser->get().result_int(); |
| 384 | BMCWEB_LOG_DEBUG << "recvMessage() Header Response Code: " << respCode; |
| 385 | |
| 386 | // Make sure the received response code is valid as defined by |
| 387 | // the associated retry policy |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 388 | if (connPolicy->invalidResp(respCode)) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 389 | { |
| 390 | // The listener failed to receive the Sent-Event |
| 391 | BMCWEB_LOG_ERROR << "recvMessage() Listener Failed to " |
| 392 | "receive Sent-Event. Header Response Code: " |
Sunitha Harish | d7043b3 | 2023-07-11 04:49:03 -0500 | [diff] [blame] | 393 | << respCode << " from " << host << ":" |
| 394 | << std::to_string(port); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 395 | state = ConnState::recvFailed; |
| 396 | waitAndRetry(); |
| 397 | return; |
| 398 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 399 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 400 | // Send is successful |
| 401 | // Reset the counter just in case this was after retrying |
| 402 | retryCount = 0; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 403 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 404 | // Keep the connection alive if server supports it |
| 405 | // Else close the connection |
| 406 | BMCWEB_LOG_DEBUG << "recvMessage() keepalive : " |
| 407 | << parser->keep_alive(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 408 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 409 | // Copy the response into a Response object so that it can be |
| 410 | // processed by the callback function. |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 411 | res.stringResponse = parser->release(); |
| 412 | callback(parser->keep_alive(), connId, res); |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 413 | res.clear(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 414 | } |
| 415 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 416 | static void onTimeout(const std::weak_ptr<ConnectionInfo>& weakSelf, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 417 | const boost::system::error_code& ec) |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 418 | { |
| 419 | if (ec == boost::asio::error::operation_aborted) |
| 420 | { |
| 421 | BMCWEB_LOG_DEBUG |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 422 | << "async_wait failed since the operation is aborted"; |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | if (ec) |
| 426 | { |
| 427 | BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message(); |
| 428 | // If the timer fails, we need to close the socket anyway, same as |
| 429 | // if it expired. |
| 430 | } |
| 431 | std::shared_ptr<ConnectionInfo> self = weakSelf.lock(); |
| 432 | if (self == nullptr) |
| 433 | { |
| 434 | return; |
| 435 | } |
| 436 | self->waitAndRetry(); |
| 437 | } |
| 438 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 439 | void waitAndRetry() |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 440 | { |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 441 | if ((retryCount >= connPolicy->maxRetryAttempts) || |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 442 | (state == ConnState::sslInitFailed)) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 443 | { |
Sunitha Harish | d7043b3 | 2023-07-11 04:49:03 -0500 | [diff] [blame] | 444 | BMCWEB_LOG_ERROR << "Maximum number of retries reached." |
| 445 | << " " << host << ":" << std::to_string(port); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 446 | BMCWEB_LOG_DEBUG << "Retry policy: " |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 447 | << connPolicy->retryPolicyAction; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 448 | |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 449 | if (connPolicy->retryPolicyAction == "TerminateAfterRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 450 | { |
| 451 | // TODO: delete subscription |
| 452 | state = ConnState::terminated; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 453 | } |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 454 | if (connPolicy->retryPolicyAction == "SuspendRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 455 | { |
| 456 | state = ConnState::suspended; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 457 | } |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 458 | |
| 459 | // We want to return a 502 to indicate there was an error with |
| 460 | // the external server |
| 461 | res.result(boost::beast::http::status::bad_gateway); |
| 462 | callback(false, connId, res); |
| 463 | res.clear(); |
| 464 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 465 | // Reset the retrycount to zero so that client can try connecting |
| 466 | // again if needed |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 467 | retryCount = 0; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 468 | return; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 469 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 470 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 471 | retryCount++; |
| 472 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 473 | BMCWEB_LOG_DEBUG << "Attempt retry after " |
| 474 | << std::to_string( |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 475 | connPolicy->retryIntervalSecs.count()) |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 476 | << " seconds. RetryCount = " << retryCount; |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 477 | timer.expires_after(connPolicy->retryIntervalSecs); |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 478 | timer.async_wait(std::bind_front(&ConnectionInfo::onTimerDone, this, |
| 479 | shared_from_this())); |
| 480 | } |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 481 | |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 482 | void onTimerDone(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 483 | const boost::system::error_code& ec) |
| 484 | { |
| 485 | if (ec == boost::asio::error::operation_aborted) |
| 486 | { |
| 487 | BMCWEB_LOG_DEBUG |
| 488 | << "async_wait failed since the operation is aborted" |
| 489 | << ec.message(); |
| 490 | } |
| 491 | else if (ec) |
| 492 | { |
| 493 | BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message(); |
| 494 | // Ignore the error and continue the retry loop to attempt |
| 495 | // sending the event as per the retry policy |
| 496 | } |
| 497 | |
| 498 | // Let's close the connection and restart from resolve. |
| 499 | doClose(true); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 500 | } |
| 501 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 502 | void shutdownConn(bool retry) |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 503 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 504 | boost::beast::error_code ec; |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 505 | conn.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 506 | conn.close(); |
| 507 | |
| 508 | // not_connected happens sometimes so don't bother reporting it. |
| 509 | if (ec && ec != boost::beast::errc::not_connected) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 510 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 511 | BMCWEB_LOG_ERROR << host << ":" << std::to_string(port) |
| 512 | << ", id: " << std::to_string(connId) |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 513 | << " shutdown failed: " << ec.message(); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 514 | } |
Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 515 | else |
| 516 | { |
| 517 | BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port) |
| 518 | << ", id: " << std::to_string(connId) |
| 519 | << " closed gracefully"; |
| 520 | } |
Ed Tanous | ca72376 | 2022-06-28 19:40:39 -0700 | [diff] [blame] | 521 | |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 522 | if (retry) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 523 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 524 | // Now let's try to resend the data |
| 525 | state = ConnState::retry; |
| 526 | doResolve(); |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | state = ConnState::closed; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 531 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 532 | } |
| 533 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 534 | void doClose(bool retry = false) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 535 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 536 | if (!sslConn) |
| 537 | { |
| 538 | shutdownConn(retry); |
| 539 | return; |
| 540 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 541 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 542 | sslConn->async_shutdown( |
| 543 | std::bind_front(&ConnectionInfo::afterSslShutdown, this, |
| 544 | shared_from_this(), retry)); |
| 545 | } |
| 546 | |
| 547 | void afterSslShutdown(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 548 | bool retry, const boost::system::error_code& ec) |
| 549 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 550 | if (ec) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 551 | { |
| 552 | BMCWEB_LOG_ERROR << host << ":" << std::to_string(port) |
| 553 | << ", id: " << std::to_string(connId) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 554 | << " shutdown failed: " << ec.message(); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 555 | } |
Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 556 | else |
| 557 | { |
| 558 | BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port) |
| 559 | << ", id: " << std::to_string(connId) |
| 560 | << " closed gracefully"; |
| 561 | } |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 562 | shutdownConn(retry); |
| 563 | } |
Ed Tanous | ca72376 | 2022-06-28 19:40:39 -0700 | [diff] [blame] | 564 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 565 | void setCipherSuiteTLSext() |
| 566 | { |
| 567 | if (!sslConn) |
| 568 | { |
| 569 | return; |
| 570 | } |
| 571 | // NOTE: The SSL_set_tlsext_host_name is defined in tlsv1.h header |
| 572 | // file but its having old style casting (name is cast to void*). |
| 573 | // Since bmcweb compiler treats all old-style-cast as error, its |
| 574 | // causing the build failure. So replaced the same macro inline and |
| 575 | // did corrected the code by doing static_cast to viod*. This has to |
| 576 | // be fixed in openssl library in long run. Set SNI Hostname (many |
| 577 | // hosts need this to handshake successfully) |
| 578 | if (SSL_ctrl(sslConn->native_handle(), SSL_CTRL_SET_TLSEXT_HOSTNAME, |
| 579 | TLSEXT_NAMETYPE_host_name, |
| 580 | static_cast<void*>(&host.front())) == 0) |
| 581 | |
| 582 | { |
| 583 | boost::beast::error_code ec{static_cast<int>(::ERR_get_error()), |
| 584 | boost::asio::error::get_ssl_category()}; |
| 585 | |
| 586 | BMCWEB_LOG_ERROR << "SSL_set_tlsext_host_name " << host << ":" |
| 587 | << port << ", id: " << std::to_string(connId) |
| 588 | << " failed: " << ec.message(); |
| 589 | // Set state as sslInit failed so that we close the connection |
| 590 | // and take appropriate action as per retry configuration. |
| 591 | state = ConnState::sslInitFailed; |
| 592 | waitAndRetry(); |
| 593 | return; |
| 594 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | public: |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 598 | explicit ConnectionInfo( |
| 599 | boost::asio::io_context& iocIn, const std::string& idIn, |
| 600 | const std::shared_ptr<ConnectionPolicy>& connPolicyIn, |
| 601 | const std::string& destIPIn, uint16_t destPortIn, bool useSSL, |
| 602 | unsigned int connIdIn) : |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 603 | subId(idIn), |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 604 | connPolicy(connPolicyIn), host(destIPIn), port(destPortIn), |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 605 | connId(connIdIn), resolver(iocIn), conn(iocIn), timer(iocIn) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 606 | { |
| 607 | if (useSSL) |
| 608 | { |
| 609 | std::optional<boost::asio::ssl::context> sslCtx = |
| 610 | ensuressl::getSSLClientContext(); |
| 611 | |
| 612 | if (!sslCtx) |
| 613 | { |
| 614 | BMCWEB_LOG_ERROR << "prepareSSLContext failed - " << host << ":" |
| 615 | << port << ", id: " << std::to_string(connId); |
| 616 | // Don't retry if failure occurs while preparing SSL context |
| 617 | // such as certificate is invalid or set cipher failure or set |
| 618 | // host name failure etc... Setting conn state to sslInitFailed |
| 619 | // and connection state will be transitioned to next state |
| 620 | // depending on retry policy set by subscription. |
| 621 | state = ConnState::sslInitFailed; |
| 622 | waitAndRetry(); |
| 623 | return; |
| 624 | } |
| 625 | sslConn.emplace(conn, *sslCtx); |
| 626 | setCipherSuiteTLSext(); |
| 627 | } |
| 628 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 629 | }; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 630 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 631 | class ConnectionPool : public std::enable_shared_from_this<ConnectionPool> |
| 632 | { |
| 633 | private: |
| 634 | boost::asio::io_context& ioc; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 635 | std::string id; |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 636 | std::shared_ptr<ConnectionPolicy> connPolicy; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 637 | std::string destIP; |
| 638 | uint16_t destPort; |
| 639 | bool useSSL; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 640 | std::vector<std::shared_ptr<ConnectionInfo>> connections; |
| 641 | boost::container::devector<PendingRequest> requestQueue; |
| 642 | |
| 643 | friend class HttpClient; |
| 644 | |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 645 | // Configure a connections's request, callback, and retry info in |
| 646 | // preparation to begin sending the request |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 647 | void setConnProps(ConnectionInfo& conn) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 648 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 649 | if (requestQueue.empty()) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 650 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 651 | BMCWEB_LOG_ERROR |
| 652 | << "setConnProps() should not have been called when requestQueue is empty"; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 653 | return; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 654 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 655 | |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 656 | auto nextReq = requestQueue.front(); |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 657 | conn.req = std::move(nextReq.req); |
| 658 | conn.callback = std::move(nextReq.callback); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 659 | |
| 660 | BMCWEB_LOG_DEBUG << "Setting properties for connection " << conn.host |
| 661 | << ":" << std::to_string(conn.port) |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 662 | << ", id: " << std::to_string(conn.connId); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 663 | |
| 664 | // We can remove the request from the queue at this point |
| 665 | requestQueue.pop_front(); |
| 666 | } |
| 667 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 668 | // Gets called as part of callback after request is sent |
| 669 | // Reuses the connection if there are any requests waiting to be sent |
| 670 | // Otherwise closes the connection if it is not a keep-alive |
| 671 | void sendNext(bool keepAlive, uint32_t connId) |
| 672 | { |
| 673 | auto conn = connections[connId]; |
Carson Labrado | 46a8146 | 2022-04-27 21:11:37 +0000 | [diff] [blame] | 674 | |
| 675 | // Allow the connection's handler to be deleted |
| 676 | // This is needed because of Redfish Aggregation passing an |
| 677 | // AsyncResponse shared_ptr to this callback |
| 678 | conn->callback = nullptr; |
| 679 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 680 | // Reuse the connection to send the next request in the queue |
| 681 | if (!requestQueue.empty()) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 682 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 683 | BMCWEB_LOG_DEBUG << std::to_string(requestQueue.size()) |
| 684 | << " requests remaining in queue for " << destIP |
| 685 | << ":" << std::to_string(destPort) |
| 686 | << ", reusing connnection " |
| 687 | << std::to_string(connId); |
| 688 | |
| 689 | setConnProps(*conn); |
| 690 | |
| 691 | if (keepAlive) |
| 692 | { |
| 693 | conn->sendMessage(); |
| 694 | } |
| 695 | else |
| 696 | { |
| 697 | // Server is not keep-alive enabled so we need to close the |
| 698 | // connection and then start over from resolve |
| 699 | conn->doClose(); |
| 700 | conn->doResolve(); |
| 701 | } |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | // No more messages to send so close the connection if necessary |
| 706 | if (keepAlive) |
| 707 | { |
| 708 | conn->state = ConnState::idle; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 709 | } |
| 710 | else |
| 711 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 712 | // Abort the connection since server is not keep-alive enabled |
| 713 | conn->state = ConnState::abortConnection; |
| 714 | conn->doClose(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 715 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 716 | } |
| 717 | |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 718 | void sendData(std::string&& data, const std::string& destUri, |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 719 | const boost::beast::http::fields& httpHeader, |
| 720 | const boost::beast::http::verb verb, |
Ed Tanous | 6b3db60 | 2022-06-28 19:41:44 -0700 | [diff] [blame] | 721 | const std::function<void(Response&)>& resHandler) |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 722 | { |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 723 | // Construct the request to be sent |
| 724 | boost::beast::http::request<boost::beast::http::string_body> thisReq( |
| 725 | verb, destUri, 11, "", httpHeader); |
| 726 | thisReq.set(boost::beast::http::field::host, destIP); |
| 727 | thisReq.keep_alive(true); |
| 728 | thisReq.body() = std::move(data); |
| 729 | thisReq.prepare_payload(); |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 730 | auto cb = std::bind_front(&ConnectionPool::afterSendData, |
| 731 | weak_from_this(), resHandler); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 732 | // Reuse an existing connection if one is available |
| 733 | for (unsigned int i = 0; i < connections.size(); i++) |
| 734 | { |
| 735 | auto conn = connections[i]; |
| 736 | if ((conn->state == ConnState::idle) || |
| 737 | (conn->state == ConnState::initialized) || |
| 738 | (conn->state == ConnState::closed)) |
| 739 | { |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 740 | conn->req = std::move(thisReq); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 741 | conn->callback = std::move(cb); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 742 | std::string commonMsg = std::to_string(i) + " from pool " + |
| 743 | destIP + ":" + std::to_string(destPort); |
| 744 | |
| 745 | if (conn->state == ConnState::idle) |
| 746 | { |
| 747 | BMCWEB_LOG_DEBUG << "Grabbing idle connection " |
| 748 | << commonMsg; |
| 749 | conn->sendMessage(); |
| 750 | } |
| 751 | else |
| 752 | { |
| 753 | BMCWEB_LOG_DEBUG << "Reusing existing connection " |
| 754 | << commonMsg; |
| 755 | conn->doResolve(); |
| 756 | } |
| 757 | return; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // All connections in use so create a new connection or add request to |
| 762 | // the queue |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 763 | if (connections.size() < connPolicy->maxConnections) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 764 | { |
| 765 | BMCWEB_LOG_DEBUG << "Adding new connection to pool " << destIP |
| 766 | << ":" << std::to_string(destPort); |
| 767 | auto conn = addConnection(); |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 768 | conn->req = std::move(thisReq); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 769 | conn->callback = std::move(cb); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 770 | conn->doResolve(); |
| 771 | } |
| 772 | else if (requestQueue.size() < maxRequestQueueSize) |
| 773 | { |
Sunitha Harish | d7043b3 | 2023-07-11 04:49:03 -0500 | [diff] [blame] | 774 | BMCWEB_LOG_ERROR << "Max pool size reached. Adding data to queue." |
| 775 | << destIP << ":" << std::to_string(destPort); |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 776 | requestQueue.emplace_back(std::move(thisReq), std::move(cb)); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 777 | } |
| 778 | else |
| 779 | { |
Carson Labrado | 43e14d3 | 2022-11-09 00:25:20 +0000 | [diff] [blame] | 780 | // If we can't buffer the request then we should let the callback |
| 781 | // handle a 429 Too Many Requests dummy response |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 782 | BMCWEB_LOG_ERROR << destIP << ":" << std::to_string(destPort) |
| 783 | << " request queue full. Dropping request."; |
Carson Labrado | 43e14d3 | 2022-11-09 00:25:20 +0000 | [diff] [blame] | 784 | Response dummyRes; |
| 785 | dummyRes.result(boost::beast::http::status::too_many_requests); |
| 786 | resHandler(dummyRes); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 787 | } |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 788 | } |
| 789 | |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 790 | // Callback to be called once the request has been sent |
| 791 | static void afterSendData(const std::weak_ptr<ConnectionPool>& weakSelf, |
| 792 | const std::function<void(Response&)>& resHandler, |
| 793 | bool keepAlive, uint32_t connId, Response& res) |
| 794 | { |
| 795 | // Allow provided callback to perform additional processing of the |
| 796 | // request |
| 797 | resHandler(res); |
| 798 | |
| 799 | // If requests remain in the queue then we want to reuse this |
| 800 | // connection to send the next request |
| 801 | std::shared_ptr<ConnectionPool> self = weakSelf.lock(); |
| 802 | if (!self) |
| 803 | { |
| 804 | BMCWEB_LOG_CRITICAL << self << " Failed to capture connection"; |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | self->sendNext(keepAlive, connId); |
| 809 | } |
| 810 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 811 | std::shared_ptr<ConnectionInfo>& addConnection() |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 812 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 813 | unsigned int newId = static_cast<unsigned int>(connections.size()); |
| 814 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 815 | auto& ret = connections.emplace_back(std::make_shared<ConnectionInfo>( |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 816 | ioc, id, connPolicy, destIP, destPort, useSSL, newId)); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 817 | |
| 818 | BMCWEB_LOG_DEBUG << "Added connection " |
| 819 | << std::to_string(connections.size() - 1) |
| 820 | << " to pool " << destIP << ":" |
| 821 | << std::to_string(destPort); |
| 822 | |
| 823 | return ret; |
| 824 | } |
| 825 | |
| 826 | public: |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 827 | explicit ConnectionPool( |
| 828 | boost::asio::io_context& iocIn, const std::string& idIn, |
| 829 | const std::shared_ptr<ConnectionPolicy>& connPolicyIn, |
| 830 | const std::string& destIPIn, uint16_t destPortIn, bool useSSLIn) : |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 831 | ioc(iocIn), |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 832 | id(idIn), connPolicy(connPolicyIn), destIP(destIPIn), |
| 833 | destPort(destPortIn), useSSL(useSSLIn) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 834 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 835 | BMCWEB_LOG_DEBUG << "Initializing connection pool for " << destIP << ":" |
| 836 | << std::to_string(destPort); |
| 837 | |
| 838 | // Initialize the pool with a single connection |
| 839 | addConnection(); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 840 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 841 | }; |
| 842 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 843 | class HttpClient |
| 844 | { |
| 845 | private: |
| 846 | std::unordered_map<std::string, std::shared_ptr<ConnectionPool>> |
| 847 | connectionPools; |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 848 | boost::asio::io_context& ioc; |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 849 | std::shared_ptr<ConnectionPolicy> connPolicy; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 850 | |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 851 | // Used as a dummy callback by sendData() in order to call |
| 852 | // sendDataWithCallback() |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 853 | static void genericResHandler(const Response& res) |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 854 | { |
| 855 | BMCWEB_LOG_DEBUG << "Response handled with return code: " |
| 856 | << std::to_string(res.resultInt()); |
Ed Tanous | 4ee8e21 | 2022-05-28 09:42:51 -0700 | [diff] [blame] | 857 | } |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 858 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 859 | public: |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 860 | HttpClient() = delete; |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 861 | explicit HttpClient(boost::asio::io_context& iocIn, |
| 862 | const std::shared_ptr<ConnectionPolicy>& connPolicyIn) : |
| 863 | ioc(iocIn), |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 864 | connPolicy(connPolicyIn) |
| 865 | {} |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 866 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 867 | HttpClient(const HttpClient&) = delete; |
| 868 | HttpClient& operator=(const HttpClient&) = delete; |
| 869 | HttpClient(HttpClient&&) = delete; |
| 870 | HttpClient& operator=(HttpClient&&) = delete; |
| 871 | ~HttpClient() = default; |
| 872 | |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 873 | // Send a request to destIP:destPort where additional processing of the |
| 874 | // result is not required |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 875 | void sendData(std::string&& data, const std::string& destIP, |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 876 | uint16_t destPort, const std::string& destUri, bool useSSL, |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 877 | const boost::beast::http::fields& httpHeader, |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 878 | const boost::beast::http::verb verb) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 879 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 880 | const std::function<void(Response&)> cb = genericResHandler; |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 881 | sendDataWithCallback(std::move(data), destIP, destPort, destUri, useSSL, |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 882 | httpHeader, verb, cb); |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | // Send request to destIP:destPort and use the provided callback to |
| 886 | // handle the response |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 887 | void sendDataWithCallback(std::string&& data, const std::string& destIP, |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 888 | uint16_t destPort, const std::string& destUri, |
| 889 | bool useSSL, |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 890 | const boost::beast::http::fields& httpHeader, |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 891 | const boost::beast::http::verb verb, |
Ed Tanous | 6b3db60 | 2022-06-28 19:41:44 -0700 | [diff] [blame] | 892 | const std::function<void(Response&)>& resHandler) |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 893 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 894 | std::string clientKey = useSSL ? "https" : "http"; |
| 895 | clientKey += destIP; |
| 896 | clientKey += ":"; |
| 897 | clientKey += std::to_string(destPort); |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 898 | auto pool = connectionPools.try_emplace(clientKey); |
| 899 | if (pool.first->second == nullptr) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 900 | { |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 901 | pool.first->second = std::make_shared<ConnectionPool>( |
| 902 | ioc, clientKey, connPolicy, destIP, destPort, useSSL); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 903 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 904 | // Send the data using either the existing connection pool or the newly |
| 905 | // created connection pool |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 906 | pool.first->second->sendData(std::move(data), destUri, httpHeader, verb, |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 907 | resHandler); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 908 | } |
| 909 | }; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 910 | } // namespace crow |