static-threaded.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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_or_allocate();
  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. web_client_initialize_connection(w);
  34. w->pollinfo_slot = pi->slot;
  35. return(w);
  36. }
  37. // --------------------------------------------------------------------------------------
  38. // the main socket listener - STATIC-THREADED
  39. struct web_server_static_threaded_worker {
  40. netdata_thread_t thread;
  41. int id;
  42. int running;
  43. size_t max_sockets;
  44. volatile size_t connected;
  45. volatile size_t disconnected;
  46. volatile size_t receptions;
  47. volatile size_t sends;
  48. volatile size_t max_concurrent;
  49. volatile size_t files_read;
  50. volatile size_t file_reads;
  51. };
  52. static long long static_threaded_workers_count = 1;
  53. static struct web_server_static_threaded_worker *static_workers_private_data = NULL;
  54. static __thread struct web_server_static_threaded_worker *worker_private = NULL;
  55. // ----------------------------------------------------------------------------
  56. static inline int web_server_check_client_status(struct web_client *w) {
  57. if(unlikely(web_client_check_dead(w) || (!web_client_has_wait_receive(w) && !web_client_has_wait_send(w))))
  58. return -1;
  59. return 0;
  60. }
  61. // ----------------------------------------------------------------------------
  62. // web server files
  63. static void *web_server_file_add_callback(POLLINFO *pi, short int *events, void *data) {
  64. struct web_client *w = (struct web_client *)data;
  65. worker_is_busy(WORKER_JOB_ADD_FILE);
  66. worker_private->files_read++;
  67. debug(D_WEB_CLIENT, "%llu: ADDED FILE READ ON FD %d", w->id, pi->fd);
  68. *events = POLLIN;
  69. pi->data = w;
  70. worker_is_idle();
  71. return w;
  72. }
  73. static void web_server_file_del_callback(POLLINFO *pi) {
  74. struct web_client *w = (struct web_client *)pi->data;
  75. debug(D_WEB_CLIENT, "%llu: RELEASE FILE READ ON FD %d", w->id, pi->fd);
  76. worker_is_busy(WORKER_JOB_DEL_FILE);
  77. w->pollinfo_filecopy_slot = 0;
  78. if(unlikely(!w->pollinfo_slot)) {
  79. debug(D_WEB_CLIENT, "%llu: CROSS WEB CLIENT CLEANUP (iFD %d, oFD %d)", w->id, pi->fd, w->ofd);
  80. web_client_release(w);
  81. }
  82. worker_is_idle();
  83. }
  84. static int web_server_file_read_callback(POLLINFO *pi, short int *events) {
  85. int retval = -1;
  86. struct web_client *w = (struct web_client *)pi->data;
  87. worker_is_busy(WORKER_JOB_READ_FILE);
  88. // if there is no POLLINFO linked to this, it means the client disconnected
  89. // stop the file reading too
  90. if(unlikely(!w->pollinfo_slot)) {
  91. debug(D_WEB_CLIENT, "%llu: PREVENTED ATTEMPT TO READ FILE ON FD %d, ON CLOSED WEB CLIENT", w->id, pi->fd);
  92. retval = -1;
  93. goto cleanup;
  94. }
  95. if(unlikely(w->mode != WEB_CLIENT_MODE_FILECOPY || w->ifd == w->ofd)) {
  96. debug(D_WEB_CLIENT, "%llu: PREVENTED ATTEMPT TO READ FILE ON FD %d, ON NON-FILECOPY WEB CLIENT", w->id, pi->fd);
  97. retval = -1;
  98. goto cleanup;
  99. }
  100. debug(D_WEB_CLIENT, "%llu: READING FILE ON FD %d", w->id, pi->fd);
  101. worker_private->file_reads++;
  102. ssize_t ret = unlikely(web_client_read_file(w));
  103. if(likely(web_client_has_wait_send(w))) {
  104. POLLJOB *p = pi->p; // our POLLJOB
  105. POLLINFO *wpi = pollinfo_from_slot(p, w->pollinfo_slot); // POLLINFO of the client socket
  106. debug(D_WEB_CLIENT, "%llu: SIGNALING W TO SEND (iFD %d, oFD %d)", w->id, pi->fd, wpi->fd);
  107. p->fds[wpi->slot].events |= POLLOUT;
  108. }
  109. if(unlikely(ret <= 0 || w->ifd == w->ofd)) {
  110. debug(D_WEB_CLIENT, "%llu: DONE READING FILE ON FD %d", w->id, pi->fd);
  111. retval = -1;
  112. goto cleanup;
  113. }
  114. *events = POLLIN;
  115. retval = 0;
  116. cleanup:
  117. worker_is_idle();
  118. return retval;
  119. }
  120. static int web_server_file_write_callback(POLLINFO *pi, short int *events) {
  121. (void)pi;
  122. (void)events;
  123. worker_is_busy(WORKER_JOB_WRITE_FILE);
  124. error("Writing to web files is not supported!");
  125. worker_is_idle();
  126. return -1;
  127. }
  128. // ----------------------------------------------------------------------------
  129. // web server clients
  130. static void *web_server_add_callback(POLLINFO *pi, short int *events, void *data) {
  131. (void)data; // Suppress warning on unused argument
  132. worker_is_busy(WORKER_JOB_ADD_CONNECTION);
  133. worker_private->connected++;
  134. size_t concurrent = worker_private->connected - worker_private->disconnected;
  135. if(unlikely(concurrent > worker_private->max_concurrent))
  136. worker_private->max_concurrent = concurrent;
  137. *events = POLLIN;
  138. debug(D_WEB_CLIENT_ACCESS, "LISTENER on %d: new connection.", pi->fd);
  139. struct web_client *w = web_client_create_on_fd(pi);
  140. if (!strncmp(pi->client_port, "UNIX", 4)) {
  141. web_client_set_unix(w);
  142. } else {
  143. web_client_set_tcp(w);
  144. }
  145. #ifdef ENABLE_HTTPS
  146. if ((!web_client_check_unix(w)) && (netdata_ssl_srv_ctx)) {
  147. if( sock_delnonblock(w->ifd) < 0 ){
  148. error("Web server cannot remove the non-blocking flag from socket %d",w->ifd);
  149. }
  150. //Read the first 7 bytes from the message, but the message
  151. //is not removed from the queue, because we are using MSG_PEEK
  152. char test[8];
  153. if ( recv(w->ifd,test, 7,MSG_PEEK) == 7 ) {
  154. test[7] = 0x00;
  155. }
  156. else {
  157. //Case I do not have success to read 7 bytes,
  158. //this means that the mensage was not completely read, so
  159. //I cannot identify it yet.
  160. sock_setnonblock(w->ifd);
  161. goto cleanup;
  162. }
  163. //The next two ifs are not together because I am reusing SSL structure
  164. if (!w->ssl.conn)
  165. {
  166. w->ssl.conn = SSL_new(netdata_ssl_srv_ctx);
  167. if ( w->ssl.conn ) {
  168. SSL_set_accept_state(w->ssl.conn);
  169. } else {
  170. error("Failed to create SSL context on socket fd %d.", w->ifd);
  171. if (test[0] < 0x18){
  172. WEB_CLIENT_IS_DEAD(w);
  173. sock_setnonblock(w->ifd);
  174. goto cleanup;
  175. }
  176. }
  177. }
  178. if (w->ssl.conn) {
  179. if (SSL_set_fd(w->ssl.conn, w->ifd) != 1) {
  180. error("Failed to set the socket to the SSL on socket fd %d.", w->ifd);
  181. //The client is not set dead, because I received a normal HTTP request
  182. //instead a Client Hello(HTTPS).
  183. if ( test[0] < 0x18 ){
  184. WEB_CLIENT_IS_DEAD(w);
  185. }
  186. }
  187. else{
  188. w->ssl.flags = security_process_accept(w->ssl.conn, (int)test[0]);
  189. }
  190. }
  191. sock_setnonblock(w->ifd);
  192. } else{
  193. w->ssl.flags = NETDATA_SSL_NO_HANDSHAKE;
  194. }
  195. #endif
  196. debug(D_WEB_CLIENT, "%llu: ADDED CLIENT FD %d", w->id, pi->fd);
  197. cleanup:
  198. worker_is_idle();
  199. return w;
  200. }
  201. // TCP client disconnected
  202. static void web_server_del_callback(POLLINFO *pi) {
  203. worker_is_busy(WORKER_JOB_DEL_COLLECTION);
  204. worker_private->disconnected++;
  205. struct web_client *w = (struct web_client *)pi->data;
  206. w->pollinfo_slot = 0;
  207. if(unlikely(w->pollinfo_filecopy_slot)) {
  208. POLLINFO *fpi = pollinfo_from_slot(pi->p, w->pollinfo_filecopy_slot); // POLLINFO of the client socket
  209. (void)fpi;
  210. debug(D_WEB_CLIENT, "%llu: THE CLIENT WILL BE FRED BY READING FILE JOB ON FD %d", w->id, fpi->fd);
  211. }
  212. else {
  213. if(web_client_flag_check(w, WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET))
  214. pi->flags |= POLLINFO_FLAG_DONT_CLOSE;
  215. debug(D_WEB_CLIENT, "%llu: CLOSING CLIENT FD %d", w->id, pi->fd);
  216. web_client_release(w);
  217. }
  218. worker_is_idle();
  219. }
  220. static int web_server_rcv_callback(POLLINFO *pi, short int *events) {
  221. int ret = -1;
  222. worker_is_busy(WORKER_JOB_RCV_DATA);
  223. worker_private->receptions++;
  224. struct web_client *w = (struct web_client *)pi->data;
  225. int fd = pi->fd;
  226. if(unlikely(web_client_receive(w) < 0)) {
  227. ret = -1;
  228. goto cleanup;
  229. }
  230. debug(D_WEB_CLIENT, "%llu: processing received data on fd %d.", w->id, fd);
  231. worker_is_idle();
  232. worker_is_busy(WORKER_JOB_PROCESS);
  233. web_client_process_request(w);
  234. if (unlikely(w->mode == WEB_CLIENT_MODE_STREAM)) {
  235. web_client_send(w);
  236. }
  237. else if(unlikely(w->mode == WEB_CLIENT_MODE_FILECOPY)) {
  238. if(w->pollinfo_filecopy_slot == 0) {
  239. debug(D_WEB_CLIENT, "%llu: FILECOPY DETECTED ON FD %d", w->id, pi->fd);
  240. if (unlikely(w->ifd != -1 && w->ifd != w->ofd && w->ifd != fd)) {
  241. // add a new socket to poll_events, with the same
  242. debug(D_WEB_CLIENT, "%llu: CREATING FILECOPY SLOT ON FD %d", w->id, pi->fd);
  243. POLLINFO *fpi = poll_add_fd(
  244. pi->p
  245. , w->ifd
  246. , pi->port_acl
  247. , 0
  248. , POLLINFO_FLAG_CLIENT_SOCKET
  249. , "FILENAME"
  250. , ""
  251. , ""
  252. , web_server_file_add_callback
  253. , web_server_file_del_callback
  254. , web_server_file_read_callback
  255. , web_server_file_write_callback
  256. , (void *) w
  257. );
  258. if(fpi)
  259. w->pollinfo_filecopy_slot = fpi->slot;
  260. else {
  261. error("Failed to add filecopy fd. Closing client.");
  262. ret = -1;
  263. goto cleanup;
  264. }
  265. }
  266. }
  267. }
  268. else {
  269. if(unlikely(w->ifd == fd && web_client_has_wait_receive(w)))
  270. *events |= POLLIN;
  271. }
  272. if(unlikely(w->ofd == fd && web_client_has_wait_send(w)))
  273. *events |= POLLOUT;
  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. debug(D_WEB_CLIENT, "%llu: sending data on fd %d.", w->id, fd);
  286. int 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. info("freeing local web clients cache...");
  305. web_client_cache_destroy();
  306. info("stopped after %zu connects, %zu disconnects (max concurrent %zu), %zu receptions and %zu sends",
  307. worker_private->connected,
  308. worker_private->disconnected,
  309. worker_private->max_concurrent,
  310. worker_private->receptions,
  311. worker_private->sends
  312. );
  313. worker_private->running = 0;
  314. worker_unregister();
  315. }
  316. static bool web_server_should_stop(void) {
  317. return !service_running(SERVICE_WEB_SERVER);
  318. }
  319. void *socket_listen_main_static_threaded_worker(void *ptr) {
  320. worker_private = (struct web_server_static_threaded_worker *)ptr;
  321. worker_private->running = 1;
  322. worker_register("WEB");
  323. worker_register_job_name(WORKER_JOB_ADD_CONNECTION, "connect");
  324. worker_register_job_name(WORKER_JOB_DEL_COLLECTION, "disconnect");
  325. worker_register_job_name(WORKER_JOB_ADD_FILE, "file start");
  326. worker_register_job_name(WORKER_JOB_DEL_FILE, "file end");
  327. worker_register_job_name(WORKER_JOB_READ_FILE, "file read");
  328. worker_register_job_name(WORKER_JOB_WRITE_FILE, "file write");
  329. worker_register_job_name(WORKER_JOB_RCV_DATA, "receive");
  330. worker_register_job_name(WORKER_JOB_SND_DATA, "send");
  331. worker_register_job_name(WORKER_JOB_PROCESS, "process");
  332. netdata_thread_cleanup_push(socket_listen_main_static_threaded_worker_cleanup, ptr);
  333. poll_events(&api_sockets
  334. , web_server_add_callback
  335. , web_server_del_callback
  336. , web_server_rcv_callback
  337. , web_server_snd_callback
  338. , NULL
  339. , web_server_should_stop
  340. , web_allow_connections_from
  341. , web_allow_connections_dns
  342. , NULL
  343. , web_client_first_request_timeout
  344. , web_client_timeout
  345. , default_rrd_update_every * 1000 // timer_milliseconds
  346. , ptr // timer_data
  347. , worker_private->max_sockets
  348. );
  349. netdata_thread_cleanup_pop(1);
  350. return NULL;
  351. }
  352. // ----------------------------------------------------------------------------
  353. // web server main thread - also becomes a worker
  354. static void socket_listen_main_static_threaded_cleanup(void *ptr) {
  355. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  356. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  357. // int i, found = 0;
  358. // usec_t max = 2 * USEC_PER_SEC, step = 50000;
  359. //
  360. // // we start from 1, - 0 is self
  361. // for(i = 1; i < static_threaded_workers_count; i++) {
  362. // if(static_workers_private_data[i].running) {
  363. // found++;
  364. // info("stopping worker %d", i + 1);
  365. // netdata_thread_cancel(static_workers_private_data[i].thread);
  366. // }
  367. // else
  368. // info("found stopped worker %d", i + 1);
  369. // }
  370. //
  371. // while(found && max > 0) {
  372. // max -= step;
  373. // info("Waiting %d static web threads to finish...", found);
  374. // sleep_usec(step);
  375. // found = 0;
  376. //
  377. // // we start from 1, - 0 is self
  378. // for(i = 1; i < static_threaded_workers_count; i++) {
  379. // if (static_workers_private_data[i].running)
  380. // found++;
  381. // }
  382. // }
  383. //
  384. // if(found)
  385. // error("%d static web threads are taking too long to finish. Giving up.", found);
  386. info("closing all web server sockets...");
  387. listen_sockets_close(&api_sockets);
  388. info("all static web threads stopped.");
  389. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  390. }
  391. void *socket_listen_main_static_threaded(void *ptr) {
  392. netdata_thread_cleanup_push(socket_listen_main_static_threaded_cleanup, ptr);
  393. web_server_mode = WEB_SERVER_MODE_STATIC_THREADED;
  394. if(!api_sockets.opened)
  395. fatal("LISTENER: no listen sockets available.");
  396. #ifdef ENABLE_HTTPS
  397. security_start_ssl(NETDATA_SSL_CONTEXT_SERVER);
  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. 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. 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. 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. }