-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdynamic_tls_graphite_conflictv2.patch
338 lines (297 loc) · 11.3 KB
/
dynamic_tls_graphite_conflictv2.patch
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
Description: This patch applies https://raw.githubusercontent.com/leonklingele/sslconfig/nginx-1.15.4-dynamic-tls-records/patches/nginx__1.15.4_dynamic_tls_records.patch and https://github.com/mailru/graphite-nginx-module/raw/master/graphite_module_v1_7_7.patch at the same time
Author: hda_launchpad (hda_launchpad) <admin@hda.me>
--- nginx-1.15.8-1.orig/src/event/ngx_event_openssl.c
+++ nginx-1.15.8-1/src/event/ngx_event_openssl.c
@@ -1272,6 +1272,7 @@ ngx_ssl_create_connection(ngx_ssl_t *ssl
sc->buffer = ((flags & NGX_SSL_BUFFER) != 0);
sc->buffer_size = ssl->buffer_size;
+ sc->dyn_rec = ssl->dyn_rec;
sc->session_ctx = ssl->ctx;
@@ -1368,6 +1369,17 @@ ngx_ssl_handshake(ngx_connection_t *c)
ngx_ssl_clear_error(c->log);
+#if (NGX_GRAPHITE)
+ struct timeval tp;
+ if (c->ssl->handshake_process == 0) {
+ c->ssl->handshake_process = 1;
+ ngx_gettimeofday(&tp);
+ c->ssl->handshake_start_sec = tp.tv_sec;
+ c->ssl->handshake_start_msec = tp.tv_usec / 1000;
+ }
+#endif
+
+
n = SSL_do_handshake(c->ssl->connection);
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n);
@@ -1406,6 +1418,12 @@ ngx_ssl_handshake(ngx_connection_t *c)
#endif
#endif
+#if (NGX_GRAPHITE)
+ c->ssl->handshake_process = 0;
+ ngx_gettimeofday(&tp);
+ c->ssl->handshake_end_sec = tp.tv_sec;
+ c->ssl->handshake_end_msec = tp.tv_usec / 1000;
+#endif
return NGX_OK;
}
@@ -2124,6 +2142,41 @@ ngx_ssl_send_chain(ngx_connection_t *c,
for ( ;; ) {
+ /* Dynamic record resizing:
+ We want the initial records to fit into one TCP segment
+ so we don't get TCP HoL blocking due to TCP Slow Start.
+ A connection always starts with small records, but after
+ a given amount of records sent, we make the records larger
+ to reduce header overhead.
+ After a connection has idled for a given timeout, begin
+ the process from the start. The actual parameters are
+ configurable. If dyn_rec_timeout is 0, we assume dyn_rec is off. */
+
+ if (c->ssl->dyn_rec.timeout > 0 ) {
+
+ if (ngx_current_msec - c->ssl->dyn_rec_last_write >
+ c->ssl->dyn_rec.timeout)
+ {
+ buf->end = buf->start + c->ssl->dyn_rec.size_lo;
+ c->ssl->dyn_rec_records_sent = 0;
+
+ } else {
+ if (c->ssl->dyn_rec_records_sent >
+ c->ssl->dyn_rec.threshold * 2)
+ {
+ buf->end = buf->start + c->ssl->buffer_size;
+
+ } else if (c->ssl->dyn_rec_records_sent >
+ c->ssl->dyn_rec.threshold)
+ {
+ buf->end = buf->start + c->ssl->dyn_rec.size_hi;
+
+ } else {
+ buf->end = buf->start + c->ssl->dyn_rec.size_lo;
+ }
+ }
+ }
+
while (in && buf->last < buf->end && send < limit) {
if (in->buf->last_buf || in->buf->flush) {
flush = 1;
@@ -2231,6 +2284,9 @@ ngx_ssl_write(ngx_connection_t *c, u_cha
if (n > 0) {
+ c->ssl->dyn_rec_records_sent++;
+ c->ssl->dyn_rec_last_write = ngx_current_msec;
+
if (c->ssl->saved_read_handler) {
c->read->handler = c->ssl->saved_read_handler;
--- nginx-1.15.8-1.orig/src/event/ngx_event_openssl.h
+++ nginx-1.15.8-1/src/event/ngx_event_openssl.h
@@ -64,10 +64,19 @@
#endif
+typedef struct {
+ ngx_msec_t timeout;
+ ngx_uint_t threshold;
+ size_t size_lo;
+ size_t size_hi;
+} ngx_ssl_dyn_rec_t;
+
+
struct ngx_ssl_s {
SSL_CTX *ctx;
ngx_log_t *log;
size_t buffer_size;
+ ngx_ssl_dyn_rec_t dyn_rec;
};
@@ -92,6 +101,15 @@ struct ngx_ssl_connection_s {
unsigned handshaked:1;
unsigned renegotiation:1;
unsigned buffer:1;
+
+#if (NGX_GRAPHITE)
+ ngx_uint_t handshake_process;
+ time_t handshake_start_sec;
+ ngx_msec_t handshake_start_msec;
+ time_t handshake_end_sec;
+ ngx_msec_t handshake_end_msec;
+#endif
+
unsigned no_wait_shutdown:1;
unsigned no_send_shutdown:1;
unsigned handshake_buffer_set:1;
@@ -99,6 +117,10 @@ struct ngx_ssl_connection_s {
unsigned in_early:1;
unsigned early_preread:1;
unsigned write_blocked:1;
+
+ ngx_ssl_dyn_rec_t dyn_rec;
+ ngx_msec_t dyn_rec_last_write;
+ ngx_uint_t dyn_rec_records_sent;
};
@@ -108,7 +130,7 @@ struct ngx_ssl_connection_s {
#define NGX_SSL_DFLT_BUILTIN_SCACHE -5
-#define NGX_SSL_MAX_SESSION_SIZE 4096
+#define NGX_SSL_MAX_SESSION_SIZE 16384
typedef struct ngx_ssl_sess_id_s ngx_ssl_sess_id_t;
--- nginx-1.15.8-1.orig/src/http/modules/ngx_http_gzip_filter_module.c
+++ nginx-1.15.8-1/src/http/modules/ngx_http_gzip_filter_module.c
@@ -426,8 +426,16 @@ ngx_http_gzip_body_filter(ngx_http_reque
goto failed;
}
-
+#if (NGX_GRAPHITE)
+ struct timeval start_tp;
+ ngx_gettimeofday(&start_tp);
+#endif
rc = ngx_http_gzip_filter_deflate(r, ctx);
+#if (NGX_GRAPHITE)
+ struct timeval stop_tp;
+ ngx_gettimeofday(&stop_tp);
+ r->gzip_time += (stop_tp.tv_sec - start_tp.tv_sec) * 1000 + (stop_tp.tv_usec - start_tp.tv_usec) / 1000.0;
+#endif
if (rc == NGX_OK) {
break;
--- nginx-1.15.8-1.orig/src/http/modules/ngx_http_ssl_module.c
+++ nginx-1.15.8-1/src/http/modules/ngx_http_ssl_module.c
@@ -246,6 +246,41 @@ static ngx_command_t ngx_http_ssl_comma
offsetof(ngx_http_ssl_srv_conf_t, early_data),
NULL },
+ { ngx_string("ssl_dyn_rec_enable"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
+ ngx_conf_set_flag_slot,
+ NGX_HTTP_SRV_CONF_OFFSET,
+ offsetof(ngx_http_ssl_srv_conf_t, dyn_rec_enable),
+ NULL },
+
+ { ngx_string("ssl_dyn_rec_timeout"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
+ ngx_conf_set_msec_slot,
+ NGX_HTTP_SRV_CONF_OFFSET,
+ offsetof(ngx_http_ssl_srv_conf_t, dyn_rec_timeout),
+ NULL },
+
+ { ngx_string("ssl_dyn_rec_size_lo"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
+ ngx_conf_set_size_slot,
+ NGX_HTTP_SRV_CONF_OFFSET,
+ offsetof(ngx_http_ssl_srv_conf_t, dyn_rec_size_lo),
+ NULL },
+
+ { ngx_string("ssl_dyn_rec_size_hi"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
+ ngx_conf_set_size_slot,
+ NGX_HTTP_SRV_CONF_OFFSET,
+ offsetof(ngx_http_ssl_srv_conf_t, dyn_rec_size_hi),
+ NULL },
+
+ { ngx_string("ssl_dyn_rec_threshold"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_SRV_CONF_OFFSET,
+ offsetof(ngx_http_ssl_srv_conf_t, dyn_rec_threshold),
+ NULL },
+
ngx_null_command
};
@@ -576,6 +611,11 @@ ngx_http_ssl_create_srv_conf(ngx_conf_t
sscf->session_ticket_keys = NGX_CONF_UNSET_PTR;
sscf->stapling = NGX_CONF_UNSET;
sscf->stapling_verify = NGX_CONF_UNSET;
+ sscf->dyn_rec_enable = NGX_CONF_UNSET;
+ sscf->dyn_rec_timeout = NGX_CONF_UNSET_MSEC;
+ sscf->dyn_rec_size_lo = NGX_CONF_UNSET_SIZE;
+ sscf->dyn_rec_size_hi = NGX_CONF_UNSET_SIZE;
+ sscf->dyn_rec_threshold = NGX_CONF_UNSET_UINT;
return sscf;
}
@@ -643,6 +683,20 @@ ngx_http_ssl_merge_srv_conf(ngx_conf_t *
ngx_conf_merge_str_value(conf->stapling_responder,
prev->stapling_responder, "");
+ ngx_conf_merge_value(conf->dyn_rec_enable, prev->dyn_rec_enable, 0);
+ ngx_conf_merge_msec_value(conf->dyn_rec_timeout, prev->dyn_rec_timeout,
+ 1000);
+ /* Default sizes for the dynamic record sizes are defined to fit maximal
+ TLS + IPv6 overhead in a single TCP segment for lo and 3 segments for hi:
+ 1369 = 1500 - 40 (IP) - 20 (TCP) - 10 (Time) - 61 (Max TLS overhead) */
+ ngx_conf_merge_size_value(conf->dyn_rec_size_lo, prev->dyn_rec_size_lo,
+ 1369);
+ /* 4229 = (1500 - 40 - 20 - 10) * 3 - 61 */
+ ngx_conf_merge_size_value(conf->dyn_rec_size_hi, prev->dyn_rec_size_hi,
+ 4229);
+ ngx_conf_merge_uint_value(conf->dyn_rec_threshold, prev->dyn_rec_threshold,
+ 40);
+
conf->ssl.log = cf->log;
if (conf->enable) {
@@ -827,6 +881,28 @@ ngx_http_ssl_merge_srv_conf(ngx_conf_t *
return NGX_CONF_ERROR;
}
+ if (conf->dyn_rec_enable) {
+ conf->ssl.dyn_rec.timeout = conf->dyn_rec_timeout;
+ conf->ssl.dyn_rec.threshold = conf->dyn_rec_threshold;
+
+ if (conf->buffer_size > conf->dyn_rec_size_lo) {
+ conf->ssl.dyn_rec.size_lo = conf->dyn_rec_size_lo;
+
+ } else {
+ conf->ssl.dyn_rec.size_lo = conf->buffer_size;
+ }
+
+ if (conf->buffer_size > conf->dyn_rec_size_hi) {
+ conf->ssl.dyn_rec.size_hi = conf->dyn_rec_size_hi;
+
+ } else {
+ conf->ssl.dyn_rec.size_hi = conf->buffer_size;
+ }
+
+ } else {
+ conf->ssl.dyn_rec.timeout = 0;
+ }
+
return NGX_CONF_OK;
}
--- nginx-1.15.8-1.orig/src/http/modules/ngx_http_ssl_module.h
+++ nginx-1.15.8-1/src/http/modules/ngx_http_ssl_module.h
@@ -58,6 +58,12 @@ typedef struct {
u_char *file;
ngx_uint_t line;
+
+ ngx_flag_t dyn_rec_enable;
+ ngx_msec_t dyn_rec_timeout;
+ size_t dyn_rec_size_lo;
+ size_t dyn_rec_size_hi;
+ ngx_uint_t dyn_rec_threshold;
} ngx_http_ssl_srv_conf_t;
--- nginx-1.15.8-1.orig/src/http/ngx_http_core_module.c
+++ nginx-1.15.8-1/src/http/ngx_http_core_module.c
@@ -1166,7 +1166,17 @@ ngx_http_core_content_phase(ngx_http_req
if (r->content_handler) {
r->write_event_handler = ngx_http_request_empty_handler;
- ngx_http_finalize_request(r, r->content_handler(r));
+#if (NGX_GRAPHITE)
+ struct timeval start_tp;
+ ngx_gettimeofday(&start_tp);
+#endif
+ ngx_int_t rc = r->content_handler(r);
+#if (NGX_GRAPHITE)
+ struct timeval stop_tp;
+ ngx_gettimeofday(&stop_tp);
+ r->content_time += (stop_tp.tv_sec - start_tp.tv_sec) * 1000 + (stop_tp.tv_usec - start_tp.tv_usec) / 1000.0;
+#endif
+ ngx_http_finalize_request(r, rc);
return NGX_OK;
}
--- nginx-1.15.8-1.orig/src/http/ngx_http_request.h
+++ nginx-1.15.8-1/src/http/ngx_http_request.h
@@ -590,6 +590,19 @@ struct ngx_http_request_s {
unsigned http_minor:16;
unsigned http_major:16;
+#define NGX_GRAPHITE_PATCH
+
+#if (NGX_GRAPHITE)
+ double content_time;
+#endif
+
+#if ((NGX_HTTP_GZIP) && (NGX_GRAPHITE))
+ double gzip_time;
+#endif
+
+#if (NGX_GRAPHITE)
+ double lua_time;
+#endif
};