2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* "White box" tests for classifier.
19 * With very few exceptions, these tests obtain complete coverage of every
20 * basic block and every branch in the classifier implementation, e.g. a clean
21 * report from "gcov -b". (Covering the exceptions would require finding
22 * collisions in the hash function used for flow data, etc.)
24 * This test should receive a clean report from "valgrind --leak-check=full":
25 * it frees every heap block that it allocates.
29 #include "classifier.h"
32 #include "byte-order.h"
33 #include "command-line.h"
37 #include "unaligned.h"
42 /* Fields in a rule. */
44 /* struct flow all-caps */ \
45 /* member name name */ \
46 /* ----------- -------- */ \
47 CLS_FIELD(tunnel.tun_id, TUN_ID) \
48 CLS_FIELD(metadata, METADATA) \
49 CLS_FIELD(nw_src, NW_SRC) \
50 CLS_FIELD(nw_dst, NW_DST) \
51 CLS_FIELD(in_port, IN_PORT) \
52 CLS_FIELD(vlan_tci, VLAN_TCI) \
53 CLS_FIELD(dl_type, DL_TYPE) \
54 CLS_FIELD(tp_src, TP_SRC) \
55 CLS_FIELD(tp_dst, TP_DST) \
56 CLS_FIELD(dl_src, DL_SRC) \
57 CLS_FIELD(dl_dst, DL_DST) \
58 CLS_FIELD(nw_proto, NW_PROTO) \
59 CLS_FIELD(nw_tos, NW_DSCP)
63 * (These are also indexed into struct classifier's 'tables' array.) */
65 #define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME,
71 /* Field information. */
73 int ofs
; /* Offset in struct flow. */
74 int len
; /* Length in bytes. */
75 const char *name
; /* Name (for debugging). */
78 static const struct cls_field cls_fields
[CLS_N_FIELDS
] = {
79 #define CLS_FIELD(MEMBER, NAME) \
80 { offsetof(struct flow, MEMBER), \
81 sizeof ((struct flow *)0)->MEMBER, \
88 int aux
; /* Auxiliary data. */
89 struct cls_rule cls_rule
; /* Classifier rule data. */
92 static struct test_rule
*
93 test_rule_from_cls_rule(const struct cls_rule
*rule
)
95 return rule
? CONTAINER_OF(rule
, struct test_rule
, cls_rule
) : NULL
;
99 test_rule_destroy(struct test_rule
*rule
)
102 cls_rule_destroy(&rule
->cls_rule
);
107 static struct test_rule
*make_rule(int wc_fields
, unsigned int priority
,
109 static void free_rule(struct test_rule
*);
110 static struct test_rule
*clone_rule(const struct test_rule
*);
112 /* Trivial (linear) classifier. */
115 size_t allocated_rules
;
116 struct test_rule
**rules
;
120 tcls_init(struct tcls
*tcls
)
123 tcls
->allocated_rules
= 0;
128 tcls_destroy(struct tcls
*tcls
)
133 for (i
= 0; i
< tcls
->n_rules
; i
++) {
134 test_rule_destroy(tcls
->rules
[i
]);
141 tcls_is_empty(const struct tcls
*tcls
)
143 return tcls
->n_rules
== 0;
146 static struct test_rule
*
147 tcls_insert(struct tcls
*tcls
, const struct test_rule
*rule
)
151 for (i
= 0; i
< tcls
->n_rules
; i
++) {
152 const struct cls_rule
*pos
= &tcls
->rules
[i
]->cls_rule
;
153 if (cls_rule_equal(pos
, &rule
->cls_rule
)) {
155 free_rule(tcls
->rules
[i
]);
156 tcls
->rules
[i
] = clone_rule(rule
);
157 return tcls
->rules
[i
];
158 } else if (pos
->priority
< rule
->cls_rule
.priority
) {
163 if (tcls
->n_rules
>= tcls
->allocated_rules
) {
164 tcls
->rules
= x2nrealloc(tcls
->rules
, &tcls
->allocated_rules
,
165 sizeof *tcls
->rules
);
167 if (i
!= tcls
->n_rules
) {
168 memmove(&tcls
->rules
[i
+ 1], &tcls
->rules
[i
],
169 sizeof *tcls
->rules
* (tcls
->n_rules
- i
));
171 tcls
->rules
[i
] = clone_rule(rule
);
173 return tcls
->rules
[i
];
177 tcls_remove(struct tcls
*cls
, const struct test_rule
*rule
)
181 for (i
= 0; i
< cls
->n_rules
; i
++) {
182 struct test_rule
*pos
= cls
->rules
[i
];
184 test_rule_destroy(pos
);
186 memmove(&cls
->rules
[i
], &cls
->rules
[i
+ 1],
187 sizeof *cls
->rules
* (cls
->n_rules
- i
- 1));
197 match(const struct cls_rule
*wild_
, const struct flow
*fixed
)
202 minimatch_expand(&wild_
->match
, &wild
);
203 for (f_idx
= 0; f_idx
< CLS_N_FIELDS
; f_idx
++) {
206 if (f_idx
== CLS_F_IDX_NW_SRC
) {
207 eq
= !((fixed
->nw_src
^ wild
.flow
.nw_src
)
208 & wild
.wc
.masks
.nw_src
);
209 } else if (f_idx
== CLS_F_IDX_NW_DST
) {
210 eq
= !((fixed
->nw_dst
^ wild
.flow
.nw_dst
)
211 & wild
.wc
.masks
.nw_dst
);
212 } else if (f_idx
== CLS_F_IDX_TP_SRC
) {
213 eq
= !((fixed
->tp_src
^ wild
.flow
.tp_src
)
214 & wild
.wc
.masks
.tp_src
);
215 } else if (f_idx
== CLS_F_IDX_TP_DST
) {
216 eq
= !((fixed
->tp_dst
^ wild
.flow
.tp_dst
)
217 & wild
.wc
.masks
.tp_dst
);
218 } else if (f_idx
== CLS_F_IDX_DL_SRC
) {
219 eq
= eth_addr_equal_except(fixed
->dl_src
, wild
.flow
.dl_src
,
220 wild
.wc
.masks
.dl_src
);
221 } else if (f_idx
== CLS_F_IDX_DL_DST
) {
222 eq
= eth_addr_equal_except(fixed
->dl_dst
, wild
.flow
.dl_dst
,
223 wild
.wc
.masks
.dl_dst
);
224 } else if (f_idx
== CLS_F_IDX_VLAN_TCI
) {
225 eq
= !((fixed
->vlan_tci
^ wild
.flow
.vlan_tci
)
226 & wild
.wc
.masks
.vlan_tci
);
227 } else if (f_idx
== CLS_F_IDX_TUN_ID
) {
228 eq
= !((fixed
->tunnel
.tun_id
^ wild
.flow
.tunnel
.tun_id
)
229 & wild
.wc
.masks
.tunnel
.tun_id
);
230 } else if (f_idx
== CLS_F_IDX_METADATA
) {
231 eq
= !((fixed
->metadata
^ wild
.flow
.metadata
)
232 & wild
.wc
.masks
.metadata
);
233 } else if (f_idx
== CLS_F_IDX_NW_DSCP
) {
234 eq
= !((fixed
->nw_tos
^ wild
.flow
.nw_tos
) &
235 (wild
.wc
.masks
.nw_tos
& IP_DSCP_MASK
));
236 } else if (f_idx
== CLS_F_IDX_NW_PROTO
) {
237 eq
= !((fixed
->nw_proto
^ wild
.flow
.nw_proto
)
238 & wild
.wc
.masks
.nw_proto
);
239 } else if (f_idx
== CLS_F_IDX_DL_TYPE
) {
240 eq
= !((fixed
->dl_type
^ wild
.flow
.dl_type
)
241 & wild
.wc
.masks
.dl_type
);
242 } else if (f_idx
== CLS_F_IDX_IN_PORT
) {
243 eq
= !((fixed
->in_port
^ wild
.flow
.in_port
)
244 & wild
.wc
.masks
.in_port
);
256 static struct cls_rule
*
257 tcls_lookup(const struct tcls
*cls
, const struct flow
*flow
)
261 for (i
= 0; i
< cls
->n_rules
; i
++) {
262 struct test_rule
*pos
= cls
->rules
[i
];
263 if (match(&pos
->cls_rule
, flow
)) {
264 return &pos
->cls_rule
;
271 tcls_delete_matches(struct tcls
*cls
, const struct cls_rule
*target
)
275 for (i
= 0; i
< cls
->n_rules
; ) {
276 struct test_rule
*pos
= cls
->rules
[i
];
277 if (!minimask_has_extra(&pos
->cls_rule
.match
.mask
,
278 &target
->match
.mask
)) {
281 miniflow_expand(&pos
->cls_rule
.match
.flow
, &flow
);
282 if (match(target
, &flow
)) {
283 tcls_remove(cls
, pos
);
291 static ovs_be32 nw_src_values
[] = { CONSTANT_HTONL(0xc0a80001),
292 CONSTANT_HTONL(0xc0a04455) };
293 static ovs_be32 nw_dst_values
[] = { CONSTANT_HTONL(0xc0a80002),
294 CONSTANT_HTONL(0xc0a04455) };
295 static ovs_be64 tun_id_values
[] = {
297 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
298 static ovs_be64 metadata_values
[] = {
300 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
301 static uint16_t in_port_values
[] = { 1, OFPP_LOCAL
};
302 static ovs_be16 vlan_tci_values
[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
303 static ovs_be16 dl_type_values
[]
304 = { CONSTANT_HTONS(ETH_TYPE_IP
), CONSTANT_HTONS(ETH_TYPE_ARP
) };
305 static ovs_be16 tp_src_values
[] = { CONSTANT_HTONS(49362),
306 CONSTANT_HTONS(80) };
307 static ovs_be16 tp_dst_values
[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
308 static uint8_t dl_src_values
[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
309 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
310 static uint8_t dl_dst_values
[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
311 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
312 static uint8_t nw_proto_values
[] = { IPPROTO_TCP
, IPPROTO_ICMP
};
313 static uint8_t nw_dscp_values
[] = { 48, 0 };
315 static void *values
[CLS_N_FIELDS
][2];
320 values
[CLS_F_IDX_TUN_ID
][0] = &tun_id_values
[0];
321 values
[CLS_F_IDX_TUN_ID
][1] = &tun_id_values
[1];
323 values
[CLS_F_IDX_METADATA
][0] = &metadata_values
[0];
324 values
[CLS_F_IDX_METADATA
][1] = &metadata_values
[1];
326 values
[CLS_F_IDX_IN_PORT
][0] = &in_port_values
[0];
327 values
[CLS_F_IDX_IN_PORT
][1] = &in_port_values
[1];
329 values
[CLS_F_IDX_VLAN_TCI
][0] = &vlan_tci_values
[0];
330 values
[CLS_F_IDX_VLAN_TCI
][1] = &vlan_tci_values
[1];
332 values
[CLS_F_IDX_DL_SRC
][0] = dl_src_values
[0];
333 values
[CLS_F_IDX_DL_SRC
][1] = dl_src_values
[1];
335 values
[CLS_F_IDX_DL_DST
][0] = dl_dst_values
[0];
336 values
[CLS_F_IDX_DL_DST
][1] = dl_dst_values
[1];
338 values
[CLS_F_IDX_DL_TYPE
][0] = &dl_type_values
[0];
339 values
[CLS_F_IDX_DL_TYPE
][1] = &dl_type_values
[1];
341 values
[CLS_F_IDX_NW_SRC
][0] = &nw_src_values
[0];
342 values
[CLS_F_IDX_NW_SRC
][1] = &nw_src_values
[1];
344 values
[CLS_F_IDX_NW_DST
][0] = &nw_dst_values
[0];
345 values
[CLS_F_IDX_NW_DST
][1] = &nw_dst_values
[1];
347 values
[CLS_F_IDX_NW_PROTO
][0] = &nw_proto_values
[0];
348 values
[CLS_F_IDX_NW_PROTO
][1] = &nw_proto_values
[1];
350 values
[CLS_F_IDX_NW_DSCP
][0] = &nw_dscp_values
[0];
351 values
[CLS_F_IDX_NW_DSCP
][1] = &nw_dscp_values
[1];
353 values
[CLS_F_IDX_TP_SRC
][0] = &tp_src_values
[0];
354 values
[CLS_F_IDX_TP_SRC
][1] = &tp_src_values
[1];
356 values
[CLS_F_IDX_TP_DST
][0] = &tp_dst_values
[0];
357 values
[CLS_F_IDX_TP_DST
][1] = &tp_dst_values
[1];
360 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
361 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
362 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
363 #define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
364 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
365 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
366 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
367 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
368 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
369 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
370 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
371 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
372 #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
374 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
378 N_VLAN_TCI_VALUES * \
384 N_NW_PROTO_VALUES * \
388 get_value(unsigned int *x
, unsigned n_values
)
390 unsigned int rem
= *x
% n_values
;
396 compare_classifiers(struct classifier
*cls
, struct tcls
*tcls
)
398 static const int confidence
= 500;
401 assert(classifier_count(cls
) == tcls
->n_rules
);
402 for (i
= 0; i
< confidence
; i
++) {
403 struct cls_rule
*cr0
, *cr1
;
407 x
= rand () % N_FLOW_VALUES
;
408 memset(&flow
, 0, sizeof flow
);
409 flow
.nw_src
= nw_src_values
[get_value(&x
, N_NW_SRC_VALUES
)];
410 flow
.nw_dst
= nw_dst_values
[get_value(&x
, N_NW_DST_VALUES
)];
411 flow
.tunnel
.tun_id
= tun_id_values
[get_value(&x
, N_TUN_ID_VALUES
)];
412 flow
.metadata
= metadata_values
[get_value(&x
, N_METADATA_VALUES
)];
413 flow
.in_port
= in_port_values
[get_value(&x
, N_IN_PORT_VALUES
)];
414 flow
.vlan_tci
= vlan_tci_values
[get_value(&x
, N_VLAN_TCI_VALUES
)];
415 flow
.dl_type
= dl_type_values
[get_value(&x
, N_DL_TYPE_VALUES
)];
416 flow
.tp_src
= tp_src_values
[get_value(&x
, N_TP_SRC_VALUES
)];
417 flow
.tp_dst
= tp_dst_values
[get_value(&x
, N_TP_DST_VALUES
)];
418 memcpy(flow
.dl_src
, dl_src_values
[get_value(&x
, N_DL_SRC_VALUES
)],
420 memcpy(flow
.dl_dst
, dl_dst_values
[get_value(&x
, N_DL_DST_VALUES
)],
422 flow
.nw_proto
= nw_proto_values
[get_value(&x
, N_NW_PROTO_VALUES
)];
423 flow
.nw_tos
= nw_dscp_values
[get_value(&x
, N_NW_DSCP_VALUES
)];
425 cr0
= classifier_lookup(cls
, &flow
, NULL
);
426 cr1
= tcls_lookup(tcls
, &flow
);
427 assert((cr0
== NULL
) == (cr1
== NULL
));
429 const struct test_rule
*tr0
= test_rule_from_cls_rule(cr0
);
430 const struct test_rule
*tr1
= test_rule_from_cls_rule(cr1
);
432 assert(cls_rule_equal(cr0
, cr1
));
433 assert(tr0
->aux
== tr1
->aux
);
439 destroy_classifier(struct classifier
*cls
)
441 struct test_rule
*rule
, *next_rule
;
442 struct cls_cursor cursor
;
444 cls_cursor_init(&cursor
, cls
, NULL
);
445 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, cls_rule
, &cursor
) {
446 classifier_remove(cls
, &rule
->cls_rule
);
449 classifier_destroy(cls
);
453 check_tables(const struct classifier
*cls
,
454 int n_tables
, int n_rules
, int n_dups
)
456 const struct cls_table
*table
;
457 struct test_rule
*test_rule
;
458 struct cls_cursor cursor
;
459 int found_tables
= 0;
462 int found_rules2
= 0;
464 HMAP_FOR_EACH (table
, hmap_node
, &cls
->tables
) {
465 const struct cls_rule
*head
;
466 unsigned int max_priority
= 0;
467 unsigned int max_count
= 0;
469 assert(!hmap_is_empty(&table
->rules
));
472 HMAP_FOR_EACH (head
, hmap_node
, &table
->rules
) {
473 unsigned int prev_priority
= UINT_MAX
;
474 const struct cls_rule
*rule
;
476 if (head
->priority
> max_priority
) {
477 max_priority
= head
->priority
;
479 } else if (head
->priority
== max_priority
) {
484 LIST_FOR_EACH (rule
, list
, &head
->list
) {
485 assert(rule
->priority
< prev_priority
);
486 assert(rule
->priority
<= table
->max_priority
);
488 prev_priority
= rule
->priority
;
491 assert(classifier_find_rule_exactly(cls
, rule
) == rule
);
494 assert(table
->max_priority
== max_priority
);
495 assert(table
->max_count
== max_count
);
498 assert(found_tables
== hmap_count(&cls
->tables
));
499 assert(n_tables
== -1 || n_tables
== hmap_count(&cls
->tables
));
500 assert(n_rules
== -1 || found_rules
== n_rules
);
501 assert(n_dups
== -1 || found_dups
== n_dups
);
503 cls_cursor_init(&cursor
, cls
, NULL
);
504 CLS_CURSOR_FOR_EACH (test_rule
, cls_rule
, &cursor
) {
507 assert(found_rules
== found_rules2
);
510 static struct test_rule
*
511 make_rule(int wc_fields
, unsigned int priority
, int value_pat
)
513 const struct cls_field
*f
;
514 struct test_rule
*rule
;
517 match_init_catchall(&match
);
518 for (f
= &cls_fields
[0]; f
< &cls_fields
[CLS_N_FIELDS
]; f
++) {
519 int f_idx
= f
- cls_fields
;
520 int value_idx
= (value_pat
& (1u << f_idx
)) != 0;
521 memcpy((char *) &match
.flow
+ f
->ofs
,
522 values
[f_idx
][value_idx
], f
->len
);
524 if (f_idx
== CLS_F_IDX_NW_SRC
) {
525 match
.wc
.masks
.nw_src
= htonl(UINT32_MAX
);
526 } else if (f_idx
== CLS_F_IDX_NW_DST
) {
527 match
.wc
.masks
.nw_dst
= htonl(UINT32_MAX
);
528 } else if (f_idx
== CLS_F_IDX_TP_SRC
) {
529 match
.wc
.masks
.tp_src
= htons(UINT16_MAX
);
530 } else if (f_idx
== CLS_F_IDX_TP_DST
) {
531 match
.wc
.masks
.tp_dst
= htons(UINT16_MAX
);
532 } else if (f_idx
== CLS_F_IDX_DL_SRC
) {
533 memset(match
.wc
.masks
.dl_src
, 0xff, ETH_ADDR_LEN
);
534 } else if (f_idx
== CLS_F_IDX_DL_DST
) {
535 memset(match
.wc
.masks
.dl_dst
, 0xff, ETH_ADDR_LEN
);
536 } else if (f_idx
== CLS_F_IDX_VLAN_TCI
) {
537 match
.wc
.masks
.vlan_tci
= htons(UINT16_MAX
);
538 } else if (f_idx
== CLS_F_IDX_TUN_ID
) {
539 match
.wc
.masks
.tunnel
.tun_id
= htonll(UINT64_MAX
);
540 } else if (f_idx
== CLS_F_IDX_METADATA
) {
541 match
.wc
.masks
.metadata
= htonll(UINT64_MAX
);
542 } else if (f_idx
== CLS_F_IDX_NW_DSCP
) {
543 match
.wc
.masks
.nw_tos
|= IP_DSCP_MASK
;
544 } else if (f_idx
== CLS_F_IDX_NW_PROTO
) {
545 match
.wc
.masks
.nw_proto
= UINT8_MAX
;
546 } else if (f_idx
== CLS_F_IDX_DL_TYPE
) {
547 match
.wc
.masks
.dl_type
= htons(UINT16_MAX
);
548 } else if (f_idx
== CLS_F_IDX_IN_PORT
) {
549 match
.wc
.masks
.in_port
= UINT16_MAX
;
555 rule
= xzalloc(sizeof *rule
);
556 cls_rule_init(&rule
->cls_rule
, &match
, wc_fields
? priority
: UINT_MAX
);
560 static struct test_rule
*
561 clone_rule(const struct test_rule
*src
)
563 struct test_rule
*dst
;
565 dst
= xmalloc(sizeof *dst
);
567 cls_rule_clone(&dst
->cls_rule
, &src
->cls_rule
);
572 free_rule(struct test_rule
*rule
)
574 cls_rule_destroy(&rule
->cls_rule
);
579 shuffle(unsigned int *p
, size_t n
)
581 for (; n
> 1; n
--, p
++) {
582 unsigned int *q
= &p
[rand() % n
];
583 unsigned int tmp
= *p
;
590 shuffle_u32s(uint32_t *p
, size_t n
)
592 for (; n
> 1; n
--, p
++) {
593 uint32_t *q
= &p
[rand() % n
];
600 /* Classifier tests. */
602 /* Tests an empty classifier. */
604 test_empty(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
606 struct classifier cls
;
609 classifier_init(&cls
);
611 assert(classifier_is_empty(&cls
));
612 assert(tcls_is_empty(&tcls
));
613 compare_classifiers(&cls
, &tcls
);
614 classifier_destroy(&cls
);
618 /* Destroys a null classifier. */
620 test_destroy_null(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
622 classifier_destroy(NULL
);
625 /* Tests classification with one rule at a time. */
627 test_single_rule(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
629 unsigned int wc_fields
; /* Hilarious. */
631 for (wc_fields
= 0; wc_fields
< (1u << CLS_N_FIELDS
); wc_fields
++) {
632 struct classifier cls
;
633 struct test_rule
*rule
, *tcls_rule
;
636 rule
= make_rule(wc_fields
,
637 hash_bytes(&wc_fields
, sizeof wc_fields
, 0), 0);
639 classifier_init(&cls
);
642 tcls_rule
= tcls_insert(&tcls
, rule
);
643 classifier_insert(&cls
, &rule
->cls_rule
);
644 check_tables(&cls
, 1, 1, 0);
645 compare_classifiers(&cls
, &tcls
);
647 classifier_remove(&cls
, &rule
->cls_rule
);
648 tcls_remove(&tcls
, tcls_rule
);
649 assert(classifier_is_empty(&cls
));
650 assert(tcls_is_empty(&tcls
));
651 compare_classifiers(&cls
, &tcls
);
654 classifier_destroy(&cls
);
659 /* Tests replacing one rule by another. */
661 test_rule_replacement(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
663 unsigned int wc_fields
;
665 for (wc_fields
= 0; wc_fields
< (1u << CLS_N_FIELDS
); wc_fields
++) {
666 struct classifier cls
;
667 struct test_rule
*rule1
;
668 struct test_rule
*rule2
;
671 rule1
= make_rule(wc_fields
, OFP_DEFAULT_PRIORITY
, UINT_MAX
);
672 rule2
= make_rule(wc_fields
, OFP_DEFAULT_PRIORITY
, UINT_MAX
);
676 classifier_init(&cls
);
678 tcls_insert(&tcls
, rule1
);
679 classifier_insert(&cls
, &rule1
->cls_rule
);
680 check_tables(&cls
, 1, 1, 0);
681 compare_classifiers(&cls
, &tcls
);
685 tcls_insert(&tcls
, rule2
);
686 assert(test_rule_from_cls_rule(
687 classifier_replace(&cls
, &rule2
->cls_rule
)) == rule1
);
689 check_tables(&cls
, 1, 1, 0);
690 compare_classifiers(&cls
, &tcls
);
692 destroy_classifier(&cls
);
697 factorial(int n_items
)
702 for (i
= 2; i
<= n_items
; i
++) {
717 reverse(int *a
, int n
)
721 for (i
= 0; i
< n
/ 2; i
++) {
728 next_permutation(int *a
, int n
)
732 for (k
= n
- 2; k
>= 0; k
--) {
733 if (a
[k
] < a
[k
+ 1]) {
736 for (l
= n
- 1; ; l
--) {
739 reverse(a
+ (k
+ 1), n
- (k
+ 1));
748 /* Tests classification with rules that have the same matching criteria. */
750 test_many_rules_in_one_list (int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
752 enum { N_RULES
= 3 };
755 for (n_pris
= N_RULES
; n_pris
>= 1; n_pris
--) {
756 int ops
[N_RULES
* 2];
762 for (i
= 1; i
< N_RULES
; i
++) {
763 pris
[i
] = pris
[i
- 1] + (n_pris
> i
);
766 for (i
= 0; i
< N_RULES
* 2; i
++) {
772 struct test_rule
*rules
[N_RULES
];
773 struct test_rule
*tcls_rules
[N_RULES
];
774 int pri_rules
[N_RULES
];
775 struct classifier cls
;
780 for (i
= 0; i
< N_RULES
; i
++) {
781 rules
[i
] = make_rule(456, pris
[i
], 0);
782 tcls_rules
[i
] = NULL
;
786 classifier_init(&cls
);
789 for (i
= 0; i
< ARRAY_SIZE(ops
); i
++) {
793 if (!tcls_rules
[j
]) {
794 struct test_rule
*displaced_rule
;
796 tcls_rules
[j
] = tcls_insert(&tcls
, rules
[j
]);
797 displaced_rule
= test_rule_from_cls_rule(
798 classifier_replace(&cls
, &rules
[j
]->cls_rule
));
799 if (pri_rules
[pris
[j
]] >= 0) {
800 int k
= pri_rules
[pris
[j
]];
801 assert(displaced_rule
!= NULL
);
802 assert(displaced_rule
!= rules
[j
]);
803 assert(pris
[j
] == displaced_rule
->cls_rule
.priority
);
804 tcls_rules
[k
] = NULL
;
806 assert(displaced_rule
== NULL
);
808 pri_rules
[pris
[j
]] = j
;
810 classifier_remove(&cls
, &rules
[j
]->cls_rule
);
811 tcls_remove(&tcls
, tcls_rules
[j
]);
812 tcls_rules
[j
] = NULL
;
813 pri_rules
[pris
[j
]] = -1;
817 for (m
= 0; m
< N_RULES
; m
++) {
818 n
+= tcls_rules
[m
] != NULL
;
820 check_tables(&cls
, n
> 0, n
, n
- 1);
822 compare_classifiers(&cls
, &tcls
);
825 classifier_destroy(&cls
);
828 for (i
= 0; i
< N_RULES
; i
++) {
831 } while (next_permutation(ops
, ARRAY_SIZE(ops
)));
832 assert(n_permutations
== (factorial(N_RULES
* 2) >> N_RULES
));
837 count_ones(unsigned long int x
)
842 x
= zero_rightmost_1bit(x
);
850 array_contains(int *array
, int n
, int value
)
854 for (i
= 0; i
< n
; i
++) {
855 if (array
[i
] == value
) {
863 /* Tests classification with two rules at a time that fall into the same
864 * table but different lists. */
866 test_many_rules_in_one_table(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
870 for (iteration
= 0; iteration
< 50; iteration
++) {
871 enum { N_RULES
= 20 };
872 struct test_rule
*rules
[N_RULES
];
873 struct test_rule
*tcls_rules
[N_RULES
];
874 struct classifier cls
;
876 int value_pats
[N_RULES
];
882 wcf
= rand() & ((1u << CLS_N_FIELDS
) - 1);
883 value_mask
= ~wcf
& ((1u << CLS_N_FIELDS
) - 1);
884 } while ((1 << count_ones(value_mask
)) < N_RULES
);
886 classifier_init(&cls
);
889 for (i
= 0; i
< N_RULES
; i
++) {
890 unsigned int priority
= rand();
893 value_pats
[i
] = rand() & value_mask
;
894 } while (array_contains(value_pats
, i
, value_pats
[i
]));
896 rules
[i
] = make_rule(wcf
, priority
, value_pats
[i
]);
897 tcls_rules
[i
] = tcls_insert(&tcls
, rules
[i
]);
898 classifier_insert(&cls
, &rules
[i
]->cls_rule
);
900 check_tables(&cls
, 1, i
+ 1, 0);
901 compare_classifiers(&cls
, &tcls
);
904 for (i
= 0; i
< N_RULES
; i
++) {
905 tcls_remove(&tcls
, tcls_rules
[i
]);
906 classifier_remove(&cls
, &rules
[i
]->cls_rule
);
909 check_tables(&cls
, i
< N_RULES
- 1, N_RULES
- (i
+ 1), 0);
910 compare_classifiers(&cls
, &tcls
);
913 classifier_destroy(&cls
);
918 /* Tests classification with many rules at a time that fall into random lists
921 test_many_rules_in_n_tables(int n_tables
)
923 enum { MAX_RULES
= 50 };
928 assert(n_tables
< 10);
929 for (i
= 0; i
< n_tables
; i
++) {
931 wcfs
[i
] = rand() & ((1u << CLS_N_FIELDS
) - 1);
932 } while (array_contains(wcfs
, i
, wcfs
[i
]));
935 for (iteration
= 0; iteration
< 30; iteration
++) {
936 unsigned int priorities
[MAX_RULES
];
937 struct classifier cls
;
941 for (i
= 0; i
< MAX_RULES
; i
++) {
942 priorities
[i
] = i
* 129;
944 shuffle(priorities
, ARRAY_SIZE(priorities
));
946 classifier_init(&cls
);
949 for (i
= 0; i
< MAX_RULES
; i
++) {
950 struct test_rule
*rule
;
951 unsigned int priority
= priorities
[i
];
952 int wcf
= wcfs
[rand() % n_tables
];
953 int value_pat
= rand() & ((1u << CLS_N_FIELDS
) - 1);
954 rule
= make_rule(wcf
, priority
, value_pat
);
955 tcls_insert(&tcls
, rule
);
956 classifier_insert(&cls
, &rule
->cls_rule
);
957 check_tables(&cls
, -1, i
+ 1, -1);
958 compare_classifiers(&cls
, &tcls
);
961 while (!classifier_is_empty(&cls
)) {
962 struct test_rule
*rule
, *next_rule
;
963 struct test_rule
*target
;
964 struct cls_cursor cursor
;
966 target
= clone_rule(tcls
.rules
[rand() % tcls
.n_rules
]);
968 cls_cursor_init(&cursor
, &cls
, &target
->cls_rule
);
969 CLS_CURSOR_FOR_EACH_SAFE (rule
, next_rule
, cls_rule
, &cursor
) {
970 classifier_remove(&cls
, &rule
->cls_rule
);
973 tcls_delete_matches(&tcls
, &target
->cls_rule
);
974 compare_classifiers(&cls
, &tcls
);
975 check_tables(&cls
, -1, -1, -1);
979 destroy_classifier(&cls
);
985 test_many_rules_in_two_tables(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
987 test_many_rules_in_n_tables(2);
991 test_many_rules_in_five_tables(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
993 test_many_rules_in_n_tables(5);
996 /* Miniflow tests. */
1001 static const uint32_t values
[] =
1002 { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1003 0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1005 return values
[random_uint32() % ARRAY_SIZE(values
)];
1009 choose(unsigned int n
, unsigned int *idxp
)
1020 init_consecutive_values(int n_consecutive
, struct flow
*flow
,
1023 uint32_t *flow_u32
= (uint32_t *) flow
;
1025 if (choose(FLOW_U32S
- n_consecutive
+ 1, idxp
)) {
1028 for (i
= 0; i
< n_consecutive
; i
++) {
1029 flow_u32
[*idxp
+ i
] = random_value();
1038 next_random_flow(struct flow
*flow
, unsigned int idx
)
1040 uint32_t *flow_u32
= (uint32_t *) flow
;
1043 memset(flow
, 0, sizeof *flow
);
1046 if (choose(1, &idx
)) {
1050 /* All flows with a small number of consecutive nonzero values. */
1051 for (i
= 1; i
<= 4; i
++) {
1052 if (init_consecutive_values(i
, flow
, &idx
)) {
1057 /* All flows with a large number of consecutive nonzero values. */
1058 for (i
= FLOW_U32S
- 4; i
<= FLOW_U32S
; i
++) {
1059 if (init_consecutive_values(i
, flow
, &idx
)) {
1064 /* All flows with exactly two nonconsecutive nonzero values. */
1065 if (choose((FLOW_U32S
- 1) * (FLOW_U32S
- 2) / 2, &idx
)) {
1068 for (ofs1
= 0; ofs1
< FLOW_U32S
- 2; ofs1
++) {
1071 for (ofs2
= ofs1
+ 2; ofs2
< FLOW_U32S
; ofs2
++) {
1072 if (choose(1, &idx
)) {
1073 flow_u32
[ofs1
] = random_value();
1074 flow_u32
[ofs2
] = random_value();
1082 /* 16 randomly chosen flows with N >= 3 nonzero values. */
1083 if (choose(16 * (FLOW_U32S
- 4), &idx
)) {
1084 int n
= idx
/ 16 + 3;
1087 for (i
= 0; i
< n
; i
++) {
1088 flow_u32
[i
] = random_value();
1090 shuffle_u32s(flow_u32
, FLOW_U32S
);
1099 any_random_flow(struct flow
*flow
)
1101 static unsigned int max
;
1103 while (next_random_flow(flow
, max
)) {
1108 next_random_flow(flow
, random_range(max
));
1112 toggle_masked_flow_bits(struct flow
*flow
, const struct flow_wildcards
*mask
)
1114 const uint32_t *mask_u32
= (const uint32_t *) &mask
->masks
;
1115 uint32_t *flow_u32
= (uint32_t *) flow
;
1118 for (i
= 0; i
< FLOW_U32S
; i
++) {
1119 if (mask_u32
[i
] != 0) {
1123 bit
= 1u << random_range(32);
1124 } while (!(bit
& mask_u32
[i
]));
1131 wildcard_extra_bits(struct flow_wildcards
*mask
)
1133 uint32_t *mask_u32
= (uint32_t *) &mask
->masks
;
1136 for (i
= 0; i
< FLOW_U32S
; i
++) {
1137 if (mask_u32
[i
] != 0) {
1141 bit
= 1u << random_range(32);
1142 } while (!(bit
& mask_u32
[i
]));
1143 mask_u32
[i
] &= ~bit
;
1149 test_miniflow(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1154 random_set_seed(0xb3faca38);
1155 for (idx
= 0; next_random_flow(&flow
, idx
); idx
++) {
1156 const uint32_t *flow_u32
= (const uint32_t *) &flow
;
1157 struct miniflow miniflow
, miniflow2
, miniflow3
;
1158 struct flow flow2
, flow3
;
1159 struct flow_wildcards mask
;
1160 struct minimask minimask
;
1163 /* Convert flow to miniflow. */
1164 miniflow_init(&miniflow
, &flow
);
1166 /* Check that the flow equals its miniflow. */
1167 assert(miniflow_get_vid(&miniflow
) == vlan_tci_to_vid(flow
.vlan_tci
));
1168 for (i
= 0; i
< FLOW_U32S
; i
++) {
1169 assert(miniflow_get(&miniflow
, i
) == flow_u32
[i
]);
1172 /* Check that the miniflow equals itself. */
1173 assert(miniflow_equal(&miniflow
, &miniflow
));
1175 /* Convert miniflow back to flow and verify that it's the same. */
1176 miniflow_expand(&miniflow
, &flow2
);
1177 assert(flow_equal(&flow
, &flow2
));
1179 /* Check that copying a miniflow works properly. */
1180 miniflow_clone(&miniflow2
, &miniflow
);
1181 assert(miniflow_equal(&miniflow
, &miniflow2
));
1182 assert(miniflow_hash(&miniflow
, 0) == miniflow_hash(&miniflow2
, 0));
1183 miniflow_expand(&miniflow2
, &flow3
);
1184 assert(flow_equal(&flow
, &flow3
));
1186 /* Check that masked matches work as expected for identical flows and
1189 next_random_flow(&mask
.masks
, 1);
1190 } while (flow_wildcards_is_catchall(&mask
));
1191 minimask_init(&minimask
, &mask
);
1192 assert(minimask_is_catchall(&minimask
)
1193 == flow_wildcards_is_catchall(&mask
));
1194 assert(miniflow_equal_in_minimask(&miniflow
, &miniflow2
, &minimask
));
1195 assert(miniflow_equal_flow_in_minimask(&miniflow
, &flow2
, &minimask
));
1196 assert(miniflow_hash_in_minimask(&miniflow
, &minimask
, 0x12345678) ==
1197 flow_hash_in_minimask(&flow
, &minimask
, 0x12345678));
1199 /* Check that masked matches work as expected for differing flows and
1201 toggle_masked_flow_bits(&flow2
, &mask
);
1202 assert(!miniflow_equal_flow_in_minimask(&miniflow
, &flow2
, &minimask
));
1203 miniflow_init(&miniflow3
, &flow2
);
1204 assert(!miniflow_equal_in_minimask(&miniflow
, &miniflow3
, &minimask
));
1207 miniflow_destroy(&miniflow
);
1208 miniflow_destroy(&miniflow2
);
1209 miniflow_destroy(&miniflow3
);
1210 minimask_destroy(&minimask
);
1215 test_minimask_has_extra(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1217 struct flow_wildcards catchall
;
1218 struct minimask minicatchall
;
1222 flow_wildcards_init_catchall(&catchall
);
1223 minimask_init(&minicatchall
, &catchall
);
1224 assert(minimask_is_catchall(&minicatchall
));
1226 random_set_seed(0x2ec7905b);
1227 for (idx
= 0; next_random_flow(&flow
, idx
); idx
++) {
1228 struct flow_wildcards mask
;
1229 struct minimask minimask
;
1232 minimask_init(&minimask
, &mask
);
1233 assert(!minimask_has_extra(&minimask
, &minimask
));
1234 assert(minimask_has_extra(&minicatchall
, &minimask
)
1235 == !minimask_is_catchall(&minimask
));
1236 if (!minimask_is_catchall(&minimask
)) {
1237 struct minimask minimask2
;
1239 wildcard_extra_bits(&mask
);
1240 minimask_init(&minimask2
, &mask
);
1241 assert(minimask_has_extra(&minimask2
, &minimask
));
1242 assert(!minimask_has_extra(&minimask
, &minimask2
));
1243 minimask_destroy(&minimask2
);
1246 minimask_destroy(&minimask
);
1249 minimask_destroy(&minicatchall
);
1253 test_minimask_combine(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1255 struct flow_wildcards catchall
;
1256 struct minimask minicatchall
;
1260 flow_wildcards_init_catchall(&catchall
);
1261 minimask_init(&minicatchall
, &catchall
);
1262 assert(minimask_is_catchall(&minicatchall
));
1264 random_set_seed(0x181bf0cd);
1265 for (idx
= 0; next_random_flow(&flow
, idx
); idx
++) {
1266 struct minimask minimask
, minimask2
, minicombined
;
1267 struct flow_wildcards mask
, mask2
, combined
, combined2
;
1268 uint32_t storage
[FLOW_U32S
];
1272 minimask_init(&minimask
, &mask
);
1274 minimask_combine(&minicombined
, &minimask
, &minicatchall
, storage
);
1275 assert(minimask_is_catchall(&minicombined
));
1277 any_random_flow(&flow2
);
1278 mask2
.masks
= flow2
;
1279 minimask_init(&minimask2
, &mask2
);
1281 minimask_combine(&minicombined
, &minimask
, &minimask2
, storage
);
1282 flow_wildcards_and(&combined
, &mask
, &mask2
);
1283 minimask_expand(&minicombined
, &combined2
);
1284 assert(flow_wildcards_equal(&combined
, &combined2
));
1286 minimask_destroy(&minimask
);
1287 minimask_destroy(&minimask2
);
1290 minimask_destroy(&minicatchall
);
1293 static const struct command commands
[] = {
1294 /* Classifier tests. */
1295 {"empty", 0, 0, test_empty
},
1296 {"destroy-null", 0, 0, test_destroy_null
},
1297 {"single-rule", 0, 0, test_single_rule
},
1298 {"rule-replacement", 0, 0, test_rule_replacement
},
1299 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list
},
1300 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table
},
1301 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables
},
1302 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables
},
1304 /* Miniflow and minimask tests. */
1305 {"miniflow", 0, 0, test_miniflow
},
1306 {"minimask_has_extra", 0, 0, test_minimask_has_extra
},
1307 {"minimask_combine", 0, 0, test_minimask_combine
},
1313 main(int argc
, char *argv
[])
1315 set_program_name(argv
[0]);
1317 run_command(argc
- 1, argv
+ 1, commands
);