command_options.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // Copyright (C) 2012-2013 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. #ifndef COMMAND_OPTIONS_H
  15. #define COMMAND_OPTIONS_H
  16. #include <boost/noncopyable.hpp>
  17. #include <stdint.h>
  18. #include <string>
  19. #include <vector>
  20. namespace isc {
  21. namespace perfdhcp {
  22. /// \brief Command Options.
  23. ///
  24. /// This class is responsible for parsing the command-line and storing the
  25. /// specified options.
  26. ///
  27. class CommandOptions : public boost::noncopyable {
  28. public:
  29. /// \brief A class encapsulating the type of lease being requested from the
  30. /// server.
  31. ///
  32. /// This class comprises convenience functions to convert the lease type
  33. /// to the textual format and to match the appropriate lease type with the
  34. /// value of the -e<lease-type> parameter specified from the command line.
  35. class LeaseType {
  36. public:
  37. /// The lease type code.
  38. enum Type {
  39. ADDRESS,
  40. PREFIX,
  41. ADDRESS_AND_PREFIX
  42. };
  43. LeaseType();
  44. /// \brief Constructor from lease type code.
  45. ///
  46. /// \param lease_type A lease type code.
  47. LeaseType(const Type lease_type);
  48. /// \brief Checks if lease type has the specified code.
  49. ///
  50. /// \param lease_type A lease type code to be checked.
  51. ///
  52. /// \return true if lease type is matched with the specified code.
  53. bool is(const Type lease_type) const;
  54. /// \brief Checks if lease type implies request for the address,
  55. /// prefix (or both) as specified by the function argument.
  56. ///
  57. /// This is a convenience function to check that, for the lease type
  58. /// specified from the command line, the address or prefix
  59. /// (IA_NA or IA_PD) option should be sent to the server.
  60. /// For example, if user specified '-e address-and-prefix' in the
  61. /// command line this function will return true for both ADDRESS
  62. /// and PREFIX, because both address and prefix is requested from
  63. /// the server.
  64. ///
  65. /// \param lease_type A lease type.
  66. ///
  67. /// \return true if the lease type implies creation of the address,
  68. /// prefix or both as specified by the argument.
  69. bool includes(const Type lease_type) const;
  70. /// \brief Sets the lease type code.
  71. ///
  72. /// \param lease_type A lease type code.
  73. void set(const Type lease_type);
  74. /// \brief Sets the lease type from the command line argument.
  75. ///
  76. /// \param cmd_line_arg An argument specified in the command line
  77. /// as -e<lease-type>:
  78. /// - address-only
  79. /// - prefix-only
  80. ///
  81. /// \throw isc::InvalidParameter if the specified argument is invalid.
  82. void fromCommandLine(const std::string& cmd_line_arg);
  83. /// \brief Return textual representation of the lease type.
  84. ///
  85. /// \return A textual representation of the lease type.
  86. std::string toText() const;
  87. private:
  88. Type type_; ///< A lease type code.
  89. };
  90. /// 2-way (cmd line param -i) or 4-way exchanges
  91. enum ExchangeMode {
  92. DO_SA,
  93. DORA_SARR
  94. };
  95. /// CommandOptions is a singleton class. This method returns reference
  96. /// to its sole instance.
  97. ///
  98. /// \return the only existing instance of command options
  99. static CommandOptions& instance();
  100. /// \brief Reset to defaults
  101. ///
  102. /// Reset data members to default values. This is specifically
  103. /// useful when unit tests are performed using different
  104. /// command line options.
  105. void reset();
  106. /// \brief Parse command line.
  107. ///
  108. /// Parses the command line and stores the selected options
  109. /// in class data members.
  110. ///
  111. /// \param argc Argument count passed to main().
  112. /// \param argv Argument value array passed to main().
  113. /// \param print_cmd_line Print the command line being run to the console.
  114. /// \throws isc::InvalidParameter if parse fails.
  115. /// \return true if program has been run in help or version mode ('h' or 'v' flag).
  116. bool parse(int argc, char** const argv, bool print_cmd_line = false);
  117. /// \brief Returns IP version.
  118. ///
  119. /// \return IP version to be used.
  120. uint8_t getIpVersion() const { return ipversion_; }
  121. /// \brief Returns packet exchange mode.
  122. ///
  123. /// \return packet exchange mode.
  124. ExchangeMode getExchangeMode() const { return exchange_mode_; }
  125. /// \ brief Returns the type of lease being requested.
  126. ///
  127. /// \return type of lease being requested by perfdhcp.
  128. LeaseType getLeaseType() const { return (lease_type_); }
  129. /// \brief Returns echange rate.
  130. ///
  131. /// \return exchange rate per second.
  132. int getRate() const { return rate_; }
  133. /// \brief Returns a rate at which DHCPv6 Renew messages are sent.
  134. ///
  135. /// \return A rate at which IPv6 Renew messages are sent.
  136. int getRenewRate() const { return (renew_rate_); }
  137. /// \brief Returns a rate at which DHCPv6 Release messages are sent.
  138. ///
  139. /// \return A rate at which DHCPv6 Release messages are sent.
  140. int getReleaseRate() const { return (release_rate_); }
  141. /// \brief Returns delay between two performance reports.
  142. ///
  143. /// \return delay between two consecutive performance reports.
  144. int getReportDelay() const { return report_delay_; }
  145. /// \brief Returns number of simulated clients.
  146. ///
  147. /// \return number of simulated clients.
  148. uint32_t getClientsNum() const { return clients_num_; }
  149. /// \brief Returns MAC address template.
  150. ///
  151. /// \return MAC address template to simulate different clients.
  152. std::vector<uint8_t> getMacTemplate() const { return mac_template_; }
  153. /// \brief Returns DUID template.
  154. ///
  155. /// \return DUID template to simulate different clients.
  156. std::vector<uint8_t> getDuidTemplate() const { return duid_template_; }
  157. /// \brief Returns base values.
  158. ///
  159. /// \return all base values specified.
  160. std::vector<std::string> getBase() const { return base_; }
  161. /// \brief Returns maximum number of exchanges.
  162. ///
  163. /// \return number of exchange requests before test is aborted.
  164. std::vector<int> getNumRequests() const { return num_request_; }
  165. /// \brief Returns test period.
  166. ///
  167. /// \return test period before it is aborted.
  168. int getPeriod() const { return period_; }
  169. /// \brief Returns drop time
  170. ///
  171. /// The method returns maximum time elapsed from
  172. /// sending the packet before it is assumed dropped.
  173. ///
  174. /// \return return time before request is assumed dropped.
  175. std::vector<double> getDropTime() const { return drop_time_; }
  176. /// \brief Returns maximum drops number.
  177. ///
  178. /// Returns maximum number of packet drops before
  179. /// aborting a test.
  180. ///
  181. /// \return maximum number of dropped requests.
  182. std::vector<int> getMaxDrop() const { return max_drop_; }
  183. /// \brief Returns maximal percentage of drops.
  184. ///
  185. /// Returns maximal percentage of packet drops
  186. /// before aborting a test.
  187. ///
  188. /// \return maximum percentage of lost requests.
  189. std::vector<double> getMaxDropPercentage() const { return max_pdrop_; }
  190. /// \brief Returns local address or interface name.
  191. ///
  192. /// \return local address or interface name.
  193. std::string getLocalName() const { return localname_; }
  194. /// \brief Checks if interface name was used.
  195. ///
  196. /// The method checks if interface name was used
  197. /// rather than address.
  198. ///
  199. /// \return true if interface name was used.
  200. bool isInterface() const { return is_interface_; }
  201. /// \brief Returns number of preload exchanges.
  202. ///
  203. /// \return number of preload exchanges.
  204. int getPreload() const { return preload_; }
  205. /// \brief Returns aggressivity value.
  206. ///
  207. /// \return aggressivity value.
  208. int getAggressivity() const { return aggressivity_; }
  209. /// \brief Returns local port number.
  210. ///
  211. /// \return local port number.
  212. int getLocalPort() const { return local_port_; }
  213. /// \brief Checks if seed provided.
  214. ///
  215. /// \return true if seed was provided.
  216. bool isSeeded() const { return seeded_; }
  217. /// \brief Returns radom seed.
  218. ///
  219. /// \return random seed.
  220. uint32_t getSeed() const { return seed_; }
  221. /// \brief Checks if broadcast address is to be used.
  222. ///
  223. /// \return true if broadcast address is to be used.
  224. bool isBroadcast() const { return broadcast_; }
  225. /// \brief Check if rapid commit option used.
  226. ///
  227. /// \return true if rapid commit option is used.
  228. bool isRapidCommit() const { return rapid_commit_; }
  229. /// \brief Check if server-ID to be taken from first package.
  230. ///
  231. /// \return true if server-iD to be taken from first package.
  232. bool isUseFirst() const { return use_first_; }
  233. /// \brief Returns template file names.
  234. ///
  235. /// \return template file names.
  236. std::vector<std::string> getTemplateFiles() const { return template_file_; }
  237. /// brief Returns template offsets for xid.
  238. ///
  239. /// \return template offsets for xid.
  240. std::vector<int> getTransactionIdOffset() const { return xid_offset_; }
  241. /// \brief Returns template offsets for rnd.
  242. ///
  243. /// \return template offsets for rnd.
  244. std::vector<int> getRandomOffset() const { return rnd_offset_; }
  245. /// \brief Returns template offset for elapsed time.
  246. ///
  247. /// \return template offset for elapsed time.
  248. int getElapsedTimeOffset() const { return elp_offset_; }
  249. /// \brief Returns template offset for server-ID.
  250. ///
  251. /// \return template offset for server-ID.
  252. int getServerIdOffset() const { return sid_offset_; }
  253. /// \brief Returns template offset for requested IP.
  254. ///
  255. /// \return template offset for requested IP.
  256. int getRequestedIpOffset() const { return rip_offset_; }
  257. /// \brief Returns diagnostic selectors.
  258. ///
  259. /// \return diagnostics selector.
  260. std::string getDiags() const { return diags_; }
  261. /// \brief Returns wrapped command.
  262. ///
  263. /// \return wrapped command (start/stop).
  264. std::string getWrapped() const { return wrapped_; }
  265. /// \brief Returns server name.
  266. ///
  267. /// \return server name.
  268. std::string getServerName() const { return server_name_; }
  269. /// \brief Print command line arguments.
  270. void printCommandLine() const;
  271. /// \brief Print usage.
  272. ///
  273. /// Prints perfdhcp usage.
  274. void usage() const;
  275. /// \brief Print program version.
  276. ///
  277. /// Prints perfdhcp version.
  278. void version() const;
  279. private:
  280. /// \brief Default Constructor.
  281. ///
  282. /// Private constructor as this is a singleton class.
  283. /// Use CommandOptions::instance() to get instance of it.
  284. CommandOptions() {
  285. reset();
  286. }
  287. /// \brief Initializes class members based on the command line.
  288. ///
  289. /// Reads each command line parameter and sets class member values.
  290. ///
  291. /// \param argc Argument count passed to main().
  292. /// \param argv Argument value array passed to main().
  293. /// \param print_cmd_line Print the command line being run to the console.
  294. /// \throws isc::InvalidParameter if command line options initialization fails.
  295. /// \return true if program has been run in help or version mode ('h' or 'v' flag).
  296. bool initialize(int argc, char** argv, bool print_cmd_line);
  297. /// \brief Validates initialized options.
  298. ///
  299. /// \throws isc::InvalidParameter if command line validation fails.
  300. void validate() const;
  301. /// \brief Throws !InvalidParameter exception if condition is true.
  302. ///
  303. /// Convenience function that throws an InvalidParameter exception if
  304. /// the condition argument is true.
  305. ///
  306. /// \param condition Condition to be checked.
  307. /// \param errmsg Error message in exception.
  308. /// \throws isc::InvalidParameter if condition argument true.
  309. inline void check(bool condition, const std::string& errmsg) const;
  310. /// \brief Casts command line argument to positive integer.
  311. ///
  312. /// \param errmsg Error message if lexical cast fails.
  313. /// \throw InvalidParameter if lexical cast fails.
  314. int positiveInteger(const std::string& errmsg) const;
  315. /// \brief Casts command line argument to non-negative integer.
  316. ///
  317. /// \param errmsg Error message if lexical cast fails.
  318. /// \throw InvalidParameter if lexical cast fails.
  319. int nonNegativeInteger(const std::string& errmsg) const;
  320. /// \brief Returns command line string if it is not empty.
  321. ///
  322. /// \param errmsg Error message if string is empty.
  323. /// \throw InvalidParameter if string is empty.
  324. std::string nonEmptyString(const std::string& errmsg) const;
  325. /// \brief Decodes the lease type requested by perfdhcp from optarg.
  326. ///
  327. /// \throw InvalidParameter if lease type value specified is invalid.
  328. void initLeaseType();
  329. /// \brief Set number of clients.
  330. ///
  331. /// Interprets the getopt() "opt" global variable as the number of clients
  332. /// (a non-negative number). This value is specified by the "-R" switch.
  333. ///
  334. /// \throw InvalidParameter if -R<value> is wrong.
  335. void initClientsNum();
  336. /// \brief Sets value indicating if interface name was given.
  337. ///
  338. /// Method checks if the command line argument given with
  339. /// '-l' option is the interface name. The is_interface_ member
  340. /// is set accordingly.
  341. void initIsInterface();
  342. /// \brief Decodes base provided with -b<base>.
  343. ///
  344. /// Function decodes argument of -b switch, which
  345. /// specifies a base value used to generate unique
  346. /// mac or duid values in packets sent to system
  347. /// under test.
  348. /// The following forms of switch arguments are supported:
  349. /// - -b mac=00:01:02:03:04:05
  350. /// - -b duid=0F1234 (duid can be up to 128 hex digits)
  351. // Function will decode 00:01:02:03:04:05 and/or
  352. /// 0F1234 respectively and initialize mac_template_
  353. /// and/or duid_template_ members.
  354. ///
  355. /// \param base Base in string format.
  356. /// \throws isc::InvalidParameter if base is invalid.
  357. void decodeBase(const std::string& base);
  358. /// \brief Decodes base MAC address provided with -b<base>.
  359. ///
  360. /// Function decodes parameter given as -b mac=00:01:02:03:04:05
  361. /// The function will decode 00:01:02:03:04:05 initialize mac_template_
  362. /// class member.
  363. /// Provided MAC address is for example only.
  364. ///
  365. /// \param base Base string given as -b mac=00:01:02:03:04:05.
  366. /// \throws isc::InvalidParameter if mac address is invalid.
  367. void decodeMac(const std::string& base);
  368. /// \brief Decodes base DUID provided with -b<base>.
  369. ///
  370. /// Function decodes parameter given as -b duid=0F1234.
  371. /// The function will decode 0F1234 and initialize duid_template_
  372. /// class member.
  373. /// Provided DUID is for example only.
  374. ///
  375. /// \param base Base string given as -b duid=0F1234.
  376. /// \throws isc::InvalidParameter if DUID is invalid.
  377. void decodeDuid(const std::string& base);
  378. /// \brief Generates DUID-LLT (based on link layer address).
  379. ///
  380. /// Function generates DUID based on link layer address and
  381. /// initiates duid_template_ value with it.
  382. /// \todo add support to generate DUIDs other than based on
  383. /// 6-octets long MACs (e.g. DUID-UUID.
  384. void generateDuidTemplate();
  385. /// \brief Converts two-digit hexadecimal string to a byte.
  386. ///
  387. /// \param hex_text Hexadecimal string e.g. AF.
  388. /// \throw isc::InvalidParameter if string does not represent hex byte.
  389. uint8_t convertHexString(const std::string& hex_text) const;
  390. /// IP protocol version to be used, expected values are:
  391. /// 4 for IPv4 and 6 for IPv6, default value 0 means "not set"
  392. uint8_t ipversion_;
  393. /// Packet exchange mode (e.g. DORA/SARR)
  394. ExchangeMode exchange_mode_;
  395. /// Lease Type to be obtained: address only, IPv6 prefix only.
  396. LeaseType lease_type_;
  397. /// Rate in exchange per second
  398. int rate_;
  399. /// A rate at which DHCPv6 Renew messages are sent.
  400. int renew_rate_;
  401. /// A rate at which DHCPv6 Release messages are sent.
  402. int release_rate_;
  403. /// Delay between generation of two consecutive
  404. /// performance reports
  405. int report_delay_;
  406. /// Number of simulated clients (aka randomization range).
  407. uint32_t clients_num_;
  408. /// MAC address template used to generate unique MAC
  409. /// addresses for simulated clients.
  410. std::vector<uint8_t> mac_template_;
  411. /// DUID template used to generate unique DUIDs for
  412. /// simulated clients
  413. std::vector<uint8_t> duid_template_;
  414. /// Collection of base values specified with -b<value>
  415. /// options. Supported "bases" are mac=<mac> and duid=<duid>
  416. std::vector<std::string> base_;
  417. /// Number of 2 or 4-way exchanges to perform.
  418. std::vector<int> num_request_;
  419. /// Test period in seconds
  420. int period_;
  421. /// Indicates number of -d<value> parameters specified by user.
  422. /// If this value goes above 2, command line parsing fails.
  423. uint8_t drop_time_set_;
  424. /// Time to elapse before request is lost. The first value of
  425. /// two-element vector refers to DO/SA exchanges,
  426. /// second value refers to RA/RR. Default values are { 1, 1 }
  427. std::vector<double> drop_time_;
  428. /// Maximum number of drops request before aborting test.
  429. /// First value of two-element vector specifies maximum
  430. /// number of drops for DO/SA exchange, second value
  431. /// specifies maximum number of drops for RA/RR.
  432. std::vector<int> max_drop_;
  433. /// Maximal percentage of lost requests before aborting test.
  434. /// First value of two-element vector specifies percentage for
  435. /// DO/SA exchanges, second value for RA/RR.
  436. std::vector<double> max_pdrop_;
  437. /// Local address or interface specified with -l<value> option.
  438. std::string localname_;
  439. /// Indicates that specified value with -l<value> is
  440. /// rather interface (not address)
  441. bool is_interface_;
  442. /// Number of preload packets. Preload packets are used to
  443. /// initiate communication with server before doing performance
  444. /// measurements.
  445. int preload_;
  446. /// Number of exchanges sent before next pause.
  447. int aggressivity_;
  448. /// Local port number (host endian)
  449. int local_port_;
  450. /// Randomization seed.
  451. uint32_t seed_;
  452. /// Indicates that randomization seed was provided.
  453. bool seeded_;
  454. /// Indicates that we use broadcast address.
  455. bool broadcast_;
  456. /// Indicates that we do rapid commit option.
  457. bool rapid_commit_;
  458. /// Indicates that we take server id from first received packet.
  459. bool use_first_;
  460. /// Packet template file names. These files store template packets
  461. /// that are used for initiating exchanges. Template packets
  462. /// read from files are later tuned with variable data.
  463. std::vector<std::string> template_file_;
  464. /// Offset of transaction id in template files. First vector
  465. /// element points to offset for DISCOVER/SOLICIT messages,
  466. /// second element points to transaction id offset for
  467. /// REQUEST messages
  468. std::vector<int> xid_offset_;
  469. /// Random value offset in templates. Random value offset
  470. /// points to last octet of DUID. Up to 4 last octets of
  471. /// DUID are randomized to simulate different clients.
  472. std::vector<int> rnd_offset_;
  473. /// Offset of elapsed time option in template packet.
  474. int elp_offset_;
  475. /// Offset of server id option in template packet.
  476. int sid_offset_;
  477. /// Offset of requested ip data in template packet
  478. int rip_offset_;
  479. /// String representing diagnostic selectors specified
  480. /// by user with -x<value>.
  481. std::string diags_;
  482. /// Command to be executed at the beginning/end of the test.
  483. /// This command is expected to expose start and stop argument.
  484. std::string wrapped_;
  485. /// Server name specified as last argument of command line.
  486. std::string server_name_;
  487. };
  488. } // namespace perfdhcp
  489. } // namespace isc
  490. #endif // COMMAND_OPTIONS_H