File: bfd.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 (282 lines) | stat: -rw-r--r-- 10,329 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
/* Copyright (c) 2017 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 "bfd.h"
#include "encaps.h"
#include "lport.h"

#include "lib/hash.h"
#include "lib/sset.h"
#include "lib/util.h"
#include "lib/vswitch-idl.h"
#include "openvswitch/vlog.h"
#include "lib/ovn-sb-idl.h"
#include "ovn-controller.h"

VLOG_DEFINE_THIS_MODULE(ovn_bfd);

void
bfd_register_ovs_idl(struct ovsdb_idl *ovs_idl)
{
    /* NOTE: this assumes that binding.c has added the
     * ovsrec_interface table */
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_bfd);
    ovsdb_idl_track_add_column(ovs_idl, &ovsrec_interface_col_bfd_status);
}

void
bfd_calculate_active_tunnels(const struct ovsrec_bridge *br_int,
                             struct sset *active_tunnels)
{
    int i;

    if (!br_int) {
        /* Nothing to do if integration bridge doesn't exist. */
        return;
    }

    for (i = 0; i < br_int->n_ports; i++) {
        const struct ovsrec_port *port_rec = br_int->ports[i];

        if (!strcmp(port_rec->name, br_int->name)) {
            continue;
        }

        int j;
        for (j = 0; j < port_rec->n_interfaces; j++) {
            const struct ovsrec_interface *iface_rec;
            iface_rec = port_rec->interfaces[j];

            /* Check if this is a tunnel interface. */
            if (smap_get(&iface_rec->options, "remote_ip")) {
                /* Add ovn-chassis-id if the bfd_status of the tunnel
                 * is active */
                const char *bfd = smap_get(&iface_rec->bfd, "enable");
                if (bfd && !strcmp(bfd, "true")) {
                    const char *status = smap_get(&iface_rec->bfd_status,
                                                  "state");
                    if (status && !strcmp(status, "up")) {
                        const char *id = smap_get(&port_rec->external_ids,
                                                  OVN_TUNNEL_ID);
                        if (id) {
                            char *chassis_name = NULL;

                            if (encaps_tunnel_id_parse(id, &chassis_name,
                                                       NULL, NULL)) {
                                if (!sset_contains(active_tunnels,
                                                   chassis_name)) {
                                    sset_add(active_tunnels, chassis_name);
                                }
                                free(chassis_name);
                            }
                        }
                    }
                }
            }
        }
    }
}

/* Loops through the HA chassis groups in the SB DB and returns
 * the set of chassis which the call can establish the BFD sessions
 * with.
 * Eg.
 * If there are 2 HA chassis groups.
 * Group name - hapgrp1
 *   - HA chassis - (HA1, HA2, HA3)
 *   - ref chassis - (C1, C2)
 *
 * Group name - hapgrp2
 *   - HA chassis - (HA1, HA4, HA5)
 *   - ref chassis - (C1, C3, C4)
 *
 * If 'our_chassis' is HA1 then this function returns
 *  bfd chassis set - (HA2, HA3, HA4 HA5, C1, C2, C3, C4)
 *
 * If 'our_chassis' is C1 then this function returns
 *  bfd chassis set - (HA1, HA2, HA3, HA4, HA5)
 *
 * If 'our_chassis' is HA5 then this function returns
 *  bfd chassis set - (HA1, HA4, C1, C3, C4)
 *
 * If 'our_chassis' is C2 then this function returns
 *  bfd chassis set - (HA1, HA2, HA3)
 *
 * If 'our_chassis' is C5 then this function returns empty bfd set.
 */
void
bfd_calculate_chassis(
    const struct sbrec_chassis *our_chassis,
    const struct sbrec_ha_chassis_group_table *ha_chassis_grp_table,
    struct sset *bfd_chassis)
{
    const struct sbrec_ha_chassis_group *ha_chassis_grp;
    SBREC_HA_CHASSIS_GROUP_TABLE_FOR_EACH (ha_chassis_grp,
                                           ha_chassis_grp_table) {
        bool is_ha_chassis = false;
        struct sset grp_chassis = SSET_INITIALIZER(&grp_chassis);
        const struct sbrec_ha_chassis *ha_ch;
        bool bfd_setup_required = false;
        if (ha_chassis_grp->n_ha_chassis < 2) {
            /* No need to consider the chassis group for BFD if
             * there is  1 or no chassis in it. */
            continue;
        }
        for (size_t i = 0; i < ha_chassis_grp->n_ha_chassis; i++) {
            ha_ch = ha_chassis_grp->ha_chassis[i];
            if (!ha_ch->chassis) {
                continue;
            }
            sset_add(&grp_chassis, ha_ch->chassis->name);
            if (our_chassis == ha_ch->chassis) {
                is_ha_chassis = true;
                bfd_setup_required = true;
            }
        }

        if (is_ha_chassis) {
            /* It's an HA chassis. So add the ref_chassis to the bfd set. */
            for (size_t i = 0; i < ha_chassis_grp->n_ref_chassis; i++) {
                struct sbrec_chassis *ref_ch = ha_chassis_grp->ref_chassis[i];
                if (smap_get_bool(&ref_ch->other_config, "is-remote", false)) {
                    continue;
                }
                /* we have bfd_setup_required == true anyway, so we skip adding
                 * it to an sset that we later move to another sset again. */
                sset_add(bfd_chassis, ref_ch->name);
            }
        } else {
            /* This is not an HA chassis. Check if this chassis is present
             * in the ref_chassis list. If so add the ha_chassis to the
             * sset .*/
            for (size_t i = 0; i < ha_chassis_grp->n_ref_chassis; i++) {
                if (our_chassis == ha_chassis_grp->ref_chassis[i]) {
                    bfd_setup_required = true;
                    break;
                }
            }
        }

        if (bfd_setup_required) {
            const char *name;
            SSET_FOR_EACH (name, &grp_chassis) {
                sset_add(bfd_chassis, name);
            }
        }
        sset_destroy(&grp_chassis);
    }
}

