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"
21 #include "lib/poll-loop.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"
29 VLOG_DEFINE_THIS_MODULE(binding);
31 /* A set of the iface-id values of local interfaces on this chassis. */
32 static struct sset local_ids = SSET_INITIALIZER(&local_ids);
34 /* When this gets set to true, the next run will re-check all binding records. */
35 static bool process_full_binding = false;
38 binding_reset_processing(void)
40 process_full_binding = true;
44 binding_register_ovs_idl(struct ovsdb_idl *ovs_idl)
46 ovsdb_idl_add_table(ovs_idl, &ovsrec_table_open_vswitch);
47 ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_bridges);
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);
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);
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);
66 get_local_iface_ids(const struct ovsrec_bridge *br_int,
67 struct shash *lport_to_iface)
72 struct sset old_local_ids = SSET_INITIALIZER(&old_local_ids);
73 sset_clone(&old_local_ids, &local_ids);
75 for (i = 0; i < br_int->n_ports; i++) {
76 const struct ovsrec_port *port_rec = br_int->ports[i];
80 if (!strcmp(port_rec->name, br_int->name)) {
84 for (j = 0; j < port_rec->n_interfaces; j++) {
85 const struct ovsrec_interface *iface_rec;
87 iface_rec = port_rec->interfaces[j];
88 iface_id = smap_get(&iface_rec->external_ids, "iface-id");
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);
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)) {
106 sset_destroy(&old_local_ids);
111 static struct local_datapath *
112 local_datapath_lookup_by_uuid(struct hmap *hmap_p, const struct uuid *uuid)
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)) {
124 remove_local_datapath(struct hmap *local_datapaths, struct local_datapath *ld)
126 if (ld->logical_port) {
127 free(ld->logical_port);
128 ld->logical_port = NULL;
130 hmap_remove(local_datapaths, &ld->hmap_node);
135 add_local_datapath(struct hmap *local_datapaths,
136 const struct sbrec_port_binding *binding_rec)
138 if (get_local_datapath(local_datapaths,
139 binding_rec->datapath->tunnel_key)) {
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);
151 update_qos(const struct ovsrec_interface *iface_rec,
152 const struct sbrec_port_binding *pb)
154 int rate = smap_get_int(&pb->options, "policing_rate", 0);
155 int burst = smap_get_int(&pb->options, "policing_burst", 0);
157 ovsrec_interface_set_ingress_policing_rate(iface_rec, MAX(0, rate));
158 ovsrec_interface_set_ingress_policing_burst(iface_rec, MAX(0, burst));
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)
168 const struct ovsrec_interface *iface_rec
169 = shash_find_data(lport_to_iface, binding_rec->logical_port);
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);
178 if (binding_rec->chassis == chassis_rec) {
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,
188 VLOG_INFO("Claiming lport %s for this chassis.",
189 binding_rec->logical_port);
191 sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
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);
205 if (binding_rec->chassis == chassis_rec) {
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);
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);
226 binding_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
227 const char *chassis_id, struct hmap *local_datapaths)
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);
233 chassis_rec = get_chassis(ctx->ovnsb_idl, chassis_id);
239 if (ctx->ovnsb_idl_txn && get_local_iface_ids(br_int, &lport_to_iface)) {
240 process_full_binding = true;
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;
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));
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,
266 remove_local_datapath(local_datapaths, old_ld);
269 hmap_destroy(&keep_local_datapath_by_uuid);
270 process_full_binding = false;
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();
283 consider_local_datapath(ctx, chassis_rec, binding_rec,
284 local_datapaths, &lport_to_iface);
289 shash_destroy(&lport_to_iface);
292 /* Returns true if the database is all cleaned up, false if more work is
295 binding_cleanup(struct controller_ctx *ctx, const char *chassis_id)
297 if (!ctx->ovnsb_idl_txn) {
305 const struct sbrec_chassis *chassis_rec
306 = get_chassis(ctx->ovnsb_idl, chassis_id);
311 ovsdb_idl_txn_add_comment(
313 "ovn-controller: removing all port bindings for '%s'", chassis_id);
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);