comparison src/event/quic/ngx_event_quic_output.c @ 8941:0433e69f5425 quic

QUIC: added path limiting function ngx_quic_path_limit().
author Roman Arutyunyan <arut@nginx.com>
date Tue, 14 Dec 2021 16:24:20 +0300
parents fb41e37ddeb0
children e72db9162180
comparison
equal deleted inserted replaced
8940:fb41e37ddeb0 8941:0433e69f5425
61 static ngx_uint_t ngx_quic_get_padding_level(ngx_connection_t *c); 61 static ngx_uint_t ngx_quic_get_padding_level(ngx_connection_t *c);
62 static ssize_t ngx_quic_send(ngx_connection_t *c, u_char *buf, size_t len, 62 static ssize_t ngx_quic_send(ngx_connection_t *c, u_char *buf, size_t len,
63 struct sockaddr *sockaddr, socklen_t socklen); 63 struct sockaddr *sockaddr, socklen_t socklen);
64 static void ngx_quic_set_packet_number(ngx_quic_header_t *pkt, 64 static void ngx_quic_set_packet_number(ngx_quic_header_t *pkt,
65 ngx_quic_send_ctx_t *ctx); 65 ngx_quic_send_ctx_t *ctx);
66 static size_t ngx_quic_path_limit(ngx_connection_t *c, ngx_quic_path_t *path,
67 size_t size);
66 68
67 69
68 size_t 70 size_t
69 ngx_quic_max_udp_payload(ngx_connection_t *c) 71 ngx_quic_max_udp_payload(ngx_connection_t *c)
70 { 72 {
135 137
136 138
137 static ngx_int_t 139 static ngx_int_t
138 ngx_quic_create_datagrams(ngx_connection_t *c, ngx_quic_socket_t *qsock) 140 ngx_quic_create_datagrams(ngx_connection_t *c, ngx_quic_socket_t *qsock)
139 { 141 {
140 off_t max;
141 size_t len, min; 142 size_t len, min;
142 ssize_t n; 143 ssize_t n;
143 u_char *p; 144 u_char *p;
144 uint64_t preserved_pnum[NGX_QUIC_SEND_CTX_LAST]; 145 uint64_t preserved_pnum[NGX_QUIC_SEND_CTX_LAST];
145 ngx_uint_t i, pad; 146 ngx_uint_t i, pad;
158 p = dst; 159 p = dst;
159 160
160 len = ngx_min(qc->ctp.max_udp_payload_size, 161 len = ngx_min(qc->ctp.max_udp_payload_size,
161 NGX_QUIC_MAX_UDP_PAYLOAD_SIZE); 162 NGX_QUIC_MAX_UDP_PAYLOAD_SIZE);
162 163
163 if (path->limited) { 164 len = ngx_quic_path_limit(c, path, len);
164 max = path->received * 3;
165 max = (path->sent >= max) ? 0 : max - path->sent;
166
167 len = ngx_min(len, (size_t) max);
168 }
169 165
170 pad = ngx_quic_get_padding_level(c); 166 pad = ngx_quic_get_padding_level(c);
171 167
172 for (i = 0; i < NGX_QUIC_SEND_CTX_LAST; i++) { 168 for (i = 0; i < NGX_QUIC_SEND_CTX_LAST; i++) {
173 169
1210 1206
1211 ngx_int_t 1207 ngx_int_t
1212 ngx_quic_frame_sendto(ngx_connection_t *c, ngx_quic_frame_t *frame, 1208 ngx_quic_frame_sendto(ngx_connection_t *c, ngx_quic_frame_t *frame,
1213 size_t min, ngx_quic_path_t *path) 1209 size_t min, ngx_quic_path_t *path)
1214 { 1210 {
1215 off_t max;
1216 size_t min_payload, pad; 1211 size_t min_payload, pad;
1217 ssize_t len, sent; 1212 ssize_t len, sent;
1218 ngx_str_t res; 1213 ngx_str_t res;
1219 ngx_quic_header_t pkt; 1214 ngx_quic_header_t pkt;
1220 ngx_quic_send_ctx_t *ctx; 1215 ngx_quic_send_ctx_t *ctx;
1226 qc = ngx_quic_get_connection(c); 1221 qc = ngx_quic_get_connection(c);
1227 ctx = ngx_quic_get_send_ctx(qc, ssl_encryption_application); 1222 ctx = ngx_quic_get_send_ctx(qc, ssl_encryption_application);
1228 1223
1229 ngx_quic_init_packet(c, ctx, qc->socket, &pkt); 1224 ngx_quic_init_packet(c, ctx, qc->socket, &pkt);
1230 1225
1231 /* account for anti-amplification limit: expand to allowed size */ 1226 min = ngx_quic_path_limit(c, path, min);
1232 if (path->limited) {
1233 max = path->received * 3;
1234 max = (path->sent >= max) ? 0 : max - path->sent;
1235 if ((off_t) min > max) {
1236 min = max;
1237 }
1238 }
1239 1227
1240 min_payload = min ? ngx_quic_payload_size(&pkt, min) : 0; 1228 min_payload = min ? ngx_quic_payload_size(&pkt, min) : 0;
1241 1229
1242 pad = 4 - pkt.num_len; 1230 pad = 4 - pkt.num_len;
1243 min_payload = ngx_max(min_payload, pad); 1231 min_payload = ngx_max(min_payload, pad);
1277 1265
1278 path->sent += sent; 1266 path->sent += sent;
1279 1267
1280 return NGX_OK; 1268 return NGX_OK;
1281 } 1269 }
1270
1271
1272 static size_t
1273 ngx_quic_path_limit(ngx_connection_t *c, ngx_quic_path_t *path, size_t size)
1274 {
1275 off_t max;
1276
1277 if (path->limited) {
1278 max = path->received * 3;
1279 max = (path->sent >= max) ? 0 : max - path->sent;
1280
1281 if ((off_t) size > max) {
1282 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
1283 "quic path limit %uz - %O", size, max);
1284 return max;
1285 }
1286 }
1287
1288 return size;
1289 }