blob: 1272ab1d328eac22c90ad93d58e1121dc3946a6a [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
akmhoquefdbddb12014-05-02 18:35:19 -05002#include <cmath>
akmhoquec8a10f72014-04-25 18:42:55 -05003
4#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -05005#include "nexthop-list.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -05006#include "fib.hpp"
7
akmhoquec8a10f72014-04-25 18:42:55 -05008
akmhoque53353462014-04-22 08:43:45 -05009
10
11namespace nlsr {
12
13using namespace std;
14using namespace ndn;
15
16static bool
akmhoquefdbddb12014-05-02 18:35:19 -050017fibEntryNameCompare(const FibEntry& fibEntry, const string& name)
akmhoque53353462014-04-22 08:43:45 -050018{
akmhoquefdbddb12014-05-02 18:35:19 -050019 return fibEntry.getName() == name ;
akmhoque53353462014-04-22 08:43:45 -050020}
21
22void
23Fib::cancelScheduledExpiringEvent(Nlsr& pnlsr, EventId eid)
24{
25 pnlsr.getScheduler().cancelEvent(eid);
26}
27
28
29ndn::EventId
akmhoquefdbddb12014-05-02 18:35:19 -050030Fib::scheduleEntryRefreshing(Nlsr& pnlsr, const string& name, int32_t feSeqNum,
31 int32_t refreshTime)
akmhoque53353462014-04-22 08:43:45 -050032{
akmhoquefdbddb12014-05-02 18:35:19 -050033 std::cout << "Fib::scheduleEntryRefreshing Called" << std::endl;
34 std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
akmhoque53353462014-04-22 08:43:45 -050035 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
akmhoquefdbddb12014-05-02 18:35:19 -050036 ndn::bind(&Fib::refreshEntry, this,
37 boost::ref(pnlsr),
38 name, feSeqNum));
akmhoque53353462014-04-22 08:43:45 -050039}
40
41void
akmhoquefdbddb12014-05-02 18:35:19 -050042Fib::refreshEntry(Nlsr& nlsr, const string& name, int32_t feSeqNum)
akmhoque53353462014-04-22 08:43:45 -050043{
akmhoquefdbddb12014-05-02 18:35:19 -050044 std::cout << "Fib::refreshEntry Called" << std::endl;
45 std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
46 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
47 m_table.end(),
48 bind(&fibEntryNameCompare, _1, name));
49 if (it != m_table.end())
50 {
51 std::cout << "Entry found with Seq Num: " << feSeqNum << std::endl;
52 if (it->getSeqNo() == feSeqNum)
53 {
54 std::cout << "Refreshing the FIB entry" << std::endl;
55 for (std::list<NextHop>::iterator nhit =
56 (*it).getNexthopList().getNextHops().begin();
57 nhit != (*it).getNexthopList().getNextHops().end(); nhit++)
58 {
59 // add entry to NDN-FIB
60 registerPrefixInNfd(it->getName(), nhit->getConnectingFace(), std::ceil(nhit->getRouteCost()));
61 }
62
63 // increase sequence number and schedule refresh again
64 it->setSeqNo(feSeqNum + 1);
65 it->setExpiringEventId(scheduleEntryRefreshing(nlsr,
66 it->getName() ,
67 it->getSeqNo(),
68 m_refreshTime));
69
70 }
71 }
akmhoque53353462014-04-22 08:43:45 -050072}
73
74void
akmhoquefdbddb12014-05-02 18:35:19 -050075Fib::remove(Nlsr& pnlsr, const std::string& name)
akmhoque53353462014-04-22 08:43:45 -050076{
77 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -050078 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -050079 bind(&fibEntryNameCompare, _1, name));
akmhoque53353462014-04-22 08:43:45 -050080 if (it != m_table.end())
81 {
82 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -050083 (*it).getNexthopList().getNextHops().begin();
84 nhit != (*it).getNexthopList().getNextHops().end(); nhit++)
akmhoque53353462014-04-22 08:43:45 -050085 {
86 //remove entry from NDN-FIB
akmhoquefdbddb12014-05-02 18:35:19 -050087 if (!pnlsr.getAdjacencyList().isNeighbor(it->getName()))
88 {
89 unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
90 }
91 else
92 {
93 if(pnlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFace() !=
94 nhit->getConnectingFace())
95 {
96 unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
97 }
98 }
akmhoque53353462014-04-22 08:43:45 -050099 }
akmhoquefdbddb12014-05-02 18:35:19 -0500100 std::cout << "Cancellling Scheduled event" << std::endl;
101 std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500102 cancelScheduledExpiringEvent(pnlsr, (*it).getExpiringEventId());
103 m_table.erase(it);
104 }
105}
106
107
108void
akmhoquefdbddb12014-05-02 18:35:19 -0500109Fib::update(Nlsr& pnlsr, const string& name, NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500110{
111 std::cout << "Fib::updateFib Called" << std::endl;
112 int startFace = 0;
113 int endFace = getNumberOfFacesForName(nextHopList,
114 pnlsr.getConfParameter().getMaxFacesPerPrefix());
115 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
116 m_table.end(),
117 bind(&fibEntryNameCompare, _1, name));
118 if (it == m_table.end())
119 {
120 if (nextHopList.getSize() > 0)
121 {
122 nextHopList.sort();
123 FibEntry newEntry(name);
akmhoquefdbddb12014-05-02 18:35:19 -0500124 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500125 std::list<NextHop>::iterator nhit = nhl.begin();
126 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++)
127 {
akmhoquefdbddb12014-05-02 18:35:19 -0500128 newEntry.getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500129 //Add entry to NDN-FIB
akmhoquefdbddb12014-05-02 18:35:19 -0500130 registerPrefixInNfd(name, nhit->getConnectingFace(), std::ceil(nhit->getRouteCost()));
akmhoque53353462014-04-22 08:43:45 -0500131 }
akmhoquefdbddb12014-05-02 18:35:19 -0500132 newEntry.getNexthopList().sort();
akmhoque53353462014-04-22 08:43:45 -0500133 newEntry.setTimeToRefresh(m_refreshTime);
134 newEntry.setSeqNo(1);
135 newEntry.setExpiringEventId(scheduleEntryRefreshing(pnlsr,
136 name , 1, m_refreshTime));
137 m_table.push_back(newEntry);
138 }
139 }
140 else
141 {
142 std::cout << "Old FIB Entry" << std::endl;
143 if (nextHopList.getSize() > 0)
144 {
145 nextHopList.sort();
146 if (!it->isEqualNextHops(nextHopList))
147 {
akmhoquefdbddb12014-05-02 18:35:19 -0500148 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500149 std::list<NextHop>::iterator nhit = nhl.begin();
150 // Add first Entry to NDN-FIB
akmhoquefdbddb12014-05-02 18:35:19 -0500151 registerPrefixInNfd(name, nhit->getConnectingFace(), std::ceil(nhit->getRouteCost()));
152 removeHop(pnlsr, it->getNexthopList(), nhit->getConnectingFace(), name);
153 it->getNexthopList().reset();
154 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500155 ++startFace;
156 ++nhit;
157 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++)
158 {
akmhoquefdbddb12014-05-02 18:35:19 -0500159 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500160 //Add Entry to NDN_FIB
akmhoquefdbddb12014-05-02 18:35:19 -0500161 registerPrefixInNfd(name, nhit->getConnectingFace(), std::ceil(nhit->getRouteCost()));
akmhoque53353462014-04-22 08:43:45 -0500162 }
163 }
164 it->setTimeToRefresh(m_refreshTime);
akmhoquefdbddb12014-05-02 18:35:19 -0500165 std::cout << "Cancellling Scheduled event" << std::endl;
166 std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
akmhoque53353462014-04-22 08:43:45 -0500167 cancelScheduledExpiringEvent(pnlsr, it->getExpiringEventId());
168 it->setSeqNo(it->getSeqNo() + 1);
169 (*it).setExpiringEventId(scheduleEntryRefreshing(pnlsr,
170 it->getName() ,
171 it->getSeqNo(), m_refreshTime));
172 }
173 else
174 {
175 remove(pnlsr, name);
176 }
177 }
178}
179
180
181
182void
183Fib::clean(Nlsr& pnlsr)
184{
185 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
186 ++it)
187 {
akmhoquefdbddb12014-05-02 18:35:19 -0500188 std::cout << "Cancellling Scheduled event" << std::endl;
189 std::cout << "Name: " << it->getName() << "Seq num: " << it->getSeqNo() << std::endl;
190 cancelScheduledExpiringEvent(pnlsr, (*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500191 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -0500192 (*it).getNexthopList().getNextHops().begin();
193 nhit != (*it).getNexthopList().getNextHops().end(); nhit++)
akmhoque53353462014-04-22 08:43:45 -0500194 {
akmhoque53353462014-04-22 08:43:45 -0500195 //Remove entry from NDN-FIB
akmhoquefdbddb12014-05-02 18:35:19 -0500196 if (!pnlsr.getAdjacencyList().isNeighbor(it->getName()))
197 {
198 unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
199 }
200 else
201 {
202 if(pnlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFace() !=
203 nhit->getConnectingFace())
204 {
205 unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
206 }
207 }
akmhoque53353462014-04-22 08:43:45 -0500208 }
209 }
210 if (m_table.size() > 0)
211 {
212 m_table.clear();
213 }
214}
215
216int
akmhoquefdbddb12014-05-02 18:35:19 -0500217Fib::getNumberOfFacesForName(NexthopList& nextHopList, uint32_t maxFacesPerPrefix)
akmhoque53353462014-04-22 08:43:45 -0500218{
219 int endFace = 0;
220 if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix))
221 {
222 return nextHopList.getSize();
223 }
224 else
225 {
226 return maxFacesPerPrefix;
227 }
228 return endFace;
229}
230
231void
akmhoquefdbddb12014-05-02 18:35:19 -0500232Fib::removeHop(Nlsr& pnlsr, NexthopList& nl, uint32_t doNotRemoveHopFaceId,
233 const std::string& name)
akmhoque53353462014-04-22 08:43:45 -0500234{
akmhoquefdbddb12014-05-02 18:35:19 -0500235 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
236 it != nl.getNextHops().end(); ++it)
akmhoque53353462014-04-22 08:43:45 -0500237 {
238 if (it->getConnectingFace() != doNotRemoveHopFaceId)
239 {
240 //Remove FIB Entry from NDN-FIB
akmhoquefdbddb12014-05-02 18:35:19 -0500241 if (!pnlsr.getAdjacencyList().isNeighbor(name))
242 {
243 unregisterPrefixFromNfd(name, it->getConnectingFace());
244 }
245 else
246 {
247 if(pnlsr.getAdjacencyList().getAdjacent(name).getConnectingFace() !=
248 it->getConnectingFace())
249 {
250 unregisterPrefixFromNfd(name, it->getConnectingFace());
251 }
252 }
akmhoque53353462014-04-22 08:43:45 -0500253 }
254 }
255}
256
257void
akmhoquefdbddb12014-05-02 18:35:19 -0500258Fib::registerPrefixInNfd(const std::string& namePrefix, uint64_t faceId, uint64_t faceCost)
259{
260 ndn::nfd::ControlParameters controlParameters;
261 controlParameters
262 .setName(namePrefix)
263 .setCost(faceCost)
264 .setFaceId(faceId)
265 .setExpirationPeriod(ndn::time::milliseconds(m_refreshTime*1000))
266 .setOrigin(128);
267
268 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
269 ndn::bind(&Fib::onSuccess, this, _1,
270 "Successful in name registration"),
271 ndn::bind(&Fib::onFailure, this, _1, _2,
272 "Failed in name registration"));
273}
274
275void
276Fib::unregisterPrefixFromNfd(const std::string& namePrefix, uint64_t faceId)
277{
278 ndn::nfd::ControlParameters controlParameters;
279 controlParameters
280 .setName(namePrefix)
281 .setFaceId(faceId)
282 .setOrigin(128);
283
284 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
285 ndn::bind(&Fib::onSuccess, this, _1,
286 "Successful in unregistering name"),
287 ndn::bind(&Fib::onFailure, this, _1, _2,
288 "Failed in unregistering name"));
289}
290
291void
292Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult, const std::string& message)
293{
294 std::cout << message << ": " << commandSuccessResult << std::endl;
295}
296
297void
298Fib::onFailure(uint32_t code, const std::string& error, const std::string& message)
299{
300 std::cout << message << ": " << error << " (code: " << code << ")";
301}
302
303
304void
akmhoque53353462014-04-22 08:43:45 -0500305Fib::print()
306{
307 cout << "-------------------FIB-----------------------------" << endl;
308 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
309 ++it)
310 {
311 cout << (*it);
312 }
313}
314
315} //namespace nlsr