1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/compat.h"
5 #include "common/errno.h"
7 #include <boost/utility/string_ref.hpp>
10 #include <curl/easy.h>
11 #include <curl/multi.h>
13 #include "rgw_common.h"
14 #include "rgw_http_client.h"
15 #include "rgw_http_errors.h"
16 #include "common/async/completion.h"
17 #include "common/RefCountedObj.h"
19 #include "rgw_coroutine.h"
23 #define dout_context g_ceph_context
24 #define dout_subsys ceph_subsys_rgw
26 RGWHTTPManager
*rgw_http_manager
;
30 static void do_curl_easy_cleanup(RGWCurlHandle
*curl_handle
);
32 struct rgw_http_req_data
: public RefCountedObject
{
33 RGWCurlHandle
*curl_handle
{nullptr};
34 curl_slist
*h
{nullptr};
37 std::atomic
<bool> done
= { false };
38 RGWHTTPClient
*client
{nullptr};
39 rgw_io_id control_io_id
;
40 void *user_info
{nullptr};
41 bool registered
{false};
42 RGWHTTPManager
*mgr
{nullptr};
43 char error_buf
[CURL_ERROR_SIZE
];
44 bool write_paused
{false};
45 bool read_paused
{false};
50 using Signature
= void(boost::system::error_code
);
51 using Completion
= ceph::async::Completion
<Signature
>;
52 std::unique_ptr
<Completion
> completion
;
54 rgw_http_req_data() : id(-1), lock("rgw_http_req_data::lock") {
55 // FIPS zeroization audit 20191115: this memset is not security related.
56 memset(error_buf
, 0, sizeof(error_buf
));
59 template <typename ExecutionContext
, typename CompletionToken
>
60 auto async_wait(ExecutionContext
& ctx
, CompletionToken
&& token
) {
61 boost::asio::async_completion
<CompletionToken
, Signature
> init(token
);
62 auto& handler
= init
.completion_handler
;
64 std::unique_lock l
{lock
};
65 completion
= Completion::create(ctx
.get_executor(), std::move(handler
));
67 return init
.result
.get();
69 int wait(optional_yield y
) {
73 #ifdef HAVE_BOOST_CONTEXT
75 auto& context
= y
.get_io_context();
76 auto& yield
= y
.get_yield_context();
77 boost::system::error_code ec
;
78 async_wait(context
, yield
[ec
]);
82 Mutex::Locker
l(lock
);
87 void set_state(int bitmask
);
90 Mutex::Locker
l(lock
);
93 do_curl_easy_cleanup(curl_handle
);
96 curl_slist_free_all(h
);
102 boost::system::error_code
ec(-ret
, boost::system::system_category());
103 Completion::post(std::move(completion
), ec
);
114 Mutex::Locker
l(lock
);
118 RGWHTTPManager
*get_manager() {
119 Mutex::Locker
l(lock
);
123 CURL
*get_easy_handle() const;
126 struct RGWCurlHandle
{
131 explicit RGWCurlHandle(CURL
* h
) : uses(0), h(h
) {};
137 void rgw_http_req_data::set_state(int bitmask
) {
138 /* no need to lock here, moreover curl_easy_pause() might trigger
139 * the data receive callback :/
141 CURLcode rc
= curl_easy_pause(**curl_handle
, bitmask
);
142 if (rc
!= CURLE_OK
) {
143 dout(0) << "ERROR: curl_easy_pause() returned rc=" << rc
<< dendl
;
148 class RGWCurlHandles
: public Thread
{
151 std::vector
<RGWCurlHandle
*>saved_curl
;
152 int cleaner_shutdown
;
156 cleaner_lock
{"RGWCurlHandles::cleaner_lock"},
157 cleaner_shutdown
{0} {
160 RGWCurlHandle
* get_curl_handle();
161 void release_curl_handle_now(RGWCurlHandle
* curl
);
162 void release_curl_handle(RGWCurlHandle
* curl
);
163 void flush_curl_handles();
168 RGWCurlHandle
* RGWCurlHandles::get_curl_handle() {
169 RGWCurlHandle
* curl
= 0;
172 Mutex::Locker
lock(cleaner_lock
);
173 if (!saved_curl
.empty()) {
174 curl
= *saved_curl
.begin();
175 saved_curl
.erase(saved_curl
.begin());
179 } else if ((h
= curl_easy_init())) {
180 curl
= new RGWCurlHandle
{h
};
187 void RGWCurlHandles::release_curl_handle_now(RGWCurlHandle
* curl
)
189 curl_easy_cleanup(**curl
);
193 void RGWCurlHandles::release_curl_handle(RGWCurlHandle
* curl
)
195 if (cleaner_shutdown
) {
196 release_curl_handle_now(curl
);
198 curl_easy_reset(**curl
);
199 Mutex::Locker
lock(cleaner_lock
);
200 curl
->lastuse
= mono_clock::now();
201 saved_curl
.insert(saved_curl
.begin(), 1, curl
);
205 void* RGWCurlHandles::entry()
208 Mutex::Locker
lock(cleaner_lock
);
211 if (cleaner_shutdown
) {
212 if (saved_curl
.empty())
215 utime_t release
= ceph_clock_now() + utime_t(MAXIDLE
,0);
216 cleaner_cond
.WaitUntil(cleaner_lock
, release
);
218 mono_time now
= mono_clock::now();
219 while (!saved_curl
.empty()) {
220 auto cend
= saved_curl
.end();
223 if (!cleaner_shutdown
&& now
- curl
->lastuse
< std::chrono::seconds(MAXIDLE
))
225 saved_curl
.erase(cend
);
226 release_curl_handle_now(curl
);
232 void RGWCurlHandles::stop()
234 Mutex::Locker
lock(cleaner_lock
);
235 cleaner_shutdown
= 1;
236 cleaner_cond
.Signal();
239 void RGWCurlHandles::flush_curl_handles()
243 if (!saved_curl
.empty()) {
244 dout(0) << "ERROR: " << __func__
<< " failed final cleanup" << dendl
;
246 saved_curl
.shrink_to_fit();
249 CURL
*rgw_http_req_data::get_easy_handle() const
251 return **curl_handle
;
254 static RGWCurlHandles
*handles
;
256 static RGWCurlHandle
*do_curl_easy_init()
258 return handles
->get_curl_handle();
261 static void do_curl_easy_cleanup(RGWCurlHandle
*curl_handle
)
263 handles
->release_curl_handle(curl_handle
);
266 // XXX make this part of the token cache? (but that's swift-only;
267 // and this especially needs to integrates with s3...)
269 void rgw_setup_saved_curl_handles()
271 handles
= new RGWCurlHandles();
272 handles
->create("rgw_curl");
275 void rgw_release_all_curl_handles()
277 handles
->flush_curl_handles();
281 void RGWIOProvider::assign_io(RGWIOIDProvider
& io_id_provider
, int io_type
)
284 id
= io_id_provider
.get_next();
289 * the following set of callbacks will be called either on RGWHTTPManager::process(),
290 * or via the RGWHTTPManager async processing.
292 size_t RGWHTTPClient::receive_http_header(void * const ptr
,
297 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
298 size_t len
= size
* nmemb
;
300 Mutex::Locker
l(req_data
->lock
);
302 if (!req_data
->registered
) {
306 int ret
= req_data
->client
->receive_header(ptr
, size
* nmemb
);
308 dout(0) << "WARNING: client->receive_header() returned ret=" << ret
<< dendl
;
314 size_t RGWHTTPClient::receive_http_data(void * const ptr
,
319 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
320 size_t len
= size
* nmemb
;
324 RGWHTTPClient
*client
;
327 Mutex::Locker
l(req_data
->lock
);
328 if (!req_data
->registered
) {
332 client
= req_data
->client
;
335 size_t& skip_bytes
= client
->receive_pause_skip
;
337 if (skip_bytes
>= len
) {
342 int ret
= client
->receive_data((char *)ptr
+ skip_bytes
, len
- skip_bytes
, &pause
);
344 dout(0) << "WARNING: client->receive_data() returned ret=" << ret
<< dendl
;
348 dout(20) << "RGWHTTPClient::receive_http_data(): pause" << dendl
;
350 Mutex::Locker
l(req_data
->lock
);
351 req_data
->read_paused
= true;
352 return CURL_WRITEFUNC_PAUSE
;
360 size_t RGWHTTPClient::send_http_data(void * const ptr
,
365 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
367 RGWHTTPClient
*client
;
370 Mutex::Locker
l(req_data
->lock
);
372 if (!req_data
->registered
) {
376 client
= req_data
->client
;
381 int ret
= client
->send_data(ptr
, size
* nmemb
, &pause
);
383 dout(0) << "WARNING: client->receive_data() returned ret=" << ret
<< dendl
;
388 Mutex::Locker
l(req_data
->lock
);
389 req_data
->write_paused
= true;
390 return CURL_READFUNC_PAUSE
;
396 Mutex
& RGWHTTPClient::get_req_lock()
398 return req_data
->lock
;
401 void RGWHTTPClient::_set_write_paused(bool pause
)
403 ceph_assert(req_data
->lock
.is_locked());
405 RGWHTTPManager
*mgr
= req_data
->mgr
;
406 if (pause
== req_data
->write_paused
) {
410 mgr
->set_request_state(this, SET_WRITE_PAUSED
);
412 mgr
->set_request_state(this, SET_WRITE_RESUME
);
416 void RGWHTTPClient::_set_read_paused(bool pause
)
418 ceph_assert(req_data
->lock
.is_locked());
420 RGWHTTPManager
*mgr
= req_data
->mgr
;
421 if (pause
== req_data
->read_paused
) {
425 mgr
->set_request_state(this, SET_READ_PAUSED
);
427 mgr
->set_request_state(this, SET_READ_RESUME
);
431 static curl_slist
*headers_to_slist(param_vec_t
& headers
)
433 curl_slist
*h
= NULL
;
435 param_vec_t::iterator iter
;
436 for (iter
= headers
.begin(); iter
!= headers
.end(); ++iter
) {
437 pair
<string
, string
>& p
= *iter
;
438 string val
= p
.first
;
440 if (strncmp(val
.c_str(), "HTTP_", 5) == 0) {
444 /* we need to convert all underscores into dashes as some web servers forbid them
445 * in the http header field names
447 for (size_t i
= 0; i
< val
.size(); i
++) {
453 val
= camelcase_dash_http_attr(val
);
455 // curl won't send headers with empty values unless it ends with a ; instead
456 if (p
.second
.empty()) {
460 val
.append(p
.second
);
462 h
= curl_slist_append(h
, val
.c_str());
468 static bool is_upload_request(const string
& method
)
470 return method
== "POST" || method
== "PUT";
474 * process a single simple one off request
476 int RGWHTTPClient::process(optional_yield y
)
478 return RGWHTTP::process(this, y
);
481 string
RGWHTTPClient::to_str()
483 string method_str
= (method
.empty() ? "<no-method>" : method
);
484 string url_str
= (url
.empty() ? "<no-url>" : url
);
485 return method_str
+ " " + url_str
;
488 int RGWHTTPClient::get_req_retcode()
494 return req_data
->get_retcode();
498 * init request, will be used later with RGWHTTPManager
500 int RGWHTTPClient::init_request(rgw_http_req_data
*_req_data
)
502 ceph_assert(!req_data
);
504 req_data
= _req_data
;
506 req_data
->curl_handle
= do_curl_easy_init();
508 CURL
*easy_handle
= req_data
->get_easy_handle();
510 dout(20) << "sending request to " << url
<< dendl
;
512 curl_slist
*h
= headers_to_slist(headers
);
516 curl_easy_setopt(easy_handle
, CURLOPT_CUSTOMREQUEST
, method
.c_str());
517 curl_easy_setopt(easy_handle
, CURLOPT_URL
, url
.c_str());
518 curl_easy_setopt(easy_handle
, CURLOPT_NOPROGRESS
, 1L);
519 curl_easy_setopt(easy_handle
, CURLOPT_NOSIGNAL
, 1L);
520 curl_easy_setopt(easy_handle
, CURLOPT_HEADERFUNCTION
, receive_http_header
);
521 curl_easy_setopt(easy_handle
, CURLOPT_WRITEHEADER
, (void *)req_data
);
522 curl_easy_setopt(easy_handle
, CURLOPT_WRITEFUNCTION
, receive_http_data
);
523 curl_easy_setopt(easy_handle
, CURLOPT_WRITEDATA
, (void *)req_data
);
524 curl_easy_setopt(easy_handle
, CURLOPT_ERRORBUFFER
, (void *)req_data
->error_buf
);
525 curl_easy_setopt(easy_handle
, CURLOPT_LOW_SPEED_TIME
, cct
->_conf
->rgw_curl_low_speed_time
);
526 curl_easy_setopt(easy_handle
, CURLOPT_LOW_SPEED_LIMIT
, cct
->_conf
->rgw_curl_low_speed_limit
);
528 curl_easy_setopt(easy_handle
, CURLOPT_HTTPHEADER
, (void *)h
);
530 curl_easy_setopt(easy_handle
, CURLOPT_READFUNCTION
, send_http_data
);
531 curl_easy_setopt(easy_handle
, CURLOPT_READDATA
, (void *)req_data
);
532 if (send_data_hint
|| is_upload_request(method
)) {
533 curl_easy_setopt(easy_handle
, CURLOPT_UPLOAD
, 1L);
536 curl_easy_setopt(easy_handle
, CURLOPT_INFILESIZE
, (void *)send_len
);
539 curl_easy_setopt(easy_handle
, CURLOPT_SSL_VERIFYPEER
, 0L);
540 curl_easy_setopt(easy_handle
, CURLOPT_SSL_VERIFYHOST
, 0L);
541 dout(20) << "ssl verification is set to off" << dendl
;
543 curl_easy_setopt(easy_handle
, CURLOPT_PRIVATE
, (void *)req_data
);
548 bool RGWHTTPClient::is_done()
550 return req_data
->is_done();
554 * wait for async request to complete
556 int RGWHTTPClient::wait(optional_yield y
)
558 return req_data
->wait(y
);
561 void RGWHTTPClient::cancel()
564 RGWHTTPManager
*http_manager
= req_data
->mgr
;
566 http_manager
->remove_request(this);
571 RGWHTTPClient::~RGWHTTPClient()
580 int RGWHTTPHeadersCollector::receive_header(void * const ptr
, const size_t len
)
582 const boost::string_ref
header_line(static_cast<const char *>(ptr
), len
);
584 /* We're tokening the line that way due to backward compatibility. */
585 const size_t sep_loc
= header_line
.find_first_of(" \t:");
587 if (boost::string_ref::npos
== sep_loc
) {
588 /* Wrongly formatted header? Just skip it. */
592 header_name_t
name(header_line
.substr(0, sep_loc
));
593 if (0 == relevant_headers
.count(name
)) {
594 /* Not interested in this particular header. */
598 const auto value_part
= header_line
.substr(sep_loc
+ 1);
600 /* Skip spaces and tabs after the separator. */
601 const size_t val_loc_s
= value_part
.find_first_not_of(' ');
602 const size_t val_loc_e
= value_part
.find_first_of("\r\n");
604 if (boost::string_ref::npos
== val_loc_s
||
605 boost::string_ref::npos
== val_loc_e
) {
606 /* Empty value case. */
607 found_headers
.emplace(name
, header_value_t());
609 found_headers
.emplace(name
, header_value_t(
610 value_part
.substr(val_loc_s
, val_loc_e
- val_loc_s
)));
616 int RGWHTTPTransceiver::send_data(void* ptr
, size_t len
, bool* pause
)
618 int length_to_copy
= 0;
619 if (post_data_index
< post_data
.length()) {
620 length_to_copy
= min(post_data
.length() - post_data_index
, len
);
621 memcpy(ptr
, post_data
.data() + post_data_index
, length_to_copy
);
622 post_data_index
+= length_to_copy
;
624 return length_to_copy
;
628 static int clear_signal(int fd
)
630 // since we're in non-blocking mode, we can try to read a lot more than
631 // one signal from signal_thread() to avoid later wakeups. non-blocking reads
632 // are also required to support the curl_multi_wait bug workaround
633 std::array
<char, 256> buf
;
634 int ret
= ::read(fd
, (void *)buf
.data(), buf
.size());
637 return ret
== -EAGAIN
? 0 : ret
; // clear EAGAIN
642 #if HAVE_CURL_MULTI_WAIT
644 static std::once_flag detect_flag
;
645 static bool curl_multi_wait_bug_present
= false;
647 static int detect_curl_multi_wait_bug(CephContext
*cct
, CURLM
*handle
,
648 int write_fd
, int read_fd
)
652 // write to write_fd so that read_fd becomes readable
654 ret
= ::write(write_fd
, &buf
, sizeof(buf
));
657 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): write() returned " << ret
<< dendl
;
661 // pass read_fd in extra_fds for curl_multi_wait()
663 struct curl_waitfd wait_fd
;
665 wait_fd
.fd
= read_fd
;
666 wait_fd
.events
= CURL_WAIT_POLLIN
;
669 ret
= curl_multi_wait(handle
, &wait_fd
, 1, 0, &num_fds
);
670 if (ret
!= CURLM_OK
) {
671 ldout(cct
, 0) << "ERROR: curl_multi_wait() returned " << ret
<< dendl
;
675 // curl_multi_wait should flag revents when extra_fd is readable. if it
676 // doesn't, the bug is present and we can't rely on revents
677 if (wait_fd
.revents
== 0) {
678 curl_multi_wait_bug_present
= true;
679 ldout(cct
, 0) << "WARNING: detected a version of libcurl which contains a "
680 "bug in curl_multi_wait(). enabling a workaround that may degrade "
681 "performance slightly." << dendl
;
684 return clear_signal(read_fd
);
687 static bool is_signaled(const curl_waitfd
& wait_fd
)
689 if (wait_fd
.fd
< 0) {
694 if (curl_multi_wait_bug_present
) {
695 // we can't rely on revents, so we always return true if a wait_fd is given.
696 // this means we'll be trying a non-blocking read on this fd every time that
697 // curl_multi_wait() wakes up
701 return wait_fd
.revents
> 0;
704 static int do_curl_wait(CephContext
*cct
, CURLM
*handle
, int signal_fd
)
707 struct curl_waitfd wait_fd
;
709 wait_fd
.fd
= signal_fd
;
710 wait_fd
.events
= CURL_WAIT_POLLIN
;
713 int ret
= curl_multi_wait(handle
, &wait_fd
, 1, cct
->_conf
->rgw_curl_wait_timeout_ms
, &num_fds
);
715 ldout(cct
, 0) << "ERROR: curl_multi_wait() returned " << ret
<< dendl
;
719 if (is_signaled(wait_fd
)) {
720 ret
= clear_signal(signal_fd
);
722 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): read() returned " << ret
<< dendl
;
731 static int do_curl_wait(CephContext
*cct
, CURLM
*handle
, int signal_fd
)
742 /* get file descriptors from the transfers */
743 int ret
= curl_multi_fdset(handle
, &fdread
, &fdwrite
, &fdexcep
, &maxfd
);
745 ldout(cct
, 0) << "ERROR: curl_multi_fdset returned " << ret
<< dendl
;
750 FD_SET(signal_fd
, &fdread
);
751 if (signal_fd
>= maxfd
) {
752 maxfd
= signal_fd
+ 1;
756 /* forcing a strict timeout, as the returned fdsets might not reference all fds we wait on */
757 uint64_t to
= cct
->_conf
->rgw_curl_wait_timeout_ms
;
758 #define RGW_CURL_TIMEOUT 1000
760 to
= RGW_CURL_TIMEOUT
;
761 struct timeval timeout
;
762 timeout
.tv_sec
= to
/ 1000;
763 timeout
.tv_usec
= to
% 1000;
765 ret
= select(maxfd
+1, &fdread
, &fdwrite
, &fdexcep
, &timeout
);
768 ldout(cct
, 0) << "ERROR: select returned " << ret
<< dendl
;
772 if (signal_fd
> 0 && FD_ISSET(signal_fd
, &fdread
)) {
773 ret
= clear_signal(signal_fd
);
775 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): read() returned " << ret
<< dendl
;
785 void *RGWHTTPManager::ReqsThread::entry()
787 manager
->reqs_thread_entry();
792 * RGWHTTPManager has two modes of operation: threaded and non-threaded.
794 RGWHTTPManager::RGWHTTPManager(CephContext
*_cct
, RGWCompletionManager
*_cm
) : cct(_cct
),
795 completion_mgr(_cm
), is_started(false),
796 reqs_lock("RGWHTTPManager::reqs_lock"), num_reqs(0), max_threaded_req(0),
799 multi_handle
= (void *)curl_multi_init();
804 RGWHTTPManager::~RGWHTTPManager() {
807 curl_multi_cleanup((CURLM
*)multi_handle
);
810 void RGWHTTPManager::register_request(rgw_http_req_data
*req_data
)
812 RWLock::WLocker
rl(reqs_lock
);
813 req_data
->id
= num_reqs
;
814 req_data
->registered
= true;
815 reqs
[num_reqs
] = req_data
;
817 ldout(cct
, 20) << __func__
<< " mgr=" << this << " req_data->id=" << req_data
->id
<< ", curl_handle=" << req_data
->curl_handle
<< dendl
;
820 bool RGWHTTPManager::unregister_request(rgw_http_req_data
*req_data
)
822 RWLock::WLocker
rl(reqs_lock
);
823 if (!req_data
->registered
) {
827 req_data
->registered
= false;
828 unregistered_reqs
.push_back(req_data
);
829 ldout(cct
, 20) << __func__
<< " mgr=" << this << " req_data->id=" << req_data
->id
<< ", curl_handle=" << req_data
->curl_handle
<< dendl
;
833 void RGWHTTPManager::complete_request(rgw_http_req_data
*req_data
)
835 RWLock::WLocker
rl(reqs_lock
);
836 _complete_request(req_data
);
839 void RGWHTTPManager::_complete_request(rgw_http_req_data
*req_data
)
841 map
<uint64_t, rgw_http_req_data
*>::iterator iter
= reqs
.find(req_data
->id
);
842 if (iter
!= reqs
.end()) {
846 Mutex::Locker
l(req_data
->lock
);
847 req_data
->mgr
= nullptr;
849 if (completion_mgr
) {
850 completion_mgr
->complete(NULL
, req_data
->control_io_id
, req_data
->user_info
);
856 void RGWHTTPManager::finish_request(rgw_http_req_data
*req_data
, int ret
)
858 req_data
->finish(ret
);
859 complete_request(req_data
);
862 void RGWHTTPManager::_finish_request(rgw_http_req_data
*req_data
, int ret
)
864 req_data
->finish(ret
);
865 _complete_request(req_data
);
868 void RGWHTTPManager::_set_req_state(set_state
& ss
)
870 ss
.req
->set_state(ss
.bitmask
);
873 * hook request to the curl multi handle
875 int RGWHTTPManager::link_request(rgw_http_req_data
*req_data
)
877 ldout(cct
, 20) << __func__
<< " req_data=" << req_data
<< " req_data->id=" << req_data
->id
<< ", curl_handle=" << req_data
->curl_handle
<< dendl
;
878 CURLMcode mstatus
= curl_multi_add_handle((CURLM
*)multi_handle
, req_data
->get_easy_handle());
880 dout(0) << "ERROR: failed on curl_multi_add_handle, status=" << mstatus
<< dendl
;
887 * unhook request from the curl multi handle, and finish request if it wasn't finished yet as
888 * there will be no more processing on this request
890 void RGWHTTPManager::_unlink_request(rgw_http_req_data
*req_data
)
892 if (req_data
->curl_handle
) {
893 curl_multi_remove_handle((CURLM
*)multi_handle
, req_data
->get_easy_handle());
895 if (!req_data
->is_done()) {
896 _finish_request(req_data
, -ECANCELED
);
900 void RGWHTTPManager::unlink_request(rgw_http_req_data
*req_data
)
902 RWLock::WLocker
wl(reqs_lock
);
903 _unlink_request(req_data
);
906 void RGWHTTPManager::manage_pending_requests()
908 reqs_lock
.get_read();
909 if (max_threaded_req
== num_reqs
&&
910 unregistered_reqs
.empty() &&
911 reqs_change_state
.empty()) {
917 RWLock::WLocker
wl(reqs_lock
);
919 if (!unregistered_reqs
.empty()) {
920 for (auto& r
: unregistered_reqs
) {
925 unregistered_reqs
.clear();
928 map
<uint64_t, rgw_http_req_data
*>::iterator iter
= reqs
.find(max_threaded_req
);
930 list
<std::pair
<rgw_http_req_data
*, int> > remove_reqs
;
932 for (; iter
!= reqs
.end(); ++iter
) {
933 rgw_http_req_data
*req_data
= iter
->second
;
934 int r
= link_request(req_data
);
936 ldout(cct
, 0) << "ERROR: failed to link http request" << dendl
;
937 remove_reqs
.push_back(std::make_pair(iter
->second
, r
));
939 max_threaded_req
= iter
->first
+ 1;
943 if (!reqs_change_state
.empty()) {
944 for (auto siter
: reqs_change_state
) {
945 _set_req_state(siter
);
947 reqs_change_state
.clear();
950 for (auto piter
: remove_reqs
) {
951 rgw_http_req_data
*req_data
= piter
.first
;
952 int r
= piter
.second
;
954 _finish_request(req_data
, r
);
958 int RGWHTTPManager::add_request(RGWHTTPClient
*client
)
960 rgw_http_req_data
*req_data
= new rgw_http_req_data
;
962 int ret
= client
->init_request(req_data
);
969 req_data
->mgr
= this;
970 req_data
->client
= client
;
971 req_data
->control_io_id
= client
->get_io_id(RGWHTTPClient::HTTPCLIENT_IO_CONTROL
);
972 req_data
->user_info
= client
->get_io_user_info();
974 register_request(req_data
);
977 ret
= link_request(req_data
);
984 ret
= signal_thread();
986 finish_request(req_data
, ret
);
992 int RGWHTTPManager::remove_request(RGWHTTPClient
*client
)
994 rgw_http_req_data
*req_data
= client
->get_req_data();
997 unlink_request(req_data
);
1000 if (!unregister_request(req_data
)) {
1003 int ret
= signal_thread();
1011 int RGWHTTPManager::set_request_state(RGWHTTPClient
*client
, RGWHTTPRequestSetState state
)
1013 rgw_http_req_data
*req_data
= client
->get_req_data();
1015 ceph_assert(req_data
->lock
.is_locked());
1017 /* can only do that if threaded */
1022 bool suggested_wr_paused
= req_data
->write_paused
;
1023 bool suggested_rd_paused
= req_data
->read_paused
;
1026 case SET_WRITE_PAUSED
:
1027 suggested_wr_paused
= true;
1029 case SET_WRITE_RESUME
:
1030 suggested_wr_paused
= false;
1032 case SET_READ_PAUSED
:
1033 suggested_rd_paused
= true;
1035 case SET_READ_RESUME
:
1036 suggested_rd_paused
= false;
1039 /* shouldn't really be here */
1042 if (suggested_wr_paused
== req_data
->write_paused
&&
1043 suggested_rd_paused
== req_data
->read_paused
) {
1047 req_data
->write_paused
= suggested_wr_paused
;
1048 req_data
->read_paused
= suggested_rd_paused
;
1050 int bitmask
= CURLPAUSE_CONT
;
1052 if (req_data
->write_paused
) {
1053 bitmask
|= CURLPAUSE_SEND
;
1056 if (req_data
->read_paused
) {
1057 bitmask
|= CURLPAUSE_RECV
;
1060 reqs_change_state
.push_back(set_state(req_data
, bitmask
));
1061 int ret
= signal_thread();
1069 int RGWHTTPManager::start()
1071 if (pipe_cloexec(thread_pipe
) < 0) {
1073 ldout(cct
, 0) << "ERROR: pipe(): " << cpp_strerror(e
) << dendl
;
1077 // enable non-blocking reads
1078 if (::fcntl(thread_pipe
[0], F_SETFL
, O_NONBLOCK
) < 0) {
1080 ldout(cct
, 0) << "ERROR: fcntl(): " << cpp_strerror(e
) << dendl
;
1081 TEMP_FAILURE_RETRY(::close(thread_pipe
[0]));
1082 TEMP_FAILURE_RETRY(::close(thread_pipe
[1]));
1086 #ifdef HAVE_CURL_MULTI_WAIT
1087 // on first initialization, use this pipe to detect whether we're using a
1088 // buggy version of libcurl
1089 std::call_once(detect_flag
, detect_curl_multi_wait_bug
, cct
,
1090 static_cast<CURLM
*>(multi_handle
),
1091 thread_pipe
[1], thread_pipe
[0]);
1095 reqs_thread
= new ReqsThread(this);
1096 reqs_thread
->create("http_manager");
1100 void RGWHTTPManager::stop()
1111 reqs_thread
->join();
1113 TEMP_FAILURE_RETRY(::close(thread_pipe
[1]));
1114 TEMP_FAILURE_RETRY(::close(thread_pipe
[0]));
1118 int RGWHTTPManager::signal_thread()
1121 int ret
= write(thread_pipe
[1], (void *)&buf
, sizeof(buf
));
1124 ldout(cct
, 0) << "ERROR: " << __func__
<< ": write() returned ret=" << ret
<< dendl
;
1130 void *RGWHTTPManager::reqs_thread_entry()
1135 ldout(cct
, 20) << __func__
<< ": start" << dendl
;
1137 while (!going_down
) {
1138 int ret
= do_curl_wait(cct
, (CURLM
*)multi_handle
, thread_pipe
[0]);
1140 dout(0) << "ERROR: do_curl_wait() returned: " << ret
<< dendl
;
1144 manage_pending_requests();
1146 mstatus
= curl_multi_perform((CURLM
*)multi_handle
, &still_running
);
1149 case CURLM_CALL_MULTI_PERFORM
:
1152 dout(10) << "curl_multi_perform returned: " << mstatus
<< dendl
;
1157 while ((msg
= curl_multi_info_read((CURLM
*)multi_handle
, &msgs_left
))) {
1158 if (msg
->msg
== CURLMSG_DONE
) {
1159 int result
= msg
->data
.result
;
1160 CURL
*e
= msg
->easy_handle
;
1161 rgw_http_req_data
*req_data
;
1162 curl_easy_getinfo(e
, CURLINFO_PRIVATE
, (void **)&req_data
);
1163 curl_multi_remove_handle((CURLM
*)multi_handle
, e
);
1166 curl_easy_getinfo(e
, CURLINFO_RESPONSE_CODE
, (void **)&http_status
);
1168 int status
= rgw_http_error_to_errno(http_status
);
1169 if (result
!= CURLE_OK
&& status
== 0) {
1170 dout(0) << "ERROR: curl error: " << curl_easy_strerror((CURLcode
)result
) << ", maybe network unstable" << dendl
;
1173 int id
= req_data
->id
;
1174 finish_request(req_data
, status
);
1178 case CURLE_OPERATION_TIMEDOUT
:
1179 dout(0) << "WARNING: curl operation timed out, network average transfer speed less than "
1180 << cct
->_conf
->rgw_curl_low_speed_limit
<< " Bytes per second during " << cct
->_conf
->rgw_curl_low_speed_time
<< " seconds." << dendl
;
1182 dout(20) << "ERROR: msg->data.result=" << result
<< " req_data->id=" << id
<< " http_status=" << http_status
<< dendl
;
1183 dout(20) << "ERROR: curl error: " << curl_easy_strerror((CURLcode
)result
) << dendl
;
1191 RWLock::WLocker
rl(reqs_lock
);
1192 for (auto r
: unregistered_reqs
) {
1196 unregistered_reqs
.clear();
1198 auto all_reqs
= std::move(reqs
);
1199 for (auto iter
: all_reqs
) {
1200 _unlink_request(iter
.second
);
1205 if (completion_mgr
) {
1206 completion_mgr
->go_down();
1212 void rgw_http_client_init(CephContext
*cct
)
1214 curl_global_init(CURL_GLOBAL_ALL
);
1215 rgw_http_manager
= new RGWHTTPManager(cct
);
1216 rgw_http_manager
->start();
1219 void rgw_http_client_cleanup()
1221 rgw_http_manager
->stop();
1222 delete rgw_http_manager
;
1223 curl_global_cleanup();
1227 int RGWHTTP::send(RGWHTTPClient
*req
) {
1231 int r
= rgw_http_manager
->add_request(req
);
1239 int RGWHTTP::process(RGWHTTPClient
*req
, optional_yield y
) {
1248 return req
->wait(y
);