classifier: Add functions and macros for iteration, and use them in ofproto.
[openvswitch] / tests / test-classifier.c
index 70af7ed05475d11164bc08909a26b2ba566781c7..9af97a4c853b1c929e22b40a23389c24efaf53b3 100644 (file)
@@ -33,6 +33,7 @@
 #include "command-line.h"
 #include "flow.h"
 #include "packets.h"
+#include "unaligned.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -51,7 +52,8 @@
     CLS_FIELD(OFPFW_TP_SRC,      tp_src,      TP_SRC)       \
     CLS_FIELD(OFPFW_TP_DST,      tp_dst,      TP_DST)       \
     CLS_FIELD(OFPFW_DL_SRC,      dl_src,      DL_SRC)       \
-    CLS_FIELD(OFPFW_DL_DST,      dl_dst,      DL_DST)       \
+    CLS_FIELD(OFPFW_DL_DST | FWW_ETH_MCAST,                 \
+                                 dl_dst,      DL_DST)       \
     CLS_FIELD(OFPFW_NW_PROTO,    nw_proto,    NW_PROTO)     \
     CLS_FIELD(OFPFW_DL_VLAN_PCP, dl_vlan_pcp, DL_VLAN_PCP)  \
     CLS_FIELD(OFPFW_NW_TOS,      nw_tos,      NW_TOS)
@@ -123,19 +125,6 @@ tcls_destroy(struct tcls *tcls)
     }
 }
 
-static int
-tcls_count_exact(const struct tcls *tcls)
-{
-    int n_exact;
-    size_t i;
-
-    n_exact = 0;
-    for (i = 0; i < tcls->n_rules; i++) {
-        n_exact += tcls->rules[i]->cls_rule.wc.wildcards == 0;
-    }
-    return n_exact;
-}
-
 static bool
 tcls_is_empty(const struct tcls *tcls)
 {
@@ -194,14 +183,6 @@ tcls_remove(struct tcls *cls, const struct test_rule *rule)
     NOT_REACHED();
 }
 
