Skip to contentSkip to conversation

net: add a new object netfilter

PATCH Superseded Yang Hongyang 2 participants 22 messages Aug 3, 2015 – Aug 4, 2015 1 message not in the archive Networking

v3 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v3 1/12 net: add a new object netfilter

Add the framework for a new netfilter object and a new -netfilter CLI option as a basis for the following patches.

---
 include/net/filter.h    | 15 +++++++++++++++
 include/sysemu/sysemu.h |  1 +
 net/Makefile.objs       |  1 +
 net/filter.c            | 27 +++++++++++++++++++++++++++
 qemu-options.hx         |  1 +
 vl.c                    | 13 +++++++++++++
 6 files changed, 58 insertions(+)
 create mode 100644 include/net/filter.h
 create mode 100644 net/filter.c
include/net/filter.h new file +15 -0
new file mode 100644
index 0000000..4242ded
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTER_H
+#define QEMU_NET_FILTER_H
+
+#include "qemu-common.h"
+
+int net_init_filters(void);
+
+#endif /* QEMU_NET_FILTER_H */
include/sysemu/sysemu.h +1 -0
index 44570d1..15d6d00 100644
@@ -212,6 +212,7 @@ extern QemuOptsList qemu_chardev_opts;
 extern QemuOptsList qemu_device_opts;
 extern QemuOptsList qemu_netdev_opts;
 extern QemuOptsList qemu_net_opts;
+extern QemuOptsList qemu_netfilter_opts;
 extern QemuOptsList qemu_global_opts;
 extern QemuOptsList qemu_mon_opts;
 
net/Makefile.objs +1 -0
index ec19cb3..914aec0 100644
@@ -13,3 +13,4 @@ common-obj-$(CONFIG_HAIKU) += tap-haiku.o
 common-obj-$(CONFIG_SLIRP) += slirp.o
 common-obj-$(CONFIG_VDE) += vde.o
 common-obj-$(CONFIG_NETMAP) += netmap.o
+common-obj-y += filter.o
net/filter.c new file +27 -0
new file mode 100644
index 0000000..4e40f08
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ *
+ * 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-common.h"
+#include "net/filter.h"
+
+int net_init_filters(void)
+{
+    return 0;
+}
+
+QemuOptsList qemu_netfilter_opts = {
+    .name = "netfilter",
+    .implied_opt_name = "type",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_netfilter_opts.head),
+    .desc = {
+        /*
+         * no elements => accept any params
+         * validation will happen later
+         */
+        { /* end of list */ }
+    },
+};
qemu-options.hx +1 -0
index 77f5853..0d52d02 100644
@@ -1575,6 +1575,7 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
     "socket][,vlan=n][,option][,option][,...]\n"
     "                old way to initialize a host network interface\n"
     "                (use the -netdev option if possible instead)\n", QEMU_ARCH_ALL)
+DEF("netfilter", HAS_ARG, QEMU_OPTION_netfilter, "", QEMU_ARCH_ALL)
 STEXI
 @item -net nic[,vlan=@var{n}][,macaddr=@var{mac}][,model=@var{type}] [,name=@var{name}][,addr=@var{addr}][,vectors=@var{v}]
 @findex -net
vl.c +13 -0
index 0adbbd6..3b70371 100644
@@ -75,6 +75,7 @@ int main(int argc, char **argv)
 #include "monitor/qdev.h"
 #include "sysemu/bt.h"
 #include "net/net.h"
+#include "net/filter.h"
 #include "net/slirp.h"
 #include "monitor/monitor.h"
 #include "ui/console.h"
@@ -2998,6 +2999,7 @@ int main(int argc, char **argv, char **envp)
     qemu_add_opts(&qemu_device_opts);
     qemu_add_opts(&qemu_netdev_opts);
     qemu_add_opts(&qemu_net_opts);
+    qemu_add_opts(&qemu_netfilter_opts);
     qemu_add_opts(&qemu_rtc_opts);
     qemu_add_opts(&qemu_global_opts);
     qemu_add_opts(&qemu_mon_opts);
@@ -3284,6 +3286,13 @@ int main(int argc, char **argv, char **envp)
                     exit(1);
                 }
                 break;
+            case QEMU_OPTION_netfilter:
+                opts = qemu_opts_parse_noisily(qemu_find_opts("netfilter"),
+                                               optarg, true);
+                if (!opts) {
+                    exit(1);
+                }
+                break;
 #ifdef CONFIG_LIBISCSI
             case QEMU_OPTION_iscsi:
                 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
@@ -4413,6 +4422,10 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
+    if (net_init_filters() < 0) {
+        exit(1);
+    }
+
 #ifdef CONFIG_TPM
     if (tpm_init() < 0) {
         exit(1);
Signature · 1 line
1.9.1
#2 lore raw

PATCH v3 2/12 init/cleanup of netfilter object

In reply to a message not in the archive

This is mostly the same with init/cleanup of netdev object.

---
 include/net/filter.h    |  38 +++++++++++++
 include/qemu/typedefs.h |   1 +
 net/filter.c            | 148 ++++++++++++++++++++++++++++++++++++++++++++++++
 qapi-schema.json        |  37 ++++++++++++
 4 files changed, 224 insertions(+)
include/net/filter.h +38 -0
index 4242ded..dee265a 100644
@@ -9,7 +9,45 @@
 #define QEMU_NET_FILTER_H
 
 #include "qemu-common.h"
+#include "qemu/typedefs.h"
+
+/* the netfilter chain */
+enum {
+    NET_FILTER_IN,
+    NET_FILTER_OUT,
+    NET_FILTER_ALL,
+};
+
+typedef void (FilterCleanup) (NetFilterState *);
+/*
+ * Return:
+ *   0: finished handling the packet, we should continue
+ *   size: filter stolen this packet, we stop pass this packet further
+ */
+typedef ssize_t (FilterReceiveIOV)(NetFilterState *, NetClientState *sender,
+                                   unsigned flags, const struct iovec *, int);
+
+typedef struct NetFilterInfo {
+    NetFilterOptionsKind type;
+    size_t size;
+    FilterCleanup *cleanup;
+    FilterReceiveIOV *receive_iov;
+} NetFilterInfo;
+
+struct NetFilterState {
+    NetFilterInfo *info;
+    char *model;
+    char *name;
+    NetClientState *netdev;
+    int chain;
+    QTAILQ_ENTRY(NetFilterState) next;
+};
 
 int net_init_filters(void);
+NetFilterState *qemu_new_net_filter(NetFilterInfo *info,
+                                    NetClientState *netdev,
+                                    const char *model,
+                                    const char *name,
+                                    int chain);
 
 #endif /* QEMU_NET_FILTER_H */
include/qemu/typedefs.h +1 -0
index 6fdcbcd..2f75109 100644
@@ -45,6 +45,7 @@ typedef struct Monitor Monitor;
 typedef struct MouseTransformInfo MouseTransformInfo;
 typedef struct MSIMessage MSIMessage;
 typedef struct NetClientState NetClientState;
+typedef struct NetFilterState NetFilterState;
 typedef struct NICInfo NICInfo;
 typedef struct PcGuestInfo PcGuestInfo;
 typedef struct PCIBridge PCIBridge;
net/filter.c +148 -0

158 lines not on this page · raw message

qapi-schema.json +37 -0
index 4342a08..d7fb578 100644
@@ -2537,6 +2537,43 @@
     'opts': 'NetClientOptions' } }
 
 ##
+# @NetFilterOptions
+#
+# A discriminated record of network filters.
+#
+# Since 2.5
+#
+##
+{ 'union': 'NetFilterOptions',
+  'data': { } }
+
+##
+# @NetFilter
+#
+# Captures the packets of a network backend.
+#
+# @id: identifier for monitor commands
+#
+# @netdev: the network backend it attached to
+#
+# @chain: #optional accept "in","out","all", if not specified, default is "all"
+#         "in" means this filter will receive packets sent to the @netdev
+#         "out" means this filter will receive packets sent from the @netdev
+#         "all" means this filter will receive packets both sent to/from
+#               the @netdev
+#
+# @opts: filter type specific properties
+#
+# Since 2.5
+##
+{ 'struct': 'NetFilter',
+  'data': {
+    'id':   'str',
+    'netdev': 'str',
+    '*chain': 'str',
+    'opts': 'NetFilterOptions' } }
+
+##
 # @InetSocketAddress
 #
 # Captures a socket address or address range in the Internet namespace.
Signature · 1 line
1.9.1
#3 lore raw

PATCH v3 3/12 netfilter: add netfilter_{add|del} commands

In reply to a message not in the archive

add netfilter_{add|del} commands
This is mostly the same with netdev_{add|del} commands.

CC Luiz Capitulino <[email protected]>
CC Markus Armbruster <[email protected]>
CC Eric Blake <[email protected]>
---
 hmp-commands.hx      | 30 +++++++++++++++++++++
 hmp.c                | 29 ++++++++++++++++++++
 hmp.h                |  4 +++
 include/net/filter.h |  2 ++
 monitor.c            | 33 +++++++++++++++++++++++
 net/filter.c         | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 qapi-schema.json     | 47 ++++++++++++++++++++++++++++++++
 qmp-commands.hx      | 57 +++++++++++++++++++++++++++++++++++++++
 8 files changed, 277 insertions(+), 1 deletion(-)
hmp-commands.hx +30 -0
index d3b7932..902e2d1 100644
@@ -1253,6 +1253,36 @@ Remove host network device.
 ETEXI
 
     {
+        .name       = "netfilter_add",
+        .args_type  = "netfilter:O",
+        .params     = "[type],id=str,netdev=str[,chain=in|out|all,prop=value][,...]",
+        .help       = "add netfilter",
+        .mhandler.cmd = hmp_netfilter_add,
+        .command_completion = netfilter_add_completion,
+    },
+
+STEXI
+@item netfilter_add
+@findex netfilter_add
+Add netfilter.
+ETEXI
+
+    {
+        .name       = "netfilter_del",
+        .args_type  = "id:s",
+        .params     = "id",
+        .help       = "remove netfilter",
+        .mhandler.cmd = hmp_netfilter_del,
+        .command_completion = netfilter_del_completion,
+    },
+
+STEXI
+@item netfilter_del
+@findex netfilter_del
+Remove netfilter.
+ETEXI
+
+    {
         .name       = "object_add",
         .args_type  = "object:O",
         .params     = "[qom-type=]type,id=str[,prop=value][,...]",
hmp.c +29 -0
index dcc66f1..09e3cda 100644
@@ -15,6 +15,7 @@
 
 #include "hmp.h"
 #include "net/net.h"
+#include "net/filter.h"
 #include "net/eth.h"
 #include "sysemu/char.h"
 #include "sysemu/block-backend.h"
@@ -1599,6 +1600,34 @@ void hmp_netdev_del(Monitor *mon, const QDict *qdict)
     hmp_handle_error(mon, &err);
 }
 
+void hmp_netfilter_add(Monitor *mon, const QDict *qdict)
+{
+    Error *err = NULL;
+    QemuOpts *opts;
+
+    opts = qemu_opts_from_qdict(qemu_find_opts("netfilter"), qdict, &err);
+    if (err) {
+        goto out;
+    }
+
+    netfilter_add(opts, &err);
+    if (err) {
+        qemu_opts_del(opts);
+    }
+
+out:
+    hmp_handle_error(mon, &err);
+}
+
+void hmp_netfilter_del(Monitor *mon, const QDict *qdict)
+{
+    const char *id = qdict_get_str(qdict, "id");
+    Error *err = NULL;
+
+    qmp_netfilter_del(id, &err);
+    hmp_handle_error(mon, &err);
+}
+
 void hmp_object_add(Monitor *mon, const QDict *qdict)
 {
     Error *err = NULL;
hmp.h +4 -0
index 0cf4f2a..a21dbbb 100644
@@ -85,6 +85,8 @@ void hmp_device_del(Monitor *mon, const QDict *qdict);
 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict);
 void hmp_netdev_add(Monitor *mon, const QDict *qdict);
 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
