command_options.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 template.
  74. ///
  75. /// \return MAC address template to simulate different clients.
  76. std::vector<uint8_t> getMacTemplate() const { return mac_template_; }
  77. /// \brief Returns DUID template.
  78. ///
  79. /// \return DUID template to simulate different clients.
  80. std::vector<uint8_t> getDuidTemplate() const { return duid_template_; }
  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 command line arguments.
  194. void printCommandLine() const;
  195. /// \brief Print usage.
  196. ///
  197. /// Prints perfdhcp usage.
  198. void usage() const;
  199. /// \brief Print program version.
  200. ///
  201. /// Prints perfdhcp version.
  202. void version() const;
  203. private:
  204. /// \brief Default Constructor.
  205. ///
  206. /// Private constructor as this is a singleton class.
  207. /// Use CommandOptions::instance() to get instance of it.
  208. CommandOptions() {
  209. reset();
  210. }
  211. /// \brief Initializes class members based on the command line.
  212. ///
  213. /// Reads each command line parameter and sets class member values.
  214. ///
  215. /// \param argc Argument count passed to main().
  216. /// \param argv Argument value array passed to main().
  217. /// \throws isc::InvalidParameter if command line options initialization fails.
  218. void initialize(int argc, char** argv);
  219. /// \brief Validates initialized options.
  220. ///
  221. /// \throws isc::InvalidParameter if command line validation fails.
  222. void validate() const;
  223. /// \brief Throws !InvalidParameter exception if condition is true.
  224. ///
  225. /// Convenience function that throws an InvalidParameter exception if
  226. /// the condition argument is true.
  227. ///
  228. /// \param condition Condition to be checked.
  229. /// \param errmsg Error message in exception.
  230. /// \throws isc::InvalidParameter if condition argument true.
  231. inline void check(bool condition, const std::string& errmsg) const;
  232. /// \brief Casts command line argument to positive integer.
  233. ///
  234. /// \param errmsg Error message if lexical cast fails.
  235. /// \throw InvalidParameter if lexical cast fails.
  236. int positiveInteger(const std::string& errmsg) const;
  237. /// \brief Casts command line argument to non-negative integer.
  238. ///
  239. /// \param errmsg Error message if lexical cast fails.
  240. /// \throw InvalidParameter if lexical cast fails.
  241. int nonNegativeInteger(const std::string& errmsg) const;
  242. /// \brief Returns command line string if it is not empty.
  243. ///
  244. /// \param errmsg Error message if string is empty.
  245. /// \throw InvalidParameter if string is empty.
  246. std::string nonEmptyString(const std::string& errmsg) const;
  247. /// \brief Set number of clients.
  248. ///
  249. /// Interprets the getopt() "opt" global variable as the number of clients
  250. /// (a non-negative number). This value is specified by the "-R" switch.
  251. ///
  252. /// \throw InvalidParameter if -R<value> is wrong.
  253. void initClientsNum();
  254. /// \brief Sets value indicating if interface name was given.
  255. ///
  256. /// Method checks if the command line argument given with
  257. /// '-l' option is the interface name. The is_interface_ member
  258. /// is set accordingly.
  259. void initIsInterface();
  260. /// \brief Decodes base provided with -b<base>.
  261. ///
  262. /// Function decodes argument of -b switch, which
  263. /// specifies a base value used to generate unique
  264. /// mac or duid values in packets sent to system
  265. /// under test.
  266. /// The following forms of switch arguments are supported:
  267. /// - -b mac=00:01:02:03:04:05
  268. /// - -b duid=0F1234 (duid can be up to 128 hex digits)
  269. // Function will decode 00:01:02:03:04:05 and/or
  270. /// 0F1234 respectively and initialize mac_template_
  271. /// and/or duid_template_ members.
  272. ///
  273. /// \param base Base in string format.
  274. /// \throws isc::InvalidParameter if base is invalid.
  275. void decodeBase(const std::string& base);
  276. /// \brief Decodes base MAC address provided with -b<base>.
  277. ///
  278. /// Function decodes parameter given as -b mac=00:01:02:03:04:05
  279. /// The function will decode 00:01:02:03:04:05 initialize mac_template_
  280. /// class member.
  281. /// Provided MAC address is for example only.
  282. ///
  283. /// \param base Base string given as -b mac=00:01:02:03:04:05.
  284. /// \throws isc::InvalidParameter if mac address is invalid.
  285. void decodeMac(const std::string& base);
  286. /// \brief Decodes base DUID provided with -b<base>.
  287. ///
  288. /// Function decodes parameter given as -b duid=0F1234.
  289. /// The function will decode 0F1234 and initialize duid_template_
  290. /// class member.
  291. /// Provided DUID is for example only.
  292. ///
  293. /// \param base Base string given as -b duid=0F1234.
  294. /// \throws isc::InvalidParameter if DUID is invalid.
  295. void decodeDuid(const std::string& base);
  296. /// \brief Generates DUID-LLT (based on link layer address).
  297. ///
  298. /// Function generates DUID based on link layer address and
  299. /// initiates duid_template_ value with it.
  300. /// \todo add support to generate DUIDs other than based on
  301. /// 6-octets long MACs (e.g. DUID-UUID.
  302. void generateDuidTemplate();
  303. /// \brief Converts two-digit hexadecimal string to a byte.
  304. ///
  305. /// \param hex_text Hexadecimal string e.g. AF.
  306. /// \throw isc::InvalidParameter if string does not represent hex byte.
  307. uint8_t convertHexString(const std::string& hex_text) const;
  308. uint8_t ipversion_; ///< IP protocol version to be used, expected values are:
  309. ///< 4 for IPv4 and 6 for IPv6, default value 0 means "not set"
  310. ExchangeMode exchange_mode_; ///< Packet exchange mode (e.g. DORA/SARR)
  311. int rate_; ///< Rate in exchange per second
  312. int report_delay_; ///< Delay between generation of two consecutive
  313. ///< performance reports
  314. uint32_t clients_num_; ///< Number of simulated clients (aka randomization range).
  315. std::vector<uint8_t> mac_template_; ///< MAC address template used to generate unique DUIDs
  316. ///< for simulated clients.
  317. std::vector<uint8_t> duid_template_; ///< DUID template used to generate unique DUIDs for
  318. ///< simulated clients
  319. std::vector<std::string> base_; ///< Collection of base values specified with -b<value>
  320. ///< options. Supported "bases" are mac=<mac> and duid=<duid>
  321. std::vector<int> num_request_; ///< Number of 2 or 4-way exchanges to perform
  322. int period_; ///< Test period in seconds
  323. uint8_t drop_time_set_; ///< Indicates number of -d<value> parameters specified by user.
  324. ///< If this value goes above 2, command line parsing fails.
  325. std::vector<double> drop_time_; ///< Time to elapse before request is lost. The fisrt value of
  326. ///< two-element vector refers to DO/SA exchanges,
  327. ///< second value refers to RA/RR. Default values are { 1, 1 }
  328. std::vector<int> max_drop_; ///< Maximum number of drops request before aborting test.
  329. ///< First value of two-element vector specifies maximum
  330. ///< number of drops for DO/SA exchange, second value
  331. ///< specifies maximum number of drops for RA/RR.
  332. std::vector<double> max_pdrop_; ///< Maximal percentage of lost requests before aborting test.
  333. ///< First value of two-element vector specifies percentage for
  334. ///< DO/SA exchanges, second value for RA/RR.
  335. std::string localname_; ///< Local address or interface specified with -l<value> option.
  336. bool is_interface_; ///< Indicates that specified value with -l<value> is
  337. ///< rather interface (not address)
  338. int preload_; ///< Number of preload packets. Preload packets are used to
  339. ///< initiate communication with server before doing performance
  340. ///< measurements.
  341. int aggressivity_; ///< Number of exchanges sent before next pause.
  342. int local_port_; ///< Local port number (host endian)
  343. bool seeded_; ///< Indicates that randomization seed was provided.
  344. uint32_t seed_; ///< Randomization seed.
  345. bool broadcast_; ///< Indicates that we use broadcast address.
  346. bool rapid_commit_; ///< Indicates that we do rapid commit option.
  347. bool use_first_; ///< Indicates that we take server id from first received packet.
  348. std::vector<std::string> template_file_; ///< Packet template file names. These files store template packets
  349. ///< that are used for initiating echanges. Template packets
  350. ///< read from files are later tuned with variable data.
  351. std::vector<int> xid_offset_; ///< Offset of transaction id in template files. First vector
  352. ///< element points to offset for DISCOVER/SOLICIT messages,
  353. ///< second element points to trasaction id offset for
  354. ///< REQUEST messages
  355. std::vector<int> rnd_offset_; ///< Random value offset in templates. Random value offset
  356. ///< points to last octet of DUID. Up to 4 last octets of
  357. ///< DUID are randomized to simulate differnt clients.
  358. int elp_offset_; ///< Offset of elapsed time option in template packet.
  359. int sid_offset_; ///< Offset of server id option in template packet.
  360. int rip_offset_; ///< Offset of requested ip data in template packet/
  361. std::string diags_; ///< String representing diagnostic selectors specified
  362. ///< by user with -x<value>.
  363. std::string wrapped_; ///< Wrapped command specified as -w<value>. Expected
  364. ///< values are start and stop.
  365. std::string server_name_; ///< Server name specified as last argument of command line.
  366. std::string commandline_; ///< Entire command line as typed in by the user.
  367. };
  368. } // namespace perfdhcp
  369. } // namespace isc
  370. #endif // __COMMAND_OPTIONS_H