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