meta-flow: Correctly set destination MAC in mf_set_flow_value().
[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: 0 to disable wildcard flow entries, a OFPFW_* bitmask to
65  * enable specific wildcards, or UINT32_MAX to use the default wildcards. */
66 static uint32_t wildcards = 0;
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: Flows to send to switch, or an empty list not to send any
82  * default flows. */
83 static struct list default_flows = LIST_INITIALIZER(&default_flows);
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     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         int iteration;
154
155         /* Accept connections on listening vconns. */
156         for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
157             struct vconn *new_vconn;
158
159             retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
160             if (!retval || retval == EAGAIN) {
161                 if (!retval) {
162                     new_switch(&switches[n_switches++], new_vconn);
163                 }
164                 i++;
165             } else {
166                 pvconn_close(listeners[i]);
167                 listeners[i] = listeners[--n_listeners];
168             }
169         }
170
171         /* Do some switching work.  Limit the number of iterations so that
172          * callbacks registered with the poll loop don't starve. */
173         for (iteration = 0; iteration < 50; iteration++) {
174             bool progress = false;
175             for (i = 0; i < n_switches; ) {
176                 struct switch_ *this = &switches[i];
177
178                 retval = do_switching(this);
179                 if (!retval || retval == EAGAIN) {
180                     if (!retval) {
181                         progress = true;
182                     }
183                     i++;
184                 } else {
185                     rconn_destroy(this->rconn);
186                     lswitch_destroy(this->lswitch);
187                     switches[i] = switches[--n_switches];
188                 }
189             }
190             if (!progress) {
191                 break;
192             }
193         }
194         for (i = 0; i < n_switches; i++) {
195             struct switch_ *this = &switches[i];
196             lswitch_run(this->lswitch);
197         }
198
199         unixctl_server_run(unixctl);
200
201         /* Wait for something to happen. */
202         if (n_switches < MAX_SWITCHES) {
203             for (i = 0; i < n_listeners; i++) {
204                 pvconn_wait(listeners[i]);
205             }
206         }
207         for (i = 0; i < n_switches; i++) {
208             struct switch_ *sw = &switches[i];
209             rconn_run_wait(sw->rconn);
210             rconn_recv_wait(sw->rconn);
211             lswitch_wait(sw->lswitch);
212         }
213         unixctl_server_wait(unixctl);
214         poll_block();
215     }
216
217     return 0;
218 }
219
220 static void
221 new_switch(struct switch_ *sw, struct vconn *vconn)
222 {
223     struct lswitch_config cfg;
224
225     sw->rconn = rconn_create(60, 0);
226     rconn_connect_unreliably(sw->rconn, vconn, NULL);
227
228     cfg.mode = (action_normal ? LSW_NORMAL
229                 : learn_macs ? LSW_LEARN
230                 : LSW_FLOOD);
231     cfg.wildcards = wildcards;
232     cfg.max_idle = set_up_flows ? max_idle : -1;
233     cfg.default_flows = &default_flows;
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     enum nx_flow_format flow_format;
265     bool flow_mod_table_id;
266     FILE *stream;
267
268     stream = fopen(optarg, "r");
269     if (!stream) {
270         ovs_fatal(errno, "%s: open", name);
271     }
272
273     flow_format = NXFF_OPENFLOW10;
274     flow_mod_table_id = false;
275     while (parse_ofp_flow_mod_file(&default_flows,
276                                    &flow_format, &flow_mod_table_id,
277                                    stream, OFPFC_ADD)) {
278         continue;
279     }
280
281     fclose(stream);
282 }
283
284 static void
285 add_port_queue(char *s)
286 {
287     char *save_ptr = NULL;
288     char *port_name;
289     char *queue_id;
290
291     port_name = strtok_r(s, ":", &save_ptr);
292     queue_id = strtok_r(NULL, "", &save_ptr);
293     if (!queue_id) {
294         ovs_fatal(0, "argument to -Q or --port-queue should take the form "
295                   "\"<port-name>:<queue-id>\"");
296     }
297
298     if (!shash_add_once(&port_queues, port_name,
299                         (void *) (uintptr_t) atoi(queue_id))) {
300         ovs_fatal(0, "<port-name> arguments for -Q or --port-queue must "
301                   "be unique");
302     }
303 }
304
305 static void
306 parse_options(int argc, char *argv[])
307 {
308     enum {
309         OPT_MAX_IDLE = UCHAR_MAX + 1,
310         OPT_PEER_CA_CERT,
311         OPT_MUTE,
312         OPT_WITH_FLOWS,
313         OPT_UNIXCTL,
314         VLOG_OPTION_ENUMS,
315         DAEMON_OPTION_ENUMS
316     };
317     static struct option long_options[] = {
318         {"hub",         no_argument, NULL, 'H'},
319         {"noflow",      no_argument, NULL, 'n'},
320         {"normal",      no_argument, NULL, 'N'},
321         {"wildcards",   optional_argument, NULL, 'w'},
322         {"max-idle",    required_argument, NULL, OPT_MAX_IDLE},
323         {"mute",        no_argument, NULL, OPT_MUTE},
324         {"queue",       required_argument, NULL, 'q'},
325         {"port-queue",  required_argument, NULL, 'Q'},
326         {"with-flows",  required_argument, NULL, OPT_WITH_FLOWS},
327         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
328         {"help",        no_argument, NULL, 'h'},
329         {"version",     no_argument, NULL, 'V'},
330         DAEMON_LONG_OPTIONS,
331         VLOG_LONG_OPTIONS,
332         STREAM_SSL_LONG_OPTIONS,
333         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
334         {NULL, 0, NULL, 0},
335     };
336     char *short_options = long_options_to_short_options(long_options);
337
338     for (;;) {
339         int indexptr;
340         int c;
341
342         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
343         if (c == -1) {
344             break;
345         }
346
347         switch (c) {
348         case 'H':
349             learn_macs = false;
350             break;
351
352         case 'n':
353             set_up_flows = false;
354             break;
355
356         case OPT_MUTE:
357             mute = true;
358             break;
359
360         case 'N':
361             action_normal = true;
362             break;
363
364         case 'w':
365             wildcards = optarg ? strtol(optarg, NULL, 16) : UINT32_MAX;
366             break;
367
368         case OPT_MAX_IDLE:
369             if (!strcmp(optarg, "permanent")) {
370                 max_idle = OFP_FLOW_PERMANENT;
371             } else {
372                 max_idle = atoi(optarg);
373                 if (max_idle < 1 || max_idle > 65535) {
374                     ovs_fatal(0, "--max-idle argument must be between 1 and "
375                               "65535 or the word 'permanent'");
376                 }
377             }
378             break;
379
380         case 'q':
381             default_queue = atoi(optarg);
382             break;
383
384         case 'Q':
385             add_port_queue(optarg);
386             break;
387
388         case OPT_WITH_FLOWS:
389             read_flow_file(optarg);
390             break;
391
392         case OPT_UNIXCTL:
393             unixctl_path = optarg;
394             break;
395
396         case 'h':
397             usage();
398
399         case 'V':
400             ovs_print_version(OFP_VERSION, OFP_VERSION);
401             exit(EXIT_SUCCESS);
402
403         VLOG_OPTION_HANDLERS
404         DAEMON_OPTION_HANDLERS
405
406         STREAM_SSL_OPTION_HANDLERS
407
408         case OPT_PEER_CA_CERT:
409             stream_ssl_set_peer_ca_cert_file(optarg);
410             break;
411
412         case '?':
413             exit(EXIT_FAILURE);
414
415         default:
416             abort();
417         }
418     }
419     free(short_options);
420
421     if (!shash_is_empty(&port_queues) || default_queue != UINT32_MAX) {
422         if (action_normal) {
423             ovs_error(0, "queue IDs are incompatible with -N or --normal; "
424                       "not using OFPP_NORMAL");
425             action_normal = false;
426         }
427
428         if (!learn_macs) {
429             ovs_error(0, "queue IDs are incompatible with -H or --hub; "
430                       "not acting as hub");
431             learn_macs = true;
432         }
433     }
434 }
435
436 static void
437 usage(void)
438 {
439     printf("%s: OpenFlow controller\n"
440            "usage: %s [OPTIONS] METHOD\n"
441            "where METHOD is any OpenFlow connection method.\n",
442            program_name, program_name);
443     vconn_usage(true, true, false);
444     daemon_usage();
445     vlog_usage();
446     printf("\nOther options:\n"
447            "  -H, --hub               act as hub instead of learning switch\n"
448            "  -n, --noflow            pass traffic, but don't add flows\n"
449            "  --max-idle=SECS         max idle time for new flows\n"
450            "  -N, --normal            use OFPP_NORMAL action\n"
451            "  -w, --wildcards[=MASK]  wildcard (specified) bits in flows\n"
452            "  -q, --queue=QUEUE-ID    OpenFlow queue ID to use for output\n"
453            "  -Q PORT-NAME:QUEUE-ID   use QUEUE-ID for frames from PORT-NAME\n"
454            "  --with-flows FILE       use the flows from FILE\n"
455            "  --unixctl=SOCKET        override default control socket name\n"
456            "  -h, --help              display this help message\n"
457            "  -V, --version           display version information\n");
458     exit(EXIT_SUCCESS);
459 }