vswitch: Implement bundle action.
[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 /* Executes 'nab' on 'flow'.  Uses 'slave_enabled' to determine if the slave
36  * designated by 'ofp_port' is up.  Returns the chosen slave, or OFPP_NONE if
37  * none of the slaves are acceptable. */
38 uint16_t
39 bundle_execute(const struct nx_action_bundle *nab, const struct flow *flow,
40                bool (*slave_enabled)(uint16_t ofp_port, void *aux), void *aux)
41 {
42     uint32_t flow_hash, best_hash;
43     int best, i;
44
45     assert(nab->algorithm == htons(NX_BD_ALG_HRW));
46
47     flow_hash = flow_hash_fields(flow, ntohs(nab->fields), ntohs(nab->basis));
48     best = -1;
49
50     for (i = 0; i < ntohs(nab->n_slaves); i++) {
51         if (slave_enabled(bundle_get_slave(nab, i), aux)) {
52             uint32_t hash = hash_2words(i, flow_hash);
53
54             if (best < 0 || hash > best_hash) {
55                 best_hash = hash;
56                 best = i;
57             }
58         }
59     }
60
61     return best >= 0 ? bundle_get_slave(nab, best) : OFPP_NONE;
62 }
63
64 /* Checks that 'nab' specifies a bundle action which is supported by this
65  * bundle module.  Uses the 'max_ports' parameter to validate each port using
66  * ofputil_check_output_port().  Returns 0 if 'nab' is supported, otherwise an
67  * OpenFlow error code (as returned by ofp_mkerr()). */
68 int
69 bundle_check(const struct nx_action_bundle *nab, int max_ports)
70 {
71     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
72     uint16_t n_slaves, fields, algorithm, slave_type, subtype;
73     size_t slaves_size, i;
74     int error;
75
76     subtype = ntohs(nab->subtype);
77     n_slaves = ntohs(nab->n_slaves);
78     fields = ntohs(nab->fields);
79     algorithm = ntohs(nab->algorithm);
80     slave_type = ntohs(nab->slave_type);
81     slaves_size = ntohs(nab->len) - sizeof *nab;
82
83     error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
84     if (!flow_hash_fields_valid(fields)) {
85         VLOG_WARN_RL(&rl, "unsupported fields %"PRIu16, fields);
86     } else if (n_slaves > BUNDLE_MAX_SLAVES) {
87         VLOG_WARN_RL(&rl, "too may slaves");
88     } else if (algorithm != NX_BD_ALG_HRW) {
89         VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16, algorithm);
90     } else if (slave_type != NXM_OF_IN_PORT) {
91         VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
92     } else {
93         error = 0;
94     }
95
96     for (i = 0; i < sizeof(nab->zero); i++) {
97         if (nab->zero[i]) {
98             VLOG_WARN_RL(&rl, "reserved field is nonzero");
99             error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
100         }
101     }
102
103     if (slaves_size < n_slaves * sizeof(ovs_be16)) {
104         VLOG_WARN_RL(&rl, "Nicira action %"PRIu16" only has %zu bytes "
105                      "allocated for slaves.  %zu bytes are required for "
106                      "%"PRIu16" slaves.", subtype, slaves_size,
107                      n_slaves * sizeof(ovs_be16), n_slaves);
108         error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
109     }
110
111     for (i = 0; i < n_slaves; i++) {
112         uint16_t ofp_port = bundle_get_slave(nab, i);
113         int ofputil_error = ofputil_check_output_port(ofp_port, max_ports);
114
115         if (ofputil_error) {
116             VLOG_WARN_RL(&rl, "invalid slave %"PRIu16, ofp_port);
117             error = ofputil_error;
118         }
119
120         /* Controller slaves are unsupported due to the lack of a max_len
121          * argument. This may or may not change in the future.  There doesn't
122          * seem to be a real-world use-case for supporting it. */
123         if (ofp_port == OFPP_CONTROLLER) {
124             VLOG_WARN_RL(&rl, "unsupported controller slave");
125             error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
126         }
127     }
128
129     return error;
130 }
131
132 /* Converts a bundle action string contained in 's' to an nx_action_bundle and
133  * stores it in 'b'.  Sets 'b''s l2 pointer to NULL. */
134 void
135 bundle_parse(struct ofpbuf *b, const char *s)
136 {
137     char *fields, *basis, *algorithm, *slave_type, *slave_delim;
138     struct nx_action_bundle *nab;
139     char *tokstr, *save_ptr;
140     uint16_t n_slaves;
141
142     save_ptr = NULL;
143     tokstr = xstrdup(s);
144     fields = strtok_r(tokstr, ", ", &save_ptr);
145     basis = strtok_r(NULL, ", ", &save_ptr);
146     algorithm = strtok_r(NULL, ", ", &save_ptr);
147     slave_type = strtok_r(NULL, ", ", &save_ptr);
148     slave_delim = strtok_r(NULL, ": ", &save_ptr);
149
150     if (!slave_delim) {
151         ovs_fatal(0, "%s: not enough arguments to bundle action", s);
152     }
153
154     if (strcasecmp(slave_delim, "slaves")) {
155         ovs_fatal(0, "%s: missing slave delimiter, expected `slaves' got `%s'",
156                    s, slave_delim);
157     }
158
159     b->l2 = ofpbuf_put_zeros(b, sizeof *nab);
160
161     n_slaves = 0;
162     for (;;) {
163         ovs_be16 slave_be;
164         char *slave;
165
166         slave = strtok_r(NULL, ", ", &save_ptr);
167         if (!slave || n_slaves >= BUNDLE_MAX_SLAVES) {
168             break;
169         }
170
171         slave_be = htons(atoi(slave));
172         ofpbuf_put(b, &slave_be, sizeof slave_be);
173
174         n_slaves++;
175     }
176
177     /* Slaves array must be multiple of 8 bytes long. */
178     if (b->size % 8) {
179         ofpbuf_put_zeros(b, 8 - (b->size % 8));
180     }
181
182     nab = b->l2;
183     nab->type = htons(OFPAT_VENDOR);
184     nab->len = htons(b->size - ((char *) b->l2 - (char *) b->data));
185     nab->vendor = htonl(NX_VENDOR_ID);
186     nab->subtype = htons(NXAST_BUNDLE);
187     nab->n_slaves = htons(n_slaves);
188     nab->basis = htons(atoi(basis));
189
190     if (!strcasecmp(fields, "eth_src")) {
191         nab->fields = htons(NX_HASH_FIELDS_ETH_SRC);
192     } else if (!strcasecmp(fields, "symmetric_l4")) {
193         nab->fields = htons(NX_HASH_FIELDS_SYMMETRIC_L4);
194     } else {
195         ovs_fatal(0, "%s: unknown fields `%s'", s, fields);
196     }
197
198     if (!strcasecmp(algorithm, "active_backup")) {
199         nab->algorithm = htons(NX_BD_ALG_ACTIVE_BACKUP);
200     } else if (!strcasecmp(algorithm, "hrw")) {
201         nab->algorithm = htons(NX_BD_ALG_HRW);
202     } else {
203         ovs_fatal(0, "%s: unknown algorithm `%s'", s, algorithm);
204     }
205
206     if (!strcasecmp(slave_type, "ofport")) {
207         nab->slave_type = htons(NXM_OF_IN_PORT);
208     } else {
209         ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
210     }
211
212     b->l2 = NULL;
213     free(tokstr);
214 }
215
216 /* Appends a human-readable representation of 'nab' to 's'. */
217 void
218 bundle_format(const struct nx_action_bundle *nab, struct ds *s)
219 {
220     const char *fields, *algorithm, *slave_type;
221     size_t i;
222
223     fields = flow_hash_fields_to_str(ntohs(nab->fields));
224
225     switch (ntohs(nab->algorithm)) {
226     case NX_BD_ALG_HRW:
227         algorithm = "hrw";
228         break;
229     case NX_BD_ALG_ACTIVE_BACKUP:
230         algorithm = "active_backup";
231         break;
232     default:
233         algorithm = "<unknown>";
234     }
235
236     switch (ntohs(nab->slave_type)) {
237     case NXM_OF_IN_PORT:
238         slave_type = "ofport";
239         break;
240     default:
241         slave_type = "<unknown>";
242     }
243
244     ds_put_format(s, "bundle(%s,%"PRIu16",%s,%s,slaves:", fields,
245                   ntohs(nab->basis), algorithm, slave_type);
246
247     for (i = 0; i < ntohs(nab->n_slaves); i++) {
248         if (i) {
249             ds_put_cstr(s, ",");
250         }
251
252         ds_put_format(s, "%"PRIu16, bundle_get_slave(nab, i));
253     }
254
255     ds_put_cstr(s, ")");
256 }