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