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