]> git.proxmox.com Git - mirror_ovs.git/blame - tests/test-classifier.c
flow: Use bit-mask for in_port match, instead of FWW_* flag.
[mirror_ovs.git] / tests / test-classifier.c
CommitLineData
064af421 1/*
e0edde6f 2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16
17/* "White box" tests for classifier.
18 *
19 * With very few exceptions, these tests obtain complete coverage of every
20 * basic block and every branch in the classifier implementation, e.g. a clean
21 * report from "gcov -b". (Covering the exceptions would require finding
22 * collisions in the hash function used for flow data, etc.)
23 *
24 * This test should receive a clean report from "valgrind --leak-check=full":
25 * it frees every heap block that it allocates.
26 */
27
28#include <config.h>
064af421
BP
29#include "classifier.h"
30#include <errno.h>
31#include <limits.h>
10a24935 32#include "byte-order.h"
3223e977 33#include "command-line.h"
064af421 34#include "flow.h"
0596e897 35#include "ofp-util.h"
064af421 36#include "packets.h"
c0a56d9f 37#include "unaligned.h"
064af421
BP
38
39#undef NDEBUG
40#include <assert.h>
41
b5d97350 42/* Fields in a rule. */
d8ae4d67 43#define CLS_FIELDS \
0bdc4bec
BP
44 /* struct flow all-caps */ \
45 /* member name name */ \
46 /* ----------- -------- */ \
47 CLS_FIELD(tun_id, TUN_ID) \
48 CLS_FIELD(metadata, METADATA) \
49 CLS_FIELD(nw_src, NW_SRC) \
50 CLS_FIELD(nw_dst, NW_DST) \
51 CLS_FIELD(in_port, IN_PORT) \
52 CLS_FIELD(vlan_tci, VLAN_TCI) \
53 CLS_FIELD(dl_type, DL_TYPE) \
54 CLS_FIELD(tp_src, TP_SRC) \
55 CLS_FIELD(tp_dst, TP_DST) \
56 CLS_FIELD(dl_src, DL_SRC) \
57 CLS_FIELD(dl_dst, DL_DST) \
58 CLS_FIELD(nw_proto, NW_PROTO) \
59 CLS_FIELD(nw_tos, NW_DSCP)
b5d97350
BP
60
61/* Field indexes.
62 *
63 * (These are also indexed into struct classifier's 'tables' array.) */
64enum {
0bdc4bec 65#define CLS_FIELD(MEMBER, NAME) CLS_F_IDX_##NAME,
b5d97350
BP
66 CLS_FIELDS
67#undef CLS_FIELD
68 CLS_N_FIELDS
69};
70
71/* Field information. */
72struct cls_field {
73 int ofs; /* Offset in struct flow. */
74 int len; /* Length in bytes. */
b5d97350
BP
75 const char *name; /* Name (for debugging). */
76};
77
78static const struct cls_field cls_fields[CLS_N_FIELDS] = {
0bdc4bec 79#define CLS_FIELD(MEMBER, NAME) \
b5d97350
BP
80 { offsetof(struct flow, MEMBER), \
81 sizeof ((struct flow *)0)->MEMBER, \
b5d97350
BP
82 #NAME },
83 CLS_FIELDS
84#undef CLS_FIELD
85};
86
064af421
BP
87struct test_rule {
88 int aux; /* Auxiliary data. */
89 struct cls_rule cls_rule; /* Classifier rule data. */
90};
91
92static struct test_rule *
93test_rule_from_cls_rule(const struct cls_rule *rule)
94{
95 return rule ? CONTAINER_OF(rule, struct test_rule, cls_rule) : NULL;
96}
97
98/* Trivial (linear) classifier. */
99struct tcls {
100 size_t n_rules;
101 size_t allocated_rules;
102 struct test_rule **rules;
103};
104
105static void
106tcls_init(struct tcls *tcls)
107{
108 tcls->n_rules = 0;
109 tcls->allocated_rules = 0;
110 tcls->rules = NULL;
111}
112
113static void
114tcls_destroy(struct tcls *tcls)
115{
116 if (tcls) {
117 size_t i;
118
119 for (i = 0; i < tcls->n_rules; i++) {
120 free(tcls->rules[i]);
121 }
122 free(tcls->rules);
123 }
124}
125
064af421
BP
126static bool
127tcls_is_empty(const struct tcls *tcls)
128{
129 return tcls->n_rules == 0;
130}
131
132static struct test_rule *
133tcls_insert(struct tcls *tcls, const struct test_rule *rule)
134{
135 size_t i;
136
00561f41
BP
137 assert(!flow_wildcards_is_exact(&rule->cls_rule.wc)
138 || rule->cls_rule.priority == UINT_MAX);
064af421
BP
139 for (i = 0; i < tcls->n_rules; i++) {
140 const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
193eb874
BP
141 if (cls_rule_equal(pos, &rule->cls_rule)) {
142 /* Exact match. */
064af421
BP
143 free(tcls->rules[i]);
144 tcls->rules[i] = xmemdup(rule, sizeof *rule);
145 return tcls->rules[i];
af7b73f4 146 } else if (pos->priority < rule->cls_rule.priority) {
064af421
BP
147 break;
148 }
149 }
150
151 if (tcls->n_rules >= tcls->allocated_rules) {
152 tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
153 sizeof *tcls->rules);
154 }
155 if (i != tcls->n_rules) {
156 memmove(&tcls->rules[i + 1], &tcls->rules[i],
157 sizeof *tcls->rules * (tcls->n_rules - i));
158 }
159 tcls->rules[i] = xmemdup(rule, sizeof *rule);
160 tcls->n_rules++;
161 return tcls->rules[i];
162}
163
164static void
165tcls_remove(struct tcls *cls, const struct test_rule *rule)
166{
167 size_t i;
168
169 for (i = 0; i < cls->n_rules; i++) {
170 struct test_rule *pos = cls->rules[i];
171 if (pos == rule) {
172 free(pos);
173 memmove(&cls->rules[i], &cls->rules[i + 1],
174 sizeof *cls->rules * (cls->n_rules - i - 1));
175 cls->n_rules--;
176 return;
177 }
178 }
179 NOT_REACHED();
180}
181
064af421 182static bool
ae412e7d 183match(const struct cls_rule *wild, const struct flow *fixed)
064af421
BP
184{
185 int f_idx;
186
187 for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
d8ae4d67
BP
188 bool eq;
189
0bdc4bec 190 if (f_idx == CLS_F_IDX_NW_SRC) {
d8ae4d67
BP
191 eq = !((fixed->nw_src ^ wild->flow.nw_src) & wild->wc.nw_src_mask);
192 } else if (f_idx == CLS_F_IDX_NW_DST) {
193 eq = !((fixed->nw_dst ^ wild->flow.nw_dst) & wild->wc.nw_dst_mask);
73f33563
BP
194 } else if (f_idx == CLS_F_IDX_TP_SRC) {
195 eq = !((fixed->tp_src ^ wild->flow.tp_src) & wild->wc.tp_src_mask);
196 } else if (f_idx == CLS_F_IDX_TP_DST) {
197 eq = !((fixed->tp_dst ^ wild->flow.tp_dst) & wild->wc.tp_dst_mask);
73c0ce34 198 } else if (f_idx == CLS_F_IDX_DL_SRC) {
3b842fc2
EJ
199 eq = eth_addr_equal_except(fixed->dl_src, wild->flow.dl_src,
200 wild->wc.dl_src_mask);
73c0ce34 201 } else if (f_idx == CLS_F_IDX_DL_DST) {
3b842fc2
EJ
202 eq = eth_addr_equal_except(fixed->dl_dst, wild->flow.dl_dst,
203 wild->wc.dl_dst_mask);
66642cb4
BP
204 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
205 eq = !((fixed->vlan_tci ^ wild->flow.vlan_tci)
206 & wild->wc.vlan_tci_mask);
8368c090
BP
207 } else if (f_idx == CLS_F_IDX_TUN_ID) {
208 eq = !((fixed->tun_id ^ wild->flow.tun_id) & wild->wc.tun_id_mask);
7525e578
JS
209 } else if (f_idx == CLS_F_IDX_METADATA) {
210 eq = !((fixed->metadata ^ wild->flow.metadata)
211 & wild->wc.metadata_mask);
2486e66a 212 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
5d9499c4
BP
213 eq = !((fixed->nw_tos ^ wild->flow.nw_tos) &
214 (wild->wc.nw_tos_mask & IP_DSCP_MASK));
851d3105
BP
215 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
216 eq = !((fixed->nw_proto ^ wild->flow.nw_proto)
217 & wild->wc.nw_proto_mask);
e2170cff
BP
218 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
219 eq = !((fixed->dl_type ^ wild->flow.dl_type)
220 & wild->wc.dl_type_mask);
0bdc4bec
BP
221 } else if (f_idx == CLS_F_IDX_IN_PORT) {
222 eq = !((fixed->in_port ^ wild->flow.in_port)
223 & wild->wc.in_port_mask);
d8ae4d67
BP
224 } else {
225 NOT_REACHED();
064af421
BP
226 }
227
d8ae4d67
BP
228 if (!eq) {
229 return false;
064af421 230 }
064af421
BP
231 }
232 return true;
233}
234
235static struct cls_rule *
3c4486a5 236tcls_lookup(const struct tcls *cls, const struct flow *flow)
064af421
BP
237{
238 size_t i;
239
240 for (i = 0; i < cls->n_rules; i++) {
241 struct test_rule *pos = cls->rules[i];
3c4486a5 242 if (match(&pos->cls_rule, flow)) {
064af421
BP
243 return &pos->cls_rule;
244 }
245 }
246 return NULL;
247}
248
249static void
3c4486a5 250tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
064af421
BP
251{
252 size_t i;
253
254 for (i = 0; i < cls->n_rules; ) {
255 struct test_rule *pos = cls->rules[i];
3c4486a5 256 if (!flow_wildcards_has_extra(&pos->cls_rule.wc, &target->wc)
064af421
BP
257 && match(target, &pos->cls_rule.flow)) {
258 tcls_remove(cls, pos);
259 } else {
260 i++;
261 }
262 }
263}
264\f
3c4486a5 265static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
965f03d8 266 CONSTANT_HTONL(0xc0a04455) };
3c4486a5 267static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
965f03d8 268 CONSTANT_HTONL(0xc0a04455) };
b9298d3f
BP
269static ovs_be64 tun_id_values[] = {
270 0,
271 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
7525e578
JS
272static ovs_be64 metadata_values[] = {
273 0,
274 CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
abe529af 275static uint16_t in_port_values[] = { 1, OFPP_LOCAL };
66642cb4 276static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
3c4486a5 277static ovs_be16 dl_type_values[]
965f03d8 278 = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
3c4486a5 279static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
965f03d8 280 CONSTANT_HTONS(80) };
3c4486a5 281static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
064af421
BP
282static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
283 { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
284static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
285 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
6767a2cc 286static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
2486e66a 287static uint8_t nw_dscp_values[] = { 48, 0 };
064af421
BP
288
289static void *values[CLS_N_FIELDS][2];
290
291static void
292init_values(void)
293{
659586ef
JG
294 values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
295 values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
296
7525e578
JS
297 values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
298 values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
299
064af421
BP
300 values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
301 values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
302
66642cb4
BP
303 values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
304 values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
959a2ecd 305
064af421
BP
306 values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
307 values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
308
309 values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
310 values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
311
312 values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
313 values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
314
315 values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
316 values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
317
318 values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
319 values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
320
321 values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
322 values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
323
2486e66a
JP
324 values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
325 values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
834377ea 326
064af421
BP
327 values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
328 values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
329
330 values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
331 values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
332}
333
334#define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
335#define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
659586ef 336#define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
7525e578 337#define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
064af421 338#define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
66642cb4 339#define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
064af421
BP
340#define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
341#define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
342#define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
343#define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
344#define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
345#define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
2486e66a 346#define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
064af421
BP
347
348#define N_FLOW_VALUES (N_NW_SRC_VALUES * \
349 N_NW_DST_VALUES * \
659586ef 350 N_TUN_ID_VALUES * \
064af421 351 N_IN_PORT_VALUES * \
66642cb4 352 N_VLAN_TCI_VALUES * \
064af421
BP
353 N_DL_TYPE_VALUES * \
354 N_TP_SRC_VALUES * \
355 N_TP_DST_VALUES * \
356 N_DL_SRC_VALUES * \
357 N_DL_DST_VALUES * \
834377ea 358 N_NW_PROTO_VALUES * \
2486e66a 359 N_NW_DSCP_VALUES)
064af421
BP
360
361static unsigned int
362get_value(unsigned int *x, unsigned n_values)
363{
364 unsigned int rem = *x % n_values;
365 *x /= n_values;
366 return rem;
367}
368
064af421
BP
369static void
370compare_classifiers(struct classifier *cls, struct tcls *tcls)
371{
70d3fbe7 372 static const int confidence = 500;
064af421
BP
373 unsigned int i;
374
375 assert(classifier_count(cls) == tcls->n_rules);
70d3fbe7 376 for (i = 0; i < confidence; i++) {
064af421 377 struct cls_rule *cr0, *cr1;
ae412e7d 378 struct flow flow;
064af421 379 unsigned int x;
064af421 380
70d3fbe7 381 x = rand () % N_FLOW_VALUES;
064af421
BP
382 flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
383 flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
659586ef 384 flow.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
7525e578 385 flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
064af421 386 flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
66642cb4 387 flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
064af421
BP
388 flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
389 flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
390 flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
391 memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
392 ETH_ADDR_LEN);
393 memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
394 ETH_ADDR_LEN);
395 flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
2486e66a 396 flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
064af421 397
3c4486a5
BP
398 cr0 = classifier_lookup(cls, &flow);
399 cr1 = tcls_lookup(tcls, &flow);
400 assert((cr0 == NULL) == (cr1 == NULL));
401 if (cr0 != NULL) {
402 const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
403 const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
404
193eb874 405 assert(cls_rule_equal(cr0, cr1));
3c4486a5 406 assert(tr0->aux == tr1->aux);
064af421
BP
407 }
408 }
409}
410
064af421
BP
411static void
412destroy_classifier(struct classifier *cls)
413{
5ecc9d81
BP
414 struct test_rule *rule, *next_rule;
415 struct cls_cursor cursor;
416
417 cls_cursor_init(&cursor, cls, NULL);
418 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
419 classifier_remove(cls, &rule->cls_rule);
420 free(rule);
421 }
064af421
BP
422 classifier_destroy(cls);
423}
424
425static void
426check_tables(const struct classifier *cls,
b5d97350 427 int n_tables, int n_rules, int n_dups)
064af421 428{
b5d97350 429 const struct cls_table *table;
955f579d
BP
430 struct test_rule *test_rule;
431 struct cls_cursor cursor;
064af421 432 int found_tables = 0;
064af421 433 int found_rules = 0;
b5d97350 434 int found_dups = 0;
955f579d 435 int found_rules2 = 0;
064af421 436
b5d97350
BP
437 HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
438 const struct cls_rule *head;
439
440 assert(!hmap_is_empty(&table->rules));
064af421 441
064af421 442 found_tables++;
b5d97350
BP
443 HMAP_FOR_EACH (head, hmap_node, &table->rules) {
444 unsigned int prev_priority = UINT_MAX;
445 const struct cls_rule *rule;
446
447 found_rules++;
448 LIST_FOR_EACH (rule, list, &head->list) {
449 assert(rule->priority < prev_priority);
450 prev_priority = rule->priority;
451 found_rules++;
452 found_dups++;
453 assert(classifier_find_rule_exactly(cls, rule) == rule);
454 }
455 }
064af421
BP
456 }
457
b5d97350
BP
458 assert(found_tables == hmap_count(&cls->tables));
459 assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
064af421 460 assert(n_rules == -1 || found_rules == n_rules);
b5d97350 461 assert(n_dups == -1 || found_dups == n_dups);
955f579d
BP
462
463 cls_cursor_init(&cursor, cls, NULL);
464 CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) {
465 found_rules2++;
466 }
467 assert(found_rules == found_rules2);
064af421
BP
468}
469
470static struct test_rule *
471make_rule(int wc_fields, unsigned int priority, int value_pat)
472{
473 const struct cls_field *f;
474 struct test_rule *rule;
064af421 475
d8ae4d67
BP
476 rule = xzalloc(sizeof *rule);
477 cls_rule_init_catchall(&rule->cls_rule, wc_fields ? priority : UINT_MAX);
064af421
BP
478 for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
479 int f_idx = f - cls_fields;
d8ae4d67
BP
480 int value_idx = (value_pat & (1u << f_idx)) != 0;
481 memcpy((char *) &rule->cls_rule.flow + f->ofs,
482 values[f_idx][value_idx], f->len);
483
0bdc4bec 484 if (f_idx == CLS_F_IDX_NW_SRC) {
d8ae4d67
BP
485 rule->cls_rule.wc.nw_src_mask = htonl(UINT32_MAX);
486 } else if (f_idx == CLS_F_IDX_NW_DST) {
487 rule->cls_rule.wc.nw_dst_mask = htonl(UINT32_MAX);
73f33563
BP
488 } else if (f_idx == CLS_F_IDX_TP_SRC) {
489 rule->cls_rule.wc.tp_src_mask = htons(UINT16_MAX);
490 } else if (f_idx == CLS_F_IDX_TP_DST) {
491 rule->cls_rule.wc.tp_dst_mask = htons(UINT16_MAX);
73c0ce34
JS
492 } else if (f_idx == CLS_F_IDX_DL_SRC) {
493 memset(rule->cls_rule.wc.dl_src_mask, 0xff, ETH_ADDR_LEN);
494 } else if (f_idx == CLS_F_IDX_DL_DST) {
495 memset(rule->cls_rule.wc.dl_dst_mask, 0xff, ETH_ADDR_LEN);
66642cb4
BP
496 } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
497 rule->cls_rule.wc.vlan_tci_mask = htons(UINT16_MAX);
8368c090
BP
498 } else if (f_idx == CLS_F_IDX_TUN_ID) {
499 rule->cls_rule.wc.tun_id_mask = htonll(UINT64_MAX);
7525e578
JS
500 } else if (f_idx == CLS_F_IDX_METADATA) {
501 rule->cls_rule.wc.metadata_mask = htonll(UINT64_MAX);
5d9499c4
BP
502 } else if (f_idx == CLS_F_IDX_NW_DSCP) {
503 rule->cls_rule.wc.nw_tos_mask |= IP_DSCP_MASK;
851d3105
BP
504 } else if (f_idx == CLS_F_IDX_NW_PROTO) {
505 rule->cls_rule.wc.nw_proto_mask = UINT8_MAX;
e2170cff
BP
506 } else if (f_idx == CLS_F_IDX_DL_TYPE) {
507 rule->cls_rule.wc.dl_type_mask = htons(UINT16_MAX);
0bdc4bec
BP
508 } else if (f_idx == CLS_F_IDX_IN_PORT) {
509 rule->cls_rule.wc.in_port_mask = UINT16_MAX;
064af421 510 } else {
d8ae4d67 511 NOT_REACHED();
064af421
BP
512 }
513 }
064af421
BP
514 return rule;
515}
516
517static void
518shuffle(unsigned int *p, size_t n)
519{
520 for (; n > 1; n--, p++) {
521 unsigned int *q = &p[rand() % n];
522 unsigned int tmp = *p;
523 *p = *q;
524 *q = tmp;
525 }
526}
527\f
528/* Tests an empty classifier. */
529static void
3223e977 530test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
531{
532 struct classifier cls;
533 struct tcls tcls;
534
535 classifier_init(&cls);
536 tcls_init(&tcls);
537 assert(classifier_is_empty(&cls));
538 assert(tcls_is_empty(&tcls));
539 compare_classifiers(&cls, &tcls);
540 classifier_destroy(&cls);
541 tcls_destroy(&tcls);
542}
543
544/* Destroys a null classifier. */
545static void
3223e977 546test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
547{
548 classifier_destroy(NULL);
549}
550
551/* Tests classification with one rule at a time. */
552static void
3223e977 553test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
554{
555 unsigned int wc_fields; /* Hilarious. */
556
557 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
558 struct classifier cls;
559 struct test_rule *rule, *tcls_rule;
560 struct tcls tcls;
561
562 rule = make_rule(wc_fields,
563 hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
564
565 classifier_init(&cls);
566 tcls_init(&tcls);
567
568 tcls_rule = tcls_insert(&tcls, rule);
08944c1d 569 classifier_insert(&cls, &rule->cls_rule);
b5d97350 570 check_tables(&cls, 1, 1, 0);
064af421
BP
571 compare_classifiers(&cls, &tcls);
572
573 classifier_remove(&cls, &rule->cls_rule);
574 tcls_remove(&tcls, tcls_rule);
575 assert(classifier_is_empty(&cls));
576 assert(tcls_is_empty(&tcls));
577 compare_classifiers(&cls, &tcls);
578
579 free(rule);
580 classifier_destroy(&cls);
581 tcls_destroy(&tcls);
582 }
583}
584
585/* Tests replacing one rule by another. */
586static void
3223e977 587test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
588{
589 unsigned int wc_fields;
590
591 for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
592 struct classifier cls;
f0f4410a
BP
593 struct test_rule *rule1;
594 struct test_rule *rule2;
064af421
BP
595 struct tcls tcls;
596
597 rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
598 rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
599 rule2->aux += 5;
600 rule2->aux += 5;
601
602 classifier_init(&cls);
603 tcls_init(&tcls);
f0f4410a 604 tcls_insert(&tcls, rule1);
08944c1d 605 classifier_insert(&cls, &rule1->cls_rule);
b5d97350 606 check_tables(&cls, 1, 1, 0);
064af421
BP
607 compare_classifiers(&cls, &tcls);
608 tcls_destroy(&tcls);
609
610 tcls_init(&tcls);
f0f4410a 611 tcls_insert(&tcls, rule2);
064af421 612 assert(test_rule_from_cls_rule(
08944c1d 613 classifier_replace(&cls, &rule2->cls_rule)) == rule1);
064af421 614 free(rule1);
b5d97350 615 check_tables(&cls, 1, 1, 0);
064af421
BP
616 compare_classifiers(&cls, &tcls);
617 tcls_destroy(&tcls);
618 destroy_classifier(&cls);
619 }
620}
621
622static int
b5d97350 623factorial(int n_items)
064af421 624{
b5d97350
BP
625 int n, i;
626
627 n = 1;
628 for (i = 2; i <= n_items; i++) {
629 n *= i;
630 }
631 return n;
064af421
BP
632}
633
b5d97350
BP
634static void
635swap(int *a, int *b)
064af421 636{
b5d97350
BP
637 int tmp = *a;
638 *a = *b;
639 *b = tmp;
064af421
BP
640}
641
064af421 642static void
b5d97350 643reverse(int *a, int n)
064af421 644{
b5d97350 645 int i;
064af421 646
b5d97350
BP
647 for (i = 0; i < n / 2; i++) {
648 int j = n - (i + 1);
649 swap(&a[i], &a[j]);
650 }
651}
064af421 652
b5d97350
BP
653static bool
654next_permutation(int *a, int n)
655{
656 int k;
064af421 657
b5d97350
BP
658 for (k = n - 2; k >= 0; k--) {
659 if (a[k] < a[k + 1]) {
660 int l;
064af421 661
b5d97350
BP
662 for (l = n - 1; ; l--) {
663 if (a[l] > a[k]) {
664 swap(&a[k], &a[l]);
665 reverse(a + (k + 1), n - (k + 1));
666 return true;
064af421
BP
667 }
668 }
669 }
670 }
b5d97350 671 return false;
064af421
BP
672}
673
b5d97350 674/* Tests classification with rules that have the same matching criteria. */
064af421 675static void
b5d97350
BP
676test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
677{
678 enum { N_RULES = 3 };
679 int n_pris;
064af421 680
b5d97350
BP
681 for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
682 int ops[N_RULES * 2];
683 int pris[N_RULES];
684 int n_permutations;
685 int i;
064af421 686
b5d97350
BP
687 pris[0] = 0;
688 for (i = 1; i < N_RULES; i++) {
689 pris[i] = pris[i - 1] + (n_pris > i);
690 }
064af421 691
b5d97350
BP
692 for (i = 0; i < N_RULES * 2; i++) {
693 ops[i] = i / 2;
064af421 694 }
064af421 695
b5d97350
BP
696 n_permutations = 0;
697 do {
698 struct test_rule *rules[N_RULES];
699 struct test_rule *tcls_rules[N_RULES];
700 int pri_rules[N_RULES];
701 struct classifier cls;
702 struct tcls tcls;
064af421 703
b5d97350 704 n_permutations++;
064af421 705
b5d97350
BP
706 for (i = 0; i < N_RULES; i++) {
707 rules[i] = make_rule(456, pris[i], 0);
708 tcls_rules[i] = NULL;
709 pri_rules[i] = -1;
710 }
064af421 711
b5d97350
BP
712 classifier_init(&cls);
713 tcls_init(&tcls);
064af421 714
b5d97350
BP
715 for (i = 0; i < ARRAY_SIZE(ops); i++) {
716 int j = ops[i];
717 int m, n;
064af421 718
b5d97350
BP
719 if (!tcls_rules[j]) {
720 struct test_rule *displaced_rule;
721
722 tcls_rules[j] = tcls_insert(&tcls, rules[j]);
723 displaced_rule = test_rule_from_cls_rule(
08944c1d 724 classifier_replace(&cls, &rules[j]->cls_rule));
b5d97350
BP
725 if (pri_rules[pris[j]] >= 0) {
726 int k = pri_rules[pris[j]];
727 assert(displaced_rule != NULL);
728 assert(displaced_rule != rules[j]);
729 assert(pris[j] == displaced_rule->cls_rule.priority);
730 tcls_rules[k] = NULL;
731 } else {
732 assert(displaced_rule == NULL);
733 }
734 pri_rules[pris[j]] = j;
735 } else {
736 classifier_remove(&cls, &rules[j]->cls_rule);
737 tcls_remove(&tcls, tcls_rules[j]);
738 tcls_rules[j] = NULL;
739 pri_rules[pris[j]] = -1;
740 }
064af421 741
b5d97350
BP
742 n = 0;
743 for (m = 0; m < N_RULES; m++) {
744 n += tcls_rules[m] != NULL;
064af421 745 }
b5d97350
BP
746 check_tables(&cls, n > 0, n, n - 1);
747
748 compare_classifiers(&cls, &tcls);
064af421 749 }
b5d97350
BP
750
751 classifier_destroy(&cls);
752 tcls_destroy(&tcls);
753
754 for (i = 0; i < N_RULES; i++) {
755 free(rules[i]);
756 }
757 } while (next_permutation(ops, ARRAY_SIZE(ops)));
758 assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
064af421
BP
759 }
760}
761
b5d97350
BP
762static int
763count_ones(unsigned long int x)
064af421 764{
b5d97350 765 int n = 0;
064af421 766
b5d97350
BP
767 while (x) {
768 x &= x - 1;
769 n++;
770 }
064af421 771
b5d97350
BP
772 return n;
773}
064af421 774
b5d97350
BP
775static bool
776array_contains(int *array, int n, int value)
777{
778 int i;
064af421 779
b5d97350
BP
780 for (i = 0; i < n; i++) {
781 if (array[i] == value) {
782 return true;
064af421
BP
783 }
784 }
b5d97350
BP
785
786 return false;
064af421
BP
787}
788
b5d97350
BP
789/* Tests classification with two rules at a time that fall into the same
790 * table but different lists. */
064af421 791static void
3223e977 792test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421 793{
b5d97350 794 int iteration;
064af421 795
b5d97350
BP
796 for (iteration = 0; iteration < 50; iteration++) {
797 enum { N_RULES = 20 };
798 struct test_rule *rules[N_RULES];
799 struct test_rule *tcls_rules[N_RULES];
800 struct classifier cls;
801 struct tcls tcls;
802 int value_pats[N_RULES];
803 int value_mask;
804 int wcf;
805 int i;
064af421 806
b5d97350
BP
807 do {
808 wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
809 value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
810 } while ((1 << count_ones(value_mask)) < N_RULES);
064af421 811
b5d97350
BP
812 classifier_init(&cls);
813 tcls_init(&tcls);
064af421 814
b5d97350
BP
815 for (i = 0; i < N_RULES; i++) {
816 unsigned int priority = rand();
064af421 817
b5d97350
BP
818 do {
819 value_pats[i] = rand() & value_mask;
820 } while (array_contains(value_pats, i, value_pats[i]));
064af421 821
b5d97350
BP
822 rules[i] = make_rule(wcf, priority, value_pats[i]);
823 tcls_rules[i] = tcls_insert(&tcls, rules[i]);
08944c1d 824 classifier_insert(&cls, &rules[i]->cls_rule);
b5d97350
BP
825
826 check_tables(&cls, 1, i + 1, 0);
827 compare_classifiers(&cls, &tcls);
828 }
829
830 for (i = 0; i < N_RULES; i++) {
831 tcls_remove(&tcls, tcls_rules[i]);
832 classifier_remove(&cls, &rules[i]->cls_rule);
833 free(rules[i]);
834
835 check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
836 compare_classifiers(&cls, &tcls);
064af421 837 }
b5d97350
BP
838
839 classifier_destroy(&cls);
840 tcls_destroy(&tcls);
064af421
BP
841 }
842}
843
b5d97350
BP
844/* Tests classification with many rules at a time that fall into random lists
845 * in 'n' tables. */
064af421 846static void
b5d97350 847test_many_rules_in_n_tables(int n_tables)
064af421
BP
848{
849 enum { MAX_RULES = 50 };
b5d97350 850 int wcfs[10];
064af421 851 int iteration;
b5d97350
BP
852 int i;
853
854 assert(n_tables < 10);
855 for (i = 0; i < n_tables; i++) {
856 do {
857 wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
858 } while (array_contains(wcfs, i, wcfs[i]));
859 }
064af421
BP
860
861 for (iteration = 0; iteration < 30; iteration++) {
862 unsigned int priorities[MAX_RULES];
863 struct classifier cls;
864 struct tcls tcls;
064af421
BP
865
866 srand(iteration);
867 for (i = 0; i < MAX_RULES; i++) {
868 priorities[i] = i * 129;
869 }
870 shuffle(priorities, ARRAY_SIZE(priorities));
871
872 classifier_init(&cls);
873 tcls_init(&tcls);
874
875 for (i = 0; i < MAX_RULES; i++) {
876 struct test_rule *rule;
877 unsigned int priority = priorities[i];
b5d97350 878 int wcf = wcfs[rand() % n_tables];
064af421
BP
879 int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
880 rule = make_rule(wcf, priority, value_pat);
881 tcls_insert(&tcls, rule);
08944c1d 882 classifier_insert(&cls, &rule->cls_rule);
b5d97350 883 check_tables(&cls, -1, i + 1, -1);
064af421
BP
884 compare_classifiers(&cls, &tcls);
885 }
886
887 while (!classifier_is_empty(&cls)) {
5ecc9d81
BP
888 struct test_rule *rule, *next_rule;
889 struct test_rule *target;
890 struct cls_cursor cursor;
891
892 target = xmemdup(tcls.rules[rand() % tcls.n_rules],
893 sizeof(struct test_rule));
894
895 cls_cursor_init(&cursor, &cls, &target->cls_rule);
896 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
897 classifier_remove(&cls, &rule->cls_rule);
898 free(rule);
899 }
900 tcls_delete_matches(&tcls, &target->cls_rule);
064af421 901 compare_classifiers(&cls, &tcls);
b5d97350 902 check_tables(&cls, -1, -1, -1);
5ecc9d81 903 free(target);
064af421 904 }
064af421
BP
905
906 destroy_classifier(&cls);
907 tcls_destroy(&tcls);
908 }
909}
b5d97350
BP
910
911static void
912test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
913{
914 test_many_rules_in_n_tables(2);
915}
916
917static void
918test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
919{
920 test_many_rules_in_n_tables(5);
921}
064af421 922\f
3223e977
BP
923static const struct command commands[] = {
924 {"empty", 0, 0, test_empty},
925 {"destroy-null", 0, 0, test_destroy_null},
926 {"single-rule", 0, 0, test_single_rule},
927 {"rule-replacement", 0, 0, test_rule_replacement},
b5d97350 928 {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
3223e977 929 {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
b5d97350
BP
930 {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
931 {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
3223e977
BP
932 {NULL, 0, 0, NULL},
933};
064af421
BP
934
935int
3223e977 936main(int argc, char *argv[])
064af421 937{
b5d97350 938 set_program_name(argv[0]);
064af421 939 init_values();
3223e977 940 run_command(argc - 1, argv + 1, commands);
064af421
BP
941 return 0;
942}