-static uint32_t
-read_uint32(const void *p)
-{
-    uint32_t x;
-    memcpy(&x, p, sizeof x);
-    return x;
-}
-
 static bool
 match(const struct cls_rule *wild, const struct flow *fixed)
 {
@@ -219,8 +200,8 @@ match(const struct cls_rule *wild, const struct flow *fixed)
         }
 
         if (wild->wc.wildcards & f->wildcards) {
-            uint32_t test = read_uint32(wild_field);
-            uint32_t ip = read_uint32(fixed_field);
+            uint32_t test = get_unaligned_u32(wild_field);
+            uint32_t ip = get_unaligned_u32(fixed_field);
             int shift = (f_idx == CLS_F_IDX_NW_SRC
                          ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
             uint32_t mask = flow_nw_bits_to_mask(wild->wc.wildcards, shift);
@@ -235,15 +216,13 @@ match(const struct cls_rule *wild, const struct flow *fixed)
 }
 
 static struct cls_rule *
-tcls_lookup(const struct tcls *cls, const struct flow *flow, int include)
+tcls_lookup(const struct tcls *cls, const struct flow *flow)
 {
     size_t i;
 
     for (i = 0; i < cls->n_rules; i++) {
         struct test_rule *pos = cls->rules[i];
-        uint32_t wildcards = pos->cls_rule.wc.wildcards;
-        if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
-            && match(&pos->cls_rule, flow)) {
+        if (match(&pos->cls_rule, flow)) {
             return &pos->cls_rule;
         }
     }
@@ -251,17 +230,13 @@ tcls_lookup(const struct tcls *cls, const struct flow *flow, int include)
 }
 
 static void
-tcls_delete_matches(struct tcls *cls,
-                    const struct cls_rule *target,
-                    int include)
+tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
 {
     size_t i;
 
     for (i = 0; i < cls->n_rules; ) {
         struct test_rule *pos = cls->rules[i];
-        uint32_t wildcards = pos->cls_rule.wc.wildcards;
-        if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
-            && !flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
+        if (!flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
             && match(target, &pos->cls_rule.flow)) {
             tcls_remove(cls, pos);
         } else {
@@ -270,20 +245,19 @@ tcls_delete_matches(struct tcls *cls,
     }
 }
 \f
-static uint32_t nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
+static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
                                     CONSTANT_HTONL(0xc0a04455) };
-static uint32_t nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
+static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
                                     CONSTANT_HTONL(0xc0a04455) };
-static uint32_t tun_id_values[] = { 0, 0xffff0000 };
-static uint16_t in_port_values[] = { CONSTANT_HTONS(1),
-                                     CONSTANT_HTONS(OFPP_LOCAL) };
-static uint16_t dl_vlan_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
+static ovs_be32 tun_id_values[] = { 0, 0xffff0000 };
+static uint16_t in_port_values[] = { 1, ODPP_LOCAL };
+static ovs_be16 dl_vlan_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
-static uint16_t dl_type_values[]
+static ovs_be16 dl_type_values[]
             = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
-static uint16_t tp_src_values[] = { CONSTANT_HTONS(49362),
+static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
                                     CONSTANT_HTONS(80) };
-static uint16_t tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
+static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
                                       { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
@@ -379,12 +353,10 @@ compare_classifiers(struct classifier *cls, struct tcls *tcls)
     unsigned int i;
 
     assert(classifier_count(cls) == tcls->n_rules);
-    assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
     for (i = 0; i < confidence; i++) {
         struct cls_rule *cr0, *cr1;
         struct flow flow;
         unsigned int x;
-        int include;
 
         x = rand () % N_FLOW_VALUES;
         flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
@@ -404,36 +376,34 @@ compare_classifiers(struct classifier *cls, struct tcls *tcls)
         flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
         flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
 
-        for (include = 1; include <= 3; include++) {
-            cr0 = classifier_lookup(cls, &flow, include);
-            cr1 = tcls_lookup(tcls, &flow, include);
-            assert((cr0 == NULL) == (cr1 == NULL));
-            if (cr0 != NULL) {
-                const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
-                const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
-
-                assert(flow_equal(&cr0->flow, &cr1->flow));
-                assert(cr0->wc.wildcards == cr1->wc.wildcards);
-                assert(cr0->priority == cr1->priority);
-                /* Skip nw_src_mask and nw_dst_mask, because they are derived
-                 * members whose values are used only for optimization. */
-                assert(tr0->aux == tr1->aux);
-            }
+        cr0 = classifier_lookup(cls, &flow);
+        cr1 = tcls_lookup(tcls, &flow);
+        assert((cr0 == NULL) == (cr1 == NULL));
+        if (cr0 != NULL) {
+            const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
+            const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
+
+            assert(flow_equal(&cr0->flow, &cr1->flow));
+            assert(cr0->wc.wildcards == cr1->wc.wildcards);
+            assert(cr0->priority == cr1->priority);
+            /* Skip nw_src_mask and nw_dst_mask, because they are derived
+             * members whose values are used only for optimization. */
+            assert(tr0->aux == tr1->aux);
         }
     }
 }
 
-static void
-free_rule(struct cls_rule *cls_rule, void *cls)
-{
-    classifier_remove(cls, cls_rule);
-    free(test_rule_from_cls_rule(cls_rule));
-}
-
 static void
 destroy_classifier(struct classifier *cls)
 {
-    classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
+    struct test_rule *rule, *next_rule;
+    struct cls_cursor cursor;
+
+    cls_cursor_init(&cursor, cls, NULL);
+    CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
+        classifier_remove(cls, &rule->cls_rule);
+        free(rule);
+    }
     classifier_destroy(cls);
 }
 
@@ -442,10 +412,12 @@ check_tables(const struct classifier *cls,
              int n_tables, int n_rules, int n_dups)
 {
     const struct cls_table *table;
+    struct flow_wildcards exact_wc;
     int found_tables = 0;
     int found_rules = 0;
     int found_dups = 0;
 
+    flow_wildcards_init_exact(&exact_wc);
     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
         const struct cls_rule *head;
 
@@ -477,6 +449,7 @@ static struct test_rule *
 make_rule(int wc_fields, unsigned int priority, int value_pat)
 {
     const struct cls_field *f;
+    struct flow_wildcards wc;
     struct test_rule *rule;
     uint32_t wildcards;
     struct flow flow;
@@ -494,8 +467,9 @@ make_rule(int wc_fields, unsigned int priority, int value_pat)
     }
 
     rule = xzalloc(sizeof *rule);
-    cls_rule_from_flow(&flow, wildcards, !wildcards ? UINT_MAX : priority,
-                       &rule->cls_rule);
+    flow_wildcards_init(&wc, wildcards);
+    cls_rule_init(&flow, &wc, !wildcards ? UINT_MAX : priority,
+                  &rule->cls_rule);
     return rule;
 }
 
@@ -870,17 +844,22 @@ test_many_rules_in_n_tables(int n_tables)
         }
 
         while (!classifier_is_empty(&cls)) {
-            struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
-                                             sizeof(struct test_rule));
-            int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
-            include |= (rule->cls_rule.wc.wildcards
-                        ? CLS_INC_WILD : CLS_INC_EXACT);
-            classifier_for_each_match(&cls, &rule->cls_rule, include,
-                                      free_rule, &cls);
-            tcls_delete_matches(&tcls, &rule->cls_rule, include);
+            struct test_rule *rule, *next_rule;
+            struct test_rule *target;
+            struct cls_cursor cursor;
+
+            target = xmemdup(tcls.rules[rand() % tcls.n_rules],
+                             sizeof(struct test_rule));
+
+            cls_cursor_init(&cursor, &cls, &target->cls_rule);
+            CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
+                classifier_remove(&cls, &rule->cls_rule);
+                free(rule);
+            }
+            tcls_delete_matches(&tcls, &target->cls_rule);
             compare_classifiers(&cls, &tcls);
             check_tables(&cls, -1, -1, -1);
-            free(rule);
+            free(target);
         }
 
         destroy_classifier(&cls);