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,
88 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
89 char *s, *port, *save_ptr = NULL;
92 open_name = xasprintf("p%s:0:127.0.0.1", type);
93 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream, DSCP_DEFAULT), 0);
95 /* Extract bound port number from pstream name. */
96 s = xstrdup(pstream_get_name(fpv->pstream));
97 strtok_r(s, ":", &save_ptr);
98 port = strtok_r(NULL, ":", &save_ptr);
101 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
102 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
111 static struct stream *
112 fpv_accept(struct fake_pvconn *fpv)
114 struct stream *stream;
116 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
122 fpv_close(struct fake_pvconn *fpv)
124 pstream_close(fpv->pstream);
129 fpv_destroy(struct fake_pvconn *fpv)
132 free(fpv->pvconn_name);
133 free(fpv->vconn_name);
136 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
137 * verifies that vconn_connect() reports 'expected_error'. */
139 test_refuse_connection(int argc OVS_UNUSED, char *argv[])
141 const char *type = argv[1];
143 struct fake_pvconn fpv;
146 expected_error = (!strcmp(type, "unix") ? EPIPE
147 : !strcmp(type, "tcp") ? ECONNRESET
150 fpv_create(type, &fpv);
151 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
155 CHECK_ERRNO(vconn_connect(vconn), expected_error);
160 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
161 * closes it immediately, and verifies that vconn_connect() reports
162 * 'expected_error'. */
164 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
166 const char *type = argv[1];
168 struct fake_pvconn fpv;
171 expected_error = (!strcmp(type, "unix") ? EPIPE
172 : !strcmp(type, "tcp") ? ECONNRESET
175 fpv_create(type, &fpv);
176 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
179 stream_close(fpv_accept(&fpv));
181 CHECK_ERRNO(vconn_connect(vconn), expected_error);
186 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
187 * reads the hello message from it, then closes the connection and verifies
188 * that vconn_connect() reports 'expected_error'. */
190 test_read_hello(int argc OVS_UNUSED, char *argv[])
192 const char *type = argv[1];
193 struct fake_pvconn fpv;
195 struct stream *stream;
197 fpv_create(type, &fpv);
198 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
201 stream = fpv_accept(&fpv);
204 struct ofp_header hello;
207 retval = stream_recv(stream, &hello, sizeof hello);
208 if (retval == sizeof hello) {
209 CHECK(hello.version, OFP10_VERSION);
210 CHECK(hello.type, OFPT_HELLO);
211 CHECK(ntohs(hello.length), sizeof hello);
214 CHECK_ERRNO(retval, -EAGAIN);
218 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
219 vconn_run_wait(vconn);
220 vconn_connect_wait(vconn);
221 stream_recv_wait(stream);
224 stream_close(stream);
225 CHECK_ERRNO(vconn_connect(vconn), ECONNRESET);
229 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
230 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
231 * message), then verifies that vconn_connect() reports
232 * 'expect_connect_error'. */
234 test_send_hello(const char *type, const void *out, size_t out_size,
235 int expect_connect_error)
237 struct fake_pvconn fpv;
239 bool read_hello, connected;
241 struct stream *stream;
244 fpv_create(type, &fpv);
245 CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
248 stream = fpv_accept(&fpv);
252 while (n_sent < out_size) {
255 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
258 } else if (retval == -EAGAIN) {
261 stream_recv_wait(stream);
262 vconn_connect_wait(vconn);
263 vconn_run_wait(vconn);
266 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
270 read_hello = connected = false;
273 struct ofp_header hello;
274 int retval = stream_recv(stream, &hello, sizeof hello);
275 if (retval == sizeof hello) {
276 CHECK(hello.version, OFP10_VERSION);
277 CHECK(hello.type, OFPT_HELLO);
278 CHECK(ntohs(hello.length), sizeof hello);
281 CHECK_ERRNO(retval, -EAGAIN);
287 int error = vconn_connect(vconn);
288 if (error == expect_connect_error) {
292 stream_close(stream);
297 CHECK_ERRNO(error, EAGAIN);
301 if (read_hello && connected) {
305 vconn_run_wait(vconn);
307 vconn_connect_wait(vconn);
310 stream_recv_wait(stream);
314 stream_close(stream);
315 CHECK_ERRNO(vconn_recv(vconn, &msg), EOF);
319 /* Try connecting and sending a normal hello, which should succeed. */
321 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
323 const char *type = argv[1];
324 struct ofp_header hello;
326 hello.version = OFP10_VERSION;
327 hello.type = OFPT_HELLO;
328 hello.length = htons(sizeof hello);
329 hello.xid = htonl(0x12345678);
330 test_send_hello(type, &hello, sizeof hello, 0);
333 /* Try connecting and sending an extra-long hello, which should succeed (since
334 * the specification says that implementations must accept and ignore extra
337 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
339 const char *type = argv[1];
340 struct ofp_header hello;
341 char buffer[sizeof hello * 2];
343 hello.version = OFP10_VERSION;
344 hello.type = OFPT_HELLO;
345 hello.length = htons(sizeof buffer);
346 hello.xid = htonl(0x12345678);
347 memset(buffer, 0, sizeof buffer);
348 memcpy(buffer, &hello, sizeof hello);
349 test_send_hello(type, buffer, sizeof buffer, 0);
352 /* Try connecting and sending an echo request instead of a hello, which should
353 * fail with EPROTO. */
355 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
357 const char *type = argv[1];
358 struct ofp_header echo;
360 echo.version = OFP10_VERSION;
361 echo.type = OFPT_ECHO_REQUEST;
362 echo.length = htons(sizeof echo);
363 echo.xid = htonl(0x89abcdef);
364 test_send_hello(type, &echo, sizeof echo, EPROTO);
367 /* Try connecting and sending a hello packet that has its length field as 0,
368 * which should fail with EPROTO. */
370 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
372 const char *type = argv[1];
373 struct ofp_header hello;
375 memset(&hello, 0, sizeof hello);
376 test_send_hello(type, &hello, sizeof hello, EPROTO);
379 /* Try connecting and sending a hello packet that has a bad version, which
380 * should fail with EPROTO. */
382 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
384 const char *type = argv[1];
385 struct ofp_header hello;
387 hello.version = OFP10_VERSION - 1;
388 hello.type = OFPT_HELLO;
389 hello.length = htons(sizeof hello);
390 hello.xid = htonl(0x12345678);
391 test_send_hello(type, &hello, sizeof hello, EPROTO);
394 static const struct command commands[] = {
395 {"refuse-connection", 1, 1, test_refuse_connection},
396 {"accept-then-close", 1, 1, test_accept_then_close},
397 {"read-hello", 1, 1, test_read_hello},
398 {"send-plain-hello", 1, 1, test_send_plain_hello},
399 {"send-long-hello", 1, 1, test_send_long_hello},
400 {"send-echo-hello", 1, 1, test_send_echo_hello},
401 {"send-short-hello", 1, 1, test_send_short_hello},
402 {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
407 main(int argc, char *argv[])
409 set_program_name(argv[0]);
410 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
411 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
412 signal(SIGPIPE, SIG_IGN);
416 run_command(argc - 1, argv + 1, commands);