rconn: Add allowed OpenFlow versions
[openvswitch] / utilities / ovs-controller.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 "simap.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 #include "socket-util.h"
44 #include "ofp-util.h"
45
46 VLOG_DEFINE_THIS_MODULE(controller);
47
48 #define MAX_SWITCHES 16
49 #define MAX_LISTENERS 16
50
51 struct switch_ {
52     struct lswitch *lswitch;
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: 0 to disable wildcard flow entries, an OFPFW10_* bitmask to
66  * enable specific wildcards, or UINT32_MAX to use the default wildcards. */
67 static uint32_t wildcards = 0;
68
69 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
70 static int max_idle = 60;
71
72 /* --mute: If true, accept connections from switches but do not reply to any
73  * of their messages (for debugging fail-open mode). */
74 static bool mute = false;
75
76 /* -q, --queue: default OpenFlow queue, none if UINT32_MAX. */
77 static uint32_t default_queue = UINT32_MAX;
78
79 /* -Q, --port-queue: map from port name to port number. */
80 static struct simap port_queues = SIMAP_INITIALIZER(&port_queues);
81
82 /* --with-flows: Flows to send to switch. */
83 static struct ofputil_flow_mod *default_flows;
84 static size_t n_default_flows;
85
86 /* --unixctl: Name of unixctl socket, or null to use the default. */
87 static char *unixctl_path = NULL;
88
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, 0, &vconn, DSCP_DEFAULT);
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, 0, &pvconn, DSCP_DEFAULT);
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     daemonize_start();
144
145     retval = unixctl_server_create(unixctl_path, &unixctl);
146     if (retval) {
147         exit(EXIT_FAILURE);
148     }
149
150     daemonize_complete();
151
152     while (n_switches > 0 || n_listeners > 0) {
153         /* Accept connections on listening vconns. */
154         for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
155             struct vconn *new_vconn;
156
157             retval = pvconn_accept(listeners[i], &new_vconn);
158             if (!retval || retval == EAGAIN) {
159                 if (!retval) {
160                     new_switch(&switches[n_switches++], new_vconn);
161                 }
162                 i++;
163             } else {
164                 pvconn_close(listeners[i]);
165                 listeners[i] = listeners[--n_listeners];
166             }
167         }
168
169         /* Do some switching work.  . */
170         for (i = 0; i < n_switches; ) {
171             struct switch_ *this = &switches[i];
172             lswitch_run(this->lswitch);
173             if (lswitch_is_alive(this->lswitch)) {
174                 i++;
175             } else {
176                 lswitch_destroy(this->lswitch);
177                 switches[i] = switches[--n_switches];
178             }
179         }
180
181         unixctl_server_run(unixctl);
182
183         /* Wait for something to happen. */
184         if (n_switches < MAX_SWITCHES) {
185             for (i = 0; i < n_listeners; i++) {
186                 pvconn_wait(listeners[i]);
187             }
188         }
189         for (i = 0; i < n_switches; i++) {
190             struct switch_ *sw = &switches[i];
191             lswitch_wait(sw->lswitch);
192         }
193         unixctl_server_wait(unixctl);
194         poll_block();
195     }
196
197     return 0;
198 }
199
200 static void
201 new_switch(struct switch_ *sw, struct vconn *vconn)
202 {
203     struct lswitch_config cfg;
204     struct rconn *rconn;
205
206     rconn = rconn_create(60, 0, DSCP_DEFAULT, 0);
207     rconn_connect_unreliably(rconn, vconn, NULL);
208
209     cfg.mode = (action_normal ? LSW_NORMAL
210                 : learn_macs ? LSW_LEARN
211                 : LSW_FLOOD);
212     cfg.wildcards = wildcards;
213     cfg.max_idle = set_up_flows ? max_idle : -1;
214     cfg.default_flows = default_flows;
215     cfg.n_default_flows = n_default_flows;
216     cfg.default_queue = default_queue;
217     cfg.port_queues = &port_queues;
218     cfg.mute = mute;
219     sw->lswitch = lswitch_create(rconn, &cfg);
220 }
221
222 static void
223 add_port_queue(char *s)
224 {
225     char *save_ptr = NULL;
226     char *port_name;
227     char *queue_id;
228
229     port_name = strtok_r(s, ":", &save_ptr);
230     queue_id = strtok_r(NULL, "", &save_ptr);
231     if (!queue_id) {
232         ovs_fatal(0, "argument to -Q or --port-queue should take the form "
233                   "\"<port-name>:<queue-id>\"");
234     }
235
236     if (!simap_put(&port_queues, port_name, atoi(queue_id))) {
237         ovs_fatal(0, "<port-name> arguments for -Q or --port-queue must "
238                   "be unique");
239     }
240 }
241
242 static void
243 parse_options(int argc, char *argv[])
244 {
245     enum {
246         OPT_MAX_IDLE = UCHAR_MAX + 1,
247         OPT_PEER_CA_CERT,
248         OPT_MUTE,
249         OPT_WITH_FLOWS,
250         OPT_UNIXCTL,
251         VLOG_OPTION_ENUMS,
252         DAEMON_OPTION_ENUMS
253     };
254     static struct option long_options[] = {
255         {"hub",         no_argument, NULL, 'H'},
256         {"noflow",      no_argument, NULL, 'n'},
257         {"normal",      no_argument, NULL, 'N'},
258         {"wildcards",   optional_argument, NULL, 'w'},
259         {"max-idle",    required_argument, NULL, OPT_MAX_IDLE},
260         {"mute",        no_argument, NULL, OPT_MUTE},
261         {"queue",       required_argument, NULL, 'q'},
262         {"port-queue",  required_argument, NULL, 'Q'},
263         {"with-flows",  required_argument, NULL, OPT_WITH_FLOWS},
264         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
265         {"help",        no_argument, NULL, 'h'},
266         {"version",     no_argument, NULL, 'V'},
267         DAEMON_LONG_OPTIONS,
268         VLOG_LONG_OPTIONS,
269         STREAM_SSL_LONG_OPTIONS,
270         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
271         {NULL, 0, NULL, 0},
272     };
273     char *short_options = long_options_to_short_options(long_options);
274
275     for (;;) {
276         int indexptr;
277         int c;
278
279         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
280         if (c == -1) {
281             break;
282         }
283
284         switch (c) {
285         case 'H':
286             learn_macs = false;
287             break;
288
289         case 'n':
290             set_up_flows = false;
291             break;
292
293         case OPT_MUTE:
294             mute = true;
295             break;
296
297         case 'N':
298             action_normal = true;
299             break;
300
301         case 'w':
302             wildcards = optarg ? strtol(optarg, NULL, 16) : UINT32_MAX;
303             break;
304
305         case OPT_MAX_IDLE:
306             if (!strcmp(optarg, "permanent")) {
307                 max_idle = OFP_FLOW_PERMANENT;
308             } else {
309                 max_idle = atoi(optarg);
310                 if (max_idle < 1 || max_idle > 65535) {
311                     ovs_fatal(0, "--max-idle argument must be between 1 and "
312                               "65535 or the word 'permanent'");
313                 }
314             }
315             break;
316
317         case 'q':
318             default_queue = atoi(optarg);
319             break;
320
321         case 'Q':
322             add_port_queue(optarg);
323             break;
324
325         case OPT_WITH_FLOWS:
326             parse_ofp_flow_mod_file(optarg, OFPFC_ADD, &default_flows,
327                                     &n_default_flows);
328             break;
329
330         case OPT_UNIXCTL:
331             unixctl_path = optarg;
332             break;
333
334         case 'h':
335             usage();
336
337         case 'V':
338             ovs_print_version(OFP10_VERSION, OFP10_VERSION);
339             exit(EXIT_SUCCESS);
340
341         VLOG_OPTION_HANDLERS
342         DAEMON_OPTION_HANDLERS
343
344         STREAM_SSL_OPTION_HANDLERS
345
346         case OPT_PEER_CA_CERT:
347             stream_ssl_set_peer_ca_cert_file(optarg);
348             break;
349
350         case '?':
351             exit(EXIT_FAILURE);
352
353         default:
354             abort();
355         }
356     }
357     free(short_options);
358
359     if (!simap_is_empty(&port_queues) || default_queue != UINT32_MAX) {
360         if (action_normal) {
361             ovs_error(0, "queue IDs are incompatible with -N or --normal; "
362                       "not using OFPP_NORMAL");
363             action_normal = false;
364         }
365
366         if (!learn_macs) {
367             ovs_error(0, "queue IDs are incompatible with -H or --hub; "
368                       "not acting as hub");
369             learn_macs = true;
370         }
371     }
372 }
373
374 static void
375 usage(void)
376 {
377     printf("%s: OpenFlow controller\n"
378            "usage: %s [OPTIONS] METHOD\n"
379            "where METHOD is any OpenFlow connection method.\n",
380            program_name, program_name);
381     vconn_usage(true, true, false);
382     daemon_usage();
383     vlog_usage();
384     printf("\nOther options:\n"
385            "  -H, --hub               act as hub instead of learning switch\n"
386            "  -n, --noflow            pass traffic, but don't add flows\n"
387            "  --max-idle=SECS         max idle time for new flows\n"
388            "  -N, --normal            use OFPP_NORMAL action\n"
389            "  -w, --wildcards[=MASK]  wildcard (specified) bits in flows\n"
390            "  -q, --queue=QUEUE-ID    OpenFlow queue ID to use for output\n"
391            "  -Q PORT-NAME:QUEUE-ID   use QUEUE-ID for frames from PORT-NAME\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 }