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 "openflow/openflow.h"
26 #include "poll-loop.h"
27 #include "socket-util.h"
29 #include "stream-ssl.h"
41 struct pstream *pstream;
45 check(int a, int b, const char *as, const char *file, int line)
48 ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
53 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
56 check_errno(int a, int b, const char *as, const char *file, int line)
59 ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
60 file, line, as, a, strerror(abs(a)), b, strerror(abs(b)));
64 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
67 fpv_create(const char *type, struct fake_pvconn *fpv)
70 if (!strcmp(type, "ssl")) {
71 stream_ssl_set_private_key_file("testpki-privkey.pem");
72 stream_ssl_set_certificate_file("testpki-cert.pem");
73 stream_ssl_set_ca_cert_file("testpki-cacert.pem", false);
78 if (!strcmp(type, "unix")) {
79 static int unix_count = 0;
82 bind_path = xasprintf("fake-pvconn.%d", unix_count++);
83 fpv->pvconn_name = xasprintf("punix:%s", bind_path);
84 fpv->vconn_name = xasprintf("unix:%s", bind_path);
85 CHECK_ERRNO(pstream_open(fpv->pvconn_name, &fpv->pstream), 0);
87 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
88 char *s, *port, *save_ptr = NULL;
91 open_name = xasprintf("p%s:0:127.0.0.1", type);
92 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream), 0);
94 /* Extract bound port number from pstream name. */
95 s = xstrdup(pstream_get_name(fpv->pstream));
96 strtok_r(s, ":", &save_ptr);
97 port = strtok_r(NULL, ":", &save_ptr);
100 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
101 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
110 static struct stream *
111 fpv_accept(struct fake_pvconn *fpv)
113 struct stream *stream;
115 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
121 fpv_close(struct fake_pvconn *fpv)
123 pstream_close(fpv->pstream);
128 fpv_destroy(struct fake_pvconn *fpv)
131 free(fpv->pvconn_name);
132 free(fpv->vconn_name);
135 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
136 * verifies that vconn_connect() reports 'expected_error'. */
138 test_refuse_connection(int argc OVS_UNUSED, char *argv[])
140 const char *type = argv[1];
142 struct fake_pvconn fpv;
145 expected_error = (!strcmp(type, "unix") ? EPIPE
146 : !strcmp(type, "tcp") ? ECONNRESET
149 fpv_create(type, &fpv);
150 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn), 0);
153 CHECK_ERRNO(vconn_connect(vconn), expected_error);
158 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
159 * closes it immediately, and verifies that vconn_connect() reports
160 * 'expected_error'. */
162 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
164 const char *type = argv[1];
166 struct fake_pvconn fpv;
169 expected_error = (!strcmp(type, "unix") ? EPIPE
170 : !strcmp(type, "tcp") ? ECONNRESET
173 fpv_create(type, &fpv);
174 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn), 0);
176 stream_close(fpv_accept(&fpv));
178 CHECK_ERRNO(vconn_connect(vconn), expected_error);
183 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
184 * reads the hello message from it, then closes the connection and verifies
185 * that vconn_connect() reports 'expected_error'. */
187 test_read_hello(int argc OVS_UNUSED, char *argv[])
189 const char *type = argv[1];
190 struct fake_pvconn fpv;
192 struct stream *stream;
194 fpv_create(type, &fpv);
195 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn), 0);
197 stream = fpv_accept(&fpv);
200 struct ofp_header hello;
203 retval = stream_recv(stream, &hello, sizeof hello);
204 if (retval == sizeof hello) {
205 CHECK(hello.version, OFP10_VERSION);
206 CHECK(hello.type, OFPT_HELLO);
207 CHECK(ntohs(hello.length), sizeof hello);
210 CHECK_ERRNO(retval, -EAGAIN);
214 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
215 vconn_run_wait(vconn);
216 vconn_connect_wait(vconn);
217 stream_recv_wait(stream);
220 stream_close(stream);
221 CHECK_ERRNO(vconn_connect(vconn), ECONNRESET);
225 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
226 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
227 * message), then verifies that vconn_connect() reports
228 * 'expect_connect_error'. */
230 test_send_hello(const char *type, const void *out, size_t out_size,
231 int expect_connect_error)
233 struct fake_pvconn fpv;
235 bool read_hello, connected;
237 struct stream *stream;
240 fpv_create(type, &fpv);
241 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn), 0);
243 stream = fpv_accept(&fpv);
247 while (n_sent < out_size) {
250 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
253 } else if (retval == -EAGAIN) {
256 stream_recv_wait(stream);
257 vconn_connect_wait(vconn);
258 vconn_run_wait(vconn);
261 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
265 read_hello = connected = false;
268 struct ofp_header hello;
269 int retval = stream_recv(stream, &hello, sizeof hello);
270 if (retval == sizeof hello) {
271 CHECK(hello.version, OFP10_VERSION);
272 CHECK(hello.type, OFPT_HELLO);
273 CHECK(ntohs(hello.length), sizeof hello);
276 CHECK_ERRNO(retval, -EAGAIN);
282 int error = vconn_connect(vconn);
283 if (error == expect_connect_error) {
287 stream_close(stream);
292 CHECK_ERRNO(error, EAGAIN);
296 if (read_hello && connected) {
300 vconn_run_wait(vconn);
302 vconn_connect_wait(vconn);
305 stream_recv_wait(stream);
309 stream_close(stream);
310 CHECK_ERRNO(vconn_recv(vconn, &msg), EOF);
314 /* Try connecting and sending a normal hello, which should succeed. */
316 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
318 const char *type = argv[1];
319 struct ofp_header hello;
321 hello.version = OFP10_VERSION;
322 hello.type = OFPT_HELLO;
323 hello.length = htons(sizeof hello);
324 hello.xid = htonl(0x12345678);
325 test_send_hello(type, &hello, sizeof hello, 0);
328 /* Try connecting and sending an extra-long hello, which should succeed (since
329 * the specification says that implementations must accept and ignore extra
332 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
334 const char *type = argv[1];
335 struct ofp_header hello;
336 char buffer[sizeof hello * 2];
338 hello.version = OFP10_VERSION;
339 hello.type = OFPT_HELLO;
340 hello.length = htons(sizeof buffer);
341 hello.xid = htonl(0x12345678);
342 memset(buffer, 0, sizeof buffer);
343 memcpy(buffer, &hello, sizeof hello);
344 test_send_hello(type, buffer, sizeof buffer, 0);
347 /* Try connecting and sending an echo request instead of a hello, which should
348 * fail with EPROTO. */
350 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
352 const char *type = argv[1];
353 struct ofp_header echo;
355 echo.version = OFP10_VERSION;
356 echo.type = OFPT_ECHO_REQUEST;
357 echo.length = htons(sizeof echo);
358 echo.xid = htonl(0x89abcdef);
359 test_send_hello(type, &echo, sizeof echo, EPROTO);
362 /* Try connecting and sending a hello packet that has its length field as 0,
363 * which should fail with EPROTO. */
365 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
367 const char *type = argv[1];
368 struct ofp_header hello;
370 memset(&hello, 0, sizeof hello);
371 test_send_hello(type, &hello, sizeof hello, EPROTO);
374 /* Try connecting and sending a hello packet that has a bad version, which
375 * should fail with EPROTO. */
377 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
379 const char *type = argv[1];
380 struct ofp_header hello;
382 hello.version = OFP10_VERSION - 1;
383 hello.type = OFPT_HELLO;
384 hello.length = htons(sizeof hello);
385 hello.xid = htonl(0x12345678);
386 test_send_hello(type, &hello, sizeof hello, EPROTO);
389 static const struct command commands[] = {
390 {"refuse-connection", 1, 1, test_refuse_connection},
391 {"accept-then-close", 1, 1, test_accept_then_close},
392 {"read-hello", 1, 1, test_read_hello},
393 {"send-plain-hello", 1, 1, test_send_plain_hello},
394 {"send-long-hello", 1, 1, test_send_long_hello},
395 {"send-echo-hello", 1, 1, test_send_echo_hello},
396 {"send-short-hello", 1, 1, test_send_short_hello},
397 {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
402 main(int argc, char *argv[])
404 set_program_name(argv[0]);
405 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
406 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
407 signal(SIGPIPE, SIG_IGN);
411 run_command(argc - 1, argv + 1, commands);