2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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, metadata, METADATA) \
49 CLS_FIELD(0, nw_src, NW_SRC) \
50 CLS_FIELD(0, nw_dst, NW_DST) \
51 CLS_FIELD(FWW_IN_PORT, in_port, IN_PORT) \
52 CLS_FIELD(0, vlan_tci, VLAN_TCI) \
53 CLS_FIELD(FWW_DL_TYPE, dl_type, DL_TYPE) \
54 CLS_FIELD(0, tp_src, TP_SRC) \
55 CLS_FIELD(0, tp_dst, TP_DST) \
56 CLS_FIELD(0, dl_src, DL_SRC) \
57 CLS_FIELD(0, dl_dst, DL_DST) \
58 CLS_FIELD(FWW_NW_PROTO, nw_proto, NW_PROTO) \
59 CLS_FIELD(0, nw_tos, NW_DSCP)
63 * (These are also indexed into struct classifier's 'tables' array.) */
65 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) CLS_F_IDX_##NAME,
71 /* Field information. */
73 int ofs; /* Offset in struct flow. */
74 int len; /* Length in bytes. */
75 flow_wildcards_t wildcards; /* FWW_* bit or bits for this field. */
76 const char *name; /* Name (for debugging). */
79 static const struct cls_field cls_fields[CLS_N_FIELDS] = {
80 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
81 { offsetof(struct flow, MEMBER), \
82 sizeof ((struct flow *)0)->MEMBER, \
90 int aux; /* Auxiliary data. */
91 struct cls_rule cls_rule; /* Classifier rule data. */
94 static struct test_rule *
95 test_rule_from_cls_rule(const struct cls_rule *rule)
97 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
100 /* Trivial (linear) classifier. */
103 size_t allocated_rules;
104 struct test_rule **rules;
108 tcls_init(struct tcls *tcls)
111 tcls->allocated_rules = 0;
116 tcls_destroy(struct tcls *tcls)
121 for (i = 0; i < tcls->n_rules; i++) {
122 free(tcls->rules[i]);
129 tcls_is_empty(const struct tcls *tcls)
131 return tcls->n_rules == 0;
134 static struct test_rule *
135 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
139 assert(!flow_wildcards_is_exact(&rule->cls_rule.wc)
140 || rule->cls_rule.priority == UINT_MAX);
141 for (i = 0; i < tcls->n_rules; i++) {
142 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
143 if (cls_rule_equal(pos, &rule->cls_rule)) {
145 free(tcls->rules[i]);
146 tcls->rules[i] = xmemdup(rule, sizeof *rule);
147 return tcls->rules[i];
148 } else if (pos->priority < rule->cls_rule.priority) {
153 if (tcls->n_rules >= tcls->allocated_rules) {
154 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
155 sizeof *tcls->rules);
157 if (i != tcls->n_rules) {
158 memmove(&tcls->rules[i + 1], &tcls->rules[i],
159 sizeof *tcls->rules * (tcls->n_rules - i));
161 tcls->rules[i] = xmemdup(rule, sizeof *rule);
163 return tcls->rules[i];
167 tcls_remove(struct tcls *cls, const struct test_rule *rule)
171 for (i = 0; i < cls->n_rules; i++) {
172 struct test_rule *pos = cls->rules[i];
175 memmove(&cls->rules[i], &cls->rules[i + 1],
176 sizeof *cls->rules * (cls->n_rules - i - 1));
185 match(const struct cls_rule *wild, const struct flow *fixed)
189 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
190 const struct cls_field *f = &cls_fields[f_idx];
194 void *wild_field = (char *) &wild->flow + f->ofs;
195 void *fixed_field = (char *) fixed + f->ofs;
196 eq = ((wild->wc.wildcards & f->wildcards) == f->wildcards
197 || !memcmp(wild_field, fixed_field, f->len));
198 } else if (f_idx == CLS_F_IDX_NW_SRC) {
199 eq = !((fixed->nw_src ^ wild->flow.nw_src) & wild->wc.nw_src_mask);
200 } else if (f_idx == CLS_F_IDX_NW_DST) {
201 eq = !((fixed->nw_dst ^ wild->flow.nw_dst) & wild->wc.nw_dst_mask);
202 } else if (f_idx == CLS_F_IDX_TP_SRC) {
203 eq = !((fixed->tp_src ^ wild->flow.tp_src) & wild->wc.tp_src_mask);
204 } else if (f_idx == CLS_F_IDX_TP_DST) {
205 eq = !((fixed->tp_dst ^ wild->flow.tp_dst) & wild->wc.tp_dst_mask);
206 } else if (f_idx == CLS_F_IDX_DL_SRC) {
207 eq = eth_addr_equal_except(fixed->dl_src, wild->flow.dl_src,
208 wild->wc.dl_src_mask);
209 } else if (f_idx == CLS_F_IDX_DL_DST) {
210 eq = eth_addr_equal_except(fixed->dl_dst, wild->flow.dl_dst,
211 wild->wc.dl_dst_mask);
212 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
213 eq = !((fixed->vlan_tci ^ wild->flow.vlan_tci)
214 & wild->wc.vlan_tci_mask);
215 } else if (f_idx == CLS_F_IDX_TUN_ID) {
216 eq = !((fixed->tun_id ^ wild->flow.tun_id) & wild->wc.tun_id_mask);
217 } else if (f_idx == CLS_F_IDX_METADATA) {
218 eq = !((fixed->metadata ^ wild->flow.metadata)
219 & wild->wc.metadata_mask);
220 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
221 eq = !((fixed->nw_tos ^ wild->flow.nw_tos) &
222 (wild->wc.nw_tos_mask & IP_DSCP_MASK));
234 static struct cls_rule *
235 tcls_lookup(const struct tcls *cls, const struct flow *flow)
239 for (i = 0; i < cls->n_rules; i++) {
240 struct test_rule *pos = cls->rules[i];
241 if (match(&pos->cls_rule, flow)) {
242 return &pos->cls_rule;
249 tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
253 for (i = 0; i < cls->n_rules; ) {
254 struct test_rule *pos = cls->rules[i];
255 if (!flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
256 && match(target, &pos->cls_rule.flow)) {
257 tcls_remove(cls, pos);
264 static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
265 CONSTANT_HTONL(0xc0a04455) };
266 static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
267 CONSTANT_HTONL(0xc0a04455) };
268 static ovs_be64 tun_id_values[] = {
270 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
271 static ovs_be64 metadata_values[] = {
273 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
274 static uint16_t in_port_values[] = { 1, OFPP_LOCAL };
275 static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
276 static ovs_be16 dl_type_values[]
277 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
278 static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
279 CONSTANT_HTONS(80) };
280 static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
281 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
282 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
283 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
284 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
285 static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
286 static uint8_t nw_dscp_values[] = { 48, 0 };
288 static void *values[CLS_N_FIELDS][2];
293 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
294 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
296 values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
297 values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
299 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
300 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
302 values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
303 values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
305 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
306 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
308 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
309 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
311 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
312 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
314 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
315 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
317 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
318 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
320 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
321 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
323 values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
324 values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
326 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
327 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
329 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
330 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
333 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
334 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
335 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
336 #define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
337 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
338 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
339 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
340 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
341 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
342 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
343 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
344 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
345 #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
347 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
351 N_VLAN_TCI_VALUES * \
357 N_NW_PROTO_VALUES * \
361 get_value(unsigned int *x, unsigned n_values)
363 unsigned int rem = *x % n_values;
369 compare_classifiers(struct classifier *cls, struct tcls *tcls)
371 static const int confidence = 500;
374 assert(classifier_count(cls) == tcls->n_rules);
375 for (i = 0; i < confidence; i++) {
376 struct cls_rule *cr0, *cr1;
380 x = rand () % N_FLOW_VALUES;
381 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
382 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
383 flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
384 flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
385 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
386 flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
387 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
388 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
389 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
390 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
392 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
394 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
395 flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
397 cr0 = classifier_lookup(cls, &flow);
398 cr1 = tcls_lookup(tcls, &flow);
399 assert((cr0 == NULL) == (cr1 == NULL));
401 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
402 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
404 assert(cls_rule_equal(cr0, cr1));
405 assert(tr0->aux == tr1->aux);
411 destroy_classifier(struct classifier *cls)
413 struct test_rule *rule, *next_rule;
414 struct cls_cursor cursor;
416 cls_cursor_init(&cursor, cls, NULL);
417 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
418 classifier_remove(cls, &rule->cls_rule);
421 classifier_destroy(cls);
425 check_tables(const struct classifier *cls,
426 int n_tables, int n_rules, int n_dups)
428 const struct cls_table *table;
429 struct test_rule *test_rule;
430 struct cls_cursor cursor;
431 int found_tables = 0;
434 int found_rules2 = 0;
436 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
437 const struct cls_rule *head;
439 assert(!hmap_is_empty(&table->rules));
442 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
443 unsigned int prev_priority = UINT_MAX;
444 const struct cls_rule *rule;
447 LIST_FOR_EACH (rule, list, &head->list) {
448 assert(rule->priority < prev_priority);
449 prev_priority = rule->priority;
452 assert(classifier_find_rule_exactly(cls, rule) == rule);
457 assert(found_tables == hmap_count(&cls->tables));
458 assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
459 assert(n_rules == -1 || found_rules == n_rules);
460 assert(n_dups == -1 || found_dups == n_dups);
462 cls_cursor_init(&cursor, cls, NULL);
463 CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) {
466 assert(found_rules == found_rules2);
469 static struct test_rule *
470 make_rule(int wc_fields, unsigned int priority, int value_pat)
472 const struct cls_field *f;
473 struct test_rule *rule;
475 rule = xzalloc(sizeof *rule);
476 cls_rule_init_catchall(&rule->cls_rule, wc_fields ? priority : UINT_MAX);
477 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
478 int f_idx = f - cls_fields;
479 int value_idx = (value_pat & (1u << f_idx)) != 0;
480 memcpy((char *) &rule->cls_rule.flow + f->ofs,
481 values[f_idx][value_idx], f->len);
484 rule->cls_rule.wc.wildcards &= ~f->wildcards;
485 } else if (f_idx == CLS_F_IDX_NW_SRC) {
486 rule->cls_rule.wc.nw_src_mask = htonl(UINT32_MAX);
487 } else if (f_idx == CLS_F_IDX_NW_DST) {
488 rule->cls_rule.wc.nw_dst_mask = htonl(UINT32_MAX);
489 } else if (f_idx == CLS_F_IDX_TP_SRC) {
490 rule->cls_rule.wc.tp_src_mask = htons(UINT16_MAX);
491 } else if (f_idx == CLS_F_IDX_TP_DST) {
492 rule->cls_rule.wc.tp_dst_mask = htons(UINT16_MAX);
493 } else if (f_idx == CLS_F_IDX_DL_SRC) {
494 memset(rule->cls_rule.wc.dl_src_mask, 0xff, ETH_ADDR_LEN);
495 } else if (f_idx == CLS_F_IDX_DL_DST) {
496 memset(rule->cls_rule.wc.dl_dst_mask, 0xff, ETH_ADDR_LEN);
497 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
498 rule->cls_rule.wc.vlan_tci_mask = htons(UINT16_MAX);
499 } else if (f_idx == CLS_F_IDX_TUN_ID) {
500 rule->cls_rule.wc.tun_id_mask = htonll(UINT64_MAX);
501 } else if (f_idx == CLS_F_IDX_METADATA) {
502 rule->cls_rule.wc.metadata_mask = htonll(UINT64_MAX);
503 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
504 rule->cls_rule.wc.nw_tos_mask |= IP_DSCP_MASK;
513 shuffle(unsigned int *p, size_t n)
515 for (; n > 1; n--, p++) {
516 unsigned int *q = &p[rand() % n];
517 unsigned int tmp = *p;
523 /* Tests an empty classifier. */
525 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
527 struct classifier cls;
530 classifier_init(&cls);
532 assert(classifier_is_empty(&cls));
533 assert(tcls_is_empty(&tcls));
534 compare_classifiers(&cls, &tcls);
535 classifier_destroy(&cls);
539 /* Destroys a null classifier. */
541 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
543 classifier_destroy(NULL);
546 /* Tests classification with one rule at a time. */
548 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
550 unsigned int wc_fields; /* Hilarious. */
552 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
553 struct classifier cls;
554 struct test_rule *rule, *tcls_rule;
557 rule = make_rule(wc_fields,
558 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
560 classifier_init(&cls);
563 tcls_rule = tcls_insert(&tcls, rule);
564 classifier_insert(&cls, &rule->cls_rule);
565 check_tables(&cls, 1, 1, 0);
566 compare_classifiers(&cls, &tcls);
568 classifier_remove(&cls, &rule->cls_rule);
569 tcls_remove(&tcls, tcls_rule);
570 assert(classifier_is_empty(&cls));
571 assert(tcls_is_empty(&tcls));
572 compare_classifiers(&cls, &tcls);
575 classifier_destroy(&cls);
580 /* Tests replacing one rule by another. */
582 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
584 unsigned int wc_fields;
586 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
587 struct classifier cls;
588 struct test_rule *rule1;
589 struct test_rule *rule2;
592 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
593 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
597 classifier_init(&cls);
599 tcls_insert(&tcls, rule1);
600 classifier_insert(&cls, &rule1->cls_rule);
601 check_tables(&cls, 1, 1, 0);
602 compare_classifiers(&cls, &tcls);
606 tcls_insert(&tcls, rule2);
607 assert(test_rule_from_cls_rule(
608 classifier_replace(&cls, &rule2->cls_rule)) == rule1);
610 check_tables(&cls, 1, 1, 0);
611 compare_classifiers(&cls, &tcls);
613 destroy_classifier(&cls);
618 factorial(int n_items)
623 for (i = 2; i <= n_items; i++) {
638 reverse(int *a, int n)
642 for (i = 0; i < n / 2; i++) {
649 next_permutation(int *a, int n)
653 for (k = n - 2; k >= 0; k--) {
654 if (a[k] < a[k + 1]) {
657 for (l = n - 1; ; l--) {
660 reverse(a + (k + 1), n - (k + 1));
669 /* Tests classification with rules that have the same matching criteria. */
671 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
673 enum { N_RULES = 3 };
676 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
677 int ops[N_RULES * 2];
683 for (i = 1; i < N_RULES; i++) {
684 pris[i] = pris[i - 1] + (n_pris > i);
687 for (i = 0; i < N_RULES * 2; i++) {
693 struct test_rule *rules[N_RULES];
694 struct test_rule *tcls_rules[N_RULES];
695 int pri_rules[N_RULES];
696 struct classifier cls;
701 for (i = 0; i < N_RULES; i++) {
702 rules[i] = make_rule(456, pris[i], 0);
703 tcls_rules[i] = NULL;
707 classifier_init(&cls);
710 for (i = 0; i < ARRAY_SIZE(ops); i++) {
714 if (!tcls_rules[j]) {
715 struct test_rule *displaced_rule;
717 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
718 displaced_rule = test_rule_from_cls_rule(
719 classifier_replace(&cls, &rules[j]->cls_rule));
720 if (pri_rules[pris[j]] >= 0) {
721 int k = pri_rules[pris[j]];
722 assert(displaced_rule != NULL);
723 assert(displaced_rule != rules[j]);
724 assert(pris[j] == displaced_rule->cls_rule.priority);
725 tcls_rules[k] = NULL;
727 assert(displaced_rule == NULL);
729 pri_rules[pris[j]] = j;
731 classifier_remove(&cls, &rules[j]->cls_rule);
732 tcls_remove(&tcls, tcls_rules[j]);
733 tcls_rules[j] = NULL;
734 pri_rules[pris[j]] = -1;
738 for (m = 0; m < N_RULES; m++) {
739 n += tcls_rules[m] != NULL;
741 check_tables(&cls, n > 0, n, n - 1);
743 compare_classifiers(&cls, &tcls);
746 classifier_destroy(&cls);
749 for (i = 0; i < N_RULES; i++) {
752 } while (next_permutation(ops, ARRAY_SIZE(ops)));
753 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
758 count_ones(unsigned long int x)
771 array_contains(int *array, int n, int value)
775 for (i = 0; i < n; i++) {
776 if (array[i] == value) {
784 /* Tests classification with two rules at a time that fall into the same
785 * table but different lists. */
787 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
791 for (iteration = 0; iteration < 50; iteration++) {
792 enum { N_RULES = 20 };
793 struct test_rule *rules[N_RULES];
794 struct test_rule *tcls_rules[N_RULES];
795 struct classifier cls;
797 int value_pats[N_RULES];
803 wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
804 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
805 } while ((1 << count_ones(value_mask)) < N_RULES);
807 classifier_init(&cls);
810 for (i = 0; i < N_RULES; i++) {
811 unsigned int priority = rand();
814 value_pats[i] = rand() & value_mask;
815 } while (array_contains(value_pats, i, value_pats[i]));
817 rules[i] = make_rule(wcf, priority, value_pats[i]);
818 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
819 classifier_insert(&cls, &rules[i]->cls_rule);
821 check_tables(&cls, 1, i + 1, 0);
822 compare_classifiers(&cls, &tcls);
825 for (i = 0; i < N_RULES; i++) {
826 tcls_remove(&tcls, tcls_rules[i]);
827 classifier_remove(&cls, &rules[i]->cls_rule);
830 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
831 compare_classifiers(&cls, &tcls);
834 classifier_destroy(&cls);
839 /* Tests classification with many rules at a time that fall into random lists
842 test_many_rules_in_n_tables(int n_tables)
844 enum { MAX_RULES = 50 };
849 assert(n_tables < 10);
850 for (i = 0; i < n_tables; i++) {
852 wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
853 } while (array_contains(wcfs, i, wcfs[i]));
856 for (iteration = 0; iteration < 30; iteration++) {
857 unsigned int priorities[MAX_RULES];
858 struct classifier cls;
862 for (i = 0; i < MAX_RULES; i++) {
863 priorities[i] = i * 129;
865 shuffle(priorities, ARRAY_SIZE(priorities));
867 classifier_init(&cls);
870 for (i = 0; i < MAX_RULES; i++) {
871 struct test_rule *rule;
872 unsigned int priority = priorities[i];
873 int wcf = wcfs[rand() % n_tables];
874 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
875 rule = make_rule(wcf, priority, value_pat);
876 tcls_insert(&tcls, rule);
877 classifier_insert(&cls, &rule->cls_rule);
878 check_tables(&cls, -1, i + 1, -1);
879 compare_classifiers(&cls, &tcls);
882 while (!classifier_is_empty(&cls)) {
883 struct test_rule *rule, *next_rule;
884 struct test_rule *target;
885 struct cls_cursor cursor;
887 target = xmemdup(tcls.rules[rand() % tcls.n_rules],
888 sizeof(struct test_rule));
890 cls_cursor_init(&cursor, &cls, &target->cls_rule);
891 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
892 classifier_remove(&cls, &rule->cls_rule);
895 tcls_delete_matches(&tcls, &target->cls_rule);
896 compare_classifiers(&cls, &tcls);
897 check_tables(&cls, -1, -1, -1);
901 destroy_classifier(&cls);
907 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
909 test_many_rules_in_n_tables(2);
913 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
915 test_many_rules_in_n_tables(5);
918 static const struct command commands[] = {
919 {"empty", 0, 0, test_empty},
920 {"destroy-null", 0, 0, test_destroy_null},
921 {"single-rule", 0, 0, test_single_rule},
922 {"rule-replacement", 0, 0, test_rule_replacement},
923 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
924 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
925 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
926 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
931 main(int argc, char *argv[])
933 set_program_name(argv[0]);
935 run_command(argc - 1, argv + 1, commands);