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