1 /* Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <arpa/inet.h>
24 #include "multipath.h"
26 #include "openflow/nicira-ext.h"
27 #include "openvswitch/dynamic-string.h"
28 #include "openvswitch/meta-flow.h"
29 #include "openvswitch/ofp-actions.h"
30 #include "openvswitch/ofp-errors.h"
31 #include "openvswitch/ofp-util.h"
32 #include "openvswitch/ofpbuf.h"
33 #include "openvswitch/vlog.h"
35 VLOG_DEFINE_THIS_MODULE(bundle);
38 execute_ab(const struct ofpact_bundle *bundle,
39 bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
43 for (i = 0; i < bundle->n_slaves; i++) {
44 ofp_port_t slave = bundle->slaves[i];
45 if (slave_enabled(slave, aux)) {
54 execute_hrw(const struct ofpact_bundle *bundle,
55 const struct flow *flow, struct flow_wildcards *wc,
56 bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
58 uint32_t flow_hash, best_hash;
61 if (bundle->n_slaves > 1) {
62 flow_mask_hash_fields(flow, wc, bundle->fields);
65 flow_hash = flow_hash_fields(flow, bundle->fields, bundle->basis);
69 for (i = 0; i < bundle->n_slaves; i++) {
70 if (slave_enabled(bundle->slaves[i], aux)) {
71 uint32_t hash = hash_2words(i, flow_hash);
73 if (best < 0 || hash > best_hash) {
80 return best >= 0 ? bundle->slaves[best] : OFPP_NONE;
83 /* Executes 'bundle' on 'flow'. Sets fields in 'wc' that were used to
84 * calculate the result. Uses 'slave_enabled' to determine if the slave
85 * designated by 'ofp_port' is up. Returns the chosen slave, or
86 * OFPP_NONE if none of the slaves are acceptable. */
88 bundle_execute(const struct ofpact_bundle *bundle,
89 const struct flow *flow, struct flow_wildcards *wc,
90 bool (*slave_enabled)(ofp_port_t ofp_port, void *aux),
93 switch (bundle->algorithm) {
95 return execute_hrw(bundle, flow, wc, slave_enabled, aux);
97 case NX_BD_ALG_ACTIVE_BACKUP:
98 return execute_ab(bundle, slave_enabled, aux);
106 bundle_check(const struct ofpact_bundle *bundle, ofp_port_t max_ports,
107 const struct flow *flow)
109 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
112 if (bundle->dst.field) {
113 enum ofperr error = mf_check_dst(&bundle->dst, flow);
119 for (i = 0; i < bundle->n_slaves; i++) {
120 ofp_port_t ofp_port = bundle->slaves[i];
122 if (ofp_port != OFPP_NONE) {
123 enum ofperr error = ofpact_check_output_port(ofp_port, max_ports);
125 VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
129 /* Controller slaves are unsupported due to the lack of a max_len
130 * argument. This may or may not change in the future. There doesn't
131 * seem to be a real-world use-case for supporting it. */
132 if (ofp_port == OFPP_CONTROLLER) {
133 VLOG_WARN_RL(&rl, "unsupported controller slave");
134 return OFPERR_OFPBAC_BAD_OUT_PORT;
142 /* Helper for bundle_parse and bundle_parse_load.
144 * Returns NULL if successful, otherwise a malloc()'d string describing the
145 * error. The caller is responsible for freeing the returned string.*/
146 static char * OVS_WARN_UNUSED_RESULT
147 bundle_parse__(const char *s, char **save_ptr,
148 const char *fields, const char *basis, const char *algorithm,
149 const char *slave_type, const char *dst,
150 const char *slave_delim, struct ofpbuf *ofpacts)
152 struct ofpact_bundle *bundle;
155 return xasprintf("%s: not enough arguments to bundle action", s);
158 if (strcasecmp(slave_delim, "slaves")) {
159 return xasprintf("%s: missing slave delimiter, expected `slaves' "
160 "got `%s'", s, slave_delim);
163 bundle = ofpact_put_BUNDLE(ofpacts);
166 ofp_port_t slave_port;
169 slave = strtok_r(NULL, ", []", save_ptr);
170 if (!slave || bundle->n_slaves >= BUNDLE_MAX_SLAVES) {
174 if (!ofputil_port_from_string(slave, &slave_port)) {
175 return xasprintf("%s: bad port number", slave);
177 ofpbuf_put(ofpacts, &slave_port, sizeof slave_port);
179 bundle = ofpacts->header;
182 ofpact_finish_BUNDLE(ofpacts, &bundle);
183 bundle->basis = atoi(basis);
185 if (!strcasecmp(fields, "eth_src")) {
186 bundle->fields = NX_HASH_FIELDS_ETH_SRC;
187 } else if (!strcasecmp(fields, "symmetric_l4")) {
188 bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4;
189 } else if (!strcasecmp(fields, "symmetric_l3l4")) {
190 bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4;
191 } else if (!strcasecmp(fields, "symmetric_l3l4+udp")) {
192 bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP;
194 return xasprintf("%s: unknown fields `%s'", s, fields);
197 if (!strcasecmp(algorithm, "active_backup")) {
198 bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
199 } else if (!strcasecmp(algorithm, "hrw")) {
200 bundle->algorithm = NX_BD_ALG_HRW;
202 return xasprintf("%s: unknown algorithm `%s'", s, algorithm);
205 if (strcasecmp(slave_type, "ofport")) {
206 return xasprintf("%s: unknown slave_type `%s'", s, slave_type);
210 char *error = mf_parse_subfield(&bundle->dst, dst);
215 if (!mf_nxm_header(bundle->dst.field->id)) {
216 return xasprintf("%s: experimenter OXM field '%s' not supported",
224 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
225 * stores it in 'b'. Sets 'b''s l2 pointer to NULL.
227 * Returns NULL if successful, otherwise a malloc()'d string describing the
228 * error. The caller is responsible for freeing the returned string. */
229 char * OVS_WARN_UNUSED_RESULT
230 bundle_parse(const char *s, struct ofpbuf *ofpacts)
232 char *fields, *basis, *algorithm, *slave_type, *slave_delim;
233 char *tokstr, *save_ptr;
238 fields = strtok_r(tokstr, ", ", &save_ptr);
239 basis = strtok_r(NULL, ", ", &save_ptr);
240 algorithm = strtok_r(NULL, ", ", &save_ptr);
241 slave_type = strtok_r(NULL, ", ", &save_ptr);
242 slave_delim = strtok_r(NULL, ": ", &save_ptr);
244 error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
245 NULL, slave_delim, ofpacts);
251 /* Converts a bundle_load action string contained in 's' to an nx_action_bundle
252 * and stores it in 'b'. Sets 'b''s l2 pointer to NULL.
254 * Returns NULL if successful, otherwise a malloc()'d string describing the
255 * error. The caller is responsible for freeing the returned string.*/
256 char * OVS_WARN_UNUSED_RESULT
257 bundle_parse_load(const char *s, struct ofpbuf *ofpacts)
259 char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
260 char *tokstr, *save_ptr;
265 fields = strtok_r(tokstr, ", ", &save_ptr);
266 basis = strtok_r(NULL, ", ", &save_ptr);
267 algorithm = strtok_r(NULL, ", ", &save_ptr);
268 slave_type = strtok_r(NULL, ", ", &save_ptr);
269 dst = strtok_r(NULL, ", ", &save_ptr);
270 slave_delim = strtok_r(NULL, ": ", &save_ptr);
272 error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
273 dst, slave_delim, ofpacts);
280 /* Appends a human-readable representation of 'nab' to 's'. */
282 bundle_format(const struct ofpact_bundle *bundle, struct ds *s)
284 const char *action, *fields, *algorithm;
287 fields = flow_hash_fields_to_str(bundle->fields);
289 switch (bundle->algorithm) {
293 case NX_BD_ALG_ACTIVE_BACKUP:
294 algorithm = "active_backup";
297 algorithm = "<unknown>";
300 action = bundle->dst.field ? "bundle_load" : "bundle";
302 ds_put_format(s, "%s%s(%s%s,%"PRIu16",%s,%s,", colors.paren, action,
303 colors.end, fields, bundle->basis, algorithm, "ofport");
305 if (bundle->dst.field) {
306 mf_format_subfield(&bundle->dst, s);
310 ds_put_format(s, "%sslaves:%s", colors.param, colors.end);
311 for (i = 0; i < bundle->n_slaves; i++) {
316 ofputil_format_port(bundle->slaves[i], s);
319 ds_put_format(s, "%s)%s", colors.paren, colors.end);