123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- // Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
- //
- // Permission to use, copy, modify, and/or distribute this software for any
- // purpose with or without fee is hereby granted, provided that the above
- // copyright notice and this permission notice appear in all copies.
- //
- // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- // PERFORMANCE OF THIS SOFTWARE.
- #ifndef COMMAND_OPTIONS_H
- #define COMMAND_OPTIONS_H
- #include <string>
- #include <vector>
- #include <boost/noncopyable.hpp>
- namespace isc {
- namespace perfdhcp {
- /// \brief Command Options.
- ///
- /// This class is responsible for parsing the command-line and storing the
- /// specified options.
- ///
- class CommandOptions : public boost::noncopyable {
- public:
- /// 2-way (cmd line param -i) or 4-way exchanges
- enum ExchangeMode {
- DO_SA,
- DORA_SARR
- };
- /// CommandOptions is a singleton class. This method returns reference
- /// to its sole instance.
- ///
- /// \return the only existing instance of command options
- static CommandOptions& instance();
- /// \brief Reset to defaults
- ///
- /// Reset data members to default values. This is specifically
- /// useful when unit tests are performed using different
- /// command line options.
- void reset();
- /// \brief Parse command line.
- ///
- /// Parses the command line and stores the selected options
- /// in class data members.
- ///
- /// \param argc Argument count passed to main().
- /// \param argv Argument value array passed to main().
- /// \param print_cmd_line Print the command line being run to the console.
- /// \throws isc::InvalidParameter if parse fails.
- /// \return true if program has been run in help or version mode ('h' or 'v' flag).
- bool parse(int argc, char** const argv, bool print_cmd_line = false);
- /// \brief Returns IP version.
- ///
- /// \return IP version to be used.
- uint8_t getIpVersion() const { return ipversion_; }
- /// \brief Returns packet exchange mode.
- ///
- /// \return packet exchange mode.
- ExchangeMode getExchangeMode() const { return exchange_mode_; }
- /// \brief Returns echange rate.
- ///
- /// \return exchange rate per second.
- int getRate() const { return rate_; }
- /// \brief Returns delay between two performance reports.
- ///
- /// \return delay between two consecutive performance reports.
- int getReportDelay() const { return report_delay_; }
- /// \brief Returns number of simulated clients.
- ///
- /// \return number of simulated clients.
- uint32_t getClientsNum() const { return clients_num_; }
- /// \brief Returns MAC address template.
- ///
- /// \return MAC address template to simulate different clients.
- std::vector<uint8_t> getMacTemplate() const { return mac_template_; }
- /// \brief Returns DUID template.
- ///
- /// \return DUID template to simulate different clients.
- std::vector<uint8_t> getDuidTemplate() const { return duid_template_; }
- /// \brief Returns base values.
- ///
- /// \return all base values specified.
- std::vector<std::string> getBase() const { return base_; }
- /// \brief Returns maximum number of exchanges.
- ///
- /// \return number of exchange requests before test is aborted.
- std::vector<int> getNumRequests() const { return num_request_; }
- /// \brief Returns test period.
- ///
- /// \return test period before it is aborted.
- int getPeriod() const { return period_; }
- /// \brief Returns drop time
- ///
- /// The method returns maximum time elapsed from
- /// sending the packet before it is assumed dropped.
- ///
- /// \return return time before request is assumed dropped.
- std::vector<double> getDropTime() const { return drop_time_; }
- /// \brief Returns maximum drops number.
- ///
- /// Returns maximum number of packet drops before
- /// aborting a test.
- ///
- /// \return maximum number of dropped requests.
- std::vector<int> getMaxDrop() const { return max_drop_; }
- /// \brief Returns maximal percentage of drops.
- ///
- /// Returns maximal percentage of packet drops
- /// before aborting a test.
- ///
- /// \return maximum percentage of lost requests.
- std::vector<double> getMaxDropPercentage() const { return max_pdrop_; }
- /// \brief Returns local address or interface name.
- ///
- /// \return local address or interface name.
- std::string getLocalName() const { return localname_; }
- /// \brief Checks if interface name was used.
- ///
- /// The method checks if interface name was used
- /// rather than address.
- ///
- /// \return true if interface name was used.
- bool isInterface() const { return is_interface_; }
- /// \brief Returns number of preload exchanges.
- ///
- /// \return number of preload exchanges.
- int getPreload() const { return preload_; }
- /// \brief Returns aggressivity value.
- ///
- /// \return aggressivity value.
- int getAggressivity() const { return aggressivity_; }
- /// \brief Returns local port number.
- ///
- /// \return local port number.
- int getLocalPort() const { return local_port_; }
- /// \brief Checks if seed provided.
- ///
- /// \return true if seed was provided.
- bool isSeeded() const { return seeded_; }
- /// \brief Returns radom seed.
- ///
- /// \return random seed.
- uint32_t getSeed() const { return seed_; }
- /// \brief Checks if broadcast address is to be used.
- ///
- /// \return true if broadcast address is to be used.
- bool isBroadcast() const { return broadcast_; }
- /// \brief Check if rapid commit option used.
- ///
- /// \return true if rapid commit option is used.
- bool isRapidCommit() const { return rapid_commit_; }
- /// \brief Check if server-ID to be taken from first package.
- ///
- /// \return true if server-iD to be taken from first package.
- bool isUseFirst() const { return use_first_; }
- /// \brief Returns template file names.
- ///
- /// \return template file names.
- std::vector<std::string> getTemplateFiles() const { return template_file_; }
- /// brief Returns template offsets for xid.
- ///
- /// \return template offsets for xid.
- std::vector<int> getTransactionIdOffset() const { return xid_offset_; }
- /// \brief Returns template offsets for rnd.
- ///
- /// \return template offsets for rnd.
- std::vector<int> getRandomOffset() const { return rnd_offset_; }
- /// \brief Returns template offset for elapsed time.
- ///
- /// \return template offset for elapsed time.
- int getElapsedTimeOffset() const { return elp_offset_; }
- /// \brief Returns template offset for server-ID.
- ///
- /// \return template offset for server-ID.
- int getServerIdOffset() const { return sid_offset_; }
- /// \brief Returns template offset for requested IP.
- ///
- /// \return template offset for requested IP.
- int getRequestedIpOffset() const { return rip_offset_; }
- /// \brief Returns diagnostic selectors.
- ///
- /// \return diagnostics selector.
- std::string getDiags() const { return diags_; }
- /// \brief Returns wrapped command.
- ///
- /// \return wrapped command (start/stop).
- std::string getWrapped() const { return wrapped_; }
- /// \brief Returns server name.
- ///
- /// \return server name.
- std::string getServerName() const { return server_name_; }
- /// \brief Print command line arguments.
- void printCommandLine() const;
- /// \brief Print usage.
- ///
- /// Prints perfdhcp usage.
- void usage() const;
- /// \brief Print program version.
- ///
- /// Prints perfdhcp version.
- void version() const;
- private:
- /// \brief Default Constructor.
- ///
- /// Private constructor as this is a singleton class.
- /// Use CommandOptions::instance() to get instance of it.
- CommandOptions() {
- reset();
- }
- /// \brief Initializes class members based on the command line.
- ///
- /// Reads each command line parameter and sets class member values.
- ///
- /// \param argc Argument count passed to main().
- /// \param argv Argument value array passed to main().
- /// \param print_cmd_line Print the command line being run to the console.
- /// \throws isc::InvalidParameter if command line options initialization fails.
- /// \return true if program has been run in help or version mode ('h' or 'v' flag).
- bool initialize(int argc, char** argv, bool print_cmd_line);
- /// \brief Validates initialized options.
- ///
- /// \throws isc::InvalidParameter if command line validation fails.
- void validate() const;
- /// \brief Throws !InvalidParameter exception if condition is true.
- ///
- /// Convenience function that throws an InvalidParameter exception if
- /// the condition argument is true.
- ///
- /// \param condition Condition to be checked.
- /// \param errmsg Error message in exception.
- /// \throws isc::InvalidParameter if condition argument true.
- inline void check(bool condition, const std::string& errmsg) const;
- /// \brief Casts command line argument to positive integer.
- ///
- /// \param errmsg Error message if lexical cast fails.
- /// \throw InvalidParameter if lexical cast fails.
- int positiveInteger(const std::string& errmsg) const;
- /// \brief Casts command line argument to non-negative integer.
- ///
- /// \param errmsg Error message if lexical cast fails.
- /// \throw InvalidParameter if lexical cast fails.
- int nonNegativeInteger(const std::string& errmsg) const;
- /// \brief Returns command line string if it is not empty.
- ///
- /// \param errmsg Error message if string is empty.
- /// \throw InvalidParameter if string is empty.
- std::string nonEmptyString(const std::string& errmsg) const;
- /// \brief Set number of clients.
- ///
- /// Interprets the getopt() "opt" global variable as the number of clients
- /// (a non-negative number). This value is specified by the "-R" switch.
- ///
- /// \throw InvalidParameter if -R<value> is wrong.
- void initClientsNum();
- /// \brief Sets value indicating if interface name was given.
- ///
- /// Method checks if the command line argument given with
- /// '-l' option is the interface name. The is_interface_ member
- /// is set accordingly.
- void initIsInterface();
- /// \brief Decodes base provided with -b<base>.
- ///
- /// Function decodes argument of -b switch, which
- /// specifies a base value used to generate unique
- /// mac or duid values in packets sent to system
- /// under test.
- /// The following forms of switch arguments are supported:
- /// - -b mac=00:01:02:03:04:05
- /// - -b duid=0F1234 (duid can be up to 128 hex digits)
- // Function will decode 00:01:02:03:04:05 and/or
- /// 0F1234 respectively and initialize mac_template_
- /// and/or duid_template_ members.
- ///
- /// \param base Base in string format.
- /// \throws isc::InvalidParameter if base is invalid.
- void decodeBase(const std::string& base);
- /// \brief Decodes base MAC address provided with -b<base>.
- ///
- /// Function decodes parameter given as -b mac=00:01:02:03:04:05
- /// The function will decode 00:01:02:03:04:05 initialize mac_template_
- /// class member.
- /// Provided MAC address is for example only.
- ///
- /// \param base Base string given as -b mac=00:01:02:03:04:05.
- /// \throws isc::InvalidParameter if mac address is invalid.
- void decodeMac(const std::string& base);
- /// \brief Decodes base DUID provided with -b<base>.
- ///
- /// Function decodes parameter given as -b duid=0F1234.
- /// The function will decode 0F1234 and initialize duid_template_
- /// class member.
- /// Provided DUID is for example only.
- ///
- /// \param base Base string given as -b duid=0F1234.
- /// \throws isc::InvalidParameter if DUID is invalid.
- void decodeDuid(const std::string& base);
- /// \brief Generates DUID-LLT (based on link layer address).
- ///
- /// Function generates DUID based on link layer address and
- /// initiates duid_template_ value with it.
- /// \todo add support to generate DUIDs other than based on
- /// 6-octets long MACs (e.g. DUID-UUID.
- void generateDuidTemplate();
- /// \brief Converts two-digit hexadecimal string to a byte.
- ///
- /// \param hex_text Hexadecimal string e.g. AF.
- /// \throw isc::InvalidParameter if string does not represent hex byte.
- uint8_t convertHexString(const std::string& hex_text) const;
- /// IP protocol version to be used, expected values are:
- /// 4 for IPv4 and 6 for IPv6, default value 0 means "not set"
- uint8_t ipversion_;
- /// Packet exchange mode (e.g. DORA/SARR)
- ExchangeMode exchange_mode_;
- /// Rate in exchange per second
- int rate_;
- /// Delay between generation of two consecutive
- /// performance reports
- int report_delay_;
- /// Number of simulated clients (aka randomization range).
- uint32_t clients_num_;
- /// MAC address template used to generate unique MAC
- /// addresses for simulated clients.
- std::vector<uint8_t> mac_template_;
- /// DUID template used to generate unique DUIDs for
- /// simulated clients
- std::vector<uint8_t> duid_template_;
- /// Collection of base values specified with -b<value>
- /// options. Supported "bases" are mac=<mac> and duid=<duid>
- std::vector<std::string> base_;
- /// Number of 2 or 4-way exchanges to perform.
- std::vector<int> num_request_;
- /// Test period in seconds
- int period_;
- /// Indicates number of -d<value> parameters specified by user.
- /// If this value goes above 2, command line parsing fails.
- uint8_t drop_time_set_;
- /// Time to elapse before request is lost. The fisrt value of
- /// two-element vector refers to DO/SA exchanges,
- /// second value refers to RA/RR. Default values are { 1, 1 }
- std::vector<double> drop_time_;
- /// Maximum number of drops request before aborting test.
- /// First value of two-element vector specifies maximum
- /// number of drops for DO/SA exchange, second value
- /// specifies maximum number of drops for RA/RR.
- std::vector<int> max_drop_;
- /// Maximal percentage of lost requests before aborting test.
- /// First value of two-element vector specifies percentage for
- /// DO/SA exchanges, second value for RA/RR.
- std::vector<double> max_pdrop_;
- /// Local address or interface specified with -l<value> option.
- std::string localname_;
- /// Indicates that specified value with -l<value> is
- /// rather interface (not address)
- bool is_interface_;
- /// Number of preload packets. Preload packets are used to
- /// initiate communication with server before doing performance
- /// measurements.
- int preload_;
- /// Number of exchanges sent before next pause.
- int aggressivity_;
- /// Local port number (host endian)
- int local_port_;
- /// Randomization seed.
- uint32_t seed_;
- /// Indicates that randomization seed was provided.
- bool seeded_;
- /// Indicates that we use broadcast address.
- bool broadcast_;
- /// Indicates that we do rapid commit option.
- bool rapid_commit_;
- /// Indicates that we take server id from first received packet.
- bool use_first_;
- /// Packet template file names. These files store template packets
- /// that are used for initiating echanges. Template packets
- /// read from files are later tuned with variable data.
- std::vector<std::string> template_file_;
- /// Offset of transaction id in template files. First vector
- /// element points to offset for DISCOVER/SOLICIT messages,
- /// second element points to trasaction id offset for
- /// REQUEST messages
- std::vector<int> xid_offset_;
- /// Random value offset in templates. Random value offset
- /// points to last octet of DUID. Up to 4 last octets of
- /// DUID are randomized to simulate differnt clients.
- std::vector<int> rnd_offset_;
- /// Offset of elapsed time option in template packet.
- int elp_offset_;
- /// Offset of server id option in template packet.
- int sid_offset_;
- /// Offset of requested ip data in template packet
- int rip_offset_;
- /// String representing diagnostic selectors specified
- /// by user with -x<value>.
- std::string diags_;
- /// Command to be executed at the beginning/end of the test.
- /// This command is expected to expose start and stop argument.
- std::string wrapped_;
- /// Server name specified as last argument of command line.
- std::string server_name_;
- };
- } // namespace perfdhcp
- } // namespace isc
- #endif // COMMAND_OPTIONS_H
|