command_options.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef COMMAND_OPTIONS_H
  7. #define COMMAND_OPTIONS_H
  8. #include <boost/noncopyable.hpp>
  9. #include <stdint.h>
  10. #include <string>
  11. #include <vector>
  12. namespace isc {
  13. namespace perfdhcp {
  14. /// \brief Command Options.
  15. ///
  16. /// This class is responsible for parsing the command-line and storing the
  17. /// specified options.
  18. ///
  19. class CommandOptions : public boost::noncopyable {
  20. public:
  21. /// \brief A class encapsulating the type of lease being requested from the
  22. /// server.
  23. ///
  24. /// This class comprises convenience functions to convert the lease type
  25. /// to the textual format and to match the appropriate lease type with the
  26. /// value of the -e<lease-type> parameter specified from the command line.
  27. class LeaseType {
  28. public:
  29. /// The lease type code.
  30. enum Type {
  31. ADDRESS,
  32. PREFIX,
  33. ADDRESS_AND_PREFIX
  34. };
  35. LeaseType();
  36. /// \brief Constructor from lease type code.
  37. ///
  38. /// \param lease_type A lease type code.
  39. LeaseType(const Type lease_type);
  40. /// \brief Checks if lease type has the specified code.
  41. ///
  42. /// \param lease_type A lease type code to be checked.
  43. ///
  44. /// \return true if lease type is matched with the specified code.
  45. bool is(const Type lease_type) const;
  46. /// \brief Checks if lease type implies request for the address,
  47. /// prefix (or both) as specified by the function argument.
  48. ///
  49. /// This is a convenience function to check that, for the lease type
  50. /// specified from the command line, the address or prefix
  51. /// (IA_NA or IA_PD) option should be sent to the server.
  52. /// For example, if user specified '-e address-and-prefix' in the
  53. /// command line this function will return true for both ADDRESS
  54. /// and PREFIX, because both address and prefix is requested from
  55. /// the server.
  56. ///
  57. /// \param lease_type A lease type.
  58. ///
  59. /// \return true if the lease type implies creation of the address,
  60. /// prefix or both as specified by the argument.
  61. bool includes(const Type lease_type) const;
  62. /// \brief Sets the lease type code.
  63. ///
  64. /// \param lease_type A lease type code.
  65. void set(const Type lease_type);
  66. /// \brief Sets the lease type from the command line argument.
  67. ///
  68. /// \param cmd_line_arg An argument specified in the command line
  69. /// as -e<lease-type>:
  70. /// - address-only
  71. /// - prefix-only
  72. ///
  73. /// \throw isc::InvalidParameter if the specified argument is invalid.
  74. void fromCommandLine(const std::string& cmd_line_arg);
  75. /// \brief Return textual representation of the lease type.
  76. ///
  77. /// \return A textual representation of the lease type.
  78. std::string toText() const;
  79. private:
  80. Type type_; ///< A lease type code.
  81. };
  82. /// 2-way (cmd line param -i) or 4-way exchanges
  83. enum ExchangeMode {
  84. DO_SA,
  85. DORA_SARR
  86. };
  87. /// CommandOptions is a singleton class. This method returns reference
  88. /// to its sole instance.
  89. ///
  90. /// \return the only existing instance of command options
  91. static CommandOptions& instance();
  92. /// \brief Reset to defaults
  93. ///
  94. /// Reset data members to default values. This is specifically
  95. /// useful when unit tests are performed using different
  96. /// command line options.
  97. void reset();
  98. /// \brief Parse command line.
  99. ///
  100. /// Parses the command line and stores the selected options
  101. /// in class data members.
  102. ///
  103. /// \param argc Argument count passed to main().
  104. /// \param argv Argument value array passed to main().
  105. /// \param print_cmd_line Print the command line being run to the console.
  106. /// \throws isc::InvalidParameter if parse fails.
  107. /// \return true if program has been run in help or version mode ('h' or 'v' flag).
  108. bool parse(int argc, char** const argv, bool print_cmd_line = false);
  109. /// \brief Returns IP version.
  110. ///
  111. /// \return IP version to be used.
  112. uint8_t getIpVersion() const { return ipversion_; }
  113. /// \brief Returns packet exchange mode.
  114. ///
  115. /// \return packet exchange mode.
  116. ExchangeMode getExchangeMode() const { return exchange_mode_; }
  117. /// \ brief Returns the type of lease being requested.
  118. ///
  119. /// \return type of lease being requested by perfdhcp.
  120. LeaseType getLeaseType() const { return (lease_type_); }
  121. /// \brief Returns echange rate.
  122. ///
  123. /// \return exchange rate per second.
  124. int getRate() const { return rate_; }
  125. /// \brief Returns a rate at which DHCPv6 Renew messages are sent.
  126. ///
  127. /// \return A rate at which IPv6 Renew messages are sent.
  128. int getRenewRate() const { return (renew_rate_); }
  129. /// \brief Returns a rate at which DHCPv6 Release messages are sent.
  130. ///
  131. /// \return A rate at which DHCPv6 Release messages are sent.
  132. int getReleaseRate() const { return (release_rate_); }
  133. /// \brief Returns delay between two performance reports.
  134. ///
  135. /// \return delay between two consecutive performance reports.
  136. int getReportDelay() const { return report_delay_; }
  137. /// \brief Returns number of simulated clients.
  138. ///
  139. /// \return number of simulated clients.
  140. uint32_t getClientsNum() const { return clients_num_; }
  141. /// \brief Returns MAC address template.
  142. ///
  143. /// \return MAC address template to simulate different clients.
  144. std::vector<uint8_t> getMacTemplate() const { return mac_template_; }
  145. /// \brief Returns DUID template.
  146. ///
  147. /// \return DUID template to simulate different clients.
  148. std::vector<uint8_t> getDuidTemplate() const { return duid_template_; }
  149. /// \brief Returns base values.
  150. ///
  151. /// \return all base values specified.
  152. std::vector<std::string> getBase() const { return base_; }
  153. /// \brief Returns maximum number of exchanges.
  154. ///
  155. /// \return number of exchange requests before test is aborted.
  156. std::vector<int> getNumRequests() const { return num_request_; }
  157. /// \brief Returns test period.
  158. ///
  159. /// \return test period before it is aborted.
  160. int getPeriod() const { return period_; }
  161. /// \brief Returns drop time
  162. ///
  163. /// The method returns maximum time elapsed from
  164. /// sending the packet before it is assumed dropped.
  165. ///
  166. /// \return return time before request is assumed dropped.
  167. std::vector<double> getDropTime() const { return drop_time_; }
  168. /// \brief Returns maximum drops number.
  169. ///
  170. /// Returns maximum number of packet drops before
  171. /// aborting a test.
  172. ///
  173. /// \return maximum number of dropped requests.
  174. std::vector<int> getMaxDrop() const { return max_drop_; }
  175. /// \brief Returns maximal percentage of drops.
  176. ///
  177. /// Returns maximal percentage of packet drops
  178. /// before aborting a test.
  179. ///
  180. /// \return maximum percentage of lost requests.
  181. std::vector<double> getMaxDropPercentage() const { return max_pdrop_; }
  182. /// \brief Returns local address or interface name.
  183. ///
  184. /// \return local address or interface name.
  185. std::string getLocalName() const { return localname_; }
  186. /// \brief Checks if interface name was used.
  187. ///
  188. /// The method checks if interface name was used
  189. /// rather than address.
  190. ///
  191. /// \return true if interface name was used.
  192. bool isInterface() const { return is_interface_; }
  193. /// \brief Returns number of preload exchanges.
  194. ///
  195. /// \return number of preload exchanges.
  196. int getPreload() const { return preload_; }
  197. /// \brief Returns aggressivity value.
  198. ///
  199. /// \return aggressivity value.
  200. int getAggressivity() const { return aggressivity_; }
  201. /// \brief Returns local port number.
  202. ///
  203. /// \return local port number.
  204. int getLocalPort() const { return local_port_; }
  205. /// \brief Checks if seed provided.
  206. ///
  207. /// \return true if seed was provided.
  208. bool isSeeded() const { return seeded_; }
  209. /// \brief Returns radom seed.
  210. ///
  211. /// \return random seed.
  212. uint32_t getSeed() const { return seed_; }
  213. /// \brief Checks if broadcast address is to be used.
  214. ///
  215. /// \return true if broadcast address is to be used.
  216. bool isBroadcast() const { return broadcast_; }
  217. /// \brief Check if rapid commit option used.
  218. ///
  219. /// \return true if rapid commit option is used.
  220. bool isRapidCommit() const { return rapid_commit_; }
  221. /// \brief Check if server-ID to be taken from first package.
  222. ///
  223. /// \return true if server-iD to be taken from first package.
  224. bool isUseFirst() const { return use_first_; }
  225. /// \brief Check if generated DHCPv6 messages shuold appear as relayed.
  226. ///
  227. /// \return true if generated traffic should appear as relayed.
  228. bool isUseRelayedV6() const { return use_relayed_v6_; }
  229. /// \brief Returns template file names.
  230. ///
  231. /// \return template file names.
  232. std::vector<std::string> getTemplateFiles() const { return template_file_; }
  233. /// \brief Returns location of the file containing list of MAC addresses
  234. ///
  235. /// MAC addresses read from the file are used by the perfdhcp in message
  236. /// exchanges with the DHCP server.
  237. ///
  238. /// \return mac_template file name.
  239. std::string getMacListFile() const { return mac_file_list_; }
  240. /// \brief Returns the list of macs, every mac is a vector<uint8_t>
  241. ///
  242. /// \return mac_list_ vector of vectors.
  243. const std::vector<std::vector<uint8_t> >& getAllMacs() const { return mac_list_; }
  244. /// brief Returns template offsets for xid.
  245. ///
  246. /// \return template offsets for xid.
  247. std::vector<int> getTransactionIdOffset() const { return xid_offset_; }
  248. /// \brief Returns template offsets for rnd.
  249. ///
  250. /// \return template offsets for rnd.
  251. std::vector<int> getRandomOffset() const { return rnd_offset_; }
  252. /// \brief Returns template offset for elapsed time.
  253. ///
  254. /// \return template offset for elapsed time.
  255. int getElapsedTimeOffset() const { return elp_offset_; }
  256. /// \brief Returns template offset for server-ID.
  257. ///
  258. /// \return template offset for server-ID.
  259. int getServerIdOffset() const { return sid_offset_; }
  260. /// \brief Returns template offset for requested IP.
  261. ///
  262. /// \return template offset for requested IP.
  263. int getRequestedIpOffset() const { return rip_offset_; }
  264. /// \brief Returns diagnostic selectors.
  265. ///
  266. /// \return diagnostics selector.
  267. std::string getDiags() const { return diags_; }
  268. /// \brief Returns wrapped command.
  269. ///
  270. /// \return wrapped command (start/stop).
  271. std::string getWrapped() const { return wrapped_; }
  272. /// \brief Returns server name.
  273. ///
  274. /// \return server name.
  275. std::string getServerName() const { return server_name_; }
  276. /// \brief Print command line arguments.
  277. void printCommandLine() const;
  278. /// \brief Print usage.
  279. ///
  280. /// Prints perfdhcp usage.
  281. void usage() const;
  282. /// \brief Print program version.
  283. ///
  284. /// Prints perfdhcp version.
  285. void version() const;
  286. private:
  287. /// \brief Default Constructor.
  288. ///
  289. /// Private constructor as this is a singleton class.
  290. /// Use CommandOptions::instance() to get instance of it.
  291. CommandOptions() {
  292. reset();
  293. }
  294. /// \brief Initializes class members based on the command line.
  295. ///
  296. /// Reads each command line parameter and sets class member values.
  297. ///
  298. /// \param argc Argument count passed to main().
  299. /// \param argv Argument value array passed to main().
  300. /// \param print_cmd_line Print the command line being run to the console.
  301. /// \throws isc::InvalidParameter if command line options initialization fails.
  302. /// \return true if program has been run in help or version mode ('h' or 'v' flag).
  303. bool initialize(int argc, char** argv, bool print_cmd_line);
  304. /// \brief Validates initialized options.
  305. ///
  306. /// \throws isc::InvalidParameter if command line validation fails.
  307. void validate() const;
  308. /// \brief Throws !InvalidParameter exception if condition is true.
  309. ///
  310. /// Convenience function that throws an InvalidParameter exception if
  311. /// the condition argument is true.
  312. ///
  313. /// \param condition Condition to be checked.
  314. /// \param errmsg Error message in exception.
  315. /// \throws isc::InvalidParameter if condition argument true.
  316. inline void check(bool condition, const std::string& errmsg) const;
  317. /// \brief Casts command line argument to positive integer.
  318. ///
  319. /// \param errmsg Error message if lexical cast fails.
  320. /// \throw InvalidParameter if lexical cast fails.
  321. int positiveInteger(const std::string& errmsg) const;
  322. /// \brief Casts command line argument to non-negative integer.
  323. ///
  324. /// \param errmsg Error message if lexical cast fails.
  325. /// \throw InvalidParameter if lexical cast fails.
  326. int nonNegativeInteger(const std::string& errmsg) const;
  327. /// \brief Returns command line string if it is not empty.
  328. ///
  329. /// \param errmsg Error message if string is empty.
  330. /// \throw InvalidParameter if string is empty.
  331. std::string nonEmptyString(const std::string& errmsg) const;
  332. /// \brief Decodes the lease type requested by perfdhcp from optarg.
  333. ///
  334. /// \throw InvalidParameter if lease type value specified is invalid.
  335. void initLeaseType();
  336. /// \brief Set number of clients.
  337. ///
  338. /// Interprets the getopt() "opt" global variable as the number of clients
  339. /// (a non-negative number). This value is specified by the "-R" switch.
  340. ///
  341. /// \throw InvalidParameter if -R<value> is wrong.
  342. void initClientsNum();
  343. /// \brief Sets value indicating if interface name was given.
  344. ///
  345. /// Method checks if the command line argument given with
  346. /// '-l' option is the interface name. The is_interface_ member
  347. /// is set accordingly.
  348. void initIsInterface();
  349. /// \brief Decodes base provided with -b<base>.
  350. ///
  351. /// Function decodes argument of -b switch, which
  352. /// specifies a base value used to generate unique
  353. /// mac or duid values in packets sent to system
  354. /// under test.
  355. /// The following forms of switch arguments are supported:
  356. /// - -b mac=00:01:02:03:04:05
  357. /// - -b duid=0F1234 (duid can be up to 128 hex digits)
  358. // Function will decode 00:01:02:03:04:05 and/or
  359. /// 0F1234 respectively and initialize mac_template_
  360. /// and/or duid_template_ members.
  361. ///
  362. /// \param base Base in string format.
  363. /// \throws isc::InvalidParameter if base is invalid.
  364. void decodeBase(const std::string& base);
  365. /// \brief Decodes base MAC address provided with -b<base>.
  366. ///
  367. /// Function decodes parameter given as -b mac=00:01:02:03:04:05
  368. /// The function will decode 00:01:02:03:04:05 initialize mac_template_
  369. /// class member.
  370. /// Provided MAC address is for example only.
  371. ///
  372. /// \param base Base string given as -b mac=00:01:02:03:04:05.
  373. /// \throws isc::InvalidParameter if mac address is invalid.
  374. void decodeMacBase(const std::string& base);
  375. /// \brief Decodes base DUID provided with -b<base>.
  376. ///
  377. /// Function decodes parameter given as -b duid=0F1234.
  378. /// The function will decode 0F1234 and initialize duid_template_
  379. /// class member.
  380. /// Provided DUID is for example only.
  381. ///
  382. /// \param base Base string given as -b duid=0F1234.
  383. /// \throws isc::InvalidParameter if DUID is invalid.
  384. void decodeDuid(const std::string& base);
  385. /// \brief Generates DUID-LLT (based on link layer address).
  386. ///
  387. /// Function generates DUID based on link layer address and
  388. /// initiates duid_template_ value with it.
  389. /// \todo add support to generate DUIDs other than based on
  390. /// 6-octets long MACs (e.g. DUID-UUID.
  391. void generateDuidTemplate();
  392. /// \brief Converts two-digit hexadecimal string to a byte.
  393. ///
  394. /// \param hex_text Hexadecimal string e.g. AF.
  395. /// \throw isc::InvalidParameter if string does not represent hex byte.
  396. uint8_t convertHexString(const std::string& hex_text) const;
  397. /// \brief Opens the text file containing list of macs (one per line)
  398. /// and adds them to the mac_list_ vector.
  399. void loadMacs();
  400. /// \brief Decodes a mac string into a vector of uint8_t and adds it to the
  401. /// mac_list_ vector.
  402. bool decodeMacString(const std::string& line);
  403. /// IP protocol version to be used, expected values are:
  404. /// 4 for IPv4 and 6 for IPv6, default value 0 means "not set"
  405. uint8_t ipversion_;
  406. /// Packet exchange mode (e.g. DORA/SARR)
  407. ExchangeMode exchange_mode_;
  408. /// Lease Type to be obtained: address only, IPv6 prefix only.
  409. LeaseType lease_type_;
  410. /// Rate in exchange per second
  411. int rate_;
  412. /// A rate at which DHCPv6 Renew messages are sent.
  413. int renew_rate_;
  414. /// A rate at which DHCPv6 Release messages are sent.
  415. int release_rate_;
  416. /// Delay between generation of two consecutive
  417. /// performance reports
  418. int report_delay_;
  419. /// Number of simulated clients (aka randomization range).
  420. uint32_t clients_num_;
  421. /// MAC address template used to generate unique MAC
  422. /// addresses for simulated clients.
  423. std::vector<uint8_t> mac_template_;
  424. /// DUID template used to generate unique DUIDs for
  425. /// simulated clients
  426. std::vector<uint8_t> duid_template_;
  427. /// Collection of base values specified with -b<value>
  428. /// options. Supported "bases" are mac=<mac> and duid=<duid>
  429. std::vector<std::string> base_;
  430. /// Number of 2 or 4-way exchanges to perform.
  431. std::vector<int> num_request_;
  432. /// Test period in seconds
  433. int period_;
  434. /// Indicates number of -d<value> parameters specified by user.
  435. /// If this value goes above 2, command line parsing fails.
  436. uint8_t drop_time_set_;
  437. /// Time to elapse before request is lost. The first value of
  438. /// two-element vector refers to DO/SA exchanges,
  439. /// second value refers to RA/RR. Default values are { 1, 1 }
  440. std::vector<double> drop_time_;
  441. /// Maximum number of drops request before aborting test.
  442. /// First value of two-element vector specifies maximum
  443. /// number of drops for DO/SA exchange, second value
  444. /// specifies maximum number of drops for RA/RR.
  445. std::vector<int> max_drop_;
  446. /// Maximal percentage of lost requests before aborting test.
  447. /// First value of two-element vector specifies percentage for
  448. /// DO/SA exchanges, second value for RA/RR.
  449. std::vector<double> max_pdrop_;
  450. /// Local address or interface specified with -l<value> option.
  451. std::string localname_;
  452. /// Indicates that specified value with -l<value> is
  453. /// rather interface (not address)
  454. bool is_interface_;
  455. /// Number of preload packets. Preload packets are used to
  456. /// initiate communication with server before doing performance
  457. /// measurements.
  458. int preload_;
  459. /// Number of exchanges sent before next pause.
  460. int aggressivity_;
  461. /// Local port number (host endian)
  462. int local_port_;
  463. /// Randomization seed.
  464. uint32_t seed_;
  465. /// Indicates that randomization seed was provided.
  466. bool seeded_;
  467. /// Indicates that we use broadcast address.
  468. bool broadcast_;
  469. /// Indicates that we do rapid commit option.
  470. bool rapid_commit_;
  471. /// Indicates that we take server id from first received packet.
  472. bool use_first_;
  473. /// Packet template file names. These files store template packets
  474. /// that are used for initiating exchanges. Template packets
  475. /// read from files are later tuned with variable data.
  476. std::vector<std::string> template_file_;
  477. /// A file containing a list of macs, one per line. This can be used if
  478. /// you don't want to genrate Mac starting from a base mac but rather provide
  479. /// the tool with a list of macs it should randomize on.
  480. std::string mac_file_list_;
  481. std::vector<std::vector<uint8_t> > mac_list_;
  482. /// Offset of transaction id in template files. First vector
  483. /// element points to offset for DISCOVER/SOLICIT messages,
  484. /// second element points to transaction id offset for
  485. /// REQUEST messages
  486. std::vector<int> xid_offset_;
  487. /// Random value offset in templates. Random value offset
  488. /// points to last octet of DUID. Up to 4 last octets of
  489. /// DUID are randomized to simulate different clients.
  490. std::vector<int> rnd_offset_;
  491. /// Offset of elapsed time option in template packet.
  492. int elp_offset_;
  493. /// Offset of server id option in template packet.
  494. int sid_offset_;
  495. /// Offset of requested ip data in template packet
  496. int rip_offset_;
  497. /// String representing diagnostic selectors specified
  498. /// by user with -x<value>.
  499. std::string diags_;
  500. /// Command to be executed at the beginning/end of the test.
  501. /// This command is expected to expose start and stop argument.
  502. std::string wrapped_;
  503. /// Server name specified as last argument of command line.
  504. std::string server_name_;
  505. /// Controls whether generated dhcpv6 test traffic should be relayed.
  506. bool use_relayed_v6_;
  507. };
  508. } // namespace perfdhcp
  509. } // namespace isc
  510. #endif // COMMAND_OPTIONS_H