command_options.h 20 KB

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