소스 검색

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.
Karsten Loesing 11 년 전
부모
커밋
2f893624ab
5개의 변경된 파일41개의 추가작업 그리고 22개의 파일을 삭제
  1. 6 0
      doc/tor.1.txt
  2. 8 0
      src/or/config.c
  3. 12 12
      src/or/connection.c
  4. 6 6
      src/or/control.c
  5. 9 4
      src/or/or.h

+ 6 - 0
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
 -------

+ 8 - 0
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 "

+ 12 - 12
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);

+ 6 - 6
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));

+ 9 - 4
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. */