ReachabilityCollector.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/AddressCalc.h"
  16. #include "dht/dhtcore/ReplySerializer.h"
  17. #include "net/SwitchPinger.h"
  18. #include "subnode/ReachabilityCollector.h"
  19. #include "util/log/Log.h"
  20. #include "util/Identity.h"
  21. #include "util/events/Timeout.h"
  22. #include "util/AddrTools.h"
  23. #include "util/events/Time.h"
  24. #include <stdbool.h>
  25. #include <inttypes.h>
  26. #define TIMEOUT_MILLISECONDS 10000
  27. struct ReachabilityCollector_pvt;
  28. struct PeerInfo_pvt
  29. {
  30. struct ReachabilityCollector_PeerInfo pub;
  31. struct ReachabilityCollector_pvt* rcp;
  32. // Next path to check when sending getPeers requests to our peer looking for ourselves.
  33. uint64_t pathToCheck;
  34. // For this 10 second period
  35. uint32_t sumOfLag;
  36. uint32_t lagSamples;
  37. uint32_t timeOfLastLagUpdate;
  38. uint64_t sumOfDropsLastSlot;
  39. uint64_t sumOfPacketsLastSlot;
  40. uint64_t sumOfKbLastSlot;
  41. uint64_t sumOfDrops;
  42. uint64_t sumOfPackets;
  43. uint64_t sumOfKb;
  44. // We have an outstanding query and we are waiting for a response from this peer.
  45. bool waitForResponse;
  46. struct Allocator* alloc;
  47. Identity
  48. };
  49. #define ArrayList_NAME OfPeerInfo_pvt
  50. #define ArrayList_TYPE struct PeerInfo_pvt
  51. #include "util/ArrayList.h"
  52. struct ReachabilityCollector_pvt
  53. {
  54. struct ReachabilityCollector pub;
  55. struct MsgCore* msgCore;
  56. struct Allocator* alloc;
  57. struct Log* log;
  58. struct BoilerplateResponder* br;
  59. struct Address* myAddr;
  60. struct EventBase* base;
  61. struct EncodingScheme* myScheme;
  62. struct SwitchPinger* sp;
  63. uint32_t resampleCycle;
  64. uint32_t linkStateSamples;
  65. struct ArrayList_OfPeerInfo_pvt* piList;
  66. Identity
  67. };
  68. static struct PeerInfo_pvt* piForLabel(struct ReachabilityCollector_pvt* rcp, uint64_t label)
  69. {
  70. for (int j = 0; j < rcp->piList->length; j++) {
  71. struct PeerInfo_pvt* pi0 = ArrayList_OfPeerInfo_pvt_get(rcp->piList, j);
  72. if (pi0->pub.addr.path == label) { return pi0; }
  73. }
  74. return NULL;
  75. }
  76. struct ReachabilityCollector_PeerInfo*
  77. ReachabilityCollector_piForLabel(struct ReachabilityCollector* rc, uint64_t label)
  78. {
  79. struct ReachabilityCollector_pvt* rcp = Identity_check((struct ReachabilityCollector_pvt*) rc);
  80. struct PeerInfo_pvt* out = piForLabel(rcp, label);
  81. return out ? &out->pub : NULL;
  82. }
  83. static struct PeerInfo_pvt* piForAddr(struct ReachabilityCollector_pvt* rcp, struct Address* addr)
  84. {
  85. struct PeerInfo_pvt* pi = piForLabel(rcp, addr->path);
  86. if (pi && Address_isSame(&pi->pub.addr, addr)) { return pi; }
  87. return NULL;
  88. }
  89. static int piOnFree(struct Allocator_OnFreeJob* j)
  90. {
  91. struct PeerInfo_pvt* pi = Identity_check((struct PeerInfo_pvt*) j->userData);
  92. struct ReachabilityCollector_pvt* rcp = Identity_check(pi->rcp);
  93. for (int j = 0; j < rcp->piList->length; j++) {
  94. struct PeerInfo_pvt* pi0 = ArrayList_OfPeerInfo_pvt_get(rcp->piList, j);
  95. if (pi0 != pi) { continue; }
  96. ArrayList_OfPeerInfo_pvt_remove(rcp->piList, j);
  97. }
  98. return 0;
  99. }
  100. static void mkNextRequest(struct ReachabilityCollector_pvt* rcp);
  101. void ReachabilityCollector_unreachable(struct ReachabilityCollector* rc, struct Address* nodeAddr)
  102. {
  103. struct ReachabilityCollector_pvt* rcp = Identity_check((struct ReachabilityCollector_pvt*) rc);
  104. struct PeerInfo_pvt* pi = piForAddr(rcp, nodeAddr);
  105. if (pi) {
  106. Log_debug(rcp->log, "Peer [%s] dropped",
  107. Address_toString(&pi->pub.addr, pi->alloc)->bytes);
  108. rcp->pub.onChange(&rcp->pub, &pi->pub.addr, NULL);
  109. Allocator_free(pi->alloc);
  110. return;
  111. }
  112. uint8_t buf[60];
  113. Address_print(buf, nodeAddr);
  114. Log_debug(rcp->log, "Nonexistant peer [%s] dropped", buf);
  115. return;
  116. }
  117. static void change0(struct ReachabilityCollector_pvt* rcp,
  118. struct Address* nodeAddr,
  119. struct Allocator* tempAlloc)
  120. {
  121. struct PeerInfo_pvt* oldPi = piForLabel(rcp, nodeAddr->path);
  122. if (oldPi) {
  123. if (Address_isSame(&oldPi->pub.addr, nodeAddr)) {
  124. // Log_debug(rcp->log, "Peer [%s] message, peer already registered",
  125. // Address_toString(nodeAddr, tempAlloc)->bytes);
  126. return;
  127. }
  128. Log_debug(rcp->log, "Dropping ghost peer [%s] to add peer [%s]",
  129. Address_toString(&oldPi->pub.addr, tempAlloc)->bytes,
  130. Address_toString(nodeAddr, tempAlloc)->bytes);
  131. ReachabilityCollector_unreachable(&rcp->pub, &oldPi->pub.addr);
  132. }
  133. struct Allocator* piAlloc = Allocator_child(rcp->alloc);
  134. struct PeerInfo_pvt* pi = Allocator_calloc(piAlloc, sizeof(struct PeerInfo_pvt), 1);
  135. Identity_set(pi);
  136. pi->rcp = rcp;
  137. Bits_memcpy(&pi->pub.addr, nodeAddr, Address_SIZE);
  138. pi->alloc = piAlloc;
  139. pi->pub.isQuerying = true;
  140. pi->pathToCheck = 1;
  141. Allocator_onFree(piAlloc, piOnFree, pi);
  142. ArrayList_OfPeerInfo_pvt_add(rcp->piList, pi);
  143. Log_debug(rcp->log, "Peer [%s] added", Address_toString(&pi->pub.addr, tempAlloc)->bytes);
  144. mkNextRequest(rcp);
  145. }
  146. void ReachabilityCollector_change(struct ReachabilityCollector* rc, struct Address* nodeAddr)
  147. {
  148. struct ReachabilityCollector_pvt* rcp = Identity_check((struct ReachabilityCollector_pvt*) rc);
  149. struct Allocator* tempAlloc = Allocator_child(rcp->alloc);
  150. change0(rcp, nodeAddr, tempAlloc);
  151. Allocator_free(tempAlloc);
  152. }
  153. struct Query {
  154. struct ReachabilityCollector_pvt* rcp;
  155. uint8_t targetPath[20];
  156. Identity
  157. };
  158. static void onReplyTimeout(struct MsgCore_Promise* prom)
  159. {
  160. struct Query* q = Identity_check((struct Query*) prom->userData);
  161. struct ReachabilityCollector_pvt* rcp =
  162. Identity_check((struct ReachabilityCollector_pvt*) q->rcp);
  163. Log_debug(rcp->log, "Timeout querying [%s]",
  164. Address_toString(prom->target, prom->alloc)->bytes);
  165. struct PeerInfo_pvt* pi = piForAddr(rcp, prom->target);
  166. if (pi) {
  167. pi->waitForResponse = false;
  168. }
  169. }
  170. static void latencyUpdate(
  171. struct ReachabilityCollector_pvt* rcp,
  172. struct PeerInfo_pvt* pip,
  173. uint32_t lag)
  174. {
  175. //Log_debug(rcp->log, "Latency update for [%" PRIx64 "] [%u]ms", pip->pub.addr.path, lag);
  176. pip->sumOfLag += lag;
  177. pip->lagSamples++;
  178. pip->timeOfLastLagUpdate = Time_currentTimeMilliseconds();
  179. }
  180. static void onReplyOld(Dict* msg, struct Address* src, struct MsgCore_Promise* prom)
  181. {
  182. struct Query* q = Identity_check((struct Query*) prom->userData);
  183. struct ReachabilityCollector_pvt* rcp =
  184. Identity_check((struct ReachabilityCollector_pvt*) q->rcp);
  185. if (!src) {
  186. onReplyTimeout(prom);
  187. mkNextRequest(rcp);
  188. return;
  189. }
  190. Log_debug(rcp->log, "Got response from peer [%s]",
  191. Address_toString(src, prom->alloc)->bytes);
  192. struct PeerInfo_pvt* pi = piForAddr(rcp, src);
  193. if (!pi) {
  194. Log_debug(rcp->log, "Got message from peer [%s] which is gone from list",
  195. Address_toString(src, prom->alloc)->bytes);
  196. return;
  197. }
  198. pi->waitForResponse = false;
  199. latencyUpdate(rcp, pi, prom->lag);
  200. struct Address_List* results = ReplySerializer_parse(src, msg, rcp->log, false, prom->alloc);
  201. uint64_t path = 1;
  202. if (!results) {
  203. Log_debug(rcp->log, "Got invalid getPeers reply from [%s]",
  204. Address_toString(src, prom->alloc)->bytes);
  205. return;
  206. }
  207. for (int i = results->length - 1; i >= 0; i--) {
  208. path = results->elems[i].path;
  209. if (Bits_memcmp(results->elems[i].ip6.bytes, rcp->myAddr->ip6.bytes, 16)) { continue; }
  210. // We're going to get one or more paths from them to us
  211. // We don't have a clue which one is correct, but we'll try to reduce
  212. // flap by sticking to one after we've adopted it.
  213. if (pi->pub.pathThemToUs == path) {
  214. Log_debug(rcp->log, "Found back-route for [%s] (exact match)",
  215. Address_toString(src, prom->alloc)->bytes);
  216. pi->pub.isQuerying = false;
  217. mkNextRequest(rcp);
  218. return;
  219. }
  220. }
  221. // Ok, no matches, if any of them mismatch then we're going to change
  222. for (int i = results->length - 1; i >= 0; i--) {
  223. path = results->elems[i].path;
  224. if (Bits_memcmp(results->elems[i].ip6.bytes, rcp->myAddr->ip6.bytes, 16)) { continue; }
  225. Log_debug(rcp->log, "Found back-route for [%s]",
  226. Address_toString(src, prom->alloc)->bytes);
  227. pi->pub.pathThemToUs = path;
  228. rcp->pub.onChange(&rcp->pub, src, &pi->pub);
  229. pi->pub.isQuerying = false;
  230. mkNextRequest(rcp);
  231. return;
  232. }
  233. if (results->length < 8) {
  234. // Peer's gp response does not include my addr, meaning the peer might not know us yet.
  235. // should wait peer sendPing (see InterfaceControl.c).
  236. Log_debug(rcp->log, "No back-route found for peer [%s]",
  237. Address_toString(src, prom->alloc)->bytes);
  238. pi->pathToCheck = 1;
  239. return;
  240. } else {
  241. pi->pathToCheck = path;
  242. }
  243. mkNextRequest(rcp);
  244. }
  245. static void queryOldPeer(struct ReachabilityCollector_pvt* rcp, struct PeerInfo_pvt* pi)
  246. {
  247. struct MsgCore_Promise* query =
  248. MsgCore_createQuery(rcp->msgCore, TIMEOUT_MILLISECONDS, rcp->alloc);
  249. struct Query* q = Allocator_calloc(query->alloc, sizeof(struct Query), 1);
  250. Identity_set(q);
  251. q->rcp = rcp;
  252. query->userData = q;
  253. query->cb = onReplyOld;
  254. Assert_true(AddressCalc_validAddress(pi->pub.addr.ip6.bytes));
  255. query->target = Address_clone(&pi->pub.addr, query->alloc);
  256. Assert_true(pi->pub.addr.path);
  257. Dict* d = query->msg = Dict_new(query->alloc);
  258. Dict_putStringCC(d, "q", "gp", query->alloc); // getpeers (if < v21)
  259. uint64_t label_be = Endian_hostToBigEndian64(pi->pathToCheck);
  260. uint8_t nearbyLabelBytes[8];
  261. Bits_memcpy(nearbyLabelBytes, &label_be, 8);
  262. AddrTools_printPath(q->targetPath, pi->pathToCheck);
  263. Log_debug(rcp->log, "Getting reverse path for old peer [%s] tar [%s]",
  264. Address_toString(&pi->pub.addr, query->alloc)->bytes,
  265. q->targetPath);
  266. Dict_putStringC(d, "tar",
  267. String_newBinary(nearbyLabelBytes, 8, query->alloc), query->alloc);
  268. BoilerplateResponder_addBoilerplate(rcp->br, d, &pi->pub.addr, query->alloc);
  269. pi->waitForResponse = true;
  270. }
  271. static void pingReply(Dict* msg, struct Address* src, struct MsgCore_Promise* prom)
  272. {
  273. struct ReachabilityCollector_pvt* rcp =
  274. Identity_check((struct ReachabilityCollector_pvt*) prom->userData);
  275. struct PeerInfo_pvt* pi = piForAddr(rcp, prom->target);
  276. if (!pi) {
  277. if (src) {
  278. Log_debug(rcp->log, "Got message from peer [%s] which is gone from list",
  279. Address_toString(src, prom->alloc)->bytes);
  280. }
  281. return;
  282. }
  283. pi->waitForResponse = false;
  284. if (!src) {
  285. //Log_debug(pf->log, "Ping timeout");
  286. return;
  287. }
  288. Log_debug(rcp->log, "Ping reply from [%s]", Address_toString(src, prom->alloc)->bytes);
  289. latencyUpdate(rcp, pi, prom->lag);
  290. }
  291. static void pingPeer(struct ReachabilityCollector_pvt* rcp, struct PeerInfo_pvt* pi)
  292. {
  293. struct MsgCore_Promise* qp = MsgCore_createQuery(rcp->msgCore, 0, rcp->alloc);
  294. Dict* dict = qp->msg = Dict_new(qp->alloc);
  295. qp->cb = pingReply;
  296. qp->userData = rcp;
  297. Assert_true(AddressCalc_validAddress(pi->pub.addr.ip6.bytes));
  298. Assert_true(pi->pub.addr.path);
  299. qp->target = Address_clone(&pi->pub.addr, qp->alloc);
  300. Log_debug(rcp->log, "Pinging [%s] to determine latency",
  301. Address_toString(qp->target, qp->alloc)->bytes);
  302. Dict_putStringCC(dict, "q", "pn", qp->alloc);
  303. BoilerplateResponder_addBoilerplate(rcp->br, dict, qp->target, qp->alloc);
  304. }
  305. static void peerResponseOK(
  306. struct ReachabilityCollector_pvt* rcp,
  307. struct PeerInfo_pvt* pi,
  308. struct SwitchPinger_Response* resp)
  309. {
  310. ReachabilityCollector_lagSample(&rcp->pub, resp->label, resp->milliseconds);
  311. Log_debug(rcp->log, "Found back-route for [%s] (sp)",
  312. Address_toString(&pi->pub.addr, resp->ping->pingAlloc)->bytes);
  313. pi->pub.pathThemToUs = resp->rpath;
  314. rcp->pub.onChange(&rcp->pub, &pi->pub.addr, &pi->pub);
  315. pi->pub.isQuerying = false;
  316. pingPeer(rcp, pi);
  317. mkNextRequest(rcp);
  318. }
  319. static void peerResponse(struct SwitchPinger_Response* resp, void* userData)
  320. {
  321. struct ReachabilityCollector_pvt* rcp =
  322. Identity_check((struct ReachabilityCollector_pvt*) userData);
  323. struct PeerInfo_pvt* pi = piForLabel(rcp, resp->label);
  324. if (!pi) {
  325. Log_debug(rcp->log, "Response from [%" PRIx64 "] with no peer info", resp->label);
  326. mkNextRequest(rcp);
  327. return;
  328. }
  329. Log_debug(rcp->log, "Response from [%" PRIx64 "] [%d]", resp->label, pi->waitForResponse);
  330. pi->waitForResponse = false;
  331. char* err = "";
  332. switch (resp->res) {
  333. case SwitchPinger_Result_OK: peerResponseOK(rcp, pi, resp); return;
  334. case SwitchPinger_Result_LABEL_MISMATCH: err = "LABEL_MISMATCH"; break;
  335. case SwitchPinger_Result_WRONG_DATA: err = "WRONG_DATA"; break;
  336. case SwitchPinger_Result_ERROR_RESPONSE: err = "ERROR_RESPONSE"; break;
  337. case SwitchPinger_Result_LOOP_ROUTE: err = "LOOP_ROUTE"; break;
  338. case SwitchPinger_Result_TIMEOUT: err = "TIMEOUT"; break;
  339. default: err = "unknown error"; break;
  340. }
  341. // We don't want to unreachable the peer here because this could be spurious packet loss
  342. Log_debug(rcp->log, "Error sending rpath query to peer [%s]", err);
  343. mkNextRequest(rcp);
  344. }
  345. static void queryBackroute(struct ReachabilityCollector_pvt* rcp, struct PeerInfo_pvt* pi)
  346. {
  347. if (pi->pub.addr.protocolVersion == 0) {
  348. // We don't know the version yet, we don't need to explicitly message the peer
  349. // here, we're going to discover it one way or another from talking to them.
  350. return;
  351. } else if (pi->pub.addr.protocolVersion < 21) {
  352. queryOldPeer(rcp, pi);
  353. return;
  354. }
  355. struct SwitchPinger_Ping* p = SwitchPinger_newPing(
  356. pi->pub.addr.path,
  357. String_CONST(""),
  358. 3000,
  359. peerResponse,
  360. rcp->alloc,
  361. rcp->sp);
  362. Assert_true(p);
  363. Log_debug(rcp->log, "Getting reverse path for new peer [%s]",
  364. Address_toString(&pi->pub.addr, p->pingAlloc)->bytes);
  365. p->type = SwitchPinger_Type_RPATH;
  366. p->onResponseContext = rcp;
  367. pi->waitForResponse = true;
  368. }
  369. static void mkNextRequest(struct ReachabilityCollector_pvt* rcp)
  370. {
  371. struct PeerInfo_pvt* pi = NULL;
  372. for (int i = 0; i < rcp->piList->length; i++) {
  373. pi = ArrayList_OfPeerInfo_pvt_get(rcp->piList, i);
  374. if (pi->pub.addr.protocolVersion == 0) { continue; }
  375. if (pi->pub.isQuerying && !pi->waitForResponse) { break; }
  376. }
  377. if (!pi || !pi->pub.isQuerying) {
  378. // if (rcp->piList->length > 0) {
  379. // Log_debug(rcp->log, "All [%u] peers have been queried", rcp->piList->length);
  380. // }
  381. return;
  382. }
  383. if (pi->waitForResponse) {
  384. Log_debug(rcp->log, "Message outstanding, waiting for peer to respond");
  385. return;
  386. }
  387. queryBackroute(rcp, pi);
  388. }
  389. static void cycle(void* vrc)
  390. {
  391. struct ReachabilityCollector_pvt* rcp = Identity_check((struct ReachabilityCollector_pvt*) vrc);
  392. mkNextRequest(rcp);
  393. // 10 second window is cut into 5 intervals
  394. // second 0, 2, 4, 6, 8
  395. // number 1, 2, 3, 4, 5
  396. // in number 4, we will ping any peer who has not received one yet to get latency
  397. // in number 5, we will collect everything back
  398. rcp->resampleCycle++;
  399. if (rcp->resampleCycle < 4) { return; }
  400. for (int j = 0; j < rcp->piList->length; j++) {
  401. struct PeerInfo_pvt* pi = ArrayList_OfPeerInfo_pvt_get(rcp->piList, j);
  402. Log_debug(rcp->log, "Visiting peer [%" PRIx64 "] samples [%u]",
  403. pi->pub.addr.path, pi->lagSamples);
  404. if (pi->lagSamples == 0 && !pi->waitForResponse) {
  405. pingPeer(rcp, pi);
  406. }
  407. if (rcp->resampleCycle < 5) { continue; }
  408. int sampleNum = rcp->linkStateSamples % LinkState_SLOTS;
  409. uint64_t drops = pi->sumOfDrops - pi->sumOfDropsLastSlot;
  410. // uint64 rollover, will "never" happen
  411. //if (drops < pi->sumOfDrops) { drops = pi->sumOfDrops; }
  412. uint64_t packets = pi->sumOfPackets - pi->sumOfPacketsLastSlot;
  413. //if (packets < pi->sumOfPackets) { drops = pi->sumOfPackets; }
  414. uint64_t dropRateShl18 = packets ? (drops << 18) / packets : 0;
  415. pi->pub.linkState.dropSlots[sampleNum] = dropRateShl18 > 0xfffe ? 0xfffe : dropRateShl18;
  416. pi->sumOfDropsLastSlot = pi->sumOfDrops;
  417. Log_debug(rcp->log,
  418. "[%" PRIx64 "] has sumOfDrops [%" PRIu64 "] sumOfDropsLastSlot [%" PRIu64 "]",
  419. pi->pub.addr.path, pi->sumOfDrops, pi->sumOfDropsLastSlot);
  420. pi->pub.linkState.kbRecvSlots[sampleNum] = pi->sumOfKb - pi->sumOfKbLastSlot;
  421. pi->sumOfKbLastSlot = pi->sumOfKb;
  422. pi->pub.linkState.lagSlots[sampleNum] = pi->lagSamples ? pi->sumOfLag / pi->lagSamples : 0;
  423. pi->sumOfLag = 0;
  424. pi->lagSamples = 0;
  425. pi->pub.linkState.samples = rcp->linkStateSamples + 1;
  426. }
  427. if (rcp->resampleCycle >= 5) {
  428. rcp->resampleCycle = 0;
  429. rcp->linkStateSamples++;
  430. }
  431. }
  432. struct ReachabilityCollector_PeerInfo*
  433. ReachabilityCollector_getPeerInfo(struct ReachabilityCollector* rc, int peerNum)
  434. {
  435. struct ReachabilityCollector_pvt* rcp = Identity_check((struct ReachabilityCollector_pvt*) rc);
  436. struct PeerInfo_pvt* pi = ArrayList_OfPeerInfo_pvt_get(rcp->piList, peerNum);
  437. return pi ? &pi->pub : NULL;
  438. }
  439. int ReachabilityCollector_peerCount(struct ReachabilityCollector* rc)
  440. {
  441. struct ReachabilityCollector_pvt* rcp = Identity_check((struct ReachabilityCollector_pvt*) rc);
  442. return rcp->piList->length;
  443. }
  444. void ReachabilityCollector_lagSample(
  445. struct ReachabilityCollector* rc, uint64_t label, uint32_t milliseconds)
  446. {
  447. struct ReachabilityCollector_pvt* rcp = Identity_check((struct ReachabilityCollector_pvt*) rc);
  448. struct PeerInfo_pvt* pi = piForLabel(rcp, label);
  449. if (!pi) { return; }
  450. latencyUpdate(rcp, pi, milliseconds);
  451. }
  452. void ReachabilityCollector_updateBandwidthAndDrops(
  453. struct ReachabilityCollector* rc,
  454. uint64_t label,
  455. uint64_t sumOfPackets,
  456. uint64_t sumOfDrops,
  457. uint64_t sumOfKb)
  458. {
  459. struct ReachabilityCollector_pvt* rcp = Identity_check((struct ReachabilityCollector_pvt*) rc);
  460. struct PeerInfo_pvt* pi = piForLabel(rcp, label);
  461. if (!pi) { return; }
  462. pi->sumOfPackets = sumOfPackets;
  463. pi->sumOfDrops = sumOfDrops;
  464. pi->sumOfKb = sumOfKb;
  465. }
  466. static void dummyOnChange(
  467. struct ReachabilityCollector* rc,
  468. struct Address* addr,
  469. struct ReachabilityCollector_PeerInfo* pi)
  470. {
  471. struct ReachabilityCollector_pvt* rcp = Identity_check((struct ReachabilityCollector_pvt*) rc);
  472. Log_debug(rcp->log, "dummyOnChange called, onChange unassigned");
  473. }
  474. struct ReachabilityCollector* ReachabilityCollector_new(struct Allocator* allocator,
  475. struct MsgCore* msgCore,
  476. struct Log* log,
  477. struct EventBase* base,
  478. struct BoilerplateResponder* br,
  479. struct Address* myAddr,
  480. struct EncodingScheme* myScheme,
  481. struct SwitchPinger* sp)
  482. {
  483. struct Allocator* alloc = Allocator_child(allocator);
  484. struct ReachabilityCollector_pvt* rcp =
  485. Allocator_calloc(alloc, sizeof(struct ReachabilityCollector_pvt), 1);
  486. rcp->myAddr = myAddr;
  487. rcp->msgCore = msgCore;
  488. rcp->alloc = alloc;
  489. rcp->piList = ArrayList_OfPeerInfo_pvt_new(alloc);
  490. rcp->log = log;
  491. rcp->br = br;
  492. rcp->base = base;
  493. rcp->pub.onChange = dummyOnChange;
  494. rcp->myScheme = myScheme;
  495. rcp->sp = sp;
  496. Identity_set(rcp);
  497. Timeout_setInterval(cycle, rcp, 2000, base, alloc);
  498. return &rcp->pub;
  499. }