cloptions.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include "procconf.h"
  18. #include "perfdhcp.h"
  19. #include "cloptions.h"
  20. #include "dkdebug.h"
  21. static void printHelp(const char* progName, const char* usage);
  22. static void initialize(void);
  23. // The current version information
  24. static const char* version = "dhcpperf v1.0 2011-10-30";
  25. const char progName[] = "dhcpperf";
  26. static int v6 = 0; // DHCPv6 operation (-6)
  27. static int initialOnly = 0; // Do only initial exchange (-i)
  28. static unsigned rate = 0; // Request rate (-r)
  29. static unsigned numRequest = 0; // Number of requests (-n)
  30. static double dropTime = 0.0; // Response timeout (-d)
  31. static double testPeriod = 0.0; // Test period (-p)
  32. static const char* server = NULL; // Server to contact
  33. static const char* maxDropOpt = NULL; // Max dropped responses (-D)
  34. static const char* localName = NULL; // Local host/interface (-l)
  35. /*
  36. * Return value:
  37. * 0 if the command has been satisfied and the program should exit 0.
  38. * 2 for usage error, in which case an error message will have been printed.
  39. * 1 if argument processing was successful and the program should continue.
  40. */
  41. int
  42. procArgs(int argc, const char* argv[]) {
  43. char usage[] =
  44. "Usage:\n\
  45. perfdhcp [-hv] [-4|-6] [-r<rate>] [-n<num-request>] [-p<test-period>]\n\
  46. [-d<drop-time>] [-D<max-drop>] [-l<local-addr|interface>] [-i]\n\
  47. [-x<diagnostic-selector>] [server]\n";
  48. int v4 = 0; /* DHCPv4 operation explicitly requested */
  49. const char* msg; /* Failure message from procOpts() */
  50. int help = 0; /* Help requested */
  51. int versionReq = 0; /* Version requested */
  52. const char* diagStr = NULL; /* Diagnostics requested (-x) */
  53. /* option descriptions */
  54. confvar_t optConf[] = {
  55. { 'h', NULL, CF_SWITCH, &help, 1 },
  56. { 'v', NULL, CF_SWITCH, &versionReq, 1 },
  57. { '4', NULL, CF_SWITCH, &v4, 1 },
  58. { '6', NULL, CF_SWITCH, &v6, 1 },
  59. { 'i', NULL, CF_SWITCH, &initialOnly, 1 },
  60. { 'l', NULL, CF_NE_STRING, &localName, 0 },
  61. { 'r', NULL, CF_POS_INT, &rate, 0 },
  62. { 'n', NULL, CF_POS_INT, &numRequest, 0 },
  63. { 'd', NULL, CF_POS_FLOAT, &dropTime, 0 },
  64. { 'p', NULL, CF_POS_FLOAT, &testPeriod, 0 },
  65. { 'D', NULL, CF_NE_STRING, &maxDropOpt, 0 },
  66. { 'x', NULL, CF_STRING, &diagStr, 0 },
  67. { '\0', NULL, CF_ENDLIST, NULL, 0 }
  68. };
  69. /* diagnostic map */
  70. const struct dkdesc diagLetters[] = {
  71. { 's', DK_SOCK },
  72. { 'm', DK_MSG },
  73. { 'p', DK_PACKET },
  74. { 'a', DK_ALL },
  75. { '\0', 0 }
  76. };
  77. initialize();
  78. /* Process command line options */
  79. msg = procOpts(&argc, &argv, optConf, NULL, progName, NULL);
  80. if (msg != NULL) {
  81. fprintf(stderr, "%s: %s\n", progName, msg);
  82. return(2);
  83. }
  84. if (help) {
  85. printHelp(progName, usage);
  86. return(0);
  87. }
  88. if (versionReq) {
  89. printf("%s\n", version);
  90. return(0);
  91. }
  92. if (diagStr != NULL) {
  93. char c;
  94. if ((c = dk_setup(diagStr, diagLetters)) != '\0') {
  95. fprintf(stderr,
  96. "%s: Invalid selector character given with -x: '%c'\n",
  97. progName, c);
  98. return(2);
  99. }
  100. }
  101. if (v4 && v6) {
  102. fprintf(stderr, "%s: Must not give -4 and -6 together.\n", progName);
  103. return(2);
  104. }
  105. switch (argc) {
  106. case 0:
  107. if (v6 && localName != NULL) {
  108. server = "all";
  109. } else {
  110. if (v6) {
  111. fprintf(stderr,
  112. "%s: Use -l to specify an interface name.\n\%s\n",
  113. progName, usage);
  114. }
  115. else {
  116. fprintf(stderr, "%s: Must specify a DHCP server.\n\%s\n",
  117. progName, usage);
  118. }
  119. return(2);
  120. }
  121. break;
  122. case 1:
  123. server = argv[0];
  124. break;
  125. default:
  126. fprintf(stderr, "%s: Too many arguments.\n\%s\n", progName, usage);
  127. return(2);
  128. }
  129. return(1);
  130. }
  131. /*
  132. * Initialize values set by procArgs().
  133. * Initialized though they are static to allow for repeated calls for testing.
  134. */
  135. static void
  136. initialize(void) {
  137. v6 = 0;
  138. initialOnly = 0;
  139. rate = 0;
  140. numRequest = 0;
  141. dropTime = 0.0;
  142. testPeriod = 0.0;
  143. maxDropOpt = NULL;
  144. localName = NULL;
  145. server = NULL;
  146. }
  147. static void
  148. printHelp(const char* progName, const char* usage) {
  149. printf(
  150. "%s: Execute a performance test against a DHCP server.\n\
  151. %s\n\
  152. The [server] argument is the name/address of the DHCP server to contact. \n\
  153. For DHCPv4 operation, exchanges are initiated by transmitting a DHCP\n\
  154. DISCOVER to this address. For DHCPv6 operation, exchanges are initiated\n\
  155. by transmitting a DHCP SOLICIT to this address. In the DHCPv6 case, the\n\
  156. special name \"all\" can be used to refer to\n\
  157. All_DHCP_Relay_Agents_and_Servers (the multicast address FF02::1:2), or\n\
  158. the special name \"servers\" to refer to All_DHCP_Servers (the multicast\n\
  159. address FF05::1:3). The [server] argument is optional only in the case\n\
  160. that -l is used to specify an interface, in which case [server] defaults\n\
  161. to \"all\".\n\
  162. \n\
  163. Exchanges are initiated by transmitting a DHCP SOLICIT to\n\
  164. All_DHCP_Relay_Agents_and_Servers (the multicast address FF02::1:2) via\n\
  165. this interface.\n\
  166. \n\
  167. The default is to perform a single 4-way exchange, effectively pinging the\n\
  168. server. The -r option is used to set up a performance test.\n\
  169. \n\
  170. Options:\n\
  171. -4: DHCPv4 operation (default). This is incompatible with the -6 option.\n\
  172. -6: DHCPv6 operation. This is incompatible with the -4 option.\n\
  173. -h: Print this help.\n\
  174. -i: Do only the initial part of an exchange: DO or SA, depending on\n\
  175. whether -6 is given.\n\
  176. -l<local-addr|interface>: For DHCPv4 operation, specify the local\n\
  177. hostname/address to use when communicating with the server. By\n\
  178. default, the interface address through which traffic would normally be\n\
  179. routed to the server is used.\n\
  180. For DHCPv6 operation, specify the name of the network interface via\n\
  181. which exchanges are initiated. This must be specified unless a server\n\
  182. name is given, in which case the interface through which traffic would\n\
  183. normally be routed to the server is used.\n\
  184. -r<rate>: Initiate <rate> DORA/SARR (or if -i is given, DO/SA) exchanges per\n\
  185. second. A periodic report is generated showing the number of exchanges\n\
  186. which were not completed, as well as the average response latency. The\n\
  187. program continues until interrupted, at which point a final report is\n\
  188. generated.\n\
  189. -v: Report the version number of this program.\n\
  190. -x<diagnostic-selector>: Include extended diagnostics in the output.\n\
  191. <diagnostic-selector> is a string of characters, each specifying an\n\
  192. operation for which verbose output is desired. The selector characters\n\
  193. are:\n\
  194. [TO BE ADDED]\n\
  195. \n\
  196. The remaining options are used only in conjunction with -r:\n\
  197. \n\
  198. -d<drop-time>: Specify the time after which a request is treated as having\n\
  199. been lost. The value is given in seconds and may contain a fractional\n\
  200. component. The default is 1 second.\n\
  201. -D<max-drop>: Abort the test if more than <max-drop> requests have been\n\
  202. dropped. Use -D0 to abort if even a single request has been dropped. \n\
  203. If <max-drop> includes the suffix \"%%\", it specifies a maximum\n\
  204. percentage of requests that may be dropped before abort. In this\n\
  205. case, testing of the threshold begins after 10 requests have been\n\
  206. expected to be received.\n\
  207. -n<num-request>: Initiate <num-request> transactions. No report is\n\
  208. generated until all transactions have been initiated/waited-for, after\n\
  209. which a report is generated and the program terminates.\n\
  210. -p<test-period>: Send requests for the given test period, which is\n\
  211. specified in the same manner as -d. This can be used as an\n\
  212. alternative to -n, or both options can be given, in which case the\n\
  213. testing is completed when either limit is reached.\n\
  214. \n\
  215. Exit status:\n\
  216. The exit status is:\n\
  217. 0 on complete success.\n\
  218. 1 for a general error.\n\
  219. 2 if an error is found in the command line arguments.\n\
  220. 3 if there are no general failures in operation, but one or more exchanges\n\
  221. are not successfully completed.\n",
  222. progName, usage);
  223. }
  224. int
  225. isV6(void) {
  226. return v6;
  227. }
  228. int
  229. getInitialOnly(void) {
  230. return initialOnly;
  231. }
  232. unsigned
  233. getRate(void) {
  234. return rate;
  235. }
  236. unsigned
  237. getNumRequest(void) {
  238. return numRequest;
  239. }
  240. double
  241. getDropTime(void) {
  242. return dropTime;
  243. }
  244. double
  245. getTestPeriod(void) {
  246. return testPeriod;
  247. }
  248. const char *
  249. getServer(void) {
  250. return server;
  251. }
  252. const char *
  253. getLocalName(void) {
  254. return localName;
  255. }
  256. const char *
  257. getMaxDrop(void) {
  258. return maxDropOpt;
  259. }