3e7939e5ba80e6379dfd3cf709a4daa333f7bfee
[cascardo/ovs.git] / tests / test-odp.c
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2014 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 #undef NDEBUG
19 #include "odp-util.h"
20 #include <stdio.h>
21 #include "dynamic-string.h"
22 #include "flow.h"
23 #include "match.h"
24 #include "ofp-parse.h"
25 #include "ofpbuf.h"
26 #include "ovstest.h"
27 #include "util.h"
28 #include "openvswitch/vlog.h"
29
30 static int
31 parse_keys(bool wc_keys)
32 {
33     int exit_code = 0;
34     struct ds in;
35
36     ds_init(&in);
37     vlog_set_levels_from_string_assert("odp_util:console:dbg");
38     while (!ds_get_test_line(&in, stdin)) {
39         enum odp_key_fitness fitness;
40         struct ofpbuf odp_key;
41         struct ofpbuf odp_mask;
42         struct flow flow;
43         struct ds out;
44         int error;
45
46         /* Convert string to OVS DP key. */
47         ofpbuf_init(&odp_key, 0);
48         ofpbuf_init(&odp_mask, 0);
49         error = odp_flow_from_string(ds_cstr(&in), NULL,
50                                      &odp_key, &odp_mask);
51         if (error) {
52             printf("odp_flow_from_string: error\n");
53             goto next;
54         }
55
56         if (!wc_keys) {
57             struct odp_flow_key_parms odp_parms = {
58                 .flow = &flow,
59                 .support = {
60                     .recirc = true,
61                 },
62             };
63
64             /* Convert odp_key to flow. */
65             fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
66             switch (fitness) {
67                 case ODP_FIT_PERFECT:
68                     break;
69
70                 case ODP_FIT_TOO_LITTLE:
71                     printf("ODP_FIT_TOO_LITTLE: ");
72                     break;
73
74                 case ODP_FIT_TOO_MUCH:
75                     printf("ODP_FIT_TOO_MUCH: ");
76                     break;
77
78                 case ODP_FIT_ERROR:
79                     printf("odp_flow_key_to_flow: error\n");
80                     goto next;
81             }
82             /* Convert cls_rule back to odp_key. */
83             ofpbuf_uninit(&odp_key);
84             ofpbuf_init(&odp_key, 0);
85             odp_parms.odp_in_port = flow.in_port.odp_port;
86             odp_flow_key_from_flow(&odp_parms, &odp_key);
87
88             if (odp_key.size > ODPUTIL_FLOW_KEY_BYTES) {
89                 printf ("too long: %"PRIu32" > %d\n",
90                         odp_key.size, ODPUTIL_FLOW_KEY_BYTES);
91                 exit_code = 1;
92             }
93         }
94
95         /* Convert odp_key to string. */
96         ds_init(&out);
97         if (wc_keys) {
98             odp_flow_format(odp_key.data, odp_key.size,
99                             odp_mask.data, odp_mask.size, NULL, &out, false);
100         } else {
101             odp_flow_key_format(odp_key.data, odp_key.size, &out);
102         }
103         puts(ds_cstr(&out));
104         ds_destroy(&out);
105
106     next:
107         ofpbuf_uninit(&odp_key);
108     }
109     ds_destroy(&in);
110
111     return exit_code;
112 }
113
114 static int
115 parse_actions(void)
116 {
117     struct ds in;
118
119     ds_init(&in);
120     vlog_set_levels_from_string_assert("odp_util:console:dbg");
121     while (!ds_get_test_line(&in, stdin)) {
122         struct ofpbuf odp_actions;
123         struct ds out;
124         int error;
125
126         /* Convert string to OVS DP actions. */
127         ofpbuf_init(&odp_actions, 0);
128         error = odp_actions_from_string(ds_cstr(&in), NULL, &odp_actions);
129         if (error) {
130             printf("odp_actions_from_string: error\n");
131             goto next;
132         }
133
134         /* Convert odp_actions back to string. */
135         ds_init(&out);
136         format_odp_actions(&out, odp_actions.data, odp_actions.size);
137         puts(ds_cstr(&out));
138         ds_destroy(&out);
139
140     next:
141         ofpbuf_uninit(&odp_actions);
142     }
143     ds_destroy(&in);
144
145     return 0;
146 }
147
148 static int
149 parse_filter(char *filter_parse)
150 {
151     struct ds in;
152     struct flow flow_filter;
153     struct flow_wildcards wc_filter;
154     char *error, *filter = NULL;
155
156     vlog_set_levels_from_string_assert("odp_util:console:dbg");
157     if (filter_parse && !strncmp(filter_parse, "filter=", 7)) {
158         filter = xstrdup(filter_parse + 7);
159         memset(&flow_filter, 0, sizeof(flow_filter));
160         memset(&wc_filter, 0, sizeof(wc_filter));
161
162         error = parse_ofp_exact_flow(&flow_filter, &wc_filter.masks, filter,
163                                      NULL);
164         if (error) {
165             ovs_fatal(0, "Failed to parse filter (%s)", error);
166         }
167     } else {
168         ovs_fatal(0, "No filter to parse.");
169     }
170
171     ds_init(&in);
172     while (!ds_get_test_line(&in, stdin)) {
173         struct ofpbuf odp_key;
174         struct ofpbuf odp_mask;
175         struct ds out;
176         int error;
177
178         /* Convert string to OVS DP key. */
179         ofpbuf_init(&odp_key, 0);
180         ofpbuf_init(&odp_mask, 0);
181         error = odp_flow_from_string(ds_cstr(&in), NULL,
182                                      &odp_key, &odp_mask);
183         if (error) {
184             printf("odp_flow_from_string: error\n");
185             goto next;
186         }
187
188         if (filter) {
189             struct flow flow;
190             struct flow_wildcards wc;
191             struct match match, match_filter;
192             struct minimatch minimatch;
193
194             odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
195             odp_flow_key_to_mask(odp_mask.data, odp_mask.size, odp_key.data,
196                                  odp_key.size, &wc.masks, &flow);
197             match_init(&match, &flow, &wc);
198
199             match_init(&match_filter, &flow_filter, &wc);
200             match_init(&match_filter, &match_filter.flow, &wc_filter);
201             minimatch_init(&minimatch, &match_filter);
202
203             if (!minimatch_matches_flow(&minimatch, &match.flow)) {
204                 minimatch_destroy(&minimatch);
205                 goto next;
206             }
207             minimatch_destroy(&minimatch);
208         }
209         /* Convert odp_key to string. */
210         ds_init(&out);
211         odp_flow_format(odp_key.data, odp_key.size,
212                         odp_mask.data, odp_mask.size, NULL, &out, false);
213         puts(ds_cstr(&out));
214         ds_destroy(&out);
215
216     next:
217         ofpbuf_uninit(&odp_key);
218         ofpbuf_uninit(&odp_mask);
219     }
220     ds_destroy(&in);
221
222     free(filter);
223     return 0;
224 }
225
226 static void
227 test_odp_main(int argc, char *argv[])
228 {
229     int exit_code = 0;
230
231     set_program_name(argv[0]);
232     if (argc == 2 &&!strcmp(argv[1], "parse-keys")) {
233         exit_code =parse_keys(false);
234     } else if (argc == 2 &&!strcmp(argv[1], "parse-wc-keys")) {
235         exit_code =parse_keys(true);
236     } else if (argc == 2 && !strcmp(argv[1], "parse-actions")) {
237         exit_code = parse_actions();
238     } else if (argc == 3 && !strcmp(argv[1], "parse-filter")) {
239         exit_code =parse_filter(argv[2]);
240     } else {
241         ovs_fatal(0, "usage: %s parse-keys | parse-wc-keys | parse-actions", argv[0]);
242     }
243
244     exit(exit_code);
245 }
246
247 OVSTEST_REGISTER("test-odp", test_odp_main);