xref: /openbmc/qemu/tests/qtest/test-netfilter.c (revision 4e6b1384)
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 */
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                    "   'props': {"
25                    "     'netdev': 'qtest-bn0',"
26                    "     'queue': 'rx',"
27                    "     'interval': 1000"
28                    "}}}");
29 
30     g_assert(response);
31     g_assert(!qdict_haskey(response, "error"));
32     qobject_unref(response);
33 
34     response = qmp("{'execute': 'object-del',"
35                    " 'arguments': {"
36                    "   'id': 'qtest-f0'"
37                    "}}");
38     g_assert(response);
39     g_assert(!qdict_haskey(response, "error"));
40     qobject_unref(response);
41 }
42 
43 /* add a netfilter to a netdev and then remove the netdev */
44 static void remove_netdev_with_one_netfilter(void)
45 {
46     QDict *response;
47 
48     response = qmp("{'execute': 'object-add',"
49                    " 'arguments': {"
50                    "   'qom-type': 'filter-buffer',"
51                    "   'id': 'qtest-f0',"
52                    "   'props': {"
53                    "     'netdev': 'qtest-bn0',"
54                    "     'queue': 'rx',"
55                    "     'interval': 1000"
56                    "}}}");
57 
58     g_assert(response);
59     g_assert(!qdict_haskey(response, "error"));
60     qobject_unref(response);
61 
62     response = qmp("{'execute': 'netdev_del',"
63                    " 'arguments': {"
64                    "   'id': 'qtest-bn0'"
65                    "}}");
66     g_assert(response);
67     g_assert(!qdict_haskey(response, "error"));
68     qobject_unref(response);
69 
70     /* add back the netdev */
71     response = qmp("{'execute': 'netdev_add',"
72                    " 'arguments': {"
73                    "   'type': 'user',"
74                    "   'id': 'qtest-bn0'"
75                    "}}");
76     g_assert(response);
77     g_assert(!qdict_haskey(response, "error"));
78     qobject_unref(response);
79 }
80 
81 /* add multi(2) netfilters to a netdev and then remove them */
82 static void add_multi_netfilter(void)
83 {
84     QDict *response;
85 
86     response = qmp("{'execute': 'object-add',"
87                    " 'arguments': {"
88                    "   'qom-type': 'filter-buffer',"
89                    "   'id': 'qtest-f0',"
90                    "   'props': {"
91                    "     'netdev': 'qtest-bn0',"
92                    "     'queue': 'rx',"
93                    "     'interval': 1000"
94                    "}}}");
95 
96     g_assert(response);
97     g_assert(!qdict_haskey(response, "error"));
98     qobject_unref(response);
99 
100     response = qmp("{'execute': 'object-add',"
101                    " 'arguments': {"
102                    "   'qom-type': 'filter-buffer',"
103                    "   'id': 'qtest-f1',"
104                    "   'props': {"
105                    "     'netdev': 'qtest-bn0',"
106                    "     'queue': 'rx',"
107                    "     'interval': 1000"
108                    "}}}");
109 
110     g_assert(response);
111     g_assert(!qdict_haskey(response, "error"));
112     qobject_unref(response);
113 
114     response = qmp("{'execute': 'object-del',"
115                    " 'arguments': {"
116                    "   'id': 'qtest-f0'"
117                    "}}");
118     g_assert(response);
119     g_assert(!qdict_haskey(response, "error"));
120     qobject_unref(response);
121 
122     response = qmp("{'execute': 'object-del',"
123                    " 'arguments': {"
124                    "   'id': 'qtest-f1'"
125                    "}}");
126     g_assert(response);
127     g_assert(!qdict_haskey(response, "error"));
128     qobject_unref(response);
129 }
130 
131 /* add multi(2) netfilters to a netdev and then remove the netdev */
132 static void remove_netdev_with_multi_netfilter(void)
133 {
134     QDict *response;
135 
136     response = qmp("{'execute': 'object-add',"
137                    " 'arguments': {"
138                    "   'qom-type': 'filter-buffer',"
139                    "   'id': 'qtest-f0',"
140                    "   'props': {"
141                    "     'netdev': 'qtest-bn0',"
142                    "     'queue': 'rx',"
143                    "     'interval': 1000"
144                    "}}}");
145 
146     g_assert(response);
147     g_assert(!qdict_haskey(response, "error"));
148     qobject_unref(response);
149 
150     response = qmp("{'execute': 'object-add',"
151                    " 'arguments': {"
152                    "   'qom-type': 'filter-buffer',"
153                    "   'id': 'qtest-f1',"
154                    "   'props': {"
155                    "     'netdev': 'qtest-bn0',"
156                    "     'queue': 'rx',"
157                    "     'interval': 1000"
158                    "}}}");
159 
160     g_assert(response);
161     g_assert(!qdict_haskey(response, "error"));
162     qobject_unref(response);
163 
164     response = qmp("{'execute': 'netdev_del',"
165                    " 'arguments': {"
166                    "   'id': 'qtest-bn0'"
167                    "}}");
168     g_assert(response);
169     g_assert(!qdict_haskey(response, "error"));
170     qobject_unref(response);
171 
172     /* add back the netdev */
173     response = qmp("{'execute': 'netdev_add',"
174                    " 'arguments': {"
175                    "   'type': 'user',"
176                    "   'id': 'qtest-bn0'"
177                    "}}");
178     g_assert(response);
179     g_assert(!qdict_haskey(response, "error"));
180     qobject_unref(response);
181 }
182 
183 int main(int argc, char **argv)
184 {
185     int ret;
186     char *args;
187     const char *devstr = "e1000";
188 
189     if (g_str_equal(qtest_get_arch(), "s390x")) {
190         devstr = "virtio-net-ccw";
191     }
192 
193     g_test_init(&argc, &argv, NULL);
194     qtest_add_func("/netfilter/addremove_one", add_one_netfilter);
195     qtest_add_func("/netfilter/remove_netdev_one",
196                    remove_netdev_with_one_netfilter);
197     qtest_add_func("/netfilter/addremove_multi", add_multi_netfilter);
198     qtest_add_func("/netfilter/remove_netdev_multi",
199                    remove_netdev_with_multi_netfilter);
200 
201     args = g_strdup_printf("-netdev user,id=qtest-bn0 "
202                            "-device %s,netdev=qtest-bn0", devstr);
203     qtest_start(args);
204     ret = g_test_run();
205 
206     qtest_end();
207     g_free(args);
208 
209     return ret;
210 }
211