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.
30 #include "classifier.h"
33 #include "command-line.h"
37 #include "test-command-line.h"
43 int aux; /* Auxiliary data. */
44 struct cls_rule cls_rule; /* Classifier rule data. */
47 static struct test_rule *
48 test_rule_from_cls_rule(const struct cls_rule *rule)
50 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
53 /* Trivial (linear) classifier. */
56 size_t allocated_rules;
57 struct test_rule **rules;
61 tcls_init(struct tcls *tcls)
64 tcls->allocated_rules = 0;
69 tcls_destroy(struct tcls *tcls)
74 for (i = 0; i < tcls->n_rules; i++) {
82 tcls_count_exact(const struct tcls *tcls)
88 for (i = 0; i < tcls->n_rules; i++) {
89 n_exact += tcls->rules[i]->cls_rule.wc.wildcards == 0;
95 tcls_is_empty(const struct tcls *tcls)
97 return tcls->n_rules == 0;
100 static struct test_rule *
101 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
105 assert(rule->cls_rule.wc.wildcards || rule->cls_rule.priority == UINT_MAX);
106 for (i = 0; i < tcls->n_rules; i++) {
107 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
108 if (pos->priority == rule->cls_rule.priority
109 && pos->wc.wildcards == rule->cls_rule.wc.wildcards
110 && flow_equal(&pos->flow, &rule->cls_rule.flow)) {
112 * XXX flow_equal should ignore wildcarded fields */
113 free(tcls->rules[i]);
114 tcls->rules[i] = xmemdup(rule, sizeof *rule);
115 return tcls->rules[i];
116 } else if (pos->priority < rule->cls_rule.priority) {
121 if (tcls->n_rules >= tcls->allocated_rules) {
122 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
123 sizeof *tcls->rules);
125 if (i != tcls->n_rules) {
126 memmove(&tcls->rules[i + 1], &tcls->rules[i],
127 sizeof *tcls->rules * (tcls->n_rules - i));
129 tcls->rules[i] = xmemdup(rule, sizeof *rule);
131 return tcls->rules[i];
135 tcls_remove(struct tcls *cls, const struct test_rule *rule)
139 for (i = 0; i < cls->n_rules; i++) {
140 struct test_rule *pos = cls->rules[i];
143 memmove(&cls->rules[i], &cls->rules[i + 1],
144 sizeof *cls->rules * (cls->n_rules - i - 1));
153 read_uint32(const void *p)
156 memcpy(&x, p, sizeof x);
161 match(const struct cls_rule *wild, const flow_t *fixed)
165 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
166 const struct cls_field *f = &cls_fields[f_idx];
167 void *wild_field = (char *) &wild->flow + f->ofs;
168 void *fixed_field = (char *) fixed + f->ofs;
170 if ((wild->wc.wildcards & f->wildcards) == f->wildcards ||
171 !memcmp(wild_field, fixed_field, f->len)) {
172 /* Definite match. */
176 if (wild->wc.wildcards & f->wildcards) {
177 uint32_t test = read_uint32(wild_field);
178 uint32_t ip = read_uint32(fixed_field);
179 int shift = (f_idx == CLS_F_IDX_NW_SRC
180 ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
181 uint32_t mask = flow_nw_bits_to_mask(wild->wc.wildcards, shift);
182 if (!((test ^ ip) & mask)) {
192 static struct cls_rule *
193 tcls_lookup(const struct tcls *cls, const flow_t *flow, int include)
197 for (i = 0; i < cls->n_rules; i++) {
198 struct test_rule *pos = cls->rules[i];
199 uint32_t wildcards = pos->cls_rule.wc.wildcards;
200 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
201 && match(&pos->cls_rule, flow)) {
202 return &pos->cls_rule;
209 tcls_delete_matches(struct tcls *cls,
210 const struct cls_rule *target,
215 for (i = 0; i < cls->n_rules; ) {
216 struct test_rule *pos = cls->rules[i];
217 uint32_t wildcards = pos->cls_rule.wc.wildcards;
218 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
219 && match(target, &pos->cls_rule.flow)) {
220 tcls_remove(cls, pos);
227 #ifdef WORDS_BIGENDIAN
228 #define T_HTONL(VALUE) ((uint32_t) (VALUE))
229 #define T_HTONS(VALUE) ((uint32_t) (VALUE))
231 #define T_HTONL(VALUE) (((((uint32_t) (VALUE)) & 0x000000ff) << 24) | \
232 ((((uint32_t) (VALUE)) & 0x0000ff00) << 8) | \
233 ((((uint32_t) (VALUE)) & 0x00ff0000) >> 8) | \
234 ((((uint32_t) (VALUE)) & 0xff000000) >> 24))
235 #define T_HTONS(VALUE) (((((uint16_t) (VALUE)) & 0xff00) >> 8) | \
236 ((((uint16_t) (VALUE)) & 0x00ff) << 8))
239 static uint32_t nw_src_values[] = { T_HTONL(0xc0a80001),
240 T_HTONL(0xc0a04455) };
241 static uint32_t nw_dst_values[] = { T_HTONL(0xc0a80002),
242 T_HTONL(0xc0a04455) };
243 static uint16_t in_port_values[] = { T_HTONS(1), T_HTONS(OFPP_LOCAL) };
244 static uint16_t dl_vlan_values[] = { T_HTONS(101), T_HTONS(0) };
245 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
246 static uint16_t dl_type_values[]
247 = { T_HTONS(ETH_TYPE_IP), T_HTONS(ETH_TYPE_ARP) };
248 static uint16_t tp_src_values[] = { T_HTONS(49362), T_HTONS(80) };
249 static uint16_t tp_dst_values[] = { T_HTONS(6667), T_HTONS(22) };
250 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
251 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
252 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
253 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
254 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
255 static uint8_t nw_tos_values[] = { 49, 0 };
257 static void *values[CLS_N_FIELDS][2];
262 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
263 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
265 values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
266 values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
268 values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
269 values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1];
271 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
272 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
274 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
275 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
277 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
278 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
280 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
281 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
283 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
284 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
286 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
287 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
289 values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0];
290 values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1];
292 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
293 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
295 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
296 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
299 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
300 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
301 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
302 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
303 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
304 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
305 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
306 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
307 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
308 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
309 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
310 #define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values)
312 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
316 N_DL_VLAN_PCP_VALUES * \
322 N_NW_PROTO_VALUES * \
326 get_value(unsigned int *x, unsigned n_values)
328 unsigned int rem = *x % n_values;
333 static struct cls_rule *
334 lookup_with_include_bits(const struct classifier *cls,
335 const flow_t *flow, int include)
339 return classifier_lookup_wild(cls, flow);
341 return classifier_lookup_exact(cls, flow);
342 case CLS_INC_WILD | CLS_INC_EXACT:
343 return classifier_lookup(cls, flow);
350 compare_classifiers(struct classifier *cls, struct tcls *tcls)
354 assert(classifier_count(cls) == tcls->n_rules);
355 assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
356 for (i = 0; i < N_FLOW_VALUES; i++) {
357 struct cls_rule *cr0, *cr1;
363 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
364 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
365 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
366 flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
367 flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
368 N_DL_VLAN_PCP_VALUES)];
369 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
370 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
371 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
372 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
374 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
376 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
377 flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
379 for (include = 1; include <= 3; include++) {
380 cr0 = lookup_with_include_bits(cls, &flow, include);
381 cr1 = tcls_lookup(tcls, &flow, include);
382 assert((cr0 == NULL) == (cr1 == NULL));
384 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
385 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
387 assert(flow_equal(&cr0->flow, &cr1->flow));
388 assert(cr0->wc.wildcards == cr1->wc.wildcards);
389 assert(cr0->priority == cr1->priority);
390 /* Skip nw_src_mask and nw_dst_mask, because they are derived
391 * members whose values are used only for optimization. */
392 assert(tr0->aux == tr1->aux);
399 free_rule(struct cls_rule *cls_rule, void *cls)
401 classifier_remove(cls, cls_rule);
402 free(test_rule_from_cls_rule(cls_rule));
406 destroy_classifier(struct classifier *cls)
408 classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
409 classifier_destroy(cls);
413 check_tables(const struct classifier *cls,
414 int n_tables, int n_buckets, int n_rules)
416 int found_tables = 0;
417 int found_buckets = 0;
421 BUILD_ASSERT(CLS_N_FIELDS == ARRAY_SIZE(cls->tables));
422 for (i = 0; i < CLS_N_FIELDS; i++) {
423 const struct cls_bucket *bucket;
424 if (!hmap_is_empty(&cls->tables[i])) {
427 HMAP_FOR_EACH (bucket, struct cls_bucket, hmap_node, &cls->tables[i]) {
429 assert(!list_is_empty(&bucket->rules));
430 found_rules += list_size(&bucket->rules);
434 if (!hmap_is_empty(&cls->exact_table)) {
437 found_rules += hmap_count(&cls->exact_table);
440 assert(n_tables == -1 || found_tables == n_tables);
441 assert(n_rules == -1 || found_rules == n_rules);
442 assert(n_buckets == -1 || found_buckets == n_buckets);
445 static struct test_rule *
446 make_rule(int wc_fields, unsigned int priority, int value_pat)
448 const struct cls_field *f;
449 struct test_rule *rule;
454 memset(&flow, 0, sizeof flow);
455 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
456 int f_idx = f - cls_fields;
457 if (wc_fields & (1u << f_idx)) {
458 wildcards |= f->wildcards;
460 int value_idx = (value_pat & (1u << f_idx)) != 0;
461 memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
465 rule = xzalloc(sizeof *rule);
466 cls_rule_from_flow(&rule->cls_rule, &flow, wildcards,
467 !wildcards ? UINT_MAX : priority);
472 shuffle(unsigned int *p, size_t n)
474 for (; n > 1; n--, p++) {
475 unsigned int *q = &p[rand() % n];
476 unsigned int tmp = *p;
482 /* Tests an empty classifier. */
484 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
486 struct classifier cls;
489 classifier_init(&cls);
491 assert(classifier_is_empty(&cls));
492 assert(tcls_is_empty(&tcls));
493 compare_classifiers(&cls, &tcls);
494 classifier_destroy(&cls);
498 /* Destroys a null classifier. */
500 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
502 classifier_destroy(NULL);
505 /* Tests classification with one rule at a time. */
507 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
509 unsigned int wc_fields; /* Hilarious. */
511 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
512 struct classifier cls;
513 struct test_rule *rule, *tcls_rule;
516 rule = make_rule(wc_fields,
517 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
519 classifier_init(&cls);
522 tcls_rule = tcls_insert(&tcls, rule);
524 assert(!classifier_insert(&cls, &rule->cls_rule));
526 classifier_insert_exact(&cls, &rule->cls_rule);
528 check_tables(&cls, 1, 1, 1);
529 compare_classifiers(&cls, &tcls);
531 classifier_remove(&cls, &rule->cls_rule);
532 tcls_remove(&tcls, tcls_rule);
533 assert(classifier_is_empty(&cls));
534 assert(tcls_is_empty(&tcls));
535 compare_classifiers(&cls, &tcls);
538 classifier_destroy(&cls);
543 /* Tests replacing one rule by another. */
545 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
547 unsigned int wc_fields;
549 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
550 struct classifier cls;
551 struct test_rule *rule1;
552 struct test_rule *rule2;
555 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
556 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
560 classifier_init(&cls);
562 tcls_insert(&tcls, rule1);
563 assert(!classifier_insert(&cls, &rule1->cls_rule));
564 check_tables(&cls, 1, 1, 1);
565 compare_classifiers(&cls, &tcls);
569 tcls_insert(&tcls, rule2);
570 assert(test_rule_from_cls_rule(
571 classifier_insert(&cls, &rule2->cls_rule)) == rule1);
573 check_tables(&cls, 1, 1, 1);
574 compare_classifiers(&cls, &tcls);
576 destroy_classifier(&cls);
581 table_mask(int table)
583 return ((1u << CLS_N_FIELDS) - 1) & ~((1u << table) - 1);
587 random_wcf_in_table(int table, int seed)
589 int wc_fields = (1u << table) | hash_int(seed, 0);
590 return wc_fields & table_mask(table);
593 /* Tests classification with two rules at a time that fall into the same
596 test_two_rules_in_one_bucket(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
598 int table, rel_pri, wcf_pat, value_pat;
600 for (table = 0; table <= CLS_N_FIELDS; table++) {
601 for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
602 for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
603 int n_value_pats = table == CLS_N_FIELDS - 1 ? 1 : 2;
604 for (value_pat = 0; value_pat < n_value_pats; value_pat++) {
605 struct test_rule *rule1, *tcls_rule1;
606 struct test_rule *rule2, *tcls_rule2;
607 struct test_rule *displaced_rule;
608 struct classifier cls;
610 unsigned int pri1, pri2;
613 if (table != CLS_F_IDX_EXACT) {
614 /* We can use identical priorities in this test because
615 * the classifier always chooses the rule added later
616 * for equal-priority rules that fall into the same
618 pri1 = table * 257 + 50;
619 pri2 = pri1 + rel_pri;
622 ? random_wcf_in_table(table, pri1)
625 ? random_wcf_in_table(table, pri2)
628 wcf1 &= ~(1u << (CLS_N_FIELDS - 1));
629 wcf2 &= ~(1u << (CLS_N_FIELDS - 1));
632 /* This classifier always puts exact-match rules at
633 * maximum priority. */
634 pri1 = pri2 = UINT_MAX;
636 /* No wildcard fields. */
640 rule1 = make_rule(wcf1, pri1, 0);
641 rule2 = make_rule(wcf2, pri2,
642 value_pat << (CLS_N_FIELDS - 1));
644 classifier_init(&cls);
647 tcls_rule1 = tcls_insert(&tcls, rule1);
648 tcls_rule2 = tcls_insert(&tcls, rule2);
649 assert(!classifier_insert(&cls, &rule1->cls_rule));
650 displaced_rule = test_rule_from_cls_rule(
651 classifier_insert(&cls, &rule2->cls_rule));
652 if (wcf1 != wcf2 || pri1 != pri2 || value_pat) {
653 assert(!displaced_rule);
655 check_tables(&cls, 1, 1, 2);
656 compare_classifiers(&cls, &tcls);
658 classifier_remove(&cls, &rule1->cls_rule);
659 tcls_remove(&tcls, tcls_rule1);
660 check_tables(&cls, 1, 1, 1);
661 compare_classifiers(&cls, &tcls);
663 assert(displaced_rule == rule1);
664 check_tables(&cls, 1, 1, 1);
665 compare_classifiers(&cls, &tcls);
669 classifier_remove(&cls, &rule2->cls_rule);
670 tcls_remove(&tcls, tcls_rule2);
671 compare_classifiers(&cls, &tcls);
674 destroy_classifier(&cls);
682 /* Tests classification with two rules at a time that fall into the same
683 * table but different buckets. */
685 test_two_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
687 int table, rel_pri, wcf_pat;
689 /* Skip tables 0 and CLS_F_IDX_EXACT because they have one bucket. */
690 for (table = 1; table < CLS_N_FIELDS; table++) {
691 for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
692 for (wcf_pat = 0; wcf_pat < 5; wcf_pat++) {
693 struct test_rule *rule1, *tcls_rule1;
694 struct test_rule *rule2, *tcls_rule2;
695 struct classifier cls;
697 unsigned int pri1, pri2;
699 int value_mask, value_pat1, value_pat2;
702 /* We can use identical priorities in this test because the
703 * classifier always chooses the rule added later for
704 * equal-priority rules that fall into the same table. */
705 pri1 = table * 257 + 50;
706 pri2 = pri1 + rel_pri;
709 wcf1 = wcf2 = random_wcf_in_table(table, pri1);
712 ? random_wcf_in_table(table, pri1)
715 ? random_wcf_in_table(table, pri2)
719 /* Generate value patterns that will put the two rules into
720 * different buckets. */
721 value_mask = ((1u << table) - 1);
722 value_pat1 = hash_int(pri1, 1) & value_mask;
725 value_pat2 = (hash_int(pri2, i++) & value_mask);
726 } while (value_pat1 == value_pat2);
727 rule1 = make_rule(wcf1, pri1, value_pat1);
728 rule2 = make_rule(wcf2, pri2, value_pat2);
730 classifier_init(&cls);
733 tcls_rule1 = tcls_insert(&tcls, rule1);
734 tcls_rule2 = tcls_insert(&tcls, rule2);
735 assert(!classifier_insert(&cls, &rule1->cls_rule));
736 assert(!classifier_insert(&cls, &rule2->cls_rule));
737 check_tables(&cls, 1, 2, 2);
738 compare_classifiers(&cls, &tcls);
740 classifier_remove(&cls, &rule1->cls_rule);
741 tcls_remove(&tcls, tcls_rule1);
742 check_tables(&cls, 1, 1, 1);
743 compare_classifiers(&cls, &tcls);
746 classifier_remove(&cls, &rule2->cls_rule);
747 tcls_remove(&tcls, tcls_rule2);
748 compare_classifiers(&cls, &tcls);
751 classifier_destroy(&cls);
758 /* Tests classification with two rules at a time that fall into different
761 test_two_rules_in_different_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
763 int table1, table2, rel_pri, wcf_pat;
765 for (table1 = 0; table1 < CLS_N_FIELDS; table1++) {
766 for (table2 = table1 + 1; table2 <= CLS_N_FIELDS; table2++) {
767 for (rel_pri = 0; rel_pri < 2; rel_pri++) {
768 for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
769 struct test_rule *rule1, *tcls_rule1;
770 struct test_rule *rule2, *tcls_rule2;
771 struct classifier cls;
773 unsigned int pri1, pri2;
776 /* We must use unique priorities in this test because the
777 * classifier makes the rule choice undefined for rules of
778 * equal priority that fall into different tables. (In
779 * practice, lower-numbered tables win.) */
780 pri1 = table1 * 257 + 50;
781 pri2 = rel_pri ? pri1 - 1 : pri1 + 1;
784 ? random_wcf_in_table(table1, pri1)
787 ? random_wcf_in_table(table2, pri2)
790 if (table2 == CLS_F_IDX_EXACT) {
795 rule1 = make_rule(wcf1, pri1, 0);
796 rule2 = make_rule(wcf2, pri2, 0);
798 classifier_init(&cls);
801 tcls_rule1 = tcls_insert(&tcls, rule1);
802 tcls_rule2 = tcls_insert(&tcls, rule2);
803 assert(!classifier_insert(&cls, &rule1->cls_rule));
804 assert(!classifier_insert(&cls, &rule2->cls_rule));
805 check_tables(&cls, 2, 2, 2);
806 compare_classifiers(&cls, &tcls);
808 classifier_remove(&cls, &rule1->cls_rule);
809 tcls_remove(&tcls, tcls_rule1);
810 check_tables(&cls, 1, 1, 1);
811 compare_classifiers(&cls, &tcls);
814 classifier_remove(&cls, &rule2->cls_rule);
815 tcls_remove(&tcls, tcls_rule2);
816 compare_classifiers(&cls, &tcls);
819 classifier_destroy(&cls);
827 /* Tests classification with many rules at a time that fall into the same
828 * bucket but have unique priorities (and various wildcards). */
830 test_many_rules_in_one_bucket(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
832 enum { MAX_RULES = 50 };
833 int iteration, table;
835 for (iteration = 0; iteration < 3; iteration++) {
836 for (table = 0; table <= CLS_N_FIELDS; table++) {
837 unsigned int priorities[MAX_RULES];
838 struct classifier cls;
842 srand(hash_int(table, iteration));
843 for (i = 0; i < MAX_RULES; i++) {
844 priorities[i] = i * 129;
846 shuffle(priorities, ARRAY_SIZE(priorities));
848 classifier_init(&cls);
851 for (i = 0; i < MAX_RULES; i++) {
852 struct test_rule *rule;
853 unsigned int priority = priorities[i];
856 wcf = random_wcf_in_table(table, priority);
857 rule = make_rule(wcf, priority,
858 table == CLS_F_IDX_EXACT ? i : 1234);
859 tcls_insert(&tcls, rule);
860 assert(!classifier_insert(&cls, &rule->cls_rule));
861 check_tables(&cls, 1, 1, i + 1);
862 compare_classifiers(&cls, &tcls);
865 destroy_classifier(&cls);
871 /* Tests classification with many rules at a time that fall into the same
872 * table but random buckets. */
874 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
876 enum { MAX_RULES = 50 };
877 int iteration, table;
879 for (iteration = 0; iteration < 3; iteration++) {
880 for (table = 0; table < CLS_N_FIELDS; table++) {
881 unsigned int priorities[MAX_RULES];
882 struct classifier cls;
886 srand(hash_int(table, iteration));
887 for (i = 0; i < MAX_RULES; i++) {
888 priorities[i] = i * 129;
890 shuffle(priorities, ARRAY_SIZE(priorities));
892 classifier_init(&cls);
895 for (i = 0; i < MAX_RULES; i++) {
896 struct test_rule *rule;
897 unsigned int priority = priorities[i];
900 wcf = random_wcf_in_table(table, priority);
901 rule = make_rule(wcf, priority, hash_int(priority, 1));
902 tcls_insert(&tcls, rule);
903 assert(!classifier_insert(&cls, &rule->cls_rule));
904 check_tables(&cls, 1, -1, i + 1);
905 compare_classifiers(&cls, &tcls);
908 destroy_classifier(&cls);
914 /* Tests classification with many rules at a time that fall into random buckets
915 * in random tables. */
917 test_many_rules_in_different_tables(int argc OVS_UNUSED,
918 char *argv[] OVS_UNUSED)
920 enum { MAX_RULES = 50 };
923 for (iteration = 0; iteration < 30; iteration++) {
924 unsigned int priorities[MAX_RULES];
925 struct classifier cls;
930 for (i = 0; i < MAX_RULES; i++) {
931 priorities[i] = i * 129;
933 shuffle(priorities, ARRAY_SIZE(priorities));
935 classifier_init(&cls);
938 for (i = 0; i < MAX_RULES; i++) {
939 struct test_rule *rule;
940 unsigned int priority = priorities[i];
941 int table = rand() % (CLS_N_FIELDS + 1);
942 int wcf = random_wcf_in_table(table, rand());
943 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
944 rule = make_rule(wcf, priority, value_pat);
945 tcls_insert(&tcls, rule);
946 assert(!classifier_insert(&cls, &rule->cls_rule));
947 check_tables(&cls, -1, -1, i + 1);
948 compare_classifiers(&cls, &tcls);
951 while (!classifier_is_empty(&cls)) {
952 struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
953 sizeof(struct test_rule));
954 int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
955 include |= (rule->cls_rule.wc.wildcards
956 ? CLS_INC_WILD : CLS_INC_EXACT);
957 classifier_for_each_match(&cls, &rule->cls_rule, include,
959 tcls_delete_matches(&tcls, &rule->cls_rule, include);
960 compare_classifiers(&cls, &tcls);
964 destroy_classifier(&cls);
970 main(int argc, char *argv[])
972 static const struct command all_commands[] = {
973 { "empty", 0, 0, test_empty },
974 { "destroy-null", 0, 0, test_destroy_null },
975 { "single-rule", 0, 0, test_single_rule },
976 { "rule-replacement", 0, 0, test_rule_replacement },
977 { "two-rules-in-one-bucket", 0, 0, test_two_rules_in_one_bucket },
978 { "two-rules-in-one-table", 0, 0, test_two_rules_in_one_table },
979 { "two-rules-in-different-tables", 0, 0,
980 test_two_rules_in_different_tables },
981 { "many-rules-in-one-bucket", 0, 0,
982 test_many_rules_in_one_bucket },
983 { "many-rules-in-one-table", 0, 0, test_many_rules_in_one_table },
984 { "many-rules-in-different-tables", 0, 0,
985 test_many_rules_in_different_tables },
986 { NULL, 0, 0, NULL },
989 set_program_name(argv[0]);
991 parse_test_options(argc, argv, all_commands);
992 run_command(argc - 1, argv + 1, all_commands);