Skip to content

Commit

Permalink
match: Color output of match conditions for ovs-ofctl dump-flows.
Browse files Browse the repository at this point in the history
Add color output for flow match conditions for ovs-ofctl dump-flows
command utility, by inserting color markers in the functions responsible
for printing those match condictions.

Signed-off-by: Quentin Monnet <quentin.monnet@6wind.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
  • Loading branch information
qmonnet authored and blp committed Mar 18, 2016
1 parent ab8e7f7 commit 0deec6d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 56 deletions.
3 changes: 2 additions & 1 deletion lib/flow.c
Expand Up @@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>
#include "byte-order.h"
#include "colors.h"
#include "coverage.h"
#include "csum.h"
#include "dynamic-string.h"
Expand Down Expand Up @@ -984,7 +985,7 @@ format_flags_masked(struct ds *ds, const char *name,
uint32_t mask, uint32_t max_mask)
{
if (name) {
ds_put_format(ds, "%s=", name);
ds_put_format(ds, "%s%s=%s", colors.param, name, colors.end);
}

if (mask == max_mask) {
Expand Down
126 changes: 71 additions & 55 deletions lib/match.c
Expand Up @@ -18,6 +18,7 @@
#include "match.h"
#include <stdlib.h>
#include "byte-order.h"
#include "colors.h"
#include "dynamic-string.h"
#include "ofp-util.h"
#include "packets.h"
Expand Down Expand Up @@ -885,7 +886,7 @@ format_eth_masked(struct ds *s, const char *name,
const struct eth_addr eth, const struct eth_addr mask)
{
if (!eth_addr_is_zero(mask)) {
ds_put_format(s, "%s=", name);
ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
eth_format_masked(eth, &mask, s);
ds_put_char(s, ',');
}
Expand All @@ -896,7 +897,7 @@ format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
ovs_be32 netmask)
{
if (netmask) {
ds_put_format(s, "%s=", name);
ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
ip_format_masked(ip, netmask, s);
ds_put_char(s, ',');
}
Expand All @@ -908,7 +909,7 @@ format_ipv6_netmask(struct ds *s, const char *name,
const struct in6_addr *netmask)
{
if (!ipv6_mask_is_any(netmask)) {
ds_put_format(s, "%s=", name);
ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
ipv6_format_masked(addr, netmask, s);
ds_put_char(s, ',');
}
Expand All @@ -919,7 +920,7 @@ format_uint16_masked(struct ds *s, const char *name,
uint16_t value, uint16_t mask)
{
if (mask != 0) {
ds_put_format(s, "%s=", name);
ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
if (mask == UINT16_MAX) {
ds_put_format(s, "%"PRIu16, value);
} else {
Expand All @@ -934,7 +935,7 @@ format_be16_masked(struct ds *s, const char *name,
ovs_be16 value, ovs_be16 mask)
{
if (mask != htons(0)) {
ds_put_format(s, "%s=", name);
ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
if (mask == OVS_BE16_MAX) {
ds_put_format(s, "%"PRIu16, ntohs(value));
} else {
Expand All @@ -950,7 +951,7 @@ format_be32_masked(struct ds *s, const char *name,
ovs_be32 value, ovs_be32 mask)
{
if (mask != htonl(0)) {
ds_put_format(s, "%s=", name);
ds_put_format(s, "%s%s=%s", colors.param, name, colors.end);
if (mask == OVS_BE32_MAX) {
ds_put_format(s, "%"PRIu32, ntohl(value));
} else {
Expand All @@ -966,7 +967,8 @@ format_uint32_masked(struct ds *s, const char *name,
uint32_t value, uint32_t mask)
{
if (mask) {
ds_put_format(s, "%s=%#"PRIx32, name, value);
ds_put_format(s, "%s%s=%s%#"PRIx32,
colors.param, name, colors.end, value);
if (mask != UINT32_MAX) {
ds_put_format(s, "/%#"PRIx32, mask);
}
Expand All @@ -979,7 +981,8 @@ format_be64_masked(struct ds *s, const char *name,
ovs_be64 value, ovs_be64 mask)
{
if (mask != htonll(0)) {
ds_put_format(s, "%s=%#"PRIx64, name, ntohll(value));
ds_put_format(s, "%s%s=%s%#"PRIx64,
colors.param, name, colors.end, ntohll(value));
if (mask != OVS_BE64_MAX) {
ds_put_format(s, "/%#"PRIx64, ntohll(mask));
}
Expand Down Expand Up @@ -1031,7 +1034,7 @@ format_ct_label_masked(struct ds *s, const ovs_u128 *key, const ovs_u128 *mask)
{
if (!ovs_u128_is_zero(mask)) {
ovs_be128 value = hton128(*key);
ds_put_format(s, "ct_label=");
ds_put_format(s, "%sct_label=%s", colors.param, colors.end);
ds_put_hex(s, &value, sizeof value);
if (!is_all_ones(mask, sizeof(*mask))) {
value = hton128(*mask);
Expand All @@ -1051,14 +1054,16 @@ match_format(const struct match *match, struct ds *s, int priority)
size_t start_len = s->length;
const struct flow *f = &match->flow;
bool skip_type = false;

bool skip_proto = false;

int i;

BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);

if (priority != OFP_DEFAULT_PRIORITY) {
ds_put_format(s, "priority=%d,", priority);
ds_put_format(s, "%spriority=%s%d,",
colors.special, colors.end, priority);
}

format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark);
Expand All @@ -1074,22 +1079,24 @@ match_format(const struct match *match, struct ds *s, int priority)
}

if (wc->masks.conj_id) {
ds_put_format(s, "conj_id=%"PRIu32",", f->conj_id);
ds_put_format(s, "%sconj_id%s=%"PRIu32",",
colors.param, colors.end, f->conj_id);
}

if (wc->masks.skb_priority) {
ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
ds_put_format(s, "%sskb_priority=%s%#"PRIx32",",
colors.param, colors.end, f->skb_priority);
}

if (wc->masks.actset_output) {
ds_put_cstr(s, "actset_output=");
ds_put_format(s, "%sactset_output=%s", colors.param, colors.end);
ofputil_format_port(f->actset_output, s);
ds_put_char(s, ',');
}

if (wc->masks.ct_state) {
if (wc->masks.ct_state == UINT16_MAX) {
ds_put_cstr(s, "ct_state=");
ds_put_format(s, "%sct_state=%s", colors.param, colors.end);
if (f->ct_state) {
format_flags(s, ct_state_to_string, f->ct_state, '|');
} else {
Expand Down Expand Up @@ -1120,48 +1127,48 @@ match_format(const struct match *match, struct ds *s, int priority)
if (wc->masks.nw_proto) {
skip_proto = true;
if (f->nw_proto == IPPROTO_ICMP) {
ds_put_cstr(s, "icmp,");
ds_put_format(s, "%sicmp%s,", colors.value, colors.end);
} else if (f->nw_proto == IPPROTO_IGMP) {
ds_put_cstr(s, "igmp,");
ds_put_format(s, "%sigmp%s,", colors.value, colors.end);
} else if (f->nw_proto == IPPROTO_TCP) {
ds_put_cstr(s, "tcp,");
ds_put_format(s, "%stcp%s,", colors.value, colors.end);
} else if (f->nw_proto == IPPROTO_UDP) {
ds_put_cstr(s, "udp,");
ds_put_format(s, "%sudp%s,", colors.value, colors.end);
} else if (f->nw_proto == IPPROTO_SCTP) {
ds_put_cstr(s, "sctp,");
ds_put_format(s, "%ssctp%s,", colors.value, colors.end);
} else {
ds_put_cstr(s, "ip,");
ds_put_format(s, "%sip%s,", colors.value, colors.end);
skip_proto = false;
}
} else {
ds_put_cstr(s, "ip,");
ds_put_format(s, "%sip%s,", colors.value, colors.end);
}
} else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
if (wc->masks.nw_proto) {
skip_proto = true;
if (f->nw_proto == IPPROTO_ICMPV6) {
ds_put_cstr(s, "icmp6,");
ds_put_format(s, "%sicmp6%s,", colors.value, colors.end);
} else if (f->nw_proto == IPPROTO_TCP) {
ds_put_cstr(s, "tcp6,");
ds_put_format(s, "%stcp6%s,", colors.value, colors.end);
} else if (f->nw_proto == IPPROTO_UDP) {
ds_put_cstr(s, "udp6,");
ds_put_format(s, "%sudp6%s,", colors.value, colors.end);
} else if (f->nw_proto == IPPROTO_SCTP) {
ds_put_cstr(s, "sctp6,");
ds_put_format(s, "%ssctp6%s,", colors.value, colors.end);
} else {
ds_put_cstr(s, "ipv6,");
ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
skip_proto = false;
}
} else {
ds_put_cstr(s, "ipv6,");
ds_put_format(s, "%sipv6%s,", colors.value, colors.end);
}
} else if (f->dl_type == htons(ETH_TYPE_ARP)) {
ds_put_cstr(s, "arp,");
ds_put_format(s, "%sarp%s,", colors.value, colors.end);
} else if (f->dl_type == htons(ETH_TYPE_RARP)) {
ds_put_cstr(s, "rarp,");
ds_put_format(s, "%srarp%s,", colors.value, colors.end);
} else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
ds_put_cstr(s, "mpls,");
ds_put_format(s, "%smpls%s,", colors.value, colors.end);
} else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
ds_put_cstr(s, "mplsm,");
ds_put_format(s, "%smplsm%s,", colors.value, colors.end);
} else {
skip_type = false;
}
Expand All @@ -1180,7 +1187,7 @@ match_format(const struct match *match, struct ds *s, int priority)
format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata);

if (wc->masks.in_port.ofp_port) {
ds_put_cstr(s, "in_port=");
ds_put_format(s, "%sin_port=%s", colors.param, colors.end);
ofputil_format_port(f->in_port.ofp_port, s);
ds_put_char(s, ',');
}
Expand All @@ -1194,35 +1201,39 @@ match_format(const struct match *match, struct ds *s, int priority)
&& (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
&& (vid_mask || pcp_mask)) {
if (vid_mask) {
ds_put_format(s, "dl_vlan=%"PRIu16",",
vlan_tci_to_vid(f->vlan_tci));
ds_put_format(s, "%sdl_vlan=%s%"PRIu16",", colors.param,
colors.end, vlan_tci_to_vid(f->vlan_tci));
}
if (pcp_mask) {
ds_put_format(s, "dl_vlan_pcp=%d,",
vlan_tci_to_pcp(f->vlan_tci));
ds_put_format(s, "%sdl_vlan_pcp=%s%d,", colors.param,
colors.end, vlan_tci_to_pcp(f->vlan_tci));
}
} else if (wc->masks.vlan_tci == htons(0xffff)) {
ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
ds_put_format(s, "%svlan_tci=%s0x%04"PRIx16",", colors.param,
colors.end, ntohs(f->vlan_tci));
} else {
ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
ds_put_format(s, "%svlan_tci=%s0x%04"PRIx16"/0x%04"PRIx16",",
colors.param, colors.end,
ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
}
}
format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
if (!skip_type && wc->masks.dl_type) {
ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
ds_put_format(s, "%sdl_type=%s0x%04"PRIx16",",
colors.param, colors.end, ntohs(f->dl_type));
}
if (f->dl_type == htons(ETH_TYPE_IPV6)) {
format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
if (wc->masks.ipv6_label) {
if (wc->masks.ipv6_label == OVS_BE32_MAX) {
ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32",",
colors.param, colors.end,
ntohl(f->ipv6_label));
} else {
ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
ntohl(f->ipv6_label),
ds_put_format(s, "%sipv6_label=%s0x%05"PRIx32"/0x%05"PRIx32",",
colors.param, colors.end, ntohl(f->ipv6_label),
ntohl(wc->masks.ipv6_label));
}
}
Expand All @@ -1237,9 +1248,11 @@ match_format(const struct match *match, struct ds *s, int priority)
if (!skip_proto && wc->masks.nw_proto) {
if (f->dl_type == htons(ETH_TYPE_ARP) ||
f->dl_type == htons(ETH_TYPE_RARP)) {
ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
ds_put_format(s, "%sarp_op=%s%"PRIu8",",
colors.param, colors.end, f->nw_proto);
} else {
ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
ds_put_format(s, "%snw_proto=%s%"PRIu8",",
colors.param, colors.end, f->nw_proto);
}
}
if (f->dl_type == htons(ETH_TYPE_ARP) ||
Expand All @@ -1248,48 +1261,51 @@ match_format(const struct match *match, struct ds *s, int priority)
format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
}
if (wc->masks.nw_tos & IP_DSCP_MASK) {
ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
ds_put_format(s, "%snw_tos=%s%"PRIu8",",
colors.param, colors.end, f->nw_tos & IP_DSCP_MASK);
}
if (wc->masks.nw_tos & IP_ECN_MASK) {
ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
ds_put_format(s, "%snw_ecn=%s%"PRIu8",",
colors.param, colors.end, f->nw_tos & IP_ECN_MASK);
}
if (wc->masks.nw_ttl) {
ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
ds_put_format(s, "%snw_ttl=%s%"PRIu8",",
colors.param, colors.end, f->nw_ttl);
}
if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) {
ds_put_format(s, "mpls_label=%"PRIu32",",
mpls_lse_to_label(f->mpls_lse[0]));
ds_put_format(s, "%smpls_label=%s%"PRIu32",", colors.param,
colors.end, mpls_lse_to_label(f->mpls_lse[0]));
}
if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) {
ds_put_format(s, "mpls_tc=%"PRIu8",",
ds_put_format(s, "%smpls_tc=%s%"PRIu8",", colors.param, colors.end,
mpls_lse_to_tc(f->mpls_lse[0]));
}
if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) {
ds_put_format(s, "mpls_ttl=%"PRIu8",",
ds_put_format(s, "%smpls_ttl=%s%"PRIu8",", colors.param, colors.end,
mpls_lse_to_ttl(f->mpls_lse[0]));
}
if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) {
ds_put_format(s, "mpls_bos=%"PRIu8",",
ds_put_format(s, "%smpls_bos=%s%"PRIu8",", colors.param, colors.end,
mpls_lse_to_bos(f->mpls_lse[0]));
}
format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]);
format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]);

switch (wc->masks.nw_frag) {
case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
ds_put_format(s, "nw_frag=%s,",
ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
f->nw_frag & FLOW_NW_FRAG_ANY
? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
: (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
break;

case FLOW_NW_FRAG_ANY:
ds_put_format(s, "nw_frag=%s,",
ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
break;

case FLOW_NW_FRAG_LATER:
ds_put_format(s, "nw_frag=%s,",
ds_put_format(s, "%snw_frag=%s%s,", colors.param, colors.end,
f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
break;
}
Expand Down

0 comments on commit 0deec6d

Please sign in to comment.