command_options.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Copyright (C) 2011 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. /// Resets the CommandOptions object to default values.
  41. void reset();
  42. /// \brief Parse command line
  43. ///
  44. /// Parses the command line and stores the selected options.
  45. ///
  46. /// \param argc Argument count passed to main().
  47. /// \param argv Argument value array passed to main().
  48. /// \param force_reset Force reset of state variables
  49. /// \throws isc::InvalidParameter if parsing fails
  50. void parse(int argc, char** const argv);
  51. /// \brief Returns IP version
  52. ///
  53. /// \return IP version to be used
  54. uint8_t getIpVersion() const { return ipversion_; }
  55. /// \brief Returns packet exchange mode
  56. ///
  57. /// \return packet exchange mode
  58. ExchangeMode getExchangeMode() const { return exchange_mode_; }
  59. /// \brief Returns echange rate
  60. ///
  61. /// \return exchange rate per second
  62. int getRate() const { return rate_; }
  63. /// \brief Returns report delay
  64. ///
  65. /// \return delay between two consecutive reports
  66. int getReportDelay() const { return report_delay_; }
  67. /// \brief Returns randomization range
  68. ///
  69. /// \return randomization range
  70. uint32_t getRandomRange() const { return random_range_; }
  71. /// \brief Returns MAC addr prefix
  72. ///
  73. /// \ return MAC addr prefix to generate different clients
  74. std::vector<uint8_t> getMacPrefix() const { return mac_prefix_; }
  75. /// \brief Returns DUID prefix
  76. ///
  77. /// \return DUID prefix to generate different clients
  78. std::vector<uint8_t> getDuidPrefix() const { return duid_prefix_; }
  79. /// \brief Returns base values
  80. ///
  81. /// \return base values for xid generation
  82. std::vector<std::string> getBase() const { return base_; }
  83. /// \brief Returns number of exchanges
  84. ///
  85. /// \return number of exchange requests
  86. std::vector<int> getNumRequests() const { return num_request_; }
  87. /// \brief Returns test period
  88. ///
  89. /// \return test period
  90. int getPeriod() const { return period_; }
  91. /// \brief Returns lost time
  92. ///
  93. /// \return return time before request is lost
  94. std::vector<double> getDropTime() const { return drop_time_; }
  95. /// \brief Returns max drops number
  96. ///
  97. /// \return maximum number of lost requests
  98. std::vector<int> getMaxDrop() const { return max_drop_; }
  99. /// \brief Returns max percentage of drops
  100. ///
  101. /// \return maximum percentage of lost requests
  102. std::vector<double> getMaxDropPercentage() const { return max_pdrop_; }
  103. /// \brief Returns local address or interface
  104. ///
  105. /// \return local address or interface
  106. std::string getLocalName() const { return localname_; }
  107. /// \brief Checks if interface name is used
  108. ///
  109. /// \return true if interface name was specified
  110. bool isInterface() const { return is_interface_; }
  111. /// \brief Returns number of preload exchanges
  112. ///
  113. /// \return number of preload exchanges
  114. int getPreload() const { return preload_; }
  115. /// \brief Returns aggressivity value
  116. ///
  117. /// \return aggressivity value
  118. int getAggressivity() const { return aggressivity_; }
  119. /// \brief Returns local port number
  120. ///
  121. /// \return local port number
  122. int getLocalPort() const { return local_port_; }
  123. /// \brief Checks if seed provided
  124. ///
  125. /// \return true if seed was provided
  126. bool isSeeded() const { return seeded_; }
  127. /// \brief Returns radom seed
  128. ///
  129. /// \return random seed
  130. uint32_t getSeed() const { return seed_; }
  131. /// \brief Checks if broadcast
  132. ///
  133. /// \return true if broadcast handling
  134. bool isBroadcast() const { return broadcast_; }
  135. /// \brief Check if rapid commit used
  136. ///
  137. /// \return true if rapid commit is used
  138. bool isRapidCommit() const { return rapid_commit_; }
  139. /// \brief Check if server-ID taken from first package
  140. ///
  141. /// \return true if server-iD to be taken from first package
  142. bool isUseFirst() const { return use_first_; }
  143. /// \brief Returns template file names
  144. ///
  145. /// \return template file names
  146. std::vector<std::string> getTemplateFiles() const { return template_file_; }
  147. /// brief Returns template offsets for xid
  148. ///
  149. /// \return template offsets for xid
  150. std::vector<int> getTransactionIdOffset() const { return xid_offset_; }
  151. /// \brief Returns template offsets for rnd
  152. ///
  153. /// \return template offsets for rnd
  154. std::vector<int> getRandomOffset() const { return rnd_offset_; }
  155. /// \brief Returns template offset for elapsed time
  156. ///
  157. /// \return template offset for elapsed time
  158. int getElapsedTimeOffset() const { return elp_offset_; }
  159. /// \brief Returns template offset for server-ID
  160. ///
  161. /// \return template offset for server-ID
  162. int getServerIdOffset() const { return sid_offset_; }
  163. /// \brief Returns template offset for requested IP
  164. ///
  165. /// \return template offset for requested IP
  166. int getRequestedIpOffset() const { return rip_offset_; }
  167. /// \brief Returns diagnostic selectors
  168. ///
  169. /// \return diagnostic selector
  170. std::string getDiags() const { return diags_; }
  171. /// \brief Returns wrapped command
  172. ///
  173. /// \return wrapped command
  174. std::string getWrapped() const { return wrapped_; }
  175. /// \brief Returns server name
  176. ///
  177. /// \return server name
  178. std::string getServerName() const { return server_name_; }
  179. /// \brief Print usage
  180. ///
  181. /// Prints perfdhcp usage
  182. void usage() const;
  183. /// \brief Print program version
  184. ///
  185. /// Prints perfdhcp version
  186. void version() const;
  187. private:
  188. /// \brief Default Constructor
  189. ///
  190. /// Protected constructor as this is a singleton class.
  191. /// Use CommandOptions::instance() to get instance of it.
  192. CommandOptions() {
  193. reset();
  194. }
  195. private:
  196. /// \brief Initializes class members based command line
  197. ///
  198. /// Reads each command line parameter and sets class member values
  199. ///
  200. /// \param argc Argument count passed to main().
  201. /// \param argv Argument value array passed to main().
  202. /// \throws isc::InvalidParameter if command line options initialization fails
  203. void initialize(int argc, char** argv);
  204. /// \brief Validates initialized options
  205. ///
  206. /// \throws isc::InvalidParameter if command line validation fails
  207. void validate() const;
  208. /// \brief Throws !InvalidParameter exception if condition is true
  209. ///
  210. /// Convenience function that throws an !InvalidParameter exception if
  211. /// the condition argument is true
  212. ///
  213. /// \param condition Condition to be checked
  214. /// \param errmsg Error message in exception
  215. /// \throws isc::InvalidParameter if condition argument true
  216. inline void check(bool condition, const std::string& errmsg) const;
  217. /// \brief Casts command line arg to positive int
  218. ///
  219. /// \param errmsg Error message if lexical cast fails
  220. /// \throw InvalidParameter if lexical cast fails
  221. int positiveInteger(const std::string& errmsg);
  222. /// \brief Casts command line arg to non-negative int
  223. ///
  224. /// \param errmsg Error message if lexical cast fails
  225. /// \throw InvalidParameter if lexical cast fails
  226. int nonNegativeInteger(const std::string& errmsg);
  227. /// \brief Returns command line string if it is non-empty
  228. ///
  229. /// \param errmsg Error message if string is empty
  230. /// \throw InvalidParameter if string is empty
  231. std::string nonEmptyString(const std::string& errmsg);
  232. /// \brief Calculates max_random_ and random_range_
  233. ///
  234. /// \param opt Value of -R option
  235. /// \throw InvalidParameter if -R<value> is wrong
  236. void initRandomRange(const std::string& opt);
  237. /// \brief Decodes base provided with -b<base>
  238. ///
  239. /// Function decodes parameters given with -b<base>
  240. /// parameter. Currently it supports the following command line options e.g.:
  241. /// -b mac=00:01:02:03:04:05
  242. /// -b duid=0F1234 (duid can be up to 128 hex digits)
  243. // Function will decode 00:01:02:03:04:05 and/or
  244. /// 0F1234 repsectively and initialize mac_prefix_
  245. /// and/or duid_prefix_ members
  246. ///
  247. /// \param base Base in string format
  248. /// \throws isc::InvalidParameter if base is invalid
  249. void decodeBase(const std::string& base);
  250. /// \brief Decodes base MAC address provided with -b<base>
  251. ///
  252. /// Function decodes parameter given as -b mac=00:01:02:03:04:05
  253. /// Function will decode 00:01:02:03:04:05 initialize mac_prefix_
  254. /// class member.
  255. /// Provided MAC address is for example only
  256. ///
  257. /// \param base Base string given as -b mac=00:01:02:03:04:05
  258. /// \throws isc::InvalidParameter if mac address is invalid
  259. void decodeMac(const std::string& base);
  260. /// \brief Decodes base DUID provided with -b<base>
  261. ///
  262. /// Function decodes parameter given as -b duid=0F1234
  263. /// Function will decode 0F1234 and initialize duid_prefix_
  264. /// class member.
  265. /// Provided DUID is for example only.
  266. ///
  267. /// \param base Base string given as -b mac=00:01:02:03:04:05
  268. /// \throws isc::InvalidParameter if DUID is invalid
  269. void decodeDuid(const std::string& base);
  270. /// \brief converts two digit hex string to byte
  271. ///
  272. /// \param s hex string e.g. AF
  273. /// \throw isc::InvalidParameter if string does not represent hex byte
  274. uint8_t convertHexString(const std::string& s);
  275. uint8_t ipversion_; ///< IP version
  276. ExchangeMode exchange_mode_ ; ///< Packet exchange mode (e.g. DORR/SARR)
  277. int rate_; ///< rate in exchange per second
  278. int report_delay_; ///< delay between two reports
  279. uint32_t random_range_; ///< number of simulated clients
  280. uint32_t max_random_; ///< maximum random value
  281. std::vector<uint8_t> mac_prefix_; ///< MAC addr prefix
  282. std::vector<uint8_t> duid_prefix_; ///< DUID prefix
  283. std::vector<std::string> base_; ///< base values for xid
  284. std::vector<int> num_request_; ///< number of exchanges
  285. int period_; ///< test period
  286. int drop_time_set_; ///< losttime[0] was set
  287. std::vector<double> drop_time_; ///< time before a request is lost
  288. int max_drop_set_; ///< max{p}drop[0] was set
  289. std::vector<int> max_drop_; ///< maximum number of lost requests
  290. std::vector<double> max_pdrop_; ///< maximum percentage
  291. std::string localname_; ///< local address or interface
  292. bool is_interface_; ///< interface vs local address
  293. int preload_; ///< preload exchanges
  294. int aggressivity_; ///< back to back exchanges
  295. int local_port_; ///< local port number (host endian)
  296. bool seeded_; ///< is a seed provided
  297. uint32_t seed_; ///< randomization seed
  298. bool broadcast_; ///< use broadcast
  299. bool rapid_commit_; ///< add rapid commit option
  300. bool use_first_; ///< where to take the server-ID
  301. std::vector<std::string> template_file_; ///< template file name
  302. std::vector<int> xid_offset_; ///< template offsets (xid)*/
  303. std::vector<int> rnd_offset_; ///< template offsets (random)
  304. int elp_offset_; ///< template offset (elapsed time)
  305. int sid_offset_; ///< template offset (server ID)
  306. int rip_offset_; ///< template offset (requested IP)
  307. std::string diags_; ///< diagnostic selectors
  308. std::string wrapped_; ///< wrapped command
  309. std::string server_name_; ///< server
  310. };
  311. } // namespace perfdhcp
  312. } // namespace isc
  313. #endif // __COMMAND_OPTIONS_H