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 11*320Smax.romanov@nginx.com struct nxt_port_handlers_s { 12*320Smax.romanov@nginx.com /* RPC responses. */ 13*320Smax.romanov@nginx.com nxt_port_handler_t rpc_ready; 14*320Smax.romanov@nginx.com nxt_port_handler_t rpc_error; 15*320Smax.romanov@nginx.com 16*320Smax.romanov@nginx.com /* Main process RPC requests. */ 17*320Smax.romanov@nginx.com nxt_port_handler_t start_worker; 18*320Smax.romanov@nginx.com nxt_port_handler_t socket; 19*320Smax.romanov@nginx.com nxt_port_handler_t modules; 20*320Smax.romanov@nginx.com nxt_port_handler_t conf_store; 21*320Smax.romanov@nginx.com 22*320Smax.romanov@nginx.com /* File descriptor exchange. */ 23*320Smax.romanov@nginx.com nxt_port_handler_t change_file; 24*320Smax.romanov@nginx.com nxt_port_handler_t new_port; 25*320Smax.romanov@nginx.com nxt_port_handler_t mmap; 26*320Smax.romanov@nginx.com 27*320Smax.romanov@nginx.com /* New process ready. */ 28*320Smax.romanov@nginx.com nxt_port_handler_t process_ready; 29*320Smax.romanov@nginx.com 30*320Smax.romanov@nginx.com /* Process exit/crash notification. */ 31*320Smax.romanov@nginx.com nxt_port_handler_t remove_pid; 32*320Smax.romanov@nginx.com 33*320Smax.romanov@nginx.com /* Stop process command. */ 34*320Smax.romanov@nginx.com nxt_port_handler_t quit; 35*320Smax.romanov@nginx.com 36*320Smax.romanov@nginx.com /* Various data. */ 37*320Smax.romanov@nginx.com nxt_port_handler_t data; 38*320Smax.romanov@nginx.com }; 39*320Smax.romanov@nginx.com 40*320Smax.romanov@nginx.com 41*320Smax.romanov@nginx.com #define nxt_port_handler_idx(name) \ 42*320Smax.romanov@nginx.com ( &((nxt_port_handlers_t *) 0)->name - (nxt_port_handler_t *) 0) 43*320Smax.romanov@nginx.com 44*320Smax.romanov@nginx.com 4542Smax.romanov@nginx.com typedef enum { 46189Smax.romanov@nginx.com NXT_PORT_MSG_LAST = 0x100, 47189Smax.romanov@nginx.com NXT_PORT_MSG_CLOSE_FD = 0x200, 48205Smax.romanov@nginx.com NXT_PORT_MSG_SYNC = 0x400, 49189Smax.romanov@nginx.com 50189Smax.romanov@nginx.com NXT_PORT_MSG_MASK = 0xFF, 51189Smax.romanov@nginx.com 52*320Smax.romanov@nginx.com _NXT_PORT_MSG_RPC_READY = nxt_port_handler_idx(rpc_ready), 53*320Smax.romanov@nginx.com _NXT_PORT_MSG_RPC_ERROR = nxt_port_handler_idx(rpc_error), 54*320Smax.romanov@nginx.com 55*320Smax.romanov@nginx.com _NXT_PORT_MSG_START_WORKER = nxt_port_handler_idx(start_worker), 56*320Smax.romanov@nginx.com _NXT_PORT_MSG_SOCKET = nxt_port_handler_idx(socket), 57*320Smax.romanov@nginx.com _NXT_PORT_MSG_MODULES = nxt_port_handler_idx(modules), 58*320Smax.romanov@nginx.com _NXT_PORT_MSG_CONF_STORE = nxt_port_handler_idx(conf_store), 59*320Smax.romanov@nginx.com 60*320Smax.romanov@nginx.com _NXT_PORT_MSG_CHANGE_FILE = nxt_port_handler_idx(change_file), 61*320Smax.romanov@nginx.com _NXT_PORT_MSG_NEW_PORT = nxt_port_handler_idx(new_port), 62*320Smax.romanov@nginx.com _NXT_PORT_MSG_MMAP = nxt_port_handler_idx(mmap), 6342Smax.romanov@nginx.com 64*320Smax.romanov@nginx.com _NXT_PORT_MSG_PROCESS_READY = nxt_port_handler_idx(process_ready), 65*320Smax.romanov@nginx.com _NXT_PORT_MSG_REMOVE_PID = nxt_port_handler_idx(remove_pid), 66*320Smax.romanov@nginx.com _NXT_PORT_MSG_QUIT = nxt_port_handler_idx(quit), 67*320Smax.romanov@nginx.com 68*320Smax.romanov@nginx.com _NXT_PORT_MSG_DATA = nxt_port_handler_idx(data), 69189Smax.romanov@nginx.com 70*320Smax.romanov@nginx.com NXT_PORT_MSG_MAX = sizeof(nxt_port_handlers_t) / 71*320Smax.romanov@nginx.com sizeof(nxt_port_handler_t), 72*320Smax.romanov@nginx.com 73*320Smax.romanov@nginx.com NXT_PORT_MSG_RPC_READY = _NXT_PORT_MSG_RPC_READY, 74*320Smax.romanov@nginx.com NXT_PORT_MSG_RPC_READY_LAST = _NXT_PORT_MSG_RPC_READY | NXT_PORT_MSG_LAST, 75*320Smax.romanov@nginx.com NXT_PORT_MSG_RPC_ERROR = _NXT_PORT_MSG_RPC_ERROR | NXT_PORT_MSG_LAST, 76*320Smax.romanov@nginx.com 77192Smax.romanov@nginx.com NXT_PORT_MSG_START_WORKER = _NXT_PORT_MSG_START_WORKER | 78192Smax.romanov@nginx.com NXT_PORT_MSG_LAST, 79198Sigor@sysoev.ru NXT_PORT_MSG_SOCKET = _NXT_PORT_MSG_SOCKET | NXT_PORT_MSG_LAST, 80216Sigor@sysoev.ru NXT_PORT_MSG_MODULES = _NXT_PORT_MSG_MODULES | NXT_PORT_MSG_LAST, 81314Svbart@nginx.com NXT_PORT_MSG_CONF_STORE = _NXT_PORT_MSG_CONF_STORE | NXT_PORT_MSG_LAST, 82*320Smax.romanov@nginx.com 83*320Smax.romanov@nginx.com NXT_PORT_MSG_CHANGE_FILE = _NXT_PORT_MSG_CHANGE_FILE | NXT_PORT_MSG_LAST, 84*320Smax.romanov@nginx.com NXT_PORT_MSG_NEW_PORT = _NXT_PORT_MSG_NEW_PORT | NXT_PORT_MSG_LAST, 85*320Smax.romanov@nginx.com NXT_PORT_MSG_MMAP = _NXT_PORT_MSG_MMAP | NXT_PORT_MSG_LAST | 86*320Smax.romanov@nginx.com NXT_PORT_MSG_CLOSE_FD | NXT_PORT_MSG_SYNC, 87*320Smax.romanov@nginx.com 88*320Smax.romanov@nginx.com NXT_PORT_MSG_PROCESS_READY = _NXT_PORT_MSG_PROCESS_READY | 89*320Smax.romanov@nginx.com NXT_PORT_MSG_LAST, 90*320Smax.romanov@nginx.com NXT_PORT_MSG_QUIT = _NXT_PORT_MSG_QUIT | NXT_PORT_MSG_LAST, 91*320Smax.romanov@nginx.com NXT_PORT_MSG_REMOVE_PID = _NXT_PORT_MSG_REMOVE_PID | NXT_PORT_MSG_LAST, 92*320Smax.romanov@nginx.com 93*320Smax.romanov@nginx.com NXT_PORT_MSG_DATA = _NXT_PORT_MSG_DATA, 94*320Smax.romanov@nginx.com NXT_PORT_MSG_DATA_LAST = _NXT_PORT_MSG_DATA | NXT_PORT_MSG_LAST, 95125Smax.romanov@nginx.com } nxt_port_msg_type_t; 9642Smax.romanov@nginx.com 9714Sigor@sysoev.ru 9842Smax.romanov@nginx.com /* Passed as a first iov chunk. */ 9942Smax.romanov@nginx.com typedef struct { 10042Smax.romanov@nginx.com uint32_t stream; 10142Smax.romanov@nginx.com nxt_pid_t pid; 10242Smax.romanov@nginx.com nxt_port_id_t reply_port; 10342Smax.romanov@nginx.com 104189Smax.romanov@nginx.com uint8_t type; 10542Smax.romanov@nginx.com uint8_t last; /* 1 bit */ 10642Smax.romanov@nginx.com 10742Smax.romanov@nginx.com /* Message data send using mmap, next chunk is a nxt_port_mmap_msg_t. */ 10842Smax.romanov@nginx.com uint8_t mmap; /* 1 bit */ 109168Svbart@nginx.com } nxt_port_msg_t; 11014Sigor@sysoev.ru 11114Sigor@sysoev.ru 11214Sigor@sysoev.ru typedef struct { 11314Sigor@sysoev.ru nxt_queue_link_t link; 11414Sigor@sysoev.ru nxt_buf_t *buf; 11514Sigor@sysoev.ru size_t share; 11614Sigor@sysoev.ru nxt_fd_t fd; 117189Smax.romanov@nginx.com nxt_bool_t close_fd; 118205Smax.romanov@nginx.com nxt_bool_t opened; 11914Sigor@sysoev.ru nxt_port_msg_t port_msg; 120122Smax.romanov@nginx.com 121122Smax.romanov@nginx.com nxt_work_t work; 122122Smax.romanov@nginx.com nxt_event_engine_t *engine; 123122Smax.romanov@nginx.com nxt_mp_t *mem_pool; 12414Sigor@sysoev.ru } nxt_port_send_msg_t; 12514Sigor@sysoev.ru 12614Sigor@sysoev.ru 12720Sigor@sysoev.ru struct nxt_port_recv_msg_s { 12814Sigor@sysoev.ru nxt_fd_t fd; 12914Sigor@sysoev.ru nxt_buf_t *buf; 13014Sigor@sysoev.ru nxt_port_t *port; 13142Smax.romanov@nginx.com nxt_port_msg_t port_msg; 13282Smax.romanov@nginx.com size_t size; 133141Smax.romanov@nginx.com nxt_port_t *new_port; 13420Sigor@sysoev.ru }; 13514Sigor@sysoev.ru 136141Smax.romanov@nginx.com typedef struct nxt_app_s nxt_app_t; 13714Sigor@sysoev.ru 13814Sigor@sysoev.ru struct nxt_port_s { 13914Sigor@sysoev.ru nxt_fd_event_t socket; 14014Sigor@sysoev.ru 141125Smax.romanov@nginx.com nxt_queue_link_t link; /* for nxt_process_t.ports */ 142141Smax.romanov@nginx.com nxt_process_t *process; 143141Smax.romanov@nginx.com 144141Smax.romanov@nginx.com nxt_queue_link_t app_link; /* for nxt_app_t.ports */ 145141Smax.romanov@nginx.com nxt_app_t *app; 14642Smax.romanov@nginx.com 14714Sigor@sysoev.ru nxt_queue_t messages; /* of nxt_port_send_msg_t */ 14814Sigor@sysoev.ru 14914Sigor@sysoev.ru /* Maximum size of message part. */ 15014Sigor@sysoev.ru uint32_t max_size; 15114Sigor@sysoev.ru /* Maximum interleave of message parts. */ 15214Sigor@sysoev.ru uint32_t max_share; 153318Smax.romanov@nginx.com uint32_t app_stream; 15414Sigor@sysoev.ru 15514Sigor@sysoev.ru nxt_port_handler_t handler; 156141Smax.romanov@nginx.com nxt_port_handler_t *data; 15714Sigor@sysoev.ru 15865Sigor@sysoev.ru nxt_mp_t *mem_pool; 159141Smax.romanov@nginx.com nxt_event_engine_t *engine; 160141Smax.romanov@nginx.com 16114Sigor@sysoev.ru nxt_buf_t *free_bufs; 16214Sigor@sysoev.ru nxt_socket_t pair[2]; 16314Sigor@sysoev.ru 16442Smax.romanov@nginx.com nxt_port_id_t id; 16514Sigor@sysoev.ru nxt_pid_t pid; 16642Smax.romanov@nginx.com 167190Smax.romanov@nginx.com nxt_lvlhsh_t rpc_streams; /* stream to nxt_port_rpc_reg_t */ 168190Smax.romanov@nginx.com nxt_lvlhsh_t rpc_peers; /* peer to queue of nxt_port_rpc_reg_t */ 169190Smax.romanov@nginx.com 170141Smax.romanov@nginx.com nxt_process_type_t type; 171163Smax.romanov@nginx.com nxt_work_t work; 172197Smax.romanov@nginx.com 173197Smax.romanov@nginx.com struct iovec *iov; 174197Smax.romanov@nginx.com void *mmsg_buf; 17514Sigor@sysoev.ru }; 17614Sigor@sysoev.ru 17714Sigor@sysoev.ru 17811Sigor@sysoev.ru typedef struct { 17942Smax.romanov@nginx.com nxt_port_id_t id; 18014Sigor@sysoev.ru nxt_pid_t pid; 18114Sigor@sysoev.ru size_t max_size; 18214Sigor@sysoev.ru size_t max_share; 18342Smax.romanov@nginx.com nxt_process_type_t type:8; 184168Svbart@nginx.com } nxt_port_msg_new_port_t; 18511Sigor@sysoev.ru 18611Sigor@sysoev.ru 18711Sigor@sysoev.ru /* 18814Sigor@sysoev.ru * nxt_port_data_t size is allocation size 18914Sigor@sysoev.ru * which enables effective reuse of memory pool cache. 19011Sigor@sysoev.ru */ 19111Sigor@sysoev.ru typedef union { 19211Sigor@sysoev.ru nxt_buf_t buf; 19314Sigor@sysoev.ru nxt_port_msg_new_port_t new_port; 19414Sigor@sysoev.ru } nxt_port_data_t; 19511Sigor@sysoev.ru 19611Sigor@sysoev.ru 197197Smax.romanov@nginx.com nxt_port_t *nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, 198163Smax.romanov@nginx.com nxt_process_type_t type); 199163Smax.romanov@nginx.com nxt_bool_t nxt_port_release(nxt_port_t *port); 200163Smax.romanov@nginx.com 201141Smax.romanov@nginx.com nxt_port_id_t nxt_port_get_next_id(void); 202141Smax.romanov@nginx.com void nxt_port_reset_next_id(void); 203141Smax.romanov@nginx.com 20414Sigor@sysoev.ru nxt_int_t nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, 20514Sigor@sysoev.ru size_t max_size); 20614Sigor@sysoev.ru void nxt_port_destroy(nxt_port_t *port); 20714Sigor@sysoev.ru void nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port); 20814Sigor@sysoev.ru void nxt_port_write_close(nxt_port_t *port); 20914Sigor@sysoev.ru void nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port); 21014Sigor@sysoev.ru void nxt_port_read_close(nxt_port_t *port); 21114Sigor@sysoev.ru nxt_int_t nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port, 21242Smax.romanov@nginx.com nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, 21342Smax.romanov@nginx.com nxt_buf_t *b); 21414Sigor@sysoev.ru 215141Smax.romanov@nginx.com void nxt_port_enable(nxt_task_t *task, nxt_port_t *port, 216*320Smax.romanov@nginx.com nxt_port_handlers_t *handlers); 21720Sigor@sysoev.ru void nxt_port_send_new_port(nxt_task_t *task, nxt_runtime_t *rt, 218141Smax.romanov@nginx.com nxt_port_t *port, uint32_t stream); 21981Smax.romanov@nginx.com nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, 220141Smax.romanov@nginx.com nxt_port_t *new_port, uint32_t stream); 22120Sigor@sysoev.ru void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, 22211Sigor@sysoev.ru nxt_uint_t slot, nxt_fd_t fd); 22311Sigor@sysoev.ru 22414Sigor@sysoev.ru void nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 22514Sigor@sysoev.ru void nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 226*320Smax.romanov@nginx.com void nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 22714Sigor@sysoev.ru void nxt_port_change_log_file_handler(nxt_task_t *task, 22811Sigor@sysoev.ru nxt_port_recv_msg_t *msg); 22942Smax.romanov@nginx.com void nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 23014Sigor@sysoev.ru void nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 231125Smax.romanov@nginx.com void nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 23214Sigor@sysoev.ru void nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 23311Sigor@sysoev.ru 23411Sigor@sysoev.ru 23511Sigor@sysoev.ru #endif /* _NXT_PORT_H_INCLUDED_ */ 236