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