blob: 50c2dab8a5b212cb2e9228f0e068d715517dd540 [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 Pesavento91c15c82024-01-15 17:14:23 -05003 * Copyright (c) 2014-2024, 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 Pesaventoa9b09b62022-06-04 14:07:25 -040031#include <boost/asio/signal_set.hpp>
Davide Pesavento97e33022019-02-14 16:00:50 -050032#include <boost/exception/diagnostic_information.hpp>
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040033#include <boost/lexical_cast.hpp>
Davide Pesavento97e33022019-02-14 16:00:50 -050034
Weiwei Liu4f1afac2016-04-02 19:08:38 -070035#include <fstream>
36#include <iostream>
37
Davide Pesavento264af772021-02-09 21:48:24 -050038#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +000039#include <valgrind/callgrind.h>
40#endif
41
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040042namespace nfd::tests {
Weiwei Liu4f1afac2016-04-02 19:08:38 -070043
44class FaceBenchmark
45{
46public:
Davide Pesavento412c9822021-07-02 00:21:05 -040047 explicit
Weiwei Liu4f1afac2016-04-02 19:08:38 -070048 FaceBenchmark(const char* configFileName)
Davide Pesavento412c9822021-07-02 00:21:05 -040049 : m_terminationSignalSet{getGlobalIoService(), SIGINT, SIGTERM}
Davide Pesavento97e33022019-02-14 16:00:50 -050050 , m_tcpChannel{tcp::Endpoint{boost::asio::ip::tcp::v4(), 6363}, false,
Davide Pesavento412c9822021-07-02 00:21:05 -040051 [] (auto&&...) { return ndn::nfd::FACE_SCOPE_NON_LOCAL; }}
Junxiao Shia6286a92021-02-23 06:43:52 -070052 , 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 -070053 {
Davide Pesavento97e33022019-02-14 16:00:50 -050054 m_terminationSignalSet.async_wait([] (const auto& error, int) {
55 if (!error)
56 getGlobalIoService().stop();
57 });
Weiwei Liu4f1afac2016-04-02 19:08:38 -070058
59 parseConfig(configFileName);
60
Davide Pesavento412c9822021-07-02 00:21:05 -040061 m_tcpChannel.listen(std::bind(&FaceBenchmark::onLeftFaceCreated, this, _1),
62 std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -070063 std::clog << "Listening on " << m_tcpChannel.getUri() << std::endl;
64
Davide Pesavento412c9822021-07-02 00:21:05 -040065 m_udpChannel.listen(std::bind(&FaceBenchmark::onLeftFaceCreated, this, _1),
66 std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -070067 std::clog << "Listening on " << m_udpChannel.getUri() << std::endl;
68 }
69
70private:
Weiwei Liu4f1afac2016-04-02 19:08:38 -070071 void
72 parseConfig(const char* configFileName)
73 {
74 std::ifstream file{configFileName};
75 std::string uriStrL;
76 std::string uriStrR;
77
78 while (file >> uriStrL >> uriStrR) {
79 FaceUri uriL{uriStrL};
80 FaceUri uriR{uriStrR};
81
82 if (uriL.getScheme() != "tcp4" && uriL.getScheme() != "udp4") {
83 std::clog << "Unsupported protocol '" << uriL.getScheme() << "'" << std::endl;
84 }
85 else if (uriR.getScheme() != "tcp4" && uriR.getScheme() != "udp4") {
86 std::clog << "Unsupported protocol '" << uriR.getScheme() << "'" << std::endl;
87 }
88 else {
Davide Pesavento412c9822021-07-02 00:21:05 -040089 m_faceUris.emplace_back(uriL, uriR);
Weiwei Liu4f1afac2016-04-02 19:08:38 -070090 }
91 }
92
93 if (m_faceUris.empty()) {
Davide Pesaventocaa60cc2024-02-18 18:18:37 -050094 NDN_THROW_NO_STACK(std::runtime_error("No supported FaceUri pairs found in config file"));
Weiwei Liu4f1afac2016-04-02 19:08:38 -070095 }
96 }
97
98 void
99 onLeftFaceCreated(const shared_ptr<Face>& faceL)
100 {
101 std::clog << "Left face created: remote=" << faceL->getRemoteUri()
102 << " local=" << faceL->getLocalUri() << std::endl;
103
104 // find a matching right uri
105 FaceUri uriR;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400106 for (const auto& [first, second] : m_faceUris) {
107 if (first.getHost() == faceL->getRemoteUri().getHost() &&
108 first.getScheme() == faceL->getRemoteUri().getScheme()) {
109 uriR = second;
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700110 }
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400111 else if (second.getHost() == faceL->getRemoteUri().getHost() &&
112 second.getScheme() == faceL->getRemoteUri().getScheme()) {
113 uriR = first;
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700114 }
115 }
116
117 if (uriR == FaceUri()) {
118 std::clog << "No FaceUri matched, ignoring..." << std::endl;
119 faceL->close();
120 return;
121 }
122
123 // create the right face
Davide Pesaventod91fe6d2023-10-04 21:40:02 -0400124 auto addr = boost::asio::ip::make_address(uriR.getHost());
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700125 auto port = boost::lexical_cast<uint16_t>(uriR.getPort());
126 if (uriR.getScheme() == "tcp4") {
Davide Pesavento15b55052018-01-27 19:09:28 -0500127 m_tcpChannel.connect(tcp::Endpoint(addr, port), {},
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400128 std::bind(&FaceBenchmark::onRightFaceCreated, faceL, _1),
Davide Pesavento412c9822021-07-02 00:21:05 -0400129 std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700130 }
131 else if (uriR.getScheme() == "udp4") {
Davide Pesavento15b55052018-01-27 19:09:28 -0500132 m_udpChannel.connect(udp::Endpoint(addr, port), {},
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400133 std::bind(&FaceBenchmark::onRightFaceCreated, faceL, _1),
Davide Pesavento412c9822021-07-02 00:21:05 -0400134 std::bind(&FaceBenchmark::onFaceCreationFailed, _1, _2));
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700135 }
136 }
137
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400138 static void
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700139 onRightFaceCreated(const shared_ptr<Face>& faceL, const shared_ptr<Face>& faceR)
140 {
141 std::clog << "Right face created: remote=" << faceR->getRemoteUri()
142 << " local=" << faceR->getLocalUri() << std::endl;
143
144 tieFaces(faceR, faceL);
145 tieFaces(faceL, faceR);
146 }
147
148 static void
149 tieFaces(const shared_ptr<Face>& face1, const shared_ptr<Face>& face2)
150 {
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400151 face1->afterReceiveInterest.connect([face2] (const auto& interest, const EndpointId&) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700152 face2->sendInterest(interest);
ashiqopu075bb7d2019-03-10 01:38:21 +0000153 });
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400154 face1->afterReceiveData.connect([face2] (const auto& data, const EndpointId&) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700155 face2->sendData(data);
ashiqopu075bb7d2019-03-10 01:38:21 +0000156 });
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400157 face1->afterReceiveNack.connect([face2] (const auto& nack, const EndpointId&) {
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700158 face2->sendNack(nack);
ashiqopu075bb7d2019-03-10 01:38:21 +0000159 });
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700160 }
161
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400162 [[noreturn]] static void
Eric Newberry42602412016-08-27 09:33:18 -0700163 onFaceCreationFailed(uint32_t status, const std::string& reason)
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700164 {
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500165 NDN_THROW_NO_STACK(std::runtime_error("Failed to create face: [" +
166 std::to_string(status) + "] " + reason));
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700167 }
168
169private:
170 boost::asio::signal_set m_terminationSignalSet;
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400171 face::TcpChannel m_tcpChannel;
172 face::UdpChannel m_udpChannel;
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700173 std::vector<std::pair<FaceUri, FaceUri>> m_faceUris;
174};
175
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400176} // namespace nfd::tests
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700177
178int
179main(int argc, char** argv)
180{
Davide Pesavento91c15c82024-01-15 17:14:23 -0500181#ifndef NDEBUG
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000182 std::cerr << "Benchmark compiled in debug mode is unreliable, please compile in release mode.\n";
183#endif
184
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700185 if (argc != 2) {
186 std::cerr << "Usage: " << argv[0] << " <config-file>" << std::endl;
187 return 2;
188 }
189
190 try {
191 nfd::tests::FaceBenchmark bench{argv[1]};
Davide Pesavento264af772021-02-09 21:48:24 -0500192#ifdef NFD_HAVE_VALGRIND
Davide Pesaventoaaa5dd32016-09-02 12:33:33 +0000193 CALLGRIND_START_INSTRUMENTATION;
194#endif
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700195 nfd::getGlobalIoService().run();
196 }
197 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500198 std::cerr << "ERROR: " << boost::diagnostic_information(e);
Weiwei Liu4f1afac2016-04-02 19:08:38 -0700199 return 1;
200 }
201
202 return 0;
203}