NS3: Dynamically Change the bandwidth

 

 

Based on https://blog.csdn.net/u010643777/article/details/80590045, I wrote this lab

 

[topology]

N0(TCP Sender)----P2Plink(5Mbps,2ms)---N1(TCP Sink)

 

At 5th second, I will change the bandwidth of P2Plink from 5Mbps to 1000000bps.

 

test-change-bandwidth.cc (put this file under scratch)

#include <fstream>

#include <string.h>

 

#include "ns3/core-module.h"

#include "ns3/internet-module.h"

#include "ns3/point-to-point-module.h"

#include "ns3/packet-sink.h"

#include "ns3/packet-sink-helper.h"

#include "ns3/applications-module.h"

 

using namespace ns3;

 

NS_LOG_COMPONENT_DEFINE ("ThroughputTCP");

 

Ptr<PacketSink> sink;                         /* Pointer to the packet sink application */

uint64_t lastTotalRx = 0;                     /* The value of the last total received bytes */

 

void

CalculateThroughput ()

{

  Time now = Simulator::Now ();                                         /* Return the simulator's virtual time. */

  double cur = (sink->GetTotalRx () - lastTotalRx) * (double) 8 / 1e6;     /* Convert Application RX Packets to MBits. */

  std::cout << now.GetSeconds () << "\t" << cur << std::endl;

  lastTotalRx = sink->GetTotalRx ();

  Simulator::Schedule (MilliSeconds (1000), &CalculateThroughput);

}

 

void

ChangeBandwith(Ptr<NetDevice> netdevice, uint64_t bps)

{

  PointToPointNetDevice *device=static_cast<PointToPointNetDevice *>(PeekPointer(netdevice));

  device->SetDataRate(DataRate(bps));

}

 

int

main (int argc, char *argv[])

{

  NS_LOG_INFO ("Create nodes.");

  NodeContainer n;

  n.Create (2);

 

  InternetStackHelper internet;

  internet.Install (n);

 

  NS_LOG_INFO ("Create channels.");

 

  PointToPointHelper pointToPoint;

  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));

  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

  pointToPoint.SetDeviceAttribute("Mtu", UintegerValue(1400));  

  NetDeviceContainer p2pDevices;

  p2pDevices = pointToPoint.Install (n);    

 

  Ipv4AddressHelper ipv4;

 

  NS_LOG_INFO ("Assign IP Addresses.");

  ipv4.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer i = ipv4.Assign (p2pDevices);

 

  NS_LOG_INFO ("Create Applications.");

 

  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9));

  ApplicationContainer sinkApp = sinkHelper.Install (n.Get(1));

  sink = StaticCast<PacketSink> (sinkApp.Get (0));

 

  BulkSendHelper source ("ns3::TcpSocketFactory", (InetSocketAddress (i.GetAddress (1), 9)));

  source.SetAttribute ("MaxBytes", UintegerValue (0));

  ApplicationContainer sourceApps = source.Install (n.Get (0));

 

  sourceApps.Start (Seconds (1.0));

  sourceApps.Stop (Seconds (15.0));

  sinkApp.Start (Seconds (0.0));

  Simulator::Schedule (Seconds (1.0), &CalculateThroughput);

 

  //At 5th second, change the bandwidth from 5Mbps to 1000000bps

  Simulator::Schedule (Seconds (5.0), &ChangeBandwith, p2pDevices.Get(0), 1000000);

 

  Simulator::Stop (Seconds (16.0));

  NS_LOG_INFO ("Run Simulation.");

  Simulator::Run ();

  Simulator::Destroy ();

  NS_LOG_INFO ("Done.");

 

}

 

Execution


Back to NS3 Learning Guide

Last Modified: 2022/2/14

 

Dr. Chih-Heng Ke

Department of Computer Science and Information Engineering, National Quemoy University, Kinmen, Taiwan

Email: smallko@gmail.com