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