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