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. */ 17*1488St.nateldemoura@f5.com nxt_port_handler_t start_process; 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 30*1488St.nateldemoura@f5.com /* New process */ 31*1488St.nateldemoura@f5.com nxt_port_handler_t process_created; 32320Smax.romanov@nginx.com nxt_port_handler_t process_ready; 33320Smax.romanov@nginx.com 34320Smax.romanov@nginx.com /* Process exit/crash notification. */ 35320Smax.romanov@nginx.com nxt_port_handler_t remove_pid; 36320Smax.romanov@nginx.com 37320Smax.romanov@nginx.com /* Stop process command. */ 38320Smax.romanov@nginx.com nxt_port_handler_t quit; 39320Smax.romanov@nginx.com 401131Smax.romanov@nginx.com /* Request headers. */ 411131Smax.romanov@nginx.com nxt_port_handler_t req_headers; 421131Smax.romanov@nginx.com 431131Smax.romanov@nginx.com /* Websocket frame. */ 441131Smax.romanov@nginx.com nxt_port_handler_t websocket_frame; 451131Smax.romanov@nginx.com 46320Smax.romanov@nginx.com /* Various data. */ 47320Smax.romanov@nginx.com nxt_port_handler_t data; 481321Smax.romanov@nginx.com 491321Smax.romanov@nginx.com nxt_port_handler_t oosm; 501321Smax.romanov@nginx.com nxt_port_handler_t shm_ack; 51320Smax.romanov@nginx.com }; 52320Smax.romanov@nginx.com 53320Smax.romanov@nginx.com 54320Smax.romanov@nginx.com #define nxt_port_handler_idx(name) \ 55389Smax.romanov@nginx.com ( offsetof(nxt_port_handlers_t, name) / sizeof(nxt_port_handler_t) ) 56320Smax.romanov@nginx.com 57*1488St.nateldemoura@f5.com #define nxt_msg_last(handler) \ 58*1488St.nateldemoura@f5.com (handler | NXT_PORT_MSG_LAST) 59320Smax.romanov@nginx.com 6042Smax.romanov@nginx.com typedef enum { 61*1488St.nateldemoura@f5.com NXT_PORT_MSG_LAST = 0x100, 62*1488St.nateldemoura@f5.com NXT_PORT_MSG_CLOSE_FD = 0x200, 63*1488St.nateldemoura@f5.com NXT_PORT_MSG_SYNC = 0x400, 64189Smax.romanov@nginx.com 65*1488St.nateldemoura@f5.com NXT_PORT_MSG_MASK = 0xFF, 66189Smax.romanov@nginx.com 67*1488St.nateldemoura@f5.com _NXT_PORT_MSG_RPC_READY = nxt_port_handler_idx(rpc_ready), 68*1488St.nateldemoura@f5.com _NXT_PORT_MSG_RPC_ERROR = nxt_port_handler_idx(rpc_error), 69320Smax.romanov@nginx.com 70*1488St.nateldemoura@f5.com _NXT_PORT_MSG_START_PROCESS = nxt_port_handler_idx(start_process), 71*1488St.nateldemoura@f5.com _NXT_PORT_MSG_SOCKET = nxt_port_handler_idx(socket), 72*1488St.nateldemoura@f5.com _NXT_PORT_MSG_MODULES = nxt_port_handler_idx(modules), 73*1488St.nateldemoura@f5.com _NXT_PORT_MSG_CONF_STORE = nxt_port_handler_idx(conf_store), 74*1488St.nateldemoura@f5.com _NXT_PORT_MSG_CERT_GET = nxt_port_handler_idx(cert_get), 75*1488St.nateldemoura@f5.com _NXT_PORT_MSG_CERT_DELETE = nxt_port_handler_idx(cert_delete), 76*1488St.nateldemoura@f5.com _NXT_PORT_MSG_ACCESS_LOG = nxt_port_handler_idx(access_log), 7742Smax.romanov@nginx.com 78*1488St.nateldemoura@f5.com _NXT_PORT_MSG_CHANGE_FILE = nxt_port_handler_idx(change_file), 79*1488St.nateldemoura@f5.com _NXT_PORT_MSG_NEW_PORT = nxt_port_handler_idx(new_port), 80*1488St.nateldemoura@f5.com _NXT_PORT_MSG_MMAP = nxt_port_handler_idx(mmap), 81320Smax.romanov@nginx.com 82*1488St.nateldemoura@f5.com _NXT_PORT_MSG_PROCESS_CREATED = nxt_port_handler_idx(process_created), 83*1488St.nateldemoura@f5.com _NXT_PORT_MSG_PROCESS_READY = nxt_port_handler_idx(process_ready), 84*1488St.nateldemoura@f5.com _NXT_PORT_MSG_REMOVE_PID = nxt_port_handler_idx(remove_pid), 85*1488St.nateldemoura@f5.com _NXT_PORT_MSG_QUIT = nxt_port_handler_idx(quit), 861131Smax.romanov@nginx.com 87*1488St.nateldemoura@f5.com _NXT_PORT_MSG_REQ_HEADERS = nxt_port_handler_idx(req_headers), 88*1488St.nateldemoura@f5.com _NXT_PORT_MSG_WEBSOCKET = nxt_port_handler_idx(websocket_frame), 89189Smax.romanov@nginx.com 90*1488St.nateldemoura@f5.com _NXT_PORT_MSG_DATA = nxt_port_handler_idx(data), 911321Smax.romanov@nginx.com 92*1488St.nateldemoura@f5.com _NXT_PORT_MSG_OOSM = nxt_port_handler_idx(oosm), 93*1488St.nateldemoura@f5.com _NXT_PORT_MSG_SHM_ACK = nxt_port_handler_idx(shm_ack), 94320Smax.romanov@nginx.com 95*1488St.nateldemoura@f5.com NXT_PORT_MSG_MAX = sizeof(nxt_port_handlers_t) 96*1488St.nateldemoura@f5.com / sizeof(nxt_port_handler_t), 97320Smax.romanov@nginx.com 98*1488St.nateldemoura@f5.com NXT_PORT_MSG_RPC_READY = _NXT_PORT_MSG_RPC_READY, 99*1488St.nateldemoura@f5.com NXT_PORT_MSG_RPC_READY_LAST = nxt_msg_last(_NXT_PORT_MSG_RPC_READY), 100*1488St.nateldemoura@f5.com NXT_PORT_MSG_RPC_ERROR = nxt_msg_last(_NXT_PORT_MSG_RPC_ERROR), 101*1488St.nateldemoura@f5.com NXT_PORT_MSG_START_PROCESS = nxt_msg_last(_NXT_PORT_MSG_START_PROCESS), 102*1488St.nateldemoura@f5.com NXT_PORT_MSG_SOCKET = nxt_msg_last(_NXT_PORT_MSG_SOCKET), 103*1488St.nateldemoura@f5.com NXT_PORT_MSG_MODULES = nxt_msg_last(_NXT_PORT_MSG_MODULES), 104*1488St.nateldemoura@f5.com NXT_PORT_MSG_CONF_STORE = nxt_msg_last(_NXT_PORT_MSG_CONF_STORE), 105*1488St.nateldemoura@f5.com NXT_PORT_MSG_CERT_GET = nxt_msg_last(_NXT_PORT_MSG_CERT_GET), 106*1488St.nateldemoura@f5.com NXT_PORT_MSG_CERT_DELETE = nxt_msg_last(_NXT_PORT_MSG_CERT_DELETE), 107*1488St.nateldemoura@f5.com NXT_PORT_MSG_ACCESS_LOG = nxt_msg_last(_NXT_PORT_MSG_ACCESS_LOG), 108*1488St.nateldemoura@f5.com NXT_PORT_MSG_CHANGE_FILE = nxt_msg_last(_NXT_PORT_MSG_CHANGE_FILE), 109*1488St.nateldemoura@f5.com NXT_PORT_MSG_NEW_PORT = nxt_msg_last(_NXT_PORT_MSG_NEW_PORT), 110*1488St.nateldemoura@f5.com NXT_PORT_MSG_MMAP = nxt_msg_last(_NXT_PORT_MSG_MMAP) 111*1488St.nateldemoura@f5.com | NXT_PORT_MSG_CLOSE_FD | NXT_PORT_MSG_SYNC, 112320Smax.romanov@nginx.com 113*1488St.nateldemoura@f5.com NXT_PORT_MSG_PROCESS_CREATED = nxt_msg_last(_NXT_PORT_MSG_PROCESS_CREATED), 114*1488St.nateldemoura@f5.com NXT_PORT_MSG_PROCESS_READY = nxt_msg_last(_NXT_PORT_MSG_PROCESS_READY), 115*1488St.nateldemoura@f5.com NXT_PORT_MSG_QUIT = nxt_msg_last(_NXT_PORT_MSG_QUIT), 116*1488St.nateldemoura@f5.com NXT_PORT_MSG_REMOVE_PID = nxt_msg_last(_NXT_PORT_MSG_REMOVE_PID), 1171131Smax.romanov@nginx.com 118*1488St.nateldemoura@f5.com NXT_PORT_MSG_REQ_HEADERS = _NXT_PORT_MSG_REQ_HEADERS, 119*1488St.nateldemoura@f5.com NXT_PORT_MSG_WEBSOCKET = _NXT_PORT_MSG_WEBSOCKET, 120*1488St.nateldemoura@f5.com NXT_PORT_MSG_WEBSOCKET_LAST = nxt_msg_last(_NXT_PORT_MSG_WEBSOCKET), 1211321Smax.romanov@nginx.com 122*1488St.nateldemoura@f5.com NXT_PORT_MSG_DATA = _NXT_PORT_MSG_DATA, 123*1488St.nateldemoura@f5.com NXT_PORT_MSG_DATA_LAST = nxt_msg_last(_NXT_PORT_MSG_DATA), 124*1488St.nateldemoura@f5.com 125*1488St.nateldemoura@f5.com NXT_PORT_MSG_OOSM = nxt_msg_last(_NXT_PORT_MSG_OOSM), 126*1488St.nateldemoura@f5.com NXT_PORT_MSG_SHM_ACK = nxt_msg_last(_NXT_PORT_MSG_SHM_ACK), 127125Smax.romanov@nginx.com } nxt_port_msg_type_t; 12842Smax.romanov@nginx.com 12914Sigor@sysoev.ru 13042Smax.romanov@nginx.com /* Passed as a first iov chunk. */ 13142Smax.romanov@nginx.com typedef struct { 13242Smax.romanov@nginx.com uint32_t stream; 13342Smax.romanov@nginx.com nxt_pid_t pid; 13442Smax.romanov@nginx.com nxt_port_id_t reply_port; 13542Smax.romanov@nginx.com 136189Smax.romanov@nginx.com uint8_t type; 137423Smax.romanov@nginx.com 138423Smax.romanov@nginx.com /* Last message for this stream. */ 13942Smax.romanov@nginx.com uint8_t last; /* 1 bit */ 14042Smax.romanov@nginx.com 14142Smax.romanov@nginx.com /* Message data send using mmap, next chunk is a nxt_port_mmap_msg_t. */ 14242Smax.romanov@nginx.com uint8_t mmap; /* 1 bit */ 143352Smax.romanov@nginx.com 144423Smax.romanov@nginx.com /* Non-First fragment in fragmented message sequence. */ 145423Smax.romanov@nginx.com uint8_t nf; /* 1 bit */ 146423Smax.romanov@nginx.com 147423Smax.romanov@nginx.com /* More Fragments followed. */ 148423Smax.romanov@nginx.com uint8_t mf; /* 1 bit */ 149423Smax.romanov@nginx.com 150423Smax.romanov@nginx.com /* Message delivery tracking enabled, next chunk is tracking msg. */ 151423Smax.romanov@nginx.com uint8_t tracking; /* 1 bit */ 152168Svbart@nginx.com } nxt_port_msg_t; 15314Sigor@sysoev.ru 15414Sigor@sysoev.ru 15514Sigor@sysoev.ru typedef struct { 15614Sigor@sysoev.ru nxt_queue_link_t link; 15714Sigor@sysoev.ru nxt_buf_t *buf; 15814Sigor@sysoev.ru size_t share; 15914Sigor@sysoev.ru nxt_fd_t fd; 16014Sigor@sysoev.ru nxt_port_msg_t port_msg; 161423Smax.romanov@nginx.com uint32_t tracking_msg[2]; 1621125Smax.romanov@nginx.com uint8_t close_fd; /* 1 bit */ 1631125Smax.romanov@nginx.com uint8_t allocated; /* 1 bit */ 16414Sigor@sysoev.ru } nxt_port_send_msg_t; 16514Sigor@sysoev.ru 16614Sigor@sysoev.ru 16720Sigor@sysoev.ru struct nxt_port_recv_msg_s { 16814Sigor@sysoev.ru nxt_fd_t fd; 16914Sigor@sysoev.ru nxt_buf_t *buf; 17014Sigor@sysoev.ru nxt_port_t *port; 17142Smax.romanov@nginx.com nxt_port_msg_t port_msg; 17282Smax.romanov@nginx.com size_t size; 173423Smax.romanov@nginx.com nxt_bool_t cancelled; 174347Smax.romanov@nginx.com union { 175347Smax.romanov@nginx.com nxt_port_t *new_port; 176347Smax.romanov@nginx.com nxt_pid_t removed_pid; 177347Smax.romanov@nginx.com void *data; 178347Smax.romanov@nginx.com } u; 17920Sigor@sysoev.ru }; 18014Sigor@sysoev.ru 181141Smax.romanov@nginx.com typedef struct nxt_app_s nxt_app_t; 18214Sigor@sysoev.ru 18314Sigor@sysoev.ru struct nxt_port_s { 18414Sigor@sysoev.ru nxt_fd_event_t socket; 18514Sigor@sysoev.ru 186125Smax.romanov@nginx.com nxt_queue_link_t link; /* for nxt_process_t.ports */ 187141Smax.romanov@nginx.com nxt_process_t *process; 188141Smax.romanov@nginx.com 189141Smax.romanov@nginx.com nxt_queue_link_t app_link; /* for nxt_app_t.ports */ 190141Smax.romanov@nginx.com nxt_app_t *app; 19142Smax.romanov@nginx.com 192507Smax.romanov@nginx.com nxt_queue_link_t idle_link; /* for nxt_app_t.idle_ports */ 193507Smax.romanov@nginx.com nxt_msec_t idle_start; 194507Smax.romanov@nginx.com 19514Sigor@sysoev.ru nxt_queue_t messages; /* of nxt_port_send_msg_t */ 196343Smax.romanov@nginx.com nxt_thread_mutex_t write_mutex; 19714Sigor@sysoev.ru 19814Sigor@sysoev.ru /* Maximum size of message part. */ 19914Sigor@sysoev.ru uint32_t max_size; 20014Sigor@sysoev.ru /* Maximum interleave of message parts. */ 20114Sigor@sysoev.ru uint32_t max_share; 202343Smax.romanov@nginx.com 203424Smax.romanov@nginx.com uint32_t app_pending_responses; 204343Smax.romanov@nginx.com uint32_t app_responses; 205425Smax.romanov@nginx.com nxt_queue_t pending_requests; 20614Sigor@sysoev.ru 2071131Smax.romanov@nginx.com nxt_queue_t active_websockets; 2081131Smax.romanov@nginx.com 20914Sigor@sysoev.ru nxt_port_handler_t handler; 210141Smax.romanov@nginx.com nxt_port_handler_t *data; 21114Sigor@sysoev.ru 21265Sigor@sysoev.ru nxt_mp_t *mem_pool; 213141Smax.romanov@nginx.com nxt_event_engine_t *engine; 214141Smax.romanov@nginx.com 21514Sigor@sysoev.ru nxt_buf_t *free_bufs; 21614Sigor@sysoev.ru nxt_socket_t pair[2]; 21714Sigor@sysoev.ru 21842Smax.romanov@nginx.com nxt_port_id_t id; 21914Sigor@sysoev.ru nxt_pid_t pid; 22042Smax.romanov@nginx.com 221190Smax.romanov@nginx.com nxt_lvlhsh_t rpc_streams; /* stream to nxt_port_rpc_reg_t */ 222190Smax.romanov@nginx.com nxt_lvlhsh_t rpc_peers; /* peer to queue of nxt_port_rpc_reg_t */ 223190Smax.romanov@nginx.com 224352Smax.romanov@nginx.com nxt_lvlhsh_t frags; 225352Smax.romanov@nginx.com 226343Smax.romanov@nginx.com nxt_atomic_t use_count; 227343Smax.romanov@nginx.com 228141Smax.romanov@nginx.com nxt_process_type_t type; 22914Sigor@sysoev.ru }; 23014Sigor@sysoev.ru 23114Sigor@sysoev.ru 23211Sigor@sysoev.ru typedef struct { 23342Smax.romanov@nginx.com nxt_port_id_t id; 23414Sigor@sysoev.ru nxt_pid_t pid; 23514Sigor@sysoev.ru size_t max_size; 23614Sigor@sysoev.ru size_t max_share; 23742Smax.romanov@nginx.com nxt_process_type_t type:8; 238168Svbart@nginx.com } nxt_port_msg_new_port_t; 23911Sigor@sysoev.ru 24011Sigor@sysoev.ru 24111Sigor@sysoev.ru /* 24214Sigor@sysoev.ru * nxt_port_data_t size is allocation size 24314Sigor@sysoev.ru * which enables effective reuse of memory pool cache. 24411Sigor@sysoev.ru */ 24511Sigor@sysoev.ru typedef union { 24611Sigor@sysoev.ru nxt_buf_t buf; 24714Sigor@sysoev.ru nxt_port_msg_new_port_t new_port; 24814Sigor@sysoev.ru } nxt_port_data_t; 24911Sigor@sysoev.ru 25011Sigor@sysoev.ru 251343Smax.romanov@nginx.com typedef void (*nxt_port_post_handler_t)(nxt_task_t *task, nxt_port_t *port, 252343Smax.romanov@nginx.com void *data); 253343Smax.romanov@nginx.com 254197Smax.romanov@nginx.com nxt_port_t *nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, 255163Smax.romanov@nginx.com nxt_process_type_t type); 256163Smax.romanov@nginx.com 257141Smax.romanov@nginx.com nxt_port_id_t nxt_port_get_next_id(void); 258141Smax.romanov@nginx.com void nxt_port_reset_next_id(void); 259141Smax.romanov@nginx.com 26014Sigor@sysoev.ru nxt_int_t nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, 26114Sigor@sysoev.ru size_t max_size); 26214Sigor@sysoev.ru void nxt_port_destroy(nxt_port_t *port); 263343Smax.romanov@nginx.com void nxt_port_close(nxt_task_t *task, nxt_port_t *port); 26414Sigor@sysoev.ru void nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port); 26514Sigor@sysoev.ru void nxt_port_write_close(nxt_port_t *port); 26614Sigor@sysoev.ru void nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port); 26714Sigor@sysoev.ru void nxt_port_read_close(nxt_port_t *port); 268423Smax.romanov@nginx.com nxt_int_t nxt_port_socket_twrite(nxt_task_t *task, nxt_port_t *port, 26942Smax.romanov@nginx.com nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, 270423Smax.romanov@nginx.com nxt_buf_t *b, void *tracking); 271423Smax.romanov@nginx.com 272423Smax.romanov@nginx.com nxt_inline nxt_int_t 273423Smax.romanov@nginx.com nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port, 274423Smax.romanov@nginx.com nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, 275423Smax.romanov@nginx.com nxt_buf_t *b) 276423Smax.romanov@nginx.com { 277423Smax.romanov@nginx.com return nxt_port_socket_twrite(task, port, type, fd, stream, reply_port, b, 278423Smax.romanov@nginx.com NULL); 279423Smax.romanov@nginx.com } 28014Sigor@sysoev.ru 281141Smax.romanov@nginx.com void nxt_port_enable(nxt_task_t *task, nxt_port_t *port, 2821302St.nateldemoura@f5.com const nxt_port_handlers_t *handlers); 28381Smax.romanov@nginx.com nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, 284141Smax.romanov@nginx.com nxt_port_t *new_port, uint32_t stream); 28520Sigor@sysoev.ru void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, 28611Sigor@sysoev.ru nxt_uint_t slot, nxt_fd_t fd); 28711Sigor@sysoev.ru 28814Sigor@sysoev.ru void nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 28914Sigor@sysoev.ru void nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 290320Smax.romanov@nginx.com void nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 29114Sigor@sysoev.ru void nxt_port_change_log_file_handler(nxt_task_t *task, 29211Sigor@sysoev.ru nxt_port_recv_msg_t *msg); 29342Smax.romanov@nginx.com void nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 29414Sigor@sysoev.ru void nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 295125Smax.romanov@nginx.com void nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 29614Sigor@sysoev.ru void nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 29711Sigor@sysoev.ru 298343Smax.romanov@nginx.com nxt_int_t nxt_port_post(nxt_task_t *task, nxt_port_t *port, 299343Smax.romanov@nginx.com nxt_port_post_handler_t handler, void *data); 300343Smax.romanov@nginx.com void nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i); 30111Sigor@sysoev.ru 302425Smax.romanov@nginx.com nxt_inline void nxt_port_inc_use(nxt_port_t *port) 303425Smax.romanov@nginx.com { 304425Smax.romanov@nginx.com nxt_atomic_fetch_add(&port->use_count, 1); 305425Smax.romanov@nginx.com } 306425Smax.romanov@nginx.com 30711Sigor@sysoev.ru #endif /* _NXT_PORT_H_INCLUDED_ */ 308