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