blob: 1df227ef260ea2bd3ddbe1c3e3803a82d896629f [file] [log] [blame]
Vince12e49462014-06-09 13:29:32 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08004 * 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.
Vince12e49462014-06-09 13:29:32 -050010 *
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#include "rib-entry.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/logger.hpp"
Vince Lehman281ded72014-08-21 12:17:08 -050028
Junxiao Shi25c6ce42016-09-09 13:49:59 +000029#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Vince12e49462014-06-09 13:29:32 -050030
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::rib {
Vince12e49462014-06-09 13:29:32 -050032
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(RibEntry);
34
Davide Pesavento412c9822021-07-02 00:21:05 -040035static bool
36compareFaceIdAndOrigin(const Route& lhs, const Route& rhs)
37{
38 return lhs.faceId == rhs.faceId && lhs.origin == rhs.origin;
39}
40
Vince Lehman218be0a2015-01-15 17:25:20 -060041RibEntry::RouteList::iterator
42RibEntry::findRoute(const Route& route)
Vince12e49462014-06-09 13:29:32 -050043{
Davide Pesavento412c9822021-07-02 00:21:05 -040044 return std::find_if(begin(), end(),
45 [&] (const auto& r) { return compareFaceIdAndOrigin(r, route); });
Vince12e49462014-06-09 13:29:32 -050046}
47
Vince Lehman76c751c2014-11-18 17:36:38 -060048RibEntry::RouteList::const_iterator
49RibEntry::findRoute(const Route& route) const
50{
Davide Pesavento412c9822021-07-02 00:21:05 -040051 return std::find_if(begin(), end(),
52 [&] (const auto& r) { return compareFaceIdAndOrigin(r, route); });
Vince Lehman76c751c2014-11-18 17:36:38 -060053}
54
Nick Gordon89c4cca2016-11-02 15:42:32 +000055std::pair<RibEntry::iterator, bool>
Vince Lehman218be0a2015-01-15 17:25:20 -060056RibEntry::insertRoute(const Route& route)
Vince12e49462014-06-09 13:29:32 -050057{
Davide Pesavento412c9822021-07-02 00:21:05 -040058 auto it = findRoute(route);
Vince12e49462014-06-09 13:29:32 -050059
Vince Lehman76c751c2014-11-18 17:36:38 -060060 if (it == end()) {
61 if (route.flags & ndn::nfd::ROUTE_FLAG_CAPTURE) {
62 m_nRoutesWithCaptureSet++;
63 }
Vince Lehman4387e782014-06-19 16:57:45 -050064
Vince Lehman76c751c2014-11-18 17:36:38 -060065 m_routes.push_back(route);
Davide Pesaventoe4b22382018-06-10 14:37:24 -040066 return {std::prev(m_routes.end()), true};
67 }
Vince Lehman76c751c2014-11-18 17:36:38 -060068
Davide Pesaventoe4b22382018-06-10 14:37:24 -040069 return {it, false};
Vince12e49462014-06-09 13:29:32 -050070}
71
Vince Lehman281ded72014-08-21 12:17:08 -050072void
Vince Lehman218be0a2015-01-15 17:25:20 -060073RibEntry::eraseRoute(const Route& route)
Vince12e49462014-06-09 13:29:32 -050074{
Davide Pesavento412c9822021-07-02 00:21:05 -040075 auto it = findRoute(route);
Vince Lehman218be0a2015-01-15 17:25:20 -060076 eraseRoute(it);
Vince12e49462014-06-09 13:29:32 -050077}
78
79bool
Vince Lehman218be0a2015-01-15 17:25:20 -060080RibEntry::hasRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -050081{
Davide Pesavento412c9822021-07-02 00:21:05 -040082 auto it = findRoute(route);
Vince Lehman218be0a2015-01-15 17:25:20 -060083 return it != end();
Vince Lehman4387e782014-06-19 16:57:45 -050084}
85
86bool
Davide Pesavento412c9822021-07-02 00:21:05 -040087RibEntry::hasFaceId(uint64_t faceId) const
Vince12e49462014-06-09 13:29:32 -050088{
Davide Pesavento412c9822021-07-02 00:21:05 -040089 auto it = std::find_if(begin(), end(), [faceId] (const auto& r) { return r.faceId == faceId; });
Vince Lehman218be0a2015-01-15 17:25:20 -060090 return it != end();
Vince12e49462014-06-09 13:29:32 -050091}
92
Vince Lehman76c751c2014-11-18 17:36:38 -060093size_t
94RibEntry::getNRoutes() const
95{
96 return m_routes.size();
97}
98
Vince12e49462014-06-09 13:29:32 -050099void
100RibEntry::addChild(shared_ptr<RibEntry> child)
101{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400102 BOOST_ASSERT(!child->getParent());
Vince12e49462014-06-09 13:29:32 -0500103 child->setParent(this->shared_from_this());
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400104 m_children.push_back(std::move(child));
Vince12e49462014-06-09 13:29:32 -0500105}
106
107void
108RibEntry::removeChild(shared_ptr<RibEntry> child)
109{
110 BOOST_ASSERT(child->getParent().get() == this);
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400111 child->setParent(nullptr);
Vince12e49462014-06-09 13:29:32 -0500112 m_children.remove(child);
113}
114
Vince Lehman218be0a2015-01-15 17:25:20 -0600115RibEntry::RouteList::iterator
116RibEntry::eraseRoute(RouteList::iterator route)
Vince12e49462014-06-09 13:29:32 -0500117{
Vince Lehman76c751c2014-11-18 17:36:38 -0600118 if (route != m_routes.end()) {
119 if (route->flags & ndn::nfd::ROUTE_FLAG_CAPTURE) {
120 m_nRoutesWithCaptureSet--;
Vince Lehman4387e782014-06-19 16:57:45 -0500121 }
122
Vince Lehman76c751c2014-11-18 17:36:38 -0600123 // Cancel any scheduled event
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400124 NFD_LOG_TRACE("Cancelling expiration event: " << route->getExpirationEvent());
125 route->cancelExpirationEvent();
Vince Lehman76c751c2014-11-18 17:36:38 -0600126
127 return m_routes.erase(route);
128 }
129
Vince Lehman218be0a2015-01-15 17:25:20 -0600130 return m_routes.end();
Vince Lehman4387e782014-06-19 16:57:45 -0500131}
132
133void
Vince Lehman218be0a2015-01-15 17:25:20 -0600134RibEntry::addInheritedRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -0500135{
Vince Lehman218be0a2015-01-15 17:25:20 -0600136 m_inheritedRoutes.push_back(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500137}
138
139void
Vince Lehman218be0a2015-01-15 17:25:20 -0600140RibEntry::removeInheritedRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -0500141{
Davide Pesavento412c9822021-07-02 00:21:05 -0400142 m_inheritedRoutes.remove_if([id = route.faceId] (const auto& r) { return r.faceId == id; });
Vince Lehman4387e782014-06-19 16:57:45 -0500143}
144
Vince Lehman76c751c2014-11-18 17:36:38 -0600145RibEntry::RouteList::const_iterator
146RibEntry::findInheritedRoute(const Route& route) const
Vince Lehman4387e782014-06-19 16:57:45 -0500147{
Vince Lehman218be0a2015-01-15 17:25:20 -0600148 return std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(),
Davide Pesavento412c9822021-07-02 00:21:05 -0400149 [id = route.faceId] (const auto& r) { return r.faceId == id; });
Vince Lehman4387e782014-06-19 16:57:45 -0500150}
151
152bool
Vince Lehman76c751c2014-11-18 17:36:38 -0600153RibEntry::hasInheritedRoute(const Route& route) const
Vince Lehman4387e782014-06-19 16:57:45 -0500154{
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400155 return findInheritedRoute(route) != m_inheritedRoutes.end();
Vince Lehman4387e782014-06-19 16:57:45 -0500156}
157
158bool
159RibEntry::hasCapture() const
160{
Vince Lehman218be0a2015-01-15 17:25:20 -0600161 return m_nRoutesWithCaptureSet > 0;
Vince Lehman4387e782014-06-19 16:57:45 -0500162}
163
164bool
165RibEntry::hasChildInheritOnFaceId(uint64_t faceId) const
166{
Vince Lehman76c751c2014-11-18 17:36:38 -0600167 for (const Route& route : m_routes) {
168 if (route.faceId == faceId && (route.flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)) {
169 return true;
Vince Lehman4387e782014-06-19 16:57:45 -0500170 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600171 }
Vince Lehman4387e782014-06-19 16:57:45 -0500172
173 return false;
174}
175
Vince Lehman9dcfc402015-03-26 03:18:54 -0500176const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600177RibEntry::getRouteWithLowestCostByFaceId(uint64_t faceId) const
Vince Lehman4387e782014-06-19 16:57:45 -0500178{
Vince Lehman9dcfc402015-03-26 03:18:54 -0500179 const Route* candidate = nullptr;
Vince Lehman4387e782014-06-19 16:57:45 -0500180
Vince Lehman76c751c2014-11-18 17:36:38 -0600181 for (const Route& route : m_routes) {
182 // Matching face ID
183 if (route.faceId == faceId) {
184 // If this is the first route with this Face ID found
185 if (candidate == nullptr) {
Vince Lehman9dcfc402015-03-26 03:18:54 -0500186 candidate = &route;
Vince Lehman76c751c2014-11-18 17:36:38 -0600187 }
188 else if (route.cost < candidate->cost) {
189 // Found a route with a lower cost
Vince Lehman9dcfc402015-03-26 03:18:54 -0500190 candidate = &route;
Vince Lehman76c751c2014-11-18 17:36:38 -0600191 }
Vince Lehman4387e782014-06-19 16:57:45 -0500192 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600193 }
Vince Lehman4387e782014-06-19 16:57:45 -0500194
Vince Lehman218be0a2015-01-15 17:25:20 -0600195 return candidate;
Vince Lehman4387e782014-06-19 16:57:45 -0500196}
197
Vince Lehman76c751c2014-11-18 17:36:38 -0600198const Route*
199RibEntry::getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const
200{
Vince Lehman4310d502016-03-04 11:58:59 -0600201 std::vector<const Route*> matches;
Vince Lehman76c751c2014-11-18 17:36:38 -0600202
203 // Copy routes which have faceId
Vince Lehman4310d502016-03-04 11:58:59 -0600204 for (const Route& route : m_routes) {
205 if (route.faceId == faceId) {
206 matches.push_back(&route);
207 }
208 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600209
Vince Lehman4310d502016-03-04 11:58:59 -0600210 // If there are less than 2 routes, there is no second lowest
Vince Lehman76c751c2014-11-18 17:36:38 -0600211 if (matches.size() < 2) {
212 return nullptr;
213 }
214
215 // Get second lowest cost
216 std::nth_element(matches.begin(), matches.begin() + 1, matches.end(),
Vince Lehman4310d502016-03-04 11:58:59 -0600217 [] (const Route* lhs, const Route* rhs) { return lhs->cost < rhs->cost; });
Vince Lehman76c751c2014-11-18 17:36:38 -0600218
Vince Lehman4310d502016-03-04 11:58:59 -0600219 return matches.at(1);
Vince Lehman76c751c2014-11-18 17:36:38 -0600220}
221
Vince Lehman9dcfc402015-03-26 03:18:54 -0500222const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600223RibEntry::getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const
Vince Lehman4387e782014-06-19 16:57:45 -0500224{
Vince Lehman9dcfc402015-03-26 03:18:54 -0500225 const Route* candidate = nullptr;
Vince Lehman4387e782014-06-19 16:57:45 -0500226
Vince Lehman76c751c2014-11-18 17:36:38 -0600227 for (const Route& route : m_routes) {
228 // Correct face ID and Child Inherit flag set
229 if (route.faceId == faceId &&
230 (route.flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) == ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
Vince Lehman4387e782014-06-19 16:57:45 -0500231 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600232 // If this is the first route with this Face ID found
233 if (candidate == nullptr) {
Vince Lehman9dcfc402015-03-26 03:18:54 -0500234 candidate = &route;
Vince Lehman76c751c2014-11-18 17:36:38 -0600235 }
236 else if (route.cost < candidate->cost) {
237 // Found a route with a lower cost
Vince Lehman9dcfc402015-03-26 03:18:54 -0500238 candidate = &route;
Vince Lehman76c751c2014-11-18 17:36:38 -0600239 }
Vince Lehman4387e782014-06-19 16:57:45 -0500240 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600241 }
Vince Lehman4387e782014-06-19 16:57:45 -0500242
Vince Lehman218be0a2015-01-15 17:25:20 -0600243 return candidate;
Vince12e49462014-06-09 13:29:32 -0500244}
245
Junxiao Shid47cd632018-09-11 03:10:00 +0000246ndn::PrefixAnnouncement
247RibEntry::getPrefixAnnouncement(time::milliseconds minExpiration,
248 time::milliseconds maxExpiration) const
249{
250 const Route* bestAnnRoute = nullptr;
Davide Pesavento412c9822021-07-02 00:21:05 -0400251 auto entryExpiry = time::steady_clock::time_point::min();
Junxiao Shid47cd632018-09-11 03:10:00 +0000252
253 for (const Route& route : *this) {
254 if (route.expires) {
255 entryExpiry = std::max(entryExpiry, *route.expires);
256 if (route.announcement) {
257 if (bestAnnRoute == nullptr || *route.expires > *bestAnnRoute->expires) {
258 bestAnnRoute = &route;
259 }
260 }
261 }
262 else {
Davide Pesavento412c9822021-07-02 00:21:05 -0400263 entryExpiry = time::steady_clock::time_point::max();
Junxiao Shid47cd632018-09-11 03:10:00 +0000264 }
265 }
266
267 if (bestAnnRoute != nullptr) {
268 return *bestAnnRoute->announcement;
269 }
270
271 ndn::PrefixAnnouncement ann;
272 ann.setAnnouncedName(m_name);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400273 ann.setExpiration(std::clamp(
Junxiao Shid47cd632018-09-11 03:10:00 +0000274 time::duration_cast<time::milliseconds>(entryExpiry - time::steady_clock::now()),
275 minExpiration, maxExpiration));
276 return ann;
277}
278
Vince12e49462014-06-09 13:29:32 -0500279std::ostream&
Vince12e49462014-06-09 13:29:32 -0500280operator<<(std::ostream& os, const RibEntry& entry)
281{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400282 os << "RibEntry {\n"
283 << " Name: " << entry.getName() << "\n";
Vince Lehman76c751c2014-11-18 17:36:38 -0600284 for (const Route& route : entry) {
Junxiao Shi3f21e582017-05-29 15:26:32 +0000285 os << " " << route << "\n";
Vince Lehman76c751c2014-11-18 17:36:38 -0600286 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400287 return os << "}";
Vince12e49462014-06-09 13:29:32 -0500288}
289
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400290} // namespace nfd::rib