-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgw_asio_frontend.cc
586 lines (514 loc) · 16 KB
/
rgw_asio_frontend.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include "rgw_asio_client.h"
#include "rgw_asio_frontend.h"
#ifdef WITH_RADOSGW_BEAST_OPENSSL
#include <boost/asio/ssl.hpp>
#endif
#define dout_subsys ceph_subsys_rgw
namespace {
class Pauser {
std::mutex mutex;
std::condition_variable cond_ready; // signaled on ready==true
std::condition_variable cond_paused; // signaled on waiters==thread_count
bool ready{false};
int waiters{0};
public:
template <typename Func>
void pause(int thread_count, Func&& func);
void unpause();
void wait();
};
template <typename Func>
void Pauser::pause(int thread_count, Func&& func)
{
std::unique_lock<std::mutex> lock(mutex);
ready = false;
lock.unlock();
func();
// wait for all threads to pause
lock.lock();
cond_paused.wait(lock, [=] { return waiters == thread_count; });
}
void Pauser::unpause()
{
std::lock_guard<std::mutex> lock(mutex);
ready = true;
cond_ready.notify_all();
}
void Pauser::wait()
{
std::unique_lock<std::mutex> lock(mutex);
++waiters;
cond_paused.notify_one(); // notify pause() that we're waiting
cond_ready.wait(lock, [this] { return ready; }); // wait for unpause()
--waiters;
}
using tcp = boost::asio::ip::tcp;
namespace beast = boost::beast;
#ifdef WITH_RADOSGW_BEAST_OPENSSL
namespace ssl = boost::asio::ssl;
#endif
template <typename Stream>
class StreamIO : public rgw::asio::ClientIO {
Stream& stream;
beast::flat_buffer& buffer;
public:
StreamIO(Stream& stream, rgw::asio::parser_type& parser,
beast::flat_buffer& buffer, bool is_ssl,
const tcp::endpoint& local_endpoint,
const tcp::endpoint& remote_endpoint)
: ClientIO(parser, is_ssl, local_endpoint, remote_endpoint),
stream(stream), buffer(buffer)
{}
size_t write_data(const char* buf, size_t len) override {
boost::system::error_code ec;
auto bytes = boost::asio::write(stream, boost::asio::buffer(buf, len), ec);
if (ec) {
derr << "write_data failed: " << ec.message() << dendl;
throw rgw::io::Exception(ec.value(), std::system_category());
}
return bytes;
}
size_t recv_body(char* buf, size_t max) override {
auto& message = parser.get();
auto& body_remaining = message.body();
body_remaining.data = buf;
body_remaining.size = max;
while (body_remaining.size && !parser.is_done()) {
boost::system::error_code ec;
beast::http::read_some(stream, buffer, parser, ec);
if (ec == beast::http::error::partial_message ||
ec == beast::http::error::need_buffer) {
break;
}
if (ec) {
derr << "failed to read body: " << ec.message() << dendl;
throw rgw::io::Exception(ec.value(), std::system_category());
}
}
return max - body_remaining.size;
}
};
template <typename Stream>
void handle_connection(RGWProcessEnv& env, Stream& stream,
beast::flat_buffer& buffer, bool is_ssl,
boost::system::error_code& ec,
boost::asio::yield_context yield)
{
// limit header to 4k, since we read it all into a single flat_buffer
static constexpr size_t header_limit = 4096;
// don't impose a limit on the body, since we read it in pieces
static constexpr size_t body_limit = std::numeric_limits<size_t>::max();
auto cct = env.store->ctx();
// read messages from the stream until eof
for (;;) {
// configure the parser
rgw::asio::parser_type parser;
parser.header_limit(header_limit);
parser.body_limit(body_limit);
// parse the header
beast::http::async_read_header(stream, buffer, parser, yield[ec]);
if (ec == boost::asio::error::connection_reset ||
#ifdef WITH_RADOSGW_BEAST_OPENSSL
ec == ssl::error::stream_truncated ||
#endif
ec == beast::http::error::end_of_stream) {
return;
}
if (ec) {
ldout(cct, 1) << "failed to read header: " << ec.message() << dendl;
auto& message = parser.get();
beast::http::response<beast::http::empty_body> response;
response.result(beast::http::status::bad_request);
response.version(message.version() == 10 ? 10 : 11);
response.prepare_payload();
beast::http::async_write(stream, response, yield[ec]);
if (ec) {
ldout(cct, 5) << "failed to write response: " << ec.message() << dendl;
}
ldout(cct, 1) << "====== req done http_status=400 ======" << dendl;
return;
}
// process the request
RGWRequest req{env.store->get_new_req_id()};
auto& socket = stream.lowest_layer();
StreamIO real_client{stream, parser, buffer, is_ssl,
socket.local_endpoint(),
socket.remote_endpoint()};
auto real_client_io = rgw::io::add_reordering(
rgw::io::add_buffering(cct,
rgw::io::add_chunking(
rgw::io::add_conlen_controlling(
&real_client))));
RGWRestfulIO client(cct, &real_client_io);
process_request(env.store, env.rest, &req, env.uri_prefix,
*env.auth_registry, &client, env.olog);
if (!parser.keep_alive()) {
return;
}
// if we failed before reading the entire message, discard any remaining
// bytes before reading the next
while (!parser.is_done()) {
static std::array<char, 1024> discard_buffer;
auto& body = parser.get().body();
body.size = discard_buffer.size();
body.data = discard_buffer.data();
beast::http::async_read_some(stream, buffer, parser, yield[ec]);
if (ec == boost::asio::error::connection_reset) {
return;
}
if (ec) {
ldout(cct, 5) << "failed to discard unread message: "
<< ec.message() << dendl;
return;
}
}
}
}
class AsioFrontend {
RGWProcessEnv env;
RGWFrontendConfig* conf;
boost::asio::io_service service;
#ifdef WITH_RADOSGW_BEAST_OPENSSL
boost::optional<ssl::context> ssl_context;
int init_ssl();
#endif
struct Listener {
tcp::endpoint endpoint;
tcp::acceptor acceptor;
tcp::socket socket;
bool use_ssl = false;
Listener(boost::asio::io_service& service)
: acceptor(service), socket(service) {}
};
std::vector<Listener> listeners;
std::vector<std::thread> threads;
Pauser pauser;
std::atomic<bool> going_down{false};
CephContext* ctx() const { return env.store->ctx(); }
void accept(Listener& listener, boost::system::error_code ec);
public:
AsioFrontend(const RGWProcessEnv& env, RGWFrontendConfig* conf)
: env(env), conf(conf) {}
int init();
int run();
void stop();
void join();
void pause();
void unpause(RGWRados* store, rgw_auth_registry_ptr_t);
};
unsigned short parse_port(const char *input, boost::system::error_code& ec)
{
char *end = nullptr;
auto port = std::strtoul(input, &end, 10);
if (port > std::numeric_limits<unsigned short>::max()) {
ec.assign(ERANGE, boost::system::system_category());
} else if (port == 0 && end == input) {
ec.assign(EINVAL, boost::system::system_category());
}
return port;
}
tcp::endpoint parse_endpoint(boost::asio::string_view input,
boost::system::error_code& ec)
{
tcp::endpoint endpoint;
auto colon = input.find(':');
if (colon != input.npos) {
auto port_str = input.substr(colon + 1);
endpoint.port(parse_port(port_str.data(), ec));
} else {
endpoint.port(80);
}
if (!ec) {
auto addr = input.substr(0, colon);
endpoint.address(boost::asio::ip::make_address(addr, ec));
}
return endpoint;
}
int AsioFrontend::init()
{
boost::system::error_code ec;
auto& config = conf->get_config_map();
#ifdef WITH_RADOSGW_BEAST_OPENSSL
int r = init_ssl();
if (r < 0) {
return r;
}
#endif
// parse endpoints
auto ports = config.equal_range("port");
for (auto i = ports.first; i != ports.second; ++i) {
auto port = parse_port(i->second.c_str(), ec);
if (ec) {
lderr(ctx()) << "failed to parse port=" << i->second << dendl;
return -ec.value();
}
listeners.emplace_back(service);
listeners.back().endpoint.port(port);
}
auto endpoints = config.equal_range("endpoint");
for (auto i = endpoints.first; i != endpoints.second; ++i) {
auto endpoint = parse_endpoint(i->second, ec);
if (ec) {
lderr(ctx()) << "failed to parse endpoint=" << i->second << dendl;
return -ec.value();
}
listeners.emplace_back(service);
listeners.back().endpoint = endpoint;
}
// start listeners
for (auto& l : listeners) {
l.acceptor.open(l.endpoint.protocol(), ec);
if (ec) {
lderr(ctx()) << "failed to open socket: " << ec.message() << dendl;
return -ec.value();
}
l.acceptor.set_option(tcp::acceptor::reuse_address(true));
l.acceptor.bind(l.endpoint, ec);
if (ec) {
lderr(ctx()) << "failed to bind address " << l.endpoint
<< ": " << ec.message() << dendl;
return -ec.value();
}
l.acceptor.listen(boost::asio::socket_base::max_connections);
l.acceptor.async_accept(l.socket,
[this, &l] (boost::system::error_code ec) {
accept(l, ec);
});
ldout(ctx(), 4) << "frontend listening on " << l.endpoint << dendl;
}
return 0;
}
#ifdef WITH_RADOSGW_BEAST_OPENSSL
int AsioFrontend::init_ssl()
{
boost::system::error_code ec;
auto& config = conf->get_config_map();
// ssl configuration
auto cert = config.find("ssl_certificate");
const bool have_cert = cert != config.end();
if (have_cert) {
// only initialize the ssl context if it's going to be used
ssl_context = boost::in_place(ssl::context::tls);
}
auto key = config.find("ssl_private_key");
const bool have_private_key = key != config.end();
if (have_private_key) {
if (!have_cert) {
lderr(ctx()) << "no ssl_certificate configured for ssl_private_key" << dendl;
return -EINVAL;
}
ssl_context->use_private_key_file(key->second, ssl::context::pem, ec);
if (ec) {
lderr(ctx()) << "failed to add ssl_private_key=" << key->second
<< ": " << ec.message() << dendl;
return -ec.value();
}
}
if (have_cert) {
ssl_context->use_certificate_chain_file(cert->second, ec);
if (ec) {
lderr(ctx()) << "failed to use ssl_certificate=" << cert->second
<< ": " << ec.message() << dendl;
return -ec.value();
}
if (!have_private_key) {
// attempt to use it as a private key if a separate one wasn't provided
ssl_context->use_private_key_file(cert->second, ssl::context::pem, ec);
if (ec) {
lderr(ctx()) << "failed to use ssl_certificate=" << cert->second
<< " as a private key: " << ec.message() << dendl;
return -ec.value();
}
}
}
// parse ssl endpoints
auto ports = config.equal_range("ssl_port");
for (auto i = ports.first; i != ports.second; ++i) {
if (!have_cert) {
lderr(ctx()) << "no ssl_certificate configured for ssl_port" << dendl;
return -EINVAL;
}
auto port = parse_port(i->second.c_str(), ec);
if (ec) {
lderr(ctx()) << "failed to parse ssl_port=" << i->second << dendl;
return -ec.value();
}
listeners.emplace_back(service);
listeners.back().endpoint.port(port);
listeners.back().use_ssl = true;
}
auto endpoints = config.equal_range("ssl_endpoint");
for (auto i = endpoints.first; i != endpoints.second; ++i) {
if (!have_cert) {
lderr(ctx()) << "no ssl_certificate configured for ssl_endpoint" << dendl;
return -EINVAL;
}
auto endpoint = parse_endpoint(i->second, ec);
if (ec) {
lderr(ctx()) << "failed to parse ssl_endpoint=" << i->second << dendl;
return -ec.value();
}
listeners.emplace_back(service);
listeners.back().endpoint = endpoint;
listeners.back().use_ssl = true;
}
return 0;
}
#endif // WITH_RADOSGW_BEAST_OPENSSL
void AsioFrontend::accept(Listener& l, boost::system::error_code ec)
{
if (!l.acceptor.is_open()) {
return;
} else if (ec == boost::asio::error::operation_aborted) {
return;
} else if (ec) {
throw ec;
}
auto socket = std::move(l.socket);
l.acceptor.async_accept(l.socket,
[this, &l] (boost::system::error_code ec) {
accept(l, ec);
});
// spawn a coroutine to handle the connection
#ifdef WITH_RADOSGW_BEAST_OPENSSL
if (l.use_ssl) {
boost::asio::spawn(service,
[this, s=std::move(socket)] (boost::asio::yield_context yield) mutable {
// wrap the socket in an ssl stream
ssl::stream<tcp::socket&> stream{s, *ssl_context};
beast::flat_buffer buffer;
// do ssl handshake
boost::system::error_code ec;
auto bytes = stream.async_handshake(ssl::stream_base::server,
buffer.data(), yield[ec]);
if (ec) {
ldout(ctx(), 1) << "ssl handshake failed: " << ec.message() << dendl;
return;
}
buffer.consume(bytes);
handle_connection(env, stream, buffer, true, ec, yield);
if (!ec) {
// ssl shutdown (ignoring errors)
stream.async_shutdown(yield[ec]);
}
s.shutdown(tcp::socket::shutdown_both, ec);
});
} else {
#else
{
#endif // WITH_RADOSGW_BEAST_OPENSSL
boost::asio::spawn(service,
[this, s=std::move(socket)] (boost::asio::yield_context yield) mutable {
beast::flat_buffer buffer;
boost::system::error_code ec;
handle_connection(env, s, buffer, false, ec, yield);
s.shutdown(tcp::socket::shutdown_both, ec);
});
}
}
int AsioFrontend::run()
{
auto cct = ctx();
const int thread_count = cct->_conf->rgw_thread_pool_size;
threads.reserve(thread_count);
ldout(cct, 4) << "frontend spawning " << thread_count << " threads" << dendl;
for (int i = 0; i < thread_count; i++) {
threads.emplace_back([=] {
for (;;) {
service.run();
if (going_down) {
break;
}
pauser.wait();
}
});
}
return 0;
}
void AsioFrontend::stop()
{
ldout(ctx(), 4) << "frontend initiating shutdown..." << dendl;
going_down = true;
boost::system::error_code ec;
// close all listeners
for (auto& listener : listeners) {
listener.acceptor.close(ec);
}
// unblock the run() threads
service.stop();
}
void AsioFrontend::join()
{
if (!going_down) {
stop();
}
ldout(ctx(), 4) << "frontend joining threads..." << dendl;
for (auto& thread : threads) {
thread.join();
}
ldout(ctx(), 4) << "frontend done" << dendl;
}
void AsioFrontend::pause()
{
ldout(ctx(), 4) << "frontend pausing threads..." << dendl;
pauser.pause(threads.size(), [=] {
// unblock the run() threads
service.stop();
});
ldout(ctx(), 4) << "frontend paused" << dendl;
}
void AsioFrontend::unpause(RGWRados* const store,
rgw_auth_registry_ptr_t auth_registry)
{
env.store = store;
env.auth_registry = std::move(auth_registry);
ldout(ctx(), 4) << "frontend unpaused" << dendl;
service.reset();
pauser.unpause();
}
} // anonymous namespace
class RGWAsioFrontend::Impl : public AsioFrontend {
public:
Impl(const RGWProcessEnv& env, RGWFrontendConfig* conf) : AsioFrontend(env, conf) {}
};
RGWAsioFrontend::RGWAsioFrontend(const RGWProcessEnv& env,
RGWFrontendConfig* conf)
: impl(new Impl(env, conf))
{
}
RGWAsioFrontend::~RGWAsioFrontend() = default;
int RGWAsioFrontend::init()
{
return impl->init();
}
int RGWAsioFrontend::run()
{
return impl->run();
}
void RGWAsioFrontend::stop()
{
impl->stop();
}
void RGWAsioFrontend::join()
{
impl->join();
}
void RGWAsioFrontend::pause_for_new_config()
{
impl->pause();
}
void RGWAsioFrontend::unpause_with_new_config(
RGWRados* const store,
rgw_auth_registry_ptr_t auth_registry
) {
impl->unpause(store, std::move(auth_registry));
}