blob: 73421b05632837aa1a90df1efdd14327a8760908 [file] [log] [blame]
Vince Lehman41b173e2015-05-07 14:13:26 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesavento658fd852023-05-10 22:15:03 -04003 * Copyright (c) 2014-2023, The University of Memphis,
Vince Lehman41b173e2015-05-07 14:13:26 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070020 */
Vince Lehman41b173e2015-05-07 14:13:26 -050021
22#include "route/routing-table-calculator.hpp"
23
24#include "adjacency-list.hpp"
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040025#include "adjacent.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050026#include "lsdb.hpp"
27#include "nlsr.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050028#include "route/map.hpp"
29#include "route/routing-table.hpp"
30
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040031#include "tests/io-key-chain-fixture.hpp"
32#include "tests/test-common.hpp"
Vince Lehman41b173e2015-05-07 14:13:26 -050033
34namespace nlsr {
35namespace test {
36
Davide Pesavento658fd852023-05-10 22:15:03 -040037constexpr ndn::time::system_clock::time_point MAX_TIME = ndn::time::system_clock::time_point::max();
Junxiao Shi6593a432023-08-21 10:50:28 +000038static const ndn::Name ROUTER_A_NAME = "/ndn/site/%C1.Router/this-router";
39static const ndn::Name ROUTER_B_NAME = "/ndn/site/%C1.Router/b";
40static const ndn::Name ROUTER_C_NAME = "/ndn/site/%C1.Router/c";
41static const ndn::FaceUri ROUTER_A_FACE("udp4://10.0.0.1");
42static const ndn::FaceUri ROUTER_B_FACE("udp4://10.0.0.2");
43static const ndn::FaceUri ROUTER_C_FACE("udp4://10.0.0.3");
44constexpr double LINK_AB_COST = 5;
45constexpr double LINK_AC_COST = 10;
46constexpr double LINK_BC_COST = 17;
Vince Lehman41b173e2015-05-07 14:13:26 -050047
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040048class LinkStateCalculatorFixture : public IoKeyChainFixture
Vince Lehman41b173e2015-05-07 14:13:26 -050049{
50public:
51 LinkStateCalculatorFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040052 : face(m_io, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060053 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060054 , confProcessor(conf)
55 , nlsr(face, m_keyChain, conf)
56 , routingTable(nlsr.m_routingTable)
57 , lsdb(nlsr.m_lsdb)
Vince Lehman41b173e2015-05-07 14:13:26 -050058 {
59 setUpTopology();
60 }
61
62 // Triangle topology with routers A, B, C connected
63 void setUpTopology()
64 {
Junxiao Shi6593a432023-08-21 10:50:28 +000065 Adjacent a(ROUTER_A_NAME, ROUTER_A_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0);
66 Adjacent b(ROUTER_B_NAME, ROUTER_B_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0);
67 Adjacent c(ROUTER_C_NAME, ROUTER_C_FACE, 0, Adjacent::STATUS_ACTIVE, 0, 0);
Vince Lehman41b173e2015-05-07 14:13:26 -050068
69 // Router A
70 b.setLinkCost(LINK_AB_COST);
71 c.setLinkCost(LINK_AC_COST);
72
Ashlesh Gawande85998a12017-12-07 22:22:13 -060073 AdjacencyList& adjacencyListA = conf.getAdjacencyList();
Vince Lehman41b173e2015-05-07 14:13:26 -050074 adjacencyListA.insert(b);
75 adjacencyListA.insert(c);
76
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060077 AdjLsa adjA(a.getName(), 1, MAX_TIME, 2, adjacencyListA);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070078 lsdb.installLsa(std::make_shared<AdjLsa>(adjA));
Vince Lehman41b173e2015-05-07 14:13:26 -050079
80 // Router B
81 a.setLinkCost(LINK_AB_COST);
82 c.setLinkCost(LINK_BC_COST);
83
84 AdjacencyList adjacencyListB;
85 adjacencyListB.insert(a);
86 adjacencyListB.insert(c);
87
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060088 AdjLsa adjB(b.getName(), 1, MAX_TIME, 2, adjacencyListB);
Ashlesh Gawande57a87172020-05-09 19:47:06 -070089 lsdb.installLsa(std::make_shared<AdjLsa>(adjB));
Vince Lehman41b173e2015-05-07 14:13:26 -050090
91 // Router C
92 a.setLinkCost(LINK_AC_COST);
93 b.setLinkCost(LINK_BC_COST);
94
95 AdjacencyList adjacencyListC;
96 adjacencyListC.insert(a);
97 adjacencyListC.insert(b);
98
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060099 AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC);
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700100 lsdb.installLsa(std::make_shared<AdjLsa>(adjC));
Vince Lehman41b173e2015-05-07 14:13:26 -0500101
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700102 auto lsaRange = lsdb.getLsdbIterator<AdjLsa>();
103 map.createFromAdjLsdb(lsaRange.first, lsaRange.second);
Vince Lehman41b173e2015-05-07 14:13:26 -0500104 }
105
106public:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000107 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600108 ConfParameter conf;
109 DummyConfFileProcessor confProcessor;
Vince Lehman41b173e2015-05-07 14:13:26 -0500110 Nlsr nlsr;
111 Map map;
112
113 RoutingTable& routingTable;
114 Lsdb& lsdb;
Vince Lehman41b173e2015-05-07 14:13:26 -0500115};
116
Vince Lehman41b173e2015-05-07 14:13:26 -0500117BOOST_FIXTURE_TEST_SUITE(TestLinkStateRoutingCalculator, LinkStateCalculatorFixture)
118
119BOOST_AUTO_TEST_CASE(Basic)
120{
121 LinkStateRoutingTableCalculator calculator(map.getMapSize());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700122 calculator.calculatePath(map, routingTable, conf, lsdb);
Vince Lehman41b173e2015-05-07 14:13:26 -0500123
124 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
125 BOOST_REQUIRE(entryB != nullptr);
126
127 // Router A should be able to get to B through B and to B through C
128 NexthopList& bHopList = entryB->getNexthopList();
129 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
130
131 for (const NextHop& hop : bHopList) {
Junxiao Shi6593a432023-08-21 10:50:28 +0000132 auto faceUri = hop.getConnectingFaceUri();
Vince Lehman41b173e2015-05-07 14:13:26 -0500133 uint64_t cost = hop.getRouteCostAsAdjustedInteger();
134
135 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == LINK_AB_COST) ||
136 (faceUri == ROUTER_C_FACE && cost == LINK_AC_COST + LINK_BC_COST));
137
138 }
139
140 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
141 BOOST_REQUIRE(entryC != nullptr);
142
143 // Router A should be able to get to C through C and to C through B
144 NexthopList& cHopList = entryC->getNexthopList();
145 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
146
147 for (const NextHop& hop : cHopList) {
Junxiao Shi6593a432023-08-21 10:50:28 +0000148 auto faceUri = hop.getConnectingFaceUri();
Vince Lehman41b173e2015-05-07 14:13:26 -0500149 uint64_t cost = hop.getRouteCostAsAdjustedInteger();
150
151 BOOST_CHECK((faceUri == ROUTER_C_FACE && cost == LINK_AC_COST) ||
152 (faceUri == ROUTER_B_FACE && cost == LINK_AB_COST + LINK_BC_COST));
153 }
154}
155
156BOOST_AUTO_TEST_CASE(Asymmetric)
157{
158 // Asymmetric link cost between B and C
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700159 auto lsa = nlsr.m_lsdb.findLsa<AdjLsa>(ndn::Name(ROUTER_B_NAME));
Vince Lehman41b173e2015-05-07 14:13:26 -0500160 BOOST_REQUIRE(lsa != nullptr);
161
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800162 auto c = lsa->m_adl.findAdjacent(ROUTER_C_NAME);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600163 BOOST_REQUIRE(c != conf.getAdjacencyList().end());
Vince Lehman41b173e2015-05-07 14:13:26 -0500164
165 double higherLinkCost = LINK_BC_COST + 1;
166 c->setLinkCost(higherLinkCost);
167
168 // Calculation should consider the link between B and C as having cost = higherLinkCost
169 LinkStateRoutingTableCalculator calculator(map.getMapSize());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700170 calculator.calculatePath(map, routingTable, conf, lsdb);
Vince Lehman41b173e2015-05-07 14:13:26 -0500171
172 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
173 BOOST_REQUIRE(entryB != nullptr);
174
175 // Router A should be able to get to B through B and to B through C
176 NexthopList& bHopList = entryB->getNexthopList();
177 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
178
179 for (const NextHop& hop : bHopList) {
Junxiao Shi6593a432023-08-21 10:50:28 +0000180 auto faceUri = hop.getConnectingFaceUri();
Vince Lehman41b173e2015-05-07 14:13:26 -0500181 uint64_t cost = hop.getRouteCostAsAdjustedInteger();
182
183 BOOST_CHECK((faceUri == ROUTER_B_FACE && cost == LINK_AB_COST) ||
184 (faceUri == ROUTER_C_FACE && cost == LINK_AC_COST + higherLinkCost));
185
186 }
187
188 RoutingTableEntry* entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
189 BOOST_REQUIRE(entryC != nullptr);
190
191 // Router A should be able to get to C through C and to C through B
192 NexthopList& cHopList = entryC->getNexthopList();
193 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
194
195 for (const NextHop& hop : cHopList) {
Junxiao Shi6593a432023-08-21 10:50:28 +0000196 auto faceUri = hop.getConnectingFaceUri();
Vince Lehman41b173e2015-05-07 14:13:26 -0500197 uint64_t cost = hop.getRouteCostAsAdjustedInteger();
198
199 BOOST_CHECK((faceUri == ROUTER_C_FACE && cost == LINK_AC_COST) ||
200 (faceUri == ROUTER_B_FACE && cost == LINK_AB_COST + higherLinkCost));
201 }
202}
203
dulalsaurabd0816a32019-07-26 13:11:24 +0000204BOOST_AUTO_TEST_CASE(NonAdjacentCost)
Vince Lehman41b173e2015-05-07 14:13:26 -0500205{
206 // Asymmetric link cost between B and C
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700207 auto lsa = nlsr.m_lsdb.findLsa<AdjLsa>(ROUTER_B_NAME);
Vince Lehman41b173e2015-05-07 14:13:26 -0500208 BOOST_REQUIRE(lsa != nullptr);
209
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800210 auto c = lsa->m_adl.findAdjacent(ROUTER_C_NAME);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600211 BOOST_REQUIRE(c != conf.getAdjacencyList().end());
Vince Lehman41b173e2015-05-07 14:13:26 -0500212
dulalsaurabd0816a32019-07-26 13:11:24 +0000213 // Break the link between B - C by setting it to a NON_ADJACENT_COST.
214 c->setLinkCost(Adjacent::NON_ADJACENT_COST);
Vince Lehman41b173e2015-05-07 14:13:26 -0500215
216 // Calculation should consider the link between B and C as down
217 LinkStateRoutingTableCalculator calculator(map.getMapSize());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700218 calculator.calculatePath(map, routingTable, conf, lsdb);
Vince Lehman41b173e2015-05-07 14:13:26 -0500219
220 // Router A should be able to get to B through B but not through C
221 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
222 BOOST_REQUIRE(entryB != nullptr);
223
dulalsaurabd0816a32019-07-26 13:11:24 +0000224 auto bHopList = entryB->getNexthopList();
Vince Lehman41b173e2015-05-07 14:13:26 -0500225 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 1);
226
dulalsaurabd0816a32019-07-26 13:11:24 +0000227 const auto nextHopForB = bHopList.getNextHops().begin();
Vince Lehman41b173e2015-05-07 14:13:26 -0500228
dulalsaurabd0816a32019-07-26 13:11:24 +0000229 BOOST_CHECK(nextHopForB->getConnectingFaceUri() == ROUTER_B_FACE &&
230 nextHopForB->getRouteCostAsAdjustedInteger() == LINK_AB_COST);
Vince Lehman41b173e2015-05-07 14:13:26 -0500231
232 // Router A should be able to get to C through C but not through B
dulalsaurabd0816a32019-07-26 13:11:24 +0000233 auto entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
Vince Lehman41b173e2015-05-07 14:13:26 -0500234 BOOST_REQUIRE(entryC != nullptr);
235
236 NexthopList& cHopList = entryC->getNexthopList();
237 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 1);
238
dulalsaurabd0816a32019-07-26 13:11:24 +0000239 const auto nextHopForC = cHopList.getNextHops().begin();
Vince Lehman41b173e2015-05-07 14:13:26 -0500240
dulalsaurabd0816a32019-07-26 13:11:24 +0000241 BOOST_CHECK(nextHopForC->getConnectingFaceUri() == ROUTER_C_FACE &&
242 nextHopForC->getRouteCostAsAdjustedInteger() == LINK_AC_COST);
243}
244
245BOOST_AUTO_TEST_CASE(AsymmetricZeroCostLink)
246{
247 // Asymmetric and zero link cost between B - C, and B - A.
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700248 auto lsaB = nlsr.m_lsdb.findLsa<AdjLsa>(ROUTER_B_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000249 BOOST_REQUIRE(lsaB != nullptr);
250
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800251 auto c = lsaB->m_adl.findAdjacent(ROUTER_C_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000252 BOOST_REQUIRE(c != conf.getAdjacencyList().end());
253 // Re-adjust link cost to 0 from B-C. However, this should not set B-C cost 0 because C-B
254 // cost is greater that 0 i.e. 17
255 c->setLinkCost(0);
256
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800257 auto a = lsaB->m_adl.findAdjacent(ROUTER_A_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000258 BOOST_REQUIRE(a != conf.getAdjacencyList().end());
259
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700260 auto lsaA = nlsr.m_lsdb.findLsa<AdjLsa>(ROUTER_A_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000261 BOOST_REQUIRE(lsaA != nullptr);
262
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800263 auto b = lsaA->m_adl.findAdjacent(ROUTER_B_NAME);
dulalsaurabd0816a32019-07-26 13:11:24 +0000264 BOOST_REQUIRE(b != conf.getAdjacencyList().end());
265
266 // Re-adjust link cost to 0 from both the direction i.e B-A and A-B
267 a->setLinkCost(0);
268 b->setLinkCost(0);
269
270 // Calculation should consider 0 link-cost between B and C
271 LinkStateRoutingTableCalculator calculator(map.getMapSize());
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700272 calculator.calculatePath(map, routingTable, conf, lsdb);
dulalsaurabd0816a32019-07-26 13:11:24 +0000273
274 // Router A should be able to get to B through B and C
275 RoutingTableEntry* entryB = routingTable.findRoutingTableEntry(ROUTER_B_NAME);
276 BOOST_REQUIRE(entryB != nullptr);
277
278 // Node can have neighbors with zero cost, so the nexthop count should be 2
279 NexthopList& bHopList = entryB->getNexthopList();
280 BOOST_REQUIRE_EQUAL(bHopList.getNextHops().size(), 2);
281
282 const auto nextHopForB = bHopList.getNextHops().begin();
283 // Check if the next hop via B is through A or not after the cost adjustment
284 BOOST_CHECK(nextHopForB->getConnectingFaceUri() == ROUTER_B_FACE &&
285 nextHopForB->getRouteCostAsAdjustedInteger() == 0);
286
287 // Router A should be able to get to C through C and B
288 auto entryC = routingTable.findRoutingTableEntry(ROUTER_C_NAME);
289 BOOST_REQUIRE(entryC != nullptr);
290
291 NexthopList& cHopList = entryC->getNexthopList();
292 BOOST_REQUIRE_EQUAL(cHopList.getNextHops().size(), 2);
293
294 const auto nextHopForC = cHopList.getNextHops().begin();
295 // Check if the nextHop from C is via A or not
296 BOOST_CHECK(nextHopForC->getConnectingFaceUri() == ROUTER_C_FACE &&
297 nextHopForC->getRouteCostAsAdjustedInteger() == LINK_AC_COST);
298
Vince Lehman41b173e2015-05-07 14:13:26 -0500299}
300
301BOOST_AUTO_TEST_SUITE_END()
302
Nick Gordonfad8e252016-08-11 14:21:38 -0500303} // namespace test
304} // namespace nlsr