File: objdep.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 (257 lines) | stat: -rw-r--r-- 8,843 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
/* Copyright (c) 2015, 2016, 2017 Nicira, Inc.
 * Copyright (c) 2022, 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 "lib/objdep.h"
#include "lib/hash.h"
#include "lib/util.h"
#include "openvswitch/vlog.h"

VLOG_DEFINE_THIS_MODULE(resource_dep);

static void resource_node_destroy(struct resource_to_objects_node *);

void
objdep_mgr_init(struct objdep_mgr *mgr)
{
    hmap_init(&mgr->resource_to_objects_table);
    hmap_init(&mgr->object_to_resources_table);
}

void
objdep_mgr_destroy(struct objdep_mgr *mgr)
{
    objdep_mgr_clear(mgr);

    hmap_destroy(&mgr->resource_to_objects_table);
    hmap_destroy(&mgr->object_to_resources_table);
}

void
objdep_mgr_clear(struct objdep_mgr *mgr)
{
    struct resource_to_objects_node *resource_node;
    HMAP_FOR_EACH_SAFE (resource_node, node, &mgr->resource_to_objects_table) {
        struct object_to_resources_list_node *object_list_node;
        HMAP_FOR_EACH_SAFE (object_list_node, hmap_node,
                            &resource_node->objs) {
            ovs_list_remove(&object_list_node->list_node);
            hmap_remove(&resource_node->objs, &object_list_node->hmap_node);
            free(object_list_node);
        }
        hmap_remove(&mgr->resource_to_objects_table, &resource_node->node);
        resource_node_destroy(resource_node);
    }

    struct object_to_resources_node *object_node;
    HMAP_FOR_EACH_SAFE (object_node, node, &mgr->object_to_resources_table) {
        hmap_remove(&mgr->object_to_resources_table, &object_node->node);
        free(object_node);
    }
}

void
objdep_mgr_add(struct objdep_mgr *mgr, enum objdep_type type,
               const char *res_name, const struct uuid *obj_uuid)
{
    objdep_mgr_add_with_refcount(mgr, type, res_name, obj_uuid, 0);
}

void
objdep_mgr_add_with_refcount(struct objdep_mgr *mgr, enum objdep_type type,
                             const char *res_name, const struct uuid *obj_uuid,
                             size_t ref_count)
{
    struct resource_to_objects_node *resource_node =
        objdep_mgr_find_objs(mgr, type, res_name);
    struct object_to_resources_node *object_node =
        objdep_mgr_find_resources(mgr, obj_uuid);
    if (resource_node && object_node) {
        /* Check if the mapping already existed before adding a new one. */
        struct object_to_resources_list_node *n;
        HMAP_FOR_EACH_WITH_HASH (n, hmap_node, uuid_hash(obj_uuid),
                                 &resource_node->objs) {
            if (uuid_equals(&n->obj_uuid, obj_uuid)) {
                return;
            }
        }
    }

    /* Create the resource node if we didn't have one already (for a
     * different object). */
    if (!resource_node) {
        resource_node = xzalloc(sizeof *resource_node);
        resource_node->node.hash = hash_string(res_name, type);
        resource_node->type = type;
        resource_node->res_name = xstrdup(res_name);
        hmap_init(&resource_node->objs);
        hmap_insert(&mgr->resource_to_objects_table,
                    &resource_node->node,
                    resource_node->node.hash);
    }

    /* Create the object node if we didn't have one already (for a
     * different resource). */
    if (!object_node) {
        object_node = xzalloc(sizeof *object_node);
        object_node->node.hash = uuid_hash(obj_uuid);
        object_node->obj_uuid = *obj_uuid;
        ovs_list_init(&object_node->resources_head);
        hmap_insert(&mgr->object_to_resources_table,
                    &object_node->node,
                    object_node->node.hash);
    }

    struct object_to_resources_list_node *resource_list_node =
        xzalloc(sizeof *resource_list_node);
    resource_list_node->obj_uuid = *obj_uuid;
    resource_list_node->ref_count = ref_count;
    resource_list_node->resource_node = resource_node;
    hmap_insert(&resource_node->objs, &resource_list_node->hmap_node,
                uuid_hash(obj_uuid));
    ovs_list_push_back(&object_node->resources_head,
                       &resource_list_node->list_node);
}