+void hmp_netfilter_add(Monitor *mon, const QDict *qdict);
+void hmp_netfilter_del(Monitor *mon, const QDict *qdict);
 void hmp_getfd(Monitor *mon, const QDict *qdict);
 void hmp_closefd(Monitor *mon, const QDict *qdict);
 void hmp_sendkey(Monitor *mon, const QDict *qdict);
@@ -112,6 +114,8 @@ void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str);
+void netfilter_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void netfilter_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str);
 void watchdog_action_completion(ReadLineState *rs, int nb_args,
                                 const char *str);
include/net/filter.h +2 -0
index dee265a..93579c1 100644
@@ -49,5 +49,7 @@ NetFilterState *qemu_new_net_filter(NetFilterInfo *info,
                                     const char *model,
                                     const char *name,
                                     int chain);
+void netfilter_add(QemuOpts *opts, Error **errp);
+void qmp_netfilter_add(QDict *qdict, QObject **ret, Error **errp);
 
 #endif /* QEMU_NET_FILTER_H */
monitor.c +33 -0
index aeea2b5..d6b8f24 100644
@@ -31,6 +31,7 @@
 #include "hw/loader.h"
 #include "exec/gdbstub.h"
 #include "net/net.h"
+#include "net/filter.h"
 #include "net/slirp.h"
 #include "sysemu/char.h"
 #include "ui/qemu-spice.h"
@@ -4193,6 +4194,21 @@ void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
     }
 }
 
+void netfilter_add_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+    int i;
+
+    if (nb_args != 2) {
+        return;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    for (i = 0; NetFilterOptionsKind_lookup[i]; i++) {
+        add_completion_option(rs, str, NetFilterOptionsKind_lookup[i]);
+    }
+}
+
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
@@ -4429,6 +4445,23 @@ void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
     }
 }
 
+void netfilter_del_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    int len;
+    QemuOpts *opts;
+
+    if (nb_args != 2) {
+        return;
+    }
+
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    opts = qemu_opts_find(qemu_find_opts_err("netfilter", NULL), str);
+    if (opts) {
+        readline_add_completion(rs, str);
+    }
+}
+
 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     int i;
net/filter.c +75 -1

94 lines not on this page · raw message

qapi-schema.json +47 -0

53 lines not on this page · raw message

qmp-commands.hx +57 -0

63 lines not on this page · raw message

Signature · 1 line
1.9.1
#4 lore raw

PATCH v3 4/12 net: add/remove filters from network backend

In reply to a message not in the archive

add/remove filters from network backend

---
 include/net/net.h |  8 ++++++++
 net/filter.c      |  4 ++--
 net/net.c         | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)
include/net/net.h +8 -0
index 6a6cbef..5c5c109 100644
@@ -40,6 +40,11 @@ typedef struct NICConf {
 
 
 /* Net clients */
+typedef struct Filter Filter;
+struct Filter {
+    NetFilterState *nf;
+    QTAILQ_ENTRY(Filter) next;
+};
 
 typedef void (NetPoll)(NetClientState *, bool enable);
 typedef int (NetCanReceive)(NetClientState *);
@@ -92,6 +97,7 @@ struct NetClientState {
     NetClientDestructor *destructor;
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, Filter) filters;
 };
 
 typedef struct NICState {
@@ -109,6 +115,8 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
                                     NetClientState *peer,
                                     const char *model,
                                     const char *name);
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf);
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf);
 NICState *qemu_new_nic(NetClientInfo *info,
                        NICConf *conf,
                        const char *model,
net/filter.c +2 -2
index 86eed8a..1ae9344 100644
@@ -38,14 +38,14 @@ NetFilterState *qemu_new_net_filter(NetFilterInfo *info,
     nf->netdev = netdev;
     nf->chain = chain;
     QTAILQ_INSERT_TAIL(&net_filters, nf, next);
-    /* TODO: attach netfilter to netdev */
+    qemu_netdev_add_filter(netdev, nf);
 
     return nf;
 }
 
 static void qemu_cleanup_net_filter(NetFilterState *nf)
 {
-    /* TODO: remove netfilter from netdev */
+    qemu_netdev_remove_filter(nf->netdev, nf);
 
     QTAILQ_REMOVE(&net_filters, nf, next);
 
net/net.c +33 -0
index 28a5597..00c5ca3 100644
@@ -287,6 +287,7 @@ static void qemu_net_client_setup(NetClientState *nc,
 
     nc->incoming_queue = qemu_new_net_queue(nc);
     nc->destructor = destructor;
+    QTAILQ_INIT(&nc->filters);
 }
 
 NetClientState *qemu_new_net_client(NetClientInfo *info,
@@ -305,6 +306,38 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
     return nc;
 }
 
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = g_malloc0(sizeof(*filter));
+
+    filter->nf = nf;
+    QTAILQ_INSERT_TAIL(&nc->filters, filter, next);
+    return 0;
+}
+
+static void remove_filter(NetClientState *nc, Filter *filter)
+{
+    if (!filter) {
+        return;
+    }
+
+    QTAILQ_REMOVE(&nc->filters, filter, next);
+    g_free(filter);
+}
+
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = NULL;
+
+    QTAILQ_FOREACH(filter, &nc->filters, next) {
+        if (filter->nf == nf) {
+            break;
+        }
+    }
+
+    remove_filter(nc, filter);
+}
+
 NICState *qemu_new_nic(NetClientInfo *info,
                        NICConf *conf,
                        const char *model,
Signature · 1 line
1.9.1
#5 lore raw

Re: [Qemu-devel] [PATCH v3 04/12] net: add/remove filters from network backend

Quoted 14 lines · Yang Hongyang

add/remove filters from network backend

---
 include/net/net.h |  8 ++++++++
 net/filter.c      |  4 ++--
 net/net.c         | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)
include/net/net.h +0 -0
index 6a6cbef..5c5c109 100644

