blob: 7fd0ed7527b71be45677cf814680eea4b01dda3b [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/*
3 * Copyright (c) 2014-2018, 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"
Vince Lehman281ded72014-08-21 12:17:08 -050027#include "core/logger.hpp"
28
Junxiao Shi25c6ce42016-09-09 13:49:59 +000029#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Vince12e49462014-06-09 13:29:32 -050030
31namespace nfd {
32namespace rib {
33
Davide Pesaventoa3148082018-04-12 18:21:54 -040034NFD_LOG_INIT(RibEntry);
35
Vince Lehman218be0a2015-01-15 17:25:20 -060036RibEntry::RouteList::iterator
37RibEntry::findRoute(const Route& route)
Vince12e49462014-06-09 13:29:32 -050038{
Vince Lehman218be0a2015-01-15 17:25:20 -060039 return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route));
Vince12e49462014-06-09 13:29:32 -050040}
41
Vince Lehman76c751c2014-11-18 17:36:38 -060042RibEntry::RouteList::const_iterator
43RibEntry::findRoute(const Route& route) const
44{
45 return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route));
46}
47
Nick Gordon89c4cca2016-11-02 15:42:32 +000048std::pair<RibEntry::iterator, bool>
Vince Lehman218be0a2015-01-15 17:25:20 -060049RibEntry::insertRoute(const Route& route)
Vince12e49462014-06-09 13:29:32 -050050{
Vince Lehman218be0a2015-01-15 17:25:20 -060051 iterator it = findRoute(route);
Vince12e49462014-06-09 13:29:32 -050052
Vince Lehman76c751c2014-11-18 17:36:38 -060053 if (it == end()) {
54 if (route.flags & ndn::nfd::ROUTE_FLAG_CAPTURE) {
55 m_nRoutesWithCaptureSet++;
56 }
Vince Lehman4387e782014-06-19 16:57:45 -050057
Vince Lehman76c751c2014-11-18 17:36:38 -060058 m_routes.push_back(route);
59
Nick Gordon89c4cca2016-11-02 15:42:32 +000060 return std::make_pair(std::prev(m_routes.end()), true);
Vince Lehman76c751c2014-11-18 17:36:38 -060061 }
62 else {
Nick Gordon89c4cca2016-11-02 15:42:32 +000063 return std::make_pair(it, false);
Vince Lehman76c751c2014-11-18 17:36:38 -060064 }
Vince12e49462014-06-09 13:29:32 -050065}
66
Vince Lehman281ded72014-08-21 12:17:08 -050067void
Vince Lehman218be0a2015-01-15 17:25:20 -060068RibEntry::eraseRoute(const Route& route)
Vince12e49462014-06-09 13:29:32 -050069{
Vince Lehman218be0a2015-01-15 17:25:20 -060070 RibEntry::iterator it = findRoute(route);
71 eraseRoute(it);
Vince12e49462014-06-09 13:29:32 -050072}
73
74bool
Vince Lehman218be0a2015-01-15 17:25:20 -060075RibEntry::hasRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -050076{
Vince Lehman218be0a2015-01-15 17:25:20 -060077 RibEntry::const_iterator it = findRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -050078
Vince Lehman218be0a2015-01-15 17:25:20 -060079 return it != end();
Vince Lehman4387e782014-06-19 16:57:45 -050080}
81
82bool
Vince12e49462014-06-09 13:29:32 -050083RibEntry::hasFaceId(const uint64_t faceId) const
84{
Vince Lehman218be0a2015-01-15 17:25:20 -060085 RibEntry::const_iterator it = std::find_if(begin(), end(), bind(&compareFaceId, _1, faceId));
Vince12e49462014-06-09 13:29:32 -050086
Vince Lehman218be0a2015-01-15 17:25:20 -060087 return it != end();
Vince12e49462014-06-09 13:29:32 -050088}
89
Vince Lehman76c751c2014-11-18 17:36:38 -060090size_t
91RibEntry::getNRoutes() const
92{
93 return m_routes.size();
94}
95
Vince12e49462014-06-09 13:29:32 -050096void
97RibEntry::addChild(shared_ptr<RibEntry> child)
98{
99 BOOST_ASSERT(!static_cast<bool>(child->getParent()));
100 child->setParent(this->shared_from_this());
101 m_children.push_back(child);
102}
103
104void
105RibEntry::removeChild(shared_ptr<RibEntry> child)
106{
107 BOOST_ASSERT(child->getParent().get() == this);
108 child->setParent(shared_ptr<RibEntry>());
109 m_children.remove(child);
110}
111
Vince Lehman218be0a2015-01-15 17:25:20 -0600112RibEntry::RouteList::iterator
113RibEntry::eraseRoute(RouteList::iterator route)
Vince12e49462014-06-09 13:29:32 -0500114{
Vince Lehman76c751c2014-11-18 17:36:38 -0600115 if (route != m_routes.end()) {
116 if (route->flags & ndn::nfd::ROUTE_FLAG_CAPTURE) {
117 m_nRoutesWithCaptureSet--;
Vince Lehman4387e782014-06-19 16:57:45 -0500118 }
119
Vince Lehman76c751c2014-11-18 17:36:38 -0600120 // Cancel any scheduled event
121 NFD_LOG_TRACE("Cancelling expiration eventId: " << route->getExpirationEvent());
122 scheduler::cancel(route->getExpirationEvent());
123
124 return m_routes.erase(route);
125 }
126
Vince Lehman218be0a2015-01-15 17:25:20 -0600127 return m_routes.end();
Vince Lehman4387e782014-06-19 16:57:45 -0500128}
129
130void
Vince Lehman218be0a2015-01-15 17:25:20 -0600131RibEntry::addInheritedRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -0500132{
Vince Lehman218be0a2015-01-15 17:25:20 -0600133 m_inheritedRoutes.push_back(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500134}
135
136void
Vince Lehman218be0a2015-01-15 17:25:20 -0600137RibEntry::removeInheritedRoute(const Route& route)
Vince Lehman4387e782014-06-19 16:57:45 -0500138{
Vince Lehman76c751c2014-11-18 17:36:38 -0600139 RouteList::iterator it = std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(),
140 bind(&compareFaceId, _1, route.faceId));
Vince Lehman218be0a2015-01-15 17:25:20 -0600141 m_inheritedRoutes.erase(it);
Vince Lehman4387e782014-06-19 16:57:45 -0500142}
143
Vince Lehman76c751c2014-11-18 17:36:38 -0600144RibEntry::RouteList::const_iterator
145RibEntry::findInheritedRoute(const Route& route) const
Vince Lehman4387e782014-06-19 16:57:45 -0500146{
Vince Lehman218be0a2015-01-15 17:25:20 -0600147 return std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(),
148 bind(&compareFaceId, _1, route.faceId));
Vince Lehman4387e782014-06-19 16:57:45 -0500149}
150
151bool
Vince Lehman76c751c2014-11-18 17:36:38 -0600152RibEntry::hasInheritedRoute(const Route& route) const
Vince Lehman4387e782014-06-19 16:57:45 -0500153{
Vince Lehman218be0a2015-01-15 17:25:20 -0600154 RouteList::const_iterator it = findInheritedRoute(route);
Vince Lehman4387e782014-06-19 16:57:45 -0500155
Vince Lehman218be0a2015-01-15 17:25:20 -0600156 return (it != m_inheritedRoutes.end());
Vince Lehman4387e782014-06-19 16:57:45 -0500157}
158
159bool
160RibEntry::hasCapture() const
161{
Vince Lehman218be0a2015-01-15 17:25:20 -0600162 return m_nRoutesWithCaptureSet > 0;
Vince Lehman4387e782014-06-19 16:57:45 -0500163}
164
165bool
166RibEntry::hasChildInheritOnFaceId(uint64_t faceId) const
167{
Vince Lehman76c751c2014-11-18 17:36:38 -0600168 for (const Route& route : m_routes) {
169 if (route.faceId == faceId && (route.flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)) {
170 return true;
Vince Lehman4387e782014-06-19 16:57:45 -0500171 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600172 }
Vince Lehman4387e782014-06-19 16:57:45 -0500173
174 return false;
175}
176
Vince Lehman9dcfc402015-03-26 03:18:54 -0500177const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600178RibEntry::getRouteWithLowestCostByFaceId(uint64_t faceId) const
Vince Lehman4387e782014-06-19 16:57:45 -0500179{
Vince Lehman9dcfc402015-03-26 03:18:54 -0500180 const Route* candidate = nullptr;
Vince Lehman4387e782014-06-19 16:57:45 -0500181
Vince Lehman76c751c2014-11-18 17:36:38 -0600182 for (const Route& route : m_routes) {
183 // Matching face ID
184 if (route.faceId == faceId) {
185 // If this is the first route with this Face ID found
186 if (candidate == nullptr) {
Vince Lehman9dcfc402015-03-26 03:18:54 -0500187 candidate = &route;
Vince Lehman76c751c2014-11-18 17:36:38 -0600188 }
189 else if (route.cost < candidate->cost) {
190 // Found a route with a lower cost
Vince Lehman9dcfc402015-03-26 03:18:54 -0500191 candidate = &route;
Vince Lehman76c751c2014-11-18 17:36:38 -0600192 }
Vince Lehman4387e782014-06-19 16:57:45 -0500193 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600194 }
Vince Lehman4387e782014-06-19 16:57:45 -0500195
Vince Lehman218be0a2015-01-15 17:25:20 -0600196 return candidate;
Vince Lehman4387e782014-06-19 16:57:45 -0500197}
198
Vince Lehman76c751c2014-11-18 17:36:38 -0600199const Route*
200RibEntry::getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const
201{
Vince Lehman4310d502016-03-04 11:58:59 -0600202 std::vector<const Route*> matches;
Vince Lehman76c751c2014-11-18 17:36:38 -0600203
204 // Copy routes which have faceId
Vince Lehman4310d502016-03-04 11:58:59 -0600205 for (const Route& route : m_routes) {
206 if (route.faceId == faceId) {
207 matches.push_back(&route);
208 }
209 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600210
Vince Lehman4310d502016-03-04 11:58:59 -0600211 // If there are less than 2 routes, there is no second lowest
Vince Lehman76c751c2014-11-18 17:36:38 -0600212 if (matches.size() < 2) {
213 return nullptr;
214 }
215
216 // Get second lowest cost
217 std::nth_element(matches.begin(), matches.begin() + 1, matches.end(),
Vince Lehman4310d502016-03-04 11:58:59 -0600218 [] (const Route* lhs, const Route* rhs) { return lhs->cost < rhs->cost; });
Vince Lehman76c751c2014-11-18 17:36:38 -0600219
Vince Lehman4310d502016-03-04 11:58:59 -0600220 return matches.at(1);
Vince Lehman76c751c2014-11-18 17:36:38 -0600221}
222
Vince Lehman9dcfc402015-03-26 03:18:54 -0500223const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600224RibEntry::getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const
Vince Lehman4387e782014-06-19 16:57:45 -0500225{
Vince Lehman9dcfc402015-03-26 03:18:54 -0500226 const Route* candidate = nullptr;
Vince Lehman4387e782014-06-19 16:57:45 -0500227
Vince Lehman76c751c2014-11-18 17:36:38 -0600228 for (const Route& route : m_routes) {
229 // Correct face ID and Child Inherit flag set
230 if (route.faceId == faceId &&
231 (route.flags & ndn::nfd::ROUTE_FLAG_CHILD_INHERIT) == ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
Vince Lehman4387e782014-06-19 16:57:45 -0500232 {
Vince Lehman76c751c2014-11-18 17:36:38 -0600233 // If this is the first route with this Face ID found
234 if (candidate == nullptr) {
Vince Lehman9dcfc402015-03-26 03:18:54 -0500235 candidate = &route;
Vince Lehman76c751c2014-11-18 17:36:38 -0600236 }
237 else if (route.cost < candidate->cost) {
238 // Found a route with a lower cost
Vince Lehman9dcfc402015-03-26 03:18:54 -0500239 candidate = &route;
Vince Lehman76c751c2014-11-18 17:36:38 -0600240 }
Vince Lehman4387e782014-06-19 16:57:45 -0500241 }
Vince Lehman76c751c2014-11-18 17:36:38 -0600242 }
Vince Lehman4387e782014-06-19 16:57:45 -0500243
Vince Lehman218be0a2015-01-15 17:25:20 -0600244 return candidate;
Vince12e49462014-06-09 13:29:32 -0500245}
246
247std::ostream&
Vince12e49462014-06-09 13:29:32 -0500248operator<<(std::ostream& os, const RibEntry& entry)
249{
Syed Obaid3313a372014-07-01 01:31:33 -0500250 os << "RibEntry {\n";
Junxiao Shi3f21e582017-05-29 15:26:32 +0000251 os << " Name: " << entry.getName() << "\n";
Vince12e49462014-06-09 13:29:32 -0500252
Vince Lehman76c751c2014-11-18 17:36:38 -0600253 for (const Route& route : entry) {
Junxiao Shi3f21e582017-05-29 15:26:32 +0000254 os << " " << route << "\n";
Vince Lehman76c751c2014-11-18 17:36:38 -0600255 }
Vince12e49462014-06-09 13:29:32 -0500256
257 os << "}";
258
259 return os;
260}
261
262} // namespace rib
263} // namespace nfd