/src/h2o/lib/core/context.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2014 DeNA Co., Ltd. |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | #include <fcntl.h> |
23 | | #include <stddef.h> |
24 | | #include <stdlib.h> |
25 | | #include <sys/time.h> |
26 | | #include "cloexec.h" |
27 | | #include "picotls.h" |
28 | | #include "picotls/openssl.h" |
29 | | #include "h2o.h" |
30 | | #include "h2o/http3_server.h" |
31 | | #include "h2o/memcached.h" |
32 | | #if H2O_USE_FUSION |
33 | | #include "picotls/fusion.h" |
34 | | #endif |
35 | | |
36 | | h2o_http3client_ctx_t *h2o_create_proxy_http3_context(h2o_context_t *ctx, SSL_CTX *ssl_ctx, int use_ecn, int use_gso) |
37 | 0 | { |
38 | | #if H2O_USE_LIBUV |
39 | | h2o_fatal("no HTTP/3 support for libuv"); |
40 | | #else |
41 | |
|
42 | 0 | h2o_http3client_ctx_t *h3ctx = h2o_mem_alloc(sizeof(*h3ctx)); |
43 | | |
44 | | /* tls (TODO inherit session cache setting of ssl_ctx) */ |
45 | 0 | h3ctx->tls = (ptls_context_t){ |
46 | 0 | .random_bytes = ptls_openssl_random_bytes, |
47 | 0 | .get_time = &ptls_get_time, |
48 | 0 | .key_exchanges = ptls_openssl_key_exchanges, |
49 | 0 | .cipher_suites = ptls_openssl_cipher_suites, |
50 | 0 | }; |
51 | 0 | h3ctx->verify_cert = (ptls_openssl_verify_certificate_t){}; |
52 | 0 | if ((SSL_CTX_get_verify_mode(ssl_ctx) & SSL_VERIFY_PEER) != 0) { |
53 | 0 | X509_STORE *store; |
54 | 0 | if ((store = SSL_CTX_get_cert_store(ssl_ctx)) == NULL) |
55 | 0 | h2o_fatal("failed to obtain the store to be used for server certificate verification"); |
56 | 0 | ptls_openssl_init_verify_certificate(&h3ctx->verify_cert, store); |
57 | 0 | h3ctx->tls.verify_certificate = &h3ctx->verify_cert.super; |
58 | 0 | } |
59 | 0 | quicly_amend_ptls_context(&h3ctx->tls); |
60 | | |
61 | | /* quic */ |
62 | 0 | h3ctx->quic = quicly_spec_context; |
63 | 0 | h3ctx->quic.tls = &h3ctx->tls; |
64 | 0 | h3ctx->quic.transport_params.max_streams_uni = 10; |
65 | 0 | if (!use_ecn) |
66 | 0 | h3ctx->quic.enable_ratio.ecn = 0; |
67 | 0 | uint8_t cid_key[PTLS_SHA256_DIGEST_SIZE]; |
68 | 0 | ptls_openssl_random_bytes(cid_key, sizeof(cid_key)); |
69 | 0 | h3ctx->quic.cid_encryptor = quicly_new_default_cid_encryptor( |
70 | | #if H2O_USE_FUSION |
71 | | ptls_fusion_is_supported_by_cpu() ? &ptls_fusion_quiclb : |
72 | | #endif |
73 | 0 | &ptls_openssl_quiclb, |
74 | 0 | &ptls_openssl_aes128ecb, &ptls_openssl_sha256, ptls_iovec_init(cid_key, sizeof(cid_key))); |
75 | 0 | ptls_clear_memory(cid_key, sizeof(cid_key)); |
76 | 0 | h3ctx->quic.stream_open = &h2o_httpclient_http3_on_stream_open; |
77 | | |
78 | | /* http3 client-specific fields */ |
79 | 0 | h3ctx->max_frame_payload_size = h2o_http3_calc_min_flow_control_size(H2O_MAX_REQLEN); /* same maximum for HEADERS frame in both |
80 | | directions */ |
81 | | |
82 | | /* h2o server http3 integration */ |
83 | 0 | h2o_socket_t *socks[2], **sp = socks; |
84 | 0 | if ((*sp = h2o_quic_create_client_socket(ctx->loop, AF_INET)) != NULL) |
85 | 0 | ++sp; |
86 | 0 | if ((*sp = h2o_quic_create_client_socket(ctx->loop, AF_INET6)) != NULL) |
87 | 0 | ++sp; |
88 | 0 | if (sp == socks) { |
89 | 0 | char buf[256]; |
90 | 0 | h2o_fatal("failed to create UDP socket for both IPv4 and v6: %s", h2o_strerror_r(errno, buf, sizeof(buf))); |
91 | 0 | } |
92 | 0 | h2o_http3_server_init_context(ctx, &h3ctx->h3, ctx->loop, socks[0], socks[1], &h3ctx->quic, &ctx->http3.next_cid, NULL, |
93 | 0 | h2o_httpclient_http3_notify_connection_update, use_gso); |
94 | |
|
95 | 0 | h3ctx->load_session = NULL; /* TODO reuse session? */ |
96 | |
|
97 | 0 | return h3ctx; |
98 | 0 | #endif |
99 | 0 | } |
100 | | |
101 | | void h2o_destroy_proxy_http3_context(h2o_http3client_ctx_t *h3ctx) |
102 | 0 | { |
103 | 0 | h2o_quic_dispose_context(&h3ctx->h3); |
104 | 0 | quicly_free_default_cid_encryptor(h3ctx->quic.cid_encryptor); |
105 | 0 | if (h3ctx->verify_cert.super.cb != NULL) |
106 | 0 | ptls_openssl_dispose_verify_certificate(&h3ctx->verify_cert); |
107 | 0 | free(h3ctx); |
108 | 0 | } |
109 | | |
110 | | void h2o_context_init_pathconf_context(h2o_context_t *ctx, h2o_pathconf_t *pathconf) |
111 | 11 | { |
112 | | /* add pathconf to the inited list (or return if already inited) */ |
113 | 11 | size_t i; |
114 | 26 | for (i = 0; i != ctx->_pathconfs_inited.size; ++i) |
115 | 15 | if (ctx->_pathconfs_inited.entries[i] == pathconf) |
116 | 0 | return; |
117 | 11 | h2o_vector_reserve(NULL, &ctx->_pathconfs_inited, ctx->_pathconfs_inited.size + 1); |
118 | 11 | ctx->_pathconfs_inited.entries[ctx->_pathconfs_inited.size++] = pathconf; |
119 | | |
120 | 11 | #define DOIT(type, list) \ |
121 | 33 | do { \ |
122 | 33 | size_t i; \ |
123 | 41 | for (i = 0; i != pathconf->list.size; ++i) { \ |
124 | 8 | type *o = pathconf->list.entries[i]; \ |
125 | 8 | if (o->on_context_init != NULL) \ |
126 | 8 | o->on_context_init(o, ctx); \ |
127 | 8 | } \ |
128 | 33 | } while (0) |
129 | | |
130 | 11 | DOIT(h2o_handler_t, handlers); |
131 | 11 | DOIT(h2o_filter_t, _filters); |
132 | 11 | DOIT(h2o_logger_t, _loggers); |
133 | | |
134 | 11 | #undef DOIT |
135 | 11 | } |
136 | | |
137 | | void h2o_context_dispose_pathconf_context(h2o_context_t *ctx, h2o_pathconf_t *pathconf) |
138 | 0 | { |
139 | | /* nullify pathconf in the inited list (or return if already disposed) */ |
140 | 0 | size_t i; |
141 | 0 | for (i = 0; i != ctx->_pathconfs_inited.size; ++i) |
142 | 0 | if (ctx->_pathconfs_inited.entries[i] == pathconf) |
143 | 0 | break; |
144 | 0 | if (i == ctx->_pathconfs_inited.size) |
145 | 0 | return; |
146 | 0 | ctx->_pathconfs_inited.entries[i] = NULL; |
147 | |
|
148 | 0 | #define DOIT(type, list) \ |
149 | 0 | do { \ |
150 | 0 | size_t i; \ |
151 | 0 | for (i = 0; i != pathconf->list.size; ++i) { \ |
152 | 0 | type *o = pathconf->list.entries[i]; \ |
153 | 0 | if (o->on_context_dispose != NULL) \ |
154 | 0 | o->on_context_dispose(o, ctx); \ |
155 | 0 | } \ |
156 | 0 | } while (0) |
157 | |
|
158 | 0 | DOIT(h2o_handler_t, handlers); |
159 | 0 | DOIT(h2o_filter_t, _filters); |
160 | 0 | DOIT(h2o_logger_t, _loggers); |
161 | |
|
162 | 0 | #undef DOIT |
163 | 0 | } |
164 | | |
165 | | void h2o_context_init(h2o_context_t *ctx, h2o_loop_t *loop, h2o_globalconf_t *config) |
166 | 3 | { |
167 | 3 | size_t i, j; |
168 | | |
169 | 3 | assert(config->hosts[0] != NULL); |
170 | | |
171 | 3 | memset(ctx, 0, sizeof(*ctx)); |
172 | 3 | ctx->loop = loop; |
173 | 3 | ctx->globalconf = config; |
174 | 3 | ctx->queue = h2o_multithread_create_queue(loop); |
175 | 3 | h2o_multithread_register_receiver(ctx->queue, &ctx->receivers.hostinfo_getaddr, h2o_hostinfo_getaddr_receiver); |
176 | 3 | ctx->filecache = h2o_filecache_create(config->filecache.capacity); |
177 | 3 | ctx->spare_pipes.pipes = h2o_mem_alloc(sizeof(ctx->spare_pipes.pipes[0]) * config->max_spare_pipes); |
178 | | |
179 | 3 | h2o_linklist_init_anchor(&ctx->_conns.active); |
180 | 3 | h2o_linklist_init_anchor(&ctx->_conns.idle); |
181 | 3 | h2o_linklist_init_anchor(&ctx->_conns.shutdown); |
182 | 3 | ctx->proxy.client_ctx.loop = loop; |
183 | 3 | ctx->proxy.client_ctx.io_timeout = ctx->globalconf->proxy.io_timeout; |
184 | 3 | ctx->proxy.client_ctx.connect_timeout = ctx->globalconf->proxy.connect_timeout; |
185 | 3 | ctx->proxy.client_ctx.first_byte_timeout = ctx->globalconf->proxy.first_byte_timeout; |
186 | 3 | ctx->proxy.client_ctx.keepalive_timeout = ctx->globalconf->proxy.keepalive_timeout; |
187 | 3 | ctx->proxy.client_ctx.getaddr_receiver = &ctx->receivers.hostinfo_getaddr; |
188 | 3 | ctx->proxy.client_ctx.http2.latency_optimization = ctx->globalconf->http2.latency_optimization; |
189 | 3 | ctx->proxy.client_ctx.max_buffer_size = ctx->globalconf->proxy.max_buffer_size; |
190 | 3 | ctx->proxy.client_ctx.http2.max_concurrent_streams = ctx->globalconf->proxy.http2.max_concurrent_streams; |
191 | 3 | ctx->proxy.client_ctx.protocol_selector.ratio.http2 = ctx->globalconf->proxy.protocol_ratio.http2; |
192 | 3 | ctx->proxy.client_ctx.protocol_selector.ratio.http3 = ctx->globalconf->proxy.protocol_ratio.http3; |
193 | 3 | if (ctx->globalconf->proxy.protocol_ratio.http3 != 0) |
194 | 0 | ctx->proxy.client_ctx.http3 = |
195 | 0 | h2o_create_proxy_http3_context(ctx, ctx->globalconf->proxy.global_socketpool._ssl_ctx, ctx->globalconf->proxy.http3.ecn, |
196 | 0 | ctx->globalconf->http3.use_gso); |
197 | 3 | ctx->proxy.connpool.socketpool = &ctx->globalconf->proxy.global_socketpool; |
198 | 3 | h2o_linklist_init_anchor(&ctx->proxy.connpool.http2.conns); |
199 | | |
200 | 3 | ctx->_module_configs = h2o_mem_alloc(sizeof(*ctx->_module_configs) * config->_num_config_slots); |
201 | 3 | memset(ctx->_module_configs, 0, sizeof(*ctx->_module_configs) * config->_num_config_slots); |
202 | | |
203 | 3 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
204 | 3 | pthread_mutex_lock(&mutex); |
205 | | |
206 | 3 | h2o_socketpool_register_loop(&ctx->globalconf->proxy.global_socketpool, loop); |
207 | | |
208 | 6 | for (i = 0; config->hosts[i] != NULL; ++i) { |
209 | 3 | h2o_hostconf_t *hostconf = config->hosts[i]; |
210 | 11 | for (j = 0; j != hostconf->paths.size; ++j) { |
211 | 8 | h2o_pathconf_t *pathconf = hostconf->paths.entries[j]; |
212 | 8 | h2o_context_init_pathconf_context(ctx, pathconf); |
213 | 8 | } |
214 | 3 | h2o_context_init_pathconf_context(ctx, &hostconf->fallback_path); |
215 | 3 | } |
216 | | |
217 | 3 | pthread_mutex_unlock(&mutex); |
218 | 3 | } |
219 | | |
220 | | void h2o_context_dispose(h2o_context_t *ctx) |
221 | 0 | { |
222 | 0 | h2o_globalconf_t *config = ctx->globalconf; |
223 | |
|
224 | 0 | h2o_socketpool_unregister_loop(&ctx->globalconf->proxy.global_socketpool, ctx->loop); |
225 | |
|
226 | 0 | for (size_t i = 0; config->hosts[i] != NULL; ++i) { |
227 | 0 | h2o_hostconf_t *hostconf = config->hosts[i]; |
228 | 0 | for (size_t j = 0; j != hostconf->paths.size; ++j) { |
229 | 0 | h2o_pathconf_t *pathconf = hostconf->paths.entries[j]; |
230 | 0 | h2o_context_dispose_pathconf_context(ctx, pathconf); |
231 | 0 | } |
232 | 0 | h2o_context_dispose_pathconf_context(ctx, &hostconf->fallback_path); |
233 | 0 | } |
234 | 0 | free(ctx->_pathconfs_inited.entries); |
235 | 0 | if (ctx->proxy.client_ctx.http3 != NULL) |
236 | 0 | h2o_destroy_proxy_http3_context(ctx->proxy.client_ctx.http3); |
237 | 0 | free(ctx->_module_configs); |
238 | | /* what should we do here? assert(!h2o_linklist_is_empty(&ctx->http2._conns); */ |
239 | |
|
240 | 0 | for (size_t i = 0; i < ctx->spare_pipes.count; ++i) { |
241 | 0 | close(ctx->spare_pipes.pipes[i][0]); |
242 | 0 | close(ctx->spare_pipes.pipes[i][1]); |
243 | 0 | } |
244 | 0 | free(ctx->spare_pipes.pipes); |
245 | |
|
246 | 0 | h2o_filecache_destroy(ctx->filecache); |
247 | 0 | ctx->filecache = NULL; |
248 | | |
249 | | /* clear storage */ |
250 | 0 | for (size_t i = 0; i != ctx->storage.size; ++i) { |
251 | 0 | h2o_context_storage_item_t *item = ctx->storage.entries + i; |
252 | 0 | if (item->dispose != NULL) { |
253 | 0 | item->dispose(item->data); |
254 | 0 | } |
255 | 0 | } |
256 | 0 | free(ctx->storage.entries); |
257 | | |
258 | | /* TODO assert that the all the getaddrinfo threads are idle */ |
259 | 0 | h2o_multithread_unregister_receiver(ctx->queue, &ctx->receivers.hostinfo_getaddr); |
260 | 0 | h2o_multithread_destroy_queue(ctx->queue); |
261 | |
|
262 | 0 | if (ctx->_timestamp_cache.value != NULL) |
263 | 0 | h2o_mem_release_shared(ctx->_timestamp_cache.value); |
264 | 0 | } |
265 | | |
266 | | void h2o_context_request_shutdown(h2o_context_t *ctx) |
267 | 0 | { |
268 | 0 | ctx->shutdown_requested = 1; |
269 | |
|
270 | 0 | H2O_CONN_LIST_FOREACH(h2o_conn_t * conn, ({&ctx->_conns.active, &ctx->_conns.idle}), { |
271 | 0 | if (conn->callbacks->request_shutdown != NULL) { |
272 | 0 | conn->callbacks->request_shutdown(conn); |
273 | 0 | } |
274 | 0 | }); |
275 | 0 | } |
276 | | |
277 | | void h2o_context_update_timestamp_string_cache(h2o_context_t *ctx) |
278 | 254 | { |
279 | 254 | struct tm gmt; |
280 | 254 | if (ctx->_timestamp_cache.value != NULL) |
281 | 251 | h2o_mem_release_shared(ctx->_timestamp_cache.value); |
282 | 254 | ctx->_timestamp_cache.value = h2o_mem_alloc_shared(NULL, sizeof(h2o_timestamp_string_t), NULL); |
283 | 254 | gmtime_r(&ctx->_timestamp_cache.tv_at.tv_sec, &gmt); |
284 | 254 | h2o_time2str_rfc1123(ctx->_timestamp_cache.value->rfc1123, &gmt); |
285 | 254 | h2o_time2str_log(ctx->_timestamp_cache.value->log, ctx->_timestamp_cache.tv_at.tv_sec); |
286 | 254 | } |
287 | | |
288 | | void h2o_context_close_idle_connections(h2o_context_t *ctx, size_t max_connections_to_close, uint64_t min_age) |
289 | 0 | { |
290 | 0 | if (max_connections_to_close <= 0) |
291 | 0 | return; |
292 | | |
293 | 0 | size_t closed = ctx->_conns.num_conns.shutdown; |
294 | |
|
295 | 0 | if (closed >= max_connections_to_close) |
296 | 0 | return; |
297 | | |
298 | 0 | H2O_CONN_LIST_FOREACH(h2o_conn_t * conn, ({&ctx->_conns.idle}), { |
299 | 0 | struct timeval now = h2o_gettimeofday(ctx->loop); |
300 | 0 | if (h2o_timeval_subtract(&conn->connected_at, &now) < (min_age * 1000)) |
301 | 0 | continue; |
302 | 0 | ctx->connection_stats.idle_closed++; |
303 | 0 | conn->callbacks->close_idle_connection(conn); |
304 | 0 | closed++; |
305 | 0 | if (closed == max_connections_to_close) |
306 | 0 | return; |
307 | 0 | }); |
308 | 0 | } |
309 | | |
310 | | static size_t *get_connection_state_counter(h2o_context_t *ctx, h2o_conn_state_t state) |
311 | 104k | { |
312 | 104k | return ctx->_conns.num_conns.counters + (size_t)state; |
313 | 104k | } |
314 | | |
315 | | static void unlink_conn(h2o_conn_t *conn) |
316 | 52.0k | { |
317 | 52.0k | --*get_connection_state_counter(conn->ctx, conn->state); |
318 | 52.0k | h2o_linklist_unlink(&conn->_conns); |
319 | 52.0k | } |
320 | | |
321 | | static void link_conn(h2o_conn_t *conn) |
322 | 52.0k | { |
323 | 52.0k | switch (conn->state) { |
324 | 20.2k | case H2O_CONN_STATE_IDLE: |
325 | 20.2k | h2o_linklist_insert(&conn->ctx->_conns.idle, &conn->_conns); |
326 | 20.2k | break; |
327 | 31.7k | case H2O_CONN_STATE_ACTIVE: |
328 | 31.7k | h2o_linklist_insert(&conn->ctx->_conns.active, &conn->_conns); |
329 | 31.7k | break; |
330 | 0 | case H2O_CONN_STATE_SHUTDOWN: |
331 | 0 | h2o_linklist_insert(&conn->ctx->_conns.shutdown, &conn->_conns); |
332 | 0 | break; |
333 | 52.0k | } |
334 | 52.0k | ++*get_connection_state_counter(conn->ctx, conn->state); |
335 | 52.0k | } |
336 | | |
337 | | h2o_conn_t *h2o_create_connection(size_t sz, h2o_context_t *ctx, h2o_hostconf_t **hosts, struct timeval connected_at, |
338 | | const h2o_conn_callbacks_t *callbacks) |
339 | 29.0k | { |
340 | 29.0k | h2o_conn_t *conn = (h2o_conn_t *)h2o_mem_alloc(sz); |
341 | | |
342 | 29.0k | conn->ctx = ctx; |
343 | 29.0k | conn->hosts = hosts; |
344 | 29.0k | conn->connected_at = connected_at; |
345 | | #ifdef H2O_NO_64BIT_ATOMICS |
346 | | pthread_mutex_lock(&h2o_conn_id_mutex); |
347 | | conn->id = ++h2o_connection_id; |
348 | | pthread_mutex_unlock(&h2o_conn_id_mutex); |
349 | | #else |
350 | 29.0k | conn->id = __sync_add_and_fetch(&h2o_connection_id, 1); |
351 | 29.0k | #endif |
352 | 29.0k | conn->callbacks = callbacks; |
353 | 29.0k | conn->_uuid.is_initialized = 0; |
354 | | |
355 | 29.0k | conn->state = H2O_CONN_STATE_ACTIVE; |
356 | 29.0k | conn->_conns = (h2o_linklist_t){}; |
357 | 29.0k | link_conn(conn); |
358 | | |
359 | 29.0k | return conn; |
360 | 29.0k | } |
361 | | |
362 | | void h2o_destroy_connection(h2o_conn_t *conn) |
363 | 29.0k | { |
364 | 29.0k | unlink_conn(conn); |
365 | 29.0k | free(conn); |
366 | 29.0k | } |
367 | | |
368 | | void h2o_conn_set_state(h2o_conn_t *conn, h2o_conn_state_t state) |
369 | 153k | { |
370 | 153k | if (conn->state != state) { |
371 | 22.9k | unlink_conn(conn); |
372 | 22.9k | conn->state = state; |
373 | 22.9k | link_conn(conn); |
374 | 22.9k | } |
375 | 153k | } |