-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathngx_http_mtask_module.c
584 lines (392 loc) · 13.3 KB
/
ngx_http_mtask_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/******************************************************************************
Copyright (c) 2011-2012, Roman Arutyunyan (arut@qip.ru)
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*******************************************************************************/
/*
NGINX module providing userspace cooperative multitasking
for IO-bound content handlers
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ucontext.h>
#include <dlfcn.h>
#include <poll.h>
#include "ngx_http_mtask_module.h"
/* NB: NGINX logger is greedy of stack; use > 4k for safety */
#define MTASK_DEFAULT_STACK_SIZE 65536
#define MTASK_DEFAULT_TIMEOUT 10000
static ngx_int_t ngx_http_mtask_init(ngx_conf_t *cf);
static void* ngx_http_mtask_create_loc_conf(ngx_conf_t *cf);
static char* ngx_http_mtask_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
struct ngx_http_mtask_ctx_s {
/* current handler contexts: wake & return */
ucontext_t wctx, rctx;
int timedout;
};
typedef struct ngx_http_mtask_ctx_s ngx_http_mtask_ctx_t;
/* Module commands */
static ngx_command_t ngx_http_mtask_commands[] = {
{ ngx_string("mtask_stack"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mtask_loc_conf_t, stack_size),
NULL },
{ ngx_string("mtask_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_mtask_loc_conf_t, timeout),
NULL },
ngx_null_command
};
/* Module context */
static ngx_http_module_t ngx_http_mtask_module_ctx = {
NULL, /* preconfiguration */
ngx_http_mtask_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_mtask_create_loc_conf, /* create location configuration */
ngx_http_mtask_merge_loc_conf /* merge location configuration */
};
/* Module */
ngx_module_t ngx_http_mtask_module = {
NGX_MODULE_V1,
&ngx_http_mtask_module_ctx, /* module context */
ngx_http_mtask_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
#define MTASK_WAKE_TIMEDOUT 0x01
#define MTASK_WAKE_NOFINALIZE 0x02
/* The request pointer is read by intercepted functions.
If non-NULL then control is yielded to caller context
and callback IO event is scheduled for later activation.
If NULL then usual behaivior takes place.
*/
/* NB: add __thread for multithreaded case */
static ngx_http_request_t *mtask_req;
#define mtask_current (mtask_req)
#define mtask_setcurrent(r) (mtask_req = (r))
#define mtask_resetcurrent() mtask_setcurrent(NULL)
#define mtask_scheduled (mtask_current != NULL)
static void mtask_proc() {
ngx_http_mtask_loc_conf_t *mlcf;
ngx_http_mtask_ctx_t *ctx;
ngx_http_request_t *r = mtask_current;
ngx_connection_t *c;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mtask_current->connection->log, 0,
"mtask proc start");
mlcf = ngx_http_get_module_loc_conf(r, ngx_http_mtask_module);
c = r->connection;
/* prevent flushing data to socket
because we cannot use blocking syscalls
which are intercepted to switch context */
c->write->delayed = 1;
if (mlcf->handler == NULL
|| mlcf->handler(r) != NGX_OK)
{
ngx_log_error(NGX_LOG_ALERT, mtask_current->connection->log, 0,
"mtask proc error");
r->headers_out.status = NGX_HTTP_INTERNAL_SERVER_ERROR;
r->header_only = 1;
r->headers_out.content_length_n = 0;
ngx_http_send_header(r);
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mtask_current->connection->log, 0,
"mtask proc end");
}
c->write->delayed = 0;
mtask_resetcurrent();
/* push data */
ngx_http_output_filter(r, NULL);
ctx = ngx_http_get_module_ctx(r, ngx_http_mtask_module);
setcontext(&ctx->rctx);
}
static int mtask_wake(ngx_http_request_t *r, int flags) {
ngx_http_mtask_ctx_t *ctx;
ngx_connection_t *c;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mtask wake");
ctx = ngx_http_get_module_ctx(r, ngx_http_mtask_module);
mtask_setcurrent(r);
if (flags & MTASK_WAKE_TIMEDOUT)
ctx->timedout = 1;
swapcontext(&ctx->rctx, &ctx->wctx);
if (!mtask_scheduled) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mtask finalize");
if (!(flags & MTASK_WAKE_NOFINALIZE)) {
c = r->connection;
ngx_http_finalize_request(r, NGX_OK);
/* we need this if this is subrequest
to continue parent request */
ngx_http_run_posted_requests(c);
}
return 1;
}
mtask_resetcurrent();
return 0;
}
static void mtask_event_handler(ngx_event_t *ev) {
ngx_http_request_t *r;
ngx_connection_t *c;
int wf = 0;
c = ev->data;
r = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mtask event");
if (ev->timedout) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mtask timeout");
wf |= MTASK_WAKE_TIMEDOUT;
}
mtask_wake(r, wf);
}
/* returns 1 on timeout */
static int mtask_yield(int fd, ngx_int_t event) {
ngx_http_mtask_ctx_t *ctx;
ngx_connection_t *c;
ngx_event_t *e;
ngx_http_mtask_loc_conf_t *mlcf;
mlcf = ngx_http_get_module_loc_conf(mtask_current, ngx_http_mtask_module);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mtask_current->connection->log, 0,
"mtask yield '%V' (%s)",
&mtask_current->uri,
event & NGX_WRITE_EVENT ? "write" : "read");
ctx = ngx_http_get_module_ctx(mtask_current, ngx_http_mtask_module);
c = ngx_get_connection(fd, mtask_current->connection->log);
c->data = mtask_current;
if (event == NGX_READ_EVENT)
e = c->read;
else
e = c->write;
e->data = c;
e->handler = &mtask_event_handler;
e->log = mtask_current->connection->log;
if (mlcf->timeout != NGX_CONF_UNSET_MSEC)
ngx_add_timer(e, mlcf->timeout);
ngx_add_event(e, event, 0);
ctx->timedout = 0;
swapcontext(&ctx->wctx, &ctx->rctx);
if (e->timer_set)
ngx_del_timer(e);
ngx_del_event(e, event, 0);
ngx_free_connection(c);
return ctx->timedout;
}
/* main request handler */
static ngx_int_t ngx_http_mtask_handler(ngx_http_request_t *r) {
ngx_http_mtask_loc_conf_t *mlcf;
ngx_http_mtask_ctx_t *ctx;
mlcf = ngx_http_get_module_loc_conf(r, ngx_http_mtask_module);
if (mlcf->handler == NULL)
return NGX_DECLINED;
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_mtask_ctx_t));
if (ctx == NULL)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
ngx_http_set_ctx(r, ctx, ngx_http_mtask_module);
getcontext(&ctx->wctx);
ctx->wctx.uc_stack.ss_sp = ngx_palloc(r->pool, mlcf->stack_size);
ctx->wctx.uc_stack.ss_size = mlcf->stack_size;
ctx->wctx.uc_stack.ss_flags = 0;
ctx->wctx.uc_link = NULL;
makecontext(&ctx->wctx, &mtask_proc, 0);
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mtask init");
if (mtask_wake(r, MTASK_WAKE_NOFINALIZE)) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mtask fast result");
return NGX_OK;
}
r->main->count++;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"mtask detach");
return NGX_DONE;
}
/* Syscalls interceptors */
typedef int (*accept_pt)(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
static accept_pt orig_accept;
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
ssize_t ret;
int flags;
if (mtask_scheduled) {
flags = fcntl(sockfd, F_GETFL, 0);
if (!(flags & O_NONBLOCK))
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
}
for(;;) {
ret = orig_accept(sockfd, addr, addrlen);
if (!mtask_scheduled || ret != -1 || errno != EAGAIN)
return ret;
if (mtask_yield(sockfd, NGX_READ_EVENT)) {
/* timeout */
errno = EINVAL;
return -1;
}
}
}
typedef int (*connect_pt)(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
static connect_pt orig_connect;
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
ssize_t ret;
int flags;
socklen_t len;
if (mtask_scheduled) {
flags = fcntl(sockfd, F_GETFL, 0);
if (!(flags & O_NONBLOCK))
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
}
ret = orig_connect(sockfd, addr, addrlen);
if (!mtask_scheduled || ret != -1 || errno != EINPROGRESS)
return ret;
for(;;) {
if (mtask_yield(sockfd, NGX_WRITE_EVENT)) {
errno = ETIMEDOUT;
return -1;
}
len = sizeof(flags);
flags = 0;
ret = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &flags, &len);
if (ret == -1 || !len)
return -1;
if (!flags)
return 0;
if (flags != EINPROGRESS) {
errno = flags;
return -1;
}
}
}
typedef ssize_t (*read_pt)(int fd, void *buf, size_t count);
static read_pt orig_read;
ssize_t read(int fd, void *buf, size_t count) {
ssize_t ret;
for(;;) {
ret = orig_read(fd, buf, count);
if (!mtask_scheduled || ret != -1 || errno != EAGAIN)
return ret;
if (mtask_yield(fd, NGX_READ_EVENT)) {
errno = ECONNREFUSED;
return -1;
}
}
}
typedef ssize_t (*write_pt)(int fd, const void *buf, size_t count);
static write_pt orig_write;
ssize_t write(int fd, const void *buf, size_t count) {
ssize_t ret;
for(;;) {
ret = orig_write(fd, buf, count);
if (!mtask_scheduled || ret != -1 || errno != EAGAIN)
return ret;
if (mtask_yield(fd, NGX_WRITE_EVENT)) {
errno = ECONNRESET;
return -1;
}
}
}
typedef ssize_t (*recv_pt)(int sockfd, void *buf, size_t len, int flags);
static recv_pt orig_recv;
ssize_t recv(int sockfd, void *buf, size_t len, int flags) {
ssize_t ret;
for(;;) {
ret = orig_recv(sockfd, buf, len, flags);
if (!mtask_scheduled || ret != -1 || errno != EAGAIN)
return ret;
if (mtask_yield(sockfd, NGX_READ_EVENT)) {
errno = ECONNRESET;
return -1;
}
}
}
typedef ssize_t (*send_pt)(int sockfd, const void *buf, size_t len, int flags);
static send_pt orig_send;
ssize_t send(int sockfd, const void *buf, size_t len, int flags) {
ssize_t ret;
for(;;) {
ret = orig_send(sockfd, buf, len, flags);
if (!mtask_scheduled || ret != -1 || errno != EAGAIN)
return ret;
if (mtask_yield(sockfd, NGX_WRITE_EVENT)) {
errno = ECONNREFUSED;
return -1;
}
}
}
typedef int (*poll_pt)(struct pollfd *fds, nfds_t nfds, int timeout);
static poll_pt orig_poll;
int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
return mtask_scheduled && timeout
? (int)nfds /* always ready! */
: orig_poll(fds, nfds, timeout);
}
/* TODO: check for fcntl() removing O_NONBLOCK flag */
__attribute__((constructor)) static void __init_scheduler() {
#define INIT_SYSCALL(name) orig_##name = (name##_pt)dlsym(RTLD_NEXT, #name)
INIT_SYSCALL(accept);
INIT_SYSCALL(connect);
INIT_SYSCALL(read);
INIT_SYSCALL(write);
INIT_SYSCALL(recv);
INIT_SYSCALL(send);
INIT_SYSCALL(poll);
#undef INIT_SYSCALL
}
static void* ngx_http_mtask_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_mtask_loc_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mtask_loc_conf_t));
conf->stack_size = NGX_CONF_UNSET_SIZE;
conf->timeout = NGX_CONF_UNSET_MSEC;
return conf;
}
static char* ngx_http_mtask_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_mtask_loc_conf_t *prev = parent;
ngx_http_mtask_loc_conf_t *conf = child;
ngx_conf_merge_size_value(conf->stack_size,
prev->stack_size,
(size_t) MTASK_DEFAULT_STACK_SIZE);
ngx_conf_merge_msec_value(conf->timeout,
prev->timeout,
MTASK_DEFAULT_TIMEOUT);
return NGX_CONF_OK;
}
static ngx_int_t ngx_http_mtask_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
if (h == NULL)
return NGX_ERROR;
*h = ngx_http_mtask_handler;
return NGX_OK;
}