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