Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 2642cd2 | 2017-07-13 21:34:53 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -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 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 26 | #include "common/global.hpp" |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 27 | #include "face/face.hpp" |
| 28 | #include "face/tcp-channel.hpp" |
| 29 | #include "face/udp-channel.hpp" |
| 30 | |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 31 | #include <boost/exception/diagnostic_information.hpp> |
| 32 | |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 33 | #include <fstream> |
| 34 | #include <iostream> |
| 35 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 36 | #ifdef NFD_HAVE_VALGRIND |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 37 | #include <valgrind/callgrind.h> |
| 38 | #endif |
| 39 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 40 | namespace nfd::tests { |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 41 | |
| 42 | class FaceBenchmark |
| 43 | { |
| 44 | public: |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 45 | explicit |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 46 | FaceBenchmark(const char* configFileName) |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 47 | : m_terminationSignalSet{getGlobalIoService(), SIGINT, SIGTERM} |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 48 | , m_tcpChannel{tcp::Endpoint{boost::asio::ip::tcp::v4(), 6363}, false, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 49 | [] (auto&&...) { return ndn::nfd::FACE_SCOPE_NON_LOCAL; }} |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 50 | , m_udpChannel{udp::Endpoint{boost::asio::ip::udp::v4(), 6363}, 10_min, false, ndn::MAX_NDN_PACKET_SIZE} |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 51 | { |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 52 | m_terminationSignalSet.async_wait([] (const auto& error, int) { |
| 53 | if (!error) |
| 54 | getGlobalIoService().stop(); |
| 55 | }); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 56 | |
| 57 | parseConfig(configFileName); |
| 58 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 59 | m_tcpChannel.listen(std::bind(&FaceBenchmark::onLeftFaceCreated, this, _1), |
| 60 | std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 61 | std::clog << "Listening on " << m_tcpChannel.getUri() << std::endl; |
| 62 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 63 | m_udpChannel.listen(std::bind(&FaceBenchmark::onLeftFaceCreated, this, _1), |
| 64 | std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 65 | std::clog << "Listening on " << m_udpChannel.getUri() << std::endl; |
| 66 | } |
| 67 | |
| 68 | private: |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 69 | void |
| 70 | parseConfig(const char* configFileName) |
| 71 | { |
| 72 | std::ifstream file{configFileName}; |
| 73 | std::string uriStrL; |
| 74 | std::string uriStrR; |
| 75 | |
| 76 | while (file >> uriStrL >> uriStrR) { |
| 77 | FaceUri uriL{uriStrL}; |
| 78 | FaceUri uriR{uriStrR}; |
| 79 | |
| 80 | if (uriL.getScheme() != "tcp4" && uriL.getScheme() != "udp4") { |
| 81 | std::clog << "Unsupported protocol '" << uriL.getScheme() << "'" << std::endl; |
| 82 | } |
| 83 | else if (uriR.getScheme() != "tcp4" && uriR.getScheme() != "udp4") { |
| 84 | std::clog << "Unsupported protocol '" << uriR.getScheme() << "'" << std::endl; |
| 85 | } |
| 86 | else { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 87 | m_faceUris.emplace_back(uriL, uriR); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | if (m_faceUris.empty()) { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 92 | NDN_THROW(std::runtime_error("No supported FaceUri pairs found in config file")); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | onLeftFaceCreated(const shared_ptr<Face>& faceL) |
| 98 | { |
| 99 | std::clog << "Left face created: remote=" << faceL->getRemoteUri() |
| 100 | << " local=" << faceL->getLocalUri() << std::endl; |
| 101 | |
| 102 | // find a matching right uri |
| 103 | FaceUri uriR; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 104 | for (const auto& [first, second] : m_faceUris) { |
| 105 | if (first.getHost() == faceL->getRemoteUri().getHost() && |
| 106 | first.getScheme() == faceL->getRemoteUri().getScheme()) { |
| 107 | uriR = second; |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 108 | } |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 109 | else if (second.getHost() == faceL->getRemoteUri().getHost() && |
| 110 | second.getScheme() == faceL->getRemoteUri().getScheme()) { |
| 111 | uriR = first; |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | if (uriR == FaceUri()) { |
| 116 | std::clog << "No FaceUri matched, ignoring..." << std::endl; |
| 117 | faceL->close(); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // create the right face |
Davide Pesavento | 9c33b90 | 2018-05-20 01:30:29 -0400 | [diff] [blame] | 122 | auto addr = boost::asio::ip::address::from_string(uriR.getHost()); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 123 | auto port = boost::lexical_cast<uint16_t>(uriR.getPort()); |
| 124 | if (uriR.getScheme() == "tcp4") { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 125 | m_tcpChannel.connect(tcp::Endpoint(addr, port), {}, |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 126 | std::bind(&FaceBenchmark::onRightFaceCreated, faceL, _1), |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 127 | std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 128 | } |
| 129 | else if (uriR.getScheme() == "udp4") { |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 130 | m_udpChannel.connect(udp::Endpoint(addr, port), {}, |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 131 | std::bind(&FaceBenchmark::onRightFaceCreated, faceL, _1), |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 132 | std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 136 | static void |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 137 | onRightFaceCreated(const shared_ptr<Face>& faceL, const shared_ptr<Face>& faceR) |
| 138 | { |
| 139 | std::clog << "Right face created: remote=" << faceR->getRemoteUri() |
| 140 | << " local=" << faceR->getLocalUri() << std::endl; |
| 141 | |
| 142 | tieFaces(faceR, faceL); |
| 143 | tieFaces(faceL, faceR); |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | tieFaces(const shared_ptr<Face>& face1, const shared_ptr<Face>& face2) |
| 148 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 149 | face1->afterReceiveInterest.connect([face2] (const auto& interest, const EndpointId&) { |
Teng Liang | f3bc3ae | 2020-06-08 10:19:25 -0700 | [diff] [blame] | 150 | face2->sendInterest(interest); |
ashiqopu | 075bb7d | 2019-03-10 01:38:21 +0000 | [diff] [blame] | 151 | }); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 152 | face1->afterReceiveData.connect([face2] (const auto& data, const EndpointId&) { |
Teng Liang | f3bc3ae | 2020-06-08 10:19:25 -0700 | [diff] [blame] | 153 | face2->sendData(data); |
ashiqopu | 075bb7d | 2019-03-10 01:38:21 +0000 | [diff] [blame] | 154 | }); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 155 | face1->afterReceiveNack.connect([face2] (const auto& nack, const EndpointId&) { |
Teng Liang | f3bc3ae | 2020-06-08 10:19:25 -0700 | [diff] [blame] | 156 | face2->sendNack(nack); |
ashiqopu | 075bb7d | 2019-03-10 01:38:21 +0000 | [diff] [blame] | 157 | }); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 160 | [[noreturn]] static void |
Eric Newberry | 4260241 | 2016-08-27 09:33:18 -0700 | [diff] [blame] | 161 | onFaceCreationFailed(uint32_t status, const std::string& reason) |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 162 | { |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 163 | NDN_THROW(std::runtime_error("Failed to create face: [" + to_string(status) + "] " + reason)); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | private: |
| 167 | boost::asio::signal_set m_terminationSignalSet; |
Davide Pesavento | 8fd15e6 | 2017-04-06 19:58:54 -0400 | [diff] [blame] | 168 | face::TcpChannel m_tcpChannel; |
| 169 | face::UdpChannel m_udpChannel; |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 170 | std::vector<std::pair<FaceUri, FaceUri>> m_faceUris; |
| 171 | }; |
| 172 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 173 | } // namespace nfd::tests |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 174 | |
| 175 | int |
| 176 | main(int argc, char** argv) |
| 177 | { |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 178 | #ifdef _DEBUG |
| 179 | std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n"; |
| 180 | #endif |
| 181 | |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 182 | if (argc != 2) { |
| 183 | std::cerr << "Usage: " << argv[0] << " <config-file>" << std::endl; |
| 184 | return 2; |
| 185 | } |
| 186 | |
| 187 | try { |
| 188 | nfd::tests::FaceBenchmark bench{argv[1]}; |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 189 | #ifdef NFD_HAVE_VALGRIND |
Davide Pesavento | aaa5dd3 | 2016-09-02 12:33:33 +0000 | [diff] [blame] | 190 | CALLGRIND_START_INSTRUMENTATION; |
| 191 | #endif |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 192 | nfd::getGlobalIoService().run(); |
| 193 | } |
| 194 | catch (const std::exception& e) { |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 195 | std::cerr << "ERROR: " << boost::diagnostic_information(e); |
Weiwei Liu | 4f1afac | 2016-04-02 19:08:38 -0700 | [diff] [blame] | 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |