Catalli's threaded switch
[sliver-openvswitch.git] / tests / test-classifier.c
index 563690d..57a1e2c 100644 (file)
  */
 
 #include <config.h>
-#include <limits.h>
 #include "classifier.h"
 #include <errno.h>
 #include <limits.h>
+#include "command-line.h"
 #include "flow.h"
-#include <limits.h>
 #include "packets.h"
 
 #undef NDEBUG
@@ -238,6 +237,7 @@ static uint32_t nw_src_values[] = { T_HTONL(0xc0a80001),
                                     T_HTONL(0xc0a04455) };
 static uint32_t nw_dst_values[] = { T_HTONL(0xc0a80002),
                                     T_HTONL(0xc0a04455) };
+static uint32_t tun_id_values[] = { 0, 0xffff0000 };
 static uint16_t in_port_values[] = { T_HTONS(1), T_HTONS(OFPP_LOCAL) };
 static uint16_t dl_vlan_values[] = { T_HTONS(101), T_HTONS(0) };
 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
@@ -257,6 +257,9 @@ static void *values[CLS_N_FIELDS][2];
 static void
 init_values(void)
 {
+    values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
+    values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
+
     values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
     values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
 
@@ -296,6 +299,7 @@ init_values(void)
 
 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
+#define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
@@ -309,6 +313,7 @@ init_values(void)
 
 #define N_FLOW_VALUES (N_NW_SRC_VALUES *        \
                        N_NW_DST_VALUES *        \
+                       N_TUN_ID_VALUES *        \
                        N_IN_PORT_VALUES *       \
                        N_DL_VLAN_VALUES *       \
                        N_DL_VLAN_PCP_VALUES *   \
@@ -347,19 +352,21 @@ lookup_with_include_bits(const struct classifier *cls,
 static void
 compare_classifiers(struct classifier *cls, struct tcls *tcls)
 {
+    static const int confidence = 500;
     unsigned int i;
 
     assert(classifier_count(cls) == tcls->n_rules);
     assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
-    for (i = 0; i < N_FLOW_VALUES; i++) {
+    for (i = 0; i < confidence; i++) {
         struct cls_rule *cr0, *cr1;
         flow_t flow;
         unsigned int x;
         int include;
 
-        x = i;
+        x = rand () % N_FLOW_VALUES;
         flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
         flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
+        flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
         flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
         flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
         flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
@@ -462,8 +469,8 @@ make_rule(int wc_fields, unsigned int priority, int value_pat)
     }
 
     rule = xzalloc(sizeof *rule);
-    cls_rule_from_flow(&rule->cls_rule, &flow, wildcards,
-                       !wildcards ? UINT_MAX : priority);
+    cls_rule_from_flow(&flow, wildcards, !wildcards ? UINT_MAX : priority,
+                       &rule->cls_rule);
     return rule;
 }
 
@@ -480,7 +487,7 @@ shuffle(unsigned int *p, size_t n)
 \f
 /* Tests an empty classifier. */
 static void
-test_empty(void)
+test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     struct classifier cls;
     struct tcls tcls;
@@ -496,14 +503,14 @@ test_empty(void)
 
 /* Destroys a null classifier. */
 static void
-test_destroy_null(void)
+test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     classifier_destroy(NULL);
 }
 
 /* Tests classification with one rule at a time. */
 static void
-test_single_rule(void)
+test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     unsigned int wc_fields;     /* Hilarious. */
 
@@ -541,7 +548,7 @@ test_single_rule(void)
 
 /* Tests replacing one rule by another. */
 static void
-test_rule_replacement(void)
+test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     unsigned int wc_fields;
 
@@ -592,7 +599,7 @@ random_wcf_in_table(int table, int seed)
 /* Tests classification with two rules at a time that fall into the same
  * bucket. */
 static void
-test_two_rules_in_one_bucket(void)
+test_two_rules_in_one_bucket(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     int table, rel_pri, wcf_pat, value_pat;
 
@@ -681,7 +688,7 @@ test_two_rules_in_one_bucket(void)
 /* Tests classification with two rules at a time that fall into the same
  * table but different buckets. */
 static void
-test_two_rules_in_one_table(void)
+test_two_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     int table, rel_pri, wcf_pat;
 
@@ -757,7 +764,8 @@ test_two_rules_in_one_table(void)
 /* Tests classification with two rules at a time that fall into different
  * tables. */
 static void
-test_two_rules_in_different_tables(void)
+test_two_rules_in_different_tables(int argc OVS_UNUSED,
+                                   char *argv[] OVS_UNUSED)
 {
     int table1, table2, rel_pri, wcf_pat;
 
@@ -826,7 +834,7 @@ test_two_rules_in_different_tables(void)
 /* Tests classification with many rules at a time that fall into the same
  * bucket but have unique priorities (and various wildcards). */
 static void
-test_many_rules_in_one_bucket(void)
+test_many_rules_in_one_bucket(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     enum { MAX_RULES = 50 };
     int iteration, table;
@@ -870,7 +878,7 @@ test_many_rules_in_one_bucket(void)
 /* Tests classification with many rules at a time that fall into the same
  * table but random buckets. */
 static void
-test_many_rules_in_one_table(void)
+test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     enum { MAX_RULES = 50 };
     int iteration, table;
@@ -913,7 +921,8 @@ test_many_rules_in_one_table(void)
 /* Tests classification with many rules at a time that fall into random buckets
  * in random tables. */
 static void
-test_many_rules_in_different_tables(void)
+test_many_rules_in_different_tables(int argc OVS_UNUSED,
+                                    char *argv[] OVS_UNUSED)
 {
     enum { MAX_RULES = 50 };
     int iteration;
@@ -958,36 +967,32 @@ test_many_rules_in_different_tables(void)
             compare_classifiers(&cls, &tcls);
             free(rule);
         }
-        putchar('.');
-        fflush(stdout);
 
         destroy_classifier(&cls);
         tcls_destroy(&tcls);
     }
 }
 \f
-static void
-run_test(void (*function)(void))
-{
-    function();
-    putchar('.');
-    fflush(stdout);
-}
+static const struct command commands[] = {
+    {"empty", 0, 0, test_empty},
+    {"destroy-null", 0, 0, test_destroy_null},
+    {"single-rule", 0, 0, test_single_rule},
+    {"rule-replacement", 0, 0, test_rule_replacement},
+    {"two-rules-in-one-bucket", 0, 0, test_two_rules_in_one_bucket},
+    {"two-rules-in-one-table", 0, 0, test_two_rules_in_one_table},
+    {"two-rules-in-different-tables", 0, 0,
+     test_two_rules_in_different_tables},
+    {"many-rules-in-one-bucket", 0, 0, test_many_rules_in_one_bucket},
+    {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
+    {"many-rules-in-different-tables", 0, 0,
+     test_many_rules_in_different_tables},
+    {NULL, 0, 0, NULL},
+};
 
 int
-main(void)
+main(int argc, char *argv[])
 {
     init_values();
-    run_test(test_empty);
-    run_test(test_destroy_null);
-    run_test(test_single_rule);
-    run_test(test_rule_replacement);
-    run_test(test_two_rules_in_one_bucket);
-    run_test(test_two_rules_in_one_table);
-    run_test(test_two_rules_in_different_tables);
-    run_test(test_many_rules_in_one_bucket);
-    run_test(test_many_rules_in_one_table);
-    run_test(test_many_rules_in_different_tables);
-    putchar('\n');
+    run_command(argc - 1, argv + 1, commands);
     return 0;
 }