Tor 0.4.9.1-alpha-dev
conflux_params.c
1/* Copyright (c) 2023, The Tor Project, Inc. */
2/* See LICENSE for licensing information */
3
4/**
5 * \file conflux_params.h
6 * \brief Header file for conflux_params.c.
7 **/
8
9#include "core/or/or.h"
10
11#include "app/config/config.h"
12
15#include "core/or/circuitlist.h"
16
21
23
24/**
25 * Consensus parameters defaults, minimums and maximums.
26 */
27
28/* For "cfx_enabled". */
29#define CONFLUX_ENABLED_MIN (0)
30#define CONFLUX_ENABLED_MAX (1)
31#define CONFLUX_ENABLED_DEFAULT (1)
32
33/* For "cfx_low_exit_threshold". This is a percentage scaled to 10000 so we can
34 * support two decimal points. For example, 65.78% would be 6578. */
35#define LOW_EXIT_THRESHOLD_MIN (0)
36#define LOW_EXIT_THRESHOLD_MAX (10000)
37#define LOW_EXIT_THRESHOLD_DEFAULT (6000)
38
39/* For "cfx_max_linked_set". */
40#define MAX_LINKED_SET_MIN (0)
41#define MAX_LINKED_SET_MAX (UINT8_MAX)
42#define MAX_LINKED_SET_DEFAULT (10)
43
44/* For "cfx_max_prebuilt_set". */
45#define MAX_PREBUILT_SET_MIN (0)
46#define MAX_PREBUILT_SET_MAX (UINT8_MAX)
47#define MAX_PREBUILT_SET_DEFAULT (3)
48
49/* For "cfx_max_leg_retry". */
50#define MAX_UNLINKED_LEG_RETRY_DEFAULT (3)
51#define MAX_UNLINKED_LEG_RETRY_MIN (0)
52#define MAX_UNLINKED_LEG_RETRY_MAX (UINT8_MAX)
53
54/* For "cfx_num_legs_set". */
55#define NUM_LEGS_SET_MIN (0)
56#define NUM_LEGS_SET_MAX (UINT8_MAX)
57#define NUM_LEGS_SET_DEFAULT (2)
58
59/* For "cfx_max_legs_set" */
60#define MAX_LEGS_SET_MIN (3)
61#define MAX_LEGS_SET_MAX (UINT8_MAX)
62#define MAX_LEGS_SET_DEFAULT (8)
63
64/* For "cfx_send_pct". */
65#define CFX_SEND_PCT_MIN (0)
66#define CFX_SEND_PCT_MAX (255)
67#define CFX_SEND_PCT_DFLT 100
68
69/* For "cfx_drain_pct". */
70#define CFX_DRAIN_PCT_MIN (0)
71#define CFX_DRAIN_PCT_MAX (255)
72#define CFX_DRAIN_PCT_DFLT 0
73
74/*
75 * Cached consensus parameters.
76 */
77
78/* Indicate if conflux is enabled or disabled. */
79static bool conflux_enabled = CONFLUX_ENABLED_DEFAULT;
80/* Maximum number of linked set we are allowed to have (even if in use). */
81static uint8_t max_linked_set = MAX_LINKED_SET_DEFAULT;
82/* Maximum number of pre built set. */
83static uint8_t max_prebuilt_set = MAX_PREBUILT_SET_DEFAULT;
84/* Maximum number of unlinked leg retry that is how many times are we allowed
85 * to retry a leg until it successfully links. */
86STATIC uint32_t max_unlinked_leg_retry = MAX_UNLINKED_LEG_RETRY_DEFAULT;
87/* Number of legs per set. */
88static uint8_t num_legs_set = NUM_LEGS_SET_DEFAULT;
89/* Maximum number of legs per set allowed at exits */
90static uint8_t max_legs_set = MAX_LEGS_SET_DEFAULT;
91/* The low Exit relay threshold, as a ratio between 0 and 1, used as a limit to
92 * decide the amount of pre-built set we build depending on how many Exit relay
93 * supports conflux in our current consensus. */
94static double low_exit_threshold_ratio =
95 LOW_EXIT_THRESHOLD_DEFAULT / (double)LOW_EXIT_THRESHOLD_MAX;
96
97static uint8_t cfx_drain_pct = CFX_DRAIN_PCT_DFLT;
98static uint8_t cfx_send_pct = CFX_SEND_PCT_DFLT;
99
100/* Ratio of Exit relays in our consensus supporting conflux. This is computed
101 * at every consensus and it is between 0 and 1. */
102static double exit_conflux_ratio = 0.0;
103
104/** Sets num_conflux_exit with the latest count of Exits in the given consensus
105 * that supports Conflux. */
106static void
107count_exit_with_conflux_support(const networkstatus_t *ns)
108{
109 double supported = 0.0;
110 int total_exits = 0;
111
112 if (!ns || smartlist_len(ns->routerstatus_list) == 0) {
113 return;
114 }
115
117 if (!rs->is_exit || rs->is_bad_exit) {
118 continue;
119 }
120 if (rs->pv.supports_conflux) {
121 supported++;
122 }
123 total_exits++;
124 } SMARTLIST_FOREACH_END(rs);
125
126 if (total_exits > 0) {
127 exit_conflux_ratio =
128 supported / total_exits;
129 } else {
130 exit_conflux_ratio = 0.0;
131 }
132
133 log_info(LD_GENERAL, "Consensus has %.2f %% Exit relays supporting Conflux",
134 exit_conflux_ratio * 100.0);
135}
136
137/**
138 * Return true iff conflux feature is enabled and usable for a given circuit.
139 *
140 * Circ may be NULL, in which case we only check the consensus and torrc. */
141bool
142conflux_is_enabled(const circuit_t *circ)
143{
144 const or_options_t *opts = get_options();
145
146 /* Conflux CAN NOT operate properly without congestion control and so
147 * automatically disabled conflux if we don't have CC enabled. */
149 return false;
150 }
151
152 if (circ) {
153 /* If circuit is non-null, we need to check to see if congestion
154 * control was successfully negotiated. Conflux depends upon congestion
155 * control, and consensus checks are not enough because there can be a
156 * race between those checks and the consensus update to enable
157 * congestion control. This happens in Shadow, and at relay restart. */
158 if (CIRCUIT_IS_ORIGIN(circ)) {
159 tor_assert(CONST_TO_ORIGIN_CIRCUIT(circ)->cpath);
160 tor_assert(CONST_TO_ORIGIN_CIRCUIT(circ)->cpath->prev);
161 if (!CONST_TO_ORIGIN_CIRCUIT(circ)->cpath->prev->ccontrol)
162 return false;
163 } else {
164 if (!circ->ccontrol)
165 return false;
166 }
167 }
168
169 /* For clients, this is mostly for sbws. For relays, this is an emergency
170 * emergency override, in case a bug is discovered by a relay operator
171 * and we can't set a consensus param fast enough. Basically gives them
172 * an option other than downgrading. */
173 if (opts->ConfluxEnabled != -1) {
174 if (server_mode(opts)) {
175 char *msg;
176 static ratelim_t rlimit = RATELIM_INIT(60 * 60); /* Hourly */
177 if ((msg = rate_limit_log(&rlimit, time(NULL)))) {
178 log_warn(LD_GENERAL,
179 "This tor is a relay and ConfluxEnabled is set to 0. "
180 "We would ask you to please write to us on "
181 "tor-relays@lists.torproject.org or file a bug explaining "
182 "why you have disabled this option. Without news from you, "
183 "we might end up marking your relay as a BadExit.");
184 tor_free(msg);
185 }
186 }
187 return opts->ConfluxEnabled;
188 }
189
190 return conflux_enabled;
191}
192
193/** Return the maximum number of linked set we are allowed to have. */
194uint8_t
196{
197 return max_linked_set;
198}
199
200/** Return the number of maximum pre built sets that is allowed to have. */
201uint8_t
203{
204 /* Without any Exit supporting conflux, we won't be able to build a set. The
205 * float problem here is minimal because exit_conflux_ratio is either a flat
206 * 0 or else it means we do have at least an exit. */
207 if (exit_conflux_ratio <= 0.0) {
208 return 0;
209 }
210
211 /* Allow only 1 pre built set if we are lower than the low exit threshold
212 * parameter from the consensus. */
213 if (exit_conflux_ratio < low_exit_threshold_ratio) {
214 return 1;
215 }
216 return max_prebuilt_set;
217}
218
219/** Return the maximum number of retry we can do until a leg links. */
220uint8_t
222{
223 return max_unlinked_leg_retry;
224}
225
226/** Return the number of legs per set. */
227uint8_t
229{
230 return num_legs_set;
231}
232
233/** Return the maximum number of legs per set. */
234uint8_t
236{
237 return max_legs_set;
238}
239
240/** Return the drain percent we must hit before switching */
241uint8_t
243{
244 return cfx_drain_pct;
245}
246
247/** Return the percent of the congestion window to send before switching. */
248uint8_t
250{
251 return cfx_send_pct;
252}
253
254/** Update global conflux related consensus parameter values, every consensus
255 * update. */
256void
258{
259 /* Params used by conflux_pool.c */
260 conflux_enabled =
261 networkstatus_get_param(ns, "cfx_enabled",
262 CONFLUX_ENABLED_DEFAULT,
263 CONFLUX_ENABLED_MIN, CONFLUX_ENABLED_MAX);
264
265 low_exit_threshold_ratio =
266 networkstatus_get_param(ns, "cfx_low_exit_threshold",
267 LOW_EXIT_THRESHOLD_DEFAULT,
268 LOW_EXIT_THRESHOLD_MIN, LOW_EXIT_THRESHOLD_MAX) /
269 (double)LOW_EXIT_THRESHOLD_MAX;
270
271 max_linked_set =
272 networkstatus_get_param(ns, "cfx_max_linked_set",
273 MAX_LINKED_SET_DEFAULT,
274 MAX_LINKED_SET_MIN, MAX_LINKED_SET_MAX);
275
276 max_prebuilt_set =
277 networkstatus_get_param(ns, "cfx_max_prebuilt_set",
278 MAX_PREBUILT_SET_DEFAULT,
279 MAX_PREBUILT_SET_MIN, MAX_PREBUILT_SET_MAX);
280
281 max_unlinked_leg_retry =
282 networkstatus_get_param(ns, "cfx_max_unlinked_leg_retry",
283 MAX_UNLINKED_LEG_RETRY_DEFAULT,
284 MAX_UNLINKED_LEG_RETRY_MIN,
285 MAX_UNLINKED_LEG_RETRY_MAX);
286
287 num_legs_set =
288 networkstatus_get_param(ns, "cfx_num_legs_set",
289 NUM_LEGS_SET_DEFAULT,
290 NUM_LEGS_SET_MIN, NUM_LEGS_SET_MAX);
291
292 max_legs_set =
293 networkstatus_get_param(ns, "cfx_max_legs_set",
294 MAX_LEGS_SET_DEFAULT,
295 MAX_LEGS_SET_MIN, MAX_LEGS_SET_MAX);
296
297 /* Params used by conflux.c */
298 cfx_send_pct = networkstatus_get_param(ns, "cfx_send_pct",
299 CFX_SEND_PCT_DFLT,
300 CFX_SEND_PCT_MIN,
301 CFX_SEND_PCT_MAX);
302
303 cfx_drain_pct = networkstatus_get_param(ns, "cfx_drain_pct",
304 CFX_DRAIN_PCT_DFLT,
305 CFX_DRAIN_PCT_MIN,
306 CFX_DRAIN_PCT_MAX);
307
308 count_exit_with_conflux_support(ns);
309}
Header file for circuitlist.c.
#define CIRCUIT_IS_ORIGIN(c)
Definition: circuitlist.h:154
const or_options_t * get_options(void)
Definition: config.c:944
Header file for config.c.
Header file for conflux_params.c.
uint8_t conflux_params_get_send_pct(void)
uint8_t conflux_params_get_drain_pct(void)
uint8_t conflux_params_get_max_legs_set(void)
uint8_t conflux_params_get_max_prebuilt(void)
uint8_t conflux_params_get_max_unlinked_leg_retry(void)
uint8_t conflux_params_get_num_legs_set(void)
uint8_t conflux_params_get_max_linked_set(void)
void conflux_params_new_consensus(const networkstatus_t *ns)
bool congestion_control_enabled(void)
Public APIs for congestion control.
#define LD_GENERAL
Definition: log.h:62
#define tor_free(p)
Definition: malloc.h:56
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Header file for networkstatus.c.
Networkstatus consensus/vote structure.
Master header file for Tor-specific functionality.
Origin circuit structure.
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition: ratelim.c:42
int server_mode(const or_options_t *options)
Definition: routermode.c:34
Header file for routermode.c.
Routerstatus (consensus entry) structure.
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
struct congestion_control_t * ccontrol
Definition: circuit_st.h:250
smartlist_t * routerstatus_list
#define STATIC
Definition: testsupport.h:32
#define tor_assert(expr)
Definition: util_bug.h:103