2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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.
30 #include "classifier.h"
34 #include "byte-order.h"
35 #include "classifier-private.h"
36 #include "command-line.h"
42 #include "unaligned.h"
45 /* Fields in a rule. */
47 /* struct flow all-caps */ \
48 /* member name name */ \
49 /* ----------- -------- */ \
50 CLS_FIELD(tunnel.tun_id, TUN_ID) \
51 CLS_FIELD(metadata, METADATA) \
52 CLS_FIELD(nw_src, NW_SRC) \
53 CLS_FIELD(nw_dst, NW_DST) \
54 CLS_FIELD(in_port, IN_PORT) \
55 CLS_FIELD(vlan_tci, VLAN_TCI) \
56 CLS_FIELD(dl_type, DL_TYPE) \
57 CLS_FIELD(tp_src, TP_SRC) \
58 CLS_FIELD(tp_dst, TP_DST) \
59 CLS_FIELD(dl_src, DL_SRC) \
60 CLS_FIELD(dl_dst, DL_DST) \
61 CLS_FIELD(nw_proto, NW_PROTO) \
62 CLS_FIELD(nw_tos, NW_DSCP)
66 * (These are also indexed into struct classifier's 'tables' array.) */
68 #define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME,
74 /* Field information. */
76 int ofs; /* Offset in struct flow. */
77 int len; /* Length in bytes. */
78 const char *name; /* Name (for debugging). */
81 static const struct cls_field cls_fields[CLS_N_FIELDS] = {
82 #define CLS_FIELD(MEMBER, NAME) \
83 { offsetof(struct flow, MEMBER), \
84 sizeof ((struct flow *)0)->MEMBER, \
91 int aux; /* Auxiliary data. */
92 struct cls_rule cls_rule; /* Classifier rule data. */
95 static struct test_rule *
96 test_rule_from_cls_rule(const struct cls_rule *rule)
98 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
102 test_rule_destroy(struct test_rule *rule)
105 cls_rule_destroy(&rule->cls_rule);
110 static struct test_rule *make_rule(int wc_fields, int priority, int value_pat);
111 static void free_rule(struct test_rule *);
112 static struct test_rule *clone_rule(const struct test_rule *);
114 /* Trivial (linear) classifier. */
117 size_t allocated_rules;
118 struct test_rule **rules;
122 tcls_init(struct tcls *tcls)
125 tcls->allocated_rules = 0;
130 tcls_destroy(struct tcls *tcls)
135 for (i = 0; i < tcls->n_rules; i++) {
136 test_rule_destroy(tcls->rules[i]);
143 tcls_is_empty(const struct tcls *tcls)
145 return tcls->n_rules == 0;
148 static struct test_rule *
149 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
153 for (i = 0; i < tcls->n_rules; i++) {
154 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
155 if (cls_rule_equal(pos, &rule->cls_rule)) {
157 ovsrcu_postpone(free_rule, tcls->rules[i]);
158 tcls->rules[i] = clone_rule(rule);
159 return tcls->rules[i];
160 } else if (pos->priority < rule->cls_rule.priority) {
165 if (tcls->n_rules >= tcls->allocated_rules) {
166 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
167 sizeof *tcls->rules);
169 if (i != tcls->n_rules) {
170 memmove(&tcls->rules[i + 1], &tcls->rules[i],
171 sizeof *tcls->rules * (tcls->n_rules - i));
173 tcls->rules[i] = clone_rule(rule);
175 return tcls->rules[i];
179 tcls_remove(struct tcls *cls, const struct test_rule *rule)
183 for (i = 0; i < cls->n_rules; i++) {
184 struct test_rule *pos = cls->rules[i];
186 test_rule_destroy(pos);
188 memmove(&cls->rules[i], &cls->rules[i + 1],
189 sizeof *cls->rules * (cls->n_rules - i - 1));
199 match(const struct cls_rule *wild_, const struct flow *fixed)
204 minimatch_expand(&wild_->match, &wild);
205 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
208 if (f_idx == CLS_F_IDX_NW_SRC) {
209 eq = !((fixed->nw_src ^ wild.flow.nw_src)
210 & wild.wc.masks.nw_src);
211 } else if (f_idx == CLS_F_IDX_NW_DST) {
212 eq = !((fixed->nw_dst ^ wild.flow.nw_dst)
213 & wild.wc.masks.nw_dst);
214 } else if (f_idx == CLS_F_IDX_TP_SRC) {
215 eq = !((fixed->tp_src ^ wild.flow.tp_src)
216 & wild.wc.masks.tp_src);
217 } else if (f_idx == CLS_F_IDX_TP_DST) {
218 eq = !((fixed->tp_dst ^ wild.flow.tp_dst)
219 & wild.wc.masks.tp_dst);
220 } else if (f_idx == CLS_F_IDX_DL_SRC) {
221 eq = eth_addr_equal_except(fixed->dl_src, wild.flow.dl_src,
222 wild.wc.masks.dl_src);
223 } else if (f_idx == CLS_F_IDX_DL_DST) {
224 eq = eth_addr_equal_except(fixed->dl_dst, wild.flow.dl_dst,
225 wild.wc.masks.dl_dst);
226 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
227 eq = !((fixed->vlan_tci ^ wild.flow.vlan_tci)
228 & wild.wc.masks.vlan_tci);
229 } else if (f_idx == CLS_F_IDX_TUN_ID) {
230 eq = !((fixed->tunnel.tun_id ^ wild.flow.tunnel.tun_id)
231 & wild.wc.masks.tunnel.tun_id);
232 } else if (f_idx == CLS_F_IDX_METADATA) {
233 eq = !((fixed->metadata ^ wild.flow.metadata)
234 & wild.wc.masks.metadata);
235 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
236 eq = !((fixed->nw_tos ^ wild.flow.nw_tos) &
237 (wild.wc.masks.nw_tos & IP_DSCP_MASK));
238 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
239 eq = !((fixed->nw_proto ^ wild.flow.nw_proto)
240 & wild.wc.masks.nw_proto);
241 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
242 eq = !((fixed->dl_type ^ wild.flow.dl_type)
243 & wild.wc.masks.dl_type);
244 } else if (f_idx == CLS_F_IDX_IN_PORT) {
245 eq = !((fixed->in_port.ofp_port
246 ^ wild.flow.in_port.ofp_port)
247 & wild.wc.masks.in_port.ofp_port);
259 static struct cls_rule *
260 tcls_lookup(const struct tcls *cls, const struct flow *flow)
264 for (i = 0; i < cls->n_rules; i++) {
265 struct test_rule *pos = cls->rules[i];
266 if (match(&pos->cls_rule, flow)) {
267 return &pos->cls_rule;
274 tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
278 for (i = 0; i < cls->n_rules; ) {
279 struct test_rule *pos = cls->rules[i];
280 if (!minimask_has_extra(&pos->cls_rule.match.mask,
281 &target->match.mask)) {
284 miniflow_expand(&pos->cls_rule.match.flow, &flow);
285 if (match(target, &flow)) {
286 tcls_remove(cls, pos);
294 static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
295 CONSTANT_HTONL(0xc0a04455) };
296 static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
297 CONSTANT_HTONL(0xc0a04455) };
298 static ovs_be64 tun_id_values[] = {
300 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
301 static ovs_be64 metadata_values[] = {
303 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
304 static ofp_port_t in_port_values[] = { OFP_PORT_C(1), OFPP_LOCAL };
305 static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
306 static ovs_be16 dl_type_values[]
307 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
308 static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
309 CONSTANT_HTONS(80) };
310 static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
311 static uint8_t dl_src_values[][ETH_ADDR_LEN] = {
312 { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
313 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
314 static uint8_t dl_dst_values[][ETH_ADDR_LEN] = {
315 { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
316 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
317 static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
318 static uint8_t nw_dscp_values[] = { 48, 0 };
320 static void *values[CLS_N_FIELDS][2];
325 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
326 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
328 values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
329 values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
331 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
332 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
334 values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
335 values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
337 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
338 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
340 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
341 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
343 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
344 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
346 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
347 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
349 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
350 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
352 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
353 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
355 values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
356 values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
358 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
359 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
361 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
362 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
365 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
366 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
367 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
368 #define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
369 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
370 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
371 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
372 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
373 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
374 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
375 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
376 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
377 #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
379 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
383 N_VLAN_TCI_VALUES * \
389 N_NW_PROTO_VALUES * \
393 get_value(unsigned int *x, unsigned n_values)
395 unsigned int rem = *x % n_values;
401 compare_classifiers(struct classifier *cls, struct tcls *tcls)
403 static const int confidence = 500;
406 assert(classifier_count(cls) == tcls->n_rules);
407 for (i = 0; i < confidence; i++) {
408 const struct cls_rule *cr0, *cr1, *cr2;
410 struct flow_wildcards wc;
413 flow_wildcards_init_catchall(&wc);
414 x = random_range(N_FLOW_VALUES);
415 memset(&flow, 0, sizeof flow);
416 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
417 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
418 flow.tunnel.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
419 flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
420 flow.in_port.ofp_port = in_port_values[get_value(&x,
422 flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
423 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
424 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
425 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
426 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
428 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
430 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
431 flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
433 /* This assertion is here to suppress a GCC 4.9 array-bounds warning */
434 ovs_assert(cls->n_tries <= CLS_MAX_TRIES);
436 cr0 = classifier_lookup(cls, &flow, &wc);
437 cr1 = tcls_lookup(tcls, &flow);
438 assert((cr0 == NULL) == (cr1 == NULL));
440 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
441 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
443 assert(cls_rule_equal(cr0, cr1));
444 assert(tr0->aux == tr1->aux);
446 cr2 = classifier_lookup(cls, &flow, NULL);
452 destroy_classifier(struct classifier *cls)
454 struct test_rule *rule;
456 classifier_defer(cls);
457 CLS_FOR_EACH (rule, cls_rule, cls) {
458 if (classifier_remove(cls, &rule->cls_rule)) {
459 ovsrcu_postpone(free_rule, rule);
462 classifier_destroy(cls);
466 pvector_verify(const struct pvector *pvec)
468 void *ptr OVS_UNUSED;
469 int prev_priority = INT_MAX;
471 PVECTOR_FOR_EACH (ptr, pvec) {
472 int priority = cursor__.vector[cursor__.entry_idx].priority;
473 if (priority > prev_priority) {
474 ovs_abort(0, "Priority vector is out of order (%u > %u)",
475 priority, prev_priority);
477 prev_priority = priority;
482 trie_verify(const rcu_trie_ptr *trie, unsigned int ofs, unsigned int n_bits)
484 const struct trie_node *node = ovsrcu_get(struct trie_node *, trie);
487 assert(node->n_rules == 0 || node->n_bits > 0);
489 assert((ofs > 0 || (ofs == 0 && node->n_bits == 0)) && ofs <= n_bits);
492 + trie_verify(&node->edges[0], ofs, n_bits)
493 + trie_verify(&node->edges[1], ofs, n_bits);
499 verify_tries(struct classifier *cls)
500 OVS_NO_THREAD_SAFETY_ANALYSIS
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 assert(n_rules <= cls->n_rules);
513 check_tables(const struct classifier *cls, int n_tables, int n_rules,
515 OVS_NO_THREAD_SAFETY_ANALYSIS
517 const struct cls_subtable *table;
518 struct test_rule *test_rule;
519 int found_tables = 0;
522 int found_rules2 = 0;
524 pvector_verify(&cls->subtables);
525 CMAP_FOR_EACH (table, cmap_node, &cls->subtables_map) {
526 const struct cls_match *head;
527 int max_priority = INT_MIN;
528 unsigned int max_count = 0;
530 const struct cls_subtable *iter;
532 /* Locate the subtable from 'subtables'. */
533 PVECTOR_FOR_EACH (iter, &cls->subtables) {
536 ovs_abort(0, "Subtable %p duplicated in 'subtables'.",
543 ovs_abort(0, "Subtable %p not found from 'subtables'.", table);
546 assert(!cmap_is_empty(&table->rules));
547 assert(trie_verify(&table->ports_trie, 0, table->ports_mask_len)
548 == (table->ports_mask_len ? cmap_count(&table->rules) : 0));
551 CMAP_FOR_EACH (head, cmap_node, &table->rules) {
552 int prev_priority = INT_MAX;
553 const struct cls_match *rule;
555 if (head->priority > max_priority) {
556 max_priority = head->priority;
558 } else if (head->priority == max_priority) {
563 RCULIST_FOR_EACH (rule, list, &head->list) {
564 assert(rule->priority < prev_priority);
565 assert(rule->priority <= table->max_priority);
567 prev_priority = rule->priority;
570 assert(classifier_find_rule_exactly(cls, rule->cls_rule)
574 assert(table->max_priority == max_priority);
575 assert(table->max_count == max_count);
578 assert(found_tables == cmap_count(&cls->subtables_map));
579 assert(found_tables == pvector_count(&cls->subtables));
580 assert(n_tables == -1 || n_tables == cmap_count(&cls->subtables_map));
581 assert(n_rules == -1 || found_rules == n_rules);
582 assert(n_dups == -1 || found_dups == n_dups);
584 CLS_FOR_EACH (test_rule, cls_rule, cls) {
587 assert(found_rules == found_rules2);
590 static struct test_rule *
591 make_rule(int wc_fields, int priority, int value_pat)
593 const struct cls_field *f;
594 struct test_rule *rule;
597 match_init_catchall(&match);
598 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
599 int f_idx = f - cls_fields;
600 int value_idx = (value_pat & (1u << f_idx)) != 0;
601 memcpy((char *) &match.flow + f->ofs,
602 values[f_idx][value_idx], f->len);
604 if (f_idx == CLS_F_IDX_NW_SRC) {
605 match.wc.masks.nw_src = OVS_BE32_MAX;
606 } else if (f_idx == CLS_F_IDX_NW_DST) {
607 match.wc.masks.nw_dst = OVS_BE32_MAX;
608 } else if (f_idx == CLS_F_IDX_TP_SRC) {
609 match.wc.masks.tp_src = OVS_BE16_MAX;
610 } else if (f_idx == CLS_F_IDX_TP_DST) {
611 match.wc.masks.tp_dst = OVS_BE16_MAX;
612 } else if (f_idx == CLS_F_IDX_DL_SRC) {
613 memset(match.wc.masks.dl_src, 0xff, ETH_ADDR_LEN);
614 } else if (f_idx == CLS_F_IDX_DL_DST) {
615 memset(match.wc.masks.dl_dst, 0xff, ETH_ADDR_LEN);
616 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
617 match.wc.masks.vlan_tci = OVS_BE16_MAX;
618 } else if (f_idx == CLS_F_IDX_TUN_ID) {
619 match.wc.masks.tunnel.tun_id = OVS_BE64_MAX;
620 } else if (f_idx == CLS_F_IDX_METADATA) {
621 match.wc.masks.metadata = OVS_BE64_MAX;
622 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
623 match.wc.masks.nw_tos |= IP_DSCP_MASK;
624 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
625 match.wc.masks.nw_proto = UINT8_MAX;
626 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
627 match.wc.masks.dl_type = OVS_BE16_MAX;
628 } else if (f_idx == CLS_F_IDX_IN_PORT) {
629 match.wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
635 rule = xzalloc(sizeof *rule);
636 cls_rule_init(&rule->cls_rule, &match, wc_fields
637 ? (priority == INT_MIN ? priority + 1 : priority)
642 static struct test_rule *
643 clone_rule(const struct test_rule *src)
645 struct test_rule *dst;
647 dst = xmalloc(sizeof *dst);
649 cls_rule_clone(&dst->cls_rule, &src->cls_rule);
654 free_rule(struct test_rule *rule)
656 cls_rule_destroy(&rule->cls_rule);
661 shuffle(int *p, size_t n)
663 for (; n > 1; n--, p++) {
664 int *q = &p[random_range(n)];
672 shuffle_u32s(uint32_t *p, size_t n)
674 for (; n > 1; n--, p++) {
675 uint32_t *q = &p[random_range(n)];
682 /* Classifier tests. */
684 static enum mf_field_id trie_fields[2] = {
685 MFF_IPV4_DST, MFF_IPV4_SRC
689 set_prefix_fields(struct classifier *cls)
692 classifier_set_prefix_fields(cls, trie_fields, ARRAY_SIZE(trie_fields));
696 /* Tests an empty classifier. */
698 test_empty(struct ovs_cmdl_context *ctx OVS_UNUSED)
700 struct classifier cls;
703 classifier_init(&cls, flow_segment_u64s);
704 set_prefix_fields(&cls);
706 assert(classifier_is_empty(&cls));
707 assert(tcls_is_empty(&tcls));
708 compare_classifiers(&cls, &tcls);
709 classifier_destroy(&cls);
713 /* Destroys a null classifier. */
715 test_destroy_null(struct ovs_cmdl_context *ctx OVS_UNUSED)
717 classifier_destroy(NULL);
720 /* Tests classification with one rule at a time. */
722 test_single_rule(struct ovs_cmdl_context *ctx OVS_UNUSED)
724 unsigned int wc_fields; /* Hilarious. */
726 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
727 struct classifier cls;
728 struct test_rule *rule, *tcls_rule;
731 rule = make_rule(wc_fields,
732 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
734 classifier_init(&cls, flow_segment_u64s);
735 set_prefix_fields(&cls);
738 tcls_rule = tcls_insert(&tcls, rule);
739 classifier_insert(&cls, &rule->cls_rule, NULL, 0);
740 compare_classifiers(&cls, &tcls);
741 check_tables(&cls, 1, 1, 0);
743 classifier_remove(&cls, &rule->cls_rule);
744 tcls_remove(&tcls, tcls_rule);
745 assert(classifier_is_empty(&cls));
746 assert(tcls_is_empty(&tcls));
747 compare_classifiers(&cls, &tcls);
749 ovsrcu_postpone(free_rule, rule);
750 classifier_destroy(&cls);
755 /* Tests replacing one rule by another. */
757 test_rule_replacement(struct ovs_cmdl_context *ctx OVS_UNUSED)
759 unsigned int wc_fields;
761 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
762 struct classifier cls;
763 struct test_rule *rule1;
764 struct test_rule *rule2;
767 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
768 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
772 classifier_init(&cls, flow_segment_u64s);
773 set_prefix_fields(&cls);
775 tcls_insert(&tcls, rule1);
776 classifier_insert(&cls, &rule1->cls_rule, NULL, 0);
777 compare_classifiers(&cls, &tcls);
778 check_tables(&cls, 1, 1, 0);
782 tcls_insert(&tcls, rule2);
784 assert(test_rule_from_cls_rule(
785 classifier_replace(&cls, &rule2->cls_rule,
787 ovsrcu_postpone(free_rule, rule1);
788 compare_classifiers(&cls, &tcls);
789 check_tables(&cls, 1, 1, 0);
790 classifier_defer(&cls);
791 classifier_remove(&cls, &rule2->cls_rule);
794 destroy_classifier(&cls);
799 factorial(int n_items)
804 for (i = 2; i <= n_items; i++) {
819 reverse(int *a, int n)
823 for (i = 0; i < n / 2; i++) {
830 next_permutation(int *a, int n)
834 for (k = n - 2; k >= 0; k--) {
835 if (a[k] < a[k + 1]) {
838 for (l = n - 1; ; l--) {
841 reverse(a + (k + 1), n - (k + 1));
850 /* Tests classification with rules that have the same matching criteria. */
852 test_many_rules_in_one_list (struct ovs_cmdl_context *ctx OVS_UNUSED)
854 enum { N_RULES = 3 };
857 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
858 int ops[N_RULES * 2];
864 for (i = 1; i < N_RULES; i++) {
865 pris[i] = pris[i - 1] + (n_pris > i);
868 for (i = 0; i < N_RULES * 2; i++) {
874 struct test_rule *rules[N_RULES];
875 struct test_rule *tcls_rules[N_RULES];
876 int pri_rules[N_RULES];
877 struct classifier cls;
882 for (i = 0; i < N_RULES; i++) {
883 rules[i] = make_rule(456, pris[i], 0);
884 tcls_rules[i] = NULL;
888 classifier_init(&cls, flow_segment_u64s);
889 set_prefix_fields(&cls);
892 for (i = 0; i < ARRAY_SIZE(ops); i++) {
896 if (!tcls_rules[j]) {
897 struct test_rule *displaced_rule;
899 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
900 displaced_rule = test_rule_from_cls_rule(
901 classifier_replace(&cls, &rules[j]->cls_rule,
903 if (pri_rules[pris[j]] >= 0) {
904 int k = pri_rules[pris[j]];
905 assert(displaced_rule != NULL);
906 assert(displaced_rule != rules[j]);
907 assert(pris[j] == displaced_rule->cls_rule.priority);
908 tcls_rules[k] = NULL;
910 assert(displaced_rule == NULL);
912 pri_rules[pris[j]] = j;
914 classifier_remove(&cls, &rules[j]->cls_rule);
915 tcls_remove(&tcls, tcls_rules[j]);
916 tcls_rules[j] = NULL;
917 pri_rules[pris[j]] = -1;
919 compare_classifiers(&cls, &tcls);
922 for (m = 0; m < N_RULES; m++) {
923 n += tcls_rules[m] != NULL;
925 check_tables(&cls, n > 0, n, n - 1);
928 classifier_defer(&cls);
929 for (i = 0; i < N_RULES; i++) {
930 if (classifier_remove(&cls, &rules[i]->cls_rule)) {
931 ovsrcu_postpone(free_rule, rules[i]);
934 classifier_destroy(&cls);
936 } while (next_permutation(ops, ARRAY_SIZE(ops)));
937 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
942 count_ones(unsigned long int x)
947 x = zero_rightmost_1bit(x);
955 array_contains(int *array, int n, int value)
959 for (i = 0; i < n; i++) {
960 if (array[i] == value) {
968 /* Tests classification with two rules at a time that fall into the same
969 * table but different lists. */
971 test_many_rules_in_one_table(struct ovs_cmdl_context *ctx OVS_UNUSED)
975 for (iteration = 0; iteration < 50; iteration++) {
976 enum { N_RULES = 20 };
977 struct test_rule *rules[N_RULES];
978 struct test_rule *tcls_rules[N_RULES];
979 struct classifier cls;
981 int value_pats[N_RULES];
987 wcf = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
988 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
989 } while ((1 << count_ones(value_mask)) < N_RULES);
991 classifier_init(&cls, flow_segment_u64s);
992 set_prefix_fields(&cls);
995 for (i = 0; i < N_RULES; i++) {
996 int priority = random_range(INT_MAX);
999 value_pats[i] = random_uint32() & value_mask;
1000 } while (array_contains(value_pats, i, value_pats[i]));
1002 rules[i] = make_rule(wcf, priority, value_pats[i]);
1003 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
1005 classifier_insert(&cls, &rules[i]->cls_rule, NULL, 0);
1006 compare_classifiers(&cls, &tcls);
1008 check_tables(&cls, 1, i + 1, 0);
1011 for (i = 0; i < N_RULES; i++) {
1012 tcls_remove(&tcls, tcls_rules[i]);
1013 classifier_remove(&cls, &rules[i]->cls_rule);
1014 compare_classifiers(&cls, &tcls);
1015 ovsrcu_postpone(free_rule, rules[i]);
1017 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
1020 classifier_destroy(&cls);
1021 tcls_destroy(&tcls);
1025 /* Tests classification with many rules at a time that fall into random lists
1028 test_many_rules_in_n_tables(int n_tables)
1030 enum { MAX_RULES = 50 };
1035 assert(n_tables < 10);
1036 for (i = 0; i < n_tables; i++) {
1038 wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1039 } while (array_contains(wcfs, i, wcfs[i]));
1042 for (iteration = 0; iteration < 30; iteration++) {
1043 int priorities[MAX_RULES];
1044 struct classifier cls;
1047 random_set_seed(iteration + 1);
1048 for (i = 0; i < MAX_RULES; i++) {
1049 priorities[i] = (i * 129) & INT_MAX;
1051 shuffle(priorities, ARRAY_SIZE(priorities));
1053 classifier_init(&cls, flow_segment_u64s);
1054 set_prefix_fields(&cls);
1057 for (i = 0; i < MAX_RULES; i++) {
1058 struct test_rule *rule;
1059 int priority = priorities[i];
1060 int wcf = wcfs[random_range(n_tables)];
1061 int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1062 rule = make_rule(wcf, priority, value_pat);
1063 tcls_insert(&tcls, rule);
1064 classifier_insert(&cls, &rule->cls_rule, NULL, 0);
1065 compare_classifiers(&cls, &tcls);
1066 check_tables(&cls, -1, i + 1, -1);
1069 while (!classifier_is_empty(&cls)) {
1070 struct test_rule *target;
1071 struct test_rule *rule;
1073 target = clone_rule(tcls.rules[random_range(tcls.n_rules)]);
1075 CLS_FOR_EACH_TARGET (rule, cls_rule, &cls, &target->cls_rule) {
1076 if (classifier_remove(&cls, &rule->cls_rule)) {
1077 ovsrcu_postpone(free_rule, rule);
1081 tcls_delete_matches(&tcls, &target->cls_rule);
1082 compare_classifiers(&cls, &tcls);
1083 check_tables(&cls, -1, -1, -1);
1087 destroy_classifier(&cls);
1088 tcls_destroy(&tcls);
1093 test_many_rules_in_two_tables(struct ovs_cmdl_context *ctx OVS_UNUSED)
1095 test_many_rules_in_n_tables(2);
1099 test_many_rules_in_five_tables(struct ovs_cmdl_context *ctx OVS_UNUSED)
1101 test_many_rules_in_n_tables(5);
1104 /* Miniflow tests. */
1109 static const uint32_t values[] =
1110 { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1111 0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1113 return values[random_range(ARRAY_SIZE(values))];
1117 choose(unsigned int n, unsigned int *idxp)
1127 #define FLOW_U32S (FLOW_U64S * 2)
1130 init_consecutive_values(int n_consecutive, struct flow *flow,
1133 uint32_t *flow_u32 = (uint32_t *) flow;
1135 if (choose(FLOW_U32S - n_consecutive + 1, idxp)) {
1138 for (i = 0; i < n_consecutive; i++) {
1139 flow_u32[*idxp + i] = random_value();
1148 next_random_flow(struct flow *flow, unsigned int idx)
1150 uint32_t *flow_u32 = (uint32_t *) flow;
1153 memset(flow, 0, sizeof *flow);
1156 if (choose(1, &idx)) {
1160 /* All flows with a small number of consecutive nonzero values. */
1161 for (i = 1; i <= 4; i++) {
1162 if (init_consecutive_values(i, flow, &idx)) {
1167 /* All flows with a large number of consecutive nonzero values. */
1168 for (i = FLOW_U32S - 4; i <= FLOW_U32S; i++) {
1169 if (init_consecutive_values(i, flow, &idx)) {
1174 /* All flows with exactly two nonconsecutive nonzero values. */
1175 if (choose((FLOW_U32S - 1) * (FLOW_U32S - 2) / 2, &idx)) {
1178 for (ofs1 = 0; ofs1 < FLOW_U32S - 2; ofs1++) {
1181 for (ofs2 = ofs1 + 2; ofs2 < FLOW_U32S; ofs2++) {
1182 if (choose(1, &idx)) {
1183 flow_u32[ofs1] = random_value();
1184 flow_u32[ofs2] = random_value();
1192 /* 16 randomly chosen flows with N >= 3 nonzero values. */
1193 if (choose(16 * (FLOW_U32S - 4), &idx)) {
1194 int n = idx / 16 + 3;
1197 for (i = 0; i < n; i++) {
1198 flow_u32[i] = random_value();
1200 shuffle_u32s(flow_u32, FLOW_U32S);
1209 any_random_flow(struct flow *flow)
1211 static unsigned int max;
1213 while (next_random_flow(flow, max)) {
1218 next_random_flow(flow, random_range(max));
1222 toggle_masked_flow_bits(struct flow *flow, const struct flow_wildcards *mask)
1224 const uint32_t *mask_u32 = (const uint32_t *) &mask->masks;
1225 uint32_t *flow_u32 = (uint32_t *) flow;
1228 for (i = 0; i < FLOW_U32S; i++) {
1229 if (mask_u32[i] != 0) {
1233 bit = 1u << random_range(32);
1234 } while (!(bit & mask_u32[i]));
1241 wildcard_extra_bits(struct flow_wildcards *mask)
1243 uint32_t *mask_u32 = (uint32_t *) &mask->masks;
1246 for (i = 0; i < FLOW_U32S; i++) {
1247 if (mask_u32[i] != 0) {
1251 bit = 1u << random_range(32);
1252 } while (!(bit & mask_u32[i]));
1253 mask_u32[i] &= ~bit;
1259 test_miniflow(struct ovs_cmdl_context *ctx OVS_UNUSED)
1264 random_set_seed(0xb3faca38);
1265 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1266 const uint64_t *flow_u64 = (const uint64_t *) &flow;
1267 struct miniflow miniflow, miniflow2, miniflow3;
1268 struct flow flow2, flow3;
1269 struct flow_wildcards mask;
1270 struct minimask minimask;
1273 /* Convert flow to miniflow. */
1274 miniflow_init(&miniflow, &flow);
1276 /* Check that the flow equals its miniflow. */
1277 assert(miniflow_get_vid(&miniflow) == vlan_tci_to_vid(flow.vlan_tci));
1278 for (i = 0; i < FLOW_U64S; i++) {
1279 assert(miniflow_get(&miniflow, i) == flow_u64[i]);
1282 /* Check that the miniflow equals itself. */
1283 assert(miniflow_equal(&miniflow, &miniflow));
1285 /* Convert miniflow back to flow and verify that it's the same. */
1286 miniflow_expand(&miniflow, &flow2);
1287 assert(flow_equal(&flow, &flow2));
1289 /* Check that copying a miniflow works properly. */
1290 miniflow_clone(&miniflow2, &miniflow);
1291 assert(miniflow_equal(&miniflow, &miniflow2));
1292 assert(miniflow_hash(&miniflow, 0) == miniflow_hash(&miniflow2, 0));
1293 miniflow_expand(&miniflow2, &flow3);
1294 assert(flow_equal(&flow, &flow3));
1296 /* Check that masked matches work as expected for identical flows and
1299 next_random_flow(&mask.masks, 1);
1300 } while (flow_wildcards_is_catchall(&mask));
1301 minimask_init(&minimask, &mask);
1302 assert(minimask_is_catchall(&minimask)
1303 == flow_wildcards_is_catchall(&mask));
1304 assert(miniflow_equal_in_minimask(&miniflow, &miniflow2, &minimask));
1305 assert(miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1306 assert(miniflow_hash_in_minimask(&miniflow, &minimask, 0x12345678) ==
1307 flow_hash_in_minimask(&flow, &minimask, 0x12345678));
1309 /* Check that masked matches work as expected for differing flows and
1311 toggle_masked_flow_bits(&flow2, &mask);
1312 assert(!miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1313 miniflow_init(&miniflow3, &flow2);
1314 assert(!miniflow_equal_in_minimask(&miniflow, &miniflow3, &minimask));
1317 miniflow_destroy(&miniflow);
1318 miniflow_destroy(&miniflow2);
1319 miniflow_destroy(&miniflow3);
1320 minimask_destroy(&minimask);
1325 test_minimask_has_extra(struct ovs_cmdl_context *ctx OVS_UNUSED)
1327 struct flow_wildcards catchall;
1328 struct minimask minicatchall;
1332 flow_wildcards_init_catchall(&catchall);
1333 minimask_init(&minicatchall, &catchall);
1334 assert(minimask_is_catchall(&minicatchall));
1336 random_set_seed(0x2ec7905b);
1337 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1338 struct flow_wildcards mask;
1339 struct minimask minimask;
1342 minimask_init(&minimask, &mask);
1343 assert(!minimask_has_extra(&minimask, &minimask));
1344 assert(minimask_has_extra(&minicatchall, &minimask)
1345 == !minimask_is_catchall(&minimask));
1346 if (!minimask_is_catchall(&minimask)) {
1347 struct minimask minimask2;
1349 wildcard_extra_bits(&mask);
1350 minimask_init(&minimask2, &mask);
1351 assert(minimask_has_extra(&minimask2, &minimask));
1352 assert(!minimask_has_extra(&minimask, &minimask2));
1353 minimask_destroy(&minimask2);
1356 minimask_destroy(&minimask);
1359 minimask_destroy(&minicatchall);
1363 test_minimask_combine(struct ovs_cmdl_context *ctx OVS_UNUSED)
1365 struct flow_wildcards catchall;
1366 struct minimask minicatchall;
1370 flow_wildcards_init_catchall(&catchall);
1371 minimask_init(&minicatchall, &catchall);
1372 assert(minimask_is_catchall(&minicatchall));
1374 random_set_seed(0x181bf0cd);
1375 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1376 struct minimask minimask, minimask2, minicombined;
1377 struct flow_wildcards mask, mask2, combined, combined2;
1378 uint64_t storage[FLOW_U64S];
1382 minimask_init(&minimask, &mask);
1384 minimask_combine(&minicombined, &minimask, &minicatchall, storage);
1385 assert(minimask_is_catchall(&minicombined));
1387 any_random_flow(&flow2);
1388 mask2.masks = flow2;
1389 minimask_init(&minimask2, &mask2);
1391 minimask_combine(&minicombined, &minimask, &minimask2, storage);
1392 flow_wildcards_and(&combined, &mask, &mask2);
1393 minimask_expand(&minicombined, &combined2);
1394 assert(flow_wildcards_equal(&combined, &combined2));
1396 minimask_destroy(&minimask);
1397 minimask_destroy(&minimask2);
1400 minimask_destroy(&minicatchall);
1403 static const struct ovs_cmdl_command commands[] = {
1404 /* Classifier tests. */
1405 {"empty", NULL, 0, 0, test_empty},
1406 {"destroy-null", NULL, 0, 0, test_destroy_null},
1407 {"single-rule", NULL, 0, 0, test_single_rule},
1408 {"rule-replacement", NULL, 0, 0, test_rule_replacement},
1409 {"many-rules-in-one-list", NULL, 0, 0, test_many_rules_in_one_list},
1410 {"many-rules-in-one-table", NULL, 0, 0, test_many_rules_in_one_table},
1411 {"many-rules-in-two-tables", NULL, 0, 0, test_many_rules_in_two_tables},
1412 {"many-rules-in-five-tables", NULL, 0, 0, test_many_rules_in_five_tables},
1414 /* Miniflow and minimask tests. */
1415 {"miniflow", NULL, 0, 0, test_miniflow},
1416 {"minimask_has_extra", NULL, 0, 0, test_minimask_has_extra},
1417 {"minimask_combine", NULL, 0, 0, test_minimask_combine},
1419 {NULL, NULL, 0, 0, NULL},
1423 test_classifier_main(int argc, char *argv[])
1425 struct ovs_cmdl_context ctx = {
1429 set_program_name(argv[0]);
1431 ovs_cmdl_run_command(&ctx, commands);
1434 OVSTEST_REGISTER("test-classifier", test_classifier_main);