2 * Copyright (c) 2008, 2009 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "command-line.h"
30 #include "learning-switch.h"
32 #include "openflow/openflow.h"
33 #include "poll-loop.h"
38 #include "vconn-ssl.h"
42 #define THIS_MODULE VLM_controller
44 #define MAX_SWITCHES 16
45 #define MAX_LISTENERS 16
48 struct lswitch
*lswitch
;
52 /* Learn the ports on which MAC addresses appear? */
53 static bool learn_macs
= true;
55 /* Set up flows? (If not, every packet is processed at the controller.) */
56 static bool set_up_flows
= true;
58 /* --max-idle: Maximum idle time, in seconds, before flows expire. */
59 static int max_idle
= 60;
61 /* --mute: If true, accept connections from switches but do not reply to any
62 * of their messages (for debugging fail-open mode). */
63 static bool mute
= false;
65 static int do_switching(struct switch_
*);
66 static void new_switch(struct switch_
*, struct vconn
*, const char *name
);
67 static void parse_options(int argc
, char *argv
[]);
68 static void usage(void) NO_RETURN
;
71 main(int argc
, char *argv
[])
73 struct unixctl_server
*unixctl
;
74 struct switch_ switches
[MAX_SWITCHES
];
75 struct pvconn
*listeners
[MAX_LISTENERS
];
76 int n_switches
, n_listeners
;
80 set_program_name(argv
[0]);
81 register_fault_handlers();
84 parse_options(argc
, argv
);
85 signal(SIGPIPE
, SIG_IGN
);
87 if (argc
- optind
< 1) {
88 ovs_fatal(0, "at least one vconn argument required; "
89 "use --help for usage");
92 n_switches
= n_listeners
= 0;
93 for (i
= optind
; i
< argc
; i
++) {
94 const char *name
= argv
[i
];
98 retval
= vconn_open(name
, OFP_VERSION
, &vconn
);
100 if (n_switches
>= MAX_SWITCHES
) {
101 ovs_fatal(0, "max %d switch connections", n_switches
);
103 new_switch(&switches
[n_switches
++], vconn
, name
);
105 } else if (retval
== EAFNOSUPPORT
) {
106 struct pvconn
*pvconn
;
107 retval
= pvconn_open(name
, &pvconn
);
109 if (n_listeners
>= MAX_LISTENERS
) {
110 ovs_fatal(0, "max %d passive connections", n_listeners
);
112 listeners
[n_listeners
++] = pvconn
;
116 VLOG_ERR("%s: connect: %s", name
, strerror(retval
));
119 if (n_switches
== 0 && n_listeners
== 0) {
120 ovs_fatal(0, "no active or passive switch connections");
123 die_if_already_running();
126 retval
= unixctl_server_create(NULL
, &unixctl
);
128 ovs_fatal(retval
, "Could not listen for unixctl connections");
131 while (n_switches
> 0 || n_listeners
> 0) {
135 /* Accept connections on listening vconns. */
136 for (i
= 0; i
< n_listeners
&& n_switches
< MAX_SWITCHES
; ) {
137 struct vconn
*new_vconn
;
140 retval
= pvconn_accept(listeners
[i
], OFP_VERSION
, &new_vconn
);
141 if (!retval
|| retval
== EAGAIN
) {
143 new_switch(&switches
[n_switches
++], new_vconn
, "tcp");
147 pvconn_close(listeners
[i
]);
148 listeners
[i
] = listeners
[--n_listeners
];
152 /* Do some switching work. Limit the number of iterations so that
153 * callbacks registered with the poll loop don't starve. */
154 for (iteration
= 0; iteration
< 50; iteration
++) {
155 bool progress
= false;
156 for (i
= 0; i
< n_switches
; ) {
157 struct switch_
*this = &switches
[i
];
158 int retval
= do_switching(this);
159 if (!retval
|| retval
== EAGAIN
) {
165 rconn_destroy(this->rconn
);
166 lswitch_destroy(this->lswitch
);
167 switches
[i
] = switches
[--n_switches
];
174 for (i
= 0; i
< n_switches
; i
++) {
175 struct switch_
*this = &switches
[i
];
176 lswitch_run(this->lswitch
, this->rconn
);
179 unixctl_server_run(unixctl
);
181 /* Wait for something to happen. */
182 if (n_switches
< MAX_SWITCHES
) {
183 for (i
= 0; i
< n_listeners
; i
++) {
184 pvconn_wait(listeners
[i
]);
187 for (i
= 0; i
< n_switches
; i
++) {
188 struct switch_
*sw
= &switches
[i
];
189 rconn_run_wait(sw
->rconn
);
190 rconn_recv_wait(sw
->rconn
);
191 lswitch_wait(sw
->lswitch
);
193 unixctl_server_wait(unixctl
);
201 new_switch(struct switch_
*sw
, struct vconn
*vconn
, const char *name
)
203 sw
->rconn
= rconn_new_from_vconn(name
, vconn
);
204 sw
->lswitch
= lswitch_create(sw
->rconn
, learn_macs
,
205 set_up_flows
? max_idle
: -1);
209 do_switching(struct switch_
*sw
)
211 unsigned int packets_sent
;
214 packets_sent
= rconn_packets_sent(sw
->rconn
);
216 msg
= rconn_recv(sw
->rconn
);
219 lswitch_process_packet(sw
->lswitch
, sw
->rconn
, msg
);
223 rconn_run(sw
->rconn
);
225 return (!rconn_is_alive(sw
->rconn
) ? EOF
226 : rconn_packets_sent(sw
->rconn
) != packets_sent
? 0
231 parse_options(int argc
, char *argv
[])
234 OPT_MAX_IDLE
= UCHAR_MAX
+ 1,
239 static struct option long_options
[] = {
240 {"hub", no_argument
, 0, 'H'},
241 {"noflow", no_argument
, 0, 'n'},
242 {"max-idle", required_argument
, 0, OPT_MAX_IDLE
},
243 {"mute", no_argument
, 0, OPT_MUTE
},
244 {"help", no_argument
, 0, 'h'},
245 {"version", no_argument
, 0, 'V'},
249 VCONN_SSL_LONG_OPTIONS
250 {"peer-ca-cert", required_argument
, 0, OPT_PEER_CA_CERT
},
254 char *short_options
= long_options_to_short_options(long_options
);
260 c
= getopt_long(argc
, argv
, short_options
, long_options
, &indexptr
);
271 set_up_flows
= false;
279 if (!strcmp(optarg
, "permanent")) {
280 max_idle
= OFP_FLOW_PERMANENT
;
282 max_idle
= atoi(optarg
);
283 if (max_idle
< 1 || max_idle
> 65535) {
284 ovs_fatal(0, "--max-idle argument must be between 1 and "
285 "65535 or the word 'permanent'");
294 OVS_PRINT_VERSION(OFP_VERSION
, OFP_VERSION
);
298 DAEMON_OPTION_HANDLERS
301 VCONN_SSL_OPTION_HANDLERS
303 case OPT_PEER_CA_CERT
:
304 vconn_ssl_set_peer_ca_cert_file(optarg
);
321 printf("%s: OpenFlow controller\n"
322 "usage: %s [OPTIONS] METHOD\n"
323 "where METHOD is any OpenFlow connection method.\n",
324 program_name
, program_name
);
325 vconn_usage(true, true, false);
328 printf("\nOther options:\n"
329 " -H, --hub act as hub instead of learning switch\n"
330 " -n, --noflow pass traffic, but don't add flows\n"
331 " --max-idle=SECS max idle time for new flows\n"
332 " -h, --help display this help message\n"
333 " -V, --version display version information\n");