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 = strdup(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) {
160 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
161 error, ovs_strerror(error));
163 } else if (!strcmp(type, "unix")) {
164 CHECK_ERRNO(error, EPIPE);
165 } else if (!strcmp(type, "ssl")) {
166 if (error != EPROTO && error != ECONNRESET) {
167 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
168 error, ovs_strerror(error));
171 ovs_fatal(0, "invalid connection type %s", type);
178 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
179 * closes it immediately, and verifies that vconn_connect() reports
180 * 'expected_error'. */
182 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
184 const char *type = argv[1];
185 struct fake_pvconn fpv;
189 fpv_create(type, &fpv);
190 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
192 stream_close(fpv_accept(&fpv));
195 error = vconn_connect_block(vconn);
196 if (!strcmp(type, "tcp") || !strcmp(type, "unix")) {
197 if (error != ECONNRESET && error != EPIPE) {
198 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
199 error, ovs_strerror(error));
202 CHECK_ERRNO(error, EPROTO);
209 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
210 * reads the hello message from it, then closes the connection and verifies
211 * that vconn_connect() reports 'expected_error'. */
213 test_read_hello(int argc OVS_UNUSED, char *argv[])
215 const char *type = argv[1];
216 struct fake_pvconn fpv;
218 struct stream *stream;
221 fpv_create(type, &fpv);
222 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
224 stream = fpv_accept(&fpv);
227 struct ofp_header hello;
230 retval = stream_recv(stream, &hello, sizeof hello);
231 if (retval == sizeof hello) {
234 CHECK(hello.version, OFP10_VERSION);
235 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
236 CHECK(raw, OFPRAW_OFPT_HELLO);
237 CHECK(ntohs(hello.length), sizeof hello);
240 CHECK_ERRNO(retval, -EAGAIN);
244 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
245 vconn_run_wait(vconn);
246 vconn_connect_wait(vconn);
247 stream_recv_wait(stream);
250 stream_close(stream);
251 error = vconn_connect_block(vconn);
252 if (error != ECONNRESET && error != EPIPE) {
253 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
254 error, ovs_strerror(error));
259 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
260 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
261 * message), then verifies that vconn_connect() reports
262 * 'expect_connect_error'. */
264 test_send_hello(const char *type, const void *out, size_t out_size,
265 int expect_connect_error)
267 struct fake_pvconn fpv;
269 bool read_hello, connected;
271 struct stream *stream;
274 fpv_create(type, &fpv);
275 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
277 stream = fpv_accept(&fpv);
281 while (n_sent < out_size) {
284 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
287 } else if (retval == -EAGAIN) {
290 stream_recv_wait(stream);
291 vconn_connect_wait(vconn);
292 vconn_run_wait(vconn);
295 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
299 read_hello = connected = false;
302 struct ofp_header hello;
303 int retval = stream_recv(stream, &hello, sizeof hello);
304 if (retval == sizeof hello) {
307 CHECK(hello.version, OFP10_VERSION);
308 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
309 CHECK(raw, OFPRAW_OFPT_HELLO);
310 CHECK(ntohs(hello.length), sizeof hello);
313 CHECK_ERRNO(retval, -EAGAIN);
319 int error = vconn_connect(vconn);
320 if (error == expect_connect_error) {
324 stream_close(stream);
329 CHECK_ERRNO(error, EAGAIN);
333 if (read_hello && connected) {
337 vconn_run_wait(vconn);
339 vconn_connect_wait(vconn);
342 stream_recv_wait(stream);
346 stream_close(stream);
347 CHECK_ERRNO(vconn_recv_block(vconn, &msg), EOF);
351 /* Try connecting and sending a normal hello, which should succeed. */
353 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
355 const char *type = argv[1];
356 struct ofpbuf *hello;
358 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
359 htonl(0x12345678), 0);
360 test_send_hello(type, ofpbuf_data(hello), ofpbuf_size(hello), 0);
361 ofpbuf_delete(hello);
364 /* Try connecting and sending an extra-long hello, which should succeed (since
365 * the specification says that implementations must accept and ignore extra
368 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
370 const char *type = argv[1];
371 struct ofpbuf *hello;
372 enum { EXTRA_BYTES = 8 };
374 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
375 htonl(0x12345678), EXTRA_BYTES);
376 ofpbuf_put_zeros(hello, EXTRA_BYTES);
377 ofpmsg_update_length(hello);
378 test_send_hello(type, ofpbuf_data(hello), ofpbuf_size(hello), 0);
379 ofpbuf_delete(hello);
382 /* Try connecting and sending an echo request instead of a hello, which should
383 * fail with EPROTO. */
385 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
387 const char *type = argv[1];
390 echo = ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
391 htonl(0x12345678), 0);
392 test_send_hello(type, ofpbuf_data(echo), ofpbuf_size(echo), EPROTO);
396 /* Try connecting and sending a hello packet that has its length field as 0,
397 * which should fail with EPROTO. */
399 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
401 const char *type = argv[1];
402 struct ofp_header hello;
404 memset(&hello, 0, sizeof hello);
405 test_send_hello(type, &hello, sizeof hello, EPROTO);
408 /* Try connecting and sending a hello packet that has a bad version, which
409 * should fail with EPROTO. */
411 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
413 const char *type = argv[1];
414 struct ofpbuf *hello;
416 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
417 htonl(0x12345678), 0);
418 ((struct ofp_header *) ofpbuf_data(hello))->version = 0;
419 test_send_hello(type, ofpbuf_data(hello), ofpbuf_size(hello), EPROTO);
420 ofpbuf_delete(hello);
423 static const struct command commands[] = {
424 {"refuse-connection", 1, 1, test_refuse_connection},
425 {"accept-then-close", 1, 1, test_accept_then_close},
426 {"read-hello", 1, 1, test_read_hello},
427 {"send-plain-hello", 1, 1, test_send_plain_hello},
428 {"send-long-hello", 1, 1, test_send_long_hello},
429 {"send-echo-hello", 1, 1, test_send_echo_hello},
430 {"send-short-hello", 1, 1, test_send_short_hello},
431 {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
436 test_vconn_main(int argc, char *argv[])
438 set_program_name(argv[0]);
439 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
440 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
441 fatal_ignore_sigpipe();
445 run_command(argc - 1, argv + 1, commands);
448 OVSTEST_REGISTER("test-vconn", test_vconn_main);