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-errors.h"
30 #include "openflow/nicira-ext.h"
33 #define BUNDLE_MAX_SLAVES 2048
35 VLOG_DEFINE_THIS_MODULE(bundle);
38 execute_ab(const struct nx_action_bundle *nab,
39 bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
43 for (i = 0; i < ntohs(nab->n_slaves); i++) {
44 uint16_t slave = bundle_get_slave(nab, i);
46 if (slave_enabled(slave, aux)) {
55 execute_hrw(const struct nx_action_bundle *nab, const struct flow *flow,
56 bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
58 uint32_t flow_hash, best_hash;
61 flow_hash = flow_hash_fields(flow, ntohs(nab->fields), ntohs(nab->basis));
65 for (i = 0; i < ntohs(nab->n_slaves); i++) {
66 if (slave_enabled(bundle_get_slave(nab, i), aux)) {
67 uint32_t hash = hash_2words(i, flow_hash);
69 if (best < 0 || hash > best_hash) {
76 return best >= 0 ? bundle_get_slave(nab, best) : OFPP_NONE;
79 /* Executes 'nab' on 'flow'. Uses 'slave_enabled' to determine if the slave
80 * designated by 'ofp_port' is up. Returns the chosen slave, or OFPP_NONE if
81 * none of the slaves are acceptable. */
83 bundle_execute(const struct nx_action_bundle *nab, const struct flow *flow,
84 bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
86 switch (ntohs(nab->algorithm)) {
87 case NX_BD_ALG_HRW: return execute_hrw(nab, flow, slave_enabled, aux);
88 case NX_BD_ALG_ACTIVE_BACKUP: return execute_ab(nab, slave_enabled, aux);
89 default: NOT_REACHED();
94 bundle_execute_load(const struct nx_action_bundle *nab, struct flow *flow,
95 bool (*slave_enabled)(uint16_t ofp_port, void *aux),
98 struct mf_subfield dst;
100 nxm_decode(&dst, nab->dst, nab->ofs_nbits);
101 mf_set_subfield_value(&dst, bundle_execute(nab, flow, slave_enabled, aux),
105 /* Checks that 'nab' specifies a bundle action which is supported by this
106 * bundle module. Uses the 'max_ports' parameter to validate each port using
107 * ofputil_check_output_port(). Returns 0 if 'nab' is supported, otherwise an
108 * OFPERR_* error code. */
110 bundle_check(const struct nx_action_bundle *nab, int max_ports,
111 const struct flow *flow)
113 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
114 uint16_t n_slaves, fields, algorithm, subtype;
116 size_t slaves_size, i;
119 subtype = ntohs(nab->subtype);
120 n_slaves = ntohs(nab->n_slaves);
121 fields = ntohs(nab->fields);
122 algorithm = ntohs(nab->algorithm);
123 slave_type = ntohl(nab->slave_type);
124 slaves_size = ntohs(nab->len) - sizeof *nab;
126 error = OFPERR_OFPBAC_BAD_ARGUMENT;
127 if (!flow_hash_fields_valid(fields)) {
128 VLOG_WARN_RL(&rl, "unsupported fields %"PRIu16, fields);
129 } else if (n_slaves > BUNDLE_MAX_SLAVES) {
130 VLOG_WARN_RL(&rl, "too may slaves");
131 } else if (algorithm != NX_BD_ALG_HRW
132 && algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
133 VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16, algorithm);
134 } else if (slave_type != NXM_OF_IN_PORT) {
135 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
140 for (i = 0; i < sizeof(nab->zero); i++) {
142 VLOG_WARN_RL(&rl, "reserved field is nonzero");
143 error = OFPERR_OFPBAC_BAD_ARGUMENT;
147 if (subtype == NXAST_BUNDLE && (nab->ofs_nbits || nab->dst)) {
148 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
149 error = OFPERR_OFPBAC_BAD_ARGUMENT;
152 if (subtype == NXAST_BUNDLE_LOAD) {
153 struct mf_subfield dst;
155 nxm_decode(&dst, nab->dst, nab->ofs_nbits);
156 if (dst.n_bits < 16) {
157 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
159 error = OFPERR_OFPBAC_BAD_ARGUMENT;
161 error = mf_check_dst(&dst, flow);
165 if (slaves_size < n_slaves * sizeof(ovs_be16)) {
166 VLOG_WARN_RL(&rl, "Nicira action %"PRIu16" only has %zu bytes "
167 "allocated for slaves. %zu bytes are required for "
168 "%"PRIu16" slaves.", subtype, slaves_size,
169 n_slaves * sizeof(ovs_be16), n_slaves);
170 error = OFPERR_OFPBAC_BAD_LEN;
173 for (i = 0; i < n_slaves; i++) {
174 uint16_t ofp_port = bundle_get_slave(nab, i);
175 enum ofperr ofputil_error;
177 ofputil_error = ofputil_check_output_port(ofp_port, max_ports);
179 VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
180 error = ofputil_error;
183 /* Controller slaves are unsupported due to the lack of a max_len
184 * argument. This may or may not change in the future. There doesn't
185 * seem to be a real-world use-case for supporting it. */
186 if (ofp_port == OFPP_CONTROLLER) {
187 VLOG_WARN_RL(&rl, "unsupported controller slave");
188 error = OFPERR_OFPBAC_BAD_OUT_PORT;
195 /* Helper for bundle_parse and bundle_parse_load. */
197 bundle_parse__(struct ofpbuf *b, const char *s, char **save_ptr,
198 const char *fields, const char *basis, const char *algorithm,
199 const char *slave_type, const char *dst_s,
200 const char *slave_delim)
202 enum ofputil_action_code code;
203 struct nx_action_bundle *nab;
207 ovs_fatal(0, "%s: not enough arguments to bundle action", s);
210 if (strcasecmp(slave_delim, "slaves")) {
211 ovs_fatal(0, "%s: missing slave delimiter, expected `slaves' got `%s'",
215 code = dst_s ? OFPUTIL_NXAST_BUNDLE_LOAD : OFPUTIL_NXAST_BUNDLE;
216 b->l2 = ofputil_put_action(code, b);
223 slave = strtok_r(NULL, ", [", save_ptr);
224 if (!slave || n_slaves >= BUNDLE_MAX_SLAVES) {
228 slave_be = htons(atoi(slave));
229 ofpbuf_put(b, &slave_be, sizeof slave_be);
234 /* Slaves array must be multiple of 8 bytes long. */
236 ofpbuf_put_zeros(b, 8 - (b->size % 8));
240 nab->len = htons(b->size - ((char *) b->l2 - (char *) b->data));
241 nab->n_slaves = htons(n_slaves);
242 nab->basis = htons(atoi(basis));
244 if (!strcasecmp(fields, "eth_src")) {
245 nab->fields = htons(NX_HASH_FIELDS_ETH_SRC);
246 } else if (!strcasecmp(fields, "symmetric_l4")) {
247 nab->fields = htons(NX_HASH_FIELDS_SYMMETRIC_L4);
249 ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
252 if (!strcasecmp(algorithm, "active_backup")) {
253 nab->algorithm = htons(NX_BD_ALG_ACTIVE_BACKUP);
254 } else if (!strcasecmp(algorithm, "hrw")) {
255 nab->algorithm = htons(NX_BD_ALG_HRW);
257 ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
260 if (!strcasecmp(slave_type, "ofport")) {
261 nab->slave_type = htonl(NXM_OF_IN_PORT);
263 ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
267 struct mf_subfield dst;
269 mf_parse_subfield(&dst, dst_s);
270 nab->dst = htonl(dst.field->nxm_header);
271 nab->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
277 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
278 * stores it in 'b'. Sets 'b''s l2 pointer to NULL. */
280 bundle_parse(struct ofpbuf *b, const char *s)
282 char *fields, *basis, *algorithm, *slave_type, *slave_delim;
283 char *tokstr, *save_ptr;
287 fields = strtok_r(tokstr, ", ", &save_ptr);
288 basis = strtok_r(NULL, ", ", &save_ptr);
289 algorithm = strtok_r(NULL, ", ", &save_ptr);
290 slave_type = strtok_r(NULL, ", ", &save_ptr);
291 slave_delim = strtok_r(NULL, ": ", &save_ptr);
293 bundle_parse__(b, s, &save_ptr, fields, basis, algorithm, slave_type, NULL,
298 /* Converts a bundle_load action string contained in 's' to an nx_action_bundle
299 * and stores it in 'b'. Sets 'b''s l2 pointer to NULL. */
301 bundle_parse_load(struct ofpbuf *b, const char *s)
303 char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
304 char *tokstr, *save_ptr;
308 fields = strtok_r(tokstr, ", ", &save_ptr);
309 basis = strtok_r(NULL, ", ", &save_ptr);
310 algorithm = strtok_r(NULL, ", ", &save_ptr);
311 slave_type = strtok_r(NULL, ", ", &save_ptr);
312 dst = strtok_r(NULL, ", ", &save_ptr);
313 slave_delim = strtok_r(NULL, ": ", &save_ptr);
315 bundle_parse__(b, s, &save_ptr, fields, basis, algorithm, slave_type, dst,
321 /* Appends a human-readable representation of 'nab' to 's'. */
323 bundle_format(const struct nx_action_bundle *nab, struct ds *s)
325 const char *action, *fields, *algorithm, *slave_type;
328 fields = flow_hash_fields_to_str(ntohs(nab->fields));
330 switch (ntohs(nab->algorithm)) {
334 case NX_BD_ALG_ACTIVE_BACKUP:
335 algorithm = "active_backup";
338 algorithm = "<unknown>";
341 switch (ntohl(nab->slave_type)) {
343 slave_type = "ofport";
346 slave_type = "<unknown>";
349 switch (ntohs(nab->subtype)) {
353 case NXAST_BUNDLE_LOAD:
354 action = "bundle_load";
360 ds_put_format(s, "%s(%s,%"PRIu16",%s,%s,", action, fields,
361 ntohs(nab->basis), algorithm, slave_type);
363 if (nab->subtype == htons(NXAST_BUNDLE_LOAD)) {
364 struct mf_subfield dst;
366 nxm_decode(&dst, nab->dst, nab->ofs_nbits);
367 mf_format_subfield(&dst, s);
371 ds_put_cstr(s, "slaves:");
372 for (i = 0; i < ntohs(nab->n_slaves); i++) {
377 ds_put_format(s, "%"PRIu16, bundle_get_slave(nab, i));