Move netstatus (mainloop) state fields into mainloop's state.

This commit is contained in:
Nick Mathewson 2019-11-05 11:58:31 -05:00
parent 3afbb29bee
commit 280a9a4760
11 changed files with 126 additions and 22 deletions

View File

@ -89,13 +89,6 @@ struct or_state_t {
/** When did we last rotate our onion key? "0" for 'no idea'. */ /** When did we last rotate our onion key? "0" for 'no idea'. */
time_t LastRotatedOnionKey; time_t LastRotatedOnionKey;
/** Number of minutes since the last user-initiated request (as defined by
* the dormant net-status system.) Set to zero if we are dormant. */
int MinutesSinceUserActivity;
/** True if we were dormant when we last wrote the file; false if we
* weren't. "auto" on initial startup. */
int Dormant;
/** /**
* State objects for individual modules. * State objects for individual modules.
* *

View File

@ -132,9 +132,6 @@ static const config_var_t state_vars_[] = {
VAR("CircuitBuildTimeBin", LINELIST_S, BuildtimeHistogram, NULL), VAR("CircuitBuildTimeBin", LINELIST_S, BuildtimeHistogram, NULL),
VAR("BuildtimeHistogram", LINELIST_V, BuildtimeHistogram, NULL), VAR("BuildtimeHistogram", LINELIST_V, BuildtimeHistogram, NULL),
V(MinutesSinceUserActivity, POSINT, NULL),
V(Dormant, AUTOBOOL, "auto"),
END_OF_CONFIG_VARS END_OF_CONFIG_VARS
}; };
@ -177,7 +174,7 @@ static const config_format_t state_format = {
static config_mgr_t *state_mgr = NULL; static config_mgr_t *state_mgr = NULL;
/** Return the configuration manager for state-file objects. */ /** Return the configuration manager for state-file objects. */
static const config_mgr_t * STATIC const config_mgr_t *
get_state_mgr(void) get_state_mgr(void)
{ {
if (PREDICT_UNLIKELY(state_mgr == NULL)) { if (PREDICT_UNLIKELY(state_mgr == NULL)) {
@ -334,7 +331,6 @@ or_state_set(or_state_t *new_state)
get_circuit_build_times_mutable(),global_state) < 0) { get_circuit_build_times_mutable(),global_state) < 0) {
ret = -1; ret = -1;
} }
netstatus_load_from_state(global_state, time(NULL));
return ret; return ret;
} }
@ -527,7 +523,6 @@ or_state_save(time_t now)
entry_guards_update_state(global_state); entry_guards_update_state(global_state);
rep_hist_update_state(global_state); rep_hist_update_state(global_state);
circuit_build_times_update_state(get_circuit_build_times(), global_state); circuit_build_times_update_state(get_circuit_build_times(), global_state);
netstatus_flush_to_state(global_state, now);
if (accounting_is_enabled(get_options())) if (accounting_is_enabled(get_options()))
accounting_run_housekeeping(now); accounting_run_housekeeping(now);

View File

