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