ovs-controller: Make --with-flows read the file only once, at startup.
[openvswitch] / 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 "ofpbuf.h"
33 #include "openflow/openflow.h"
34 #include "poll-loop.h"
35 #include "rconn.h"
36 #include "stream-ssl.h"
37 #include "timeval.h"
38 #include "unixctl.h"
39 #include "util.h"
40 #include "vconn.h"
41 #include "vlog.h"
42
43 VLOG_DEFINE_THIS_MODULE(controller)
44
45 #define MAX_SWITCHES 16
46 #define MAX_LISTENERS 16
47
48 struct switch_ {
49     struct lswitch *lswitch;
50     struct rconn *rconn;
51 };
52
53 /* Learn the ports on which MAC addresses appear? */
54 static bool learn_macs = true;
55
56 /* Set up flows?  (If not, every packet is processed at the controller.) */
57 static bool set_up_flows = true;
58
59 /* -N, --normal: Use "NORMAL" action instead of explicit port? */
60 static bool action_normal = false;
61
62 /* -w, --wildcard: Set up exact match or wildcard flow entries? */
63 static bool exact_flows = true;
64
65 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
66 static int max_idle = 60;
67
68 /* --mute: If true, accept connections from switches but do not reply to any
69  * of their messages (for debugging fail-open mode). */
70 static bool mute = false;
71
72 /* -q, --queue: OpenFlow queue to use, or the default queue if UINT32_MAX. */
73 static uint32_t queue_id = UINT32_MAX;
74
75 /* --with-flows: File with flows to send to switch, or null to not load
76  * any default flows. */
77 static struct ovs_queue default_flows = OVS_QUEUE_INITIALIZER;
78
79 /* --unixctl: Name of unixctl socket, or null to use the default. */
80 static char *unixctl_path = NULL;
81
82 static int do_switching(struct switch_ *);
83 static void new_switch(struct switch_ *, struct vconn *);
84 static void parse_options(int argc, char *argv[]);
85 static void usage(void) NO_RETURN;
86
87 int
88 main(int argc, char *argv[])
89 {
90     struct unixctl_server *unixctl;
91     struct switch_ switches[MAX_SWITCHES];
92     struct pvconn *listeners[MAX_LISTENERS];
93     int n_switches, n_listeners;
94     int retval;
95     int i;
96
97     proctitle_init(argc, argv);
98     set_program_name(argv[0]);
99     parse_options(argc, argv);
100     signal(SIGPIPE, SIG_IGN);
101
102     if (argc - optind < 1) {
103         ovs_fatal(0, "at least one vconn argument required; "
104                   "use --help for usage");
105     }
106
107     n_switches = n_listeners = 0;
108     for (i = optind; i < argc; i++) {
109         const char *name = argv[i];
110         struct vconn *vconn;
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
150         /* Accept connections on listening vconns. */
151         for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
152             struct vconn *new_vconn;
153
154             retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
155             if (!retval || retval == EAGAIN) {
156                 if (!retval) {
157                     new_switch(&switches[n_switches++], new_vconn);
158                 }
159                 i++;
160             } else {
161                 pvconn_close(listeners[i]);
162                 listeners[i] = listeners[--n_listeners];
163             }
164         }
165
166         /* Do some switching work.  Limit the number of iterations so that
167          * callbacks registered with the poll loop don't starve. */
168         for (iteration = 0; iteration < 50; iteration++) {
169             bool progress = false;
170             for (i = 0; i < n_switches; ) {
171                 struct switch_ *this = &switches[i];
172
173                 retval = do_switching(this);
174                 if (!retval || retval == EAGAIN) {
175                     if (!retval) {
176                         progress = true;
177                     }
178                     i++;
179                 } else {
180                     rconn_destroy(this->rconn);
181                     lswitch_destroy(this->lswitch);
182                     switches[i] = switches[--n_switches];
183                 }
184             }
185             if (!progress) {
186                 break;
187             }
188         }
189         for (i = 0; i < n_switches; i++) {
190             struct switch_ *this = &switches[i];
191             lswitch_run(this->lswitch);
192         }
193
194         unixctl_server_run(unixctl);
195
196         /* Wait for something to happen. */
197         if (n_switches < MAX_SWITCHES) {
198             for (i = 0; i < n_listeners; i++) {
199                 pvconn_wait(listeners[i]);
200             }
201         }
202         for (i = 0; i < n_switches; i++) {
203             struct switch_ *sw = &switches[i];
204             rconn_run_wait(sw->rconn);
205             rconn_recv_wait(sw->rconn);
206             lswitch_wait(sw->lswitch);
207         }
208         unixctl_server_wait(unixctl);
209         poll_block();
210     }
211
212     return 0;
213 }
214
215 static void
216 new_switch(struct switch_ *sw, struct vconn *vconn)
217 {
218     sw->rconn = rconn_create(60, 0);
219     rconn_connect_unreliably(sw->rconn, vconn, NULL);
220     sw->lswitch = lswitch_create(sw->rconn, learn_macs, exact_flows,
221                                  set_up_flows ? max_idle : -1,
222                                  action_normal, default_flows.head);
223
224     lswitch_set_queue(sw->lswitch, queue_id);
225 }
226
227 static int
228 do_switching(struct switch_ *sw)
229 {
230     unsigned int packets_sent;
231     struct ofpbuf *msg;
232
233     packets_sent = rconn_packets_sent(sw->rconn);
234
235     msg = rconn_recv(sw->rconn);
236     if (msg) {
237         if (!mute) {
238             lswitch_process_packet(sw->lswitch, sw->rconn, msg);
239         }
240         ofpbuf_delete(msg);
241     }
242     rconn_run(sw->rconn);
243
244     return (!rconn_is_alive(sw->rconn) ? EOF
245             : rconn_packets_sent(sw->rconn) != packets_sent ? 0
246             : EAGAIN);
247 }
248
249 static void
250 read_flow_file(const char *name)
251 {
252     struct ofpbuf *b;
253     FILE *stream;
254
255     stream = fopen(optarg, "r");
256     if (!stream) {
257         ovs_fatal(errno, "%s: open", name);
258     }
259
260     while ((b = parse_ofp_add_flow_file(stream)) != NULL) {
261         queue_push_tail(&default_flows, b);
262     }
263
264     fclose(stream);
265 }
266
267 static void
268 parse_options(int argc, char *argv[])
269 {
270     enum {
271         OPT_MAX_IDLE = UCHAR_MAX + 1,
272         OPT_PEER_CA_CERT,
273         OPT_MUTE,
274         OPT_WITH_FLOWS,
275         OPT_UNIXCTL,
276         VLOG_OPTION_ENUMS
277     };
278     static struct option long_options[] = {
279         {"hub",         no_argument, 0, 'H'},
280         {"noflow",      no_argument, 0, 'n'},
281         {"normal",      no_argument, 0, 'N'},
282         {"wildcard",    no_argument, 0, 'w'},
283         {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
284         {"mute",        no_argument, 0, OPT_MUTE},
285         {"queue",       required_argument, 0, 'q'},
286         {"with-flows",  required_argument, 0, OPT_WITH_FLOWS},
287         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
288         {"help",        no_argument, 0, 'h'},
289         {"version",     no_argument, 0, 'V'},
290         DAEMON_LONG_OPTIONS,
291         VLOG_LONG_OPTIONS,
292 #ifdef HAVE_OPENSSL
293         STREAM_SSL_LONG_OPTIONS
294         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
295 #endif
296         {0, 0, 0, 0},
297     };
298     char *short_options = long_options_to_short_options(long_options);
299
300     for (;;) {
301         int indexptr;
302         int c;
303
304         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
305         if (c == -1) {
306             break;
307         }
308
309         switch (c) {
310         case 'H':
311             learn_macs = false;
312             break;
313
314         case 'n':
315             set_up_flows = false;
316             break;
317
318         case OPT_MUTE:
319             mute = true;
320             break;
321
322         case 'N':
323             action_normal = true;
324             break;
325
326         case 'w':
327             exact_flows = false;
328             break;
329
330         case OPT_MAX_IDLE:
331             if (!strcmp(optarg, "permanent")) {
332                 max_idle = OFP_FLOW_PERMANENT;
333             } else {
334                 max_idle = atoi(optarg);
335                 if (max_idle < 1 || max_idle > 65535) {
336                     ovs_fatal(0, "--max-idle argument must be between 1 and "
337                               "65535 or the word 'permanent'");
338                 }
339             }
340             break;
341
342         case 'q':
343             queue_id = atoi(optarg);
344             break;
345
346         case OPT_WITH_FLOWS:
347             read_flow_file(optarg);
348             break;
349
350         case OPT_UNIXCTL:
351             unixctl_path = optarg;
352             break;
353
354         case 'h':
355             usage();
356
357         case 'V':
358             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
359             exit(EXIT_SUCCESS);
360
361         VLOG_OPTION_HANDLERS
362         DAEMON_OPTION_HANDLERS
363
364 #ifdef HAVE_OPENSSL
365         STREAM_SSL_OPTION_HANDLERS
366
367         case OPT_PEER_CA_CERT:
368             stream_ssl_set_peer_ca_cert_file(optarg);
369             break;
370 #endif
371
372         case '?':
373             exit(EXIT_FAILURE);
374
375         default:
376             abort();
377         }
378     }
379     free(short_options);
380 }
381
382 static void
383 usage(void)
384 {
385     printf("%s: OpenFlow controller\n"
386            "usage: %s [OPTIONS] METHOD\n"
387            "where METHOD is any OpenFlow connection method.\n",
388            program_name, program_name);
389     vconn_usage(true, true, false);
390     daemon_usage();
391     vlog_usage();
392     printf("\nOther options:\n"
393            "  -H, --hub               act as hub instead of learning switch\n"
394            "  -n, --noflow            pass traffic, but don't add flows\n"
395            "  --max-idle=SECS         max idle time for new flows\n"
396            "  -N, --normal            use OFPAT_NORMAL action\n"
397            "  -w, --wildcard          use wildcards, not exact-match rules\n"
398            "  -q, --queue=QUEUE       OpenFlow queue ID to use for output\n"
399            "  --with-flows FILE       use the flows from FILE\n"
400            "  --unixctl=SOCKET        override default control socket name\n"
401            "  -h, --help              display this help message\n"
402            "  -V, --version           display version information\n");
403     exit(EXIT_SUCCESS);
404 }