@ -31,6 +31,8 @@ STATIC struct config_line_t *get_transport_in_state_by_name(
STATIC void or_state_free_(or_state_t *state); STATIC void or_state_free_(or_state_t *state);
#define or_state_free(st) FREE_AND_NULL(or_state_t, or_state_free_, (st)) #define or_state_free(st) FREE_AND_NULL(or_state_t, or_state_free_, (st))
STATIC or_state_t *or_state_new(void); STATIC or_state_t *or_state_new(void);
struct config_mgr_t;
STATIC const struct config_mgr_t *get_state_mgr(void);
#endif /* defined(STATEFILE_PRIVATE) */ #endif /* defined(STATEFILE_PRIVATE) */
#endif /* !defined(TOR_STATEFILE_H) */ #endif /* !defined(TOR_STATEFILE_H) */

View File

@ -245,6 +245,8 @@ noinst_HEADERS += \
src/core/mainloop/cpuworker.h \ src/core/mainloop/cpuworker.h \
src/core/mainloop/mainloop.h \ src/core/mainloop/mainloop.h \
src/core/mainloop/mainloop_pubsub.h \ src/core/mainloop/mainloop_pubsub.h \
src/core/mainloop/mainloop_state.inc \
src/core/mainloop/mainloop_state_st.h \
src/core/mainloop/mainloop_sys.h \ src/core/mainloop/mainloop_sys.h \
src/core/mainloop/netstatus.h \ src/core/mainloop/netstatus.h \
src/core/mainloop/periodic.h \ src/core/mainloop/periodic.h \

View File

@ -2,6 +2,7 @@
orconfig.h orconfig.h
lib/conf/*.h
lib/container/*.h lib/container/*.h
lib/dispatch/*.h lib/dispatch/*.h
lib/evloop/*.h lib/evloop/*.h
@ -17,4 +18,5 @@ lib/geoip/*.h
lib/sandbox/*.h lib/sandbox/*.h
lib/compress/*.h lib/compress/*.h
core/mainloop/*.h core/mainloop/*.h
core/mainloop/*.inc

View File

@ -0,0 +1,19 @@
/**
* @file mainloop_state.inc
* @brief Declare configuration options for the crypto_ops module.
**/
/** Holds state for the mainloop, corresponding to part of the state
* file in Tor's DataDirectory. */
BEGIN_CONF_STRUCT(mainloop_state_t)
/** Number of minutes since the last user-initiated request (as defined by
* the dormant net-status system.) Set to zero if we are dormant. */
CONF_VAR(MinutesSinceUserActivity, POSINT, 0, NULL)
/** True if we were dormant when we last wrote the file; false if we
* weren't. "auto" on initial startup. */
CONF_VAR(Dormant, AUTOBOOL, 0, "auto")
END_CONF_STRUCT(mainloop_state_t)

View File

@ -0,0 +1,23 @@
/* Copyright (c) 2001 Matej Pfajfar.
* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* @file mainloop_state_st.h
* @brief Declare a state structure for mainloop-relevant fields
**/
#ifndef TOR_CORE_MAINLOOP_MAINLOOP_STATE_ST_H
#define TOR_CORE_MAINLOOP_MAINLOOP_STATE_ST_H
#include "lib/conf/confdecl.h"
#define CONF_CONTEXT STRUCT
#include "core/mainloop/mainloop_state.inc"
#undef CONF_CONTEXT
typedef struct mainloop_state_t mainloop_state_t;
#endif /* !defined(TOR_CORE_MAINLOOP_MAINLOOP_STATE_ST_H) */

View File

@ -12,6 +12,10 @@
#include "core/or/or.h" #include "core/or/or.h"
#include "core/mainloop/mainloop_sys.h" #include "core/mainloop/mainloop_sys.h"
#include "core/mainloop/mainloop.h" #include "core/mainloop/mainloop.h"
#include "core/mainloop/mainloop_state_st.h"
#include "core/mainloop/netstatus.h"
#include "lib/conf/conftypes.h"
#include "lib/conf/confdecl.h"
#include "lib/subsys/subsys.h" #include "lib/subsys/subsys.h"
@ -28,10 +32,58 @@ subsys_mainloop_shutdown(void)
tor_mainloop_free_all(); tor_mainloop_free_all();
} }
/** Declare a list of state variables for mainloop state. */
#define CONF_CONTEXT TABLE
#include "core/mainloop/mainloop_state.inc"
#undef CONF_CONTEXT
/** Magic number for mainloop state objects */
#define MAINLOOP_STATE_MAGIC 0x59455449
/**
* Format object for mainloop state.
**/
static config_format_t mainloop_state_fmt = {
.size = sizeof(mainloop_state_t),
.magic = { "mainloop_state",
MAINLOOP_STATE_MAGIC,
offsetof(mainloop_state_t, magic)
},
.vars = mainloop_state_t_vars,
};
/**
*/
static int
mainloop_set_state(void *arg)
{
const mainloop_state_t *state = arg;
tor_assert(state->magic == MAINLOOP_STATE_MAGIC);
netstatus_load_from_state(state, approx_time());
return 0;
}
static int
mainloop_flush_state(void *arg)
{
mainloop_state_t *state = arg;
tor_assert(state->magic == MAINLOOP_STATE_MAGIC);
netstatus_flush_to_state(state, approx_time());
return 0;
}
const struct subsys_fns_t sys_mainloop = { const struct subsys_fns_t sys_mainloop = {
.name = "mainloop", .name = "mainloop",
.supported = true, .supported = true,
.level = 5, .level = 5,
.initialize = subsys_mainloop_initialize, .initialize = subsys_mainloop_initialize,
.shutdown = subsys_mainloop_shutdown, .shutdown = subsys_mainloop_shutdown,
.state_format = &mainloop_state_fmt,
.set_state = mainloop_set_state,
.flush_state = mainloop_flush_state,
}; };

View File

