Coverage Report

Created: 2026-09-01 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/bundle.c
Line
Count
Source
1
/* Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
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 <sys/types.h>
21
#include <netinet/in.h>
22
#include <arpa/inet.h>
23
#include <inttypes.h>
24
25
#include "colors.h"
26
#include "multipath.h"
27
#include "nx-match.h"
28
#include "openflow/nicira-ext.h"
29
#include "openvswitch/dynamic-string.h"
30
#include "openvswitch/meta-flow.h"
31
#include "openvswitch/ofp-actions.h"
32
#include "openvswitch/ofp-errors.h"
33
#include "openvswitch/ofp-port.h"
34
#include "openvswitch/ofpbuf.h"
35
#include "openvswitch/vlog.h"
36
#include "util.h"
37
38
VLOG_DEFINE_THIS_MODULE(bundle);
39
40
static ofp_port_t
41
execute_ab(const struct ofpact_bundle *bundle,
42
           bool (*member_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
43
0
{
44
0
    size_t i;
45
46
0
    for (i = 0; i < bundle->n_members; i++) {
47
0
        ofp_port_t member = bundle->members[i];
48
0
        if (member_enabled(member, aux)) {
49
0
            return member;
50
0
        }
51
0
    }
52
53
0
    return OFPP_NONE;
54
0
}
55
56
static ofp_port_t
57
execute_hrw(const struct ofpact_bundle *bundle,
58
            const struct flow *flow, struct flow_wildcards *wc,
59
            bool (*member_enabled)(ofp_port_t ofp_port, void *aux), void *aux)
60
0
{
61
0
    uint32_t flow_hash, best_hash;
62
0
    int best, i;
63
64
0
    if (bundle->n_members > 1) {
65
0
        flow_mask_hash_fields(flow, wc, bundle->fields);
66
0
    }
67
68
0
    flow_hash = flow_hash_fields(flow, bundle->fields, bundle->basis);
69
0
    best = -1;
70
0
    best_hash = 0;
71
72
0
    for (i = 0; i < bundle->n_members; i++) {
73
0
        if (member_enabled(bundle->members[i], aux)) {
74
0
            uint32_t hash = hash_2words(i, flow_hash);
75
76
0
            if (best < 0 || hash > best_hash) {
77
0
                best_hash = hash;
78
0
                best = i;
79
0
            }
80
0
        }
81
0
    }
82
83
0
    return best >= 0 ? bundle->members[best] : OFPP_NONE;
84
0
}
85
86
/* Executes 'bundle' on 'flow'.  Sets fields in 'wc' that were used to
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. */
90
ofp_port_t
91
bundle_execute(const struct ofpact_bundle *bundle,
92
               const struct flow *flow, struct flow_wildcards *wc,
93
               bool (*member_enabled)(ofp_port_t ofp_port, void *aux),
94
               void *aux)
95
0
{
96
0
    switch (bundle->algorithm) {
97
0
    case NX_BD_ALG_HRW:
98
0
        return execute_hrw(bundle, flow, wc, member_enabled, aux);
99
100
0
    case NX_BD_ALG_ACTIVE_BACKUP:
101
0
        return execute_ab(bundle, member_enabled, aux);
102
103
0
    default:
104
0
        OVS_NOT_REACHED();
105
0
    }
106
0
}
107
108
enum ofperr
109
bundle_check(const struct ofpact_bundle *bundle, ofp_port_t max_ports,
110
             const struct match *match)
