summaryrefslogtreecommitdiff
path: root/libnm-core/nm-setting-ovs-interface.c
blob: 941bfd71bda538cd8cde2dd0f3c077978a70d931 (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
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
// SPDX-License-Identifier: LGPL-2.1+
/*
 * Copyright (C) 2017 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-setting-ovs-interface.h"

#include "nm-connection-private.h"
#include "nm-setting-connection.h"
#include "nm-setting-private.h"

/**
 * SECTION:nm-setting-ovs-interface
 * @short_description: Describes connection properties for Open vSwitch interfaces.
 *
 * The #NMSettingOvsInterface object is a #NMSetting subclass that describes properties
 * necessary for Open vSwitch interfaces.
 **/

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

NM_GOBJECT_PROPERTIES_DEFINE_BASE(PROP_TYPE, );

/**
 * NMSettingOvsInterface:
 *
 * Open vSwitch Interface Settings
 */
struct _NMSettingOvsInterface {
    NMSetting parent;

    char *type;
};

struct _NMSettingOvsInterfaceClass {
    NMSettingClass parent;
};

G_DEFINE_TYPE(NMSettingOvsInterface, nm_setting_ovs_interface, NM_TYPE_SETTING)

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

/**
 * nm_setting_ovs_interface_get_interface_type:
 * @self: the #NMSettingOvsInterface
 *
 * Returns: the #NMSettingOvsInterface:type property of the setting
 *
 * Since: 1.10
 **/
const char *
nm_setting_ovs_interface_get_interface_type(NMSettingOvsInterface *self)
{
    g_return_val_if_fail(NM_IS_SETTING_OVS_INTERFACE(self), NULL);

    return self->type;
}

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

int
_nm_setting_ovs_interface_verify_interface_type(NMSettingOvsInterface *self,
                                                const char *           type,
                                                NMConnection *         connection,
                                                gboolean               normalize,
                                                gboolean *             out_modified,
                                                const char **          out_normalized_type,
                                                GError **              error)
{
    const char *type_from_setting = NULL;
    const char *type_setting      = NULL;
    const char *connection_type;
    gboolean    is_ovs_connection_type;

    if (normalize) {
        g_return_val_if_fail(NM_IS_SETTING_OVS_INTERFACE(self), FALSE);
        g_return_val_if_fail(NM_IS_CONNECTION(connection), FALSE);
        nm_assert(self == nm_connection_get_setting_ovs_interface(connection));
    } else {
        g_return_val_if_fail(!self || NM_IS_SETTING_OVS_INTERFACE(self), FALSE);
        g_return_val_if_fail(!connection || NM_IS_CONNECTION(connection), FALSE);
    }

    NM_SET_OUT(out_modified, FALSE);
    NM_SET_OUT(out_normalized_type, NULL);

    if (type && !NM_IN_STRSET(type, "internal", "system", "patch", "dpdk")) {
        g_set_error(error,
                    NM_CONNECTION_ERROR,
                    NM_CONNECTION_ERROR_INVALID_PROPERTY,
                    _("'%s' is not a valid interface type"),
                    type);
        g_prefix_error(error,
                       "%s.%s: ",
                       NM_SETTING_OVS_INTERFACE_SETTING_NAME,
                       NM_SETTING_OVS_INTERFACE_TYPE);
        return FALSE;
    }

    if (!connection) {
        NM_SET_OUT(out_normalized_type, type);
        return TRUE;
    }

    connection_type = nm_connection_get_connection_type(connection);
    if (!connection_type) {
        /* if we have an ovs-interface, then the connection type must be either
         * "ovs-interface" (for non "system" type) or anything else (for "system" type).
         *
         * The connection type usually can be normalized based on the presence of a
         * base setting. However, in this case, if the connection type is missing,
         * that is too complicate to guess what the user wanted.
         *
         * Require the use to be explicit and fail. */
        g_set_error(error,
                    NM_CONNECTION_ERROR,
                    NM_CONNECTION_ERROR_INVALID_PROPERTY,
                    _("A connection with a '%s' setting needs connection.type explicitly set"),
                    NM_SETTING_OVS_INTERFACE_SETTING_NAME);
        g_prefix_error(error,
                       "%s.%s: ",
                       NM_SETTING_CONNECTION_SETTING_NAME,
                       NM_SETTING_CONNECTION_TYPE);
        return FALSE;
    }

    if (nm_streq(connection_type, NM_SETTING_OVS_INTERFACE_SETTING_NAME)) {
        if (type && nm_streq(type, "system")) {
            g_set_error(error,
                        NM_CONNECTION_ERROR,
                        NM_CONNECTION_ERROR_INVALID_PROPERTY,
                        _("A connection of type '%s' cannot have ovs-interface.type \"system\""),
                        NM_SETTING_OVS_INTERFACE_SETTING_NAME);
            g_prefix_error(error,
                           "%s.%s: ",
                           NM_SETTING_OVS_INTERFACE_SETTING_NAME,
                           NM_SETTING_OVS_INTERFACE_TYPE);
            return FALSE;
        }
        is_ovs_connection_type = TRUE;
    } else {
        if (type && !nm_streq(type, "system")) {
            g_set_error(error,
                        NM_CONNECTION_ERROR,
                        NM_CONNECTION_ERROR_INVALID_PROPERTY,
                        _("A connection of type '%s' cannot have an ovs-interface.type \"%s\""),
                        connection_type,
                        type);
            g_prefix_error(error,
                           "%s.%s: ",
                           NM_SETTING_OVS_INTERFACE_SETTING_NAME,
                           NM_SETTING_OVS_INTERFACE_TYPE);
            return FALSE;
        }
        is_ovs_connection_type = FALSE;
    }

    if (nm_connection_get_setting_by_name(connection, NM_SETTING_OVS_PATCH_SETTING_NAME)) {
        type_from_setting = "patch";
        type_setting      = NM_SETTING_OVS_PATCH_SETTING_NAME;
    }

    if (nm_connection_get_setting_by_name(connection, NM_SETTING_OVS_DPDK_SETTING_NAME)) {
        if (type_from_setting) {
            g_set_error(error,
                        NM_CONNECTION_ERROR,
                        NM_CONNECTION_ERROR_INVALID_PROPERTY,
                        _("A connection can not have both '%s' and '%s' settings at the same time"),
                        NM_SETTING_OVS_DPDK_SETTING_NAME,
                        type_setting);
            return FALSE;
        }
        type_from_setting = "dpdk";
        type_setting      = NM_SETTING_OVS_DPDK_SETTING_NAME;
    }

    if (type_from_setting) {
        if (!is_ovs_connection_type) {
            g_set_error(error,
                        NM_CONNECTION_ERROR,
                        NM_CONNECTION_ERROR_INVALID_PROPERTY,
                        _("A connection with '%s' setting must be of connection.type "
                          "\"ovs-interface\" but is \"%s\""),
                        NM_SETTING_OVS_PATCH_SETTING_NAME,
                        connection_type);
            g_prefix_error(error,
                           "%s.%s: ",
                           NM_SETTING_OVS_INTERFACE_SETTING_NAME,
                           NM_SETTING_OVS_INTERFACE_TYPE);
            return FALSE;
        }

        if (type) {
            if (!nm_streq(type, type_from_setting)) {
                g_set_error(error,
                            NM_CONNECTION_ERROR,
                            NM_CONNECTION_ERROR_INVALID_PROPERTY,
                            _("A connection with '%s' setting needs to be of '%s' interface type, "
                              "not '%s'"),
                            type_setting,
                            type_from_setting,
                            type);
                g_prefix_error(error,
                               "%s.%s: ",
                               NM_SETTING_OVS_INTERFACE_SETTING_NAME,
                               NM_SETTING_OVS_INTERFACE_TYPE);
                return FALSE;
            }
            NM_SET_OUT(out_normalized_type, type);
            return TRUE;
        }
        type = type_from_setting;
        goto normalize;
    } else {
        if (nm_streq0(type, "patch")) {
            g_set_error(
                error,
                NM_CONNECTION_ERROR,
                NM_CONNECTION_ERROR_MISSING_SETTING,
                _("A connection with ovs-interface.type '%s' setting a 'ovs-patch' setting"),
                type);
            g_prefix_error(error,
                           "%s.%s: ",
                           NM_SETTING_OVS_INTERFACE_SETTING_NAME,
                           NM_SETTING_OVS_INTERFACE_TYPE);
            return FALSE;
        }
    }

    if (type) {
        NM_SET_OUT(out_normalized_type, type);
        return TRUE;
    }

    if (is_ovs_connection_type)
        type = "internal";
    else
        type = "system";

    NM_SET_OUT(out_normalized_type, type);

normalize:
    if (!normalize) {
        if (!self) {
            g_set_error(error,
                        NM_CONNECTION_ERROR,
                        NM_CONNECTION_ERROR_MISSING_SETTING,
                        _("Missing ovs interface setting"));
            g_prefix_error(error, "%s: ", NM_SETTING_OVS_INTERFACE_SETTING_NAME);
        } else {
            g_set_error(error,
                        NM_CONNECTION_ERROR,
                        NM_CONNECTION_ERROR_MISSING_PROPERTY,
                        _("Missing ovs interface type"));
            g_prefix_error(error,
                           "%s.%s: ",
                           NM_SETTING_OVS_INTERFACE_SETTING_NAME,
                           NM_SETTING_OVS_INTERFACE_TYPE);
        }
        return NM_SETTING_VERIFY_NORMALIZABLE_ERROR;
    }

    if (!self) {
        self = NM_SETTING_OVS_INTERFACE(nm_setting_ovs_interface_new());
        nm_connection_add_setting(connection, NM_SETTING(self));
    }
    g_object_set(self, NM_SETTING_OVS_INTERFACE_TYPE, type, NULL);
    NM_SET_OUT(out_modified, TRUE);

    return TRUE;
}

static int
verify(NMSetting *setting, NMConnection *connection, GError **error)
{
    NMSettingOvsInterface *self  = NM_SETTING_OVS_INTERFACE(setting);
    NMSettingConnection *  s_con = NULL;

    if (connection) {
        const char *slave_type;

        s_con = nm_connection_get_setting_connection(connection);
        if (!s_con) {
            g_set_error(error,
                        NM_CONNECTION_ERROR,
                        NM_CONNECTION_ERROR_MISSING_SETTING,
                        _("missing setting"));
            g_prefix_error(error, "%s: ", NM_SETTING_CONNECTION_SETTING_NAME);
            return FALSE;
        }

        if (!nm_setting_connection_get_master(s_con)) {
            g_set_error(error,
                        NM_CONNECTION_ERROR,
                        NM_CONNECTION_ERROR_INVALID_PROPERTY,
                        _("A connection with a '%s' setting must have a master."),
                        NM_SETTING_OVS_INTERFACE_SETTING_NAME);
            g_prefix_error(error,
                           "%s.%s: ",
                           NM_SETTING_CONNECTION_SETTING_NAME,
                           NM_SETTING_CONNECTION_MASTER);
            return FALSE;
        }

        slave_type = nm_setting_connection_get_slave_type(s_con);
        if (slave_type && !nm_streq(slave_type, NM_SETTING_OVS_PORT_SETTING_NAME)) {
            g_set_error(error,
                        NM_CONNECTION_ERROR,
                        NM_CONNECTION_ERROR_INVALID_PROPERTY,
                        _("A connection with a '%s' setting must have the slave-type set to '%s'. "
                          "Instead it is '%s'"),
                        NM_SETTING_OVS_INTERFACE_SETTING_NAME,
                        NM_SETTING_OVS_PORT_SETTING_NAME,
                        slave_type);
            g_prefix_error(error,
                           "%s.%s: ",
                           NM_SETTING_CONNECTION_SETTING_NAME,
                           NM_SETTING_CONNECTION_SLAVE_TYPE);
            return FALSE;
        }
    }

    return _nm_setting_ovs_interface_verify_interface_type(self,
                                                           self->type,
                                                           connection,
                                                           FALSE,
                                                           NULL,
                                                           NULL,
                                                           error);
}

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

static void
get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    NMSettingOvsInterface *self = NM_SETTING_OVS_INTERFACE(object);

    switch (prop_id) {
    case PROP_TYPE:
        g_value_set_string(value, self->type);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    NMSettingOvsInterface *self = NM_SETTING_OVS_INTERFACE(object);

    switch (prop_id) {
    case PROP_TYPE:
        g_free(self->type);
        self->type = g_value_dup_string(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

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

static void
nm_setting_ovs_interface_init(NMSettingOvsInterface *self)
{}

/**
 * nm_setting_ovs_interface_new:
 *
 * Creates a new #NMSettingOvsInterface object with default values.
 *
 * Returns: (transfer full): the new empty #NMSettingOvsInterface object
 *
 * Since: 1.10
 **/
NMSetting *
nm_setting_ovs_interface_new(void)
{
    return (NMSetting *) g_object_new(NM_TYPE_SETTING_OVS_INTERFACE, NULL);
}

static void
finalize(GObject *object)
{
    NMSettingOvsInterface *self = NM_SETTING_OVS_INTERFACE(object);

    g_free(self->type);

    G_OBJECT_CLASS(nm_setting_ovs_interface_parent_class)->finalize(object);
}

static void
nm_setting_ovs_interface_class_init(NMSettingOvsInterfaceClass *klass)
{
    GObjectClass *  object_class  = G_OBJECT_CLASS(klass);
    NMSettingClass *setting_class = NM_SETTING_CLASS(klass);

    object_class->get_property = get_property;
    object_class->set_property = set_property;
    object_class->finalize     = finalize;

    setting_class->verify = verify;

    /**
     * NMSettingOvsInterface:type:
     *
     * The interface type. Either "internal", "system", "patch", "dpdk", or empty.
     *
     * Since: 1.10
     **/
    obj_properties[PROP_TYPE] = g_param_spec_string(NM_SETTING_OVS_INTERFACE_TYPE,
                                                    "",
                                                    "",
                                                    NULL,
                                                    G_PARAM_READWRITE | NM_SETTING_PARAM_INFERRABLE
                                                        | G_PARAM_STATIC_STRINGS);

    g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);

    _nm_setting_class_commit(setting_class, NM_META_SETTING_TYPE_OVS_INTERFACE);
}