lib: Move vlog.h to <openvswitch/vlog.h>
[cascardo/ovs.git] / lib / ofp-util.c
index 7655e3e..986659e 100644 (file)
@@ -42,7 +42,7 @@
 #include "random.h"
 #include "unaligned.h"
 #include "type-props.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 #include "bitmap.h"
 
 VLOG_DEFINE_THIS_MODULE(ofp_util);
@@ -108,7 +108,7 @@ ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property,
     return ofputil_pull_property__(msg, property, 8, typep);
 }
 
-static void PRINTF_FORMAT(2, 3)
+static void OVS_PRINTF_FORMAT(2, 3)
 log_property(bool loose, const char *message, ...)
 {
     enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
@@ -1969,7 +1969,7 @@ ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
 
 /* Encode a meter stat for 'mc' and append it to 'replies'. */
 void
-ofputil_append_meter_config(struct list *replies,
+ofputil_append_meter_config(struct ovs_list *replies,
                             const struct ofputil_meter_config *mc)
 {
     struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
@@ -1987,7 +1987,7 @@ ofputil_append_meter_config(struct list *replies,
 
 /* Encode a meter stat for 'ms' and append it to 'replies'. */
 void
-ofputil_append_meter_stats(struct list *replies,
+ofputil_append_meter_stats(struct ovs_list *replies,
                            const struct ofputil_meter_stats *ms)
 {
     struct ofp13_meter_stats *reply;
@@ -2973,7 +2973,7 @@ unknown_to_zero(uint64_t count)
  * have been initialized with ofpmp_init(). */
 void
 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
-                                struct list *replies)
+                                struct ovs_list *replies)
 {
     struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
     size_t start_ofs = ofpbuf_size(reply);
@@ -3527,7 +3527,7 @@ ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
         packet_in_size = sizeof (struct ofp12_packet_in);
     } else {
         packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
-        packet_in_version = OFP13_VERSION;
+        packet_in_version = ofputil_protocol_to_ofp_version(protocol);
         packet_in_size = sizeof (struct ofp13_packet_in);
     }
 
@@ -3547,7 +3547,7 @@ ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
     opi->pi.total_len = htons(pin->total_len);
     opi->pi.reason = pin->reason;
     opi->pi.table_id = pin->table_id;
-    if (protocol == OFPUTIL_P_OF13_OXM) {
+    if (protocol != OFPUTIL_P_OF12_OXM) {
         opi->cookie = pin->cookie;
     }
 
@@ -3606,6 +3606,12 @@ ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
         return "action";
     case OFPR_INVALID_TTL:
         return "invalid_ttl";
+    case OFPR_ACTION_SET:
+        return "action_set";
+    case OFPR_GROUP:
+        return "group";
+    case OFPR_PACKET_OUT:
+        return "packet_out";
 
     case OFPR_N_REASONS:
     default:
@@ -4048,7 +4054,7 @@ ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
 
 void
 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
-                                     struct list *replies)
+                                     struct ovs_list *replies)
 {
     struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
     size_t start_ofs = ofpbuf_size(reply);
@@ -4071,7 +4077,7 @@ BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
 static uint32_t
 ofputil_capabilities_mask(enum ofp_version ofp_version)
 {
-    /* Handle capabilities whose bit is unique for all Open Flow versions */
+    /* Handle capabilities whose bit is unique for all OpenFlow versions */
     switch (ofp_version) {
     case OFP10_VERSION:
     case OFP11_VERSION:
@@ -4869,7 +4875,7 @@ put_table_instruction_features(
 
 void
 ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
-                                    struct list *replies)
+                                    struct ovs_list *replies)
 {
     struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
     enum ofp_version version = ofpmp_version(replies);
@@ -5803,7 +5809,7 @@ ofputil_encode_flow_monitor_cancel(uint32_t id)
 }
 
 void
-ofputil_start_flow_update(struct list *replies)
+ofputil_start_flow_update(struct ovs_list *replies)
 {
     struct ofpbuf *msg;
 
@@ -5816,7 +5822,7 @@ ofputil_start_flow_update(struct list *replies)
 
 void
 ofputil_append_flow_update(const struct ofputil_flow_update *update,
-                           struct list *replies)
+                           struct ovs_list *replies)
 {
     enum ofp_version version = ofpmp_version(replies);
     struct nx_flow_update_header *nfuh;
@@ -6450,7 +6456,7 @@ ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
 }
 
 /* Encode a dump ports request for 'port', the encoded message
- * will be for Open Flow version 'ofp_version'. Returns message
+ * will be for OpenFlow version 'ofp_version'. Returns message
  * as a struct ofpbuf. Returns encoded message on success, NULL on error */
 struct ofpbuf *
 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
@@ -6534,7 +6540,7 @@ ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
 
 static void
 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
-                                struct list *replies)
+                                struct ovs_list *replies)
 {
     struct ofp14_port_stats_prop_ethernet *eth;
     struct ofp14_port_stats *ps14;
@@ -6569,7 +6575,7 @@ ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
 
 /* Encode a ports stat for 'ops' and append it to 'replies'. */
 void
-ofputil_append_port_stat(struct list *replies,
+ofputil_append_port_stat(struct ovs_list *replies,
                          const struct ofputil_port_stats *ops)
 {
     switch (ofpmp_version(replies)) {
@@ -6863,7 +6869,7 @@ ofputil_decode_port_stats_request(const struct ofp_header *request,
 
 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
 void
-ofputil_bucket_list_destroy(struct list *buckets)
+ofputil_bucket_list_destroy(struct ovs_list *buckets)
 {
     struct ofputil_bucket *bucket, *next_bucket;
 
@@ -6874,8 +6880,66 @@ ofputil_bucket_list_destroy(struct list *buckets)
     }
 }
 
+/* Clones 'bucket' and its ofpacts data */
+static struct ofputil_bucket *
+ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
+{
+    struct ofputil_bucket *new;
+
+    new = xmemdup(bucket, sizeof *bucket);
+    new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
+
+    return new;
+}
+
+/* Clones each of the buckets in the list 'src' appending them
+ * in turn to 'dest' which should be an initialised list.
+ * An exception is that if the pointer value of a bucket in 'src'
+ * matches 'skip' then it is not cloned or appended to 'dest'.
+ * This allows all of 'src' or 'all of 'src' except 'skip' to
+ * be cloned and appended to 'dest'. */
+void
+ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
+                          const struct ofputil_bucket *skip)
+{
+    struct ofputil_bucket *bucket;
+
+    LIST_FOR_EACH (bucket, list_node, src) {
+        struct ofputil_bucket *new_bucket;
+
+        if (bucket == skip) {
+            continue;
+        }
+
+        new_bucket = ofputil_bucket_clone_data(bucket);
+        list_push_back(dest, &new_bucket->list_node);
+    }
+}
+
+/* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
+ * Returns the first bucket found or NULL if no buckets are found. */
+struct ofputil_bucket *
+ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
+{
+    struct ofputil_bucket *bucket;
+
+    if (bucket_id > OFPG15_BUCKET_MAX) {
+        return NULL;
+    }
+
+    LIST_FOR_EACH (bucket, list_node, buckets) {
+        if (bucket->bucket_id == bucket_id) {
+            return bucket;
+        }
+    }
+
+    return NULL;
+}
+
+/* Returns true if more than one bucket in the list 'buckets'
+ * have the same bucket id. Returns false otherwise. */
 bool
-ofputil_bucket_check_duplicate_id(const struct list *buckets)
+ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
 {
     struct ofputil_bucket *i, *j;
 
@@ -6893,6 +6957,30 @@ ofputil_bucket_check_duplicate_id(const struct list *buckets)
     return false;
 }
 
+/* Returns the bucket at the front of the list 'buckets'.
+ * Undefined if 'buckets is empty. */
+struct ofputil_bucket *
+ofputil_bucket_list_front(const struct ovs_list *buckets)
+{
+    static struct ofputil_bucket *bucket;
+
+    ASSIGN_CONTAINER(bucket, list_front(buckets), list_node);
+
+    return bucket;
+}
+
+/* Returns the bucket at the back of the list 'buckets'.
+ * Undefined if 'buckets is empty. */
+struct ofputil_bucket *
+ofputil_bucket_list_back(const struct ovs_list *buckets)
+{
+    static struct ofputil_bucket *bucket;
+
+    ASSIGN_CONTAINER(bucket, list_back(buckets), list_node);
+
+    return bucket;
+}
+
 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
  * that requests stats for group 'group_id'.  (Use OFPG_ALL to request stats
  * for all groups.)
@@ -7026,7 +7114,7 @@ ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
  * replies already begun in 'replies' and appends it to the list.  'replies'
  * must have originally been initialized with ofpmp_init(). */
 void
-ofputil_append_group_stats(struct list *replies,
+ofputil_append_group_stats(struct ovs_list *replies,
                            const struct ofputil_group_stats *gs)
 {
     size_t bucket_counter_size;
@@ -7314,14 +7402,14 @@ ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
 
     ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
     ob->len = htons(ofpbuf_size(openflow) - start);
-    ob->actions_len = htons(actions_len);
+    ob->action_array_len = htons(actions_len);
     ob->bucket_id = htonl(bucket_id);
 }
 
 static void
 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
-                                      struct list *buckets,
-                                      struct list *replies,
+                                      struct ovs_list *buckets,
+                                      struct ovs_list *replies,
                                       enum ofp_version version)
 {
     struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
@@ -7344,8 +7432,8 @@ ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
 
 static void
 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
-                                      struct list *buckets,
-                                      struct list *replies,
+                                      struct ovs_list *buckets,
+                                      struct ovs_list *replies,
                                       enum ofp_version version)
 {
     struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
@@ -7374,8 +7462,8 @@ ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
  * initialized with ofpmp_init(). */
 void
 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
-                                struct list *buckets,
-                                struct list *replies)
+                                struct ovs_list *buckets,
+                                struct ovs_list *replies)
 {
     enum ofp_version version = ofpmp_version(replies);
 
@@ -7400,7 +7488,7 @@ ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
 
 static enum ofperr
 ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
-                           enum ofp_version version, struct list *buckets)
+                           enum ofp_version version, struct ovs_list *buckets)
 {
     struct ofp11_bucket *ob;
     uint32_t bucket_id = 0;
@@ -7498,7 +7586,7 @@ parse_ofp15_group_bucket_prop_watch(const struct ofpbuf *payload,
 
 static enum ofperr
 ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
-                           enum ofp_version version, struct list *buckets)
+                           enum ofp_version version, struct ovs_list *buckets)
 {
     struct ofp15_bucket *ob;
 
@@ -7523,7 +7611,7 @@ ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
         }
 
         ob_len = ntohs(ob->len);
-        actions_len = ntohs(ob->actions_len);
+        actions_len = ntohs(ob->action_array_len);
 
         if (ob_len < sizeof *ob) {
             VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
@@ -7812,7 +7900,7 @@ ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
     ogm->type = gm->type;
     ogm->group_id = htonl(gm->group_id);
     ogm->command_bucket_id = htonl(gm->command_bucket_id);
-    ogm->bucket_list_len = htons(ofpbuf_size(b) - start_ogm - sizeof *ogm);
+    ogm->bucket_array_len = htons(ofpbuf_size(b) - start_ogm - sizeof *ogm);
 
     id_pool_destroy(bucket_ids);
     return b;
@@ -7959,7 +8047,7 @@ ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
         return OFPERR_OFPGMFC_BAD_BUCKET;
     }
 
-    bucket_list_len = ntohs(ogm->bucket_list_len);
+    bucket_list_len = ntohs(ogm->bucket_array_len);
     if (bucket_list_len < ofpbuf_size(msg)) {
         VLOG_WARN_RL(&bad_ofmsg_rl, "group has %u trailing bytes",
                      ofpbuf_size(msg) - bucket_list_len);
@@ -8091,9 +8179,9 @@ ofputil_decode_queue_stats_request(const struct ofp_header *request,
     }
 }
 
-/* Encode a queue statsrequest for 'oqsr', the encoded message
- * will be fore Open Flow version 'ofp_version'. Returns message
- * as a struct ofpbuf. Returns encoded message on success, NULL on error */
+/* Encode a queue stats request for 'oqsr', the encoded message
+ * will be for OpenFlow version 'ofp_version'. Returns message
+ * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
 struct ofpbuf *
 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
                                    const struct ofputil_queue_stats_request *oqsr)
@@ -8327,7 +8415,7 @@ ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
 
 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
 void
-ofputil_append_queue_stat(struct list *replies,
+ofputil_append_queue_stat(struct ovs_list *replies,
                           const struct ofputil_queue_stats *oqs)
 {
     switch (ofpmp_version(replies)) {