blob: 737f2aa98248816b5c5e230a866761a75262fdbc [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/*
Chavoosh Ghasemid8f9af22019-02-28 09:47:26 -08003 * Copyright (c) 2016-2019, 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"
33#include "discover-version-fixed.hpp"
Chavoosh Ghasemid8f9af22019-02-28 09:47:26 -080034#include "discover-version-realtime.hpp"
Junxiao Shif8606492017-07-23 03:44:34 +000035#include "options.hpp"
schneiderklausd8197df2019-03-16 11:31:40 -070036#include "pipeline-interests-adaptive.hpp"
37#include "pipeline-interests-fixed.hpp"
38#include "rtt-estimator.hpp"
39#include "statistics-collector.hpp"
Junxiao Shif8606492017-07-23 03:44:34 +000040#include "core/version.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010041
Weiwei Liu245d7912016-07-28 00:04:25 -070042#include <fstream>
Junxiao Shif8606492017-07-23 03:44:34 +000043#include <ndn-cxx/security/validator-null.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010044
45namespace ndn {
46namespace chunks {
47
48static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040049main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010050{
51 std::string programName(argv[0]);
52 Options options;
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080053 std::string discoverType("realtime");
Klaus Schneider2c3083d2017-12-18 14:19:04 -070054 std::string pipelineType("aimd");
Andrea Tosatto672b9a72016-01-05 16:18:20 +010055 size_t maxPipelineSize(1);
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080056 int64_t discoveryTimeoutMs(DEFAULT_INTEREST_LIFETIME.count());
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057 std::string uri;
58
Weiwei Liu245d7912016-07-28 00:04:25 -070059 // congestion control parameters, CWA refers to conservative window adaptation,
60 // i.e. only reduce window size at most once per RTT
Chavoosh Ghasemi641f5932017-11-06 22:45:11 +000061 bool disableCwa(false), resetCwndToInit(false), ignoreCongMarks(false);
Weiwei Liu245d7912016-07-28 00:04:25 -070062 double aiStep(1.0), mdCoef(0.5), alpha(0.125), beta(0.25),
63 minRto(200.0), maxRto(4000.0);
64 int initCwnd(1), initSsthresh(std::numeric_limits<int>::max()), k(4);
65 std::string cwndPath, rttPath;
66
Andrea Tosatto672b9a72016-01-05 16:18:20 +010067 namespace po = boost::program_options;
Weiwei Liu245d7912016-07-28 00:04:25 -070068 po::options_description basicDesc("Basic Options");
69 basicDesc.add_options()
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070 ("help,h", "print this help message and exit")
Klaus Schneider7072e162017-09-16 13:43:00 -070071 ("discover-version,d", po::value<std::string>(&discoverType)->default_value(discoverType),
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080072 "version discovery algorithm to use; valid values are: 'fixed', 'realtime'")
Klaus Schneider7072e162017-09-16 13:43:00 -070073 ("pipeline-type,p", po::value<std::string>(&pipelineType)->default_value(pipelineType),
Weiwei Liu245d7912016-07-28 00:04:25 -070074 "type of Interest pipeline to use; valid values are: 'fixed', 'aimd'")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010075 ("fresh,f", po::bool_switch(&options.mustBeFresh), "only return fresh content")
Klaus Schneider7072e162017-09-16 13:43:00 -070076 ("lifetime,l", po::value<int64_t>()->default_value(options.interestLifetime.count()),
Andrea Tosatto672b9a72016-01-05 16:18:20 +010077 "lifetime of expressed Interests, in milliseconds")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010078 ("retries,r", po::value<int>(&options.maxRetriesOnTimeoutOrNack)->default_value(options.maxRetriesOnTimeoutOrNack),
79 "maximum number of retries in case of Nack or timeout (-1 = no limit)")
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
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080085 po::options_description realDiscoveryDesc("Realtime version discovery options");
86 realDiscoveryDesc.add_options()
Klaus Schneider7072e162017-09-16 13:43:00 -070087 ("discovery-timeout,t", po::value<int64_t>(&discoveryTimeoutMs)->default_value(discoveryTimeoutMs),
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080088 "discovery timeout (in milliseconds)");
Weiwei Liu245d7912016-07-28 00:04:25 -070089
90 po::options_description fixedPipeDesc("Fixed pipeline options");
91 fixedPipeDesc.add_options()
92 ("pipeline-size,s", po::value<size_t>(&maxPipelineSize)->default_value(maxPipelineSize),
93 "size of the Interest pipeline")
94 ;
95
schneiderklausd8197df2019-03-16 11:31:40 -070096 po::options_description adaptivePipeDesc("Adaptive pipeline options (AIMD)");
97 adaptivePipeDesc.add_options()
98 ("log-cwnd", po::value<std::string>(&cwndPath), "log file for cwnd statistics")
99 ("log-rtt", po::value<std::string>(&rttPath), "log file for rtt statistics")
100 ("disable-cwa", po::bool_switch(&disableCwa),
101 "disable Conservative Window Adaptation, "
102 "i.e. reduce window on each congestion event (timeout or congestion mark) "
103 "instead of at most once per RTT")
104 ("ignore-marks", po::bool_switch(&ignoreCongMarks),
105 "ignore congestion marks, "
106 "the default is to decrease the window after receiving a congestion mark")
107 ("reset-cwnd-to-init", po::bool_switch(&resetCwndToInit),
108 "reset cwnd to initial value after loss/mark, default is "
109 "resetting to ssthresh")
110 ("init-cwnd", po::value<int>(&initCwnd)->default_value(initCwnd), "initial cwnd")
111 ("init-ssthresh", po::value<int>(&initSsthresh),
112 "initial slow start threshold (defaults to infinity)")
113 ("aistep", po::value<double>(&aiStep)->default_value(aiStep),
114 "additive-increase step")
115 ("mdcoef", po::value<double>(&mdCoef)->default_value(mdCoef),
116 "multiplicative-decrease coefficient")
117 ("rto-alpha", po::value<double>(&alpha)->default_value(alpha),
118 "alpha value for rto calculation")
119 ("rto-beta", po::value<double>(&beta)->default_value(beta),
120 "beta value for rto calculation")
121 ("rto-k", po::value<int>(&k)->default_value(k),
122 "k value for rto calculation")
123 ("min-rto", po::value<double>(&minRto)->default_value(minRto),
124 "minimum rto value in milliseconds")
125 ("max-rto", po::value<double>(&maxRto)->default_value(maxRto),
126 "maximum rto value in milliseconds")
Weiwei Liu245d7912016-07-28 00:04:25 -0700127 ;
128
129 po::options_description visibleDesc;
schneiderklausd8197df2019-03-16 11:31:40 -0700130 visibleDesc.add(basicDesc).add(realDiscoveryDesc).add(fixedPipeDesc).add(adaptivePipeDesc);
Weiwei Liu245d7912016-07-28 00:04:25 -0700131
132 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100133 hiddenDesc.add_options()
134 ("ndn-name,n", po::value<std::string>(&uri), "NDN name of the requested content");
135
136 po::positional_options_description p;
137 p.add("ndn-name", -1);
138
Weiwei Liu245d7912016-07-28 00:04:25 -0700139 po::options_description optDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100140 optDesc.add(visibleDesc).add(hiddenDesc);
141
142 po::variables_map vm;
143 try {
144 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
145 po::notify(vm);
146 }
147 catch (const po::error& e) {
148 std::cerr << "ERROR: " << e.what() << std::endl;
149 return 2;
150 }
151 catch (const boost::bad_any_cast& e) {
152 std::cerr << "ERROR: " << e.what() << std::endl;
153 return 2;
154 }
155
156 if (vm.count("help") > 0) {
157 std::cout << "Usage: " << programName << " [options] ndn:/name" << std::endl;
158 std::cout << visibleDesc;
159 return 0;
160 }
161
162 if (vm.count("version") > 0) {
163 std::cout << "ndncatchunks " << tools::VERSION << std::endl;
164 return 0;
165 }
166
167 if (vm.count("ndn-name") == 0) {
168 std::cerr << "Usage: " << programName << " [options] ndn:/name" << std::endl;
169 std::cerr << visibleDesc;
170 return 2;
171 }
172
173 Name prefix(uri);
174 if (discoverType == "fixed" && (prefix.empty() || !prefix[-1].isVersion())) {
175 std::cerr << "ERROR: The specified name must contain a version component when using "
176 "fixed version discovery" << std::endl;
177 return 2;
178 }
179
180 if (maxPipelineSize < 1 || maxPipelineSize > 1024) {
181 std::cerr << "ERROR: pipeline size must be between 1 and 1024" << std::endl;
182 return 2;
183 }
184
185 if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
186 std::cerr << "ERROR: retries value must be between -1 and 1024" << std::endl;
187 return 2;
188 }
189
Klaus Schneider7072e162017-09-16 13:43:00 -0700190 if (discoveryTimeoutMs < 0) {
191 std::cerr << "ERROR: timeout cannot be negative" << std::endl;
192 return 2;
193 }
194
195 if (vm["lifetime"].as<int64_t>() < 0) {
196 std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
197 return 2;
198 }
Klaus Schneider7072e162017-09-16 13:43:00 -0700199 options.interestLifetime = time::milliseconds(vm["lifetime"].as<int64_t>());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100200
Davide Pesaventof6991e12018-01-08 20:58:50 -0500201 if (options.isQuiet && options.isVerbose) {
202 std::cerr << "ERROR: cannot be quiet and verbose at the same time" << std::endl;
203 return 2;
204 }
205
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100206 try {
207 Face face;
208
209 unique_ptr<DiscoverVersion> discover;
210 if (discoverType == "fixed") {
211 discover = make_unique<DiscoverVersionFixed>(prefix, face, options);
212 }
Chavoosh Ghasemid8f9af22019-02-28 09:47:26 -0800213 else if (discoverType == "realtime") {
214 DiscoverVersionRealtime::Options optionsRealtime(options);
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -0800215 optionsRealtime.discoveryTimeout = time::milliseconds(discoveryTimeoutMs);
Chavoosh Ghasemid8f9af22019-02-28 09:47:26 -0800216 discover = make_unique<DiscoverVersionRealtime>(prefix, face, optionsRealtime);
217 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100218 else {
219 std::cerr << "ERROR: discover version type not valid" << std::endl;
220 return 2;
221 }
222
Weiwei Liue4765012016-06-01 00:10:29 -0700223 unique_ptr<PipelineInterests> pipeline;
schneiderklausd8197df2019-03-16 11:31:40 -0700224 unique_ptr<StatisticsCollector> statsCollector;
225 unique_ptr<RttEstimator> rttEstimator;
Weiwei Liu245d7912016-07-28 00:04:25 -0700226 std::ofstream statsFileCwnd;
227 std::ofstream statsFileRtt;
228
Weiwei Liue4765012016-06-01 00:10:29 -0700229 if (pipelineType == "fixed") {
schneiderklausd8197df2019-03-16 11:31:40 -0700230 PipelineInterestsFixed::Options optionsPipeline(options);
Weiwei Liue4765012016-06-01 00:10:29 -0700231 optionsPipeline.maxPipelineSize = maxPipelineSize;
schneiderklausd8197df2019-03-16 11:31:40 -0700232 pipeline = make_unique<PipelineInterestsFixed>(face, optionsPipeline);
Weiwei Liue4765012016-06-01 00:10:29 -0700233 }
Weiwei Liu245d7912016-07-28 00:04:25 -0700234 else if (pipelineType == "aimd") {
schneiderklausd8197df2019-03-16 11:31:40 -0700235 RttEstimator::Options optionsRttEst;
Weiwei Liu245d7912016-07-28 00:04:25 -0700236 optionsRttEst.isVerbose = options.isVerbose;
237 optionsRttEst.alpha = alpha;
238 optionsRttEst.beta = beta;
239 optionsRttEst.k = k;
schneiderklausd8197df2019-03-16 11:31:40 -0700240 optionsRttEst.minRto = Milliseconds(minRto);
241 optionsRttEst.maxRto = Milliseconds(maxRto);
Weiwei Liu245d7912016-07-28 00:04:25 -0700242
schneiderklausd8197df2019-03-16 11:31:40 -0700243 rttEstimator = make_unique<RttEstimator>(optionsRttEst);
Weiwei Liu245d7912016-07-28 00:04:25 -0700244
schneiderklausd8197df2019-03-16 11:31:40 -0700245 PipelineInterestsAdaptive::Options optionsPipeline(options);
Weiwei Liu245d7912016-07-28 00:04:25 -0700246 optionsPipeline.disableCwa = disableCwa;
247 optionsPipeline.resetCwndToInit = resetCwndToInit;
248 optionsPipeline.initCwnd = static_cast<double>(initCwnd);
249 optionsPipeline.initSsthresh = static_cast<double>(initSsthresh);
250 optionsPipeline.aiStep = aiStep;
251 optionsPipeline.mdCoef = mdCoef;
Chavoosh Ghasemi641f5932017-11-06 22:45:11 +0000252 optionsPipeline.ignoreCongMarks = ignoreCongMarks;
Weiwei Liu245d7912016-07-28 00:04:25 -0700253
schneiderklausd8197df2019-03-16 11:31:40 -0700254 auto adaptivePipeline = make_unique<PipelineInterestsAdaptive>(face, *rttEstimator, optionsPipeline);
Weiwei Liu245d7912016-07-28 00:04:25 -0700255
256 if (!cwndPath.empty() || !rttPath.empty()) {
257 if (!cwndPath.empty()) {
258 statsFileCwnd.open(cwndPath);
259 if (statsFileCwnd.fail()) {
260 std::cerr << "ERROR: failed to open " << cwndPath << std::endl;
261 return 4;
262 }
263 }
264 if (!rttPath.empty()) {
265 statsFileRtt.open(rttPath);
266 if (statsFileRtt.fail()) {
267 std::cerr << "ERROR: failed to open " << rttPath << std::endl;
268 return 4;
269 }
270 }
schneiderklausd8197df2019-03-16 11:31:40 -0700271 statsCollector = make_unique<StatisticsCollector>(*adaptivePipeline, *rttEstimator,
272 statsFileCwnd, statsFileRtt);
Weiwei Liu245d7912016-07-28 00:04:25 -0700273 }
274
schneiderklausd8197df2019-03-16 11:31:40 -0700275 pipeline = std::move(adaptivePipeline);
Weiwei Liu245d7912016-07-28 00:04:25 -0700276 }
Weiwei Liue4765012016-06-01 00:10:29 -0700277 else {
278 std::cerr << "ERROR: Interest pipeline type not valid" << std::endl;
279 return 2;
280 }
281
Davide Pesaventof6991e12018-01-08 20:58:50 -0500282 Consumer consumer(security::v2::getAcceptAllValidator());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100283 BOOST_ASSERT(discover != nullptr);
Weiwei Liue4765012016-06-01 00:10:29 -0700284 BOOST_ASSERT(pipeline != nullptr);
285 consumer.run(std::move(discover), std::move(pipeline));
Weiwei Liu05d92092016-07-19 17:34:33 -0700286 face.processEvents();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100287 }
288 catch (const Consumer::ApplicationNackError& e) {
289 std::cerr << "ERROR: " << e.what() << std::endl;
290 return 3;
291 }
Davide Pesaventof6991e12018-01-08 20:58:50 -0500292 catch (const Consumer::DataValidationError& e) {
293 std::cerr << "ERROR: " << e.what() << std::endl;
294 return 5;
295 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100296 catch (const std::exception& e) {
297 std::cerr << "ERROR: " << e.what() << std::endl;
298 return 1;
299 }
300
301 return 0;
302}
303
304} // namespace chunks
305} // namespace ndn
306
307int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400308main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100309{
310 return ndn::chunks::main(argc, argv);
311}