ofproto: Get rid of archaic "switch status" OpenFlow extension.
[sliver-openvswitch.git] / lib / ofp-util.c
index 4d89e0a..9c894d7 100644 (file)
@@ -29,6 +29,7 @@
 #include "ofpbuf.h"
 #include "packets.h"
 #include "random.h"
+#include "unaligned.h"
 #include "type-props.h"
 #include "vlog.h"
 
@@ -126,7 +127,7 @@ ofputil_cls_rule_from_match(const struct ofp_match *match,
     wc->wildcards = ofpfw & WC_INVARIANTS;
 
     /* Wildcard fields that aren't defined by ofp_match or tun_id. */
-    wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA);
+    wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET);
 
     if (ofpfw & OFPFW_NW_TOS) {
         wc->wildcards |= FWW_NW_TOS;
@@ -398,14 +399,6 @@ ofputil_decode_vendor(const struct ofp_header *oh,
                       const struct ofputil_msg_type **typep)
 {
     static const struct ofputil_msg_type nxt_messages[] = {
-        { OFPUTIL_NXT_STATUS_REQUEST,
-          NXT_STATUS_REQUEST, "NXT_STATUS_REQUEST",
-          sizeof(struct nicira_header), 1 },
-
-        { OFPUTIL_NXT_STATUS_REPLY,
-          NXT_STATUS_REPLY, "NXT_STATUS_REPLY",
-          sizeof(struct nicira_header), 1 },
-
         { OFPUTIL_NXT_TUN_ID_FROM_COOKIE,
           NXT_TUN_ID_FROM_COOKIE, "NXT_TUN_ID_FROM_COOKIE",
           sizeof(struct nxt_tun_id_cookie), 0 },
@@ -856,7 +849,8 @@ is_nxm_required(const struct cls_rule *rule, bool cookie_support,
                 ovs_be64 cookie)
 {
     const struct flow_wildcards *wc = &rule->wc;
-    ovs_be32 cookie_hi;
+    uint32_t cookie_hi;
+    uint64_t tun_id;
 
     /* Only NXM supports separately wildcards the Ethernet multicast bit. */
     if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
@@ -885,11 +879,21 @@ is_nxm_required(const struct cls_rule *rule, bool cookie_support,
         break;
 
     case CONSTANT_HTONLL(UINT64_MAX):
-        /* Only NXM supports matching tunnel ID, unless there is a cookie and
-         * the top 32 bits of the cookie are the desired tunnel ID value. */
-        cookie_hi = htonl(ntohll(cookie) >> 32);
-        if (!cookie_support
-            || (cookie_hi && cookie_hi != ntohll(rule->flow.tun_id))) {
+        /* Only NXM supports tunnel ID matching without a cookie. */
+        if (!cookie_support) {
+            return true;
+        }
+
+        /* Only NXM supports 64-bit tunnel IDs. */
+        tun_id = ntohll(rule->flow.tun_id);
+        if (tun_id > UINT32_MAX) {
+            return true;
+        }
+
+        /* Only NXM supports a cookie whose top 32 bits conflict with the
+         * tunnel ID. */
+        cookie_hi = ntohll(cookie) >> 32;
+        if (cookie_hi && cookie_hi != tun_id) {
             return true;
         }
         break;
@@ -1146,7 +1150,7 @@ ofputil_decode_nxst_flow_request(struct flow_stats_request *fsr,
 }
 
 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
- * message 'oh', received when the current flow format was 'flow_format', into
+ * request 'oh', received when the current flow format was 'flow_format', into
  * an abstract flow_stats_request in 'fsr'.  Returns 0 if successful, otherwise
  * an OpenFlow error code.
  *
@@ -1186,7 +1190,7 @@ ofputil_decode_flow_stats_request(struct flow_stats_request *fsr,
 }
 
 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
- * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE message 'oh' according to
+ * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
  * 'flow_format', and returns the message. */
 struct ofpbuf *
 ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
@@ -1228,6 +1232,117 @@ ofputil_encode_flow_stats_request(const struct flow_stats_request *fsr,
     return msg;
 }
 
+/* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
+ * ofputil_flow_stats in 'fs'.  For OFPST_FLOW messages, 'flow_format' should
+ * be the current flow format at the time when the request corresponding to the
+ * reply in 'msg' was sent.  Otherwise 'flow_format' is ignored.
+ *
+ * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
+ * OpenFlow message.  Calling this function multiple times for a single 'msg'
+ * iterates through the replies.  The caller must initially leave 'msg''s layer
+ * pointers null and not modify them between calls.
+ *
+ * Returns 0 if successful, EOF if no replies were left in this 'msg',
+ * otherwise a positive errno value. */
+int
+ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
+                                struct ofpbuf *msg,
+                                enum nx_flow_format flow_format)
+{
+    const struct ofputil_msg_type *type;
+    int code;
+
+    ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
+    code = ofputil_msg_type_code(type);
+    if (!msg->l2) {
+        msg->l2 = msg->data;
+        if (code == OFPUTIL_OFPST_FLOW_REPLY) {
+            ofpbuf_pull(msg, sizeof(struct ofp_stats_reply));
+        } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
+            ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
+        } else {
+            NOT_REACHED();
+        }
+    }
+
+    if (!msg->size) {
+        return EOF;
+    } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
+        const struct ofp_flow_stats *ofs;
+        size_t length;
+
+        ofs = ofpbuf_try_pull(msg, sizeof *ofs);
+        if (!ofs) {
+            VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
+                         "bytes at end", msg->size);
+            return EINVAL;
+        }
+
+        length = ntohs(ofs->length);
+        if (length < sizeof *ofs) {
+            VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
+                         "length %zu", length);
+            return EINVAL;
+        }
+
+        if (ofputil_pull_actions(msg, length - sizeof *ofs,
+                                 &fs->actions, &fs->n_actions)) {
+            return EINVAL;
+        }
+
+        fs->cookie = get_32aligned_be64(&ofs->cookie);
+        ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
+                                    flow_format, fs->cookie, &fs->rule);
+        fs->table_id = ofs->table_id;
+        fs->duration_sec = ntohl(ofs->duration_sec);
+        fs->duration_nsec = ntohl(ofs->duration_nsec);
+        fs->idle_timeout = ntohs(ofs->idle_timeout);
+        fs->hard_timeout = ntohs(ofs->hard_timeout);
+        fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
+        fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
+    } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
+        const struct nx_flow_stats *nfs;
+        size_t match_len, length;
+
+        nfs = ofpbuf_try_pull(msg, sizeof *nfs);
+        if (!nfs) {
+            VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
+                         "bytes at end", msg->size);
+            return EINVAL;
+        }
+
+        length = ntohs(nfs->length);
+        match_len = ntohs(nfs->match_len);
+        if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
+            VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
+                         "claims invalid length %zu", match_len, length);
+            return EINVAL;
+        }
+        if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
+            return EINVAL;
+        }
+
+        if (ofputil_pull_actions(msg,
+                                 length - sizeof *nfs - ROUND_UP(match_len, 8),
+                                 &fs->actions, &fs->n_actions)) {
+            return EINVAL;
+        }
+
+        fs->cookie = nfs->cookie;
+        fs->table_id = nfs->table_id;
+        fs->duration_sec = ntohl(nfs->duration_sec);
+        fs->duration_nsec = ntohl(nfs->duration_nsec);
+        fs->idle_timeout = ntohs(nfs->idle_timeout);
+        fs->hard_timeout = ntohs(nfs->hard_timeout);
+        fs->packet_count = ntohll(nfs->packet_count);
+        fs->byte_count = ntohll(nfs->byte_count);
+    } else {
+        NOT_REACHED();
+    }
+
+    return 0;
+}
+
 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh', received
  * when the current flow format was 'flow_format', into an abstract
  * ofputil_flow_removed in 'fr'.  Returns 0 if successful, otherwise an