blob: 89ce352db17e9692e133729d2dc997cb7d53718d [file] [log] [blame]
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
Alexander Afanasyev042b4a72012-11-09 17:47:48 -08003 * Copyright (c) 2012 University of California, Los Angeles
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -07004 *
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21
Alexander Afanasyev042b4a72012-11-09 17:47:48 -080022#ifndef NDNSIM_PER_FIB_LIMITS_H
23#define NDNSIM_PER_FIB_LIMITS_H
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070024
25#include "ns3/event-id.h"
Alexander Afanasyevdb15acb2012-11-09 14:16:27 -080026#include "ns3/log.h"
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -070027#include "ns3/ndn-pit.h"
28#include "ns3/ndn-pit-entry.h"
Alexander Afanasyev0484e772012-10-29 11:02:08 -070029#include "ns3/simulator.h"
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070030#include "ns3/string.h"
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -070031
32#include "ns3/ndn-forwarding-strategy.h"
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070033
Alexander Afanasyev6f95e702012-10-31 16:27:31 -070034#include "ns3/ndn-limits.h"
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070035
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070036namespace ns3 {
37namespace ndn {
38namespace fw {
39
40/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070041 * @ingroup ndn-fw
42 * @brief Strategy implementing per-FIB entry limits
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070043 */
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -070044template<class Parent>
Alexander Afanasyev042b4a72012-11-09 17:47:48 -080045class PerFibLimits :
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -070046 public Parent
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070047{
48private:
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -070049 typedef Parent super;
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070050
51public:
52 static TypeId
53 GetTypeId ();
54
55 /**
Alexander Afanasyev042b4a72012-11-09 17:47:48 -080056 * @brief Helper function to retrieve logging name for the forwarding strategy
57 */
58 static std::string
59 GetLogName ();
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080060
Alexander Afanasyev042b4a72012-11-09 17:47:48 -080061 /**
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070062 * @brief Default constructor
63 */
Alexander Afanasyev042b4a72012-11-09 17:47:48 -080064 PerFibLimits ()
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070065 { }
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080066
Alexander Afanasyev042b4a72012-11-09 17:47:48 -080067 /// \copydoc ForwardingStrategy::WillEraseTimedOutPendingInterest
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070068 virtual void
69 WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry);
70
Alexander Afanasyev042b4a72012-11-09 17:47:48 -080071 /// \copydoc ForwardingStrategy::AddFace
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070072 virtual void
73 AddFace (Ptr<Face> face)
74 {
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070075 super::AddFace (face);
Alexander Afanasyev042b4a72012-11-09 17:47:48 -080076
77 if (face->GetObject<Limits> () == 0)
78 {
79 NS_FATAL_ERROR ("At least per-face limits should be enabled");
80 exit (1);
81 }
Alexander Afanasyevf5c07742012-10-31 13:13:05 -070082 }
Alexander Afanasyevadcccf42012-11-26 23:55:34 -080083
84 /// \copydoc ForwardingStrategy::DidAddFibEntry
85 virtual void
86 DidAddFibEntry (Ptr<fib::Entry> fibEntry)
87 {
88 ObjectFactory factory;
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080089 factory.SetTypeId (fibEntry->m_faces.begin ()->GetFace ()->GetObject<Limits> ()->GetInstanceTypeId ());
90
Alexander Afanasyevadcccf42012-11-26 23:55:34 -080091 Ptr<Limits> limits = factory.template Create<Limits> ();
92 fibEntry->AggregateObject (limits);
93
94 super::DidAddFibEntry (fibEntry);
95 }
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080096
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070097protected:
Alexander Afanasyev042b4a72012-11-09 17:47:48 -080098 /// \copydoc ForwardingStrategy::CanSendOutInterest
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -070099 virtual bool
Alexander Afanasyevdb15acb2012-11-09 14:16:27 -0800100 CanSendOutInterest (Ptr<Face> inFace,
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -0700101 Ptr<Face> outFace,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700102 Ptr<const Interest> interest,
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -0700103 Ptr<pit::Entry> pitEntry);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800104
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800105 /// \copydoc ForwardingStrategy::WillSatisfyPendingInterest
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -0700106 virtual void
107 WillSatisfyPendingInterest (Ptr<Face> inFace,
108 Ptr<pit::Entry> pitEntry);
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -0700109
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800110protected:
111 static LogComponent g_log; ///< @brief Logging variable
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800112
Alexander Afanasyev6f95e702012-10-31 16:27:31 -0700113private:
114 std::string m_limitType;
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -0700115};
116
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700117template<class Parent>
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800118LogComponent PerFibLimits<Parent>::g_log = LogComponent (PerFibLimits<Parent>::GetLogName ().c_str ());
119
120template<class Parent>
121std::string
122PerFibLimits<Parent>::GetLogName ()
123{
124 return super::GetLogName ()+".PerFibLimits";
125}
Alexander Afanasyevdb15acb2012-11-09 14:16:27 -0800126
127template<class Parent>
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700128TypeId
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800129PerFibLimits<Parent>::GetTypeId (void)
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700130{
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800131 static TypeId tid = TypeId ((super::GetTypeId ().GetName ()+"::PerFibLimits").c_str ())
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700132 .SetGroupName ("Ndn")
133 .template SetParent <super> ()
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800134 .template AddConstructor <PerFibLimits> ()
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700135 ;
136 return tid;
137}
138
139template<class Parent>
140bool
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800141PerFibLimits<Parent>::CanSendOutInterest (Ptr<Face> inFace,
Alexander Afanasyevdb15acb2012-11-09 14:16:27 -0800142 Ptr<Face> outFace,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700143 Ptr<const Interest> interest,
Alexander Afanasyevdb15acb2012-11-09 14:16:27 -0800144 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700145{
Alexander Afanasyevdb15acb2012-11-09 14:16:27 -0800146 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700147
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800148 Ptr<Limits> fibLimits = pitEntry->GetFibEntry ()->template GetObject<Limits> ();
149 // no checks for the limit here. the check should be somewhere elese
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800150
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800151 if (fibLimits->IsBelowLimit ())
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700152 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700153 if (super::CanSendOutInterest (inFace, outFace, interest, pitEntry))
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800154 {
155 fibLimits->BorrowLimit ();
156 return true;
157 }
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700158 }
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800159
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800160 return false;
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700161}
162
163template<class Parent>
164void
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800165PerFibLimits<Parent>::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry)
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700166{
Alexander Afanasyevdb15acb2012-11-09 14:16:27 -0800167 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700168
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800169 Ptr<Limits> fibLimits = pitEntry->GetFibEntry ()->template GetObject<Limits> ();
Alexander Afanasyev3749fd12013-05-15 16:05:33 -0700170
171 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
172 face != pitEntry->GetOutgoing ().end ();
173 face ++)
174 {
175 for (uint32_t i = 0; i <= face->m_retxCount; i++)
176 fibLimits->ReturnLimit ();
177 }
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700178
179 super::WillEraseTimedOutPendingInterest (pitEntry);
180}
181
182
183template<class Parent>
184void
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800185PerFibLimits<Parent>::WillSatisfyPendingInterest (Ptr<Face> inFace,
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700186 Ptr<pit::Entry> pitEntry)
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700187{
Alexander Afanasyevdb15acb2012-11-09 14:16:27 -0800188 NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700189
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800190 Ptr<Limits> fibLimits = pitEntry->GetFibEntry ()->template GetObject<Limits> ();
Alexander Afanasyev3749fd12013-05-15 16:05:33 -0700191
192 for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
193 face != pitEntry->GetOutgoing ().end ();
194 face ++)
195 {
196 for (uint32_t i = 0; i <= face->m_retxCount; i++)
197 fibLimits->ReturnLimit ();
198 }
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800199
Alexander Afanasyev0fff1db2012-10-29 10:14:47 -0700200 super::WillSatisfyPendingInterest (inFace, pitEntry);
201}
Alexander Afanasyev1e7bcaf2012-09-05 10:17:53 -0700202
203} // namespace fw
204} // namespace ndn
205} // namespace ns3
206
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800207#endif // NDNSIM_PER_FIB_LIMITS_H