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