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