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 "sysemu/char.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_mirror_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_mirror_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_mirror_send(&s
->chr_out
, iov
, iovcnt
);
169 error_report("filter_mirror_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
);
184 static void filter_redirector_cleanup(NetFilterState
*nf
)
186 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
188 qemu_chr_fe_deinit(&s
->chr_in
);
189 qemu_chr_fe_deinit(&s
->chr_out
);
192 static void filter_mirror_setup(NetFilterState
*nf
, Error
**errp
)
194 MirrorState
*s
= FILTER_MIRROR(nf
);
198 error_setg(errp
, "filter mirror needs 'outdev' "
203 chr
= qemu_chr_find(s
->outdev
);
205 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
206 "Device '%s' not found", s
->outdev
);
210 qemu_chr_fe_init(&s
->chr_out
, chr
, errp
);
213 static void redirector_rs_finalize(SocketReadState
*rs
)
215 MirrorState
*s
= container_of(rs
, MirrorState
, rs
);
216 NetFilterState
*nf
= NETFILTER(s
);
218 redirector_to_filter(nf
, rs
->buf
, rs
->packet_len
);
221 static void filter_redirector_setup(NetFilterState
*nf
, Error
**errp
)
223 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
226 if (!s
->indev
&& !s
->outdev
) {
227 error_setg(errp
, "filter redirector needs 'indev' or "
228 "'outdev' at least one property set");
230 } else if (s
->indev
&& s
->outdev
) {
231 if (!strcmp(s
->indev
, s
->outdev
)) {
232 error_setg(errp
, "'indev' and 'outdev' could not be same "
233 "for filter redirector");
238 net_socket_rs_init(&s
->rs
, redirector_rs_finalize
);
241 chr
= qemu_chr_find(s
->indev
);
243 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
244 "IN Device '%s' not found", s
->indev
);
248 if (!qemu_chr_fe_init(&s
->chr_in
, chr
, errp
)) {
252 qemu_chr_fe_set_handlers(&s
->chr_in
, redirector_chr_can_read
,
253 redirector_chr_read
, redirector_chr_event
,
258 chr
= qemu_chr_find(s
->outdev
);
260 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
261 "OUT Device '%s' not found", s
->outdev
);
264 if (!qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
270 static void filter_mirror_class_init(ObjectClass
*oc
, void *data
)
272 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
274 nfc
->setup
= filter_mirror_setup
;
275 nfc
->cleanup
= filter_mirror_cleanup
;
276 nfc
->receive_iov
= filter_mirror_receive_iov
;
279 static void filter_redirector_class_init(ObjectClass
*oc
, void *data
)
281 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
283 nfc
->setup
= filter_redirector_setup
;
284 nfc
->cleanup
= filter_redirector_cleanup
;
285 nfc
->receive_iov
= filter_redirector_receive_iov
;
288 static char *filter_redirector_get_indev(Object
*obj
, Error
**errp
)
290 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
292 return g_strdup(s
->indev
);
296 filter_redirector_set_indev(Object
*obj
, const char *value
, Error
**errp
)
298 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
301 s
->indev
= g_strdup(value
);
304 static char *filter_mirror_get_outdev(Object
*obj
, Error
**errp
)
306 MirrorState
*s
= FILTER_MIRROR(obj
);
308 return g_strdup(s
->outdev
);
312 filter_mirror_set_outdev(Object
*obj
, const char *value
, Error
**errp
)
314 MirrorState
*s
= FILTER_MIRROR(obj
);
317 s
->outdev
= g_strdup(value
);
319 error_setg(errp
, "filter mirror needs 'outdev' "
325 static char *filter_redirector_get_outdev(Object
*obj
, Error
**errp
)
327 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
329 return g_strdup(s
->outdev
);
333 filter_redirector_set_outdev(Object
*obj
, const char *value
, Error
**errp
)
335 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
338 s
->outdev
= g_strdup(value
);
341 static void filter_mirror_init(Object
*obj
)
343 object_property_add_str(obj
, "outdev", filter_mirror_get_outdev
,
344 filter_mirror_set_outdev
, NULL
);
347 static void filter_redirector_init(Object
*obj
)
349 object_property_add_str(obj
, "indev", filter_redirector_get_indev
,
350 filter_redirector_set_indev
, NULL
);
351 object_property_add_str(obj
, "outdev", filter_redirector_get_outdev
,
352 filter_redirector_set_outdev
, NULL
);
355 static void filter_mirror_fini(Object
*obj
)
357 MirrorState
*s
= FILTER_MIRROR(obj
);
362 static void filter_redirector_fini(Object
*obj
)
364 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
370 static const TypeInfo filter_redirector_info
= {
371 .name
= TYPE_FILTER_REDIRECTOR
,
372 .parent
= TYPE_NETFILTER
,
373 .class_init
= filter_redirector_class_init
,
374 .instance_init
= filter_redirector_init
,
375 .instance_finalize
= filter_redirector_fini
,
376 .instance_size
= sizeof(MirrorState
),
379 static const TypeInfo filter_mirror_info
= {
380 .name
= TYPE_FILTER_MIRROR
,
381 .parent
= TYPE_NETFILTER
,
382 .class_init
= filter_mirror_class_init
,
383 .instance_init
= filter_mirror_init
,
384 .instance_finalize
= filter_mirror_fini
,
385 .instance_size
= sizeof(MirrorState
),
388 static void register_types(void)
390 type_register_static(&filter_mirror_info
);
391 type_register_static(&filter_redirector_info
);
394 type_init(register_types
);