tests: Fix memory leaks in test-classifier program.
[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(tunnel.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 static void
99 test_rule_destroy(struct test_rule *rule)
100 {
101     if (rule) {
102         cls_rule_destroy(&rule->cls_rule);
103         free(rule);
104     }
105 }
106
107 static struct test_rule *make_rule(int wc_fields, unsigned int priority,
108                                    int value_pat);
109 static void free_rule(struct test_rule *);
110 static struct test_rule *clone_rule(const struct test_rule *);
111
112 /* Trivial (linear) classifier. */
113 struct tcls {
114     size_t n_rules;
115     size_t allocated_rules;
116     struct test_rule **rules;
117 };
118
119 static void
120 tcls_init(struct tcls *tcls)
121 {
122     tcls->n_rules = 0;
123     tcls->allocated_rules = 0;
124     tcls->rules = NULL;
125 }
126
127 static void
128 tcls_destroy(struct tcls *tcls)
129 {
130     if (tcls) {
131         size_t i;
132
133         for (i = 0; i < tcls->n_rules; i++) {
134             test_rule_destroy(tcls->rules[i]);
135         }
136         free(tcls->rules);
137     }
138 }
139
140 static bool
141 tcls_is_empty(const struct tcls *tcls)
142 {
143     return tcls->n_rules == 0;
144 }
145
146 static struct test_rule *
147 tcls_insert(struct tcls *tcls, const struct test_rule *rule)
148 {
149     size_t i;
150
151     for (i = 0; i < tcls->n_rules; i++) {
152         const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
153         if (cls_rule_equal(pos, &rule->cls_rule)) {
154             /* Exact match. */
155             free_rule(tcls->rules[i]);
156             tcls->rules[i] = clone_rule(rule);
157             return tcls->rules[i];
158         } else if (pos->priority < rule->cls_rule.priority) {
159             break;
160         }
161     }
162
163     if (tcls->n_rules >= tcls->allocated_rules) {
164         tcls->rules = x2nrealloc(tcls->rules, &tcls->allocated_rules,
165                                  sizeof *tcls->rules);
166     }
167     if (i != tcls->n_rules) {
168         memmove(&tcls->rules[i + 1], &tcls->rules[i],
169                 sizeof *tcls->rules * (tcls->n_rules - i));
170     }
171     tcls->rules[i] = clone_rule(rule);
172     tcls->n_rules++;
173     return tcls->rules[i];
174 }
175
176 static void
177 tcls_remove(struct tcls *cls, const struct test_rule *rule)
178 {
179     size_t i;
180
181     for (i = 0; i < cls->n_rules; i++) {
182         struct test_rule *pos = cls->rules[i];
183         if (pos == rule) {
184             test_rule_destroy(pos);
185
186             memmove(&cls->rules[i], &cls->rules[i + 1],
187                     sizeof *cls->rules * (cls->n_rules - i - 1));
188
189             cls->n_rules--;
190             return;
191         }
192     }
193     NOT_REACHED();
194 }
195
196 static bool
197 match(const struct cls_rule *wild_, const struct flow *fixed)
198 {
199     struct match wild;
200     int f_idx;
201
202     minimatch_expand(&wild_->match, &wild);
203     for (f_idx = 0; f_idx < CLS_N_FIELDS; f_idx++) {
204         bool eq;
205
206         if (f_idx == CLS_F_IDX_NW_SRC) {
207             eq = !((fixed->nw_src ^ wild.flow.nw_src)
208                    & wild.wc.masks.nw_src);
209         } else if (f_idx == CLS_F_IDX_NW_DST) {
210             eq = !((fixed->nw_dst ^ wild.flow.nw_dst)
211                    & wild.wc.masks.nw_dst);
212         } else if (f_idx == CLS_F_IDX_TP_SRC) {
213             eq = !((fixed->tp_src ^ wild.flow.tp_src)
214                    & wild.wc.masks.tp_src);
215         } else if (f_idx == CLS_F_IDX_TP_DST) {
216             eq = !((fixed->tp_dst ^ wild.flow.tp_dst)
217                    & wild.wc.masks.tp_dst);
218         } else if (f_idx == CLS_F_IDX_DL_SRC) {
219             eq = eth_addr_equal_except(fixed->dl_src, wild.flow.dl_src,
220                                        wild.wc.masks.dl_src);
221         } else if (f_idx == CLS_F_IDX_DL_DST) {
222             eq = eth_addr_equal_except(fixed->dl_dst, wild.flow.dl_dst,
223                                        wild.wc.masks.dl_dst);
224         } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
225             eq = !((fixed->vlan_tci ^ wild.flow.vlan_tci)
226                    & wild.wc.masks.vlan_tci);
227         } else if (f_idx == CLS_F_IDX_TUN_ID) {
228             eq = !((fixed->tunnel.tun_id ^ wild.flow.tunnel.tun_id)
229                    & wild.wc.masks.tunnel.tun_id);
230         } else if (f_idx == CLS_F_IDX_METADATA) {
231             eq = !((fixed->metadata ^ wild.flow.metadata)
232                    & wild.wc.masks.metadata);
233         } else if (f_idx == CLS_F_IDX_NW_DSCP) {
234             eq = !((fixed->nw_tos ^ wild.flow.nw_tos) &
235                    (wild.wc.masks.nw_tos & IP_DSCP_MASK));
236         } else if (f_idx == CLS_F_IDX_NW_PROTO) {
237             eq = !((fixed->nw_proto ^ wild.flow.nw_proto)
238                    & wild.wc.masks.nw_proto);
239         } else if (f_idx == CLS_F_IDX_DL_TYPE) {
240             eq = !((fixed->dl_type ^ wild.flow.dl_type)
241                    & wild.wc.masks.dl_type);
242         } else if (f_idx == CLS_F_IDX_IN_PORT) {
243             eq = !((fixed->in_port ^ wild.flow.in_port)
244                    & wild.wc.masks.in_port);
245         } else {
246             NOT_REACHED();
247         }
248
249         if (!eq) {
250             return false;
251         }
252     }
253     return true;
254 }
255
256 static struct cls_rule *
257 tcls_lookup(const struct tcls *cls, const struct flow *flow)
258 {
259     size_t i;
260
261     for (i = 0; i < cls->n_rules; i++) {
262         struct test_rule *pos = cls->rules[i];
263         if (match(&pos->cls_rule, flow)) {
264             return &pos->cls_rule;
265         }
266     }
267     return NULL;
268 }
269
270 static void
271 tcls_delete_matches(struct tcls *cls, const struct cls_rule *target)
272 {
273     size_t i;
274
275     for (i = 0; i < cls->n_rules; ) {
276         struct test_rule *pos = cls->rules[i];
277         if (!minimask_has_extra(&pos->cls_rule.match.mask,
278                                 &target->match.mask)) {
279             struct flow flow;
280
281             miniflow_expand(&pos->cls_rule.match.flow, &flow);
282             if (match(target, &flow)) {
283                 tcls_remove(cls, pos);
284                 continue;
285             }
286         }
287         i++;
288     }
289 }
290 \f
291 static ovs_be32 nw_src_values[] = { CONSTANT_HTONL(0xc0a80001),
292                                     CONSTANT_HTONL(0xc0a04455) };
293 static ovs_be32 nw_dst_values[] = { CONSTANT_HTONL(0xc0a80002),
294                                     CONSTANT_HTONL(0xc0a04455) };
295 static ovs_be64 tun_id_values[] = {
296     0,
297     CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
298 static ovs_be64 metadata_values[] = {
299     0,
300     CONSTANT_HTONLL(UINT64_C(0xfedcba9876543210)) };
301 static uint16_t in_port_values[] = { 1, OFPP_LOCAL };
302 static ovs_be16 vlan_tci_values[] = { CONSTANT_HTONS(101), CONSTANT_HTONS(0) };
303 static ovs_be16 dl_type_values[]
304             = { CONSTANT_HTONS(ETH_TYPE_IP), CONSTANT_HTONS(ETH_TYPE_ARP) };
305 static ovs_be16 tp_src_values[] = { CONSTANT_HTONS(49362),
306                                     CONSTANT_HTONS(80) };
307 static ovs_be16 tp_dst_values[] = { CONSTANT_HTONS(6667), CONSTANT_HTONS(22) };
308 static uint8_t dl_src_values[][6] = { { 0x00, 0x02, 0xe3, 0x0f, 0x80, 0xa4 },
309                                       { 0x5e, 0x33, 0x7f, 0x5f, 0x1e, 0x99 } };
310 static uint8_t dl_dst_values[][6] = { { 0x4a, 0x27, 0x71, 0xae, 0x64, 0xc1 },
311                                       { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
312 static uint8_t nw_proto_values[] = { IPPROTO_TCP, IPPROTO_ICMP };
313 static uint8_t nw_dscp_values[] = { 48, 0 };
314
315 static void *values[CLS_N_FIELDS][2];
316
317 static void
318 init_values(void)
319 {
320     values[CLS_F_IDX_TUN_ID][0] = &tun_id_values[0];
321     values[CLS_F_IDX_TUN_ID][1] = &tun_id_values[1];
322
323     values[CLS_F_IDX_METADATA][0] = &metadata_values[0];
324     values[CLS_F_IDX_METADATA][1] = &metadata_values[1];
325
326     values[CLS_F_IDX_IN_PORT][0] = &in_port_values[0];
327     values[CLS_F_IDX_IN_PORT][1] = &in_port_values[1];
328
329     values[CLS_F_IDX_VLAN_TCI][0] = &vlan_tci_values[0];
330     values[CLS_F_IDX_VLAN_TCI][1] = &vlan_tci_values[1];
331
332     values[CLS_F_IDX_DL_SRC][0] = dl_src_values[0];
333     values[CLS_F_IDX_DL_SRC][1] = dl_src_values[1];
334
335     values[CLS_F_IDX_DL_DST][0] = dl_dst_values[0];
336     values[CLS_F_IDX_DL_DST][1] = dl_dst_values[1];
337
338     values[CLS_F_IDX_DL_TYPE][0] = &dl_type_values[0];
339     values[CLS_F_IDX_DL_TYPE][1] = &dl_type_values[1];
340
341     values[CLS_F_IDX_NW_SRC][0] = &nw_src_values[0];
342     values[CLS_F_IDX_NW_SRC][1] = &nw_src_values[1];
343
344     values[CLS_F_IDX_NW_DST][0] = &nw_dst_values[0];
345     values[CLS_F_IDX_NW_DST][1] = &nw_dst_values[1];
346
347     values[CLS_F_IDX_NW_PROTO][0] = &nw_proto_values[0];
348     values[CLS_F_IDX_NW_PROTO][1] = &nw_proto_values[1];
349
350     values[CLS_F_IDX_NW_DSCP][0] = &nw_dscp_values[0];
351     values[CLS_F_IDX_NW_DSCP][1] = &nw_dscp_values[1];
352
353     values[CLS_F_IDX_TP_SRC][0] = &tp_src_values[0];
354     values[CLS_F_IDX_TP_SRC][1] = &tp_src_values[1];
355
356     values[CLS_F_IDX_TP_DST][0] = &tp_dst_values[0];
357     values[CLS_F_IDX_TP_DST][1] = &tp_dst_values[1];
358 }
359
360 #define N_NW_SRC_VALUES ARRAY_SIZE(nw_src_values)
361 #define N_NW_DST_VALUES ARRAY_SIZE(nw_dst_values)
362 #define N_TUN_ID_VALUES ARRAY_SIZE(tun_id_values)
363 #define N_METADATA_VALUES ARRAY_SIZE(metadata_values)
364 #define N_IN_PORT_VALUES ARRAY_SIZE(in_port_values)
365 #define N_VLAN_TCI_VALUES ARRAY_SIZE(vlan_tci_values)
366 #define N_DL_TYPE_VALUES ARRAY_SIZE(dl_type_values)
367 #define N_TP_SRC_VALUES ARRAY_SIZE(tp_src_values)
368 #define N_TP_DST_VALUES ARRAY_SIZE(tp_dst_values)
369 #define N_DL_SRC_VALUES ARRAY_SIZE(dl_src_values)
370 #define N_DL_DST_VALUES ARRAY_SIZE(dl_dst_values)
371 #define N_NW_PROTO_VALUES ARRAY_SIZE(nw_proto_values)
372 #define N_NW_DSCP_VALUES ARRAY_SIZE(nw_dscp_values)
373
374 #define N_FLOW_VALUES (N_NW_SRC_VALUES *        \
375                        N_NW_DST_VALUES *        \
376                        N_TUN_ID_VALUES *        \
377                        N_IN_PORT_VALUES *       \
378                        N_VLAN_TCI_VALUES *       \
379                        N_DL_TYPE_VALUES *       \
380                        N_TP_SRC_VALUES *        \
381                        N_TP_DST_VALUES *        \
382                        N_DL_SRC_VALUES *        \
383                        N_DL_DST_VALUES *        \
384                        N_NW_PROTO_VALUES *      \
385                        N_NW_DSCP_VALUES)
386
387 static unsigned int
388 get_value(unsigned int *x, unsigned n_values)
389 {
390     unsigned int rem = *x % n_values;
391     *x /= n_values;
392     return rem;
393 }
394
395 static void
396 compare_classifiers(struct classifier *cls, struct tcls *tcls)
397 {
398     static const int confidence = 500;
399     unsigned int i;
400
401     assert(classifier_count(cls) == tcls->n_rules);
402     for (i = 0; i < confidence; i++) {
403         struct cls_rule *cr0, *cr1;
404         struct flow flow;
405         unsigned int x;
406
407         x = rand () % N_FLOW_VALUES;
408         memset(&flow, 0, sizeof flow);
409         flow.nw_src = nw_src_values[get_value(&x, N_NW_SRC_VALUES)];
410         flow.nw_dst = nw_dst_values[get_value(&x, N_NW_DST_VALUES)];
411         flow.tunnel.tun_id = tun_id_values[get_value(&x, N_TUN_ID_VALUES)];
412         flow.metadata = metadata_values[get_value(&x, N_METADATA_VALUES)];
413         flow.in_port = in_port_values[get_value(&x, N_IN_PORT_VALUES)];
414         flow.vlan_tci = vlan_tci_values[get_value(&x, N_VLAN_TCI_VALUES)];
415         flow.dl_type = dl_type_values[get_value(&x, N_DL_TYPE_VALUES)];
416         flow.tp_src = tp_src_values[get_value(&x, N_TP_SRC_VALUES)];
417         flow.tp_dst = tp_dst_values[get_value(&x, N_TP_DST_VALUES)];
418         memcpy(flow.dl_src, dl_src_values[get_value(&x, N_DL_SRC_VALUES)],
419                ETH_ADDR_LEN);
420         memcpy(flow.dl_dst, dl_dst_values[get_value(&x, N_DL_DST_VALUES)],
421                ETH_ADDR_LEN);
422         flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
423         flow.nw_tos = nw_dscp_values[get_value(&x, N_NW_DSCP_VALUES)];
424
425         cr0 = classifier_lookup(cls, &flow);
426         cr1 = tcls_lookup(tcls, &flow);
427         assert((cr0 == NULL) == (cr1 == NULL));
428         if (cr0 != NULL) {
429             const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
430             const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
431
432             assert(cls_rule_equal(cr0, cr1));
433             assert(tr0->aux == tr1->aux);
434         }
435     }
436 }
437
438 static void
439 destroy_classifier(struct classifier *cls)
440 {
441     struct test_rule *rule, *next_rule;
442     struct cls_cursor cursor;
443
444     cls_cursor_init(&cursor, cls, NULL);
445     CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
446         classifier_remove(cls, &rule->cls_rule);
447         free_rule(rule);
448     }
449     classifier_destroy(cls);
450 }
451
452 static void
453 check_tables(const struct classifier *cls,
454              int n_tables, int n_rules, int n_dups)
455 {
456     const struct cls_table *table;
457     struct test_rule *test_rule;
458     struct cls_cursor cursor;
459     int found_tables = 0;
460     int found_rules = 0;
461     int found_dups = 0;
462     int found_rules2 = 0;
463
464     HMAP_FOR_EACH (table, hmap_node, &cls->tables) {
465         const struct cls_rule *head;
466
467         assert(!hmap_is_empty(&table->rules));
468
469         found_tables++;
470         HMAP_FOR_EACH (head, hmap_node, &table->rules) {
471             unsigned int prev_priority = UINT_MAX;
472             const struct cls_rule *rule;
473
474             found_rules++;
475             LIST_FOR_EACH (rule, list, &head->list) {
476                 assert(rule->priority < prev_priority);
477                 prev_priority = rule->priority;
478                 found_rules++;
479                 found_dups++;
480                 assert(classifier_find_rule_exactly(cls, rule) == rule);
481             }
482         }
483     }
484
485     assert(found_tables == hmap_count(&cls->tables));
486     assert(n_tables == -1 || n_tables == hmap_count(&cls->tables));
487     assert(n_rules == -1 || found_rules == n_rules);
488     assert(n_dups == -1 || found_dups == n_dups);
489
490     cls_cursor_init(&cursor, cls, NULL);
491     CLS_CURSOR_FOR_EACH (test_rule, cls_rule, &cursor) {
492         found_rules2++;
493     }
494     assert(found_rules == found_rules2);
495 }
496
497 static struct test_rule *
498 make_rule(int wc_fields, unsigned int priority, int value_pat)
499 {
500     const struct cls_field *f;
501     struct test_rule *rule;
502     struct match match;
503
504     match_init_catchall(&match);
505     for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
506         int f_idx = f - cls_fields;
507         int value_idx = (value_pat & (1u << f_idx)) != 0;
508         memcpy((char *) &match.flow + f->ofs,
509                values[f_idx][value_idx], f->len);
510
511         if (f_idx == CLS_F_IDX_NW_SRC) {
512             match.wc.masks.nw_src = htonl(UINT32_MAX);
513         } else if (f_idx == CLS_F_IDX_NW_DST) {
514             match.wc.masks.nw_dst = htonl(UINT32_MAX);
515         } else if (f_idx == CLS_F_IDX_TP_SRC) {
516             match.wc.masks.tp_src = htons(UINT16_MAX);
517         } else if (f_idx == CLS_F_IDX_TP_DST) {
518             match.wc.masks.tp_dst = htons(UINT16_MAX);
519         } else if (f_idx == CLS_F_IDX_DL_SRC) {
520             memset(match.wc.masks.dl_src, 0xff, ETH_ADDR_LEN);
521         } else if (f_idx == CLS_F_IDX_DL_DST) {
522             memset(match.wc.masks.dl_dst, 0xff, ETH_ADDR_LEN);
523         } else if (f_idx == CLS_F_IDX_VLAN_TCI) {
524             match.wc.masks.vlan_tci = htons(UINT16_MAX);
525         } else if (f_idx == CLS_F_IDX_TUN_ID) {
526             match.wc.masks.tunnel.tun_id = htonll(UINT64_MAX);
527         } else if (f_idx == CLS_F_IDX_METADATA) {
528             match.wc.masks.metadata = htonll(UINT64_MAX);
529         } else if (f_idx == CLS_F_IDX_NW_DSCP) {
530             match.wc.masks.nw_tos |= IP_DSCP_MASK;
531         } else if (f_idx == CLS_F_IDX_NW_PROTO) {
532             match.wc.masks.nw_proto = UINT8_MAX;
533         } else if (f_idx == CLS_F_IDX_DL_TYPE) {
534             match.wc.masks.dl_type = htons(UINT16_MAX);
535         } else if (f_idx == CLS_F_IDX_IN_PORT) {
536             match.wc.masks.in_port = UINT16_MAX;
537         } else {
538             NOT_REACHED();
539         }
540     }
541
542     rule = xzalloc(sizeof *rule);
543     cls_rule_init(&rule->cls_rule, &match, wc_fields ? priority : UINT_MAX);
544     return rule;
545 }
546
547 static struct test_rule *
548 clone_rule(const struct test_rule *src)
549 {
550     struct test_rule *dst;
551
552     dst = xmalloc(sizeof *dst);
553     dst->aux = src->aux;
554     cls_rule_clone(&dst->cls_rule, &src->cls_rule);
555     return dst;
556 }
557
558 static void
559 free_rule(struct test_rule *rule)
560 {
561     cls_rule_destroy(&rule->cls_rule);
562     free(rule);
563 }
564
565 static void
566 shuffle(unsigned int *p, size_t n)
567 {
568     for (; n > 1; n--, p++) {
569         unsigned int *q = &p[rand() % n];
570         unsigned int tmp = *p;
571         *p = *q;
572         *q = tmp;
573     }
574 }
575
576 static void
577 shuffle_u32s(uint32_t *p, size_t n)
578 {
579     for (; n > 1; n--, p++) {
580         uint32_t *q = &p[rand() % n];
581         uint32_t tmp = *p;
582         *p = *q;
583         *q = tmp;
584     }
585 }
586 \f
587 /* Classifier tests. */
588
589 /* Tests an empty classifier. */
590 static void
591 test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
592 {
593     struct classifier cls;
594     struct tcls tcls;
595
596     classifier_init(&cls);
597     tcls_init(&tcls);
598     assert(classifier_is_empty(&cls));
599     assert(tcls_is_empty(&tcls));
600     compare_classifiers(&cls, &tcls);
601     classifier_destroy(&cls);
602     tcls_destroy(&tcls);
603 }
604
605 /* Destroys a null classifier. */
606 static void
607 test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
608 {
609     classifier_destroy(NULL);
610 }
611
612 /* Tests classification with one rule at a time. */
613 static void
614 test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
615 {
616     unsigned int wc_fields;     /* Hilarious. */
617
618     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
619         struct classifier cls;
620         struct test_rule *rule, *tcls_rule;
621         struct tcls tcls;
622
623         rule = make_rule(wc_fields,
624                          hash_bytes(&wc_fields, sizeof wc_fields, 0), 0);
625
626         classifier_init(&cls);
627         tcls_init(&tcls);
628
629         tcls_rule = tcls_insert(&tcls, rule);
630         classifier_insert(&cls, &rule->cls_rule);
631         check_tables(&cls, 1, 1, 0);
632         compare_classifiers(&cls, &tcls);
633
634         classifier_remove(&cls, &rule->cls_rule);
635         tcls_remove(&tcls, tcls_rule);
636         assert(classifier_is_empty(&cls));
637         assert(tcls_is_empty(&tcls));
638         compare_classifiers(&cls, &tcls);
639
640         free_rule(rule);
641         classifier_destroy(&cls);
642         tcls_destroy(&tcls);
643     }
644 }
645
646 /* Tests replacing one rule by another. */
647 static void
648 test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
649 {
650     unsigned int wc_fields;
651
652     for (wc_fields = 0; wc_fields < (1u << CLS_N_FIELDS); wc_fields++) {
653         struct classifier cls;
654         struct test_rule *rule1;
655         struct test_rule *rule2;
656         struct tcls tcls;
657
658         rule1 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
659         rule2 = make_rule(wc_fields, OFP_DEFAULT_PRIORITY, UINT_MAX);
660         rule2->aux += 5;
661         rule2->aux += 5;
662
663         classifier_init(&cls);
664         tcls_init(&tcls);
665         tcls_insert(&tcls, rule1);
666         classifier_insert(&cls, &rule1->cls_rule);
667         check_tables(&cls, 1, 1, 0);
668         compare_classifiers(&cls, &tcls);
669         tcls_destroy(&tcls);
670
671         tcls_init(&tcls);
672         tcls_insert(&tcls, rule2);
673         assert(test_rule_from_cls_rule(
674                    classifier_replace(&cls, &rule2->cls_rule)) == rule1);
675         free_rule(rule1);
676         check_tables(&cls, 1, 1, 0);
677         compare_classifiers(&cls, &tcls);
678         tcls_destroy(&tcls);
679         destroy_classifier(&cls);
680     }
681 }
682
683 static int
684 factorial(int n_items)
685 {
686     int n, i;
687
688     n = 1;
689     for (i = 2; i <= n_items; i++) {
690         n *= i;
691     }
692     return n;
693 }
694
695 static void
696 swap(int *a, int *b)
697 {
698     int tmp = *a;
699     *a = *b;
700     *b = tmp;
701 }
702
703 static void
704 reverse(int *a, int n)
705 {
706     int i;
707
708     for (i = 0; i < n / 2; i++) {
709         int j = n - (i + 1);
710         swap(&a[i], &a[j]);
711     }
712 }
713
714 static bool
715 next_permutation(int *a, int n)
716 {
717     int k;
718
719     for (k = n - 2; k >= 0; k--) {
720         if (a[k] < a[k + 1]) {
721             int l;
722
723             for (l = n - 1; ; l--) {
724                 if (a[l] > a[k]) {
725                     swap(&a[k], &a[l]);
726                     reverse(a + (k + 1), n - (k + 1));
727                     return true;
728                 }
729             }
730         }
731     }
732     return false;
733 }
734
735 /* Tests classification with rules that have the same matching criteria. */
736 static void
737 test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
738 {
739     enum { N_RULES = 3 };
740     int n_pris;
741
742     for (n_pris = N_RULES; n_pris >= 1; n_pris--) {
743         int ops[N_RULES * 2];
744         int pris[N_RULES];
745         int n_permutations;
746         int i;
747
748         pris[0] = 0;
749         for (i = 1; i < N_RULES; i++) {
750             pris[i] = pris[i - 1] + (n_pris > i);
751         }
752
753         for (i = 0; i < N_RULES * 2; i++) {
754             ops[i] = i / 2;
755         }
756
757         n_permutations = 0;
758         do {
759             struct test_rule *rules[N_RULES];
760             struct test_rule *tcls_rules[N_RULES];
761             int pri_rules[N_RULES];
762             struct classifier cls;
763             struct tcls tcls;
764
765             n_permutations++;
766
767             for (i = 0; i < N_RULES; i++) {
768                 rules[i] = make_rule(456, pris[i], 0);
769                 tcls_rules[i] = NULL;
770                 pri_rules[i] = -1;
771             }
772
773             classifier_init(&cls);
774             tcls_init(&tcls);
775
776             for (i = 0; i < ARRAY_SIZE(ops); i++) {
777                 int j = ops[i];
778                 int m, n;
779
780                 if (!tcls_rules[j]) {
781                     struct test_rule *displaced_rule;
782
783                     tcls_rules[j] = tcls_insert(&tcls, rules[j]);
784                     displaced_rule = test_rule_from_cls_rule(
785                         classifier_replace(&cls, &rules[j]->cls_rule));
786                     if (pri_rules[pris[j]] >= 0) {
787                         int k = pri_rules[pris[j]];
788                         assert(displaced_rule != NULL);
789                         assert(displaced_rule != rules[j]);
790                         assert(pris[j] == displaced_rule->cls_rule.priority);
791                         tcls_rules[k] = NULL;
792                     } else {
793                         assert(displaced_rule == NULL);
794                     }
795                     pri_rules[pris[j]] = j;
796                 } else {
797                     classifier_remove(&cls, &rules[j]->cls_rule);
798                     tcls_remove(&tcls, tcls_rules[j]);
799                     tcls_rules[j] = NULL;
800                     pri_rules[pris[j]] = -1;
801                 }
802
803                 n = 0;
804                 for (m = 0; m < N_RULES; m++) {
805                     n += tcls_rules[m] != NULL;
806                 }
807                 check_tables(&cls, n > 0, n, n - 1);
808
809                 compare_classifiers(&cls, &tcls);
810             }
811
812             classifier_destroy(&cls);
813             tcls_destroy(&tcls);
814
815             for (i = 0; i < N_RULES; i++) {
816                 free_rule(rules[i]);
817             }
818         } while (next_permutation(ops, ARRAY_SIZE(ops)));
819         assert(n_permutations == (factorial(N_RULES * 2) >> N_RULES));
820     }
821 }
822
823 static int
824 count_ones(unsigned long int x)
825 {
826     int n = 0;
827
828     while (x) {
829         x = zero_rightmost_1bit(x);
830         n++;
831     }
832
833     return n;
834 }
835
836 static bool
837 array_contains(int *array, int n, int value)
838 {
839     int i;
840
841     for (i = 0; i < n; i++) {
842         if (array[i] == value) {
843             return true;
844         }
845     }
846
847     return false;
848 }
849
850 /* Tests classification with two rules at a time that fall into the same
851  * table but different lists. */
852 static void
853 test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
854 {
855     int iteration;
856
857     for (iteration = 0; iteration < 50; iteration++) {
858         enum { N_RULES = 20 };
859         struct test_rule *rules[N_RULES];
860         struct test_rule *tcls_rules[N_RULES];
861         struct classifier cls;
862         struct tcls tcls;
863         int value_pats[N_RULES];
864         int value_mask;
865         int wcf;
866         int i;
867
868         do {
869             wcf = rand() & ((1u << CLS_N_FIELDS) - 1);
870             value_mask = ~wcf & ((1u << CLS_N_FIELDS) - 1);
871         } while ((1 << count_ones(value_mask)) < N_RULES);
872
873         classifier_init(&cls);
874         tcls_init(&tcls);
875
876         for (i = 0; i < N_RULES; i++) {
877             unsigned int priority = rand();
878
879             do {
880                 value_pats[i] = rand() & value_mask;
881             } while (array_contains(value_pats, i, value_pats[i]));
882
883             rules[i] = make_rule(wcf, priority, value_pats[i]);
884             tcls_rules[i] = tcls_insert(&tcls, rules[i]);
885             classifier_insert(&cls, &rules[i]->cls_rule);
886
887             check_tables(&cls, 1, i + 1, 0);
888             compare_classifiers(&cls, &tcls);
889         }
890
891         for (i = 0; i < N_RULES; i++) {
892             tcls_remove(&tcls, tcls_rules[i]);
893             classifier_remove(&cls, &rules[i]->cls_rule);
894             free_rule(rules[i]);
895
896             check_tables(&cls, i < N_RULES - 1, N_RULES - (i + 1), 0);
897             compare_classifiers(&cls, &tcls);
898         }
899
900         classifier_destroy(&cls);
901         tcls_destroy(&tcls);
902     }
903 }
904
905 /* Tests classification with many rules at a time that fall into random lists
906  * in 'n' tables. */
907 static void
908 test_many_rules_in_n_tables(int n_tables)
909 {
910     enum { MAX_RULES = 50 };
911     int wcfs[10];
912     int iteration;
913     int i;
914
915     assert(n_tables < 10);
916     for (i = 0; i < n_tables; i++) {
917         do {
918             wcfs[i] = rand() & ((1u << CLS_N_FIELDS) - 1);
919         } while (array_contains(wcfs, i, wcfs[i]));
920     }
921
922     for (iteration = 0; iteration < 30; iteration++) {
923         unsigned int priorities[MAX_RULES];
924         struct classifier cls;
925         struct tcls tcls;
926
927         srand(iteration);
928         for (i = 0; i < MAX_RULES; i++) {
929             priorities[i] = i * 129;
930         }
931         shuffle(priorities, ARRAY_SIZE(priorities));
932
933         classifier_init(&cls);
934         tcls_init(&tcls);
935
936         for (i = 0; i < MAX_RULES; i++) {
937             struct test_rule *rule;
938             unsigned int priority = priorities[i];
939             int wcf = wcfs[rand() % n_tables];
940             int value_pat = rand() & ((1u << CLS_N_FIELDS) - 1);
941             rule = make_rule(wcf, priority, value_pat);
942             tcls_insert(&tcls, rule);
943             classifier_insert(&cls, &rule->cls_rule);
944             check_tables(&cls, -1, i + 1, -1);
945             compare_classifiers(&cls, &tcls);
946         }
947
948         while (!classifier_is_empty(&cls)) {
949             struct test_rule *rule, *next_rule;
950             struct test_rule *target;
951             struct cls_cursor cursor;
952
953             target = clone_rule(tcls.rules[rand() % tcls.n_rules]);
954
955             cls_cursor_init(&cursor, &cls, &target->cls_rule);
956             CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cls_rule, &cursor) {
957                 classifier_remove(&cls, &rule->cls_rule);
958                 free_rule(rule);
959             }
960             tcls_delete_matches(&tcls, &target->cls_rule);
961             compare_classifiers(&cls, &tcls);
962             check_tables(&cls, -1, -1, -1);
963             free_rule(target);
964         }
965
966         destroy_classifier(&cls);
967         tcls_destroy(&tcls);
968     }
969 }
970
971 static void
972 test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
973 {
974     test_many_rules_in_n_tables(2);
975 }
976
977 static void
978 test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
979 {
980     test_many_rules_in_n_tables(5);
981 }
982 \f
983 /* Miniflow tests. */
984
985 static uint32_t
986 random_value(void)
987 {
988     static const uint32_t values[] =
989         { 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x80000000,
990           0x00000001, 0xface0000, 0x00d00d1e, 0xdeadbeef };
991
992     return values[random_uint32() % ARRAY_SIZE(values)];
993 }
994
995 static bool
996 choose(unsigned int n, unsigned int *idxp)
997 {
998     if (*idxp < n) {
999         return true;
1000     } else {
1001         *idxp -= n;
1002         return false;
1003     }
1004 }
1005
1006 static bool
1007 init_consecutive_values(int n_consecutive, struct flow *flow,
1008                         unsigned int *idxp)
1009 {
1010     uint32_t *flow_u32 = (uint32_t *) flow;
1011
1012     if (choose(FLOW_U32S - n_consecutive + 1, idxp)) {
1013         int i;
1014
1015         for (i = 0; i < n_consecutive; i++) {
1016             flow_u32[*idxp + i] = random_value();
1017         }
1018         return true;
1019     } else {
1020         return false;
1021     }
1022 }
1023
1024 static bool
1025 next_random_flow(struct flow *flow, unsigned int idx)
1026 {
1027     uint32_t *flow_u32 = (uint32_t *) flow;
1028     int i;
1029
1030     memset(flow, 0, sizeof *flow);
1031
1032     /* Empty flow. */
1033     if (choose(1, &idx)) {
1034         return true;
1035     }
1036
1037     /* All flows with a small number of consecutive nonzero values. */
1038     for (i = 1; i <= 4; i++) {
1039         if (init_consecutive_values(i, flow, &idx)) {
1040             return true;
1041         }
1042     }
1043
1044     /* All flows with a large number of consecutive nonzero values. */
1045     for (i = FLOW_U32S - 4; i <= FLOW_U32S; i++) {
1046         if (init_consecutive_values(i, flow, &idx)) {
1047             return true;
1048         }
1049     }
1050
1051     /* All flows with exactly two nonconsecutive nonzero values. */
1052     if (choose((FLOW_U32S - 1) * (FLOW_U32S - 2) / 2, &idx)) {
1053         int ofs1;
1054
1055         for (ofs1 = 0; ofs1 < FLOW_U32S - 2; ofs1++) {
1056             int ofs2;
1057
1058             for (ofs2 = ofs1 + 2; ofs2 < FLOW_U32S; ofs2++) {
1059                 if (choose(1, &idx)) {
1060                     flow_u32[ofs1] = random_value();
1061                     flow_u32[ofs2] = random_value();
1062                     return true;
1063                 }
1064             }
1065         }
1066         NOT_REACHED();
1067     }
1068
1069     /* 16 randomly chosen flows with N >= 3 nonzero values. */
1070     if (choose(16 * (FLOW_U32S - 4), &idx)) {
1071         int n = idx / 16 + 3;
1072         int i;
1073
1074         for (i = 0; i < n; i++) {
1075             flow_u32[i] = random_value();
1076         }
1077         shuffle_u32s(flow_u32, FLOW_U32S);
1078
1079         return true;
1080     }
1081
1082     return false;
1083 }
1084
1085 static void
1086 any_random_flow(struct flow *flow)
1087 {
1088     static unsigned int max;
1089     if (!max) {
1090         while (next_random_flow(flow, max)) {
1091             max++;
1092         }
1093     }
1094
1095     next_random_flow(flow, random_range(max));
1096 }
1097
1098 static void
1099 toggle_masked_flow_bits(struct flow *flow, const struct flow_wildcards *mask)
1100 {
1101     const uint32_t *mask_u32 = (const uint32_t *) &mask->masks;
1102     uint32_t *flow_u32 = (uint32_t *) flow;
1103     int i;
1104
1105     for (i = 0; i < FLOW_U32S; i++) {
1106         if (mask_u32[i] != 0) {
1107             uint32_t bit;
1108
1109             do {
1110                 bit = 1u << random_range(32);
1111             } while (!(bit & mask_u32[i]));
1112             flow_u32[i] ^= bit;
1113         }
1114     }
1115 }
1116
1117 static void
1118 wildcard_extra_bits(struct flow_wildcards *mask)
1119 {
1120     uint32_t *mask_u32 = (uint32_t *) &mask->masks;
1121     int i;
1122
1123     for (i = 0; i < FLOW_U32S; i++) {
1124         if (mask_u32[i] != 0) {
1125             uint32_t bit;
1126
1127             do {
1128                 bit = 1u << random_range(32);
1129             } while (!(bit & mask_u32[i]));
1130             mask_u32[i] &= ~bit;
1131         }
1132     }
1133 }
1134
1135 static void
1136 test_miniflow(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1137 {
1138     struct flow flow;
1139     unsigned int idx;
1140
1141     random_set_seed(0xb3faca38);
1142     for (idx = 0; next_random_flow(&flow, idx); idx++) {
1143         const uint32_t *flow_u32 = (const uint32_t *) &flow;
1144         struct miniflow miniflow, miniflow2, miniflow3;
1145         struct flow flow2, flow3;
1146         struct flow_wildcards mask;
1147         struct minimask minimask;
1148         int i;
1149
1150         /* Convert flow to miniflow. */
1151         miniflow_init(&miniflow, &flow);
1152
1153         /* Check that the flow equals its miniflow. */
1154         assert(miniflow_get_vid(&miniflow) == vlan_tci_to_vid(flow.vlan_tci));
1155         for (i = 0; i < FLOW_U32S; i++) {
1156             assert(miniflow_get(&miniflow, i) == flow_u32[i]);
1157         }
1158
1159         /* Check that the miniflow equals itself. */
1160         assert(miniflow_equal(&miniflow, &miniflow));
1161
1162         /* Convert miniflow back to flow and verify that it's the same. */
1163         miniflow_expand(&miniflow, &flow2);
1164         assert(flow_equal(&flow, &flow2));
1165
1166         /* Check that copying a miniflow works properly. */
1167         miniflow_clone(&miniflow2, &miniflow);
1168         assert(miniflow_equal(&miniflow, &miniflow2));
1169         assert(miniflow_hash(&miniflow, 0) == miniflow_hash(&miniflow2, 0));
1170         miniflow_expand(&miniflow2, &flow3);
1171         assert(flow_equal(&flow, &flow3));
1172
1173         /* Check that masked matches work as expected for identical flows and
1174          * miniflows. */
1175         do {
1176             next_random_flow(&mask.masks, 1);
1177         } while (flow_wildcards_is_catchall(&mask));
1178         minimask_init(&minimask, &mask);
1179         assert(minimask_is_catchall(&minimask)
1180                == flow_wildcards_is_catchall(&mask));
1181         assert(miniflow_equal_in_minimask(&miniflow, &miniflow2, &minimask));
1182         assert(miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1183         assert(miniflow_hash_in_minimask(&miniflow, &minimask, 0x12345678) ==
1184                flow_hash_in_minimask(&flow, &minimask, 0x12345678));
1185
1186         /* Check that masked matches work as expected for differing flows and
1187          * miniflows. */
1188         toggle_masked_flow_bits(&flow2, &mask);
1189         assert(!miniflow_equal_flow_in_minimask(&miniflow, &flow2, &minimask));
1190         miniflow_init(&miniflow3, &flow2);
1191         assert(!miniflow_equal_in_minimask(&miniflow, &miniflow3, &minimask));
1192
1193         /* Clean up. */
1194         miniflow_destroy(&miniflow);
1195         miniflow_destroy(&miniflow2);
1196         miniflow_destroy(&miniflow3);
1197         minimask_destroy(&minimask);
1198     }
1199 }
1200
1201 static void
1202 test_minimask_has_extra(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1203 {
1204     struct flow_wildcards catchall;
1205     struct minimask minicatchall;
1206     struct flow flow;
1207     unsigned int idx;
1208
1209     flow_wildcards_init_catchall(&catchall);
1210     minimask_init(&minicatchall, &catchall);
1211     assert(minimask_is_catchall(&minicatchall));
1212
1213     random_set_seed(0x2ec7905b);
1214     for (idx = 0; next_random_flow(&flow, idx); idx++) {
1215         struct flow_wildcards mask;
1216         struct minimask minimask;
1217
1218         mask.masks = flow;
1219         minimask_init(&minimask, &mask);
1220         assert(!minimask_has_extra(&minimask, &minimask));
1221         assert(minimask_has_extra(&minicatchall, &minimask)
1222                == !minimask_is_catchall(&minimask));
1223         if (!minimask_is_catchall(&minimask)) {
1224             struct minimask minimask2;
1225
1226             wildcard_extra_bits(&mask);
1227             minimask_init(&minimask2, &mask);
1228             assert(minimask_has_extra(&minimask2, &minimask));
1229             assert(!minimask_has_extra(&minimask, &minimask2));
1230             minimask_destroy(&minimask2);
1231         }
1232
1233         minimask_destroy(&minimask);
1234     }
1235
1236     minimask_destroy(&minicatchall);
1237 }
1238
1239 static void
1240 test_minimask_combine(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1241 {
1242     struct flow_wildcards catchall;
1243     struct minimask minicatchall;
1244     struct flow flow;
1245     unsigned int idx;
1246
1247     flow_wildcards_init_catchall(&catchall);
1248     minimask_init(&minicatchall, &catchall);
1249     assert(minimask_is_catchall(&minicatchall));
1250
1251     random_set_seed(0x181bf0cd);
1252     for (idx = 0; next_random_flow(&flow, idx); idx++) {
1253         struct minimask minimask, minimask2, minicombined;
1254         struct flow_wildcards mask, mask2, combined, combined2;
1255         uint32_t storage[FLOW_U32S];
1256         struct flow flow2;
1257
1258         mask.masks = flow;
1259         minimask_init(&minimask, &mask);
1260
1261         minimask_combine(&minicombined, &minimask, &minicatchall, storage);
1262         assert(minimask_is_catchall(&minicombined));
1263
1264         any_random_flow(&flow2);
1265         mask2.masks = flow2;
1266         minimask_init(&minimask2, &mask2);
1267
1268         minimask_combine(&minicombined, &minimask, &minimask2, storage);
1269         flow_wildcards_combine(&combined, &mask, &mask2);
1270         minimask_expand(&minicombined, &combined2);
1271         assert(flow_wildcards_equal(&combined, &combined2));
1272
1273         minimask_destroy(&minimask);
1274         minimask_destroy(&minimask2);
1275     }
1276
1277     minimask_destroy(&minicatchall);
1278 }
1279 \f
1280 static const struct command commands[] = {
1281     /* Classifier tests. */
1282     {"empty", 0, 0, test_empty},
1283     {"destroy-null", 0, 0, test_destroy_null},
1284     {"single-rule", 0, 0, test_single_rule},
1285     {"rule-replacement", 0, 0, test_rule_replacement},
1286     {"many-rules-in-one-list", 0, 0, test_many_rules_in_one_list},
1287     {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
1288     {"many-rules-in-two-tables", 0, 0, test_many_rules_in_two_tables},
1289     {"many-rules-in-five-tables", 0, 0, test_many_rules_in_five_tables},
1290
1291     /* Miniflow and minimask tests. */
1292     {"miniflow", 0, 0, test_miniflow},
1293         {"minimask_has_extra", 0, 0, test_minimask_has_extra},
1294         {"minimask_combine", 0, 0, test_minimask_combine},
1295
1296     {NULL, 0, 0, NULL},
1297 };
1298
1299 int
1300 main(int argc, char *argv[])
1301 {
1302     set_program_name(argv[0]);
1303     init_values();
1304     run_command(argc - 1, argv + 1, commands);
1305     return 0;
1306 }