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