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 39320Smax.romanov@nginx.com /* Various data. */ 40320Smax.romanov@nginx.com nxt_port_handler_t data; 41320Smax.romanov@nginx.com }; 42320Smax.romanov@nginx.com 43320Smax.romanov@nginx.com 44320Smax.romanov@nginx.com #define nxt_port_handler_idx(name) \ 45389Smax.romanov@nginx.com ( offsetof(nxt_port_handlers_t, name) / sizeof(nxt_port_handler_t) ) 46320Smax.romanov@nginx.com 47320Smax.romanov@nginx.com 4842Smax.romanov@nginx.com typedef enum { 49189Smax.romanov@nginx.com NXT_PORT_MSG_LAST = 0x100, 50189Smax.romanov@nginx.com NXT_PORT_MSG_CLOSE_FD = 0x200, 51205Smax.romanov@nginx.com NXT_PORT_MSG_SYNC = 0x400, 52189Smax.romanov@nginx.com 53189Smax.romanov@nginx.com NXT_PORT_MSG_MASK = 0xFF, 54189Smax.romanov@nginx.com 55320Smax.romanov@nginx.com _NXT_PORT_MSG_RPC_READY = nxt_port_handler_idx(rpc_ready), 56320Smax.romanov@nginx.com _NXT_PORT_MSG_RPC_ERROR = nxt_port_handler_idx(rpc_error), 57320Smax.romanov@nginx.com 58320Smax.romanov@nginx.com _NXT_PORT_MSG_START_WORKER = nxt_port_handler_idx(start_worker), 59320Smax.romanov@nginx.com _NXT_PORT_MSG_SOCKET = nxt_port_handler_idx(socket), 60320Smax.romanov@nginx.com _NXT_PORT_MSG_MODULES = nxt_port_handler_idx(modules), 61320Smax.romanov@nginx.com _NXT_PORT_MSG_CONF_STORE = nxt_port_handler_idx(conf_store), 62774Svbart@nginx.com _NXT_PORT_MSG_CERT_GET = nxt_port_handler_idx(cert_get), 63774Svbart@nginx.com _NXT_PORT_MSG_CERT_DELETE = nxt_port_handler_idx(cert_delete), 64630Svbart@nginx.com _NXT_PORT_MSG_ACCESS_LOG = nxt_port_handler_idx(access_log), 65320Smax.romanov@nginx.com 66320Smax.romanov@nginx.com _NXT_PORT_MSG_CHANGE_FILE = nxt_port_handler_idx(change_file), 67320Smax.romanov@nginx.com _NXT_PORT_MSG_NEW_PORT = nxt_port_handler_idx(new_port), 68320Smax.romanov@nginx.com _NXT_PORT_MSG_MMAP = nxt_port_handler_idx(mmap), 6942Smax.romanov@nginx.com 70320Smax.romanov@nginx.com _NXT_PORT_MSG_PROCESS_READY = nxt_port_handler_idx(process_ready), 71320Smax.romanov@nginx.com _NXT_PORT_MSG_REMOVE_PID = nxt_port_handler_idx(remove_pid), 72320Smax.romanov@nginx.com _NXT_PORT_MSG_QUIT = nxt_port_handler_idx(quit), 73320Smax.romanov@nginx.com 74320Smax.romanov@nginx.com _NXT_PORT_MSG_DATA = nxt_port_handler_idx(data), 75189Smax.romanov@nginx.com 76613Svbart@nginx.com NXT_PORT_MSG_MAX = sizeof(nxt_port_handlers_t) 77613Svbart@nginx.com / sizeof(nxt_port_handler_t), 78320Smax.romanov@nginx.com 79320Smax.romanov@nginx.com NXT_PORT_MSG_RPC_READY = _NXT_PORT_MSG_RPC_READY, 80320Smax.romanov@nginx.com NXT_PORT_MSG_RPC_READY_LAST = _NXT_PORT_MSG_RPC_READY | NXT_PORT_MSG_LAST, 81320Smax.romanov@nginx.com NXT_PORT_MSG_RPC_ERROR = _NXT_PORT_MSG_RPC_ERROR | NXT_PORT_MSG_LAST, 82320Smax.romanov@nginx.com 83613Svbart@nginx.com NXT_PORT_MSG_START_WORKER = _NXT_PORT_MSG_START_WORKER 84613Svbart@nginx.com | NXT_PORT_MSG_LAST, 85198Sigor@sysoev.ru NXT_PORT_MSG_SOCKET = _NXT_PORT_MSG_SOCKET | NXT_PORT_MSG_LAST, 86216Sigor@sysoev.ru NXT_PORT_MSG_MODULES = _NXT_PORT_MSG_MODULES | NXT_PORT_MSG_LAST, 87314Svbart@nginx.com NXT_PORT_MSG_CONF_STORE = _NXT_PORT_MSG_CONF_STORE | NXT_PORT_MSG_LAST, 88774Svbart@nginx.com NXT_PORT_MSG_CERT_GET = _NXT_PORT_MSG_CERT_GET | NXT_PORT_MSG_LAST, 89774Svbart@nginx.com NXT_PORT_MSG_CERT_DELETE = _NXT_PORT_MSG_CERT_DELETE | NXT_PORT_MSG_LAST, 90630Svbart@nginx.com NXT_PORT_MSG_ACCESS_LOG = _NXT_PORT_MSG_ACCESS_LOG | NXT_PORT_MSG_LAST, 91320Smax.romanov@nginx.com 92320Smax.romanov@nginx.com NXT_PORT_MSG_CHANGE_FILE = _NXT_PORT_MSG_CHANGE_FILE | NXT_PORT_MSG_LAST, 93320Smax.romanov@nginx.com NXT_PORT_MSG_NEW_PORT = _NXT_PORT_MSG_NEW_PORT | NXT_PORT_MSG_LAST, 94613Svbart@nginx.com NXT_PORT_MSG_MMAP = _NXT_PORT_MSG_MMAP | NXT_PORT_MSG_LAST 95613Svbart@nginx.com | NXT_PORT_MSG_CLOSE_FD | NXT_PORT_MSG_SYNC, 96320Smax.romanov@nginx.com 97613Svbart@nginx.com NXT_PORT_MSG_PROCESS_READY = _NXT_PORT_MSG_PROCESS_READY 98613Svbart@nginx.com | NXT_PORT_MSG_LAST, 99320Smax.romanov@nginx.com NXT_PORT_MSG_QUIT = _NXT_PORT_MSG_QUIT | NXT_PORT_MSG_LAST, 100320Smax.romanov@nginx.com NXT_PORT_MSG_REMOVE_PID = _NXT_PORT_MSG_REMOVE_PID | NXT_PORT_MSG_LAST, 101320Smax.romanov@nginx.com 102320Smax.romanov@nginx.com NXT_PORT_MSG_DATA = _NXT_PORT_MSG_DATA, 103320Smax.romanov@nginx.com NXT_PORT_MSG_DATA_LAST = _NXT_PORT_MSG_DATA | NXT_PORT_MSG_LAST, 104125Smax.romanov@nginx.com } nxt_port_msg_type_t; 10542Smax.romanov@nginx.com 10614Sigor@sysoev.ru 10742Smax.romanov@nginx.com /* Passed as a first iov chunk. */ 10842Smax.romanov@nginx.com typedef struct { 10942Smax.romanov@nginx.com uint32_t stream; 11042Smax.romanov@nginx.com nxt_pid_t pid; 11142Smax.romanov@nginx.com nxt_port_id_t reply_port; 11242Smax.romanov@nginx.com 113189Smax.romanov@nginx.com uint8_t type; 114423Smax.romanov@nginx.com 115423Smax.romanov@nginx.com /* Last message for this stream. */ 11642Smax.romanov@nginx.com uint8_t last; /* 1 bit */ 11742Smax.romanov@nginx.com 11842Smax.romanov@nginx.com /* Message data send using mmap, next chunk is a nxt_port_mmap_msg_t. */ 11942Smax.romanov@nginx.com uint8_t mmap; /* 1 bit */ 120352Smax.romanov@nginx.com 121423Smax.romanov@nginx.com /* Non-First fragment in fragmented message sequence. */ 122423Smax.romanov@nginx.com uint8_t nf; /* 1 bit */ 123423Smax.romanov@nginx.com 124423Smax.romanov@nginx.com /* More Fragments followed. */ 125423Smax.romanov@nginx.com uint8_t mf; /* 1 bit */ 126423Smax.romanov@nginx.com 127423Smax.romanov@nginx.com /* Message delivery tracking enabled, next chunk is tracking msg. */ 128423Smax.romanov@nginx.com uint8_t tracking; /* 1 bit */ 129168Svbart@nginx.com } nxt_port_msg_t; 13014Sigor@sysoev.ru 13114Sigor@sysoev.ru 13214Sigor@sysoev.ru typedef struct { 13314Sigor@sysoev.ru nxt_queue_link_t link; 13414Sigor@sysoev.ru nxt_buf_t *buf; 13514Sigor@sysoev.ru size_t share; 13614Sigor@sysoev.ru nxt_fd_t fd; 13714Sigor@sysoev.ru nxt_port_msg_t port_msg; 138423Smax.romanov@nginx.com uint32_t tracking_msg[2]; 139*1125Smax.romanov@nginx.com uint8_t close_fd; /* 1 bit */ 140*1125Smax.romanov@nginx.com uint8_t allocated; /* 1 bit */ 14114Sigor@sysoev.ru } nxt_port_send_msg_t; 14214Sigor@sysoev.ru 14314Sigor@sysoev.ru 14420Sigor@sysoev.ru struct nxt_port_recv_msg_s { 14514Sigor@sysoev.ru nxt_fd_t fd; 14614Sigor@sysoev.ru nxt_buf_t *buf; 14714Sigor@sysoev.ru nxt_port_t *port; 14842Smax.romanov@nginx.com nxt_port_msg_t port_msg; 14982Smax.romanov@nginx.com size_t size; 150423Smax.romanov@nginx.com nxt_bool_t cancelled; 151347Smax.romanov@nginx.com union { 152347Smax.romanov@nginx.com nxt_port_t *new_port; 153347Smax.romanov@nginx.com nxt_pid_t removed_pid; 154347Smax.romanov@nginx.com void *data; 155347Smax.romanov@nginx.com } u; 15620Sigor@sysoev.ru }; 15714Sigor@sysoev.ru 158141Smax.romanov@nginx.com typedef struct nxt_app_s nxt_app_t; 15914Sigor@sysoev.ru 16014Sigor@sysoev.ru struct nxt_port_s { 16114Sigor@sysoev.ru nxt_fd_event_t socket; 16214Sigor@sysoev.ru 163125Smax.romanov@nginx.com nxt_queue_link_t link; /* for nxt_process_t.ports */ 164141Smax.romanov@nginx.com nxt_process_t *process; 165141Smax.romanov@nginx.com 166141Smax.romanov@nginx.com nxt_queue_link_t app_link; /* for nxt_app_t.ports */ 167141Smax.romanov@nginx.com nxt_app_t *app; 16842Smax.romanov@nginx.com 169507Smax.romanov@nginx.com nxt_queue_link_t idle_link; /* for nxt_app_t.idle_ports */ 170507Smax.romanov@nginx.com nxt_msec_t idle_start; 171507Smax.romanov@nginx.com 17214Sigor@sysoev.ru nxt_queue_t messages; /* of nxt_port_send_msg_t */ 173343Smax.romanov@nginx.com nxt_thread_mutex_t write_mutex; 17414Sigor@sysoev.ru 17514Sigor@sysoev.ru /* Maximum size of message part. */ 17614Sigor@sysoev.ru uint32_t max_size; 17714Sigor@sysoev.ru /* Maximum interleave of message parts. */ 17814Sigor@sysoev.ru uint32_t max_share; 179343Smax.romanov@nginx.com 180424Smax.romanov@nginx.com uint32_t app_pending_responses; 181343Smax.romanov@nginx.com uint32_t app_responses; 182425Smax.romanov@nginx.com nxt_queue_t pending_requests; 18314Sigor@sysoev.ru 18414Sigor@sysoev.ru nxt_port_handler_t handler; 185141Smax.romanov@nginx.com nxt_port_handler_t *data; 18614Sigor@sysoev.ru 18765Sigor@sysoev.ru nxt_mp_t *mem_pool; 188141Smax.romanov@nginx.com nxt_event_engine_t *engine; 189141Smax.romanov@nginx.com 19014Sigor@sysoev.ru nxt_buf_t *free_bufs; 19114Sigor@sysoev.ru nxt_socket_t pair[2]; 19214Sigor@sysoev.ru 19342Smax.romanov@nginx.com nxt_port_id_t id; 19414Sigor@sysoev.ru nxt_pid_t pid; 19542Smax.romanov@nginx.com 196190Smax.romanov@nginx.com nxt_lvlhsh_t rpc_streams; /* stream to nxt_port_rpc_reg_t */ 197190Smax.romanov@nginx.com nxt_lvlhsh_t rpc_peers; /* peer to queue of nxt_port_rpc_reg_t */ 198190Smax.romanov@nginx.com 199352Smax.romanov@nginx.com nxt_lvlhsh_t frags; 200352Smax.romanov@nginx.com 201343Smax.romanov@nginx.com nxt_atomic_t use_count; 202343Smax.romanov@nginx.com 203141Smax.romanov@nginx.com nxt_process_type_t type; 20414Sigor@sysoev.ru }; 20514Sigor@sysoev.ru 20614Sigor@sysoev.ru 20711Sigor@sysoev.ru typedef struct { 20842Smax.romanov@nginx.com nxt_port_id_t id; 20914Sigor@sysoev.ru nxt_pid_t pid; 21014Sigor@sysoev.ru size_t max_size; 21114Sigor@sysoev.ru size_t max_share; 21242Smax.romanov@nginx.com nxt_process_type_t type:8; 213168Svbart@nginx.com } nxt_port_msg_new_port_t; 21411Sigor@sysoev.ru 21511Sigor@sysoev.ru 21611Sigor@sysoev.ru /* 21714Sigor@sysoev.ru * nxt_port_data_t size is allocation size 21814Sigor@sysoev.ru * which enables effective reuse of memory pool cache. 21911Sigor@sysoev.ru */ 22011Sigor@sysoev.ru typedef union { 22111Sigor@sysoev.ru nxt_buf_t buf; 22214Sigor@sysoev.ru nxt_port_msg_new_port_t new_port; 22314Sigor@sysoev.ru } nxt_port_data_t; 22411Sigor@sysoev.ru 22511Sigor@sysoev.ru 226343Smax.romanov@nginx.com typedef void (*nxt_port_post_handler_t)(nxt_task_t *task, nxt_port_t *port, 227343Smax.romanov@nginx.com void *data); 228343Smax.romanov@nginx.com 229197Smax.romanov@nginx.com nxt_port_t *nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, 230163Smax.romanov@nginx.com nxt_process_type_t type); 231163Smax.romanov@nginx.com 232141Smax.romanov@nginx.com nxt_port_id_t nxt_port_get_next_id(void); 233141Smax.romanov@nginx.com void nxt_port_reset_next_id(void); 234141Smax.romanov@nginx.com 23514Sigor@sysoev.ru nxt_int_t nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, 23614Sigor@sysoev.ru size_t max_size); 23714Sigor@sysoev.ru void nxt_port_destroy(nxt_port_t *port); 238343Smax.romanov@nginx.com void nxt_port_close(nxt_task_t *task, nxt_port_t *port); 23914Sigor@sysoev.ru void nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port); 24014Sigor@sysoev.ru void nxt_port_write_close(nxt_port_t *port); 24114Sigor@sysoev.ru void nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port); 24214Sigor@sysoev.ru void nxt_port_read_close(nxt_port_t *port); 243423Smax.romanov@nginx.com nxt_int_t nxt_port_socket_twrite(nxt_task_t *task, nxt_port_t *port, 24442Smax.romanov@nginx.com nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, 245423Smax.romanov@nginx.com nxt_buf_t *b, void *tracking); 246423Smax.romanov@nginx.com 247423Smax.romanov@nginx.com nxt_inline nxt_int_t 248423Smax.romanov@nginx.com nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port, 249423Smax.romanov@nginx.com nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, 250423Smax.romanov@nginx.com nxt_buf_t *b) 251423Smax.romanov@nginx.com { 252423Smax.romanov@nginx.com return nxt_port_socket_twrite(task, port, type, fd, stream, reply_port, b, 253423Smax.romanov@nginx.com NULL); 254423Smax.romanov@nginx.com } 25514Sigor@sysoev.ru 256141Smax.romanov@nginx.com void nxt_port_enable(nxt_task_t *task, nxt_port_t *port, 257320Smax.romanov@nginx.com nxt_port_handlers_t *handlers); 25881Smax.romanov@nginx.com nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, 259141Smax.romanov@nginx.com nxt_port_t *new_port, uint32_t stream); 26020Sigor@sysoev.ru void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, 26111Sigor@sysoev.ru nxt_uint_t slot, nxt_fd_t fd); 26211Sigor@sysoev.ru 26314Sigor@sysoev.ru void nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 26414Sigor@sysoev.ru void nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 265320Smax.romanov@nginx.com void nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 26614Sigor@sysoev.ru void nxt_port_change_log_file_handler(nxt_task_t *task, 26711Sigor@sysoev.ru nxt_port_recv_msg_t *msg); 26842Smax.romanov@nginx.com void nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 26914Sigor@sysoev.ru void nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 270125Smax.romanov@nginx.com void nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 27114Sigor@sysoev.ru void nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 27211Sigor@sysoev.ru 273343Smax.romanov@nginx.com nxt_int_t nxt_port_post(nxt_task_t *task, nxt_port_t *port, 274343Smax.romanov@nginx.com nxt_port_post_handler_t handler, void *data); 275343Smax.romanov@nginx.com void nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i); 27611Sigor@sysoev.ru 277425Smax.romanov@nginx.com nxt_inline void nxt_port_inc_use(nxt_port_t *port) 278425Smax.romanov@nginx.com { 279425Smax.romanov@nginx.com nxt_atomic_fetch_add(&port->use_count, 1); 280425Smax.romanov@nginx.com } 281425Smax.romanov@nginx.com 28211Sigor@sysoev.ru #endif /* _NXT_PORT_H_INCLUDED_ */ 283