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