ofp-util: New functions for string versions of ofp_packet_in_reason.
authorBen Pfaff <blp@nicira.com>
Tue, 7 Feb 2012 22:46:34 +0000 (14:46 -0800)
committerBen Pfaff <blp@nicira.com>
Mon, 27 Feb 2012 21:21:43 +0000 (13:21 -0800)
Upcoming commits add a user for ofputil_packet_in_reason_from_string()
and more users for ofputil_packet_in_reason_to_string().

Signed-off-by: Ben Pfaff <blp@nicira.com>
include/openflow/openflow.h
lib/ofp-print.c
lib/ofp-util.c
lib/ofp-util.h

index a707087b95ef8012d81bdfd6a99fcccdb00c4385..cfdb192375064580abfa217e69ec03afbdc77122 100644 (file)
@@ -294,7 +294,8 @@ OFP_ASSERT(sizeof(struct ofp_port_mod) == 32);
 enum ofp_packet_in_reason {
     OFPR_NO_MATCH,          /* No matching flow. */
     OFPR_ACTION,            /* Action explicitly output to controller. */
-    OFPR_INVALID_TTL        /* Packet has invalid TTL. */
+    OFPR_INVALID_TTL,       /* Packet has invalid TTL. */
+    OFPR_N_REASONS
 };
 
 /* Packet received on port (datapath -> controller). */
index f1eeebb44aa98b735128b3ffc4c13270839b9999..5f55da3b7560d9338925a5c7d00043caab053b0b 100644 (file)
@@ -80,24 +80,6 @@ ofp_packet_to_string(const void *data, size_t len)
     return ds_cstr(&ds);
 }
 
-static const char *
-ofp_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
-{
-    static char s[32];
-
-    switch (reason) {
-    case OFPR_NO_MATCH:
-        return "no_match";
-    case OFPR_ACTION:
-        return "action";
-    case OFPR_INVALID_TTL:
-        return "invalid_ttl";
-    default:
-        sprintf(s, "%d", (int) reason);
-        return s;
-    }
-}
-
 static void
 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
                     int verbosity)
@@ -140,7 +122,7 @@ ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
     }
 
     ds_put_format(string, " (via %s)",
-                  ofp_packet_in_reason_to_string(pin.reason));
+                  ofputil_packet_in_reason_to_string(pin.reason));
 
     ds_put_format(string, " data_len=%zu", pin.packet_len);
     if (pin.buffer_id == UINT32_MAX) {
@@ -1376,7 +1358,7 @@ ofp_print_nxt_set_async_config(struct ds *string,
         for (j = 0; j < 32; j++) {
             if (nac->packet_in_mask[i] & htonl(1u << j)) {
                 ds_put_format(string, " %s",
-                              ofp_packet_in_reason_to_string(j));
+                              ofputil_packet_in_reason_to_string(j));
             }
         }
         if (!nac->packet_in_mask[i]) {
index ffdccda43ef1f0d5f7de19dbbe0fc6641137ae34..f9f4249047413e6181ac247a33b838b4523f51c7 100644 (file)
@@ -1784,6 +1784,41 @@ ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
     return packet;
 }
 
+const char *
+ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
+{
+    static char s[INT_STRLEN(int) + 1];
+
+    switch (reason) {
+    case OFPR_NO_MATCH:
+        return "no_match";
+    case OFPR_ACTION:
+        return "action";
+    case OFPR_INVALID_TTL:
+        return "invalid_ttl";
+
+    case OFPR_N_REASONS:
+    default:
+        sprintf(s, "%d", (int) reason);
+        return s;
+    }
+}
+
+bool
+ofputil_packet_in_reason_from_string(const char *s,
+                                     enum ofp_packet_in_reason *reason)
+{
+    int i;
+
+    for (i = 0; i < OFPR_N_REASONS; i++) {
+        if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
+            *reason = i;
+            return true;
+        }
+    }
+    return false;
+}
+
 enum ofperr
 ofputil_decode_packet_out(struct ofputil_packet_out *po,
                           const struct ofp_packet_out *opo)
index 59d6b1abf9e686f763b55ffc5c640fbc0caf2a5e..6da652770680fee4322bfcdc9ba86a1d5f28144c 100644 (file)
@@ -248,6 +248,10 @@ int ofputil_decode_packet_in(struct ofputil_packet_in *,
 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
                                         enum nx_packet_in_format);
 
+const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason);
+bool ofputil_packet_in_reason_from_string(const char *,
+                                          enum ofp_packet_in_reason *);
+
 /* Abstract packet-out message. */
 struct ofputil_packet_out {
     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */