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 /* Contains "struct local_datpath" nodes whose hash values are the
111 * row uuids of datapaths with at least one local port binding. */
112 static struct hmap local_datapaths_by_uuid =
113 HMAP_INITIALIZER(&local_datapaths_by_uuid);
115 static struct local_datapath *
116 local_datapath_lookup_by_uuid(struct hmap *hmap_p, const struct uuid *uuid)
118 struct local_datapath *ld;
119 HMAP_FOR_EACH_WITH_HASH(ld, uuid_hmap_node, uuid_hash(uuid), hmap_p) {
120 if (uuid_equals(&ld->uuid, uuid)) {
128 remove_local_datapath(struct hmap *local_datapaths, struct local_datapath *ld)
130 if (ld->logical_port) {
131 free(ld->logical_port);
132 ld->logical_port = NULL;
134 hmap_remove(local_datapaths, &ld->hmap_node);
135 hmap_remove(&local_datapaths_by_uuid, &ld->uuid_hmap_node);
140 remove_local_datapath_by_binding(struct hmap *local_datapaths,
141 const struct sbrec_port_binding *binding_rec)
143 const struct uuid *uuid = &binding_rec->header_.uuid;
144 struct local_datapath *ld = local_datapath_lookup_by_uuid(local_datapaths,
147 remove_local_datapath(local_datapaths, ld);
149 struct local_datapath *ld;
150 HMAP_FOR_EACH (ld, hmap_node, local_datapaths) {
151 if (ld->localnet_port == binding_rec) {
152 ld->localnet_port = NULL;
159 add_local_datapath(struct hmap *local_datapaths,
160 const struct sbrec_port_binding *binding_rec,
161 const struct uuid *uuid)
163 if (get_local_datapath(local_datapaths,
164 binding_rec->datapath->tunnel_key)) {
168 struct local_datapath *ld = xzalloc(sizeof *ld);
169 ld->logical_port = xstrdup(binding_rec->logical_port);
170 memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
171 hmap_insert(local_datapaths, &ld->hmap_node,
172 binding_rec->datapath->tunnel_key);
173 hmap_insert(&local_datapaths_by_uuid, &ld->uuid_hmap_node,
178 update_qos(const struct ovsrec_interface *iface_rec,
179 const struct sbrec_port_binding *pb)
181 int rate = smap_get_int(&pb->options, "policing_rate", 0);
182 int burst = smap_get_int(&pb->options, "policing_burst", 0);
184 ovsrec_interface_set_ingress_policing_rate(iface_rec, MAX(0, rate));
185 ovsrec_interface_set_ingress_policing_burst(iface_rec, MAX(0, burst));
189 consider_local_datapath(struct controller_ctx *ctx,
190 const struct sbrec_chassis *chassis_rec,
191 const struct sbrec_port_binding *binding_rec,
192 struct hmap *local_datapaths,
193 struct shash *lport_to_iface)
195 const struct ovsrec_interface *iface_rec
196 = shash_find_data(lport_to_iface, binding_rec->logical_port);
199 || (binding_rec->parent_port && binding_rec->parent_port[0] &&
200 sset_contains(&local_ids, binding_rec->parent_port))) {
201 add_local_datapath(local_datapaths, binding_rec,
202 &binding_rec->header_.uuid);
203 if (iface_rec && ctx->ovs_idl_txn) {
204 update_qos(iface_rec, binding_rec);
206 if (binding_rec->chassis == chassis_rec) {
209 if (ctx->ovnsb_idl_txn) {
210 if (binding_rec->chassis) {
211 VLOG_INFO("Changing chassis for lport %s from %s to %s.",
212 binding_rec->logical_port,
213 binding_rec->chassis->name,
216 VLOG_INFO("Claiming lport %s for this chassis.",
217 binding_rec->logical_port);
219 sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
221 } else if (!strcmp(binding_rec->type, "l2gateway")) {
222 const char *chassis_id = smap_get(&binding_rec->options,
223 "l2gateway-chassis");
224 if (!chassis_id || strcmp(chassis_id, chassis_rec->name)) {
225 if (binding_rec->chassis == chassis_rec && ctx->ovnsb_idl_txn) {
226 VLOG_INFO("Releasing l2gateway port %s from this chassis.",
227 binding_rec->logical_port);
228 sbrec_port_binding_set_chassis(binding_rec, NULL);
233 if (binding_rec->chassis == chassis_rec) {
237 if (!strcmp(chassis_id, chassis_rec->name) && ctx->ovnsb_idl_txn) {
238 VLOG_INFO("Claiming l2gateway port %s for this chassis.",
239 binding_rec->logical_port);
240 sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
241 add_local_datapath(local_datapaths, binding_rec,
242 &binding_rec->header_.uuid);
244 } else if (chassis_rec && binding_rec->chassis == chassis_rec
245 && strcmp(binding_rec->type, "gateway")) {
246 if (ctx->ovnsb_idl_txn) {
247 VLOG_INFO("Releasing lport %s from this chassis.",
248 binding_rec->logical_port);
249 sbrec_port_binding_set_chassis(binding_rec, NULL);
255 binding_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
256 const char *chassis_id, struct hmap *local_datapaths)
258 const struct sbrec_chassis *chassis_rec;
259 const struct sbrec_port_binding *binding_rec;
260 struct shash lport_to_iface = SHASH_INITIALIZER(&lport_to_iface);
262 chassis_rec = get_chassis(ctx->ovnsb_idl, chassis_id);
268 if (ctx->ovnsb_idl_txn && get_local_iface_ids(br_int, &lport_to_iface)) {
269 process_full_binding = true;
272 /* We have no integration bridge, therefore no local logical ports.
273 * We'll remove our chassis from all port binding records below. */
274 process_full_binding = true;
277 /* Run through each binding record to see if it is resident on this
278 * chassis and update the binding accordingly. This includes both
279 * directly connected logical ports and children of those ports. */
280 if (process_full_binding) {
281 struct hmap keep_local_datapath_by_uuid =
282 HMAP_INITIALIZER(&keep_local_datapath_by_uuid);
283 SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
284 consider_local_datapath(ctx, chassis_rec, binding_rec,
285 local_datapaths, &lport_to_iface);
286 struct local_datapath *ld = xzalloc(sizeof *ld);
287 memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
288 hmap_insert(&keep_local_datapath_by_uuid, &ld->uuid_hmap_node,
289 uuid_hash(&ld->uuid));
291 struct local_datapath *old_ld, *next;
292 HMAP_FOR_EACH_SAFE (old_ld, next, hmap_node, local_datapaths) {
293 if (!local_datapath_lookup_by_uuid(&keep_local_datapath_by_uuid,
295 remove_local_datapath(local_datapaths, old_ld);
298 hmap_destroy(&keep_local_datapath_by_uuid);
299 process_full_binding = false;
301 SBREC_PORT_BINDING_FOR_EACH_TRACKED(binding_rec, ctx->ovnsb_idl) {
302 if (sbrec_port_binding_is_deleted(binding_rec)) {
303 remove_local_datapath_by_binding(local_datapaths, binding_rec);
305 consider_local_datapath(ctx, chassis_rec, binding_rec,
306 local_datapaths, &lport_to_iface);
311 shash_destroy(&lport_to_iface);
314 /* Returns true if the database is all cleaned up, false if more work is
317 binding_cleanup(struct controller_ctx *ctx, const char *chassis_id)
319 if (!ctx->ovnsb_idl_txn) {
327 const struct sbrec_chassis *chassis_rec
328 = get_chassis(ctx->ovnsb_idl, chassis_id);
333 ovsdb_idl_txn_add_comment(
335 "ovn-controller: removing all port bindings for '%s'", chassis_id);
337 const struct sbrec_port_binding *binding_rec;
338 bool any_changes = false;
339 SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
340 if (binding_rec->chassis == chassis_rec) {
341 sbrec_port_binding_set_chassis(binding_rec, NULL);