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