2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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 "poll-loop.h"
26 #include "socket-util.h"
28 #include "stream-ssl.h"
40 struct pstream *pstream;
44 check(int a, int b, const char *as, const char *file, int line)
47 ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
52 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
55 check_errno(int a, int b, const char *as, const char *file, int line)
58 ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
59 file, line, as, a, strerror(abs(a)), b, strerror(abs(b)));
63 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
66 fpv_create(const char *type, struct fake_pvconn *fpv)
69 if (!strcmp(type, "ssl")) {
70 stream_ssl_set_private_key_file("testpki-privkey.pem");
71 stream_ssl_set_certificate_file("testpki-cert.pem");
72 stream_ssl_set_ca_cert_file("testpki-cacert.pem", false);
77 if (!strcmp(type, "unix")) {
78 static int unix_count = 0;
81 bind_path = xasprintf("fake-pvconn.%d", unix_count++);
82 fpv->pvconn_name = xasprintf("punix:%s", bind_path);
83 fpv->vconn_name = xasprintf("unix:%s", bind_path);
84 CHECK_ERRNO(pstream_open(fpv->pvconn_name, &fpv->pstream), 0);
86 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
87 char *s, *port, *save_ptr = NULL;
90 open_name = xasprintf("p%s:0:127.0.0.1", type);
91 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream), 0);
93 /* Extract bound port number from pstream name. */
94 s = xstrdup(pstream_get_name(fpv->pstream));
95 strtok_r(s, ":", &save_ptr);
96 port = strtok_r(NULL, ":", &save_ptr);
99 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
100 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
109 static struct stream *
110 fpv_accept(struct fake_pvconn *fpv)
112 struct stream *stream;
114 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
120 fpv_close(struct fake_pvconn *fpv)
122 pstream_close(fpv->pstream);
127 fpv_destroy(struct fake_pvconn *fpv)
130 free(fpv->pvconn_name);
131 free(fpv->vconn_name);
134 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
135 * verifies that vconn_connect() reports 'expected_error'. */
137 test_refuse_connection(int argc OVS_UNUSED, char *argv[])
139 const char *type = argv[1];
141 struct fake_pvconn fpv;
144 expected_error = (!strcmp(type, "unix") ? EPIPE
145 : !strcmp(type, "tcp") ? ECONNRESET
148 fpv_create(type, &fpv);
149 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
152 CHECK_ERRNO(vconn_connect(vconn), expected_error);
157 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
158 * closes it immediately, and verifies that vconn_connect() reports
159 * 'expected_error'. */
161 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
163 const char *type = argv[1];
165 struct fake_pvconn fpv;
168 expected_error = (!strcmp(type, "unix") ? EPIPE
169 : !strcmp(type, "tcp") ? ECONNRESET
172 fpv_create(type, &fpv);
173 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
175 stream_close(fpv_accept(&fpv));
177 CHECK_ERRNO(vconn_connect(vconn), expected_error);
182 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
183 * reads the hello message from it, then closes the connection and verifies
184 * that vconn_connect() reports 'expected_error'. */
186 test_read_hello(int argc OVS_UNUSED, char *argv[])
188 const char *type = argv[1];
189 struct fake_pvconn fpv;
191 struct stream *stream;
193 fpv_create(type, &fpv);
194 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
196 stream = fpv_accept(&fpv);
199 struct ofp_header hello;
202 retval = stream_recv(stream, &hello, sizeof hello);
203 if (retval == sizeof hello) {
204 CHECK(hello.version, OFP_VERSION);
205 CHECK(hello.type, OFPT_HELLO);
206 CHECK(ntohs(hello.length), sizeof hello);
209 CHECK_ERRNO(retval, -EAGAIN);
213 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
214 vconn_run_wait(vconn);
215 vconn_connect_wait(vconn);
216 stream_recv_wait(stream);
219 stream_close(stream);
220 CHECK_ERRNO(vconn_connect(vconn), ECONNRESET);
224 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
225 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
226 * message), then verifies that vconn_connect() reports
227 * 'expect_connect_error'. */
229 test_send_hello(const char *type, const void *out, size_t out_size,
230 int expect_connect_error)
232 struct fake_pvconn fpv;
234 bool read_hello, connected;
236 struct stream *stream;
239 fpv_create(type, &fpv);
240 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP_VERSION, &vconn), 0);
242 stream = fpv_accept(&fpv);
246 while (n_sent < out_size) {
249 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
252 } else if (retval == -EAGAIN) {
255 stream_recv_wait(stream);
256 vconn_connect_wait(vconn);
257 vconn_run_wait(vconn);
260 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
264 read_hello = connected = false;
267 struct ofp_header hello;
268 int retval = stream_recv(stream, &hello, sizeof hello);
269 if (retval == sizeof hello) {
270 CHECK(hello.version, OFP_VERSION);
271 CHECK(hello.type, OFPT_HELLO);
272 CHECK(ntohs(hello.length), sizeof hello);
275 CHECK_ERRNO(retval, -EAGAIN);
281 int error = vconn_connect(vconn);
282 if (error == expect_connect_error) {
286 stream_close(stream);
291 CHECK_ERRNO(error, EAGAIN);
295 if (read_hello && connected) {
299 vconn_run_wait(vconn);
301 vconn_connect_wait(vconn);
304 stream_recv_wait(stream);
308 stream_close(stream);
309 CHECK_ERRNO(vconn_recv(vconn, &msg), EOF);
313 /* Try connecting and sending a normal hello, which should succeed. */
315 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
317 const char *type = argv[1];
318 struct ofp_header hello;
320 hello.version = OFP_VERSION;
321 hello.type = OFPT_HELLO;
322 hello.length = htons(sizeof hello);
323 hello.xid = htonl(0x12345678);
324 test_send_hello(type, &hello, sizeof hello, 0);
327 /* Try connecting and sending an extra-long hello, which should succeed (since
328 * the specification says that implementations must accept and ignore extra
331 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
333 const char *type = argv[1];
334 struct ofp_header hello;
335 char buffer[sizeof hello * 2];
337 hello.version = OFP_VERSION;
338 hello.type = OFPT_HELLO;
339 hello.length = htons(sizeof buffer);
340 hello.xid = htonl(0x12345678);
341 memset(buffer, 0, sizeof buffer);
342 memcpy(buffer, &hello, sizeof hello);
343 test_send_hello(type, buffer, sizeof buffer, 0);
346 /* Try connecting and sending an echo request instead of a hello, which should
347 * fail with EPROTO. */
349 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
351 const char *type = argv[1];
352 struct ofp_header echo;
354 echo.version = OFP_VERSION;
355 echo.type = OFPT_ECHO_REQUEST;
356 echo.length = htons(sizeof echo);
357 echo.xid = htonl(0x89abcdef);
358 test_send_hello(type, &echo, sizeof echo, EPROTO);
361 /* Try connecting and sending a hello packet that has its length field as 0,
362 * which should fail with EPROTO. */
364 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
366 const char *type = argv[1];
367 struct ofp_header hello;
369 memset(&hello, 0, sizeof hello);
370 test_send_hello(type, &hello, sizeof hello, EPROTO);
373 /* Try connecting and sending a hello packet that has a bad version, which
374 * should fail with EPROTO. */
376 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
378 const char *type = argv[1];
379 struct ofp_header hello;
381 hello.version = OFP_VERSION - 1;
382 hello.type = OFPT_HELLO;
383 hello.length = htons(sizeof hello);
384 hello.xid = htonl(0x12345678);
385 test_send_hello(type, &hello, sizeof hello, EPROTO);
388 static const struct command commands[] = {
389 {"refuse-connection", 1, 1, test_refuse_connection},
390 {"accept-then-close", 1, 1, test_accept_then_close},
391 {"read-hello", 1, 1, test_read_hello},
392 {"send-plain-hello", 1, 1, test_send_plain_hello},
393 {"send-long-hello", 1, 1, test_send_long_hello},
394 {"send-echo-hello", 1, 1, test_send_echo_hello},
395 {"send-short-hello", 1, 1, test_send_short_hello},
396 {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
401 main(int argc, char *argv[])
403 set_program_name(argv[0]);
404 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
405 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
406 signal(SIGPIPE, SIG_IGN);
410 run_command(argc - 1, argv + 1, commands);