blob: 067e2b762f8172a7ea2587df8ab05ceba2f6fed7 [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/*
ashiqopu3ad49db2018-10-20 22:38:47 +00003 * Copyright (c) 2014-2019, 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); };
Junxiao Shi5e5e4452015-09-24 16:56:52 -070077
Davide Pesavento284bd622019-03-31 02:10:02 -040078 auto ret = m_transports.emplace(std::piecewise_construct,
79 std::forward_as_tuple(i),
80 std::forward_as_tuple(std::move(face), std::move(receiveCb)));
81 BOOST_ASSERT(ret.second);
Teng Liangf995f382017-04-04 22:09:39 +000082
Davide Pesavento284bd622019-03-31 02:10:02 -040083 auto& node = ret.first->second;
84 node.transport->setPeer(&node.proxy);
Junxiao Shi5e5e4452015-09-24 16:56:52 -070085}
86
87void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040088TopologyLink::transmit(TopologyNode i, const Block& packet)
Junxiao Shi5e5e4452015-09-24 16:56:52 -070089{
90 if (!m_isUp) {
91 return;
92 }
93
Teng Liangf995f382017-04-04 22:09:39 +000094 const auto& blockedDestinations = m_transports.at(i).blockedDestinations;
95
Davide Pesavento97210d52016-10-14 15:45:48 +020096 for (const auto& p : m_transports) {
Teng Liangf995f382017-04-04 22:09:39 +000097 if (p.first == i || blockedDestinations.count(p.first) > 0) {
Junxiao Shi5e5e4452015-09-24 16:56:52 -070098 continue;
99 }
100
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400101 getScheduler().schedule(m_delay, [packet, recipient = p.second.transport] {
102 recipient->receivePacket(packet);
103 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700104 }
105}
106
Junxiao Shicde37ad2015-12-24 01:02:05 -0700107TopologyAppLink::TopologyAppLink(shared_ptr<Face> forwarderFace)
Davide Pesavento284bd622019-03-31 02:10:02 -0400108 : m_face(std::move(forwarderFace))
109 , m_forwarderTransport(static_cast<InternalForwarderTransport*>(m_face->getTransport()))
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700110 , m_clientTransport(make_shared<InternalClientTransport>())
111 , m_client(make_shared<ndn::Face>(m_clientTransport, getGlobalIoService()))
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700112{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700113 this->recover();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700114}
115
116void
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700117TopologyAppLink::fail()
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700118{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700119 m_clientTransport->connectToForwarder(nullptr);
120}
121
122void
123TopologyAppLink::recover()
124{
125 m_clientTransport->connectToForwarder(m_forwarderTransport);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700126}
127
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000128class TopologyPcapLinkService : public GenericLinkService
129 , public TopologyPcap
130{
131private:
132 ///\todo #3941 call GenericLinkServiceCounters constructor in TopologyPcapLinkService constructor
133
134 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000135 doSendInterest(const Interest& interest, const EndpointId& endpointId) override
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000136 {
137 this->sentInterests.push_back(interest);
138 this->sentInterests.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
ashiqopu075bb7d2019-03-10 01:38:21 +0000139 this->GenericLinkService::doSendInterest(interest, endpointId);
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000140 }
141
142 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000143 doSendData(const Data& data, const EndpointId& endpointId) override
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000144 {
145 this->sentData.push_back(data);
146 this->sentData.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
ashiqopu075bb7d2019-03-10 01:38:21 +0000147 this->GenericLinkService::doSendData(data, endpointId);
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000148 }
149
150 void
ashiqopu075bb7d2019-03-10 01:38:21 +0000151 doSendNack(const lp::Nack& nack, const EndpointId& endpointId) override
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000152 {
153 this->sentNacks.push_back(nack);
154 this->sentNacks.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now()));
ashiqopu075bb7d2019-03-10 01:38:21 +0000155 this->GenericLinkService::doSendNack(nack, endpointId);
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000156 }
157};
158
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700159TopologyNode
160TopologyTester::addForwarder(const std::string& label)
161{
162 size_t i = m_forwarders.size();
Davide Pesavento459cf192016-03-03 02:20:12 +0100163 m_forwarders.push_back(make_unique<Forwarder>());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700164 m_forwarderLabels.push_back(label);
165 BOOST_ASSERT(m_forwarders.size() == m_forwarderLabels.size());
166 return i;
167}
168
169shared_ptr<TopologyLink>
Teng Liangf995f382017-04-04 22:09:39 +0000170TopologyTester::addLink(const std::string& label, time::nanoseconds delay,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700171 std::initializer_list<TopologyNode> forwarders,
Teng Liangf995f382017-04-04 22:09:39 +0000172 ndn::nfd::LinkType linkType)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700173{
Yumin Xiaab497452016-05-10 20:23:24 +0800174 auto link = std::make_shared<TopologyLink>(delay);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700175 FaceUri remoteUri("topology://link/" + label);
Teng Liangf995f382017-04-04 22:09:39 +0000176 if (linkType == ndn::nfd::LINK_TYPE_NONE) {
177 linkType = forwarders.size() > 2 ? ndn::nfd::LINK_TYPE_MULTI_ACCESS :
178 ndn::nfd::LINK_TYPE_POINT_TO_POINT;
179 }
180 BOOST_ASSERT(forwarders.size() <= 2 || linkType != ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700181
182 for (TopologyNode i : forwarders) {
183 Forwarder& forwarder = this->getForwarder(i);
184 FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/" + label);
185
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000186 unique_ptr<GenericLinkService> service = m_wantPcap ? make_unique<TopologyPcapLinkService>() :
187 make_unique<GenericLinkService>();
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700188 auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700189 ndn::nfd::FACE_SCOPE_NON_LOCAL, linkType);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700190 auto face = make_shared<Face>(std::move(service), std::move(transport));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700191
Junxiao Shicde37ad2015-12-24 01:02:05 -0700192 forwarder.addFace(face);
Davide Pesavento284bd622019-03-31 02:10:02 -0400193 link->addFace(i, std::move(face));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700194 }
195
196 m_links.push_back(link); // keep a shared_ptr so callers don't have to
197 return link;
198}
199
200shared_ptr<TopologyAppLink>
201TopologyTester::addAppFace(const std::string& label, TopologyNode i)
202{
203 Forwarder& forwarder = this->getForwarder(i);
204 FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/local/" + label);
205 FaceUri remoteUri("topology://" + m_forwarderLabels.at(i) + "/app/" + label);
206
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000207 unique_ptr<GenericLinkService> service = m_wantPcap ? make_unique<TopologyPcapLinkService>() :
208 make_unique<GenericLinkService>();
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700209 auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700210 ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LINK_TYPE_POINT_TO_POINT);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700211 auto face = make_shared<Face>(std::move(service), std::move(transport));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700212
Junxiao Shicde37ad2015-12-24 01:02:05 -0700213 forwarder.addFace(face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700214
Davide Pesavento284bd622019-03-31 02:10:02 -0400215 auto al = make_shared<TopologyAppLink>(std::move(face));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700216 m_appLinks.push_back(al); // keep a shared_ptr so callers don't have to
217 return al;
218}
219
220shared_ptr<TopologyAppLink>
221TopologyTester::addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost)
222{
223 shared_ptr<TopologyAppLink> al = this->addAppFace(label, i);
224 this->registerPrefix(i, al->getForwarderFace(), prefix, cost);
225 return al;
226}
227
228void
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000229TopologyTester::enablePcap(bool isEnabled)
230{
231 m_wantPcap = isEnabled;
232}
233
234TopologyPcap&
235TopologyTester::getPcap(const Face& face)
236{
237 return dynamic_cast<TopologyPcapLinkService&>(*face.getLinkService());
238}
239
240void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700241TopologyTester::registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost)
242{
243 Forwarder& forwarder = this->getForwarder(i);
244 Fib& fib = forwarder.getFib();
ashiqopu3ad49db2018-10-20 22:38:47 +0000245 fib.insert(prefix).first->addOrUpdateNextHop(const_cast<Face&>(face), 0, cost);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700246}
247
248void
Saurab Dulala6dec222019-04-01 00:15:10 -0500249TopologyTester::addEchoProducer(ndn::Face& face, const Name& prefix, time::nanoseconds replyDelay)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700250{
Saurab Dulala6dec222019-04-01 00:15:10 -0500251 BOOST_ASSERT(replyDelay >= 0_ns);
252
253 face.setInterestFilter(prefix, [=, &face] (const auto&, const auto& interest) {
254 auto data = makeData(interest.getName());
255 if (replyDelay == 0_ns) {
256 // reply immediately
257 face.put(*data);
258 }
259 else {
260 // delay the reply
261 getScheduler().schedule(replyDelay, [&face, data = std::move(data)] {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700262 face.put(*data);
263 });
Saurab Dulala6dec222019-04-01 00:15:10 -0500264 }
265 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700266}
267
268void
269TopologyTester::addIntervalConsumer(ndn::Face& face, const Name& prefix,
Teng Liangf995f382017-04-04 22:09:39 +0000270 time::nanoseconds interval, size_t n, int seq)
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700271{
272 Name name(prefix);
Teng Liangf995f382017-04-04 22:09:39 +0000273 if (seq >= 0) {
274 name.appendSequenceNumber(seq);
275 ++seq;
276 }
277 else {
278 name.appendTimestamp();
279 }
280
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400281 auto interest = makeInterest(name);
Junxiao Shi9ede5bd2016-08-09 03:51:27 +0000282 face.expressInterest(*interest, nullptr, nullptr, nullptr);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700283
284 if (n > 1) {
Davide Pesavento3dade002019-03-19 11:29:56 -0600285 getScheduler().schedule(interval, [=, &face] {
286 addIntervalConsumer(face, prefix, interval, n - 1, seq);
287 });
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700288 }
289}
290
291} // namespace tests
292} // namespace fw
293} // namespace nfd