2 * Copyright (c) 2015 FUJITSU LIMITED
3 * Author: Yang Hongyang <yanghy@cn.fujitsu.com>
5 * This work is licensed under the terms of the GNU GPL, version 2 or
6 * later. See the COPYING file in the top-level directory.
9 #include "qemu-common.h"
10 #include "qapi/qmp/qerror.h"
11 #include "qemu/error-report.h"
13 #include "net/filter.h"
15 #include "net/vhost_net.h"
16 #include "qom/object_interfaces.h"
18 #include "qapi/string-output-visitor.h"
20 ssize_t
qemu_netfilter_receive(NetFilterState
*nf
,
21 NetFilterDirection direction
,
22 NetClientState
*sender
,
24 const struct iovec
*iov
,
26 NetPacketSent
*sent_cb
)
28 if (nf
->direction
== direction
||
29 nf
->direction
== NET_FILTER_DIRECTION_ALL
) {
30 return NETFILTER_GET_CLASS(OBJECT(nf
))->receive_iov(
31 nf
, sender
, flags
, iov
, iovcnt
, sent_cb
);
37 ssize_t
qemu_netfilter_pass_to_next(NetClientState
*sender
,
39 const struct iovec
*iov
,
45 NetFilterState
*nf
= opaque
;
46 NetFilterState
*next
= QTAILQ_NEXT(nf
, next
);
48 if (!sender
|| !sender
->peer
) {
49 /* no receiver, or sender been deleted, no need to pass it further */
53 if (nf
->direction
== NET_FILTER_DIRECTION_ALL
) {
54 if (sender
== nf
->netdev
) {
55 /* This packet is sent by netdev itself */
56 direction
= NET_FILTER_DIRECTION_TX
;
58 direction
= NET_FILTER_DIRECTION_RX
;
61 direction
= nf
->direction
;
66 * if qemu_netfilter_pass_to_next been called, means that
67 * the packet has been hold by filter and has already retured size
68 * to the sender, so sent_cb shouldn't be called later, just
71 ret
= qemu_netfilter_receive(next
, direction
, sender
, flags
, iov
,
76 next
= QTAILQ_NEXT(next
, next
);
80 * We have gone through all filters, pass it to receiver.
81 * Do the valid check again incase sender or receiver been
82 * deleted while we go through filters.
84 if (sender
&& sender
->peer
) {
85 qemu_net_queue_send_iov(sender
->peer
->incoming_queue
,
86 sender
, flags
, iov
, iovcnt
, NULL
);
90 /* no receiver, or sender been deleted */
91 return iov_size(iov
, iovcnt
);
94 static char *netfilter_get_netdev_id(Object
*obj
, Error
**errp
)
96 NetFilterState
*nf
= NETFILTER(obj
);
98 return g_strdup(nf
->netdev_id
);
101 static void netfilter_set_netdev_id(Object
*obj
, const char *str
, Error
**errp
)
103 NetFilterState
*nf
= NETFILTER(obj
);
105 nf
->netdev_id
= g_strdup(str
);
108 static int netfilter_get_direction(Object
*obj
, Error
**errp G_GNUC_UNUSED
)
110 NetFilterState
*nf
= NETFILTER(obj
);
111 return nf
->direction
;
114 static void netfilter_set_direction(Object
*obj
, int direction
, Error
**errp
)
116 NetFilterState
*nf
= NETFILTER(obj
);
117 nf
->direction
= direction
;
120 static void netfilter_init(Object
*obj
)
122 object_property_add_str(obj
, "netdev",
123 netfilter_get_netdev_id
, netfilter_set_netdev_id
,
125 object_property_add_enum(obj
, "queue", "NetFilterDirection",
126 NetFilterDirection_lookup
,
127 netfilter_get_direction
, netfilter_set_direction
,
131 static void netfilter_complete(UserCreatable
*uc
, Error
**errp
)
133 NetFilterState
*nf
= NETFILTER(uc
);
134 NetClientState
*ncs
[MAX_QUEUE_NUM
];
135 NetFilterClass
*nfc
= NETFILTER_GET_CLASS(uc
);
137 Error
*local_err
= NULL
;
139 ObjectProperty
*prop
;
140 StringOutputVisitor
*ov
;
142 if (!nf
->netdev_id
) {
143 error_setg(errp
, "Parameter 'netdev' is required");
147 queues
= qemu_find_net_clients_except(nf
->netdev_id
, ncs
,
148 NET_CLIENT_OPTIONS_KIND_NIC
,
151 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "netdev",
152 "a network backend id");
154 } else if (queues
> 1) {
155 error_setg(errp
, "multiqueue is not supported");
159 if (get_vhost_net(ncs
[0])) {
160 error_setg(errp
, "Vhost is not supported");
167 nfc
->setup(nf
, &local_err
);
169 error_propagate(errp
, local_err
);
173 QTAILQ_INSERT_TAIL(&nf
->netdev
->filters
, nf
, next
);
175 /* generate info str */
176 QTAILQ_FOREACH(prop
, &OBJECT(nf
)->properties
, node
) {
177 if (!strcmp(prop
->name
, "type")) {
180 ov
= string_output_visitor_new(false);
181 object_property_get(OBJECT(nf
), string_output_get_visitor(ov
),
183 str
= string_output_get_string(ov
);
184 string_output_visitor_cleanup(ov
);
185 info
= g_strdup_printf(",%s=%s", prop
->name
, str
);
186 g_strlcat(nf
->info_str
, info
, sizeof(nf
->info_str
));
192 static void netfilter_finalize(Object
*obj
)
194 NetFilterState
*nf
= NETFILTER(obj
);
195 NetFilterClass
*nfc
= NETFILTER_GET_CLASS(obj
);
201 if (nf
->netdev
&& !QTAILQ_EMPTY(&nf
->netdev
->filters
)) {
202 QTAILQ_REMOVE(&nf
->netdev
->filters
, nf
, next
);
206 static void netfilter_class_init(ObjectClass
*oc
, void *data
)
208 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
210 ucc
->complete
= netfilter_complete
;
213 static const TypeInfo netfilter_info
= {
214 .name
= TYPE_NETFILTER
,
215 .parent
= TYPE_OBJECT
,
217 .class_size
= sizeof(NetFilterClass
),
218 .class_init
= netfilter_class_init
,
219 .instance_size
= sizeof(NetFilterState
),
220 .instance_init
= netfilter_init
,
221 .instance_finalize
= netfilter_finalize
,
222 .interfaces
= (InterfaceInfo
[]) {
223 { TYPE_USER_CREATABLE
},
228 static void register_types(void)
230 type_register_static(&netfilter_info
);
233 type_init(register_types
);