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