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 : m_lost(0),
23 m_bitMapSize(0),
24 m_lastMaxSeqNum(0),
25 m_receiveBitMap(nullptr)
26{
27 NS_LOG_FUNCTION(this << bitmapSize);
28 SetBitMapSize(bitmapSize);
29}
30
36
37uint16_t
39{
40 NS_LOG_FUNCTION(this);
41 return m_bitMapSize * 8;
42}
43
44void
46{
47 NS_LOG_FUNCTION(this << winSize);
48
49 NS_ASSERT_MSG(winSize % 8 == 0, "The packet window size should be a multiple of 8");
50 m_bitMapSize = winSize / 8;
51 if (m_receiveBitMap != nullptr)
52 {
53 delete[] m_receiveBitMap;
54 }
55 m_receiveBitMap = new uint8_t[m_bitMapSize]();
56 memset(m_receiveBitMap, 0xFF, m_bitMapSize);
57}
58
61{
62 NS_LOG_FUNCTION(this);
63 return m_lost;
64}
65
66bool
68{
69 NS_LOG_FUNCTION(this << seqNum);
70 return ((m_receiveBitMap[(seqNum % (m_bitMapSize * 8)) / 8] >> (7 - (seqNum % 8))) & 0x01);
71}
72
73void
75{
76 NS_LOG_FUNCTION(this << seqNum << val);
77 if (val)
78 {
79 m_receiveBitMap[(seqNum % (m_bitMapSize * 8)) / 8] |= 0x80 >> (seqNum % 8);
80 }
81 else
82 {
83 m_receiveBitMap[(seqNum % (m_bitMapSize * 8)) / 8] &= ~(0x80 >> (seqNum % 8));
84 }
85}
86
87/*
88 * This algo works as follows:
89 * When a packet is received:
90 * 1) From the last received packet to the current one:
91 * 1.1) check the corresponding bit in the bitMAP.
92 * This bit indicates if the packet with (SeqNum-bitMapSizeInBit) is
93 * received (1) or not (0)
94 * 1.2) Mark the packet as lost (0) in the bitMap
95 * 2) Mark the current packet as received (1) in the bitMap
96 * 3) Update the value of the last received packet
97 */
98
99void
101{
102 NS_LOG_FUNCTION(this << seqNum);
103 for (uint32_t i = m_lastMaxSeqNum + 1; i <= seqNum; i++)
104 {
105 if (!GetBit(i))
106 {
107 NS_LOG_INFO("Packet lost: " << i - (m_bitMapSize * 8));
108 m_lost++;
109 }
110 SetBit(i, false);
111 }
112 SetBit(seqNum, true);
113 if (seqNum > m_lastMaxSeqNum)
114 {
115 m_lastMaxSeqNum = seqNum;
116 }
117}
118
119} // 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.