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