@ -12,6 +12,7 @@
#include "core/or/or.h" #include "core/or/or.h"
#include "core/mainloop/netstatus.h" #include "core/mainloop/netstatus.h"
#include "core/mainloop/mainloop.h" #include "core/mainloop/mainloop.h"
#include "core/mainloop/mainloop_state_st.h"
#include "app/config/config.h" #include "app/config/config.h"
#include "feature/hibernate/hibernate.h" #include "feature/hibernate/hibernate.h"
@ -115,7 +116,7 @@ is_participating_on_network(void)
* Update 'state' with the last time at which we were active on the network. * Update 'state' with the last time at which we were active on the network.
**/ **/
void void
netstatus_flush_to_state(or_state_t *state, time_t now) netstatus_flush_to_state(mainloop_state_t *state, time_t now)
{ {
state->Dormant = ! participating_on_network; state->Dormant = ! participating_on_network;
if (participating_on_network) { if (participating_on_network) {
@ -130,7 +131,7 @@ netstatus_flush_to_state(or_state_t *state, time_t now)
* Update our current view of network participation from an or_state_t object. * Update our current view of network participation from an or_state_t object.
**/ **/
void void
netstatus_load_from_state(const or_state_t *state, time_t now) netstatus_load_from_state(const mainloop_state_t *state, time_t now)
{ {
time_t last_activity; time_t last_activity;
if (state->Dormant == -1) { // Initial setup. if (state->Dormant == -1) { // Initial setup.

View File

@ -22,8 +22,11 @@ time_t get_last_user_activity_time(void);
void set_network_participation(bool participation); void set_network_participation(bool participation);
bool is_participating_on_network(void); bool is_participating_on_network(void);
void netstatus_flush_to_state(or_state_t *state, time_t now); struct mainloop_state_t;
void netstatus_load_from_state(const or_state_t *state, time_t now);
void netstatus_flush_to_state(struct mainloop_state_t *state, time_t now);
void netstatus_load_from_state(const struct mainloop_state_t *state,
time_t now);
void netstatus_note_clock_jumped(time_t seconds_diff); void netstatus_note_clock_jumped(time_t seconds_diff);
#endif /* !defined(TOR_NETSTATUS_H) */ #endif /* !defined(TOR_NETSTATUS_H) */

View File

@ -13,9 +13,13 @@
#include "test/test.h" #include "test/test.h"
#include "test/log_test_helpers.h" #include "test/log_test_helpers.h"
#include "lib/confmgt/confmgt.h"
#include "core/or/or.h" #include "core/or/or.h"
#include "core/mainloop/connection.h" #include "core/mainloop/connection.h"
#include "core/mainloop/mainloop.h" #include "core/mainloop/mainloop.h"
#include "core/mainloop/mainloop_state_st.h"
#include "core/mainloop/mainloop_sys.h"
#include "core/mainloop/netstatus.h" #include "core/mainloop/netstatus.h"
#include "feature/hs/hs_service.h" #include "feature/hs/hs_service.h"
@ -24,6 +28,8 @@
#include "app/config/statefile.h" #include "app/config/statefile.h"
#include "app/config/or_state_st.h" #include "app/config/or_state_st.h"
#include "app/main/subsysmgr.h"
static const uint64_t BILLION = 1000000000; static const uint64_t BILLION = 1000000000;
static void static void
@ -287,7 +293,13 @@ static void
test_mainloop_dormant_load_state(void *arg) test_mainloop_dormant_load_state(void *arg)
{ {
(void)arg; (void)arg;
or_state_t *state = or_state_new(); or_state_t *or_state = or_state_new();
mainloop_state_t *state;
{
int idx = subsystems_get_state_idx(&sys_mainloop);
tor_assert(idx >= 0);
state = config_mgr_get_obj_mutable(get_state_mgr(), or_state, idx);
}
const time_t start = 1543956575; const time_t start = 1543956575;
reset_user_activity(0); reset_user_activity(0);
@ -326,14 +338,14 @@ test_mainloop_dormant_load_state(void *arg)
tt_i64_op(get_last_user_activity_time(), OP_EQ, start); tt_i64_op(get_last_user_activity_time(), OP_EQ, start);
done: done:
or_state_free(state); or_state_free(or_state);
} }
static void static void
test_mainloop_dormant_save_state(void *arg) test_mainloop_dormant_save_state(void *arg)
{ {
(void)arg; (void)arg;
or_state_t *state = or_state_new(); mainloop_state_t *state = tor_malloc_zero(sizeof(mainloop_state_t));
const time_t start = 1543956575; const time_t start = 1543956575;
// Can we save a non-dormant state correctly? // Can we save a non-dormant state correctly?
@ -352,7 +364,7 @@ test_mainloop_dormant_save_state(void *arg)
tt_int_op(state->MinutesSinceUserActivity, OP_EQ, 0); tt_int_op(state->MinutesSinceUserActivity, OP_EQ, 0);
done: done:
or_state_free(state); tor_free(state);
} }
#define MAINLOOP_TEST(name) \ #define MAINLOOP_TEST(name) \