xref: /unit/src/nxt_port.h (revision 425)
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 
11320Smax.romanov@nginx.com struct nxt_port_handlers_s {
12320Smax.romanov@nginx.com     /* RPC responses. */
13320Smax.romanov@nginx.com     nxt_port_handler_t  rpc_ready;
14320Smax.romanov@nginx.com     nxt_port_handler_t  rpc_error;
15320Smax.romanov@nginx.com 
16320Smax.romanov@nginx.com     /* Main process RPC requests. */
17320Smax.romanov@nginx.com     nxt_port_handler_t  start_worker;
18320Smax.romanov@nginx.com     nxt_port_handler_t  socket;
19320Smax.romanov@nginx.com     nxt_port_handler_t  modules;
20320Smax.romanov@nginx.com     nxt_port_handler_t  conf_store;
21320Smax.romanov@nginx.com 
22320Smax.romanov@nginx.com     /* File descriptor exchange. */
23320Smax.romanov@nginx.com     nxt_port_handler_t  change_file;
24320Smax.romanov@nginx.com     nxt_port_handler_t  new_port;
25320Smax.romanov@nginx.com     nxt_port_handler_t  mmap;
26320Smax.romanov@nginx.com 
27320Smax.romanov@nginx.com     /* New process ready. */
28320Smax.romanov@nginx.com     nxt_port_handler_t  process_ready;
29320Smax.romanov@nginx.com 
30320Smax.romanov@nginx.com     /* Process exit/crash notification. */
31320Smax.romanov@nginx.com     nxt_port_handler_t  remove_pid;
32320Smax.romanov@nginx.com 
33320Smax.romanov@nginx.com     /* Stop process command. */
34320Smax.romanov@nginx.com     nxt_port_handler_t  quit;
35320Smax.romanov@nginx.com 
36320Smax.romanov@nginx.com     /* Various data. */
37320Smax.romanov@nginx.com     nxt_port_handler_t  data;
38320Smax.romanov@nginx.com };
39320Smax.romanov@nginx.com 
40320Smax.romanov@nginx.com 
41320Smax.romanov@nginx.com #define nxt_port_handler_idx(name)                                            \
42389Smax.romanov@nginx.com     ( offsetof(nxt_port_handlers_t, name) / sizeof(nxt_port_handler_t) )
43320Smax.romanov@nginx.com 
44320Smax.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 
52320Smax.romanov@nginx.com     _NXT_PORT_MSG_RPC_READY     = nxt_port_handler_idx(rpc_ready),
53320Smax.romanov@nginx.com     _NXT_PORT_MSG_RPC_ERROR     = nxt_port_handler_idx(rpc_error),
54320Smax.romanov@nginx.com 
55320Smax.romanov@nginx.com     _NXT_PORT_MSG_START_WORKER  = nxt_port_handler_idx(start_worker),
56320Smax.romanov@nginx.com     _NXT_PORT_MSG_SOCKET        = nxt_port_handler_idx(socket),
57320Smax.romanov@nginx.com     _NXT_PORT_MSG_MODULES       = nxt_port_handler_idx(modules),
58320Smax.romanov@nginx.com     _NXT_PORT_MSG_CONF_STORE    = nxt_port_handler_idx(conf_store),
59320Smax.romanov@nginx.com 
60320Smax.romanov@nginx.com     _NXT_PORT_MSG_CHANGE_FILE   = nxt_port_handler_idx(change_file),
61320Smax.romanov@nginx.com     _NXT_PORT_MSG_NEW_PORT      = nxt_port_handler_idx(new_port),
62320Smax.romanov@nginx.com     _NXT_PORT_MSG_MMAP          = nxt_port_handler_idx(mmap),
6342Smax.romanov@nginx.com 
64320Smax.romanov@nginx.com     _NXT_PORT_MSG_PROCESS_READY = nxt_port_handler_idx(process_ready),
65320Smax.romanov@nginx.com     _NXT_PORT_MSG_REMOVE_PID    = nxt_port_handler_idx(remove_pid),
66320Smax.romanov@nginx.com     _NXT_PORT_MSG_QUIT          = nxt_port_handler_idx(quit),
67320Smax.romanov@nginx.com 
68320Smax.romanov@nginx.com     _NXT_PORT_MSG_DATA          = nxt_port_handler_idx(data),
69189Smax.romanov@nginx.com 
70320Smax.romanov@nginx.com     NXT_PORT_MSG_MAX            = sizeof(nxt_port_handlers_t) /
71320Smax.romanov@nginx.com                                       sizeof(nxt_port_handler_t),
72320Smax.romanov@nginx.com 
73320Smax.romanov@nginx.com     NXT_PORT_MSG_RPC_READY      = _NXT_PORT_MSG_RPC_READY,
74320Smax.romanov@nginx.com     NXT_PORT_MSG_RPC_READY_LAST = _NXT_PORT_MSG_RPC_READY | NXT_PORT_MSG_LAST,
75320Smax.romanov@nginx.com     NXT_PORT_MSG_RPC_ERROR      = _NXT_PORT_MSG_RPC_ERROR | NXT_PORT_MSG_LAST,
76320Smax.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,
82320Smax.romanov@nginx.com 
83320Smax.romanov@nginx.com     NXT_PORT_MSG_CHANGE_FILE    = _NXT_PORT_MSG_CHANGE_FILE | NXT_PORT_MSG_LAST,
84320Smax.romanov@nginx.com     NXT_PORT_MSG_NEW_PORT       = _NXT_PORT_MSG_NEW_PORT | NXT_PORT_MSG_LAST,
85320Smax.romanov@nginx.com     NXT_PORT_MSG_MMAP           = _NXT_PORT_MSG_MMAP | NXT_PORT_MSG_LAST |
86320Smax.romanov@nginx.com                                   NXT_PORT_MSG_CLOSE_FD | NXT_PORT_MSG_SYNC,
87320Smax.romanov@nginx.com 
88320Smax.romanov@nginx.com     NXT_PORT_MSG_PROCESS_READY  = _NXT_PORT_MSG_PROCESS_READY |
89320Smax.romanov@nginx.com                                   NXT_PORT_MSG_LAST,
90320Smax.romanov@nginx.com     NXT_PORT_MSG_QUIT           = _NXT_PORT_MSG_QUIT | NXT_PORT_MSG_LAST,
91320Smax.romanov@nginx.com     NXT_PORT_MSG_REMOVE_PID     = _NXT_PORT_MSG_REMOVE_PID | NXT_PORT_MSG_LAST,
92320Smax.romanov@nginx.com 
93320Smax.romanov@nginx.com     NXT_PORT_MSG_DATA           = _NXT_PORT_MSG_DATA,
94320Smax.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;
105423Smax.romanov@nginx.com 
106423Smax.romanov@nginx.com     /* Last message for this stream. */
10742Smax.romanov@nginx.com     uint8_t              last;      /* 1 bit */
10842Smax.romanov@nginx.com 
10942Smax.romanov@nginx.com     /* Message data send using mmap, next chunk is a nxt_port_mmap_msg_t. */
11042Smax.romanov@nginx.com     uint8_t              mmap;      /* 1 bit */
111352Smax.romanov@nginx.com 
112423Smax.romanov@nginx.com     /* Non-First fragment in fragmented message sequence. */
113423Smax.romanov@nginx.com     uint8_t              nf;        /* 1 bit */
114423Smax.romanov@nginx.com 
115423Smax.romanov@nginx.com     /* More Fragments followed. */
116423Smax.romanov@nginx.com     uint8_t              mf;        /* 1 bit */
117423Smax.romanov@nginx.com 
118423Smax.romanov@nginx.com     /* Message delivery tracking enabled, next chunk is tracking msg. */
119423Smax.romanov@nginx.com     uint8_t              tracking;  /* 1 bit */
120168Svbart@nginx.com } nxt_port_msg_t;
12114Sigor@sysoev.ru 
12214Sigor@sysoev.ru 
12314Sigor@sysoev.ru typedef struct {
12414Sigor@sysoev.ru     nxt_queue_link_t    link;
12514Sigor@sysoev.ru     nxt_buf_t           *buf;
12614Sigor@sysoev.ru     size_t              share;
12714Sigor@sysoev.ru     nxt_fd_t            fd;
128189Smax.romanov@nginx.com     nxt_bool_t          close_fd;
12914Sigor@sysoev.ru     nxt_port_msg_t      port_msg;
130423Smax.romanov@nginx.com     uint32_t            tracking_msg[2];
131122Smax.romanov@nginx.com 
132122Smax.romanov@nginx.com     nxt_work_t          work;
13314Sigor@sysoev.ru } nxt_port_send_msg_t;
13414Sigor@sysoev.ru 
13514Sigor@sysoev.ru 
13620Sigor@sysoev.ru struct nxt_port_recv_msg_s {
13714Sigor@sysoev.ru     nxt_fd_t            fd;
13814Sigor@sysoev.ru     nxt_buf_t           *buf;
13914Sigor@sysoev.ru     nxt_port_t          *port;
14042Smax.romanov@nginx.com     nxt_port_msg_t      port_msg;
14182Smax.romanov@nginx.com     size_t              size;
142423Smax.romanov@nginx.com     nxt_bool_t          cancelled;
143347Smax.romanov@nginx.com     union {
144347Smax.romanov@nginx.com         nxt_port_t      *new_port;
145347Smax.romanov@nginx.com         nxt_pid_t       removed_pid;
146347Smax.romanov@nginx.com         void            *data;
147347Smax.romanov@nginx.com     } u;
14820Sigor@sysoev.ru };
14914Sigor@sysoev.ru 
150141Smax.romanov@nginx.com typedef struct nxt_app_s  nxt_app_t;
15114Sigor@sysoev.ru 
15214Sigor@sysoev.ru struct nxt_port_s {
15314Sigor@sysoev.ru     nxt_fd_event_t      socket;
15414Sigor@sysoev.ru 
155125Smax.romanov@nginx.com     nxt_queue_link_t    link;       /* for nxt_process_t.ports */
156141Smax.romanov@nginx.com     nxt_process_t       *process;
157141Smax.romanov@nginx.com 
158141Smax.romanov@nginx.com     nxt_queue_link_t    app_link;   /* for nxt_app_t.ports */
159141Smax.romanov@nginx.com     nxt_app_t           *app;
16042Smax.romanov@nginx.com 
16114Sigor@sysoev.ru     nxt_queue_t         messages;   /* of nxt_port_send_msg_t */
162343Smax.romanov@nginx.com     nxt_thread_mutex_t  write_mutex;
16314Sigor@sysoev.ru 
16414Sigor@sysoev.ru     /* Maximum size of message part. */
16514Sigor@sysoev.ru     uint32_t            max_size;
16614Sigor@sysoev.ru     /* Maximum interleave of message parts. */
16714Sigor@sysoev.ru     uint32_t            max_share;
168343Smax.romanov@nginx.com 
169424Smax.romanov@nginx.com     uint32_t            app_pending_responses;
170343Smax.romanov@nginx.com     uint32_t            app_responses;
171*425Smax.romanov@nginx.com     nxt_queue_t         pending_requests;
17214Sigor@sysoev.ru 
17314Sigor@sysoev.ru     nxt_port_handler_t  handler;
174141Smax.romanov@nginx.com     nxt_port_handler_t  *data;
17514Sigor@sysoev.ru 
17665Sigor@sysoev.ru     nxt_mp_t            *mem_pool;
177141Smax.romanov@nginx.com     nxt_event_engine_t  *engine;
178141Smax.romanov@nginx.com 
17914Sigor@sysoev.ru     nxt_buf_t           *free_bufs;
18014Sigor@sysoev.ru     nxt_socket_t        pair[2];
18114Sigor@sysoev.ru 
18242Smax.romanov@nginx.com     nxt_port_id_t       id;
18314Sigor@sysoev.ru     nxt_pid_t           pid;
18442Smax.romanov@nginx.com 
185190Smax.romanov@nginx.com     nxt_lvlhsh_t        rpc_streams; /* stream to nxt_port_rpc_reg_t */
186190Smax.romanov@nginx.com     nxt_lvlhsh_t        rpc_peers;   /* peer to queue of nxt_port_rpc_reg_t */
187190Smax.romanov@nginx.com 
188352Smax.romanov@nginx.com     nxt_lvlhsh_t        frags;
189352Smax.romanov@nginx.com 
190343Smax.romanov@nginx.com     nxt_atomic_t        use_count;
191343Smax.romanov@nginx.com 
192141Smax.romanov@nginx.com     nxt_process_type_t  type;
193197Smax.romanov@nginx.com 
194197Smax.romanov@nginx.com     struct iovec        *iov;
195197Smax.romanov@nginx.com     void                *mmsg_buf;
19614Sigor@sysoev.ru };
19714Sigor@sysoev.ru 
19814Sigor@sysoev.ru 
19911Sigor@sysoev.ru typedef struct {
20042Smax.romanov@nginx.com     nxt_port_id_t       id;
20114Sigor@sysoev.ru     nxt_pid_t           pid;
20214Sigor@sysoev.ru     size_t              max_size;
20314Sigor@sysoev.ru     size_t              max_share;
20442Smax.romanov@nginx.com     nxt_process_type_t  type:8;
205168Svbart@nginx.com } nxt_port_msg_new_port_t;
20611Sigor@sysoev.ru 
20711Sigor@sysoev.ru 
20811Sigor@sysoev.ru /*
20914Sigor@sysoev.ru  * nxt_port_data_t size is allocation size
21014Sigor@sysoev.ru  * which enables effective reuse of memory pool cache.
21111Sigor@sysoev.ru  */
21211Sigor@sysoev.ru typedef union {
21311Sigor@sysoev.ru     nxt_buf_t                buf;
21414Sigor@sysoev.ru     nxt_port_msg_new_port_t  new_port;
21514Sigor@sysoev.ru } nxt_port_data_t;
21611Sigor@sysoev.ru 
21711Sigor@sysoev.ru 
218343Smax.romanov@nginx.com typedef void (*nxt_port_post_handler_t)(nxt_task_t *task, nxt_port_t *port,
219343Smax.romanov@nginx.com     void *data);
220343Smax.romanov@nginx.com 
221197Smax.romanov@nginx.com nxt_port_t *nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid,
222163Smax.romanov@nginx.com     nxt_process_type_t type);
223163Smax.romanov@nginx.com 
224141Smax.romanov@nginx.com nxt_port_id_t nxt_port_get_next_id(void);
225141Smax.romanov@nginx.com void nxt_port_reset_next_id(void);
226141Smax.romanov@nginx.com 
22714Sigor@sysoev.ru nxt_int_t nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port,
22814Sigor@sysoev.ru     size_t max_size);
22914Sigor@sysoev.ru void nxt_port_destroy(nxt_port_t *port);
230343Smax.romanov@nginx.com void nxt_port_close(nxt_task_t *task, nxt_port_t *port);
23114Sigor@sysoev.ru void nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port);
23214Sigor@sysoev.ru void nxt_port_write_close(nxt_port_t *port);
23314Sigor@sysoev.ru void nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port);
23414Sigor@sysoev.ru void nxt_port_read_close(nxt_port_t *port);
235423Smax.romanov@nginx.com nxt_int_t nxt_port_socket_twrite(nxt_task_t *task, nxt_port_t *port,
23642Smax.romanov@nginx.com     nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port,
237423Smax.romanov@nginx.com     nxt_buf_t *b, void *tracking);
238423Smax.romanov@nginx.com 
239423Smax.romanov@nginx.com nxt_inline nxt_int_t
240423Smax.romanov@nginx.com nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port,
241423Smax.romanov@nginx.com     nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port,
242423Smax.romanov@nginx.com     nxt_buf_t *b)
243423Smax.romanov@nginx.com {
244423Smax.romanov@nginx.com     return nxt_port_socket_twrite(task, port, type, fd, stream, reply_port, b,
245423Smax.romanov@nginx.com                                   NULL);
246423Smax.romanov@nginx.com }
24714Sigor@sysoev.ru 
248141Smax.romanov@nginx.com void nxt_port_enable(nxt_task_t *task, nxt_port_t *port,
249320Smax.romanov@nginx.com     nxt_port_handlers_t *handlers);
25081Smax.romanov@nginx.com nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port,
251141Smax.romanov@nginx.com     nxt_port_t *new_port, uint32_t stream);
25220Sigor@sysoev.ru void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt,
25311Sigor@sysoev.ru     nxt_uint_t slot, nxt_fd_t fd);
25411Sigor@sysoev.ru 
25514Sigor@sysoev.ru void nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
25614Sigor@sysoev.ru void nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
257320Smax.romanov@nginx.com void nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
25814Sigor@sysoev.ru void nxt_port_change_log_file_handler(nxt_task_t *task,
25911Sigor@sysoev.ru     nxt_port_recv_msg_t *msg);
26042Smax.romanov@nginx.com void nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
26114Sigor@sysoev.ru void nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
262125Smax.romanov@nginx.com void nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
26314Sigor@sysoev.ru void nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
26411Sigor@sysoev.ru 
265343Smax.romanov@nginx.com nxt_int_t nxt_port_post(nxt_task_t *task, nxt_port_t *port,
266343Smax.romanov@nginx.com     nxt_port_post_handler_t handler, void *data);
267343Smax.romanov@nginx.com void nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i);
26811Sigor@sysoev.ru 
269*425Smax.romanov@nginx.com nxt_inline void nxt_port_inc_use(nxt_port_t *port)
270*425Smax.romanov@nginx.com {
271*425Smax.romanov@nginx.com     nxt_atomic_fetch_add(&port->use_count, 1);
272*425Smax.romanov@nginx.com }
273*425Smax.romanov@nginx.com 
27411Sigor@sysoev.ru #endif /* _NXT_PORT_H_INCLUDED_ */
275