blob: 93741d8aee4372d4b5d3810a504e990f024bec30 [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#ifndef NFD_TABLE_FIB_HPP
8#define NFD_TABLE_FIB_HPP
9
10#include "fib-entry.hpp"
HangZhangad4afd12014-03-01 11:03:08 +080011#include "name-tree.hpp"
Junxiao Shidbe71732014-02-21 22:23:28 -070012
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080013namespace nfd {
Junxiao Shic1e12362014-01-24 20:03:26 -070014
HangZhangad4afd12014-03-01 11:03:08 +080015namespace measurements {
16class Entry;
17}
18namespace pit {
19class Entry;
20}
21
Junxiao Shic1e12362014-01-24 20:03:26 -070022/** \class Fib
23 * \brief represents the FIB
24 */
25class Fib : noncopyable
26{
27public:
HangZhang5d469422014-03-12 09:26:26 +080028 class const_iterator;
29
HangZhangad4afd12014-03-01 11:03:08 +080030 explicit
31 Fib(NameTree& nameTree);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070032
Junxiao Shic1e12362014-01-24 20:03:26 -070033 ~Fib();
Steve DiBenedettod5f87932014-02-05 15:11:39 -070034
Junxiao Shic1e12362014-01-24 20:03:26 -070035 /** \brief inserts a FIB entry for prefix
36 * If an entry for exact same prefix exists, that entry is returned.
37 * \return{ the entry, and true for new entry, false for existing entry }
38 */
39 std::pair<shared_ptr<fib::Entry>, bool>
40 insert(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070041
Junxiao Shic1e12362014-01-24 20:03:26 -070042 /// performs a longest prefix match
43 shared_ptr<fib::Entry>
44 findLongestPrefixMatch(const Name& prefix) const;
Steve DiBenedettod5f87932014-02-05 15:11:39 -070045
Junxiao Shidbe71732014-02-21 22:23:28 -070046 /// performs a longest prefix match
47 shared_ptr<fib::Entry>
48 findLongestPrefixMatch(const pit::Entry& pitEntry) const;
49
50 /// performs a longest prefix match
51 shared_ptr<fib::Entry>
52 findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const;
53
Steve DiBenedettod5f87932014-02-05 15:11:39 -070054 shared_ptr<fib::Entry>
55 findExactMatch(const Name& prefix) const;
56
57 void
HangZhangad4afd12014-03-01 11:03:08 +080058 erase(const Name& prefix);
Steve DiBenedettod5f87932014-02-05 15:11:39 -070059
Steve DiBenedettod030cfc2014-03-10 20:04:47 -060060 void
61 erase(const fib::Entry& entry);
62
Junxiao Shic1e12362014-01-24 20:03:26 -070063 /** \brief removes the NextHop record for face in all entrites
64 * This is usually invoked when face goes away.
65 * Removing all NextHops in a FIB entry will not remove the FIB entry.
66 */
67 void
68 removeNextHopFromAllEntries(shared_ptr<Face> face);
69
HangZhangad4afd12014-03-01 11:03:08 +080070 size_t
71 size() const;
72
HangZhang5d469422014-03-12 09:26:26 +080073 const_iterator
74 begin() const;
75
76 const_iterator
77 end() const;
78
79 class const_iterator : public std::iterator<std::forward_iterator_tag, fib::Entry>
80 {
81 public:
82 explicit
83 const_iterator(const NameTree::const_iterator& it);
84
85 ~const_iterator();
86
87 const fib::Entry&
88 operator*() const;
89
90 shared_ptr<fib::Entry>
91 operator->() const;
92
93 const_iterator&
94 operator++();
95
96 const_iterator
97 operator++(int);
98
99 bool
100 operator==(const const_iterator& other) const;
101
102 bool
103 operator!=(const const_iterator& other) const;
104
105 private:
106 NameTree::const_iterator m_nameTreeIterator;
107 };
108
Junxiao Shic1e12362014-01-24 20:03:26 -0700109private:
HangZhangcb4fc832014-03-11 16:57:11 +0800110 shared_ptr<fib::Entry>
111 findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const;
112
Junxiao Shiefceadc2014-03-09 18:52:57 -0700113private:
HangZhangad4afd12014-03-01 11:03:08 +0800114 NameTree& m_nameTree;
Junxiao Shi40631842014-03-01 13:52:37 -0700115 size_t m_nItems;
Junxiao Shiefceadc2014-03-09 18:52:57 -0700116
Junxiao Shi40631842014-03-01 13:52:37 -0700117 /** \brief The empty FIB entry.
118 *
119 * This entry has no nexthops.
120 * It is returned by findLongestPrefixMatch if nothing is matched.
121 */
122 // Returning empty entry instead of nullptr makes forwarding and strategy implementation easier.
HangZhangcb4fc832014-03-11 16:57:11 +0800123 static const shared_ptr<fib::Entry> s_emptyEntry;
Junxiao Shic1e12362014-01-24 20:03:26 -0700124};
125
HangZhangad4afd12014-03-01 11:03:08 +0800126inline size_t
127Fib::size() const
128{
129 return m_nItems;
130}
131
HangZhang5d469422014-03-12 09:26:26 +0800132inline Fib::const_iterator
133Fib::end() const
134{
135 return const_iterator(m_nameTree.end());
136}
137
138inline
139Fib::const_iterator::const_iterator(const NameTree::const_iterator& it)
140 : m_nameTreeIterator(it)
141{
142}
143
144inline
145Fib::const_iterator::~const_iterator()
146{
147}
148
149inline
150Fib::const_iterator
151Fib::const_iterator::operator++(int)
152{
153 Fib::const_iterator temp(*this);
154 ++(*this);
155 return temp;
156}
157
158inline Fib::const_iterator&
159Fib::const_iterator::operator++()
160{
161 ++m_nameTreeIterator;
162 return *this;
163}
164
165inline const fib::Entry&
166Fib::const_iterator::operator*() const
167{
168 return *(m_nameTreeIterator->getFibEntry());
169}
170
171inline shared_ptr<fib::Entry>
172Fib::const_iterator::operator->() const
173{
174 return m_nameTreeIterator->getFibEntry();
175}
176
177inline bool
178Fib::const_iterator::operator==(const Fib::const_iterator& other) const
179{
180 return m_nameTreeIterator == other.m_nameTreeIterator;
181}
182
183inline bool
184Fib::const_iterator::operator!=(const Fib::const_iterator& other) const
185{
186 return m_nameTreeIterator != other.m_nameTreeIterator;
187}
188
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800189} // namespace nfd
Junxiao Shic1e12362014-01-24 20:03:26 -0700190
191#endif // NFD_TABLE_FIB_HPP