summaryrefslogtreecommitdiffstats
path: root/tests/test-netfilter.c
blob: e47075dd06b67bd46239c78aab2d245f505cfe55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * QTest testcase for netfilter
 *
 * Copyright (c) 2015 FUJITSU LIMITED
 * Author: Yang Hongyang <yanghy@cn.fujitsu.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or
 * later.  See the COPYING file in the top-level directory.
 */

#include "qemu/osdep.h"
#include "libqtest.h"
#include "qapi/qmp/qdict.h"

/* add a netfilter to a netdev and then remove it */
static void add_one_netfilter(void)
{
    QDict *response;

    response = qmp("{'execute': 'object-add',"
                   " 'arguments': {"
                   "   'qom-type': 'filter-buffer',"
                   "   'id': 'qtest-f0',"
                   "   'props': {"
                   "     'netdev': 'qtest-bn0',"
                   "     'queue': 'rx',"
                   "     'interval': 1000"
                   "}}}");

    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);

    response = qmp("{'execute': 'object-del',"
                   " 'arguments': {"
                   "   'id': 'qtest-f0'"
                   "}}");
    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);
}

/* add a netfilter to a netdev and then remove the netdev */
static void remove_netdev_with_one_netfilter(void)
{
    QDict *response;

    response = qmp("{'execute': 'object-add',"
                   " 'arguments': {"
                   "   'qom-type': 'filter-buffer',"
                   "   'id': 'qtest-f0',"
                   "   'props': {"
                   "     'netdev': 'qtest-bn0',"
                   "     'queue': 'rx',"
                   "     'interval': 1000"
                   "}}}");

    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);

    response = qmp("{'execute': 'netdev_del',"
                   " 'arguments': {"
                   "   'id': 'qtest-bn0'"
                   "}}");
    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);

    /* add back the netdev */
    response = qmp("{'execute': 'netdev_add',"
                   " 'arguments': {"
                   "   'type': 'user',"
                   "   'id': 'qtest-bn0'"
                   "}}");
    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);
}

/* add multi(2) netfilters to a netdev and then remove them */
static void add_multi_netfilter(void)
{
    QDict *response;

    response = qmp("{'execute': 'object-add',"
                   " 'arguments': {"
                   "   'qom-type': 'filter-buffer',"
                   "   'id': 'qtest-f0',"
                   "   'props': {"
                   "     'netdev': 'qtest-bn0',"
                   "     'queue': 'rx',"
                   "     'interval': 1000"
                   "}}}");

    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);

    response = qmp("{'execute': 'object-add',"
                   " 'arguments': {"
                   "   'qom-type': 'filter-buffer',"
                   "   'id': 'qtest-f1',"
                   "   'props': {"
                   "     'netdev': 'qtest-bn0',"
                   "     'queue': 'rx',"
                   "     'interval': 1000"
                   "}}}");

    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);

    response = qmp("{'execute': 'object-del',"
                   " 'arguments': {"
                   "   'id': 'qtest-f0'"
                   "}}");
    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);

    response = qmp("{'execute': 'object-del',"
                   " 'arguments': {"
                   "   'id': 'qtest-f1'"
                   "}}");
    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);
}

/* add multi(2) netfilters to a netdev and then remove the netdev */
static void remove_netdev_with_multi_netfilter(void)
{
    QDict *response;

    response = qmp("{'execute': 'object-add',"
                   " 'arguments': {"
                   "   'qom-type': 'filter-buffer',"
                   "   'id': 'qtest-f0',"
                   "   'props': {"
                   "     'netdev': 'qtest-bn0',"
                   "     'queue': 'rx',"
                   "     'interval': 1000"
                   "}}}");

    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);

    response = qmp("{'execute': 'object-add',"
                   " 'arguments': {"
                   "   'qom-type': 'filter-buffer',"
                   "   'id': 'qtest-f1',"
                   "   'props': {"
                   "     'netdev': 'qtest-bn0',"
                   "     'queue': 'rx',"
                   "     'interval': 1000"
                   "}}}");

    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);

    response = qmp("{'execute': 'netdev_del',"
                   " 'arguments': {"
                   "   'id': 'qtest-bn0'"
                   "}}");
    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);

    /* add back the netdev */
    response = qmp("{'execute': 'netdev_add',"
                   " 'arguments': {"
                   "   'type': 'user',"
                   "   'id': 'qtest-bn0'"
                   "}}");
    g_assert(response);
    g_assert(!qdict_haskey(response, "error"));
    qobject_unref(response);
}

int main(int argc, char **argv)
{
    int ret;
    char *args;
    const char *devstr = "e1000";

    if (g_str_equal(qtest_get_arch(), "s390x")) {
        devstr = "virtio-net-ccw";
    }

    g_test_init(&argc, &argv, NULL);
    qtest_add_func("/netfilter/addremove_one", add_one_netfilter);
    qtest_add_func("/netfilter/remove_netdev_one",
                   remove_netdev_with_one_netfilter);
    qtest_add_func("/netfilter/addremove_multi", add_multi_netfilter);
    qtest_add_func("/netfilter/remove_netdev_multi",
                   remove_netdev_with_multi_netfilter);

    args = g_strdup_printf("-netdev user,id=qtest-bn0 "
                           "-device %s,netdev=qtest-bn0", devstr);
    qtest_start(args);
    ret = g_test_run();

    qtest_end();
    g_free(args);

    return ret;
}