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