forked from josemariasaldana/ns3_802.11_frame_aggregation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi-central-controlled-aggregation_v140.cc
3948 lines (3208 loc) · 168 KB
/
wifi-central-controlled-aggregation_v140.cc
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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2017 Jose Saldana, University of Zaragoza ([email protected])
*
* This work has been partially financed by the EU H2020 Wi-5 project (G.A. no: 644262).
*
* If you use this code, please cite the next research article:
*
* Jose Saldana, Jose Ruiz-Mas, Jose Almodovar, "Frame Aggregation in Central Controlled
* 802.11 WLANs: the Latency vs. Throughput Trade-off," IEEE Communications Letters,
* vol.21, no. 2, pp. 2500-2530, Nov. 2017. ISSN 1089-7798.
* http://dx.doi.org/10.1109/LCOMM.2017.2741940
*
* http://ieeexplore.ieee.org/document/8013762/
* Author's self-archive version: http://diec.unizar.es/~jsaldana/personal/amsterdam_2017_in_proc.pdf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Some parts are inspired on https://www.nsnam.org/doxygen/wifi-aggregation_8cc.html, by Sebastien Deronne
* Other parts are inspired on https://www.nsnam.org/doxygen/wifi-wired-bridging_8cc_source.html
* The flow monitor part is inspired on https://www.nsnam.org/doxygen/wifi-hidden-terminal_8cc_source.html
* The association record is inspired on https://github.com/MOSAIC-UA/802.11ah-ns3/blob/master/ns-3/scratch/s1g-mac-test.cc
* The hub is inspired on https://www.nsnam.org/doxygen/csma-bridge_8cc_source.html
*
* v140
* Developed and tested for ns-3.26, although the simulation crashes in some cases. One example:
* - more than one AP
* - set the RtsCtsThreshold below 48000
* - AMPDU aggregation On
* - Ideal or Minstrel wifi manager
*
* This problem does not exist in ns3-devel (ns-3-dev-444dfd0968eb, Jun 2017).
*/
// PENDING
/*
1) make the 'unset assoc' change the channel to that of the closest AP
Two possibilities:
- WORKING: the STA deassociates by itself. I have to use Yanswifi.
- Not working yet: That it needs the new AP to send beacons to it. In this second option,
the command 'addoperational channel' should be used on each STA.
see https://www.nsnam.org/doxygen/classns3_1_1_spectrum_wifi_phy.html#a948c6d197accf2028529a2842ec68816
2) To separate this file into a number of them, using .h files.
3) Modify the deadlines in ns-3.26/src/internet/model/arp-cache.cc
I have gone to this file and put
- AliveTimeout as 12000 instead of 120
- DeadTimeout as 10000 instead of 100
*/
//
// The network scenario includes
// - a number of STA wifi nodes, with different mobility patterns
// - a number of AP wifi nodes. They are distributed in rows and columns in an area
// - a number of servers: each of them communicates with one STA (it is the origin or the destination of the packets)
//
// On each AP node, there is
// - a csma device
// - a wifi device
// - a bridge that binds both interfaces the whole thing into one network
//
// IP addresses:
// - the STAs have addresses 10.0.0.0 (mask 255.255.0.0)
// - the servers behind the router (only in topology 2) have addresses 10.1.0.0 (mask 255.255.0.0)
//
// There are three topologies:
//
// topology = 0
//
// (*)
// +--|-+ 10.0.0.0
// (*) |STA1| csma +-----------+ csma +--------+
// +--|-+ +----+ +---------------| hub |--------| single | All the server applications
// |STA0| | +-----------+ | server | are in this node
// +----+ | | +--------+
// | csma|
// +-------------|--+ +------------|--+
// | +----+ +----+ | | +----+ +----+ |
// ((*))--|WIFI| |CSMA| | ((*))--|WIFI| |CSMA| |
// | +----+ +----+ | | +----+ +----+ |
// | | | | | | | |
// | +----------+ | | +---------+ |
// | | BRIDGE | | | | BRIDGE | |
// | +----------+ | | +---------+ |
// +----------------+ +---------------+
// AP 0 AP 1
//
//
//
// topology = 1 (DEFAULT)
//
// (*)
// +--|-+ 10.0.0.0
// (*) |STA1| csma +-----------+ csma
// +--|-+ +----+ +---------------| hub |----------------------------------------+
// |STA0| | +-----------+----------------------+ |
// +----+ | | | |
// | csma| | |
// +-------------|--+ +------------|--+ +--------+ +--------+
// | +----+ +----+ | | +----+ +----+ | | +----+ | | +----+ |
// ((*))--|WIFI| |CSMA| | ((*))--|WIFI| |CSMA| | | |CSMA| | | |CSMA| | ...
// | +----+ +----+ | | +----+ +----+ | | +----+ | | +----+ |
// | | | | | | | | | | | |
// | +----------+ | | +---------+ | +--------+ +--------+
// | | BRIDGE | | | | BRIDGE | | server 0 server 1
// | +----------+ | | +---------+ |
// +----------------+ +---------------+ talks with talks with
// AP 0 AP 1 STA 0 STA 1
//
//
//
// topology = 2
//
// (*)
// +--|-+ 10.0.0.0 10.1.0.0
// (*) |STA1| csma +-----------+ csma +--------+ point to point
// +--|-+ +----+ +---------------| hub |--------| router |----------------------+
// |STA0| | +-----------+ | |----+ |
// +----+ | | +--------+ | |
// | csma| | |
// +-------------|--+ +------------|--+ +--------+ +--------+
// | +----+ +----+ | | +----+ +----+ | | +----+ | | +----+ |
// ((*))--|WIFI| |CSMA| | ((*))--|WIFI| |CSMA| | | |CSMA| | | |CSMA| | ...
// | +----+ +----+ | | +----+ +----+ | | +----+ | | +----+ |
// | | | | | | | | | | | |
// | +----------+ | | +---------+ | +--------+ +--------+
// | | BRIDGE | | | | BRIDGE | | server 0 server 1
// | +----------+ | | +---------+ |
// +----------------+ +---------------+ talks with talks with
// AP 0 AP 1 STA 0 STA 1
//
//
// When the default aggregation parameters are enabled, the
// maximum A-MPDU size is the one defined by the standard, and the throughput is maximal.
// When aggregation is disabled, the thoughput is lower
//
// Packets in this simulation can be marked with a QosTag so they
// will be considered belonging to different queues.
// By default, all the packets belong to the BestEffort Access Class (AC_BE).
//
// The user can select many parameters when calling the program. Examples:
// ns-3.26$ ./waf --run "scratch/wifi-central-controlled-aggregation --PrintHelp"
// ns-3.26$ ./waf --run "scratch/wifi-central-controlled-aggregation --number_of_APs=1 --nodeMobility=1 --nodeSpeed=0.1 --simulationTime=10 --distance_between_APs=20"
//
// if you want different results in different runs, use a different seed each time you call the program
// (see https://www.nsnam.org/docs/manual/html/random-variables.html). One example:
//
// ns-3.26$ NS_GLOBAL_VALUE="RngRun=3" ./waf --run "scratch/wifi-central-controlled-aggregation --simulationTime=2 --nodeMobility=3 --verboseLevel=2 --number_of_APs=10 --number_of_APs_per_row=1"
// you can call it with different values of RngRun to obtain different realizations
//
// for being able to see the logs, do ns-3.26$ export NS_LOG=UdpEchoClientApplication=level_all
// or /ns-3-dev$ export 'NS_LOG=ArpCache=level_all'
// or /ns-3-dev$ export 'NS_LOG=ArpCache=level_error' for showing only errors
// see https://www.nsnam.org/docs/release/3.7/tutorial/tutorial_21.html
// see https://www.nsnam.org/docs/tutorial/html/tweaking.html#usinglogging
// Output files
// You can establish a 'name' and a 'surname' for the output files, using the parameters:
// --outputFileName=name --outputFileSurname=seed-1
//
// You can run a battery of tests with the same name and different surname, and you
// will finally obtain a single file name_average.txt with all the averaged results.
//
// Example: if you use --outputFileName=name --outputFileSurname=seed-1
// you will obtain the next output files:
//
// - name_average.txt it integrates all the tests with the same name, even if they have a different surname
// the file is not deleted, so each test with the same name is added at the bottom
// - name_seed-1_flows.txt information of all the flows of this run
// - name_seed-1_flow_1_delay_histogram.txt delay histogram of flow #1
// - name_seed-1_flow_1_jitter_histogram.txt
// - name_seed-1_flow_1_packetsize_histogram.txt
// - name_seed-1_flowmonitor.xml
// - name_seed-1_AP-0.2.pcap pcap file of the device 2 of AP #0
// - name_seed-1_server-2-1.pcap pcap file of the device 1 of server #2
// - name_seed-1_STA-8-1.pcap pcap file of the device 1 of STA #8
// - name_seed-1_hub.pcap pcap file of the hub connecting all the APs
#include "ns3/core-module.h"
#include "ns3/mobility-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/internet-module.h"
#include "ns3/bridge-helper.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/nstime.h"
#include "ns3/spectrum-module.h" // For the spectrum channel
#include "ns3/ipv4-static-routing-helper.h"
#include <sstream>
//#include "ns3/arp-cache.h" // If you want to do things with the ARPs
//#include "ns3/arp-header.h"
using namespace ns3;
// Maximum AMPDU size of 802.11n
#define MAXSIZE80211n 65535
// Maximum AMPDU size of 802.11ac
//https://groups.google.com/forum/#!topic/ns-3-users/T_21O5mGlgM
//802.11ac allows the maximum A-MPDU length to range from 8 KB to 1 MB. http://chimera.labs.oreilly.com/books/1234000001739/ch03.html#section-mac-agg
// The maximum transmission length is defined by time, and is a little less than 5.5 microseconds. At the highest data rates for 802.11ac,
//an aggregate frame can hold almost four and a half megabytes of data. Rather than represent such a large number of bytes in the PLCP header,
//which is transmitted at the lowest possible data rate, 802.11ac shifts the length indication to the MPDU delimiters that are transmitted
//as part of the high-data-rate payload
// http://www.rfwireless-world.com/Tutorials/802-11ac-MAC-layer.html
// Max. length of A-MPDU = 2^13+Exp -1 bytes
// Exp can range from 0 to 7, this yields A-MPDU to be of length:
// - 2^13 - 1 = 8191 (8KB)
// - 2^20 - 1 = 1,048,575 (about 1MB)
#define MAXSIZE80211ac 1000000
// Define a log component
NS_LOG_COMPONENT_DEFINE ("SimpleMpduAggregation");
/********* FUNCTIONS ************/
// Change the frequency of a STA
// Copied from https://groups.google.com/forum/#!topic/ns-3-users/Ih8Hgs2qgeg
// https://10343742895474358856.googlegroups.com/attach/1b7c2a3108d5e/channel-switch-minimal.cc?part=0.1&view=1&vt=ANaJVrGFRkTkufO3dLFsc9u1J_v2-SUCAMtR0V86nVmvXWXGwwZ06cmTSv7DrQUKMWTVMt_lxuYTsrYxgVS59WU3kBd7dkkH5hQsLE8Em0FHO4jx8NbjrPk
void ChangeFrequencyLocal
(NetDeviceContainer deviceslink, uint8_t channel, uint32_t mywifiModel, uint32_t myverbose) {
for (uint32_t i = 0; i < deviceslink.GetN (); i++)
{
Ptr<WifiNetDevice> wifidevice = DynamicCast<WifiNetDevice> (deviceslink.Get(i));
if (wifidevice == 0) std::cout << "[ChangeFrequencyLocal]\tWARNING: wifidevice IS NULL" << '\n';
Ptr<WifiPhy> phy0 = wifidevice->GetPhy();
phy0->SetChannelNumber (channel); //https://www.nsnam.org/doxygen/classns3_1_1_wifi_phy.html#a2d13cf6ae4c185cae8516516afe4a32a
/*
if (mywifiModel == 0) {
Ptr<WifiPhy> phy0 = wifidevice->GetPhy();
phy0->SetChannelNumber (channel);
} else {
Ptr<SpectrumWifiPhy> phy0 = wifidevice->GetPhy()->GetSpectrumPhy();
phy0->SetChannelNumber (channel);
}
*/
if (myverbose > 1)
std::cout << Simulator::Now()
<< "\t[ChangeFrequencyLocal]\tChanged channel on STA with MAC " << deviceslink.Get (i)->GetAddress ()
<< " to: " << uint16_t(channel) << std::endl;
}
}
/*********** This part is only for the ARPs. Not used **************/
typedef std::pair<Ptr<Packet>, Ipv4Header> Ipv4PayloadHeaderPair;
static void PrintArpCache (uint32_t nodenumber, Ptr <Node> node, Ptr <NetDevice> nd/*, Ptr <Ipv4Interface> interface*/)
{
std::cout << "Printing Arp Cache of Node#" << nodenumber << '\n';
Ptr <ArpL3Protocol> arpL3 = node->GetObject <ArpL3Protocol> ();
//Ptr <ArpCache> arpCache = arpL3->FindCache (nd);
//arpCache->Flush ();
// Get an interactor to Ipv4L3Protocol instance
Ptr<Ipv4L3Protocol> ip = node->GetObject<Ipv4L3Protocol> ();
NS_ASSERT(ip !=0);
// Get interfaces list from Ipv4L3Protocol iteractor
ObjectVectorValue interfaces;
ip->GetAttribute("InterfaceList", interfaces);
// For each interface
uint32_t l = 0;
for(ObjectVectorValue::Iterator j = interfaces.Begin(); j !=interfaces.End (); j ++)
{
// Get an interactor to Ipv4L3Protocol instance
Ptr<Ipv4Interface> ipIface = (j->second)->GetObject<Ipv4Interface> ();
NS_ASSERT(ipIface != 0);
std::cout << "Interface #" << l << " IP address" << /*<< */'\n';
l++;
// Get interfaces list from Ipv4L3Protocol iteractor
Ptr<NetDevice> device = ipIface->GetDevice();
NS_ASSERT(device != 0);
if (device == nd) {
// Get MacAddress assigned to this device
Mac48Address addr = Mac48Address::ConvertFrom(device->GetAddress ());
// For each Ipv4Address in the list of Ipv4Addresses assign to this interface...
for(uint32_t k = 0; k < ipIface->GetNAddresses (); k++)
{
// Get Ipv4Address
Ipv4Address ipAddr = ipIface->GetAddress (k).GetLocal();
// If Loopback address, go to the next
if(ipAddr == Ipv4Address::GetLoopback())
{
NS_LOG_UNCOND ("[PrintArpCache] Node #" << nodenumber << " " << addr << ", " << ipAddr << "");
} else {
NS_LOG_UNCOND ("[PrintArpCache] Node #" << nodenumber << " " << addr << ", " << ipAddr << "");
Ptr<ArpCache> m_arpCache = nd->GetObject<ArpCache> ();
m_arpCache = ipIface->GetObject<ArpCache> (); // FIXME: THIS DOES NOT WORK
//m_arpCache = node->GetObject<ArpCache> ();
//m_arpCache = nd->GetObject<ArpCache> ();
//m_arpCache->SetAliveTimeout(Seconds(7));
//if (m_arpCache != 0) {
NS_LOG_UNCOND ("[PrintArpCache] " << nodenumber << " " << addr << ", " << ipAddr << "");
AsciiTraceHelper asciiTraceHelper;
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("arpcache.txt");
m_arpCache->PrintArpCache(stream);
m_arpCache->Flush();
//ArpCache::Entry * entry = m_arpCache->Add(ipAddr);
//entry->MarkWaitReply(0);
//entry->MarkAlive(addr);
//}
}
}
}
}
Simulator::Schedule (Seconds(1.0), &PrintArpCache, nodenumber, node, nd);
}
void emtpyArpCache()
{
// Creates ARP Cache object
Ptr<ArpCache> arp = CreateObject<ArpCache> ();
uint32_t l = 0;
for (NodeList::Iterator i = NodeList::Begin(); i != NodeList::End(); ++i)
{
std::cout << "Node #" << l << '\n';
l ++;
arp = (*i)->GetObject<ArpCache> ();
arp->SetAliveTimeout (Seconds(3600 * 24 )); // 1-year
//arp->Flush();
}
}
void infoArpCache(uint32_t nodenumber, Ptr <Node> mynode, uint32_t myverbose)
{
// Create ARP Cache object
Ptr<ArpCache> arp = CreateObject<ArpCache> ();
Ptr<Ipv4L3Protocol> ip = mynode->GetObject<Ipv4L3Protocol> ();
if (ip!=0) {
std::cout << "[infoArpCache] Adding the Arp Cache to Node #" << nodenumber << '\n';
ObjectVectorValue interfaces;
ip->GetAttribute("InterfaceList", interfaces);
for(ObjectVectorValue::Iterator j = interfaces.Begin(); j !=interfaces.End (); j ++)
{
Ptr<Ipv4Interface> ipIface = (j->second)->GetObject<Ipv4Interface> ();
// Get interfaces list from Ipv4L3Protocol iteractor
Ptr<NetDevice> device = ipIface->GetDevice();
NS_ASSERT(device != 0);
arp->SetDevice (device, ipIface); // https://www.nsnam.org/doxygen/classns3_1_1_arp_cache.html#details
//ipIface->SetAttribute("ArpCache", PointerValue(arp));
AsciiTraceHelper asciiTraceHelper;
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("arpcache.txt");
arp->PrintArpCache(stream);
if (myverbose) {
Time mytime = arp->GetAliveTimeout();
std::cout << "Alive Timeout [s]: " << mytime.GetSeconds() << '\n';
mytime = arp->GetDeadTimeout();
std::cout << "Dead Timeout [s]: " << mytime.GetSeconds() << '\n';
}
}
}
Simulator::Schedule (Seconds(1.0), &infoArpCache, nodenumber, mynode, myverbose);
}
// Taken from here https://github.com/MOSAIC-UA/802.11ah-ns3/blob/master/ns-3/scratch/s1g-mac-test.cc
// Two typos corrected here https://groups.google.com/forum/#!topic/ns-3-users/JRE_BsNEJrY
// It seems this is not feasible: https://www.nsnam.org/bugzilla/show_bug.cgi?id=187
void PopulateArpCache (uint32_t nodenumber, Ptr <Node> mynode)
{
// Create ARP Cache object
Ptr<ArpCache> arp = CreateObject<ArpCache> ();
Ptr<Packet> dummy = Create<Packet> ();
// Set ARP Timeout
//arp->SetAliveTimeout (Seconds(3600 * 24 )); // 1-year
//arp->SetWaitReplyTimeout (Seconds(200));
// Populates ARP Cache with information from all nodes
/*for (NodeList::Iterator i = NodeList::Begin(); i != NodeList::End(); ++i)
{*/
std::cout << "[PopulateArpCache] Node #" << nodenumber << '\n';
// Get an interactor to Ipv4L3Protocol instance
Ptr<Ipv4L3Protocol> ip = mynode->GetObject<Ipv4L3Protocol> ();
NS_ASSERT(ip !=0);
// Get interfaces list from Ipv4L3Protocol iteractor
ObjectVectorValue interfaces;
ip->GetAttribute("InterfaceList", interfaces);
// For each interface
for(ObjectVectorValue::Iterator j = interfaces.Begin(); j !=interfaces.End (); j ++)
{
// Get an interactor to Ipv4L3Protocol instance
Ptr<Ipv4Interface> ipIface = (j->second)->GetObject<Ipv4Interface> ();
NS_ASSERT(ipIface != 0);
// Get interfaces list from Ipv4L3Protocol iteractor
Ptr<NetDevice> device = ipIface->GetDevice();
NS_ASSERT(device != 0);
// Get MacAddress assigned to this device
Mac48Address addr = Mac48Address::ConvertFrom(device->GetAddress ());
// For each Ipv4Address in the list of Ipv4Addresses assign to this interface...
for(uint32_t k = 0; k < ipIface->GetNAddresses (); k++)
{
// Get Ipv4Address
Ipv4Address ipAddr = ipIface->GetAddress (k).GetLocal();
// If Loopback address, go to the next
if(ipAddr == Ipv4Address::GetLoopback())
continue;
std::cout << "[PopulateArpCache] Arp Cache: Adding the pair (" << addr << "," << ipAddr << ")" << '\n';
// Creates an ARP entry for this Ipv4Address and adds it to the ARP Cache
Ipv4Header ipHeader;
ArpCache::Entry * entry = arp->Add(ipAddr);
//entry->IsPermanent();
//entry->MarkWaitReply();
//entry->MarkPermanent();
//entry->MarkAlive(addr);
//entry->MarkDead();
entry->MarkWaitReply (Ipv4PayloadHeaderPair(dummy,ipHeader));
entry->MarkAlive (addr);
entry->ClearPendingPacket();
entry->MarkPermanent ();
NS_LOG_UNCOND ("[PopulateArpCache] Arp Cache: Added the pair (" << addr << "," << ipAddr << ")");
AsciiTraceHelper asciiTraceHelper;
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("arpcache.txt");
arp->PrintArpCache(stream);
}
// }
// Assign ARP Cache to each interface of each node
for (NodeList::Iterator i = NodeList::Begin(); i != NodeList::End(); ++i)
{
Ptr<Ipv4L3Protocol> ip = (*i)->GetObject<Ipv4L3Protocol> ();
if (ip!=0) {
std::cout << "[PopulateArpCache] Adding the Arp Cache to Node #" << nodenumber << '\n';
ObjectVectorValue interfaces;
ip->GetAttribute("InterfaceList", interfaces);
for(ObjectVectorValue::Iterator j = interfaces.Begin(); j !=interfaces.End (); j ++)
{
Ptr<Ipv4Interface> ipIface = (j->second)->GetObject<Ipv4Interface> ();
// Get interfaces list from Ipv4L3Protocol iteractor
Ptr<NetDevice> device = ipIface->GetDevice();
NS_ASSERT(device != 0);
arp->SetDevice (device, ipIface); // https://www.nsnam.org/doxygen/classns3_1_1_arp_cache.html#details
//ipIface->SetAttribute("ArpCache", PointerValue(arp));
AsciiTraceHelper asciiTraceHelper;
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("arpcache.txt");
arp->PrintArpCache(stream);
Time mytime = arp->GetAliveTimeout();
std::cout << "Alive Timeout [s]: " << mytime.GetSeconds() << '\n';
}
}
}
}
}
/************* END of the ARP part (not used) *************/
// Modify the max AMPDU value of a node
void ModifyAmpdu (uint32_t nodeNumber, uint32_t ampduValue, uint32_t myverbose)
{
// These are the attributes of regular-wifi-mac: https://www.nsnam.org/doxygen/regular-wifi-mac_8cc_source.html
// You have to build a line like this (e.g. for node 0):
// Config::Set("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMac/BE_MaxAmpduSize", UintegerValue(ampduValue));
// There are 4 queues: VI, VO, BE and BK
// FIXME: Check if I only have to modify the parameters of all the devices (*), or only some of them.
// I use an auxiliar string for creating the first argument of Config::Set
std::ostringstream auxString;
// VI queue
auxString << "/NodeList/" << nodeNumber << "/DeviceList/*/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMac/VI_MaxAmpduSize";
// std::cout << auxString.str() << '\n';
Config::Set(auxString.str(), UintegerValue(ampduValue));
// clean the string
auxString.str(std::string());
// VO queue
auxString << "/NodeList/" << nodeNumber << "/DeviceList/*/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMac/VO_MaxAmpduSize";
// std::cout << auxString.str() << '\n';
Config::Set(auxString.str(), UintegerValue(ampduValue));
// clean the string
auxString.str(std::string());
// BE queue
auxString << "/NodeList/" << nodeNumber << "/DeviceList/*/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMac/BE_MaxAmpduSize";
// std::cout << auxString.str() << '\n';
Config::Set(auxString.str(), UintegerValue(ampduValue));
// clean the string
auxString.str(std::string());
// BK queue
auxString << "/NodeList/" << nodeNumber << "/DeviceList/*/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMac/BK_MaxAmpduSize";
//std::cout << auxString.str() << '\n';
Config::Set(auxString.str(), UintegerValue(ampduValue));
if ( myverbose > 1 )
std::cout << Simulator::Now()
<< "\t[ModifyAmpdu] Node #" << nodeNumber
<< " AMPDU max size changed to " << ampduValue << " bytes"
<< std::endl;
}
/*
// Not used
// taken from https://www.nsnam.org/doxygen/wifi-ap_8cc.html
// set the position of a node
static void
SetPosition (Ptr<Node> node, Vector position)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
mobility->SetPosition (position);
}*/
// Return a vector with the position of a node
// taken from https://www.nsnam.org/doxygen/wifi-ap_8cc.html
static Vector
GetPosition (Ptr<Node> node)
{
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
return mobility->GetPosition ();
}
// obtain the nearest AP of a STA
static Ptr<Node>
nearestAp (NodeContainer APs, Ptr<Node> mySTA, int myverbose)
{
// calculate an initial value for the minimum distance (a very high value)
double mimimumDistance = APs.GetN() * 100000;
// variable for storing the nearest AP
Ptr<Node> nearest;
// vector with the position of the STA
Vector posSta = GetPosition (mySTA);
// vector with the position of the AP
Vector posAp;
if (myverbose > 3)
std::cout << (Simulator::Now()) << "\t[nearestAp]\tSTA #" << mySTA->GetId() << "\tPosition: " << posSta.x << "," << posSta.y << std::endl;
// Check all the APs to find the nearest one
NodeContainer::Iterator i;
for (i = APs.Begin (); i != APs.End (); ++i)
{
//(*i)->method (); // some Node method
posAp = GetPosition((*i));
uint32_t distance = sqrt ( ( (posSta.x - posAp.x)*(posSta.x - posAp.x) ) + ( (posSta.y - posAp.y)*(posSta.y - posAp.y) ) );
if (distance < mimimumDistance ) {
mimimumDistance = distance;
nearest = *i;
}
}
if (myverbose > 3)
std::cout << Simulator::Now()
<< "\t[nearestAp]\t\tNearest AP is AP#" << nearest->GetId()
<< ". Position: " << GetPosition((nearest)).x
<< "," << GetPosition((nearest)).y
<< std::endl;
return nearest;
}
// Print the position of a node
// taken from https://www.nsnam.org/doxygen/wifi-ap_8cc.html
static void
ReportPosition (Ptr<Node> node, int i, int type, int myverbose, NodeContainer myApNodes)
// type = 0 means it will write 'AP#'
// type = 1 means it will write 'STA#'
{
Vector pos = GetPosition (node);
if (myverbose > 2)
{
if (type == 0) {
std::cout << Simulator::Now()
<< "\t[ReportPosition] AP #" << i
<< " Position: " << pos.x
<< "," << pos.y
<< std::endl;
} else {
// Find the nearest AP
Ptr<Node> nearest;
nearest = nearestAp (myApNodes, node, myverbose);
std::cout << Simulator::Now()
<< "\t[ReportPosition] STA #" << i
<< " Position: " << pos.x
<< "," << pos.y
<< ". The nearest AP is AP#" << (nearest)->GetId()
<< std::endl;
}
}
// re-schedule in 1 second
Simulator::Schedule (Seconds (1.0), &ReportPosition, node, i, type, myverbose, myApNodes);
}
// Print the simulation time to std::cout
static void printTime (uint32_t period, std::string myoutputFileName, std::string myoutputFileSurname)
{
std::cout << Simulator::Now() << "\t" << myoutputFileName << "_" << myoutputFileSurname << '\n';
// re-schedule
Simulator::Schedule (Seconds (period), &printTime, period, myoutputFileName, myoutputFileSurname);
}
// function for tracking mobility changes
static void
CourseChange (std::string foo, Ptr<const MobilityModel> mobility)
{
Vector pos = mobility->GetPosition ();
Vector vel = mobility->GetVelocity ();
std::cout << Simulator::Now () << "\t[CourseChange] MOBILITY CHANGE. model= "
<< mobility << ", POS: x=" << pos.x
<< ", y=" << pos.y
<< ", z=" << pos.z
<< "; VEL:" << vel.x
<< ", y=" << vel.y
<< ", z=" << vel.z << std::endl;
}
// Print the statistics to an output file and/or to the screen
void
print_stats ( FlowMonitor::FlowStats st,
double simulationTime,
uint32_t mygenerateHistograms,
std::string fileName,
std::string fileSurname,
uint32_t myverbose,
std::string flowID,
uint32_t printColumnTitles )
{
// print the results to a file (they are written at the end of the file)
if ( fileName != "" ) {
std::ofstream ofs;
ofs.open ( fileName + "_flows.txt", std::ofstream::out | std::ofstream::app); // with "trunc" Any contents that existed in the file before it is open are discarded. with "app", all output operations happen at the end of the file, appending to its existing contents
// Print a line in the output file, with the title of each column
if ( printColumnTitles == 1 ) {
ofs << "Flow_ID" << "\t"
<< "Protocol" << "\t"
<< "source_Address" << "\t"
<< "source_Port" << "\t"
<< "destination_Address" << "\t"
<< "destination_Port" << "\t"
<< "Num_Tx_Packets" << "\t"
<< "Num_Tx_Bytes" << "\t"
<< "Tx_Throughput_[bps]" << "\t"
<< "Num_Rx_Packets" << "\t"
<< "Num_RX_Bytes" << "\t"
<< "Num_lost_packets" << "\t"
<< "Rx_Throughput_[bps]" << "\t"
<< "Average_Latency_[s]" << "\t"
<< "Average_Jitter_[s]" << "\t"
<< "Average_Number_of_hops" << "\t"
<< "Simulation_time_[s]" << "\n";
}
// Print a line in the output file, with the data of this flow
ofs << flowID << "\t" // flowID includes the protocol, IP addresses and ports
<< st.txPackets << "\t"
<< st.txBytes << "\t"
<< st.txBytes * 8.0 / simulationTime << "\t"
<< st.rxPackets << "\t"
<< st.rxBytes << "\t"
<< st.txPackets - st.rxPackets << "\t"
<< st.rxBytes * 8.0 / simulationTime << "\t";
if (st.rxPackets > 0)
{
ofs << (st.delaySum.GetSeconds() / st.rxPackets) << "\t";
if (st.rxPackets > 1) { // I need at least two packets for calculating the jitter
ofs << (st.jitterSum.GetSeconds() / (st.rxPackets - 1.0)) << "\t";
} else {
ofs << "\t";
}
ofs << st.timesForwarded / st.rxPackets + 1 << "\t";
} else { //no packets arrived
ofs << "\t" << "\t" << "\t";
}
ofs << simulationTime << "\n";
ofs.close();
// save the histogram to a file
if ( mygenerateHistograms > 0 )
{
std::ofstream ofs_histo;
ofs_histo.open ( fileName + fileSurname + "_delay_histogram.txt", std::ofstream::out | std::ofstream::trunc);
ofs_histo << "Flow #" << flowID << "\n";
ofs_histo << "number\tinit_interval\tend_interval\tnumber_of_samples" << std::endl;
for (uint32_t i=0; i < st.delayHistogram.GetNBins (); i++)
ofs_histo << i << "\t" << st.delayHistogram.GetBinStart (i) << "\t" << st.delayHistogram.GetBinEnd (i) << "\t" << st.delayHistogram.GetBinCount (i) << std::endl;
ofs_histo.close();
ofs_histo.open ( fileName + fileSurname + "_jitter_histogram.txt", std::ofstream::out | std::ofstream::trunc); // with "trunc", Any contents that existed in the file before it is open are discarded
ofs_histo << "Flow #" << flowID << "\n";
ofs_histo << "number\tinit_interval\tend_interval\tnumber_of_samples" << std::endl;
for (uint32_t i=0; i < st.jitterHistogram.GetNBins (); i++ )
ofs_histo << i << "\t" << st.jitterHistogram.GetBinStart (i) << "\t" << st.jitterHistogram.GetBinEnd (i) << "\t" << st.jitterHistogram.GetBinCount (i) << std::endl;
ofs_histo.close();
ofs_histo.open ( fileName + fileSurname + "_packetsize_histogram.txt", std::ofstream::out | std::ofstream::trunc); // with "trunc", Any contents that existed in the file before it is open are discarded
ofs_histo << "Flow #" << flowID << "\n";
ofs_histo << "number\tinit_interval\tend_interval\tnumber_of_samples"<< std::endl;
for (uint32_t i=0; i < st.packetSizeHistogram.GetNBins (); i++ )
ofs_histo << i << "\t" << st.packetSizeHistogram.GetBinStart (i) << "\t" << st.packetSizeHistogram.GetBinEnd (i) << "\t" << st.packetSizeHistogram.GetBinCount (i) << std::endl;
ofs_histo.close();
}
}
// print the results by the screen
if ( myverbose > 0 ) {
std::cout << " -Flow #" << flowID << "\n";
if ( mygenerateHistograms > 0)
std::cout << " The name of the output files starts with: " << fileName << fileSurname << "\n";
std::cout << " Tx Packets: " << st.txPackets << "\n";
std::cout << " Tx Bytes: " << st.txBytes << "\n";
std::cout << " TxOffered: " << st.txBytes * 8.0 / simulationTime / 1000 / 1000 << " Mbps\n";
std::cout << " Rx Packets: " << st.rxPackets << "\n";
std::cout << " Rx Bytes: " << st.rxBytes << "\n";
std::cout << " Lost Packets: " << st.txPackets - st.rxPackets << "\n";
std::cout << " Throughput: " << st.rxBytes * 8.0 / simulationTime / 1000 / 1000 << " Mbps\n";
if (st.rxPackets > 0) // some packets have arrived
{
std::cout << " Mean{Delay}: " << (st.delaySum.GetSeconds() / st.rxPackets);
if (st.rxPackets > 1) // I need at least two packets for calculating the jitter
{
std::cout << " Mean{Jitter}: " << (st.jitterSum.GetSeconds() / (st.rxPackets - 1.0 ));
} else {
std::cout << " Mean{Jitter}: only one packet arrived. ";
}
std::cout << " Mean{Hop Count}: " << st.timesForwarded / st.rxPackets + 1 << "\n";
} else { //no packets arrived
std::cout << " Mean{Delay}: no packets arrived. ";
std::cout << " Mean{Jitter}: no packets arrived. ";
std::cout << " Mean{Hop Count}: no packets arrived. \n";
}
if (( mygenerateHistograms > 0 ) && ( myverbose > 3 ))
{
std::cout << " Delay Histogram" << std::endl;
for (uint32_t i=0; i < st.delayHistogram.GetNBins (); i++)
std::cout << " " << i << "(" << st.delayHistogram.GetBinStart (i)
<< "-" << st.delayHistogram.GetBinEnd (i)
<< "): " << st.delayHistogram.GetBinCount (i)
<< std::endl;
std::cout << " Jitter Histogram" << std::endl;
for (uint32_t i=0; i < st.jitterHistogram.GetNBins (); i++ )
std::cout << " " << i << "(" << st.jitterHistogram.GetBinStart (i)
<< "-" << st.jitterHistogram.GetBinEnd (i)
<< "): " << st.jitterHistogram.GetBinCount (i)
<< std::endl;
std::cout << " PacketSize Histogram "<< std::endl;
for (uint32_t i=0; i < st.packetSizeHistogram.GetNBins (); i++ )
std::cout << " " << i << "(" << st.packetSizeHistogram.GetBinStart (i)
<< "-" << st.packetSizeHistogram.GetBinEnd (i)
<< "): " << st.packetSizeHistogram.GetBinCount (i)
<< std::endl;
}
for (uint32_t i=0; i < st.packetsDropped.size (); i++)
std::cout << " Packets dropped by reason " << i << ": " << st.packetsDropped [i] << std::endl;
// for (uint32_t i=0; i<st.bytesDropped.size(); i++)
// std::cout << "Bytes dropped by reason " << i << ": " << st.bytesDropped[i] << std::endl;
std::cout << "\n";
}
}
// this class stores a number of records: each one contains a pair AP node id - AP MAC address
// the node id is the one given by ns3 when creating the node
class AP_record
{
public:
AP_record ();
//void SetApRecord (uint16_t thisId, Mac48Address thisMac);
void SetApRecord (uint16_t thisId, std::string thisMac, uint32_t thisMaxSizeAmpdu);
//uint16_t GetApid (Mac48Address thisMac);
uint16_t GetApid ();
//Mac48Address GetMac (uint16_t thisId);
std::string GetMac ();
uint32_t GetMaxSizeAmpdu ();
uint8_t GetWirelessChannel();
void setWirelessChannel(uint8_t thisWirelessChannel);
private:
uint16_t apId;
//Mac48Address apMac;
std::string apMac;
uint32_t apMaxSizeAmpdu;
uint8_t apWirelessChannel;
};
typedef std::vector <AP_record * > AP_recordVector;
AP_recordVector AP_vector;
AP_record::AP_record ()
{
apId = 0;
apMac = "02-06-00:00:00:00:00:00";
apMaxSizeAmpdu = 0;
}
void
//AP_record::SetApRecord (uint16_t thisId, Mac48Address thisMac)
AP_record::SetApRecord (uint16_t thisId, std::string thisMac, uint32_t thisMaxSizeAmpdu)
{
apId = thisId;
apMac = thisMac;
apMaxSizeAmpdu = thisMaxSizeAmpdu;
}
uint16_t
//AP_record::GetApid (Mac48Address thisMac)
AP_record::GetApid ()
{
return apId;
}
//Mac48Address
std::string
AP_record::GetMac ()
{
return apMac;
}
uint8_t
AP_record::GetWirelessChannel()
{
return apWirelessChannel;
}
void
AP_record::setWirelessChannel(uint8_t thisWirelessChannel)
{
apWirelessChannel = thisWirelessChannel;
}
//MaxSizeAmpdu
uint32_t
AP_record::GetMaxSizeAmpdu ()
{
return apMaxSizeAmpdu;
}
void
Modify_AP_Record (uint16_t thisId, std::string thisMac, uint32_t thisMaxSizeAmpdu) // FIXME: Can this be done just with Set_AP_Record?
{
for (AP_recordVector::const_iterator index = AP_vector.begin (); index != AP_vector.end (); index++) {
//std::cout << Simulator::Now () << " ********************** AP with ID " << (*index)->GetApid() << " has MAC: " << (*index)->GetMac() << " *****" << std::endl;
if ( (*index)->GetMac () == thisMac ) {
(*index)->SetApRecord (thisId, thisMac, thisMaxSizeAmpdu);
//std::cout << Simulator::Now () << "\t[GetAnAP_Id] AP #" << (*index)->GetApid() << " has MAC: " << (*index)->GetMac() << "" << std::endl;
}
}
}
uint16_t
GetAnAP_Id (std::string thisMac)
// lists all the STAs associated to an AP, with the MAC of the AP
{
uint16_t APid = 0;
//std::cout << Simulator::Now () << " *** Number of STA associated: " << Get_STA_record_num() << " *****" << std::endl;
for (AP_recordVector::const_iterator index = AP_vector.begin (); index != AP_vector.end (); index++) {
//std::cout << Simulator::Now () << " ********************** AP with ID " << (*index)->GetApid() << " has MAC: " << (*index)->GetMac() << " *****" << std::endl;
if ( (*index)->GetMac () == thisMac ) {
APid = (*index)->GetApid ();
//std::cout << Simulator::Now () << "\t[GetAnAP_Id] AP #" << (*index)->GetApid() << " has MAC: " << (*index)->GetMac() << "" << std::endl;
}
}
return APid;
}
uint32_t
GetAP_MaxSizeAmpdu (uint16_t thisAPid, uint32_t myverbose)
// returns the max size of the Ampdu of an AP
{
uint32_t APMaxSizeAmpdu = 0;
//std::cout << Simulator::Now () << " *** Number of STA associated: " << Get_STA_record_num() << " *****" << std::endl;
for (AP_recordVector::const_iterator index = AP_vector.begin (); index != AP_vector.end (); index++) {
if ( (*index)->GetApid () == thisAPid ) {
APMaxSizeAmpdu = (*index)->GetMaxSizeAmpdu ();
if ( myverbose > 2 )
std::cout << Simulator::Now ()
<< "\t[GetAP_MaxSizeAmpdu] AP #" << (*index)->GetApid()
<< " has AMDPU: " << (*index)->GetMaxSizeAmpdu()
<< "" << std::endl;
}
}
return APMaxSizeAmpdu;
}
uint8_t
GetAP_WirelessChannel (uint16_t thisAPid, uint32_t myverbose)
// returns the wireless channel of an AP
{
uint8_t APWirelessChannel = 0;
//std::cout << Simulator::Now () << " *** Number of STA associated: " << Get_STA_record_num() << " *****" << std::endl;
for (AP_recordVector::const_iterator index = AP_vector.begin (); index != AP_vector.end (); index++) {
if ( (*index)->GetApid () == thisAPid ) {
APWirelessChannel = (*index)->GetWirelessChannel();
if ( myverbose > 2 )
std::cout << Simulator::Now ()
<< "\t[GetAP_WirelessChannel] AP #" << (*index)->GetApid()
<< " has channel: " << uint16_t((*index)->GetWirelessChannel())
<< "" << std::endl;
}