From 7393104d1a31b5b256e491ec689cc7358f101280 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 5 Apr 2012 10:24:56 -0700 Subject: [PATCH] dpif: Include TCP flags in "ovs-dpctl dump-flows" output. Signed-off-by: Ben Pfaff --- NEWS | 2 +- lib/dpif.c | 5 ++++- lib/packets.c | 37 +++++++++++++++++++++++++++++++++++++ lib/packets.h | 1 + tests/test-netflow.c | 30 +++++------------------------- 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/NEWS b/NEWS index ac00335bd..9c7335209 100644 --- a/NEWS +++ b/NEWS @@ -11,7 +11,7 @@ post-v1.6.0 - ovs-test: - Added support for spawning ovs-test server from the client. - Now ovs-test is able to automatically create test bridges and ports. - + - "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 35df1e5d8..e000e13d2 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 8fb7f6b03..cd9227b36 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 34a8b4eaf..dc71b0532 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 7d0288747..5f30c6360 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; -- 2.43.0