block: m25p80: n25q256a/n25q512a models
[qemu/ar7.git] / net / filter-mirror.c
blobc0c4dc60b62c99f7f994607e36c78d997dfe861e
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 CharDriverState *chr_in;
42 CharDriverState *chr_out;
43 int state; /* 0 = getting length, 1 = getting data */
44 unsigned int index;
45 unsigned int packet_len;
46 uint8_t buf[REDIRECTOR_MAX_LEN];
47 } MirrorState;
49 static int filter_mirror_send(CharDriverState *chr_out,
50 const struct iovec *iov,
51 int iovcnt)
53 int ret = 0;
54 ssize_t size = 0;
55 uint32_t len = 0;
56 char *buf;
58 size = iov_size(iov, iovcnt);
59 if (!size) {
60 return 0;
63 len = htonl(size);
64 ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)&len, sizeof(len));
65 if (ret != sizeof(len)) {
66 goto err;
69 buf = g_malloc(size);
70 iov_to_buf(iov, iovcnt, 0, buf, size);
71 ret = qemu_chr_fe_write_all(chr_out, (uint8_t *)buf, size);
72 g_free(buf);
73 if (ret != size) {
74 goto err;
77 return 0;
79 err:
80 return ret < 0 ? ret : -EIO;
83 static void
84 redirector_to_filter(NetFilterState *nf, const uint8_t *buf, int len)
86 struct iovec iov = {
87 .iov_base = (void *)buf,
88 .iov_len = len,
91 if (nf->direction == NET_FILTER_DIRECTION_ALL ||
92 nf->direction == NET_FILTER_DIRECTION_TX) {
93 qemu_netfilter_pass_to_next(nf->netdev, 0, &iov, 1, nf);
96 if (nf->direction == NET_FILTER_DIRECTION_ALL ||
97 nf->direction == NET_FILTER_DIRECTION_RX) {
98 qemu_netfilter_pass_to_next(nf->netdev->peer, 0, &iov, 1, nf);
102 static int redirector_chr_can_read(void *opaque)
104 return REDIRECTOR_MAX_LEN;
107 static void redirector_chr_read(void *opaque, const uint8_t *buf, int size)
109 NetFilterState *nf = opaque;
110 MirrorState *s = FILTER_REDIRECTOR(nf);
111 unsigned int l;
113 while (size > 0) {
114 /* reassemble a packet from the network */
115 switch (s->state) { /* 0 = getting length, 1 = getting data */
116 case 0:
117 l = 4 - s->index;
118 if (l > size) {
119 l = size;
121 memcpy(s->buf + s->index, buf, l);
122 buf += l;
123 size -= l;
124 s->index += l;
125 if (s->index == 4) {
126 /* got length */
127 s->packet_len = ntohl(*(uint32_t *)s->buf);
128 s->index = 0;
129 s->state = 1;
131 break;
132 case 1:
133 l = s->packet_len - s->index;
134 if (l > size) {
135 l = size;
137 if (s->index + l <= sizeof(s->buf)) {
138 memcpy(s->buf + s->index, buf, l);
139 } else {
140 error_report("serious error: oversized packet received.");
141 s->index = s->state = 0;
142 qemu_chr_add_handlers(s->chr_in, NULL, NULL, NULL, NULL);
143 return;
146 s->index += l;
147 buf += l;
148 size -= l;
149 if (s->index >= s->packet_len) {
150 s->index = 0;
151 s->state = 0;
152 redirector_to_filter(nf, s->buf, s->packet_len);
154 break;
159 static void redirector_chr_event(void *opaque, int event)
161 NetFilterState *nf = opaque;
162 MirrorState *s = FILTER_REDIRECTOR(nf);
164 switch (event) {
165 case CHR_EVENT_CLOSED:
166 qemu_chr_add_handlers(s->chr_in, NULL, NULL, NULL, NULL);
167 break;
168 default:
169 break;
173 static ssize_t filter_mirror_receive_iov(NetFilterState *nf,
174 NetClientState *sender,
175 unsigned flags,
176 const struct iovec *iov,
177 int iovcnt,
178 NetPacketSent *sent_cb)
180 MirrorState *s = FILTER_MIRROR(nf);
181 int ret;
183 ret = filter_mirror_send(s->chr_out, iov, iovcnt);
184 if (ret) {
185 error_report("filter_mirror_send failed(%s)", strerror(-ret));
189 * we don't hope this error interrupt the normal
190 * path of net packet, so we always return zero.
192 return 0;
195 static ssize_t filter_redirector_receive_iov(NetFilterState *nf,
196 NetClientState *sender,
197 unsigned flags,
198 const struct iovec *iov,
199 int iovcnt,
200 NetPacketSent *sent_cb)
202 MirrorState *s = FILTER_REDIRECTOR(nf);
203 int ret;
205 if (s->chr_out) {
206 ret = filter_mirror_send(s->chr_out, iov, iovcnt);
207 if (ret) {
208 error_report("filter_mirror_send failed(%s)", strerror(-ret));
210 return iov_size(iov, iovcnt);
211 } else {
212 return 0;
216 static void filter_mirror_cleanup(NetFilterState *nf)
218 MirrorState *s = FILTER_MIRROR(nf);
220 if (s->chr_out) {
221 qemu_chr_fe_release(s->chr_out);
225 static void filter_redirector_cleanup(NetFilterState *nf)
227 MirrorState *s = FILTER_REDIRECTOR(nf);
229 if (s->chr_in) {
230 qemu_chr_add_handlers(s->chr_in, NULL, NULL, NULL, NULL);
231 qemu_chr_fe_release(s->chr_in);
233 if (s->chr_out) {
234 qemu_chr_fe_release(s->chr_out);
238 static void filter_mirror_setup(NetFilterState *nf, Error **errp)
240 MirrorState *s = FILTER_MIRROR(nf);
242 if (!s->outdev) {
243 error_setg(errp, "filter filter mirror needs 'outdev' "
244 "property set");
245 return;
248 s->chr_out = qemu_chr_find(s->outdev);
249 if (s->chr_out == NULL) {
250 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
251 "Device '%s' not found", s->outdev);
252 return;
255 if (qemu_chr_fe_claim(s->chr_out) != 0) {
256 error_setg(errp, QERR_DEVICE_IN_USE, s->outdev);
257 return;
261 static void filter_redirector_setup(NetFilterState *nf, Error **errp)
263 MirrorState *s = FILTER_REDIRECTOR(nf);
265 if (!s->indev && !s->outdev) {
266 error_setg(errp, "filter redirector needs 'indev' or "
267 "'outdev' at least one property set");
268 return;
269 } else if (s->indev && s->outdev) {
270 if (!strcmp(s->indev, s->outdev)) {
271 error_setg(errp, "'indev' and 'outdev' could not be same "
272 "for filter redirector");
273 return;
277 s->state = s->index = 0;
279 if (s->indev) {
280 s->chr_in = qemu_chr_find(s->indev);
281 if (s->chr_in == NULL) {
282 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
283 "IN Device '%s' not found", s->indev);
284 return;
287 qemu_chr_fe_claim_no_fail(s->chr_in);
288 qemu_chr_add_handlers(s->chr_in, redirector_chr_can_read,
289 redirector_chr_read, redirector_chr_event, nf);
292 if (s->outdev) {
293 s->chr_out = qemu_chr_find(s->outdev);
294 if (s->chr_out == NULL) {
295 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
296 "OUT Device '%s' not found", s->outdev);
297 return;
299 qemu_chr_fe_claim_no_fail(s->chr_out);
303 static void filter_mirror_class_init(ObjectClass *oc, void *data)
305 NetFilterClass *nfc = NETFILTER_CLASS(oc);
307 nfc->setup = filter_mirror_setup;
308 nfc->cleanup = filter_mirror_cleanup;
309 nfc->receive_iov = filter_mirror_receive_iov;
312 static void filter_redirector_class_init(ObjectClass *oc, void *data)
314 NetFilterClass *nfc = NETFILTER_CLASS(oc);
316 nfc->setup = filter_redirector_setup;
317 nfc->cleanup = filter_redirector_cleanup;
318 nfc->receive_iov = filter_redirector_receive_iov;
321 static char *filter_redirector_get_indev(Object *obj, Error **errp)
323 MirrorState *s = FILTER_REDIRECTOR(obj);
325 return g_strdup(s->indev);
328 static void
329 filter_redirector_set_indev(Object *obj, const char *value, Error **errp)
331 MirrorState *s = FILTER_REDIRECTOR(obj);
333 g_free(s->indev);
334 s->indev = g_strdup(value);
337 static char *filter_mirror_get_outdev(Object *obj, Error **errp)
339 MirrorState *s = FILTER_MIRROR(obj);
341 return g_strdup(s->outdev);
344 static void
345 filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
347 MirrorState *s = FILTER_MIRROR(obj);
349 g_free(s->outdev);
350 s->outdev = g_strdup(value);
351 if (!s->outdev) {
352 error_setg(errp, "filter filter mirror needs 'outdev' "
353 "property set");
354 return;
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
366 filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
368 MirrorState *s = FILTER_REDIRECTOR(obj);
370 g_free(s->outdev);
371 s->outdev = g_strdup(value);
374 static void filter_mirror_init(Object *obj)
376 object_property_add_str(obj, "outdev", filter_mirror_get_outdev,
377 filter_mirror_set_outdev, NULL);
380 static void filter_redirector_init(Object *obj)
382 object_property_add_str(obj, "indev", filter_redirector_get_indev,
383 filter_redirector_set_indev, NULL);
384 object_property_add_str(obj, "outdev", filter_redirector_get_outdev,
385 filter_redirector_set_outdev, NULL);
388 static void filter_mirror_fini(Object *obj)
390 MirrorState *s = FILTER_MIRROR(obj);
392 g_free(s->outdev);
395 static void filter_redirector_fini(Object *obj)
397 MirrorState *s = FILTER_REDIRECTOR(obj);
399 g_free(s->indev);
400 g_free(s->outdev);
403 static const TypeInfo filter_redirector_info = {
404 .name = TYPE_FILTER_REDIRECTOR,
405 .parent = TYPE_NETFILTER,
406 .class_init = filter_redirector_class_init,
407 .instance_init = filter_redirector_init,
408 .instance_finalize = filter_redirector_fini,
409 .instance_size = sizeof(MirrorState),
412 static const TypeInfo filter_mirror_info = {
413 .name = TYPE_FILTER_MIRROR,
414 .parent = TYPE_NETFILTER,
415 .class_init = filter_mirror_class_init,
416 .instance_init = filter_mirror_init,
417 .instance_finalize = filter_mirror_fini,
418 .instance_size = sizeof(MirrorState),
421 static void register_types(void)
423 type_register_static(&filter_mirror_info);
424 type_register_static(&filter_redirector_info);
427 type_init(register_types);