]> git.proxmox.com Git - mirror_ovs.git/blame - tests/test-classifier.c
lib/ofpbuf: Compact
[mirror_ovs.git] / tests / test-classifier.c
CommitLineData
064af421 1/*
b028db44 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
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>
064af421
BP
29#include "classifier.h"
30#include <errno.h>
31#include <limits.h>
10a24935 32#include "byte-order.h"
3223e977 33#include "command-line.h"
064af421 34#include "flow.h"
0596e897 35#include "ofp-util.h"
064af421 36#include "packets.h"
b028db44 37#include "random.h"
c0a56d9f 38#include "unaligned.h"
064af421
BP
39
40#undef NDEBUG
41#include <assert.h>
42
b5d97350 43/* Fields in a rule. */
d8ae4d67 44#define CLS_FIELDS \
296e07ac
JG
45 /* struct flow all-caps */ \
46 /* member name name */ \
47 /* ----------- -------- */ \
48 CLS_FIELD(tunnel.tun_id, TUN_ID) \
49 CLS_FIELD(metadata, METADATA) \
50 CLS_FIELD(nw_src, NW_SRC) \
51 CLS_FIELD(nw_dst, NW_DST) \
52 CLS_FIELD(in_port, IN_PORT) \
53 CLS_FIELD(vlan_tci, VLAN_TCI) \
54 CLS_FIELD(dl_type, DL_TYPE) \
55 CLS_FIELD(tp_src, TP_SRC) \
56 CLS_FIELD(tp_dst, TP_DST) \
57 CLS_FIELD(dl_src, DL_SRC) \
58 CLS_FIELD(dl_dst, DL_DST) \
59 CLS_FIELD(nw_proto, NW_PROTO) \
60 CLS_FIELD(nw_tos, NW_DSCP)
b5d97350
BP
61
62/* Field indexes.
63 *
64 * (These are also indexed into struct classifier's 'tables' array.) */
65enum {
0bdc4bec 66#define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME,
b5d97350
BP
67 CLS_FIELDS
68#undef CLS_FIELD
69 CLS_N_FIELDS
70};
71
72/* Field information. */
73struct cls_field {
74 int ofs; /* Offset in struct flow. */
75 int len; /* Length in bytes. */
b5d97350
BP
76 const char *name; /* Name (for debugging). */
77};
78
79static const struct cls_field cls_fields[CLS_N_FIELDS] = {
0bdc4bec 80#define CLS_FIELD(MEMBER, NAME) \
b5d97350
BP
81 { offsetof(struct flow, MEMBER), \
82 sizeof ((struct flow *)0)->MEMBER, \
b5d97350
BP
83 #NAME },
84 CLS_FIELDS
85#undef CLS_FIELD
86};
87
064af421
BP
88struct test_rule {
89 int aux; /* Auxiliary data. */
90 struct cls_rule cls_rule; /* Classifier rule data. */
91};
92
93static struct test_rule *
94test_rule_from_cls_rule(const struct cls_rule *rule)
95{
96 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
97}
98
f2f3f5cb
BP
99static void
100test_rule_destroy(struct test_rule *rule)
101{
102 if (rule) {
103 cls_rule_destroy(&rule->cls_rule);
104 free(rule);
105 }
106}
107
48d28ac1
BP
108static struct test_rule *make_rule(int wc_fields, unsigned int priority,
109 int value_pat);
110static void free_rule(struct test_rule *);
111static struct test_rule *clone_rule(const struct test_rule *);
112
064af421
BP
113/* Trivial (linear) classifier. */
114struct tcls {
115 size_t n_rules;
116 size_t allocated_rules;
117 struct test_rule **rules;
118};
119
120static void
121tcls_init(struct tcls *tcls)
122{
123 tcls->n_rules = 0;
124 tcls->allocated_rules = 0;
125 tcls->rules = NULL;
126}
127
128static void
129tcls_destroy(struct tcls *tcls)
130{
131 if (tcls) {
132 size_t i;
133
134 for (i = 0; i < tcls->n_rules; i++) {
f2f3f5cb 135 test_rule_destroy(tcls->rules[i]);
064af421
BP
136 }
137 free(tcls->rules);
138 }
139}
140
064af421
BP
141static bool
142tcls_is_empty(const struct tcls *tcls)
143{
144 return tcls->n_rules == 0;
145}
146
147static struct test_rule *
148tcls_insert(struct tcls *tcls, const struct test_rule *rule)
149{
150 size_t i;
151
064af421
BP
152 for (i = 0; i < tcls->n_rules; i++) {
153 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
193eb874
BP
154 if (cls_rule_equal(pos, &rule->cls_rule)) {
155 /* Exact match. */
48d28ac1
BP
156 free_rule(tcls->rules[i]);
157 tcls->rules[i] = clone_rule(rule);
064af421 158 return tcls->rules[i];
af7b73f4 159 } else if (pos->priority < rule->cls_rule.priority) {
064af421
BP
160 break;
161 }
162 }
163
164 if (tcls->n_rules >= tcls->allocated_rules) {
165 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
166 sizeof *tcls->rules);
167 }
168 if (i != tcls->n_rules) {
169 memmove(&tcls->rules[i + 1], &tcls->rules[i],
170 sizeof *tcls->rules * (tcls->n_rules - i));
171 }
48d28ac1 172 tcls->rules[i] = clone_rule(rule);
064af421
BP
173 tcls->n_rules++;
174 return tcls->rules[i];
175}
176
177static void
178tcls_remove(struct tcls *cls, const struct test_rule *rule)
179{
180 size_t i;
181
182 for (i = 0; i < cls->n_rules; i++) {
183 struct test_rule *pos = cls->rules[i];
184 if (pos == rule) {
f2f3f5cb
BP
185 test_rule_destroy(pos);
186
064af421
BP
187 memmove(&cls->rules[i], &cls->rules[i + 1],
188 sizeof *cls->rules * (cls->n_rules - i - 1));
f2f3f5cb 189
064af421
BP
190 cls->n_rules--;
191 return;
192 }
193 }
428b2edd 194 OVS_NOT_REACHED();
064af421
BP
195}
196
064af421 197static bool
5cb7a798 198match(const struct cls_rule *wild_, const struct flow *fixed)
064af421 199{
5cb7a798 200 struct match wild;
064af421
BP
201 int f_idx;
202
5cb7a798 203 minimatch_expand(&wild_->match, &wild);
064af421 204 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
d8ae4d67
BP
205 bool eq;
206
0bdc4bec 207 if (f_idx == CLS_F_IDX_NW_SRC) {
5cb7a798
BP
208 eq = !((fixed->nw_src ^ wild.flow.nw_src)
209 & wild.wc.masks.nw_src);
d8ae4d67 210 } else if (f_idx == CLS_F_IDX_NW_DST) {
5cb7a798
BP
211 eq = !((fixed->nw_dst ^ wild.flow.nw_dst)
212 & wild.wc.masks.nw_dst);
73f33563 213 } else if (f_idx == CLS_F_IDX_TP_SRC) {
5cb7a798
BP
214 eq = !((fixed->tp_src ^ wild.flow.tp_src)
215 & wild.wc.masks.tp_src);
73f33563 216 } else if (f_idx == CLS_F_IDX_TP_DST) {
5cb7a798
BP
217 eq = !((fixed->tp_dst ^ wild.flow.tp_dst)
218 & wild.wc.masks.tp_dst);
73c0ce34 219 } else if (f_idx == CLS_F_IDX_DL_SRC) {
5cb7a798
BP
220 eq = eth_addr_equal_except(fixed->dl_src, wild.flow.dl_src,
221 wild.wc.masks.dl_src);
73c0ce34 222 } else if (f_idx == CLS_F_IDX_DL_DST) {
5cb7a798
BP
223 eq = eth_addr_equal_except(fixed->dl_dst, wild.flow.dl_dst,
224 wild.wc.masks.dl_dst);
66642cb4 225 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
5cb7a798
BP
226 eq = !((fixed->vlan_tci ^ wild.flow.vlan_tci)
227 & wild.wc.masks.vlan_tci);
8368c090 228 } else if (f_idx == CLS_F_IDX_TUN_ID) {
296e07ac
JG
229 eq = !((fixed->tunnel.tun_id ^ wild.flow.tunnel.tun_id)
230 & wild.wc.masks.tunnel.tun_id);
7525e578 231 } else if (f_idx == CLS_F_IDX_METADATA) {
5cb7a798
BP
232 eq = !((fixed->metadata ^ wild.flow.metadata)
233 & wild.wc.masks.metadata);
2486e66a 234 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
5cb7a798
BP
235 eq = !((fixed->nw_tos ^ wild.flow.nw_tos) &
236 (wild.wc.masks.nw_tos & IP_DSCP_MASK));
851d3105 237 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
5cb7a798
BP
238 eq = !((fixed->nw_proto ^ wild.flow.nw_proto)
239 & wild.wc.masks.nw_proto);
e2170cff 240 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
5cb7a798
BP
241 eq = !((fixed->dl_type ^ wild.flow.dl_type)
242 & wild.wc.masks.dl_type);
0bdc4bec 243 } else if (f_idx == CLS_F_IDX_IN_PORT) {
4e022ec0
AW
244 eq = !((fixed->in_port.ofp_port
245 ^ wild.flow.in_port.ofp_port)
246 & wild.wc.masks.in_port.ofp_port);
d8ae4d67 247 } else {
428b2edd 248 OVS_NOT_REACHED();
064af421
BP
249 }
250
d8ae4d67
BP
251 if (!eq) {
252 return false;
064af421 253 }
064af421
BP
254 }
255 return true;
256}
257
258static struct cls_rule *
3c4486a5 259tcls_lookup(const struct tcls *cls, const struct flow *flow)
064af421
BP
260{
261 size_t i;
262
263 for (i = 0; i < cls->n_rules; i++) {
264 struct test_rule *pos = cls->rules[i];
3c4486a5 265 if (match(&pos->cls_rule, flow)) {
064af421
BP
266 return &pos->cls_rule;
267 }
268 }
269 return NULL;
270}
271
272static void
3c4486a5 273tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
064af421
BP
274{
275 size_t i;
276
277 for (i = 0; i < cls->n_rules; ) {
278 struct test_rule *pos = cls->rules[i];
5cb7a798
BP
279 if (!minimask_has_extra(&pos->cls_rule.match.mask,
280 &target->match.mask)) {
281 struct flow flow;
282
283 miniflow_expand(&pos->cls_rule.match.flow, &flow);
284 if (match(target, &flow)) {
285 tcls_remove(cls, pos);
286 continue;
287 }
064af421 288 }
5cb7a798 289 i++;
064af421
BP
290 }
291}
292\f
3c4486a5 293static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
965f03d8 294 CONSTANT_HTONL(0xc0a04455) };
3c4486a5 295static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
965f03d8 296 CONSTANT_HTONL(0xc0a04455) };
b9298d3f
BP
297static ovs_be64 tun_id_values[] = {
298 0,
299 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
7525e578
JS
300static ovs_be64 metadata_values[] = {
301 0,
302 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
4e022ec0 303static ofp_port_t in_port_values[] = { OFP_PORT_C(1), OFPP_LOCAL };
66642cb4 304static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
3c4486a5 305static ovs_be16 dl_type_values[]
965f03d8 306 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
3c4486a5 307static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
965f03d8 308 CONSTANT_HTONS(80) };
3c4486a5 309static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
064af421
BP
310static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
311 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
312static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
313 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
6767a2cc 314static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
2486e66a 315static uint8_t nw_dscp_values[] = { 48, 0 };
064af421
BP
316
317static void *values[CLS_N_FIELDS][2];
318
319static void
320init_values(void)
321{
659586ef
JG
322 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
323 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
324
7525e578
JS
325 values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
326 values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
327
064af421
BP
328 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
329 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
330
66642cb4
BP
331 values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
332 values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
959a2ecd 333
064af421
BP
334 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
335 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
336
337 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
338 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
339
340 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
341 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
342
343 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
344 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
345
346 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
347 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
348
349 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
350 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
351
2486e66a
JP
352 values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
353 values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
834377ea 354
064af421
BP
355 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
356 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
357
358 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
359 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
360}
361
362#define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
363#define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
659586ef 364#define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
7525e578 365#define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
064af421 366#define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
66642cb4 367#define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
064af421
BP
368#define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
369#define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
370#define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
371#define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
372#define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
373#define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
2486e66a 374#define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
064af421
BP
375
376#define N_FLOW_VALUES (N_NW_SRC_VALUES * \
377 N_NW_DST_VALUES * \
659586ef 378 N_TUN_ID_VALUES * \
064af421 379 N_IN_PORT_VALUES * \
66642cb4 380 N_VLAN_TCI_VALUES * \
064af421
BP
381 N_DL_TYPE_VALUES * \
382 N_TP_SRC_VALUES * \
383 N_TP_DST_VALUES * \
384 N_DL_SRC_VALUES * \
385 N_DL_DST_VALUES * \
834377ea 386 N_NW_PROTO_VALUES * \
2486e66a 387 N_NW_DSCP_VALUES)
064af421
BP
388
389static unsigned int
390get_value(unsigned int *x, unsigned n_values)
391{
392 unsigned int rem = *x % n_values;
393 *x /= n_values;
394 return rem;
395}
396
064af421
BP
397static void
398compare_classifiers(struct classifier *cls, struct tcls *tcls)
0b4f2078 399 OVS_REQ_RDLOCK(cls->rwlock)
064af421 400{
70d3fbe7 401 static const int confidence = 500;
064af421
BP
402 unsigned int i;
403
404 assert(classifier_count(cls) == tcls->n_rules);
70d3fbe7 405 for (i = 0; i < confidence; i++) {
476f36e8 406 struct cls_rule *cr0, *cr1, *cr2;
ae412e7d 407 struct flow flow;
476f36e8 408 struct flow_wildcards wc;
064af421 409 unsigned int x;
064af421 410
476f36e8 411 flow_wildcards_init_catchall(&wc);
b028db44 412 x = random_range(N_FLOW_VALUES);
51c14ddd 413 memset(&flow, 0, sizeof flow);
064af421
BP
414 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
415 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
296e07ac 416 flow.tunnel.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
7525e578 417 flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
4e022ec0
AW
418 flow.in_port.ofp_port = in_port_values[get_value(&x,
419 N_IN_PORT_VALUES)];
66642cb4 420 flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
064af421
BP
421 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
422 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
423 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
424 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
425 ETH_ADDR_LEN);
426 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
427 ETH_ADDR_LEN);
428 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
2486e66a 429 flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
064af421 430
476f36e8 431 cr0 = classifier_lookup(cls, &flow, &wc);
3c4486a5
BP
432 cr1 = tcls_lookup(tcls, &flow);
433 assert((cr0 == NULL) == (cr1 == NULL));
434 if (cr0 != NULL) {
435 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
436 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
437
193eb874 438 assert(cls_rule_equal(cr0, cr1));
3c4486a5 439 assert(tr0->aux == tr1->aux);
064af421 440 }
476f36e8
JR
441 cr2 = classifier_lookup(cls, &flow, NULL);
442 assert(cr2 == cr0);
064af421
BP
443 }
444}
445
064af421
BP
446static void
447destroy_classifier(struct classifier *cls)
448{
5ecc9d81
BP
449 struct test_rule *rule, *next_rule;
450 struct cls_cursor cursor;
451
06f81620 452 fat_rwlock_wrlock(&cls->rwlock);
5ecc9d81
BP
453 cls_cursor_init(&cursor, cls, NULL);
454 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
455 classifier_remove(cls, &rule->cls_rule);
48d28ac1 456 free_rule(rule);
5ecc9d81 457 }
06f81620 458 fat_rwlock_unlock(&cls->rwlock);
064af421
BP
459 classifier_destroy(cls);
460}
461
462static void
0b4f2078
EJ
463check_tables(const struct classifier *cls, int n_tables, int n_rules,
464 int n_dups) OVS_REQ_RDLOCK(cls->rwlock)
064af421 465{
03868246 466 const struct cls_subtable *table;
955f579d
BP
467 struct test_rule *test_rule;
468 struct cls_cursor cursor;
064af421 469 int found_tables = 0;
064af421 470 int found_rules = 0;
b5d97350 471 int found_dups = 0;
955f579d 472 int found_rules2 = 0;
064af421 473
03868246 474 HMAP_FOR_EACH (table, hmap_node, &cls->subtables) {
b5d97350 475 const struct cls_rule *head;
4d935a6b
JR
476 unsigned int max_priority = 0;
477 unsigned int max_count = 0;
b5d97350
BP
478
479 assert(!hmap_is_empty(&table->rules));
064af421 480
064af421 481 found_tables++;
b5d97350
BP
482 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
483 unsigned int prev_priority = UINT_MAX;
484 const struct cls_rule *rule;
485
4d935a6b
JR
486 if (head->priority > max_priority) {
487 max_priority = head->priority;
488 max_count = 1;
489 } else if (head->priority == max_priority) {
490 ++max_count;
491 }
492
b5d97350
BP
493 found_rules++;
494 LIST_FOR_EACH (rule, list, &head->list) {
495 assert(rule->priority < prev_priority);
4d935a6b
JR
496 assert(rule->priority <= table->max_priority);
497
b5d97350
BP
498 prev_priority = rule->priority;
499 found_rules++;
500 found_dups++;
501 assert(classifier_find_rule_exactly(cls, rule) == rule);
502 }
503 }
4d935a6b
JR
504 assert(table->max_priority == max_priority);
505 assert(table->max_count == max_count);
064af421
BP
506 }
507
03868246
JR
508 assert(found_tables == hmap_count(&cls->subtables));
509 assert(n_tables == -1 || n_tables == hmap_count(&cls->subtables));
064af421 510 assert(n_rules == -1 || found_rules == n_rules);
b5d97350 511 assert(n_dups == -1 || found_dups == n_dups);
955f579d
BP
512
513 cls_cursor_init(&cursor, cls, NULL);
514 CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) {
515 found_rules2++;
516 }
517 assert(found_rules == found_rules2);
064af421
BP
518}
519
520static struct test_rule *
521make_rule(int wc_fields, unsigned int priority, int value_pat)
522{
523 const struct cls_field *f;
524 struct test_rule *rule;
81a76618 525 struct match match;
064af421 526
81a76618 527 match_init_catchall(&match);
064af421
BP
528 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
529 int f_idx = f - cls_fields;
d8ae4d67 530 int value_idx = (value_pat & (1u << f_idx)) != 0;
81a76618 531 memcpy((char *) &match.flow + f->ofs,
d8ae4d67
BP
532 values[f_idx][value_idx], f->len);
533
0bdc4bec 534 if (f_idx == CLS_F_IDX_NW_SRC) {
b8266395 535 match.wc.masks.nw_src = OVS_BE32_MAX;
d8ae4d67 536 } else if (f_idx == CLS_F_IDX_NW_DST) {
b8266395 537 match.wc.masks.nw_dst = OVS_BE32_MAX;
73f33563 538 } else if (f_idx == CLS_F_IDX_TP_SRC) {
b8266395 539 match.wc.masks.tp_src = OVS_BE16_MAX;
73f33563 540 } else if (f_idx == CLS_F_IDX_TP_DST) {
b8266395 541 match.wc.masks.tp_dst = OVS_BE16_MAX;
73c0ce34 542 } else if (f_idx == CLS_F_IDX_DL_SRC) {
81a76618 543 memset(match.wc.masks.dl_src, 0xff, ETH_ADDR_LEN);
73c0ce34 544 } else if (f_idx == CLS_F_IDX_DL_DST) {
81a76618 545 memset(match.wc.masks.dl_dst, 0xff, ETH_ADDR_LEN);
66642cb4 546 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
b8266395 547 match.wc.masks.vlan_tci = OVS_BE16_MAX;
8368c090 548 } else if (f_idx == CLS_F_IDX_TUN_ID) {
b8266395 549 match.wc.masks.tunnel.tun_id = OVS_BE64_MAX;
7525e578 550 } else if (f_idx == CLS_F_IDX_METADATA) {
b8266395 551 match.wc.masks.metadata = OVS_BE64_MAX;
5d9499c4 552 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
81a76618 553 match.wc.masks.nw_tos |= IP_DSCP_MASK;
851d3105 554 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
81a76618 555 match.wc.masks.nw_proto = UINT8_MAX;
e2170cff 556 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
b8266395 557 match.wc.masks.dl_type = OVS_BE16_MAX;
0bdc4bec 558 } else if (f_idx == CLS_F_IDX_IN_PORT) {
e2711da9 559 match.wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
064af421 560 } else {
428b2edd 561 OVS_NOT_REACHED();
064af421
BP
562 }
563 }
81a76618
BP
564
565 rule = xzalloc(sizeof *rule);
566 cls_rule_init(&rule->cls_rule, &match, wc_fields ? priority : UINT_MAX);
064af421
BP
567 return rule;
568}
569
48d28ac1
BP
570static struct test_rule *
571clone_rule(const struct test_rule *src)
572{
573 struct test_rule *dst;
574
575 dst = xmalloc(sizeof *dst);
576 dst->aux = src->aux;
577 cls_rule_clone(&dst->cls_rule, &src->cls_rule);
578 return dst;
579}
580
581static void
582free_rule(struct test_rule *rule)
583{
584 cls_rule_destroy(&rule->cls_rule);
585 free(rule);
586}
587
064af421
BP
588static void
589shuffle(unsigned int *p, size_t n)
590{
591 for (; n > 1; n--, p++) {
b028db44 592 unsigned int *q = &p[random_range(n)];
064af421
BP
593 unsigned int tmp = *p;
594 *p = *q;
595 *q = tmp;
596 }
597}
5cb7a798
BP
598
599static void
600shuffle_u32s(uint32_t *p, size_t n)
601{
602 for (; n > 1; n--, p++) {
b028db44 603 uint32_t *q = &p[random_range(n)];
5cb7a798
BP
604 uint32_t tmp = *p;
605 *p = *q;
606 *q = tmp;
607 }
608}
064af421 609\f
5cb7a798
BP
610/* Classifier tests. */
611
13751fd8
JR
612static enum mf_field_id trie_fields[2] = {
613 MFF_IPV4_DST, MFF_IPV4_SRC
614};
615
064af421
BP
616/* Tests an empty classifier. */
617static void
3223e977 618test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
619{
620 struct classifier cls;
621 struct tcls tcls;
622
476f36e8 623 classifier_init(&cls, flow_segment_u32s);
06f81620 624 fat_rwlock_wrlock(&cls.rwlock);
13751fd8 625 classifier_set_prefix_fields(&cls, trie_fields, ARRAY_SIZE(trie_fields));
064af421
BP
626 tcls_init(&tcls);
627 assert(classifier_is_empty(&cls));
628 assert(tcls_is_empty(&tcls));
629 compare_classifiers(&cls, &tcls);
06f81620 630 fat_rwlock_unlock(&cls.rwlock);
064af421
BP
631 classifier_destroy(&cls);
632 tcls_destroy(&tcls);
633}
634
635/* Destroys a null classifier. */
636static void
3223e977 637test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
638{
639 classifier_destroy(NULL);
640}
641
642/* Tests classification with one rule at a time. */
643static void
3223e977 644test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
645{
646 unsigned int wc_fields; /* Hilarious. */
647
648 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
649 struct classifier cls;
650 struct test_rule *rule, *tcls_rule;
651 struct tcls tcls;
652
653 rule = make_rule(wc_fields,
654 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
655
476f36e8 656 classifier_init(&cls, flow_segment_u32s);
06f81620 657 fat_rwlock_wrlock(&cls.rwlock);
13751fd8
JR
658 classifier_set_prefix_fields(&cls, trie_fields,
659 ARRAY_SIZE(trie_fields));
064af421
BP
660 tcls_init(&tcls);
661
662 tcls_rule = tcls_insert(&tcls, rule);
08944c1d 663 classifier_insert(&cls, &rule->cls_rule);
b5d97350 664 check_tables(&cls, 1, 1, 0);
064af421
BP
665 compare_classifiers(&cls, &tcls);
666
667 classifier_remove(&cls, &rule->cls_rule);
668 tcls_remove(&tcls, tcls_rule);
669 assert(classifier_is_empty(&cls));
670 assert(tcls_is_empty(&tcls));
671 compare_classifiers(&cls, &tcls);
672
48d28ac1 673 free_rule(rule);
06f81620 674 fat_rwlock_unlock(&cls.rwlock);
064af421
BP
675 classifier_destroy(&cls);
676 tcls_destroy(&tcls);
677 }
678}
679
680/* Tests replacing one rule by another. */
681static void
3223e977 682test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
683{
684 unsigned int wc_fields;
685
686 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
687 struct classifier cls;
f0f4410a
BP
688 struct test_rule *rule1;
689 struct test_rule *rule2;
064af421
BP
690 struct tcls tcls;
691
692 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
693 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
694 rule2->aux += 5;
695 rule2->aux += 5;
696
476f36e8 697 classifier_init(&cls, flow_segment_u32s);
06f81620 698 fat_rwlock_wrlock(&cls.rwlock);
13751fd8
JR
699 classifier_set_prefix_fields(&cls, trie_fields,
700 ARRAY_SIZE(trie_fields));
064af421 701 tcls_init(&tcls);
f0f4410a 702 tcls_insert(&tcls, rule1);
08944c1d 703 classifier_insert(&cls, &rule1->cls_rule);
b5d97350 704 check_tables(&cls, 1, 1, 0);
064af421
BP
705 compare_classifiers(&cls, &tcls);
706 tcls_destroy(&tcls);
707
708 tcls_init(&tcls);
f0f4410a 709 tcls_insert(&tcls, rule2);
064af421 710 assert(test_rule_from_cls_rule(
08944c1d 711 classifier_replace(&cls, &rule2->cls_rule)) == rule1);
48d28ac1 712 free_rule(rule1);
b5d97350 713 check_tables(&cls, 1, 1, 0);
064af421
BP
714 compare_classifiers(&cls, &tcls);
715 tcls_destroy(&tcls);
06f81620 716 fat_rwlock_unlock(&cls.rwlock);
064af421
BP
717 destroy_classifier(&cls);
718 }
719}
720
721static int
b5d97350 722factorial(int n_items)
064af421 723{
b5d97350
BP
724 int n, i;
725
726 n = 1;
727 for (i = 2; i <= n_items; i++) {
728 n *= i;
729 }
730 return n;
064af421
BP
731}
732
b5d97350
BP
733static void
734swap(int *a, int *b)
064af421 735{
b5d97350
BP
736 int tmp = *a;
737 *a = *b;
738 *b = tmp;
064af421
BP
739}
740
064af421 741static void
b5d97350 742reverse(int *a, int n)
064af421 743{
b5d97350 744 int i;
064af421 745
b5d97350
BP
746 for (i = 0; i < n / 2; i++) {
747 int j = n - (i + 1);
748 swap(&a[i], &a[j]);
749 }
750}
064af421 751
b5d97350
BP
752static bool
753next_permutation(int *a, int n)
754{
755 int k;
064af421 756
b5d97350
BP
757 for (k = n - 2; k >= 0; k--) {
758 if (a[k] < a[k + 1]) {
759 int l;
064af421 760
b5d97350
BP
761 for (l = n - 1; ; l--) {
762 if (a[l] > a[k]) {
763 swap(&a[k], &a[l]);
764 reverse(a + (k + 1), n - (k + 1));
765 return true;
064af421
BP
766 }
767 }
768 }
769 }
b5d97350 770 return false;
064af421
BP
771}
772
b5d97350 773/* Tests classification with rules that have the same matching criteria. */
064af421 774static void
b5d97350
BP
775test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
776{
777 enum { N_RULES = 3 };
778 int n_pris;
064af421 779
b5d97350
BP
780 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
781 int ops[N_RULES * 2];
782 int pris[N_RULES];
783 int n_permutations;
784 int i;
064af421 785
b5d97350
BP
786 pris[0] = 0;
787 for (i = 1; i < N_RULES; i++) {
788 pris[i] = pris[i - 1] + (n_pris > i);
789 }
064af421 790
b5d97350
BP
791 for (i = 0; i < N_RULES * 2; i++) {
792 ops[i] = i / 2;
064af421 793 }
064af421 794
b5d97350
BP
795 n_permutations = 0;
796 do {
797 struct test_rule *rules[N_RULES];
798 struct test_rule *tcls_rules[N_RULES];
799 int pri_rules[N_RULES];
800 struct classifier cls;
801 struct tcls tcls;
064af421 802
b5d97350 803 n_permutations++;
064af421 804
b5d97350
BP
805 for (i = 0; i < N_RULES; i++) {
806 rules[i] = make_rule(456, pris[i], 0);
807 tcls_rules[i] = NULL;
808 pri_rules[i] = -1;
809 }
064af421 810
476f36e8 811 classifier_init(&cls, flow_segment_u32s);
06f81620 812 fat_rwlock_wrlock(&cls.rwlock);
13751fd8
JR
813 classifier_set_prefix_fields(&cls, trie_fields,
814 ARRAY_SIZE(trie_fields));
b5d97350 815 tcls_init(&tcls);
064af421 816
b5d97350
BP
817 for (i = 0; i < ARRAY_SIZE(ops); i++) {
818 int j = ops[i];
819 int m, n;
064af421 820
b5d97350
BP
821 if (!tcls_rules[j]) {
822 struct test_rule *displaced_rule;
823
824 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
825 displaced_rule = test_rule_from_cls_rule(
08944c1d 826 classifier_replace(&cls, &rules[j]->cls_rule));
b5d97350
BP
827 if (pri_rules[pris[j]] >= 0) {
828 int k = pri_rules[pris[j]];
829 assert(displaced_rule != NULL);
830 assert(displaced_rule != rules[j]);
831 assert(pris[j] == displaced_rule->cls_rule.priority);
832 tcls_rules[k] = NULL;
833 } else {
834 assert(displaced_rule == NULL);
835 }
836 pri_rules[pris[j]] = j;
837 } else {
838 classifier_remove(&cls, &rules[j]->cls_rule);
839 tcls_remove(&tcls, tcls_rules[j]);
840 tcls_rules[j] = NULL;
841 pri_rules[pris[j]] = -1;
842 }
064af421 843
b5d97350
BP
844 n = 0;
845 for (m = 0; m < N_RULES; m++) {
846 n += tcls_rules[m] != NULL;
064af421 847 }
b5d97350
BP
848 check_tables(&cls, n > 0, n, n - 1);
849
850 compare_classifiers(&cls, &tcls);
064af421 851 }
b5d97350 852
06f81620 853 fat_rwlock_unlock(&cls.rwlock);
b5d97350
BP
854 classifier_destroy(&cls);
855 tcls_destroy(&tcls);
856
857 for (i = 0; i < N_RULES; i++) {
48d28ac1 858 free_rule(rules[i]);
b5d97350
BP
859 }
860 } while (next_permutation(ops, ARRAY_SIZE(ops)));
861 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
064af421
BP
862 }
863}
864
b5d97350
BP
865static int
866count_ones(unsigned long int x)
064af421 867{
b5d97350 868 int n = 0;
064af421 869
b5d97350 870 while (x) {
8472a3ce 871 x = zero_rightmost_1bit(x);
b5d97350
BP
872 n++;
873 }
064af421 874
b5d97350
BP
875 return n;
876}
064af421 877
b5d97350
BP
878static bool
879array_contains(int *array, int n, int value)
880{
881 int i;
064af421 882
b5d97350
BP
883 for (i = 0; i < n; i++) {
884 if (array[i] == value) {
885 return true;
064af421
BP
886 }
887 }
b5d97350
BP
888
889 return false;
064af421
BP
890}
891
b5d97350
BP
892/* Tests classification with two rules at a time that fall into the same
893 * table but different lists. */
064af421 894static void
3223e977 895test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421 896{
b5d97350 897 int iteration;
064af421 898
b5d97350
BP
899 for (iteration = 0; iteration < 50; iteration++) {
900 enum { N_RULES = 20 };
901 struct test_rule *rules[N_RULES];
902 struct test_rule *tcls_rules[N_RULES];
903 struct classifier cls;
904 struct tcls tcls;
905 int value_pats[N_RULES];
906 int value_mask;
907 int wcf;
908 int i;
064af421 909
b5d97350 910 do {
b028db44 911 wcf = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
b5d97350
BP
912 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
913 } while ((1 << count_ones(value_mask)) < N_RULES);
064af421 914
476f36e8 915 classifier_init(&cls, flow_segment_u32s);
06f81620 916 fat_rwlock_wrlock(&cls.rwlock);
13751fd8
JR
917 classifier_set_prefix_fields(&cls, trie_fields,
918 ARRAY_SIZE(trie_fields));
b5d97350 919 tcls_init(&tcls);
064af421 920
b5d97350 921 for (i = 0; i < N_RULES; i++) {
b028db44 922 unsigned int priority = random_uint32();
064af421 923
b5d97350 924 do {
b028db44 925 value_pats[i] = random_uint32() & value_mask;
b5d97350 926 } while (array_contains(value_pats, i, value_pats[i]));
064af421 927
b5d97350
BP
928 rules[i] = make_rule(wcf, priority, value_pats[i]);
929 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
08944c1d 930 classifier_insert(&cls, &rules[i]->cls_rule);
b5d97350
BP
931
932 check_tables(&cls, 1, i + 1, 0);
933 compare_classifiers(&cls, &tcls);
934 }
935
936 for (i = 0; i < N_RULES; i++) {
937 tcls_remove(&tcls, tcls_rules[i]);
938 classifier_remove(&cls, &rules[i]->cls_rule);
48d28ac1 939 free_rule(rules[i]);
b5d97350
BP
940
941 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
942 compare_classifiers(&cls, &tcls);
064af421 943 }
b5d97350 944
06f81620 945 fat_rwlock_unlock(&cls.rwlock);
b5d97350
BP
946 classifier_destroy(&cls);
947 tcls_destroy(&tcls);
064af421
BP
948 }
949}
950
b5d97350
BP
951/* Tests classification with many rules at a time that fall into random lists
952 * in 'n' tables. */
064af421 953static void
b5d97350 954test_many_rules_in_n_tables(int n_tables)
064af421
BP
955{
956 enum { MAX_RULES = 50 };
b5d97350 957 int wcfs[10];
064af421 958 int iteration;
b5d97350
BP
959 int i;
960
961 assert(n_tables < 10);
962 for (i = 0; i < n_tables; i++) {
963 do {
b028db44 964 wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
b5d97350
BP
965 } while (array_contains(wcfs, i, wcfs[i]));
966 }
064af421
BP
967
968 for (iteration = 0; iteration < 30; iteration++) {
969 unsigned int priorities[MAX_RULES];
970 struct classifier cls;
971 struct tcls tcls;
064af421 972
b028db44 973 random_set_seed(iteration + 1);
064af421
BP
974 for (i = 0; i < MAX_RULES; i++) {
975 priorities[i] = i * 129;
976 }
977 shuffle(priorities, ARRAY_SIZE(priorities));
978
476f36e8 979 classifier_init(&cls, flow_segment_u32s);
06f81620 980 fat_rwlock_wrlock(&cls.rwlock);
13751fd8
JR
981 classifier_set_prefix_fields(&cls, trie_fields,
982 ARRAY_SIZE(trie_fields));
064af421
BP
983 tcls_init(&tcls);
984
985 for (i = 0; i < MAX_RULES; i++) {
986 struct test_rule *rule;
987 unsigned int priority = priorities[i];
b028db44
BP
988 int wcf = wcfs[random_range(n_tables)];
989 int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
064af421
BP
990 rule = make_rule(wcf, priority, value_pat);
991 tcls_insert(&tcls, rule);
08944c1d 992 classifier_insert(&cls, &rule->cls_rule);
b5d97350 993 check_tables(&cls, -1, i + 1, -1);
064af421
BP
994 compare_classifiers(&cls, &tcls);
995 }
996
997 while (!classifier_is_empty(&cls)) {
5ecc9d81
BP
998 struct test_rule *rule, *next_rule;
999 struct test_rule *target;
1000 struct cls_cursor cursor;
1001
b028db44 1002 target = clone_rule(tcls.rules[random_range(tcls.n_rules)]);
5ecc9d81
BP
1003
1004 cls_cursor_init(&cursor, &cls, &target->cls_rule);
1005 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
1006 classifier_remove(&cls, &rule->cls_rule);
48d28ac1 1007 free_rule(rule);
5ecc9d81
BP
1008 }
1009 tcls_delete_matches(&tcls, &target->cls_rule);
064af421 1010 compare_classifiers(&cls, &tcls);
b5d97350 1011 check_tables(&cls, -1, -1, -1);
48d28ac1 1012 free_rule(target);
064af421 1013 }
064af421 1014
06f81620 1015 fat_rwlock_unlock(&cls.rwlock);
064af421
BP
1016 destroy_classifier(&cls);
1017 tcls_destroy(&tcls);
1018 }
1019}
b5d97350
BP
1020
1021static void
1022test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1023{
1024 test_many_rules_in_n_tables(2);
1025}
1026
1027static void
1028test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1029{
1030 test_many_rules_in_n_tables(5);
1031}
064af421 1032\f
5cb7a798
BP
1033/* Miniflow tests. */
1034
1035static uint32_t
1036random_value(void)
1037{
1038 static const uint32_t values[] =
1039 { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1040 0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1041
b028db44 1042 return values[random_range(ARRAY_SIZE(values))];
5cb7a798
BP
1043}
1044
1045static bool
1046choose(unsigned int n, unsigned int *idxp)
1047{
1048 if (*idxp < n) {
1049 return true;
1050 } else {
1051 *idxp -= n;
1052 return false;
1053 }
1054}
1055
1056static bool
1057init_consecutive_values(int n_consecutive, struct flow *flow,
1058 unsigned int *idxp)
1059{
1060 uint32_t *flow_u32 = (uint32_t *) flow;
1061
1062 if (choose(FLOW_U32S - n_consecutive + 1, idxp)) {
1063 int i;
1064
1065 for (i = 0; i < n_consecutive; i++) {
1066 flow_u32[*idxp + i] = random_value();
1067 }
1068 return true;
1069 } else {
1070 return false;
1071 }
1072}
1073
1074static bool
1075next_random_flow(struct flow *flow, unsigned int idx)
1076{
1077 uint32_t *flow_u32 = (uint32_t *) flow;
1078 int i;
1079
1080 memset(flow, 0, sizeof *flow);
1081
1082 /* Empty flow. */
1083 if (choose(1, &idx)) {
1084 return true;
1085 }
1086
1087 /* All flows with a small number of consecutive nonzero values. */
1088 for (i = 1; i <= 4; i++) {
1089 if (init_consecutive_values(i, flow, &idx)) {
1090 return true;
1091 }
1092 }
1093
1094 /* All flows with a large number of consecutive nonzero values. */
1095 for (i = FLOW_U32S - 4; i <= FLOW_U32S; i++) {
1096 if (init_consecutive_values(i, flow, &idx)) {
1097 return true;
1098 }
1099 }
1100
1101 /* All flows with exactly two nonconsecutive nonzero values. */
1102 if (choose((FLOW_U32S - 1) * (FLOW_U32S - 2) / 2, &idx)) {
1103 int ofs1;
1104
1105 for (ofs1 = 0; ofs1 < FLOW_U32S - 2; ofs1++) {
1106 int ofs2;
1107
1108 for (ofs2 = ofs1 + 2; ofs2 < FLOW_U32S; ofs2++) {
1109 if (choose(1, &idx)) {
1110 flow_u32[ofs1] = random_value();
1111 flow_u32[ofs2] = random_value();
1112 return true;
1113 }
1114 }
1115 }
428b2edd 1116 OVS_NOT_REACHED();
5cb7a798
BP
1117 }
1118
1119 /* 16 randomly chosen flows with N >= 3 nonzero values. */
1120 if (choose(16 * (FLOW_U32S - 4), &idx)) {
1121 int n = idx / 16 + 3;
1122 int i;
1123
1124 for (i = 0; i < n; i++) {
1125 flow_u32[i] = random_value();
1126 }
1127 shuffle_u32s(flow_u32, FLOW_U32S);
1128
1129 return true;
1130 }
1131
1132 return false;
1133}
1134
1135static void
1136any_random_flow(struct flow *flow)
1137{
1138 static unsigned int max;
1139 if (!max) {
1140 while (next_random_flow(flow, max)) {
1141 max++;
1142 }
1143 }
1144
1145 next_random_flow(flow, random_range(max));
1146}
1147
1148static void
1149toggle_masked_flow_bits(struct flow *flow, const struct flow_wildcards *mask)
1150{
1151 const uint32_t *mask_u32 = (const uint32_t *) &mask->masks;
1152 uint32_t *flow_u32 = (uint32_t *) flow;
1153 int i;
1154
1155 for (i = 0; i < FLOW_U32S; i++) {
1156 if (mask_u32[i] != 0) {
1157 uint32_t bit;
1158
1159 do {
1160 bit = 1u << random_range(32);
1161 } while (!(bit & mask_u32[i]));
1162 flow_u32[i] ^= bit;
1163 }
1164 }
1165}
1166
1167static void
1168wildcard_extra_bits(struct flow_wildcards *mask)
1169{
1170 uint32_t *mask_u32 = (uint32_t *) &mask->masks;
1171 int i;
1172
1173 for (i = 0; i < FLOW_U32S; i++) {
1174 if (mask_u32[i] != 0) {
1175 uint32_t bit;
1176
1177 do {
1178 bit = 1u << random_range(32);
1179 } while (!(bit & mask_u32[i]));
1180 mask_u32[i] &= ~bit;
1181 }
1182 }
1183}
1184
1185static void
1186test_miniflow(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1187{
1188 struct flow flow;
1189 unsigned int idx;
1190
1191 random_set_seed(0xb3faca38);
1192 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1193 const uint32_t *flow_u32 = (const uint32_t *) &flow;
1194 struct miniflow miniflow, miniflow2, miniflow3;
1195 struct flow flow2, flow3;
1196 struct flow_wildcards mask;
1197 struct minimask minimask;
1198 int i;
1199
1200 /* Convert flow to miniflow. */
1201 miniflow_init(&miniflow, &flow);
1202
1203 /* Check that the flow equals its miniflow. */
1204 assert(miniflow_get_vid(&miniflow) == vlan_tci_to_vid(flow.vlan_tci));
1205 for (i = 0; i < FLOW_U32S; i++) {
1206 assert(miniflow_get(&miniflow, i) == flow_u32[i]);
1207 }
1208
1209 /* Check that the miniflow equals itself. */
1210 assert(miniflow_equal(&miniflow, &miniflow));
1211
1212 /* Convert miniflow back to flow and verify that it's the same. */
1213 miniflow_expand(&miniflow, &flow2);
1214 assert(flow_equal(&flow, &flow2));
1215
1216 /* Check that copying a miniflow works properly. */
1217 miniflow_clone(&miniflow2, &miniflow);
1218 assert(miniflow_equal(&miniflow, &miniflow2));
1219 assert(miniflow_hash(&miniflow, 0) == miniflow_hash(&miniflow2, 0));
1220 miniflow_expand(&miniflow2, &flow3);
1221 assert(flow_equal(&flow, &flow3));
1222
1223 /* Check that masked matches work as expected for identical flows and
1224 * miniflows. */
1225 do {
1226 next_random_flow(&mask.masks, 1);
1227 } while (flow_wildcards_is_catchall(&mask));
1228 minimask_init(&minimask, &mask);
1229 assert(minimask_is_catchall(&minimask)
1230 == flow_wildcards_is_catchall(&mask));
1231 assert(miniflow_equal_in_minimask(&miniflow, &miniflow2, &minimask));
1232 assert(miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1233 assert(miniflow_hash_in_minimask(&miniflow, &minimask, 0x12345678) ==
1234 flow_hash_in_minimask(&flow, &minimask, 0x12345678));
1235
1236 /* Check that masked matches work as expected for differing flows and
1237 * miniflows. */
1238 toggle_masked_flow_bits(&flow2, &mask);
1239 assert(!miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1240 miniflow_init(&miniflow3, &flow2);
1241 assert(!miniflow_equal_in_minimask(&miniflow, &miniflow3, &minimask));
1242
1243 /* Clean up. */
1244 miniflow_destroy(&miniflow);
1245 miniflow_destroy(&miniflow2);
1246 miniflow_destroy(&miniflow3);
1247 minimask_destroy(&minimask);
1248 }
1249}
1250
1251static void
1252test_minimask_has_extra(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1253{
1254 struct flow_wildcards catchall;
1255 struct minimask minicatchall;
1256 struct flow flow;
1257 unsigned int idx;
1258
1259 flow_wildcards_init_catchall(&catchall);
1260 minimask_init(&minicatchall, &catchall);
1261 assert(minimask_is_catchall(&minicatchall));
1262
1263 random_set_seed(0x2ec7905b);
1264 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1265 struct flow_wildcards mask;
1266 struct minimask minimask;
1267
1268 mask.masks = flow;
1269 minimask_init(&minimask, &mask);
1270 assert(!minimask_has_extra(&minimask, &minimask));
1271 assert(minimask_has_extra(&minicatchall, &minimask)
1272 == !minimask_is_catchall(&minimask));
1273 if (!minimask_is_catchall(&minimask)) {
1274 struct minimask minimask2;
1275
1276 wildcard_extra_bits(&mask);
1277 minimask_init(&minimask2, &mask);
1278 assert(minimask_has_extra(&minimask2, &minimask));
1279 assert(!minimask_has_extra(&minimask, &minimask2));
1280 minimask_destroy(&minimask2);
1281 }
1282
1283 minimask_destroy(&minimask);
1284 }
f2f3f5cb
BP
1285
1286 minimask_destroy(&minicatchall);
5cb7a798
BP
1287}
1288
1289static void
1290test_minimask_combine(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1291{
1292 struct flow_wildcards catchall;
1293 struct minimask minicatchall;
1294 struct flow flow;
1295 unsigned int idx;
1296
1297 flow_wildcards_init_catchall(&catchall);
1298 minimask_init(&minicatchall, &catchall);
1299 assert(minimask_is_catchall(&minicatchall));
1300
1301 random_set_seed(0x181bf0cd);
1302 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1303 struct minimask minimask, minimask2, minicombined;
1304 struct flow_wildcards mask, mask2, combined, combined2;
1305 uint32_t storage[FLOW_U32S];
1306 struct flow flow2;
1307
1308 mask.masks = flow;
1309 minimask_init(&minimask, &mask);
1310
1311 minimask_combine(&minicombined, &minimask, &minicatchall, storage);
1312 assert(minimask_is_catchall(&minicombined));
1313
1314 any_random_flow(&flow2);
1315 mask2.masks = flow2;
1316 minimask_init(&minimask2, &mask2);
1317
1318 minimask_combine(&minicombined, &minimask, &minimask2, storage);
368eefac 1319 flow_wildcards_and(&combined, &mask, &mask2);
5cb7a798
BP
1320 minimask_expand(&minicombined, &combined2);
1321 assert(flow_wildcards_equal(&combined, &combined2));
1322
1323 minimask_destroy(&minimask);
1324 minimask_destroy(&minimask2);
1325 }
f2f3f5cb
BP
1326
1327 minimask_destroy(&minicatchall);
5cb7a798
BP
1328}
1329\f
3223e977 1330static const struct command commands[] = {
5cb7a798 1331 /* Classifier tests. */
3223e977
BP
1332 {"empty", 0, 0, test_empty},
1333 {"destroy-null", 0, 0, test_destroy_null},
1334 {"single-rule", 0, 0, test_single_rule},
1335 {"rule-replacement", 0, 0, test_rule_replacement},
b5d97350 1336 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
3223e977 1337 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
b5d97350
BP
1338 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
1339 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
5cb7a798
BP
1340
1341 /* Miniflow and minimask tests. */
1342 {"miniflow", 0, 0, test_miniflow},
1343 {"minimask_has_extra", 0, 0, test_minimask_has_extra},
1344 {"minimask_combine", 0, 0, test_minimask_combine},
1345
3223e977
BP
1346 {NULL, 0, 0, NULL},
1347};
064af421
BP
1348
1349int
3223e977 1350main(int argc, char *argv[])
064af421 1351{
b5d97350 1352 set_program_name(argv[0]);
064af421 1353 init_values();
3223e977 1354 run_command(argc - 1, argv + 1, commands);
064af421
BP
1355 return 0;
1356}