include/net/net.h typedef struct NICConf { · line 47

Quoted 8 lines
include/net/net.h +5 -0
@@ -40,6 +40,11 @@ typedef struct NICConf {
 
 
 /* Net clients */
+typedef struct Filter Filter;
+struct Filter {
+    NetFilterState *nf;
+    QTAILQ_ENTRY(Filter) next;
+};

Didn't understand why need another structure here. Could we just use NetFilterState?

Quoted 82 lines
include/net/net.h +3 -0
 typedef void (NetPoll)(NetClientState *, bool enable);
 typedef int (NetCanReceive)(NetClientState *);
@@ -92,6 +97,7 @@ struct NetClientState {
     NetClientDestructor *destructor;
     unsigned int queue_index;
     unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, Filter) filters;
 };
 
 typedef struct NICState {
@@ -109,6 +115,8 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
                                     NetClientState *peer,
                                     const char *model,
                                     const char *name);
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf);
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf);
 NICState *qemu_new_nic(NetClientInfo *info,
                        NICConf *conf,
                        const char *model,
net/filter.c +2 -2
index 86eed8a..1ae9344 100644
@@ -38,14 +38,14 @@ NetFilterState *qemu_new_net_filter(NetFilterInfo *info,
     nf->netdev = netdev;
     nf->chain = chain;
     QTAILQ_INSERT_TAIL(&net_filters, nf, next);
-    /* TODO: attach netfilter to netdev */
+    qemu_netdev_add_filter(netdev, nf);
 
     return nf;
 }
 
 static void qemu_cleanup_net_filter(NetFilterState *nf)
 {
-    /* TODO: remove netfilter from netdev */
+    qemu_netdev_remove_filter(nf->netdev, nf);
 
     QTAILQ_REMOVE(&net_filters, nf, next);
 
net/net.c +26 -0
index 28a5597..00c5ca3 100644
@@ -287,6 +287,7 @@ static void qemu_net_client_setup(NetClientState *nc,
 
     nc->incoming_queue = qemu_new_net_queue(nc);
     nc->destructor = destructor;
+    QTAILQ_INIT(&nc->filters);
 }
 
 NetClientState *qemu_new_net_client(NetClientInfo *info,
@@ -305,6 +306,38 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
     return nc;
 }
 
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = g_malloc0(sizeof(*filter));
+
+    filter->nf = nf;
+    QTAILQ_INSERT_TAIL(&nc->filters, filter, next);
+    return 0;
+}
+
+static void remove_filter(NetClientState *nc, Filter *filter)
+{
+    if (!filter) {
+        return;
+    }
+
+    QTAILQ_REMOVE(&nc->filters, filter, next);
+    g_free(filter);
+}
+
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = NULL;
+
+    QTAILQ_FOREACH(filter, &nc->filters, next) {
+        if (filter->nf == nf) {

net/net.c NetClientState *qemu_new_net_client(NetClientInfo *info, · line 343

Quoted 10 lines
net/net.c +7 -0
+            break;
+        }
+    }
+
+    remove_filter(nc, filter);
+}
+
 NICState *qemu_new_nic(NetClientInfo *info,
                        NICConf *conf,
                        const char *model,

Another thing may need consider is qemu_flush_queued_packets(). Look like we need also flush packets inside each filter in this case?

#6 lore raw

Re: [Qemu-devel] [PATCH v3 04/12] net: add/remove filters from network backend

Quoted 19 lines · Jason Wang
Quoted 16 lines · Yang Hongyang

add/remove filters from network backend

---
  include/net/net.h |  8 ++++++++
  net/filter.c      |  4 ++--
  net/net.c         | 33 +++++++++++++++++++++++++++++++++
  3 files changed, 43 insertions(+), 2 deletions(-)
include/net/net.h +0 -0
index 6a6cbef..5c5c109 100644
@@ -40,6 +40,11 @@ typedef struct NICConf {
 
 
Quoted 9 lines

include/net/net.h typedef struct NICConf { · line 47

Quoted 6 lines
include/net/net.h +5 -0
  /* Net clients */
+typedef struct Filter Filter;
+struct Filter {
+    NetFilterState *nf;
+    QTAILQ_ENTRY(Filter) next;
+};

Didn't understand why need another structure here. Could we just use NetFilterState?

There's a QTAILQ_ENTRY next in NetFilterState, but used by filter layer, so that we can manage all filters together.

This struct used by netdev. filter belongs to the specific netdev is in this queue.

Just use NetFilterState in this case need to introduce another QTAILQ_ENTRY in NetFilterState, maybe will make it confusing? and it seems that it's not common to have 2 QTAILQ_ENTRYs in one struct?

Quoted 88 lines
Quoted 88 lines
include/net/net.h +3 -0
  typedef void (NetPoll)(NetClientState *, bool enable);
  typedef int (NetCanReceive)(NetClientState *);
@@ -92,6 +97,7 @@ struct NetClientState {
      NetClientDestructor *destructor;
      unsigned int queue_index;
      unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, Filter) filters;
  };
 
  typedef struct NICState {
@@ -109,6 +115,8 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
                                      NetClientState *peer,
                                      const char *model,
                                      const char *name);
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf);
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf);
  NICState *qemu_new_nic(NetClientInfo *info,
                         NICConf *conf,
                         const char *model,
net/filter.c +2 -2
index 86eed8a..1ae9344 100644
@@ -38,14 +38,14 @@ NetFilterState *qemu_new_net_filter(NetFilterInfo *info,
      nf->netdev = netdev;
      nf->chain = chain;
      QTAILQ_INSERT_TAIL(&net_filters, nf, next);
-    /* TODO: attach netfilter to netdev */
+    qemu_netdev_add_filter(netdev, nf);
 
      return nf;
  }
 
  static void qemu_cleanup_net_filter(NetFilterState *nf)
  {
-    /* TODO: remove netfilter from netdev */
+    qemu_netdev_remove_filter(nf->netdev, nf);
 
      QTAILQ_REMOVE(&net_filters, nf, next);
 
net/net.c +31 -0
index 28a5597..00c5ca3 100644
@@ -287,6 +287,7 @@ static void qemu_net_client_setup(NetClientState *nc,
 
      nc->incoming_queue = qemu_new_net_queue(nc);
      nc->destructor = destructor;
+    QTAILQ_INIT(&nc->filters);
  }
 
  NetClientState *qemu_new_net_client(NetClientInfo *info,
@@ -305,6 +306,38 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
      return nc;
  }
 
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = g_malloc0(sizeof(*filter));
+
+    filter->nf = nf;
+    QTAILQ_INSERT_TAIL(&nc->filters, filter, next);
+    return 0;
+}
+
+static void remove_filter(NetClientState *nc, Filter *filter)
+{
+    if (!filter) {
+        return;
+    }
+
+    QTAILQ_REMOVE(&nc->filters, filter, next);
+    g_free(filter);
+}
+
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = NULL;
+
+    QTAILQ_FOREACH(filter, &nc->filters, next) {
+        if (filter->nf == nf) {
+            break;
+        }
+    }
+
+    remove_filter(nc, filter);
Quoted 8 lines

net/net.c NetClientState *qemu_new_net_client(NetClientInfo *info, · line 343

Quoted 5 lines
net/net.c +2 -0
+}
+
  NICState *qemu_new_nic(NetClientInfo *info,
                         NICConf *conf,
                         const char *model,

Another thing may need consider is qemu_flush_queued_packets(). Look like we need also flush packets inside each filter in this case?

When a filter is removed, the filter itself will flush the packets. when a filter queued a packet, we assume the filter will take care of it. the filter is not a netdev, so I think we do not need to flush packets in qemu_flush_queued_packets(), otherwise, the buffer filter will be useless, because when qemu_flush_queued_packets() is called, it will flush the packets, it's not what we want.

Quoted 2 lines

.

Signature · 2 lines
Thanks,
Yang.
#7 lore raw

Re: [Qemu-devel] [PATCH v3 04/12] net: add/remove filters from network backend

Quoted 34 lines · Yang Hongyang
Quoted 19 lines · Jason Wang
Quoted 16 lines · Yang Hongyang

add/remove filters from network backend

---
  include/net/net.h |  8 ++++++++
  net/filter.c      |  4 ++--
  net/net.c         | 33 +++++++++++++++++++++++++++++++++
  3 files changed, 43 insertions(+), 2 deletions(-)
include/net/net.h +0 -0
index 6a6cbef..5c5c109 100644
@@ -40,6 +40,11 @@ typedef struct NICConf {
 
 
Quoted 9 lines

include/net/net.h typedef struct NICConf { · line 47

Quoted 6 lines
include/net/net.h +5 -0
  /* Net clients */
+typedef struct Filter Filter;
+struct Filter {
+    NetFilterState *nf;
+    QTAILQ_ENTRY(Filter) next;
+};

Didn't understand why need another structure here. Could we just use NetFilterState?

There's a QTAILQ_ENTRY next in NetFilterState, but used by filter layer, so
that we can manage all filters together.

Quoted 6 lines

This struct used by netdev. filter belongs to the specific netdev is in this queue.

Just use NetFilterState in this case need to introduce another QTAILQ_ENTRY
in NetFilterState, maybe will make it confusing?

Probably not.

Quoted 2 lines

and it seems that it's
not common to have 2 QTAILQ_ENTRYs in one struct?

I think this is ok. You can use something like global_list.

Quoted 103 lines
Quoted 94 lines
Quoted 94 lines
include/net/net.h +3 -0
  typedef void (NetPoll)(NetClientState *, bool enable);
  typedef int (NetCanReceive)(NetClientState *);
@@ -92,6 +97,7 @@ struct NetClientState {
      NetClientDestructor *destructor;
      unsigned int queue_index;
      unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, Filter) filters;
  };
 
  typedef struct NICState {
@@ -109,6 +115,8 @@ NetClientState
 *qemu_new_net_client(NetClientInfo *info,
                                      NetClientState *peer,
                                      const char *model,
                                      const char *name);
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf);
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState
 *nf);
  NICState *qemu_new_nic(NetClientInfo *info,

NICConf *conf,
const char *model,

net/filter.c +2 -2
index 86eed8a..1ae9344 100644
@@ -38,14 +38,14 @@ NetFilterState
 *qemu_new_net_filter(NetFilterInfo *info,
      nf->netdev = netdev;
      nf->chain = chain;
      QTAILQ_INSERT_TAIL(&net_filters, nf, next);
-    /* TODO: attach netfilter to netdev */
+    qemu_netdev_add_filter(netdev, nf);
 
      return nf;
  }
 
  static void qemu_cleanup_net_filter(NetFilterState *nf)
  {
-    /* TODO: remove netfilter from netdev */
+    qemu_netdev_remove_filter(nf->netdev, nf);
 
      QTAILQ_REMOVE(&net_filters, nf, next);
net/net.c +1 -0
index 28a5597..00c5ca3 100644
@@ -287,6 +287,7 @@ static void qemu_net_client_setup(NetClientState
 *nc,
 
      nc->incoming_queue = qemu_new_net_queue(nc);
      nc->destructor = destructor;
+    QTAILQ_INIT(&nc->filters);
  }
 

NetClientState *qemu_new_net_client(NetClientInfo *info,

net/net.c +30 -0
@@ -305,6 +306,38 @@ NetClientState
 *qemu_new_net_client(NetClientInfo *info,
      return nc;
  }
 
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = g_malloc0(sizeof(*filter));
+
+    filter->nf = nf;
+    QTAILQ_INSERT_TAIL(&nc->filters, filter, next);
+    return 0;
+}
+
+static void remove_filter(NetClientState *nc, Filter *filter)
+{
+    if (!filter) {
+        return;
+    }
+
+    QTAILQ_REMOVE(&nc->filters, filter, next);
+    g_free(filter);
+}
+
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = NULL;
+
+    QTAILQ_FOREACH(filter, &nc->filters, next) {
+        if (filter->nf == nf) {
+            break;
+        }
+    }
+
+    remove_filter(nc, filter);
Quoted 8 lines
Quoted 5 lines
net/net.c +2 -0
+}
+
  NICState *qemu_new_nic(NetClientInfo *info,
                         NICConf *conf,

const char *model,

Another thing may need consider is qemu_flush_queued_packets(). Look like we need also flush packets inside each filter in this case?

Quoted 6 lines

When a filter is removed, the filter itself will flush the packets. when a filter queued a packet, we assume the filter will take care of it. the filter is not a netdev, so I think we do not need to flush packets in qemu_flush_queued_packets(), otherwise, the buffer filter will be useless, because when qemu_flush_queued_packets() is called, it will flush the packets, it's not what we want.

Right.

#8 lore raw

Re: [Qemu-devel] [PATCH v3 04/12] net: add/remove filters from network backend

Quoted 44 lines · Jason Wang
Quoted 34 lines · Yang Hongyang
Quoted 19 lines · Jason Wang
Quoted 16 lines · Yang Hongyang

add/remove filters from network backend

---
   include/net/net.h |  8 ++++++++
   net/filter.c      |  4 ++--
   net/net.c         | 33 +++++++++++++++++++++++++++++++++
   3 files changed, 43 insertions(+), 2 deletions(-)
include/net/net.h +0 -0
index 6a6cbef..5c5c109 100644
@@ -40,6 +40,11 @@ typedef struct NICConf {
 
 
Quoted 9 lines

include/net/net.h typedef struct NICConf { · line 47

Quoted 6 lines
include/net/net.h +5 -0
   /* Net clients */
+typedef struct Filter Filter;
+struct Filter {
+    NetFilterState *nf;
+    QTAILQ_ENTRY(Filter) next;
+};

Didn't understand why need another structure here. Could we just use NetFilterState?

There's a QTAILQ_ENTRY next in NetFilterState, but used by filter layer, so
that we can manage all filters together.

Quoted 6 lines

This struct used by netdev. filter belongs to the specific netdev is in this queue.

Just use NetFilterState in this case need to introduce another QTAILQ_ENTRY
in NetFilterState, maybe will make it confusing?

Quoted 6 lines

Probably not.

Quoted 2 lines

and it seems that it's
not common to have 2 QTAILQ_ENTRYs in one struct?

I think this is ok. You can use something like global_list.

Sounds good, thank you!

Quoted 114 lines
Quoted 104 lines
Quoted 95 lines
Quoted 95 lines
include/net/net.h +3 -0
   typedef void (NetPoll)(NetClientState *, bool enable);
   typedef int (NetCanReceive)(NetClientState *);
@@ -92,6 +97,7 @@ struct NetClientState {
       NetClientDestructor *destructor;
       unsigned int queue_index;
       unsigned rxfilter_notify_enabled:1;
+    QTAILQ_HEAD(, Filter) filters;
   };
 
   typedef struct NICState {
@@ -109,6 +115,8 @@ NetClientState
 *qemu_new_net_client(NetClientInfo *info,
                                       NetClientState *peer,
                                       const char *model,
                                       const char *name);
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf);
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState
 *nf);
   NICState *qemu_new_nic(NetClientInfo *info,

NICConf *conf,
const char *model,

net/filter.c +2 -2
index 86eed8a..1ae9344 100644
@@ -38,14 +38,14 @@ NetFilterState
 *qemu_new_net_filter(NetFilterInfo *info,
       nf->netdev = netdev;
       nf->chain = chain;
       QTAILQ_INSERT_TAIL(&net_filters, nf, next);
-    /* TODO: attach netfilter to netdev */
+    qemu_netdev_add_filter(netdev, nf);
 
       return nf;
   }
 
   static void qemu_cleanup_net_filter(NetFilterState *nf)
   {
-    /* TODO: remove netfilter from netdev */
+    qemu_netdev_remove_filter(nf->netdev, nf);
 
       QTAILQ_REMOVE(&net_filters, nf, next);
net/net.c +1 -0
index 28a5597..00c5ca3 100644
@@ -287,6 +287,7 @@ static void qemu_net_client_setup(NetClientState
 *nc,
 
       nc->incoming_queue = qemu_new_net_queue(nc);
       nc->destructor = destructor;
+    QTAILQ_INIT(&nc->filters);
   }
 

NetClientState *qemu_new_net_client(NetClientInfo *info,

net/net.c +30 -0
@@ -305,6 +306,38 @@ NetClientState
 *qemu_new_net_client(NetClientInfo *info,
       return nc;
   }
 
+int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = g_malloc0(sizeof(*filter));
+
+    filter->nf = nf;
+    QTAILQ_INSERT_TAIL(&nc->filters, filter, next);
+    return 0;
+}
+
+static void remove_filter(NetClientState *nc, Filter *filter)
+{
+    if (!filter) {
+        return;
+    }
+
+    QTAILQ_REMOVE(&nc->filters, filter, next);
+    g_free(filter);
+}
+
+void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = NULL;
+
+    QTAILQ_FOREACH(filter, &nc->filters, next) {
+        if (filter->nf == nf) {
+            break;
+        }
+    }
+
+    remove_filter(nc, filter);
Quoted 8 lines
Quoted 5 lines
net/net.c +2 -0
+}
+
   NICState *qemu_new_nic(NetClientInfo *info,
                          NICConf *conf,

const char *model,

Another thing may need consider is qemu_flush_queued_packets(). Look like we need also flush packets inside each filter in this case?

Quoted 6 lines

When a filter is removed, the filter itself will flush the packets. when a filter queued a packet, we assume the filter will take care of it. the filter is not a netdev, so I think we do not need to flush packets in qemu_flush_queued_packets(), otherwise, the buffer filter will be useless, because when qemu_flush_queued_packets() is called, it will flush the packets, it's not what we want.

Right.
.

Signature · 2 lines
Thanks,
Yang.
#9 lore raw

PATCH v3 5/12 net: delete netfilter object when delete netdev

In reply to a message not in the archive

When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filters which attached to it is useless.

---
 include/net/filter.h |  1 +
 net/filter.c         |  2 +-
 net/net.c            | 14 ++++++++++++++
 3 files changed, 16 insertions(+), 1 deletion(-)
include/net/filter.h +1 -0
index 93579c1..7f0c949 100644
@@ -49,6 +49,7 @@ NetFilterState *qemu_new_net_filter(NetFilterInfo *info,
                                     const char *model,
                                     const char *name,
                                     int chain);
+void qemu_del_net_filter(NetFilterState *nf);
 void netfilter_add(QemuOpts *opts, Error **errp);
 void qmp_netfilter_add(QDict *qdict, QObject **ret, Error **errp);
 
net/filter.c +1 -1
index 1ae9344..bf113e9 100644
@@ -58,7 +58,7 @@ static void qemu_cleanup_net_filter(NetFilterState *nf)
     g_free(nf);
 }
 
-static void qemu_del_net_filter(NetFilterState *nf)
+void qemu_del_net_filter(NetFilterState *nf)
 {
     /* handle multi queue? */
     qemu_cleanup_net_filter(nf);
net/net.c +14 -0
index 00c5ca3..7f84a6d 100644
@@ -417,6 +417,8 @@ void qemu_del_net_client(NetClientState *nc)
 {
     NetClientState *ncs[MAX_QUEUE_NUM];
     int queues, i;
+    Filter *filter, *next;
+    QemuOpts *opts;
 
     assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
 
@@ -428,6 +430,18 @@ void qemu_del_net_client(NetClientState *nc)
                                           MAX_QUEUE_NUM);
     assert(queues != 0);
 
+    /*
+     * we delete/free the netfilter object attached to this netdev
+     * multiqueue netfilter is not supported now, so only delete
+     * nc->filters is enough.
+     */
+    QTAILQ_FOREACH_SAFE(filter, &nc->filters, next, next) {
+        opts = qemu_opts_find(qemu_find_opts_err("netfilter", NULL),
+                              filter->nf->name);
+        qemu_del_net_filter(filter->nf);
+        qemu_opts_del(opts);
+    }
+
     /* If there is a peer NIC, delete and cleanup client, but do not free. */
     if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
         NICState *nic = qemu_get_nic(nc->peer);
Signature · 1 line
1.9.1
#10 lore raw

PATCH v3 6/12 netfilter: hook packets before net queue send

In reply to a message not in the archive

Capture packets that will be sent.

---
 net/net.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 68 insertions(+), 1 deletion(-)
net/net.c +68 -1
index 7f84a6d..f774c39 100644
@@ -24,6 +24,7 @@
 #include "config-host.h"
 
 #include "net/net.h"
+#include "net/filter.h"
 #include "clients.h"
 #include "hub.h"
 #include "net/slirp.h"
@@ -601,6 +602,44 @@ int qemu_can_send_packet(NetClientState *sender)
     return 1;
 }
 
+static ssize_t filter_receive_iov(NetClientState *nc, int chain,
+                                  NetClientState *sender,
+                                  unsigned flags,
+                                  const struct iovec *iov,
+                                  int iovcnt)
+{
+    ssize_t ret = 0;
+    Filter *filter = NULL;
+    NetFilterState *nf = NULL;
+    ssize_t size = iov_size(iov, iovcnt);
+
+    QTAILQ_FOREACH(filter, &nc->filters, next) {
+        nf = filter->nf;
+        if (nf->chain == chain || nf->chain == NET_FILTER_ALL) {
+            ret = nf->info->receive_iov(nf, sender, flags, iov, iovcnt);
+            if (ret == size) {
+                return ret;
+            }
+        }
+    }
+
+    return ret;
+}
+
+static ssize_t filter_receive(NetClientState *nc, int chain,
+                              NetClientState *sender,
+                              unsigned flags,
+                              const uint8_t *data,
+                              size_t size)
+{
+    struct iovec iov = {
+        .iov_base = (void *)data,
+        .iov_len = size
+    };
+
+    return filter_receive_iov(nc, chain, sender, flags, &iov, 1);
+}
+
 ssize_t qemu_deliver_packet(NetClientState *sender,
                             unsigned flags,
                             const uint8_t *data,
@@ -672,6 +711,7 @@ static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
                                                  NetPacketSent *sent_cb)
 {
     NetQueue *queue;
+    int ret;
 
 #ifdef DEBUG_NET
     printf("qemu_send_packet_async:\n");
@@ -682,6 +722,18 @@ static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
         return size;
     }
 
+    /* Let filters handle the packet first */
+    ret = filter_receive(sender, NET_FILTER_OUT, sender, flags, buf, size);
+    if (ret == size) {
+        return size;
+    }
+
+    ret = filter_receive(sender->peer, NET_FILTER_IN,
+                         sender, flags, buf, size);
+    if (ret == size) {
+        return size;
+    }
+
     queue = sender->peer->incoming_queue;
 
     return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
@@ -752,9 +804,24 @@ ssize_t qemu_sendv_packet_async(NetClientState *sender,
                                 NetPacketSent *sent_cb)
 {
     NetQueue *queue;
+    int size = iov_size(iov, iovcnt);
+    int ret;
 
     if (sender->link_down || !sender->peer) {
-        return iov_size(iov, iovcnt);
+        return size;
+    }
+
+    /* Let filters handle the packet first */
+    ret = filter_receive_iov(sender, NET_FILTER_OUT,
+                             sender, QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt);
+    if (ret == size) {
+        return size;
+    }
+
+    ret = filter_receive_iov(sender->peer, NET_FILTER_IN,
+                             sender, QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt);
+    if (ret == size) {
+        return size;
     }
 
     queue = sender->peer->incoming_queue;
Signature · 1 line
1.9.1
#11 lore raw

PATCH v3 7/12 netfilter: add an API to pass the packet to next filter

In reply to a message not in the archive

add an API qemu_netfilter_pass_to_next_iov() to pass the packet to next filter, and a wrapper qemu_netfilter_pass_to_next().

---
 include/net/filter.h | 12 ++++++++++++
 include/net/net.h    |  1 +
 net/filter.c         | 31 +++++++++++++++++++++++++++++++
 net/net.c            | 13 +++++++++++++
 4 files changed, 57 insertions(+)
include/net/filter.h +12 -0
index 7f0c949..c2be970 100644
@@ -53,4 +53,16 @@ void qemu_del_net_filter(NetFilterState *nf);
 void netfilter_add(QemuOpts *opts, Error **errp);
 void qmp_netfilter_add(QDict *qdict, QObject **ret, Error **errp);
 
+/* pass the packet to the next filter */
+void qemu_netfilter_pass_to_next_iov(NetFilterState *nf,
+                                     NetClientState *sender,
+                                     unsigned flags,
+                                     const struct iovec *iov,
+                                     int iovcnt);
+void qemu_netfilter_pass_to_next(NetFilterState *nf,
+                                 NetClientState *sender,
+                                 unsigned flags,
+                                 const uint8_t *data,
+                                 size_t size);
+
 #endif /* QEMU_NET_FILTER_H */
include/net/net.h +1 -0
index 5c5c109..d3bfe12 100644
@@ -117,6 +117,7 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
                                     const char *name);
 int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf);
 void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf);
+NetFilterState *qemu_netdev_next_filter(NetClientState *nc, NetFilterState *nf);
 NICState *qemu_new_nic(NetClientInfo *info,
                        NICConf *conf,
                        const char *model,
net/filter.c +31 -0
index bf113e9..84845b1 100644
@@ -131,6 +131,37 @@ void qmp_netfilter_del(const char *id, Error **errp)
     qemu_opts_del(opts);
 }
 
+void qemu_netfilter_pass_to_next_iov(NetFilterState *nf,
+                                     NetClientState *sender,
+                                     unsigned flags,
+                                     const struct iovec *iov,
+                                     int iovcnt)
+{
+    NetFilterState *next = qemu_netdev_next_filter(nf->netdev, nf);
+
+    while (next) {
+        if (next->chain == nf->chain || next->chain == NET_FILTER_ALL) {
+            next->info->receive_iov(next, sender, flags, iov, iovcnt);
+            return;
+        }
+        next = qemu_netdev_next_filter(next->netdev, next);
+    }
+}
+
+void qemu_netfilter_pass_to_next(NetFilterState *nf,
+                                 NetClientState *sender,
+                                 unsigned flags,
+                                 const uint8_t *data,
+                                 size_t size)
+{
+    struct iovec iov = {
+        .iov_base = (void *)data,
+        .iov_len = size
+    };
+
+    return qemu_netfilter_pass_to_next_iov(nf, sender, flags, &iov, 1);
+}
+
 typedef int (NetFilterInit)(const NetFilterOptions *opts,
                             const char *name, int chain,
                             NetClientState *netdev, Error **errp);
net/net.c +13 -0
index f774c39..e087763 100644
@@ -339,6 +339,19 @@ void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf)
     remove_filter(nc, filter);
 }
 
+NetFilterState *qemu_netdev_next_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = NULL;
+
+    QTAILQ_FOREACH(filter, &nc->filters, next) {
+        if (filter->nf == nf) {
+            break;
+        }
+    }
+
+    return QTAILQ_NEXT(filter, next)->nf;
+}
+
 NICState *qemu_new_nic(NetClientInfo *info,
                        NICConf *conf,
                        const char *model,
Signature · 1 line
1.9.1
#12 lore raw

Re: [Qemu-devel] [PATCH v3 07/12] netfilter: add an API to pass the packet to next filter

Quoted 53 lines · Yang Hongyang

add an API qemu_netfilter_pass_to_next_iov() to pass the packet to next filter, and a wrapper qemu_netfilter_pass_to_next().

---
 include/net/filter.h | 12 ++++++++++++
 include/net/net.h    |  1 +
 net/filter.c         | 31 +++++++++++++++++++++++++++++++
 net/net.c            | 13 +++++++++++++
 4 files changed, 57 insertions(+)
include/net/filter.h +12 -0
index 7f0c949..c2be970 100644
@@ -53,4 +53,16 @@ void qemu_del_net_filter(NetFilterState *nf);
 void netfilter_add(QemuOpts *opts, Error **errp);
 void qmp_netfilter_add(QDict *qdict, QObject **ret, Error **errp);
 
+/* pass the packet to the next filter */
+void qemu_netfilter_pass_to_next_iov(NetFilterState *nf,
+                                     NetClientState *sender,
+                                     unsigned flags,
+                                     const struct iovec *iov,
+                                     int iovcnt);
+void qemu_netfilter_pass_to_next(NetFilterState *nf,
+                                 NetClientState *sender,
+                                 unsigned flags,
+                                 const uint8_t *data,
+                                 size_t size);
+
 #endif /* QEMU_NET_FILTER_H */
include/net/net.h +1 -0
index 5c5c109..d3bfe12 100644
@@ -117,6 +117,7 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
                                     const char *name);
 int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf);
 void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf);
