blob: 1ed2143664a74bc80e2713abb84d50af89693207 [file] [log] [blame]
Weiwei Liu4f1afac2016-04-02 19:08:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry2642cd22017-07-13 21:34:53 -04002/*
Davide Pesavento264af772021-02-09 21:48:24 -05003 * Copyright (c) 2014-2021, Regents of the University of California,
Weiwei Liu4f1afac2016-04-02 19:08:38 -07004 * 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 Pesavento2cae8ca2019-04-18 20:48:05 -040026#include "common/global.hpp"
Weiwei Liu4f1afac2016-04-02 19:08:38 -070027#include "face/face.hpp"
28#include "face/tcp-channel.hpp"
29#include "face/udp-channel.hpp"
30
Davide Pesavento97e33022019-02-14 16:00:50 -050031#include <boost/exception/diagnostic_information.hpp>
32
Weiwei Liu4f1afac2016-04-02 19:08:38 -070033#include <fstream>
34#include <iostream>
35
Davide Pesavento264af772021-02-09 21:48:24 -050036#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000037#include <valgrind/callgrind.h>
38#endif
39
Weiwei Liu4f1afac2016-04-02 19:08:38 -070040namespace nfd {
41namespace tests {
42
43class FaceBenchmark
44{
45public:
Davide Pesavento412c9822021-07-02 00:21:05 -040046 explicit
Weiwei Liu4f1afac2016-04-02 19:08:38 -070047 FaceBenchmark(const char* configFileName)
Davide Pesavento412c9822021-07-02 00:21:05 -040048 : m_terminationSignalSet{getGlobalIoService(), SIGINT, SIGTERM}
Davide Pesavento97e33022019-02-14 16:00:50 -050049 , m_tcpChannel{tcp::Endpoint{boost::asio::ip::tcp::v4(), 6363}, false,
Davide Pesavento412c9822021-07-02 00:21:05 -040050 [] (auto&&...) { return ndn::nfd::FACE_SCOPE_NON_LOCAL; }}
Junxiao Shia6286a92021-02-23 06:43:52 -070051 , m_udpChannel{udp::Endpoint{boost::asio::ip::udp::v4(), 6363}, 10_min, false, ndn::MAX_NDN_PACKET_SIZE}
Weiwei Liu4f1afac2016-04-02 19:08:38 -070052 {
Davide Pesavento97e33022019-02-14 16:00:50 -050053 m_terminationSignalSet.async_wait([] (const auto& error, int) {
54 if (!error)
55 getGlobalIoService().stop();
56 });
Weiwei Liu4f1afac2016-04-02 19:08:38 -070057
58 parseConfig(configFileName);
59
Davide Pesavento412c9822021-07-02 00:21:05 -040060 m_tcpChannel.listen(std::bind(&FaceBenchmark::onLeftFaceCreated, this, _1),
61 std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -070062 std::clog << "Listening on " << m_tcpChannel.getUri() << std::endl;
63
Davide Pesavento412c9822021-07-02 00:21:05 -040064 m_udpChannel.listen(std::bind(&FaceBenchmark::onLeftFaceCreated, this, _1),
65 std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -070066 std::clog << "Listening on " << m_udpChannel.getUri() << std::endl;
67 }
68
69private:
Weiwei Liu4f1afac2016-04-02 19:08:38 -070070 void
71 parseConfig(const char* configFileName)
72 {
73 std::ifstream file{configFileName};
74 std::string uriStrL;
75 std::string uriStrR;
76
77 while (file >> uriStrL >> uriStrR) {
78 FaceUri uriL{uriStrL};
79 FaceUri uriR{uriStrR};
80
81 if (uriL.getScheme() != "tcp4" && uriL.getScheme() != "udp4") {
82 std::clog << "Unsupported protocol '" << uriL.getScheme() << "'" << std::endl;
83 }
84 else if (uriR.getScheme() != "tcp4" && uriR.getScheme() != "udp4") {
85 std::clog << "Unsupported protocol '" << uriR.getScheme() << "'" << std::endl;
86 }
87 else {
Davide Pesavento412c9822021-07-02 00:21:05 -040088 m_faceUris.emplace_back(uriL, uriR);
Weiwei Liu4f1afac2016-04-02 19:08:38 -070089 }
90 }
91
92 if (m_faceUris.empty()) {
Davide Pesavento19779d82019-02-14 13:40:04 -050093 NDN_THROW(std::runtime_error("No supported FaceUri pairs found in config file"));
Weiwei Liu4f1afac2016-04-02 19:08:38 -070094 }
95 }
96
97 void
98 onLeftFaceCreated(const shared_ptr<Face>& faceL)
99 {
100 std::clog << "Left face created: remote=" << faceL->getRemoteUri()
101 << " local=" << faceL->getLocalUri() << std::endl;
102
103 // find a matching right uri
104 FaceUri uriR;
105 for (const auto& pair : m_faceUris) {
106 if (pair.first.getHost() == faceL->getRemoteUri().getHost() &&
107 pair.first.getScheme() == faceL->getRemoteUri().getScheme()) {
108 uriR = pair.second;
109 }
110 else if (pair.second.getHost() == faceL->getRemoteUri().getHost() &&
111 pair.second.getScheme() == faceL->getRemoteUri().getScheme()) {
112 uriR = pair.first;
113 }
114 }
115
116 if (uriR == FaceUri()) {
117 std::clog << "No FaceUri matched, ignoring..." << std::endl;
118 faceL->close();
119 return;
120 }
121
122 // create the right face
Davide Pesavento9c33b902018-05-20 01:30:29 -0400123 auto addr = boost::asio::ip::address::from_string(uriR.getHost());
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700124 auto port = boost::lexical_cast<uint16_t>(uriR.getPort());
125 if (uriR.getScheme() == "tcp4") {
Davide Pesavento15b55052018-01-27 19:09:28 -0500126 m_tcpChannel.connect(tcp::Endpoint(addr, port), {},
Davide Pesavento412c9822021-07-02 00:21:05 -0400127 std::bind(&FaceBenchmark::onRightFaceCreated, this, faceL, _1),
128 std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700129 }
130 else if (uriR.getScheme() == "udp4") {
Davide Pesavento15b55052018-01-27 19:09:28 -0500131 m_udpChannel.connect(udp::Endpoint(addr, port), {},
Davide Pesavento412c9822021-07-02 00:21:05 -0400132 std::bind(&FaceBenchmark::onRightFaceCreated, this, faceL, _1),
133 std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700134 }
135 }
136
137 void
138 onRightFaceCreated(const shared_ptr<Face>& faceL, const shared_ptr<Face>& faceR)
139 {
140 std::clog << "Right face created: remote=" << faceR->getRemoteUri()
141 << " local=" << faceR->getLocalUri() << std::endl;
142
143 tieFaces(faceR, faceL);
144 tieFaces(faceL, faceR);
145 }
146
147 static void
148 tieFaces(const shared_ptr<Face>& face1, const shared_ptr<Face>& face2)
149 {
ashiqopu075bb7d2019-03-10 01:38:21 +0000150 face1->afterReceiveInterest.connect([face2] (const Interest& interest, const EndpointId&) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700151 face2->sendInterest(interest);
ashiqopu075bb7d2019-03-10 01:38:21 +0000152 });
153 face1->afterReceiveData.connect([face2] (const Data& data, const EndpointId&) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700154 face2->sendData(data);
ashiqopu075bb7d2019-03-10 01:38:21 +0000155 });
156 face1->afterReceiveNack.connect([face2] (const ndn::lp::Nack& nack, const EndpointId&) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700157 face2->sendNack(nack);
ashiqopu075bb7d2019-03-10 01:38:21 +0000158 });
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700159 }
160
161 static void
Eric Newberry42602412016-08-27 09:33:18 -0700162 onFaceCreationFailed(uint32_t status, const std::string& reason)
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700163 {
Davide Pesavento19779d82019-02-14 13:40:04 -0500164 NDN_THROW(std::runtime_error("Failed to create face: [" + to_string(status) + "] " + reason));
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700165 }
166
167private:
168 boost::asio::signal_set m_terminationSignalSet;
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400169 face::TcpChannel m_tcpChannel;
170 face::UdpChannel m_udpChannel;
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700171 std::vector<std::pair<FaceUri, FaceUri>> m_faceUris;
172};
173
174} // namespace tests
175} // namespace nfd
176
177int
178main(int argc, char** argv)
179{
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000180#ifdef _DEBUG
181 std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n";
182#endif
183
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700184 if (argc != 2) {
185 std::cerr << "Usage: " << argv[0] << " <config-file>" << std::endl;
186 return 2;
187 }
188
189 try {
190 nfd::tests::FaceBenchmark bench{argv[1]};
Davide Pesavento264af772021-02-09 21:48:24 -0500191#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000192 CALLGRIND_START_INSTRUMENTATION;
193#endif
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700194 nfd::getGlobalIoService().run();
195 }
196 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500197 std::cerr << "ERROR: " << boost::diagnostic_information(e);
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700198 return 1;
199 }
200
201 return 0;
202}