-
Notifications
You must be signed in to change notification settings - Fork 0
/
olsr-driver.c
2043 lines (1743 loc) · 70.4 KB
/
olsr-driver.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
#include "ross.h"
#include "olsr.h"
#include <assert.h>
/**
* @file
* @brief OLSR Driver
*
* Simple driver to test out various functionalities in the OLSR impl.
*/
double g_X[OLSR_MAX_NEIGHBORS];
double g_Y[OLSR_MAX_NEIGHBORS];
#define GRID_MAX 100
#define STAGGER_MAX 10
#define HELLO_DELTA 0.0001
#define OLSR_NO_FINAL_OUTPUT 1
#define USE_RADIO_DISTANCE 1
#define RWALK_INTERVAL 20
#define DEBUG 0
unsigned int nlp_per_pe = OLSR_MAX_NEIGHBORS;
// Used as scratch space for MPR calculations
unsigned g_Dy[OLSR_MAX_NEIGHBORS];
unsigned g_num_one_hop;
neigh_tuple g_mpr_one_hop[OLSR_MAX_NEIGHBORS];
unsigned g_num_two_hop;
two_hop_neigh_tuple g_mpr_two_hop[OLSR_MAX_2_HOP];
unsigned g_reachability[OLSR_MAX_NEIGHBORS];
neigh_tuple g_mpr_neigh_to_add;
unsigned g_mpr_num_add_nodes;
char g_covered[BITNSLOTS(OLSR_MAX_NEIGHBORS)];
char g_olsr_mobility = 'N';
unsigned long long g_olsr_event_stats[OLSR_END_EVENT];
unsigned long long g_olsr_root_event_stats[OLSR_END_EVENT];
char *event_names[OLSR_END_EVENT] = {
"HELLO_RX",
"HELLO_TX",
"TC_RX",
"TC_TX",
"SA_RX",
"SA_TX",
"SA_MASTER_TX",
"SA_MASTER_RX",
"RWALK_CHANGE"
};
FILE *olsr_event_log=NULL;
unsigned region(o_addr a)
{
return a / OLSR_MAX_NEIGHBORS;
}
unsigned int SA_range_start;
/**
* Returns the lpid of the master SA aggregator for the region containing lpid.
* For example, if OMN = 16 then we have 16 OLSR nodes followed by one master
* on each pe.
*/
o_addr sa_master_for_level(o_addr lpid)
{
// Get the region number
int rnum = region(lpid);
// Now correct for all the LPs before this aggregator
rnum += SA_range_start * tw_nnodes();
//printf("We're sending SA data to node %d\n", rnum);
return rnum;
}
/**
*/
o_addr master_hierarchy(o_addr lpid, int level)
{
long val;
//printf("master_hiearchy(%lld,%d): ", lpid, level);
val = (long)pow(2.0, (double)level);
//printf("(val=%ld) ", val);
// First, normalize the lpid
lpid -= SA_range_start * tw_nnodes();
assert(lpid >= 0);
//printf("%lld -> ", lpid);
lpid /= val;
lpid *= val;
//printf("%lld -> ", lpid);
lpid += SA_range_start * tw_nnodes();
//printf("%lld\n", lpid);
return lpid;
}
/**
* Initializer for OLSR
*/
void olsr_init(node_state *s, tw_lp *lp)
{
hello *h;
TC *t;
tw_event *e;
olsr_msg_data *msg;
tw_stime ts;
int i;
#if DEBUG
fprintf( olsr_event_log, "OLSR Init LP %d RNG Seeds Are: ", lp->gid);
rng_write_state( lp->rng, olsr_event_log );
#endif
//s->num_tuples = 0;
s->num_neigh = 0;
s->num_two_hop = 0;
s->num_mpr = 0;
s->num_mpr_sel = 0;
s->num_top_set = 0;
s->num_dupes = 0;
for (i = 0; i < OLSR_MAX_NEIGHBORS; i++) {
s->SA_per_node[i] = 0;
}
// Now we store the GID as opposed to an int from 0-OMN
s->local_address = lp->gid;// % OLSR_MAX_NEIGHBORS;
s->lng = tw_rand_unif(lp->rng) * GRID_MAX;
s->lat = tw_rand_unif(lp->rng) * GRID_MAX;
// printf("Initializing node %lu on CPU %llu\n", lp->gid, lp->pe->id);
//g_X[s->local_address] = s->lng;
//g_Y[s->local_address] = s->lat;
// Build our initial HELLO_TX messages
ts = tw_rand_unif(lp->rng) * STAGGER_MAX;
e = tw_event_new(lp->gid, ts, lp);
msg = tw_event_data(e);
msg->type = HELLO_TX;
msg->originator = s->local_address;
msg->lng = s->lng;
msg->lat = s->lat;
h = &msg->mt.h;
h->num_neighbors = 0;
tw_event_send(e);
// Build our initial TC_TX messages
ts = tw_rand_unif(lp->rng) * STAGGER_MAX;
e = tw_event_new(lp->gid, ts, lp);
msg = tw_event_data(e);
msg->type = TC_TX;
msg->originator = s->local_address;
msg->lng = s->lng;
msg->lat = s->lat;
t = &msg->mt.t;
//t->num_mpr_sel = 0;
t->num_neighbors = 0;
tw_event_send(e);
// Build our initial SA_TX messages
ts = tw_rand_unif(lp->rng) * STAGGER_MAX + SA_INTERVAL;
e = tw_event_new(lp->gid, ts, lp);
msg = tw_event_data(e);
msg->type = SA_TX;
msg->originator = s->local_address;
msg->destination = MASTER_NODE;
msg->lng = s->lng;
msg->lat = s->lat;
tw_event_send(e);
if (g_olsr_mobility != 'n' && g_olsr_mobility != 'N') {
// Build our initial RWALK_CHANGE messages
ts = tw_rand_unif(lp->rng) * RWALK_INTERVAL + 1.0;
e = tw_event_new(lp->gid, ts, lp);
msg = tw_event_data(e);
msg->type = RWALK_CHANGE;
msg->lng = tw_rand_unif(lp->rng) * GRID_MAX;
msg->lat = tw_rand_unif(lp->rng) * GRID_MAX;
tw_event_send(e);
}
#if 1 /* Source of instability if done naively */
// Build our initial SA_MASTER_TX messages
if (s->local_address == MASTER_NODE) {
ts = tw_rand_unif(lp->rng) * MASTER_SA_INTERVAL + MASTER_SA_INTERVAL;
e = tw_event_new(lp->gid, ts, lp);
//e = tw_event_new(sa_master_for_level(lp->gid), ts, lp);
msg = tw_event_data(e);
msg->type = SA_MASTER_TX;
msg->originator = s->local_address;
// Always send these to node zero, who receives all SA_MASTER msgs
msg->destination = sa_master_for_level(lp->gid);
msg->lng = s->lng;
msg->lat = s->lat;
tw_event_send(e);
}
#endif
}
void sa_master_init(node_state *s, tw_lp *lp)
{
#if DEBUG
fprintf( olsr_event_log, "SA Master Init LP %d RNG Seeds Are: ", lp->gid);
rng_write_state( lp->rng, olsr_event_log );
#endif
s->local_address = lp->gid;
//printf("I am an SA master and my local_address is %lu\n", s->local_address);
}
/**
Copied from ns3 - propogation-loss-model.cc
*/
double
DoCalcRxPower (double txPowerDbm,
node_state *s,
olsr_msg_data *m)
{
/*
* Friis free space equation:
* where Pt, Gr, Gr and P are in Watt units
* L is in meter units.
*
* P Gt * Gr * (lambda^2)
* --- = ---------------------
* Pt (4 * pi * d)^2 * L
*
* Gt: tx gain (unit-less)
* Gr: rx gain (unit-less)
* Pt: tx power (W)
* d: distance (m)
* L: system loss
* lambda: wavelength (m)
*
* Here, we ignore tx and rx gain and the input and output values
* are in dB or dBm:
*
* lambda^2
* rx = tx + 10 log10 (-------------------)
* (4 * pi * d)^2 * L
*
* rx: rx power (dB)
* tx: tx power (dB)
* d: distance (m)
* L: system loss (unit-less)
* lambda: wavelength (m)
*/
double sender_lng = m->lng;
double sender_lat = m->lat;
double receiver_lng = s->lng;
double receiver_lat = s->lat;
// We have to make sure that everyone is in the same region even though
// they may have overlapping x/y coordinates, i.e. a region describes the
// local plane of existence for the nodes
assert(region(s->local_address) == region(m->originator));
double distance = (sender_lng - receiver_lng) * (sender_lng - receiver_lng);
distance += (sender_lat - receiver_lat) * (sender_lat - receiver_lat);
distance = sqrt(distance);
//double distance = a->GetDistanceFrom (b);
double m_minDistance = 1.0; // A reasonable default
if (distance <= m_minDistance)
{
return txPowerDbm;
}
//double m_lambda = 3.0e8 / 2437000000.0; // (2.437 GHz, chan 6)
double m_lambda = 0.058; // Stolen from Ken's slides, ~ 5GHz
double numerator = m_lambda * m_lambda;
double denominator = 16 * M_PI * M_PI * distance * distance;// * m_systemLoss;
double pr = 10 * log10 (numerator / denominator);
//printf("distance=%fm, attenuation coefficient=%fdB\n", distance, pr);
return txPowerDbm + pr;
}
#define RANGE 60.0
static inline int out_of_radio_range(node_state *s, olsr_msg_data *m)
{
#if USE_RADIO_DISTANCE
const double range = RANGE;
double sender_lng = m->lng;
double sender_lat = m->lat;
double receiver_lng = s->lng;
double receiver_lat = s->lat;
// We have to make sure that everyone is in the same region even though
// they may have overlapping x/y coordinates, i.e. a region describes the
// local plane of existence for the nodes
assert(region(s->local_address) == region(m->originator));
double dist = (sender_lng - receiver_lng) * (sender_lng - receiver_lng);
dist += (sender_lat - receiver_lat) * (sender_lat - receiver_lat);
dist = sqrt(dist);
if (dist > range) {
return 1;
}
return 0;
#else
if (DoCalcRxPower(OLSR_MPR_POWER, s, m) < -96.0)
return 1;
return 0;
#endif
}
/**
* Compute D(y) as described in the "MPR Computation" section. Description:
*
* The degree of an one hop neighbor node y (where y is a member of N), is
* defined as the number of symmetric neighbors of node y, EXCLUDING all the
* members of N and EXCLUDING the node performing the computation.
*
* N is the subset of neighbors of the node, which are neighbors of the
* interface I.
*/
static inline unsigned Dy(node_state *s, o_addr target)
{
int i, j, in;
o_addr temp[OLSR_MAX_NEIGHBORS];
int temp_size = 0;
for (i = 0; i < s->num_two_hop; i++) {
in = 0;
for (j = 0; j < s->num_neigh; j++) {
if (s->twoHopSet[i].twoHopNeighborAddr == s->neighSet[j].neighborMainAddr) {
// EXCLUDING all members of N...
in = 1;
continue;
}
}
if (in) continue;
if (s->twoHopSet[i].neighborMainAddr == target) {
in = 0;
// Add s->twoHopSet[i].twoHopNeighborAddr to this set
for (j = 0; j < temp_size; j++) {
if (temp[j] == s->twoHopSet[i].twoHopNeighborAddr) {
in = 1;
}
}
if (!in) {
temp[temp_size] = s->twoHopSet[i].twoHopNeighborAddr;
temp_size++;
assert(temp_size < OLSR_MAX_NEIGHBORS);
}
}
}
return temp_size;
}
/**
* Remove "n" from N2 (stored in g_mpr_two_hop)
*/
static inline void remove_node_from_n2(o_addr n)
{
int i;
int index_to_remove;
while (1) {
index_to_remove = -1;
for (i = 0; i < g_num_two_hop; i++) {
if (g_mpr_two_hop[i].twoHopNeighborAddr == n) {
index_to_remove = i;
break;
}
}
if (index_to_remove == -1) break;
//printf("Removing %d\n", index_to_remove);
g_mpr_two_hop[index_to_remove].expirationTime = g_mpr_two_hop[g_num_two_hop-1].expirationTime;
g_mpr_two_hop[index_to_remove].neighborMainAddr = g_mpr_two_hop[g_num_two_hop-1].neighborMainAddr;
g_mpr_two_hop[index_to_remove].twoHopNeighborAddr = g_mpr_two_hop[g_num_two_hop-1].twoHopNeighborAddr;
g_num_two_hop--;
}
}
/**
* Ensure that all nodes in MPR set are unique (hence "set")
*/
void mpr_set_uniq(node_state *s)
{
int i;
// Presumably if we just added a MPR, we only need to check all the
// others against the last one
o_addr last = s->mprSet[s->num_mpr-1];
for (i = 0; i < s->num_mpr - 1; i++) {
if (s->mprSet[i] == last) {
s->num_mpr--;
return;
}
}
}
/**
* Ensure that all nodes in MPR selector set are unique (hence "set")
*/
void mpr_sel_set_uniq(node_state *s)
{
int i;
// Presumably if we just added a MPR, we only need to check all the
// others against the last one
o_addr last = s->mprSelSet[s->num_mpr_sel-1].mainAddr;
for (i = 0; i < s->num_mpr_sel - 1; i++) {
if (s->mprSelSet[i].mainAddr == last) {
s->num_mpr_sel--;
return;
}
}
s->ansn++;
}
/**
* Direct ripoff of corresponding ns3 function
*/
top_tuple * FindNewerTopologyTuple(o_addr last, uint16_t ansn, node_state *s)
{
int i;
for (i = 0; i < s->num_top_set; i++) {
if (s->topSet[i].lastAddr == last && s->topSet[i].sequenceNumber > ansn)
return &s->topSet[i];
}
return NULL;
}
/**
* Direct ripoff of corresponding ns3 function
*/
void EraseOlderTopologyTuples(o_addr last, uint16_t ansn, node_state *s)
{
int i;
int index_to_remove;
while (1) {
index_to_remove = -1;
for (i = 0; i < s->num_top_set; i++) {
if (s->topSet[i].lastAddr == last && s->topSet[i].sequenceNumber < ansn) {
index_to_remove = i;
break;
}
}
if (index_to_remove == -1) break;
s->topSet[index_to_remove].destAddr = s->topSet[s->num_top_set-1].destAddr;
s->topSet[index_to_remove].expirationTime = s->topSet[s->num_top_set-1].expirationTime;
s->topSet[index_to_remove].lastAddr = s->topSet[s->num_top_set-1].lastAddr;
s->topSet[index_to_remove].sequenceNumber = s->topSet[s->num_top_set-1].sequenceNumber;
s->num_top_set--;
}
}
/**
* Direct ripoff of corresponding ns3 function
*/
top_tuple * FindTopologyTuple(o_addr destAddr, o_addr lastAddr, node_state *s)
{
int i;
for (i = 0; i < s->num_top_set; i++) {
if (s->topSet[i].destAddr == destAddr && s->topSet[i].lastAddr == lastAddr) {
return &s->topSet[i];
}
}
return NULL;
}
neigh_tuple * FindSymNeighborTuple(node_state *s, o_addr mainAddr)
{
int i;
for (i = 0; i < s->num_neigh; i++) {
if (s->neighSet[i].neighborMainAddr == mainAddr) {
return &s->neighSet[i];
}
}
return NULL;
}
RT_entry * Lookup(node_state *s, o_addr dest)
{
int i;
for (i = 0; i < s->num_routes; i++) {
if (s->route_table[i].destAddr == dest) {
return &s->route_table[i];
}
}
return NULL;
}
/**
* Trying to ripoff the corresponding ns3 function :)
* Fortunately we don't need steps 4 or 5 since we don't support
* multiple interfaces or HNA.
*/
void RoutingTableComputation(node_state *s)
{
int i, h;
RT_entry *route;
// 1. All the entries from the routing table are removed.
s->num_routes = 0;
// 2. The new routing entries are added starting with the
// symmetric neighbors (h=1) as the destination nodes.
for (i = 0; i < s->num_neigh; i++) {
s->route_table[s->num_routes].destAddr = s->neighSet[i].neighborMainAddr;
s->route_table[s->num_routes].nextAddr = s->neighSet[i].neighborMainAddr;
s->route_table[s->num_routes].distance = 1;
s->num_routes++;
assert(s->num_routes < OLSR_MAX_ROUTES);
}
// 3. for each node in N2, i.e., a 2-hop neighbor which is not a
// neighbor node or the node itself, and such that there exist at
// least one entry in the 2-hop neighbor set where
// N_neighbor_main_addr correspond to a neighbor node with
// willingness different of WILL_NEVER,
for (i = 0; i < s->num_two_hop; i++) {
if (FindSymNeighborTuple(s, s->twoHopSet[i].twoHopNeighborAddr)) {
continue;
}
if (s->twoHopSet[i].twoHopNeighborAddr == s->local_address) {
continue;
}
// one selects one 2-hop tuple and creates one entry in the routing table with:
// R_dest_addr = the main address of the 2-hop neighbor;
// R_next_addr = the R_next_addr of the entry in the
// routing table with:
// R_dest_addr == N_neighbor_main_addr
// of the 2-hop tuple;
// R_dist = 2;
// R_iface_addr = the R_iface_addr of the entry in the
// routing table with:
// R_dest_addr == N_neighbor_main_addr
// of the 2-hop tuple;
if ((route = Lookup(s, s->twoHopSet[i].neighborMainAddr))) {
s->route_table[s->num_routes].destAddr = s->twoHopSet[i].twoHopNeighborAddr;
s->route_table[s->num_routes].nextAddr = route->nextAddr;
s->route_table[s->num_routes].distance = 2;
s->num_routes++;
assert(s->num_routes < OLSR_MAX_ROUTES);
}
}
// 3.1. For each topology entry in the topology table, if its
// T_dest_addr does not correspond to R_dest_addr of any
// route entry in the routing table AND its T_last_addr
// corresponds to R_dest_addr of a route entry whose R_dist
// is equal to h, then a new route entry MUST be recorded in
// the routing table (if it does not already exist)
for (h = 2; ; h++) {
int added = 0;
for (i = 0; i < s->num_top_set; i++) {
//printf("Looking at node %lu top_tuple[%d] dest: %lu, last: %lu, seq: %d\n", s->local_address, i, s->topSet[i].destAddr, s->topSet[i].lastAddr, s->topSet[i].sequenceNumber);
RT_entry *destAddrEntry = Lookup(s, s->topSet[i].destAddr);
RT_entry *lastAddrEntry = Lookup(s, s->topSet[i].lastAddr);
if (!destAddrEntry && lastAddrEntry && lastAddrEntry->distance == h) {
s->route_table[s->num_routes].destAddr = s->topSet[i].destAddr;
s->route_table[s->num_routes].nextAddr = lastAddrEntry->nextAddr;
s->route_table[s->num_routes].distance = h + 1;
s->num_routes++;
assert(s->num_routes < OLSR_MAX_ROUTES);
added = 1;
}
else {
if (lastAddrEntry && !destAddrEntry) {
//printf("Not right length!\n");
}
}
}
if (!added) {
break;
}
}
}
dup_tuple * FindDuplicateTuple(o_addr addr, uint16_t seq_num, node_state *s)
{
int i;
for (i = 0; i < s->num_dupes; i++) {
if (s->dupSet[i].address == addr && s->dupSet[i].sequenceNumber == seq_num) {
return &s->dupSet[i];
}
}
return NULL;
}
/**
* Had to add this function to minimize our dupe array
*/
void AddDuplicate(o_addr originator,
uint16_t seq_num,
Time ts,
int retransmitted,
node_state *s,
tw_lp *lp)
{
int i;
int index_to_remove;
Time exp = tw_now(lp);
while (1) {
index_to_remove = -1;
for (i = 0; i < s->num_dupes; i++) {
if (s->dupSet[i].expirationTime < exp) {
index_to_remove = i;
break;
}
}
if (index_to_remove == -1) break;
//printf("Expiring Dupe\n");
s->dupSet[index_to_remove].address = s->dupSet[s->num_dupes-1].address;
s->dupSet[index_to_remove].expirationTime = s->dupSet[s->num_dupes-1].expirationTime;
s->dupSet[index_to_remove].retransmitted = s->dupSet[s->num_dupes-1].retransmitted;
s->dupSet[index_to_remove].sequenceNumber = s->dupSet[s->num_dupes-1].sequenceNumber;
s->num_dupes--;
}
if (s->num_dupes == OLSR_MAX_DUPES - 1) {
// Find the oldest and replace it
int oldest = 0;
for (i = 0; i < s->num_dupes; i++) {
if (s->dupSet[i].expirationTime < s->dupSet[oldest].expirationTime) {
oldest = i;
}
}
//printf("node %lu (lpid = %llu) evicting dup %d (%lu) at time %f\n", s->local_address, lp->gid,
// oldest, s->dupSet[oldest].address, tw_now(lp));
s->dupSet[oldest].address = originator;
s->dupSet[oldest].sequenceNumber = seq_num;
s->dupSet[oldest].expirationTime = ts;
s->dupSet[oldest].retransmitted = retransmitted;
}
else {
s->dupSet[s->num_dupes].address = originator;
s->dupSet[s->num_dupes].sequenceNumber = seq_num;
s->dupSet[s->num_dupes].expirationTime = ts;
s->dupSet[s->num_dupes].retransmitted = retransmitted;
s->num_dupes++;
assert(s->num_dupes < OLSR_MAX_DUPES);
}
}
void printTC(olsr_msg_data *m, node_state *s)
{
#ifdef JML_DEBUG
int i;
printf("Node %lu has %d neighbors:\n", s->local_address, s->num_neigh);
for (i = 0; i < s->num_neigh; i++) {
printf(" neighbor %lu\n", s->neighSet[i].neighborMainAddr);
}
printf("Received TC message with %d neighbors of node %lu\n",
m->mt.t.num_neighbors, m->originator);
for (i = 0; i < m->mt.t.num_neighbors; i++) {
printf(" TC-NEIGH %lu\n", m->mt.t.neighborAddresses[i]);
}
printf("\n");
#endif /* JML_DEBUG */
//printf("Node %lu just heard a TC from %lu\n", s->local_address, m->originator);
}
///
/// \brief OLSR's default forwarding algorithm.
///
/// See RFC 3626 for details.
///
/// \param p the %OLSR packet which has been received.
/// \param msg the %OLSR message which must be forwarded.
/// \param dup_tuple NULL if the message has never been considered for forwarding,
/// or a duplicate tuple in other case.
/// \param local_iface the address of the interface where the message was received from.
///
void ForwardDefault(olsr_msg_data *olsrMessage,
dup_tuple *duplicated,
o_addr localIface,
o_addr senderAddress,
node_state *s,
tw_lp *lp)
{
int i;
int j;
TC *t;
tw_event *e;
tw_stime ts;
tw_lp *cur_lp;
olsr_msg_data *msg;
// If the sender interface address is not in the symmetric
// 1-hop neighborhood the message must not be forwarded
if (NULL == FindSymNeighborTuple(s, senderAddress)) {
return;
}
// If the message has already been considered for forwarding,
// it must not be retransmitted again
if (duplicated != NULL && duplicated->retransmitted)
{
return;
}
// If the sender interface address is an interface address
// of a MPR selector of this node and ttl is greater than 1,
// the message must be retransmitted
int retransmitted = 0;
for (i = 0; i < s->num_mpr_sel; i++) {
if (s->mprSelSet[i].mainAddr == senderAddress) {
// Round-robin-RX
// Might want to rename HELLO_DELTA...
ts = g_tw_lookahead + tw_rand_unif(lp->rng) * HELLO_DELTA;
// ts += 1;
cur_lp = tw_getlocal_lp(region(s->local_address)*OLSR_MAX_NEIGHBORS);
e = tw_event_new(cur_lp->gid, ts, lp);
msg = tw_event_data(e);
msg->type = TC_RX;
msg->ttl = olsrMessage->ttl - 1;
msg->originator = olsrMessage->originator;
msg->sender = s->local_address;
msg->lng = s->lng;
msg->lat = s->lat;
msg->target = region(s->local_address) * OLSR_MAX_NEIGHBORS;
t = &msg->mt.t;
t->ansn = olsrMessage->mt.t.ansn;
//t->num_mpr_sel = olsrMessage->mt.t.num_mpr_sel;
t->num_neighbors = olsrMessage->mt.t.num_neighbors;
for (j = 0; j < t->num_neighbors; j++) {
t->neighborAddresses[j] = olsrMessage->mt.t.neighborAddresses[j];
}
//if (t->num_mpr_sel > 0) {
//printTC(t);
tw_event_send(e);
//}
//tw_event_send(e);
retransmitted = 1;
// TODO: Check if ns3 stuff is required here... Possibly
// actually do the retransmission here.
}
}
if (duplicated != NULL) {
duplicated->expirationTime = tw_now(lp) + OLSR_DUP_HOLD_TIME;
duplicated->retransmitted = retransmitted;
}
else {
AddDuplicate(olsrMessage->originator,
olsrMessage->seq_num,
tw_now(lp) + OLSR_DUP_HOLD_TIME,
retransmitted,
s, lp);
// s->dupSet[s->num_dupes].address = olsrMessage->originator;
// s->dupSet[s->num_dupes].sequenceNumber = olsrMessage->seq_num;
// s->dupSet[s->num_dupes].expirationTime = tw_now(lp) + OLSR_DUP_HOLD_TIME;
// s->dupSet[s->num_dupes].retransmitted = retransmitted;
// s->num_dupes++;
// assert(s->num_dupes < OLSR_MAX_DUPES);
}
}
void route_packet(node_state *s, tw_event *e)
{
olsr_msg_data *m = tw_event_data(e);
RT_entry * route = Lookup(s, m->destination);
if (route == NULL) {
printf("Node %lu doesn't have a route to %lu\n", s->local_address, m->destination);
return;
}
m->ttl--;
//printf("routing from %lu to %lu, next hop %lu\n", m->originator,
// m->destination, route->nextAddr);
m->sender = route->nextAddr;
tw_event_send(e);
}
void process_sa(node_state *s, olsr_msg_data *m)
{
s->SA_per_node[m->originator % OLSR_MAX_NEIGHBORS]++;
}
/**
* Event handler. Basically covers two events at the moment:
* - HELLO_TX: HELLO transmit required now, so package up all of our
* neighbors into a message and send it. Also schedule our next TX
* - HELLO_RX: HELLO received so we just pull the neighbor's address from
* the message. HELLO_RX messages are generated by the last HELLO_RX
* message until we have exhausted all neighbors.
* - TC_TX: Similar to HELLO_TX but for Topology Control
* - TC_RX: Similar to HELLO_RX but for Topology Control
*/
void olsr_event(node_state *s, tw_bf *bf, olsr_msg_data *m, tw_lp *lp)
{
int in;
int i, j, k;
int is_mpr;
TC *t;
hello *h;
tw_event *e;
tw_stime ts;
tw_lp *cur_lp;
olsr_msg_data *msg;
//latlng *ll;
//latlng_cluster *llc;
#if DEBUG
fprintf( olsr_event_log, "OLSR Event: LP %d Type %d at TS = %lf RNGs:", lp->gid, m->type, tw_now(lp) );
rng_write_state( lp->rng, olsr_event_log );
if( lp->gid == 1023 ) {
printf("LP DUMP Node %llu on Rank %d at TS=%lf: S Local Address = %llu, M Type = %d,M Sender = %llu, M Originator = %llu \n",
lp->gid, g_tw_mynode, tw_now(lp), s->local_address, m->type, m->sender, m->originator );
}
#endif /* DEBUG */
#if ENABLE_OPTIMISTIC
if( g_tw_synchronization_protocol == OPTIMISTIC )
{
memcpy( &(m->state_copy), s, sizeof(node_state));
}
#endif
g_olsr_event_stats[m->type]++;
switch(m->type) {
case HELLO_TX:
{
ts = g_tw_lookahead + tw_rand_unif(lp->rng) * HELLO_DELTA;
cur_lp = tw_getlocal_lp(region(s->local_address)*OLSR_MAX_NEIGHBORS);
e = tw_event_new(cur_lp->gid, ts, lp);
msg = tw_event_data(e);
msg->type = HELLO_RX;
msg->originator = m->originator;
msg->lng = s->lng;
msg->lat = s->lat;
msg->target = tw_getlocal_lp(region(s->local_address)*OLSR_MAX_NEIGHBORS)->gid;
h = &msg->mt.h;
h->num_neighbors = s->num_neigh;// + 1;
//h->neighbor_addrs[0] = s->local_address;
for (j = 0; j < s->num_neigh; j++) {
h->neighbor_addrs[j] = s->neighSet[j].neighborMainAddr;
// If s->neighSet[j].neighborMainAddr is our MPR, we need
// to set this appropriately
is_mpr = 0;
for (k = 0; k < s->num_mpr; k++) {
if (s->mprSet[k] == s->neighSet[j].neighborMainAddr) {
is_mpr = 1;
}
}
if (is_mpr) {
h->is_mpr[j] = 1;
}
else {
h->is_mpr[j] = 0;
}
}
tw_event_send(e);
e = tw_event_new(lp->gid, HELLO_INTERVAL, lp);
msg = tw_event_data(e);
msg->type = HELLO_TX;
msg->originator = s->local_address;
msg->lng = s->lng;
msg->lat = s->lat;
h = &msg->mt.h;
h->num_neighbors = 0;//1;
//h->neighbor_addrs[0] = s->local_address;
tw_event_send(e);
break;
}
case HELLO_RX:
{
h = &m->mt.h;
in = 0;
// If we receive our own message, don't add ourselves but
// DO generate a new event for the next guy!
// Copy the message we just received; we can't add data to
// a message sent by another node
if (m->target < region(s->local_address)*OLSR_MAX_NEIGHBORS+OLSR_MAX_NEIGHBORS-1) {
ts = g_tw_lookahead + tw_rand_unif(lp->rng) * HELLO_DELTA;
tw_lp *cur_lp = tw_getlocal_lp(m->target + 1);
e = tw_event_new(cur_lp->gid, ts, lp);
msg = tw_event_data(e);
msg->type = HELLO_RX;
msg->originator = m->originator;
msg->sender = m->sender;
msg->lng = m->lng;
msg->lat = m->lat;
msg->target = m->target + 1;
h = &msg->mt.h;
h->num_neighbors = m->mt.h.num_neighbors;//m->num_neigh + 1;
//h->neighbor_addrs[0] = s->local_address;
for (j = 0; j < h->num_neighbors; j++) {
h->neighbor_addrs[j] = m->mt.h.neighbor_addrs[j];
//h->neighbor_addrs[j] = s->neighSet[j].neighborMainAddr;
}
tw_event_send(e);
}
// We've already passed along the message which has to happen
// regardless of whether or not it can be heard, handled, etc.
// Check to see if we can hear this message or not
if (out_of_radio_range(s, m)) {
//printf("Out of range!\n");
return;
}
if (s->local_address == m->originator) {
return;
}
// BEGIN 1-HOP PROCESSING
for (i = 0; i < s->num_neigh; i++) {
if (s->neighSet[i].neighborMainAddr == m->originator) {
in = 1;
}
}
if (!in) {
s->neighSet[s->num_neigh].neighborMainAddr = m->originator;
s->num_neigh++;
assert(s->num_neigh < OLSR_MAX_NEIGHBORS);
assert(region(s->local_address) == region(m->originator));
s->ansn++;
}
// END 1-HOP PROCESSING
// BEGIN 2-HOP PROCESSING
h = &m->mt.h;
for (i = 0; i < h->num_neighbors; i++) {
if (s->local_address == h->neighbor_addrs[i]) {
// We are not going to be our own 2-hop neighbor!
continue;
}
// Check and see if h->neighbor_addrs[i] is in our list
// already
in = 0;
for (j = 0; j < s->num_two_hop; j++) {