File: ovsport.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 (291 lines) | stat: -rw-r--r-- 10,930 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
/* Copyright (c) 2021 Canonical
 *
 * 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 "ovsport.h"

#include "lib/vswitch-idl.h"
#include "openvswitch/vlog.h"

VLOG_DEFINE_THIS_MODULE(ovsport);

/* Create a port and interface record and add it to 'bridge' in the Open
 * vSwitch database represented by 'ovs_idl_txn'.
 *
 * 'name' is required and is used both for the name of the port and interface
 * records.  Depending on the contents of the optional 'iface_type' parameter
 * the name may need to refer to an existing interface in the system.  It is
 * the caller's responsibility to ensure that no other port with the desired
 * name already exists.
 *
 * 'iface_type' optionally specifies the type of interface, otherwise set it to
 * NULL.
 *
 * 'port_external_ids' - the contents of the map will be used to fill the
 * external_ids column of the created port record, otherwise set it to NULL.
 *
 * 'iface_external_ids' - the contents of the map will be used to fill the
 * external_ids column of the created interface record, otherwise set it to
 * NULL.
 *
 * 'iface_options' - the contents of the map will be used to fill the options
 * column of the created interface record, otherwise set it to NULL.
 *
 * 'iface_mtu_request' - if a value > 0 is provided it will be filled into the
 * mtu_request column of the created interface record. */
void
ovsport_create(struct ovsdb_idl_txn *ovs_idl_txn,
               const struct ovsrec_bridge *bridge,
               const char *name,
               const char *iface_type,
               const struct smap *port_external_ids,
               const struct smap *iface_external_ids,
               const struct smap *iface_options,
               const int64_t iface_mtu_request)
{
    struct ovsrec_interface *iface;
    iface = ovsrec_interface_insert(ovs_idl_txn);
    ovsrec_interface_set_name(iface, name);
    if (iface_type) {
        ovsrec_interface_set_type(iface, iface_type);
    }
    ovsrec_interface_set_external_ids(iface, iface_external_ids);
    ovsrec_interface_set_options(iface, iface_options);
    ovsrec_interface_set_mtu_request(
            iface, &iface_mtu_request, iface_mtu_request > 0);

    struct ovsrec_port *port;
    port = ovsrec_port_insert(ovs_idl_txn);
    ovsrec_port_set_name(port, name);
    ovsrec_port_set_interfaces(port, &iface, 1);
    ovsrec_port_set_external_ids(port, port_external_ids);

    ovsrec_bridge_verify_ports(bridge);
    ovsrec_bridge_update_ports_addvalue(bridge, port);
}

/* Remove 'port' from 'bridge' and delete the 'port' record and any records
 * with a weakRef to it. */
void
ovsport_remove(const struct ovsrec_bridge *bridge,
               const struct ovsrec_port *port)
{
    ovsrec_bridge_verify_ports(bridge);
    ovsrec_port_verify_interfaces(port);
    for (struct ovsrec_interface *iface = *port->interfaces;
         iface - *port->interfaces < port->n_interfaces;
         iface++) {
        ovsrec_interface_delete(iface);
    }
    ovsrec_bridge_update_ports_delvalue(bridge, port);
    ovsrec_port_delete(port);
}

static void update_interface_smap_column(
        const struct ovsrec_interface *, const struct smap *,
        const struct smap *, void (*fsetkey)(const struct ovsrec_interface *,
                                             const char *, const char *));
static void maintain_interface_smap_column(
        const struct ovsrec_interface *, const struct sset *,
        const struct smap *, const struct smap *,
        void (*fsetkey)(const struct ovsrec_interface *, const char *,
                        const char *),
        void (*fdelkey)(const struct ovsrec_interface *,
                         const char *));

/* Update interface record as represented by 'iface'.
 *
 * 'type' optionally specifies the type of interface, to unset type set to an
 * empty string, to not update type set to NULL.
 *
 * 'external_ids' optionally provide a map of external_ids to update, to not
 * update external_ids set to NULL.
 *
 * 'mnt_external_ids' optionally provide set of 'external_ids' to maintain.
 * When set the function will make sure that all keys in the 'mnt_external_ids'
 * set have values from the 'external_ids' map in the database.  Every key that
 * exists in 'mnt_external_ids' with no corresponding key in 'external_ids'
 * will be removed from the database if present.  Set to NULL to not maintain
 * the record in this way.
 *
 * 'options' optionally provide a map of options to update, to not
 * update options set to NULL.
 *
 * 'mnt_options' optionally provide set of 'options' to maintain.
 * When set the function will make sure that all keys in the 'mnt_options' set
 * have values from the 'options' map in the database.  Every key that exists
 * in 'mnt_options' with no corresponding key in 'options' will be removed from
 * the database if present.  Set to NULL to not maintain the record in this
 * way.
 *
 * 'iface_mtu_request' - if a value > 0 is provided it will be filled into the
 * mtu_request column of the created interface record. */
