]> git.proxmox.com Git - ovs.git/blob - tests/test-vconn.c
netlink linux: enable listening to all nsids
[ovs.git] / tests / test-vconn.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2017 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #undef NDEBUG
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include "command-line.h"
26 #include "fatal-signal.h"
27 #include "openflow/openflow.h"
28 #include "openvswitch/ofp-msgs.h"
29 #include "openvswitch/ofpbuf.h"
30 #include "openvswitch/vconn.h"
31 #include "openvswitch/vlog.h"
32 #include "ovstest.h"
33 #include "openvswitch/poll-loop.h"
34 #include "socket-util.h"
35 #include "stream.h"
36 #include "stream-ssl.h"
37 #include "timeval.h"
38 #include "util.h"
39
40 struct fake_pvconn {
41 const char *type;
42 char *pvconn_name;
43 char *vconn_name;
44 struct pstream *pstream;
45 };
46
47 static void
48 check(int a, int b, const char *as, const char *file, int line)
49 {
50 if (a != b) {
51 ovs_fatal(0, "%s:%d: %s is %d but should be %d", file, line, as, a, b);
52 }
53 }
54
55
56 #define CHECK(A, B) check(A, B, #A, __FILE__, __LINE__)
57
58 static void
59 check_errno(int a, int b, const char *as, const char *file, int line)
60 {
61 if (a != b) {
62 char *str_b = xstrdup(ovs_strerror(abs(b)));
63 ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
64 file, line, as, a, ovs_strerror(abs(a)), b, str_b);
65 }
66 }
67
68 #define CHECK_ERRNO(A, B) check_errno(A, B, #A, __FILE__, __LINE__)
69
70 static void
71 fpv_create(const char *type, struct fake_pvconn *fpv)
72 {
73 #ifdef HAVE_OPENSSL
74 if (!strcmp(type, "ssl")) {
75 stream_ssl_set_private_key_file("testpki-privkey.pem");
76 stream_ssl_set_certificate_file("testpki-cert.pem");
77 stream_ssl_set_ca_cert_file("testpki-cacert.pem", false);
78 }
79 #endif
80
81 fpv->type = type;
82 if (!strcmp(type, "unix")) {
83 static int unix_count = 0;
84 char *bind_path;
85
86 bind_path = xasprintf("fake-pvconn.%d", unix_count++);
87 fpv->pvconn_name = xasprintf("punix:%s", bind_path);
88 fpv->vconn_name = xasprintf("unix:%s", bind_path);
89 CHECK_ERRNO(pstream_open(fpv->pvconn_name, &fpv->pstream,
90 DSCP_DEFAULT), 0);
91 free(bind_path);
92 } else if (!strcmp(type, "tcp") || !strcmp(type, "ssl")) {
93 char *s, *port, *save_ptr = NULL;
94 char *open_name;
95
96 open_name = xasprintf("p%s:0:127.0.0.1", type);
97 CHECK_ERRNO(pstream_open(open_name, &fpv->pstream, DSCP_DEFAULT), 0);
98
99 /* Extract bound port number from pstream name. */
100 s = xstrdup(pstream_get_name(fpv->pstream));
101 strtok_r(s, ":", &save_ptr);
102 port = strtok_r(NULL, ":", &save_ptr);
103
104 /* Save info. */
105 fpv->pvconn_name = xstrdup(pstream_get_name(fpv->pstream));
106 fpv->vconn_name = xasprintf("%s:127.0.0.1:%s", type, port);
107
108 free(open_name);
109 free(s);
110 } else {
111 abort();
112 }
113 }
114
115 static struct stream *
116 fpv_accept(struct fake_pvconn *fpv)
117 {
118 struct stream *stream;
119
120 CHECK_ERRNO(pstream_accept_block(fpv->pstream, &stream), 0);
121
122 return stream;
123 }
124
125 static void
126 fpv_close(struct fake_pvconn *fpv)
127 {
128 pstream_close(fpv->pstream);
129 fpv->pstream = NULL;
130 }
131
132 static void
133 fpv_destroy(struct fake_pvconn *fpv)
134 {
135 fpv_close(fpv);
136 free(fpv->pvconn_name);
137 free(fpv->vconn_name);
138 }
139
140 /* Connects to a fake_pvconn with vconn_open(), then closes the listener and
141 * verifies that vconn_connect() reports 'expected_error'. */
142 static void
143 test_refuse_connection(struct ovs_cmdl_context *ctx)
144 {
145 const char *type = ctx->argv[1];
146 struct fake_pvconn fpv;
147 struct vconn *vconn;
148 int error;
149
150 fpv_create(type, &fpv);
151 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
152 fpv_close(&fpv);
153 vconn_run(vconn);
154
155 error = vconn_connect_block(vconn);
156 if (!strcmp(type, "tcp")) {
157 if (error != ECONNRESET && error != EPIPE
158 #ifdef _WIN32
159 && error != WSAECONNRESET
160 #endif
161 ) {
162 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
163 error, ovs_strerror(error));
164 }
165 } else if (!strcmp(type, "unix")) {
166 #ifndef _WIN32
167 CHECK_ERRNO(error, EPIPE);
168 #else
169 CHECK_ERRNO(error, WSAECONNRESET);
170 #endif
171 } else if (!strcmp(type, "ssl")) {
172 if (error != EPROTO && error != ECONNRESET) {
173 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
174 error, ovs_strerror(error));
175 }
176 } else {
177 ovs_fatal(0, "invalid connection type %s", type);
178 }
179
180 vconn_close(vconn);
181 fpv_destroy(&fpv);
182 }
183
184 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
185 * closes it immediately, and verifies that vconn_connect() reports
186 * 'expected_error'. */
187 static void
188 test_accept_then_close(struct ovs_cmdl_context *ctx)
189 {
190 const char *type = ctx->argv[1];
191 struct fake_pvconn fpv;
192 struct vconn *vconn;
193 int error;
194
195 fpv_create(type, &fpv);
196 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
197 vconn_run(vconn);
198 stream_close(fpv_accept(&fpv));
199 fpv_close(&fpv);
200
201 error = vconn_connect_block(vconn);
202 if (!strcmp(type, "tcp") || !strcmp(type, "unix")) {
203 if (error != ECONNRESET && error != EPIPE
204 #ifdef _WIN32
205 && error != WSAECONNRESET
206 #endif
207 ) {
208 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
209 error, ovs_strerror(error));
210 }
211 } else {
212 CHECK_ERRNO(error, EPROTO);
213 }
214
215 vconn_close(vconn);
216 fpv_destroy(&fpv);
217 }
218
219 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
220 * reads the hello message from it, then closes the connection and verifies
221 * that vconn_connect() reports 'expected_error'. */
222 static void
223 test_read_hello(struct ovs_cmdl_context *ctx)
224 {
225 const char *type = ctx->argv[1];
226 struct fake_pvconn fpv;
227 struct vconn *vconn;
228 struct stream *stream;
229 int error;
230
231 fpv_create(type, &fpv);
232 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
233 vconn_run(vconn);
234 stream = fpv_accept(&fpv);
235 fpv_destroy(&fpv);
236 for (;;) {
237 struct ofp_header hello;
238 int retval;
239
240 retval = stream_recv(stream, &hello, sizeof hello);
241 if (retval == sizeof hello) {
242 enum ofpraw raw;
243
244 CHECK(hello.version, OFP14_VERSION);
245 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
246 CHECK(raw, OFPRAW_OFPT_HELLO);
247 CHECK(ntohs(hello.length), sizeof hello);
248 break;
249 } else {
250 CHECK_ERRNO(retval, -EAGAIN);
251 }
252
253 vconn_run(vconn);
254 CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
255 vconn_run_wait(vconn);
256 vconn_connect_wait(vconn);
257 stream_recv_wait(stream);
258 poll_block();
259 }
260 stream_close(stream);
261 error = vconn_connect_block(vconn);
262 if (error != ECONNRESET && error != EPIPE) {
263 ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
264 error, ovs_strerror(error));
265 }
266 vconn_close(vconn);
267 }
268
269 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
270 * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
271 * message), then verifies that vconn_connect() reports
272 * 'expect_connect_error'. */
273 static void
274 test_send_hello(const char *type, const void *out, size_t out_size,
275 int expect_connect_error)
276 {
277 struct fake_pvconn fpv;
278 struct vconn *vconn;
279 bool read_hello, connected;
280 struct ofpbuf *msg;
281 struct stream *stream;
282 size_t n_sent;
283
284 fpv_create(type, &fpv);
285 CHECK_ERRNO(vconn_open(fpv.vconn_name, 0, DSCP_DEFAULT, &vconn), 0);
286 vconn_run(vconn);
287 stream = fpv_accept(&fpv);
288 fpv_destroy(&fpv);
289
290 n_sent = 0;
291 while (n_sent < out_size) {
292 int retval;
293
294 retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
295 if (retval > 0) {
296 n_sent += retval;
297 } else if (retval == -EAGAIN) {
298 stream_run(stream);
299 vconn_run(vconn);
300 stream_recv_wait(stream);
301 vconn_connect_wait(vconn);
302 vconn_run_wait(vconn);
303 poll_block();
304 } else {
305 ovs_fatal(0, "stream_send returned unexpected value %d", retval);
306 }
307 }
308
309 read_hello = connected = false;
310 for (;;) {
311 if (!read_hello) {
312 struct ofp_header hello;
313 int retval = stream_recv(stream, &hello, sizeof hello);
314 if (retval == sizeof hello) {
315 enum ofpraw raw;
316
317 CHECK(hello.version, OFP14_VERSION);
318 CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
319 CHECK(raw, OFPRAW_OFPT_HELLO);
320 CHECK(ntohs(hello.length), sizeof hello);
321 read_hello = true;
322 } else {
323 CHECK_ERRNO(retval, -EAGAIN);
324 }
325 }
326
327 vconn_run(vconn);
328 if (!connected) {
329 int error = vconn_connect(vconn);
330 if (error == expect_connect_error) {
331 if (!error) {
332 connected = true;
333 } else {
334 stream_close(stream);
335 vconn_close(vconn);
336 return;
337 }
338 } else {
339 CHECK_ERRNO(error, EAGAIN);
340 }
341 }
342
343 if (read_hello && connected) {
344 break;
345 }
346
347 vconn_run_wait(vconn);
348 if (!connected) {
349 vconn_connect_wait(vconn);
350 }
351 if (!read_hello) {
352 stream_recv_wait(stream);
353 }
354 poll_block();
355 }
356 stream_close(stream);
357 CHECK_ERRNO(vconn_recv_block(vconn, &msg), EOF);
358 vconn_close(vconn);
359 }
360
361 /* Try connecting and sending a normal hello, which should succeed. */
362 static void
363 test_send_plain_hello(struct ovs_cmdl_context *ctx)
364 {
365 const char *type = ctx->argv[1];
366 struct ofpbuf *hello;
367
368 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP14_VERSION,
369 htonl(0x12345678), 0);
370 test_send_hello(type, hello->data, hello->size, 0);
371 ofpbuf_delete(hello);
372 }
373
374 /* Try connecting and sending an extra-long hello, which should succeed (since
375 * the specification says that implementations must accept and ignore extra
376 * data). */
377 static void
378 test_send_long_hello(struct ovs_cmdl_context *ctx)
379 {
380 const char *type = ctx->argv[1];
381 struct ofpbuf *hello;
382 enum { EXTRA_BYTES = 8 };
383
384 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP14_VERSION,
385 htonl(0x12345678), EXTRA_BYTES);
386 ofpbuf_put_zeros(hello, EXTRA_BYTES);
387 ofpmsg_update_length(hello);
388 test_send_hello(type, hello->data, hello->size, 0);
389 ofpbuf_delete(hello);
390 }
391
392 /* Try connecting and sending an echo request instead of a hello, which should
393 * fail with EPROTO. */
394 static void
395 test_send_echo_hello(struct ovs_cmdl_context *ctx)
396 {
397 const char *type = ctx->argv[1];
398 struct ofpbuf *echo;
399
400 echo = ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, OFP14_VERSION,
401 htonl(0x12345678), 0);
402 test_send_hello(type, echo->data, echo->size, EPROTO);
403 ofpbuf_delete(echo);
404 }
405
406 /* Try connecting and sending a hello packet that has its length field as 0,
407 * which should fail with EPROTO. */
408 static void
409 test_send_short_hello(struct ovs_cmdl_context *ctx)
410 {
411 const char *type = ctx->argv[1];
412 struct ofp_header hello;
413
414 memset(&hello, 0, sizeof hello);
415 test_send_hello(type, &hello, sizeof hello, EPROTO);
416 }
417
418 /* Try connecting and sending a hello packet that has a bad version, which
419 * should fail with EPROTO. */
420 static void
421 test_send_invalid_version_hello(struct ovs_cmdl_context *ctx)
422 {
423 const char *type = ctx->argv[1];
424 struct ofpbuf *hello;
425
426 hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP14_VERSION,
427 htonl(0x12345678), 0);
428 ((struct ofp_header *) hello->data)->version = 0;
429 test_send_hello(type, hello->data, hello->size, EPROTO);
430 ofpbuf_delete(hello);
431 }
432
433 static const struct ovs_cmdl_command commands[] = {
434 {"refuse-connection", NULL, 1, 1, test_refuse_connection, OVS_RO},
435 {"accept-then-close", NULL, 1, 1, test_accept_then_close, OVS_RO},
436 {"read-hello", NULL, 1, 1, test_read_hello, OVS_RO},
437 {"send-plain-hello", NULL, 1, 1, test_send_plain_hello, OVS_RO},
438 {"send-long-hello", NULL, 1, 1, test_send_long_hello, OVS_RO},
439 {"send-echo-hello", NULL, 1, 1, test_send_echo_hello, OVS_RO},
440 {"send-short-hello", NULL, 1, 1, test_send_short_hello, OVS_RO},
441 {"send-invalid-version-hello", NULL, 1, 1, test_send_invalid_version_hello, OVS_RO},
442 {NULL, NULL, 0, 0, NULL, OVS_RO},
443 };
444
445 static void
446 test_vconn_main(int argc, char *argv[])
447 {
448 struct ovs_cmdl_context ctx = {
449 .argc = argc - 1,
450 .argv = argv + 1,
451 };
452
453 set_program_name(argv[0]);
454 vlog_set_levels(NULL, VLF_ANY_DESTINATION, VLL_EMER);
455 vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
456 fatal_ignore_sigpipe();
457
458 time_alarm(10);
459
460 ovs_cmdl_run_command(&ctx, commands);
461 }
462
463 OVSTEST_REGISTER("test-vconn", test_vconn_main);