command_options.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <unistd.h>
  18. #include <boost/algorithm/string.hpp>
  19. #include <boost/foreach.hpp>
  20. #include <boost/lexical_cast.hpp>
  21. #include <exceptions/exceptions.h>
  22. #include <dhcp/iface_mgr.h>
  23. #include "command_options.h"
  24. using namespace std;
  25. using namespace isc;
  26. namespace isc {
  27. namespace perfdhcp {
  28. CommandOptions&
  29. CommandOptions::instance() {
  30. static CommandOptions options;
  31. return (options);
  32. }
  33. void
  34. CommandOptions::reset() {
  35. // Default mac address used in DHCP messages
  36. // if -b mac=<mac-address> was not specified
  37. uint8_t mac[6] = { 0x0, 0xC, 0x1, 0x2, 0x3, 0x4 };
  38. // Default packet drop time if -D<drop-time> parameter
  39. // was not specified
  40. double dt[2] = { 1., 1. };
  41. // We don't use constructor initialization list because we
  42. // will need to reset all members many times to perform unit tests
  43. ipversion_ = 0;
  44. exchange_mode_ = DORA_SARR;
  45. rate_ = 0;
  46. report_delay_ = 0;
  47. clients_num_ = 0;
  48. mac_prefix_.assign(mac, mac + 6);
  49. base_.resize(0);
  50. num_request_.resize(0);
  51. period_ = 0;
  52. drop_time_set_ = 0;
  53. drop_time_.assign(dt, dt + 2);
  54. max_drop_.clear();
  55. max_pdrop_.clear();
  56. localname_.clear();
  57. is_interface_ = false;
  58. preload_ = 0;
  59. aggressivity_ = 1;
  60. local_port_ = 0;
  61. seeded_ = false;
  62. seed_ = 0;
  63. broadcast_ = false;
  64. rapid_commit_ = false;
  65. use_first_ = false;
  66. template_file_.clear();
  67. rnd_offset_.clear();
  68. xid_offset_.clear();
  69. elp_offset_ = -1;
  70. sid_offset_ = -1;
  71. rip_offset_ = -1;
  72. diags_.clear();
  73. wrapped_.clear();
  74. server_name_.clear();
  75. }
  76. void
  77. CommandOptions::parse(int argc, char** const argv) {
  78. // Reset internal variables used by getopt
  79. // to eliminate undefined behavior when
  80. // parsing different command lines multiple times
  81. #ifdef __GLIBC__
  82. // Warning: non-portable code. This is due to a bug in glibc's
  83. // getopt() which keeps internal state about an old argument vector
  84. // (argc, argv) from last call and tries to scan them when a new
  85. // argument vector (argc, argv) is passed. As the old vector may not
  86. // be main()'s arguments, but heap allocated and may have been freed
  87. // since, this becomes a use after free and results in random
  88. // behavior. According to the NOTES section in glibc getopt()'s
  89. // manpage, setting optind=0 resets getopt()'s state. Though this is
  90. // not required in our usage of getopt(), the bug still happens
  91. // unless we set optind=0.
  92. //
  93. // Setting optind=0 is non-portable code.
  94. optind = 0;
  95. #else
  96. optind = 1;
  97. #endif
  98. opterr = 0;
  99. // Reset values of class members
  100. reset();
  101. initialize(argc, argv);
  102. validate();
  103. }
  104. void
  105. CommandOptions::initialize(int argc, char** argv) {
  106. char opt = 0; // Subsequent options returned by getopt()
  107. std::string drop_arg; // Value of -D<value>argument
  108. size_t percent_loc = 0; // Location of % sign in -D<value>
  109. double drop_percent = 0; // % value (1..100) in -D<value%>
  110. int num_drops = 0; // Max number of drops specified in -D<value>
  111. int num_req = 0; // Max number of dropped requests in -n<max-drops>
  112. int offset_arg = 0; // Temporary variable holding offset arguments
  113. std::string sarg; // Temporary variable for string args
  114. // In this section we collect argument values from command line
  115. // they will be tuned and validated elsewhere
  116. while((opt = getopt(argc, argv, "hv46r:t:R:b:n:p:d:D:l:P:a:L:s:iBc1T:X:O:E:S:I:x:w:")) != -1) {
  117. switch (opt) {
  118. case 'v':
  119. version();
  120. return;
  121. case '1':
  122. use_first_ = true;
  123. break;
  124. case '4':
  125. check(ipversion_ == 6, "IP version already set to 6");
  126. ipversion_ = 4;
  127. break;
  128. case '6':
  129. check(ipversion_ == 4, "IP version already set to 4");
  130. ipversion_ = 6;
  131. break;
  132. case 'a':
  133. aggressivity_ = positiveInteger("value of aggressivity: -a<value> must be a positive integer");
  134. break;
  135. case 'b':
  136. check(base_.size() > 3, "-b<value> already specified, unexpected occurence of 5th -b<value>");
  137. base_.push_back(optarg);
  138. decodeBase(base_.back());
  139. break;
  140. case 'B':
  141. broadcast_ = true;
  142. break;
  143. case 'c':
  144. rapid_commit_ = true;
  145. break;
  146. case 'd':
  147. check(drop_time_set_ > 1, "maximum number of drops already specified, "
  148. "unexpected 3rd occurence of -d<value>");
  149. try {
  150. drop_time_[drop_time_set_] = boost::lexical_cast<double>(optarg);
  151. } catch (boost::bad_lexical_cast&) {
  152. isc_throw(isc::InvalidParameter,
  153. "value of drop time: -d<value> must be positive number");
  154. }
  155. check(drop_time_[drop_time_set_] <= 0., "drop-time must be a positive number");
  156. drop_time_set_ = true;
  157. break;
  158. case 'D':
  159. drop_arg = std::string(optarg);
  160. percent_loc = drop_arg.find('%');
  161. check(max_pdrop_.size() > 1 || max_drop_.size() > 1, "values of maximum drops: -D<value> already "
  162. "specified, unexpected 3rd occurence of -D,value>");
  163. if ((percent_loc) != std::string::npos) {
  164. try {
  165. drop_percent = boost::lexical_cast<double>(drop_arg.substr(0, percent_loc));
  166. } catch (boost::bad_lexical_cast&) {
  167. isc_throw(isc::InvalidParameter,
  168. "value of drop percentage: -D<value%> must be 0..100");
  169. }
  170. check((drop_percent <= 0) || (drop_percent >= 100),
  171. "value of drop percentage: -D<value%> must be 0..100");
  172. max_pdrop_.push_back(drop_percent);
  173. } else {
  174. num_drops = positiveInteger("value of max drops number: -d<value> must be a positive integer");
  175. max_drop_.push_back(num_drops);
  176. }
  177. break;
  178. case 'E':
  179. elp_offset_ = nonNegativeInteger("value of time-offset: -E<value> must not be a negative integer");
  180. break;
  181. case 'h':
  182. usage();
  183. return;
  184. case 'i':
  185. exchange_mode_ = DO_SA;
  186. break;
  187. case 'I':
  188. rip_offset_ = positiveInteger("value of ip address offset: -I<value> must be a positive integer");
  189. break;
  190. case 'l':
  191. localname_ = std::string(optarg);
  192. initIsInterface();
  193. break;
  194. case 'L':
  195. local_port_ = nonNegativeInteger("value of local port: -L<value> must not be a negative integer");
  196. check(local_port_ > static_cast<int>(std::numeric_limits<uint16_t>::max()),
  197. "local-port must be lower than " +
  198. boost::lexical_cast<std::string>(std::numeric_limits<uint16_t>::max()));
  199. break;
  200. case 'n':
  201. num_req = positiveInteger("value of num-request: -n<value> must be a positive integer");
  202. if (num_request_.size() >= 2) {
  203. isc_throw(isc::InvalidParameter,"value of maximum number of requests: -n<value> "
  204. "already specified, unexpected 3rd occurence of -n<value>");
  205. }
  206. num_request_.push_back(num_req);
  207. break;
  208. case 'O':
  209. if (rnd_offset_.size() < 2) {
  210. offset_arg = positiveInteger("value of random offset: -O<value> must be greater than 3");
  211. } else {
  212. isc_throw(isc::InvalidParameter,
  213. "random offsets already specified, unexpected 3rd occurence of -O<value>");
  214. }
  215. check(offset_arg < 3, "value of random random-offset: -O<value> must be greater than 3 ");
  216. rnd_offset_.push_back(offset_arg);
  217. break;
  218. case 'p':
  219. period_ = positiveInteger("value of test period: -p<value> must be a positive integer");
  220. break;
  221. case 'P':
  222. preload_ = nonNegativeInteger("number of preload packets: -P<value> must not be "
  223. "a negative integer");
  224. break;
  225. case 'r':
  226. rate_ = positiveInteger("value of rate: -r<value> must be a positive integer");
  227. break;
  228. case 'R':
  229. initClientsNum();
  230. break;
  231. case 's':
  232. seed_ = static_cast<unsigned int>
  233. (nonNegativeInteger("value of seed: -s <seed> must be non-negative integer"));
  234. seeded_ = seed_ > 0 ? true : false;
  235. break;
  236. case 'S':
  237. sid_offset_ = positiveInteger("value of server id offset: -S<value> must be a positive integer");
  238. break;
  239. case 't':
  240. report_delay_ = positiveInteger("value of report delay: -t<value> must be a positive integer");
  241. break;
  242. case 'T':
  243. if (template_file_.size() < 2) {
  244. sarg = nonEmptyString("template file name not specified, expected -T<filename>");
  245. template_file_.push_back(sarg);
  246. } else {
  247. isc_throw(isc::InvalidParameter,
  248. "template files are already specified, unexpected 3rd -T<filename> occurence");
  249. }
  250. break;
  251. case 'w':
  252. wrapped_ = nonEmptyString("command for wrapped mode: -w<command> must be specified");
  253. break;
  254. case 'x':
  255. diags_ = nonEmptyString("value of diagnostics selectors: -x<value> must be specified");
  256. break;
  257. case 'X':
  258. if (xid_offset_.size() < 2) {
  259. offset_arg = positiveInteger("value of transaction id: -X<value> must be a positive integer");
  260. } else {
  261. isc_throw(isc::InvalidParameter,
  262. "transaction ids already specified, unexpected 3rd -X<value> occurence");
  263. }
  264. xid_offset_.push_back(offset_arg);
  265. break;
  266. default:
  267. isc_throw(isc::InvalidParameter, "unknown command line option");
  268. }
  269. }
  270. // If the IP version was not specified in the
  271. // command line, assume IPv4.
  272. if (ipversion_ == 0) {
  273. ipversion_ = 4;
  274. }
  275. // If template packet files specified for both DISCOVER/SOLICIT
  276. // and REQUEST/REPLY exchanges make sure we have transaction id
  277. // and random duid offsets for both exchanges. We will duplicate
  278. // value specified as -X<value> and -R<value> for second
  279. // exchange if user did not specified otherwise.
  280. if (template_file_.size() > 1) {
  281. if (xid_offset_.size() == 1) {
  282. xid_offset_.push_back(xid_offset_[0]);
  283. }
  284. if (rnd_offset_.size() == 1) {
  285. rnd_offset_.push_back(rnd_offset_[0]);
  286. }
  287. }
  288. // Get server argument
  289. // NoteFF02::1:2 and FF02::1:3 are defined in RFC3315 as
  290. // All_DHCP_Relay_Agents_and_Servers and All_DHCP_Servers
  291. // addresses
  292. check(optind < argc -1, "extra arguments?");
  293. if (optind == argc - 1) {
  294. server_name_ = argv[optind];
  295. // Decode special cases
  296. if ((ipversion_ == 4) && (server_name_.compare("all") == 0)) {
  297. broadcast_ = 1;
  298. // 255.255.255.255 is IPv4 broadcast address
  299. server_name_ = "255.255.255.255";
  300. } else if ((ipversion_ == 6) && (server_name_.compare("all") == 0)) {
  301. server_name_ = "FF02::1:2";
  302. } else if ((ipversion_ == 6) && (server_name_.compare("servers") == 0)) {
  303. server_name_ = "FF05::1:3";
  304. }
  305. }
  306. // Handle the local '-l' address/interface
  307. if (!localname_.empty()) {
  308. if (server_name_.empty()) {
  309. if (is_interface_ && (ipversion_ == 4)) {
  310. broadcast_ = 1;
  311. server_name_ = "255.255.255.255";
  312. } else if (is_interface_ && (ipversion_ == 6)) {
  313. server_name_ = "FF02::1:2";
  314. }
  315. }
  316. }
  317. if (server_name_.empty()) {
  318. isc_throw(InvalidParameter,
  319. "without an inteface server is required");
  320. }
  321. }
  322. void
  323. CommandOptions::initClientsNum() {
  324. const std::string errmsg = "value of -R <value> must be non-negative integer";
  325. // Declare clients_num as as 64-bit signed value to
  326. // be able to detect negative values provided
  327. // by user. We would not detect negative values
  328. // if we casted directly to unsigned value.
  329. long long clients_num = 0;
  330. try {
  331. clients_num = boost::lexical_cast<long long>(optarg);
  332. } catch (boost::bad_lexical_cast&) {
  333. isc_throw(isc::InvalidParameter, errmsg.c_str());
  334. }
  335. check(clients_num < 0, errmsg);
  336. try {
  337. clients_num_ = boost::lexical_cast<uint32_t>(optarg);
  338. } catch (boost::bad_lexical_cast&) {
  339. isc_throw(isc::InvalidParameter, errmsg);
  340. }
  341. }
  342. void
  343. CommandOptions::initIsInterface() {
  344. is_interface_ = false;
  345. if (!localname_.empty()) {
  346. dhcp::IfaceMgr& iface_mgr = dhcp::IfaceMgr::instance();
  347. if (iface_mgr.getIface(localname_) != NULL) {
  348. is_interface_ = true;
  349. }
  350. }
  351. }
  352. void
  353. CommandOptions::decodeBase(const std::string& base) {
  354. std::string b(base);
  355. boost::algorithm::to_lower(b);
  356. // Currently we only support mac and duid
  357. if ((b.substr(0, 4) == "mac=") || (b.substr(0, 6) == "ether=")) {
  358. decodeMac(b);
  359. } else if (b.substr(0, 5) == "duid=") {
  360. decodeDuid(b);
  361. } else {
  362. isc_throw(isc::InvalidParameter,
  363. "base value not provided as -b<value>, expected -b mac=<mac> or -b duid=<duid>");
  364. }
  365. }
  366. void
  367. CommandOptions::decodeMac(const std::string& base) {
  368. // Strip string from mac=
  369. size_t found = base.find('=');
  370. static const char* errmsg = "expected -b<base> format for mac address is -b mac=00::0C::01::02::03::04";
  371. check(found == std::string::npos, errmsg);
  372. // Decode mac address to vector of uint8_t
  373. std::istringstream s1(base.substr(found + 1));
  374. std::string token;
  375. mac_prefix_.clear();
  376. // Get pieces of MAC address separated with : (or even ::)
  377. while (std::getline(s1, token, ':')) {
  378. unsigned int ui = 0;
  379. // Convert token to byte value using std::istringstream
  380. if (token.length() > 0) {
  381. try {
  382. // Do actual conversion
  383. ui = convertHexString(token);
  384. } catch (isc::InvalidParameter&) {
  385. isc_throw(isc::InvalidParameter,
  386. "invalid characters in MAC provided");
  387. }
  388. // If conversion succeeded store byte value
  389. mac_prefix_.push_back(ui);
  390. }
  391. }
  392. // MAC address must consist of 6 octets, otherwise it is invalid
  393. check(mac_prefix_.size() != 6, errmsg);
  394. }
  395. void
  396. CommandOptions::decodeDuid(const std::string& base) {
  397. // Strip argument from duid=
  398. size_t found = base.find('=');
  399. check(found == std::string::npos, "expected -b<base> format for duid is -b duid=<duid>");
  400. std::string b = base.substr(found + 1);
  401. // DUID must have even number of digits and must not be longer than 64 bytes
  402. check(b.length() & 1, "odd number of hexadecimal digits in duid");
  403. check(b.length() > 128, "duid too large");
  404. check(b.length() == 0, "no duid specified");
  405. // Turn pairs of hexadecimal digits into vector of octets
  406. for (int i = 0; i < b.length(); i += 2) {
  407. unsigned int ui = 0;
  408. try {
  409. // Do actual conversion
  410. ui = convertHexString(b.substr(i, 2));
  411. } catch (isc::InvalidParameter&) {
  412. isc_throw(isc::InvalidParameter,
  413. "invalid characters in DUID provided, exepected hex digits");
  414. }
  415. duid_prefix_.push_back(static_cast<uint8_t>(ui));
  416. }
  417. }
  418. uint8_t
  419. CommandOptions::convertHexString(const std::string& text) const {
  420. unsigned int ui = 0;
  421. // First, check if we are dealing with hexadecimal digits only
  422. for (int i = 0; i < text.length(); ++i) {
  423. if (!std::isxdigit(text[i])) {
  424. isc_throw(isc::InvalidParameter,
  425. "The following digit: " << text[i] << " in "
  426. << text << "is not hexadecimal");
  427. }
  428. }
  429. // If we are here, we have valid string to convert to octet
  430. std::istringstream text_stream(text);
  431. text_stream >> std::hex >> ui >> std::dec;
  432. // Check if for some reason we have overflow - this should never happen!
  433. if (ui > 0xFF) {
  434. isc_throw(isc::InvalidParameter, "Can't convert more than two hex digits to byte");
  435. }
  436. return ui;
  437. }
  438. void
  439. CommandOptions::validate() const {
  440. check((getIpVersion() != 4) && (isBroadcast() != 0),
  441. "-B is not compatible with IPv6 (-6)");
  442. check((getIpVersion() != 6) && (isRapidCommit() != 0),
  443. "-6 (IPv6) must be set to use -c");
  444. check((getExchangeMode() == DO_SA) && (getNumRequests().size() > 1),
  445. "second -n<num-request> is not compatible with -i");
  446. check((getExchangeMode() == DO_SA) && (getDropTime()[1] != 1.),
  447. "second -d<drop-time> is not compatible with -i");
  448. check((getExchangeMode() == DO_SA) &&
  449. ((getMaxDrop().size() > 1) || (getMaxDropPercentage().size() > 1)),
  450. "second -D<max-drop> is not compatible with -i\n");
  451. check((getExchangeMode() == DO_SA) && (isUseFirst()),
  452. "-1 is not compatible with -i\n");
  453. check((getExchangeMode() == DO_SA) && (getTemplateFiles().size() > 1),
  454. "second -T<template-file> is not compatible with -i\n");
  455. check((getExchangeMode() == DO_SA) && (getTransactionIdOffset().size() > 1),
  456. "second -X<xid-offset> is not compatible with -i\n");
  457. check((getExchangeMode() == DO_SA) && (getRandomOffset().size() > 1),
  458. "second -O<random-offset is not compatible with -i\n");
  459. check((getExchangeMode() == DO_SA) && (getElapsedTimeOffset() >= 0),
  460. "-E<time-offset> is not compatible with -i\n");
  461. check((getExchangeMode() == DO_SA) && (getServerIdOffset() >= 0),
  462. "-S<srvid-offset> is not compatible with -i\n");
  463. check((getExchangeMode() == DO_SA) && (getRequestedIpOffset() >= 0),
  464. "-I<ip-offset> is not compatible with -i\n");
  465. check((getExchangeMode() != DO_SA) && (isRapidCommit() != 0),
  466. "-i must be set to use -c\n");
  467. check((getRate() == 0) && (getReportDelay() != 0),
  468. "-r<rate> must be set to use -t<report>\n");
  469. check((getRate() == 0) && (getNumRequests().size() > 0),
  470. "-r<rate> must be set to use -n<num-request>\n");
  471. check((getRate() == 0) && (getPeriod() != 0),
  472. "-r<rate> must be set to use -p<test-period>\n");
  473. check((getRate() == 0) &&
  474. ((getMaxDrop().size() > 0) || getMaxDropPercentage().size() > 0),
  475. "-r<rate> must be set to use -D<max-drop>\n");
  476. check((getTemplateFiles().size() < getTransactionIdOffset().size()),
  477. "-T<template-file> must be set to use -X<xid-offset>\n");
  478. check((getTemplateFiles().size() < getRandomOffset().size()),
  479. "-T<template-file> must be set to use -O<random-offset>\n");
  480. check((getTemplateFiles().size() < 2) && (getElapsedTimeOffset() >= 0),
  481. "second/request -T<template-file> must be set to use -E<time-offset>\n");
  482. check((getTemplateFiles().size() < 2) && (getServerIdOffset() >= 0),
  483. "second/request -T<template-file> must be set to "
  484. "use -S<srvid-offset>\n");
  485. check((getTemplateFiles().size() < 2) && (getRequestedIpOffset() >= 0),
  486. "second/request -T<template-file> must be set to "
  487. "use -I<ip-offset>\n");
  488. }
  489. void
  490. CommandOptions::check(bool condition, const std::string& errmsg) const {
  491. // The same could have been done with macro or just if statement but
  492. // we prefer functions to macros here
  493. if (condition) {
  494. isc_throw(isc::InvalidParameter, errmsg);
  495. }
  496. }
  497. int
  498. CommandOptions::positiveInteger(const std::string& errmsg) const {
  499. try {
  500. int value = boost::lexical_cast<int>(optarg);
  501. check(value <= 0, errmsg);
  502. return (value);
  503. } catch (boost::bad_lexical_cast&) {
  504. isc_throw(InvalidParameter, errmsg);
  505. }
  506. }
  507. int
  508. CommandOptions::nonNegativeInteger(const std::string& errmsg) const {
  509. try {
  510. int value = boost::lexical_cast<int>(optarg);
  511. check(value < 0, errmsg);
  512. return (value);
  513. } catch (boost::bad_lexical_cast&) {
  514. isc_throw(InvalidParameter, errmsg);
  515. }
  516. }
  517. std::string
  518. CommandOptions::nonEmptyString(const std::string& errmsg) const {
  519. std::string sarg = optarg;
  520. if (sarg.length() == 0) {
  521. isc_throw(isc::InvalidParameter, errmsg);
  522. }
  523. return sarg;
  524. }
  525. void
  526. CommandOptions::usage() const {
  527. fprintf(stdout, "%s",
  528. "perfdhcp [-hv] [-4|-6] [-r<rate>] [-t<report>] [-R<range>] [-b<base>]\n"
  529. " [-n<num-request>] [-p<test-period>] [-d<drop-time>] [-D<max-drop>]\n"
  530. " [-l<local-addr|interface>] [-P<preload>] [-a<aggressivity>]\n"
  531. " [-L<local-port>] [-s<seed>] [-i] [-B] [-c] [-1]\n"
  532. " [-T<template-file>] [-X<xid-offset>] [-O<random-offset]\n"
  533. " [-E<time-offset>] [-S<srvid-offset>] [-I<ip-offset>]\n"
  534. " [-x<diagnostic-selector>] [-w<wrapped>] [server]\n"
  535. "\n"
  536. "The [server] argument is the name/address of the DHCP server to\n"
  537. "contact. For DHCPv4 operation, exchanges are initiated by\n"
  538. "transmitting a DHCP DISCOVER to this address.\n"
  539. "\n"
  540. "For DHCPv6 operation, exchanges are initiated by transmitting a DHCP\n"
  541. "SOLICIT to this address. In the DHCPv6 case, the special name 'all'\n"
  542. "can be used to refer to All_DHCP_Relay_Agents_and_Servers (the\n"
  543. "multicast address FF02::1:2), or the special name 'servers' to refer\n"
  544. "to All_DHCP_Servers (the multicast address FF05::1:3). The [server]\n"
  545. "argument is optional only in the case that -l is used to specify an\n"
  546. "interface, in which case [server] defaults to 'all'.\n"
  547. "\n"
  548. "The default is to perform a single 4-way exchange, effectively pinging\n"
  549. "the server.\n"
  550. "The -r option is used to set up a performance test, without\n"
  551. "it exchanges are initiated as fast as possible.\n"
  552. "\n"
  553. "Options:\n"
  554. "-1: Take the server-ID option from the first received message.\n"
  555. "-4: DHCPv4 operation (default). This is incompatible with the -6 option.\n"
  556. "-6: DHCPv6 operation. This is incompatible with the -4 option.\n"
  557. "-a<aggressivity>: When the target sending rate is not yet reached,\n"
  558. " control how many exchanges are initiated before the next pause.\n"
  559. "-b<base>: The base mac, duid, IP, etc, used to simulate different\n"
  560. " clients. This can be specified multiple times, each instance is\n"
  561. " in the <type>=<value> form, for instance:\n"
  562. " (and default) mac=00:0c:01:02:03:04.\n"
  563. "-d<drop-time>: Specify the time after which a request is treated as\n"
  564. " having been lost. The value is given in seconds and may contain a\n"
  565. " fractional component. The default is 1 second.\n"
  566. "-E<time-offset>: Offset of the (DHCPv4) secs field / (DHCPv6)\n"
  567. " elapsed-time option in the (second/request) template.\n"
  568. " The value 0 disables it.\n"
  569. "-h: Print this help.\n"
  570. "-i: Do only the initial part of an exchange: DO or SA, depending on\n"
  571. " whether -6 is given.\n"
  572. "-I<ip-offset>: Offset of the (DHCPv4) IP address in the requested-IP\n"
  573. " option / (DHCPv6) IA_NA option in the (second/request) template.\n"
  574. "-l<local-addr|interface>: For DHCPv4 operation, specify the local\n"
  575. " hostname/address to use when communicating with the server. By\n"
  576. " default, the interface address through which traffic would\n"
  577. " normally be routed to the server is used.\n"
  578. " For DHCPv6 operation, specify the name of the network interface\n"
  579. " via which exchanges are initiated.\n"
  580. "-L<local-port>: Specify the local port to use\n"
  581. " (the value 0 means to use the default).\n"
  582. "-O<random-offset>: Offset of the last octet to randomize in the template.\n"
  583. "-P<preload>: Initiate first <preload> exchanges back to back at startup.\n"
  584. "-r<rate>: Initiate <rate> DORA/SARR (or if -i is given, DO/SA)\n"
  585. " exchanges per second. A periodic report is generated showing the\n"
  586. " number of exchanges which were not completed, as well as the\n"
  587. " average response latency. The program continues until\n"
  588. " interrupted, at which point a final report is generated.\n"
  589. "-R<range>: Specify how many different clients are used. With 1\n"
  590. " (the default), all requests seem to come from the same client.\n"
  591. "-s<seed>: Specify the seed for randomization, making it repeatable.\n"
  592. "-S<srvid-offset>: Offset of the server-ID option in the\n"
  593. " (second/request) template.\n"
  594. "-T<template-file>: The name of a file containing the template to use\n"
  595. " as a stream of hexadecimal digits.\n"
  596. "-v: Report the version number of this program.\n"
  597. "-w<wrapped>: Command to call with start/stop at the beginning/end of\n"
  598. " the program.\n"
  599. "-x<diagnostic-selector>: Include extended diagnostics in the output.\n"
  600. " <diagnostic-selector> is a string of single-keywords specifying\n"
  601. " the operations for which verbose output is desired. The selector\n"
  602. " keyletters are:\n"
  603. " * 'a': print the decoded command line arguments\n"
  604. " * 'e': print the exit reason\n"
  605. " * 'i': print rate processing details\n"
  606. " * 'r': print randomization details\n"
  607. " * 's': print first server-id\n"
  608. " * 't': when finished, print timers of all successful exchanges\n"
  609. " * 'T': when finished, print templates\n"
  610. "-X<xid-offset>: Transaction ID (aka. xid) offset in the template.\n"
  611. "\n"
  612. "DHCPv4 only options:\n"
  613. "-B: Force broadcast handling.\n"
  614. "\n"
  615. "DHCPv6 only options:\n"
  616. "-c: Add a rapid commit option (exchanges will be SA).\n"
  617. "\n"
  618. "The remaining options are used only in conjunction with -r:\n"
  619. "\n"
  620. "-D<max-drop>: Abort the test if more than <max-drop> requests have\n"
  621. " been dropped. Use -D0 to abort if even a single request has been\n"
  622. " dropped. If <max-drop> includes the suffix '%', it specifies a\n"
  623. " maximum percentage of requests that may be dropped before abort.\n"
  624. " In this case, testing of the threshold begins after 10 requests\n"
  625. " have been expected to be received.\n"
  626. "-n<num-request>: Initiate <num-request> transactions. No report is\n"
  627. " generated until all transactions have been initiated/waited-for,\n"
  628. " after which a report is generated and the program terminates.\n"
  629. "-p<test-period>: Send requests for the given test period, which is\n"
  630. " specified in the same manner as -d. This can be used as an\n"
  631. " alternative to -n, or both options can be given, in which case the\n"
  632. " testing is completed when either limit is reached.\n"
  633. "-t<report>: Delay in seconds between two periodic reports.\n"
  634. "\n"
  635. "Errors:\n"
  636. "- tooshort: received a too short message\n"
  637. "- orphans: received a message which doesn't match an exchange\n"
  638. " (duplicate, late or not related)\n"
  639. "- locallimit: reached to local system limits when sending a message.\n"
  640. "\n"
  641. "Exit status:\n"
  642. "The exit status is:\n"
  643. "0 on complete success.\n"
  644. "1 for a general error.\n"
  645. "2 if an error is found in the command line arguments.\n"
  646. "3 if there are no general failures in operation, but one or more\n"
  647. " exchanges are not successfully completed.\n");
  648. }
  649. void
  650. CommandOptions::version() const {
  651. fprintf(stdout, "version 0.01\n");
  652. }
  653. } // namespace perfdhcp
  654. } // namespace isc