I will show how to dynamically change the link bandwidth here. The link capacity is changed every 20 seconds.
#include "ns3/core-module.h"
#include "ns3/applications-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/traffic-control-module.h"
#include "ns3/bulk-send-helper.h"
#include "ns3/packet-sink-helper.h"
#include "ns3/nstime.h"
using namespace ns3;
using namespace std;
const uint32_t TOPO_DEFAULT_BW = 2000000; // in bps: 2Mbps
const uint32_t TOPO_DEFAULT_PDELAY = 100; // in ms: 100ms
const uint32_t TOPO_DEFAULT_QDELAY = 300; // in ms: 300ms
const uint32_t DEFAULT_PACKET_SIZE = 1000;
NS_LOG_COMPONENT_DEFINE ("Dynamic-Link");
const static uint32_t rateArray[]=
{
2000000,
1500000,
1000000,
500000,
1000000,
1500000,
};
class ChangeBw
{
public:
ChangeBw(Ptr<NetDevice> netdevice)
{
m_total=sizeof(rateArray)/sizeof(rateArray[0]);
m_netdevice=netdevice;
}
//ChangeBw(){}
~ChangeBw(){}
void Start()
{
Time next=Seconds(m_gap);
m_timer=Simulator::Schedule(next,&ChangeBw::ChangeRate,this);
}
void ChangeRate()
{
if(m_timer.IsExpired())
{
NS_LOG_INFO(Simulator::Now().GetSeconds()<<" "<<rateArray[m_index]/1000);
PointToPointNetDevice *device=static_cast<PointToPointNetDevice*>(PeekPointer(m_netdevice));
device->SetDataRate(DataRate(rateArray[m_index]));
m_index=(m_index+1)%m_total;
Time next=Seconds(m_gap);
m_timer=Simulator::Schedule(next,&ChangeBw::ChangeRate,this);
}
}
private:
uint32_t m_index{1};
uint32_t m_gap{20};//change the link banwidth every 20s
uint32_t m_total{0};
Ptr<NetDevice>m_netdevice;
EventId m_timer;
};
static NodeContainer BuildExampleTopo (uint64_t bps,
uint32_t msDelay,
uint32_t msQdelay)
{
NodeContainer nodes;
nodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (bps)));
pointToPoint.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (msDelay)));
auto bufSize = std::max<uint32_t> (DEFAULT_PACKET_SIZE, bps * msQdelay / 8000);
pointToPoint.SetQueue ("ns3::DropTailQueue",
"Mode", StringValue ("QUEUE_MODE_BYTES"),
"MaxBytes", UintegerValue (bufSize));
NetDeviceContainer devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
address.Assign (devices);
// Uncomment to capture simulated traffic
// pointToPoint.EnablePcapAll ("rmcat-example");
// disable tc for now, some bug in ns3 causes extra delay
TrafficControlHelper tch;
tch.Uninstall (devices);
return nodes;
}
static double simDuration=200;
int main(int argc, char *argv[])
{
/*create your own topology here*/
const uint64_t linkBw = TOPO_DEFAULT_BW;
const uint32_t msDelay = TOPO_DEFAULT_PDELAY;
const uint32_t msQDelay = TOPO_DEFAULT_QDELAY;
NodeContainer nodes = BuildExampleTopo (linkBw, msDelay, msQDelay);
/*nodes.Get(0) make sure this is node that is data generator*/
Ptr<NetDevice> netDevice=nodes.Get(0)->GetDevice(0);
ChangeBw change(netDevice);
change.Start();
Simulator::Stop (Seconds(simDuration + 10.0));
Simulator::Run ();
Simulator::Destroy();
return 0;
}