Skip to content

Instantly share code, notes, and snippets.

@Blackhawk95
Last active April 10, 2018 07:01
Show Gist options
  • Select an option

  • Save Blackhawk95/84f09a2ab07a2ae77e943d232bbfdcd7 to your computer and use it in GitHub Desktop.

Select an option

Save Blackhawk95/84f09a2ab07a2ae77e943d232bbfdcd7 to your computer and use it in GitHub Desktop.
ns3 third output
CODE
---------i think u already took this ------------
#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"
#include "ns3/applications-module.h"
#include "ns3/tcp-socket-base.h"
#include <sstream>
#include <fstream>
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("FastRetransmission");
uint32_t avg_cwd = 0;
// Default Network Topology
//
// 10.1.2.0
// n1 n2 n3 n4
// | | | |
// ================
// LAN 10.1.2.0
class MyApp : public Application
{
public:
MyApp ();
virtual ~MyApp();
void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
private:
virtual void StartApplication (void);
virtual void StopApplication (void);
void ScheduleTx (void);
void SendPacket (void);
Ptr<Socket> m_socket;
Address m_peer;
uint32_t m_packetSize;
uint32_t m_nPackets;
DataRate m_dataRate;
EventId m_sendEvent;
bool m_running;
uint32_t m_packetsSent;
};
MyApp::MyApp ()
: m_socket (0),
m_peer (),
m_packetSize (0),
m_nPackets (0),
m_dataRate (0),
m_sendEvent (),
m_running (false),
m_packetsSent (0)
{
}
MyApp::~MyApp()
{
m_socket = 0;
}
void
MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
{
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
m_nPackets = nPackets;
m_dataRate = dataRate;
}
void
MyApp::StartApplication (void)
{
m_running = true;
m_packetsSent = 0;
m_socket->Bind ();
m_socket->Connect (m_peer);
SendPacket ();
}
void
MyApp::StopApplication (void)
{
m_running = false;
if (m_sendEvent.IsRunning ())
{
Simulator::Cancel (m_sendEvent);
}
if (m_socket)
{
m_socket->Close ();
}
}
void
MyApp::SendPacket (void)
{
Ptr<Packet> packet = Create<Packet> (m_packetSize);
m_socket->Send (packet);
if (++m_packetsSent < m_nPackets)
{
ScheduleTx ();
}
}
void
MyApp::ScheduleTx (void)
{
if (m_running)
{
Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
}
}
static void
CwndChange (uint32_t oldCwnd, uint32_t newCwnd)
{ std::fstream f;
f.open("output3.csv", std::fstream::in| std::fstream::app);
f << Simulator::Now ().GetSeconds () << " " << newCwnd << "\n";
//NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
}
static void
RxDrop (Ptr<const Packet> p)
{
NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());
}
int
main (int argc, char *argv[])
{
uint32_t nCsma = 2;
CommandLine cmd;
cmd.AddValue ("nCsma", "Number of CSMA nodes/devices", nCsma);
cmd.Parse (argc,argv);
Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpNewReno::GetTypeId ()));
uint32_t thresh = 100;
Config::SetDefault ("ns3::TcpSocketBase::ReTxThreshold", UintegerValue(thresh));
Config::SetDefault ("ns3::TcpSocketBase::LimitedTransmit", BooleanValue (false));
nCsma = nCsma == 0 ? 1 : nCsma;
NodeContainer csmaNodes;
csmaNodes.Create (nCsma);
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", StringValue ("10Kbps"));
csma.SetChannelAttribute ("Delay", StringValue ("10ms"));
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 << 20000 + ((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 ();
//uint16_t sinkPort = 8080;
Address sinkAddress (InetSocketAddress (csmaInterfaces.GetAddress ((nCsma/2)+i), cbrPort));
PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), cbrPort));
ApplicationContainer sinkApps = packetSinkHelper.Install (csmaNodes.Get ((nCsma/2)+i));
sinkApps.Start (Seconds (0.));
sinkApps.Stop (Seconds (1000.));
Ptr<Socket> ns3TcpSocket = Socket::CreateSocket (csmaNodes.Get (i), TcpSocketFactory::GetTypeId ());
if(i==0)
ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow", MakeCallback (&CwndChange));
Ptr<MyApp> app = CreateObject<MyApp> ();
app->Setup (ns3TcpSocket, InetSocketAddress(addri,cbrPort) , 1500, 700000, DataRate ( datarate_cbr+"bps"));
csmaNodes.Get (i)->AddApplication (app);
app->SetStartTime (Seconds (1.));
app->SetStopTime (Seconds (1000.));
csmaDevices.Get ((nCsma/2)+i)->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&RxDrop));
}
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
csma.EnablePcap ("second", csmaDevices.Get (1), true);
//Install FlowMonitor on all nodes
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
Simulator::Stop (Seconds (1000.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 / 999.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 / 999.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;
}
.........................................................................
[jeevan@jeevan-pc ns-3.27]$ ./waf --run "scratch/irdk --nCsma=10"
Waf: Entering directory `/home/jeevan/networklab/ns-allinone-3.27/ns-3.27/build'
Waf: Leaving directory `/home/jeevan/networklab/ns-allinone-3.27/ns-3.27/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (1.125s)
Flow 1 (10.1.1.1 -> 10.1.1.6)
Tx Packets: 1522
Tx Bytes: 893868
TxOffered: 0.0071581 Mbps
Rx Packets: 1426
Rx Bytes: 837420
Throughput: 0.00670607 Mbps
Loss Packets 56448
PDR 0.93685
PLR 0.0631503
Sum of all Jitter 821.141
Flow 2 (10.1.1.2 -> 10.1.1.7)
Tx Packets: 6
Tx Bytes: 336
TxOffered: 2.69069e-06 Mbps
Rx Packets: 0
Rx Bytes: 0
Throughput: 0 Mbps
Loss Packets 336
PDR 0
PLR 1
Sum of all Jitter 0
Flow 3 (10.1.1.3 -> 10.1.1.8)
Tx Packets: 6
Tx Bytes: 336
TxOffered: 2.69069e-06 Mbps
Rx Packets: 0
Rx Bytes: 0
Throughput: 0 Mbps
Loss Packets 336
PDR 0
PLR 1
Sum of all Jitter 0
Flow 4 (10.1.1.4 -> 10.1.1.9)
Tx Packets: 6
Tx Bytes: 336
TxOffered: 2.69069e-06 Mbps
Rx Packets: 2
Rx Bytes: 112
Throughput: 8.96897e-07 Mbps
Loss Packets 224
PDR 0.333333
PLR 0.666667
Sum of all Jitter 0.271907
Flow 5 (10.1.1.5 -> 10.1.1.10)
Tx Packets: 508
Tx Bytes: 297636
TxOffered: 0.00238347 Mbps
Rx Packets: 449
Rx Bytes: 262944
Throughput: 0.00210566 Mbps
Loss Packets 34692
PDR 0.883442
PLR 0.116558
Sum of all Jitter 225.364
Flow 6 (10.1.1.6 -> 10.1.1.1)
Tx Packets: 1425
Tx Bytes: 77496
TxOffered: 0.000620589 Mbps
Rx Packets: 790
Rx Bytes: 44468
Throughput: 0.0003561 Mbps
Loss Packets 33028
PDR 0.57381
PLR 0.42619
Sum of all Jitter 24.1233
Flow 7 (10.1.1.10 -> 10.1.1.5)
Tx Packets: 448
Tx Bytes: 23572
TxOffered: 0.000188765 Mbps
Rx Packets: 229
Rx Bytes: 12184
Throughput: 9.75696e-05 Mbps
Loss Packets 11388
PDR 0.516884
PLR 0.483116
Sum of all Jitter 12.8234
Flow 8 (10.1.1.9 -> 10.1.1.4)
Tx Packets: 6
Tx Bytes: 336
TxOffered: 2.69069e-06 Mbps
Rx Packets: 0
Rx Bytes: 0
Throughput: 0 Mbps
Loss Packets 336
PDR 0
PLR 1
Sum of all Jitter 0
.........................................................
CSV file is very big ..its in the format
time windowsize
..........................................................
use images from mail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment