blob: 718fb492b5ced694c7704310ca26fde7c9188c10 [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"
Klaus Schneider9e5122b2019-03-19 17:03:25 -070036#include "pipeline-interests-aimd.hpp"
37#include "pipeline-interests-cubic.hpp"
schneiderklausd8197df2019-03-16 11:31:40 -070038#include "pipeline-interests-fixed.hpp"
39#include "rtt-estimator.hpp"
40#include "statistics-collector.hpp"
Junxiao Shif8606492017-07-23 03:44:34 +000041#include "core/version.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010042
Weiwei Liu245d7912016-07-28 00:04:25 -070043#include <fstream>
Junxiao Shif8606492017-07-23 03:44:34 +000044#include <ndn-cxx/security/validator-null.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010045
46namespace ndn {
47namespace chunks {
48
49static int
Davide Pesaventoda85e252019-03-18 11:42:01 -040050main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +010051{
52 std::string programName(argv[0]);
53 Options options;
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080054 std::string discoverType("realtime");
Klaus Schneider9e5122b2019-03-19 17:03:25 -070055 std::string pipelineType("cubic");
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056 size_t maxPipelineSize(1);
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080057 int64_t discoveryTimeoutMs(DEFAULT_INTEREST_LIFETIME.count());
Andrea Tosatto672b9a72016-01-05 16:18:20 +010058 std::string uri;
59
Weiwei Liu245d7912016-07-28 00:04:25 -070060 // congestion control parameters, CWA refers to conservative window adaptation,
61 // i.e. only reduce window size at most once per RTT
Klaus Schneider9e5122b2019-03-19 17:03:25 -070062 bool disableCwa(false), resetCwndToInit(false),
63 ignoreCongMarks(false), enableFastConv(false);
64 double aiStep(1.0), rtoAlpha(0.125), rtoBeta(0.25), minRto(200.0), maxRto(4000.0),
65 aimdBeta(0.5), cubicBeta(0.7);
66 int initCwnd(1), initSsthresh(std::numeric_limits<int>::max()), k(8);
Weiwei Liu245d7912016-07-28 00:04:25 -070067 std::string cwndPath, rttPath;
68
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069 namespace po = boost::program_options;
Weiwei Liu245d7912016-07-28 00:04:25 -070070 po::options_description basicDesc("Basic Options");
71 basicDesc.add_options()
Andrea Tosatto672b9a72016-01-05 16:18:20 +010072 ("help,h", "print this help message and exit")
Klaus Schneider7072e162017-09-16 13:43:00 -070073 ("discover-version,d", po::value<std::string>(&discoverType)->default_value(discoverType),
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080074 "version discovery algorithm to use; valid values are: 'fixed', 'realtime'")
Klaus Schneider7072e162017-09-16 13:43:00 -070075 ("pipeline-type,p", po::value<std::string>(&pipelineType)->default_value(pipelineType),
Klaus Schneider9e5122b2019-03-19 17:03:25 -070076 "type of Interest pipeline to use; valid values are: 'fixed', 'aimd', 'cubic'")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010077 ("fresh,f", po::bool_switch(&options.mustBeFresh), "only return fresh content")
Klaus Schneider7072e162017-09-16 13:43:00 -070078 ("lifetime,l", po::value<int64_t>()->default_value(options.interestLifetime.count()),
Andrea Tosatto672b9a72016-01-05 16:18:20 +010079 "lifetime of expressed Interests, in milliseconds")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080 ("retries,r", po::value<int>(&options.maxRetriesOnTimeoutOrNack)->default_value(options.maxRetriesOnTimeoutOrNack),
81 "maximum number of retries in case of Nack or timeout (-1 = no limit)")
Davide Pesaventof6991e12018-01-08 20:58:50 -050082 ("quiet,q", po::bool_switch(&options.isQuiet), "suppress all diagnostic output, except fatal errors")
83 ("verbose,v", po::bool_switch(&options.isVerbose), "turn on verbose output (per segment information")
Andrea Tosatto672b9a72016-01-05 16:18:20 +010084 ("version,V", "print program version and exit")
85 ;
86
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080087 po::options_description realDiscoveryDesc("Realtime version discovery options");
88 realDiscoveryDesc.add_options()
Klaus Schneider7072e162017-09-16 13:43:00 -070089 ("discovery-timeout,t", po::value<int64_t>(&discoveryTimeoutMs)->default_value(discoveryTimeoutMs),
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -080090 "discovery timeout (in milliseconds)");
Weiwei Liu245d7912016-07-28 00:04:25 -070091
92 po::options_description fixedPipeDesc("Fixed pipeline options");
93 fixedPipeDesc.add_options()
94 ("pipeline-size,s", po::value<size_t>(&maxPipelineSize)->default_value(maxPipelineSize),
95 "size of the Interest pipeline")
96 ;
97
Klaus Schneider9e5122b2019-03-19 17:03:25 -070098 po::options_description adaptivePipeDesc("Adaptive pipeline options (AIMD & CUBIC)");
schneiderklausd8197df2019-03-16 11:31:40 -070099 adaptivePipeDesc.add_options()
schneiderklausd8197df2019-03-16 11:31:40 -0700100 ("ignore-marks", po::bool_switch(&ignoreCongMarks),
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700101 "do not decrease the window after receiving a congestion mark")
102 ("disable-cwa", po::bool_switch(&disableCwa),
103 "disable Conservative Window Adaptation, i.e., reduce the window on "
104 "each timeout or congestion mark instead of at most once per RTT")
schneiderklausd8197df2019-03-16 11:31:40 -0700105 ("reset-cwnd-to-init", po::bool_switch(&resetCwndToInit),
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700106 "after a timeout or congestion mark, reset the window "
107 "to the initial value instead of resetting to ssthresh")
108 ("init-cwnd", po::value<int>(&initCwnd)->default_value(initCwnd),
109 "initial congestion window in segments")
110 ("init-ssthresh", po::value<int>(&initSsthresh),
111 "initial slow start threshold in segments (defaults to infinity)")
112 ("aimd-step", po::value<double>(&aiStep)->default_value(aiStep),
schneiderklausd8197df2019-03-16 11:31:40 -0700113 "additive-increase step")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700114 ("aimd-beta", po::value<double>(&aimdBeta)->default_value(aimdBeta),
115 "multiplicative decrease factor (AIMD)")
116 ("rto-alpha", po::value<double>(&rtoAlpha)->default_value(rtoAlpha),
schneiderklausd8197df2019-03-16 11:31:40 -0700117 "alpha value for rto calculation")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700118 ("rto-beta", po::value<double>(&rtoBeta)->default_value(rtoBeta),
schneiderklausd8197df2019-03-16 11:31:40 -0700119 "beta value for rto calculation")
120 ("rto-k", po::value<int>(&k)->default_value(k),
121 "k value for rto calculation")
122 ("min-rto", po::value<double>(&minRto)->default_value(minRto),
123 "minimum rto value in milliseconds")
124 ("max-rto", po::value<double>(&maxRto)->default_value(maxRto),
125 "maximum rto value in milliseconds")
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700126 ("log-cwnd", po::value<std::string>(&cwndPath), "log file for congestion window stats")
127 ("log-rtt", po::value<std::string>(&rttPath), "log file for round-trip time stats")
128 ;
129
130 po::options_description cubicPipeDesc("CUBIC pipeline options");
131 cubicPipeDesc.add_options()
132 ("fast-conv", po::bool_switch(&enableFastConv), "enable cubic fast convergence")
133 ("cubic-beta", po::value<double>(&cubicBeta),
134 "window decrease factor for CUBIC (defaults to 0.7)")
Weiwei Liu245d7912016-07-28 00:04:25 -0700135 ;
136
137 po::options_description visibleDesc;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700138 visibleDesc.add(basicDesc)
139 .add(realDiscoveryDesc)
140 .add(fixedPipeDesc)
141 .add(adaptivePipeDesc)
142 .add(cubicPipeDesc);
Weiwei Liu245d7912016-07-28 00:04:25 -0700143
144 po::options_description hiddenDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100145 hiddenDesc.add_options()
146 ("ndn-name,n", po::value<std::string>(&uri), "NDN name of the requested content");
147
148 po::positional_options_description p;
149 p.add("ndn-name", -1);
150
Weiwei Liu245d7912016-07-28 00:04:25 -0700151 po::options_description optDesc;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100152 optDesc.add(visibleDesc).add(hiddenDesc);
153
154 po::variables_map vm;
155 try {
156 po::store(po::command_line_parser(argc, argv).options(optDesc).positional(p).run(), vm);
157 po::notify(vm);
158 }
159 catch (const po::error& e) {
160 std::cerr << "ERROR: " << e.what() << std::endl;
161 return 2;
162 }
163 catch (const boost::bad_any_cast& e) {
164 std::cerr << "ERROR: " << e.what() << std::endl;
165 return 2;
166 }
167
168 if (vm.count("help") > 0) {
169 std::cout << "Usage: " << programName << " [options] ndn:/name" << std::endl;
170 std::cout << visibleDesc;
171 return 0;
172 }
173
174 if (vm.count("version") > 0) {
175 std::cout << "ndncatchunks " << tools::VERSION << std::endl;
176 return 0;
177 }
178
179 if (vm.count("ndn-name") == 0) {
180 std::cerr << "Usage: " << programName << " [options] ndn:/name" << std::endl;
181 std::cerr << visibleDesc;
182 return 2;
183 }
184
185 Name prefix(uri);
186 if (discoverType == "fixed" && (prefix.empty() || !prefix[-1].isVersion())) {
187 std::cerr << "ERROR: The specified name must contain a version component when using "
188 "fixed version discovery" << std::endl;
189 return 2;
190 }
191
192 if (maxPipelineSize < 1 || maxPipelineSize > 1024) {
193 std::cerr << "ERROR: pipeline size must be between 1 and 1024" << std::endl;
194 return 2;
195 }
196
197 if (options.maxRetriesOnTimeoutOrNack < -1 || options.maxRetriesOnTimeoutOrNack > 1024) {
198 std::cerr << "ERROR: retries value must be between -1 and 1024" << std::endl;
199 return 2;
200 }
201
Klaus Schneider7072e162017-09-16 13:43:00 -0700202 if (discoveryTimeoutMs < 0) {
203 std::cerr << "ERROR: timeout cannot be negative" << std::endl;
204 return 2;
205 }
206
207 if (vm["lifetime"].as<int64_t>() < 0) {
208 std::cerr << "ERROR: lifetime cannot be negative" << std::endl;
209 return 2;
210 }
Klaus Schneider7072e162017-09-16 13:43:00 -0700211 options.interestLifetime = time::milliseconds(vm["lifetime"].as<int64_t>());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100212
Davide Pesaventof6991e12018-01-08 20:58:50 -0500213 if (options.isQuiet && options.isVerbose) {
214 std::cerr << "ERROR: cannot be quiet and verbose at the same time" << std::endl;
215 return 2;
216 }
217
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100218 try {
219 Face face;
220
221 unique_ptr<DiscoverVersion> discover;
222 if (discoverType == "fixed") {
223 discover = make_unique<DiscoverVersionFixed>(prefix, face, options);
224 }
Chavoosh Ghasemid8f9af22019-02-28 09:47:26 -0800225 else if (discoverType == "realtime") {
226 DiscoverVersionRealtime::Options optionsRealtime(options);
Chavoosh Ghasemi0c6fcb52019-03-04 10:29:25 -0800227 optionsRealtime.discoveryTimeout = time::milliseconds(discoveryTimeoutMs);
Chavoosh Ghasemid8f9af22019-02-28 09:47:26 -0800228 discover = make_unique<DiscoverVersionRealtime>(prefix, face, optionsRealtime);
229 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100230 else {
231 std::cerr << "ERROR: discover version type not valid" << std::endl;
232 return 2;
233 }
234
Weiwei Liue4765012016-06-01 00:10:29 -0700235 unique_ptr<PipelineInterests> pipeline;
schneiderklausd8197df2019-03-16 11:31:40 -0700236 unique_ptr<StatisticsCollector> statsCollector;
237 unique_ptr<RttEstimator> 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") {
schneiderklausd8197df2019-03-16 11:31:40 -0700242 PipelineInterestsFixed::Options optionsPipeline(options);
Weiwei Liue4765012016-06-01 00:10:29 -0700243 optionsPipeline.maxPipelineSize = maxPipelineSize;
schneiderklausd8197df2019-03-16 11:31:40 -0700244 pipeline = make_unique<PipelineInterestsFixed>(face, optionsPipeline);
Weiwei Liue4765012016-06-01 00:10:29 -0700245 }
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700246 else if (pipelineType == "aimd" || pipelineType == "cubic") {
schneiderklausd8197df2019-03-16 11:31:40 -0700247 RttEstimator::Options optionsRttEst;
Weiwei Liu245d7912016-07-28 00:04:25 -0700248 optionsRttEst.isVerbose = options.isVerbose;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700249 optionsRttEst.alpha = rtoAlpha;
250 optionsRttEst.beta = rtoBeta;
Weiwei Liu245d7912016-07-28 00:04:25 -0700251 optionsRttEst.k = k;
schneiderklausd8197df2019-03-16 11:31:40 -0700252 optionsRttEst.minRto = Milliseconds(minRto);
253 optionsRttEst.maxRto = Milliseconds(maxRto);
Weiwei Liu245d7912016-07-28 00:04:25 -0700254
schneiderklausd8197df2019-03-16 11:31:40 -0700255 rttEstimator = make_unique<RttEstimator>(optionsRttEst);
Weiwei Liu245d7912016-07-28 00:04:25 -0700256
schneiderklausd8197df2019-03-16 11:31:40 -0700257 PipelineInterestsAdaptive::Options optionsPipeline(options);
Weiwei Liu245d7912016-07-28 00:04:25 -0700258 optionsPipeline.disableCwa = disableCwa;
259 optionsPipeline.resetCwndToInit = resetCwndToInit;
260 optionsPipeline.initCwnd = static_cast<double>(initCwnd);
261 optionsPipeline.initSsthresh = static_cast<double>(initSsthresh);
262 optionsPipeline.aiStep = aiStep;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700263 optionsPipeline.mdCoef = aimdBeta;
Chavoosh Ghasemi641f5932017-11-06 22:45:11 +0000264 optionsPipeline.ignoreCongMarks = ignoreCongMarks;
Weiwei Liu245d7912016-07-28 00:04:25 -0700265
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700266 unique_ptr<PipelineInterestsAdaptive> adaptivePipeline;
267 if (pipelineType == "aimd") {
268 adaptivePipeline = make_unique<PipelineInterestsAimd>(face, *rttEstimator, optionsPipeline);
269 }
270 else {
271 PipelineInterestsCubic::Options optionsCubic(optionsPipeline);
272 optionsCubic.enableFastConv = enableFastConv;
273 optionsCubic.cubicBeta = cubicBeta;
274 adaptivePipeline = make_unique<PipelineInterestsCubic>(face, *rttEstimator, optionsCubic);
275 }
Weiwei Liu245d7912016-07-28 00:04:25 -0700276
277 if (!cwndPath.empty() || !rttPath.empty()) {
278 if (!cwndPath.empty()) {
279 statsFileCwnd.open(cwndPath);
280 if (statsFileCwnd.fail()) {
281 std::cerr << "ERROR: failed to open " << cwndPath << std::endl;
282 return 4;
283 }
284 }
285 if (!rttPath.empty()) {
286 statsFileRtt.open(rttPath);
287 if (statsFileRtt.fail()) {
288 std::cerr << "ERROR: failed to open " << rttPath << std::endl;
289 return 4;
290 }
291 }
schneiderklausd8197df2019-03-16 11:31:40 -0700292 statsCollector = make_unique<StatisticsCollector>(*adaptivePipeline, *rttEstimator,
293 statsFileCwnd, statsFileRtt);
Weiwei Liu245d7912016-07-28 00:04:25 -0700294 }
295
schneiderklausd8197df2019-03-16 11:31:40 -0700296 pipeline = std::move(adaptivePipeline);
Weiwei Liu245d7912016-07-28 00:04:25 -0700297 }
Weiwei Liue4765012016-06-01 00:10:29 -0700298 else {
299 std::cerr << "ERROR: Interest pipeline type not valid" << std::endl;
300 return 2;
301 }
302
Davide Pesaventof6991e12018-01-08 20:58:50 -0500303 Consumer consumer(security::v2::getAcceptAllValidator());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100304 BOOST_ASSERT(discover != nullptr);
Weiwei Liue4765012016-06-01 00:10:29 -0700305 BOOST_ASSERT(pipeline != nullptr);
306 consumer.run(std::move(discover), std::move(pipeline));
Weiwei Liu05d92092016-07-19 17:34:33 -0700307 face.processEvents();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100308 }
309 catch (const Consumer::ApplicationNackError& e) {
310 std::cerr << "ERROR: " << e.what() << std::endl;
311 return 3;
312 }
Davide Pesaventof6991e12018-01-08 20:58:50 -0500313 catch (const Consumer::DataValidationError& e) {
314 std::cerr << "ERROR: " << e.what() << std::endl;
315 return 5;
316 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100317 catch (const std::exception& e) {
318 std::cerr << "ERROR: " << e.what() << std::endl;
319 return 1;
320 }
321
322 return 0;
323}
324
325} // namespace chunks
326} // namespace ndn
327
328int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400329main(int argc, char* argv[])
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100330{
331 return ndn::chunks::main(argc, argv);
332}