2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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.
31 #include "byte-order.h"
32 #include "command-line.h"
37 #include "unaligned.h"
42 /* We need access to classifier internal definitions to be able to fully
43 * test them. The alternative would be to expose them all in the classifier
45 #include "classifier.c"
47 /* Fields in a rule. */
49 /* struct flow all-caps */ \
50 /* member name name */ \
51 /* ----------- -------- */ \
52 CLS_FIELD(tunnel.tun_id, TUN_ID) \
53 CLS_FIELD(metadata, METADATA) \
54 CLS_FIELD(nw_src, NW_SRC) \
55 CLS_FIELD(nw_dst, NW_DST) \
56 CLS_FIELD(in_port, IN_PORT) \
57 CLS_FIELD(vlan_tci, VLAN_TCI) \
58 CLS_FIELD(dl_type, DL_TYPE) \
59 CLS_FIELD(tp_src, TP_SRC) \
60 CLS_FIELD(tp_dst, TP_DST) \
61 CLS_FIELD(dl_src, DL_SRC) \
62 CLS_FIELD(dl_dst, DL_DST) \
63 CLS_FIELD(nw_proto, NW_PROTO) \
64 CLS_FIELD(nw_tos, NW_DSCP)
68 * (These are also indexed into struct classifier's 'tables' array.) */
70 #define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME,
76 /* Field information. */
78 int ofs; /* Offset in struct flow. */
79 int len; /* Length in bytes. */
80 const char *name; /* Name (for debugging). */
83 static const struct cls_field cls_fields[CLS_N_FIELDS] = {
84 #define CLS_FIELD(MEMBER, NAME) \
85 { offsetof(struct flow, MEMBER), \
86 sizeof ((struct flow *)0)->MEMBER, \
93 int aux; /* Auxiliary data. */
94 struct cls_rule cls_rule; /* Classifier rule data. */
97 static struct test_rule *
98 test_rule_from_cls_rule(const struct cls_rule *rule)
100 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
104 test_rule_destroy(struct test_rule *rule)
107 cls_rule_destroy(&rule->cls_rule);
112 static struct test_rule *make_rule(int wc_fields, unsigned int priority,
114 static void free_rule(struct test_rule *);
115 static struct test_rule *clone_rule(const struct test_rule *);
117 /* Trivial (linear) classifier. */
120 size_t allocated_rules;
121 struct test_rule **rules;
125 tcls_init(struct tcls *tcls)
128 tcls->allocated_rules = 0;
133 tcls_destroy(struct tcls *tcls)
138 for (i = 0; i < tcls->n_rules; i++) {
139 test_rule_destroy(tcls->rules[i]);
146 tcls_is_empty(const struct tcls *tcls)
148 return tcls->n_rules == 0;
151 static struct test_rule *
152 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
156 for (i = 0; i < tcls->n_rules; i++) {
157 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
158 if (cls_rule_equal(pos, &rule->cls_rule)) {
160 free_rule(tcls->rules[i]);
161 tcls->rules[i] = clone_rule(rule);
162 return tcls->rules[i];
163 } else if (pos->priority < rule->cls_rule.priority) {
168 if (tcls->n_rules >= tcls->allocated_rules) {
169 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
170 sizeof *tcls->rules);
172 if (i != tcls->n_rules) {
173 memmove(&tcls->rules[i + 1], &tcls->rules[i],
174 sizeof *tcls->rules * (tcls->n_rules - i));
176 tcls->rules[i] = clone_rule(rule);
178 return tcls->rules[i];
182 tcls_remove(struct tcls *cls, const struct test_rule *rule)
186 for (i = 0; i < cls->n_rules; i++) {
187 struct test_rule *pos = cls->rules[i];
189 test_rule_destroy(pos);
191 memmove(&cls->rules[i], &cls->rules[i + 1],
192 sizeof *cls->rules * (cls->n_rules - i - 1));
202 match(const struct cls_rule *wild_, const struct flow *fixed)
207 minimatch_expand(&wild_->match, &wild);
208 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
211 if (f_idx == CLS_F_IDX_NW_SRC) {
212 eq = !((fixed->nw_src ^ wild.flow.nw_src)
213 & wild.wc.masks.nw_src);
214 } else if (f_idx == CLS_F_IDX_NW_DST) {
215 eq = !((fixed->nw_dst ^ wild.flow.nw_dst)
216 & wild.wc.masks.nw_dst);
217 } else if (f_idx == CLS_F_IDX_TP_SRC) {
218 eq = !((fixed->tp_src ^ wild.flow.tp_src)
219 & wild.wc.masks.tp_src);
220 } else if (f_idx == CLS_F_IDX_TP_DST) {
221 eq = !((fixed->tp_dst ^ wild.flow.tp_dst)
222 & wild.wc.masks.tp_dst);
223 } else if (f_idx == CLS_F_IDX_DL_SRC) {
224 eq = eth_addr_equal_except(fixed->dl_src, wild.flow.dl_src,
225 wild.wc.masks.dl_src);
226 } else if (f_idx == CLS_F_IDX_DL_DST) {
227 eq = eth_addr_equal_except(fixed->dl_dst, wild.flow.dl_dst,
228 wild.wc.masks.dl_dst);
229 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
230 eq = !((fixed->vlan_tci ^ wild.flow.vlan_tci)
231 & wild.wc.masks.vlan_tci);
232 } else if (f_idx == CLS_F_IDX_TUN_ID) {
233 eq = !((fixed->tunnel.tun_id ^ wild.flow.tunnel.tun_id)
234 & wild.wc.masks.tunnel.tun_id);
235 } else if (f_idx == CLS_F_IDX_METADATA) {
236 eq = !((fixed->metadata ^ wild.flow.metadata)
237 & wild.wc.masks.metadata);
238 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
239 eq = !((fixed->nw_tos ^ wild.flow.nw_tos) &
240 (wild.wc.masks.nw_tos & IP_DSCP_MASK));
241 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
242 eq = !((fixed->nw_proto ^ wild.flow.nw_proto)
243 & wild.wc.masks.nw_proto);
244 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
245 eq = !((fixed->dl_type ^ wild.flow.dl_type)
246 & wild.wc.masks.dl_type);
247 } else if (f_idx == CLS_F_IDX_IN_PORT) {
248 eq = !((fixed->in_port.ofp_port
249 ^ wild.flow.in_port.ofp_port)
250 & wild.wc.masks.in_port.ofp_port);
262 static struct cls_rule *
263 tcls_lookup(const struct tcls *cls, const struct flow *flow)
267 for (i = 0; i < cls->n_rules; i++) {
268 struct test_rule *pos = cls->rules[i];
269 if (match(&pos->cls_rule, flow)) {
270 return &pos->cls_rule;
277 tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
281 for (i = 0; i < cls->n_rules; ) {
282 struct test_rule *pos = cls->rules[i];
283 if (!minimask_has_extra(&pos->cls_rule.match.mask,
284 &target->match.mask)) {
287 miniflow_expand(&pos->cls_rule.match.flow, &flow);
288 if (match(target, &flow)) {
289 tcls_remove(cls, pos);
297 static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
298 CONSTANT_HTONL(0xc0a04455) };
299 static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
300 CONSTANT_HTONL(0xc0a04455) };
301 static ovs_be64 tun_id_values[] = {
303 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
304 static ovs_be64 metadata_values[] = {
306 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
307 static ofp_port_t in_port_values[] = { OFP_PORT_C(1), OFPP_LOCAL };
308 static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
309 static ovs_be16 dl_type_values[]
310 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
311 static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
312 CONSTANT_HTONS(80) };
313 static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
314 static uint8_t dl_src_values[][ETH_ADDR_LEN] = {
315 { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
316 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
317 static uint8_t dl_dst_values[][ETH_ADDR_LEN] = {
318 { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
319 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
320 static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
321 static uint8_t nw_dscp_values[] = { 48, 0 };
323 static void *values[CLS_N_FIELDS][2];
328 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
329 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
331 values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
332 values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
334 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
335 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
337 values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
338 values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
340 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
341 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
343 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
344 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
346 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
347 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
349 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
350 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
352 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
353 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
355 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
356 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
358 values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
359 values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
361 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
362 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
364 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
365 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
368 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
369 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
370 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
371 #define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
372 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
373 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
374 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
375 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
376 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
377 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
378 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
379 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
380 #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
382 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
386 N_VLAN_TCI_VALUES * \
392 N_NW_PROTO_VALUES * \
396 get_value(unsigned int *x, unsigned n_values)
398 unsigned int rem = *x % n_values;
404 compare_classifiers(struct classifier *cls, struct tcls *tcls)
406 static const int confidence = 500;
409 assert(classifier_count(cls) == tcls->n_rules);
410 for (i = 0; i < confidence; i++) {
411 struct cls_rule *cr0, *cr1, *cr2;
413 struct flow_wildcards wc;
416 flow_wildcards_init_catchall(&wc);
417 x = random_range(N_FLOW_VALUES);
418 memset(&flow, 0, sizeof flow);
419 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
420 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
421 flow.tunnel.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
422 flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
423 flow.in_port.ofp_port = in_port_values[get_value(&x,
425 flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
426 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
427 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
428 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
429 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
431 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
433 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
434 flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
436 /* This assertion is here to suppress a GCC 4.9 array-bounds warning */
437 ovs_assert(cls->n_tries <= CLS_MAX_TRIES);
439 cr0 = classifier_lookup(cls, &flow, &wc);
440 cr1 = tcls_lookup(tcls, &flow);
441 assert((cr0 == NULL) == (cr1 == NULL));
443 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
444 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
446 assert(cls_rule_equal(cr0, cr1));
447 assert(tr0->aux == tr1->aux);
449 cr2 = classifier_lookup(cls, &flow, NULL);
455 destroy_classifier(struct classifier *cls)
457 struct test_rule *rule;
459 CLS_FOR_EACH_SAFE (rule, cls_rule, cls) {
460 classifier_remove(cls, &rule->cls_rule);
463 classifier_destroy(cls);
467 pvector_verify(const struct pvector *pvec)
469 void *ptr OVS_UNUSED;
470 unsigned int priority, prev_priority = UINT_MAX;
472 PVECTOR_FOR_EACH (ptr, pvec) {
473 priority = cursor__.vector[cursor__.entry_idx].priority;
474 if (priority > prev_priority) {
475 VLOG_ABORT("Priority vector is out of order (%u > %u)",
476 priority, prev_priority);
478 prev_priority = priority;
483 trie_verify(const rcu_trie_ptr *trie, unsigned int ofs, unsigned int n_bits)
485 const struct trie_node *node = ovsrcu_get(struct trie_node *, trie);
488 assert(node->n_rules == 0 || node->n_bits > 0);
490 assert((ofs > 0 || (ofs == 0 && node->n_bits == 0)) && ofs <= n_bits);
493 + trie_verify(&node->edges[0], ofs, n_bits)
494 + trie_verify(&node->edges[1], ofs, n_bits);
500 verify_tries(struct classifier *cls)
502 unsigned int n_rules = 0;
505 for (i = 0; i < cls->n_tries; i++) {
506 n_rules += trie_verify(&cls->tries[i].root, 0,
507 cls->tries[i].field->n_bits);
509 ovs_mutex_lock(&cls->mutex);
510 assert(n_rules <= cls->n_rules);
511 ovs_mutex_unlock(&cls->mutex);
515 check_tables(const struct classifier *cls, int n_tables, int n_rules,
518 const struct cls_subtable *table;
519 struct test_rule *test_rule;
520 int found_tables = 0;
523 int found_rules2 = 0;
525 pvector_verify(&cls->subtables);
526 CMAP_FOR_EACH (table, cmap_node, &cls->subtables_map) {
527 const struct cls_match *head;
528 unsigned int max_priority = 0;
529 unsigned int max_count = 0;
531 const struct cls_subtable *iter;
533 /* Locate the subtable from 'subtables'. */
534 PVECTOR_FOR_EACH (iter, &cls->subtables) {
537 VLOG_ABORT("Subtable %p duplicated in 'subtables'.",
544 VLOG_ABORT("Subtable %p not found from 'subtables'.", table);
547 assert(!cmap_is_empty(&table->rules));
549 ovs_mutex_lock(&cls->mutex);
550 assert(trie_verify(&table->ports_trie, 0, table->ports_mask_len)
551 == (table->ports_mask_len ? table->n_rules : 0));
552 ovs_mutex_unlock(&cls->mutex);
555 CMAP_FOR_EACH (head, cmap_node, &table->rules) {
556 unsigned int prev_priority = UINT_MAX;
557 const struct cls_match *rule;
559 if (head->priority > max_priority) {
560 max_priority = head->priority;
562 } else if (head->priority == max_priority) {
567 ovs_mutex_lock(&cls->mutex);
568 LIST_FOR_EACH (rule, list, &head->list) {
569 assert(rule->priority < prev_priority);
570 assert(rule->priority <= table->max_priority);
572 prev_priority = rule->priority;
575 ovs_mutex_unlock(&cls->mutex);
576 assert(classifier_find_rule_exactly(cls, rule->cls_rule)
578 ovs_mutex_lock(&cls->mutex);
580 ovs_mutex_unlock(&cls->mutex);
582 ovs_mutex_lock(&cls->mutex);
583 assert(table->max_priority == max_priority);
584 assert(table->max_count == max_count);
585 ovs_mutex_unlock(&cls->mutex);
588 assert(found_tables == cmap_count(&cls->subtables_map));
589 assert(found_tables == pvector_count(&cls->subtables));
590 assert(n_tables == -1 || n_tables == cmap_count(&cls->subtables_map));
591 assert(n_rules == -1 || found_rules == n_rules);
592 assert(n_dups == -1 || found_dups == n_dups);
594 CLS_FOR_EACH (test_rule, cls_rule, cls) {
597 assert(found_rules == found_rules2);
600 static struct test_rule *
601 make_rule(int wc_fields, unsigned int priority, int value_pat)
603 const struct cls_field *f;
604 struct test_rule *rule;
607 match_init_catchall(&match);
608 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
609 int f_idx = f - cls_fields;
610 int value_idx = (value_pat & (1u << f_idx)) != 0;
611 memcpy((char *) &match.flow + f->ofs,
612 values[f_idx][value_idx], f->len);
614 if (f_idx == CLS_F_IDX_NW_SRC) {
615 match.wc.masks.nw_src = OVS_BE32_MAX;
616 } else if (f_idx == CLS_F_IDX_NW_DST) {
617 match.wc.masks.nw_dst = OVS_BE32_MAX;
618 } else if (f_idx == CLS_F_IDX_TP_SRC) {
619 match.wc.masks.tp_src = OVS_BE16_MAX;
620 } else if (f_idx == CLS_F_IDX_TP_DST) {
621 match.wc.masks.tp_dst = OVS_BE16_MAX;
622 } else if (f_idx == CLS_F_IDX_DL_SRC) {
623 memset(match.wc.masks.dl_src, 0xff, ETH_ADDR_LEN);
624 } else if (f_idx == CLS_F_IDX_DL_DST) {
625 memset(match.wc.masks.dl_dst, 0xff, ETH_ADDR_LEN);
626 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
627 match.wc.masks.vlan_tci = OVS_BE16_MAX;
628 } else if (f_idx == CLS_F_IDX_TUN_ID) {
629 match.wc.masks.tunnel.tun_id = OVS_BE64_MAX;
630 } else if (f_idx == CLS_F_IDX_METADATA) {
631 match.wc.masks.metadata = OVS_BE64_MAX;
632 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
633 match.wc.masks.nw_tos |= IP_DSCP_MASK;
634 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
635 match.wc.masks.nw_proto = UINT8_MAX;
636 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
637 match.wc.masks.dl_type = OVS_BE16_MAX;
638 } else if (f_idx == CLS_F_IDX_IN_PORT) {
639 match.wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
645 rule = xzalloc(sizeof *rule);
646 cls_rule_init(&rule->cls_rule, &match, wc_fields ? priority : UINT_MAX);
650 static struct test_rule *
651 clone_rule(const struct test_rule *src)
653 struct test_rule *dst;
655 dst = xmalloc(sizeof *dst);
657 cls_rule_clone(&dst->cls_rule, &src->cls_rule);
662 free_rule(struct test_rule *rule)
664 cls_rule_destroy(&rule->cls_rule);
669 shuffle(unsigned int *p, size_t n)
671 for (; n > 1; n--, p++) {
672 unsigned int *q = &p[random_range(n)];
673 unsigned int tmp = *p;
680 shuffle_u32s(uint32_t *p, size_t n)
682 for (; n > 1; n--, p++) {
683 uint32_t *q = &p[random_range(n)];
690 /* Classifier tests. */
692 static enum mf_field_id trie_fields[2] = {
693 MFF_IPV4_DST, MFF_IPV4_SRC
697 set_prefix_fields(struct classifier *cls)
700 classifier_set_prefix_fields(cls, trie_fields, ARRAY_SIZE(trie_fields));
704 /* Tests an empty classifier. */
706 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
708 struct classifier cls;
711 classifier_init(&cls, flow_segment_u32s);
712 set_prefix_fields(&cls);
714 assert(classifier_is_empty(&cls));
715 assert(tcls_is_empty(&tcls));
716 compare_classifiers(&cls, &tcls);
717 classifier_destroy(&cls);
721 /* Destroys a null classifier. */
723 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
725 classifier_destroy(NULL);
728 /* Tests classification with one rule at a time. */
730 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
732 unsigned int wc_fields; /* Hilarious. */
734 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
735 struct classifier cls;
736 struct test_rule *rule, *tcls_rule;
739 rule = make_rule(wc_fields,
740 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
742 classifier_init(&cls, flow_segment_u32s);
743 set_prefix_fields(&cls);
746 tcls_rule = tcls_insert(&tcls, rule);
747 classifier_insert(&cls, &rule->cls_rule);
748 compare_classifiers(&cls, &tcls);
749 check_tables(&cls, 1, 1, 0);
751 classifier_remove(&cls, &rule->cls_rule);
752 tcls_remove(&tcls, tcls_rule);
753 assert(classifier_is_empty(&cls));
754 assert(tcls_is_empty(&tcls));
755 compare_classifiers(&cls, &tcls);
758 classifier_destroy(&cls);
763 /* Tests replacing one rule by another. */
765 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
767 unsigned int wc_fields;
769 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
770 struct classifier cls;
771 struct test_rule *rule1;
772 struct test_rule *rule2;
775 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
776 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
780 classifier_init(&cls, flow_segment_u32s);
781 set_prefix_fields(&cls);
783 tcls_insert(&tcls, rule1);
784 classifier_insert(&cls, &rule1->cls_rule);
785 compare_classifiers(&cls, &tcls);
786 check_tables(&cls, 1, 1, 0);
790 tcls_insert(&tcls, rule2);
792 assert(test_rule_from_cls_rule(
793 classifier_replace(&cls, &rule2->cls_rule)) == rule1);
795 compare_classifiers(&cls, &tcls);
796 check_tables(&cls, 1, 1, 0);
799 destroy_classifier(&cls);
804 factorial(int n_items)
809 for (i = 2; i <= n_items; i++) {
824 reverse(int *a, int n)
828 for (i = 0; i < n / 2; i++) {
835 next_permutation(int *a, int n)
839 for (k = n - 2; k >= 0; k--) {
840 if (a[k] < a[k + 1]) {
843 for (l = n - 1; ; l--) {
846 reverse(a + (k + 1), n - (k + 1));
855 /* Tests classification with rules that have the same matching criteria. */
857 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
859 enum { N_RULES = 3 };
862 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
863 int ops[N_RULES * 2];
869 for (i = 1; i < N_RULES; i++) {
870 pris[i] = pris[i - 1] + (n_pris > i);
873 for (i = 0; i < N_RULES * 2; i++) {
879 struct test_rule *rules[N_RULES];
880 struct test_rule *tcls_rules[N_RULES];
881 int pri_rules[N_RULES];
882 struct classifier cls;
887 for (i = 0; i < N_RULES; i++) {
888 rules[i] = make_rule(456, pris[i], 0);
889 tcls_rules[i] = NULL;
893 classifier_init(&cls, flow_segment_u32s);
894 set_prefix_fields(&cls);
897 for (i = 0; i < ARRAY_SIZE(ops); i++) {
901 if (!tcls_rules[j]) {
902 struct test_rule *displaced_rule;
904 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
905 displaced_rule = test_rule_from_cls_rule(
906 classifier_replace(&cls, &rules[j]->cls_rule));
907 if (pri_rules[pris[j]] >= 0) {
908 int k = pri_rules[pris[j]];
909 assert(displaced_rule != NULL);
910 assert(displaced_rule != rules[j]);
911 assert(pris[j] == displaced_rule->cls_rule.priority);
912 tcls_rules[k] = NULL;
914 assert(displaced_rule == NULL);
916 pri_rules[pris[j]] = j;
918 classifier_remove(&cls, &rules[j]->cls_rule);
919 tcls_remove(&tcls, tcls_rules[j]);
920 tcls_rules[j] = NULL;
921 pri_rules[pris[j]] = -1;
923 compare_classifiers(&cls, &tcls);
926 for (m = 0; m < N_RULES; m++) {
927 n += tcls_rules[m] != NULL;
929 check_tables(&cls, n > 0, n, n - 1);
932 for (i = 0; i < N_RULES; i++) {
933 if (rules[i]->cls_rule.cls_match) {
934 classifier_remove(&cls, &rules[i]->cls_rule);
938 classifier_destroy(&cls);
940 } while (next_permutation(ops, ARRAY_SIZE(ops)));
941 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
946 count_ones(unsigned long int x)
951 x = zero_rightmost_1bit(x);
959 array_contains(int *array, int n, int value)
963 for (i = 0; i < n; i++) {
964 if (array[i] == value) {
972 /* Tests classification with two rules at a time that fall into the same
973 * table but different lists. */
975 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
979 for (iteration = 0; iteration < 50; iteration++) {
980 enum { N_RULES = 20 };
981 struct test_rule *rules[N_RULES];
982 struct test_rule *tcls_rules[N_RULES];
983 struct classifier cls;
985 int value_pats[N_RULES];
991 wcf = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
992 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
993 } while ((1 << count_ones(value_mask)) < N_RULES);
995 classifier_init(&cls, flow_segment_u32s);
996 set_prefix_fields(&cls);
999 for (i = 0; i < N_RULES; i++) {
1000 unsigned int priority = random_uint32();
1003 value_pats[i] = random_uint32() & value_mask;
1004 } while (array_contains(value_pats, i, value_pats[i]));
1006 rules[i] = make_rule(wcf, priority, value_pats[i]);
1007 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
1009 classifier_insert(&cls, &rules[i]->cls_rule);
1010 compare_classifiers(&cls, &tcls);
1012 check_tables(&cls, 1, i + 1, 0);
1015 for (i = 0; i < N_RULES; i++) {
1016 tcls_remove(&tcls, tcls_rules[i]);
1017 classifier_remove(&cls, &rules[i]->cls_rule);
1018 compare_classifiers(&cls, &tcls);
1019 free_rule(rules[i]);
1021 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
1024 classifier_destroy(&cls);
1025 tcls_destroy(&tcls);
1029 /* Tests classification with many rules at a time that fall into random lists
1032 test_many_rules_in_n_tables(int n_tables)
1034 enum { MAX_RULES = 50 };
1039 assert(n_tables < 10);
1040 for (i = 0; i < n_tables; i++) {
1042 wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1043 } while (array_contains(wcfs, i, wcfs[i]));
1046 for (iteration = 0; iteration < 30; iteration++) {
1047 unsigned int priorities[MAX_RULES];
1048 struct classifier cls;
1051 random_set_seed(iteration + 1);
1052 for (i = 0; i < MAX_RULES; i++) {
1053 priorities[i] = i * 129;
1055 shuffle(priorities, ARRAY_SIZE(priorities));
1057 classifier_init(&cls, flow_segment_u32s);
1058 set_prefix_fields(&cls);
1061 for (i = 0; i < MAX_RULES; i++) {
1062 struct test_rule *rule;
1063 unsigned int priority = priorities[i];
1064 int wcf = wcfs[random_range(n_tables)];
1065 int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1066 rule = make_rule(wcf, priority, value_pat);
1067 tcls_insert(&tcls, rule);
1068 classifier_insert(&cls, &rule->cls_rule);
1069 compare_classifiers(&cls, &tcls);
1070 check_tables(&cls, -1, i + 1, -1);
1073 while (!classifier_is_empty(&cls)) {
1074 struct test_rule *target;
1075 struct test_rule *rule;
1077 target = clone_rule(tcls.rules[random_range(tcls.n_rules)]);
1079 CLS_FOR_EACH_TARGET_SAFE (rule, cls_rule, &cls,
1080 &target->cls_rule) {
1081 classifier_remove(&cls, &rule->cls_rule);
1085 tcls_delete_matches(&tcls, &target->cls_rule);
1086 compare_classifiers(&cls, &tcls);
1087 check_tables(&cls, -1, -1, -1);
1091 destroy_classifier(&cls);
1092 tcls_destroy(&tcls);
1097 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1099 test_many_rules_in_n_tables(2);
1103 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1105 test_many_rules_in_n_tables(5);
1108 /* Miniflow tests. */
1113 static const uint32_t values[] =
1114 { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1115 0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1117 return values[random_range(ARRAY_SIZE(values))];
1121 choose(unsigned int n, unsigned int *idxp)
1132 init_consecutive_values(int n_consecutive, struct flow *flow,
1135 uint32_t *flow_u32 = (uint32_t *) flow;
1137 if (choose(FLOW_U32S - n_consecutive + 1, idxp)) {
1140 for (i = 0; i < n_consecutive; i++) {
1141 flow_u32[*idxp + i] = random_value();
1150 next_random_flow(struct flow *flow, unsigned int idx)
1152 uint32_t *flow_u32 = (uint32_t *) flow;
1155 memset(flow, 0, sizeof *flow);
1158 if (choose(1, &idx)) {
1162 /* All flows with a small number of consecutive nonzero values. */
1163 for (i = 1; i <= 4; i++) {
1164 if (init_consecutive_values(i, flow, &idx)) {
1169 /* All flows with a large number of consecutive nonzero values. */
1170 for (i = FLOW_U32S - 4; i <= FLOW_U32S; i++) {
1171 if (init_consecutive_values(i, flow, &idx)) {
1176 /* All flows with exactly two nonconsecutive nonzero values. */
1177 if (choose((FLOW_U32S - 1) * (FLOW_U32S - 2) / 2, &idx)) {
1180 for (ofs1 = 0; ofs1 < FLOW_U32S - 2; ofs1++) {
1183 for (ofs2 = ofs1 + 2; ofs2 < FLOW_U32S; ofs2++) {
1184 if (choose(1, &idx)) {
1185 flow_u32[ofs1] = random_value();
1186 flow_u32[ofs2] = random_value();
1194 /* 16 randomly chosen flows with N >= 3 nonzero values. */
1195 if (choose(16 * (FLOW_U32S - 4), &idx)) {
1196 int n = idx / 16 + 3;
1199 for (i = 0; i < n; i++) {
1200 flow_u32[i] = random_value();
1202 shuffle_u32s(flow_u32, FLOW_U32S);
1211 any_random_flow(struct flow *flow)
1213 static unsigned int max;
1215 while (next_random_flow(flow, max)) {
1220 next_random_flow(flow, random_range(max));
1224 toggle_masked_flow_bits(struct flow *flow, const struct flow_wildcards *mask)
1226 const uint32_t *mask_u32 = (const uint32_t *) &mask->masks;
1227 uint32_t *flow_u32 = (uint32_t *) flow;
1230 for (i = 0; i < FLOW_U32S; i++) {
1231 if (mask_u32[i] != 0) {
1235 bit = 1u << random_range(32);
1236 } while (!(bit & mask_u32[i]));
1243 wildcard_extra_bits(struct flow_wildcards *mask)
1245 uint32_t *mask_u32 = (uint32_t *) &mask->masks;
1248 for (i = 0; i < FLOW_U32S; i++) {
1249 if (mask_u32[i] != 0) {
1253 bit = 1u << random_range(32);
1254 } while (!(bit & mask_u32[i]));
1255 mask_u32[i] &= ~bit;
1261 test_miniflow(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1266 random_set_seed(0xb3faca38);
1267 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1268 const uint32_t *flow_u32 = (const uint32_t *) &flow;
1269 struct miniflow miniflow, miniflow2, miniflow3;
1270 struct flow flow2, flow3;
1271 struct flow_wildcards mask;
1272 struct minimask minimask;
1275 /* Convert flow to miniflow. */
1276 miniflow_init(&miniflow, &flow);
1278 /* Check that the flow equals its miniflow. */
1279 assert(miniflow_get_vid(&miniflow) == vlan_tci_to_vid(flow.vlan_tci));
1280 for (i = 0; i < FLOW_U32S; i++) {
1281 assert(MINIFLOW_GET_TYPE(&miniflow, uint32_t, i * 4)
1285 /* Check that the miniflow equals itself. */
1286 assert(miniflow_equal(&miniflow, &miniflow));
1288 /* Convert miniflow back to flow and verify that it's the same. */
1289 miniflow_expand(&miniflow, &flow2);
1290 assert(flow_equal(&flow, &flow2));
1292 /* Check that copying a miniflow works properly. */
1293 miniflow_clone(&miniflow2, &miniflow);
1294 assert(miniflow_equal(&miniflow, &miniflow2));
1295 assert(miniflow_hash(&miniflow, 0) == miniflow_hash(&miniflow2, 0));
1296 miniflow_expand(&miniflow2, &flow3);
1297 assert(flow_equal(&flow, &flow3));
1299 /* Check that masked matches work as expected for identical flows and
1302 next_random_flow(&mask.masks, 1);
1303 } while (flow_wildcards_is_catchall(&mask));
1304 minimask_init(&minimask, &mask);
1305 assert(minimask_is_catchall(&minimask)
1306 == flow_wildcards_is_catchall(&mask));
1307 assert(miniflow_equal_in_minimask(&miniflow, &miniflow2, &minimask));
1308 assert(miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1309 assert(miniflow_hash_in_minimask(&miniflow, &minimask, 0x12345678) ==
1310 flow_hash_in_minimask(&flow, &minimask, 0x12345678));
1312 /* Check that masked matches work as expected for differing flows and
1314 toggle_masked_flow_bits(&flow2, &mask);
1315 assert(!miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1316 miniflow_init(&miniflow3, &flow2);
1317 assert(!miniflow_equal_in_minimask(&miniflow, &miniflow3, &minimask));
1320 miniflow_destroy(&miniflow);
1321 miniflow_destroy(&miniflow2);
1322 miniflow_destroy(&miniflow3);
1323 minimask_destroy(&minimask);
1328 test_minimask_has_extra(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1330 struct flow_wildcards catchall;
1331 struct minimask minicatchall;
1335 flow_wildcards_init_catchall(&catchall);
1336 minimask_init(&minicatchall, &catchall);
1337 assert(minimask_is_catchall(&minicatchall));
1339 random_set_seed(0x2ec7905b);
1340 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1341 struct flow_wildcards mask;
1342 struct minimask minimask;
1345 minimask_init(&minimask, &mask);
1346 assert(!minimask_has_extra(&minimask, &minimask));
1347 assert(minimask_has_extra(&minicatchall, &minimask)
1348 == !minimask_is_catchall(&minimask));
1349 if (!minimask_is_catchall(&minimask)) {
1350 struct minimask minimask2;
1352 wildcard_extra_bits(&mask);
1353 minimask_init(&minimask2, &mask);
1354 assert(minimask_has_extra(&minimask2, &minimask));
1355 assert(!minimask_has_extra(&minimask, &minimask2));
1356 minimask_destroy(&minimask2);
1359 minimask_destroy(&minimask);
1362 minimask_destroy(&minicatchall);
1366 test_minimask_combine(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1368 struct flow_wildcards catchall;
1369 struct minimask minicatchall;
1373 flow_wildcards_init_catchall(&catchall);
1374 minimask_init(&minicatchall, &catchall);
1375 assert(minimask_is_catchall(&minicatchall));
1377 random_set_seed(0x181bf0cd);
1378 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1379 struct minimask minimask, minimask2, minicombined;
1380 struct flow_wildcards mask, mask2, combined, combined2;
1381 uint32_t storage[FLOW_U32S];
1385 minimask_init(&minimask, &mask);
1387 minimask_combine(&minicombined, &minimask, &minicatchall, storage);
1388 assert(minimask_is_catchall(&minicombined));
1390 any_random_flow(&flow2);
1391 mask2.masks = flow2;
1392 minimask_init(&minimask2, &mask2);
1394 minimask_combine(&minicombined, &minimask, &minimask2, storage);
1395 flow_wildcards_and(&combined, &mask, &mask2);
1396 minimask_expand(&minicombined, &combined2);
1397 assert(flow_wildcards_equal(&combined, &combined2));
1399 minimask_destroy(&minimask);
1400 minimask_destroy(&minimask2);
1403 minimask_destroy(&minicatchall);
1406 static const struct command commands[] = {
1407 /* Classifier tests. */
1408 {"empty", 0, 0, test_empty},
1409 {"destroy-null", 0, 0, test_destroy_null},
1410 {"single-rule", 0, 0, test_single_rule},
1411 {"rule-replacement", 0, 0, test_rule_replacement},
1412 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
1413 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
1414 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
1415 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
1417 /* Miniflow and minimask tests. */
1418 {"miniflow", 0, 0, test_miniflow},
1419 {"minimask_has_extra", 0, 0, test_minimask_has_extra},
1420 {"minimask_combine", 0, 0, test_minimask_combine},
1426 test_classifier_main(int argc, char *argv[])
1428 set_program_name(argv[0]);
1430 run_command(argc - 1, argv + 1, commands);
1433 OVSTEST_REGISTER("test-classifier", test_classifier_main);