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 "byte-order.h"
33 #include "command-line.h"
40 /* Fields in a rule. */
42 /* struct flow all-caps */ \
43 /* wildcard bit(s) member name name */ \
44 /* ----------------- ----------- -------- */ \
45 CLS_FIELD(NXFW_TUN_ID, tun_id, TUN_ID) \
46 CLS_FIELD(OFPFW_NW_SRC_MASK, nw_src, NW_SRC) \
47 CLS_FIELD(OFPFW_NW_DST_MASK, nw_dst, NW_DST) \
48 CLS_FIELD(OFPFW_IN_PORT, in_port, IN_PORT) \
49 CLS_FIELD(OFPFW_DL_VLAN, dl_vlan, DL_VLAN) \
50 CLS_FIELD(OFPFW_DL_TYPE, dl_type, DL_TYPE) \
51 CLS_FIELD(OFPFW_TP_SRC, tp_src, TP_SRC) \
52 CLS_FIELD(OFPFW_TP_DST, tp_dst, TP_DST) \
53 CLS_FIELD(OFPFW_DL_SRC, dl_src, DL_SRC) \
54 CLS_FIELD(OFPFW_DL_DST | FWW_ETH_MCAST, \
56 CLS_FIELD(OFPFW_NW_PROTO, nw_proto, NW_PROTO) \
57 CLS_FIELD(OFPFW_DL_VLAN_PCP, dl_vlan_pcp, DL_VLAN_PCP) \
58 CLS_FIELD(OFPFW_NW_TOS, nw_tos, NW_TOS)
62 * (These are also indexed into struct classifier's 'tables' array.) */
64 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) CLS_F_IDX_##NAME,
70 /* Field information. */
72 int ofs; /* Offset in struct flow. */
73 int len; /* Length in bytes. */
74 uint32_t wildcards; /* OFPFW_* bit or bits for this field. */
75 const char *name; /* Name (for debugging). */
78 static const struct cls_field cls_fields[CLS_N_FIELDS] = {
79 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
80 { offsetof(struct flow, MEMBER), \
81 sizeof ((struct flow *)0)->MEMBER, \
89 int aux; /* Auxiliary data. */
90 struct cls_rule cls_rule; /* Classifier rule data. */
93 static struct test_rule *
94 test_rule_from_cls_rule(const struct cls_rule *rule)
96 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
99 /* Trivial (linear) classifier. */
102 size_t allocated_rules;
103 struct test_rule **rules;
107 tcls_init(struct tcls *tcls)
110 tcls->allocated_rules = 0;
115 tcls_destroy(struct tcls *tcls)
120 for (i = 0; i < tcls->n_rules; i++) {
121 free(tcls->rules[i]);
128 tcls_count_exact(const struct tcls *tcls)
134 for (i = 0; i < tcls->n_rules; i++) {
135 n_exact += tcls->rules[i]->cls_rule.wc.wildcards == 0;
141 tcls_is_empty(const struct tcls *tcls)
143 return tcls->n_rules == 0;
146 static struct test_rule *
147 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
151 assert(rule->cls_rule.wc.wildcards || rule->cls_rule.priority == UINT_MAX);
152 for (i = 0; i < tcls->n_rules; i++) {
153 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
154 if (pos->priority == rule->cls_rule.priority
155 && pos->wc.wildcards == rule->cls_rule.wc.wildcards
156 && flow_equal(&pos->flow, &rule->cls_rule.flow)) {
158 * XXX flow_equal should ignore wildcarded fields */
159 free(tcls->rules[i]);
160 tcls->rules[i] = xmemdup(rule, sizeof *rule);
161 return tcls->rules[i];
162 } else if (pos->priority < rule->cls_rule.priority) {
167 if (tcls->n_rules >= tcls->allocated_rules) {
168 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
169 sizeof *tcls->rules);
171 if (i != tcls->n_rules) {
172 memmove(&tcls->rules[i + 1], &tcls->rules[i],
173 sizeof *tcls->rules * (tcls->n_rules - i));
175 tcls->rules[i] = xmemdup(rule, sizeof *rule);
177 return tcls->rules[i];
181 tcls_remove(struct tcls *cls, const struct test_rule *rule)
185 for (i = 0; i < cls->n_rules; i++) {
186 struct test_rule *pos = cls->rules[i];
189 memmove(&cls->rules[i], &cls->rules[i + 1],
190 sizeof *cls->rules * (cls->n_rules - i - 1));
199 read_uint32(const void *p)
202 memcpy(&x, p, sizeof x);
207 match(const struct cls_rule *wild, const struct flow *fixed)
211 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
212 const struct cls_field *f = &cls_fields[f_idx];
213 void *wild_field = (char *) &wild->flow + f->ofs;
214 void *fixed_field = (char *) fixed + f->ofs;
216 if ((wild->wc.wildcards & f->wildcards) == f->wildcards ||
217 !memcmp(wild_field, fixed_field, f->len)) {
218 /* Definite match. */
222 if (wild->wc.wildcards & f->wildcards) {
223 uint32_t test = read_uint32(wild_field);
224 uint32_t ip = read_uint32(fixed_field);
225 int shift = (f_idx == CLS_F_IDX_NW_SRC
226 ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
227 uint32_t mask = flow_nw_bits_to_mask(wild->wc.wildcards, shift);
228 if (!((test ^ ip) & mask)) {
238 static struct cls_rule *
239 tcls_lookup(const struct tcls *cls, const struct flow *flow, int include)
243 for (i = 0; i < cls->n_rules; i++) {
244 struct test_rule *pos = cls->rules[i];
245 uint32_t wildcards = pos->cls_rule.wc.wildcards;
246 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
247 && match(&pos->cls_rule, flow)) {
248 return &pos->cls_rule;
255 tcls_delete_matches(struct tcls *cls,
256 const struct cls_rule *target,
261 for (i = 0; i < cls->n_rules; ) {
262 struct test_rule *pos = cls->rules[i];
263 uint32_t wildcards = pos->cls_rule.wc.wildcards;
264 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
265 && !flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
266 && match(target, &pos->cls_rule.flow)) {
267 tcls_remove(cls, pos);
274 static uint32_t nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
275 CONSTANT_HTONL(0xc0a04455) };
276 static uint32_t nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
277 CONSTANT_HTONL(0xc0a04455) };
278 static uint32_t tun_id_values[] = { 0, 0xffff0000 };
279 static uint16_t in_port_values[] = { CONSTANT_HTONS(1),
280 CONSTANT_HTONS(OFPP_LOCAL) };
281 static uint16_t dl_vlan_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
282 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
283 static uint16_t dl_type_values[]
284 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
285 static uint16_t tp_src_values[] = { CONSTANT_HTONS(49362),
286 CONSTANT_HTONS(80) };
287 static uint16_t tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
288 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
289 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
290 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
291 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
292 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
293 static uint8_t nw_tos_values[] = { 49, 0 };
295 static void *values[CLS_N_FIELDS][2];
300 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
301 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
303 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
304 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
306 values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
307 values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
309 values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
310 values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1];
312 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
313 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
315 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
316 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
318 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
319 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
321 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
322 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
324 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
325 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
327 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
328 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
330 values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0];
331 values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1];
333 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
334 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
336 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
337 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
340 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
341 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
342 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
343 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
344 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
345 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
346 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
347 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
348 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
349 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
350 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
351 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
352 #define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values)
354 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
359 N_DL_VLAN_PCP_VALUES * \
365 N_NW_PROTO_VALUES * \
369 get_value(unsigned int *x, unsigned n_values)
371 unsigned int rem = *x % n_values;
377 compare_classifiers(struct classifier *cls, struct tcls *tcls)
379 static const int confidence = 500;
382 assert(classifier_count(cls) == tcls->n_rules);
383 assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
384 for (i = 0; i < confidence; i++) {
385 struct cls_rule *cr0, *cr1;
390 x = rand () % N_FLOW_VALUES;
391 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
392 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
393 flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
394 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
395 flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
396 flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
397 N_DL_VLAN_PCP_VALUES)];
398 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
399 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
400 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
401 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
403 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
405 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
406 flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
408 for (include = 1; include <= 3; include++) {
409 cr0 = classifier_lookup(cls, &flow, include);
410 cr1 = tcls_lookup(tcls, &flow, include);
411 assert((cr0 == NULL) == (cr1 == NULL));
413 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
414 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
416 assert(flow_equal(&cr0->flow, &cr1->flow));
417 assert(cr0->wc.wildcards == cr1->wc.wildcards);
418 assert(cr0->priority == cr1->priority);
419 /* Skip nw_src_mask and nw_dst_mask, because they are derived
420 * members whose values are used only for optimization. */
421 assert(tr0->aux == tr1->aux);
428 free_rule(struct cls_rule *cls_rule, void *cls)
430 classifier_remove(cls, cls_rule);
431 free(test_rule_from_cls_rule(cls_rule));
435 destroy_classifier(struct classifier *cls)
437 classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
438 classifier_destroy(cls);
442 check_tables(const struct classifier *cls,
443 int n_tables, int n_rules, int n_dups)
445 const struct cls_table *table;
446 const struct test_rule *test_rule;
447 struct flow_wildcards exact_wc;
448 int found_tables = 0;
454 flow_wildcards_init_exact(&exact_wc);
455 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
456 bool is_exact = flow_wildcards_equal(&table->wc, &exact_wc);
457 const struct cls_rule *head;
459 assert(!hmap_is_empty(&table->rules));
462 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
463 unsigned int prev_priority = UINT_MAX;
464 const struct cls_rule *rule;
470 LIST_FOR_EACH (rule, list, &head->list) {
471 assert(rule->priority < prev_priority);
472 prev_priority = rule->priority;
478 assert(classifier_find_rule_exactly(cls, rule) == rule);
483 CLASSIFIER_FOR_EACH_EXACT_RULE (test_rule, cls_rule, cls) {
487 assert(found_tables == hmap_count(&cls->tables));
488 assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
489 assert(n_rules == -1 || found_rules == n_rules);
490 assert(n_dups == -1 || found_dups == n_dups);
491 assert(n_exact1 == n_exact2);
494 static struct test_rule *
495 make_rule(int wc_fields, unsigned int priority, int value_pat)
497 const struct cls_field *f;
498 struct flow_wildcards wc;
499 struct test_rule *rule;
504 memset(&flow, 0, sizeof flow);
505 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
506 int f_idx = f - cls_fields;
507 if (wc_fields & (1u << f_idx)) {
508 wildcards |= f->wildcards;
510 int value_idx = (value_pat & (1u << f_idx)) != 0;
511 memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
515 rule = xzalloc(sizeof *rule);
516 flow_wildcards_init(&wc, wildcards);
517 cls_rule_init(&flow, &wc, !wildcards ? UINT_MAX : priority,
523 shuffle(unsigned int *p, size_t n)
525 for (; n > 1; n--, p++) {
526 unsigned int *q = &p[rand() % n];
527 unsigned int tmp = *p;
533 /* Tests an empty classifier. */
535 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
537 struct classifier cls;
540 classifier_init(&cls);
542 assert(classifier_is_empty(&cls));
543 assert(tcls_is_empty(&tcls));
544 compare_classifiers(&cls, &tcls);
545 classifier_destroy(&cls);
549 /* Destroys a null classifier. */
551 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
553 classifier_destroy(NULL);
556 /* Tests classification with one rule at a time. */
558 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
560 unsigned int wc_fields; /* Hilarious. */
562 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
563 struct classifier cls;
564 struct test_rule *rule, *tcls_rule;
567 rule = make_rule(wc_fields,
568 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
570 classifier_init(&cls);
573 tcls_rule = tcls_insert(&tcls, rule);
574 assert(!classifier_insert(&cls, &rule->cls_rule));
575 check_tables(&cls, 1, 1, 0);
576 compare_classifiers(&cls, &tcls);
578 classifier_remove(&cls, &rule->cls_rule);
579 tcls_remove(&tcls, tcls_rule);
580 assert(classifier_is_empty(&cls));
581 assert(tcls_is_empty(&tcls));
582 compare_classifiers(&cls, &tcls);
585 classifier_destroy(&cls);
590 /* Tests replacing one rule by another. */
592 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
594 unsigned int wc_fields;
596 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
597 struct classifier cls;
598 struct test_rule *rule1;
599 struct test_rule *rule2;
602 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
603 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
607 classifier_init(&cls);
609 tcls_insert(&tcls, rule1);
610 assert(!classifier_insert(&cls, &rule1->cls_rule));
611 check_tables(&cls, 1, 1, 0);
612 compare_classifiers(&cls, &tcls);
616 tcls_insert(&tcls, rule2);
617 assert(test_rule_from_cls_rule(
618 classifier_insert(&cls, &rule2->cls_rule)) == rule1);
620 check_tables(&cls, 1, 1, 0);
621 compare_classifiers(&cls, &tcls);
623 destroy_classifier(&cls);
628 factorial(int n_items)
633 for (i = 2; i <= n_items; i++) {
648 reverse(int *a, int n)
652 for (i = 0; i < n / 2; i++) {
659 next_permutation(int *a, int n)
663 for (k = n - 2; k >= 0; k--) {
664 if (a[k] < a[k + 1]) {
667 for (l = n - 1; ; l--) {
670 reverse(a + (k + 1), n - (k + 1));
679 /* Tests classification with rules that have the same matching criteria. */
681 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
683 enum { N_RULES = 3 };
686 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
687 int ops[N_RULES * 2];
693 for (i = 1; i < N_RULES; i++) {
694 pris[i] = pris[i - 1] + (n_pris > i);
697 for (i = 0; i < N_RULES * 2; i++) {
703 struct test_rule *rules[N_RULES];
704 struct test_rule *tcls_rules[N_RULES];
705 int pri_rules[N_RULES];
706 struct classifier cls;
711 for (i = 0; i < N_RULES; i++) {
712 rules[i] = make_rule(456, pris[i], 0);
713 tcls_rules[i] = NULL;
717 classifier_init(&cls);
720 for (i = 0; i < ARRAY_SIZE(ops); i++) {
724 if (!tcls_rules[j]) {
725 struct test_rule *displaced_rule;
727 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
728 displaced_rule = test_rule_from_cls_rule(
729 classifier_insert(&cls, &rules[j]->cls_rule));
730 if (pri_rules[pris[j]] >= 0) {
731 int k = pri_rules[pris[j]];
732 assert(displaced_rule != NULL);
733 assert(displaced_rule != rules[j]);
734 assert(pris[j] == displaced_rule->cls_rule.priority);
735 tcls_rules[k] = NULL;
737 assert(displaced_rule == NULL);
739 pri_rules[pris[j]] = j;
741 classifier_remove(&cls, &rules[j]->cls_rule);
742 tcls_remove(&tcls, tcls_rules[j]);
743 tcls_rules[j] = NULL;
744 pri_rules[pris[j]] = -1;
748 for (m = 0; m < N_RULES; m++) {
749 n += tcls_rules[m] != NULL;
751 check_tables(&cls, n > 0, n, n - 1);
753 compare_classifiers(&cls, &tcls);
756 classifier_destroy(&cls);
759 for (i = 0; i < N_RULES; i++) {
762 } while (next_permutation(ops, ARRAY_SIZE(ops)));
763 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
768 count_ones(unsigned long int x)
781 array_contains(int *array, int n, int value)
785 for (i = 0; i < n; i++) {
786 if (array[i] == value) {
794 /* Tests classification with two rules at a time that fall into the same
795 * table but different lists. */
797 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
801 for (iteration = 0; iteration < 50; iteration++) {
802 enum { N_RULES = 20 };
803 struct test_rule *rules[N_RULES];
804 struct test_rule *tcls_rules[N_RULES];
805 struct classifier cls;
807 int value_pats[N_RULES];
813 wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
814 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
815 } while ((1 << count_ones(value_mask)) < N_RULES);
817 classifier_init(&cls);
820 for (i = 0; i < N_RULES; i++) {
821 unsigned int priority = rand();
824 value_pats[i] = rand() & value_mask;
825 } while (array_contains(value_pats, i, value_pats[i]));
827 rules[i] = make_rule(wcf, priority, value_pats[i]);
828 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
829 assert(!classifier_insert(&cls, &rules[i]->cls_rule));
831 check_tables(&cls, 1, i + 1, 0);
832 compare_classifiers(&cls, &tcls);
835 for (i = 0; i < N_RULES; i++) {
836 tcls_remove(&tcls, tcls_rules[i]);
837 classifier_remove(&cls, &rules[i]->cls_rule);
840 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
841 compare_classifiers(&cls, &tcls);
844 classifier_destroy(&cls);
849 /* Tests classification with many rules at a time that fall into random lists
852 test_many_rules_in_n_tables(int n_tables)
854 enum { MAX_RULES = 50 };
859 assert(n_tables < 10);
860 for (i = 0; i < n_tables; i++) {
862 wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
863 } while (array_contains(wcfs, i, wcfs[i]));
866 for (iteration = 0; iteration < 30; iteration++) {
867 unsigned int priorities[MAX_RULES];
868 struct classifier cls;
872 for (i = 0; i < MAX_RULES; i++) {
873 priorities[i] = i * 129;
875 shuffle(priorities, ARRAY_SIZE(priorities));
877 classifier_init(&cls);
880 for (i = 0; i < MAX_RULES; i++) {
881 struct test_rule *rule;
882 unsigned int priority = priorities[i];
883 int wcf = wcfs[rand() % n_tables];
884 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
885 rule = make_rule(wcf, priority, value_pat);
886 tcls_insert(&tcls, rule);
887 assert(!classifier_insert(&cls, &rule->cls_rule));
888 check_tables(&cls, -1, i + 1, -1);
889 compare_classifiers(&cls, &tcls);
892 while (!classifier_is_empty(&cls)) {
893 struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
894 sizeof(struct test_rule));
895 int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
896 include |= (rule->cls_rule.wc.wildcards
897 ? CLS_INC_WILD : CLS_INC_EXACT);
898 classifier_for_each_match(&cls, &rule->cls_rule, include,
900 tcls_delete_matches(&tcls, &rule->cls_rule, include);
901 compare_classifiers(&cls, &tcls);
902 check_tables(&cls, -1, -1, -1);
906 destroy_classifier(&cls);
912 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
914 test_many_rules_in_n_tables(2);
918 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
920 test_many_rules_in_n_tables(5);
923 static const struct command commands[] = {
924 {"empty", 0, 0, test_empty},
925 {"destroy-null", 0, 0, test_destroy_null},
926 {"single-rule", 0, 0, test_single_rule},
927 {"rule-replacement", 0, 0, test_rule_replacement},
928 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
929 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
930 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
931 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
936 main(int argc, char *argv[])
938 set_program_name(argv[0]);
940 run_command(argc - 1, argv + 1, commands);