blob: 538e1270c1ff86ee96677c4ba98e15131c44954e [file] [log] [blame]
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#ifndef _CCNX_PIT_H_
22#define _CCNX_PIT_H_
23
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070024#include "ns3/object.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070025#include "ns3/nstime.h"
26#include "ns3/event-id.h"
27
28#include "hash-helper.h"
29#include "ccnx-pit-entry.h"
30
31#include <boost/multi_index_container.hpp>
32#include <boost/multi_index/tag.hpp>
33#include <boost/multi_index/ordered_index.hpp>
34#include <boost/multi_index/composite_key.hpp>
35#include <boost/multi_index/hashed_index.hpp>
36#include <boost/multi_index/member.hpp>
37#include <boost/multi_index/mem_fun.hpp>
38#include <boost/multi_index/sequenced_index.hpp>
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -070039#include <map>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070040#include <iostream>
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -070041#include <algorithm>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080042#include <boost/tuple/tuple.hpp>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070043
44namespace ns3 {
45
46class Ccnx;
47class CcnxFace;
48class CcnxContentObjectHeader;
49class CcnxInterestHeader;
50
51/**
52 * \ingroup ccnx
53 * \private
54 * \brief Private namespace for CCNx PIT implementation
55 */
56namespace __ccnx_private
57{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070058// class i_prefix{}; ///< tag for prefix hash
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070059class i_timestamp {}; ///< tag for timestamp-ordered records (for cleanup optimization)
60};
61
62/**
63 * \ingroup ccnx
64 * \brief Typedef for RIT container implemented as a Boost.MultiIndex container
65 *
66 * - First index (tag<i_prefix>) is a unique hash index based on
67 * prefixes
68 * - Second index (tag<i_timestamp>) is a sequenced index based on
69 * arrival order (for clean-up optimizations)
70 *
71 * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library
72 */
73struct CcnxPitEntryContainer
74{
75 typedef
76 boost::multi_index::multi_index_container<
77 CcnxPitEntry,
78 boost::multi_index::indexed_by<
79 // indexed by hash
80 boost::multi_index::hashed_unique<
81 boost::multi_index::tag<__ccnx_private::i_prefix>,
82 boost::multi_index::const_mem_fun<CcnxPitEntry, const CcnxNameComponents&, &CcnxPitEntry::GetPrefix>,
83 CcnxPrefixHash
84 >,
85 // sequenced to implement MRU
86 boost::multi_index::sequenced<
87 boost::multi_index::tag<__ccnx_private::i_timestamp> >
88 >
89 > type;
90};
91
92// typedef std::map<int,int> PitCounter;
93// typedef std::map<int,int>::iterator PitCounterIterator;
94
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -070095 typedef std::map<int,double> PitBucket;
96 typedef std::map<int,double>::iterator PitBucketIterator;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070097
98
99////////////////////////////////////////////////////////////////////////
100////////////////////////////////////////////////////////////////////////
101
102/**
103 * \ingroup ccnx
104 * \brief Class implementing Pending Interests Table
105 */
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700106class CcnxPit : public CcnxPitEntryContainer::type, public Object
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700107{
108public:
109 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700110 * \brief Interface ID
111 *
112 * \return interface ID
113 */
114 static TypeId GetTypeId ();
115
116 /**
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700117 * \brief PIT constructor
118 */
119 CcnxPit ();
120
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800121 /**
122 * \brief Destructor
123 */
124 virtual ~CcnxPit ();
125
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700126 /*CcnxPitEntryContainer::type::iterator
127 Add (const CcnxInterestHeader &header, CcnxFibEntryContainer::type::iterator fibEntry, Ptr<CcnxFace> face);*/
128
129 bool
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800130 TryAddOutgoing(CcnxPitEntryContainer::type::iterator pitEntry, Ptr<CcnxFace> face);
131
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700132 /**
133 * \brief Find corresponding PIT entry for the given content name
134 * \param prefix Prefix for which to lookup the entry
135 * \returns const reference to Pit entry. If record not found,
136 * CcnxPitEntryNotFound exception will be thrown
137 */
138 const CcnxPitEntry&
139 Lookup (const CcnxContentObjectHeader &header) const;
140
141 /**
142 * \brief Find corresponding PIT entry for the given content name
143 * \param prefix Prefix for which to lookup the entry
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800144 * \returns a tuple:
145 * get<0>: `const CcnxPitEntry&`: a valid PIT entry (if record does not exist, it will be created)
146 * get<1>: `bool`: true if a new entry was created
147 * get<2>: `bool`: true if a PIT entry exists and Nonce that present in header has been already seen
148 *
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700149 */
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800150 boost::tuple<const CcnxPitEntry&, bool, bool>
151 Lookup (const CcnxInterestHeader &header);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700152
153 // remove a PIT entry
154 //void erase (const string &contentName);
155
156 // Reset pending state in outgoing interests
157 // void resetPendingState( PitEntry &pe );
158
159 // // Check if there are any interfaces that we haven't sent data to yet
160 // bool areFreeInterfaces( PitEntry &pe, int interface );
161
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700162 // Periodically generate pre-calculated number of tokens (leak buckets)
163 void LeakBuckets( );
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700164
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700165 // Selectively leak a bucket
166 void LeakBucket (Ptr<CcnxFace> face, int amount);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700167
168 /**
169 * \brief Set cleanup timeout
170 *
171 * Side effect: current clean up even (if any) will be cancelled and a new event started
172 *
173 * \param timeout cleanup timeout
174 */
175 void SetCleanupTimeout (const Time &timeout);
176
177 /**
178 * \brief Get cleanup timeout
179 *
180 * \returns cleanup timeout
181 */
182 Time GetCleanupTimeout () const;
183
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800184 Time GetPitEntryPruningTimeout () const
185 {
186 return m_PitEntryPruningTimout;
187 }
188
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700189 /**
190 * \brief Set FIB table
191 */
192 void SetFib (Ptr<CcnxFib> fib);
193
Alexander Afanasyev18252852011-11-21 13:35:31 -0800194protected:
195 // inherited from Object class
196 virtual void NotifyNewAggregate ();
197 virtual void DoDispose ();
198
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700199public:
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700200 PitBucket maxBucketsPerFace; // maximum number of buckets. Automatically computed based on link capacity
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700201 // // averaging over 1 second (bandwidth * 1second)
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700202 PitBucket leakSize; // size of a periodic bucket leak
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700203
204private:
205 /** \brief Remove expired records from PIT */
206 void CleanExpired ();
207
208 friend std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
209
210private:
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700211 Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working
212 EventId m_cleanupEvent; ///< \brief Cleanup event
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800213 Time m_PitEntryPruningTimout;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700214
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700215 Ptr<CcnxFib> m_fib; ///< \brief Link to FIB table
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800216 PitBucket m_bucketsPerFace; ///< \brief pending interface counter per face
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700217};
218
219///////////////////////////////////////////////////////////////////////////////
220///////////////////////////////////////////////////////////////////////////////
221
222std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
223std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry);
224// std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
225
226class CcnxPitEntryNotFound {};
227
228} // namespace ns3
229
230#endif /* CCNX_PIT_H */