forked from quic-interop/quic-network-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.cc
98 lines (76 loc) · 3.46 KB
/
sim.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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include "ns3/abort.h"
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/fd-net-device-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/csma-module.h"
#include "ns3/point-to-point-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("ns3 simulator");
void installNetDevice(Ptr<Node> node, std::string deviceName, Mac48AddressValue macAddress, Ipv4InterfaceAddress ipv4Address) {
EmuFdNetDeviceHelper emu;
emu.SetDeviceName(deviceName);
NetDeviceContainer devices = emu.Install(node);
Ptr<NetDevice> device = devices.Get(0);
device->SetAttribute("Address", macAddress);
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
uint32_t interface = ipv4->AddInterface(device);
ipv4->AddAddress(interface, ipv4Address);
ipv4->SetMetric(interface, 1);
ipv4->SetUp(interface);
}
int main(int argc, char *argv[]) {
GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
NodeContainer nodesLeft;
nodesLeft.Create(1);
InternetStackHelper internetLeft;
internetLeft.Install(nodesLeft);
NodeContainer nodesRight;
nodesRight.Create(1);
InternetStackHelper internetRight;
internetRight.Install(nodesRight);
// Stick in the point-to-point line between the sides.
PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
p2p.SetChannelAttribute("Delay", StringValue("10ms"));
NodeContainer nodes = NodeContainer(nodesLeft.Get(0), nodesRight.Get(0));
NetDeviceContainer devices = p2p.Install(nodes);
Ipv4AddressHelper ipv4;
ipv4.SetBase("10.50.0.0", "255.255.0.0");
Ipv4InterfaceContainer interfaces = ipv4.Assign(devices);
NS_LOG_INFO("Create eth0");
installNetDevice(nodesLeft.Get(0), "eth0", Mac48AddressValue("02:51:55:49:43:00"), Ipv4InterfaceAddress("10.0.0.2", "255.255.0.0"));
NS_LOG_INFO("Create eth1");
installNetDevice(nodesRight.Get(0), "eth1", Mac48AddressValue("02:51:55:49:43:01"), Ipv4InterfaceAddress("10.100.0.2", "255.255.0.0"));
// Simulate some CBR traffic over the point-to-point link
uint16_t port = 9; // Discard port (RFC 863)
OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (interfaces.GetAddress (1), port));
onoff.SetConstantRate (DataRate ("5000kb/s"));
ApplicationContainer apps = onoff.Install (nodesLeft.Get (0));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (60.0));
apps = onoff.Install (nodesLeft.Get (0));
apps.Start (Seconds (90.0));
// Create a packet sink to receive these packets
PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
apps = sink.Install (nodesRight.Get (0));
apps.Start (Seconds (1.0));
// enable pcaps for all nodes
// csmaLeft.EnablePcapAll("tap-wifi-dumbbell", false);
// csmaRight.EnablePcapAll("tap-wifi-dumbbell", false);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
// write the routing table to file
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>("dynamic-global-routing.routes", std::ios::out);
Ipv4RoutingHelper::PrintRoutingTableAllAt(Seconds(0.), routingStream);
NS_LOG_INFO("Run Emulation.");
Simulator::Stop(Seconds(36000.0));
Simulator::Run();
Simulator::Destroy();
NS_LOG_INFO("Done.");
}