Browse Source
Add a netfilter object based on QOM. A netfilter is attached to a netdev, captures all network packets that pass through the netdev. When we delete the netdev, we also delete the netfilter object attached to it, because if the netdev is removed, the filter which attached to it is useless. Signed-off-by: Yang Hongyang <yanghy@cn.fujitsu.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Jason Wang <jasowang@redhat.com>pull/30/head
committed by
Jason Wang
7 changed files with 229 additions and 0 deletions
@ -0,0 +1,61 @@ |
|||
/*
|
|||
* Copyright (c) 2015 FUJITSU LIMITED |
|||
* Author: Yang Hongyang <yanghy@cn.fujitsu.com> |
|||
* |
|||
* This work is licensed under the terms of the GNU GPL, version 2 or |
|||
* later. See the COPYING file in the top-level directory. |
|||
*/ |
|||
|
|||
#ifndef QEMU_NET_FILTER_H |
|||
#define QEMU_NET_FILTER_H |
|||
|
|||
#include "qom/object.h" |
|||
#include "qemu-common.h" |
|||
#include "qemu/typedefs.h" |
|||
#include "net/queue.h" |
|||
|
|||
#define TYPE_NETFILTER "netfilter" |
|||
#define NETFILTER(obj) \ |
|||
OBJECT_CHECK(NetFilterState, (obj), TYPE_NETFILTER) |
|||
#define NETFILTER_GET_CLASS(obj) \ |
|||
OBJECT_GET_CLASS(NetFilterClass, (obj), TYPE_NETFILTER) |
|||
#define NETFILTER_CLASS(klass) \ |
|||
OBJECT_CLASS_CHECK(NetFilterClass, (klass), TYPE_NETFILTER) |
|||
|
|||
typedef void (FilterSetup) (NetFilterState *nf, Error **errp); |
|||
typedef void (FilterCleanup) (NetFilterState *nf); |
|||
/*
|
|||
* Return: |
|||
* 0: finished handling the packet, we should continue |
|||
* size: filter stolen this packet, we stop pass this packet further |
|||
*/ |
|||
typedef ssize_t (FilterReceiveIOV)(NetFilterState *nc, |
|||
NetClientState *sender, |
|||
unsigned flags, |
|||
const struct iovec *iov, |
|||
int iovcnt, |
|||
NetPacketSent *sent_cb); |
|||
|
|||
typedef struct NetFilterClass { |
|||
ObjectClass parent_class; |
|||
|
|||
/* optional */ |
|||
FilterSetup *setup; |
|||
FilterCleanup *cleanup; |
|||
/* mandatory */ |
|||
FilterReceiveIOV *receive_iov; |
|||
} NetFilterClass; |
|||
|
|||
|
|||
struct NetFilterState { |
|||
/* private */ |
|||
Object parent; |
|||
|
|||
/* protected */ |
|||
char *netdev_id; |
|||
NetClientState *netdev; |
|||
NetFilterDirection direction; |
|||
QTAILQ_ENTRY(NetFilterState) next; |
|||
}; |
|||
|
|||
#endif /* QEMU_NET_FILTER_H */ |
|||
@ -0,0 +1,138 @@ |
|||
/*
|
|||
* Copyright (c) 2015 FUJITSU LIMITED |
|||
* Author: Yang Hongyang <yanghy@cn.fujitsu.com> |
|||
* |
|||
* This work is licensed under the terms of the GNU GPL, version 2 or |
|||
* later. See the COPYING file in the top-level directory. |
|||
*/ |
|||
|
|||
#include "qemu-common.h" |
|||
#include "qapi/qmp/qerror.h" |
|||
#include "qemu/error-report.h" |
|||
|
|||
#include "net/filter.h" |
|||
#include "net/net.h" |
|||
#include "net/vhost_net.h" |
|||
#include "qom/object_interfaces.h" |
|||
|
|||
static char *netfilter_get_netdev_id(Object *obj, Error **errp) |
|||
{ |
|||
NetFilterState *nf = NETFILTER(obj); |
|||
|
|||
return g_strdup(nf->netdev_id); |
|||
} |
|||
|
|||
static void netfilter_set_netdev_id(Object *obj, const char *str, Error **errp) |
|||
{ |
|||
NetFilterState *nf = NETFILTER(obj); |
|||
|
|||
nf->netdev_id = g_strdup(str); |
|||
} |
|||
|
|||
static int netfilter_get_direction(Object *obj, Error **errp G_GNUC_UNUSED) |
|||
{ |
|||
NetFilterState *nf = NETFILTER(obj); |
|||
return nf->direction; |
|||
} |
|||
|
|||
static void netfilter_set_direction(Object *obj, int direction, Error **errp) |
|||
{ |
|||
NetFilterState *nf = NETFILTER(obj); |
|||
nf->direction = direction; |
|||
} |
|||
|
|||
static void netfilter_init(Object *obj) |
|||
{ |
|||
object_property_add_str(obj, "netdev", |
|||
netfilter_get_netdev_id, netfilter_set_netdev_id, |
|||
NULL); |
|||
object_property_add_enum(obj, "queue", "NetFilterDirection", |
|||
NetFilterDirection_lookup, |
|||
netfilter_get_direction, netfilter_set_direction, |
|||
NULL); |
|||
} |
|||
|
|||
static void netfilter_complete(UserCreatable *uc, Error **errp) |
|||
{ |
|||
NetFilterState *nf = NETFILTER(uc); |
|||
NetClientState *ncs[MAX_QUEUE_NUM]; |
|||
NetFilterClass *nfc = NETFILTER_GET_CLASS(uc); |
|||
int queues; |
|||
Error *local_err = NULL; |
|||
|
|||
if (!nf->netdev_id) { |
|||
error_setg(errp, "Parameter 'netdev' is required"); |
|||
return; |
|||
} |
|||
|
|||
queues = qemu_find_net_clients_except(nf->netdev_id, ncs, |
|||
NET_CLIENT_OPTIONS_KIND_NIC, |
|||
MAX_QUEUE_NUM); |
|||
if (queues < 1) { |
|||
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "netdev", |
|||
"a network backend id"); |
|||
return; |
|||
} else if (queues > 1) { |
|||
error_setg(errp, "multiqueue is not supported"); |
|||
return; |
|||
} |
|||
|
|||
if (get_vhost_net(ncs[0])) { |
|||
error_setg(errp, "Vhost is not supported"); |
|||
return; |
|||
} |
|||
|
|||
nf->netdev = ncs[0]; |
|||
|
|||
if (nfc->setup) { |
|||
nfc->setup(nf, &local_err); |
|||
if (local_err) { |
|||
error_propagate(errp, local_err); |
|||
return; |
|||
} |
|||
} |
|||
QTAILQ_INSERT_TAIL(&nf->netdev->filters, nf, next); |
|||
} |
|||
|
|||
static void netfilter_finalize(Object *obj) |
|||
{ |
|||
NetFilterState *nf = NETFILTER(obj); |
|||
NetFilterClass *nfc = NETFILTER_GET_CLASS(obj); |
|||
|
|||
if (nfc->cleanup) { |
|||
nfc->cleanup(nf); |
|||
} |
|||
|
|||
if (nf->netdev && !QTAILQ_EMPTY(&nf->netdev->filters)) { |
|||
QTAILQ_REMOVE(&nf->netdev->filters, nf, next); |
|||
} |
|||
} |
|||
|
|||
static void netfilter_class_init(ObjectClass *oc, void *data) |
|||
{ |
|||
UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); |
|||
|
|||
ucc->complete = netfilter_complete; |
|||
} |
|||
|
|||
static const TypeInfo netfilter_info = { |
|||
.name = TYPE_NETFILTER, |
|||
.parent = TYPE_OBJECT, |
|||
.abstract = true, |
|||
.class_size = sizeof(NetFilterClass), |
|||
.class_init = netfilter_class_init, |
|||
.instance_size = sizeof(NetFilterState), |
|||
.instance_init = netfilter_init, |
|||
.instance_finalize = netfilter_finalize, |
|||
.interfaces = (InterfaceInfo[]) { |
|||
{ TYPE_USER_CREATABLE }, |
|||
{ } |
|||
} |
|||
}; |
|||
|
|||
static void register_types(void) |
|||
{ |
|||
type_register_static(&netfilter_info); |
|||
} |
|||
|
|||
type_init(register_types); |
|||
Loading…
Reference in new issue