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