Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / tests / qtest / test-netfilter.c
blob785b6f3226c6e3ebe9b321b3c8b1a3b94fef8ea6
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-single.h"
13 #include "qapi/qmp/qdict.h"
15 /* add a netfilter to a netdev and then remove it */
16 static void add_one_netfilter(void)
18 QDict *response;
20 response = qmp("{'execute': 'object-add',"
21 " 'arguments': {"
22 " 'qom-type': 'filter-buffer',"
23 " 'id': 'qtest-f0',"
24 " 'netdev': 'qtest-bn0',"
25 " 'queue': 'rx',"
26 " 'interval': 1000"
27 "}}");
29 g_assert(response);
30 g_assert(!qdict_haskey(response, "error"));
31 qobject_unref(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 qobject_unref(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 " 'netdev': 'qtest-bn0',"
52 " 'queue': 'rx',"
53 " 'interval': 1000"
54 "}}");
56 g_assert(response);
57 g_assert(!qdict_haskey(response, "error"));
58 qobject_unref(response);
60 response = qmp("{'execute': 'netdev_del',"
61 " 'arguments': {"
62 " 'id': 'qtest-bn0'"
63 "}}");
64 g_assert(response);
65 g_assert(!qdict_haskey(response, "error"));
66 qobject_unref(response);
68 /* add back the netdev */
69 response = qmp("{'execute': 'netdev_add',"
70 " 'arguments': {"
71 " 'type': 'user',"
72 " 'id': 'qtest-bn0'"
73 "}}");
74 g_assert(response);
75 g_assert(!qdict_haskey(response, "error"));
76 qobject_unref(response);
79 /* add multi(2) netfilters to a netdev and then remove them */
80 static void add_multi_netfilter(void)
82 QDict *response;
84 response = qmp("{'execute': 'object-add',"
85 " 'arguments': {"
86 " 'qom-type': 'filter-buffer',"
87 " 'id': 'qtest-f0',"
88 " 'netdev': 'qtest-bn0',"
89 " 'queue': 'rx',"
90 " 'interval': 1000"
91 "}}");
93 g_assert(response);
94 g_assert(!qdict_haskey(response, "error"));
95 qobject_unref(response);
97 response = qmp("{'execute': 'object-add',"
98 " 'arguments': {"
99 " 'qom-type': 'filter-buffer',"
100 " 'id': 'qtest-f1',"
101 " 'netdev': 'qtest-bn0',"
102 " 'queue': 'rx',"
103 " 'interval': 1000"
104 "}}");
106 g_assert(response);
107 g_assert(!qdict_haskey(response, "error"));
108 qobject_unref(response);
110 response = qmp("{'execute': 'object-del',"
111 " 'arguments': {"
112 " 'id': 'qtest-f0'"
113 "}}");
114 g_assert(response);
115 g_assert(!qdict_haskey(response, "error"));
116 qobject_unref(response);
118 response = qmp("{'execute': 'object-del',"
119 " 'arguments': {"
120 " 'id': 'qtest-f1'"
121 "}}");
122 g_assert(response);
123 g_assert(!qdict_haskey(response, "error"));
124 qobject_unref(response);
127 /* add multi(2) netfilters to a netdev and then remove the netdev */
128 static void remove_netdev_with_multi_netfilter(void)
130 QDict *response;
132 response = qmp("{'execute': 'object-add',"
133 " 'arguments': {"
134 " 'qom-type': 'filter-buffer',"
135 " 'id': 'qtest-f0',"
136 " 'netdev': 'qtest-bn0',"
137 " 'queue': 'rx',"
138 " 'interval': 1000"
139 "}}");
141 g_assert(response);
142 g_assert(!qdict_haskey(response, "error"));
143 qobject_unref(response);
145 response = qmp("{'execute': 'object-add',"
146 " 'arguments': {"
147 " 'qom-type': 'filter-buffer',"
148 " 'id': 'qtest-f1',"
149 " 'netdev': 'qtest-bn0',"
150 " 'queue': 'rx',"
151 " 'interval': 1000"
152 "}}");
154 g_assert(response);
155 g_assert(!qdict_haskey(response, "error"));
156 qobject_unref(response);
158 response = qmp("{'execute': 'netdev_del',"
159 " 'arguments': {"
160 " 'id': 'qtest-bn0'"
161 "}}");
162 g_assert(response);
163 g_assert(!qdict_haskey(response, "error"));
164 qobject_unref(response);
166 /* add back the netdev */
167 response = qmp("{'execute': 'netdev_add',"
168 " 'arguments': {"
169 " 'type': 'user',"
170 " 'id': 'qtest-bn0'"
171 "}}");
172 g_assert(response);
173 g_assert(!qdict_haskey(response, "error"));
174 qobject_unref(response);
177 int main(int argc, char **argv)
179 int ret;
180 char *args;
181 const char *devstr = "e1000";
183 if (g_str_equal(qtest_get_arch(), "s390x")) {
184 devstr = "virtio-net-ccw";
187 g_test_init(&argc, &argv, NULL);
188 qtest_add_func("/netfilter/addremove_one", add_one_netfilter);
189 qtest_add_func("/netfilter/remove_netdev_one",
190 remove_netdev_with_one_netfilter);
191 qtest_add_func("/netfilter/addremove_multi", add_multi_netfilter);
192 qtest_add_func("/netfilter/remove_netdev_multi",
193 remove_netdev_with_multi_netfilter);
195 args = g_strdup_printf("-netdev user,id=qtest-bn0 "
196 "-device %s,netdev=qtest-bn0", devstr);
197 qtest_start(args);
198 ret = g_test_run();
200 qtest_end();
201 g_free(args);
203 return ret;