111Sigor@sysoev.ru 211Sigor@sysoev.ru /* 311Sigor@sysoev.ru * Copyright (C) Igor Sysoev 411Sigor@sysoev.ru * Copyright (C) NGINX, Inc. 511Sigor@sysoev.ru */ 611Sigor@sysoev.ru 711Sigor@sysoev.ru #ifndef _NXT_PORT_H_INCLUDED_ 811Sigor@sysoev.ru #define _NXT_PORT_H_INCLUDED_ 911Sigor@sysoev.ru 1011Sigor@sysoev.ru 11320Smax.romanov@nginx.com struct nxt_port_handlers_s { 12320Smax.romanov@nginx.com /* RPC responses. */ 13320Smax.romanov@nginx.com nxt_port_handler_t rpc_ready; 14320Smax.romanov@nginx.com nxt_port_handler_t rpc_error; 15320Smax.romanov@nginx.com 16320Smax.romanov@nginx.com /* Main process RPC requests. */ 17320Smax.romanov@nginx.com nxt_port_handler_t start_worker; 18320Smax.romanov@nginx.com nxt_port_handler_t socket; 19320Smax.romanov@nginx.com nxt_port_handler_t modules; 20320Smax.romanov@nginx.com nxt_port_handler_t conf_store; 21774Svbart@nginx.com nxt_port_handler_t cert_get; 22774Svbart@nginx.com nxt_port_handler_t cert_delete; 23630Svbart@nginx.com nxt_port_handler_t access_log; 24320Smax.romanov@nginx.com 25320Smax.romanov@nginx.com /* File descriptor exchange. */ 26320Smax.romanov@nginx.com nxt_port_handler_t change_file; 27320Smax.romanov@nginx.com nxt_port_handler_t new_port; 28320Smax.romanov@nginx.com nxt_port_handler_t mmap; 29320Smax.romanov@nginx.com 30320Smax.romanov@nginx.com /* New process ready. */ 31320Smax.romanov@nginx.com nxt_port_handler_t process_ready; 32320Smax.romanov@nginx.com 33320Smax.romanov@nginx.com /* Process exit/crash notification. */ 34320Smax.romanov@nginx.com nxt_port_handler_t remove_pid; 35320Smax.romanov@nginx.com 36320Smax.romanov@nginx.com /* Stop process command. */ 37320Smax.romanov@nginx.com nxt_port_handler_t quit; 38320Smax.romanov@nginx.com 391131Smax.romanov@nginx.com /* Request headers. */ 401131Smax.romanov@nginx.com nxt_port_handler_t req_headers; 411131Smax.romanov@nginx.com 421131Smax.romanov@nginx.com /* Websocket frame. */ 431131Smax.romanov@nginx.com nxt_port_handler_t websocket_frame; 441131Smax.romanov@nginx.com 45320Smax.romanov@nginx.com /* Various data. */ 46320Smax.romanov@nginx.com nxt_port_handler_t data; 47*1321Smax.romanov@nginx.com 48*1321Smax.romanov@nginx.com nxt_port_handler_t oosm; 49*1321Smax.romanov@nginx.com nxt_port_handler_t shm_ack; 50320Smax.romanov@nginx.com }; 51320Smax.romanov@nginx.com 52320Smax.romanov@nginx.com 53320Smax.romanov@nginx.com #define nxt_port_handler_idx(name) \ 54389Smax.romanov@nginx.com ( offsetof(nxt_port_handlers_t, name) / sizeof(nxt_port_handler_t) ) 55320Smax.romanov@nginx.com 56320Smax.romanov@nginx.com 5742Smax.romanov@nginx.com typedef enum { 58189Smax.romanov@nginx.com NXT_PORT_MSG_LAST = 0x100, 59189Smax.romanov@nginx.com NXT_PORT_MSG_CLOSE_FD = 0x200, 60205Smax.romanov@nginx.com NXT_PORT_MSG_SYNC = 0x400, 61189Smax.romanov@nginx.com 62189Smax.romanov@nginx.com NXT_PORT_MSG_MASK = 0xFF, 63189Smax.romanov@nginx.com 64320Smax.romanov@nginx.com _NXT_PORT_MSG_RPC_READY = nxt_port_handler_idx(rpc_ready), 65320Smax.romanov@nginx.com _NXT_PORT_MSG_RPC_ERROR = nxt_port_handler_idx(rpc_error), 66320Smax.romanov@nginx.com 67320Smax.romanov@nginx.com _NXT_PORT_MSG_START_WORKER = nxt_port_handler_idx(start_worker), 68320Smax.romanov@nginx.com _NXT_PORT_MSG_SOCKET = nxt_port_handler_idx(socket), 69320Smax.romanov@nginx.com _NXT_PORT_MSG_MODULES = nxt_port_handler_idx(modules), 70320Smax.romanov@nginx.com _NXT_PORT_MSG_CONF_STORE = nxt_port_handler_idx(conf_store), 71774Svbart@nginx.com _NXT_PORT_MSG_CERT_GET = nxt_port_handler_idx(cert_get), 72774Svbart@nginx.com _NXT_PORT_MSG_CERT_DELETE = nxt_port_handler_idx(cert_delete), 73630Svbart@nginx.com _NXT_PORT_MSG_ACCESS_LOG = nxt_port_handler_idx(access_log), 74320Smax.romanov@nginx.com 75320Smax.romanov@nginx.com _NXT_PORT_MSG_CHANGE_FILE = nxt_port_handler_idx(change_file), 76320Smax.romanov@nginx.com _NXT_PORT_MSG_NEW_PORT = nxt_port_handler_idx(new_port), 77320Smax.romanov@nginx.com _NXT_PORT_MSG_MMAP = nxt_port_handler_idx(mmap), 7842Smax.romanov@nginx.com 79320Smax.romanov@nginx.com _NXT_PORT_MSG_PROCESS_READY = nxt_port_handler_idx(process_ready), 80320Smax.romanov@nginx.com _NXT_PORT_MSG_REMOVE_PID = nxt_port_handler_idx(remove_pid), 81320Smax.romanov@nginx.com _NXT_PORT_MSG_QUIT = nxt_port_handler_idx(quit), 82320Smax.romanov@nginx.com 831131Smax.romanov@nginx.com _NXT_PORT_MSG_REQ_HEADERS = nxt_port_handler_idx(req_headers), 841131Smax.romanov@nginx.com _NXT_PORT_MSG_WEBSOCKET = nxt_port_handler_idx(websocket_frame), 851131Smax.romanov@nginx.com 86320Smax.romanov@nginx.com _NXT_PORT_MSG_DATA = nxt_port_handler_idx(data), 87189Smax.romanov@nginx.com 88*1321Smax.romanov@nginx.com _NXT_PORT_MSG_OOSM = nxt_port_handler_idx(oosm), 89*1321Smax.romanov@nginx.com _NXT_PORT_MSG_SHM_ACK = nxt_port_handler_idx(shm_ack), 90*1321Smax.romanov@nginx.com 91613Svbart@nginx.com NXT_PORT_MSG_MAX = sizeof(nxt_port_handlers_t) 92613Svbart@nginx.com / sizeof(nxt_port_handler_t), 93320Smax.romanov@nginx.com 94320Smax.romanov@nginx.com NXT_PORT_MSG_RPC_READY = _NXT_PORT_MSG_RPC_READY, 95320Smax.romanov@nginx.com NXT_PORT_MSG_RPC_READY_LAST = _NXT_PORT_MSG_RPC_READY | NXT_PORT_MSG_LAST, 96320Smax.romanov@nginx.com NXT_PORT_MSG_RPC_ERROR = _NXT_PORT_MSG_RPC_ERROR | NXT_PORT_MSG_LAST, 97320Smax.romanov@nginx.com 98613Svbart@nginx.com NXT_PORT_MSG_START_WORKER = _NXT_PORT_MSG_START_WORKER 99613Svbart@nginx.com | NXT_PORT_MSG_LAST, 100198Sigor@sysoev.ru NXT_PORT_MSG_SOCKET = _NXT_PORT_MSG_SOCKET | NXT_PORT_MSG_LAST, 101216Sigor@sysoev.ru NXT_PORT_MSG_MODULES = _NXT_PORT_MSG_MODULES | NXT_PORT_MSG_LAST, 102314Svbart@nginx.com NXT_PORT_MSG_CONF_STORE = _NXT_PORT_MSG_CONF_STORE | NXT_PORT_MSG_LAST, 103774Svbart@nginx.com NXT_PORT_MSG_CERT_GET = _NXT_PORT_MSG_CERT_GET | NXT_PORT_MSG_LAST, 104774Svbart@nginx.com NXT_PORT_MSG_CERT_DELETE = _NXT_PORT_MSG_CERT_DELETE | NXT_PORT_MSG_LAST, 105630Svbart@nginx.com NXT_PORT_MSG_ACCESS_LOG = _NXT_PORT_MSG_ACCESS_LOG | NXT_PORT_MSG_LAST, 106320Smax.romanov@nginx.com 107320Smax.romanov@nginx.com NXT_PORT_MSG_CHANGE_FILE = _NXT_PORT_MSG_CHANGE_FILE | NXT_PORT_MSG_LAST, 108320Smax.romanov@nginx.com NXT_PORT_MSG_NEW_PORT = _NXT_PORT_MSG_NEW_PORT | NXT_PORT_MSG_LAST, 109613Svbart@nginx.com NXT_PORT_MSG_MMAP = _NXT_PORT_MSG_MMAP | NXT_PORT_MSG_LAST 110613Svbart@nginx.com | NXT_PORT_MSG_CLOSE_FD | NXT_PORT_MSG_SYNC, 111320Smax.romanov@nginx.com 112613Svbart@nginx.com NXT_PORT_MSG_PROCESS_READY = _NXT_PORT_MSG_PROCESS_READY 113613Svbart@nginx.com | NXT_PORT_MSG_LAST, 114320Smax.romanov@nginx.com NXT_PORT_MSG_QUIT = _NXT_PORT_MSG_QUIT | NXT_PORT_MSG_LAST, 115320Smax.romanov@nginx.com NXT_PORT_MSG_REMOVE_PID = _NXT_PORT_MSG_REMOVE_PID | NXT_PORT_MSG_LAST, 116320Smax.romanov@nginx.com 1171131Smax.romanov@nginx.com NXT_PORT_MSG_REQ_HEADERS = _NXT_PORT_MSG_REQ_HEADERS, 1181131Smax.romanov@nginx.com NXT_PORT_MSG_WEBSOCKET = _NXT_PORT_MSG_WEBSOCKET, 1191131Smax.romanov@nginx.com NXT_PORT_MSG_WEBSOCKET_LAST = _NXT_PORT_MSG_WEBSOCKET | NXT_PORT_MSG_LAST, 1201131Smax.romanov@nginx.com 121320Smax.romanov@nginx.com NXT_PORT_MSG_DATA = _NXT_PORT_MSG_DATA, 122320Smax.romanov@nginx.com NXT_PORT_MSG_DATA_LAST = _NXT_PORT_MSG_DATA | NXT_PORT_MSG_LAST, 123*1321Smax.romanov@nginx.com 124*1321Smax.romanov@nginx.com NXT_PORT_MSG_OOSM = _NXT_PORT_MSG_OOSM | NXT_PORT_MSG_LAST, 125*1321Smax.romanov@nginx.com NXT_PORT_MSG_SHM_ACK = _NXT_PORT_MSG_SHM_ACK | NXT_PORT_MSG_LAST, 126125Smax.romanov@nginx.com } nxt_port_msg_type_t; 12742Smax.romanov@nginx.com 12814Sigor@sysoev.ru 12942Smax.romanov@nginx.com /* Passed as a first iov chunk. */ 13042Smax.romanov@nginx.com typedef struct { 13142Smax.romanov@nginx.com uint32_t stream; 13242Smax.romanov@nginx.com nxt_pid_t pid; 13342Smax.romanov@nginx.com nxt_port_id_t reply_port; 13442Smax.romanov@nginx.com 135189Smax.romanov@nginx.com uint8_t type; 136423Smax.romanov@nginx.com 137423Smax.romanov@nginx.com /* Last message for this stream. */ 13842Smax.romanov@nginx.com uint8_t last; /* 1 bit */ 13942Smax.romanov@nginx.com 14042Smax.romanov@nginx.com /* Message data send using mmap, next chunk is a nxt_port_mmap_msg_t. */ 14142Smax.romanov@nginx.com uint8_t mmap; /* 1 bit */ 142352Smax.romanov@nginx.com 143423Smax.romanov@nginx.com /* Non-First fragment in fragmented message sequence. */ 144423Smax.romanov@nginx.com uint8_t nf; /* 1 bit */ 145423Smax.romanov@nginx.com 146423Smax.romanov@nginx.com /* More Fragments followed. */ 147423Smax.romanov@nginx.com uint8_t mf; /* 1 bit */ 148423Smax.romanov@nginx.com 149423Smax.romanov@nginx.com /* Message delivery tracking enabled, next chunk is tracking msg. */ 150423Smax.romanov@nginx.com uint8_t tracking; /* 1 bit */ 151168Svbart@nginx.com } nxt_port_msg_t; 15214Sigor@sysoev.ru 15314Sigor@sysoev.ru 15414Sigor@sysoev.ru typedef struct { 15514Sigor@sysoev.ru nxt_queue_link_t link; 15614Sigor@sysoev.ru nxt_buf_t *buf; 15714Sigor@sysoev.ru size_t share; 15814Sigor@sysoev.ru nxt_fd_t fd; 15914Sigor@sysoev.ru nxt_port_msg_t port_msg; 160423Smax.romanov@nginx.com uint32_t tracking_msg[2]; 1611125Smax.romanov@nginx.com uint8_t close_fd; /* 1 bit */ 1621125Smax.romanov@nginx.com uint8_t allocated; /* 1 bit */ 16314Sigor@sysoev.ru } nxt_port_send_msg_t; 16414Sigor@sysoev.ru 16514Sigor@sysoev.ru 16620Sigor@sysoev.ru struct nxt_port_recv_msg_s { 16714Sigor@sysoev.ru nxt_fd_t fd; 16814Sigor@sysoev.ru nxt_buf_t *buf; 16914Sigor@sysoev.ru nxt_port_t *port; 17042Smax.romanov@nginx.com nxt_port_msg_t port_msg; 17182Smax.romanov@nginx.com size_t size; 172423Smax.romanov@nginx.com nxt_bool_t cancelled; 173347Smax.romanov@nginx.com union { 174347Smax.romanov@nginx.com nxt_port_t *new_port; 175347Smax.romanov@nginx.com nxt_pid_t removed_pid; 176347Smax.romanov@nginx.com void *data; 177347Smax.romanov@nginx.com } u; 17820Sigor@sysoev.ru }; 17914Sigor@sysoev.ru 180141Smax.romanov@nginx.com typedef struct nxt_app_s nxt_app_t; 18114Sigor@sysoev.ru 18214Sigor@sysoev.ru struct nxt_port_s { 18314Sigor@sysoev.ru nxt_fd_event_t socket; 18414Sigor@sysoev.ru 185125Smax.romanov@nginx.com nxt_queue_link_t link; /* for nxt_process_t.ports */ 186141Smax.romanov@nginx.com nxt_process_t *process; 187141Smax.romanov@nginx.com 188141Smax.romanov@nginx.com nxt_queue_link_t app_link; /* for nxt_app_t.ports */ 189141Smax.romanov@nginx.com nxt_app_t *app; 19042Smax.romanov@nginx.com 191507Smax.romanov@nginx.com nxt_queue_link_t idle_link; /* for nxt_app_t.idle_ports */ 192507Smax.romanov@nginx.com nxt_msec_t idle_start; 193507Smax.romanov@nginx.com 19414Sigor@sysoev.ru nxt_queue_t messages; /* of nxt_port_send_msg_t */ 195343Smax.romanov@nginx.com nxt_thread_mutex_t write_mutex; 19614Sigor@sysoev.ru 19714Sigor@sysoev.ru /* Maximum size of message part. */ 19814Sigor@sysoev.ru uint32_t max_size; 19914Sigor@sysoev.ru /* Maximum interleave of message parts. */ 20014Sigor@sysoev.ru uint32_t max_share; 201343Smax.romanov@nginx.com 202424Smax.romanov@nginx.com uint32_t app_pending_responses; 203343Smax.romanov@nginx.com uint32_t app_responses; 204425Smax.romanov@nginx.com nxt_queue_t pending_requests; 20514Sigor@sysoev.ru 2061131Smax.romanov@nginx.com nxt_queue_t active_websockets; 2071131Smax.romanov@nginx.com 20814Sigor@sysoev.ru nxt_port_handler_t handler; 209141Smax.romanov@nginx.com nxt_port_handler_t *data; 21014Sigor@sysoev.ru 21165Sigor@sysoev.ru nxt_mp_t *mem_pool; 212141Smax.romanov@nginx.com nxt_event_engine_t *engine; 213141Smax.romanov@nginx.com 21414Sigor@sysoev.ru nxt_buf_t *free_bufs; 21514Sigor@sysoev.ru nxt_socket_t pair[2]; 21614Sigor@sysoev.ru 21742Smax.romanov@nginx.com nxt_port_id_t id; 21814Sigor@sysoev.ru nxt_pid_t pid; 21942Smax.romanov@nginx.com 220190Smax.romanov@nginx.com nxt_lvlhsh_t rpc_streams; /* stream to nxt_port_rpc_reg_t */ 221190Smax.romanov@nginx.com nxt_lvlhsh_t rpc_peers; /* peer to queue of nxt_port_rpc_reg_t */ 222190Smax.romanov@nginx.com 223352Smax.romanov@nginx.com nxt_lvlhsh_t frags; 224352Smax.romanov@nginx.com 225343Smax.romanov@nginx.com nxt_atomic_t use_count; 226343Smax.romanov@nginx.com 227141Smax.romanov@nginx.com nxt_process_type_t type; 22814Sigor@sysoev.ru }; 22914Sigor@sysoev.ru 23014Sigor@sysoev.ru 23111Sigor@sysoev.ru typedef struct { 23242Smax.romanov@nginx.com nxt_port_id_t id; 23314Sigor@sysoev.ru nxt_pid_t pid; 23414Sigor@sysoev.ru size_t max_size; 23514Sigor@sysoev.ru size_t max_share; 23642Smax.romanov@nginx.com nxt_process_type_t type:8; 237168Svbart@nginx.com } nxt_port_msg_new_port_t; 23811Sigor@sysoev.ru 23911Sigor@sysoev.ru 24011Sigor@sysoev.ru /* 24114Sigor@sysoev.ru * nxt_port_data_t size is allocation size 24214Sigor@sysoev.ru * which enables effective reuse of memory pool cache. 24311Sigor@sysoev.ru */ 24411Sigor@sysoev.ru typedef union { 24511Sigor@sysoev.ru nxt_buf_t buf; 24614Sigor@sysoev.ru nxt_port_msg_new_port_t new_port; 24714Sigor@sysoev.ru } nxt_port_data_t; 24811Sigor@sysoev.ru 24911Sigor@sysoev.ru 250343Smax.romanov@nginx.com typedef void (*nxt_port_post_handler_t)(nxt_task_t *task, nxt_port_t *port, 251343Smax.romanov@nginx.com void *data); 252343Smax.romanov@nginx.com 253197Smax.romanov@nginx.com nxt_port_t *nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, 254163Smax.romanov@nginx.com nxt_process_type_t type); 255163Smax.romanov@nginx.com 256141Smax.romanov@nginx.com nxt_port_id_t nxt_port_get_next_id(void); 257141Smax.romanov@nginx.com void nxt_port_reset_next_id(void); 258141Smax.romanov@nginx.com 25914Sigor@sysoev.ru nxt_int_t nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, 26014Sigor@sysoev.ru size_t max_size); 26114Sigor@sysoev.ru void nxt_port_destroy(nxt_port_t *port); 262343Smax.romanov@nginx.com void nxt_port_close(nxt_task_t *task, nxt_port_t *port); 26314Sigor@sysoev.ru void nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port); 26414Sigor@sysoev.ru void nxt_port_write_close(nxt_port_t *port); 26514Sigor@sysoev.ru void nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port); 26614Sigor@sysoev.ru void nxt_port_read_close(nxt_port_t *port); 267423Smax.romanov@nginx.com nxt_int_t nxt_port_socket_twrite(nxt_task_t *task, nxt_port_t *port, 26842Smax.romanov@nginx.com nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, 269423Smax.romanov@nginx.com nxt_buf_t *b, void *tracking); 270423Smax.romanov@nginx.com 271423Smax.romanov@nginx.com nxt_inline nxt_int_t 272423Smax.romanov@nginx.com nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port, 273423Smax.romanov@nginx.com nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, 274423Smax.romanov@nginx.com nxt_buf_t *b) 275423Smax.romanov@nginx.com { 276423Smax.romanov@nginx.com return nxt_port_socket_twrite(task, port, type, fd, stream, reply_port, b, 277423Smax.romanov@nginx.com NULL); 278423Smax.romanov@nginx.com } 27914Sigor@sysoev.ru 280141Smax.romanov@nginx.com void nxt_port_enable(nxt_task_t *task, nxt_port_t *port, 2811302St.nateldemoura@f5.com const nxt_port_handlers_t *handlers); 28281Smax.romanov@nginx.com nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, 283141Smax.romanov@nginx.com nxt_port_t *new_port, uint32_t stream); 28420Sigor@sysoev.ru void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, 28511Sigor@sysoev.ru nxt_uint_t slot, nxt_fd_t fd); 28611Sigor@sysoev.ru 28714Sigor@sysoev.ru void nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 28814Sigor@sysoev.ru void nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 289320Smax.romanov@nginx.com void nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 29014Sigor@sysoev.ru void nxt_port_change_log_file_handler(nxt_task_t *task, 29111Sigor@sysoev.ru nxt_port_recv_msg_t *msg); 29242Smax.romanov@nginx.com void nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 29314Sigor@sysoev.ru void nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 294125Smax.romanov@nginx.com void nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 29514Sigor@sysoev.ru void nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 29611Sigor@sysoev.ru 297343Smax.romanov@nginx.com nxt_int_t nxt_port_post(nxt_task_t *task, nxt_port_t *port, 298343Smax.romanov@nginx.com nxt_port_post_handler_t handler, void *data); 299343Smax.romanov@nginx.com void nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i); 30011Sigor@sysoev.ru 301425Smax.romanov@nginx.com nxt_inline void nxt_port_inc_use(nxt_port_t *port) 302425Smax.romanov@nginx.com { 303425Smax.romanov@nginx.com nxt_atomic_fetch_add(&port->use_count, 1); 304425Smax.romanov@nginx.com } 305425Smax.romanov@nginx.com 30611Sigor@sysoev.ru #endif /* _NXT_PORT_H_INCLUDED_ */ 307