2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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"
38 #include "unaligned.h"
43 /* Fields in a rule. */
45 /* struct flow all-caps */ \
46 /* member name name */ \
47 /* ----------- -------- */ \
48 CLS_FIELD(tunnel.tun_id, TUN_ID) \
49 CLS_FIELD(metadata, METADATA) \
50 CLS_FIELD(nw_src, NW_SRC) \
51 CLS_FIELD(nw_dst, NW_DST) \
52 CLS_FIELD(in_port, IN_PORT) \
53 CLS_FIELD(vlan_tci, VLAN_TCI) \
54 CLS_FIELD(dl_type, DL_TYPE) \
55 CLS_FIELD(tp_src, TP_SRC) \
56 CLS_FIELD(tp_dst, TP_DST) \
57 CLS_FIELD(dl_src, DL_SRC) \
58 CLS_FIELD(dl_dst, DL_DST) \
59 CLS_FIELD(nw_proto, NW_PROTO) \
60 CLS_FIELD(nw_tos, NW_DSCP)
64 * (These are also indexed into struct classifier's 'tables' array.) */
66 #define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME,
72 /* Field information. */
74 int ofs; /* Offset in struct flow. */
75 int len; /* Length in bytes. */
76 const char *name; /* Name (for debugging). */
79 static const struct cls_field cls_fields[CLS_N_FIELDS] = {
80 #define CLS_FIELD(MEMBER, NAME) \
81 { offsetof(struct flow, MEMBER), \
82 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;
100 test_rule_destroy(struct test_rule *rule)
103 cls_rule_destroy(&rule->cls_rule);
108 static struct test_rule *make_rule(int wc_fields, unsigned int priority,
110 static void free_rule(struct test_rule *);
111 static struct test_rule *clone_rule(const struct test_rule *);
113 /* Trivial (linear) classifier. */
116 size_t allocated_rules;
117 struct test_rule **rules;
121 tcls_init(struct tcls *tcls)
124 tcls->allocated_rules = 0;
129 tcls_destroy(struct tcls *tcls)
134 for (i = 0; i < tcls->n_rules; i++) {
135 test_rule_destroy(tcls->rules[i]);
142 tcls_is_empty(const struct tcls *tcls)
144 return tcls->n_rules == 0;
147 static struct test_rule *
148 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
152 for (i = 0; i < tcls->n_rules; i++) {
153 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
154 if (cls_rule_equal(pos, &rule->cls_rule)) {
156 free_rule(tcls->rules[i]);
157 tcls->rules[i] = clone_rule(rule);
158 return tcls->rules[i];
159 } else if (pos->priority < rule->cls_rule.priority) {
164 if (tcls->n_rules >= tcls->allocated_rules) {
165 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
166 sizeof *tcls->rules);
168 if (i != tcls->n_rules) {
169 memmove(&tcls->rules[i + 1], &tcls->rules[i],
170 sizeof *tcls->rules * (tcls->n_rules - i));
172 tcls->rules[i] = clone_rule(rule);
174 return tcls->rules[i];
178 tcls_remove(struct tcls *cls, const struct test_rule *rule)
182 for (i = 0; i < cls->n_rules; i++) {
183 struct test_rule *pos = cls->rules[i];
185 test_rule_destroy(pos);
187 memmove(&cls->rules[i], &cls->rules[i + 1],
188 sizeof *cls->rules * (cls->n_rules - i - 1));
198 match(const struct cls_rule *wild_, const struct flow *fixed)
203 minimatch_expand(&wild_->match, &wild);
204 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
207 if (f_idx == CLS_F_IDX_NW_SRC) {
208 eq = !((fixed->nw_src ^ wild.flow.nw_src)
209 & wild.wc.masks.nw_src);
210 } else if (f_idx == CLS_F_IDX_NW_DST) {
211 eq = !((fixed->nw_dst ^ wild.flow.nw_dst)
212 & wild.wc.masks.nw_dst);
213 } else if (f_idx == CLS_F_IDX_TP_SRC) {
214 eq = !((fixed->tp_src ^ wild.flow.tp_src)
215 & wild.wc.masks.tp_src);
216 } else if (f_idx == CLS_F_IDX_TP_DST) {
217 eq = !((fixed->tp_dst ^ wild.flow.tp_dst)
218 & wild.wc.masks.tp_dst);
219 } else if (f_idx == CLS_F_IDX_DL_SRC) {
220 eq = eth_addr_equal_except(fixed->dl_src, wild.flow.dl_src,
221 wild.wc.masks.dl_src);
222 } else if (f_idx == CLS_F_IDX_DL_DST) {
223 eq = eth_addr_equal_except(fixed->dl_dst, wild.flow.dl_dst,
224 wild.wc.masks.dl_dst);
225 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
226 eq = !((fixed->vlan_tci ^ wild.flow.vlan_tci)
227 & wild.wc.masks.vlan_tci);
228 } else if (f_idx == CLS_F_IDX_TUN_ID) {
229 eq = !((fixed->tunnel.tun_id ^ wild.flow.tunnel.tun_id)
230 & wild.wc.masks.tunnel.tun_id);
231 } else if (f_idx == CLS_F_IDX_METADATA) {
232 eq = !((fixed->metadata ^ wild.flow.metadata)
233 & wild.wc.masks.metadata);
234 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
235 eq = !((fixed->nw_tos ^ wild.flow.nw_tos) &
236 (wild.wc.masks.nw_tos & IP_DSCP_MASK));
237 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
238 eq = !((fixed->nw_proto ^ wild.flow.nw_proto)
239 & wild.wc.masks.nw_proto);
240 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
241 eq = !((fixed->dl_type ^ wild.flow.dl_type)
242 & wild.wc.masks.dl_type);
243 } else if (f_idx == CLS_F_IDX_IN_PORT) {
244 eq = !((fixed->in_port.ofp_port
245 ^ wild.flow.in_port.ofp_port)
246 & wild.wc.masks.in_port.ofp_port);
258 static struct cls_rule *
259 tcls_lookup(const struct tcls *cls, const struct flow *flow)
263 for (i = 0; i < cls->n_rules; i++) {
264 struct test_rule *pos = cls->rules[i];
265 if (match(&pos->cls_rule, flow)) {
266 return &pos->cls_rule;
273 tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
277 for (i = 0; i < cls->n_rules; ) {
278 struct test_rule *pos = cls->rules[i];
279 if (!minimask_has_extra(&pos->cls_rule.match.mask,
280 &target->match.mask)) {
283 miniflow_expand(&pos->cls_rule.match.flow, &flow);
284 if (match(target, &flow)) {
285 tcls_remove(cls, pos);
293 static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
294 CONSTANT_HTONL(0xc0a04455) };
295 static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
296 CONSTANT_HTONL(0xc0a04455) };
297 static ovs_be64 tun_id_values[] = {
299 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
300 static ovs_be64 metadata_values[] = {
302 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
303 static ofp_port_t in_port_values[] = { OFP_PORT_C(1), OFPP_LOCAL };
304 static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
305 static ovs_be16 dl_type_values[]
306 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
307 static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
308 CONSTANT_HTONS(80) };
309 static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
310 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
311 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
312 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
313 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
314 static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
315 static uint8_t nw_dscp_values[] = { 48, 0 };
317 static void *values[CLS_N_FIELDS][2];
322 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
323 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
325 values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
326 values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
328 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
329 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
331 values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
332 values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
334 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
335 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
337 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
338 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
340 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
341 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
343 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
344 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
346 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
347 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
349 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
350 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
352 values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
353 values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
355 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
356 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
358 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
359 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
362 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
363 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
364 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
365 #define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
366 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
367 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
368 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
369 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
370 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
371 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
372 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
373 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
374 #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
376 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
380 N_VLAN_TCI_VALUES * \
386 N_NW_PROTO_VALUES * \
390 get_value(unsigned int *x, unsigned n_values)
392 unsigned int rem = *x % n_values;
398 compare_classifiers(struct classifier *cls, struct tcls *tcls)
400 static const int confidence = 500;
403 assert(classifier_count(cls) == tcls->n_rules);
404 for (i = 0; i < confidence; i++) {
405 struct cls_rule *cr0, *cr1;
409 x = random_range(N_FLOW_VALUES);
410 memset(&flow, 0, sizeof flow);
411 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
412 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
413 flow.tunnel.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
414 flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
415 flow.in_port.ofp_port = in_port_values[get_value(&x,
417 flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
418 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
419 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
420 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
421 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
423 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
425 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
426 flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
428 cr0 = classifier_lookup(cls, &flow, NULL);
429 cr1 = tcls_lookup(tcls, &flow);
430 assert((cr0 == NULL) == (cr1 == NULL));
432 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
433 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
435 assert(cls_rule_equal(cr0, cr1));
436 assert(tr0->aux == tr1->aux);
442 destroy_classifier(struct classifier *cls)
444 struct test_rule *rule, *next_rule;
445 struct cls_cursor cursor;
447 cls_cursor_init(&cursor, cls, NULL);
448 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
449 classifier_remove(cls, &rule->cls_rule);
452 classifier_destroy(cls);
456 check_tables(const struct classifier *cls,
457 int n_tables, int n_rules, int n_dups)
459 const struct cls_table *table;
460 struct test_rule *test_rule;
461 struct cls_cursor cursor;
462 int found_tables = 0;
465 int found_rules2 = 0;
467 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
468 const struct cls_rule *head;
469 unsigned int max_priority = 0;
470 unsigned int max_count = 0;
472 assert(!hmap_is_empty(&table->rules));
475 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
476 unsigned int prev_priority = UINT_MAX;
477 const struct cls_rule *rule;
479 if (head->priority > max_priority) {
480 max_priority = head->priority;
482 } else if (head->priority == max_priority) {
487 LIST_FOR_EACH (rule, list, &head->list) {
488 assert(rule->priority < prev_priority);
489 assert(rule->priority <= table->max_priority);
491 prev_priority = rule->priority;
494 assert(classifier_find_rule_exactly(cls, rule) == rule);
497 assert(table->max_priority == max_priority);
498 assert(table->max_count == max_count);
501 assert(found_tables == hmap_count(&cls->tables));
502 assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
503 assert(n_rules == -1 || found_rules == n_rules);
504 assert(n_dups == -1 || found_dups == n_dups);
506 cls_cursor_init(&cursor, cls, NULL);
507 CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) {
510 assert(found_rules == found_rules2);
513 static struct test_rule *
514 make_rule(int wc_fields, unsigned int priority, int value_pat)
516 const struct cls_field *f;
517 struct test_rule *rule;
520 match_init_catchall(&match);
521 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
522 int f_idx = f - cls_fields;
523 int value_idx = (value_pat & (1u << f_idx)) != 0;
524 memcpy((char *) &match.flow + f->ofs,
525 values[f_idx][value_idx], f->len);
527 if (f_idx == CLS_F_IDX_NW_SRC) {
528 match.wc.masks.nw_src = htonl(UINT32_MAX);
529 } else if (f_idx == CLS_F_IDX_NW_DST) {
530 match.wc.masks.nw_dst = htonl(UINT32_MAX);
531 } else if (f_idx == CLS_F_IDX_TP_SRC) {
532 match.wc.masks.tp_src = htons(UINT16_MAX);
533 } else if (f_idx == CLS_F_IDX_TP_DST) {
534 match.wc.masks.tp_dst = htons(UINT16_MAX);
535 } else if (f_idx == CLS_F_IDX_DL_SRC) {
536 memset(match.wc.masks.dl_src, 0xff, ETH_ADDR_LEN);
537 } else if (f_idx == CLS_F_IDX_DL_DST) {
538 memset(match.wc.masks.dl_dst, 0xff, ETH_ADDR_LEN);
539 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
540 match.wc.masks.vlan_tci = htons(UINT16_MAX);
541 } else if (f_idx == CLS_F_IDX_TUN_ID) {
542 match.wc.masks.tunnel.tun_id = htonll(UINT64_MAX);
543 } else if (f_idx == CLS_F_IDX_METADATA) {
544 match.wc.masks.metadata = htonll(UINT64_MAX);
545 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
546 match.wc.masks.nw_tos |= IP_DSCP_MASK;
547 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
548 match.wc.masks.nw_proto = UINT8_MAX;
549 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
550 match.wc.masks.dl_type = htons(UINT16_MAX);
551 } else if (f_idx == CLS_F_IDX_IN_PORT) {
552 match.wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
558 rule = xzalloc(sizeof *rule);
559 cls_rule_init(&rule->cls_rule, &match, wc_fields ? priority : UINT_MAX);
563 static struct test_rule *
564 clone_rule(const struct test_rule *src)
566 struct test_rule *dst;
568 dst = xmalloc(sizeof *dst);
570 cls_rule_clone(&dst->cls_rule, &src->cls_rule);
575 free_rule(struct test_rule *rule)
577 cls_rule_destroy(&rule->cls_rule);
582 shuffle(unsigned int *p, size_t n)
584 for (; n > 1; n--, p++) {
585 unsigned int *q = &p[random_range(n)];
586 unsigned int tmp = *p;
593 shuffle_u32s(uint32_t *p, size_t n)
595 for (; n > 1; n--, p++) {
596 uint32_t *q = &p[random_range(n)];
603 /* Classifier tests. */
605 /* Tests an empty classifier. */
607 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
609 struct classifier cls;
612 classifier_init(&cls);
614 assert(classifier_is_empty(&cls));
615 assert(tcls_is_empty(&tcls));
616 compare_classifiers(&cls, &tcls);
617 classifier_destroy(&cls);
621 /* Destroys a null classifier. */
623 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
625 classifier_destroy(NULL);
628 /* Tests classification with one rule at a time. */
630 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
632 unsigned int wc_fields; /* Hilarious. */
634 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
635 struct classifier cls;
636 struct test_rule *rule, *tcls_rule;
639 rule = make_rule(wc_fields,
640 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
642 classifier_init(&cls);
645 tcls_rule = tcls_insert(&tcls, rule);
646 classifier_insert(&cls, &rule->cls_rule);
647 check_tables(&cls, 1, 1, 0);
648 compare_classifiers(&cls, &tcls);
650 classifier_remove(&cls, &rule->cls_rule);
651 tcls_remove(&tcls, tcls_rule);
652 assert(classifier_is_empty(&cls));
653 assert(tcls_is_empty(&tcls));
654 compare_classifiers(&cls, &tcls);
657 classifier_destroy(&cls);
662 /* Tests replacing one rule by another. */
664 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
666 unsigned int wc_fields;
668 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
669 struct classifier cls;
670 struct test_rule *rule1;
671 struct test_rule *rule2;
674 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
675 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
679 classifier_init(&cls);
681 tcls_insert(&tcls, rule1);
682 classifier_insert(&cls, &rule1->cls_rule);
683 check_tables(&cls, 1, 1, 0);
684 compare_classifiers(&cls, &tcls);
688 tcls_insert(&tcls, rule2);
689 assert(test_rule_from_cls_rule(
690 classifier_replace(&cls, &rule2->cls_rule)) == rule1);
692 check_tables(&cls, 1, 1, 0);
693 compare_classifiers(&cls, &tcls);
695 destroy_classifier(&cls);
700 factorial(int n_items)
705 for (i = 2; i <= n_items; i++) {
720 reverse(int *a, int n)
724 for (i = 0; i < n / 2; i++) {
731 next_permutation(int *a, int n)
735 for (k = n - 2; k >= 0; k--) {
736 if (a[k] < a[k + 1]) {
739 for (l = n - 1; ; l--) {
742 reverse(a + (k + 1), n - (k + 1));
751 /* Tests classification with rules that have the same matching criteria. */
753 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
755 enum { N_RULES = 3 };
758 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
759 int ops[N_RULES * 2];
765 for (i = 1; i < N_RULES; i++) {
766 pris[i] = pris[i - 1] + (n_pris > i);
769 for (i = 0; i < N_RULES * 2; i++) {
775 struct test_rule *rules[N_RULES];
776 struct test_rule *tcls_rules[N_RULES];
777 int pri_rules[N_RULES];
778 struct classifier cls;
783 for (i = 0; i < N_RULES; i++) {
784 rules[i] = make_rule(456, pris[i], 0);
785 tcls_rules[i] = NULL;
789 classifier_init(&cls);
792 for (i = 0; i < ARRAY_SIZE(ops); i++) {
796 if (!tcls_rules[j]) {
797 struct test_rule *displaced_rule;
799 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
800 displaced_rule = test_rule_from_cls_rule(
801 classifier_replace(&cls, &rules[j]->cls_rule));
802 if (pri_rules[pris[j]] >= 0) {
803 int k = pri_rules[pris[j]];
804 assert(displaced_rule != NULL);
805 assert(displaced_rule != rules[j]);
806 assert(pris[j] == displaced_rule->cls_rule.priority);
807 tcls_rules[k] = NULL;
809 assert(displaced_rule == NULL);
811 pri_rules[pris[j]] = j;
813 classifier_remove(&cls, &rules[j]->cls_rule);
814 tcls_remove(&tcls, tcls_rules[j]);
815 tcls_rules[j] = NULL;
816 pri_rules[pris[j]] = -1;
820 for (m = 0; m < N_RULES; m++) {
821 n += tcls_rules[m] != NULL;
823 check_tables(&cls, n > 0, n, n - 1);
825 compare_classifiers(&cls, &tcls);
828 classifier_destroy(&cls);
831 for (i = 0; i < N_RULES; i++) {
834 } while (next_permutation(ops, ARRAY_SIZE(ops)));
835 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
840 count_ones(unsigned long int x)
845 x = zero_rightmost_1bit(x);
853 array_contains(int *array, int n, int value)
857 for (i = 0; i < n; i++) {
858 if (array[i] == value) {
866 /* Tests classification with two rules at a time that fall into the same
867 * table but different lists. */
869 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
873 for (iteration = 0; iteration < 50; iteration++) {
874 enum { N_RULES = 20 };
875 struct test_rule *rules[N_RULES];
876 struct test_rule *tcls_rules[N_RULES];
877 struct classifier cls;
879 int value_pats[N_RULES];
885 wcf = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
886 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
887 } while ((1 << count_ones(value_mask)) < N_RULES);
889 classifier_init(&cls);
892 for (i = 0; i < N_RULES; i++) {
893 unsigned int priority = random_uint32();
896 value_pats[i] = random_uint32() & value_mask;
897 } while (array_contains(value_pats, i, value_pats[i]));
899 rules[i] = make_rule(wcf, priority, value_pats[i]);
900 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
901 classifier_insert(&cls, &rules[i]->cls_rule);
903 check_tables(&cls, 1, i + 1, 0);
904 compare_classifiers(&cls, &tcls);
907 for (i = 0; i < N_RULES; i++) {
908 tcls_remove(&tcls, tcls_rules[i]);
909 classifier_remove(&cls, &rules[i]->cls_rule);
912 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
913 compare_classifiers(&cls, &tcls);
916 classifier_destroy(&cls);
921 /* Tests classification with many rules at a time that fall into random lists
924 test_many_rules_in_n_tables(int n_tables)
926 enum { MAX_RULES = 50 };
931 assert(n_tables < 10);
932 for (i = 0; i < n_tables; i++) {
934 wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
935 } while (array_contains(wcfs, i, wcfs[i]));
938 for (iteration = 0; iteration < 30; iteration++) {
939 unsigned int priorities[MAX_RULES];
940 struct classifier cls;
943 random_set_seed(iteration + 1);
944 for (i = 0; i < MAX_RULES; i++) {
945 priorities[i] = i * 129;
947 shuffle(priorities, ARRAY_SIZE(priorities));
949 classifier_init(&cls);
952 for (i = 0; i < MAX_RULES; i++) {
953 struct test_rule *rule;
954 unsigned int priority = priorities[i];
955 int wcf = wcfs[random_range(n_tables)];
956 int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
957 rule = make_rule(wcf, priority, value_pat);
958 tcls_insert(&tcls, rule);
959 classifier_insert(&cls, &rule->cls_rule);
960 check_tables(&cls, -1, i + 1, -1);
961 compare_classifiers(&cls, &tcls);
964 while (!classifier_is_empty(&cls)) {
965 struct test_rule *rule, *next_rule;
966 struct test_rule *target;
967 struct cls_cursor cursor;
969 target = clone_rule(tcls.rules[random_range(tcls.n_rules)]);
971 cls_cursor_init(&cursor, &cls, &target->cls_rule);
972 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
973 classifier_remove(&cls, &rule->cls_rule);
976 tcls_delete_matches(&tcls, &target->cls_rule);
977 compare_classifiers(&cls, &tcls);
978 check_tables(&cls, -1, -1, -1);
982 destroy_classifier(&cls);
988 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
990 test_many_rules_in_n_tables(2);
994 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
996 test_many_rules_in_n_tables(5);
999 /* Miniflow tests. */
1004 static const uint32_t values[] =
1005 { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1006 0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1008 return values[random_range(ARRAY_SIZE(values))];
1012 choose(unsigned int n, unsigned int *idxp)
1023 init_consecutive_values(int n_consecutive, struct flow *flow,
1026 uint32_t *flow_u32 = (uint32_t *) flow;
1028 if (choose(FLOW_U32S - n_consecutive + 1, idxp)) {
1031 for (i = 0; i < n_consecutive; i++) {
1032 flow_u32[*idxp + i] = random_value();
1041 next_random_flow(struct flow *flow, unsigned int idx)
1043 uint32_t *flow_u32 = (uint32_t *) flow;
1046 memset(flow, 0, sizeof *flow);
1049 if (choose(1, &idx)) {
1053 /* All flows with a small number of consecutive nonzero values. */
1054 for (i = 1; i <= 4; i++) {
1055 if (init_consecutive_values(i, flow, &idx)) {
1060 /* All flows with a large number of consecutive nonzero values. */
1061 for (i = FLOW_U32S - 4; i <= FLOW_U32S; i++) {
1062 if (init_consecutive_values(i, flow, &idx)) {
1067 /* All flows with exactly two nonconsecutive nonzero values. */
1068 if (choose((FLOW_U32S - 1) * (FLOW_U32S - 2) / 2, &idx)) {
1071 for (ofs1 = 0; ofs1 < FLOW_U32S - 2; ofs1++) {
1074 for (ofs2 = ofs1 + 2; ofs2 < FLOW_U32S; ofs2++) {
1075 if (choose(1, &idx)) {
1076 flow_u32[ofs1] = random_value();
1077 flow_u32[ofs2] = random_value();
1085 /* 16 randomly chosen flows with N >= 3 nonzero values. */
1086 if (choose(16 * (FLOW_U32S - 4), &idx)) {
1087 int n = idx / 16 + 3;
1090 for (i = 0; i < n; i++) {
1091 flow_u32[i] = random_value();
1093 shuffle_u32s(flow_u32, FLOW_U32S);
1102 any_random_flow(struct flow *flow)
1104 static unsigned int max;
1106 while (next_random_flow(flow, max)) {
1111 next_random_flow(flow, random_range(max));
1115 toggle_masked_flow_bits(struct flow *flow, const struct flow_wildcards *mask)
1117 const uint32_t *mask_u32 = (const uint32_t *) &mask->masks;
1118 uint32_t *flow_u32 = (uint32_t *) flow;
1121 for (i = 0; i < FLOW_U32S; i++) {
1122 if (mask_u32[i] != 0) {
1126 bit = 1u << random_range(32);
1127 } while (!(bit & mask_u32[i]));
1134 wildcard_extra_bits(struct flow_wildcards *mask)
1136 uint32_t *mask_u32 = (uint32_t *) &mask->masks;
1139 for (i = 0; i < FLOW_U32S; i++) {
1140 if (mask_u32[i] != 0) {
1144 bit = 1u << random_range(32);
1145 } while (!(bit & mask_u32[i]));
1146 mask_u32[i] &= ~bit;
1152 test_miniflow(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1157 random_set_seed(0xb3faca38);
1158 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1159 const uint32_t *flow_u32 = (const uint32_t *) &flow;
1160 struct miniflow miniflow, miniflow2, miniflow3;
1161 struct flow flow2, flow3;
1162 struct flow_wildcards mask;
1163 struct minimask minimask;
1166 /* Convert flow to miniflow. */
1167 miniflow_init(&miniflow, &flow);
1169 /* Check that the flow equals its miniflow. */
1170 assert(miniflow_get_vid(&miniflow) == vlan_tci_to_vid(flow.vlan_tci));
1171 for (i = 0; i < FLOW_U32S; i++) {
1172 assert(miniflow_get(&miniflow, i) == flow_u32[i]);
1175 /* Check that the miniflow equals itself. */
1176 assert(miniflow_equal(&miniflow, &miniflow));
1178 /* Convert miniflow back to flow and verify that it's the same. */
1179 miniflow_expand(&miniflow, &flow2);
1180 assert(flow_equal(&flow, &flow2));
1182 /* Check that copying a miniflow works properly. */
1183 miniflow_clone(&miniflow2, &miniflow);
1184 assert(miniflow_equal(&miniflow, &miniflow2));
1185 assert(miniflow_hash(&miniflow, 0) == miniflow_hash(&miniflow2, 0));
1186 miniflow_expand(&miniflow2, &flow3);
1187 assert(flow_equal(&flow, &flow3));
1189 /* Check that masked matches work as expected for identical flows and
1192 next_random_flow(&mask.masks, 1);
1193 } while (flow_wildcards_is_catchall(&mask));
1194 minimask_init(&minimask, &mask);
1195 assert(minimask_is_catchall(&minimask)
1196 == flow_wildcards_is_catchall(&mask));
1197 assert(miniflow_equal_in_minimask(&miniflow, &miniflow2, &minimask));
1198 assert(miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1199 assert(miniflow_hash_in_minimask(&miniflow, &minimask, 0x12345678) ==
1200 flow_hash_in_minimask(&flow, &minimask, 0x12345678));
1202 /* Check that masked matches work as expected for differing flows and
1204 toggle_masked_flow_bits(&flow2, &mask);
1205 assert(!miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1206 miniflow_init(&miniflow3, &flow2);
1207 assert(!miniflow_equal_in_minimask(&miniflow, &miniflow3, &minimask));
1210 miniflow_destroy(&miniflow);
1211 miniflow_destroy(&miniflow2);
1212 miniflow_destroy(&miniflow3);
1213 minimask_destroy(&minimask);
1218 test_minimask_has_extra(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1220 struct flow_wildcards catchall;
1221 struct minimask minicatchall;
1225 flow_wildcards_init_catchall(&catchall);
1226 minimask_init(&minicatchall, &catchall);
1227 assert(minimask_is_catchall(&minicatchall));
1229 random_set_seed(0x2ec7905b);
1230 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1231 struct flow_wildcards mask;
1232 struct minimask minimask;
1235 minimask_init(&minimask, &mask);
1236 assert(!minimask_has_extra(&minimask, &minimask));
1237 assert(minimask_has_extra(&minicatchall, &minimask)
1238 == !minimask_is_catchall(&minimask));
1239 if (!minimask_is_catchall(&minimask)) {
1240 struct minimask minimask2;
1242 wildcard_extra_bits(&mask);
1243 minimask_init(&minimask2, &mask);
1244 assert(minimask_has_extra(&minimask2, &minimask));
1245 assert(!minimask_has_extra(&minimask, &minimask2));
1246 minimask_destroy(&minimask2);
1249 minimask_destroy(&minimask);
1252 minimask_destroy(&minicatchall);
1256 test_minimask_combine(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1258 struct flow_wildcards catchall;
1259 struct minimask minicatchall;
1263 flow_wildcards_init_catchall(&catchall);
1264 minimask_init(&minicatchall, &catchall);
1265 assert(minimask_is_catchall(&minicatchall));
1267 random_set_seed(0x181bf0cd);
1268 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1269 struct minimask minimask, minimask2, minicombined;
1270 struct flow_wildcards mask, mask2, combined, combined2;
1271 uint32_t storage[FLOW_U32S];
1275 minimask_init(&minimask, &mask);
1277 minimask_combine(&minicombined, &minimask, &minicatchall, storage);
1278 assert(minimask_is_catchall(&minicombined));
1280 any_random_flow(&flow2);
1281 mask2.masks = flow2;
1282 minimask_init(&minimask2, &mask2);
1284 minimask_combine(&minicombined, &minimask, &minimask2, storage);
1285 flow_wildcards_and(&combined, &mask, &mask2);
1286 minimask_expand(&minicombined, &combined2);
1287 assert(flow_wildcards_equal(&combined, &combined2));
1289 minimask_destroy(&minimask);
1290 minimask_destroy(&minimask2);
1293 minimask_destroy(&minicatchall);
1296 static const struct command commands[] = {
1297 /* Classifier tests. */
1298 {"empty", 0, 0, test_empty},
1299 {"destroy-null", 0, 0, test_destroy_null},
1300 {"single-rule", 0, 0, test_single_rule},
1301 {"rule-replacement", 0, 0, test_rule_replacement},
1302 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
1303 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
1304 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
1305 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
1307 /* Miniflow and minimask tests. */
1308 {"miniflow", 0, 0, test_miniflow},
1309 {"minimask_has_extra", 0, 0, test_minimask_has_extra},
1310 {"minimask_combine", 0, 0, test_minimask_combine},
1316 main(int argc, char *argv[])
1318 set_program_name(argv[0]);
1320 run_command(argc - 1, argv + 1, commands);