[ovs-dev] [PATCH v1 5/6] ovs-dpctl: Make 'show_dpif' more general.

Gurucharan Shetty shettyg at nicira.com
Fri Nov 1 09:15:16 UTC 2013


Currently, with 'ovs-dpctl show', we call show_dpif().
Move this functionality to lib/dpif.c so that it can
be used by an upcoming commit.

Signed-off-by: Gurucharan Shetty <gshetty at nicira.com>
---
 lib/dpif.c            |  136 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/dpif.h            |    1 +
 utilities/ovs-dpctl.c |  141 +++----------------------------------------------
 3 files changed, 145 insertions(+), 133 deletions(-)

diff --git a/lib/dpif.c b/lib/dpif.c
index b284e13..6275570 100644
--- a/lib/dpif.c
+++ b/lib/dpif.c
@@ -37,6 +37,7 @@
 #include "packets.h"
 #include "poll-loop.h"
 #include "shash.h"
+#include "smap.h"
 #include "sset.h"
 #include "timeval.h"
 #include "util.h"
@@ -474,6 +475,141 @@ dpif_get_dp_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
     return error;
 }
 
+static void
+print_stat(struct ds *ds, const char *leader, uint64_t value)
+{
+    ds_put_cstr(ds, leader);
+    if (value != UINT64_MAX) {
+        ds_put_format(ds, "%"PRIu64, value);
+    } else {
+        ds_put_cstr(ds, "?");
+    }
+}
+
+static void
+print_human_size(struct ds *ds, uint64_t value)
+{
+    if (value == UINT64_MAX) {
+        /* Nothing to do. */
+    } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
+        ds_put_format(ds, " (%.1f TiB)",
+                      value / (1024.0 * 1024 * 1024 * 1024));
+    } else if (value >= 1024ULL * 1024 * 1024) {
+        ds_put_format(ds, " (%.1f GiB)", value / (1024.0 * 1024 * 1024));
+    } else if (value >= 1024ULL * 1024) {
+        ds_put_format(ds, " (%.1f MiB)", value / (1024.0 * 1024));
+    } else if (value >= 1024) {
+        ds_put_format(ds, " (%.1f KiB)", value / 1024.0);
+    }
+}
+
+void
+dpif_show(struct dpif *dpif, struct ds *ds, bool print_statistics)
+{
+    struct dpif_port_dump dump;
+    struct dpif_port dpif_port;
+    struct dpif_dp_stats stats;
+    struct netdev *netdev;
+
+    ds_put_format(ds, "%s:\n", dpif_name(dpif));
+    if (!dpif_get_dp_stats(dpif, &stats)) {
+        ds_put_format(ds, "\tlookups: hit:%"PRIu64" missed:%"PRIu64""
+                      " lost:%"PRIu64"\n\tflows: %"PRIu64"\n",
+                      stats.n_hit, stats.n_missed, stats.n_lost,
+                      stats.n_flows);
+        if (stats.n_masks != UINT64_MAX) {
+            uint64_t n_pkts = stats.n_hit + stats.n_missed;
+            double avg = n_pkts ? (double) stats.n_mask_hit / n_pkts : 0.0;
+
+            ds_put_format(ds, "\tmasks: hit:%"PRIu64" total:%"PRIu64" "
+                         "hit/pkt:%.2f\n",
+                         stats.n_mask_hit, stats.n_masks, avg);
+        }
+    }
+
+    DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
+        ds_put_format(ds, "\tport %u: %s", dpif_port.port_no, dpif_port.name);
+
+        if (strcmp(dpif_port.type, "system")) {
+            int error;
+
+            ds_put_format(ds, " (%s", dpif_port.type);
+
+            error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
+            if (!error) {
+                struct smap config;
+
+                smap_init(&config);
+                error = netdev_get_config(netdev, &config);
+                if (!error) {
+                    const struct smap_node **nodes;
+                    size_t i;
+
+                    nodes = smap_sort(&config);
+                    for (i = 0; i < smap_count(&config); i++) {
+                        const struct smap_node *node = nodes[i];
+                        ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
+                                      node->key, node->value);
+                    }
+                    free(nodes);
+                } else {
+                    ds_put_format(ds, ", could not retrieve configuration"
+                                 " (%s)", ovs_strerror(error));
+                }
+                smap_destroy(&config);
+
+                netdev_close(netdev);
+            } else {
+                ds_put_format(ds, ": open failed (%s)", ovs_strerror(error));
+            }
+            ds_put_cstr(ds, ")");
+        }
+        ds_put_cstr(ds, "\n");
+
+        if (print_statistics) {
+            struct netdev_stats s;
+            int error;
+
+            error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
+            if (error) {
+                ds_put_format(ds, ", open failed (%s)", ovs_strerror(error));
+                continue;
+            }
+            error = netdev_get_stats(netdev, &s);
+            if (error) {
+                ds_put_format(ds, ", could not retrieve stats (%s)",
+                              ovs_strerror(error));
+                continue;
+            }
+
+            netdev_close(netdev);
+            print_stat(ds, "\t\tRX packets:", s.rx_packets);
+            print_stat(ds, " errors:", s.rx_errors);
+            print_stat(ds, " dropped:", s.rx_dropped);
+            print_stat(ds, " overruns:", s.rx_over_errors);
+            print_stat(ds, " frame:", s.rx_frame_errors);
+            ds_put_cstr(ds, "\n");
+
+            print_stat(ds, "\t\tTX packets:", s.tx_packets);
+            print_stat(ds, " errors:", s.tx_errors);
+            print_stat(ds, " dropped:", s.tx_dropped);
+            print_stat(ds, " aborted:", s.tx_aborted_errors);
+            print_stat(ds, " carrier:", s.tx_carrier_errors);
+            ds_put_cstr(ds, "\n");
+
+            print_stat(ds, "\t\tcollisions:", s.collisions);
+            ds_put_cstr(ds, "\n");
+
+            print_stat(ds, "\t\tRX bytes:", s.rx_bytes);
+            print_human_size(ds, s.rx_bytes);
+            print_stat(ds, "  TX bytes:", s.tx_bytes);
+            print_human_size(ds, s.tx_bytes);
+            ds_put_cstr(ds, "\n");
+        }
+    }
+    dpif_close(dpif);
+}
+
 const char *
 dpif_port_open_type(const char *datapath_type, const char *port_type)
 {
diff --git a/lib/dpif.h b/lib/dpif.h
index de7450a..1bbc0d7 100644
--- a/lib/dpif.h
+++ b/lib/dpif.h
@@ -396,6 +396,7 @@ int dpif_get_dp_stats(const struct dpif *, struct dpif_dp_stats *);
 
 /* Port operations. */
 
+void dpif_show(struct dpif *, struct ds *, bool print_statistics);
 const char *dpif_port_open_type(const char *datapath_type,
                                 const char *port_type);
 int dpif_port_add(struct dpif *, struct netdev *, odp_port_t *port_nop);
diff --git a/utilities/ovs-dpctl.c b/utilities/ovs-dpctl.c
index 78475e7..526a158 100644
--- a/utilities/ovs-dpctl.c
+++ b/utilities/ovs-dpctl.c
@@ -524,140 +524,12 @@ dpctl_del_if(int argc OVS_UNUSED, char *argv[])
 }
 
 static void
