flow: Only un-wildcard relevant IP headers.
[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-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         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 %zu bytes "
169                      "allocated for slaves.  %zu 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->l2;
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 = ofputil_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 static void
256 bundle_parse__(const char *s, char **save_ptr,
257                const char *fields, const char *basis, const char *algorithm,
258                const char *slave_type, const char *dst,
259                const char *slave_delim, struct ofpbuf *ofpacts)
260 {
261     struct ofpact_bundle *bundle;
262
263     if (!slave_delim) {
264         ovs_fatal(0, "%s: not enough arguments to bundle action", s);
265     }
266
267     if (strcasecmp(slave_delim, "slaves")) {
268         ovs_fatal(0, "%s: missing slave delimiter, expected `slaves' got `%s'",
269                    s, slave_delim);
270     }
271
272     bundle = ofpact_put_BUNDLE(ofpacts);
273
274     for (;;) {
275         ofp_port_t slave_port;
276         char *slave;
277
278         slave = strtok_r(NULL, ", []", save_ptr);
279         if (!slave || bundle->n_slaves >= BUNDLE_MAX_SLAVES) {
280             break;
281         }
282
283         if (!ofputil_port_from_string(slave, &slave_port)) {
284             ovs_fatal(0, "%s: bad port number", slave);
285         }
286         ofpbuf_put(ofpacts, &slave_port, sizeof slave_port);
287
288         bundle = ofpacts->l2;
289         bundle->n_slaves++;
290     }
291     ofpact_update_len(ofpacts, &bundle->ofpact);
292
293     bundle->basis = atoi(basis);
294
295     if (!strcasecmp(fields, "eth_src")) {
296         bundle->fields = NX_HASH_FIELDS_ETH_SRC;
297     } else if (!strcasecmp(fields, "symmetric_l4")) {
298         bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4;
299     } else {
300         ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
301     }
302
303     if (!strcasecmp(algorithm, "active_backup")) {
304         bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
305     } else if (!strcasecmp(algorithm, "hrw")) {
306         bundle->algorithm = NX_BD_ALG_HRW;
307     } else {
308         ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
309     }
310
311     if (strcasecmp(slave_type, "ofport")) {
312         ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
313     }
314
315     if (dst) {
316         mf_parse_subfield(&bundle->dst, dst);
317     }
318 }
319
320 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
321  * stores it in 'b'.  Sets 'b''s l2 pointer to NULL. */
322 void
323 bundle_parse(const char *s, struct ofpbuf *ofpacts)
324 {
325     char *fields, *basis, *algorithm, *slave_type, *slave_delim;
326     char *tokstr, *save_ptr;
327
328     save_ptr = NULL;
329     tokstr = xstrdup(s);
330     fields = strtok_r(tokstr, ", ", &save_ptr);
331     basis = strtok_r(NULL, ", ", &save_ptr);
332     algorithm = strtok_r(NULL, ", ", &save_ptr);
333     slave_type = strtok_r(NULL, ", ", &save_ptr);
334     slave_delim = strtok_r(NULL, ": ", &save_ptr);
335
336     bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type, NULL,
337                    slave_delim, ofpacts);
338     free(tokstr);
339 }
340
341 /* Converts a bundle_load action string contained in 's' to an nx_action_bundle
342  * and stores it in 'b'.  Sets 'b''s l2 pointer to NULL. */
343 void
344 bundle_parse_load(const char *s, struct ofpbuf *ofpacts)
345 {
346     char *fields, *basis, *algorithm, *slave_type, *dst, *slave_delim;
347     char *tokstr, *save_ptr;
348
349     save_ptr = NULL;
350     tokstr = xstrdup(s);
351     fields = strtok_r(tokstr, ", ", &save_ptr);
352     basis = strtok_r(NULL, ", ", &save_ptr);
353     algorithm = strtok_r(NULL, ", ", &save_ptr);
354     slave_type = strtok_r(NULL, ", ", &save_ptr);
355     dst = strtok_r(NULL, ", ", &save_ptr);
356     slave_delim = strtok_r(NULL, ": ", &save_ptr);
357
358     bundle_parse__(s, &save_ptr, fields, basis, algorithm, slave_type, dst,
359                    slave_delim, ofpacts);
360
361     free(tokstr);
362 }
363
364 /* Appends a human-readable representation of 'nab' to 's'. */
365 void
366 bundle_format(const struct ofpact_bundle *bundle, struct ds *s)
367 {
368     const char *action, *fields, *algorithm;
369     size_t i;
370
371     fields = flow_hash_fields_to_str(bundle->fields);
372
373     switch (bundle->algorithm) {
374     case NX_BD_ALG_HRW:
375         algorithm = "hrw";
376         break;
377     case NX_BD_ALG_ACTIVE_BACKUP:
378         algorithm = "active_backup";
379         break;
380     default:
381         algorithm = "<unknown>";
382     }
383
384     action = bundle->dst.field ? "bundle_load" : "bundle";
385
386     ds_put_format(s, "%s(%s,%"PRIu16",%s,%s,", action, fields,
387                   bundle->basis, algorithm, "ofport");
388
389     if (bundle->dst.field) {
390         mf_format_subfield(&bundle->dst, s);
391         ds_put_cstr(s, ",");
392     }
393
394     ds_put_cstr(s, "slaves:");
395     for (i = 0; i < bundle->n_slaves; i++) {
396         if (i) {
397             ds_put_cstr(s, ",");
398         }
399
400         ofputil_format_port(bundle->slaves[i], s);
401     }
402
403     ds_put_cstr(s, ")");
404 }