command_options.h 17 KB

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