Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / bundle.c
1 /* Copyright (c) 2011, 2012 Nicira, Inc.
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 "meta-flow.h"
26 #include "nx-match.h"
27 #include "ofpbuf.h"
28 #include "ofp-errors.h"
29 #include "ofp-util.h"
30 #include "openflow/nicira-ext.h"
31 #include "vlog.h"
32
33 #define BUNDLE_MAX_SLAVES 2048
34
35 VLOG_DEFINE_THIS_MODULE(bundle);
36
37 static uint16_t
38 execute_ab(const struct nx_action_bundle *nab,
39            bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
40 {
41     size_t i;
42
43     for (i = 0; i < ntohs(nab->n_slaves); i++) {
44         uint16_t slave = bundle_get_slave(nab, i);
45
46         if (slave_enabled(slave, aux)) {
47             return slave;
48         }
49     }
50
51     return OFPP_NONE;
52 }
53
54 static uint16_t
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)
57 {
58     uint32_t flow_hash, best_hash;
59     int best, i;
60
61     flow_hash = flow_hash_fields(flow, ntohs(nab->fields), ntohs(nab->basis));
62     best = -1;
63     best_hash = 0;
64
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);
68
69             if (best < 0 || hash > best_hash) {
70                 best_hash = hash;
71                 best = i;
72             }
73         }
74     }
75
76     return best >= 0 ? bundle_get_slave(nab, best) : OFPP_NONE;
77 }
78
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. */
82 uint16_t
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)
85 {
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();
90     }
91 }
92
93 void
94 bundle_execute_load(const struct nx_action_bundle *nab, struct flow *flow,
95                     bool (*slave_enabled)(uint16_t ofp_port, void *aux),
96                     void *aux)
97 {
98     struct mf_subfield dst;
99
100     nxm_decode(&dst, nab->dst, nab->ofs_nbits);
101     mf_set_subfield_value(&dst, bundle_execute(nab, flow, slave_enabled, aux),
102                           flow);
103 }
104
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. */
109 enum ofperr
110 bundle_check(const struct nx_action_bundle *nab, int max_ports,
111              const struct flow *flow)
112 {
113     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
114     uint16_t n_slaves, fields, algorithm, subtype;
115     uint32_t slave_type;
116     size_t slaves_size, i;
117     enum ofperr error;
118
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;
125
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);
136     } else {
137         error = 0;
138     }
139
140     for (i = 0; i < sizeof(nab->zero); i++) {
141         if (nab->zero[i]) {
142             VLOG_WARN_RL(&rl, "reserved field is nonzero");
143             error = OFPERR_OFPBAC_BAD_ARGUMENT;
144         }
145     }
146
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;
150     }
151
152     if (subtype == NXAST_BUNDLE_LOAD) {
153         struct mf_subfield dst;
154
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 "
158                          "destination.");
159             error = OFPERR_OFPBAC_BAD_ARGUMENT;
160         } else if (!error) {
161             error = mf_check_dst(&dst, flow);
162         }
163     }
164
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;
171     }
172
173     for (i = 0; i < n_slaves; i++) {
174         uint16_t ofp_port = bundle_get_slave(nab, i);
175         enum ofperr ofputil_error;
176
177         ofputil_error = ofputil_check_output_port(ofp_port, max_ports);
178         if (ofputil_error) {
179             VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
180             error = ofputil_error;
181         }
182
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;
189         }
190     }
191
192     return error;
193 }
194
195 /* Helper for bundle_parse and bundle_parse_load. */
196 static void
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)
201 {
202     enum ofputil_action_code code;
203     struct nx_action_bundle *nab;
204     uint16_t n_slaves;
205
206     if (!slave_delim) {
207         ovs_fatal(0, "%s: not enough arguments to bundle action", s);
208     }
209
210     if (strcasecmp(slave_delim, "slaves")) {
211         ovs_fatal(0, "%s: missing slave delimiter, expected `slaves' got `%s'",
212                    s, slave_delim);
213     }
214
215     code = dst_s ? OFPUTIL_NXAST_BUNDLE_LOAD : OFPUTIL_NXAST_BUNDLE;
216     b->l2 = ofputil_put_action(code, b);
217
218     n_slaves = 0;
219     for (;;) {
220         ovs_be16 slave_be;
221         char *slave;
222
223         slave = strtok_r(NULL, ", [", save_ptr);
224         if (!slave || n_slaves >= BUNDLE_MAX_SLAVES) {
225             break;
226         }
227
228         slave_be = htons(atoi(slave));
229         ofpbuf_put(b, &slave_be, sizeof slave_be);
230
231         n_slaves++;
232     }
233
234     /* Slaves array must be multiple of 8 bytes long. */
235     if (b->size % 8) {
236         ofpbuf_put_zeros(b, 8 - (b->size % 8));
237     }
238
239     nab = b->l2;
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));
243
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);
248     } else {
249         ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
250     }
251
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);
256     } else {
257         ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
258     }
259
260     if (!strcasecmp(slave_type, "ofport")) {
261         nab->slave_type = htonl(NXM_OF_IN_PORT);
262     } else {
263         ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
264     }
265
266     if (dst_s) {
267         struct mf_subfield dst;
268
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);
272     }
273
274     b->l2 = NULL;
275 }
276
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. */
279 void
280 bundle_parse(struct ofpbuf *b, const char *s)
281 {
282     char *fields, *basis, *algorithm, *slave_type, *slave_delim;
283     char *tokstr, *save_ptr;
284
285     save_ptr = NULL;
286     tokstr = xstrdup(s);
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);
292
293     bundle_parse__(b, s, &save_ptr, fields, basis, algorithm, slave_type, NULL,
294                    slave_delim);
295     free(tokstr);
296 }
297
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. */
300 void
301 bundle_parse_load(struct ofpbuf *b, const char *s)
302 {
303     char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
304     char *tokstr, *save_ptr;
305
306     save_ptr = NULL;
307     tokstr = xstrdup(s);
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);
314
315     bundle_parse__(b, s, &save_ptr, fields, basis, algorithm, slave_type, dst,
316                    slave_delim);
317
318     free(tokstr);
319 }
320
321 /* Appends a human-readable representation of 'nab' to 's'. */
322 void
323 bundle_format(const struct nx_action_bundle *nab, struct ds *s)
324 {
325     const char *action, *fields, *algorithm, *slave_type;
326     size_t i;
327
328     fields = flow_hash_fields_to_str(ntohs(nab->fields));
329
330     switch (ntohs(nab->algorithm)) {
331     case NX_BD_ALG_HRW:
332         algorithm = "hrw";
333         break;
334     case NX_BD_ALG_ACTIVE_BACKUP:
335         algorithm = "active_backup";
336         break;
337     default:
338         algorithm = "<unknown>";
339     }
340
341     switch (ntohl(nab->slave_type)) {
342     case NXM_OF_IN_PORT:
343         slave_type = "ofport";
344         break;
345     default:
346         slave_type = "<unknown>";
347     }
348
349     switch (ntohs(nab->subtype)) {
350     case NXAST_BUNDLE:
351         action = "bundle";
352         break;
353     case NXAST_BUNDLE_LOAD:
354         action = "bundle_load";
355         break;
356     default:
357         NOT_REACHED();
358     }
359
360     ds_put_format(s, "%s(%s,%"PRIu16",%s,%s,", action, fields,
361                   ntohs(nab->basis), algorithm, slave_type);
362
363     if (nab->subtype == htons(NXAST_BUNDLE_LOAD)) {
364         struct mf_subfield dst;
365
366         nxm_decode(&dst, nab->dst, nab->ofs_nbits);
367         mf_format_subfield(&dst, s);
368         ds_put_cstr(s, ",");
369     }
370
371     ds_put_cstr(s, "slaves:");
372     for (i = 0; i < ntohs(nab->n_slaves); i++) {
373         if (i) {
374             ds_put_cstr(s, ",");
375         }
376
377         ds_put_format(s, "%"PRIu16, bundle_get_slave(nab, i));
378     }
379
380     ds_put_cstr(s, ")");
381 }