command_options.cc 29 KB

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