Add function to determine whether the max number of ports are contains
in a Features Reply.  If so, it removes the port list, since it may be
incomplete.  This function will be used in a later commit.

Signed-off-by: Justin Pettit <[email protected]>
---
 lib/ofp-util.c |   37 +++++++++++++++++++++++++++++++++++++
 lib/ofp-util.h |    1 +
 2 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/lib/ofp-util.c b/lib/ofp-util.c
index f4ca110..a24ab59 100644
--- a/lib/ofp-util.c
+++ b/lib/ofp-util.c
@@ -2568,6 +2568,43 @@ ofputil_decode_switch_features(const struct 
ofp_switch_features *osf,
     return 0;
 }
 
+/* Returns true if the maximum number of ports are in 'osf'. */
+static bool
+max_ports_in_features(const struct ofp_switch_features *osf)
+{
+    size_t pp_size = osf->header.version == OFP10_VERSION ?
+                        sizeof(struct ofp10_phy_port) :
+                        sizeof(struct ofp11_port);
+
+    return ntohs(osf->header.length) + pp_size > UINT16_MAX;
+}
+
+/* Given a buffer 'b' that contains a Features Reply message, checks if
+ * it contains the maximum number of ports that will fit.  If so, it
+ * returns true and removes the ports from the message.  The caller
+ * should then send an OFPST_PORT_DESC stats request to get the ports,
+ * since the switch may have more ports than could be represented in the
+ * Features Reply.  Otherwise, returns false.
+ */
+bool
+ofputil_switch_features_ports_trunc(struct ofpbuf *b)
+{
+    struct ofp_switch_features *osf = b->data;
+
+    if (max_ports_in_features(osf)) {
+        /* Remove all the ports. */
+        b->size = sizeof(*osf);
+
+        /* Update the OpenFlow header to indicate we truncated the
+         * packet. */
+        osf->header.length = htons(sizeof(*osf));
+
+        return true;
+    }
+
+    return false;
+}
+
 static ovs_be32
 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
                    const struct ofputil_action_bit_translation *x)
diff --git a/lib/ofp-util.h b/lib/ofp-util.h
index 43bbf31..0a239ce 100644
--- a/lib/ofp-util.h
+++ b/lib/ofp-util.h
@@ -440,6 +440,7 @@ struct ofpbuf *ofputil_encode_switch_features(
     ovs_be32 xid);
 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
                                       struct ofpbuf *);
+bool ofputil_switch_features_ports_trunc(struct ofpbuf *b);
 
 /* phy_port helper functions. */
 int ofputil_pull_phy_port(uint8_t ofp_version, struct ofpbuf *,
-- 
1.7.5.4

_______________________________________________
dev mailing list
[email protected]
http://openvswitch.org/mailman/listinfo/dev

Reply via email to