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