]> git.proxmox.com Git - mirror_ovs.git/blob - tests/test-classifier.c
e4eb0f436d2bd8e30072f13da7f2d9db1cfe38e4
[mirror_ovs.git] / tests / test-classifier.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 /* "White box" tests for classifier.
18 *
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.)
23 *
24 * This test should receive a clean report from "valgrind --leak-check=full":
25 * it frees every heap block that it allocates.
26 */
27
28 #include <config.h>
29 #undef NDEBUG
30 #include "classifier.h"
31 #include <assert.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include "byte-order.h"
35 #include "classifier-private.h"
36 #include "command-line.h"
37 #include "flow.h"
38 #include "ofp-util.h"
39 #include "ovstest.h"
40 #include "packets.h"
41 #include "random.h"
42 #include "unaligned.h"
43 #include "util.h"
44
45 /* Fields in a rule. */
46 #define CLS_FIELDS \
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)
63
64 /* Field indexes.
65 *
66 * (These are also indexed into struct classifier's 'tables' array.) */
67 enum {
68 #define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME,
69 CLS_FIELDS
70 #undef CLS_FIELD
71 CLS_N_FIELDS
72 };
73
74 /* Field information. */
75 struct cls_field {
76 int ofs; /* Offset in struct flow. */
77 int len; /* Length in bytes. */
78 const char *name; /* Name (for debugging). */
79 };
80
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, \
85 #NAME },
86 CLS_FIELDS
87 #undef CLS_FIELD
88 };
89
90 struct test_rule {
91 int aux; /* Auxiliary data. */
92 struct cls_rule cls_rule; /* Classifier rule data. */
93 };
94
95 static struct test_rule *
96 test_rule_from_cls_rule(const struct cls_rule *rule)
97 {
98 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
99 }
100
101 static void
102 test_rule_destroy(struct test_rule *rule)
103 {
104 if (rule) {
105 cls_rule_destroy(&rule->cls_rule);
106 free(rule);
107 }
108 }
109
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 *);
113
114 /* Trivial (linear) classifier. */
115 struct tcls {
116 size_t n_rules;
117 size_t allocated_rules;
118 struct test_rule **rules;
119 };
120
121 static void
122 tcls_init(struct tcls *tcls)
123 {
124 tcls->n_rules = 0;
125 tcls->allocated_rules = 0;
126 tcls->rules = NULL;
127 }
128
129 static void
130 tcls_destroy(struct tcls *tcls)
131 {
132 if (tcls) {
133 size_t i;
134
135 for (i = 0; i < tcls->n_rules; i++) {
136 test_rule_destroy(tcls->rules[i]);
137 }
138 free(tcls->rules);
139 }
140 }
141
142 static bool
143 tcls_is_empty(const struct tcls *tcls)
144 {
145 return tcls->n_rules == 0;
146 }
147
148 static struct test_rule *
149 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
150 {
151 size_t i;
152
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)) {
156 /* Exact match. */
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) {
161 break;
162 }
163 }
164
165 if (tcls->n_rules >= tcls->allocated_rules) {
166 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
167 sizeof *tcls->rules);
168 }
169 if (i != tcls->n_rules) {
170 memmove(&tcls->rules[i + 1], &tcls->rules[i],
171 sizeof *tcls->rules * (tcls->n_rules - i));
172 }
173 tcls->rules[i] = clone_rule(rule);
174 tcls->n_rules++;
175 return tcls->rules[i];
176 }
177
178 static void
179 tcls_remove(struct tcls *cls, const struct test_rule *rule)
180 {
181 size_t i;
182
183 for (i = 0; i < cls->n_rules; i++) {
184 struct test_rule *pos = cls->rules[i];
185 if (pos == rule) {
186 test_rule_destroy(pos);
187
188 memmove(&cls->rules[i], &cls->rules[i + 1],
189 sizeof *cls->rules * (cls->n_rules - i - 1));
190
191 cls->n_rules--;
192 return;
193 }
194 }
195 OVS_NOT_REACHED();
196 }
197
198 static bool
199 match(const struct cls_rule *wild_, const struct flow *fixed)
200 {
201 struct match wild;
202 int f_idx;
203
204 minimatch_expand(&wild_->match, &wild);
205 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
206 bool eq;
207
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);
248 } else {
249 OVS_NOT_REACHED();
250 }
251
252 if (!eq) {
253 return false;
254 }
255 }
256 return true;
257 }
258
259 static struct cls_rule *
260 tcls_lookup(const struct tcls *cls, const struct flow *flow)
261 {
262 size_t i;
263
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;
268 }
269 }
270 return NULL;
271 }
272
273 static void
274 tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
275 {
276 size_t i;
277
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)) {
282 struct flow flow;
283
284 miniflow_expand(&pos->cls_rule.match.flow, &flow);
285 if (match(target, &flow)) {
286 tcls_remove(cls, pos);
287 continue;
288 }
289 }
290 i++;
291 }
292 }
293 \f
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[] = {
299 0,
300 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
301 static ovs_be64 metadata_values[] = {
302 0,
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 };
319
320 static void *values[CLS_N_FIELDS][2];
321
322 static void
323 init_values(void)
324 {
325 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
326 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
327
328 values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
329 values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
330
331 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
332 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
333
334 values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
335 values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
336
337 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
338 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
339
340 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
341 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
342
343 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
344 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
345
346 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
347 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
348
349 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
350 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
351
352 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
353 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
354
355 values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
356 values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
357
358 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
359 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
360
361 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
362 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
363 }
364
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)
378
379 #define N_FLOW_VALUES (N_NW_SRC_VALUES * \
380 N_NW_DST_VALUES * \
381 N_TUN_ID_VALUES * \
382 N_IN_PORT_VALUES * \
383 N_VLAN_TCI_VALUES * \
384 N_DL_TYPE_VALUES * \
385 N_TP_SRC_VALUES * \
386 N_TP_DST_VALUES * \
387 N_DL_SRC_VALUES * \
388 N_DL_DST_VALUES * \
389 N_NW_PROTO_VALUES * \
390 N_NW_DSCP_VALUES)
391
392 static unsigned int
393 get_value(unsigned int *x, unsigned n_values)
394 {
395 unsigned int rem = *x % n_values;
396 *x /= n_values;
397 return rem;
398 }
399
400 static void
401 compare_classifiers(struct classifier *cls, struct tcls *tcls)
402 {
403 static const int confidence = 500;
404 unsigned int i;
405
406 assert(classifier_count(cls) == tcls->n_rules);
407 for (i = 0; i < confidence; i++) {
408 const struct cls_rule *cr0, *cr1, *cr2;
409 struct flow flow;
410 struct flow_wildcards wc;
411 unsigned int x;
412
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,
421 N_IN_PORT_VALUES)];
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)],
427 ETH_ADDR_LEN);
428 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
429 ETH_ADDR_LEN);
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)];
432
433 /* This assertion is here to suppress a GCC 4.9 array-bounds warning */
434 ovs_assert(cls->n_tries <= CLS_MAX_TRIES);
435
436 cr0 = classifier_lookup(cls, &flow, &wc);
437 cr1 = tcls_lookup(tcls, &flow);
438 assert((cr0 == NULL) == (cr1 == NULL));
439 if (cr0 != 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);
442
443 assert(cls_rule_equal(cr0, cr1));
444 assert(tr0->aux == tr1->aux);
445 }
446 cr2 = classifier_lookup(cls, &flow, NULL);
447 assert(cr2 == cr0);
448 }
449 }
450
451 static void
452 destroy_classifier(struct classifier *cls)
453 {
454 struct test_rule *rule;
455
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);
460 }
461 }
462 classifier_destroy(cls);
463 }
464
465 static void
466 pvector_verify(const struct pvector *pvec)
467 {
468 void *ptr OVS_UNUSED;
469 int prev_priority = INT_MAX;
470
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);
476 }
477 prev_priority = priority;
478 }
479 }
480
481 static unsigned int
482 trie_verify(const rcu_trie_ptr *trie, unsigned int ofs, unsigned int n_bits)
483 {
484 const struct trie_node *node = ovsrcu_get(struct trie_node *, trie);
485
486 if (node) {
487 assert(node->n_rules == 0 || node->n_bits > 0);
488 ofs += node->n_bits;
489 assert((ofs > 0 || (ofs == 0 && node->n_bits == 0)) && ofs <= n_bits);
490
491 return node->n_rules
492 + trie_verify(&node->edges[0], ofs, n_bits)
493 + trie_verify(&node->edges[1], ofs, n_bits);
494 }
495 return 0;
496 }
497
498 static void
499 verify_tries(struct classifier *cls)
500 OVS_NO_THREAD_SAFETY_ANALYSIS
501 {
502 unsigned int n_rules = 0;
503 int i;
504
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);
508 }
509 assert(n_rules <= cls->n_rules);
510 }
511
512 static void
513 check_tables(const struct classifier *cls, int n_tables, int n_rules,
514 int n_dups)
515 OVS_NO_THREAD_SAFETY_ANALYSIS
516 {
517 const struct cls_subtable *table;
518 struct test_rule *test_rule;
519 int found_tables = 0;
520 int found_rules = 0;
521 int found_dups = 0;
522 int found_rules2 = 0;
523
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;
529 bool found = false;
530 const struct cls_subtable *iter;
531
532 /* Locate the subtable from 'subtables'. */
533 PVECTOR_FOR_EACH (iter, &cls->subtables) {
534 if (iter == table) {
535 if (found) {
536 ovs_abort(0, "Subtable %p duplicated in 'subtables'.",
537 table);
538 }
539 found = true;
540 }
541 }
542 if (!found) {
543 ovs_abort(0, "Subtable %p not found from 'subtables'.", table);
544 }
545
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));
549
550 found_tables++;
551 CMAP_FOR_EACH (head, cmap_node, &table->rules) {
552 int prev_priority = INT_MAX;
553 const struct cls_match *rule;
554
555 if (head->priority > max_priority) {
556 max_priority = head->priority;
557 max_count = 1;
558 } else if (head->priority == max_priority) {
559 ++max_count;
560 }
561
562 found_rules++;
563 RCULIST_FOR_EACH (rule, list, &head->list) {
564 assert(rule->priority < prev_priority);
565 assert(rule->priority <= table->max_priority);
566
567 prev_priority = rule->priority;
568 found_rules++;
569 found_dups++;
570 assert(classifier_find_rule_exactly(cls, rule->cls_rule)
571 == rule->cls_rule);
572 }
573 }
574 assert(table->max_priority == max_priority);
575 assert(table->max_count == max_count);
576 }
577
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);
583
584 CLS_FOR_EACH (test_rule, cls_rule, cls) {
585 found_rules2++;
586 }
587 assert(found_rules == found_rules2);
588 }
589
590 static struct test_rule *
591 make_rule(int wc_fields, int priority, int value_pat)
592 {
593 const struct cls_field *f;
594 struct test_rule *rule;
595 struct match match;
596
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);
603
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);
630 } else {
631 OVS_NOT_REACHED();
632 }
633 }
634
635 rule = xzalloc(sizeof *rule);
636 cls_rule_init(&rule->cls_rule, &match, wc_fields
637 ? (priority == INT_MIN ? priority + 1 : priority)
638 : INT_MAX);
639 return rule;
640 }
641
642 static struct test_rule *
643 clone_rule(const struct test_rule *src)
644 {
645 struct test_rule *dst;
646
647 dst = xmalloc(sizeof *dst);
648 dst->aux = src->aux;
649 cls_rule_clone(&dst->cls_rule, &src->cls_rule);
650 return dst;
651 }
652
653 static void
654 free_rule(struct test_rule *rule)
655 {
656 cls_rule_destroy(&rule->cls_rule);
657 free(rule);
658 }
659
660 static void
661 shuffle(int *p, size_t n)
662 {
663 for (; n > 1; n--, p++) {
664 int *q = &p[random_range(n)];
665 int tmp = *p;
666 *p = *q;
667 *q = tmp;
668 }
669 }
670
671 static void
672 shuffle_u32s(uint32_t *p, size_t n)
673 {
674 for (; n > 1; n--, p++) {
675 uint32_t *q = &p[random_range(n)];
676 uint32_t tmp = *p;
677 *p = *q;
678 *q = tmp;
679 }
680 }
681 \f
682 /* Classifier tests. */
683
684 static enum mf_field_id trie_fields[2] = {
685 MFF_IPV4_DST, MFF_IPV4_SRC
686 };
687
688 static void
689 set_prefix_fields(struct classifier *cls)
690 {
691 verify_tries(cls);
692 classifier_set_prefix_fields(cls, trie_fields, ARRAY_SIZE(trie_fields));
693 verify_tries(cls);
694 }
695
696 /* Tests an empty classifier. */
697 static void
698 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
699 {
700 struct classifier cls;
701 struct tcls tcls;
702
703 classifier_init(&cls, flow_segment_u32s);
704 set_prefix_fields(&cls);
705 tcls_init(&tcls);
706 assert(classifier_is_empty(&cls));
707 assert(tcls_is_empty(&tcls));
708 compare_classifiers(&cls, &tcls);
709 classifier_destroy(&cls);
710 tcls_destroy(&tcls);
711 }
712
713 /* Destroys a null classifier. */
714 static void
715 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
716 {
717 classifier_destroy(NULL);
718 }
719
720 /* Tests classification with one rule at a time. */
721 static void
722 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
723 {
724 unsigned int wc_fields; /* Hilarious. */
725
726 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
727 struct classifier cls;
728 struct test_rule *rule, *tcls_rule;
729 struct tcls tcls;
730
731 rule = make_rule(wc_fields,
732 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
733
734 classifier_init(&cls, flow_segment_u32s);
735 set_prefix_fields(&cls);
736 tcls_init(&tcls);
737
738 tcls_rule = tcls_insert(&tcls, rule);
739 classifier_insert(&cls, &rule->cls_rule);
740 compare_classifiers(&cls, &tcls);
741 check_tables(&cls, 1, 1, 0);
742
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);
748
749 ovsrcu_postpone(free_rule, rule);
750 classifier_destroy(&cls);
751 tcls_destroy(&tcls);
752 }
753 }
754
755 /* Tests replacing one rule by another. */
756 static void
757 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
758 {
759 unsigned int wc_fields;
760
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;
765 struct tcls tcls;
766
767 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
768 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
769 rule2->aux += 5;
770 rule2->aux += 5;
771
772 classifier_init(&cls, flow_segment_u32s);
773 set_prefix_fields(&cls);
774 tcls_init(&tcls);
775 tcls_insert(&tcls, rule1);
776 classifier_insert(&cls, &rule1->cls_rule);
777 compare_classifiers(&cls, &tcls);
778 check_tables(&cls, 1, 1, 0);
779 tcls_destroy(&tcls);
780
781 tcls_init(&tcls);
782 tcls_insert(&tcls, rule2);
783
784 assert(test_rule_from_cls_rule(
785 classifier_replace(&cls, &rule2->cls_rule)) == rule1);
786 ovsrcu_postpone(free_rule, rule1);
787 compare_classifiers(&cls, &tcls);
788 check_tables(&cls, 1, 1, 0);
789 classifier_defer(&cls);
790 classifier_remove(&cls, &rule2->cls_rule);
791
792 tcls_destroy(&tcls);
793 destroy_classifier(&cls);
794 }
795 }
796
797 static int
798 factorial(int n_items)
799 {
800 int n, i;
801
802 n = 1;
803 for (i = 2; i <= n_items; i++) {
804 n *= i;
805 }
806 return n;
807 }
808
809 static void
810 swap(int *a, int *b)
811 {
812 int tmp = *a;
813 *a = *b;
814 *b = tmp;
815 }
816
817 static void
818 reverse(int *a, int n)
819 {
820 int i;
821
822 for (i = 0; i < n / 2; i++) {
823 int j = n - (i + 1);
824 swap(&a[i], &a[j]);
825 }
826 }
827
828 static bool
829 next_permutation(int *a, int n)
830 {
831 int k;
832
833 for (k = n - 2; k >= 0; k--) {
834 if (a[k] < a[k + 1]) {
835 int l;
836
837 for (l = n - 1; ; l--) {
838 if (a[l] > a[k]) {
839 swap(&a[k], &a[l]);
840 reverse(a + (k + 1), n - (k + 1));
841 return true;
842 }
843 }
844 }
845 }
846 return false;
847 }
848
849 /* Tests classification with rules that have the same matching criteria. */
850 static void
851 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
852 {
853 enum { N_RULES = 3 };
854 int n_pris;
855
856 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
857 int ops[N_RULES * 2];
858 int pris[N_RULES];
859 int n_permutations;
860 int i;
861
862 pris[0] = 0;
863 for (i = 1; i < N_RULES; i++) {
864 pris[i] = pris[i - 1] + (n_pris > i);
865 }
866
867 for (i = 0; i < N_RULES * 2; i++) {
868 ops[i] = i / 2;
869 }
870
871 n_permutations = 0;
872 do {
873 struct test_rule *rules[N_RULES];
874 struct test_rule *tcls_rules[N_RULES];
875 int pri_rules[N_RULES];
876 struct classifier cls;
877 struct tcls tcls;
878
879 n_permutations++;
880
881 for (i = 0; i < N_RULES; i++) {
882 rules[i] = make_rule(456, pris[i], 0);
883 tcls_rules[i] = NULL;
884 pri_rules[i] = -1;
885 }
886
887 classifier_init(&cls, flow_segment_u32s);
888 set_prefix_fields(&cls);
889 tcls_init(&tcls);
890
891 for (i = 0; i < ARRAY_SIZE(ops); i++) {
892 int j = ops[i];
893 int m, n;
894
895 if (!tcls_rules[j]) {
896 struct test_rule *displaced_rule;
897
898 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
899 displaced_rule = test_rule_from_cls_rule(
900 classifier_replace(&cls, &rules[j]->cls_rule));
901 if (pri_rules[pris[j]] >= 0) {
902 int k = pri_rules[pris[j]];
903 assert(displaced_rule != NULL);
904 assert(displaced_rule != rules[j]);
905 assert(pris[j] == displaced_rule->cls_rule.priority);
906 tcls_rules[k] = NULL;
907 } else {
908 assert(displaced_rule == NULL);
909 }
910 pri_rules[pris[j]] = j;
911 } else {
912 classifier_remove(&cls, &rules[j]->cls_rule);
913 tcls_remove(&tcls, tcls_rules[j]);
914 tcls_rules[j] = NULL;
915 pri_rules[pris[j]] = -1;
916 }
917 compare_classifiers(&cls, &tcls);
918
919 n = 0;
920 for (m = 0; m < N_RULES; m++) {
921 n += tcls_rules[m] != NULL;
922 }
923 check_tables(&cls, n > 0, n, n - 1);
924 }
925
926 classifier_defer(&cls);
927 for (i = 0; i < N_RULES; i++) {
928 if (classifier_remove(&cls, &rules[i]->cls_rule)) {
929 ovsrcu_postpone(free_rule, rules[i]);
930 }
931 }
932 classifier_destroy(&cls);
933 tcls_destroy(&tcls);
934 } while (next_permutation(ops, ARRAY_SIZE(ops)));
935 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
936 }
937 }
938
939 static int
940 count_ones(unsigned long int x)
941 {
942 int n = 0;
943
944 while (x) {
945 x = zero_rightmost_1bit(x);
946 n++;
947 }
948
949 return n;
950 }
951
952 static bool
953 array_contains(int *array, int n, int value)
954 {
955 int i;
956
957 for (i = 0; i < n; i++) {
958 if (array[i] == value) {
959 return true;
960 }
961 }
962
963 return false;
964 }
965
966 /* Tests classification with two rules at a time that fall into the same
967 * table but different lists. */
968 static void
969 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
970 {
971 int iteration;
972
973 for (iteration = 0; iteration < 50; iteration++) {
974 enum { N_RULES = 20 };
975 struct test_rule *rules[N_RULES];
976 struct test_rule *tcls_rules[N_RULES];
977 struct classifier cls;
978 struct tcls tcls;
979 int value_pats[N_RULES];
980 int value_mask;
981 int wcf;
982 int i;
983
984 do {
985 wcf = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
986 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
987 } while ((1 << count_ones(value_mask)) < N_RULES);
988
989 classifier_init(&cls, flow_segment_u32s);
990 set_prefix_fields(&cls);
991 tcls_init(&tcls);
992
993 for (i = 0; i < N_RULES; i++) {
994 int priority = random_range(INT_MAX);
995
996 do {
997 value_pats[i] = random_uint32() & value_mask;
998 } while (array_contains(value_pats, i, value_pats[i]));
999
1000 rules[i] = make_rule(wcf, priority, value_pats[i]);
1001 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
1002
1003 classifier_insert(&cls, &rules[i]->cls_rule);
1004 compare_classifiers(&cls, &tcls);
1005
1006 check_tables(&cls, 1, i + 1, 0);
1007 }
1008
1009 for (i = 0; i < N_RULES; i++) {
1010 tcls_remove(&tcls, tcls_rules[i]);
1011 classifier_remove(&cls, &rules[i]->cls_rule);
1012 compare_classifiers(&cls, &tcls);
1013 ovsrcu_postpone(free_rule, rules[i]);
1014
1015 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
1016 }
1017
1018 classifier_destroy(&cls);
1019 tcls_destroy(&tcls);
1020 }
1021 }
1022
1023 /* Tests classification with many rules at a time that fall into random lists
1024 * in 'n' tables. */
1025 static void
1026 test_many_rules_in_n_tables(int n_tables)
1027 {
1028 enum { MAX_RULES = 50 };
1029 int wcfs[10];
1030 int iteration;
1031 int i;
1032
1033 assert(n_tables < 10);
1034 for (i = 0; i < n_tables; i++) {
1035 do {
1036 wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1037 } while (array_contains(wcfs, i, wcfs[i]));
1038 }
1039
1040 for (iteration = 0; iteration < 30; iteration++) {
1041 int priorities[MAX_RULES];
1042 struct classifier cls;
1043 struct tcls tcls;
1044
1045 random_set_seed(iteration + 1);
1046 for (i = 0; i < MAX_RULES; i++) {
1047 priorities[i] = (i * 129) & INT_MAX;
1048 }
1049 shuffle(priorities, ARRAY_SIZE(priorities));
1050
1051 classifier_init(&cls, flow_segment_u32s);
1052 set_prefix_fields(&cls);
1053 tcls_init(&tcls);
1054
1055 for (i = 0; i < MAX_RULES; i++) {
1056 struct test_rule *rule;
1057 int priority = priorities[i];
1058 int wcf = wcfs[random_range(n_tables)];
1059 int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1060 rule = make_rule(wcf, priority, value_pat);
1061 tcls_insert(&tcls, rule);
1062 classifier_insert(&cls, &rule->cls_rule);
1063 compare_classifiers(&cls, &tcls);
1064 check_tables(&cls, -1, i + 1, -1);
1065 }
1066
1067 while (!classifier_is_empty(&cls)) {
1068 struct test_rule *target;
1069 struct test_rule *rule;
1070
1071 target = clone_rule(tcls.rules[random_range(tcls.n_rules)]);
1072
1073 CLS_FOR_EACH_TARGET (rule, cls_rule, &cls, &target->cls_rule) {
1074 if (classifier_remove(&cls, &rule->cls_rule)) {
1075 ovsrcu_postpone(free_rule, rule);
1076 }
1077 }
1078
1079 tcls_delete_matches(&tcls, &target->cls_rule);
1080 compare_classifiers(&cls, &tcls);
1081 check_tables(&cls, -1, -1, -1);
1082 free_rule(target);
1083 }
1084
1085 destroy_classifier(&cls);
1086 tcls_destroy(&tcls);
1087 }
1088 }
1089
1090 static void
1091 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1092 {
1093 test_many_rules_in_n_tables(2);
1094 }
1095
1096 static void
1097 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1098 {
1099 test_many_rules_in_n_tables(5);
1100 }
1101 \f
1102 /* Miniflow tests. */
1103
1104 static uint32_t
1105 random_value(void)
1106 {
1107 static const uint32_t values[] =
1108 { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1109 0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1110
1111 return values[random_range(ARRAY_SIZE(values))];
1112 }
1113
1114 static bool
1115 choose(unsigned int n, unsigned int *idxp)
1116 {
1117 if (*idxp < n) {
1118 return true;
1119 } else {
1120 *idxp -= n;
1121 return false;
1122 }
1123 }
1124
1125 static bool
1126 init_consecutive_values(int n_consecutive, struct flow *flow,
1127 unsigned int *idxp)
1128 {
1129 uint32_t *flow_u32 = (uint32_t *) flow;
1130
1131 if (choose(FLOW_U32S - n_consecutive + 1, idxp)) {
1132 int i;
1133
1134 for (i = 0; i < n_consecutive; i++) {
1135 flow_u32[*idxp + i] = random_value();
1136 }
1137 return true;
1138 } else {
1139 return false;
1140 }
1141 }
1142
1143 static bool
1144 next_random_flow(struct flow *flow, unsigned int idx)
1145 {
1146 uint32_t *flow_u32 = (uint32_t *) flow;
1147 int i;
1148
1149 memset(flow, 0, sizeof *flow);
1150
1151 /* Empty flow. */
1152 if (choose(1, &idx)) {
1153 return true;
1154 }
1155
1156 /* All flows with a small number of consecutive nonzero values. */
1157 for (i = 1; i <= 4; i++) {
1158 if (init_consecutive_values(i, flow, &idx)) {
1159 return true;
1160 }
1161 }
1162
1163 /* All flows with a large number of consecutive nonzero values. */
1164 for (i = FLOW_U32S - 4; i <= FLOW_U32S; i++) {
1165 if (init_consecutive_values(i, flow, &idx)) {
1166 return true;
1167 }
1168 }
1169
1170 /* All flows with exactly two nonconsecutive nonzero values. */
1171 if (choose((FLOW_U32S - 1) * (FLOW_U32S - 2) / 2, &idx)) {
1172 int ofs1;
1173
1174 for (ofs1 = 0; ofs1 < FLOW_U32S - 2; ofs1++) {
1175 int ofs2;
1176
1177 for (ofs2 = ofs1 + 2; ofs2 < FLOW_U32S; ofs2++) {
1178 if (choose(1, &idx)) {
1179 flow_u32[ofs1] = random_value();
1180 flow_u32[ofs2] = random_value();
1181 return true;
1182 }
1183 }
1184 }
1185 OVS_NOT_REACHED();
1186 }
1187
1188 /* 16 randomly chosen flows with N >= 3 nonzero values. */
1189 if (choose(16 * (FLOW_U32S - 4), &idx)) {
1190 int n = idx / 16 + 3;
1191 int i;
1192
1193 for (i = 0; i < n; i++) {
1194 flow_u32[i] = random_value();
1195 }
1196 shuffle_u32s(flow_u32, FLOW_U32S);
1197
1198 return true;
1199 }
1200
1201 return false;
1202 }
1203
1204 static void
1205 any_random_flow(struct flow *flow)
1206 {
1207 static unsigned int max;
1208 if (!max) {
1209 while (next_random_flow(flow, max)) {
1210 max++;
1211 }
1212 }
1213
1214 next_random_flow(flow, random_range(max));
1215 }
1216
1217 static void
1218 toggle_masked_flow_bits(struct flow *flow, const struct flow_wildcards *mask)
1219 {
1220 const uint32_t *mask_u32 = (const uint32_t *) &mask->masks;
1221 uint32_t *flow_u32 = (uint32_t *) flow;
1222 int i;
1223
1224 for (i = 0; i < FLOW_U32S; i++) {
1225 if (mask_u32[i] != 0) {
1226 uint32_t bit;
1227
1228 do {
1229 bit = 1u << random_range(32);
1230 } while (!(bit & mask_u32[i]));
1231 flow_u32[i] ^= bit;
1232 }
1233 }
1234 }
1235
1236 static void
1237 wildcard_extra_bits(struct flow_wildcards *mask)
1238 {
1239 uint32_t *mask_u32 = (uint32_t *) &mask->masks;
1240 int i;
1241
1242 for (i = 0; i < FLOW_U32S; i++) {
1243 if (mask_u32[i] != 0) {
1244 uint32_t bit;
1245
1246 do {
1247 bit = 1u << random_range(32);
1248 } while (!(bit & mask_u32[i]));
1249 mask_u32[i] &= ~bit;
1250 }
1251 }
1252 }
1253
1254 static void
1255 test_miniflow(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1256 {
1257 struct flow flow;
1258 unsigned int idx;
1259
1260 random_set_seed(0xb3faca38);
1261 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1262 const uint32_t *flow_u32 = (const uint32_t *) &flow;
1263 struct miniflow miniflow, miniflow2, miniflow3;
1264 struct flow flow2, flow3;
1265 struct flow_wildcards mask;
1266 struct minimask minimask;
1267 int i;
1268
1269 /* Convert flow to miniflow. */
1270 miniflow_init(&miniflow, &flow);
1271
1272 /* Check that the flow equals its miniflow. */
1273 assert(miniflow_get_vid(&miniflow) == vlan_tci_to_vid(flow.vlan_tci));
1274 for (i = 0; i < FLOW_U32S; i++) {
1275 assert(MINIFLOW_GET_TYPE(&miniflow, uint32_t, i * 4)
1276 == flow_u32[i]);
1277 }
1278
1279 /* Check that the miniflow equals itself. */
1280 assert(miniflow_equal(&miniflow, &miniflow));
1281
1282 /* Convert miniflow back to flow and verify that it's the same. */
1283 miniflow_expand(&miniflow, &flow2);
1284 assert(flow_equal(&flow, &flow2));
1285
1286 /* Check that copying a miniflow works properly. */
1287 miniflow_clone(&miniflow2, &miniflow);
1288 assert(miniflow_equal(&miniflow, &miniflow2));
1289 assert(miniflow_hash(&miniflow, 0) == miniflow_hash(&miniflow2, 0));
1290 miniflow_expand(&miniflow2, &flow3);
1291 assert(flow_equal(&flow, &flow3));
1292
1293 /* Check that masked matches work as expected for identical flows and
1294 * miniflows. */
1295 do {
1296 next_random_flow(&mask.masks, 1);
1297 } while (flow_wildcards_is_catchall(&mask));
1298 minimask_init(&minimask, &mask);
1299 assert(minimask_is_catchall(&minimask)
1300 == flow_wildcards_is_catchall(&mask));
1301 assert(miniflow_equal_in_minimask(&miniflow, &miniflow2, &minimask));
1302 assert(miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1303 assert(miniflow_hash_in_minimask(&miniflow, &minimask, 0x12345678) ==
1304 flow_hash_in_minimask(&flow, &minimask, 0x12345678));
1305
1306 /* Check that masked matches work as expected for differing flows and
1307 * miniflows. */
1308 toggle_masked_flow_bits(&flow2, &mask);
1309 assert(!miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1310 miniflow_init(&miniflow3, &flow2);
1311 assert(!miniflow_equal_in_minimask(&miniflow, &miniflow3, &minimask));
1312
1313 /* Clean up. */
1314 miniflow_destroy(&miniflow);
1315 miniflow_destroy(&miniflow2);
1316 miniflow_destroy(&miniflow3);
1317 minimask_destroy(&minimask);
1318 }
1319 }
1320
1321 static void
1322 test_minimask_has_extra(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1323 {
1324 struct flow_wildcards catchall;
1325 struct minimask minicatchall;
1326 struct flow flow;
1327 unsigned int idx;
1328
1329 flow_wildcards_init_catchall(&catchall);
1330 minimask_init(&minicatchall, &catchall);
1331 assert(minimask_is_catchall(&minicatchall));
1332
1333 random_set_seed(0x2ec7905b);
1334 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1335 struct flow_wildcards mask;
1336 struct minimask minimask;
1337
1338 mask.masks = flow;
1339 minimask_init(&minimask, &mask);
1340 assert(!minimask_has_extra(&minimask, &minimask));
1341 assert(minimask_has_extra(&minicatchall, &minimask)
1342 == !minimask_is_catchall(&minimask));
1343 if (!minimask_is_catchall(&minimask)) {
1344 struct minimask minimask2;
1345
1346 wildcard_extra_bits(&mask);
1347 minimask_init(&minimask2, &mask);
1348 assert(minimask_has_extra(&minimask2, &minimask));
1349 assert(!minimask_has_extra(&minimask, &minimask2));
1350 minimask_destroy(&minimask2);
1351 }
1352
1353 minimask_destroy(&minimask);
1354 }
1355
1356 minimask_destroy(&minicatchall);
1357 }
1358
1359 static void
1360 test_minimask_combine(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1361 {
1362 struct flow_wildcards catchall;
1363 struct minimask minicatchall;
1364 struct flow flow;
1365 unsigned int idx;
1366
1367 flow_wildcards_init_catchall(&catchall);
1368 minimask_init(&minicatchall, &catchall);
1369 assert(minimask_is_catchall(&minicatchall));
1370
1371 random_set_seed(0x181bf0cd);
1372 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1373 struct minimask minimask, minimask2, minicombined;
1374 struct flow_wildcards mask, mask2, combined, combined2;
1375 uint32_t storage[FLOW_U32S];
1376 struct flow flow2;
1377
1378 mask.masks = flow;
1379 minimask_init(&minimask, &mask);
1380
1381 minimask_combine(&minicombined, &minimask, &minicatchall, storage);
1382 assert(minimask_is_catchall(&minicombined));
1383
1384 any_random_flow(&flow2);
1385 mask2.masks = flow2;
1386 minimask_init(&minimask2, &mask2);
1387
1388 minimask_combine(&minicombined, &minimask, &minimask2, storage);
1389 flow_wildcards_and(&combined, &mask, &mask2);
1390 minimask_expand(&minicombined, &combined2);
1391 assert(flow_wildcards_equal(&combined, &combined2));
1392
1393 minimask_destroy(&minimask);
1394 minimask_destroy(&minimask2);
1395 }
1396
1397 minimask_destroy(&minicatchall);
1398 }
1399 \f
1400 static const struct command commands[] = {
1401 /* Classifier tests. */
1402 {"empty", NULL, 0, 0, test_empty},
1403 {"destroy-null", NULL, 0, 0, test_destroy_null},
1404 {"single-rule", NULL, 0, 0, test_single_rule},
1405 {"rule-replacement", NULL, 0, 0, test_rule_replacement},
1406 {"many-rules-in-one-list", NULL, 0, 0, test_many_rules_in_one_list},
1407 {"many-rules-in-one-table", NULL, 0, 0, test_many_rules_in_one_table},
1408 {"many-rules-in-two-tables", NULL, 0, 0, test_many_rules_in_two_tables},
1409 {"many-rules-in-five-tables", NULL, 0, 0, test_many_rules_in_five_tables},
1410
1411 /* Miniflow and minimask tests. */
1412 {"miniflow", NULL, 0, 0, test_miniflow},
1413 {"minimask_has_extra", NULL, 0, 0, test_minimask_has_extra},
1414 {"minimask_combine", NULL, 0, 0, test_minimask_combine},
1415
1416 {NULL, NULL, 0, 0, NULL},
1417 };
1418
1419 static void
1420 test_classifier_main(int argc, char *argv[])
1421 {
1422 set_program_name(argv[0]);
1423 init_values();
1424 run_command(argc - 1, argv + 1, commands);
1425 }
1426
1427 OVSTEST_REGISTER("test-classifier", test_classifier_main);