command_options.h 18 KB

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