xref: /openbmc/qemu/tests/qtest/test-netfilter.c (revision 31fb263c)
1 /*
2  * QTest testcase for netfilter
3  *
4  * Copyright (c) 2015 FUJITSU LIMITED
5  * Author: Yang Hongyang <yanghy@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"
12 #include "libqtest-single.h"
13 #include "qapi/qmp/qdict.h"
14 
15 /* add a netfilter to a netdev and then remove it */
add_one_netfilter(void)16 static void add_one_netfilter(void)
17 {
18     QDict *response;
19 
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                    "}}");
28 
29     g_assert(response);
30     g_assert(!qdict_haskey(response, "error"));
31     qobject_unref(response);
32 
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);
40 }
41 
42 /* add a netfilter to a netdev and then remove the netdev */
remove_netdev_with_one_netfilter(void)43 static void remove_netdev_with_one_netfilter(void)
44 {
45     QDict *response;
46 
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                    "}}");
55 
56     g_assert(response);
57     g_assert(!qdict_haskey(response, "error"));
58     qobject_unref(response);
59 
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);
67 
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);
77 }
78 
79 /* add multi(2) netfilters to a netdev and then remove them */
add_multi_netfilter(void)80 static void add_multi_netfilter(void)
81 {
82     QDict *response;
83 
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                    "}}");
92 
93     g_assert(response);
94     g_assert(!qdict_haskey(response, "error"));
95     qobject_unref(response);
96 
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                    "}}");
105 
106     g_assert(response);
107     g_assert(!qdict_haskey(response, "error"));
108     qobject_unref(response);
109 
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);
117 
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);
125 }
126 
127 /* add multi(2) netfilters to a netdev and then remove the netdev */
remove_netdev_with_multi_netfilter(void)128 static void remove_netdev_with_multi_netfilter(void)
129 {
130     QDict *response;
131 
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                    "}}");
140 
141     g_assert(response);
142     g_assert(!qdict_haskey(response, "error"));
143     qobject_unref(response);
144 
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                    "}}");
153 
154     g_assert(response);
155     g_assert(!qdict_haskey(response, "error"));
156     qobject_unref(response);
157 
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);
165 
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);
175 }
176 
main(int argc,char ** argv)177 int main(int argc, char **argv)
178 {
179     int ret;
180     char *args;
181 
182     g_test_init(&argc, &argv, NULL);
183     qtest_add_func("/netfilter/addremove_one", add_one_netfilter);
184     qtest_add_func("/netfilter/remove_netdev_one",
185                    remove_netdev_with_one_netfilter);
186     qtest_add_func("/netfilter/addremove_multi", add_multi_netfilter);
187     qtest_add_func("/netfilter/remove_netdev_multi",
188                    remove_netdev_with_multi_netfilter);
189 
190     args = g_strdup_printf("-nic user,id=qtest-bn0");
191     qtest_start(args);
192     ret = g_test_run();
193 
194     qtest_end();
195     g_free(args);
196 
197     return ret;
198 }
199