tor-commits
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- 1 participants
- 216219 discussions
31 Oct '13
commit 2e0fad542cccddf9ad8b8dbaeba8b1e825c09ff4
Merge: 49278cd e46de82
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Mon Oct 28 11:30:49 2013 +0100
Merge branch 'morestats4' into morestats5
Conflicts:
doc/tor.1.txt
src/or/config.c
src/or/connection.h
src/or/control.c
src/or/control.h
src/or/or.h
src/or/relay.c
src/or/relay.h
src/test/test.c
doc/tor.1.txt | 19 +++
src/or/circuitmux.c | 2 +-
src/or/command.c | 27 ++++
src/or/command.h | 2 +
src/or/config.c | 24 ++++
src/or/connection.c | 180 ++++++++++++++++++++++-
src/or/connection.h | 9 ++
src/or/control.c | 274 ++++++++++++++++++++++++++++++++++-
src/or/control.h | 35 +++++
src/or/main.c | 5 +
src/or/or.h | 70 +++++++++
src/or/relay.c | 121 ++++++++++++++--
src/or/relay.h | 3 +-
src/test/Makefile.nmake | 6 +-
src/test/include.am | 1 +
src/test/test.c | 2 +
src/test/test_cell_queue.c | 6 +-
src/test/test_controller_events.c | 287 +++++++++++++++++++++++++++++++++++++
18 files changed, 1047 insertions(+), 26 deletions(-)
diff --cc doc/tor.1.txt
index ac33c59,3996334..615da2d
--- a/doc/tor.1.txt
+++ b/doc/tor.1.txt
@@@ -2050,19 -2005,11 +2050,22 @@@ The following options are used for runn
TestingV3AuthInitialDistDelay 20 seconds
TestingAuthDirTimeToLearnReachability 0 minutes
TestingEstimatedDescriptorPropagationTime 0 minutes
+ TestingServerDownloadSchedule 0, 0, 0, 5, 10, 15, 20, 30, 60
+ TestingClientDownloadSchedule 0, 0, 5, 10, 15, 20, 30, 60
+ TestingServerConsensusDownloadSchedule 0, 0, 5, 10, 15, 20, 30, 60
+ TestingClientConsensusDownloadSchedule 0, 0, 5, 10, 15, 20, 30, 60
+ TestingBridgeDownloadSchedule 60, 30, 30, 60
+ TestingClientMaxIntervalWithoutRequest 5 seconds
+ TestingDirConnectionMaxStall 30 seconds
+ TestingConsensusMaxDownloadTries 80
+ TestingDescriptorMaxDownloadTries 80
+ TestingMicrodescMaxDownloadTries 80
+ TestingCertMaxDownloadTries 80
+ TestingEnableConnBwEvent 1
+ TestingEnableCellStatsEvent 1
+ TestingEnableTbEmptyEvent 1
-**TestingV3AuthInitialVotingInterval** __N__ **minutes**|**hours**::
+[[TestingV3AuthInitialVotingInterval]] **TestingV3AuthInitialVotingInterval** __N__ **minutes**|**hours**::
Like V3AuthVotingInterval, but for initial voting interval before the first
consensus has been created. Changing this requires that
**TestingTorNetwork** is set. (Default: 30 minutes)
@@@ -2095,66 -2038,22 +2098,82 @@@
Minimum value for the Fast flag. Overrides the ordinary minimum taken
from the consensus when TestingTorNetwork is set. (Default: 0.)
-**TestingEnableConnBwEvent** **0**|**1**::
+[[TestingServerDownloadSchedule]] **TestingServerDownloadSchedule** __N__,__N__,__...__::
+ Schedule for when servers should download things in general. Changing this
+ requires that **TestingTorNetwork** is set. (Default: 0, 0, 0, 60, 60, 120,
+ 300, 900, 2147483647)
+
+[[TestingClientDownloadSchedule]] **TestingClientDownloadSchedule** __N__,__N__,__...__::
+ Schedule for when clients should download things in general. Changing this
+ requires that **TestingTorNetwork** is set. (Default: 0, 0, 60, 300, 600,
+ 2147483647)
+
+[[TestingServerConsensusDownloadSchedule]] **TestingServerConsensusDownloadSchedule** __N__,__N__,__...__::
+ Schedule for when servers should download consensuses. Changing this
+ requires that **TestingTorNetwork** is set. (Default: 0, 0, 60, 300, 600,
+ 1800, 1800, 1800, 1800, 1800, 3600, 7200)
+
+[[TestingClientConsensusDownloadSchedule]] **TestingClientConsensusDownloadSchedule** __N__,__N__,__...__::
+ Schedule for when clients should download consensuses. Changing this
+ requires that **TestingTorNetwork** is set. (Default: 0, 0, 60, 300, 600,
+ 1800, 3600, 3600, 3600, 10800, 21600, 43200)
+
+[[TestingBridgeDownloadSchedule]] **TestingBridgeDownloadSchedule** __N__,__N__,__...__::
+ Schedule for when clients should download bridge descriptors. Changing this
+ requires that **TestingTorNetwork** is set. (Default: 3600, 900, 900, 3600)
+
+[[TestingClientMaxIntervalWithoutRequest]] **TestingClientMaxIntervalWithoutRequest** __N__ **seconds**|**minutes**::
+ When directory clients have only a few descriptors to request, they batch
+ them until they have more, or until this amount of time has passed.
+ Changing this requires that **TestingTorNetwork** is set. (Default: 10
+ minutes)
+
+[[TestingDirConnectionMaxStall]] **TestingDirConnectionMaxStall** __N__ **seconds**|**minutes**::
+ Let a directory connection stall this long before expiring it.
+ Changing this requires that **TestingTorNetwork** is set. (Default:
+ 5 minutes)
+
+[[TestingConsensusMaxDownloadTries]] **TestingConsensusMaxDownloadTries** __NUM__::
+ Try this often to download a consensus before giving up. Changing
+ this requires that **TestingTorNetwork** is set. (Default: 8)
+
+[[TestingDescriptorMaxDownloadTries]] **TestingDescriptorMaxDownloadTries** __NUM__::
+ Try this often to download a router descriptor before giving up.
+ Changing this requires that **TestingTorNetwork** is set. (Default: 8)
+
+[[TestingMicrodescMaxDownloadTries]] **TestingMicrodescMaxDownloadTries** __NUM__::
+ Try this often to download a microdesc descriptor before giving up.
+ Changing this requires that **TestingTorNetwork** is set. (Default: 8)
+
+[[TestingCertMaxDownloadTries]] **TestingCertMaxDownloadTries** __NUM__::
+ Try this often to download a v3 authority certificate before giving up.
+ Changing this requires that **TestingTorNetwork** is set. (Default: 8)
+
+**TestingDirAuthVoteGuard** __node__,__node__,__...__::
+ A list of identity fingerprints, nicknames, country codes and
+ address patterns of nodes to vote Guard for regardless of their
+ uptime and bandwidth. See the **ExcludeNodes** option for more
+ information on how to specify nodes.
+ +
+ In order for this option to have any effect, **TestingTorNetwork**
+ has to be set.
+
++[[TestingEnableConnBwEvent]] **TestingEnableConnBwEvent** **0**|**1**::
+ If this option is set, then Tor controllers may register for CONN_BW
+ events. Changing this requires that **TestingTorNetwork** is set.
+ (Default: 0)
+
-**TestingEnableCellStatsEvent** **0**|**1**::
++[[TestingEnableCellStatsEvent]] **TestingEnableCellStatsEvent** **0**|**1**::
+ If this option is set, then Tor controllers may register for CELL_STATS
+ events. Changing this requires that **TestingTorNetwork** is set.
+ (Default: 0)
+
-**TestingEnableTbEmptyEvent** **0**|**1**::
++[[TestingEnableTbEmptyEvent]] **TestingEnableTbEmptyEvent** **0**|**1**::
+ If this option is set, then Tor controllers may register for TB_EMPTY
+ events. Changing this requires that **TestingTorNetwork** is set.
+ (Default: 0)
+
+
SIGNALS
-------
diff --cc src/or/circuitmux.c
index 47b4230,545cfd0..f2af943
--- a/src/or/circuitmux.c
+++ b/src/or/circuitmux.c
@@@ -1859,40 -1743,3 +1859,40 @@@ circuitmux_assert_okay_pass_three(circu
}
}
+/*DOCDOC */
+void
+circuitmux_append_destroy_cell(channel_t *chan,
+ circuitmux_t *cmux,
+ circid_t circ_id,
+ uint8_t reason)
+{
+ cell_t cell;
+ memset(&cell, 0, sizeof(cell_t));
+ cell.circ_id = circ_id;
+ cell.command = CELL_DESTROY;
+ cell.payload[0] = (uint8_t) reason;
+
- cell_queue_append_packed_copy(&cmux->destroy_cell_queue, &cell,
++ cell_queue_append_packed_copy(NULL, &cmux->destroy_cell_queue, 0, &cell,
+ chan->wide_circ_ids, 0);
+
+ /* Destroy entering the queue, update counters */
+ ++(cmux->destroy_ctr);
+ ++global_destroy_ctr;
+ log_debug(LD_CIRC,
+ "Cmux at %p queued a destroy for circ %u, cmux counter is now "
+ I64_FORMAT", global counter is now "I64_FORMAT,
+ cmux, circ_id,
+ I64_PRINTF_ARG(cmux->destroy_ctr),
+ I64_PRINTF_ARG(global_destroy_ctr));
+
+ /* XXXX Duplicate code from append_cell_to_circuit_queue */
+ if (!channel_has_queued_writes(chan)) {
+ /* There is no data at all waiting to be sent on the outbuf. Add a
+ * cell, so that we can notice when it gets flushed, flushed_some can
+ * get called, and we can start putting more data onto the buffer then.
+ */
+ log_debug(LD_GENERAL, "Primed a buffer.");
+ channel_flush_from_first_active_circuit(chan, 1);
+ }
+}
+
diff --cc src/or/config.c
index 95cede0,4ca0338..e0e5bb6
--- a/src/or/config.c
+++ b/src/or/config.c
@@@ -488,21 -464,9 +491,24 @@@ static const config_var_t testing_tor_n
V(TestingAuthDirTimeToLearnReachability, INTERVAL, "0 minutes"),
V(TestingEstimatedDescriptorPropagationTime, INTERVAL, "0 minutes"),
V(MinUptimeHidServDirectoryV2, INTERVAL, "0 minutes"),
+ V(TestingServerDownloadSchedule, CSV_INTERVAL, "0, 0, 0, 5, 10, 15, "
+ "20, 30, 60"),
+ V(TestingClientDownloadSchedule, CSV_INTERVAL, "0, 0, 5, 10, 15, 20, "
+ "30, 60"),
+ V(TestingServerConsensusDownloadSchedule, CSV_INTERVAL, "0, 0, 5, 10, "
+ "15, 20, 30, 60"),
+ V(TestingClientConsensusDownloadSchedule, CSV_INTERVAL, "0, 0, 5, 10, "
+ "15, 20, 30, 60"),
+ V(TestingBridgeDownloadSchedule, CSV_INTERVAL, "60, 30, 30, 60"),
+ V(TestingClientMaxIntervalWithoutRequest, INTERVAL, "5 seconds"),
+ V(TestingDirConnectionMaxStall, INTERVAL, "30 seconds"),
+ V(TestingConsensusMaxDownloadTries, UINT, "80"),
+ V(TestingDescriptorMaxDownloadTries, UINT, "80"),
+ V(TestingMicrodescMaxDownloadTries, UINT, "80"),
+ V(TestingCertMaxDownloadTries, UINT, "80"),
+ V(TestingEnableConnBwEvent, BOOL, "1"),
+ V(TestingEnableCellStatsEvent, BOOL, "1"),
+ V(TestingEnableTbEmptyEvent, BOOL, "1"),
VAR("___UsingTestNetworkDefaults", BOOL, UsingTestNetworkDefaults_, "1"),
{ NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
@@@ -3431,42 -3242,24 +3437,60 @@@ options_validate(or_options_t *old_opti
COMPLAIN("TestingEstimatedDescriptorPropagationTime is insanely high.");
}
+ if (options->TestingClientMaxIntervalWithoutRequest < 1) {
+ REJECT("TestingClientMaxIntervalWithoutRequest is way too low.");
+ } else if (options->TestingClientMaxIntervalWithoutRequest > 3600) {
+ COMPLAIN("TestingClientMaxIntervalWithoutRequest is insanely high.");
+ }
+
+ if (options->TestingDirConnectionMaxStall < 5) {
+ REJECT("TestingDirConnectionMaxStall is way too low.");
+ } else if (options->TestingDirConnectionMaxStall > 3600) {
+ COMPLAIN("TestingDirConnectionMaxStall is insanely high.");
+ }
+
+ if (options->TestingConsensusMaxDownloadTries < 2) {
+ REJECT("TestingConsensusMaxDownloadTries must be greater than 1.");
+ } else if (options->TestingConsensusMaxDownloadTries > 800) {
+ COMPLAIN("TestingConsensusMaxDownloadTries is insanely high.");
+ }
+
+ if (options->TestingDescriptorMaxDownloadTries < 2) {
+ REJECT("TestingDescriptorMaxDownloadTries must be greater than 1.");
+ } else if (options->TestingDescriptorMaxDownloadTries > 800) {
+ COMPLAIN("TestingDescriptorMaxDownloadTries is insanely high.");
+ }
+
+ if (options->TestingMicrodescMaxDownloadTries < 2) {
+ REJECT("TestingMicrodescMaxDownloadTries must be greater than 1.");
+ } else if (options->TestingMicrodescMaxDownloadTries > 800) {
+ COMPLAIN("TestingMicrodescMaxDownloadTries is insanely high.");
+ }
+
+ if (options->TestingCertMaxDownloadTries < 2) {
+ REJECT("TestingCertMaxDownloadTries must be greater than 1.");
+ } else if (options->TestingCertMaxDownloadTries > 800) {
+ COMPLAIN("TestingCertMaxDownloadTries is insanely high.");
+ }
+
+ if (options->TestingEnableConnBwEvent &&
+ !options->TestingTorNetwork && !options->UsingTestNetworkDefaults_) {
+ REJECT("TestingEnableConnBwEvent may only be changed in testing "
+ "Tor networks!");
+ }
+
+ if (options->TestingEnableCellStatsEvent &&
+ !options->TestingTorNetwork && !options->UsingTestNetworkDefaults_) {
+ REJECT("TestingEnableCellStatsEvent may only be changed in testing "
+ "Tor networks!");
+ }
+
+ if (options->TestingEnableTbEmptyEvent &&
+ !options->TestingTorNetwork && !options->UsingTestNetworkDefaults_) {
+ REJECT("TestingEnableTbEmptyEvent may only be changed in testing "
+ "Tor networks!");
+ }
+
if (options->TestingTorNetwork) {
log_warn(LD_CONFIG, "TestingTorNetwork is set. This will make your node "
"almost unusable in the public Tor network, and is "
diff --cc src/or/connection.h
index 0454ac2,53d3da0..4073d9f
--- a/src/or/connection.h
+++ b/src/or/connection.h
@@@ -215,7 -215,14 +215,16 @@@ void connection_enable_rate_limiting(co
#endif
#ifdef CONNECTION_PRIVATE
+STATIC void connection_free_(connection_t *conn);
++
+ /* Used only by connection.c and test*.c */
+ uint32_t bucket_millis_empty(int tokens_before, uint32_t last_empty_time,
+ int tokens_after, int milliseconds_elapsed,
+ const struct timeval *tvnow);
+ void connection_buckets_note_empty_ts(uint32_t *timestamp_var,
+ int tokens_before,
+ size_t tokens_removed,
+ const struct timeval *tvnow);
#endif
#endif
diff --cc src/or/control.c
index 65c543a,9e026b3..49212de
--- a/src/or/control.c
+++ b/src/or/control.c
@@@ -190,6 -237,20 +191,20 @@@ log_severity_to_event(int severity
}
}
+ /** Helper: clear bandwidth counters of all origin circuits. */
+ static void
+ clear_circ_bw_fields(void)
+ {
+ circuit_t *circ;
+ origin_circuit_t *ocirc;
- for (circ = circuit_get_global_list_(); circ; circ = circ->next) {
++ TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
+ if (!CIRCUIT_IS_ORIGIN(circ))
+ continue;
+ ocirc = TO_ORIGIN_CIRCUIT(circ);
+ ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
+ }
+ }
+
/** Set <b>global_event_mask*</b> to the bitwise OR of each live control
* connection's event_mask field. */
void
@@@ -916,7 -981,10 +935,11 @@@ static const struct control_event_t con
{ EVENT_BUILDTIMEOUT_SET, "BUILDTIMEOUT_SET" },
{ EVENT_SIGNAL, "SIGNAL" },
{ EVENT_CONF_CHANGED, "CONF_CHANGED"},
+ { EVENT_CONN_BW, "CONN_BW" },
+ { EVENT_CELL_STATS, "CELL_STATS" },
+ { EVENT_TB_EMPTY, "TB_EMPTY" },
+ { EVENT_CIRC_BANDWIDTH_USED, "CIRC_BW" },
+ { EVENT_TRANSPORT_LAUNCHED, "TRANSPORT_LAUNCHED" },
{ 0, NULL },
};
@@@ -3902,6 -3940,235 +3933,235 @@@ control_event_stream_bandwidth_used(voi
return 0;
}
+ /** A second or more has elapsed: tell any interested control connections
+ * how much bandwidth origin circuits have used. */
+ int
+ control_event_circ_bandwidth_used(void)
+ {
+ circuit_t *circ;
+ origin_circuit_t *ocirc;
+ if (!EVENT_IS_INTERESTING(EVENT_CIRC_BANDWIDTH_USED))
+ return 0;
+
- for (circ = circuit_get_global_list_(); circ; circ = circ->next) {
++ TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
+ if (!CIRCUIT_IS_ORIGIN(circ))
+ continue;
+ ocirc = TO_ORIGIN_CIRCUIT(circ);
+ if (!ocirc->n_read_circ_bw && !ocirc->n_written_circ_bw)
+ continue;
+ send_control_event(EVENT_CIRC_BANDWIDTH_USED, ALL_FORMATS,
+ "650 CIRC_BW ID=%d READ=%lu WRITTEN=%lu\r\n",
+ ocirc->global_identifier,
+ (unsigned long)ocirc->n_read_circ_bw,
+ (unsigned long)ocirc->n_written_circ_bw);
+ ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
+ }
+
+ return 0;
+ }
+
+ /** Print out CONN_BW event for a single OR/DIR/EXIT <b>conn</b> and reset
+ * bandwidth counters. */
+ int
+ control_event_conn_bandwidth(connection_t *conn)
+ {
+ const char *conn_type_str;
+ if (!get_options()->TestingEnableConnBwEvent ||
+ !EVENT_IS_INTERESTING(EVENT_CONN_BW))
+ return 0;
+ if (!conn->n_read_conn_bw && !conn->n_written_conn_bw)
+ return 0;
+ switch (conn->type) {
+ case CONN_TYPE_OR:
+ conn_type_str = "OR";
+ break;
+ case CONN_TYPE_DIR:
+ conn_type_str = "DIR";
+ break;
+ case CONN_TYPE_EXIT:
+ conn_type_str = "EXIT";
+ break;
+ default:
+ return 0;
+ }
+ send_control_event(EVENT_CONN_BW, ALL_FORMATS,
+ "650 CONN_BW ID="U64_FORMAT" TYPE=%s "
+ "READ=%lu WRITTEN=%lu\r\n",
+ U64_PRINTF_ARG(conn->global_identifier),
+ conn_type_str,
+ (unsigned long)conn->n_read_conn_bw,
+ (unsigned long)conn->n_written_conn_bw);
+ conn->n_written_conn_bw = conn->n_read_conn_bw = 0;
+ return 0;
+ }
+
+ /** A second or more has elapsed: tell any interested control
+ * connections how much bandwidth connections have used. */
+ int
+ control_event_conn_bandwidth_used(void)
+ {
+ if (get_options()->TestingEnableConnBwEvent &&
+ EVENT_IS_INTERESTING(EVENT_CONN_BW)) {
+ SMARTLIST_FOREACH(get_connection_array(), connection_t *, conn,
+ control_event_conn_bandwidth(conn));
+ }
+ return 0;
+ }
+
+ /** Helper: iterate over cell statistics of <b>circ</b> and sum up added
+ * cells, removed cells, and waiting times by cell command and direction.
+ * Store results in <b>cell_stats</b>. Free cell statistics of the
+ * circuit afterwards. */
+ void
+ sum_up_cell_stats_by_command(circuit_t *circ, cell_stats_t *cell_stats)
+ {
+ memset(cell_stats, 0, sizeof(cell_stats_t));
+ SMARTLIST_FOREACH_BEGIN(circ->testing_cell_stats,
+ testing_cell_stats_entry_t *, ent) {
+ tor_assert(ent->command <= CELL_COMMAND_MAX_);
+ if (!ent->removed && !ent->exitward) {
+ cell_stats->added_cells_appward[ent->command] += 1;
+ } else if (!ent->removed && ent->exitward) {
+ cell_stats->added_cells_exitward[ent->command] += 1;
+ } else if (!ent->exitward) {
+ cell_stats->removed_cells_appward[ent->command] += 1;
+ cell_stats->total_time_appward[ent->command] += ent->waiting_time * 10;
+ } else {
+ cell_stats->removed_cells_exitward[ent->command] += 1;
+ cell_stats->total_time_exitward[ent->command] += ent->waiting_time * 10;
+ }
+ tor_free(ent);
+ } SMARTLIST_FOREACH_END(ent);
+ smartlist_free(circ->testing_cell_stats);
+ circ->testing_cell_stats = NULL;
+ }
+
+ /** Helper: append a cell statistics string to <code>event_parts</code>,
+ * prefixed with <code>key</code>=. Statistics consist of comma-separated
+ * key:value pairs with lower-case command strings as keys and cell
+ * numbers or total waiting times as values. A key:value pair is included
+ * if the entry in <code>include_if_non_zero</code> is not zero, but with
+ * the (possibly zero) entry from <code>number_to_include</code>. Both
+ * arrays are expected to have a length of CELL_COMMAND_MAX_ + 1. If no
+ * entry in <code>include_if_non_zero</code> is positive, no string will
+ * be added to <code>event_parts</code>. */
+ void
+ append_cell_stats_by_command(smartlist_t *event_parts, const char *key,
+ const uint64_t *include_if_non_zero,
+ const uint64_t *number_to_include)
+ {
+ smartlist_t *key_value_strings = smartlist_new();
+ int i;
+ for (i = 0; i <= CELL_COMMAND_MAX_; i++) {
+ if (include_if_non_zero[i] > 0) {
+ smartlist_add_asprintf(key_value_strings, "%s:"U64_FORMAT,
+ cell_command_to_string(i),
+ U64_PRINTF_ARG(number_to_include[i]));
+ }
+ }
+ if (smartlist_len(key_value_strings) > 0) {
+ char *joined = smartlist_join_strings(key_value_strings, ",", 0, NULL);
+ smartlist_add_asprintf(event_parts, "%s=%s", key, joined);
+ SMARTLIST_FOREACH(key_value_strings, char *, cp, tor_free(cp));
+ tor_free(joined);
+ }
+ smartlist_free(key_value_strings);
+ }
+
+ /** Helper: format <b>cell_stats</b> for <b>circ</b> for inclusion in a
+ * CELL_STATS event and write result string to <b>event_string</b>. */
+ void
+ format_cell_stats(char **event_string, circuit_t *circ,
+ cell_stats_t *cell_stats)
+ {
+ smartlist_t *event_parts = smartlist_new();
+ if (CIRCUIT_IS_ORIGIN(circ)) {
+ origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
+ smartlist_add_asprintf(event_parts, "ID=%lu",
+ (unsigned long)ocirc->global_identifier);
+ } else if (TO_OR_CIRCUIT(circ)->p_chan) {
+ or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
+ smartlist_add_asprintf(event_parts, "InboundQueue=%lu",
+ (unsigned long)or_circ->p_circ_id);
+ smartlist_add_asprintf(event_parts, "InboundConn="U64_FORMAT,
+ U64_PRINTF_ARG(or_circ->p_chan->global_identifier));
+ append_cell_stats_by_command(event_parts, "InboundAdded",
+ cell_stats->added_cells_appward,
+ cell_stats->added_cells_appward);
+ append_cell_stats_by_command(event_parts, "InboundRemoved",
+ cell_stats->removed_cells_appward,
+ cell_stats->removed_cells_appward);
+ append_cell_stats_by_command(event_parts, "InboundTime",
+ cell_stats->removed_cells_appward,
+ cell_stats->total_time_appward);
+ }
+ if (circ->n_chan) {
+ smartlist_add_asprintf(event_parts, "OutboundQueue=%lu",
+ (unsigned long)circ->n_circ_id);
+ smartlist_add_asprintf(event_parts, "OutboundConn="U64_FORMAT,
+ U64_PRINTF_ARG(circ->n_chan->global_identifier));
+ append_cell_stats_by_command(event_parts, "OutboundAdded",
+ cell_stats->added_cells_exitward,
+ cell_stats->added_cells_exitward);
+ append_cell_stats_by_command(event_parts, "OutboundRemoved",
+ cell_stats->removed_cells_exitward,
+ cell_stats->removed_cells_exitward);
+ append_cell_stats_by_command(event_parts, "OutboundTime",
+ cell_stats->removed_cells_exitward,
+ cell_stats->total_time_exitward);
+ }
+ *event_string = smartlist_join_strings(event_parts, " ", 0, NULL);
+ SMARTLIST_FOREACH(event_parts, char *, cp, tor_free(cp));
+ smartlist_free(event_parts);
+ }
+
+ /** A second or more has elapsed: tell any interested control connection
+ * how many cells have been processed for a given circuit. */
+ int
+ control_event_circuit_cell_stats(void)
+ {
+ circuit_t *circ;
+ cell_stats_t *cell_stats;
+ char *event_string;
+ if (!get_options()->TestingEnableCellStatsEvent ||
+ !EVENT_IS_INTERESTING(EVENT_CELL_STATS))
+ return 0;
+ cell_stats = tor_malloc(sizeof(cell_stats_t));;
- for (circ = circuit_get_global_list_(); circ; circ = circ->next) {
++ TOR_LIST_FOREACH(circ, circuit_get_global_list(), head) {
+ if (!circ->testing_cell_stats)
+ continue;
+ sum_up_cell_stats_by_command(circ, cell_stats);
+ format_cell_stats(&event_string, circ, cell_stats);
+ send_control_event(EVENT_CELL_STATS, ALL_FORMATS,
+ "650 CELL_STATS %s\r\n", event_string);
+ tor_free(event_string);
+ }
+ tor_free(cell_stats);
+ return 0;
+ }
+
+ /** Tokens in <b>bucket</b> have been refilled: the read bucket was empty
+ * for <b>read_empty_time</b> millis, the write bucket was empty for
+ * <b>write_empty_time</b> millis, and buckets were last refilled
+ * <b>milliseconds_elapsed</b> millis ago. Only emit TB_EMPTY event if
+ * either read or write bucket have been empty before. */
+ int
+ control_event_tb_empty(const char *bucket, uint32_t read_empty_time,
+ uint32_t write_empty_time,
+ int milliseconds_elapsed)
+ {
+ if (get_options()->TestingEnableTbEmptyEvent &&
+ EVENT_IS_INTERESTING(EVENT_TB_EMPTY) &&
+ (read_empty_time > 0 || write_empty_time > 0)) {
+ send_control_event(EVENT_TB_EMPTY, ALL_FORMATS,
+ "650 TB_EMPTY %s READ=%d WRITTEN=%d "
+ "LAST=%d\r\n",
+ bucket, read_empty_time, write_empty_time,
+ milliseconds_elapsed);
+ }
+ return 0;
+ }
+
/** A second or more has elapsed: tell any interested control
* connections how much bandwidth we used. */
int
diff --cc src/or/control.h
index 099782a,1921d97..c8db643
--- a/src/or/control.h
+++ b/src/or/control.h
@@@ -85,72 -92,38 +92,100 @@@ void enable_control_logging(void)
void monitor_owning_controller_process(const char *process_spec);
void control_event_bootstrap(bootstrap_status_t status, int progress);
-void control_event_bootstrap_problem(const char *warn, int reason);
+MOCK_DECL(void, control_event_bootstrap_problem,(const char *warn,
+ int reason));
void control_event_clients_seen(const char *controller_str);
+void control_event_transport_launched(const char *mode,
+ const char *transport_name,
+ tor_addr_t *addr, uint16_t port);
+
+void control_free_all(void);
#ifdef CONTROL_PRIVATE
-/* Used only by control.c and test*.c */
-size_t write_escaped_data(const char *data, size_t len, char **out);
-size_t read_escaped_data(const char *data, size_t len, char **out);
+/* Recognized asynchronous event types. It's okay to expand this list
+ * because it is used both as a list of v0 event types, and as indices
+ * into the bitfield to determine which controllers want which events.
+ */
+#define EVENT_MIN_ 0x0001
+#define EVENT_CIRCUIT_STATUS 0x0001
+#define EVENT_STREAM_STATUS 0x0002
+#define EVENT_OR_CONN_STATUS 0x0003
+#define EVENT_BANDWIDTH_USED 0x0004
+#define EVENT_CIRCUIT_STATUS_MINOR 0x0005
+#define EVENT_NEW_DESC 0x0006
+#define EVENT_DEBUG_MSG 0x0007
+#define EVENT_INFO_MSG 0x0008
+#define EVENT_NOTICE_MSG 0x0009
+#define EVENT_WARN_MSG 0x000A
+#define EVENT_ERR_MSG 0x000B
+#define EVENT_ADDRMAP 0x000C
+/* Exposed above */
+// #define EVENT_AUTHDIR_NEWDESCS 0x000D
+#define EVENT_DESCCHANGED 0x000E
+/* Exposed above */
+// #define EVENT_NS 0x000F
+#define EVENT_STATUS_CLIENT 0x0010
+#define EVENT_STATUS_SERVER 0x0011
+#define EVENT_STATUS_GENERAL 0x0012
+#define EVENT_GUARD 0x0013
+#define EVENT_STREAM_BANDWIDTH_USED 0x0014
+#define EVENT_CLIENTS_SEEN 0x0015
+#define EVENT_NEWCONSENSUS 0x0016
+#define EVENT_BUILDTIMEOUT_SET 0x0017
+#define EVENT_SIGNAL 0x0018
+#define EVENT_CONF_CHANGED 0x0019
++#define EVENT_CONN_BW 0x001A
++#define EVENT_CELL_STATS 0x001B
++#define EVENT_TB_EMPTY 0x001C
++#define EVENT_CIRC_BANDWIDTH_USED 0x001D
+#define EVENT_TRANSPORT_LAUNCHED 0x0020
+#define EVENT_MAX_ 0x0020
+/* If EVENT_MAX_ ever hits 0x0040, we need to make the mask into a
+ * different structure. */
+
+/* Used only by control.c and test.c */
+STATIC size_t write_escaped_data(const char *data, size_t len, char **out);
+STATIC size_t read_escaped_data(const char *data, size_t len, char **out);
+/** Flag for event_format_t. Indicates that we should use the one standard
+ format. (Other formats previous existed, and are now deprecated)
+ */
+#define ALL_FORMATS 1
+/** Bit field of flags to select how to format a controller event. Recognized
+ * flag is ALL_FORMATS. */
+typedef int event_format_t;
+
+#ifdef TOR_UNIT_TESTS
+MOCK_DECL(STATIC void,
+send_control_event_string,(uint16_t event, event_format_t which,
+ const char *msg));
+
+void control_testing_set_global_event_mask(uint64_t mask);
+#endif
+
+ /** Helper structure: temporarily stores cell statistics for a circuit. */
+ typedef struct cell_stats_t {
+ /** Number of cells added in app-ward direction by command. */
+ uint64_t added_cells_appward[CELL_COMMAND_MAX_ + 1];
+ /** Number of cells added in exit-ward direction by command. */
+ uint64_t added_cells_exitward[CELL_COMMAND_MAX_ + 1];
+ /** Number of cells removed in app-ward direction by command. */
+ uint64_t removed_cells_appward[CELL_COMMAND_MAX_ + 1];
+ /** Number of cells removed in exit-ward direction by command. */
+ uint64_t removed_cells_exitward[CELL_COMMAND_MAX_ + 1];
+ /** Total waiting time of cells in app-ward direction by command. */
+ uint64_t total_time_appward[CELL_COMMAND_MAX_ + 1];
+ /** Total waiting time of cells in exit-ward direction by command. */
+ uint64_t total_time_exitward[CELL_COMMAND_MAX_ + 1];
+ } cell_stats_t;
+ void sum_up_cell_stats_by_command(circuit_t *circ,
+ cell_stats_t *cell_stats);
+ void append_cell_stats_by_command(smartlist_t *event_parts,
+ const char *key,
+ const uint64_t *include_if_non_zero,
+ const uint64_t *number_to_include);
+ void format_cell_stats(char **event_string, circuit_t *circ,
+ cell_stats_t *cell_stats);
#endif
#endif
diff --cc src/or/or.h
index 2f0f9eb,6dd3ce4..92c0692
--- a/src/or/or.h
+++ b/src/or/or.h
@@@ -1139,10 -1106,12 +1155,12 @@@ typedef struct insertion_command_queue_
/** A queue of cells on a circuit, waiting to be added to the
* or_connection_t's outbuf. */
typedef struct cell_queue_t {
- packed_cell_t *head; /**< The first cell, or NULL if the queue is empty. */
- packed_cell_t *tail; /**< The last cell, or NULL if the queue is empty. */
+ /** Linked list of packed_cell_t*/
+ TOR_SIMPLEQ_HEAD(cell_simpleq, packed_cell_t) head;
int n; /**< The number of cells in the queue. */
insertion_time_queue_t *insertion_times; /**< Insertion times of cells. */
+ /** Commands of inserted cells. */
+ insertion_command_queue_t *insertion_commands;
} cell_queue_t;
/** Beginning of a RELAY cell payload. */
@@@ -4065,10 -3987,15 +4126,19 @@@ typedef struct
/** Minimum value for the Fast flag threshold on testing networks. */
uint64_t TestingMinFastFlagThreshold;
+ /** Relays in a testing network which should be voted Guard
+ * regardless of uptime and bandwidth. */
+ routerset_t *TestingDirAuthVoteGuard;
+
+ /** Enable CONN_BW events. Only altered on testing networks. */
+ int TestingEnableConnBwEvent;
+
+ /** Enable CELL_STATS events. Only altered on testing networks. */
+ int TestingEnableCellStatsEvent;
+
+ /** Enable TB_EMPTY events. Only altered on testing networks. */
+ int TestingEnableTbEmptyEvent;
+
/** If true, and we have GeoIP data, and we're a bridge, keep a per-country
* count of how many client addresses have contacted us so that we can help
* the bridge authority guess which countries have blocked access to us. */
diff --cc src/or/relay.c
index d12850d,66c24f3..7947ca3
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@@ -2144,14 -2153,68 +2152,68 @@@ cell_queue_append(cell_queue_t *queue,
++queue->n;
}
- /** Append a newly allocated copy of <b>cell</b> to the end of <b>queue</b> */
+ /** Append command of type <b>command</b> in direction to <b>queue</b> for
+ * CELL_STATS event. */
+ static void
+ cell_command_queue_append(cell_queue_t *queue, uint8_t command)
+ {
+ insertion_command_queue_t *ic_queue = queue->insertion_commands;
+ if (!ic_pool)
+ ic_pool = mp_pool_new(sizeof(insertion_command_elem_t), 1024);
+ if (!ic_queue) {
+ ic_queue = tor_malloc_zero(sizeof(insertion_command_queue_t));
+ queue->insertion_commands = ic_queue;
+ }
+ if (ic_queue->last && ic_queue->last->command == command) {
+ ic_queue->last->counter++;
+ } else {
+ insertion_command_elem_t *elem = mp_pool_get(ic_pool);
+ elem->next = NULL;
+ elem->command = command;
+ elem->counter = 1;
+ if (ic_queue->last) {
+ ic_queue->last->next = elem;
+ ic_queue->last = elem;
+ } else {
+ ic_queue->first = ic_queue->last = elem;
+ }
+ }
+ }
+
+ /** Retrieve oldest command from <b>queue</b> and write it to
+ * <b>command</b> for CELL_STATS event. Return 0 for success, -1
+ * otherwise. */
+ static int
+ cell_command_queue_pop(uint8_t *command, cell_queue_t *queue)
+ {
+ int res = -1;
+ insertion_command_queue_t *ic_queue = queue->insertion_commands;
+ if (ic_queue && ic_queue->first) {
+ insertion_command_elem_t *ic_elem = ic_queue->first;
+ ic_elem->counter--;
+ if (ic_elem->counter < 1) {
+ ic_queue->first = ic_elem->next;
+ if (ic_elem == ic_queue->last)
+ ic_queue->last = NULL;
+ mp_pool_release(ic_elem);
+ }
+ *command = ic_elem->command;
+ res = 0;
+ }
+ return res;
+ }
+
+ /** Append a newly allocated copy of <b>cell</b> to the end of the
+ * <b>exitward</b> (or app-ward) <b>queue</b> of <b>circ</b>. */
void
- cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell,
+ cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
+ int exitward, const cell_t *cell,
- int wide_circ_ids)
+ int wide_circ_ids, int use_stats)
{
packed_cell_t *copy = packed_cell_copy(cell, wide_circ_ids);
/* Remember the time when this cell was put in the queue. */
- if (get_options()->CellStatistics && use_stats) {
- if (get_options()->CellStatistics ||
- get_options()->TestingEnableCellStatsEvent) {
++ if ((get_options()->CellStatistics ||
++ get_options()->TestingEnableCellStatsEvent) && use_stats) {
struct timeval now;
uint32_t added;
insertion_time_queue_t *it_queue = queue->insertion_times;
@@@ -2517,13 -2564,9 +2613,14 @@@ append_cell_to_circuit_queue(circuit_t
cell_t *cell, cell_direction_t direction,
streamid_t fromstream)
{
+ or_circuit_t *orcirc = NULL;
cell_queue_t *queue;
int streams_blocked;
+#if 0
+ uint32_t tgt_max_middle_cells, p_len, n_len, tmp, hard_max_middle_cells;
+#endif
+
+ int exitward;
if (circ->marked_for_close)
return;
@@@ -2536,95 -2580,8 +2634,96 @@@
streams_blocked = circ->streams_blocked_on_p_chan;
}
+ /*
+ * Disabling this for now because of a possible guard discovery attack
+ */
+#if 0
+ /* Are we a middle circuit about to exceed ORCIRC_MAX_MIDDLE_CELLS? */
+ if ((circ->n_chan != NULL) && CIRCUIT_IS_ORCIRC(circ)) {
+ orcirc = TO_OR_CIRCUIT(circ);
+ if (orcirc->p_chan) {
+ /* We are a middle circuit if we have both n_chan and p_chan */
+ /* We'll need to know the current preferred maximum */
+ tgt_max_middle_cells = get_max_middle_cells();
+ if (tgt_max_middle_cells > 0) {
+ /* Do we need to initialize middle_max_cells? */
+ if (orcirc->max_middle_cells == 0) {
+ orcirc->max_middle_cells = tgt_max_middle_cells;
+ } else {
+ if (tgt_max_middle_cells > orcirc->max_middle_cells) {
+ /* If we want to increase the cap, we can do so right away */
+ orcirc->max_middle_cells = tgt_max_middle_cells;
+ } else if (tgt_max_middle_cells < orcirc->max_middle_cells) {
+ /*
+ * If we're shrinking the cap, we can't shrink past either queue;
+ * compare tgt_max_middle_cells rather than tgt_max_middle_cells *
+ * ORCIRC_MAX_MIDDLE_KILL_THRESH so the queues don't shrink enough
+ * to generate spurious warnings, either.
+ */
+ n_len = circ->n_chan_cells.n;
+ p_len = orcirc->p_chan_cells.n;
+ tmp = tgt_max_middle_cells;
+ if (tmp < n_len) tmp = n_len;
+ if (tmp < p_len) tmp = p_len;
+ orcirc->max_middle_cells = tmp;
+ }
+ /* else no change */
+ }
+ } else {
+ /* tgt_max_middle_cells == 0 indicates we should disable the cap */
+ orcirc->max_middle_cells = 0;
+ }
+
+ /* Now we know orcirc->max_middle_cells is set correctly */
+ if (orcirc->max_middle_cells > 0) {
+ hard_max_middle_cells =
+ (uint32_t)(((double)orcirc->max_middle_cells) *
+ ORCIRC_MAX_MIDDLE_KILL_THRESH);
+
+ if ((unsigned)queue->n + 1 >= hard_max_middle_cells) {
+ /* Queueing this cell would put queue over the kill theshold */
+ log_warn(LD_CIRC,
+ "Got a cell exceeding the hard cap of %u in the "
+ "%s direction on middle circ ID %u on chan ID "
+ U64_FORMAT "; killing the circuit.",
+ hard_max_middle_cells,
+ (direction == CELL_DIRECTION_OUT) ? "n" : "p",
+ (direction == CELL_DIRECTION_OUT) ?
+ circ->n_circ_id : orcirc->p_circ_id,
+ U64_PRINTF_ARG(
+ (direction == CELL_DIRECTION_OUT) ?
+ circ->n_chan->global_identifier :
+ orcirc->p_chan->global_identifier));
+ circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
+ return;
+ } else if ((unsigned)queue->n + 1 == orcirc->max_middle_cells) {
+ /* Only use ==, not >= for this test so we don't spam the log */
+ log_warn(LD_CIRC,
+ "While trying to queue a cell, reached the soft cap of %u "
+ "in the %s direction on middle circ ID %u "
+ "on chan ID " U64_FORMAT ".",
+ orcirc->max_middle_cells,
+ (direction == CELL_DIRECTION_OUT) ? "n" : "p",
+ (direction == CELL_DIRECTION_OUT) ?
+ circ->n_circ_id : orcirc->p_circ_id,
+ U64_PRINTF_ARG(
+ (direction == CELL_DIRECTION_OUT) ?
+ circ->n_chan->global_identifier :
+ orcirc->p_chan->global_identifier));
+ }
+ }
+ }
+ }
+#endif
+
- cell_queue_append_packed_copy(queue, cell, chan->wide_circ_ids, 1);
+ cell_queue_append_packed_copy(circ, queue, exitward, cell,
- chan->wide_circ_ids);
++ chan->wide_circ_ids, 1);
+
+ if (PREDICT_UNLIKELY(cell_queues_check_size())) {
+ /* We ran the OOM handler */
+ if (circ->marked_for_close)
+ return;
+ }
/* If we have too many cells on the circuit, we should stop reading from
* the edge streams for a while. */
diff --cc src/or/relay.h
index e1b5e38,b545857..20eecfb
--- a/src/or/relay.h
+++ b/src/or/relay.h
@@@ -51,11 -50,11 +51,12 @@@ size_t packed_cell_mem_cost(void)
/* For channeltls.c */
void packed_cell_free(packed_cell_t *cell);
+void cell_queue_init(cell_queue_t *queue);
void cell_queue_clear(cell_queue_t *queue);
void cell_queue_append(cell_queue_t *queue, packed_cell_t *cell);
- void cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell,
+ void cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
+ int exitward, const cell_t *cell,
- int wide_circ_ids);
+ int wide_circ_ids, int use_stats);
void append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
cell_t *cell, cell_direction_t direction,
diff --cc src/test/include.am
index 41e1153,84a7643..5510293
--- a/src/test/include.am
+++ b/src/test/include.am
@@@ -18,22 -14,16 +18,23 @@@ src_test_AM_CPPFLAGS = -DSHARE_DATADIR=
src_test_test_SOURCES = \
src/test/test.c \
src/test/test_addr.c \
+ src/test/test_buffers.c \
src/test/test_cell_formats.c \
+ src/test/test_circuitlist.c \
+ src/test/test_circuitmux.c \
src/test/test_containers.c \
+ src/test/test_controller_events.c \
src/test/test_crypto.c \
+ src/test/test_cell_queue.c \
src/test/test_data.c \
src/test/test_dir.c \
+ src/test/test_extorport.c \
src/test/test_introduce.c \
src/test/test_microdesc.c \
+ src/test/test_options.c \
src/test/test_pt.c \
src/test/test_replay.c \
+ src/test/test_socks.c \
src/test/test_util.c \
src/test/test_config.c \
src/ext/tinytest.c
diff --cc src/test/test.c
index 6b45acf,eec591a..562de48
--- a/src/test/test.c
+++ b/src/test/test.c
@@@ -1614,12 -2133,7 +1614,13 @@@ extern struct testcase_t config_tests[]
extern struct testcase_t introduce_tests[];
extern struct testcase_t replaycache_tests[];
extern struct testcase_t cell_format_tests[];
+extern struct testcase_t circuitlist_tests[];
+extern struct testcase_t circuitmux_tests[];
+extern struct testcase_t cell_queue_tests[];
+extern struct testcase_t options_tests[];
+extern struct testcase_t socks_tests[];
+extern struct testcase_t extorport_tests[];
+ extern struct testcase_t controller_event_tests[];
static struct testgroup_t testgroups[] = {
{ "", test_array },
@@@ -1637,10 -2149,7 +1638,11 @@@
{ "config/", config_tests },
{ "replaycache/", replaycache_tests },
{ "introduce/", introduce_tests },
+ { "circuitlist/", circuitlist_tests },
+ { "circuitmux/", circuitmux_tests },
+ { "options/", options_tests },
+ { "extorport/", extorport_tests },
+ { "control/", controller_event_tests },
END_OF_GROUPS
};
diff --cc src/test/test_cell_queue.c
index cf2d11a,0000000..1eac073
mode 100644,000000..100644
--- a/src/test/test_cell_queue.c
+++ b/src/test/test_cell_queue.c
@@@ -1,146 -1,0 +1,148 @@@
+/* Copyright (c) 2013, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define CIRCUITLIST_PRIVATE
+#define RELAY_PRIVATE
+#include "or.h"
+#include "circuitlist.h"
+#include "relay.h"
+#include "test.h"
+
+static void
+test_cq_manip(void *arg)
+{
+ packed_cell_t *pc1=NULL, *pc2=NULL, *pc3=NULL, *pc4=NULL, *pc_tmp=NULL;
+ cell_queue_t cq;
+ cell_t cell;
+ (void) arg;
+
+ init_cell_pool();
+ cell_queue_init(&cq);
+ tt_int_op(cq.n, ==, 0);
+
+ pc1 = packed_cell_new();
+ pc2 = packed_cell_new();
+ pc3 = packed_cell_new();
+ pc4 = packed_cell_new();
+ tt_assert(pc1 && pc2 && pc3 && pc4);
+
+ tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
+
+ /* Add and remove a singleton. */
+ cell_queue_append(&cq, pc1);
+ tt_int_op(cq.n, ==, 1);
+ tt_ptr_op(pc1, ==, cell_queue_pop(&cq));
+ tt_int_op(cq.n, ==, 0);
+
+ /* Add and remove four items */
+ cell_queue_append(&cq, pc4);
+ cell_queue_append(&cq, pc3);
+ cell_queue_append(&cq, pc2);
+ cell_queue_append(&cq, pc1);
+ tt_int_op(cq.n, ==, 4);
+ tt_ptr_op(pc4, ==, cell_queue_pop(&cq));
+ tt_ptr_op(pc3, ==, cell_queue_pop(&cq));
+ tt_ptr_op(pc2, ==, cell_queue_pop(&cq));
+ tt_ptr_op(pc1, ==, cell_queue_pop(&cq));
+ tt_int_op(cq.n, ==, 0);
+ tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
+
+ /* Try a packed copy (wide, then narrow, which is a bit of a cheat, since a
+ * real cell queue has only one type.) */
+ memset(&cell, 0, sizeof(cell));
+ cell.circ_id = 0x12345678;
+ cell.command = 10;
+ strlcpy((char*)cell.payload, "Lorax ipsum gruvvulus thneed amet, snergelly "
+ "once-ler lerkim, sed do barbaloot tempor gluppitus ut labore et "
+ "truffula magna aliqua.",
+ sizeof(cell.payload));
- cell_queue_append_packed_copy(&cq, &cell, 1 /*wide*/, 0 /*stats*/);
++ cell_queue_append_packed_copy(NULL /*circ*/, &cq, 0 /*exitward*/, &cell,
++ 1 /*wide*/, 0 /*stats*/);
+ cell.circ_id = 0x2013;
- cell_queue_append_packed_copy(&cq, &cell, 0 /*wide*/, 0 /*stats*/);
++ cell_queue_append_packed_copy(NULL /*circ*/, &cq, 0 /*exitward*/, &cell,
++ 0 /*wide*/, 0 /*stats*/);
+ tt_int_op(cq.n, ==, 2);
+
+ pc_tmp = cell_queue_pop(&cq);
+ tt_int_op(cq.n, ==, 1);
+ tt_ptr_op(pc_tmp, !=, NULL);
+ test_mem_op(pc_tmp->body, ==, "\x12\x34\x56\x78\x0a", 5);
+ test_mem_op(pc_tmp->body+5, ==, cell.payload, sizeof(cell.payload));
+ packed_cell_free(pc_tmp);
+
+ pc_tmp = cell_queue_pop(&cq);
+ tt_int_op(cq.n, ==, 0);
+ tt_ptr_op(pc_tmp, !=, NULL);
+ test_mem_op(pc_tmp->body, ==, "\x20\x13\x0a", 3);
+ test_mem_op(pc_tmp->body+3, ==, cell.payload, sizeof(cell.payload));
+ packed_cell_free(pc_tmp);
+ pc_tmp = NULL;
+
+ tt_ptr_op(NULL, ==, cell_queue_pop(&cq));
+
+ /* Now make sure cell_queue_clear works. */
+ cell_queue_append(&cq, pc2);
+ cell_queue_append(&cq, pc1);
+ tt_int_op(cq.n, ==, 2);
+ cell_queue_clear(&cq);
+ pc2 = pc1 = NULL; /* prevent double-free */
+ tt_int_op(cq.n, ==, 0);
+
+ done:
+ packed_cell_free(pc1);
+ packed_cell_free(pc2);
+ packed_cell_free(pc3);
+ packed_cell_free(pc4);
+ packed_cell_free(pc_tmp);
+
+ cell_queue_clear(&cq);
+ free_cell_pool();
+}
+
+static void
+test_circuit_n_cells(void *arg)
+{
+ packed_cell_t *pc1=NULL, *pc2=NULL, *pc3=NULL, *pc4=NULL, *pc5=NULL;
+ origin_circuit_t *origin_c=NULL;
+ or_circuit_t *or_c=NULL;
+
+ (void)arg;
+
+ init_cell_pool();
+
+ pc1 = packed_cell_new();
+ pc2 = packed_cell_new();
+ pc3 = packed_cell_new();
+ pc4 = packed_cell_new();
+ pc5 = packed_cell_new();
+ tt_assert(pc1 && pc2 && pc3 && pc4 && pc5);
+
+ or_c = or_circuit_new(0, NULL);
+ origin_c = origin_circuit_new();
+ origin_c->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
+
+ tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 0);
+ cell_queue_append(&or_c->p_chan_cells, pc1);
+ tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 1);
+ cell_queue_append(&or_c->base_.n_chan_cells, pc2);
+ cell_queue_append(&or_c->base_.n_chan_cells, pc3);
+ tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(or_c)), ==, 3);
+
+ tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c)), ==, 0);
+ cell_queue_append(&origin_c->base_.n_chan_cells, pc4);
+ cell_queue_append(&origin_c->base_.n_chan_cells, pc5);
+ tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c)), ==, 2);
+
+ done:
+ circuit_free(TO_CIRCUIT(or_c));
+ circuit_free(TO_CIRCUIT(origin_c));
+
+ free_cell_pool();
+}
+
+struct testcase_t cell_queue_tests[] = {
+ { "basic", test_cq_manip, TT_FORK, NULL, NULL, },
+ { "circ_n_cells", test_circuit_n_cells, TT_FORK, NULL, NULL },
+ END_OF_TESTCASES
+};
+
1
0
[tor/master] Add some clarity and checks to cell_queue_append_packed_copy
by nickm@torproject.org 31 Oct '13
by nickm@torproject.org 31 Oct '13
31 Oct '13
commit f6fee77375d246d3607b82577464a499685c1ce6
Author: Nick Mathewson <nickm(a)torproject.org>
Date: Wed Oct 30 23:00:52 2013 -0400
Add some clarity and checks to cell_queue_append_packed_copy
It's not cool to have "circ may be NULL if use_stats false, but
otherwise we crash" as an undocumented API constraint. :)
---
src/or/relay.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/or/relay.c b/src/or/relay.c
index 7947ca3..e0b5d26 100644
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@ -2204,7 +2204,9 @@ cell_command_queue_pop(uint8_t *command, cell_queue_t *queue)
}
/** Append a newly allocated copy of <b>cell</b> to the end of the
- * <b>exitward</b> (or app-ward) <b>queue</b> of <b>circ</b>. */
+ * <b>exitward</b> (or app-ward) <b>queue</b> of <b>circ</b>. If
+ * <b>use_stats</b> is true, record statistics about the cell.
+ */
void
cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
int exitward, const cell_t *cell,
@@ -2244,7 +2246,7 @@ cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
}
/* Remember that we added a cell to the queue, and remember the cell
* command. */
- if (get_options()->TestingEnableCellStatsEvent) {
+ if (get_options()->TestingEnableCellStatsEvent && circ) {
testing_cell_stats_entry_t *ent =
tor_malloc_zero(sizeof(testing_cell_stats_entry_t));
ent->command = cell->command;
1
0
commit 8d1f78c556abb570bb80ea84261c954d9746cf33
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Wed Feb 6 14:15:32 2013 +0100
Add new CONN_BW event.
Jointly authored with Rob Jansen <jansen(a)cs.umn.edu>.
---
src/or/connection.c | 24 ++++++++++++++++++++++++
src/or/control.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-
src/or/control.h | 2 ++
src/or/main.c | 3 +++
src/or/or.h | 6 ++++++
5 files changed, 86 insertions(+), 1 deletion(-)
diff --git a/src/or/connection.c b/src/or/connection.c
index 87fa799..81b4991 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -3148,6 +3148,18 @@ connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
else
edge_conn->n_read = UINT32_MAX;
}
+
+ /* In TestingTorNetwork mode, update conn->n_read for OR/DIR/EXIT
+ * connections, checking for overflow. */
+ if (get_options()->TestingTorNetwork &&
+ (conn->type == CONN_TYPE_OR ||
+ conn->type == CONN_TYPE_DIR ||
+ conn->type == CONN_TYPE_EXIT)) {
+ if (PREDICT_LIKELY(UINT32_MAX - conn->n_read > n_read))
+ conn->n_read += (int)n_read;
+ else
+ conn->n_read = UINT32_MAX;
+ }
}
connection_buckets_decrement(conn, approx_time(), n_read, n_written);
@@ -3594,6 +3606,18 @@ connection_handle_write_impl(connection_t *conn, int force)
edge_conn->n_written = UINT32_MAX;
}
+ /* In TestingTorNetwork mode, update conn->n_written for OR/DIR/EXIT
+ * connections, checking for overflow. */
+ if (n_written && get_options()->TestingTorNetwork &&
+ (conn->type == CONN_TYPE_OR ||
+ conn->type == CONN_TYPE_DIR ||
+ conn->type == CONN_TYPE_EXIT)) {
+ if (PREDICT_LIKELY(UINT32_MAX - conn->n_written > n_written))
+ conn->n_written += (int)n_written;
+ else
+ conn->n_written = UINT32_MAX;
+ }
+
connection_buckets_decrement(conn, approx_time(), n_read, n_written);
if (result > 0) {
diff --git a/src/or/control.c b/src/or/control.c
index 0f202c8..0a49c29 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -82,7 +82,8 @@
#define EVENT_BUILDTIMEOUT_SET 0x0017
#define EVENT_SIGNAL 0x0018
#define EVENT_CONF_CHANGED 0x0019
-#define EVENT_MAX_ 0x0019
+#define EVENT_CONN_BW 0x001A
+#define EVENT_MAX_ 0x001A
/* If EVENT_MAX_ ever hits 0x0020, we need to make the mask wider. */
/** Bitfield: The bit 1<<e is set if <b>any</b> open control
@@ -958,6 +959,7 @@ static const struct control_event_t control_event_table[] = {
{ EVENT_BUILDTIMEOUT_SET, "BUILDTIMEOUT_SET" },
{ EVENT_SIGNAL, "SIGNAL" },
{ EVENT_CONF_CHANGED, "CONF_CHANGED"},
+ { EVENT_CONN_BW, "CONN_BW" },
{ 0, NULL },
};
@@ -3906,6 +3908,54 @@ control_event_stream_bandwidth_used(void)
return 0;
}
+/** Print out CONN_BW event for a single OR/DIR/EXIT <b>conn</b> and reset
+ * bandwidth counters. */
+int
+control_event_conn_bandwidth(connection_t *conn)
+{
+ const char *conn_type_str;
+ if (!get_options()->TestingTorNetwork ||
+ !EVENT_IS_INTERESTING(EVENT_CONN_BW))
+ return 0;
+ if (!conn->n_read && !conn->n_written)
+ return 0;
+ switch (conn->type) {
+ case CONN_TYPE_OR:
+ conn_type_str = "OR";
+ break;
+ case CONN_TYPE_DIR:
+ conn_type_str = "DIR";
+ break;
+ case CONN_TYPE_EXIT:
+ conn_type_str = "EXIT";
+ break;
+ default:
+ return 0;
+ }
+ send_control_event(EVENT_CONN_BW, ALL_FORMATS,
+ "650 CONN_BW ID="U64_FORMAT" TYPE=%s "
+ "READ=%lu WRITTEN=%lu\r\n",
+ U64_PRINTF_ARG(conn->global_identifier),
+ conn_type_str,
+ (unsigned long)conn->n_read,
+ (unsigned long)conn->n_written);
+ conn->n_written = conn->n_read = 0;
+ return 0;
+}
+
+/** A second or more has elapsed: tell any interested control
+ * connections how much bandwidth connections have used. */
+int
+control_event_conn_bandwidth_used(void)
+{
+ if (get_options()->TestingTorNetwork &&
+ EVENT_IS_INTERESTING(EVENT_CONN_BW)) {
+ SMARTLIST_FOREACH(get_connection_array(), connection_t *, conn,
+ control_event_conn_bandwidth(conn));
+ }
+ return 0;
+}
+
/** A second or more has elapsed: tell any interested control
* connections how much bandwidth we used. */
int
diff --git a/src/or/control.h b/src/or/control.h
index 61062da..fe5c0ef 100644
--- a/src/or/control.h
+++ b/src/or/control.h
@@ -50,6 +50,8 @@ int control_event_or_conn_status(or_connection_t *conn,
int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
int control_event_stream_bandwidth(edge_connection_t *edge_conn);
int control_event_stream_bandwidth_used(void);
+int control_event_conn_bandwidth(connection_t *conn);
+int control_event_conn_bandwidth_used(void);
void control_event_logmsg(int severity, uint32_t domain, const char *msg);
int control_event_descriptors_changed(smartlist_t *routers);
int control_event_address_mapped(const char *from, const char *to,
diff --git a/src/or/main.c b/src/or/main.c
index fd8b6cf..c145f9d 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -351,6 +351,8 @@ connection_remove(connection_t *conn)
(int)conn->s, conn_type_to_string(conn->type),
smartlist_len(connection_array));
+ control_event_conn_bandwidth(conn);
+
tor_assert(conn->conn_array_index >= 0);
current_index = conn->conn_array_index;
connection_unregister_events(conn); /* This is redundant, but cheap. */
@@ -1638,6 +1640,7 @@ second_elapsed_callback(periodic_timer_t *timer, void *arg)
control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
control_event_stream_bandwidth_used();
+ control_event_conn_bandwidth_used();
if (server_mode(options) &&
!net_is_disabled() &&
diff --git a/src/or/or.h b/src/or/or.h
index 7b6c65f..08a94aa 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -1230,6 +1230,12 @@ typedef struct connection_t {
/** Unique identifier for this connection on this Tor instance. */
uint64_t global_identifier;
+
+ /** Bytes read since last call to control_event_conn_bandwidth_used() */
+ uint32_t n_read;
+
+ /** Bytes written since last call to control_event_conn_bandwidth_used() */
+ uint32_t n_written;
} connection_t;
/** Subtype of connection_t; used for a listener socket. */
1
0
commit e54d664f7bb1205162c1df3495f8ebc30c23d867
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Wed Feb 6 14:04:27 2013 +0100
Add connection ID to ORCONN event.
Jointly authored with Rob Jansen <jansen(a)cs.umn.edu>.
---
src/or/control.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/or/control.c b/src/or/control.c
index 75f7af9..0f202c8 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -3843,8 +3843,9 @@ control_event_or_conn_status(or_connection_t *conn, or_conn_status_event_t tp,
orconn_target_get_name(name, sizeof(name), conn);
send_control_event(EVENT_OR_CONN_STATUS, ALL_FORMATS,
- "650 ORCONN %s %s %s%s%s\r\n",
+ "650 ORCONN %s %s ID="U64_FORMAT" %s%s%s\r\n",
name, status,
+ U64_PRINTF_ARG(conn->base_.global_identifier),
reason ? "REASON=" : "",
orconn_end_reason_to_control_string(reason),
ncircs_buf);
1
0
commit c386d2d6ce4c4f58163acb385c7a5de1da8c5e30
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Wed Feb 6 14:37:38 2013 +0100
Add new CELL_STATS event.
Jointly authored with Rob Jansen <jansen(a)cs.umn.edu>.
---
src/or/control.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
src/or/control.h | 1 +
src/or/main.c | 1 +
src/or/or.h | 36 ++++++++++++
src/or/relay.c | 97 ++++++++++++++++++++++++++++----
src/or/relay.h | 3 +-
6 files changed, 290 insertions(+), 13 deletions(-)
diff --git a/src/or/control.c b/src/or/control.c
index 0a49c29..6625877 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -83,7 +83,8 @@
#define EVENT_SIGNAL 0x0018
#define EVENT_CONF_CHANGED 0x0019
#define EVENT_CONN_BW 0x001A
-#define EVENT_MAX_ 0x001A
+#define EVENT_CELL_STATS 0x001B
+#define EVENT_MAX_ 0x001B
/* If EVENT_MAX_ ever hits 0x0020, we need to make the mask wider. */
/** Bitfield: The bit 1<<e is set if <b>any</b> open control
@@ -960,6 +961,7 @@ static const struct control_event_t control_event_table[] = {
{ EVENT_SIGNAL, "SIGNAL" },
{ EVENT_CONF_CHANGED, "CONF_CHANGED"},
{ EVENT_CONN_BW, "CONN_BW" },
+ { EVENT_CELL_STATS, "CELL_STATS" },
{ 0, NULL },
};
@@ -3956,6 +3958,167 @@ control_event_conn_bandwidth_used(void)
return 0;
}
+extern circuit_t *global_circuitlist;
+
+/** Convert the cell <b>command</b> into a lower-case, human-readable
+ * string. */
+static const char *
+cell_command_to_string(uint8_t command)
+{
+ switch (command) {
+ case CELL_PADDING: return "padding";
+ case CELL_CREATE: return "create";
+ case CELL_CREATED: return "created";
+ case CELL_RELAY: return "relay";
+ case CELL_DESTROY: return "destroy";
+ case CELL_CREATE_FAST: return "create_fast";
+ case CELL_CREATED_FAST: return "created_fast";
+ case CELL_VERSIONS: return "versions";
+ case CELL_NETINFO: return "netinfo";
+ case CELL_RELAY_EARLY: return "relay_early";
+ case CELL_CREATE2: return "create2";
+ case CELL_CREATED2: return "created2";
+ case CELL_VPADDING: return "vpadding";
+ case CELL_CERTS: return "certs";
+ case CELL_AUTH_CHALLENGE: return "auth_challenge";
+ case CELL_AUTHENTICATE: return "authenticate";
+ case CELL_AUTHORIZE: return "authorize";
+ default: return "unrecognized";
+ }
+}
+
+/** Helper: append a cell statistics string to <code>event_parts</code>,
+ * prefixed with <code>key</code>=. Statistics consist of comma-separated
+ * key:value pairs with lower-case command strings as keys and cell
+ * numbers or total waiting times as values. A key:value pair is included
+ * if the entry in <code>include_if_positive</code> is positive, but with
+ * the (possibly zero) entry from <code>number_to_include</code>. If no
+ * entry in <code>include_if_positive</code> is positive, no string will
+ * be added to <code>event_parts</code>. */
+static void
+append_cell_stats_by_command(smartlist_t *event_parts, const char *key,
+ uint64_t *include_if_positive,
+ uint64_t *number_to_include)
+{
+ smartlist_t *key_value_strings = smartlist_new();
+ int i;
+ for (i = 0; i <= CELL_MAX_; i++) {
+ if (include_if_positive[i] > 0) {
+ smartlist_add_asprintf(key_value_strings, "%s:"U64_FORMAT,
+ cell_command_to_string(i),
+ U64_PRINTF_ARG(number_to_include[i]));
+ }
+ }
+ if (key_value_strings->num_used > 0) {
+ char *joined = smartlist_join_strings(key_value_strings, ",", 0, NULL);
+ char *result;
+ tor_asprintf(&result, "%s=%s", key, joined);
+ smartlist_add(event_parts, result);
+ SMARTLIST_FOREACH(key_value_strings, char *, cp, tor_free(cp));
+ tor_free(joined);
+ }
+ smartlist_free(key_value_strings);
+}
+
+/** A second or more has elapsed: tell any interested control connection
+ * how many cells have been processed for a given circuit. */
+int
+control_event_circuit_cell_stats(void)
+{
+ /* These arrays temporarily consume slightly over 6 KiB on the stack
+ * every second, most of which are wasted for the non-existant commands
+ * between CELL_RELAY_EARLY (9) and CELL_VPADDING (128). But nothing
+ * beats the stack when it comes to performance. */
+ uint64_t added_cells_appward[CELL_MAX_ + 1],
+ added_cells_exitward[CELL_MAX_ + 1],
+ removed_cells_appward[CELL_MAX_ + 1],
+ removed_cells_exitward[CELL_MAX_ + 1],
+ total_time_appward[CELL_MAX_ + 1],
+ total_time_exitward[CELL_MAX_ + 1];
+ circuit_t *circ;
+ if (!get_options()->TestingTorNetwork ||
+ !EVENT_IS_INTERESTING(EVENT_CELL_STATS))
+ return 0;
+ for (circ = global_circuitlist; circ; circ = circ->next) {
+ smartlist_t *event_parts;
+ char *event_string;
+
+ if (!circ->testing_cell_stats)
+ continue;
+
+ memset(added_cells_appward, 0, (CELL_MAX_ + 1) * sizeof(uint64_t));
+ memset(added_cells_exitward, 0, (CELL_MAX_ + 1) * sizeof(uint64_t));
+ memset(removed_cells_appward, 0, (CELL_MAX_ + 1) * sizeof(uint64_t));
+ memset(removed_cells_exitward, 0, (CELL_MAX_ + 1) * sizeof(uint64_t));
+ memset(total_time_appward, 0, (CELL_MAX_ + 1) * sizeof(uint64_t));
+ memset(total_time_exitward, 0, (CELL_MAX_ + 1) * sizeof(uint64_t));
+ SMARTLIST_FOREACH_BEGIN(circ->testing_cell_stats,
+ testing_cell_stats_entry_t *, ent) {
+ tor_assert(ent->command <= CELL_MAX_);
+ if (!ent->removed && !ent->exit_ward) {
+ added_cells_appward[ent->command] += 1;
+ } else if (!ent->removed && ent->exit_ward) {
+ added_cells_exitward[ent->command] += 1;
+ } else if (!ent->exit_ward) {
+ removed_cells_appward[ent->command] += 1;
+ total_time_appward[ent->command] += ent->waiting_time * 10;
+ } else {
+ removed_cells_exitward[ent->command] += 1;
+ total_time_exitward[ent->command] += ent->waiting_time * 10;
+ }
+ tor_free(ent);
+ } SMARTLIST_FOREACH_END(ent);
+ smartlist_free(circ->testing_cell_stats);
+ circ->testing_cell_stats = NULL;
+
+ event_parts = smartlist_new();
+ if (CIRCUIT_IS_ORIGIN(circ)) {
+ origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
+ char *id_string;
+ tor_asprintf(&id_string, "ID=%lu",
+ (unsigned long)ocirc->global_identifier);
+ smartlist_add(event_parts, id_string);
+ } else {
+ or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
+ char *queue_string, *conn_string;
+ tor_asprintf(&queue_string, "InboundQueue=%lu",
+ (unsigned long)or_circ->p_circ_id);
+ tor_asprintf(&conn_string, "InboundConn="U64_FORMAT,
+ U64_PRINTF_ARG(or_circ->p_chan->global_identifier));
+ smartlist_add(event_parts, queue_string);
+ smartlist_add(event_parts, conn_string);
+ append_cell_stats_by_command(event_parts, "InboundAdded",
+ added_cells_appward, added_cells_appward);
+ append_cell_stats_by_command(event_parts, "InboundRemoved",
+ removed_cells_appward, removed_cells_appward);
+ append_cell_stats_by_command(event_parts, "InboundTime",
+ removed_cells_appward, total_time_appward);
+ }
+ if (circ->n_chan) {
+ char *queue_string, *conn_string;
+ tor_asprintf(&queue_string, "OutboundQueue=%lu",
+ (unsigned long)circ->n_circ_id);
+ tor_asprintf(&conn_string, "OutboundConn="U64_FORMAT,
+ U64_PRINTF_ARG(circ->n_chan->global_identifier));
+ smartlist_add(event_parts, queue_string);
+ smartlist_add(event_parts, conn_string);
+ append_cell_stats_by_command(event_parts, "OutboundAdded",
+ added_cells_exitward, added_cells_exitward);
+ append_cell_stats_by_command(event_parts, "OutboundRemoved",
+ removed_cells_exitward, removed_cells_exitward);
+ append_cell_stats_by_command(event_parts, "OutboundTime",
+ removed_cells_exitward, total_time_exitward);
+ }
+ event_string = smartlist_join_strings(event_parts, " ", 0, NULL);
+ send_control_event(EVENT_CELL_STATS, ALL_FORMATS,
+ "650 CELL_STATS %s\r\n", event_string);
+ SMARTLIST_FOREACH(event_parts, char *, cp, tor_free(cp));
+ smartlist_free(event_parts);
+ tor_free(event_string);
+ }
+ return 0;
+}
+
/** A second or more has elapsed: tell any interested control
* connections how much bandwidth we used. */
int
diff --git a/src/or/control.h b/src/or/control.h
index fe5c0ef..4d950bf 100644
--- a/src/or/control.h
+++ b/src/or/control.h
@@ -52,6 +52,7 @@ int control_event_stream_bandwidth(edge_connection_t *edge_conn);
int control_event_stream_bandwidth_used(void);
int control_event_conn_bandwidth(connection_t *conn);
int control_event_conn_bandwidth_used(void);
+int control_event_circuit_cell_stats(void);
void control_event_logmsg(int severity, uint32_t domain, const char *msg);
int control_event_descriptors_changed(smartlist_t *routers);
int control_event_address_mapped(const char *from, const char *to,
diff --git a/src/or/main.c b/src/or/main.c
index c145f9d..ae60548 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -1641,6 +1641,7 @@ second_elapsed_callback(periodic_timer_t *timer, void *arg)
control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
control_event_stream_bandwidth_used();
control_event_conn_bandwidth_used();
+ control_event_circuit_cell_stats();
if (server_mode(options) &&
!net_is_disabled() &&
diff --git a/src/or/or.h b/src/or/or.h
index 08a94aa..22e4b96 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -839,6 +839,7 @@ typedef enum {
#define CELL_AUTH_CHALLENGE 130
#define CELL_AUTHENTICATE 131
#define CELL_AUTHORIZE 132
+#define CELL_MAX_ 132
/** How long to test reachability before complaining to the user. */
#define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60)
@@ -1087,6 +1088,21 @@ typedef struct insertion_time_queue_t {
struct insertion_time_elem_t *last; /**< Last element in queue. */
} insertion_time_queue_t;
+/** Number of cells with the same command consecutively added to a circuit
+ * queue; used for cell statistics only in TestingTorNetwork mode. */
+typedef struct insertion_command_elem_t {
+ struct insertion_command_elem_t *next; /**< Next element in queue. */
+ /** Which command did these consecutively added cells have? */
+ uint8_t command;
+ unsigned counter; /**< How many cells were inserted? */
+} insertion_command_elem_t;
+
+/** Queue of insertion commands. */
+typedef struct insertion_command_queue_t {
+ struct insertion_command_elem_t *first; /**< First element in queue. */
+ struct insertion_command_elem_t *last; /**< Last element in queue. */
+} insertion_command_queue_t;
+
/** A queue of cells on a circuit, waiting to be added to the
* or_connection_t's outbuf. */
typedef struct cell_queue_t {
@@ -1094,6 +1110,8 @@ typedef struct cell_queue_t {
packed_cell_t *tail; /**< The last cell, or NULL if the queue is empty. */
int n; /**< The number of cells in the queue. */
insertion_time_queue_t *insertion_times; /**< Insertion times of cells. */
+ /** Commands of inserted cells. */
+ insertion_command_queue_t *insertion_commands;
} cell_queue_t;
/** Beginning of a RELAY cell payload. */
@@ -2730,6 +2748,19 @@ typedef struct {
struct create_cell_t;
+/** Entry in the cell stats list of a circuit; used only when
+ * TestingTorNetwork is set. */
+typedef struct testing_cell_stats_entry_t {
+ uint8_t command; /**< cell command number. */
+ /** Waiting time in centiseconds if this event is for a removed cell,
+ * or 0 if this event is for adding a cell to the queue. 22 bits can
+ * store more than 11 hours, enough to assume that a circuit with this
+ * delay would long have been closed. */
+ unsigned int waiting_time:22;
+ unsigned int removed:1; /**< 0 for added to, 1 for removed from queue. */
+ unsigned int exit_ward:1; /**< 0 for app-ward, 1 for exit-ward. */
+} testing_cell_stats_entry_t;
+
/**
* A circuit is a path over the onion routing
* network. Applications can connect to one end of the circuit, and can
@@ -2855,6 +2886,11 @@ typedef struct circuit_t {
* cells to n_conn. NULL if we have no cells pending, or if we're not
* linked to an OR connection. */
struct circuit_t *prev_active_on_n_chan;
+
+ /** Various statistics about cells being added to or removed from this
+ * circuit's queues; used only when TestingTorNetwork is set and cleared
+ * after being sent to control port. */
+ smartlist_t *testing_cell_stats;
} circuit_t;
/** Largest number of relay_early cells that we can send on a given
diff --git a/src/or/relay.c b/src/or/relay.c
index 52ff32f..02b3b1c 100644
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@ -2045,6 +2045,10 @@ static mp_pool_t *cell_pool = NULL;
* statistics. */
static mp_pool_t *it_pool = NULL;
+/** Memory pool to allocate insertion_command_elem_t objects used for cell
+ * statistics in TestingTorNetwork mode. */
+static mp_pool_t *ic_pool = NULL;
+
/** Allocate structures to hold cells. */
void
init_cell_pool(void)
@@ -2053,8 +2057,8 @@ init_cell_pool(void)
cell_pool = mp_pool_new(sizeof(packed_cell_t), 128*1024);
}
-/** Free all storage used to hold cells (and insertion times if we measure
- * cell statistics). */
+/** Free all storage used to hold cells (and insertion times/commands if we
+ * measure cell statistics and/or are in TestingTorNetwork mode). */
void
free_cell_pool(void)
{
@@ -2067,6 +2071,10 @@ free_cell_pool(void)
mp_pool_destroy(it_pool);
it_pool = NULL;
}
+ if (ic_pool) {
+ mp_pool_destroy(ic_pool);
+ ic_pool = NULL;
+ }
}
/** Free excess storage in cell pool. */
@@ -2145,14 +2153,16 @@ cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
++queue->n;
}
-/** Append a newly allocated copy of <b>cell</b> to the end of <b>queue</b> */
+/** Append a newly allocated copy of <b>cell</b> to the end of the
+ * <b>exit_ward</b> (or app-ward) <b>queue</b> of <b>circ</b>. */
void
-cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell,
+cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
+ int exit_ward, const cell_t *cell,
int wide_circ_ids)
{
packed_cell_t *copy = packed_cell_copy(cell, wide_circ_ids);
/* Remember the time when this cell was put in the queue. */
- if (get_options()->CellStatistics) {
+ if (get_options()->CellStatistics || get_options()->TestingTorNetwork) {
struct timeval now;
uint32_t added;
insertion_time_queue_t *it_queue = queue->insertion_times;
@@ -2181,6 +2191,38 @@ cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell,
}
}
}
+ /* Remember that we added a cell to the queue, and remember the cell
+ * command. */
+ if (get_options()->TestingTorNetwork) {
+ insertion_command_queue_t *ic_queue = queue->insertion_commands;
+ testing_cell_stats_entry_t *ent =
+ tor_malloc_zero(sizeof(testing_cell_stats_entry_t));
+ ent->command = cell->command;
+ ent->exit_ward = exit_ward;
+ if (!circ->testing_cell_stats)
+ circ->testing_cell_stats = smartlist_new();
+ smartlist_add(circ->testing_cell_stats, ent);
+ if (!ic_pool)
+ ic_pool = mp_pool_new(sizeof(insertion_command_elem_t), 1024);
+ if (!ic_queue) {
+ ic_queue = tor_malloc_zero(sizeof(insertion_command_queue_t));
+ queue->insertion_commands = ic_queue;
+ }
+ if (ic_queue->last && ic_queue->last->command == cell->command) {
+ ic_queue->last->counter++;
+ } else {
+ insertion_command_elem_t *elem = mp_pool_get(ic_pool);
+ elem->next = NULL;
+ elem->command = cell->command;
+ elem->counter = 1;
+ if (ic_queue->last) {
+ ic_queue->last->next = elem;
+ ic_queue->last = elem;
+ } else {
+ ic_queue->first = ic_queue->last = elem;
+ }
+ }
+ }
cell_queue_append(queue, copy);
}
@@ -2386,7 +2428,8 @@ channel_flush_from_first_active_circuit(channel_t *chan, int max)
cell = cell_queue_pop(queue);
/* Calculate the exact time that this cell has spent in the queue. */
- if (get_options()->CellStatistics && !CIRCUIT_IS_ORIGIN(circ)) {
+ if (get_options()->CellStatistics ||
+ get_options()->TestingTorNetwork) {
struct timeval tvnow;
uint32_t flushed;
uint32_t cell_waiting_time;
@@ -2400,7 +2443,6 @@ channel_flush_from_first_active_circuit(channel_t *chan, int max)
"recently enabled.");
} else {
insertion_time_elem_t *elem = it_queue->first;
- or_circ = TO_OR_CIRCUIT(circ);
cell_waiting_time =
(uint32_t)((flushed * 10L + SECONDS_IN_A_DAY * 1000L -
elem->insertion_time * 10L) %
@@ -2413,8 +2455,38 @@ channel_flush_from_first_active_circuit(channel_t *chan, int max)
it_queue->last = NULL;
mp_pool_release(elem);
}
- or_circ->total_cell_waiting_time += cell_waiting_time;
- or_circ->processed_cells++;
+ if (get_options()->CellStatistics && !CIRCUIT_IS_ORIGIN(circ)) {
+ or_circ = TO_OR_CIRCUIT(circ);
+ or_circ->total_cell_waiting_time += cell_waiting_time;
+ or_circ->processed_cells++;
+ }
+ if (get_options()->TestingTorNetwork) {
+ insertion_command_queue_t *ic_queue = queue->insertion_commands;
+ if (!ic_queue || !ic_queue->first) {
+ log_info(LD_BUG, "Cannot determine command of cell, which "
+ "is a bug, because TestingTorNetwork cannot "
+ "be enabled while running.");
+ } else {
+ testing_cell_stats_entry_t *ent =
+ tor_malloc_zero(sizeof(testing_cell_stats_entry_t));
+ insertion_command_elem_t *ic_elem = ic_queue->first;
+ ent->command = ic_elem->command;
+ ic_elem->counter--;
+ if (ic_elem->counter < 1) {
+ ic_queue->first = ic_elem->next;
+ if (ic_elem == ic_queue->last)
+ ic_queue->last = NULL;
+ mp_pool_release(ic_elem);
+ }
+ ent->waiting_time = (unsigned int)cell_waiting_time / 10;
+ ent->removed = 1;
+ if (circ->n_chan == chan)
+ ent->exit_ward = 1;
+ if (!circ->testing_cell_stats)
+ circ->testing_cell_stats = smartlist_new();
+ smartlist_add(circ->testing_cell_stats, ent);
+ }
+ }
}
}
@@ -2470,10 +2542,12 @@ append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
{
cell_queue_t *queue;
int streams_blocked;
+ int exit_ward;
if (circ->marked_for_close)
return;
- if (direction == CELL_DIRECTION_OUT) {
+ exit_ward = (direction == CELL_DIRECTION_OUT);
+ if (exit_ward) {
queue = &circ->n_chan_cells;
streams_blocked = circ->streams_blocked_on_n_chan;
} else {
@@ -2482,7 +2556,8 @@ append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
streams_blocked = circ->streams_blocked_on_p_chan;
}
- cell_queue_append_packed_copy(queue, cell, chan->wide_circ_ids);
+ cell_queue_append_packed_copy(circ, queue, exit_ward, cell,
+ chan->wide_circ_ids);
/* If we have too many cells on the circuit, we should stop reading from
* the edge streams for a while. */
diff --git a/src/or/relay.h b/src/or/relay.h
index 229fb4f..e3b392c 100644
--- a/src/or/relay.h
+++ b/src/or/relay.h
@@ -52,7 +52,8 @@ void packed_cell_free(packed_cell_t *cell);
void cell_queue_clear(cell_queue_t *queue);
void cell_queue_append(cell_queue_t *queue, packed_cell_t *cell);
-void cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell,
+void cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
+ int exit_ward, const cell_t *cell,
int wide_circ_ids);
void append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
1
0
commit dd5ce2157d8c47ffa3686b0579813e7b1aae8069
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Wed Feb 6 14:26:11 2013 +0100
Add new TB_EMPTY event.
Jointly authored with Rob Jansen <jansen(a)cs.umn.edu>.
---
src/or/connection.c | 64 ++++++++++++++++++++++++++++++++
src/or/control.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++-
src/or/control.h | 12 ++++++
src/or/or.h | 6 +++
4 files changed, 183 insertions(+), 1 deletion(-)
diff --git a/src/or/connection.c b/src/or/connection.c
index 81b4991..dfcd1ab 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -2541,6 +2541,27 @@ record_num_bytes_transferred(connection_t *conn,
#endif
#ifndef USE_BUFFEREVENTS
+/** Last emptied global or relay buckets in msec since midnight; only used
+ * in TestingTorNetwork mode. */
+static uint32_t global_relayed_read_emptied = 0,
+ global_relayed_write_emptied = 0,
+ global_read_emptied = 0,
+ global_write_emptied = 0;
+
+/** Check if a bucket has just run out of tokens, and if so, note the
+ * timestamp for TB_EMPTY events; only used in TestingTorNetwork mode. */
+static void
+connection_buckets_empty_ts(uint32_t *timestamp_var, int tokens_before,
+ size_t tokens_removed)
+{
+ if (tokens_before > 0 && tokens_before - (int)tokens_removed <= 0) {
+ struct timeval tvnow;
+ tor_gettimeofday_cached(&tvnow);
+ *timestamp_var = (uint32_t)(((tvnow.tv_sec % 86400L) * 1000L) +
+ ((uint32_t)tvnow.tv_usec / (uint32_t)1000L));
+ }
+}
+
/** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
* onto <b>conn</b>. Decrement buckets appropriately. */
static void
@@ -2563,6 +2584,28 @@ connection_buckets_decrement(connection_t *conn, time_t now,
if (!connection_is_rate_limited(conn))
return; /* local IPs are free */
+ /* If one or more of our token buckets ran dry just now, note the
+ * timestamp for TB_EMPTY events. */
+ if (get_options()->TestingTorNetwork) {
+ if (connection_counts_as_relayed_traffic(conn, now)) {
+ connection_buckets_empty_ts(&global_relayed_read_emptied,
+ global_relayed_read_bucket, num_read);
+ connection_buckets_empty_ts(&global_relayed_write_emptied,
+ global_relayed_write_bucket, num_written);
+ }
+ connection_buckets_empty_ts(&global_read_emptied, global_read_bucket,
+ num_read);
+ connection_buckets_empty_ts(&global_write_emptied, global_write_bucket,
+ num_written);
+ if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
+ or_connection_t *or_conn = TO_OR_CONN(conn);
+ connection_buckets_empty_ts(&or_conn->read_emptied_time,
+ or_conn->read_bucket, num_read);
+ connection_buckets_empty_ts(&or_conn->write_emptied_time,
+ or_conn->write_bucket, num_written);
+ }
+ }
+
if (connection_counts_as_relayed_traffic(conn, now)) {
global_relayed_read_bucket -= (int)num_read;
global_relayed_write_bucket -= (int)num_written;
@@ -2677,6 +2720,11 @@ connection_bucket_refill(int milliseconds_elapsed, time_t now)
smartlist_t *conns = get_connection_array();
int bandwidthrate, bandwidthburst, relayrate, relayburst;
+ int prev_global_read = global_read_bucket;
+ int prev_global_write = global_write_bucket;
+ int prev_relay_read = global_relayed_read_bucket;
+ int prev_relay_write = global_relayed_write_bucket;
+
bandwidthrate = (int)options->BandwidthRate;
bandwidthburst = (int)options->BandwidthBurst;
@@ -2711,12 +2759,25 @@ connection_bucket_refill(int milliseconds_elapsed, time_t now)
milliseconds_elapsed,
"global_relayed_write_bucket");
+ control_event_refill_global(global_read_bucket, prev_global_read,
+ global_read_emptied, global_write_bucket,
+ prev_global_write, global_write_emptied,
+ global_relayed_read_bucket, prev_relay_read,
+ global_relayed_read_emptied,
+ global_relayed_write_bucket, prev_relay_write,
+ global_relayed_write_emptied,
+ milliseconds_elapsed);
+
/* refill the per-connection buckets */
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
if (connection_speaks_cells(conn)) {
or_connection_t *or_conn = TO_OR_CONN(conn);
int orbandwidthrate = or_conn->bandwidthrate;
int orbandwidthburst = or_conn->bandwidthburst;
+
+ int prev_conn_read = or_conn->read_bucket;
+ int prev_conn_write = or_conn->write_bucket;
+
if (connection_bucket_should_increase(or_conn->read_bucket, or_conn)) {
connection_bucket_refill_helper(&or_conn->read_bucket,
orbandwidthrate,
@@ -2731,6 +2792,9 @@ connection_bucket_refill(int milliseconds_elapsed, time_t now)
milliseconds_elapsed,
"or_conn->write_bucket");
}
+
+ control_event_refill_conn(or_conn, prev_conn_read, prev_conn_write,
+ (uint32_t)milliseconds_elapsed);
}
if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
diff --git a/src/or/control.c b/src/or/control.c
index 6625877..47d08c2 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -84,7 +84,8 @@
#define EVENT_CONF_CHANGED 0x0019
#define EVENT_CONN_BW 0x001A
#define EVENT_CELL_STATS 0x001B
-#define EVENT_MAX_ 0x001B
+#define EVENT_TB_EMPTY 0x001C
+#define EVENT_MAX_ 0x001C
/* If EVENT_MAX_ ever hits 0x0020, we need to make the mask wider. */
/** Bitfield: The bit 1<<e is set if <b>any</b> open control
@@ -962,6 +963,7 @@ static const struct control_event_t control_event_table[] = {
{ EVENT_CONF_CHANGED, "CONF_CHANGED"},
{ EVENT_CONN_BW, "CONN_BW" },
{ EVENT_CELL_STATS, "CELL_STATS" },
+ { EVENT_TB_EMPTY, "TB_EMPTY" },
{ 0, NULL },
};
@@ -4119,6 +4121,104 @@ control_event_circuit_cell_stats(void)
return 0;
}
+/** Helper: return the time in millis that a given bucket was empty,
+ * capped at the time in millis since last refilling that bucket. Return
+ * 0 if the bucket was not empty during the last refill period. */
+static uint32_t
+bucket_millis_empty(int prev_tokens, uint32_t last_empty_time,
+ uint32_t milliseconds_elapsed)
+{
+ uint32_t result = 0, refilled;
+ if (prev_tokens <= 0) {
+ struct timeval tvnow;
+ tor_gettimeofday_cached(&tvnow);
+ refilled = (uint32_t)((tvnow.tv_sec % 86400L) * 1000L +
+ (uint32_t)tvnow.tv_usec / (uint32_t)1000L);
+ result = (uint32_t)((refilled + 86400L * 1000L - last_empty_time) %
+ (86400L * 1000L));
+ if (result > milliseconds_elapsed)
+ result = milliseconds_elapsed;
+ }
+ return result;
+}
+
+/** Token buckets have been refilled: tell any interested control
+ * connections how global and relay token buckets have changed. */
+int
+control_event_refill_global(int global_read, int prev_global_read,
+ uint32_t global_read_emptied_time,
+ int global_write, int prev_global_write,
+ uint32_t global_write_emptied_time,
+ int relay_read, int prev_relay_read,
+ uint32_t relay_read_emptied_time,
+ int relay_write, int prev_relay_write,
+ uint32_t relay_write_emptied_time,
+ uint32_t milliseconds_elapsed)
+{
+ uint32_t global_read_empty_time, global_write_empty_time,
+ relay_read_empty_time, relay_write_empty_time;
+ if (!get_options()->TestingTorNetwork ||
+ !EVENT_IS_INTERESTING(EVENT_TB_EMPTY))
+ return 0;
+ if (prev_global_read == global_read &&
+ prev_global_write == global_write &&
+ prev_relay_read == relay_read &&
+ prev_relay_write == relay_write)
+ return 0;
+ if (prev_global_read <= 0 && prev_global_write <= 0) {
+ global_read_empty_time = bucket_millis_empty(prev_global_read,
+ global_read_emptied_time, milliseconds_elapsed);
+ global_write_empty_time = bucket_millis_empty(prev_global_write,
+ global_write_emptied_time, milliseconds_elapsed);
+ send_control_event(EVENT_TB_EMPTY, ALL_FORMATS,
+ "650 TB_EMPTY GLOBAL READ=%d WRITTEN=%d "
+ "LAST=%d\r\n",
+ global_read_empty_time, global_write_empty_time,
+ milliseconds_elapsed);
+ }
+ if (prev_relay_read <= 0 && prev_relay_write <= 0) {
+ relay_read_empty_time = bucket_millis_empty(prev_relay_read,
+ relay_read_emptied_time, milliseconds_elapsed);
+ relay_write_empty_time = bucket_millis_empty(prev_relay_write,
+ relay_write_emptied_time, milliseconds_elapsed);
+ send_control_event(EVENT_TB_EMPTY, ALL_FORMATS,
+ "650 TB_EMPTY RELAY READ=%d WRITTEN=%d "
+ "LAST=%d\r\n",
+ relay_read_empty_time, relay_write_empty_time,
+ milliseconds_elapsed);
+ }
+ return 0;
+}
+
+/** Token buckets of a connection have been refilled: tell any interested
+ * control connections how per-connection token buckets have changed. */
+int
+control_event_refill_conn(or_connection_t *or_conn,
+ int prev_read, int prev_write,
+ uint32_t milliseconds_elapsed)
+{
+ uint32_t read_bucket_empty_time, write_bucket_empty_time;
+ if (!get_options()->TestingTorNetwork ||
+ !EVENT_IS_INTERESTING(EVENT_TB_EMPTY))
+ return 0;
+ if (prev_read == or_conn->read_bucket &&
+ prev_write == or_conn->write_bucket)
+ return 0;
+ if (prev_read <= 0 || prev_write <= 0) {
+ read_bucket_empty_time = bucket_millis_empty(prev_read,
+ or_conn->read_emptied_time, milliseconds_elapsed);
+ write_bucket_empty_time = bucket_millis_empty(prev_write,
+ or_conn->write_emptied_time, milliseconds_elapsed);
+ send_control_event(EVENT_TB_EMPTY, ALL_FORMATS,
+ "650 TB_EMPTY ORCONN ID="U64_FORMAT" READ=%d "
+ "WRITTEN=%d LAST=%d\r\n",
+ U64_PRINTF_ARG(or_conn->base_.global_identifier),
+ read_bucket_empty_time, write_bucket_empty_time,
+ milliseconds_elapsed);
+ }
+ return 0;
+}
+
/** A second or more has elapsed: tell any interested control
* connections how much bandwidth we used. */
int
diff --git a/src/or/control.h b/src/or/control.h
index 4d950bf..2977159 100644
--- a/src/or/control.h
+++ b/src/or/control.h
@@ -53,6 +53,18 @@ int control_event_stream_bandwidth_used(void);
int control_event_conn_bandwidth(connection_t *conn);
int control_event_conn_bandwidth_used(void);
int control_event_circuit_cell_stats(void);
+int control_event_refill_global(int global_read, int prev_global_read,
+ uint32_t global_read_emptied,
+ int global_write, int prev_global_write,
+ uint32_t global_write_emptied,
+ int relay_read, int prev_relay_read,
+ uint32_t relay_read_emptied,
+ int relay_write, int prev_relay_write,
+ uint32_t relay_write_emptied,
+ uint32_t milliseconds_elapsed);
+int control_event_refill_conn(or_connection_t *or_conn,
+ int prev_read, int prev_write,
+ uint32_t milliseconds_elapsed);
void control_event_logmsg(int severity, uint32_t domain, const char *msg);
int control_event_descriptors_changed(smartlist_t *routers);
int control_event_address_mapped(const char *from, const char *to,
diff --git a/src/or/or.h b/src/or/or.h
index 22e4b96..c2be282 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -1477,6 +1477,12 @@ typedef struct or_connection_t {
struct or_connection_t *next_with_same_id; /**< Next connection with same
* identity digest as this one. */
+ /** Last emptied read token bucket in msec since midnight; only used in
+ * TestingTorNetwork mode. */
+ uint32_t read_emptied_time;
+ /** Last emptied write token bucket in msec since midnight; only used in
+ * TestingTorNetwork mode. */
+ uint32_t write_emptied_time;
} or_connection_t;
/** Subtype of connection_t for an "edge connection" -- that is, an entry (ap)
1
0
commit 2925e2fe786dfd9a27c2dff503996712cd180de4
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Sun Feb 24 13:32:57 2013 +0100
Add new CIRC_BW event.
---
src/or/connection.c | 23 +++++++++++++++++++-
src/or/control.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++-----
src/or/control.h | 1 +
src/or/main.c | 1 +
src/or/or.h | 9 ++++++++
5 files changed, 86 insertions(+), 6 deletions(-)
diff --git a/src/or/connection.c b/src/or/connection.c
index dfcd1ab..a00351a 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -3203,14 +3203,25 @@ connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
/* change *max_to_read */
*max_to_read = at_most - n_read;
- /* Update edge_conn->n_read */
+ /* Update edge_conn->n_read and ocirc->n_read */
if (conn->type == CONN_TYPE_AP) {
edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
+ circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
+ origin_circuit_t *ocirc;
+
/* Check for overflow: */
if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_read > n_read))
edge_conn->n_read += (int)n_read;
else
edge_conn->n_read = UINT32_MAX;
+
+ if (circ && CIRCUIT_IS_ORIGIN(circ)) {
+ ocirc = TO_ORIGIN_CIRCUIT(circ);
+ if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_read > n_read))
+ ocirc->n_read += (int)n_read;
+ else
+ ocirc->n_read = UINT32_MAX;
+ }
}
/* In TestingTorNetwork mode, update conn->n_read for OR/DIR/EXIT
@@ -3662,12 +3673,22 @@ connection_handle_write_impl(connection_t *conn, int force)
if (n_written && conn->type == CONN_TYPE_AP) {
edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
+ circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
+ origin_circuit_t *ocirc;
/* Check for overflow: */
if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_written > n_written))
edge_conn->n_written += (int)n_written;
else
edge_conn->n_written = UINT32_MAX;
+
+ if (circ && CIRCUIT_IS_ORIGIN(circ)) {
+ ocirc = TO_ORIGIN_CIRCUIT(circ);
+ if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_written > n_written))
+ ocirc->n_written += (int)n_written;
+ else
+ ocirc->n_written = UINT32_MAX;
+ }
}
/* In TestingTorNetwork mode, update conn->n_written for OR/DIR/EXIT
diff --git a/src/or/control.c b/src/or/control.c
index 47d08c2..d3b968c 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -46,6 +46,8 @@
#include <sys/resource.h>
#endif
+extern circuit_t *global_circuitlist; /* from circuitlist.c */
+
#include "procmon.h"
/** Yield true iff <b>s</b> is the state of a control_connection_t that has
@@ -85,7 +87,8 @@
#define EVENT_CONN_BW 0x001A
#define EVENT_CELL_STATS 0x001B
#define EVENT_TB_EMPTY 0x001C
-#define EVENT_MAX_ 0x001C
+#define EVENT_CIRC_BANDWIDTH_USED 0x001D
+#define EVENT_MAX_ 0x001D
/* If EVENT_MAX_ ever hits 0x0020, we need to make the mask wider. */
/** Bitfield: The bit 1<<e is set if <b>any</b> open control
@@ -260,8 +263,8 @@ control_update_global_event_mask(void)
* we want to hear...*/
control_adjust_event_log_severity();
- /* ...then, if we've started logging stream bw, clear the appropriate
- * fields. */
+ /* ...then, if we've started logging stream or circ bw, clear the
+ * appropriate fields. */
if (! (old_mask & EVENT_STREAM_BANDWIDTH_USED) &&
(new_mask & EVENT_STREAM_BANDWIDTH_USED)) {
SMARTLIST_FOREACH(conns, connection_t *, conn,
@@ -272,6 +275,17 @@ control_update_global_event_mask(void)
}
});
}
+ if (! (old_mask & EVENT_CIRC_BANDWIDTH_USED) &&
+ (new_mask & EVENT_CIRC_BANDWIDTH_USED)) {
+ circuit_t *circ;
+ origin_circuit_t *ocirc;
+ for (circ = global_circuitlist; circ; circ = circ->next) {
+ if (!CIRCUIT_IS_ORIGIN(circ))
+ continue;
+ ocirc = TO_ORIGIN_CIRCUIT(circ);
+ ocirc->n_written = ocirc->n_read = 0;
+ }
+ }
}
/** Adjust the log severities that result in control_event_logmsg being called
@@ -964,6 +978,7 @@ static const struct control_event_t control_event_table[] = {
{ EVENT_CONN_BW, "CONN_BW" },
{ EVENT_CELL_STATS, "CELL_STATS" },
{ EVENT_TB_EMPTY, "TB_EMPTY" },
+ { EVENT_CIRC_BANDWIDTH_USED, "CIRC_BW" },
{ 0, NULL },
};
@@ -3865,6 +3880,8 @@ control_event_or_conn_status(or_connection_t *conn, or_conn_status_event_t tp,
int
control_event_stream_bandwidth(edge_connection_t *edge_conn)
{
+ circuit_t *circ;
+ origin_circuit_t *ocirc;
if (EVENT_IS_INTERESTING(EVENT_STREAM_BANDWIDTH_USED)) {
if (!edge_conn->n_read && !edge_conn->n_written)
return 0;
@@ -3875,6 +3892,12 @@ control_event_stream_bandwidth(edge_connection_t *edge_conn)
(unsigned long)edge_conn->n_read,
(unsigned long)edge_conn->n_written);
+ circ = circuit_get_by_edge_conn(edge_conn);
+ if (circ && CIRCUIT_IS_ORIGIN(circ)) {
+ ocirc = TO_ORIGIN_CIRCUIT(circ);
+ ocirc->n_read += edge_conn->n_read;
+ ocirc->n_written += edge_conn->n_written;
+ }
edge_conn->n_written = edge_conn->n_read = 0;
}
@@ -3912,6 +3935,33 @@ control_event_stream_bandwidth_used(void)
return 0;
}
+/** A second or more has elapsed: tell any interested control connections
+ * how much bandwidth origin circuits have used. */
+int
+control_event_circ_bandwidth_used(void)
+{
+ circuit_t *circ;
+ origin_circuit_t *ocirc;
+ if (!EVENT_IS_INTERESTING(EVENT_CIRC_BANDWIDTH_USED))
+ return 0;
+
+ for (circ = global_circuitlist; circ; circ = circ->next) {
+ if (!CIRCUIT_IS_ORIGIN(circ))
+ continue;
+ ocirc = TO_ORIGIN_CIRCUIT(circ);
+ if (!ocirc->n_read && !ocirc->n_written)
+ continue;
+ send_control_event(EVENT_CIRC_BANDWIDTH_USED, ALL_FORMATS,
+ "650 CIRC_BW ID=%d READ=%lu WRITTEN=%lu\r\n",
+ ocirc->global_identifier,
+ (unsigned long)ocirc->n_read,
+ (unsigned long)ocirc->n_written);
+ ocirc->n_written = ocirc->n_read = 0;
+ }
+
+ return 0;
+}
+
/** Print out CONN_BW event for a single OR/DIR/EXIT <b>conn</b> and reset
* bandwidth counters. */
int
@@ -3960,8 +4010,6 @@ control_event_conn_bandwidth_used(void)
return 0;
}
-extern circuit_t *global_circuitlist;
-
/** Convert the cell <b>command</b> into a lower-case, human-readable
* string. */
static const char *
diff --git a/src/or/control.h b/src/or/control.h
index 2977159..06a3849 100644
--- a/src/or/control.h
+++ b/src/or/control.h
@@ -50,6 +50,7 @@ int control_event_or_conn_status(or_connection_t *conn,
int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
int control_event_stream_bandwidth(edge_connection_t *edge_conn);
int control_event_stream_bandwidth_used(void);
+int control_event_circ_bandwidth_used(void);
int control_event_conn_bandwidth(connection_t *conn);
int control_event_conn_bandwidth_used(void);
int control_event_circuit_cell_stats(void);
diff --git a/src/or/main.c b/src/or/main.c
index ae60548..d381677 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -1641,6 +1641,7 @@ second_elapsed_callback(periodic_timer_t *timer, void *arg)
control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
control_event_stream_bandwidth_used();
control_event_conn_bandwidth_used();
+ control_event_circ_bandwidth_used();
control_event_circuit_cell_stats();
if (server_mode(options) &&
diff --git a/src/or/or.h b/src/or/or.h
index c2be282..d41034c 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -2967,6 +2967,15 @@ typedef struct origin_circuit_t {
/** Linked list of AP streams (or EXIT streams if hidden service)
* associated with this circuit. */
edge_connection_t *p_streams;
+
+ /** Bytes read from any attached stream since last call to
+ * control_event_circ_bandwidth_used() */
+ uint32_t n_read;
+
+ /** Bytes written to any attached stream since last call to
+ * control_event_circ_bandwidth_used() */
+ uint32_t n_written;
+
/** Build state for this circuit. It includes the intended path
* length, the chosen exit router, rendezvous information, etc.
*/
1
0
31 Oct '13
commit a84fae789206db4e27486e693488328da7b3474a
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Fri May 24 11:25:30 2013 +0200
Tweak ORCONN event based on comments by nickm.
- Move new ID= parameter in ORCONN event to end. Avoids possible trouble
from controllers that parse parameters by position, even though they
shouldn't.
---
src/or/control.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/src/or/control.c b/src/or/control.c
index d3b968c..2accf7f 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -3858,18 +3858,17 @@ control_event_or_conn_status(or_connection_t *conn, or_conn_status_event_t tp,
}
ncircs += connection_or_get_num_circuits(conn);
if (ncircs && (tp == OR_CONN_EVENT_FAILED || tp == OR_CONN_EVENT_CLOSED)) {
- tor_snprintf(ncircs_buf, sizeof(ncircs_buf), "%sNCIRCS=%d",
- reason ? " " : "", ncircs);
+ tor_snprintf(ncircs_buf, sizeof(ncircs_buf), " NCIRCS=%d", ncircs);
}
orconn_target_get_name(name, sizeof(name), conn);
send_control_event(EVENT_OR_CONN_STATUS, ALL_FORMATS,
- "650 ORCONN %s %s ID="U64_FORMAT" %s%s%s\r\n",
+ "650 ORCONN %s %s%s%s%s ID="U64_FORMAT"\r\n",
name, status,
- U64_PRINTF_ARG(conn->base_.global_identifier),
- reason ? "REASON=" : "",
+ reason ? " REASON=" : "",
orconn_end_reason_to_control_string(reason),
- ncircs_buf);
+ ncircs_buf,
+ U64_PRINTF_ARG(conn->base_.global_identifier));
return 0;
}
1
0
31 Oct '13
commit 2f893624abb65a0df4f0f8ca6fbbe0c00fbf216a
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Fri May 24 12:01:32 2013 +0200
Tweak CONN_BW event based on comments by nickm.
- Rename read/write counters in connection_t to make it clear that these
are only used for CONN_BW events.
- Add TestingEnableConnBwEvent option.
---
doc/tor.1.txt | 6 ++++++
src/or/config.c | 8 ++++++++
src/or/connection.c | 24 ++++++++++++------------
src/or/control.c | 12 ++++++------
src/or/or.h | 13 +++++++++----
5 files changed, 41 insertions(+), 22 deletions(-)
diff --git a/doc/tor.1.txt b/doc/tor.1.txt
index 0a6f8f5..209670a 100644
--- a/doc/tor.1.txt
+++ b/doc/tor.1.txt
@@ -2005,6 +2005,7 @@ The following options are used for running a testing Tor network.
TestingV3AuthInitialDistDelay 20 seconds
TestingAuthDirTimeToLearnReachability 0 minutes
TestingEstimatedDescriptorPropagationTime 0 minutes
+ TestingEnableConnBwEvent 1
**TestingV3AuthInitialVotingInterval** __N__ **minutes**|**hours**::
Like V3AuthVotingInterval, but for initial voting interval before the first
@@ -2035,6 +2036,11 @@ The following options are used for running a testing Tor network.
Minimum value for the Fast flag. Overrides the ordinary minimum taken
from the consensus when TestingTorNetwork is set. (Default: 0.)
+**TestingEnableConnBwEvent** **0**|**1**::
+ If this option is set, then Tor controllers may register for CONN_BW
+ events. Changing this requires that **TestingTorNetwork** is set.
+ (Default: 0)
+
SIGNALS
-------
diff --git a/src/or/config.c b/src/or/config.c
index 8ca89b6..e7060a5 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -218,6 +218,7 @@ static config_var_t option_vars_[] = {
VPORT(DNSPort, LINELIST, NULL),
V(DNSListenAddress, LINELIST, NULL),
V(DownloadExtraInfo, BOOL, "0"),
+ V(TestingEnableConnBwEvent, BOOL, "0"),
V(EnforceDistinctSubnets, BOOL, "1"),
V(EntryNodes, ROUTERSET, NULL),
V(EntryStatistics, BOOL, "0"),
@@ -461,6 +462,7 @@ static const config_var_t testing_tor_network_defaults[] = {
V(TestingAuthDirTimeToLearnReachability, INTERVAL, "0 minutes"),
V(TestingEstimatedDescriptorPropagationTime, INTERVAL, "0 minutes"),
V(MinUptimeHidServDirectoryV2, INTERVAL, "0 minutes"),
+ V(TestingEnableConnBwEvent, BOOL, "1"),
VAR("___UsingTestNetworkDefaults", BOOL, UsingTestNetworkDefaults_, "1"),
{ NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
@@ -3236,6 +3238,12 @@ options_validate(or_options_t *old_options, or_options_t *options,
COMPLAIN("TestingEstimatedDescriptorPropagationTime is insanely high.");
}
+ if (options->TestingEnableConnBwEvent &&
+ !options->TestingTorNetwork && !options->UsingTestNetworkDefaults_) {
+ REJECT("TestingEnableConnBwEvent may only be changed in testing "
+ "Tor networks!");
+ }
+
if (options->TestingTorNetwork) {
log_warn(LD_CONFIG, "TestingTorNetwork is set. This will make your node "
"almost unusable in the public Tor network, and is "
diff --git a/src/or/connection.c b/src/or/connection.c
index a00351a..f7f028b 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -3224,16 +3224,16 @@ connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
}
}
- /* In TestingTorNetwork mode, update conn->n_read for OR/DIR/EXIT
- * connections, checking for overflow. */
- if (get_options()->TestingTorNetwork &&
+ /* If CONN_BW events are enabled, update conn->n_read_conn_bw for
+ * OR/DIR/EXIT connections, checking for overflow. */
+ if (get_options()->TestingEnableConnBwEvent &&
(conn->type == CONN_TYPE_OR ||
conn->type == CONN_TYPE_DIR ||
conn->type == CONN_TYPE_EXIT)) {
- if (PREDICT_LIKELY(UINT32_MAX - conn->n_read > n_read))
- conn->n_read += (int)n_read;
+ if (PREDICT_LIKELY(UINT32_MAX - conn->n_read_conn_bw > n_read))
+ conn->n_read_conn_bw += (int)n_read;
else
- conn->n_read = UINT32_MAX;
+ conn->n_read_conn_bw = UINT32_MAX;
}
}
@@ -3691,16 +3691,16 @@ connection_handle_write_impl(connection_t *conn, int force)
}
}
- /* In TestingTorNetwork mode, update conn->n_written for OR/DIR/EXIT
- * connections, checking for overflow. */
- if (n_written && get_options()->TestingTorNetwork &&
+ /* If CONN_BW events are enabled, update conn->n_written_conn_bw for
+ * OR/DIR/EXIT connections, checking for overflow. */
+ if (n_written && get_options()->TestingEnableConnBwEvent &&
(conn->type == CONN_TYPE_OR ||
conn->type == CONN_TYPE_DIR ||
conn->type == CONN_TYPE_EXIT)) {
- if (PREDICT_LIKELY(UINT32_MAX - conn->n_written > n_written))
- conn->n_written += (int)n_written;
+ if (PREDICT_LIKELY(UINT32_MAX - conn->n_written_conn_bw > n_written))
+ conn->n_written_conn_bw += (int)n_written;
else
- conn->n_written = UINT32_MAX;
+ conn->n_written_conn_bw = UINT32_MAX;
}
connection_buckets_decrement(conn, approx_time(), n_read, n_written);
diff --git a/src/or/control.c b/src/or/control.c
index 2accf7f..10f96b3 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -3967,10 +3967,10 @@ int
control_event_conn_bandwidth(connection_t *conn)
{
const char *conn_type_str;
- if (!get_options()->TestingTorNetwork ||
+ if (!get_options()->TestingEnableConnBwEvent ||
!EVENT_IS_INTERESTING(EVENT_CONN_BW))
return 0;
- if (!conn->n_read && !conn->n_written)
+ if (!conn->n_read_conn_bw && !conn->n_written_conn_bw)
return 0;
switch (conn->type) {
case CONN_TYPE_OR:
@@ -3990,9 +3990,9 @@ control_event_conn_bandwidth(connection_t *conn)
"READ=%lu WRITTEN=%lu\r\n",
U64_PRINTF_ARG(conn->global_identifier),
conn_type_str,
- (unsigned long)conn->n_read,
- (unsigned long)conn->n_written);
- conn->n_written = conn->n_read = 0;
+ (unsigned long)conn->n_read_conn_bw,
+ (unsigned long)conn->n_written_conn_bw);
+ conn->n_written_conn_bw = conn->n_read_conn_bw = 0;
return 0;
}
@@ -4001,7 +4001,7 @@ control_event_conn_bandwidth(connection_t *conn)
int
control_event_conn_bandwidth_used(void)
{
- if (get_options()->TestingTorNetwork &&
+ if (get_options()->TestingEnableConnBwEvent &&
EVENT_IS_INTERESTING(EVENT_CONN_BW)) {
SMARTLIST_FOREACH(get_connection_array(), connection_t *, conn,
control_event_conn_bandwidth(conn));
diff --git a/src/or/or.h b/src/or/or.h
index d41034c..c807fb0 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -1249,11 +1249,13 @@ typedef struct connection_t {
/** Unique identifier for this connection on this Tor instance. */
uint64_t global_identifier;
- /** Bytes read since last call to control_event_conn_bandwidth_used() */
- uint32_t n_read;
+ /** Bytes read since last call to control_event_conn_bandwidth_used().
+ * Only used if we're configured to emit CONN_BW events. */
+ uint32_t n_read_conn_bw;
- /** Bytes written since last call to control_event_conn_bandwidth_used() */
- uint32_t n_written;
+ /** Bytes written since last call to control_event_conn_bandwidth_used().
+ * Only used if we're configured to emit CONN_BW events. */
+ uint32_t n_written_conn_bw;
} connection_t;
/** Subtype of connection_t; used for a listener socket. */
@@ -3983,6 +3985,9 @@ typedef struct {
/** Minimum value for the Fast flag threshold on testing networks. */
uint64_t TestingMinFastFlagThreshold;
+ /** Enable CONN_BW events. Only altered on testing networks. */
+ int TestingEnableConnBwEvent;
+
/** If true, and we have GeoIP data, and we're a bridge, keep a per-country
* count of how many client addresses have contacted us so that we can help
* the bridge authority guess which countries have blocked access to us. */
1
0
31 Oct '13
commit b33b366a7f8bcab1c9b4859788e3b2c7d3dcf180
Author: Karsten Loesing <karsten.loesing(a)gmx.net>
Date: Sat May 25 13:04:33 2013 +0200
Tweak CIRC_BW event based on comments by nickm.
- Rename n_read and n_written in origin_circuit_t to make it clear that
these are only used for CIRC_BW events.
- Extract new code in control_update_global_event_mask to new
clear_circ_bw_fields function.
---
src/or/connection.c | 14 +++++++-------
src/or/control.c | 35 +++++++++++++++++++++--------------
src/or/or.h | 10 ++++++----
3 files changed, 34 insertions(+), 25 deletions(-)
diff --git a/src/or/connection.c b/src/or/connection.c
index 2f2a421..88def49 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -3271,7 +3271,7 @@ connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
/* change *max_to_read */
*max_to_read = at_most - n_read;
- /* Update edge_conn->n_read and ocirc->n_read */
+ /* Update edge_conn->n_read and ocirc->n_read_circ_bw */
if (conn->type == CONN_TYPE_AP) {
edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
@@ -3285,10 +3285,10 @@ connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
if (circ && CIRCUIT_IS_ORIGIN(circ)) {
ocirc = TO_ORIGIN_CIRCUIT(circ);
- if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_read > n_read))
- ocirc->n_read += (int)n_read;
+ if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_read_circ_bw > n_read))
+ ocirc->n_read_circ_bw += (int)n_read;
else
- ocirc->n_read = UINT32_MAX;
+ ocirc->n_read_circ_bw = UINT32_MAX;
}
}
@@ -3752,10 +3752,10 @@ connection_handle_write_impl(connection_t *conn, int force)
if (circ && CIRCUIT_IS_ORIGIN(circ)) {
ocirc = TO_ORIGIN_CIRCUIT(circ);
- if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_written > n_written))
- ocirc->n_written += (int)n_written;
+ if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_written_circ_bw > n_written))
+ ocirc->n_written_circ_bw += (int)n_written;
else
- ocirc->n_written = UINT32_MAX;
+ ocirc->n_written_circ_bw = UINT32_MAX;
}
}
diff --git a/src/or/control.c b/src/or/control.c
index ac7be8d..5e2020f 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -237,6 +237,20 @@ log_severity_to_event(int severity)
}
}
+/** Helper: clear bandwidth counters of all origin circuits. */
+static void
+clear_circ_bw_fields(void)
+{
+ circuit_t *circ;
+ origin_circuit_t *ocirc;
+ for (circ = circuit_get_global_list_(); circ; circ = circ->next) {
+ if (!CIRCUIT_IS_ORIGIN(circ))
+ continue;
+ ocirc = TO_ORIGIN_CIRCUIT(circ);
+ ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
+ }
+}
+
/** Set <b>global_event_mask*</b> to the bitwise OR of each live control
* connection's event_mask field. */
void
@@ -276,14 +290,7 @@ control_update_global_event_mask(void)
}
if (! (old_mask & EVENT_CIRC_BANDWIDTH_USED) &&
(new_mask & EVENT_CIRC_BANDWIDTH_USED)) {
- circuit_t *circ;
- origin_circuit_t *ocirc;
- for (circ = circuit_get_global_list_(); circ; circ = circ->next) {
- if (!CIRCUIT_IS_ORIGIN(circ))
- continue;
- ocirc = TO_ORIGIN_CIRCUIT(circ);
- ocirc->n_written = ocirc->n_read = 0;
- }
+ clear_circ_bw_fields();
}
}
@@ -3893,8 +3900,8 @@ control_event_stream_bandwidth(edge_connection_t *edge_conn)
circ = circuit_get_by_edge_conn(edge_conn);
if (circ && CIRCUIT_IS_ORIGIN(circ)) {
ocirc = TO_ORIGIN_CIRCUIT(circ);
- ocirc->n_read += edge_conn->n_read;
- ocirc->n_written += edge_conn->n_written;
+ ocirc->n_read_circ_bw += edge_conn->n_read;
+ ocirc->n_written_circ_bw += edge_conn->n_written;
}
edge_conn->n_written = edge_conn->n_read = 0;
}
@@ -3947,14 +3954,14 @@ control_event_circ_bandwidth_used(void)
if (!CIRCUIT_IS_ORIGIN(circ))
continue;
ocirc = TO_ORIGIN_CIRCUIT(circ);
- if (!ocirc->n_read && !ocirc->n_written)
+ if (!ocirc->n_read_circ_bw && !ocirc->n_written_circ_bw)
continue;
send_control_event(EVENT_CIRC_BANDWIDTH_USED, ALL_FORMATS,
"650 CIRC_BW ID=%d READ=%lu WRITTEN=%lu\r\n",
ocirc->global_identifier,
- (unsigned long)ocirc->n_read,
- (unsigned long)ocirc->n_written);
- ocirc->n_written = ocirc->n_read = 0;
+ (unsigned long)ocirc->n_read_circ_bw,
+ (unsigned long)ocirc->n_written_circ_bw);
+ ocirc->n_written_circ_bw = ocirc->n_read_circ_bw = 0;
}
return 0;
diff --git a/src/or/or.h b/src/or/or.h
index 21a36c9..6dd3ce4 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -2971,12 +2971,14 @@ typedef struct origin_circuit_t {
edge_connection_t *p_streams;
/** Bytes read from any attached stream since last call to
- * control_event_circ_bandwidth_used() */
- uint32_t n_read;
+ * control_event_circ_bandwidth_used(). Only used if we're configured
+ * to emit CIRC_BW events. */
+ uint32_t n_read_circ_bw;
/** Bytes written to any attached stream since last call to
- * control_event_circ_bandwidth_used() */
- uint32_t n_written;
+ * control_event_circ_bandwidth_used(). Only used if we're configured
+ * to emit CIRC_BW events. */
+ uint32_t n_written_circ_bw;
/** Build state for this circuit. It includes the intended path
* length, the chosen exit router, rendezvous information, etc.
1
0