1 /* Copyright (c) 2011, 2012, 2013 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>
23 #include "dynamic-string.h"
24 #include "multipath.h"
25 #include "meta-flow.h"
28 #include "ofp-actions.h"
29 #include "ofp-errors.h"
31 #include "openflow/nicira-ext.h"
34 #define BUNDLE_MAX_SLAVES 2048
36 VLOG_DEFINE_THIS_MODULE(bundle);
39 execute_ab(const struct ofpact_bundle *bundle,
40 bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
44 for (i = 0; i < bundle->n_slaves; i++) {
45 ofp_port_t slave = bundle->slaves[i];
46 if (slave_enabled(slave, aux)) {
55 execute_hrw(const struct ofpact_bundle *bundle,
56 const struct flow *flow, struct flow_wildcards *wc,
57 bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
59 uint32_t flow_hash, best_hash;
62 if (bundle->n_slaves > 1) {
63 flow_mask_hash_fields(flow, wc, bundle->fields);
66 flow_hash = flow_hash_fields(flow, bundle->fields, bundle->basis);
70 for (i = 0; i < bundle->n_slaves; i++) {
71 if (slave_enabled(bundle->slaves[i], aux)) {
72 uint32_t hash = hash_2words(i, flow_hash);
74 if (best < 0 || hash > best_hash) {
81 return best >= 0 ? bundle->slaves[best] : OFPP_NONE;
84 /* Executes 'bundle' on 'flow'. Sets fields in 'wc' that were used to
85 * calculate the result. Uses 'slave_enabled' to determine if the slave
86 * designated by 'ofp_port' is up. Returns the chosen slave, or
87 * OFPP_NONE if none of the slaves are acceptable. */
89 bundle_execute(const struct ofpact_bundle *bundle,
90 const struct flow *flow, struct flow_wildcards *wc,
91 bool (*slave_enabled)(ofp_port_t ofp_port, void *aux),
94 switch (bundle->algorithm) {
96 return execute_hrw(bundle, flow, wc, slave_enabled, aux);
98 case NX_BD_ALG_ACTIVE_BACKUP:
99 return execute_ab(bundle, slave_enabled, aux);
106 /* Checks that 'nab' specifies a bundle action which is supported by this
107 * bundle module. Uses the 'max_ports' parameter to validate each port using
108 * ofputil_check_output_port(). Returns 0 if 'nab' is supported, otherwise an
109 * OFPERR_* error code. */
111 bundle_from_openflow(const struct nx_action_bundle *nab,
112 struct ofpbuf *ofpacts)
114 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
115 struct ofpact_bundle *bundle;
118 size_t slaves_size, i;
121 bundle = ofpact_put_BUNDLE(ofpacts);
123 subtype = ntohs(nab->subtype);
124 bundle->n_slaves = ntohs(nab->n_slaves);
125 bundle->basis = ntohs(nab->basis);
126 bundle->fields = ntohs(nab->fields);
127 bundle->algorithm = ntohs(nab->algorithm);
128 slave_type = ntohl(nab->slave_type);
129 slaves_size = ntohs(nab->len) - sizeof *nab;
131 error = OFPERR_OFPBAC_BAD_ARGUMENT;
132 if (!flow_hash_fields_valid(bundle->fields)) {
133 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
134 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
135 VLOG_WARN_RL(&rl, "too may slaves");
136 } else if (bundle->algorithm != NX_BD_ALG_HRW
137 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
138 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
139 } else if (slave_type != NXM_OF_IN_PORT) {
140 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
145 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
146 VLOG_WARN_RL(&rl, "reserved field is nonzero");
147 error = OFPERR_OFPBAC_BAD_ARGUMENT;
150 if (subtype == NXAST_BUNDLE && (nab->ofs_nbits || nab->dst)) {
151 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
152 error = OFPERR_OFPBAC_BAD_ARGUMENT;
155 if (subtype == NXAST_BUNDLE_LOAD) {
156 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
157 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
158 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
160 if (bundle->dst.n_bits < 16) {
161 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
163 error = OFPERR_OFPBAC_BAD_ARGUMENT;
167 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
168 VLOG_WARN_RL(&rl, "Nicira action %"PRIu16" only has %"PRIuSIZE" bytes "
169 "allocated for slaves. %"PRIuSIZE" bytes are required for "
170 "%"PRIu16" slaves.", subtype, slaves_size,
171 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
172 error = OFPERR_OFPBAC_BAD_LEN;
175 for (i = 0; i < bundle->n_slaves; i++) {
176 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
177 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
180 bundle = ofpacts->frame;
181 ofpact_update_len(ofpacts, &bundle->ofpact);
184 error = bundle_check(bundle, OFPP_MAX, NULL);
190 bundle_check(const struct ofpact_bundle *bundle, ofp_port_t max_ports,
191 const struct flow *flow)
193 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
196 if (bundle->dst.field) {
197 enum ofperr error = mf_check_dst(&bundle->dst, flow);
203 for (i = 0; i < bundle->n_slaves; i++) {
204 ofp_port_t ofp_port = bundle->slaves[i];
207 error = ofpact_check_output_port(ofp_port, max_ports);
209 VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
213 /* Controller slaves are unsupported due to the lack of a max_len
214 * argument. This may or may not change in the future. There doesn't
215 * seem to be a real-world use-case for supporting it. */
216 if (ofp_port == OFPP_CONTROLLER) {
217 VLOG_WARN_RL(&rl, "unsupported controller slave");
218 return OFPERR_OFPBAC_BAD_OUT_PORT;
226 bundle_to_nxast(const struct ofpact_bundle *bundle, struct ofpbuf *openflow)
228 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
229 struct nx_action_bundle *nab;
233 nab = (bundle->dst.field
234 ? ofputil_put_NXAST_BUNDLE_LOAD(openflow)
235 : ofputil_put_NXAST_BUNDLE(openflow));
236 nab->len = htons(ntohs(nab->len) + slaves_len);
237 nab->algorithm = htons(bundle->algorithm);
238 nab->fields = htons(bundle->fields);
239 nab->basis = htons(bundle->basis);
240 nab->slave_type = htonl(NXM_OF_IN_PORT);
241 nab->n_slaves = htons(bundle->n_slaves);
242 if (bundle->dst.field) {
243 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
245 nab->dst = htonl(bundle->dst.field->nxm_header);
248 slaves = ofpbuf_put_zeros(openflow, slaves_len);
249 for (i = 0; i < bundle->n_slaves; i++) {
250 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
254 /* Helper for bundle_parse and bundle_parse_load.
256 * Returns NULL if successful, otherwise a malloc()'d string describing the
257 * error. The caller is responsible for freeing the returned string.*/
258 static char * WARN_UNUSED_RESULT
259 bundle_parse__(const char *s, char **save_ptr,
260 const char *fields, const char *basis, const char *algorithm,
261 const char *slave_type, const char *dst,
262 const char *slave_delim, struct ofpbuf *ofpacts)
264 struct ofpact_bundle *bundle;
267 return xasprintf("%s: not enough arguments to bundle action", s);
270 if (strcasecmp(slave_delim, "slaves")) {
271 return xasprintf("%s: missing slave delimiter, expected `slaves' "
272 "got `%s'", s, slave_delim);
275 bundle = ofpact_put_BUNDLE(ofpacts);
278 ofp_port_t slave_port;
281 slave = strtok_r(NULL, ", []", save_ptr);
282 if (!slave || bundle->n_slaves >= BUNDLE_MAX_SLAVES) {
286 if (!ofputil_port_from_string(slave, &slave_port)) {
287 return xasprintf("%s: bad port number", slave);
289 ofpbuf_put(ofpacts, &slave_port, sizeof slave_port);
291 bundle = ofpacts->frame;
294 ofpact_update_len(ofpacts, &bundle->ofpact);
296 bundle->basis = atoi(basis);
298 if (!strcasecmp(fields, "eth_src")) {
299 bundle->fields = NX_HASH_FIELDS_ETH_SRC;
300 } else if (!strcasecmp(fields, "symmetric_l4")) {
301 bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4;
303 return xasprintf("%s: unknown fields `%s'", s, fields);
306 if (!strcasecmp(algorithm, "active_backup")) {
307 bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
308 } else if (!strcasecmp(algorithm, "hrw")) {
309 bundle->algorithm = NX_BD_ALG_HRW;
311 return xasprintf("%s: unknown algorithm `%s'", s, algorithm);
314 if (strcasecmp(slave_type, "ofport")) {
315 return xasprintf("%s: unknown slave_type `%s'", s, slave_type);
319 char *error = mf_parse_subfield(&bundle->dst, dst);
328 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
329 * stores it in 'b'. Sets 'b''s l2 pointer to NULL.
331 * Returns NULL if successful, otherwise a malloc()'d string describing the
332 * error. The caller is responsible for freeing the returned string. */
333 char * WARN_UNUSED_RESULT
334 bundle_parse(const char *s, struct ofpbuf *ofpacts)
336 char *fields, *basis, *algorithm, *slave_type, *slave_delim;
337 char *tokstr, *save_ptr;
342 fields = strtok_r(tokstr, ", ", &save_ptr);
343 basis = strtok_r(NULL, ", ", &save_ptr);
344 algorithm = strtok_r(NULL, ", ", &save_ptr);
345 slave_type = strtok_r(NULL, ", ", &save_ptr);
346 slave_delim = strtok_r(NULL, ": ", &save_ptr);
348 error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
349 NULL, slave_delim, ofpacts);
355 /* Converts a bundle_load action string contained in 's' to an nx_action_bundle
356 * and stores it in 'b'. Sets 'b''s l2 pointer to NULL.
358 * Returns NULL if successful, otherwise a malloc()'d string describing the
359 * error. The caller is responsible for freeing the returned string.*/
360 char * WARN_UNUSED_RESULT
361 bundle_parse_load(const char *s, struct ofpbuf *ofpacts)
363 char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
364 char *tokstr, *save_ptr;
369 fields = strtok_r(tokstr, ", ", &save_ptr);
370 basis = strtok_r(NULL, ", ", &save_ptr);
371 algorithm = strtok_r(NULL, ", ", &save_ptr);
372 slave_type = strtok_r(NULL, ", ", &save_ptr);
373 dst = strtok_r(NULL, ", ", &save_ptr);
374 slave_delim = strtok_r(NULL, ": ", &save_ptr);
376 error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
377 dst, slave_delim, ofpacts);
384 /* Appends a human-readable representation of 'nab' to 's'. */
386 bundle_format(const struct ofpact_bundle *bundle, struct ds *s)
388 const char *action, *fields, *algorithm;
391 fields = flow_hash_fields_to_str(bundle->fields);
393 switch (bundle->algorithm) {
397 case NX_BD_ALG_ACTIVE_BACKUP:
398 algorithm = "active_backup";
401 algorithm = "<unknown>";
404 action = bundle->dst.field ? "bundle_load" : "bundle";
406 ds_put_format(s, "%s(%s,%"PRIu16",%s,%s,", action, fields,
407 bundle->basis, algorithm, "ofport");
409 if (bundle->dst.field) {
410 mf_format_subfield(&bundle->dst, s);
414 ds_put_cstr(s, "slaves:");
415 for (i = 0; i < bundle->n_slaves; i++) {
420 ofputil_format_port(bundle->slaves[i], s);