-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmySim.cc
More file actions
146 lines (118 loc) · 4.82 KB
/
Copy pathmySim.cc
File metadata and controls
146 lines (118 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-module.h"
using namespace ns3;
int main (int argc, char *argv[])
{
std::string bottleneckBandwidth = "10Mbps";
std::string bottleneckDelay = "10ms";
uint32_t queueSize = 100;
std::string accessDelay1 = "5ms"; // Flow 1 (short RTT)
std::string accessDelay2 = "5ms"; // Flow 2 (long RTT)
CommandLine cmd;
cmd.AddValue("bandwidth", "Bottleneck bandwidth", bottleneckBandwidth);
cmd.AddValue("delay", "Bottleneck delay", bottleneckDelay);
cmd.AddValue("queueSize", "Queue size (packets)", queueSize);
cmd.AddValue("accessDelay1", "Access delay flow 1", accessDelay1);
cmd.AddValue("accessDelay2", "Access delay flow 2", accessDelay2);
cmd.Parse(argc, argv);
std::cout << "Simulation starting..." << std::endl;
std::cout << "Flow1 Access Delay: " << accessDelay1 << std::endl;
std::cout << "Flow2 Access Delay: " << accessDelay2 << std::endl;
NodeContainer senders, receivers, routers;
senders.Create(2);
receivers.Create(2);
routers.Create(2);
InternetStackHelper stack;
stack.InstallAll();
// Flow 1 Access Link
PointToPointHelper access1;
access1.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
access1.SetChannelAttribute("Delay", StringValue(accessDelay1));
// Flow 2 Access Link
PointToPointHelper access2;
access2.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
access2.SetChannelAttribute("Delay", StringValue(accessDelay2));
// Bottleneck
PointToPointHelper bottleneck;
bottleneck.SetDeviceAttribute("DataRate", StringValue(bottleneckBandwidth));
bottleneck.SetChannelAttribute("Delay", StringValue(bottleneckDelay));
bottleneck.SetQueue("ns3::DropTailQueue",
"MaxSize", StringValue(std::to_string(queueSize) + "p"));
NetDeviceContainer d1 = access1.Install(senders.Get(0), routers.Get(0));
NetDeviceContainer d2 = access2.Install(senders.Get(1), routers.Get(0));
NetDeviceContainer d3 = bottleneck.Install(routers.Get(0), routers.Get(1));
NetDeviceContainer d4 = access1.Install(routers.Get(1), receivers.Get(0));
NetDeviceContainer d5 = access2.Install(routers.Get(1), receivers.Get(1));
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
address.Assign(d1);
address.SetBase("10.1.2.0", "255.255.255.0");
address.Assign(d2);
address.SetBase("10.1.3.0", "255.255.255.0");
address.Assign(d3);
address.SetBase("10.1.4.0", "255.255.255.0");
address.Assign(d4);
address.SetBase("10.1.5.0", "255.255.255.0");
address.Assign(d5);
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
uint16_t port = 50000;
BulkSendHelper source1("ns3::TcpSocketFactory",
InetSocketAddress(Ipv4Address("10.1.4.2"), port));
source1.SetAttribute("MaxBytes", UintegerValue(0));
ApplicationContainer app1 = source1.Install(senders.Get(0));
app1.Start(Seconds(1.0));
app1.Stop(Seconds(20.0));
BulkSendHelper source2("ns3::TcpSocketFactory",
InetSocketAddress(Ipv4Address("10.1.5.2"), port));
source2.SetAttribute("MaxBytes", UintegerValue(0));
ApplicationContainer app2 = source2.Install(senders.Get(1));
app2.Start(Seconds(1.0));
app2.Stop(Seconds(20.0));
PacketSinkHelper sink("ns3::TcpSocketFactory",
InetSocketAddress(Ipv4Address::GetAny(), port));
ApplicationContainer sinkApp1 = sink.Install(receivers.Get(0));
sinkApp1.Start(Seconds(0.0));
ApplicationContainer sinkApp2 = sink.Install(receivers.Get(1));
sinkApp2.Start(Seconds(0.0));
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
Simulator::Stop(Seconds(20));
Simulator::Run();
monitor->CheckForLostPackets();
Ptr<Ipv4FlowClassifier> classifier =
DynamicCast<Ipv4FlowClassifier>(flowmon.GetClassifier());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats();
double totalThroughput = 0;
double sumSquares = 0;
int flowCount = 0;
for (auto const &flow : stats)
{
Ipv4FlowClassifier::FiveTuple t =
classifier->FindFlow(flow.first);
if (t.destinationPort == port)
{
double throughput =
flow.second.rxBytes * 8.0 /
(flow.second.timeLastRxPacket.GetSeconds() -
flow.second.timeFirstTxPacket.GetSeconds()) /
1000000;
std::cout << "Flow " << flow.first
<< " Throughput: " << throughput
<< " Mbps" << std::endl;
totalThroughput += throughput;
sumSquares += throughput * throughput;
flowCount++;
}
}
double fairness =
(totalThroughput * totalThroughput) /
(flowCount * sumSquares);
std::cout << "Jain Fairness Index: "
<< fairness << std::endl;
Simulator::Destroy();
return 0;
}