2 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
3 * Copyright (c) 2016 FUJITSU LIMITED
4 * Copyright (c) 2016 Intel Corporation
6 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or
9 * later. See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "net/filter.h"
15 #include "qemu-common.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qapi-visit.h"
19 #include "qom/object.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/error-report.h"
23 #include "chardev/char-fe.h"
25 #include "qemu/sockets.h"
27 #define FILTER_MIRROR(obj) \
28 OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR)
30 #define FILTER_REDIRECTOR(obj) \
31 OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_REDIRECTOR)
33 #define TYPE_FILTER_MIRROR "filter-mirror"
34 #define TYPE_FILTER_REDIRECTOR "filter-redirector"
35 #define REDIRECTOR_MAX_LEN NET_BUFSIZE
37 typedef struct MirrorState
{
38 NetFilterState parent_obj
;
46 static int filter_send(CharBackend
*chr_out
,
47 const struct iovec
*iov
,
55 size
= iov_size(iov
, iovcnt
);
61 ret
= qemu_chr_fe_write_all(chr_out
, (uint8_t *)&len
, sizeof(len
));
62 if (ret
!= sizeof(len
)) {
67 iov_to_buf(iov
, iovcnt
, 0, buf
, size
);
68 ret
= qemu_chr_fe_write_all(chr_out
, (uint8_t *)buf
, size
);
77 return ret
< 0 ? ret
: -EIO
;
80 static void redirector_to_filter(NetFilterState
*nf
,
85 .iov_base
= (void *)buf
,
89 if (nf
->direction
== NET_FILTER_DIRECTION_ALL
||
90 nf
->direction
== NET_FILTER_DIRECTION_TX
) {
91 qemu_netfilter_pass_to_next(nf
->netdev
, 0, &iov
, 1, nf
);
94 if (nf
->direction
== NET_FILTER_DIRECTION_ALL
||
95 nf
->direction
== NET_FILTER_DIRECTION_RX
) {
96 qemu_netfilter_pass_to_next(nf
->netdev
->peer
, 0, &iov
, 1, nf
);
100 static int redirector_chr_can_read(void *opaque
)
102 return REDIRECTOR_MAX_LEN
;
105 static void redirector_chr_read(void *opaque
, const uint8_t *buf
, int size
)
107 NetFilterState
*nf
= opaque
;
108 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
111 ret
= net_fill_rstate(&s
->rs
, buf
, size
);
114 qemu_chr_fe_set_handlers(&s
->chr_in
, NULL
, NULL
, NULL
,
119 static void redirector_chr_event(void *opaque
, int event
)
121 NetFilterState
*nf
= opaque
;
122 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
125 case CHR_EVENT_CLOSED
:
126 qemu_chr_fe_set_handlers(&s
->chr_in
, NULL
, NULL
, NULL
,
134 static ssize_t
filter_mirror_receive_iov(NetFilterState
*nf
,
135 NetClientState
*sender
,
137 const struct iovec
*iov
,
139 NetPacketSent
*sent_cb
)
141 MirrorState
*s
= FILTER_MIRROR(nf
);
144 ret
= filter_send(&s
->chr_out
, iov
, iovcnt
);
146 error_report("filter mirror send failed(%s)", strerror(-ret
));
150 * we don't hope this error interrupt the normal
151 * path of net packet, so we always return zero.
156 static ssize_t
filter_redirector_receive_iov(NetFilterState
*nf
,
157 NetClientState
*sender
,
159 const struct iovec
*iov
,
161 NetPacketSent
*sent_cb
)
163 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
166 if (qemu_chr_fe_get_driver(&s
->chr_out
)) {
167 ret
= filter_send(&s
->chr_out
, iov
, iovcnt
);
169 error_report("filter redirector send failed(%s)", strerror(-ret
));
171 return iov_size(iov
, iovcnt
);
177 static void filter_mirror_cleanup(NetFilterState
*nf
)
179 MirrorState
*s
= FILTER_MIRROR(nf
);
181 qemu_chr_fe_deinit(&s
->chr_out
, false);
184 static void filter_redirector_cleanup(NetFilterState
*nf
)
186 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
188 qemu_chr_fe_deinit(&s
->chr_in
, false);
189 qemu_chr_fe_deinit(&s
->chr_out
, false);
192 static void filter_mirror_setup(NetFilterState
*nf
, Error
**errp
)
194 MirrorState
*s
= FILTER_MIRROR(nf
);
197 chr
= qemu_chr_find(s
->outdev
);
199 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
200 "Device '%s' not found", s
->outdev
);
204 qemu_chr_fe_init(&s
->chr_out
, chr
, errp
);
207 static void redirector_rs_finalize(SocketReadState
*rs
)
209 MirrorState
*s
= container_of(rs
, MirrorState
, rs
);
210 NetFilterState
*nf
= NETFILTER(s
);
212 redirector_to_filter(nf
, rs
->buf
, rs
->packet_len
);
215 static void filter_redirector_setup(NetFilterState
*nf
, Error
**errp
)
217 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
220 if (!s
->indev
&& !s
->outdev
) {
221 error_setg(errp
, "filter redirector needs 'indev' or "
222 "'outdev' at least one property set");
224 } else if (s
->indev
&& s
->outdev
) {
225 if (!strcmp(s
->indev
, s
->outdev
)) {
226 error_setg(errp
, "'indev' and 'outdev' could not be same "
227 "for filter redirector");
232 net_socket_rs_init(&s
->rs
, redirector_rs_finalize
);
235 chr
= qemu_chr_find(s
->indev
);
237 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
238 "IN Device '%s' not found", s
->indev
);
242 if (!qemu_chr_fe_init(&s
->chr_in
, chr
, errp
)) {
246 qemu_chr_fe_set_handlers(&s
->chr_in
, redirector_chr_can_read
,
247 redirector_chr_read
, redirector_chr_event
,
252 chr
= qemu_chr_find(s
->outdev
);
254 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
255 "OUT Device '%s' not found", s
->outdev
);
258 if (!qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
264 static void filter_mirror_class_init(ObjectClass
*oc
, void *data
)
266 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
268 nfc
->setup
= filter_mirror_setup
;
269 nfc
->cleanup
= filter_mirror_cleanup
;
270 nfc
->receive_iov
= filter_mirror_receive_iov
;
273 static void filter_redirector_class_init(ObjectClass
*oc
, void *data
)
275 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
277 nfc
->setup
= filter_redirector_setup
;
278 nfc
->cleanup
= filter_redirector_cleanup
;
279 nfc
->receive_iov
= filter_redirector_receive_iov
;
282 static char *filter_redirector_get_indev(Object
*obj
, Error
**errp
)
284 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
286 return g_strdup(s
->indev
);
289 static void filter_redirector_set_indev(Object
*obj
,
293 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
296 s
->indev
= g_strdup(value
);
299 static char *filter_mirror_get_outdev(Object
*obj
, Error
**errp
)
301 MirrorState
*s
= FILTER_MIRROR(obj
);
303 return g_strdup(s
->outdev
);
306 static void filter_mirror_set_outdev(Object
*obj
,
310 MirrorState
*s
= FILTER_MIRROR(obj
);
313 s
->outdev
= g_strdup(value
);
315 error_setg(errp
, "filter mirror needs 'outdev' "
321 static char *filter_redirector_get_outdev(Object
*obj
, Error
**errp
)
323 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
325 return g_strdup(s
->outdev
);
328 static void filter_redirector_set_outdev(Object
*obj
,
332 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
335 s
->outdev
= g_strdup(value
);
338 static void filter_mirror_init(Object
*obj
)
340 object_property_add_str(obj
, "outdev", filter_mirror_get_outdev
,
341 filter_mirror_set_outdev
, NULL
);
344 static void filter_redirector_init(Object
*obj
)
346 object_property_add_str(obj
, "indev", filter_redirector_get_indev
,
347 filter_redirector_set_indev
, NULL
);
348 object_property_add_str(obj
, "outdev", filter_redirector_get_outdev
,
349 filter_redirector_set_outdev
, NULL
);
352 static void filter_mirror_fini(Object
*obj
)
354 MirrorState
*s
= FILTER_MIRROR(obj
);
359 static void filter_redirector_fini(Object
*obj
)
361 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
367 static const TypeInfo filter_redirector_info
= {
368 .name
= TYPE_FILTER_REDIRECTOR
,
369 .parent
= TYPE_NETFILTER
,
370 .class_init
= filter_redirector_class_init
,
371 .instance_init
= filter_redirector_init
,
372 .instance_finalize
= filter_redirector_fini
,
373 .instance_size
= sizeof(MirrorState
),
376 static const TypeInfo filter_mirror_info
= {
377 .name
= TYPE_FILTER_MIRROR
,
378 .parent
= TYPE_NETFILTER
,
379 .class_init
= filter_mirror_class_init
,
380 .instance_init
= filter_mirror_init
,
381 .instance_finalize
= filter_mirror_fini
,
382 .instance_size
= sizeof(MirrorState
),
385 static void register_types(void)
387 type_register_static(&filter_mirror_info
);
388 type_register_static(&filter_redirector_info
);
391 type_init(register_types
);