Introduce ofpacts, an abstraction of OpenFlow actions.
[cascardo/ovs.git] / tests / test-bundle.c
index 672c426..f2d9b82 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2011 Nicira, Inc.
+/* Copyright (c) 2011, 2012 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 
 #include "flow.h"
+#include "ofp-actions.h"
 #include "ofpbuf.h"
 #include "random.h"
 
@@ -64,22 +65,24 @@ slave_enabled_cb(uint16_t slave_id, void *aux)
     return slave ? slave->enabled : false;
 }
 
-static struct nx_action_bundle *
+static struct ofpact_bundle *
 parse_bundle_actions(char *actions)
 {
-    struct nx_action_bundle *nab;
-    struct ofpbuf b;
+    struct ofpact_bundle *bundle;
+    struct ofpbuf ofpacts;
+    struct ofpact *action;
 
-    ofpbuf_init(&b, 0);
-    bundle_parse_load(&b, actions);
-    nab = ofpbuf_steal_data(&b);
-    ofpbuf_uninit(&b);
+    ofpbuf_init(&ofpacts, 0);
+    bundle_parse_load(actions, &ofpacts);
+    action = ofpacts.data;
+    bundle = ofpact_get_BUNDLE(xmemdup(action, action->len));
+    ofpbuf_uninit(&ofpacts);
 
-    if (ntohs(nab->n_slaves) > MAX_SLAVES) {
+    if (bundle->n_slaves > MAX_SLAVES) {
         ovs_fatal(0, "At most %u slaves are supported", MAX_SLAVES);
     }
 
-    return nab;
+    return bundle;
 }
 
 static const char *
@@ -101,7 +104,7 @@ int
 main(int argc, char *argv[])
 {
     bool ok = true;
-    struct nx_action_bundle *nab;
+    struct ofpact_bundle *bundle;
     struct flow *flows;
     size_t i, n_permute, old_n_enabled;
     struct slave_group sg;
@@ -114,12 +117,12 @@ main(int argc, char *argv[])
         ovs_fatal(0, "usage: %s bundle_action", program_name);
     }
 
-    nab = parse_bundle_actions(argv[1]);
+    bundle = parse_bundle_actions(argv[1]);
 
     /* Generate 'slaves' array. */
     sg.n_slaves = 0;
-    for (i = 0; i < ntohs(nab->n_slaves); i++) {
-        uint16_t slave_id = bundle_get_slave(nab, i);
+    for (i = 0; i < bundle->n_slaves; i++) {
+        uint16_t slave_id = bundle->slaves[i];
 
         if (slave_lookup(&sg, slave_id)) {
             ovs_fatal(0, "Redundant slaves are not supported. ");
@@ -136,10 +139,6 @@ main(int argc, char *argv[])
         flows[i].regs[0] = OFPP_NONE;
     }
 
-    if (bundle_check(nab, 1024, flows)) {
-        ovs_fatal(0, "Bundle action fails to check.");
-    }
-
     /* Cycles through each possible liveness permutation for the given
      * n_slaves.  The initial state is equivalent to all slaves down, so we
      * skip it by starting at i = 1. We do one extra iteration to cover
@@ -188,23 +187,19 @@ main(int argc, char *argv[])
             uint16_t old_slave_id, ofp_port;
 
             old_slave_id = flow->regs[0];
-            ofp_port = bundle_execute(nab, flow, slave_enabled_cb, &sg);
-            bundle_execute_load(nab, flow, slave_enabled_cb, &sg);
-            if (flow->regs[0] != ofp_port) {
-                ovs_fatal(0, "bundle_execute_load() and bundle_execute() "
-                          "disagree");
-            }
+            ofp_port = bundle_execute(bundle, flow, slave_enabled_cb, &sg);
+            flow->regs[0] = ofp_port;
 
-            if (flow->regs[0] != OFPP_NONE) {
-                slave_lookup(&sg, flow->regs[0])->flow_count++;
+            if (ofp_port != OFPP_NONE) {
+                slave_lookup(&sg, ofp_port)->flow_count++;
             }
 
-            if (old_slave_id != flow->regs[0]) {
+            if (old_slave_id != ofp_port) {
                 changed++;
             }
         }
 
-        if (nab->algorithm == htons(NX_BD_ALG_ACTIVE_BACKUP)) {
+        if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
             perfect = active == old_active ? 0.0 : 1.0;
         } else {
             if (old_n_enabled || n_enabled) {
@@ -229,7 +224,7 @@ main(int argc, char *argv[])
             if (slave->enabled) {
                 double perfect_fp;
 
-                if (nab->algorithm == htons(NX_BD_ALG_ACTIVE_BACKUP)) {
+                if (bundle->algorithm == NX_BD_ALG_ACTIVE_BACKUP) {
                     perfect_fp = j == active ? 1.0 : 0.0;
                 } else {
                     perfect_fp = 1.0 / n_enabled;
@@ -262,7 +257,7 @@ main(int argc, char *argv[])
         old_n_enabled = n_enabled;
     }
 
-    free(nab);
+    free(bundle);
     free(flows);
     return ok ? 0 : 1;
 }