static-threaded.c 19 KB

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