static-threaded.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. #ifdef ENABLE_HTTPS
  184. cleanup:
  185. #endif
  186. worker_is_idle();
  187. return w;
  188. }
  189. // TCP client disconnected
  190. static void web_server_del_callback(POLLINFO *pi) {
  191. worker_is_busy(WORKER_JOB_DEL_COLLECTION);
  192. worker_private->disconnected++;
  193. struct web_client *w = (struct web_client *)pi->data;
  194. w->pollinfo_slot = 0;
  195. if(unlikely(w->pollinfo_filecopy_slot)) {
  196. POLLINFO *fpi = pollinfo_from_slot(pi->p, w->pollinfo_filecopy_slot); // POLLINFO of the client socket
  197. (void)fpi;
  198. netdata_log_debug(D_WEB_CLIENT, "%llu: THE CLIENT WILL BE FRED BY READING FILE JOB ON FD %d", w->id, fpi->fd);
  199. }
  200. else {
  201. if(web_client_flag_check(w, WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET))
  202. pi->flags |= POLLINFO_FLAG_DONT_CLOSE;
  203. netdata_log_debug(D_WEB_CLIENT, "%llu: CLOSING CLIENT FD %d", w->id, pi->fd);
  204. web_server_log_connection(w, "DISCONNECTED");
  205. web_client_request_done(w);
  206. web_client_release_to_cache(w);
  207. global_statistics_web_client_disconnected();
  208. }
  209. worker_is_idle();
  210. }
  211. static int web_server_rcv_callback(POLLINFO *pi, short int *events) {
  212. int ret = -1;
  213. worker_is_busy(WORKER_JOB_RCV_DATA);
  214. worker_private->receptions++;
  215. struct web_client *w = (struct web_client *)pi->data;
  216. int fd = pi->fd;
  217. ssize_t bytes;
  218. bytes = web_client_receive(w);
  219. if (likely(bytes > 0)) {
  220. netdata_log_debug(D_WEB_CLIENT, "%llu: processing received data on fd %d.", w->id, fd);
  221. worker_is_idle();
  222. worker_is_busy(WORKER_JOB_PROCESS);
  223. web_client_process_request(w);
  224. if (unlikely(w->mode == WEB_CLIENT_MODE_STREAM)) {
  225. web_client_send(w);
  226. }
  227. else if(unlikely(w->mode == WEB_CLIENT_MODE_FILECOPY)) {
  228. if(w->pollinfo_filecopy_slot == 0) {
  229. netdata_log_debug(D_WEB_CLIENT, "%llu: FILECOPY DETECTED ON FD %d", w->id, pi->fd);
  230. if (unlikely(w->ifd != -1 && w->ifd != w->ofd && w->ifd != fd)) {
  231. // add a new socket to poll_events, with the same
  232. netdata_log_debug(D_WEB_CLIENT, "%llu: CREATING FILECOPY SLOT ON FD %d", w->id, pi->fd);
  233. POLLINFO *fpi = poll_add_fd(
  234. pi->p
  235. , w->ifd
  236. , pi->port_acl
  237. , 0
  238. , POLLINFO_FLAG_CLIENT_SOCKET
  239. , "FILENAME"
  240. , ""
  241. , ""
  242. , web_server_file_add_callback
  243. , web_server_file_del_callback
  244. , web_server_file_read_callback
  245. , web_server_file_write_callback
  246. , (void *) w
  247. );
  248. if(fpi)
  249. w->pollinfo_filecopy_slot = fpi->slot;
  250. else {
  251. netdata_log_error("Failed to add filecopy fd. Closing client.");
  252. ret = -1;
  253. goto cleanup;
  254. }
  255. }
  256. }
  257. }
  258. else {
  259. if(unlikely(w->ifd == fd && web_client_has_wait_receive(w)))
  260. *events |= POLLIN;
  261. }
  262. if(unlikely(w->ofd == fd && web_client_has_wait_send(w)))
  263. *events |= POLLOUT;
  264. } else if(unlikely(bytes < 0)) {
  265. ret = -1;
  266. goto cleanup;
  267. } else if (unlikely(bytes == 0)) {
  268. if(unlikely(w->ifd == fd && web_client_has_ssl_wait_receive(w)))
  269. *events |= POLLIN;
  270. if(unlikely(w->ofd == fd && web_client_has_ssl_wait_send(w)))
  271. *events |= POLLOUT;
  272. }
  273. ret = web_server_check_client_status(w);
  274. cleanup:
  275. worker_is_idle();
  276. return ret;
  277. }
  278. static int web_server_snd_callback(POLLINFO *pi, short int *events) {
  279. int retval = -1;
  280. worker_is_busy(WORKER_JOB_SND_DATA);
  281. worker_private->sends++;
  282. struct web_client *w = (struct web_client *)pi->data;
  283. int fd = pi->fd;
  284. netdata_log_debug(D_WEB_CLIENT, "%llu: sending data on fd %d.", w->id, fd);
  285. ssize_t ret = web_client_send(w);
  286. if(unlikely(ret < 0)) {
  287. retval = -1;
  288. goto cleanup;
  289. }
  290. if(unlikely(w->ifd == fd && web_client_has_wait_receive(w)))
  291. *events |= POLLIN;
  292. if(unlikely(w->ofd == fd && web_client_has_wait_send(w)))
  293. *events |= POLLOUT;
  294. retval = web_server_check_client_status(w);
  295. cleanup:
  296. worker_is_idle();
  297. return retval;
  298. }
  299. // ----------------------------------------------------------------------------
  300. // web server worker thread
  301. static void socket_listen_main_static_threaded_worker_cleanup(void *ptr) {
  302. worker_private = (struct web_server_static_threaded_worker *)ptr;
  303. netdata_log_info("stopped after %zu connects, %zu disconnects (max concurrent %zu), %zu receptions and %zu sends",
  304. worker_private->connected,
  305. worker_private->disconnected,
  306. worker_private->max_concurrent,
  307. worker_private->receptions,
  308. worker_private->sends
  309. );
  310. worker_private->running = 0;
  311. worker_unregister();
  312. }
  313. static bool web_server_should_stop(void) {
  314. return !service_running(SERVICE_WEB_SERVER);
  315. }
  316. void *socket_listen_main_static_threaded_worker(void *ptr) {
  317. worker_private = (struct web_server_static_threaded_worker *)ptr;
  318. worker_private->running = 1;
  319. worker_register("WEB");
  320. worker_register_job_name(WORKER_JOB_ADD_CONNECTION, "connect");
  321. worker_register_job_name(WORKER_JOB_DEL_COLLECTION, "disconnect");
  322. worker_register_job_name(WORKER_JOB_ADD_FILE, "file start");
  323. worker_register_job_name(WORKER_JOB_DEL_FILE, "file end");
  324. worker_register_job_name(WORKER_JOB_READ_FILE, "file read");
  325. worker_register_job_name(WORKER_JOB_WRITE_FILE, "file write");
  326. worker_register_job_name(WORKER_JOB_RCV_DATA, "receive");
  327. worker_register_job_name(WORKER_JOB_SND_DATA, "send");
  328. worker_register_job_name(WORKER_JOB_PROCESS, "process");
  329. netdata_thread_cleanup_push(socket_listen_main_static_threaded_worker_cleanup, ptr);
  330. poll_events(&api_sockets
  331. , web_server_add_callback
  332. , web_server_del_callback
  333. , web_server_rcv_callback
  334. , web_server_snd_callback
  335. , NULL
  336. , web_server_should_stop
  337. , web_allow_connections_from
  338. , web_allow_connections_dns
  339. , NULL
  340. , web_client_first_request_timeout
  341. , web_client_timeout
  342. , default_rrd_update_every * 1000 // timer_milliseconds
  343. , ptr // timer_data
  344. , worker_private->max_sockets
  345. );
  346. netdata_thread_cleanup_pop(1);
  347. return NULL;
  348. }
  349. // ----------------------------------------------------------------------------
  350. // web server main thread - also becomes a worker
  351. static void socket_listen_main_static_threaded_cleanup(void *ptr) {
  352. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  353. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  354. // int i, found = 0;
  355. // usec_t max = 2 * USEC_PER_SEC, step = 50000;
  356. //
  357. // // we start from 1, - 0 is self
  358. // for(i = 1; i < static_threaded_workers_count; i++) {
  359. // if(static_workers_private_data[i].running) {
  360. // found++;
  361. // netdata_log_info("stopping worker %d", i + 1);
  362. // netdata_thread_cancel(static_workers_private_data[i].thread);
  363. // }
  364. // else
  365. // netdata_log_info("found stopped worker %d", i + 1);
  366. // }
  367. //
  368. // while(found && max > 0) {
  369. // max -= step;
  370. // netdata_log_info("Waiting %d static web threads to finish...", found);
  371. // sleep_usec(step);
  372. // found = 0;
  373. //
  374. // // we start from 1, - 0 is self
  375. // for(i = 1; i < static_threaded_workers_count; i++) {
  376. // if (static_workers_private_data[i].running)
  377. // found++;
  378. // }
  379. // }
  380. //
  381. // if(found)
  382. // netdata_log_error("%d static web threads are taking too long to finish. Giving up.", found);
  383. netdata_log_info("closing all web server sockets...");
  384. listen_sockets_close(&api_sockets);
  385. netdata_log_info("all static web threads stopped.");
  386. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  387. }
  388. void *socket_listen_main_static_threaded(void *ptr) {
  389. netdata_thread_cleanup_push(socket_listen_main_static_threaded_cleanup, ptr);
  390. web_server_mode = WEB_SERVER_MODE_STATIC_THREADED;
  391. if(!api_sockets.opened)
  392. fatal("LISTENER: no listen sockets available.");
  393. #ifdef ENABLE_HTTPS
  394. netdata_ssl_validate_certificate = !config_get_boolean(CONFIG_SECTION_WEB, "ssl skip certificate verification", !netdata_ssl_validate_certificate);
  395. if(!netdata_ssl_validate_certificate_sender)
  396. netdata_log_info("SSL: web server will skip SSL certificates verification.");
  397. netdata_ssl_initialize_ctx(NETDATA_SSL_WEB_SERVER_CTX);
  398. #endif
  399. // 6 threads is the optimal value
  400. // since 6 are the parallel connections browsers will do
  401. // so, if the machine has more CPUs, avoid using resources unnecessarily
  402. int def_thread_count = MIN(get_netdata_cpus(), 6);
  403. if (!strcmp(config_get(CONFIG_SECTION_WEB, "mode", ""),"single-threaded")) {
  404. netdata_log_info("Running web server with one thread, because mode is single-threaded");
  405. config_set(CONFIG_SECTION_WEB, "mode", "static-threaded");
  406. def_thread_count = 1;
  407. }
  408. static_threaded_workers_count = config_get_number(CONFIG_SECTION_WEB, "web server threads", def_thread_count);
  409. if (static_threaded_workers_count < 1) static_threaded_workers_count = 1;
  410. #ifdef ENABLE_HTTPS
  411. // See https://github.com/netdata/netdata/issues/11081#issuecomment-831998240 for more details
  412. if (OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_110) {
  413. static_threaded_workers_count = 1;
  414. netdata_log_info("You are running an OpenSSL older than 1.1.0, web server will not enable multithreading.");
  415. }
  416. #endif
  417. size_t max_sockets = (size_t)config_get_number(CONFIG_SECTION_WEB, "web server max sockets",
  418. (long long int)(rlimit_nofile.rlim_cur / 4));
  419. static_workers_private_data = callocz((size_t)static_threaded_workers_count,
  420. sizeof(struct web_server_static_threaded_worker));
  421. web_server_is_multithreaded = (static_threaded_workers_count > 1);
  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, 50, "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. }