+NetFilterState *qemu_netdev_next_filter(NetClientState *nc, NetFilterState *nf);
 NICState *qemu_new_nic(NetClientInfo *info,
                        NICConf *conf,
                        const char *model,
net/filter.c +1 -0
index bf113e9..84845b1 100644
@@ -131,6 +131,37 @@ void qmp_netfilter_del(const char *id, Error **errp)
     qemu_opts_del(opts);
 }
 
+void qemu_netfilter_pass_to_next_iov(NetFilterState *nf,

net/filter.c void qmp_netfilter_del(const char *id, Error **errp) · line 144

Quoted 10 lines
net/filter.c +10 -0
+                                     NetClientState *sender,
+                                     unsigned flags,
+                                     const struct iovec *iov,
+                                     int iovcnt)
+{
+    NetFilterState *next = qemu_netdev_next_filter(nf->netdev, nf);
+
+    while (next) {
+        if (next->chain == nf->chain || next->chain == NET_FILTER_ALL) {
+            next->info->receive_iov(next, sender, flags, iov, iovcnt);

If a packet is not held by one filter, we need pass it to next filter?

net/filter.c void qmp_netfilter_del(const char *id, Error **errp) · line 148

Quoted 4 lines
net/filter.c +4 -0
+            return;
+        }
+        next = qemu_netdev_next_filter(next->netdev, next);
+    }

If a packet has gone through all filters, we probably need to pass it the receiver's incoming queue.

net/filter.c void qmp_netfilter_del(const char *id, Error **errp) · line 149

Quoted 1 line
net/filter.c +1 -0
+}

