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