2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2017 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.
25 #include "command-line.h"
26 #include "fatal-signal.h"
27 #include "openflow/openflow.h"
28 #include "openvswitch/ofp-msgs.h"
29 #include "openvswitch/ofp-util.h"
30 #include "openvswitch/ofpbuf.h"
31 #include "openvswitch/vconn.h"
32 #include "openvswitch/vlog.h"
34 #include "poll-loop.h"
35 #include "socket-util.h"
37 #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
= xstrdup(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(struct ovs_cmdl_context
*ctx
)
146 const char *type
= ctx
->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
160 && error
!= WSAECONNRESET
163 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
164 error
, ovs_strerror(error
));
166 } else if (!strcmp(type
, "unix")) {
168 CHECK_ERRNO(error
, EPIPE
);
170 CHECK_ERRNO(error
, WSAECONNRESET
);
172 } else if (!strcmp(type
, "ssl")) {
173 if (error
!= EPROTO
&& error
!= ECONNRESET
) {
174 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
175 error
, ovs_strerror(error
));
178 ovs_fatal(0, "invalid connection type %s", type
);
185 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
186 * closes it immediately, and verifies that vconn_connect() reports
187 * 'expected_error'. */
189 test_accept_then_close(struct ovs_cmdl_context
*ctx
)
191 const char *type
= ctx
->argv
[1];
192 struct fake_pvconn fpv
;
196 fpv_create(type
, &fpv
);
197 CHECK_ERRNO(vconn_open(fpv
.vconn_name
, 0, DSCP_DEFAULT
, &vconn
), 0);
199 stream_close(fpv_accept(&fpv
));
202 error
= vconn_connect_block(vconn
);
203 if (!strcmp(type
, "tcp") || !strcmp(type
, "unix")) {
204 if (error
!= ECONNRESET
&& error
!= EPIPE
206 && error
!= WSAECONNRESET
209 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
210 error
, ovs_strerror(error
));
213 CHECK_ERRNO(error
, EPROTO
);
220 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
221 * reads the hello message from it, then closes the connection and verifies
222 * that vconn_connect() reports 'expected_error'. */
224 test_read_hello(struct ovs_cmdl_context
*ctx
)
226 const char *type
= ctx
->argv
[1];
227 struct fake_pvconn fpv
;
229 struct stream
*stream
;
232 fpv_create(type
, &fpv
);
233 CHECK_ERRNO(vconn_open(fpv
.vconn_name
, 0, DSCP_DEFAULT
, &vconn
), 0);
235 stream
= fpv_accept(&fpv
);
238 struct ofp_header hello
;
241 retval
= stream_recv(stream
, &hello
, sizeof hello
);
242 if (retval
== sizeof hello
) {
245 CHECK(hello
.version
, OFP14_VERSION
);
246 CHECK(ofpraw_decode_partial(&raw
, &hello
, sizeof hello
), 0);
247 CHECK(raw
, OFPRAW_OFPT_HELLO
);
248 CHECK(ntohs(hello
.length
), sizeof hello
);
251 CHECK_ERRNO(retval
, -EAGAIN
);
255 CHECK_ERRNO(vconn_connect(vconn
), EAGAIN
);
256 vconn_run_wait(vconn
);
257 vconn_connect_wait(vconn
);
258 stream_recv_wait(stream
);
261 stream_close(stream
);
262 error
= vconn_connect_block(vconn
);
263 if (error
!= ECONNRESET
&& error
!= EPIPE
) {
264 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
265 error
, ovs_strerror(error
));
270 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
271 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
272 * message), then verifies that vconn_connect() reports
273 * 'expect_connect_error'. */
275 test_send_hello(const char *type
, const void *out
, size_t out_size
,
276 int expect_connect_error
)
278 struct fake_pvconn fpv
;
280 bool read_hello
, connected
;
282 struct stream
*stream
;
285 fpv_create(type
, &fpv
);
286 CHECK_ERRNO(vconn_open(fpv
.vconn_name
, 0, DSCP_DEFAULT
, &vconn
), 0);
288 stream
= fpv_accept(&fpv
);
292 while (n_sent
< out_size
) {
295 retval
= stream_send(stream
, (char *) out
+ n_sent
, out_size
- n_sent
);
298 } else if (retval
== -EAGAIN
) {
301 stream_recv_wait(stream
);
302 vconn_connect_wait(vconn
);
303 vconn_run_wait(vconn
);
306 ovs_fatal(0, "stream_send returned unexpected value %d", retval
);
310 read_hello
= connected
= false;
313 struct ofp_header hello
;
314 int retval
= stream_recv(stream
, &hello
, sizeof hello
);
315 if (retval
== sizeof hello
) {
318 CHECK(hello
.version
, OFP14_VERSION
);
319 CHECK(ofpraw_decode_partial(&raw
, &hello
, sizeof hello
), 0);
320 CHECK(raw
, OFPRAW_OFPT_HELLO
);
321 CHECK(ntohs(hello
.length
), sizeof hello
);
324 CHECK_ERRNO(retval
, -EAGAIN
);
330 int error
= vconn_connect(vconn
);
331 if (error
== expect_connect_error
) {
335 stream_close(stream
);
340 CHECK_ERRNO(error
, EAGAIN
);
344 if (read_hello
&& connected
) {
348 vconn_run_wait(vconn
);
350 vconn_connect_wait(vconn
);
353 stream_recv_wait(stream
);
357 stream_close(stream
);
358 CHECK_ERRNO(vconn_recv_block(vconn
, &msg
), EOF
);
362 /* Try connecting and sending a normal hello, which should succeed. */
364 test_send_plain_hello(struct ovs_cmdl_context
*ctx
)
366 const char *type
= ctx
->argv
[1];
367 struct ofpbuf
*hello
;
369 hello
= ofpraw_alloc_xid(OFPRAW_OFPT_HELLO
, OFP14_VERSION
,
370 htonl(0x12345678), 0);
371 test_send_hello(type
, hello
->data
, hello
->size
, 0);
372 ofpbuf_delete(hello
);
375 /* Try connecting and sending an extra-long hello, which should succeed (since
376 * the specification says that implementations must accept and ignore extra
379 test_send_long_hello(struct ovs_cmdl_context
*ctx
)
381 const char *type
= ctx
->argv
[1];
382 struct ofpbuf
*hello
;
383 enum { EXTRA_BYTES
= 8 };
385 hello
= ofpraw_alloc_xid(OFPRAW_OFPT_HELLO
, OFP14_VERSION
,
386 htonl(0x12345678), EXTRA_BYTES
);
387 ofpbuf_put_zeros(hello
, EXTRA_BYTES
);
388 ofpmsg_update_length(hello
);
389 test_send_hello(type
, hello
->data
, hello
->size
, 0);
390 ofpbuf_delete(hello
);
393 /* Try connecting and sending an echo request instead of a hello, which should
394 * fail with EPROTO. */
396 test_send_echo_hello(struct ovs_cmdl_context
*ctx
)
398 const char *type
= ctx
->argv
[1];
401 echo
= ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST
, OFP14_VERSION
,
402 htonl(0x12345678), 0);
403 test_send_hello(type
, echo
->data
, echo
->size
, EPROTO
);
407 /* Try connecting and sending a hello packet that has its length field as 0,
408 * which should fail with EPROTO. */
410 test_send_short_hello(struct ovs_cmdl_context
*ctx
)
412 const char *type
= ctx
->argv
[1];
413 struct ofp_header hello
;
415 memset(&hello
, 0, sizeof hello
);
416 test_send_hello(type
, &hello
, sizeof hello
, EPROTO
);
419 /* Try connecting and sending a hello packet that has a bad version, which
420 * should fail with EPROTO. */
422 test_send_invalid_version_hello(struct ovs_cmdl_context
*ctx
)
424 const char *type
= ctx
->argv
[1];
425 struct ofpbuf
*hello
;
427 hello
= ofpraw_alloc_xid(OFPRAW_OFPT_HELLO
, OFP14_VERSION
,
428 htonl(0x12345678), 0);
429 ((struct ofp_header
*) hello
->data
)->version
= 0;
430 test_send_hello(type
, hello
->data
, hello
->size
, EPROTO
);
431 ofpbuf_delete(hello
);
434 static const struct ovs_cmdl_command commands
[] = {
435 {"refuse-connection", NULL
, 1, 1, test_refuse_connection
, OVS_RO
},
436 {"accept-then-close", NULL
, 1, 1, test_accept_then_close
, OVS_RO
},
437 {"read-hello", NULL
, 1, 1, test_read_hello
, OVS_RO
},
438 {"send-plain-hello", NULL
, 1, 1, test_send_plain_hello
, OVS_RO
},
439 {"send-long-hello", NULL
, 1, 1, test_send_long_hello
, OVS_RO
},
440 {"send-echo-hello", NULL
, 1, 1, test_send_echo_hello
, OVS_RO
},
441 {"send-short-hello", NULL
, 1, 1, test_send_short_hello
, OVS_RO
},
442 {"send-invalid-version-hello", NULL
, 1, 1, test_send_invalid_version_hello
, OVS_RO
},
443 {NULL
, NULL
, 0, 0, NULL
, OVS_RO
},
447 test_vconn_main(int argc
, char *argv
[])
449 struct ovs_cmdl_context ctx
= {
454 set_program_name(argv
[0]);
455 vlog_set_levels(NULL
, VLF_ANY_DESTINATION
, VLL_EMER
);
456 vlog_set_levels(NULL
, VLF_CONSOLE
, VLL_DBG
);
457 fatal_ignore_sigpipe();
461 ovs_cmdl_run_command(&ctx
, commands
);
464 OVSTEST_REGISTER("test-vconn", test_vconn_main
);