bundle: Fix returned error code in one bundle_check() corner case.
[sliver-openvswitch.git] / lib / bundle.c
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
33 VLOG_DEFINE_THIS_MODULE(bundle);
34
35 static uint16_t
36 execute_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
52 static uint16_t
53 execute_hrw(const struct nx_action_bundle *nab, const struct flow *flow,
54             bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
55 {
56     uint32_t flow_hash, best_hash;
57     int best, i;
58
59     flow_hash = flow_hash_fields(flow, ntohs(nab->fields), ntohs(nab->basis));
60     best = -1;
61     best_hash = 0;
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
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. */
80 uint16_t
81 bundle_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
91 void
92 bundle_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
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()). */
104 int
105 bundle_check(const struct nx_action_bundle *nab, int max_ports,
106              const struct flow *flow)
107 {
108     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
109     uint16_t n_slaves, fields, algorithm, subtype;
110     uint32_t slave_type;
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);
118     slave_type = ntohl(nab->slave_type);
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");
126     } else if (algorithm != NX_BD_ALG_HRW
127                && algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
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
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 && !error) {
148         error = nxm_dst_check(nab->dst, nab->ofs_nbits, 16, flow);
149     }
150
151     if (slaves_size < n_slaves * sizeof(ovs_be16)) {
152         VLOG_WARN_RL(&rl, "Nicira action %"PRIu16" only has %zu bytes "
153                      "allocated for slaves.  %zu bytes are required for "
154                      "%"PRIu16" slaves.", subtype, slaves_size,
155                      n_slaves * sizeof(ovs_be16), n_slaves);
156         error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
157     }
158
159     for (i = 0; i < n_slaves; i++) {
160         uint16_t ofp_port = bundle_get_slave(nab, i);
161         int ofputil_error = ofputil_check_output_port(ofp_port, max_ports);
162
163         if (ofputil_error) {
164             VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
165             error = ofputil_error;
166         }
167
168         /* Controller slaves are unsupported due to the lack of a max_len
169          * argument. This may or may not change in the future.  There doesn't
170          * seem to be a real-world use-case for supporting it. */
171         if (ofp_port == OFPP_CONTROLLER) {
172             VLOG_WARN_RL(&rl, "unsupported controller slave");
173             error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
174         }
175     }
176
177     return error;
178 }
179
180 /* Helper for bundle_parse and bundle_parse_load. */
181 static void
182 bundle_parse__(struct ofpbuf *b, const char *s, char **save_ptr,
183                const char *fields, const char *basis, const char *algorithm,
184                const char *slave_type, const char *dst,
185                const char *slave_delim)
186 {
187     struct nx_action_bundle *nab;
188     uint16_t n_slaves;
189
190     if (!slave_delim) {
191         ovs_fatal(0, "%s: not enough arguments to bundle action", s);
192     }
193
194     if (strcasecmp(slave_delim, "slaves")) {
195         ovs_fatal(0, "%s: missing slave delimiter, expected `slaves' got `%s'",
196                    s, slave_delim);
197     }
198
199     b->l2 = ofpbuf_put_zeros(b, sizeof *nab);
200
201     n_slaves = 0;
202     for (;;) {
203         ovs_be16 slave_be;
204         char *slave;
205
206         slave = strtok_r(NULL, ", ", save_ptr);
207         if (!slave || n_slaves >= BUNDLE_MAX_SLAVES) {
208             break;
209         }
210
211         slave_be = htons(atoi(slave));
212         ofpbuf_put(b, &slave_be, sizeof slave_be);
213
214         n_slaves++;
215     }
216
217     /* Slaves array must be multiple of 8 bytes long. */
218     if (b->size % 8) {
219         ofpbuf_put_zeros(b, 8 - (b->size % 8));
220     }
221
222     nab = b->l2;
223     nab->type = htons(OFPAT_VENDOR);
224     nab->len = htons(b->size - ((char *) b->l2 - (char *) b->data));
225     nab->vendor = htonl(NX_VENDOR_ID);
226     nab->subtype = htons(dst ? NXAST_BUNDLE_LOAD: NXAST_BUNDLE);
227     nab->n_slaves = htons(n_slaves);
228     nab->basis = htons(atoi(basis));
229
230     if (!strcasecmp(fields, "eth_src")) {
231         nab->fields = htons(NX_HASH_FIELDS_ETH_SRC);
232     } else if (!strcasecmp(fields, "symmetric_l4")) {
233         nab->fields = htons(NX_HASH_FIELDS_SYMMETRIC_L4);
234     } else {
235         ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
236     }
237
238     if (!strcasecmp(algorithm, "active_backup")) {
239         nab->algorithm = htons(NX_BD_ALG_ACTIVE_BACKUP);
240     } else if (!strcasecmp(algorithm, "hrw")) {
241         nab->algorithm = htons(NX_BD_ALG_HRW);
242     } else {
243         ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
244     }
245
246     if (!strcasecmp(slave_type, "ofport")) {
247         nab->slave_type = htonl(NXM_OF_IN_PORT);
248     } else {
249         ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
250     }
251
252     if (dst) {
253         uint32_t reg;
254         int ofs, n_bits;
255
256         nxm_parse_field_bits(dst, &reg, &ofs, &n_bits);
257
258         nab->dst = htonl(reg);
259         nab->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
260     }
261
262     b->l2 = NULL;
263 }
264
265 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
266  * stores it in 'b'.  Sets 'b''s l2 pointer to NULL. */
267 void
268 bundle_parse(struct ofpbuf *b, const char *s)
269 {
270     char *fields, *basis, *algorithm, *slave_type, *slave_delim;
271     char *tokstr, *save_ptr;
272
273     save_ptr = NULL;
274     tokstr = xstrdup(s);
275     fields = strtok_r(tokstr, ", ", &save_ptr);
276     basis = strtok_r(NULL, ", ", &save_ptr);
277     algorithm = strtok_r(NULL, ", ", &save_ptr);
278     slave_type = strtok_r(NULL, ", ", &save_ptr);
279     slave_delim = strtok_r(NULL, ": ", &save_ptr);
280
281     bundle_parse__(b, s, &save_ptr, fields, basis, algorithm, slave_type, NULL,
282                    slave_delim);
283     free(tokstr);
284 }
285
286 /* Converts a bundle_load action string contained in 's' to an nx_action_bundle
287  * and stores it in 'b'.  Sets 'b''s l2 pointer to NULL. */
288 void
289 bundle_parse_load(struct ofpbuf *b, const char *s)
290 {
291     char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
292     char *tokstr, *save_ptr;
293
294     save_ptr = NULL;
295     tokstr = xstrdup(s);
296     fields = strtok_r(tokstr, ", ", &save_ptr);
297     basis = strtok_r(NULL, ", ", &save_ptr);
298     algorithm = strtok_r(NULL, ", ", &save_ptr);
299     slave_type = strtok_r(NULL, ", ", &save_ptr);
300     dst = strtok_r(NULL, ", ", &save_ptr);
301     slave_delim = strtok_r(NULL, ": ", &save_ptr);
302
303     bundle_parse__(b, s, &save_ptr, fields, basis, algorithm, slave_type, dst,
304                    slave_delim);
305
306     free(tokstr);
307 }
308
309 /* Appends a human-readable representation of 'nab' to 's'. */
310 void
311 bundle_format(const struct nx_action_bundle *nab, struct ds *s)
312 {
313     const char *action, *fields, *algorithm, *slave_type;
314     size_t i;
315
316     fields = flow_hash_fields_to_str(ntohs(nab->fields));
317
318     switch (ntohs(nab->algorithm)) {
319     case NX_BD_ALG_HRW:
320         algorithm = "hrw";
321         break;
322     case NX_BD_ALG_ACTIVE_BACKUP:
323         algorithm = "active_backup";
324         break;
325     default:
326         algorithm = "<unknown>";
327     }
328
329     switch (ntohl(nab->slave_type)) {
330     case NXM_OF_IN_PORT:
331         slave_type = "ofport";
332         break;
333     default:
334         slave_type = "<unknown>";
335     }
336
337     switch (ntohs(nab->subtype)) {
338     case NXAST_BUNDLE:
339         action = "bundle";
340         break;
341     case NXAST_BUNDLE_LOAD:
342         action = "bundle_load";
343         break;
344     default:
345         NOT_REACHED();
346     }
347
348     ds_put_format(s, "%s(%s,%"PRIu16",%s,%s,", action, fields,
349                   ntohs(nab->basis), algorithm, slave_type);
350
351     if (nab->subtype == htons(NXAST_BUNDLE_LOAD)) {
352         nxm_format_field_bits(s, ntohl(nab->dst),
353                               nxm_decode_ofs(nab->ofs_nbits),
354                               nxm_decode_n_bits(nab->ofs_nbits));
355         ds_put_cstr(s, ",");
356     }
357
358     ds_put_cstr(s, "slaves:");
359     for (i = 0; i < ntohs(nab->n_slaves); i++) {
360         if (i) {
361             ds_put_cstr(s, ",");
362         }
363
364         ds_put_format(s, "%"PRIu16, bundle_get_slave(nab, i));
365     }
366
367     ds_put_cstr(s, ")");
368 }