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