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