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