blob: e89d9047750098f9f745aff4bce438678b5f395f [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 Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2016-2022, 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
Weiwei Liu245d7912016-07-28 00:04:25 -070040#include <fstream>
Junxiao Shif8606492017-07-23 03:44:34 +000041#include <ndn-cxx/security/validator-null.hpp>
Davide Pesaventof8a14d82021-03-12 00:42:03 -050042#include <ndn-cxx/util/rtt-estimator.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050044#include <boost/program_options/options_description.hpp>
45#include <boost/program_options/parsers.hpp>
46#include <boost/program_options/variables_map.hpp>
47
Davide Pesaventob3570c62022-02-19 19:19:00 -050048namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010049
Davide Pesaventof8a14d82021-03-12 00:42:03 -050050namespace po = boost::program_options;
51
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040053main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054{
Davide Pesaventof8a14d82021-03-12 00:42:03 -050055 const std::string programName(argv[0]);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056
Davide Pesavento97a33b22019-10-17 22:10:47 -040057 Options options;
Davide Pesaventof8a14d82021-03-12 00:42:03 -050058 std::string prefix, nameConv, pipelineType("cubic");
59 std::string cwndPath, rttPath;
60 auto rttEstOptions = make_shared<util::RttEstimator::Options>();
61 rttEstOptions->k = 8; // increased from the ndn-cxx default of 4
Weiwei Liu245d7912016-07-28 00:04:25 -070062
Weiwei Liu245d7912016-07-28 00:04:25 -070063 po::options_description basicDesc("Basic Options");
64 basicDesc.add_options()
Andrea Tosatto672b9a72016-01-05 16:18:20 +010065 ("help,h", "print this help message and exit")
Davide Pesavento296b3852019-10-20 16:00:16 -040066 ("fresh,f", po::bool_switch(&options.mustBeFresh),
67 "only return fresh content (set MustBeFresh on all outgoing Interests)")
Davide Pesavento0da1f442019-07-26 17:38:13 -040068 ("lifetime,l", po::value<time::milliseconds::rep>()->default_value(options.interestLifetime.count()),
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069 "lifetime of expressed Interests, in milliseconds")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070 ("retries,r", po::value<int>(&options.maxRetriesOnTimeoutOrNack)->default_value(options.maxRetriesOnTimeoutOrNack),
71 "maximum number of retries in case of Nack or timeout (-1 = no limit)")
Davide Pesaventof8a14d82021-03-12 00:42:03 -050072 ("pipeline-type,p", po::value<std::string>(&pipelineType)->default_value(pipelineType),
73 "type of Interest pipeline to use; valid values are: 'fixed', 'aimd', 'cubic'")
Davide Pesavento296b3852019-10-20 16:00:16 -040074 ("no-version-discovery,D", po::bool_switch(&options.disableVersionDiscovery),
Davide Pesaventof8a14d82021-03-12 00:42:03 -050075 "skip version discovery even if the name does not end with a version component")
76 ("naming-convention,N", po::value<std::string>(&nameConv),
77 "encoding convention to use for name components, either 'marker' or 'typed'")
Davide Pesaventof6991e12018-01-08 20:58:50 -050078 ("quiet,q", po::bool_switch(&options.isQuiet), "suppress all diagnostic output, except fatal errors")
79 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output (per segment information")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080 ("version,V", "print program version and exit")
81 ;
82
Weiwei Liu245d7912016-07-28 00:04:25 -070083 po::options_description fixedPipeDesc("Fixed pipeline options");
84 fixedPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -040085 ("pipeline-size,s", po::value<size_t>(&options.maxPipelineSize)->default_value(options.maxPipelineSize),
Weiwei Liu245d7912016-07-28 00:04:25 -070086 "size of the Interest pipeline")
87 ;
88
Klaus Schneider9e5122b2019-03-19 17:03:25 -070089 po::options_description adaptivePipeDesc("Adaptive pipeline options (AIMD & CUBIC)");
schneiderklausd8197df2019-03-16 11:31:40 -070090 adaptivePipeDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -050091 ("ignore-marks", po::bool_switch(&options.ignoreCongMarks),
92 "do not reduce the window after receiving a congestion mark")
93 ("disable-cwa", po::bool_switch(&options.disableCwa),
94 "disable Conservative Window Adaptation (reduce the window "
95 "on each congestion event instead of at most once per RTT)")
Davide Pesavento97a33b22019-10-17 22:10:47 -040096 ("init-cwnd", po::value<double>(&options.initCwnd)->default_value(options.initCwnd),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070097 "initial congestion window in segments")
Davide Pesavento97a33b22019-10-17 22:10:47 -040098 ("init-ssthresh", po::value<double>(&options.initSsthresh),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070099 "initial slow start threshold in segments (defaults to infinity)")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500100 ("rto-alpha", po::value<double>(&rttEstOptions->alpha)->default_value(rttEstOptions->alpha),
Davide Pesaventoba560662019-06-26 22:45:44 -0400101 "alpha value for RTO calculation")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500102 ("rto-beta", po::value<double>(&rttEstOptions->beta)->default_value(rttEstOptions->beta),
Davide Pesaventoba560662019-06-26 22:45:44 -0400103 "beta value for RTO calculation")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500104 ("rto-k", po::value<int>(&rttEstOptions->k)->default_value(rttEstOptions->k),
Davide Pesaventoba560662019-06-26 22:45:44 -0400105 "k value for RTO calculation")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500106 ("min-rto", po::value<time::milliseconds::rep>()->default_value(
107 time::duration_cast<time::milliseconds>(rttEstOptions->minRto).count()),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400108 "minimum RTO value, in milliseconds")
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500109 ("max-rto", po::value<time::milliseconds::rep>()->default_value(
110 time::duration_cast<time::milliseconds>(rttEstOptions->maxRto).count()),
Davide Pesavento0da1f442019-07-26 17:38:13 -0400111 "maximum RTO value, in milliseconds")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700112 ("log-cwnd", po::value<std::string>(&cwndPath), "log file for congestion window stats")
113 ("log-rtt", po::value<std::string>(&rttPath), "log file for round-trip time stats")
114 ;
115
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500116 po::options_description aimdPipeDesc("AIMD pipeline options");
117 aimdPipeDesc.add_options()
118 ("aimd-step", po::value<double>(&options.aiStep)->default_value(options.aiStep),
119 "additive increase step")
120 ("aimd-beta", po::value<double>(&options.mdCoef)->default_value(options.mdCoef),
121 "multiplicative decrease factor")
122 ("reset-cwnd-to-init", po::bool_switch(&options.resetCwndToInit),
123 "after a congestion event, reset the window to the "
124 "initial value instead of resetting to ssthresh")
125 ;
126
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700127 po::options_description cubicPipeDesc("CUBIC pipeline options");
128 cubicPipeDesc.add_options()
Davide Pesavento97a33b22019-10-17 22:10:47 -0400129 ("cubic-beta", po::value<double>(&options.cubicBeta), "window decrease factor (defaults to 0.7)")
130 ("fast-conv", po::bool_switch(&options.enableFastConv), "enable fast convergence")
Weiwei Liu245d7912016-07-28 00:04:25 -0700131 ;
132
133 po::options_description visibleDesc;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700134 visibleDesc.add(basicDesc)
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700135 .add(fixedPipeDesc)
136 .add(adaptivePipeDesc)
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500137 .add(aimdPipeDesc)
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700138 .add(cubicPipeDesc);
Weiwei Liu245d7912016-07-28 00:04:25 -0700139
140 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100141 hiddenDesc.add_options()
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500142 ("name", po::value<std::string>(&prefix), "NDN name of the requested content");
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100143
Davide Pesavento6752d942019-07-26 16:26:32 -0400144 po::options_description optDesc;
145 optDesc.add(visibleDesc).add(hiddenDesc);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700146
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100147 po::positional_options_description p;
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500148 p.add("name", -1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100149
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100150 po::variables_map vm;
151 try {
152 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
153 po::notify(vm);
154 }
155 catch (const po::error& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500156 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100157 return 2;
158 }
159 catch (const boost::bad_any_cast& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500160 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100161 return 2;
162 }
163
164 if (vm.count("help") > 0) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500165 std::cout << "Usage: " << programName << " [options] ndn:/name\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100166 std::cout << visibleDesc;
167 return 0;
168 }
169
170 if (vm.count("version") > 0) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500171 std::cout << "ndncatchunks " << tools::VERSION << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100172 return 0;
173 }
174
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500175 if (prefix.empty()) {
176 std::cerr << "Usage: " << programName << " [options] ndn:/name\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100177 std::cerr << visibleDesc;
178 return 2;
179 }
180
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500181 if (nameConv == "marker" || nameConv == "m" || nameConv == "1") {
182 name::setConventionEncoding(name::Convention::MARKER);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100183 }
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500184 else if (nameConv == "typed" || nameConv == "t" || nameConv == "2") {
185 name::setConventionEncoding(name::Convention::TYPED);
186 }
187 else if (!nameConv.empty()) {
188 std::cerr << "ERROR: '" << nameConv << "' is not a valid naming convention\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100189 return 2;
190 }
191
Davide Pesavento0da1f442019-07-26 17:38:13 -0400192 options.interestLifetime = time::milliseconds(vm["lifetime"].as<time::milliseconds::rep>());
193 if (options.interestLifetime < 0_ms) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500194 std::cerr << "ERROR: --lifetime cannot be negative\n";
195 return 2;
196 }
197
198 if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
199 std::cerr << "ERROR: --retries must be between -1 and 1024\n";
Klaus Schneider7072e162017-09-16 13:43:00 -0700200 return 2;
201 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100202
Davide Pesaventof6991e12018-01-08 20:58:50 -0500203 if (options.isQuiet && options.isVerbose) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500204 std::cerr << "ERROR: cannot be quiet and verbose at the same time\n";
205 return 2;
206 }
207
208 if (options.maxPipelineSize < 1 || options.maxPipelineSize > 1024) {
209 std::cerr << "ERROR: --pipeline-size must be between 1 and 1024\n";
210 return 2;
211 }
212
213 if (rttEstOptions->k < 0) {
214 std::cerr << "ERROR: --rto-k cannot be negative\n";
215 return 2;
216 }
217
218 rttEstOptions->minRto = time::milliseconds(vm["min-rto"].as<time::milliseconds::rep>());
219 if (rttEstOptions->minRto < 0_ms) {
220 std::cerr << "ERROR: --min-rto cannot be negative\n";
221 return 2;
222 }
223
224 rttEstOptions->maxRto = time::milliseconds(vm["max-rto"].as<time::milliseconds::rep>());
225 if (rttEstOptions->maxRto < rttEstOptions->minRto) {
226 std::cerr << "ERROR: --max-rto cannot be smaller than --min-rto\n";
Davide Pesaventof6991e12018-01-08 20:58:50 -0500227 return 2;
228 }
229
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100230 try {
231 Face face;
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500232 auto discover = make_unique<DiscoverVersion>(face, Name(prefix), options);
Weiwei Liue4765012016-06-01 00:10:29 -0700233 unique_ptr<PipelineInterests> pipeline;
schneiderklausd8197df2019-03-16 11:31:40 -0700234 unique_ptr<StatisticsCollector> statsCollector;
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400235 unique_ptr<RttEstimatorWithStats> rttEstimator;
Weiwei Liu245d7912016-07-28 00:04:25 -0700236 std::ofstream statsFileCwnd;
237 std::ofstream statsFileRtt;
238
Weiwei Liue4765012016-06-01 00:10:29 -0700239 if (pipelineType == "fixed") {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400240 pipeline = make_unique<PipelineInterestsFixed>(face, options);
Weiwei Liue4765012016-06-01 00:10:29 -0700241 }
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700242 else if (pipelineType == "aimd" || pipelineType == "cubic") {
Davide Pesavento70576402019-06-07 16:42:21 -0400243 if (options.isVerbose) {
Davide Pesaventoba560662019-06-26 22:45:44 -0400244 using namespace ndn::time;
Davide Pesavento70576402019-06-07 16:42:21 -0400245 std::cerr << "RTT estimator parameters:\n"
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500246 << "\tAlpha = " << rttEstOptions->alpha << "\n"
247 << "\tBeta = " << rttEstOptions->beta << "\n"
248 << "\tK = " << rttEstOptions->k << "\n"
249 << "\tInitial RTO = " << duration_cast<milliseconds>(rttEstOptions->initialRto) << "\n"
250 << "\tMin RTO = " << duration_cast<milliseconds>(rttEstOptions->minRto) << "\n"
251 << "\tMax RTO = " << duration_cast<milliseconds>(rttEstOptions->maxRto) << "\n"
252 << "\tBackoff multiplier = " << rttEstOptions->rtoBackoffMultiplier << "\n";
Davide Pesavento70576402019-06-07 16:42:21 -0400253 }
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500254 rttEstimator = make_unique<RttEstimatorWithStats>(std::move(rttEstOptions));
Davide Pesavento70576402019-06-07 16:42:21 -0400255
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700256 unique_ptr<PipelineInterestsAdaptive> adaptivePipeline;
257 if (pipelineType == "aimd") {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400258 adaptivePipeline = make_unique<PipelineInterestsAimd>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700259 }
260 else {
Davide Pesavento97a33b22019-10-17 22:10:47 -0400261 adaptivePipeline = make_unique<PipelineInterestsCubic>(face, *rttEstimator, options);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700262 }
Weiwei Liu245d7912016-07-28 00:04:25 -0700263
264 if (!cwndPath.empty() || !rttPath.empty()) {
265 if (!cwndPath.empty()) {
266 statsFileCwnd.open(cwndPath);
267 if (statsFileCwnd.fail()) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500268 std::cerr << "ERROR: failed to open '" << cwndPath << "'\n";
Weiwei Liu245d7912016-07-28 00:04:25 -0700269 return 4;
270 }
271 }
272 if (!rttPath.empty()) {
273 statsFileRtt.open(rttPath);
274 if (statsFileRtt.fail()) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500275 std::cerr << "ERROR: failed to open '" << rttPath << "'\n";
Weiwei Liu245d7912016-07-28 00:04:25 -0700276 return 4;
277 }
278 }
Davide Pesavento5e3773d2019-08-22 15:35:08 -0400279 statsCollector = make_unique<StatisticsCollector>(*adaptivePipeline, statsFileCwnd, statsFileRtt);
Weiwei Liu245d7912016-07-28 00:04:25 -0700280 }
281
schneiderklausd8197df2019-03-16 11:31:40 -0700282 pipeline = std::move(adaptivePipeline);
Weiwei Liu245d7912016-07-28 00:04:25 -0700283 }
Weiwei Liue4765012016-06-01 00:10:29 -0700284 else {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500285 std::cerr << "ERROR: '" << pipelineType << "' is not a valid pipeline type\n";
Weiwei Liue4765012016-06-01 00:10:29 -0700286 return 2;
287 }
288
Alexander Afanasyev28181ee2020-06-03 13:58:47 -0400289 Consumer consumer(security::getAcceptAllValidator());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100290 BOOST_ASSERT(discover != nullptr);
Weiwei Liue4765012016-06-01 00:10:29 -0700291 BOOST_ASSERT(pipeline != nullptr);
292 consumer.run(std::move(discover), std::move(pipeline));
Weiwei Liu05d92092016-07-19 17:34:33 -0700293 face.processEvents();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100294 }
295 catch (const Consumer::ApplicationNackError& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500296 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100297 return 3;
298 }
Davide Pesaventof6991e12018-01-08 20:58:50 -0500299 catch (const Consumer::DataValidationError& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500300 std::cerr << "ERROR: " << e.what() << "\n";
Davide Pesaventof6991e12018-01-08 20:58:50 -0500301 return 5;
302 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100303 catch (const std::exception& e) {
Davide Pesaventof8a14d82021-03-12 00:42:03 -0500304 std::cerr << "ERROR: " << e.what() << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100305 return 1;
306 }
307
308 return 0;
309}
310
Davide Pesaventob3570c62022-02-19 19:19:00 -0500311} // namespace ndn::chunks
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100312
313int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400314main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100315{
316 return ndn::chunks::main(argc, argv);
317}