ovn-controller: race between binding-run and patch-run for localnet ports
[cascardo/ovs.git] / ovn / controller / patch.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
18 #include "patch.h"
19
20 #include "hash.h"
21 #include "lib/hmap.h"
22 #include "lib/vswitch-idl.h"
23 #include "openvswitch/vlog.h"
24 #include "ovn-controller.h"
25
26 VLOG_DEFINE_THIS_MODULE(patch);
27
28 static char *
29 patch_port_name(const char *src, const char *dst)
30 {
31     return xasprintf("patch-%s-to-%s", src, dst);
32 }
33
34 /* Return true if 'port' is a patch port with the specified 'peer'. */
35 static bool
36 match_patch_port(const struct ovsrec_port *port, const char *peer)
37 {
38     for (size_t i = 0; i < port->n_interfaces; i++) {
39         struct ovsrec_interface *iface = port->interfaces[i];
40         if (strcmp(iface->type, "patch")) {
41             continue;
42         }
43         const char *iface_peer = smap_get(&iface->options, "peer");
44         if (peer && !strcmp(iface_peer, peer)) {
45             return true;
46         }
47     }
48     return false;
49 }
50
51 /* Creates a patch port in bridge 'src' named 'src_name', whose peer is
52  * 'dst_name' in bridge 'dst'.  Initializes the patch port's external-ids:'key'
53  * to 'key'.
54  *
55  * If such a patch port already exists, removes it from 'existing_ports'. */
56 static void
57 create_patch_port(struct controller_ctx *ctx,
58                   const char *key, const char *value,
59                   const struct ovsrec_bridge *src, const char *src_name,
60                   const struct ovsrec_bridge *dst, const char *dst_name,
61                   struct shash *existing_ports)
62 {
63     for (size_t i = 0; i < src->n_ports; i++) {
64         if (match_patch_port(src->ports[i], dst_name)) {
65             /* Patch port already exists on 'src'. */
66             shash_find_and_delete(existing_ports, src->ports[i]->name);
67             return;
68         }
69     }
70
71     ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
72             "ovn-controller: creating patch port '%s' from '%s' to '%s'",
73             src_name, src->name, dst->name);
74
75     struct ovsrec_interface *iface;
76     iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
77     ovsrec_interface_set_name(iface, src_name);
78     ovsrec_interface_set_type(iface, "patch");
79     const struct smap options = SMAP_CONST1(&options, "peer", dst_name);
80     ovsrec_interface_set_options(iface, &options);
81
82     struct ovsrec_port *port;
83     port = ovsrec_port_insert(ctx->ovs_idl_txn);
84     ovsrec_port_set_name(port, src_name);
85     ovsrec_port_set_interfaces(port, &iface, 1);
86     const struct smap ids = SMAP_CONST1(&ids, key, value);
87     ovsrec_port_set_external_ids(port, &ids);
88
89     struct ovsrec_port **ports;
90     ports = xmalloc(sizeof *ports * (src->n_ports + 1));
91     memcpy(ports, src->ports, sizeof *ports * src->n_ports);
92     ports[src->n_ports] = port;
93     ovsrec_bridge_verify_ports(src);
94     ovsrec_bridge_set_ports(src, ports, src->n_ports + 1);
95
96     free(ports);
97 }
98
99 static void
100 remove_port(struct controller_ctx *ctx,
101             const struct ovsrec_port *port)
102 {
103     const struct ovsrec_bridge *bridge;
104
105     /* We know the port we want to delete, but we have to find the bridge its
106      * on to do so.  Note this only runs on a config change that should be
107      * pretty rare. */
108     OVSREC_BRIDGE_FOR_EACH (bridge, ctx->ovs_idl) {
109         size_t i;
110         for (i = 0; i < bridge->n_ports; i++) {
111             if (bridge->ports[i] != port) {
112                 continue;
113             }
114             struct ovsrec_port **new_ports;
115             new_ports = xmemdup(bridge->ports,
116                     sizeof *new_ports * (bridge->n_ports - 1));
117             if (i != bridge->n_ports - 1) {
118                 /* Removed port was not last */
119                 new_ports[i] = bridge->ports[bridge->n_ports - 1];
120             }
121             ovsrec_bridge_verify_ports(bridge);
122             ovsrec_bridge_set_ports(bridge, new_ports, bridge->n_ports - 1);
123             free(new_ports);
124             ovsrec_port_delete(port);
125             return;
126         }
127     }
128 }
129
130 /* Obtains external-ids:ovn-bridge-mappings from OVSDB and adds patch ports for
131  * the local bridge mappings.  Removes any patch ports for bridge mappings that
132  * already existed from 'existing_ports'. */
133 static void
134 add_bridge_mappings(struct controller_ctx *ctx,
135                     const struct ovsrec_bridge *br_int,
136                     struct shash *existing_ports,
137                     struct hmap *local_datapaths)
138 {
139     /* Get ovn-bridge-mappings. */
140     const char *mappings_cfg = "";
141     const struct ovsrec_open_vswitch *cfg;
142     cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
143     if (cfg) {
144         mappings_cfg = smap_get(&cfg->external_ids, "ovn-bridge-mappings");
145         if (!mappings_cfg) {
146             mappings_cfg = "";
147         }
148     }
149
150     /* Parse bridge mappings. */
151     struct shash bridge_mappings = SHASH_INITIALIZER(&bridge_mappings);
152     char *cur, *next, *start;
153     next = start = xstrdup(mappings_cfg);
154     while ((cur = strsep(&next, ",")) && *cur) {
155         char *network, *bridge = cur;
156         const struct ovsrec_bridge *ovs_bridge;
157
158         network = strsep(&bridge, ":");
159         if (!bridge || !*network || !*bridge) {
160             VLOG_ERR("Invalid ovn-bridge-mappings configuration: '%s'",
161                     mappings_cfg);
162             break;
163         }
164
165         ovs_bridge = get_bridge(ctx->ovs_idl, bridge);
166         if (!ovs_bridge) {
167             VLOG_WARN("Bridge '%s' not found for network '%s'",
168                     bridge, network);
169             continue;
170         }
171
172         shash_add(&bridge_mappings, network, ovs_bridge);
173     }
174     free(start);
175
176     const struct sbrec_port_binding *binding;
177     SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
178         if (strcmp(binding->type, "localnet")) {
179             /* Not a binding for a localnet port. */
180             continue;
181         }
182
183         struct local_datapath *ld;
184         ld = CONTAINER_OF(hmap_first_with_hash(local_datapaths,
185                           binding->datapath->tunnel_key),
186                           struct local_datapath, hmap_node);
187         if (!ld) {
188             /* This localnet port is on a datapath with no
189              * logical ports bound to this chassis, so there's no need
190              * to create patch ports for it. */
191             continue;
192         }
193         if (ld->localnet_port) {
194             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
195             VLOG_WARN_RL(&rl, "localnet port '%s' already set for datapath "
196                          "'%"PRId64"', skipping the new port '%s'.",
197                          ld->localnet_port->logical_port,
198                          binding->datapath->tunnel_key,
199                          binding->logical_port);
200             continue;
201         }
202         ld->localnet_port = binding;
203
204         const char *network = smap_get(&binding->options, "network_name");
205         if (!network) {
206             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
207             VLOG_ERR_RL(&rl, "localnet port '%s' has no network name.",
208                          binding->logical_port);
209             continue;
210         }
211         struct ovsrec_bridge *br_ln = shash_find_data(&bridge_mappings, network);
212         if (!br_ln) {
213             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
214             VLOG_ERR_RL(&rl, "bridge not found for localnet port '%s' "
215                     "with network name '%s'", binding->logical_port, network);
216             continue;
217         }
218
219         char *name1 = patch_port_name(br_int->name, binding->logical_port);
220         char *name2 = patch_port_name(binding->logical_port, br_int->name);
221         create_patch_port(ctx, "ovn-localnet-port", binding->logical_port,
222                           br_int, name1, br_ln, name2, existing_ports);
223         create_patch_port(ctx, "ovn-localnet-port", binding->logical_port,
224                           br_ln, name2, br_int, name1, existing_ports);
225         free(name1);
226         free(name2);
227     }
228
229     shash_destroy(&bridge_mappings);
230 }
231
232 /* Add one OVS patch port for each OVN logical patch port.
233  *
234  * This is suboptimal for several reasons.  First, it creates an OVS port for
235  * every OVN logical patch port, not just for the ones that are actually useful
236  * on this hypervisor.  Second, it's wasteful to create an OVS patch port per
237  * OVN logical patch port, when really there's no benefit to them beyond a way
238  * to identify how a packet ingressed into a logical datapath.
239  *
240  * There are two obvious ways to improve the situation here, by modifying
241  * OVS:
242  *
243  *     1. Add a way to configure in OVS which fields are preserved on a hop
244  *        across an OVS patch port.  If MFF_LOG_DATAPATH and MFF_LOG_INPORT
245  *        were preserved, then only a single pair of OVS patch ports would be
246  *        required regardless of the number of OVN logical patch ports.
247  *
248  *     2. Add a new OpenFlow extension action modeled on "resubmit" that also
249  *        saves and restores the packet data and metadata (the inability to do
250  *        this is the only reason that "resubmit" can't be used already).  Or
251  *        add OpenFlow extension actions to otherwise save and restore packet
252  *        data and metadata.
253  */
254 static void
255 add_logical_patch_ports(struct controller_ctx *ctx,
256                         const struct ovsrec_bridge *br_int,
257                         struct shash *existing_ports)
258 {
259     const struct sbrec_port_binding *binding;
260     SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
261         if (!strcmp(binding->type, "patch")) {
262             const char *local = binding->logical_port;
263             const char *peer = smap_get(&binding->options, "peer");
264             if (!peer) {
265                 continue;
266             }
267
268             char *src_name = patch_port_name(local, peer);
269             char *dst_name = patch_port_name(peer, local);
270             create_patch_port(ctx, "ovn-logical-patch-port", local,
271                               br_int, src_name, br_int, dst_name,
272                               existing_ports);
273             free(dst_name);
274             free(src_name);
275         }
276     }
277 }
278
279 void
280 patch_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
281           struct hmap *local_datapaths)
282 {
283     if (!ctx->ovs_idl_txn || !ctx->ovnsb_idl_txn) {
284         return;
285     }
286
287     /* Figure out what patch ports already exist. */
288     struct shash existing_ports = SHASH_INITIALIZER(&existing_ports);
289     const struct ovsrec_port *port;
290     OVSREC_PORT_FOR_EACH (port, ctx->ovs_idl) {
291         if (smap_get(&port->external_ids, "ovn-localnet-port") ||
292             smap_get(&port->external_ids, "ovn-logical-patch-port")) {
293             shash_add(&existing_ports, port->name, port);
294         }
295     }
296
297     /* Create in the database any patch ports that should exist.  Remove from
298      * 'existing_ports' any patch ports that do exist in the database and
299      * should be there. */
300     add_bridge_mappings(ctx, br_int, &existing_ports, local_datapaths);
301     add_logical_patch_ports(ctx, br_int, &existing_ports);
302
303     /* Now 'existing_ports' only still contains patch ports that exist in the
304      * database but shouldn't.  Delete them from the database. */
305     struct shash_node *port_node, *port_next_node;
306     SHASH_FOR_EACH_SAFE (port_node, port_next_node, &existing_ports) {
307         struct ovsrec_port *port = port_node->data;
308         shash_delete(&existing_ports, port_node);
309         remove_port(ctx, port);
310     }
311     shash_destroy(&existing_ports);
312 }