Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- 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" |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 11 | #include "name-tree.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 12 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 13 | namespace nfd { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 14 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 15 | namespace measurements { |
| 16 | class Entry; |
| 17 | } |
| 18 | namespace pit { |
| 19 | class Entry; |
| 20 | } |
| 21 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 22 | /** \class Fib |
| 23 | * \brief represents the FIB |
| 24 | */ |
| 25 | class Fib : noncopyable |
| 26 | { |
| 27 | public: |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 28 | class const_iterator; |
| 29 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 30 | explicit |
| 31 | Fib(NameTree& nameTree); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 33 | ~Fib(); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 34 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 35 | /** \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 DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 42 | /// performs a longest prefix match |
| 43 | shared_ptr<fib::Entry> |
| 44 | findLongestPrefixMatch(const Name& prefix) const; |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 45 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 46 | /// 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 DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 54 | shared_ptr<fib::Entry> |
| 55 | findExactMatch(const Name& prefix) const; |
| 56 | |
| 57 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 58 | erase(const Name& prefix); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 59 | |
Steve DiBenedetto | d030cfc | 2014-03-10 20:04:47 -0600 | [diff] [blame] | 60 | void |
| 61 | erase(const fib::Entry& entry); |
| 62 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 63 | /** \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 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 70 | size_t |
| 71 | size() const; |
| 72 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 73 | 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 Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 109 | private: |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 110 | shared_ptr<fib::Entry> |
| 111 | findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const; |
| 112 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 113 | private: |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 114 | NameTree& m_nameTree; |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 115 | size_t m_nItems; |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 117 | /** \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. |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 123 | static const shared_ptr<fib::Entry> s_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 126 | inline size_t |
| 127 | Fib::size() const |
| 128 | { |
| 129 | return m_nItems; |
| 130 | } |
| 131 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame] | 132 | inline Fib::const_iterator |
| 133 | Fib::end() const |
| 134 | { |
| 135 | return const_iterator(m_nameTree.end()); |
| 136 | } |
| 137 | |
| 138 | inline |
| 139 | Fib::const_iterator::const_iterator(const NameTree::const_iterator& it) |
| 140 | : m_nameTreeIterator(it) |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | inline |
| 145 | Fib::const_iterator::~const_iterator() |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | inline |
| 150 | Fib::const_iterator |
| 151 | Fib::const_iterator::operator++(int) |
| 152 | { |
| 153 | Fib::const_iterator temp(*this); |
| 154 | ++(*this); |
| 155 | return temp; |
| 156 | } |
| 157 | |
| 158 | inline Fib::const_iterator& |
| 159 | Fib::const_iterator::operator++() |
| 160 | { |
| 161 | ++m_nameTreeIterator; |
| 162 | return *this; |
| 163 | } |
| 164 | |
| 165 | inline const fib::Entry& |
| 166 | Fib::const_iterator::operator*() const |
| 167 | { |
| 168 | return *(m_nameTreeIterator->getFibEntry()); |
| 169 | } |
| 170 | |
| 171 | inline shared_ptr<fib::Entry> |
| 172 | Fib::const_iterator::operator->() const |
| 173 | { |
| 174 | return m_nameTreeIterator->getFibEntry(); |
| 175 | } |
| 176 | |
| 177 | inline bool |
| 178 | Fib::const_iterator::operator==(const Fib::const_iterator& other) const |
| 179 | { |
| 180 | return m_nameTreeIterator == other.m_nameTreeIterator; |
| 181 | } |
| 182 | |
| 183 | inline bool |
| 184 | Fib::const_iterator::operator!=(const Fib::const_iterator& other) const |
| 185 | { |
| 186 | return m_nameTreeIterator != other.m_nameTreeIterator; |
| 187 | } |
| 188 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 189 | } // namespace nfd |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 190 | |
| 191 | #endif // NFD_TABLE_FIB_HPP |