summaryrefslogtreecommitdiff
path: root/src/core/devices/ovs/nm-ovs-factory.c
blob: 3a9a736448a9038c52bc618cec4d329743eb17bb (plain)
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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2017 Red Hat, Inc.
 */

#include "src/core/nm-default-daemon.h"

#include "nm-manager.h"
#include "nm-ovsdb.h"
#include "nm-device-ovs-interface.h"
#include "nm-device-ovs-port.h"
#include "nm-device-ovs-bridge.h"
#include "platform/nm-platform.h"
#include "libnm-core-intern/nm-core-internal.h"
#include "settings/nm-settings.h"
#include "devices/nm-device-factory.h"
#include "devices/nm-device-private.h"

/*****************************************************************************/

typedef struct {
    NMDeviceFactory parent;
} NMOvsFactory;

typedef struct {
    NMDeviceFactoryClass parent;
} NMOvsFactoryClass;

#define NM_TYPE_OVS_FACTORY (nm_ovs_factory_get_type())
#define NM_OVS_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_OVS_FACTORY, NMOvsFactory))
#define NM_OVS_FACTORY_CLASS(klass) \
    (G_TYPE_CHECK_CLASS_CAST((klass), NM_TYPE_OVS_FACTORY, NMOvsFactoryClass))
#define NM_IS_OVS_FACTORY(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), NM_TYPE_OVS_FACTORY))
#define NM_IS_OVS_FACTORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), NM_TYPE_OVS_FACTORY))
#define NM_OVS_FACTORY_GET_CLASS(obj) \
    (G_TYPE_INSTANCE_GET_CLASS((obj), NM_TYPE_OVS_FACTORY, NMOvsFactoryClass))

static GType nm_ovs_factory_get_type(void);
G_DEFINE_TYPE(NMOvsFactory, nm_ovs_factory, NM_TYPE_DEVICE_FACTORY)

/*****************************************************************************/

