Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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 | /** \file |
| 27 | * \brief allows testing forwarding in a network topology |
| 28 | */ |
| 29 | |
| 30 | #ifndef NFD_TESTS_NFD_FW_TOPOLOGY_TESTER_HPP |
| 31 | #define NFD_TESTS_NFD_FW_TOPOLOGY_TESTER_HPP |
| 32 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 33 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 34 | #include "face/internal-transport.hpp" |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 35 | #include "face/lp-face-wrapper.hpp" |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 36 | #include "fw/strategy.hpp" |
| 37 | #include "tests/test-common.hpp" |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 38 | |
| 39 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 40 | namespace fw { |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 41 | namespace tests { |
| 42 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 43 | using namespace nfd::tests; |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 44 | |
| 45 | /** \brief identifies a node (forwarder) in the topology |
| 46 | */ |
| 47 | typedef size_t TopologyNode; |
| 48 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 49 | /** \brief represents a network link in the topology which connects two or more nodes |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 50 | */ |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 51 | class TopologyLink : noncopyable |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 52 | { |
| 53 | public: |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 54 | explicit |
| 55 | TopologyLink(const time::nanoseconds& delay); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 56 | |
| 57 | /** \brief fail the link, cause packets to be dropped silently |
| 58 | */ |
| 59 | void |
| 60 | fail() |
| 61 | { |
| 62 | m_isUp = false; |
| 63 | } |
| 64 | |
| 65 | /** \brief recover the link from a failure |
| 66 | */ |
| 67 | void |
| 68 | recover() |
| 69 | { |
| 70 | m_isUp = true; |
| 71 | } |
| 72 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 73 | void |
| 74 | addFace(TopologyNode i, shared_ptr<face::LpFaceWrapper> face); |
| 75 | |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 76 | /** \return a face of forwarder \p i which is attached to this link |
| 77 | */ |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 78 | Face& |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 79 | getFace(TopologyNode i) |
| 80 | { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 81 | return *m_faces.at(i); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 84 | protected: |
| 85 | /** \brief attach a Transport onto this link |
| 86 | */ |
| 87 | void |
| 88 | attachTransport(TopologyNode i, face::InternalTransportBase* transport); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 89 | |
| 90 | private: |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 91 | void |
| 92 | transmit(TopologyNode i, const Block& packet); |
| 93 | |
| 94 | void |
| 95 | scheduleReceive(face::InternalTransportBase* recipient, const Block& packet); |
| 96 | |
| 97 | private: |
| 98 | bool m_isUp; |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 99 | time::nanoseconds m_delay; |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 100 | std::unordered_map<TopologyNode, face::InternalTransportBase*> m_transports; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 101 | std::unordered_map<TopologyNode, shared_ptr<face::LpFaceWrapper>> m_faces; |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /** \brief represents a link to a local application |
| 105 | */ |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 106 | class TopologyAppLink : noncopyable |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 107 | { |
| 108 | public: |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 109 | explicit |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 110 | TopologyAppLink(shared_ptr<face::LpFaceWrapper> forwarderFace); |
| 111 | |
| 112 | /** \brief fail the link, cause packets to be dropped silently |
| 113 | */ |
| 114 | void |
| 115 | fail(); |
| 116 | |
| 117 | /** \brief recover the link from a failure |
| 118 | */ |
| 119 | void |
| 120 | recover(); |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 122 | /** \return face on forwarder side |
| 123 | */ |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 124 | Face& |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 125 | getForwarderFace() |
| 126 | { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 127 | return *m_face; |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** \return face on application side |
| 131 | */ |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 132 | ndn::Face& |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 133 | getClientFace() |
| 134 | { |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 135 | return *m_client; |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private: |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame^] | 139 | shared_ptr<Face> m_face; |
| 140 | face::InternalForwarderTransport* m_forwarderTransport; |
| 141 | shared_ptr<face::InternalClientTransport> m_clientTransport; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 142 | shared_ptr<ndn::Face> m_client; |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /** \brief builds a topology for forwarding tests |
| 146 | */ |
| 147 | class TopologyTester : noncopyable |
| 148 | { |
| 149 | public: |
| 150 | /** \brief creates a forwarder |
| 151 | * \return index of new forwarder |
| 152 | */ |
| 153 | TopologyNode |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 154 | addForwarder(const std::string& label); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 155 | |
| 156 | /** \return forwarder instance \p i |
| 157 | */ |
| 158 | Forwarder& |
| 159 | getForwarder(TopologyNode i) |
| 160 | { |
| 161 | return *m_forwarders.at(i); |
| 162 | } |
| 163 | |
| 164 | /** \brief sets strategy on forwarder \p i |
| 165 | * \tparam the strategy type |
| 166 | * \note Test scenario can also access StrategyChoice table directly. |
| 167 | */ |
| 168 | template<typename S> |
| 169 | void |
| 170 | setStrategy(TopologyNode i, Name prefix = Name("ndn:/")) |
| 171 | { |
| 172 | Forwarder& forwarder = this->getForwarder(i); |
| 173 | StrategyChoice& strategyChoice = forwarder.getStrategyChoice(); |
| 174 | shared_ptr<S> strategy = make_shared<S>(ref(forwarder)); |
| 175 | strategyChoice.install(strategy); |
| 176 | strategyChoice.insert(prefix, strategy->getName()); |
| 177 | } |
| 178 | |
| 179 | /** \brief makes a link that interconnects two or more forwarders |
| 180 | * |
| 181 | * A face is created on each of \p forwarders . |
| 182 | * When a packet is sent onto one of the faces on this link, |
| 183 | * this packet will be received by all other faces on this link after \p delay . |
| 184 | */ |
| 185 | shared_ptr<TopologyLink> |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 186 | addLink(const std::string& label, const time::nanoseconds& delay, |
| 187 | std::initializer_list<TopologyNode> forwarders, |
| 188 | bool forceMultiAccessFace = false); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 189 | |
| 190 | /** \brief makes a link to local application |
| 191 | */ |
| 192 | shared_ptr<TopologyAppLink> |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 193 | addAppFace(const std::string& label, TopologyNode i); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 194 | |
| 195 | /** \brief makes a link to local application, and register a prefix |
| 196 | */ |
| 197 | shared_ptr<TopologyAppLink> |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 198 | addAppFace(const std::string& label, TopologyNode i, const Name& prefix, uint64_t cost = 0); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 199 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 200 | /** \brief registers a prefix on a forwarder face |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 201 | */ |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 202 | void |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 203 | registerPrefix(TopologyNode i, const Face& face, const Name& prefix, uint64_t cost = 0); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 204 | |
| 205 | /** \brief creates a producer application that answers every Interest with Data of same Name |
| 206 | */ |
| 207 | void |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 208 | addEchoProducer(ndn::Face& face, const Name& prefix = "/"); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 209 | |
| 210 | /** \brief creates a consumer application that sends \p n Interests under \p prefix |
| 211 | * at \p interval fixed rate. |
| 212 | */ |
| 213 | void |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 214 | addIntervalConsumer(ndn::Face& face, const Name& prefix, |
| 215 | const time::nanoseconds& interval, size_t n); |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 216 | |
| 217 | private: |
| 218 | std::vector<unique_ptr<Forwarder>> m_forwarders; |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 219 | std::vector<std::string> m_forwarderLabels; |
| 220 | std::vector<shared_ptr<TopologyLink>> m_links; |
| 221 | std::vector<shared_ptr<TopologyAppLink>> m_appLinks; |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 225 | } // namespace fw |
Junxiao Shi | 80ee7cb | 2014-12-14 10:53:05 -0700 | [diff] [blame] | 226 | } // namespace nfd |
| 227 | |
| 228 | #endif // NFD_TESTS_NFD_FW_TOPOLOGY_TESTER_HPP |