NS3: Link Down and Link Up Test

 

[Topology]

term_0 ……router_0……router_1….term1

 

At router_0:

0:lookback interface

1: interface that connects to term_0

2:interface that connects to router_1

 

From 5th second to 10th second, I will let the link between router_0 and router_1 down.

 

test-linkdownup.cc (put this file under scratch)

#include <iostream>

#include <fstream>

#include <string>

#include <cassert>

 

#include "ns3/core-module.h"

#include "ns3/network-module.h"

#include "ns3/internet-module.h"

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

#include "ns3/csma-module.h"

#include "ns3/applications-module.h"

#include "ns3/packet-sink.h"

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

 

using namespace ns3;

 

NS_LOG_COMPONENT_DEFINE ("LinkDownUpTest");

 

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 / 1e3;     /* Convert Application RX Packets to KBits. */

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

  lastTotalRx = sink->GetTotalRx ();

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

}

 

int

main (int argc, char *argv[])

{    

  CommandLine cmd (__FILE__);

  cmd.Parse (argc, argv);

 

   /* Build nodes. */

  NodeContainer term_0;

  term_0.Create (1);

  NodeContainer term_1;

  term_1.Create (1);

  NodeContainer router_0;

  router_0.Create (1);

  NodeContainer router_1;

  router_1.Create (1);

 

  /* Build link. */

  PointToPointHelper p2p_p2p_0;

  p2p_p2p_0.SetDeviceAttribute ("DataRate", StringValue ("1Mbps"));

  p2p_p2p_0.SetChannelAttribute ("Delay", StringValue ("1ms"));

  PointToPointHelper p2p_p2p_1;

  p2p_p2p_1.SetDeviceAttribute ("DataRate", StringValue ("1Mbps"));

  p2p_p2p_1.SetChannelAttribute ("Delay", StringValue ("1ms"));

  PointToPointHelper p2p_p2p_2;

  p2p_p2p_2.SetDeviceAttribute ("DataRate", StringValue ("1Mbps"));

  p2p_p2p_2.SetChannelAttribute ("Delay", StringValue ("1ms"));

 

  /* Build link net device container. */

  NodeContainer all_p2p_0;

  all_p2p_0.Add (router_0);

  all_p2p_0.Add (term_0);

  NetDeviceContainer ndc_p2p_0 = p2p_p2p_0.Install (all_p2p_0);

  NodeContainer all_p2p_1;

  all_p2p_1.Add (router_0);

  all_p2p_1.Add (router_1);

  NetDeviceContainer ndc_p2p_1 = p2p_p2p_1.Install (all_p2p_1);

  NodeContainer all_p2p_2;

  all_p2p_2.Add (router_1);

  all_p2p_2.Add (term_1);

  NetDeviceContainer ndc_p2p_2 = p2p_p2p_2.Install (all_p2p_2);

 

  /* Install the IP stack. */

  InternetStackHelper internetStackH;

  internetStackH.Install (term_0);

  internetStackH.Install (term_1);

  internetStackH.Install (router_0);

  internetStackH.Install (router_1);

 

  /* IP assign. */

  Ipv4AddressHelper ipv4;

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

  Ipv4InterfaceContainer iface_ndc_p2p_0 = ipv4.Assign (ndc_p2p_0);

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

  Ipv4InterfaceContainer iface_ndc_p2p_1 = ipv4.Assign (ndc_p2p_1);

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

  Ipv4InterfaceContainer iface_ndc_p2p_2 = ipv4.Assign (ndc_p2p_2);

 

  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

 

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

  ApplicationContainer sinkApp = sinkHelper.Install (term_1.Get(0));

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

 

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

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

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

  sourceApps.Start (Seconds (1.0));

  sourceApps.Stop (Seconds (15.0));

  sinkApp.Start (Seconds (0.0));

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

 

 

  Ptr<Node> n1 = router_0.Get (0);

  Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();

  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,

  // then the next p2p is numbered 2 (router_0....router_1)

  uint32_t ipv4ifIndex1 = 2;

  Simulator::Schedule (Seconds (5),&Ipv4::SetDown,ipv41, ipv4ifIndex1);

  Simulator::Schedule (Seconds (10),&Ipv4::SetUp,ipv41, ipv4ifIndex1);

 

  Simulator::Stop (Seconds (16.0));

  Simulator::Run ();

  Simulator::Destroy ();

 

  return 0;

 }

 

Execution

 

We can see that when the link is down, throughput is 0 from 7th second to12th second.

 

Reference

https://www.nsnam.org/docs/release/3.19/doxygen/dynamic-global-routing_8cc_source.html

 

Back to NS3 Learning Guide

Last Modified: 2022/2/13 done

 

[Author]

Dr. Chih-Heng Ke

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

Email: smallko@gmail.com