qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Yang Hongyang <yanghy@cn.fujitsu.com>
To: qemu-devel@nongnu.org
Cc: thuth@redhat.com, zhang.zhanghailiang@huawei.com,
	jasowang@redhat.com, mrhines@linux.vnet.ibm.com,
	stefanha@redhat.com, Yang Hongyang <yanghy@cn.fujitsu.com>
Subject: [Qemu-devel] [PATCH 04/12] net: add/remove filters from network backend
Date: Wed, 29 Jul 2015 18:51:48 +0800	[thread overview]
Message-ID: <1438167116-29270-5-git-send-email-yanghy@cn.fujitsu.com> (raw)
In-Reply-To: <1438167116-29270-1-git-send-email-yanghy@cn.fujitsu.com>

add/remove filters from network backend

Signed-off-by: Yang Hongyang <yanghy@cn.fujitsu.com>
---
 include/net/net.h |  8 ++++++++
 net/filter.c      |  4 ++--
 net/net.c         | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/include/net/net.h b/include/net/net.h
index 6a6cbef..5c5c109 100644
--- a/include/net/net.h
+++ b/include/net/net.h
@@ -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,
diff --git a/net/filter.c b/net/filter.c
index 1685fbc..3d3b7bc 100644
--- a/net/filter.c
+++ b/net/filter.c
@@ -35,14 +35,14 @@ NetFilterState *qemu_new_net_filter(NetFilterInfo *info,
     nf->name = g_strdup(name);
     nf->netdev = netdev;
     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);
 
diff --git a/net/net.c b/net/net.c
index 28a5597..22748e0 100644
--- a/net/net.c
+++ b/net/net.c
@@ -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,
@@ -367,6 +400,8 @@ static void qemu_cleanup_net_client(NetClientState *nc)
 
 static void qemu_free_net_client(NetClientState *nc)
 {
+    Filter *filter, *next;
+
     if (nc->incoming_queue) {
         qemu_del_net_queue(nc->incoming_queue);
     }
@@ -375,6 +410,9 @@ static void qemu_free_net_client(NetClientState *nc)
     }
     g_free(nc->name);
     g_free(nc->model);
+    QTAILQ_FOREACH_SAFE(filter, &nc->filters, next, next) {
+        remove_filter(nc, filter);
+    }
     if (nc->destructor) {
         nc->destructor(nc);
     }
-- 
1.9.1

  parent reply	other threads:[~2015-07-29 11:07 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-29 10:51 [Qemu-devel] [PATCH 00/12] For QEMU 2.5: Add a netfilter object and netbuffer filter Yang Hongyang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 01/12] net: add a new object netfilter Yang Hongyang
2015-07-29 13:53   ` Thomas Huth
2015-07-29 14:05     ` Yang Hongyang
2015-07-29 14:20       ` Thomas Huth
2015-07-29 14:32         ` Yang Hongyang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 02/12] init/cleanup of netfilter object Yang Hongyang
2015-07-29 13:33   ` Thomas Huth
2015-07-29 13:50     ` Yang Hongyang
2015-07-29 13:58       ` Thomas Huth
2015-07-29 14:08         ` Yang Hongyang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 03/12] netfilter: add netfilter_{add|del} commands Yang Hongyang
2015-07-29 14:15   ` Thomas Huth
2015-07-29 14:28     ` Yang Hongyang
2015-07-29 14:30       ` Yang Hongyang
2015-07-29 10:51 ` Yang Hongyang [this message]
2015-07-29 10:51 ` [Qemu-devel] [PATCH 05/12] netfilter: hook packets before receive Yang Hongyang
2015-07-30  4:51   ` Jason Wang
2015-07-30  7:22     ` Yang Hongyang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 06/12] netfilter: provide a compat receive_iov Yang Hongyang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 07/12] net/queue: export qemu_net_queue_append Yang Hongyang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 08/12] move out net queue structs define Yang Hongyang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 09/12] netfilter: add a netbuffer filter Yang Hongyang
2015-07-30  1:45   ` Li Zhijian
2015-07-30  1:53     ` Yang Hongyang
2015-07-30  5:13   ` Jason Wang
2015-07-30  6:47     ` Yang Hongyang
2015-07-30  8:40       ` Jason Wang
2015-07-30  9:04         ` Yang Hongyang
2015-07-30  9:33           ` Jason Wang
2015-07-30  9:49             ` Yang Hongyang
2015-07-30 10:14               ` Jason Wang
2015-07-30 10:28                 ` Yang Hongyang
2015-07-30 14:16                   ` Thomas Huth
2015-07-30 15:00                     ` Yang Hongyang
2015-07-30 13:46                 ` Yang Hongyang
2015-07-30  7:00     ` Yang Hongyang
2015-07-30  8:52       ` Jason Wang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 10/12] netbuffer: add a public api filter_buffer_release_all Yang Hongyang
2015-07-30  5:25   ` Jason Wang
2015-07-30  5:50     ` Yang Hongyang
2015-07-30  8:42       ` Jason Wang
2015-07-30  8:53         ` Yang Hongyang
2015-07-30  8:50       ` Jason Wang
2015-07-30  9:06         ` Yang Hongyang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 11/12] filter/buffer: add an interval option to buffer filter Yang Hongyang
2015-07-30  5:27   ` Jason Wang
2015-07-30  5:37     ` Yang Hongyang
2015-07-30  8:53       ` Jason Wang
2015-07-30  9:12         ` Yang Hongyang
2015-07-29 10:51 ` [Qemu-devel] [PATCH 12/12] filter/buffer: update command description and help Yang Hongyang
2015-07-29 12:56 ` [Qemu-devel] [PATCH 00/12] For QEMU 2.5: Add a netfilter object and netbuffer filter Thomas Huth
2015-07-29 13:39   ` Yang Hongyang
2015-07-29 13:48     ` Thomas Huth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1438167116-29270-5-git-send-email-yanghy@cn.fujitsu.com \
    --to=yanghy@cn.fujitsu.com \
    --cc=jasowang@redhat.com \
    --cc=mrhines@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=thuth@redhat.com \
    --cc=zhang.zhanghailiang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).