File: evpn-binding.c

package info (click to toggle)
ovn 26.03.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,480 kB
  • sloc: ansic: 117,371; xml: 25,046; sh: 3,524; python: 1,918; makefile: 866
file content (430 lines) | stat: -rw-r--r-- 13,393 bytes parent folder | download | duplicates (2)
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
428
429
430
/* Copyright (c) 2025, 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 "flow.h"
#include "local_data.h"
#include "neighbor-exchange.h"
#include "openvswitch/vlog.h"
#include "ovn-sb-idl.h"
#include "unixctl.h"
#include "vec.h"
#include "vswitch-idl.h"

#include "evpn-binding.h"

VLOG_DEFINE_THIS_MODULE(evpn_binding);

static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);

static void collect_evpn_datapaths(const struct hmap *local_datapaths,
                                   struct hmap *evpn_datapaths);

struct evpn_tunnel {
    uint16_t dst_port;
    ofp_port_t ofport;
};

static struct vector collect_evpn_tunnel_interfaces(
    const struct ovsrec_bridge *br_int);
static const struct evpn_tunnel *evpn_tunnel_find(
    const struct vector *evpn_tunnels, uint16_t dst_port);

static struct evpn_binding *evpn_binding_add(
    struct hmap *evpn_bindings, const struct evpn_remote_vtep *vtep,
    uint32_t binding_key);
static uint32_t evpn_binding_hash(const struct in6_addr *remote_ip,
                                  uint32_t vni);
static struct evpn_multicast_group *evpn_multicast_group_find(
    const struct hmap *evpn_mc_groups, uint32_t vni);
static struct evpn_multicast_group *evpn_multicast_group_add(
    struct hmap *evpn_mc_groups, uint32_t vni);

void
evpn_binding_run(const struct evpn_binding_ctx_in *b_ctx_in,
                 struct evpn_binding_ctx_out *b_ctx_out)
{
    struct vector tunnels = collect_evpn_tunnel_interfaces(b_ctx_in->br_int);
    struct hmapx stale_bindings = HMAPX_INITIALIZER(&stale_bindings);
    struct hmapx stale_mc_groups = HMAPX_INITIALIZER(&stale_mc_groups);
    uint32_t hint = OVN_MIN_EVPN_KEY;

    collect_evpn_datapaths(b_ctx_in->local_datapaths, b_ctx_out->datapaths);

    struct evpn_binding *binding;
    HMAP_FOR_EACH (binding, hmap_node, b_ctx_out->bindings) {
        hmapx_add(&stale_bindings, binding);
    }

    struct evpn_multicast_group *mc_group;
    HMAP_FOR_EACH (mc_group, hmap_node, b_ctx_out->multicast_groups) {
        hmapx_add(&stale_mc_groups, mc_group);
    }

    const struct evpn_remote_vtep *vtep;
    HMAP_FOR_EACH (vtep, hmap_node, b_ctx_in->remote_vteps) {
        const struct evpn_tunnel *tun = evpn_tunnel_find(&tunnels, vtep->port);
        if (!tun) {
            VLOG_WARN_RL(&rl, "Couldn't find EVPN tunnel for %"PRIu16
                         " destination port.", vtep->port);
            continue;
        }

        const struct evpn_datapath *edp =
            evpn_datapath_find(b_ctx_out->datapaths, vtep->vni);
        if (!edp) {
            VLOG_WARN_RL(&rl, "Couldn't find EVPN datapath for %"PRIu16" VNI",
                         vtep->vni);
            continue;
        }

        binding = evpn_binding_find(b_ctx_out->bindings, &vtep->ip, vtep->vni);
        if (!binding) {
            uint32_t tunnel_key =
                ovn_allocate_tnlid(b_ctx_out->tunnel_keys, "evpn-binding",
                                   OVN_MIN_EVPN_KEY, OVN_MAX_EVPN_KEY, &hint);
            if (!tunnel_key) {
                continue;
            }

            binding = evpn_binding_add(b_ctx_out->bindings, vtep, tunnel_key);
        }

        mc_group = evpn_multicast_group_find(b_ctx_out->multicast_groups,
                                             vtep->vni);
        if (!mc_group) {
            mc_group = evpn_multicast_group_add(b_ctx_out->multicast_groups,
                                                vtep->vni);
        }

        bool updated = false;
        if (binding->tunnel_ofport != tun->ofport) {
            binding->tunnel_ofport = tun->ofport;
            updated = true;
        }

        if (binding->dp_key != edp->ldp->datapath->tunnel_key) {
            binding->dp_key = edp->ldp->datapath->tunnel_key;
            updated = true;
        }

        uint16_t fdb_age_threshold =
            MIN(smap_get_uint(&edp->ldp->datapath->external_ids,
                              "fdb_age_threshold", 0), UINT16_MAX);
        if (binding->fdb_age_threshold != fdb_age_threshold) {
            binding->fdb_age_threshold = fdb_age_threshold;
            updated = true;
        }

        if (updated) {
            hmapx_add(b_ctx_out->updated_bindings, binding);

            hmapx_add(&mc_group->bindings, binding);
            hmapx_add(b_ctx_out->updated_multicast_groups, mc_group);
        }

        hmapx_find_and_delete(&stale_bindings, binding);
        hmapx_find_and_delete(&stale_mc_groups, mc_group);
    }

    struct hmapx_node *node;
    HMAPX_FOR_EACH (node, &stale_mc_groups) {
        mc_group = node->data;

        uuidset_insert(b_ctx_out->removed_multicast_groups,
                       &mc_group->flow_uuid);
        hmap_remove(b_ctx_out->multicast_groups, &mc_group->hmap_node);
        hmapx_destroy(&mc_group->bindings);
        free(mc_group);
    }

    HMAPX_FOR_EACH (node, &stale_bindings) {
        binding = node->data;

        mc_group = evpn_multicast_group_find(b_ctx_out->multicast_groups,
                                             binding->vni);
        if (mc_group) {
            hmapx_find_and_delete(&mc_group->bindings, binding);
            hmapx_add(b_ctx_out->updated_multicast_groups, mc_group);
        }
        uuidset_insert(b_ctx_out->removed_bindings, &binding->flow_uuid);
        hmap_remove(b_ctx_out->bindings, &binding->hmap_node);
        free(binding);
    }

    vector_destroy(&tunnels);
    hmapx_destroy(&stale_bindings);
    hmapx_destroy(&stale_mc_groups);
}

struct evpn_binding *
evpn_binding_find(const struct hmap *evpn_bindings,
                  const struct in6_addr *remote_ip, uint32_t vni)
{
    uint32_t hash = evpn_binding_hash(remote_ip, vni);

    struct evpn_binding *binding;
    HMAP_FOR_EACH_WITH_HASH (binding, hmap_node, hash, evpn_bindings) {
        if (ipv6_addr_equals(&binding->remote_ip, remote_ip) &&
            binding->vni == vni) {
            return binding;
        }
    }

    return NULL;
}

void
evpn_bindings_destroy(struct hmap *bindings)
{
    struct evpn_binding *binding;
    HMAP_FOR_EACH_POP (binding, hmap_node, bindings) {
        free(binding);
    }
    hmap_destroy(bindings);
}

void
evpn_vtep_binding_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
                        const char *argv[] OVS_UNUSED, void *data_)
{
    struct hmap *bindings = data_;
    struct ds ds = DS_EMPTY_INITIALIZER;

    const struct evpn_binding *binding;
    HMAP_FOR_EACH (binding, hmap_node, bindings) {
        ds_put_format(&ds, "UUID: "UUID_FMT", Remote IP: ",
                      UUID_ARGS(&binding->flow_uuid));
        ipv6_format_mapped(&binding->remote_ip, &ds);
        ds_put_format(&ds, ", vni: %"PRIu32", binding_key: %#"PRIx32", "
                      "tunnel_ofport: %"PRIu32", dp_key: %"PRIu32,
                      binding->vni, binding->binding_key,
                      binding->tunnel_ofport, binding->dp_key);
        ds_put_char(&ds, '\n');
    }

    unixctl_command_reply(conn, ds_cstr_ro(&ds));
    ds_destroy(&ds);
}

const struct evpn_datapath *
evpn_datapath_find(const struct hmap *evpn_datapaths, uint32_t vni)
{
    const struct evpn_datapath *edp;
    HMAP_FOR_EACH_WITH_HASH (edp, hmap_node, vni, evpn_datapaths) {
        if (edp->vni == vni) {
            return edp;
        }
    }

    return NULL;
}

void
evpn_datapaths_clear(struct hmap *evpn_datapaths)
{
    struct evpn_datapath *edp;
    HMAP_FOR_EACH_POP (edp, hmap_node, evpn_datapaths) {
        free(edp);
    }
}

void
evpn_datapaths_destroy(struct hmap *evpn_datapaths)
{
    evpn_datapaths_clear(evpn_datapaths);
    hmap_destroy(evpn_datapaths);
}

void
evpn_multicast_groups_destroy(struct hmap *multicast_groups)
{
    struct evpn_multicast_group *mc_group;
    HMAP_FOR_EACH_POP (mc_group, hmap_node, multicast_groups) {
        hmapx_destroy(&mc_group->bindings);
        free(mc_group);
    }
    hmap_destroy(multicast_groups);
}

void
evpn_multicast_group_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
                           const char *argv[] OVS_UNUSED, void *data_)
{
    struct hmap *mc_groups = data_;
    struct ds ds = DS_EMPTY_INITIALIZER;

    const struct evpn_multicast_group *mc_group;
    HMAP_FOR_EACH (mc_group, hmap_node, mc_groups) {
        ds_put_format(&ds, "UUID: "UUID_FMT", Remote IPs: ",
                      UUID_ARGS(&mc_group->flow_uuid));

        struct hmapx_node *node;
        HMAPX_FOR_EACH (node, &mc_group->bindings) {
            const struct evpn_binding *binding = node->data;
            ipv6_format_mapped(&binding->remote_ip, &ds);
            ds_put_cstr(&ds, ", ");
        }

        ds_put_format(&ds, "vni: %"PRIu32, mc_group->vni);
        ds_put_char(&ds, '\n');
    }

    unixctl_command_reply(conn, ds_cstr_ro(&ds));
    ds_destroy(&ds);
}

static void
collect_evpn_datapaths(const struct hmap *local_datapaths,
                       struct hmap *evpn_datapaths)
{
    struct local_datapath *ld;
    HMAP_FOR_EACH (ld, hmap_node, local_datapaths) {
        if (!ld->is_switch) {
            continue;
        }

        int64_t vni = ovn_smap_get_llong(&ld->datapath->external_ids,
                                         "dynamic-routing-vni", -1);
        if (!ovn_is_valid_vni(vni)) {
            continue;
        }

        if (evpn_datapath_find(evpn_datapaths, vni)) {
            VLOG_WARN_RL(&rl, "Datapath "UUID_FMT" with duplicate VNI %"PRIi64,
                         UUID_ARGS(&ld->datapath->header_.uuid), vni);
            continue;
        }

        struct evpn_datapath *edp = xmalloc(sizeof *edp);
        *edp = (struct evpn_datapath) {
            .ldp = ld,
            .vni = vni,
        };

        hmap_insert(evpn_datapaths, &edp->hmap_node, edp->vni);
    }
}

static struct vector
collect_evpn_tunnel_interfaces(const struct ovsrec_bridge *br_int)
{
    struct vector evpn_tunnels = VECTOR_EMPTY_INITIALIZER(struct evpn_tunnel);

    for (size_t i = 0; i < br_int->n_ports; i++) {
        const struct ovsrec_port *port = br_int->ports[i];
        if (!smap_get_bool(&port->external_ids, "ovn-evpn-tunnel", false)) {
            continue;
        }

        const struct ovsrec_interface *iface = port->interfaces[0];
        if (iface->n_ofport != 1) {
            continue;
        }

        const char *dst_port_str =
            smap_get_def(&iface->options, "dst_port",
                         OVS_STRINGIZE(DEFAULT_VXLAN_PORT));
        unsigned int dst_port;
        if (!str_to_uint(dst_port_str, 10, &dst_port)) {
            VLOG_WARN_RL(&rl, "Couldn't parse \"dst_port\" %s for tunnel"
                         " interface %s", dst_port_str, iface->name);
            continue;
        }

        struct evpn_tunnel tun = {
            .dst_port = dst_port,
            .ofport = u16_to_ofp(iface->ofport[0]),
        };
        vector_push(&evpn_tunnels, &tun);
    }

    return evpn_tunnels;
}

static const struct evpn_tunnel *
evpn_tunnel_find(const struct vector *evpn_tunnels, uint16_t dst_port)
{
    const struct evpn_tunnel *tun;
    VECTOR_FOR_EACH_PTR (evpn_tunnels, tun) {
        if (tun->dst_port == dst_port) {
            return tun;
        }
    }

    return NULL;
}

static uint32_t
evpn_binding_hash(const struct in6_addr *remote_ip, uint32_t vni)
{
    uint32_t hash = 0;
    hash = hash_add_in6_addr(hash, remote_ip);
    hash = hash_add(hash, vni);

    return hash_finish(hash, 20);
}

static struct evpn_binding *
evpn_binding_add(struct hmap *evpn_bindings,
                 const struct evpn_remote_vtep *vtep, uint32_t binding_key)
{
    struct evpn_binding *binding = xmalloc(sizeof *binding);
    *binding = (struct evpn_binding) {
        .flow_uuid = uuid_random(),
        .remote_ip = vtep->ip,
        .vni = vtep->vni,
        .binding_key = binding_key,
        .tunnel_ofport = OFPP_NONE,
        .dp_key = 0,
    };

    uint32_t hash = evpn_binding_hash(&vtep->ip, vtep->vni);
    hmap_insert(evpn_bindings, &binding->hmap_node, hash);

    return binding;
}

static struct evpn_multicast_group *
evpn_multicast_group_find(const struct hmap *evpn_mc_groups, uint32_t vni)
{
    uint32_t hash = hash_int(vni, 4);

    struct evpn_multicast_group *mc_group;
    HMAP_FOR_EACH_WITH_HASH (mc_group, hmap_node, hash, evpn_mc_groups) {
        if (mc_group->vni == vni) {
            return mc_group;
        }
    }

    return NULL;
}

static struct evpn_multicast_group *
evpn_multicast_group_add(struct hmap *evpn_mc_groups, uint32_t vni)
{
    struct evpn_multicast_group *mc_group = xmalloc(sizeof *mc_group);
    *mc_group = (struct evpn_multicast_group) {
        .flow_uuid = uuid_random(),
        .bindings = HMAPX_INITIALIZER(&mc_group->bindings),
        .vni = vni,
    };

    uint32_t hash = hash_int(vni, 4);
    hmap_insert(evpn_mc_groups, &mc_group->hmap_node, hash);

    return mc_group;
}