blob: 17269dce78e2d6d84f28d696a84ccf26cfa55227 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
akmhoquefdbddb12014-05-02 18:35:19 -05002#include <cmath>
akmhoque157b0a42014-05-13 00:26:37 -05003#include <ndn-cxx/common.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -05004
5#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -05006#include "nexthop-list.hpp"
akmhoque157b0a42014-05-13 00:26:37 -05007#include "face-map.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -05008#include "fib.hpp"
akmhoque674b0b12014-05-20 14:33:28 -05009#include "logger.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050010
akmhoque53353462014-04-22 08:43:45 -050011
12
13namespace nlsr {
14
akmhoque674b0b12014-05-20 14:33:28 -050015INIT_LOGGER("Fib");
16
akmhoque53353462014-04-22 08:43:45 -050017using namespace std;
18using namespace ndn;
19
20static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050021fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050022{
akmhoquefdbddb12014-05-02 18:35:19 -050023 return fibEntry.getName() == name ;
akmhoque53353462014-04-22 08:43:45 -050024}
25
26void
akmhoque31d1d4b2014-05-05 22:08:14 -050027Fib::cancelScheduledExpiringEvent(EventId eid)
akmhoque53353462014-04-22 08:43:45 -050028{
akmhoque31d1d4b2014-05-05 22:08:14 -050029 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050030}
31
32
33ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -050034Fib::scheduleEntryRefreshing(const ndn::Name& name, int32_t feSeqNum,
akmhoquefdbddb12014-05-02 18:35:19 -050035 int32_t refreshTime)
akmhoque53353462014-04-22 08:43:45 -050036{
akmhoquefdbddb12014-05-02 18:35:19 -050037 std::cout << "Fib::scheduleEntryRefreshing Called" << std::endl;
38 std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050039 _LOG_DEBUG("Fib::scheduleEntryRefreshing Called");
40 _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum);
akmhoque31d1d4b2014-05-05 22:08:14 -050041 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
42 ndn::bind(&Fib::refreshEntry, this,
43 name, feSeqNum));
akmhoque53353462014-04-22 08:43:45 -050044}
45
46void
akmhoque31d1d4b2014-05-05 22:08:14 -050047Fib::refreshEntry(const ndn::Name& name, int32_t feSeqNum)
akmhoque53353462014-04-22 08:43:45 -050048{
akmhoquefdbddb12014-05-02 18:35:19 -050049 std::cout << "Fib::refreshEntry Called" << std::endl;
50 std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
51 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
52 m_table.end(),
53 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -050054 if (it != m_table.end()) {
akmhoquefdbddb12014-05-02 18:35:19 -050055 std::cout << "Entry found with Seq Num: " << feSeqNum << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050056 if (it->getSeqNo() == feSeqNum) {
akmhoquefdbddb12014-05-02 18:35:19 -050057 std::cout << "Refreshing the FIB entry" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050058 _LOG_DEBUG("Refreshing the FIB entry. Name: " << name);
akmhoquefdbddb12014-05-02 18:35:19 -050059 for (std::list<NextHop>::iterator nhit =
akmhoque31d1d4b2014-05-05 22:08:14 -050060 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050061 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoquefdbddb12014-05-02 18:35:19 -050062 // add entry to NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -050063 registerPrefix(it->getName(), nhit->getConnectingFaceUri(),
64 std::ceil(nhit->getRouteCost()), m_refreshTime);
akmhoquefdbddb12014-05-02 18:35:19 -050065 }
akmhoquefdbddb12014-05-02 18:35:19 -050066 // increase sequence number and schedule refresh again
67 it->setSeqNo(feSeqNum + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -050068 it->setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
69 it->getSeqNo(),
akmhoquefdbddb12014-05-02 18:35:19 -050070 m_refreshTime));
akmhoquefdbddb12014-05-02 18:35:19 -050071 }
72 }
akmhoque53353462014-04-22 08:43:45 -050073}
74
75void
akmhoque31d1d4b2014-05-05 22:08:14 -050076Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050077{
akmhoque674b0b12014-05-20 14:33:28 -050078 _LOG_DEBUG("Fib::remove called");
akmhoque53353462014-04-22 08:43:45 -050079 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -050080 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -050081 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -050082 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050083 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -050084 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050085 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -050086 //remove entry from NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -050087 if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
88 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050089 }
90 else
91 {
akmhoque157b0a42014-05-13 00:26:37 -050092 if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
93 nhit->getConnectingFaceUri()) {
94 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050095 }
96 }
akmhoque53353462014-04-22 08:43:45 -050097 }
akmhoquefdbddb12014-05-02 18:35:19 -050098 std::cout << "Cancellling Scheduled event" << std::endl;
99 std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500100 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500102 m_table.erase(it);
103 }
104}
105
106
107void
akmhoque31d1d4b2014-05-05 22:08:14 -0500108Fib::update(const ndn::Name& name, NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500109{
110 std::cout << "Fib::updateFib Called" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500111 _LOG_DEBUG("Fib::updateFib Called");
akmhoque53353462014-04-22 08:43:45 -0500112 int startFace = 0;
113 int endFace = getNumberOfFacesForName(nextHopList,
akmhoque31d1d4b2014-05-05 22:08:14 -0500114 m_nlsr.getConfParameter().getMaxFacesPerPrefix());
akmhoque53353462014-04-22 08:43:45 -0500115 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
116 m_table.end(),
117 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -0500118 if (it == m_table.end()) {
119 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500120 nextHopList.sort();
121 FibEntry newEntry(name);
akmhoquefdbddb12014-05-02 18:35:19 -0500122 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500123 std::list<NextHop>::iterator nhit = nhl.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500124 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500125 newEntry.getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500126 //Add entry to NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500127 registerPrefix(name, nhit->getConnectingFaceUri(),
128 std::ceil(nhit->getRouteCost()), m_refreshTime);
akmhoque53353462014-04-22 08:43:45 -0500129 }
akmhoquefdbddb12014-05-02 18:35:19 -0500130 newEntry.getNexthopList().sort();
akmhoque53353462014-04-22 08:43:45 -0500131 newEntry.setTimeToRefresh(m_refreshTime);
132 newEntry.setSeqNo(1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500133 newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1, m_refreshTime));
akmhoque53353462014-04-22 08:43:45 -0500134 m_table.push_back(newEntry);
135 }
136 }
akmhoque157b0a42014-05-13 00:26:37 -0500137 else {
akmhoque53353462014-04-22 08:43:45 -0500138 std::cout << "Old FIB Entry" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500139 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500140 nextHopList.sort();
akmhoque157b0a42014-05-13 00:26:37 -0500141 if (!it->isEqualNextHops(nextHopList)) {
akmhoquefdbddb12014-05-02 18:35:19 -0500142 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500143 std::list<NextHop>::iterator nhit = nhl.begin();
144 // Add first Entry to NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500145 registerPrefix(name, nhit->getConnectingFaceUri(),
146 std::ceil(nhit->getRouteCost()), m_refreshTime);
147 removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name);
akmhoquefdbddb12014-05-02 18:35:19 -0500148 it->getNexthopList().reset();
149 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500150 ++startFace;
151 ++nhit;
akmhoque157b0a42014-05-13 00:26:37 -0500152 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500153 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500154 //Add Entry to NDN_FIB
akmhoque157b0a42014-05-13 00:26:37 -0500155 registerPrefix(name, nhit->getConnectingFaceUri(),
156 std::ceil(nhit->getRouteCost()), m_refreshTime);
akmhoque53353462014-04-22 08:43:45 -0500157 }
158 }
159 it->setTimeToRefresh(m_refreshTime);
akmhoquefdbddb12014-05-02 18:35:19 -0500160 std::cout << "Cancellling Scheduled event" << std::endl;
161 std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500162 _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -0500163 cancelScheduledExpiringEvent(it->getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500164 it->setSeqNo(it->getSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500165 (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
akmhoque53353462014-04-22 08:43:45 -0500166 it->getSeqNo(), m_refreshTime));
167 }
akmhoque157b0a42014-05-13 00:26:37 -0500168 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500169 remove(name);
akmhoque53353462014-04-22 08:43:45 -0500170 }
171 }
172}
173
174
175
176void
akmhoque31d1d4b2014-05-05 22:08:14 -0500177Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500178{
akmhoque674b0b12014-05-20 14:33:28 -0500179 _LOG_DEBUG("Fib::clean called");
akmhoque53353462014-04-22 08:43:45 -0500180 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500181 ++it) {
akmhoquefdbddb12014-05-02 18:35:19 -0500182 std::cout << "Cancellling Scheduled event" << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500183 std::cout << "Name: " << it->getName() << "Seq num: " << it->getSeqNo() <<
184 std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500185 _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500186 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500187 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -0500188 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500189 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500190 //Remove entry from NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500191 if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
192 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500193 }
akmhoque157b0a42014-05-13 00:26:37 -0500194 else {
195 if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
196 nhit->getConnectingFaceUri()) {
197 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500198 }
199 }
akmhoque53353462014-04-22 08:43:45 -0500200 }
201 }
akmhoque157b0a42014-05-13 00:26:37 -0500202 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500203 m_table.clear();
204 }
205}
206
207int
akmhoque31d1d4b2014-05-05 22:08:14 -0500208Fib::getNumberOfFacesForName(NexthopList& nextHopList,
209 uint32_t maxFacesPerPrefix)
akmhoque53353462014-04-22 08:43:45 -0500210{
211 int endFace = 0;
akmhoque157b0a42014-05-13 00:26:37 -0500212 if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) {
akmhoque53353462014-04-22 08:43:45 -0500213 return nextHopList.getSize();
214 }
akmhoque157b0a42014-05-13 00:26:37 -0500215 else {
akmhoque53353462014-04-22 08:43:45 -0500216 return maxFacesPerPrefix;
217 }
218 return endFace;
219}
220
221void
akmhoque157b0a42014-05-13 00:26:37 -0500222Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500223 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500224{
akmhoquefdbddb12014-05-02 18:35:19 -0500225 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500226 it != nl.getNextHops().end(); ++it) {
227 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500228 //Remove FIB Entry from NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500229 if (!m_nlsr.getAdjacencyList().isNeighbor(name)) {
230 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500231 }
akmhoque157b0a42014-05-13 00:26:37 -0500232 else {
233 if (m_nlsr.getAdjacencyList().getAdjacent(name).getConnectingFaceUri() !=
234 it->getConnectingFaceUri()) {
235 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500236 }
237 }
akmhoque53353462014-04-22 08:43:45 -0500238 }
239 }
240}
241
242void
akmhoque157b0a42014-05-13 00:26:37 -0500243Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
244 uint64_t faceCost, uint64_t timeout)
245{
246 ndn::nfd::ControlParameters faceParameters;
247 faceParameters
248 .setUri(faceUri);
249
250 m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters,
251 ndn::bind(&Fib::registerPrefixInNfd, this,_1,
252 namePrefix, faceCost, timeout),
253 ndn::bind(&Fib::onFailure, this, _1, _2,
254 "Failed in name registration"));
255
256}
257
258void
259Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
260 const ndn::Name& namePrefix, uint64_t faceCost, uint64_t timeout)
akmhoquefdbddb12014-05-02 18:35:19 -0500261{
262 ndn::nfd::ControlParameters controlParameters;
263 controlParameters
akmhoque157b0a42014-05-13 00:26:37 -0500264 .setName(namePrefix)
265 .setFaceId(faceCreateResult.getFaceId())
266 .setCost(faceCost)
267 .setExpirationPeriod(ndn::time::milliseconds(timeout * 1000))
268 .setOrigin(128);
akmhoquefdbddb12014-05-02 18:35:19 -0500269 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
akmhoque157b0a42014-05-13 00:26:37 -0500270 ndn::bind(&Fib::onRegistration, this, _1,
271 "Successful in name registration",
272 faceCreateResult.getUri()),
akmhoquefdbddb12014-05-02 18:35:19 -0500273 ndn::bind(&Fib::onFailure, this, _1, _2,
274 "Failed in name registration"));
275}
akmhoque31d1d4b2014-05-05 22:08:14 -0500276
akmhoquefdbddb12014-05-02 18:35:19 -0500277void
akmhoque157b0a42014-05-13 00:26:37 -0500278Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500279{
akmhoque157b0a42014-05-13 00:26:37 -0500280 uint32_t faceId = m_faceMap.getFaceId(faceUri);
281 if (faceId > 0) {
282 ndn::nfd::ControlParameters controlParameters;
283 controlParameters
284 .setName(namePrefix)
285 .setFaceId(faceId)
286 .setOrigin(128);
287 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoquefdbddb12014-05-02 18:35:19 -0500288 ndn::bind(&Fib::onSuccess, this, _1,
289 "Successful in unregistering name"),
290 ndn::bind(&Fib::onFailure, this, _1, _2,
291 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500292 }
akmhoquefdbddb12014-05-02 18:35:19 -0500293}
294
295void
akmhoque157b0a42014-05-13 00:26:37 -0500296Fib::setStrategy(const ndn::Name& name, const std::string& strategy)
297{
298 ndn::nfd::ControlParameters parameters;
299 parameters
300 .setName(name)
301 .setStrategy(strategy);
302
303 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
304 bind(&Fib::onSuccess, this, _1,
305 "Successfully set strategy choice"),
306 bind(&Fib::onFailure, this, _1, _2,
307 "Failed to set strategy choice"));
308}
309
310void
311Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
312 const std::string& message, const std::string& faceUri)
313{
314 //std::cout << message << ": " << commandSuccessResult << std::endl;
315 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
316 m_faceMap.print();
317}
318
319
320void
akmhoque31d1d4b2014-05-05 22:08:14 -0500321Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
322 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500323{
akmhoque157b0a42014-05-13 00:26:37 -0500324 //std::cout << message << ": " << commandSuccessResult << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -0500325}
326
327void
akmhoque31d1d4b2014-05-05 22:08:14 -0500328Fib::onFailure(uint32_t code, const std::string& error,
329 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500330{
331 std::cout << message << ": " << error << " (code: " << code << ")";
332}
333
akmhoque674b0b12014-05-20 14:33:28 -0500334void
335Fib::writeLog()
336{
337 _LOG_DEBUG("-------------------FIB-----------------------------");
338 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
339 ++it) {
340 (*it).writeLog();
341 }
342}
akmhoquefdbddb12014-05-02 18:35:19 -0500343
344void
akmhoque53353462014-04-22 08:43:45 -0500345Fib::print()
346{
347 cout << "-------------------FIB-----------------------------" << endl;
348 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500349 ++it) {
akmhoque53353462014-04-22 08:43:45 -0500350 cout << (*it);
351 }
352}
353
354} //namespace nlsr