qemu/tests/test-netfilter.c
<<
>>
Prefs
   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.h"
  13
  14/* add a netfilter to a netdev and then remove it */
  15static void add_one_netfilter(void)
  16{
  17    QDict *response;
  18
  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                   "}}}");
  28
  29    g_assert(response);
  30    g_assert(!qdict_haskey(response, "error"));
  31    QDECREF(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    QDECREF(response);
  40}
  41
  42/* add a netfilter to a netdev and then remove the netdev */
  43static 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                   "   'props': {"
  52                   "     'netdev': 'qtest-bn0',"
  53                   "     'queue': 'rx',"
  54                   "     'interval': 1000"
  55                   "}}}");
  56
  57    g_assert(response);
  58    g_assert(!qdict_haskey(response, "error"));
  59    QDECREF(response);
  60
  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);
  68
  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);
  78}
  79
  80/* add multi(2) netfilters to a netdev and then remove them */
  81static void add_multi_netfilter(void)
  82{
  83    QDict *response;
  84
  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                   "}}}");
  94
  95    g_assert(response);
  96    g_assert(!qdict_haskey(response, "error"));
  97    QDECREF(response);
  98
  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                   "}}}");
 108
 109    g_assert(response);
 110    g_assert(!qdict_haskey(response, "error"));
 111    QDECREF(response);
 112
 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);
 120
 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);
 128}
 129
 130/* add multi(2) netfilters to a netdev and then remove the netdev */
 131static void remove_netdev_with_multi_netfilter(void)
 132{
 133    QDict *response;
 134
 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                   "}}}");
 144
 145    g_assert(response);
 146    g_assert(!qdict_haskey(response, "error"));
 147    QDECREF(response);
 148
 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                   "}}}");
 158
 159    g_assert(response);
 160    g_assert(!qdict_haskey(response, "error"));
 161    QDECREF(response);
 162
 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);
 170
 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);
 180}
 181
 182int main(int argc, char **argv)
 183{
 184    int ret;
 185    char *args;
 186    const char *devstr = "e1000";
 187
 188    if (g_str_equal(qtest_get_arch(), "s390x")) {
 189        devstr = "virtio-net-ccw";
 190    }
 191
 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);
 199
 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();
 204
 205    qtest_end();
 206    g_free(args);
 207
 208    return ret;
 209}
 210