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 "qapi/error.h"
16 #include "qom/object.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/error-report.h"
20 #include "chardev/char-fe.h"
22 #include "qemu/sockets.h"
24 #define FILTER_MIRROR(obj) \
25 OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR)
27 #define FILTER_REDIRECTOR(obj) \
28 OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_REDIRECTOR)
30 #define TYPE_FILTER_MIRROR "filter-mirror"
31 #define TYPE_FILTER_REDIRECTOR "filter-redirector"
32 #define REDIRECTOR_MAX_LEN NET_BUFSIZE
34 typedef struct MirrorState
{
35 NetFilterState parent_obj
;
44 static int filter_send(MirrorState
*s
,
45 const struct iovec
*iov
,
48 NetFilterState
*nf
= NETFILTER(s
);
54 size
= iov_size(iov
, iovcnt
);
60 ret
= qemu_chr_fe_write_all(&s
->chr_out
, (uint8_t *)&len
, sizeof(len
));
61 if (ret
!= sizeof(len
)) {
67 * If vnet_hdr = on, we send vnet header len to make other
68 * module(like colo-compare) know how to parse net
73 vnet_hdr_len
= nf
->netdev
->vnet_hdr_len
;
75 len
= htonl(vnet_hdr_len
);
76 ret
= qemu_chr_fe_write_all(&s
->chr_out
, (uint8_t *)&len
, sizeof(len
));
77 if (ret
!= sizeof(len
)) {
83 iov_to_buf(iov
, iovcnt
, 0, buf
, size
);
84 ret
= qemu_chr_fe_write_all(&s
->chr_out
, (uint8_t *)buf
, size
);
93 return ret
< 0 ? ret
: -EIO
;
96 static void redirector_to_filter(NetFilterState
*nf
,
101 .iov_base
= (void *)buf
,
105 if (nf
->direction
== NET_FILTER_DIRECTION_ALL
||
106 nf
->direction
== NET_FILTER_DIRECTION_TX
) {
107 qemu_netfilter_pass_to_next(nf
->netdev
, 0, &iov
, 1, nf
);
110 if (nf
->direction
== NET_FILTER_DIRECTION_ALL
||
111 nf
->direction
== NET_FILTER_DIRECTION_RX
) {
112 qemu_netfilter_pass_to_next(nf
->netdev
->peer
, 0, &iov
, 1, nf
);
116 static int redirector_chr_can_read(void *opaque
)
118 return REDIRECTOR_MAX_LEN
;
121 static void redirector_chr_read(void *opaque
, const uint8_t *buf
, int size
)
123 NetFilterState
*nf
= opaque
;
124 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
127 ret
= net_fill_rstate(&s
->rs
, buf
, size
);
130 qemu_chr_fe_set_handlers(&s
->chr_in
, NULL
, NULL
, NULL
,
131 NULL
, NULL
, NULL
, true);
135 static void redirector_chr_event(void *opaque
, QEMUChrEvent event
)
137 NetFilterState
*nf
= opaque
;
138 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
141 case CHR_EVENT_CLOSED
:
142 qemu_chr_fe_set_handlers(&s
->chr_in
, NULL
, NULL
, NULL
,
143 NULL
, NULL
, NULL
, true);
150 static ssize_t
filter_mirror_receive_iov(NetFilterState
*nf
,
151 NetClientState
*sender
,
153 const struct iovec
*iov
,
155 NetPacketSent
*sent_cb
)
157 MirrorState
*s
= FILTER_MIRROR(nf
);
160 ret
= filter_send(s
, iov
, iovcnt
);
162 error_report("filter mirror send failed(%s)", strerror(-ret
));
166 * we don't hope this error interrupt the normal
167 * path of net packet, so we always return zero.
172 static ssize_t
filter_redirector_receive_iov(NetFilterState
*nf
,
173 NetClientState
*sender
,
175 const struct iovec
*iov
,
177 NetPacketSent
*sent_cb
)
179 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
182 if (qemu_chr_fe_backend_connected(&s
->chr_out
)) {
183 ret
= filter_send(s
, iov
, iovcnt
);
185 error_report("filter redirector send failed(%s)", strerror(-ret
));
187 return iov_size(iov
, iovcnt
);
193 static void filter_mirror_cleanup(NetFilterState
*nf
)
195 MirrorState
*s
= FILTER_MIRROR(nf
);
197 qemu_chr_fe_deinit(&s
->chr_out
, false);
200 static void filter_redirector_cleanup(NetFilterState
*nf
)
202 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
204 qemu_chr_fe_deinit(&s
->chr_in
, false);
205 qemu_chr_fe_deinit(&s
->chr_out
, false);
208 static void filter_mirror_setup(NetFilterState
*nf
, Error
**errp
)
210 MirrorState
*s
= FILTER_MIRROR(nf
);
213 if (s
->outdev
== NULL
) {
214 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
, "filter-mirror parameter"\
215 " 'outdev' cannot be empty");
219 chr
= qemu_chr_find(s
->outdev
);
221 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
222 "Device '%s' not found", s
->outdev
);
226 qemu_chr_fe_init(&s
->chr_out
, chr
, errp
);
229 static void redirector_rs_finalize(SocketReadState
*rs
)
231 MirrorState
*s
= container_of(rs
, MirrorState
, rs
);
232 NetFilterState
*nf
= NETFILTER(s
);
234 redirector_to_filter(nf
, rs
->buf
, rs
->packet_len
);
237 static void filter_redirector_setup(NetFilterState
*nf
, Error
**errp
)
239 MirrorState
*s
= FILTER_REDIRECTOR(nf
);
242 if (!s
->indev
&& !s
->outdev
) {
243 error_setg(errp
, "filter redirector needs 'indev' or "
244 "'outdev' at least one property set");
246 } else if (s
->indev
&& s
->outdev
) {
247 if (!strcmp(s
->indev
, s
->outdev
)) {
248 error_setg(errp
, "'indev' and 'outdev' could not be same "
249 "for filter redirector");
254 net_socket_rs_init(&s
->rs
, redirector_rs_finalize
, s
->vnet_hdr
);
257 chr
= qemu_chr_find(s
->indev
);
259 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
260 "IN Device '%s' not found", s
->indev
);
264 if (!qemu_chr_fe_init(&s
->chr_in
, chr
, errp
)) {
268 qemu_chr_fe_set_handlers(&s
->chr_in
, redirector_chr_can_read
,
269 redirector_chr_read
, redirector_chr_event
,
270 NULL
, nf
, NULL
, true);
274 chr
= qemu_chr_find(s
->outdev
);
276 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
277 "OUT Device '%s' not found", s
->outdev
);
280 if (!qemu_chr_fe_init(&s
->chr_out
, chr
, errp
)) {
286 static void filter_mirror_class_init(ObjectClass
*oc
, void *data
)
288 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
290 nfc
->setup
= filter_mirror_setup
;
291 nfc
->cleanup
= filter_mirror_cleanup
;
292 nfc
->receive_iov
= filter_mirror_receive_iov
;
295 static void filter_redirector_class_init(ObjectClass
*oc
, void *data
)
297 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
299 nfc
->setup
= filter_redirector_setup
;
300 nfc
->cleanup
= filter_redirector_cleanup
;
301 nfc
->receive_iov
= filter_redirector_receive_iov
;
304 static char *filter_redirector_get_indev(Object
*obj
, Error
**errp
)
306 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
308 return g_strdup(s
->indev
);
311 static void filter_redirector_set_indev(Object
*obj
,
315 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
318 s
->indev
= g_strdup(value
);
321 static char *filter_mirror_get_outdev(Object
*obj
, Error
**errp
)
323 MirrorState
*s
= FILTER_MIRROR(obj
);
325 return g_strdup(s
->outdev
);
328 static void filter_mirror_set_outdev(Object
*obj
,
332 MirrorState
*s
= FILTER_MIRROR(obj
);
335 s
->outdev
= g_strdup(value
);
337 error_setg(errp
, "filter mirror needs 'outdev' "
343 static bool filter_mirror_get_vnet_hdr(Object
*obj
, Error
**errp
)
345 MirrorState
*s
= FILTER_MIRROR(obj
);
350 static void filter_mirror_set_vnet_hdr(Object
*obj
, bool value
, Error
**errp
)
352 MirrorState
*s
= FILTER_MIRROR(obj
);
357 static char *filter_redirector_get_outdev(Object
*obj
, Error
**errp
)
359 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
361 return g_strdup(s
->outdev
);
364 static void filter_redirector_set_outdev(Object
*obj
,
368 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
371 s
->outdev
= g_strdup(value
);
374 static bool filter_redirector_get_vnet_hdr(Object
*obj
, Error
**errp
)
376 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
381 static void filter_redirector_set_vnet_hdr(Object
*obj
,
385 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
390 static void filter_mirror_init(Object
*obj
)
392 MirrorState
*s
= FILTER_MIRROR(obj
);
394 object_property_add_str(obj
, "outdev", filter_mirror_get_outdev
,
395 filter_mirror_set_outdev
, NULL
);
398 object_property_add_bool(obj
, "vnet_hdr_support",
399 filter_mirror_get_vnet_hdr
,
400 filter_mirror_set_vnet_hdr
, NULL
);
403 static void filter_redirector_init(Object
*obj
)
405 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
407 object_property_add_str(obj
, "indev", filter_redirector_get_indev
,
408 filter_redirector_set_indev
, NULL
);
409 object_property_add_str(obj
, "outdev", filter_redirector_get_outdev
,
410 filter_redirector_set_outdev
, NULL
);
413 object_property_add_bool(obj
, "vnet_hdr_support",
414 filter_redirector_get_vnet_hdr
,
415 filter_redirector_set_vnet_hdr
, NULL
);
418 static void filter_mirror_fini(Object
*obj
)
420 MirrorState
*s
= FILTER_MIRROR(obj
);
425 static void filter_redirector_fini(Object
*obj
)
427 MirrorState
*s
= FILTER_REDIRECTOR(obj
);
433 static const TypeInfo filter_redirector_info
= {
434 .name
= TYPE_FILTER_REDIRECTOR
,
435 .parent
= TYPE_NETFILTER
,
436 .class_init
= filter_redirector_class_init
,
437 .instance_init
= filter_redirector_init
,
438 .instance_finalize
= filter_redirector_fini
,
439 .instance_size
= sizeof(MirrorState
),
442 static const TypeInfo filter_mirror_info
= {
443 .name
= TYPE_FILTER_MIRROR
,
444 .parent
= TYPE_NETFILTER
,
445 .class_init
= filter_mirror_class_init
,
446 .instance_init
= filter_mirror_init
,
447 .instance_finalize
= filter_mirror_fini
,
448 .instance_size
= sizeof(MirrorState
),
451 static void register_types(void)
453 type_register_static(&filter_mirror_info
);
454 type_register_static(&filter_redirector_info
);
457 type_init(register_types
);