A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet-loss-counter.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 INRIA, UDCAST
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Amine Ismail <amine.ismail@sophia.inria.fr>
7 * <amine.ismail@udcast.com>
8 */
9
10#include "packet-loss-counter.h"
11
12#include "ns3/log.h"
13#include "ns3/simulator.h"
14#include "ns3/uinteger.h"
15
16namespace ns3
17{
18
19NS_LOG_COMPONENT_DEFINE("PacketLossCounter");
20
22{
23 NS_LOG_FUNCTION(this << bitmapSize);
24 SetBitMapSize(bitmapSize);
25}
26
32
33uint16_t
35{
36 NS_LOG_FUNCTION(this);
37 return m_bitMapSize * 8;
38}
39
40void
42{
43 NS_LOG_FUNCTION(this << winSize);
44
45 NS_ASSERT_MSG(winSize % 8 == 0, "The packet window size should be a multiple of 8");
46 m_bitMapSize = winSize / 8;
47 if (m_receiveBitMap != nullptr)
48 {
49 delete[] m_receiveBitMap;
50 }
51 m_receiveBitMap = new uint8_t[m_bitMapSize]();
52 memset(m_receiveBitMap, 0xFF, m_bitMapSize);
53}
54
57{
58 NS_LOG_FUNCTION(this);
59 return m_lost;
60}
61
62bool
64{
65 NS_LOG_FUNCTION(this << seqNum);
66 return ((m_receiveBitMap[(seqNum % (m_bitMapSize * 8)) / 8] >> (7 - (seqNum % 8))) & 0x01);
67}
68
69void
71{
72 NS_LOG_FUNCTION(this << seqNum << val);
73 if (val)
74 {
75 m_receiveBitMap[(seqNum % (m_bitMapSize * 8)) / 8] |= 0x80 >> (seqNum % 8);
76 }
77 else
78 {
79 m_receiveBitMap[(seqNum % (m_bitMapSize * 8)) / 8] &= ~(0x80 >> (seqNum % 8));
80 }
81}
82
83/*
84 * This algo works as follows:
85 * When a packet is received:
86 * 1) From the last received packet to the current one:
87 * 1.1) check the corresponding bit in the bitMAP.
88 * This bit indicates if the packet with (SeqNum-bitMapSizeInBit) is
89 * received (1) or not (0)
90 * 1.2) Mark the packet as lost (0) in the bitMap
91 * 2) Mark the current packet as received (1) in the bitMap
92 * 3) Update the value of the last received packet
93 */
94
95void
97{
98 NS_LOG_FUNCTION(this << seqNum);
99 for (uint32_t i = m_lastMaxSeqNum + 1; i <= seqNum; i++)
100 {
101 if (!GetBit(i))
102 {
103 NS_LOG_INFO("Packet lost: " << i - (m_bitMapSize * 8));
104 m_lost++;
105 }
106 SetBit(i, false);
107 }
108 SetBit(seqNum, true);
109 if (seqNum > m_lastMaxSeqNum)
110 {
111 m_lastMaxSeqNum = seqNum;
112 }
113}
114
115} // namespace ns3
void SetBit(uint32_t seqNum, bool val)
Set a sequence number to a given state.
void NotifyReceived(uint32_t seq)
Record a successfully received packet.
uint32_t m_lastMaxSeqNum
Last sequence number seen.
bool GetBit(uint32_t seqNum)
Check if a sequence number in the window has been received.
PacketLossCounter(uint8_t bitmapSize)
Constructor.
uint32_t m_lost
Lost packets counter.
void SetBitMapSize(uint16_t size)
Set the size of the window used to compute the packet loss.
uint8_t * m_receiveBitMap
Received packets in the window size.
uint16_t GetBitMapSize() const
Return the size of the window used to compute the packet loss.
uint32_t GetLost() const
Get the number of lost packets.
uint16_t m_bitMapSize
Window size.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Every class exported by the ns3 library is enclosed in the ns3 namespace.