stream: Switch stream_populate() to byte-based
[qemu/kevin.git] / net / filter-mirror.c
blob52d978fce2ef1b47e8c1159fd20f71483df0a6cf
1 /*
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"
14 #include "net/net.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"
22 #include "trace.h"
23 #include "chardev/char-fe.h"
24 #include "qemu/iov.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;
39 char *indev;
40 char *outdev;
41 CharBackend chr_in;
42 CharBackend chr_out;
43 SocketReadState rs;
44 } MirrorState;
46 static int filter_send(CharBackend *chr_out,
47 const struct iovec *iov,
48 int iovcnt)
50 int ret = 0;
51 ssize_t size = 0;
52 uint32_t len = 0;
53 char *buf;
55 size = iov_size(iov, iovcnt);
56 if (!size) {
57 return 0;
60 len = htonl(size);
61 ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)&len, sizeof(len));
62 if (ret != sizeof(len)) {
63 goto err;
66 buf = g_malloc(size);
67 iov_to_buf(iov, iovcnt, 0, buf, size);
68 ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)buf, size);
69 g_free(buf);
70 if (ret != size) {
71 goto err;
74 return 0;
76 err:
77 return ret < 0 ? ret : -EIO;
80 static void redirector_to_filter(NetFilterState *nf,
81 const uint8_t *buf,
82 int len)
84 struct iovec iov = {
85 .iov_base = (void *)buf,
86 .iov_len = len,
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);
109 int ret;
111 ret = net_fill_rstate(&s->rs, buf, size);
113 if (ret == -1) {
114 qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
115 NULL, NULL, true);
119 static void redirector_chr_event(void *opaque, int event)
121 NetFilterState *nf = opaque;
122 MirrorState *s = FILTER_REDIRECTOR(nf);
124 switch (event) {
125 case CHR_EVENT_CLOSED:
126 qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
127 NULL, NULL, true);
128 break;
129 default:
130 break;
134 static ssize_t filter_mirror_receive_iov(NetFilterState *nf,
135 NetClientState *sender,
136 unsigned flags,
137 const struct iovec *iov,
138 int iovcnt,
139 NetPacketSent *sent_cb)
141 MirrorState *s = FILTER_MIRROR(nf);
142 int ret;
144 ret = filter_send(&s->chr_out, iov, iovcnt);
145 if (ret) {
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.
153 return 0;
156 static ssize_t filter_redirector_receive_iov(NetFilterState *nf,
157 NetClientState *sender,
158 unsigned flags,
159 const struct iovec *iov,
160 int iovcnt,
161 NetPacketSent *sent_cb)
163 MirrorState *s = FILTER_REDIRECTOR(nf);
164 int ret;
166 if (qemu_chr_fe_get_driver(&s->chr_out)) {
167 ret = filter_send(&s->chr_out, iov, iovcnt);
168 if (ret) {
169 error_report("filter redirector send failed(%s)", strerror(-ret));
171 return iov_size(iov, iovcnt);
172 } else {
173 return 0;
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);
195 Chardev *chr;
197 chr = qemu_chr_find(s->outdev);
198 if (chr == NULL) {
199 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
200 "Device '%s' not found", s->outdev);
201 return;
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);
218 Chardev *chr;
220 if (!s->indev && !s->outdev) {
221 error_setg(errp, "filter redirector needs 'indev' or "
222 "'outdev' at least one property set");
223 return;
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");
228 return;
232 net_socket_rs_init(&s->rs, redirector_rs_finalize);
234 if (s->indev) {
235 chr = qemu_chr_find(s->indev);
236 if (chr == NULL) {
237 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
238 "IN Device '%s' not found", s->indev);
239 return;
242 if (!qemu_chr_fe_init(&s->chr_in, chr, errp)) {
243 return;
246 qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
247 redirector_chr_read, redirector_chr_event,
248 nf, NULL, true);
251 if (s->outdev) {
252 chr = qemu_chr_find(s->outdev);
253 if (chr == NULL) {
254 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
255 "OUT Device '%s' not found", s->outdev);
256 return;
258 if (!qemu_chr_fe_init(&s->chr_out, chr, errp)) {
259 return;
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,
290 const char *value,
291 Error **errp)
293 MirrorState *s = FILTER_REDIRECTOR(obj);
295 g_free(s->indev);
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,
307 const char *value,
308 Error **errp)
310 MirrorState *s = FILTER_MIRROR(obj);
312 g_free(s->outdev);
313 s->outdev = g_strdup(value);
314 if (!s->outdev) {
315 error_setg(errp, "filter mirror needs 'outdev' "
316 "property set");
317 return;
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,
329 const char *value,
330 Error **errp)
332 MirrorState *s = FILTER_REDIRECTOR(obj);
334 g_free(s->outdev);
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);
356 g_free(s->outdev);
359 static void filter_redirector_fini(Object *obj)
361 MirrorState *s = FILTER_REDIRECTOR(obj);
363 g_free(s->indev);
364 g_free(s->outdev);
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);