command_options.cc 36 KB

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