1 2 /* 3 * Copyright (C) Igor Sysoev 4 * Copyright (C) NGINX, Inc. 5 */ 6 7 #ifndef _NXT_PORT_H_INCLUDED_ 8 #define _NXT_PORT_H_INCLUDED_ 9 10 11 struct nxt_port_handlers_s { 12 /* RPC responses. */ 13 nxt_port_handler_t rpc_ready; 14 nxt_port_handler_t rpc_error; 15 16 /* Main process RPC requests. */ 17 nxt_port_handler_t start_process; 18 nxt_port_handler_t socket; 19 nxt_port_handler_t modules; 20 nxt_port_handler_t conf_store; 21 nxt_port_handler_t cert_get; 22 nxt_port_handler_t cert_delete; 23 nxt_port_handler_t access_log; 24 25 /* File descriptor exchange. */ 26 nxt_port_handler_t change_file; 27 nxt_port_handler_t new_port; 28 nxt_port_handler_t get_port; 29 nxt_port_handler_t port_ack; 30 nxt_port_handler_t mmap; 31 nxt_port_handler_t get_mmap; 32 33 /* New process */ 34 nxt_port_handler_t process_created; 35 nxt_port_handler_t process_ready; 36 37 /* Process exit/crash notification. */ 38 nxt_port_handler_t remove_pid; 39 40 /* Stop process command. */ 41 nxt_port_handler_t quit; 42 43 /* Request headers. */ 44 nxt_port_handler_t req_headers; 45 nxt_port_handler_t req_headers_ack; 46 nxt_port_handler_t req_body; 47 48 /* Websocket frame. */ 49 nxt_port_handler_t websocket_frame; 50 51 /* Various data. */ 52 nxt_port_handler_t data; 53 54 nxt_port_handler_t oosm; 55 nxt_port_handler_t shm_ack; 56 nxt_port_handler_t read_queue; 57 nxt_port_handler_t read_socket; 58 }; 59 60 61 #define nxt_port_handler_idx(name) \ 62 ( offsetof(nxt_port_handlers_t, name) / sizeof(nxt_port_handler_t) ) 63 64 #define nxt_msg_last(handler) \ 65 (handler | NXT_PORT_MSG_LAST) 66 67 typedef enum { 68 NXT_PORT_MSG_LAST = 0x100, 69 NXT_PORT_MSG_CLOSE_FD = 0x200, 70 NXT_PORT_MSG_SYNC = 0x400, 71 72 NXT_PORT_MSG_MASK = 0xFF, 73 74 _NXT_PORT_MSG_RPC_READY = nxt_port_handler_idx(rpc_ready), 75 _NXT_PORT_MSG_RPC_ERROR = nxt_port_handler_idx(rpc_error), 76 77 _NXT_PORT_MSG_START_PROCESS = nxt_port_handler_idx(start_process), 78 _NXT_PORT_MSG_SOCKET = nxt_port_handler_idx(socket), 79 _NXT_PORT_MSG_MODULES = nxt_port_handler_idx(modules), 80 _NXT_PORT_MSG_CONF_STORE = nxt_port_handler_idx(conf_store), 81 _NXT_PORT_MSG_CERT_GET = nxt_port_handler_idx(cert_get), 82 _NXT_PORT_MSG_CERT_DELETE = nxt_port_handler_idx(cert_delete), 83 _NXT_PORT_MSG_ACCESS_LOG = nxt_port_handler_idx(access_log), 84 85 _NXT_PORT_MSG_CHANGE_FILE = nxt_port_handler_idx(change_file), 86 _NXT_PORT_MSG_NEW_PORT = nxt_port_handler_idx(new_port), 87 _NXT_PORT_MSG_GET_PORT = nxt_port_handler_idx(get_port), 88 _NXT_PORT_MSG_PORT_ACK = nxt_port_handler_idx(port_ack), 89 _NXT_PORT_MSG_MMAP = nxt_port_handler_idx(mmap), 90 _NXT_PORT_MSG_GET_MMAP = nxt_port_handler_idx(get_mmap), 91 92 _NXT_PORT_MSG_PROCESS_CREATED = nxt_port_handler_idx(process_created), 93 _NXT_PORT_MSG_PROCESS_READY = nxt_port_handler_idx(process_ready), 94 _NXT_PORT_MSG_REMOVE_PID = nxt_port_handler_idx(remove_pid), 95 _NXT_PORT_MSG_QUIT = nxt_port_handler_idx(quit), 96 97 _NXT_PORT_MSG_REQ_HEADERS = nxt_port_handler_idx(req_headers), 98 _NXT_PORT_MSG_REQ_HEADERS_ACK = nxt_port_handler_idx(req_headers_ack), 99 _NXT_PORT_MSG_REQ_BODY = nxt_port_handler_idx(req_body), 100 _NXT_PORT_MSG_WEBSOCKET = nxt_port_handler_idx(websocket_frame), 101 102 _NXT_PORT_MSG_DATA = nxt_port_handler_idx(data), 103 104 _NXT_PORT_MSG_OOSM = nxt_port_handler_idx(oosm), 105 _NXT_PORT_MSG_SHM_ACK = nxt_port_handler_idx(shm_ack), 106 _NXT_PORT_MSG_READ_QUEUE = nxt_port_handler_idx(read_queue), 107 _NXT_PORT_MSG_READ_SOCKET = nxt_port_handler_idx(read_socket), 108 109 NXT_PORT_MSG_MAX = sizeof(nxt_port_handlers_t) 110 / sizeof(nxt_port_handler_t), 111 112 NXT_PORT_MSG_RPC_READY = _NXT_PORT_MSG_RPC_READY, 113 NXT_PORT_MSG_RPC_READY_LAST = nxt_msg_last(_NXT_PORT_MSG_RPC_READY), 114 NXT_PORT_MSG_RPC_ERROR = nxt_msg_last(_NXT_PORT_MSG_RPC_ERROR), 115 NXT_PORT_MSG_START_PROCESS = nxt_msg_last(_NXT_PORT_MSG_START_PROCESS), 116 NXT_PORT_MSG_SOCKET = nxt_msg_last(_NXT_PORT_MSG_SOCKET), 117 NXT_PORT_MSG_MODULES = nxt_msg_last(_NXT_PORT_MSG_MODULES), 118 NXT_PORT_MSG_CONF_STORE = nxt_msg_last(_NXT_PORT_MSG_CONF_STORE), 119 NXT_PORT_MSG_CERT_GET = nxt_msg_last(_NXT_PORT_MSG_CERT_GET), 120 NXT_PORT_MSG_CERT_DELETE = nxt_msg_last(_NXT_PORT_MSG_CERT_DELETE), 121 NXT_PORT_MSG_ACCESS_LOG = nxt_msg_last(_NXT_PORT_MSG_ACCESS_LOG), 122 NXT_PORT_MSG_CHANGE_FILE = nxt_msg_last(_NXT_PORT_MSG_CHANGE_FILE), 123 NXT_PORT_MSG_NEW_PORT = nxt_msg_last(_NXT_PORT_MSG_NEW_PORT), 124 NXT_PORT_MSG_GET_PORT = nxt_msg_last(_NXT_PORT_MSG_GET_PORT), 125 NXT_PORT_MSG_PORT_ACK = nxt_msg_last(_NXT_PORT_MSG_PORT_ACK), 126 NXT_PORT_MSG_MMAP = nxt_msg_last(_NXT_PORT_MSG_MMAP) 127 | NXT_PORT_MSG_SYNC, 128 NXT_PORT_MSG_GET_MMAP = nxt_msg_last(_NXT_PORT_MSG_GET_MMAP), 129 130 NXT_PORT_MSG_PROCESS_CREATED = nxt_msg_last(_NXT_PORT_MSG_PROCESS_CREATED), 131 NXT_PORT_MSG_PROCESS_READY = nxt_msg_last(_NXT_PORT_MSG_PROCESS_READY), 132 NXT_PORT_MSG_QUIT = nxt_msg_last(_NXT_PORT_MSG_QUIT), 133 NXT_PORT_MSG_REMOVE_PID = nxt_msg_last(_NXT_PORT_MSG_REMOVE_PID), 134 135 NXT_PORT_MSG_REQ_HEADERS = _NXT_PORT_MSG_REQ_HEADERS, 136 NXT_PORT_MSG_REQ_BODY = _NXT_PORT_MSG_REQ_BODY, 137 NXT_PORT_MSG_WEBSOCKET = _NXT_PORT_MSG_WEBSOCKET, 138 NXT_PORT_MSG_WEBSOCKET_LAST = nxt_msg_last(_NXT_PORT_MSG_WEBSOCKET), 139 140 NXT_PORT_MSG_DATA = _NXT_PORT_MSG_DATA, 141 NXT_PORT_MSG_DATA_LAST = nxt_msg_last(_NXT_PORT_MSG_DATA), 142 143 NXT_PORT_MSG_OOSM = nxt_msg_last(_NXT_PORT_MSG_OOSM), 144 NXT_PORT_MSG_SHM_ACK = nxt_msg_last(_NXT_PORT_MSG_SHM_ACK), 145 NXT_PORT_MSG_READ_QUEUE = _NXT_PORT_MSG_READ_QUEUE, 146 NXT_PORT_MSG_READ_SOCKET = _NXT_PORT_MSG_READ_SOCKET, 147 } nxt_port_msg_type_t; 148 149 150 /* Passed as a first iov chunk. */ 151 typedef struct { 152 uint32_t stream; 153 nxt_pid_t pid; 154 nxt_port_id_t reply_port; 155 156 uint8_t type; 157 158 /* Last message for this stream. */ 159 uint8_t last; /* 1 bit */ 160 161 /* Message data send using mmap, next chunk is a nxt_port_mmap_msg_t. */ 162 uint8_t mmap; /* 1 bit */ 163 164 /* Non-First fragment in fragmented message sequence. */ 165 uint8_t nf; /* 1 bit */ 166 167 /* More Fragments followed. */ 168 uint8_t mf; /* 1 bit */ 169 170 /* Message delivery tracking enabled, next chunk is tracking msg. */ 171 uint8_t tracking; /* 1 bit */ 172 } nxt_port_msg_t; 173 174 175 typedef struct { 176 nxt_queue_link_t link; 177 nxt_buf_t *buf; 178 size_t share; 179 nxt_fd_t fd[2]; 180 nxt_port_msg_t port_msg; 181 uint32_t tracking_msg[2]; 182 uint8_t close_fd; /* 1 bit */ 183 uint8_t allocated; /* 1 bit */ 184 } nxt_port_send_msg_t; 185 186 187 struct nxt_port_recv_msg_s { 188 nxt_fd_t fd[2]; 189 nxt_buf_t *buf; 190 nxt_port_t *port; 191 nxt_port_msg_t port_msg; 192 size_t size; 193 nxt_bool_t cancelled; 194 union { 195 nxt_port_t *new_port; 196 nxt_pid_t removed_pid; 197 void *data; 198 } u; 199 }; 200 201 typedef struct nxt_app_s nxt_app_t; 202 203 struct nxt_port_s { 204 nxt_fd_event_t socket; 205 206 nxt_queue_link_t link; /* for nxt_process_t.ports */ 207 nxt_process_t *process; 208 209 nxt_queue_link_t app_link; /* for nxt_app_t.ports */ 210 nxt_app_t *app; 211 nxt_port_t *main_app_port; 212 213 nxt_queue_link_t idle_link; /* for nxt_app_t.idle_ports */ 214 nxt_msec_t idle_start; 215 216 nxt_queue_t messages; /* of nxt_port_send_msg_t */ 217 nxt_thread_mutex_t write_mutex; 218 219 /* Maximum size of message part. */ 220 uint32_t max_size; 221 /* Maximum interleave of message parts. */ 222 uint32_t max_share; 223 224 uint32_t app_responses; 225 226 uint32_t active_websockets; 227 uint32_t active_requests; 228 229 nxt_port_handler_t handler; 230 nxt_port_handler_t *data; 231 232 nxt_mp_t *mem_pool; 233 nxt_event_engine_t *engine; 234 235 nxt_buf_t *free_bufs; 236 nxt_socket_t pair[2]; 237 238 nxt_port_id_t id; 239 nxt_pid_t pid; 240 241 nxt_lvlhsh_t rpc_streams; /* stream to nxt_port_rpc_reg_t */ 242 nxt_lvlhsh_t rpc_peers; /* peer to queue of nxt_port_rpc_reg_t */ 243 244 nxt_lvlhsh_t frags; 245 246 nxt_atomic_t use_count; 247 248 nxt_process_type_t type; 249 250 nxt_fd_t queue_fd; 251 void *queue; 252 253 void *socket_msg; 254 int from_socket; 255 }; 256 257 258 typedef struct { 259 nxt_port_id_t id; 260 nxt_pid_t pid; 261 size_t max_size; 262 size_t max_share; 263 nxt_process_type_t type:8; 264 } nxt_port_msg_new_port_t; 265 266 267 typedef struct { 268 nxt_port_id_t id; 269 nxt_pid_t pid; 270 } nxt_port_msg_get_port_t; 271 272 273 typedef struct { 274 uint32_t id; 275 } nxt_port_msg_get_mmap_t; 276 277 278 /* 279 * nxt_port_data_t size is allocation size 280 * which enables effective reuse of memory pool cache. 281 */ 282 typedef union { 283 nxt_buf_t buf; 284 nxt_port_msg_new_port_t new_port; 285 } nxt_port_data_t; 286 287 288 typedef void (*nxt_port_post_handler_t)(nxt_task_t *task, nxt_port_t *port, 289 void *data); 290 291 nxt_port_t *nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, 292 nxt_process_type_t type); 293 294 nxt_port_id_t nxt_port_get_next_id(void); 295 void nxt_port_reset_next_id(void); 296 297 nxt_int_t nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, 298 size_t max_size); 299 void nxt_port_destroy(nxt_port_t *port); 300 void nxt_port_close(nxt_task_t *task, nxt_port_t *port); 301 void nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port); 302 void nxt_port_write_close(nxt_port_t *port); 303 void nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port); 304 void nxt_port_read_close(nxt_port_t *port); 305 nxt_int_t nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port, 306 nxt_uint_t type, nxt_fd_t fd, nxt_fd_t fd2, uint32_t stream, 307 nxt_port_id_t reply_port, nxt_buf_t *b); 308 309 nxt_inline nxt_int_t 310 nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port, 311 nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, 312 nxt_buf_t *b) 313 { 314 return nxt_port_socket_write2(task, port, type, fd, -1, stream, reply_port, 315 b); 316 } 317 318 void nxt_port_enable(nxt_task_t *task, nxt_port_t *port, 319 const nxt_port_handlers_t *handlers); 320 nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, 321 nxt_port_t *new_port, uint32_t stream); 322 void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, 323 nxt_uint_t slot, nxt_fd_t fd); 324 325 void nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 326 void nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 327 void nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 328 void nxt_port_change_log_file_handler(nxt_task_t *task, 329 nxt_port_recv_msg_t *msg); 330 void nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 331 void nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 332 void nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 333 void nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); 334 335 nxt_int_t nxt_port_post(nxt_task_t *task, nxt_port_t *port, 336 nxt_port_post_handler_t handler, void *data); 337 void nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i); 338 339 nxt_inline void nxt_port_inc_use(nxt_port_t *port) 340 { 341 nxt_atomic_fetch_add(&port->use_count, 1); 342 } 343 344 #endif /* _NXT_PORT_H_INCLUDED_ */ 345