dpif-netlink: add GENEVE creation support
[cascardo/ovs.git] / lib / ovsdb-map-op.c
1 /* Copyright (C) 2016 Hewlett Packard Enterprise Development LP
2  * All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16
17 #include <config.h>
18 #include "ovsdb-map-op.h"
19 #include "util.h"
20 #include "hmap.h"
21 #include "hash.h"
22
23 /* Map Operation: a Partial Map Update */
24 struct map_op {
25     struct hmap_node node;
26     struct ovsdb_datum *datum;
27     enum map_op_type type;
28 };
29
30 /* List of Map Operations */
31 struct map_op_list {
32     struct hmap hmap;
33 };
34
35 static void map_op_destroy_datum(struct map_op *, const struct ovsdb_type *);
36 static struct map_op *map_op_list_find(struct map_op_list *, struct map_op *,
37                                        const struct ovsdb_type *, size_t);
38
39 struct map_op*
40 map_op_create(struct ovsdb_datum *datum, enum map_op_type type)
41 {
42     struct map_op *map_op = xmalloc(sizeof *map_op);
43     map_op->node.hash = 0;
44     map_op->node.next = HMAP_NODE_NULL;
45     map_op->datum = datum;
46     map_op->type = type;
47     return map_op;
48 }
49
50 static void
51 map_op_destroy_datum(struct map_op *map_op, const struct ovsdb_type *type)
52 {
53     if (map_op->type == MAP_OP_DELETE){
54         struct ovsdb_type type_ = *type;
55         type_.value.type = OVSDB_TYPE_VOID;
56         ovsdb_datum_destroy(map_op->datum, &type_);
57     } else {
58         ovsdb_datum_destroy(map_op->datum, type);
59     }
60     map_op->datum = NULL;
61 }
62
63 void
64 map_op_destroy(struct map_op *map_op, const struct ovsdb_type *type)
65 {
66     map_op_destroy_datum(map_op, type);
67     free(map_op);
68 }
69
70 struct ovsdb_datum*
71 map_op_datum(const struct map_op *map_op)
72 {
73     return map_op->datum;
74 }
75
76 enum map_op_type
77 map_op_type(const struct map_op *map_op)
78 {
79     return map_op->type;
80 }
81
82 struct map_op_list*
83 map_op_list_create(void)
84 {
85     struct map_op_list *list = xmalloc(sizeof *list);
86     hmap_init(&list->hmap);
87     return list;
88 }
89
90 void
91 map_op_list_destroy(struct map_op_list *list, const struct ovsdb_type *type)
92 {
93     struct map_op *map_op, *next;
94     HMAP_FOR_EACH_SAFE (map_op, next, node, &list->hmap) {
95         map_op_destroy(map_op, type);
96     }
97     hmap_destroy(&list->hmap);
98     free(list);
99 }
100
101 static struct map_op*
102 map_op_list_find(struct map_op_list *list, struct map_op *map_op,
103                  const struct ovsdb_type *type, size_t hash)
104 {
105     struct map_op *found = NULL;
106     struct map_op *old;
107     HMAP_FOR_EACH_WITH_HASH(old, node, hash, &list->hmap) {
108         if (ovsdb_atom_equals(&old->datum->keys[0], &map_op->datum->keys[0],
109                               type->key.type)) {
110             found = old;
111             break;
112         }
113     }
114     return found;
115 }
116
117 /* Inserts 'map_op' into 'list'. Makes sure that any conflict with a previous
118  * map operation is resolved, so only one map operation is possible on each key
119  * per transactions. 'type' must be the type of the column over which the map
120  * operation will be applied. */
121 void
122 map_op_list_add(struct map_op_list *list, struct map_op *map_op,
123                 const struct ovsdb_type *type)
124 {
125     /* Check if there is a previous update with the same key. */
126     size_t hash;
127     struct map_op *prev_map_op;
128
129     hash = ovsdb_atom_hash(&map_op->datum->keys[0], type->key.type, 0);
130     prev_map_op = map_op_list_find(list, map_op, type, hash);
131     if (prev_map_op == NULL){
132         hmap_insert(&list->hmap, &map_op->node, hash);
133     } else {
134         if (prev_map_op->type == MAP_OP_INSERT &&
135             map_op->type == MAP_OP_DELETE) {
136             /* These operations cancel each other out. */
137             hmap_remove(&list->hmap, &prev_map_op->node);
138             map_op_destroy(prev_map_op, type);
139             map_op_destroy(map_op, type);
140         } else {
141             /* For any other case, the new update operation replaces
142              * the previous update operation. */
143             map_op_destroy_datum(prev_map_op, type);
144             prev_map_op->type = map_op->type;
145             prev_map_op->datum = map_op->datum;
146             free(map_op);
147         }
148     }
149 }
150
151 struct map_op*
152 map_op_list_first(struct map_op_list *list)
153 {
154     struct hmap_node *node = hmap_first(&list->hmap);
155     if (node == NULL) {
156         return NULL;
157     }
158     struct map_op *map_op = CONTAINER_OF(node, struct map_op, node);
159     return map_op;
160 }
161
162 struct map_op*
163 map_op_list_next(struct map_op_list *list, struct map_op *map_op)
164 {
165     struct hmap_node *node = hmap_next(&list->hmap, &map_op->node);
166     if (node == NULL) {
167         return NULL;
168     }
169     struct map_op *next = CONTAINER_OF(node, struct map_op, node);
170     return next;
171 }