]> git.proxmox.com Git - ovs.git/blame - ovn/controller/patch.c
ovn-controller: Change encaps_run to work incrementally.
[ovs.git] / ovn / controller / patch.c
CommitLineData
e4426e34 1/* Copyright (c) 2015, 2016 Nicira, Inc.
a1d210a6
BP
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"
e90aeb57 21#include "lib/hmap.h"
a1d210a6
BP
22#include "lib/vswitch-idl.h"
23#include "openvswitch/vlog.h"
24#include "ovn-controller.h"
25
26VLOG_DEFINE_THIS_MODULE(patch);
27
28static char *
e51d619c 29patch_port_name(const char *src, const char *dst)
a1d210a6 30{
e51d619c 31 return xasprintf("patch-%s-to-%s", src, dst);
a1d210a6
BP
32}
33
e51d619c 34/* Return true if 'port' is a patch port with the specified 'peer'. */
a1d210a6 35static bool
e51d619c 36match_patch_port(const struct ovsrec_port *port, const char *peer)
a1d210a6 37{
e51d619c
BP
38 for (size_t i = 0; i < port->n_interfaces; i++) {
39 struct ovsrec_interface *iface = port->interfaces[i];
a1d210a6
BP
40 if (strcmp(iface->type, "patch")) {
41 continue;
42 }
e51d619c
BP
43 const char *iface_peer = smap_get(&iface->options, "peer");
44 if (peer && !strcmp(iface_peer, peer)) {
45 return true;
a1d210a6
BP
46 }
47 }
e51d619c 48 return false;
a1d210a6
BP
49}
50
e51d619c 51/* Creates a patch port in bridge 'src' named 'src_name', whose peer is
d387d24d
BP
52 * 'dst_name' in bridge 'dst'. Initializes the patch port's external-ids:'key'
53 * to 'key'.
e51d619c
BP
54 *
55 * If such a patch port already exists, removes it from 'existing_ports'. */
a1d210a6
BP
56static void
57create_patch_port(struct controller_ctx *ctx,
d387d24d 58 const char *key, const char *value,
e51d619c
BP
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)
a1d210a6 62{
e51d619c
BP
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 }
a1d210a6
BP
70
71 ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
72 "ovn-controller: creating patch port '%s' from '%s' to '%s'",
e51d619c 73 src_name, src->name, dst->name);
a1d210a6
BP
74
75 struct ovsrec_interface *iface;
76 iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
e51d619c 77 ovsrec_interface_set_name(iface, src_name);
a1d210a6 78 ovsrec_interface_set_type(iface, "patch");
e51d619c 79 const struct smap options = SMAP_CONST1(&options, "peer", dst_name);
a1d210a6
BP
80 ovsrec_interface_set_options(iface, &options);
81
82 struct ovsrec_port *port;
83 port = ovsrec_port_insert(ctx->ovs_idl_txn);
e51d619c 84 ovsrec_port_set_name(port, src_name);
a1d210a6 85 ovsrec_port_set_interfaces(port, &iface, 1);
d387d24d 86 const struct smap ids = SMAP_CONST1(&ids, key, value);
a1d210a6
BP
87 ovsrec_port_set_external_ids(port, &ids);
88
89 struct ovsrec_port **ports;
e51d619c
BP
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);
a1d210a6
BP
95
96 free(ports);
a1d210a6
BP
97}
98
a1d210a6
BP
99static void
100remove_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
e51d619c
BP
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'. */
a1d210a6 133static void
e51d619c
BP
134add_bridge_mappings(struct controller_ctx *ctx,
135 const struct ovsrec_bridge *br_int,
e90aeb57
RB
136 struct shash *existing_ports,
137 struct hmap *local_datapaths)
a1d210a6 138{
58bcc67c
BP
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 }
a1d210a6 149
e90aeb57
RB
150 /* Parse bridge mappings. */
151 struct shash bridge_mappings = SHASH_INITIALIZER(&bridge_mappings);
a1d210a6
BP
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
e90aeb57 172 shash_add(&bridge_mappings, network, ovs_bridge);
a1d210a6
BP
173 }
174 free(start);
e90aeb57
RB
175
176 const struct sbrec_port_binding *binding;
177 SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
1021e19b 178 if (!strcmp(binding->type, "localnet")) {
e4426e34
BP
179 struct local_datapath *ld
180 = get_local_datapath(local_datapaths,
181 binding->datapath->tunnel_key);
1021e19b
RB
182 if (!ld) {
183 /* This localnet port is on a datapath with no
184 * logical ports bound to this chassis, so there's no need
185 * to create patch ports for it. */
186 continue;
187 }
188 if (ld->localnet_port) {
189 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
190 VLOG_WARN_RL(&rl, "localnet port '%s' already set for datapath "
191 "'%"PRId64"', skipping the new port '%s'.",
192 ld->localnet_port->logical_port,
193 binding->datapath->tunnel_key,
194 binding->logical_port);
195 continue;
196 }
197 ld->localnet_port = binding;
198 } else {
e90aeb57
RB
199 /* Not a binding for a localnet port. */
200 continue;
201 }
202
e90aeb57
RB
203 const char *network = smap_get(&binding->options, "network_name");
204 if (!network) {
205 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
206 VLOG_ERR_RL(&rl, "localnet port '%s' has no network name.",
207 binding->logical_port);
208 continue;
209 }
210 struct ovsrec_bridge *br_ln = shash_find_data(&bridge_mappings, network);
211 if (!br_ln) {
212 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
213 VLOG_ERR_RL(&rl, "bridge not found for localnet port '%s' "
214 "with network name '%s'", binding->logical_port, network);
215 continue;
216 }
217
218 char *name1 = patch_port_name(br_int->name, binding->logical_port);
219 char *name2 = patch_port_name(binding->logical_port, br_int->name);
220 create_patch_port(ctx, "ovn-localnet-port", binding->logical_port,
221 br_int, name1, br_ln, name2, existing_ports);
222 create_patch_port(ctx, "ovn-localnet-port", binding->logical_port,
223 br_ln, name2, br_int, name1, existing_ports);
224 free(name1);
225 free(name2);
226 }
227
228 shash_destroy(&bridge_mappings);
a1d210a6
BP
229}
230
c4f32696
HZ
231static void
232add_patched_datapath(struct hmap *patched_datapaths,
3bd4ae23 233 const struct sbrec_port_binding *binding_rec, bool local)
c4f32696 234{
e4426e34 235 if (get_patched_datapath(patched_datapaths,
c4f32696
HZ
236 binding_rec->datapath->tunnel_key)) {
237 return;
238 }
239
240 struct patched_datapath *pd = xzalloc(sizeof *pd);
3bd4ae23
GS
241 pd->local = local;
242 pd->port_binding = binding_rec;
c4f32696
HZ
243 hmap_insert(patched_datapaths, &pd->hmap_node,
244 binding_rec->datapath->tunnel_key);
245}
246
d387d24d
BP
247/* Add one OVS patch port for each OVN logical patch port.
248 *
249 * This is suboptimal for several reasons. First, it creates an OVS port for
250 * every OVN logical patch port, not just for the ones that are actually useful
251 * on this hypervisor. Second, it's wasteful to create an OVS patch port per
252 * OVN logical patch port, when really there's no benefit to them beyond a way
253 * to identify how a packet ingressed into a logical datapath.
254 *
255 * There are two obvious ways to improve the situation here, by modifying
256 * OVS:
257 *
258 * 1. Add a way to configure in OVS which fields are preserved on a hop
259 * across an OVS patch port. If MFF_LOG_DATAPATH and MFF_LOG_INPORT
260 * were preserved, then only a single pair of OVS patch ports would be
261 * required regardless of the number of OVN logical patch ports.
262 *
263 * 2. Add a new OpenFlow extension action modeled on "resubmit" that also
264 * saves and restores the packet data and metadata (the inability to do
265 * this is the only reason that "resubmit" can't be used already). Or
266 * add OpenFlow extension actions to otherwise save and restore packet
267 * data and metadata.
268 */
269static void
270add_logical_patch_ports(struct controller_ctx *ctx,
271 const struct ovsrec_bridge *br_int,
c1645003 272 const char *local_chassis_id,
c4f32696
HZ
273 struct shash *existing_ports,
274 struct hmap *patched_datapaths)
d387d24d 275{
c1645003
GS
276 const struct sbrec_chassis *chassis_rec;
277 chassis_rec = get_chassis(ctx->ovnsb_idl, local_chassis_id);
278 if (!chassis_rec) {
279 return;
280 }
281
d387d24d
BP
282 const struct sbrec_port_binding *binding;
283 SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
c1645003
GS
284 bool local_port = false;
285 if (!strcmp(binding->type, "gateway")) {
286 const char *chassis = smap_get(&binding->options,
287 "gateway-chassis");
288 if (!strcmp(local_chassis_id, chassis)) {
289 local_port = true;
290 }
291 }
292
293 if (!strcmp(binding->type, "patch") || local_port) {
d387d24d
BP
294 const char *local = binding->logical_port;
295 const char *peer = smap_get(&binding->options, "peer");
296 if (!peer) {
297 continue;
298 }
299
300 char *src_name = patch_port_name(local, peer);
301 char *dst_name = patch_port_name(peer, local);
302 create_patch_port(ctx, "ovn-logical-patch-port", local,
303 br_int, src_name, br_int, dst_name,
304 existing_ports);
305 free(dst_name);
306 free(src_name);
3bd4ae23 307 add_patched_datapath(patched_datapaths, binding, local_port);
c1645003
GS
308 if (local_port) {
309 if (binding->chassis != chassis_rec && ctx->ovnsb_idl_txn) {
310 sbrec_port_binding_set_chassis(binding, chassis_rec);
311 }
312 }
d387d24d
BP
313 }
314 }
315}
316
a1d210a6 317void
e90aeb57 318patch_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
c1645003
GS
319 const char *chassis_id, struct hmap *local_datapaths,
320 struct hmap *patched_datapaths)
a1d210a6 321{
edc0a107 322 if (!ctx->ovs_idl_txn) {
cb08edc6
BP
323 return;
324 }
325
58bcc67c
BP
326 /* Figure out what patch ports already exist. */
327 struct shash existing_ports = SHASH_INITIALIZER(&existing_ports);
328 const struct ovsrec_port *port;
329 OVSREC_PORT_FOR_EACH (port, ctx->ovs_idl) {
d387d24d
BP
330 if (smap_get(&port->external_ids, "ovn-localnet-port") ||
331 smap_get(&port->external_ids, "ovn-logical-patch-port")) {
58bcc67c 332 shash_add(&existing_ports, port->name, port);
a1d210a6
BP
333 }
334 }
58bcc67c
BP
335
336 /* Create in the database any patch ports that should exist. Remove from
337 * 'existing_ports' any patch ports that do exist in the database and
338 * should be there. */
e90aeb57 339 add_bridge_mappings(ctx, br_int, &existing_ports, local_datapaths);
c1645003
GS
340 add_logical_patch_ports(ctx, br_int, chassis_id, &existing_ports,
341 patched_datapaths);
58bcc67c
BP
342
343 /* Now 'existing_ports' only still contains patch ports that exist in the
344 * database but shouldn't. Delete them from the database. */
345 struct shash_node *port_node, *port_next_node;
346 SHASH_FOR_EACH_SAFE (port_node, port_next_node, &existing_ports) {
347 struct ovsrec_port *port = port_node->data;
348 shash_delete(&existing_ports, port_node);
349 remove_port(ctx, port);
350 }
351 shash_destroy(&existing_ports);
a1d210a6 352}