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"
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"
20 #include "rgw_tools.h"
24 #define dout_context g_ceph_context
25 #define dout_subsys ceph_subsys_rgw
27 RGWHTTPManager
*rgw_http_manager
;
31 static void do_curl_easy_cleanup(RGWCurlHandle
*curl_handle
);
33 struct rgw_http_req_data
: public RefCountedObject
{
34 RGWCurlHandle
*curl_handle
{nullptr};
35 curl_slist
*h
{nullptr};
38 std::atomic
<bool> done
= { false };
39 RGWHTTPClient
*client
{nullptr};
40 rgw_io_id control_io_id
;
41 void *user_info
{nullptr};
42 bool registered
{false};
43 RGWHTTPManager
*mgr
{nullptr};
44 char error_buf
[CURL_ERROR_SIZE
];
45 bool write_paused
{false};
46 bool read_paused
{false};
48 optional
<int> user_ret
;
50 ceph::mutex lock
= ceph::make_mutex("rgw_http_req_data::lock");
51 ceph::condition_variable cond
;
53 using Signature
= void(boost::system::error_code
);
54 using Completion
= ceph::async::Completion
<Signature
>;
55 std::unique_ptr
<Completion
> completion
;
57 rgw_http_req_data() : id(-1) {
58 // FIPS zeroization audit 20191115: this memset is not security related.
59 memset(error_buf
, 0, sizeof(error_buf
));
62 template <typename ExecutionContext
, typename CompletionToken
>
63 auto async_wait(ExecutionContext
& ctx
, CompletionToken
&& token
) {
64 boost::asio::async_completion
<CompletionToken
, Signature
> init(token
);
65 auto& handler
= init
.completion_handler
;
67 std::unique_lock l
{lock
};
68 completion
= Completion::create(ctx
.get_executor(), std::move(handler
));
70 return init
.result
.get();
73 int wait(optional_yield y
) {
77 #ifdef HAVE_BOOST_CONTEXT
79 auto& context
= y
.get_io_context();
80 auto& yield
= y
.get_yield_context();
81 boost::system::error_code ec
;
82 async_wait(context
, yield
[ec
]);
85 // work on asio threads should be asynchronous, so warn when they block
87 dout(20) << "WARNING: blocking http request" << dendl
;
90 std::unique_lock l
{lock
};
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();
300 * the following set of callbacks will be called either on RGWHTTPManager::process(),
301 * or via the RGWHTTPManager async processing.
303 size_t RGWHTTPClient::receive_http_header(void * const ptr
,
308 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
309 size_t len
= size
* nmemb
;
311 std::lock_guard l
{req_data
->lock
};
313 if (!req_data
->registered
) {
317 int ret
= req_data
->client
->receive_header(ptr
, size
* nmemb
);
319 dout(5) << "WARNING: client->receive_header() returned ret=" << ret
<< dendl
;
320 req_data
->user_ret
= ret
;
321 return CURLE_WRITE_ERROR
;
327 size_t RGWHTTPClient::receive_http_data(void * const ptr
,
332 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
333 size_t len
= size
* nmemb
;
337 RGWHTTPClient
*client
;
340 std::lock_guard l
{req_data
->lock
};
341 if (!req_data
->registered
) {
345 client
= req_data
->client
;
348 size_t& skip_bytes
= client
->receive_pause_skip
;
350 if (skip_bytes
>= len
) {
355 int ret
= client
->receive_data((char *)ptr
+ skip_bytes
, len
- skip_bytes
, &pause
);
357 dout(5) << "WARNING: client->receive_data() returned ret=" << ret
<< dendl
;
358 req_data
->user_ret
= ret
;
359 return CURLE_WRITE_ERROR
;
363 dout(20) << "RGWHTTPClient::receive_http_data(): pause" << dendl
;
365 std::lock_guard l
{req_data
->lock
};
366 req_data
->read_paused
= true;
367 return CURL_WRITEFUNC_PAUSE
;
375 size_t RGWHTTPClient::send_http_data(void * const ptr
,
380 rgw_http_req_data
*req_data
= static_cast<rgw_http_req_data
*>(_info
);
382 RGWHTTPClient
*client
;
385 std::lock_guard l
{req_data
->lock
};
387 if (!req_data
->registered
) {
391 client
= req_data
->client
;
396 int ret
= client
->send_data(ptr
, size
* nmemb
, &pause
);
398 dout(5) << "WARNING: client->send_data() returned ret=" << ret
<< dendl
;
399 req_data
->user_ret
= ret
;
400 return CURLE_READ_ERROR
;
405 std::lock_guard l
{req_data
->lock
};
406 req_data
->write_paused
= true;
407 return CURL_READFUNC_PAUSE
;
413 ceph::mutex
& RGWHTTPClient::get_req_lock()
415 return req_data
->lock
;
418 void RGWHTTPClient::_set_write_paused(bool pause
)
420 ceph_assert(ceph_mutex_is_locked(req_data
->lock
));
422 RGWHTTPManager
*mgr
= req_data
->mgr
;
423 if (pause
== req_data
->write_paused
) {
427 mgr
->set_request_state(this, SET_WRITE_PAUSED
);
429 mgr
->set_request_state(this, SET_WRITE_RESUME
);
433 void RGWHTTPClient::_set_read_paused(bool pause
)
435 ceph_assert(ceph_mutex_is_locked(req_data
->lock
));
437 RGWHTTPManager
*mgr
= req_data
->mgr
;
438 if (pause
== req_data
->read_paused
) {
442 mgr
->set_request_state(this, SET_READ_PAUSED
);
444 mgr
->set_request_state(this, SET_READ_RESUME
);
448 static curl_slist
*headers_to_slist(param_vec_t
& headers
)
450 curl_slist
*h
= NULL
;
452 param_vec_t::iterator iter
;
453 for (iter
= headers
.begin(); iter
!= headers
.end(); ++iter
) {
454 pair
<string
, string
>& p
= *iter
;
455 string val
= p
.first
;
457 if (strncmp(val
.c_str(), "HTTP_", 5) == 0) {
461 /* we need to convert all underscores into dashes as some web servers forbid them
462 * in the http header field names
464 for (size_t i
= 0; i
< val
.size(); i
++) {
470 val
= camelcase_dash_http_attr(val
);
472 // curl won't send headers with empty values unless it ends with a ; instead
473 if (p
.second
.empty()) {
477 val
.append(p
.second
);
479 h
= curl_slist_append(h
, val
.c_str());
485 static bool is_upload_request(const string
& method
)
487 return method
== "POST" || method
== "PUT";
491 * process a single simple one off request
493 int RGWHTTPClient::process(optional_yield y
)
495 return RGWHTTP::process(this, y
);
498 string
RGWHTTPClient::to_str()
500 string method_str
= (method
.empty() ? "<no-method>" : method
);
501 string url_str
= (url
.empty() ? "<no-url>" : url
);
502 return method_str
+ " " + url_str
;
505 int RGWHTTPClient::get_req_retcode()
511 return req_data
->get_retcode();
515 * init request, will be used later with RGWHTTPManager
517 int RGWHTTPClient::init_request(rgw_http_req_data
*_req_data
)
519 ceph_assert(!req_data
);
521 req_data
= _req_data
;
523 req_data
->curl_handle
= do_curl_easy_init();
525 CURL
*easy_handle
= req_data
->get_easy_handle();
527 dout(20) << "sending request to " << url
<< dendl
;
529 curl_slist
*h
= headers_to_slist(headers
);
533 curl_easy_setopt(easy_handle
, CURLOPT_CUSTOMREQUEST
, method
.c_str());
534 curl_easy_setopt(easy_handle
, CURLOPT_URL
, url
.c_str());
535 curl_easy_setopt(easy_handle
, CURLOPT_NOPROGRESS
, 1L);
536 curl_easy_setopt(easy_handle
, CURLOPT_NOSIGNAL
, 1L);
537 curl_easy_setopt(easy_handle
, CURLOPT_HEADERFUNCTION
, receive_http_header
);
538 curl_easy_setopt(easy_handle
, CURLOPT_WRITEHEADER
, (void *)req_data
);
539 curl_easy_setopt(easy_handle
, CURLOPT_WRITEFUNCTION
, receive_http_data
);
540 curl_easy_setopt(easy_handle
, CURLOPT_WRITEDATA
, (void *)req_data
);
541 curl_easy_setopt(easy_handle
, CURLOPT_ERRORBUFFER
, (void *)req_data
->error_buf
);
542 curl_easy_setopt(easy_handle
, CURLOPT_LOW_SPEED_TIME
, cct
->_conf
->rgw_curl_low_speed_time
);
543 curl_easy_setopt(easy_handle
, CURLOPT_LOW_SPEED_LIMIT
, cct
->_conf
->rgw_curl_low_speed_limit
);
545 curl_easy_setopt(easy_handle
, CURLOPT_HTTPHEADER
, (void *)h
);
547 curl_easy_setopt(easy_handle
, CURLOPT_READFUNCTION
, send_http_data
);
548 curl_easy_setopt(easy_handle
, CURLOPT_READDATA
, (void *)req_data
);
549 if (send_data_hint
|| is_upload_request(method
)) {
550 curl_easy_setopt(easy_handle
, CURLOPT_UPLOAD
, 1L);
553 curl_easy_setopt(easy_handle
, CURLOPT_INFILESIZE
, (void *)send_len
);
556 curl_easy_setopt(easy_handle
, CURLOPT_SSL_VERIFYPEER
, 0L);
557 curl_easy_setopt(easy_handle
, CURLOPT_SSL_VERIFYHOST
, 0L);
558 dout(20) << "ssl verification is set to off" << dendl
;
560 curl_easy_setopt(easy_handle
, CURLOPT_PRIVATE
, (void *)req_data
);
565 bool RGWHTTPClient::is_done()
567 return req_data
->is_done();
571 * wait for async request to complete
573 int RGWHTTPClient::wait(optional_yield y
)
575 return req_data
->wait(y
);
578 void RGWHTTPClient::cancel()
581 RGWHTTPManager
*http_manager
= req_data
->mgr
;
583 http_manager
->remove_request(this);
588 RGWHTTPClient::~RGWHTTPClient()
597 int RGWHTTPHeadersCollector::receive_header(void * const ptr
, const size_t len
)
599 const boost::string_ref
header_line(static_cast<const char *>(ptr
), len
);
601 /* We're tokening the line that way due to backward compatibility. */
602 const size_t sep_loc
= header_line
.find_first_of(" \t:");
604 if (boost::string_ref::npos
== sep_loc
) {
605 /* Wrongly formatted header? Just skip it. */
609 header_name_t
name(header_line
.substr(0, sep_loc
));
610 if (0 == relevant_headers
.count(name
)) {
611 /* Not interested in this particular header. */
615 const auto value_part
= header_line
.substr(sep_loc
+ 1);
617 /* Skip spaces and tabs after the separator. */
618 const size_t val_loc_s
= value_part
.find_first_not_of(' ');
619 const size_t val_loc_e
= value_part
.find_first_of("\r\n");
621 if (boost::string_ref::npos
== val_loc_s
||
622 boost::string_ref::npos
== val_loc_e
) {
623 /* Empty value case. */
624 found_headers
.emplace(name
, header_value_t());
626 found_headers
.emplace(name
, header_value_t(
627 value_part
.substr(val_loc_s
, val_loc_e
- val_loc_s
)));
633 int RGWHTTPTransceiver::send_data(void* ptr
, size_t len
, bool* pause
)
635 int length_to_copy
= 0;
636 if (post_data_index
< post_data
.length()) {
637 length_to_copy
= min(post_data
.length() - post_data_index
, len
);
638 memcpy(ptr
, post_data
.data() + post_data_index
, length_to_copy
);
639 post_data_index
+= length_to_copy
;
641 return length_to_copy
;
645 static int clear_signal(int fd
)
647 // since we're in non-blocking mode, we can try to read a lot more than
648 // one signal from signal_thread() to avoid later wakeups. non-blocking reads
649 // are also required to support the curl_multi_wait bug workaround
650 std::array
<char, 256> buf
;
651 int ret
= ::read(fd
, (void *)buf
.data(), buf
.size());
654 return ret
== -EAGAIN
? 0 : ret
; // clear EAGAIN
659 #if HAVE_CURL_MULTI_WAIT
661 static std::once_flag detect_flag
;
662 static bool curl_multi_wait_bug_present
= false;
664 static int detect_curl_multi_wait_bug(CephContext
*cct
, CURLM
*handle
,
665 int write_fd
, int read_fd
)
669 // write to write_fd so that read_fd becomes readable
671 ret
= ::write(write_fd
, &buf
, sizeof(buf
));
674 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): write() returned " << ret
<< dendl
;
678 // pass read_fd in extra_fds for curl_multi_wait()
680 struct curl_waitfd wait_fd
;
682 wait_fd
.fd
= read_fd
;
683 wait_fd
.events
= CURL_WAIT_POLLIN
;
686 ret
= curl_multi_wait(handle
, &wait_fd
, 1, 0, &num_fds
);
687 if (ret
!= CURLM_OK
) {
688 ldout(cct
, 0) << "ERROR: curl_multi_wait() returned " << ret
<< dendl
;
692 // curl_multi_wait should flag revents when extra_fd is readable. if it
693 // doesn't, the bug is present and we can't rely on revents
694 if (wait_fd
.revents
== 0) {
695 curl_multi_wait_bug_present
= true;
696 ldout(cct
, 0) << "WARNING: detected a version of libcurl which contains a "
697 "bug in curl_multi_wait(). enabling a workaround that may degrade "
698 "performance slightly." << dendl
;
701 return clear_signal(read_fd
);
704 static bool is_signaled(const curl_waitfd
& wait_fd
)
706 if (wait_fd
.fd
< 0) {
711 if (curl_multi_wait_bug_present
) {
712 // we can't rely on revents, so we always return true if a wait_fd is given.
713 // this means we'll be trying a non-blocking read on this fd every time that
714 // curl_multi_wait() wakes up
718 return wait_fd
.revents
> 0;
721 static int do_curl_wait(CephContext
*cct
, CURLM
*handle
, int signal_fd
)
724 struct curl_waitfd wait_fd
;
726 wait_fd
.fd
= signal_fd
;
727 wait_fd
.events
= CURL_WAIT_POLLIN
;
730 int ret
= curl_multi_wait(handle
, &wait_fd
, 1, cct
->_conf
->rgw_curl_wait_timeout_ms
, &num_fds
);
732 ldout(cct
, 0) << "ERROR: curl_multi_wait() returned " << ret
<< dendl
;
736 if (is_signaled(wait_fd
)) {
737 ret
= clear_signal(signal_fd
);
739 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): read() returned " << ret
<< dendl
;
748 static int do_curl_wait(CephContext
*cct
, CURLM
*handle
, int signal_fd
)
759 /* get file descriptors from the transfers */
760 int ret
= curl_multi_fdset(handle
, &fdread
, &fdwrite
, &fdexcep
, &maxfd
);
762 ldout(cct
, 0) << "ERROR: curl_multi_fdset returned " << ret
<< dendl
;
767 FD_SET(signal_fd
, &fdread
);
768 if (signal_fd
>= maxfd
) {
769 maxfd
= signal_fd
+ 1;
773 /* forcing a strict timeout, as the returned fdsets might not reference all fds we wait on */
774 uint64_t to
= cct
->_conf
->rgw_curl_wait_timeout_ms
;
775 #define RGW_CURL_TIMEOUT 1000
777 to
= RGW_CURL_TIMEOUT
;
778 struct timeval timeout
;
779 timeout
.tv_sec
= to
/ 1000;
780 timeout
.tv_usec
= to
% 1000;
782 ret
= select(maxfd
+1, &fdread
, &fdwrite
, &fdexcep
, &timeout
);
785 ldout(cct
, 0) << "ERROR: select returned " << ret
<< dendl
;
789 if (signal_fd
> 0 && FD_ISSET(signal_fd
, &fdread
)) {
790 ret
= clear_signal(signal_fd
);
792 ldout(cct
, 0) << "ERROR: " << __func__
<< "(): read() returned " << ret
<< dendl
;
802 void *RGWHTTPManager::ReqsThread::entry()
804 manager
->reqs_thread_entry();
809 * RGWHTTPManager has two modes of operation: threaded and non-threaded.
811 RGWHTTPManager::RGWHTTPManager(CephContext
*_cct
, RGWCompletionManager
*_cm
) : cct(_cct
),
814 multi_handle
= (void *)curl_multi_init();
819 RGWHTTPManager::~RGWHTTPManager() {
822 curl_multi_cleanup((CURLM
*)multi_handle
);
825 void RGWHTTPManager::register_request(rgw_http_req_data
*req_data
)
827 std::unique_lock rl
{reqs_lock
};
828 req_data
->id
= num_reqs
;
829 req_data
->registered
= true;
830 reqs
[num_reqs
] = req_data
;
832 ldout(cct
, 20) << __func__
<< " mgr=" << this << " req_data->id=" << req_data
->id
<< ", curl_handle=" << req_data
->curl_handle
<< dendl
;
835 bool RGWHTTPManager::unregister_request(rgw_http_req_data
*req_data
)
837 std::unique_lock rl
{reqs_lock
};
838 if (!req_data
->registered
) {
842 req_data
->registered
= false;
843 unregistered_reqs
.push_back(req_data
);
844 ldout(cct
, 20) << __func__
<< " mgr=" << this << " req_data->id=" << req_data
->id
<< ", curl_handle=" << req_data
->curl_handle
<< dendl
;
848 void RGWHTTPManager::complete_request(rgw_http_req_data
*req_data
)
850 std::unique_lock rl
{reqs_lock
};
851 _complete_request(req_data
);
854 void RGWHTTPManager::_complete_request(rgw_http_req_data
*req_data
)
856 map
<uint64_t, rgw_http_req_data
*>::iterator iter
= reqs
.find(req_data
->id
);
857 if (iter
!= reqs
.end()) {
861 std::lock_guard l
{req_data
->lock
};
862 req_data
->mgr
= nullptr;
864 if (completion_mgr
) {
865 completion_mgr
->complete(NULL
, req_data
->control_io_id
, req_data
->user_info
);
871 void RGWHTTPManager::finish_request(rgw_http_req_data
*req_data
, int ret
, long http_status
)
873 req_data
->finish(ret
, http_status
);
874 complete_request(req_data
);
877 void RGWHTTPManager::_finish_request(rgw_http_req_data
*req_data
, int ret
)
879 req_data
->finish(ret
);
880 _complete_request(req_data
);
883 void RGWHTTPManager::_set_req_state(set_state
& ss
)
885 ss
.req
->set_state(ss
.bitmask
);
888 * hook request to the curl multi handle
890 int RGWHTTPManager::link_request(rgw_http_req_data
*req_data
)
892 ldout(cct
, 20) << __func__
<< " req_data=" << req_data
<< " req_data->id=" << req_data
->id
<< ", curl_handle=" << req_data
->curl_handle
<< dendl
;
893 CURLMcode mstatus
= curl_multi_add_handle((CURLM
*)multi_handle
, req_data
->get_easy_handle());
895 dout(0) << "ERROR: failed on curl_multi_add_handle, status=" << mstatus
<< dendl
;
902 * unhook request from the curl multi handle, and finish request if it wasn't finished yet as
903 * there will be no more processing on this request
905 void RGWHTTPManager::_unlink_request(rgw_http_req_data
*req_data
)
907 if (req_data
->curl_handle
) {
908 curl_multi_remove_handle((CURLM
*)multi_handle
, req_data
->get_easy_handle());
910 if (!req_data
->is_done()) {
911 _finish_request(req_data
, -ECANCELED
);
915 void RGWHTTPManager::unlink_request(rgw_http_req_data
*req_data
)
917 std::unique_lock wl
{reqs_lock
};
918 _unlink_request(req_data
);
921 void RGWHTTPManager::manage_pending_requests()
923 reqs_lock
.lock_shared();
924 if (max_threaded_req
== num_reqs
&&
925 unregistered_reqs
.empty() &&
926 reqs_change_state
.empty()) {
927 reqs_lock
.unlock_shared();
930 reqs_lock
.unlock_shared();
932 std::unique_lock wl
{reqs_lock
};
934 if (!reqs_change_state
.empty()) {
935 for (auto siter
: reqs_change_state
) {
936 _set_req_state(siter
);
938 reqs_change_state
.clear();
941 if (!unregistered_reqs
.empty()) {
942 for (auto& r
: unregistered_reqs
) {
947 unregistered_reqs
.clear();
950 map
<uint64_t, rgw_http_req_data
*>::iterator iter
= reqs
.find(max_threaded_req
);
952 list
<std::pair
<rgw_http_req_data
*, int> > remove_reqs
;
954 for (; iter
!= reqs
.end(); ++iter
) {
955 rgw_http_req_data
*req_data
= iter
->second
;
956 int r
= link_request(req_data
);
958 ldout(cct
, 0) << "ERROR: failed to link http request" << dendl
;
959 remove_reqs
.push_back(std::make_pair(iter
->second
, r
));
961 max_threaded_req
= iter
->first
+ 1;
965 for (auto piter
: remove_reqs
) {
966 rgw_http_req_data
*req_data
= piter
.first
;
967 int r
= piter
.second
;
969 _finish_request(req_data
, r
);
973 int RGWHTTPManager::add_request(RGWHTTPClient
*client
)
975 rgw_http_req_data
*req_data
= new rgw_http_req_data
;
977 int ret
= client
->init_request(req_data
);
984 req_data
->mgr
= this;
985 req_data
->client
= client
;
986 req_data
->control_io_id
= client
->get_io_id(RGWHTTPClient::HTTPCLIENT_IO_CONTROL
);
987 req_data
->user_info
= client
->get_io_user_info();
989 register_request(req_data
);
992 ret
= link_request(req_data
);
999 ret
= signal_thread();
1001 finish_request(req_data
, ret
);
1007 int RGWHTTPManager::remove_request(RGWHTTPClient
*client
)
1009 rgw_http_req_data
*req_data
= client
->get_req_data();
1012 unlink_request(req_data
);
1015 if (!unregister_request(req_data
)) {
1018 int ret
= signal_thread();
1026 int RGWHTTPManager::set_request_state(RGWHTTPClient
*client
, RGWHTTPRequestSetState state
)
1028 rgw_http_req_data
*req_data
= client
->get_req_data();
1030 ceph_assert(ceph_mutex_is_locked(req_data
->lock
));
1032 /* can only do that if threaded */
1037 bool suggested_wr_paused
= req_data
->write_paused
;
1038 bool suggested_rd_paused
= req_data
->read_paused
;
1041 case SET_WRITE_PAUSED
:
1042 suggested_wr_paused
= true;
1044 case SET_WRITE_RESUME
:
1045 suggested_wr_paused
= false;
1047 case SET_READ_PAUSED
:
1048 suggested_rd_paused
= true;
1050 case SET_READ_RESUME
:
1051 suggested_rd_paused
= false;
1054 /* shouldn't really be here */
1057 if (suggested_wr_paused
== req_data
->write_paused
&&
1058 suggested_rd_paused
== req_data
->read_paused
) {
1062 req_data
->write_paused
= suggested_wr_paused
;
1063 req_data
->read_paused
= suggested_rd_paused
;
1065 int bitmask
= CURLPAUSE_CONT
;
1067 if (req_data
->write_paused
) {
1068 bitmask
|= CURLPAUSE_SEND
;
1071 if (req_data
->read_paused
) {
1072 bitmask
|= CURLPAUSE_RECV
;
1075 reqs_change_state
.push_back(set_state(req_data
, bitmask
));
1076 int ret
= signal_thread();
1084 int RGWHTTPManager::start()
1086 if (pipe_cloexec(thread_pipe
, 0) < 0) {
1088 ldout(cct
, 0) << "ERROR: pipe(): " << cpp_strerror(e
) << dendl
;
1092 // enable non-blocking reads
1093 if (::fcntl(thread_pipe
[0], F_SETFL
, O_NONBLOCK
) < 0) {
1095 ldout(cct
, 0) << "ERROR: fcntl(): " << cpp_strerror(e
) << dendl
;
1096 TEMP_FAILURE_RETRY(::close(thread_pipe
[0]));
1097 TEMP_FAILURE_RETRY(::close(thread_pipe
[1]));
1101 #ifdef HAVE_CURL_MULTI_WAIT
1102 // on first initialization, use this pipe to detect whether we're using a
1103 // buggy version of libcurl
1104 std::call_once(detect_flag
, detect_curl_multi_wait_bug
, cct
,
1105 static_cast<CURLM
*>(multi_handle
),
1106 thread_pipe
[1], thread_pipe
[0]);
1110 reqs_thread
= new ReqsThread(this);
1111 reqs_thread
->create("http_manager");
1115 void RGWHTTPManager::stop()
1126 reqs_thread
->join();
1128 TEMP_FAILURE_RETRY(::close(thread_pipe
[1]));
1129 TEMP_FAILURE_RETRY(::close(thread_pipe
[0]));
1133 int RGWHTTPManager::signal_thread()
1136 int ret
= write(thread_pipe
[1], (void *)&buf
, sizeof(buf
));
1139 ldout(cct
, 0) << "ERROR: " << __func__
<< ": write() returned ret=" << ret
<< dendl
;
1145 void *RGWHTTPManager::reqs_thread_entry()
1150 ldout(cct
, 20) << __func__
<< ": start" << dendl
;
1152 while (!going_down
) {
1153 int ret
= do_curl_wait(cct
, (CURLM
*)multi_handle
, thread_pipe
[0]);
1155 dout(0) << "ERROR: do_curl_wait() returned: " << ret
<< dendl
;
1159 manage_pending_requests();
1161 mstatus
= curl_multi_perform((CURLM
*)multi_handle
, &still_running
);
1164 case CURLM_CALL_MULTI_PERFORM
:
1167 dout(10) << "curl_multi_perform returned: " << mstatus
<< dendl
;
1172 while ((msg
= curl_multi_info_read((CURLM
*)multi_handle
, &msgs_left
))) {
1173 if (msg
->msg
== CURLMSG_DONE
) {
1174 int result
= msg
->data
.result
;
1175 CURL
*e
= msg
->easy_handle
;
1176 rgw_http_req_data
*req_data
;
1177 curl_easy_getinfo(e
, CURLINFO_PRIVATE
, (void **)&req_data
);
1178 curl_multi_remove_handle((CURLM
*)multi_handle
, e
);
1182 if (!req_data
->user_ret
) {
1183 curl_easy_getinfo(e
, CURLINFO_RESPONSE_CODE
, (void **)&http_status
);
1185 status
= rgw_http_error_to_errno(http_status
);
1186 if (result
!= CURLE_OK
&& status
== 0) {
1187 dout(0) << "ERROR: curl error: " << curl_easy_strerror((CURLcode
)result
) << ", maybe network unstable" << dendl
;
1191 status
= *req_data
->user_ret
;
1193 set_req_state_err(err
, status
, 0);
1194 http_status
= err
.http_ret
;
1196 int id
= req_data
->id
;
1197 finish_request(req_data
, status
, http_status
);
1201 case CURLE_OPERATION_TIMEDOUT
:
1202 dout(0) << "WARNING: curl operation timed out, network average transfer speed less than "
1203 << cct
->_conf
->rgw_curl_low_speed_limit
<< " Bytes per second during " << cct
->_conf
->rgw_curl_low_speed_time
<< " seconds." << dendl
;
1205 dout(20) << "ERROR: msg->data.result=" << result
<< " req_data->id=" << id
<< " http_status=" << http_status
<< dendl
;
1206 dout(20) << "ERROR: curl error: " << curl_easy_strerror((CURLcode
)result
) << dendl
;
1214 std::unique_lock rl
{reqs_lock
};
1215 for (auto r
: unregistered_reqs
) {
1219 unregistered_reqs
.clear();
1221 auto all_reqs
= std::move(reqs
);
1222 for (auto iter
: all_reqs
) {
1223 _unlink_request(iter
.second
);
1228 if (completion_mgr
) {
1229 completion_mgr
->go_down();
1235 void rgw_http_client_init(CephContext
*cct
)
1237 curl_global_init(CURL_GLOBAL_ALL
);
1238 rgw_http_manager
= new RGWHTTPManager(cct
);
1239 rgw_http_manager
->start();
1242 void rgw_http_client_cleanup()
1244 rgw_http_manager
->stop();
1245 delete rgw_http_manager
;
1246 curl_global_cleanup();
1250 int RGWHTTP::send(RGWHTTPClient
*req
) {
1254 int r
= rgw_http_manager
->add_request(req
);
1262 int RGWHTTP::process(RGWHTTPClient
*req
, optional_yield y
) {
1271 return req
->wait(y
);