blob: c9e9f34381f811a40bb8fdfcfaab6d70d8d2800c [file] [log] [blame]
Davide Pesavento912d2e82019-01-10 17:04:31 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento84e77302023-04-17 01:10:06 -04003 * Copyright (c) 2014-2023, Arizona Board of Regents.
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -08004 *
Davide Pesaventod0b59982015-02-27 19:15:15 +01005 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080017 *
18 * Author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
19 */
20
Davide Pesavento35185332019-01-14 04:00:15 -050021#include "util.hpp"
22
Davide Pesaventoef064892022-04-05 02:26:03 -040023#include <ndn-cxx/data.hpp>
Davide Pesavento35185332019-01-14 04:00:15 -050024#include <ndn-cxx/face.hpp>
Davide Pesaventoef064892022-04-05 02:26:03 -040025#include <ndn-cxx/interest.hpp>
Davide Pesavento35185332019-01-14 04:00:15 -050026#include <ndn-cxx/lp/tags.hpp>
Davide Pesavento35185332019-01-14 04:00:15 -050027#include <ndn-cxx/util/random.hpp>
Davide Pesaventoef064892022-04-05 02:26:03 -040028#include <ndn-cxx/util/time.hpp>
Davide Pesavento912d2e82019-01-10 17:04:31 -050029
Davide Pesavento35185332019-01-14 04:00:15 -050030#include <limits>
Davide Pesaventoef064892022-04-05 02:26:03 -040031#include <optional>
Davide Pesavento56f79d62019-01-26 17:30:32 -050032#include <sstream>
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080033#include <vector>
34
Davide Pesavento912d2e82019-01-10 17:04:31 -050035#include <boost/asio/deadline_timer.hpp>
36#include <boost/asio/io_service.hpp>
37#include <boost/asio/signal_set.hpp>
Alexander Lanee9fe1872023-07-25 20:00:23 -040038#include <boost/core/noncopyable.hpp>
jeraldabraham420dbf02014-04-25 22:58:31 -070039#include <boost/date_time/posix_time/posix_time.hpp>
Davide Pesavento912d2e82019-01-10 17:04:31 -050040#include <boost/lexical_cast.hpp>
Davide Pesavento35185332019-01-14 04:00:15 -050041#include <boost/program_options/options_description.hpp>
42#include <boost/program_options/parsers.hpp>
43#include <boost/program_options/variables_map.hpp>
jeraldabraham420dbf02014-04-25 22:58:31 -070044
Davide Pesaventoef064892022-04-05 02:26:03 -040045using namespace ndn::time_literals;
46using namespace std::string_literals;
jeraldabraham420dbf02014-04-25 22:58:31 -070047
Davide Pesaventoef064892022-04-05 02:26:03 -040048namespace ndntg {
49
50namespace time = ndn::time;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080051
jeraldabraham420dbf02014-04-25 22:58:31 -070052class NdnTrafficClient : boost::noncopyable
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080053{
54public:
jeraldabrahamcc3c6c92014-03-28 02:21:45 -070055 explicit
Alexander Lanee9fe1872023-07-25 20:00:23 -040056 NdnTrafficClient(std::string configFile)
57 : m_configurationFile(std::move(configFile))
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080058 {
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080059 }
60
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080061 void
Davide Pesavento35185332019-01-14 04:00:15 -050062 setMaximumInterests(uint64_t maxInterests)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080063 {
Davide Pesavento35185332019-01-14 04:00:15 -050064 m_nMaximumInterests = maxInterests;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080065 }
66
67 void
Davide Pesavento35185332019-01-14 04:00:15 -050068 setInterestInterval(time::milliseconds interval)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080069 {
Davide Pesavento35185332019-01-14 04:00:15 -050070 BOOST_ASSERT(interval > 0_ms);
71 m_interestInterval = interval;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080072 }
73
74 void
Alexander Lanee9fe1872023-07-25 20:00:23 -040075 setTimestampFormat(std::string format)
76 {
77 m_timestampFormat = std::move(format);
78 }
79
80 void
jeraldabraham420dbf02014-04-25 22:58:31 -070081 setQuietLogging()
82 {
Davide Pesavento35185332019-01-14 04:00:15 -050083 m_wantQuiet = true;
jeraldabraham420dbf02014-04-25 22:58:31 -070084 }
85
Alexander Lanee9fe1872023-07-25 20:00:23 -040086 void
87 setVerboseLogging()
88 {
89 m_wantVerbose = true;
90 }
91
Davide Pesavento306e5bc2019-01-26 16:20:34 -050092 int
Davide Pesavento35185332019-01-14 04:00:15 -050093 run()
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -080094 {
Alexander Lanee9fe1872023-07-25 20:00:23 -040095 m_logger.initialize(std::to_string(ndn::random::generateWord32()), m_timestampFormat);
Davide Pesavento306e5bc2019-01-26 16:20:34 -050096
97 if (!readConfigurationFile(m_configurationFile, m_trafficPatterns, m_logger)) {
98 return 2;
99 }
100
101 if (!checkTrafficPatternCorrectness()) {
102 m_logger.log("ERROR: Traffic configuration provided is not proper", false, true);
103 return 2;
104 }
105
Alexander Lanee9fe1872023-07-25 20:00:23 -0400106 m_logger.log("Traffic configuration file processing completed\n", true, false);
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500107 for (std::size_t i = 0; i < m_trafficPatterns.size(); i++) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400108 m_logger.log("Traffic Pattern Type #" + std::to_string(i + 1), false, false);
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500109 m_trafficPatterns[i].printTrafficConfiguration(m_logger);
110 m_logger.log("", false, false);
111 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100112
Davide Pesavento35185332019-01-14 04:00:15 -0500113 if (m_nMaximumInterests == 0) {
114 logStatistics();
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500115 return 0;
Davide Pesavento35185332019-01-14 04:00:15 -0500116 }
Davide Pesaventod0b59982015-02-27 19:15:15 +0100117
Davide Pesaventoef064892022-04-05 02:26:03 -0400118 m_signalSet.async_wait([this] (auto&&...) { stop(); });
Davide Pesavento35185332019-01-14 04:00:15 -0500119
120 boost::asio::deadline_timer timer(m_ioService,
121 boost::posix_time::millisec(m_interestInterval.count()));
Davide Pesaventoef064892022-04-05 02:26:03 -0400122 timer.async_wait([this, &timer] (auto&&...) { generateTraffic(timer); });
Davide Pesavento35185332019-01-14 04:00:15 -0500123
124 try {
125 m_face.processEvents();
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500126 return m_hasError ? 1 : 0;
Davide Pesavento35185332019-01-14 04:00:15 -0500127 }
128 catch (const std::exception& e) {
129 m_logger.log("ERROR: "s + e.what(), true, true);
Davide Pesavento35185332019-01-14 04:00:15 -0500130 m_ioService.stop();
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500131 return 1;
Davide Pesavento35185332019-01-14 04:00:15 -0500132 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800133 }
134
Davide Pesavento35185332019-01-14 04:00:15 -0500135private:
136 class InterestTrafficConfiguration
137 {
138 public:
139 void
140 printTrafficConfiguration(Logger& logger) const
141 {
Davide Pesavento56f79d62019-01-26 17:30:32 -0500142 std::ostringstream os;
Davide Pesavento35185332019-01-14 04:00:15 -0500143
Davide Pesavento56f79d62019-01-26 17:30:32 -0500144 os << "TrafficPercentage=" << m_trafficPercentage << ", ";
145 os << "Name=" << m_name << ", ";
146 if (m_nameAppendBytes) {
147 os << "NameAppendBytes=" << *m_nameAppendBytes << ", ";
148 }
149 if (m_nameAppendSeqNum) {
150 os << "NameAppendSequenceNumber=" << *m_nameAppendSeqNum << ", ";
151 }
152 if (m_canBePrefix) {
153 os << "CanBePrefix=" << m_canBePrefix << ", ";
154 }
155 if (m_mustBeFresh) {
156 os << "MustBeFresh=" << m_mustBeFresh << ", ";
157 }
158 if (m_nonceDuplicationPercentage > 0) {
159 os << "NonceDuplicationPercentage=" << m_nonceDuplicationPercentage << ", ";
160 }
161 if (m_interestLifetime >= 0_ms) {
162 os << "InterestLifetime=" << m_interestLifetime.count() << ", ";
163 }
164 if (m_nextHopFaceId > 0) {
165 os << "NextHopFaceId=" << m_nextHopFaceId << ", ";
166 }
167 if (m_expectedContent) {
168 os << "ExpectedContent=" << *m_expectedContent << ", ";
169 }
Davide Pesavento35185332019-01-14 04:00:15 -0500170
Davide Pesavento56f79d62019-01-26 17:30:32 -0500171 auto str = os.str();
172 str = str.substr(0, str.length() - 2); // remove suffix ", "
173 logger.log(str, false, false);
Davide Pesavento35185332019-01-14 04:00:15 -0500174 }
175
176 bool
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500177 parseConfigurationLine(const std::string& line, Logger& logger, int lineNumber)
Davide Pesavento35185332019-01-14 04:00:15 -0500178 {
179 std::string parameter, value;
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500180 if (!extractParameterAndValue(line, parameter, value)) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400181 logger.log("Line " + std::to_string(lineNumber) + " - Invalid syntax: " + line,
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500182 false, true);
183 return false;
184 }
185
186 if (parameter == "TrafficPercentage") {
187 m_trafficPercentage = std::stoul(value);
188 }
189 else if (parameter == "Name") {
190 m_name = value;
191 }
192 else if (parameter == "NameAppendBytes") {
193 m_nameAppendBytes = std::stoul(value);
194 }
195 else if (parameter == "NameAppendSequenceNumber") {
196 m_nameAppendSeqNum = std::stoull(value);
197 }
Davide Pesavento56f79d62019-01-26 17:30:32 -0500198 else if (parameter == "CanBePrefix") {
199 m_canBePrefix = parseBoolean(value);
200 }
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500201 else if (parameter == "MustBeFresh") {
202 m_mustBeFresh = parseBoolean(value);
203 }
204 else if (parameter == "NonceDuplicationPercentage") {
205 m_nonceDuplicationPercentage = std::stoul(value);
206 }
207 else if (parameter == "InterestLifetime") {
208 m_interestLifetime = time::milliseconds(std::stoul(value));
209 }
210 else if (parameter == "NextHopFaceId") {
211 m_nextHopFaceId = std::stoull(value);
212 }
213 else if (parameter == "ExpectedContent") {
214 m_expectedContent = value;
Davide Pesavento35185332019-01-14 04:00:15 -0500215 }
216 else {
Davide Pesaventoef064892022-04-05 02:26:03 -0400217 logger.log("Line " + std::to_string(lineNumber) + " - Ignoring unknown parameter: " + parameter,
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500218 false, true);
Davide Pesavento35185332019-01-14 04:00:15 -0500219 }
220 return true;
221 }
222
223 bool
224 checkTrafficDetailCorrectness() const
225 {
226 return true;
227 }
228
229 public:
230 uint8_t m_trafficPercentage = 0;
231 std::string m_name;
Davide Pesaventoef064892022-04-05 02:26:03 -0400232 std::optional<std::size_t> m_nameAppendBytes;
233 std::optional<uint64_t> m_nameAppendSeqNum;
Davide Pesavento56f79d62019-01-26 17:30:32 -0500234 bool m_canBePrefix = false;
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500235 bool m_mustBeFresh = false;
Davide Pesavento35185332019-01-14 04:00:15 -0500236 uint8_t m_nonceDuplicationPercentage = 0;
237 time::milliseconds m_interestLifetime = -1_ms;
238 uint64_t m_nextHopFaceId = 0;
Davide Pesaventoef064892022-04-05 02:26:03 -0400239 std::optional<std::string> m_expectedContent;
Davide Pesavento35185332019-01-14 04:00:15 -0500240
241 uint64_t m_nInterestsSent = 0;
242 uint64_t m_nInterestsReceived = 0;
243 uint64_t m_nNacks = 0;
244 uint64_t m_nContentInconsistencies = 0;
245
246 // RTT is stored as milliseconds with fractional sub-milliseconds precision
247 double m_minimumInterestRoundTripTime = std::numeric_limits<double>::max();
248 double m_maximumInterestRoundTripTime = 0;
249 double m_totalInterestRoundTripTime = 0;
250 };
251
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800252 void
253 logStatistics()
254 {
Davide Pesavento84e77302023-04-17 01:10:06 -0400255 using std::to_string;
256
Alexander Lanee9fe1872023-07-25 20:00:23 -0400257 m_logger.log("\n\n== Traffic Report ==\n", false, true);
Davide Pesavento84e77302023-04-17 01:10:06 -0400258 m_logger.log("Total Traffic Pattern Types = " + to_string(m_trafficPatterns.size()), false, true);
259 m_logger.log("Total Interests Sent = " + to_string(m_nInterestsSent), false, true);
260 m_logger.log("Total Responses Received = " + to_string(m_nInterestsReceived), false, true);
261 m_logger.log("Total Nacks Received = " + to_string(m_nNacks), false, true);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100262
Davide Pesavento35185332019-01-14 04:00:15 -0500263 double loss = 0.0;
264 if (m_nInterestsSent > 0) {
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700265 loss = (m_nInterestsSent - m_nInterestsReceived) * 100.0 / m_nInterestsSent;
Davide Pesavento35185332019-01-14 04:00:15 -0500266 }
Davide Pesavento84e77302023-04-17 01:10:06 -0400267 m_logger.log("Total Interest Loss = " + to_string(loss) + "%", false, true);
Davide Pesaventod0b59982015-02-27 19:15:15 +0100268
Davide Pesavento35185332019-01-14 04:00:15 -0500269 double average = 0.0;
270 double inconsistency = 0.0;
271 if (m_nInterestsReceived > 0) {
272 average = m_totalInterestRoundTripTime / m_nInterestsReceived;
273 inconsistency = m_nContentInconsistencies * 100.0 / m_nInterestsReceived;
274 }
Davide Pesavento84e77302023-04-17 01:10:06 -0400275 m_logger.log("Total Data Inconsistency = " + to_string(inconsistency) + "%", false, true);
276 m_logger.log("Total Round Trip Time = " + to_string(m_totalInterestRoundTripTime) + "ms", false, true);
277 m_logger.log("Average Round Trip Time = " + to_string(average) + "ms\n", false, true);
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700278
Davide Pesavento35185332019-01-14 04:00:15 -0500279 for (std::size_t patternId = 0; patternId < m_trafficPatterns.size(); patternId++) {
Davide Pesavento84e77302023-04-17 01:10:06 -0400280 const auto& pattern = m_trafficPatterns[patternId];
281
282 m_logger.log("Traffic Pattern Type #" + to_string(patternId + 1), false, true);
283 pattern.printTrafficConfiguration(m_logger);
284 m_logger.log("Total Interests Sent = " + to_string(pattern.m_nInterestsSent), false, true);
285 m_logger.log("Total Responses Received = " + to_string(pattern.m_nInterestsReceived), false, true);
286 m_logger.log("Total Nacks Received = " + to_string(pattern.m_nNacks), false, true);
287
288 loss = 0.0;
289 if (pattern.m_nInterestsSent > 0) {
290 loss = (pattern.m_nInterestsSent - pattern.m_nInterestsReceived) * 100.0 / pattern.m_nInterestsSent;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800291 }
Davide Pesavento84e77302023-04-17 01:10:06 -0400292 m_logger.log("Total Interest Loss = " + to_string(loss) + "%", false, true);
293
294 average = 0.0;
295 inconsistency = 0.0;
296 if (pattern.m_nInterestsReceived > 0) {
297 average = pattern.m_totalInterestRoundTripTime / pattern.m_nInterestsReceived;
298 inconsistency = pattern.m_nContentInconsistencies * 100.0 / pattern.m_nInterestsReceived;
Davide Pesavento35185332019-01-14 04:00:15 -0500299 }
Davide Pesavento84e77302023-04-17 01:10:06 -0400300 m_logger.log("Total Data Inconsistency = " + to_string(inconsistency) + "%", false, true);
Davide Pesavento35185332019-01-14 04:00:15 -0500301 m_logger.log("Total Round Trip Time = " +
Davide Pesavento84e77302023-04-17 01:10:06 -0400302 to_string(pattern.m_totalInterestRoundTripTime) + "ms", false, true);
303 m_logger.log("Average Round Trip Time = " + to_string(average) + "ms\n", false, true);
Davide Pesavento35185332019-01-14 04:00:15 -0500304 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800305 }
306
307 bool
Davide Pesavento35185332019-01-14 04:00:15 -0500308 checkTrafficPatternCorrectness() const
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800309 {
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500310 // TODO
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800311 return true;
312 }
313
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700314 uint32_t
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800315 getNewNonce()
316 {
Davide Pesavento35185332019-01-14 04:00:15 -0500317 if (m_nonces.size() >= 1000)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700318 m_nonces.clear();
jeraldabraham473ef3d2014-03-06 12:40:35 -0700319
Davide Pesaventoef064892022-04-05 02:26:03 -0400320 auto randomNonce = ndn::random::generateWord32();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700321 while (std::find(m_nonces.begin(), m_nonces.end(), randomNonce) != m_nonces.end())
Davide Pesaventoef064892022-04-05 02:26:03 -0400322 randomNonce = ndn::random::generateWord32();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700323
324 m_nonces.push_back(randomNonce);
325 return randomNonce;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800326 }
327
Davide Pesavento35185332019-01-14 04:00:15 -0500328 uint32_t
329 getOldNonce()
330 {
331 if (m_nonces.empty())
332 return getNewNonce();
333
334 std::uniform_int_distribution<std::size_t> dist(0, m_nonces.size() - 1);
Davide Pesaventoef064892022-04-05 02:26:03 -0400335 return m_nonces[dist(ndn::random::getRandomNumberEngine())];
Davide Pesavento35185332019-01-14 04:00:15 -0500336 }
337
Davide Pesaventoef064892022-04-05 02:26:03 -0400338 static auto
Davide Pesavento912d2e82019-01-10 17:04:31 -0500339 generateRandomNameComponent(std::size_t length)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800340 {
Davide Pesavento35185332019-01-14 04:00:15 -0500341 // per ISO C++ std, cannot instantiate uniform_int_distribution with uint8_t
342 static std::uniform_int_distribution<unsigned short> dist(std::numeric_limits<uint8_t>::min(),
343 std::numeric_limits<uint8_t>::max());
344
Davide Pesaventoef064892022-04-05 02:26:03 -0400345 ndn::Buffer buf(length);
Davide Pesavento912d2e82019-01-10 17:04:31 -0500346 for (std::size_t i = 0; i < length; i++) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400347 buf[i] = static_cast<uint8_t>(dist(ndn::random::getRandomNumberEngine()));
Eric Newberry3b284192015-07-06 21:44:46 -0700348 }
Davide Pesaventoef064892022-04-05 02:26:03 -0400349 return ndn::name::Component(buf);
Davide Pesavento35185332019-01-14 04:00:15 -0500350 }
351
Davide Pesaventoef064892022-04-05 02:26:03 -0400352 auto
Davide Pesavento35185332019-01-14 04:00:15 -0500353 prepareInterest(std::size_t patternId)
354 {
Davide Pesaventoef064892022-04-05 02:26:03 -0400355 ndn::Interest interest;
Davide Pesavento35185332019-01-14 04:00:15 -0500356 auto& pattern = m_trafficPatterns[patternId];
357
Davide Pesaventoef064892022-04-05 02:26:03 -0400358 ndn::Name name(pattern.m_name);
Davide Pesavento35185332019-01-14 04:00:15 -0500359 if (pattern.m_nameAppendBytes > 0) {
360 name.append(generateRandomNameComponent(*pattern.m_nameAppendBytes));
361 }
362 if (pattern.m_nameAppendSeqNum) {
363 auto seqNum = *pattern.m_nameAppendSeqNum;
364 name.appendSequenceNumber(seqNum);
365 pattern.m_nameAppendSeqNum = seqNum + 1;
366 }
367 interest.setName(name);
368
Davide Pesavento56f79d62019-01-26 17:30:32 -0500369 interest.setCanBePrefix(pattern.m_canBePrefix);
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500370 interest.setMustBeFresh(pattern.m_mustBeFresh);
Davide Pesavento35185332019-01-14 04:00:15 -0500371
372 static std::uniform_int_distribution<> duplicateNonceDist(1, 100);
Davide Pesaventoef064892022-04-05 02:26:03 -0400373 if (duplicateNonceDist(ndn::random::getRandomNumberEngine()) <= pattern.m_nonceDuplicationPercentage)
Davide Pesavento35185332019-01-14 04:00:15 -0500374 interest.setNonce(getOldNonce());
375 else
376 interest.setNonce(getNewNonce());
377
378 if (pattern.m_interestLifetime >= 0_ms)
379 interest.setInterestLifetime(pattern.m_interestLifetime);
380
381 if (pattern.m_nextHopFaceId > 0)
Davide Pesaventoef064892022-04-05 02:26:03 -0400382 interest.setTag(std::make_shared<ndn::lp::NextHopFaceIdTag>(pattern.m_nextHopFaceId));
Davide Pesavento35185332019-01-14 04:00:15 -0500383
384 return interest;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800385 }
386
387 void
Davide Pesavento84e77302023-04-17 01:10:06 -0400388 onData(const ndn::Interest&, const ndn::Data& data, int globalRef, int localRef,
389 std::size_t patternId, const time::steady_clock::time_point& sentTime)
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800390 {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400391 auto now = time::steady_clock::now();
Davide Pesaventoef064892022-04-05 02:26:03 -0400392 auto logLine = "Data Received - PatternType=" + std::to_string(patternId + 1) +
393 ", GlobalID=" + std::to_string(globalRef) +
394 ", LocalID=" + std::to_string(localRef) +
Davide Pesavento35185332019-01-14 04:00:15 -0500395 ", Name=" + data.getName().toUri();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700396
397 m_nInterestsReceived++;
398 m_trafficPatterns[patternId].m_nInterestsReceived++;
Davide Pesavento35185332019-01-14 04:00:15 -0500399
400 if (m_trafficPatterns[patternId].m_expectedContent) {
Davide Pesavento92669062022-03-10 19:09:44 -0500401 std::string receivedContent = readString(data.getContent());
Davide Pesavento35185332019-01-14 04:00:15 -0500402 if (receivedContent != *m_trafficPatterns[patternId].m_expectedContent) {
Davide Pesavento912d2e82019-01-10 17:04:31 -0500403 m_nContentInconsistencies++;
404 m_trafficPatterns[patternId].m_nContentInconsistencies++;
405 logLine += ", IsConsistent=No";
jeraldabraham473ef3d2014-03-06 12:40:35 -0700406 }
Davide Pesavento35185332019-01-14 04:00:15 -0500407 else {
Davide Pesavento912d2e82019-01-10 17:04:31 -0500408 logLine += ", IsConsistent=Yes";
Davide Pesavento35185332019-01-14 04:00:15 -0500409 }
Davide Pesavento912d2e82019-01-10 17:04:31 -0500410 }
Davide Pesavento35185332019-01-14 04:00:15 -0500411 else {
jeraldabraham473ef3d2014-03-06 12:40:35 -0700412 logLine += ", IsConsistent=NotChecked";
Davide Pesavento35185332019-01-14 04:00:15 -0500413 }
414 if (!m_wantQuiet) {
jeraldabraham420dbf02014-04-25 22:58:31 -0700415 m_logger.log(logLine, true, false);
Davide Pesavento35185332019-01-14 04:00:15 -0500416 }
417
Alexander Lanee9fe1872023-07-25 20:00:23 -0400418 double roundTripTime = (now - sentTime).count() / 1000000.0;
419 if (m_wantVerbose) {
420 auto rttLine = "RTT - Name=" + data.getName().toUri() +
421 ", RTT=" + std::to_string(roundTripTime) + "ms";
422 m_logger.log(rttLine, true, false);
423 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700424 if (m_minimumInterestRoundTripTime > roundTripTime)
425 m_minimumInterestRoundTripTime = roundTripTime;
426 if (m_maximumInterestRoundTripTime < roundTripTime)
427 m_maximumInterestRoundTripTime = roundTripTime;
428 if (m_trafficPatterns[patternId].m_minimumInterestRoundTripTime > roundTripTime)
429 m_trafficPatterns[patternId].m_minimumInterestRoundTripTime = roundTripTime;
430 if (m_trafficPatterns[patternId].m_maximumInterestRoundTripTime < roundTripTime)
431 m_trafficPatterns[patternId].m_maximumInterestRoundTripTime = roundTripTime;
432 m_totalInterestRoundTripTime += roundTripTime;
433 m_trafficPatterns[patternId].m_totalInterestRoundTripTime += roundTripTime;
Davide Pesavento35185332019-01-14 04:00:15 -0500434
435 if (m_nMaximumInterests == globalRef) {
436 stop();
Davide Pesavento912d2e82019-01-10 17:04:31 -0500437 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700438 }
439
440 void
Davide Pesaventoef064892022-04-05 02:26:03 -0400441 onNack(const ndn::Interest& interest, const ndn::lp::Nack& nack,
Davide Pesavento35185332019-01-14 04:00:15 -0500442 int globalRef, int localRef, std::size_t patternId)
Eric Newberry976c2042016-06-19 23:37:35 -0700443 {
Davide Pesaventoef064892022-04-05 02:26:03 -0400444 auto logLine = "Interest Nack'd - PatternType=" + std::to_string(patternId + 1) +
445 ", GlobalID=" + std::to_string(globalRef) +
446 ", LocalID=" + std::to_string(localRef) +
Davide Pesavento35185332019-01-14 04:00:15 -0500447 ", Name=" + interest.getName().toUri() +
448 ", NackReason=" + boost::lexical_cast<std::string>(nack.getReason());
Eric Newberry976c2042016-06-19 23:37:35 -0700449 m_logger.log(logLine, true, false);
450
451 m_nNacks++;
452 m_trafficPatterns[patternId].m_nNacks++;
453
Davide Pesavento35185332019-01-14 04:00:15 -0500454 if (m_nMaximumInterests == globalRef) {
455 stop();
Eric Newberry976c2042016-06-19 23:37:35 -0700456 }
457 }
458
459 void
Davide Pesavento84e77302023-04-17 01:10:06 -0400460 onTimeout(const ndn::Interest& interest, int globalRef, int localRef, std::size_t patternId)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700461 {
Davide Pesaventoef064892022-04-05 02:26:03 -0400462 auto logLine = "Interest Timed Out - PatternType=" + std::to_string(patternId + 1) +
463 ", GlobalID=" + std::to_string(globalRef) +
464 ", LocalID=" + std::to_string(localRef) +
Davide Pesavento35185332019-01-14 04:00:15 -0500465 ", Name=" + interest.getName().toUri();
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700466 m_logger.log(logLine, true, false);
Davide Pesavento35185332019-01-14 04:00:15 -0500467
468 if (m_nMaximumInterests == globalRef) {
469 stop();
470 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700471 }
472
473 void
Davide Pesavento35185332019-01-14 04:00:15 -0500474 generateTraffic(boost::asio::deadline_timer& timer)
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700475 {
Davide Pesavento35185332019-01-14 04:00:15 -0500476 if (m_nMaximumInterests && m_nInterestsSent >= *m_nMaximumInterests) {
Davide Pesavento912d2e82019-01-10 17:04:31 -0500477 return;
478 }
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700479
Davide Pesavento35185332019-01-14 04:00:15 -0500480 static std::uniform_int_distribution<> trafficDist(1, 100);
Davide Pesaventoef064892022-04-05 02:26:03 -0400481 int trafficKey = trafficDist(ndn::random::getRandomNumberEngine());
Davide Pesaventod0b59982015-02-27 19:15:15 +0100482
Davide Pesavento35185332019-01-14 04:00:15 -0500483 int cumulativePercentage = 0;
484 std::size_t patternId = 0;
485 for (; patternId < m_trafficPatterns.size(); patternId++) {
Davide Pesavento84e77302023-04-17 01:10:06 -0400486 auto& pattern = m_trafficPatterns[patternId];
487 cumulativePercentage += pattern.m_trafficPercentage;
Davide Pesavento35185332019-01-14 04:00:15 -0500488 if (trafficKey <= cumulativePercentage) {
Davide Pesavento84e77302023-04-17 01:10:06 -0400489 m_nInterestsSent++;
490 pattern.m_nInterestsSent++;
Davide Pesavento35185332019-01-14 04:00:15 -0500491 auto interest = prepareInterest(patternId);
492 try {
Davide Pesavento84e77302023-04-17 01:10:06 -0400493 int globalRef = m_nInterestsSent;
494 int localRef = pattern.m_nInterestsSent;
Davide Pesavento35185332019-01-14 04:00:15 -0500495 m_face.expressInterest(interest,
Davide Pesavento84e77302023-04-17 01:10:06 -0400496 [=, now = time::steady_clock::now()] (auto&&... args) {
497 onData(std::forward<decltype(args)>(args)..., globalRef, localRef, patternId, now);
498 },
499 [=] (auto&&... args) {
500 onNack(std::forward<decltype(args)>(args)..., globalRef, localRef, patternId);
501 },
502 [=] (auto&&... args) {
503 onTimeout(std::forward<decltype(args)>(args)..., globalRef, localRef, patternId);
504 });
Davide Pesavento35185332019-01-14 04:00:15 -0500505
506 if (!m_wantQuiet) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400507 auto logLine = "Sending Interest - PatternType=" + std::to_string(patternId + 1) +
508 ", GlobalID=" + std::to_string(m_nInterestsSent) +
Davide Pesavento84e77302023-04-17 01:10:06 -0400509 ", LocalID=" + std::to_string(pattern.m_nInterestsSent) +
Davide Pesavento35185332019-01-14 04:00:15 -0500510 ", Name=" + interest.getName().toUri();
511 m_logger.log(logLine, true, false);
512 }
513
Davide Pesaventoef064892022-04-05 02:26:03 -0400514 timer.expires_at(timer.expires_at() + boost::posix_time::millisec(m_interestInterval.count()));
515 timer.async_wait([this, &timer] (auto&&...) { generateTraffic(timer); });
Davide Pesavento35185332019-01-14 04:00:15 -0500516 }
517 catch (const std::exception& e) {
518 m_logger.log("ERROR: "s + e.what(), true, true);
519 }
520 break;
521 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800522 }
Alexander Lanee9fe1872023-07-25 20:00:23 -0400523
Davide Pesavento35185332019-01-14 04:00:15 -0500524 if (patternId == m_trafficPatterns.size()) {
Davide Pesaventoef064892022-04-05 02:26:03 -0400525 timer.expires_at(timer.expires_at() + boost::posix_time::millisec(m_interestInterval.count()));
526 timer.async_wait([this, &timer] (auto&&...) { generateTraffic(timer); });
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800527 }
528 }
529
Davide Pesavento35185332019-01-14 04:00:15 -0500530 void
531 stop()
532 {
533 if (m_nContentInconsistencies > 0 || m_nInterestsSent != m_nInterestsReceived) {
534 m_hasError = true;
535 }
536
537 logStatistics();
538 m_face.shutdown();
539 m_ioService.stop();
540 }
541
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800542private:
Alexander Lanee9fe1872023-07-25 20:00:23 -0400543 Logger m_logger{"NdnTrafficClient"};
Alexander Afanasyev976c3972014-05-26 17:03:40 +0300544 boost::asio::io_service m_ioService;
Alexander Lanee9fe1872023-07-25 20:00:23 -0400545 boost::asio::signal_set m_signalSet{m_ioService, SIGINT, SIGTERM};
546 ndn::Face m_face{m_ioService};
Davide Pesavento35185332019-01-14 04:00:15 -0500547
548 std::string m_configurationFile;
Alexander Lanee9fe1872023-07-25 20:00:23 -0400549 std::string m_timestampFormat;
Davide Pesaventoef064892022-04-05 02:26:03 -0400550 std::optional<uint64_t> m_nMaximumInterests;
Davide Pesavento35185332019-01-14 04:00:15 -0500551 time::milliseconds m_interestInterval = 1_s;
Davide Pesavento35185332019-01-14 04:00:15 -0500552
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700553 std::vector<InterestTrafficConfiguration> m_trafficPatterns;
554 std::vector<uint32_t> m_nonces;
Davide Pesavento35185332019-01-14 04:00:15 -0500555 uint64_t m_nInterestsSent = 0;
556 uint64_t m_nInterestsReceived = 0;
557 uint64_t m_nNacks = 0;
558 uint64_t m_nContentInconsistencies = 0;
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700559
Davide Pesavento912d2e82019-01-10 17:04:31 -0500560 // RTT is stored as milliseconds with fractional sub-milliseconds precision
Davide Pesavento35185332019-01-14 04:00:15 -0500561 double m_minimumInterestRoundTripTime = std::numeric_limits<double>::max();
562 double m_maximumInterestRoundTripTime = 0;
563 double m_totalInterestRoundTripTime = 0;
564
Davide Pesaventoef064892022-04-05 02:26:03 -0400565 bool m_wantQuiet = false;
Alexander Lanee9fe1872023-07-25 20:00:23 -0400566 bool m_wantVerbose = false;
Davide Pesavento35185332019-01-14 04:00:15 -0500567 bool m_hasError = false;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800568};
569
Davide Pesaventoef064892022-04-05 02:26:03 -0400570} // namespace ndntg
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800571
Davide Pesavento84e77302023-04-17 01:10:06 -0400572namespace po = boost::program_options;
573
Davide Pesavento35185332019-01-14 04:00:15 -0500574static void
Davide Pesaventoef064892022-04-05 02:26:03 -0400575usage(std::ostream& os, std::string_view programName, const po::options_description& desc)
Davide Pesavento35185332019-01-14 04:00:15 -0500576{
577 os << "Usage: " << programName << " [options] <Traffic_Configuration_File>\n"
578 << "\n"
579 << "Generate Interest traffic as per provided Traffic_Configuration_File.\n"
580 << "Interests are continuously generated unless a total number is specified.\n"
581 << "Set the environment variable NDN_TRAFFIC_LOGFOLDER to redirect output to a log file.\n"
582 << "\n"
583 << desc;
584}
585
jeraldabrahamcc3c6c92014-03-28 02:21:45 -0700586int
587main(int argc, char* argv[])
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800588{
Davide Pesavento35185332019-01-14 04:00:15 -0500589 std::string configFile;
Alexander Lanee9fe1872023-07-25 20:00:23 -0400590 std::string timestampFormat;
Davide Pesaventod0b59982015-02-27 19:15:15 +0100591
Davide Pesavento35185332019-01-14 04:00:15 -0500592 po::options_description visibleOptions("Options");
593 visibleOptions.add_options()
594 ("help,h", "print this help message and exit")
Alexander Lanee9fe1872023-07-25 20:00:23 -0400595 ("count,c", po::value<int64_t>(), "total number of Interests to be generated")
Davide Pesavento35185332019-01-14 04:00:15 -0500596 ("interval,i", po::value<ndn::time::milliseconds::rep>()->default_value(1000),
597 "Interest generation interval in milliseconds")
Alexander Lanee9fe1872023-07-25 20:00:23 -0400598 ("timestamp-format,t", po::value<std::string>(&timestampFormat), "format string for timestamp output")
599 ("quiet,q", po::bool_switch(), "turn off logging of Interest generation and Data reception")
600 ("verbose,v", po::bool_switch(), "log additional per-packet information")
Davide Pesavento35185332019-01-14 04:00:15 -0500601 ;
602
603 po::options_description hiddenOptions;
604 hiddenOptions.add_options()
605 ("config-file", po::value<std::string>(&configFile))
606 ;
607
608 po::positional_options_description posOptions;
609 posOptions.add("config-file", -1);
610
611 po::options_description allOptions;
612 allOptions.add(visibleOptions).add(hiddenOptions);
613
614 po::variables_map vm;
615 try {
616 po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm);
617 po::notify(vm);
618 }
619 catch (const po::error& e) {
620 std::cerr << "ERROR: " << e.what() << std::endl;
621 return 2;
622 }
623 catch (const boost::bad_any_cast& e) {
624 std::cerr << "ERROR: " << e.what() << std::endl;
625 return 2;
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800626 }
627
Davide Pesavento35185332019-01-14 04:00:15 -0500628 if (vm.count("help") > 0) {
629 usage(std::cout, argv[0], visibleOptions);
630 return 0;
631 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800632
Davide Pesavento35185332019-01-14 04:00:15 -0500633 if (configFile.empty()) {
634 usage(std::cerr, argv[0], visibleOptions);
635 return 2;
636 }
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800637
Alexander Lanee9fe1872023-07-25 20:00:23 -0400638 ndntg::NdnTrafficClient client(std::move(configFile));
Davide Pesavento35185332019-01-14 04:00:15 -0500639
640 if (vm.count("count") > 0) {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400641 auto count = vm["count"].as<int64_t>();
Davide Pesavento35185332019-01-14 04:00:15 -0500642 if (count < 0) {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400643 std::cerr << "ERROR: the argument for option '--count' cannot be negative\n";
Davide Pesavento35185332019-01-14 04:00:15 -0500644 return 2;
645 }
646 client.setMaximumInterests(static_cast<uint64_t>(count));
647 }
648
649 if (vm.count("interval") > 0) {
650 ndn::time::milliseconds interval(vm["interval"].as<ndn::time::milliseconds::rep>());
Davide Pesaventoef064892022-04-05 02:26:03 -0400651 if (interval <= 0_ms) {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400652 std::cerr << "ERROR: the argument for option '--interval' must be positive\n";
Davide Pesavento35185332019-01-14 04:00:15 -0500653 return 2;
654 }
655 client.setInterestInterval(interval);
656 }
657
Alexander Lanee9fe1872023-07-25 20:00:23 -0400658 if (!timestampFormat.empty()) {
659 client.setTimestampFormat(std::move(timestampFormat));
660 }
661
Davide Pesavento35185332019-01-14 04:00:15 -0500662 if (vm["quiet"].as<bool>()) {
Alexander Lanee9fe1872023-07-25 20:00:23 -0400663 if (vm["verbose"].as<bool>()) {
664 std::cerr << "ERROR: cannot set both '--quiet' and '--verbose'\n";
665 return 2;
666 }
Davide Pesavento35185332019-01-14 04:00:15 -0500667 client.setQuietLogging();
668 }
669
Alexander Lanee9fe1872023-07-25 20:00:23 -0400670 if (vm["verbose"].as<bool>()) {
671 client.setVerboseLogging();
672 }
673
Davide Pesavento306e5bc2019-01-26 16:20:34 -0500674 return client.run();
Alexander Afanasyeva8f2a922014-02-26 14:21:56 -0800675}