Looks like no real user for this helper?

Quoted 25 lines
net/filter.c +15 -0
+
+void qemu_netfilter_pass_to_next(NetFilterState *nf,
+                                 NetClientState *sender,
+                                 unsigned flags,
+                                 const uint8_t *data,
+                                 size_t size)
+{
+    struct iovec iov = {
+        .iov_base = (void *)data,
+        .iov_len = size
+    };
+
+    return qemu_netfilter_pass_to_next_iov(nf, sender, flags, &iov, 1);
+}
+
 typedef int (NetFilterInit)(const NetFilterOptions *opts,
                             const char *name, int chain,
                             NetClientState *netdev, Error **errp);
net/net.c +0 -0
index f774c39..e087763 100644
@@ -339,6 +339,19 @@ void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf)
     remove_filter(nc, filter);
 }

net/net.c void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf) · line 350

Quoted 10 lines
net/net.c +9 -0
 
+NetFilterState *qemu_netdev_next_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = NULL;
+
+    QTAILQ_FOREACH(filter, &nc->filters, next) {
+        if (filter->nf == nf) {
+            break;
+        }
+    }

A little bit confused, can we just use net->next?

Quoted 7 lines
net/net.c +4 -0
+
+    return QTAILQ_NEXT(filter, next)->nf;
+}
+
 NICState *qemu_new_nic(NetClientInfo *info,
                        NICConf *conf,
                        const char *model,
#13 lore raw

Re: [Qemu-devel] [PATCH v3 07/12] netfilter: add an API to pass the packet to next filter

Quoted 55 lines · Jason Wang
Quoted 52 lines · Yang Hongyang

add an API qemu_netfilter_pass_to_next_iov() to pass the packet to next filter, and a wrapper qemu_netfilter_pass_to_next().

---
  include/net/filter.h | 12 ++++++++++++
  include/net/net.h    |  1 +
  net/filter.c         | 31 +++++++++++++++++++++++++++++++
  net/net.c            | 13 +++++++++++++
  4 files changed, 57 insertions(+)
include/net/filter.h +12 -0
index 7f0c949..c2be970 100644
@@ -53,4 +53,16 @@ void qemu_del_net_filter(NetFilterState *nf);
  void netfilter_add(QemuOpts *opts, Error **errp);
  void qmp_netfilter_add(QDict *qdict, QObject **ret, Error **errp);
 
+/* pass the packet to the next filter */
+void qemu_netfilter_pass_to_next_iov(NetFilterState *nf,
+                                     NetClientState *sender,
+                                     unsigned flags,
+                                     const struct iovec *iov,
+                                     int iovcnt);
+void qemu_netfilter_pass_to_next(NetFilterState *nf,
+                                 NetClientState *sender,
+                                 unsigned flags,
+                                 const uint8_t *data,
+                                 size_t size);
+
  #endif /* QEMU_NET_FILTER_H */
include/net/net.h +1 -0
index 5c5c109..d3bfe12 100644
@@ -117,6 +117,7 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
                                      const char *name);
  int qemu_netdev_add_filter(NetClientState *nc, NetFilterState *nf);
  void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf);
+NetFilterState *qemu_netdev_next_filter(NetClientState *nc, NetFilterState *nf);
  NICState *qemu_new_nic(NetClientInfo *info,
                         NICConf *conf,
                         const char *model,
net/filter.c +0 -0
index bf113e9..84845b1 100644
@@ -131,6 +131,37 @@ void qmp_netfilter_del(const char *id, Error **errp)
      qemu_opts_del(opts);
  }
 
Quoted 13 lines

net/filter.c void qmp_netfilter_del(const char *id, Error **errp) · line 144

Quoted 11 lines
net/filter.c +11 -0
+void qemu_netfilter_pass_to_next_iov(NetFilterState *nf,
+                                     NetClientState *sender,
+                                     unsigned flags,
+                                     const struct iovec *iov,
+                                     int iovcnt)
+{
+    NetFilterState *next = qemu_netdev_next_filter(nf->netdev, nf);
+
+    while (next) {
+        if (next->chain == nf->chain || next->chain == NET_FILTER_ALL) {
+            next->info->receive_iov(next, sender, flags, iov, iovcnt);

If a packet is not held by one filter, we need pass it to next filter?

Yes, will fix, thank you.

Quoted 8 lines

net/filter.c void qmp_netfilter_del(const char *id, Error **errp) · line 148

Quoted 5 lines
net/filter.c +4 -0
+            return;
+        }
+        next = qemu_netdev_next_filter(next->netdev, next);
+    }

If a packet has gone through all filters, we probably need to pass it the receiver's incoming queue.

Ok, will do, thanks.

Quoted 4 lines

net/filter.c void qmp_netfilter_del(const char *id, Error **errp) · line 149

Quoted 2 lines
net/filter.c +1 -0
+}

Looks like no real user for this helper?

No real user in this series, I see your suggestion on the buffer filter, will use this api in that filter, thank you.

Quoted 27 lines
Quoted 27 lines
net/filter.c +15 -0
+
+void qemu_netfilter_pass_to_next(NetFilterState *nf,
+                                 NetClientState *sender,
+                                 unsigned flags,
+                                 const uint8_t *data,
+                                 size_t size)
+{
+    struct iovec iov = {
+        .iov_base = (void *)data,
+        .iov_len = size
+    };
+
+    return qemu_netfilter_pass_to_next_iov(nf, sender, flags, &iov, 1);
+}
+
  typedef int (NetFilterInit)(const NetFilterOptions *opts,
                              const char *name, int chain,
                              NetClientState *netdev, Error **errp);
net/net.c +0 -0
index f774c39..e087763 100644
@@ -339,6 +339,19 @@ void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf)
      remove_filter(nc, filter);
  }
 
Quoted 11 lines

net/net.c void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf) · line 350

