File: | lwan-thread.c |
Warning: | line 1290, column 35 Array subscript is undefined |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * lwan - simple web server | |||
3 | * Copyright (c) 2012, 2013 L. A. F. Pereira <l@tia.mat.br> | |||
4 | * | |||
5 | * This program is free software; you can redistribute it and/or | |||
6 | * modify it under the terms of the GNU General Public License | |||
7 | * as published by the Free Software Foundation; either version 2 | |||
8 | * of the License, or any later version. | |||
9 | * | |||
10 | * This program is distributed in the hope that it will be useful, | |||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
13 | * GNU General Public License for more details. | |||
14 | * | |||
15 | * You should have received a copy of the GNU General Public License | |||
16 | * along with this program; if not, write to the Free Software | |||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | |||
18 | * USA. | |||
19 | */ | |||
20 | ||||
21 | #define _GNU_SOURCE | |||
22 | #include <assert.h> | |||
23 | #include <errno(*__errno_location ()).h> | |||
24 | #include <fcntl.h> | |||
25 | #include <pthread.h> | |||
26 | #include <sched.h> | |||
27 | #include <stdlib.h> | |||
28 | #include <string.h> | |||
29 | #include <sys/epoll.h> | |||
30 | #include <sys/ioctl.h> | |||
31 | #include <sys/socket.h> | |||
32 | #include <unistd.h> | |||
33 | ||||
34 | #if defined(HAVE_SO_ATTACH_REUSEPORT_CBPF) | |||
35 | #include <linux1/filter.h> | |||
36 | #endif | |||
37 | ||||
38 | #if defined(HAVE_MBEDTLS) | |||
39 | #include <mbedtls/entropy.h> | |||
40 | #include <mbedtls/error.h> | |||
41 | #include <mbedtls/gcm.h> | |||
42 | #include <mbedtls/net_sockets.h> | |||
43 | #include <mbedtls/ssl_internal.h> | |||
44 | ||||
45 | #include <linux1/tls.h> | |||
46 | #include <netinet/tcp.h> | |||
47 | #endif | |||
48 | ||||
49 | #include "list.h" | |||
50 | #include "murmur3.h" | |||
51 | #include "lwan-private.h" | |||
52 | #include "lwan-tq.h" | |||
53 | ||||
54 | static void lwan_strbuf_free_defer(void *data) | |||
55 | { | |||
56 | return lwan_strbuf_free((struct lwan_strbuf *)data); | |||
57 | } | |||
58 | ||||
59 | static void graceful_close(struct lwan *l, | |||
60 | struct lwan_connection *conn, | |||
61 | char buffer[static DEFAULT_BUFFER_SIZE4096]) | |||
62 | { | |||
63 | int fd = lwan_connection_get_fd(l, conn); | |||
64 | ||||
65 | while (TIOCOUTQ0x5411) { | |||
66 | /* This ioctl isn't probably doing what it says on the tin; the details | |||
67 | * are subtle, but it seems to do the trick to allow gracefully closing | |||
68 | * the connection in some cases with minimal system calls. */ | |||
69 | int bytes_waiting; | |||
70 | int r = ioctl(fd, TIOCOUTQ0x5411, &bytes_waiting); | |||
71 | ||||
72 | if (!r && !bytes_waiting) /* See note about close(2) below. */ | |||
73 | return; | |||
74 | if (r < 0 && errno(*__errno_location ()) == EINTR4) | |||
75 | continue; | |||
76 | ||||
77 | break; | |||
78 | } | |||
79 | ||||
80 | if (UNLIKELY(shutdown(fd, SHUT_WR) < 0)__builtin_expect(((shutdown(fd, SHUT_WR) < 0)), (0))) { | |||
81 | if (UNLIKELY(errno == ENOTCONN)__builtin_expect((((*__errno_location ()) == 107)), (0))) | |||
82 | return; | |||
83 | } | |||
84 | ||||
85 | for (int tries = 0; tries < 20; tries++) { | |||
86 | ssize_t r = recv(fd, buffer, DEFAULT_BUFFER_SIZE4096, 0); | |||
87 | ||||
88 | if (!r) | |||
89 | break; | |||
90 | ||||
91 | if (r < 0) { | |||
92 | switch (errno(*__errno_location ())) { | |||
93 | case EAGAIN11: | |||
94 | break; | |||
95 | case EINTR4: | |||
96 | continue; | |||
97 | default: | |||
98 | return; | |||
99 | } | |||
100 | } | |||
101 | ||||
102 | coro_yield(conn->coro, CONN_CORO_WANT_READ); | |||
103 | } | |||
104 | ||||
105 | /* close(2) will be called when the coroutine yields with CONN_CORO_ABORT */ | |||
106 | } | |||
107 | ||||
108 | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) | |||
109 | static void lwan_random_seed_prng_for_thread(const struct lwan_thread *t) | |||
110 | { | |||
111 | (void)t; | |||
112 | } | |||
113 | ||||
114 | uint64_t lwan_random_uint64() | |||
115 | { | |||
116 | static uint64_t value; | |||
117 | ||||
118 | return ATOMIC_INC(value)(__sync_add_and_fetch(((&(value))), ((1)))); | |||
119 | } | |||
120 | #else | |||
121 | static __thread __uint128_t lehmer64_state; | |||
122 | ||||
123 | static void lwan_random_seed_prng_for_thread(const struct lwan_thread *t) | |||
124 | { | |||
125 | if (lwan_getentropy(&lehmer64_state, sizeof(lehmer64_state), 0) < 0) { | |||
126 | lwan_status_warning("Couldn't get proper entropy for PRNG, using fallback seed")lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 126, __FUNCTION__, "Couldn't get proper entropy for PRNG, using fallback seed" ); | |||
127 | lehmer64_state |= murmur3_fmix64((uint64_t)(uintptr_t)t); | |||
128 | lehmer64_state <<= 64; | |||
129 | lehmer64_state |= murmur3_fmix64((uint64_t)t->epoll_fd); | |||
130 | } | |||
131 | } | |||
132 | ||||
133 | uint64_t lwan_random_uint64() | |||
134 | { | |||
135 | /* https://lemire.me/blog/2019/03/19/the-fastest-conventional-random-number-generator-that-can-pass-big-crush/ */ | |||
136 | lehmer64_state *= 0xda942042e4dd58b5ull; | |||
137 | return (uint64_t)(lehmer64_state >> 64); | |||
138 | } | |||
139 | #endif | |||
140 | ||||
141 | uint64_t lwan_request_get_id(struct lwan_request *request) | |||
142 | { | |||
143 | struct lwan_request_parser_helper *helper = request->helper; | |||
144 | ||||
145 | if (helper->request_id == 0) | |||
146 | helper->request_id = lwan_random_uint64(); | |||
147 | ||||
148 | return helper->request_id; | |||
149 | } | |||
150 | ||||
151 | #if defined(HAVE_MBEDTLS) | |||
152 | static bool_Bool | |||
153 | lwan_setup_tls_keys(int fd, const mbedtls_ssl_context *ssl, int rx_or_tx) | |||
154 | { | |||
155 | struct tls12_crypto_info_aes_gcm_128 info = { | |||
156 | .info = {.version = TLS_1_2_VERSION((((0x3) & 0xFF) << 8) | ((0x3) & 0xFF)), | |||
157 | .cipher_type = TLS_CIPHER_AES_GCM_12851}, | |||
158 | }; | |||
159 | const unsigned char *salt, *iv, *rec_seq; | |||
160 | const mbedtls_gcm_context *gcm_ctx; | |||
161 | const mbedtls_aes_context *aes_ctx; | |||
162 | ||||
163 | switch (rx_or_tx) { | |||
164 | case TLS_RX2: | |||
165 | salt = ssl->transform->iv_dec; | |||
166 | rec_seq = ssl->in_ctr; | |||
167 | gcm_ctx = ssl->transform->cipher_ctx_dec.cipher_ctx; | |||
168 | break; | |||
169 | case TLS_TX1: | |||
170 | salt = ssl->transform->iv_enc; | |||
171 | rec_seq = ssl->cur_out_ctr; | |||
172 | gcm_ctx = ssl->transform->cipher_ctx_enc.cipher_ctx; | |||
173 | break; | |||
174 | default: | |||
175 | __builtin_unreachable(); | |||
176 | } | |||
177 | ||||
178 | iv = salt + 4; | |||
179 | aes_ctx = gcm_ctx->cipher_ctx.cipher_ctx; | |||
180 | ||||
181 | memcpy(info.iv, iv, TLS_CIPHER_AES_GCM_128_IV_SIZE8); | |||
182 | memcpy(info.rec_seq, rec_seq, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE8); | |||
183 | memcpy(info.key, aes_ctx->rk, TLS_CIPHER_AES_GCM_128_KEY_SIZE16); | |||
184 | memcpy(info.salt, salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE4); | |||
185 | ||||
186 | if (UNLIKELY(setsockopt(fd, SOL_TLS, rx_or_tx, &info, sizeof(info)) < 0)__builtin_expect(((setsockopt(fd, 282, rx_or_tx, &info, sizeof (info)) < 0)), (0))) { | |||
187 | lwan_status_perror("Could not set %s kTLS keys for fd %d",lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 189, __FUNCTION__, "Could not set %s kTLS keys for fd %d", rx_or_tx == 1 ? "transmission" : "reception", fd) | |||
188 | rx_or_tx == TLS_TX ? "transmission" : "reception",lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 189, __FUNCTION__, "Could not set %s kTLS keys for fd %d", rx_or_tx == 1 ? "transmission" : "reception", fd) | |||
189 | fd)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 189, __FUNCTION__, "Could not set %s kTLS keys for fd %d", rx_or_tx == 1 ? "transmission" : "reception", fd); | |||
190 | lwan_always_bzero(&info, sizeof(info)); | |||
191 | return false0; | |||
192 | } | |||
193 | ||||
194 | lwan_always_bzero(&info, sizeof(info)); | |||
195 | return true1; | |||
196 | } | |||
197 | ||||
198 | __attribute__((format(printf, 2, 3))) | |||
199 | __attribute__((noinline, cold)) | |||
200 | static void lwan_status_mbedtls_error(int error_code, const char *fmt, ...) | |||
201 | { | |||
202 | char *formatted; | |||
203 | va_list ap; | |||
204 | int r; | |||
205 | ||||
206 | va_start(ap, fmt)__builtin_va_start(ap, fmt); | |||
207 | r = vasprintf(&formatted, fmt, ap); | |||
208 | if (r >= 0) { | |||
209 | char mbedtls_errbuf[128]; | |||
210 | ||||
211 | mbedtls_strerror(error_code, mbedtls_errbuf, sizeof(mbedtls_errbuf)); | |||
212 | lwan_status_error("%s: %s", formatted, mbedtls_errbuf)lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 212, __FUNCTION__, "%s: %s", formatted, mbedtls_errbuf); | |||
213 | free(formatted); | |||
214 | } | |||
215 | va_end(ap)__builtin_va_end(ap); | |||
216 | } | |||
217 | ||||
218 | static void lwan_setup_tls_free_ssl_context(void *data) | |||
219 | { | |||
220 | mbedtls_ssl_context *ssl = data; | |||
221 | ||||
222 | mbedtls_ssl_free(ssl); | |||
223 | } | |||
224 | ||||
225 | struct lwan_mbedtls_handshake_ctx { | |||
226 | int fd; | |||
227 | bool_Bool last_was_send; | |||
228 | }; | |||
229 | ||||
230 | static int lwan_mbedtls_send(void *ctx, const unsigned char *buf, size_t len) | |||
231 | { | |||
232 | struct lwan_mbedtls_handshake_ctx *hs_ctx = ctx; | |||
233 | ssize_t r; | |||
234 | ||||
235 | /* We use MSG_MORE -- flushing when we transition from send() to recv() | |||
236 | * -- rather than buffering on our side because this contains key | |||
237 | * material that we would need to only copy, but also zero out after | |||
238 | * finishing the handshake. */ | |||
239 | ||||
240 | r = send(hs_ctx->fd, buf, len, MSG_MOREMSG_MORE); | |||
241 | if (UNLIKELY(r < 0)__builtin_expect(((r < 0)), (0))) { | |||
242 | switch (errno(*__errno_location ())) { | |||
243 | case EINTR4: | |||
244 | case EAGAIN11: | |||
245 | return MBEDTLS_ERR_SSL_WANT_WRITE-0x6880; | |||
246 | ||||
247 | default: | |||
248 | /* It's not an internal error here, but this seemed the least | |||
249 | * innapropriate error code for this situation. lwan_setup_tls() | |||
250 | * doesn't care. */ | |||
251 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR-0x6C00; | |||
252 | } | |||
253 | } | |||
254 | ||||
255 | if (UNLIKELY((ssize_t)(int)r != r)__builtin_expect((((ssize_t)(int)r != r)), (0))) | |||
256 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR-0x6C00; | |||
257 | ||||
258 | hs_ctx->last_was_send = true1; | |||
259 | return (int)r; | |||
260 | } | |||
261 | ||||
262 | static void flush_pending_output(int fd) | |||
263 | { | |||
264 | int zero = 0; | |||
265 | setsockopt(fd, SOL_TCP6, TCP_CORK3, &zero, sizeof(zero)); | |||
266 | } | |||
267 | ||||
268 | static int lwan_mbedtls_recv(void *ctx, unsigned char *buf, size_t len) | |||
269 | { | |||
270 | struct lwan_mbedtls_handshake_ctx *hs_ctx = ctx; | |||
271 | ssize_t r; | |||
272 | ||||
273 | if (hs_ctx->last_was_send) | |||
274 | flush_pending_output(hs_ctx->fd); | |||
275 | ||||
276 | r = recv(hs_ctx->fd, buf, len, 0); | |||
277 | if (UNLIKELY(r < 0)__builtin_expect(((r < 0)), (0))) { | |||
278 | switch (errno(*__errno_location ())) { | |||
279 | case EINTR4: | |||
280 | case EAGAIN11: | |||
281 | return MBEDTLS_ERR_SSL_WANT_READ-0x6900; | |||
282 | ||||
283 | default: | |||
284 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR-0x6C00; | |||
285 | } | |||
286 | } | |||
287 | ||||
288 | if (UNLIKELY((ssize_t)(int)r != r)__builtin_expect((((ssize_t)(int)r != r)), (0))) | |||
289 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR-0x6C00; | |||
290 | ||||
291 | hs_ctx->last_was_send = false0; | |||
292 | return (int)r; | |||
293 | } | |||
294 | ||||
295 | static bool_Bool lwan_setup_tls(const struct lwan *l, struct lwan_connection *conn) | |||
296 | { | |||
297 | mbedtls_ssl_context ssl; | |||
298 | bool_Bool retval = false0; | |||
299 | int r; | |||
300 | ||||
301 | mbedtls_ssl_init(&ssl); | |||
302 | ||||
303 | r = mbedtls_ssl_setup(&ssl, &l->tls->config); | |||
304 | if (UNLIKELY(r != 0)__builtin_expect(((r != 0)), (0))) { | |||
305 | lwan_status_mbedtls_error(r, "Could not setup TLS context"); | |||
306 | return false0; | |||
307 | } | |||
308 | ||||
309 | /* Yielding the coroutine during the handshake enables the I/O loop to | |||
310 | * destroy this coro (e.g. on connection hangup) before we have the | |||
311 | * opportunity to free the SSL context. Defer this call for these | |||
312 | * cases. */ | |||
313 | struct coro_defer *defer = | |||
314 | coro_defer(conn->coro, lwan_setup_tls_free_ssl_context, &ssl); | |||
315 | ||||
316 | if (UNLIKELY(!defer)__builtin_expect(((!defer)), (0))) { | |||
317 | lwan_status_error("Could not defer cleanup of the TLS context")lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 317, __FUNCTION__, "Could not defer cleanup of the TLS context" ); | |||
318 | return false0; | |||
319 | } | |||
320 | ||||
321 | int fd = lwan_connection_get_fd(l, conn); | |||
322 | ||||
323 | struct lwan_mbedtls_handshake_ctx ctx = { .fd = fd }; | |||
324 | mbedtls_ssl_set_bio(&ssl, &ctx, lwan_mbedtls_send, | |||
325 | lwan_mbedtls_recv, NULL((void*)0)); | |||
326 | ||||
327 | while (true1) { | |||
328 | switch (mbedtls_ssl_handshake(&ssl)) { | |||
329 | case 0: | |||
330 | flush_pending_output(fd); | |||
331 | goto enable_tls_ulp; | |||
332 | case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS-0x6500: | |||
333 | case MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS-0x7000: | |||
334 | case MBEDTLS_ERR_SSL_WANT_READ-0x6900: | |||
335 | coro_yield(conn->coro, CONN_CORO_WANT_READ); | |||
336 | break; | |||
337 | case MBEDTLS_ERR_SSL_WANT_WRITE-0x6880: | |||
338 | coro_yield(conn->coro, CONN_CORO_WANT_WRITE); | |||
339 | break; | |||
340 | default: | |||
341 | goto fail; | |||
342 | } | |||
343 | } | |||
344 | ||||
345 | enable_tls_ulp: | |||
346 | if (UNLIKELY(setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) < 0)__builtin_expect(((setsockopt(fd, 6, 31, "tls", sizeof("tls") ) < 0)), (0))) | |||
347 | goto fail; | |||
348 | if (UNLIKELY(!lwan_setup_tls_keys(fd, &ssl, TLS_RX))__builtin_expect(((!lwan_setup_tls_keys(fd, &ssl, 2))), ( 0))) | |||
349 | goto fail; | |||
350 | if (UNLIKELY(!lwan_setup_tls_keys(fd, &ssl, TLS_TX))__builtin_expect(((!lwan_setup_tls_keys(fd, &ssl, 1))), ( 0))) | |||
351 | goto fail; | |||
352 | ||||
353 | retval = true1; | |||
354 | ||||
355 | fail: | |||
356 | coro_defer_disarm(conn->coro, defer); | |||
357 | mbedtls_ssl_free(&ssl); | |||
358 | return retval; | |||
359 | } | |||
360 | #endif | |||
361 | ||||
362 | __attribute__((noreturn)) static int process_request_coro(struct coro *coro, | |||
363 | void *data) | |||
364 | { | |||
365 | /* NOTE: This function should not return; coro_yield should be used | |||
366 | * instead. This ensures the storage for `strbuf` is alive when the | |||
367 | * coroutine ends and lwan_strbuf_free() is called. */ | |||
368 | struct lwan_connection *conn = data; | |||
369 | struct lwan *lwan = conn->thread->lwan; | |||
370 | int fd = lwan_connection_get_fd(lwan, conn); | |||
371 | enum lwan_request_flags flags = lwan->config.request_flags; | |||
372 | struct lwan_strbuf strbuf = LWAN_STRBUF_STATIC_INIT(struct lwan_strbuf) { .buffer = "" }; | |||
373 | char request_buffer[DEFAULT_BUFFER_SIZE4096]; | |||
374 | struct lwan_value buffer = {.value = request_buffer, .len = 0}; | |||
375 | char *next_request = NULL((void*)0); | |||
376 | char *header_start[N_HEADER_START64]; | |||
377 | struct lwan_proxy proxy; | |||
378 | const int error_when_n_packets = lwan_calculate_n_packets(DEFAULT_BUFFER_SIZE4096); | |||
379 | ||||
380 | coro_defer(coro, lwan_strbuf_free_defer, &strbuf); | |||
381 | ||||
382 | const size_t init_gen = 1; /* 1 call to coro_defer() */ | |||
383 | assert(init_gen == coro_deferred_get_generation(coro))((void) sizeof ((init_gen == coro_deferred_get_generation(coro )) ? 1 : 0), __extension__ ({ if (init_gen == coro_deferred_get_generation (coro)) ; else __assert_fail ("init_gen == coro_deferred_get_generation(coro)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 383, __extension__ __PRETTY_FUNCTION__); })); | |||
384 | ||||
385 | #if defined(HAVE_MBEDTLS) | |||
386 | if (conn->flags & CONN_TLS) { | |||
387 | if (UNLIKELY(!lwan_setup_tls(lwan, conn))__builtin_expect(((!lwan_setup_tls(lwan, conn))), (0))) { | |||
388 | coro_yield(conn->coro, CONN_CORO_ABORT); | |||
389 | __builtin_unreachable(); | |||
390 | } | |||
391 | } | |||
392 | #else | |||
393 | assert(!(conn->flags & CONN_TLS))((void) sizeof ((!(conn->flags & CONN_TLS)) ? 1 : 0), __extension__ ({ if (!(conn->flags & CONN_TLS)) ; else __assert_fail ("!(conn->flags & CONN_TLS)", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 393, __extension__ __PRETTY_FUNCTION__); })); | |||
394 | #endif | |||
395 | ||||
396 | while (true1) { | |||
397 | struct lwan_request_parser_helper helper = { | |||
398 | .buffer = &buffer, | |||
399 | .next_request = next_request, | |||
400 | .error_when_n_packets = error_when_n_packets, | |||
401 | .header_start = header_start, | |||
402 | }; | |||
403 | struct lwan_request request = {.conn = conn, | |||
404 | .global_response_headers = &lwan->headers, | |||
405 | .fd = fd, | |||
406 | .response = {.buffer = &strbuf}, | |||
407 | .flags = flags, | |||
408 | .proxy = &proxy, | |||
409 | .helper = &helper}; | |||
410 | ||||
411 | lwan_process_request(lwan, &request); | |||
412 | ||||
413 | /* Run the deferred instructions now (except those used to initialize | |||
414 | * the coroutine), so that if the connection is gracefully closed, | |||
415 | * the storage for ``helper'' is still there. */ | |||
416 | coro_deferred_run(coro, init_gen); | |||
417 | ||||
418 | if (UNLIKELY(!(conn->flags & CONN_IS_KEEP_ALIVE))__builtin_expect(((!(conn->flags & CONN_IS_KEEP_ALIVE) )), (0))) { | |||
419 | graceful_close(lwan, conn, request_buffer); | |||
420 | break; | |||
421 | } | |||
422 | ||||
423 | if (next_request && *next_request) { | |||
424 | conn->flags |= CONN_CORK; | |||
425 | ||||
426 | if (!(conn->flags & CONN_EVENTS_WRITE)) | |||
427 | coro_yield(coro, CONN_CORO_WANT_WRITE); | |||
428 | } else { | |||
429 | conn->flags &= ~CONN_CORK; | |||
430 | coro_yield(coro, CONN_CORO_WANT_READ); | |||
431 | } | |||
432 | ||||
433 | /* Ensure string buffer is reset between requests, and that the backing | |||
434 | * store isn't over 2KB. */ | |||
435 | lwan_strbuf_reset_trim(&strbuf, 2048); | |||
436 | ||||
437 | /* Only allow flags from config. */ | |||
438 | flags = request.flags & (REQUEST_PROXIED | REQUEST_ALLOW_CORS | REQUEST_WANTS_HSTS_HEADER); | |||
439 | next_request = helper.next_request; | |||
440 | } | |||
441 | ||||
442 | coro_yield(coro, CONN_CORO_ABORT); | |||
443 | __builtin_unreachable(); | |||
444 | } | |||
445 | ||||
446 | static ALWAYS_INLINEinline __attribute__((always_inline)) uint32_t | |||
447 | conn_flags_to_epoll_events(enum lwan_connection_flags flags) | |||
448 | { | |||
449 | static const uint32_t map[CONN_EVENTS_MASK + 1] = { | |||
450 | [0 /* Suspended (timer or await) */] = EPOLLRDHUPEPOLLRDHUP, | |||
451 | [CONN_EVENTS_WRITE] = EPOLLOUTEPOLLOUT | EPOLLRDHUPEPOLLRDHUP, | |||
452 | [CONN_EVENTS_READ] = EPOLLINEPOLLIN | EPOLLRDHUPEPOLLRDHUP, | |||
453 | [CONN_EVENTS_READ_WRITE] = EPOLLINEPOLLIN | EPOLLOUTEPOLLOUT | EPOLLRDHUPEPOLLRDHUP, | |||
454 | }; | |||
455 | ||||
456 | return map[flags & CONN_EVENTS_MASK]; | |||
457 | } | |||
458 | ||||
459 | static void update_epoll_flags(int fd, | |||
460 | struct lwan_connection *conn, | |||
461 | int epoll_fd, | |||
462 | enum lwan_connection_coro_yield yield_result) | |||
463 | { | |||
464 | static const enum lwan_connection_flags or_mask[CONN_CORO_MAX] = { | |||
465 | [CONN_CORO_YIELD] = 0, | |||
466 | ||||
467 | [CONN_CORO_WANT_READ_WRITE] = CONN_EVENTS_READ_WRITE, | |||
468 | [CONN_CORO_WANT_READ] = CONN_EVENTS_READ, | |||
469 | [CONN_CORO_WANT_WRITE] = CONN_EVENTS_WRITE, | |||
470 | ||||
471 | /* While the coro is suspended, we're not interested in either EPOLLIN | |||
472 | * or EPOLLOUT events. We still want to track this fd in epoll, though, | |||
473 | * so unset both so that only EPOLLRDHUP (plus the implicitly-set ones) | |||
474 | * are set. */ | |||
475 | [CONN_CORO_SUSPEND] = CONN_SUSPENDED, | |||
476 | ||||
477 | /* Ideally, when suspending a coroutine, the current flags&CONN_EVENTS_MASK | |||
478 | * would have to be stored and restored -- however, resuming as if the | |||
479 | * client coroutine is interested in a write event always guarantees that | |||
480 | * they'll be resumed as they're TCP sockets. There's a good chance that | |||
481 | * trying to read from a socket after resuming a coroutine will succeed, | |||
482 | * but if it doesn't because read() returns -EAGAIN, the I/O wrappers will | |||
483 | * yield with CONN_CORO_WANT_READ anyway. */ | |||
484 | [CONN_CORO_RESUME] = CONN_EVENTS_WRITE, | |||
485 | }; | |||
486 | static const enum lwan_connection_flags and_mask[CONN_CORO_MAX] = { | |||
487 | [CONN_CORO_YIELD] = ~0, | |||
488 | ||||
489 | [CONN_CORO_WANT_READ_WRITE] = ~0, | |||
490 | [CONN_CORO_WANT_READ] = ~CONN_EVENTS_WRITE, | |||
491 | [CONN_CORO_WANT_WRITE] = ~CONN_EVENTS_READ, | |||
492 | ||||
493 | [CONN_CORO_SUSPEND] = ~CONN_EVENTS_READ_WRITE, | |||
494 | [CONN_CORO_RESUME] = ~CONN_SUSPENDED, | |||
495 | }; | |||
496 | enum lwan_connection_flags prev_flags = conn->flags; | |||
497 | ||||
498 | conn->flags |= or_mask[yield_result]; | |||
499 | conn->flags &= and_mask[yield_result]; | |||
500 | ||||
501 | assert(!(conn->flags & (CONN_LISTENER_HTTP | CONN_LISTENER_HTTPS)))((void) sizeof ((!(conn->flags & (CONN_LISTENER_HTTP | CONN_LISTENER_HTTPS))) ? 1 : 0), __extension__ ({ if (!(conn ->flags & (CONN_LISTENER_HTTP | CONN_LISTENER_HTTPS))) ; else __assert_fail ("!(conn->flags & (CONN_LISTENER_HTTP | CONN_LISTENER_HTTPS))" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 501, __extension__ __PRETTY_FUNCTION__); })); | |||
502 | assert((conn->flags & CONN_TLS) == (prev_flags & CONN_TLS))((void) sizeof (((conn->flags & CONN_TLS) == (prev_flags & CONN_TLS)) ? 1 : 0), __extension__ ({ if ((conn->flags & CONN_TLS) == (prev_flags & CONN_TLS)) ; else __assert_fail ("(conn->flags & CONN_TLS) == (prev_flags & CONN_TLS)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 502, __extension__ __PRETTY_FUNCTION__); })); | |||
503 | ||||
504 | if (conn->flags == prev_flags) | |||
505 | return; | |||
506 | ||||
507 | struct epoll_event event = { | |||
508 | .events = conn_flags_to_epoll_events(conn->flags), | |||
509 | .data.ptr = conn, | |||
510 | }; | |||
511 | ||||
512 | if (UNLIKELY(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event) < 0)__builtin_expect(((epoll_ctl(epoll_fd, 3, fd, &event) < 0)), (0))) | |||
513 | lwan_status_perror("epoll_ctl")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 513, __FUNCTION__, "epoll_ctl"); | |||
514 | } | |||
515 | ||||
516 | static void clear_async_await_flag(void *data) | |||
517 | { | |||
518 | struct lwan_connection *async_fd_conn = data; | |||
519 | ||||
520 | async_fd_conn->flags &= ~CONN_ASYNC_AWAIT; | |||
521 | } | |||
522 | ||||
523 | static enum lwan_connection_coro_yield | |||
524 | resume_async(struct timeout_queue *tq, | |||
525 | enum lwan_connection_coro_yield yield_result, | |||
526 | int64_t from_coro, | |||
527 | struct lwan_connection *conn, | |||
528 | int epoll_fd) | |||
529 | { | |||
530 | static const enum lwan_connection_flags to_connection_flags[] = { | |||
531 | [CONN_CORO_ASYNC_AWAIT_READ] = CONN_EVENTS_READ, | |||
532 | [CONN_CORO_ASYNC_AWAIT_WRITE] = CONN_EVENTS_WRITE, | |||
533 | [CONN_CORO_ASYNC_AWAIT_READ_WRITE] = CONN_EVENTS_READ_WRITE, | |||
534 | }; | |||
535 | int await_fd = (int)((uint64_t)from_coro >> 32); | |||
536 | enum lwan_connection_flags flags; | |||
537 | int op; | |||
538 | ||||
539 | assert(await_fd >= 0)((void) sizeof ((await_fd >= 0) ? 1 : 0), __extension__ ({ if (await_fd >= 0) ; else __assert_fail ("await_fd >= 0" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 539, __extension__ __PRETTY_FUNCTION__); })); | |||
540 | assert(yield_result >= CONN_CORO_ASYNC_AWAIT_READ &&((void) sizeof ((yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE ) ? 1 : 0), __extension__ ({ if (yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE ) ; else __assert_fail ("yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 541, __extension__ __PRETTY_FUNCTION__); })) | |||
541 | yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE)((void) sizeof ((yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE ) ? 1 : 0), __extension__ ({ if (yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE ) ; else __assert_fail ("yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 541, __extension__ __PRETTY_FUNCTION__); })); | |||
542 | ||||
543 | flags = to_connection_flags[yield_result]; | |||
544 | ||||
545 | struct lwan_connection *await_fd_conn = &tq->lwan->conns[await_fd]; | |||
546 | if (LIKELY(await_fd_conn->flags & CONN_ASYNC_AWAIT)__builtin_expect((!!(await_fd_conn->flags & CONN_ASYNC_AWAIT )), (1))) { | |||
547 | if (LIKELY((await_fd_conn->flags & CONN_EVENTS_MASK) == flags)__builtin_expect((!!((await_fd_conn->flags & CONN_EVENTS_MASK ) == flags)), (1))) | |||
548 | return CONN_CORO_SUSPEND; | |||
549 | ||||
550 | op = EPOLL_CTL_MOD3; | |||
551 | } else { | |||
552 | op = EPOLL_CTL_ADD1; | |||
553 | flags |= CONN_ASYNC_AWAIT; | |||
554 | coro_defer(conn->coro, clear_async_await_flag, await_fd_conn); | |||
555 | } | |||
556 | ||||
557 | struct epoll_event event = {.events = conn_flags_to_epoll_events(flags), | |||
558 | .data.ptr = conn}; | |||
559 | if (LIKELY(!epoll_ctl(epoll_fd, op, await_fd, &event))__builtin_expect((!!(!epoll_ctl(epoll_fd, op, await_fd, & event))), (1))) { | |||
560 | await_fd_conn->flags &= ~CONN_EVENTS_MASK; | |||
561 | await_fd_conn->flags |= flags; | |||
562 | return CONN_CORO_SUSPEND; | |||
563 | } | |||
564 | ||||
565 | return CONN_CORO_ABORT; | |||
566 | } | |||
567 | ||||
568 | static ALWAYS_INLINEinline __attribute__((always_inline)) void resume_coro(struct timeout_queue *tq, | |||
569 | struct lwan_connection *conn, | |||
570 | int epoll_fd) | |||
571 | { | |||
572 | assert(conn->coro)((void) sizeof ((conn->coro) ? 1 : 0), __extension__ ({ if (conn->coro) ; else __assert_fail ("conn->coro", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 572, __extension__ __PRETTY_FUNCTION__); })); | |||
573 | ||||
574 | int64_t from_coro = coro_resume(conn->coro); | |||
575 | enum lwan_connection_coro_yield yield_result = from_coro & 0xffffffff; | |||
576 | ||||
577 | if (UNLIKELY(yield_result >= CONN_CORO_ASYNC)__builtin_expect(((yield_result >= CONN_CORO_ASYNC)), (0))) | |||
578 | yield_result = resume_async(tq, yield_result, from_coro, conn, epoll_fd); | |||
579 | ||||
580 | if (UNLIKELY(yield_result == CONN_CORO_ABORT)__builtin_expect(((yield_result == CONN_CORO_ABORT)), (0))) | |||
581 | return timeout_queue_expire(tq, conn); | |||
582 | ||||
583 | return update_epoll_flags(lwan_connection_get_fd(tq->lwan, conn), conn, | |||
584 | epoll_fd, yield_result); | |||
585 | } | |||
586 | ||||
587 | static void update_date_cache(struct lwan_thread *thread) | |||
588 | { | |||
589 | time_t now = time(NULL((void*)0)); | |||
590 | ||||
591 | lwan_format_rfc_time(now, thread->date.date); | |||
592 | lwan_format_rfc_time(now + (time_t)thread->lwan->config.expires, | |||
593 | thread->date.expires); | |||
594 | } | |||
595 | ||||
596 | __attribute__((cold)) | |||
597 | static bool_Bool send_buffer_without_coro(int fd, const char *buf, size_t buf_len, int flags) | |||
598 | { | |||
599 | size_t total_sent = 0; | |||
600 | ||||
601 | for (int try = 0; try < 10; try++) { | |||
602 | size_t to_send = buf_len - total_sent; | |||
603 | if (!to_send) | |||
604 | return true1; | |||
605 | ||||
606 | ssize_t sent = send(fd, buf + total_sent, to_send, flags); | |||
607 | if (sent <= 0) { | |||
608 | if (errno(*__errno_location ()) == EINTR4) | |||
609 | continue; | |||
610 | if (errno(*__errno_location ()) == EAGAIN11) | |||
611 | continue; | |||
612 | break; | |||
613 | } | |||
614 | ||||
615 | total_sent += (size_t)sent; | |||
616 | } | |||
617 | ||||
618 | return false0; | |||
619 | } | |||
620 | ||||
621 | __attribute__((cold)) | |||
622 | static bool_Bool send_string_without_coro(int fd, const char *str, int flags) | |||
623 | { | |||
624 | return send_buffer_without_coro(fd, str, strlen(str), flags); | |||
625 | } | |||
626 | ||||
627 | __attribute__((cold)) static void | |||
628 | send_last_response_without_coro(const struct lwan *l, | |||
629 | const struct lwan_connection *conn, | |||
630 | enum lwan_http_status status) | |||
631 | { | |||
632 | int fd = lwan_connection_get_fd(l, conn); | |||
633 | ||||
634 | if (conn->flags & CONN_TLS) { | |||
635 | /* There's nothing that can be done here if a client is expecting a | |||
636 | * TLS connection: the TLS handshake requires a coroutine as it | |||
637 | * might yield. (In addition, the TLS handshake might allocate | |||
638 | * memory, and if you couldn't create a coroutine at this point, | |||
639 | * it's unlikely you'd be able to allocate memory for the TLS | |||
640 | * context anyway.) */ | |||
641 | goto shutdown_and_close; | |||
642 | } | |||
643 | ||||
644 | if (!send_string_without_coro(fd, "HTTP/1.0 ", MSG_MOREMSG_MORE)) | |||
645 | goto shutdown_and_close; | |||
646 | ||||
647 | if (!send_string_without_coro( | |||
648 | fd, lwan_http_status_as_string_with_code(status), MSG_MOREMSG_MORE)) | |||
649 | goto shutdown_and_close; | |||
650 | ||||
651 | if (!send_string_without_coro(fd, "\r\nConnection: close", MSG_MOREMSG_MORE)) | |||
652 | goto shutdown_and_close; | |||
653 | ||||
654 | if (!send_string_without_coro(fd, "\r\nContent-Type: text/html", MSG_MOREMSG_MORE)) | |||
655 | goto shutdown_and_close; | |||
656 | ||||
657 | if (send_buffer_without_coro(fd, l->headers.value, l->headers.len, | |||
658 | MSG_MOREMSG_MORE)) { | |||
659 | struct lwan_strbuf buffer; | |||
660 | ||||
661 | lwan_strbuf_init(&buffer); | |||
662 | lwan_fill_default_response(&buffer, status); | |||
663 | ||||
664 | send_buffer_without_coro(fd, lwan_strbuf_get_buffer(&buffer), | |||
665 | lwan_strbuf_get_length(&buffer), 0); | |||
666 | ||||
667 | lwan_strbuf_free(&buffer); | |||
668 | } | |||
669 | ||||
670 | shutdown_and_close: | |||
671 | shutdown(fd, SHUT_RDWRSHUT_RDWR); | |||
672 | close(fd); | |||
673 | } | |||
674 | ||||
675 | static ALWAYS_INLINEinline __attribute__((always_inline)) bool_Bool spawn_coro(struct lwan_connection *conn, | |||
676 | struct coro_switcher *switcher, | |||
677 | struct timeout_queue *tq) | |||
678 | { | |||
679 | struct lwan_thread *t = conn->thread; | |||
680 | #if defined(HAVE_MBEDTLS) | |||
681 | const enum lwan_connection_flags flags_to_keep = conn->flags & CONN_TLS; | |||
682 | #else | |||
683 | const enum lwan_connection_flags flags_to_keep = 0; | |||
684 | #endif | |||
685 | ||||
686 | assert(!conn->coro)((void) sizeof ((!conn->coro) ? 1 : 0), __extension__ ({ if (!conn->coro) ; else __assert_fail ("!conn->coro", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 686, __extension__ __PRETTY_FUNCTION__); })); | |||
687 | assert(!(conn->flags & CONN_ASYNC_AWAIT))((void) sizeof ((!(conn->flags & CONN_ASYNC_AWAIT)) ? 1 : 0), __extension__ ({ if (!(conn->flags & CONN_ASYNC_AWAIT )) ; else __assert_fail ("!(conn->flags & CONN_ASYNC_AWAIT)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 687, __extension__ __PRETTY_FUNCTION__); })); | |||
688 | assert(t)((void) sizeof ((t) ? 1 : 0), __extension__ ({ if (t) ; else __assert_fail ("t", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 688, __extension__ __PRETTY_FUNCTION__); })); | |||
689 | assert((uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads)((void) sizeof (((uintptr_t)t >= (uintptr_t)tq->lwan-> thread.threads) ? 1 : 0), __extension__ ({ if ((uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads) ; else __assert_fail ("(uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 689, __extension__ __PRETTY_FUNCTION__); })); | |||
690 | assert((uintptr_t)t <((void) sizeof (((uintptr_t)t < (uintptr_t)(tq->lwan-> thread.threads + tq->lwan->thread.count)) ? 1 : 0), __extension__ ({ if ((uintptr_t)t < (uintptr_t)(tq->lwan->thread. threads + tq->lwan->thread.count)) ; else __assert_fail ("(uintptr_t)t < (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 691, __extension__ __PRETTY_FUNCTION__); })) | |||
691 | (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count))((void) sizeof (((uintptr_t)t < (uintptr_t)(tq->lwan-> thread.threads + tq->lwan->thread.count)) ? 1 : 0), __extension__ ({ if ((uintptr_t)t < (uintptr_t)(tq->lwan->thread. threads + tq->lwan->thread.count)) ; else __assert_fail ("(uintptr_t)t < (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 691, __extension__ __PRETTY_FUNCTION__); })); | |||
692 | ||||
693 | *conn = (struct lwan_connection){ | |||
694 | .coro = coro_new(switcher, process_request_coro, conn), | |||
695 | .flags = CONN_EVENTS_READ | flags_to_keep, | |||
696 | .time_to_expire = tq->current_time + tq->move_to_last_bump, | |||
697 | .thread = t, | |||
698 | }; | |||
699 | if (LIKELY(conn->coro)__builtin_expect((!!(conn->coro)), (1))) { | |||
700 | timeout_queue_insert(tq, conn); | |||
701 | return true1; | |||
702 | } | |||
703 | ||||
704 | conn->flags = 0; | |||
705 | ||||
706 | int fd = lwan_connection_get_fd(tq->lwan, conn); | |||
707 | ||||
708 | lwan_status_error("Couldn't spawn coroutine for file descriptor %d", fd)lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 708, __FUNCTION__, "Couldn't spawn coroutine for file descriptor %d" , fd); | |||
709 | ||||
710 | send_last_response_without_coro(tq->lwan, conn, HTTP_UNAVAILABLE); | |||
711 | return false0; | |||
712 | } | |||
713 | ||||
714 | static bool_Bool process_pending_timers(struct timeout_queue *tq, | |||
715 | struct lwan_thread *t, | |||
716 | int epoll_fd) | |||
717 | { | |||
718 | struct timeout *timeout; | |||
719 | bool_Bool should_expire_timers = false0; | |||
720 | ||||
721 | while ((timeout = timeouts_get(t->wheel))) { | |||
722 | struct lwan_request *request; | |||
723 | ||||
724 | if (timeout == &tq->timeout) { | |||
725 | should_expire_timers = true1; | |||
726 | continue; | |||
727 | } | |||
728 | ||||
729 | request = container_of(timeout, struct lwan_request, timeout)((struct lwan_request *) ((char *)(timeout) - __builtin_offsetof (struct lwan_request, timeout)) + ((typeof(*(timeout)) *)0 != (typeof(((struct lwan_request *)0)->timeout) *)0)); | |||
730 | ||||
731 | update_epoll_flags(request->fd, request->conn, epoll_fd, | |||
732 | CONN_CORO_RESUME); | |||
733 | } | |||
734 | ||||
735 | if (should_expire_timers) { | |||
736 | timeout_queue_expire_waiting(tq); | |||
737 | ||||
738 | /* tq timeout expires every 1000ms if there are connections, so | |||
739 | * update the date cache at this point as well. */ | |||
740 | update_date_cache(t); | |||
741 | ||||
742 | if (!timeout_queue_empty(tq)) { | |||
743 | timeouts_add(t->wheel, &tq->timeout, 1000); | |||
744 | return true1; | |||
745 | } | |||
746 | ||||
747 | timeouts_del(t->wheel, &tq->timeout); | |||
748 | } | |||
749 | ||||
750 | return false0; | |||
751 | } | |||
752 | ||||
753 | static int | |||
754 | turn_timer_wheel(struct timeout_queue *tq, struct lwan_thread *t, int epoll_fd) | |||
755 | { | |||
756 | const int infinite_timeout = -1; | |||
757 | timeout_t wheel_timeout; | |||
758 | struct timespec now; | |||
759 | ||||
760 | if (UNLIKELY(clock_gettime(monotonic_clock_id, &now) < 0)__builtin_expect(((clock_gettime(monotonic_clock_id, &now ) < 0)), (0))) | |||
761 | lwan_status_critical("Could not get monotonic time")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 761, __FUNCTION__, "Could not get monotonic time"); | |||
762 | ||||
763 | timeouts_update(t->wheel, | |||
764 | (timeout_t)(now.tv_sec * 1000 + now.tv_nsec / 1000000)); | |||
765 | ||||
766 | /* Check if there's an expired timer. */ | |||
767 | wheel_timeout = timeouts_timeout(t->wheel); | |||
768 | if (wheel_timeout > 0) { | |||
769 | return (int)wheel_timeout; /* No, but will soon. Wake us up in | |||
770 | wheel_timeout ms. */ | |||
771 | } | |||
772 | ||||
773 | if (UNLIKELY((int64_t)wheel_timeout < 0)__builtin_expect((((int64_t)wheel_timeout < 0)), (0))) | |||
774 | return infinite_timeout; /* None found. */ | |||
775 | ||||
776 | if (!process_pending_timers(tq, t, epoll_fd)) | |||
777 | return infinite_timeout; /* No more timers to process. */ | |||
778 | ||||
779 | /* After processing pending timers, determine when to wake up. */ | |||
780 | return (int)timeouts_timeout(t->wheel); | |||
781 | } | |||
782 | ||||
783 | static bool_Bool accept_waiting_clients(const struct lwan_thread *t, | |||
784 | const struct lwan_connection *listen_socket) | |||
785 | { | |||
786 | const uint32_t read_events = conn_flags_to_epoll_events(CONN_EVENTS_READ); | |||
787 | struct lwan_connection *conns = t->lwan->conns; | |||
788 | int listen_fd = (int)(intptr_t)(listen_socket - conns); | |||
789 | enum lwan_connection_flags new_conn_flags = 0; | |||
790 | ||||
791 | #if defined(HAVE_MBEDTLS) | |||
792 | if (listen_socket->flags & CONN_LISTENER_HTTPS) { | |||
793 | assert(listen_fd == t->tls_listen_fd)((void) sizeof ((listen_fd == t->tls_listen_fd) ? 1 : 0), __extension__ ({ if (listen_fd == t->tls_listen_fd) ; else __assert_fail ("listen_fd == t->tls_listen_fd", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 793, __extension__ __PRETTY_FUNCTION__); })); | |||
794 | assert(!(listen_socket->flags & CONN_LISTENER_HTTP))((void) sizeof ((!(listen_socket->flags & CONN_LISTENER_HTTP )) ? 1 : 0), __extension__ ({ if (!(listen_socket->flags & CONN_LISTENER_HTTP)) ; else __assert_fail ("!(listen_socket->flags & CONN_LISTENER_HTTP)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 794, __extension__ __PRETTY_FUNCTION__); })); | |||
795 | new_conn_flags = CONN_TLS; | |||
796 | } else { | |||
797 | assert(listen_fd == t->listen_fd)((void) sizeof ((listen_fd == t->listen_fd) ? 1 : 0), __extension__ ({ if (listen_fd == t->listen_fd) ; else __assert_fail ("listen_fd == t->listen_fd" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 797, __extension__ __PRETTY_FUNCTION__); })); | |||
798 | assert(listen_socket->flags & CONN_LISTENER_HTTP)((void) sizeof ((listen_socket->flags & CONN_LISTENER_HTTP ) ? 1 : 0), __extension__ ({ if (listen_socket->flags & CONN_LISTENER_HTTP) ; else __assert_fail ("listen_socket->flags & CONN_LISTENER_HTTP" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 798, __extension__ __PRETTY_FUNCTION__); })); | |||
799 | } | |||
800 | #endif | |||
801 | ||||
802 | while (true1) { | |||
803 | int fd = accept4(listen_fd, NULL((void*)0), NULL((void*)0), SOCK_NONBLOCKSOCK_NONBLOCK | SOCK_CLOEXECSOCK_CLOEXEC); | |||
804 | ||||
805 | if (LIKELY(fd >= 0)__builtin_expect((!!(fd >= 0)), (1))) { | |||
806 | struct lwan_connection *conn = &conns[fd]; | |||
807 | struct epoll_event ev = {.data.ptr = conn, .events = read_events}; | |||
808 | int r; | |||
809 | ||||
810 | conn->flags = new_conn_flags; | |||
811 | ||||
812 | r = epoll_ctl(conn->thread->epoll_fd, EPOLL_CTL_ADD1, fd, &ev); | |||
813 | if (UNLIKELY(r < 0)__builtin_expect(((r < 0)), (0))) { | |||
814 | lwan_status_perror("Could not add file descriptor %d to epoll "lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 816, __FUNCTION__, "Could not add file descriptor %d to epoll " "set %d. Dropping connection", fd, conn->thread->epoll_fd ) | |||
815 | "set %d. Dropping connection",lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 816, __FUNCTION__, "Could not add file descriptor %d to epoll " "set %d. Dropping connection", fd, conn->thread->epoll_fd ) | |||
816 | fd, conn->thread->epoll_fd)lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 816, __FUNCTION__, "Could not add file descriptor %d to epoll " "set %d. Dropping connection", fd, conn->thread->epoll_fd ); | |||
817 | send_last_response_without_coro(t->lwan, conn, HTTP_UNAVAILABLE); | |||
818 | conn->flags = 0; | |||
819 | } | |||
820 | ||||
821 | continue; | |||
822 | } | |||
823 | ||||
824 | switch (errno(*__errno_location ())) { | |||
825 | default: | |||
826 | lwan_status_perror("Unexpected error while accepting connections")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 826, __FUNCTION__, "Unexpected error while accepting connections" ); | |||
827 | /* fallthrough */ | |||
828 | ||||
829 | case EAGAIN11: | |||
830 | return true1; | |||
831 | ||||
832 | case EBADF9: | |||
833 | case ECONNABORTED103: | |||
834 | case EINVAL22: | |||
835 | lwan_status_info("Listening socket closed")lwan_status_info_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 835, __FUNCTION__, "Listening socket closed"); | |||
836 | return false0; | |||
837 | } | |||
838 | } | |||
839 | ||||
840 | __builtin_unreachable(); | |||
841 | } | |||
842 | ||||
843 | static int create_listen_socket(struct lwan_thread *t, | |||
844 | unsigned int num, | |||
845 | bool_Bool tls) | |||
846 | { | |||
847 | const struct lwan *lwan = t->lwan; | |||
848 | int listen_fd; | |||
849 | ||||
850 | listen_fd = lwan_create_listen_socket(lwan, num == 0, tls); | |||
851 | if (listen_fd < 0) | |||
852 | lwan_status_critical("Could not create listen_fd")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 852, __FUNCTION__, "Could not create listen_fd"); | |||
853 | ||||
854 | /* Ignore errors here, as this is just a hint */ | |||
855 | #if defined(HAVE_SO_ATTACH_REUSEPORT_CBPF) | |||
856 | /* From socket(7): "These options may be set repeatedly at any time on | |||
857 | * any socket in the group to replace the current BPF program used by | |||
858 | * all sockets in the group." */ | |||
859 | if (num == 0) { | |||
860 | /* From socket(7): "The BPF program must return an index between 0 and | |||
861 | * N-1 representing the socket which should receive the packet (where N | |||
862 | * is the number of sockets in the group)." */ | |||
863 | const uint32_t cpu_ad_off = (uint32_t)SKF_AD_OFF(-0x1000) + SKF_AD_CPU36; | |||
864 | struct sock_filter filter[] = { | |||
865 | {BPF_LD0x00 | BPF_W0x00 | BPF_ABS0x20, 0, 0, cpu_ad_off}, /* A = curr_cpu_index */ | |||
866 | {BPF_RET0x06 | BPF_A0x10, 0, 0, 0}, /* return A */ | |||
867 | }; | |||
868 | struct sock_fprog fprog = {.filter = filter, .len = N_ELEMENTS(filter)((!sizeof(char[1 - 2 * __builtin_types_compatible_p( __typeof__ (filter), __typeof__(&(filter)[0]))])) | sizeof(filter) / sizeof(filter[0]))}; | |||
869 | ||||
870 | (void)setsockopt(listen_fd, SOL_SOCKET1, SO_ATTACH_REUSEPORT_CBPF51, | |||
871 | &fprog, sizeof(fprog)); | |||
872 | (void)setsockopt(listen_fd, SOL_SOCKET1, SO_LOCK_FILTER44, | |||
873 | (int[]){1}, sizeof(int)); | |||
874 | } | |||
875 | #elif defined(HAVE_SO_INCOMING_CPU) && defined(__x86_64__1) | |||
876 | (void)setsockopt(listen_fd, SOL_SOCKET1, SO_INCOMING_CPU49, &t->cpu, | |||
877 | sizeof(t->cpu)); | |||
878 | #endif | |||
879 | ||||
880 | struct epoll_event event = { | |||
881 | .events = EPOLLINEPOLLIN | EPOLLETEPOLLET | EPOLLERREPOLLERR, | |||
882 | .data.ptr = &t->lwan->conns[listen_fd], | |||
883 | }; | |||
884 | if (epoll_ctl(t->epoll_fd, EPOLL_CTL_ADD1, listen_fd, &event) < 0) | |||
885 | lwan_status_critical_perror("Could not add socket to epoll")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 885, __FUNCTION__, "Could not add socket to epoll"); | |||
886 | ||||
887 | return listen_fd; | |||
888 | } | |||
889 | ||||
890 | static void *thread_io_loop(void *data) | |||
891 | { | |||
892 | struct lwan_thread *t = data; | |||
893 | int epoll_fd = t->epoll_fd; | |||
894 | const int max_events = LWAN_MIN((int)t->lwan->thread.max_fd, 1024)({ const __typeof__(((int)t->lwan->thread.max_fd) + 0) lwan_tmp_id4 = ((int)t->lwan->thread.max_fd); const __typeof__((1024 ) + 0) lwan_tmp_id5 = (1024); lwan_tmp_id4 > lwan_tmp_id5 ? lwan_tmp_id5 : lwan_tmp_id4; }); | |||
895 | struct lwan *lwan = t->lwan; | |||
896 | struct epoll_event *events; | |||
897 | struct coro_switcher switcher; | |||
898 | struct timeout_queue tq; | |||
899 | ||||
900 | lwan_status_debug("Worker thread #%zd starting",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 901, __FUNCTION__, "Worker thread #%zd starting", t - t-> lwan->thread.threads + 1) | |||
901 | t - t->lwan->thread.threads + 1)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 901, __FUNCTION__, "Worker thread #%zd starting", t - t-> lwan->thread.threads + 1); | |||
902 | lwan_set_thread_name("worker"); | |||
903 | ||||
904 | events = calloc((size_t)max_events, sizeof(*events)); | |||
905 | if (UNLIKELY(!events)__builtin_expect(((!events)), (0))) | |||
906 | lwan_status_critical("Could not allocate memory for events")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 906, __FUNCTION__, "Could not allocate memory for events"); | |||
907 | ||||
908 | update_date_cache(t); | |||
909 | ||||
910 | timeout_queue_init(&tq, lwan); | |||
911 | ||||
912 | lwan_random_seed_prng_for_thread(t); | |||
913 | ||||
914 | pthread_barrier_wait(&lwan->thread.barrier); | |||
915 | ||||
916 | for (;;) { | |||
917 | int timeout = turn_timer_wheel(&tq, t, epoll_fd); | |||
918 | int n_fds = epoll_wait(epoll_fd, events, max_events, timeout); | |||
919 | bool_Bool accepted_connections = false0; | |||
920 | ||||
921 | if (UNLIKELY(n_fds < 0)__builtin_expect(((n_fds < 0)), (0))) { | |||
922 | if (errno(*__errno_location ()) == EBADF9 || errno(*__errno_location ()) == EINVAL22) | |||
923 | break; | |||
924 | continue; | |||
925 | } | |||
926 | ||||
927 | for (struct epoll_event *event = events; n_fds--; event++) { | |||
928 | struct lwan_connection *conn = event->data.ptr; | |||
929 | ||||
930 | if (UNLIKELY(event->events & (EPOLLRDHUP | EPOLLHUP))__builtin_expect(((event->events & (EPOLLRDHUP | EPOLLHUP ))), (0))) { | |||
931 | timeout_queue_expire(&tq, conn); | |||
932 | continue; | |||
933 | } | |||
934 | ||||
935 | if (conn->flags & (CONN_LISTENER_HTTP | CONN_LISTENER_HTTPS)) { | |||
936 | if (LIKELY(accept_waiting_clients(t, conn))__builtin_expect((!!(accept_waiting_clients(t, conn))), (1))) { | |||
937 | accepted_connections = true1; | |||
938 | continue; | |||
939 | } | |||
940 | close(epoll_fd); | |||
941 | epoll_fd = -1; | |||
942 | break; | |||
943 | } | |||
944 | ||||
945 | if (!conn->coro) { | |||
946 | if (UNLIKELY(!spawn_coro(conn, &switcher, &tq))__builtin_expect(((!spawn_coro(conn, &switcher, &tq)) ), (0))) { | |||
947 | send_last_response_without_coro(t->lwan, conn, HTTP_INTERNAL_ERROR); | |||
948 | continue; | |||
949 | } | |||
950 | } | |||
951 | ||||
952 | resume_coro(&tq, conn, epoll_fd); | |||
953 | timeout_queue_move_to_last(&tq, conn); | |||
954 | } | |||
955 | ||||
956 | if (accepted_connections) | |||
957 | timeouts_add(t->wheel, &tq.timeout, 1000); | |||
958 | } | |||
959 | ||||
960 | pthread_barrier_wait(&lwan->thread.barrier); | |||
961 | ||||
962 | timeout_queue_expire_all(&tq); | |||
963 | free(events); | |||
964 | ||||
965 | return NULL((void*)0); | |||
966 | } | |||
967 | ||||
968 | static void create_thread(struct lwan *l, struct lwan_thread *thread) | |||
969 | { | |||
970 | int ignore; | |||
971 | pthread_attr_t attr; | |||
972 | ||||
973 | thread->lwan = l; | |||
974 | ||||
975 | thread->wheel = timeouts_open(&ignore); | |||
976 | if (!thread->wheel) | |||
977 | lwan_status_critical("Could not create timer wheel")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 977, __FUNCTION__, "Could not create timer wheel"); | |||
978 | ||||
979 | if ((thread->epoll_fd = epoll_create1(EPOLL_CLOEXECEPOLL_CLOEXEC)) < 0) | |||
980 | lwan_status_critical_perror("epoll_create")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 980, __FUNCTION__, "epoll_create"); | |||
981 | ||||
982 | if (pthread_attr_init(&attr)) | |||
983 | lwan_status_critical_perror("pthread_attr_init")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 983, __FUNCTION__, "pthread_attr_init"); | |||
984 | ||||
985 | if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEMPTHREAD_SCOPE_SYSTEM)) | |||
986 | lwan_status_critical_perror("pthread_attr_setscope")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 986, __FUNCTION__, "pthread_attr_setscope"); | |||
987 | ||||
988 | if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLEPTHREAD_CREATE_JOINABLE)) | |||
989 | lwan_status_critical_perror("pthread_attr_setdetachstate")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 989, __FUNCTION__, "pthread_attr_setdetachstate"); | |||
990 | ||||
991 | if (pthread_create(&thread->self, &attr, thread_io_loop, thread)) | |||
992 | lwan_status_critical_perror("pthread_create")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 992, __FUNCTION__, "pthread_create"); | |||
993 | ||||
994 | if (pthread_attr_destroy(&attr)) | |||
995 | lwan_status_critical_perror("pthread_attr_destroy")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 995, __FUNCTION__, "pthread_attr_destroy"); | |||
996 | } | |||
997 | ||||
998 | #if defined(__linux__1) && defined(__x86_64__1) | |||
999 | static bool_Bool read_cpu_topology(struct lwan *l, uint32_t siblings[]) | |||
1000 | { | |||
1001 | char path[PATH_MAX4096]; | |||
1002 | ||||
1003 | for (uint32_t i = 0; i < l->available_cpus; i++) | |||
1004 | siblings[i] = 0xbebacafe; | |||
1005 | ||||
1006 | for (unsigned int i = 0; i < l->available_cpus; i++) { | |||
1007 | FILE *sib; | |||
1008 | uint32_t id, sibling; | |||
1009 | char separator; | |||
1010 | ||||
1011 | snprintf(path, sizeof(path), | |||
1012 | "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", | |||
1013 | i); | |||
1014 | ||||
1015 | sib = fopen(path, "re"); | |||
1016 | if (!sib) { | |||
1017 | lwan_status_warning("Could not open `%s` to determine CPU topology",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1018, __FUNCTION__, "Could not open `%s` to determine CPU topology" , path) | |||
1018 | path)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1018, __FUNCTION__, "Could not open `%s` to determine CPU topology" , path); | |||
1019 | return false0; | |||
1020 | } | |||
1021 | ||||
1022 | switch (fscanf(sib, "%u%c%u", &id, &separator, &sibling)) { | |||
1023 | case 2: /* No SMT */ | |||
1024 | siblings[i] = id; | |||
1025 | break; | |||
1026 | case 3: /* SMT */ | |||
1027 | if (!(separator == ',' || separator == '-')) { | |||
1028 | lwan_status_critical("Expecting either ',' or '-' for sibling separator")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1028, __FUNCTION__, "Expecting either ',' or '-' for sibling separator" ); | |||
1029 | __builtin_unreachable(); | |||
1030 | } | |||
1031 | ||||
1032 | siblings[i] = sibling; | |||
1033 | break; | |||
1034 | default: | |||
1035 | lwan_status_critical("%s has invalid format", path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1035, __FUNCTION__, "%s has invalid format", path); | |||
1036 | __builtin_unreachable(); | |||
1037 | } | |||
1038 | ||||
1039 | fclose(sib); | |||
1040 | } | |||
1041 | ||||
1042 | /* Perform a sanity check here, as some systems seem to filter out the | |||
1043 | * result of sysconf() to obtain the number of configured and online | |||
1044 | * CPUs but don't bother changing what's available through sysfs as far | |||
1045 | * as the CPU topology information goes. It's better to fall back to a | |||
1046 | * possibly non-optimal setup than just crash during startup while | |||
1047 | * trying to perform an out-of-bounds array access. */ | |||
1048 | for (unsigned int i = 0; i < l->available_cpus; i++) { | |||
1049 | if (siblings[i] == 0xbebacafe) { | |||
1050 | lwan_status_warning("Could not determine sibling for CPU %d", i)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1050, __FUNCTION__, "Could not determine sibling for CPU %d" , i); | |||
1051 | return false0; | |||
1052 | } | |||
1053 | ||||
1054 | if (siblings[i] >= l->available_cpus) { | |||
1055 | lwan_status_warning("CPU information topology says CPU %d exists, "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1058, __FUNCTION__, "CPU information topology says CPU %d exists, " "but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?" , siblings[i], l->available_cpus, l->online_cpus) | |||
1056 | "but max available CPUs is %d (online CPUs: %d). "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1058, __FUNCTION__, "CPU information topology says CPU %d exists, " "but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?" , siblings[i], l->available_cpus, l->online_cpus) | |||
1057 | "Is Lwan running in a (broken) container?",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1058, __FUNCTION__, "CPU information topology says CPU %d exists, " "but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?" , siblings[i], l->available_cpus, l->online_cpus) | |||
1058 | siblings[i], l->available_cpus, l->online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1058, __FUNCTION__, "CPU information topology says CPU %d exists, " "but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?" , siblings[i], l->available_cpus, l->online_cpus); | |||
1059 | return false0; | |||
1060 | } | |||
1061 | } | |||
1062 | ||||
1063 | return true1; | |||
1064 | } | |||
1065 | ||||
1066 | static void | |||
1067 | siblings_to_schedtbl(struct lwan *l, uint32_t siblings[], uint32_t schedtbl[]) | |||
1068 | { | |||
1069 | int *seen = alloca(l->available_cpus * sizeof(int))__builtin_alloca (l->available_cpus * sizeof(int)); | |||
1070 | unsigned int n_schedtbl = 0; | |||
1071 | ||||
1072 | for (uint32_t i = 0; i < l->available_cpus; i++) | |||
1073 | seen[i] = -1; | |||
1074 | ||||
1075 | for (uint32_t i = 0; i < l->available_cpus; i++) { | |||
1076 | if (seen[siblings[i]] < 0) { | |||
1077 | seen[siblings[i]] = (int)i; | |||
1078 | } else { | |||
1079 | schedtbl[n_schedtbl++] = (uint32_t)seen[siblings[i]]; | |||
1080 | schedtbl[n_schedtbl++] = i; | |||
1081 | } | |||
1082 | } | |||
1083 | ||||
1084 | if (n_schedtbl != l->available_cpus) | |||
1085 | memcpy(schedtbl, seen, l->available_cpus * sizeof(int)); | |||
1086 | } | |||
1087 | ||||
1088 | static bool_Bool | |||
1089 | topology_to_schedtbl(struct lwan *l, uint32_t schedtbl[], uint32_t n_threads) | |||
1090 | { | |||
1091 | uint32_t *siblings = alloca(l->available_cpus * sizeof(uint32_t))__builtin_alloca (l->available_cpus * sizeof(uint32_t)); | |||
1092 | ||||
1093 | if (read_cpu_topology(l, siblings)) { | |||
1094 | uint32_t *affinity = alloca(l->available_cpus * sizeof(uint32_t))__builtin_alloca (l->available_cpus * sizeof(uint32_t)); | |||
1095 | ||||
1096 | siblings_to_schedtbl(l, siblings, affinity); | |||
1097 | ||||
1098 | for (uint32_t i = 0; i < n_threads; i++) | |||
1099 | schedtbl[i] = affinity[i % l->available_cpus]; | |||
1100 | return true1; | |||
1101 | } | |||
1102 | ||||
1103 | for (uint32_t i = 0; i < n_threads; i++) | |||
1104 | schedtbl[i] = (i / 2) % l->thread.count; | |||
1105 | return false0; | |||
1106 | } | |||
1107 | ||||
1108 | static void | |||
1109 | adjust_thread_affinity(const struct lwan_thread *thread) | |||
1110 | { | |||
1111 | cpu_set_t set; | |||
1112 | ||||
1113 | CPU_ZERO(&set)do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while (0); | |||
1114 | CPU_SET(thread->cpu, &set)(__extension__ ({ size_t __cpu = (thread->cpu); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&set)->__bits ))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; })); | |||
1115 | ||||
1116 | if (pthread_setaffinity_np(thread->self, sizeof(set), &set)) | |||
1117 | lwan_status_warning("Could not set thread affinity")lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1117, __FUNCTION__, "Could not set thread affinity"); | |||
1118 | } | |||
1119 | #endif | |||
1120 | ||||
1121 | #if defined(HAVE_MBEDTLS) | |||
1122 | static bool_Bool is_tls_ulp_supported(void) | |||
1123 | { | |||
1124 | FILE *available_ulp = fopen("/proc/sys/net/ipv4/tcp_available_ulp", "re"); | |||
1125 | char buffer[512]; | |||
1126 | bool_Bool available = false0; | |||
1127 | ||||
1128 | if (!available_ulp) | |||
1129 | return false0; | |||
1130 | ||||
1131 | if (fgets(buffer, 512, available_ulp)) { | |||
1132 | if (strstr(buffer, "tls")) | |||
1133 | available = true1; | |||
1134 | } | |||
1135 | ||||
1136 | fclose(available_ulp); | |||
1137 | return available; | |||
1138 | } | |||
1139 | ||||
1140 | static bool_Bool lwan_init_tls(struct lwan *l) | |||
1141 | { | |||
1142 | static const int aes128_ciphers[] = { | |||
1143 | /* Only allow Ephemeral Diffie-Hellman key exchange, so Perfect | |||
1144 | * Forward Secrecy is possible. */ | |||
1145 | MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA2560xC02F, | |||
1146 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA2560xC02B, | |||
1147 | MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA2560x9E, | |||
1148 | MBEDTLS_TLS_DHE_PSK_WITH_AES_128_GCM_SHA2560xAA, | |||
1149 | ||||
1150 | /* FIXME: Other ciphers are supported by kTLS, notably AES256 and | |||
1151 | * ChaCha20-Poly1305. Add those here and patch | |||
1152 | * lwan_setup_tls_keys() to match. */ | |||
1153 | ||||
1154 | /* FIXME: Maybe allow this to be user-tunable like other servers do? */ | |||
1155 | 0, | |||
1156 | }; | |||
1157 | int r; | |||
1158 | ||||
1159 | if (!l->config.ssl.cert || !l->config.ssl.key) | |||
1160 | return false0; | |||
1161 | ||||
1162 | if (!is_tls_ulp_supported()) { | |||
1163 | lwan_status_critical(lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1164, __FUNCTION__, "TLS ULP not loaded. Try running `modprobe tls` as root." ) | |||
1164 | "TLS ULP not loaded. Try running `modprobe tls` as root.")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1164, __FUNCTION__, "TLS ULP not loaded. Try running `modprobe tls` as root." ); | |||
1165 | } | |||
1166 | ||||
1167 | l->tls = calloc(1, sizeof(*l->tls)); | |||
1168 | if (!l->tls) | |||
1169 | lwan_status_critical("Could not allocate memory for SSL context")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1169, __FUNCTION__, "Could not allocate memory for SSL context" ); | |||
1170 | ||||
1171 | lwan_status_debug("Initializing mbedTLS")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1171, __FUNCTION__, "Initializing mbedTLS"); | |||
1172 | ||||
1173 | mbedtls_ssl_config_init(&l->tls->config); | |||
1174 | mbedtls_x509_crt_init(&l->tls->server_cert); | |||
1175 | mbedtls_pk_init(&l->tls->server_key); | |||
1176 | mbedtls_entropy_init(&l->tls->entropy); | |||
1177 | mbedtls_ctr_drbg_init(&l->tls->ctr_drbg); | |||
1178 | ||||
1179 | r = mbedtls_x509_crt_parse_file(&l->tls->server_cert, l->config.ssl.cert); | |||
1180 | if (r) { | |||
1181 | lwan_status_mbedtls_error(r, "Could not parse certificate at %s", | |||
1182 | l->config.ssl.cert); | |||
1183 | abort(); | |||
1184 | } | |||
1185 | ||||
1186 | r = mbedtls_pk_parse_keyfile(&l->tls->server_key, l->config.ssl.key, NULL((void*)0)); | |||
1187 | if (r) { | |||
1188 | lwan_status_mbedtls_error(r, "Could not parse key file at %s", | |||
1189 | l->config.ssl.key); | |||
1190 | abort(); | |||
1191 | } | |||
1192 | ||||
1193 | /* Even though this points to files that will probably be outside | |||
1194 | * the reach of the server (if straightjackets are used), wipe this | |||
1195 | * struct to get rid of the paths to these files. */ | |||
1196 | lwan_always_bzero(l->config.ssl.cert, strlen(l->config.ssl.cert)); | |||
1197 | free(l->config.ssl.cert); | |||
1198 | lwan_always_bzero(l->config.ssl.key, strlen(l->config.ssl.key)); | |||
1199 | free(l->config.ssl.key); | |||
1200 | lwan_always_bzero(&l->config.ssl, sizeof(l->config.ssl)); | |||
1201 | ||||
1202 | mbedtls_ssl_conf_ca_chain(&l->tls->config, l->tls->server_cert.next, NULL((void*)0)); | |||
1203 | r = mbedtls_ssl_conf_own_cert(&l->tls->config, &l->tls->server_cert, | |||
1204 | &l->tls->server_key); | |||
1205 | if (r) { | |||
1206 | lwan_status_mbedtls_error(r, "Could not set cert/key"); | |||
1207 | abort(); | |||
1208 | } | |||
1209 | ||||
1210 | r = mbedtls_ctr_drbg_seed(&l->tls->ctr_drbg, mbedtls_entropy_func, | |||
1211 | &l->tls->entropy, NULL((void*)0), 0); | |||
1212 | if (r) { | |||
1213 | lwan_status_mbedtls_error(r, "Could not seed ctr_drbg"); | |||
1214 | abort(); | |||
1215 | } | |||
1216 | ||||
1217 | r = mbedtls_ssl_config_defaults(&l->tls->config, MBEDTLS_SSL_IS_SERVER1, | |||
1218 | MBEDTLS_SSL_TRANSPORT_STREAM0, | |||
1219 | MBEDTLS_SSL_PRESET_DEFAULT0); | |||
1220 | if (r) { | |||
1221 | lwan_status_mbedtls_error(r, "Could not set mbedTLS default config"); | |||
1222 | abort(); | |||
1223 | } | |||
1224 | ||||
1225 | mbedtls_ssl_conf_rng(&l->tls->config, mbedtls_ctr_drbg_random, | |||
1226 | &l->tls->ctr_drbg); | |||
1227 | mbedtls_ssl_conf_ciphersuites(&l->tls->config, aes128_ciphers); | |||
1228 | ||||
1229 | mbedtls_ssl_conf_renegotiation(&l->tls->config, | |||
1230 | MBEDTLS_SSL_RENEGOTIATION_DISABLED0); | |||
1231 | mbedtls_ssl_conf_legacy_renegotiation(&l->tls->config, | |||
1232 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION0); | |||
1233 | ||||
1234 | #if defined(MBEDTLS_SSL_ALPN) | |||
1235 | static const char *alpn_protos[] = {"http/1.1", NULL((void*)0)}; | |||
1236 | mbedtls_ssl_conf_alpn_protocols(&l->tls->config, alpn_protos); | |||
1237 | #endif | |||
1238 | ||||
1239 | return true1; | |||
1240 | } | |||
1241 | #endif | |||
1242 | ||||
1243 | void lwan_thread_init(struct lwan *l) | |||
1244 | { | |||
1245 | const unsigned int total_conns = l->thread.max_fd * l->thread.count; | |||
1246 | #if defined(HAVE_MBEDTLS) | |||
1247 | const bool_Bool tls_initialized = lwan_init_tls(l); | |||
1248 | #else | |||
1249 | const bool_Bool tls_initialized = false0; | |||
1250 | #endif | |||
1251 | ||||
1252 | lwan_status_debug("Initializing threads")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1252, __FUNCTION__, "Initializing threads"); | |||
1253 | ||||
1254 | l->thread.threads = | |||
1255 | calloc((size_t)l->thread.count, sizeof(struct lwan_thread)); | |||
1256 | if (!l->thread.threads) | |||
| ||||
1257 | lwan_status_critical("Could not allocate memory for threads")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1257, __FUNCTION__, "Could not allocate memory for threads" ); | |||
1258 | ||||
1259 | uint32_t *schedtbl; | |||
1260 | uint32_t n_threads; | |||
1261 | bool_Bool adj_affinity; | |||
1262 | ||||
1263 | #if defined(__x86_64__1) && defined(__linux__1) | |||
1264 | if (l->online_cpus > 1) { | |||
1265 | static_assert_Static_assert(sizeof(struct lwan_connection) == 32, | |||
1266 | "Two connections per cache line"); | |||
1267 | #ifdef _SC_LEVEL1_DCACHE_LINESIZE_SC_LEVEL1_DCACHE_LINESIZE | |||
1268 | assert(sysconf(_SC_LEVEL1_DCACHE_LINESIZE) == 64)((void) sizeof ((sysconf(_SC_LEVEL1_DCACHE_LINESIZE) == 64) ? 1 : 0), __extension__ ({ if (sysconf(_SC_LEVEL1_DCACHE_LINESIZE ) == 64) ; else __assert_fail ("sysconf(_SC_LEVEL1_DCACHE_LINESIZE) == 64" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1268, __extension__ __PRETTY_FUNCTION__); })); | |||
1269 | #endif | |||
1270 | lwan_status_debug("%d CPUs of %d are online. "lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1272, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients" , l->online_cpus, l->available_cpus) | |||
1271 | "Reading topology to pre-schedule clients",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1272, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients" , l->online_cpus, l->available_cpus) | |||
1272 | l->online_cpus, l->available_cpus)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1272, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients" , l->online_cpus, l->available_cpus); | |||
1273 | /* | |||
1274 | * Pre-schedule each file descriptor, to reduce some operations in the | |||
1275 | * fast path. | |||
1276 | * | |||
1277 | * Since struct lwan_connection is guaranteed to be 32-byte long, two of | |||
1278 | * them can fill up a cache line. Assume siblings share cache lines and | |||
1279 | * use the CPU topology to group two connections per cache line in such | |||
1280 | * a way that false sharing is avoided. | |||
1281 | */ | |||
1282 | n_threads = (uint32_t)lwan_nextpow2((size_t)((l->thread.count - 1) * 2)); | |||
1283 | schedtbl = alloca(n_threads * sizeof(uint32_t))__builtin_alloca (n_threads * sizeof(uint32_t)); | |||
1284 | ||||
1285 | adj_affinity = topology_to_schedtbl(l, schedtbl, n_threads); | |||
1286 | ||||
1287 | n_threads--; /* Transform count into mask for AND below */ | |||
1288 | ||||
1289 | for (unsigned int i = 0; i < total_conns; i++) | |||
1290 | l->conns[i].thread = &l->thread.threads[schedtbl[i & n_threads]]; | |||
| ||||
1291 | } else | |||
1292 | #endif /* __x86_64__ && __linux__ */ | |||
1293 | { | |||
1294 | lwan_status_debug("Using round-robin to preschedule clients")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1294, __FUNCTION__, "Using round-robin to preschedule clients" ); | |||
1295 | ||||
1296 | for (unsigned int i = 0; i < l->thread.count; i++) | |||
1297 | l->thread.threads[i].cpu = i % l->online_cpus; | |||
1298 | for (unsigned int i = 0; i < total_conns; i++) | |||
1299 | l->conns[i].thread = &l->thread.threads[i % l->thread.count]; | |||
1300 | ||||
1301 | schedtbl = NULL((void*)0); | |||
1302 | adj_affinity = false0; | |||
1303 | n_threads = l->thread.count; | |||
1304 | } | |||
1305 | ||||
1306 | for (unsigned int i = 0; i < l->thread.count; i++) { | |||
1307 | struct lwan_thread *thread = NULL((void*)0); | |||
1308 | ||||
1309 | if (schedtbl) { | |||
1310 | /* This is not the most elegant thing, but this assures that the | |||
1311 | * listening sockets are added to the SO_REUSEPORT group in a | |||
1312 | * specific order, because that's what the CBPF program to direct | |||
1313 | * the incoming connection to the right CPU will use. */ | |||
1314 | for (uint32_t thread_id = 0; thread_id < l->thread.count; | |||
1315 | thread_id++) { | |||
1316 | if (schedtbl[thread_id & n_threads] == i) { | |||
1317 | thread = &l->thread.threads[thread_id]; | |||
1318 | break; | |||
1319 | } | |||
1320 | } | |||
1321 | if (!thread) { | |||
1322 | /* FIXME: can this happen when we have a offline CPU? */ | |||
1323 | lwan_status_critical(lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1324, __FUNCTION__, "Could not figure out which CPU thread %d should go to" , i) | |||
1324 | "Could not figure out which CPU thread %d should go to", i)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1324, __FUNCTION__, "Could not figure out which CPU thread %d should go to" , i); | |||
1325 | } | |||
1326 | } else { | |||
1327 | thread = &l->thread.threads[i % l->thread.count]; | |||
1328 | } | |||
1329 | ||||
1330 | if (pthread_barrier_init(&l->thread.barrier, NULL((void*)0), 2)) | |||
1331 | lwan_status_critical("Could not create barrier")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1331, __FUNCTION__, "Could not create barrier"); | |||
1332 | ||||
1333 | create_thread(l, thread); | |||
1334 | ||||
1335 | if ((thread->listen_fd = create_listen_socket(thread, i, false0)) < 0) | |||
1336 | lwan_status_critical_perror("Could not create listening socket")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1336, __FUNCTION__, "Could not create listening socket"); | |||
1337 | l->conns[thread->listen_fd].flags |= CONN_LISTENER_HTTP; | |||
1338 | ||||
1339 | if (tls_initialized) { | |||
1340 | if ((thread->tls_listen_fd = create_listen_socket(thread, i, true1)) < 0) | |||
1341 | lwan_status_critical_perror("Could not create TLS listening socket")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1341, __FUNCTION__, "Could not create TLS listening socket" ); | |||
1342 | l->conns[thread->tls_listen_fd].flags |= CONN_LISTENER_HTTPS; | |||
1343 | } else { | |||
1344 | thread->tls_listen_fd = -1; | |||
1345 | } | |||
1346 | ||||
1347 | if (adj_affinity) { | |||
1348 | l->thread.threads[i].cpu = schedtbl[i & n_threads]; | |||
1349 | adjust_thread_affinity(thread); | |||
1350 | } | |||
1351 | ||||
1352 | pthread_barrier_wait(&l->thread.barrier); | |||
1353 | } | |||
1354 | ||||
1355 | lwan_status_debug("Worker threads created and ready to serve")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1355, __FUNCTION__, "Worker threads created and ready to serve" ); | |||
1356 | } | |||
1357 | ||||
1358 | void lwan_thread_shutdown(struct lwan *l) | |||
1359 | { | |||
1360 | lwan_status_debug("Shutting down threads")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 1360, __FUNCTION__, "Shutting down threads"); | |||
1361 | ||||
1362 | for (unsigned int i = 0; i < l->thread.count; i++) { | |||
1363 | struct lwan_thread *t = &l->thread.threads[i]; | |||
1364 | int epoll_fd = t->epoll_fd; | |||
1365 | int listen_fd = t->listen_fd; | |||
1366 | ||||
1367 | t->listen_fd = -1; | |||
1368 | t->epoll_fd = -1; | |||
1369 | close(epoll_fd); | |||
1370 | close(listen_fd); | |||
1371 | } | |||
1372 | ||||
1373 | pthread_barrier_wait(&l->thread.barrier); | |||
1374 | pthread_barrier_destroy(&l->thread.barrier); | |||
1375 | ||||
1376 | for (unsigned int i = 0; i < l->thread.count; i++) { | |||
1377 | struct lwan_thread *t = &l->thread.threads[i]; | |||
1378 | ||||
1379 | pthread_join(l->thread.threads[i].self, NULL((void*)0)); | |||
1380 | timeouts_close(t->wheel); | |||
1381 | } | |||
1382 | ||||
1383 | free(l->thread.threads); | |||
1384 | ||||
1385 | #if defined(HAVE_MBEDTLS) | |||
1386 | if (l->tls) { | |||
1387 | mbedtls_ssl_config_free(&l->tls->config); | |||
1388 | mbedtls_x509_crt_free(&l->tls->server_cert); | |||
1389 | mbedtls_pk_free(&l->tls->server_key); | |||
1390 | mbedtls_entropy_free(&l->tls->entropy); | |||
1391 | mbedtls_ctr_drbg_free(&l->tls->ctr_drbg); | |||
1392 | free(l->tls); | |||
1393 | } | |||
1394 | #endif | |||
1395 | } |