d1efca473b10afc561a4dc47c0038b814c2c1cc5
[cascardo/ovs.git] / ovn / controller / binding.c
1 /* Copyright (c) 2015, 2016 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17 #include "binding.h"
18
19 #include "lib/bitmap.h"
20 #include "lib/hmap.h"
21 #include "lib/sset.h"
22 #include "lib/util.h"
23 #include "lib/vswitch-idl.h"
24 #include "openvswitch/vlog.h"
25 #include "ovn/lib/ovn-sb-idl.h"
26 #include "ovn-controller.h"
27
28 VLOG_DEFINE_THIS_MODULE(binding);
29
30 /* A set of the iface-id values of local interfaces on this chassis. */
31 static struct sset local_ids = SSET_INITIALIZER(&local_ids);
32
33 /* When this gets set to true, the next run will re-check all binding records. */
34 static bool process_full_binding = false;
35
36 void
37 binding_reset_processing(void)
38 {
39     process_full_binding = true;
40 }
41
42 void
43 binding_register_ovs_idl(struct ovsdb_idl *ovs_idl)
44 {
45     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_open_vswitch);
46     ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_bridges);
47
48     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_bridge);
49     ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_name);
50     ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_ports);
51
52     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_port);
53     ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_name);
54     ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_interfaces);
55
56     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_interface);
57     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_name);
58     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_external_ids);
59     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_ingress_policing_rate);
60     ovsdb_idl_add_column(ovs_idl,
61                          &ovsrec_interface_col_ingress_policing_burst);
62 }
63
64 static bool
65 get_local_iface_ids(const struct ovsrec_bridge *br_int,
66                     struct shash *lport_to_iface)
67 {
68     int i;
69     bool changed = false;
70
71     struct sset old_local_ids = SSET_INITIALIZER(&old_local_ids);
72     sset_clone(&old_local_ids, &local_ids);
73
74     for (i = 0; i < br_int->n_ports; i++) {
75         const struct ovsrec_port *port_rec = br_int->ports[i];
76         const char *iface_id;
77         int j;
78
79         if (!strcmp(port_rec->name, br_int->name)) {
80             continue;
81         }
82
83         for (j = 0; j < port_rec->n_interfaces; j++) {
84             const struct ovsrec_interface *iface_rec;
85
86             iface_rec = port_rec->interfaces[j];
87             iface_id = smap_get(&iface_rec->external_ids, "iface-id");
88             if (!iface_id) {
89                 continue;
90             }
91             shash_add(lport_to_iface, iface_id, iface_rec);
92             if (!sset_find_and_delete(&old_local_ids, iface_id)) {
93                 sset_add(&local_ids, iface_id);
94                 changed = true;
95             }
96         }
97     }
98
99     /* Any item left in old_local_ids is an ID for an interface
100      * that has been removed. */
101     if (!changed && !sset_is_empty(&old_local_ids)) {
102         changed = true;
103     }
104
105     sset_destroy(&old_local_ids);
106
107     return changed;
108 }
109
110 /* Contains "struct local_datpath" nodes whose hash values are the
111  * row uuids of datapaths with at least one local port binding. */
112 static struct hmap local_datapaths_by_uuid =
113     HMAP_INITIALIZER(&local_datapaths_by_uuid);
114
115 static struct local_datapath *
116 local_datapath_lookup_by_uuid(struct hmap *hmap_p, const struct uuid *uuid)
117 {
118     struct local_datapath *ld;
119     HMAP_FOR_EACH_WITH_HASH(ld, uuid_hmap_node, uuid_hash(uuid), hmap_p) {
120         if (uuid_equals(&ld->uuid, uuid)) {
121             return ld;
122         }
123     }
124     return NULL;
125 }
126
127 static void
128 remove_local_datapath(struct hmap *local_datapaths, struct local_datapath *ld)
129 {
130     if (ld->logical_port) {
131         free(ld->logical_port);
132         ld->logical_port = NULL;
133     }
134     hmap_remove(local_datapaths, &ld->hmap_node);
135     hmap_remove(&local_datapaths_by_uuid, &ld->uuid_hmap_node);
136     free(ld);
137 }
138
139 static void
140 remove_local_datapath_by_binding(struct hmap *local_datapaths,
141                                  const struct sbrec_port_binding *binding_rec)
142 {
143     const struct uuid *uuid = &binding_rec->header_.uuid;
144     struct local_datapath *ld = local_datapath_lookup_by_uuid(local_datapaths,
145                                                               uuid);
146     if (ld) {
147         remove_local_datapath(local_datapaths, ld);
148     } else {
149         struct local_datapath *ld;
150         HMAP_FOR_EACH (ld, hmap_node, local_datapaths) {
151             if (ld->localnet_port == binding_rec) {
152                 ld->localnet_port = NULL;
153             }
154         }
155     }
156 }
157
158 static void
159 add_local_datapath(struct hmap *local_datapaths,
160         const struct sbrec_port_binding *binding_rec,
161         const struct uuid *uuid)
162 {
163     if (get_local_datapath(local_datapaths,
164                            binding_rec->datapath->tunnel_key)) {
165         return;
166     }
167
168     struct local_datapath *ld = xzalloc(sizeof *ld);
169     ld->logical_port = xstrdup(binding_rec->logical_port);
170     memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
171     hmap_insert(local_datapaths, &ld->hmap_node,
172                 binding_rec->datapath->tunnel_key);
173     hmap_insert(&local_datapaths_by_uuid, &ld->uuid_hmap_node,
174                 uuid_hash(uuid));
175 }
176
177 static void
178 update_qos(const struct ovsrec_interface *iface_rec,
179            const struct sbrec_port_binding *pb)
180 {
181     int rate = smap_get_int(&pb->options, "policing_rate", 0);
182     int burst = smap_get_int(&pb->options, "policing_burst", 0);
183
184     ovsrec_interface_set_ingress_policing_rate(iface_rec, MAX(0, rate));
185     ovsrec_interface_set_ingress_policing_burst(iface_rec, MAX(0, burst));
186 }
187
188 static void
189 consider_local_datapath(struct controller_ctx *ctx,
190                         const struct sbrec_chassis *chassis_rec,
191                         const struct sbrec_port_binding *binding_rec,
192                         struct hmap *local_datapaths,
193                         struct shash *lport_to_iface)
194 {
195     const struct ovsrec_interface *iface_rec
196         = shash_find_data(lport_to_iface, binding_rec->logical_port);
197
198     if (iface_rec
199         || (binding_rec->parent_port && binding_rec->parent_port[0] &&
200             sset_contains(&local_ids, binding_rec->parent_port))) {
201         add_local_datapath(local_datapaths, binding_rec,
202                            &binding_rec->header_.uuid);
203         if (iface_rec && ctx->ovs_idl_txn) {
204             update_qos(iface_rec, binding_rec);
205         }
206         if (binding_rec->chassis == chassis_rec) {
207             return;
208         }
209         if (ctx->ovnsb_idl_txn) {
210             if (binding_rec->chassis) {
211                 VLOG_INFO("Changing chassis for lport %s from %s to %s.",
212                           binding_rec->logical_port,
213                           binding_rec->chassis->name,
214                           chassis_rec->name);
215             } else {
216                 VLOG_INFO("Claiming lport %s for this chassis.",
217                           binding_rec->logical_port);
218             }
219             sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
220         }
221     } else if (!strcmp(binding_rec->type, "l2gateway")) {
222         const char *chassis_id = smap_get(&binding_rec->options,
223                                           "l2gateway-chassis");
224         if (!chassis_id || strcmp(chassis_id, chassis_rec->name)) {
225             if (binding_rec->chassis == chassis_rec && ctx->ovnsb_idl_txn) {
226                 VLOG_INFO("Releasing l2gateway port %s from this chassis.",
227                           binding_rec->logical_port);
228                 sbrec_port_binding_set_chassis(binding_rec, NULL);
229             }
230             return;
231         }
232
233         if (binding_rec->chassis == chassis_rec) {
234             return;
235         }
236
237         if (!strcmp(chassis_id, chassis_rec->name) && ctx->ovnsb_idl_txn) {
238             VLOG_INFO("Claiming l2gateway port %s for this chassis.",
239                       binding_rec->logical_port);
240             sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
241             add_local_datapath(local_datapaths, binding_rec,
242                                &binding_rec->header_.uuid);
243         }
244     } else if (chassis_rec && binding_rec->chassis == chassis_rec
245                && strcmp(binding_rec->type, "gateway")) {
246         if (ctx->ovnsb_idl_txn) {
247             VLOG_INFO("Releasing lport %s from this chassis.",
248                       binding_rec->logical_port);
249             sbrec_port_binding_set_chassis(binding_rec, NULL);
250         }
251     }
252 }
253
254 void
255 binding_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
256             const char *chassis_id, struct hmap *local_datapaths)
257 {
258     const struct sbrec_chassis *chassis_rec;
259     const struct sbrec_port_binding *binding_rec;
260     struct shash lport_to_iface = SHASH_INITIALIZER(&lport_to_iface);
261
262     chassis_rec = get_chassis(ctx->ovnsb_idl, chassis_id);
263     if (!chassis_rec) {
264         return;
265     }
266
267     if (br_int) {
268         if (ctx->ovnsb_idl_txn && get_local_iface_ids(br_int, &lport_to_iface)) {
269             process_full_binding = true;
270         }
271     } else {
272         /* We have no integration bridge, therefore no local logical ports.
273          * We'll remove our chassis from all port binding records below. */
274         process_full_binding = true;
275     }
276
277     /* Run through each binding record to see if it is resident on this
278      * chassis and update the binding accordingly.  This includes both
279      * directly connected logical ports and children of those ports. */
280     if (process_full_binding) {
281         struct hmap keep_local_datapath_by_uuid =
282             HMAP_INITIALIZER(&keep_local_datapath_by_uuid);
283         SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
284             consider_local_datapath(ctx, chassis_rec, binding_rec,
285                                     local_datapaths, &lport_to_iface);
286             struct local_datapath *ld = xzalloc(sizeof *ld);
287             memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
288             hmap_insert(&keep_local_datapath_by_uuid, &ld->uuid_hmap_node,
289                         uuid_hash(&ld->uuid));
290         }
291         struct local_datapath *old_ld, *next;
292         HMAP_FOR_EACH_SAFE (old_ld, next, hmap_node, local_datapaths) {
293             if (!local_datapath_lookup_by_uuid(&keep_local_datapath_by_uuid,
294                                                &old_ld->uuid)) {
295                 remove_local_datapath(local_datapaths, old_ld);
296             }
297         }
298         hmap_destroy(&keep_local_datapath_by_uuid);
299         process_full_binding = false;
300     } else {
301         SBREC_PORT_BINDING_FOR_EACH_TRACKED(binding_rec, ctx->ovnsb_idl) {
302             if (sbrec_port_binding_is_deleted(binding_rec)) {
303                 remove_local_datapath_by_binding(local_datapaths, binding_rec);
304             } else {
305                 consider_local_datapath(ctx, chassis_rec, binding_rec,
306                                         local_datapaths, &lport_to_iface);
307             }
308         }
309     }
310
311     shash_destroy(&lport_to_iface);
312 }
313
314 /* Returns true if the database is all cleaned up, false if more work is
315  * required. */
316 bool
317 binding_cleanup(struct controller_ctx *ctx, const char *chassis_id)
318 {
319     if (!ctx->ovnsb_idl_txn) {
320         return false;
321     }
322
323     if (!chassis_id) {
324         return true;
325     }
326
327     const struct sbrec_chassis *chassis_rec
328         = get_chassis(ctx->ovnsb_idl, chassis_id);
329     if (!chassis_rec) {
330         return true;
331     }
332
333     ovsdb_idl_txn_add_comment(
334         ctx->ovnsb_idl_txn,
335         "ovn-controller: removing all port bindings for '%s'", chassis_id);
336
337     const struct sbrec_port_binding *binding_rec;
338     bool any_changes = false;
339     SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
340         if (binding_rec->chassis == chassis_rec) {
341             sbrec_port_binding_set_chassis(binding_rec, NULL);
342             any_changes = true;
343         }
344     }
345     return !any_changes;
346 }