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