111
1.41k
{
112
1.41k
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
113
1.41k
    size_t i;
114
115
1.41k
    if (bundle->dst.field) {
116
199
        enum ofperr error = mf_check_dst(&bundle->dst, match);
117
199
        if (error) {
118
1
            return error;
119
1
        }
120
199
    }
121
122
44.1k
    for (i = 0; i < bundle->n_members; i++) {
123
42.7k
        ofp_port_t ofp_port = bundle->members[i];
124
125
42.7k
        if (ofp_port != OFPP_NONE) {
126
42.5k
            enum ofperr error = ofpact_check_output_port(ofp_port, max_ports);
127
42.5k
            if (error) {
128
3
                VLOG_WARN_RL(&rl, "invalid member %"PRIu32, ofp_port);
129
3
                return error;
130
3
            }
131
42.5k
        }
132
        /* Controller members are unsupported due to the lack of a max_len
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
42.7k
        if (ofp_port == OFPP_CONTROLLER) {
136
1
            VLOG_WARN_RL(&rl, "unsupported controller member");
137
1
            return OFPERR_OFPBAC_BAD_OUT_PORT;
138
1
        }
139
42.7k
    }
140
141
1.40k
    return 0;
142
1.41k
}
143
144
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.*/
149
static char * OVS_WARN_UNUSED_RESULT
150
bundle_parse__(const char *s, const struct ofputil_port_map *port_map,
151
               char **save_ptr,
152
               const char *fields, const char *basis, const char *algorithm,
153
               const char *member_type, const char *dst,
154
               const char *member_delim, struct ofpbuf *ofpacts)
155
3.17k
{
156
3.17k
    struct ofpact_bundle *bundle;
157
158
3.17k
    if (!member_delim) {
159
11
        return xasprintf("%s: not enough arguments to bundle action", s);
160
11
    }
161
162
3.16k
    if (strcasecmp(member_delim, "members")
163
2.95k
        && strcasecmp(member_delim, "slaves")) {
164
115
        return xasprintf("%s: missing member delimiter, expected `members', "
165
115
                         "got `%s'", s, member_delim);
166
115
    }
167
168
3.05k
    bundle = ofpact_put_BUNDLE(ofpacts);
169
170
68.2k
    for (;;) {
171
68.2k
        ofp_port_t member_port;
172
68.2k
        char *member;
173
174
68.2k
        member = strtok_r(NULL, ", []", save_ptr);
175
68.2k
        if (!member || bundle->n_members >= BUNDLE_MAX_MEMBERS) {
176
3.04k
            break;
177
3.04k
        }
178
179
65.1k
        if (!ofputil_port_from_string(member, port_map, &member_port)) {
180
2
            return xasprintf("%s: bad port number", member);
181
2
        }
182
65.1k
        ofpbuf_put(ofpacts, &member_port, sizeof member_port);
183
184
65.1k
        bundle = ofpacts->header;
185
65.1k
        bundle->n_members++;
186
65.1k
    }
187
188
3.04k
    if (ofpbuf_oversized(ofpacts)) {
189
0
        return xasprintf("input too big");
190
0
    }
191
192
3.04k
    ofpact_finish_BUNDLE(ofpacts, &bundle);
193
3.04k
    bundle->basis = atoi(basis);
194
195
3.04k
    if (!strcasecmp(fields, "eth_src")) {
196
199
        bundle->fields = NX_HASH_FIELDS_ETH_SRC;
197
2.84k
    } else if (!strcasecmp(fields, "symmetric_l4")) {
198
509
        bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L4;
199
2.34k
    } else if (!strcasecmp(fields, "symmetric_l3l4")) {
200
345
        bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4;
201
1.99k
    } else if (!strcasecmp(fields, "symmetric_l3l4+udp")) {
202
385
        bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP;
203
1.61k
    } else if (!strcasecmp(fields, "nw_src")) {
204
540
        bundle->fields = NX_HASH_FIELDS_NW_SRC;
205
1.07k
    } else if (!strcasecmp(fields, "nw_dst")) {
206
390
        bundle->fields = NX_HASH_FIELDS_NW_DST;
207
680
    } else if (!strcasecmp(fields, "symmetric_l3")) {
208
418
        bundle->fields = NX_HASH_FIELDS_SYMMETRIC_L3;
209
418
    } else {
210
262
        return xasprintf("%s: unknown fields `%s'", s, fields);
211
262
    }
212
213
2.78k
    if (!strcasecmp(algorithm, "active_backup")) {
214
440
        bundle->algorithm = NX_BD_ALG_ACTIVE_BACKUP;
215
2.34k
    } else if (!strcasecmp(algorithm, "hrw")) {
216
2.25k
        bundle->algorithm = NX_BD_ALG_HRW;
217
2.25k
    } else {
218
89
        return xasprintf("%s: unknown algorithm `%s'", s, algorithm);
219
89
    }
220
221
2.69k
    if (strcasecmp(member_type, "ofport")) {
222
49
        return xasprintf("%s: unknown member_type `%s'", s, member_type);
223
49
    }
224
225
2.64k
    if (dst) {
226
215
        char *error = mf_parse_subfield(&bundle->dst, dst);
227
215
        if (error) {
228
1
            return error;
229
1
        }
230
231
214
        if (!mf_nxm_header(bundle->dst.field->id)) {
232
1
            return xasprintf("%s: experimenter OXM field '%s' not supported",
233
1
                             s, dst);
234
1
        }
235
214
    }
236
237
2.64k
    return NULL;
238
2.64k
}
239
240
/* Converts a bundle action string contained in 's' to an nx_action_bundle and
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. */
245
char * OVS_WARN_UNUSED_RESULT
246
bundle_parse(const char *s, const struct ofputil_port_map *port_map,
247
             struct ofpbuf *ofpacts)
248
2.95k
{
249
2.95k
    char *fields, *basis, *algorithm, *member_type, *member_delim;
250
2.95k
    char *tokstr, *save_ptr;
251
2.95k
    char *error;
252
253
2.95k
    save_ptr = NULL;
254
2.95k
    tokstr = xstrdup(s);
255
2.95k
    fields = strtok_r(tokstr, ", ", &save_ptr);
256
2.95k
    basis = strtok_r(NULL, ", ", &save_ptr);
257
2.95k
    algorithm = strtok_r(NULL, ", ", &save_ptr);
258
2.95k
    member_type = strtok_r(NULL, ", ", &save_ptr);
259
2.95k
    member_delim = strtok_r(NULL, ": ", &save_ptr);
260
261
2.95k
    error = bundle_parse__(s, port_map,
262
2.95k
                           &save_ptr, fields, basis, algorithm, member_type,
263
2.95k
                           NULL, member_delim, ofpacts);
264
2.95k
    free(tokstr);
265
266
2.95k
    return error;
267
2.95k
}
268
269
/* Converts a bundle_load action string contained in 's' to an nx_action_bundle
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.*/
274
char * OVS_WARN_UNUSED_RESULT
275
bundle_parse_load(const char *s, const struct ofputil_port_map *port_map,
276
                  struct ofpbuf *ofpacts)
277
220
{
278
220
    char *fields, *basis, *algorithm, *member_type, *dst, *member_delim;
279
220
    char *tokstr, *save_ptr;
280
220
    char *error;
281
282
220
    save_ptr = NULL;
283
220
    tokstr = xstrdup(s);
284
220
    fields = strtok_r(tokstr, ", ", &save_ptr);
285
220
    basis = strtok_r(NULL, ", ", &save_ptr);
286
220
    algorithm = strtok_r(NULL, ", ", &save_ptr);
287
220
    member_type = strtok_r(NULL, ", ", &save_ptr);
288
220
    dst = strtok_r(NULL, ", ", &save_ptr);
289
220
    member_delim = strtok_r(NULL, ": ", &save_ptr);
290
291
220
    error = bundle_parse__(s, port_map,
292
220
                           &save_ptr, fields, basis, algorithm, member_type,
293
220
                           dst, member_delim, ofpacts);
294
295
220
    free(tokstr);
296
297
220
    return error;
298
220
}
299
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. */
302
void
303
bundle_format(const struct ofpact_bundle *bundle,
304
              const struct ofputil_port_map *port_map, struct ds *s)
305
0
{
306
0
    const char *action, *fields, *algorithm;
307
0
    size_t i;
308
309
0
    fields = flow_hash_fields_to_str(bundle->fields);
310
311
0
    switch (bundle->algorithm) {
312
0
    case NX_BD_ALG_HRW:
313
0
        algorithm = "hrw";
314
0
        break;
315
0
    case NX_BD_ALG_ACTIVE_BACKUP:
316
0
        algorithm = "active_backup";
317
0
        break;
318
0
    default:
319
0
        algorithm = "<unknown>";
320
0
    }
321
322
0
    action = bundle->dst.field ? "bundle_load" : "bundle";
323
324
0
    ds_put_format(s, "%s%s(%s%s,%"PRIu16",%s,%s,", colors.paren, action,
325
0
                  colors.end, fields, bundle->basis, algorithm, "ofport");
326
327
0
    if (bundle->dst.field) {
328
0
        mf_format_subfield(&bundle->dst, s);
329
0
        ds_put_char(s, ',');
330
0
    }
331
332
0
    ds_put_format(s, "%smembers:%s", colors.param, colors.end);
333
0
    for (i = 0; i < bundle->n_members; i++) {
334
0
        if (i) {
335
0
            ds_put_char(s, ',');
336
0
        }
337
338
0
        ofputil_format_port(bundle->members[i], port_map, s);
339
0
    }
340
341
0
    ds_put_format(s, "%s)%s", colors.paren, colors.end);
342
0
}