3288e6f33291f19c2548550e9905124b26aa5346
[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         int retval;
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         int i;
150
151         /* Accept connections on listening vconns. */
152         for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
153             struct vconn *new_vconn;
154             int retval;
155
156             retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
157             if (!retval || retval == EAGAIN) {
158                 if (!retval) {
159                     new_switch(&switches[n_switches++], new_vconn);
160                 }
161                 i++;
162             } else {
163                 pvconn_close(listeners[i]);
164                 listeners[i] = listeners[--n_listeners];
165             }
166         }
167
168         /* Do some switching work.  Limit the number of iterations so that
169          * callbacks registered with the poll loop don't starve. */
170         for (iteration = 0; iteration < 50; iteration++) {
171             bool progress = false;
172             for (i = 0; i < n_switches; ) {
173                 struct switch_ *this = &switches[i];
174                 int retval = do_switching(this);
175                 if (!retval || retval == EAGAIN) {
176                     if (!retval) {
177                         progress = true;
178                     }
179                     i++;
180                 } else {
181                     rconn_destroy(this->rconn);
182                     lswitch_destroy(this->lswitch);
183                     switches[i] = switches[--n_switches];
184                 }
185             }
186             if (!progress) {
187                 break;
188             }
189         }
190         for (i = 0; i < n_switches; i++) {
191             struct switch_ *this = &switches[i];
192             lswitch_run(this->lswitch);
193         }
194
195         unixctl_server_run(unixctl);
196
197         /* Wait for something to happen. */
198         if (n_switches < MAX_SWITCHES) {
199             for (i = 0; i < n_listeners; i++) {
200                 pvconn_wait(listeners[i]);
201             }
202         }
203         for (i = 0; i < n_switches; i++) {
204             struct switch_ *sw = &switches[i];
205             rconn_run_wait(sw->rconn);
206             rconn_recv_wait(sw->rconn);
207             lswitch_wait(sw->lswitch);
208         }
209         unixctl_server_wait(unixctl);
210         poll_block();
211     }
212
213     return 0;
214 }
215
216 static void
217 new_switch(struct switch_ *sw, struct vconn *vconn)
218 {
219     sw->rconn = rconn_create(60, 0);
220     rconn_connect_unreliably(sw->rconn, vconn, NULL);
221
222     /* If it was set, rewind 'flow_file' to the beginning, since a
223      * previous call to lswitch_create() will leave the stream at the
224      * end. */
225     if (flow_file) {
226         rewind(flow_file);
227     }
228     sw->lswitch = lswitch_create(sw->rconn, learn_macs, exact_flows,
229                                  set_up_flows ? max_idle : -1,
230                                  action_normal, flow_file);
231
232     lswitch_set_queue(sw->lswitch, queue_id);
233 }
234
235 static int
236 do_switching(struct switch_ *sw)
237 {
238     unsigned int packets_sent;
239     struct ofpbuf *msg;
240
241     packets_sent = rconn_packets_sent(sw->rconn);
242
243     msg = rconn_recv(sw->rconn);
244     if (msg) {
245         if (!mute) {
246             lswitch_process_packet(sw->lswitch, sw->rconn, msg);
247         }
248         ofpbuf_delete(msg);
249     }
250     rconn_run(sw->rconn);
251
252     return (!rconn_is_alive(sw->rconn) ? EOF
253             : rconn_packets_sent(sw->rconn) != packets_sent ? 0
254             : EAGAIN);
255 }
256
257 static void
258 parse_options(int argc, char *argv[])
259 {
260     enum {
261         OPT_MAX_IDLE = UCHAR_MAX + 1,
262         OPT_PEER_CA_CERT,
263         OPT_MUTE,
264         OPT_WITH_FLOWS,
265         OPT_UNIXCTL,
266         VLOG_OPTION_ENUMS
267     };
268     static struct option long_options[] = {
269         {"hub",         no_argument, 0, 'H'},
270         {"noflow",      no_argument, 0, 'n'},
271         {"normal",      no_argument, 0, 'N'},
272         {"wildcard",    no_argument, 0, 'w'},
273         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
274         {"mute",        no_argument, 0, OPT_MUTE},
275         {"queue",       required_argument, 0, 'q'},
276         {"with-flows",  required_argument, 0, OPT_WITH_FLOWS},
277         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
278         {"help",        no_argument, 0, 'h'},
279         {"version",     no_argument, 0, 'V'},
280         DAEMON_LONG_OPTIONS,
281         VLOG_LONG_OPTIONS,
282 #ifdef HAVE_OPENSSL
283         STREAM_SSL_LONG_OPTIONS
284         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
285 #endif
286         {0, 0, 0, 0},
287     };
288     char *short_options = long_options_to_short_options(long_options);
289
290     for (;;) {
291         int indexptr;
292         int c;
293
294         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
295         if (c == -1) {
296             break;
297         }
298
299         switch (c) {
300         case 'H':
301             learn_macs = false;
302             break;
303
304         case 'n':
305             set_up_flows = false;
306             break;
307
308         case OPT_MUTE:
309             mute = true;
310             break;
311
312         case 'N':
313             action_normal = true;
314             break;
315
316         case 'w':
317             exact_flows = false;
318             break;
319
320         case OPT_MAX_IDLE:
321             if (!strcmp(optarg, "permanent")) {
322                 max_idle = OFP_FLOW_PERMANENT;
323             } else {
324                 max_idle = atoi(optarg);
325                 if (max_idle < 1 || max_idle > 65535) {
326                     ovs_fatal(0, "--max-idle argument must be between 1 and "
327                               "65535 or the word 'permanent'");
328                 }
329             }
330             break;
331
332         case 'q':
333             queue_id = atoi(optarg);
334             break;
335
336         case OPT_WITH_FLOWS:
337             flow_file = fopen(optarg, "r");
338             if (flow_file == NULL) {
339                 ovs_fatal(errno, "%s: open", optarg);
340             }
341             break;
342
343         case OPT_UNIXCTL:
344             unixctl_path = optarg;
345             break;
346
347         case 'h':
348             usage();
349
350         case 'V':
351             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
352             exit(EXIT_SUCCESS);
353
354         VLOG_OPTION_HANDLERS
355         DAEMON_OPTION_HANDLERS
356
357 #ifdef HAVE_OPENSSL
358         STREAM_SSL_OPTION_HANDLERS
359
360         case OPT_PEER_CA_CERT:
361             stream_ssl_set_peer_ca_cert_file(optarg);
362             break;
363 #endif
364
365         case '?':
366             exit(EXIT_FAILURE);
367
368         default:
369             abort();
370         }
371     }
372     free(short_options);
373 }
374
375 static void
376 usage(void)
377 {
378     printf("%s: OpenFlow controller\n"
379            "usage: %s [OPTIONS] METHOD\n"
380            "where METHOD is any OpenFlow connection method.\n",
381            program_name, program_name);
382     vconn_usage(true, true, false);
383     daemon_usage();
384     vlog_usage();
385     printf("\nOther options:\n"
386            "  -H, --hub               act as hub instead of learning switch\n"
387            "  -n, --noflow            pass traffic, but don't add flows\n"
388            "  --max-idle=SECS         max idle time for new flows\n"
389            "  -N, --normal            use OFPAT_NORMAL action\n"
390            "  -w, --wildcard          use wildcards, not exact-match rules\n"
391            "  -q, --queue=QUEUE       OpenFlow queue ID to use for output\n"
392            "  --with-flows FILE       use the flows from FILE\n"
393            "  --unixctl=SOCKET        override default control socket name\n"
394            "  -h, --help              display this help message\n"
395            "  -V, --version           display version information\n");
396     exit(EXIT_SUCCESS);
397 }