Avoid shadowing local variable names.
[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 <stdio.h>
25 #include <string.h>
26
27 #include "command-line.h"
28 #include "compiler.h"
29 #include "daemon.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 "stream-ssl.h"
36 #include "timeval.h"
37 #include "unixctl.h"
38 #include "util.h"
39 #include "vconn.h"
40 #include "vlog.h"
41
42 VLOG_DEFINE_THIS_MODULE(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 /* -q, --queue: OpenFlow queue to use, or the default queue if UINT32_MAX. */
72 static uint32_t queue_id = UINT32_MAX;
73
74 /* --with-flows: File with flows to send to switch, or null to not load
75  * any default flows. */
76 static FILE *flow_file = NULL;
77
78 /* --unixctl: Name of unixctl socket, or null to use the default. */
79 static char *unixctl_path = NULL;
80
81 static int do_switching(struct switch_ *);
82 static void new_switch(struct switch_ *, struct vconn *);
83 static void parse_options(int argc, char *argv[]);
84 static void usage(void) NO_RETURN;
85
86 int
87 main(int argc, char *argv[])
88 {
89     struct unixctl_server *unixctl;
90     struct switch_ switches[MAX_SWITCHES];
91     struct pvconn *listeners[MAX_LISTENERS];
92     int n_switches, n_listeners;
93     int retval;
94     int i;
95
96     proctitle_init(argc, argv);
97     set_program_name(argv[0]);
98     parse_options(argc, argv);
99     signal(SIGPIPE, SIG_IGN);
100
101     if (argc - optind < 1) {
102         ovs_fatal(0, "at least one vconn argument required; "
103                   "use --help for usage");
104     }
105
106     n_switches = n_listeners = 0;
107     for (i = optind; i < argc; i++) {
108         const char *name = argv[i];
109         struct vconn *vconn;
110
111         retval = vconn_open(name, OFP_VERSION, &vconn);
112         if (!retval) {
113             if (n_switches >= MAX_SWITCHES) {
114                 ovs_fatal(0, "max %d switch connections", n_switches);
115             }
116             new_switch(&switches[n_switches++], vconn);
117             continue;
118         } else if (retval == EAFNOSUPPORT) {
119             struct pvconn *pvconn;
120             retval = pvconn_open(name, &pvconn);
121             if (!retval) {
122                 if (n_listeners >= MAX_LISTENERS) {
123                     ovs_fatal(0, "max %d passive connections", n_listeners);
124                 }
125                 listeners[n_listeners++] = pvconn;
126             }
127         }
128         if (retval) {
129             VLOG_ERR("%s: connect: %s", name, strerror(retval));
130         }
131     }
132     if (n_switches == 0 && n_listeners == 0) {
133         ovs_fatal(0, "no active or passive switch connections");
134     }
135
136     die_if_already_running();
137     daemonize_start();
138
139     retval = unixctl_server_create(unixctl_path, &unixctl);
140     if (retval) {
141         exit(EXIT_FAILURE);
142     }
143
144     daemonize_complete();
145
146     while (n_switches > 0 || n_listeners > 0) {
147         int iteration;
148
149         /* Accept connections on listening vconns. */
150         for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
151             struct vconn *new_vconn;
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
172                 retval = do_switching(this);
173                 if (!retval || retval == EAGAIN) {
174                     if (!retval) {
175                         progress = true;
176                     }
177                     i++;
178                 } else {
179                     rconn_destroy(this->rconn);
180                     lswitch_destroy(this->lswitch);
181                     switches[i] = switches[--n_switches];
182                 }
183             }
184             if (!progress) {
185                 break;
186             }
187         }
188         for (i = 0; i < n_switches; i++) {
189             struct switch_ *this = &switches[i];
190             lswitch_run(this->lswitch);
191         }
192
193         unixctl_server_run(unixctl);
194
195         /* Wait for something to happen. */
196         if (n_switches < MAX_SWITCHES) {
197             for (i = 0; i < n_listeners; i++) {
198                 pvconn_wait(listeners[i]);
199             }
200         }
201         for (i = 0; i < n_switches; i++) {
202             struct switch_ *sw = &switches[i];
203             rconn_run_wait(sw->rconn);
204             rconn_recv_wait(sw->rconn);
205             lswitch_wait(sw->lswitch);
206         }
207         unixctl_server_wait(unixctl);
208         poll_block();
209     }
210
211     return 0;
212 }
213
214 static void
215 new_switch(struct switch_ *sw, struct vconn *vconn)
216 {
217     sw->rconn = rconn_create(60, 0);
218     rconn_connect_unreliably(sw->rconn, vconn, NULL);
219
220     /* If it was set, rewind 'flow_file' to the beginning, since a
221      * previous call to lswitch_create() will leave the stream at the
222      * end. */
223     if (flow_file) {
224         rewind(flow_file);
225     }
226     sw->lswitch = lswitch_create(sw->rconn, learn_macs, exact_flows,
227                                  set_up_flows ? max_idle : -1,
228                                  action_normal, flow_file);
229
230     lswitch_set_queue(sw->lswitch, queue_id);
231 }
232
233 static int
234 do_switching(struct switch_ *sw)
235 {
236     unsigned int packets_sent;
237     struct ofpbuf *msg;
238
239     packets_sent = rconn_packets_sent(sw->rconn);
240
241     msg = rconn_recv(sw->rconn);
242     if (msg) {
243         if (!mute) {
244             lswitch_process_packet(sw->lswitch, sw->rconn, msg);
245         }
246         ofpbuf_delete(msg);
247     }
248     rconn_run(sw->rconn);
249
250     return (!rconn_is_alive(sw->rconn) ? EOF
251             : rconn_packets_sent(sw->rconn) != packets_sent ? 0
252             : EAGAIN);
253 }
254
255 static void
256 parse_options(int argc, char *argv[])
257 {
258     enum {
259         OPT_MAX_IDLE = UCHAR_MAX + 1,
260         OPT_PEER_CA_CERT,
261         OPT_MUTE,
262         OPT_WITH_FLOWS,
263         OPT_UNIXCTL,
264         VLOG_OPTION_ENUMS
265     };
266     static struct option long_options[] = {
267         {"hub",         no_argument, 0, 'H'},
268         {"noflow",      no_argument, 0, 'n'},
269         {"normal",      no_argument, 0, 'N'},
270         {"wildcard",    no_argument, 0, 'w'},
271         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
272         {"mute",        no_argument, 0, OPT_MUTE},
273         {"queue",       required_argument, 0, 'q'},
274         {"with-flows",  required_argument, 0, OPT_WITH_FLOWS},
275         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
276         {"help",        no_argument, 0, 'h'},
277         {"version",     no_argument, 0, 'V'},
278         DAEMON_LONG_OPTIONS,
279         VLOG_LONG_OPTIONS,
280 #ifdef HAVE_OPENSSL
281         STREAM_SSL_LONG_OPTIONS
282         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
283 #endif
284         {0, 0, 0, 0},
285     };
286     char *short_options = long_options_to_short_options(long_options);
287
288     for (;;) {
289         int indexptr;
290         int c;
291
292         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
293         if (c == -1) {
294             break;
295         }
296
297         switch (c) {
298         case 'H':
299             learn_macs = false;
300             break;
301
302         case 'n':
303             set_up_flows = false;
304             break;
305
306         case OPT_MUTE:
307             mute = true;
308             break;
309
310         case 'N':
311             action_normal = true;
312             break;
313
314         case 'w':
315             exact_flows = false;
316             break;
317
318         case OPT_MAX_IDLE:
319             if (!strcmp(optarg, "permanent")) {
320                 max_idle = OFP_FLOW_PERMANENT;
321             } else {
322                 max_idle = atoi(optarg);
323                 if (max_idle < 1 || max_idle > 65535) {
324                     ovs_fatal(0, "--max-idle argument must be between 1 and "
325                               "65535 or the word 'permanent'");
326                 }
327             }
328             break;
329
330         case 'q':
331             queue_id = atoi(optarg);
332             break;
333
334         case OPT_WITH_FLOWS:
335             flow_file = fopen(optarg, "r");
336             if (flow_file == NULL) {
337                 ovs_fatal(errno, "%s: open", optarg);
338             }
339             break;
340
341         case OPT_UNIXCTL:
342             unixctl_path = optarg;
343             break;
344
345         case 'h':
346             usage();
347
348         case 'V':
349             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
350             exit(EXIT_SUCCESS);
351
352         VLOG_OPTION_HANDLERS
353         DAEMON_OPTION_HANDLERS
354
355 #ifdef HAVE_OPENSSL
356         STREAM_SSL_OPTION_HANDLERS
357
358         case OPT_PEER_CA_CERT:
359             stream_ssl_set_peer_ca_cert_file(optarg);
360             break;
361 #endif
362
363         case '?':
364             exit(EXIT_FAILURE);
365
366         default:
367             abort();
368         }
369     }
370     free(short_options);
371 }
372
373 static void
374 usage(void)
375 {
376     printf("%s: OpenFlow controller\n"
377            "usage: %s [OPTIONS] METHOD\n"
378            "where METHOD is any OpenFlow connection method.\n",
379            program_name, program_name);
380     vconn_usage(true, true, false);
381     daemon_usage();
382     vlog_usage();
383     printf("\nOther options:\n"
384            "  -H, --hub               act as hub instead of learning switch\n"
385            "  -n, --noflow            pass traffic, but don't add flows\n"
386            "  --max-idle=SECS         max idle time for new flows\n"
387            "  -N, --normal            use OFPAT_NORMAL action\n"
388            "  -w, --wildcard          use wildcards, not exact-match rules\n"
389            "  -q, --queue=QUEUE       OpenFlow queue ID to use for output\n"
390            "  --with-flows FILE       use the flows from FILE\n"
391            "  --unixctl=SOCKET        override default control socket name\n"
392            "  -h, --help              display this help message\n"
393            "  -V, --version           display version information\n");
394     exit(EXIT_SUCCESS);
395 }