test-vconn: Block in three cases where a race is visible on FreeBSD.
[sliver-openvswitch.git] / tests / test-vconn.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012 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 "ofp-msgs.h"
26 #include "ofp-util.h"
27 #include "ofpbuf.h"
28 #include "openflow/openflow.h"
29 #include "poll-loop.h"
30 #include "socket-util.h"
31 #include "stream.h"
32 #include "stream-ssl.h"
33 #include "timeval.h"
34 #include "util.h"
35 #include "vlog.h"
36
37 #undef NDEBUG
38 #include <assert.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 = strdup(strerror(abs(b)));
63         ovs_fatal(0, "%s:%d: %s is %d (%s) but should be %d (%s)",
64                   file, line, as, a, 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(int argc OVS_UNUSED, char *argv[])
144 {
145     const char *type = argv[1];
146     int expected_error;
147     struct fake_pvconn fpv;
148     struct vconn *vconn;
149
150     expected_error = (!strcmp(type, "unix") ? EPIPE
151                       : !strcmp(type, "tcp") ? ECONNRESET
152                       : EPROTO);
153
154     fpv_create(type, &fpv);
155     CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
156                            DSCP_DEFAULT), 0);
157     fpv_close(&fpv);
158     vconn_run(vconn);
159     CHECK_ERRNO(vconn_connect_block(vconn), expected_error);
160     vconn_close(vconn);
161     fpv_destroy(&fpv);
162 }
163
164 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
165  * closes it immediately, and verifies that vconn_connect() reports
166  * 'expected_error'. */
167 static void
168 test_accept_then_close(int argc OVS_UNUSED, char *argv[])
169 {
170     const char *type = argv[1];
171     int expected_error;
172     struct fake_pvconn fpv;
173     struct vconn *vconn;
174
175     expected_error = (!strcmp(type, "unix") ? EPIPE
176                       : !strcmp(type, "tcp") ? ECONNRESET
177                       : EPROTO);
178
179     fpv_create(type, &fpv);
180     CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
181                            DSCP_DEFAULT), 0);
182     vconn_run(vconn);
183     stream_close(fpv_accept(&fpv));
184     fpv_close(&fpv);
185     CHECK_ERRNO(vconn_connect(vconn), expected_error);
186     vconn_close(vconn);
187     fpv_destroy(&fpv);
188 }
189
190 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
191  * reads the hello message from it, then closes the connection and verifies
192  * that vconn_connect() reports 'expected_error'. */
193 static void
194 test_read_hello(int argc OVS_UNUSED, char *argv[])
195 {
196     const char *type = argv[1];
197     struct fake_pvconn fpv;
198     struct vconn *vconn;
199     struct stream *stream;
200
201     fpv_create(type, &fpv);
202     CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
203                            DSCP_DEFAULT), 0);
204     vconn_run(vconn);
205     stream = fpv_accept(&fpv);
206     fpv_destroy(&fpv);
207     for (;;) {
208        struct ofp_header hello;
209        int retval;
210
211        retval = stream_recv(stream, &hello, sizeof hello);
212        if (retval == sizeof hello) {
213            enum ofpraw raw;
214
215            CHECK(hello.version, OFP10_VERSION);
216            CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
217            CHECK(raw, OFPRAW_OFPT_HELLO);
218            CHECK(ntohs(hello.length), sizeof hello);
219            break;
220        } else {
221            CHECK_ERRNO(retval, -EAGAIN);
222        }
223
224        vconn_run(vconn);
225        CHECK_ERRNO(vconn_connect(vconn), EAGAIN);
226        vconn_run_wait(vconn);
227        vconn_connect_wait(vconn);
228        stream_recv_wait(stream);
229        poll_block();
230     }
231     stream_close(stream);
232     CHECK_ERRNO(vconn_connect_block(vconn), ECONNRESET);
233     vconn_close(vconn);
234 }
235
236 /* Connects to a fake_pvconn with vconn_open(), accepts that connection and
237  * sends the 'out' bytes in 'out_size' to it (presumably an OFPT_HELLO
238  * message), then verifies that vconn_connect() reports
239  * 'expect_connect_error'. */
240 static void
241 test_send_hello(const char *type, const void *out, size_t out_size,
242                 int expect_connect_error)
243 {
244     struct fake_pvconn fpv;
245     struct vconn *vconn;
246     bool read_hello, connected;
247     struct ofpbuf *msg;
248     struct stream *stream;
249     size_t n_sent;
250
251     fpv_create(type, &fpv);
252     CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
253                            DSCP_DEFAULT), 0);
254     vconn_run(vconn);
255     stream = fpv_accept(&fpv);
256     fpv_destroy(&fpv);
257
258     n_sent = 0;
259     while (n_sent < out_size) {
260         int retval;
261
262         retval = stream_send(stream, (char *) out + n_sent, out_size - n_sent);
263         if (retval > 0) {
264             n_sent += retval;
265         } else if (retval == -EAGAIN) {
266             stream_run(stream);
267             vconn_run(vconn);
268             stream_recv_wait(stream);
269             vconn_connect_wait(vconn);
270             vconn_run_wait(vconn);
271             poll_block();
272         } else {
273             ovs_fatal(0, "stream_send returned unexpected value %d", retval);
274         }
275     }
276
277     read_hello = connected = false;
278     for (;;) {
279        if (!read_hello) {
280            struct ofp_header hello;
281            int retval = stream_recv(stream, &hello, sizeof hello);
282            if (retval == sizeof hello) {
283                enum ofpraw raw;
284
285                CHECK(hello.version, OFP10_VERSION);
286                CHECK(ofpraw_decode_partial(&raw, &hello, sizeof hello), 0);
287                CHECK(raw, OFPRAW_OFPT_HELLO);
288                CHECK(ntohs(hello.length), sizeof hello);
289                read_hello = true;
290            } else {
291                CHECK_ERRNO(retval, -EAGAIN);
292            }
293        }
294
295        vconn_run(vconn);
296        if (!connected) {
297            int error = vconn_connect(vconn);
298            if (error == expect_connect_error) {
299                if (!error) {
300                    connected = true;
301                } else {
302                    stream_close(stream);
303                    vconn_close(vconn);
304                    return;
305                }
306            } else {
307                CHECK_ERRNO(error, EAGAIN);
308            }
309        }
310
311        if (read_hello && connected) {
312            break;
313        }
314
315        vconn_run_wait(vconn);
316        if (!connected) {
317            vconn_connect_wait(vconn);
318        }
319        if (!read_hello) {
320            stream_recv_wait(stream);
321        }
322        poll_block();
323     }
324     stream_close(stream);
325     CHECK_ERRNO(vconn_recv_block(vconn, &msg), EOF);
326     vconn_close(vconn);
327 }
328
329 /* Try connecting and sending a normal hello, which should succeed. */
330 static void
331 test_send_plain_hello(int argc OVS_UNUSED, char *argv[])
332 {
333     const char *type = argv[1];
334     struct ofpbuf *hello;
335
336     hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
337                              htonl(0x12345678), 0);
338     test_send_hello(type, hello->data, hello->size, 0);
339     ofpbuf_delete(hello);
340 }
341
342 /* Try connecting and sending an extra-long hello, which should succeed (since
343  * the specification says that implementations must accept and ignore extra
344  * data). */
345 static void
346 test_send_long_hello(int argc OVS_UNUSED, char *argv[])
347 {
348     const char *type = argv[1];
349     struct ofpbuf *hello;
350     enum { EXTRA_BYTES = 8 };
351
352     hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
353                              htonl(0x12345678), EXTRA_BYTES);
354     ofpbuf_put_zeros(hello, EXTRA_BYTES);
355     ofpmsg_update_length(hello);
356     test_send_hello(type, hello->data, hello->size, 0);
357     ofpbuf_delete(hello);
358 }
359
360 /* Try connecting and sending an echo request instead of a hello, which should
361  * fail with EPROTO. */
362 static void
363 test_send_echo_hello(int argc OVS_UNUSED, char *argv[])
364 {
365     const char *type = argv[1];
366     struct ofpbuf *echo;
367
368     echo = ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
369                              htonl(0x12345678), 0);
370     test_send_hello(type, echo->data, echo->size, EPROTO);
371     ofpbuf_delete(echo);
372 }
373
374 /* Try connecting and sending a hello packet that has its length field as 0,
375  * which should fail with EPROTO. */
376 static void
377 test_send_short_hello(int argc OVS_UNUSED, char *argv[])
378 {
379     const char *type = argv[1];
380     struct ofp_header hello;
381
382     memset(&hello, 0, sizeof hello);
383     test_send_hello(type, &hello, sizeof hello, EPROTO);
384 }
385
386 /* Try connecting and sending a hello packet that has a bad version, which
387  * should fail with EPROTO. */
388 static void
389 test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[])
390 {
391     const char *type = argv[1];
392     struct ofpbuf *hello;
393
394     hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP10_VERSION,
395                              htonl(0x12345678), 0);
396     ((struct ofp_header *) hello->data)->version = 0;
397     test_send_hello(type, hello->data, hello->size, EPROTO);
398     ofpbuf_delete(hello);
399 }
400
401 static const struct command commands[] = {
402     {"refuse-connection", 1, 1, test_refuse_connection},
403     {"accept-then-close", 1, 1, test_accept_then_close},
404     {"read-hello", 1, 1, test_read_hello},
405     {"send-plain-hello", 1, 1, test_send_plain_hello},
406     {"send-long-hello", 1, 1, test_send_long_hello},
407     {"send-echo-hello", 1, 1, test_send_echo_hello},
408     {"send-short-hello", 1, 1, test_send_short_hello},
409     {"send-invalid-version-hello", 1, 1, test_send_invalid_version_hello},
410     {NULL, 0, 0, NULL},
411 };
412
413 int
414 main(int argc, char *argv[])
415 {
416     set_program_name(argv[0]);
417     vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
418     vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
419     signal(SIGPIPE, SIG_IGN);
420
421     time_alarm(10);
422
423     run_command(argc - 1, argv + 1, commands);
424
425     return 0;
426 }