A Discrete-Event Network Simulator
API
packet-loss-counter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 INRIA, UDCAST
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Amine Ismail <amine.ismail@sophia.inria.fr>
18  * <amine.ismail@udcast.com>
19  */
20 
21 #include "packet-loss-counter.h"
22 
23 #include "ns3/log.h"
24 #include "ns3/simulator.h"
25 #include "ns3/uinteger.h"
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("PacketLossCounter");
31 
33  : m_lost(0),
34  m_bitMapSize(0),
35  m_lastMaxSeqNum(0),
36  m_receiveBitMap(nullptr)
37 {
38  NS_LOG_FUNCTION(this << bitmapSize);
39  SetBitMapSize(bitmapSize);
40 }
41 
43 {
44  NS_LOG_FUNCTION(this);
45  delete[] m_receiveBitMap;
46 }
47 
48 uint16_t
50 {
51  NS_LOG_FUNCTION(this);
52  return m_bitMapSize * 8;
53 }
54 
55 void
57 {
58  NS_LOG_FUNCTION(this << winSize);
59 
60  NS_ASSERT_MSG(winSize % 8 == 0, "The packet window size should be a multiple of 8");
61  m_bitMapSize = winSize / 8;
62  if (m_receiveBitMap != nullptr)
63  {
64  delete[] m_receiveBitMap;
65  }
66  m_receiveBitMap = new uint8_t[m_bitMapSize]();
67  memset(m_receiveBitMap, 0xFF, m_bitMapSize);
68 }
69 
70 uint32_t
72 {
73  NS_LOG_FUNCTION(this);
74  return m_lost;
75 }
76 
77 bool
78 PacketLossCounter::GetBit(uint32_t seqNum)
79 {
80  NS_LOG_FUNCTION(this << seqNum);
81  return ((m_receiveBitMap[(seqNum % (m_bitMapSize * 8)) / 8] >> (7 - (seqNum % 8))) & 0x01);
82 }
83 
84 void
85 PacketLossCounter::SetBit(uint32_t seqNum, bool val)
86 {
87  NS_LOG_FUNCTION(this << seqNum << val);
88  if (val)
89  {
90  m_receiveBitMap[(seqNum % (m_bitMapSize * 8)) / 8] |= 0x80 >> (seqNum % 8);
91  }
92  else
93  {
94  m_receiveBitMap[(seqNum % (m_bitMapSize * 8)) / 8] &= ~(0x80 >> (seqNum % 8));
95  }
96 }
97 
98 /*
99  * This algo works as follows:
100  * When a packet is received:
101  * 1) From the last received packet to the current one:
102  * 1.1) check the corresponding bit in the bitMAP.
103  * This bit indicates if the packet with (SeqNum-bitMapSizeInBit) is
104  * received (1) or not (0)
105  * 1.2) Mark the packet as lost (0) in the bitMap
106  * 2) Mark the current packet as received (1) in the bitMap
107  * 3) Update the value of the last received packet
108  */
109 
110 void
112 {
113  NS_LOG_FUNCTION(this << seqNum);
114  for (uint32_t i = m_lastMaxSeqNum + 1; i <= seqNum; i++)
115  {
116  if (!GetBit(i))
117  {
118  NS_LOG_INFO("Packet lost: " << i - (m_bitMapSize * 8));
119  m_lost++;
120  }
121  SetBit(i, false);
122  }
123  SetBit(seqNum, true);
124  if (seqNum > m_lastMaxSeqNum)
125  {
126  m_lastMaxSeqNum = seqNum;
127  }
128 }
129 
130 } // 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:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:275
Every class exported by the ns3 library is enclosed in the ns3 namespace.