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