void
bfd_run(const struct ovsrec_interface_table *interface_table,
        const struct ovsrec_bridge *br_int,
        const struct sset *bfd_chassis,
        const struct sbrec_chassis *chassis_rec,
        const struct sbrec_sb_global_table *sb_global_table,
        const struct ovsrec_open_vswitch_table *ovs_table)
{
    if (!chassis_rec) {
        return;
    }

    /* Identify tunnels ports(connected to remote chassis id) to enable bfd */
    struct sset tunnels = SSET_INITIALIZER(&tunnels);
    struct sset bfd_ifaces = SSET_INITIALIZER(&bfd_ifaces);
    for (size_t k = 0; k < br_int->n_ports; k++) {
        const char *tunnel_id = smap_get(&br_int->ports[k]->external_ids,
                                          OVN_TUNNEL_ID);
        if (tunnel_id) {
            char *chassis_name = NULL;
            char *port_name = br_int->ports[k]->name;

            sset_add(&tunnels, port_name);

            if (encaps_tunnel_id_parse(tunnel_id, &chassis_name, NULL, NULL)) {
                if (sset_contains(bfd_chassis, chassis_name)) {
                    sset_add(&bfd_ifaces, port_name);
                }
                free(chassis_name);
            }
        }
    }

    /* Warn if BFD is needed but flow-based tunnels are enabled */
    if (is_flow_based_tunnels_enabled(ovs_table, chassis_rec)
        && !sset_is_empty(bfd_chassis)) {
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
        VLOG_WARN_RL(&rl, "Flow-based tunnels are enabled but BFD is required "
                     "for HA chassis groups. BFD will not function correctly "
                     "with flow-based tunnels.");
    }

    const struct sbrec_sb_global *sb
        = sbrec_sb_global_table_first(sb_global_table);
    struct smap bfd = SMAP_INITIALIZER(&bfd);
    smap_add(&bfd, "enable", "true");

    if (sb) {
        const char *min_rx = smap_get(&sb->options, "bfd-min-rx");
        const char *decay_min_rx = smap_get(&sb->options, "bfd-decay-min-rx");
        const char *min_tx = smap_get(&sb->options, "bfd-min-tx");
        const char *mult = smap_get(&sb->options, "bfd-mult");
        if (min_rx) {
            smap_add(&bfd, "min_rx", min_rx);
        }
        if (decay_min_rx) {
            smap_add(&bfd, "decay_min_rx", decay_min_rx);
        }
        if (min_tx) {
            smap_add(&bfd, "min_tx", min_tx);
        }
        if (mult) {
            smap_add(&bfd, "mult", mult);
        }
        /* `check_tnl_key` must always be set to "true" to avoid processing of
         * BFD control messages originating from a logical port. */
        smap_add(&bfd, "check_tnl_key", "true");
    }

    /* Enable or disable bfd */
    const struct ovsrec_interface *iface;
    OVSREC_INTERFACE_TABLE_FOR_EACH (iface, interface_table) {
        if (sset_contains(&tunnels, iface->name)) {
            if (sset_contains(&bfd_ifaces, iface->name)) {
                /* We need to enable BFD for this interface. Configure the
                 * BFD params if
                 *  - If BFD was disabled earlier
                 *  - Or if CMS has updated BFD config options.
                 */
                if (!smap_equal(&iface->bfd, &bfd)) {
                    ovsrec_interface_verify_bfd(iface);
                    ovsrec_interface_set_bfd(iface, &bfd);
                    VLOG_INFO("Enabled BFD on interface %s", iface->name);
                }
            } else {
                /* We need to disable BFD for this interface if it was enabled
                 * earlier. */
                if (smap_count(&iface->bfd)) {
                    ovsrec_interface_verify_bfd(iface);
                    ovsrec_interface_set_bfd(iface, NULL);
                    VLOG_INFO("Disabled BFD on interface %s", iface->name);
                }
            }
        }
    }

    smap_destroy(&bfd);
    sset_destroy(&tunnels);
    sset_destroy(&bfd_ifaces);
}