command_options.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. enum ExchangeMode {
  29. DO_SA,
  30. DORR_SARR
  31. };
  32. /// CommandOptions is a singleton class. This method returns reference
  33. /// to its sole instance.
  34. ///
  35. /// \return the only existing instance of command options
  36. static CommandOptions& instance();
  37. /// \brief Reset to defaults
  38. ///
  39. /// Resets the CommandOptions object to default values.
  40. void reset();
  41. /// \brief Parse command line
  42. ///
  43. /// Parses the command line and stores the selected options.
  44. ///
  45. /// \param argc Argument count passed to main().
  46. /// \param argv Argument value array passed to main().
  47. /// \param force_reset Force reset of state variables
  48. /// \throws isc::InvalidParameter if parsing fails
  49. void parse(int argc, char** const argv, bool force_reset = false);
  50. /// \brief Returns IP version
  51. ///
  52. /// \return IP version to be used
  53. uint8_t getIpVersion() const { return ipversion_; }
  54. /// \brief Returns packet exchange mode
  55. ///
  56. /// \return packet exchange mode
  57. ExchangeMode getExchangeMode() const { return exchange_mode_; }
  58. /// \brief Returns echange rate
  59. ///
  60. /// \return exchange rate per second
  61. int getRate() const { return rate_; }
  62. /// \brief Returns report delay
  63. ///
  64. /// \return delay between two consecutive reports
  65. int getReportDelay() const { return report_delay_; }
  66. /// \brief Returns randomization range
  67. ///
  68. /// \return randomization range
  69. uint32_t getRandomRange() const { return random_range_; }
  70. /// \brief Returns MAC addr prefix
  71. ///
  72. /// \ return MAC addr prefix to generate different clients
  73. std::vector<uint8_t> getMacPrefix() const { return mac_prefix_; }
  74. /// \brief Returns DUID prefix
  75. ///
  76. /// \return DUID prefix to generate different clients
  77. std::vector<uint8_t> getDuidPrefix() const { return duid_prefix_; }
  78. /// \brief Returns base values
  79. ///
  80. /// \return base values for xid generation
  81. std::vector<std::string> getBase() const { return base_; }
  82. /// \brief Returns number of exchanges
  83. ///
  84. /// \return number of exchange requests
  85. std::vector<int> getNumRequests() const { return num_request_; }
  86. /// \brief Returns test period
  87. ///
  88. /// \return test period
  89. int getPeriod() const { return period_; }
  90. /// \brief Returns lost time
  91. ///
  92. /// \return return time before request is lost
  93. std::vector<double> getLostTime() const { return lost_time_; }
  94. /// \brief Returns max drops number
  95. ///
  96. /// \return maximum number of lost requests
  97. std::vector<int> getMaxDrop() const { return max_drop_; }
  98. /// \brief Returns max percentage of drops
  99. ///
  100. /// \return maximum percentage of lost requests
  101. std::vector<double> getMaxDropPercentage() const { return max_pdrop_; }
  102. /// \brief Returns local address or interface
  103. ///
  104. /// \return local address or interface
  105. std::string getLocalName() const { return localname_; }
  106. /// \brief Checks if interface name is used
  107. ///
  108. /// \return true if interface name was specified
  109. bool isInterface() const { return is_interface_; }
  110. /// \brief Returns number of preload exchanges
  111. ///
  112. /// \return number of preload exchanges
  113. int getPreload() const { return preload_; }
  114. /// \brief Returns aggressivity value
  115. ///
  116. /// \return aggressivity value
  117. int getAggressivity() const { return aggressivity_; }
  118. /// \brief Returns local port number
  119. ///
  120. /// \return local port number
  121. int getLocalPort() const { return local_port_; }
  122. /// \brief Checks if seed provided
  123. ///
  124. /// \return true if seed was provided
  125. bool isSeeded() const { return seeded_; }
  126. /// \brief Returns radom seed
  127. ///
  128. /// \return random seed
  129. uint32_t getSeed() const { return seed_; }
  130. /// \brief Checks if broadcast
  131. ///
  132. /// \return true if broadcast handling
  133. bool isBroadcast() const { return broadcast_; }
  134. /// \brief Check if rapid commit used
  135. ///
  136. /// \return true if rapid commit is used
  137. bool isRapidCommit() const { return rapid_commit_; }
  138. /// \brief Check if server-ID taken from first package
  139. ///
  140. /// \return true if server-iD to be taken from first package
  141. bool isUseFirst() const { return use_first_; }
  142. /// \brief Returns template file names
  143. ///
  144. /// \return template file names
  145. std::vector<std::string> getTemplateFiles() const { return template_file_; }
  146. /// brief Returns template offsets for xid
  147. ///
  148. /// \return template offsets for xid
  149. std::vector<int> getXidOffset() const { return xid_offset_; }
  150. /// \brief Returns template offsets for rnd
  151. ///
  152. /// \return template offsets for rnd
  153. std::vector<int> getRndOffset() const { return rnd_offset_; }
  154. /// \brief Returns template offset for elapsed time
  155. ///
  156. /// \return template offset for elapsed time
  157. int getElpOffset() const { return elp_offset_; }
  158. /// \brief Returns template offset for server-ID
  159. ///
  160. /// \return template offset for server-ID
  161. int getSidOffset() const { return sid_offset_; }
  162. /// \brief Returns template offset for requested IP
  163. ///
  164. /// \return template offset for requested IP
  165. int getRipOffset() const { return rip_offset_; }
  166. /// \brief Returns diagnostic selectors
  167. ///
  168. /// \return diagnostic selector
  169. std::string getDiags() const { return diags_; }
  170. /// \brief Returns wrapped command
  171. ///
  172. /// \return wrapped command
  173. std::string getWrapped() const { return wrapped_; }
  174. /// \brief Returns server name
  175. ///
  176. /// \return server name
  177. std::string getServerName() const { return server_name_; }
  178. /// \brief Print usage
  179. ///
  180. /// Prints perfdhcp usage
  181. void usage() const;
  182. /// \brief Print program version
  183. ///
  184. /// Prints perfdhcp version
  185. void version() const;
  186. protected:
  187. /// \brief Default Constructor
  188. ///
  189. /// Protected constructor as this is a singleton class.
  190. /// Use CommandOptions::instance() to get instance of it.
  191. CommandOptions() {
  192. reset();
  193. }
  194. private:
  195. /// \brief Create instance of the singleton class
  196. static void instanceCreate();
  197. /// \brief Initializes class members based command line
  198. ///
  199. /// Reads each command line parameter and sets class member values
  200. ///
  201. /// \param argc Argument count passed to main().
  202. /// \param argv Argument value array passed to main().
  203. /// \throws isc::InvalidParameter if command line options initialization fails
  204. void initialize(int argc, char** const argv);
  205. /// \brief Validates initialized options
  206. ///
  207. /// \throws isc::InvalidPrameter if command line validation fails
  208. void validate() const;
  209. /// \brief Checks given condition
  210. ///
  211. /// \param condition Condition to be checked
  212. /// \param errmsg Error message in exception
  213. /// \throws isc::InvalidParameter if check fails
  214. inline void check(bool condition, const std::string& errmsg) const;
  215. /// \brief Decodes base provided with -b
  216. ///
  217. /// \param base Base in string format
  218. /// \throws isc::InvalidParameter if base is invalid
  219. void decodeBase(const std::string& base);
  220. /// \brief Decodes base MAC address provided with -b
  221. ///
  222. /// \param base MAC address in string format
  223. /// \throws isc::InvalidParameter if base is invalid
  224. void decodeMac(const std::string& base);
  225. /// \brief Decodes base DUID provided with -b
  226. ///
  227. /// \param base DUID in string format
  228. /// \throws isc::InvalidParameter if base is invalid
  229. void decodeDuid(const std::string& base);
  230. static CommandOptions * instance_; ///< A pointer to sole instance of this class
  231. uint8_t ipversion_; ///< IP version
  232. ExchangeMode exchange_mode_ ; ///< Packet exchange mode (e.g. DORR/SARR)
  233. int rate_; ///< rate in exchange per second
  234. int report_delay_; ///< delay between two reports
  235. uint32_t random_range_; ///< randomization range
  236. uint32_t max_random_; ///< maximum random value
  237. std::vector<uint8_t> mac_prefix_; ///< MAC addr prefix
  238. std::vector<uint8_t> duid_prefix_; ///< DUID prefix
  239. std::vector<std::string> base_; ///< base values for xid
  240. std::vector<int> num_request_; ///< number of exchanges
  241. int period_; ///< test period
  242. int lost_time_set_; ///< losttime[0] was set
  243. std::vector<double> lost_time_; ///< time before a request is lost
  244. int max_drop_set_; ///< max{p}drop[0] was set
  245. std::vector<int> max_drop_; ///< maximum number of lost requests
  246. std::vector<double> max_pdrop_; ///< maximum percentage
  247. std::string localname_; ///< local address or interface
  248. bool is_interface_; ///< interface vs local address
  249. int preload_; ///< preload exchanges
  250. int aggressivity_; ///< back to back exchanges
  251. int local_port_; ///< local port number (host endian)
  252. bool seeded_; ///< is a seed provided
  253. uint32_t seed_; ///< randomization seed
  254. bool broadcast_; ///< use broadcast
  255. bool rapid_commit_; ///< add rapid commit option
  256. bool use_first_; ///< where to take the server-ID
  257. std::vector<std::string> template_file_; ///< template file name
  258. std::vector<int> xid_offset_; ///< template offsets (xid)*/
  259. std::vector<int> rnd_offset_; ///< template offsets (random)
  260. int elp_offset_; ///< template offset (elapsed time)
  261. int sid_offset_; ///< template offset (server ID)
  262. int rip_offset_; ///< template offset (requested IP)
  263. std::string diags_; ///< diagnostic selectors
  264. std::string wrapped_; ///< wrapped command
  265. std::string server_name_; ///< server
  266. };
  267. } // namespace perfdhcp
  268. } // namespace isc
  269. #endif // __COMMAND_OPTIONS_H