2 * Copyright (c) 2009, 2010, 2011 Nicira, Inc.
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.
24 #include "command-line.h"
25 #include "openflow/openflow.h"
26 #include "poll-loop.h"
27 #include "socket-util.h"
29 #include "stream-ssl.h"
41 struct pstream *pstream;
45 check(int a, int b, const char *as, const char *file, int line)
48 ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
53 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
56 check_errno(int a, int b, const char *as, const char *file, int line)
59 char *str_b = strdup(strerror(abs(b)));
60 ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
61 file, line, as, a, strerror(abs(a)), b, str_b);
65 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
68 fpv_create(const char *type, struct fake_pvconn *fpv)
71 if (!strcmp(type, "ssl")) {
72 stream_ssl_set_private_key_file("testpki-privkey.pem");
73 stream_ssl_set_certificate_file("testpki-cert.pem");
74 stream_ssl_set_ca_cert_file("testpki-cacert.pem", false);
79 if (!strcmp(type, "unix")) {
80 static int unix_count = 0;
83 bind_path = xasprintf("fake-pvconn.%d", unix_count++);
84 fpv->pvconn_name = xasprintf("punix:%s", bind_path);
85 fpv->vconn_name = xasprintf("unix:%s", bind_path);
86 CHECK_ERRNO(pstream_open(fpv->pvconn_name, &fpv->pstream,
89 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
90 char *s, *port, *save_ptr = NULL;
93 open_name = xasprintf("p%s:0:127.0.0.1", type);
94 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream, DSCP_DEFAULT), 0);
96 /* Extract bound port number from pstream name. */
97 s = xstrdup(pstream_get_name(fpv->pstream));
98 strtok_r(s, ":", &save_ptr);
99 port = strtok_r(NULL, ":", &save_ptr);
102 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
103 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
112 static struct stream *
113 fpv_accept(struct fake_pvconn *fpv)
115 struct stream *stream;
117 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
123 fpv_close(struct fake_pvconn *fpv)
125 pstream_close(fpv->pstream);
130 fpv_destroy(struct fake_pvconn *fpv)
133 free(fpv->pvconn_name);
134 free(fpv->vconn_name);
137 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
138 * verifies that vconn_connect() reports 'expected_error'. */
140 test_refuse_connection(int argc OVS_UNUSED, char *argv[])
142 const char *type = argv[1];
144 struct fake_pvconn fpv;
147 expected_error = (!strcmp(type, "unix") ? EPIPE
148 : !strcmp(type, "tcp") ? ECONNRESET
151 fpv_create(type, &fpv);
152 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
156 CHECK_ERRNO(vconn_connect(vconn), expected_error);
161 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
162 * closes it immediately, and verifies that vconn_connect() reports
163 * 'expected_error'. */
165 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
167 const char *type = argv[1];
169 struct fake_pvconn fpv;
172 expected_error = (!strcmp(type, "unix") ? EPIPE
173 : !strcmp(type, "tcp") ? ECONNRESET
176 fpv_create(type, &fpv);
177 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
180 stream_close(fpv_accept(&fpv));
182 CHECK_ERRNO(vconn_connect(vconn), expected_error);
187 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
188 * reads the hello message from it, then closes the connection and verifies
189 * that vconn_connect() reports 'expected_error'. */
191 test_read_hello(int argc OVS_UNUSED, char *argv[])
193 const char *type = argv[1];
194 struct fake_pvconn fpv;
196 struct stream *stream;
198 fpv_create(type, &fpv);
199 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
202 stream = fpv_accept(&fpv);
205 struct ofp_header hello;
208 retval = stream_recv(stream, &hello, sizeof hello);
209 if (retval == sizeof hello) {
210 CHECK(hello.version, OFP10_VERSION);
211 CHECK(hello.type, OFPT_HELLO);
212 CHECK(ntohs(hello.length), sizeof hello);
215 CHECK_ERRNO(retval, -EAGAIN);
219 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
220 vconn_run_wait(vconn);
221 vconn_connect_wait(vconn);
222 stream_recv_wait(stream);
225 stream_close(stream);
226 CHECK_ERRNO(vconn_connect(vconn), ECONNRESET);
230 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
231 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
232 * message), then verifies that vconn_connect() reports
233 * 'expect_connect_error'. */
235 test_send_hello(const char *type, const void *out, size_t out_size,
236 int expect_connect_error)
238 struct fake_pvconn fpv;
240 bool read_hello, connected;
242 struct stream *stream;
245 fpv_create(type, &fpv);
246 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
249 stream = fpv_accept(&fpv);
253 while (n_sent < out_size) {
256 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
259 } else if (retval == -EAGAIN) {
262 stream_recv_wait(stream);
263 vconn_connect_wait(vconn);
264 vconn_run_wait(vconn);
267 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
271 read_hello = connected = false;
274 struct ofp_header hello;
275 int retval = stream_recv(stream, &hello, sizeof hello);
276 if (retval == sizeof hello) {
277 CHECK(hello.version, OFP10_VERSION);
278 CHECK(hello.type, OFPT_HELLO);
279 CHECK(ntohs(hello.length), sizeof hello);
282 CHECK_ERRNO(retval, -EAGAIN);
288 int error = vconn_connect(vconn);
289 if (error == expect_connect_error) {
293 stream_close(stream);
298 CHECK_ERRNO(error, EAGAIN);
302 if (read_hello && connected) {
306 vconn_run_wait(vconn);
308 vconn_connect_wait(vconn);
311 stream_recv_wait(stream);
315 stream_close(stream);
316 CHECK_ERRNO(vconn_recv(vconn, &msg), EOF);
320 /* Try connecting and sending a normal hello, which should succeed. */
322 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
324 const char *type = argv[1];
325 struct ofp_header hello;
327 hello.version = OFP10_VERSION;
328 hello.type = OFPT_HELLO;
329 hello.length = htons(sizeof hello);
330 hello.xid = htonl(0x12345678);
331 test_send_hello(type, &hello, sizeof hello, 0);
334 /* Try connecting and sending an extra-long hello, which should succeed (since
335 * the specification says that implementations must accept and ignore extra
338 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
340 const char *type = argv[1];
341 struct ofp_header hello;
342 char buffer[sizeof hello * 2];
344 hello.version = OFP10_VERSION;
345 hello.type = OFPT_HELLO;
346 hello.length = htons(sizeof buffer);
347 hello.xid = htonl(0x12345678);
348 memset(buffer, 0, sizeof buffer);
349 memcpy(buffer, &hello, sizeof hello);
350 test_send_hello(type, buffer, sizeof buffer, 0);
353 /* Try connecting and sending an echo request instead of a hello, which should
354 * fail with EPROTO. */
356 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
358 const char *type = argv[1];
359 struct ofp_header echo;
361 echo.version = OFP10_VERSION;
362 echo.type = OFPT_ECHO_REQUEST;
363 echo.length = htons(sizeof echo);
364 echo.xid = htonl(0x89abcdef);
365 test_send_hello(type, &echo, sizeof echo, EPROTO);
368 /* Try connecting and sending a hello packet that has its length field as 0,
369 * which should fail with EPROTO. */
371 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
373 const char *type = argv[1];
374 struct ofp_header hello;
376 memset(&hello, 0, sizeof hello);
377 test_send_hello(type, &hello, sizeof hello, EPROTO);
380 /* Try connecting and sending a hello packet that has a bad version, which
381 * should fail with EPROTO. */
383 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
385 const char *type = argv[1];
386 struct ofp_header hello;
388 hello.version = OFP10_VERSION - 1;
389 hello.type = OFPT_HELLO;
390 hello.length = htons(sizeof hello);
391 hello.xid = htonl(0x12345678);
392 test_send_hello(type, &hello, sizeof hello, EPROTO);
395 static const struct command commands[] = {
396 {"refuse-connection", 1, 1, test_refuse_connection},
397 {"accept-then-close", 1, 1, test_accept_then_close},
398 {"read-hello", 1, 1, test_read_hello},
399 {"send-plain-hello", 1, 1, test_send_plain_hello},
400 {"send-long-hello", 1, 1, test_send_long_hello},
401 {"send-echo-hello", 1, 1, test_send_echo_hello},
402 {"send-short-hello", 1, 1, test_send_short_hello},
403 {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
408 main(int argc, char *argv[])
410 set_program_name(argv[0]);
411 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
412 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
413 signal(SIGPIPE, SIG_IGN);
417 run_command(argc - 1, argv + 1, commands);