blob: c44d61df06814b7cffccc4dea7ae4d42b6597202 [file] [log] [blame]
Junxiao Shi5e5e4452015-09-24 16:56:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe4b22382018-06-10 14:37:24 -04002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi5e5e4452015-09-24 16:56:52 -07004 * 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.
10 *
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 "topology-tester.hpp"
Davide Pesavento284bd622019-03-31 02:10:02 -040027
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
Junxiao Shi5e5e4452015-09-24 16:56:52 -070029#include "face/generic-link-service.hpp"
30
Davide Pesaventoe4b22382018-06-10 14:37:24 -040031#include <ndn-cxx/encoding/encoding-buffer-fwd.hpp>
32
Junxiao Shi5e5e4452015-09-24 16:56:52 -070033namespace nfd {
34namespace fw {
35namespace tests {
36
Junxiao Shi6535f1e2015-10-08 13:02:18 -070037using face::GenericLinkService;
Davide Pesavento284bd622019-03-31 02:10:02 -040038using face::InternalClientTransport;
39using face::InternalForwarderTransport;
40
41TopologyLink::NodeTransport::NodeTransport(shared_ptr<Face> f, ReceiveProxy::Callback cb)
42 : face(std::move(f))
43 , transport(dynamic_cast<InternalForwarderTransport*>(face->getTransport()))
44 , proxy(std::move(cb))
45{
46 BOOST_ASSERT(transport != nullptr);
47}
Junxiao Shi5e5e4452015-09-24 16:56:52 -070048
Teng Liangf995f382017-04-04 22:09:39 +000049TopologyLink::TopologyLink(time::nanoseconds delay)
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070050{
51 this->setDelay(delay);
52}
53
54void
Teng Liangf995f382017-04-04 22:09:39 +000055TopologyLink::block(TopologyNode i, TopologyNode j)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070056{
Teng Liangf995f382017-04-04 22:09:39 +000057 m_transports.at(i).blockedDestinations.insert(j);
58}
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070059
Teng Liangf995f382017-04-04 22:09:39 +000060void
61TopologyLink::unblock(TopologyNode i, TopologyNode j)
62{
63 m_transports.at(i).blockedDestinations.erase(j);
64}
65
66void
67TopologyLink::setDelay(time::nanoseconds delay)
68{
69 BOOST_ASSERT(delay > time::nanoseconds::zero()); // zero delay does not work on macOS
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070070 m_delay = delay;
Junxiao Shi5e5e4452015-09-24 16:56:52 -070071}
72
73void
Junxiao Shicde37ad2015-12-24 01:02:05 -070074TopologyLink::addFace(TopologyNode i, shared_ptr<Face> face)
Junxiao Shi6535f1e2015-10-08 13:02:18 -070075{
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040076 auto receiveCb = [this, i] (const Block& packet) { transmit(i, packet); };
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040077 auto ret = m_transports.try_emplace(i, std::move(face), std::move(receiveCb));
Davide Pesavento284bd622019-03-31 02:10:02 -040078 BOOST_ASSERT(ret.second);
Teng Liangf995f382017-04-04 22:09:39 +000079
Davide Pesavento284bd622019-03-31 02:10:02 -040080 auto& node = ret.first->second;
81 node.transport->setPeer(&node.proxy);
Junxiao Shi5e5e4452015-09-24 16:56:52 -070082}
83
84void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040085TopologyLink::transmit(TopologyNode i, const Block& packet)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070086{
87 if (!m_isUp) {
88 return;
89 }
90
Teng Liangf995f382017-04-04 22:09:39 +000091 const auto& blockedDestinations = m_transports.at(i).blockedDestinations;
92
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040093 for (const auto& [node, transport] : m_transports) {
94 if (node == i || blockedDestinations.count(node) > 0) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -070095 continue;
96 }
97
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040098 getScheduler().schedule(m_delay, [packet, recipient = transport.transport] {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040099 recipient->receivePacket(packet);
100 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700101 }
102}
103
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600104TopologySingleLink::TopologySingleLink(shared_ptr<Face> forwarderFace)
Davide Pesavento284bd622019-03-31 02:10:02 -0400105 : m_face(std::move(forwarderFace))
106 , m_forwarderTransport(static_cast<InternalForwarderTransport*>(m_face->getTransport()))
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600107{
108}
109
110TopologyAppLink::TopologyAppLink(shared_ptr<Face> forwarderFace)
111 : TopologySingleLink(std::move(forwarderFace))
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700112 , m_clientTransport(make_shared<InternalClientTransport>())
113 , m_client(make_shared<ndn::Face>(m_clientTransport, getGlobalIoService()))
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700114{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700115 this->recover();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700116}
117
118void
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700119TopologyAppLink::fail()
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700120{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700121 m_clientTransport->connectToForwarder(nullptr);
122}
123
124void
125TopologyAppLink::recover()
126{
127 m_clientTransport->connectToForwarder(m_forwarderTransport);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700128}
129
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600130class TopologyBareLink::Observer : public face::InternalTransportBase
131{
132public:
133 explicit
134 Observer(std::vector<Block>& receivedPackets)
135 : m_receivedPackets(receivedPackets)
136 {
137 }
138
139 void
140 receivePacket(const Block& packet) final
141 {
142 m_receivedPackets.push_back(packet);
143 }
144
145private:
146 std::vector<Block>& m_receivedPackets;
147};
148
149TopologyBareLink::TopologyBareLink(shared_ptr<Face> forwarderFace)
150 : TopologySingleLink(std::move(forwarderFace))
151 , m_observer(make_unique<Observer>(sentPackets))
152{
153 m_forwarderTransport->setPeer(m_observer.get());
154}
155
156void
157TopologyBareLink::receivePacket(const Block& packet)
158{
159 m_forwarderTransport->receivePacket(packet);
160}
161
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000162class TopologyPcapLinkService : public GenericLinkService
163 , public TopologyPcap
164{
165private:
166 ///\todo #3941 call GenericLinkServiceCounters constructor in TopologyPcapLinkService constructor
167
168 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700169 doSendInterest(const Interest& interest) override
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000170 {
171 this->sentInterests.push_back(interest);
172 this->sentInterests.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700173 this->GenericLinkService::doSendInterest(interest);
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000174 }
175
176 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700177 doSendData(const Data& data) override
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000178 {
179 this->sentData.push_back(data);
180 this->sentData.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700181 this->GenericLinkService::doSendData(data);
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000182 }
183
184 void
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700185 doSendNack(const lp::Nack& nack) override
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000186 {
187 this->sentNacks.push_back(nack);
188 this->sentNacks.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700189 this->GenericLinkService::doSendNack(nack);
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000190 }
191};
192
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700193TopologyNode
194TopologyTester::addForwarder(const std::string& label)
195{
196 size_t i = m_forwarders.size();
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400197 m_forwarders.push_back(make_unique<TopologyForwarder>(label));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700198 return i;
199}
200
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600201shared_ptr<Face>
202TopologyTester::makeFace(TopologyNode i, const FaceUri& localUri, const FaceUri& remoteUri,
203 ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType)
204{
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600205 unique_ptr<GenericLinkService> service = m_wantPcap ? make_unique<TopologyPcapLinkService>() :
206 make_unique<GenericLinkService>();
207 auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri, scope, linkType);
208 auto face = make_shared<Face>(std::move(service), std::move(transport));
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400209 m_forwarders.at(i)->faceTable.add(face);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600210 return face;
211}
212
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700213shared_ptr<TopologyLink>
Teng Liangf995f382017-04-04 22:09:39 +0000214TopologyTester::addLink(const std::string& label, time::nanoseconds delay,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700215 std::initializer_list<TopologyNode> forwarders,
Teng Liangf995f382017-04-04 22:09:39 +0000216 ndn::nfd::LinkType linkType)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700217{
Yumin Xiaab497452016-05-10 20:23:24 +0800218 auto link = std::make_shared<TopologyLink>(delay);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700219 FaceUri remoteUri("topology://link/" + label);
Teng Liangf995f382017-04-04 22:09:39 +0000220 if (linkType == ndn::nfd::LINK_TYPE_NONE) {
221 linkType = forwarders.size() > 2 ? ndn::nfd::LINK_TYPE_MULTI_ACCESS :
222 ndn::nfd::LINK_TYPE_POINT_TO_POINT;
223 }
224 BOOST_ASSERT(forwarders.size() <= 2 || linkType != ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700225
226 for (TopologyNode i : forwarders) {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400227 FaceUri localUri("topology://" + m_forwarders.at(i)->label + "/" + label);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600228 auto face = makeFace(i, localUri, remoteUri, ndn::nfd::FACE_SCOPE_NON_LOCAL, linkType);
Davide Pesavento284bd622019-03-31 02:10:02 -0400229 link->addFace(i, std::move(face));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700230 }
231
232 m_links.push_back(link); // keep a shared_ptr so callers don't have to
233 return link;
234}
235
236shared_ptr<TopologyAppLink>
237TopologyTester::addAppFace(const std::string& label, TopologyNode i)
238{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400239 FaceUri localUri("topology://" + m_forwarders.at(i)->label + "/local/" + label);
240 FaceUri remoteUri("topology://" + m_forwarders.at(i)->label + "/app/" + label);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600241 auto face = makeFace(i, localUri, remoteUri, ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700242
Davide Pesavento284bd622019-03-31 02:10:02 -0400243 auto al = make_shared<TopologyAppLink>(std::move(face));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700244 m_appLinks.push_back(al); // keep a shared_ptr so callers don't have to
245 return al;
246}
247
248shared_ptr<TopologyAppLink>
249TopologyTester::addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost)
250{
251 shared_ptr<TopologyAppLink> al = this->addAppFace(label, i);
252 this->registerPrefix(i, al->getForwarderFace(), prefix, cost);
253 return al;
254}
255
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600256shared_ptr<TopologyBareLink>
257TopologyTester::addBareLink(const std::string& label, TopologyNode i, ndn::nfd::FaceScope scope,
258 ndn::nfd::LinkType linkType)
259{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400260 FaceUri localUri("topology://" + m_forwarders.at(i)->label + "/local/" + label);
261 FaceUri remoteUri("topology://" + m_forwarders.at(i)->label + "/bare/" + label);
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600262 auto face = makeFace(i, localUri, remoteUri, scope, linkType);
263
264 auto bl = make_shared<TopologyBareLink>(std::move(face));
265 m_bareLinks.push_back(bl); // keep a shared_ptr so callers don't have to
266 return bl;
267}
268
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000269TopologyPcap&
270TopologyTester::getPcap(const Face& face)
271{
272 return dynamic_cast<TopologyPcapLinkService&>(*face.getLinkService());
273}
274
275void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700276TopologyTester::registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost)
277{
278 Forwarder& forwarder = this->getForwarder(i);
279 Fib& fib = forwarder.getFib();
Ju Pand8315bf2019-07-31 06:59:07 +0000280 fib::Entry* entry = fib.insert(prefix).first;
281 fib.addOrUpdateNextHop(*entry, const_cast<Face&>(face), cost);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700282}
283
284void
Saurab Dulala6dec222019-04-01 00:15:10 -0500285TopologyTester::addEchoProducer(ndn::Face& face, const Name& prefix, time::nanoseconds replyDelay)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700286{
Saurab Dulala6dec222019-04-01 00:15:10 -0500287 BOOST_ASSERT(replyDelay >= 0_ns);
288
289 face.setInterestFilter(prefix, [=, &face] (const auto&, const auto& interest) {
290 auto data = makeData(interest.getName());
291 if (replyDelay == 0_ns) {
292 // reply immediately
293 face.put(*data);
294 }
295 else {
296 // delay the reply
297 getScheduler().schedule(replyDelay, [&face, data = std::move(data)] {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700298 face.put(*data);
299 });
Saurab Dulala6dec222019-04-01 00:15:10 -0500300 }
301 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700302}
303
304void
305TopologyTester::addIntervalConsumer(ndn::Face& face, const Name& prefix,
Teng Liangf995f382017-04-04 22:09:39 +0000306 time::nanoseconds interval, size_t n, int seq)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700307{
308 Name name(prefix);
Teng Liangf995f382017-04-04 22:09:39 +0000309 if (seq >= 0) {
310 name.appendSequenceNumber(seq);
311 ++seq;
312 }
313 else {
314 name.appendTimestamp();
315 }
316
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400317 auto interest = makeInterest(name);
Junxiao Shi9ede5bd2016-08-09 03:51:27 +0000318 face.expressInterest(*interest, nullptr, nullptr, nullptr);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700319
320 if (n > 1) {
Davide Pesavento3dade002019-03-19 11:29:56 -0600321 getScheduler().schedule(interval, [=, &face] {
322 addIntervalConsumer(face, prefix, interval, n - 1, seq);
323 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700324 }
325}
326
327} // namespace tests
328} // namespace fw
329} // namespace nfd