void
ovsport_update_iface(const struct ovsrec_interface *iface,
                     const char *type,
                     const struct smap *external_ids,
                     const struct sset *mnt_external_ids,
                     const struct smap *options,
                     const struct sset *mnt_options,
                     const int64_t mtu_request)
{
    if (type && strcmp(iface->type, type)) {
        ovsrec_interface_verify_type(iface);
        ovsrec_interface_set_type(iface, type);
    }

    if (external_ids && mnt_external_ids) {
        ovsrec_interface_verify_external_ids(iface);
        maintain_interface_smap_column(
                iface, mnt_external_ids, external_ids, &iface->external_ids,
                ovsrec_interface_update_external_ids_setkey,
                ovsrec_interface_update_external_ids_delkey);
    } else if (external_ids) {
        ovsrec_interface_verify_external_ids(iface);
        update_interface_smap_column(
                iface, external_ids, &iface->external_ids,
                ovsrec_interface_update_external_ids_setkey);
    }

    if (options && mnt_options) {
        ovsrec_interface_verify_options(iface);
        maintain_interface_smap_column(
                iface, mnt_options, options, &iface->options,
                ovsrec_interface_update_options_setkey,
                ovsrec_interface_update_options_delkey);
    } else if (options) {
        ovsrec_interface_verify_options(iface);
        update_interface_smap_column(
                iface, options, &iface->options,
                ovsrec_interface_update_options_setkey);
    }

    if (mtu_request > 0) {
        if ((iface->mtu_request && *iface->mtu_request != mtu_request)
            || !iface->mtu_request)
        {
            ovsrec_interface_verify_mtu_request(iface);
            ovsrec_interface_set_mtu_request(
                    iface, &mtu_request, mtu_request > 0);
        }
    } else if (iface->mtu_request) {
        ovsrec_interface_verify_mtu_request(iface);
        ovsrec_interface_update_mtu_request_delvalue(iface,
                                                     *iface->mtu_request);
    }
}

const struct ovsrec_port *
ovsport_lookup_by_interfaces(
        struct ovsdb_idl_index *ovsrec_port_by_interfaces,
        struct ovsrec_interface **interfaces,
        const size_t n_interfaces)
{
    struct ovsrec_port *port = ovsrec_port_index_init_row(
            ovsrec_port_by_interfaces);
    ovsrec_port_index_set_interfaces(port, interfaces, n_interfaces);

    const struct ovsrec_port *retval = ovsrec_port_index_find(
            ovsrec_port_by_interfaces, port);

    ovsrec_port_index_destroy_row(port);

    return retval;
}

const struct
ovsrec_port * ovsport_lookup_by_interface(
        struct ovsdb_idl_index *ovsrec_port_by_interfaces,
        struct ovsrec_interface *interface)
{
    struct ovsrec_interface *interfaces[] = {interface};

    return ovsport_lookup_by_interfaces(ovsrec_port_by_interfaces,
                                        interfaces, 1);
}

const struct ovsrec_port *
ovsport_lookup_by_qos(struct ovsdb_idl_index *ovsrec_port_by_qos,
                      const struct ovsrec_qos *qos)
{
    const struct ovsrec_port *port =
        ovsrec_port_index_init_row(ovsrec_port_by_qos);
    ovsrec_port_index_set_qos(port, qos);

    const struct ovsrec_port *retval =
        ovsrec_port_index_find(ovsrec_port_by_qos, port);

    ovsrec_port_index_destroy_row(port);

    return retval;
}

/* Update an interface map column with the key/value pairs present in the
 * provided smap, only applying changes when necessary. */
static void
update_interface_smap_column(
        const struct ovsrec_interface *iface,
        const struct smap *smap,
        const struct smap *db_smap,
        void (*fsetkey)(const struct ovsrec_interface *,
                         const char *, const char *))
{
    struct smap_node *node;

    SMAP_FOR_EACH (node, smap) {
        const char *db_value = smap_get(db_smap, node->key);

        if (!db_value || strcmp(db_value, node->value)) {
            fsetkey(iface, node->key, node->value);
        }
    }
}

/* Like update_interface_smap_column, but also takes an sset with all the keys
 * we want to maintain.  Any key present in the sset but not in the provided
 * smap will be removed from the database if present there. */
static void
maintain_interface_smap_column(
        const struct ovsrec_interface *iface,
        const struct sset *mnt_items,
        const struct smap *smap,
        const struct smap *db_smap,
        void (*fsetkey)(const struct ovsrec_interface *,
                         const char *, const char *),
        void (*fdelkey)(const struct ovsrec_interface *,
                         const char *))
{
    const char *ref_name;

    SSET_FOR_EACH (ref_name, mnt_items) {
        const char *value = smap_get(smap, ref_name);
        const char *db_value = smap_get(db_smap, ref_name);
        if (!value && db_value) {
            fdelkey(iface, ref_name);
        } else if (value && (!db_value || strcmp(db_value, value)))
        {
            fsetkey(iface, ref_name, value);
        }
    }
}

uint16_t
get_iface_mtu(const struct ovsrec_interface *iface)
{
    if (!iface || !iface->n_mtu || iface->mtu[0] <= 0) {
        return 0;
    }
    return (uint16_t) iface->mtu[0];
}