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