2 * Copyright (c) 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* "White box" tests for classifier.
19 * With very few exceptions, these tests obtain complete coverage of every
20 * basic block and every branch in the classifier implementation, e.g. a clean
21 * report from "gcov -b". (Covering the exceptions would require finding
22 * collisions in the hash function used for flow data, etc.)
24 * This test should receive a clean report from "valgrind --leak-check=full":
25 * it frees every heap block that it allocates.
29 #include "classifier.h"
32 #include "command-line.h"
35 #include "test-command-line.h"
41 int aux; /* Auxiliary data. */
42 struct cls_rule cls_rule; /* Classifier rule data. */
45 static struct test_rule *
46 test_rule_from_cls_rule(const struct cls_rule *rule)
48 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
51 /* Trivial (linear) classifier. */
54 size_t allocated_rules;
55 struct test_rule **rules;
59 tcls_init(struct tcls *tcls)
62 tcls->allocated_rules = 0;
67 tcls_destroy(struct tcls *tcls)
72 for (i = 0; i < tcls->n_rules; i++) {
80 tcls_count_exact(const struct tcls *tcls)
86 for (i = 0; i < tcls->n_rules; i++) {
87 n_exact += tcls->rules[i]->cls_rule.flow.wildcards == 0;
93 tcls_is_empty(const struct tcls *tcls)
95 return tcls->n_rules == 0;
99 effective_priority(const flow_t *flow)
101 return flow->wildcards ? flow->priority : MAX(flow->priority, UINT16_MAX);
104 static struct test_rule *
105 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
107 unsigned int priority = effective_priority(&rule->cls_rule.flow);
110 for (i = 0; i < tcls->n_rules; i++) {
111 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
112 if (pos->flow.priority == priority
113 && pos->flow.wildcards == rule->cls_rule.flow.wildcards
114 && flow_equal_headers(&pos->flow, &rule->cls_rule.flow)) {
116 free(tcls->rules[i]);
117 tcls->rules[i] = xmemdup(rule, sizeof *rule);
118 return tcls->rules[i];
119 } else if (pos->flow.priority < priority) {
124 if (tcls->n_rules >= tcls->allocated_rules) {
125 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
126 sizeof *tcls->rules);
128 if (i != tcls->n_rules) {
129 memmove(&tcls->rules[i + 1], &tcls->rules[i],
130 sizeof *tcls->rules * (tcls->n_rules - i));
132 tcls->rules[i] = xmemdup(rule, sizeof *rule);
134 return tcls->rules[i];
138 tcls_remove(struct tcls *cls, const struct test_rule *rule)
142 for (i = 0; i < cls->n_rules; i++) {
143 struct test_rule *pos = cls->rules[i];
146 memmove(&cls->rules[i], &cls->rules[i + 1],
147 sizeof *cls->rules * (cls->n_rules - i - 1));
156 read_uint32(const void *p)
159 memcpy(&x, p, sizeof x);
164 match(const struct cls_rule *wild, const flow_t *fixed)
168 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
169 const struct cls_field *f = &cls_fields[f_idx];
170 void *wild_field = (char *) &wild->flow + f->ofs;
171 void *fixed_field = (char *) fixed + f->ofs;
173 if ((wild->flow.wildcards & f->wildcards) == f->wildcards ||
174 !memcmp(wild_field, fixed_field, f->len)) {
175 /* Definite match. */
179 if (wild->flow.wildcards & f->wildcards) {
180 uint32_t test = read_uint32(wild_field);
181 uint32_t ip = read_uint32(fixed_field);
182 int shift = (f_idx == CLS_F_IDX_NW_SRC
183 ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
184 uint32_t mask = flow_nw_bits_to_mask(wild->flow.wildcards, shift);
185 if (!((test ^ ip) & mask)) {
195 static struct cls_rule *
196 tcls_lookup(const struct tcls *cls, const flow_t *flow, int include)
200 for (i = 0; i < cls->n_rules; i++) {
201 struct test_rule *pos = cls->rules[i];
202 uint32_t wildcards = pos->cls_rule.flow.wildcards;
203 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
204 && match(&pos->cls_rule, flow)) {
205 return &pos->cls_rule;
212 tcls_delete_matches(struct tcls *cls,
213 const struct cls_rule *target,
218 for (i = 0; i < cls->n_rules; ) {
219 struct test_rule *pos = cls->rules[i];
220 uint32_t wildcards = pos->cls_rule.flow.wildcards;
221 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
222 && match(target, &pos->cls_rule.flow)) {
223 tcls_remove(cls, pos);
230 #ifdef WORDS_BIGENDIAN
231 #define T_HTONL(VALUE) ((uint32_t) (VALUE))
232 #define T_HTONS(VALUE) ((uint32_t) (VALUE))
234 #define T_HTONL(VALUE) (((((uint32_t) (VALUE)) & 0x000000ff) << 24) | \
235 ((((uint32_t) (VALUE)) & 0x0000ff00) << 8) | \
236 ((((uint32_t) (VALUE)) & 0x00ff0000) >> 8) | \
237 ((((uint32_t) (VALUE)) & 0xff000000) >> 24))
238 #define T_HTONS(VALUE) (((((uint16_t) (VALUE)) & 0xff00) >> 8) | \
239 ((((uint16_t) (VALUE)) & 0x00ff) << 8))
242 static uint32_t nw_src_values[] = { T_HTONL(0xc0a80001),
243 T_HTONL(0xc0a04455) };
244 static uint32_t nw_dst_values[] = { T_HTONL(0xc0a80002),
245 T_HTONL(0xc0a04455) };
246 static uint32_t tun_id_values[] = { 0, 0xffff0000 };
247 static uint16_t in_port_values[] = { T_HTONS(1), T_HTONS(OFPP_LOCAL) };
248 static uint16_t dl_vlan_values[] = { T_HTONS(101), T_HTONS(0) };
249 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
250 static uint16_t dl_type_values[]
251 = { T_HTONS(ETH_TYPE_IP), T_HTONS(ETH_TYPE_ARP) };
252 static uint16_t tp_src_values[] = { T_HTONS(49362), T_HTONS(80) };
253 static uint16_t tp_dst_values[] = { T_HTONS(6667), T_HTONS(22) };
254 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
255 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
256 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
257 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
258 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
259 static uint8_t nw_tos_values[] = { 49, 0 };
261 static void *values[CLS_N_FIELDS][2];
266 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
267 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
269 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
270 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
272 values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
273 values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
275 values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
276 values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1];
278 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
279 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
281 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
282 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
284 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
285 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
287 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
288 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
290 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
291 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
293 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
294 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
296 values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0];
297 values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1];
299 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
300 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
302 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
303 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
306 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
307 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
308 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
309 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
310 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
311 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
312 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
313 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
314 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
315 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
316 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
317 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
318 #define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values)
320 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
325 N_DL_VLAN_PCP_VALUES * \
331 N_NW_PROTO_VALUES * \
335 get_value(unsigned int *x, unsigned n_values)
337 unsigned int rem = *x % n_values;
342 static struct cls_rule *
343 lookup_with_include_bits(const struct classifier *cls,
344 const flow_t *flow, int include)
348 return classifier_lookup_wild(cls, flow);
350 return classifier_lookup_exact(cls, flow);
351 case CLS_INC_WILD | CLS_INC_EXACT:
352 return classifier_lookup(cls, flow);
359 compare_classifiers(struct classifier *cls, struct tcls *tcls)
361 static const int confidence = 500;
364 assert(classifier_count(cls) == tcls->n_rules);
365 assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
366 for (i = 0; i < confidence; i++) {
367 struct cls_rule *cr0, *cr1;
372 x = rand () % N_FLOW_VALUES;
373 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
374 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
375 flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
376 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
377 flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
378 flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
379 N_DL_VLAN_PCP_VALUES)];
380 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
381 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
382 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
383 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
385 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
387 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
388 flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
390 for (include = 1; include <= 3; include++) {
391 cr0 = lookup_with_include_bits(cls, &flow, include);
392 cr1 = tcls_lookup(tcls, &flow, include);
393 assert((cr0 == NULL) == (cr1 == NULL));
395 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
396 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
398 assert(flow_equal_headers(&cr0->flow, &cr1->flow));
399 assert(cr0->flow.wildcards == cr1->flow.wildcards);
400 assert(cr0->flow.priority == cr1->flow.priority);
401 /* Skip nw_src_mask, nw_dst_mask, and dl_tci_mask, because they
402 * are derived members used only for optimization. */
403 assert(tr0->aux == tr1->aux);
410 free_rule(struct cls_rule *cls_rule, void *cls)
412 classifier_remove(cls, cls_rule);
413 free(test_rule_from_cls_rule(cls_rule));
417 destroy_classifier(struct classifier *cls)
419 classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
420 classifier_destroy(cls);
424 check_tables(const struct classifier *cls,
425 int n_tables, int n_buckets, int n_rules)
427 int found_tables = 0;
428 int found_buckets = 0;
432 BUILD_ASSERT(CLS_N_FIELDS == ARRAY_SIZE(cls->tables));
433 for (i = 0; i < CLS_N_FIELDS; i++) {
434 const struct cls_bucket *bucket;
435 if (!hmap_is_empty(&cls->tables[i])) {
438 HMAP_FOR_EACH (bucket, struct cls_bucket, hmap_node, &cls->tables[i]) {
440 assert(!list_is_empty(&bucket->rules));
441 found_rules += list_size(&bucket->rules);
445 if (!hmap_is_empty(&cls->exact_table)) {
448 found_rules += hmap_count(&cls->exact_table);
451 assert(n_tables == -1 || found_tables == n_tables);
452 assert(n_rules == -1 || found_rules == n_rules);
453 assert(n_buckets == -1 || found_buckets == n_buckets);
456 static struct test_rule *
457 make_rule(int wc_fields, unsigned int priority, int value_pat)
459 const struct cls_field *f;
460 struct test_rule *rule;
463 memset(&flow, 0, sizeof flow);
464 flow.priority = priority;
465 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
466 int f_idx = f - cls_fields;
467 if (wc_fields & (1u << f_idx)) {
468 flow.wildcards |= f->wildcards;
470 int value_idx = (value_pat & (1u << f_idx)) != 0;
471 memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
475 rule = xzalloc(sizeof *rule);
476 cls_rule_from_flow(&flow, &rule->cls_rule);
481 shuffle(unsigned int *p, size_t n)
483 for (; n > 1; n--, p++) {
484 unsigned int *q = &p[rand() % n];
485 unsigned int tmp = *p;
491 /* Tests an empty classifier. */
493 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
495 struct classifier cls;
498 classifier_init(&cls);
500 assert(classifier_is_empty(&cls));
501 assert(tcls_is_empty(&tcls));
502 compare_classifiers(&cls, &tcls);
503 classifier_destroy(&cls);
507 /* Destroys a null classifier. */
509 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
511 classifier_destroy(NULL);
514 /* Tests classification with one rule at a time. */
516 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
518 unsigned int wc_fields; /* Hilarious. */
520 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
521 struct classifier cls;
522 struct test_rule *rule, *tcls_rule;
525 rule = make_rule(wc_fields,
526 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
528 classifier_init(&cls);
531 tcls_rule = tcls_insert(&tcls, rule);
533 assert(!classifier_insert(&cls, &rule->cls_rule));
535 classifier_insert_exact(&cls, &rule->cls_rule);
537 check_tables(&cls, 1, 1, 1);
538 compare_classifiers(&cls, &tcls);
540 classifier_remove(&cls, &rule->cls_rule);
541 tcls_remove(&tcls, tcls_rule);
542 assert(classifier_is_empty(&cls));
543 assert(tcls_is_empty(&tcls));
544 compare_classifiers(&cls, &tcls);
547 classifier_destroy(&cls);
552 /* Tests replacing one rule by another. */
554 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
556 unsigned int wc_fields;
558 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
559 struct classifier cls;
560 struct test_rule *rule1;
561 struct test_rule *rule2;
564 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
565 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
569 classifier_init(&cls);
571 tcls_insert(&tcls, rule1);
572 assert(!classifier_insert(&cls, &rule1->cls_rule));
573 check_tables(&cls, 1, 1, 1);
574 compare_classifiers(&cls, &tcls);
578 tcls_insert(&tcls, rule2);
579 assert(test_rule_from_cls_rule(
580 classifier_insert(&cls, &rule2->cls_rule)) == rule1);
582 check_tables(&cls, 1, 1, 1);
583 compare_classifiers(&cls, &tcls);
585 destroy_classifier(&cls);
590 table_mask(int table)
592 return ((1u << CLS_N_FIELDS) - 1) & ~((1u << table) - 1);
596 random_wcf_in_table(int table, int seed)
598 int wc_fields = (1u << table) | hash_int(seed, 0);
599 return wc_fields & table_mask(table);
602 /* Tests classification with two rules at a time that fall into the same
605 test_two_rules_in_one_bucket(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
607 int table, rel_pri, wcf_pat, value_pat;
609 for (table = 0; table <= CLS_N_FIELDS; table++) {
610 for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
611 for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
612 int n_value_pats = table == CLS_N_FIELDS - 1 ? 1 : 2;
613 for (value_pat = 0; value_pat < n_value_pats; value_pat++) {
614 struct test_rule *rule1, *tcls_rule1;
615 struct test_rule *rule2, *tcls_rule2;
616 struct test_rule *displaced_rule;
617 struct classifier cls;
619 unsigned int pri1, pri2;
622 if (table != CLS_F_IDX_EXACT) {
623 /* We can use identical priorities in this test because
624 * the classifier always chooses the rule added later
625 * for equal-priority rules that fall into the same
627 pri1 = table * 257 + 50;
628 pri2 = pri1 + rel_pri;
631 ? random_wcf_in_table(table, pri1)
634 ? random_wcf_in_table(table, pri2)
637 wcf1 &= ~(1u << (CLS_N_FIELDS - 1));
638 wcf2 &= ~(1u << (CLS_N_FIELDS - 1));
641 /* This classifier always puts exact-match rules at
642 * maximum priority. */
643 pri1 = pri2 = UINT_MAX;
645 /* No wildcard fields. */
649 rule1 = make_rule(wcf1, pri1, 0);
650 rule2 = make_rule(wcf2, pri2,
651 value_pat << (CLS_N_FIELDS - 1));
653 classifier_init(&cls);
656 tcls_rule1 = tcls_insert(&tcls, rule1);
657 tcls_rule2 = tcls_insert(&tcls, rule2);
658 assert(!classifier_insert(&cls, &rule1->cls_rule));
659 displaced_rule = test_rule_from_cls_rule(
660 classifier_insert(&cls, &rule2->cls_rule));
661 if (wcf1 != wcf2 || pri1 != pri2 || value_pat) {
662 assert(!displaced_rule);
664 check_tables(&cls, 1, 1, 2);
665 compare_classifiers(&cls, &tcls);
667 classifier_remove(&cls, &rule1->cls_rule);
668 tcls_remove(&tcls, tcls_rule1);
669 check_tables(&cls, 1, 1, 1);
670 compare_classifiers(&cls, &tcls);
672 assert(displaced_rule == rule1);
673 check_tables(&cls, 1, 1, 1);
674 compare_classifiers(&cls, &tcls);
678 classifier_remove(&cls, &rule2->cls_rule);
679 tcls_remove(&tcls, tcls_rule2);
680 compare_classifiers(&cls, &tcls);
683 destroy_classifier(&cls);
691 /* Tests classification with two rules at a time that fall into the same
692 * table but different buckets. */
694 test_two_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
696 int table, rel_pri, wcf_pat;
698 /* Skip tables 0 and CLS_F_IDX_EXACT because they have one bucket. */
699 for (table = 1; table < CLS_N_FIELDS; table++) {
700 for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
701 for (wcf_pat = 0; wcf_pat < 5; wcf_pat++) {
702 struct test_rule *rule1, *tcls_rule1;
703 struct test_rule *rule2, *tcls_rule2;
704 struct classifier cls;
706 unsigned int pri1, pri2;
708 int value_mask, value_pat1, value_pat2;
711 /* We can use identical priorities in this test because the
712 * classifier always chooses the rule added later for
713 * equal-priority rules that fall into the same table. */
714 pri1 = table * 257 + 50;
715 pri2 = pri1 + rel_pri;
718 wcf1 = wcf2 = random_wcf_in_table(table, pri1);
721 ? random_wcf_in_table(table, pri1)
724 ? random_wcf_in_table(table, pri2)
728 /* Generate value patterns that will put the two rules into
729 * different buckets. */
730 value_mask = ((1u << table) - 1);
731 value_pat1 = hash_int(pri1, 1) & value_mask;
734 value_pat2 = (hash_int(pri2, i++) & value_mask);
735 } while (value_pat1 == value_pat2);
736 rule1 = make_rule(wcf1, pri1, value_pat1);
737 rule2 = make_rule(wcf2, pri2, value_pat2);
739 classifier_init(&cls);
742 tcls_rule1 = tcls_insert(&tcls, rule1);
743 tcls_rule2 = tcls_insert(&tcls, rule2);
744 assert(!classifier_insert(&cls, &rule1->cls_rule));
745 assert(!classifier_insert(&cls, &rule2->cls_rule));
746 check_tables(&cls, 1, 2, 2);
747 compare_classifiers(&cls, &tcls);
749 classifier_remove(&cls, &rule1->cls_rule);
750 tcls_remove(&tcls, tcls_rule1);
751 check_tables(&cls, 1, 1, 1);
752 compare_classifiers(&cls, &tcls);
755 classifier_remove(&cls, &rule2->cls_rule);
756 tcls_remove(&tcls, tcls_rule2);
757 compare_classifiers(&cls, &tcls);
760 classifier_destroy(&cls);
767 /* Tests classification with two rules at a time that fall into different
770 test_two_rules_in_different_tables(int argc OVS_UNUSED,
771 char *argv[] OVS_UNUSED)
773 int table1, table2, rel_pri, wcf_pat;
775 for (table1 = 0; table1 < CLS_N_FIELDS; table1++) {
776 for (table2 = table1 + 1; table2 <= CLS_N_FIELDS; table2++) {
777 for (rel_pri = 0; rel_pri < 2; rel_pri++) {
778 for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
779 struct test_rule *rule1, *tcls_rule1;
780 struct test_rule *rule2, *tcls_rule2;
781 struct classifier cls;
783 unsigned int pri1, pri2;
786 /* We must use unique priorities in this test because the
787 * classifier makes the rule choice undefined for rules of
788 * equal priority that fall into different tables. (In
789 * practice, lower-numbered tables win.) */
790 pri1 = table1 * 257 + 50;
791 pri2 = rel_pri ? pri1 - 1 : pri1 + 1;
794 ? random_wcf_in_table(table1, pri1)
797 ? random_wcf_in_table(table2, pri2)
800 if (table2 == CLS_F_IDX_EXACT) {
805 rule1 = make_rule(wcf1, pri1, 0);
806 rule2 = make_rule(wcf2, pri2, 0);
808 classifier_init(&cls);
811 tcls_rule1 = tcls_insert(&tcls, rule1);
812 tcls_rule2 = tcls_insert(&tcls, rule2);
813 assert(!classifier_insert(&cls, &rule1->cls_rule));
814 assert(!classifier_insert(&cls, &rule2->cls_rule));
815 check_tables(&cls, 2, 2, 2);
816 compare_classifiers(&cls, &tcls);
818 classifier_remove(&cls, &rule1->cls_rule);
819 tcls_remove(&tcls, tcls_rule1);
820 check_tables(&cls, 1, 1, 1);
821 compare_classifiers(&cls, &tcls);
824 classifier_remove(&cls, &rule2->cls_rule);
825 tcls_remove(&tcls, tcls_rule2);
826 compare_classifiers(&cls, &tcls);
829 classifier_destroy(&cls);
837 /* Tests classification with many rules at a time that fall into the same
838 * bucket but have unique priorities (and various wildcards). */
840 test_many_rules_in_one_bucket(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
842 enum { MAX_RULES = 50 };
843 int iteration, table;
845 for (iteration = 0; iteration < 3; iteration++) {
846 for (table = 0; table <= CLS_N_FIELDS; table++) {
847 unsigned int priorities[MAX_RULES];
848 struct classifier cls;
852 srand(hash_int(table, iteration));
853 for (i = 0; i < MAX_RULES; i++) {
854 priorities[i] = i * 129;
856 shuffle(priorities, ARRAY_SIZE(priorities));
858 classifier_init(&cls);
861 for (i = 0; i < MAX_RULES; i++) {
862 struct test_rule *rule;
863 unsigned int priority = priorities[i];
866 wcf = random_wcf_in_table(table, priority);
867 rule = make_rule(wcf, priority,
868 table == CLS_F_IDX_EXACT ? i : 1234);
869 tcls_insert(&tcls, rule);
870 assert(!classifier_insert(&cls, &rule->cls_rule));
871 check_tables(&cls, 1, 1, i + 1);
872 compare_classifiers(&cls, &tcls);
875 destroy_classifier(&cls);
881 /* Tests classification with many rules at a time that fall into the same
882 * table but random buckets. */
884 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
886 enum { MAX_RULES = 50 };
887 int iteration, table;
889 for (iteration = 0; iteration < 3; iteration++) {
890 for (table = 0; table < CLS_N_FIELDS; table++) {
891 unsigned int priorities[MAX_RULES];
892 struct classifier cls;
896 srand(hash_int(table, iteration));
897 for (i = 0; i < MAX_RULES; i++) {
898 priorities[i] = i * 129;
900 shuffle(priorities, ARRAY_SIZE(priorities));
902 classifier_init(&cls);
905 for (i = 0; i < MAX_RULES; i++) {
906 struct test_rule *rule;
907 unsigned int priority = priorities[i];
910 wcf = random_wcf_in_table(table, priority);
911 rule = make_rule(wcf, priority, hash_int(priority, 1));
912 tcls_insert(&tcls, rule);
913 assert(!classifier_insert(&cls, &rule->cls_rule));
914 check_tables(&cls, 1, -1, i + 1);
915 compare_classifiers(&cls, &tcls);
918 destroy_classifier(&cls);
924 /* Tests classification with many rules at a time that fall into random buckets
925 * in random tables. */
927 test_many_rules_in_different_tables(int argc OVS_UNUSED,
928 char *argv[] OVS_UNUSED)
930 enum { MAX_RULES = 50 };
933 for (iteration = 0; iteration < 30; iteration++) {
934 unsigned int priorities[MAX_RULES];
935 struct classifier cls;
940 for (i = 0; i < MAX_RULES; i++) {
941 priorities[i] = i * 129;
943 shuffle(priorities, ARRAY_SIZE(priorities));
945 classifier_init(&cls);
948 for (i = 0; i < MAX_RULES; i++) {
949 struct test_rule *rule;
950 unsigned int priority = priorities[i];
951 int table = rand() % (CLS_N_FIELDS + 1);
952 int wcf = random_wcf_in_table(table, rand());
953 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
954 rule = make_rule(wcf, priority, value_pat);
955 tcls_insert(&tcls, rule);
956 assert(!classifier_insert(&cls, &rule->cls_rule));
957 check_tables(&cls, -1, -1, i + 1);
958 compare_classifiers(&cls, &tcls);
961 while (!classifier_is_empty(&cls)) {
962 struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
963 sizeof(struct test_rule));
964 int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
965 include |= (rule->cls_rule.flow.wildcards
966 ? CLS_INC_WILD : CLS_INC_EXACT);
967 classifier_for_each_match(&cls, &rule->cls_rule.flow, include,
969 tcls_delete_matches(&tcls, &rule->cls_rule, include);
970 compare_classifiers(&cls, &tcls);
974 destroy_classifier(&cls);
980 main(int argc, char *argv[])
982 static const struct command all_commands[] = {
983 { "empty", 0, 0, test_empty },
984 { "destroy-null", 0, 0, test_destroy_null },
985 { "single-rule", 0, 0, test_single_rule },
986 { "rule-replacement", 0, 0, test_rule_replacement },
987 { "two-rules-in-one-bucket", 0, 0, test_two_rules_in_one_bucket },
988 { "two-rules-in-one-table", 0, 0, test_two_rules_in_one_table },
989 { "two-rules-in-different-tables", 0, 0,
990 test_two_rules_in_different_tables },
991 { "many-rules-in-one-bucket", 0, 0,
992 test_many_rules_in_one_bucket },
993 { "many-rules-in-one-table", 0, 0, test_many_rules_in_one_table },
994 { "many-rules-in-different-tables", 0, 0,
995 test_many_rules_in_different_tables },
996 { NULL, 0, 0, NULL },
999 set_program_name(argv[0]);
1001 parse_test_options(argc, argv, all_commands);
1002 run_command(argc - 1, argv + 1, all_commands);