linux-user: Support for restarting system calls for OpenRISC targets
[qemu.git] / tests / test-filter-redirector.c
1 /*
2 * QTest testcase for filter-redirector
3 *
4 * Copyright (c) 2016 FUJITSU LIMITED
5 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or
8 * later. See the COPYING file in the top-level directory.
9 *
10 * Case 1, tx traffic flow:
11 *
12 * qemu side | test side
13 * |
14 * +---------+ | +-------+
15 * | backend <---------------+ sock0 |
16 * +----+----+ | +-------+
17 * | |
18 * +----v----+ +-------+ |
19 * | rd0 +->+chardev| |
20 * +---------+ +---+---+ |
21 * | |
22 * +---------+ | |
23 * | rd1 <------+ |
24 * +----+----+ |
25 * | |
26 * +----v----+ | +-------+
27 * | rd2 +--------------->sock1 |
28 * +---------+ | +-------+
29 * +
30 *
31 * --------------------------------------
32 * Case 2, rx traffic flow
33 * qemu side | test side
34 * |
35 * +---------+ | +-------+
36 * | backend +---------------> sock1 |
37 * +----^----+ | +-------+
38 * | |
39 * +----+----+ +-------+ |
40 * | rd0 +<-+chardev| |
41 * +---------+ +---+---+ |
42 * ^ |
43 * +---------+ | |
44 * | rd1 +------+ |
45 * +----^----+ |
46 * | |
47 * +----+----+ | +-------+
48 * | rd2 <---------------+sock0 |
49 * +---------+ | +-------+
50 * +
51 */
52
53 #include "qemu/osdep.h"
54 #include <glib.h>
55 #include "libqtest.h"
56 #include "qemu/iov.h"
57 #include "qemu/sockets.h"
58 #include "qemu/error-report.h"
59 #include "qemu/main-loop.h"
60
61 static void test_redirector_tx(void)
62 {
63 #ifndef _WIN32
64 /* socketpair(PF_UNIX) which does not exist on windows */
65
66 int backend_sock[2], recv_sock;
67 char *cmdline;
68 uint32_t ret = 0, len = 0;
69 char send_buf[] = "Hello!!";
70 char sock_path0[] = "filter-redirector0.XXXXXX";
71 char sock_path1[] = "filter-redirector1.XXXXXX";
72 char *recv_buf;
73 uint32_t size = sizeof(send_buf);
74 size = htonl(size);
75
76 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, backend_sock);
77 g_assert_cmpint(ret, !=, -1);
78
79 ret = mkstemp(sock_path0);
80 g_assert_cmpint(ret, !=, -1);
81 ret = mkstemp(sock_path1);
82 g_assert_cmpint(ret, !=, -1);
83
84 cmdline = g_strdup_printf("-netdev socket,id=qtest-bn0,fd=%d "
85 "-device rtl8139,netdev=qtest-bn0,id=qtest-e0 "
86 "-chardev socket,id=redirector0,path=%s,server,nowait "
87 "-chardev socket,id=redirector1,path=%s,server,nowait "
88 "-chardev socket,id=redirector2,path=%s,nowait "
89 "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
90 "queue=tx,outdev=redirector0 "
91 "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"
92 "queue=tx,indev=redirector2 "
93 "-object filter-redirector,id=qtest-f2,netdev=qtest-bn0,"
94 "queue=tx,outdev=redirector1 "
95 , backend_sock[1], sock_path0, sock_path1, sock_path0);
96 qtest_start(cmdline);
97 g_free(cmdline);
98
99 recv_sock = unix_connect(sock_path1, NULL);
100 g_assert_cmpint(recv_sock, !=, -1);
101
102 /* send a qmp command to guarantee that 'connected' is setting to true. */
103 qmp("{ 'execute' : 'query-status'}");
104
105 struct iovec iov[] = {
106 {
107 .iov_base = &size,
108 .iov_len = sizeof(size),
109 }, {
110 .iov_base = send_buf,
111 .iov_len = sizeof(send_buf),
112 },
113 };
114
115 ret = iov_send(backend_sock[0], iov, 2, 0, sizeof(size) + sizeof(send_buf));
116 g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
117 close(backend_sock[0]);
118
119 ret = qemu_recv(recv_sock, &len, sizeof(len), 0);
120 g_assert_cmpint(ret, ==, sizeof(len));
121 len = ntohl(len);
122
123 g_assert_cmpint(len, ==, sizeof(send_buf));
124 recv_buf = g_malloc(len);
125 ret = qemu_recv(recv_sock, recv_buf, len, 0);
126 g_assert_cmpstr(recv_buf, ==, send_buf);
127
128 g_free(recv_buf);
129 close(recv_sock);
130 unlink(sock_path0);
131 unlink(sock_path1);
132 qtest_end();
133
134 #endif
135 }
136
137 static void test_redirector_rx(void)
138 {
139 #ifndef _WIN32
140 /* socketpair(PF_UNIX) which does not exist on windows */
141
142 int backend_sock[2], send_sock;
143 char *cmdline;
144 uint32_t ret = 0, len = 0;
145 char send_buf[] = "Hello!!";
146 char sock_path0[] = "filter-redirector0.XXXXXX";
147 char sock_path1[] = "filter-redirector1.XXXXXX";
148 char *recv_buf;
149 uint32_t size = sizeof(send_buf);
150 size = htonl(size);
151
152 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, backend_sock);
153 g_assert_cmpint(ret, !=, -1);
154
155 ret = mkstemp(sock_path0);
156 g_assert_cmpint(ret, !=, -1);
157 ret = mkstemp(sock_path1);
158 g_assert_cmpint(ret, !=, -1);
159
160 cmdline = g_strdup_printf("-netdev socket,id=qtest-bn0,fd=%d "
161 "-device rtl8139,netdev=qtest-bn0,id=qtest-e0 "
162 "-chardev socket,id=redirector0,path=%s,server,nowait "
163 "-chardev socket,id=redirector1,path=%s,server,nowait "
164 "-chardev socket,id=redirector2,path=%s,nowait "
165 "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0,"
166 "queue=rx,indev=redirector0 "
167 "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"
168 "queue=rx,outdev=redirector2 "
169 "-object filter-redirector,id=qtest-f2,netdev=qtest-bn0,"
170 "queue=rx,indev=redirector1 "
171 , backend_sock[1], sock_path0, sock_path1, sock_path0);
172 qtest_start(cmdline);
173 g_free(cmdline);
174
175 struct iovec iov[] = {
176 {
177 .iov_base = &size,
178 .iov_len = sizeof(size),
179 }, {
180 .iov_base = send_buf,
181 .iov_len = sizeof(send_buf),
182 },
183 };
184
185 send_sock = unix_connect(sock_path1, NULL);
186 g_assert_cmpint(send_sock, !=, -1);
187 /* send a qmp command to guarantee that 'connected' is setting to true. */
188 qmp("{ 'execute' : 'query-status'}");
189
190 ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
191 g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
192 close(send_sock);
193
194 ret = qemu_recv(backend_sock[0], &len, sizeof(len), 0);
195 g_assert_cmpint(ret, ==, sizeof(len));
196 len = ntohl(len);
197
198 g_assert_cmpint(len, ==, sizeof(send_buf));
199 recv_buf = g_malloc(len);
200 ret = qemu_recv(backend_sock[0], recv_buf, len, 0);
201 g_assert_cmpstr(recv_buf, ==, send_buf);
202
203 g_free(recv_buf);
204 unlink(sock_path0);
205 unlink(sock_path1);
206 qtest_end();
207
208 #endif
209 }
210
211 int main(int argc, char **argv)
212 {
213 int ret;
214
215 g_test_init(&argc, &argv, NULL);
216 qtest_add_func("/netfilter/redirector_tx", test_redirector_tx);
217 qtest_add_func("/netfilter/redirector_rx", test_redirector_rx);
218 ret = g_test_run();
219
220 return ret;
221 }