File: lb.c

package info (click to toggle)
ovn 25.09.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,492 kB
  • sloc: ansic: 106,060; xml: 23,314; sh: 3,322; python: 1,838; makefile: 836
file content (427 lines) | stat: -rw-r--r-- 12,989 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* Copyright (c) 2020, Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include "lb.h"
#include "lib/ovn-util.h"
#include "ovn/lex.h"

/* OpenvSwitch lib includes. */
#include "openvswitch/vlog.h"

VLOG_DEFINE_THIS_MODULE(lb);

static bool
ovn_lb_backend_init_explicit(const struct ovn_lb_vip *lb_vip,
                             struct ovn_lb_backend *backend,
                             const char *token, struct ds *errors)
{
    int backend_addr_family;
    if (!ip_address_and_port_from_lb_key(token, &backend->ip_str,
                                         &backend->ip, &backend->port,
                                         &backend_addr_family)) {
        if (lb_vip->port_str) {
            ds_put_format(errors, "%s: should be an IP address and a "
                                   "port number with : as a separator, ",
                          token);
        } else {
            ds_put_format(errors, "%s: should be an IP address, ", token);
        }
        return false;
    }

    if (lb_vip->address_family != backend_addr_family) {
        free(backend->ip_str);
        ds_put_format(errors, "%s: IP address family is different from "
                               "VIP %s, ",
                      token, lb_vip->vip_str);
        return false;
    }

    if (lb_vip->port_str) {
        if (!backend->port) {
            free(backend->ip_str);
            ds_put_format(errors, "%s: should be an IP address and "
                                   "a port number with : as a separator, ",
                          token);
            return false;
        }
    } else {
        if (backend->port) {
            free(backend->ip_str);
            ds_put_format(errors, "%s: should be an IP address, ", token);
            return false;
        }
    }

    backend->port_str =
        backend->port ? xasprintf("%"PRIu16, backend->port) : NULL;
    return true;
}

/* Format for backend ips: "IP1:port1,IP2:port2,...". */
static char *
ovn_lb_backends_init_explicit(struct ovn_lb_vip *lb_vip, const char *value)
{
    struct ds errors = DS_EMPTY_INITIALIZER;
    char *tokstr = xstrdup(value);
    char *save_ptr = NULL;
    lb_vip->backends = VECTOR_EMPTY_INITIALIZER(struct ovn_lb_backend);

    for (char *token = strtok_r(tokstr, ",", &save_ptr);
        token != NULL;
        token = strtok_r(NULL, ",", &save_ptr)) {

        struct ovn_lb_backend backend = {0};
        if (!ovn_lb_backend_init_explicit(lb_vip, &backend, token, &errors)) {
            continue;
        }

        vector_push(&lb_vip->backends, &backend);
    }
    free(tokstr);

    if (ds_last(&errors) != EOF) {
        ds_chomp(&errors, ' ');
        ds_chomp(&errors, ',');
        ds_put_char(&errors, '.');
        return ds_steal_cstr(&errors);
    }
    return NULL;
}

char *
ovn_lb_vip_init_explicit(struct ovn_lb_vip *lb_vip, const char *lb_key,
                         const char *lb_value)
{
    if (!ip_address_and_port_from_lb_key(lb_key, &lb_vip->vip_str,
                                         &lb_vip->vip, &lb_vip->vip_port,
                                         &lb_vip->address_family)) {
        return xasprintf("%s: should be an IP address (or an IP address "
                         "and a port number with : as a separator).", lb_key);
    }

    lb_vip->port_str = lb_vip->vip_port
                       ? xasprintf("%"PRIu16, lb_vip->vip_port)
                       : NULL;

    return ovn_lb_backends_init_explicit(lb_vip, lb_value);
}

static bool
ovn_lb_backend_init_template(struct ovn_lb_backend *backend, const char *token,
                             struct ds *errors)
{
    char *atom = xstrdup(token);
    char *save_ptr = NULL;
    bool success = false;
    char *backend_ip = NULL;
    char *backend_port = NULL;

    for (char *subatom = strtok_r(atom, ":", &save_ptr); subatom;
         subatom = strtok_r(NULL, ":", &save_ptr)) {
        if (backend_ip && backend_port) {
            success = false;
            break;
        }
        success = true;
        if (!backend_ip) {
            backend_ip = xstrdup(subatom);
        } else {
            backend_port = xstrdup(subatom);
        }
    }

