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