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