command_options.cc 35 KB

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