    if (success) {
        backend->ip_str = backend_ip;
        backend->port_str = backend_port;
        backend->port = 0;
        memset(&backend->ip, 0, sizeof backend->ip);
    } else {
        ds_put_format(errors, "%s: should be a template of the form: "
                      "'^backendip_variable1[:^port_variable1|:port]', ",
                      atom);
        free(backend_port);
        free(backend_ip);
    }
    free(atom);

    return success;
}

/* Parses backends of a templated LB VIP.
 * For now only the following template forms are supported:
 * A.
 *   ^backendip_variable1[:^port_variable1|:port],
 *   ^backendip_variable2[:^port_variable2|:port]
 *
 * B.
 *   ^backends_variable1,^backends_variable2 is also a thing
 *      where 'backends_variable1' may expand to IP1_1:PORT1_1 on chassis-1
 *                                               IP1_2:PORT1_2 on chassis-2
 *        and 'backends_variable2' may expand to IP2_1:PORT2_1 on chassis-1
 *                                               IP2_2:PORT2_2 on chassis-2
 * C.
 *    backendip1[:port],backendip2[:port]
 */
static char *
ovn_lb_backends_init_template(struct ovn_lb_vip *lb_vip, const char *value_)
{
    struct ds errors = DS_EMPTY_INITIALIZER;
    char *value = xstrdup(value_);
    char *save_ptr = NULL;
    lb_vip->backends = VECTOR_EMPTY_INITIALIZER(struct ovn_lb_backend);

    for (char *token = strtok_r(value, ",", &save_ptr); token;
         token = strtok_r(NULL, ",", &save_ptr)) {


        struct ovn_lb_backend backend = {0};
        if (token[0] && token[0] == '^') {
            if (!ovn_lb_backend_init_template(&backend, token, &errors)) {
                continue;
            }
        } else {
            if (!ovn_lb_backend_init_explicit(lb_vip, &backend,
                                              token, &errors)) {
                continue;
            }
        }

        vector_push(&lb_vip->backends, &backend);
    }

    free(value);
    if (ds_last(&errors) != EOF) {
        ds_chomp(&errors, ' ');
        ds_chomp(&errors, ',');
        ds_put_char(&errors, '.');
        return ds_steal_cstr(&errors);
    }
    return NULL;
}

/* Parses a VIP of a templated LB.
 * For now only the following template forms are supported:
 *   ^vip_variable[:^port_variable|:port]
 */
static char *
ovn_lb_vip_init_template(struct ovn_lb_vip *lb_vip, const char *lb_key_,
                         const char *lb_value, int address_family)
{
    char *save_ptr = NULL;
    char *lb_key = xstrdup(lb_key_);
    bool success = false;

    for (char *atom = strtok_r(lb_key, ":", &save_ptr); atom;
         atom = strtok_r(NULL, ":", &save_ptr)) {
        if (lb_vip->vip_str && lb_vip->port_str) {
            success = false;
            break;
        }
        success = true;
        if (!lb_vip->vip_str) {
            lb_vip->vip_str = xstrdup(atom);
            lb_vip->template_vips = !!strchr(atom, LEX_TEMPLATE_PREFIX);
        } else {
            lb_vip->port_str = xstrdup(atom);
        }
    }
    free(lb_key);

    if (!success) {
        return xasprintf("%s: should be a template of the form: "
                         "'^vip_variable[:^port_variable|:port]'.",
                         lb_key_);
    }

    /* Backends can either be templates or explicit IPs and ports. */
    lb_vip->address_family = address_family;
    char *template_error = ovn_lb_backends_init_template(lb_vip, lb_value);

    if (template_error) {
        char *error = xasprintf("invalid backend: template (%s)",
                                template_error);
            free(template_error);
            return error;
    }
    return NULL;
}

/* Returns NULL on success, an error string on failure.  The caller is
 * responsible for destroying 'lb_vip' in all cases.
 */
char *
ovn_lb_vip_init(struct ovn_lb_vip *lb_vip, const char *lb_key,
                const char *lb_value, bool template, int address_family)
{
    memset(lb_vip, 0, sizeof *lb_vip);

    return !template
           ?  ovn_lb_vip_init_explicit(lb_vip, lb_key, lb_value)
           :  ovn_lb_vip_init_template(lb_vip, lb_key, lb_value,
                                       address_family);
}

static void
ovn_lb_backends_destroy(struct ovn_lb_vip *vip)
{
    struct ovn_lb_backend *backend;
    VECTOR_FOR_EACH_PTR (&vip->backends, backend) {
        free(backend->ip_str);
        free(backend->port_str);
    }
    vector_destroy(&vip->backends);
}

