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