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.
21 #include "lib/bitmap.h"
22 #include "lib/poll-loop.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"
31 VLOG_DEFINE_THIS_MODULE(binding);
33 /* A set of the iface-id values of local interfaces on this chassis. */
34 static struct sset local_ids = SSET_INITIALIZER(&local_ids);
36 /* When this gets set to true, the next run will re-check all binding records. */
37 static bool process_full_binding = false;
40 binding_reset_processing(void)
42 process_full_binding = true;
46 binding_register_ovs_idl(struct ovsdb_idl *ovs_idl)
48 ovsdb_idl_add_table(ovs_idl, &ovsrec_table_open_vswitch);
49 ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_bridges);
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);
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);
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);
68 get_local_iface_ids(const struct ovsrec_bridge *br_int,
69 struct shash *lport_to_iface)
74 struct sset old_local_ids = SSET_INITIALIZER(&old_local_ids);
75 sset_clone(&old_local_ids, &local_ids);
77 for (i = 0; i < br_int->n_ports; i++) {
78 const struct ovsrec_port *port_rec = br_int->ports[i];
82 if (!strcmp(port_rec->name, br_int->name)) {
86 for (j = 0; j < port_rec->n_interfaces; j++) {
87 const struct ovsrec_interface *iface_rec;
89 iface_rec = port_rec->interfaces[j];
90 iface_id = smap_get(&iface_rec->external_ids, "iface-id");
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);
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)) {
108 sset_destroy(&old_local_ids);
113 static struct local_datapath *
114 local_datapath_lookup_by_uuid(struct hmap *hmap_p, const struct uuid *uuid)
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)) {
126 remove_local_datapath(struct hmap *local_datapaths, struct local_datapath *ld)
128 if (ld->logical_port) {
129 free(ld->logical_port);
130 ld->logical_port = NULL;
132 hmap_remove(local_datapaths, &ld->hmap_node);
134 lflow_reset_processing();
138 add_local_datapath(struct hmap *local_datapaths,
139 const struct sbrec_port_binding *binding_rec)
141 if (get_local_datapath(local_datapaths,
142 binding_rec->datapath->tunnel_key)) {
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);
152 mcgroup_index_reset();
153 lflow_reset_processing();
157 update_qos(const struct ovsrec_interface *iface_rec,
158 const struct sbrec_port_binding *pb)
160 int rate = smap_get_int(&pb->options, "policing_rate", 0);
161 int burst = smap_get_int(&pb->options, "policing_burst", 0);
163 ovsrec_interface_set_ingress_policing_rate(iface_rec, MAX(0, rate));
164 ovsrec_interface_set_ingress_policing_burst(iface_rec, MAX(0, burst));
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)
174 const struct ovsrec_interface *iface_rec
175 = shash_find_data(lport_to_iface, binding_rec->logical_port);
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);
184 if (binding_rec->chassis == chassis_rec) {
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,
194 VLOG_INFO("Claiming lport %s for this chassis.",
195 binding_rec->logical_port);
197 sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
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);
211 if (binding_rec->chassis == chassis_rec) {
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);
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);
232 binding_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
233 const char *chassis_id, struct hmap *local_datapaths)
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);
239 chassis_rec = get_chassis(ctx->ovnsb_idl, chassis_id);
245 if (ctx->ovnsb_idl_txn && get_local_iface_ids(br_int, &lport_to_iface)) {
246 process_full_binding = true;
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;
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));
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,
272 remove_local_datapath(local_datapaths, old_ld);
275 hmap_destroy(&keep_local_datapath_by_uuid);
276 process_full_binding = false;
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();
289 consider_local_datapath(ctx, chassis_rec, binding_rec,
290 local_datapaths, &lport_to_iface);
295 shash_destroy(&lport_to_iface);
298 /* Returns true if the database is all cleaned up, false if more work is
301 binding_cleanup(struct controller_ctx *ctx, const char *chassis_id)
303 if (!ctx->ovnsb_idl_txn) {
311 const struct sbrec_chassis *chassis_rec
312 = get_chassis(ctx->ovnsb_idl, chassis_id);
317 ovsdb_idl_txn_add_comment(
319 "ovn-controller: removing all port bindings for '%s'", chassis_id);
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);