blob: 8b5d668603a056e8bff8fcf189d8be34d65aaa23 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <list>
akmhoquefdbddb12014-05-02 18:35:19 -050024#include <cmath>
akmhoque157b0a42014-05-13 00:26:37 -050025#include <ndn-cxx/common.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -050026
27#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050028#include "nexthop-list.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050029#include "face-map.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -050030#include "fib.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050031#include "logger.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050032
akmhoque53353462014-04-22 08:43:45 -050033
34
35namespace nlsr {
36
akmhoque674b0b12014-05-20 14:33:28 -050037INIT_LOGGER("Fib");
38
akmhoque53353462014-04-22 08:43:45 -050039using namespace std;
40using namespace ndn;
41
42static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050043fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050044{
akmhoquefdbddb12014-05-02 18:35:19 -050045 return fibEntry.getName() == name ;
akmhoque53353462014-04-22 08:43:45 -050046}
47
48void
akmhoque31d1d4b2014-05-05 22:08:14 -050049Fib::cancelScheduledExpiringEvent(EventId eid)
akmhoque53353462014-04-22 08:43:45 -050050{
akmhoque31d1d4b2014-05-05 22:08:14 -050051 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050052}
53
54
55ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -050056Fib::scheduleEntryRefreshing(const ndn::Name& name, int32_t feSeqNum,
akmhoquec7a79b22014-05-26 08:06:19 -050057 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050058{
akmhoque674b0b12014-05-20 14:33:28 -050059 _LOG_DEBUG("Fib::scheduleEntryRefreshing Called");
60 _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum);
akmhoquec7a79b22014-05-26 08:06:19 -050061 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -050062 ndn::bind(&Fib::refreshEntry, this,
63 name, feSeqNum));
akmhoque53353462014-04-22 08:43:45 -050064}
65
66void
akmhoque31d1d4b2014-05-05 22:08:14 -050067Fib::refreshEntry(const ndn::Name& name, int32_t feSeqNum)
akmhoque53353462014-04-22 08:43:45 -050068{
akmhoque2f423352014-06-03 11:49:35 -050069 _LOG_DEBUG("Fib::refreshEntry Called");
70 _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum);
akmhoquefdbddb12014-05-02 18:35:19 -050071 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
72 m_table.end(),
73 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -050074 if (it != m_table.end()) {
akmhoque157b0a42014-05-13 00:26:37 -050075 if (it->getSeqNo() == feSeqNum) {
akmhoque674b0b12014-05-20 14:33:28 -050076 _LOG_DEBUG("Refreshing the FIB entry. Name: " << name);
akmhoquefdbddb12014-05-02 18:35:19 -050077 for (std::list<NextHop>::iterator nhit =
akmhoque31d1d4b2014-05-05 22:08:14 -050078 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050079 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoquefdbddb12014-05-02 18:35:19 -050080 // add entry to NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -050081 registerPrefix(it->getName(), nhit->getConnectingFaceUri(),
82 std::ceil(nhit->getRouteCost()), m_refreshTime);
akmhoquefdbddb12014-05-02 18:35:19 -050083 }
akmhoquefdbddb12014-05-02 18:35:19 -050084 // increase sequence number and schedule refresh again
85 it->setSeqNo(feSeqNum + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -050086 it->setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
87 it->getSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -050088 ndn::time::seconds(m_refreshTime)));
akmhoquefdbddb12014-05-02 18:35:19 -050089 }
90 }
akmhoque53353462014-04-22 08:43:45 -050091}
92
93void
akmhoque31d1d4b2014-05-05 22:08:14 -050094Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050095{
akmhoque674b0b12014-05-20 14:33:28 -050096 _LOG_DEBUG("Fib::remove called");
akmhoque53353462014-04-22 08:43:45 -050097 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -050098 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -050099 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -0500100 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -0500101 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -0500102 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500103 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500104 //remove entry from NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500105 if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
106 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500107 }
108 else
109 {
akmhoque157b0a42014-05-13 00:26:37 -0500110 if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
111 nhit->getConnectingFaceUri()) {
112 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500113 }
114 }
akmhoque53353462014-04-22 08:43:45 -0500115 }
akmhoque674b0b12014-05-20 14:33:28 -0500116 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -0500117 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500118 m_table.erase(it);
119 }
120}
121
122
123void
akmhoque31d1d4b2014-05-05 22:08:14 -0500124Fib::update(const ndn::Name& name, NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500125{
akmhoque674b0b12014-05-20 14:33:28 -0500126 _LOG_DEBUG("Fib::updateFib Called");
akmhoque53353462014-04-22 08:43:45 -0500127 int startFace = 0;
128 int endFace = getNumberOfFacesForName(nextHopList,
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 m_nlsr.getConfParameter().getMaxFacesPerPrefix());
akmhoque53353462014-04-22 08:43:45 -0500130 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
131 m_table.end(),
132 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -0500133 if (it == m_table.end()) {
134 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500135 nextHopList.sort();
136 FibEntry newEntry(name);
akmhoquefdbddb12014-05-02 18:35:19 -0500137 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500138 std::list<NextHop>::iterator nhit = nhl.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500139 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500140 newEntry.getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500141 //Add entry to NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500142 registerPrefix(name, nhit->getConnectingFaceUri(),
143 std::ceil(nhit->getRouteCost()), m_refreshTime);
akmhoque53353462014-04-22 08:43:45 -0500144 }
akmhoquefdbddb12014-05-02 18:35:19 -0500145 newEntry.getNexthopList().sort();
akmhoquec7a79b22014-05-26 08:06:19 -0500146 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
147 expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime);
148 newEntry.setExpirationTimePoint(expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500149 newEntry.setSeqNo(1);
akmhoquec7a79b22014-05-26 08:06:19 -0500150 newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1,
151 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500152 m_table.push_back(newEntry);
153 }
154 }
akmhoque157b0a42014-05-13 00:26:37 -0500155 else {
akmhoque2f423352014-06-03 11:49:35 -0500156 _LOG_DEBUG("Old FIB Entry");
akmhoque157b0a42014-05-13 00:26:37 -0500157 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500158 nextHopList.sort();
akmhoque157b0a42014-05-13 00:26:37 -0500159 if (!it->isEqualNextHops(nextHopList)) {
akmhoquefdbddb12014-05-02 18:35:19 -0500160 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500161 std::list<NextHop>::iterator nhit = nhl.begin();
162 // Add first Entry to NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500163 registerPrefix(name, nhit->getConnectingFaceUri(),
164 std::ceil(nhit->getRouteCost()), m_refreshTime);
165 removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name);
akmhoquefdbddb12014-05-02 18:35:19 -0500166 it->getNexthopList().reset();
167 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500168 ++startFace;
169 ++nhit;
akmhoque157b0a42014-05-13 00:26:37 -0500170 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500171 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500172 //Add Entry to NDN_FIB
akmhoque157b0a42014-05-13 00:26:37 -0500173 registerPrefix(name, nhit->getConnectingFaceUri(),
174 std::ceil(nhit->getRouteCost()), m_refreshTime);
akmhoque53353462014-04-22 08:43:45 -0500175 }
176 }
akmhoquec7a79b22014-05-26 08:06:19 -0500177 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
178 expirationTimePoint = expirationTimePoint + ndn::time::seconds(m_refreshTime);
179 it->setExpirationTimePoint(expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500180 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -0500181 cancelScheduledExpiringEvent(it->getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500182 it->setSeqNo(it->getSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500183 (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
akmhoquec7a79b22014-05-26 08:06:19 -0500184 it->getSeqNo(),
185 ndn::time::seconds(m_refreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500186 }
akmhoque157b0a42014-05-13 00:26:37 -0500187 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500188 remove(name);
akmhoque53353462014-04-22 08:43:45 -0500189 }
190 }
191}
192
193
194
195void
akmhoque31d1d4b2014-05-05 22:08:14 -0500196Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500197{
akmhoque674b0b12014-05-20 14:33:28 -0500198 _LOG_DEBUG("Fib::clean called");
akmhoque53353462014-04-22 08:43:45 -0500199 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500200 ++it) {
akmhoque674b0b12014-05-20 14:33:28 -0500201 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500202 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500203 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -0500204 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500205 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500206 //Remove entry from NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500207 if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
208 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500209 }
akmhoque157b0a42014-05-13 00:26:37 -0500210 else {
211 if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
212 nhit->getConnectingFaceUri()) {
213 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500214 }
215 }
akmhoque53353462014-04-22 08:43:45 -0500216 }
217 }
akmhoque157b0a42014-05-13 00:26:37 -0500218 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500219 m_table.clear();
220 }
221}
222
223int
akmhoque31d1d4b2014-05-05 22:08:14 -0500224Fib::getNumberOfFacesForName(NexthopList& nextHopList,
225 uint32_t maxFacesPerPrefix)
akmhoque53353462014-04-22 08:43:45 -0500226{
227 int endFace = 0;
akmhoque157b0a42014-05-13 00:26:37 -0500228 if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) {
akmhoque53353462014-04-22 08:43:45 -0500229 return nextHopList.getSize();
230 }
akmhoque157b0a42014-05-13 00:26:37 -0500231 else {
akmhoque53353462014-04-22 08:43:45 -0500232 return maxFacesPerPrefix;
233 }
234 return endFace;
235}
236
237void
akmhoque157b0a42014-05-13 00:26:37 -0500238Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500239 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500240{
akmhoquefdbddb12014-05-02 18:35:19 -0500241 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500242 it != nl.getNextHops().end(); ++it) {
243 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500244 //Remove FIB Entry from NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500245 if (!m_nlsr.getAdjacencyList().isNeighbor(name)) {
246 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500247 }
akmhoque157b0a42014-05-13 00:26:37 -0500248 else {
249 if (m_nlsr.getAdjacencyList().getAdjacent(name).getConnectingFaceUri() !=
250 it->getConnectingFaceUri()) {
251 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500252 }
253 }
akmhoque53353462014-04-22 08:43:45 -0500254 }
255 }
256}
257
258void
akmhoque157b0a42014-05-13 00:26:37 -0500259Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
260 uint64_t faceCost, uint64_t timeout)
261{
262 ndn::nfd::ControlParameters faceParameters;
263 faceParameters
264 .setUri(faceUri);
265
266 m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters,
267 ndn::bind(&Fib::registerPrefixInNfd, this,_1,
268 namePrefix, faceCost, timeout),
269 ndn::bind(&Fib::onFailure, this, _1, _2,
270 "Failed in name registration"));
271
272}
273
274void
275Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
276 const ndn::Name& namePrefix, uint64_t faceCost, uint64_t timeout)
akmhoquefdbddb12014-05-02 18:35:19 -0500277{
278 ndn::nfd::ControlParameters controlParameters;
279 controlParameters
akmhoque157b0a42014-05-13 00:26:37 -0500280 .setName(namePrefix)
281 .setFaceId(faceCreateResult.getFaceId())
282 .setCost(faceCost)
283 .setExpirationPeriod(ndn::time::milliseconds(timeout * 1000))
284 .setOrigin(128);
akmhoquefdbddb12014-05-02 18:35:19 -0500285 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
akmhoque157b0a42014-05-13 00:26:37 -0500286 ndn::bind(&Fib::onRegistration, this, _1,
287 "Successful in name registration",
288 faceCreateResult.getUri()),
akmhoquefdbddb12014-05-02 18:35:19 -0500289 ndn::bind(&Fib::onFailure, this, _1, _2,
290 "Failed in name registration"));
291}
akmhoque31d1d4b2014-05-05 22:08:14 -0500292
akmhoquefdbddb12014-05-02 18:35:19 -0500293void
akmhoque157b0a42014-05-13 00:26:37 -0500294Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500295{
akmhoque157b0a42014-05-13 00:26:37 -0500296 uint32_t faceId = m_faceMap.getFaceId(faceUri);
297 if (faceId > 0) {
298 ndn::nfd::ControlParameters controlParameters;
299 controlParameters
300 .setName(namePrefix)
301 .setFaceId(faceId)
302 .setOrigin(128);
303 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoquefdbddb12014-05-02 18:35:19 -0500304 ndn::bind(&Fib::onSuccess, this, _1,
305 "Successful in unregistering name"),
306 ndn::bind(&Fib::onFailure, this, _1, _2,
307 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500308 }
akmhoquefdbddb12014-05-02 18:35:19 -0500309}
310
311void
akmhoque157b0a42014-05-13 00:26:37 -0500312Fib::setStrategy(const ndn::Name& name, const std::string& strategy)
313{
314 ndn::nfd::ControlParameters parameters;
315 parameters
316 .setName(name)
317 .setStrategy(strategy);
318
319 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
320 bind(&Fib::onSuccess, this, _1,
321 "Successfully set strategy choice"),
322 bind(&Fib::onFailure, this, _1, _2,
323 "Failed to set strategy choice"));
324}
325
326void
327Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
328 const std::string& message, const std::string& faceUri)
329{
akmhoque157b0a42014-05-13 00:26:37 -0500330 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
akmhoque2f423352014-06-03 11:49:35 -0500331 m_faceMap.writeLog();
akmhoque157b0a42014-05-13 00:26:37 -0500332}
333
334
335void
akmhoque31d1d4b2014-05-05 22:08:14 -0500336Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
337 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500338{
akmhoquefdbddb12014-05-02 18:35:19 -0500339}
340
341void
akmhoque31d1d4b2014-05-05 22:08:14 -0500342Fib::onFailure(uint32_t code, const std::string& error,
343 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500344{
akmhoque2f423352014-06-03 11:49:35 -0500345 _LOG_DEBUG(message << ": " << error << " (code: " << code << ")");
akmhoquefdbddb12014-05-02 18:35:19 -0500346}
347
akmhoque674b0b12014-05-20 14:33:28 -0500348void
349Fib::writeLog()
350{
351 _LOG_DEBUG("-------------------FIB-----------------------------");
352 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
353 ++it) {
354 (*it).writeLog();
355 }
356}
akmhoquefdbddb12014-05-02 18:35:19 -0500357
akmhoque53353462014-04-22 08:43:45 -0500358} //namespace nlsr