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 int found_tables = 0;
449 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
450 const struct cls_rule *head;
452 assert(!hmap_is_empty(&table->rules));
455 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
456 unsigned int prev_priority = UINT_MAX;
457 const struct cls_rule *rule;
460 LIST_FOR_EACH (rule, list, &head->list) {
461 assert(rule->priority < prev_priority);
462 prev_priority = rule->priority;
465 assert(classifier_find_rule_exactly(cls, rule) == rule);
470 assert(found_tables == hmap_count(&cls->tables));
471 assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
472 assert(n_rules == -1 || found_rules == n_rules);
473 assert(n_dups == -1 || found_dups == n_dups);
476 static struct test_rule *
477 make_rule(int wc_fields, unsigned int priority, int value_pat)
479 const struct cls_field *f;
480 struct test_rule *rule;
485 memset(&flow, 0, sizeof flow);
486 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
487 int f_idx = f - cls_fields;
488 if (wc_fields & (1u << f_idx)) {
489 wildcards |= f->wildcards;
491 int value_idx = (value_pat & (1u << f_idx)) != 0;
492 memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
496 rule = xzalloc(sizeof *rule);
497 cls_rule_from_flow(&flow, wildcards, !wildcards ? UINT_MAX : priority,
503 shuffle(unsigned int *p, size_t n)
505 for (; n > 1; n--, p++) {
506 unsigned int *q = &p[rand() % n];
507 unsigned int tmp = *p;
513 /* Tests an empty classifier. */
515 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
517 struct classifier cls;
520 classifier_init(&cls);
522 assert(classifier_is_empty(&cls));
523 assert(tcls_is_empty(&tcls));
524 compare_classifiers(&cls, &tcls);
525 classifier_destroy(&cls);
529 /* Destroys a null classifier. */
531 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
533 classifier_destroy(NULL);
536 /* Tests classification with one rule at a time. */
538 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
540 unsigned int wc_fields; /* Hilarious. */
542 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
543 struct classifier cls;
544 struct test_rule *rule, *tcls_rule;
547 rule = make_rule(wc_fields,
548 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
550 classifier_init(&cls);
553 tcls_rule = tcls_insert(&tcls, rule);
554 assert(!classifier_insert(&cls, &rule->cls_rule));
555 check_tables(&cls, 1, 1, 0);
556 compare_classifiers(&cls, &tcls);
558 classifier_remove(&cls, &rule->cls_rule);
559 tcls_remove(&tcls, tcls_rule);
560 assert(classifier_is_empty(&cls));
561 assert(tcls_is_empty(&tcls));
562 compare_classifiers(&cls, &tcls);
565 classifier_destroy(&cls);
570 /* Tests replacing one rule by another. */
572 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
574 unsigned int wc_fields;
576 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
577 struct classifier cls;
578 struct test_rule *rule1;
579 struct test_rule *rule2;
582 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
583 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
587 classifier_init(&cls);
589 tcls_insert(&tcls, rule1);
590 assert(!classifier_insert(&cls, &rule1->cls_rule));
591 check_tables(&cls, 1, 1, 0);
592 compare_classifiers(&cls, &tcls);
596 tcls_insert(&tcls, rule2);
597 assert(test_rule_from_cls_rule(
598 classifier_insert(&cls, &rule2->cls_rule)) == rule1);
600 check_tables(&cls, 1, 1, 0);
601 compare_classifiers(&cls, &tcls);
603 destroy_classifier(&cls);
608 factorial(int n_items)
613 for (i = 2; i <= n_items; i++) {
628 reverse(int *a, int n)
632 for (i = 0; i < n / 2; i++) {
639 next_permutation(int *a, int n)
643 for (k = n - 2; k >= 0; k--) {
644 if (a[k] < a[k + 1]) {
647 for (l = n - 1; ; l--) {
650 reverse(a + (k + 1), n - (k + 1));
659 /* Tests classification with rules that have the same matching criteria. */
661 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
663 enum { N_RULES = 3 };
666 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
667 int ops[N_RULES * 2];
673 for (i = 1; i < N_RULES; i++) {
674 pris[i] = pris[i - 1] + (n_pris > i);
677 for (i = 0; i < N_RULES * 2; i++) {
683 struct test_rule *rules[N_RULES];
684 struct test_rule *tcls_rules[N_RULES];
685 int pri_rules[N_RULES];
686 struct classifier cls;
691 for (i = 0; i < N_RULES; i++) {
692 rules[i] = make_rule(456, pris[i], 0);
693 tcls_rules[i] = NULL;
697 classifier_init(&cls);
700 for (i = 0; i < ARRAY_SIZE(ops); i++) {
704 if (!tcls_rules[j]) {
705 struct test_rule *displaced_rule;
707 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
708 displaced_rule = test_rule_from_cls_rule(
709 classifier_insert(&cls, &rules[j]->cls_rule));
710 if (pri_rules[pris[j]] >= 0) {
711 int k = pri_rules[pris[j]];
712 assert(displaced_rule != NULL);
713 assert(displaced_rule != rules[j]);
714 assert(pris[j] == displaced_rule->cls_rule.priority);
715 tcls_rules[k] = NULL;
717 assert(displaced_rule == NULL);
719 pri_rules[pris[j]] = j;
721 classifier_remove(&cls, &rules[j]->cls_rule);
722 tcls_remove(&tcls, tcls_rules[j]);
723 tcls_rules[j] = NULL;
724 pri_rules[pris[j]] = -1;
728 for (m = 0; m < N_RULES; m++) {
729 n += tcls_rules[m] != NULL;
731 check_tables(&cls, n > 0, n, n - 1);
733 compare_classifiers(&cls, &tcls);
736 classifier_destroy(&cls);
739 for (i = 0; i < N_RULES; i++) {
742 } while (next_permutation(ops, ARRAY_SIZE(ops)));
743 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
748 count_ones(unsigned long int x)
761 array_contains(int *array, int n, int value)
765 for (i = 0; i < n; i++) {
766 if (array[i] == value) {
774 /* Tests classification with two rules at a time that fall into the same
775 * table but different lists. */
777 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
781 for (iteration = 0; iteration < 50; iteration++) {
782 enum { N_RULES = 20 };
783 struct test_rule *rules[N_RULES];
784 struct test_rule *tcls_rules[N_RULES];
785 struct classifier cls;
787 int value_pats[N_RULES];
793 wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
794 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
795 } while ((1 << count_ones(value_mask)) < N_RULES);
797 classifier_init(&cls);
800 for (i = 0; i < N_RULES; i++) {
801 unsigned int priority = rand();
804 value_pats[i] = rand() & value_mask;
805 } while (array_contains(value_pats, i, value_pats[i]));
807 rules[i] = make_rule(wcf, priority, value_pats[i]);
808 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
809 assert(!classifier_insert(&cls, &rules[i]->cls_rule));
811 check_tables(&cls, 1, i + 1, 0);
812 compare_classifiers(&cls, &tcls);
815 for (i = 0; i < N_RULES; i++) {
816 tcls_remove(&tcls, tcls_rules[i]);
817 classifier_remove(&cls, &rules[i]->cls_rule);
820 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
821 compare_classifiers(&cls, &tcls);
824 classifier_destroy(&cls);
829 /* Tests classification with many rules at a time that fall into random lists
832 test_many_rules_in_n_tables(int n_tables)
834 enum { MAX_RULES = 50 };
839 assert(n_tables < 10);
840 for (i = 0; i < n_tables; i++) {
842 wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
843 } while (array_contains(wcfs, i, wcfs[i]));
846 for (iteration = 0; iteration < 30; iteration++) {
847 unsigned int priorities[MAX_RULES];
848 struct classifier cls;
852 for (i = 0; i < MAX_RULES; i++) {
853 priorities[i] = i * 129;
855 shuffle(priorities, ARRAY_SIZE(priorities));
857 classifier_init(&cls);
860 for (i = 0; i < MAX_RULES; i++) {
861 struct test_rule *rule;
862 unsigned int priority = priorities[i];
863 int wcf = wcfs[rand() % n_tables];
864 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
865 rule = make_rule(wcf, priority, value_pat);
866 tcls_insert(&tcls, rule);
867 assert(!classifier_insert(&cls, &rule->cls_rule));
868 check_tables(&cls, -1, i + 1, -1);
869 compare_classifiers(&cls, &tcls);
872 while (!classifier_is_empty(&cls)) {
873 struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
874 sizeof(struct test_rule));
875 int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
876 include |= (rule->cls_rule.wc.wildcards
877 ? CLS_INC_WILD : CLS_INC_EXACT);
878 classifier_for_each_match(&cls, &rule->cls_rule, include,
880 tcls_delete_matches(&tcls, &rule->cls_rule, include);
881 compare_classifiers(&cls, &tcls);
882 check_tables(&cls, -1, -1, -1);
886 destroy_classifier(&cls);
892 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
894 test_many_rules_in_n_tables(2);
898 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
900 test_many_rules_in_n_tables(5);
903 static const struct command commands[] = {
904 {"empty", 0, 0, test_empty},
905 {"destroy-null", 0, 0, test_destroy_null},
906 {"single-rule", 0, 0, test_single_rule},
907 {"rule-replacement", 0, 0, test_rule_replacement},
908 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
909 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
910 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
911 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
916 main(int argc, char *argv[])
918 set_program_name(argv[0]);
920 run_command(argc - 1, argv + 1, commands);