Quoted 9 lines
net/net.c +9 -0
+NetFilterState *qemu_netdev_next_filter(NetClientState *nc, NetFilterState *nf)
+{
+    Filter *filter = NULL;
+
+    QTAILQ_FOREACH(filter, &nc->filters, next) {
+        if (filter->nf == nf) {
+            break;
+        }
+    }

A little bit confused, can we just use net->next?

it's because we use struct Filter to manage filters attached to a netdev, not struct NetFilterState.

Quoted 11 lines

net/net.c void qemu_netdev_remove_filter(NetClientState *nc, NetFilterState *nf) · line 357

Quoted 8 lines
net/net.c +4 -0
+
+    return QTAILQ_NEXT(filter, next)->nf;
+}
+
  NICState *qemu_new_nic(NetClientInfo *info,
                         NICConf *conf,
                         const char *model,

.

Signature · 2 lines
Thanks,
Yang.
#14 lore raw

PATCH v3 8/12 net/queue: export qemu_net_queue_append_iov

In reply to a message not in the archive
---
 include/net/queue.h |  7 +++++++
 net/queue.c         | 12 ++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
include/net/queue.h +7 -0
index fc02b33..f10fab0 100644
@@ -36,6 +36,13 @@ typedef void (NetPacketSent) (NetClientState *sender, ssize_t ret);
 
 NetQueue *qemu_new_net_queue(void *opaque);
 
+void qemu_net_queue_append_iov(NetQueue *queue,
+                               NetClientState *sender,
+                               unsigned flags,
+                               const struct iovec *iov,
+                               int iovcnt,
+                               NetPacketSent *sent_cb);
+
 void qemu_del_net_queue(NetQueue *queue);
 
 ssize_t qemu_net_queue_send(NetQueue *queue,
net/queue.c +6 -6
index ebbe2bb..c34a3e0 100644
@@ -110,12 +110,12 @@ static void qemu_net_queue_append(NetQueue *queue,
     QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
 }
 
-static void qemu_net_queue_append_iov(NetQueue *queue,
-                                      NetClientState *sender,
-                                      unsigned flags,
-                                      const struct iovec *iov,
-                                      int iovcnt,
-                                      NetPacketSent *sent_cb)
+void qemu_net_queue_append_iov(NetQueue *queue,
+                               NetClientState *sender,
+                               unsigned flags,
+                               const struct iovec *iov,
+                               int iovcnt,
+                               NetPacketSent *sent_cb)
 {
     NetPacket *packet;
     size_t max_len = 0;
Signature · 1 line
1.9.1
#15 lore raw

PATCH v3 9/12 move out net queue structs define

In reply to a message not in the archive
---
 include/net/queue.h | 19 +++++++++++++++++++
 net/queue.c         | 19 -------------------
 2 files changed, 19 insertions(+), 19 deletions(-)
include/net/queue.h +19 -0
index f10fab0..e139cc7 100644
@@ -31,6 +31,25 @@ typedef struct NetQueue NetQueue;
 
 typedef void (NetPacketSent) (NetClientState *sender, ssize_t ret);
 
+struct NetPacket {
+    QTAILQ_ENTRY(NetPacket) entry;
+    NetClientState *sender;
+    unsigned flags;
+    int size;
+    NetPacketSent *sent_cb;
+    uint8_t data[0];
+};
+
+struct NetQueue {
+    void *opaque;
+    uint32_t nq_maxlen;
+    uint32_t nq_count;
+
+    QTAILQ_HEAD(packets, NetPacket) packets;
+
+    unsigned delivering:1;
+};
+
 #define QEMU_NET_PACKET_FLAG_NONE  0
 #define QEMU_NET_PACKET_FLAG_RAW  (1<<0)
 
net/queue.c +0 -19
index c34a3e0..428fdd6 100644
@@ -39,25 +39,6 @@
  * unbounded queueing.
  */
 
-struct NetPacket {
-    QTAILQ_ENTRY(NetPacket) entry;
-    NetClientState *sender;
-    unsigned flags;
-    int size;
-    NetPacketSent *sent_cb;
-    uint8_t data[0];
-};
-
-struct NetQueue {
-    void *opaque;
-    uint32_t nq_maxlen;
-    uint32_t nq_count;
-
-    QTAILQ_HEAD(packets, NetPacket) packets;
-
-    unsigned delivering : 1;
-};
-
 NetQueue *qemu_new_net_queue(void *opaque)
 {
     NetQueue *queue;
Signature · 1 line
1.9.1
#16 lore raw

PATCH v3 10/12 netfilter: add a netbuffer filter

In reply to a message not in the archive

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Usage:
-netdev tap,id=bn0
-netfilter buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

---
v3: check packet's sender and sender->peer when flush it

---
 net/Makefile.objs   |   1 +
 net/filter-buffer.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/filter.c        |   2 +
 net/filters.h       |  17 ++++++
 qapi-schema.json    |  18 +++++-
 5 files changed, 199 insertions(+), 1 deletion(-)
 create mode 100644 net/filter-buffer.c
 create mode 100644 net/filters.h
net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
 common-obj-$(CONFIG_VDE) += vde.o
 common-obj-$(CONFIG_NETMAP) += netmap.o
 common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +162 -0

162 lines not on this page · raw message

net/filter.c +2 -0
index 84845b1..ba78683 100644
@@ -17,6 +17,7 @@
 
 #include "net/filter.h"
 #include "net/net.h"
+#include "filters.h"
 
 static QTAILQ_HEAD(, NetFilterState) net_filters;
 
@@ -168,6 +169,7 @@ typedef int (NetFilterInit)(const NetFilterOptions *opts,
 
 static
 NetFilterInit * const net_filter_init_fun[NET_FILTER_OPTIONS_KIND_MAX] = {
+    [NET_FILTER_OPTIONS_KIND_BUFFER] = net_init_filter_buffer,
 };
 
 static int net_filter_init1(const NetFilter *netfilter, Error **errp)
net/filters.h new file +17 -0
new file mode 100644
index 0000000..3b546db
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTERS_H
+#define QEMU_NET_FILTERS_H
+
+#include "net/net.h"
+#include "net/filter.h"
+
+int net_init_filter_buffer(const NetFilterOptions *opts, const char *name,
+                           int chain, NetClientState *netdev, Error **errp);
+
+#endif /* QEMU_NET_FILTERS_H */
qapi-schema.json +17 -1
index 9d97c21..e51bb59 100644
@@ -2584,6 +2584,21 @@
 { 'command': 'netfilter_del', 'data': {'id': 'str'} }
 
 ##
+# @NetFilterBufferOptions
+#
+# a netbuffer filter for network backend.
+#
+# @interval: #optional release packets by interval, if no interval supplied,
+#            will release packets when filter_buffer_release_all been called.
+#            scale: microsecond
+#
+# Since 2.5
+##
+{ 'struct': 'NetFilterBufferOptions',
+  'data': {
+    '*interval':     'int64' } }
+
+##
 # @NetFilterOptions
 #
 # A discriminated record of network filters.
@@ -2592,7 +2607,8 @@
 #
 ##
 { 'union': 'NetFilterOptions',
-  'data': { } }
+  'data': {
+    'buffer':     'NetFilterBufferOptions'} }
 
 ##
 # @NetFilter
Signature · 1 line
1.9.1
#17 lore raw

Re: [Qemu-devel] [PATCH v3 10/12] netfilter: add a netbuffer filter

Quoted 88 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Usage:
-netdev tap,id=bn0
-netfilter buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

---
v3: check packet's sender and sender->peer when flush it

---
 net/Makefile.objs   |   1 +
 net/filter-buffer.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/filter.c        |   2 +
 net/filters.h       |  17 ++++++
 qapi-schema.json    |  18 +++++-
 5 files changed, 199 insertions(+), 1 deletion(-)
 create mode 100644 net/filter-buffer.c
 create mode 100644 net/filters.h
net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
 common-obj-$(CONFIG_VDE) += vde.o
 common-obj-$(CONFIG_NETMAP) += netmap.o
 common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +47 -0
new file mode 100644
index 0000000..1547765
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * 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 "net/filter.h"
+#include "net/queue.h"
+#include "filters.h"
+#include "qemu-common.h"
+#include "qemu/error-report.h"
+#include "qemu/main-loop.h"
+#include "qemu/timer.h"
+#include "qemu/iov.h"
+
+typedef struct FILTERBUFFERState {
+    NetFilterState nf;
+    NetQueue *incoming_queue;
+    NetQueue *inflight_queue;
+    QEMUBH *flush_bh;
+    int64_t interval;
+    QEMUTimer release_timer;
+} FILTERBUFFERState;
+
+static void packet_send_completed(NetClientState *nc, ssize_t len)
+{
+    return;
+}
+
+static void filter_buffer_flush(NetFilterState *nf)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+    NetQueue *queue = s->inflight_queue;
+    NetPacket *packet;
+
+    while (queue && !QTAILQ_EMPTY(&queue->packets)) {
+        packet = QTAILQ_FIRST(&queue->packets);
+        QTAILQ_REMOVE(&queue->packets, packet, entry);
+        queue->nq_count--;
+
+        if (packet->sender && packet->sender->peer) {
+            qemu_net_queue_send(packet->sender->peer->incoming_queue,
+                                packet->sender,
+                                packet->flags,
+                                packet->data,

net/filter-buffer.c line 57

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+                                packet->size,
+                                packet->sent_cb);
+        }
+
+        /*
+         * now that we pass the packet to sender->peer->incoming_queue, we
+         * don't care the reture value here, because the peer's queue will
+         * take care of this packet
+         */
+        g_free(packet);

So looks like the packet was still not passed to next filter?

Quoted 14 lines
net/filter-buffer.c new file +14 -0
+    }
+
+    g_free(queue);
+    s->inflight_queue = NULL;
+}
+
+static void filter_buffer_flush_bh(void *opaque)
+{
+    FILTERBUFFERState *s = opaque;
+    NetFilterState *nf = &s->nf;
+    filter_buffer_flush(nf);
+}
+
+static void filter_buffer_release_one(NetFilterState *nf)

net/filter-buffer.c line 81

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+
+    /* flush inflight packets */
+    if (s->inflight_queue) {
+        filter_buffer_flush(nf);
+    }
+
+    s->inflight_queue = s->incoming_queue;
+    s->incoming_queue = qemu_new_net_queue(nf);

So this in fact flush a brunch of packets. If yes, the name of function is confusing

net/filter-buffer.c line 82

Quoted 1 line
net/filter-buffer.c new file +1 -0
+    qemu_bh_schedule(s->flush_bh);

Don't get why a bh is needed. If we could get rid of it, there's probably no need for inflight_queue, and we can just drain incoming_queue here.

Quoted 159 lines
net/filter-buffer.c new file +80 -0
+}
+
+static void filter_buffer_release_timer(void *opaque)
+{
+    FILTERBUFFERState *s = opaque;
+    filter_buffer_release_one(&s->nf);
+    timer_mod(&s->release_timer,
+              qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
+}
+
+/* filter APIs */
+static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
+                                         NetClientState *sender,
+                                         unsigned flags,
+                                         const struct iovec *iov,
+                                         int iovcnt)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+    NetQueue *queue = s->incoming_queue;
+
+    qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt,
+                              packet_send_completed);
+    return iov_size(iov, iovcnt);
+}
+
+static void filter_buffer_cleanup(NetFilterState *nf)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+
+    if (s->interval) {
+        timer_del(&s->release_timer);
+    }
+
+    /* flush inflight packets */
+    filter_buffer_flush(nf);
+    /* flush incoming packets */
+    s->inflight_queue = s->incoming_queue;
+    s->incoming_queue = NULL;
+    filter_buffer_flush(nf);
+
+    if (s->flush_bh) {
+        qemu_bh_delete(s->flush_bh);
+        s->flush_bh = NULL;
+    }
+    return;
+}
+
+
+static NetFilterInfo net_filter_buffer_info = {
+    .type = NET_FILTER_OPTIONS_KIND_BUFFER,
+    .size = sizeof(FILTERBUFFERState),
+    .receive_iov = filter_buffer_receive_iov,
+    .cleanup = filter_buffer_cleanup,
+};
+
+int net_init_filter_buffer(const NetFilterOptions *opts, const char *name,
+                           int chain, NetClientState *netdev, Error **errp)
+{
+    NetFilterState *nf;
+    FILTERBUFFERState *s;
+    const NetFilterBufferOptions *bufferopt;
+
+    assert(opts->kind == NET_FILTER_OPTIONS_KIND_BUFFER);
+    bufferopt = opts->buffer;
+
+    nf = qemu_new_net_filter(&net_filter_buffer_info,
+                             netdev, "buffer", name, chain);
+    s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+    s->incoming_queue = qemu_new_net_queue(nf);
+    s->flush_bh = qemu_bh_new(filter_buffer_flush_bh, s);
+    s->interval = bufferopt->has_interval ? bufferopt->interval : 0;
+    if (s->interval) {
+        timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
+                      filter_buffer_release_timer, s);
+        timer_mod(&s->release_timer,
+                  qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
+    }
+
+    return 0;
+}
net/filter.c +2 -0
index 84845b1..ba78683 100644
@@ -17,6 +17,7 @@
 
 #include "net/filter.h"
 #include "net/net.h"
+#include "filters.h"
 
 static QTAILQ_HEAD(, NetFilterState) net_filters;
 
@@ -168,6 +169,7 @@ typedef int (NetFilterInit)(const NetFilterOptions *opts,
 
 static
 NetFilterInit * const net_filter_init_fun[NET_FILTER_OPTIONS_KIND_MAX] = {
+    [NET_FILTER_OPTIONS_KIND_BUFFER] = net_init_filter_buffer,
 };
 
 static int net_filter_init1(const NetFilter *netfilter, Error **errp)
net/filters.h new file +17 -0
new file mode 100644
index 0000000..3b546db
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTERS_H
+#define QEMU_NET_FILTERS_H
+
+#include "net/net.h"
+#include "net/filter.h"
+
+int net_init_filter_buffer(const NetFilterOptions *opts, const char *name,
+                           int chain, NetClientState *netdev, Error **errp);
+
+#endif /* QEMU_NET_FILTERS_H */
qapi-schema.json +17 -1
index 9d97c21..e51bb59 100644
@@ -2584,6 +2584,21 @@
 { 'command': 'netfilter_del', 'data': {'id': 'str'} }
 
 ##
+# @NetFilterBufferOptions
+#
+# a netbuffer filter for network backend.
+#
+# @interval: #optional release packets by interval, if no interval supplied,
+#            will release packets when filter_buffer_release_all been called.
+#            scale: microsecond
+#
+# Since 2.5
+##
+{ 'struct': 'NetFilterBufferOptions',
+  'data': {
+    '*interval':     'int64' } }
+
+##
 # @NetFilterOptions
 #
 # A discriminated record of network filters.
@@ -2592,7 +2607,8 @@
 #
 ##
 { 'union': 'NetFilterOptions',
-  'data': { } }
+  'data': {
+    'buffer':     'NetFilterBufferOptions'} }
 
 ##
 # @NetFilter
