Add new function ofp_match_to_string() to ofp-print library.
authorBen Pfaff <blp@nicira.com>
Sat, 17 Jan 2009 01:18:20 +0000 (17:18 -0800)
committerBen Pfaff <blp@nicira.com>
Fri, 23 Jan 2009 22:05:43 +0000 (14:05 -0800)
lib/ofp-print.c
lib/ofp-print.h

index 6f989eaed384cba5016fd3acf3a570f5c240e864..430b494a407216aa036cb653bd923fa883576248 100644 (file)
@@ -654,10 +654,18 @@ print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
     ds_put_char(string, ',');
 }
 
-/* Pretty-print the ofp_match structure */
-static void ofp_print_match(struct ds *f, const struct ofp_match *om, 
-        int verbosity)
+static void
+ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
+{
+    char *s = ofp_match_to_string(om, verbosity);
+    ds_put_cstr(f, s);
+    free(s);
+}
+
+char *
+ofp_match_to_string(const struct ofp_match *om, int verbosity)
 {
+    struct ds f = DS_EMPTY_INITIALIZER;
     uint32_t w = ntohl(om->wildcards);
     bool skip_type = false;
     bool skip_proto = false;
@@ -668,55 +676,56 @@ static void ofp_print_match(struct ds *f, const struct ofp_match *om,
             if (!(w & OFPFW_NW_PROTO)) {
                 skip_proto = true;
                 if (om->nw_proto == IP_TYPE_ICMP) {
-                    ds_put_cstr(f, "icmp,");
+                    ds_put_cstr(&f, "icmp,");
                 } else if (om->nw_proto == IP_TYPE_TCP) {
-                    ds_put_cstr(f, "tcp,");
+                    ds_put_cstr(&f, "tcp,");
                 } else if (om->nw_proto == IP_TYPE_UDP) {
-                    ds_put_cstr(f, "udp,");
+                    ds_put_cstr(&f, "udp,");
                 } else {
-                    ds_put_cstr(f, "ip,");
+                    ds_put_cstr(&f, "ip,");
                     skip_proto = false;
                 }
             } else {
-                ds_put_cstr(f, "ip,");
+                ds_put_cstr(&f, "ip,");
             }
         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
-            ds_put_cstr(f, "arp,");
+            ds_put_cstr(&f, "arp,");
         } else {
             skip_type = false;
         }
     }
-    print_wild(f, "in_port=", w & OFPFW_IN_PORT, verbosity,
+    print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
                "%d", ntohs(om->in_port));
-    print_wild(f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
+    print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
                "0x%04x", ntohs(om->dl_vlan));
-    print_wild(f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
+    print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
-    print_wild(f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
+    print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
     if (!skip_type) {
-        print_wild(f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
+        print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
                    "0x%04x", ntohs(om->dl_type));
     }
-    print_ip_netmask(f, "nw_src=", om->nw_src,
+    print_ip_netmask(&f, "nw_src=", om->nw_src,
                      (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
-    print_ip_netmask(f, "nw_dst=", om->nw_dst,
+    print_ip_netmask(&f, "nw_dst=", om->nw_dst,
                      (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
     if (!skip_proto) {
-        print_wild(f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
+        print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
                    "%u", om->nw_proto);
     }
     if (om->nw_proto == IP_TYPE_ICMP) {
-        print_wild(f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
+        print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
                    "%d", ntohs(om->icmp_type));
-        print_wild(f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
+        print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
                    "%d", ntohs(om->icmp_code));
     } else {
-        print_wild(f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
+        print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
                    "%d", ntohs(om->tp_src));
-        print_wild(f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
+        print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
                    "%d", ntohs(om->tp_dst));
     }
+    return ds_cstr(&f);
 }
 
 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
index 0d7e06b652c75a48803083d7d22e409549353c50..d559d9c2750207b3a135b27392952b529727c8cf 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
+/* Copyright (c) 2008, 2009 The Board of Trustees of The Leland Stanford
  * Junior University
  * 
  * We are making the OpenFlow specification and associated documentation
@@ -40,6 +40,7 @@
 #include <stdio.h>
 
 struct ofp_flow_mod;
+struct ofp_match;
 
 #ifdef  __cplusplus
 extern "C" {
@@ -49,6 +50,7 @@ void ofp_print(FILE *, const void *, size_t, int verbosity);
 void ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len);
 
 char *ofp_to_string(const void *, size_t, int verbosity);
+char *ofp_match_to_string(const struct ofp_match *, int verbosity);
 char *ofp_packet_to_string(const void *data, size_t len, size_t total_len);
 char *ofp_message_type_to_string(uint8_t type);