xref: /unit/src/nxt_port.h (revision 1302)
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;
21774Svbart@nginx.com     nxt_port_handler_t  cert_get;
22774Svbart@nginx.com     nxt_port_handler_t  cert_delete;
23630Svbart@nginx.com     nxt_port_handler_t  access_log;
24320Smax.romanov@nginx.com 
25320Smax.romanov@nginx.com     /* File descriptor exchange. */
26320Smax.romanov@nginx.com     nxt_port_handler_t  change_file;
27320Smax.romanov@nginx.com     nxt_port_handler_t  new_port;
28320Smax.romanov@nginx.com     nxt_port_handler_t  mmap;
29320Smax.romanov@nginx.com 
30320Smax.romanov@nginx.com     /* New process ready. */
31320Smax.romanov@nginx.com     nxt_port_handler_t  process_ready;
32320Smax.romanov@nginx.com 
33320Smax.romanov@nginx.com     /* Process exit/crash notification. */
34320Smax.romanov@nginx.com     nxt_port_handler_t  remove_pid;
35320Smax.romanov@nginx.com 
36320Smax.romanov@nginx.com     /* Stop process command. */
37320Smax.romanov@nginx.com     nxt_port_handler_t  quit;
38320Smax.romanov@nginx.com 
391131Smax.romanov@nginx.com     /* Request headers. */
401131Smax.romanov@nginx.com     nxt_port_handler_t  req_headers;
411131Smax.romanov@nginx.com 
421131Smax.romanov@nginx.com     /* Websocket frame. */
431131Smax.romanov@nginx.com     nxt_port_handler_t  websocket_frame;
441131Smax.romanov@nginx.com 
45320Smax.romanov@nginx.com     /* Various data. */
46320Smax.romanov@nginx.com     nxt_port_handler_t  data;
47320Smax.romanov@nginx.com };
48320Smax.romanov@nginx.com 
49320Smax.romanov@nginx.com 
50320Smax.romanov@nginx.com #define nxt_port_handler_idx(name)                                            \
51389Smax.romanov@nginx.com     ( offsetof(nxt_port_handlers_t, name) / sizeof(nxt_port_handler_t) )
52320Smax.romanov@nginx.com 
53320Smax.romanov@nginx.com 
5442Smax.romanov@nginx.com typedef enum {
55189Smax.romanov@nginx.com     NXT_PORT_MSG_LAST           = 0x100,
56189Smax.romanov@nginx.com     NXT_PORT_MSG_CLOSE_FD       = 0x200,
57205Smax.romanov@nginx.com     NXT_PORT_MSG_SYNC           = 0x400,
58189Smax.romanov@nginx.com 
59189Smax.romanov@nginx.com     NXT_PORT_MSG_MASK           = 0xFF,
60189Smax.romanov@nginx.com 
61320Smax.romanov@nginx.com     _NXT_PORT_MSG_RPC_READY     = nxt_port_handler_idx(rpc_ready),
62320Smax.romanov@nginx.com     _NXT_PORT_MSG_RPC_ERROR     = nxt_port_handler_idx(rpc_error),
63320Smax.romanov@nginx.com 
64320Smax.romanov@nginx.com     _NXT_PORT_MSG_START_WORKER  = nxt_port_handler_idx(start_worker),
65320Smax.romanov@nginx.com     _NXT_PORT_MSG_SOCKET        = nxt_port_handler_idx(socket),
66320Smax.romanov@nginx.com     _NXT_PORT_MSG_MODULES       = nxt_port_handler_idx(modules),
67320Smax.romanov@nginx.com     _NXT_PORT_MSG_CONF_STORE    = nxt_port_handler_idx(conf_store),
68774Svbart@nginx.com     _NXT_PORT_MSG_CERT_GET      = nxt_port_handler_idx(cert_get),
69774Svbart@nginx.com     _NXT_PORT_MSG_CERT_DELETE   = nxt_port_handler_idx(cert_delete),
70630Svbart@nginx.com     _NXT_PORT_MSG_ACCESS_LOG    = nxt_port_handler_idx(access_log),
71320Smax.romanov@nginx.com 
72320Smax.romanov@nginx.com     _NXT_PORT_MSG_CHANGE_FILE   = nxt_port_handler_idx(change_file),
73320Smax.romanov@nginx.com     _NXT_PORT_MSG_NEW_PORT      = nxt_port_handler_idx(new_port),
74320Smax.romanov@nginx.com     _NXT_PORT_MSG_MMAP          = nxt_port_handler_idx(mmap),
7542Smax.romanov@nginx.com 
76320Smax.romanov@nginx.com     _NXT_PORT_MSG_PROCESS_READY = nxt_port_handler_idx(process_ready),
77320Smax.romanov@nginx.com     _NXT_PORT_MSG_REMOVE_PID    = nxt_port_handler_idx(remove_pid),
78320Smax.romanov@nginx.com     _NXT_PORT_MSG_QUIT          = nxt_port_handler_idx(quit),
79320Smax.romanov@nginx.com 
801131Smax.romanov@nginx.com     _NXT_PORT_MSG_REQ_HEADERS   = nxt_port_handler_idx(req_headers),
811131Smax.romanov@nginx.com     _NXT_PORT_MSG_WEBSOCKET     = nxt_port_handler_idx(websocket_frame),
821131Smax.romanov@nginx.com 
83320Smax.romanov@nginx.com     _NXT_PORT_MSG_DATA          = nxt_port_handler_idx(data),
84189Smax.romanov@nginx.com 
85613Svbart@nginx.com     NXT_PORT_MSG_MAX            = sizeof(nxt_port_handlers_t)
86613Svbart@nginx.com                                   / sizeof(nxt_port_handler_t),
87320Smax.romanov@nginx.com 
88320Smax.romanov@nginx.com     NXT_PORT_MSG_RPC_READY      = _NXT_PORT_MSG_RPC_READY,
89320Smax.romanov@nginx.com     NXT_PORT_MSG_RPC_READY_LAST = _NXT_PORT_MSG_RPC_READY | NXT_PORT_MSG_LAST,
90320Smax.romanov@nginx.com     NXT_PORT_MSG_RPC_ERROR      = _NXT_PORT_MSG_RPC_ERROR | NXT_PORT_MSG_LAST,
91320Smax.romanov@nginx.com 
92613Svbart@nginx.com     NXT_PORT_MSG_START_WORKER   = _NXT_PORT_MSG_START_WORKER
93613Svbart@nginx.com                                   | NXT_PORT_MSG_LAST,
94198Sigor@sysoev.ru     NXT_PORT_MSG_SOCKET         = _NXT_PORT_MSG_SOCKET | NXT_PORT_MSG_LAST,
95216Sigor@sysoev.ru     NXT_PORT_MSG_MODULES        = _NXT_PORT_MSG_MODULES | NXT_PORT_MSG_LAST,
96314Svbart@nginx.com     NXT_PORT_MSG_CONF_STORE     = _NXT_PORT_MSG_CONF_STORE | NXT_PORT_MSG_LAST,
97774Svbart@nginx.com     NXT_PORT_MSG_CERT_GET       = _NXT_PORT_MSG_CERT_GET | NXT_PORT_MSG_LAST,
98774Svbart@nginx.com     NXT_PORT_MSG_CERT_DELETE    = _NXT_PORT_MSG_CERT_DELETE | NXT_PORT_MSG_LAST,
99630Svbart@nginx.com     NXT_PORT_MSG_ACCESS_LOG     = _NXT_PORT_MSG_ACCESS_LOG | NXT_PORT_MSG_LAST,
100320Smax.romanov@nginx.com 
101320Smax.romanov@nginx.com     NXT_PORT_MSG_CHANGE_FILE    = _NXT_PORT_MSG_CHANGE_FILE | NXT_PORT_MSG_LAST,
102320Smax.romanov@nginx.com     NXT_PORT_MSG_NEW_PORT       = _NXT_PORT_MSG_NEW_PORT | NXT_PORT_MSG_LAST,
103613Svbart@nginx.com     NXT_PORT_MSG_MMAP           = _NXT_PORT_MSG_MMAP | NXT_PORT_MSG_LAST
104613Svbart@nginx.com                                   | NXT_PORT_MSG_CLOSE_FD | NXT_PORT_MSG_SYNC,
105320Smax.romanov@nginx.com 
106613Svbart@nginx.com     NXT_PORT_MSG_PROCESS_READY  = _NXT_PORT_MSG_PROCESS_READY
107613Svbart@nginx.com                                   | NXT_PORT_MSG_LAST,
108320Smax.romanov@nginx.com     NXT_PORT_MSG_QUIT           = _NXT_PORT_MSG_QUIT | NXT_PORT_MSG_LAST,
109320Smax.romanov@nginx.com     NXT_PORT_MSG_REMOVE_PID     = _NXT_PORT_MSG_REMOVE_PID | NXT_PORT_MSG_LAST,
110320Smax.romanov@nginx.com 
1111131Smax.romanov@nginx.com     NXT_PORT_MSG_REQ_HEADERS    = _NXT_PORT_MSG_REQ_HEADERS,
1121131Smax.romanov@nginx.com     NXT_PORT_MSG_WEBSOCKET      = _NXT_PORT_MSG_WEBSOCKET,
1131131Smax.romanov@nginx.com     NXT_PORT_MSG_WEBSOCKET_LAST = _NXT_PORT_MSG_WEBSOCKET | NXT_PORT_MSG_LAST,
1141131Smax.romanov@nginx.com 
115320Smax.romanov@nginx.com     NXT_PORT_MSG_DATA           = _NXT_PORT_MSG_DATA,
116320Smax.romanov@nginx.com     NXT_PORT_MSG_DATA_LAST      = _NXT_PORT_MSG_DATA | NXT_PORT_MSG_LAST,
117125Smax.romanov@nginx.com } nxt_port_msg_type_t;
11842Smax.romanov@nginx.com 
11914Sigor@sysoev.ru 
12042Smax.romanov@nginx.com /* Passed as a first iov chunk. */
12142Smax.romanov@nginx.com typedef struct {
12242Smax.romanov@nginx.com     uint32_t             stream;
12342Smax.romanov@nginx.com     nxt_pid_t            pid;
12442Smax.romanov@nginx.com     nxt_port_id_t        reply_port;
12542Smax.romanov@nginx.com 
126189Smax.romanov@nginx.com     uint8_t              type;
127423Smax.romanov@nginx.com 
128423Smax.romanov@nginx.com     /* Last message for this stream. */
12942Smax.romanov@nginx.com     uint8_t              last;      /* 1 bit */
13042Smax.romanov@nginx.com 
13142Smax.romanov@nginx.com     /* Message data send using mmap, next chunk is a nxt_port_mmap_msg_t. */
13242Smax.romanov@nginx.com     uint8_t              mmap;      /* 1 bit */
133352Smax.romanov@nginx.com 
134423Smax.romanov@nginx.com     /* Non-First fragment in fragmented message sequence. */
135423Smax.romanov@nginx.com     uint8_t              nf;        /* 1 bit */
136423Smax.romanov@nginx.com 
137423Smax.romanov@nginx.com     /* More Fragments followed. */
138423Smax.romanov@nginx.com     uint8_t              mf;        /* 1 bit */
139423Smax.romanov@nginx.com 
140423Smax.romanov@nginx.com     /* Message delivery tracking enabled, next chunk is tracking msg. */
141423Smax.romanov@nginx.com     uint8_t              tracking;  /* 1 bit */
142168Svbart@nginx.com } nxt_port_msg_t;
14314Sigor@sysoev.ru 
14414Sigor@sysoev.ru 
14514Sigor@sysoev.ru typedef struct {
14614Sigor@sysoev.ru     nxt_queue_link_t    link;
14714Sigor@sysoev.ru     nxt_buf_t           *buf;
14814Sigor@sysoev.ru     size_t              share;
14914Sigor@sysoev.ru     nxt_fd_t            fd;
15014Sigor@sysoev.ru     nxt_port_msg_t      port_msg;
151423Smax.romanov@nginx.com     uint32_t            tracking_msg[2];
1521125Smax.romanov@nginx.com     uint8_t             close_fd;   /* 1 bit */
1531125Smax.romanov@nginx.com     uint8_t             allocated;  /* 1 bit */
15414Sigor@sysoev.ru } nxt_port_send_msg_t;
15514Sigor@sysoev.ru 
15614Sigor@sysoev.ru 
15720Sigor@sysoev.ru struct nxt_port_recv_msg_s {
15814Sigor@sysoev.ru     nxt_fd_t            fd;
15914Sigor@sysoev.ru     nxt_buf_t           *buf;
16014Sigor@sysoev.ru     nxt_port_t          *port;
16142Smax.romanov@nginx.com     nxt_port_msg_t      port_msg;
16282Smax.romanov@nginx.com     size_t              size;
163423Smax.romanov@nginx.com     nxt_bool_t          cancelled;
164347Smax.romanov@nginx.com     union {
165347Smax.romanov@nginx.com         nxt_port_t      *new_port;
166347Smax.romanov@nginx.com         nxt_pid_t       removed_pid;
167347Smax.romanov@nginx.com         void            *data;
168347Smax.romanov@nginx.com     } u;
16920Sigor@sysoev.ru };
17014Sigor@sysoev.ru 
171141Smax.romanov@nginx.com typedef struct nxt_app_s  nxt_app_t;
17214Sigor@sysoev.ru 
17314Sigor@sysoev.ru struct nxt_port_s {
17414Sigor@sysoev.ru     nxt_fd_event_t      socket;
17514Sigor@sysoev.ru 
176125Smax.romanov@nginx.com     nxt_queue_link_t    link;       /* for nxt_process_t.ports */
177141Smax.romanov@nginx.com     nxt_process_t       *process;
178141Smax.romanov@nginx.com 
179141Smax.romanov@nginx.com     nxt_queue_link_t    app_link;   /* for nxt_app_t.ports */
180141Smax.romanov@nginx.com     nxt_app_t           *app;
18142Smax.romanov@nginx.com 
182507Smax.romanov@nginx.com     nxt_queue_link_t    idle_link;  /* for nxt_app_t.idle_ports */
183507Smax.romanov@nginx.com     nxt_msec_t          idle_start;
184507Smax.romanov@nginx.com 
18514Sigor@sysoev.ru     nxt_queue_t         messages;   /* of nxt_port_send_msg_t */
186343Smax.romanov@nginx.com     nxt_thread_mutex_t  write_mutex;
18714Sigor@sysoev.ru 
18814Sigor@sysoev.ru     /* Maximum size of message part. */
18914Sigor@sysoev.ru     uint32_t            max_size;
19014Sigor@sysoev.ru     /* Maximum interleave of message parts. */
19114Sigor@sysoev.ru     uint32_t            max_share;
192343Smax.romanov@nginx.com 
193424Smax.romanov@nginx.com     uint32_t            app_pending_responses;
194343Smax.romanov@nginx.com     uint32_t            app_responses;
195425Smax.romanov@nginx.com     nxt_queue_t         pending_requests;
19614Sigor@sysoev.ru 
1971131Smax.romanov@nginx.com     nxt_queue_t         active_websockets;
1981131Smax.romanov@nginx.com 
19914Sigor@sysoev.ru     nxt_port_handler_t  handler;
200141Smax.romanov@nginx.com     nxt_port_handler_t  *data;
20114Sigor@sysoev.ru 
20265Sigor@sysoev.ru     nxt_mp_t            *mem_pool;
203141Smax.romanov@nginx.com     nxt_event_engine_t  *engine;
204141Smax.romanov@nginx.com 
20514Sigor@sysoev.ru     nxt_buf_t           *free_bufs;
20614Sigor@sysoev.ru     nxt_socket_t        pair[2];
20714Sigor@sysoev.ru 
20842Smax.romanov@nginx.com     nxt_port_id_t       id;
20914Sigor@sysoev.ru     nxt_pid_t           pid;
21042Smax.romanov@nginx.com 
211190Smax.romanov@nginx.com     nxt_lvlhsh_t        rpc_streams; /* stream to nxt_port_rpc_reg_t */
212190Smax.romanov@nginx.com     nxt_lvlhsh_t        rpc_peers;   /* peer to queue of nxt_port_rpc_reg_t */
213190Smax.romanov@nginx.com 
214352Smax.romanov@nginx.com     nxt_lvlhsh_t        frags;
215352Smax.romanov@nginx.com 
216343Smax.romanov@nginx.com     nxt_atomic_t        use_count;
217343Smax.romanov@nginx.com 
218141Smax.romanov@nginx.com     nxt_process_type_t  type;
21914Sigor@sysoev.ru };
22014Sigor@sysoev.ru 
22114Sigor@sysoev.ru 
22211Sigor@sysoev.ru typedef struct {
22342Smax.romanov@nginx.com     nxt_port_id_t       id;
22414Sigor@sysoev.ru     nxt_pid_t           pid;
22514Sigor@sysoev.ru     size_t              max_size;
22614Sigor@sysoev.ru     size_t              max_share;
22742Smax.romanov@nginx.com     nxt_process_type_t  type:8;
228168Svbart@nginx.com } nxt_port_msg_new_port_t;
22911Sigor@sysoev.ru 
23011Sigor@sysoev.ru 
23111Sigor@sysoev.ru /*
23214Sigor@sysoev.ru  * nxt_port_data_t size is allocation size
23314Sigor@sysoev.ru  * which enables effective reuse of memory pool cache.
23411Sigor@sysoev.ru  */
23511Sigor@sysoev.ru typedef union {
23611Sigor@sysoev.ru     nxt_buf_t                buf;
23714Sigor@sysoev.ru     nxt_port_msg_new_port_t  new_port;
23814Sigor@sysoev.ru } nxt_port_data_t;
23911Sigor@sysoev.ru 
24011Sigor@sysoev.ru 
241343Smax.romanov@nginx.com typedef void (*nxt_port_post_handler_t)(nxt_task_t *task, nxt_port_t *port,
242343Smax.romanov@nginx.com     void *data);
243343Smax.romanov@nginx.com 
244197Smax.romanov@nginx.com nxt_port_t *nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid,
245163Smax.romanov@nginx.com     nxt_process_type_t type);
246163Smax.romanov@nginx.com 
247141Smax.romanov@nginx.com nxt_port_id_t nxt_port_get_next_id(void);
248141Smax.romanov@nginx.com void nxt_port_reset_next_id(void);
249141Smax.romanov@nginx.com 
25014Sigor@sysoev.ru nxt_int_t nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port,
25114Sigor@sysoev.ru     size_t max_size);
25214Sigor@sysoev.ru void nxt_port_destroy(nxt_port_t *port);
253343Smax.romanov@nginx.com void nxt_port_close(nxt_task_t *task, nxt_port_t *port);
25414Sigor@sysoev.ru void nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port);
25514Sigor@sysoev.ru void nxt_port_write_close(nxt_port_t *port);
25614Sigor@sysoev.ru void nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port);
25714Sigor@sysoev.ru void nxt_port_read_close(nxt_port_t *port);
258423Smax.romanov@nginx.com nxt_int_t nxt_port_socket_twrite(nxt_task_t *task, nxt_port_t *port,
25942Smax.romanov@nginx.com     nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port,
260423Smax.romanov@nginx.com     nxt_buf_t *b, void *tracking);
261423Smax.romanov@nginx.com 
262423Smax.romanov@nginx.com nxt_inline nxt_int_t
263423Smax.romanov@nginx.com nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port,
264423Smax.romanov@nginx.com     nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port,
265423Smax.romanov@nginx.com     nxt_buf_t *b)
266423Smax.romanov@nginx.com {
267423Smax.romanov@nginx.com     return nxt_port_socket_twrite(task, port, type, fd, stream, reply_port, b,
268423Smax.romanov@nginx.com                                   NULL);
269423Smax.romanov@nginx.com }
27014Sigor@sysoev.ru 
271141Smax.romanov@nginx.com void nxt_port_enable(nxt_task_t *task, nxt_port_t *port,
272*1302St.nateldemoura@f5.com     const nxt_port_handlers_t *handlers);
27381Smax.romanov@nginx.com nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port,
274141Smax.romanov@nginx.com     nxt_port_t *new_port, uint32_t stream);
27520Sigor@sysoev.ru void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt,
27611Sigor@sysoev.ru     nxt_uint_t slot, nxt_fd_t fd);
27711Sigor@sysoev.ru 
27814Sigor@sysoev.ru void nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
27914Sigor@sysoev.ru void nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
280320Smax.romanov@nginx.com void nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
28114Sigor@sysoev.ru void nxt_port_change_log_file_handler(nxt_task_t *task,
28211Sigor@sysoev.ru     nxt_port_recv_msg_t *msg);
28342Smax.romanov@nginx.com void nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
28414Sigor@sysoev.ru void nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
285125Smax.romanov@nginx.com void nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
28614Sigor@sysoev.ru void nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
28711Sigor@sysoev.ru 
288343Smax.romanov@nginx.com nxt_int_t nxt_port_post(nxt_task_t *task, nxt_port_t *port,
289343Smax.romanov@nginx.com     nxt_port_post_handler_t handler, void *data);
290343Smax.romanov@nginx.com void nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i);
29111Sigor@sysoev.ru 
292425Smax.romanov@nginx.com nxt_inline void nxt_port_inc_use(nxt_port_t *port)
293425Smax.romanov@nginx.com {
294425Smax.romanov@nginx.com     nxt_atomic_fetch_add(&port->use_count, 1);
295425Smax.romanov@nginx.com }
296425Smax.romanov@nginx.com 
29711Sigor@sysoev.ru #endif /* _NXT_PORT_H_INCLUDED_ */
298