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