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 "qom/object.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/error-report.h"
21 #include "chardev/char-fe.h"
23 #include "qemu/sockets.h"
25 #define FILTER_MIRROR(obj) \
26 OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR)
28 #define FILTER_REDIRECTOR(obj) \
29 OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_REDIRECTOR)
31 #define TYPE_FILTER_MIRROR "filter-mirror"
32 #define TYPE_FILTER_REDIRECTOR "filter-redirector"
33 #define REDIRECTOR_MAX_LEN NET_BUFSIZE
35 typedef struct MirrorState
{
36 NetFilterState parent_obj
;
45 static int filter_send(MirrorState
*s
,
46 const struct iovec
*iov
,
49 NetFilterState
*nf
= NETFILTER(s
);
55 size
= iov_size(iov
, iovcnt
);
61 ret
= qemu_chr_fe_write_all(&s
->chr_out
, (uint8_t *)&len
, sizeof(len
));
62 if (ret
!= sizeof(len
)) {
68 * If vnet_hdr = on, we send vnet header len to make other
69 * module(like colo-compare) know how to parse net
74 vnet_hdr_len
= nf
->netdev
->vnet_hdr_len
;
76 len
= htonl(vnet_hdr_len
);
77 ret
= qemu_chr_fe_write_all(&s
->chr_out
, (uint8_t *)&len
, sizeof(len
));
78 if (ret
!= sizeof(len
)) {
84 iov_to_buf(iov
, iovcnt
, 0, buf
, size
);
85 ret
= qemu_chr_fe_write_all(&s
->chr_out
, (uint8_t *)buf
, size
);
94 return ret
< 0 ? ret
: -EIO
;
97 static void redirector_to_filter(NetFilterState
*nf
,
102 .iov_base
= (void *)buf
,
106 if (nf
->direction
== NET_FILTER_DIRECTION_ALL
||
107 nf
->direction
== NET_FILTER_DIRECTION_TX
) {
108 qemu_netfilter_pass_to_next(nf
->netdev
, 0, &iov
, 1, nf
);
111 if (nf
->direction
== NET_FILTER_DIRECTION_ALL
||
112 nf
->direction
== NET_FILTER_DIRECTION_RX
) {
113 qemu_netfilter_pass_to_next(nf
->netdev
->peer
, 0, &iov
, 1, nf
);
117 static int redirector_chr_can_read(void *opaque
)
119 return REDIRECTOR_MAX_LEN
;
122 static void redirector_chr_read(void *opaque
, const uint8_t *buf
, int size
)
124 NetFilterState
*nf
= opaque
;
125 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
128 ret
= net_fill_rstate(&s
->rs
, buf
, size
);
131 qemu_chr_fe_set_handlers(&s
->chr_in
, NULL
, NULL
, NULL
,
132 NULL
, NULL
, NULL
, true);
136 static void redirector_chr_event(void *opaque
, int event
)
138 NetFilterState
*nf
= opaque
;
139 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
142 case CHR_EVENT_CLOSED
:
143 qemu_chr_fe_set_handlers(&s
->chr_in
, NULL
, NULL
, NULL
,
144 NULL
, NULL
, NULL
, true);
151 static ssize_t
filter_mirror_receive_iov(NetFilterState
*nf
,
152 NetClientState
*sender
,
154 const struct iovec
*iov
,
156 NetPacketSent
*sent_cb
)
158 MirrorState
*s
= FILTER_MIRROR(nf
);
161 ret
= filter_send(s
, iov
, iovcnt
);
163 error_report("filter mirror send failed(%s)", strerror(-ret
));
167 * we don't hope this error interrupt the normal
168 * path of net packet, so we always return zero.
173 static ssize_t
filter_redirector_receive_iov(NetFilterState
*nf
,
174 NetClientState
*sender
,
176 const struct iovec
*iov
,
178 NetPacketSent
*sent_cb
)
180 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
183 if (qemu_chr_fe_backend_connected(&s
->chr_out
)) {
184 ret
= filter_send(s
, iov
, iovcnt
);
186 error_report("filter redirector send failed(%s)", strerror(-ret
));
188 return iov_size(iov
, iovcnt
);
194 static void filter_mirror_cleanup(NetFilterState
*nf
)
196 MirrorState
*s
= FILTER_MIRROR(nf
);
198 qemu_chr_fe_deinit(&s
->chr_out
, false);
201 static void filter_redirector_cleanup(NetFilterState
*nf
)
203 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
205 qemu_chr_fe_deinit(&s
->chr_in
, false);
206 qemu_chr_fe_deinit(&s
->chr_out
, false);
209 static void filter_mirror_setup(NetFilterState
*nf
, Error
**errp
)
211 MirrorState
*s
= FILTER_MIRROR(nf
);
214 if (s
->outdev
== NULL
) {
215 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
, "filter-mirror parameter"\
216 " 'outdev' cannot be empty");
220 chr
= qemu_chr_find(s
->outdev
);
222 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
223 "Device '%s' not found", s
->outdev
);
227 qemu_chr_fe_init(&s
->chr_out
, chr
, errp
);
230 static void redirector_rs_finalize(SocketReadState
*rs
)
232 MirrorState
*s
= container_of(rs
, MirrorState
, rs
);
233 NetFilterState
*nf
= NETFILTER(s
);
235 redirector_to_filter(nf
, rs
->buf
, rs
->packet_len
);
238 static void filter_redirector_setup(NetFilterState
*nf
, Error
**errp
)
240 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
243 if (!s
->indev
&& !s
->outdev
) {
244 error_setg(errp
, "filter redirector needs 'indev' or "
245 "'outdev' at least one property set");
247 } else if (s
->indev
&& s
->outdev
) {
248 if (!strcmp(s
->indev
, s
->outdev
)) {
249 error_setg(errp
, "'indev' and 'outdev' could not be same "
250 "for filter redirector");
255 net_socket_rs_init(&s
->rs
, redirector_rs_finalize
, s
->vnet_hdr
);
258 chr
= qemu_chr_find(s
->indev
);
260 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
261 "IN Device '%s' not found", s
->indev
);
265 if (!qemu_chr_fe_init(&s
->chr_in
, chr
, errp
)) {
269 qemu_chr_fe_set_handlers(&s
->chr_in
, redirector_chr_can_read
,
270 redirector_chr_read
, redirector_chr_event
,
271 NULL
, nf
, NULL
, true);
275 chr
= qemu_chr_find(s
->outdev
);
277 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
278 "OUT Device '%s' not found", s
->outdev
);
281 if (!qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
287 static void filter_mirror_class_init(ObjectClass
*oc
, void *data
)
289 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
291 nfc
->setup
= filter_mirror_setup
;
292 nfc
->cleanup
= filter_mirror_cleanup
;
293 nfc
->receive_iov
= filter_mirror_receive_iov
;
296 static void filter_redirector_class_init(ObjectClass
*oc
, void *data
)
298 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
300 nfc
->setup
= filter_redirector_setup
;
301 nfc
->cleanup
= filter_redirector_cleanup
;
302 nfc
->receive_iov
= filter_redirector_receive_iov
;
305 static char *filter_redirector_get_indev(Object
*obj
, Error
**errp
)
307 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
309 return g_strdup(s
->indev
);
312 static void filter_redirector_set_indev(Object
*obj
,
316 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
319 s
->indev
= g_strdup(value
);
322 static char *filter_mirror_get_outdev(Object
*obj
, Error
**errp
)
324 MirrorState
*s
= FILTER_MIRROR(obj
);
326 return g_strdup(s
->outdev
);
329 static void filter_mirror_set_outdev(Object
*obj
,
333 MirrorState
*s
= FILTER_MIRROR(obj
);
336 s
->outdev
= g_strdup(value
);
338 error_setg(errp
, "filter mirror needs 'outdev' "
344 static bool filter_mirror_get_vnet_hdr(Object
*obj
, Error
**errp
)
346 MirrorState
*s
= FILTER_MIRROR(obj
);
351 static void filter_mirror_set_vnet_hdr(Object
*obj
, bool value
, Error
**errp
)
353 MirrorState
*s
= FILTER_MIRROR(obj
);
358 static char *filter_redirector_get_outdev(Object
*obj
, Error
**errp
)
360 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
362 return g_strdup(s
->outdev
);
365 static void filter_redirector_set_outdev(Object
*obj
,
369 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
372 s
->outdev
= g_strdup(value
);
375 static bool filter_redirector_get_vnet_hdr(Object
*obj
, Error
**errp
)
377 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
382 static void filter_redirector_set_vnet_hdr(Object
*obj
,
386 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
391 static void filter_mirror_init(Object
*obj
)
393 MirrorState
*s
= FILTER_MIRROR(obj
);
395 object_property_add_str(obj
, "outdev", filter_mirror_get_outdev
,
396 filter_mirror_set_outdev
, NULL
);
399 object_property_add_bool(obj
, "vnet_hdr_support",
400 filter_mirror_get_vnet_hdr
,
401 filter_mirror_set_vnet_hdr
, NULL
);
404 static void filter_redirector_init(Object
*obj
)
406 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
408 object_property_add_str(obj
, "indev", filter_redirector_get_indev
,
409 filter_redirector_set_indev
, NULL
);
410 object_property_add_str(obj
, "outdev", filter_redirector_get_outdev
,
411 filter_redirector_set_outdev
, NULL
);
414 object_property_add_bool(obj
, "vnet_hdr_support",
415 filter_redirector_get_vnet_hdr
,
416 filter_redirector_set_vnet_hdr
, NULL
);
419 static void filter_mirror_fini(Object
*obj
)
421 MirrorState
*s
= FILTER_MIRROR(obj
);
426 static void filter_redirector_fini(Object
*obj
)
428 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
434 static const TypeInfo filter_redirector_info
= {
435 .name
= TYPE_FILTER_REDIRECTOR
,
436 .parent
= TYPE_NETFILTER
,
437 .class_init
= filter_redirector_class_init
,
438 .instance_init
= filter_redirector_init
,
439 .instance_finalize
= filter_redirector_fini
,
440 .instance_size
= sizeof(MirrorState
),
443 static const TypeInfo filter_mirror_info
= {
444 .name
= TYPE_FILTER_MIRROR
,
445 .parent
= TYPE_NETFILTER
,
446 .class_init
= filter_mirror_class_init
,
447 .instance_init
= filter_mirror_init
,
448 .instance_finalize
= filter_mirror_fini
,
449 .instance_size
= sizeof(MirrorState
),
452 static void register_types(void)
454 type_register_static(&filter_mirror_info
);
455 type_register_static(&filter_redirector_info
);
458 type_init(register_types
);