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