blob: 1e0f04fb5ae099d1a63a9f5c6f7acd8ec98cf105 [file] [log] [blame]
Alexander Afanasyev78057c32012-07-06 15:18:46 -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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-fib-impl.h"
Alexander Afanasyev78057c32012-07-06 15:18:46 -070022
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070023#include "ns3/ndn-face.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080024#include "ns3/ndn-interest.h"
Alexander Afanasyevadcccf42012-11-26 23:55:34 -080025#include "ns3/ndn-forwarding-strategy.h"
Alexander Afanasyev78057c32012-07-06 15:18:46 -070026
27#include "ns3/node.h"
28#include "ns3/assert.h"
29#include "ns3/names.h"
30#include "ns3/log.h"
31
32#include <boost/ref.hpp>
33#include <boost/lambda/lambda.hpp>
34#include <boost/lambda/bind.hpp>
35namespace ll = boost::lambda;
36
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037NS_LOG_COMPONENT_DEFINE ("ndn.fib.FibImpl");
Alexander Afanasyev78057c32012-07-06 15:18:46 -070038
39namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040namespace ndn {
41namespace fib {
Alexander Afanasyev78057c32012-07-06 15:18:46 -070042
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043NS_OBJECT_ENSURE_REGISTERED (FibImpl);
Alexander Afanasyev78057c32012-07-06 15:18:46 -070044
45TypeId
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046FibImpl::GetTypeId (void)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070047{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048 static TypeId tid = TypeId ("ns3::ndn::fib::Default") // cheating ns3 object system
49 .SetParent<Fib> ()
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070050 .SetGroupName ("Ndn")
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070051 .AddConstructor<FibImpl> ()
Alexander Afanasyev78057c32012-07-06 15:18:46 -070052 ;
53 return tid;
54}
55
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070056FibImpl::FibImpl ()
Alexander Afanasyev78057c32012-07-06 15:18:46 -070057{
58}
59
60void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070061FibImpl::NotifyNewAggregate ()
Alexander Afanasyev78057c32012-07-06 15:18:46 -070062{
63 Object::NotifyNewAggregate ();
64}
65
66void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070067FibImpl::DoDispose (void)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070068{
69 clear ();
70 Object::DoDispose ();
71}
72
73
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074Ptr<Entry>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070075FibImpl::LongestPrefixMatch (const Interest &interest)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070076{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070077 super::iterator item = super::longest_prefix_match (interest.GetName ());
Alexander Afanasyev78057c32012-07-06 15:18:46 -070078 // @todo use predicate to search with exclude filters
79
80 if (item == super::end ())
81 return 0;
82 else
83 return item->payload ();
84}
85
86
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087Ptr<Entry>
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070088FibImpl::Add (const Name &prefix, Ptr<Face> face, int32_t metric)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070089{
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070090 return Add (Create<Name> (prefix), face, metric);
Alexander Afanasyev78057c32012-07-06 15:18:46 -070091}
92
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070093Ptr<Entry>
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070094FibImpl::Add (const Ptr<const Name> &prefix, Ptr<Face> face, int32_t metric)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070095{
Alexander Afanasyev1ba09b82012-07-09 09:16:14 -070096 NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix) << boost::cref(*face) << metric);
Alexander Afanasyev78057c32012-07-06 15:18:46 -070097
98 // will add entry if doesn't exists, or just return an iterator to the existing entry
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070099 std::pair< super::iterator, bool > result = super::insert (*prefix, 0);
100 if (result.first != super::end ())
101 {
102 if (result.second)
103 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700104 Ptr<EntryImpl> newEntry = Create<EntryImpl> (prefix);
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700105 newEntry->SetTrie (result.first);
106 result.first->set_payload (newEntry);
107 }
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700108
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700109 super::modify (result.first,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110 ll::bind (&Entry::AddOrUpdateRoutingMetric, ll::_1, face, metric));
Alexander Afanasyevadcccf42012-11-26 23:55:34 -0800111
112 if (result.second)
113 {
114 // notify forwarding strategy about new FIB entry
115 NS_ASSERT (this->GetObject<ForwardingStrategy> () != 0);
116 this->GetObject<ForwardingStrategy> ()->DidAddFibEntry (result.first->payload ());
117 }
118
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700119 return result.first->payload ();
120 }
121 else
122 return 0;
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700123}
124
125void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700126FibImpl::Remove (const Ptr<const Name> &prefix)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700127{
128 NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix));
129
Alexander Afanasyevadcccf42012-11-26 23:55:34 -0800130 super::iterator fibEntry = super::find_exact (*prefix);
131 if (fibEntry != super::end ())
132 {
133 // notify forwarding strategy about soon be removed FIB entry
134 NS_ASSERT (this->GetObject<ForwardingStrategy> () != 0);
135 this->GetObject<ForwardingStrategy> ()->WillRemoveFibEntry (fibEntry->payload ());
136
137 super::erase (fibEntry);
138 }
139 // else do nothing
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700140}
141
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700142// void
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700143// FibImpl::Invalidate (const Ptr<const Name> &prefix)
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700144// {
145// NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId () << boost::cref(*prefix));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700146
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700147// super::iterator foundItem, lastItem;
148// bool reachLast;
149// boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (*prefix);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700150
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700151// if (!reachLast || lastItem->payload () == 0)
152// return; // nothing to invalidate
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700153
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700154// super::modify (lastItem,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700155// ll::bind (&Entry::Invalidate, ll::_1));
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700156// }
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700157
158void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700159FibImpl::InvalidateAll ()
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700160{
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700161 NS_LOG_FUNCTION (this->GetObject<Node> ()->GetId ());
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700162
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700163 super::parent_trie::recursive_iterator item (super::getTrie ());
164 super::parent_trie::recursive_iterator end (0);
165 for (; item != end; item++)
166 {
167 if (item->payload () == 0) continue;
168
169 super::modify (&(*item),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700170 ll::bind (&Entry::Invalidate, ll::_1));
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700171 }
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700172}
173
174void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700175FibImpl::RemoveFace (super::parent_trie &item, Ptr<Face> face)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700176{
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700177 if (item.payload () == 0) return;
178 NS_LOG_FUNCTION (this);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700179
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700180 super::modify (&item,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700181 ll::bind (&Entry::RemoveFace, ll::_1, face));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700182}
183
184void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185FibImpl::RemoveFromAll (Ptr<Face> face)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700186{
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700187 NS_LOG_FUNCTION (this);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700188
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700189 std::for_each (super::parent_trie::recursive_iterator (super::getTrie ()),
190 super::parent_trie::recursive_iterator (0),
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700191 ll::bind (&FibImpl::RemoveFace,
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700192 this, ll::_1, face));
193
194 super::parent_trie::recursive_iterator trieNode (super::getTrie ());
195 super::parent_trie::recursive_iterator end (0);
196 for (; trieNode != end; trieNode++)
197 {
198 if (trieNode->payload () == 0) continue;
199
200 if (trieNode->payload ()->m_faces.size () == 0)
201 {
Alexander Afanasyevadcccf42012-11-26 23:55:34 -0800202 // notify forwarding strategy about soon be removed FIB entry
203 NS_ASSERT (this->GetObject<ForwardingStrategy> () != 0);
204 this->GetObject<ForwardingStrategy> ()->WillRemoveFibEntry (trieNode->payload ());
205
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700206 trieNode = super::parent_trie::recursive_iterator (trieNode->erase ());
207 }
208 }
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700209}
210
211void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700212FibImpl::Print (std::ostream &os) const
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700213{
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700214 // !!! unordered_set imposes "random" order of item in the same level !!!
215 super::parent_trie::const_recursive_iterator item (super::getTrie ());
216 super::parent_trie::const_recursive_iterator end (0);
217 for (; item != end; item++)
218 {
219 if (item->payload () == 0) continue;
220
221 os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n";
222 }
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700223}
224
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700225uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700226FibImpl::GetSize () const
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700227{
228 return super::getPolicy ().size ();
229}
230
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700231Ptr<const Entry>
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700232FibImpl::Begin () const
Alexander Afanasyev95a4fa32012-07-09 15:23:59 -0700233{
234 super::parent_trie::const_recursive_iterator item (super::getTrie ());
235 super::parent_trie::const_recursive_iterator end (0);
236 for (; item != end; item++)
237 {
238 if (item->payload () == 0) continue;
239 break;
240 }
241
242 if (item == end)
243 return End ();
244 else
245 return item->payload ();
246}
247
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700248Ptr<const Entry>
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700249FibImpl::End () const
Alexander Afanasyev95a4fa32012-07-09 15:23:59 -0700250{
251 return 0;
252}
253
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700254Ptr<const Entry>
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700255FibImpl::Next (Ptr<const Entry> from) const
Alexander Afanasyev95a4fa32012-07-09 15:23:59 -0700256{
257 if (from == 0) return 0;
258
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700259 super::parent_trie::const_recursive_iterator item (*StaticCast<const EntryImpl> (from)->to_iterator ());
Alexander Afanasyev95a4fa32012-07-09 15:23:59 -0700260 super::parent_trie::const_recursive_iterator end (0);
261 for (item++; item != end; item++)
262 {
263 if (item->payload () == 0) continue;
264 break;
265 }
266
267 if (item == end)
268 return End ();
269 else
270 return item->payload ();
271}
272
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700273Ptr<Entry>
274FibImpl::Begin ()
275{
276 super::parent_trie::recursive_iterator item (super::getTrie ());
277 super::parent_trie::recursive_iterator end (0);
278 for (; item != end; item++)
279 {
280 if (item->payload () == 0) continue;
281 break;
282 }
283
284 if (item == end)
285 return End ();
286 else
287 return item->payload ();
288}
289
290Ptr<Entry>
291FibImpl::End ()
292{
293 return 0;
294}
295
296Ptr<Entry>
297FibImpl::Next (Ptr<Entry> from)
298{
299 if (from == 0) return 0;
300
301 super::parent_trie::recursive_iterator item (*StaticCast<EntryImpl> (from)->to_iterator ());
302 super::parent_trie::recursive_iterator end (0);
303 for (item++; item != end; item++)
304 {
305 if (item->payload () == 0) continue;
306 break;
307 }
308
309 if (item == end)
310 return End ();
311 else
312 return item->payload ();
313}
314
315
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700316} // namespace fib
317} // namespace ndn
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700318} // namespace ns3