-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl2.cc
142 lines (110 loc) · 5.23 KB
/
l2.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
#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/traffic-control-module.h"
#include "ns3/flow-monitor-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("TrafficControlExample");
int
main (int argc, char *argv[])
{
double simulationTime = 10; //seconds
std::string transportProt = "Udp";
std::string socketType;
CommandLine cmd;
cmd.Parse (argc, argv);
if (transportProt.compare ("Tcp") == 0)
{
socketType = "ns3::TcpSocketFactory";
}
else
{
socketType = "ns3::UdpSocketFactory";
}
NodeContainer nodes;
nodes.Create (4);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
pointToPoint.SetQueue ("ns3::DropTailQueue", "Mode", StringValue ("QUEUE_MODE_PACKETS"), "MaxPackets", UintegerValue (1));
NetDeviceContainer devices1 = pointToPoint.Install (nodes.Get(0),nodes.Get(2));
NetDeviceContainer devices2 = pointToPoint.Install (nodes.Get(1),nodes.Get(2));
NetDeviceContainer devices3 = pointToPoint.Install (nodes.Get(2),nodes.Get(3));
InternetStackHelper stack;
stack.Install (nodes);
TrafficControlHelper tch;
uint16_t handle = tch.SetRootQueueDisc ("ns3::RedQueueDisc");
// Add the internal queue used by Red
tch.AddInternalQueues (handle, 1, "ns3::DropTailQueue", "MaxPackets", UintegerValue (10000));
QueueDiscContainer qdiscs1 = tch.Install (devices1);
QueueDiscContainer qdiscs2 = tch.Install (devices2);
QueueDiscContainer qdiscs3 = tch.Install (devices3);
Ipv4AddressHelper address1;
address1.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces1 = address1.Assign (devices1);
Ipv4AddressHelper address2;
address2.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces2 = address2.Assign (devices2);
Ipv4AddressHelper address3;
address3.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces3 = address3.Assign (devices3);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
//Udp Flow
uint16_t port = 7;
Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
PacketSinkHelper packetSinkHelper (socketType, localAddress);
ApplicationContainer sinkApp = packetSinkHelper.Install (nodes.Get (3));
sinkApp.Start (Seconds (0.0));
sinkApp.Stop (Seconds (simulationTime + 0.1));
uint32_t payloadSize = 1448;
Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
OnOffHelper onoff (socketType, Ipv4Address::GetAny ());
onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
onoff.SetAttribute ("DataRate", StringValue ("50Mbps")); //bit/s
ApplicationContainer apps;
AddressValue remoteAddress (InetSocketAddress (interfaces3.GetAddress (1), port));
onoff.SetAttribute ("Remote", remoteAddress);
apps.Add (onoff.Install (nodes.Get (0)));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (simulationTime + 0.1));
//Tcp Flow
uint16_t port_tcp = 9;
socketType = "ns3::TcpSocketFactory";
Address localAddress_tcp (InetSocketAddress (Ipv4Address::GetAny (), port_tcp));
PacketSinkHelper packetSinkHelper_tcp (socketType, localAddress_tcp);
ApplicationContainer sinkApp_tcp = packetSinkHelper_tcp.Install (nodes.Get (3));
sinkApp_tcp.Start (Seconds (1.0));
sinkApp_tcp.Stop (Seconds (simulationTime + 0.1));
Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
OnOffHelper onoff_tcp (socketType, Ipv4Address::GetAny ());
onoff_tcp.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onoff_tcp.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onoff_tcp.SetAttribute ("PacketSize", UintegerValue (payloadSize));
onoff_tcp.SetAttribute ("DataRate", StringValue ("50Mbps")); //bit/s
ApplicationContainer apps_tcp;
AddressValue remoteAddress_tcp (InetSocketAddress (interfaces3.GetAddress (1), port_tcp));
onoff_tcp.SetAttribute ("Remote", remoteAddress_tcp);
apps_tcp.Add (onoff_tcp.Install (nodes.Get (1)));
apps_tcp.Start (Seconds (2.0));
apps_tcp.Stop (Seconds (simulationTime + 0.1));
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
Simulator::Stop (Seconds (simulationTime + 5));
Simulator::Run ();
// Print per flow statistics
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator iter = stats.begin (); iter != stats.end (); ++iter)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (iter->first);
NS_LOG_UNCOND("Flow ID: " << iter->first << " Src Addr " << t.sourceAddress << " Dst Addr " << t.destinationAddress);
NS_LOG_UNCOND("Tx Packets = " << iter->second.txPackets);
}
Simulator::Destroy ();
return 0;
}