1 /* Copyright (c) 2015, 2016 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "lib/bitmap.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"
28 VLOG_DEFINE_THIS_MODULE(binding);
30 /* A set of the iface-id values of local interfaces on this chassis. */
31 static struct sset local_ids = SSET_INITIALIZER(&local_ids);
33 /* When this gets set to true, the next run will re-check all binding records. */
34 static bool process_full_binding = false;
37 binding_reset_processing(void)
39 process_full_binding = true;
43 binding_register_ovs_idl(struct ovsdb_idl *ovs_idl)
45 ovsdb_idl_add_table(ovs_idl, &ovsrec_table_open_vswitch);
46 ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_bridges);
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);
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);
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);
65 get_local_iface_ids(const struct ovsrec_bridge *br_int,
66 struct shash *lport_to_iface)
71 struct sset old_local_ids = SSET_INITIALIZER(&old_local_ids);
72 sset_clone(&old_local_ids, &local_ids);
74 for (i = 0; i < br_int->n_ports; i++) {
75 const struct ovsrec_port *port_rec = br_int->ports[i];
79 if (!strcmp(port_rec->name, br_int->name)) {
83 for (j = 0; j < port_rec->n_interfaces; j++) {
84 const struct ovsrec_interface *iface_rec;
86 iface_rec = port_rec->interfaces[j];
87 iface_id = smap_get(&iface_rec->external_ids, "iface-id");
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);
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)) {
105 sset_destroy(&old_local_ids);
110 static struct local_datapath *
111 local_datapath_lookup_by_uuid(struct hmap *hmap_p, const struct uuid *uuid)
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)) {
123 remove_local_datapath(struct hmap *local_datapaths, struct local_datapath *ld)
125 if (ld->logical_port) {
126 free(ld->logical_port);
127 ld->logical_port = NULL;
129 hmap_remove(local_datapaths, &ld->hmap_node);
134 remove_local_datapath_by_binding(struct hmap *local_datapaths,
135 const struct sbrec_port_binding *binding_rec)
137 const struct uuid *uuid = &binding_rec->header_.uuid;
138 struct local_datapath *ld = local_datapath_lookup_by_uuid(local_datapaths,
141 remove_local_datapath(local_datapaths, ld);
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;
153 add_local_datapath(struct hmap *local_datapaths,
154 const struct sbrec_port_binding *binding_rec)
156 if (get_local_datapath(local_datapaths,
157 binding_rec->datapath->tunnel_key)) {
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);
169 update_qos(const struct ovsrec_interface *iface_rec,
170 const struct sbrec_port_binding *pb)
172 int rate = smap_get_int(&pb->options, "policing_rate", 0);
173 int burst = smap_get_int(&pb->options, "policing_burst", 0);
175 ovsrec_interface_set_ingress_policing_rate(iface_rec, MAX(0, rate));
176 ovsrec_interface_set_ingress_policing_burst(iface_rec, MAX(0, burst));
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)
186 const struct ovsrec_interface *iface_rec
187 = shash_find_data(lport_to_iface, binding_rec->logical_port);
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);
196 if (binding_rec->chassis == chassis_rec) {
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,
206 VLOG_INFO("Claiming lport %s for this chassis.",
207 binding_rec->logical_port);
209 sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
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);
223 if (binding_rec->chassis == chassis_rec) {
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);
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);
244 binding_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
245 const char *chassis_id, struct hmap *local_datapaths)
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);
251 chassis_rec = get_chassis(ctx->ovnsb_idl, chassis_id);
257 if (ctx->ovnsb_idl_txn && get_local_iface_ids(br_int, &lport_to_iface)) {
258 process_full_binding = true;
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;
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));
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,
284 remove_local_datapath(local_datapaths, old_ld);
287 hmap_destroy(&keep_local_datapath_by_uuid);
288 process_full_binding = false;
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);
294 consider_local_datapath(ctx, chassis_rec, binding_rec,
295 local_datapaths, &lport_to_iface);
300 shash_destroy(&lport_to_iface);
303 /* Returns true if the database is all cleaned up, false if more work is
306 binding_cleanup(struct controller_ctx *ctx, const char *chassis_id)
308 if (!ctx->ovnsb_idl_txn) {
316 const struct sbrec_chassis *chassis_rec
317 = get_chassis(ctx->ovnsb_idl, chassis_id);
322 ovsdb_idl_txn_add_comment(
324 "ovn-controller: removing all port bindings for '%s'", chassis_id);
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);