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