ReachabilityCollector.c 20 KB

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