-
Notifications
You must be signed in to change notification settings - Fork 25
/
metrics.c
1647 lines (1147 loc) · 64.3 KB
/
metrics.c
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) 2010 Axel Neumann
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
//#include "math.h"
#include "bmx.h"
#include "msg.h"
#include "ip.h"
#include "plugin.h"
#include "schedule.h"
#include "tools.h"
#include "metrics.h"
#define CODE_CATEGORY_NAME "metric"
static int32_t descMetricalgo = DEF_DESC_METRICALGO;
static int32_t my_path_algo = DEF_METRIC_ALGO;
static int32_t my_path_rp_exp_numerator = DEF_PATH_RP_EXP_NUMERATOR;
static int32_t my_path_rp_exp_divisor = DEF_PATH_RP_EXP_DIVISOR;
static int32_t my_path_tp_exp_numerator = DEF_PATH_TP_EXP_NUMERATOR;
static int32_t my_path_tp_exp_divisor = DEF_PATH_TP_EXP_DIVISOR;
static int32_t my_path_metric_flags = DEF_PATH_METRIC_FLAGS;
static int32_t my_path_umetric_min = DEF_PATH_UMETRIC_MIN;
static int32_t my_path_window = DEF_PATH_WINDOW;
static int32_t my_path_lounge = DEF_PATH_LOUNGE;
static int32_t my_path_regression = DEF_PATH_REGRESSION_SLOW;
static int32_t my_path_hystere = DEF_PATH_HYST;
static int32_t my_hop_penalty = DEF_HOP_PENALTY;
static int32_t my_late_penalty = DEF_LATE_PENAL;
static int32_t new_rt_dismissal_div100 = DEF_NEW_RT_DISMISSAL;
static int32_t my_link_window = DEF_HELLO_SQN_WINDOW;
//int32_t link_ignore_min = DEF_LINK_IGNORE_MIN;
//int32_t link_ignore_max = DEF_LINK_IGNORE_MAX;
//TODO: evaluate my_fmetric_exp_offset based on max configured dev_metric_max:
//static int32_t my_fmetric_exp_offset = DEF_FMETRIC_EXP_OFFSET;
//TODO: reevaluate my_dev_metric_max based on deduced my_fmetric_exp_offset (see above)
//UMETRIC_T my_dev_metric_max = umetric_max(DEF_FMETRIC_EXP_OFFSET);
static
void (*path_metric_algos[BIT_METRIC_ALGO_ARRSZ])
(UMETRIC_T *path_out, UMETRIC_T *umetric_max, UMETRIC_T lp) = {NULL};
static UMETRIC_T UMETRIC_MAX_SQRT;
static UMETRIC_T U64_MAX_HALF_SQRT;
#define U64_MAX_HALF (U64_MAX>>1)
#define UMETRIC_MAX_SQRT_SQUARE (UMETRIC_MAX_SQRT * UMETRIC_MAX_SQRT)
#define U64_MAX_HALF_SQRT_SQUARE (U64_MAX_HALF_SQRT * U64_MAX_HALF_SQRT)
//TODO: remove:
UMETRIC_T UMETRIC_NBDISCOVERY_MIN = (UMETRIC_MAX/10);
FMETRIC_U16_T fmetric(uint8_t mantissa, uint8_t exp)
{
FMETRIC_U16_T fm;
fm.val.f.mantissa_fm16 = mantissa;
fm.val.f.exp_fm16 = exp;
return fm;
}
UMETRIC_T umetric(uint8_t mantissa, uint8_t exp)
{
return fmetric_to_umetric(fmetric(mantissa, exp));
}
IDM_T is_umetric_valid(const UMETRIC_T *um)
{
assertion(-500704, (um));
return ( *um <= UMETRIC_MAX);
}
IDM_T is_fmetric_valid(FMETRIC_U16_T fm)
{
return fm.val.f.mantissa_fm16 <= OGM_MANTISSA_MASK && (
fm.val.f.exp_fm16 < OGM_EXPONENT_MAX || (
fm.val.f.exp_fm16 == OGM_EXPONENT_MAX && fm.val.f.mantissa_fm16 <= OGM_MANTISSA_MAX));
}
IDM_T fmetric_cmp(FMETRIC_U16_T a, unsigned char cmp, FMETRIC_U16_T b)
{
assertion(-500706, (is_fmetric_valid(a) && is_fmetric_valid(b)));
switch (cmp) {
case '!':
return (a.val.u16 != b.val.u16);
case '<':
return (a.val.u16 < b.val.u16);
case '[':
return (a.val.u16 <= b.val.u16);
case '=':
return (a.val.u16 == b.val.u16);
case ']':
return (a.val.u16 >= b.val.u16);
case '>':
return (a.val.u16 > b.val.u16);
}
assertion(-500707, (0));
return FAILURE;
}
UMETRIC_T fmetric_to_umetric(FMETRIC_U16_T fm)
{
TRACE_FUNCTION_CALL;
assertion(-500680, (is_fmetric_valid(fm)));
return (((UMETRIC_T) 1) << (fm.val.f.exp_fm16 + OGM_EXPONENT_OFFSET)) +
(((UMETRIC_T) fm.val.f.mantissa_fm16) << (fm.val.f.exp_fm16));
}
FMETRIC_U16_T umetric_to_fmetric(UMETRIC_T val)
{
TRACE_FUNCTION_CALL;
FMETRIC_U16_T fm = {.val.u16 = 0};
if( val < UMETRIC_MIN__NOT_ROUTABLE ) {
//assign minimum possible value:
fm.val.f.exp_fm16 = 0;
fm.val.f.mantissa_fm16 = OGM_MANTISSA_INVALID;
} else if ( val >= UMETRIC_MAX ) {
//assign maximum possible value:
fm.val.f.exp_fm16 = OGM_EXPONENT_MAX;
fm.val.f.mantissa_fm16 = OGM_MANTISSA_MAX;
} else {
uint8_t exp_sum = 0;
UMETRIC_T tmp = 0;
tmp = val + (val/UMETRIC_TO_FMETRIC_INPUT_FIX);
LOG2(exp_sum, tmp, UMETRIC_T);
fm.val.f.exp_fm16 = (exp_sum - OGM_EXPONENT_OFFSET);
fm.val.f.mantissa_fm16 = ( (tmp>>(exp_sum-OGM_MANTISSA_BIT_SIZE)) - (1<<OGM_MANTISSA_BIT_SIZE) );
assertion(-501025, (tmp >= val));
assertion(-501026, (val > (1<<OGM_EXPONENT_OFFSET)));
assertion(-501027, (exp_sum >= OGM_EXPONENT_OFFSET));
assertion(-501028, ((tmp>>(exp_sum-OGM_MANTISSA_BIT_SIZE)) >= (1<<OGM_EXPONENT_OFFSET)));
}
/*
#ifdef EXTREME_PARANOIA
UMETRIC_T reverse = fmetric_to_umetric(fm);
int32_t failure = - ((int32_t)((val<<10)/(val?val:1))) + ((int32_t)((reverse<<10)/(val?val:1)));
dbgf_track(DBGT_INFO, "val=%-12ju tmp=%-12ju reverse=%-12ju failure=%5d/1024 exp_sum=%2d exp=%d mantissa=%d",
val, tmp, reverse, failure, exp_sum, fm.val.fu16_exp, fm.val.fu16_mantissa);
#endif
*/
assertion(-500681, (is_fmetric_valid(fm)));
return fm;
}
char *umetric_to_human(UMETRIC_T val) {
#define UMETRIC_TO_HUMAN_ARRAYS 4
static char out[UMETRIC_TO_HUMAN_ARRAYS][12] = {{0},{0},{0},{0}};
static uint8_t p=0;
if (val < UMETRIC_MIN__NOT_ROUTABLE) {
return "INVALID";
} else if (val <= UMETRIC_MIN__NOT_ROUTABLE) {
return "noROUTE";
} else if (val <= UMETRIC_ROUTABLE) {
return "ROUTE ";
} else {
p = ((p + 1) % UMETRIC_TO_HUMAN_ARRAYS);
if (val < 100000) {
sprintf(out[p], "%ju", val);
} else if (val < 100000000) {
sprintf(out[p], "%juK", val/1000);
} else if (val < 100000000000) {
sprintf(out[p], "%juM", val/1000000);
} else if (val < 100000000000000) {
sprintf(out[p], "%juG", val/1000000000);
}
return out[p];
}
return NULL;
}
FMETRIC_U16_T fmetric_u8_to_fmu16( FMETRIC_U8_T fmu8 ) {
FMETRIC_U16_T fm = {.val.f =
{
.mantissa_fm16 = (fmu8.val.f.mantissa_fmu8<<(OGM_MANTISSA_BIT_SIZE - FM8_MANTISSA_BIT_SIZE)),
.exp_fm16 = fmu8.val.f.exp_fmu8
}
};
assertion(-501032, (is_fmetric_valid(fm)));
return fm;
}
FMETRIC_U8_T fmetric_to_fmu8( FMETRIC_U16_T fm ) {
assertion(-501035, (is_fmetric_valid(fm)));
FMETRIC_U8_T fmu8 = {.val.f =
{
.mantissa_fmu8 = (FM8_MANTISSA_MASK & ((fm.val.f.mantissa_fm16) >> (OGM_MANTISSA_BIT_SIZE - FM8_MANTISSA_BIT_SIZE))),
.exp_fmu8 = fm.val.f.exp_fm16
}
};
return fmu8;
}
UMETRIC_T fmetric_u8_to_umetric( FMETRIC_U8_T fmu8 ) {
return fmetric_to_umetric(fmetric_u8_to_fmu16(fmu8));
}
FMETRIC_U8_T umetric_to_fmu8( UMETRIC_T *um )
{
return fmetric_to_fmu8(umetric_to_fmetric(*um));
}
STATIC_INLINE_FUNC
FMETRIC_U16_T fmetric_substract_min(FMETRIC_U16_T f)
{
if (f.val.f.mantissa_fm16) {
f.val.f.mantissa_fm16--;
} else if (f.val.f.exp_fm16) {
f.val.f.mantissa_fm16 = OGM_MANTISSA_MASK;
f.val.f.exp_fm16--;
}
return f;
}
STATIC_INLINE_FUNC
UMETRIC_T umetric_substract_min(const UMETRIC_T *val)
{
return fmetric_to_umetric(fmetric_substract_min(umetric_to_fmetric(*val)));
}
STATIC_INLINE_FUNC
UMETRIC_T umetric_multiply_normalized(UMETRIC_T a, UMETRIC_T b)
{
if (b < UMETRIC_MULTIPLY_MAX)
return (a * b) / UMETRIC_MAX;
else
return (a * ((b << UMETRIC_SHIFT_MAX) / UMETRIC_MAX)) >> UMETRIC_SHIFT_MAX;
}
STATIC_INLINE_FUNC
UMETRIC_T umetric_fast_sqrt(float x)
{
return (((1.0f) / fast_inverse_sqrt(x)) + 0.5f);
}
STATIC_INLINE_FUNC
UMETRIC_T umetric_multiply_sqrt(UMETRIC_T um, UMETRIC_T x)
{
ASSERTION(-501076, (x <= UMETRIC_MAX));
return (um * umetric_fast_sqrt(x)) / UMETRIC_MAX_SQRT;
}
UMETRIC_T umetric_to_the_power_of_n(UMETRIC_T x, uint8_t n_exp_numerator, uint8_t n_exp_divisor)
{
assertion(-501077, (n_exp_divisor == 1 || n_exp_divisor == 2));
switch ( n_exp_numerator ) {
case 0:
return UMETRIC_MAX;
case 1:
return (n_exp_divisor == 1 ? x : umetric_fast_sqrt(x) * UMETRIC_MAX_SQRT);
case 2:
return (n_exp_divisor == 1 ? umetric_multiply_normalized(x, x) : x);
case 3:
return (n_exp_divisor == 1 ?
umetric_multiply_normalized(x, umetric_multiply_normalized(x, x)) :
(x * umetric_fast_sqrt(x)) / UMETRIC_MAX_SQRT);
}
return 0;
}
STATIC_FUNC
void path_metricalgo_MultiplyQuality(UMETRIC_T *path, UMETRIC_T *linkMax, UMETRIC_T linkQuality)
{
*path = umetric_multiply_normalized(*path, linkQuality);
}
STATIC_FUNC
void path_metricalgo_ExpectedQuality(UMETRIC_T *path, UMETRIC_T *linkMax, UMETRIC_T linkQuality)
{
if (*path < 2 || linkQuality < 2)
*path = UMETRIC_MIN__NOT_ROUTABLE;
else
*path = (U64_MAX / ((U64_MAX / *path) + (U64_MAX / linkQuality)));
}
STATIC_FUNC
void path_metricalgo_MultiplyBandwidth(UMETRIC_T *path, UMETRIC_T *linkMax, UMETRIC_T linkQuality)
{
*path = umetric_multiply_normalized(XMIN(*path, *linkMax), linkQuality);
}
STATIC_FUNC
void path_metricalgo_ExpectedBandwidth(UMETRIC_T *path, UMETRIC_T *linkMax, UMETRIC_T linkQuality)
{
UMETRIC_T linkBandwidth = umetric_multiply_normalized(*linkMax, linkQuality);
if (*path < 2 || linkBandwidth < 2)
*path = UMETRIC_MIN__NOT_ROUTABLE;
else
*path = (U64_MAX / ((U64_MAX / *path) + (U64_MAX / linkBandwidth)));
}
STATIC_FUNC
void path_metricalgo_VectorBandwidth(UMETRIC_T *path, UMETRIC_T *linkMax, UMETRIC_T linkQuality)
{
assertion(-501085, (*path > UMETRIC_MIN__NOT_ROUTABLE));
UMETRIC_T inverseSquaredPathBandwidth = 0;
UMETRIC_T inverseSquaredLinkQuality = 0;
UMETRIC_T rootOfSum = 0;
UMETRIC_T path_out = UMETRIC_MIN__NOT_ROUTABLE;
UMETRIC_T linkBandwidth = umetric_multiply_normalized(*linkMax, linkQuality);
UMETRIC_T maxPrecisionScaler = XMIN(*path, linkBandwidth) * U64_MAX_HALF_SQRT;
if (linkQuality > UMETRIC_MIN__NOT_ROUTABLE) {
inverseSquaredPathBandwidth = ((maxPrecisionScaler / *path) * (maxPrecisionScaler / *path));
inverseSquaredLinkQuality = ((maxPrecisionScaler / linkBandwidth) * (maxPrecisionScaler / linkBandwidth));
rootOfSum = umetric_fast_sqrt(inverseSquaredPathBandwidth + inverseSquaredLinkQuality);
path_out = maxPrecisionScaler / rootOfSum;
}
dbgf_all( DBGT_INFO,
"pb=%-12ju max_extension=%-19ju (me/pb)^2=%-19ju lp=%-12ju link=%-12ju lb=%-12ju (me/lb)^2=%-19ju ufs=%-12ju UMETRIC_MIN=%ju -> path_out=%ju",
*path, maxPrecisionScaler, inverseSquaredPathBandwidth, linkQuality, *linkMax,
linkBandwidth, inverseSquaredLinkQuality, rootOfSum, UMETRIC_MIN__NOT_ROUTABLE, path_out);
*path = path_out;
}
STATIC_FUNC
void register_path_metricalgo(uint8_t algo_type_bit, void (*algo) (UMETRIC_T *path_out, UMETRIC_T *umetric_max, UMETRIC_T lp))
{
assertion(-500838, (!path_metric_algos[algo_type_bit]));
assertion(-500839, (algo_type_bit < BIT_METRIC_ALGO_ARRSZ));
path_metric_algos[algo_type_bit] = algo;
}
UMETRIC_T apply_metric_algo(UMETRIC_T *linkQuality, UMETRIC_T *linkMax, const UMETRIC_T *path, struct host_metricalgo *algo)
{
TRACE_FUNCTION_CALL;
assertion(-501037, ((*path & ~UMETRIC_MASK) == 0));
assertion(-501038, (*path <= UMETRIC_MAX));
assertion(-501039, (*path >= UMETRIC_MIN__NOT_ROUTABLE));
ALGO_T unsupported_algos = 0;
ALGO_T algo_type = algo->algo_type;
UMETRIC_T max_out = umetric_substract_min(path);
UMETRIC_T path_out = *path;
if (max_out <= UMETRIC_MIN__NOT_ROUTABLE)
return UMETRIC_MIN__NOT_ROUTABLE;
if (algo_type) {
while (algo_type) {
uint8_t algo_type_bit;
LOG2(algo_type_bit, algo_type, ALGO_T);
algo_type -= (0x01 << algo_type_bit);
if (path_metric_algos[algo_type_bit]) {
(*(path_metric_algos[algo_type_bit])) (&path_out, linkMax, *linkQuality);
dbgf_all(DBGT_INFO, "algo=%d in=%-12ju=%7s out=%-12ju=%7s",
algo_type_bit, *path, umetric_to_human(*path), path_out, umetric_to_human(path_out));
} else {
unsupported_algos |= (0x01 << algo_type_bit);
}
}
if (unsupported_algos) {
uint8_t i = bits_count(unsupported_algos);
dbgf_sys(DBGT_WARN,
"unsupported %s=%d (0x%X) - Need an update?! - applying pessimistic ETTv0 %d times",
ARG_PATH_METRIC_ALGO, unsupported_algos, unsupported_algos, i);
while (i--)
(*(path_metric_algos[BIT_METRIC_ALGO_EB])) (&path_out, linkMax, *linkQuality);
}
}
if (algo->hop_penalty)
path_out = (path_out * ((UMETRIC_T) (MAX_HOP_PENALTY - algo->hop_penalty))) >> MAX_HOP_PENALTY_PRECISION_EXP;
if (path_out <= UMETRIC_MIN__NOT_ROUTABLE)
return UMETRIC_MIN__NOT_ROUTABLE;
if (path_out > max_out) {
dbgf_all(DBGT_WARN, "out=%ju > out_max=%ju, %s=%d, path=%ju",
path_out, max_out, ARG_PATH_METRIC_ALGO, algo->algo_type, *path);
}
return XMIN(path_out, max_out); // ensure out always decreases
}
STATIC_FUNC
UMETRIC_T apply_lndev_metric_algo(struct link_dev_node *lndev, const UMETRIC_T *path, struct host_metricalgo *algo)
{
TRACE_FUNCTION_CALL;
assertion(-500823, (lndev->key.dev->umetric_max));
UMETRIC_T tq = umetric_to_the_power_of_n(lndev->timeaware_tx_probe, algo->algo_tp_exp_numerator, algo->algo_tp_exp_divisor);
UMETRIC_T rq = umetric_to_the_power_of_n(lndev->timeaware_rx_probe, algo->algo_rp_exp_numerator, algo->algo_rp_exp_divisor);
UMETRIC_T lq = umetric_multiply_normalized(tq, rq);
return apply_metric_algo(&lq, &lndev->key.dev->umetric_max, path, algo);
}
STATIC_FUNC
void _reconfigure_metric_record_position(const char *f, struct metric_record *rec, struct host_metricalgo *alg,
SQN_T min, SQN_T in, uint8_t sqn_bit_size, uint8_t reset)
{
TRACE_FUNCTION_CALL;
assertion(-500737, (XOR(sqn_bit_size, rec->sqn_bit_mask)));
if (sqn_bit_size)
rec->sqn_bit_mask = (~(SQN_MAX << sqn_bit_size));
assertion(-500738, (rec->sqn_bit_mask == U16_MAX || rec->sqn_bit_mask == U8_MAX ||
rec->sqn_bit_mask == HELLO_SQN_MAX));
if (rec->clr && /*alg->window_size > 1 &&*/ ((rec->sqn_bit_mask)&(in - rec->clr)) > (alg->lounge_size + 1)) {
dbgf_track(DBGT_WARN, "reset_metric=%d sqn_bit_size=%d sqn_bit_mask=0x%X umetric=%ju clr=%d to ((in=%d) - (lounge=%d))=%d",
reset, sqn_bit_size, rec->sqn_bit_mask, rec->umetric, rec->clr, in, alg->lounge_size, ((rec->sqn_bit_mask)& (in - alg->lounge_size)));
}
rec->clr = MAX_UXX(rec->sqn_bit_mask, min, ((rec->sqn_bit_mask)&(in - alg->lounge_size)));
rec->set = ((rec->sqn_bit_mask)&(rec->clr - 1));
if (reset) {
rec->umetric = 0;
}
dbgf_all(DBGT_WARN, "%s reset_metric=%d sqn_bit_size=%d sqn_bit_mask=0x%X min=%d in=%d lounge_size=%d umetric=%ju clr=%d",
f, reset, sqn_bit_size, rec->sqn_bit_mask, min, in, alg->lounge_size, rec->umetric, rec->clr);
}
#define reconfigure_metric_record_position(rec, alg, min, in, sqn_bit_size, reset) \
_reconfigure_metric_record_position(__func__, rec, alg, min, in, sqn_bit_size, reset)
STATIC_FUNC
IDM_T update_metric_record(struct orig_node *on, struct router_node *rt, SQN_T in, const UMETRIC_T *probe)
{
TRACE_FUNCTION_CALL;
char *scenario = NULL;
struct metric_record *rec = &rt->mr;
struct host_metricalgo *alg = on->path_metricalgo;
SQN_T range = on->ogmSqn_rangeSize;
SQN_T min = on->ogmSqn_rangeMin;
SQN_T i, purge = 0;
SQN_T dbg_clr;
SQN_T dbg_set;
dbg_clr = rec->clr; // avoid unused warning
dbg_set = rec->set; // avoid unused warning
IDM_T ret = FAILURE;
ASSERTION(-500739, (!((~(rec->sqn_bit_mask))&(min))));
ASSERTION(-500740, (!((~(rec->sqn_bit_mask))&(in))));
ASSERTION(-500741, (!((~(rec->sqn_bit_mask))&(rec->clr))));
ASSERTION(-500742, (!((~(rec->sqn_bit_mask))&(rec->set))));
assertion(-500743, (range <= MAX_SQN_RANGE));
assertion(-500908, (is_umetric_valid(&rec->umetric)));
assertion(-500174, (IMPLIES(probe, (*probe <= UMETRIC_MAX))));
if (((rec->sqn_bit_mask)&(rec->clr - min)) >= range) {
if (rec->clr) {
dbgf_track(DBGT_ERR, "sqn_bit_mask=0x%X clr=%d out of range, in=%d valid_min=%d defined_range=%d",
rec->sqn_bit_mask, rec->clr, in, min, range);
}
reconfigure_metric_record_position(rec, alg, min, in, 0, YES/*reset_metric*/);
}
if (probe && !is_umetric_valid(probe)) {
scenario = "probe contains illegal value";
goto update_metric_error;
} else if (((rec->sqn_bit_mask)&(in - min)) >= range) {
scenario = "in out of defined_range (sector J)";
goto update_metric_error;
} else if (((rec->sqn_bit_mask)&(rec->clr - min)) >= range) {
scenario = "clr out of defined_range (sector J)";
goto update_metric_error;
} else if (((rec->sqn_bit_mask)&(rec->clr - rec->set)) > 1) {
scenario = "set invalid (compared to clr)";
goto update_metric_error;
}
if (UXX_LE(rec->sqn_bit_mask, in, (rec->clr - alg->window_size))) {
scenario = "in within valid past (sector I|H)";
goto update_metric_success;
} else if (in == rec->set) {
// assertion(-501071, (in == rec->clr));
if ( probe ) {
if ((*probe == 0 || *probe > rec->umetric))
rec->umetric = *probe;
}
} else if (UXX_LE(rec->sqn_bit_mask, in, rec->set)) {
scenario = "in within (closed) window, in < set (sector F|G)";
goto update_metric_success;
} else if (probe) {
dbgf_all(DBGT_INFO,"in=%d min=%d range=%d clr=%d", in, min, range, rec->clr);
assertion(-500708, (UXX_LE(rec->sqn_bit_mask, in, min + range)));
assertion(-500721, (UXX_GE(rec->sqn_bit_mask, in, rec->clr)));
purge = ((rec->sqn_bit_mask)&(in - rec->clr));
if (purge > 2) {
// scenario = "resetting and updating: probe!=NULL, in within lounge or valid future (sector E|D|C|B|A)";
}
if (alg->regression == 1 /*|| alg->flags & TYP_METRIC_FLAG_STRAIGHT*/) {
rec->umetric = *probe;
} else {
if (purge < alg->window_size) {
for (i = 0; i < purge; i++)
rec->umetric -= (rec->umetric / alg->regression);
} else {
reconfigure_metric_record_position(rec, alg, min, in, 0, YES/*reset_metric*/);
}
if ((rec->umetric += (*probe / alg->regression)) > *probe) {
dbgf_track(DBGT_WARN, "resulting path metric=%ju > probe=%ju", rec->umetric, *probe);
rec->umetric = *probe;
}
}
rec->clr = rec->set = in;
} else if (UXX_LE(rec->sqn_bit_mask, in, rec->clr + alg->lounge_size)) {
//scenario = "ignoring: probe==NULL, in within lounge (sector E,D,C)";
} else {
assertion(-500709, (UXX_LE(rec->sqn_bit_mask, in, min + range)));
//[20609 1347921] ERROR metric_record_init: reset_metric=0 sqn_bit_size=0 sqn_bit_mask=0xFF clr=186 to ((in=188) - (lounge=1))=187
purge = ((rec->sqn_bit_mask)&((in - alg->lounge_size) - rec->clr));
if (purge > 2)
scenario = "purging: probe==NULL, in within valid future (sector B|A)";
assertion(-500710, (purge > 0));
if (purge >= alg->window_size) {
reconfigure_metric_record_position(rec, alg, min, in, 0, YES/*reset_metric*/);
} else {
for (i = 0; i < purge; i++)
rec->umetric -= (rec->umetric / alg->regression);
reconfigure_metric_record_position(rec, alg, min, in, 0, NO/*reset_metric*/);
}
}
assertion(-500711, (is_umetric_valid(&rec->umetric)));
goto update_metric_success;
update_metric_success:
ret = SUCCESS;
update_metric_error:
if (scenario) {
dbgf_track(ret == FAILURE ? DBGT_ERR : DBGT_WARN,
"[%s] sqn_bit_mask=0x%X in=%d clr=%d>%d set=%d>%d valid_min=%d range=%d lounge=%d window=%d purge=%d probe=%ju ",
scenario, rec->sqn_bit_mask, in, dbg_clr, rec->clr, dbg_set, rec->set, min, range, alg->lounge_size, alg->window_size, purge, probe ? *probe : 0);
}
EXITERROR(-500712, (ret != FAILURE));
if (on && on->curr_rt_local == rt && rec->umetric < on->path_metricalgo->umetric_min)
set_ogmSqn_toBeSend_and_aggregated(on, on->ogmMetric_next, on->ogmSqn_send, on->ogmSqn_send);
return ret;
}
STATIC_FUNC
UMETRIC_T timeaware_rx_probe(struct link_dev_node *lndev)
{
if (((TIME_T) (bmx_time - lndev->rx_probe_record.hello_time_max)) < RP_ADV_DELAY_TOLERANCE)
return lndev->rx_probe_record.hello_umetric;
if (((TIME_T) (bmx_time - lndev->rx_probe_record.hello_time_max)) < RP_ADV_DELAY_RANGE) {
return (lndev->rx_probe_record.hello_umetric *
((UMETRIC_T) (RP_ADV_DELAY_RANGE - (bmx_time - lndev->rx_probe_record.hello_time_max)))) /
RP_ADV_DELAY_RANGE;
}
return 0;
}
STATIC_FUNC
UMETRIC_T timeaware_tx_probe(struct link_dev_node *lndev)
{
if (((TIME_T) (bmx_time - lndev->key.link->local->rp_adv_time)) < TP_ADV_DELAY_TOLERANCE)
return lndev->tx_probe_umetric;
if (((TIME_T) (bmx_time - lndev->key.link->local->rp_adv_time)) < TP_ADV_DELAY_RANGE) {
return (lndev->tx_probe_umetric *
((UMETRIC_T) (TP_ADV_DELAY_RANGE - (bmx_time - lndev->key.link->local->rp_adv_time)))) /
TP_ADV_DELAY_RANGE;
}
return 0;
}
void lndev_assign_best(struct local_node *only_local, struct link_dev_node *only_lndev )
{
TRACE_FUNCTION_CALL;
assertion(-501133, (IMPLIES(only_lndev, only_local && only_local == only_lndev->key.link->local)));
ASSERTION(-500792, (IMPLIES(only_lndev, only_lndev->key.link == avl_find_item(&only_local->link_tree, &only_lndev->key.link->key.dev_idx))));
dbgf_all(DBGT_INFO, "only_local=%X only_lndev.link=%s only_lndev.dev=%s",
only_local ? ntohl(only_local->local_id) : 0,
only_lndev ? ipFAsStr(&only_lndev->key.link->link_ip) : DBG_NIL,
only_lndev ? only_lndev->key.dev->ifname_label.str : DBG_NIL);
struct avl_node *local_an = NULL;
struct local_node *local;
while ((local = only_local) || (local = avl_iterate_item(&local_tree, &local_an))) {
assertion(-500794, (local->link_tree.items));
struct link_node *link = NULL;
struct avl_node *link_an = NULL;
if (local->best_rp_lndev)
local->best_rp_lndev->timeaware_rx_probe = timeaware_rx_probe(local->best_rp_lndev);
if (local->best_tp_lndev)
local->best_tp_lndev->timeaware_tx_probe = timeaware_tx_probe(local->best_tp_lndev);
dbgf_all(DBGT_INFO, "local_id=%X", ntohl(local->local_id));
while ((only_lndev && (link = only_lndev->key.link)) || (link = avl_iterate_item(&local->link_tree, &link_an))) {
struct link_dev_node *lndev = NULL;
struct link_dev_node *prev_lndev = NULL;
dbgf_all(DBGT_INFO, "link=%s", ipFAsStr(&link->link_ip));
while ((only_lndev && (lndev = only_lndev)) || (lndev = list_iterate(&link->lndev_list, lndev))) {
dbgf_all(DBGT_INFO, "lndev=%s items=%d",
lndev->key.dev->ifname_label.str, link->lndev_list.items);
lndev->timeaware_rx_probe = timeaware_rx_probe(lndev);
lndev->timeaware_tx_probe = timeaware_tx_probe(lndev);
if (!local->best_rp_lndev || local->best_rp_lndev->timeaware_rx_probe < lndev->timeaware_rx_probe)
local->best_rp_lndev = lndev;
if (!local->best_tp_lndev || local->best_tp_lndev->timeaware_tx_probe < lndev->timeaware_tx_probe)
local->best_tp_lndev = lndev;
if (only_lndev)
break;
assertion(-501134, (prev_lndev != lndev));
prev_lndev = lndev;
}
if (only_lndev)
break;
}
assertion(-500406, (local->best_rp_lndev));
assertion(-501086, (local->best_tp_lndev));
if (local->best_tp_lndev->timeaware_tx_probe == 0)
local->best_tp_lndev = local->best_rp_lndev;
local->best_lndev = (local->best_tp_lndev == local->best_rp_lndev) ? local->best_rp_lndev : NULL;
if(only_local)
break;
}
}
void update_link_probe_record(struct link_dev_node *lndev, HELLO_SQN_T sqn, uint8_t probe)
{
TRACE_FUNCTION_CALL;
struct link_node *link = lndev->key.link;
struct lndev_probe_record *lpr = &lndev->rx_probe_record;
ASSERTION(-501049, ((sizeof (((struct lndev_probe_record*) NULL)->hello_array)) * 8 == MAX_HELLO_SQN_WINDOW));
assertion(-501050, (probe <= 1));
ASSERTION(-501055, (bits_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1) == lpr->hello_sum));
if ((link->hello_time_max || link->hello_sqn_max) && link->hello_sqn_max != sqn &&
((HELLO_SQN_MASK)&(link->hello_sqn_max - sqn)) < HELLO_SQN_TOLERANCE)
return;
if (((HELLO_SQN_MASK)&(sqn - lpr->hello_sqn_max)) >= my_link_window) {
memset(lpr->hello_array, 0, MAX_HELLO_SQN_WINDOW/8);
ASSERTION(-500159, is_zero(lpr->hello_array, MAX_HELLO_SQN_WINDOW / 8));
if (probe)
bit_set(lpr->hello_array, MAX_HELLO_SQN_WINDOW, sqn, 1);
lpr->hello_sum = probe;
dbgf_all(DBGT_INFO, "probe=%d probe_sum=%d %d",
probe, lpr->hello_sum, bits_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1));
ASSERTION(-501058, (bits_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1) == lpr->hello_sum));
} else {
if (sqn != lpr->hello_sqn_max) {
HELLO_SQN_T prev_sqn_min = (HELLO_SQN_MASK)&(lpr->hello_sqn_max + 1 - ((HELLO_SQN_T) my_link_window));
HELLO_SQN_T new_sqn_min_minus_one = (HELLO_SQN_MASK)&(sqn - ((HELLO_SQN_T) my_link_window));
dbgf_all(DBGT_INFO, "prev_min=%5d prev_max=%d new_min=%5d sqn=%5d sum=%3d bits=%3d %s",
prev_sqn_min,lpr->hello_sqn_max, new_sqn_min_minus_one+1, sqn, lpr->hello_sum,
bits_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1),
bits_print(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1));
lpr->hello_sum -= bits_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, prev_sqn_min, new_sqn_min_minus_one);
dbgf_all(DBGT_INFO, "prev_min=%5d prev_max=%d new_min=%5d sqn=%5d sum=%3d bits=%3d %s",
prev_sqn_min,lpr->hello_sqn_max, new_sqn_min_minus_one+1, sqn, lpr->hello_sum,
bits_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1),
bits_print(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1));
bits_clear(lpr->hello_array, MAX_HELLO_SQN_WINDOW, prev_sqn_min, new_sqn_min_minus_one, HELLO_SQN_MASK);
dbgf_all(DBGT_INFO, "prev_min=%5d prev_max=%d new_min=%5d sqn=%5d sum=%3d bits=%3d %s\n",
prev_sqn_min,lpr->hello_sqn_max, new_sqn_min_minus_one+1, sqn, lpr->hello_sum,
bits_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1),
bits_print(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1));
}
ASSERTION(-501057, (bits_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1) == lpr->hello_sum));
if (!bit_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, sqn) && probe) {
bit_set(lpr->hello_array, MAX_HELLO_SQN_WINDOW, sqn, 1);
lpr->hello_sum++;
}
ASSERTION(-501056, (bits_get(lpr->hello_array, MAX_HELLO_SQN_WINDOW, 0, MAX_HELLO_SQN_WINDOW - 1) == lpr->hello_sum));
}
lpr->hello_sqn_max = sqn;
lpr->hello_umetric = (UMETRIC_MAX / my_link_window) * lpr->hello_sum;
lpr->hello_time_max = bmx_time;
link->hello_sqn_max = sqn;
link->hello_time_max = bmx_time;
lndev_assign_best(link->local, lndev);
dbgf_all(DBGT_INFO, "%s metric %ju", ipFAsStr(&link->link_ip), lndev->timeaware_rx_probe);
}
STATIC_FUNC
struct router_node * router_node_create(struct local_node *local, struct orig_node *on, OGM_SQN_T ogm_sqn_max)
{
struct router_node* rt = debugMallocReset(sizeof (struct router_node), -300222);
rt->local_key = local;
reconfigure_metric_record_position(&rt->mr, on->path_metricalgo, on->ogmSqn_rangeMin, ogm_sqn_max, OGM_SQN_BIT_SIZE, YES/*reset_metric*/);
avl_insert(&on->rt_tree, rt, -300223);
return rt;
}
STATIC_FUNC
UMETRIC_T lndev_best_via_router(struct local_node *local, struct orig_node *on, UMETRIC_T *ogm_metric, struct link_dev_node **path_lndev_best)
{
UMETRIC_T metric_best = 0;
// find best path lndev for this local router:
if (local->best_lndev) {
metric_best = apply_lndev_metric_algo(local->best_lndev, ogm_metric, on->path_metricalgo);
*path_lndev_best = local->best_lndev;
} else {
struct avl_node *link_an = NULL;
struct link_node *link;
while ((link = avl_iterate_item(&local->link_tree, &link_an))) {
struct link_dev_node *lndev_tmp = NULL;
while ((lndev_tmp = list_iterate(&link->lndev_list, lndev_tmp))) {
UMETRIC_T um = apply_lndev_metric_algo(lndev_tmp, ogm_metric, on->path_metricalgo);
if (metric_best <= um) {
metric_best = um;
*path_lndev_best = lndev_tmp;
}
}
}
}
assertion(-501088, (*path_lndev_best));
return metric_best;
}
IDM_T update_path_metrics(struct packet_buff *pb, struct orig_node *on, OGM_SQN_T ogm_sqn, UMETRIC_T *ogm_metric)
{
TRACE_FUNCTION_CALL;
assertion(-500876, (!on->blocked));
assertion(-500734, (on->path_metricalgo));
assertion(-501052, ((((OGM_SQN_MASK)&(ogm_sqn - on->ogmSqn_rangeMin)) < on->ogmSqn_rangeSize)));
OGM_SQN_T ogm_sqn_max = UXX_GET_MAX(OGM_SQN_MASK, on->ogmSqn_maxRcvd, ogm_sqn);
dbgf_all(DBGT_INFO, "global_id=%s orig_sqn %d via neigh %s", globalIdAsString(&on->global_id), ogm_sqn, pb->i.llip_str);
if (UXX_LT(OGM_SQN_MASK, ogm_sqn, (OGM_SQN_MASK & (ogm_sqn_max - on->path_metricalgo->lounge_size)))) {
dbgf_track(DBGT_WARN, "dropping late sqn=%d via neigh=%s from global_id=%s",
ogm_sqn, pb->i.llip_str, globalIdAsString(&on->global_id));
return SUCCESS;
}
if (UXX_LT(OGM_SQN_MASK, ogm_sqn, on->ogmSqn_next) || (ogm_sqn == on->ogmSqn_next && *ogm_metric <= on->ogmMetric_next)) {
dbgf_all(DBGT_WARN, "dropping already scheduled sqn=%d via neigh=%s from global_id=%s",
ogm_sqn, pb->i.llip_str, globalIdAsString(&on->global_id));
return SUCCESS;
}
struct local_node *local = pb->i.lndev->key.link->local;
struct router_node *next_rt = NULL;
struct router_node *prev_rt = on->curr_rt_local;
IDM_T is_ogm_sqn_new = UXX_GT(OGM_SQN_MASK, ogm_sqn, on->ogmSqn_maxRcvd);
struct link_dev_node *best_rt_lndev = NULL;
UMETRIC_T best_rt_metric = lndev_best_via_router(local, on, ogm_metric, &best_rt_lndev);
struct router_node *rt = NULL;
if (is_ogm_sqn_new || (prev_rt && prev_rt->local_key == local && prev_rt->mr.umetric > best_rt_metric)) {
struct router_node *rt_tmp;