]> git.proxmox.com Git - mirror_ovs.git/blame - lib/bundle.c
nicira-ext: Fix stale documentation in the bundle action.
[mirror_ovs.git] / lib / bundle.c
CommitLineData
daff3353
EJ
1/* Copyright (c) 2011 Nicira Networks.
2 *
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <config.h>
17
18#include "bundle.h"
19
20#include <arpa/inet.h>
21#include <inttypes.h>
22
23#include "dynamic-string.h"
24#include "multipath.h"
25#include "nx-match.h"
26#include "ofpbuf.h"
27#include "ofp-util.h"
28#include "openflow/nicira-ext.h"
29#include "vlog.h"
30
31#define BUNDLE_MAX_SLAVES 2048
32
33VLOG_DEFINE_THIS_MODULE(bundle);
34
04a85497
EJ
35static uint16_t
36execute_ab(const struct nx_action_bundle *nab,
37 bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
38{
39 size_t i;
40
41 for (i = 0; i < ntohs(nab->n_slaves); i++) {
42 uint16_t slave = bundle_get_slave(nab, i);
43
44 if (slave_enabled(slave, aux)) {
45 return slave;
46 }
47 }
48
49 return OFPP_NONE;
50}
51
52static uint16_t
53execute_hrw(const struct nx_action_bundle *nab, const struct flow *flow,
54 bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
daff3353
EJ
55{
56 uint32_t flow_hash, best_hash;
57 int best, i;
58
daff3353
EJ
59 flow_hash = flow_hash_fields(flow, ntohs(nab->fields), ntohs(nab->basis));
60 best = -1;
481db488 61 best_hash = 0;
daff3353
EJ
62
63 for (i = 0; i < ntohs(nab->n_slaves); i++) {
64 if (slave_enabled(bundle_get_slave(nab, i), aux)) {
65 uint32_t hash = hash_2words(i, flow_hash);
66
67 if (best < 0 || hash > best_hash) {
68 best_hash = hash;
69 best = i;
70 }
71 }
72 }
73
74 return best >= 0 ? bundle_get_slave(nab, best) : OFPP_NONE;
75}
76
04a85497
EJ
77/* Executes 'nab' on 'flow'. Uses 'slave_enabled' to determine if the slave
78 * designated by 'ofp_port' is up. Returns the chosen slave, or OFPP_NONE if
79 * none of the slaves are acceptable. */
80uint16_t
81bundle_execute(const struct nx_action_bundle *nab, const struct flow *flow,
82 bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
83{
84 switch (ntohs(nab->algorithm)) {
85 case NX_BD_ALG_HRW: return execute_hrw(nab, flow, slave_enabled, aux);
86 case NX_BD_ALG_ACTIVE_BACKUP: return execute_ab(nab, slave_enabled, aux);
87 default: NOT_REACHED();
88 }
89}
90
a368bb53
EJ
91void
92bundle_execute_load(const struct nx_action_bundle *nab, struct flow *flow,
93 bool (*slave_enabled)(uint16_t ofp_port, void *aux),
94 void *aux)
95{
96 nxm_reg_load(nab->dst, nab->ofs_nbits,
97 bundle_execute(nab, flow, slave_enabled, aux), flow);
98}
99
daff3353
EJ
100/* Checks that 'nab' specifies a bundle action which is supported by this
101 * bundle module. Uses the 'max_ports' parameter to validate each port using
102 * ofputil_check_output_port(). Returns 0 if 'nab' is supported, otherwise an
103 * OpenFlow error code (as returned by ofp_mkerr()). */
104int
a368bb53
EJ
105bundle_check(const struct nx_action_bundle *nab, int max_ports,
106 const struct flow *flow)
daff3353
EJ
107{
108 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
8815f11c
BP
109 uint16_t n_slaves, fields, algorithm, subtype;
110 uint32_t slave_type;
daff3353
EJ
111 size_t slaves_size, i;
112 int error;
113
114 subtype = ntohs(nab->subtype);
115 n_slaves = ntohs(nab->n_slaves);
116 fields = ntohs(nab->fields);
117 algorithm = ntohs(nab->algorithm);
8815f11c 118 slave_type = ntohl(nab->slave_type);
daff3353
EJ
119 slaves_size = ntohs(nab->len) - sizeof *nab;
120
121 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
122 if (!flow_hash_fields_valid(fields)) {
123 VLOG_WARN_RL(&rl, "unsupported fields %"PRIu16, fields);
124 } else if (n_slaves > BUNDLE_MAX_SLAVES) {
125 VLOG_WARN_RL(&rl, "too may slaves");
04a85497
EJ
126 } else if (algorithm != NX_BD_ALG_HRW
127 && algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
daff3353
EJ
128 VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16, algorithm);
129 } else if (slave_type != NXM_OF_IN_PORT) {
130 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
131 } else {
132 error = 0;
133 }
134
135 for (i = 0; i < sizeof(nab->zero); i++) {
136 if (nab->zero[i]) {
137 VLOG_WARN_RL(&rl, "reserved field is nonzero");
138 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
139 }
140 }
141
a368bb53
EJ
142 if (subtype == NXAST_BUNDLE && (nab->ofs_nbits || nab->dst)) {
143 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
144 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
145 }
146
147 if (subtype == NXAST_BUNDLE_LOAD) {
ce523f65
EJ
148 int ofs = nxm_decode_ofs(nab->ofs_nbits);
149 int n_bits = nxm_decode_n_bits(nab->ofs_nbits);
150
151 if (n_bits < 16) {
152 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
153 "destination.");
154 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
155 } else {
156 error = nxm_dst_check(nab->dst, ofs, n_bits, flow) || error;
157 }
a368bb53
EJ
158 }
159
daff3353
EJ
160 if (slaves_size < n_slaves * sizeof(ovs_be16)) {
161 VLOG_WARN_RL(&rl, "Nicira action %"PRIu16" only has %zu bytes "
162 "allocated for slaves. %zu bytes are required for "
163 "%"PRIu16" slaves.", subtype, slaves_size,
164 n_slaves * sizeof(ovs_be16), n_slaves);
165 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
166 }
167
168 for (i = 0; i < n_slaves; i++) {
169 uint16_t ofp_port = bundle_get_slave(nab, i);
170 int ofputil_error = ofputil_check_output_port(ofp_port, max_ports);
171
172 if (ofputil_error) {
173 VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
174 error = ofputil_error;
175 }
176
177 /* Controller slaves are unsupported due to the lack of a max_len
178 * argument. This may or may not change in the future. There doesn't
179 * seem to be a real-world use-case for supporting it. */
180 if (ofp_port == OFPP_CONTROLLER) {
181 VLOG_WARN_RL(&rl, "unsupported controller slave");
182 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
183 }
184 }
185
186 return error;
187}
188
a368bb53
EJ
189/* Helper for bundle_parse and bundle_parse_load. */
190static void
191bundle_parse__(struct ofpbuf *b, const char *s, char **save_ptr,
192 const char *fields, const char *basis, const char *algorithm,
193 const char *slave_type, const char *dst,
194 const char *slave_delim)
daff3353 195{
93996add 196 enum ofputil_action_code code;
daff3353 197 struct nx_action_bundle *nab;
daff3353
EJ
198 uint16_t n_slaves;
199
daff3353
EJ
200 if (!slave_delim) {
201 ovs_fatal(0, "%s: not enough arguments to bundle action", s);
202 }
203
204 if (strcasecmp(slave_delim, "slaves")) {
205 ovs_fatal(0, "%s: missing slave delimiter, expected `slaves' got `%s'",
206 s, slave_delim);
207 }
208
93996add
BP
209 code = dst ? OFPUTIL_NXAST_BUNDLE_LOAD : OFPUTIL_NXAST_BUNDLE;
210 b->l2 = ofputil_put_action(code, b);
daff3353
EJ
211
212 n_slaves = 0;
213 for (;;) {
214 ovs_be16 slave_be;
215 char *slave;
216
a368bb53 217 slave = strtok_r(NULL, ", ", save_ptr);
daff3353
EJ
218 if (!slave || n_slaves >= BUNDLE_MAX_SLAVES) {
219 break;
220 }
221
222 slave_be = htons(atoi(slave));
223 ofpbuf_put(b, &slave_be, sizeof slave_be);
224
225 n_slaves++;
226 }
227
228 /* Slaves array must be multiple of 8 bytes long. */
229 if (b->size % 8) {
230 ofpbuf_put_zeros(b, 8 - (b->size % 8));
231 }
232
233 nab = b->l2;
daff3353 234 nab->len = htons(b->size - ((char *) b->l2 - (char *) b->data));
daff3353
EJ
235 nab->n_slaves = htons(n_slaves);
236 nab->basis = htons(atoi(basis));
237
238 if (!strcasecmp(fields, "eth_src")) {
239 nab->fields = htons(NX_HASH_FIELDS_ETH_SRC);
240 } else if (!strcasecmp(fields, "symmetric_l4")) {
241 nab->fields = htons(NX_HASH_FIELDS_SYMMETRIC_L4);
242 } else {
243 ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
244 }
245
246 if (!strcasecmp(algorithm, "active_backup")) {
247 nab->algorithm = htons(NX_BD_ALG_ACTIVE_BACKUP);
248 } else if (!strcasecmp(algorithm, "hrw")) {
249 nab->algorithm = htons(NX_BD_ALG_HRW);
250 } else {
251 ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
252 }
253
254 if (!strcasecmp(slave_type, "ofport")) {
8815f11c 255 nab->slave_type = htonl(NXM_OF_IN_PORT);
daff3353
EJ
256 } else {
257 ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
258 }
259
a368bb53
EJ
260 if (dst) {
261 uint32_t reg;
262 int ofs, n_bits;
263
264 nxm_parse_field_bits(dst, &reg, &ofs, &n_bits);
265
266 nab->dst = htonl(reg);
267 nab->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
268 }
269
daff3353 270 b->l2 = NULL;
a368bb53
EJ
271}
272
273/* Converts a bundle action string contained in 's' to an nx_action_bundle and
274 * stores it in 'b'. Sets 'b''s l2 pointer to NULL. */
275void
276bundle_parse(struct ofpbuf *b, const char *s)
277{
278 char *fields, *basis, *algorithm, *slave_type, *slave_delim;
279 char *tokstr, *save_ptr;
280
281 save_ptr = NULL;
282 tokstr = xstrdup(s);
283 fields = strtok_r(tokstr, ", ", &save_ptr);
284 basis = strtok_r(NULL, ", ", &save_ptr);
285 algorithm = strtok_r(NULL, ", ", &save_ptr);
286 slave_type = strtok_r(NULL, ", ", &save_ptr);
287 slave_delim = strtok_r(NULL, ": ", &save_ptr);
288
289 bundle_parse__(b, s, &save_ptr, fields, basis, algorithm, slave_type, NULL,
290 slave_delim);
291 free(tokstr);
292}
293
294/* Converts a bundle_load action string contained in 's' to an nx_action_bundle
295 * and stores it in 'b'. Sets 'b''s l2 pointer to NULL. */
296void
297bundle_parse_load(struct ofpbuf *b, const char *s)
298{
299 char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
300 char *tokstr, *save_ptr;
301
302 save_ptr = NULL;
303 tokstr = xstrdup(s);
304 fields = strtok_r(tokstr, ", ", &save_ptr);
305 basis = strtok_r(NULL, ", ", &save_ptr);
306 algorithm = strtok_r(NULL, ", ", &save_ptr);
307 slave_type = strtok_r(NULL, ", ", &save_ptr);
308 dst = strtok_r(NULL, ", ", &save_ptr);
309 slave_delim = strtok_r(NULL, ": ", &save_ptr);
310
311 bundle_parse__(b, s, &save_ptr, fields, basis, algorithm, slave_type, dst,
312 slave_delim);
313
daff3353
EJ
314 free(tokstr);
315}
316
317/* Appends a human-readable representation of 'nab' to 's'. */
318void
319bundle_format(const struct nx_action_bundle *nab, struct ds *s)
320{
a368bb53 321 const char *action, *fields, *algorithm, *slave_type;
daff3353
EJ
322 size_t i;
323
324 fields = flow_hash_fields_to_str(ntohs(nab->fields));
325
326 switch (ntohs(nab->algorithm)) {
327 case NX_BD_ALG_HRW:
328 algorithm = "hrw";
329 break;
330 case NX_BD_ALG_ACTIVE_BACKUP:
331 algorithm = "active_backup";
332 break;
333 default:
334 algorithm = "<unknown>";
335 }
336
8815f11c 337 switch (ntohl(nab->slave_type)) {
daff3353
EJ
338 case NXM_OF_IN_PORT:
339 slave_type = "ofport";
340 break;
341 default:
342 slave_type = "<unknown>";
343 }
344
a368bb53
EJ
345 switch (ntohs(nab->subtype)) {
346 case NXAST_BUNDLE:
347 action = "bundle";
348 break;
349 case NXAST_BUNDLE_LOAD:
350 action = "bundle_load";
351 break;
352 default:
353 NOT_REACHED();
354 }
355
356 ds_put_format(s, "%s(%s,%"PRIu16",%s,%s,", action, fields,
daff3353
EJ
357 ntohs(nab->basis), algorithm, slave_type);
358
a368bb53
EJ
359 if (nab->subtype == htons(NXAST_BUNDLE_LOAD)) {
360 nxm_format_field_bits(s, ntohl(nab->dst),
361 nxm_decode_ofs(nab->ofs_nbits),
362 nxm_decode_n_bits(nab->ofs_nbits));
363 ds_put_cstr(s, ",");
364 }
365
366 ds_put_cstr(s, "slaves:");
daff3353
EJ
367 for (i = 0; i < ntohs(nab->n_slaves); i++) {
368 if (i) {
369 ds_put_cstr(s, ",");
370 }
371
372 ds_put_format(s, "%"PRIu16, bundle_get_slave(nab, i));
373 }
374
375 ds_put_cstr(s, ")");
376}