NS3 Code Examples

Example Source code for NS3 Network simulator 3

Ns3 code examples give the overview of how the network performance parameters are calculated using network simulator 3. There are,

  • Jitter.
  • Throughput.
  • Bandwidth .

Jitter:

  • Jitter is defined as the time difference in packet inter-arrival time to their destination can be called jitter.
  • Jitter is specific issue that normally exists in packet networks and this phenomenon is usually not causing any communication problems.

Sample code for jitter: [ NS3 Code Examples]
void DelayJitterEstimation::PrepareTx (Ptr packet)
{
DelayJitterEstimationTimestampTag tag;
packet->AddByteTag (tag);
}
void DelayJitterEstimation::RecordRx (Ptr packet)
{
DelayJitterEstimationTimestampTag tag;
bool found;
found = packet->FindFirstMatchingByteTag (tag);
if (!found)
{
return;
}
tag.GetTxTime ();
Time delta = (Simulator::Now () – m_previousRx) – (tag.GetTxTime () – m_previousRxTx);
m_jitter += (Abs (delta) – m_jitter) / 16;
m_previousRx = Simulator::Now ();
m_previousRxTx = tag.GetTxTime ();
m_delay = Simulator::Now () – tag.GetTxTime ();
}
Time
DelayJitterEstimation::GetLastDelay (void) const
{
return m_delay;
}
uint64_t
DelayJitterEstimation::GetLastJitter (void) const
{
return m_jitter.GetHigh ();
}

Throughput:

  • Throughput is the sum of data rates at which data is delivered to all the nodes or terminals in network.
  • Throughput is measured in bits per second under digital bandwidth of channel n sometimes in terms of packets per time slot.

Sample code for throughput – NS3 Code Examples:
void
ThroughputPerSecond (Ptr sink1Apps, int totalPacketsThrough, Ptr node)
{
double throughput = 0.0;
Ptr sink1 = DynamicCast (sink1Apps);
// Calculate and output throughput per second
totalPacketsThrough = sink1->GetTotalRx ();
throughput = totalPacketsThrough*8/((237.0)*1000000.0);
std::cout << throughput;
// Reschedule ThroughputPerSecond
//Simulator::Schedule (Seconds (0.0), &ThroughputPerSecond, sink1Apps, totalPacketsThrough, node);
}

Bandwidth:

  • Bandwidth is defined as the data-carrying capacity of a network or data transmission medium.
  • It indicates the maximum amount of data that can pass from one point to another in a unit of time.

Sample code for bandwidth – NS3 Code Examples:
void BandwidthManager::SendBandwidthRequest (uint8_t uiuc, uint16_t allocationSize)
{
Ptr ss = m_device->GetObject ();
uint32_t bytesToRequest = 0;
ServiceFlow *serviceFlow = SelectFlowForRequest (bytesToRequest);
if (!serviceFlow || !bytesToRequest)
{
return;
}
BandwidthRequestHeader bwRequestHdr;
bwRequestHdr.SetType ((uint8_t) BandwidthRequestHeader::HEADER_TYPE_AGGREGATE);
bwRequestHdr.SetCid (serviceFlow->GetConnection ()->GetCid ());
bwRequestHdr.SetBr (bytesToRequest);
Ptr packet = Create ();
packet->AddHeader (bwRequestHdr);
ss->Enqueue (packet, MacHeaderType (MacHeaderType::HEADER_TYPE_BANDWIDTH), serviceFlow->GetConnection ());
m_nrBwReqsSent++;
NS_ASSERT_MSG (uiuc == OfdmUlBurstProfile::UIUC_REQ_REGION_FULL, “Send Bandwidth Request: !UIUC_REQ_REGION_FULL”);
ss->SendBurst (uiuc, allocationSize, serviceFlow->GetConnection (), MacHeaderType::HEADER_TYPE_BANDWIDTH);
}
void BandwidthManager::ProcessBandwidthRequest (const BandwidthRequestHeader &bwRequestHdr)
{
Ptr bs = m_device->GetObject ();
ServiceFlow *serviceFlow = bs->GetConnectionManager ()->GetConnection (bwRequestHdr.GetCid ())->GetServiceFlow ();
if (bwRequestHdr.GetType () == (uint8_t) BandwidthRequestHeader::HEADER_TYPE_INCREMENTAL)
{
serviceFlow->GetRecord ()->UpdateRequestedBandwidth (bwRequestHdr.GetBr ());
}
else
{
serviceFlow->GetRecord ()->SetRequestedBandwidth (bwRequestHdr.GetBr ());
bs->GetUplinkScheduler ()->OnSetRequestedBandwidth (serviceFlow->GetRecord ());
}
bs->GetUplinkScheduler ()->ProcessBandwidthRequest (bwRequestHdr);
serviceFlow->GetRecord ()->IncreaseBacklogged (bwRequestHdr.GetBr ());
}
void BandwidthManager::SetSubframeRatio (void)
{
Ptr bs = m_device->GetObject ();
uint32_t symbolsPerFrame = bs->GetPhy ()->GetSymbolsPerFrame ();
bs->SetNrDlSymbols (symbolsPerFrame / 2);
bs->SetNrUlSymbols (symbolsPerFrame / 2);
}