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
[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
315 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
316 static uint8_t dl_dst_values
[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
317 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
318 static uint8_t nw_proto_values
[] = { IPPROTO_TCP
, IPPROTO_ICMP
};
319 static uint8_t nw_dscp_values
[] = { 48, 0 };
321 static void *values
[CLS_N_FIELDS
][2];
326 values
[CLS_F_IDX_TUN_ID
][0] = &tun_id_values
[0];
327 values
[CLS_F_IDX_TUN_ID
][1] = &tun_id_values
[1];
329 values
[CLS_F_IDX_METADATA
][0] = &metadata_values
[0];
330 values
[CLS_F_IDX_METADATA
][1] = &metadata_values
[1];
332 values
[CLS_F_IDX_IN_PORT
][0] = &in_port_values
[0];
333 values
[CLS_F_IDX_IN_PORT
][1] = &in_port_values
[1];
335 values
[CLS_F_IDX_VLAN_TCI
][0] = &vlan_tci_values
[0];
336 values
[CLS_F_IDX_VLAN_TCI
][1] = &vlan_tci_values
[1];
338 values
[CLS_F_IDX_DL_SRC
][0] = dl_src_values
[0];
339 values
[CLS_F_IDX_DL_SRC
][1] = dl_src_values
[1];
341 values
[CLS_F_IDX_DL_DST
][0] = dl_dst_values
[0];
342 values
[CLS_F_IDX_DL_DST
][1] = dl_dst_values
[1];
344 values
[CLS_F_IDX_DL_TYPE
][0] = &dl_type_values
[0];
345 values
[CLS_F_IDX_DL_TYPE
][1] = &dl_type_values
[1];
347 values
[CLS_F_IDX_NW_SRC
][0] = &nw_src_values
[0];
348 values
[CLS_F_IDX_NW_SRC
][1] = &nw_src_values
[1];
350 values
[CLS_F_IDX_NW_DST
][0] = &nw_dst_values
[0];
351 values
[CLS_F_IDX_NW_DST
][1] = &nw_dst_values
[1];
353 values
[CLS_F_IDX_NW_PROTO
][0] = &nw_proto_values
[0];
354 values
[CLS_F_IDX_NW_PROTO
][1] = &nw_proto_values
[1];
356 values
[CLS_F_IDX_NW_DSCP
][0] = &nw_dscp_values
[0];
357 values
[CLS_F_IDX_NW_DSCP
][1] = &nw_dscp_values
[1];
359 values
[CLS_F_IDX_TP_SRC
][0] = &tp_src_values
[0];
360 values
[CLS_F_IDX_TP_SRC
][1] = &tp_src_values
[1];
362 values
[CLS_F_IDX_TP_DST
][0] = &tp_dst_values
[0];
363 values
[CLS_F_IDX_TP_DST
][1] = &tp_dst_values
[1];
366 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
367 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
368 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
369 #define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
370 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
371 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
372 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
373 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
374 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
375 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
376 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
377 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
378 #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
380 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
384 N_VLAN_TCI_VALUES * \
390 N_NW_PROTO_VALUES * \
394 get_value(unsigned int *x
, unsigned n_values
)
396 unsigned int rem
= *x
% n_values
;
402 compare_classifiers(struct classifier
*cls
, struct tcls
*tcls
)
403 OVS_REQ_RDLOCK(cls
->rwlock
)
405 static const int confidence
= 500;
408 assert(classifier_count(cls
) == tcls
->n_rules
);
409 for (i
= 0; i
< confidence
; i
++) {
410 struct cls_rule
*cr0
, *cr1
, *cr2
;
412 struct flow_wildcards wc
;
415 flow_wildcards_init_catchall(&wc
);
416 x
= random_range(N_FLOW_VALUES
);
417 memset(&flow
, 0, sizeof flow
);
418 flow
.nw_src
= nw_src_values
[get_value(&x
, N_NW_SRC_VALUES
)];
419 flow
.nw_dst
= nw_dst_values
[get_value(&x
, N_NW_DST_VALUES
)];
420 flow
.tunnel
.tun_id
= tun_id_values
[get_value(&x
, N_TUN_ID_VALUES
)];
421 flow
.metadata
= metadata_values
[get_value(&x
, N_METADATA_VALUES
)];
422 flow
.in_port
.ofp_port
= in_port_values
[get_value(&x
,
424 flow
.vlan_tci
= vlan_tci_values
[get_value(&x
, N_VLAN_TCI_VALUES
)];
425 flow
.dl_type
= dl_type_values
[get_value(&x
, N_DL_TYPE_VALUES
)];
426 flow
.tp_src
= tp_src_values
[get_value(&x
, N_TP_SRC_VALUES
)];
427 flow
.tp_dst
= tp_dst_values
[get_value(&x
, N_TP_DST_VALUES
)];
428 memcpy(flow
.dl_src
, dl_src_values
[get_value(&x
, N_DL_SRC_VALUES
)],
430 memcpy(flow
.dl_dst
, dl_dst_values
[get_value(&x
, N_DL_DST_VALUES
)],
432 flow
.nw_proto
= nw_proto_values
[get_value(&x
, N_NW_PROTO_VALUES
)];
433 flow
.nw_tos
= nw_dscp_values
[get_value(&x
, N_NW_DSCP_VALUES
)];
435 cr0
= classifier_lookup(cls
, &flow
, &wc
);
436 cr1
= tcls_lookup(tcls
, &flow
);
437 assert((cr0
== NULL
) == (cr1
== NULL
));
439 const struct test_rule
*tr0
= test_rule_from_cls_rule(cr0
);
440 const struct test_rule
*tr1
= test_rule_from_cls_rule(cr1
);
442 assert(cls_rule_equal(cr0
, cr1
));
443 assert(tr0
->aux
== tr1
->aux
);
445 cr2
= classifier_lookup(cls
, &flow
, NULL
);
451 destroy_classifier(struct classifier
*cls
)
453 struct test_rule
*rule
, *next_rule
;
454 struct cls_cursor cursor
;
456 fat_rwlock_wrlock(&cls
->rwlock
);
457 cls_cursor_init(&cursor
, cls
, NULL
);
458 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, cls_rule
, &cursor
) {
459 classifier_remove(cls
, &rule
->cls_rule
);
462 fat_rwlock_unlock(&cls
->rwlock
);
463 classifier_destroy(cls
);
467 check_tables(const struct classifier
*cls
, int n_tables
, int n_rules
,
468 int n_dups
) OVS_REQ_RDLOCK(cls
->rwlock
)
470 const struct cls_subtable
*table
;
471 struct test_rule
*test_rule
;
472 struct cls_cursor cursor
;
473 int found_tables
= 0;
476 int found_rules2
= 0;
478 HMAP_FOR_EACH (table
, hmap_node
, &cls
->cls
->subtables
) {
479 const struct cls_rule
*head
;
480 unsigned int max_priority
= 0;
481 unsigned int max_count
= 0;
483 assert(!hmap_is_empty(&table
->rules
));
486 HMAP_FOR_EACH (head
, hmap_node
, &table
->rules
) {
487 unsigned int prev_priority
= UINT_MAX
;
488 const struct cls_rule
*rule
;
490 if (head
->priority
> max_priority
) {
491 max_priority
= head
->priority
;
493 } else if (head
->priority
== max_priority
) {
498 LIST_FOR_EACH (rule
, list
, &head
->list
) {
499 assert(rule
->priority
< prev_priority
);
500 assert(rule
->priority
<= table
->max_priority
);
502 prev_priority
= rule
->priority
;
505 assert(classifier_find_rule_exactly(cls
, rule
) == rule
);
508 assert(table
->max_priority
== max_priority
);
509 assert(table
->max_count
== max_count
);
512 assert(found_tables
== hmap_count(&cls
->cls
->subtables
));
513 assert(n_tables
== -1 || n_tables
== hmap_count(&cls
->cls
->subtables
));
514 assert(n_rules
== -1 || found_rules
== n_rules
);
515 assert(n_dups
== -1 || found_dups
== n_dups
);
517 cls_cursor_init(&cursor
, cls
, NULL
);
518 CLS_CURSOR_FOR_EACH (test_rule
, cls_rule
, &cursor
) {
521 assert(found_rules
== found_rules2
);
524 static struct test_rule
*
525 make_rule(int wc_fields
, unsigned int priority
, int value_pat
)
527 const struct cls_field
*f
;
528 struct test_rule
*rule
;
531 match_init_catchall(&match
);
532 for (f
= &cls_fields
[0]; f
< &cls_fields
[CLS_N_FIELDS
]; f
++) {
533 int f_idx
= f
- cls_fields
;
534 int value_idx
= (value_pat
& (1u << f_idx
)) != 0;
535 memcpy((char *) &match
.flow
+ f
->ofs
,
536 values
[f_idx
][value_idx
], f
->len
);
538 if (f_idx
== CLS_F_IDX_NW_SRC
) {
539 match
.wc
.masks
.nw_src
= OVS_BE32_MAX
;
540 } else if (f_idx
== CLS_F_IDX_NW_DST
) {
541 match
.wc
.masks
.nw_dst
= OVS_BE32_MAX
;
542 } else if (f_idx
== CLS_F_IDX_TP_SRC
) {
543 match
.wc
.masks
.tp_src
= OVS_BE16_MAX
;
544 } else if (f_idx
== CLS_F_IDX_TP_DST
) {
545 match
.wc
.masks
.tp_dst
= OVS_BE16_MAX
;
546 } else if (f_idx
== CLS_F_IDX_DL_SRC
) {
547 memset(match
.wc
.masks
.dl_src
, 0xff, ETH_ADDR_LEN
);
548 } else if (f_idx
== CLS_F_IDX_DL_DST
) {
549 memset(match
.wc
.masks
.dl_dst
, 0xff, ETH_ADDR_LEN
);
550 } else if (f_idx
== CLS_F_IDX_VLAN_TCI
) {
551 match
.wc
.masks
.vlan_tci
= OVS_BE16_MAX
;
552 } else if (f_idx
== CLS_F_IDX_TUN_ID
) {
553 match
.wc
.masks
.tunnel
.tun_id
= OVS_BE64_MAX
;
554 } else if (f_idx
== CLS_F_IDX_METADATA
) {
555 match
.wc
.masks
.metadata
= OVS_BE64_MAX
;
556 } else if (f_idx
== CLS_F_IDX_NW_DSCP
) {
557 match
.wc
.masks
.nw_tos
|= IP_DSCP_MASK
;
558 } else if (f_idx
== CLS_F_IDX_NW_PROTO
) {
559 match
.wc
.masks
.nw_proto
= UINT8_MAX
;
560 } else if (f_idx
== CLS_F_IDX_DL_TYPE
) {
561 match
.wc
.masks
.dl_type
= OVS_BE16_MAX
;
562 } else if (f_idx
== CLS_F_IDX_IN_PORT
) {
563 match
.wc
.masks
.in_port
.ofp_port
= u16_to_ofp(UINT16_MAX
);
569 rule
= xzalloc(sizeof *rule
);
570 cls_rule_init(&rule
->cls_rule
, &match
, wc_fields
? priority
: UINT_MAX
);
574 static struct test_rule
*
575 clone_rule(const struct test_rule
*src
)
577 struct test_rule
*dst
;
579 dst
= xmalloc(sizeof *dst
);
581 cls_rule_clone(&dst
->cls_rule
, &src
->cls_rule
);
586 free_rule(struct test_rule
*rule
)
588 cls_rule_destroy(&rule
->cls_rule
);
593 shuffle(unsigned int *p
, size_t n
)
595 for (; n
> 1; n
--, p
++) {
596 unsigned int *q
= &p
[random_range(n
)];
597 unsigned int tmp
= *p
;
604 shuffle_u32s(uint32_t *p
, size_t n
)
606 for (; n
> 1; n
--, p
++) {
607 uint32_t *q
= &p
[random_range(n
)];
614 /* Classifier tests. */
616 static enum mf_field_id trie_fields
[2] = {
617 MFF_IPV4_DST
, MFF_IPV4_SRC
620 /* Tests an empty classifier. */
622 test_empty(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
624 struct classifier cls
;
627 classifier_init(&cls
, flow_segment_u32s
);
628 fat_rwlock_wrlock(&cls
.rwlock
);
629 classifier_set_prefix_fields(&cls
, trie_fields
, ARRAY_SIZE(trie_fields
));
631 assert(classifier_is_empty(&cls
));
632 assert(tcls_is_empty(&tcls
));
633 compare_classifiers(&cls
, &tcls
);
634 fat_rwlock_unlock(&cls
.rwlock
);
635 classifier_destroy(&cls
);
639 /* Destroys a null classifier. */
641 test_destroy_null(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
643 classifier_destroy(NULL
);
646 /* Tests classification with one rule at a time. */
648 test_single_rule(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
650 unsigned int wc_fields
; /* Hilarious. */
652 for (wc_fields
= 0; wc_fields
< (1u << CLS_N_FIELDS
); wc_fields
++) {
653 struct classifier cls
;
654 struct test_rule
*rule
, *tcls_rule
;
657 rule
= make_rule(wc_fields
,
658 hash_bytes(&wc_fields
, sizeof wc_fields
, 0), 0);
660 classifier_init(&cls
, flow_segment_u32s
);
661 fat_rwlock_wrlock(&cls
.rwlock
);
662 classifier_set_prefix_fields(&cls
, trie_fields
,
663 ARRAY_SIZE(trie_fields
));
666 tcls_rule
= tcls_insert(&tcls
, rule
);
667 classifier_insert(&cls
, &rule
->cls_rule
);
668 check_tables(&cls
, 1, 1, 0);
669 compare_classifiers(&cls
, &tcls
);
671 classifier_remove(&cls
, &rule
->cls_rule
);
672 tcls_remove(&tcls
, tcls_rule
);
673 assert(classifier_is_empty(&cls
));
674 assert(tcls_is_empty(&tcls
));
675 compare_classifiers(&cls
, &tcls
);
678 fat_rwlock_unlock(&cls
.rwlock
);
679 classifier_destroy(&cls
);
684 /* Tests replacing one rule by another. */
686 test_rule_replacement(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
688 unsigned int wc_fields
;
690 for (wc_fields
= 0; wc_fields
< (1u << CLS_N_FIELDS
); wc_fields
++) {
691 struct classifier cls
;
692 struct test_rule
*rule1
;
693 struct test_rule
*rule2
;
696 rule1
= make_rule(wc_fields
, OFP_DEFAULT_PRIORITY
, UINT_MAX
);
697 rule2
= make_rule(wc_fields
, OFP_DEFAULT_PRIORITY
, UINT_MAX
);
701 classifier_init(&cls
, flow_segment_u32s
);
702 fat_rwlock_wrlock(&cls
.rwlock
);
703 classifier_set_prefix_fields(&cls
, trie_fields
,
704 ARRAY_SIZE(trie_fields
));
706 tcls_insert(&tcls
, rule1
);
707 classifier_insert(&cls
, &rule1
->cls_rule
);
708 check_tables(&cls
, 1, 1, 0);
709 compare_classifiers(&cls
, &tcls
);
713 tcls_insert(&tcls
, rule2
);
714 assert(test_rule_from_cls_rule(
715 classifier_replace(&cls
, &rule2
->cls_rule
)) == rule1
);
717 check_tables(&cls
, 1, 1, 0);
718 compare_classifiers(&cls
, &tcls
);
720 fat_rwlock_unlock(&cls
.rwlock
);
721 destroy_classifier(&cls
);
726 factorial(int n_items
)
731 for (i
= 2; i
<= n_items
; i
++) {
746 reverse(int *a
, int n
)
750 for (i
= 0; i
< n
/ 2; i
++) {
757 next_permutation(int *a
, int n
)
761 for (k
= n
- 2; k
>= 0; k
--) {
762 if (a
[k
] < a
[k
+ 1]) {
765 for (l
= n
- 1; ; l
--) {
768 reverse(a
+ (k
+ 1), n
- (k
+ 1));
777 /* Tests classification with rules that have the same matching criteria. */
779 test_many_rules_in_one_list (int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
781 enum { N_RULES
= 3 };
784 for (n_pris
= N_RULES
; n_pris
>= 1; n_pris
--) {
785 int ops
[N_RULES
* 2];
791 for (i
= 1; i
< N_RULES
; i
++) {
792 pris
[i
] = pris
[i
- 1] + (n_pris
> i
);
795 for (i
= 0; i
< N_RULES
* 2; i
++) {
801 struct test_rule
*rules
[N_RULES
];
802 struct test_rule
*tcls_rules
[N_RULES
];
803 int pri_rules
[N_RULES
];
804 struct classifier cls
;
809 for (i
= 0; i
< N_RULES
; i
++) {
810 rules
[i
] = make_rule(456, pris
[i
], 0);
811 tcls_rules
[i
] = NULL
;
815 classifier_init(&cls
, flow_segment_u32s
);
816 fat_rwlock_wrlock(&cls
.rwlock
);
817 classifier_set_prefix_fields(&cls
, trie_fields
,
818 ARRAY_SIZE(trie_fields
));
821 for (i
= 0; i
< ARRAY_SIZE(ops
); i
++) {
825 if (!tcls_rules
[j
]) {
826 struct test_rule
*displaced_rule
;
828 tcls_rules
[j
] = tcls_insert(&tcls
, rules
[j
]);
829 displaced_rule
= test_rule_from_cls_rule(
830 classifier_replace(&cls
, &rules
[j
]->cls_rule
));
831 if (pri_rules
[pris
[j
]] >= 0) {
832 int k
= pri_rules
[pris
[j
]];
833 assert(displaced_rule
!= NULL
);
834 assert(displaced_rule
!= rules
[j
]);
835 assert(pris
[j
] == displaced_rule
->cls_rule
.priority
);
836 tcls_rules
[k
] = NULL
;
838 assert(displaced_rule
== NULL
);
840 pri_rules
[pris
[j
]] = j
;
842 classifier_remove(&cls
, &rules
[j
]->cls_rule
);
843 tcls_remove(&tcls
, tcls_rules
[j
]);
844 tcls_rules
[j
] = NULL
;
845 pri_rules
[pris
[j
]] = -1;
849 for (m
= 0; m
< N_RULES
; m
++) {
850 n
+= tcls_rules
[m
] != NULL
;
852 check_tables(&cls
, n
> 0, n
, n
- 1);
854 compare_classifiers(&cls
, &tcls
);
857 fat_rwlock_unlock(&cls
.rwlock
);
858 classifier_destroy(&cls
);
861 for (i
= 0; i
< N_RULES
; i
++) {
864 } while (next_permutation(ops
, ARRAY_SIZE(ops
)));
865 assert(n_permutations
== (factorial(N_RULES
* 2) >> N_RULES
));
870 count_ones(unsigned long int x
)
875 x
= zero_rightmost_1bit(x
);
883 array_contains(int *array
, int n
, int value
)
887 for (i
= 0; i
< n
; i
++) {
888 if (array
[i
] == value
) {
896 /* Tests classification with two rules at a time that fall into the same
897 * table but different lists. */
899 test_many_rules_in_one_table(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
903 for (iteration
= 0; iteration
< 50; iteration
++) {
904 enum { N_RULES
= 20 };
905 struct test_rule
*rules
[N_RULES
];
906 struct test_rule
*tcls_rules
[N_RULES
];
907 struct classifier cls
;
909 int value_pats
[N_RULES
];
915 wcf
= random_uint32() & ((1u << CLS_N_FIELDS
) - 1);
916 value_mask
= ~wcf
& ((1u << CLS_N_FIELDS
) - 1);
917 } while ((1 << count_ones(value_mask
)) < N_RULES
);
919 classifier_init(&cls
, flow_segment_u32s
);
920 fat_rwlock_wrlock(&cls
.rwlock
);
921 classifier_set_prefix_fields(&cls
, trie_fields
,
922 ARRAY_SIZE(trie_fields
));
925 for (i
= 0; i
< N_RULES
; i
++) {
926 unsigned int priority
= random_uint32();
929 value_pats
[i
] = random_uint32() & value_mask
;
930 } while (array_contains(value_pats
, i
, value_pats
[i
]));
932 rules
[i
] = make_rule(wcf
, priority
, value_pats
[i
]);
933 tcls_rules
[i
] = tcls_insert(&tcls
, rules
[i
]);
934 classifier_insert(&cls
, &rules
[i
]->cls_rule
);
936 check_tables(&cls
, 1, i
+ 1, 0);
937 compare_classifiers(&cls
, &tcls
);
940 for (i
= 0; i
< N_RULES
; i
++) {
941 tcls_remove(&tcls
, tcls_rules
[i
]);
942 classifier_remove(&cls
, &rules
[i
]->cls_rule
);
945 check_tables(&cls
, i
< N_RULES
- 1, N_RULES
- (i
+ 1), 0);
946 compare_classifiers(&cls
, &tcls
);
949 fat_rwlock_unlock(&cls
.rwlock
);
950 classifier_destroy(&cls
);
955 /* Tests classification with many rules at a time that fall into random lists
958 test_many_rules_in_n_tables(int n_tables
)
960 enum { MAX_RULES
= 50 };
965 assert(n_tables
< 10);
966 for (i
= 0; i
< n_tables
; i
++) {
968 wcfs
[i
] = random_uint32() & ((1u << CLS_N_FIELDS
) - 1);
969 } while (array_contains(wcfs
, i
, wcfs
[i
]));
972 for (iteration
= 0; iteration
< 30; iteration
++) {
973 unsigned int priorities
[MAX_RULES
];
974 struct classifier cls
;
977 random_set_seed(iteration
+ 1);
978 for (i
= 0; i
< MAX_RULES
; i
++) {
979 priorities
[i
] = i
* 129;
981 shuffle(priorities
, ARRAY_SIZE(priorities
));
983 classifier_init(&cls
, flow_segment_u32s
);
984 fat_rwlock_wrlock(&cls
.rwlock
);
985 classifier_set_prefix_fields(&cls
, trie_fields
,
986 ARRAY_SIZE(trie_fields
));
989 for (i
= 0; i
< MAX_RULES
; i
++) {
990 struct test_rule
*rule
;
991 unsigned int priority
= priorities
[i
];
992 int wcf
= wcfs
[random_range(n_tables
)];
993 int value_pat
= random_uint32() & ((1u << CLS_N_FIELDS
) - 1);
994 rule
= make_rule(wcf
, priority
, value_pat
);
995 tcls_insert(&tcls
, rule
);
996 classifier_insert(&cls
, &rule
->cls_rule
);
997 check_tables(&cls
, -1, i
+ 1, -1);
998 compare_classifiers(&cls
, &tcls
);
1001 while (!classifier_is_empty(&cls
)) {
1002 struct test_rule
*rule
, *next_rule
;
1003 struct test_rule
*target
;
1004 struct cls_cursor cursor
;
1006 target
= clone_rule(tcls
.rules
[random_range(tcls
.n_rules
)]);
1008 cls_cursor_init(&cursor
, &cls
, &target
->cls_rule
);
1009 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, cls_rule
, &cursor
) {
1010 classifier_remove(&cls
, &rule
->cls_rule
);
1013 tcls_delete_matches(&tcls
, &target
->cls_rule
);
1014 compare_classifiers(&cls
, &tcls
);
1015 check_tables(&cls
, -1, -1, -1);
1019 fat_rwlock_unlock(&cls
.rwlock
);
1020 destroy_classifier(&cls
);
1021 tcls_destroy(&tcls
);
1026 test_many_rules_in_two_tables(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1028 test_many_rules_in_n_tables(2);
1032 test_many_rules_in_five_tables(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1034 test_many_rules_in_n_tables(5);
1037 /* Miniflow tests. */
1042 static const uint32_t values
[] =
1043 { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1044 0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1046 return values
[random_range(ARRAY_SIZE(values
))];
1050 choose(unsigned int n
, unsigned int *idxp
)
1061 init_consecutive_values(int n_consecutive
, struct flow
*flow
,
1064 uint32_t *flow_u32
= (uint32_t *) flow
;
1066 if (choose(FLOW_U32S
- n_consecutive
+ 1, idxp
)) {
1069 for (i
= 0; i
< n_consecutive
; i
++) {
1070 flow_u32
[*idxp
+ i
] = random_value();
1079 next_random_flow(struct flow
*flow
, unsigned int idx
)
1081 uint32_t *flow_u32
= (uint32_t *) flow
;
1084 memset(flow
, 0, sizeof *flow
);
1087 if (choose(1, &idx
)) {
1091 /* All flows with a small number of consecutive nonzero values. */
1092 for (i
= 1; i
<= 4; i
++) {
1093 if (init_consecutive_values(i
, flow
, &idx
)) {
1098 /* All flows with a large number of consecutive nonzero values. */
1099 for (i
= FLOW_U32S
- 4; i
<= FLOW_U32S
; i
++) {
1100 if (init_consecutive_values(i
, flow
, &idx
)) {
1105 /* All flows with exactly two nonconsecutive nonzero values. */
1106 if (choose((FLOW_U32S
- 1) * (FLOW_U32S
- 2) / 2, &idx
)) {
1109 for (ofs1
= 0; ofs1
< FLOW_U32S
- 2; ofs1
++) {
1112 for (ofs2
= ofs1
+ 2; ofs2
< FLOW_U32S
; ofs2
++) {
1113 if (choose(1, &idx
)) {
1114 flow_u32
[ofs1
] = random_value();
1115 flow_u32
[ofs2
] = random_value();
1123 /* 16 randomly chosen flows with N >= 3 nonzero values. */
1124 if (choose(16 * (FLOW_U32S
- 4), &idx
)) {
1125 int n
= idx
/ 16 + 3;
1128 for (i
= 0; i
< n
; i
++) {
1129 flow_u32
[i
] = random_value();
1131 shuffle_u32s(flow_u32
, FLOW_U32S
);
1140 any_random_flow(struct flow
*flow
)
1142 static unsigned int max
;
1144 while (next_random_flow(flow
, max
)) {
1149 next_random_flow(flow
, random_range(max
));
1153 toggle_masked_flow_bits(struct flow
*flow
, const struct flow_wildcards
*mask
)
1155 const uint32_t *mask_u32
= (const uint32_t *) &mask
->masks
;
1156 uint32_t *flow_u32
= (uint32_t *) flow
;
1159 for (i
= 0; i
< FLOW_U32S
; i
++) {
1160 if (mask_u32
[i
] != 0) {
1164 bit
= 1u << random_range(32);
1165 } while (!(bit
& mask_u32
[i
]));
1172 wildcard_extra_bits(struct flow_wildcards
*mask
)
1174 uint32_t *mask_u32
= (uint32_t *) &mask
->masks
;
1177 for (i
= 0; i
< FLOW_U32S
; i
++) {
1178 if (mask_u32
[i
] != 0) {
1182 bit
= 1u << random_range(32);
1183 } while (!(bit
& mask_u32
[i
]));
1184 mask_u32
[i
] &= ~bit
;
1190 test_miniflow(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1195 random_set_seed(0xb3faca38);
1196 for (idx
= 0; next_random_flow(&flow
, idx
); idx
++) {
1197 const uint32_t *flow_u32
= (const uint32_t *) &flow
;
1198 struct miniflow miniflow
, miniflow2
, miniflow3
;
1199 struct flow flow2
, flow3
;
1200 struct flow_wildcards mask
;
1201 struct minimask minimask
;
1204 /* Convert flow to miniflow. */
1205 miniflow_init(&miniflow
, &flow
);
1207 /* Check that the flow equals its miniflow. */
1208 assert(miniflow_get_vid(&miniflow
) == vlan_tci_to_vid(flow
.vlan_tci
));
1209 for (i
= 0; i
< FLOW_U32S
; i
++) {
1210 assert(MINIFLOW_GET_TYPE(&miniflow
, uint32_t, i
* 4)
1214 /* Check that the miniflow equals itself. */
1215 assert(miniflow_equal(&miniflow
, &miniflow
));
1217 /* Convert miniflow back to flow and verify that it's the same. */
1218 miniflow_expand(&miniflow
, &flow2
);
1219 assert(flow_equal(&flow
, &flow2
));
1221 /* Check that copying a miniflow works properly. */
1222 miniflow_clone(&miniflow2
, &miniflow
);
1223 assert(miniflow_equal(&miniflow
, &miniflow2
));
1224 assert(miniflow_hash(&miniflow
, 0) == miniflow_hash(&miniflow2
, 0));
1225 miniflow_expand(&miniflow2
, &flow3
);
1226 assert(flow_equal(&flow
, &flow3
));
1228 /* Check that masked matches work as expected for identical flows and
1231 next_random_flow(&mask
.masks
, 1);
1232 } while (flow_wildcards_is_catchall(&mask
));
1233 minimask_init(&minimask
, &mask
);
1234 assert(minimask_is_catchall(&minimask
)
1235 == flow_wildcards_is_catchall(&mask
));
1236 assert(miniflow_equal_in_minimask(&miniflow
, &miniflow2
, &minimask
));
1237 assert(miniflow_equal_flow_in_minimask(&miniflow
, &flow2
, &minimask
));
1238 assert(miniflow_hash_in_minimask(&miniflow
, &minimask
, 0x12345678) ==
1239 flow_hash_in_minimask(&flow
, &minimask
, 0x12345678));
1241 /* Check that masked matches work as expected for differing flows and
1243 toggle_masked_flow_bits(&flow2
, &mask
);
1244 assert(!miniflow_equal_flow_in_minimask(&miniflow
, &flow2
, &minimask
));
1245 miniflow_init(&miniflow3
, &flow2
);
1246 assert(!miniflow_equal_in_minimask(&miniflow
, &miniflow3
, &minimask
));
1249 miniflow_destroy(&miniflow
);
1250 miniflow_destroy(&miniflow2
);
1251 miniflow_destroy(&miniflow3
);
1252 minimask_destroy(&minimask
);
1257 test_minimask_has_extra(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1259 struct flow_wildcards catchall
;
1260 struct minimask minicatchall
;
1264 flow_wildcards_init_catchall(&catchall
);
1265 minimask_init(&minicatchall
, &catchall
);
1266 assert(minimask_is_catchall(&minicatchall
));
1268 random_set_seed(0x2ec7905b);
1269 for (idx
= 0; next_random_flow(&flow
, idx
); idx
++) {
1270 struct flow_wildcards mask
;
1271 struct minimask minimask
;
1274 minimask_init(&minimask
, &mask
);
1275 assert(!minimask_has_extra(&minimask
, &minimask
));
1276 assert(minimask_has_extra(&minicatchall
, &minimask
)
1277 == !minimask_is_catchall(&minimask
));
1278 if (!minimask_is_catchall(&minimask
)) {
1279 struct minimask minimask2
;
1281 wildcard_extra_bits(&mask
);
1282 minimask_init(&minimask2
, &mask
);
1283 assert(minimask_has_extra(&minimask2
, &minimask
));
1284 assert(!minimask_has_extra(&minimask
, &minimask2
));
1285 minimask_destroy(&minimask2
);
1288 minimask_destroy(&minimask
);
1291 minimask_destroy(&minicatchall
);
1295 test_minimask_combine(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1297 struct flow_wildcards catchall
;
1298 struct minimask minicatchall
;
1302 flow_wildcards_init_catchall(&catchall
);
1303 minimask_init(&minicatchall
, &catchall
);
1304 assert(minimask_is_catchall(&minicatchall
));
1306 random_set_seed(0x181bf0cd);
1307 for (idx
= 0; next_random_flow(&flow
, idx
); idx
++) {
1308 struct minimask minimask
, minimask2
, minicombined
;
1309 struct flow_wildcards mask
, mask2
, combined
, combined2
;
1310 uint32_t storage
[FLOW_U32S
];
1314 minimask_init(&minimask
, &mask
);
1316 minimask_combine(&minicombined
, &minimask
, &minicatchall
, storage
);
1317 assert(minimask_is_catchall(&minicombined
));
1319 any_random_flow(&flow2
);
1320 mask2
.masks
= flow2
;
1321 minimask_init(&minimask2
, &mask2
);
1323 minimask_combine(&minicombined
, &minimask
, &minimask2
, storage
);
1324 flow_wildcards_and(&combined
, &mask
, &mask2
);
1325 minimask_expand(&minicombined
, &combined2
);
1326 assert(flow_wildcards_equal(&combined
, &combined2
));
1328 minimask_destroy(&minimask
);
1329 minimask_destroy(&minimask2
);
1332 minimask_destroy(&minicatchall
);
1335 static const struct command commands
[] = {
1336 /* Classifier tests. */
1337 {"empty", 0, 0, test_empty
},
1338 {"destroy-null", 0, 0, test_destroy_null
},
1339 {"single-rule", 0, 0, test_single_rule
},
1340 {"rule-replacement", 0, 0, test_rule_replacement
},
1341 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list
},
1342 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table
},
1343 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables
},
1344 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables
},
1346 /* Miniflow and minimask tests. */
1347 {"miniflow", 0, 0, test_miniflow
},
1348 {"minimask_has_extra", 0, 0, test_minimask_has_extra
},
1349 {"minimask_combine", 0, 0, test_minimask_combine
},
1355 test_classifier_main(int argc
, char *argv
[])
1357 set_program_name(argv
[0]);
1359 run_command(argc
- 1, argv
+ 1, commands
);
1362 OVSTEST_REGISTER("test-classifier", test_classifier_main
);