forked from haproxy/haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux_fcgi.c
4303 lines (3716 loc) · 142 KB
/
mux_fcgi.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FastCGI mux-demux for connections
*
* Copyright (C) 2019 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
*/
#include <import/ist.h>
#include <import/eb32tree.h>
#include <import/ebmbtree.h>
#include <haproxy/api.h>
#include <haproxy/cfgparse.h>
#include <haproxy/connection.h>
#include <haproxy/dynbuf.h>
#include <haproxy/errors.h>
#include <haproxy/fcgi-app.h>
#include <haproxy/fcgi.h>
#include <haproxy/h1.h>
#include <haproxy/h1_htx.h>
#include <haproxy/http_htx.h>
#include <haproxy/htx.h>
#include <haproxy/list.h>
#include <haproxy/log.h>
#include <haproxy/mux_fcgi-t.h>
#include <haproxy/net_helper.h>
#include <haproxy/proxy.h>
#include <haproxy/regex.h>
#include <haproxy/sc_strm.h>
#include <haproxy/server.h>
#include <haproxy/session-t.h>
#include <haproxy/stconn.h>
#include <haproxy/stream.h>
#include <haproxy/trace.h>
#include <haproxy/version.h>
/* 32 buffers: one for the ring's root, rest for the mbuf itself */
#define FCGI_C_MBUF_CNT 32
/* Size for a record header (also size of empty record) */
#define FCGI_RECORD_HEADER_SZ 8
/* FCGI connection descriptor */
struct fcgi_conn {
struct connection *conn;
enum fcgi_conn_st state; /* FCGI connection state */
int16_t max_id; /* highest ID known on this connection, <0 before mgmt records */
uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
uint32_t flags; /* Connection flags: FCGI_CF_* */
int16_t dsi; /* dmux stream ID (<0 = idle ) */
uint16_t drl; /* demux record length (if dsi >= 0) */
uint8_t drt; /* demux record type (if dsi >= 0) */
uint8_t drp; /* demux record padding (if dsi >= 0) */
struct buffer dbuf; /* demux buffer */
struct buffer mbuf[FCGI_C_MBUF_CNT]; /* mux buffers (ring) */
int timeout; /* idle timeout duration in ticks */
int shut_timeout; /* idle timeout duration in ticks after shutdown */
unsigned int nb_streams; /* number of streams in the tree */
unsigned int nb_sc; /* number of attached stream connectors */
unsigned int nb_reserved; /* number of reserved streams */
unsigned int stream_cnt; /* total number of streams seen */
struct proxy *proxy; /* the proxy this connection was created for */
struct fcgi_app *app; /* FCGI application used by this mux */
struct task *task; /* timeout management task */
struct eb_root streams_by_id; /* all active streams by their ID */
struct list send_list; /* list of blocked streams requesting to send */
struct buffer_wait buf_wait; /* Wait list for buffer allocation */
struct wait_event wait_event; /* To be used if we're waiting for I/Os */
};
/* FCGI stream descriptor */
struct fcgi_strm {
struct sedesc *sd;
struct session *sess;
struct fcgi_conn *fconn;
int32_t id; /* stream ID */
uint32_t flags; /* Connection flags: FCGI_SF_* */
enum fcgi_strm_st state; /* FCGI stream state */
int proto_status; /* FCGI_PS_* */
struct h1m h1m; /* response parser state for H1 */
struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
struct eb32_node by_id; /* place in fcgi_conn's streams_by_id */
struct wait_event *subs; /* Address of the wait_event the stream connector associated is waiting on */
struct list send_list; /* To be used when adding in fcgi_conn->send_list */
struct tasklet *shut_tl; /* deferred shutdown tasklet, to retry to close after we failed to by lack of space */
};
/* Flags representing all default FCGI parameters */
#define FCGI_SP_CGI_GATEWAY 0x00000001
#define FCGI_SP_DOC_ROOT 0x00000002
#define FCGI_SP_SCRIPT_NAME 0x00000004
#define FCGI_SP_PATH_INFO 0x00000008
#define FCGI_SP_REQ_URI 0x00000010
#define FCGI_SP_REQ_METH 0x00000020
#define FCGI_SP_REQ_QS 0x00000040
#define FCGI_SP_SRV_PORT 0x00000080
#define FCGI_SP_SRV_PROTO 0x00000100
#define FCGI_SP_SRV_NAME 0x00000200
#define FCGI_SP_REM_ADDR 0x00000400
#define FCGI_SP_REM_PORT 0x00000800
#define FCGI_SP_SCRIPT_FILE 0x00001000
#define FCGI_SP_PATH_TRANS 0x00002000
#define FCGI_SP_CONT_LEN 0x00004000
#define FCGI_SP_HTTPS 0x00008000
#define FCGI_SP_SRV_SOFT 0x00010000
#define FCGI_SP_MASK 0x0001FFFF
#define FCGI_SP_URI_MASK (FCGI_SP_SCRIPT_NAME|FCGI_SP_PATH_INFO|FCGI_SP_REQ_QS)
/* FCGI parameters used when PARAMS record is sent */
struct fcgi_strm_params {
uint32_t mask;
struct ist docroot;
struct ist scriptname;
struct ist pathinfo;
struct ist meth;
struct ist uri;
struct ist vsn;
struct ist qs;
struct ist srv_name;
struct ist srv_port;
struct ist rem_addr;
struct ist rem_port;
struct ist cont_len;
struct ist srv_soft;
int https;
struct buffer *p;
};
/* Maximum amount of data we're OK with re-aligning for buffer optimizations */
#define MAX_DATA_REALIGN 1024
/* trace source and events */
static void fcgi_trace(enum trace_level level, uint64_t mask,
const struct trace_source *src,
const struct ist where, const struct ist func,
const void *a1, const void *a2, const void *a3, const void *a4);
/* The event representation is split like this :
* fconn - internal FCGI connection
* fstrm - internal FCGI stream
* strm - application layer
* rx - data receipt
* tx - data transmission
* rsp - response parsing
*/
static const struct trace_event fcgi_trace_events[] = {
#define FCGI_EV_FCONN_NEW (1ULL << 0)
{ .mask = FCGI_EV_FCONN_NEW, .name = "fconn_new", .desc = "new FCGI connection" },
#define FCGI_EV_FCONN_RECV (1ULL << 1)
{ .mask = FCGI_EV_FCONN_RECV, .name = "fconn_recv", .desc = "Rx on FCGI connection" },
#define FCGI_EV_FCONN_SEND (1ULL << 2)
{ .mask = FCGI_EV_FCONN_SEND, .name = "fconn_send", .desc = "Tx on FCGI connection" },
#define FCGI_EV_FCONN_BLK (1ULL << 3)
{ .mask = FCGI_EV_FCONN_BLK, .name = "fconn_blk", .desc = "FCGI connection blocked" },
#define FCGI_EV_FCONN_WAKE (1ULL << 4)
{ .mask = FCGI_EV_FCONN_WAKE, .name = "fconn_wake", .desc = "FCGI connection woken up" },
#define FCGI_EV_FCONN_END (1ULL << 5)
{ .mask = FCGI_EV_FCONN_END, .name = "fconn_end", .desc = "FCGI connection terminated" },
#define FCGI_EV_FCONN_ERR (1ULL << 6)
{ .mask = FCGI_EV_FCONN_ERR, .name = "fconn_err", .desc = "error on FCGI connection" },
#define FCGI_EV_RX_FHDR (1ULL << 7)
{ .mask = FCGI_EV_RX_FHDR, .name = "rx_fhdr", .desc = "FCGI record header received" },
#define FCGI_EV_RX_RECORD (1ULL << 8)
{ .mask = FCGI_EV_RX_RECORD, .name = "rx_record", .desc = "receipt of any FCGI record" },
#define FCGI_EV_RX_EOI (1ULL << 9)
{ .mask = FCGI_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of FCGI input" },
#define FCGI_EV_RX_GETVAL (1ULL << 10)
{ .mask = FCGI_EV_RX_GETVAL, .name = "rx_get_values", .desc = "receipt of FCGI GET_VALUES_RESULT record" },
#define FCGI_EV_RX_STDOUT (1ULL << 11)
{ .mask = FCGI_EV_RX_STDOUT, .name = "rx_stdout", .desc = "receipt of FCGI STDOUT record" },
#define FCGI_EV_RX_STDERR (1ULL << 12)
{ .mask = FCGI_EV_RX_STDERR, .name = "rx_stderr", .desc = "receipt of FCGI STDERR record" },
#define FCGI_EV_RX_ENDREQ (1ULL << 13)
{ .mask = FCGI_EV_RX_ENDREQ, .name = "rx_end_req", .desc = "receipt of FCGI END_REQUEST record" },
#define FCGI_EV_TX_RECORD (1ULL << 14)
{ .mask = FCGI_EV_TX_RECORD, .name = "tx_record", .desc = "transmission of any FCGI record" },
#define FCGI_EV_TX_EOI (1ULL << 15)
{ .mask = FCGI_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of FCGI end of input" },
#define FCGI_EV_TX_BEGREQ (1ULL << 16)
{ .mask = FCGI_EV_TX_BEGREQ, .name = "tx_begin_request", .desc = "transmission of FCGI BEGIN_REQUEST record" },
#define FCGI_EV_TX_GETVAL (1ULL << 17)
{ .mask = FCGI_EV_TX_GETVAL, .name = "tx_get_values", .desc = "transmission of FCGI GET_VALUES record" },
#define FCGI_EV_TX_PARAMS (1ULL << 18)
{ .mask = FCGI_EV_TX_PARAMS, .name = "tx_params", .desc = "transmission of FCGI PARAMS record" },
#define FCGI_EV_TX_STDIN (1ULL << 19)
{ .mask = FCGI_EV_TX_STDIN, .name = "tx_stding", .desc = "transmission of FCGI STDIN record" },
#define FCGI_EV_TX_ABORT (1ULL << 20)
{ .mask = FCGI_EV_TX_ABORT, .name = "tx_abort", .desc = "transmission of FCGI ABORT record" },
#define FCGI_EV_RSP_DATA (1ULL << 21)
{ .mask = FCGI_EV_RSP_DATA, .name = "rsp_data", .desc = "parse any data of H1 response" },
#define FCGI_EV_RSP_EOM (1ULL << 22)
{ .mask = FCGI_EV_RSP_EOM, .name = "rsp_eom", .desc = "reach the end of message of H1 response" },
#define FCGI_EV_RSP_HDRS (1ULL << 23)
{ .mask = FCGI_EV_RSP_HDRS, .name = "rsp_headers", .desc = "parse headers of H1 response" },
#define FCGI_EV_RSP_BODY (1ULL << 24)
{ .mask = FCGI_EV_RSP_BODY, .name = "rsp_body", .desc = "parse body part of H1 response" },
#define FCGI_EV_RSP_TLRS (1ULL << 25)
{ .mask = FCGI_EV_RSP_TLRS, .name = "rsp_trailerus", .desc = "parse trailers of H1 response" },
#define FCGI_EV_FSTRM_NEW (1ULL << 26)
{ .mask = FCGI_EV_FSTRM_NEW, .name = "fstrm_new", .desc = "new FCGI stream" },
#define FCGI_EV_FSTRM_BLK (1ULL << 27)
{ .mask = FCGI_EV_FSTRM_BLK, .name = "fstrm_blk", .desc = "FCGI stream blocked" },
#define FCGI_EV_FSTRM_END (1ULL << 28)
{ .mask = FCGI_EV_FSTRM_END, .name = "fstrm_end", .desc = "FCGI stream terminated" },
#define FCGI_EV_FSTRM_ERR (1ULL << 29)
{ .mask = FCGI_EV_FSTRM_ERR, .name = "fstrm_err", .desc = "error on FCGI stream" },
#define FCGI_EV_STRM_NEW (1ULL << 30)
{ .mask = FCGI_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
#define FCGI_EV_STRM_RECV (1ULL << 31)
{ .mask = FCGI_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
#define FCGI_EV_STRM_SEND (1ULL << 32)
{ .mask = FCGI_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
#define FCGI_EV_STRM_FULL (1ULL << 33)
{ .mask = FCGI_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
#define FCGI_EV_STRM_WAKE (1ULL << 34)
{ .mask = FCGI_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
#define FCGI_EV_STRM_SHUT (1ULL << 35)
{ .mask = FCGI_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
#define FCGI_EV_STRM_END (1ULL << 36)
{ .mask = FCGI_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
#define FCGI_EV_STRM_ERR (1ULL << 37)
{ .mask = FCGI_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
{ }
};
static const struct name_desc fcgi_trace_lockon_args[4] = {
/* arg1 */ { /* already used by the connection */ },
/* arg2 */ { .name="fstrm", .desc="FCGI stream" },
/* arg3 */ { },
/* arg4 */ { }
};
static const struct name_desc fcgi_trace_decoding[] = {
#define FCGI_VERB_CLEAN 1
{ .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
#define FCGI_VERB_MINIMAL 2
{ .name="minimal", .desc="report only fconn/fstrm state and flags, no real decoding" },
#define FCGI_VERB_SIMPLE 3
{ .name="simple", .desc="add request/response status line or htx info when available" },
#define FCGI_VERB_ADVANCED 4
{ .name="advanced", .desc="add header fields or record decoding when available" },
#define FCGI_VERB_COMPLETE 5
{ .name="complete", .desc="add full data dump when available" },
{ /* end */ }
};
static struct trace_source trace_fcgi __read_mostly = {
.name = IST("fcgi"),
.desc = "FastCGI multiplexer",
.arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
.default_cb = fcgi_trace,
.known_events = fcgi_trace_events,
.lockon_args = fcgi_trace_lockon_args,
.decoding = fcgi_trace_decoding,
.report_events = ~0, // report everything by default
};
#define TRACE_SOURCE &trace_fcgi
INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
/* FCGI connection and stream pools */
DECLARE_STATIC_POOL(pool_head_fcgi_conn, "fcgi_conn", sizeof(struct fcgi_conn));
DECLARE_STATIC_POOL(pool_head_fcgi_strm, "fcgi_strm", sizeof(struct fcgi_strm));
struct task *fcgi_timeout_task(struct task *t, void *context, unsigned int state);
static int fcgi_process(struct fcgi_conn *fconn);
/* fcgi_io_cb is exported to see it resolved in "show fd" */
struct task *fcgi_io_cb(struct task *t, void *ctx, unsigned int state);
static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id);
struct task *fcgi_deferred_shut(struct task *t, void *ctx, unsigned int state);
static struct fcgi_strm *fcgi_stconn_new(struct fcgi_conn *fconn, struct stconn *sc, struct session *sess);
static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm);
static void fcgi_strm_notify_send(struct fcgi_strm *fstrm);
static void fcgi_strm_alert(struct fcgi_strm *fstrm);
static int fcgi_strm_send_abort(struct fcgi_conn *fconn, struct fcgi_strm *fstrm);
/* a dummy closed endpoint */
static const struct sedesc closed_ep = {
.sc = NULL,
.flags = SE_FL_DETACHED,
};
/* a dmumy management stream */
static const struct fcgi_strm *fcgi_mgmt_stream = &(const struct fcgi_strm){
.sd = (struct sedesc*)&closed_ep,
.fconn = NULL,
.state = FCGI_SS_CLOSED,
.flags = FCGI_SF_NONE,
.id = 0,
};
/* and a dummy idle stream for use with any unknown stream */
static const struct fcgi_strm *fcgi_unknown_stream = &(const struct fcgi_strm){
.sd = (struct sedesc*)&closed_ep,
.fconn = NULL,
.state = FCGI_SS_IDLE,
.flags = FCGI_SF_NONE,
.id = 0,
};
/* returns the stconn associated to the FCGI stream */
static forceinline struct stconn *fcgi_strm_sc(const struct fcgi_strm *fstrm)
{
return fstrm->sd->sc;
}
/* the FCGI traces always expect that arg1, if non-null, is of type connection
* (from which we can derive fconn), that arg2, if non-null, is of type fstrm,
* and that arg3, if non-null, is a htx for rx/tx headers.
*/
static void fcgi_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
const struct ist where, const struct ist func,
const void *a1, const void *a2, const void *a3, const void *a4)
{
const struct connection *conn = a1;
struct fcgi_conn *fconn = conn ? conn->ctx : NULL;
const struct fcgi_strm *fstrm = a2;
const struct htx *htx = a3;
const size_t *val = a4;
if (!fconn)
fconn = (fstrm ? fstrm->fconn : NULL);
if (!fconn || src->verbosity < FCGI_VERB_CLEAN)
return;
/* Display the response state if fstrm is defined */
if (fstrm)
chunk_appendf(&trace_buf, " [rsp:%s]", h1m_state_str(fstrm->h1m.state));
if (src->verbosity == FCGI_VERB_CLEAN)
return;
/* Display the value to the 4th argument (level > STATE) */
if (src->level > TRACE_LEVEL_STATE && val)
chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val);
/* Display status-line if possible (verbosity > MINIMAL) */
if (src->verbosity > FCGI_VERB_MINIMAL && htx && htx_nbblks(htx)) {
const struct htx_blk *blk = __htx_get_head_blk(htx);
const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
enum htx_blk_type type = htx_get_blk_type(blk);
if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL)
chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"",
HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
}
/* Display fconn info and, if defined, fstrm info */
chunk_appendf(&trace_buf, " - fconn=%p(%s,0x%08x)", fconn, fconn_st_to_str(fconn->state), fconn->flags);
if (fstrm)
chunk_appendf(&trace_buf, " fstrm=%p(%d,%s,0x%08x)", fstrm, fstrm->id, fstrm_st_to_str(fstrm->state), fstrm->flags);
if (!fstrm || fstrm->id <= 0)
chunk_appendf(&trace_buf, " dsi=%d", fconn->dsi);
if (fconn->dsi >= 0 && (mask & FCGI_EV_RX_FHDR))
chunk_appendf(&trace_buf, " drt=%s", fcgi_rt_str(fconn->drt));
if (src->verbosity == FCGI_VERB_MINIMAL)
return;
/* Display mbuf and dbuf info (level > USER & verbosity > SIMPLE) */
if (src->level > TRACE_LEVEL_USER) {
if (src->verbosity == FCGI_VERB_COMPLETE ||
(src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_RECV|FCGI_EV_RX_RECORD))))
chunk_appendf(&trace_buf, " dbuf=%u@%p+%u/%u",
(unsigned int)b_data(&fconn->dbuf), b_orig(&fconn->dbuf),
(unsigned int)b_head_ofs(&fconn->dbuf), (unsigned int)b_size(&fconn->dbuf));
if (src->verbosity == FCGI_VERB_COMPLETE ||
(src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_FCONN_SEND|FCGI_EV_TX_RECORD)))) {
struct buffer *hmbuf = br_head(fconn->mbuf);
struct buffer *tmbuf = br_tail(fconn->mbuf);
chunk_appendf(&trace_buf, " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
br_head_idx(fconn->mbuf), br_tail_idx(fconn->mbuf), br_size(fconn->mbuf),
(unsigned int)b_data(hmbuf), b_orig(hmbuf),
(unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
(unsigned int)b_data(tmbuf), b_orig(tmbuf),
(unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
}
if (fstrm && (src->verbosity == FCGI_VERB_COMPLETE ||
(src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_STRM_RECV|FCGI_EV_RSP_DATA)))))
chunk_appendf(&trace_buf, " rxbuf=%u@%p+%u/%u",
(unsigned int)b_data(&fstrm->rxbuf), b_orig(&fstrm->rxbuf),
(unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf));
}
/* Display htx info if defined (level > USER) */
if (src->level > TRACE_LEVEL_USER && htx) {
int full = 0;
/* Full htx info (level > STATE && verbosity > SIMPLE) */
if (src->level > TRACE_LEVEL_STATE) {
if (src->verbosity == FCGI_VERB_COMPLETE)
full = 1;
else if (src->verbosity == FCGI_VERB_ADVANCED && (mask & (FCGI_EV_RSP_HDRS|FCGI_EV_TX_PARAMS)))
full = 1;
}
chunk_memcat(&trace_buf, "\n\t", 2);
htx_dump(&trace_buf, htx, full);
}
}
/*****************************************************/
/* functions below are for dynamic buffer management */
/*****************************************************/
/* Indicates whether or not the we may call the fcgi_recv() function to attempt
* to receive data into the buffer and/or demux pending data. The condition is
* a bit complex due to some API limits for now. The rules are the following :
* - if an error or a shutdown was detected on the connection and the buffer
* is empty, we must not attempt to receive
* - if the demux buf failed to be allocated, we must not try to receive and
* we know there is nothing pending
* - if no flag indicates a blocking condition, we may attempt to receive,
* regardless of whether the demux buffer is full or not, so that only
* de demux part decides whether or not to block. This is needed because
* the connection API indeed prevents us from re-enabling receipt that is
* already enabled in a polled state, so we must always immediately stop
* as soon as the demux can't proceed so as never to hit an end of read
* with data pending in the buffers.
* - otherwise must may not attempt
*/
static inline int fcgi_recv_allowed(const struct fcgi_conn *fconn)
{
if (fconn->flags & (FCGI_CF_EOS|FCGI_CF_ERROR))
return 0;
if (b_data(&fconn->dbuf) == 0 && fconn->state == FCGI_CS_CLOSED)
return 0;
if (!(fconn->flags & FCGI_CF_DEM_DALLOC) &&
!(fconn->flags & FCGI_CF_DEM_BLOCK_ANY))
return 1;
return 0;
}
/* Restarts reading on the connection if it was not enabled */
static inline void fcgi_conn_restart_reading(const struct fcgi_conn *fconn, int consider_buffer)
{
if (!fcgi_recv_allowed(fconn))
return;
if ((!consider_buffer || !b_data(&fconn->dbuf)) &&
(fconn->wait_event.events & SUB_RETRY_RECV))
return;
tasklet_wakeup(fconn->wait_event.tasklet);
}
/* Tries to grab a buffer and to re-enable processing on mux <target>. The
* fcgi_conn flags are used to figure what buffer was requested. It returns 1 if
* the allocation succeeds, in which case the connection is woken up, or 0 if
* it's impossible to wake up and we prefer to be woken up later.
*/
static int fcgi_buf_available(void *target)
{
struct fcgi_conn *fconn = target;
struct fcgi_strm *fstrm;
if ((fconn->flags & FCGI_CF_DEM_DALLOC) && b_alloc(&fconn->dbuf, DB_MUX_RX)) {
TRACE_STATE("unblocking fconn, dbuf allocated", FCGI_EV_FCONN_RECV|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
fconn->flags &= ~FCGI_CF_DEM_DALLOC;
fcgi_conn_restart_reading(fconn, 1);
return 1;
}
if ((fconn->flags & FCGI_CF_MUX_MALLOC) && b_alloc(br_tail(fconn->mbuf), DB_MUX_TX)) {
TRACE_STATE("unblocking fconn, mbuf allocated", FCGI_EV_FCONN_SEND|FCGI_EV_FCONN_BLK|FCGI_EV_FCONN_WAKE, fconn->conn);
fconn->flags &= ~FCGI_CF_MUX_MALLOC;
if (fconn->flags & FCGI_CF_DEM_MROOM) {
fconn->flags &= ~FCGI_CF_DEM_MROOM;
fcgi_conn_restart_reading(fconn, 1);
}
return 1;
}
if ((fconn->flags & FCGI_CF_DEM_SALLOC) &&
(fstrm = fcgi_conn_st_by_id(fconn, fconn->dsi)) && fcgi_strm_sc(fstrm) &&
b_alloc(&fstrm->rxbuf, DB_SE_RX)) {
TRACE_STATE("unblocking fstrm, rxbuf allocated", FCGI_EV_STRM_RECV|FCGI_EV_FSTRM_BLK|FCGI_EV_STRM_WAKE, fconn->conn, fstrm);
fconn->flags &= ~FCGI_CF_DEM_SALLOC;
fcgi_conn_restart_reading(fconn, 1);
fcgi_strm_notify_recv(fstrm);
return 1;
}
return 0;
}
static inline struct buffer *fcgi_get_buf(struct fcgi_conn *fconn, struct buffer *bptr)
{
struct buffer *buf = NULL;
if (likely(!LIST_INLIST(&fconn->buf_wait.list)) &&
unlikely((buf = b_alloc(bptr, DB_MUX_RX)) == NULL)) {
b_queue(DB_MUX_RX, &fconn->buf_wait, fconn, fcgi_buf_available);
}
return buf;
}
static inline void fcgi_release_buf(struct fcgi_conn *fconn, struct buffer *bptr)
{
if (bptr->size) {
b_free(bptr);
offer_buffers(NULL, 1);
}
}
static inline void fcgi_release_mbuf(struct fcgi_conn *fconn)
{
struct buffer *buf;
unsigned int count = 0;
while (b_size(buf = br_head_pick(fconn->mbuf))) {
b_free(buf);
count++;
}
if (count)
offer_buffers(NULL, count);
}
/* Returns the number of allocatable outgoing streams for the connection taking
* the number reserved streams into account.
*/
static inline int fcgi_streams_left(const struct fcgi_conn *fconn)
{
int ret;
ret = (unsigned int)(0x7FFF - fconn->max_id) - fconn->nb_reserved - 1;
if (ret < 0)
ret = 0;
return ret;
}
/* Returns the number of streams in use on a connection to figure if it's
* idle or not. We check nb_sc and not nb_streams as the caller will want
* to know if it was the last one after a detach().
*/
static int fcgi_used_streams(struct connection *conn)
{
struct fcgi_conn *fconn = conn->ctx;
return fconn->nb_sc;
}
/* Returns the number of concurrent streams available on the connection */
static int fcgi_avail_streams(struct connection *conn)
{
struct server *srv = objt_server(conn->target);
struct fcgi_conn *fconn = conn->ctx;
int ret1, ret2;
/* Don't open new stream if the connection is closed */
if (fconn->state == FCGI_CS_CLOSED)
return 0;
/* May be negative if this setting has changed */
ret1 = (fconn->streams_limit - fconn->nb_streams);
/* we must also consider the limit imposed by stream IDs */
ret2 = fcgi_streams_left(fconn);
ret1 = MIN(ret1, ret2);
if (ret1 > 0 && srv && srv->max_reuse >= 0) {
ret2 = ((fconn->stream_cnt <= srv->max_reuse) ? srv->max_reuse - fconn->stream_cnt + 1: 0);
ret1 = MIN(ret1, ret2);
}
return ret1;
}
/*****************************************************************/
/* functions below are dedicated to the mux setup and management */
/*****************************************************************/
/* Initializes the mux once it's attached. Only outgoing connections are
* supported. So the context is already initialized before installing the
* mux. <input> is always used as Input buffer and may contain data. It is the
* caller responsibility to not reuse it anymore. Returns < 0 on error.
*/
static int fcgi_init(struct connection *conn, struct proxy *px, struct session *sess,
struct buffer *input)
{
struct fcgi_conn *fconn;
struct fcgi_strm *fstrm;
struct fcgi_app *app = get_px_fcgi_app(px);
struct task *t = NULL;
void *conn_ctx = conn->ctx;
TRACE_ENTER(FCGI_EV_FSTRM_NEW);
if (!app) {
TRACE_ERROR("No FCGI app found, don't create fconn", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
goto fail_conn;
}
fconn = pool_alloc(pool_head_fcgi_conn);
if (!fconn) {
TRACE_ERROR("fconn allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
goto fail_conn;
}
fconn->shut_timeout = fconn->timeout = px->timeout.server;
if (tick_isset(px->timeout.serverfin))
fconn->shut_timeout = px->timeout.serverfin;
fconn->flags = FCGI_CF_NONE;
/* Retrieve useful info from the FCGI app */
if (app->flags & FCGI_APP_FL_KEEP_CONN)
fconn->flags |= FCGI_CF_KEEP_CONN;
if (app->flags & FCGI_APP_FL_GET_VALUES)
fconn->flags |= FCGI_CF_GET_VALUES;
if (app->flags & FCGI_APP_FL_MPXS_CONNS)
fconn->flags |= FCGI_CF_MPXS_CONNS;
fconn->proxy = px;
fconn->app = app;
fconn->task = NULL;
if (tick_isset(fconn->timeout)) {
t = task_new_here();
if (!t) {
TRACE_ERROR("fconn task allocation failure", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
goto fail;
}
fconn->task = t;
t->process = fcgi_timeout_task;
t->context = fconn;
t->expire = tick_add(now_ms, fconn->timeout);
}
fconn->wait_event.tasklet = tasklet_new();
if (!fconn->wait_event.tasklet)
goto fail;
fconn->wait_event.tasklet->process = fcgi_io_cb;
fconn->wait_event.tasklet->context = fconn;
fconn->wait_event.events = 0;
/* Initialise the context. */
fconn->state = FCGI_CS_INIT;
fconn->conn = conn;
fconn->streams_limit = app->maxreqs;
fconn->max_id = -1;
fconn->nb_streams = 0;
fconn->nb_sc = 0;
fconn->nb_reserved = 0;
fconn->stream_cnt = 0;
fconn->dbuf = *input;
fconn->dsi = -1;
br_init(fconn->mbuf, sizeof(fconn->mbuf) / sizeof(fconn->mbuf[0]));
fconn->streams_by_id = EB_ROOT;
LIST_INIT(&fconn->send_list);
LIST_INIT(&fconn->buf_wait.list);
conn->ctx = fconn;
if (t)
task_queue(t);
/* FIXME: this is temporary, for outgoing connections we need to
* immediately allocate a stream until the code is modified so that the
* caller calls ->attach(). For now the outgoing sc is stored as
* conn->ctx by the caller and saved in conn_ctx.
*/
fstrm = fcgi_stconn_new(fconn, conn_ctx, sess);
if (!fstrm)
goto fail;
/* Repare to read something */
fcgi_conn_restart_reading(fconn, 1);
TRACE_LEAVE(FCGI_EV_FCONN_NEW, conn);
return 0;
fail:
task_destroy(t);
tasklet_free(fconn->wait_event.tasklet);
pool_free(pool_head_fcgi_conn, fconn);
fail_conn:
conn->ctx = conn_ctx; // restore saved ctx
TRACE_DEVEL("leaving in error", FCGI_EV_FCONN_NEW|FCGI_EV_FCONN_END|FCGI_EV_FCONN_ERR);
return -1;
}
/* Returns the next allocatable outgoing stream ID for the FCGI connection, or
* -1 if no more is allocatable.
*/
static inline int32_t fcgi_conn_get_next_sid(const struct fcgi_conn *fconn)
{
int32_t id = (fconn->max_id + 1) | 1;
if ((id & 0x80000000U))
id = -1;
return id;
}
/* Returns the stream associated with id <id> or NULL if not found */
static inline struct fcgi_strm *fcgi_conn_st_by_id(struct fcgi_conn *fconn, int id)
{
struct eb32_node *node;
if (id == 0)
return (struct fcgi_strm *)fcgi_mgmt_stream;
if (id > fconn->max_id)
return (struct fcgi_strm *)fcgi_unknown_stream;
node = eb32_lookup(&fconn->streams_by_id, id);
if (!node)
return (struct fcgi_strm *)fcgi_unknown_stream;
return container_of(node, struct fcgi_strm, by_id);
}
/* Release function. This one should be called to free all resources allocated
* to the mux.
*/
static void fcgi_release(struct fcgi_conn *fconn)
{
struct connection *conn = fconn->conn;
TRACE_POINT(FCGI_EV_FCONN_END);
b_dequeue(&fconn->buf_wait);
fcgi_release_buf(fconn, &fconn->dbuf);
fcgi_release_mbuf(fconn);
if (fconn->task) {
fconn->task->context = NULL;
task_wakeup(fconn->task, TASK_WOKEN_OTHER);
fconn->task = NULL;
}
tasklet_free(fconn->wait_event.tasklet);
if (conn && fconn->wait_event.events != 0)
conn->xprt->unsubscribe(conn, conn->xprt_ctx, fconn->wait_event.events,
&fconn->wait_event);
pool_free(pool_head_fcgi_conn, fconn);
if (conn) {
conn->mux = NULL;
conn->ctx = NULL;
TRACE_DEVEL("freeing conn", FCGI_EV_FCONN_END, conn);
conn_stop_tracking(conn);
conn_full_close(conn);
if (conn->destroy_cb)
conn->destroy_cb(conn);
conn_free(conn);
}
}
/* Detect a pending read0 for a FCGI connection. It happens if a read0 is
* pending on the connection AND if there is no more data in the demux
* buffer. The function returns 1 to report a read0 or 0 otherwise.
*/
static int fcgi_conn_read0_pending(struct fcgi_conn *fconn)
{
if ((fconn->flags & FCGI_CF_EOS) && !b_data(&fconn->dbuf))
return 1;
return 0;
}
/* Returns true if the FCGI connection must be release */
static inline int fcgi_conn_is_dead(struct fcgi_conn *fconn)
{
if (eb_is_empty(&fconn->streams_by_id) && /* don't close if streams exist */
(!(fconn->flags & FCGI_CF_KEEP_CONN) || /* don't keep the connection alive */
(fconn->flags & FCGI_CF_ERROR) || /* errors close immediately */
(fconn->state == FCGI_CS_CLOSED && !fconn->task) ||/* a timeout stroke earlier */
(!(fconn->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
(!br_data(fconn->mbuf) && /* mux buffer empty, also process clean events below */
(fconn->flags & FCGI_CF_EOS))))
return 1;
return 0;
}
/********************************************************/
/* functions below are for the FCGI protocol processing */
/********************************************************/
/* Marks an error on the stream. */
static inline void fcgi_strm_error(struct fcgi_strm *fstrm)
{
if (fstrm->id && fstrm->state != FCGI_SS_ERROR) {
TRACE_POINT(FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
if (fstrm->state < FCGI_SS_ERROR) {
fstrm->state = FCGI_SS_ERROR;
TRACE_STATE("switching to ERROR", FCGI_EV_FSTRM_ERR, fstrm->fconn->conn, fstrm);
}
se_fl_set_error(fstrm->sd);
}
}
/* Attempts to notify the data layer of recv availability */
static void fcgi_strm_notify_recv(struct fcgi_strm *fstrm)
{
if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_RECV)) {
TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
tasklet_wakeup(fstrm->subs->tasklet);
fstrm->subs->events &= ~SUB_RETRY_RECV;
if (!fstrm->subs->events)
fstrm->subs = NULL;
}
}
/* Attempts to notify the data layer of send availability */
static void fcgi_strm_notify_send(struct fcgi_strm *fstrm)
{
if (fstrm->subs && (fstrm->subs->events & SUB_RETRY_SEND)) {
TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
fstrm->flags |= FCGI_SF_NOTIFIED;
tasklet_wakeup(fstrm->subs->tasklet);
fstrm->subs->events &= ~SUB_RETRY_SEND;
if (!fstrm->subs->events)
fstrm->subs = NULL;
}
else if (fstrm->flags & (FCGI_SF_WANT_SHUTR | FCGI_SF_WANT_SHUTW)) {
TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
tasklet_wakeup(fstrm->shut_tl);
}
}
/* Alerts the data layer, trying to wake it up by all means, following
* this sequence :
* - if the fcgi stream' data layer is subscribed to recv, then it's woken up
* for recv
* - if its subscribed to send, then it's woken up for send
* - if it was subscribed to neither, its ->wake() callback is called
* It is safe to call this function with a closed stream which doesn't have a
* stream connector anymore.
*/
static void fcgi_strm_alert(struct fcgi_strm *fstrm)
{
TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
if (fstrm->subs ||
(fstrm->flags & (FCGI_SF_WANT_SHUTR|FCGI_SF_WANT_SHUTW))) {
fcgi_strm_notify_recv(fstrm);
fcgi_strm_notify_send(fstrm);
}
else if (fcgi_strm_sc(fstrm) && fcgi_strm_sc(fstrm)->app_ops->wake != NULL) {
TRACE_POINT(FCGI_EV_STRM_WAKE, fstrm->fconn->conn, fstrm);
fcgi_strm_sc(fstrm)->app_ops->wake(fcgi_strm_sc(fstrm));
}
}
/* Writes the 16-bit record size <len> at address <record> */
static inline void fcgi_set_record_size(void *record, uint16_t len)
{
uint8_t *out = (record + 4);
*out = (len >> 8);
*(out + 1) = (len & 0xff);
}
/* Writes the 16-bit stream id <id> at address <record> */
static inline void fcgi_set_record_id(void *record, uint16_t id)
{
uint8_t *out = (record + 2);
*out = (id >> 8);
*(out + 1) = (id & 0xff);
}
/* Marks a FCGI stream as CLOSED and decrement the number of active streams for
* its connection if the stream was not yet closed. Please use this exclusively
* before closing a stream to ensure stream count is well maintained.
*/
static inline void fcgi_strm_close(struct fcgi_strm *fstrm)
{
if (fstrm->state != FCGI_SS_CLOSED) {
TRACE_ENTER(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
fstrm->fconn->nb_streams--;
if (!fstrm->id)
fstrm->fconn->nb_reserved--;
if (fcgi_strm_sc(fstrm)) {
if (!se_fl_test(fstrm->sd, SE_FL_EOS) && !b_data(&fstrm->rxbuf))
fcgi_strm_notify_recv(fstrm);
}
fstrm->state = FCGI_SS_CLOSED;
TRACE_STATE("switching to CLOSED", FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
TRACE_LEAVE(FCGI_EV_FSTRM_END, fstrm->fconn->conn, fstrm);
}
}
/* Detaches a FCGI stream from its FCGI connection and releases it to the
* fcgi_strm pool.
*/
static void fcgi_strm_destroy(struct fcgi_strm *fstrm)
{
struct connection *conn = fstrm->fconn->conn;
TRACE_ENTER(FCGI_EV_FSTRM_END, conn, fstrm);
fcgi_strm_close(fstrm);
eb32_delete(&fstrm->by_id);
if (b_size(&fstrm->rxbuf)) {
b_free(&fstrm->rxbuf);
offer_buffers(NULL, 1);
}
if (fstrm->subs)
fstrm->subs->events = 0;
/* There's no need to explicitly call unsubscribe here, the only
* reference left would be in the fconn send_list/fctl_list, and if
* we're in it, we're getting out anyway
*/
LIST_DEL_INIT(&fstrm->send_list);
tasklet_free(fstrm->shut_tl);
BUG_ON(fstrm->sd && !se_fl_test(fstrm->sd, SE_FL_ORPHAN));
sedesc_free(fstrm->sd);
pool_free(pool_head_fcgi_strm, fstrm);
TRACE_LEAVE(FCGI_EV_FSTRM_END, conn);
}
/* Allocates a new stream <id> for connection <fconn> and adds it into fconn's
* stream tree. In case of error, nothing is added and NULL is returned. The
* causes of errors can be any failed memory allocation. The caller is
* responsible for checking if the connection may support an extra stream prior
* to calling this function.
*/
static struct fcgi_strm *fcgi_strm_new(struct fcgi_conn *fconn, int id)
{
struct fcgi_strm *fstrm;
TRACE_ENTER(FCGI_EV_FSTRM_NEW, fconn->conn);
fstrm = pool_alloc(pool_head_fcgi_strm);
if (!fstrm) {
TRACE_ERROR("fstrm allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
goto out;
}
fstrm->shut_tl = tasklet_new();
if (!fstrm->shut_tl) {
TRACE_ERROR("fstrm shut tasklet allocation failure", FCGI_EV_FSTRM_NEW|FCGI_EV_FSTRM_ERR|FCGI_EV_FSTRM_END, fconn->conn);
pool_free(pool_head_fcgi_strm, fstrm);
goto out;
}
fstrm->subs = NULL;
fstrm->shut_tl->process = fcgi_deferred_shut;
fstrm->shut_tl->context = fstrm;
LIST_INIT(&fstrm->send_list);
fstrm->fconn = fconn;
fstrm->sd = NULL;
fstrm->flags = FCGI_SF_NONE;
fstrm->proto_status = 0;
fstrm->state = FCGI_SS_IDLE;
fstrm->rxbuf = BUF_NULL;
h1m_init_res(&fstrm->h1m);
fstrm->h1m.err_pos = -1; // don't care about errors on the request path
fstrm->h1m.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR);
fstrm->by_id.key = fstrm->id = id;
if (id > 0)
fconn->max_id = id;
else
fconn->nb_reserved++;
eb32_insert(&fconn->streams_by_id, &fstrm->by_id);
fconn->nb_streams++;
fconn->stream_cnt++;
TRACE_LEAVE(FCGI_EV_FSTRM_NEW, fconn->conn, fstrm);