command_options.h 20 KB

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