bc6df32f6f8e1fb65ec9d1e8e34691d447b458f4
[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/poll-loop.h"
22 #include "lib/sset.h"
23 #include "lib/util.h"
24 #include "lib/vswitch-idl.h"
25 #include "openvswitch/vlog.h"
26 #include "ovn/lib/ovn-sb-idl.h"
27 #include "ovn-controller.h"
28
29 VLOG_DEFINE_THIS_MODULE(binding);
30
31 /* A set of the iface-id values of local interfaces on this chassis. */
32 static struct sset local_ids = SSET_INITIALIZER(&local_ids);
33
34 /* When this gets set to true, the next run will re-check all binding records. */
35 static bool process_full_binding = false;
36
37 void
38 binding_reset_processing(void)
39 {
40     process_full_binding = true;
41 }
42
43 void
44 binding_register_ovs_idl(struct ovsdb_idl *ovs_idl)
45 {
46     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_open_vswitch);
47     ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_bridges);
48
49     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_bridge);
50     ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_name);
51     ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_ports);
52
53     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_port);
54     ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_name);
55     ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_interfaces);
56
57     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_interface);
58     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_name);
59     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_external_ids);
60     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_ingress_policing_rate);
61     ovsdb_idl_add_column(ovs_idl,
62                          &ovsrec_interface_col_ingress_policing_burst);
63 }
64
65 static bool
66 get_local_iface_ids(const struct ovsrec_bridge *br_int,
67                     struct shash *lport_to_iface)
68 {
69     int i;
70     bool changed = false;
71
72     struct sset old_local_ids = SSET_INITIALIZER(&old_local_ids);
73     sset_clone(&old_local_ids, &local_ids);
74
75     for (i = 0; i < br_int->n_ports; i++) {
76         const struct ovsrec_port *port_rec = br_int->ports[i];
77         const char *iface_id;
78         int j;
79
80         if (!strcmp(port_rec->name, br_int->name)) {
81             continue;
82         }
83
84         for (j = 0; j < port_rec->n_interfaces; j++) {
85             const struct ovsrec_interface *iface_rec;
86
87             iface_rec = port_rec->interfaces[j];
88             iface_id = smap_get(&iface_rec->external_ids, "iface-id");
89             if (!iface_id) {
90                 continue;
91             }
92             shash_add(lport_to_iface, iface_id, iface_rec);
93             if (!sset_find_and_delete(&old_local_ids, iface_id)) {
94                 sset_add(&local_ids, iface_id);
95                 changed = true;
96             }
97         }
98     }
99
100     /* Any item left in old_local_ids is an ID for an interface
101      * that has been removed. */
102     if (!changed && !sset_is_empty(&old_local_ids)) {
103         changed = true;
104     }
105
106     sset_destroy(&old_local_ids);
107
108     return changed;
109 }
110
111 static struct local_datapath *
112 local_datapath_lookup_by_uuid(struct hmap *hmap_p, const struct uuid *uuid)
113 {
114     struct local_datapath *ld;
115     HMAP_FOR_EACH_WITH_HASH(ld, uuid_hmap_node, uuid_hash(uuid), hmap_p) {
116         if (uuid_equals(&ld->uuid, uuid)) {
117             return ld;
118         }
119     }
120     return NULL;
121 }
122
123 static void
124 remove_local_datapath(struct hmap *local_datapaths, struct local_datapath *ld)
125 {
126     if (ld->logical_port) {
127         free(ld->logical_port);
128         ld->logical_port = NULL;
129     }
130     hmap_remove(local_datapaths, &ld->hmap_node);
131     free(ld);
132 }
133
134 static void
135 add_local_datapath(struct hmap *local_datapaths,
136         const struct sbrec_port_binding *binding_rec)
137 {
138     if (get_local_datapath(local_datapaths,
139                            binding_rec->datapath->tunnel_key)) {
140         return;
141     }
142
143     struct local_datapath *ld = xzalloc(sizeof *ld);
144     ld->logical_port = xstrdup(binding_rec->logical_port);
145     memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
146     hmap_insert(local_datapaths, &ld->hmap_node,
147                 binding_rec->datapath->tunnel_key);
148 }
149
150 static void
151 update_qos(const struct ovsrec_interface *iface_rec,
152            const struct sbrec_port_binding *pb)
153 {
154     int rate = smap_get_int(&pb->options, "policing_rate", 0);
155     int burst = smap_get_int(&pb->options, "policing_burst", 0);
156
157     ovsrec_interface_set_ingress_policing_rate(iface_rec, MAX(0, rate));
158     ovsrec_interface_set_ingress_policing_burst(iface_rec, MAX(0, burst));
159 }
160
161 static void
162 consider_local_datapath(struct controller_ctx *ctx,
163                         const struct sbrec_chassis *chassis_rec,
164                         const struct sbrec_port_binding *binding_rec,
165                         struct hmap *local_datapaths,
166                         struct shash *lport_to_iface)
167 {
168     const struct ovsrec_interface *iface_rec
169         = shash_find_data(lport_to_iface, binding_rec->logical_port);
170
171     if (iface_rec
172         || (binding_rec->parent_port && binding_rec->parent_port[0] &&
173             sset_contains(&local_ids, binding_rec->parent_port))) {
174         add_local_datapath(local_datapaths, binding_rec);
175         if (iface_rec && ctx->ovs_idl_txn) {
176             update_qos(iface_rec, binding_rec);
177         }
178         if (binding_rec->chassis == chassis_rec) {
179             return;
180         }
181         if (ctx->ovnsb_idl_txn) {
182             if (binding_rec->chassis) {
183                 VLOG_INFO("Changing chassis for lport %s from %s to %s.",
184                           binding_rec->logical_port,
185                           binding_rec->chassis->name,
186                           chassis_rec->name);
187             } else {
188                 VLOG_INFO("Claiming lport %s for this chassis.",
189                           binding_rec->logical_port);
190             }
191             sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
192         }
193     } else if (!strcmp(binding_rec->type, "l2gateway")) {
194         const char *chassis_id = smap_get(&binding_rec->options,
195                                           "l2gateway-chassis");
196         if (!chassis_id || strcmp(chassis_id, chassis_rec->name)) {
197             if (binding_rec->chassis == chassis_rec && ctx->ovnsb_idl_txn) {
198                 VLOG_INFO("Releasing l2gateway port %s from this chassis.",
199                           binding_rec->logical_port);
200                 sbrec_port_binding_set_chassis(binding_rec, NULL);
201             }
202             return;
203         }
204
205         if (binding_rec->chassis == chassis_rec) {
206             return;
207         }
208
209         if (!strcmp(chassis_id, chassis_rec->name) && ctx->ovnsb_idl_txn) {
210             VLOG_INFO("Claiming l2gateway port %s for this chassis.",
211                       binding_rec->logical_port);
212             sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
213             add_local_datapath(local_datapaths, binding_rec);
214         }
215     } else if (chassis_rec && binding_rec->chassis == chassis_rec
216                && strcmp(binding_rec->type, "gateway")) {
217         if (ctx->ovnsb_idl_txn) {
218             VLOG_INFO("Releasing lport %s from this chassis.",
219                       binding_rec->logical_port);
220             sbrec_port_binding_set_chassis(binding_rec, NULL);
221         }
222     }
223 }
224
225 void
226 binding_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
227             const char *chassis_id, struct hmap *local_datapaths)
228 {
229     const struct sbrec_chassis *chassis_rec;
230     const struct sbrec_port_binding *binding_rec;
231     struct shash lport_to_iface = SHASH_INITIALIZER(&lport_to_iface);
232
233     chassis_rec = get_chassis(ctx->ovnsb_idl, chassis_id);
234     if (!chassis_rec) {
235         return;
236     }
237
238     if (br_int) {
239         if (ctx->ovnsb_idl_txn && get_local_iface_ids(br_int, &lport_to_iface)) {
240             process_full_binding = true;
241         }
242     } else {
243         /* We have no integration bridge, therefore no local logical ports.
244          * We'll remove our chassis from all port binding records below. */
245         process_full_binding = true;
246     }
247
248     /* Run through each binding record to see if it is resident on this
249      * chassis and update the binding accordingly.  This includes both
250      * directly connected logical ports and children of those ports. */
251     if (process_full_binding) {
252         struct hmap keep_local_datapath_by_uuid =
253             HMAP_INITIALIZER(&keep_local_datapath_by_uuid);
254         SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
255             consider_local_datapath(ctx, chassis_rec, binding_rec,
256                                     local_datapaths, &lport_to_iface);
257             struct local_datapath *ld = xzalloc(sizeof *ld);
258             memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
259             hmap_insert(&keep_local_datapath_by_uuid, &ld->uuid_hmap_node,
260                         uuid_hash(&ld->uuid));
261         }
262         struct local_datapath *old_ld, *next;
263         HMAP_FOR_EACH_SAFE (old_ld, next, hmap_node, local_datapaths) {
264             if (!local_datapath_lookup_by_uuid(&keep_local_datapath_by_uuid,
265                                                &old_ld->uuid)) {
266                 remove_local_datapath(local_datapaths, old_ld);
267             }
268         }
269         hmap_destroy(&keep_local_datapath_by_uuid);
270         process_full_binding = false;
271     } else {
272         SBREC_PORT_BINDING_FOR_EACH_TRACKED(binding_rec, ctx->ovnsb_idl) {
273             if (sbrec_port_binding_is_deleted(binding_rec)) {
274                 /* If a port binding was bound to this chassis and removed before
275                  * the ovs interface was removed, we'll catch that here and trigger
276                  * a full bindings refresh.  This is to see if we need to clear
277                  * an entry out of local_datapaths. */
278                 if (binding_rec->chassis == chassis_rec) {
279                     process_full_binding = true;
280                     poll_immediate_wake();
281                 }
282             } else {
283                 consider_local_datapath(ctx, chassis_rec, binding_rec,
284                                         local_datapaths, &lport_to_iface);
285             }
286         }
287     }
288
289     shash_destroy(&lport_to_iface);
290 }
291
292 /* Returns true if the database is all cleaned up, false if more work is
293  * required. */
294 bool
295 binding_cleanup(struct controller_ctx *ctx, const char *chassis_id)
296 {
297     if (!ctx->ovnsb_idl_txn) {
298         return false;
299     }
300
301     if (!chassis_id) {
302         return true;
303     }
304
305     const struct sbrec_chassis *chassis_rec
306         = get_chassis(ctx->ovnsb_idl, chassis_id);
307     if (!chassis_rec) {
308         return true;
309     }
310
311     ovsdb_idl_txn_add_comment(
312         ctx->ovnsb_idl_txn,
313         "ovn-controller: removing all port bindings for '%s'", chassis_id);
314
315     const struct sbrec_port_binding *binding_rec;
316     bool any_changes = false;
317     SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
318         if (binding_rec->chassis == chassis_rec) {
319             sbrec_port_binding_set_chassis(binding_rec, NULL);
320             any_changes = true;
321         }
322     }
323     return !any_changes;
324 }