src/dsr/model/dsr-maintain-buff.h
changeset 8751 efad81f3cb47
child 8752 2da1fab73114
equal deleted inserted replaced
8750:b3db7d51f260 8751:efad81f3cb47
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2011 Yufei Cheng
       
     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: Yufei Cheng   <yfcheng@ittc.ku.edu>
       
    19  *
       
    20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
       
    21  * ResiliNets Research Group  http://wiki.ittc.ku.edu/resilinets
       
    22  * Information and Telecommunication Technology Center (ITTC)
       
    23  * and Department of Electrical Engineering and Computer Science
       
    24  * The University of Kansas Lawrence, KS USA.
       
    25  *
       
    26  * Work supported in part by NSF FIND (Future Internet Design) Program
       
    27  * under grant CNS-0626918 (Postmodern Internet Architecture),
       
    28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
       
    29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
       
    30  */
       
    31 
       
    32 #ifndef DSR_MAINTAIN_BUFF_H
       
    33 #define DSR_MAINTAIN_BUFF_H
       
    34 
       
    35 #include <vector>
       
    36 #include "ns3/ipv4-routing-protocol.h"
       
    37 #include "ns3/simulator.h"
       
    38 #include "ns3/ipv4-header.h"
       
    39 #include "dsr-option-header.h"
       
    40 
       
    41 
       
    42 namespace ns3 {
       
    43 namespace dsr {
       
    44 /*
       
    45  * The maintenance buffer is responsible for maintaining packet next hop delivery
       
    46  * The data packet is saved in maintenance buffer whenever the data packet is sent out of send buffer
       
    47  */
       
    48 
       
    49 /*
       
    50  * The packet timer key for controlling data packet retransmission
       
    51  */
       
    52 struct PacketKey
       
    53 {
       
    54   uint16_t m_ackId;
       
    55   Ipv4Address m_ourAdd;
       
    56   Ipv4Address m_nextHop;
       
    57   Ipv4Address m_source;
       
    58   Ipv4Address m_destination;
       
    59   uint8_t m_segsLeft;
       
    60 
       
    61   /**
       
    62    * Compare maintain Buffer entries
       
    63    * \return true if equal
       
    64    */
       
    65   bool operator < (PacketKey const & o) const
       
    66   {
       
    67     return ((m_ackId < o.m_ackId) && (m_ourAdd < o.m_ourAdd) && (m_nextHop < o.m_nextHop) && (m_source < o.m_source)
       
    68             && (m_destination < o.m_destination) && (m_segsLeft < o.m_segsLeft)
       
    69             );
       
    70   }
       
    71 };
       
    72 /**
       
    73  * \ingroup dsr
       
    74  * \brief DSR Maintain Buffer Entry
       
    75  */
       
    76 class MaintainBuffEntry
       
    77 {
       
    78 public:
       
    79   // / c-tor
       
    80   MaintainBuffEntry (Ptr<const Packet> pa = 0, Ipv4Address us = Ipv4Address (),
       
    81                      Ipv4Address n = Ipv4Address (), Ipv4Address s = Ipv4Address (), Ipv4Address dst = Ipv4Address (),
       
    82                      uint16_t ackId = 0, uint8_t segs = 0, Time exp = Simulator::Now ())
       
    83     : m_packet (pa),
       
    84       m_ourAdd (us),
       
    85       m_nextHop (n),
       
    86       m_src (s),
       
    87       m_dst (dst),
       
    88       m_ackId (ackId),
       
    89       m_segsLeft (segs),
       
    90       m_isPassive (true),
       
    91       m_expire (exp + Simulator::Now ())
       
    92   {
       
    93   }
       
    94 
       
    95   // /\name Fields
       
    96   // \{
       
    97   Ptr<const Packet> GetPacket () const
       
    98   {
       
    99     return m_packet;
       
   100   }
       
   101   void SetPacket (Ptr<const Packet> p)
       
   102   {
       
   103     m_packet = p;
       
   104   }
       
   105   Ipv4Address GetOurAdd () const
       
   106   {
       
   107     return m_ourAdd;
       
   108   }
       
   109   void SetOurAdd (Ipv4Address us)
       
   110   {
       
   111     m_ourAdd = us;
       
   112   }
       
   113   Ipv4Address GetNextHop () const
       
   114   {
       
   115     return m_nextHop;
       
   116   }
       
   117   void SetNextHop (Ipv4Address n)
       
   118   {
       
   119     m_nextHop = n;
       
   120   }
       
   121   Ipv4Address GetDst () const
       
   122   {
       
   123     return m_dst;
       
   124   }
       
   125   void SetDst (Ipv4Address n)
       
   126   {
       
   127     m_dst = n;
       
   128   }
       
   129   Ipv4Address GetSrc () const
       
   130   {
       
   131     return m_src;
       
   132   }
       
   133   void SetSrc (Ipv4Address s)
       
   134   {
       
   135     m_src = s;
       
   136   }
       
   137   uint16_t GetAckId () const
       
   138   {
       
   139     return m_ackId;
       
   140   }
       
   141   void SetAckId (uint16_t ackId)
       
   142   {
       
   143     m_ackId = ackId;
       
   144   }
       
   145   uint8_t GetSegsLeft () const
       
   146   {
       
   147     return m_segsLeft;
       
   148   }
       
   149   void SetSegsLeft (uint8_t segs)
       
   150   {
       
   151     m_segsLeft = segs;
       
   152   }
       
   153   bool GetPassive () const
       
   154   {
       
   155     return m_isPassive;
       
   156   }
       
   157   void SetPassive (bool pa)
       
   158   {
       
   159     m_isPassive = pa;
       
   160   }
       
   161   void SetExpireTime (Time exp)
       
   162   {
       
   163     m_expire = exp + Simulator::Now ();
       
   164   }
       
   165   Time GetExpireTime () const
       
   166   {
       
   167     return m_expire - Simulator::Now ();
       
   168   }
       
   169   // \}
       
   170 private:
       
   171   // / Data packet
       
   172   Ptr<const Packet> m_packet;
       
   173   // / our own ip address
       
   174   Ipv4Address m_ourAdd;
       
   175   // / Next hop Ip address
       
   176   Ipv4Address m_nextHop;
       
   177   // / The source address
       
   178   Ipv4Address m_src;
       
   179   // / The destination address
       
   180   Ipv4Address m_dst;
       
   181   // / The data ack id
       
   182   uint16_t m_ackId;
       
   183   // / The ipv4 id
       
   184   uint16_t m_id;
       
   185   // / The segments left field
       
   186   uint8_t m_segsLeft;
       
   187   // / The fragment offset of ipv4 header
       
   188   uint16_t m_fragment;
       
   189   // / if the ack metric is passive ack or not
       
   190   bool m_isPassive;
       
   191   // / Expire time for queue entry
       
   192   Time m_expire;
       
   193 };
       
   194 /**
       
   195  * \ingroup dsr
       
   196  * \brief DSR maintain buffer
       
   197  */
       
   198 /************************************************************************************************************************/
       
   199 class MaintainBuffer
       
   200 {
       
   201 public:
       
   202   // / Default c-tor
       
   203   MaintainBuffer ()
       
   204   {
       
   205   }
       
   206   // / Push entry in queue, if there is no entry with the same packet and destination address in queue.
       
   207   bool Enqueue (MaintainBuffEntry & entry);
       
   208   // / Return first found (the earliest) entry for given destination
       
   209   bool Dequeue (Ipv4Address dst, MaintainBuffEntry & entry);
       
   210   // / Remove all packets with destination IP address dst
       
   211   void DropPacketWithNextHop (Ipv4Address nextHop);
       
   212   // / Finds whether a packet with destination dst exists in the queue
       
   213   bool Find (Ipv4Address nextHop);
       
   214   // / Number of entries
       
   215   uint32_t GetSize ();
       
   216   // /\name Fields
       
   217   // \{
       
   218   uint32_t GetMaxQueueLen () const
       
   219   {
       
   220     return m_maxLen;
       
   221   }
       
   222   void SetMaxQueueLen (uint32_t len)
       
   223   {
       
   224     m_maxLen = len;
       
   225   }
       
   226   Time GetMaintainBufferTimeout () const
       
   227   {
       
   228     return m_maintainBufferTimeout;
       
   229   }
       
   230   void SetMaintainBufferTimeout (Time t)
       
   231   {
       
   232     m_maintainBufferTimeout = t;
       
   233   }
       
   234   // / Verify if the maintain buffer entry is the same in every field for network ack
       
   235   bool AllEqual (MaintainBuffEntry & entry);
       
   236   // / Verify if the maintain buffer entry is the same in every field for promiscuous ack
       
   237   bool PromiscEqual (MaintainBuffEntry & entry);
       
   238   // \}
       
   239 
       
   240 private:
       
   241   // / The vector of maintain buffer entries
       
   242   std::vector<MaintainBuffEntry> m_maintainBuffer;
       
   243   // / Remove all expired entries
       
   244   void Purge ();
       
   245   // / The maximum number of packets that we allow a routing protocol to buffer.
       
   246   uint32_t m_maxLen;
       
   247   // / The maximum period of time that a routing protocol is allowed to buffer a packet for, seconds.
       
   248   Time m_maintainBufferTimeout;
       
   249   // / Verify if the maintain buffer is equal or not
       
   250   static bool IsEqual (MaintainBuffEntry en, const Ipv4Address nextHop)
       
   251   {
       
   252     return (en.GetNextHop () == nextHop);
       
   253   }
       
   254 };
       
   255 /*******************************************************************************************************************************/
       
   256 } // namespace dsr
       
   257 } // namespace ns3
       
   258 #endif /* DSR_MAINTAIN_BUFF_H */