List SASL config file under the cryptography maintainer's realm
[qemu/kevin.git] / net / filter-mirror.c
blob72fa7c2b6c3ef247df0f89b84060096acbbbdfea
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 "sysemu/char.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_mirror_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_mirror_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_mirror_send(&s->chr_out, iov, iovcnt);
168 if (ret) {
169 error_report("filter_mirror_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);
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);
195 Chardev *chr;
197 if (!s->outdev) {
198 error_setg(errp, "filter mirror needs 'outdev' "
199 "property set");
200 return;
203 chr = qemu_chr_find(s->outdev);
204 if (chr == NULL) {
205 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
206 "Device '%s' not found", s->outdev);
207 return;
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);
224 Chardev *chr;
226 if (!s->indev && !s->outdev) {
227 error_setg(errp, "filter redirector needs 'indev' or "
228 "'outdev' at least one property set");
229 return;
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");
234 return;
238 net_socket_rs_init(&s->rs, redirector_rs_finalize);
240 if (s->indev) {
241 chr = qemu_chr_find(s->indev);
242 if (chr == NULL) {
243 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
244 "IN Device '%s' not found", s->indev);
245 return;
248 if (!qemu_chr_fe_init(&s->chr_in, chr, errp)) {
249 return;
252 qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
253 redirector_chr_read, redirector_chr_event,
254 nf, NULL, true);
257 if (s->outdev) {
258 chr = qemu_chr_find(s->outdev);
259 if (chr == NULL) {
260 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
261 "OUT Device '%s' not found", s->outdev);
262 return;
264 if (!qemu_chr_fe_init(&s->chr_out, chr, errp)) {
265 return;
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);
295 static void
296 filter_redirector_set_indev(Object *obj, const char *value, Error **errp)
298 MirrorState *s = FILTER_REDIRECTOR(obj);
300 g_free(s->indev);
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);
311 static void
312 filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
314 MirrorState *s = FILTER_MIRROR(obj);
316 g_free(s->outdev);
317 s->outdev = g_strdup(value);
318 if (!s->outdev) {
319 error_setg(errp, "filter mirror needs 'outdev' "
320 "property set");
321 return;
325 static char *filter_redirector_get_outdev(Object *obj, Error **errp)
327 MirrorState *s = FILTER_REDIRECTOR(obj);
329 return g_strdup(s->outdev);
332 static void
333 filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
335 MirrorState *s = FILTER_REDIRECTOR(obj);
337 g_free(s->outdev);
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);
359 g_free(s->outdev);
362 static void filter_redirector_fini(Object *obj)
364 MirrorState *s = FILTER_REDIRECTOR(obj);
366 g_free(s->indev);
367 g_free(s->outdev);
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);