2 * Copyright (c) 2009, 2010, 2011, 2012 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"
28 #include "openflow/openflow.h"
29 #include "poll-loop.h"
30 #include "socket-util.h"
32 #include "stream-ssl.h"
44 struct pstream *pstream;
48 check(int a, int b, const char *as, const char *file, int line)
51 ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
56 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
59 check_errno(int a, int b, const char *as, const char *file, int line)
62 ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
63 file, line, as, a, strerror(abs(a)), b, strerror(abs(b)));
67 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
70 fpv_create(const char *type, struct fake_pvconn *fpv)
73 if (!strcmp(type, "ssl")) {
74 stream_ssl_set_private_key_file("testpki-privkey.pem");
75 stream_ssl_set_certificate_file("testpki-cert.pem");
76 stream_ssl_set_ca_cert_file("testpki-cacert.pem", false);
81 if (!strcmp(type, "unix")) {
82 static int unix_count = 0;
85 bind_path = xasprintf("fake-pvconn.%d", unix_count++);
86 fpv->pvconn_name = xasprintf("punix:%s", bind_path);
87 fpv->vconn_name = xasprintf("unix:%s", bind_path);
88 CHECK_ERRNO(pstream_open(fpv->pvconn_name, &fpv->pstream,
91 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
92 char *s, *port, *save_ptr = NULL;
95 open_name = xasprintf("p%s:0:127.0.0.1", type);
96 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream, DSCP_DEFAULT), 0);
98 /* Extract bound port number from pstream name. */
99 s = xstrdup(pstream_get_name(fpv->pstream));
100 strtok_r(s, ":", &save_ptr);
101 port = strtok_r(NULL, ":", &save_ptr);
104 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
105 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
114 static struct stream *
115 fpv_accept(struct fake_pvconn *fpv)
117 struct stream *stream;
119 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
125 fpv_close(struct fake_pvconn *fpv)
127 pstream_close(fpv->pstream);
132 fpv_destroy(struct fake_pvconn *fpv)
135 free(fpv->pvconn_name);
136 free(fpv->vconn_name);
139 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
140 * verifies that vconn_connect() reports 'expected_error'. */
142 test_refuse_connection(int argc OVS_UNUSED, char *argv[])
144 const char *type = argv[1];
146 struct fake_pvconn fpv;
149 expected_error = (!strcmp(type, "unix") ? EPIPE
150 : !strcmp(type, "tcp") ? ECONNRESET
153 fpv_create(type, &fpv);
154 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
158 CHECK_ERRNO(vconn_connect(vconn), expected_error);
163 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
164 * closes it immediately, and verifies that vconn_connect() reports
165 * 'expected_error'. */
167 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
169 const char *type = argv[1];
171 struct fake_pvconn fpv;
174 expected_error = (!strcmp(type, "unix") ? EPIPE
175 : !strcmp(type, "tcp") ? ECONNRESET
178 fpv_create(type, &fpv);
179 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
182 stream_close(fpv_accept(&fpv));
184 CHECK_ERRNO(vconn_connect(vconn), expected_error);
189 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
190 * reads the hello message from it, then closes the connection and verifies
191 * that vconn_connect() reports 'expected_error'. */
193 test_read_hello(int argc OVS_UNUSED, char *argv[])
195 const char *type = argv[1];
196 struct fake_pvconn fpv;
198 struct stream *stream;
200 fpv_create(type, &fpv);
201 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
204 stream = fpv_accept(&fpv);
207 struct ofp_header hello;
210 retval = stream_recv(stream, &hello, sizeof hello);
211 if (retval == sizeof hello) {
214 CHECK(hello.version, OFP10_VERSION);
215 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
216 CHECK(raw, OFPRAW_OFPT_HELLO);
217 CHECK(ntohs(hello.length), sizeof hello);
220 CHECK_ERRNO(retval, -EAGAIN);
224 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
225 vconn_run_wait(vconn);
226 vconn_connect_wait(vconn);
227 stream_recv_wait(stream);
230 stream_close(stream);
231 CHECK_ERRNO(vconn_connect(vconn), ECONNRESET);
235 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
236 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
237 * message), then verifies that vconn_connect() reports
238 * 'expect_connect_error'. */
240 test_send_hello(const char *type, const void *out, size_t out_size,
241 int expect_connect_error)
243 struct fake_pvconn fpv;
245 bool read_hello, connected;
247 struct stream *stream;
250 fpv_create(type, &fpv);
251 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
254 stream = fpv_accept(&fpv);
258 while (n_sent < out_size) {
261 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
264 } else if (retval == -EAGAIN) {
267 stream_recv_wait(stream);
268 vconn_connect_wait(vconn);
269 vconn_run_wait(vconn);
272 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
276 read_hello = connected = false;
279 struct ofp_header hello;
280 int retval = stream_recv(stream, &hello, sizeof hello);
281 if (retval == sizeof hello) {
284 CHECK(hello.version, OFP10_VERSION);
285 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
286 CHECK(raw, OFPRAW_OFPT_HELLO);
287 CHECK(ntohs(hello.length), sizeof hello);
290 CHECK_ERRNO(retval, -EAGAIN);
296 int error = vconn_connect(vconn);
297 if (error == expect_connect_error) {
301 stream_close(stream);
306 CHECK_ERRNO(error, EAGAIN);
310 if (read_hello && connected) {
314 vconn_run_wait(vconn);
316 vconn_connect_wait(vconn);
319 stream_recv_wait(stream);
323 stream_close(stream);
324 CHECK_ERRNO(vconn_recv(vconn, &msg), EOF);
328 /* Try connecting and sending a normal hello, which should succeed. */
330 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
332 const char *type = argv[1];
333 struct ofpbuf *hello;
335 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
336 htonl(0x12345678), 0);
337 test_send_hello(type, hello->data, hello->size, 0);
338 ofpbuf_delete(hello);
341 /* Try connecting and sending an extra-long hello, which should succeed (since
342 * the specification says that implementations must accept and ignore extra
345 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
347 const char *type = argv[1];
348 struct ofpbuf *hello;
349 enum { EXTRA_BYTES = 8 };
351 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
352 htonl(0x12345678), EXTRA_BYTES);
353 ofpbuf_put_zeros(hello, EXTRA_BYTES);
354 ofpmsg_update_length(hello);
355 test_send_hello(type, hello->data, hello->size, 0);
356 ofpbuf_delete(hello);
359 /* Try connecting and sending an echo request instead of a hello, which should
360 * fail with EPROTO. */
362 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
364 const char *type = argv[1];
367 echo = ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
368 htonl(0x12345678), 0);
369 test_send_hello(type, echo->data, echo->size, EPROTO);
373 /* Try connecting and sending a hello packet that has its length field as 0,
374 * which should fail with EPROTO. */
376 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
378 const char *type = argv[1];
379 struct ofp_header hello;
381 memset(&hello, 0, sizeof hello);
382 test_send_hello(type, &hello, sizeof hello, EPROTO);
385 /* Try connecting and sending a hello packet that has a bad version, which
386 * should fail with EPROTO. */
388 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
390 const char *type = argv[1];
391 struct ofpbuf *hello;
393 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
394 htonl(0x12345678), 0);
395 ((struct ofp_header *) hello->data)->version = 0;
396 test_send_hello(type, hello->data, hello->size, EPROTO);
397 ofpbuf_delete(hello);
400 static const struct command commands[] = {
401 {"refuse-connection", 1, 1, test_refuse_connection},
402 {"accept-then-close", 1, 1, test_accept_then_close},
403 {"read-hello", 1, 1, test_read_hello},
404 {"send-plain-hello", 1, 1, test_send_plain_hello},
405 {"send-long-hello", 1, 1, test_send_long_hello},
406 {"send-echo-hello", 1, 1, test_send_echo_hello},
407 {"send-short-hello", 1, 1, test_send_short_hello},
408 {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
413 main(int argc, char *argv[])
415 set_program_name(argv[0]);
416 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
417 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
418 signal(SIGPIPE, SIG_IGN);
422 run_command(argc - 1, argv + 1, commands);