Global replace of Nicira Networks.
[sliver-openvswitch.git] / utilities / ovs-controller.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 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 "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 #include "socket-util.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: 0 to disable wildcard flow entries, a OFPFW_* 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 (cast to void *). */
80 static struct shash port_queues = SHASH_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 int do_switching(struct switch_ *);
90 static void new_switch(struct switch_ *, struct vconn *);
91 static void parse_options(int argc, char *argv[]);
92 static void usage(void) NO_RETURN;
93
94 int
95 main(int argc, char *argv[])
96 {
97     struct unixctl_server *unixctl;
98     struct switch_ switches[MAX_SWITCHES];
99     struct pvconn *listeners[MAX_LISTENERS];
100     int n_switches, n_listeners;
101     int retval;
102     int i;
103
104     proctitle_init(argc, argv);
105     set_program_name(argv[0]);
106     parse_options(argc, argv);
107     signal(SIGPIPE, SIG_IGN);
108
109     if (argc - optind < 1) {
110         ovs_fatal(0, "at least one vconn argument required; "
111                   "use --help for usage");
112     }
113
114     n_switches = n_listeners = 0;
115     for (i = optind; i < argc; i++) {
116         const char *name = argv[i];
117         struct vconn *vconn;
118
119         retval = vconn_open(name, OFP10_VERSION, &vconn, DSCP_DEFAULT);
120         if (!retval) {
121             if (n_switches >= MAX_SWITCHES) {
122                 ovs_fatal(0, "max %d switch connections", n_switches);
123             }
124             new_switch(&switches[n_switches++], vconn);
125             continue;
126         } else if (retval == EAFNOSUPPORT) {
127             struct pvconn *pvconn;
128             retval = pvconn_open(name, &pvconn, DSCP_DEFAULT);
129             if (!retval) {
130                 if (n_listeners >= MAX_LISTENERS) {
131                     ovs_fatal(0, "max %d passive connections", n_listeners);
132                 }
133                 listeners[n_listeners++] = pvconn;
134             }
135         }
136         if (retval) {
137             VLOG_ERR("%s: connect: %s", name, strerror(retval));
138         }
139     }
140     if (n_switches == 0 && n_listeners == 0) {
141         ovs_fatal(0, "no active or passive switch connections");
142     }
143
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], OFP10_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, DSCP_DEFAULT);
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.wildcards = wildcards;
233     cfg.max_idle = set_up_flows ? max_idle : -1;
234     cfg.default_flows = default_flows;
235     cfg.n_default_flows = n_default_flows;
236     cfg.default_queue = default_queue;
237     cfg.port_queues = &port_queues;
238     sw->lswitch = lswitch_create(sw->rconn, &cfg);
239 }
240
241 static int
242 do_switching(struct switch_ *sw)
243 {
244     unsigned int packets_sent;
245     struct ofpbuf *msg;
246
247     packets_sent = rconn_packets_sent(sw->rconn);
248
249     msg = rconn_recv(sw->rconn);
250     if (msg) {
251         if (!mute) {
252             lswitch_process_packet(sw->lswitch, sw->rconn, msg);
253         }
254         ofpbuf_delete(msg);
255     }
256     rconn_run(sw->rconn);
257
258     return (!rconn_is_alive(sw->rconn) ? EOF
259             : rconn_packets_sent(sw->rconn) != packets_sent ? 0
260             : EAGAIN);
261 }
262
263 static void
264 add_port_queue(char *s)
265 {
266     char *save_ptr = NULL;
267     char *port_name;
268     char *queue_id;
269
270     port_name = strtok_r(s, ":", &save_ptr);
271     queue_id = strtok_r(NULL, "", &save_ptr);
272     if (!queue_id) {
273         ovs_fatal(0, "argument to -Q or --port-queue should take the form "
274                   "\"<port-name>:<queue-id>\"");
275     }
276
277     if (!shash_add_once(&port_queues, port_name,
278                         (void *) (uintptr_t) atoi(queue_id))) {
279         ovs_fatal(0, "<port-name> arguments for -Q or --port-queue must "
280                   "be unique");
281     }
282 }
283
284 static void
285 parse_options(int argc, char *argv[])
286 {
287     enum {
288         OPT_MAX_IDLE = UCHAR_MAX + 1,
289         OPT_PEER_CA_CERT,
290         OPT_MUTE,
291         OPT_WITH_FLOWS,
292         OPT_UNIXCTL,
293         VLOG_OPTION_ENUMS,
294         DAEMON_OPTION_ENUMS
295     };
296     static struct option long_options[] = {
297         {"hub",         no_argument, NULL, 'H'},
298         {"noflow",      no_argument, NULL, 'n'},
299         {"normal",      no_argument, NULL, 'N'},
300         {"wildcards",   optional_argument, NULL, 'w'},
301         {"max-idle",    required_argument, NULL, OPT_MAX_IDLE},
302         {"mute",        no_argument, NULL, OPT_MUTE},
303         {"queue",       required_argument, NULL, 'q'},
304         {"port-queue",  required_argument, NULL, 'Q'},
305         {"with-flows",  required_argument, NULL, OPT_WITH_FLOWS},
306         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
307         {"help",        no_argument, NULL, 'h'},
308         {"version",     no_argument, NULL, 'V'},
309         DAEMON_LONG_OPTIONS,
310         VLOG_LONG_OPTIONS,
311         STREAM_SSL_LONG_OPTIONS,
312         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
313         {NULL, 0, NULL, 0},
314     };
315     char *short_options = long_options_to_short_options(long_options);
316
317     for (;;) {
318         int indexptr;
319         int c;
320
321         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
322         if (c == -1) {
323             break;
324         }
325
326         switch (c) {
327         case 'H':
328             learn_macs = false;
329             break;
330
331         case 'n':
332             set_up_flows = false;
333             break;
334
335         case OPT_MUTE:
336             mute = true;
337             break;
338
339         case 'N':
340             action_normal = true;
341             break;
342
343         case 'w':
344             wildcards = optarg ? strtol(optarg, NULL, 16) : UINT32_MAX;
345             break;
346
347         case OPT_MAX_IDLE:
348             if (!strcmp(optarg, "permanent")) {
349                 max_idle = OFP_FLOW_PERMANENT;
350             } else {
351                 max_idle = atoi(optarg);
352                 if (max_idle < 1 || max_idle > 65535) {
353                     ovs_fatal(0, "--max-idle argument must be between 1 and "
354                               "65535 or the word 'permanent'");
355                 }
356             }
357             break;
358
359         case 'q':
360             default_queue = atoi(optarg);
361             break;
362
363         case 'Q':
364             add_port_queue(optarg);
365             break;
366
367         case OPT_WITH_FLOWS:
368             parse_ofp_flow_mod_file(optarg, OFPFC_ADD, &default_flows,
369                                     &n_default_flows);
370             break;
371
372         case OPT_UNIXCTL:
373             unixctl_path = optarg;
374             break;
375
376         case 'h':
377             usage();
378
379         case 'V':
380             ovs_print_version(OFP10_VERSION, OFP10_VERSION);
381             exit(EXIT_SUCCESS);
382
383         VLOG_OPTION_HANDLERS
384         DAEMON_OPTION_HANDLERS
385
386         STREAM_SSL_OPTION_HANDLERS
387
388         case OPT_PEER_CA_CERT:
389             stream_ssl_set_peer_ca_cert_file(optarg);
390             break;
391
392         case '?':
393             exit(EXIT_FAILURE);
394
395         default:
396             abort();
397         }
398     }
399     free(short_options);
400
401     if (!shash_is_empty(&port_queues) || default_queue != UINT32_MAX) {
402         if (action_normal) {
403             ovs_error(0, "queue IDs are incompatible with -N or --normal; "
404                       "not using OFPP_NORMAL");
405             action_normal = false;
406         }
407
408         if (!learn_macs) {
409             ovs_error(0, "queue IDs are incompatible with -H or --hub; "
410                       "not acting as hub");
411             learn_macs = true;
412         }
413     }
414 }
415
416 static void
417 usage(void)
418 {
419     printf("%s: OpenFlow controller\n"
420            "usage: %s [OPTIONS] METHOD\n"
421            "where METHOD is any OpenFlow connection method.\n",
422            program_name, program_name);
423     vconn_usage(true, true, false);
424     daemon_usage();
425     vlog_usage();
426     printf("\nOther options:\n"
427            "  -H, --hub               act as hub instead of learning switch\n"
428            "  -n, --noflow            pass traffic, but don't add flows\n"
429            "  --max-idle=SECS         max idle time for new flows\n"
430            "  -N, --normal            use OFPP_NORMAL action\n"
431            "  -w, --wildcards[=MASK]  wildcard (specified) bits in flows\n"
432            "  -q, --queue=QUEUE-ID    OpenFlow queue ID to use for output\n"
433            "  -Q PORT-NAME:QUEUE-ID   use QUEUE-ID for frames from PORT-NAME\n"
434            "  --with-flows FILE       use the flows from FILE\n"
435            "  --unixctl=SOCKET        override default control socket name\n"
436            "  -h, --help              display this help message\n"
437            "  -V, --version           display version information\n");
438     exit(EXIT_SUCCESS);
439 }