command_options.h 20 KB

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