]>
Commit | Line | Data |
---|---|---|
3bffc610 | 1 | /* |
316bd0f8 | 2 | * Copyright (c) 2011, 2012, 2013 Nicira, Inc. |
3bffc610 BP |
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 "odp-util.h" | |
24 | #include "ofpbuf.h" | |
eb1b39b3 | 25 | #include "util.h" |
b0f7b9b5 | 26 | #include "vlog.h" |
3bffc610 | 27 | |
eb1b39b3 | 28 | static int |
e6cc0bab | 29 | parse_keys(bool wc_keys) |
3bffc610 | 30 | { |
2508ac16 | 31 | int exit_code = 0; |
3bffc610 BP |
32 | struct ds in; |
33 | ||
34 | ds_init(&in); | |
316bd0f8 | 35 | vlog_set_levels_from_string_assert("odp_util:console:dbg"); |
06d7ae7d | 36 | while (!ds_get_test_line(&in, stdin)) { |
b0f7b9b5 | 37 | enum odp_key_fitness fitness; |
3bffc610 | 38 | struct ofpbuf odp_key; |
e6cc0bab | 39 | struct ofpbuf odp_mask; |
3bffc610 BP |
40 | struct flow flow; |
41 | struct ds out; | |
42 | int error; | |
3bffc610 | 43 | |
df2c07f4 | 44 | /* Convert string to OVS DP key. */ |
3bffc610 | 45 | ofpbuf_init(&odp_key, 0); |
e6cc0bab AZ |
46 | ofpbuf_init(&odp_mask, 0); |
47 | error = odp_flow_from_string(ds_cstr(&in), NULL, | |
48 | &odp_key, &odp_mask); | |
3bffc610 | 49 | if (error) { |
e6cc0bab | 50 | printf("odp_flow_from_string: error\n"); |
3bffc610 BP |
51 | goto next; |
52 | } | |
53 | ||
e6cc0bab AZ |
54 | if (!wc_keys) { |
55 | /* Convert odp_key to flow. */ | |
56 | fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow); | |
57 | switch (fitness) { | |
58 | case ODP_FIT_PERFECT: | |
59 | break; | |
60 | ||
61 | case ODP_FIT_TOO_LITTLE: | |
62 | printf("ODP_FIT_TOO_LITTLE: "); | |
63 | break; | |
64 | ||
65 | case ODP_FIT_TOO_MUCH: | |
66 | printf("ODP_FIT_TOO_MUCH: "); | |
67 | break; | |
68 | ||
69 | case ODP_FIT_ERROR: | |
70 | printf("odp_flow_key_to_flow: error\n"); | |
71 | goto next; | |
72 | } | |
73 | /* Convert cls_rule back to odp_key. */ | |
74 | ofpbuf_uninit(&odp_key); | |
75 | ofpbuf_init(&odp_key, 0); | |
4e022ec0 | 76 | odp_flow_key_from_flow(&odp_key, &flow, flow.in_port.odp_port); |
e6cc0bab AZ |
77 | |
78 | if (odp_key.size > ODPUTIL_FLOW_KEY_BYTES) { | |
79 | printf ("too long: %zu > %d\n", | |
80 | odp_key.size, ODPUTIL_FLOW_KEY_BYTES); | |
81 | exit_code = 1; | |
82 | } | |
2508ac16 BP |
83 | } |
84 | ||
3bffc610 BP |
85 | /* Convert odp_key to string. */ |
86 | ds_init(&out); | |
e6cc0bab AZ |
87 | if (wc_keys) { |
88 | odp_flow_format(odp_key.data, odp_key.size, | |
89 | odp_mask.data, odp_mask.size, &out); | |
90 | } else { | |
91 | odp_flow_key_format(odp_key.data, odp_key.size, &out); | |
92 | } | |
3bffc610 BP |
93 | puts(ds_cstr(&out)); |
94 | ds_destroy(&out); | |
95 | ||
96 | next: | |
97 | ofpbuf_uninit(&odp_key); | |
98 | } | |
99 | ds_destroy(&in); | |
100 | ||
2508ac16 | 101 | return exit_code; |
3bffc610 | 102 | } |
eb1b39b3 BP |
103 | |
104 | static int | |
105 | parse_actions(void) | |
106 | { | |
107 | struct ds in; | |
108 | ||
109 | ds_init(&in); | |
316bd0f8 | 110 | vlog_set_levels_from_string_assert("odp_util:console:dbg"); |
eb1b39b3 BP |
111 | while (!ds_get_test_line(&in, stdin)) { |
112 | struct ofpbuf odp_actions; | |
113 | struct ds out; | |
114 | int error; | |
115 | ||
116 | /* Convert string to OVS DP actions. */ | |
117 | ofpbuf_init(&odp_actions, 0); | |
118 | error = odp_actions_from_string(ds_cstr(&in), NULL, &odp_actions); | |
119 | if (error) { | |
120 | printf("odp_actions_from_string: error\n"); | |
121 | goto next; | |
122 | } | |
123 | ||
124 | /* Convert odp_actions back to string. */ | |
125 | ds_init(&out); | |
126 | format_odp_actions(&out, odp_actions.data, odp_actions.size); | |
127 | puts(ds_cstr(&out)); | |
128 | ds_destroy(&out); | |
129 | ||
130 | next: | |
131 | ofpbuf_uninit(&odp_actions); | |
132 | } | |
133 | ds_destroy(&in); | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
138 | int | |
139 | main(int argc, char *argv[]) | |
140 | { | |
141 | if (argc == 2 &&!strcmp(argv[1], "parse-keys")) { | |
e6cc0bab AZ |
142 | return parse_keys(false); |
143 | } else if (argc == 2 &&!strcmp(argv[1], "parse-wc-keys")) { | |
144 | return parse_keys(true); | |
eb1b39b3 BP |
145 | } else if (argc == 2 && !strcmp(argv[1], "parse-actions")) { |
146 | return parse_actions(); | |
147 | } else { | |
e6cc0bab | 148 | ovs_fatal(0, "usage: %s parse-keys | parse-wc-keys | parse-actions", argv[0]); |
eb1b39b3 BP |
149 | } |
150 | } |