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