Implement userspace switch.
[openvswitch] / switch / switch.c
1 /* Copyright (C) 2008 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #include <errno.h>
23 #include <getopt.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "command-line.h"
29 #include "controller.h"
30 #include "datapath.h"
31 #include "fault.h"
32 #include "openflow.h"
33 #include "poll-loop.h"
34 #include "queue.h"
35 #include "util.h"
36 #include "vconn.h"
37 #include "vconn-ssl.h"
38 #include "vlog-socket.h"
39
40 #define THIS_MODULE VLM_switch
41 #include "vlog.h"
42
43 static void parse_options(int argc, char *argv[]);
44 static void usage(void) NO_RETURN;
45
46 static bool reliable = true;
47 static struct datapath *dp;
48 static uint64_t dpid = UINT64_MAX;
49 static char *port_list;
50
51 static void add_ports(struct datapath *dp, char *port_list);
52
53 int
54 main(int argc, char *argv[])
55 {
56     struct controller_connection cc;
57     int error;
58
59     set_program_name(argv[0]);
60     register_fault_handlers();
61     vlog_init();
62     parse_options(argc, argv);
63
64     if (argc - optind != 1) {
65         fatal(0, "missing controller argument; use --help for usage");
66     }
67
68     controller_init(&cc, argv[optind], reliable);
69     error = dp_new(&dp, dpid, &cc);
70     if (error) {
71         fatal(error, "could not create datapath");
72     }
73     if (port_list) {
74         add_ports(dp, port_list); 
75     }
76
77     error = vlog_server_listen(NULL, NULL);
78     if (error) {
79         fatal(error, "could not listen for vlog connections");
80     }
81
82     for (;;) {
83         controller_run(&cc, dp);
84         dp_run(dp);
85         dp_wait(dp);
86         controller_wait(&cc);
87         poll_block();
88     }
89
90     return 0;
91 }
92
93 static void
94 add_ports(struct datapath *dp, char *port_list)
95 {
96     char *port, *save_ptr;
97
98     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
99      * can cause segfaults here:
100      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
101      * Using ",," instead of the obvious "," works around it. */
102     for (port = strtok_r(port_list, ",,", &save_ptr); port;
103          port = strtok_r(NULL, ",,", &save_ptr)) {
104         int error = dp_add_port(dp, port);
105         if (error) {
106             fatal(error, "failed to add port %s", port);
107         }
108     }
109 }
110
111 static void
112 parse_options(int argc, char *argv[])
113 {
114     static struct option long_options[] = {
115         {"interfaces",  required_argument, 0, 'i'},
116         {"unreliable",  no_argument, 0, 'u'},
117         {"datapath-id", required_argument, 0, 'd'},
118         {"verbose",     optional_argument, 0, 'v'},
119         {"help",        no_argument, 0, 'h'},
120         {"version",     no_argument, 0, 'V'},
121 #ifdef HAVE_OPENSSL
122         {"private-key", required_argument, 0, 'p'},
123         {"certificate", required_argument, 0, 'c'},
124         {"ca-cert",     required_argument, 0, 'C'},
125 #endif
126         {0, 0, 0, 0},
127     };
128     char *short_options = long_options_to_short_options(long_options);
129
130     for (;;) {
131         int indexptr;
132         int c;
133
134         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
135         if (c == -1) {
136             break;
137         }
138
139         switch (c) {
140         case 'u':
141             reliable = false;
142             break;
143
144         case 'd':
145             if (strlen(optarg) != 12
146                 || strspn(optarg, "0123456789abcdefABCDEF") != 12) {
147                 fatal(0, "argument to -d or --datapath-id must be "
148                       "exactly 12 hex digits");
149             }
150             dpid = strtoll(optarg, NULL, 16);
151             if (!dpid) {
152                 fatal(0, "argument to -d or --datapath-id must be nonzero");
153             }
154             break;
155
156         case 'h':
157             usage();
158
159         case 'V':
160             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
161             exit(EXIT_SUCCESS);
162
163         case 'v':
164             vlog_set_verbosity(optarg);
165             break;
166
167         case 'i':
168             if (!port_list) {
169                 port_list = optarg;
170             } else {
171                 port_list = xasprintf("%s,%s", port_list, optarg);
172             }
173             break;
174
175 #ifdef HAVE_OPENSSL
176         case 'p':
177             vconn_ssl_set_private_key_file(optarg);
178             break;
179
180         case 'c':
181             vconn_ssl_set_certificate_file(optarg);
182             break;
183
184         case 'C':
185             vconn_ssl_set_ca_cert_file(optarg);
186             break;
187 #endif
188
189         case '?':
190             exit(EXIT_FAILURE);
191
192         default:
193             abort();
194         }
195     }
196     free(short_options);
197 }
198
199 static void
200 usage(void)
201 {
202     printf("%s: userspace OpenFlow switch\n"
203            "usage: %s [OPTIONS] CONTROLLER\n"
204            "CONTROLLER must be one of the following:\n"
205            "  tcp:HOST[:PORT]         PORT (default: %d) on remote TCP HOST\n",
206            program_name, program_name, OFP_TCP_PORT);
207 #ifdef HAVE_OPENSSL
208     printf("  ssl:HOST[:PORT]         SSL PORT (default: %d) on remote HOST\n"
209            "\nPKI configuration (required to use SSL):\n"
210            "  -p, --private-key=FILE  file with private key\n"
211            "  -c, --certificate=FILE  file with certificate for private key\n"
212            "  -C, --ca-cert=FILE      file with peer CA certificate\n",
213            OFP_SSL_PORT);
214 #endif
215     printf("Options:\n"
216            "  -i, --interfaces=NETDEV[,NETDEV]...\n"
217            "                          add specified initial switch ports\n"
218            "  -d, --datapath-id=ID    Use ID as the OpenFlow switch ID\n"
219            "                          (ID must consist of 12 hex digits)\n"
220            "  -u, --unreliable        do not reconnect to controller\n"
221            "  -v, --verbose           set maximum verbosity level\n"
222            "  -h, --help              display this help message\n"
223            "  -V, --version           display version information\n");
224     exit(EXIT_SUCCESS);
225 }