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