Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
congestion_control_flow.c
Go to the documentation of this file.
1/* Copyright (c) 2019-2021, The Tor Project, Inc. */
2/* See LICENSE for licensing information */
3
4/**
5 * \file congestion_control_flow.c
6 * \brief Code that implements flow control for congestion controlled
7 * circuits.
8 */
9
10#define TOR_CONGESTION_CONTROL_FLOW_PRIVATE
11
12#include "core/or/or.h"
13
14#include "core/or/relay.h"
21#include "core/or/circuitlist.h"
22#include "core/or/trace_probes_cc.h"
24#include "trunnel/flow_control_cells.h"
26#include "lib/math/stats.h"
27
29#include "core/or/cell_st.h"
30#include "app/config/config.h"
32
33/** Cache consensus parameters */
34static uint32_t xoff_client;
35static uint32_t xoff_exit;
36
37static uint32_t xon_change_pct;
38static uint32_t xon_ewma_cnt;
39static uint32_t xon_rate_bytes;
40
41/** Metricsport stats */
43uint64_t cc_stats_flow_num_xon_sent;
44double cc_stats_flow_xoff_outbuf_ma = 0;
45double cc_stats_flow_xon_outbuf_ma = 0;
46
47/* In normal operation, we can get a burst of up to 32 cells before returning
48 * to libevent to flush the outbuf. This is a heuristic from hardcoded values
49 * and strange logic in connection_bucket_get_share(). */
50#define MAX_EXPECTED_CELL_BURST 32
51
52/* The following three are for dropmark rate limiting. They define when we
53 * scale down our XON, XOFF, and xmit byte counts. Early scaling is beneficial
54 * because it limits the ability of spurious XON/XOFF to be sent after large
55 * amounts of data without XON/XOFF. At these limits, after 10MB of data (or
56 * more), an adversary can only inject (log2(10MB)-log2(200*500))*100 ~= 1000
57 * cells of fake XOFF/XON before the xmit byte count will be halved enough to
58 * triggering a limit. */
59#define XON_COUNT_SCALE_AT 200
60#define XOFF_COUNT_SCALE_AT 200
61#define ONE_MEGABYTE (UINT64_C(1) << 20)
62#define TOTAL_XMIT_SCALE_AT (10 * ONE_MEGABYTE)
63
64/**
65 * Update global congestion control related consensus parameter values, every
66 * consensus update.
67 *
68 * More details for each of the parameters can be found in proposal 324,
69 * section 6.5 including tuning notes.
70 */
71void
73{
74#define CC_XOFF_CLIENT_DFLT 500
75#define CC_XOFF_CLIENT_MIN 1
76#define CC_XOFF_CLIENT_MAX 10000
77 xoff_client = networkstatus_get_param(ns, "cc_xoff_client",
78 CC_XOFF_CLIENT_DFLT,
79 CC_XOFF_CLIENT_MIN,
80 CC_XOFF_CLIENT_MAX)*RELAY_PAYLOAD_SIZE_MIN;
81
82#define CC_XOFF_EXIT_DFLT 500
83#define CC_XOFF_EXIT_MIN 1
84#define CC_XOFF_EXIT_MAX 10000
85 xoff_exit = networkstatus_get_param(ns, "cc_xoff_exit",
86 CC_XOFF_EXIT_DFLT,
87 CC_XOFF_EXIT_MIN,
88 CC_XOFF_EXIT_MAX)*RELAY_PAYLOAD_SIZE_MIN;
89
90#define CC_XON_CHANGE_PCT_DFLT 25
91#define CC_XON_CHANGE_PCT_MIN 1
92#define CC_XON_CHANGE_PCT_MAX 99
93 xon_change_pct = networkstatus_get_param(ns, "cc_xon_change_pct",
94 CC_XON_CHANGE_PCT_DFLT,
95 CC_XON_CHANGE_PCT_MIN,
96 CC_XON_CHANGE_PCT_MAX);
97
98#define CC_XON_RATE_BYTES_DFLT (500)
99#define CC_XON_RATE_BYTES_MIN (1)
100#define CC_XON_RATE_BYTES_MAX (5000)
101 xon_rate_bytes = networkstatus_get_param(ns, "cc_xon_rate",
102 CC_XON_RATE_BYTES_DFLT,
103 CC_XON_RATE_BYTES_MIN,
104 CC_XON_RATE_BYTES_MAX)*RELAY_PAYLOAD_SIZE_MAX;
105
106#define CC_XON_EWMA_CNT_DFLT (2)
107#define CC_XON_EWMA_CNT_MIN (2)
108#define CC_XON_EWMA_CNT_MAX (100)
109 xon_ewma_cnt = networkstatus_get_param(ns, "cc_xon_ewma_cnt",
110 CC_XON_EWMA_CNT_DFLT,
111 CC_XON_EWMA_CNT_MIN,
112 CC_XON_EWMA_CNT_MAX);
113}
114
115/**
116 * Send an XOFF for this stream, and note that we sent one
117 */
118static void
120{
121 xoff_cell_t xoff;
122 uint8_t payload[CELL_PAYLOAD_SIZE];
123 ssize_t xoff_size;
124
125 memset(&xoff, 0, sizeof(xoff));
126 memset(payload, 0, sizeof(payload));
127
128 xoff_cell_set_version(&xoff, 0);
129
130 if ((xoff_size = xoff_cell_encode(payload, CELL_PAYLOAD_SIZE, &xoff)) < 0) {
131 log_warn(LD_BUG, "Failed to encode xon cell");
132 return;
133 }
134
135 if (connection_edge_send_command(stream, RELAY_COMMAND_XOFF,
136 (char*)payload, (size_t)xoff_size) == 0) {
137 stream->xoff_sent = true;
139
140 /* If this is an entry conn, notify control port */
141 if (TO_CONN(stream)->type == CONN_TYPE_AP) {
143 STREAM_EVENT_XOFF_SENT,
144 0);
145 }
146 }
147}
148
149/**
150 * Compute the recent drain rate (write rate) for this edge
151 * connection and return it, in KB/sec (1000 bytes/sec).
152 *
153 * Returns 0 if the monotime clock is busted.
154 */
155static inline uint32_t
157{
158 if (BUG(!is_monotime_clock_reliable())) {
159 log_warn(LD_BUG, "Computing drain rate with stalled monotime clock");
160 return 0;
161 }
162
163 uint64_t delta = monotime_absolute_usec() - stream->drain_start_usec;
164
165 if (delta == 0) {
166 log_warn(LD_BUG, "Computing stream drain rate with zero time delta");
167 return 0;
168 }
169
170 /* Overflow checks */
171 if (stream->prev_drained_bytes > INT32_MAX/1000 || /* Intermediate */
172 stream->prev_drained_bytes/delta > INT32_MAX/1000) { /* full value */
173 return INT32_MAX;
174 }
175
176 /* kb/sec = bytes/usec * 1000 usec/msec * 1000 msec/sec * kb/1000bytes */
177 return MAX(1, (uint32_t)(stream->prev_drained_bytes * 1000)/delta);
178}
179
180/**
181 * Send an XON for this stream, with appropriate advisory rate information.
182 *
183 * Reverts the xoff sent status, and stores the rate information we sent,
184 * in case it changes.
185 */
186static void
188{
189 xon_cell_t xon;
190 uint8_t payload[CELL_PAYLOAD_SIZE];
191 ssize_t xon_size;
192
193 memset(&xon, 0, sizeof(xon));
194 memset(payload, 0, sizeof(payload));
195
196 xon_cell_set_version(&xon, 0);
197 xon_cell_set_kbps_ewma(&xon, stream->ewma_drain_rate);
198
199 if ((xon_size = xon_cell_encode(payload, CELL_PAYLOAD_SIZE, &xon)) < 0) {
200 log_warn(LD_BUG, "Failed to encode xon cell");
201 return;
202 }
203
204 /* Store the advisory rate information, to send advisory updates if
205 * it changes */
206 stream->ewma_rate_last_sent = stream->ewma_drain_rate;
207
208 if (connection_edge_send_command(stream, RELAY_COMMAND_XON, (char*)payload,
209 (size_t)xon_size) == 0) {
210 /* Revert the xoff sent status, so we can send another one if need be */
211 stream->xoff_sent = false;
212
213 cc_stats_flow_num_xon_sent++;
214
215 /* If it's an entry conn, notify control port */
216 if (TO_CONN(stream)->type == CONN_TYPE_AP) {
218 STREAM_EVENT_XON_SENT,
219 0);
220 }
221 }
222}
223
224/**
225 * Process a stream XOFF, parsing it, and then stopping reading on
226 * the edge connection.
227 *
228 * Record that we have received an xoff, so we know not to resume
229 * reading on this edge conn until we get an XON.
230 *
231 * Returns false if the XOFF did not validate; true if it does.
232 */
233bool
235 const crypt_path_t *layer_hint)
236{
237 bool retval = true;
238
239 if (BUG(!conn)) {
240 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
241 "Got XOFF on invalid stream?");
242 return false;
243 }
244
245 /* Make sure this XOFF came from the right hop */
246 if (!edge_uses_cpath(conn, layer_hint)) {
247 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
248 "Got XOFF from wrong hop.");
249 return false;
250 }
251
252 if (!edge_uses_flow_control(conn)) {
253 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
254 "Got XOFF for non-congestion control circuit");
255 return false;
256 }
257
258 if (conn->xoff_received) {
259 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
260 "Got multiple XOFF on connection");
261 return false;
262 }
263
264 /* If we are near the max, scale everything down */
265 if (conn->num_xoff_recv == XOFF_COUNT_SCALE_AT) {
266 log_info(LD_EDGE, "Scaling down for XOFF count: %d %d %d",
267 conn->total_bytes_xmit,
268 conn->num_xoff_recv,
269 conn->num_xon_recv);
270 conn->total_bytes_xmit /= 2;
271 conn->num_xoff_recv /= 2;
272 conn->num_xon_recv /= 2;
273 }
274
275 conn->num_xoff_recv++;
276
277 /* Client-side check to make sure that XOFF is not sent too early,
278 * for dropmark attacks. The main sidechannel risk is early cells,
279 * but we also check to make sure that we have not received more XOFFs
280 * than could have been generated by the bytes we sent.
281 */
282 if (TO_CONN(conn)->type == CONN_TYPE_AP || conn->hs_ident != NULL) {
283 uint32_t limit = 0;
284 if (conn->hs_ident)
285 limit = xoff_client;
286 else
287 limit = xoff_exit;
288
289 if (conn->total_bytes_xmit < limit*conn->num_xoff_recv) {
290 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
291 "Got extra XOFF for bytes sent. Got %d, expected max %d",
292 conn->num_xoff_recv, conn->total_bytes_xmit/limit);
293 /* We still process this, because the only dropmark defenses
294 * in C tor are via the vanguards addon's use of the read valid
295 * cells. So just signal that we think this is not valid protocol
296 * data and proceed. */
297 retval = false;
298 }
299 }
300
301 log_info(LD_EDGE, "Got XOFF!");
303 conn->xoff_received = true;
304
305 /* If this is an entry conn, notify control port */
306 if (TO_CONN(conn)->type == CONN_TYPE_AP) {
308 STREAM_EVENT_XOFF_RECV,
309 0);
310 }
311
312 return retval;
313}
314
315/**
316 * Process a stream XON, and if it validates, clear the xoff
317 * flag and resume reading on this edge connection.
318 *
319 * Also, use provided rate information to rate limit
320 * reading on this edge (or packagaing from it onto
321 * the circuit), to avoid XON/XOFF chatter.
322 *
323 * Returns true if the XON validates, false otherwise.
324 */
325bool
327 const crypt_path_t *layer_hint,
328 const relay_msg_t *msg)
329{
330 xon_cell_t *xon;
331 bool retval = true;
332
333 if (BUG(!conn)) {
334 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
335 "Got XON on invalid stream?");
336 return false;
337 }
338
339 /* Make sure this XON came from the right hop */
340 if (!edge_uses_cpath(conn, layer_hint)) {
341 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
342 "Got XON from wrong hop.");
343 return false;
344 }
345
346 if (!edge_uses_flow_control(conn)) {
347 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
348 "Got XON for non-congestion control circuit");
349 return false;
350 }
351
352 if (xon_cell_parse(&xon, msg->body, msg->length) < 0) {
353 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
354 "Received malformed XON cell.");
355 return false;
356 }
357
358 /* If we are near the max, scale everything down */
359 if (conn->num_xon_recv == XON_COUNT_SCALE_AT) {
360 log_info(LD_EDGE, "Scaling down for XON count: %d %d %d",
361 conn->total_bytes_xmit,
362 conn->num_xoff_recv,
363 conn->num_xon_recv);
364 conn->total_bytes_xmit /= 2;
365 conn->num_xoff_recv /= 2;
366 conn->num_xon_recv /= 2;
367 }
368
369 conn->num_xon_recv++;
370
371 /* Client-side check to make sure that XON is not sent too early,
372 * for dropmark attacks. The main sidechannel risk is early cells,
373 * but we also check to see that we did not get more XONs than make
374 * sense for the number of bytes we sent.
375 */
376 if (TO_CONN(conn)->type == CONN_TYPE_AP || conn->hs_ident != NULL) {
377 uint32_t limit = 0;
378
379 if (conn->hs_ident)
380 limit = MIN(xoff_client, xon_rate_bytes);
381 else
382 limit = MIN(xoff_exit, xon_rate_bytes);
383
384 if (conn->total_bytes_xmit < limit*conn->num_xon_recv) {
385 log_fn(LOG_PROTOCOL_WARN, LD_EDGE,
386 "Got extra XON for bytes sent. Got %d, expected max %d",
387 conn->num_xon_recv, conn->total_bytes_xmit/limit);
388
389 /* We still process this, because the only dropmark defenses
390 * in C tor are via the vanguards addon's use of the read valid
391 * cells. So just signal that we think this is not valid protocol
392 * data and proceed. */
393 retval = false;
394 }
395 }
396
397 log_info(LD_EDGE, "Got XON: %d", xon->kbps_ewma);
398
399 /* Adjust the token bucket of this edge connection with the drain rate in
400 * the XON. Rate is in bytes from kilobit (kpbs). */
401 uint64_t rate = ((uint64_t) xon_cell_get_kbps_ewma(xon) * 1000);
402 if (rate == 0 || INT32_MAX < rate) {
403 /* No rate. */
404 rate = INT32_MAX;
405 }
406 token_bucket_rw_adjust(&conn->bucket, (uint32_t) rate, (uint32_t) rate);
407
408 if (conn->xoff_received) {
409 /* Clear the fact that we got an XOFF, so that this edge can
410 * start and stop reading normally */
411 conn->xoff_received = false;
413 }
414
415 /* If this is an entry conn, notify control port */
416 if (TO_CONN(conn)->type == CONN_TYPE_AP) {
418 STREAM_EVENT_XON_RECV,
419 0);
420 }
421
422 xon_cell_free(xon);
423
424 return retval;
425}
426
427/**
428 * Called from sendme_stream_data_received(), when data arrives
429 * from a circuit to our edge's outbuf, to decide if we need to send
430 * an XOFF.
431 *
432 * Returns the amount of cells remaining until the buffer is full, at
433 * which point it sends an XOFF, and returns 0.
434 *
435 * Returns less than 0 if we have queued more than a congestion window
436 * worth of data and need to close the circuit.
437 */
438int
440{
441 size_t total_buffered = connection_get_outbuf_len(TO_CONN(stream));
442 uint32_t buffer_limit_xoff = 0;
443
444 if (BUG(!edge_uses_flow_control(stream))) {
445 log_err(LD_BUG, "Flow control called for non-congestion control circuit");
446 return -1;
447 }
448
449 /* Onion services and clients are typically localhost edges, so they
450 * need different buffering limits than exits do */
451 if (TO_CONN(stream)->type == CONN_TYPE_AP || stream->hs_ident != NULL) {
452 buffer_limit_xoff = xoff_client;
453 } else {
454 buffer_limit_xoff = xoff_exit;
455 }
456
457 if (total_buffered > buffer_limit_xoff) {
458 if (!stream->xoff_sent) {
459 log_info(LD_EDGE, "Sending XOFF: %"TOR_PRIuSZ" %d",
460 total_buffered, buffer_limit_xoff);
461 tor_trace(TR_SUBSYS(cc), TR_EV(flow_decide_xoff_sending), stream);
462
463 cc_stats_flow_xoff_outbuf_ma =
464 stats_update_running_avg(cc_stats_flow_xoff_outbuf_ma,
465 total_buffered);
466
468
469 /* Clear the drain rate. It is considered wrong if we
470 * got all the way to XOFF */
471 stream->ewma_drain_rate = 0;
472 }
473 }
474
475 /* If the outbuf has accumulated more than the expected burst limit of
476 * cells, then assume it is not draining, and call decide_xon. We must
477 * do this because writes only happen when the socket unblocks, so
478 * may not otherwise notice accumulation of data in the outbuf for
479 * advisory XONs. */
480 if (total_buffered > MAX_EXPECTED_CELL_BURST*RELAY_PAYLOAD_SIZE_MIN) {
481 flow_control_decide_xon(stream, 0);
482 }
483
484 /* Flow control always takes more data; we rely on the oomkiller to
485 * handle misbehavior. */
486 return 0;
487}
488
489/**
490 * Returns true if the stream's drain rate has changed significantly.
491 *
492 * Returns false if the monotime clock is stalled, or if we have
493 * no previous drain rate information.
494 */
495static bool
497{
499 return false;
500 }
501
502 if (!stream->ewma_rate_last_sent) {
503 return false;
504 }
505
506 if (stream->ewma_drain_rate >
507 (100+(uint64_t)xon_change_pct)*stream->ewma_rate_last_sent/100) {
508 return true;
509 }
510
511 if (stream->ewma_drain_rate <
512 (100-(uint64_t)xon_change_pct)*stream->ewma_rate_last_sent/100) {
513 return true;
514 }
515
516 return false;
517}
518
519/**
520 * Called whenever we drain an edge connection outbuf by writing on
521 * its socket, to decide if it is time to send an xon.
522 *
523 * The n_written parameter tells us how many bytes we have written
524 * this time, which is used to compute the advisory drain rate fields.
525 */
526void
528{
529 size_t total_buffered = connection_get_outbuf_len(TO_CONN(stream));
530
531 /* Bounds check the number of drained bytes, and scale */
532 if (stream->drained_bytes >= UINT32_MAX - n_written) {
533 /* Cut the bytes in half, and move the start time up halfway to now
534 * (if we have one). */
535 stream->drained_bytes /= 2;
536
537 if (stream->drain_start_usec) {
538 uint64_t now = monotime_absolute_usec();
539
540 stream->drain_start_usec = now - (now-stream->drain_start_usec)/2;
541 }
542 }
543
544 /* Accumulate drained bytes since last rate computation */
545 stream->drained_bytes += n_written;
546
547 tor_trace(TR_SUBSYS(cc), TR_EV(flow_decide_xon), stream, n_written);
548
549 /* Check for bad monotime clock and bytecount wrap */
551 /* If the monotime clock ever goes wrong, the safest thing to do
552 * is just clear our short-term rate info and wait for the clock to
553 * become reliable again.. */
554 stream->drain_start_usec = 0;
555 stream->drained_bytes = 0;
556 } else {
557 /* If we have no drain start timestamp, and we still have
558 * remaining buffer, start the buffering counter */
559 if (!stream->drain_start_usec && total_buffered > 0) {
560 log_debug(LD_EDGE, "Began edge buffering: %d %d %"TOR_PRIuSZ,
561 stream->ewma_rate_last_sent,
562 stream->ewma_drain_rate,
563 total_buffered);
564 tor_trace(TR_SUBSYS(cc), TR_EV(flow_decide_xon_drain_start),
565 stream);
567 stream->drained_bytes = 0;
568 }
569 }
570
571 if (stream->drain_start_usec) {
572 /* If we have spent enough time in a queued state, update our drain
573 * rate. */
574 if (stream->drained_bytes > xon_rate_bytes) {
575 /* No previous drained bytes means it is the first time we are computing
576 * it so use the value we just drained onto the socket as a baseline. It
577 * won't be accurate but it will be a start towards the right value.
578 *
579 * We have to do this in order to have a drain rate else we could be
580 * sending a drain rate of 0 in an XON which would be undesirable and
581 * basically like sending an XOFF. */
582 if (stream->prev_drained_bytes == 0) {
583 stream->prev_drained_bytes = stream->drained_bytes;
584 }
585 uint32_t drain_rate = compute_drain_rate(stream);
586 /* Once the drain rate has been computed, note how many bytes we just
587 * drained so it can be used at the next calculation. We do this here
588 * because it gets reset once the rate is changed. */
589 stream->prev_drained_bytes = stream->drained_bytes;
590
591 if (drain_rate) {
592 stream->ewma_drain_rate =
593 (uint32_t)n_count_ewma(drain_rate,
594 stream->ewma_drain_rate,
595 xon_ewma_cnt);
596 log_debug(LD_EDGE, "Updating drain rate: %d %d %"TOR_PRIuSZ,
597 drain_rate,
598 stream->ewma_drain_rate,
599 total_buffered);
600 tor_trace(TR_SUBSYS(cc), TR_EV(flow_decide_xon_drain_update),
601 stream, drain_rate);
602 /* Reset recent byte counts. This prevents us from sending advisory
603 * XONs more frequent than every xon_rate_bytes. */
604 stream->drained_bytes = 0;
605 stream->drain_start_usec = 0;
606 }
607 }
608 }
609
610 /* If we don't have an XOFF outstanding, consider updating an
611 * old rate */
612 if (!stream->xoff_sent) {
613 if (stream_drain_rate_changed(stream)) {
614 /* If we are still buffering and the rate changed, update
615 * advisory XON */
616 log_info(LD_EDGE, "Sending rate-change XON: %d %d %"TOR_PRIuSZ,
617 stream->ewma_rate_last_sent,
618 stream->ewma_drain_rate,
619 total_buffered);
620 tor_trace(TR_SUBSYS(cc), TR_EV(flow_decide_xon_rate_change), stream);
621
622 cc_stats_flow_xon_outbuf_ma =
623 stats_update_running_avg(cc_stats_flow_xon_outbuf_ma,
624 total_buffered);
625
627 }
628 } else if (total_buffered == 0) {
629 log_info(LD_EDGE, "Sending XON: %d %d %"TOR_PRIuSZ,
630 stream->ewma_rate_last_sent,
631 stream->ewma_drain_rate,
632 total_buffered);
633 tor_trace(TR_SUBSYS(cc), TR_EV(flow_decide_xon_partial_drain), stream);
635 }
636
637 /* If the buffer has fully emptied, clear the drain timestamp,
638 * so we can total only bytes drained while outbuf is 0. */
639 if (total_buffered == 0) {
640 stream->drain_start_usec = 0;
641
642 /* After we've spent 'xon_rate_bytes' with the queue fully drained,
643 * double any rate we sent. */
644 if (stream->drained_bytes >= xon_rate_bytes &&
645 stream->ewma_rate_last_sent) {
646 stream->ewma_drain_rate = MIN(INT32_MAX, 2*stream->ewma_drain_rate);
647
648 log_debug(LD_EDGE,
649 "Queue empty for xon_rate_limit bytes: %d %d",
650 stream->ewma_rate_last_sent,
651 stream->ewma_drain_rate);
652 tor_trace(TR_SUBSYS(cc), TR_EV(flow_decide_xon_drain_doubled), stream);
653 /* Resetting the drained bytes count. We need to keep its value as a
654 * previous so the drain rate calculation takes into account what was
655 * actually drain the last time. */
656 stream->prev_drained_bytes = stream->drained_bytes;
657 stream->drained_bytes = 0;
658 }
659 }
660
661 return;
662}
663
664/**
665 * Note that we packaged some data on this stream. Used to enforce
666 * client-side dropmark limits
667 */
668void
670{
671 /* If we are near the max, scale everything down */
672 if (stream->total_bytes_xmit >= TOTAL_XMIT_SCALE_AT-len) {
673 log_info(LD_EDGE, "Scaling down for flow control xmit bytes:: %d %d %d",
674 stream->total_bytes_xmit,
675 stream->num_xoff_recv,
676 stream->num_xon_recv);
677
678 stream->total_bytes_xmit /= 2;
679 stream->num_xoff_recv /= 2;
680 stream->num_xon_recv /= 2;
681 }
682
683 stream->total_bytes_xmit += len;
684}
685
686/** Returns true if an edge connection uses flow control */
687bool
689{
690 bool ret = (stream->on_circuit && stream->on_circuit->ccontrol) ||
691 (stream->cpath_layer && stream->cpath_layer->ccontrol);
692
693 /* All circuits with congestion control use flow control */
694 return ret;
695}
696
697/** Returns true if a connection is an edge conn that uses flow control */
698bool
700{
701 bool ret = false;
702
703 if (CONN_IS_EDGE(conn)) {
704 edge_connection_t *edge = TO_EDGE_CONN(conn);
705
706 if (edge_uses_flow_control(edge)) {
707 ret = true;
708 }
709 }
710
711 return ret;
712}
Fixed-size cell structure.
Header file for circuitlist.c.
#define MAX(a, b)
Definition: cmp.h:22
uint64_t monotime_absolute_usec(void)
Definition: compat_time.c:804
Header file for config.c.
bool edge_uses_cpath(const edge_connection_t *conn, const crypt_path_t *cpath)
Definition: conflux_util.c:172
Header file for conflux_util.c.
bool is_monotime_clock_reliable(void)
Public APIs for congestion control.
static uint64_t n_count_ewma(uint64_t curr, uint64_t prev, uint64_t N)
bool circuit_process_stream_xoff(edge_connection_t *conn, const crypt_path_t *layer_hint)
static uint32_t compute_drain_rate(const edge_connection_t *stream)
int flow_control_decide_xoff(edge_connection_t *stream)
void flow_control_note_sent_data(edge_connection_t *stream, size_t len)
static bool stream_drain_rate_changed(const edge_connection_t *stream)
bool conn_uses_flow_control(connection_t *conn)
void flow_control_new_consensus_params(const networkstatus_t *ns)
bool edge_uses_flow_control(const edge_connection_t *stream)
uint64_t cc_stats_flow_num_xoff_sent
bool circuit_process_stream_xon(edge_connection_t *conn, const crypt_path_t *layer_hint, const relay_msg_t *msg)
static void circuit_send_stream_xon(edge_connection_t *stream)
void flow_control_decide_xon(edge_connection_t *stream, size_t n_written)
static void circuit_send_stream_xoff(edge_connection_t *stream)
static uint32_t xoff_client
APIs for stream flow control on congestion controlled circuits.
Structure definitions for congestion control.
Header file for connection.c.
#define CONN_TYPE_AP
Definition: connection.h:51
entry_connection_t * TO_ENTRY_CONN(connection_t *c)
edge_connection_t * TO_EDGE_CONN(connection_t *c)
Header file for connection_edge.c.
Base connection structure.
#define CONN_IS_EDGE(x)
int control_event_stream_status(entry_connection_t *conn, stream_status_event_t tp, int reason_code)
Header file for control_events.c.
#define TR_SUBSYS(name)
Definition: events.h:45
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define LD_EDGE
Definition: log.h:94
#define LD_BUG
Definition: log.h:86
void connection_stop_reading(connection_t *conn)
Definition: mainloop.c:601
void connection_start_reading(connection_t *conn)
Definition: mainloop.c:623
Header file for mainloop.c.
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Header file for networkstatus.c.
Master header file for Tor-specific functionality.
#define CELL_PAYLOAD_SIZE
Definition: or.h:520
#define RELAY_PAYLOAD_SIZE_MIN
Definition: or.h:570
#define TO_CONN(c)
Definition: or.h:700
#define RELAY_PAYLOAD_SIZE_MAX
Definition: or.h:567
int connection_edge_send_command(edge_connection_t *fromconn, uint8_t relay_command, const char *payload, size_t payload_len)
Definition: relay.c:766
Header file for relay.c.
Header for stats.c.
struct congestion_control_t * ccontrol
Definition: circuit_st.h:250
struct congestion_control_t * ccontrol
Definition: crypt_path_st.h:88
struct crypt_path_t * cpath_layer
token_bucket_rw_t bucket
struct circuit_t * on_circuit
void token_bucket_rw_adjust(token_bucket_rw_t *bucket, uint32_t rate, uint32_t burst)
Definition: token_bucket.c:154