#18 lore raw

Re: [Qemu-devel] [PATCH v3 10/12] netfilter: add a netbuffer filter

Quoted 95 lines · Jason Wang
Quoted 92 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Usage:
-netdev tap,id=bn0
-netfilter buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

---
v3: check packet's sender and sender->peer when flush it

---
  net/Makefile.objs   |   1 +
  net/filter-buffer.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  net/filter.c        |   2 +
  net/filters.h       |  17 ++++++
  qapi-schema.json    |  18 +++++-
  5 files changed, 199 insertions(+), 1 deletion(-)

create mode 100644 net/filter-buffer.c
create mode 100644 net/filters.h

net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
  common-obj-$(CONFIG_VDE) += vde.o
  common-obj-$(CONFIG_NETMAP) += netmap.o
  common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +51 -0
new file mode 100644
index 0000000..1547765
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * 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 "net/filter.h"
+#include "net/queue.h"
+#include "filters.h"
+#include "qemu-common.h"
+#include "qemu/error-report.h"
+#include "qemu/main-loop.h"
+#include "qemu/timer.h"
+#include "qemu/iov.h"
+
+typedef struct FILTERBUFFERState {
+    NetFilterState nf;
+    NetQueue *incoming_queue;
+    NetQueue *inflight_queue;
+    QEMUBH *flush_bh;
+    int64_t interval;
+    QEMUTimer release_timer;
+} FILTERBUFFERState;
+
+static void packet_send_completed(NetClientState *nc, ssize_t len)
+{
+    return;
+}
+
+static void filter_buffer_flush(NetFilterState *nf)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+    NetQueue *queue = s->inflight_queue;
+    NetPacket *packet;
+
+    while (queue && !QTAILQ_EMPTY(&queue->packets)) {
+        packet = QTAILQ_FIRST(&queue->packets);
+        QTAILQ_REMOVE(&queue->packets, packet, entry);
+        queue->nq_count--;
+
+        if (packet->sender && packet->sender->peer) {
+            qemu_net_queue_send(packet->sender->peer->incoming_queue,
+                                packet->sender,
+                                packet->flags,
+                                packet->data,
+                                packet->size,
+                                packet->sent_cb);
+        }
+
Quoted 8 lines

net/filter-buffer.c line 57

Quoted 6 lines
net/filter-buffer.c new file +6 -0
+        /*
+         * now that we pass the packet to sender->peer->incoming_queue, we
+         * don't care the reture value here, because the peer's queue will
+         * take care of this packet
+         */
+        g_free(packet);

So looks like the packet was still not passed to next filter?

I didn't get your suggestion last time, sorry, will add it in next version. Just to confirm, do you mean we need to pass the packet to next filter instead of pass to the receiver's incoming_queue? and even if we pass to next filter, the check of sender and it's peer is still needed, because if there's no receiver, it's nonsense to pass it further?

Quoted 20 lines
Quoted 20 lines
net/filter-buffer.c new file +19 -0
+    }
+
+    g_free(queue);
+    s->inflight_queue = NULL;
+}
+
+static void filter_buffer_flush_bh(void *opaque)
+{
+    FILTERBUFFERState *s = opaque;
+    NetFilterState *nf = &s->nf;
+    filter_buffer_flush(nf);
+}
+
+static void filter_buffer_release_one(NetFilterState *nf)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+
+    /* flush inflight packets */
+    if (s->inflight_queue) {
Quoted 8 lines

net/filter-buffer.c line 81

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+        filter_buffer_flush(nf);
+    }
+
+    s->inflight_queue = s->incoming_queue;
+    s->incoming_queue = qemu_new_net_queue(nf);

So this in fact flush a brunch of packets. If yes, the name of function is confusing

maybe filter_buffer_release ?

Quoted 6 lines

net/filter-buffer.c line 82

Quoted 2 lines
net/filter-buffer.c new file +1 -0
+    qemu_bh_schedule(s->flush_bh);

Don't get why a bh is needed. If we could get rid of it, there's probably no need for inflight_queue, and we can just drain incoming_queue here.

Seems we can get rid of bh, will do, thanks.

Quoted 163 lines
Quoted 151 lines
net/filter-buffer.c new file +80 -0
+}
+
+static void filter_buffer_release_timer(void *opaque)
+{
+    FILTERBUFFERState *s = opaque;
+    filter_buffer_release_one(&s->nf);
+    timer_mod(&s->release_timer,
+              qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
+}
+
+/* filter APIs */
+static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
+                                         NetClientState *sender,
+                                         unsigned flags,
+                                         const struct iovec *iov,
+                                         int iovcnt)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+    NetQueue *queue = s->incoming_queue;
+
+    qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt,
+                              packet_send_completed);
+    return iov_size(iov, iovcnt);
+}
+
+static void filter_buffer_cleanup(NetFilterState *nf)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+
+    if (s->interval) {
+        timer_del(&s->release_timer);
+    }
+
+    /* flush inflight packets */
+    filter_buffer_flush(nf);
+    /* flush incoming packets */
+    s->inflight_queue = s->incoming_queue;
+    s->incoming_queue = NULL;
+    filter_buffer_flush(nf);
+
+    if (s->flush_bh) {
+        qemu_bh_delete(s->flush_bh);
+        s->flush_bh = NULL;
+    }
+    return;
+}
+
+
+static NetFilterInfo net_filter_buffer_info = {
+    .type = NET_FILTER_OPTIONS_KIND_BUFFER,
+    .size = sizeof(FILTERBUFFERState),
+    .receive_iov = filter_buffer_receive_iov,
+    .cleanup = filter_buffer_cleanup,
+};
+
+int net_init_filter_buffer(const NetFilterOptions *opts, const char *name,
+                           int chain, NetClientState *netdev, Error **errp)
+{
+    NetFilterState *nf;
+    FILTERBUFFERState *s;
+    const NetFilterBufferOptions *bufferopt;
+
+    assert(opts->kind == NET_FILTER_OPTIONS_KIND_BUFFER);
+    bufferopt = opts->buffer;
+
+    nf = qemu_new_net_filter(&net_filter_buffer_info,
+                             netdev, "buffer", name, chain);
+    s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+    s->incoming_queue = qemu_new_net_queue(nf);
+    s->flush_bh = qemu_bh_new(filter_buffer_flush_bh, s);
+    s->interval = bufferopt->has_interval ? bufferopt->interval : 0;
+    if (s->interval) {
+        timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
+                      filter_buffer_release_timer, s);
+        timer_mod(&s->release_timer,
+                  qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
+    }
+
+    return 0;
+}
net/filter.c +2 -0
index 84845b1..ba78683 100644
@@ -17,6 +17,7 @@
 
  #include "net/filter.h"
  #include "net/net.h"
+#include "filters.h"
 
  static QTAILQ_HEAD(, NetFilterState) net_filters;
 
@@ -168,6 +169,7 @@ typedef int (NetFilterInit)(const NetFilterOptions *opts,
 
  static
  NetFilterInit * const net_filter_init_fun[NET_FILTER_OPTIONS_KIND_MAX] = {
+    [NET_FILTER_OPTIONS_KIND_BUFFER] = net_init_filter_buffer,
  };
 
  static int net_filter_init1(const NetFilter *netfilter, Error **errp)
net/filters.h new file +17 -0
new file mode 100644
index 0000000..3b546db
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * later.  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_NET_FILTERS_H
+#define QEMU_NET_FILTERS_H
+
+#include "net/net.h"
+#include "net/filter.h"
+
+int net_init_filter_buffer(const NetFilterOptions *opts, const char *name,
+                           int chain, NetClientState *netdev, Error **errp);
+
+#endif /* QEMU_NET_FILTERS_H */
qapi-schema.json +15 -0
index 9d97c21..e51bb59 100644
@@ -2584,6 +2584,21 @@
  { 'command': 'netfilter_del', 'data': {'id': 'str'} }
 
  ##
+# @NetFilterBufferOptions
+#
+# a netbuffer filter for network backend.
+#
+# @interval: #optional release packets by interval, if no interval supplied,
+#            will release packets when filter_buffer_release_all been called.
+#            scale: microsecond
+#
+# Since 2.5
+##
+{ 'struct': 'NetFilterBufferOptions',
+  'data': {
+    '*interval':     'int64' } }
+
+##
  # @NetFilterOptions
  #
  # A discriminated record of network filters.

qapi-schema.json line 2614

Quoted 9 lines
qapi-schema.json +2 -1
@@ -2592,7 +2607,8 @@
  #
  ##
  { 'union': 'NetFilterOptions',
-  'data': { } }
+  'data': {
+    'buffer':     'NetFilterBufferOptions'} }
 
  ##
  # @NetFilter

.

Signature · 2 lines
Thanks,
Yang.
#19 lore raw

Re: [Qemu-devel] [PATCH v3 10/12] netfilter: add a netbuffer filter

Quoted 108 lines · Yang Hongyang
Quoted 98 lines · Jason Wang
Quoted 95 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Usage:
-netdev tap,id=bn0
-netfilter buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

---
v3: check packet's sender and sender->peer when flush it

---
  net/Makefile.objs   |   1 +
  net/filter-buffer.c | 162
++++++++++++++++++++++++++++++++++++++++++++++++++++
  net/filter.c        |   2 +
  net/filters.h       |  17 ++++++
  qapi-schema.json    |  18 +++++-
  5 files changed, 199 insertions(+), 1 deletion(-)

create mode 100644 net/filter-buffer.c
create mode 100644 net/filters.h

net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
  common-obj-$(CONFIG_VDE) += vde.o
  common-obj-$(CONFIG_NETMAP) += netmap.o
  common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +53 -0
new file mode 100644
index 0000000..1547765
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * 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 "net/filter.h"
+#include "net/queue.h"
+#include "filters.h"
+#include "qemu-common.h"
+#include "qemu/error-report.h"
+#include "qemu/main-loop.h"
+#include "qemu/timer.h"
+#include "qemu/iov.h"
+
+typedef struct FILTERBUFFERState {
+    NetFilterState nf;
+    NetQueue *incoming_queue;
+    NetQueue *inflight_queue;
+    QEMUBH *flush_bh;
+    int64_t interval;
+    QEMUTimer release_timer;
+} FILTERBUFFERState;
+
+static void packet_send_completed(NetClientState *nc, ssize_t len)
+{
+    return;
+}
+
+static void filter_buffer_flush(NetFilterState *nf)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+    NetQueue *queue = s->inflight_queue;
+    NetPacket *packet;
+
+    while (queue && !QTAILQ_EMPTY(&queue->packets)) {
+        packet = QTAILQ_FIRST(&queue->packets);
+        QTAILQ_REMOVE(&queue->packets, packet, entry);
+        queue->nq_count--;
+
+        if (packet->sender && packet->sender->peer) {
+            qemu_net_queue_send(packet->sender->peer->incoming_queue,
+                                packet->sender,
+                                packet->flags,
+                                packet->data,
+                                packet->size,
+                                packet->sent_cb);
+        }
+
+        /*
+         * now that we pass the packet to
Quoted 8 lines

net/filter-buffer.c line 57

Quoted 6 lines

sender->peer->incoming_queue, we
+ * don't care the reture value here, because the peer's
queue will

net/filter-buffer.c new file +3 -0
+         * take care of this packet
+         */
+        g_free(packet);

So looks like the packet was still not passed to next filter?

Quoted 4 lines

I didn't get your suggestion last time, sorry, will add it in next version.
Just to confirm, do you mean we need to pass the packet to next filter instead of pass to the receiver's incoming_queue?

Yes, consider you may have two filters. First is dump and second is buffer, I believe you still want to buffer the packet even if it has been dumped.

Quoted 3 lines

and even if we pass to next
filter, the check of sender and it's peer is still needed, because if there's no receiver, it's nonsense to pass it further?

Yes.

Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
net/filter-buffer.c new file +19 -0
+    }
+
+    g_free(queue);
+    s->inflight_queue = NULL;
+}
+
+static void filter_buffer_flush_bh(void *opaque)
+{
+    FILTERBUFFERState *s = opaque;
+    NetFilterState *nf = &s->nf;
+    filter_buffer_flush(nf);
+}
+
+static void filter_buffer_release_one(NetFilterState *nf)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+
+    /* flush inflight packets */
+    if (s->inflight_queue) {
Quoted 8 lines

net/filter-buffer.c line 81

Quoted 5 lines
net/filter-buffer.c new file +5 -0
+        filter_buffer_flush(nf);
+    }
+
+    s->inflight_queue = s->incoming_queue;
+    s->incoming_queue = qemu_new_net_queue(nf);

