xref: /unit/src/nxt_port.h (revision 1996:35873fa78fed)
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 
157     nxt_pid_t            pid;       /* not used on Linux and FreeBSD */
158 
159     nxt_port_id_t        reply_port;
160 
161     uint8_t              type;
162 
163     /* Last message for this stream. */
164     uint8_t              last;      /* 1 bit */
165 
166     /* Message data send using mmap, next chunk is a nxt_port_mmap_msg_t. */
167     uint8_t              mmap;      /* 1 bit */
168 
169     /* Non-First fragment in fragmented message sequence. */
170     uint8_t              nf;        /* 1 bit */
171 
172     /* More Fragments followed. */
173     uint8_t              mf;        /* 1 bit */
174 
175     /* Message delivery tracking enabled, next chunk is tracking msg. */
176     uint8_t              tracking;  /* 1 bit */
177 } nxt_port_msg_t;
178 
179 
180 typedef struct {
181     nxt_queue_link_t    link;
182     nxt_buf_t           *buf;
183     size_t              share;
184     nxt_fd_t            fd[2];
185     nxt_port_msg_t      port_msg;
186     uint32_t            tracking_msg[2];
187     uint8_t             close_fd;   /* 1 bit */
188     uint8_t             allocated;  /* 1 bit */
189 } nxt_port_send_msg_t;
190 
191 #if (NXT_HAVE_UCRED) || (NXT_HAVE_MSGHDR_CMSGCRED)
192 #define NXT_USE_CMSG_PID    1
193 #endif
194 
195 struct nxt_port_recv_msg_s {
196     nxt_fd_t            fd[2];
197     nxt_buf_t           *buf;
198     nxt_port_t          *port;
199     nxt_port_msg_t      port_msg;
200     size_t              size;
201 #if (NXT_USE_CMSG_PID)
202     nxt_pid_t           cmsg_pid;
203 #endif
204     nxt_bool_t          cancelled;
205     union {
206         nxt_port_t      *new_port;
207         nxt_pid_t       removed_pid;
208         void            *data;
209     } u;
210 };
211 
212 
213 #if (NXT_USE_CMSG_PID)
214 #define nxt_recv_msg_cmsg_pid(msg)      ((msg)->cmsg_pid)
215 #define nxt_recv_msg_cmsg_pid_ref(msg)  (&(msg)->cmsg_pid)
216 #else
217 #define nxt_recv_msg_cmsg_pid(msg)      ((msg)->port_msg.pid)
218 #define nxt_recv_msg_cmsg_pid_ref(msg)  (NULL)
219 #endif
220 
221 typedef struct nxt_app_s  nxt_app_t;
222 
223 struct nxt_port_s {
224     nxt_fd_event_t      socket;
225 
226     nxt_queue_link_t    link;       /* for nxt_process_t.ports */
227     nxt_process_t       *process;
228 
229     nxt_queue_link_t    app_link;   /* for nxt_app_t.ports */
230     nxt_app_t           *app;
231     nxt_port_t          *main_app_port;
232 
233     nxt_queue_link_t    idle_link;  /* for nxt_app_t.idle_ports */
234     nxt_msec_t          idle_start;
235 
236     nxt_queue_t         messages;   /* of nxt_port_send_msg_t */
237     nxt_thread_mutex_t  write_mutex;
238 
239     /* Maximum size of message part. */
240     uint32_t            max_size;
241     /* Maximum interleave of message parts. */
242     uint32_t            max_share;
243 
244     uint32_t            active_websockets;
245     uint32_t            active_requests;
246 
247     nxt_port_handler_t  handler;
248     nxt_port_handler_t  *data;
249 
250     nxt_mp_t            *mem_pool;
251     nxt_event_engine_t  *engine;
252 
253     nxt_buf_t           *free_bufs;
254     nxt_socket_t        pair[2];
255 
256     nxt_port_id_t       id;
257     nxt_pid_t           pid;
258 
259     nxt_lvlhsh_t        rpc_streams; /* stream to nxt_port_rpc_reg_t */
260     nxt_lvlhsh_t        rpc_peers;   /* peer to queue of nxt_port_rpc_reg_t */
261 
262     nxt_lvlhsh_t        frags;
263 
264     nxt_atomic_t        use_count;
265 
266     nxt_process_type_t  type;
267 
268     nxt_fd_t            queue_fd;
269     void                *queue;
270 
271     void                *socket_msg;
272     int                 from_socket;
273 };
274 
275 
276 typedef struct {
277     nxt_port_id_t       id;
278     nxt_pid_t           pid;
279     size_t              max_size;
280     size_t              max_share;
281     nxt_process_type_t  type:8;
282 } nxt_port_msg_new_port_t;
283 
284 
285 typedef struct {
286     nxt_port_id_t       id;
287     nxt_pid_t           pid;
288 } nxt_port_msg_get_port_t;
289 
290 
291 typedef struct {
292     uint32_t            id;
293 } nxt_port_msg_get_mmap_t;
294 
295 
296 /*
297  * nxt_port_data_t size is allocation size
298  * which enables effective reuse of memory pool cache.
299  */
300 typedef union {
301     nxt_buf_t                buf;
302     nxt_port_msg_new_port_t  new_port;
303 } nxt_port_data_t;
304 
305 
306 typedef void (*nxt_port_post_handler_t)(nxt_task_t *task, nxt_port_t *port,
307     void *data);
308 
309 nxt_port_t *nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid,
310     nxt_process_type_t type);
311 
312 nxt_port_id_t nxt_port_get_next_id(void);
313 void nxt_port_reset_next_id(void);
314 
315 nxt_int_t nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port,
316     size_t max_size);
317 void nxt_port_destroy(nxt_port_t *port);
318 void nxt_port_close(nxt_task_t *task, nxt_port_t *port);
319 void nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port);
320 void nxt_port_write_close(nxt_port_t *port);
321 void nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port);
322 void nxt_port_read_close(nxt_port_t *port);
323 nxt_int_t nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port,
324     nxt_uint_t type, nxt_fd_t fd, nxt_fd_t fd2, uint32_t stream,
325     nxt_port_id_t reply_port, nxt_buf_t *b);
326 
327 nxt_inline nxt_int_t
328 nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port,
329     nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port,
330     nxt_buf_t *b)
331 {
332     return nxt_port_socket_write2(task, port, type, fd, -1, stream, reply_port,
333                                   b);
334 }
335 
336 void nxt_port_enable(nxt_task_t *task, nxt_port_t *port,
337     const nxt_port_handlers_t *handlers);
338 nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port,
339     nxt_port_t *new_port, uint32_t stream);
340 void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt,
341     nxt_uint_t slot, nxt_fd_t fd);
342 
343 void nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
344 void nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
345 void nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
346 void nxt_port_change_log_file_handler(nxt_task_t *task,
347     nxt_port_recv_msg_t *msg);
348 void nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
349 void nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
350 void nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
351 void nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
352 
353 nxt_int_t nxt_port_post(nxt_task_t *task, nxt_port_t *port,
354     nxt_port_post_handler_t handler, void *data);
355 void nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i);
356 
357 nxt_inline void nxt_port_inc_use(nxt_port_t *port)
358 {
359     nxt_atomic_fetch_add(&port->use_count, 1);
360 }
361 
362 #endif /* _NXT_PORT_H_INCLUDED_ */
363