blob: 5d973925459f9f7f3f60beb61d9496da8e4a7c8e [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
27 * \brief allows testing forwarding in a network topology
28 */
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
43/** \brief identifies a node (forwarder) in the topology
44 */
45typedef size_t TopologyNode;
46
Junxiao Shi6535f1e2015-10-08 13:02:18 -070047/** \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
55 /** \brief fail the link, cause packets to be dropped silently
56 */
57 void
58 fail()
59 {
60 m_isUp = false;
61 }
62
63 /** \brief recover the link from a failure
64 */
65 void
66 recover()
67 {
68 m_isUp = true;
69 }
70
Teng Liangf995f382017-04-04 22:09:39 +000071 /** \brief block transmission from i to j
72 *
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
79 /** \brief unblock transmission from i to j
80 */
81 void
82 unblock(TopologyNode i, TopologyNode j);
83
Hila Ben Abraham39a79be2016-03-30 22:00:55 -070084 /** \brief change the link delay
85 * \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
Junxiao Shicde37ad2015-12-24 01:02:05 -070090 /** \brief attach a face to the link
91 * \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 Pesavento284bd622019-03-31 02:10:02 -0400113 class ReceiveProxy : 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
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600149/** \brief represents a link on a single forwarder
150 */
151class TopologySingleLink : noncopyable
152{
153public:
154 /** \brief constructor
155 * \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
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700173/** \brief represents a link to a local application
174 */
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600175class TopologyAppLink : public TopologySingleLink
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700176{
177public:
Junxiao Shicde37ad2015-12-24 01:02:05 -0700178 /** \brief constructor
179 * \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
184 /** \brief fail the link, cause packets to be dropped silently
185 */
186 void
187 fail();
188
189 /** \brief recover the link from a failure
190 */
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
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600207/** \brief allows the test case to inject and observe L2 packets on a link
208 */
209class TopologyBareLink : public TopologySingleLink
210{
211public:
212 /** \brief constructor
213 * \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
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000229/** \brief captured packets on a face
230 */
231class TopologyPcap : noncopyable
232{
233public:
234 std::vector<Interest> sentInterests;
235 std::vector<Data> sentData;
236 std::vector<lp::Nack> sentNacks;
237};
238
239/** \brief captured packet timestamp tag
240 */
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
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700243/** \brief builds a topology for forwarding tests
244 */
245class TopologyTester : noncopyable
246{
247public:
248 /** \brief creates a forwarder
249 * \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
262 /** \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
275 /** \brief makes a link that interconnects two or more forwarders
Teng Liangf995f382017-04-04 22:09:39 +0000276 * \brief linkType desired link type; LINK_TYPE_NONE to use point-to-point for two forwarders
277 * and multi-access for more than two forwarders; it's an error to specify
278 * point-to-point when there are more than two forwarders
Junxiao Shi80ee7cb2014-12-14 10:53:05 -0700279 *
280 * A face is created on each of \p forwarders .
281 * When a packet is sent onto one of the faces on this link,
282 * this packet will be received by all other faces on this link after \p delay .
283 */
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
289 /** \brief makes a link to local application
290 */
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
294 /** \brief makes a link to local application, and register a prefix
295 */
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
Junxiao Shi606d5dd2019-09-23 12:47:44 -0600299 /** \brief makes a link that allows the test case to inject and observe L2 packets
300 */
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
Junxiao Shifab9e0d2017-02-02 06:04:59 +0000306 /** \brief enables packet capture on every forwarder face
307 */
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
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700320 /** \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
325 /** \brief creates a producer application that answers every Interest with Data of same Name
326 */
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
330 /** \brief creates a consumer application that sends \p n Interests under \p prefix
331 * 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