static-threaded.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define WEB_SERVER_INTERNALS 1
  3. #include "static-threaded.h"
  4. int web_client_timeout = DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS;
  5. int web_client_first_request_timeout = DEFAULT_TIMEOUT_TO_RECEIVE_FIRST_WEB_REQUEST;
  6. long web_client_streaming_rate_t = 0L;
  7. #define WORKER_JOB_ADD_CONNECTION 0
  8. #define WORKER_JOB_DEL_COLLECTION 1
  9. #define WORKER_JOB_ADD_FILE 2
  10. #define WORKER_JOB_DEL_FILE 3
  11. #define WORKER_JOB_READ_FILE 4
  12. #define WORKER_JOB_WRITE_FILE 5
  13. #define WORKER_JOB_RCV_DATA 6
  14. #define WORKER_JOB_SND_DATA 7
  15. #define WORKER_JOB_PROCESS 8
  16. #if (WORKER_UTILIZATION_MAX_JOB_TYPES < 9)
  17. #error Please increase WORKER_UTILIZATION_MAX_JOB_TYPES to at least 8
  18. #endif
  19. /*
  20. * --------------------------------------------------------------------------------------------------------------------
  21. * Build web_client state from the pollinfo that describes an accepted connection.
  22. */
  23. static struct web_client *web_client_create_on_fd(POLLINFO *pi) {
  24. struct web_client *w;
  25. w = web_client_get_from_cache();
  26. w->ifd = w->ofd = pi->fd;
  27. strncpyz(w->client_ip, pi->client_ip, sizeof(w->client_ip) - 1);
  28. strncpyz(w->client_port, pi->client_port, sizeof(w->client_port) - 1);
  29. strncpyz(w->client_host, pi->client_host, sizeof(w->client_host) - 1);
  30. if(unlikely(!*w->client_ip)) strcpy(w->client_ip, "-");
  31. if(unlikely(!*w->client_port)) strcpy(w->client_port, "-");
  32. w->port_acl = pi->port_acl;
  33. int flag = 1;
  34. if(unlikely(
  35. web_client_check_conn_tcp(w) && setsockopt(w->ifd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) != 0))
  36. netdata_log_debug(D_WEB_CLIENT, "%llu: failed to enable TCP_NODELAY on socket fd %d.", w->id, w->ifd);
  37. flag = 1;
  38. if(unlikely(setsockopt(w->ifd, SOL_SOCKET, SO_KEEPALIVE, (char *) &flag, sizeof(int)) != 0))
  39. netdata_log_debug(D_WEB_CLIENT, "%llu: failed to enable SO_KEEPALIVE on socket fd %d.", w->id, w->ifd);
  40. web_client_update_acl_matches(w);
  41. web_client_enable_wait_receive(w);
  42. web_server_log_connection(w, "CONNECTED");
  43. w->pollinfo_slot = pi->slot;
  44. return(w);
  45. }
  46. // --------------------------------------------------------------------------------------
  47. // the main socket listener - STATIC-THREADED
  48. struct web_server_static_threaded_worker {
  49. netdata_thread_t thread;
  50. int id;
  51. int running;
  52. size_t max_sockets;
  53. volatile size_t connected;
  54. volatile size_t disconnected;
  55. volatile size_t receptions;
  56. volatile size_t sends;
  57. volatile size_t max_concurrent;
  58. volatile size_t files_read;
  59. volatile size_t file_reads;
  60. };
  61. static long long static_threaded_workers_count = 1;
  62. static struct web_server_static_threaded_worker *static_workers_private_data = NULL;
  63. static __thread struct web_server_static_threaded_worker *worker_private = NULL;
  64. // ----------------------------------------------------------------------------
  65. static inline int web_server_check_client_status(struct web_client *w) {
  66. if(unlikely(web_client_check_dead(w) || (!web_client_has_wait_receive(w) && !web_client_has_wait_send(w))))
  67. return -1;
  68. return 0;
  69. }
  70. // ----------------------------------------------------------------------------
  71. // web server files
  72. static void *web_server_file_add_callback(POLLINFO *pi, short int *events, void *data) {
  73. struct web_client *w = (struct web_client *)data;
  74. worker_is_busy(WORKER_JOB_ADD_FILE);
  75. worker_private->files_read++;
  76. netdata_log_debug(D_WEB_CLIENT, "%llu: ADDED FILE READ ON FD %d", w->id, pi->fd);
  77. *events = POLLIN;
  78. pi->data = w;
  79. worker_is_idle();
  80. return w;
  81. }
  82. static void web_server_file_del_callback(POLLINFO *pi) {
  83. struct web_client *w = (struct web_client *)pi->data;
  84. netdata_log_debug(D_WEB_CLIENT, "%llu: RELEASE FILE READ ON FD %d", w->id, pi->fd);
  85. worker_is_busy(WORKER_JOB_DEL_FILE);
  86. w->pollinfo_filecopy_slot = 0;
  87. if(unlikely(!w->pollinfo_slot)) {
  88. netdata_log_debug(D_WEB_CLIENT, "%llu: CROSS WEB CLIENT CLEANUP (iFD %d, oFD %d)", w->id, pi->fd, w->ofd);
  89. web_server_log_connection(w, "DISCONNECTED");
  90. web_client_request_done(w);
  91. web_client_release_to_cache(w);
  92. global_statistics_web_client_disconnected();
  93. }
  94. worker_is_idle();
  95. }
  96. static int web_server_file_read_callback(POLLINFO *pi, short int *events) {
  97. int retval = -1;
  98. struct web_client *w = (struct web_client *)pi->data;
  99. worker_is_busy(WORKER_JOB_READ_FILE);
  100. // if there is no POLLINFO linked to this, it means the client disconnected
  101. // stop the file reading too
  102. if(unlikely(!w->pollinfo_slot)) {
  103. netdata_log_debug(D_WEB_CLIENT, "%llu: PREVENTED ATTEMPT TO READ FILE ON FD %d, ON CLOSED WEB CLIENT", w->id, pi->fd);
  104. retval = -1;
  105. goto cleanup;
  106. }
  107. if(unlikely(w->mode != HTTP_REQUEST_MODE_FILECOPY || w->ifd == w->ofd)) {
  108. netdata_log_debug(D_WEB_CLIENT, "%llu: PREVENTED ATTEMPT TO READ FILE ON FD %d, ON NON-FILECOPY WEB CLIENT", w->id, pi->fd);
  109. retval = -1;
  110. goto cleanup;
  111. }
  112. netdata_log_debug(D_WEB_CLIENT, "%llu: READING FILE ON FD %d", w->id, pi->fd);
  113. worker_private->file_reads++;
  114. ssize_t ret = unlikely(web_client_read_file(w));
  115. if(likely(web_client_has_wait_send(w))) {
  116. POLLJOB *p = pi->p; // our POLLJOB
  117. POLLINFO *wpi = pollinfo_from_slot(p, w->pollinfo_slot); // POLLINFO of the client socket
  118. netdata_log_debug(D_WEB_CLIENT, "%llu: SIGNALING W TO SEND (iFD %d, oFD %d)", w->id, pi->fd, wpi->fd);
  119. p->fds[wpi->slot].events |= POLLOUT;
  120. }
  121. if(unlikely(ret <= 0 || w->ifd == w->ofd)) {
  122. netdata_log_debug(D_WEB_CLIENT, "%llu: DONE READING FILE ON FD %d", w->id, pi->fd);
  123. retval = -1;
  124. goto cleanup;
  125. }
  126. *events = POLLIN;
  127. retval = 0;
  128. cleanup:
  129. worker_is_idle();
  130. return retval;
  131. }
  132. static int web_server_file_write_callback(POLLINFO *pi, short int *events) {
  133. (void)pi;
  134. (void)events;
  135. worker_is_busy(WORKER_JOB_WRITE_FILE);
  136. netdata_log_error("Writing to web files is not supported!");
  137. worker_is_idle();
  138. return -1;
  139. }
  140. // ----------------------------------------------------------------------------
  141. // web server clients
  142. static void *web_server_add_callback(POLLINFO *pi, short int *events, void *data) {
  143. (void)data; // Suppress warning on unused argument
  144. worker_is_busy(WORKER_JOB_ADD_CONNECTION);
  145. worker_private->connected++;
  146. size_t concurrent = worker_private->connected - worker_private->disconnected;
  147. if(unlikely(concurrent > worker_private->max_concurrent))
  148. worker_private->max_concurrent = concurrent;
  149. *events = POLLIN;
  150. netdata_log_debug(D_WEB_CLIENT_ACCESS, "LISTENER on %d: new connection.", pi->fd);
  151. struct web_client *w = web_client_create_on_fd(pi);
  152. if (!strncmp(pi->client_port, "UNIX", 4)) {
  153. web_client_set_conn_unix(w);
  154. } else {
  155. web_client_set_conn_tcp(w);
  156. }
  157. #ifdef ENABLE_HTTPS
  158. if ((web_client_check_conn_tcp(w)) && (netdata_ssl_web_server_ctx)) {
  159. sock_delnonblock(w->ifd);
  160. //Read the first 7 bytes from the message, but the message
  161. //is not removed from the queue, because we are using MSG_PEEK
  162. char test[8];
  163. if ( recv(w->ifd,test, 7, MSG_PEEK) == 7 ) {
  164. test[7] = '\0';
  165. }
  166. else {
  167. // we couldn't read 7 bytes
  168. sock_setnonblock(w->ifd);
  169. goto cleanup;
  170. }
  171. if(test[0] > 0x17) {
  172. // no SSL
  173. netdata_ssl_close(&w->ssl); // free any previous SSL data
  174. }
  175. else {
  176. // SSL
  177. if(!netdata_ssl_open(&w->ssl, netdata_ssl_web_server_ctx, w->ifd) || !netdata_ssl_accept(&w->ssl))
  178. WEB_CLIENT_IS_DEAD(w);
  179. }
  180. sock_setnonblock(w->ifd);
  181. }
  182. #endif
  183. netdata_log_debug(D_WEB_CLIENT, "%llu: ADDED CLIENT FD %d", w->id, pi->fd);
  184. #ifdef ENABLE_HTTPS
  185. cleanup:
  186. #endif
  187. worker_is_idle();
  188. return w;
  189. }
  190. // TCP client disconnected
  191. static void web_server_del_callback(POLLINFO *pi) {
  192. worker_is_busy(WORKER_JOB_DEL_COLLECTION);
  193. worker_private->disconnected++;
  194. struct web_client *w = (struct web_client *)pi->data;
  195. w->pollinfo_slot = 0;
  196. if(unlikely(w->pollinfo_filecopy_slot)) {
  197. POLLINFO *fpi = pollinfo_from_slot(pi->p, w->pollinfo_filecopy_slot); // POLLINFO of the client socket
  198. (void)fpi;
  199. netdata_log_debug(D_WEB_CLIENT, "%llu: THE CLIENT WILL BE FRED BY READING FILE JOB ON FD %d", w->id, fpi->fd);
  200. }
  201. else {
  202. if(web_client_flag_check(w, WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET))
  203. pi->flags |= POLLINFO_FLAG_DONT_CLOSE;
  204. netdata_log_debug(D_WEB_CLIENT, "%llu: CLOSING CLIENT FD %d", w->id, pi->fd);
  205. web_server_log_connection(w, "DISCONNECTED");
  206. web_client_request_done(w);
  207. web_client_release_to_cache(w);
  208. global_statistics_web_client_disconnected();
  209. }
  210. worker_is_idle();
  211. }
  212. static int web_server_rcv_callback(POLLINFO *pi, short int *events) {
  213. int ret = -1;
  214. worker_is_busy(WORKER_JOB_RCV_DATA);
  215. worker_private->receptions++;
  216. struct web_client *w = (struct web_client *)pi->data;
  217. int fd = pi->fd;
  218. ssize_t bytes;
  219. bytes = web_client_receive(w);
  220. if (likely(bytes > 0)) {
  221. netdata_log_debug(D_WEB_CLIENT, "%llu: processing received data on fd %d.", w->id, fd);
  222. worker_is_idle();
  223. worker_is_busy(WORKER_JOB_PROCESS);
  224. web_client_process_request_from_web_server(w);
  225. if (unlikely(w->mode == HTTP_REQUEST_MODE_STREAM)) {
  226. web_client_send(w);
  227. }
  228. else if(unlikely(w->mode == HTTP_REQUEST_MODE_FILECOPY)) {
  229. if(w->pollinfo_filecopy_slot == 0) {
  230. netdata_log_debug(D_WEB_CLIENT, "%llu: FILECOPY DETECTED ON FD %d", w->id, pi->fd);
  231. if (unlikely(w->ifd != -1 && w->ifd != w->ofd && w->ifd != fd)) {
  232. // add a new socket to poll_events, with the same
  233. netdata_log_debug(D_WEB_CLIENT, "%llu: CREATING FILECOPY SLOT ON FD %d", w->id, pi->fd);
  234. POLLINFO *fpi = poll_add_fd(
  235. pi->p
  236. , w->ifd
  237. , pi->port_acl
  238. , 0
  239. , POLLINFO_FLAG_CLIENT_SOCKET
  240. , "FILENAME"
  241. , ""
  242. , ""
  243. , web_server_file_add_callback
  244. , web_server_file_del_callback
  245. , web_server_file_read_callback
  246. , web_server_file_write_callback
  247. , (void *) w
  248. );
  249. if(fpi)
  250. w->pollinfo_filecopy_slot = fpi->slot;
  251. else {
  252. netdata_log_error("Failed to add filecopy fd. Closing client.");
  253. ret = -1;
  254. goto cleanup;
  255. }
  256. }
  257. }
  258. }
  259. else {
  260. if(unlikely(w->ifd == fd && web_client_has_wait_receive(w)))
  261. *events |= POLLIN;
  262. }
  263. if(unlikely(w->ofd == fd && web_client_has_wait_send(w)))
  264. *events |= POLLOUT;
  265. } else if(unlikely(bytes < 0)) {
  266. ret = -1;
  267. goto cleanup;
  268. } else if (unlikely(bytes == 0)) {
  269. if(unlikely(w->ifd == fd && web_client_has_ssl_wait_receive(w)))
  270. *events |= POLLIN;
  271. if(unlikely(w->ofd == fd && web_client_has_ssl_wait_send(w)))
  272. *events |= POLLOUT;
  273. }
  274. ret = web_server_check_client_status(w);
  275. cleanup:
  276. worker_is_idle();
  277. return ret;
  278. }
  279. static int web_server_snd_callback(POLLINFO *pi, short int *events) {
  280. int retval = -1;
  281. worker_is_busy(WORKER_JOB_SND_DATA);
  282. worker_private->sends++;
  283. struct web_client *w = (struct web_client *)pi->data;
  284. int fd = pi->fd;
  285. netdata_log_debug(D_WEB_CLIENT, "%llu: sending data on fd %d.", w->id, fd);
  286. ssize_t ret = web_client_send(w);
  287. if(unlikely(ret < 0)) {
  288. retval = -1;
  289. goto cleanup;
  290. }
  291. if(unlikely(w->ifd == fd && web_client_has_wait_receive(w)))
  292. *events |= POLLIN;
  293. if(unlikely(w->ofd == fd && web_client_has_wait_send(w)))
  294. *events |= POLLOUT;
  295. retval = web_server_check_client_status(w);
  296. cleanup:
  297. worker_is_idle();
  298. return retval;
  299. }
  300. // ----------------------------------------------------------------------------
  301. // web server worker thread
  302. static void socket_listen_main_static_threaded_worker_cleanup(void *ptr) {
  303. worker_private = (struct web_server_static_threaded_worker *)ptr;
  304. netdata_log_info("stopped after %zu connects, %zu disconnects (max concurrent %zu), %zu receptions and %zu sends",
  305. worker_private->connected,
  306. worker_private->disconnected,
  307. worker_private->max_concurrent,
  308. worker_private->receptions,
  309. worker_private->sends
  310. );
  311. worker_private->running = 0;
  312. worker_unregister();
  313. }
  314. static bool web_server_should_stop(void) {
  315. return !service_running(SERVICE_WEB_SERVER);
  316. }
  317. void *socket_listen_main_static_threaded_worker(void *ptr) {
  318. worker_private = (struct web_server_static_threaded_worker *)ptr;
  319. worker_private->running = 1;
  320. worker_register("WEB");
  321. worker_register_job_name(WORKER_JOB_ADD_CONNECTION, "connect");
  322. worker_register_job_name(WORKER_JOB_DEL_COLLECTION, "disconnect");
  323. worker_register_job_name(WORKER_JOB_ADD_FILE, "file start");
  324. worker_register_job_name(WORKER_JOB_DEL_FILE, "file end");
  325. worker_register_job_name(WORKER_JOB_READ_FILE, "file read");
  326. worker_register_job_name(WORKER_JOB_WRITE_FILE, "file write");
  327. worker_register_job_name(WORKER_JOB_RCV_DATA, "receive");
  328. worker_register_job_name(WORKER_JOB_SND_DATA, "send");
  329. worker_register_job_name(WORKER_JOB_PROCESS, "process");
  330. netdata_thread_cleanup_push(socket_listen_main_static_threaded_worker_cleanup, ptr);
  331. poll_events(&api_sockets
  332. , web_server_add_callback
  333. , web_server_del_callback
  334. , web_server_rcv_callback
  335. , web_server_snd_callback
  336. , NULL
  337. , web_server_should_stop
  338. , web_allow_connections_from
  339. , web_allow_connections_dns
  340. , NULL
  341. , web_client_first_request_timeout
  342. , web_client_timeout
  343. , default_rrd_update_every * 1000 // timer_milliseconds
  344. , ptr // timer_data
  345. , worker_private->max_sockets
  346. );
  347. netdata_thread_cleanup_pop(1);
  348. return NULL;
  349. }
  350. // ----------------------------------------------------------------------------
  351. // web server main thread - also becomes a worker
  352. static void socket_listen_main_static_threaded_cleanup(void *ptr) {
  353. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  354. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  355. // int i, found = 0;
  356. // usec_t max = 2 * USEC_PER_SEC, step = 50000;
  357. //
  358. // // we start from 1, - 0 is self
  359. // for(i = 1; i < static_threaded_workers_count; i++) {
  360. // if(static_workers_private_data[i].running) {
  361. // found++;
  362. // netdata_log_info("stopping worker %d", i + 1);
  363. // netdata_thread_cancel(static_workers_private_data[i].thread);
  364. // }
  365. // else
  366. // netdata_log_info("found stopped worker %d", i + 1);
  367. // }
  368. //
  369. // while(found && max > 0) {
  370. // max -= step;
  371. // netdata_log_info("Waiting %d static web threads to finish...", found);
  372. // sleep_usec(step);
  373. // found = 0;
  374. //
  375. // // we start from 1, - 0 is self
  376. // for(i = 1; i < static_threaded_workers_count; i++) {
  377. // if (static_workers_private_data[i].running)
  378. // found++;
  379. // }
  380. // }
  381. //
  382. // if(found)
  383. // netdata_log_error("%d static web threads are taking too long to finish. Giving up.", found);
  384. netdata_log_info("closing all web server sockets...");
  385. listen_sockets_close(&api_sockets);
  386. netdata_log_info("all static web threads stopped.");
  387. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  388. }
  389. void *socket_listen_main_static_threaded(void *ptr) {
  390. netdata_thread_cleanup_push(socket_listen_main_static_threaded_cleanup, ptr);
  391. web_server_mode = WEB_SERVER_MODE_STATIC_THREADED;
  392. if(!api_sockets.opened)
  393. fatal("LISTENER: no listen sockets available.");
  394. #ifdef ENABLE_HTTPS
  395. netdata_ssl_validate_certificate = !config_get_boolean(CONFIG_SECTION_WEB, "ssl skip certificate verification", !netdata_ssl_validate_certificate);
  396. if(!netdata_ssl_validate_certificate_sender)
  397. netdata_log_info("SSL: web server will skip SSL certificates verification.");
  398. netdata_ssl_initialize_ctx(NETDATA_SSL_WEB_SERVER_CTX);
  399. #endif
  400. // 6 threads is the optimal value
  401. // since 6 are the parallel connections browsers will do
  402. // so, if the machine has more CPUs, avoid using resources unnecessarily
  403. int def_thread_count = MIN(get_netdata_cpus(), 6);
  404. if (!strcmp(config_get(CONFIG_SECTION_WEB, "mode", ""),"single-threaded")) {
  405. netdata_log_info("Running web server with one thread, because mode is single-threaded");
  406. config_set(CONFIG_SECTION_WEB, "mode", "static-threaded");
  407. def_thread_count = 1;
  408. }
  409. static_threaded_workers_count = config_get_number(CONFIG_SECTION_WEB, "web server threads", def_thread_count);
  410. if (static_threaded_workers_count < 1) static_threaded_workers_count = 1;
  411. #ifdef ENABLE_HTTPS
  412. // See https://github.com/netdata/netdata/issues/11081#issuecomment-831998240 for more details
  413. if (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110) {
  414. static_threaded_workers_count = 1;
  415. netdata_log_info("You are running an OpenSSL older than 1.1.0, web server will not enable multithreading.");
  416. }
  417. #endif
  418. size_t max_sockets = (size_t)config_get_number(CONFIG_SECTION_WEB, "web server max sockets",
  419. (long long int)(rlimit_nofile.rlim_cur / 4));
  420. static_workers_private_data = callocz((size_t)static_threaded_workers_count,
  421. sizeof(struct web_server_static_threaded_worker));
  422. int i;
  423. for (i = 1; i < static_threaded_workers_count; i++) {
  424. static_workers_private_data[i].id = i;
  425. static_workers_private_data[i].max_sockets = max_sockets / static_threaded_workers_count;
  426. char tag[50 + 1];
  427. snprintfz(tag, sizeof(tag) - 1, "WEB[%d]", i+1);
  428. netdata_log_info("starting worker %d", i+1);
  429. netdata_thread_create(&static_workers_private_data[i].thread, tag, NETDATA_THREAD_OPTION_DEFAULT,
  430. socket_listen_main_static_threaded_worker, (void *)&static_workers_private_data[i]);
  431. }
  432. // and the main one
  433. static_workers_private_data[0].max_sockets = max_sockets / static_threaded_workers_count;
  434. socket_listen_main_static_threaded_worker((void *)&static_workers_private_data[0]);
  435. netdata_thread_cleanup_pop(1);
  436. return NULL;
  437. }