1 /* Copyright (c) 2011, 2012 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 %zu bytes "
169 "allocated for slaves. %zu 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->l2;
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 = ofputil_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 bundle_parse__(const char *s, char **save_ptr,
257 const char *fields, const char *basis, const char *algorithm,
258 const char *slave_type, const char *dst,
259 const char *slave_delim, struct ofpbuf *ofpacts)
261 struct ofpact_bundle *bundle;
264 ovs_fatal(0, "%s: not enough arguments to bundle action", s);
267 if (strcasecmp(slave_delim, "slaves")) {
268 ovs_fatal(0, "%s: missing slave delimiter, expected `slaves' got `%s'",
272 bundle = ofpact_put_BUNDLE(ofpacts);
275 ofp_port_t slave_port;
278 slave = strtok_r(NULL, ", []", save_ptr);
279 if (!slave || bundle->n_slaves >= BUNDLE_MAX_SLAVES) {
283 if (!ofputil_port_from_string(slave, &slave_port)) {
284 ovs_fatal(0, "%s: bad port number", slave);
286 ofpbuf_put(ofpacts, &slave_port, sizeof slave_port);
288 bundle = ofpacts->l2;
291 ofpact_update_len(ofpacts, &bundle->ofpact);
293 bundle->basis = atoi(basis);
295 if (!strcasecmp(fields, "eth_src")) {
296 bundle->fields = NX_HASH_FIELDS_ETH_SRC;
297 } else if (!strcasecmp(fields, "symmetric_l4")) {
298 bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4;
300 ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
303 if (!strcasecmp(algorithm, "active_backup")) {
304 bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
305 } else if (!strcasecmp(algorithm, "hrw")) {
306 bundle->algorithm = NX_BD_ALG_HRW;
308 ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
311 if (strcasecmp(slave_type, "ofport")) {
312 ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
316 mf_parse_subfield(&bundle->dst, dst);
320 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
321 * stores it in 'b'. Sets 'b''s l2 pointer to NULL. */
323 bundle_parse(const char *s, struct ofpbuf *ofpacts)
325 char *fields, *basis, *algorithm, *slave_type, *slave_delim;
326 char *tokstr, *save_ptr;
330 fields = strtok_r(tokstr, ", ", &save_ptr);
331 basis = strtok_r(NULL, ", ", &save_ptr);
332 algorithm = strtok_r(NULL, ", ", &save_ptr);
333 slave_type = strtok_r(NULL, ", ", &save_ptr);
334 slave_delim = strtok_r(NULL, ": ", &save_ptr);
336 bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type, NULL,
337 slave_delim, ofpacts);
341 /* Converts a bundle_load action string contained in 's' to an nx_action_bundle
342 * and stores it in 'b'. Sets 'b''s l2 pointer to NULL. */
344 bundle_parse_load(const char *s, struct ofpbuf *ofpacts)
346 char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
347 char *tokstr, *save_ptr;
351 fields = strtok_r(tokstr, ", ", &save_ptr);
352 basis = strtok_r(NULL, ", ", &save_ptr);
353 algorithm = strtok_r(NULL, ", ", &save_ptr);
354 slave_type = strtok_r(NULL, ", ", &save_ptr);
355 dst = strtok_r(NULL, ", ", &save_ptr);
356 slave_delim = strtok_r(NULL, ": ", &save_ptr);
358 bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type, dst,
359 slave_delim, ofpacts);
364 /* Appends a human-readable representation of 'nab' to 's'. */
366 bundle_format(const struct ofpact_bundle *bundle, struct ds *s)
368 const char *action, *fields, *algorithm;
371 fields = flow_hash_fields_to_str(bundle->fields);
373 switch (bundle->algorithm) {
377 case NX_BD_ALG_ACTIVE_BACKUP:
378 algorithm = "active_backup";
381 algorithm = "<unknown>";
384 action = bundle->dst.field ? "bundle_load" : "bundle";
386 ds_put_format(s, "%s(%s,%"PRIu16",%s,%s,", action, fields,
387 bundle->basis, algorithm, "ofport");
389 if (bundle->dst.field) {
390 mf_format_subfield(&bundle->dst, s);
394 ds_put_cstr(s, "slaves:");
395 for (i = 0; i < bundle->n_slaves; i++) {
400 ofputil_format_port(bundle->slaves[i], s);