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