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