blob: e82705ce8ab20225585395200610598e9ee6644e [file] [log] [blame]
Junxiao Shi80ee7cb2014-12-14 10:53:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -05002/*
Davide Pesavento0a05f7a2023-10-16 20:28:06 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Junxiao Shi80ee7cb2014-12-14 10:53:05 -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/** \file
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040027 * \brief Allows testing forwarding in a network topology.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070028 */
29
Davide Pesavento97210d52016-10-14 15:45:48 +020030#ifndef NFD_TESTS_DAEMON_FW_TOPOLOGY_TESTER_HPP
31#define NFD_TESTS_DAEMON_FW_TOPOLOGY_TESTER_HPP
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070032
Junxiao Shi6535f1e2015-10-08 13:02:18 -070033#include "face/internal-transport.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070034#include "face/face.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070035#include "fw/strategy.hpp"
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000036#include "choose-strategy.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070037#include "tests/test-common.hpp"
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070038
Davide Pesavento97210d52016-10-14 15:45:48 +020039#include <ndn-cxx/face.hpp>
40
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040041namespace nfd::tests {
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070042
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040043/** \brief Identifies a node (forwarder) in the topology.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070044 */
45typedef size_t TopologyNode;
46
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040047/** \brief Represents a network link in the topology which connects two or more nodes.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070048 */
Junxiao Shi6535f1e2015-10-08 13:02:18 -070049class TopologyLink : noncopyable
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070050{
51public:
Junxiao Shi6535f1e2015-10-08 13:02:18 -070052 explicit
Teng Liangf995f382017-04-04 22:09:39 +000053 TopologyLink(time::nanoseconds delay);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070054
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040055 /** \brief Fail the link, cause packets to be dropped silently.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070056 */
57 void
58 fail()
59 {
60 m_isUp = false;
61 }
62
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040063 /** \brief Recover the link from a failure.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070064 */
65 void
66 recover()
67 {
68 m_isUp = true;
69 }
70
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040071 /** \brief Block transmission from i to j.
Teng Liangf995f382017-04-04 22:09:39 +000072 *
73 * Packets transmitted by i would not be delivered to j. Packets from j to i are unaffected.
74 * This can be used to simulate a wireless channel.
75 */
76 void
77 block(TopologyNode i, TopologyNode j);
78
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040079 /** \brief Unblock transmission from i to j.
Teng Liangf995f382017-04-04 22:09:39 +000080 */
81 void
82 unblock(TopologyNode i, TopologyNode j);
83
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040084 /** \brief Change the link delay.
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070085 * \param delay link delay, must be positive
86 */
87 void
Teng Liangf995f382017-04-04 22:09:39 +000088 setDelay(time::nanoseconds delay);
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070089
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040090 /** \brief Attach a face to the link.
Junxiao Shicde37ad2015-12-24 01:02:05 -070091 * \param i forwarder index
92 * \param face a Face with InternalForwarderTransport
93 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -070094 void
Junxiao Shicde37ad2015-12-24 01:02:05 -070095 addFace(TopologyNode i, shared_ptr<Face> face);
Junxiao Shi5e5e4452015-09-24 16:56:52 -070096
Junxiao Shi80ee7cb2014-12-14 10:53:05 -070097 /** \return a face of forwarder \p i which is attached to this link
98 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -070099 Face&
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700100 getFace(TopologyNode i)
101 {
Teng Liangf995f382017-04-04 22:09:39 +0000102 return *m_transports.at(i).face;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700103 }
104
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700105private:
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700106 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400107 transmit(TopologyNode i, const Block& packet);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700108
109private:
Davide Pesavento284bd622019-03-31 02:10:02 -0400110 bool m_isUp = true;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700111 time::nanoseconds m_delay;
Teng Liangf995f382017-04-04 22:09:39 +0000112
Davide Pesavento0a05f7a2023-10-16 20:28:06 -0400113 class ReceiveProxy final : public face::InternalTransportBase
Teng Liangf995f382017-04-04 22:09:39 +0000114 {
Davide Pesavento284bd622019-03-31 02:10:02 -0400115 public:
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400116 using Callback = std::function<void(const Block&)>;
Davide Pesavento284bd622019-03-31 02:10:02 -0400117
118 explicit
119 ReceiveProxy(Callback cb)
120 : m_cb(std::move(cb))
121 {
122 }
123
124 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400125 receivePacket(const Block& packet) final
Davide Pesavento284bd622019-03-31 02:10:02 -0400126 {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400127 m_cb(packet);
Davide Pesavento284bd622019-03-31 02:10:02 -0400128 }
129
130 private:
131 Callback m_cb;
132 };
133
134 class NodeTransport
135 {
136 public:
137 NodeTransport(shared_ptr<Face> face, ReceiveProxy::Callback receiveCallback);
138
139 public:
Teng Liangf995f382017-04-04 22:09:39 +0000140 shared_ptr<Face> face;
Davide Pesavento284bd622019-03-31 02:10:02 -0400141 face::InternalForwarderTransport* transport;
142 ReceiveProxy proxy;
Teng Liangf995f382017-04-04 22:09:39 +0000143 std::set<TopologyNode> blockedDestinations;
144 };
Davide Pesavento284bd622019-03-31 02:10:02 -0400145
Teng Liangf995f382017-04-04 22:09:39 +0000146 std::unordered_map<TopologyNode, NodeTransport> m_transports;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700147};
148
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400149/** \brief Represents a link on a single forwarder.
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600150 */
151class TopologySingleLink : noncopyable
152{
153public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400154 /** \brief Constructor.
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600155 * \param forwarderFace a Face with InternalForwarderTransport
156 */
157 explicit
158 TopologySingleLink(shared_ptr<Face> forwarderFace);
159
160 /** \return face on forwarder side
161 */
162 Face&
163 getForwarderFace()
164 {
165 return *m_face;
166 }
167
168protected:
169 shared_ptr<Face> m_face;
170 face::InternalForwarderTransport* m_forwarderTransport;
171};
172
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400173/** \brief Represents a link to a local application.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700174 */
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600175class TopologyAppLink : public TopologySingleLink
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700176{
177public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400178 /** \brief Constructor.
Junxiao Shicde37ad2015-12-24 01:02:05 -0700179 * \param forwarderFace a Face with InternalForwarderTransport
180 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700181 explicit
Junxiao Shicde37ad2015-12-24 01:02:05 -0700182 TopologyAppLink(shared_ptr<Face> forwarderFace);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700183
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400184 /** \brief Fail the link, cause packets to be dropped silently.
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700185 */
186 void
187 fail();
188
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400189 /** \brief Recover the link from a failure.
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700190 */
191 void
192 recover();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700193
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700194 /** \return face on application side
195 */
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700196 ndn::Face&
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700197 getClientFace()
198 {
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700199 return *m_client;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700200 }
201
202private:
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700203 shared_ptr<face::InternalClientTransport> m_clientTransport;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700204 shared_ptr<ndn::Face> m_client;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700205};
206
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400207/** \brief Allows the test case to inject and observe L2 packets on a link.
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600208 */
209class TopologyBareLink : public TopologySingleLink
210{
211public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400212 /** \brief Constructor.
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600213 * \param forwarderFace a Face with InternalForwarderTransport
214 */
215 explicit
216 TopologyBareLink(shared_ptr<Face> forwarderFace);
217
218 void
219 receivePacket(const Block& packet);
220
221public:
222 std::vector<Block> sentPackets;
223
224private:
225 class Observer;
226 unique_ptr<Observer> m_observer;
227};
228
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400229/** \brief Captured packets on a face.
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000230 */
231class TopologyPcap : noncopyable
232{
233public:
234 std::vector<Interest> sentInterests;
235 std::vector<Data> sentData;
236 std::vector<lp::Nack> sentNacks;
237};
238
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400239/** \brief Captured packet timestamp tag.
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000240 */
Davide Pesavento412c9822021-07-02 00:21:05 -0400241using TopologyPcapTimestamp = ndn::SimpleTag<time::steady_clock::time_point, 0>;
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000242
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400243/** \brief Builds a topology for forwarding tests.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700244 */
245class TopologyTester : noncopyable
246{
247public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400248 /** \brief Creates a forwarder.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700249 * \return index of new forwarder
250 */
251 TopologyNode
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700252 addForwarder(const std::string& label);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700253
254 /** \return forwarder instance \p i
255 */
256 Forwarder&
257 getForwarder(TopologyNode i)
258 {
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400259 return m_forwarders.at(i)->forwarder;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700260 }
261
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400262 /** \brief Sets strategy on forwarder \p i.
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000263 * \tparam S the strategy type
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700264 * \note Test scenario can also access StrategyChoice table directly.
265 */
266 template<typename S>
267 void
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500268 setStrategy(TopologyNode i, Name prefix = Name("ndn:/"),
269 Name instanceName = S::getStrategyName())
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700270 {
271 Forwarder& forwarder = this->getForwarder(i);
Ashlesh Gawande92e4ea52017-07-19 11:38:12 -0500272 choose<S>(forwarder, prefix, instanceName);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700273 }
274
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400275 /** \brief Makes a link that interconnects two or more forwarders.
276 * \param linkType Desired link type; LINK_TYPE_NONE to use point-to-point for two forwarders
Teng Liangf995f382017-04-04 22:09:39 +0000277 * and multi-access for more than two forwarders; it's an error to specify
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400278 * point-to-point when there are more than two forwarders.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700279 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400280 * A face is created on each of \p forwarders.
281 * When a packet is sent onto one of the faces on this link, the packet will be
282 * received by all other faces on this link after the specified delay.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700283 */
284 shared_ptr<TopologyLink>
Teng Liangf995f382017-04-04 22:09:39 +0000285 addLink(const std::string& label, time::nanoseconds delay,
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700286 std::initializer_list<TopologyNode> forwarders,
Teng Liangf995f382017-04-04 22:09:39 +0000287 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_NONE);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700288
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400289 /** \brief Makes a link to a local application.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700290 */
291 shared_ptr<TopologyAppLink>
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700292 addAppFace(const std::string& label, TopologyNode i);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700293
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400294 /** \brief Makes a link to a local application and registers a prefix.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700295 */
296 shared_ptr<TopologyAppLink>
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700297 addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost = 0);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700298
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400299 /** \brief Makes a link that allows the test case to inject and observe layer-2 packets.
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600300 */
301 shared_ptr<TopologyBareLink>
302 addBareLink(const std::string& label, TopologyNode i,
303 ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_LOCAL,
304 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT);
305
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400306 /** \brief Enables packet capture on every forwarder face.
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000307 */
308 void
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400309 enablePcap(bool isEnabled = true)
310 {
311 m_wantPcap = isEnabled;
312 }
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000313
314 /** \return captured packets on a forwarder face
315 * \pre enablePcap(true) is in effect when the face was created
316 */
317 TopologyPcap&
318 getPcap(const Face& face);
319
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400320 /** \brief Registers a prefix on a forwarder face.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700321 */
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700322 void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700323 registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost = 0);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700324
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400325 /** \brief Creates a producer application that answers every Interest with Data of same Name.
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700326 */
327 void
Saurab Dulala6dec222019-04-01 00:15:10 -0500328 addEchoProducer(ndn::Face& face, const Name& prefix = "/", time::nanoseconds replyDelay = 0_ns);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700329
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400330 /** \brief Creates a consumer application that sends \p n Interests under \p prefix
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700331 * at \p interval fixed rate.
Teng Liangf995f382017-04-04 22:09:39 +0000332 * \param seq if non-negative, append sequence number instead of timestamp
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700333 */
334 void
Teng Liangf995f382017-04-04 22:09:39 +0000335 addIntervalConsumer(ndn::Face& face, const Name& prefix, time::nanoseconds interval,
336 size_t n, int seq = -1);
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700337
338private:
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600339 shared_ptr<Face>
340 makeFace(TopologyNode i, const FaceUri& localUri, const FaceUri& remoteUri,
341 ndn::nfd::FaceScope scope, ndn::nfd::LinkType linkType);
342
343private:
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400344 class TopologyForwarder
345 {
346 public:
347 explicit
348 TopologyForwarder(const std::string& label)
349 : label(label)
350 {
351 }
352
353 public:
354 std::string label;
355 FaceTable faceTable;
356 Forwarder forwarder{faceTable};
357 };
358
359 std::vector<unique_ptr<TopologyForwarder>> m_forwarders;
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700360 std::vector<shared_ptr<TopologyLink>> m_links;
361 std::vector<shared_ptr<TopologyAppLink>> m_appLinks;
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600362 std::vector<shared_ptr<TopologyBareLink>> m_bareLinks;
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400363 bool m_wantPcap = false;
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700364};
365
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400366} // namespace nfd::tests
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700367
Davide Pesavento97210d52016-10-14 15:45:48 +0200368#endif // NFD_TESTS_DAEMON_FW_TOPOLOGY_TESTER_HPP