Skip to content

Instantly share code, notes, and snippets.

@patiljeevanr
Last active March 26, 2018 10:25
Show Gist options
  • Save patiljeevanr/10313168882e8b4f9cac2d1445550082 to your computer and use it in GitHub Desktop.
Save patiljeevanr/10313168882e8b4f9cac2d1445550082 to your computer and use it in GitHub Desktop.
ethernet tcp
#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/flow-monitor-module.h"
// Default Network Topology
//
//
// n1 n2 n3 n4
// | | | |
// ================
// LAN 10.1.2.0
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("FastRetransmission");
int
main (int argc, char *argv[])
{
uint32_t nCsma = 3;
CommandLine cmd;
cmd.AddValue ("nCsma", "Number of CSMA nodes/devices", nCsma);
cmd.Parse (argc,argv);
nCsma = nCsma == 0 ? 1 : nCsma;
NodeContainer csmaNodes;
csmaNodes.Create (nCsma);
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);
InternetStackHelper stack;
stack.Install (csmaNodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);
/*UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma-1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma-1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (csmaNodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
*/
ApplicationContainer cbrApps;
uint16_t cbrPort = 12345;
for (size_t i = 0; i < nCsma/2; ++i)
{
std::string datarate_cbr;
std::ostringstream oss;
oss << 3000000 + ((i+1)*1000);
datarate_cbr += oss.str();
//std::cout<<datarate_cbr<<"\n";
double time_cbr = 1.000 + ((.001)*(i));
//getting ip address of a sink node
Ptr<Ipv4> ipv4 = NodeList::GetNode((nCsma/2) + i)->GetObject<Ipv4>();
Ipv4InterfaceAddress iaddr = ipv4->GetAddress (1,0);
Ipv4Address addri = iaddr.GetLocal ();
OnOffHelper onOffHelper ("ns3::TcpSocketFactory", InetSocketAddress (addri, cbrPort));
onOffHelper.SetAttribute ("PacketSize", UintegerValue (1400));
onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onOffHelper.SetAttribute ("DataRate", StringValue (datarate_cbr + "bps"));
onOffHelper.SetAttribute ("StartTime", TimeValue (Seconds (time_cbr)));
cbrApps.Add (onOffHelper.Install (csmaNodes.Get (i)));
}
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
csma.EnablePcap ("second", csmaDevices.Get (1), true);
//Install FlowMonitor on all nodes
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
Simulator::Stop (Seconds (20.0));
Simulator::Run ();
//Print per flow statistics
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
// first 2 FlowIds are for ECHO apps, we don't want to display them
//
// Duration for throughput measurement is 9.0 seconds, since
// StartTime of the OnOffApplication is at about "second 1"
// and
// Simulator::Stops at "second 10".
//first few are echo packets we sent
//if (i->first > 0)//(uint16_t)(nwifisource + nwifisink - 1))
// {
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
std::cout << "Flow " << i->first - 0 << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";//(uint16_t)(nwifisource + nwifisink - 1)
std::cout << " Tx Packets: " << i->second.txPackets << "\n";
std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
std::cout << " TxOffered: " << i->second.txBytes * 8.0 / 19.0 / 1000 / 1000 << " Mbps\n"; // 20 - 7 = 13
std::cout << " Rx Packets: " << i->second.rxPackets << "\n";
std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
std::cout << " Throughput: " << i->second.rxBytes * 8.0 / 19.0 / 1000 / 1000 << " Mbps\n";
float prx = (i->second.rxBytes);// / i->second.txBytes);
float ptx = (i->second.txBytes);
std::cout << " Loss Packets " << (int)(ptx - prx) <<"\n";
//std::cout << " Loss Packets " << i->second.lostPackets << "\n"; only for 10 seconds
std::cout << " PDR " << prx/ptx << "\n";
std::cout << " PLR " << (ptx - prx)/ptx << "\n";
std::cout << " Sum of all Jitter " << (i->second.jitterSum).GetSeconds() << "\n" ;
// }
}
Simulator::Destroy ();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment