Change the link bandwidth dynamically on ns3

 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;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值