command_options.h 17 KB

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