blob: fb437a906c665d55d71d537127cf7199819860a4 [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 Afanasyeva98cdd22011-08-29 17:32:37 -070042
43namespace ns3 {
44
45class Ccnx;
46class CcnxFace;
47class CcnxContentObjectHeader;
48class CcnxInterestHeader;
49
50/**
51 * \ingroup ccnx
52 * \private
53 * \brief Private namespace for CCNx PIT implementation
54 */
55namespace __ccnx_private
56{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070057// class i_prefix{}; ///< tag for prefix hash
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070058class i_timestamp {}; ///< tag for timestamp-ordered records (for cleanup optimization)
59};
60
61/**
62 * \ingroup ccnx
63 * \brief Typedef for RIT container implemented as a Boost.MultiIndex container
64 *
65 * - First index (tag<i_prefix>) is a unique hash index based on
66 * prefixes
67 * - Second index (tag<i_timestamp>) is a sequenced index based on
68 * arrival order (for clean-up optimizations)
69 *
70 * \see http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/ for more information on Boost.MultiIndex library
71 */
72struct CcnxPitEntryContainer
73{
74 typedef
75 boost::multi_index::multi_index_container<
76 CcnxPitEntry,
77 boost::multi_index::indexed_by<
78 // indexed by hash
79 boost::multi_index::hashed_unique<
80 boost::multi_index::tag<__ccnx_private::i_prefix>,
81 boost::multi_index::const_mem_fun<CcnxPitEntry, const CcnxNameComponents&, &CcnxPitEntry::GetPrefix>,
82 CcnxPrefixHash
83 >,
84 // sequenced to implement MRU
85 boost::multi_index::sequenced<
86 boost::multi_index::tag<__ccnx_private::i_timestamp> >
87 >
88 > type;
89};
90
91// typedef std::map<int,int> PitCounter;
92// typedef std::map<int,int>::iterator PitCounterIterator;
93
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -070094 typedef std::map<int,double> PitBucket;
95 typedef std::map<int,double>::iterator PitBucketIterator;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070096
97
98////////////////////////////////////////////////////////////////////////
99////////////////////////////////////////////////////////////////////////
100
101/**
102 * \ingroup ccnx
103 * \brief Class implementing Pending Interests Table
104 */
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700105class CcnxPit : public CcnxPitEntryContainer::type, public Object
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700106{
107public:
108 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700109 * \brief Interface ID
110 *
111 * \return interface ID
112 */
113 static TypeId GetTypeId ();
114
115 /**
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700116 * \brief PIT constructor
117 */
118 CcnxPit ();
119
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800120 /**
121 * \brief Destructor
122 */
123 virtual ~CcnxPit ();
124
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700125 /*CcnxPitEntryContainer::type::iterator
126 Add (const CcnxInterestHeader &header, CcnxFibEntryContainer::type::iterator fibEntry, Ptr<CcnxFace> face);*/
127
128 bool
129 TryAddOutgoing(CcnxPitEntryContainer::type::iterator pitEntry, Ptr<CcnxFace> face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700130 /**
131 * \brief Find corresponding PIT entry for the given content name
132 * \param prefix Prefix for which to lookup the entry
133 * \returns const reference to Pit entry. If record not found,
134 * CcnxPitEntryNotFound exception will be thrown
135 */
136 const CcnxPitEntry&
137 Lookup (const CcnxContentObjectHeader &header) const;
138
139 /**
140 * \brief Find corresponding PIT entry for the given content name
141 * \param prefix Prefix for which to lookup the entry
142 * \returns const reference to Pit entry. If record does not exist, it will be created
143 */
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700144 CcnxPitEntryContainer::type::iterator
145 Lookup (const CcnxInterestHeader &header,CcnxFibEntryContainer::type::iterator &outFibEntry);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700146
147 // remove a PIT entry
148 //void erase (const string &contentName);
149
150 // Reset pending state in outgoing interests
151 // void resetPendingState( PitEntry &pe );
152
153 // // Check if there are any interfaces that we haven't sent data to yet
154 // bool areFreeInterfaces( PitEntry &pe, int interface );
155
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700156 // Periodically generate pre-calculated number of tokens (leak buckets)
157 void LeakBuckets( );
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700158
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700159 // Selectively leak a bucket
160 void LeakBucket (Ptr<CcnxFace> face, int amount);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700161
162 /**
163 * \brief Set cleanup timeout
164 *
165 * Side effect: current clean up even (if any) will be cancelled and a new event started
166 *
167 * \param timeout cleanup timeout
168 */
169 void SetCleanupTimeout (const Time &timeout);
170
171 /**
172 * \brief Get cleanup timeout
173 *
174 * \returns cleanup timeout
175 */
176 Time GetCleanupTimeout () const;
177
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700178 /**
179 * \brief Set FIB table
180 */
181 void SetFib (Ptr<CcnxFib> fib);
182
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700183public:
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700184 PitBucket maxBucketsPerFace; // maximum number of buckets. Automatically computed based on link capacity
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700185 // // averaging over 1 second (bandwidth * 1second)
Ilya Moiseenko1c73cb52011-10-28 13:13:11 -0700186 PitBucket leakSize; // size of a periodic bucket leak
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700187
188private:
189 /** \brief Remove expired records from PIT */
190 void CleanExpired ();
191
192 friend std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
193
194private:
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700195 Time m_cleanupTimeout; ///< \brief Configurable timeout of how often cleanup events are working
196 EventId m_cleanupEvent; ///< \brief Cleanup event
197
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700198 Ptr<CcnxFib> m_fib; ///< \brief Link to FIB table
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800199 PitBucket m_bucketsPerFace; ///< \brief pending interface counter per face
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700200};
201
202///////////////////////////////////////////////////////////////////////////////
203///////////////////////////////////////////////////////////////////////////////
204
205std::ostream& operator<< (std::ostream& os, const CcnxPit &fib);
206std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry);
207// std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
208
209class CcnxPitEntryNotFound {};
210
211} // namespace ns3
212
213#endif /* CCNX_PIT_H */