void
ovn_lb_vip_destroy(struct ovn_lb_vip *vip)
{
    free(vip->vip_str);
    free(vip->port_str);
    ovn_lb_backends_destroy(vip);
}

static void
ovn_lb_vip_format__(const struct ovn_lb_vip *vip, struct ds *s,
                    bool needs_brackets)
{
    if (needs_brackets) {
        ds_put_char(s, '[');
    }
    ds_put_cstr(s, vip->vip_str);
    if (needs_brackets) {
        ds_put_char(s, ']');
    }
    if (vip->port_str) {
        ds_put_format(s, ":%s", vip->port_str);
    }
}

void
ovn_lb_vip_format(const struct ovn_lb_vip *vip, struct ds *s, bool template)
{
    bool needs_brackets = vip->address_family == AF_INET6 && vip->port_str
                          && !template;
    ovn_lb_vip_format__(vip, s, needs_brackets);
}

void
ovn_lb_vip_backends_format(const struct ovn_lb_vip *vip, struct ds *s)
{
    const struct ovn_lb_backend *backend;
    VECTOR_FOR_EACH_PTR (&vip->backends, backend) {
        bool needs_brackets = vip->address_family == AF_INET6 &&
                              vip->port_str &&
                              !ipv6_addr_equals(&backend->ip, &in6addr_any);
        if (needs_brackets) {
            ds_put_char(s, '[');
        }
        ds_put_cstr(s, backend->ip_str);
        if (needs_brackets) {
            ds_put_char(s, ']');
        }
        if (backend->port_str) {
            ds_put_format(s, ":%s", backend->port_str);
        }
        ds_put_char(s, ',');
    }
    ds_chomp(s, ',');
}

/* Formats the VIP in the way the ovn-controller expects it, that is,
 * template IPv6 variables need to be between brackets too.
 */
char *
ovn_lb_vip6_template_format_internal(const struct ovn_lb_vip *vip)
{
    struct ds s = DS_EMPTY_INITIALIZER;

    if (vip->vip_str && *vip->vip_str == LEX_TEMPLATE_PREFIX) {
        ovn_lb_vip_format__(vip, &s, true);
    } else {
        ovn_lb_vip_format(vip, &s, !!vip->port_str);
    }
    return ds_steal_cstr(&s);
}

static uint32_t
ovn_lb_5tuple_hash(const struct ovn_lb_5tuple *tuple)
{
    uint32_t hash = 0;

    hash = hash_add_in6_addr(hash, &tuple->vip_ip);
    hash = hash_add_in6_addr(hash, &tuple->backend_ip);

    hash = hash_add(hash, tuple->vip_port);
    hash = hash_add(hash, tuple->backend_port);

    hash = hash_add(hash, tuple->proto);

    return hash_finish(hash, 0);

}

void
ovn_lb_5tuple_init(struct ovn_lb_5tuple *tuple, const struct ovn_lb_vip *vip,
                   const struct ovn_lb_backend *backend, uint8_t proto)
{
    tuple->vip_ip = vip->vip;
    tuple->vip_port = vip->vip_port;
    tuple->backend_ip = backend->ip;
    tuple->backend_port = backend->port;
    tuple->proto = vip->vip_port ? proto : 0;
}

void
ovn_lb_5tuple_add(struct hmap *tuples, const struct ovn_lb_vip *vip,
                  const struct ovn_lb_backend *backend, uint8_t proto)
{
    struct ovn_lb_5tuple *tuple = xmalloc(sizeof *tuple);
    ovn_lb_5tuple_init(tuple, vip, backend, proto);
    hmap_insert(tuples, &tuple->hmap_node, ovn_lb_5tuple_hash(tuple));
}

void
ovn_lb_5tuple_find_and_delete(struct hmap *tuples,
                              const struct ovn_lb_5tuple *tuple)
{
    uint32_t hash = ovn_lb_5tuple_hash(tuple);

    struct ovn_lb_5tuple *node;
    HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash, tuples) {
        if (ipv6_addr_equals(&tuple->vip_ip, &node->vip_ip) &&
            ipv6_addr_equals(&tuple->backend_ip, &node->backend_ip) &&
            tuple->vip_port == node->vip_port &&
            tuple->backend_port == node->backend_port &&
            tuple->proto == node->proto) {
            hmap_remove(tuples, &node->hmap_node);
            free(node);
            return;
        }
    }
}

void
ovn_lb_5tuples_destroy(struct hmap *tuples)
{
    struct ovn_lb_5tuple *tuple;
    HMAP_FOR_EACH_POP (tuple, hmap_node, tuples) {
        free(tuple);
    }

    hmap_destroy(tuples);
}