Simulation of persistent vs. non-persistent CSMA in a 15-node wired LAN and compare channel utilization | NS3 Project 9
Computer Networks Digital Assignment
Topic:
Simulate persistent vs. non-persistent CSMA in a 15-node wired LAN and compare channel utilization.
Code:
// csma-persistent.cc
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/flow-monitor-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("CsmaSimulation");
int main (int argc, char *argv[])
{
bool persistent = true; // change to false for non-persistent
CommandLine cmd;
cmd.AddValue("persistent", "Enable persistent CSMA", persistent);
cmd.Parse(argc, argv);
NodeContainer nodes;
nodes.Create(15);
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
NetDeviceContainer devices = csma.Install(nodes);
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
uint16_t port = 9;
// Server on node 0
UdpServerHelper server(port);
ApplicationContainer serverApp = server.Install(nodes.Get(0));
serverApp.Start(Seconds(1.0));
serverApp.Stop(Seconds(20.0));
// Clients on remaining nodes
for (int i = 1; i < 15; i++)
{
UdpClientHelper client(interfaces.GetAddress(0), port);
client.SetAttribute("MaxPackets", UintegerValue(1000));
client.SetAttribute("Interval",
TimeValue(persistent ? Seconds(0.01) : Seconds(0.05))); // key difference
client.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer clientApp = client.Install(nodes.Get(i));
clientApp.Start(Seconds(2.0 + i * 0.1));
clientApp.Stop(Seconds(20.0));
}
// Enable tracing
csma.EnablePcapAll("csma");
// NetAnim
std::string mode = persistent ? "persistent" : "nonpersistent";
AnimationInterface anim("csma-" + mode + ".xml");
// PCAP trace file
csma.EnablePcapAll("csma-" + mode);
Simulator::Stop(Seconds(20.0));
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
Simulator::Run();
monitor->CheckForLostPackets();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>(flowmon.GetClassifier());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats();
for (auto &flow : stats)
{
std::cout << "Flow ID: " << flow.first << std::endl;
std::cout << "Tx Packets: " << flow.second.txPackets << std::endl;
std::cout << "Rx Packets: " << flow.second.rxPackets << std::endl;
std::cout << "Throughput: " << flow.second.rxBytes * 8.0 /
(flow.second.timeLastRxPacket.GetSeconds() -
flow.second.timeFirstTxPacket.GetSeconds()) / 1024 << " Kbps\n";
}
Simulator::Destroy();
return 0;
}
Commands to run the code:
- Create a file inside the
scratchfolder and save the code ascsma-persistent.cc. -
To run code in persistent mode:
./ns3 run "scratch/csma-persistent --persistent=1" -
To run code in non-persistent mode:
./ns3 run "scratch/csma-persistent --persistent=0" -
Generated Output Files:
-
For Persistent:
csma-persistent.xmlcsma-persistent-*.pcap
-
For Non-Persistent:
csma-nonpersistent.xmlcsma-nonpersistent-*.pcap
-
For Persistent:
Comments
Post a Comment