blob: 6c763a9bcf9a7546f7829f0b09cc8d762d507c2d [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Klaus Schneider7072e162017-09-16 13:43:00 -07002/*
Davide Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2016-2024, Regents of the University of California,
Davide Pesavento92998fe2017-01-18 21:04:52 -05004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Andrea Tosatto672b9a72016-01-05 16:18:20 +01006 *
7 * This file is part of ndn-tools (Named Data Networking Essential Tools).
8 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
9 *
10 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 *
23 * @author Wentao Shang
24 * @author Steve DiBenedetto
25 * @author Andrea Tosatto
Weiwei Liue4765012016-06-01 00:10:29 -070026 * @author Davide Pesavento
27 * @author Weiwei Liu
Klaus Schneider7072e162017-09-16 13:43:00 -070028 * @author Klaus Schneider
Chavoosh Ghasemi641f5932017-11-06 22:45:11 +000029 * @author Chavoosh Ghasemi
Andrea Tosatto672b9a72016-01-05 16:18:20 +010030 */
31
Andrea Tosatto672b9a72016-01-05 16:18:20 +010032#include "consumer.hpp"
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070033#include "discover-version.hpp"
Klaus Schneider9e5122b2019-03-19 17:03:25 -070034#include "pipeline-interests-aimd.hpp"
35#include "pipeline-interests-cubic.hpp"
schneiderklausd8197df2019-03-16 11:31:40 -070036#include "pipeline-interests-fixed.hpp"
schneiderklausd8197df2019-03-16 11:31:40 -070037#include "statistics-collector.hpp"
Junxiao Shif8606492017-07-23 03:44:34 +000038#include "core/version.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010039
Junxiao Shif8606492017-07-23 03:44:34 +000040#include <ndn-cxx/security/validator-null.hpp>
Davide Pesaventof8a14d82021-03-12 00:42:03 -050041#include <ndn-cxx/util/rtt-estimator.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010042
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050043#include <boost/program_options/options_description.hpp>
44#include <boost/program_options/parsers.hpp>
45#include <boost/program_options/variables_map.hpp>
46
Davide Pesavento5748e822024-01-26 18:40:22 -050047#include <fstream>
48#include <iostream>
49
Davide Pesaventob3570c62022-02-19 19:19:00 -050050namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010051
Davide Pesaventof8a14d82021-03-12 00:42:03 -050052namespace po = boost::program_options;
53
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040055main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056{
Davide Pesaventof8a14d82021-03-12 00:42:03 -050057 const std::string programName(argv[0]);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010058
Davide Pesavento97a33b22019-10-17 22:10:47 -040059 Options options;
Davide Pesaventof8a14d82021-03-12 00:42:03 -050060 std::string prefix, nameConv, pipelineType("cubic");
61 std::string cwndPath, rttPath;
Davide Pesavento5748e822024-01-26 18:40:22 -050062 auto rttEstOptions = std::make_shared<util::RttEstimator::Options>();
Davide Pesaventof8a14d82021-03-12 00:42:03 -050063 rttEstOptions->k = 8; // increased from the ndn-cxx default of 4
Weiwei Liu245d7912016-07-28 00:04:25 -070064
Weiwei Liu245d7912016-07-28 00:04:25 -070065 po::options_description basicDesc("Basic Options");
66 basicDesc.add_options()
Andrea Tosatto672b9a72016-01-05 16:18:20 +010067 ("help,h", "print this help message and exit")
Davide Pesavento296b3852019-10-20 16:00:16 -040068 ("fresh,f", po::bool_switch(&options.mustBeFresh),
69 "only return fresh content (set MustBeFresh on all outgoing Interests)")
Davide Pesavento0da1f442019-07-26 17:38:13 -040070 ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071 "lifetime of expressed Interests, in milliseconds")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010072 ("retries,r", po::value<int>(&options.maxRetriesOnTimeoutOrNack)->default_value(options.maxRetriesOnTimeoutOrNack),
73 "maximum number of retries in case of Nack or timeout (-1 = no limit)")
Davide Pesaventof8a14d82021-03-12 00:42:03 -050074 ("pipeline-type,p", po::value<std::string>(&pipelineType)->default_value(pipelineType),
75 "type of Interest pipeline to use; valid values are: 'fixed', 'aimd', 'cubic'")
Davide Pesavento296b3852019-10-20 16:00:16 -040076 ("no-version-discovery,D", po::bool_switch(&options.disableVersionDiscovery),
Davide Pesaventof8a14d82021-03-12 00:42:03 -050077 "skip version discovery even if the name does not end with a version component")
78 ("naming-convention,N", po::value<std::string>(&nameConv),
79 "encoding convention to use for name components, either 'marker' or 'typed'")
Davide Pesaventof6991e12018-01-08 20:58:50 -050080 ("quiet,q", po::bool_switch(&options.isQuiet), "suppress all diagnostic output, except fatal errors")
81 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output (per segment information")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010082 ("version,V", "print program version and exit")
83 ;
84
Weiwei Liu245d7912016-07-28 00:04:25 -070085 po::options_description fixedPipeDesc("Fixed pipeline options");
86 fixedPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -040087 ("pipeline-size,s", po::value<size_t>(&options.maxPipelineSize)->default_value(options.maxPipelineSize),
Weiwei Liu245d7912016-07-28 00:04:25 -070088 "size of the Interest pipeline")
89 ;
90
Klaus Schneider9e5122b2019-03-19 17:03:25 -070091 po::options_description adaptivePipeDesc("Adaptive pipeline options (AIMD & CUBIC)");
schneiderklausd8197df2019-03-16 11:31:40 -070092 adaptivePipeDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -050093 ("ignore-marks", po::bool_switch(&options.ignoreCongMarks),
94 "do not reduce the window after receiving a congestion mark")
95 ("disable-cwa", po::bool_switch(&options.disableCwa),
96 "disable Conservative Window Adaptation (reduce the window "
97 "on each congestion event instead of at most once per RTT)")
Davide Pesavento97a33b22019-10-17 22:10:47 -040098 ("init-cwnd", po::value<double>(&options.initCwnd)->default_value(options.initCwnd),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070099 "initial congestion window in segments")
Davide Pesavento97a33b22019-10-17 22:10:47 -0400100 ("init-ssthresh", po::value<double>(&options.initSsthresh),
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700101 "initial slow start threshold in segments (defaults to infinity)")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500102 ("rto-alpha", po::value<double>(&rttEstOptions->alpha)->default_value(rttEstOptions->alpha),
Davide Pesaventoba560662019-06-26 22:45:44 -0400103 "alpha value for RTO calculation")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500104 ("rto-beta", po::value<double>(&rttEstOptions->beta)->default_value(rttEstOptions->beta),
Davide Pesaventoba560662019-06-26 22:45:44 -0400105 "beta value for RTO calculation")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500106 ("rto-k", po::value<int>(&rttEstOptions->k)->default_value(rttEstOptions->k),
Davide Pesaventoba560662019-06-26 22:45:44 -0400107 "k value for RTO calculation")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500108 ("min-rto", po::value<time::milliseconds::rep>()->default_value(
109 time::duration_cast<time::milliseconds>(rttEstOptions->minRto).count()),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400110 "minimum RTO value, in milliseconds")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500111 ("max-rto", po::value<time::milliseconds::rep>()->default_value(
112 time::duration_cast<time::milliseconds>(rttEstOptions->maxRto).count()),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400113 "maximum RTO value, in milliseconds")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700114 ("log-cwnd", po::value<std::string>(&cwndPath), "log file for congestion window stats")
115 ("log-rtt", po::value<std::string>(&rttPath), "log file for round-trip time stats")
116 ;
117
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500118 po::options_description aimdPipeDesc("AIMD pipeline options");
119 aimdPipeDesc.add_options()
120 ("aimd-step", po::value<double>(&options.aiStep)->default_value(options.aiStep),
121 "additive increase step")
122 ("aimd-beta", po::value<double>(&options.mdCoef)->default_value(options.mdCoef),
123 "multiplicative decrease factor")
124 ("reset-cwnd-to-init", po::bool_switch(&options.resetCwndToInit),
125 "after a congestion event, reset the window to the "
126 "initial value instead of resetting to ssthresh")
127 ;
128
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700129 po::options_description cubicPipeDesc("CUBIC pipeline options");
130 cubicPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -0400131 ("cubic-beta", po::value<double>(&options.cubicBeta), "window decrease factor (defaults to 0.7)")
132 ("fast-conv", po::bool_switch(&options.enableFastConv), "enable fast convergence")
Weiwei Liu245d7912016-07-28 00:04:25 -0700133 ;
134
135 po::options_description visibleDesc;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700136 visibleDesc.add(basicDesc)
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700137 .add(fixedPipeDesc)
138 .add(adaptivePipeDesc)
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500139 .add(aimdPipeDesc)
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700140 .add(cubicPipeDesc);
Weiwei Liu245d7912016-07-28 00:04:25 -0700141
142 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100143 hiddenDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500144 ("name", po::value<std::string>(&prefix), "NDN name of the requested content");
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100145
Davide Pesavento6752d942019-07-26 16:26:32 -0400146 po::options_description optDesc;
147 optDesc.add(visibleDesc).add(hiddenDesc);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700148
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100149 po::positional_options_description p;
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500150 p.add("name", -1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100151
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100152 po::variables_map vm;
153 try {
154 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
155 po::notify(vm);
156 }
157 catch (const po::error& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500158 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100159 return 2;
160 }
161 catch (const boost::bad_any_cast& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500162 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100163 return 2;
164 }
165
166 if (vm.count("help") > 0) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500167 std::cout << "Usage: " << programName << " [options] ndn:/name\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100168 std::cout << visibleDesc;
169 return 0;
170 }
171
172 if (vm.count("version") > 0) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500173 std::cout << "ndncatchunks " << tools::VERSION << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100174 return 0;
175 }
176
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500177 if (prefix.empty()) {
178 std::cerr << "Usage: " << programName << " [options] ndn:/name\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100179 std::cerr << visibleDesc;
180 return 2;
181 }
182
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500183 if (nameConv == "marker" || nameConv == "m" || nameConv == "1") {
184 name::setConventionEncoding(name::Convention::MARKER);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100185 }
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500186 else if (nameConv == "typed" || nameConv == "t" || nameConv == "2") {
187 name::setConventionEncoding(name::Convention::TYPED);
188 }
189 else if (!nameConv.empty()) {
190 std::cerr << "ERROR: '" << nameConv << "' is not a valid naming convention\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100191 return 2;
192 }
193
Davide Pesavento0da1f442019-07-26 17:38:13 -0400194 options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
195 if (options.interestLifetime < 0_ms) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500196 std::cerr << "ERROR: --lifetime cannot be negative\n";
197 return 2;
198 }
199
200 if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
201 std::cerr << "ERROR: --retries must be between -1 and 1024\n";
Klaus Schneider7072e162017-09-16 13:43:00 -0700202 return 2;
203 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100204
Davide Pesaventof6991e12018-01-08 20:58:50 -0500205 if (options.isQuiet && options.isVerbose) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500206 std::cerr << "ERROR: cannot be quiet and verbose at the same time\n";
207 return 2;
208 }
209
210 if (options.maxPipelineSize < 1 || options.maxPipelineSize > 1024) {
211 std::cerr << "ERROR: --pipeline-size must be between 1 and 1024\n";
212 return 2;
213 }
214
215 if (rttEstOptions->k < 0) {
216 std::cerr << "ERROR: --rto-k cannot be negative\n";
217 return 2;
218 }
219
220 rttEstOptions->minRto = time::milliseconds(vm["min-rto"].as<time::milliseconds::rep>());
221 if (rttEstOptions->minRto < 0_ms) {
222 std::cerr << "ERROR: --min-rto cannot be negative\n";
223 return 2;
224 }
225
226 rttEstOptions->maxRto = time::milliseconds(vm["max-rto"].as<time::milliseconds::rep>());
227 if (rttEstOptions->maxRto < rttEstOptions->minRto) {
228 std::cerr << "ERROR: --max-rto cannot be smaller than --min-rto\n";
Davide Pesaventof6991e12018-01-08 20:58:50 -0500229 return 2;
230 }
231
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100232 try {
233 Face face;
Davide Pesavento5748e822024-01-26 18:40:22 -0500234 auto discover = std::make_unique<DiscoverVersion>(face, Name(prefix), options);
235 std::unique_ptr<PipelineInterests> pipeline;
236 std::unique_ptr<StatisticsCollector> statsCollector;
237 std::unique_ptr<RttEstimatorWithStats> rttEstimator;
Weiwei Liu245d7912016-07-28 00:04:25 -0700238 std::ofstream statsFileCwnd;
239 std::ofstream statsFileRtt;
240
Weiwei Liue4765012016-06-01 00:10:29 -0700241 if (pipelineType == "fixed") {
Davide Pesavento5748e822024-01-26 18:40:22 -0500242 pipeline = std::make_unique<PipelineInterestsFixed>(face, options);
Weiwei Liue4765012016-06-01 00:10:29 -0700243 }
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700244 else if (pipelineType == "aimd" || pipelineType == "cubic") {
Davide Pesavento70576402019-06-07 16:42:21 -0400245 if (options.isVerbose) {
Davide Pesaventoba560662019-06-26 22:45:44 -0400246 using namespace ndn::time;
Davide Pesavento70576402019-06-07 16:42:21 -0400247 std::cerr << "RTT estimator parameters:\n"
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500248 << "\tAlpha = " << rttEstOptions->alpha << "\n"
249 << "\tBeta = " << rttEstOptions->beta << "\n"
250 << "\tK = " << rttEstOptions->k << "\n"
251 << "\tInitial RTO = " << duration_cast<milliseconds>(rttEstOptions->initialRto) << "\n"
252 << "\tMin RTO = " << duration_cast<milliseconds>(rttEstOptions->minRto) << "\n"
253 << "\tMax RTO = " << duration_cast<milliseconds>(rttEstOptions->maxRto) << "\n"
254 << "\tBackoff multiplier = " << rttEstOptions->rtoBackoffMultiplier << "\n";
Davide Pesavento70576402019-06-07 16:42:21 -0400255 }
Davide Pesavento5748e822024-01-26 18:40:22 -0500256 rttEstimator = std::make_unique<RttEstimatorWithStats>(std::move(rttEstOptions));
Davide Pesavento70576402019-06-07 16:42:21 -0400257
Davide Pesavento5748e822024-01-26 18:40:22 -0500258 std::unique_ptr<PipelineInterestsAdaptive> adaptivePipeline;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700259 if (pipelineType == "aimd") {
Davide Pesavento5748e822024-01-26 18:40:22 -0500260 adaptivePipeline = std::make_unique<PipelineInterestsAimd>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700261 }
262 else {
Davide Pesavento5748e822024-01-26 18:40:22 -0500263 adaptivePipeline = std::make_unique<PipelineInterestsCubic>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700264 }
Weiwei Liu245d7912016-07-28 00:04:25 -0700265
266 if (!cwndPath.empty() || !rttPath.empty()) {
267 if (!cwndPath.empty()) {
268 statsFileCwnd.open(cwndPath);
269 if (statsFileCwnd.fail()) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500270 std::cerr << "ERROR: failed to open '" << cwndPath << "'\n";
Weiwei Liu245d7912016-07-28 00:04:25 -0700271 return 4;
272 }
273 }
274 if (!rttPath.empty()) {
275 statsFileRtt.open(rttPath);
276 if (statsFileRtt.fail()) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500277 std::cerr << "ERROR: failed to open '" << rttPath << "'\n";
Weiwei Liu245d7912016-07-28 00:04:25 -0700278 return 4;
279 }
280 }
Davide Pesavento5748e822024-01-26 18:40:22 -0500281 statsCollector = std::make_unique<StatisticsCollector>(*adaptivePipeline, statsFileCwnd, statsFileRtt);
Weiwei Liu245d7912016-07-28 00:04:25 -0700282 }
283
schneiderklausd8197df2019-03-16 11:31:40 -0700284 pipeline = std::move(adaptivePipeline);
Weiwei Liu245d7912016-07-28 00:04:25 -0700285 }
Weiwei Liue4765012016-06-01 00:10:29 -0700286 else {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500287 std::cerr << "ERROR: '" << pipelineType << "' is not a valid pipeline type\n";
Weiwei Liue4765012016-06-01 00:10:29 -0700288 return 2;
289 }
290
Alexander Afanasyev28181ee2020-06-03 13:58:47 -0400291 Consumer consumer(security::getAcceptAllValidator());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100292 BOOST_ASSERT(discover != nullptr);
Weiwei Liue4765012016-06-01 00:10:29 -0700293 BOOST_ASSERT(pipeline != nullptr);
294 consumer.run(std::move(discover), std::move(pipeline));
Weiwei Liu05d92092016-07-19 17:34:33 -0700295 face.processEvents();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100296 }
297 catch (const Consumer::ApplicationNackError& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500298 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100299 return 3;
300 }
Davide Pesaventof6991e12018-01-08 20:58:50 -0500301 catch (const Consumer::DataValidationError& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500302 std::cerr << "ERROR: " << e.what() << "\n";
Davide Pesaventof6991e12018-01-08 20:58:50 -0500303 return 5;
304 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100305 catch (const std::exception& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500306 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100307 return 1;
308 }
309
310 return 0;
311}
312
Davide Pesaventob3570c62022-02-19 19:19:00 -0500313} // namespace ndn::chunks
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100314
315int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400316main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100317{
318 return ndn::chunks::main(argc, argv);
319}