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