command_options.cc 33 KB

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