1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
4 #include "include/compat.h"
5 #include "common/errno.h"
10 #include <curl/multi.h>
12 #include "rgw_common.h"
13 #include "rgw_http_client.h"
14 #include "rgw_http_errors.h"
15 #include "common/async/completion.h"
16 #include "common/RefCountedObj.h"
18 #include "rgw_coroutine.h"
19 #include "rgw_tools.h"
22 #include <string_view>
24 #define dout_context g_ceph_context
25 #define dout_subsys ceph_subsys_rgw
29 RGWHTTPManager
*rgw_http_manager
;
33 static void do_curl_easy_cleanup(RGWCurlHandle
*curl_handle
);
35 struct rgw_http_req_data
: public RefCountedObject
{
36 RGWCurlHandle
*curl_handle
{nullptr};
37 curl_slist
*h
{nullptr};
40 std::atomic
<bool> done
= { false };
41 RGWHTTPClient
*client
{nullptr};
42 rgw_io_id control_io_id
;
43 void *user_info
{nullptr};
44 bool registered
{false};
45 RGWHTTPManager
*mgr
{nullptr};
46 char error_buf
[CURL_ERROR_SIZE
];
47 bool write_paused
{false};
48 bool read_paused
{false};
50 optional
<int> user_ret
;
52 ceph::mutex lock
= ceph::make_mutex("rgw_http_req_data::lock");
53 ceph::condition_variable cond
;
55 using Signature
= void(boost::system::error_code
);
56 using Completion
= ceph::async::Completion
<Signature
>;
57 std::unique_ptr
<Completion
> completion
;
59 rgw_http_req_data() : id(-1) {
60 // FIPS zeroization audit 20191115: this memset is not security related.
61 memset(error_buf
, 0, sizeof(error_buf
));
64 template <typename ExecutionContext
, typename CompletionToken
>
65 auto async_wait(ExecutionContext
& ctx
, CompletionToken
&& token
) {
66 boost::asio::async_completion
<CompletionToken
, Signature
> init(token
);
67 auto& handler
= init
.completion_handler
;
69 std::unique_lock l
{lock
};
70 completion
= Completion::create(ctx
.get_executor(), std::move(handler
));
72 return init
.result
.get();
75 int wait(optional_yield y
) {
80 auto& context
= y
.get_io_context();
81 auto& yield
= y
.get_yield_context();
82 boost::system::error_code ec
;
83 async_wait(context
, yield
[ec
]);
86 // work on asio threads should be asynchronous, so warn when they block
88 dout(20) << "WARNING: blocking http request" << dendl
;
90 std::unique_lock l
{lock
};
91 cond
.wait(l
, [this]{return done
==true;});
95 void set_state(int bitmask
);
97 void finish(int r
, long http_status
= -1) {
98 std::lock_guard l
{lock
};
99 if (http_status
!= -1) {
101 client
->set_http_status(http_status
);
106 do_curl_easy_cleanup(curl_handle
);
109 curl_slist_free_all(h
);
115 boost::system::error_code
ec(-ret
, boost::system::system_category());
116 Completion::post(std::move(completion
), ec
);
127 std::lock_guard l
{lock
};
131 RGWHTTPManager
*get_manager() {
132 std::lock_guard l
{lock
};
136 CURL
*get_easy_handle() const;
139 struct RGWCurlHandle
{
144 explicit RGWCurlHandle(CURL
* h
) : uses(0), h(h
) {};
150 void rgw_http_req_data::set_state(int bitmask
) {
151 /* no need to lock here, moreover curl_easy_pause() might trigger
152 * the data receive callback :/
154 CURLcode rc
= curl_easy_pause(**curl_handle
, bitmask
);
155 if (rc
!= CURLE_OK
) {
156 dout(0) << "ERROR: curl_easy_pause() returned rc=" << rc
<< dendl
;
161 class RGWCurlHandles
: public Thread
{
163 ceph::mutex cleaner_lock
= ceph::make_mutex("RGWCurlHandles::cleaner_lock");
164 std::vector
<RGWCurlHandle
*> saved_curl
;
165 int cleaner_shutdown
;
166 ceph::condition_variable cleaner_cond
;
169 cleaner_shutdown
{0} {
172 RGWCurlHandle
* get_curl_handle();
173 void release_curl_handle_now(RGWCurlHandle
* curl
);
174 void release_curl_handle(RGWCurlHandle
* curl
);
175 void flush_curl_handles();
180 RGWCurlHandle
* RGWCurlHandles::get_curl_handle() {
181 RGWCurlHandle
* curl
= 0;
184 std::lock_guard lock
{cleaner_lock
};
185 if (!saved_curl
.empty()) {
186 curl
= *saved_curl
.begin();
187 saved_curl
.erase(saved_curl
.begin());
191 } else if ((h
= curl_easy_init())) {
192 curl
= new RGWCurlHandle
{h
};
199 void RGWCurlHandles::release_curl_handle_now(RGWCurlHandle
* curl
)
201 curl_easy_cleanup(**curl
);
205 void RGWCurlHandles::release_curl_handle(RGWCurlHandle
* curl
)
207 if (cleaner_shutdown
) {
208 release_curl_handle_now(curl
);
210 curl_easy_reset(**curl
);
211 std::lock_guard lock
{cleaner_lock
};
212 curl
->lastuse
= mono_clock::now();
213 saved_curl
.insert(saved_curl
.begin(), 1, curl
);
217 void* RGWCurlHandles::entry()
220 std::unique_lock lock
{cleaner_lock
};
223 if (cleaner_shutdown
) {
224 if (saved_curl
.empty())
227 cleaner_cond
.wait_for(lock
, std::chrono::seconds(MAXIDLE
));
229 mono_time now
= mono_clock::now();
230 while (!saved_curl
.empty()) {
231 auto cend
= saved_curl
.end();
234 if (!cleaner_shutdown
&& now
- curl
->lastuse
< std::chrono::seconds(MAXIDLE
))
236 saved_curl
.erase(cend
);
237 release_curl_handle_now(curl
);
243 void RGWCurlHandles::stop()
245 std::lock_guard lock
{cleaner_lock
};
246 cleaner_shutdown
= 1;
247 cleaner_cond
.notify_all();
250 void RGWCurlHandles::flush_curl_handles()
254 if (!saved_curl
.empty()) {
255 dout(0) << "ERROR: " << __func__
<< " failed final cleanup" << dendl
;
257 saved_curl
.shrink_to_fit();
260 CURL
*rgw_http_req_data::get_easy_handle() const
262 return **curl_handle
;
265 static RGWCurlHandles
*handles
;
267 static RGWCurlHandle
*do_curl_easy_init()
269 return handles
->get_curl_handle();
272 static void do_curl_easy_cleanup(RGWCurlHandle
*curl_handle
)
274 handles
->release_curl_handle(curl_handle
);
277 // XXX make this part of the token cache? (but that's swift-only;
278 // and this especially needs to integrates with s3...)
280 void rgw_setup_saved_curl_handles()
282 handles
= new RGWCurlHandles();
283 handles
->create("rgw_curl");
286 void rgw_release_all_curl_handles()
288 handles
->flush_curl_handles();
292 void RGWIOProvider::assign_io(RGWIOIDProvider
& io_id_provider
, int io_type
)
295 id
= io_id_provider
.get_next();
299 RGWHTTPClient::RGWHTTPClient(CephContext
*cct
,
300 const string
& _method
,
302 : NoDoutPrefix(cct
, dout_subsys
),
304 http_status(HTTP_STATUS_NOSTATUS
),
306 verify_ssl(cct
->_conf
->rgw_verify_ssl
),
313 std::ostream
& RGWHTTPClient::gen_prefix(std::ostream
& out
) const
315 out
<< "http_client[" << method
<< "/" << url
<< "]";
319 void RGWHTTPClient::init()
321 auto pos
= url
.find("://");
322 if (pos
== string::npos
) {
327 protocol
= url
.substr(0, pos
);
331 auto host_end_pos
= url
.find("/", pos
);
332 if (host_end_pos
== string::npos
) {
333 host
= url
.substr(pos
);
337 host
= url
.substr(pos
, host_end_pos
- pos
);
338 resource_prefix
= url
.substr(host_end_pos
+ 1);
339 if (resource_prefix
.size() > 0 && resource_prefix
[resource_prefix
.size() - 1] != '/') {
340 resource_prefix
.append("/");
345 * the following set of callbacks will be called either on RGWHTTPManager::process(),
346 * or via the RGWHTTPManager async processing.
348 size_t RGWHTTPClient::receive_http_header(void * const ptr
,
353 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
354 size_t len
= size
* nmemb
;
356 std::lock_guard l
{req_data
->lock
};
358 if (!req_data
->registered
) {
362 int ret
= req_data
->client
->receive_header(ptr
, size
* nmemb
);
364 dout(5) << "WARNING: client->receive_header() returned ret=" << ret
<< dendl
;
365 req_data
->user_ret
= ret
;
366 return CURLE_WRITE_ERROR
;
372 size_t RGWHTTPClient::receive_http_data(void * const ptr
,
377 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
378 size_t len
= size
* nmemb
;
382 RGWHTTPClient
*client
;
385 std::lock_guard l
{req_data
->lock
};
386 if (!req_data
->registered
) {
390 client
= req_data
->client
;
393 size_t& skip_bytes
= client
->receive_pause_skip
;
395 if (skip_bytes
>= len
) {
400 int ret
= client
->receive_data((char *)ptr
+ skip_bytes
, len
- skip_bytes
, &pause
);
402 dout(5) << "WARNING: client->receive_data() returned ret=" << ret
<< dendl
;
403 req_data
->user_ret
= ret
;
404 return CURLE_WRITE_ERROR
;
408 dout(20) << "RGWHTTPClient::receive_http_data(): pause" << dendl
;
410 std::lock_guard l
{req_data
->lock
};
411 req_data
->read_paused
= true;
412 return CURL_WRITEFUNC_PAUSE
;
420 size_t RGWHTTPClient::send_http_data(void * const ptr
,
425 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
427 RGWHTTPClient
*client
;
430 std::lock_guard l
{req_data
->lock
};
432 if (!req_data
->registered
) {
436 client
= req_data
->client
;
441 int ret
= client
->send_data(ptr
, size
* nmemb
, &pause
);
443 dout(5) << "WARNING: client->send_data() returned ret=" << ret
<< dendl
;
444 req_data
->user_ret
= ret
;
445 return CURLE_READ_ERROR
;
450 std::lock_guard l
{req_data
->lock
};
451 req_data
->write_paused
= true;
452 return CURL_READFUNC_PAUSE
;
458 ceph::mutex
& RGWHTTPClient::get_req_lock()
460 return req_data
->lock
;
463 void RGWHTTPClient::_set_write_paused(bool pause
)
465 ceph_assert(ceph_mutex_is_locked(req_data
->lock
));
467 RGWHTTPManager
*mgr
= req_data
->mgr
;
468 if (pause
== req_data
->write_paused
) {
472 mgr
->set_request_state(this, SET_WRITE_PAUSED
);
474 mgr
->set_request_state(this, SET_WRITE_RESUME
);
478 void RGWHTTPClient::_set_read_paused(bool pause
)
480 ceph_assert(ceph_mutex_is_locked(req_data
->lock
));
482 RGWHTTPManager
*mgr
= req_data
->mgr
;
483 if (pause
== req_data
->read_paused
) {
487 mgr
->set_request_state(this, SET_READ_PAUSED
);
489 mgr
->set_request_state(this, SET_READ_RESUME
);
493 static curl_slist
*headers_to_slist(param_vec_t
& headers
)
495 curl_slist
*h
= NULL
;
497 param_vec_t::iterator iter
;
498 for (iter
= headers
.begin(); iter
!= headers
.end(); ++iter
) {
499 pair
<string
, string
>& p
= *iter
;
500 string val
= p
.first
;
502 if (strncmp(val
.c_str(), "HTTP_", 5) == 0) {
506 /* we need to convert all underscores into dashes as some web servers forbid them
507 * in the http header field names
509 for (size_t i
= 0; i
< val
.size(); i
++) {
515 val
= camelcase_dash_http_attr(val
);
517 // curl won't send headers with empty values unless it ends with a ; instead
518 if (p
.second
.empty()) {
522 val
.append(p
.second
);
524 h
= curl_slist_append(h
, val
.c_str());
530 static bool is_upload_request(const string
& method
)
532 return method
== "POST" || method
== "PUT";
536 * process a single simple one off request
538 int RGWHTTPClient::process(optional_yield y
)
540 return RGWHTTP::process(this, y
);
543 string
RGWHTTPClient::to_str()
545 string method_str
= (method
.empty() ? "<no-method>" : method
);
546 string url_str
= (url
.empty() ? "<no-url>" : url
);
547 return method_str
+ " " + url_str
;
550 int RGWHTTPClient::get_req_retcode()
556 return req_data
->get_retcode();
560 * init request, will be used later with RGWHTTPManager
562 int RGWHTTPClient::init_request(rgw_http_req_data
*_req_data
)
564 ceph_assert(!req_data
);
566 req_data
= _req_data
;
568 req_data
->curl_handle
= do_curl_easy_init();
570 CURL
*easy_handle
= req_data
->get_easy_handle();
572 dout(20) << "sending request to " << url
<< dendl
;
574 curl_slist
*h
= headers_to_slist(headers
);
578 curl_easy_setopt(easy_handle
, CURLOPT_CUSTOMREQUEST
, method
.c_str());
579 curl_easy_setopt(easy_handle
, CURLOPT_URL
, url
.c_str());
580 curl_easy_setopt(easy_handle
, CURLOPT_NOPROGRESS
, 1L);
581 curl_easy_setopt(easy_handle
, CURLOPT_NOSIGNAL
, 1L);
582 curl_easy_setopt(easy_handle
, CURLOPT_HEADERFUNCTION
, receive_http_header
);
583 curl_easy_setopt(easy_handle
, CURLOPT_WRITEHEADER
, (void *)req_data
);
584 curl_easy_setopt(easy_handle
, CURLOPT_WRITEFUNCTION
, receive_http_data
);
585 curl_easy_setopt(easy_handle
, CURLOPT_WRITEDATA
, (void *)req_data
);
586 curl_easy_setopt(easy_handle
, CURLOPT_ERRORBUFFER
, (void *)req_data
->error_buf
);
587 curl_easy_setopt(easy_handle
, CURLOPT_LOW_SPEED_TIME
, cct
->_conf
->rgw_curl_low_speed_time
);
588 curl_easy_setopt(easy_handle
, CURLOPT_LOW_SPEED_LIMIT
, cct
->_conf
->rgw_curl_low_speed_limit
);
589 curl_easy_setopt(easy_handle
, CURLOPT_READFUNCTION
, send_http_data
);
590 curl_easy_setopt(easy_handle
, CURLOPT_READDATA
, (void *)req_data
);
591 curl_easy_setopt(easy_handle
, CURLOPT_BUFFERSIZE
, cct
->_conf
->rgw_curl_buffersize
);
592 if (send_data_hint
|| is_upload_request(method
)) {
593 curl_easy_setopt(easy_handle
, CURLOPT_UPLOAD
, 1L);
596 // TODO: prevent overflow by using curl_off_t
597 // and: CURLOPT_INFILESIZE_LARGE, CURLOPT_POSTFIELDSIZE_LARGE
598 const long size
= send_len
;
599 curl_easy_setopt(easy_handle
, CURLOPT_INFILESIZE
, size
);
600 if (method
== "POST") {
601 curl_easy_setopt(easy_handle
, CURLOPT_POSTFIELDSIZE
, size
);
602 // TODO: set to size smaller than 1MB should prevent the "Expect" field
603 // from being sent. So explicit removal is not needed
604 h
= curl_slist_append(h
, "Expect:");
608 if (method
== "HEAD") {
609 curl_easy_setopt(easy_handle
, CURLOPT_NOBODY
, 1L);
613 curl_easy_setopt(easy_handle
, CURLOPT_HTTPHEADER
, (void *)h
);
616 curl_easy_setopt(easy_handle
, CURLOPT_SSL_VERIFYPEER
, 0L);
617 curl_easy_setopt(easy_handle
, CURLOPT_SSL_VERIFYHOST
, 0L);
618 dout(20) << "ssl verification is set to off" << dendl
;
620 if (!ca_path
.empty()) {
621 curl_easy_setopt(easy_handle
, CURLOPT_CAINFO
, ca_path
.c_str());
622 dout(20) << "using custom ca cert "<< ca_path
.c_str() << " for ssl" << dendl
;
624 if (!client_cert
.empty()) {
625 if (!client_key
.empty()) {
626 curl_easy_setopt(easy_handle
, CURLOPT_SSLCERT
, client_cert
.c_str());
627 curl_easy_setopt(easy_handle
, CURLOPT_SSLKEY
, client_key
.c_str());
628 dout(20) << "using custom client cert " << client_cert
.c_str()
629 << " and private key " << client_key
.c_str() << dendl
;
631 dout(5) << "private key is missing for client certificate" << dendl
;
635 curl_easy_setopt(easy_handle
, CURLOPT_PRIVATE
, (void *)req_data
);
636 curl_easy_setopt(easy_handle
, CURLOPT_TIMEOUT
, req_timeout
);
641 bool RGWHTTPClient::is_done()
643 return req_data
->is_done();
647 * wait for async request to complete
649 int RGWHTTPClient::wait(optional_yield y
)
651 return req_data
->wait(y
);
654 void RGWHTTPClient::cancel()
657 RGWHTTPManager
*http_manager
= req_data
->mgr
;
659 http_manager
->remove_request(this);
664 RGWHTTPClient::~RGWHTTPClient()
673 int RGWHTTPHeadersCollector::receive_header(void * const ptr
, const size_t len
)
675 const std::string_view
header_line(static_cast<const char *>(ptr
), len
);
677 /* We're tokening the line that way due to backward compatibility. */
678 const size_t sep_loc
= header_line
.find_first_of(" \t:");
680 if (std::string_view::npos
== sep_loc
) {
681 /* Wrongly formatted header? Just skip it. */
685 header_name_t
name(header_line
.substr(0, sep_loc
));
686 if (0 == relevant_headers
.count(name
)) {
687 /* Not interested in this particular header. */
691 const auto value_part
= header_line
.substr(sep_loc
+ 1);
693 /* Skip spaces and tabs after the separator. */
694 const size_t val_loc_s
= value_part
.find_first_not_of(' ');
695 const size_t val_loc_e
= value_part
.find_first_of("\r\n");
697 if (std::string_view::npos
== val_loc_s
||
698 std::string_view::npos
== val_loc_e
) {
699 /* Empty value case. */
700 found_headers
.emplace(name
, header_value_t());
702 found_headers
.emplace(name
, header_value_t(
703 value_part
.substr(val_loc_s
, val_loc_e
- val_loc_s
)));
709 int RGWHTTPTransceiver::send_data(void* ptr
, size_t len
, bool* pause
)
711 int length_to_copy
= 0;
712 if (post_data_index
< post_data
.length()) {
713 length_to_copy
= min(post_data
.length() - post_data_index
, len
);
714 memcpy(ptr
, post_data
.data() + post_data_index
, length_to_copy
);
715 post_data_index
+= length_to_copy
;
717 return length_to_copy
;
721 static int clear_signal(int fd
)
723 // since we're in non-blocking mode, we can try to read a lot more than
724 // one signal from signal_thread() to avoid later wakeups. non-blocking reads
725 // are also required to support the curl_multi_wait bug workaround
726 std::array
<char, 256> buf
{};
727 int ret
= ::read(fd
, (void *)buf
.data(), buf
.size());
730 return ret
== -EAGAIN
? 0 : ret
; // clear EAGAIN
735 #if HAVE_CURL_MULTI_WAIT
737 static std::once_flag detect_flag
;
738 static bool curl_multi_wait_bug_present
= false;
740 static int detect_curl_multi_wait_bug(CephContext
*cct
, CURLM
*handle
,
741 int write_fd
, int read_fd
)
745 // write to write_fd so that read_fd becomes readable
747 ret
= ::write(write_fd
, &buf
, sizeof(buf
));
750 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): write() returned " << ret
<< dendl
;
754 // pass read_fd in extra_fds for curl_multi_wait()
756 struct curl_waitfd wait_fd
;
758 wait_fd
.fd
= read_fd
;
759 wait_fd
.events
= CURL_WAIT_POLLIN
;
762 ret
= curl_multi_wait(handle
, &wait_fd
, 1, 0, &num_fds
);
763 if (ret
!= CURLM_OK
) {
764 ldout(cct
, 0) << "ERROR: curl_multi_wait() returned " << ret
<< dendl
;
768 // curl_multi_wait should flag revents when extra_fd is readable. if it
769 // doesn't, the bug is present and we can't rely on revents
770 if (wait_fd
.revents
== 0) {
771 curl_multi_wait_bug_present
= true;
772 ldout(cct
, 0) << "WARNING: detected a version of libcurl which contains a "
773 "bug in curl_multi_wait(). enabling a workaround that may degrade "
774 "performance slightly." << dendl
;
777 return clear_signal(read_fd
);
780 static bool is_signaled(const curl_waitfd
& wait_fd
)
782 if (wait_fd
.fd
< 0) {
787 if (curl_multi_wait_bug_present
) {
788 // we can't rely on revents, so we always return true if a wait_fd is given.
789 // this means we'll be trying a non-blocking read on this fd every time that
790 // curl_multi_wait() wakes up
794 return wait_fd
.revents
> 0;
797 static int do_curl_wait(CephContext
*cct
, CURLM
*handle
, int signal_fd
)
800 struct curl_waitfd wait_fd
;
802 wait_fd
.fd
= signal_fd
;
803 wait_fd
.events
= CURL_WAIT_POLLIN
;
806 int ret
= curl_multi_wait(handle
, &wait_fd
, 1, cct
->_conf
->rgw_curl_wait_timeout_ms
, &num_fds
);
808 ldout(cct
, 0) << "ERROR: curl_multi_wait() returned " << ret
<< dendl
;
812 if (is_signaled(wait_fd
)) {
813 ret
= clear_signal(signal_fd
);
815 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): read() returned " << ret
<< dendl
;
824 static int do_curl_wait(CephContext
*cct
, CURLM
*handle
, int signal_fd
)
835 /* get file descriptors from the transfers */
836 int ret
= curl_multi_fdset(handle
, &fdread
, &fdwrite
, &fdexcep
, &maxfd
);
838 ldout(cct
, 0) << "ERROR: curl_multi_fdset returned " << ret
<< dendl
;
843 FD_SET(signal_fd
, &fdread
);
844 if (signal_fd
>= maxfd
) {
845 maxfd
= signal_fd
+ 1;
849 /* forcing a strict timeout, as the returned fdsets might not reference all fds we wait on */
850 uint64_t to
= cct
->_conf
->rgw_curl_wait_timeout_ms
;
851 #define RGW_CURL_TIMEOUT 1000
853 to
= RGW_CURL_TIMEOUT
;
854 struct timeval timeout
;
855 timeout
.tv_sec
= to
/ 1000;
856 timeout
.tv_usec
= to
% 1000;
858 ret
= select(maxfd
+1, &fdread
, &fdwrite
, &fdexcep
, &timeout
);
861 ldout(cct
, 0) << "ERROR: select returned " << ret
<< dendl
;
865 if (signal_fd
> 0 && FD_ISSET(signal_fd
, &fdread
)) {
866 ret
= clear_signal(signal_fd
);
868 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): read() returned " << ret
<< dendl
;
878 void *RGWHTTPManager::ReqsThread::entry()
880 manager
->reqs_thread_entry();
885 * RGWHTTPManager has two modes of operation: threaded and non-threaded.
887 RGWHTTPManager::RGWHTTPManager(CephContext
*_cct
, RGWCompletionManager
*_cm
) : cct(_cct
),
890 multi_handle
= (void *)curl_multi_init();
895 RGWHTTPManager::~RGWHTTPManager() {
898 curl_multi_cleanup((CURLM
*)multi_handle
);
901 void RGWHTTPManager::register_request(rgw_http_req_data
*req_data
)
903 std::unique_lock rl
{reqs_lock
};
904 req_data
->id
= num_reqs
;
905 req_data
->registered
= true;
906 reqs
[num_reqs
] = req_data
;
908 ldout(cct
, 20) << __func__
<< " mgr=" << this << " req_data->id=" << req_data
->id
<< ", curl_handle=" << req_data
->curl_handle
<< dendl
;
911 bool RGWHTTPManager::unregister_request(rgw_http_req_data
*req_data
)
913 std::unique_lock rl
{reqs_lock
};
914 if (!req_data
->registered
) {
918 req_data
->registered
= false;
919 unregistered_reqs
.push_back(req_data
);
920 ldout(cct
, 20) << __func__
<< " mgr=" << this << " req_data->id=" << req_data
->id
<< ", curl_handle=" << req_data
->curl_handle
<< dendl
;
924 void RGWHTTPManager::complete_request(rgw_http_req_data
*req_data
)
926 std::unique_lock rl
{reqs_lock
};
927 _complete_request(req_data
);
930 void RGWHTTPManager::_complete_request(rgw_http_req_data
*req_data
)
932 map
<uint64_t, rgw_http_req_data
*>::iterator iter
= reqs
.find(req_data
->id
);
933 if (iter
!= reqs
.end()) {
937 std::lock_guard l
{req_data
->lock
};
938 req_data
->mgr
= nullptr;
940 if (completion_mgr
) {
941 completion_mgr
->complete(NULL
, req_data
->control_io_id
, req_data
->user_info
);
947 void RGWHTTPManager::finish_request(rgw_http_req_data
*req_data
, int ret
, long http_status
)
949 req_data
->finish(ret
, http_status
);
950 complete_request(req_data
);
953 void RGWHTTPManager::_finish_request(rgw_http_req_data
*req_data
, int ret
)
955 req_data
->finish(ret
);
956 _complete_request(req_data
);
959 void RGWHTTPManager::_set_req_state(set_state
& ss
)
961 ss
.req
->set_state(ss
.bitmask
);
964 * hook request to the curl multi handle
966 int RGWHTTPManager::link_request(rgw_http_req_data
*req_data
)
968 ldout(cct
, 20) << __func__
<< " req_data=" << req_data
<< " req_data->id=" << req_data
->id
<< ", curl_handle=" << req_data
->curl_handle
<< dendl
;
969 CURLMcode mstatus
= curl_multi_add_handle((CURLM
*)multi_handle
, req_data
->get_easy_handle());
971 dout(0) << "ERROR: failed on curl_multi_add_handle, status=" << mstatus
<< dendl
;
978 * unhook request from the curl multi handle, and finish request if it wasn't finished yet as
979 * there will be no more processing on this request
981 void RGWHTTPManager::_unlink_request(rgw_http_req_data
*req_data
)
983 if (req_data
->curl_handle
) {
984 curl_multi_remove_handle((CURLM
*)multi_handle
, req_data
->get_easy_handle());
986 if (!req_data
->is_done()) {
987 _finish_request(req_data
, -ECANCELED
);
991 void RGWHTTPManager::unlink_request(rgw_http_req_data
*req_data
)
993 std::unique_lock wl
{reqs_lock
};
994 _unlink_request(req_data
);
997 void RGWHTTPManager::manage_pending_requests()
999 reqs_lock
.lock_shared();
1000 if (max_threaded_req
== num_reqs
&&
1001 unregistered_reqs
.empty() &&
1002 reqs_change_state
.empty()) {
1003 reqs_lock
.unlock_shared();
1006 reqs_lock
.unlock_shared();
1008 std::unique_lock wl
{reqs_lock
};
1010 if (!reqs_change_state
.empty()) {
1011 for (auto siter
: reqs_change_state
) {
1012 _set_req_state(siter
);
1014 reqs_change_state
.clear();
1017 if (!unregistered_reqs
.empty()) {
1018 for (auto& r
: unregistered_reqs
) {
1023 unregistered_reqs
.clear();
1026 map
<uint64_t, rgw_http_req_data
*>::iterator iter
= reqs
.find(max_threaded_req
);
1028 list
<std::pair
<rgw_http_req_data
*, int> > remove_reqs
;
1030 for (; iter
!= reqs
.end(); ++iter
) {
1031 rgw_http_req_data
*req_data
= iter
->second
;
1032 int r
= link_request(req_data
);
1034 ldout(cct
, 0) << "ERROR: failed to link http request" << dendl
;
1035 remove_reqs
.push_back(std::make_pair(iter
->second
, r
));
1037 max_threaded_req
= iter
->first
+ 1;
1041 for (auto piter
: remove_reqs
) {
1042 rgw_http_req_data
*req_data
= piter
.first
;
1043 int r
= piter
.second
;
1045 _finish_request(req_data
, r
);
1049 int RGWHTTPManager::add_request(RGWHTTPClient
*client
)
1051 rgw_http_req_data
*req_data
= new rgw_http_req_data
;
1053 int ret
= client
->init_request(req_data
);
1060 req_data
->mgr
= this;
1061 req_data
->client
= client
;
1062 req_data
->control_io_id
= client
->get_io_id(RGWHTTPClient::HTTPCLIENT_IO_CONTROL
);
1063 req_data
->user_info
= client
->get_io_user_info();
1065 register_request(req_data
);
1068 ret
= link_request(req_data
);
1075 ret
= signal_thread();
1077 finish_request(req_data
, ret
);
1083 int RGWHTTPManager::remove_request(RGWHTTPClient
*client
)
1085 rgw_http_req_data
*req_data
= client
->get_req_data();
1088 unlink_request(req_data
);
1091 if (!unregister_request(req_data
)) {
1094 int ret
= signal_thread();
1102 int RGWHTTPManager::set_request_state(RGWHTTPClient
*client
, RGWHTTPRequestSetState state
)
1104 rgw_http_req_data
*req_data
= client
->get_req_data();
1106 ceph_assert(ceph_mutex_is_locked(req_data
->lock
));
1108 /* can only do that if threaded */
1113 bool suggested_wr_paused
= req_data
->write_paused
;
1114 bool suggested_rd_paused
= req_data
->read_paused
;
1117 case SET_WRITE_PAUSED
:
1118 suggested_wr_paused
= true;
1120 case SET_WRITE_RESUME
:
1121 suggested_wr_paused
= false;
1123 case SET_READ_PAUSED
:
1124 suggested_rd_paused
= true;
1126 case SET_READ_RESUME
:
1127 suggested_rd_paused
= false;
1130 /* shouldn't really be here */
1133 if (suggested_wr_paused
== req_data
->write_paused
&&
1134 suggested_rd_paused
== req_data
->read_paused
) {
1138 req_data
->write_paused
= suggested_wr_paused
;
1139 req_data
->read_paused
= suggested_rd_paused
;
1141 int bitmask
= CURLPAUSE_CONT
;
1143 if (req_data
->write_paused
) {
1144 bitmask
|= CURLPAUSE_SEND
;
1147 if (req_data
->read_paused
) {
1148 bitmask
|= CURLPAUSE_RECV
;
1151 reqs_change_state
.push_back(set_state(req_data
, bitmask
));
1152 int ret
= signal_thread();
1160 int RGWHTTPManager::start()
1162 if (pipe_cloexec(thread_pipe
, 0) < 0) {
1164 ldout(cct
, 0) << "ERROR: pipe(): " << cpp_strerror(e
) << dendl
;
1168 // enable non-blocking reads
1169 if (::fcntl(thread_pipe
[0], F_SETFL
, O_NONBLOCK
) < 0) {
1171 ldout(cct
, 0) << "ERROR: fcntl(): " << cpp_strerror(e
) << dendl
;
1172 TEMP_FAILURE_RETRY(::close(thread_pipe
[0]));
1173 TEMP_FAILURE_RETRY(::close(thread_pipe
[1]));
1177 #ifdef HAVE_CURL_MULTI_WAIT
1178 // on first initialization, use this pipe to detect whether we're using a
1179 // buggy version of libcurl
1180 std::call_once(detect_flag
, detect_curl_multi_wait_bug
, cct
,
1181 static_cast<CURLM
*>(multi_handle
),
1182 thread_pipe
[1], thread_pipe
[0]);
1186 reqs_thread
= new ReqsThread(this);
1187 reqs_thread
->create("http_manager");
1191 void RGWHTTPManager::stop()
1202 reqs_thread
->join();
1204 TEMP_FAILURE_RETRY(::close(thread_pipe
[1]));
1205 TEMP_FAILURE_RETRY(::close(thread_pipe
[0]));
1209 int RGWHTTPManager::signal_thread()
1212 int ret
= write(thread_pipe
[1], (void *)&buf
, sizeof(buf
));
1215 ldout(cct
, 0) << "ERROR: " << __func__
<< ": write() returned ret=" << ret
<< dendl
;
1221 void *RGWHTTPManager::reqs_thread_entry()
1226 ldout(cct
, 20) << __func__
<< ": start" << dendl
;
1228 while (!going_down
) {
1229 int ret
= do_curl_wait(cct
, (CURLM
*)multi_handle
, thread_pipe
[0]);
1231 dout(0) << "ERROR: do_curl_wait() returned: " << ret
<< dendl
;
1235 manage_pending_requests();
1237 mstatus
= curl_multi_perform((CURLM
*)multi_handle
, &still_running
);
1240 case CURLM_CALL_MULTI_PERFORM
:
1243 dout(10) << "curl_multi_perform returned: " << mstatus
<< dendl
;
1248 while ((msg
= curl_multi_info_read((CURLM
*)multi_handle
, &msgs_left
))) {
1249 if (msg
->msg
== CURLMSG_DONE
) {
1250 int result
= msg
->data
.result
;
1251 CURL
*e
= msg
->easy_handle
;
1252 rgw_http_req_data
*req_data
;
1253 curl_easy_getinfo(e
, CURLINFO_PRIVATE
, (void **)&req_data
);
1254 curl_multi_remove_handle((CURLM
*)multi_handle
, e
);
1258 if (!req_data
->user_ret
) {
1259 curl_easy_getinfo(e
, CURLINFO_RESPONSE_CODE
, (void **)&http_status
);
1261 status
= rgw_http_error_to_errno(http_status
);
1262 if (result
!= CURLE_OK
&& status
== 0) {
1263 dout(0) << "ERROR: curl error: " << curl_easy_strerror((CURLcode
)result
) << ", maybe network unstable" << dendl
;
1267 status
= *req_data
->user_ret
;
1269 set_req_state_err(err
, status
, 0);
1270 http_status
= err
.http_ret
;
1272 int id
= req_data
->id
;
1273 finish_request(req_data
, status
, http_status
);
1277 case CURLE_OPERATION_TIMEDOUT
:
1278 dout(0) << "WARNING: curl operation timed out, network average transfer speed less than "
1279 << cct
->_conf
->rgw_curl_low_speed_limit
<< " Bytes per second during " << cct
->_conf
->rgw_curl_low_speed_time
<< " seconds." << dendl
;
1281 dout(20) << "ERROR: msg->data.result=" << result
<< " req_data->id=" << id
<< " http_status=" << http_status
<< dendl
;
1282 dout(20) << "ERROR: curl error: " << curl_easy_strerror((CURLcode
)result
) << " req_data->error_buf=" << req_data
->error_buf
<< dendl
;
1290 std::unique_lock rl
{reqs_lock
};
1291 for (auto r
: unregistered_reqs
) {
1295 unregistered_reqs
.clear();
1297 auto all_reqs
= std::move(reqs
);
1298 for (auto iter
: all_reqs
) {
1299 _unlink_request(iter
.second
);
1304 if (completion_mgr
) {
1305 completion_mgr
->go_down();
1311 void rgw_http_client_init(CephContext
*cct
)
1313 curl_global_init(CURL_GLOBAL_ALL
);
1314 rgw_http_manager
= new RGWHTTPManager(cct
);
1315 rgw_http_manager
->start();
1318 void rgw_http_client_cleanup()
1320 rgw_http_manager
->stop();
1321 delete rgw_http_manager
;
1322 curl_global_cleanup();
1326 int RGWHTTP::send(RGWHTTPClient
*req
) {
1330 int r
= rgw_http_manager
->add_request(req
);
1338 int RGWHTTP::process(RGWHTTPClient
*req
, optional_yield y
) {
1347 return req
->wait(y
);