1f981b7a0a2a3027865033b7e68fbc51feabfbd6
[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 hmap_node *ld;
184         ld = hmap_first_with_hash(local_datapaths,
185                                   binding->datapath->tunnel_key);
186         if (!ld) {
187             /* This localnet port is on a datapath with no
188              * logical ports bound to this chassis, so there's no need
189              * to create patch ports for it. */
190             continue;
191         }
192
193         const char *network = smap_get(&binding->options, "network_name");
194         if (!network) {
195             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
196             VLOG_ERR_RL(&rl, "localnet port '%s' has no network name.",
197                          binding->logical_port);
198             continue;
199         }
200         struct ovsrec_bridge *br_ln = shash_find_data(&bridge_mappings, network);
201         if (!br_ln) {
202             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
203             VLOG_ERR_RL(&rl, "bridge not found for localnet port '%s' "
204                     "with network name '%s'", binding->logical_port, network);
205             continue;
206         }
207
208         char *name1 = patch_port_name(br_int->name, binding->logical_port);
209         char *name2 = patch_port_name(binding->logical_port, br_int->name);
210         create_patch_port(ctx, "ovn-localnet-port", binding->logical_port,
211                           br_int, name1, br_ln, name2, existing_ports);
212         create_patch_port(ctx, "ovn-localnet-port", binding->logical_port,
213                           br_ln, name2, br_int, name1, existing_ports);
214         free(name1);
215         free(name2);
216     }
217
218     shash_destroy(&bridge_mappings);
219 }
220
221 /* Add one OVS patch port for each OVN logical patch port.
222  *
223  * This is suboptimal for several reasons.  First, it creates an OVS port for
224  * every OVN logical patch port, not just for the ones that are actually useful
225  * on this hypervisor.  Second, it's wasteful to create an OVS patch port per
226  * OVN logical patch port, when really there's no benefit to them beyond a way
227  * to identify how a packet ingressed into a logical datapath.
228  *
229  * There are two obvious ways to improve the situation here, by modifying
230  * OVS:
231  *
232  *     1. Add a way to configure in OVS which fields are preserved on a hop
233  *        across an OVS patch port.  If MFF_LOG_DATAPATH and MFF_LOG_INPORT
234  *        were preserved, then only a single pair of OVS patch ports would be
235  *        required regardless of the number of OVN logical patch ports.
236  *
237  *     2. Add a new OpenFlow extension action modeled on "resubmit" that also
238  *        saves and restores the packet data and metadata (the inability to do
239  *        this is the only reason that "resubmit" can't be used already).  Or
240  *        add OpenFlow extension actions to otherwise save and restore packet
241  *        data and metadata.
242  */
243 static void
244 add_logical_patch_ports(struct controller_ctx *ctx,
245                         const struct ovsrec_bridge *br_int,
246                         struct shash *existing_ports)
247 {
248     const struct sbrec_port_binding *binding;
249     SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
250         if (!strcmp(binding->type, "patch")) {
251             const char *local = binding->logical_port;
252             const char *peer = smap_get(&binding->options, "peer");
253             if (!peer) {
254                 continue;
255             }
256
257             char *src_name = patch_port_name(local, peer);
258             char *dst_name = patch_port_name(peer, local);
259             create_patch_port(ctx, "ovn-logical-patch-port", local,
260                               br_int, src_name, br_int, dst_name,
261                               existing_ports);
262             free(dst_name);
263             free(src_name);
264         }
265     }
266 }
267
268 void
269 patch_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
270           struct hmap *local_datapaths)
271 {
272     if (!ctx->ovs_idl_txn) {
273         return;
274     }
275
276     /* Figure out what patch ports already exist. */
277     struct shash existing_ports = SHASH_INITIALIZER(&existing_ports);
278     const struct ovsrec_port *port;
279     OVSREC_PORT_FOR_EACH (port, ctx->ovs_idl) {
280         if (smap_get(&port->external_ids, "ovn-localnet-port") ||
281             smap_get(&port->external_ids, "ovn-logical-patch-port")) {
282             shash_add(&existing_ports, port->name, port);
283         }
284     }
285
286     /* Create in the database any patch ports that should exist.  Remove from
287      * 'existing_ports' any patch ports that do exist in the database and
288      * should be there. */
289     add_bridge_mappings(ctx, br_int, &existing_ports, local_datapaths);
290     add_logical_patch_ports(ctx, br_int, &existing_ports);
291
292     /* Now 'existing_ports' only still contains patch ports that exist in the
293      * database but shouldn't.  Delete them from the database. */
294     struct shash_node *port_node, *port_next_node;
295     SHASH_FOR_EACH_SAFE (port_node, port_next_node, &existing_ports) {
296         struct ovsrec_port *port = port_node->data;
297         shash_delete(&existing_ports, port_node);
298         remove_port(ctx, port);
299     }
300     shash_destroy(&existing_ports);
301 }