3638342eca2771b19a44c3592c8d8f6b00f65b42
[cascardo/ovs.git] / ovn / controller / ovn-controller.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 "ovn-controller.h"
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "command-line.h"
27 #include "compiler.h"
28 #include "daemon.h"
29 #include "dirs.h"
30 #include "dynamic-string.h"
31 #include "openvswitch/vconn.h"
32 #include "openvswitch/vlog.h"
33 #include "ovn/lib/ovn-sb-idl.h"
34 #include "poll-loop.h"
35 #include "fatal-signal.h"
36 #include "lib/hmap.h"
37 #include "lib/vswitch-idl.h"
38 #include "smap.h"
39 #include "stream.h"
40 #include "stream-ssl.h"
41 #include "unixctl.h"
42 #include "util.h"
43
44 #include "ofctrl.h"
45 #include "pinctrl.h"
46 #include "binding.h"
47 #include "chassis.h"
48 #include "encaps.h"
49 #include "patch.h"
50 #include "physical.h"
51 #include "lflow.h"
52
53 VLOG_DEFINE_THIS_MODULE(main);
54
55 static unixctl_cb_func ovn_controller_exit;
56 static unixctl_cb_func ct_zone_list;
57
58 #define DEFAULT_BRIDGE_NAME "br-int"
59
60 static void parse_options(int argc, char *argv[]);
61 OVS_NO_RETURN static void usage(void);
62
63 static char *ovs_remote;
64
65 const struct sbrec_chassis *
66 get_chassis(struct ovsdb_idl *ovnsb_idl, const char *chassis_id)
67 {
68     const struct sbrec_chassis *chassis_rec;
69
70     SBREC_CHASSIS_FOR_EACH(chassis_rec, ovnsb_idl) {
71         if (!strcmp(chassis_rec->name, chassis_id)) {
72             break;
73         }
74     }
75
76     return chassis_rec;
77 }
78
79 uint32_t
80 get_tunnel_type(const char *name)
81 {
82     if (!strcmp(name, "geneve")) {
83         return GENEVE;
84     } else if (!strcmp(name, "stt")) {
85         return STT;
86     } else if (!strcmp(name, "vxlan")) {
87         return VXLAN;
88     }
89
90     return 0;
91 }
92
93 const struct ovsrec_bridge *
94 get_bridge(struct ovsdb_idl *ovs_idl, const char *br_name)
95 {
96     const struct ovsrec_bridge *br;
97     OVSREC_BRIDGE_FOR_EACH (br, ovs_idl) {
98         if (!strcmp(br->name, br_name)) {
99             return br;
100         }
101     }
102     return NULL;
103 }
104
105 static const struct ovsrec_bridge *
106 create_br_int(struct controller_ctx *ctx,
107               const struct ovsrec_open_vswitch *cfg,
108               const char *bridge_name)
109 {
110     if (!ctx->ovs_idl_txn) {
111         return NULL;
112     }
113
114     ovsdb_idl_txn_add_comment(ctx->ovs_idl_txn,
115             "ovn-controller: creating integration bridge '%s'", bridge_name);
116
117     struct ovsrec_interface *iface;
118     iface = ovsrec_interface_insert(ctx->ovs_idl_txn);
119     ovsrec_interface_set_name(iface, bridge_name);
120     ovsrec_interface_set_type(iface, "internal");
121
122     struct ovsrec_port *port;
123     port = ovsrec_port_insert(ctx->ovs_idl_txn);
124     ovsrec_port_set_name(port, bridge_name);
125     ovsrec_port_set_interfaces(port, &iface, 1);
126
127     struct ovsrec_bridge *bridge;
128     bridge = ovsrec_bridge_insert(ctx->ovs_idl_txn);
129     ovsrec_bridge_set_name(bridge, bridge_name);
130     ovsrec_bridge_set_fail_mode(bridge, "secure");
131     const struct smap oc = SMAP_CONST1(&oc, "disable-in-band", "true");
132     ovsrec_bridge_set_other_config(bridge, &oc);
133     ovsrec_bridge_set_ports(bridge, &port, 1);
134
135     struct ovsrec_bridge **bridges;
136     size_t bytes = sizeof *bridges * cfg->n_bridges;
137     bridges = xmalloc(bytes + sizeof *bridges);
138     memcpy(bridges, cfg->bridges, bytes);
139     bridges[cfg->n_bridges] = bridge;
140     ovsrec_open_vswitch_verify_bridges(cfg);
141     ovsrec_open_vswitch_set_bridges(cfg, bridges, cfg->n_bridges + 1);
142
143     return bridge;
144 }
145
146 static const struct ovsrec_bridge *
147 get_br_int(struct controller_ctx *ctx)
148 {
149     const struct ovsrec_open_vswitch *cfg;
150     cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
151     if (!cfg) {
152         return NULL;
153     }
154
155     const char *br_int_name = smap_get(&cfg->external_ids, "ovn-bridge");
156     if (!br_int_name) {
157         br_int_name = DEFAULT_BRIDGE_NAME;
158     }
159
160     const struct ovsrec_bridge *br;
161     br = get_bridge(ctx->ovs_idl, br_int_name);
162     if (!br) {
163         return create_br_int(ctx, cfg, br_int_name);
164     }
165     return br;
166 }
167
168 static const char *
169 get_chassis_id(const struct ovsdb_idl *ovs_idl)
170 {
171     const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(ovs_idl);
172     return cfg ? smap_get(&cfg->external_ids, "system-id") : NULL;
173 }
174
175 /* Retrieves the OVN Southbound remote location from the
176  * "external-ids:ovn-remote" key in 'ovs_idl' and returns a copy of it.
177  *
178  * XXX ovn-controller does not support this changing mid-run, but that should
179  * be addressed later. */
180 static char *
181 get_ovnsb_remote(struct ovsdb_idl *ovs_idl)
182 {
183     while (1) {
184         ovsdb_idl_run(ovs_idl);
185
186         const struct ovsrec_open_vswitch *cfg
187             = ovsrec_open_vswitch_first(ovs_idl);
188         if (cfg) {
189             const char *remote = smap_get(&cfg->external_ids, "ovn-remote");
190             if (remote) {
191                 return xstrdup(remote);
192             }
193         }
194
195         VLOG_INFO("OVN OVSDB remote not specified.  Waiting...");
196         ovsdb_idl_wait(ovs_idl);
197         poll_block();
198     }
199 }
200
201 int
202 main(int argc, char *argv[])
203 {
204     struct unixctl_server *unixctl;
205     bool exiting;
206     int retval;
207
208     ovs_cmdl_proctitle_init(argc, argv);
209     set_program_name(argv[0]);
210     service_start(&argc, &argv);
211     parse_options(argc, argv);
212     fatal_ignore_sigpipe();
213
214     daemonize_start(false);
215
216     retval = unixctl_server_create(NULL, &unixctl);
217     if (retval) {
218         exit(EXIT_FAILURE);
219     }
220     unixctl_command_register("exit", "", 0, 0, ovn_controller_exit, &exiting);
221
222     daemonize_complete();
223
224     ovsrec_init();
225     sbrec_init();
226
227     ofctrl_init();
228     pinctrl_init();
229     lflow_init();
230
231     /* Connect to OVS OVSDB instance.  We do not monitor all tables by
232      * default, so modules must register their interest explicitly.  */
233     struct ovsdb_idl_loop ovs_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
234         ovsdb_idl_create(ovs_remote, &ovsrec_idl_class, false, true));
235     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_open_vswitch);
236     ovsdb_idl_add_column(ovs_idl_loop.idl,
237                          &ovsrec_open_vswitch_col_external_ids);
238     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_open_vswitch_col_bridges);
239     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_interface);
240     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_name);
241     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_type);
242     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_interface_col_options);
243     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_port);
244     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_name);
245     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_interfaces);
246     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_port_col_external_ids);
247     ovsdb_idl_add_table(ovs_idl_loop.idl, &ovsrec_table_bridge);
248     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_ports);
249     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_name);
250     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_fail_mode);
251     ovsdb_idl_add_column(ovs_idl_loop.idl, &ovsrec_bridge_col_other_config);
252     chassis_register_ovs_idl(ovs_idl_loop.idl);
253     encaps_register_ovs_idl(ovs_idl_loop.idl);
254     binding_register_ovs_idl(ovs_idl_loop.idl);
255     physical_register_ovs_idl(ovs_idl_loop.idl);
256     ovsdb_idl_get_initial_snapshot(ovs_idl_loop.idl);
257
258     /* Connect to OVN SB database. */
259     char *ovnsb_remote = get_ovnsb_remote(ovs_idl_loop.idl);
260     struct ovsdb_idl_loop ovnsb_idl_loop = OVSDB_IDL_LOOP_INITIALIZER(
261         ovsdb_idl_create(ovnsb_remote, &sbrec_idl_class, true, true));
262     ovsdb_idl_get_initial_snapshot(ovnsb_idl_loop.idl);
263
264     /* Initialize connection tracking zones. */
265     struct simap ct_zones = SIMAP_INITIALIZER(&ct_zones);
266     unsigned long ct_zone_bitmap[BITMAP_N_LONGS(MAX_CT_ZONES)];
267     bitmap_set1(ct_zone_bitmap, 0); /* Zone 0 is reserved. */
268     unixctl_command_register("ct-zone-list", "", 0, 0,
269                              ct_zone_list, &ct_zones);
270
271     /* Main loop. */
272     exiting = false;
273     while (!exiting) {
274         struct controller_ctx ctx = {
275             .ovs_idl = ovs_idl_loop.idl,
276             .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
277             .ovnsb_idl = ovnsb_idl_loop.idl,
278             .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
279         };
280
281         /* Contains bare "struct hmap_node"s whose hash values are the tunnel_key
282          * of datapaths with at least one local port binding. */
283         struct hmap local_datapaths = HMAP_INITIALIZER(&local_datapaths);
284
285         const struct ovsrec_bridge *br_int = get_br_int(&ctx);
286         const char *chassis_id = get_chassis_id(ctx.ovs_idl);
287
288         if (chassis_id) {
289             chassis_run(&ctx, chassis_id);
290             encaps_run(&ctx, br_int, chassis_id);
291             binding_run(&ctx, br_int, chassis_id, &ct_zones, ct_zone_bitmap,
292                     &local_datapaths);
293         }
294
295         if (br_int) {
296             patch_run(&ctx, br_int, &local_datapaths);
297
298             enum mf_field_id mff_ovn_geneve = ofctrl_run(br_int);
299
300             pinctrl_run(&ctx, br_int);
301
302             struct hmap flow_table = HMAP_INITIALIZER(&flow_table);
303             lflow_run(&ctx, &flow_table, &ct_zones, &local_datapaths);
304             if (chassis_id) {
305                 physical_run(&ctx, mff_ovn_geneve,
306                              br_int, chassis_id, &ct_zones, &flow_table);
307             }
308             ofctrl_put(&flow_table);
309             hmap_destroy(&flow_table);
310         }
311
312         /* local_datapaths contains bare hmap_node instances.
313          * We use this wrapper so that we can make use of
314          * HMAP_FOR_EACH_SAFE to tear down the hmap. */
315         struct {
316             struct hmap_node node;
317         } *cur_node, *next_node;
318         HMAP_FOR_EACH_SAFE (cur_node, next_node, node, &local_datapaths) {
319             hmap_remove(&local_datapaths, &cur_node->node);
320             free(cur_node);
321         }
322         hmap_destroy(&local_datapaths);
323
324         unixctl_server_run(unixctl);
325
326         unixctl_server_wait(unixctl);
327         if (exiting) {
328             poll_immediate_wake();
329         }
330
331         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
332         ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
333
334         if (br_int) {
335             ofctrl_wait();
336             pinctrl_wait();
337         }
338         poll_block();
339         if (should_service_stop()) {
340             exiting = true;
341         }
342     }
343
344     /* It's time to exit.  Clean up the databases. */
345     bool done = false;
346     while (!done) {
347         struct controller_ctx ctx = {
348             .ovs_idl = ovs_idl_loop.idl,
349             .ovs_idl_txn = ovsdb_idl_loop_run(&ovs_idl_loop),
350             .ovnsb_idl = ovnsb_idl_loop.idl,
351             .ovnsb_idl_txn = ovsdb_idl_loop_run(&ovnsb_idl_loop),
352         };
353
354         const struct ovsrec_bridge *br_int = get_br_int(&ctx);
355         const char *chassis_id = get_chassis_id(ctx.ovs_idl);
356
357         /* Run all of the cleanup functions, even if one of them returns false.
358          * We're done if all of them return true. */
359         done = binding_cleanup(&ctx, chassis_id);
360         done = chassis_cleanup(&ctx, chassis_id) && done;
361         done = encaps_cleanup(&ctx, br_int) && done;
362         if (done) {
363             poll_immediate_wake();
364         }
365
366         ovsdb_idl_loop_commit_and_wait(&ovnsb_idl_loop);
367         ovsdb_idl_loop_commit_and_wait(&ovs_idl_loop);
368         poll_block();
369     }
370
371     unixctl_server_destroy(unixctl);
372     lflow_destroy();
373     ofctrl_destroy();
374     pinctrl_destroy();
375
376     simap_destroy(&ct_zones);
377
378     ovsdb_idl_loop_destroy(&ovs_idl_loop);
379     ovsdb_idl_loop_destroy(&ovnsb_idl_loop);
380
381     free(ovnsb_remote);
382     free(ovs_remote);
383     service_stop();
384
385     exit(retval);
386 }
387
388 static void
389 parse_options(int argc, char *argv[])
390 {
391     enum {
392         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
393         OPT_BOOTSTRAP_CA_CERT,
394         VLOG_OPTION_ENUMS,
395         DAEMON_OPTION_ENUMS
396     };
397
398     static struct option long_options[] = {
399         {"help", no_argument, NULL, 'h'},
400         {"version", no_argument, NULL, 'V'},
401         VLOG_LONG_OPTIONS,
402         DAEMON_LONG_OPTIONS,
403         STREAM_SSL_LONG_OPTIONS,
404         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
405         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
406         {NULL, 0, NULL, 0}
407     };
408     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
409
410     for (;;) {
411         int c;
412
413         c = getopt_long(argc, argv, short_options, long_options, NULL);
414         if (c == -1) {
415             break;
416         }
417
418         switch (c) {
419         case 'h':
420             usage();
421
422         case 'V':
423             ovs_print_version(OFP13_VERSION, OFP13_VERSION);
424             exit(EXIT_SUCCESS);
425
426         VLOG_OPTION_HANDLERS
427         DAEMON_OPTION_HANDLERS
428         STREAM_SSL_OPTION_HANDLERS
429
430         case OPT_PEER_CA_CERT:
431             stream_ssl_set_peer_ca_cert_file(optarg);
432             break;
433
434         case OPT_BOOTSTRAP_CA_CERT:
435             stream_ssl_set_ca_cert_file(optarg, true);
436             break;
437
438         case '?':
439             exit(EXIT_FAILURE);
440
441         default:
442             abort();
443         }
444     }
445     free(short_options);
446
447     argc -= optind;
448     argv += optind;
449
450     if (argc == 0) {
451         ovs_remote = xasprintf("unix:%s/db.sock", ovs_rundir());
452     } else if (argc == 1) {
453         ovs_remote = xstrdup(argv[0]);
454     } else {
455         VLOG_FATAL("exactly zero or one non-option argument required; "
456                    "use --help for usage");
457     }
458 }
459
460 static void
461 usage(void)
462 {
463     printf("%s: OVN controller\n"
464            "usage %s [OPTIONS] [OVS-DATABASE]\n"
465            "where OVS-DATABASE is a socket on which the OVS OVSDB server is listening.\n",
466                program_name, program_name);
467     stream_usage("OVS-DATABASE", true, false, false);
468     daemon_usage();
469     vlog_usage();
470     printf("\nOther options:\n"
471            "  -h, --help              display this help message\n"
472            "  -V, --version           display version information\n");
473     exit(EXIT_SUCCESS);
474 }
475
476 static void
477 ovn_controller_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
478              const char *argv[] OVS_UNUSED, void *exiting_)
479 {
480     bool *exiting = exiting_;
481     *exiting = true;
482
483     unixctl_command_reply(conn, NULL);
484 }
485
486 static void
487 ct_zone_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
488              const char *argv[] OVS_UNUSED, void *ct_zones_)
489 {
490     struct simap *ct_zones = ct_zones_;
491     struct ds ds = DS_EMPTY_INITIALIZER;
492     struct simap_node *zone;
493
494     SIMAP_FOR_EACH(zone, ct_zones) {
495         ds_put_format(&ds, "%s %d\n", zone->name, zone->data);
496     }
497
498     unixctl_command_reply(conn, ds_cstr(&ds));
499     ds_destroy(&ds);
500 }