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