2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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"
45 struct pstream *pstream;
49 check(int a, int b, const char *as, const char *file, int line)
52 ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
57 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
60 check_errno(int a, int b, const char *as, const char *file, int line)
63 char *str_b = strdup(ovs_strerror(abs(b)));
64 ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
65 file, line, as, a, ovs_strerror(abs(a)), b, str_b);
69 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
72 fpv_create(const char *type, struct fake_pvconn *fpv)
75 if (!strcmp(type, "ssl")) {
76 stream_ssl_set_private_key_file("testpki-privkey.pem");
77 stream_ssl_set_certificate_file("testpki-cert.pem");
78 stream_ssl_set_ca_cert_file("testpki-cacert.pem", false);
83 if (!strcmp(type, "unix")) {
84 static int unix_count = 0;
87 bind_path = xasprintf("fake-pvconn.%d", unix_count++);
88 fpv->pvconn_name = xasprintf("punix:%s", bind_path);
89 fpv->vconn_name = xasprintf("unix:%s", bind_path);
90 CHECK_ERRNO(pstream_open(fpv->pvconn_name, &fpv->pstream,
93 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
94 char *s, *port, *save_ptr = NULL;
97 open_name = xasprintf("p%s:0:127.0.0.1", type);
98 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream, DSCP_DEFAULT), 0);
100 /* Extract bound port number from pstream name. */
101 s = xstrdup(pstream_get_name(fpv->pstream));
102 strtok_r(s, ":", &save_ptr);
103 port = strtok_r(NULL, ":", &save_ptr);
106 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
107 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
116 static struct stream *
117 fpv_accept(struct fake_pvconn *fpv)
119 struct stream *stream;
121 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
127 fpv_close(struct fake_pvconn *fpv)
129 pstream_close(fpv->pstream);
134 fpv_destroy(struct fake_pvconn *fpv)
137 free(fpv->pvconn_name);
138 free(fpv->vconn_name);
141 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
142 * verifies that vconn_connect() reports 'expected_error'. */
144 test_refuse_connection(int argc OVS_UNUSED, char *argv[])
146 const char *type = argv[1];
147 struct fake_pvconn fpv;
151 fpv_create(type, &fpv);
152 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
156 error = vconn_connect_block(vconn);
157 if (!strcmp(type, "tcp")) {
158 if (error != ECONNRESET && error != EPIPE) {
159 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
160 error, ovs_strerror(error));
162 } else if (!strcmp(type, "unix")) {
163 CHECK_ERRNO(error, EPIPE);
164 } else if (!strcmp(type, "ssl")) {
165 if (error != EPROTO && error != ECONNRESET) {
166 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
167 error, ovs_strerror(error));
170 ovs_fatal(0, "invalid connection type %s", type);
177 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
178 * closes it immediately, and verifies that vconn_connect() reports
179 * 'expected_error'. */
181 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
183 const char *type = argv[1];
184 struct fake_pvconn fpv;
188 fpv_create(type, &fpv);
189 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
191 stream_close(fpv_accept(&fpv));
194 error = vconn_connect_block(vconn);
195 if (!strcmp(type, "tcp") || !strcmp(type, "unix")) {
196 if (error != ECONNRESET && error != EPIPE) {
197 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
198 error, ovs_strerror(error));
201 CHECK_ERRNO(error, EPROTO);
208 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
209 * reads the hello message from it, then closes the connection and verifies
210 * that vconn_connect() reports 'expected_error'. */
212 test_read_hello(int argc OVS_UNUSED, char *argv[])
214 const char *type = argv[1];
215 struct fake_pvconn fpv;
217 struct stream *stream;
220 fpv_create(type, &fpv);
221 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
223 stream = fpv_accept(&fpv);
226 struct ofp_header hello;
229 retval = stream_recv(stream, &hello, sizeof hello);
230 if (retval == sizeof hello) {
233 CHECK(hello.version, OFP10_VERSION);
234 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
235 CHECK(raw, OFPRAW_OFPT_HELLO);
236 CHECK(ntohs(hello.length), sizeof hello);
239 CHECK_ERRNO(retval, -EAGAIN);
243 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
244 vconn_run_wait(vconn);
245 vconn_connect_wait(vconn);
246 stream_recv_wait(stream);
249 stream_close(stream);
250 error = vconn_connect_block(vconn);
251 if (error != ECONNRESET && error != EPIPE) {
252 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
253 error, ovs_strerror(error));
258 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
259 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
260 * message), then verifies that vconn_connect() reports
261 * 'expect_connect_error'. */
263 test_send_hello(const char *type, const void *out, size_t out_size,
264 int expect_connect_error)
266 struct fake_pvconn fpv;
268 bool read_hello, connected;
270 struct stream *stream;
273 fpv_create(type, &fpv);
274 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
276 stream = fpv_accept(&fpv);
280 while (n_sent < out_size) {
283 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
286 } else if (retval == -EAGAIN) {
289 stream_recv_wait(stream);
290 vconn_connect_wait(vconn);
291 vconn_run_wait(vconn);
294 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
298 read_hello = connected = false;
301 struct ofp_header hello;
302 int retval = stream_recv(stream, &hello, sizeof hello);
303 if (retval == sizeof hello) {
306 CHECK(hello.version, OFP10_VERSION);
307 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
308 CHECK(raw, OFPRAW_OFPT_HELLO);
309 CHECK(ntohs(hello.length), sizeof hello);
312 CHECK_ERRNO(retval, -EAGAIN);
318 int error = vconn_connect(vconn);
319 if (error == expect_connect_error) {
323 stream_close(stream);
328 CHECK_ERRNO(error, EAGAIN);
332 if (read_hello && connected) {
336 vconn_run_wait(vconn);
338 vconn_connect_wait(vconn);
341 stream_recv_wait(stream);
345 stream_close(stream);
346 CHECK_ERRNO(vconn_recv_block(vconn, &msg), EOF);
350 /* Try connecting and sending a normal hello, which should succeed. */
352 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
354 const char *type = argv[1];
355 struct ofpbuf *hello;
357 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
358 htonl(0x12345678), 0);
359 test_send_hello(type, hello->data, hello->size, 0);
360 ofpbuf_delete(hello);
363 /* Try connecting and sending an extra-long hello, which should succeed (since
364 * the specification says that implementations must accept and ignore extra
367 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
369 const char *type = argv[1];
370 struct ofpbuf *hello;
371 enum { EXTRA_BYTES = 8 };
373 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
374 htonl(0x12345678), EXTRA_BYTES);
375 ofpbuf_put_zeros(hello, EXTRA_BYTES);
376 ofpmsg_update_length(hello);
377 test_send_hello(type, hello->data, hello->size, 0);
378 ofpbuf_delete(hello);
381 /* Try connecting and sending an echo request instead of a hello, which should
382 * fail with EPROTO. */
384 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
386 const char *type = argv[1];
389 echo = ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
390 htonl(0x12345678), 0);
391 test_send_hello(type, echo->data, echo->size, EPROTO);
395 /* Try connecting and sending a hello packet that has its length field as 0,
396 * which should fail with EPROTO. */
398 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
400 const char *type = argv[1];
401 struct ofp_header hello;
403 memset(&hello, 0, sizeof hello);
404 test_send_hello(type, &hello, sizeof hello, EPROTO);
407 /* Try connecting and sending a hello packet that has a bad version, which
408 * should fail with EPROTO. */
410 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
412 const char *type = argv[1];
413 struct ofpbuf *hello;
415 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
416 htonl(0x12345678), 0);
417 ((struct ofp_header *) hello->data)->version = 0;
418 test_send_hello(type, hello->data, hello->size, EPROTO);
419 ofpbuf_delete(hello);
422 static const struct command commands[] = {
423 {"refuse-connection", 1, 1, test_refuse_connection},
424 {"accept-then-close", 1, 1, test_accept_then_close},
425 {"read-hello", 1, 1, test_read_hello},
426 {"send-plain-hello", 1, 1, test_send_plain_hello},
427 {"send-long-hello", 1, 1, test_send_long_hello},
428 {"send-echo-hello", 1, 1, test_send_echo_hello},
429 {"send-short-hello", 1, 1, test_send_short_hello},
430 {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
435 main(int argc, char *argv[])
437 set_program_name(argv[0]);
438 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
439 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
440 fatal_ignore_sigpipe();
444 run_command(argc - 1, argv + 1, commands);