]> git.proxmox.com Git - mirror_ovs.git/blame - lib/bundle.c
netdev-offload-tc: Use single 'once' variable for probing tc features
[mirror_ovs.git] / lib / bundle.c
CommitLineData
50f96b10 1/* Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
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
b2befd5b
BP
20#include <sys/types.h>
21#include <netinet/in.h>
daff3353
EJ
22#include <arpa/inet.h>
23#include <inttypes.h>
24
b1c5bf1f 25#include "colors.h"
daff3353
EJ
26#include "multipath.h"
27#include "nx-match.h"
daff3353 28#include "openflow/nicira-ext.h"
b598f214
BW
29#include "openvswitch/dynamic-string.h"
30#include "openvswitch/meta-flow.h"
31#include "openvswitch/ofp-actions.h"
e03c096d 32#include "openvswitch/ofp-errors.h"
0d71302e 33#include "openvswitch/ofp-port.h"
b598f214 34#include "openvswitch/ofpbuf.h"
e6211adc 35#include "openvswitch/vlog.h"
ee89ea7b 36#include "util.h"
daff3353 37
daff3353
EJ
38VLOG_DEFINE_THIS_MODULE(bundle);
39
4e022ec0 40static ofp_port_t
f25d0cf3 41execute_ab(const struct ofpact_bundle *bundle,
91fc374a 42 bool (*member_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
04a85497
EJ
43{
44 size_t i;
45
91fc374a
BP
46 for (i = 0; i < bundle->n_members; i++) {
47 ofp_port_t member = bundle->members[i];
48 if (member_enabled(member, aux)) {
49 return member;
04a85497
EJ
50 }
51 }
52
53 return OFPP_NONE;
54}
55
4e022ec0 56static ofp_port_t
bcd2633a
JP
57execute_hrw(const struct ofpact_bundle *bundle,
58 const struct flow *flow, struct flow_wildcards *wc,
91fc374a 59 bool (*member_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
daff3353
EJ
60{
61 uint32_t flow_hash, best_hash;
62 int best, i;
63
91fc374a 64 if (bundle->n_members > 1) {
6cdd5145 65 flow_mask_hash_fields(flow, wc, bundle->fields);
bcd2633a
JP
66 }
67
f25d0cf3 68 flow_hash = flow_hash_fields(flow, bundle->fields, bundle->basis);
daff3353 69 best = -1;
481db488 70 best_hash = 0;
daff3353 71
91fc374a
BP
72 for (i = 0; i < bundle->n_members; i++) {
73 if (member_enabled(bundle->members[i], aux)) {
daff3353
EJ
74 uint32_t hash = hash_2words(i, flow_hash);
75
76 if (best < 0 || hash > best_hash) {
77 best_hash = hash;
78 best = i;
79 }
80 }
81 }
82
91fc374a 83 return best >= 0 ? bundle->members[best] : OFPP_NONE;
daff3353
EJ
84}
85
bcd2633a 86/* Executes 'bundle' on 'flow'. Sets fields in 'wc' that were used to
91fc374a
BP
87 * calculate the result. Uses 'member_enabled' to determine if the member
88 * designated by 'ofp_port' is up. Returns the chosen member, or
89 * OFPP_NONE if none of the members are acceptable. */
4e022ec0 90ofp_port_t
bcd2633a
JP
91bundle_execute(const struct ofpact_bundle *bundle,
92 const struct flow *flow, struct flow_wildcards *wc,
91fc374a 93 bool (*member_enabled)(ofp_port_t ofp_port, void *aux),
4e022ec0 94 void *aux)
04a85497 95{
f25d0cf3
BP
96 switch (bundle->algorithm) {
97 case NX_BD_ALG_HRW:
91fc374a 98 return execute_hrw(bundle, flow, wc, member_enabled, aux);
04a85497 99
f25d0cf3 100 case NX_BD_ALG_ACTIVE_BACKUP:
91fc374a 101 return execute_ab(bundle, member_enabled, aux);
816fd533 102
f25d0cf3 103 default:
428b2edd 104 OVS_NOT_REACHED();
f25d0cf3 105 }
a368bb53
EJ
106}
107
f25d0cf3 108enum ofperr
4e022ec0 109bundle_check(const struct ofpact_bundle *bundle, ofp_port_t max_ports,
67210a55 110 const struct match *match)
f25d0cf3
BP
111{
112 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
113 size_t i;
114
115 if (bundle->dst.field) {
67210a55 116 enum ofperr error = mf_check_dst(&bundle->dst, match);
f25d0cf3
BP
117 if (error) {
118 return error;
119 }
120 }
121
91fc374a
BP
122 for (i = 0; i < bundle->n_members; i++) {
123 ofp_port_t ofp_port = bundle->members[i];
daff3353 124
13d2c689
BP
125 if (ofp_port != OFPP_NONE) {
126 enum ofperr error = ofpact_check_output_port(ofp_port, max_ports);
127 if (error) {
91fc374a 128 VLOG_WARN_RL(&rl, "invalid member %"PRIu32, ofp_port);
13d2c689
BP
129 return error;
130 }
daff3353 131 }
91fc374a 132 /* Controller members are unsupported due to the lack of a max_len
daff3353
EJ
133 * argument. This may or may not change in the future. There doesn't
134 * seem to be a real-world use-case for supporting it. */
135 if (ofp_port == OFPP_CONTROLLER) {
91fc374a 136 VLOG_WARN_RL(&rl, "unsupported controller member");
f25d0cf3 137 return OFPERR_OFPBAC_BAD_OUT_PORT;
daff3353
EJ
138 }
139 }
140
f25d0cf3
BP
141 return 0;
142}
143
daff3353 144
bdda5aca
BP
145/* Helper for bundle_parse and bundle_parse_load.
146 *
147 * Returns NULL if successful, otherwise a malloc()'d string describing the
148 * error. The caller is responsible for freeing the returned string.*/
cab50449 149static char * OVS_WARN_UNUSED_RESULT
50f96b10
BP
150bundle_parse__(const char *s, const struct ofputil_port_map *port_map,
151 char **save_ptr,
a368bb53 152 const char *fields, const char *basis, const char *algorithm,
91fc374a
BP
153 const char *member_type, const char *dst,
154 const char *member_delim, struct ofpbuf *ofpacts)
daff3353 155{
f25d0cf3 156 struct ofpact_bundle *bundle;
daff3353 157
91fc374a 158 if (!member_delim) {
bdda5aca 159 return xasprintf("%s: not enough arguments to bundle action", s);
daff3353
EJ
160 }
161
91fc374a
BP
162 if (strcasecmp(member_delim, "members")
163 && strcasecmp(member_delim, "slaves")) {
164 return xasprintf("%s: missing member delimiter, expected `members', "
165 "got `%s'", s, member_delim);
daff3353
EJ
166 }
167
f25d0cf3 168 bundle = ofpact_put_BUNDLE(ofpacts);
daff3353 169
daff3353 170 for (;;) {
91fc374a
BP
171 ofp_port_t member_port;
172 char *member;
daff3353 173
91fc374a
BP
174 member = strtok_r(NULL, ", []", save_ptr);
175 if (!member || bundle->n_members >= BUNDLE_MAX_MEMBERS) {
daff3353
EJ
176 break;
177 }
178
91fc374a
BP
179 if (!ofputil_port_from_string(member, port_map, &member_port)) {
180 return xasprintf("%s: bad port number", member);
c6100d92 181 }
91fc374a 182 ofpbuf_put(ofpacts, &member_port, sizeof member_port);
daff3353 183
6fd6ed71 184 bundle = ofpacts->header;
91fc374a 185 bundle->n_members++;
daff3353 186 }
8e738337
TA
187
188 if (ofpbuf_oversized(ofpacts)) {
189 return xasprintf("input too big");
190 }
191
ce058104 192 ofpact_finish_BUNDLE(ofpacts, &bundle);
f25d0cf3 193 bundle->basis = atoi(basis);
daff3353
EJ
194
195 if (!strcasecmp(fields, "eth_src")) {
f25d0cf3 196 bundle->fields = NX_HASH_FIELDS_ETH_SRC;
daff3353 197 } else if (!strcasecmp(fields, "symmetric_l4")) {
f25d0cf3 198 bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4;
4249b547
JB
199 } else if (!strcasecmp(fields, "symmetric_l3l4")) {
200 bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4;
201 } else if (!strcasecmp(fields, "symmetric_l3l4+udp")) {
202 bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP;
417cfdb6 203 } else if (!strcasecmp(fields, "nw_src")) {
204 bundle->fields = NX_HASH_FIELDS_NW_SRC;
205 } else if (!strcasecmp(fields, "nw_dst")) {
206 bundle->fields = NX_HASH_FIELDS_NW_DST;
84ddf96c
MX
207 } else if (!strcasecmp(fields, "symmetric_l3")) {
208 bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3;
daff3353 209 } else {
bdda5aca 210 return xasprintf("%s: unknown fields `%s'", s, fields);
daff3353
EJ
211 }
212
213 if (!strcasecmp(algorithm, "active_backup")) {
f25d0cf3 214 bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
daff3353 215 } else if (!strcasecmp(algorithm, "hrw")) {
f25d0cf3 216 bundle->algorithm = NX_BD_ALG_HRW;
daff3353 217 } else {
bdda5aca 218 return xasprintf("%s: unknown algorithm `%s'", s, algorithm);
daff3353
EJ
219 }
220
91fc374a
BP
221 if (strcasecmp(member_type, "ofport")) {
222 return xasprintf("%s: unknown member_type `%s'", s, member_type);
daff3353
EJ
223 }
224
f25d0cf3 225 if (dst) {
bdda5aca
BP
226 char *error = mf_parse_subfield(&bundle->dst, dst);
227 if (error) {
228 return error;
229 }
bad8a439
BP
230
231 if (!mf_nxm_header(bundle->dst.field->id)) {
232 return xasprintf("%s: experimenter OXM field '%s' not supported",
233 s, dst);
234 }
a368bb53 235 }
bdda5aca
BP
236
237 return NULL;
a368bb53
EJ
238}
239
240/* Converts a bundle action string contained in 's' to an nx_action_bundle and
bdda5aca
BP
241 * stores it in 'b'. Sets 'b''s l2 pointer to NULL.
242 *
243 * Returns NULL if successful, otherwise a malloc()'d string describing the
244 * error. The caller is responsible for freeing the returned string. */
cab50449 245char * OVS_WARN_UNUSED_RESULT
50f96b10
BP
246bundle_parse(const char *s, const struct ofputil_port_map *port_map,
247 struct ofpbuf *ofpacts)
a368bb53 248{
91fc374a 249 char *fields, *basis, *algorithm, *member_type, *member_delim;
a368bb53 250 char *tokstr, *save_ptr;
bdda5aca 251 char *error;
a368bb53
EJ
252
253 save_ptr = NULL;
254 tokstr = xstrdup(s);
255 fields = strtok_r(tokstr, ", ", &save_ptr);
256 basis = strtok_r(NULL, ", ", &save_ptr);
257 algorithm = strtok_r(NULL, ", ", &save_ptr);
91fc374a
BP
258 member_type = strtok_r(NULL, ", ", &save_ptr);
259 member_delim = strtok_r(NULL, ": ", &save_ptr);
a368bb53 260
50f96b10 261 error = bundle_parse__(s, port_map,
91fc374a
BP
262 &save_ptr, fields, basis, algorithm, member_type,
263 NULL, member_delim, ofpacts);
a368bb53 264 free(tokstr);
bdda5aca
BP
265
266 return error;
a368bb53
EJ
267}
268
269/* Converts a bundle_load action string contained in 's' to an nx_action_bundle
bdda5aca
BP
270 * and stores it in 'b'. Sets 'b''s l2 pointer to NULL.
271 *
272 * Returns NULL if successful, otherwise a malloc()'d string describing the
273 * error. The caller is responsible for freeing the returned string.*/
cab50449 274char * OVS_WARN_UNUSED_RESULT
50f96b10
BP
275bundle_parse_load(const char *s, const struct ofputil_port_map *port_map,
276 struct ofpbuf *ofpacts)
a368bb53 277{
91fc374a 278 char *fields, *basis, *algorithm, *member_type, *dst, *member_delim;
a368bb53 279 char *tokstr, *save_ptr;
bdda5aca 280 char *error;
a368bb53
EJ
281
282 save_ptr = NULL;
283 tokstr = xstrdup(s);
284 fields = strtok_r(tokstr, ", ", &save_ptr);
285 basis = strtok_r(NULL, ", ", &save_ptr);
286 algorithm = strtok_r(NULL, ", ", &save_ptr);
91fc374a 287 member_type = strtok_r(NULL, ", ", &save_ptr);
a368bb53 288 dst = strtok_r(NULL, ", ", &save_ptr);
91fc374a 289 member_delim = strtok_r(NULL, ": ", &save_ptr);
a368bb53 290
50f96b10 291 error = bundle_parse__(s, port_map,
91fc374a
BP
292 &save_ptr, fields, basis, algorithm, member_type,
293 dst, member_delim, ofpacts);
a368bb53 294
daff3353 295 free(tokstr);
bdda5aca
BP
296
297 return error;
daff3353
EJ
298}
299
50f96b10
BP
300/* Appends a human-readable representation of 'nab' to 's'. If 'port_map' is
301 * nonnull, uses it to translate port numbers to names in output. */
daff3353 302void
50f96b10
BP
303bundle_format(const struct ofpact_bundle *bundle,
304 const struct ofputil_port_map *port_map, struct ds *s)
daff3353 305{
f25d0cf3 306 const char *action, *fields, *algorithm;
daff3353
EJ
307 size_t i;
308
f25d0cf3 309 fields = flow_hash_fields_to_str(bundle->fields);
daff3353 310
f25d0cf3 311 switch (bundle->algorithm) {
daff3353
EJ
312 case NX_BD_ALG_HRW:
313 algorithm = "hrw";
314 break;
315 case NX_BD_ALG_ACTIVE_BACKUP:
316 algorithm = "active_backup";
317 break;
318 default:
319 algorithm = "<unknown>";
320 }
321
f25d0cf3 322 action = bundle->dst.field ? "bundle_load" : "bundle";
a368bb53 323
b1c5bf1f
QM
324 ds_put_format(s, "%s%s(%s%s,%"PRIu16",%s,%s,", colors.paren, action,
325 colors.end, fields, bundle->basis, algorithm, "ofport");
816fd533 326
f25d0cf3
BP
327 if (bundle->dst.field) {
328 mf_format_subfield(&bundle->dst, s);
b1c5bf1f 329 ds_put_char(s, ',');
a368bb53
EJ
330 }
331
91fc374a
BP
332 ds_put_format(s, "%smembers:%s", colors.param, colors.end);
333 for (i = 0; i < bundle->n_members; i++) {
daff3353 334 if (i) {
b1c5bf1f 335 ds_put_char(s, ',');
daff3353
EJ
336 }
337
91fc374a 338 ofputil_format_port(bundle->members[i], port_map, s);
daff3353
EJ
339 }
340
b1c5bf1f 341 ds_put_format(s, "%s)%s", colors.paren, colors.end);
daff3353 342}