ce9cccfd4be9756cdabf36a0ce2a6fd9c43c1773
[cascardo/ovs.git] / ovn / controller / binding.c
1 /* Copyright (c) 2015 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
19 #include "lib/bitmap.h"
20 #include "lib/hmap.h"
21 #include "lib/sset.h"
22 #include "lib/util.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"
27
28 VLOG_DEFINE_THIS_MODULE(binding);
29
30 void
31 binding_register_ovs_idl(struct ovsdb_idl *ovs_idl)
32 {
33     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_open_vswitch);
34     ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_bridges);
35
36     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_bridge);
37     ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_name);
38     ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_ports);
39
40     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_port);
41     ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_name);
42     ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_interfaces);
43
44     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_interface);
45     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_name);
46     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_external_ids);
47 }
48
49 static void
50 get_local_iface_ids(const struct ovsrec_bridge *br_int, struct sset *lports)
51 {
52     int i;
53
54     for (i = 0; i < br_int->n_ports; i++) {
55         const struct ovsrec_port *port_rec = br_int->ports[i];
56         const char *iface_id;
57         int j;
58
59         if (!strcmp(port_rec->name, br_int->name)) {
60             continue;
61         }
62
63         for (j = 0; j < port_rec->n_interfaces; j++) {
64             const struct ovsrec_interface *iface_rec;
65
66             iface_rec = port_rec->interfaces[j];
67             iface_id = smap_get(&iface_rec->external_ids, "iface-id");
68             if (!iface_id) {
69                 continue;
70             }
71             sset_add(lports, iface_id);
72         }
73     }
74 }
75
76 static void
77 update_ct_zones(struct sset *lports, struct simap *ct_zones,
78                 unsigned long *ct_zone_bitmap)
79 {
80     struct simap_node *ct_zone, *ct_zone_next;
81     const char *iface_id;
82     int scan_start = 1;
83
84     /* xxx This is wasteful to assign a zone to each port--even if no
85      * xxx security policy is applied. */
86
87     /* Delete any zones that are associated with removed ports. */
88     SIMAP_FOR_EACH_SAFE(ct_zone, ct_zone_next, ct_zones) {
89         if (!sset_contains(lports, ct_zone->name)) {
90             bitmap_set0(ct_zone_bitmap, ct_zone->data);
91             simap_delete(ct_zones, ct_zone);
92         }
93     }
94
95     /* Assign a unique zone id for each logical port. */
96     SSET_FOR_EACH(iface_id, lports) {
97         size_t zone;
98
99         if (simap_contains(ct_zones, iface_id)) {
100             continue;
101         }
102
103         /* We assume that there are 64K zones and that we own them all. */
104         zone = bitmap_scan(ct_zone_bitmap, 0, scan_start, MAX_CT_ZONES + 1);
105         if (zone == MAX_CT_ZONES + 1) {
106             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
107             VLOG_WARN_RL(&rl, "exhausted all ct zones");
108             return;
109         }
110         scan_start = zone + 1;
111
112         bitmap_set1(ct_zone_bitmap, zone);
113         simap_put(ct_zones, iface_id, zone);
114
115         /* xxx We should erase any old entries for this
116          * xxx zone, but we need a generic interface to the conntrack
117          * xxx table. */
118     }
119 }
120
121 static void
122 add_local_datapath(struct hmap *local_datapaths,
123         const struct sbrec_port_binding *binding_rec)
124 {
125     struct hmap_node *ld;
126     ld = hmap_first_with_hash(local_datapaths,
127                               binding_rec->datapath->tunnel_key);
128     if (!ld) {
129         ld = xmalloc(sizeof *ld);
130         hmap_insert(local_datapaths, ld,
131                     binding_rec->datapath->tunnel_key);
132     }
133 }
134
135 void
136 binding_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
137             const char *chassis_id, struct simap *ct_zones,
138             unsigned long *ct_zone_bitmap, struct hmap *local_datapaths)
139 {
140     const struct sbrec_chassis *chassis_rec;
141     const struct sbrec_port_binding *binding_rec;
142     struct sset lports, all_lports;
143     const char *name;
144
145     if (!ctx->ovnsb_idl_txn) {
146         return;
147     }
148
149     chassis_rec = get_chassis(ctx->ovnsb_idl, chassis_id);
150     if (!chassis_rec) {
151         return;
152     }
153
154     sset_init(&lports);
155     sset_init(&all_lports);
156     if (br_int) {
157         get_local_iface_ids(br_int, &lports);
158     } else {
159         /* We have no integration bridge, therefore no local logical ports.
160          * We'll remove our chassis from all port binding records below. */
161     }
162     sset_clone(&all_lports, &lports);
163
164     ovsdb_idl_txn_add_comment(
165         ctx->ovnsb_idl_txn,"ovn-controller: updating port bindings for '%s'",
166         chassis_id);
167
168     /* Run through each binding record to see if it is resident on this
169      * chassis and update the binding accordingly.  This includes both
170      * directly connected logical ports and children of those ports. */
171     SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
172         if (sset_find_and_delete(&lports, binding_rec->logical_port) ||
173                 (binding_rec->parent_port && binding_rec->parent_port[0] &&
174                  sset_contains(&all_lports, binding_rec->parent_port))) {
175             if (binding_rec->parent_port && binding_rec->parent_port[0]) {
176                 /* Add child logical port to the set of all local ports. */
177                 sset_add(&all_lports, binding_rec->logical_port);
178             }
179             add_local_datapath(local_datapaths, binding_rec);
180             if (binding_rec->chassis == chassis_rec) {
181                 continue;
182             }
183             if (binding_rec->chassis) {
184                 VLOG_INFO("Changing chassis for lport %s from %s to %s",
185                           binding_rec->logical_port,
186                           binding_rec->chassis->name,
187                           chassis_rec->name);
188             }
189             sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
190         } else if (binding_rec->chassis == chassis_rec) {
191             sbrec_port_binding_set_chassis(binding_rec, NULL);
192         } else if (!binding_rec->chassis
193                    && !strcmp(binding_rec->type, "localnet")) {
194             /* localnet ports will never be bound to a chassis, but we want
195              * to list them in all_lports because we want to allocate
196              * a conntrack zone ID for each one, as we'll be creating
197              * a patch port for each one. */
198             sset_add(&all_lports, binding_rec->logical_port);
199         }
200     }
201
202     SSET_FOR_EACH (name, &lports) {
203         VLOG_DBG("No port binding record for lport %s", name);
204     }
205
206     update_ct_zones(&all_lports, ct_zones, ct_zone_bitmap);
207
208     sset_destroy(&lports);
209     sset_destroy(&all_lports);
210 }
211
212 /* Returns true if the database is all cleaned up, false if more work is
213  * required. */
214 bool
215 binding_cleanup(struct controller_ctx *ctx, const char *chassis_id)
216 {
217     if (!ctx->ovnsb_idl_txn) {
218         return false;
219     }
220
221     if (!chassis_id) {
222         return true;
223     }
224
225     const struct sbrec_chassis *chassis_rec
226         = get_chassis(ctx->ovnsb_idl, chassis_id);
227     if (!chassis_rec) {
228         return true;
229     }
230
231     ovsdb_idl_txn_add_comment(
232         ctx->ovnsb_idl_txn,
233         "ovn-controller: removing all port bindings for '%s'", chassis_id);
234
235     const struct sbrec_port_binding *binding_rec;
236     bool any_changes = false;
237     SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
238         if (binding_rec->chassis == chassis_rec) {
239             sbrec_port_binding_set_chassis(binding_rec, NULL);
240             any_changes = true;
241         }
242     }
243     return !any_changes;
244 }