]> git.proxmox.com Git - ovs.git/blame - tests/test-classifier.c
raft: Fix the problem of stuck in candidate role forever.
[ovs.git] / tests / test-classifier.c
CommitLineData
064af421 1/*
da9cfca6 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"
1add079e 37#include "fatal-signal.h"
064af421 38#include "flow.h"
3f636c7e 39#include "ovstest.h"
1add079e
JR
40#include "ovs-atomic.h"
41#include "ovs-thread.h"
064af421 42#include "packets.h"
b028db44 43#include "random.h"
1add079e 44#include "timeval.h"
c0a56d9f 45#include "unaligned.h"
38c449e0 46#include "util.h"
3d91d909 47
3bbe9a1f
JR
48static bool versioned = false;
49
b5d97350 50/* Fields in a rule. */
efe2037e
WT
51#define CLS_FIELDS \
52 /* struct flow all-caps */ \
53 /* member name name */ \
54 /* ----------- -------- */ \
55 CLS_FIELD(tunnel.tun_id, TUN_ID) \
56 CLS_FIELD(metadata, METADATA) \
57 CLS_FIELD(nw_src, NW_SRC) \
58 CLS_FIELD(nw_dst, NW_DST) \
59 CLS_FIELD(in_port.ofp_port, IN_PORT) \
f0fb825a 60 CLS_FIELD(vlans[0].tci, VLAN_TCI) \
efe2037e
WT
61 CLS_FIELD(dl_type, DL_TYPE) \
62 CLS_FIELD(tp_src, TP_SRC) \
63 CLS_FIELD(tp_dst, TP_DST) \
64 CLS_FIELD(dl_src, DL_SRC) \
65 CLS_FIELD(dl_dst, DL_DST) \
66 CLS_FIELD(nw_proto, NW_PROTO) \
67 CLS_FIELD(nw_tos, NW_DSCP)
b5d97350
BP
68
69/* Field indexes.
70 *
71 * (These are also indexed into struct classifier's 'tables' array.) */
72enum {
0bdc4bec 73#define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME,
b5d97350
BP
74 CLS_FIELDS
75#undef CLS_FIELD
76 CLS_N_FIELDS
77};
78
79/* Field information. */
80struct cls_field {
81 int ofs; /* Offset in struct flow. */
82 int len; /* Length in bytes. */
b5d97350
BP
83 const char *name; /* Name (for debugging). */
84};
85
86static const struct cls_field cls_fields[CLS_N_FIELDS] = {
0bdc4bec 87#define CLS_FIELD(MEMBER, NAME) \
b5d97350
BP
88 { offsetof(struct flow, MEMBER), \
89 sizeof ((struct flow *)0)->MEMBER, \
b5d97350
BP
90 #NAME },
91 CLS_FIELDS
92#undef CLS_FIELD
93};
94
064af421 95struct test_rule {
3bbe9a1f 96 struct ovs_list list_node;
064af421
BP
97 int aux; /* Auxiliary data. */
98 struct cls_rule cls_rule; /* Classifier rule data. */
99};
100
101static struct test_rule *
102test_rule_from_cls_rule(const struct cls_rule *rule)
103{
104 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
105}
106
f2f3f5cb
BP
107static void
108test_rule_destroy(struct test_rule *rule)
109{
110 if (rule) {
111 cls_rule_destroy(&rule->cls_rule);
112 free(rule);
113 }
114}
115
bd53aa17 116static struct test_rule *make_rule(int wc_fields, int priority, int value_pat);
48d28ac1
BP
117static void free_rule(struct test_rule *);
118static struct test_rule *clone_rule(const struct test_rule *);
119
064af421
BP
120/* Trivial (linear) classifier. */
121struct tcls {
122 size_t n_rules;
123 size_t allocated_rules;
124 struct test_rule **rules;
125};
126
127static void
128tcls_init(struct tcls *tcls)
129{
130 tcls->n_rules = 0;
131 tcls->allocated_rules = 0;
132 tcls->rules = NULL;
133}
134
135static void
136tcls_destroy(struct tcls *tcls)
137{
138 if (tcls) {
139 size_t i;
140
141 for (i = 0; i < tcls->n_rules; i++) {
f2f3f5cb 142 test_rule_destroy(tcls->rules[i]);
064af421
BP
143 }
144 free(tcls->rules);
145 }
146}
147
064af421
BP
148static bool
149tcls_is_empty(const struct tcls *tcls)
150{
151 return tcls->n_rules == 0;
152}
153
154static struct test_rule *
155tcls_insert(struct tcls *tcls, const struct test_rule *rule)
156{
157 size_t i;
158
064af421
BP
159 for (i = 0; i < tcls->n_rules; i++) {
160 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
193eb874
BP
161 if (cls_rule_equal(pos, &rule->cls_rule)) {
162 /* Exact match. */
42767cce 163 ovsrcu_postpone(free_rule, tcls->rules[i]);
48d28ac1 164 tcls->rules[i] = clone_rule(rule);
064af421 165 return tcls->rules[i];
af7b73f4 166 } else if (pos->priority < rule->cls_rule.priority) {
064af421
BP
167 break;
168 }
169 }
170
171 if (tcls->n_rules >= tcls->allocated_rules) {
172 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
173 sizeof *tcls->rules);
174 }
175 if (i != tcls->n_rules) {
176 memmove(&tcls->rules[i + 1], &tcls->rules[i],
177 sizeof *tcls->rules * (tcls->n_rules - i));
178 }
48d28ac1 179 tcls->rules[i] = clone_rule(rule);
064af421
BP
180 tcls->n_rules++;
181 return tcls->rules[i];
182}
183
184static void
185tcls_remove(struct tcls *cls, const struct test_rule *rule)
186{
187 size_t i;
188
189 for (i = 0; i < cls->n_rules; i++) {
190 struct test_rule *pos = cls->rules[i];
191 if (pos == rule) {
f2f3f5cb
BP
192 test_rule_destroy(pos);
193
064af421
BP
194 memmove(&cls->rules[i], &cls->rules[i + 1],
195 sizeof *cls->rules * (cls->n_rules - i - 1));
f2f3f5cb 196
064af421
BP
197 cls->n_rules--;
198 return;
199 }
200 }
428b2edd 201 OVS_NOT_REACHED();
064af421
BP
202}
203
064af421 204static bool
5cb7a798 205match(const struct cls_rule *wild_, const struct flow *fixed)
064af421 206{
5cb7a798 207 struct match wild;
064af421
BP
208 int f_idx;
209
5cb7a798 210 minimatch_expand(&wild_->match, &wild);
064af421 211 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
d8ae4d67
BP
212 bool eq;
213
0bdc4bec 214 if (f_idx == CLS_F_IDX_NW_SRC) {
5cb7a798
BP
215 eq = !((fixed->nw_src ^ wild.flow.nw_src)
216 & wild.wc.masks.nw_src);
d8ae4d67 217 } else if (f_idx == CLS_F_IDX_NW_DST) {
5cb7a798
BP
218 eq = !((fixed->nw_dst ^ wild.flow.nw_dst)
219 & wild.wc.masks.nw_dst);
73f33563 220 } else if (f_idx == CLS_F_IDX_TP_SRC) {
5cb7a798
BP
221 eq = !((fixed->tp_src ^ wild.flow.tp_src)
222 & wild.wc.masks.tp_src);
73f33563 223 } else if (f_idx == CLS_F_IDX_TP_DST) {
5cb7a798
BP
224 eq = !((fixed->tp_dst ^ wild.flow.tp_dst)
225 & wild.wc.masks.tp_dst);
73c0ce34 226 } else if (f_idx == CLS_F_IDX_DL_SRC) {
5cb7a798
BP
227 eq = eth_addr_equal_except(fixed->dl_src, wild.flow.dl_src,
228 wild.wc.masks.dl_src);
73c0ce34 229 } else if (f_idx == CLS_F_IDX_DL_DST) {
5cb7a798
BP
230 eq = eth_addr_equal_except(fixed->dl_dst, wild.flow.dl_dst,
231 wild.wc.masks.dl_dst);
66642cb4 232 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
f0fb825a
EG
233 eq = !((fixed->vlans[0].tci ^ wild.flow.vlans[0].tci)
234 & wild.wc.masks.vlans[0].tci);
8368c090 235 } else if (f_idx == CLS_F_IDX_TUN_ID) {
296e07ac
JG
236 eq = !((fixed->tunnel.tun_id ^ wild.flow.tunnel.tun_id)
237 & wild.wc.masks.tunnel.tun_id);
7525e578 238 } else if (f_idx == CLS_F_IDX_METADATA) {
5cb7a798
BP
239 eq = !((fixed->metadata ^ wild.flow.metadata)
240 & wild.wc.masks.metadata);
2486e66a 241 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
5cb7a798
BP
242 eq = !((fixed->nw_tos ^ wild.flow.nw_tos) &
243 (wild.wc.masks.nw_tos & IP_DSCP_MASK));
851d3105 244 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
5cb7a798
BP
245 eq = !((fixed->nw_proto ^ wild.flow.nw_proto)
246 & wild.wc.masks.nw_proto);
e2170cff 247 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
5cb7a798
BP
248 eq = !((fixed->dl_type ^ wild.flow.dl_type)
249 & wild.wc.masks.dl_type);
0bdc4bec 250 } else if (f_idx == CLS_F_IDX_IN_PORT) {
4e022ec0
AW
251 eq = !((fixed->in_port.ofp_port
252 ^ wild.flow.in_port.ofp_port)
253 & wild.wc.masks.in_port.ofp_port);
d8ae4d67 254 } else {
428b2edd 255 OVS_NOT_REACHED();
064af421
BP
256 }
257
d8ae4d67
BP
258 if (!eq) {
259 return false;
064af421 260 }
064af421
BP
261 }
262 return true;
263}
264
265static struct cls_rule *
3c4486a5 266tcls_lookup(const struct tcls *cls, const struct flow *flow)
064af421
BP
267{
268 size_t i;
269
270 for (i = 0; i < cls->n_rules; i++) {
271 struct test_rule *pos = cls->rules[i];
3c4486a5 272 if (match(&pos->cls_rule, flow)) {
064af421
BP
273 return &pos->cls_rule;
274 }
275 }
276 return NULL;
277}
278
279static void
3c4486a5 280tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
064af421
BP
281{
282 size_t i;
283
284 for (i = 0; i < cls->n_rules; ) {
285 struct test_rule *pos = cls->rules[i];
8fd47924
JR
286 if (!minimask_has_extra(pos->cls_rule.match.mask,
287 target->match.mask)) {
5cb7a798
BP
288 struct flow flow;
289
8fd47924 290 miniflow_expand(pos->cls_rule.match.flow, &flow);
5cb7a798
BP
291 if (match(target, &flow)) {
292 tcls_remove(cls, pos);
293 continue;
294 }
064af421 295 }
5cb7a798 296 i++;
064af421
BP
297 }
298}
299\f
3c4486a5 300static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
965f03d8 301 CONSTANT_HTONL(0xc0a04455) };
3c4486a5 302static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
965f03d8 303 CONSTANT_HTONL(0xc0a04455) };
b9298d3f
BP
304static ovs_be64 tun_id_values[] = {
305 0,
306 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
7525e578
JS
307static ovs_be64 metadata_values[] = {
308 0,
309 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
4e022ec0 310static ofp_port_t in_port_values[] = { OFP_PORT_C(1), OFPP_LOCAL };
66642cb4 311static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
3c4486a5 312static ovs_be16 dl_type_values[]
965f03d8 313 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
3c4486a5 314static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
965f03d8 315 CONSTANT_HTONS(80) };
3c4486a5 316static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
74ff3298 317static struct eth_addr dl_src_values[] = {
134fefa4
BP
318 ETH_ADDR_C(00,02,e3,0f,80,a4),
319 ETH_ADDR_C(5e,33,7f,5f,1e,99)
320};
74ff3298 321static struct eth_addr dl_dst_values[] = {
134fefa4
BP
322 ETH_ADDR_C(4a,27,71,ae,64,c1),
323 ETH_ADDR_C(ff,ff,ff,ff,ff,ff)
324};
6767a2cc 325static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
2486e66a 326static uint8_t nw_dscp_values[] = { 48, 0 };
064af421
BP
327
328static void *values[CLS_N_FIELDS][2];
329
330static void
331init_values(void)
332{
659586ef
JG
333 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
334 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
335
7525e578
JS
336 values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
337 values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
338
064af421
BP
339 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
340 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
341
66642cb4
BP
342 values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
343 values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
959a2ecd 344
74ff3298
JR
345 values[CLS_F_IDX_DL_SRC][0] = &dl_src_values[0];
346 values[CLS_F_IDX_DL_SRC][1] = &dl_src_values[1];
064af421 347
74ff3298
JR
348 values[CLS_F_IDX_DL_DST][0] = &dl_dst_values[0];
349 values[CLS_F_IDX_DL_DST][1] = &dl_dst_values[1];
064af421
BP
350
351 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
352 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
353
354 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
355 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
356
357 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
358 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
359
360 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
361 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
362
2486e66a
JP
363 values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
364 values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
834377ea 365
064af421
BP
366 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
367 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
368
369 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
370 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
371}
372
373#define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
374#define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
659586ef 375#define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
7525e578 376#define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
064af421 377#define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
66642cb4 378#define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
064af421
BP
379#define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
380#define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
381#define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
382#define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
383#define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
384#define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
2486e66a 385#define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
064af421
BP
386
387#define N_FLOW_VALUES (N_NW_SRC_VALUES * \
388 N_NW_DST_VALUES * \
659586ef 389 N_TUN_ID_VALUES * \
064af421 390 N_IN_PORT_VALUES * \
66642cb4 391 N_VLAN_TCI_VALUES * \
064af421
BP
392 N_DL_TYPE_VALUES * \
393 N_TP_SRC_VALUES * \
394 N_TP_DST_VALUES * \
395 N_DL_SRC_VALUES * \
396 N_DL_DST_VALUES * \
834377ea 397 N_NW_PROTO_VALUES * \
2486e66a 398 N_NW_DSCP_VALUES)
064af421
BP
399
400static unsigned int
401get_value(unsigned int *x, unsigned n_values)
402{
403 unsigned int rem = *x % n_values;
404 *x /= n_values;
405 return rem;
406}
407
064af421 408static void
3bbe9a1f 409compare_classifiers(struct classifier *cls, size_t n_invisible_rules,
44e0c35d 410 ovs_version_t version, struct tcls *tcls)
064af421 411{
70d3fbe7 412 static const int confidence = 500;
064af421
BP
413 unsigned int i;
414
3bbe9a1f 415 assert(classifier_count(cls) == tcls->n_rules + n_invisible_rules);
70d3fbe7 416 for (i = 0; i < confidence; i++) {
dfea28b3 417 const struct cls_rule *cr0, *cr1, *cr2;
ae412e7d 418 struct flow flow;
476f36e8 419 struct flow_wildcards wc;
064af421 420 unsigned int x;
064af421 421
476f36e8 422 flow_wildcards_init_catchall(&wc);
b028db44 423 x = random_range(N_FLOW_VALUES);
51c14ddd 424 memset(&flow, 0, sizeof flow);
064af421
BP
425 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
426 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
296e07ac 427 flow.tunnel.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
7525e578 428 flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
4e022ec0
AW
429 flow.in_port.ofp_port = in_port_values[get_value(&x,
430 N_IN_PORT_VALUES)];
f0fb825a 431 flow.vlans[0].tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
064af421
BP
432 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
433 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
434 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
74ff3298
JR
435 flow.dl_src = dl_src_values[get_value(&x, N_DL_SRC_VALUES)];
436 flow.dl_dst = dl_dst_values[get_value(&x, N_DL_DST_VALUES)];
064af421 437 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
2486e66a 438 flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
064af421 439
c424c2f7 440 /* This assertion is here to suppress a GCC 4.9 array-bounds warning */
e48eccd1 441 ovs_assert(cls->n_tries <= CLS_MAX_TRIES);
c424c2f7 442
3bbe9a1f 443 cr0 = classifier_lookup(cls, version, &flow, &wc);
3c4486a5
BP
444 cr1 = tcls_lookup(tcls, &flow);
445 assert((cr0 == NULL) == (cr1 == NULL));
446 if (cr0 != NULL) {
447 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
448 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
449
193eb874 450 assert(cls_rule_equal(cr0, cr1));
3c4486a5 451 assert(tr0->aux == tr1->aux);
3bbe9a1f
JR
452
453 /* Make sure the rule should have been visible. */
5e27fe97 454 assert(cls_rule_visible_in_version(cr0, version));
064af421 455 }
3bbe9a1f 456 cr2 = classifier_lookup(cls, version, &flow, NULL);
476f36e8 457 assert(cr2 == cr0);
064af421
BP
458 }
459}
460
064af421
BP
461static void
462destroy_classifier(struct classifier *cls)
463{
78c8df12 464 struct test_rule *rule;
5ecc9d81 465
802f84ff 466 classifier_defer(cls);
de4ad4a2 467 CLS_FOR_EACH (rule, cls_rule, cls) {
46ab60bf
BP
468 classifier_remove_assert(cls, &rule->cls_rule);
469 ovsrcu_postpone(free_rule, rule);
5ecc9d81 470 }
064af421
BP
471 classifier_destroy(cls);
472}
473
fe7cfa5c 474static void
da9cfca6 475pvector_verify(const struct pvector *pvec)
fe7cfa5c
JR
476{
477 void *ptr OVS_UNUSED;
eb391b76 478 int prev_priority = INT_MAX;
fe7cfa5c 479
da9cfca6 480 PVECTOR_FOR_EACH (ptr, pvec) {
eb391b76 481 int priority = cursor__.vector[cursor__.entry_idx].priority;
fe7cfa5c 482 if (priority > prev_priority) {
38c449e0
JR
483 ovs_abort(0, "Priority vector is out of order (%u > %u)",
484 priority, prev_priority);
fe7cfa5c
JR
485 }
486 prev_priority = priority;
487 }
488}
489
f358a2cb
JR
490static unsigned int
491trie_verify(const rcu_trie_ptr *trie, unsigned int ofs, unsigned int n_bits)
492{
493 const struct trie_node *node = ovsrcu_get(struct trie_node *, trie);
494
495 if (node) {
496 assert(node->n_rules == 0 || node->n_bits > 0);
497 ofs += node->n_bits;
498 assert((ofs > 0 || (ofs == 0 && node->n_bits == 0)) && ofs <= n_bits);
499
500 return node->n_rules
501 + trie_verify(&node->edges[0], ofs, n_bits)
502 + trie_verify(&node->edges[1], ofs, n_bits);
503 }
504 return 0;
505}
506
507static void
e48eccd1 508verify_tries(struct classifier *cls)
fccd7c09 509 OVS_NO_THREAD_SAFETY_ANALYSIS
f358a2cb 510{
f358a2cb
JR
511 unsigned int n_rules = 0;
512 int i;
513
514 for (i = 0; i < cls->n_tries; i++) {
515 n_rules += trie_verify(&cls->tries[i].root, 0,
516 cls->tries[i].field->n_bits);
517 }
f358a2cb 518 assert(n_rules <= cls->n_rules);
f358a2cb
JR
519}
520
064af421 521static void
0b4f2078 522check_tables(const struct classifier *cls, int n_tables, int n_rules,
44e0c35d 523 int n_dups, int n_invisible, ovs_version_t version)
fccd7c09 524 OVS_NO_THREAD_SAFETY_ANALYSIS
064af421 525{
03868246 526 const struct cls_subtable *table;
955f579d 527 struct test_rule *test_rule;
064af421 528 int found_tables = 0;
3bbe9a1f 529 int found_tables_with_visible_rules = 0;
064af421 530 int found_rules = 0;
b5d97350 531 int found_dups = 0;
3bbe9a1f
JR
532 int found_invisible = 0;
533 int found_visible_but_removable = 0;
955f579d 534 int found_rules2 = 0;
064af421 535
da9cfca6 536 pvector_verify(&cls->subtables);
e48eccd1 537 CMAP_FOR_EACH (table, cmap_node, &cls->subtables_map) {
627fb667 538 const struct cls_match *head;
eb391b76 539 int max_priority = INT_MIN;
4d935a6b 540 unsigned int max_count = 0;
fe7cfa5c 541 bool found = false;
3bbe9a1f 542 bool found_visible_rules = false;
fe7cfa5c
JR
543 const struct cls_subtable *iter;
544
545 /* Locate the subtable from 'subtables'. */
da9cfca6 546 PVECTOR_FOR_EACH (iter, &cls->subtables) {
fe7cfa5c
JR
547 if (iter == table) {
548 if (found) {
38c449e0
JR
549 ovs_abort(0, "Subtable %p duplicated in 'subtables'.",
550 table);
fe7cfa5c
JR
551 }
552 found = true;
553 }
554 }
555 if (!found) {
38c449e0 556 ovs_abort(0, "Subtable %p not found from 'subtables'.", table);
fe7cfa5c 557 }
b5d97350 558
f2c21402 559 assert(!cmap_is_empty(&table->rules));
f358a2cb 560 assert(trie_verify(&table->ports_trie, 0, table->ports_mask_len)
f47eef15 561 == (table->ports_mask_len ? cmap_count(&table->rules) : 0));
f358a2cb 562
064af421 563 found_tables++;
3bbe9a1f 564
f2c21402 565 CMAP_FOR_EACH (head, cmap_node, &table->rules) {
eb391b76 566 int prev_priority = INT_MAX;
44e0c35d 567 ovs_version_t prev_version = 0;
3bbe9a1f
JR
568 const struct cls_match *rule, *prev;
569 bool found_visible_rules_in_list = false;
570
571 assert(head->priority <= table->max_priority);
b5d97350 572
4d935a6b
JR
573 if (head->priority > max_priority) {
574 max_priority = head->priority;
3bbe9a1f 575 max_count = 0;
4d935a6b
JR
576 }
577
3bbe9a1f 578 FOR_EACH_RULE_IN_LIST_PROTECTED(rule, prev, head) {
44e0c35d 579 ovs_version_t rule_version;
3bbe9a1f
JR
580 const struct cls_rule *found_rule;
581
582 /* Priority may not increase. */
583 assert(rule->priority <= prev_priority);
584
585 if (rule->priority == max_priority) {
586 ++max_count;
587 }
588
589 /* Count invisible rules and visible duplicates. */
590 if (!cls_match_visible_in_version(rule, version)) {
591 found_invisible++;
592 } else {
593 if (cls_match_is_eventually_invisible(rule)) {
594 found_visible_but_removable++;
595 }
596 if (found_visible_rules_in_list) {
597 found_dups++;
598 }
599 found_visible_rules_in_list = true;
600 found_visible_rules = true;
601 }
602
603 /* Rule must be visible in the version it was inserted. */
44e0c35d 604 rule_version = rule->versions.add_version;
3bbe9a1f
JR
605 assert(cls_match_visible_in_version(rule, rule_version));
606
607 /* We should always find the latest version of the rule,
608 * unless all rules have been marked for removal.
609 * Later versions must always be later in the list. */
bd53aa17
JR
610 found_rule = classifier_find_rule_exactly(cls, rule->cls_rule,
611 rule_version);
3bbe9a1f 612 if (found_rule && found_rule != rule->cls_rule) {
5e27fe97
JR
613 struct cls_match *cls_match;
614 cls_match = get_cls_match_protected(found_rule);
3bbe9a1f
JR
615
616 assert(found_rule->priority == rule->priority);
617
618 /* Found rule may not have a lower version. */
44e0c35d 619 assert(cls_match->versions.add_version >= rule_version);
3bbe9a1f
JR
620
621 /* This rule must not be visible in the found rule's
622 * version. */
bd53aa17 623 assert(!cls_match_visible_in_version(
44e0c35d 624 rule, cls_match->versions.add_version));
3bbe9a1f
JR
625 }
626
627 if (rule->priority == prev_priority) {
628 /* Exact duplicate rule may not have a lower version. */
629 assert(rule_version >= prev_version);
630
631 /* Previous rule must not be visible in rule's version. */
632 assert(!cls_match_visible_in_version(prev, rule_version));
633 }
4d935a6b 634
b5d97350 635 prev_priority = rule->priority;
3bbe9a1f 636 prev_version = rule_version;
b5d97350 637 found_rules++;
b5d97350
BP
638 }
639 }
3bbe9a1f
JR
640
641 if (found_visible_rules) {
642 found_tables_with_visible_rules++;
643 }
644
4d935a6b
JR
645 assert(table->max_priority == max_priority);
646 assert(table->max_count == max_count);
064af421
BP
647 }
648
e48eccd1 649 assert(found_tables == cmap_count(&cls->subtables_map));
da9cfca6 650 assert(found_tables == pvector_count(&cls->subtables));
3bbe9a1f
JR
651 assert(n_tables == -1 || n_tables == found_tables_with_visible_rules);
652 assert(n_rules == -1 || found_rules == n_rules + found_invisible);
b5d97350 653 assert(n_dups == -1 || found_dups == n_dups);
3bbe9a1f 654 assert(found_invisible == n_invisible);
955f579d 655
5f0476ce 656 CLS_FOR_EACH (test_rule, cls_rule, cls) {
955f579d
BP
657 found_rules2++;
658 }
3bbe9a1f
JR
659 /* Iteration does not see removable rules. */
660 assert(found_rules
661 == found_rules2 + found_visible_but_removable + found_invisible);
064af421
BP
662}
663
664static struct test_rule *
bd53aa17 665make_rule(int wc_fields, int priority, int value_pat)
064af421
BP
666{
667 const struct cls_field *f;
668 struct test_rule *rule;
81a76618 669 struct match match;
064af421 670
81a76618 671 match_init_catchall(&match);
064af421
BP
672 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
673 int f_idx = f - cls_fields;
d8ae4d67 674 int value_idx = (value_pat & (1u << f_idx)) != 0;
81a76618 675 memcpy((char *) &match.flow + f->ofs,
d8ae4d67
BP
676 values[f_idx][value_idx], f->len);
677
0bdc4bec 678 if (f_idx == CLS_F_IDX_NW_SRC) {
b8266395 679 match.wc.masks.nw_src = OVS_BE32_MAX;
d8ae4d67 680 } else if (f_idx == CLS_F_IDX_NW_DST) {
b8266395 681 match.wc.masks.nw_dst = OVS_BE32_MAX;
73f33563 682 } else if (f_idx == CLS_F_IDX_TP_SRC) {
b8266395 683 match.wc.masks.tp_src = OVS_BE16_MAX;
73f33563 684 } else if (f_idx == CLS_F_IDX_TP_DST) {
b8266395 685 match.wc.masks.tp_dst = OVS_BE16_MAX;
73c0ce34 686 } else if (f_idx == CLS_F_IDX_DL_SRC) {
74ff3298 687 WC_MASK_FIELD(&match.wc, dl_src);
73c0ce34 688 } else if (f_idx == CLS_F_IDX_DL_DST) {
74ff3298 689 WC_MASK_FIELD(&match.wc, dl_dst);
66642cb4 690 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
f0fb825a 691 match.wc.masks.vlans[0].tci = OVS_BE16_MAX;
8368c090 692 } else if (f_idx == CLS_F_IDX_TUN_ID) {
b8266395 693 match.wc.masks.tunnel.tun_id = OVS_BE64_MAX;
7525e578 694 } else if (f_idx == CLS_F_IDX_METADATA) {
b8266395 695 match.wc.masks.metadata = OVS_BE64_MAX;
5d9499c4 696 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
81a76618 697 match.wc.masks.nw_tos |= IP_DSCP_MASK;
851d3105 698 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
81a76618 699 match.wc.masks.nw_proto = UINT8_MAX;
e2170cff 700 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
b8266395 701 match.wc.masks.dl_type = OVS_BE16_MAX;
0bdc4bec 702 } else if (f_idx == CLS_F_IDX_IN_PORT) {
e2711da9 703 match.wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
064af421 704 } else {
428b2edd 705 OVS_NOT_REACHED();
064af421
BP
706 }
707 }
81a76618
BP
708
709 rule = xzalloc(sizeof *rule);
712b4d24 710 cls_rule_init(&rule->cls_rule, &match, wc_fields
3bbe9a1f
JR
711 ? (priority == INT_MIN ? priority + 1 :
712 priority == INT_MAX ? priority - 1 : priority)
bd53aa17 713 : 0);
064af421
BP
714 return rule;
715}
716
48d28ac1
BP
717static struct test_rule *
718clone_rule(const struct test_rule *src)
719{
720 struct test_rule *dst;
721
722 dst = xmalloc(sizeof *dst);
723 dst->aux = src->aux;
724 cls_rule_clone(&dst->cls_rule, &src->cls_rule);
725 return dst;
726}
727
728static void
729free_rule(struct test_rule *rule)
730{
731 cls_rule_destroy(&rule->cls_rule);
732 free(rule);
733}
734
064af421 735static void
eb391b76 736shuffle(int *p, size_t n)
064af421
BP
737{
738 for (; n > 1; n--, p++) {
eb391b76
BP
739 int *q = &p[random_range(n)];
740 int tmp = *p;
064af421
BP
741 *p = *q;
742 *q = tmp;
743 }
744}
5cb7a798
BP
745
746static void
747shuffle_u32s(uint32_t *p, size_t n)
748{
749 for (; n > 1; n--, p++) {
b028db44 750 uint32_t *q = &p[random_range(n)];
5cb7a798
BP
751 uint32_t tmp = *p;
752 *p = *q;
753 *q = tmp;
754 }
755}
064af421 756\f
5cb7a798
BP
757/* Classifier tests. */
758
13751fd8
JR
759static enum mf_field_id trie_fields[2] = {
760 MFF_IPV4_DST, MFF_IPV4_SRC
761};
762
f358a2cb
JR
763static void
764set_prefix_fields(struct classifier *cls)
f358a2cb
JR
765{
766 verify_tries(cls);
767 classifier_set_prefix_fields(cls, trie_fields, ARRAY_SIZE(trie_fields));
768 verify_tries(cls);
769}
770
064af421
BP
771/* Tests an empty classifier. */
772static void
1636c761 773test_empty(struct ovs_cmdl_context *ctx OVS_UNUSED)
064af421
BP
774{
775 struct classifier cls;
776 struct tcls tcls;
777
d70e8c28 778 classifier_init(&cls, flow_segment_u64s);
f358a2cb 779 set_prefix_fields(&cls);
064af421
BP
780 tcls_init(&tcls);
781 assert(classifier_is_empty(&cls));
782 assert(tcls_is_empty(&tcls));
44e0c35d 783 compare_classifiers(&cls, 0, OVS_VERSION_MIN, &tcls);
064af421
BP
784 classifier_destroy(&cls);
785 tcls_destroy(&tcls);
786}
787
788/* Destroys a null classifier. */
789static void
1636c761 790test_destroy_null(struct ovs_cmdl_context *ctx OVS_UNUSED)
064af421
BP
791{
792 classifier_destroy(NULL);
793}
794
795/* Tests classification with one rule at a time. */
796static void
1636c761 797test_single_rule(struct ovs_cmdl_context *ctx OVS_UNUSED)
064af421
BP
798{
799 unsigned int wc_fields; /* Hilarious. */
800
801 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
802 struct classifier cls;
803 struct test_rule *rule, *tcls_rule;
804 struct tcls tcls;
805
806 rule = make_rule(wc_fields,
bd53aa17 807 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
d70e8c28 808 classifier_init(&cls, flow_segment_u64s);
f358a2cb 809 set_prefix_fields(&cls);
064af421 810 tcls_init(&tcls);
064af421 811 tcls_rule = tcls_insert(&tcls, rule);
3bbe9a1f 812
44e0c35d
JR
813 classifier_insert(&cls, &rule->cls_rule, OVS_VERSION_MIN, NULL, 0);
814 compare_classifiers(&cls, 0, OVS_VERSION_MIN, &tcls);
815 check_tables(&cls, 1, 1, 0, 0, OVS_VERSION_MIN);
064af421 816
46ab60bf 817 classifier_remove_assert(&cls, &rule->cls_rule);
064af421
BP
818 tcls_remove(&tcls, tcls_rule);
819 assert(classifier_is_empty(&cls));
820 assert(tcls_is_empty(&tcls));
44e0c35d 821 compare_classifiers(&cls, 0, OVS_VERSION_MIN, &tcls);
064af421 822
42767cce 823 ovsrcu_postpone(free_rule, rule);
064af421
BP
824 classifier_destroy(&cls);
825 tcls_destroy(&tcls);
826 }
827}
828
829/* Tests replacing one rule by another. */
830static void
1636c761 831test_rule_replacement(struct ovs_cmdl_context *ctx OVS_UNUSED)
064af421
BP
832{
833 unsigned int wc_fields;
834
835 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
836 struct classifier cls;
f0f4410a
BP
837 struct test_rule *rule1;
838 struct test_rule *rule2;
064af421
BP
839 struct tcls tcls;
840
bd53aa17
JR
841 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
842 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
064af421
BP
843 rule2->aux += 5;
844 rule2->aux += 5;
845
d70e8c28 846 classifier_init(&cls, flow_segment_u64s);
f358a2cb 847 set_prefix_fields(&cls);
064af421 848 tcls_init(&tcls);
f0f4410a 849 tcls_insert(&tcls, rule1);
44e0c35d
JR
850 classifier_insert(&cls, &rule1->cls_rule, OVS_VERSION_MIN, NULL, 0);
851 compare_classifiers(&cls, 0, OVS_VERSION_MIN, &tcls);
852 check_tables(&cls, 1, 1, 0, 0, OVS_VERSION_MIN);
064af421
BP
853 tcls_destroy(&tcls);
854
855 tcls_init(&tcls);
f0f4410a 856 tcls_insert(&tcls, rule2);
5f0476ce 857
064af421 858 assert(test_rule_from_cls_rule(
44e0c35d 859 classifier_replace(&cls, &rule2->cls_rule, OVS_VERSION_MIN,
18080541 860 NULL, 0)) == rule1);
42767cce 861 ovsrcu_postpone(free_rule, rule1);
44e0c35d
JR
862 compare_classifiers(&cls, 0, OVS_VERSION_MIN, &tcls);
863 check_tables(&cls, 1, 1, 0, 0, OVS_VERSION_MIN);
802f84ff 864 classifier_defer(&cls);
46ab60bf 865 classifier_remove_assert(&cls, &rule2->cls_rule);
5f0476ce
JR
866
867 tcls_destroy(&tcls);
064af421
BP
868 destroy_classifier(&cls);
869 }
870}
871
872static int
b5d97350 873factorial(int n_items)
064af421 874{
b5d97350
BP
875 int n, i;
876
877 n = 1;
878 for (i = 2; i <= n_items; i++) {
879 n *= i;
880 }
881 return n;
064af421
BP
882}
883
b5d97350
BP
884static void
885swap(int *a, int *b)
064af421 886{
b5d97350
BP
887 int tmp = *a;
888 *a = *b;
889 *b = tmp;
064af421
BP
890}
891
064af421 892static void
b5d97350 893reverse(int *a, int n)
064af421 894{
b5d97350 895 int i;
064af421 896
b5d97350
BP
897 for (i = 0; i < n / 2; i++) {
898 int j = n - (i + 1);
899 swap(&a[i], &a[j]);
900 }
901}
064af421 902
b5d97350
BP
903static bool
904next_permutation(int *a, int n)
905{
906 int k;
064af421 907
b5d97350
BP
908 for (k = n - 2; k >= 0; k--) {
909 if (a[k] < a[k + 1]) {
910 int l;
064af421 911
b5d97350
BP
912 for (l = n - 1; ; l--) {
913 if (a[l] > a[k]) {
914 swap(&a[k], &a[l]);
915 reverse(a + (k + 1), n - (k + 1));
916 return true;
064af421
BP
917 }
918 }
919 }
920 }
b5d97350 921 return false;
064af421
BP
922}
923
b5d97350 924/* Tests classification with rules that have the same matching criteria. */
064af421 925static void
1636c761 926test_many_rules_in_one_list (struct ovs_cmdl_context *ctx OVS_UNUSED)
b5d97350
BP
927{
928 enum { N_RULES = 3 };
929 int n_pris;
064af421 930
b5d97350
BP
931 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
932 int ops[N_RULES * 2];
933 int pris[N_RULES];
934 int n_permutations;
935 int i;
064af421 936
b5d97350
BP
937 pris[0] = 0;
938 for (i = 1; i < N_RULES; i++) {
939 pris[i] = pris[i - 1] + (n_pris > i);
940 }
064af421 941
b5d97350
BP
942 for (i = 0; i < N_RULES * 2; i++) {
943 ops[i] = i / 2;
064af421 944 }
064af421 945
b5d97350
BP
946 n_permutations = 0;
947 do {
948 struct test_rule *rules[N_RULES];
949 struct test_rule *tcls_rules[N_RULES];
950 int pri_rules[N_RULES];
951 struct classifier cls;
952 struct tcls tcls;
44e0c35d 953 ovs_version_t version = OVS_VERSION_MIN;
3bbe9a1f 954 size_t n_invisible_rules = 0;
064af421 955
b5d97350 956 n_permutations++;
064af421 957
b5d97350 958 for (i = 0; i < N_RULES; i++) {
bd53aa17 959 rules[i] = make_rule(456, pris[i], 0);
b5d97350
BP
960 tcls_rules[i] = NULL;
961 pri_rules[i] = -1;
962 }
064af421 963
d70e8c28 964 classifier_init(&cls, flow_segment_u64s);
f358a2cb 965 set_prefix_fields(&cls);
b5d97350 966 tcls_init(&tcls);
064af421 967
b5d97350 968 for (i = 0; i < ARRAY_SIZE(ops); i++) {
3bbe9a1f
JR
969 struct test_rule *displaced_rule = NULL;
970 struct cls_rule *removable_rule = NULL;
b5d97350
BP
971 int j = ops[i];
972 int m, n;
064af421 973
b5d97350 974 if (!tcls_rules[j]) {
b5d97350 975 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
3bbe9a1f
JR
976 if (versioned) {
977 /* Insert the new rule in the next version. */
bd53aa17 978 ++version;
3bbe9a1f
JR
979
980 displaced_rule = test_rule_from_cls_rule(
981 classifier_find_rule_exactly(&cls,
bd53aa17
JR
982 &rules[j]->cls_rule,
983 version));
3bbe9a1f
JR
984 if (displaced_rule) {
985 /* Mark the old rule for removal after the current
986 * version. */
987 cls_rule_make_invisible_in_version(
18721c4a 988 &displaced_rule->cls_rule, version);
3bbe9a1f
JR
989 n_invisible_rules++;
990 removable_rule = &displaced_rule->cls_rule;
991 }
bd53aa17
JR
992 classifier_insert(&cls, &rules[j]->cls_rule, version,
993 NULL, 0);
3bbe9a1f
JR
994 } else {
995 displaced_rule = test_rule_from_cls_rule(
996 classifier_replace(&cls, &rules[j]->cls_rule,
bd53aa17 997 version, NULL, 0));
3bbe9a1f 998 }
b5d97350
BP
999 if (pri_rules[pris[j]] >= 0) {
1000 int k = pri_rules[pris[j]];
1001 assert(displaced_rule != NULL);
1002 assert(displaced_rule != rules[j]);
1003 assert(pris[j] == displaced_rule->cls_rule.priority);
1004 tcls_rules[k] = NULL;
1005 } else {
1006 assert(displaced_rule == NULL);
1007 }
1008 pri_rules[pris[j]] = j;
1009 } else {
3bbe9a1f
JR
1010 if (versioned) {
1011 /* Mark the rule for removal after the current
1012 * version. */
3bbe9a1f 1013 ++version;
bd53aa17
JR
1014 cls_rule_make_invisible_in_version(
1015 &rules[j]->cls_rule, version);
3bbe9a1f
JR
1016 n_invisible_rules++;
1017 removable_rule = &rules[j]->cls_rule;
1018 } else {
46ab60bf 1019 classifier_remove_assert(&cls, &rules[j]->cls_rule);
3bbe9a1f 1020 }
b5d97350
BP
1021 tcls_remove(&tcls, tcls_rules[j]);
1022 tcls_rules[j] = NULL;
1023 pri_rules[pris[j]] = -1;
1024 }
3bbe9a1f 1025 compare_classifiers(&cls, n_invisible_rules, version, &tcls);
b5d97350
BP
1026 n = 0;
1027 for (m = 0; m < N_RULES; m++) {
1028 n += tcls_rules[m] != NULL;
064af421 1029 }
3bbe9a1f
JR
1030 check_tables(&cls, n > 0, n, n - 1, n_invisible_rules,
1031 version);
1032
1033 if (versioned && removable_rule) {
5e27fe97
JR
1034 struct cls_match *cls_match =
1035 get_cls_match_protected(removable_rule);
1036
3bbe9a1f 1037 /* Removable rule is no longer visible. */
5e27fe97
JR
1038 assert(cls_match);
1039 assert(!cls_match_visible_in_version(cls_match, version));
46ab60bf 1040 classifier_remove_assert(&cls, removable_rule);
3bbe9a1f
JR
1041 n_invisible_rules--;
1042 }
064af421 1043 }
b5d97350 1044
802f84ff 1045 classifier_defer(&cls);
b5d97350 1046 for (i = 0; i < N_RULES; i++) {
42767cce
JR
1047 if (classifier_remove(&cls, &rules[i]->cls_rule)) {
1048 ovsrcu_postpone(free_rule, rules[i]);
627fb667 1049 }
b5d97350 1050 }
627fb667
JR
1051 classifier_destroy(&cls);
1052 tcls_destroy(&tcls);
b5d97350
BP
1053 } while (next_permutation(ops, ARRAY_SIZE(ops)));
1054 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
064af421
BP
1055 }
1056}
1057
b5d97350
BP
1058static int
1059count_ones(unsigned long int x)
064af421 1060{
b5d97350 1061 int n = 0;
064af421 1062
b5d97350 1063 while (x) {
8472a3ce 1064 x = zero_rightmost_1bit(x);
b5d97350
BP
1065 n++;
1066 }
064af421 1067
b5d97350
BP
1068 return n;
1069}
064af421 1070
b5d97350
BP
1071static bool
1072array_contains(int *array, int n, int value)
1073{
1074 int i;
064af421 1075
b5d97350
BP
1076 for (i = 0; i < n; i++) {
1077 if (array[i] == value) {
1078 return true;
064af421
BP
1079 }
1080 }
b5d97350
BP
1081
1082 return false;
064af421
BP
1083}
1084
b5d97350
BP
1085/* Tests classification with two rules at a time that fall into the same
1086 * table but different lists. */
064af421 1087static void
1636c761 1088test_many_rules_in_one_table(struct ovs_cmdl_context *ctx OVS_UNUSED)
064af421 1089{
b5d97350 1090 int iteration;
064af421 1091
b5d97350
BP
1092 for (iteration = 0; iteration < 50; iteration++) {
1093 enum { N_RULES = 20 };
1094 struct test_rule *rules[N_RULES];
1095 struct test_rule *tcls_rules[N_RULES];
1096 struct classifier cls;
1097 struct tcls tcls;
44e0c35d 1098 ovs_version_t version = OVS_VERSION_MIN;
3bbe9a1f 1099 size_t n_invisible_rules = 0;
b5d97350
BP
1100 int value_pats[N_RULES];
1101 int value_mask;
1102 int wcf;
1103 int i;
064af421 1104
b5d97350 1105 do {
b028db44 1106 wcf = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
b5d97350
BP
1107 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
1108 } while ((1 << count_ones(value_mask)) < N_RULES);
064af421 1109
d70e8c28 1110 classifier_init(&cls, flow_segment_u64s);
f358a2cb 1111 set_prefix_fields(&cls);
b5d97350 1112 tcls_init(&tcls);
064af421 1113
b5d97350 1114 for (i = 0; i < N_RULES; i++) {
eb391b76 1115 int priority = random_range(INT_MAX);
064af421 1116
b5d97350 1117 do {
b028db44 1118 value_pats[i] = random_uint32() & value_mask;
b5d97350 1119 } while (array_contains(value_pats, i, value_pats[i]));
064af421 1120
3bbe9a1f 1121 ++version;
bd53aa17 1122 rules[i] = make_rule(wcf, priority, value_pats[i]);
b5d97350 1123 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
5f0476ce 1124
bd53aa17 1125 classifier_insert(&cls, &rules[i]->cls_rule, version, NULL, 0);
3bbe9a1f 1126 compare_classifiers(&cls, n_invisible_rules, version, &tcls);
b5d97350 1127
3bbe9a1f 1128 check_tables(&cls, 1, i + 1, 0, n_invisible_rules, version);
b5d97350
BP
1129 }
1130
1131 for (i = 0; i < N_RULES; i++) {
1132 tcls_remove(&tcls, tcls_rules[i]);
3bbe9a1f
JR
1133 if (versioned) {
1134 /* Mark the rule for removal after the current version. */
3bbe9a1f 1135 ++version;
bd53aa17
JR
1136 cls_rule_make_invisible_in_version(&rules[i]->cls_rule,
1137 version);
3bbe9a1f
JR
1138 n_invisible_rules++;
1139 } else {
46ab60bf 1140 classifier_remove_assert(&cls, &rules[i]->cls_rule);
3bbe9a1f
JR
1141 }
1142 compare_classifiers(&cls, n_invisible_rules, version, &tcls);
1143 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0,
1144 n_invisible_rules, version);
1145 if (!versioned) {
1146 ovsrcu_postpone(free_rule, rules[i]);
1147 }
1148 }
b5d97350 1149
3bbe9a1f
JR
1150 if (versioned) {
1151 for (i = 0; i < N_RULES; i++) {
46ab60bf 1152 classifier_remove_assert(&cls, &rules[i]->cls_rule);
3bbe9a1f
JR
1153 n_invisible_rules--;
1154
1155 compare_classifiers(&cls, n_invisible_rules, version, &tcls);
1156 check_tables(&cls, 0, 0, 0, n_invisible_rules, version);
1157 ovsrcu_postpone(free_rule, rules[i]);
1158 }
064af421 1159 }
b5d97350
BP
1160
1161 classifier_destroy(&cls);
1162 tcls_destroy(&tcls);
064af421
BP
1163 }
1164}
1165
b5d97350
BP
1166/* Tests classification with many rules at a time that fall into random lists
1167 * in 'n' tables. */
064af421 1168static void
b5d97350 1169test_many_rules_in_n_tables(int n_tables)
064af421
BP
1170{
1171 enum { MAX_RULES = 50 };
b5d97350 1172 int wcfs[10];
064af421 1173 int iteration;
b5d97350
BP
1174 int i;
1175
1176 assert(n_tables < 10);
1177 for (i = 0; i < n_tables; i++) {
1178 do {
b028db44 1179 wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
b5d97350
BP
1180 } while (array_contains(wcfs, i, wcfs[i]));
1181 }
064af421
BP
1182
1183 for (iteration = 0; iteration < 30; iteration++) {
eb391b76 1184 int priorities[MAX_RULES];
064af421
BP
1185 struct classifier cls;
1186 struct tcls tcls;
44e0c35d 1187 ovs_version_t version = OVS_VERSION_MIN;
3bbe9a1f
JR
1188 size_t n_invisible_rules = 0;
1189 struct ovs_list list = OVS_LIST_INITIALIZER(&list);
064af421 1190
b028db44 1191 random_set_seed(iteration + 1);
064af421 1192 for (i = 0; i < MAX_RULES; i++) {
eb391b76 1193 priorities[i] = (i * 129) & INT_MAX;
064af421
BP
1194 }
1195 shuffle(priorities, ARRAY_SIZE(priorities));
1196
d70e8c28 1197 classifier_init(&cls, flow_segment_u64s);
f358a2cb 1198 set_prefix_fields(&cls);
064af421
BP
1199 tcls_init(&tcls);
1200
1201 for (i = 0; i < MAX_RULES; i++) {
1202 struct test_rule *rule;
eb391b76 1203 int priority = priorities[i];
b028db44
BP
1204 int wcf = wcfs[random_range(n_tables)];
1205 int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
bd53aa17 1206 rule = make_rule(wcf, priority, value_pat);
064af421 1207 tcls_insert(&tcls, rule);
bd53aa17 1208 classifier_insert(&cls, &rule->cls_rule, version, NULL, 0);
3bbe9a1f
JR
1209 compare_classifiers(&cls, n_invisible_rules, version, &tcls);
1210 check_tables(&cls, -1, i + 1, -1, n_invisible_rules, version);
064af421
BP
1211 }
1212
3bbe9a1f 1213 while (classifier_count(&cls) - n_invisible_rules > 0) {
5ecc9d81 1214 struct test_rule *target;
78c8df12 1215 struct test_rule *rule;
3bbe9a1f 1216 size_t n_removable_rules = 0;
5ecc9d81 1217
b028db44 1218 target = clone_rule(tcls.rules[random_range(tcls.n_rules)]);
5ecc9d81 1219
bd53aa17
JR
1220 CLS_FOR_EACH_TARGET (rule, cls_rule, &cls, &target->cls_rule,
1221 version) {
3bbe9a1f
JR
1222 if (versioned) {
1223 /* Mark the rule for removal after the current version. */
1224 cls_rule_make_invisible_in_version(&rule->cls_rule,
18721c4a 1225 version + 1);
3bbe9a1f
JR
1226 n_removable_rules++;
1227 compare_classifiers(&cls, n_invisible_rules, version,
1228 &tcls);
1229 check_tables(&cls, -1, -1, -1, n_invisible_rules, version);
1230
417e7e66 1231 ovs_list_push_back(&list, &rule->list_node);
3bbe9a1f 1232 } else if (classifier_remove(&cls, &rule->cls_rule)) {
42767cce
JR
1233 ovsrcu_postpone(free_rule, rule);
1234 }
5ecc9d81 1235 }
5f0476ce 1236
3bbe9a1f
JR
1237 ++version;
1238 n_invisible_rules += n_removable_rules;
1239
5ecc9d81 1240 tcls_delete_matches(&tcls, &target->cls_rule);
48d28ac1 1241 free_rule(target);
3bbe9a1f
JR
1242
1243 compare_classifiers(&cls, n_invisible_rules, version, &tcls);
1244 check_tables(&cls, -1, -1, -1, n_invisible_rules, version);
1245 }
1246 if (versioned) {
1247 struct test_rule *rule;
1248
1249 /* Remove rules that are no longer visible. */
1250 LIST_FOR_EACH_POP (rule, list_node, &list) {
46ab60bf 1251 classifier_remove_assert(&cls, &rule->cls_rule);
3bbe9a1f
JR
1252 n_invisible_rules--;
1253
1254 compare_classifiers(&cls, n_invisible_rules, version,
1255 &tcls);
1256 check_tables(&cls, -1, -1, -1, n_invisible_rules, version);
1257 }
064af421 1258 }
064af421
BP
1259
1260 destroy_classifier(&cls);
1261 tcls_destroy(&tcls);
1262 }
1263}
b5d97350
BP
1264
1265static void
1636c761 1266test_many_rules_in_two_tables(struct ovs_cmdl_context *ctx OVS_UNUSED)
b5d97350
BP
1267{
1268 test_many_rules_in_n_tables(2);
1269}
1270
1271static void
1636c761 1272test_many_rules_in_five_tables(struct ovs_cmdl_context *ctx OVS_UNUSED)
b5d97350
BP
1273{
1274 test_many_rules_in_n_tables(5);
1275}
064af421 1276\f
1add079e
JR
1277/* Classifier benchmarks. */
1278
1279static int n_rules; /* Number of rules to insert. */
1280static int n_priorities; /* Number of priorities to use. */
1281static int n_tables; /* Number of subtables. */
1282static int n_threads; /* Number of threads to search and mutate. */
1283static int n_lookups; /* Number of lookups each thread performs. */
1284
1285static void benchmark(bool use_wc);
1286
1287static int
1288elapsed(const struct timeval *start)
1289{
1290 struct timeval end;
1291
1292 xgettimeofday(&end);
1293 return timeval_to_msec(&end) - timeval_to_msec(start);
1294}
1295
1296static void
1297run_benchmarks(struct ovs_cmdl_context *ctx)
1298{
1299 if (ctx->argc < 5
1300 || (ctx->argc > 1 && !strcmp(ctx->argv[1], "--help"))) {
1301 printf(
1302 "usage: ovstest %s benchmark <n_rules> <n_priorities> <n_subtables> <n_threads> <n_lookups>\n"
1303 "\n"
1304 "where:\n"
1305 "\n"
1306 "<n_rules> - The number of rules to install for lookups. More rules\n"
1307 " makes misses less likely.\n"
1308 "<n_priorities> - How many different priorities to use. Using only 1\n"
1309 " priority will force lookups to continue through all\n"
1310 " subtables.\n"
1311 "<n_subtables> - Number of subtables to use. Normally a classifier has\n"
1312 " rules with different kinds of masks, resulting in\n"
1313 " multiple subtables (one per mask). However, in some\n"
1314 " special cases a table may consist of only one kind of\n"
1315 " rules, so there will be only one subtable.\n"
1316 "<n_threads> - How many lookup threads to use. Using one thread should\n"
1317 " give less variance accross runs, but classifier\n"
1318 " scaling can be tested with multiple threads.\n"
1319 "<n_lookups> - How many lookups each thread should perform.\n"
1320 "\n", program_name);
1321 return;
1322 }
1323
1324 n_rules = strtol(ctx->argv[1], NULL, 10);
1325 n_priorities = strtol(ctx->argv[2], NULL, 10);
1326 n_tables = strtol(ctx->argv[3], NULL, 10);
1327 n_threads = strtol(ctx->argv[4], NULL, 10);
1328 n_lookups = strtol(ctx->argv[5], NULL, 10);
1329
1330 printf("\nBenchmarking with:\n"
1331 "%d rules with %d priorities in %d tables, "
1332 "%d threads doing %d lookups each\n",
1333 n_rules, n_priorities, n_tables, n_threads, n_lookups);
1334
1335 puts("\nWithout wildcards: \n");
1336 benchmark(false);
1337 puts("\nWith wildcards: \n");
1338 benchmark(true);
1339}
1340
1341struct cls_aux {
1342 const struct classifier *cls;
1343 size_t n_lookup_flows;
1344 struct flow *lookup_flows;
1345 bool use_wc;
1346 atomic_int hits;
1347 atomic_int misses;
1348};
1349
1350static void *
1351lookup_classifier(void *aux_)
1352{
1353 struct cls_aux *aux = aux_;
44e0c35d 1354 ovs_version_t version = OVS_VERSION_MIN;
1add079e
JR
1355 int hits = 0, old_hits;
1356 int misses = 0, old_misses;
1357 size_t i;
1358
1359 random_set_seed(1);
1360
1361 for (i = 0; i < n_lookups; i++) {
1362 const struct cls_rule *cr;
1363 struct flow_wildcards wc;
1364 unsigned int x;
1365
1366 x = random_range(aux->n_lookup_flows);
1367
1368 if (aux->use_wc) {
1369 flow_wildcards_init_catchall(&wc);
1370 cr = classifier_lookup(aux->cls, version, &aux->lookup_flows[x],
1371 &wc);
1372 } else {
1373 cr = classifier_lookup(aux->cls, version, &aux->lookup_flows[x],
1374 NULL);
1375 }
1376 if (cr) {
1377 hits++;
1378 } else {
1379 misses++;
1380 }
1381 }
1382 atomic_add(&aux->hits, hits, &old_hits);
1383 atomic_add(&aux->misses, misses, &old_misses);
1384 return NULL;
1385}
1386
1387/* Benchmark classification. */
1388static void
1389benchmark(bool use_wc)
1390{
1391 struct classifier cls;
44e0c35d 1392 ovs_version_t version = OVS_VERSION_MIN;
1add079e
JR
1393 struct cls_aux aux;
1394 int *wcfs = xmalloc(n_tables * sizeof *wcfs);
1395 int *priorities = xmalloc(n_priorities * sizeof *priorities);
1396 struct timeval start;
1397 pthread_t *threads;
1398 int i;
1399
1400 fatal_signal_init();
1401
1402 random_set_seed(1);
1403
1404 for (i = 0; i < n_tables; i++) {
1405 do {
1406 wcfs[i] = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1407 } while (array_contains(wcfs, i, wcfs[i]));
1408 }
1409
1410 for (i = 0; i < n_priorities; i++) {
1411 priorities[i] = (i * 129) & INT_MAX;
1412 }
1413 shuffle(priorities, n_priorities);
1414
1415 classifier_init(&cls, flow_segment_u64s);
1416 set_prefix_fields(&cls);
1417
1418 /* Create lookup flows. */
1419 aux.use_wc = use_wc;
1420 aux.cls = &cls;
1421 aux.n_lookup_flows = 2 * N_FLOW_VALUES;
1422 aux.lookup_flows = xzalloc(aux.n_lookup_flows * sizeof *aux.lookup_flows);
1423 for (i = 0; i < aux.n_lookup_flows; i++) {
1424 struct flow *flow = &aux.lookup_flows[i];
1425 unsigned int x;
1426
1427 x = random_range(N_FLOW_VALUES);
1428 flow->nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
1429 flow->nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
1430 flow->tunnel.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
1431 flow->metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
1432 flow->in_port.ofp_port = in_port_values[get_value(&x,
1433 N_IN_PORT_VALUES)];
f0fb825a 1434 flow->vlans[0].tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
1add079e
JR
1435 flow->dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
1436 flow->tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
1437 flow->tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
74ff3298
JR
1438 flow->dl_src = dl_src_values[get_value(&x, N_DL_SRC_VALUES)];
1439 flow->dl_dst = dl_dst_values[get_value(&x, N_DL_DST_VALUES)];
1add079e
JR
1440 flow->nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
1441 flow->nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
1442 }
1443 atomic_init(&aux.hits, 0);
1444 atomic_init(&aux.misses, 0);
1445
1446 /* Rule insertion. */
1447 for (i = 0; i < n_rules; i++) {
1448 struct test_rule *rule;
1449 const struct cls_rule *old_cr;
1450
1451 int priority = priorities[random_range(n_priorities)];
1452 int wcf = wcfs[random_range(n_tables)];
1453 int value_pat = random_uint32() & ((1u << CLS_N_FIELDS) - 1);
1454
1455 rule = make_rule(wcf, priority, value_pat);
1456 old_cr = classifier_find_rule_exactly(&cls, &rule->cls_rule, version);
1457 if (!old_cr) {
1458 classifier_insert(&cls, &rule->cls_rule, version, NULL, 0);
1459 } else {
1460 free_rule(rule);
1461 }
1462 }
1463
1464 /* Lookup. */
1465 xgettimeofday(&start);
1466 threads = xmalloc(n_threads * sizeof *threads);
1467 for (i = 0; i < n_threads; i++) {
1468 threads[i] = ovs_thread_create("lookups", lookup_classifier, &aux);
1469 }
1470 for (i = 0; i < n_threads; i++) {
1471 xpthread_join(threads[i], NULL);
1472 }
1473
1474 int elapsed_msec = elapsed(&start);
1475
1476 free(threads);
1477
1478 int hits, misses;
1479 atomic_read(&aux.hits, &hits);
1480 atomic_read(&aux.misses, &misses);
1481 printf("hits: %d, misses: %d\n", hits, misses);
1482
1483 printf("classifier lookups: %5d ms, %"PRId64" lookups/sec\n",
1484 elapsed_msec,
1485 (((uint64_t)hits + misses) * 1000) / elapsed_msec);
1486
1487 destroy_classifier(&cls);
1488 free(aux.lookup_flows);
1489 free(priorities);
1490 free(wcfs);
1491}
1492\f
5cb7a798
BP
1493/* Miniflow tests. */
1494
1495static uint32_t
1496random_value(void)
1497{
396d492c 1498 static const uint32_t values_[] =
5cb7a798
BP
1499 { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
1500 0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
1501
396d492c 1502 return values_[random_range(ARRAY_SIZE(values_))];
5cb7a798
BP
1503}
1504
1505static bool
1506choose(unsigned int n, unsigned int *idxp)
1507{
1508 if (*idxp < n) {
1509 return true;
1510 } else {
1511 *idxp -= n;
1512 return false;
1513 }
1514}
1515
d70e8c28
JR
1516#define FLOW_U32S (FLOW_U64S * 2)
1517
5cb7a798
BP
1518static bool
1519init_consecutive_values(int n_consecutive, struct flow *flow,
1520 unsigned int *idxp)
1521{
1522 uint32_t *flow_u32 = (uint32_t *) flow;
1523
1524 if (choose(FLOW_U32S - n_consecutive + 1, idxp)) {
1525 int i;
1526
1527 for (i = 0; i < n_consecutive; i++) {
1528 flow_u32[*idxp + i] = random_value();
1529 }
1530 return true;
1531 } else {
1532 return false;
1533 }
1534}
1535
1536static bool
1537next_random_flow(struct flow *flow, unsigned int idx)
1538{
1539 uint32_t *flow_u32 = (uint32_t *) flow;
5cb7a798
BP
1540
1541 memset(flow, 0, sizeof *flow);
1542
1543 /* Empty flow. */
1544 if (choose(1, &idx)) {
1545 return true;
1546 }
1547
1548 /* All flows with a small number of consecutive nonzero values. */
71f21279 1549 for (int i = 1; i <= 4; i++) {
5cb7a798
BP
1550 if (init_consecutive_values(i, flow, &idx)) {
1551 return true;
1552 }
1553 }
1554
1555 /* All flows with a large number of consecutive nonzero values. */
71f21279 1556 for (int i = FLOW_U32S - 4; i <= FLOW_U32S; i++) {
5cb7a798
BP
1557 if (init_consecutive_values(i, flow, &idx)) {
1558 return true;
1559 }
1560 }
1561
1562 /* All flows with exactly two nonconsecutive nonzero values. */
1563 if (choose((FLOW_U32S - 1) * (FLOW_U32S - 2) / 2, &idx)) {
1564 int ofs1;
1565
1566 for (ofs1 = 0; ofs1 < FLOW_U32S - 2; ofs1++) {
1567 int ofs2;
1568
1569 for (ofs2 = ofs1 + 2; ofs2 < FLOW_U32S; ofs2++) {
1570 if (choose(1, &idx)) {
1571 flow_u32[ofs1] = random_value();
1572 flow_u32[ofs2] = random_value();
1573 return true;
1574 }
1575 }
1576 }
428b2edd 1577 OVS_NOT_REACHED();
5cb7a798
BP
1578 }
1579
1580 /* 16 randomly chosen flows with N >= 3 nonzero values. */
1581 if (choose(16 * (FLOW_U32S - 4), &idx)) {
1582 int n = idx / 16 + 3;
5cb7a798 1583
71f21279 1584 for (int i = 0; i < n; i++) {
5cb7a798
BP
1585 flow_u32[i] = random_value();
1586 }
1587 shuffle_u32s(flow_u32, FLOW_U32S);
1588
1589 return true;
1590 }
1591
1592 return false;
1593}
1594
1595static void
1596any_random_flow(struct flow *flow)
1597{
1598 static unsigned int max;
1599 if (!max) {
1600 while (next_random_flow(flow, max)) {
1601 max++;
1602 }
1603 }
1604
1605 next_random_flow(flow, random_range(max));
1606}
1607
1608static void
1609toggle_masked_flow_bits(struct flow *flow, const struct flow_wildcards *mask)
1610{
1611 const uint32_t *mask_u32 = (const uint32_t *) &mask->masks;
1612 uint32_t *flow_u32 = (uint32_t *) flow;
1613 int i;
1614
1615 for (i = 0; i < FLOW_U32S; i++) {
1616 if (mask_u32[i] != 0) {
1617 uint32_t bit;
1618
1619 do {
1620 bit = 1u << random_range(32);
1621 } while (!(bit & mask_u32[i]));
1622 flow_u32[i] ^= bit;
1623 }
1624 }
1625}
1626
1627static void
1628wildcard_extra_bits(struct flow_wildcards *mask)
1629{
1630 uint32_t *mask_u32 = (uint32_t *) &mask->masks;
1631 int i;
1632
1633 for (i = 0; i < FLOW_U32S; i++) {
1634 if (mask_u32[i] != 0) {
1635 uint32_t bit;
1636
1637 do {
1638 bit = 1u << random_range(32);
1639 } while (!(bit & mask_u32[i]));
1640 mask_u32[i] &= ~bit;
1641 }
1642 }
1643}
1644
a851eb94
JR
1645/* Returns a copy of 'src'. The caller must eventually free the returned
1646 * miniflow with free(). */
1647static struct miniflow *
1648miniflow_clone__(const struct miniflow *src)
1649{
1650 struct miniflow *dst;
1651 size_t data_size;
1652
1653 data_size = miniflow_alloc(&dst, 1, src);
1654 miniflow_clone(dst, src, data_size / sizeof(uint64_t));
1655 return dst;
1656}
1657
b30001c7
JR
1658/* Returns a hash value for 'flow', given 'basis'. */
1659static inline uint32_t
1660miniflow_hash__(const struct miniflow *flow, uint32_t basis)
1661{
5fcff47b
JR
1662 const uint64_t *p = miniflow_get_values(flow);
1663 size_t n_values = miniflow_n_values(flow);
1664 struct flowmap hash_map = FLOWMAP_EMPTY_INITIALIZER;
b30001c7 1665 uint32_t hash = basis;
5fcff47b 1666 size_t idx;
b30001c7 1667
5fcff47b
JR
1668 FLOWMAP_FOR_EACH_INDEX(idx, flow->map) {
1669 uint64_t value = *p++;
1670
1671 if (value) {
1672 hash = hash_add64(hash, value);
1673 flowmap_set(&hash_map, idx, 1);
b30001c7 1674 }
b30001c7 1675 }
5fcff47b
JR
1676 map_t map;
1677 FLOWMAP_FOR_EACH_MAP (map, hash_map) {
1678 hash = hash_add64(hash, map);
b30001c7 1679 }
b30001c7 1680
5fcff47b 1681 return hash_finish(hash, n_values);
b30001c7
JR
1682}
1683
5cb7a798 1684static void
1636c761 1685test_miniflow(struct ovs_cmdl_context *ctx OVS_UNUSED)
5cb7a798
BP
1686{
1687 struct flow flow;
1688 unsigned int idx;
1689
1690 random_set_seed(0xb3faca38);
1691 for (idx = 0; next_random_flow(&flow, idx); idx++) {
d70e8c28 1692 const uint64_t *flow_u64 = (const uint64_t *) &flow;
8fd47924 1693 struct miniflow *miniflow, *miniflow2, *miniflow3;
5cb7a798
BP
1694 struct flow flow2, flow3;
1695 struct flow_wildcards mask;
8fd47924 1696 struct minimask *minimask;
5cb7a798
BP
1697 int i;
1698
1699 /* Convert flow to miniflow. */
8fd47924 1700 miniflow = miniflow_create(&flow);
5cb7a798
BP
1701
1702 /* Check that the flow equals its miniflow. */
f0fb825a
EG
1703 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
1704 assert(miniflow_get_vid(miniflow, i) ==
1705 vlan_tci_to_vid(flow.vlans[i].tci));
1706 }
d70e8c28 1707 for (i = 0; i < FLOW_U64S; i++) {
8fd47924 1708 assert(miniflow_get(miniflow, i) == flow_u64[i]);
5cb7a798
BP
1709 }
1710
1711 /* Check that the miniflow equals itself. */
8fd47924 1712 assert(miniflow_equal(miniflow, miniflow));
5cb7a798
BP
1713
1714 /* Convert miniflow back to flow and verify that it's the same. */
8fd47924 1715 miniflow_expand(miniflow, &flow2);
5cb7a798
BP
1716 assert(flow_equal(&flow, &flow2));
1717
1718 /* Check that copying a miniflow works properly. */
a851eb94 1719 miniflow2 = miniflow_clone__(miniflow);
8fd47924 1720 assert(miniflow_equal(miniflow, miniflow2));
b30001c7 1721 assert(miniflow_hash__(miniflow, 0) == miniflow_hash__(miniflow2, 0));
8fd47924 1722 miniflow_expand(miniflow2, &flow3);
5cb7a798
BP
1723 assert(flow_equal(&flow, &flow3));
1724
1725 /* Check that masked matches work as expected for identical flows and
1726 * miniflows. */
1727 do {
1728 next_random_flow(&mask.masks, 1);
1729 } while (flow_wildcards_is_catchall(&mask));
8fd47924
JR
1730 minimask = minimask_create(&mask);
1731 assert(minimask_is_catchall(minimask)
5cb7a798 1732 == flow_wildcards_is_catchall(&mask));
8fd47924
JR
1733 assert(miniflow_equal_in_minimask(miniflow, miniflow2, minimask));
1734 assert(miniflow_equal_flow_in_minimask(miniflow, &flow2, minimask));
1735 assert(miniflow_hash_in_minimask(miniflow, minimask, 0x12345678) ==
1736 flow_hash_in_minimask(&flow, minimask, 0x12345678));
b30001c7
JR
1737 assert(minimask_hash(minimask, 0) ==
1738 miniflow_hash__(&minimask->masks, 0));
5cb7a798
BP
1739
1740 /* Check that masked matches work as expected for differing flows and
1741 * miniflows. */
1742 toggle_masked_flow_bits(&flow2, &mask);
8fd47924
JR
1743 assert(!miniflow_equal_flow_in_minimask(miniflow, &flow2, minimask));
1744 miniflow3 = miniflow_create(&flow2);
1745 assert(!miniflow_equal_in_minimask(miniflow, miniflow3, minimask));
5cb7a798
BP
1746
1747 /* Clean up. */
8fd47924
JR
1748 free(miniflow);
1749 free(miniflow2);
1750 free(miniflow3);
1751 free(minimask);
5cb7a798
BP
1752 }
1753}
1754
1755static void
1636c761 1756test_minimask_has_extra(struct ovs_cmdl_context *ctx OVS_UNUSED)
5cb7a798
BP
1757{
1758 struct flow_wildcards catchall;
8fd47924 1759 struct minimask *minicatchall;
5cb7a798
BP
1760 struct flow flow;
1761 unsigned int idx;
1762
1763 flow_wildcards_init_catchall(&catchall);
8fd47924
JR
1764 minicatchall = minimask_create(&catchall);
1765 assert(minimask_is_catchall(minicatchall));
5cb7a798
BP
1766
1767 random_set_seed(0x2ec7905b);
1768 for (idx = 0; next_random_flow(&flow, idx); idx++) {
1769 struct flow_wildcards mask;
8fd47924 1770 struct minimask *minimask;
5cb7a798
BP
1771
1772 mask.masks = flow;
8fd47924
JR
1773 minimask = minimask_create(&mask);
1774 assert(!minimask_has_extra(minimask, minimask));
1775 assert(minimask_has_extra(minicatchall, minimask)
1776 == !minimask_is_catchall(minimask));
1777 if (!minimask_is_catchall(minimask)) {
1778 struct minimask *minimask2;
5cb7a798
BP
1779
1780 wildcard_extra_bits(&mask);
8fd47924
JR
1781 minimask2 = minimask_create(&mask);
1782 assert(minimask_has_extra(minimask2, minimask));
1783 assert(!minimask_has_extra(minimask, minimask2));
1784 free(minimask2);
5cb7a798
BP
1785 }
1786
8fd47924 1787 free(minimask);
5cb7a798 1788 }
f2f3f5cb 1789
8fd47924 1790 free(minicatchall);
5cb7a798
BP
1791}
1792
1793static void
1636c761 1794test_minimask_combine(struct ovs_cmdl_context *ctx OVS_UNUSED)
5cb7a798
BP
1795{
1796 struct flow_wildcards catchall;
8fd47924 1797 struct minimask *minicatchall;
5cb7a798
BP
1798 struct flow flow;
1799 unsigned int idx;
1800
1801 flow_wildcards_init_catchall(&catchall);
8fd47924
JR
1802 minicatchall = minimask_create(&catchall);
1803 assert(minimask_is_catchall(minicatchall));
5cb7a798
BP
1804
1805 random_set_seed(0x181bf0cd);
1806 for (idx = 0; next_random_flow(&flow, idx); idx++) {
8fd47924 1807 struct minimask *minimask, *minimask2;
5cb7a798 1808 struct flow_wildcards mask, mask2, combined, combined2;
8fd47924
JR
1809 struct {
1810 struct minimask minicombined;
1811 uint64_t storage[FLOW_U64S];
1812 } m;
5cb7a798
BP
1813 struct flow flow2;
1814
1815 mask.masks = flow;
8fd47924 1816 minimask = minimask_create(&mask);
5cb7a798 1817
8fd47924
JR
1818 minimask_combine(&m.minicombined, minimask, minicatchall, m.storage);
1819 assert(minimask_is_catchall(&m.minicombined));
5cb7a798
BP
1820
1821 any_random_flow(&flow2);
1822 mask2.masks = flow2;
8fd47924 1823 minimask2 = minimask_create(&mask2);
5cb7a798 1824
8fd47924 1825 minimask_combine(&m.minicombined, minimask, minimask2, m.storage);
368eefac 1826 flow_wildcards_and(&combined, &mask, &mask2);
8fd47924 1827 minimask_expand(&m.minicombined, &combined2);
5cb7a798
BP
1828 assert(flow_wildcards_equal(&combined, &combined2));
1829
8fd47924
JR
1830 free(minimask);
1831 free(minimask2);
5cb7a798 1832 }
f2f3f5cb 1833
8fd47924 1834 free(minicatchall);
5cb7a798
BP
1835}
1836\f
1add079e
JR
1837
1838static void help(struct ovs_cmdl_context *ctx);
1839
5f383751 1840static const struct ovs_cmdl_command commands[] = {
5cb7a798 1841 /* Classifier tests. */
1f4a7252
RM
1842 {"empty", NULL, 0, 0, test_empty, OVS_RO },
1843 {"destroy-null", NULL, 0, 0, test_destroy_null, OVS_RO },
1844 {"single-rule", NULL, 0, 0, test_single_rule, OVS_RO },
1845 {"rule-replacement", NULL, 0, 0, test_rule_replacement, OVS_RO },
1846 {"many-rules-in-one-list", NULL, 0, 1, test_many_rules_in_one_list, OVS_RO },
1847 {"many-rules-in-one-table", NULL, 0, 1, test_many_rules_in_one_table, OVS_RO },
1848 {"many-rules-in-two-tables", NULL, 0, 0, test_many_rules_in_two_tables, OVS_RO },
1849 {"many-rules-in-five-tables", NULL, 0, 0, test_many_rules_in_five_tables, OVS_RO },
1850 {"benchmark", NULL, 0, 5, run_benchmarks, OVS_RO },
5cb7a798
BP
1851
1852 /* Miniflow and minimask tests. */
1f4a7252
RM
1853 {"miniflow", NULL, 0, 0, test_miniflow, OVS_RO },
1854 {"minimask_has_extra", NULL, 0, 0, test_minimask_has_extra, OVS_RO },
1855 {"minimask_combine", NULL, 0, 0, test_minimask_combine, OVS_RO },
5cb7a798 1856
1f4a7252
RM
1857 {"--help", NULL, 0, 0, help, OVS_RO },
1858 {NULL, NULL, 0, 0, NULL, OVS_RO },
3223e977 1859};
064af421 1860
1add079e
JR
1861static void
1862help(struct ovs_cmdl_context *ctx OVS_UNUSED)
1863{
1864 const struct ovs_cmdl_command *p;
1865 struct ds test_names = DS_EMPTY_INITIALIZER;
1866 const int linesize = 80;
1867
1868 printf("usage: ovstest %s TEST [TESTARGS]\n"
1869 "where TEST is one of the following:\n\n",
1870 program_name);
1871
1872 for (p = commands; p->name != NULL; p++) {
1873 if (*p->name != '-') { /* Skip internal commands */
1874 if (test_names.length > 1
1875 && test_names.length + strlen(p->name) + 1 >= linesize) {
1876 test_names.length -= 1;
1877 printf ("%s\n", ds_cstr(&test_names));
1878 ds_clear(&test_names);
1879 }
1880 ds_put_format(&test_names, "%s, ", p->name);
1881 }
1882 }
1883 if (test_names.length > 2) {
1884 test_names.length -= 2;
1885 printf("%s\n", ds_cstr(&test_names));
1886 }
1887 ds_destroy(&test_names);
1888}
1889
eadd1644
AZ
1890static void
1891test_classifier_main(int argc, char *argv[])
064af421 1892{
1636c761
RB
1893 struct ovs_cmdl_context ctx = {
1894 .argc = argc - 1,
1895 .argv = argv + 1,
1896 };
b5d97350 1897 set_program_name(argv[0]);
3bbe9a1f
JR
1898
1899 if (argc > 1 && !strcmp(argv[1], "--versioned")) {
1900 versioned = true;
1901 ctx.argc--;
1902 ctx.argv++;
1903 }
1904
064af421 1905 init_values();
1636c761 1906 ovs_cmdl_run_command(&ctx, commands);
064af421 1907}
eadd1644
AZ
1908
1909OVSTEST_REGISTER("test-classifier", test_classifier_main);