Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / lib / bundle.c
1 /* Copyright (c) 2011, 2012, 2013 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-actions.h"
29 #include "ofp-errors.h"
30 #include "ofp-util.h"
31 #include "openflow/nicira-ext.h"
32 #include "vlog.h"
33
34 #define BUNDLE_MAX_SLAVES 2048
35
36 VLOG_DEFINE_THIS_MODULE(bundle);
37
38 static ofp_port_t
39 execute_ab(const struct ofpact_bundle *bundle,
40            bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
41 {
42     size_t i;
43
44     for (i = 0; i < bundle->n_slaves; i++) {
45         ofp_port_t slave = bundle->slaves[i];
46         if (slave_enabled(slave, aux)) {
47             return slave;
48         }
49     }
50
51     return OFPP_NONE;
52 }
53
54 static ofp_port_t
55 execute_hrw(const struct ofpact_bundle *bundle,
56             const struct flow *flow, struct flow_wildcards *wc,
57             bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
58 {
59     uint32_t flow_hash, best_hash;
60     int best, i;
61
62     if (bundle->n_slaves > 1) {
63         flow_mask_hash_fields(flow, wc, bundle->fields);
64     }
65
66     flow_hash = flow_hash_fields(flow, bundle->fields, bundle->basis);
67     best = -1;
68     best_hash = 0;
69
70     for (i = 0; i < bundle->n_slaves; i++) {
71         if (slave_enabled(bundle->slaves[i], aux)) {
72             uint32_t hash = hash_2words(i, flow_hash);
73
74             if (best < 0 || hash > best_hash) {
75                 best_hash = hash;
76                 best = i;
77             }
78         }
79     }
80
81     return best >= 0 ? bundle->slaves[best] : OFPP_NONE;
82 }
83
84 /* Executes 'bundle' on 'flow'.  Sets fields in 'wc' that were used to
85  * calculate the result.  Uses 'slave_enabled' to determine if the slave
86  * designated by 'ofp_port' is up.  Returns the chosen slave, or
87  * OFPP_NONE if none of the slaves are acceptable. */
88 ofp_port_t
89 bundle_execute(const struct ofpact_bundle *bundle,
90                const struct flow *flow, struct flow_wildcards *wc,
91                bool (*slave_enabled)(ofp_port_t ofp_port, void *aux),
92                void *aux)
93 {
94     switch (bundle->algorithm) {
95     case NX_BD_ALG_HRW:
96         return execute_hrw(bundle, flow, wc, slave_enabled, aux);
97
98     case NX_BD_ALG_ACTIVE_BACKUP:
99         return execute_ab(bundle, slave_enabled, aux);
100
101     default:
102         OVS_NOT_REACHED();
103     }
104 }
105
106 /* Checks that 'nab' specifies a bundle action which is supported by this
107  * bundle module.  Uses the 'max_ports' parameter to validate each port using
108  * ofputil_check_output_port().  Returns 0 if 'nab' is supported, otherwise an
109  * OFPERR_* error code. */
110 enum ofperr
111 bundle_from_openflow(const struct nx_action_bundle *nab,
112                      struct ofpbuf *ofpacts)
113 {
114     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
115     struct ofpact_bundle *bundle;
116     uint16_t subtype;
117     uint32_t slave_type;
118     size_t slaves_size, i;
119     enum ofperr error;
120
121     bundle = ofpact_put_BUNDLE(ofpacts);
122
123     subtype = ntohs(nab->subtype);
124     bundle->n_slaves = ntohs(nab->n_slaves);
125     bundle->basis = ntohs(nab->basis);
126     bundle->fields = ntohs(nab->fields);
127     bundle->algorithm = ntohs(nab->algorithm);
128     slave_type = ntohl(nab->slave_type);
129     slaves_size = ntohs(nab->len) - sizeof *nab;
130
131     error = OFPERR_OFPBAC_BAD_ARGUMENT;
132     if (!flow_hash_fields_valid(bundle->fields)) {
133         VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
134     } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
135         VLOG_WARN_RL(&rl, "too may slaves");
136     } else if (bundle->algorithm != NX_BD_ALG_HRW
137                && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
138         VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
139     } else if (slave_type != NXM_OF_IN_PORT) {
140         VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
141     } else {
142         error = 0;
143     }
144
145     if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
146         VLOG_WARN_RL(&rl, "reserved field is nonzero");
147         error = OFPERR_OFPBAC_BAD_ARGUMENT;
148     }
149
150     if (subtype == NXAST_BUNDLE && (nab->ofs_nbits || nab->dst)) {
151         VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
152         error = OFPERR_OFPBAC_BAD_ARGUMENT;
153     }
154
155     if (subtype == NXAST_BUNDLE_LOAD) {
156         bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
157         bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
158         bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
159
160         if (bundle->dst.n_bits < 16) {
161             VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
162                          "destination.");
163             error = OFPERR_OFPBAC_BAD_ARGUMENT;
164         }
165     }
166
167     if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
168         VLOG_WARN_RL(&rl, "Nicira action %"PRIu16" only has %"PRIuSIZE" bytes "
169                      "allocated for slaves.  %"PRIuSIZE" bytes are required for "
170                      "%"PRIu16" slaves.", subtype, slaves_size,
171                      bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
172         error = OFPERR_OFPBAC_BAD_LEN;
173     }
174
175     for (i = 0; i < bundle->n_slaves; i++) {
176         uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
177         ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
178     }
179
180     bundle = ofpacts->frame;
181     ofpact_update_len(ofpacts, &bundle->ofpact);
182
183     if (!error) {
184         error = bundle_check(bundle, OFPP_MAX, NULL);
185     }
186     return error;
187 }
188
189 enum ofperr
190 bundle_check(const struct ofpact_bundle *bundle, ofp_port_t max_ports,
191              const struct flow *flow)
192 {
193     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
194     size_t i;
195
196     if (bundle->dst.field) {
197         enum ofperr error = mf_check_dst(&bundle->dst, flow);
198         if (error) {
199             return error;
200         }
201     }
202
203     for (i = 0; i < bundle->n_slaves; i++) {
204         ofp_port_t ofp_port = bundle->slaves[i];
205         enum ofperr error;
206
207         error = ofpact_check_output_port(ofp_port, max_ports);
208         if (error) {
209             VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
210             return error;
211         }
212
213         /* Controller slaves are unsupported due to the lack of a max_len
214          * argument. This may or may not change in the future.  There doesn't
215          * seem to be a real-world use-case for supporting it. */
216         if (ofp_port == OFPP_CONTROLLER) {
217             VLOG_WARN_RL(&rl, "unsupported controller slave");
218             return OFPERR_OFPBAC_BAD_OUT_PORT;
219         }
220     }
221
222     return 0;
223 }
224
225 void
226 bundle_to_nxast(const struct ofpact_bundle *bundle, struct ofpbuf *openflow)
227 {
228     int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
229     struct nx_action_bundle *nab;
230     ovs_be16 *slaves;
231     size_t i;
232
233     nab = (bundle->dst.field
234            ? ofputil_put_NXAST_BUNDLE_LOAD(openflow)
235            : ofputil_put_NXAST_BUNDLE(openflow));
236     nab->len = htons(ntohs(nab->len) + slaves_len);
237     nab->algorithm = htons(bundle->algorithm);
238     nab->fields = htons(bundle->fields);
239     nab->basis = htons(bundle->basis);
240     nab->slave_type = htonl(NXM_OF_IN_PORT);
241     nab->n_slaves = htons(bundle->n_slaves);
242     if (bundle->dst.field) {
243         nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
244                                               bundle->dst.n_bits);
245         nab->dst = htonl(bundle->dst.field->nxm_header);
246     }
247
248     slaves = ofpbuf_put_zeros(openflow, slaves_len);
249     for (i = 0; i < bundle->n_slaves; i++) {
250         slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
251     }
252 }
253
254 /* Helper for bundle_parse and bundle_parse_load.
255  *
256  * Returns NULL if successful, otherwise a malloc()'d string describing the
257  * error.  The caller is responsible for freeing the returned string.*/
258 static char * WARN_UNUSED_RESULT
259 bundle_parse__(const char *s, char **save_ptr,
260                const char *fields, const char *basis, const char *algorithm,
261                const char *slave_type, const char *dst,
262                const char *slave_delim, struct ofpbuf *ofpacts)
263 {
264     struct ofpact_bundle *bundle;
265
266     if (!slave_delim) {
267         return xasprintf("%s: not enough arguments to bundle action", s);
268     }
269
270     if (strcasecmp(slave_delim, "slaves")) {
271         return xasprintf("%s: missing slave delimiter, expected `slaves' "
272                          "got `%s'", s, slave_delim);
273     }
274
275     bundle = ofpact_put_BUNDLE(ofpacts);
276
277     for (;;) {
278         ofp_port_t slave_port;
279         char *slave;
280
281         slave = strtok_r(NULL, ", []", save_ptr);
282         if (!slave || bundle->n_slaves >= BUNDLE_MAX_SLAVES) {
283             break;
284         }
285
286         if (!ofputil_port_from_string(slave, &slave_port)) {
287             return xasprintf("%s: bad port number", slave);
288         }
289         ofpbuf_put(ofpacts, &slave_port, sizeof slave_port);
290
291         bundle = ofpacts->frame;
292         bundle->n_slaves++;
293     }
294     ofpact_update_len(ofpacts, &bundle->ofpact);
295
296     bundle->basis = atoi(basis);
297
298     if (!strcasecmp(fields, "eth_src")) {
299         bundle->fields = NX_HASH_FIELDS_ETH_SRC;
300     } else if (!strcasecmp(fields, "symmetric_l4")) {
301         bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4;
302     } else {
303         return xasprintf("%s: unknown fields `%s'", s, fields);
304     }
305
306     if (!strcasecmp(algorithm, "active_backup")) {
307         bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
308     } else if (!strcasecmp(algorithm, "hrw")) {
309         bundle->algorithm = NX_BD_ALG_HRW;
310     } else {
311         return xasprintf("%s: unknown algorithm `%s'", s, algorithm);
312     }
313
314     if (strcasecmp(slave_type, "ofport")) {
315         return xasprintf("%s: unknown slave_type `%s'", s, slave_type);
316     }
317
318     if (dst) {
319         char *error = mf_parse_subfield(&bundle->dst, dst);
320         if (error) {
321             return error;
322         }
323     }
324
325     return NULL;
326 }
327
328 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
329  * stores it in 'b'.  Sets 'b''s l2 pointer to NULL.
330  *
331  * Returns NULL if successful, otherwise a malloc()'d string describing the
332  * error.  The caller is responsible for freeing the returned string. */
333 char * WARN_UNUSED_RESULT
334 bundle_parse(const char *s, struct ofpbuf *ofpacts)
335 {
336     char *fields, *basis, *algorithm, *slave_type, *slave_delim;
337     char *tokstr, *save_ptr;
338     char *error;
339
340     save_ptr = NULL;
341     tokstr = xstrdup(s);
342     fields = strtok_r(tokstr, ", ", &save_ptr);
343     basis = strtok_r(NULL, ", ", &save_ptr);
344     algorithm = strtok_r(NULL, ", ", &save_ptr);
345     slave_type = strtok_r(NULL, ", ", &save_ptr);
346     slave_delim = strtok_r(NULL, ": ", &save_ptr);
347
348     error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
349                            NULL, slave_delim, ofpacts);
350     free(tokstr);
351
352     return error;
353 }
354
355 /* Converts a bundle_load action string contained in 's' to an nx_action_bundle
356  * and stores it in 'b'.  Sets 'b''s l2 pointer to NULL.
357  *
358  * Returns NULL if successful, otherwise a malloc()'d string describing the
359  * error.  The caller is responsible for freeing the returned string.*/
360 char * WARN_UNUSED_RESULT
361 bundle_parse_load(const char *s, struct ofpbuf *ofpacts)
362 {
363     char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
364     char *tokstr, *save_ptr;
365     char *error;
366
367     save_ptr = NULL;
368     tokstr = xstrdup(s);
369     fields = strtok_r(tokstr, ", ", &save_ptr);
370     basis = strtok_r(NULL, ", ", &save_ptr);
371     algorithm = strtok_r(NULL, ", ", &save_ptr);
372     slave_type = strtok_r(NULL, ", ", &save_ptr);
373     dst = strtok_r(NULL, ", ", &save_ptr);
374     slave_delim = strtok_r(NULL, ": ", &save_ptr);
375
376     error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
377                            dst, slave_delim, ofpacts);
378
379     free(tokstr);
380
381     return error;
382 }
383
384 /* Appends a human-readable representation of 'nab' to 's'. */
385 void
386 bundle_format(const struct ofpact_bundle *bundle, struct ds *s)
387 {
388     const char *action, *fields, *algorithm;
389     size_t i;
390
391     fields = flow_hash_fields_to_str(bundle->fields);
392
393     switch (bundle->algorithm) {
394     case NX_BD_ALG_HRW:
395         algorithm = "hrw";
396         break;
397     case NX_BD_ALG_ACTIVE_BACKUP:
398         algorithm = "active_backup";
399         break;
400     default:
401         algorithm = "<unknown>";
402     }
403
404     action = bundle->dst.field ? "bundle_load" : "bundle";
405
406     ds_put_format(s, "%s(%s,%"PRIu16",%s,%s,", action, fields,
407                   bundle->basis, algorithm, "ofport");
408
409     if (bundle->dst.field) {
410         mf_format_subfield(&bundle->dst, s);
411         ds_put_cstr(s, ",");
412     }
413
414     ds_put_cstr(s, "slaves:");
415     for (i = 0; i < bundle->n_slaves; i++) {
416         if (i) {
417             ds_put_cstr(s, ",");
418         }
419
420         ofputil_format_port(bundle->slaves[i], s);
421     }
422
423     ds_put_cstr(s, ")");
424 }