2012-06-05 18:26:04 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-06-05 18:26:04 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_worker.h"
|
|
|
|
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2018-06-09 09:21:30 +02:00
|
|
|
# include <unistd.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_UNISTD_H
|
2013-09-24 16:17:53 +02:00
|
|
|
|
2021-08-24 14:13:05 +02:00
|
|
|
#include <cstdio>
|
2013-09-23 17:14:55 +02:00
|
|
|
#include <memory>
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2021-08-31 09:45:08 +02:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
2021-08-24 14:13:05 +02:00
|
|
|
#ifdef HAVE_LIBBPF
|
|
|
|
# include <bpf/bpf.h>
|
|
|
|
# include <bpf/libbpf.h>
|
|
|
|
#endif // HAVE_LIBBPF
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
#include "shrpx_tls.h"
|
2012-06-05 18:26:04 +02:00
|
|
|
#include "shrpx_log.h"
|
2014-12-27 18:59:06 +01:00
|
|
|
#include "shrpx_client_handler.h"
|
2013-11-04 09:53:57 +01:00
|
|
|
#include "shrpx_http2_session.h"
|
2015-02-25 16:02:29 +01:00
|
|
|
#include "shrpx_log_config.h"
|
2015-07-25 15:22:17 +02:00
|
|
|
#include "shrpx_memcached_dispatcher.h"
|
2015-09-03 17:54:41 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
2018-06-09 09:21:30 +02:00
|
|
|
# include "shrpx_mruby.h"
|
2015-09-03 17:54:41 +02:00
|
|
|
#endif // HAVE_MRUBY
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
|
|
|
# include "shrpx_quic_listener.h"
|
|
|
|
#endif // ENABLE_HTTP3
|
2021-08-24 14:13:05 +02:00
|
|
|
#include "shrpx_connection_handler.h"
|
2013-09-23 17:14:55 +02:00
|
|
|
#include "util.h"
|
2015-02-05 15:21:53 +01:00
|
|
|
#include "template.h"
|
2021-08-24 14:13:05 +02:00
|
|
|
#include "xsi_strerror.h"
|
2013-09-23 17:14:55 +02:00
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace {
|
2014-12-27 18:59:06 +01:00
|
|
|
void eventcb(struct ev_loop *loop, ev_async *w, int revents) {
|
|
|
|
auto worker = static_cast<Worker *>(w->data);
|
|
|
|
worker->process_events();
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-04-08 06:43:57 +02:00
|
|
|
namespace {
|
|
|
|
void mcpool_clear_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto worker = static_cast<Worker *>(w->data);
|
|
|
|
if (worker->get_worker_stat()->num_connections != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-12-16 15:27:58 +01:00
|
|
|
auto mcpool = worker->get_mcpool();
|
|
|
|
if (mcpool->freelistsize == mcpool->poolsize) {
|
|
|
|
worker->get_mcpool()->clear();
|
|
|
|
}
|
2015-04-08 06:43:57 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
namespace {
|
|
|
|
void proc_wev_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto worker = static_cast<Worker *>(w->data);
|
|
|
|
worker->process_events();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2018-08-21 14:11:07 +02:00
|
|
|
DownstreamAddrGroup::DownstreamAddrGroup() : retired{false} {}
|
|
|
|
|
|
|
|
DownstreamAddrGroup::~DownstreamAddrGroup() {}
|
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
// DownstreamKey is used to index SharedDownstreamAddr in order to
|
|
|
|
// find the same configuration.
|
2022-03-16 09:39:33 +01:00
|
|
|
using DownstreamKey = std::tuple<
|
|
|
|
std::vector<
|
|
|
|
std::tuple<StringRef, StringRef, StringRef, size_t, size_t, Proto,
|
|
|
|
uint32_t, uint32_t, uint32_t, bool, bool, bool, bool>>,
|
|
|
|
bool, SessionAffinity, StringRef, StringRef, SessionAffinityCookieSecure,
|
|
|
|
SessionAffinityCookieStickiness, int64_t, int64_t, StringRef, bool>;
|
2016-03-22 15:51:00 +01:00
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
namespace {
|
2019-09-16 15:25:06 +02:00
|
|
|
DownstreamKey
|
|
|
|
create_downstream_key(const std::shared_ptr<SharedDownstreamAddr> &shared_addr,
|
|
|
|
const StringRef &mruby_file) {
|
2017-11-30 16:28:36 +01:00
|
|
|
DownstreamKey dkey;
|
|
|
|
|
|
|
|
auto &addrs = std::get<0>(dkey);
|
|
|
|
addrs.resize(shared_addr->addrs.size());
|
|
|
|
auto p = std::begin(addrs);
|
|
|
|
for (auto &a : shared_addr->addrs) {
|
|
|
|
std::get<0>(*p) = a.host;
|
|
|
|
std::get<1>(*p) = a.sni;
|
2019-01-19 03:12:05 +01:00
|
|
|
std::get<2>(*p) = a.group;
|
|
|
|
std::get<3>(*p) = a.fall;
|
|
|
|
std::get<4>(*p) = a.rise;
|
|
|
|
std::get<5>(*p) = a.proto;
|
|
|
|
std::get<6>(*p) = a.port;
|
|
|
|
std::get<7>(*p) = a.weight;
|
|
|
|
std::get<8>(*p) = a.group_weight;
|
|
|
|
std::get<9>(*p) = a.host_unix;
|
|
|
|
std::get<10>(*p) = a.tls;
|
|
|
|
std::get<11>(*p) = a.dns;
|
|
|
|
std::get<12>(*p) = a.upgrade_scheme;
|
2017-11-30 16:28:36 +01:00
|
|
|
++p;
|
2017-10-25 17:45:22 +02:00
|
|
|
}
|
2017-11-30 16:28:36 +01:00
|
|
|
std::sort(std::begin(addrs), std::end(addrs));
|
2017-10-25 17:45:22 +02:00
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
std::get<1>(dkey) = shared_addr->redirect_if_not_tls;
|
2016-03-23 14:13:53 +01:00
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
auto &affinity = shared_addr->affinity;
|
|
|
|
std::get<2>(dkey) = affinity.type;
|
|
|
|
std::get<3>(dkey) = affinity.cookie.name;
|
|
|
|
std::get<4>(dkey) = affinity.cookie.path;
|
|
|
|
std::get<5>(dkey) = affinity.cookie.secure;
|
2022-03-16 09:39:33 +01:00
|
|
|
std::get<6>(dkey) = affinity.cookie.stickiness;
|
2018-09-30 05:23:34 +02:00
|
|
|
auto &timeout = shared_addr->timeout;
|
2022-03-16 09:39:33 +01:00
|
|
|
std::get<7>(dkey) = timeout.read;
|
|
|
|
std::get<8>(dkey) = timeout.write;
|
|
|
|
std::get<9>(dkey) = mruby_file;
|
|
|
|
std::get<10>(dkey) = shared_addr->dnf;
|
2016-03-23 14:13:53 +01:00
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
return dkey;
|
2016-03-22 15:51:00 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
Worker::Worker(struct ev_loop *loop, SSL_CTX *sv_ssl_ctx, SSL_CTX *cl_ssl_ctx,
|
2016-02-11 14:56:45 +01:00
|
|
|
SSL_CTX *tls_session_cache_memcached_ssl_ctx,
|
2021-08-21 12:00:43 +02:00
|
|
|
tls::CertLookupTree *cert_tree,
|
|
|
|
#ifdef ENABLE_HTTP3
|
|
|
|
SSL_CTX *quic_sv_ssl_ctx, tls::CertLookupTree *quic_cert_tree,
|
2021-08-23 14:40:24 +02:00
|
|
|
const uint8_t *cid_prefix, size_t cid_prefixlen,
|
2021-08-24 14:13:05 +02:00
|
|
|
# ifdef HAVE_LIBBPF
|
|
|
|
size_t index,
|
|
|
|
# endif // HAVE_LIBBPF
|
|
|
|
#endif // ENABLE_HTTP3
|
2016-06-03 18:02:57 +02:00
|
|
|
const std::shared_ptr<TicketKeys> &ticket_keys,
|
|
|
|
ConnectionHandler *conn_handler,
|
2017-03-26 14:14:34 +02:00
|
|
|
std::shared_ptr<DownstreamConfig> downstreamconf)
|
2021-08-24 14:13:05 +02:00
|
|
|
:
|
|
|
|
#if defined(ENABLE_HTTP3) && defined(HAVE_LIBBPF)
|
|
|
|
index_{index},
|
|
|
|
#endif // ENABLE_HTTP3 && HAVE_LIBBPF
|
|
|
|
randgen_(util::make_mt19937()),
|
2016-02-27 15:24:14 +01:00
|
|
|
worker_stat_{},
|
2016-12-04 15:43:41 +01:00
|
|
|
dns_tracker_(loop),
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-08-15 05:22:10 +02:00
|
|
|
quic_upstream_addrs_{get_config()->conn.quic_listener.addrs},
|
2021-08-21 12:00:43 +02:00
|
|
|
#endif // ENABLE_HTTP3
|
2016-01-27 13:14:07 +01:00
|
|
|
loop_(loop),
|
|
|
|
sv_ssl_ctx_(sv_ssl_ctx),
|
|
|
|
cl_ssl_ctx_(cl_ssl_ctx),
|
|
|
|
cert_tree_(cert_tree),
|
2016-06-03 18:02:57 +02:00
|
|
|
conn_handler_(conn_handler),
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-08-16 08:11:18 +02:00
|
|
|
quic_sv_ssl_ctx_{quic_sv_ssl_ctx},
|
|
|
|
quic_cert_tree_{quic_cert_tree},
|
|
|
|
quic_conn_handler_{this},
|
2021-08-21 12:00:43 +02:00
|
|
|
#endif // ENABLE_HTTP3
|
2015-07-09 19:52:11 +02:00
|
|
|
ticket_keys_(ticket_keys),
|
2016-05-25 16:07:04 +02:00
|
|
|
connect_blocker_(
|
2019-01-19 03:12:05 +01:00
|
|
|
std::make_unique<ConnectBlocker>(randgen_, loop_, nullptr, nullptr)),
|
2015-02-25 14:53:23 +01:00
|
|
|
graceful_shutdown_(false) {
|
2021-08-23 14:40:24 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
|
|
|
std::copy_n(cid_prefix, cid_prefixlen, std::begin(cid_prefix_));
|
|
|
|
#endif // ENABLE_HTTP3
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_async_init(&w_, eventcb);
|
|
|
|
w_.data = this;
|
|
|
|
ev_async_start(loop_, &w_);
|
|
|
|
|
2015-04-08 06:43:57 +02:00
|
|
|
ev_timer_init(&mcpool_clear_timer_, mcpool_clear_cb, 0., 0.);
|
|
|
|
mcpool_clear_timer_.data = this;
|
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
ev_timer_init(&proc_wev_timer_, proc_wev_cb, 0., 0.);
|
|
|
|
proc_wev_timer_.data = this;
|
|
|
|
|
2016-01-18 06:21:09 +01:00
|
|
|
auto &session_cacheconf = get_config()->tls.session_cache;
|
|
|
|
|
2016-02-14 12:59:10 +01:00
|
|
|
if (!session_cacheconf.memcached.host.empty()) {
|
2018-10-15 16:02:44 +02:00
|
|
|
session_cache_memcached_dispatcher_ = std::make_unique<MemcachedDispatcher>(
|
2016-02-11 14:56:45 +01:00
|
|
|
&session_cacheconf.memcached.addr, loop,
|
|
|
|
tls_session_cache_memcached_ssl_ctx,
|
2016-07-08 16:41:53 +02:00
|
|
|
StringRef{session_cacheconf.memcached.host}, &mcpool_, randgen_);
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 18:02:57 +02:00
|
|
|
replace_downstream_config(std::move(downstreamconf));
|
2016-06-03 12:13:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
namespace {
|
2019-01-21 16:01:17 +01:00
|
|
|
void ensure_enqueue_addr(
|
|
|
|
std::priority_queue<WeightGroupEntry, std::vector<WeightGroupEntry>,
|
|
|
|
WeightGroupEntryGreater> &wgpq,
|
|
|
|
WeightGroup *wg, DownstreamAddr *addr) {
|
2019-01-19 03:12:05 +01:00
|
|
|
uint32_t cycle;
|
|
|
|
if (!wg->pq.empty()) {
|
2019-01-21 16:01:17 +01:00
|
|
|
auto &top = wg->pq.top();
|
|
|
|
cycle = top.cycle;
|
2019-01-19 03:12:05 +01:00
|
|
|
} else {
|
|
|
|
cycle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr->cycle = cycle;
|
|
|
|
addr->pending_penalty = 0;
|
2019-01-21 16:01:17 +01:00
|
|
|
wg->pq.push(DownstreamAddrEntry{addr, addr->seq, addr->cycle});
|
2019-01-19 03:12:05 +01:00
|
|
|
addr->queued = true;
|
|
|
|
|
|
|
|
if (!wg->queued) {
|
|
|
|
if (!wgpq.empty()) {
|
2019-01-21 16:01:17 +01:00
|
|
|
auto &top = wgpq.top();
|
|
|
|
cycle = top.cycle;
|
2019-01-19 03:12:05 +01:00
|
|
|
} else {
|
|
|
|
cycle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
wg->cycle = cycle;
|
|
|
|
wg->pending_penalty = 0;
|
2019-01-21 16:01:17 +01:00
|
|
|
wgpq.push(WeightGroupEntry{wg, wg->seq, wg->cycle});
|
2019-01-19 03:12:05 +01:00
|
|
|
wg->queued = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-06-03 12:13:02 +02:00
|
|
|
void Worker::replace_downstream_config(
|
2016-06-03 18:02:57 +02:00
|
|
|
std::shared_ptr<DownstreamConfig> downstreamconf) {
|
2016-06-04 05:16:31 +02:00
|
|
|
for (auto &g : downstream_addr_groups_) {
|
|
|
|
g->retired = true;
|
2016-06-09 15:35:59 +02:00
|
|
|
|
|
|
|
auto &shared_addr = g->shared_addr;
|
|
|
|
for (auto &addr : shared_addr->addrs) {
|
|
|
|
addr.dconn_pool->remove_all();
|
|
|
|
}
|
2016-06-04 05:16:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 12:13:02 +02:00
|
|
|
downstreamconf_ = downstreamconf;
|
|
|
|
|
2016-06-16 16:04:06 +02:00
|
|
|
// Making a copy is much faster with multiple thread on
|
|
|
|
// backendconfig API call.
|
|
|
|
auto groups = downstreamconf->addr_groups;
|
2016-02-27 15:24:14 +01:00
|
|
|
|
2016-06-16 16:04:06 +02:00
|
|
|
downstream_addr_groups_ =
|
|
|
|
std::vector<std::shared_ptr<DownstreamAddrGroup>>(groups.size());
|
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
std::map<DownstreamKey, size_t> addr_groups_indexer;
|
2018-08-31 14:58:35 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
2018-08-24 16:11:21 +02:00
|
|
|
// TODO It is a bit less efficient because
|
|
|
|
// mruby::create_mruby_context returns std::unique_ptr and we cannot
|
|
|
|
// use std::make_shared.
|
|
|
|
std::map<StringRef, std::shared_ptr<mruby::MRubyContext>> shared_mruby_ctxs;
|
2018-08-31 14:58:35 +02:00
|
|
|
#endif // HAVE_MRUBY
|
2017-11-30 16:28:36 +01:00
|
|
|
|
2016-06-16 16:04:06 +02:00
|
|
|
for (size_t i = 0; i < groups.size(); ++i) {
|
|
|
|
auto &src = groups[i];
|
2016-02-27 15:24:14 +01:00
|
|
|
auto &dst = downstream_addr_groups_[i];
|
|
|
|
|
2016-06-02 18:20:49 +02:00
|
|
|
dst = std::make_shared<DownstreamAddrGroup>();
|
2016-10-02 10:44:45 +02:00
|
|
|
dst->pattern =
|
|
|
|
ImmutableString{std::begin(src.pattern), std::end(src.pattern)};
|
2016-03-22 15:51:00 +01:00
|
|
|
|
|
|
|
auto shared_addr = std::make_shared<SharedDownstreamAddr>();
|
2016-03-22 17:32:17 +01:00
|
|
|
|
2016-03-22 15:51:00 +01:00
|
|
|
shared_addr->addrs.resize(src.addrs.size());
|
2017-10-25 17:45:22 +02:00
|
|
|
shared_addr->affinity.type = src.affinity.type;
|
2018-10-16 15:25:42 +02:00
|
|
|
if (src.affinity.type == SessionAffinity::COOKIE) {
|
2017-10-25 17:45:22 +02:00
|
|
|
shared_addr->affinity.cookie.name =
|
|
|
|
make_string_ref(shared_addr->balloc, src.affinity.cookie.name);
|
|
|
|
if (!src.affinity.cookie.path.empty()) {
|
|
|
|
shared_addr->affinity.cookie.path =
|
|
|
|
make_string_ref(shared_addr->balloc, src.affinity.cookie.path);
|
|
|
|
}
|
2017-11-21 14:24:38 +01:00
|
|
|
shared_addr->affinity.cookie.secure = src.affinity.cookie.secure;
|
2022-03-16 09:39:33 +01:00
|
|
|
shared_addr->affinity.cookie.stickiness = src.affinity.cookie.stickiness;
|
2017-10-25 17:45:22 +02:00
|
|
|
}
|
2016-07-06 15:31:28 +02:00
|
|
|
shared_addr->affinity_hash = src.affinity_hash;
|
2022-03-16 09:39:33 +01:00
|
|
|
shared_addr->affinity_hash_map = src.affinity_hash_map;
|
2017-02-18 10:23:06 +01:00
|
|
|
shared_addr->redirect_if_not_tls = src.redirect_if_not_tls;
|
2021-08-13 14:20:08 +02:00
|
|
|
shared_addr->dnf = src.dnf;
|
2018-09-30 05:23:34 +02:00
|
|
|
shared_addr->timeout.read = src.timeout.read;
|
|
|
|
shared_addr->timeout.write = src.timeout.write;
|
2016-05-24 16:36:43 +02:00
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
for (size_t j = 0; j < src.addrs.size(); ++j) {
|
|
|
|
auto &src_addr = src.addrs[j];
|
2016-03-22 15:51:00 +01:00
|
|
|
auto &dst_addr = shared_addr->addrs[j];
|
2016-02-27 15:24:14 +01:00
|
|
|
|
|
|
|
dst_addr.addr = src_addr.addr;
|
2016-10-02 10:44:45 +02:00
|
|
|
dst_addr.host = make_string_ref(shared_addr->balloc, src_addr.host);
|
2016-10-02 09:18:35 +02:00
|
|
|
dst_addr.hostport =
|
2016-10-02 10:44:45 +02:00
|
|
|
make_string_ref(shared_addr->balloc, src_addr.hostport);
|
2016-02-27 15:24:14 +01:00
|
|
|
dst_addr.port = src_addr.port;
|
|
|
|
dst_addr.host_unix = src_addr.host_unix;
|
2019-01-19 03:12:05 +01:00
|
|
|
dst_addr.weight = src_addr.weight;
|
|
|
|
dst_addr.group = make_string_ref(shared_addr->balloc, src_addr.group);
|
|
|
|
dst_addr.group_weight = src_addr.group_weight;
|
2022-03-16 09:39:33 +01:00
|
|
|
dst_addr.affinity_hash = src_addr.affinity_hash;
|
2016-05-24 16:36:43 +02:00
|
|
|
dst_addr.proto = src_addr.proto;
|
|
|
|
dst_addr.tls = src_addr.tls;
|
2016-10-02 10:44:45 +02:00
|
|
|
dst_addr.sni = make_string_ref(shared_addr->balloc, src_addr.sni);
|
2016-04-09 14:58:08 +02:00
|
|
|
dst_addr.fall = src_addr.fall;
|
|
|
|
dst_addr.rise = src_addr.rise;
|
2016-12-04 15:43:41 +01:00
|
|
|
dst_addr.dns = src_addr.dns;
|
2018-01-08 10:08:01 +01:00
|
|
|
dst_addr.upgrade_scheme = src_addr.upgrade_scheme;
|
2016-02-21 06:53:06 +01:00
|
|
|
}
|
2016-03-22 15:51:00 +01:00
|
|
|
|
2019-09-16 15:25:06 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
|
|
|
auto mruby_ctx_it = shared_mruby_ctxs.find(src.mruby_file);
|
|
|
|
if (mruby_ctx_it == std::end(shared_mruby_ctxs)) {
|
|
|
|
shared_addr->mruby_ctx = mruby::create_mruby_context(src.mruby_file);
|
|
|
|
assert(shared_addr->mruby_ctx);
|
|
|
|
shared_mruby_ctxs.emplace(src.mruby_file, shared_addr->mruby_ctx);
|
|
|
|
} else {
|
|
|
|
shared_addr->mruby_ctx = (*mruby_ctx_it).second;
|
|
|
|
}
|
|
|
|
#endif // HAVE_MRUBY
|
|
|
|
|
2016-03-22 15:51:00 +01:00
|
|
|
// share the connection if patterns have the same set of backend
|
|
|
|
// addresses.
|
2017-11-30 16:28:36 +01:00
|
|
|
|
2019-09-16 15:25:06 +02:00
|
|
|
auto dkey = create_downstream_key(shared_addr, src.mruby_file);
|
2017-11-30 16:28:36 +01:00
|
|
|
auto it = addr_groups_indexer.find(dkey);
|
|
|
|
|
|
|
|
if (it == std::end(addr_groups_indexer)) {
|
2021-07-14 16:09:28 +02:00
|
|
|
auto shared_addr_ptr = shared_addr.get();
|
|
|
|
|
|
|
|
for (auto &addr : shared_addr->addrs) {
|
|
|
|
addr.connect_blocker = std::make_unique<ConnectBlocker>(
|
|
|
|
randgen_, loop_, nullptr, [shared_addr_ptr, &addr]() {
|
|
|
|
if (!addr.queued) {
|
|
|
|
if (!addr.wg) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ensure_enqueue_addr(shared_addr_ptr->pq, addr.wg, &addr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
addr.live_check = std::make_unique<LiveCheck>(loop_, cl_ssl_ctx_, this,
|
|
|
|
&addr, randgen_);
|
|
|
|
}
|
|
|
|
|
2019-01-19 03:12:05 +01:00
|
|
|
size_t seq = 0;
|
2019-01-11 15:26:08 +01:00
|
|
|
for (auto &addr : shared_addr->addrs) {
|
|
|
|
addr.dconn_pool = std::make_unique<DownstreamConnectionPool>();
|
2019-01-19 03:12:05 +01:00
|
|
|
addr.seq = seq++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shared_addr->affinity.type == SessionAffinity::NONE) {
|
|
|
|
std::map<StringRef, WeightGroup *> wgs;
|
|
|
|
size_t num_wgs = 0;
|
|
|
|
for (auto &addr : shared_addr->addrs) {
|
|
|
|
if (wgs.find(addr.group) == std::end(wgs)) {
|
|
|
|
++num_wgs;
|
|
|
|
wgs.emplace(addr.group, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_addr->wgs = std::vector<WeightGroup>(num_wgs);
|
|
|
|
|
|
|
|
for (auto &addr : shared_addr->addrs) {
|
|
|
|
auto &wg = wgs[addr.group];
|
|
|
|
if (wg == nullptr) {
|
|
|
|
wg = &shared_addr->wgs[--num_wgs];
|
|
|
|
wg->seq = num_wgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
wg->weight = addr.group_weight;
|
2019-01-21 16:01:17 +01:00
|
|
|
wg->pq.push(DownstreamAddrEntry{&addr, addr.seq, addr.cycle});
|
2019-01-19 03:12:05 +01:00
|
|
|
addr.queued = true;
|
|
|
|
addr.wg = wg;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(num_wgs == 0);
|
|
|
|
|
|
|
|
for (auto &kv : wgs) {
|
2019-01-21 16:01:17 +01:00
|
|
|
shared_addr->pq.push(
|
|
|
|
WeightGroupEntry{kv.second, kv.second->seq, kv.second->cycle});
|
2019-01-19 03:12:05 +01:00
|
|
|
kv.second->queued = true;
|
|
|
|
}
|
2016-06-09 15:35:59 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 18:20:49 +02:00
|
|
|
dst->shared_addr = shared_addr;
|
2017-11-30 16:28:36 +01:00
|
|
|
|
|
|
|
addr_groups_indexer.emplace(std::move(dkey), i);
|
2016-03-22 15:51:00 +01:00
|
|
|
} else {
|
2017-11-30 16:28:36 +01:00
|
|
|
auto &g = *(std::begin(downstream_addr_groups_) + (*it).second);
|
2016-04-16 15:04:35 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2016-06-02 18:20:49 +02:00
|
|
|
LOG(INFO) << dst->pattern << " shares the same backend group with "
|
2017-11-30 16:28:36 +01:00
|
|
|
<< g->pattern;
|
2016-04-16 15:04:35 +02:00
|
|
|
}
|
2017-11-30 16:28:36 +01:00
|
|
|
dst->shared_addr = g->shared_addr;
|
2016-03-22 15:51:00 +01:00
|
|
|
}
|
2016-02-21 06:53:06 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2015-04-08 06:43:57 +02:00
|
|
|
Worker::~Worker() {
|
|
|
|
ev_async_stop(loop_, &w_);
|
|
|
|
ev_timer_stop(loop_, &mcpool_clear_timer_);
|
2016-06-25 04:50:33 +02:00
|
|
|
ev_timer_stop(loop_, &proc_wev_timer_);
|
2015-04-08 06:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::schedule_clear_mcpool() {
|
|
|
|
// libev manual says: "If the watcher is already active nothing will
|
|
|
|
// happen." Since we don't change any timeout here, we don't have
|
|
|
|
// to worry about querying ev_is_active.
|
|
|
|
ev_timer_start(loop_, &mcpool_clear_timer_);
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-07 17:57:59 +01:00
|
|
|
void Worker::wait() {
|
|
|
|
#ifndef NOTHREADS
|
|
|
|
fut_.get();
|
|
|
|
#endif // !NOTHREADS
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
void Worker::run_async() {
|
|
|
|
#ifndef NOTHREADS
|
|
|
|
fut_ = std::async(std::launch::async, [this] {
|
2017-02-16 14:46:22 +01:00
|
|
|
(void)reopen_log_files(get_config()->logging);
|
2015-02-11 11:18:41 +01:00
|
|
|
ev_run(loop_);
|
2017-02-08 14:14:23 +01:00
|
|
|
delete_log_config();
|
2015-02-11 11:18:41 +01:00
|
|
|
});
|
|
|
|
#endif // !NOTHREADS
|
|
|
|
}
|
|
|
|
|
2021-08-23 14:40:24 +02:00
|
|
|
void Worker::send(WorkerEvent event) {
|
2014-12-27 18:59:06 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> g(m_);
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2021-08-23 14:40:24 +02:00
|
|
|
q_.emplace_back(std::move(event));
|
2013-09-24 16:17:53 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
ev_async_send(loop_, &w_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::process_events() {
|
2016-06-25 04:50:33 +02:00
|
|
|
WorkerEvent wev;
|
2014-12-27 18:59:06 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> g(m_);
|
2016-01-19 08:56:12 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
// Process event one at a time. This is important for
|
2018-10-17 07:18:07 +02:00
|
|
|
// WorkerEventType::NEW_CONNECTION event since accepting large
|
|
|
|
// number of new connections at once may delay time to 1st byte
|
|
|
|
// for existing connections.
|
2016-01-19 08:56:12 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
if (q_.empty()) {
|
|
|
|
ev_timer_stop(loop_, &proc_wev_timer_);
|
|
|
|
return;
|
|
|
|
}
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2021-08-23 14:40:24 +02:00
|
|
|
wev = std::move(q_.front());
|
2016-06-25 04:50:33 +02:00
|
|
|
q_.pop_front();
|
|
|
|
}
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
ev_timer_start(loop_, &proc_wev_timer_);
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2017-02-16 14:46:22 +01:00
|
|
|
auto config = get_config();
|
|
|
|
|
|
|
|
auto worker_connections = config->conn.upstream.worker_connections;
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
switch (wev.type) {
|
2018-10-17 07:18:07 +02:00
|
|
|
case WorkerEventType::NEW_CONNECTION: {
|
2016-06-25 04:50:33 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(INFO, this) << "WorkerEvent: client_fd=" << wev.client_fd
|
|
|
|
<< ", addrlen=" << wev.client_addrlen;
|
|
|
|
}
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
if (worker_stat_.num_connections >= worker_connections) {
|
2015-01-08 13:20:17 +01:00
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2016-06-25 04:50:33 +02:00
|
|
|
WLOG(INFO, this) << "Too many connections >= " << worker_connections;
|
2015-01-08 13:20:17 +01:00
|
|
|
}
|
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
close(wev.client_fd);
|
|
|
|
|
2015-01-08 14:23:30 +01:00
|
|
|
break;
|
2015-01-08 13:20:17 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
auto client_handler =
|
2017-04-01 08:07:32 +02:00
|
|
|
tls::accept_connection(this, wev.client_fd, &wev.client_addr.sa,
|
2016-06-25 04:50:33 +02:00
|
|
|
wev.client_addrlen, wev.faddr);
|
|
|
|
if (!client_handler) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(ERROR, this) << "ClientHandler creation failed";
|
|
|
|
}
|
|
|
|
close(wev.client_fd);
|
2015-01-08 14:23:30 +01:00
|
|
|
break;
|
2016-06-25 04:50:33 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(INFO, this) << "CLIENT_HANDLER:" << client_handler << " created ";
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-17 07:18:07 +02:00
|
|
|
case WorkerEventType::REOPEN_LOG:
|
2016-06-25 04:50:33 +02:00
|
|
|
WLOG(NOTICE, this) << "Reopening log files: worker process (thread " << this
|
|
|
|
<< ")";
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2017-02-16 14:46:22 +01:00
|
|
|
reopen_log_files(config->logging);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
break;
|
2018-10-17 07:18:07 +02:00
|
|
|
case WorkerEventType::GRACEFUL_SHUTDOWN:
|
2016-06-25 04:50:33 +02:00
|
|
|
WLOG(NOTICE, this) << "Graceful shutdown commencing";
|
2016-06-03 18:02:57 +02:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
graceful_shutdown_ = true;
|
2016-06-03 18:02:57 +02:00
|
|
|
|
2021-09-05 11:01:27 +02:00
|
|
|
if (worker_stat_.num_connections == 0 &&
|
|
|
|
worker_stat_.num_close_waits == 0) {
|
2016-06-25 04:50:33 +02:00
|
|
|
ev_break(loop_);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2018-10-17 07:18:07 +02:00
|
|
|
case WorkerEventType::REPLACE_DOWNSTREAM:
|
2016-06-25 04:50:33 +02:00
|
|
|
WLOG(NOTICE, this) << "Replace downstream";
|
|
|
|
|
|
|
|
replace_downstream_config(wev.downstreamconf);
|
|
|
|
|
|
|
|
break;
|
2021-08-23 14:40:24 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
|
|
|
case WorkerEventType::QUIC_PKT_FORWARD: {
|
2021-08-29 03:18:59 +02:00
|
|
|
const UpstreamAddr *faddr;
|
|
|
|
|
|
|
|
if (wev.quic_pkt->upstream_addr_index == static_cast<size_t>(-1)) {
|
|
|
|
faddr = find_quic_upstream_addr(wev.quic_pkt->local_addr);
|
|
|
|
if (faddr == nullptr) {
|
|
|
|
LOG(ERROR) << "No suitable upstream address found";
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (quic_upstream_addrs_.size() <=
|
|
|
|
wev.quic_pkt->upstream_addr_index) {
|
|
|
|
LOG(ERROR) << "upstream_addr_index is too large";
|
|
|
|
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
faddr = &quic_upstream_addrs_[wev.quic_pkt->upstream_addr_index];
|
|
|
|
}
|
|
|
|
|
2021-08-23 14:40:24 +02:00
|
|
|
quic_conn_handler_.handle_packet(
|
2021-08-29 03:18:59 +02:00
|
|
|
faddr, wev.quic_pkt->remote_addr, wev.quic_pkt->local_addr,
|
2021-11-05 11:19:23 +01:00
|
|
|
wev.quic_pkt->pi, wev.quic_pkt->data.data(), wev.quic_pkt->data.size());
|
2021-08-23 14:40:24 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif // ENABLE_HTTP3
|
2016-06-25 04:50:33 +02:00
|
|
|
default:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2018-10-17 07:18:07 +02:00
|
|
|
WLOG(INFO, this) << "unknown event type " << static_cast<int>(wev.type);
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
tls::CertLookupTree *Worker::get_cert_lookup_tree() const { return cert_tree_; }
|
2015-02-11 11:18:41 +01:00
|
|
|
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-08-16 08:11:18 +02:00
|
|
|
tls::CertLookupTree *Worker::get_quic_cert_lookup_tree() const {
|
|
|
|
return quic_cert_tree_;
|
|
|
|
}
|
2021-08-21 12:00:43 +02:00
|
|
|
#endif // ENABLE_HTTP3
|
2021-08-16 08:11:18 +02:00
|
|
|
|
2015-07-23 16:13:29 +02:00
|
|
|
std::shared_ptr<TicketKeys> Worker::get_ticket_keys() {
|
2016-09-20 15:24:12 +02:00
|
|
|
#ifdef HAVE_ATOMIC_STD_SHARED_PTR
|
|
|
|
return std::atomic_load_explicit(&ticket_keys_, std::memory_order_acquire);
|
|
|
|
#else // !HAVE_ATOMIC_STD_SHARED_PTR
|
|
|
|
std::lock_guard<std::mutex> g(ticket_keys_m_);
|
2015-02-11 11:18:41 +01:00
|
|
|
return ticket_keys_;
|
2016-09-20 15:24:12 +02:00
|
|
|
#endif // !HAVE_ATOMIC_STD_SHARED_PTR
|
2015-02-11 11:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::set_ticket_keys(std::shared_ptr<TicketKeys> ticket_keys) {
|
2016-09-20 15:24:12 +02:00
|
|
|
#ifdef HAVE_ATOMIC_STD_SHARED_PTR
|
|
|
|
// This is single writer
|
|
|
|
std::atomic_store_explicit(&ticket_keys_, std::move(ticket_keys),
|
|
|
|
std::memory_order_release);
|
|
|
|
#else // !HAVE_ATOMIC_STD_SHARED_PTR
|
|
|
|
std::lock_guard<std::mutex> g(ticket_keys_m_);
|
2015-02-11 11:18:41 +01:00
|
|
|
ticket_keys_ = std::move(ticket_keys);
|
2016-09-20 15:24:12 +02:00
|
|
|
#endif // !HAVE_ATOMIC_STD_SHARED_PTR
|
2015-02-11 11:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkerStat *Worker::get_worker_stat() { return &worker_stat_; }
|
|
|
|
|
|
|
|
struct ev_loop *Worker::get_loop() const {
|
|
|
|
return loop_;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX *Worker::get_sv_ssl_ctx() const { return sv_ssl_ctx_; }
|
|
|
|
|
2015-03-10 13:54:29 +01:00
|
|
|
SSL_CTX *Worker::get_cl_ssl_ctx() const { return cl_ssl_ctx_; }
|
|
|
|
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-08-16 08:11:18 +02:00
|
|
|
SSL_CTX *Worker::get_quic_sv_ssl_ctx() const { return quic_sv_ssl_ctx_; }
|
2021-08-21 12:00:43 +02:00
|
|
|
#endif // ENABLE_HTTP3
|
2021-08-16 08:11:18 +02:00
|
|
|
|
2015-02-25 14:53:23 +01:00
|
|
|
void Worker::set_graceful_shutdown(bool f) { graceful_shutdown_ = f; }
|
|
|
|
|
|
|
|
bool Worker::get_graceful_shutdown() const { return graceful_shutdown_; }
|
|
|
|
|
2015-04-07 15:13:01 +02:00
|
|
|
MemchunkPool *Worker::get_mcpool() { return &mcpool_; }
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
MemcachedDispatcher *Worker::get_session_cache_memcached_dispatcher() {
|
|
|
|
return session_cache_memcached_dispatcher_.get();
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:04:58 +01:00
|
|
|
std::mt19937 &Worker::get_randgen() { return randgen_; }
|
|
|
|
|
2015-09-03 17:54:41 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
2015-09-01 17:19:32 +02:00
|
|
|
int Worker::create_mruby_context() {
|
2016-02-14 14:27:59 +01:00
|
|
|
mruby_ctx_ = mruby::create_mruby_context(StringRef{get_config()->mruby_file});
|
2015-09-01 17:19:32 +02:00
|
|
|
if (!mruby_ctx_) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mruby::MRubyContext *Worker::get_mruby_context() const {
|
|
|
|
return mruby_ctx_.get();
|
|
|
|
}
|
2015-09-03 17:54:41 +02:00
|
|
|
#endif // HAVE_MRUBY
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2016-06-02 18:20:49 +02:00
|
|
|
std::vector<std::shared_ptr<DownstreamAddrGroup>> &
|
|
|
|
Worker::get_downstream_addr_groups() {
|
2016-02-21 06:53:06 +01:00
|
|
|
return downstream_addr_groups_;
|
|
|
|
}
|
|
|
|
|
2016-02-21 07:27:19 +01:00
|
|
|
ConnectBlocker *Worker::get_connect_blocker() const {
|
|
|
|
return connect_blocker_.get();
|
|
|
|
}
|
|
|
|
|
2016-06-03 12:13:02 +02:00
|
|
|
const DownstreamConfig *Worker::get_downstream_config() const {
|
|
|
|
return downstreamconf_.get();
|
2016-06-02 18:20:49 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 18:02:57 +02:00
|
|
|
ConnectionHandler *Worker::get_connection_handler() const {
|
|
|
|
return conn_handler_;
|
|
|
|
}
|
|
|
|
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-08-16 08:11:18 +02:00
|
|
|
QUICConnectionHandler *Worker::get_quic_connection_handler() {
|
|
|
|
return &quic_conn_handler_;
|
|
|
|
}
|
2021-08-21 12:00:43 +02:00
|
|
|
#endif // ENABLE_HTTP3
|
2021-08-16 08:11:18 +02:00
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
DNSTracker *Worker::get_dns_tracker() { return &dns_tracker_; }
|
|
|
|
|
2021-08-21 12:00:43 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-08-24 14:13:05 +02:00
|
|
|
# ifdef HAVE_LIBBPF
|
|
|
|
bool Worker::should_attach_bpf() const {
|
|
|
|
auto config = get_config();
|
2021-08-26 07:13:14 +02:00
|
|
|
auto &quicconf = config->quic;
|
2021-08-24 14:13:05 +02:00
|
|
|
auto &apiconf = config->api;
|
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
if (quicconf.bpf.disabled) {
|
2021-08-24 14:13:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
if (!config->single_thread && apiconf.enabled) {
|
2021-08-24 14:13:05 +02:00
|
|
|
return index_ == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index_ == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Worker::should_update_bpf_map() const {
|
|
|
|
auto config = get_config();
|
2021-08-26 07:13:14 +02:00
|
|
|
auto &quicconf = config->quic;
|
2021-08-24 14:13:05 +02:00
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
return !quicconf.bpf.disabled;
|
2021-08-24 14:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Worker::compute_sk_index() const {
|
|
|
|
auto config = get_config();
|
|
|
|
auto &apiconf = config->api;
|
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
if (!config->single_thread && apiconf.enabled) {
|
2021-08-24 14:13:05 +02:00
|
|
|
return index_ - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index_;
|
|
|
|
}
|
|
|
|
# endif // HAVE_LIBBPF
|
|
|
|
|
2021-08-15 05:22:10 +02:00
|
|
|
int Worker::setup_quic_server_socket() {
|
2021-08-26 11:21:59 +02:00
|
|
|
size_t n = 0;
|
|
|
|
|
2021-08-15 05:22:10 +02:00
|
|
|
for (auto &addr : quic_upstream_addrs_) {
|
2021-08-15 09:57:39 +02:00
|
|
|
assert(!addr.host_unix);
|
2021-08-15 05:22:10 +02:00
|
|
|
if (create_quic_server_socket(addr) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-08-15 09:57:39 +02:00
|
|
|
|
2021-08-26 11:21:59 +02:00
|
|
|
// Make sure that each endpoint has a unique address.
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
|
|
|
const auto &a = quic_upstream_addrs_[i];
|
|
|
|
|
|
|
|
if (addr.hostport == a.hostport) {
|
|
|
|
LOG(FATAL)
|
|
|
|
<< "QUIC frontend endpoint must be unique: a duplicate found for "
|
|
|
|
<< addr.hostport;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++n;
|
|
|
|
|
2021-08-15 09:57:39 +02:00
|
|
|
quic_listeners_.emplace_back(std::make_unique<QUICListener>(&addr, this));
|
2021-08-15 05:22:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-23 14:40:24 +02:00
|
|
|
|
2021-08-24 14:13:05 +02:00
|
|
|
int Worker::create_quic_server_socket(UpstreamAddr &faddr) {
|
|
|
|
std::array<char, STRERROR_BUFSIZE> errbuf;
|
|
|
|
int fd = -1;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
auto service = util::utos(faddr.port);
|
|
|
|
addrinfo hints{};
|
|
|
|
hints.ai_family = faddr.family;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
# ifdef AI_ADDRCONFIG
|
|
|
|
hints.ai_flags |= AI_ADDRCONFIG;
|
|
|
|
# endif // AI_ADDRCONFIG
|
|
|
|
|
|
|
|
auto node =
|
|
|
|
faddr.host == StringRef::from_lit("*") ? nullptr : faddr.host.c_str();
|
|
|
|
|
|
|
|
addrinfo *res, *rp;
|
|
|
|
rv = getaddrinfo(node, service.c_str(), &hints, &res);
|
|
|
|
# ifdef AI_ADDRCONFIG
|
|
|
|
if (rv != 0) {
|
|
|
|
// Retry without AI_ADDRCONFIG
|
|
|
|
hints.ai_flags &= ~AI_ADDRCONFIG;
|
|
|
|
rv = getaddrinfo(node, service.c_str(), &hints, &res);
|
|
|
|
}
|
|
|
|
# endif // AI_ADDRCONFIG
|
|
|
|
if (rv != 0) {
|
|
|
|
LOG(FATAL) << "Unable to get IPv" << (faddr.family == AF_INET ? "4" : "6")
|
|
|
|
<< " address for " << faddr.host << ", port " << faddr.port
|
|
|
|
<< ": " << gai_strerror(rv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto res_d = defer(freeaddrinfo, res);
|
|
|
|
|
|
|
|
std::array<char, NI_MAXHOST> host;
|
|
|
|
|
|
|
|
for (rp = res; rp; rp = rp->ai_next) {
|
|
|
|
rv = getnameinfo(rp->ai_addr, rp->ai_addrlen, host.data(), host.size(),
|
|
|
|
nullptr, 0, NI_NUMERICHOST);
|
|
|
|
if (rv != 0) {
|
|
|
|
LOG(WARN) << "getnameinfo() failed: " << gai_strerror(rv);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef SOCK_NONBLOCK
|
|
|
|
fd = socket(rp->ai_family, rp->ai_socktype | SOCK_NONBLOCK | SOCK_CLOEXEC,
|
|
|
|
rp->ai_protocol);
|
|
|
|
if (fd == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "socket() syscall failed: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
# else // !SOCK_NONBLOCK
|
|
|
|
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if (fd == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "socket() syscall failed: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
util::make_socket_nonblocking(fd);
|
|
|
|
util::make_socket_closeonexec(fd);
|
|
|
|
# endif // !SOCK_NONBLOCK
|
|
|
|
|
|
|
|
int val = 1;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "Failed to set SO_REUSEADDR option to listener socket: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "Failed to set SO_REUSEPORT option to listener socket: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (faddr.family == AF_INET6) {
|
|
|
|
# ifdef IPV6_V6ONLY
|
|
|
|
if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "Failed to set IPV6_V6ONLY option to listener socket: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
# endif // IPV6_V6ONLY
|
|
|
|
|
|
|
|
if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN)
|
|
|
|
<< "Failed to set IPV6_RECVPKTINFO option to listener socket: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-06 08:28:49 +01:00
|
|
|
|
2021-11-06 09:19:02 +01:00
|
|
|
if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVTCLASS, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "Failed to set IPV6_RECVTCLASS option to listener socket: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-19 15:57:34 +02:00
|
|
|
# if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
|
|
|
|
int mtu_disc = IPV6_PMTUDISC_DO;
|
2021-11-06 08:28:49 +01:00
|
|
|
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &mtu_disc,
|
|
|
|
static_cast<socklen_t>(sizeof(mtu_disc))) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN)
|
|
|
|
<< "Failed to set IPV6_MTU_DISCOVER option to listener socket: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
# endif // defined(IPV6_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
|
2021-08-24 14:13:05 +02:00
|
|
|
} else {
|
|
|
|
if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "Failed to set IP_PKTINFO option to listener socket: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-06 08:28:49 +01:00
|
|
|
|
2021-11-06 09:19:02 +01:00
|
|
|
if (setsockopt(fd, IPPROTO_IP, IP_RECVTOS, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "Failed to set IP_RECVTOS option to listener socket: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-06 08:28:49 +01:00
|
|
|
# if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
|
|
|
|
int mtu_disc = IP_PMTUDISC_DO;
|
|
|
|
if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &mtu_disc,
|
|
|
|
static_cast<socklen_t>(sizeof(mtu_disc))) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "Failed to set IP_MTU_DISCOVER option to listener socket: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
# endif // defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
|
2021-08-24 14:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bind(fd, rp->ai_addr, rp->ai_addrlen) == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
LOG(WARN) << "bind() syscall failed: "
|
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef HAVE_LIBBPF
|
2021-08-26 07:13:14 +02:00
|
|
|
auto config = get_config();
|
|
|
|
|
2021-08-24 14:13:05 +02:00
|
|
|
auto &quic_bpf_refs = conn_handler_->get_quic_bpf_refs();
|
|
|
|
|
|
|
|
if (should_attach_bpf()) {
|
2021-08-26 07:13:14 +02:00
|
|
|
auto &bpfconf = config->quic.bpf;
|
|
|
|
|
|
|
|
auto obj = bpf_object__open_file(bpfconf.prog_file.c_str(), nullptr);
|
2022-02-21 13:18:52 +01:00
|
|
|
if (!obj) {
|
|
|
|
auto error = errno;
|
2021-08-24 14:13:05 +02:00
|
|
|
LOG(FATAL) << "Failed to open bpf object file: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
2021-08-24 14:13:05 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:18:52 +01:00
|
|
|
rv = bpf_object__load(obj);
|
|
|
|
if (rv != 0) {
|
2021-08-24 14:13:05 +02:00
|
|
|
LOG(FATAL) << "Failed to load bpf object file: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(-rv, errbuf.data(), errbuf.size());
|
2021-08-24 14:13:05 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto prog = bpf_object__find_program_by_name(obj, "select_reuseport");
|
2022-02-21 13:18:52 +01:00
|
|
|
if (!prog) {
|
|
|
|
auto error = errno;
|
2021-08-24 14:13:05 +02:00
|
|
|
LOG(FATAL) << "Failed to find sk_reuseport program: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
2021-08-24 14:13:05 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &ref = quic_bpf_refs[faddr.index];
|
|
|
|
|
2021-09-29 12:33:16 +02:00
|
|
|
ref.obj = obj;
|
|
|
|
|
2021-08-24 14:13:05 +02:00
|
|
|
auto reuseport_array =
|
|
|
|
bpf_object__find_map_by_name(obj, "reuseport_array");
|
2022-02-21 13:18:52 +01:00
|
|
|
if (!reuseport_array) {
|
|
|
|
auto error = errno;
|
2021-08-24 14:13:05 +02:00
|
|
|
LOG(FATAL) << "Failed to get reuseport_array: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
2021-08-24 14:13:05 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref.reuseport_array = bpf_map__fd(reuseport_array);
|
|
|
|
|
|
|
|
auto cid_prefix_map = bpf_object__find_map_by_name(obj, "cid_prefix_map");
|
2022-02-21 13:18:52 +01:00
|
|
|
if (!cid_prefix_map) {
|
|
|
|
auto error = errno;
|
2021-08-24 14:13:05 +02:00
|
|
|
LOG(FATAL) << "Failed to get cid_prefix_map: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
2021-08-24 14:13:05 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref.cid_prefix_map = bpf_map__fd(cid_prefix_map);
|
|
|
|
|
|
|
|
auto sk_info = bpf_object__find_map_by_name(obj, "sk_info");
|
2022-02-21 13:18:52 +01:00
|
|
|
if (!sk_info) {
|
|
|
|
auto error = errno;
|
2021-08-24 14:13:05 +02:00
|
|
|
LOG(FATAL) << "Failed to get sk_info: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(error, errbuf.data(), errbuf.size());
|
2021-08-24 14:13:05 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr uint32_t zero = 0;
|
2021-09-15 13:07:33 +02:00
|
|
|
uint64_t num_socks = config->num_worker;
|
2021-08-24 14:13:05 +02:00
|
|
|
|
2022-02-21 13:18:52 +01:00
|
|
|
rv =
|
|
|
|
bpf_map_update_elem(bpf_map__fd(sk_info), &zero, &num_socks, BPF_ANY);
|
|
|
|
if (rv != 0) {
|
2021-08-24 14:13:05 +02:00
|
|
|
LOG(FATAL) << "Failed to update sk_info: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(-rv, errbuf.data(), errbuf.size());
|
2021-08-24 14:13:05 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
2021-09-15 13:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr uint32_t key_high_idx = 1;
|
|
|
|
constexpr uint32_t key_low_idx = 2;
|
|
|
|
|
2021-09-22 11:24:50 +02:00
|
|
|
auto &qkms = conn_handler_->get_quic_keying_materials();
|
|
|
|
auto &qkm = qkms->keying_materials.front();
|
|
|
|
|
2022-02-21 13:18:52 +01:00
|
|
|
rv = bpf_map_update_elem(bpf_map__fd(sk_info), &key_high_idx,
|
|
|
|
qkm.cid_encryption_key.data(), BPF_ANY);
|
|
|
|
if (rv != 0) {
|
2021-09-15 13:07:33 +02:00
|
|
|
LOG(FATAL) << "Failed to update key_high_idx sk_info: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(-rv, errbuf.data(), errbuf.size());
|
2021-09-15 13:07:33 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:18:52 +01:00
|
|
|
rv = bpf_map_update_elem(bpf_map__fd(sk_info), &key_low_idx,
|
|
|
|
qkm.cid_encryption_key.data() + 8, BPF_ANY);
|
|
|
|
if (rv != 0) {
|
2021-09-15 13:07:33 +02:00
|
|
|
LOG(FATAL) << "Failed to update key_low_idx sk_info: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(-rv, errbuf.data(), errbuf.size());
|
2021-09-15 13:07:33 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
2021-08-24 14:13:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto prog_fd = bpf_program__fd(prog);
|
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF, &prog_fd,
|
|
|
|
static_cast<socklen_t>(sizeof(prog_fd))) == -1) {
|
|
|
|
LOG(FATAL) << "Failed to attach bpf program: "
|
|
|
|
<< xsi_strerror(errno, errbuf.data(), errbuf.size());
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_update_bpf_map()) {
|
|
|
|
const auto &ref = quic_bpf_refs[faddr.index];
|
|
|
|
auto sk_index = compute_sk_index();
|
|
|
|
|
2022-02-21 13:18:52 +01:00
|
|
|
rv =
|
|
|
|
bpf_map_update_elem(ref.reuseport_array, &sk_index, &fd, BPF_NOEXIST);
|
|
|
|
if (rv != 0) {
|
2021-08-24 14:13:05 +02:00
|
|
|
LOG(FATAL) << "Failed to update reuseport_array: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(-rv, errbuf.data(), errbuf.size());
|
2021-08-24 14:13:05 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:18:52 +01:00
|
|
|
rv = bpf_map_update_elem(ref.cid_prefix_map, cid_prefix_.data(),
|
|
|
|
&sk_index, BPF_NOEXIST);
|
|
|
|
if (rv != 0) {
|
2021-08-24 14:13:05 +02:00
|
|
|
LOG(FATAL) << "Failed to update cid_prefix_map: "
|
2022-02-21 13:18:52 +01:00
|
|
|
<< xsi_strerror(-rv, errbuf.data(), errbuf.size());
|
2021-08-24 14:13:05 +02:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif // HAVE_LIBBPF
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rp) {
|
|
|
|
LOG(FATAL) << "Listening " << (faddr.family == AF_INET ? "IPv4" : "IPv6")
|
|
|
|
<< " socket failed";
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
faddr.fd = fd;
|
|
|
|
faddr.hostport = util::make_http_hostport(mod_config()->balloc,
|
|
|
|
StringRef{host.data()}, faddr.port);
|
|
|
|
|
|
|
|
LOG(NOTICE) << "Listening on " << faddr.hostport << ", quic";
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-23 14:40:24 +02:00
|
|
|
const uint8_t *Worker::get_cid_prefix() const { return cid_prefix_.data(); }
|
2021-08-26 07:46:34 +02:00
|
|
|
|
2021-08-29 03:18:59 +02:00
|
|
|
const UpstreamAddr *Worker::find_quic_upstream_addr(const Address &local_addr) {
|
|
|
|
std::array<char, NI_MAXHOST> host;
|
|
|
|
|
|
|
|
auto rv = getnameinfo(&local_addr.su.sa, local_addr.len, host.data(),
|
|
|
|
host.size(), nullptr, 0, NI_NUMERICHOST);
|
|
|
|
if (rv != 0) {
|
|
|
|
LOG(ERROR) << "getnameinfo: " << gai_strerror(rv);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t port;
|
|
|
|
|
|
|
|
switch (local_addr.su.sa.sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
port = htons(local_addr.su.in.sin_port);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
port = htons(local_addr.su.in6.sin6_port);
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
2021-10-06 14:28:00 +02:00
|
|
|
abort();
|
2021-08-29 03:18:59 +02:00
|
|
|
}
|
|
|
|
|
2021-09-12 10:48:57 +02:00
|
|
|
std::array<char, util::max_hostport> hostport_buf;
|
|
|
|
|
|
|
|
auto hostport = util::make_http_hostport(std::begin(hostport_buf),
|
|
|
|
StringRef{host.data()}, port);
|
2021-08-29 03:18:59 +02:00
|
|
|
const UpstreamAddr *fallback_faddr = nullptr;
|
|
|
|
|
|
|
|
for (auto &faddr : quic_upstream_addrs_) {
|
|
|
|
if (faddr.hostport == hostport) {
|
|
|
|
return &faddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (faddr.port != port || faddr.family != local_addr.su.sa.sa_family) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:48:57 +02:00
|
|
|
if (faddr.port == 443 || faddr.port == 80) {
|
|
|
|
switch (faddr.family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (util::streq(faddr.hostport, StringRef::from_lit("0.0.0.0"))) {
|
|
|
|
fallback_faddr = &faddr;
|
|
|
|
}
|
2021-08-29 03:18:59 +02:00
|
|
|
|
2021-09-12 10:48:57 +02:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (util::streq(faddr.hostport, StringRef::from_lit("[::]"))) {
|
|
|
|
fallback_faddr = &faddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
2021-08-29 03:18:59 +02:00
|
|
|
}
|
2021-09-12 10:48:57 +02:00
|
|
|
} else {
|
|
|
|
switch (faddr.family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (util::starts_with(faddr.hostport,
|
|
|
|
StringRef::from_lit("0.0.0.0:"))) {
|
|
|
|
fallback_faddr = &faddr;
|
|
|
|
}
|
2021-08-29 03:18:59 +02:00
|
|
|
|
2021-09-12 10:48:57 +02:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
if (util::starts_with(faddr.hostport, StringRef::from_lit("[::]:"))) {
|
|
|
|
fallback_faddr = &faddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2021-08-29 03:18:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback_faddr;
|
|
|
|
}
|
2021-08-21 12:00:43 +02:00
|
|
|
#endif // ENABLE_HTTP3
|
2021-08-15 05:22:10 +02:00
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
namespace {
|
|
|
|
size_t match_downstream_addr_group_host(
|
2016-06-11 11:21:37 +02:00
|
|
|
const RouterConfig &routerconf, const StringRef &host,
|
|
|
|
const StringRef &path,
|
2016-06-02 18:20:49 +02:00
|
|
|
const std::vector<std::shared_ptr<DownstreamAddrGroup>> &groups,
|
2016-06-11 11:41:43 +02:00
|
|
|
size_t catch_all, BlockAllocator &balloc) {
|
2016-06-11 11:21:37 +02:00
|
|
|
|
|
|
|
const auto &router = routerconf.router;
|
|
|
|
const auto &rev_wildcard_router = routerconf.rev_wildcard_router;
|
|
|
|
const auto &wildcard_patterns = routerconf.wildcard_patterns;
|
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Perform mapping selection, using host=" << host
|
|
|
|
<< ", path=" << path;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto group = router.match(host, path);
|
|
|
|
if (group != -1) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Found pattern with query " << host << path
|
2016-06-02 18:20:49 +02:00
|
|
|
<< ", matched pattern=" << groups[group]->pattern;
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2016-06-10 16:13:40 +02:00
|
|
|
if (!wildcard_patterns.empty() && !host.empty()) {
|
2016-06-11 11:41:43 +02:00
|
|
|
auto rev_host_src = make_byte_ref(balloc, host.size() - 1);
|
|
|
|
auto ep =
|
|
|
|
std::copy(std::begin(host) + 1, std::end(host), rev_host_src.base);
|
|
|
|
std::reverse(rev_host_src.base, ep);
|
|
|
|
auto rev_host = StringRef{rev_host_src.base, ep};
|
2016-06-11 06:31:04 +02:00
|
|
|
|
|
|
|
ssize_t best_group = -1;
|
|
|
|
const RNode *last_node = nullptr;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
size_t nread = 0;
|
|
|
|
auto wcidx =
|
|
|
|
rev_wildcard_router.match_prefix(&nread, &last_node, rev_host);
|
|
|
|
if (wcidx == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rev_host = StringRef{std::begin(rev_host) + nread, std::end(rev_host)};
|
2016-06-10 16:13:40 +02:00
|
|
|
|
|
|
|
auto &wc = wildcard_patterns[wcidx];
|
|
|
|
auto group = wc.router.match(StringRef{}, path);
|
|
|
|
if (group != -1) {
|
|
|
|
// We sorted wildcard_patterns in a way that first match is the
|
|
|
|
// longest host pattern.
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Found wildcard pattern with query " << host << path
|
|
|
|
<< ", matched pattern=" << groups[group]->pattern;
|
|
|
|
}
|
2016-06-11 06:31:04 +02:00
|
|
|
|
|
|
|
best_group = group;
|
2016-03-12 16:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-11 06:31:04 +02:00
|
|
|
|
|
|
|
if (best_group != -1) {
|
|
|
|
return best_group;
|
|
|
|
}
|
2016-03-12 16:59:25 +01:00
|
|
|
}
|
|
|
|
|
2016-02-28 13:35:26 +01:00
|
|
|
group = router.match(StringRef::from_lit(""), path);
|
2016-02-27 15:24:14 +01:00
|
|
|
if (group != -1) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Found pattern with query " << path
|
2016-06-02 18:20:49 +02:00
|
|
|
<< ", matched pattern=" << groups[group]->pattern;
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "None match. Use catch-all pattern";
|
|
|
|
}
|
|
|
|
return catch_all;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
size_t match_downstream_addr_group(
|
2016-06-11 11:21:37 +02:00
|
|
|
const RouterConfig &routerconf, const StringRef &hostport,
|
|
|
|
const StringRef &raw_path,
|
2016-06-02 18:20:49 +02:00
|
|
|
const std::vector<std::shared_ptr<DownstreamAddrGroup>> &groups,
|
2016-06-11 11:41:43 +02:00
|
|
|
size_t catch_all, BlockAllocator &balloc) {
|
2016-02-27 15:24:14 +01:00
|
|
|
if (std::find(std::begin(hostport), std::end(hostport), '/') !=
|
|
|
|
std::end(hostport)) {
|
|
|
|
// We use '/' specially, and if '/' is included in host, it breaks
|
|
|
|
// our code. Select catch-all case.
|
|
|
|
return catch_all;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto fragment = std::find(std::begin(raw_path), std::end(raw_path), '#');
|
|
|
|
auto query = std::find(std::begin(raw_path), fragment, '?');
|
|
|
|
auto path = StringRef{std::begin(raw_path), query};
|
|
|
|
|
2017-04-18 14:03:50 +02:00
|
|
|
if (path.empty() || path[0] != '/') {
|
|
|
|
path = StringRef::from_lit("/");
|
|
|
|
}
|
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
if (hostport.empty()) {
|
2016-06-11 11:21:37 +02:00
|
|
|
return match_downstream_addr_group_host(routerconf, hostport, path, groups,
|
2016-06-11 11:41:43 +02:00
|
|
|
catch_all, balloc);
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:59:25 +01:00
|
|
|
StringRef host;
|
2016-02-27 15:24:14 +01:00
|
|
|
if (hostport[0] == '[') {
|
|
|
|
// assume this is IPv6 numeric address
|
|
|
|
auto p = std::find(std::begin(hostport), std::end(hostport), ']');
|
|
|
|
if (p == std::end(hostport)) {
|
|
|
|
return catch_all;
|
|
|
|
}
|
|
|
|
if (p + 1 < std::end(hostport) && *(p + 1) != ':') {
|
|
|
|
return catch_all;
|
|
|
|
}
|
2016-03-12 16:59:25 +01:00
|
|
|
host = StringRef{std::begin(hostport), p + 1};
|
2016-02-27 15:24:14 +01:00
|
|
|
} else {
|
|
|
|
auto p = std::find(std::begin(hostport), std::end(hostport), ':');
|
|
|
|
if (p == std::begin(hostport)) {
|
|
|
|
return catch_all;
|
|
|
|
}
|
2016-03-12 16:59:25 +01:00
|
|
|
host = StringRef{std::begin(hostport), p};
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:59:25 +01:00
|
|
|
if (std::find_if(std::begin(host), std::end(host), [](char c) {
|
|
|
|
return 'A' <= c || c <= 'Z';
|
|
|
|
}) != std::end(host)) {
|
2016-06-11 11:41:43 +02:00
|
|
|
auto low_host = make_byte_ref(balloc, host.size() + 1);
|
|
|
|
auto ep = std::copy(std::begin(host), std::end(host), low_host.base);
|
|
|
|
*ep = '\0';
|
|
|
|
util::inp_strlower(low_host.base, ep);
|
|
|
|
host = StringRef{low_host.base, ep};
|
2016-03-12 16:59:25 +01:00
|
|
|
}
|
2016-06-11 11:21:37 +02:00
|
|
|
return match_downstream_addr_group_host(routerconf, host, path, groups,
|
2016-06-11 11:41:43 +02:00
|
|
|
catch_all, balloc);
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
void downstream_failure(DownstreamAddr *addr, const Address *raddr) {
|
2016-04-07 18:04:16 +02:00
|
|
|
const auto &connect_blocker = addr->connect_blocker;
|
|
|
|
|
2016-05-21 03:28:16 +02:00
|
|
|
if (connect_blocker->in_offline()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-07 18:04:16 +02:00
|
|
|
connect_blocker->on_failure();
|
|
|
|
|
2016-04-09 14:58:08 +02:00
|
|
|
if (addr->fall == 0) {
|
2016-04-08 15:35:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-09 14:58:08 +02:00
|
|
|
auto fail_count = connect_blocker->get_fail_count();
|
|
|
|
|
|
|
|
if (fail_count >= addr->fall) {
|
2016-12-04 15:43:41 +01:00
|
|
|
if (raddr) {
|
|
|
|
LOG(WARN) << "Could not connect to " << util::to_numeric_addr(raddr)
|
|
|
|
<< " " << fail_count
|
|
|
|
<< " times in a row; considered as offline";
|
|
|
|
} else {
|
|
|
|
LOG(WARN) << "Could not connect to " << addr->host << ":" << addr->port
|
|
|
|
<< " " << fail_count
|
|
|
|
<< " times in a row; considered as offline";
|
|
|
|
}
|
2016-04-07 18:04:16 +02:00
|
|
|
|
|
|
|
connect_blocker->offline();
|
2016-04-08 15:35:45 +02:00
|
|
|
|
2016-04-09 14:58:08 +02:00
|
|
|
if (addr->rise) {
|
2016-04-08 15:35:45 +02:00
|
|
|
addr->live_check->schedule();
|
|
|
|
}
|
2016-04-07 18:04:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 14:40:24 +02:00
|
|
|
#ifdef ENABLE_HTTP3
|
2021-09-21 15:46:32 +02:00
|
|
|
int create_cid_prefix(uint8_t *cid_prefix, const uint8_t *server_id) {
|
|
|
|
auto p = std::copy_n(server_id, SHRPX_QUIC_SERVER_IDLEN, cid_prefix);
|
|
|
|
|
|
|
|
if (RAND_bytes(p, SHRPX_QUIC_CID_PREFIXLEN - SHRPX_QUIC_SERVER_IDLEN) != 1) {
|
2021-08-23 14:40:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif // ENABLE_HTTP3
|
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
} // namespace shrpx
|