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