/src/openvswitch/lib/bundle.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc. |
2 | | * |
3 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | * you may not use this file except in compliance with the License. |
5 | | * You may obtain a copy of the License at: |
6 | | * |
7 | | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | | * |
9 | | * Unless required by applicable law or agreed to in writing, software |
10 | | * distributed under the License is distributed on an "AS IS" BASIS, |
11 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | * See the License for the specific language governing permissions and |
13 | | * limitations under the License. |
14 | | */ |
15 | | |
16 | | #include <config.h> |
17 | | |
18 | | #include "bundle.h" |
19 | | |
20 | | #include <sys/types.h> |
21 | | #include <netinet/in.h> |
22 | | #include <arpa/inet.h> |
23 | | #include <inttypes.h> |
24 | | |
25 | | #include "colors.h" |
26 | | #include "multipath.h" |
27 | | #include "nx-match.h" |
28 | | #include "openflow/nicira-ext.h" |
29 | | #include "openvswitch/dynamic-string.h" |
30 | | #include "openvswitch/meta-flow.h" |
31 | | #include "openvswitch/ofp-actions.h" |
32 | | #include "openvswitch/ofp-errors.h" |
33 | | #include "openvswitch/ofp-port.h" |
34 | | #include "openvswitch/ofpbuf.h" |
35 | | #include "openvswitch/vlog.h" |
36 | | #include "util.h" |
37 | | |
38 | | VLOG_DEFINE_THIS_MODULE(bundle); |
39 | | |
40 | | static ofp_port_t |
41 | | execute_ab(const struct ofpact_bundle *bundle, |
42 | | bool (*member_enabled)(ofp_port_t ofp_port, void *aux), void *aux) |
43 | 0 | { |
44 | 0 | size_t i; |
45 | |
|
46 | 0 | for (i = 0; i < bundle->n_members; i++) { |
47 | 0 | ofp_port_t member = bundle->members[i]; |
48 | 0 | if (member_enabled(member, aux)) { |
49 | 0 | return member; |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | 0 | return OFPP_NONE; |
54 | 0 | } |
55 | | |
56 | | static ofp_port_t |
57 | | execute_hrw(const struct ofpact_bundle *bundle, |
58 | | const struct flow *flow, struct flow_wildcards *wc, |
59 | | bool (*member_enabled)(ofp_port_t ofp_port, void *aux), void *aux) |
60 | 0 | { |
61 | 0 | uint32_t flow_hash, best_hash; |
62 | 0 | int best, i; |
63 | |
|
64 | 0 | if (bundle->n_members > 1) { |
65 | 0 | flow_mask_hash_fields(flow, wc, bundle->fields); |
66 | 0 | } |
67 | |
|
68 | 0 | flow_hash = flow_hash_fields(flow, bundle->fields, bundle->basis); |
69 | 0 | best = -1; |
70 | 0 | best_hash = 0; |
71 | |
|
72 | 0 | for (i = 0; i < bundle->n_members; i++) { |
73 | 0 | if (member_enabled(bundle->members[i], aux)) { |
74 | 0 | uint32_t hash = hash_2words(i, flow_hash); |
75 | |
|
76 | 0 | if (best < 0 || hash > best_hash) { |
77 | 0 | best_hash = hash; |
78 | 0 | best = i; |
79 | 0 | } |
80 | 0 | } |
81 | 0 | } |
82 | |
|
83 | 0 | return best >= 0 ? bundle->members[best] : OFPP_NONE; |
84 | 0 | } |
85 | | |
86 | | /* Executes 'bundle' on 'flow'. Sets fields in 'wc' that were used to |
87 | | * calculate the result. Uses 'member_enabled' to determine if the member |
88 | | * designated by 'ofp_port' is up. Returns the chosen member, or |
89 | | * OFPP_NONE if none of the members are acceptable. */ |
90 | | ofp_port_t |
91 | | bundle_execute(const struct ofpact_bundle *bundle, |
92 | | const struct flow *flow, struct flow_wildcards *wc, |
93 | | bool (*member_enabled)(ofp_port_t ofp_port, void *aux), |
94 | | void *aux) |
95 | 0 | { |
96 | 0 | switch (bundle->algorithm) { |
97 | 0 | case NX_BD_ALG_HRW: |
98 | 0 | return execute_hrw(bundle, flow, wc, member_enabled, aux); |
99 | | |
100 | 0 | case NX_BD_ALG_ACTIVE_BACKUP: |
101 | 0 | return execute_ab(bundle, member_enabled, aux); |
102 | | |
103 | 0 | default: |
104 | 0 | OVS_NOT_REACHED(); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | enum ofperr |
109 | | bundle_check(const struct ofpact_bundle *bundle, ofp_port_t max_ports, |
110 | | const struct match *match) |
111 | 2.38k | { |
112 | 2.38k | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
113 | 2.38k | size_t i; |
114 | | |
115 | 2.38k | if (bundle->dst.field) { |
116 | 1.15k | enum ofperr error = mf_check_dst(&bundle->dst, match); |
117 | 1.15k | if (error) { |
118 | 16 | return error; |
119 | 16 | } |
120 | 1.15k | } |
121 | | |
122 | 21.6k | for (i = 0; i < bundle->n_members; i++) { |
123 | 19.5k | ofp_port_t ofp_port = bundle->members[i]; |
124 | | |
125 | 19.5k | if (ofp_port != OFPP_NONE) { |
126 | 14.8k | enum ofperr error = ofpact_check_output_port(ofp_port, max_ports); |
127 | 14.8k | if (error) { |
128 | 262 | VLOG_WARN_RL(&rl, "invalid member %"PRIu32, ofp_port); |
129 | 262 | return error; |
130 | 262 | } |
131 | 14.8k | } |
132 | | /* Controller members are unsupported due to the lack of a max_len |
133 | | * argument. This may or may not change in the future. There doesn't |
134 | | * seem to be a real-world use-case for supporting it. */ |
135 | 19.2k | if (ofp_port == OFPP_CONTROLLER) { |
136 | 10 | VLOG_WARN_RL(&rl, "unsupported controller member"); |
137 | 10 | return OFPERR_OFPBAC_BAD_OUT_PORT; |
138 | 10 | } |
139 | 19.2k | } |
140 | | |
141 | 2.09k | return 0; |
142 | 2.37k | } |
143 | | |
144 | | |
145 | | /* Helper for bundle_parse and bundle_parse_load. |
146 | | * |
147 | | * Returns NULL if successful, otherwise a malloc()'d string describing the |
148 | | * error. The caller is responsible for freeing the returned string.*/ |
149 | | static char * OVS_WARN_UNUSED_RESULT |
150 | | bundle_parse__(const char *s, const struct ofputil_port_map *port_map, |
151 | | char **save_ptr, |
152 | | const char *fields, const char *basis, const char *algorithm, |
153 | | const char *member_type, const char *dst, |
154 | | const char *member_delim, struct ofpbuf *ofpacts) |
155 | 0 | { |
156 | 0 | struct ofpact_bundle *bundle; |
157 | |
|
158 | 0 | if (!member_delim) { |
159 | 0 | return xasprintf("%s: not enough arguments to bundle action", s); |
160 | 0 | } |
161 | | |
162 | 0 | if (strcasecmp(member_delim, "members") |
163 | 0 | && strcasecmp(member_delim, "slaves")) { |
164 | 0 | return xasprintf("%s: missing member delimiter, expected `members', " |
165 | 0 | "got `%s'", s, member_delim); |
166 | 0 | } |
167 | | |
168 | 0 | bundle = ofpact_put_BUNDLE(ofpacts); |
169 | |
|
170 | 0 | for (;;) { |
171 | 0 | ofp_port_t member_port; |
172 | 0 | char *member; |
173 | |
|
174 | 0 | member = strtok_r(NULL, ", []", save_ptr); |
175 | 0 | if (!member || bundle->n_members >= BUNDLE_MAX_MEMBERS) { |
176 | 0 | break; |
177 | 0 | } |
178 | | |
179 | 0 | if (!ofputil_port_from_string(member, port_map, &member_port)) { |
180 | 0 | return xasprintf("%s: bad port number", member); |
181 | 0 | } |
182 | 0 | ofpbuf_put(ofpacts, &member_port, sizeof member_port); |
183 | |
|
184 | 0 | bundle = ofpacts->header; |
185 | 0 | bundle->n_members++; |
186 | 0 | } |
187 | | |
188 | 0 | if (ofpbuf_oversized(ofpacts)) { |
189 | 0 | return xasprintf("input too big"); |
190 | 0 | } |
191 | | |
192 | 0 | ofpact_finish_BUNDLE(ofpacts, &bundle); |
193 | 0 | bundle->basis = atoi(basis); |
194 | |
|
195 | 0 | if (!strcasecmp(fields, "eth_src")) { |
196 | 0 | bundle->fields = NX_HASH_FIELDS_ETH_SRC; |
197 | 0 | } else if (!strcasecmp(fields, "symmetric_l4")) { |
198 | 0 | bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4; |
199 | 0 | } else if (!strcasecmp(fields, "symmetric_l3l4")) { |
200 | 0 | bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4; |
201 | 0 | } else if (!strcasecmp(fields, "symmetric_l3l4+udp")) { |
202 | 0 | bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP; |
203 | 0 | } else if (!strcasecmp(fields, "nw_src")) { |
204 | 0 | bundle->fields = NX_HASH_FIELDS_NW_SRC; |
205 | 0 | } else if (!strcasecmp(fields, "nw_dst")) { |
206 | 0 | bundle->fields = NX_HASH_FIELDS_NW_DST; |
207 | 0 | } else if (!strcasecmp(fields, "symmetric_l3")) { |
208 | 0 | bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3; |
209 | 0 | } else { |
210 | 0 | return xasprintf("%s: unknown fields `%s'", s, fields); |
211 | 0 | } |
212 | | |
213 | 0 | if (!strcasecmp(algorithm, "active_backup")) { |
214 | 0 | bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP; |
215 | 0 | } else if (!strcasecmp(algorithm, "hrw")) { |
216 | 0 | bundle->algorithm = NX_BD_ALG_HRW; |
217 | 0 | } else { |
218 | 0 | return xasprintf("%s: unknown algorithm `%s'", s, algorithm); |
219 | 0 | } |
220 | | |
221 | 0 | if (strcasecmp(member_type, "ofport")) { |
222 | 0 | return xasprintf("%s: unknown member_type `%s'", s, member_type); |
223 | 0 | } |
224 | | |
225 | 0 | if (dst) { |
226 | 0 | char *error = mf_parse_subfield(&bundle->dst, dst); |
227 | 0 | if (error) { |
228 | 0 | return error; |
229 | 0 | } |
230 | | |
231 | 0 | if (!mf_nxm_header(bundle->dst.field->id)) { |
232 | 0 | return xasprintf("%s: experimenter OXM field '%s' not supported", |
233 | 0 | s, dst); |
234 | 0 | } |
235 | 0 | } |
236 | | |
237 | 0 | return NULL; |
238 | 0 | } |
239 | | |
240 | | /* Converts a bundle action string contained in 's' to an nx_action_bundle and |
241 | | * stores it in 'b'. Sets 'b''s l2 pointer to NULL. |
242 | | * |
243 | | * Returns NULL if successful, otherwise a malloc()'d string describing the |
244 | | * error. The caller is responsible for freeing the returned string. */ |
245 | | char * OVS_WARN_UNUSED_RESULT |
246 | | bundle_parse(const char *s, const struct ofputil_port_map *port_map, |
247 | | struct ofpbuf *ofpacts) |
248 | 0 | { |
249 | 0 | char *fields, *basis, *algorithm, *member_type, *member_delim; |
250 | 0 | char *tokstr, *save_ptr; |
251 | 0 | char *error; |
252 | |
|
253 | 0 | save_ptr = NULL; |
254 | 0 | tokstr = xstrdup(s); |
255 | 0 | fields = strtok_r(tokstr, ", ", &save_ptr); |
256 | 0 | basis = strtok_r(NULL, ", ", &save_ptr); |
257 | 0 | algorithm = strtok_r(NULL, ", ", &save_ptr); |
258 | 0 | member_type = strtok_r(NULL, ", ", &save_ptr); |
259 | 0 | member_delim = strtok_r(NULL, ": ", &save_ptr); |
260 | |
|
261 | 0 | error = bundle_parse__(s, port_map, |
262 | 0 | &save_ptr, fields, basis, algorithm, member_type, |
263 | 0 | NULL, member_delim, ofpacts); |
264 | 0 | free(tokstr); |
265 | |
|
266 | 0 | return error; |
267 | 0 | } |
268 | | |
269 | | /* Converts a bundle_load action string contained in 's' to an nx_action_bundle |
270 | | * and stores it in 'b'. Sets 'b''s l2 pointer to NULL. |
271 | | * |
272 | | * Returns NULL if successful, otherwise a malloc()'d string describing the |
273 | | * error. The caller is responsible for freeing the returned string.*/ |
274 | | char * OVS_WARN_UNUSED_RESULT |
275 | | bundle_parse_load(const char *s, const struct ofputil_port_map *port_map, |
276 | | struct ofpbuf *ofpacts) |
277 | 0 | { |
278 | 0 | char *fields, *basis, *algorithm, *member_type, *dst, *member_delim; |
279 | 0 | char *tokstr, *save_ptr; |
280 | 0 | char *error; |
281 | |
|
282 | 0 | save_ptr = NULL; |
283 | 0 | tokstr = xstrdup(s); |
284 | 0 | fields = strtok_r(tokstr, ", ", &save_ptr); |
285 | 0 | basis = strtok_r(NULL, ", ", &save_ptr); |
286 | 0 | algorithm = strtok_r(NULL, ", ", &save_ptr); |
287 | 0 | member_type = strtok_r(NULL, ", ", &save_ptr); |
288 | 0 | dst = strtok_r(NULL, ", ", &save_ptr); |
289 | 0 | member_delim = strtok_r(NULL, ": ", &save_ptr); |
290 | |
|
291 | 0 | error = bundle_parse__(s, port_map, |
292 | 0 | &save_ptr, fields, basis, algorithm, member_type, |
293 | 0 | dst, member_delim, ofpacts); |
294 | |
|
295 | 0 | free(tokstr); |
296 | |
|
297 | 0 | return error; |
298 | 0 | } |
299 | | |
300 | | /* Appends a human-readable representation of 'nab' to 's'. If 'port_map' is |
301 | | * nonnull, uses it to translate port numbers to names in output. */ |
302 | | void |
303 | | bundle_format(const struct ofpact_bundle *bundle, |
304 | | const struct ofputil_port_map *port_map, struct ds *s) |
305 | 1.51k | { |
306 | 1.51k | const char *action, *fields, *algorithm; |
307 | 1.51k | size_t i; |
308 | | |
309 | 1.51k | fields = flow_hash_fields_to_str(bundle->fields); |
310 | | |
311 | 1.51k | switch (bundle->algorithm) { |
312 | 13 | case NX_BD_ALG_HRW: |
313 | 13 | algorithm = "hrw"; |
314 | 13 | break; |
315 | 951 | case NX_BD_ALG_ACTIVE_BACKUP: |
316 | 951 | algorithm = "active_backup"; |
317 | 951 | break; |
318 | 552 | default: |
319 | 552 | algorithm = "<unknown>"; |
320 | 1.51k | } |
321 | | |
322 | 1.51k | action = bundle->dst.field ? "bundle_load" : "bundle"; |
323 | | |
324 | 1.51k | ds_put_format(s, "%s%s(%s%s,%"PRIu16",%s,%s,", colors.paren, action, |
325 | 1.51k | colors.end, fields, bundle->basis, algorithm, "ofport"); |
326 | | |
327 | 1.51k | if (bundle->dst.field) { |
328 | 934 | mf_format_subfield(&bundle->dst, s); |
329 | 934 | ds_put_char(s, ','); |
330 | 934 | } |
331 | | |
332 | 1.51k | ds_put_format(s, "%smembers:%s", colors.param, colors.end); |
333 | 14.5k | for (i = 0; i < bundle->n_members; i++) { |
334 | 12.9k | if (i) { |
335 | 11.4k | ds_put_char(s, ','); |
336 | 11.4k | } |
337 | | |
338 | 12.9k | ofputil_format_port(bundle->members[i], port_map, s); |
339 | 12.9k | } |
340 | | |
341 | 1.51k | ds_put_format(s, "%s)%s", colors.paren, colors.end); |
342 | 1.51k | } |