diff mbox

[v4,2/4] net/filter: Introduce a helper to add a filter to the netdev

Message ID 1454640424-12612-3-git-send-email-zhang.zhanghailiang@huawei.com (mailing list archive)
State New, archived
Headers show

Commit Message

Zhanghailiang Feb. 5, 2016, 2:47 a.m. UTC
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 <zhang.zhanghailiang@huawei.com>
---
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 mbox

Patch

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);