command_options.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // Copyright (C) 2011 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. #define __STDC_LIMIT_MACROS
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <boost/algorithm/string.hpp>
  21. #include <boost/tokenizer.hpp>
  22. #include <boost/foreach.hpp>
  23. #include <boost/lexical_cast.hpp>
  24. #include "exceptions/exceptions.h"
  25. #include "command_options.h"
  26. using namespace std;
  27. using namespace isc;
  28. namespace isc {
  29. namespace perfdhcp {
  30. // Reset stored values to the defaults.
  31. void
  32. CommandOptions::reset() {
  33. uint8_t mac[6] = { 0x0, 0xC, 0x1, 0x2, 0x3, 0x4 };
  34. double lt[2] = { 1., 1. };
  35. ipversion_ = 0;
  36. exchange_mode_ = DORR_SARR;
  37. rate_ = 0;
  38. report_delay_ = 0;
  39. random_range_ = 0;
  40. max_random_ = 0;
  41. mac_prefix_.assign(mac, mac + 6);
  42. base_.resize(0);
  43. num_request_.resize(0);
  44. period_ = 0;
  45. lost_time_set_ = 0;
  46. lost_time_.assign(lt, lt + 2);
  47. max_drop_set_ = 0;
  48. max_drop_.resize(0);
  49. max_pdrop_.resize(0);
  50. localname_.resize(0);
  51. is_interface_ = false;
  52. preload_ = 0;
  53. aggressivity_ = 1;
  54. local_port_ = 0;
  55. seeded_ = false;
  56. seed_ = 0;
  57. broadcast_ = false;
  58. rapid_commit_ = false;
  59. use_first_ = false;
  60. template_file_.resize(0);
  61. rnd_offset_.resize(0);
  62. xid_offset_.resize(0);
  63. elp_offset_ = -1;
  64. sid_offset_ = -1;
  65. rip_offset_ = -1;
  66. diags_.resize(0);
  67. wrapped_.resize(0);
  68. server_name_.resize(0);
  69. }
  70. void
  71. CommandOptions::parse(int argc, char** const argv, bool force_reset /*=false */) {
  72. if (force_reset) {
  73. reset();
  74. }
  75. // Reset internal variables used by getopt
  76. // to eliminate underfined behavior when
  77. // parsing different command lines multiple times
  78. optind = 1;
  79. opterr = 0;
  80. initialize(argc, argv);
  81. validate();
  82. }
  83. void
  84. CommandOptions::initialize(int argc, char** const argv) {
  85. char opt;
  86. char* pc;
  87. int nr, of;
  88. int di = 0;
  89. float dp = 0;
  90. long long r;
  91. 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) {
  92. switch (opt) {
  93. case 'h':
  94. usage();
  95. case 'v':
  96. // version();
  97. ;
  98. case '4':
  99. check(ipversion_ == 6, "IP version already set to 6");
  100. ipversion_ = 4;
  101. break;
  102. case '6':
  103. check(ipversion_ == 4, "IP version already set to 4");
  104. ipversion_ = 6;
  105. break;
  106. case 'r':
  107. rate_ = atoi(optarg);
  108. check(rate_ <= 0, "rate_ must be a positive integer");
  109. break;
  110. case 't':
  111. report_delay_ = atoi(optarg);
  112. check(report_delay_ <= 0, "report_delay_ must be a positive integer");
  113. break;
  114. case 'R':
  115. r = atoll(optarg);
  116. check(r < 0, "random_range_ must not be a negative integer");
  117. random_range_ = (uint32_t) r;
  118. if ((random_range_ != 0) && (random_range_ != UINT32_MAX)) {
  119. uint32_t s = random_range_ + 1;
  120. uint64_t b = UINT32_MAX + 1, m;
  121. m = (b / s) * s;
  122. if (m == b)
  123. max_random_ = 0;
  124. else
  125. max_random_ = (uint32_t) m;
  126. }
  127. break;
  128. case 'b':
  129. check(base_.size() > 3, "too many bases");
  130. base_.push_back(optarg);
  131. decodeBase(base_.back());
  132. break;
  133. case 'n':
  134. nr = atoi(optarg);
  135. check(nr <= 0, "num-request must be a positive integer");
  136. if (num_request_.size() >= 2) {
  137. isc_throw(isc::InvalidParameter,
  138. "too many num-request values");
  139. }
  140. num_request_.push_back(nr);
  141. break;
  142. case 'p':
  143. period_ = atoi(optarg);
  144. check(period_ <= 0, "test-period must be a positive integer");
  145. break;
  146. case 'd':
  147. lost_time_[lost_time_set_] = atof(optarg);
  148. check(lost_time_[lost_time_set_] <= 0., "drop-time must be a positive number");
  149. lost_time_set_ = 1;
  150. break;
  151. case 'D':
  152. pc = strchr(optarg, '%');
  153. if (pc != NULL) {
  154. *pc = '\0';
  155. dp = atof(optarg);
  156. max_pdrop_[max_drop_set_] = atof(optarg);
  157. check((dp <= 0) || (dp >= 100), "invalid drop-time percentage");
  158. max_pdrop_.push_back(dp);
  159. break;
  160. }
  161. di = atoi(optarg);
  162. check(di <= 0, "max-drop must be a positive integer");
  163. max_drop_.push_back(di);
  164. break;
  165. case 'l':
  166. localname_ = optarg;
  167. break;
  168. case 'P':
  169. preload_ = atoi(optarg);
  170. check(preload_ < 0, "preload must not be a negative integer");
  171. break;
  172. case 'a':
  173. aggressivity_ = atoi(optarg);
  174. check(aggressivity_ <= 0, "aggressivity must be a positive integer");
  175. break;
  176. case 'L':
  177. local_port_ = atoi(optarg);
  178. check(local_port_ < 0, "local-port must not be a negative integer");
  179. check(local_port_ > (int) UINT16_MAX, "local-port must be lower than UINT16_MAX");
  180. break;
  181. case 's':
  182. seeded_ = true;
  183. seed_ = (unsigned int) atol(optarg);
  184. break;
  185. case 'i':
  186. exchange_mode_ = DO_SA;
  187. break;
  188. case 'B':
  189. broadcast_ = 1;
  190. break;
  191. case 'c':
  192. rapid_commit_ = 1;
  193. break;
  194. case '1':
  195. use_first_ = 1;
  196. break;
  197. case 'T':
  198. switch (template_file_.size()) {
  199. case 0:
  200. case 1:
  201. template_file_.push_back(std::string(optarg));
  202. break;
  203. default:
  204. isc_throw(isc::InvalidParameter,
  205. "template-files are already set");
  206. }
  207. break;
  208. case 'X':
  209. of = atoi(optarg);
  210. check(of <= 0, "xid-offset must be a positive integer");
  211. if (xid_offset_.size() >= 2) {
  212. xid_offset_.resize(0);
  213. }
  214. xid_offset_.push_back(of);
  215. break;
  216. case 'O':
  217. of = atoi(optarg);
  218. check(of < 3, "random-offset must be greater than 3");
  219. if (rnd_offset_.size() >= 2) {
  220. rnd_offset_.resize(0);
  221. }
  222. rnd_offset_.push_back(of);
  223. break;
  224. case 'E':
  225. elp_offset_ = atoi(optarg);
  226. check(elp_offset_ < 0, "time-offset must not be a negative integer");
  227. break;
  228. case 'S':
  229. sid_offset_ = atoi(optarg);
  230. check(sid_offset_ <= 0, "srvid-offset must be a positive integer");
  231. break;
  232. case 'I':
  233. rip_offset_ = atoi(optarg);
  234. check(rip_offset_ <= 0, "ip-offset must be a positive integer");
  235. break;
  236. case 'x':
  237. diags_.assign(optarg);
  238. break;
  239. case 'w':
  240. wrapped_.assign(optarg);
  241. break;
  242. default:
  243. isc_throw(isc::InvalidParameter,
  244. "unknown command line option");
  245. }
  246. }
  247. if (ipversion_ == 0)
  248. ipversion_ = 4;
  249. if (template_file_.size() > 1) {
  250. if (xid_offset_.size() == 1)
  251. xid_offset_.push_back(xid_offset_[0]);
  252. if (rnd_offset_.size() == 1)
  253. rnd_offset_.push_back(rnd_offset_[0]);
  254. }
  255. // TODO HADNLE SERVER ARG
  256. }
  257. void
  258. CommandOptions::decodeBase(const std::string& base) {
  259. std::string b(base);
  260. boost::algorithm::to_lower(b);
  261. if ((b.substr(0, 4) == "mac=") || (b.substr(0, 6) == "ether=")) {
  262. decodeMac(b);
  263. } else if (b.substr(0, 5) == "duid=") {
  264. decodeDuid(b);
  265. }
  266. }
  267. void
  268. CommandOptions::decodeMac(const std::string& base) {
  269. typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
  270. size_t found = base.find('=');
  271. check(found == std::string::npos, "expected -b<base> format for MAC address is -b MAC=00::0C::01::02::03::04");
  272. boost::char_separator<char> sep(":-");
  273. tokenizer tokens(base.substr(found + 1), sep);
  274. std::vector<std::string> stokens(tokens.begin(), tokens.end());
  275. check(stokens.size() != 6, "expected -b<base> format for MAC address is -b MAC=00::0C::01::02::03::04");
  276. mac_prefix_.resize(0);
  277. BOOST_FOREACH(std::string t, stokens) {
  278. std::istringstream ss(t);
  279. unsigned int ui = 0;
  280. ss >> std::hex >> ui >> std::dec;
  281. check(ss.fail() || (ui > 0xFF),
  282. "expected -b<base> format for MAC address is -b MAC=00::0C::01::02::03::04");
  283. mac_prefix_.push_back(static_cast<uint8_t>(ui));
  284. }
  285. }
  286. void
  287. CommandOptions::decodeDuid(const std::string& base) {
  288. size_t found = base.find('=');
  289. check(found == std::string::npos, "expected -b<base> format for DUID is -b DUID=<duid>");
  290. std::string b = base.substr(found + 1);
  291. check(b.length() & 1, "odd number of hexadecimal digits in duid");
  292. check(b.length() > 128, "duid too large");
  293. check(b.length() == 0, "no duid specified");
  294. for (int i = 0; i < b.length(); i += 2) {
  295. unsigned int ui = 0;
  296. std::istringstream ss(b.substr(i, 2));
  297. check(!(ss >> std::hex >> ui >> std::dec) || (ui > 0xFF),
  298. "illegal characters " + b + " in duid");
  299. duid_prefix_.push_back(static_cast<uint8_t>(ui));
  300. }
  301. }
  302. void
  303. CommandOptions::validate() const {
  304. check((getIpVersion() != 4) && (isBroadcast() != 0),
  305. "-B is not compatible with IPv6 (-6)");
  306. check((getIpVersion() != 6) && (isRapidCommit() != 0),
  307. "-6 (IPv6) must be set to use -c");
  308. check((getExchangeMode() == DO_SA) && (getNumRequests().size() > 1),
  309. "second -n<num-request> is not compatible with -i");
  310. check((getExchangeMode() == DO_SA) && (getLostTime()[1] != 1.),
  311. "second -d<drop-time> is not compatible with -i");
  312. check((getExchangeMode() == DO_SA) &&
  313. ((getMaxDrop().size() > 1) || (getMaxDropPercentage().size() > 1)),
  314. "second -D<max-drop> is not compatible with -i\n");
  315. check((getExchangeMode() == DO_SA) && (isUseFirst()),
  316. "-1 is not compatible with -i\n");
  317. check((getExchangeMode() == DO_SA) && (getTemplateFiles().size() > 1),
  318. "second -T<template-file> is not compatible with -i\n");
  319. check((getExchangeMode() == DO_SA) && (getXidOffset().size() > 1),
  320. "second -X<xid-offset> is not compatible with -i\n");
  321. check((getExchangeMode() == DO_SA) && (getRndOffset().size() > 1),
  322. "second -O<random-offset is not compatible with -i\n");
  323. check((getExchangeMode() == DO_SA) && (getElpOffset() >= 0),
  324. "-E<time-offset> is not compatible with -i\n");
  325. check((getExchangeMode() == DO_SA) && (getSidOffset() >= 0),
  326. "-S<srvid-offset> is not compatible with -i\n");
  327. check((getExchangeMode() == DO_SA) && (getRipOffset() >= 0),
  328. "-I<ip-offset> is not compatible with -i\n");
  329. check((getExchangeMode() != DO_SA) && (isRapidCommit() != 0),
  330. "-i must be set to use -c\n");
  331. check((getRate() == 0) && (getReportDelay() != 0),
  332. "-r<rate> must be set to use -t<report>\n");
  333. check((getRate() == 0) && (getNumRequests().size() > 0),
  334. "-r<getRate()> must be set to use -n<num-request>\n");
  335. check((getRate() == 0) && (getPeriod() != 0),
  336. "-r<rate> must be set to use -p<test-period>\n");
  337. check((getRate() == 0) &&
  338. ((getMaxDrop().size() > 0) || getMaxDropPercentage().size() > 0),
  339. "-r<rate> must be set to use -D<max-drop>\n");
  340. check((getTemplateFiles().size() < getXidOffset().size()),
  341. "-T<template-file> must be set to use -X<xid-offset>\n");
  342. check((getTemplateFiles().size() < getRndOffset().size()),
  343. "-T<template-file> must be set to use -O<random-offset>\n");
  344. check((getTemplateFiles().size() < 2) && (getElpOffset() >= 0),
  345. "second/request -T<template-file> must be set to use -E<time-offset>\n");
  346. check((getTemplateFiles().size() < 2) && (getSidOffset() >= 0),
  347. "second/request -T<template-file> must be set to "
  348. "use -S<srvid-offset>\n");
  349. check((getTemplateFiles().size() < 2) && (getRipOffset() >= 0),
  350. "second/request -T<template-file> must be set to "
  351. "use -I<ip-offset>\n");
  352. }
  353. void
  354. CommandOptions::check(bool condition, const std::string errmsg) const {
  355. if (condition) {
  356. isc_throw(isc::InvalidParameter, errmsg);
  357. }
  358. }
  359. void
  360. CommandOptions::usage(void)
  361. {
  362. fprintf(stderr, "%s",
  363. "perfdhcp [-hv] [-4|-6] [-r<rate>] [-t<report>] [-R<range>] [-b<base>]\n"
  364. " [-n<num-request>] [-p<test-period>] [-d<drop-time>] [-D<max-drop>]\n"
  365. " [-l<local-addr|interface>] [-P<preload>] [-a<aggressivity>]\n"
  366. " [-L<local-port>] [-s<seed>] [-i] [-B] [-c] [-1]\n"
  367. " [-T<template-file>] [-X<xid-offset>] [-O<random-offset]\n"
  368. " [-E<time-offset>] [-S<srvid-offset>] [-I<ip-offset>]\n"
  369. " [-x<diagnostic-selector>] [-w<wrapped>] [server]\n"
  370. "\f\n"
  371. "The [server] argument is the name/address of the DHCP server to\n"
  372. "contact. For DHCPv4 operation, exchanges are initiated by\n"
  373. "transmitting a DHCP DISCOVER to this address.\n"
  374. "\n"
  375. "For DHCPv6 operation, exchanges are initiated by transmitting a DHCP\n"
  376. "SOLICIT to this address. In the DHCPv6 case, the special name 'all'\n"
  377. "can be used to refer to All_DHCP_Relay_Agents_and_Servers (the\n"
  378. "multicast address FF02::1:2), or the special name 'servers' to refer\n"
  379. "to All_DHCP_Servers (the multicast address FF05::1:3). The [server]\n"
  380. "argument is optional only in the case that -l is used to specify an\n"
  381. "interface, in which case [server] defaults to 'all'.\n"
  382. "\n"
  383. "The default is to perform a single 4-way exchange, effectively pinging\n"
  384. "the server.\n"
  385. "The -r option is used to set up a performance test, without\n"
  386. "it exchanges are initiated as fast as possible.\n"
  387. "\n"
  388. "Options:\n"
  389. "-1: Take the server-ID option from the first received message.\n"
  390. "-4: DHCPv4 operation (default). This is incompatible with the -6 option.\n"
  391. "-6: DHCPv6 operation. This is incompatible with the -4 option.\n"
  392. "-a<aggressivity>: When the target sending rate is not yet reached,\n"
  393. " control how many exchanges are initiated before the next pause.\n"
  394. "-b<base>: The base MAC, DUID, IP, etc, used to simulate different\n"
  395. " clients. This can be specified multiple times, each instance is\n"
  396. " in the <type>=<value> form, for instance:\n"
  397. " (and default) MAC=00:0c:01:02:03:04.\n"
  398. "-d<drop-time>: Specify the time after which a request is treated as\n"
  399. " having been lost. The value is given in seconds and may contain a\n"
  400. " fractional component. The default is 1 second.\n"
  401. "-E<time-offset>: Offset of the (DHCPv4) secs field / (DHCPv6)\n"
  402. " elapsed-time option in the (second/request) template.\n"
  403. " The value 0 disables it.\n"
  404. "-h: Print this help.\n"
  405. "-i: Do only the initial part of an exchange: DO or SA, depending on\n"
  406. " whether -6 is given.\n"
  407. "-I<ip-offset>: Offset of the (DHCPv4) IP address in the requested-IP\n"
  408. " option / (DHCPv6) IA_NA option in the (second/request) template.\n"
  409. "-l<local-addr|interface>: For DHCPv4 operation, specify the local\n"
  410. " hostname/address to use when communicating with the server. By\n"
  411. " default, the interface address through which traffic would\n"
  412. " normally be routed to the server is used.\n"
  413. " For DHCPv6 operation, specify the name of the network interface\n"
  414. " via which exchanges are initiated.\n"
  415. "-L<local-port>: Specify the local port to use\n"
  416. " (the value 0 means to use the default).\n"
  417. "-O<random-offset>: Offset of the last octet to randomize in the template.\n"
  418. "-P<preload>: Initiate first <preload> exchanges back to back at startup.\n"
  419. "-r<rate>: Initiate <rate> DORA/SARR (or if -i is given, DO/SA)\n"
  420. " exchanges per second. A periodic report is generated showing the\n"
  421. " number of exchanges which were not completed, as well as the\n"
  422. " average response latency. The program continues until\n"
  423. " interrupted, at which point a final report is generated.\n"
  424. "-R<range>: Specify how many different clients are used. With 1\n"
  425. " (the default), all requests seem to come from the same client.\n"
  426. "-s<seed>: Specify the seed for randomization, making it repeatable.\n"
  427. "-S<srvid-offset>: Offset of the server-ID option in the\n"
  428. " (second/request) template.\n"
  429. "-T<template-file>: The name of a file containing the template to use\n"
  430. " as a stream of hexadecimal digits.\n"
  431. "-v: Report the version number of this program.\n"
  432. "-w<wrapped>: Command to call with start/stop at the beginning/end of\n"
  433. " the program.\n"
  434. "-x<diagnostic-selector>: Include extended diagnostics in the output.\n"
  435. " <diagnostic-selector> is a string of single-keywords specifying\n"
  436. " the operations for which verbose output is desired. The selector\n"
  437. " keyletters are:\n"
  438. " * 'a': print the decoded command line arguments\n"
  439. " * 'e': print the exit reason\n"
  440. " * 'i': print rate processing details\n"
  441. " * 'r': print randomization details\n"
  442. " * 's': print first server-id\n"
  443. " * 't': when finished, print timers of all successful exchanges\n"
  444. " * 'T': when finished, print templates\n"
  445. "-X<xid-offset>: Transaction ID (aka. xid) offset in the template.\n"
  446. "\n"
  447. "DHCPv4 only options:\n"
  448. "-B: Force broadcast handling.\n"
  449. "\n"
  450. "DHCPv6 only options:\n"
  451. "-c: Add a rapid commit option (exchanges will be SA).\n"
  452. "\n"
  453. "The remaining options are used only in conjunction with -r:\n"
  454. "\n"
  455. "-D<max-drop>: Abort the test if more than <max-drop> requests have\n"
  456. " been dropped. Use -D0 to abort if even a single request has been\n"
  457. " dropped. If <max-drop> includes the suffix '%', it specifies a\n"
  458. " maximum percentage of requests that may be dropped before abort.\n"
  459. " In this case, testing of the threshold begins after 10 requests\n"
  460. " have been expected to be received.\n"
  461. "-n<num-request>: Initiate <num-request> transactions. No report is\n"
  462. " generated until all transactions have been initiated/waited-for,\n"
  463. " after which a report is generated and the program terminates.\n"
  464. "-p<test-period>: Send requests for the given test period, which is\n"
  465. " specified in the same manner as -d. This can be used as an\n"
  466. " alternative to -n, or both options can be given, in which case the\n"
  467. " testing is completed when either limit is reached.\n"
  468. "-t<report>: Delay in seconds between two periodic reports.\n"
  469. "\n"
  470. "Errors:\n"
  471. "- tooshort: received a too short message\n"
  472. "- orphans: received a message which doesn't match an exchange\n"
  473. " (duplicate, late or not related)\n"
  474. "- locallimit: reached to local system limits when sending a message.\n"
  475. "\n"
  476. "Exit status:\n"
  477. "The exit status is:\n"
  478. "0 on complete success.\n"
  479. "1 for a general error.\n"
  480. "2 if an error is found in the command line arguments.\n"
  481. "3 if there are no general failures in operation, but one or more\n"
  482. " exchanges are not successfully completed.\n");
  483. }
  484. } // namespace perfdhcp
  485. } // namespace isc