From patchwork Fri Feb 5 02:47:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhanghailiang X-Patchwork-Id: 8230491 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 9EC8FBEEE5 for ; Fri, 5 Feb 2016 02:49:27 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EF4EB20373 for ; Fri, 5 Feb 2016 02:49:26 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 37B3A20253 for ; Fri, 5 Feb 2016 02:49:26 +0000 (UTC) Received: from localhost ([::1]:45564 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aRWSb-00079r-N1 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 04 Feb 2016 21:49:25 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50405) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aRWRA-0004bM-2n for qemu-devel@nongnu.org; Thu, 04 Feb 2016 21:47:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aRWR6-0002m6-SJ for qemu-devel@nongnu.org; Thu, 04 Feb 2016 21:47:56 -0500 Received: from szxga01-in.huawei.com ([58.251.152.64]:32926) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aRWR6-0002lK-5U for qemu-devel@nongnu.org; Thu, 04 Feb 2016 21:47:52 -0500 Received: from 172.24.1.49 (EHLO szxeml431-hub.china.huawei.com) ([172.24.1.49]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DEF05960; Fri, 05 Feb 2016 10:47:37 +0800 (CST) Received: from localhost (10.177.24.212) by szxeml431-hub.china.huawei.com (10.82.67.208) with Microsoft SMTP Server id 14.3.235.1; Fri, 5 Feb 2016 10:47:25 +0800 From: zhanghailiang To: Date: Fri, 5 Feb 2016 10:47:02 +0800 Message-ID: <1454640424-12612-3-git-send-email-zhang.zhanghailiang@huawei.com> X-Mailer: git-send-email 1.9.0.msysgit.0 In-Reply-To: <1454640424-12612-1-git-send-email-zhang.zhanghailiang@huawei.com> References: <1454640424-12612-1-git-send-email-zhang.zhanghailiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.24.212] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.56B40D49.00E7, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 257fabf1c40d263c44ebb203c493b257 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 58.251.152.64 Cc: zhanghailiang , jasowang@redhat.com, dgilbert@redhat.com, zhangchen.fnst@cn.fujitsu.com, hongyang.yang@easystack.cn Subject: [Qemu-devel] [PATCH v4 2/4] net/filter: Introduce a helper to add a filter to the netdev X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP We add a new helper function netdev_add_default_filter(), this function can help adding a filter object to a netdev. Besides, we add a is_default member for struct NetFilterState to indicate whether the filter is default or not. Signed-off-by: zhanghailiang --- v4: - Rename netdev_add_filter() to netdev_add_default_filter() - Drop useless is_default parameter for netdev_add_default_filter() (Hongyang's suggestion) v3: - Use object_new_with_props() instead of object_create() (Daniel's suggestion) v2: - Re-implement netdev_add_filter() by re-using object_create() (Jason's suggestion) --- include/net/filter.h | 6 ++++++ net/filter.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/net/filter.h b/include/net/filter.h index af3c53c..b42f473 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 is_default; bool enabled; QTAILQ_ENTRY(NetFilterState) next; }; @@ -74,4 +75,9 @@ ssize_t qemu_netfilter_pass_to_next(NetClientState *sender, int iovcnt, void *opaque); +void netdev_add_default_filter(const char *netdev_id, + const char *filter_type, + const char *filter_id, + Error **errp); + #endif /* QEMU_NET_FILTER_H */ diff --git a/net/filter.c b/net/filter.c index 5551cf1..079dc4c 100644 --- a/net/filter.c +++ b/net/filter.c @@ -177,6 +177,7 @@ static void netfilter_init(Object *obj) * for netfilter will be enabled. */ nf->enabled = true; + nf->is_default = false; object_property_add_str(obj, "netdev", netfilter_get_netdev_id, netfilter_set_netdev_id, @@ -232,6 +233,46 @@ static void netfilter_complete(UserCreatable *uc, Error **errp) QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next); } +/* + * Attach a default filter to the netdev, the default + * filter will be disabled by default, and it will be + * used internally, the net packets will not pass through + * it before it is enabled. + */ +void netdev_add_default_filter(const char *netdev_id, + const char *filter_type, + const char *filter_id, + Error **errp) +{ + NetClientState *nc = qemu_find_netdev(netdev_id); + Object *filter; + NetFilterState *nf; + Error *local_err = NULL; + + /* FIXME: Not support multiple queues */ + if (!nc || nc->queue_index > 1) { + return; + } + /* Not support vhost-net */ + if (get_vhost_net(nc)) { + return; + } + + filter = object_new_with_props(filter_type, + object_get_objects_root(), + filter_id, + &local_err, + "netdev", netdev_id, + "status", "disable", + NULL); + if (local_err) { + error_propagate(errp, local_err); + return; + } + nf = NETFILTER(filter); + nf->is_default = true; +} + static void netfilter_finalize(Object *obj) { NetFilterState *nf = NETFILTER(obj);