Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 2 | /* |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 4 | * 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 Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 27 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/global.hpp" |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 29 | #include "face/generic-link-service.hpp" |
| 30 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 31 | #include <ndn-cxx/encoding/encoding-buffer-fwd.hpp> |
| 32 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 33 | namespace nfd { |
| 34 | namespace fw { |
| 35 | namespace tests { |
| 36 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 37 | using face::GenericLinkService; |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 38 | using face::InternalClientTransport; |
| 39 | using face::InternalForwarderTransport; |
| 40 | |
| 41 | TopologyLink::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 Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 48 | |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 49 | TopologyLink::TopologyLink(time::nanoseconds delay) |
Hila Ben Abraham | 39a79be | 2016-03-30 22:00:55 -0700 | [diff] [blame] | 50 | { |
| 51 | this->setDelay(delay); |
| 52 | } |
| 53 | |
| 54 | void |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 55 | TopologyLink::block(TopologyNode i, TopologyNode j) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 56 | { |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 57 | m_transports.at(i).blockedDestinations.insert(j); |
| 58 | } |
Hila Ben Abraham | 39a79be | 2016-03-30 22:00:55 -0700 | [diff] [blame] | 59 | |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 60 | void |
| 61 | TopologyLink::unblock(TopologyNode i, TopologyNode j) |
| 62 | { |
| 63 | m_transports.at(i).blockedDestinations.erase(j); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | TopologyLink::setDelay(time::nanoseconds delay) |
| 68 | { |
| 69 | BOOST_ASSERT(delay > time::nanoseconds::zero()); // zero delay does not work on macOS |
Hila Ben Abraham | 39a79be | 2016-03-30 22:00:55 -0700 | [diff] [blame] | 70 | m_delay = delay; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 74 | TopologyLink::addFace(TopologyNode i, shared_ptr<Face> face) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 75 | { |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 76 | auto receiveCb = [this, i] (Block&& pkt) { transmit(i, std::move(pkt)); }; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 77 | |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 78 | 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 Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 82 | |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 83 | auto& node = ret.first->second; |
| 84 | node.transport->setPeer(&node.proxy); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 88 | TopologyLink::transmit(TopologyNode i, Block&& packet) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 89 | { |
| 90 | if (!m_isUp) { |
| 91 | return; |
| 92 | } |
| 93 | |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 94 | const auto& blockedDestinations = m_transports.at(i).blockedDestinations; |
| 95 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 96 | for (const auto& p : m_transports) { |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 97 | if (p.first == i || blockedDestinations.count(p.first) > 0) { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 98 | continue; |
| 99 | } |
| 100 | |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 101 | this->scheduleReceive(p.second.transport, Block{packet}); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 105 | void |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 106 | TopologyLink::scheduleReceive(face::InternalTransportBase* recipient, Block&& packet) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 107 | { |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 108 | getScheduler().schedule(m_delay, [=, pkt = std::move(packet)] () mutable { |
| 109 | recipient->receivePacket(std::move(pkt)); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 110 | }); |
| 111 | } |
| 112 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 113 | TopologyAppLink::TopologyAppLink(shared_ptr<Face> forwarderFace) |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 114 | : m_face(std::move(forwarderFace)) |
| 115 | , m_forwarderTransport(static_cast<InternalForwarderTransport*>(m_face->getTransport())) |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 116 | , m_clientTransport(make_shared<InternalClientTransport>()) |
| 117 | , m_client(make_shared<ndn::Face>(m_clientTransport, getGlobalIoService())) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 118 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 119 | this->recover(); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 123 | TopologyAppLink::fail() |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 124 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 125 | m_clientTransport->connectToForwarder(nullptr); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | TopologyAppLink::recover() |
| 130 | { |
| 131 | m_clientTransport->connectToForwarder(m_forwarderTransport); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Junxiao Shi | fab9e0d | 2017-02-02 06:04:59 +0000 | [diff] [blame] | 134 | class TopologyPcapLinkService : public GenericLinkService |
| 135 | , public TopologyPcap |
| 136 | { |
| 137 | private: |
| 138 | ///\todo #3941 call GenericLinkServiceCounters constructor in TopologyPcapLinkService constructor |
| 139 | |
| 140 | void |
| 141 | doSendInterest(const Interest& interest) override |
| 142 | { |
| 143 | this->sentInterests.push_back(interest); |
| 144 | this->sentInterests.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now())); |
| 145 | this->GenericLinkService::doSendInterest(interest); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | doSendData(const Data& data) override |
| 150 | { |
| 151 | this->sentData.push_back(data); |
| 152 | this->sentData.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now())); |
| 153 | this->GenericLinkService::doSendData(data); |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | doSendNack(const lp::Nack& nack) override |
| 158 | { |
| 159 | this->sentNacks.push_back(nack); |
| 160 | this->sentNacks.back().setTag(std::make_shared<TopologyPcapTimestamp>(time::steady_clock::now())); |
| 161 | this->GenericLinkService::doSendNack(nack); |
| 162 | } |
| 163 | }; |
| 164 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 165 | TopologyNode |
| 166 | TopologyTester::addForwarder(const std::string& label) |
| 167 | { |
| 168 | size_t i = m_forwarders.size(); |
Davide Pesavento | 459cf19 | 2016-03-03 02:20:12 +0100 | [diff] [blame] | 169 | m_forwarders.push_back(make_unique<Forwarder>()); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 170 | m_forwarderLabels.push_back(label); |
| 171 | BOOST_ASSERT(m_forwarders.size() == m_forwarderLabels.size()); |
| 172 | return i; |
| 173 | } |
| 174 | |
| 175 | shared_ptr<TopologyLink> |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 176 | TopologyTester::addLink(const std::string& label, time::nanoseconds delay, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 177 | std::initializer_list<TopologyNode> forwarders, |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 178 | ndn::nfd::LinkType linkType) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 179 | { |
Yumin Xia | ab49745 | 2016-05-10 20:23:24 +0800 | [diff] [blame] | 180 | auto link = std::make_shared<TopologyLink>(delay); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 181 | FaceUri remoteUri("topology://link/" + label); |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 182 | if (linkType == ndn::nfd::LINK_TYPE_NONE) { |
| 183 | linkType = forwarders.size() > 2 ? ndn::nfd::LINK_TYPE_MULTI_ACCESS : |
| 184 | ndn::nfd::LINK_TYPE_POINT_TO_POINT; |
| 185 | } |
| 186 | BOOST_ASSERT(forwarders.size() <= 2 || linkType != ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 187 | |
| 188 | for (TopologyNode i : forwarders) { |
| 189 | Forwarder& forwarder = this->getForwarder(i); |
| 190 | FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/" + label); |
| 191 | |
Junxiao Shi | fab9e0d | 2017-02-02 06:04:59 +0000 | [diff] [blame] | 192 | unique_ptr<GenericLinkService> service = m_wantPcap ? make_unique<TopologyPcapLinkService>() : |
| 193 | make_unique<GenericLinkService>(); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 194 | auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 195 | ndn::nfd::FACE_SCOPE_NON_LOCAL, linkType); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 196 | auto face = make_shared<Face>(std::move(service), std::move(transport)); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 197 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 198 | forwarder.addFace(face); |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 199 | link->addFace(i, std::move(face)); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | m_links.push_back(link); // keep a shared_ptr so callers don't have to |
| 203 | return link; |
| 204 | } |
| 205 | |
| 206 | shared_ptr<TopologyAppLink> |
| 207 | TopologyTester::addAppFace(const std::string& label, TopologyNode i) |
| 208 | { |
| 209 | Forwarder& forwarder = this->getForwarder(i); |
| 210 | FaceUri localUri("topology://" + m_forwarderLabels.at(i) + "/local/" + label); |
| 211 | FaceUri remoteUri("topology://" + m_forwarderLabels.at(i) + "/app/" + label); |
| 212 | |
Junxiao Shi | fab9e0d | 2017-02-02 06:04:59 +0000 | [diff] [blame] | 213 | unique_ptr<GenericLinkService> service = m_wantPcap ? make_unique<TopologyPcapLinkService>() : |
| 214 | make_unique<GenericLinkService>(); |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 215 | auto transport = make_unique<InternalForwarderTransport>(localUri, remoteUri, |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 216 | ndn::nfd::FACE_SCOPE_LOCAL, ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 217 | auto face = make_shared<Face>(std::move(service), std::move(transport)); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 218 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 219 | forwarder.addFace(face); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 220 | |
Davide Pesavento | 284bd62 | 2019-03-31 02:10:02 -0400 | [diff] [blame] | 221 | auto al = make_shared<TopologyAppLink>(std::move(face)); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 222 | m_appLinks.push_back(al); // keep a shared_ptr so callers don't have to |
| 223 | return al; |
| 224 | } |
| 225 | |
| 226 | shared_ptr<TopologyAppLink> |
| 227 | TopologyTester::addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost) |
| 228 | { |
| 229 | shared_ptr<TopologyAppLink> al = this->addAppFace(label, i); |
| 230 | this->registerPrefix(i, al->getForwarderFace(), prefix, cost); |
| 231 | return al; |
| 232 | } |
| 233 | |
| 234 | void |
Junxiao Shi | fab9e0d | 2017-02-02 06:04:59 +0000 | [diff] [blame] | 235 | TopologyTester::enablePcap(bool isEnabled) |
| 236 | { |
| 237 | m_wantPcap = isEnabled; |
| 238 | } |
| 239 | |
| 240 | TopologyPcap& |
| 241 | TopologyTester::getPcap(const Face& face) |
| 242 | { |
| 243 | return dynamic_cast<TopologyPcapLinkService&>(*face.getLinkService()); |
| 244 | } |
| 245 | |
| 246 | void |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 247 | TopologyTester::registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost) |
| 248 | { |
| 249 | Forwarder& forwarder = this->getForwarder(i); |
| 250 | Fib& fib = forwarder.getFib(); |
ashiqopu | 3ad49db | 2018-10-20 22:38:47 +0000 | [diff] [blame] | 251 | fib.insert(prefix).first->addOrUpdateNextHop(const_cast<Face&>(face), 0, cost); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 255 | TopologyTester::addEchoProducer(ndn::Face& face, const Name& prefix, time::nanoseconds replyDelay) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 256 | { |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 257 | BOOST_ASSERT(replyDelay >= 0_ns); |
| 258 | |
| 259 | face.setInterestFilter(prefix, [=, &face] (const auto&, const auto& interest) { |
| 260 | auto data = makeData(interest.getName()); |
| 261 | if (replyDelay == 0_ns) { |
| 262 | // reply immediately |
| 263 | face.put(*data); |
| 264 | } |
| 265 | else { |
| 266 | // delay the reply |
| 267 | getScheduler().schedule(replyDelay, [&face, data = std::move(data)] { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 268 | face.put(*data); |
| 269 | }); |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 270 | } |
| 271 | }); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void |
| 275 | TopologyTester::addIntervalConsumer(ndn::Face& face, const Name& prefix, |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 276 | time::nanoseconds interval, size_t n, int seq) |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 277 | { |
| 278 | Name name(prefix); |
Teng Liang | f995f38 | 2017-04-04 22:09:39 +0000 | [diff] [blame] | 279 | if (seq >= 0) { |
| 280 | name.appendSequenceNumber(seq); |
| 281 | ++seq; |
| 282 | } |
| 283 | else { |
| 284 | name.appendTimestamp(); |
| 285 | } |
| 286 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 287 | auto interest = makeInterest(name); |
Junxiao Shi | 9ede5bd | 2016-08-09 03:51:27 +0000 | [diff] [blame] | 288 | face.expressInterest(*interest, nullptr, nullptr, nullptr); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 289 | |
| 290 | if (n > 1) { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 291 | getScheduler().schedule(interval, [=, &face] { |
| 292 | addIntervalConsumer(face, prefix, interval, n - 1, seq); |
| 293 | }); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | } // namespace tests |
| 298 | } // namespace fw |
| 299 | } // namespace nfd |