[ovs-dev] [optimize 04/26] dpif: Include TCP flags in "ovs-dpctl dump-flows" output.

Ben Pfaff blp at nicira.com
Tue Apr 17 00:18:44 UTC 2012


From: Ben Pfaff <blp at hardrock.nicira.com>

Signed-off-by: Ben Pfaff <blp at hardrock.nicira.com>
---
 NEWS                 |    1 +
 lib/dpif.c           |    5 ++++-
 lib/packets.c        |   37 +++++++++++++++++++++++++++++++++++++
 lib/packets.h        |    1 +
 tests/test-netflow.c |   30 +++++-------------------------
 5 files changed, 48 insertions(+), 26 deletions(-)

diff --git a/NEWS b/NEWS
index a466f92..f6bd5b3 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,7 @@ post-v1.6.0
     - Added ability to configure dscp setting for manager and controller
       connections.  By default, these connections have a DSCP value of
       Internetwork Control (0xc0).
+    - "ovs-dpctl dump-flows" now prints observed TCP flags in TCP flows.
 
 
 v1.6.0 - xx xxx xxxx
diff --git a/lib/dpif.c b/lib/dpif.c
index 0199b49..dc42a2d 100644
--- a/lib/dpif.c
+++ b/lib/dpif.c
@@ -697,7 +697,10 @@ dpif_flow_stats_format(const struct dpif_flow_stats *stats, struct ds *s)
     } else {
         ds_put_format(s, "never");
     }
-    /* XXX tcp_flags? */
+    if (stats->tcp_flags) {
+        ds_put_cstr(s, ", flags:");
+        packet_format_tcp_flags(s, stats->tcp_flags);
+    }
 }
 
 /* Deletes all flows from 'dpif'.  Returns 0 if successful, otherwise a
diff --git a/lib/packets.c b/lib/packets.c
index 8fb7f6b..cd9227b 100644
--- a/lib/packets.c
+++ b/lib/packets.c
@@ -496,3 +496,40 @@ packet_get_tcp_flags(const struct ofpbuf *packet, const struct flow *flow)
         return 0;
     }
 }
+
+/* Appends a string representation of the TCP flags value 'tcp_flags'
+ * (e.g. obtained via packet_get_tcp_flags() or TCP_FLAGS) to 's', in the
+ * format used by tcpdump. */
+void
+packet_format_tcp_flags(struct ds *s, uint8_t tcp_flags)
+{
+    if (!tcp_flags) {
+        ds_put_cstr(s, "none");
+        return;
+    }
+
+    if (tcp_flags & TCP_SYN) {
+        ds_put_char(s, 'S');
+    }
+    if (tcp_flags & TCP_FIN) {
+        ds_put_char(s, 'F');
+    }
+    if (tcp_flags & TCP_PSH) {
+        ds_put_char(s, 'P');
+    }
+    if (tcp_flags & TCP_RST) {
+        ds_put_char(s, 'R');
+    }
+    if (tcp_flags & TCP_URG) {
+        ds_put_char(s, 'U');
+    }
+    if (tcp_flags & TCP_ACK) {
+        ds_put_char(s, '.');
+    }
+    if (tcp_flags & 0x40) {
+        ds_put_cstr(s, "[40]");
+    }
+    if (tcp_flags & 0x80) {
+        ds_put_cstr(s, "[80]");
+    }
+}
diff --git a/lib/packets.h b/lib/packets.h
index 34a8b4e..dc71b05 100644
--- a/lib/packets.h
+++ b/lib/packets.h
@@ -470,5 +470,6 @@ void packet_set_tcp_port(struct ofpbuf *, ovs_be16 src, ovs_be16 dst);
 void packet_set_udp_port(struct ofpbuf *, ovs_be16 src, ovs_be16 dst);
 
 uint8_t packet_get_tcp_flags(const struct ofpbuf *, const struct flow *);
+void packet_format_tcp_flags(struct ds *, uint8_t);
 
 #endif /* packets.h */
diff --git a/tests/test-netflow.c b/tests/test-netflow.c
index 49cab49..3717862 100644
--- a/tests/test-netflow.c
+++ b/tests/test-netflow.c
@@ -24,6 +24,7 @@
 
 #include "command-line.h"
 #include "daemon.h"
+#include "dynamic-string.h"
 #include "netflow.h"
 #include "ofpbuf.h"
 #include "packets.h"
@@ -87,31 +88,10 @@ print_netflow(struct ofpbuf *buf)
             printf(", TCP %"PRIu16" > %"PRIu16,
                    ntohs(rec->src_port), ntohs(rec->dst_port));
             if (rec->tcp_flags) {
-                putchar(' ');
-                if (rec->tcp_flags & TCP_SYN) {
-                    putchar('S');
-                }
-                if (rec->tcp_flags & TCP_FIN) {
-                    putchar('F');
-                }
-                if (rec->tcp_flags & TCP_PSH) {
-                    putchar('P');
-                }
-                if (rec->tcp_flags & TCP_RST) {
-                    putchar('R');
-                }
-                if (rec->tcp_flags & TCP_URG) {
-                    putchar('U');
-                }
-                if (rec->tcp_flags & TCP_ACK) {
-                    putchar('.');
-                }
-                if (rec->tcp_flags & 0x40) {
-                    printf("[40]");
-                }
-                if (rec->tcp_flags & 0x80) {
-                    printf("[80]");
-                }
+                struct ds s = DS_EMPTY_INITIALIZER;
+                packet_format_tcp_flags(&s, rec->tcp_flags);
+                printf(" %s", ds_cstr(&s));
+                ds_destroy(&s);
             }
             break;
 
-- 
1.7.9




More information about the dev mailing list