Merge remote-tracking branch 'remotes/dgilbert/tags/pull-migration-20170927a' into...
[qemu.git] / tests / test-netfilter.c
blob250647336518218aaa0eb92c389809fc6b6e70d6
1 /*
2 * QTest testcase for netfilter
4 * Copyright (c) 2015 FUJITSU LIMITED
5 * Author: Yang Hongyang <yanghy@cn.fujitsu.com>
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 */
11 #include "qemu/osdep.h"
12 #include "libqtest.h"
14 /* add a netfilter to a netdev and then remove it */
15 static void add_one_netfilter(void)
17 QDict *response;
19 response = qmp("{'execute': 'object-add',"
20 " 'arguments': {"
21 " 'qom-type': 'filter-buffer',"
22 " 'id': 'qtest-f0',"
23 " 'props': {"
24 " 'netdev': 'qtest-bn0',"
25 " 'queue': 'rx',"
26 " 'interval': 1000"
27 "}}}");
29 g_assert(response);
30 g_assert(!qdict_haskey(response, "error"));
31 QDECREF(response);
33 response = qmp("{'execute': 'object-del',"
34 " 'arguments': {"
35 " 'id': 'qtest-f0'"
36 "}}");
37 g_assert(response);
38 g_assert(!qdict_haskey(response, "error"));
39 QDECREF(response);
42 /* add a netfilter to a netdev and then remove the netdev */
43 static void remove_netdev_with_one_netfilter(void)
45 QDict *response;
47 response = qmp("{'execute': 'object-add',"
48 " 'arguments': {"
49 " 'qom-type': 'filter-buffer',"
50 " 'id': 'qtest-f0',"
51 " 'props': {"
52 " 'netdev': 'qtest-bn0',"
53 " 'queue': 'rx',"
54 " 'interval': 1000"
55 "}}}");
57 g_assert(response);
58 g_assert(!qdict_haskey(response, "error"));
59 QDECREF(response);
61 response = qmp("{'execute': 'netdev_del',"
62 " 'arguments': {"
63 " 'id': 'qtest-bn0'"
64 "}}");
65 g_assert(response);
66 g_assert(!qdict_haskey(response, "error"));
67 QDECREF(response);
69 /* add back the netdev */
70 response = qmp("{'execute': 'netdev_add',"
71 " 'arguments': {"
72 " 'type': 'user',"
73 " 'id': 'qtest-bn0'"
74 "}}");
75 g_assert(response);
76 g_assert(!qdict_haskey(response, "error"));
77 QDECREF(response);
80 /* add multi(2) netfilters to a netdev and then remove them */
81 static void add_multi_netfilter(void)
83 QDict *response;
85 response = qmp("{'execute': 'object-add',"
86 " 'arguments': {"
87 " 'qom-type': 'filter-buffer',"
88 " 'id': 'qtest-f0',"
89 " 'props': {"
90 " 'netdev': 'qtest-bn0',"
91 " 'queue': 'rx',"
92 " 'interval': 1000"
93 "}}}");
95 g_assert(response);
96 g_assert(!qdict_haskey(response, "error"));
97 QDECREF(response);
99 response = qmp("{'execute': 'object-add',"
100 " 'arguments': {"
101 " 'qom-type': 'filter-buffer',"
102 " 'id': 'qtest-f1',"
103 " 'props': {"
104 " 'netdev': 'qtest-bn0',"
105 " 'queue': 'rx',"
106 " 'interval': 1000"
107 "}}}");
109 g_assert(response);
110 g_assert(!qdict_haskey(response, "error"));
111 QDECREF(response);
113 response = qmp("{'execute': 'object-del',"
114 " 'arguments': {"
115 " 'id': 'qtest-f0'"
116 "}}");
117 g_assert(response);
118 g_assert(!qdict_haskey(response, "error"));
119 QDECREF(response);
121 response = qmp("{'execute': 'object-del',"
122 " 'arguments': {"
123 " 'id': 'qtest-f1'"
124 "}}");
125 g_assert(response);
126 g_assert(!qdict_haskey(response, "error"));
127 QDECREF(response);
130 /* add multi(2) netfilters to a netdev and then remove the netdev */
131 static void remove_netdev_with_multi_netfilter(void)
133 QDict *response;
135 response = qmp("{'execute': 'object-add',"
136 " 'arguments': {"
137 " 'qom-type': 'filter-buffer',"
138 " 'id': 'qtest-f0',"
139 " 'props': {"
140 " 'netdev': 'qtest-bn0',"
141 " 'queue': 'rx',"
142 " 'interval': 1000"
143 "}}}");
145 g_assert(response);
146 g_assert(!qdict_haskey(response, "error"));
147 QDECREF(response);
149 response = qmp("{'execute': 'object-add',"
150 " 'arguments': {"
151 " 'qom-type': 'filter-buffer',"
152 " 'id': 'qtest-f1',"
153 " 'props': {"
154 " 'netdev': 'qtest-bn0',"
155 " 'queue': 'rx',"
156 " 'interval': 1000"
157 "}}}");
159 g_assert(response);
160 g_assert(!qdict_haskey(response, "error"));
161 QDECREF(response);
163 response = qmp("{'execute': 'netdev_del',"
164 " 'arguments': {"
165 " 'id': 'qtest-bn0'"
166 "}}");
167 g_assert(response);
168 g_assert(!qdict_haskey(response, "error"));
169 QDECREF(response);
171 /* add back the netdev */
172 response = qmp("{'execute': 'netdev_add',"
173 " 'arguments': {"
174 " 'type': 'user',"
175 " 'id': 'qtest-bn0'"
176 "}}");
177 g_assert(response);
178 g_assert(!qdict_haskey(response, "error"));
179 QDECREF(response);
182 int main(int argc, char **argv)
184 int ret;
185 char *args;
186 const char *devstr = "e1000";
188 if (g_str_equal(qtest_get_arch(), "s390x")) {
189 devstr = "virtio-net-ccw";
192 g_test_init(&argc, &argv, NULL);
193 qtest_add_func("/netfilter/addremove_one", add_one_netfilter);
194 qtest_add_func("/netfilter/remove_netdev_one",
195 remove_netdev_with_one_netfilter);
196 qtest_add_func("/netfilter/addremove_multi", add_multi_netfilter);
197 qtest_add_func("/netfilter/remove_netdev_multi",
198 remove_netdev_with_multi_netfilter);
200 args = g_strdup_printf("-netdev user,id=qtest-bn0 "
201 "-device %s,netdev=qtest-bn0", devstr);
202 qtest_start(args);
203 ret = g_test_run();
205 qtest_end();
206 g_free(args);
208 return ret;