command_options.cc 36 KB

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