1 /* Copyright (c) 2011 Nicira Networks.
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"
28 #include "openflow/nicira-ext.h"
31 #define BUNDLE_MAX_SLAVES 2048
33 VLOG_DEFINE_THIS_MODULE(bundle);
35 /* Executes 'nab' on 'flow'. Uses 'slave_enabled' to determine if the slave
36 * designated by 'ofp_port' is up. Returns the chosen slave, or OFPP_NONE if
37 * none of the slaves are acceptable. */
39 bundle_execute(const struct nx_action_bundle *nab, const struct flow *flow,
40 bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
42 uint32_t flow_hash, best_hash;
45 assert(nab->algorithm == htons(NX_BD_ALG_HRW));
47 flow_hash = flow_hash_fields(flow, ntohs(nab->fields), ntohs(nab->basis));
50 for (i = 0; i < ntohs(nab->n_slaves); i++) {
51 if (slave_enabled(bundle_get_slave(nab, i), aux)) {
52 uint32_t hash = hash_2words(i, flow_hash);
54 if (best < 0 || hash > best_hash) {
61 return best >= 0 ? bundle_get_slave(nab, best) : OFPP_NONE;
64 /* Checks that 'nab' specifies a bundle action which is supported by this
65 * bundle module. Uses the 'max_ports' parameter to validate each port using
66 * ofputil_check_output_port(). Returns 0 if 'nab' is supported, otherwise an
67 * OpenFlow error code (as returned by ofp_mkerr()). */
69 bundle_check(const struct nx_action_bundle *nab, int max_ports)
71 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
72 uint16_t n_slaves, fields, algorithm, subtype;
74 size_t slaves_size, i;
77 subtype = ntohs(nab->subtype);
78 n_slaves = ntohs(nab->n_slaves);
79 fields = ntohs(nab->fields);
80 algorithm = ntohs(nab->algorithm);
81 slave_type = ntohl(nab->slave_type);
82 slaves_size = ntohs(nab->len) - sizeof *nab;
84 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
85 if (!flow_hash_fields_valid(fields)) {
86 VLOG_WARN_RL(&rl, "unsupported fields %"PRIu16, fields);
87 } else if (n_slaves > BUNDLE_MAX_SLAVES) {
88 VLOG_WARN_RL(&rl, "too may slaves");
89 } else if (algorithm != NX_BD_ALG_HRW) {
90 VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16, algorithm);
91 } else if (slave_type != NXM_OF_IN_PORT) {
92 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
97 for (i = 0; i < sizeof(nab->zero); i++) {
99 VLOG_WARN_RL(&rl, "reserved field is nonzero");
100 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
104 if (slaves_size < n_slaves * sizeof(ovs_be16)) {
105 VLOG_WARN_RL(&rl, "Nicira action %"PRIu16" only has %zu bytes "
106 "allocated for slaves. %zu bytes are required for "
107 "%"PRIu16" slaves.", subtype, slaves_size,
108 n_slaves * sizeof(ovs_be16), n_slaves);
109 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
112 for (i = 0; i < n_slaves; i++) {
113 uint16_t ofp_port = bundle_get_slave(nab, i);
114 int ofputil_error = ofputil_check_output_port(ofp_port, max_ports);
117 VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
118 error = ofputil_error;
121 /* Controller slaves are unsupported due to the lack of a max_len
122 * argument. This may or may not change in the future. There doesn't
123 * seem to be a real-world use-case for supporting it. */
124 if (ofp_port == OFPP_CONTROLLER) {
125 VLOG_WARN_RL(&rl, "unsupported controller slave");
126 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
133 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
134 * stores it in 'b'. Sets 'b''s l2 pointer to NULL. */
136 bundle_parse(struct ofpbuf *b, const char *s)
138 char *fields, *basis, *algorithm, *slave_type, *slave_delim;
139 struct nx_action_bundle *nab;
140 char *tokstr, *save_ptr;
145 fields = strtok_r(tokstr, ", ", &save_ptr);
146 basis = strtok_r(NULL, ", ", &save_ptr);
147 algorithm = strtok_r(NULL, ", ", &save_ptr);
148 slave_type = strtok_r(NULL, ", ", &save_ptr);
149 slave_delim = strtok_r(NULL, ": ", &save_ptr);
152 ovs_fatal(0, "%s: not enough arguments to bundle action", s);
155 if (strcasecmp(slave_delim, "slaves")) {
156 ovs_fatal(0, "%s: missing slave delimiter, expected `slaves' got `%s'",
160 b->l2 = ofpbuf_put_zeros(b, sizeof *nab);
167 slave = strtok_r(NULL, ", ", &save_ptr);
168 if (!slave || n_slaves >= BUNDLE_MAX_SLAVES) {
172 slave_be = htons(atoi(slave));
173 ofpbuf_put(b, &slave_be, sizeof slave_be);
178 /* Slaves array must be multiple of 8 bytes long. */
180 ofpbuf_put_zeros(b, 8 - (b->size % 8));
184 nab->type = htons(OFPAT_VENDOR);
185 nab->len = htons(b->size - ((char *) b->l2 - (char *) b->data));
186 nab->vendor = htonl(NX_VENDOR_ID);
187 nab->subtype = htons(NXAST_BUNDLE);
188 nab->n_slaves = htons(n_slaves);
189 nab->basis = htons(atoi(basis));
191 if (!strcasecmp(fields, "eth_src")) {
192 nab->fields = htons(NX_HASH_FIELDS_ETH_SRC);
193 } else if (!strcasecmp(fields, "symmetric_l4")) {
194 nab->fields = htons(NX_HASH_FIELDS_SYMMETRIC_L4);
196 ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
199 if (!strcasecmp(algorithm, "active_backup")) {
200 nab->algorithm = htons(NX_BD_ALG_ACTIVE_BACKUP);
201 } else if (!strcasecmp(algorithm, "hrw")) {
202 nab->algorithm = htons(NX_BD_ALG_HRW);
204 ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
207 if (!strcasecmp(slave_type, "ofport")) {
208 nab->slave_type = htonl(NXM_OF_IN_PORT);
210 ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
217 /* Appends a human-readable representation of 'nab' to 's'. */
219 bundle_format(const struct nx_action_bundle *nab, struct ds *s)
221 const char *fields, *algorithm, *slave_type;
224 fields = flow_hash_fields_to_str(ntohs(nab->fields));
226 switch (ntohs(nab->algorithm)) {
230 case NX_BD_ALG_ACTIVE_BACKUP:
231 algorithm = "active_backup";
234 algorithm = "<unknown>";
237 switch (ntohl(nab->slave_type)) {
239 slave_type = "ofport";
242 slave_type = "<unknown>";
245 ds_put_format(s, "bundle(%s,%"PRIu16",%s,%s,slaves:", fields,
246 ntohs(nab->basis), algorithm, slave_type);
248 for (i = 0; i < ntohs(nab->n_slaves); i++) {
253 ds_put_format(s, "%"PRIu16, bundle_get_slave(nab, i));