rib.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef NFD_DAEMON_RIB_RIB_HPP
27 #define NFD_DAEMON_RIB_RIB_HPP
28 
29 #include "rib-entry.hpp"
30 #include "rib-update-batch.hpp"
31 
32 #include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
33 
34 namespace nfd::rib {
35 
36 using ndn::nfd::ControlParameters;
37 
38 class FibUpdater;
39 
43 {
44  shared_ptr<RibEntry> entry;
46 };
47 
48 bool
49 operator<(const RibRouteRef& lhs, const RibRouteRef& rhs);
50 
61 class Rib : noncopyable
62 {
63 public:
64  using RibEntryList = std::list<shared_ptr<RibEntry>>;
65  using RibTable = std::map<Name, shared_ptr<RibEntry>>;
66  using const_iterator = RibTable::const_iterator;
67 
68  void
69  setFibUpdater(FibUpdater* updater);
70 
72  find(const Name& prefix) const;
73 
74  Route*
75  find(const Name& prefix, const Route& route) const;
76 
77  Route*
78  findLongestPrefix(const Name& prefix, const Route& route) const;
79 
81  begin() const
82  {
83  return m_rib.begin();
84  }
85 
87  end() const
88  {
89  return m_rib.end();
90  }
91 
92  size_t
93  size() const noexcept
94  {
95  return m_nItems;
96  }
97 
98  [[nodiscard]] bool
99  empty() const noexcept
100  {
101  return m_rib.empty();
102  }
103 
104  shared_ptr<RibEntry>
105  findParent(const Name& prefix) const;
106 
107 public:
108  using UpdateSuccessCallback = std::function<void()>;
109  using UpdateFailureCallback = std::function<void(uint32_t code, const std::string& error)>;
110 
119  void
120  beginApplyUpdate(const RibUpdate& update,
121  const UpdateSuccessCallback& onSuccess,
122  const UpdateFailureCallback& onFailure);
123 
126  void
127  beginRemoveFace(uint64_t faceId);
128 
129  void
130  beginRemoveFailedFaces(const std::set<uint64_t>& activeFaceIds);
131 
132  void
133  onRouteExpiration(const Name& prefix, const Route& route);
134 
135  void
136  insert(const Name& prefix, const Route& route);
137 
138 private:
139  void
140  enqueueRemoveFace(const RibEntry& entry, uint64_t faceId);
141 
146  void
147  addUpdateToQueue(const RibUpdate& update,
148  const Rib::UpdateSuccessCallback& onSuccess,
149  const Rib::UpdateFailureCallback& onFailure);
150 
153  void
154  sendBatchFromQueue();
155 
156  void
157  onFibUpdateSuccess(const RibUpdateBatch& batch,
158  const RibUpdateList& inheritedRoutes,
159  const Rib::UpdateSuccessCallback& onSuccess);
160 
161  void
162  onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure,
163  uint32_t code, const std::string& error);
164 
166  void
167  erase(const Name& prefix, const Route& route);
168 
169 private:
170  using RouteComparePredicate = bool (*)(const Route&, const Route&);
171  using RouteSet = std::set<Route, RouteComparePredicate>;
172 
176  std::list<shared_ptr<RibEntry>>
177  findDescendants(const Name& prefix) const;
178 
182  std::list<shared_ptr<RibEntry>>
183  findDescendantsForNonInsertedName(const Name& prefix) const;
184 
185  RibTable::iterator
186  eraseEntry(RibTable::iterator it);
187 
188  void
189  updateRib(const RibUpdateBatch& batch);
190 
194  RouteSet
195  getAncestorRoutes(const RibEntry& entry) const;
196 
201  RouteSet
202  getAncestorRoutes(const Name& name) const;
203 
207  void
208  modifyInheritedRoutes(const RibUpdateList& inheritedRoutes);
209 
210 public:
216  signal::Signal<Rib, Name> afterInsertEntry;
217 
223  signal::Signal<Rib, Name> afterEraseEntry;
224 
227  signal::Signal<Rib, RibRouteRef> afterAddRoute;
228 
231  signal::Signal<Rib, RibRouteRef> beforeRemoveRoute;
232 
233 private:
234  RibTable m_rib;
235  // FaceId => Entry with Route on this face
236  std::multimap<uint64_t, shared_ptr<RibEntry>> m_faceEntries;
237  size_t m_nItems = 0;
238  FibUpdater* m_fibUpdater = nullptr;
239 
240  struct UpdateQueueItem
241  {
242  RibUpdateBatch batch;
243  const Rib::UpdateSuccessCallback managerSuccessCallback;
244  const Rib::UpdateFailureCallback managerFailureCallback;
245  };
246 
247  using UpdateQueue = std::list<UpdateQueueItem>;
248  UpdateQueue m_updateBatches;
249  bool m_isUpdateInProgress = false;
250 
251  friend FibUpdater;
252 };
253 
254 std::ostream&
255 operator<<(std::ostream& os, const Rib& rib);
256 
257 } // namespace nfd::rib
258 
259 #endif // NFD_DAEMON_RIB_RIB_HPP
Computes FibUpdates based on updates to the RIB and sends them to NFD.
Definition: fib-updater.hpp:42
Represents a RIB entry, which contains one or more Routes with the same prefix.
Definition: rib-entry.hpp:39
RouteList::const_iterator const_iterator
Definition: rib-entry.hpp:43
Represents the Routing Information Base.
Definition: rib.hpp:62
signal::Signal< Rib, Name > afterInsertEntry
Signals after a RIB entry is inserted.
Definition: rib.hpp:216
size_t size() const noexcept
Definition: rib.hpp:93
signal::Signal< Rib, RibRouteRef > afterAddRoute
Signals after a Route is added.
Definition: rib.hpp:227
void beginRemoveFailedFaces(const std::set< uint64_t > &activeFaceIds)
Definition: rib.cpp:362
const_iterator begin() const
Definition: rib.hpp:81
signal::Signal< Rib, RibRouteRef > beforeRemoveRoute
Signals before a route is removed.
Definition: rib.hpp:231
std::function< void()> UpdateSuccessCallback
Definition: rib.hpp:108
void setFibUpdater(FibUpdater *updater)
Definition: rib.cpp:48
void beginRemoveFace(uint64_t faceId)
Starts the FIB update process when a face has been destroyed.
Definition: rib.cpp:352
RibTable::const_iterator const_iterator
Definition: rib.hpp:66
Route * findLongestPrefix(const Name &prefix, const Route &route) const
Definition: rib.cpp:77
void insert(const Name &prefix, const Route &route)
Definition: rib.cpp:91
const_iterator end() const
Definition: rib.hpp:87
signal::Signal< Rib, Name > afterEraseEntry
Signals after a RIB entry is erased.
Definition: rib.hpp:223
std::list< shared_ptr< RibEntry > > RibEntryList
Definition: rib.hpp:64
void onRouteExpiration(const Name &prefix, const Route &route)
Definition: rib.cpp:196
bool empty() const noexcept
Definition: rib.hpp:99
std::map< Name, shared_ptr< RibEntry > > RibTable
Definition: rib.hpp:65
const_iterator find(const Name &prefix) const
Definition: rib.cpp:54
void beginApplyUpdate(const RibUpdate &update, const UpdateSuccessCallback &onSuccess, const UpdateFailureCallback &onFailure)
Passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates.
Definition: rib.cpp:342
std::function< void(uint32_t code, const std::string &error)> UpdateFailureCallback
Definition: rib.hpp:109
shared_ptr< RibEntry > findParent(const Name &prefix) const
Definition: rib.cpp:209
Represents a collection of RibUpdates to be applied to a single FaceId.
Represents a route that will be added to or removed from a namespace.
Definition: rib-update.hpp:39
Represents a route for a name prefix.
Definition: route.hpp:43
#define NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:41
std::list< RibUpdate > RibUpdateList
bool operator<(const ReadvertisedRoute &lhs, const ReadvertisedRoute &rhs)
std::ostream & operator<<(std::ostream &os, const FibUpdate &update)
Definition: fib-update.hpp:73
References a route.
Definition: rib.hpp:43
shared_ptr< RibEntry > entry
Definition: rib.hpp:44
RibEntry::const_iterator route
Definition: rib.hpp:45