/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_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 | uint8_t cid_key[PTLS_SHA256_DIGEST_SIZE]; |
66 | 0 | ptls_openssl_random_bytes(cid_key, sizeof(cid_key)); |
67 | 0 | h3ctx->quic.cid_encryptor = quicly_new_default_cid_encryptor( |
68 | | #if H2O_USE_FUSION |
69 | | ptls_fusion_is_supported_by_cpu() ? &ptls_fusion_quiclb : |
70 | | #endif |
71 | 0 | &ptls_openssl_quiclb, |
72 | 0 | &ptls_openssl_aes128ecb, &ptls_openssl_sha256, ptls_iovec_init(cid_key, sizeof(cid_key))); |
73 | 0 | ptls_clear_memory(cid_key, sizeof(cid_key)); |
74 | 0 | h3ctx->quic.stream_open = &h2o_httpclient_http3_on_stream_open; |
75 | | |
76 | | /* http3 client-specific fields */ |
77 | 0 | h3ctx->max_frame_payload_size = h2o_http3_calc_min_flow_control_size(H2O_MAX_REQLEN); /* same maximum for HEADERS frame in both |
78 | | directions */ |
79 | | |
80 | | /* h2o server http3 integration */ |
81 | 0 | h2o_socket_t *socks[2], **sp = socks; |
82 | 0 | if ((*sp = h2o_quic_create_client_socket(ctx->loop, AF_INET)) != NULL) |
83 | 0 | ++sp; |
84 | 0 | if ((*sp = h2o_quic_create_client_socket(ctx->loop, AF_INET6)) != NULL) |
85 | 0 | ++sp; |
86 | 0 | if (sp == socks) { |
87 | 0 | char buf[256]; |
88 | 0 | h2o_fatal("failed to create UDP socket for both IPv4 and v6: %s", h2o_strerror_r(errno, buf, sizeof(buf))); |
89 | 0 | } |
90 | 0 | h2o_http3_server_init_context(ctx, &h3ctx->h3, ctx->loop, socks[0], socks[1], &h3ctx->quic, &ctx->http3.next_cid, NULL, |
91 | 0 | h2o_httpclient_http3_notify_connection_update, use_gso); |
92 | |
|
93 | 0 | h3ctx->load_session = NULL; /* TODO reuse session? */ |
94 | |
|
95 | 0 | return h3ctx; |
96 | 0 | #endif |
97 | 0 | } |
98 | | |
99 | | void h2o_destroy_proxy_http3_context(h2o_http3client_ctx_t *h3ctx) |
100 | 0 | { |
101 | 0 | h2o_quic_dispose_context(&h3ctx->h3); |
102 | 0 | quicly_free_default_cid_encryptor(h3ctx->quic.cid_encryptor); |
103 | 0 | if (h3ctx->verify_cert.super.cb != NULL) |
104 | 0 | ptls_openssl_dispose_verify_certificate(&h3ctx->verify_cert); |
105 | 0 | free(h3ctx); |
106 | 0 | } |
107 | | |
108 | | void h2o_context_init_pathconf_context(h2o_context_t *ctx, h2o_pathconf_t *pathconf) |
109 | 11 | { |
110 | | /* add pathconf to the inited list (or return if already inited) */ |
111 | 11 | size_t i; |
112 | 26 | for (i = 0; i != ctx->_pathconfs_inited.size; ++i) |
113 | 15 | if (ctx->_pathconfs_inited.entries[i] == pathconf) |
114 | 0 | return; |
115 | 11 | h2o_vector_reserve(NULL, &ctx->_pathconfs_inited, ctx->_pathconfs_inited.size + 1); |
116 | 11 | ctx->_pathconfs_inited.entries[ctx->_pathconfs_inited.size++] = pathconf; |
117 | | |
118 | 11 | #define DOIT(type, list) \ |
119 | 33 | do { \ |
120 | 33 | size_t i; \ |
121 | 41 | for (i = 0; i != pathconf->list.size; ++i) { \ |
122 | 8 | type *o = pathconf->list.entries[i]; \ |
123 | 8 | if (o->on_context_init != NULL) \ |
124 | 8 | o->on_context_init(o, ctx); \ |
125 | 8 | } \ |
126 | 33 | } while (0) |
127 | | |
128 | 11 | DOIT(h2o_handler_t, handlers); |
129 | 11 | DOIT(h2o_filter_t, _filters); |
130 | 11 | DOIT(h2o_logger_t, _loggers); |
131 | | |
132 | 11 | #undef DOIT |
133 | 11 | } |
134 | | |
135 | | void h2o_context_dispose_pathconf_context(h2o_context_t *ctx, h2o_pathconf_t *pathconf) |
136 | 0 | { |
137 | | /* nullify pathconf in the inited list (or return if already disposed) */ |
138 | 0 | size_t i; |
139 | 0 | for (i = 0; i != ctx->_pathconfs_inited.size; ++i) |
140 | 0 | if (ctx->_pathconfs_inited.entries[i] == pathconf) |
141 | 0 | break; |
142 | 0 | if (i == ctx->_pathconfs_inited.size) |
143 | 0 | return; |
144 | 0 | ctx->_pathconfs_inited.entries[i] = NULL; |
145 | |
|
146 | 0 | #define DOIT(type, list) \ |
147 | 0 | do { \ |
148 | 0 | size_t i; \ |
149 | 0 | for (i = 0; i != pathconf->list.size; ++i) { \ |
150 | 0 | type *o = pathconf->list.entries[i]; \ |
151 | 0 | if (o->on_context_dispose != NULL) \ |
152 | 0 | o->on_context_dispose(o, ctx); \ |
153 | 0 | } \ |
154 | 0 | } while (0) |
155 | |
|
156 | 0 | DOIT(h2o_handler_t, handlers); |
157 | 0 | DOIT(h2o_filter_t, _filters); |
158 | 0 | DOIT(h2o_logger_t, _loggers); |
159 | |
|
160 | 0 | #undef DOIT |
161 | 0 | } |
162 | | |
163 | | void h2o_context_init(h2o_context_t *ctx, h2o_loop_t *loop, h2o_globalconf_t *config) |
164 | 3 | { |
165 | 3 | size_t i, j; |
166 | | |
167 | 3 | assert(config->hosts[0] != NULL); |
168 | | |
169 | 3 | memset(ctx, 0, sizeof(*ctx)); |
170 | 3 | ctx->loop = loop; |
171 | 3 | ctx->globalconf = config; |
172 | 3 | ctx->queue = h2o_multithread_create_queue(loop); |
173 | 3 | h2o_multithread_register_receiver(ctx->queue, &ctx->receivers.hostinfo_getaddr, h2o_hostinfo_getaddr_receiver); |
174 | 3 | ctx->filecache = h2o_filecache_create(config->filecache.capacity); |
175 | 3 | ctx->spare_pipes.pipes = h2o_mem_alloc(sizeof(ctx->spare_pipes.pipes[0]) * config->max_spare_pipes); |
176 | | |
177 | 3 | h2o_linklist_init_anchor(&ctx->_conns.active); |
178 | 3 | h2o_linklist_init_anchor(&ctx->_conns.idle); |
179 | 3 | h2o_linklist_init_anchor(&ctx->_conns.shutdown); |
180 | 3 | ctx->proxy.client_ctx.loop = loop; |
181 | 3 | ctx->proxy.client_ctx.io_timeout = ctx->globalconf->proxy.io_timeout; |
182 | 3 | ctx->proxy.client_ctx.connect_timeout = ctx->globalconf->proxy.connect_timeout; |
183 | 3 | ctx->proxy.client_ctx.first_byte_timeout = ctx->globalconf->proxy.first_byte_timeout; |
184 | 3 | ctx->proxy.client_ctx.keepalive_timeout = ctx->globalconf->proxy.keepalive_timeout; |
185 | 3 | ctx->proxy.client_ctx.getaddr_receiver = &ctx->receivers.hostinfo_getaddr; |
186 | 3 | ctx->proxy.client_ctx.http2.latency_optimization = ctx->globalconf->http2.latency_optimization; |
187 | 3 | ctx->proxy.client_ctx.max_buffer_size = ctx->globalconf->proxy.max_buffer_size; |
188 | 3 | ctx->proxy.client_ctx.http2.max_concurrent_streams = ctx->globalconf->proxy.http2.max_concurrent_streams; |
189 | 3 | ctx->proxy.client_ctx.protocol_selector.ratio.http2 = ctx->globalconf->proxy.protocol_ratio.http2; |
190 | 3 | ctx->proxy.client_ctx.protocol_selector.ratio.http3 = ctx->globalconf->proxy.protocol_ratio.http3; |
191 | 3 | if (ctx->globalconf->proxy.protocol_ratio.http3 != 0) |
192 | 0 | ctx->proxy.client_ctx.http3 = h2o_create_proxy_http3_context( |
193 | 0 | ctx, ctx->globalconf->proxy.global_socketpool._ssl_ctx, ctx->globalconf->http3.use_gso); |
194 | 3 | ctx->proxy.connpool.socketpool = &ctx->globalconf->proxy.global_socketpool; |
195 | 3 | h2o_linklist_init_anchor(&ctx->proxy.connpool.http2.conns); |
196 | | |
197 | 3 | ctx->_module_configs = h2o_mem_alloc(sizeof(*ctx->_module_configs) * config->_num_config_slots); |
198 | 3 | memset(ctx->_module_configs, 0, sizeof(*ctx->_module_configs) * config->_num_config_slots); |
199 | | |
200 | 3 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
201 | 3 | pthread_mutex_lock(&mutex); |
202 | | |
203 | 3 | h2o_socketpool_register_loop(&ctx->globalconf->proxy.global_socketpool, loop); |
204 | | |
205 | 6 | for (i = 0; config->hosts[i] != NULL; ++i) { |
206 | 3 | h2o_hostconf_t *hostconf = config->hosts[i]; |
207 | 11 | for (j = 0; j != hostconf->paths.size; ++j) { |
208 | 8 | h2o_pathconf_t *pathconf = hostconf->paths.entries[j]; |
209 | 8 | h2o_context_init_pathconf_context(ctx, pathconf); |
210 | 8 | } |
211 | 3 | h2o_context_init_pathconf_context(ctx, &hostconf->fallback_path); |
212 | 3 | } |
213 | | |
214 | 3 | pthread_mutex_unlock(&mutex); |
215 | 3 | } |
216 | | |
217 | | void h2o_context_dispose(h2o_context_t *ctx) |
218 | 0 | { |
219 | 0 | h2o_globalconf_t *config = ctx->globalconf; |
220 | |
|
221 | 0 | h2o_socketpool_unregister_loop(&ctx->globalconf->proxy.global_socketpool, ctx->loop); |
222 | |
|
223 | 0 | for (size_t i = 0; config->hosts[i] != NULL; ++i) { |
224 | 0 | h2o_hostconf_t *hostconf = config->hosts[i]; |
225 | 0 | for (size_t j = 0; j != hostconf->paths.size; ++j) { |
226 | 0 | h2o_pathconf_t *pathconf = hostconf->paths.entries[j]; |
227 | 0 | h2o_context_dispose_pathconf_context(ctx, pathconf); |
228 | 0 | } |
229 | 0 | h2o_context_dispose_pathconf_context(ctx, &hostconf->fallback_path); |
230 | 0 | } |
231 | 0 | free(ctx->_pathconfs_inited.entries); |
232 | 0 | if (ctx->proxy.client_ctx.http3 != NULL) |
233 | 0 | h2o_destroy_proxy_http3_context(ctx->proxy.client_ctx.http3); |
234 | 0 | free(ctx->_module_configs); |
235 | | /* what should we do here? assert(!h2o_linklist_is_empty(&ctx->http2._conns); */ |
236 | |
|
237 | 0 | for (size_t i = 0; i < ctx->spare_pipes.count; ++i) { |
238 | 0 | close(ctx->spare_pipes.pipes[i][0]); |
239 | 0 | close(ctx->spare_pipes.pipes[i][1]); |
240 | 0 | } |
241 | 0 | free(ctx->spare_pipes.pipes); |
242 | |
|
243 | 0 | h2o_filecache_destroy(ctx->filecache); |
244 | 0 | ctx->filecache = NULL; |
245 | | |
246 | | /* clear storage */ |
247 | 0 | for (size_t i = 0; i != ctx->storage.size; ++i) { |
248 | 0 | h2o_context_storage_item_t *item = ctx->storage.entries + i; |
249 | 0 | if (item->dispose != NULL) { |
250 | 0 | item->dispose(item->data); |
251 | 0 | } |
252 | 0 | } |
253 | 0 | free(ctx->storage.entries); |
254 | | |
255 | | /* TODO assert that the all the getaddrinfo threads are idle */ |
256 | 0 | h2o_multithread_unregister_receiver(ctx->queue, &ctx->receivers.hostinfo_getaddr); |
257 | 0 | h2o_multithread_destroy_queue(ctx->queue); |
258 | |
|
259 | 0 | if (ctx->_timestamp_cache.value != NULL) |
260 | 0 | h2o_mem_release_shared(ctx->_timestamp_cache.value); |
261 | 0 | } |
262 | | |
263 | | void h2o_context_request_shutdown(h2o_context_t *ctx) |
264 | 0 | { |
265 | 0 | ctx->shutdown_requested = 1; |
266 | |
|
267 | 0 | H2O_CONN_LIST_FOREACH(h2o_conn_t * conn, ({&ctx->_conns.active, &ctx->_conns.idle}), { |
268 | 0 | if (conn->callbacks->request_shutdown != NULL) { |
269 | 0 | conn->callbacks->request_shutdown(conn); |
270 | 0 | } |
271 | 0 | }); |
272 | 0 | } |
273 | | |
274 | | void h2o_context_update_timestamp_string_cache(h2o_context_t *ctx) |
275 | 250 | { |
276 | 250 | struct tm gmt; |
277 | 250 | if (ctx->_timestamp_cache.value != NULL) |
278 | 247 | h2o_mem_release_shared(ctx->_timestamp_cache.value); |
279 | 250 | ctx->_timestamp_cache.value = h2o_mem_alloc_shared(NULL, sizeof(h2o_timestamp_string_t), NULL); |
280 | 250 | gmtime_r(&ctx->_timestamp_cache.tv_at.tv_sec, &gmt); |
281 | 250 | h2o_time2str_rfc1123(ctx->_timestamp_cache.value->rfc1123, &gmt); |
282 | 250 | h2o_time2str_log(ctx->_timestamp_cache.value->log, ctx->_timestamp_cache.tv_at.tv_sec); |
283 | 250 | } |
284 | | |
285 | | void h2o_context_close_idle_connections(h2o_context_t *ctx, size_t max_connections_to_close, uint64_t min_age) |
286 | 0 | { |
287 | 0 | if (max_connections_to_close <= 0) |
288 | 0 | return; |
289 | | |
290 | 0 | size_t closed = ctx->_conns.num_conns.shutdown; |
291 | |
|
292 | 0 | if (closed >= max_connections_to_close) |
293 | 0 | return; |
294 | | |
295 | 0 | H2O_CONN_LIST_FOREACH(h2o_conn_t * conn, ({&ctx->_conns.idle}), { |
296 | 0 | struct timeval now = h2o_gettimeofday(ctx->loop); |
297 | 0 | if (h2o_timeval_subtract(&conn->connected_at, &now) < (min_age * 1000)) |
298 | 0 | continue; |
299 | 0 | ctx->connection_stats.idle_closed++; |
300 | 0 | conn->callbacks->close_idle_connection(conn); |
301 | 0 | closed++; |
302 | 0 | if (closed == max_connections_to_close) |
303 | 0 | return; |
304 | 0 | }); |
305 | 0 | } |
306 | | |
307 | | static size_t *get_connection_state_counter(h2o_context_t *ctx, h2o_conn_state_t state) |
308 | 101k | { |
309 | 101k | return ctx->_conns.num_conns.counters + (size_t)state; |
310 | 101k | } |
311 | | |
312 | | static void unlink_conn(h2o_conn_t *conn) |
313 | 50.8k | { |
314 | 50.8k | --*get_connection_state_counter(conn->ctx, conn->state); |
315 | 50.8k | h2o_linklist_unlink(&conn->_conns); |
316 | 50.8k | } |
317 | | |
318 | | static void link_conn(h2o_conn_t *conn) |
319 | 50.8k | { |
320 | 50.8k | switch (conn->state) { |
321 | 19.5k | case H2O_CONN_STATE_IDLE: |
322 | 19.5k | h2o_linklist_insert(&conn->ctx->_conns.idle, &conn->_conns); |
323 | 19.5k | break; |
324 | 31.2k | case H2O_CONN_STATE_ACTIVE: |
325 | 31.2k | h2o_linklist_insert(&conn->ctx->_conns.active, &conn->_conns); |
326 | 31.2k | break; |
327 | 0 | case H2O_CONN_STATE_SHUTDOWN: |
328 | 0 | h2o_linklist_insert(&conn->ctx->_conns.shutdown, &conn->_conns); |
329 | 0 | break; |
330 | 50.8k | } |
331 | 50.8k | ++*get_connection_state_counter(conn->ctx, conn->state); |
332 | 50.8k | } |
333 | | |
334 | | h2o_conn_t *h2o_create_connection(size_t sz, h2o_context_t *ctx, h2o_hostconf_t **hosts, struct timeval connected_at, |
335 | | const h2o_conn_callbacks_t *callbacks) |
336 | 28.7k | { |
337 | 28.7k | h2o_conn_t *conn = (h2o_conn_t *)h2o_mem_alloc(sz); |
338 | | |
339 | 28.7k | conn->ctx = ctx; |
340 | 28.7k | conn->hosts = hosts; |
341 | 28.7k | conn->connected_at = connected_at; |
342 | | #ifdef H2O_NO_64BIT_ATOMICS |
343 | | pthread_mutex_lock(&h2o_conn_id_mutex); |
344 | | conn->id = ++h2o_connection_id; |
345 | | pthread_mutex_unlock(&h2o_conn_id_mutex); |
346 | | #else |
347 | 28.7k | conn->id = __sync_add_and_fetch(&h2o_connection_id, 1); |
348 | 28.7k | #endif |
349 | 28.7k | conn->callbacks = callbacks; |
350 | 28.7k | conn->_uuid.is_initialized = 0; |
351 | | |
352 | 28.7k | conn->state = H2O_CONN_STATE_ACTIVE; |
353 | 28.7k | conn->_conns = (h2o_linklist_t){}; |
354 | 28.7k | link_conn(conn); |
355 | | |
356 | 28.7k | return conn; |
357 | 28.7k | } |
358 | | |
359 | | void h2o_destroy_connection(h2o_conn_t *conn) |
360 | 28.7k | { |
361 | 28.7k | unlink_conn(conn); |
362 | 28.7k | free(conn); |
363 | 28.7k | } |
364 | | |
365 | | void h2o_conn_set_state(h2o_conn_t *conn, h2o_conn_state_t state) |
366 | 148k | { |
367 | 148k | if (conn->state != state) { |
368 | 22.0k | unlink_conn(conn); |
369 | 22.0k | conn->state = state; |
370 | 22.0k | link_conn(conn); |
371 | 22.0k | } |
372 | 148k | } |