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"
40 int aux; /* Auxiliary data. */
41 struct cls_rule cls_rule; /* Classifier rule data. */
44 static struct test_rule *
45 test_rule_from_cls_rule(const struct cls_rule *rule)
47 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
50 /* Trivial (linear) classifier. */
53 size_t allocated_rules;
54 struct test_rule **rules;
58 tcls_init(struct tcls *tcls)
61 tcls->allocated_rules = 0;
66 tcls_destroy(struct tcls *tcls)
71 for (i = 0; i < tcls->n_rules; i++) {
79 tcls_count_exact(const struct tcls *tcls)
85 for (i = 0; i < tcls->n_rules; i++) {
86 n_exact += tcls->rules[i]->cls_rule.wc.wildcards == 0;
92 tcls_is_empty(const struct tcls *tcls)
94 return tcls->n_rules == 0;
97 static struct test_rule *
98 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
102 assert(rule->cls_rule.wc.wildcards || rule->cls_rule.priority == UINT_MAX);
103 for (i = 0; i < tcls->n_rules; i++) {
104 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
105 if (pos->priority == rule->cls_rule.priority
106 && pos->wc.wildcards == rule->cls_rule.wc.wildcards
107 && flow_equal(&pos->flow, &rule->cls_rule.flow)) {
109 * XXX flow_equal should ignore wildcarded fields */
110 free(tcls->rules[i]);
111 tcls->rules[i] = xmemdup(rule, sizeof *rule);
112 return tcls->rules[i];
113 } else if (pos->priority < rule->cls_rule.priority) {
118 if (tcls->n_rules >= tcls->allocated_rules) {
119 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
120 sizeof *tcls->rules);
122 if (i != tcls->n_rules) {
123 memmove(&tcls->rules[i + 1], &tcls->rules[i],
124 sizeof *tcls->rules * (tcls->n_rules - i));
126 tcls->rules[i] = xmemdup(rule, sizeof *rule);
128 return tcls->rules[i];
132 tcls_remove(struct tcls *cls, const struct test_rule *rule)
136 for (i = 0; i < cls->n_rules; i++) {
137 struct test_rule *pos = cls->rules[i];
140 memmove(&cls->rules[i], &cls->rules[i + 1],
141 sizeof *cls->rules * (cls->n_rules - i - 1));
150 read_uint32(const void *p)
153 memcpy(&x, p, sizeof x);
158 match(const struct cls_rule *wild, const struct flow *fixed)
162 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
163 const struct cls_field *f = &cls_fields[f_idx];
164 void *wild_field = (char *) &wild->flow + f->ofs;
165 void *fixed_field = (char *) fixed + f->ofs;
167 if ((wild->wc.wildcards & f->wildcards) == f->wildcards ||
168 !memcmp(wild_field, fixed_field, f->len)) {
169 /* Definite match. */
173 if (wild->wc.wildcards & f->wildcards) {
174 uint32_t test = read_uint32(wild_field);
175 uint32_t ip = read_uint32(fixed_field);
176 int shift = (f_idx == CLS_F_IDX_NW_SRC
177 ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
178 uint32_t mask = flow_nw_bits_to_mask(wild->wc.wildcards, shift);
179 if (!((test ^ ip) & mask)) {
189 static struct cls_rule *
190 tcls_lookup(const struct tcls *cls, const struct flow *flow, int include)
194 for (i = 0; i < cls->n_rules; i++) {
195 struct test_rule *pos = cls->rules[i];
196 uint32_t wildcards = pos->cls_rule.wc.wildcards;
197 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
198 && match(&pos->cls_rule, flow)) {
199 return &pos->cls_rule;
206 tcls_delete_matches(struct tcls *cls,
207 const struct cls_rule *target,
212 for (i = 0; i < cls->n_rules; ) {
213 struct test_rule *pos = cls->rules[i];
214 uint32_t wildcards = pos->cls_rule.wc.wildcards;
215 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
216 && match(target, &pos->cls_rule.flow)) {
217 tcls_remove(cls, pos);
224 #ifdef WORDS_BIGENDIAN
225 #define T_HTONL(VALUE) ((uint32_t) (VALUE))
226 #define T_HTONS(VALUE) ((uint32_t) (VALUE))
228 #define T_HTONL(VALUE) (((((uint32_t) (VALUE)) & 0x000000ff) << 24) | \
229 ((((uint32_t) (VALUE)) & 0x0000ff00) << 8) | \
230 ((((uint32_t) (VALUE)) & 0x00ff0000) >> 8) | \
231 ((((uint32_t) (VALUE)) & 0xff000000) >> 24))
232 #define T_HTONS(VALUE) (((((uint16_t) (VALUE)) & 0xff00) >> 8) | \
233 ((((uint16_t) (VALUE)) & 0x00ff) << 8))
236 static uint32_t nw_src_values[] = { T_HTONL(0xc0a80001),
237 T_HTONL(0xc0a04455) };
238 static uint32_t nw_dst_values[] = { T_HTONL(0xc0a80002),
239 T_HTONL(0xc0a04455) };
240 static uint32_t tun_id_values[] = { 0, 0xffff0000 };
241 static uint16_t in_port_values[] = { T_HTONS(1), T_HTONS(OFPP_LOCAL) };
242 static uint16_t dl_vlan_values[] = { T_HTONS(101), T_HTONS(0) };
243 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
244 static uint16_t dl_type_values[]
245 = { T_HTONS(ETH_TYPE_IP), T_HTONS(ETH_TYPE_ARP) };
246 static uint16_t tp_src_values[] = { T_HTONS(49362), T_HTONS(80) };
247 static uint16_t tp_dst_values[] = { T_HTONS(6667), T_HTONS(22) };
248 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
249 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
250 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
251 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
252 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
253 static uint8_t nw_tos_values[] = { 49, 0 };
255 static void *values[CLS_N_FIELDS][2];
260 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
261 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
263 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
264 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
266 values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
267 values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
269 values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
270 values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1];
272 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
273 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
275 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
276 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
278 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
279 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
281 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
282 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
284 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
285 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
287 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
288 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
290 values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0];
291 values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1];
293 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
294 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
296 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
297 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
300 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
301 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
302 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
303 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
304 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
305 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
306 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
307 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
308 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
309 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
310 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
311 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
312 #define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values)
314 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
319 N_DL_VLAN_PCP_VALUES * \
325 N_NW_PROTO_VALUES * \
329 get_value(unsigned int *x, unsigned n_values)
331 unsigned int rem = *x % n_values;
336 static struct cls_rule *
337 lookup_with_include_bits(const struct classifier *cls,
338 const struct flow *flow, int include)
342 return classifier_lookup_wild(cls, flow);
344 return classifier_lookup_exact(cls, flow);
345 case CLS_INC_WILD | CLS_INC_EXACT:
346 return classifier_lookup(cls, flow);
353 compare_classifiers(struct classifier *cls, struct tcls *tcls)
355 static const int confidence = 500;
358 assert(classifier_count(cls) == tcls->n_rules);
359 assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
360 for (i = 0; i < confidence; i++) {
361 struct cls_rule *cr0, *cr1;
366 x = rand () % N_FLOW_VALUES;
367 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
368 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
369 flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
370 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
371 flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
372 flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
373 N_DL_VLAN_PCP_VALUES)];
374 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
375 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
376 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
377 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
379 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
381 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
382 flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
384 for (include = 1; include <= 3; include++) {
385 cr0 = lookup_with_include_bits(cls, &flow, include);
386 cr1 = tcls_lookup(tcls, &flow, include);
387 assert((cr0 == NULL) == (cr1 == NULL));
389 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
390 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
392 assert(flow_equal(&cr0->flow, &cr1->flow));
393 assert(cr0->wc.wildcards == cr1->wc.wildcards);
394 assert(cr0->priority == cr1->priority);
395 /* Skip nw_src_mask and nw_dst_mask, because they are derived
396 * members whose values are used only for optimization. */
397 assert(tr0->aux == tr1->aux);
404 free_rule(struct cls_rule *cls_rule, void *cls)
406 classifier_remove(cls, cls_rule);
407 free(test_rule_from_cls_rule(cls_rule));
411 destroy_classifier(struct classifier *cls)
413 classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
414 classifier_destroy(cls);
418 check_tables(const struct classifier *cls,
419 int n_tables, int n_buckets, int n_rules)
421 int found_tables = 0;
422 int found_buckets = 0;
426 BUILD_ASSERT(CLS_N_FIELDS == ARRAY_SIZE(cls->tables));
427 for (i = 0; i < CLS_N_FIELDS; i++) {
428 const struct cls_bucket *bucket;
429 if (!hmap_is_empty(&cls->tables[i])) {
432 HMAP_FOR_EACH (bucket, hmap_node, &cls->tables[i]) {
434 assert(!list_is_empty(&bucket->rules));
435 found_rules += list_size(&bucket->rules);
439 if (!hmap_is_empty(&cls->exact_table)) {
442 found_rules += hmap_count(&cls->exact_table);
445 assert(n_tables == -1 || found_tables == n_tables);
446 assert(n_rules == -1 || found_rules == n_rules);
447 assert(n_buckets == -1 || found_buckets == n_buckets);
450 static struct test_rule *
451 make_rule(int wc_fields, unsigned int priority, int value_pat)
453 const struct cls_field *f;
454 struct test_rule *rule;
459 memset(&flow, 0, sizeof flow);
460 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
461 int f_idx = f - cls_fields;
462 if (wc_fields & (1u << f_idx)) {
463 wildcards |= f->wildcards;
465 int value_idx = (value_pat & (1u << f_idx)) != 0;
466 memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
470 rule = xzalloc(sizeof *rule);
471 cls_rule_from_flow(&flow, wildcards, !wildcards ? UINT_MAX : priority,
477 shuffle(unsigned int *p, size_t n)
479 for (; n > 1; n--, p++) {
480 unsigned int *q = &p[rand() % n];
481 unsigned int tmp = *p;
487 /* Tests an empty classifier. */
489 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
491 struct classifier cls;
494 classifier_init(&cls);
496 assert(classifier_is_empty(&cls));
497 assert(tcls_is_empty(&tcls));
498 compare_classifiers(&cls, &tcls);
499 classifier_destroy(&cls);
503 /* Destroys a null classifier. */
505 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
507 classifier_destroy(NULL);
510 /* Tests classification with one rule at a time. */
512 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
514 unsigned int wc_fields; /* Hilarious. */
516 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
517 struct classifier cls;
518 struct test_rule *rule, *tcls_rule;
521 rule = make_rule(wc_fields,
522 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
524 classifier_init(&cls);
527 tcls_rule = tcls_insert(&tcls, rule);
529 assert(!classifier_insert(&cls, &rule->cls_rule));
531 classifier_insert_exact(&cls, &rule->cls_rule);
533 check_tables(&cls, 1, 1, 1);
534 compare_classifiers(&cls, &tcls);
536 classifier_remove(&cls, &rule->cls_rule);
537 tcls_remove(&tcls, tcls_rule);
538 assert(classifier_is_empty(&cls));
539 assert(tcls_is_empty(&tcls));
540 compare_classifiers(&cls, &tcls);
543 classifier_destroy(&cls);
548 /* Tests replacing one rule by another. */
550 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
552 unsigned int wc_fields;
554 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
555 struct classifier cls;
556 struct test_rule *rule1;
557 struct test_rule *rule2;
560 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
561 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
565 classifier_init(&cls);
567 tcls_insert(&tcls, rule1);
568 assert(!classifier_insert(&cls, &rule1->cls_rule));
569 check_tables(&cls, 1, 1, 1);
570 compare_classifiers(&cls, &tcls);
574 tcls_insert(&tcls, rule2);
575 assert(test_rule_from_cls_rule(
576 classifier_insert(&cls, &rule2->cls_rule)) == rule1);
578 check_tables(&cls, 1, 1, 1);
579 compare_classifiers(&cls, &tcls);
581 destroy_classifier(&cls);
586 table_mask(int table)
588 return ((1u << CLS_N_FIELDS) - 1) & ~((1u << table) - 1);
592 random_wcf_in_table(int table, int seed)
594 int wc_fields = (1u << table) | hash_int(seed, 0);
595 return wc_fields & table_mask(table);
598 /* Tests classification with two rules at a time that fall into the same
601 test_two_rules_in_one_bucket(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
603 int table, rel_pri, wcf_pat, value_pat;
605 for (table = 0; table <= CLS_N_FIELDS; table++) {
606 for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
607 for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
608 int n_value_pats = table == CLS_N_FIELDS - 1 ? 1 : 2;
609 for (value_pat = 0; value_pat < n_value_pats; value_pat++) {
610 struct test_rule *rule1, *tcls_rule1;
611 struct test_rule *rule2, *tcls_rule2;
612 struct test_rule *displaced_rule;
613 struct classifier cls;
615 unsigned int pri1, pri2;
618 if (table != CLS_F_IDX_EXACT) {
619 /* We can use identical priorities in this test because
620 * the classifier always chooses the rule added later
621 * for equal-priority rules that fall into the same
623 pri1 = table * 257 + 50;
624 pri2 = pri1 + rel_pri;
627 ? random_wcf_in_table(table, pri1)
630 ? random_wcf_in_table(table, pri2)
633 wcf1 &= ~(1u << (CLS_N_FIELDS - 1));
634 wcf2 &= ~(1u << (CLS_N_FIELDS - 1));
637 /* This classifier always puts exact-match rules at
638 * maximum priority. */
639 pri1 = pri2 = UINT_MAX;
641 /* No wildcard fields. */
645 rule1 = make_rule(wcf1, pri1, 0);
646 rule2 = make_rule(wcf2, pri2,
647 value_pat << (CLS_N_FIELDS - 1));
649 classifier_init(&cls);
652 tcls_rule1 = tcls_insert(&tcls, rule1);
653 tcls_rule2 = tcls_insert(&tcls, rule2);
654 assert(!classifier_insert(&cls, &rule1->cls_rule));
655 displaced_rule = test_rule_from_cls_rule(
656 classifier_insert(&cls, &rule2->cls_rule));
657 if (wcf1 != wcf2 || pri1 != pri2 || value_pat) {
658 assert(!displaced_rule);
660 check_tables(&cls, 1, 1, 2);
661 compare_classifiers(&cls, &tcls);
663 classifier_remove(&cls, &rule1->cls_rule);
664 tcls_remove(&tcls, tcls_rule1);
665 check_tables(&cls, 1, 1, 1);
666 compare_classifiers(&cls, &tcls);
668 assert(displaced_rule == rule1);
669 check_tables(&cls, 1, 1, 1);
670 compare_classifiers(&cls, &tcls);
674 classifier_remove(&cls, &rule2->cls_rule);
675 tcls_remove(&tcls, tcls_rule2);
676 compare_classifiers(&cls, &tcls);
679 destroy_classifier(&cls);
687 /* Tests classification with two rules at a time that fall into the same
688 * table but different buckets. */
690 test_two_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
692 int table, rel_pri, wcf_pat;
694 /* Skip tables 0 and CLS_F_IDX_EXACT because they have one bucket. */
695 for (table = 1; table < CLS_N_FIELDS; table++) {
696 for (rel_pri = -1; rel_pri <= +1; rel_pri++) {
697 for (wcf_pat = 0; wcf_pat < 5; wcf_pat++) {
698 struct test_rule *rule1, *tcls_rule1;
699 struct test_rule *rule2, *tcls_rule2;
700 struct classifier cls;
702 unsigned int pri1, pri2;
704 int value_mask, value_pat1, value_pat2;
707 /* We can use identical priorities in this test because the
708 * classifier always chooses the rule added later for
709 * equal-priority rules that fall into the same table. */
710 pri1 = table * 257 + 50;
711 pri2 = pri1 + rel_pri;
714 wcf1 = wcf2 = random_wcf_in_table(table, pri1);
717 ? random_wcf_in_table(table, pri1)
720 ? random_wcf_in_table(table, pri2)
724 /* Generate value patterns that will put the two rules into
725 * different buckets. */
726 value_mask = ((1u << table) - 1);
727 value_pat1 = hash_int(pri1, 1) & value_mask;
730 value_pat2 = (hash_int(pri2, i++) & value_mask);
731 } while (value_pat1 == value_pat2);
732 rule1 = make_rule(wcf1, pri1, value_pat1);
733 rule2 = make_rule(wcf2, pri2, value_pat2);
735 classifier_init(&cls);
738 tcls_rule1 = tcls_insert(&tcls, rule1);
739 tcls_rule2 = tcls_insert(&tcls, rule2);
740 assert(!classifier_insert(&cls, &rule1->cls_rule));
741 assert(!classifier_insert(&cls, &rule2->cls_rule));
742 check_tables(&cls, 1, 2, 2);
743 compare_classifiers(&cls, &tcls);
745 classifier_remove(&cls, &rule1->cls_rule);
746 tcls_remove(&tcls, tcls_rule1);
747 check_tables(&cls, 1, 1, 1);
748 compare_classifiers(&cls, &tcls);
751 classifier_remove(&cls, &rule2->cls_rule);
752 tcls_remove(&tcls, tcls_rule2);
753 compare_classifiers(&cls, &tcls);
756 classifier_destroy(&cls);
763 /* Tests classification with two rules at a time that fall into different
766 test_two_rules_in_different_tables(int argc OVS_UNUSED,
767 char *argv[] OVS_UNUSED)
769 int table1, table2, rel_pri, wcf_pat;
771 for (table1 = 0; table1 < CLS_N_FIELDS; table1++) {
772 for (table2 = table1 + 1; table2 <= CLS_N_FIELDS; table2++) {
773 for (rel_pri = 0; rel_pri < 2; rel_pri++) {
774 for (wcf_pat = 0; wcf_pat < 4; wcf_pat++) {
775 struct test_rule *rule1, *tcls_rule1;
776 struct test_rule *rule2, *tcls_rule2;
777 struct classifier cls;
779 unsigned int pri1, pri2;
782 /* We must use unique priorities in this test because the
783 * classifier makes the rule choice undefined for rules of
784 * equal priority that fall into different tables. (In
785 * practice, lower-numbered tables win.) */
786 pri1 = table1 * 257 + 50;
787 pri2 = rel_pri ? pri1 - 1 : pri1 + 1;
790 ? random_wcf_in_table(table1, pri1)
793 ? random_wcf_in_table(table2, pri2)
796 if (table2 == CLS_F_IDX_EXACT) {
801 rule1 = make_rule(wcf1, pri1, 0);
802 rule2 = make_rule(wcf2, pri2, 0);
804 classifier_init(&cls);
807 tcls_rule1 = tcls_insert(&tcls, rule1);
808 tcls_rule2 = tcls_insert(&tcls, rule2);
809 assert(!classifier_insert(&cls, &rule1->cls_rule));
810 assert(!classifier_insert(&cls, &rule2->cls_rule));
811 check_tables(&cls, 2, 2, 2);
812 compare_classifiers(&cls, &tcls);
814 classifier_remove(&cls, &rule1->cls_rule);
815 tcls_remove(&tcls, tcls_rule1);
816 check_tables(&cls, 1, 1, 1);
817 compare_classifiers(&cls, &tcls);
820 classifier_remove(&cls, &rule2->cls_rule);
821 tcls_remove(&tcls, tcls_rule2);
822 compare_classifiers(&cls, &tcls);
825 classifier_destroy(&cls);
833 /* Tests classification with many rules at a time that fall into the same
834 * bucket but have unique priorities (and various wildcards). */
836 test_many_rules_in_one_bucket(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
838 enum { MAX_RULES = 50 };
839 int iteration, table;
841 for (iteration = 0; iteration < 3; iteration++) {
842 for (table = 0; table <= CLS_N_FIELDS; table++) {
843 unsigned int priorities[MAX_RULES];
844 struct classifier cls;
848 srand(hash_int(table, iteration));
849 for (i = 0; i < MAX_RULES; i++) {
850 priorities[i] = i * 129;
852 shuffle(priorities, ARRAY_SIZE(priorities));
854 classifier_init(&cls);
857 for (i = 0; i < MAX_RULES; i++) {
858 struct test_rule *rule;
859 unsigned int priority = priorities[i];
862 wcf = random_wcf_in_table(table, priority);
863 rule = make_rule(wcf, priority,
864 table == CLS_F_IDX_EXACT ? i : 1234);
865 tcls_insert(&tcls, rule);
866 assert(!classifier_insert(&cls, &rule->cls_rule));
867 check_tables(&cls, 1, 1, i + 1);
868 compare_classifiers(&cls, &tcls);
871 destroy_classifier(&cls);
877 /* Tests classification with many rules at a time that fall into the same
878 * table but random buckets. */
880 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
882 enum { MAX_RULES = 50 };
883 int iteration, table;
885 for (iteration = 0; iteration < 3; iteration++) {
886 for (table = 0; table < CLS_N_FIELDS; table++) {
887 unsigned int priorities[MAX_RULES];
888 struct classifier cls;
892 srand(hash_int(table, iteration));
893 for (i = 0; i < MAX_RULES; i++) {
894 priorities[i] = i * 129;
896 shuffle(priorities, ARRAY_SIZE(priorities));
898 classifier_init(&cls);
901 for (i = 0; i < MAX_RULES; i++) {
902 struct test_rule *rule;
903 unsigned int priority = priorities[i];
906 wcf = random_wcf_in_table(table, priority);
907 rule = make_rule(wcf, priority, hash_int(priority, 1));
908 tcls_insert(&tcls, rule);
909 assert(!classifier_insert(&cls, &rule->cls_rule));
910 check_tables(&cls, 1, -1, i + 1);
911 compare_classifiers(&cls, &tcls);
914 destroy_classifier(&cls);
920 /* Tests classification with many rules at a time that fall into random buckets
921 * in random tables. */
923 test_many_rules_in_different_tables(int argc OVS_UNUSED,
924 char *argv[] OVS_UNUSED)
926 enum { MAX_RULES = 50 };
929 for (iteration = 0; iteration < 30; iteration++) {
930 unsigned int priorities[MAX_RULES];
931 struct classifier cls;
936 for (i = 0; i < MAX_RULES; i++) {
937 priorities[i] = i * 129;
939 shuffle(priorities, ARRAY_SIZE(priorities));
941 classifier_init(&cls);
944 for (i = 0; i < MAX_RULES; i++) {
945 struct test_rule *rule;
946 unsigned int priority = priorities[i];
947 int table = rand() % (CLS_N_FIELDS + 1);
948 int wcf = random_wcf_in_table(table, rand());
949 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
950 rule = make_rule(wcf, priority, value_pat);
951 tcls_insert(&tcls, rule);
952 assert(!classifier_insert(&cls, &rule->cls_rule));
953 check_tables(&cls, -1, -1, i + 1);
954 compare_classifiers(&cls, &tcls);
957 while (!classifier_is_empty(&cls)) {
958 struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
959 sizeof(struct test_rule));
960 int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
961 include |= (rule->cls_rule.wc.wildcards
962 ? CLS_INC_WILD : CLS_INC_EXACT);
963 classifier_for_each_match(&cls, &rule->cls_rule, include,
965 tcls_delete_matches(&tcls, &rule->cls_rule, include);
966 compare_classifiers(&cls, &tcls);
970 destroy_classifier(&cls);
975 static const struct command commands[] = {
976 {"empty", 0, 0, test_empty},
977 {"destroy-null", 0, 0, test_destroy_null},
978 {"single-rule", 0, 0, test_single_rule},
979 {"rule-replacement", 0, 0, test_rule_replacement},
980 {"two-rules-in-one-bucket", 0, 0, test_two_rules_in_one_bucket},
981 {"two-rules-in-one-table", 0, 0, test_two_rules_in_one_table},
982 {"two-rules-in-different-tables", 0, 0,
983 test_two_rules_in_different_tables},
984 {"many-rules-in-one-bucket", 0, 0, test_many_rules_in_one_bucket},
985 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
986 {"many-rules-in-different-tables", 0, 0,
987 test_many_rules_in_different_tables},
992 main(int argc, char *argv[])
995 run_command(argc - 1, argv + 1, commands);