hibernate.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /* Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. const char hibernate_c_id[] =
  5. "$Id$";
  6. /**
  7. * \file hibernate.c
  8. * \brief Functions to close listeners, stop allowing new circuits,
  9. * etc in preparation for closing down or going dormant; and to track
  10. * bandwidth and time intervals to know when to hibernate and when to
  11. * stop hibernating.
  12. **/
  13. /*
  14. hibernating, phase 1:
  15. - send destroy in response to create cells
  16. - send end (policy failed) in response to begin cells
  17. - close an OR conn when it has no circuits
  18. hibernating, phase 2:
  19. (entered when bandwidth hard limit reached)
  20. - close all OR/AP/exit conns)
  21. */
  22. #include "or.h"
  23. /** Possible values of hibernate_state */
  24. typedef enum {
  25. /** We are running normally. */
  26. HIBERNATE_STATE_LIVE=1,
  27. /** We're trying to shut down cleanly, and we'll kill all active connections
  28. * at shutdown_time. */
  29. HIBERNATE_STATE_EXITING=2,
  30. /** We're running low on allocated bandwidth for this period, so we won't
  31. * accept any new connections. */
  32. HIBERNATE_STATE_LOWBANDWIDTH=3,
  33. /** We are hibernating, and we won't wake up till there's more bandwidth to
  34. * use. */
  35. HIBERNATE_STATE_DORMANT=4
  36. } hibernate_state_t;
  37. extern long stats_n_seconds_working; /* published uptime */
  38. /** Are we currently awake, asleep, running out of bandwidth, or shutting
  39. * down? */
  40. static hibernate_state_t hibernate_state = HIBERNATE_STATE_LIVE;
  41. /** If are hibernating, when do we plan to wake up? Set to 0 if we
  42. * aren't hibernating. */
  43. static time_t hibernate_end_time = 0;
  44. /** If we are shutting down, when do we plan finally exit? Set to 0 if
  45. * we aren't shutting down. */
  46. static time_t shutdown_time = 0;
  47. /** Possible accounting periods. */
  48. typedef enum {
  49. UNIT_MONTH=1, UNIT_WEEK=2, UNIT_DAY=3,
  50. } time_unit_t;
  51. /* Fields for accounting logic. Accounting overview:
  52. *
  53. * Accounting is designed to ensure that no more than N bytes are sent in
  54. * either direction over a given interval (currently, one month, one week, or
  55. * one day) We could
  56. * try to do this by choking our bandwidth to a trickle, but that
  57. * would make our streams useless. Instead, we estimate what our
  58. * bandwidth usage will be, and guess how long we'll be able to
  59. * provide that much bandwidth before hitting our limit. We then
  60. * choose a random time within the accounting interval to come up (so
  61. * that we don't get 50 Tors running on the 1st of the month and none
  62. * on the 30th).
  63. *
  64. * Each interval runs as follows:
  65. *
  66. * 1. We guess our bandwidth usage, based on how much we used
  67. * last time. We choose a "wakeup time" within the interval to come up.
  68. * 2. Until the chosen wakeup time, we hibernate.
  69. * 3. We come up at the wakeup time, and provide bandwidth until we are
  70. * "very close" to running out.
  71. * 4. Then we go into low-bandwidth mode, and stop accepting new
  72. * connections, but provide bandwidth until we run out.
  73. * 5. Then we hibernate until the end of the interval.
  74. *
  75. * If the interval ends before we run out of bandwidth, we go back to
  76. * step one.
  77. */
  78. /** How many bytes have we read in this accounting interval? */
  79. static uint64_t n_bytes_read_in_interval = 0;
  80. /** How many bytes have we written in this accounting interval? */
  81. static uint64_t n_bytes_written_in_interval = 0;
  82. /** How many seconds have we been running this interval? */
  83. static uint32_t n_seconds_active_in_interval = 0;
  84. /** When did this accounting interval start? */
  85. static time_t interval_start_time = 0;
  86. /** When will this accounting interval end? */
  87. static time_t interval_end_time = 0;
  88. /** How far into the accounting interval should we hibernate? */
  89. static time_t interval_wakeup_time = 0;
  90. /** How much bandwidth do we 'expect' to use per minute? (0 if we have no
  91. * info from the last period.) */
  92. static uint64_t expected_bandwidth_usage = 0;
  93. /** What unit are we using for our accounting? */
  94. static time_unit_t cfg_unit = UNIT_MONTH;
  95. /** How many days,hours,minutes into each unit does our accounting interval
  96. * start? */
  97. static int cfg_start_day = 0;
  98. static int cfg_start_hour = 0;
  99. static int cfg_start_min = 0;
  100. static void reset_accounting(time_t now);
  101. static int read_bandwidth_usage(void);
  102. static time_t start_of_accounting_period_after(time_t now);
  103. static time_t start_of_accounting_period_containing(time_t now);
  104. static void accounting_set_wakeup_time(void);
  105. /* ************
  106. * Functions for bandwidth accounting.
  107. * ************/
  108. /** Configure accounting start/end time settings based on
  109. * options->AccountingStart. Return 0 on success, -1 on failure. If
  110. * <b>validate_only</b> is true, do not change the current settings. */
  111. int
  112. accounting_parse_options(or_options_t *options, int validate_only)
  113. {
  114. time_unit_t unit;
  115. int ok, idx;
  116. long d,h,m;
  117. smartlist_t *items;
  118. const char *v = options->AccountingStart;
  119. const char *s;
  120. char *cp;
  121. if (!v) {
  122. if (!validate_only) {
  123. cfg_unit = UNIT_MONTH;
  124. cfg_start_day = 1;
  125. cfg_start_hour = 0;
  126. cfg_start_min = 0;
  127. }
  128. return 0;
  129. }
  130. items = smartlist_create();
  131. smartlist_split_string(items, v, NULL,
  132. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
  133. if (smartlist_len(items)<2) {
  134. log_warn(LD_CONFIG, "Too few arguments to AccountingStart");
  135. goto err;
  136. }
  137. s = smartlist_get(items,0);
  138. if (0==strcasecmp(s, "month")) {
  139. unit = UNIT_MONTH;
  140. } else if (0==strcasecmp(s, "week")) {
  141. unit = UNIT_WEEK;
  142. } else if (0==strcasecmp(s, "day")) {
  143. unit = UNIT_DAY;
  144. } else {
  145. log_warn(LD_CONFIG,
  146. "Unrecognized accounting unit '%s': only 'month', 'week',"
  147. " and 'day' are supported.", s);
  148. goto err;
  149. }
  150. switch (unit) {
  151. case UNIT_WEEK:
  152. d = tor_parse_long(smartlist_get(items,1), 10, 1, 7, &ok, NULL);
  153. if (!ok) {
  154. log_warn(LD_CONFIG, "Weekly accounting must begin on a day between "
  155. "1 (Monday) and 7 (Sunday)");
  156. goto err;
  157. }
  158. break;
  159. case UNIT_MONTH:
  160. d = tor_parse_long(smartlist_get(items,1), 10, 1, 28, &ok, NULL);
  161. if (!ok) {
  162. log_warn(LD_CONFIG, "Monthly accounting must begin on a day between "
  163. "1 and 28");
  164. goto err;
  165. }
  166. break;
  167. case UNIT_DAY:
  168. d = 0;
  169. break;
  170. default:
  171. tor_assert(0);
  172. }
  173. idx = unit==UNIT_DAY?1:2;
  174. if (smartlist_len(items) != (idx+1)) {
  175. log_warn(LD_CONFIG,"Accounting unit '%s' requires %d argument%s.",
  176. s, idx, (idx>1)?"s":"");
  177. goto err;
  178. }
  179. s = smartlist_get(items, idx);
  180. h = tor_parse_long(s, 10, 0, 23, &ok, &cp);
  181. if (!ok) {
  182. log_warn(LD_CONFIG,"Accounting start time not parseable: bad hour.");
  183. goto err;
  184. }
  185. if (!cp || *cp!=':') {
  186. log_warn(LD_CONFIG,
  187. "Accounting start time not parseable: not in HH:MM format");
  188. goto err;
  189. }
  190. m = tor_parse_long(cp+1, 10, 0, 59, &ok, &cp);
  191. if (!ok) {
  192. log_warn(LD_CONFIG, "Accounting start time not parseable: bad minute");
  193. goto err;
  194. }
  195. if (!cp || *cp!='\0') {
  196. log_warn(LD_CONFIG,
  197. "Accounting start time not parseable: not in HH:MM format");
  198. goto err;
  199. }
  200. if (!validate_only) {
  201. cfg_unit = unit;
  202. cfg_start_day = (int)d;
  203. cfg_start_hour = (int)h;
  204. cfg_start_min = (int)m;
  205. }
  206. SMARTLIST_FOREACH(items, char *, item, tor_free(item));
  207. smartlist_free(items);
  208. return 0;
  209. err:
  210. SMARTLIST_FOREACH(items, char *, item, tor_free(item));
  211. smartlist_free(items);
  212. return -1;
  213. }
  214. /** If we want to manage the accounting system and potentially
  215. * hibernate, return 1, else return 0.
  216. */
  217. int
  218. accounting_is_enabled(or_options_t *options)
  219. {
  220. if (options->AccountingMax)
  221. return 1;
  222. return 0;
  223. }
  224. /** Called from main.c to tell us that <b>seconds</b> seconds have
  225. * passed, <b>n_read</b> bytes have been read, and <b>n_written</b>
  226. * bytes have been written. */
  227. void
  228. accounting_add_bytes(size_t n_read, size_t n_written, int seconds)
  229. {
  230. n_bytes_read_in_interval += n_read;
  231. n_bytes_written_in_interval += n_written;
  232. /* If we haven't been called in 10 seconds, we're probably jumping
  233. * around in time. */
  234. n_seconds_active_in_interval += (seconds < 10) ? seconds : 0;
  235. }
  236. /** If get_end, return the end of the accounting period that contains
  237. * the time <b>now</b>. Else, return the start of the accounting
  238. * period that contains the time <b>now</b> */
  239. static time_t
  240. edge_of_accounting_period_containing(time_t now, int get_end)
  241. {
  242. int before;
  243. struct tm tm;
  244. tor_localtime_r(&now, &tm);
  245. /* Set 'before' to true iff the current time is before the hh:mm
  246. * changeover time for today. */
  247. before = tm.tm_hour < cfg_start_hour ||
  248. (tm.tm_hour == cfg_start_hour && tm.tm_min < cfg_start_min);
  249. /* Dispatch by unit. First, find the start day of the given period;
  250. * then, if get_end is true, increment to the end day. */
  251. switch (cfg_unit)
  252. {
  253. case UNIT_MONTH: {
  254. /* If this is before the Nth, we want the Nth of last month. */
  255. if (tm.tm_mday < cfg_start_day ||
  256. (tm.tm_mday < cfg_start_day && before)) {
  257. --tm.tm_mon;
  258. }
  259. /* Otherwise, the month is correct. */
  260. tm.tm_mday = cfg_start_day;
  261. if (get_end)
  262. ++tm.tm_mon;
  263. break;
  264. }
  265. case UNIT_WEEK: {
  266. /* What is the 'target' day of the week in struct tm format? (We
  267. say Sunday==7; struct tm says Sunday==0.) */
  268. int wday = cfg_start_day % 7;
  269. /* How many days do we subtract from today to get to the right day? */
  270. int delta = (7+tm.tm_wday-wday)%7;
  271. /* If we are on the right day, but the changeover hasn't happened yet,
  272. * then subtract a whole week. */
  273. if (delta == 0 && before)
  274. delta = 7;
  275. tm.tm_mday -= delta;
  276. if (get_end)
  277. tm.tm_mday += 7;
  278. break;
  279. }
  280. case UNIT_DAY:
  281. if (before)
  282. --tm.tm_mday;
  283. if (get_end)
  284. ++tm.tm_mday;
  285. break;
  286. default:
  287. tor_assert(0);
  288. }
  289. tm.tm_hour = cfg_start_hour;
  290. tm.tm_min = cfg_start_min;
  291. tm.tm_sec = 0;
  292. tm.tm_isdst = -1; /* Autodetect DST */
  293. return mktime(&tm);
  294. }
  295. /** Return the start of the accounting period containing the time
  296. * <b>now</b>. */
  297. static time_t
  298. start_of_accounting_period_containing(time_t now)
  299. {
  300. return edge_of_accounting_period_containing(now, 0);
  301. }
  302. /** Return the start of the accounting period that comes after the one
  303. * containing the time <b>now</b>. */
  304. static time_t
  305. start_of_accounting_period_after(time_t now)
  306. {
  307. return edge_of_accounting_period_containing(now, 1);
  308. }
  309. /** Initialize the accounting subsystem. */
  310. void
  311. configure_accounting(time_t now)
  312. {
  313. /* Try to remember our recorded usage. */
  314. if (!interval_start_time)
  315. read_bandwidth_usage(); /* If we fail, we'll leave values at zero, and
  316. * reset below.*/
  317. if (!interval_start_time ||
  318. start_of_accounting_period_after(interval_start_time) <= now) {
  319. /* We didn't have recorded usage, or we don't have recorded usage
  320. * for this interval. Start a new interval. */
  321. log_info(LD_ACCT, "Starting new accounting interval.");
  322. reset_accounting(now);
  323. } else if (interval_start_time ==
  324. start_of_accounting_period_containing(interval_start_time)) {
  325. log_info(LD_ACCT, "Continuing accounting interval.");
  326. /* We are in the interval we thought we were in. Do nothing.*/
  327. interval_end_time = start_of_accounting_period_after(interval_start_time);
  328. } else {
  329. log_warn(LD_ACCT,
  330. "Mismatched accounting interval; starting a fresh one.");
  331. reset_accounting(now);
  332. }
  333. accounting_set_wakeup_time();
  334. }
  335. /** Set expected_bandwidth_usage based on how much we sent/received
  336. * per minute last interval (if we were up for at least 30 minutes),
  337. * or based on our declared bandwidth otherwise. */
  338. static void
  339. update_expected_bandwidth(void)
  340. {
  341. uint64_t used, expected;
  342. uint64_t max_configured = (get_options()->BandwidthRate * 60);
  343. if (n_seconds_active_in_interval < 1800) {
  344. /* If we haven't gotten enough data last interval, set 'expected'
  345. * to 0. This will set our wakeup to the start of the interval.
  346. * Next interval, we'll choose our starting time based on how much
  347. * we sent this interval.
  348. */
  349. expected = 0;
  350. } else {
  351. used = n_bytes_written_in_interval < n_bytes_read_in_interval ?
  352. n_bytes_read_in_interval : n_bytes_written_in_interval;
  353. expected = used / (n_seconds_active_in_interval / 60);
  354. if (expected > max_configured)
  355. expected = max_configured;
  356. }
  357. expected_bandwidth_usage = expected;
  358. }
  359. /** Called at the start of a new accounting interval: reset our
  360. * expected bandwidth usage based on what happened last time, set up
  361. * the start and end of the interval, and clear byte/time totals.
  362. */
  363. static void
  364. reset_accounting(time_t now)
  365. {
  366. log_info(LD_ACCT, "Starting new accounting interval.");
  367. update_expected_bandwidth();
  368. interval_start_time = start_of_accounting_period_containing(now);
  369. interval_end_time = start_of_accounting_period_after(interval_start_time);
  370. n_bytes_read_in_interval = 0;
  371. n_bytes_written_in_interval = 0;
  372. n_seconds_active_in_interval = 0;
  373. }
  374. /** Return true iff we should save our bandwidth usage to disk. */
  375. static INLINE int
  376. time_to_record_bandwidth_usage(time_t now)
  377. {
  378. /* Note every 600 sec */
  379. #define NOTE_INTERVAL (600)
  380. /* Or every 20 megabytes */
  381. #define NOTE_BYTES 20*(1024*1024)
  382. static uint64_t last_read_bytes_noted = 0;
  383. static uint64_t last_written_bytes_noted = 0;
  384. static time_t last_time_noted = 0;
  385. if (last_time_noted + NOTE_INTERVAL <= now ||
  386. last_read_bytes_noted + NOTE_BYTES <= n_bytes_read_in_interval ||
  387. last_written_bytes_noted + NOTE_BYTES <= n_bytes_written_in_interval ||
  388. (interval_end_time && interval_end_time <= now)) {
  389. last_time_noted = now;
  390. last_read_bytes_noted = n_bytes_read_in_interval;
  391. last_written_bytes_noted = n_bytes_written_in_interval;
  392. return 1;
  393. }
  394. return 0;
  395. }
  396. /** Invoked once per second. Checks whether it is time to hibernate,
  397. * record bandwidth used, etc. */
  398. void
  399. accounting_run_housekeeping(time_t now)
  400. {
  401. if (now >= interval_end_time) {
  402. configure_accounting(now);
  403. }
  404. if (time_to_record_bandwidth_usage(now)) {
  405. if (accounting_record_bandwidth_usage(now, get_or_state())) {
  406. log_warn(LD_FS, "Couldn't record bandwidth usage to disk.");
  407. }
  408. }
  409. }
  410. /** When we have no idea how fast we are, how long do we assume it will take
  411. * us to exhaust our bandwidth? */
  412. #define GUESS_TIME_TO_USE_BANDWIDTH (24*60*60)
  413. /** Based on our interval and our estimated bandwidth, choose a
  414. * deterministic (but random-ish) time to wake up. */
  415. static void
  416. accounting_set_wakeup_time(void)
  417. {
  418. char buf[ISO_TIME_LEN+1];
  419. char digest[DIGEST_LEN];
  420. crypto_digest_env_t *d_env;
  421. int time_in_interval;
  422. uint64_t time_to_exhaust_bw;
  423. int time_to_consider;
  424. if (! identity_key_is_set()) {
  425. if (init_keys() < 0) {
  426. log_err(LD_BUG, "Error initializing keys");
  427. tor_assert(0);
  428. }
  429. }
  430. format_iso_time(buf, interval_start_time);
  431. crypto_pk_get_digest(get_identity_key(), digest);
  432. d_env = crypto_new_digest_env();
  433. crypto_digest_add_bytes(d_env, buf, ISO_TIME_LEN);
  434. crypto_digest_add_bytes(d_env, digest, DIGEST_LEN);
  435. crypto_digest_get_digest(d_env, digest, DIGEST_LEN);
  436. crypto_free_digest_env(d_env);
  437. if (!expected_bandwidth_usage) {
  438. char buf1[ISO_TIME_LEN+1];
  439. char buf2[ISO_TIME_LEN+1];
  440. format_local_iso_time(buf1, interval_start_time);
  441. format_local_iso_time(buf2, interval_end_time);
  442. time_to_exhaust_bw = GUESS_TIME_TO_USE_BANDWIDTH;
  443. interval_wakeup_time = interval_start_time;
  444. log_notice(LD_ACCT,
  445. "Configured hibernation. This interval begins at %s "
  446. "and ends at %s. We have no prior estimate for bandwidth, so "
  447. "we will start out awake and hibernate when we exhaust our quota.",
  448. buf1, buf2);
  449. return;
  450. }
  451. time_in_interval = interval_end_time - interval_start_time;
  452. time_to_exhaust_bw =
  453. (get_options()->AccountingMax/expected_bandwidth_usage)*60;
  454. if (time_to_exhaust_bw > TIME_MAX) {
  455. time_to_exhaust_bw = TIME_MAX;
  456. time_to_consider = 0;
  457. } else {
  458. time_to_consider = time_in_interval - (int)time_to_exhaust_bw;
  459. }
  460. if (time_to_consider<=0) {
  461. interval_wakeup_time = interval_start_time;
  462. } else {
  463. /* XXX can we simplify this just by picking a random (non-deterministic)
  464. * time to be up? If we go down and come up, then we pick a new one. Is
  465. * that good enough? -RD */
  466. /* This is not a perfectly unbiased conversion, but it is good enough:
  467. * in the worst case, the first half of the day is 0.06 percent likelier
  468. * to be chosen than the last half. */
  469. interval_wakeup_time = interval_start_time +
  470. (get_uint32(digest) % time_to_consider);
  471. format_iso_time(buf, interval_wakeup_time);
  472. }
  473. {
  474. char buf1[ISO_TIME_LEN+1];
  475. char buf2[ISO_TIME_LEN+1];
  476. char buf3[ISO_TIME_LEN+1];
  477. char buf4[ISO_TIME_LEN+1];
  478. time_t down_time;
  479. if (interval_wakeup_time+time_to_exhaust_bw > TIME_MAX)
  480. down_time = TIME_MAX;
  481. else
  482. down_time = (time_t)(interval_wakeup_time+time_to_exhaust_bw);
  483. if (down_time>interval_end_time)
  484. down_time = interval_end_time;
  485. format_local_iso_time(buf1, interval_start_time);
  486. format_local_iso_time(buf2, interval_wakeup_time);
  487. format_local_iso_time(buf3, down_time);
  488. format_local_iso_time(buf4, interval_end_time);
  489. log_notice(LD_ACCT,
  490. "Configured hibernation. This interval began at %s; "
  491. "the scheduled wake-up time %s %s; "
  492. "we expect%s to exhaust our quota for this interval around %s; "
  493. "the next interval begins at %s (all times local)",
  494. buf1,
  495. time(NULL)<interval_wakeup_time?"is":"was", buf2,
  496. time(NULL)<down_time?"":"ed", buf3,
  497. buf4);
  498. }
  499. }
  500. /* This rounds 0 up to 1000, but that's actually a feature. */
  501. #define ROUND_UP(x) (((x) + 0x3ff) & ~0x3ff)
  502. #define BW_ACCOUNTING_VERSION 1
  503. /** Save all our bandwidth tracking information to disk. Return 0 on
  504. * success, -1 on failure. */
  505. int
  506. accounting_record_bandwidth_usage(time_t now, or_state_t *state)
  507. {
  508. char buf[128];
  509. char fname[512];
  510. char time1[ISO_TIME_LEN+1];
  511. char time2[ISO_TIME_LEN+1];
  512. char *cp = buf;
  513. time_t tmp;
  514. int r = 0;
  515. uint64_t expected;
  516. static time_t last_recorded = 0;
  517. /* First, update bw_accounting. Until 0.1.2.5-alpha, this was the only place
  518. * we stored this information. The format is:
  519. * Version\nTime\nTime\nRead\nWrite\nSeconds\nExpected-Rate\n */
  520. format_iso_time(time1, interval_start_time);
  521. format_iso_time(time2, now);
  522. /* now check to see if they're valid times -- if they're not,
  523. * and we write them, then tor will refuse to start next time. */
  524. if (parse_iso_time(time1, &tmp) || parse_iso_time(time2, &tmp)) {
  525. log_warn(LD_ACCT, "Created a time that we refused to parse.");
  526. return -1;
  527. }
  528. expected = expected_bandwidth_usage;
  529. /* Cap this value, since older versions won't parse a uint64_t here. */
  530. if (expected > UINT32_MAX)
  531. expected = UINT32_MAX;
  532. tor_snprintf(cp, sizeof(buf),
  533. "%d\n%s\n%s\n"U64_FORMAT"\n"U64_FORMAT"\n%lu\n%lu\n",
  534. BW_ACCOUNTING_VERSION,
  535. time1,
  536. time2,
  537. U64_PRINTF_ARG(ROUND_UP(n_bytes_read_in_interval)),
  538. U64_PRINTF_ARG(ROUND_UP(n_bytes_written_in_interval)),
  539. (unsigned long)n_seconds_active_in_interval,
  540. (unsigned long)expected);
  541. tor_snprintf(fname, sizeof(fname), "%s"PATH_SEPARATOR"bw_accounting",
  542. get_options()->DataDirectory);
  543. if (!get_options()->AvoidDiskWrites || (last_recorded + 3600 < now)) {
  544. r = write_str_to_file(fname, buf, 0);
  545. last_recorded = now;
  546. }
  547. /* Now update the state */
  548. state->AccountingIntervalStart = interval_start_time;
  549. state->AccountingBytesReadInInterval = ROUND_UP(n_bytes_read_in_interval);
  550. state->AccountingBytesWrittenInInterval =
  551. ROUND_UP(n_bytes_written_in_interval);
  552. state->AccountingSecondsActive = n_seconds_active_in_interval;
  553. state->AccountingExpectedUsage = expected_bandwidth_usage;
  554. or_state_mark_dirty(state,
  555. now+(get_options()->AvoidDiskWrites ? 7200 : 60));
  556. return r;
  557. }
  558. #undef ROUND_UP
  559. /** Read stored accounting information from disk. Return 0 on success;
  560. * return -1 and change nothing on failure. */
  561. static int
  562. read_bandwidth_usage(void)
  563. {
  564. char *s = NULL;
  565. char fname[512];
  566. time_t t1, t2;
  567. uint64_t n_read, n_written;
  568. uint32_t expected_bw, n_seconds;
  569. smartlist_t *elts = NULL;
  570. int ok, use_state=0, r=-1;
  571. or_state_t *state = get_or_state();
  572. tor_snprintf(fname, sizeof(fname), "%s/bw_accounting",
  573. get_options()->DataDirectory);
  574. elts = smartlist_create();
  575. if ((s = read_file_to_str(fname, 0, NULL)) == NULL) {
  576. /* We have an old-format bw_accounting file. */
  577. use_state = 1;
  578. }
  579. if (!use_state) {
  580. smartlist_split_string(elts, s, "\n",
  581. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
  582. tor_free(s);
  583. if (smartlist_len(elts)<1 ||
  584. atoi(smartlist_get(elts,0)) != BW_ACCOUNTING_VERSION) {
  585. log_warn(LD_ACCT, "Unrecognized bw_accounting file version: %s",
  586. (const char*)smartlist_get(elts,0));
  587. use_state = 1;
  588. }
  589. }
  590. if (!use_state && smartlist_len(elts) < 7) {
  591. log_warn(LD_ACCT, "Corrupted bw_accounting file: %d lines",
  592. smartlist_len(elts));
  593. use_state = 1;
  594. }
  595. if (!use_state && parse_iso_time(smartlist_get(elts,2), &t2)) {
  596. log_warn(LD_ACCT, "Error parsing bandwidth usage last-written time");
  597. use_state = 1;
  598. }
  599. if (use_state || t2 <= state->LastWritten) {
  600. /* Okay; it looks like the state file is more up-to-date than the
  601. * bw_accounting file, or the bw_accounting file is nonexistant,
  602. * or the bw_accounting file is corrupt.
  603. */
  604. log_info(LD_ACCT, "Reading bandwdith accounting data from state file");
  605. n_bytes_read_in_interval = state->AccountingBytesReadInInterval;
  606. n_bytes_written_in_interval = state->AccountingBytesWrittenInInterval;
  607. n_seconds_active_in_interval = state->AccountingSecondsActive;
  608. interval_start_time = state->AccountingIntervalStart;
  609. expected_bandwidth_usage = state->AccountingExpectedUsage;
  610. r = 0;
  611. goto done;
  612. }
  613. if (parse_iso_time(smartlist_get(elts,1), &t1)) {
  614. log_warn(LD_ACCT, "Error parsing bandwidth usage start time.");
  615. goto done;
  616. }
  617. n_read = tor_parse_uint64(smartlist_get(elts,3), 10, 0, UINT64_MAX,
  618. &ok, NULL);
  619. if (!ok) {
  620. log_warn(LD_ACCT, "Error parsing number of bytes read");
  621. goto done;
  622. }
  623. n_written = tor_parse_uint64(smartlist_get(elts,4), 10, 0, UINT64_MAX,
  624. &ok, NULL);
  625. if (!ok) {
  626. log_warn(LD_ACCT, "Error parsing number of bytes written");
  627. goto done;
  628. }
  629. n_seconds = (uint32_t)tor_parse_ulong(smartlist_get(elts,5), 10,0,ULONG_MAX,
  630. &ok, NULL);
  631. if (!ok) {
  632. log_warn(LD_ACCT, "Error parsing number of seconds live");
  633. goto done;
  634. }
  635. expected_bw =(uint32_t)tor_parse_ulong(smartlist_get(elts,6), 10,0,ULONG_MAX,
  636. &ok, NULL);
  637. if (!ok) {
  638. log_warn(LD_ACCT, "Error parsing expected bandwidth");
  639. goto done;
  640. }
  641. /* XXXX020 do something if the interval "starts" far in the future?
  642. * or do we alrady handle that. */
  643. n_bytes_read_in_interval = n_read;
  644. n_bytes_written_in_interval = n_written;
  645. n_seconds_active_in_interval = n_seconds;
  646. interval_start_time = t1;
  647. expected_bandwidth_usage = expected_bw;
  648. log_info(LD_ACCT,
  649. "Successfully read bandwidth accounting file written at %s "
  650. "for interval starting at %s. We have been active for %lu seconds in "
  651. "this interval. At the start of the interval, we expected to use "
  652. "about %lu KB per second. ("U64_FORMAT" bytes read so far, "
  653. U64_FORMAT" bytes written so far)",
  654. (char*)smartlist_get(elts,2),
  655. (char*)smartlist_get(elts,1),
  656. (unsigned long)n_seconds_active_in_interval,
  657. (unsigned long)(expected_bandwidth_usage*1024/60),
  658. U64_PRINTF_ARG(n_bytes_read_in_interval),
  659. U64_PRINTF_ARG(n_bytes_written_in_interval));
  660. r = 0;
  661. done:
  662. if (elts) {
  663. SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
  664. smartlist_free(elts);
  665. }
  666. return r;
  667. }
  668. /** Return true iff we have sent/received all the bytes we are willing
  669. * to send/receive this interval. */
  670. static int
  671. hibernate_hard_limit_reached(void)
  672. {
  673. uint64_t hard_limit = get_options()->AccountingMax;
  674. if (!hard_limit)
  675. return 0;
  676. return n_bytes_read_in_interval >= hard_limit
  677. || n_bytes_written_in_interval >= hard_limit;
  678. }
  679. /** Return true iff we have sent/received almost all the bytes we are willing
  680. * to send/receive this interval. */
  681. static int
  682. hibernate_soft_limit_reached(void)
  683. {
  684. uint64_t soft_limit = DBL_TO_U64(U64_TO_DBL(get_options()->AccountingMax)
  685. * .95);
  686. if (!soft_limit)
  687. return 0;
  688. return n_bytes_read_in_interval >= soft_limit
  689. || n_bytes_written_in_interval >= soft_limit;
  690. }
  691. /** Called when we get a SIGINT, or when bandwidth soft limit is
  692. * reached. Puts us into "loose hibernation": we don't accept new
  693. * connections, but we continue handling old ones. */
  694. static void
  695. hibernate_begin(hibernate_state_t new_state, time_t now)
  696. {
  697. connection_t *conn;
  698. or_options_t *options = get_options();
  699. if (new_state == HIBERNATE_STATE_EXITING &&
  700. hibernate_state != HIBERNATE_STATE_LIVE) {
  701. log_notice(LD_GENERAL,"Sigint received %s; exiting now.",
  702. hibernate_state == HIBERNATE_STATE_EXITING ?
  703. "a second time" : "while hibernating");
  704. tor_cleanup();
  705. exit(0);
  706. }
  707. /* close listeners. leave control listener(s). */
  708. while ((conn = connection_get_by_type(CONN_TYPE_OR_LISTENER)) ||
  709. (conn = connection_get_by_type(CONN_TYPE_AP_LISTENER)) ||
  710. (conn = connection_get_by_type(CONN_TYPE_AP_TRANS_LISTENER)) ||
  711. (conn = connection_get_by_type(CONN_TYPE_AP_DNS_LISTENER)) ||
  712. (conn = connection_get_by_type(CONN_TYPE_AP_NATD_LISTENER)) ||
  713. (conn = connection_get_by_type(CONN_TYPE_DIR_LISTENER))) {
  714. log_info(LD_NET,"Closing listener type %d", conn->type);
  715. connection_mark_for_close(conn);
  716. }
  717. /* XXX kill intro point circs */
  718. /* XXX upload rendezvous service descriptors with no intro points */
  719. if (new_state == HIBERNATE_STATE_EXITING) {
  720. log_notice(LD_GENERAL,"Interrupt: will shut down in %d seconds. Interrupt "
  721. "again to exit now.", options->ShutdownWaitLength);
  722. shutdown_time = time(NULL) + options->ShutdownWaitLength;
  723. } else { /* soft limit reached */
  724. hibernate_end_time = interval_end_time;
  725. }
  726. hibernate_state = new_state;
  727. accounting_record_bandwidth_usage(now, get_or_state());
  728. or_state_mark_dirty(get_or_state(),
  729. get_options()->AvoidDiskWrites ? now+600 : 0);
  730. }
  731. /** Called when we've been hibernating and our timeout is reached. */
  732. static void
  733. hibernate_end(hibernate_state_t new_state)
  734. {
  735. tor_assert(hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH ||
  736. hibernate_state == HIBERNATE_STATE_DORMANT);
  737. /* listeners will be relaunched in run_scheduled_events() in main.c */
  738. log_notice(LD_ACCT,"Hibernation period ended. Resuming normal activity.");
  739. hibernate_state = new_state;
  740. hibernate_end_time = 0; /* no longer hibernating */
  741. stats_n_seconds_working = 0; /* reset published uptime */
  742. }
  743. /** A wrapper around hibernate_begin, for when we get SIGINT. */
  744. void
  745. hibernate_begin_shutdown(void)
  746. {
  747. hibernate_begin(HIBERNATE_STATE_EXITING, time(NULL));
  748. }
  749. /** Return true iff we are currently hibernating. */
  750. int
  751. we_are_hibernating(void)
  752. {
  753. return hibernate_state != HIBERNATE_STATE_LIVE;
  754. }
  755. /** If we aren't currently dormant, close all connections and become
  756. * dormant. */
  757. static void
  758. hibernate_go_dormant(time_t now)
  759. {
  760. connection_t *conn;
  761. if (hibernate_state == HIBERNATE_STATE_DORMANT)
  762. return;
  763. else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
  764. hibernate_state = HIBERNATE_STATE_DORMANT;
  765. else
  766. hibernate_begin(HIBERNATE_STATE_DORMANT, now);
  767. log_notice(LD_ACCT,"Going dormant. Blowing away remaining connections.");
  768. /* Close all OR/AP/exit conns. Leave dir conns because we still want
  769. * to be able to upload server descriptors so people know we're still
  770. * running, and download directories so we can detect if we're obsolete.
  771. * Leave control conns because we still want to be controllable.
  772. */
  773. while ((conn = connection_get_by_type(CONN_TYPE_OR)) ||
  774. (conn = connection_get_by_type(CONN_TYPE_AP)) ||
  775. (conn = connection_get_by_type(CONN_TYPE_EXIT))) {
  776. if (CONN_IS_EDGE(conn))
  777. connection_edge_end(TO_EDGE_CONN(conn), END_STREAM_REASON_HIBERNATING);
  778. log_info(LD_NET,"Closing conn type %d", conn->type);
  779. if (conn->type == CONN_TYPE_AP) /* send socks failure if needed */
  780. connection_mark_unattached_ap(TO_EDGE_CONN(conn),
  781. END_STREAM_REASON_HIBERNATING);
  782. else
  783. connection_mark_for_close(conn);
  784. }
  785. if (now < interval_wakeup_time)
  786. hibernate_end_time = interval_wakeup_time;
  787. else
  788. hibernate_end_time = interval_end_time;
  789. accounting_record_bandwidth_usage(now, get_or_state());
  790. or_state_mark_dirty(get_or_state(),
  791. get_options()->AvoidDiskWrites ? now+600 : 0);
  792. }
  793. /** Called when hibernate_end_time has arrived. */
  794. static void
  795. hibernate_end_time_elapsed(time_t now)
  796. {
  797. char buf[ISO_TIME_LEN+1];
  798. /* The interval has ended, or it is wakeup time. Find out which. */
  799. accounting_run_housekeeping(now);
  800. if (interval_wakeup_time <= now) {
  801. /* The interval hasn't changed, but interval_wakeup_time has passed.
  802. * It's time to wake up and start being a server. */
  803. hibernate_end(HIBERNATE_STATE_LIVE);
  804. return;
  805. } else {
  806. /* The interval has changed, and it isn't time to wake up yet. */
  807. hibernate_end_time = interval_wakeup_time;
  808. format_iso_time(buf,interval_wakeup_time);
  809. if (hibernate_state != HIBERNATE_STATE_DORMANT) {
  810. /* We weren't sleeping before; we should sleep now. */
  811. log_notice(LD_ACCT,
  812. "Accounting period ended. Commencing hibernation until "
  813. "%s GMT", buf);
  814. hibernate_go_dormant(now);
  815. } else {
  816. log_notice(LD_ACCT,
  817. "Accounting period ended. This period, we will hibernate"
  818. " until %s GMT",buf);
  819. }
  820. }
  821. }
  822. /** Consider our environment and decide if it's time
  823. * to start/stop hibernating.
  824. */
  825. void
  826. consider_hibernation(time_t now)
  827. {
  828. int accounting_enabled = get_options()->AccountingMax != 0;
  829. char buf[ISO_TIME_LEN+1];
  830. /* If we're in 'exiting' mode, then we just shut down after the interval
  831. * elapses. */
  832. if (hibernate_state == HIBERNATE_STATE_EXITING) {
  833. tor_assert(shutdown_time);
  834. if (shutdown_time <= now) {
  835. log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
  836. tor_cleanup();
  837. exit(0);
  838. }
  839. return; /* if exiting soon, don't worry about bandwidth limits */
  840. }
  841. if (hibernate_state == HIBERNATE_STATE_DORMANT) {
  842. /* We've been hibernating because of bandwidth accounting. */
  843. tor_assert(hibernate_end_time);
  844. if (hibernate_end_time > now && accounting_enabled) {
  845. /* If we're hibernating, don't wake up until it's time, regardless of
  846. * whether we're in a new interval. */
  847. return ;
  848. } else {
  849. hibernate_end_time_elapsed(now);
  850. }
  851. }
  852. /* Else, we aren't hibernating. See if it's time to start hibernating, or to
  853. * go dormant. */
  854. if (hibernate_state == HIBERNATE_STATE_LIVE) {
  855. if (hibernate_soft_limit_reached()) {
  856. log_notice(LD_ACCT,
  857. "Bandwidth soft limit reached; commencing hibernation.");
  858. hibernate_begin(HIBERNATE_STATE_LOWBANDWIDTH, now);
  859. } else if (accounting_enabled && now < interval_wakeup_time) {
  860. format_local_iso_time(buf,interval_wakeup_time);
  861. log_notice(LD_ACCT,
  862. "Commencing hibernation. We will wake up at %s local time.",
  863. buf);
  864. hibernate_go_dormant(now);
  865. }
  866. }
  867. if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH) {
  868. if (!accounting_enabled) {
  869. hibernate_end_time_elapsed(now);
  870. } else if (hibernate_hard_limit_reached()) {
  871. hibernate_go_dormant(now);
  872. } else if (hibernate_end_time <= now) {
  873. /* The hibernation period ended while we were still in lowbandwidth.*/
  874. hibernate_end_time_elapsed(now);
  875. }
  876. }
  877. }
  878. /** Helper function: called when we get a GETINFO request for an
  879. * accounting-related key on the control connection <b>conn</b>. If we can
  880. * answer the request for <b>question</b>, then set *<b>answer</b> to a newly
  881. * allocated string holding the result. Otherwise, set *<b>answer</b> to
  882. * NULL. */
  883. int
  884. getinfo_helper_accounting(control_connection_t *conn,
  885. const char *question, char **answer)
  886. {
  887. (void) conn;
  888. if (!strcmp(question, "accounting/enabled")) {
  889. *answer = tor_strdup(accounting_is_enabled(get_options()) ? "1" : "0");
  890. } else if (!strcmp(question, "accounting/hibernating")) {
  891. if (hibernate_state == HIBERNATE_STATE_DORMANT)
  892. *answer = tor_strdup("hard");
  893. else if (hibernate_state == HIBERNATE_STATE_LOWBANDWIDTH)
  894. *answer = tor_strdup("soft");
  895. else
  896. *answer = tor_strdup("awake");
  897. } else if (!strcmp(question, "accounting/bytes")) {
  898. *answer = tor_malloc(32);
  899. tor_snprintf(*answer, 32, U64_FORMAT" "U64_FORMAT,
  900. U64_PRINTF_ARG(n_bytes_read_in_interval),
  901. U64_PRINTF_ARG(n_bytes_written_in_interval));
  902. } else if (!strcmp(question, "accounting/bytes-left")) {
  903. uint64_t limit = get_options()->AccountingMax;
  904. uint64_t read_left = 0, write_left = 0;
  905. if (n_bytes_read_in_interval < limit)
  906. read_left = limit - n_bytes_read_in_interval;
  907. if (n_bytes_written_in_interval < limit)
  908. write_left = limit - n_bytes_written_in_interval;
  909. *answer = tor_malloc(64);
  910. tor_snprintf(*answer, 64, U64_FORMAT" "U64_FORMAT,
  911. U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left));
  912. } else if (!strcmp(question, "accounting/interval-start")) {
  913. *answer = tor_malloc(ISO_TIME_LEN+1);
  914. format_iso_time(*answer, interval_start_time);
  915. } else if (!strcmp(question, "accounting/interval-wake")) {
  916. *answer = tor_malloc(ISO_TIME_LEN+1);
  917. format_iso_time(*answer, interval_wakeup_time);
  918. } else if (!strcmp(question, "accounting/interval-end")) {
  919. *answer = tor_malloc(ISO_TIME_LEN+1);
  920. format_iso_time(*answer, interval_end_time);
  921. } else {
  922. *answer = NULL;
  923. }
  924. return 0;
  925. }