Janitor.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "dht/Address.h"
  17. #include "dht/dhtcore/Janitor.h"
  18. #include "dht/dhtcore/Node.h"
  19. #include "dht/dhtcore/NodeList.h"
  20. #include "dht/dhtcore/RumorMill.h"
  21. #include "dht/dhtcore/RouterModule.h"
  22. #include "dht/dhtcore/SearchRunner.h"
  23. #include "dht/dhtcore/ReplySerializer.h"
  24. #include "benc/Object.h"
  25. #include "memory/Allocator.h"
  26. #include "util/AddrTools.h"
  27. #include "util/AverageRoller.h"
  28. #include "util/Bits.h"
  29. #include "util/events/EventBase.h"
  30. #include "util/Hex.h"
  31. #include "util/events/Timeout.h"
  32. #include "util/events/Time.h"
  33. #include <stdint.h>
  34. #include <stdbool.h>
  35. #define MAX_SEARCHES 10
  36. /**
  37. * The goal of this is to run searches in the local area of this node.
  38. * it searches for hashes every localMaintainenceSearchPeriod milliseconds.
  39. * it runs searches by picking hashes at random, if a hash is chosen and there is a
  40. * non-zero-reach node which services that space, it stops. This way it will run many
  41. * searches early on but as the number of known nodes increases, it begins to taper off.
  42. */
  43. struct Janitor
  44. {
  45. struct RouterModule* routerModule;
  46. struct NodeStore* nodeStore;
  47. struct SearchRunner* searchRunner;
  48. // Externally accessible RumorMill.
  49. // Used for direct peers and search results that are closer than the responder.
  50. struct RumorMill* rumorMill;
  51. // High priority RumorMill.
  52. // Used when the response could help split non-one-hop links.
  53. struct RumorMill* splitMill;
  54. // Low priority RumorMill.
  55. // Used to explore physically nearby nodes. By far the most used mill.
  56. struct RumorMill* idleMill;
  57. struct Timeout* timeout;
  58. struct Log* logger;
  59. uint64_t globalMaintainenceMilliseconds;
  60. uint64_t timeOfNextGlobalMaintainence;
  61. uint64_t localMaintainenceMilliseconds;
  62. struct Allocator* allocator;
  63. uint64_t timeOfNextSearchRepeat;
  64. uint64_t searchRepeatMilliseconds;
  65. struct EventBase* eventBase;
  66. struct Random* rand;
  67. /** Number of concurrent searches taking place. */
  68. int searches;
  69. // Used to keep dht healthy
  70. uint8_t keyspaceMaintainenceCounter;
  71. uint8_t keyspaceHoleDepthCounter;
  72. Identity
  73. };
  74. struct Janitor_Search
  75. {
  76. struct Janitor* janitor;
  77. struct Address best;
  78. uint8_t target[16];
  79. struct Allocator* alloc;
  80. Identity
  81. };
  82. static void responseCallback(struct RouterModule_Promise* promise,
  83. uint32_t lagMilliseconds,
  84. struct Address* from,
  85. Dict* result)
  86. {
  87. struct Janitor_Search* search = Identity_check((struct Janitor_Search*)promise->userData);
  88. if (from) {
  89. Bits_memcpyConst(&search->best, from, sizeof(struct Address));
  90. return;
  91. }
  92. search->janitor->searches--;
  93. if (!search->best.path) {
  94. Log_debug(search->janitor->logger, "Search completed with no nodes found");
  95. }
  96. Allocator_free(search->alloc);
  97. }
  98. static void search(uint8_t target[16], struct Janitor* janitor)
  99. {
  100. if (janitor->searches >= MAX_SEARCHES) {
  101. Log_debug(janitor->logger, "Skipping search because 20 are in progress");
  102. return;
  103. }
  104. #ifdef Log_DEBUG
  105. uint8_t targetStr[40];
  106. AddrTools_printIp(targetStr, target);
  107. Log_debug(janitor->logger, "Beginning search for [%s]", targetStr);
  108. #endif
  109. struct Allocator* searchAlloc = Allocator_child(janitor->allocator);
  110. struct RouterModule_Promise* rp =
  111. SearchRunner_search(target, janitor->searchRunner, searchAlloc);
  112. if (!rp) {
  113. Log_debug(janitor->logger, "SearchRunner_search() returned NULL, probably full.");
  114. Allocator_free(searchAlloc);
  115. return;
  116. }
  117. janitor->searches++;
  118. struct Janitor_Search* search = Allocator_clone(rp->alloc, (&(struct Janitor_Search) {
  119. .janitor = janitor,
  120. .alloc = searchAlloc,
  121. }));
  122. Identity_set(search);
  123. Bits_memcpyConst(search->target, target, 16);
  124. rp->callback = responseCallback;
  125. rp->userData = search;
  126. }
  127. static void searchNoDupe(uint8_t target[Address_SEARCH_TARGET_SIZE], struct Janitor* janitor)
  128. {
  129. // See if we're already searching for this address.
  130. struct Allocator* seachListAlloc = Allocator_child(janitor->allocator);
  131. struct SearchRunner_SearchData* searchData;
  132. for (int i = 0; i < SearchRunner_DEFAULT_MAX_CONCURRENT_SEARCHES; i++) {
  133. searchData = SearchRunner_showActiveSearch(janitor->searchRunner,
  134. i,
  135. seachListAlloc);
  136. if (!searchData) { continue; }
  137. if (!Bits_memcmp(searchData->target, target, Address_SEARCH_TARGET_SIZE)) {
  138. // Already have a search going for this address, so nothing to do.
  139. Allocator_free(seachListAlloc);
  140. return;
  141. }
  142. }
  143. Allocator_free(seachListAlloc);
  144. // There's no search running for this address, so we start one.
  145. search(target, janitor);
  146. #ifdef Log_DEBUG
  147. uint8_t addrStr[40];
  148. AddrTools_printIp(addrStr, target);
  149. Log_debug(janitor->logger, "No active search for [%s], starting one.", addrStr);
  150. #endif
  151. }
  152. /**
  153. * For a Distributed Hash Table to work, each node must know a valid next hop in the search,
  154. * if such a thing exists.
  155. *
  156. * In a Kademlia DHT, can be done by organizing nodes into k-buckets. These are collections
  157. * of k nodes for which the first n bits of your node IDs match. Among other things, k-buckets
  158. * allow a node to identify holes in their lookup table and fill them. If the nth bucket is empty,
  159. * it means your node knows of no valid next hop for any key matching the first n bits of your
  160. * address and differing in bit n+1.
  161. *
  162. * Without going to the trouble of organizing nodes in the buckets, this function iterates
  163. * bitwise over keyspace, to identify the same kind of routing holes.
  164. * It then dispatches a search for the first (largest) such hole in keyspace that it finds.
  165. */
  166. static void plugLargestKeyspaceHole(struct Janitor* janitor, bool force)
  167. {
  168. struct Address addr = *janitor->nodeStore->selfAddress;
  169. int byte = 0;
  170. int bit = 0;
  171. uint8_t holeDepth = 0;
  172. for (uint8_t i = 0; i < 128 ; i++) {
  173. // Bitwise walk across keyspace
  174. if (63 < i && i < 72) {
  175. // We want to leave the 0xfc alone
  176. continue;
  177. }
  178. // Figure out which bit of the address to flip for this step in keyspace.
  179. // This looks ugly because of the rot64 done in distance calculations.
  180. if (i < 64) { byte = 8 + (i/8); }
  181. else { byte = (i/8) - 8; }
  182. bit = (i % 8);
  183. // Flip that bit.
  184. addr.ip6.bytes[byte] = addr.ip6.bytes[byte] ^ (0x80 >> bit);
  185. // See if we know a valid next hop.
  186. struct Node_Two* n = RouterModule_lookup(addr.ip6.bytes, janitor->routerModule);
  187. if (n) {
  188. // We do know a valid next hop, so flip the bit back and continue.
  189. addr.ip6.bytes[byte] = addr.ip6.bytes[byte] ^ (0x80 >> bit);
  190. continue;
  191. }
  192. // We found a hole! Exit loop and let the search trigger.
  193. holeDepth = i;
  194. break;
  195. }
  196. // Search for a node that satisfies the address requirements to fill the hole.
  197. if (holeDepth != janitor->keyspaceHoleDepthCounter || force) {
  198. Log_debug(janitor->logger, "Setting keyspaceHoleDepthCounter [%u]", holeDepth);
  199. janitor->keyspaceHoleDepthCounter = holeDepth;
  200. searchNoDupe(addr.ip6.bytes, janitor);
  201. }
  202. }
  203. // Counterpart to plugLargestKeyspaceHole, used to refresh reach of known routes with a search.
  204. // This also finds redundant routes for that area of keyspace, which helps the DHT some.
  205. static void keyspaceMaintainence(struct Janitor* janitor)
  206. {
  207. struct Address addr = *janitor->nodeStore->selfAddress;
  208. int byte = 0;
  209. int bit = 0;
  210. // Restart cycle if we've already finished it.
  211. if (janitor->keyspaceMaintainenceCounter > 127) {
  212. janitor->keyspaceMaintainenceCounter = 0;
  213. }
  214. for (;janitor->keyspaceMaintainenceCounter < 128;
  215. janitor->keyspaceMaintainenceCounter++) {
  216. // Just to make referring to this thing quicker
  217. int i = janitor->keyspaceMaintainenceCounter;
  218. if (63 < i && i < 72) {
  219. // We want to leave the 0xfc alone
  220. continue;
  221. }
  222. // Figure out which bit of the address to flip for this step in keyspace.
  223. // This looks ugly because of the rot64 done in distance calculations.
  224. if (i < 64) { byte = 8 + (i/8); }
  225. else { byte = (i/8) - 8; }
  226. bit = (i % 8);
  227. // Flip that bit.
  228. addr.ip6.bytes[byte] = addr.ip6.bytes[byte] ^ (0x80 >> bit);
  229. // See if we know a valid next hop.
  230. struct Node_Two* n = RouterModule_lookup(addr.ip6.bytes, janitor->routerModule);
  231. if (n) {
  232. // Start the next search 1 step further into keyspace.
  233. janitor->keyspaceMaintainenceCounter = i+1;
  234. break;
  235. }
  236. // Clean up address and move further into keyspace.
  237. addr.ip6.bytes[byte] = addr.ip6.bytes[byte] ^ (0x80 >> bit);
  238. continue;
  239. }
  240. // Search for a node that satisfies the address requirements to fill the hole.
  241. // Should end up self-searching in the event that we're all the way through keyspace.
  242. searchNoDupe(addr.ip6.bytes, janitor);
  243. }
  244. static void peersResponseCallback(struct RouterModule_Promise* promise,
  245. uint32_t lagMilliseconds,
  246. struct Address* from,
  247. Dict* result)
  248. {
  249. struct Janitor* janitor = Identity_check((struct Janitor*)promise->userData);
  250. if (!from) { return; }
  251. struct Address_List* addresses =
  252. ReplySerializer_parse(from, result, janitor->logger, promise->alloc);
  253. struct Node_Two* parent = NodeStore_nodeForAddr(janitor->nodeStore, from->ip6.bytes);
  254. if (!parent) { return; }
  255. // Figure out if this node has any split-able links.
  256. bool hasSplitableLinks = false;
  257. struct Node_Link* link = NodeStore_nextLink(parent, NULL);
  258. while (link) {
  259. if (!Node_isOneHopLink(link)) {
  260. hasSplitableLinks = true;
  261. break;
  262. }
  263. link = NodeStore_nextLink(parent, link);
  264. }
  265. int loopCount = 0;
  266. for (int i = 0; addresses && i < addresses->length; i++) {
  267. struct Node_Link* nl = NodeStore_linkForPath(janitor->nodeStore, addresses->elems[i].path);
  268. if (!nl) {
  269. addresses->elems[i].path = NodeStore_optimizePath(janitor->nodeStore,
  270. addresses->elems[i].path);
  271. if (hasSplitableLinks) {
  272. RumorMill_addNode(janitor->splitMill, &addresses->elems[i]);
  273. } else {
  274. RumorMill_addNode(janitor->idleMill, &addresses->elems[i]);
  275. }
  276. } else if (!Address_isSameIp(&addresses->elems[i], &nl->child->address)) {
  277. // they're telling us about themselves, how helpful...
  278. if (nl && nl->child == parent) { continue; }
  279. if (nl->parent != parent) {
  280. #ifdef Log_INFO
  281. uint8_t newAddr[60];
  282. Address_print(newAddr, from);
  283. uint8_t labelStr[20];
  284. AddrTools_printPath(labelStr, nl->cannonicalLabel);
  285. Log_info(janitor->logger, "Apparently [%s] reported [%s] as it's peer",
  286. newAddr, labelStr);
  287. #endif
  288. continue;
  289. }
  290. #ifdef Log_INFO
  291. uint8_t newAddr[60];
  292. Address_print(newAddr, from);
  293. Log_info(janitor->logger, "Apparently [%s] has renumbered it's switch", newAddr);
  294. #endif
  295. link = NodeStore_nextLink(parent, NULL);
  296. while (link) {
  297. struct Node_Link* nextLink = NodeStore_nextLink(parent, link);
  298. NodeStore_unlinkNodes(janitor->nodeStore, link);
  299. link = nextLink;
  300. // restart from the beginning...
  301. i = 0;
  302. Assert_true(!loopCount);
  303. }
  304. Assert_true(!NodeStore_nextLink(parent, NULL));
  305. loopCount++;
  306. }
  307. }
  308. }
  309. static void checkPeers(struct Janitor* janitor, struct Node_Two* n)
  310. {
  311. // Lets check for non-one-hop links at each node along the path between us and this node.
  312. uint64_t path = n->address.path;
  313. struct Node_Link* link = NULL;
  314. for (;;) {
  315. link = NodeStore_firstHopInPath(janitor->nodeStore, path, &path, link);
  316. if (!link) { return; }
  317. if (link->parent == janitor->nodeStore->selfNode) { continue; }
  318. struct Node_Link* l = NULL;
  319. do {
  320. l = NodeStore_nextLink(link->child, l);
  321. if (l && (!Node_isOneHopLink(l) || Node_getReach(link->parent) == 0)) {
  322. struct RouterModule_Promise* rp =
  323. RouterModule_getPeers(&link->parent->address, l->cannonicalLabel, 0,
  324. janitor->routerModule, janitor->allocator);
  325. rp->callback = peersResponseCallback;
  326. rp->userData = janitor;
  327. // Only send max 1 getPeers req per second.
  328. return;
  329. }
  330. } while (l);
  331. }
  332. }
  333. // Iterate over all nodes in the table. Try to split any split-able links.
  334. static void splitLinks(struct Janitor* janitor)
  335. {
  336. uint32_t index = 0;
  337. struct Node_Two* node = NodeStore_dumpTable(janitor->nodeStore, index);
  338. while (node) {
  339. struct Node_Link* bestParent = Node_getBestParent(node);
  340. if (bestParent) {
  341. struct Node_Link* link = NodeStore_nextLink(node, NULL);
  342. while (link) {
  343. if (!Node_isOneHopLink(link)) {
  344. RumorMill_addNode(janitor->splitMill, &node->address);
  345. break;
  346. }
  347. link = NodeStore_nextLink(node, link);
  348. }
  349. }
  350. index++;
  351. node = NodeStore_dumpTable(janitor->nodeStore, index);
  352. }
  353. }
  354. static void maintanenceCycle(void* vcontext)
  355. {
  356. struct Janitor* const janitor = Identity_check((struct Janitor*) vcontext);
  357. uint64_t now = Time_currentTimeMilliseconds(janitor->eventBase);
  358. uint64_t nextTimeout = (janitor->localMaintainenceMilliseconds / 2);
  359. nextTimeout += Random_uint32(janitor->rand) % (nextTimeout * 2);
  360. Timeout_resetTimeout(janitor->timeout, nextTimeout);
  361. if (janitor->nodeStore->nodeCount == 0 && janitor->rumorMill->count == 0) {
  362. if (now > janitor->timeOfNextGlobalMaintainence) {
  363. Log_warn(janitor->logger,
  364. "No nodes in routing table, check network connection and configuration.");
  365. janitor->timeOfNextGlobalMaintainence += janitor->globalMaintainenceMilliseconds;
  366. }
  367. return;
  368. }
  369. struct Address addr = { .protocolVersion = 0 };
  370. if (RumorMill_getNode(janitor->splitMill, &addr)) {
  371. // ping a link-splitting node from the high-priority ping queue
  372. addr.path = NodeStore_optimizePath(janitor->nodeStore, addr.path);
  373. if (NodeStore_optimizePath_INVALID != addr.path) {
  374. struct RouterModule_Promise* rp =
  375. RouterModule_getPeers(&addr,
  376. Random_uint32(janitor->rand),
  377. 0,
  378. janitor->routerModule,
  379. janitor->allocator);
  380. rp->callback = peersResponseCallback;
  381. rp->userData = janitor;
  382. #ifdef Log_DEBUG
  383. uint8_t addrStr[60];
  384. Address_print(addrStr, &addr);
  385. Log_debug(janitor->logger, "Pinging possible node [%s] from "
  386. "priority RumorMill", addrStr);
  387. #endif
  388. }
  389. } else if (RumorMill_getNode(janitor->rumorMill, &addr)) {
  390. // ping a node from the ping normal-priority queue
  391. addr.path = NodeStore_optimizePath(janitor->nodeStore, addr.path);
  392. if (NodeStore_optimizePath_INVALID != addr.path) {
  393. struct RouterModule_Promise* rp =
  394. RouterModule_getPeers(&addr,
  395. Random_uint32(janitor->rand),
  396. 0,
  397. janitor->routerModule,
  398. janitor->allocator);
  399. rp->callback = peersResponseCallback;
  400. rp->userData = janitor;
  401. #ifdef Log_DEBUG
  402. uint8_t addrStr[60];
  403. Address_print(addrStr, &addr);
  404. Log_debug(janitor->logger, "Pinging possible node [%s] from "
  405. "normal RumorMill", addrStr);
  406. #endif
  407. }
  408. } else if (RumorMill_getNode(janitor->idleMill, &addr)) {
  409. // ping a node from the low-priority ping queue
  410. addr.path = NodeStore_optimizePath(janitor->nodeStore, addr.path);
  411. if (NodeStore_optimizePath_INVALID != addr.path) {
  412. struct RouterModule_Promise* rp =
  413. RouterModule_getPeers(&addr,
  414. Random_uint32(janitor->rand),
  415. 0,
  416. janitor->routerModule,
  417. janitor->allocator);
  418. rp->callback = peersResponseCallback;
  419. rp->userData = janitor;
  420. #ifdef Log_DEBUG
  421. uint8_t addrStr[60];
  422. Address_print(addrStr, &addr);
  423. Log_debug(janitor->logger, "Pinging possible node [%s] from "
  424. "idle RumorMill", addrStr);
  425. #endif
  426. }
  427. }
  428. // random search
  429. Random_bytes(janitor->rand, addr.ip6.bytes, 16);
  430. // Make this a valid address.
  431. addr.ip6.bytes[0] = 0xfc;
  432. struct Node_Two* n = RouterModule_lookup(addr.ip6.bytes, janitor->routerModule);
  433. // If the best next node doesn't exist or has 0 reach, run a local maintenance search.
  434. if (n == NULL || Node_getReach(n) == 0) {
  435. //search(addr.ip6.bytes, janitor);
  436. plugLargestKeyspaceHole(janitor, true);
  437. return;
  438. } else {
  439. checkPeers(janitor, n);
  440. }
  441. plugLargestKeyspaceHole(janitor, false);
  442. Log_debug(janitor->logger,
  443. "Global Mean Response Time: %u nodes [%d] links [%d]",
  444. RouterModule_globalMeanResponseTime(janitor->routerModule),
  445. janitor->nodeStore->nodeCount,
  446. janitor->nodeStore->linkCount);
  447. if (now > janitor->timeOfNextGlobalMaintainence) {
  448. //search(addr.ip6.bytes, janitor);
  449. plugLargestKeyspaceHole(janitor, true);
  450. keyspaceMaintainence(janitor);
  451. splitLinks(janitor);
  452. janitor->timeOfNextGlobalMaintainence += janitor->globalMaintainenceMilliseconds;
  453. }
  454. }
  455. struct Janitor* Janitor_new(uint64_t localMaintainenceMilliseconds,
  456. uint64_t globalMaintainenceMilliseconds,
  457. struct RouterModule* routerModule,
  458. struct NodeStore* nodeStore,
  459. struct SearchRunner* searchRunner,
  460. struct RumorMill* rumorMill,
  461. struct Log* logger,
  462. struct Allocator* allocator,
  463. struct EventBase* eventBase,
  464. struct Random* rand)
  465. {
  466. struct Allocator* alloc = Allocator_child(allocator);
  467. struct Janitor* janitor = Allocator_clone(alloc, (&(struct Janitor) {
  468. .eventBase = eventBase,
  469. .routerModule = routerModule,
  470. .nodeStore = nodeStore,
  471. .searchRunner = searchRunner,
  472. .rumorMill = rumorMill,
  473. .logger = logger,
  474. .globalMaintainenceMilliseconds = globalMaintainenceMilliseconds,
  475. .localMaintainenceMilliseconds = localMaintainenceMilliseconds,
  476. .keyspaceMaintainenceCounter = 0,
  477. .keyspaceHoleDepthCounter = 0,
  478. .allocator = alloc,
  479. .rand = rand
  480. }));
  481. Identity_set(janitor);
  482. janitor->splitMill = RumorMill_new(janitor->allocator, janitor->nodeStore->selfAddress, 16);
  483. janitor->idleMill = RumorMill_new(janitor->allocator, janitor->nodeStore->selfAddress, 64);
  484. janitor->timeOfNextGlobalMaintainence = Time_currentTimeMilliseconds(eventBase);
  485. janitor->timeout = Timeout_setTimeout(maintanenceCycle,
  486. janitor,
  487. localMaintainenceMilliseconds,
  488. eventBase,
  489. alloc);
  490. return janitor;
  491. }