ovsdb: Refactor JSON-RPC database server implementation.
[sliver-openvswitch.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009 Nicira Networks
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 "ovsdb.h"
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <signal.h>
23
24 #include "command-line.h"
25 #include "daemon.h"
26 #include "fault.h"
27 #include "file.h"
28 #include "json.h"
29 #include "jsonrpc.h"
30 #include "jsonrpc-server.h"
31 #include "leak-checker.h"
32 #include "list.h"
33 #include "ovsdb-error.h"
34 #include "poll-loop.h"
35 #include "process.h"
36 #include "stream.h"
37 #include "svec.h"
38 #include "timeval.h"
39 #include "trigger.h"
40 #include "util.h"
41 #include "unixctl.h"
42
43 #include "vlog.h"
44 #define THIS_MODULE VLM_ovsdb_server
45
46 static const struct jsonrpc_server_cbs ovsdb_jsonrpc_cbs;
47
48 static void parse_options(int argc, char *argv[], char **file_namep,
49                           struct svec *active, struct svec *passive);
50 static void usage(void) NO_RETURN;
51
52 int
53 main(int argc, char *argv[])
54 {
55     struct unixctl_server *unixctl;
56     struct ovsdb_jsonrpc_server *jsonrpc;
57     struct svec active, passive;
58     struct ovsdb_error *error;
59     struct ovsdb *db;
60     const char *name;
61     char *file_name;
62     int retval;
63     size_t i;
64
65     set_program_name(argv[0]);
66     register_fault_handlers();
67     time_init();
68     vlog_init();
69     signal(SIGPIPE, SIG_IGN);
70     process_init();
71
72     parse_options(argc, argv, &file_name, &active, &passive);
73
74     error = ovsdb_file_open(file_name, false, &db);
75     if (error) {
76         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
77     }
78
79     jsonrpc = ovsdb_jsonrpc_server_create(db);
80     SVEC_FOR_EACH (i, name, &active) {
81         ovsdb_jsonrpc_server_connect(jsonrpc, name);
82     }
83     SVEC_FOR_EACH (i, name, &passive) {
84         retval = ovsdb_jsonrpc_server_listen(jsonrpc, name);
85         if (retval) {
86             ovs_fatal(retval, "failed to listen on %s", name);
87         }
88     }
89     svec_destroy(&active);
90     svec_destroy(&passive);
91
92     die_if_already_running();
93     daemonize();
94
95     retval = unixctl_server_create(NULL, &unixctl);
96     if (retval) {
97         ovs_fatal(retval, "could not listen for control connections");
98     }
99
100     for (;;) {
101         ovsdb_jsonrpc_server_run(jsonrpc);
102         unixctl_server_run(unixctl);
103         ovsdb_trigger_run(db, time_msec());
104
105         ovsdb_jsonrpc_server_wait(jsonrpc);
106         unixctl_server_wait(unixctl);
107         ovsdb_trigger_wait(db, time_msec());
108         poll_block();
109     }
110
111     return 0;
112 }
113
114 static void
115 parse_options(int argc, char *argv[], char **file_namep,
116               struct svec *active, struct svec *passive)
117 {
118     enum {
119         OPT_DUMMY = UCHAR_MAX + 1,
120         OPT_CONNECT,
121         OPT_LISTEN,
122         VLOG_OPTION_ENUMS,
123         LEAK_CHECKER_OPTION_ENUMS
124     };
125     static struct option long_options[] = {
126         {"connect",     required_argument, 0, OPT_CONNECT},
127         {"listen",      required_argument, 0, OPT_LISTEN},
128         {"help",        no_argument, 0, 'h'},
129         {"version",     no_argument, 0, 'V'},
130         DAEMON_LONG_OPTIONS,
131         VLOG_LONG_OPTIONS,
132         LEAK_CHECKER_LONG_OPTIONS,
133         {0, 0, 0, 0},
134     };
135     char *short_options = long_options_to_short_options(long_options);
136
137     svec_init(active);
138     svec_init(passive);
139     for (;;) {
140         int c;
141
142         c = getopt_long(argc, argv, short_options, long_options, NULL);
143         if (c == -1) {
144             break;
145         }
146
147         switch (c) {
148         case OPT_CONNECT:
149             svec_add(active, optarg);
150             break;
151
152         case OPT_LISTEN:
153             svec_add(passive, optarg);
154             break;
155
156         case 'h':
157             usage();
158
159         case 'V':
160             OVS_PRINT_VERSION(0, 0);
161             exit(EXIT_SUCCESS);
162
163         VLOG_OPTION_HANDLERS
164         DAEMON_OPTION_HANDLERS
165         LEAK_CHECKER_OPTION_HANDLERS
166
167         case '?':
168             exit(EXIT_FAILURE);
169
170         default:
171             abort();
172         }
173     }
174     free(short_options);
175
176     argc -= optind;
177     argv += optind;
178
179     if (argc != 1) {
180         ovs_fatal(0, "database file is only non-option argument; "
181                 "use --help for usage");
182     }
183
184     *file_namep = argv[0];
185 }
186
187 static void
188 usage(void)
189 {
190     printf("%s: Open vSwitch database server\n"
191            "usage: %s [OPTIONS] DATABASE\n"
192            "where DATABASE is a database file in ovsdb format.\n",
193            program_name, program_name);
194     printf("\nJSON-RPC options (may be specified any number of times):\n"
195            "  --connect=REMOTE        make active connection to REMOTE\n"
196            "  --listen=LOCAL          passively listen on LOCAL\n");
197     stream_usage("JSON-RPC", true, true);
198     daemon_usage();
199     vlog_usage();
200     printf("\nOther options:\n"
201            "  -h, --help              display this help message\n"
202            "  -V, --version           display version information\n");
203     leak_checker_usage();
204     exit(EXIT_SUCCESS);
205 }