tc-homework2/scratch/task_2001600.cc

398 lines
16 KiB
C++
Raw Normal View History

2023-12-27 12:52:48 +01:00
#include <iostream>
#include <string>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/mobility-module.h"
#include "ns3/network-module.h"
#include "ns3/ssid.h"
#include "ns3/yans-wifi-helper.h"
2023-12-27 12:52:48 +01:00
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("Task_2001600");
NetDeviceContainer createCSMA(NodeContainer nodes, StringValue DataRate, StringValue Delay);
2023-12-27 12:52:48 +01:00
int main(int argc, char* argv[])
{
// Hello
NS_LOG_UNCOND("Hello Task_2001600");
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
2023-12-27 12:52:48 +01:00
/* ### Creazione della Rete ### */
/* ######################################################## */
2023-12-27 12:52:48 +01:00
/* PARAMETRI */
/*
Parametro obbligatorio di nome studentId che rappresenta la stringa della matricola referente. Se
questo parametro non viene passato, la simulazione non deve partire
*/
std::string studentId;
/*
Parametro opzionale di nome enableRtsCts che rappresenta un valore booleano con valore di
default uguale a Falso. Quando il suo valore è Vero, deve forzare luso del meccanismo RTS/CTS
*/
bool enableRtsCts = false;
/*
Parametro opzionale di nome tracing che rappresenta un valore booleano con valore di default
uguale a Falso. Quando il suo valore è Vero, deve abilitare il tracing in modalità promiscua sugli
switch di rete e sul router Wi-Fi.
*/
bool tracing = false;
// Parse command line arguments
// To be passed `./ns3 run task_2001600 -- --studentId=2001600 --tracing=true --enableRtsCts=true`
2023-12-27 12:52:48 +01:00
CommandLine cmd;
cmd.AddValue("studentId", "studentId", studentId);
cmd.AddValue("enableRtsCts", "Enable RTS/CTS", enableRtsCts);
cmd.AddValue("tracing", "Enable tracing", tracing);
cmd.Parse(argc, argv);
// Se studentId vuoto, la simulazione non deve partire
if (studentId.empty())
2023-12-27 12:52:48 +01:00
{
std::cout << "studentId is empty, simulation will not start. exiting." << std::endl;
return 1;
}
2023-12-30 10:16:26 +01:00
// if rts disabled, cts threshold to more than max ever sent
Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", (enableRtsCts ? UintegerValue(100) : UintegerValue(2200)));
2023-12-27 12:52:48 +01:00
// All the nodes, each subnet uses a subset of these.
// A node can belong to multiple subnets
NodeContainer allNodes;
allNodes.Create(16);
2023-12-27 12:52:48 +01:00
2023-12-30 10:16:26 +01:00
/* ######################################################## */
/* ----- Start of Subnets setup ----- */
// Reti PTP
2023-12-27 12:52:48 +01:00
/* SOTTORETE PTP Node 0,2 */
NodeContainer nodes02;
nodes02.Add(allNodes.Get(0));
nodes02.Add(allNodes.Get(2));
PointToPointHelper ptph02;
2023-12-30 10:16:26 +01:00
ptph02.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
ptph02.SetChannelAttribute("Delay", StringValue("200ms"));
NetDeviceContainer ptp02 = ptph02.Install(nodes02);
2023-12-27 12:52:48 +01:00
/* SOTTORETE PTP Node 1,2 */
NodeContainer nodes12;
nodes12.Add(allNodes.Get(1));
nodes12.Add(allNodes.Get(2));
PointToPointHelper ptph12;
ptph12.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
ptph12.SetChannelAttribute("Delay", StringValue("200ms"));
NetDeviceContainer ptp12 = ptph12.Install(nodes12);
2023-12-27 12:52:48 +01:00
/* SOTTORETE PTP Node 2,4 */
NodeContainer nodes24;
nodes24.Add(allNodes.Get(2));
nodes24.Add(allNodes.Get(4));
PointToPointHelper ptph24;
ptph24.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph24.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp24 = ptph24.Install(nodes24);
2023-12-27 12:52:48 +01:00
/* SOTTORETE PTP Node 3,5 */
NodeContainer nodes35;
nodes35.Add(allNodes.Get(3));
nodes35.Add(allNodes.Get(5));
PointToPointHelper ptph35;
ptph35.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph35.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp35 = ptph35.Install(nodes35);
2023-12-27 12:52:48 +01:00
/* SOTTORETE PTP Node 4,5 */
NodeContainer nodes45;
nodes45.Add(allNodes.Get(4));
nodes45.Add(allNodes.Get(5));
PointToPointHelper ptph45;
ptph45.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph45.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp45 = ptph45.Install(nodes45);
2023-12-27 12:52:48 +01:00
/* SOTTORETE PTP Node 3,9 */
NodeContainer nodes39;
nodes39.Add(allNodes.Get(3));
nodes39.Add(allNodes.Get(9));
PointToPointHelper ptph39;
ptph39.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph39.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp39 = ptph39.Install(nodes39);
2023-12-27 12:52:48 +01:00
/* SOTTORETE PTP Node 4,9 */
NodeContainer nodes49;
nodes49.Add(allNodes.Get(4));
nodes49.Add(allNodes.Get(9));
PointToPointHelper ptph49;
ptph49.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph49.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp49 = ptph49.Install(nodes49);
// Reti CSMA
/* SOTTORETE CSMA Nodi 3,4 */
NodeContainer nodes34;
nodes34.Add(allNodes.Get(3));
nodes34.Add(allNodes.Get(4));
CsmaHelper csmah34;
csmah34.SetChannelAttribute("DataRate", StringValue("10Mbps"));
csmah34.SetChannelAttribute("Delay", TimeValue(MicroSeconds(200)));
NetDeviceContainer csma34 = csmah34.Install(nodes34);
/* SOTTORETE CSMA Nodi 5,6,7,8 */
NodeContainer nodes5678;
nodes5678.Add(allNodes.Get(5));
nodes5678.Add(allNodes.Get(6));
nodes5678.Add(allNodes.Get(7));
nodes5678.Add(allNodes.Get(8));
CsmaHelper csmah5678;
csmah5678.SetChannelAttribute("DataRate", StringValue("5Mbps"));
csmah5678.SetChannelAttribute("Delay", TimeValue(MicroSeconds(20)));
NetDeviceContainer csma5678 = csmah5678.Install(nodes5678);
// Rete Wi-Fi AARF 802.11g
/*
Wi-Fi operating in Infrastructure mode, AP is stationary, Stations nodes move with random walk mobility pattern over a bounded square of 30 meters for each side
*/
// Channel
// AP
NodeContainer wifiApNode;
wifiApNode.Add(allNodes.Get(9));
2023-12-30 10:16:26 +01:00
// Station devices (e.g. laptops)
NodeContainer wifiStaNodes;
wifiStaNodes.Add(allNodes.Get(10));
wifiStaNodes.Add(allNodes.Get(11));
wifiStaNodes.Add(allNodes.Get(12));
wifiStaNodes.Add(allNodes.Get(13));
wifiStaNodes.Add(allNodes.Get(14));
wifiStaNodes.Add(allNodes.Get(15));
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy;
phy.SetChannel(channel.Create());
WifiHelper wifi;
wifi.SetStandard (WIFI_STANDARD_80211g);
wifi.SetRemoteStationManager("ns3::AarfWifiManager");
// you can do in this way if you want to set a constant data rate
//wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode", StringValue("OfdmRate6Mbps"));
WifiMacHelper mac;
// Infrastructure Mode
Ssid ssid = Ssid("2001600");
2023-12-30 10:16:26 +01:00
// Stations
NetDeviceContainer staDevices;
mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
staDevices = wifi.Install(phy, mac, wifiStaNodes);
2023-12-30 10:16:26 +01:00
// AP
2023-12-30 12:56:02 +01:00
NetDeviceContainer apDevice;
mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
2023-12-30 12:56:02 +01:00
apDevice = wifi.Install(phy, mac, wifiApNode);
// Mobility helper
MobilityHelper mobility;
// Actually needed?
/*mobility.SetPositionAllocator("ns3::GridPositionAllocator",
"MinX",
DoubleValue(0.0),
"MinY",
DoubleValue(0.0),
"DeltaX",
DoubleValue(5.0),
"DeltaY",
DoubleValue(10.0),
"GridWidth",
UintegerValue(3),
"LayoutType",
2023-12-30 10:16:26 +01:00
StringValue("RowFirst"));*/
// Stations move with random walk over a square of 30 meters each side
mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
"Bounds",
RectangleValue(Rectangle(-15, 15, -15, 15)));
mobility.Install(wifiStaNodes);
// AP is stationary
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(wifiApNode);
2023-12-30 10:16:26 +01:00
/* ----- End of subnets setup*/
/* ######################################################## */
2023-12-30 10:16:26 +01:00
/* ---- ROUTING ----- */
2023-12-28 11:34:14 +01:00
// InternetStackHelper for everyone!
InternetStackHelper stack;
stack.Install(allNodes);
2023-12-30 10:16:26 +01:00
// Assign IP address to subnets
Ipv4AddressHelper address;
// nodes 2,4
2023-12-30 10:16:26 +01:00
address.SetBase(Ipv4Address("10.1.4.44"), "255.225.255.252"); // 10.1.0.0/30
Ipv4InterfaceContainer ipptp24;
ipptp24 = address.Assign(ptp24);
// nodes 4,5
2023-12-30 10:16:26 +01:00
address.SetBase(Ipv4Address("10.1.4.40"), "255.225.255.252"); // 10.1.1.0/30
Ipv4InterfaceContainer ipptp45;
ipptp45 = address.Assign(ptp45);
// nodes 3,4
2023-12-30 10:16:26 +01:00
address.SetBase(Ipv4Address("10.1.4.36"), "255.225.255.252"); // 10.1.2.0/30
Ipv4InterfaceContainer icsma34;
icsma34 = address.Assign(csma34);
// nodes 4,9
2023-12-30 10:16:26 +01:00
address.SetBase(Ipv4Address("10.1.4.32"), "255.225.255.252"); // 10.1.3.0/30
Ipv4InterfaceContainer ipptp49;
ipptp49 = address.Assign(ptp49);
// nodes 0,2
2023-12-30 10:16:26 +01:00
address.SetBase(Ipv4Address("10.1.4.52"), "255.225.255.252"); // 10.1.4.0/30
Ipv4InterfaceContainer ipptp02;
ipptp02 = address.Assign(ptp02);
2023-12-30 10:16:26 +01:00
// nodes 1,2
address.SetBase(Ipv4Address("10.1.4.48"), "255.225.255.252"); // 10.1.5.0/30
Ipv4InterfaceContainer ipptp12;
ipptp12 = address.Assign(ptp12);
// nodes 3,5
2023-12-30 10:16:26 +01:00
address.SetBase(Ipv4Address("10.1.4.24"), "255.225.255.252"); // 10.1.8.0/30
Ipv4InterfaceContainer ipptp35;
ipptp35 = address.Assign(ptp35);
// nodes 3,9
2023-12-30 10:16:26 +01:00
address.SetBase(Ipv4Address("10.1.4.28"), "255.225.255.252"); // 10.1.7.0/30
Ipv4InterfaceContainer ipptp39;
ipptp39 = address.Assign(ptp39);
// csma nodes
2023-12-30 10:16:26 +01:00
address.SetBase(Ipv4Address("10.1.4.16"), "255.225.255.248"); // 10.1.9.0/29
Ipv4InterfaceContainer icsma5678;
icsma5678 = address.Assign(csma5678);
// wifi nodes
2023-12-30 10:16:26 +01:00
address.SetBase(Ipv4Address("10.1.4.0"), "255.225.255.240"); // 10.1.9.0/28
Ipv4InterfaceContainer iStations = address.Assign(staDevices);
2023-12-30 12:56:02 +01:00
Ipv4InterfaceContainer iAp = address.Assign(apDevice);
2023-12-30 10:16:26 +01:00
// Set up Routing Table
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
/* ######################################################## */
2023-12-30 10:16:26 +01:00
/* APPLICATIONS */
/* ----- Start of TCP Burst ----- */
// Server 0 packet sink
// TCP burst traffic of 1821 B for each packet starting at 1.15 s
// Sender: Node 11 Receiver: Server 0
int port0 = 5000;
int port1 = 5001;
// Server 0 that will receive from node 11
Address server0_address1(InetSocketAddress(ipptp02.GetAddress(0), port0));
PacketSinkHelper sinkHelper0_1("ns3::TcpSocketFactory", server0_address1);
ApplicationContainer server0_app1 = sinkHelper0_1.Install(nodes02.Get(0));
server0_app1.Start(Seconds(0));
// Server 0 that will receive from node 15
Address server0_address2(InetSocketAddress(ipptp02.GetAddress(0), port1));
PacketSinkHelper sinkHelper0_2("ns3::TcpSocketFactory", server0_address2);
ApplicationContainer server0_app2 = sinkHelper0_2.Install(nodes02.Get(0));
server0_app2.Start(Seconds(0));
// Server 1 that will receive from node 10
Address server1_address(InetSocketAddress(ipptp12.GetAddress(0), port0));
PacketSinkHelper sinkHelper1("ns3::TcpSocketFactory", server1_address);
ApplicationContainer server1_app = sinkHelper1.Install(nodes12.Get(0));
server1_app.Start(Seconds(0));
// Node 11 that sends to server 0
2023-12-30 13:10:49 +01:00
OnOffHelper onOffHelper_11("ns3::TcpSocketFactory", InetSocketAddress(ipptp02.GetAddress(0), port0));
2023-12-30 10:16:26 +01:00
onOffHelper_11.SetAttribute("OnTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]"));
2023-12-30 10:24:48 +01:00
onOffHelper_11.SetAttribute("OffTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]"));
2023-12-30 10:16:26 +01:00
onOffHelper_11.SetAttribute("PacketSize", UintegerValue(1821));
onOffHelper_11.SetAttribute("StartTime", StringValue("1.15s"));
ApplicationContainer node11_app;
node11_app.Add(onOffHelper_11.Install(wifiStaNodes.Get(1)));
node11_app.Start(Seconds(1.15));
// Node 15 that sends to server 1
2023-12-30 13:10:49 +01:00
OnOffHelper onOffHelper_10("ns3::TcpSocketFactory", InetSocketAddress(ipptp12.GetAddress(0), port0));
2023-12-30 10:16:26 +01:00
onOffHelper_10.SetAttribute("OnTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]"));
2023-12-30 10:24:48 +01:00
onOffHelper_10.SetAttribute("OffTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]"));
2023-12-30 10:16:26 +01:00
onOffHelper_10.SetAttribute("PacketSize", UintegerValue(1829));
onOffHelper_10.SetAttribute("StartTime", StringValue("3.39s"));
ApplicationContainer node10_app;
node10_app.Add(onOffHelper_10.Install(wifiStaNodes.Get(0)));
node10_app.Start(Seconds(3.39));
// Node 15 that sends to server 0
2023-12-30 13:10:49 +01:00
OnOffHelper onOffHelper_15("ns3::TcpSocketFactory", InetSocketAddress(ipptp02.GetAddress(0), port1));
2023-12-30 10:16:26 +01:00
onOffHelper_15.SetAttribute("OnTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]"));
2023-12-30 10:24:48 +01:00
onOffHelper_15.SetAttribute("OffTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]"));
2023-12-30 10:16:26 +01:00
onOffHelper_15.SetAttribute("PacketSize", UintegerValue(1099));
onOffHelper_15.SetAttribute("StartTime", StringValue("3.71s"));
ApplicationContainer node15_app;
node15_app.Add(onOffHelper_15.Install(wifiStaNodes.Get(5)));
node15_app.Start(Seconds(3.71));
/* ----- End of TCP Burst ----- */
/* ----- Start of UDP Echo ----- */
// UDP Echo application with Client 13 and Server 3
// Size of packet: 1477 Bytes Periodicity: 20ms MaxPackets: 250
// UDP Echo server
UdpEchoServerHelper echoServer(9); // UDP server on port 9
ApplicationContainer serverApps = echoServer.Install(allNodes.Get(3));
serverApps.Start(Seconds(0.0));
// UDP Echo client
// UDP client targeting server 3 using the IP of the interface connected to the wifi ap
UdpEchoClientHelper echoClient(ipptp39.GetAddress(0), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(250));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(1447));
ApplicationContainer clientApps = echoClient.Install(allNodes.Get(13));
2023-12-30 10:24:48 +01:00
echoClient.SetFill(clientApps.Get(0), "Giulia,Battioni,1986870,Emanuele,Coletta,2001600,Federico,Corsale,1985903,Francesco,Maura,2017683,Marco,Totaro,1871834");
clientApps.Start(Seconds(0.0));
2023-12-30 10:16:26 +01:00
/* ----- End of UDP Echo ----- */
2023-12-30 10:16:26 +01:00
/* ######################################################## */
2023-12-30 10:16:26 +01:00
/* ----- Start of PCAP Tracing ----- */
// Only tracing switches, routers and wifi ap
// Using promiscous mode
if(tracing){
2023-12-30 10:16:26 +01:00
ptph24.EnablePcap("task-2-subnet24", ptp24.Get(0), true);
ptph24.EnablePcap("task-4-subnet24", ptp24.Get(1), true);
ptph49.EnablePcap("task-4-subnet49", ptp49.Get(0), true);
ptph49.EnablePcap("task-9-subnet49", ptp49.Get(1), true);
ptph39.EnablePcap("task-3-subnet39", ptp39.Get(0), true);
ptph39.EnablePcap("task-9-subnet39", ptp39.Get(1), true);
ptph45.EnablePcap("task-4-subnet45", ptp45.Get(0), true);
ptph45.EnablePcap("task-5-subnet45", ptp45.Get(1), true);
ptph35.EnablePcap("task-3-subnet35", ptp35.Get(0), true);
ptph35.EnablePcap("task-5-subnet35", ptp35.Get(1), true);
2023-12-30 12:56:02 +01:00
phy.EnablePcap("task-9-wifi", apDevice, true);
2023-12-30 10:16:26 +01:00
}
/* ----- End of PCAP Tracing ----- */
2023-12-27 12:52:48 +01:00
/* ######################################################## */
// Max simulation time: 15s
2023-12-28 11:34:14 +01:00
Simulator::Stop(Seconds(15));
2023-12-27 12:52:48 +01:00
Simulator::Run();
Simulator::Destroy();
2023-12-27 12:52:48 +01:00
return 0;
}
NetDeviceContainer createCSMA(NodeContainer nodes, StringValue DataRate, StringValue Delay){
CsmaHelper csma;
csma.SetDeviceAttribute("DataRate", DataRate);
csma.SetChannelAttribute("Delay", Delay);
return csma.Install(nodes);
2023-12-27 12:52:48 +01:00
}