xref: /unit/src/nxt_unit.h (revision 1131:ec7d924d8dfb)
1 
2 /*
3  * Copyright (C) NGINX, Inc.
4  */
5 
6 #ifndef _NXT_UNIT_H_INCLUDED_
7 #define _NXT_UNIT_H_INCLUDED_
8 
9 
10 #include <inttypes.h>
11 #include <sys/types.h>
12 #include <sys/uio.h>
13 #include <string.h>
14 
15 #include "nxt_version.h"
16 #include "nxt_unit_typedefs.h"
17 
18 
19 enum {
20     NXT_UNIT_OK          = 0,
21     NXT_UNIT_ERROR       = 1,
22 };
23 
24 enum {
25     NXT_UNIT_LOG_ALERT   = 0,
26     NXT_UNIT_LOG_ERR     = 1,
27     NXT_UNIT_LOG_WARN    = 2,
28     NXT_UNIT_LOG_NOTICE  = 3,
29     NXT_UNIT_LOG_INFO    = 4,
30     NXT_UNIT_LOG_DEBUG   = 5,
31 };
32 
33 #define NXT_UNIT_INIT_ENV  "NXT_UNIT_INIT"
34 
35 /*
36  * Mostly opaque structure with library state.
37  *
38  * Only user defined 'data' pointer exposed here.  The rest is unit
39  * implementation specific and hidden.
40  */
41 struct nxt_unit_s {
42     void                  *data;  /* User defined data. */
43 };
44 
45 /*
46  * Thread context.
47  *
48  * First (main) context is provided 'for free'.  To receive and process
49  * requests in other thread, one need to allocate context and use it
50  * further in this thread.
51  */
52 struct nxt_unit_ctx_s {
53     void                  *data;  /* User context-specific data. */
54     nxt_unit_t            *unit;
55 };
56 
57 /*
58  * Unit port identification structure.
59  *
60  * Each port can be uniquely identified by listen process id (pid) and port id.
61  * This identification is required to refer the port from different process.
62  */
63 struct nxt_unit_port_id_s {
64     pid_t                 pid;
65     uint32_t              hash;
66     uint16_t              id;
67 };
68 
69 /*
70  * unit provides port storage which is able to store and find the following
71  * data structures.
72  */
73 struct nxt_unit_port_s {
74     nxt_unit_port_id_t    id;
75 
76     int                   in_fd;
77     int                   out_fd;
78 
79     void                  *data;
80 };
81 
82 
83 struct nxt_unit_buf_s {
84     char                  *start;
85     char                  *free;
86     char                  *end;
87 };
88 
89 
90 struct nxt_unit_request_info_s {
91     nxt_unit_t            *unit;
92     nxt_unit_ctx_t        *ctx;
93 
94     nxt_unit_port_id_t    request_port;
95     nxt_unit_port_id_t    response_port;
96 
97     nxt_unit_request_t    *request;
98     nxt_unit_buf_t        *request_buf;
99 
100     nxt_unit_response_t   *response;
101     nxt_unit_buf_t        *response_buf;
102     uint32_t              response_max_fields;
103 
104     nxt_unit_buf_t        *content_buf;
105     uint64_t              content_length;
106 
107     void                  *data;
108 };
109 
110 
111 /*
112  * Set of application-specific callbacks. Application may leave all optional
113  * callbacks as NULL.
114  */
115 struct nxt_unit_callbacks_s {
116     /*
117      * Process request. Unlike all other callback, this callback
118      * need to be defined by application.
119      */
120     void     (*request_handler)(nxt_unit_request_info_t *req);
121 
122     /* Process websocket frame. */
123     void     (*websocket_handler)(nxt_unit_websocket_frame_t *ws);
124 
125     /* Connection closed. */
126     void     (*close_handler)(nxt_unit_request_info_t *req);
127 
128     /* Add new Unit port to communicate with process pid. Optional. */
129     int      (*add_port)(nxt_unit_ctx_t *, nxt_unit_port_t *port);
130 
131     /* Remove previously added port. Optional. */
132     void     (*remove_port)(nxt_unit_ctx_t *, nxt_unit_port_id_t *port_id);
133 
134     /* Remove all data associated with process pid including ports. Optional. */
135     void     (*remove_pid)(nxt_unit_ctx_t *, pid_t pid);
136 
137     /* Gracefully quit the application. Optional. */
138     void     (*quit)(nxt_unit_ctx_t *);
139 
140     /* Send data and control to process pid using port id. Optional. */
141     ssize_t  (*port_send)(nxt_unit_ctx_t *, nxt_unit_port_id_t *port_id,
142                  const void *buf, size_t buf_size,
143                  const void *oob, size_t oob_size);
144 
145     /* Receive data on port id. Optional. */
146     ssize_t  (*port_recv)(nxt_unit_ctx_t *, nxt_unit_port_id_t *port_id,
147                  void *buf, size_t buf_size, void *oob, size_t oob_size);
148 
149 };
150 
151 
152 struct nxt_unit_init_s {
153     void                  *data;     /* Opaque pointer to user-defined data. */
154     void                  *ctx_data; /* Opaque pointer to user-defined data. */
155     int                   max_pending_requests;
156 
157     uint32_t              request_data_size;
158 
159     nxt_unit_callbacks_t  callbacks;
160 
161     nxt_unit_port_t       ready_port;
162     uint32_t              ready_stream;
163     nxt_unit_port_t       read_port;
164     int                   log_fd;
165 };
166 
167 
168 typedef ssize_t (*nxt_unit_read_func_t)(nxt_unit_read_info_t *read_info,
169     void *dst, size_t size);
170 
171 
172 struct nxt_unit_read_info_s {
173     nxt_unit_read_func_t  read;
174     int                   eof;
175     uint32_t              buf_size;
176     void                  *data;
177 };
178 
179 
180 /*
181  * Initialize Unit application library with necessary callbacks and
182  * ready/reply port parameters, send 'READY' response to master.
183  */
184 nxt_unit_ctx_t *nxt_unit_init(nxt_unit_init_t *);
185 
186 /*
187  * Process received message, invoke configured callbacks.
188  *
189  * If application implements it's own event loop, each datagram received
190  * from port socket should be initially processed by unit.  This function
191  * may invoke other application-defined callback for message processing.
192  */
193 int nxt_unit_process_msg(nxt_unit_ctx_t *, nxt_unit_port_id_t *port_id,
194     void *buf, size_t buf_size, void *oob, size_t oob_size);
195 
196 /*
197  * Main function useful in case when application does not have it's own
198  * event loop. nxt_unit_run() starts infinite message wait and process loop.
199  *
200  *  for (;;) {
201  *      app_lib->port_recv(...);
202  *      nxt_unit_process_msg(...);
203  *  }
204  *
205  * The normally function returns when QUIT message received from Unit.
206  */
207 int nxt_unit_run(nxt_unit_ctx_t *);
208 
209 int nxt_unit_run_once(nxt_unit_ctx_t *ctx);
210 
211 /* Destroy application library object. */
212 void nxt_unit_done(nxt_unit_ctx_t *);
213 
214 /*
215  * Allocate and initialize new execution context with new listen port to
216  * process requests in other thread.
217  */
218 nxt_unit_ctx_t *nxt_unit_ctx_alloc(nxt_unit_ctx_t *, void *);
219 
220 /* Free unused context.  It is not required to free main context. */
221 void nxt_unit_ctx_free(nxt_unit_ctx_t *);
222 
223 /* Initialize port_id, calculate hash. */
224 void nxt_unit_port_id_init(nxt_unit_port_id_t *port_id, pid_t pid, uint16_t id);
225 
226 /*
227  * Create extra incoming port, perform all required actions to propogate
228  * the port to Unit server.  Fills structure referenced by port_id with
229  * current pid and new port id.
230  */
231 int nxt_unit_create_send_port(nxt_unit_ctx_t *, nxt_unit_port_id_t *dst,
232     nxt_unit_port_id_t *port_id);
233 
234 /* Default 'add_port' implementation. */
235 int nxt_unit_add_port(nxt_unit_ctx_t *, nxt_unit_port_t *port);
236 
237 /* Find previously added port. */
238 nxt_unit_port_t *nxt_unit_find_port(nxt_unit_ctx_t *,
239     nxt_unit_port_id_t *port_id);
240 
241 /* Find, fill output 'port' and remove port from storage.  */
242 void nxt_unit_find_remove_port(nxt_unit_ctx_t *, nxt_unit_port_id_t *port_id,
243     nxt_unit_port_t *port);
244 
245 /* Default 'remove_port' implementation. */
246 void nxt_unit_remove_port(nxt_unit_ctx_t *, nxt_unit_port_id_t *port_id);
247 
248 /* Default 'remove_pid' implementation. */
249 void nxt_unit_remove_pid(nxt_unit_ctx_t *, pid_t pid);
250 
251 /* Default 'quit' implementation. */
252 void nxt_unit_quit(nxt_unit_ctx_t *);
253 
254 /* Default 'port_send' implementation. */
255 ssize_t nxt_unit_port_send(nxt_unit_ctx_t *, int fd,
256     const void *buf, size_t buf_size,
257     const void *oob, size_t oob_size);
258 
259 /* Default 'port_recv' implementation. */
260 ssize_t nxt_unit_port_recv(nxt_unit_ctx_t *, int fd, void *buf, size_t buf_size,
261     void *oob, size_t oob_size);
262 
263 /* Calculates hash for given field name. */
264 uint16_t nxt_unit_field_hash(const char* name, size_t name_length);
265 
266 /* Split host for server name and port. */
267 void nxt_unit_split_host(char *host_start, uint32_t host_length,
268     char **name, uint32_t *name_length, char **port, uint32_t *port_length);
269 
270 /* Group duplicate fields for easy enumeration. */
271 void nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req);
272 
273 /*
274  * Allocate response structure capable to store limited numer of fields.
275  * The structure may be accessed directly via req->response pointer or
276  * filled step-by-step using functions add_field and add_content.
277  */
278 int nxt_unit_response_init(nxt_unit_request_info_t *req,
279     uint16_t status, uint32_t max_fields_count, uint32_t max_fields_size);
280 
281 int nxt_unit_response_realloc(nxt_unit_request_info_t *req,
282     uint32_t max_fields_count, uint32_t max_fields_size);
283 
284 int nxt_unit_response_is_init(nxt_unit_request_info_t *req);
285 
286 int nxt_unit_response_add_field(nxt_unit_request_info_t *req,
287     const char* name, uint8_t name_length,
288     const char* value, uint32_t value_length);
289 
290 int nxt_unit_response_add_content(nxt_unit_request_info_t *req,
291     const void* src, uint32_t size);
292 
293 /*
294  * Send prepared response to Unit server.  Response structure destroyed during
295  * this call.
296  */
297 int nxt_unit_response_send(nxt_unit_request_info_t *req);
298 
299 int nxt_unit_response_is_sent(nxt_unit_request_info_t *req);
300 
301 nxt_unit_buf_t *nxt_unit_response_buf_alloc(nxt_unit_request_info_t *req,
302     uint32_t size);
303 
304 int nxt_unit_request_is_websocket_handshake(nxt_unit_request_info_t *req);
305 
306 int nxt_unit_response_upgrade(nxt_unit_request_info_t *req);
307 
308 int nxt_unit_response_is_websocket(nxt_unit_request_info_t *req);
309 
310 nxt_unit_request_info_t *nxt_unit_get_request_info_from_data(void *data);
311 
312 int nxt_unit_buf_send(nxt_unit_buf_t *buf);
313 
314 void nxt_unit_buf_free(nxt_unit_buf_t *buf);
315 
316 nxt_unit_buf_t *nxt_unit_buf_next(nxt_unit_buf_t *buf);
317 
318 uint32_t nxt_unit_buf_max(void);
319 
320 uint32_t nxt_unit_buf_min(void);
321 
322 int nxt_unit_response_write(nxt_unit_request_info_t *req, const void *start,
323     size_t size);
324 
325 int nxt_unit_response_write_cb(nxt_unit_request_info_t *req,
326     nxt_unit_read_info_t *read_info);
327 
328 ssize_t nxt_unit_request_read(nxt_unit_request_info_t *req, void *dst,
329     size_t size);
330 
331 void nxt_unit_request_done(nxt_unit_request_info_t *req, int rc);
332 
333 
334 int nxt_unit_websocket_send(nxt_unit_request_info_t *req, uint8_t opcode,
335     uint8_t last, const void *start, size_t size);
336 
337 int nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode,
338     uint8_t last, const struct iovec *iov, int iovcnt);
339 
340 ssize_t nxt_unit_websocket_read(nxt_unit_websocket_frame_t *ws, void *dst,
341     size_t size);
342 
343 int nxt_unit_websocket_retain(nxt_unit_websocket_frame_t *ws);
344 
345 void nxt_unit_websocket_done(nxt_unit_websocket_frame_t *ws);
346 
347 
348 void nxt_unit_log(nxt_unit_ctx_t *ctx, int level, const char* fmt, ...);
349 
350 void nxt_unit_req_log(nxt_unit_request_info_t *req, int level,
351     const char* fmt, ...);
352 
353 #if (NXT_DEBUG)
354 
355 #define nxt_unit_debug(ctx, fmt, ARGS...) \
356     nxt_unit_log(ctx, NXT_UNIT_LOG_DEBUG, fmt, ##ARGS)
357 
358 #define nxt_unit_req_debug(req, fmt, ARGS...) \
359     nxt_unit_req_log(req, NXT_UNIT_LOG_DEBUG, fmt, ##ARGS)
360 
361 #else
362 
363 #define nxt_unit_debug(ctx, fmt, ARGS...)
364 
365 #define nxt_unit_req_debug(req, fmt, ARGS...)
366 
367 #endif
368 
369 
370 #define nxt_unit_warn(ctx, fmt, ARGS...) \
371     nxt_unit_log(ctx, NXT_UNIT_LOG_WARN, fmt, ##ARGS)
372 
373 #define nxt_unit_req_warn(req, fmt, ARGS...) \
374     nxt_unit_req_log(req, NXT_UNIT_LOG_WARN, fmt, ##ARGS)
375 
376 #define nxt_unit_error(ctx, fmt, ARGS...) \
377     nxt_unit_log(ctx, NXT_UNIT_LOG_ERR, fmt, ##ARGS)
378 
379 #define nxt_unit_req_error(req, fmt, ARGS...) \
380     nxt_unit_req_log(req, NXT_UNIT_LOG_ERR, fmt, ##ARGS)
381 
382 #define nxt_unit_alert(ctx, fmt, ARGS...) \
383     nxt_unit_log(ctx, NXT_UNIT_LOG_ALERT, fmt, ##ARGS)
384 
385 #define nxt_unit_req_alert(req, fmt, ARGS...) \
386     nxt_unit_req_log(req, NXT_UNIT_LOG_ALERT, fmt, ##ARGS)
387 
388 
389 #endif /* _NXT_UNIT_H_INCLUDED_ */
390