2 * Copyright (c) 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "openvswitch/dynamic-string.h"
23 #include "openvswitch/match.h"
24 #include "openvswitch/ofpbuf.h"
27 #include "openvswitch/ofp-parse.h"
28 #include "openvswitch/vlog.h"
31 parse_keys(bool wc_keys
)
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
;
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
,
52 printf("odp_flow_from_string: error\n");
57 struct odp_flow_key_parms odp_parms
= {
65 .max_vlan_headers
= SIZE_MAX
,
69 /* Convert odp_key to flow. */
70 fitness
= odp_flow_key_to_flow(odp_key
.data
, odp_key
.size
, &flow
);
75 case ODP_FIT_TOO_LITTLE
:
76 printf("ODP_FIT_TOO_LITTLE: ");
79 case ODP_FIT_TOO_MUCH
:
80 printf("ODP_FIT_TOO_MUCH: ");
84 printf("odp_flow_key_to_flow: error\n");
87 /* Convert cls_rule back to odp_key. */
88 ofpbuf_uninit(&odp_key
);
89 ofpbuf_init(&odp_key
, 0);
90 odp_flow_key_from_flow(&odp_parms
, &odp_key
);
92 if (odp_key
.size
> ODPUTIL_FLOW_KEY_BYTES
) {
93 printf ("too long: %"PRIu32
" > %d\n",
94 odp_key
.size
, ODPUTIL_FLOW_KEY_BYTES
);
99 /* Convert odp_key to string. */
102 odp_flow_format(odp_key
.data
, odp_key
.size
,
103 odp_mask
.data
, odp_mask
.size
, NULL
, &out
, false);
105 odp_flow_key_format(odp_key
.data
, odp_key
.size
, &out
);
111 ofpbuf_uninit(&odp_key
);
112 ofpbuf_uninit(&odp_mask
);
125 vlog_set_levels_from_string_assert("odp_util:console:dbg");
126 while (!ds_get_test_line(&in
, stdin
)) {
127 struct ofpbuf odp_actions
;
131 /* Convert string to OVS DP actions. */
132 ofpbuf_init(&odp_actions
, 0);
133 error
= odp_actions_from_string(ds_cstr(&in
), NULL
, &odp_actions
);
135 printf("odp_actions_from_string: error\n");
139 /* Convert odp_actions back to string. */
141 format_odp_actions(&out
, odp_actions
.data
, odp_actions
.size
, NULL
);
146 ofpbuf_uninit(&odp_actions
);
154 parse_filter(char *filter_parse
)
157 struct flow flow_filter
;
158 struct flow_wildcards wc_filter
;
159 char *error
, *filter
= NULL
;
161 vlog_set_levels_from_string_assert("odp_util:console:dbg");
162 if (filter_parse
&& !strncmp(filter_parse
, "filter=", 7)) {
163 filter
= xstrdup(filter_parse
+ 7);
164 memset(&flow_filter
, 0, sizeof(flow_filter
));
165 memset(&wc_filter
, 0, sizeof(wc_filter
));
167 error
= parse_ofp_exact_flow(&flow_filter
, &wc_filter
, NULL
, filter
,
170 ovs_fatal(0, "Failed to parse filter (%s)", error
);
173 ovs_fatal(0, "No filter to parse.");
177 while (!ds_get_test_line(&in
, stdin
)) {
178 struct ofpbuf odp_key
;
179 struct ofpbuf odp_mask
;
182 /* Convert string to OVS DP key. */
183 ofpbuf_init(&odp_key
, 0);
184 ofpbuf_init(&odp_mask
, 0);
185 if (odp_flow_from_string(ds_cstr(&in
), NULL
, &odp_key
, &odp_mask
)) {
186 printf("odp_flow_from_string: error\n");
192 struct flow_wildcards wc
;
193 struct match match
, match_filter
;
194 struct minimatch minimatch
;
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
, &wc
, &flow
);
198 match_init(&match
, &flow
, &wc
);
200 match_init(&match_filter
, &flow_filter
, &wc
);
201 match_init(&match_filter
, &match_filter
.flow
, &wc_filter
);
202 minimatch_init(&minimatch
, &match_filter
);
204 if (!minimatch_matches_flow(&minimatch
, &match
.flow
)) {
205 minimatch_destroy(&minimatch
);
208 minimatch_destroy(&minimatch
);
210 /* Convert odp_key to string. */
212 odp_flow_format(odp_key
.data
, odp_key
.size
,
213 odp_mask
.data
, odp_mask
.size
, NULL
, &out
, false);
218 ofpbuf_uninit(&odp_key
);
219 ofpbuf_uninit(&odp_mask
);
228 test_odp_main(int argc
, char *argv
[])
232 set_program_name(argv
[0]);
233 if (argc
== 2 &&!strcmp(argv
[1], "parse-keys")) {
234 exit_code
=parse_keys(false);
235 } else if (argc
== 2 &&!strcmp(argv
[1], "parse-wc-keys")) {
236 exit_code
=parse_keys(true);
237 } else if (argc
== 2 && !strcmp(argv
[1], "parse-actions")) {
238 exit_code
= parse_actions();
239 } else if (argc
== 3 && !strcmp(argv
[1], "parse-filter")) {
240 exit_code
=parse_filter(argv
[2]);
242 ovs_fatal(0, "usage: %s parse-keys | parse-wc-keys | parse-actions", argv
[0]);
248 OVSTEST_REGISTER("test-odp", test_odp_main
);