1 /* Copyright (c) 2015 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "lib/vswitch-idl.h"
23 #include "openvswitch/vlog.h"
24 #include "ovn-controller.h"
26 VLOG_DEFINE_THIS_MODULE(patch);
29 patch_port_name(const char *src, const char *dst)
31 return xasprintf("patch-%s-to-%s", src, dst);
34 /* Return true if 'port' is a patch port with the specified 'peer'. */
36 match_patch_port(const struct ovsrec_port *port, const char *peer)
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")) {
43 const char *iface_peer = smap_get(&iface->options, "peer");
44 if (peer && !strcmp(iface_peer, peer)) {
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'
55 * If such a patch port already exists, removes it from 'existing_ports'. */
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)
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);
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);
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);
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);
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);
100 remove_port(struct controller_ctx *ctx,
101 const struct ovsrec_port *port)
103 const struct ovsrec_bridge *bridge;
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
108 OVSREC_BRIDGE_FOR_EACH (bridge, ctx->ovs_idl) {
110 for (i = 0; i < bridge->n_ports; i++) {
111 if (bridge->ports[i] != port) {
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];
121 ovsrec_bridge_verify_ports(bridge);
122 ovsrec_bridge_set_ports(bridge, new_ports, bridge->n_ports - 1);
124 ovsrec_port_delete(port);
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'. */
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)
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);
144 mappings_cfg = smap_get(&cfg->external_ids, "ovn-bridge-mappings");
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;
158 network = strsep(&bridge, ":");
159 if (!bridge || !*network || !*bridge) {
160 VLOG_ERR("Invalid ovn-bridge-mappings configuration: '%s'",
165 ovs_bridge = get_bridge(ctx->ovs_idl, bridge);
167 VLOG_WARN("Bridge '%s' not found for network '%s'",
172 shash_add(&bridge_mappings, network, ovs_bridge);
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. */
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);
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. */
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);
202 ld->localnet_port = binding;
204 const char *network = smap_get(&binding->options, "network_name");
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);
211 struct ovsrec_bridge *br_ln = shash_find_data(&bridge_mappings, network);
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);
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);
229 shash_destroy(&bridge_mappings);
232 /* Add one OVS patch port for each OVN logical patch port.
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.
240 * There are two obvious ways to improve the situation here, by modifying
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.
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
255 add_logical_patch_ports(struct controller_ctx *ctx,
256 const struct ovsrec_bridge *br_int,
257 struct shash *existing_ports)
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");
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,
280 patch_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
281 struct hmap *local_datapaths)
283 if (!ctx->ovs_idl_txn) {
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);
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);
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);
311 shash_destroy(&existing_ports);