command_options.h 19 KB

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