Move lib/ofp-errors.h to include/openvswitch directory
[cascardo/ovs.git] / lib / bundle.c
1 /* Copyright (c) 2011, 2012, 2013, 2014, 2015 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 "colors.h"
24 #include "dynamic-string.h"
25 #include "multipath.h"
26 #include "meta-flow.h"
27 #include "nx-match.h"
28 #include "ofpbuf.h"
29 #include "ofp-actions.h"
30 #include "ofp-util.h"
31 #include "openflow/nicira-ext.h"
32 #include "openvswitch/ofp-errors.h"
33 #include "openvswitch/vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(bundle);
36
37 static ofp_port_t
38 execute_ab(const struct ofpact_bundle *bundle,
39            bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
40 {
41     size_t i;
42
43     for (i = 0; i < bundle->n_slaves; i++) {
44         ofp_port_t slave = bundle->slaves[i];
45         if (slave_enabled(slave, aux)) {
46             return slave;
47         }
48     }
49
50     return OFPP_NONE;
51 }
52
53 static ofp_port_t
54 execute_hrw(const struct ofpact_bundle *bundle,
55             const struct flow *flow, struct flow_wildcards *wc,
56             bool (*slave_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
57 {
58     uint32_t flow_hash, best_hash;
59     int best, i;
60
61     if (bundle->n_slaves > 1) {
62         flow_mask_hash_fields(flow, wc, bundle->fields);
63     }
64
65     flow_hash = flow_hash_fields(flow, bundle->fields, bundle->basis);
66     best = -1;
67     best_hash = 0;
68
69     for (i = 0; i < bundle->n_slaves; i++) {
70         if (slave_enabled(bundle->slaves[i], aux)) {
71             uint32_t hash = hash_2words(i, flow_hash);
72
73             if (best < 0 || hash > best_hash) {
74                 best_hash = hash;
75                 best = i;
76             }
77         }
78     }
79
80     return best >= 0 ? bundle->slaves[best] : OFPP_NONE;
81 }
82
83 /* Executes 'bundle' on 'flow'.  Sets fields in 'wc' that were used to
84  * calculate the result.  Uses 'slave_enabled' to determine if the slave
85  * designated by 'ofp_port' is up.  Returns the chosen slave, or
86  * OFPP_NONE if none of the slaves are acceptable. */
87 ofp_port_t
88 bundle_execute(const struct ofpact_bundle *bundle,
89                const struct flow *flow, struct flow_wildcards *wc,
90                bool (*slave_enabled)(ofp_port_t ofp_port, void *aux),
91                void *aux)
92 {
93     switch (bundle->algorithm) {
94     case NX_BD_ALG_HRW:
95         return execute_hrw(bundle, flow, wc, slave_enabled, aux);
96
97     case NX_BD_ALG_ACTIVE_BACKUP:
98         return execute_ab(bundle, slave_enabled, aux);
99
100     default:
101         OVS_NOT_REACHED();
102     }
103 }
104
105 enum ofperr
106 bundle_check(const struct ofpact_bundle *bundle, ofp_port_t max_ports,
107              const struct flow *flow)
108 {
109     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
110     size_t i;
111
112     if (bundle->dst.field) {
113         enum ofperr error = mf_check_dst(&bundle->dst, flow);
114         if (error) {
115             return error;
116         }
117     }
118
119     for (i = 0; i < bundle->n_slaves; i++) {
120         ofp_port_t ofp_port = bundle->slaves[i];
121
122         if (ofp_port != OFPP_NONE) {
123             enum ofperr error = ofpact_check_output_port(ofp_port, max_ports);
124             if (error) {
125                 VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
126                 return error;
127             }
128         }
129         /* Controller slaves are unsupported due to the lack of a max_len
130          * argument. This may or may not change in the future.  There doesn't
131          * seem to be a real-world use-case for supporting it. */
132         if (ofp_port == OFPP_CONTROLLER) {
133             VLOG_WARN_RL(&rl, "unsupported controller slave");
134             return OFPERR_OFPBAC_BAD_OUT_PORT;
135         }
136     }
137
138     return 0;
139 }
140
141
142 /* Helper for bundle_parse and bundle_parse_load.
143  *
144  * Returns NULL if successful, otherwise a malloc()'d string describing the
145  * error.  The caller is responsible for freeing the returned string.*/
146 static char * OVS_WARN_UNUSED_RESULT
147 bundle_parse__(const char *s, char **save_ptr,
148                const char *fields, const char *basis, const char *algorithm,
149                const char *slave_type, const char *dst,
150                const char *slave_delim, struct ofpbuf *ofpacts)
151 {
152     struct ofpact_bundle *bundle;
153
154     if (!slave_delim) {
155         return xasprintf("%s: not enough arguments to bundle action", s);
156     }
157
158     if (strcasecmp(slave_delim, "slaves")) {
159         return xasprintf("%s: missing slave delimiter, expected `slaves' "
160                          "got `%s'", s, slave_delim);
161     }
162
163     bundle = ofpact_put_BUNDLE(ofpacts);
164
165     for (;;) {
166         ofp_port_t slave_port;
167         char *slave;
168
169         slave = strtok_r(NULL, ", []", save_ptr);
170         if (!slave || bundle->n_slaves >= BUNDLE_MAX_SLAVES) {
171             break;
172         }
173
174         if (!ofputil_port_from_string(slave, &slave_port)) {
175             return xasprintf("%s: bad port number", slave);
176         }
177         ofpbuf_put(ofpacts, &slave_port, sizeof slave_port);
178
179         bundle = ofpacts->header;
180         bundle->n_slaves++;
181     }
182     ofpact_finish(ofpacts, &bundle->ofpact);
183
184     bundle = ofpacts->header;
185     bundle->basis = atoi(basis);
186
187     if (!strcasecmp(fields, "eth_src")) {
188         bundle->fields = NX_HASH_FIELDS_ETH_SRC;
189     } else if (!strcasecmp(fields, "symmetric_l4")) {
190         bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4;
191     } else if (!strcasecmp(fields, "symmetric_l3l4")) {
192         bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4;
193     } else if (!strcasecmp(fields, "symmetric_l3l4+udp")) {
194         bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP;
195     } else {
196         return xasprintf("%s: unknown fields `%s'", s, fields);
197     }
198
199     if (!strcasecmp(algorithm, "active_backup")) {
200         bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
201     } else if (!strcasecmp(algorithm, "hrw")) {
202         bundle->algorithm = NX_BD_ALG_HRW;
203     } else {
204         return xasprintf("%s: unknown algorithm `%s'", s, algorithm);
205     }
206
207     if (strcasecmp(slave_type, "ofport")) {
208         return xasprintf("%s: unknown slave_type `%s'", s, slave_type);
209     }
210
211     if (dst) {
212         char *error = mf_parse_subfield(&bundle->dst, dst);
213         if (error) {
214             return error;
215         }
216
217         if (!mf_nxm_header(bundle->dst.field->id)) {
218             return xasprintf("%s: experimenter OXM field '%s' not supported",
219                              s, dst);
220         }
221     }
222
223     return NULL;
224 }
225
226 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
227  * stores it in 'b'.  Sets 'b''s l2 pointer to NULL.
228  *
229  * Returns NULL if successful, otherwise a malloc()'d string describing the
230  * error.  The caller is responsible for freeing the returned string. */
231 char * OVS_WARN_UNUSED_RESULT
232 bundle_parse(const char *s, struct ofpbuf *ofpacts)
233 {
234     char *fields, *basis, *algorithm, *slave_type, *slave_delim;
235     char *tokstr, *save_ptr;
236     char *error;
237
238     save_ptr = NULL;
239     tokstr = xstrdup(s);
240     fields = strtok_r(tokstr, ", ", &save_ptr);
241     basis = strtok_r(NULL, ", ", &save_ptr);
242     algorithm = strtok_r(NULL, ", ", &save_ptr);
243     slave_type = strtok_r(NULL, ", ", &save_ptr);
244     slave_delim = strtok_r(NULL, ": ", &save_ptr);
245
246     error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
247                            NULL, slave_delim, ofpacts);
248     free(tokstr);
249
250     return error;
251 }
252
253 /* Converts a bundle_load action string contained in 's' to an nx_action_bundle
254  * and stores it in 'b'.  Sets 'b''s l2 pointer to NULL.
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 char * OVS_WARN_UNUSED_RESULT
259 bundle_parse_load(const char *s, struct ofpbuf *ofpacts)
260 {
261     char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
262     char *tokstr, *save_ptr;
263     char *error;
264
265     save_ptr = NULL;
266     tokstr = xstrdup(s);
267     fields = strtok_r(tokstr, ", ", &save_ptr);
268     basis = strtok_r(NULL, ", ", &save_ptr);
269     algorithm = strtok_r(NULL, ", ", &save_ptr);
270     slave_type = strtok_r(NULL, ", ", &save_ptr);
271     dst = strtok_r(NULL, ", ", &save_ptr);
272     slave_delim = strtok_r(NULL, ": ", &save_ptr);
273
274     error = bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type,
275                            dst, slave_delim, ofpacts);
276
277     free(tokstr);
278
279     return error;
280 }
281
282 /* Appends a human-readable representation of 'nab' to 's'. */
283 void
284 bundle_format(const struct ofpact_bundle *bundle, struct ds *s)
285 {
286     const char *action, *fields, *algorithm;
287     size_t i;
288
289     fields = flow_hash_fields_to_str(bundle->fields);
290
291     switch (bundle->algorithm) {
292     case NX_BD_ALG_HRW:
293         algorithm = "hrw";
294         break;
295     case NX_BD_ALG_ACTIVE_BACKUP:
296         algorithm = "active_backup";
297         break;
298     default:
299         algorithm = "<unknown>";
300     }
301
302     action = bundle->dst.field ? "bundle_load" : "bundle";
303
304     ds_put_format(s, "%s%s(%s%s,%"PRIu16",%s,%s,", colors.paren, action,
305                   colors.end, fields, bundle->basis, algorithm, "ofport");
306
307     if (bundle->dst.field) {
308         mf_format_subfield(&bundle->dst, s);
309         ds_put_char(s, ',');
310     }
311
312     ds_put_format(s, "%sslaves:%s", colors.param, colors.end);
313     for (i = 0; i < bundle->n_slaves; i++) {
314         if (i) {
315             ds_put_char(s, ',');
316         }
317
318         ofputil_format_port(bundle->slaves[i], s);
319     }
320
321     ds_put_format(s, "%s)%s", colors.paren, colors.end);
322 }