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, dl_dst, DL_DST) \
55 CLS_FIELD(OFPFW_NW_PROTO, nw_proto, NW_PROTO) \
56 CLS_FIELD(OFPFW_DL_VLAN_PCP, dl_vlan_pcp, DL_VLAN_PCP) \
57 CLS_FIELD(OFPFW_NW_TOS, nw_tos, NW_TOS)
61 * (These are also indexed into struct classifier's 'tables' array.) */
63 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) CLS_F_IDX_##NAME,
69 /* Field information. */
71 int ofs; /* Offset in struct flow. */
72 int len; /* Length in bytes. */
73 uint32_t wildcards; /* OFPFW_* bit or bits for this field. */
74 const char *name; /* Name (for debugging). */
77 static const struct cls_field cls_fields[CLS_N_FIELDS] = {
78 #define CLS_FIELD(WILDCARDS, MEMBER, NAME) \
79 { offsetof(struct flow, MEMBER), \
80 sizeof ((struct flow *)0)->MEMBER, \
88 int aux; /* Auxiliary data. */
89 struct cls_rule cls_rule; /* Classifier rule data. */
92 static struct test_rule *
93 test_rule_from_cls_rule(const struct cls_rule *rule)
95 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
98 /* Trivial (linear) classifier. */
101 size_t allocated_rules;
102 struct test_rule **rules;
106 tcls_init(struct tcls *tcls)
109 tcls->allocated_rules = 0;
114 tcls_destroy(struct tcls *tcls)
119 for (i = 0; i < tcls->n_rules; i++) {
120 free(tcls->rules[i]);
127 tcls_count_exact(const struct tcls *tcls)
133 for (i = 0; i < tcls->n_rules; i++) {
134 n_exact += tcls->rules[i]->cls_rule.wc.wildcards == 0;
140 tcls_is_empty(const struct tcls *tcls)
142 return tcls->n_rules == 0;
145 static struct test_rule *
146 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
150 assert(rule->cls_rule.wc.wildcards || rule->cls_rule.priority == UINT_MAX);
151 for (i = 0; i < tcls->n_rules; i++) {
152 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
153 if (pos->priority == rule->cls_rule.priority
154 && pos->wc.wildcards == rule->cls_rule.wc.wildcards
155 && flow_equal(&pos->flow, &rule->cls_rule.flow)) {
157 * XXX flow_equal should ignore wildcarded fields */
158 free(tcls->rules[i]);
159 tcls->rules[i] = xmemdup(rule, sizeof *rule);
160 return tcls->rules[i];
161 } else if (pos->priority < rule->cls_rule.priority) {
166 if (tcls->n_rules >= tcls->allocated_rules) {
167 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
168 sizeof *tcls->rules);
170 if (i != tcls->n_rules) {
171 memmove(&tcls->rules[i + 1], &tcls->rules[i],
172 sizeof *tcls->rules * (tcls->n_rules - i));
174 tcls->rules[i] = xmemdup(rule, sizeof *rule);
176 return tcls->rules[i];
180 tcls_remove(struct tcls *cls, const struct test_rule *rule)
184 for (i = 0; i < cls->n_rules; i++) {
185 struct test_rule *pos = cls->rules[i];
188 memmove(&cls->rules[i], &cls->rules[i + 1],
189 sizeof *cls->rules * (cls->n_rules - i - 1));
198 read_uint32(const void *p)
201 memcpy(&x, p, sizeof x);
206 match(const struct cls_rule *wild, const struct flow *fixed)
210 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
211 const struct cls_field *f = &cls_fields[f_idx];
212 void *wild_field = (char *) &wild->flow + f->ofs;
213 void *fixed_field = (char *) fixed + f->ofs;
215 if ((wild->wc.wildcards & f->wildcards) == f->wildcards ||
216 !memcmp(wild_field, fixed_field, f->len)) {
217 /* Definite match. */
221 if (wild->wc.wildcards & f->wildcards) {
222 uint32_t test = read_uint32(wild_field);
223 uint32_t ip = read_uint32(fixed_field);
224 int shift = (f_idx == CLS_F_IDX_NW_SRC
225 ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
226 uint32_t mask = flow_nw_bits_to_mask(wild->wc.wildcards, shift);
227 if (!((test ^ ip) & mask)) {
237 static struct cls_rule *
238 tcls_lookup(const struct tcls *cls, const struct flow *flow, int include)
242 for (i = 0; i < cls->n_rules; i++) {
243 struct test_rule *pos = cls->rules[i];
244 uint32_t wildcards = pos->cls_rule.wc.wildcards;
245 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
246 && match(&pos->cls_rule, flow)) {
247 return &pos->cls_rule;
254 tcls_delete_matches(struct tcls *cls,
255 const struct cls_rule *target,
260 for (i = 0; i < cls->n_rules; ) {
261 struct test_rule *pos = cls->rules[i];
262 uint32_t wildcards = pos->cls_rule.wc.wildcards;
263 if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
264 && !flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
265 && match(target, &pos->cls_rule.flow)) {
266 tcls_remove(cls, pos);
273 static uint32_t nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
274 CONSTANT_HTONL(0xc0a04455) };
275 static uint32_t nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
276 CONSTANT_HTONL(0xc0a04455) };
277 static uint32_t tun_id_values[] = { 0, 0xffff0000 };
278 static uint16_t in_port_values[] = { CONSTANT_HTONS(1),
279 CONSTANT_HTONS(OFPP_LOCAL) };
280 static uint16_t dl_vlan_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
281 static uint8_t dl_vlan_pcp_values[] = { 7, 0 };
282 static uint16_t dl_type_values[]
283 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
284 static uint16_t tp_src_values[] = { CONSTANT_HTONS(49362),
285 CONSTANT_HTONS(80) };
286 static uint16_t tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
287 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
288 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
289 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
290 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
291 static uint8_t nw_proto_values[] = { IP_TYPE_TCP, IP_TYPE_ICMP };
292 static uint8_t nw_tos_values[] = { 49, 0 };
294 static void *values[CLS_N_FIELDS][2];
299 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
300 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
302 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
303 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
305 values[CLS_F_IDX_DL_VLAN][0] = &dl_vlan_values[0];
306 values[CLS_F_IDX_DL_VLAN][1] = &dl_vlan_values[1];
308 values[CLS_F_IDX_DL_VLAN_PCP][0] = &dl_vlan_pcp_values[0];
309 values[CLS_F_IDX_DL_VLAN_PCP][1] = &dl_vlan_pcp_values[1];
311 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
312 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
314 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
315 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
317 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
318 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
320 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
321 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
323 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
324 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
326 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
327 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
329 values[CLS_F_IDX_NW_TOS][0] = &nw_tos_values[0];
330 values[CLS_F_IDX_NW_TOS][1] = &nw_tos_values[1];
332 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
333 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
335 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
336 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
339 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
340 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
341 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
342 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
343 #define N_DL_VLAN_VALUES ARRAY_SIZE(dl_vlan_values)
344 #define N_DL_VLAN_PCP_VALUES ARRAY_SIZE(dl_vlan_pcp_values)
345 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
346 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
347 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
348 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
349 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
350 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
351 #define N_NW_TOS_VALUES ARRAY_SIZE(nw_tos_values)
353 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
358 N_DL_VLAN_PCP_VALUES * \
364 N_NW_PROTO_VALUES * \
368 get_value(unsigned int *x, unsigned n_values)
370 unsigned int rem = *x % n_values;
376 compare_classifiers(struct classifier *cls, struct tcls *tcls)
378 static const int confidence = 500;
381 assert(classifier_count(cls) == tcls->n_rules);
382 assert(classifier_count_exact(cls) == tcls_count_exact(tcls));
383 for (i = 0; i < confidence; i++) {
384 struct cls_rule *cr0, *cr1;
389 x = rand () % N_FLOW_VALUES;
390 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
391 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
392 flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
393 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
394 flow.dl_vlan = dl_vlan_values[get_value(&x, N_DL_VLAN_VALUES)];
395 flow.dl_vlan_pcp = dl_vlan_pcp_values[get_value(&x,
396 N_DL_VLAN_PCP_VALUES)];
397 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
398 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
399 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
400 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
402 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
404 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
405 flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
407 for (include = 1; include <= 3; include++) {
408 cr0 = classifier_lookup(cls, &flow, include);
409 cr1 = tcls_lookup(tcls, &flow, include);
410 assert((cr0 == NULL) == (cr1 == NULL));
412 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
413 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
415 assert(flow_equal(&cr0->flow, &cr1->flow));
416 assert(cr0->wc.wildcards == cr1->wc.wildcards);
417 assert(cr0->priority == cr1->priority);
418 /* Skip nw_src_mask and nw_dst_mask, because they are derived
419 * members whose values are used only for optimization. */
420 assert(tr0->aux == tr1->aux);
427 free_rule(struct cls_rule *cls_rule, void *cls)
429 classifier_remove(cls, cls_rule);
430 free(test_rule_from_cls_rule(cls_rule));
434 destroy_classifier(struct classifier *cls)
436 classifier_for_each(cls, CLS_INC_ALL, free_rule, cls);
437 classifier_destroy(cls);
441 check_tables(const struct classifier *cls,
442 int n_tables, int n_rules, int n_dups)
444 const struct cls_table *table;
445 const struct test_rule *test_rule;
446 struct flow_wildcards exact_wc;
447 int found_tables = 0;
453 flow_wildcards_init_exact(&exact_wc);
454 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
455 bool is_exact = flow_wildcards_equal(&table->wc, &exact_wc);
456 const struct cls_rule *head;
458 assert(!hmap_is_empty(&table->rules));
461 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
462 unsigned int prev_priority = UINT_MAX;
463 const struct cls_rule *rule;
469 LIST_FOR_EACH (rule, list, &head->list) {
470 assert(rule->priority < prev_priority);
471 prev_priority = rule->priority;
477 assert(classifier_find_rule_exactly(cls, rule) == rule);
482 CLASSIFIER_FOR_EACH_EXACT_RULE (test_rule, cls_rule, cls) {
486 assert(found_tables == hmap_count(&cls->tables));
487 assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
488 assert(n_rules == -1 || found_rules == n_rules);
489 assert(n_dups == -1 || found_dups == n_dups);
490 assert(n_exact1 == n_exact2);
493 static struct test_rule *
494 make_rule(int wc_fields, unsigned int priority, int value_pat)
496 const struct cls_field *f;
497 struct flow_wildcards wc;
498 struct test_rule *rule;
503 memset(&flow, 0, sizeof flow);
504 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
505 int f_idx = f - cls_fields;
506 if (wc_fields & (1u << f_idx)) {
507 wildcards |= f->wildcards;
509 int value_idx = (value_pat & (1u << f_idx)) != 0;
510 memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
514 rule = xzalloc(sizeof *rule);
515 flow_wildcards_init(&wc, wildcards);
516 cls_rule_init(&flow, &wc, !wildcards ? UINT_MAX : priority,
522 shuffle(unsigned int *p, size_t n)
524 for (; n > 1; n--, p++) {
525 unsigned int *q = &p[rand() % n];
526 unsigned int tmp = *p;
532 /* Tests an empty classifier. */
534 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
536 struct classifier cls;
539 classifier_init(&cls);
541 assert(classifier_is_empty(&cls));
542 assert(tcls_is_empty(&tcls));
543 compare_classifiers(&cls, &tcls);
544 classifier_destroy(&cls);
548 /* Destroys a null classifier. */
550 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
552 classifier_destroy(NULL);
555 /* Tests classification with one rule at a time. */
557 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
559 unsigned int wc_fields; /* Hilarious. */
561 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
562 struct classifier cls;
563 struct test_rule *rule, *tcls_rule;
566 rule = make_rule(wc_fields,
567 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
569 classifier_init(&cls);
572 tcls_rule = tcls_insert(&tcls, rule);
573 assert(!classifier_insert(&cls, &rule->cls_rule));
574 check_tables(&cls, 1, 1, 0);
575 compare_classifiers(&cls, &tcls);
577 classifier_remove(&cls, &rule->cls_rule);
578 tcls_remove(&tcls, tcls_rule);
579 assert(classifier_is_empty(&cls));
580 assert(tcls_is_empty(&tcls));
581 compare_classifiers(&cls, &tcls);
584 classifier_destroy(&cls);
589 /* Tests replacing one rule by another. */
591 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
593 unsigned int wc_fields;
595 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
596 struct classifier cls;
597 struct test_rule *rule1;
598 struct test_rule *rule2;
601 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
602 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
606 classifier_init(&cls);
608 tcls_insert(&tcls, rule1);
609 assert(!classifier_insert(&cls, &rule1->cls_rule));
610 check_tables(&cls, 1, 1, 0);
611 compare_classifiers(&cls, &tcls);
615 tcls_insert(&tcls, rule2);
616 assert(test_rule_from_cls_rule(
617 classifier_insert(&cls, &rule2->cls_rule)) == rule1);
619 check_tables(&cls, 1, 1, 0);
620 compare_classifiers(&cls, &tcls);
622 destroy_classifier(&cls);
627 factorial(int n_items)
632 for (i = 2; i <= n_items; i++) {
647 reverse(int *a, int n)
651 for (i = 0; i < n / 2; i++) {
658 next_permutation(int *a, int n)
662 for (k = n - 2; k >= 0; k--) {
663 if (a[k] < a[k + 1]) {
666 for (l = n - 1; ; l--) {
669 reverse(a + (k + 1), n - (k + 1));
678 /* Tests classification with rules that have the same matching criteria. */
680 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
682 enum { N_RULES = 3 };
685 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
686 int ops[N_RULES * 2];
692 for (i = 1; i < N_RULES; i++) {
693 pris[i] = pris[i - 1] + (n_pris > i);
696 for (i = 0; i < N_RULES * 2; i++) {
702 struct test_rule *rules[N_RULES];
703 struct test_rule *tcls_rules[N_RULES];
704 int pri_rules[N_RULES];
705 struct classifier cls;
710 for (i = 0; i < N_RULES; i++) {
711 rules[i] = make_rule(456, pris[i], 0);
712 tcls_rules[i] = NULL;
716 classifier_init(&cls);
719 for (i = 0; i < ARRAY_SIZE(ops); i++) {
723 if (!tcls_rules[j]) {
724 struct test_rule *displaced_rule;
726 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
727 displaced_rule = test_rule_from_cls_rule(
728 classifier_insert(&cls, &rules[j]->cls_rule));
729 if (pri_rules[pris[j]] >= 0) {
730 int k = pri_rules[pris[j]];
731 assert(displaced_rule != NULL);
732 assert(displaced_rule != rules[j]);
733 assert(pris[j] == displaced_rule->cls_rule.priority);
734 tcls_rules[k] = NULL;
736 assert(displaced_rule == NULL);
738 pri_rules[pris[j]] = j;
740 classifier_remove(&cls, &rules[j]->cls_rule);
741 tcls_remove(&tcls, tcls_rules[j]);
742 tcls_rules[j] = NULL;
743 pri_rules[pris[j]] = -1;
747 for (m = 0; m < N_RULES; m++) {
748 n += tcls_rules[m] != NULL;
750 check_tables(&cls, n > 0, n, n - 1);
752 compare_classifiers(&cls, &tcls);
755 classifier_destroy(&cls);
758 for (i = 0; i < N_RULES; i++) {
761 } while (next_permutation(ops, ARRAY_SIZE(ops)));
762 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
767 count_ones(unsigned long int x)
780 array_contains(int *array, int n, int value)
784 for (i = 0; i < n; i++) {
785 if (array[i] == value) {
793 /* Tests classification with two rules at a time that fall into the same
794 * table but different lists. */
796 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
800 for (iteration = 0; iteration < 50; iteration++) {
801 enum { N_RULES = 20 };
802 struct test_rule *rules[N_RULES];
803 struct test_rule *tcls_rules[N_RULES];
804 struct classifier cls;
806 int value_pats[N_RULES];
812 wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
813 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
814 } while ((1 << count_ones(value_mask)) < N_RULES);
816 classifier_init(&cls);
819 for (i = 0; i < N_RULES; i++) {
820 unsigned int priority = rand();
823 value_pats[i] = rand() & value_mask;
824 } while (array_contains(value_pats, i, value_pats[i]));
826 rules[i] = make_rule(wcf, priority, value_pats[i]);
827 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
828 assert(!classifier_insert(&cls, &rules[i]->cls_rule));
830 check_tables(&cls, 1, i + 1, 0);
831 compare_classifiers(&cls, &tcls);
834 for (i = 0; i < N_RULES; i++) {
835 tcls_remove(&tcls, tcls_rules[i]);
836 classifier_remove(&cls, &rules[i]->cls_rule);
839 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
840 compare_classifiers(&cls, &tcls);
843 classifier_destroy(&cls);
848 /* Tests classification with many rules at a time that fall into random lists
851 test_many_rules_in_n_tables(int n_tables)
853 enum { MAX_RULES = 50 };
858 assert(n_tables < 10);
859 for (i = 0; i < n_tables; i++) {
861 wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
862 } while (array_contains(wcfs, i, wcfs[i]));
865 for (iteration = 0; iteration < 30; iteration++) {
866 unsigned int priorities[MAX_RULES];
867 struct classifier cls;
871 for (i = 0; i < MAX_RULES; i++) {
872 priorities[i] = i * 129;
874 shuffle(priorities, ARRAY_SIZE(priorities));
876 classifier_init(&cls);
879 for (i = 0; i < MAX_RULES; i++) {
880 struct test_rule *rule;
881 unsigned int priority = priorities[i];
882 int wcf = wcfs[rand() % n_tables];
883 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
884 rule = make_rule(wcf, priority, value_pat);
885 tcls_insert(&tcls, rule);
886 assert(!classifier_insert(&cls, &rule->cls_rule));
887 check_tables(&cls, -1, i + 1, -1);
888 compare_classifiers(&cls, &tcls);
891 while (!classifier_is_empty(&cls)) {
892 struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
893 sizeof(struct test_rule));
894 int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
895 include |= (rule->cls_rule.wc.wildcards
896 ? CLS_INC_WILD : CLS_INC_EXACT);
897 classifier_for_each_match(&cls, &rule->cls_rule, include,
899 tcls_delete_matches(&tcls, &rule->cls_rule, include);
900 compare_classifiers(&cls, &tcls);
901 check_tables(&cls, -1, -1, -1);
905 destroy_classifier(&cls);
911 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
913 test_many_rules_in_n_tables(2);
917 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
919 test_many_rules_in_n_tables(5);
922 static const struct command commands[] = {
923 {"empty", 0, 0, test_empty},
924 {"destroy-null", 0, 0, test_destroy_null},
925 {"single-rule", 0, 0, test_single_rule},
926 {"rule-replacement", 0, 0, test_rule_replacement},
927 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
928 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
929 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
930 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
935 main(int argc, char *argv[])
937 set_program_name(argv[0]);
939 run_command(argc - 1, argv + 1, commands);