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"
37 #include "unaligned.h"
42 /* Fields in a rule. */
44 /* struct flow all-caps */ \
45 /* wildcard bit(s) member name name */ \
46 /* ----------------- ----------- -------- */ \
47 CLS_FIELD(NXFW_TUN_ID, tun_id, TUN_ID) \
48 CLS_FIELD(OFPFW_NW_SRC_MASK, nw_src, NW_SRC) \
49 CLS_FIELD(OFPFW_NW_DST_MASK, nw_dst, NW_DST) \
50 CLS_FIELD(OFPFW_IN_PORT, in_port, IN_PORT) \
51 CLS_FIELD(OFPFW_DL_VLAN, dl_vlan, DL_VLAN) \
52 CLS_FIELD(OFPFW_DL_TYPE, dl_type, DL_TYPE) \
53 CLS_FIELD(OFPFW_TP_SRC, tp_src, TP_SRC) \
54 CLS_FIELD(OFPFW_TP_DST, tp_dst, TP_DST) \
55 CLS_FIELD(OFPFW_DL_SRC, dl_src, DL_SRC) \
56 CLS_FIELD(OFPFW_DL_DST | FWW_ETH_MCAST, \
58 CLS_FIELD(OFPFW_NW_PROTO, nw_proto, NW_PROTO) \
59 CLS_FIELD(OFPFW_DL_VLAN_PCP, dl_vlan_pcp, DL_VLAN_PCP) \
60 CLS_FIELD(OFPFW_NW_TOS, nw_tos, NW_TOS)
64 * (These are also indexed into struct classifier's 'tables' array.) */
66 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) CLS_F_IDX_##NAME,
72 /* Field information. */
74 int ofs; /* Offset in struct flow. */
75 int len; /* Length in bytes. */
76 uint32_t wildcards; /* OFPFW_* bit or bits for this field. */
77 const char *name; /* Name (for debugging). */
80 static const struct cls_field cls_fields[CLS_N_FIELDS] = {
81 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
82 { offsetof(struct flow, MEMBER), \
83 sizeof ((struct flow *)0)->MEMBER, \
91 int aux; /* Auxiliary data. */
92 struct cls_rule cls_rule; /* Classifier rule data. */
95 static struct test_rule *
96 test_rule_from_cls_rule(const struct cls_rule *rule)
98 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
101 /* Trivial (linear) classifier. */
104 size_t allocated_rules;
105 struct test_rule **rules;
109 tcls_init(struct tcls *tcls)
112 tcls->allocated_rules = 0;
117 tcls_destroy(struct tcls *tcls)
122 for (i = 0; i < tcls->n_rules; i++) {
123 free(tcls->rules[i]);
130 tcls_is_empty(const struct tcls *tcls)
132 return tcls->n_rules == 0;
135 static struct test_rule *
136 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
140 assert(!flow_wildcards_is_exact(&rule->cls_rule.wc)
141 || rule->cls_rule.priority == UINT_MAX);
142 for (i = 0; i < tcls->n_rules; i++) {
143 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
144 if (cls_rule_equal(pos, &rule->cls_rule)) {
146 free(tcls->rules[i]);
147 tcls->rules[i] = xmemdup(rule, sizeof *rule);
148 return tcls->rules[i];
149 } else if (pos->priority < rule->cls_rule.priority) {
154 if (tcls->n_rules >= tcls->allocated_rules) {
155 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
156 sizeof *tcls->rules);
158 if (i != tcls->n_rules) {
159 memmove(&tcls->rules[i + 1], &tcls->rules[i],
160 sizeof *tcls->rules * (tcls->n_rules - i));
162 tcls->rules[i] = xmemdup(rule, sizeof *rule);
164 return tcls->rules[i];
168 tcls_remove(struct tcls *cls, const struct test_rule *rule)
172 for (i = 0; i < cls->n_rules; i++) {
173 struct test_rule *pos = cls->rules[i];
176 memmove(&cls->rules[i], &cls->rules[i + 1],
177 sizeof *cls->rules * (cls->n_rules - i - 1));
186 match(const struct cls_rule *wild, const struct flow *fixed)
190 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
191 const struct cls_field *f = &cls_fields[f_idx];
192 void *wild_field = (char *) &wild->flow + f->ofs;
193 void *fixed_field = (char *) fixed + f->ofs;
195 if ((wild->wc.wildcards & f->wildcards) == f->wildcards ||
196 !memcmp(wild_field, fixed_field, f->len)) {
197 /* Definite match. */
201 if (wild->wc.wildcards & f->wildcards) {
202 uint32_t test = get_unaligned_u32(wild_field);
203 uint32_t ip = get_unaligned_u32(fixed_field);
207 shift = (f_idx == CLS_F_IDX_NW_SRC
208 ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
209 mask = ofputil_wcbits_to_netmask(wild->wc.wildcards >> shift);
210 if (!((test ^ ip) & mask)) {
220 static struct cls_rule *
221 tcls_lookup(const struct tcls *cls, const struct flow *flow)
225 for (i = 0; i < cls->n_rules; i++) {
226 struct test_rule *pos = cls->rules[i];
227 if (match(&pos->cls_rule, flow)) {
228 return &pos->cls_rule;
235 tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
239 for (i = 0; i < cls->n_rules; ) {
240 struct test_rule *pos = cls->rules[i];
241 if (!flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
242 && match(target, &pos->cls_rule.flow)) {
243 tcls_remove(cls, pos);
250 static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
251 CONSTANT_HTONL(0xc0a04455) };
252 static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
253 CONSTANT_HTONL(0xc0a04455) };
254 static ovs_be32 tun_id_values[] = { 0, 0xffff0000 };
255 static uint16_t in_port_values[] = { 1, ODPP_LOCAL };
256 static ovs_be16 dl_vlan_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
257 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
258 static ovs_be16 dl_type_values[]
259 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
260 static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
261 CONSTANT_HTONS(80) };
262 static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
263 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
264 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
265 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
266 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
267 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
268 static uint8_t nw_tos_values[] = { 49, 0 };
270 static void *values[CLS_N_FIELDS][2];
275 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
276 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
278 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
279 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
281 values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
282 values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
284 values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
285 values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1];
287 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
288 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
290 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
291 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
293 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
294 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
296 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
297 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
299 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
300 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
302 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
303 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
305 values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0];
306 values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1];
308 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
309 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
311 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
312 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
315 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
316 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
317 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
318 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
319 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
320 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
321 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
322 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
323 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
324 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
325 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
326 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
327 #define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values)
329 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
334 N_DL_VLAN_PCP_VALUES * \
340 N_NW_PROTO_VALUES * \
344 get_value(unsigned int *x, unsigned n_values)
346 unsigned int rem = *x % n_values;
352 compare_classifiers(struct classifier *cls, struct tcls *tcls)
354 static const int confidence = 500;
357 assert(classifier_count(cls) == tcls->n_rules);
358 for (i = 0; i < confidence; i++) {
359 struct cls_rule *cr0, *cr1;
363 x = rand () % N_FLOW_VALUES;
364 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
365 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
366 flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
367 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
368 flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
369 flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
370 N_DL_VLAN_PCP_VALUES)];
371 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
372 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
373 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
374 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
376 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
378 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
379 flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
381 cr0 = classifier_lookup(cls, &flow);
382 cr1 = tcls_lookup(tcls, &flow);
383 assert((cr0 == NULL) == (cr1 == NULL));
385 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
386 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
388 assert(cls_rule_equal(cr0, cr1));
389 assert(tr0->aux == tr1->aux);
395 destroy_classifier(struct classifier *cls)
397 struct test_rule *rule, *next_rule;
398 struct cls_cursor cursor;
400 cls_cursor_init(&cursor, cls, NULL);
401 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
402 classifier_remove(cls, &rule->cls_rule);
405 classifier_destroy(cls);
409 check_tables(const struct classifier *cls,
410 int n_tables, int n_rules, int n_dups)
412 const struct cls_table *table;
413 struct flow_wildcards exact_wc;
414 struct test_rule *test_rule;
415 struct cls_cursor cursor;
416 int found_tables = 0;
419 int found_rules2 = 0;
421 flow_wildcards_init_exact(&exact_wc);
422 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
423 const struct cls_rule *head;
425 assert(!hmap_is_empty(&table->rules));
428 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
429 unsigned int prev_priority = UINT_MAX;
430 const struct cls_rule *rule;
433 LIST_FOR_EACH (rule, list, &head->list) {
434 assert(rule->priority < prev_priority);
435 prev_priority = rule->priority;
438 assert(classifier_find_rule_exactly(cls, rule) == rule);
443 assert(found_tables == hmap_count(&cls->tables));
444 assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
445 assert(n_rules == -1 || found_rules == n_rules);
446 assert(n_dups == -1 || found_dups == n_dups);
448 cls_cursor_init(&cursor, cls, NULL);
449 CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) {
452 assert(found_rules == found_rules2);
455 static struct test_rule *
456 make_rule(int wc_fields, unsigned int priority, int value_pat)
458 const struct cls_field *f;
459 struct flow_wildcards wc;
460 struct test_rule *rule;
465 memset(&flow, 0, sizeof flow);
466 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
467 int f_idx = f - cls_fields;
468 if (wc_fields & (1u << f_idx)) {
469 wildcards |= f->wildcards;
471 int value_idx = (value_pat & (1u << f_idx)) != 0;
472 memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
476 rule = xzalloc(sizeof *rule);
477 flow_wildcards_init(&wc, wildcards);
478 cls_rule_init(&flow, &wc, !wildcards ? UINT_MAX : priority,
484 shuffle(unsigned int *p, size_t n)
486 for (; n > 1; n--, p++) {
487 unsigned int *q = &p[rand() % n];
488 unsigned int tmp = *p;
494 /* Tests an empty classifier. */
496 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
498 struct classifier cls;
501 classifier_init(&cls);
503 assert(classifier_is_empty(&cls));
504 assert(tcls_is_empty(&tcls));
505 compare_classifiers(&cls, &tcls);
506 classifier_destroy(&cls);
510 /* Destroys a null classifier. */
512 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
514 classifier_destroy(NULL);
517 /* Tests classification with one rule at a time. */
519 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
521 unsigned int wc_fields; /* Hilarious. */
523 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
524 struct classifier cls;
525 struct test_rule *rule, *tcls_rule;
528 rule = make_rule(wc_fields,
529 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
531 classifier_init(&cls);
534 tcls_rule = tcls_insert(&tcls, rule);
535 assert(!classifier_insert(&cls, &rule->cls_rule));
536 check_tables(&cls, 1, 1, 0);
537 compare_classifiers(&cls, &tcls);
539 classifier_remove(&cls, &rule->cls_rule);
540 tcls_remove(&tcls, tcls_rule);
541 assert(classifier_is_empty(&cls));
542 assert(tcls_is_empty(&tcls));
543 compare_classifiers(&cls, &tcls);
546 classifier_destroy(&cls);
551 /* Tests replacing one rule by another. */
553 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
555 unsigned int wc_fields;
557 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
558 struct classifier cls;
559 struct test_rule *rule1;
560 struct test_rule *rule2;
563 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
564 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
568 classifier_init(&cls);
570 tcls_insert(&tcls, rule1);
571 assert(!classifier_insert(&cls, &rule1->cls_rule));
572 check_tables(&cls, 1, 1, 0);
573 compare_classifiers(&cls, &tcls);
577 tcls_insert(&tcls, rule2);
578 assert(test_rule_from_cls_rule(
579 classifier_insert(&cls, &rule2->cls_rule)) == rule1);
581 check_tables(&cls, 1, 1, 0);
582 compare_classifiers(&cls, &tcls);
584 destroy_classifier(&cls);
589 factorial(int n_items)
594 for (i = 2; i <= n_items; i++) {
609 reverse(int *a, int n)
613 for (i = 0; i < n / 2; i++) {
620 next_permutation(int *a, int n)
624 for (k = n - 2; k >= 0; k--) {
625 if (a[k] < a[k + 1]) {
628 for (l = n - 1; ; l--) {
631 reverse(a + (k + 1), n - (k + 1));
640 /* Tests classification with rules that have the same matching criteria. */
642 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
644 enum { N_RULES = 3 };
647 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
648 int ops[N_RULES * 2];
654 for (i = 1; i < N_RULES; i++) {
655 pris[i] = pris[i - 1] + (n_pris > i);
658 for (i = 0; i < N_RULES * 2; i++) {
664 struct test_rule *rules[N_RULES];
665 struct test_rule *tcls_rules[N_RULES];
666 int pri_rules[N_RULES];
667 struct classifier cls;
672 for (i = 0; i < N_RULES; i++) {
673 rules[i] = make_rule(456, pris[i], 0);
674 tcls_rules[i] = NULL;
678 classifier_init(&cls);
681 for (i = 0; i < ARRAY_SIZE(ops); i++) {
685 if (!tcls_rules[j]) {
686 struct test_rule *displaced_rule;
688 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
689 displaced_rule = test_rule_from_cls_rule(
690 classifier_insert(&cls, &rules[j]->cls_rule));
691 if (pri_rules[pris[j]] >= 0) {
692 int k = pri_rules[pris[j]];
693 assert(displaced_rule != NULL);
694 assert(displaced_rule != rules[j]);
695 assert(pris[j] == displaced_rule->cls_rule.priority);
696 tcls_rules[k] = NULL;
698 assert(displaced_rule == NULL);
700 pri_rules[pris[j]] = j;
702 classifier_remove(&cls, &rules[j]->cls_rule);
703 tcls_remove(&tcls, tcls_rules[j]);
704 tcls_rules[j] = NULL;
705 pri_rules[pris[j]] = -1;
709 for (m = 0; m < N_RULES; m++) {
710 n += tcls_rules[m] != NULL;
712 check_tables(&cls, n > 0, n, n - 1);
714 compare_classifiers(&cls, &tcls);
717 classifier_destroy(&cls);
720 for (i = 0; i < N_RULES; i++) {
723 } while (next_permutation(ops, ARRAY_SIZE(ops)));
724 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
729 count_ones(unsigned long int x)
742 array_contains(int *array, int n, int value)
746 for (i = 0; i < n; i++) {
747 if (array[i] == value) {
755 /* Tests classification with two rules at a time that fall into the same
756 * table but different lists. */
758 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
762 for (iteration = 0; iteration < 50; iteration++) {
763 enum { N_RULES = 20 };
764 struct test_rule *rules[N_RULES];
765 struct test_rule *tcls_rules[N_RULES];
766 struct classifier cls;
768 int value_pats[N_RULES];
774 wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
775 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
776 } while ((1 << count_ones(value_mask)) < N_RULES);
778 classifier_init(&cls);
781 for (i = 0; i < N_RULES; i++) {
782 unsigned int priority = rand();
785 value_pats[i] = rand() & value_mask;
786 } while (array_contains(value_pats, i, value_pats[i]));
788 rules[i] = make_rule(wcf, priority, value_pats[i]);
789 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
790 assert(!classifier_insert(&cls, &rules[i]->cls_rule));
792 check_tables(&cls, 1, i + 1, 0);
793 compare_classifiers(&cls, &tcls);
796 for (i = 0; i < N_RULES; i++) {
797 tcls_remove(&tcls, tcls_rules[i]);
798 classifier_remove(&cls, &rules[i]->cls_rule);
801 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
802 compare_classifiers(&cls, &tcls);
805 classifier_destroy(&cls);
810 /* Tests classification with many rules at a time that fall into random lists
813 test_many_rules_in_n_tables(int n_tables)
815 enum { MAX_RULES = 50 };
820 assert(n_tables < 10);
821 for (i = 0; i < n_tables; i++) {
823 wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
824 } while (array_contains(wcfs, i, wcfs[i]));
827 for (iteration = 0; iteration < 30; iteration++) {
828 unsigned int priorities[MAX_RULES];
829 struct classifier cls;
833 for (i = 0; i < MAX_RULES; i++) {
834 priorities[i] = i * 129;
836 shuffle(priorities, ARRAY_SIZE(priorities));
838 classifier_init(&cls);
841 for (i = 0; i < MAX_RULES; i++) {
842 struct test_rule *rule;
843 unsigned int priority = priorities[i];
844 int wcf = wcfs[rand() % n_tables];
845 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
846 rule = make_rule(wcf, priority, value_pat);
847 tcls_insert(&tcls, rule);
848 assert(!classifier_insert(&cls, &rule->cls_rule));
849 check_tables(&cls, -1, i + 1, -1);
850 compare_classifiers(&cls, &tcls);
853 while (!classifier_is_empty(&cls)) {
854 struct test_rule *rule, *next_rule;
855 struct test_rule *target;
856 struct cls_cursor cursor;
858 target = xmemdup(tcls.rules[rand() % tcls.n_rules],
859 sizeof(struct test_rule));
861 cls_cursor_init(&cursor, &cls, &target->cls_rule);
862 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
863 classifier_remove(&cls, &rule->cls_rule);
866 tcls_delete_matches(&tcls, &target->cls_rule);
867 compare_classifiers(&cls, &tcls);
868 check_tables(&cls, -1, -1, -1);
872 destroy_classifier(&cls);
878 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
880 test_many_rules_in_n_tables(2);
884 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
886 test_many_rules_in_n_tables(5);
889 static const struct command commands[] = {
890 {"empty", 0, 0, test_empty},
891 {"destroy-null", 0, 0, test_destroy_null},
892 {"single-rule", 0, 0, test_single_rule},
893 {"rule-replacement", 0, 0, test_rule_replacement},
894 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
895 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
896 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
897 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
902 main(int argc, char *argv[])
904 set_program_name(argv[0]);
906 run_command(argc - 1, argv + 1, commands);