command_options.h 17 KB

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