2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 "fatal-signal.h"
29 #include "openflow/openflow.h"
30 #include "poll-loop.h"
31 #include "socket-util.h"
33 #include "stream-ssl.h"
46 struct pstream *pstream;
50 check(int a, int b, const char *as, const char *file, int line)
53 ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
58 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
61 check_errno(int a, int b, const char *as, const char *file, int line)
64 char *str_b = xstrdup(ovs_strerror(abs(b)));
65 ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
66 file, line, as, a, ovs_strerror(abs(a)), b, str_b);
70 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
73 fpv_create(const char *type, struct fake_pvconn *fpv)
76 if (!strcmp(type, "ssl")) {
77 stream_ssl_set_private_key_file("testpki-privkey.pem");
78 stream_ssl_set_certificate_file("testpki-cert.pem");
79 stream_ssl_set_ca_cert_file("testpki-cacert.pem", false);
84 if (!strcmp(type, "unix")) {
85 static int unix_count = 0;
88 bind_path = xasprintf("fake-pvconn.%d", unix_count++);
89 fpv->pvconn_name = xasprintf("punix:%s", bind_path);
90 fpv->vconn_name = xasprintf("unix:%s", bind_path);
91 CHECK_ERRNO(pstream_open(fpv->pvconn_name, &fpv->pstream,
94 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
95 char *s, *port, *save_ptr = NULL;
98 open_name = xasprintf("p%s:0:127.0.0.1", type);
99 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream, DSCP_DEFAULT), 0);
101 /* Extract bound port number from pstream name. */
102 s = xstrdup(pstream_get_name(fpv->pstream));
103 strtok_r(s, ":", &save_ptr);
104 port = strtok_r(NULL, ":", &save_ptr);
107 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
108 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
117 static struct stream *
118 fpv_accept(struct fake_pvconn *fpv)
120 struct stream *stream;
122 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
128 fpv_close(struct fake_pvconn *fpv)
130 pstream_close(fpv->pstream);
135 fpv_destroy(struct fake_pvconn *fpv)
138 free(fpv->pvconn_name);
139 free(fpv->vconn_name);
142 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
143 * verifies that vconn_connect() reports 'expected_error'. */
145 test_refuse_connection(int argc OVS_UNUSED, char *argv[])
147 const char *type = argv[1];
148 struct fake_pvconn fpv;
152 fpv_create(type, &fpv);
153 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
157 error = vconn_connect_block(vconn);
158 if (!strcmp(type, "tcp")) {
159 if (error != ECONNRESET && error != EPIPE
161 && error != WSAECONNRESET
164 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
165 error, ovs_strerror(error));
167 } else if (!strcmp(type, "unix")) {
169 CHECK_ERRNO(error, EPIPE);
171 CHECK_ERRNO(error, WSAECONNRESET);
173 } else if (!strcmp(type, "ssl")) {
174 if (error != EPROTO && error != ECONNRESET) {
175 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
176 error, ovs_strerror(error));
179 ovs_fatal(0, "invalid connection type %s", type);
186 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
187 * closes it immediately, and verifies that vconn_connect() reports
188 * 'expected_error'. */
190 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
192 const char *type = argv[1];
193 struct fake_pvconn fpv;
197 fpv_create(type, &fpv);
198 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
200 stream_close(fpv_accept(&fpv));
203 error = vconn_connect_block(vconn);
204 if (!strcmp(type, "tcp") || !strcmp(type, "unix")) {
205 if (error != ECONNRESET && error != EPIPE
207 && error != WSAECONNRESET
210 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
211 error, ovs_strerror(error));
214 CHECK_ERRNO(error, EPROTO);
221 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
222 * reads the hello message from it, then closes the connection and verifies
223 * that vconn_connect() reports 'expected_error'. */
225 test_read_hello(int argc OVS_UNUSED, char *argv[])
227 const char *type = argv[1];
228 struct fake_pvconn fpv;
230 struct stream *stream;
233 fpv_create(type, &fpv);
234 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
236 stream = fpv_accept(&fpv);
239 struct ofp_header hello;
242 retval = stream_recv(stream, &hello, sizeof hello);
243 if (retval == sizeof hello) {
246 CHECK(hello.version, OFP13_VERSION);
247 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
248 CHECK(raw, OFPRAW_OFPT_HELLO);
249 CHECK(ntohs(hello.length), sizeof hello);
252 CHECK_ERRNO(retval, -EAGAIN);
256 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
257 vconn_run_wait(vconn);
258 vconn_connect_wait(vconn);
259 stream_recv_wait(stream);
262 stream_close(stream);
263 error = vconn_connect_block(vconn);
264 if (error != ECONNRESET && error != EPIPE) {
265 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
266 error, ovs_strerror(error));
271 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
272 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
273 * message), then verifies that vconn_connect() reports
274 * 'expect_connect_error'. */
276 test_send_hello(const char *type, const void *out, size_t out_size,
277 int expect_connect_error)
279 struct fake_pvconn fpv;
281 bool read_hello, connected;
283 struct stream *stream;
286 fpv_create(type, &fpv);
287 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
289 stream = fpv_accept(&fpv);
293 while (n_sent < out_size) {
296 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
299 } else if (retval == -EAGAIN) {
302 stream_recv_wait(stream);
303 vconn_connect_wait(vconn);
304 vconn_run_wait(vconn);
307 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
311 read_hello = connected = false;
314 struct ofp_header hello;
315 int retval = stream_recv(stream, &hello, sizeof hello);
316 if (retval == sizeof hello) {
319 CHECK(hello.version, OFP13_VERSION);
320 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
321 CHECK(raw, OFPRAW_OFPT_HELLO);
322 CHECK(ntohs(hello.length), sizeof hello);
325 CHECK_ERRNO(retval, -EAGAIN);
331 int error = vconn_connect(vconn);
332 if (error == expect_connect_error) {
336 stream_close(stream);
341 CHECK_ERRNO(error, EAGAIN);
345 if (read_hello && connected) {
349 vconn_run_wait(vconn);
351 vconn_connect_wait(vconn);
354 stream_recv_wait(stream);
358 stream_close(stream);
359 CHECK_ERRNO(vconn_recv_block(vconn, &msg), EOF);
363 /* Try connecting and sending a normal hello, which should succeed. */
365 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
367 const char *type = argv[1];
368 struct ofpbuf *hello;
370 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP13_VERSION,
371 htonl(0x12345678), 0);
372 test_send_hello(type, ofpbuf_data(hello), ofpbuf_size(hello), 0);
373 ofpbuf_delete(hello);
376 /* Try connecting and sending an extra-long hello, which should succeed (since
377 * the specification says that implementations must accept and ignore extra
380 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
382 const char *type = argv[1];
383 struct ofpbuf *hello;
384 enum { EXTRA_BYTES = 8 };
386 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP13_VERSION,
387 htonl(0x12345678), EXTRA_BYTES);
388 ofpbuf_put_zeros(hello, EXTRA_BYTES);
389 ofpmsg_update_length(hello);
390 test_send_hello(type, ofpbuf_data(hello), ofpbuf_size(hello), 0);
391 ofpbuf_delete(hello);
394 /* Try connecting and sending an echo request instead of a hello, which should
395 * fail with EPROTO. */
397 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
399 const char *type = argv[1];
402 echo = ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, OFP13_VERSION,
403 htonl(0x12345678), 0);
404 test_send_hello(type, ofpbuf_data(echo), ofpbuf_size(echo), EPROTO);
408 /* Try connecting and sending a hello packet that has its length field as 0,
409 * which should fail with EPROTO. */
411 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
413 const char *type = argv[1];
414 struct ofp_header hello;
416 memset(&hello, 0, sizeof hello);
417 test_send_hello(type, &hello, sizeof hello, EPROTO);
420 /* Try connecting and sending a hello packet that has a bad version, which
421 * should fail with EPROTO. */
423 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
425 const char *type = argv[1];
426 struct ofpbuf *hello;
428 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP13_VERSION,
429 htonl(0x12345678), 0);
430 ((struct ofp_header *) ofpbuf_data(hello))->version = 0;
431 test_send_hello(type, ofpbuf_data(hello), ofpbuf_size(hello), EPROTO);
432 ofpbuf_delete(hello);
435 static const struct command commands[] = {
436 {"refuse-connection", 1, 1, test_refuse_connection},
437 {"accept-then-close", 1, 1, test_accept_then_close},
438 {"read-hello", 1, 1, test_read_hello},
439 {"send-plain-hello", 1, 1, test_send_plain_hello},
440 {"send-long-hello", 1, 1, test_send_long_hello},
441 {"send-echo-hello", 1, 1, test_send_echo_hello},
442 {"send-short-hello", 1, 1, test_send_short_hello},
443 {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
448 test_vconn_main(int argc, char *argv[])
450 set_program_name(argv[0]);
451 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
452 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
453 fatal_ignore_sigpipe();
457 run_command(argc - 1, argv + 1, commands);
460 OVSTEST_REGISTER("test-vconn", test_vconn_main);