From: Marcin Siodelski Date: Thu, 10 May 2012 10:02:23 +0000 (+0200) Subject: New skeleton class for command options parsing. X-Git-Tag: trac2351_base~226^2~76^2~17 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a7557c12f65bb21145a117df4770863737d4acee;p=thirdparty%2Fkea.git New skeleton class for command options parsing. --- diff --git a/tests/tools/perfdhcp/Makefile.am b/tests/tools/perfdhcp/Makefile.am index bbad595f31..bfcd3b87a6 100644 --- a/tests/tools/perfdhcp/Makefile.am +++ b/tests/tools/perfdhcp/Makefile.am @@ -10,3 +10,4 @@ endif pkglibexec_PROGRAMS = perfdhcp perfdhcp_SOURCES = perfdhcp.c +perfdhcp_SOURCES += command_options.cc command_options.h diff --git a/tests/tools/perfdhcp/command_options.cc b/tests/tools/perfdhcp/command_options.cc new file mode 100644 index 0000000000..0d61d461d5 --- /dev/null +++ b/tests/tools/perfdhcp/command_options.cc @@ -0,0 +1,174 @@ +// Copyright (C) 2011 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. + +#include +#include +#include + +#include "command_options.h" + +using namespace std; +using namespace isc; + +namespace isc { +namespace perfdhcp { + +// Reset stored values to the defaults. +void +CommandOptions::reset() { + ipversion_ = 4; +} + +/// Parses the command-line options and records the results. +void +CommandOptions::parse(int argc, char* const argv[]) { + int ch; + + while((ch = getopt(argc, argv, ":h")) != -1) { + switch (ch) { + case 'h': + default: + usage(); + } + } +} + +void +CommandOptions::usage(void) +{ + fprintf(stderr, "%s", +"perfdhcp [-hv] [-4|-6] [-r] [-t] [-R] [-b]\n" +" [-n] [-p] [-d] [-D]\n" +" [-l] [-P] [-a]\n" +" [-L] [-s] [-i] [-B] [-c] [-1]\n" +" [-T] [-X] [-O] [-S] [-I]\n" +" [-x] [-w] [server]\n" +"\f\n" +"The [server] argument is the name/address of the DHCP server to\n" +"contact. For DHCPv4 operation, exchanges are initiated by\n" +"transmitting a DHCP DISCOVER to this address.\n" +"\n" +"For DHCPv6 operation, exchanges are initiated by transmitting a DHCP\n" +"SOLICIT to this address. In the DHCPv6 case, the special name 'all'\n" +"can be used to refer to All_DHCP_Relay_Agents_and_Servers (the\n" +"multicast address FF02::1:2), or the special name 'servers' to refer\n" +"to All_DHCP_Servers (the multicast address FF05::1:3). The [server]\n" +"argument is optional only in the case that -l is used to specify an\n" +"interface, in which case [server] defaults to 'all'.\n" +"\n" +"The default is to perform a single 4-way exchange, effectively pinging\n" +"the server.\n" +"The -r option is used to set up a performance test, without\n" +"it exchanges are initiated as fast as possible.\n" +"\n" +"Options:\n" +"-1: Take the server-ID option from the first received message.\n" +"-4: DHCPv4 operation (default). This is incompatible with the -6 option.\n" +"-6: DHCPv6 operation. This is incompatible with the -4 option.\n" +"-a: When the target sending rate is not yet reached,\n" +" control how many exchanges are initiated before the next pause.\n" +"-b: The base MAC, DUID, IP, etc, used to simulate different\n" +" clients. This can be specified multiple times, each instance is\n" +" in the = form, for instance:\n" +" (and default) MAC=00:0c:01:02:03:04.\n" +"-d: Specify the time after which a request is treated as\n" +" having been lost. The value is given in seconds and may contain a\n" +" fractional component. The default is 1 second.\n" +"-E: Offset of the (DHCPv4) secs field / (DHCPv6)\n" +" elapsed-time option in the (second/request) template.\n" +" The value 0 disables it.\n" +"-h: Print this help.\n" +"-i: Do only the initial part of an exchange: DO or SA, depending on\n" +" whether -6 is given.\n" +"-I: Offset of the (DHCPv4) IP address in the requested-IP\n" +" option / (DHCPv6) IA_NA option in the (second/request) template.\n" +"-l: For DHCPv4 operation, specify the local\n" +" hostname/address to use when communicating with the server. By\n" +" default, the interface address through which traffic would\n" +" normally be routed to the server is used.\n" +" For DHCPv6 operation, specify the name of the network interface\n" +" via which exchanges are initiated.\n" +"-L: Specify the local port to use\n" +" (the value 0 means to use the default).\n" +"-O: Offset of the last octet to randomize in the template.\n" +"-P: Initiate first exchanges back to back at startup.\n" +"-r: Initiate DORA/SARR (or if -i is given, DO/SA)\n" +" exchanges per second. A periodic report is generated showing the\n" +" number of exchanges which were not completed, as well as the\n" +" average response latency. The program continues until\n" +" interrupted, at which point a final report is generated.\n" +"-R: Specify how many different clients are used. With 1\n" +" (the default), all requests seem to come from the same client.\n" +"-s: Specify the seed for randomization, making it repeatable.\n" +"-S: Offset of the server-ID option in the\n" +" (second/request) template.\n" +"-T: The name of a file containing the template to use\n" +" as a stream of hexadecimal digits.\n" +"-v: Report the version number of this program.\n" +"-w: Command to call with start/stop at the beginning/end of\n" +" the program.\n" +"-x: Include extended diagnostics in the output.\n" +" is a string of single-keywords specifying\n" +" the operations for which verbose output is desired. The selector\n" +" keyletters are:\n" +" * 'a': print the decoded command line arguments\n" +" * 'e': print the exit reason\n" +" * 'i': print rate processing details\n" +" * 'r': print randomization details\n" +" * 's': print first server-id\n" +" * 't': when finished, print timers of all successful exchanges\n" +" * 'T': when finished, print templates\n" +"-X: Transaction ID (aka. xid) offset in the template.\n" +"\n" +"DHCPv4 only options:\n" +"-B: Force broadcast handling.\n" +"\n" +"DHCPv6 only options:\n" +"-c: Add a rapid commit option (exchanges will be SA).\n" +"\n" +"The remaining options are used only in conjunction with -r:\n" +"\n" +"-D: Abort the test if more than requests have\n" +" been dropped. Use -D0 to abort if even a single request has been\n" +" dropped. If includes the suffix '%', it specifies a\n" +" maximum percentage of requests that may be dropped before abort.\n" +" In this case, testing of the threshold begins after 10 requests\n" +" have been expected to be received.\n" +"-n: Initiate transactions. No report is\n" +" generated until all transactions have been initiated/waited-for,\n" +" after which a report is generated and the program terminates.\n" +"-p: Send requests for the given test period, which is\n" +" specified in the same manner as -d. This can be used as an\n" +" alternative to -n, or both options can be given, in which case the\n" +" testing is completed when either limit is reached.\n" +"-t: Delay in seconds between two periodic reports.\n" +"\n" +"Errors:\n" +"- tooshort: received a too short message\n" +"- orphans: received a message which doesn't match an exchange\n" +" (duplicate, late or not related)\n" +"- locallimit: reached to local system limits when sending a message.\n" +"\n" +"Exit status:\n" +"The exit status is:\n" +"0 on complete success.\n" +"1 for a general error.\n" +"2 if an error is found in the command line arguments.\n" +"3 if there are no general failures in operation, but one or more\n" +" exchanges are not successfully completed.\n"); +} + +} // namespace perfdhcp +} // namespace isc diff --git a/tests/tools/perfdhcp/command_options.h b/tests/tools/perfdhcp/command_options.h new file mode 100644 index 0000000000..36bc1a3605 --- /dev/null +++ b/tests/tools/perfdhcp/command_options.h @@ -0,0 +1,67 @@ +// Copyright (C) 2011 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 + +namespace isc { +namespace perfdhcp { + +/// \brief Command Options +/// +/// This class is responsible for parsing the command-line and storing the +/// specified options. +/// +class CommandOptions { +public: + + /// \brief Default Constructor + /// + /// Set values to defaults. + CommandOptions() { + reset(); + } + + /// \brief Reset to defaults + /// + /// Resets the CommandOptions object to default values. + void reset(); + + /// \brief Parse command line + /// + /// Parses the command line and stores the selected options. + /// + /// \param argc Argument count passed to main(). + /// \param argv Argument value array passed to main(). + void parse(int argc, char* const argv[]); + + /// \brief Returns IP version + /// + /// \return IP version to be used + uint8_t getIpVersion() const { return ipversion_; } + + /// \brief Print usage + /// + /// Prints perfdhcp usage + void usage(void); + +private: + uint8_t ipversion_; + +}; + +} // namespace perfdhcp +} // namespace isc + +#endif // __COMMAND_OPTIONS_H