So this in fact flush a brunch of packets. If yes, the name of function is confusing

Quoted 2 lines

maybe filter_buffer_release ?

Right.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

+ qemu_bh_schedule(s->flush_bh);

Don't get why a bh is needed. If we could get rid of it, there's probably no need for inflight_queue, and we can just drain incoming_queue here.

Seems we can get rid of bh, will do, thanks.

#20 lore raw

Re: [Qemu-devel] [PATCH v3 10/12] netfilter: add a netbuffer filter

Quoted 120 lines · Jason Wang
Quoted 108 lines · Yang Hongyang
Quoted 98 lines · Jason Wang
Quoted 95 lines · Yang Hongyang

This filter is to buffer/release packets, this feature can be used when using MicroCheckpointing, or other Remus like VM FT solutions, you can also use it to simulate the network delay.
It has an interval option, if supplied, this filter will release packets by interval.

Usage:

   -netdev tap,id=bn0
   -netfilter buffer,id=f0,netdev=bn0,chain=in,interval=1000

NOTE:
the scale of interval is microsecond.

---
v3: check packet's sender and sender->peer when flush it

---
   net/Makefile.objs   |   1 +
   net/filter-buffer.c | 162
++++++++++++++++++++++++++++++++++++++++++++++++++++
   net/filter.c        |   2 +
   net/filters.h       |  17 ++++++
   qapi-schema.json    |  18 +++++-
   5 files changed, 199 insertions(+), 1 deletion(-)

create mode 100644 net/filter-buffer.c
create mode 100644 net/filters.h

net/Makefile.objs +1 -0
index 914aec0..5fa2f97 100644
@@ -14,3 +14,4 @@ common-obj-$(CONFIG_SLIRP) += slirp.o
   common-obj-$(CONFIG_VDE) += vde.o
   common-obj-$(CONFIG_NETMAP) += netmap.o
   common-obj-y += filter.o
+common-obj-y += filter-buffer.o
net/filter-buffer.c new file +53 -0
new file mode 100644
index 0000000..1547765
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2015 FUJITSU LIMITED
+ * Author: Yang Hongyang <[email protected]>
+ *
+ * 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 "net/filter.h"
+#include "net/queue.h"
+#include "filters.h"
+#include "qemu-common.h"
+#include "qemu/error-report.h"
+#include "qemu/main-loop.h"
+#include "qemu/timer.h"
+#include "qemu/iov.h"
+
+typedef struct FILTERBUFFERState {
+    NetFilterState nf;
+    NetQueue *incoming_queue;
+    NetQueue *inflight_queue;
+    QEMUBH *flush_bh;
+    int64_t interval;
+    QEMUTimer release_timer;
+} FILTERBUFFERState;
+
+static void packet_send_completed(NetClientState *nc, ssize_t len)
+{
+    return;
+}
+
+static void filter_buffer_flush(NetFilterState *nf)
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+    NetQueue *queue = s->inflight_queue;
+    NetPacket *packet;
+
+    while (queue && !QTAILQ_EMPTY(&queue->packets)) {
+        packet = QTAILQ_FIRST(&queue->packets);
+        QTAILQ_REMOVE(&queue->packets, packet, entry);
+        queue->nq_count--;
+
+        if (packet->sender && packet->sender->peer) {
+            qemu_net_queue_send(packet->sender->peer->incoming_queue,
+                                packet->sender,
+                                packet->flags,
+                                packet->data,
+                                packet->size,
+                                packet->sent_cb);
+        }
+
+        /*
+         * now that we pass the packet to
Quoted 8 lines

net/filter-buffer.c line 57

Quoted 6 lines

sender->peer->incoming_queue, we
+ * don't care the reture value here, because the peer's
queue will

net/filter-buffer.c new file +3 -0
+         * take care of this packet
+         */
+        g_free(packet);

So looks like the packet was still not passed to next filter?

Quoted 4 lines

I didn't get your suggestion last time, sorry, will add it in next version.
Just to confirm, do you mean we need to pass the packet to next filter instead of pass to the receiver's incoming_queue?

Yes, consider you may have two filters. First is dump and second is buffer, I believe you still want to buffer the packet even if it has been dumped.

Quoted 5 lines
Quoted 3 lines

and even if we pass to next
filter, the check of sender and it's peer is still needed, because if there's no receiver, it's nonsense to pass it further?

Yes.

thanks.

Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
Quoted 17 lines
net/filter-buffer.c new file +14 -0
+    }
+
+    g_free(queue);
+    s->inflight_queue = NULL;
+}
+
+static void filter_buffer_flush_bh(void *opaque)
+{
+    FILTERBUFFERState *s = opaque;
+    NetFilterState *nf = &s->nf;
+    filter_buffer_flush(nf);
+}
+
+static void filter_buffer_release_one(NetFilterState *nf)

net/filter-buffer.c line 81

Quoted 10 lines
net/filter-buffer.c new file +10 -0
+{
+    FILTERBUFFERState *s = DO_UPCAST(FILTERBUFFERState, nf, nf);
+
+    /* flush inflight packets */
+    if (s->inflight_queue) {
+        filter_buffer_flush(nf);
+    }
+
+    s->inflight_queue = s->incoming_queue;
+    s->incoming_queue = qemu_new_net_queue(nf);

So this in fact flush a brunch of packets. If yes, the name of function is confusing

Quoted 4 lines
Quoted 2 lines

maybe filter_buffer_release ?

Right.

By removing bh, this function can be dropped, just call filter_buffer_flush. thanks.

Quoted 12 lines
Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

+ qemu_bh_schedule(s->flush_bh);

Don't get why a bh is needed. If we could get rid of it, there's probably no need for inflight_queue, and we can just drain incoming_queue here.

Seems we can get rid of bh, will do, thanks.

.

Signature · 2 lines
Thanks,
Yang.
#21 lore raw

PATCH v3 11/12 filter/buffer: update command description and help

In reply to a message not in the archive

now that we have a buffer netfilter, update the command description and help.

CC Luiz Capitulino <[email protected]>
CC Markus Armbruster <[email protected]>
---
 hmp-commands.hx | 2 +-
 qemu-options.hx | 5 ++++-
 qmp-commands.hx | 2 +-
 3 files changed, 6 insertions(+), 3 deletions(-)
hmp-commands.hx +1 -1
index 902e2d1..63177a8 100644
@@ -1255,7 +1255,7 @@ ETEXI
     {
         .name       = "netfilter_add",
         .args_type  = "netfilter:O",
-        .params     = "[type],id=str,netdev=str[,chain=in|out|all,prop=value][,...]",
+        .params     = "[buffer],id=str,netdev=str[,chain=in|out|all,prop=value][,...]",
         .help       = "add netfilter",
         .mhandler.cmd = hmp_netfilter_add,
         .command_completion = netfilter_add_completion,
qemu-options.hx +4 -1
index 0d52d02..eeaf2a1 100644
@@ -1575,7 +1575,10 @@ DEF("net", HAS_ARG, QEMU_OPTION_net,
     "socket][,vlan=n][,option][,option][,...]\n"
     "                old way to initialize a host network interface\n"
     "                (use the -netdev option if possible instead)\n", QEMU_ARCH_ALL)
-DEF("netfilter", HAS_ARG, QEMU_OPTION_netfilter, "", QEMU_ARCH_ALL)
+DEF("netfilter", HAS_ARG, QEMU_OPTION_netfilter,
+    "-netfilter buffer,id=str,netdev=str[,chain=in|out|all,interval=n]\n"
+    "                buffer netdev in/out packets. if interval provided, will release\n"
+    "                packets by interval. interval scale: microsecond\n", QEMU_ARCH_ALL)
 STEXI
 @item -net nic[,vlan=@var{n}][,macaddr=@var{mac}][,model=@var{type}] [,name=@var{name}][,addr=@var{addr}][,vectors=@var{v}]
 @findex -net
qmp-commands.hx +1 -1
index 4f0dc98..9419a6f 100644
@@ -947,7 +947,7 @@ Arguments:
 Example:
 
 -> { "execute": "netfilter_add",
-                "arguments": { "type": "type", "id": "nf0",
+                "arguments": { "type": "buffer", "id": "nf0",
                                "netdev": "bn",
                                "chain": "in" } }
 <- { "return": {} }
Signature · 1 line
1.9.1
#22 lore raw

PATCH v3 12/12 tests: add test cases for netfilter object

In reply to a message not in the archive

Using qtest qmp interface to implement following cases:
1) add/remove netfilter
2) add a netfilter then delete the netdev
3) add/remove more than one netfilters
4) add more than one netfilters and then delete the netdev

---
 tests/.gitignore       |   1 +
 tests/Makefile         |   2 +
 tests/test-netfilter.c | 197 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 200 insertions(+)
 create mode 100644 tests/test-netfilter.c
tests/.gitignore +1 -0
index ccc92e4..395962b 100644
@@ -41,5 +41,6 @@ test-vmstate
 test-write-threshold
 test-x86-cpuid
 test-xbzrle
+test-netfilter
 *-test
 qapi-schema/*.test.*
tests/Makefile +2 -0
index 7494582..e39c7e6 100644
@@ -185,6 +185,7 @@ check-qtest-i386-y += tests/pc-cpu-test$(EXESUF)
 check-qtest-i386-y += tests/q35-test$(EXESUF)
 gcov-files-i386-y += hw/pci-host/q35.c
 check-qtest-i386-$(CONFIG_LINUX) += tests/vhost-user-test$(EXESUF)
+check-qtest-i386-y += tests/test-netfilter$(EXESUF)
 check-qtest-x86_64-y = $(check-qtest-i386-y)
 gcov-files-i386-y += i386-softmmu/hw/timer/mc146818rtc.c
 gcov-files-x86_64-y = $(subst i386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y))
@@ -409,6 +410,7 @@ tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o
 tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o
 tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o libqemuutil.a libqemustub.a
 tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(block-obj-y) libqemuutil.a libqemustub.a
+tests/test-netfilter$(EXESUF): tests/test-netfilter.o $(qtest-obj-y)
 
 ifeq ($(CONFIG_POSIX),y)
 LIBS += -lutil
tests/test-netfilter.c new file +197 -0

197 lines not on this page · raw message

Signature · 1 line
1.9.1