268a10518749801910d531b64c06da327e7cf655
[sliver-openvswitch.git] / tests / test-odp.c
1 /*
2  * Copyright (c) 2011, 2012, 2013 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 #include <config.h>
18
19 #include <stdio.h>
20
21 #include "dynamic-string.h"
22 #include "flow.h"
23 #include "odp-util.h"
24 #include "ofpbuf.h"
25 #include "util.h"
26 #include "vlog.h"
27
28 static int
29 parse_keys(void)
30 {
31     int exit_code = 0;
32     struct ds in;
33
34     ds_init(&in);
35     vlog_set_levels_from_string_assert("odp_util:console:dbg");
36     while (!ds_get_test_line(&in, stdin)) {
37         enum odp_key_fitness fitness;
38         struct ofpbuf odp_key;
39         struct flow flow;
40         struct ds out;
41         int error;
42
43         /* Convert string to OVS DP key. */
44         ofpbuf_init(&odp_key, 0);
45         error = odp_flow_key_from_string(ds_cstr(&in), NULL, &odp_key);
46         if (error) {
47             printf("odp_flow_key_from_string: error\n");
48             goto next;
49         }
50
51         /* Convert odp_key to flow. */
52         fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
53         switch (fitness) {
54         case ODP_FIT_PERFECT:
55             break;
56
57         case ODP_FIT_TOO_LITTLE:
58             printf("ODP_FIT_TOO_LITTLE: ");
59             break;
60
61         case ODP_FIT_TOO_MUCH:
62             printf("ODP_FIT_TOO_MUCH: ");
63             break;
64
65         case ODP_FIT_ERROR:
66             printf("odp_flow_key_to_flow: error\n");
67             goto next;
68         }
69
70         /* Convert cls_rule back to odp_key. */
71         ofpbuf_uninit(&odp_key);
72         ofpbuf_init(&odp_key, 0);
73         odp_flow_key_from_flow(&odp_key, &flow, flow.in_port);
74
75         if (odp_key.size > ODPUTIL_FLOW_KEY_BYTES) {
76             printf ("too long: %zu > %d\n",
77                     odp_key.size, ODPUTIL_FLOW_KEY_BYTES);
78             exit_code = 1;
79         }
80
81         /* Convert odp_key to string. */
82         ds_init(&out);
83         odp_flow_key_format(odp_key.data, odp_key.size, &out);
84         puts(ds_cstr(&out));
85         ds_destroy(&out);
86
87     next:
88         ofpbuf_uninit(&odp_key);
89     }
90     ds_destroy(&in);
91
92     return exit_code;
93 }
94
95 static int
96 parse_actions(void)
97 {
98     struct ds in;
99
100     ds_init(&in);
101     vlog_set_levels_from_string_assert("odp_util:console:dbg");
102     while (!ds_get_test_line(&in, stdin)) {
103         struct ofpbuf odp_actions;
104         struct ds out;
105         int error;
106
107         /* Convert string to OVS DP actions. */
108         ofpbuf_init(&odp_actions, 0);
109         error = odp_actions_from_string(ds_cstr(&in), NULL, &odp_actions);
110         if (error) {
111             printf("odp_actions_from_string: error\n");
112             goto next;
113         }
114
115         /* Convert odp_actions back to string. */
116         ds_init(&out);
117         format_odp_actions(&out, odp_actions.data, odp_actions.size);
118         puts(ds_cstr(&out));
119         ds_destroy(&out);
120
121     next:
122         ofpbuf_uninit(&odp_actions);
123     }
124     ds_destroy(&in);
125
126     return 0;
127 }
128
129 int
130 main(int argc, char *argv[])
131 {
132     if (argc == 2 &&!strcmp(argv[1], "parse-keys")) {
133         return parse_keys();
134     } else if (argc == 2 && !strcmp(argv[1], "parse-actions")) {
135         return parse_actions();
136     } else {
137         ovs_fatal(0, "usage: %s parse-keys | parse-actions", argv[0]);
138     }
139 }