2 * Copyright (c) 2009, 2010, 2011 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 /* FWW_* bit(s) member name name */ \
46 /* -------------------------- ----------- -------- */ \
47 CLS_FIELD(0, tun_id, TUN_ID) \
48 CLS_FIELD(0, nw_src, NW_SRC) \
49 CLS_FIELD(0, nw_dst, NW_DST) \
50 CLS_FIELD(FWW_IN_PORT, in_port, IN_PORT) \
51 CLS_FIELD(0, vlan_tci, VLAN_TCI) \
52 CLS_FIELD(FWW_DL_TYPE, dl_type, DL_TYPE) \
53 CLS_FIELD(FWW_TP_SRC, tp_src, TP_SRC) \
54 CLS_FIELD(FWW_TP_DST, tp_dst, TP_DST) \
55 CLS_FIELD(FWW_DL_SRC, dl_src, DL_SRC) \
56 CLS_FIELD(FWW_DL_DST | FWW_ETH_MCAST, dl_dst, DL_DST) \
57 CLS_FIELD(FWW_NW_PROTO, nw_proto, NW_PROTO) \
58 CLS_FIELD(0, tos, 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 flow_wildcards_t wildcards; /* FWW_* 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_is_empty(const struct tcls *tcls)
130 return tcls->n_rules == 0;
133 static struct test_rule *
134 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
138 assert(!flow_wildcards_is_exact(&rule->cls_rule.wc)
139 || rule->cls_rule.priority == UINT_MAX);
140 for (i = 0; i < tcls->n_rules; i++) {
141 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
142 if (cls_rule_equal(pos, &rule->cls_rule)) {
144 free(tcls->rules[i]);
145 tcls->rules[i] = xmemdup(rule, sizeof *rule);
146 return tcls->rules[i];
147 } else if (pos->priority < rule->cls_rule.priority) {
152 if (tcls->n_rules >= tcls->allocated_rules) {
153 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
154 sizeof *tcls->rules);
156 if (i != tcls->n_rules) {
157 memmove(&tcls->rules[i + 1], &tcls->rules[i],
158 sizeof *tcls->rules * (tcls->n_rules - i));
160 tcls->rules[i] = xmemdup(rule, sizeof *rule);
162 return tcls->rules[i];
166 tcls_remove(struct tcls *cls, const struct test_rule *rule)
170 for (i = 0; i < cls->n_rules; i++) {
171 struct test_rule *pos = cls->rules[i];
174 memmove(&cls->rules[i], &cls->rules[i + 1],
175 sizeof *cls->rules * (cls->n_rules - i - 1));
184 match(const struct cls_rule *wild, const struct flow *fixed)
188 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
189 const struct cls_field *f = &cls_fields[f_idx];
193 void *wild_field = (char *) &wild->flow + f->ofs;
194 void *fixed_field = (char *) fixed + f->ofs;
195 eq = ((wild->wc.wildcards & f->wildcards) == f->wildcards
196 || !memcmp(wild_field, fixed_field, f->len));
197 } else if (f_idx == CLS_F_IDX_NW_SRC) {
198 eq = !((fixed->nw_src ^ wild->flow.nw_src) & wild->wc.nw_src_mask);
199 } else if (f_idx == CLS_F_IDX_NW_DST) {
200 eq = !((fixed->nw_dst ^ wild->flow.nw_dst) & wild->wc.nw_dst_mask);
201 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
202 eq = !((fixed->vlan_tci ^ wild->flow.vlan_tci)
203 & wild->wc.vlan_tci_mask);
204 } else if (f_idx == CLS_F_IDX_TUN_ID) {
205 eq = !((fixed->tun_id ^ wild->flow.tun_id) & wild->wc.tun_id_mask);
206 } else if (f_idx == CLS_F_IDX_TOS) {
207 eq = !((fixed->tos ^ wild->flow.tos)
208 & wild->wc.tos_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_be64 tun_id_values[] = {
256 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
257 static uint16_t in_port_values[] = { 1, OFPP_LOCAL };
258 static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
259 static ovs_be16 dl_type_values[]
260 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
261 static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
262 CONSTANT_HTONS(80) };
263 static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
264 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
265 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
266 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
267 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
268 static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
269 static uint8_t tos_values[] = { 48, 0 };
271 static void *values[CLS_N_FIELDS][2];
276 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
277 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
279 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
280 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
282 values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
283 values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
285 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
286 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
288 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
289 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
291 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
292 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
294 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
295 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
297 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
298 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
300 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
301 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
303 values[CLS_F_IDX_TOS][0] = &tos_values[0];
304 values[CLS_F_IDX_TOS][1] = &tos_values[1];
306 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
307 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
309 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
310 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
313 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
314 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
315 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
316 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
317 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
318 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
319 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
320 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
321 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
322 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
323 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
324 #define N_TOS_VALUES ARRAY_SIZE(tos_values)
326 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
330 N_VLAN_TCI_VALUES * \
336 N_NW_PROTO_VALUES * \
340 get_value(unsigned int *x, unsigned n_values)
342 unsigned int rem = *x % n_values;
348 compare_classifiers(struct classifier *cls, struct tcls *tcls)
350 static const int confidence = 500;
353 assert(classifier_count(cls) == tcls->n_rules);
354 for (i = 0; i < confidence; i++) {
355 struct cls_rule *cr0, *cr1;
359 x = rand () % N_FLOW_VALUES;
360 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
361 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
362 flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
363 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
364 flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
365 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
366 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
367 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
368 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
370 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
372 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
373 flow.tos = tos_values[get_value(&x, N_TOS_VALUES)];
375 cr0 = classifier_lookup(cls, &flow);
376 cr1 = tcls_lookup(tcls, &flow);
377 assert((cr0 == NULL) == (cr1 == NULL));
379 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
380 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
382 assert(cls_rule_equal(cr0, cr1));
383 assert(tr0->aux == tr1->aux);
389 destroy_classifier(struct classifier *cls)
391 struct test_rule *rule, *next_rule;
392 struct cls_cursor cursor;
394 cls_cursor_init(&cursor, cls, NULL);
395 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
396 classifier_remove(cls, &rule->cls_rule);
399 classifier_destroy(cls);
403 check_tables(const struct classifier *cls,
404 int n_tables, int n_rules, int n_dups)
406 const struct cls_table *table;
407 struct test_rule *test_rule;
408 struct cls_cursor cursor;
409 int found_tables = 0;
412 int found_rules2 = 0;
414 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
415 const struct cls_rule *head;
417 assert(!hmap_is_empty(&table->rules));
420 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
421 unsigned int prev_priority = UINT_MAX;
422 const struct cls_rule *rule;
425 LIST_FOR_EACH (rule, list, &head->list) {
426 assert(rule->priority < prev_priority);
427 prev_priority = rule->priority;
430 assert(classifier_find_rule_exactly(cls, rule) == rule);
435 assert(found_tables == hmap_count(&cls->tables));
436 assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
437 assert(n_rules == -1 || found_rules == n_rules);
438 assert(n_dups == -1 || found_dups == n_dups);
440 cls_cursor_init(&cursor, cls, NULL);
441 CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) {
444 assert(found_rules == found_rules2);
447 static struct test_rule *
448 make_rule(int wc_fields, unsigned int priority, int value_pat)
450 const struct cls_field *f;
451 struct test_rule *rule;
453 rule = xzalloc(sizeof *rule);
454 cls_rule_init_catchall(&rule->cls_rule, wc_fields ? priority : UINT_MAX);
455 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
456 int f_idx = f - cls_fields;
457 int value_idx = (value_pat & (1u << f_idx)) != 0;
458 memcpy((char *) &rule->cls_rule.flow + f->ofs,
459 values[f_idx][value_idx], f->len);
462 rule->cls_rule.wc.wildcards &= ~f->wildcards;
463 } else if (f_idx == CLS_F_IDX_NW_SRC) {
464 rule->cls_rule.wc.nw_src_mask = htonl(UINT32_MAX);
465 } else if (f_idx == CLS_F_IDX_NW_DST) {
466 rule->cls_rule.wc.nw_dst_mask = htonl(UINT32_MAX);
467 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
468 rule->cls_rule.wc.vlan_tci_mask = htons(UINT16_MAX);
469 } else if (f_idx == CLS_F_IDX_TUN_ID) {
470 rule->cls_rule.wc.tun_id_mask = htonll(UINT64_MAX);
471 } else if (f_idx == CLS_F_IDX_TOS) {
472 rule->cls_rule.wc.tos_mask = UINT8_MAX;
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);
532 classifier_insert(&cls, &rule->cls_rule);
533 check_tables(&cls, 1, 1, 0);
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 classifier_insert(&cls, &rule1->cls_rule);
569 check_tables(&cls, 1, 1, 0);
570 compare_classifiers(&cls, &tcls);
574 tcls_insert(&tcls, rule2);
575 assert(test_rule_from_cls_rule(
576 classifier_replace(&cls, &rule2->cls_rule)) == rule1);
578 check_tables(&cls, 1, 1, 0);
579 compare_classifiers(&cls, &tcls);
581 destroy_classifier(&cls);
586 factorial(int n_items)
591 for (i = 2; i <= n_items; i++) {
606 reverse(int *a, int n)
610 for (i = 0; i < n / 2; i++) {
617 next_permutation(int *a, int n)
621 for (k = n - 2; k >= 0; k--) {
622 if (a[k] < a[k + 1]) {
625 for (l = n - 1; ; l--) {
628 reverse(a + (k + 1), n - (k + 1));
637 /* Tests classification with rules that have the same matching criteria. */
639 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
641 enum { N_RULES = 3 };
644 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
645 int ops[N_RULES * 2];
651 for (i = 1; i < N_RULES; i++) {
652 pris[i] = pris[i - 1] + (n_pris > i);
655 for (i = 0; i < N_RULES * 2; i++) {
661 struct test_rule *rules[N_RULES];
662 struct test_rule *tcls_rules[N_RULES];
663 int pri_rules[N_RULES];
664 struct classifier cls;
669 for (i = 0; i < N_RULES; i++) {
670 rules[i] = make_rule(456, pris[i], 0);
671 tcls_rules[i] = NULL;
675 classifier_init(&cls);
678 for (i = 0; i < ARRAY_SIZE(ops); i++) {
682 if (!tcls_rules[j]) {
683 struct test_rule *displaced_rule;
685 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
686 displaced_rule = test_rule_from_cls_rule(
687 classifier_replace(&cls, &rules[j]->cls_rule));
688 if (pri_rules[pris[j]] >= 0) {
689 int k = pri_rules[pris[j]];
690 assert(displaced_rule != NULL);
691 assert(displaced_rule != rules[j]);
692 assert(pris[j] == displaced_rule->cls_rule.priority);
693 tcls_rules[k] = NULL;
695 assert(displaced_rule == NULL);
697 pri_rules[pris[j]] = j;
699 classifier_remove(&cls, &rules[j]->cls_rule);
700 tcls_remove(&tcls, tcls_rules[j]);
701 tcls_rules[j] = NULL;
702 pri_rules[pris[j]] = -1;
706 for (m = 0; m < N_RULES; m++) {
707 n += tcls_rules[m] != NULL;
709 check_tables(&cls, n > 0, n, n - 1);
711 compare_classifiers(&cls, &tcls);
714 classifier_destroy(&cls);
717 for (i = 0; i < N_RULES; i++) {
720 } while (next_permutation(ops, ARRAY_SIZE(ops)));
721 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
726 count_ones(unsigned long int x)
739 array_contains(int *array, int n, int value)
743 for (i = 0; i < n; i++) {
744 if (array[i] == value) {
752 /* Tests classification with two rules at a time that fall into the same
753 * table but different lists. */
755 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
759 for (iteration = 0; iteration < 50; iteration++) {
760 enum { N_RULES = 20 };
761 struct test_rule *rules[N_RULES];
762 struct test_rule *tcls_rules[N_RULES];
763 struct classifier cls;
765 int value_pats[N_RULES];
771 wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
772 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
773 } while ((1 << count_ones(value_mask)) < N_RULES);
775 classifier_init(&cls);
778 for (i = 0; i < N_RULES; i++) {
779 unsigned int priority = rand();
782 value_pats[i] = rand() & value_mask;
783 } while (array_contains(value_pats, i, value_pats[i]));
785 rules[i] = make_rule(wcf, priority, value_pats[i]);
786 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
787 classifier_insert(&cls, &rules[i]->cls_rule);
789 check_tables(&cls, 1, i + 1, 0);
790 compare_classifiers(&cls, &tcls);
793 for (i = 0; i < N_RULES; i++) {
794 tcls_remove(&tcls, tcls_rules[i]);
795 classifier_remove(&cls, &rules[i]->cls_rule);
798 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
799 compare_classifiers(&cls, &tcls);
802 classifier_destroy(&cls);
807 /* Tests classification with many rules at a time that fall into random lists
810 test_many_rules_in_n_tables(int n_tables)
812 enum { MAX_RULES = 50 };
817 assert(n_tables < 10);
818 for (i = 0; i < n_tables; i++) {
820 wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
821 } while (array_contains(wcfs, i, wcfs[i]));
824 for (iteration = 0; iteration < 30; iteration++) {
825 unsigned int priorities[MAX_RULES];
826 struct classifier cls;
830 for (i = 0; i < MAX_RULES; i++) {
831 priorities[i] = i * 129;
833 shuffle(priorities, ARRAY_SIZE(priorities));
835 classifier_init(&cls);
838 for (i = 0; i < MAX_RULES; i++) {
839 struct test_rule *rule;
840 unsigned int priority = priorities[i];
841 int wcf = wcfs[rand() % n_tables];
842 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
843 rule = make_rule(wcf, priority, value_pat);
844 tcls_insert(&tcls, rule);
845 classifier_insert(&cls, &rule->cls_rule);
846 check_tables(&cls, -1, i + 1, -1);
847 compare_classifiers(&cls, &tcls);
850 while (!classifier_is_empty(&cls)) {
851 struct test_rule *rule, *next_rule;
852 struct test_rule *target;
853 struct cls_cursor cursor;
855 target = xmemdup(tcls.rules[rand() % tcls.n_rules],
856 sizeof(struct test_rule));
858 cls_cursor_init(&cursor, &cls, &target->cls_rule);
859 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
860 classifier_remove(&cls, &rule->cls_rule);
863 tcls_delete_matches(&tcls, &target->cls_rule);
864 compare_classifiers(&cls, &tcls);
865 check_tables(&cls, -1, -1, -1);
869 destroy_classifier(&cls);
875 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
877 test_many_rules_in_n_tables(2);
881 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
883 test_many_rules_in_n_tables(5);
886 static const struct command commands[] = {
887 {"empty", 0, 0, test_empty},
888 {"destroy-null", 0, 0, test_destroy_null},
889 {"single-rule", 0, 0, test_single_rule},
890 {"rule-replacement", 0, 0, test_rule_replacement},
891 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
892 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
893 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
894 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
899 main(int argc, char *argv[])
901 set_program_name(argv[0]);
903 run_command(argc - 1, argv + 1, commands);