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/osdep.h"
10 #include "qapi/error.h"
11 #include "qemu-common.h"
12 #include "qapi/qmp/qerror.h"
13 #include "qemu/error-report.h"
15 #include "net/filter.h"
17 #include "net/vhost_net.h"
18 #include "qom/object_interfaces.h"
21 static inline bool qemu_can_skip_netfilter(NetFilterState
*nf
)
26 ssize_t
qemu_netfilter_receive(NetFilterState
*nf
,
27 NetFilterDirection direction
,
28 NetClientState
*sender
,
30 const struct iovec
*iov
,
32 NetPacketSent
*sent_cb
)
34 if (qemu_can_skip_netfilter(nf
)) {
37 if (nf
->direction
== direction
||
38 nf
->direction
== NET_FILTER_DIRECTION_ALL
) {
39 return NETFILTER_GET_CLASS(OBJECT(nf
))->receive_iov(
40 nf
, sender
, flags
, iov
, iovcnt
, sent_cb
);
46 static NetFilterState
*netfilter_next(NetFilterState
*nf
,
47 NetFilterDirection dir
)
51 if (dir
== NET_FILTER_DIRECTION_TX
) {
52 /* forward walk through filters */
53 next
= QTAILQ_NEXT(nf
, next
);
56 next
= QTAILQ_PREV(nf
, NetFilterHead
, next
);
62 ssize_t
qemu_netfilter_pass_to_next(NetClientState
*sender
,
64 const struct iovec
*iov
,
70 NetFilterState
*nf
= opaque
;
71 NetFilterState
*next
= NULL
;
73 if (!sender
|| !sender
->peer
) {
74 /* no receiver, or sender been deleted, no need to pass it further */
78 if (nf
->direction
== NET_FILTER_DIRECTION_ALL
) {
79 if (sender
== nf
->netdev
) {
80 /* This packet is sent by netdev itself */
81 direction
= NET_FILTER_DIRECTION_TX
;
83 direction
= NET_FILTER_DIRECTION_RX
;
86 direction
= nf
->direction
;
89 next
= netfilter_next(nf
, direction
);
92 * if qemu_netfilter_pass_to_next been called, means that
93 * the packet has been hold by filter and has already retured size
94 * to the sender, so sent_cb shouldn't be called later, just
97 ret
= qemu_netfilter_receive(next
, direction
, sender
, flags
, iov
,
102 next
= netfilter_next(next
, direction
);
106 * We have gone through all filters, pass it to receiver.
107 * Do the valid check again incase sender or receiver been
108 * deleted while we go through filters.
110 if (sender
&& sender
->peer
) {
111 qemu_net_queue_send_iov(sender
->peer
->incoming_queue
,
112 sender
, flags
, iov
, iovcnt
, NULL
);
116 /* no receiver, or sender been deleted */
117 return iov_size(iov
, iovcnt
);
120 static char *netfilter_get_netdev_id(Object
*obj
, Error
**errp
)
122 NetFilterState
*nf
= NETFILTER(obj
);
124 return g_strdup(nf
->netdev_id
);
127 static void netfilter_set_netdev_id(Object
*obj
, const char *str
, Error
**errp
)
129 NetFilterState
*nf
= NETFILTER(obj
);
131 nf
->netdev_id
= g_strdup(str
);
134 static int netfilter_get_direction(Object
*obj
, Error
**errp G_GNUC_UNUSED
)
136 NetFilterState
*nf
= NETFILTER(obj
);
137 return nf
->direction
;
140 static void netfilter_set_direction(Object
*obj
, int direction
, Error
**errp
)
142 NetFilterState
*nf
= NETFILTER(obj
);
143 nf
->direction
= direction
;
146 static char *netfilter_get_status(Object
*obj
, Error
**errp
)
148 NetFilterState
*nf
= NETFILTER(obj
);
150 return nf
->on
? g_strdup("on") : g_strdup("off");
153 static void netfilter_set_status(Object
*obj
, const char *str
, Error
**errp
)
155 NetFilterState
*nf
= NETFILTER(obj
);
156 NetFilterClass
*nfc
= NETFILTER_GET_CLASS(obj
);
158 if (strcmp(str
, "on") && strcmp(str
, "off")) {
159 error_setg(errp
, "Invalid value for netfilter status, "
160 "should be 'on' or 'off'");
163 if (nf
->on
== !strcmp(str
, "on")) {
167 if (nf
->netdev
&& nfc
->status_changed
) {
168 nfc
->status_changed(nf
, errp
);
172 static void netfilter_init(Object
*obj
)
174 NetFilterState
*nf
= NETFILTER(obj
);
178 object_property_add_str(obj
, "netdev",
179 netfilter_get_netdev_id
, netfilter_set_netdev_id
,
181 object_property_add_enum(obj
, "queue", "NetFilterDirection",
182 NetFilterDirection_lookup
,
183 netfilter_get_direction
, netfilter_set_direction
,
185 object_property_add_str(obj
, "status",
186 netfilter_get_status
, netfilter_set_status
,
190 static void netfilter_complete(UserCreatable
*uc
, Error
**errp
)
192 NetFilterState
*nf
= NETFILTER(uc
);
193 NetClientState
*ncs
[MAX_QUEUE_NUM
];
194 NetFilterClass
*nfc
= NETFILTER_GET_CLASS(uc
);
196 Error
*local_err
= NULL
;
198 if (!nf
->netdev_id
) {
199 error_setg(errp
, "Parameter 'netdev' is required");
203 queues
= qemu_find_net_clients_except(nf
->netdev_id
, ncs
,
204 NET_CLIENT_DRIVER_NIC
,
207 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "netdev",
208 "a network backend id");
210 } else if (queues
> 1) {
211 error_setg(errp
, "multiqueue is not supported");
215 if (get_vhost_net(ncs
[0])) {
216 error_setg(errp
, "Vhost is not supported");
223 nfc
->setup(nf
, &local_err
);
225 error_propagate(errp
, local_err
);
229 QTAILQ_INSERT_TAIL(&nf
->netdev
->filters
, nf
, next
);
232 static void netfilter_finalize(Object
*obj
)
234 NetFilterState
*nf
= NETFILTER(obj
);
235 NetFilterClass
*nfc
= NETFILTER_GET_CLASS(obj
);
241 if (nf
->netdev
&& !QTAILQ_EMPTY(&nf
->netdev
->filters
) &&
242 QTAILQ_IN_USE(nf
, next
)) {
243 QTAILQ_REMOVE(&nf
->netdev
->filters
, nf
, next
);
245 g_free(nf
->netdev_id
);
248 static void netfilter_class_init(ObjectClass
*oc
, void *data
)
250 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
252 ucc
->complete
= netfilter_complete
;
255 static const TypeInfo netfilter_info
= {
256 .name
= TYPE_NETFILTER
,
257 .parent
= TYPE_OBJECT
,
259 .class_size
= sizeof(NetFilterClass
),
260 .class_init
= netfilter_class_init
,
261 .instance_size
= sizeof(NetFilterState
),
262 .instance_init
= netfilter_init
,
263 .instance_finalize
= netfilter_finalize
,
264 .interfaces
= (InterfaceInfo
[]) {
265 { TYPE_USER_CREATABLE
},
270 static void register_types(void)
272 type_register_static(&netfilter_info
);
275 type_init(register_types
);