-print_stat(const char *leader, uint64_t value)
-{
-    fputs(leader, stdout);
-    if (value != UINT64_MAX) {
-        printf("%"PRIu64, value);
-    } else {
-        putchar('?');
-    }
-}
-
-static void
-print_human_size(uint64_t value)
-{
-    if (value == UINT64_MAX) {
-        /* Nothing to do. */
-    } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
-        printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
-    } else if (value >= 1024ULL * 1024 * 1024) {
-        printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
-    } else if (value >= 1024ULL * 1024) {
-        printf(" (%.1f MiB)", value / (1024.0 * 1024));
-    } else if (value >= 1024) {
-        printf(" (%.1f KiB)", value / 1024.0);
-    }
-}
-
-static void
-show_dpif(struct dpif *dpif)
-{
-    struct dpif_port_dump dump;
-    struct dpif_port dpif_port;
-    struct dpif_dp_stats stats;
-    struct netdev *netdev;
-
-    printf("%s:\n", dpif_name(dpif));
-    if (!dpif_get_dp_stats(dpif, &stats)) {
-        printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
-               "\tflows: %"PRIu64"\n",
-               stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
-        if (stats.n_masks != UINT64_MAX) {
-            uint64_t n_pkts = stats.n_hit + stats.n_missed;
-            double avg = n_pkts ? (double) stats.n_mask_hit / n_pkts : 0.0;
-
-            printf("\tmasks: hit:%"PRIu64" total:%"PRIu64" hit/pkt:%.2f\n",
-                   stats.n_mask_hit, stats.n_masks, avg);
-        }
-    }
-
-    DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
-        printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
-
-        if (strcmp(dpif_port.type, "system")) {
-            int error;
-
-            printf (" (%s", dpif_port.type);
-
-            error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
-            if (!error) {
-                struct smap config;
-
-                smap_init(&config);
-                error = netdev_get_config(netdev, &config);
-                if (!error) {
-                    const struct smap_node **nodes;
-                    size_t i;
-
-                    nodes = smap_sort(&config);
-                    for (i = 0; i < smap_count(&config); i++) {
-                        const struct smap_node *node = nodes[i];
-                        printf("%c %s=%s", i ? ',' : ':', node->key,
-                               node->value);
-                    }
-                    free(nodes);
-                } else {
-                    printf(", could not retrieve configuration (%s)",
-                           ovs_strerror(error));
-                }
-                smap_destroy(&config);
-
-                netdev_close(netdev);
-            } else {
-                printf(": open failed (%s)", ovs_strerror(error));
-            }
-            putchar(')');
-        }
-        putchar('\n');
-
-        if (print_statistics) {
-            struct netdev_stats s;
-            int error;
-
-            error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
-            if (error) {
-                printf(", open failed (%s)", ovs_strerror(error));
-                continue;
-            }
-            error = netdev_get_stats(netdev, &s);
-            if (error) {
-                printf(", could not retrieve stats (%s)", ovs_strerror(error));
-                continue;
-            }
-
-            netdev_close(netdev);
-            print_stat("\t\tRX packets:", s.rx_packets);
-            print_stat(" errors:", s.rx_errors);
-            print_stat(" dropped:", s.rx_dropped);
-            print_stat(" overruns:", s.rx_over_errors);
-            print_stat(" frame:", s.rx_frame_errors);
-            printf("\n");
-
-            print_stat("\t\tTX packets:", s.tx_packets);
-            print_stat(" errors:", s.tx_errors);
-            print_stat(" dropped:", s.tx_dropped);
-            print_stat(" aborted:", s.tx_aborted_errors);
-            print_stat(" carrier:", s.tx_carrier_errors);
-            printf("\n");
-
-            print_stat("\t\tcollisions:", s.collisions);
-            printf("\n");
-
-            print_stat("\t\tRX bytes:", s.rx_bytes);
-            print_human_size(s.rx_bytes);
-            print_stat("  TX bytes:", s.tx_bytes);
-            print_human_size(s.tx_bytes);
-            printf("\n");
-        }
-    }
-    dpif_close(dpif);
-}
-
-static void
 dpctl_show(int argc, char *argv[])
 {
     bool failure = false;
+    struct ds ds;
+
+    ds_init(&ds);
     if (argc > 1) {
         int i;
         for (i = 1; i < argc; i++) {
@@ -667,7 +539,8 @@ dpctl_show(int argc, char *argv[])
 
             error = parsed_dpif_open(name, false, &dpif);
             if (!error) {
-                show_dpif(dpif);
+                dpif_show(dpif, &ds, print_statistics);
+                printf("%s\n", ds_cstr(&ds));
             } else {
                 ovs_error(error, "opening datapath %s failed", name);
                 failure = true;
@@ -694,7 +567,8 @@ dpctl_show(int argc, char *argv[])
 
                 error = dpif_open(name, type, &dpif);
                 if (!error) {
-                    show_dpif(dpif);
+                    dpif_show(dpif, &ds, print_statistics);
+                    printf("%s\n", ds_cstr(&ds));
                 } else {
                     ovs_error(error, "opening datapath %s failed", name);
                     failure = true;
@@ -704,6 +578,7 @@ dpctl_show(int argc, char *argv[])
         }
         sset_destroy(&types);
     }
+    ds_destroy(&ds);
     if (failure) {
         exit(EXIT_FAILURE);
     }
-- 
1.7.9.5




More information about the dev mailing list