]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-filter-mirror.c
tests/libqos/e1000e: Make e1000e libqos functions independent from global_qtest
[mirror_qemu.git] / tests / test-filter-mirror.c
CommitLineData
06809ecf
ZC
1/*
2 * QTest testcase for filter-mirror
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
11#include "qemu/osdep.h"
a8d25326 12#include "qemu-common.h"
06809ecf 13#include "libqtest.h"
055a1efc 14#include "qapi/qmp/qdict.h"
06809ecf
ZC
15#include "qemu/iov.h"
16#include "qemu/sockets.h"
17#include "qemu/error-report.h"
18#include "qemu/main-loop.h"
055a1efc
MA
19
20/* TODO actually test the results and get rid of this */
a2569b00 21#define qmp_discard_response(qs, ...) qobject_unref(qtest_qmp(qs, __VA_ARGS__))
06809ecf
ZC
22
23static void test_mirror(void)
24{
773c4a62 25 int send_sock[2], recv_sock[2];
06809ecf
ZC
26 uint32_t ret = 0, len = 0;
27 char send_buf[] = "Hello! filter-mirror~";
06809ecf
ZC
28 char *recv_buf;
29 uint32_t size = sizeof(send_buf);
30 size = htonl(size);
ea5bef49 31 const char *devstr = "e1000";
a2569b00 32 QTestState *qts;
ea5bef49
TH
33
34 if (g_str_equal(qtest_get_arch(), "s390x")) {
35 devstr = "virtio-net-ccw";
36 }
06809ecf
ZC
37
38 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, send_sock);
39 g_assert_cmpint(ret, !=, -1);
40
773c4a62 41 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, recv_sock);
06809ecf
ZC
42 g_assert_cmpint(ret, !=, -1);
43
a2569b00 44 qts = qtest_initf(
78b27bad
EB
45 "-netdev socket,id=qtest-bn0,fd=%d "
46 "-device %s,netdev=qtest-bn0,id=qtest-e0 "
773c4a62 47 "-chardev socket,id=mirror0,fd=%d "
78b27bad 48 "-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,outdev=mirror0 "
773c4a62 49 , send_sock[1], devstr, recv_sock[1]);
06809ecf
ZC
50
51 struct iovec iov[] = {
52 {
53 .iov_base = &size,
54 .iov_len = sizeof(size),
55 }, {
56 .iov_base = send_buf,
57 .iov_len = sizeof(send_buf),
58 },
59 };
60
61 /* send a qmp command to guarantee that 'connected' is setting to true. */
a2569b00 62 qmp_discard_response(qts, "{ 'execute' : 'query-status'}");
06809ecf
ZC
63 ret = iov_send(send_sock[0], iov, 2, 0, sizeof(size) + sizeof(send_buf));
64 g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
65 close(send_sock[0]);
66
773c4a62 67 ret = qemu_recv(recv_sock[0], &len, sizeof(len), 0);
06809ecf
ZC
68 g_assert_cmpint(ret, ==, sizeof(len));
69 len = ntohl(len);
70
71 g_assert_cmpint(len, ==, sizeof(send_buf));
72 recv_buf = g_malloc(len);
773c4a62 73 ret = qemu_recv(recv_sock[0], recv_buf, len, 0);
06809ecf
ZC
74 g_assert_cmpstr(recv_buf, ==, send_buf);
75
76 g_free(recv_buf);
773c4a62
JW
77 close(send_sock[0]);
78 close(send_sock[1]);
79 close(recv_sock[0]);
80 close(recv_sock[1]);
a2569b00 81 qtest_quit(qts);
06809ecf
ZC
82}
83
84int main(int argc, char **argv)
85{
86 int ret;
87
88 g_test_init(&argc, &argv, NULL);
89
90 qtest_add_func("/netfilter/mirror", test_mirror);
91 ret = g_test_run();
06809ecf
ZC
92
93 return ret;
94}