blob: 940f60455401c4810434c08b10a8ecb55caf4874 [file] [log] [blame]
Junxiao Shic1e12362014-01-24 20:03:26 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "fib.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -07008#include "pit-entry.hpp"
9#include "measurements-entry.hpp"
Junxiao Shic1e12362014-01-24 20:03:26 -070010
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080011namespace nfd {
Junxiao Shi40631842014-03-01 13:52:37 -070012
HangZhangcb4fc832014-03-11 16:57:11 +080013const shared_ptr<fib::Entry> Fib::s_emptyEntry = make_shared<fib::Entry>(Name());
Junxiao Shi40631842014-03-01 13:52:37 -070014
HangZhangad4afd12014-03-01 11:03:08 +080015Fib::Fib(NameTree& nameTree)
16 : m_nameTree(nameTree)
17 , m_nItems(0)
Junxiao Shic1e12362014-01-24 20:03:26 -070018{
Junxiao Shic1e12362014-01-24 20:03:26 -070019}
20
21Fib::~Fib()
22{
23}
24
Junxiao Shi40631842014-03-01 13:52:37 -070025static inline bool
26predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& entry)
27{
28 return static_cast<bool>(entry.getFibEntry());
29}
30
Junxiao Shic1e12362014-01-24 20:03:26 -070031std::pair<shared_ptr<fib::Entry>, bool>
32Fib::insert(const Name& prefix)
33{
HangZhangad4afd12014-03-01 11:03:08 +080034 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
35 shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
36 if (static_cast<bool>(entry))
37 return std::make_pair(entry, false);
38 entry = make_shared<fib::Entry>(prefix);
39 nameTreeEntry->setFibEntry(entry);
Junxiao Shiefceadc2014-03-09 18:52:57 -070040 ++m_nItems;
Junxiao Shic1e12362014-01-24 20:03:26 -070041 return std::make_pair(entry, true);
42}
43
Junxiao Shic1e12362014-01-24 20:03:26 -070044shared_ptr<fib::Entry>
45Fib::findLongestPrefixMatch(const Name& prefix) const
46{
Junxiao Shi40631842014-03-01 13:52:37 -070047 shared_ptr<name_tree::Entry> nameTreeEntry =
48 m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry);
49 if (static_cast<bool>(nameTreeEntry)) {
50 return nameTreeEntry->getFibEntry();
HangZhangad4afd12014-03-01 11:03:08 +080051 }
HangZhangcb4fc832014-03-11 16:57:11 +080052 return s_emptyEntry;
53}
54
55shared_ptr<fib::Entry>
56Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const
57{
58 shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry();
59 if (static_cast<bool>(entry))
60 return entry;
61 nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry,
62 &predicate_NameTreeEntry_hasFibEntry);
63 if (static_cast<bool>(nameTreeEntry)) {
64 return nameTreeEntry->getFibEntry();
65 }
66 return s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -070067}
68
Steve DiBenedettod5f87932014-02-05 15:11:39 -070069shared_ptr<fib::Entry>
Junxiao Shidbe71732014-02-21 22:23:28 -070070Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const
71{
HangZhangcb4fc832014-03-11 16:57:11 +080072 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry);
73
74 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
75
76 return findLongestPrefixMatch(nameTreeEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -070077}
78
79shared_ptr<fib::Entry>
80Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
81{
HangZhangcb4fc832014-03-11 16:57:11 +080082 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry);
83
84 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
85
86 return findLongestPrefixMatch(nameTreeEntry);
Junxiao Shidbe71732014-02-21 22:23:28 -070087}
88
89shared_ptr<fib::Entry>
Steve DiBenedettod5f87932014-02-05 15:11:39 -070090Fib::findExactMatch(const Name& prefix) const
91{
HangZhangad4afd12014-03-01 11:03:08 +080092 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
93 if (static_cast<bool>(nameTreeEntry))
94 return nameTreeEntry->getFibEntry();
Steve DiBenedettod5f87932014-02-05 15:11:39 -070095 return shared_ptr<fib::Entry>();
96}
97
98void
HangZhangad4afd12014-03-01 11:03:08 +080099Fib::erase(const Name& prefix)
Steve DiBenedettod5f87932014-02-05 15:11:39 -0700100{
HangZhangad4afd12014-03-01 11:03:08 +0800101 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
102 if (static_cast<bool>(nameTreeEntry))
103 {
Junxiao Shiefceadc2014-03-09 18:52:57 -0700104 nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
105 --m_nItems;
106 }
107}
108
109void
110Fib::erase(const fib::Entry& entry)
111{
112 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry);
113 if (static_cast<bool>(nameTreeEntry))
114 {
115 nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>());
116 --m_nItems;
HangZhangad4afd12014-03-01 11:03:08 +0800117 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700118}
119
120void
121Fib::removeNextHopFromAllEntries(shared_ptr<Face> face)
122{
Junxiao Shiefceadc2014-03-09 18:52:57 -0700123 for (NameTree::const_iterator it = m_nameTree.fullEnumerate(
124 &predicate_NameTreeEntry_hasFibEntry); it != m_nameTree.end(); ++it) {
Haowei Yuane1079fc2014-03-08 14:41:25 -0600125 shared_ptr<fib::Entry> entry = it->getFibEntry();
Junxiao Shi40631842014-03-01 13:52:37 -0700126 entry->removeNextHop(face);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700127 if (!entry->hasNextHops()) {
128 this->erase(*entry);
129 }
HangZhangad4afd12014-03-01 11:03:08 +0800130 }
Junxiao Shic1e12362014-01-24 20:03:26 -0700131}
132
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800133} // namespace nfd