#define _NMLOG_DOMAIN LOGD_DEVICE
#define _NMLOG(level, ifname, con_uuid, ...)                                                  \
    G_STMT_START                                                                              \
    {                                                                                         \
        nm_log((level),                                                                       \
               _NMLOG_DOMAIN,                                                                 \
               (ifname),                                                                      \
               (con_uuid),                                                                    \
               "ovs: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__) _NM_UTILS_MACRO_REST(__VA_ARGS__)); \
    }                                                                                         \
    G_STMT_END

/*****************************************************************************/

NM_DEVICE_FACTORY_DECLARE_TYPES(
    NM_DEVICE_FACTORY_DECLARE_LINK_TYPES(NM_LINK_TYPE_OPENVSWITCH)
        NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES(NM_SETTING_OVS_BRIDGE_SETTING_NAME,
                                                NM_SETTING_OVS_INTERFACE_SETTING_NAME,
                                                NM_SETTING_OVS_PORT_SETTING_NAME))

G_MODULE_EXPORT NMDeviceFactory *
                nm_device_factory_create(GError **error)
{
    nm_manager_set_capability(NM_MANAGER_GET, NM_CAPABILITY_OVS);
    return g_object_new(NM_TYPE_OVS_FACTORY, NULL);
}

static NMDevice *
new_device_from_type(const char *name, NMDeviceType device_type)
{
    GType       type;
    const char *type_desc;
    NMLinkType  link_type = NM_LINK_TYPE_NONE;

    if (nm_manager_get_device(NM_MANAGER_GET, name, device_type))
        return NULL;

    if (device_type == NM_DEVICE_TYPE_OVS_INTERFACE) {
        type      = NM_TYPE_DEVICE_OVS_INTERFACE;
        type_desc = "Open vSwitch Interface";
        link_type = NM_LINK_TYPE_OPENVSWITCH;
    } else if (device_type == NM_DEVICE_TYPE_OVS_PORT) {
        type      = NM_TYPE_DEVICE_OVS_PORT;
        type_desc = "Open vSwitch Port";
    } else if (device_type == NM_DEVICE_TYPE_OVS_BRIDGE) {
        type      = NM_TYPE_DEVICE_OVS_BRIDGE;
        type_desc = "Open vSwitch Bridge";
    } else {
        return NULL;
    }

    return g_object_new(type,
                        NM_DEVICE_IFACE,
                        name,
                        NM_DEVICE_DRIVER,
                        "openvswitch",
                        NM_DEVICE_DEVICE_TYPE,
                        device_type,
                        NM_DEVICE_TYPE_DESC,
                        type_desc,
                        NM_DEVICE_LINK_TYPE,
                        link_type,
                        NULL);
}

static void
ovsdb_device_added(NMOvsdb *        ovsdb,
                   const char *     name,
                   guint            device_type_i,
                   const char *     subtype,
                   NMDeviceFactory *self)
{
    const NMDeviceType device_type = device_type_i;
    NMDevice *         device;

    if (device_type == NM_DEVICE_TYPE_OVS_INTERFACE
        && !NM_IN_STRSET(subtype, "internal", "patch")) {
        /* system interfaces refer to kernel devices and
         * don't need to be created by this factory. Ignore
         * anything that is not an internal or patch
         * interface. */
        return;
    }

    device = new_device_from_type(name, device_type);
    if (!device)
        return;

    g_signal_emit_by_name(self, NM_DEVICE_FACTORY_DEVICE_ADDED, device);
    g_object_unref(device);
}

static void
ovsdb_device_removed(NMOvsdb *        ovsdb,
                     const char *     name,
                     guint            device_type_i,
                     const char *     subtype,
                     NMDeviceFactory *self)
{
    const NMDeviceType device_type = device_type_i;
    NMDevice *         device      = NULL;
    NMDeviceState      device_state;
    gboolean           is_system_interface = FALSE;

    if (device_type == NM_DEVICE_TYPE_OVS_INTERFACE
        && !NM_IN_STRSET(subtype, "internal", "patch", "system"))
        return;

    if (device_type == NM_DEVICE_TYPE_OVS_INTERFACE && nm_streq0(subtype, "system")) {
        NMDevice *             d;
        const CList *          list;
        NMSettingOvsInterface *s_ovs_int;

        /* The device associated to an OVS system interface can be of
         * any kind. Find an interface with the same name and which has
         * the OVS-interface setting. */
        is_system_interface = TRUE;
        nm_manager_for_each_device (NM_MANAGER_GET, d, list) {
            if (!nm_streq0(nm_device_get_iface(d), name))
                continue;
            s_ovs_int = nm_device_get_applied_setting(d, NM_TYPE_SETTING_OVS_INTERFACE);
            if (!s_ovs_int)
                continue;
            if (!nm_streq0(nm_setting_ovs_interface_get_interface_type(s_ovs_int), "system"))
                continue;
            /* Failing the system interface device is almost always the right
             * thing to do when the ovsdb entry is removed. However, to avoid
             * that a late device-removed signal tears down a different,
             * newly-activated connection, let's also check that we have a master.
             * Or in alternative, that the device is assumed/external: in such
             * case it's always fine to fail the device.
             */
            if (!nm_device_get_master(d) && !nm_device_sys_iface_state_is_external_or_assume(d))
                continue;

            device = d;
        }
    } else {
        device = nm_manager_get_device(NM_MANAGER_GET, name, device_type);
    }

    if (!device)
        return;

    device_state = nm_device_get_state(device);

    if (device_type == NM_DEVICE_TYPE_OVS_INTERFACE && nm_device_get_act_request(device)
        && device_state < NM_DEVICE_STATE_DEACTIVATING) {
        nm_device_state_changed(device,
                                NM_DEVICE_STATE_DEACTIVATING,
                                NM_DEVICE_STATE_REASON_REMOVED);
        return;
    }

    /* OVS system interfaces still exist even without the ovsdb entry */
    if (!is_system_interface && device_state == NM_DEVICE_STATE_UNMANAGED) {
        nm_device_unrealize(device, TRUE, NULL);
    }
}

static void
ovsdb_interface_failed(NMOvsdb *        ovsdb,
                       const char *     name,
                       const char *     connection_uuid,
                       const char *     error,
                       NMDeviceFactory *self)
{
    NMDevice *             device     = NULL;
    NMSettingsConnection * connection = NULL;
    NMConnection *         c;
    const char *           type;
    NMSettingOvsInterface *s_ovs_int;
    gboolean               is_patch = FALSE;
    gboolean               ignore;

    device = nm_manager_get_device(NM_MANAGER_GET, name, NM_DEVICE_TYPE_OVS_INTERFACE);
    if (device && connection_uuid) {
        connection =
            nm_settings_get_connection_by_uuid(nm_device_get_settings(device), connection_uuid);
    }

    /* The patch interface which gets created first is expected to
     * fail because the second patch doesn't exist yet. Ignore all
     * failures of patch interfaces. */
    if (connection && (c = nm_settings_connection_get_connection(connection))
        && (type = nm_connection_get_connection_type(c))
        && nm_streq0(type, NM_SETTING_OVS_INTERFACE_SETTING_NAME)
        && (s_ovs_int = nm_connection_get_setting_ovs_interface(c))
        && nm_streq0(nm_setting_ovs_interface_get_interface_type(s_ovs_int), "patch"))
        is_patch = TRUE;

    ignore = !device || is_patch;

    _NMLOG(ignore ? LOGL_DEBUG : LOGL_INFO,
           name,
           connection_uuid,
           "ovs interface \"%s\" (%s) failed%s: %s",
           name,
           connection_uuid,
           ignore ? " (ignored)" : "",
           error);

    if (ignore)
        return;

    if (connection) {
        nm_settings_connection_autoconnect_blocked_reason_set(
            connection,
            NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_FAILED,
            TRUE);
    }

    nm_device_state_changed(device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_OVSDB_FAILED);
}

static void
start(NMDeviceFactory *self)
{
    NMOvsdb *ovsdb;

    ovsdb = nm_ovsdb_get();

    g_signal_connect_object(ovsdb,
                            NM_OVSDB_DEVICE_ADDED,
                            G_CALLBACK(ovsdb_device_added),
                            self,
                            (GConnectFlags) 0);
    g_signal_connect_object(ovsdb,
                            NM_OVSDB_DEVICE_REMOVED,
                            G_CALLBACK(ovsdb_device_removed),
                            self,
                            (GConnectFlags) 0);
    g_signal_connect_object(ovsdb,
                            NM_OVSDB_INTERFACE_FAILED,
                            G_CALLBACK(ovsdb_interface_failed),
                            self,
                            (GConnectFlags) 0);
}

static NMDevice *
create_device(NMDeviceFactory *     self,
              const char *          iface,
              const NMPlatformLink *plink,
              NMConnection *        connection,
              gboolean *            out_ignore)
{
    NMDeviceType device_type     = NM_DEVICE_TYPE_UNKNOWN;
    const char * connection_type = NULL;

    if (g_strcmp0(iface, "ovs-system") == 0) {
        *out_ignore = TRUE;
        return NULL;
    }

    if (connection)
        connection_type = nm_connection_get_connection_type(connection);

    if (plink)
        device_type = NM_DEVICE_TYPE_OVS_INTERFACE;
    else if (g_strcmp0(connection_type, NM_SETTING_OVS_INTERFACE_SETTING_NAME) == 0)
        device_type = NM_DEVICE_TYPE_OVS_INTERFACE;
    else if (g_strcmp0(connection_type, NM_SETTING_OVS_PORT_SETTING_NAME) == 0)
        device_type = NM_DEVICE_TYPE_OVS_PORT;
    else if (g_strcmp0(connection_type, NM_SETTING_OVS_BRIDGE_SETTING_NAME) == 0)
        device_type = NM_DEVICE_TYPE_OVS_BRIDGE;

    return new_device_from_type(iface, device_type);
}

static void
nm_ovs_factory_init(NMOvsFactory *self)
{}

static void
nm_ovs_factory_class_init(NMOvsFactoryClass *klass)
{
    NMDeviceFactoryClass *factory_class = NM_DEVICE_FACTORY_CLASS(klass);

    factory_class->get_supported_types = get_supported_types;
    factory_class->start               = start;
    factory_class->create_device       = create_device;
}