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.
20 #include "lib/poll-loop.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"
28 VLOG_DEFINE_THIS_MODULE(chassis);
31 chassis_init(struct controller_ctx *ctx)
33 ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_open_vswitch);
34 ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_open_vswitch_col_external_ids);
35 ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_bridge);
36 ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_bridge_col_ports);
37 ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_port);
38 ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_port_col_name);
39 ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_port_col_interfaces);
40 ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_port_col_external_ids);
41 ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_interface);
42 ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_interface_col_name);
43 ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_interface_col_type);
44 ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_interface_col_options);
48 register_chassis(struct controller_ctx *ctx)
50 const struct sbrec_chassis *chassis_rec;
51 const struct ovsrec_open_vswitch *cfg;
52 const char *encap_type, *encap_ip;
53 struct sbrec_encap *encap_rec;
54 static bool inited = false;
55 int retval = TXN_TRY_AGAIN;
56 struct ovsdb_idl_txn *txn;
58 SBREC_CHASSIS_FOR_EACH(chassis_rec, ctx->ovnsb_idl) {
59 if (!strcmp(chassis_rec->name, ctx->chassis_id)) {
64 /* xxx Need to support more than one encap. Also need to support
65 * xxx encap options. */
66 cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
68 VLOG_INFO("No Open_vSwitch row defined.");
72 encap_type = smap_get(&cfg->external_ids, "ovn-encap-type");
73 encap_ip = smap_get(&cfg->external_ids, "ovn-encap-ip");
74 if (!encap_type || !encap_ip) {
75 VLOG_INFO("Need to specify an encap type and ip");
82 for (i = 0; i < chassis_rec->n_encaps; i++) {
83 if (!strcmp(chassis_rec->encaps[i]->type, encap_type)
84 && !strcmp(chassis_rec->encaps[i]->ip, encap_ip)) {
85 /* Nothing changed. */
89 VLOG_WARN("Chassis config changing on startup, make sure "
90 "multiple chassis are not configured : %s/%s->%s/%s",
91 chassis_rec->encaps[i]->type,
92 chassis_rec->encaps[i]->ip,
93 encap_type, encap_ip);
99 txn = ovsdb_idl_txn_create(ctx->ovnsb_idl);
100 ovsdb_idl_txn_add_comment(txn,
101 "ovn-controller: registering chassis '%s'",
105 chassis_rec = sbrec_chassis_insert(txn);
106 sbrec_chassis_set_name(chassis_rec, ctx->chassis_id);
109 encap_rec = sbrec_encap_insert(txn);
111 sbrec_encap_set_type(encap_rec, encap_type);
112 sbrec_encap_set_ip(encap_rec, encap_ip);
114 sbrec_chassis_set_encaps(chassis_rec, &encap_rec, 1);
116 retval = ovsdb_idl_txn_commit_block(txn);
117 if (retval != TXN_SUCCESS && retval != TXN_UNCHANGED) {
118 VLOG_INFO("Problem registering chassis: %s",
119 ovsdb_idl_txn_status_to_string(retval));
120 poll_immediate_wake();
122 ovsdb_idl_txn_destroy(txn);
127 /* Enough context to create a new tunnel, using tunnel_add(). */
129 /* Contains "struct port_hash_node"s. Used to figure out what
130 * existing tunnels should be deleted: we index all of the OVN encap
131 * rows into this data structure, then as existing rows are
132 * generated we remove them. After generating all the rows, any
133 * remaining in 'tunnel_hmap' must be deleted from the database. */
134 struct hmap tunnel_hmap;
136 /* Names of all ports in the bridge, to allow checking uniqueness when
137 * adding a new tunnel. */
138 struct sset port_names;
140 struct ovsdb_idl_txn *ovs_txn;
141 const struct ovsrec_bridge *br_int;
144 struct port_hash_node {
145 struct hmap_node node;
146 const struct ovsrec_port *port;
147 const struct ovsrec_bridge *bridge;
151 port_hash(const char *chassis_id, const char *type, const char *ip)
153 size_t hash = hash_string(chassis_id, 0);
154 hash = hash_string(type, hash);
155 return hash_string(ip, hash);
159 port_hash_rec(const struct ovsrec_port *port)
161 const char *chassis_id, *ip;
162 const struct ovsrec_interface *iface;
164 chassis_id = smap_get(&port->external_ids, "ovn-chassis-id");
166 if (!chassis_id || !port->n_interfaces) {
167 /* This should not happen for an OVN-created port. */
171 iface = port->interfaces[0];
172 ip = smap_get(&iface->options, "remote_ip");
174 return port_hash(chassis_id, iface->type, ip);
178 tunnel_create_name(struct tunnel_ctx *tc, const char *chassis_id)
182 for (i = 0; i < UINT16_MAX; i++) {
184 port_name = xasprintf("ovn-%.6s-%x", chassis_id, i);
186 if (!sset_contains(&tc->port_names, port_name)) {
198 tunnel_add(struct tunnel_ctx *tc, const char *new_chassis_id,
199 const struct sbrec_encap *encap)
201 struct port_hash_node *hash_node;
203 /* Check whether such a row already exists in OVS. If so, remove it
204 * from 'tc->tunnel_hmap' and we're done. */
205 HMAP_FOR_EACH_WITH_HASH (hash_node, node,
206 port_hash(new_chassis_id,
207 encap->type, encap->ip),
209 const struct ovsrec_port *port = hash_node->port;
210 const char *chassis_id = smap_get(&port->external_ids,
212 const struct ovsrec_interface *iface;
215 if (!chassis_id || !port->n_interfaces) {
219 iface = port->interfaces[0];
220 ip = smap_get(&iface->options, "remote_ip");
225 if (!strcmp(new_chassis_id, chassis_id)
226 && !strcmp(encap->type, iface->type)
227 && !strcmp(encap->ip, ip)) {
228 hmap_remove(&tc->tunnel_hmap, &hash_node->node);
234 /* No such port, so add one. */
235 struct smap external_ids = SMAP_INITIALIZER(&external_ids);
236 struct smap options = SMAP_INITIALIZER(&options);
237 struct ovsrec_port *port, **ports;
238 struct ovsrec_interface *iface;
242 port_name = tunnel_create_name(tc, new_chassis_id);
244 VLOG_WARN("Unable to allocate unique name for '%s' tunnel",
249 iface = ovsrec_interface_insert(tc->ovs_txn);
250 ovsrec_interface_set_name(iface, port_name);
251 ovsrec_interface_set_type(iface, encap->type);
252 smap_add(&options, "remote_ip", encap->ip);
253 smap_add(&options, "key", "flow");
254 ovsrec_interface_set_options(iface, &options);
255 smap_destroy(&options);
257 port = ovsrec_port_insert(tc->ovs_txn);
258 ovsrec_port_set_name(port, port_name);
259 ovsrec_port_set_interfaces(port, &iface, 1);
260 smap_add(&external_ids, "ovn-chassis-id", new_chassis_id);
261 ovsrec_port_set_external_ids(port, &external_ids);
262 smap_destroy(&external_ids);
264 ports = xmalloc(sizeof *tc->br_int->ports * (tc->br_int->n_ports + 1));
265 for (i = 0; i < tc->br_int->n_ports; i++) {
266 ports[i] = tc->br_int->ports[i];
268 ports[tc->br_int->n_ports] = port;
269 ovsrec_bridge_set_ports(tc->br_int, ports, tc->br_int->n_ports + 1);
271 sset_add(&tc->port_names, port_name);
277 bridge_delete_port(const struct ovsrec_bridge *br,
278 const struct ovsrec_port *port)
280 struct ovsrec_port **ports;
283 ports = xmalloc(sizeof *br->ports * br->n_ports);
284 for (i = n = 0; i < br->n_ports; i++) {
285 if (br->ports[i] != port) {
286 ports[n++] = br->ports[i];
289 ovsrec_bridge_set_ports(br, ports, n);
293 static struct sbrec_encap *
294 preferred_encap(const struct sbrec_chassis *chassis_rec)
298 /* For hypervisors, we only support Geneve and STT encapsulations.
299 * Sets are returned alphabetically, so "geneve" will be preferred
301 for (i = 0; i < chassis_rec->n_encaps; i++) {
302 if (!strcmp(chassis_rec->encaps[i]->type, "geneve")
303 || !strcmp(chassis_rec->encaps[i]->type, "stt")) {
304 return chassis_rec->encaps[i];
312 update_encaps(struct controller_ctx *ctx)
314 const struct sbrec_chassis *chassis_rec;
315 const struct ovsrec_bridge *br;
318 struct tunnel_ctx tc = {
319 .tunnel_hmap = HMAP_INITIALIZER(&tc.tunnel_hmap),
320 .port_names = SSET_INITIALIZER(&tc.port_names),
321 .br_int = ctx->br_int
324 tc.ovs_txn = ovsdb_idl_txn_create(ctx->ovs_idl);
325 ovsdb_idl_txn_add_comment(tc.ovs_txn,
326 "ovn-controller: modifying OVS tunnels '%s'",
329 /* Collect all port names into tc.port_names.
331 * Collect all the OVN-created tunnels into tc.tunnel_hmap. */
332 OVSREC_BRIDGE_FOR_EACH(br, ctx->ovs_idl) {
335 for (i = 0; i < br->n_ports; i++) {
336 const struct ovsrec_port *port = br->ports[i];
338 sset_add(&tc.port_names, port->name);
340 if (smap_get(&port->external_ids, "ovn-chassis-id")) {
341 struct port_hash_node *hash_node = xzalloc(sizeof *hash_node);
342 hash_node->bridge = br;
343 hash_node->port = port;
344 hmap_insert(&tc.tunnel_hmap, &hash_node->node,
345 port_hash_rec(port));
350 SBREC_CHASSIS_FOR_EACH(chassis_rec, ctx->ovnsb_idl) {
351 if (strcmp(chassis_rec->name, ctx->chassis_id)) {
352 /* Create tunnels to the other chassis. */
353 const struct sbrec_encap *encap = preferred_encap(chassis_rec);
355 VLOG_INFO("No supported encaps for '%s'", chassis_rec->name);
358 tunnel_add(&tc, chassis_rec->name, encap);
362 /* Delete any existing OVN tunnels that were not still around. */
363 struct port_hash_node *hash_node, *next_hash_node;
364 HMAP_FOR_EACH_SAFE (hash_node, next_hash_node, node, &tc.tunnel_hmap) {
365 hmap_remove(&tc.tunnel_hmap, &hash_node->node);
366 bridge_delete_port(hash_node->bridge, hash_node->port);
369 hmap_destroy(&tc.tunnel_hmap);
370 sset_destroy(&tc.port_names);
372 retval = ovsdb_idl_txn_commit_block(tc.ovs_txn);
373 if (retval != TXN_SUCCESS && retval != TXN_UNCHANGED) {
374 VLOG_INFO("Problem modifying OVS tunnels: %s",
375 ovsdb_idl_txn_status_to_string(retval));
376 poll_immediate_wake();
378 ovsdb_idl_txn_destroy(tc.ovs_txn);
382 chassis_run(struct controller_ctx *ctx)
384 register_chassis(ctx);
389 chassis_destroy(struct controller_ctx *ctx)
391 int retval = TXN_TRY_AGAIN;
393 ovs_assert(ctx->ovnsb_idl);
395 while (retval != TXN_SUCCESS && retval != TXN_UNCHANGED) {
396 const struct sbrec_chassis *chassis_rec;
397 struct ovsdb_idl_txn *txn;
399 SBREC_CHASSIS_FOR_EACH(chassis_rec, ctx->ovnsb_idl) {
400 if (!strcmp(chassis_rec->name, ctx->chassis_id)) {
409 txn = ovsdb_idl_txn_create(ctx->ovnsb_idl);
410 ovsdb_idl_txn_add_comment(txn,
411 "ovn-controller: unregistering chassis '%s'",
413 sbrec_chassis_delete(chassis_rec);
415 retval = ovsdb_idl_txn_commit_block(txn);
416 if (retval == TXN_ERROR) {
417 VLOG_INFO("Problem unregistering chassis: %s",
418 ovsdb_idl_txn_status_to_string(retval));
420 ovsdb_idl_txn_destroy(txn);
423 retval = TXN_TRY_AGAIN;
424 while (retval != TXN_SUCCESS && retval != TXN_UNCHANGED) {
425 struct ovsrec_port **ports;
426 struct ovsdb_idl_txn *txn;
429 txn = ovsdb_idl_txn_create(ctx->ovs_idl);
430 ovsdb_idl_txn_add_comment(txn,
431 "ovn-controller: destroying tunnels");
433 /* Delete all the OVS-created tunnels from the integration
435 ports = xmalloc(sizeof *ctx->br_int->ports * ctx->br_int->n_ports);
436 for (i = n = 0; i < ctx->br_int->n_ports; i++) {
437 if (!smap_get(&ctx->br_int->ports[i]->external_ids,
439 ports[n++] = ctx->br_int->ports[i];
442 ovsrec_bridge_set_ports(ctx->br_int, ports, n);
445 retval = ovsdb_idl_txn_commit_block(txn);
446 if (retval == TXN_ERROR) {
447 VLOG_INFO("Problem destroying tunnels: %s",
448 ovsdb_idl_txn_status_to_string(retval));
450 ovsdb_idl_txn_destroy(txn);