All of lore.kernel.org
 help / color / mirror / Atom feed
From: zhanghailiang <zhang.zhanghailiang@huawei.com>
To: qemu-devel@nongnu.org
Cc: zhanghailiang <zhang.zhanghailiang@huawei.com>,
	zhangchen.fnst@cn.fujitsu.com, jasowang@redhat.com,
	dgilbert@redhat.com, hongyang.yang@easystack.cn
Subject: [Qemu-devel] [PATCH v3 1/4] net/filter: Add a 'status' property for filter object
Date: Mon, 1 Feb 2016 20:01:14 +0800	[thread overview]
Message-ID: <1454328077-18820-2-git-send-email-zhang.zhanghailiang@huawei.com> (raw)
In-Reply-To: <1454328077-18820-1-git-send-email-zhang.zhanghailiang@huawei.com>

With this property, users can control if this filter is 'enable'
or 'disable'. The default behavior for filter is enabled.

We will skip the disabled filter when delivering packets in net layer.

Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
---
v2:
 - Squash previous patch 3 into this patch (Jason's suggestion)
---
 include/net/filter.h |  1 +
 net/filter.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/include/net/filter.h b/include/net/filter.h
index 5639976..af3c53c 100644
--- a/include/net/filter.h
+++ b/include/net/filter.h
@@ -55,6 +55,7 @@ struct NetFilterState {
     char *netdev_id;
     NetClientState *netdev;
     NetFilterDirection direction;
+    bool enabled;
     QTAILQ_ENTRY(NetFilterState) next;
 };
 
diff --git a/net/filter.c b/net/filter.c
index 5adcec8..d08a2be 100644
--- a/net/filter.c
+++ b/net/filter.c
@@ -16,6 +16,11 @@
 #include "qom/object_interfaces.h"
 #include "qemu/iov.h"
 
+static inline bool qemu_need_skip_netfilter(NetFilterState *nf)
+{
+    return nf->enabled ? false : true;
+}
+
 ssize_t qemu_netfilter_receive(NetFilterState *nf,
                                NetFilterDirection direction,
                                NetClientState *sender,
@@ -24,6 +29,10 @@ ssize_t qemu_netfilter_receive(NetFilterState *nf,
                                int iovcnt,
                                NetPacketSent *sent_cb)
 {
+    /* Don't go through the filter if it is disabled */
+    if (qemu_need_skip_netfilter(nf)) {
+        return 0;
+    }
     if (nf->direction == direction ||
         nf->direction == NET_FILTER_DIRECTION_ALL) {
         return NETFILTER_GET_CLASS(OBJECT(nf))->receive_iov(
@@ -116,8 +125,41 @@ static void netfilter_set_direction(Object *obj, int direction, Error **errp)
     nf->direction = direction;
 }
 
+static char *netfilter_get_status(Object *obj, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    if (nf->enabled) {
+        return g_strdup("enable");
+    } else {
+        return g_strdup("disable");
+    }
+}
+
+static void netfilter_set_status(Object *obj, const char *str, Error **errp)
+{
+    NetFilterState *nf = NETFILTER(obj);
+
+    if (!strcmp(str, "enable")) {
+        nf->enabled = true;
+    } else if (!strcmp(str, "disable")) {
+        nf->enabled = false;
+    } else {
+        error_setg(errp, "Invalid value for netfilter status, "
+                         "should be 'enable' or 'disable'");
+    }
+}
+
 static void netfilter_init(Object *obj)
 {
+    NetFilterState *nf = NETFILTER(obj);
+
+    /*
+    * If not configured with 'status' property, the default status
+    * for netfilter will be enabled.
+    */
+    nf->enabled = true;
+
     object_property_add_str(obj, "netdev",
                             netfilter_get_netdev_id, netfilter_set_netdev_id,
                             NULL);
@@ -125,6 +167,9 @@ static void netfilter_init(Object *obj)
                              NetFilterDirection_lookup,
                              netfilter_get_direction, netfilter_set_direction,
                              NULL);
+    object_property_add_str(obj, "status",
+                            netfilter_get_status, netfilter_set_status,
+                            NULL);
 }
 
 static void netfilter_complete(UserCreatable *uc, Error **errp)
-- 
1.8.3.1

  reply	other threads:[~2016-02-01 12:02 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-01 12:01 [Qemu-devel] [PATCH v3 0/4] Netfilter: Add each netdev a default filter zhanghailiang
2016-02-01 12:01 ` zhanghailiang [this message]
2016-02-01 12:01 ` [Qemu-devel] [PATCH v3 2/4] net/filter: Introduce a helper to add a filter to the netdev zhanghailiang
2016-02-02  3:14   ` Yang Hongyang
2016-02-05  2:24     ` Hailiang Zhang
2016-02-01 12:01 ` [Qemu-devel] [PATCH v3 3/4] filter-buffer: Accept zero interval zhanghailiang
2016-02-01 12:01 ` [Qemu-devel] [PATCH v3 4/4] net/filter: Add a default filter to each netdev zhanghailiang
2016-02-02  3:16   ` Yang Hongyang
2016-02-05  2:35     ` Hailiang Zhang
2016-02-02  3:11 ` [Qemu-devel] [PATCH v3 0/4] Netfilter: Add each netdev a default filter Yang Hongyang
2016-02-02 12:34   ` Hailiang Zhang

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=1454328077-18820-2-git-send-email-zhang.zhanghailiang@huawei.com \
    --to=zhang.zhanghailiang@huawei.com \
    --cc=dgilbert@redhat.com \
    --cc=hongyang.yang@easystack.cn \
    --cc=jasowang@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=zhangchen.fnst@cn.fujitsu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.