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