Repository navigation
Expand file tree
/
Copy pathBond.cpp
More file actions
1833 lines (1764 loc) · 69.9 KB
/
Copy pathBond.cpp
File metadata and controls
1833 lines (1764 loc) · 69.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c)2013-2020 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2025-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
#include <cmath>
#include "../osdep/OSUtils.hpp"
#include "Peer.hpp"
#include "Bond.hpp"
#include "Switch.hpp"
#include "Flow.hpp"
#include "Path.hpp"
namespace ZeroTier {
Bond::Bond(const RuntimeEnvironment *renv, int policy, const SharedPtr<Peer>& peer) :
RR(renv),
_peer(peer),
_qosCutoffCount(0),
_ackCutoffCount(0),
_lastAckRateCheck(0),
_lastQoSRateCheck(0),
_lastQualityEstimation(0),
_lastCheckUserPreferences(0),
_lastBackgroundTaskCheck(0),
_lastBondStatusLog(0),
_lastPathNegotiationReceived(0),
_lastPathNegotiationCheck(0),
_lastSentPathNegotiationRequest(0),
_lastFlowStatReset(0),
_lastFlowExpirationCheck(0),
_lastFlowRebalance(0),
_lastFrame(0),
_lastActiveBackupPathChange(0)
{
setReasonableDefaults(policy, SharedPtr<Bond>(), false);
_policyAlias = BondController::getPolicyStrByCode(policy);
}
Bond::Bond(const RuntimeEnvironment *renv, std::string& basePolicy, std::string& policyAlias, const SharedPtr<Peer>& peer) :
RR(renv),
_policyAlias(policyAlias),
_peer(peer)
{
setReasonableDefaults(BondController::getPolicyCodeByStr(basePolicy), SharedPtr<Bond>(), false);
}
Bond::Bond(const RuntimeEnvironment *renv, SharedPtr<Bond> originalBond, const SharedPtr<Peer>& peer) :
RR(renv),
_peer(peer),
_lastAckRateCheck(0),
_lastQoSRateCheck(0),
_lastQualityEstimation(0),
_lastCheckUserPreferences(0),
_lastBackgroundTaskCheck(0),
_lastBondStatusLog(0),
_lastPathNegotiationReceived(0),
_lastPathNegotiationCheck(0),
_lastFlowStatReset(0),
_lastFlowExpirationCheck(0),
_lastFlowRebalance(0),
_lastFrame(0)
{
setReasonableDefaults(originalBond->_bondingPolicy, originalBond, true);
}
void Bond::nominatePath(const SharedPtr<Path>& path, int64_t now)
{
char traceMsg[256]; char pathStr[128]; path->address().toString(pathStr);
Mutex::Lock _l(_paths_m);
if (!RR->bc->linkAllowed(_policyAlias, getLink(path))) {
return;
}
bool alreadyPresent = false;
for (int i=0; i<ZT_MAX_PEER_NETWORK_PATHS; ++i) {
if (path.ptr() == _paths[i].ptr()) {
// Previously encountered path, not notifying bond
alreadyPresent = true;
break;
}
}
if (!alreadyPresent) {
for (int i=0; i<ZT_MAX_PEER_NETWORK_PATHS; ++i) {
if (!_paths[i]) {
_paths[i] = path;
sprintf(traceMsg, "%s (bond) Nominating link %s/%s to peer %llx. It has now entered its trial period",
OSUtils::humanReadableTimestamp().c_str(), getLink(path)->ifname().c_str(), pathStr, _peer->_id.address().toInt());
RR->t->bondStateMessage(NULL, traceMsg);
_paths[i]->startTrial(now);
break;
}
}
}
curateBond(now, true);
estimatePathQuality(now);
}
SharedPtr<Path> Bond::getAppropriatePath(int64_t now, int32_t flowId)
{
Mutex::Lock _l(_paths_m);
/**
* active-backup
*/
if (_bondingPolicy == ZT_BONDING_POLICY_ACTIVE_BACKUP) {
if (_abPath) {
return _abPath;
}
}
/**
* broadcast
*/
if (_bondingPolicy == ZT_BONDING_POLICY_BROADCAST) {
return SharedPtr<Path>(); // Handled in Switch::_trySend()
}
if (!_numBondedPaths) {
return SharedPtr<Path>(); // No paths assigned to bond yet, cannot balance traffic
}
/**
* balance-rr
*/
if (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_RR) {
if (!_allowFlowHashing) {
if (_packetsPerLink == 0) {
// Randomly select a path
return _paths[_bondedIdx[_freeRandomByte % _numBondedPaths]]; // TODO: Optimize
}
if (_rrPacketsSentOnCurrLink < _packetsPerLink) {
// Continue to use this link
++_rrPacketsSentOnCurrLink;
return _paths[_bondedIdx[_rrIdx]];
}
// Reset striping counter
_rrPacketsSentOnCurrLink = 0;
if (_numBondedPaths == 1) {
_rrIdx = 0;
}
else {
int _tempIdx = _rrIdx;
for (int searchCount = 0; searchCount < (_numBondedPaths-1); searchCount++) {
_tempIdx = (_tempIdx == (_numBondedPaths-1)) ? 0 : _tempIdx+1;
if (_bondedIdx[_tempIdx] != ZT_MAX_PEER_NETWORK_PATHS) {
if (_paths[_bondedIdx[_tempIdx]] && _paths[_bondedIdx[_tempIdx]]->eligible(now,_ackSendInterval)) {
_rrIdx = _tempIdx;
break;
}
}
}
}
if (_paths[_bondedIdx[_rrIdx]]) {
return _paths[_bondedIdx[_rrIdx]];
}
}
}
/**
* balance-xor
*/
if (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_XOR || _bondingPolicy == ZT_BONDING_POLICY_BALANCE_AWARE) {
if (!_allowFlowHashing || flowId == -1) {
// No specific path required for unclassified traffic, send on anything
return _paths[_bondedIdx[_freeRandomByte % _numBondedPaths]]; // TODO: Optimize
}
else if (_allowFlowHashing) {
// TODO: Optimize
Mutex::Lock _l(_flows_m);
SharedPtr<Flow> flow;
if (_flows.count(flowId)) {
flow = _flows[flowId];
flow->updateActivity(now);
}
else {
unsigned char entropy;
Utils::getSecureRandom(&entropy, 1);
flow = createFlow(SharedPtr<Path>(), flowId, entropy, now);
}
if (flow) {
return flow->assignedPath();
}
}
}
return SharedPtr<Path>();
}
void Bond::recordIncomingInvalidPacket(const SharedPtr<Path>& path)
{
//char traceMsg[256]; char pathStr[128]; path->address().toString(pathStr);
//sprintf(traceMsg, "%s (qos) Invalid packet on link %s/%s from peer %llx",
// OSUtils::humanReadableTimestamp().c_str(), getLink(path)->ifname().c_str(), pathStr, _peer->_id.address().toInt());
//RR->t->bondStateMessage(NULL, traceMsg);
Mutex::Lock _l(_paths_m);
for (int i=0; i<ZT_MAX_PEER_NETWORK_PATHS; ++i) {
if (_paths[i] == path) {
_paths[i]->packetValiditySamples.push(false);
}
}
}
void Bond::recordOutgoingPacket(const SharedPtr<Path> &path, const uint64_t packetId,
uint16_t payloadLength, const Packet::Verb verb, const int32_t flowId, int64_t now)
{
//char traceMsg[256]; char pathStr[128]; path->address().toString(pathStr);
//sprintf(traceMsg, "%s (bond) Outgoing packet on link %s/%s to peer %llx",
// OSUtils::humanReadableTimestamp().c_str(), getLink(path)->ifname().c_str(), pathStr, _peer->_id.address().toInt());
//RR->t->bondStateMessage(NULL, traceMsg);
_freeRandomByte += (unsigned char)(packetId >> 8); // Grab entropy to use in path selection logic
if (!_shouldCollectPathStatistics) {
return;
}
bool isFrame = (verb == Packet::VERB_FRAME || verb == Packet::VERB_EXT_FRAME);
bool shouldRecord = (packetId & (ZT_QOS_ACK_DIVISOR - 1)
&& (verb != Packet::VERB_ACK)
&& (verb != Packet::VERB_QOS_MEASUREMENT));
if (isFrame || shouldRecord) {
Mutex::Lock _l(_paths_m);
if (isFrame) {
++(path->_packetsOut);
_lastFrame=now;
}
if (shouldRecord) {
path->_unackedBytes += payloadLength;
// Take note that we're expecting a VERB_ACK on this path as of a specific time
if (path->qosStatsOut.size() < ZT_QOS_MAX_OUTSTANDING_RECORDS) {
path->qosStatsOut[packetId] = now;
}
}
}
if (_allowFlowHashing && (flowId != ZT_QOS_NO_FLOW)) {
Mutex::Lock _l(_flows_m);
if (_flows.count(flowId)) {
_flows[flowId]->recordOutgoingBytes(payloadLength);
}
}
}
void Bond::recordIncomingPacket(const SharedPtr<Path>& path, uint64_t packetId, uint16_t payloadLength,
Packet::Verb verb, int32_t flowId, int64_t now)
{
//char traceMsg[256]; char pathStr[128]; path->address().toString(pathStr);
//sprintf(traceMsg, "%s (bond) Incoming packet on link %s/%s from peer %llx [id=%llx, len=%d, verb=%d, flowId=%x]",
// OSUtils::humanReadableTimestamp().c_str(), getLink(path)->ifname().c_str(), pathStr, _peer->_id.address().toInt(), packetId, payloadLength, verb, flowId);
//RR->t->bondStateMessage(NULL, traceMsg);
bool isFrame = (verb == Packet::VERB_FRAME || verb == Packet::VERB_EXT_FRAME);
bool shouldRecord = (packetId & (ZT_QOS_ACK_DIVISOR - 1)
&& (verb != Packet::VERB_ACK)
&& (verb != Packet::VERB_QOS_MEASUREMENT));
if (isFrame || shouldRecord) {
Mutex::Lock _l(_paths_m);
if (isFrame) {
++(path->_packetsIn);
_lastFrame=now;
}
if (shouldRecord) {
path->ackStatsIn[packetId] = payloadLength;
++(path->_packetsReceivedSinceLastAck);
path->qosStatsIn[packetId] = now;
++(path->_packetsReceivedSinceLastQoS);
path->packetValiditySamples.push(true);
}
}
/**
* Learn new flows and pro-actively create entries for them in the bond so
* that the next time we send a packet out that is part of a flow we know
* which path to use.
*/
if ((flowId != ZT_QOS_NO_FLOW)
&& (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_RR
|| _bondingPolicy == ZT_BONDING_POLICY_BALANCE_XOR
|| _bondingPolicy == ZT_BONDING_POLICY_BALANCE_AWARE)) {
Mutex::Lock _l(_flows_m);
SharedPtr<Flow> flow;
if (!_flows.count(flowId)) {
flow = createFlow(path, flowId, 0, now);
} else {
flow = _flows[flowId];
}
if (flow) {
flow->recordIncomingBytes(payloadLength);
}
}
}
void Bond::receivedQoS(const SharedPtr<Path>& path, int64_t now, int count, uint64_t *rx_id, uint16_t *rx_ts)
{
Mutex::Lock _l(_paths_m);
//char traceMsg[256]; char pathStr[128]; path->address().toString(pathStr);
//sprintf(traceMsg, "%s (qos) Received QoS packet sampling %d frames from peer %llx via %s/%s",
// OSUtils::humanReadableTimestamp().c_str(), count, _peer->_id.address().toInt(), getLink(path)->ifname().c_str(), pathStr);
//RR->t->bondStateMessage(NULL, traceMsg);
// Look up egress times and compute latency values for each record
std::map<uint64_t,uint64_t>::iterator it;
for (int j=0; j<count; j++) {
it = path->qosStatsOut.find(rx_id[j]);
if (it != path->qosStatsOut.end()) {
path->latencySamples.push(((uint16_t)(now - it->second) - rx_ts[j]) / 2);
path->qosStatsOut.erase(it);
}
}
path->qosRecordSize.push(count);
}
void Bond::receivedAck(const SharedPtr<Path>& path, int64_t now, int32_t ackedBytes)
{
Mutex::Lock _l(_paths_m);
//char traceMsg[256]; char pathStr[128]; path->address().toString(pathStr);
//sprintf(traceMsg, "%s (qos) Received ACK packet for %d bytes from peer %llx via %s/%s",
// OSUtils::humanReadableTimestamp().c_str(), ackedBytes, _peer->_id.address().toInt(), getLink(path)->ifname().c_str(), pathStr);
//RR->t->bondStateMessage(NULL, traceMsg);
path->_lastAckReceived = now;
path->_unackedBytes = (ackedBytes > path->_unackedBytes) ? 0 : path->_unackedBytes - ackedBytes;
int64_t timeSinceThroughputEstimate = (now - path->_lastThroughputEstimation);
if (timeSinceThroughputEstimate >= throughputMeasurementInterval) {
// TODO: See if this floating point math can be reduced
uint64_t throughput = (uint64_t)((float)(path->_bytesAckedSinceLastThroughputEstimation) / ((float)timeSinceThroughputEstimate / (float)1000));
throughput /= 1000;
if (throughput > 0.0) {
path->throughputSamples.push(throughput);
path->_throughputMax = throughput > path->_throughputMax ? throughput : path->_throughputMax;
}
path->_lastThroughputEstimation = now;
path->_bytesAckedSinceLastThroughputEstimation = 0;
} else {
path->_bytesAckedSinceLastThroughputEstimation += ackedBytes;
}
}
int32_t Bond::generateQoSPacket(const SharedPtr<Path>& path, int64_t now, char *qosBuffer)
{
int32_t len = 0;
std::map<uint64_t,uint64_t>::iterator it = path->qosStatsIn.begin();
int i=0;
int numRecords = std::min(path->_packetsReceivedSinceLastQoS,ZT_QOS_TABLE_SIZE);
while (i<numRecords && it != path->qosStatsIn.end()) {
uint64_t id = it->first;
memcpy(qosBuffer, &id, sizeof(uint64_t));
qosBuffer+=sizeof(uint64_t);
uint16_t holdingTime = (uint16_t)(now - it->second);
memcpy(qosBuffer, &holdingTime, sizeof(uint16_t));
qosBuffer+=sizeof(uint16_t);
len+=sizeof(uint64_t)+sizeof(uint16_t);
path->qosStatsIn.erase(it++);
++i;
}
return len;
}
bool Bond::assignFlowToBondedPath(SharedPtr<Flow> &flow, int64_t now)
{
char traceMsg[256];
char curPathStr[128];
unsigned int idx = ZT_MAX_PEER_NETWORK_PATHS;
if (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_XOR) {
idx = abs((int)(flow->id() % (_numBondedPaths)));
SharedPtr<Link> link = RR->bc->getLinkBySocket(_policyAlias, _paths[_bondedIdx[idx]]->localSocket());
_paths[_bondedIdx[idx]]->address().toString(curPathStr);
sprintf(traceMsg, "%s (balance-xor) Assigned outgoing flow %x to peer %llx to link %s/%s, %lu active flow(s)",
OSUtils::humanReadableTimestamp().c_str(), flow->id(), _peer->_id.address().toInt(), link->ifname().c_str(), curPathStr, _flows.size());
RR->t->bondStateMessage(NULL, traceMsg);
flow->assignPath(_paths[_bondedIdx[idx]],now);
++(_paths[_bondedIdx[idx]]->_assignedFlowCount);
}
if (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_AWARE) {
unsigned char entropy;
Utils::getSecureRandom(&entropy, 1);
if (_totalBondUnderload) {
entropy %= _totalBondUnderload;
}
if (!_numBondedPaths) {
sprintf(traceMsg, "%s (balance-aware) There are no bonded paths, cannot assign flow %x\n",
OSUtils::humanReadableTimestamp().c_str(), flow->id());
RR->t->bondStateMessage(NULL, traceMsg);
return false;
}
/* Since there may be scenarios where a path is removed before we can re-estimate
relative qualities (and thus allocations) we need to down-modulate the entropy
value that we use to randomly assign among the surviving paths, otherwise we risk
not being able to find a path to assign this flow to. */
int totalIncompleteAllocation = 0;
for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
if (_paths[i] && _paths[i]->bonded()) {
totalIncompleteAllocation += _paths[i]->_allocation;
}
}
entropy %= totalIncompleteAllocation;
for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
if (_paths[i] && _paths[i]->bonded()) {
SharedPtr<Link> link = RR->bc->getLinkBySocket(_policyAlias, _paths[i]->localSocket());
_paths[i]->address().toString(curPathStr);
uint8_t probabilitySegment = (_totalBondUnderload > 0) ? _paths[i]->_affinity : _paths[i]->_allocation;
if (entropy <= probabilitySegment) {
idx = i;
break;
}
entropy -= probabilitySegment;
}
}
if (idx < ZT_MAX_PEER_NETWORK_PATHS) {
if (flow->_assignedPath) {
flow->_previouslyAssignedPath = flow->_assignedPath;
}
flow->assignPath(_paths[idx],now);
++(_paths[idx]->_assignedFlowCount);
}
else {
fprintf(stderr, "could not assign flow?\n"); exit(0); // TODO: Remove for production
return false;
}
}
if (_bondingPolicy == ZT_BONDING_POLICY_ACTIVE_BACKUP) {
if (_abOverflowEnabled) {
flow->assignPath(_abPath, now);
} else {
sprintf(traceMsg, "%s (bond) Unable to assign outgoing flow %x to peer %llx, no active overflow link",
OSUtils::humanReadableTimestamp().c_str(), flow->id(), _peer->_id.address().toInt());
RR->t->bondStateMessage(NULL, traceMsg);
return false;
}
}
flow->assignedPath()->address().toString(curPathStr);
SharedPtr<Link> link = RR->bc->getLinkBySocket(_policyAlias, flow->assignedPath()->localSocket());
sprintf(traceMsg, "%s (bond) Assigned outgoing flow %x to peer %llx to link %s/%s, %lu active flow(s)",
OSUtils::humanReadableTimestamp().c_str(), flow->id(), _peer->_id.address().toInt(), link->ifname().c_str(), curPathStr, _flows.size());
RR->t->bondStateMessage(NULL, traceMsg);
return true;
}
SharedPtr<Flow> Bond::createFlow(const SharedPtr<Path> &path, int32_t flowId, unsigned char entropy, int64_t now)
{
char traceMsg[256];
char curPathStr[128];
// ---
if (!_numBondedPaths) {
sprintf(traceMsg, "%s (bond) There are no bonded paths to peer %llx, cannot assign flow %x\n",
OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), flowId);
RR->t->bondStateMessage(NULL, traceMsg);
return SharedPtr<Flow>();
}
if (_flows.size() >= ZT_FLOW_MAX_COUNT) {
sprintf(traceMsg, "%s (bond) Maximum number of flows on bond to peer %llx reached (%d), forcibly forgetting oldest flow\n",
OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), ZT_FLOW_MAX_COUNT);
RR->t->bondStateMessage(NULL, traceMsg);
forgetFlowsWhenNecessary(0, true, now);
}
SharedPtr<Flow> flow = new Flow(flowId, now);
_flows[flowId] = flow;
/**
* Add a flow with a given Path already provided. This is the case when a packet
* is received on a path but no flow exists, in this case we simply assign the path
* that the remote peer chose for us.
*/
if (path) {
flow->assignPath(path,now);
path->address().toString(curPathStr);
path->_assignedFlowCount++;
SharedPtr<Link> link = RR->bc->getLinkBySocket(_policyAlias, flow->assignedPath()->localSocket());
sprintf(traceMsg, "%s (bond) Assigned incoming flow %x from peer %llx to link %s/%s, %lu active flow(s)",
OSUtils::humanReadableTimestamp().c_str(), flow->id(), _peer->_id.address().toInt(), link->ifname().c_str(), curPathStr, _flows.size());
RR->t->bondStateMessage(NULL, traceMsg);
}
/**
* Add a flow when no path was provided. This means that it is an outgoing packet
* and that it is up to the local peer to decide how to load-balance its transmission.
*/
else if (!path) {
assignFlowToBondedPath(flow, now);
}
return flow;
}
void Bond::forgetFlowsWhenNecessary(uint64_t age, bool oldest, int64_t now)
{
char traceMsg[256];
std::map<int32_t,SharedPtr<Flow> >::iterator it = _flows.begin();
std::map<int32_t,SharedPtr<Flow> >::iterator oldestFlow = _flows.end();
SharedPtr<Flow> expiredFlow;
if (age) { // Remove by specific age
while (it != _flows.end()) {
if (it->second->age(now) > age) {
sprintf(traceMsg, "%s (bond) Forgetting flow %x between this node and peer %llx, %lu active flow(s)",
OSUtils::humanReadableTimestamp().c_str(), it->first, _peer->_id.address().toInt(), (_flows.size()-1));
RR->t->bondStateMessage(NULL, traceMsg);
it->second->assignedPath()->_assignedFlowCount--;
it = _flows.erase(it);
} else {
++it;
}
}
}
else if (oldest) { // Remove single oldest by natural expiration
uint64_t maxAge = 0;
while (it != _flows.end()) {
if (it->second->age(now) > maxAge) {
maxAge = (now - it->second->age(now));
oldestFlow = it;
}
++it;
}
if (oldestFlow != _flows.end()) {
sprintf(traceMsg, "%s (bond) Forgetting oldest flow %x (of age %llu) between this node and peer %llx, %lu active flow(s)",
OSUtils::humanReadableTimestamp().c_str(), oldestFlow->first, oldestFlow->second->age(now), _peer->_id.address().toInt(), (_flows.size()-1));
RR->t->bondStateMessage(NULL, traceMsg);
oldestFlow->second->assignedPath()->_assignedFlowCount--;
_flows.erase(oldestFlow);
}
}
}
void Bond::processIncomingPathNegotiationRequest(uint64_t now, SharedPtr<Path> &path, int16_t remoteUtility)
{
char traceMsg[256];
if (_abLinkSelectMethod != ZT_MULTIPATH_RESELECTION_POLICY_OPTIMIZE) {
return;
}
Mutex::Lock _l(_paths_m);
char pathStr[128];
path->address().toString(pathStr);
if (!_lastPathNegotiationCheck) {
return;
}
SharedPtr<Link> link = RR->bc->getLinkBySocket(_policyAlias, path->localSocket());
if (remoteUtility > _localUtility) {
char pathStr[128]; path->address().toString(pathStr);
sprintf(traceMsg, "%s (bond) Peer %llx suggests using alternate link %s/%s. Remote utility (%d) is GREATER than local utility (%d), switching to said link\n",
OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), link->ifname().c_str(), pathStr, remoteUtility, _localUtility);
RR->t->bondStateMessage(NULL, traceMsg);
negotiatedPath = path;
}
if (remoteUtility < _localUtility) {
sprintf(traceMsg, "%s (bond) Peer %llx suggests using alternate link %s/%s. Remote utility (%d) is LESS than local utility (%d), not switching\n",
OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), link->ifname().c_str(), pathStr, remoteUtility, _localUtility);
RR->t->bondStateMessage(NULL, traceMsg);
}
if (remoteUtility == _localUtility) {
sprintf(traceMsg, "%s (bond) Peer %llx suggests using alternate link %s/%s. Remote utility (%d) is equal to local utility (%d)\n",
OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), link->ifname().c_str(), pathStr, remoteUtility, _localUtility);
RR->t->bondStateMessage(NULL, traceMsg);
if (_peer->_id.address().toInt() > RR->node->identity().address().toInt()) {
sprintf(traceMsg, "%s (bond) Agreeing with peer %llx to use alternate link %s/%s\n",
OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), link->ifname().c_str(), pathStr);
RR->t->bondStateMessage(NULL, traceMsg);
negotiatedPath = path;
} else {
sprintf(traceMsg, "%s (bond) Ignoring petition from peer %llx to use alternate link %s/%s\n",
OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), link->ifname().c_str(), pathStr);
RR->t->bondStateMessage(NULL, traceMsg);
}
}
}
void Bond::pathNegotiationCheck(void *tPtr, const int64_t now)
{
char pathStr[128];
int maxInPathIdx = ZT_MAX_PEER_NETWORK_PATHS;
int maxOutPathIdx = ZT_MAX_PEER_NETWORK_PATHS;
uint64_t maxInCount = 0;
uint64_t maxOutCount = 0;
for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
if (!_paths[i]) {
continue;
}
if (_paths[i]->_packetsIn > maxInCount) {
maxInCount = _paths[i]->_packetsIn;
maxInPathIdx = i;
}
if (_paths[i]->_packetsOut > maxOutCount) {
maxOutCount = _paths[i]->_packetsOut;
maxOutPathIdx = i;
}
_paths[i]->resetPacketCounts();
}
bool _peerLinksSynchronized = ((maxInPathIdx != ZT_MAX_PEER_NETWORK_PATHS)
&& (maxOutPathIdx != ZT_MAX_PEER_NETWORK_PATHS)
&& (maxInPathIdx != maxOutPathIdx)) ? false : true;
/**
* Determine utility and attempt to petition remote peer to switch to our chosen path
*/
if (!_peerLinksSynchronized) {
_localUtility = _paths[maxOutPathIdx]->_failoverScore - _paths[maxInPathIdx]->_failoverScore;
if (_paths[maxOutPathIdx]->_negotiated) {
_localUtility -= ZT_MULTIPATH_FAILOVER_HANDICAP_NEGOTIATED;
}
if ((now - _lastSentPathNegotiationRequest) > ZT_PATH_NEGOTIATION_CUTOFF_TIME) {
//fprintf(stderr, "BT: (sync) it's been long enough, sending more requests.\n");
_numSentPathNegotiationRequests = 0;
}
if (_numSentPathNegotiationRequests < ZT_PATH_NEGOTIATION_TRY_COUNT) {
if (_localUtility >= 0) {
//fprintf(stderr, "BT: (sync) paths appear to be out of sync (utility=%d)\n", _localUtility);
sendPATH_NEGOTIATION_REQUEST(tPtr, _paths[maxOutPathIdx]);
++_numSentPathNegotiationRequests;
_lastSentPathNegotiationRequest = now;
_paths[maxOutPathIdx]->address().toString(pathStr);
SharedPtr<Link> link =RR->bc->getLinkBySocket(_policyAlias, _paths[maxOutPathIdx]->localSocket());
//fprintf(stderr, "sending request to use %s on %s, ls=%llx, utility=%d\n", pathStr, link->ifname().c_str(), _paths[maxOutPathIdx]->localSocket(), _localUtility);
}
}
/**
* Give up negotiating and consider switching
*/
else if ((now - _lastSentPathNegotiationRequest) > (2 * ZT_PATH_NEGOTIATION_CHECK_INTERVAL)) {
if (_localUtility == 0) {
// There's no loss to us, just switch without sending a another request
//fprintf(stderr, "BT: (sync) giving up, switching to remote peer's path.\n");
negotiatedPath = _paths[maxInPathIdx];
}
}
}
}
void Bond::sendPATH_NEGOTIATION_REQUEST(void *tPtr, const SharedPtr<Path> &path)
{
char traceMsg[256]; char pathStr[128]; path->address().toString(pathStr);
sprintf(traceMsg, "%s (bond) Sending link negotiation request to peer %llx via link %s/%s, local utility is %d",
OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), getLink(path)->ifname().c_str(), pathStr, _localUtility);
RR->t->bondStateMessage(NULL, traceMsg);
if (_abLinkSelectMethod != ZT_MULTIPATH_RESELECTION_POLICY_OPTIMIZE) {
return;
}
Packet outp(_peer->_id.address(),RR->identity.address(),Packet::VERB_PATH_NEGOTIATION_REQUEST);
outp.append<int16_t>(_localUtility);
if (path->address()) {
outp.armor(_peer->key(),false,_peer->aesKeysIfSupported());
RR->node->putPacket(tPtr,path->localSocket(),path->address(),outp.data(),outp.size());
}
}
void Bond::sendACK(void *tPtr, const SharedPtr<Path> &path,const int64_t localSocket,
const InetAddress &atAddress,int64_t now)
{
Packet outp(_peer->_id.address(),RR->identity.address(),Packet::VERB_ACK);
int32_t bytesToAck = 0;
std::map<uint64_t,uint16_t>::iterator it = path->ackStatsIn.begin();
while (it != path->ackStatsIn.end()) {
bytesToAck += it->second;
++it;
}
//char traceMsg[256]; char pathStr[128]; path->address().toString(pathStr);
//sprintf(traceMsg, "%s (qos) Sending ACK packet for %d bytes to peer %llx via link %s/%s",
// OSUtils::humanReadableTimestamp().c_str(), bytesToAck, _peer->_id.address().toInt(), getLink(path)->ifname().c_str(), pathStr);
//RR->t->bondStateMessage(NULL, traceMsg);
outp.append<uint32_t>(bytesToAck);
if (atAddress) {
outp.armor(_peer->key(),false,_peer->aesKeysIfSupported());
RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
} else {
RR->sw->send(tPtr,outp,false);
}
path->ackStatsIn.clear();
path->_packetsReceivedSinceLastAck = 0;
path->_lastAckSent = now;
}
void Bond::sendQOS_MEASUREMENT(void *tPtr,const SharedPtr<Path> &path,const int64_t localSocket,
const InetAddress &atAddress,int64_t now)
{
//char traceMsg[256]; char pathStr[128]; path->address().toString(pathStr);
//sprintf(traceMsg, "%s (qos) Sending QoS packet to peer %llx via link %s/%s",
// OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), getLink(path)->ifname().c_str(), pathStr);
//RR->t->bondStateMessage(NULL, traceMsg);
const int64_t _now = RR->node->now();
Packet outp(_peer->_id.address(),RR->identity.address(),Packet::VERB_QOS_MEASUREMENT);
char qosData[ZT_QOS_MAX_PACKET_SIZE];
int16_t len = generateQoSPacket(path, _now,qosData);
outp.append(qosData,len);
if (atAddress) {
outp.armor(_peer->key(),false,_peer->aesKeysIfSupported());
RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
} else {
RR->sw->send(tPtr,outp,false);
}
// Account for the fact that a VERB_QOS_MEASUREMENT was just sent. Reset timers.
path->_packetsReceivedSinceLastQoS = 0;
path->_lastQoSMeasurement = now;
}
void Bond::processBackgroundTasks(void *tPtr, const int64_t now)
{
Mutex::Lock _l(_paths_m);
if (!_peer->_canUseMultipath || (now - _lastBackgroundTaskCheck) < ZT_BOND_BACKGROUND_TASK_MIN_INTERVAL) {
return;
}
_lastBackgroundTaskCheck = now;
// Compute dynamic path monitor timer interval
if (_linkMonitorStrategy == ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_DYNAMIC) {
int suggestedMonitorInterval = (now - _lastFrame) / 100;
_dynamicPathMonitorInterval = std::min(ZT_PATH_HEARTBEAT_PERIOD, ((suggestedMonitorInterval > _bondMonitorInterval) ? suggestedMonitorInterval : _bondMonitorInterval));
}
// TODO: Clarify and generalize this logic
if (_linkMonitorStrategy == ZT_MULTIPATH_SLAVE_MONITOR_STRATEGY_DYNAMIC) {
_shouldCollectPathStatistics = true;
}
// Memoize oft-used properties in the packet ingress/egress logic path
if (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_AWARE) {
// Required for real-time balancing
_shouldCollectPathStatistics = true;
}
if (_bondingPolicy == ZT_BONDING_POLICY_ACTIVE_BACKUP) {
if (_abLinkSelectMethod == ZT_MULTIPATH_RESELECTION_POLICY_BETTER) {
// Required for judging suitability of primary link after recovery
_shouldCollectPathStatistics = true;
}
if (_abLinkSelectMethod == ZT_MULTIPATH_RESELECTION_POLICY_OPTIMIZE) {
// Required for judging suitability of new candidate primary
_shouldCollectPathStatistics = true;
}
}
if ((now - _lastCheckUserPreferences) > 1000) {
_lastCheckUserPreferences = now;
applyUserPrefs();
}
curateBond(now,false);
if ((now - _lastQualityEstimation) > _qualityEstimationInterval) {
_lastQualityEstimation = now;
estimatePathQuality(now);
}
dumpInfo(now);
// Send QOS/ACK packets as needed
if (_shouldCollectPathStatistics) {
for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
if (_paths[i] && _paths[i]->allowed()) {
if (_paths[i]->needsToSendQoS(now,_qosSendInterval)) {
sendQOS_MEASUREMENT(tPtr, _paths[i], _paths[i]->localSocket(), _paths[i]->address(), now);
}
if (_paths[i]->needsToSendAck(now,_ackSendInterval)) {
sendACK(tPtr, _paths[i], _paths[i]->localSocket(), _paths[i]->address(), now);
}
}
}
}
// Perform periodic background tasks unique to each bonding policy
switch (_bondingPolicy)
{
case ZT_BONDING_POLICY_ACTIVE_BACKUP:
processActiveBackupTasks(tPtr, now);
break;
case ZT_BONDING_POLICY_BROADCAST:
break;
case ZT_BONDING_POLICY_BALANCE_RR:
case ZT_BONDING_POLICY_BALANCE_XOR:
case ZT_BONDING_POLICY_BALANCE_AWARE:
processBalanceTasks(now);
break;
default:
break;
}
// Check whether or not a path negotiation needs to be performed
if (((now - _lastPathNegotiationCheck) > ZT_PATH_NEGOTIATION_CHECK_INTERVAL) && _allowPathNegotiation) {
_lastPathNegotiationCheck = now;
pathNegotiationCheck(tPtr, now);
}
}
void Bond::applyUserPrefs()
{
for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
if (!_paths[i]) {
continue;
}
SharedPtr<Link> sl = getLink(_paths[i]);
if (sl) {
if (sl->monitorInterval() == 0) { // If no interval was specified for this link, use more generic bond-wide interval
sl->setMonitorInterval(_bondMonitorInterval);
}
RR->bc->setMinReqPathMonitorInterval((sl->monitorInterval() < RR->bc->minReqPathMonitorInterval()) ? sl->monitorInterval() : RR->bc->minReqPathMonitorInterval());
bool bFoundCommonLink = false;
SharedPtr<Link> commonLink =RR->bc->getLinkBySocket(_policyAlias, _paths[i]->localSocket());
for(unsigned int j=0;j<ZT_MAX_PEER_NETWORK_PATHS;++j) {
if (_paths[j] && _paths[j].ptr() != _paths[i].ptr()) {
if (RR->bc->getLinkBySocket(_policyAlias, _paths[j]->localSocket()) == commonLink) {
bFoundCommonLink = true;
}
}
}
_paths[i]->_monitorInterval = sl->monitorInterval();
_paths[i]->_upDelay = sl->upDelay() ? sl->upDelay() : _upDelay;
_paths[i]->_downDelay = sl->downDelay() ? sl->downDelay() : _downDelay;
_paths[i]->_ipvPref = sl->ipvPref();
_paths[i]->_mode = sl->mode();
_paths[i]->_enabled = sl->enabled();
_paths[i]->_onlyPathOnLink = !bFoundCommonLink;
}
}
if (_peer) {
_peer->_shouldCollectPathStatistics = _shouldCollectPathStatistics;
_peer->_bondingPolicy = _bondingPolicy;
}
}
void Bond::curateBond(const int64_t now, bool rebuildBond)
{
char traceMsg[256];
char pathStr[128];
uint8_t tmpNumAliveLinks = 0;
uint8_t tmpNumTotalLinks = 0;
/**
* Update path states
*/
for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
if (!_paths[i]) {
continue;
}
tmpNumTotalLinks++;
if (_paths[i]->alive(now, true)) {
tmpNumAliveLinks++;
}
bool currEligibility = _paths[i]->eligible(now,_ackSendInterval);
if (currEligibility != _paths[i]->_lastEligibilityState) {
_paths[i]->address().toString(pathStr);
char traceMsg[256]; _paths[i]->address().toString(pathStr);
sprintf(traceMsg, "%s (bond) Eligibility of link %s/%s to peer %llx has changed from %d to %d",
OSUtils::humanReadableTimestamp().c_str(), getLink(_paths[i])->ifname().c_str(), pathStr, _peer->_id.address().toInt(), _paths[i]->_lastEligibilityState, currEligibility);
RR->t->bondStateMessage(NULL, traceMsg);
if (currEligibility) {
rebuildBond = true;
}
if (!currEligibility) {
_paths[i]->adjustRefractoryPeriod(now, _defaultPathRefractoryPeriod, !currEligibility);
if (_paths[i]->bonded()) {
char pathStr[128]; _paths[i]->address().toString(pathStr);
sprintf(traceMsg, "%s (bond) Link %s/%s to peer %llx was bonded, reallocation of its flows will occur soon",
OSUtils::humanReadableTimestamp().c_str(), getLink(_paths[i])->ifname().c_str(), pathStr, _peer->_id.address().toInt());
RR->t->bondStateMessage(NULL, traceMsg);
rebuildBond = true;
_paths[i]->_shouldReallocateFlows = _paths[i]->bonded();
_paths[i]->setBonded(false);
} else {
sprintf(traceMsg, "%s (bond) Link %s/%s to peer %llx was not bonded, no allocation consequences",
OSUtils::humanReadableTimestamp().c_str(), getLink(_paths[i])->ifname().c_str(), pathStr, _peer->_id.address().toInt());
RR->t->bondStateMessage(NULL, traceMsg);
}
}
}
if (currEligibility) {
_paths[i]->adjustRefractoryPeriod(now, _defaultPathRefractoryPeriod, false);
}
_paths[i]->_lastEligibilityState = currEligibility;
}
_numAliveLinks = tmpNumAliveLinks;
_numTotalLinks = tmpNumTotalLinks;
/* Determine health status to report to user */
bool tmpHealthStatus = true;
if (_bondingPolicy == ZT_BONDING_POLICY_ACTIVE_BACKUP) {
if (_numAliveLinks < 2) {
// Considered healthy if there is at least one failover link
tmpHealthStatus = false;
}
}
if (_bondingPolicy == ZT_BONDING_POLICY_BROADCAST) {
if (_numAliveLinks < 1) {
// Considerd healthy if we're able to send frames at all
tmpHealthStatus = false;
}
}
if (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_RR) {
if (_numAliveLinks < _numTotalLinks) {
// Considerd healthy if all known paths are alive, this should be refined to account for user bond config settings
tmpHealthStatus = false;
}
}
if (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_XOR) {
if (_numAliveLinks < _numTotalLinks) {
// Considerd healthy if all known paths are alive, this should be refined to account for user bond config settings
tmpHealthStatus = false;
}
}
if (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_AWARE) {
if (_numAliveLinks < _numTotalLinks) {
// Considerd healthy if all known paths are alive, this should be refined to account for user bond config settings
tmpHealthStatus = false;
}
}
if (tmpHealthStatus != _isHealthy) {
std::string healthStatusStr;
if (tmpHealthStatus == true) {
healthStatusStr = "HEALTHY";
} else {
healthStatusStr = "DEGRADED";
}
sprintf(traceMsg, "%s (bond) Bond to peer %llx is in a %s state (%d/%d links)",
OSUtils::humanReadableTimestamp().c_str(), _peer->_id.address().toInt(), healthStatusStr.c_str(), _numAliveLinks, _numTotalLinks);
RR->t->bondStateMessage(NULL, traceMsg);
}
_isHealthy = tmpHealthStatus;
/**
* Curate the set of paths that are part of the bond proper. Selects a single path
* per logical link according to eligibility and user-specified constraints.
*/
if ((_bondingPolicy == ZT_BONDING_POLICY_BALANCE_RR)
|| (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_XOR)
|| (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_AWARE)) {
if (!_numBondedPaths) {
rebuildBond = true;
}
// TODO: Optimize
if (rebuildBond) {
int updatedBondedPathCount = 0;
std::map<SharedPtr<Link>,int> linkMap;
for (int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
if (_paths[i] && _paths[i]->allowed() && (_paths[i]->eligible(now,_ackSendInterval) || !_numBondedPaths)) {
SharedPtr<Link> link =RR->bc->getLinkBySocket(_policyAlias, _paths[i]->localSocket());
if (!linkMap.count(link)) {
linkMap[link] = i;
}
else {
bool overriden = false;
_paths[i]->address().toString(pathStr);
//fprintf(stderr, " link representative path already exists! (%s %s)\n", getLink(_paths[i])->ifname().c_str(), pathStr);
if (_paths[i]->preferred() && !_paths[linkMap[link]]->preferred()) {
// Override previous choice if preferred
if (_paths[linkMap[link]]->_assignedFlowCount) {
_paths[linkMap[link]]->_deprecated = true;
}
else {
_paths[linkMap[link]]->_deprecated = true;
_paths[linkMap[link]]->setBonded(false);
}
linkMap[link] = i;
overriden = true;
}
if ((_paths[i]->preferred() && _paths[linkMap[link]]->preferred())
|| (!_paths[i]->preferred() && !_paths[linkMap[link]]->preferred())) {
if (_paths[i]->preferenceRank() > _paths[linkMap[link]]->preferenceRank()) {
// Override if higher preference
if (_paths[linkMap[link]]->_assignedFlowCount) {
_paths[linkMap[link]]->_deprecated = true;
}
else {
_paths[linkMap[link]]->_deprecated = true;
_paths[linkMap[link]]->setBonded(false);
}
linkMap[link] = i;
}
}
}
}
}
std::map<SharedPtr<Link>,int>::iterator it = linkMap.begin();
for (int i=0; i<ZT_MAX_PEER_NETWORK_PATHS; ++i) {
if (!_paths[i]) {
continue;
}
_bondedIdx[i] = ZT_MAX_PEER_NETWORK_PATHS;
if (it != linkMap.end()) {
_bondedIdx[i] = it->second;
_paths[_bondedIdx[i]]->setBonded(true);
++it;
++updatedBondedPathCount;
_paths[_bondedIdx[i]]->address().toString(pathStr);
//fprintf(stderr, "setting i=%d, _bondedIdx[%d]=%d to bonded (%s %s)\n", i, i, _bondedIdx[i], getLink(_paths[_bondedIdx[i]])->ifname().c_str(), pathStr);
}
}
_numBondedPaths = updatedBondedPathCount;
if (_bondingPolicy == ZT_BONDING_POLICY_BALANCE_RR) {
// Cause a RR reset since the currently used index might no longer be valid
_rrPacketsSentOnCurrLink = _packetsPerLink;
}
}
}
}
void Bond::estimatePathQuality(const int64_t now)
{
char pathStr[128];
uint32_t totUserSpecifiedLinkSpeed = 0;
if (_numBondedPaths) { // Compute relative user-specified speeds of links
for(unsigned int i=0;i<_numBondedPaths;++i) {
SharedPtr<Link> link =RR->bc->getLinkBySocket(_policyAlias, _paths[i]->localSocket());
if (_paths[i] && _paths[i]->allowed()) {
totUserSpecifiedLinkSpeed += link->speed();
}
}
for(unsigned int i=0;i<_numBondedPaths;++i) {
SharedPtr<Link> link =RR->bc->getLinkBySocket(_policyAlias, _paths[i]->localSocket());
if (_paths[i] && _paths[i]->allowed()) {
link->setRelativeSpeed(round( ((float)link->speed() / (float)totUserSpecifiedLinkSpeed) * 255));
}
}
}
float lat[ZT_MAX_PEER_NETWORK_PATHS];
float pdv[ZT_MAX_PEER_NETWORK_PATHS];
float plr[ZT_MAX_PEER_NETWORK_PATHS];
float per[ZT_MAX_PEER_NETWORK_PATHS];