ovs-ofctl: Refine documentation of Geneve option mapping.
[cascardo/ovs.git] / utilities / ovs-benchmark.c
index bc28dab..228af6e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2011 Nicira Networks.
+ * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@
 #include "socket-util.h"
 #include "timeval.h"
 #include "util.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 #define DEFAULT_PORT 6630
 
@@ -49,19 +49,31 @@ static double max_rate;
 
 static double timeout;
 
-static const struct command all_commands[];
+static const struct ovs_cmdl_command *get_all_commands(void);
 
 static void parse_options(int argc, char *argv[]);
 static void usage(void);
 
+static int
+do_poll(struct pollfd *fds, int nfds, int timeout)
+{
+    int retval;
+#ifndef _WIN32
+    do {
+        retval = poll(fds, nfds, timeout);
+    } while (retval < 0 && errno == EINTR);
+#else
+    retval = WSAPoll(fds, nfds, timeout);
+#endif
+    return retval;
+}
+
 static long long int
 time_in_msec(void)
 {
     struct timeval tv;
 
-    if (gettimeofday(&tv, NULL) < 0) {
-        ovs_fatal(errno, "gettimeofday");
-    }
+    xgettimeofday(&tv);
 
     return tv.tv_sec * 1000LL + tv.tv_usec / 1000;
 }
@@ -69,10 +81,13 @@ time_in_msec(void)
 int
 main(int argc, char *argv[])
 {
+    struct ovs_cmdl_context ctx = { .argc = 0, };
     set_program_name(argv[0]);
-    vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
+    vlog_set_levels(NULL, VLF_ANY_DESTINATION, VLL_EMER);
     parse_options(argc, argv);
-    run_command(argc - optind, argv + optind, all_commands);
+    ctx.argc = argc - optind;
+    ctx.argv = argv + optind;
+    ovs_cmdl_run_command(&ctx, get_all_commands());
     return 0;
 }
 
@@ -101,11 +116,11 @@ parse_target(const char *s_, struct in_addr *addr,
     *min = *max = 0;
     if (colon && colon[1] != '\0') {
         const char *ports = colon + 1;
-        if (sscanf(ports, "%hu-%hu", min, max) == 2) {
+        if (ovs_scan(ports, "%hu-%hu", min, max)) {
             if (*min > *max) {
                 ovs_fatal(0, "%s: minimum is greater than maximum", s_);
             }
-        } else if (sscanf(ports, "%hu", min) == 1) {
+        } else if (ovs_scan(ports, "%hu", min)) {
             *max = *min;
         } else {
             ovs_fatal(0, "%s: number or range expected", s_);
@@ -118,7 +133,7 @@ parse_target(const char *s_, struct in_addr *addr,
 static void
 parse_options(int argc, char *argv[])
 {
-    static struct option long_options[] = {
+    static const struct option long_options[] = {
         {"local", required_argument, NULL, 'l'},
         {"remote", required_argument, NULL, 'r'},
         {"batches", required_argument, NULL, 'b'},
@@ -129,7 +144,7 @@ parse_options(int argc, char *argv[])
         {"version", no_argument, NULL, 'V'},
         {NULL, 0, NULL, 0},
     };
-    char *short_options = long_options_to_short_options(long_options);
+    char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
 
     local_addr.s_addr = htonl(INADDR_ANY);
     local_min_port = local_max_port = 0;
@@ -232,7 +247,7 @@ Other options:\n\
 }
 
 static void
-cmd_listen(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+cmd_listen(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     struct pollfd *fds;
     int n_fds;
@@ -284,9 +299,7 @@ cmd_listen(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
     for (;;) {
         int retval;
 
-        do {
-            retval = poll(fds, n_fds, -1);
-        } while (retval < 0 && errno == EINTR);
+        retval = do_poll(fds, n_fds, -1);
         if (retval < 0) {
             ovs_fatal(errno, "poll failed");
         }
@@ -360,7 +373,7 @@ bind_local_port(int fd, unsigned short int *local_port,
 }
 
 static void
-cmd_rate(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+cmd_rate(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     unsigned short int local_port;
     unsigned short int remote_port;
@@ -445,9 +458,7 @@ cmd_rate(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
             delay = 1000;
         }
 
-        do {
-            error = poll(fds, n_fds, delay) < 0 ? errno : 0;
-        } while (error == EINTR);
+        error = do_poll(fds, n_fds, delay);
         if (error) {
             ovs_fatal(errno, "poll");
         }
@@ -467,9 +478,9 @@ cmd_rate(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
         }
 
         now = time_in_msec();
-        if (now >= prev + 10) {
+        if (now >= prev + 1000) {
             long long int elapsed = now - start;
-            printf("%.3f s elapsed, %u OK, %u failed, avg %.1f/s     \r",
+            printf("%.3f s elapsed, %u OK, %u failed, avg %.1f/s\n",
                    elapsed / 1000.0, completed - failures, failures,
                    completed / (elapsed / 1000.0));
             fflush(stdout);
@@ -512,7 +523,7 @@ timer_end(long long int start, bool error,
 }
 
 static void
-cmd_latency(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+cmd_latency(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     unsigned short int local_port;
     unsigned short int remote_port;
@@ -578,9 +589,7 @@ cmd_latency(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
         while (n_fds > 0) {
             int error;
 
-            do {
-                error = poll(fds, n_fds, -1) < 0 ? errno : 0;
-            } while (error == EINTR);
+            error = do_poll(fds, n_fds, -1);
             if (error) {
                 ovs_fatal(errno, "poll");
             }
@@ -605,15 +614,20 @@ cmd_latency(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 }
 
 static void
-cmd_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+cmd_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     usage();
 }
 
-static const struct command all_commands[] = {
-    { "listen", 0, 0, cmd_listen },
-    { "rate", 0, 0, cmd_rate },
-    { "latency", 0, 0, cmd_latency },
-    { "help", 0, 0, cmd_help },
-    { NULL, 0, 0, NULL },
+static const struct ovs_cmdl_command all_commands[] = {
+    { "listen", NULL, 0, 0, cmd_listen },
+    { "rate", NULL, 0, 0, cmd_rate },
+    { "latency", NULL, 0, 0, cmd_latency },
+    { "help", NULL, 0, 0, cmd_help },
+    { NULL, NULL, 0, 0, NULL },
 };
+
+static const struct ovs_cmdl_command *get_all_commands(void)
+{
+  return all_commands;
+}