void
objdep_mgr_remove_obj(struct objdep_mgr *mgr, const struct uuid *obj_uuid)
{
    struct object_to_resources_node *object_node =
        objdep_mgr_find_resources(mgr, obj_uuid);
    if (!object_node) {
        return;
    }

    hmap_remove(&mgr->object_to_resources_table, &object_node->node);

    struct object_to_resources_list_node *resource_list_node;
    LIST_FOR_EACH_SAFE (resource_list_node, list_node,
                        &object_node->resources_head) {
        struct resource_to_objects_node *resource_node =
            resource_list_node->resource_node;
        ovs_list_remove(&resource_list_node->list_node);
        hmap_remove(&resource_node->objs, &resource_list_node->hmap_node);

        /* Clean up the node in ref_obj_table if the resource is not
         * referred by any logical flows. */
        if (hmap_is_empty(&resource_node->objs)) {
            hmap_remove(&mgr->resource_to_objects_table, &resource_node->node);
            resource_node_destroy(resource_list_node->resource_node);
        }

        free(resource_list_node);
    }
    free(object_node);
}

struct resource_to_objects_node *
objdep_mgr_find_objs(struct objdep_mgr *mgr, enum objdep_type type,
                     const char *res_name)
{
    struct resource_to_objects_node *resource_node;

    HMAP_FOR_EACH_WITH_HASH (resource_node, node, hash_string(res_name, type),
                             &mgr->resource_to_objects_table) {
        if (resource_node->type == type &&
                !strcmp(resource_node->res_name, res_name)) {
            return resource_node;
        }
    }
    return NULL;
}

struct object_to_resources_node *
objdep_mgr_find_resources(struct objdep_mgr *mgr,
                          const struct uuid *obj_uuid)
{
    struct object_to_resources_node *object_node;

    HMAP_FOR_EACH_WITH_HASH (object_node, node, uuid_hash(obj_uuid),
                             &mgr->object_to_resources_table) {
        if (uuid_equals(&object_node->obj_uuid, obj_uuid)) {
            return object_node;
        }
    }
    return NULL;
}

bool
objdep_mgr_contains_obj(struct objdep_mgr *mgr, const struct uuid *obj_uuid)
{
    return !!objdep_mgr_find_resources(mgr, obj_uuid);
}

bool
objdep_mgr_handle_change(struct objdep_mgr *mgr,
                         enum objdep_type type,
                         const char *res_name,
                         objdep_change_handler handler,
                         struct uuidset *objs_processed,
                         const void *in_arg, void *out_arg,
                         bool *changed)
{
    struct resource_to_objects_node *resource_node =
        objdep_mgr_find_objs(mgr, type, res_name);
    if (!resource_node) {
        *changed = false;
        return true;
    }
    VLOG_DBG("Handle changed object reference for resource type: %s,"
             " name: %s.", objdep_type_name(type), res_name);
    *changed = false;

    struct uuidset objs_todo = UUIDSET_INITIALIZER(&objs_todo);
    struct object_to_resources_list_node *resource_list_node;
    HMAP_FOR_EACH (resource_list_node, hmap_node, &resource_node->objs) {
        if (uuidset_find(objs_processed, &resource_list_node->obj_uuid)) {
            continue;
        }
        /* Use object_to_resources_list_node as list node to store the uuid.
         * Other fields are not used here. */
        uuidset_insert(&objs_todo, &resource_list_node->obj_uuid);
    }
    if (uuidset_is_empty(&objs_todo)) {
        return true;
    }
    *changed = true;

    /* This takes ownership of objs_todo. */
    return handler(type, res_name, &objs_todo, in_arg, out_arg);
}

const char *
objdep_type_name(enum objdep_type type)
{
    static const char *type_names[OBJDEP_TYPE_MAX] = {
        [OBJDEP_TYPE_ADDRSET] = "Address_Set",
        [OBJDEP_TYPE_PORTGROUP] = "Port_Group",
        [OBJDEP_TYPE_PORTBINDING] = "Port_Binding",
        [OBJDEP_TYPE_MC_GROUP] = "Multicast_Group",
        [OBJDEP_TYPE_TEMPLATE] = "Template",
    };

    ovs_assert(type < OBJDEP_TYPE_MAX);
    return type_names[type];
}

static void
resource_node_destroy(struct resource_to_objects_node *resource_node)
{
    free(resource_node->res_name);
    hmap_destroy(&resource_node->objs);
    free(resource_node);
}