daemon: Allow daemon child process to report success or failure to parent.
[sliver-openvswitch.git] / utilities / ovs-controller.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <getopt.h>
21 #include <limits.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 "fault.h"
30 #include "learning-switch.h"
31 #include "ofpbuf.h"
32 #include "openflow/openflow.h"
33 #include "poll-loop.h"
34 #include "rconn.h"
35 #include "timeval.h"
36 #include "unixctl.h"
37 #include "util.h"
38 #include "vconn-ssl.h"
39 #include "vconn.h"
40
41 #include "vlog.h"
42 #define THIS_MODULE VLM_controller
43
44 #define MAX_SWITCHES 16
45 #define MAX_LISTENERS 16
46
47 struct switch_ {
48     struct lswitch *lswitch;
49     struct rconn *rconn;
50 };
51
52 /* Learn the ports on which MAC addresses appear? */
53 static bool learn_macs = true;
54
55 /* Set up flows?  (If not, every packet is processed at the controller.) */
56 static bool set_up_flows = true;
57
58 /* -N, --normal: Use "NORMAL" action instead of explicit port? */
59 static bool action_normal = false;
60
61 /* -w, --wildcard: Set up exact match or wildcard flow entries? */
62 static bool exact_flows = true;
63
64 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
65 static int max_idle = 60;
66
67 /* --mute: If true, accept connections from switches but do not reply to any
68  * of their messages (for debugging fail-open mode). */
69 static bool mute = false;
70
71 static int do_switching(struct switch_ *);
72 static void new_switch(struct switch_ *, struct vconn *, const char *name);
73 static void parse_options(int argc, char *argv[]);
74 static void usage(void) NO_RETURN;
75
76 int
77 main(int argc, char *argv[])
78 {
79     struct unixctl_server *unixctl;
80     struct switch_ switches[MAX_SWITCHES];
81     struct pvconn *listeners[MAX_LISTENERS];
82     int n_switches, n_listeners;
83     int retval;
84     int i;
85
86     set_program_name(argv[0]);
87     register_fault_handlers();
88     time_init();
89     vlog_init();
90     parse_options(argc, argv);
91     signal(SIGPIPE, SIG_IGN);
92
93     if (argc - optind < 1) {
94         ovs_fatal(0, "at least one vconn argument required; "
95                   "use --help for usage");
96     }
97
98     n_switches = n_listeners = 0;
99     for (i = optind; i < argc; i++) {
100         const char *name = argv[i];
101         struct vconn *vconn;
102         int retval;
103
104         retval = vconn_open(name, OFP_VERSION, &vconn);
105         if (!retval) {
106             if (n_switches >= MAX_SWITCHES) {
107                 ovs_fatal(0, "max %d switch connections", n_switches);
108             }
109             new_switch(&switches[n_switches++], vconn, name);
110             continue;
111         } else if (retval == EAFNOSUPPORT) {
112             struct pvconn *pvconn;
113             retval = pvconn_open(name, &pvconn);
114             if (!retval) {
115                 if (n_listeners >= MAX_LISTENERS) {
116                     ovs_fatal(0, "max %d passive connections", n_listeners);
117                 }
118                 listeners[n_listeners++] = pvconn;
119             }
120         }
121         if (retval) {
122             VLOG_ERR("%s: connect: %s", name, strerror(retval));
123         }
124     }
125     if (n_switches == 0 && n_listeners == 0) {
126         ovs_fatal(0, "no active or passive switch connections");
127     }
128
129     die_if_already_running();
130     daemonize_start();
131
132     retval = unixctl_server_create(NULL, &unixctl);
133     if (retval) {
134         ovs_fatal(retval, "Could not listen for unixctl connections");
135     }
136
137     daemonize_complete();
138
139     while (n_switches > 0 || n_listeners > 0) {
140         int iteration;
141         int i;
142
143         /* Accept connections on listening vconns. */
144         for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
145             struct vconn *new_vconn;
146             int retval;
147
148             retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
149             if (!retval || retval == EAGAIN) {
150                 if (!retval) {
151                     new_switch(&switches[n_switches++], new_vconn, "tcp");
152                 }
153                 i++;
154             } else {
155                 pvconn_close(listeners[i]);
156                 listeners[i] = listeners[--n_listeners];
157             }
158         }
159
160         /* Do some switching work.  Limit the number of iterations so that
161          * callbacks registered with the poll loop don't starve. */
162         for (iteration = 0; iteration < 50; iteration++) {
163             bool progress = false;
164             for (i = 0; i < n_switches; ) {
165                 struct switch_ *this = &switches[i];
166                 int retval = do_switching(this);
167                 if (!retval || retval == EAGAIN) {
168                     if (!retval) {
169                         progress = true;
170                     }
171                     i++;
172                 } else {
173                     rconn_destroy(this->rconn);
174                     lswitch_destroy(this->lswitch);
175                     switches[i] = switches[--n_switches];
176                 }
177             }
178             if (!progress) {
179                 break;
180             }
181         }
182         for (i = 0; i < n_switches; i++) {
183             struct switch_ *this = &switches[i];
184             lswitch_run(this->lswitch, this->rconn);
185         }
186
187         unixctl_server_run(unixctl);
188
189         /* Wait for something to happen. */
190         if (n_switches < MAX_SWITCHES) {
191             for (i = 0; i < n_listeners; i++) {
192                 pvconn_wait(listeners[i]);
193             }
194         }
195         for (i = 0; i < n_switches; i++) {
196             struct switch_ *sw = &switches[i];
197             rconn_run_wait(sw->rconn);
198             rconn_recv_wait(sw->rconn);
199             lswitch_wait(sw->lswitch);
200         }
201         unixctl_server_wait(unixctl);
202         poll_block();
203     }
204
205     return 0;
206 }
207
208 static void
209 new_switch(struct switch_ *sw, struct vconn *vconn, const char *name)
210 {
211     sw->rconn = rconn_new_from_vconn(name, vconn);
212     sw->lswitch = lswitch_create(sw->rconn, learn_macs, exact_flows,
213                                  set_up_flows ? max_idle : -1,
214                                  action_normal);
215 }
216
217 static int
218 do_switching(struct switch_ *sw)
219 {
220     unsigned int packets_sent;
221     struct ofpbuf *msg;
222
223     packets_sent = rconn_packets_sent(sw->rconn);
224
225     msg = rconn_recv(sw->rconn);
226     if (msg) {
227         if (!mute) {
228             lswitch_process_packet(sw->lswitch, sw->rconn, msg);
229         }
230         ofpbuf_delete(msg);
231     }
232     rconn_run(sw->rconn);
233
234     return (!rconn_is_alive(sw->rconn) ? EOF
235             : rconn_packets_sent(sw->rconn) != packets_sent ? 0
236             : EAGAIN);
237 }
238
239 static void
240 parse_options(int argc, char *argv[])
241 {
242     enum {
243         OPT_MAX_IDLE = UCHAR_MAX + 1,
244         OPT_PEER_CA_CERT,
245         OPT_MUTE,
246         VLOG_OPTION_ENUMS
247     };
248     static struct option long_options[] = {
249         {"hub",         no_argument, 0, 'H'},
250         {"noflow",      no_argument, 0, 'n'},
251         {"normal",      no_argument, 0, 'N'},
252         {"wildcard",    no_argument, 0, 'w'},
253         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
254         {"mute",        no_argument, 0, OPT_MUTE},
255         {"help",        no_argument, 0, 'h'},
256         {"version",     no_argument, 0, 'V'},
257         DAEMON_LONG_OPTIONS,
258         VLOG_LONG_OPTIONS,
259 #ifdef HAVE_OPENSSL
260         VCONN_SSL_LONG_OPTIONS
261         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
262 #endif
263         {0, 0, 0, 0},
264     };
265     char *short_options = long_options_to_short_options(long_options);
266
267     for (;;) {
268         int indexptr;
269         int c;
270
271         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
272         if (c == -1) {
273             break;
274         }
275
276         switch (c) {
277         case 'H':
278             learn_macs = false;
279             break;
280
281         case 'n':
282             set_up_flows = false;
283             break;
284
285         case OPT_MUTE:
286             mute = true;
287             break;
288
289         case 'N':
290             action_normal = true;
291             break;
292
293         case 'w':
294             exact_flows = false;
295             break;
296
297         case OPT_MAX_IDLE:
298             if (!strcmp(optarg, "permanent")) {
299                 max_idle = OFP_FLOW_PERMANENT;
300             } else {
301                 max_idle = atoi(optarg);
302                 if (max_idle < 1 || max_idle > 65535) {
303                     ovs_fatal(0, "--max-idle argument must be between 1 and "
304                               "65535 or the word 'permanent'");
305                 }
306             }
307             break;
308
309         case 'h':
310             usage();
311
312         case 'V':
313             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
314             exit(EXIT_SUCCESS);
315
316         VLOG_OPTION_HANDLERS
317         DAEMON_OPTION_HANDLERS
318
319 #ifdef HAVE_OPENSSL
320         VCONN_SSL_OPTION_HANDLERS
321
322         case OPT_PEER_CA_CERT:
323             vconn_ssl_set_peer_ca_cert_file(optarg);
324             break;
325 #endif
326
327         case '?':
328             exit(EXIT_FAILURE);
329
330         default:
331             abort();
332         }
333     }
334     free(short_options);
335 }
336
337 static void
338 usage(void)
339 {
340     printf("%s: OpenFlow controller\n"
341            "usage: %s [OPTIONS] METHOD\n"
342            "where METHOD is any OpenFlow connection method.\n",
343            program_name, program_name);
344     vconn_usage(true, true, false);
345     daemon_usage();
346     vlog_usage();
347     printf("\nOther options:\n"
348            "  -H, --hub               act as hub instead of learning switch\n"
349            "  -n, --noflow            pass traffic, but don't add flows\n"
350            "  --max-idle=SECS         max idle time for new flows\n"
351            "  -N, --normal            use OFPAT_NORMAL action\n"
352            "  -w, --wildcard          use wildcards, not exact-match rules\n"
353            "  -h, --help              display this help message\n"
354            "  -V, --version           display version information\n");
355     exit(EXIT_SUCCESS);
356 }