adf41274dfd2b93368a99cf93ce0d95eee69f477
[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 static struct local_datapath *
111 local_datapath_lookup_by_uuid(struct hmap *hmap_p, const struct uuid *uuid)
112 {
113     struct local_datapath *ld;
114     HMAP_FOR_EACH_WITH_HASH(ld, uuid_hmap_node, uuid_hash(uuid), hmap_p) {
115         if (uuid_equals(&ld->uuid, uuid)) {
116             return ld;
117         }
118     }
119     return NULL;
120 }
121
122 static void
123 remove_local_datapath(struct hmap *local_datapaths, struct local_datapath *ld)
124 {
125     if (ld->logical_port) {
126         free(ld->logical_port);
127         ld->logical_port = NULL;
128     }
129     hmap_remove(local_datapaths, &ld->hmap_node);
130     free(ld);
131 }
132
133 static void
134 remove_local_datapath_by_binding(struct hmap *local_datapaths,
135                                  const struct sbrec_port_binding *binding_rec)
136 {
137     const struct uuid *uuid = &binding_rec->header_.uuid;
138     struct local_datapath *ld = local_datapath_lookup_by_uuid(local_datapaths,
139                                                               uuid);
140     if (ld) {
141         remove_local_datapath(local_datapaths, ld);
142     } else {
143         struct local_datapath *ld;
144         HMAP_FOR_EACH (ld, hmap_node, local_datapaths) {
145             if (ld->localnet_port == binding_rec) {
146                 ld->localnet_port = NULL;
147             }
148         }
149     }
150 }
151
152 static void
153 add_local_datapath(struct hmap *local_datapaths,
154         const struct sbrec_port_binding *binding_rec)
155 {
156     if (get_local_datapath(local_datapaths,
157                            binding_rec->datapath->tunnel_key)) {
158         return;
159     }
160
161     struct local_datapath *ld = xzalloc(sizeof *ld);
162     ld->logical_port = xstrdup(binding_rec->logical_port);
163     memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
164     hmap_insert(local_datapaths, &ld->hmap_node,
165                 binding_rec->datapath->tunnel_key);
166 }
167
168 static void
169 update_qos(const struct ovsrec_interface *iface_rec,
170            const struct sbrec_port_binding *pb)
171 {
172     int rate = smap_get_int(&pb->options, "policing_rate", 0);
173     int burst = smap_get_int(&pb->options, "policing_burst", 0);
174
175     ovsrec_interface_set_ingress_policing_rate(iface_rec, MAX(0, rate));
176     ovsrec_interface_set_ingress_policing_burst(iface_rec, MAX(0, burst));
177 }
178
179 static void
180 consider_local_datapath(struct controller_ctx *ctx,
181                         const struct sbrec_chassis *chassis_rec,
182                         const struct sbrec_port_binding *binding_rec,
183                         struct hmap *local_datapaths,
184                         struct shash *lport_to_iface)
185 {
186     const struct ovsrec_interface *iface_rec
187         = shash_find_data(lport_to_iface, binding_rec->logical_port);
188
189     if (iface_rec
190         || (binding_rec->parent_port && binding_rec->parent_port[0] &&
191             sset_contains(&local_ids, binding_rec->parent_port))) {
192         add_local_datapath(local_datapaths, binding_rec);
193         if (iface_rec && ctx->ovs_idl_txn) {
194             update_qos(iface_rec, binding_rec);
195         }
196         if (binding_rec->chassis == chassis_rec) {
197             return;
198         }
199         if (ctx->ovnsb_idl_txn) {
200             if (binding_rec->chassis) {
201                 VLOG_INFO("Changing chassis for lport %s from %s to %s.",
202                           binding_rec->logical_port,
203                           binding_rec->chassis->name,
204                           chassis_rec->name);
205             } else {
206                 VLOG_INFO("Claiming lport %s for this chassis.",
207                           binding_rec->logical_port);
208             }
209             sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
210         }
211     } else if (!strcmp(binding_rec->type, "l2gateway")) {
212         const char *chassis_id = smap_get(&binding_rec->options,
213                                           "l2gateway-chassis");
214         if (!chassis_id || strcmp(chassis_id, chassis_rec->name)) {
215             if (binding_rec->chassis == chassis_rec && ctx->ovnsb_idl_txn) {
216                 VLOG_INFO("Releasing l2gateway port %s from this chassis.",
217                           binding_rec->logical_port);
218                 sbrec_port_binding_set_chassis(binding_rec, NULL);
219             }
220             return;
221         }
222
223         if (binding_rec->chassis == chassis_rec) {
224             return;
225         }
226
227         if (!strcmp(chassis_id, chassis_rec->name) && ctx->ovnsb_idl_txn) {
228             VLOG_INFO("Claiming l2gateway port %s for this chassis.",
229                       binding_rec->logical_port);
230             sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
231             add_local_datapath(local_datapaths, binding_rec);
232         }
233     } else if (chassis_rec && binding_rec->chassis == chassis_rec
234                && strcmp(binding_rec->type, "gateway")) {
235         if (ctx->ovnsb_idl_txn) {
236             VLOG_INFO("Releasing lport %s from this chassis.",
237                       binding_rec->logical_port);
238             sbrec_port_binding_set_chassis(binding_rec, NULL);
239         }
240     }
241 }
242
243 void
244 binding_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
245             const char *chassis_id, struct hmap *local_datapaths)
246 {
247     const struct sbrec_chassis *chassis_rec;
248     const struct sbrec_port_binding *binding_rec;
249     struct shash lport_to_iface = SHASH_INITIALIZER(&lport_to_iface);
250
251     chassis_rec = get_chassis(ctx->ovnsb_idl, chassis_id);
252     if (!chassis_rec) {
253         return;
254     }
255
256     if (br_int) {
257         if (ctx->ovnsb_idl_txn && get_local_iface_ids(br_int, &lport_to_iface)) {
258             process_full_binding = true;
259         }
260     } else {
261         /* We have no integration bridge, therefore no local logical ports.
262          * We'll remove our chassis from all port binding records below. */
263         process_full_binding = true;
264     }
265
266     /* Run through each binding record to see if it is resident on this
267      * chassis and update the binding accordingly.  This includes both
268      * directly connected logical ports and children of those ports. */
269     if (process_full_binding) {
270         struct hmap keep_local_datapath_by_uuid =
271             HMAP_INITIALIZER(&keep_local_datapath_by_uuid);
272         SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
273             consider_local_datapath(ctx, chassis_rec, binding_rec,
274                                     local_datapaths, &lport_to_iface);
275             struct local_datapath *ld = xzalloc(sizeof *ld);
276             memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
277             hmap_insert(&keep_local_datapath_by_uuid, &ld->uuid_hmap_node,
278                         uuid_hash(&ld->uuid));
279         }
280         struct local_datapath *old_ld, *next;
281         HMAP_FOR_EACH_SAFE (old_ld, next, hmap_node, local_datapaths) {
282             if (!local_datapath_lookup_by_uuid(&keep_local_datapath_by_uuid,
283                                                &old_ld->uuid)) {
284                 remove_local_datapath(local_datapaths, old_ld);
285             }
286         }
287         hmap_destroy(&keep_local_datapath_by_uuid);
288         process_full_binding = false;
289     } else {
290         SBREC_PORT_BINDING_FOR_EACH_TRACKED(binding_rec, ctx->ovnsb_idl) {
291             if (sbrec_port_binding_is_deleted(binding_rec)) {
292                 remove_local_datapath_by_binding(local_datapaths, binding_rec);
293             } else {
294                 consider_local_datapath(ctx, chassis_rec, binding_rec,
295                                         local_datapaths, &lport_to_iface);
296             }
297         }
298     }
299
300     shash_destroy(&lport_to_iface);
301 }
302
303 /* Returns true if the database is all cleaned up, false if more work is
304  * required. */
305 bool
306 binding_cleanup(struct controller_ctx *ctx, const char *chassis_id)
307 {
308     if (!ctx->ovnsb_idl_txn) {
309         return false;
310     }
311
312     if (!chassis_id) {
313         return true;
314     }
315
316     const struct sbrec_chassis *chassis_rec
317         = get_chassis(ctx->ovnsb_idl, chassis_id);
318     if (!chassis_rec) {
319         return true;
320     }
321
322     ovsdb_idl_txn_add_comment(
323         ctx->ovnsb_idl_txn,
324         "ovn-controller: removing all port bindings for '%s'", chassis_id);
325
326     const struct sbrec_port_binding *binding_rec;
327     bool any_changes = false;
328     SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
329         if (binding_rec->chassis == chassis_rec) {
330             sbrec_port_binding_set_chassis(binding_rec, NULL);
331             any_changes = true;
332         }
333     }
334     return !any_changes;
335 }