On 2016/2/2 11:14, Yang Hongyang wrote:
On 02/01/2016 08:01 PM, zhanghailiang wrote:
We add a new helper function netdev_add_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.zhanghaili...@huawei.com>
---
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 | 7 +++++++
net/filter.c | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+)
diff --git a/include/net/filter.h b/include/net/filter.h
index af3c53c..661abef 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;
You added a member, but seems did not initialize it. although the
default behavior of a struct init is 0, a explicit is_defuault=false
is better IMO.
OK, i will fix it in next version.
bool enabled;
QTAILQ_ENTRY(NetFilterState) next;
};
@@ -74,4 +75,10 @@ ssize_t qemu_netfilter_pass_to_next(NetClientState *sender,
int iovcnt,
void *opaque);
+void netdev_add_filter(const char *netdev_id,
+ const char *filter_type,
+ const char *filter_id,
+ bool is_default,
+ Error **errp);
+
#endif /* QEMU_NET_FILTER_H */
diff --git a/net/filter.c b/net/filter.c
index d08a2be..7f9df57 100644
--- a/net/filter.c
+++ b/net/filter.c
@@ -214,6 +214,40 @@ static void netfilter_complete(UserCreatable *uc, Error
**errp)
QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next);
}
+void netdev_add_filter(const char *netdev_id,
+ const char *filter_type,
+ const char *filter_id,
+ bool is_default,
+ Error **errp)
We could assume all calls to this API is adding default filter.
so is_default is useness.
Make sense, i will rename this API to netdev_add_default_filter()
and drop the is_default parameter.
Thanks,
Hailiang
+{
+ 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", is_default ? "disable" : "enable",
+ NULL);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ return;
+ }
+ nf = NETFILTER(filter);
+ nf->is_default = is_default;
+}
+
static void netfilter_finalize(Object *obj)
{
NetFilterState *nf = NETFILTER(obj);