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));
51 for (i = 0; i < ntohs(nab->n_slaves); i++) {
52 if (slave_enabled(bundle_get_slave(nab, i), aux)) {
53 uint32_t hash = hash_2words(i, flow_hash);
55 if (best < 0 || hash > best_hash) {
62 return best >= 0 ? bundle_get_slave(nab, best) : OFPP_NONE;
65 /* Checks that 'nab' specifies a bundle action which is supported by this
66 * bundle module. Uses the 'max_ports' parameter to validate each port using
67 * ofputil_check_output_port(). Returns 0 if 'nab' is supported, otherwise an
68 * OpenFlow error code (as returned by ofp_mkerr()). */
70 bundle_check(const struct nx_action_bundle *nab, int max_ports)
72 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
73 uint16_t n_slaves, fields, algorithm, subtype;
75 size_t slaves_size, i;
78 subtype = ntohs(nab->subtype);
79 n_slaves = ntohs(nab->n_slaves);
80 fields = ntohs(nab->fields);
81 algorithm = ntohs(nab->algorithm);
82 slave_type = ntohl(nab->slave_type);
83 slaves_size = ntohs(nab->len) - sizeof *nab;
85 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
86 if (!flow_hash_fields_valid(fields)) {
87 VLOG_WARN_RL(&rl, "unsupported fields %"PRIu16, fields);
88 } else if (n_slaves > BUNDLE_MAX_SLAVES) {
89 VLOG_WARN_RL(&rl, "too may slaves");
90 } else if (algorithm != NX_BD_ALG_HRW) {
91 VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16, algorithm);
92 } else if (slave_type != NXM_OF_IN_PORT) {
93 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
98 for (i = 0; i < sizeof(nab->zero); i++) {
100 VLOG_WARN_RL(&rl, "reserved field is nonzero");
101 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
105 if (slaves_size < n_slaves * sizeof(ovs_be16)) {
106 VLOG_WARN_RL(&rl, "Nicira action %"PRIu16" only has %zu bytes "
107 "allocated for slaves. %zu bytes are required for "
108 "%"PRIu16" slaves.", subtype, slaves_size,
109 n_slaves * sizeof(ovs_be16), n_slaves);
110 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
113 for (i = 0; i < n_slaves; i++) {
114 uint16_t ofp_port = bundle_get_slave(nab, i);
115 int ofputil_error = ofputil_check_output_port(ofp_port, max_ports);
118 VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
119 error = ofputil_error;
122 /* Controller slaves are unsupported due to the lack of a max_len
123 * argument. This may or may not change in the future. There doesn't
124 * seem to be a real-world use-case for supporting it. */
125 if (ofp_port == OFPP_CONTROLLER) {
126 VLOG_WARN_RL(&rl, "unsupported controller slave");
127 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
134 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
135 * stores it in 'b'. Sets 'b''s l2 pointer to NULL. */
137 bundle_parse(struct ofpbuf *b, const char *s)
139 char *fields, *basis, *algorithm, *slave_type, *slave_delim;
140 struct nx_action_bundle *nab;
141 char *tokstr, *save_ptr;
146 fields = strtok_r(tokstr, ", ", &save_ptr);
147 basis = strtok_r(NULL, ", ", &save_ptr);
148 algorithm = strtok_r(NULL, ", ", &save_ptr);
149 slave_type = strtok_r(NULL, ", ", &save_ptr);
150 slave_delim = strtok_r(NULL, ": ", &save_ptr);
153 ovs_fatal(0, "%s: not enough arguments to bundle action", s);
156 if (strcasecmp(slave_delim, "slaves")) {
157 ovs_fatal(0, "%s: missing slave delimiter, expected `slaves' got `%s'",
161 b->l2 = ofpbuf_put_zeros(b, sizeof *nab);
168 slave = strtok_r(NULL, ", ", &save_ptr);
169 if (!slave || n_slaves >= BUNDLE_MAX_SLAVES) {
173 slave_be = htons(atoi(slave));
174 ofpbuf_put(b, &slave_be, sizeof slave_be);
179 /* Slaves array must be multiple of 8 bytes long. */
181 ofpbuf_put_zeros(b, 8 - (b->size % 8));
185 nab->type = htons(OFPAT_VENDOR);
186 nab->len = htons(b->size - ((char *) b->l2 - (char *) b->data));
187 nab->vendor = htonl(NX_VENDOR_ID);
188 nab->subtype = htons(NXAST_BUNDLE);
189 nab->n_slaves = htons(n_slaves);
190 nab->basis = htons(atoi(basis));
192 if (!strcasecmp(fields, "eth_src")) {
193 nab->fields = htons(NX_HASH_FIELDS_ETH_SRC);
194 } else if (!strcasecmp(fields, "symmetric_l4")) {
195 nab->fields = htons(NX_HASH_FIELDS_SYMMETRIC_L4);
197 ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
200 if (!strcasecmp(algorithm, "active_backup")) {
201 nab->algorithm = htons(NX_BD_ALG_ACTIVE_BACKUP);
202 } else if (!strcasecmp(algorithm, "hrw")) {
203 nab->algorithm = htons(NX_BD_ALG_HRW);
205 ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
208 if (!strcasecmp(slave_type, "ofport")) {
209 nab->slave_type = htonl(NXM_OF_IN_PORT);
211 ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
218 /* Appends a human-readable representation of 'nab' to 's'. */
220 bundle_format(const struct nx_action_bundle *nab, struct ds *s)
222 const char *fields, *algorithm, *slave_type;
225 fields = flow_hash_fields_to_str(ntohs(nab->fields));
227 switch (ntohs(nab->algorithm)) {
231 case NX_BD_ALG_ACTIVE_BACKUP:
232 algorithm = "active_backup";
235 algorithm = "<unknown>";
238 switch (ntohl(nab->slave_type)) {
240 slave_type = "ofport";
243 slave_type = "<unknown>";
246 ds_put_format(s, "bundle(%s,%"PRIu16",%s,%s,slaves:", fields,
247 ntohs(nab->basis), algorithm, slave_type);
249 for (i = 0; i < ntohs(nab->n_slaves); i++) {
254 ds_put_format(s, "%"PRIu16, bundle_get_slave(nab, i));