blob: 8d36009c5347ac87f83902a1130f90d44a32de61 [file] [log] [blame]
Zhang Chenf6d3afb2016-03-15 15:41:33 +08001/*
2 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
3 * Copyright (c) 2016 FUJITSU LIMITED
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
7 *
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.
10 */
11
12#include "qemu/osdep.h"
13#include "net/filter.h"
14#include "net/net.h"
Zhang Chenf6d3afb2016-03-15 15:41:33 +080015#include "qapi/error.h"
Zhang Chenf6d3afb2016-03-15 15:41:33 +080016#include "qom/object.h"
17#include "qemu/main-loop.h"
18#include "qemu/error-report.h"
19#include "trace.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040020#include "chardev/char-fe.h"
Zhang Chenf6d3afb2016-03-15 15:41:33 +080021#include "qemu/iov.h"
22#include "qemu/sockets.h"
23
24#define FILTER_MIRROR(obj) \
25 OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR)
26
Zhang Chend46f75b2016-03-17 16:16:26 +080027#define FILTER_REDIRECTOR(obj) \
28 OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_REDIRECTOR)
29
Zhang Chenf6d3afb2016-03-15 15:41:33 +080030#define TYPE_FILTER_MIRROR "filter-mirror"
Zhang Chend46f75b2016-03-17 16:16:26 +080031#define TYPE_FILTER_REDIRECTOR "filter-redirector"
32#define REDIRECTOR_MAX_LEN NET_BUFSIZE
Zhang Chenf6d3afb2016-03-15 15:41:33 +080033
34typedef struct MirrorState {
35 NetFilterState parent_obj;
Zhang Chend46f75b2016-03-17 16:16:26 +080036 char *indev;
Zhang Chenf6d3afb2016-03-15 15:41:33 +080037 char *outdev;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +030038 CharBackend chr_in;
39 CharBackend chr_out;
Zhang Chen16a3df42016-05-13 15:35:19 +080040 SocketReadState rs;
Zhang Chene2521f02017-07-04 14:53:48 +080041 bool vnet_hdr;
Zhang Chenf6d3afb2016-03-15 15:41:33 +080042} MirrorState;
43
Zhang Chendc3c5ac2017-07-04 14:53:47 +080044static int filter_send(MirrorState *s,
Zhang Chene05dc4c2017-05-17 10:09:39 +080045 const struct iovec *iov,
46 int iovcnt)
Zhang Chenf6d3afb2016-03-15 15:41:33 +080047{
Zhang Chene2521f02017-07-04 14:53:48 +080048 NetFilterState *nf = NETFILTER(s);
Zhang Chenf6d3afb2016-03-15 15:41:33 +080049 int ret = 0;
50 ssize_t size = 0;
Zhang Chenf0aabd52017-03-02 11:59:30 +080051 uint32_t len = 0;
Zhang Chenf6d3afb2016-03-15 15:41:33 +080052 char *buf;
53
54 size = iov_size(iov, iovcnt);
55 if (!size) {
56 return 0;
57 }
58
59 len = htonl(size);
Zhang Chendc3c5ac2017-07-04 14:53:47 +080060 ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)&len, sizeof(len));
Zhang Chenf6d3afb2016-03-15 15:41:33 +080061 if (ret != sizeof(len)) {
62 goto err;
63 }
64
Zhang Chene2521f02017-07-04 14:53:48 +080065 if (s->vnet_hdr) {
66 /*
67 * If vnet_hdr = on, we send vnet header len to make other
68 * module(like colo-compare) know how to parse net
69 * packet correctly.
70 */
71 ssize_t vnet_hdr_len;
72
73 vnet_hdr_len = nf->netdev->vnet_hdr_len;
74
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)) {
78 goto err;
79 }
80 }
81
Zhang Chenf6d3afb2016-03-15 15:41:33 +080082 buf = g_malloc(size);
83 iov_to_buf(iov, iovcnt, 0, buf, size);
Zhang Chendc3c5ac2017-07-04 14:53:47 +080084 ret = qemu_chr_fe_write_all(&s->chr_out, (uint8_t *)buf, size);
Zhang Chenf6d3afb2016-03-15 15:41:33 +080085 g_free(buf);
86 if (ret != size) {
87 goto err;
88 }
89
90 return 0;
91
92err:
93 return ret < 0 ? ret : -EIO;
94}
95
Zhang Chenf0aabd52017-03-02 11:59:30 +080096static void redirector_to_filter(NetFilterState *nf,
97 const uint8_t *buf,
98 int len)
Zhang Chend46f75b2016-03-17 16:16:26 +080099{
100 struct iovec iov = {
101 .iov_base = (void *)buf,
102 .iov_len = len,
103 };
104
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);
108 }
109
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);
113 }
114}
115
116static int redirector_chr_can_read(void *opaque)
117{
118 return REDIRECTOR_MAX_LEN;
119}
120
121static void redirector_chr_read(void *opaque, const uint8_t *buf, int size)
122{
123 NetFilterState *nf = opaque;
124 MirrorState *s = FILTER_REDIRECTOR(nf);
Zhang Chen16a3df42016-05-13 15:35:19 +0800125 int ret;
Zhang Chend46f75b2016-03-17 16:16:26 +0800126
Zhang Chen16a3df42016-05-13 15:35:19 +0800127 ret = net_fill_rstate(&s->rs, buf, size);
Zhang Chend46f75b2016-03-17 16:16:26 +0800128
Zhang Chen16a3df42016-05-13 15:35:19 +0800129 if (ret == -1) {
Marc-André Lureau39ab61c2016-10-22 12:53:03 +0300130 qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300131 NULL, NULL, NULL, true);
Zhang Chend46f75b2016-03-17 16:16:26 +0800132 }
133}
134
135static void redirector_chr_event(void *opaque, int event)
136{
137 NetFilterState *nf = opaque;
138 MirrorState *s = FILTER_REDIRECTOR(nf);
139
140 switch (event) {
141 case CHR_EVENT_CLOSED:
Marc-André Lureau39ab61c2016-10-22 12:53:03 +0300142 qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300143 NULL, NULL, NULL, true);
Zhang Chend46f75b2016-03-17 16:16:26 +0800144 break;
145 default:
146 break;
147 }
148}
149
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800150static ssize_t filter_mirror_receive_iov(NetFilterState *nf,
151 NetClientState *sender,
152 unsigned flags,
153 const struct iovec *iov,
154 int iovcnt,
155 NetPacketSent *sent_cb)
156{
Zhang Chenba8940d2016-03-15 18:02:51 +0800157 MirrorState *s = FILTER_MIRROR(nf);
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800158 int ret;
159
Zhang Chendc3c5ac2017-07-04 14:53:47 +0800160 ret = filter_send(s, iov, iovcnt);
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800161 if (ret) {
Zhang Chene05dc4c2017-05-17 10:09:39 +0800162 error_report("filter mirror send failed(%s)", strerror(-ret));
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800163 }
164
165 /*
166 * we don't hope this error interrupt the normal
167 * path of net packet, so we always return zero.
168 */
169 return 0;
170}
171
Zhang Chend46f75b2016-03-17 16:16:26 +0800172static ssize_t filter_redirector_receive_iov(NetFilterState *nf,
173 NetClientState *sender,
174 unsigned flags,
175 const struct iovec *iov,
176 int iovcnt,
177 NetPacketSent *sent_cb)
178{
179 MirrorState *s = FILTER_REDIRECTOR(nf);
180 int ret;
181
Anton Nefedov30650702017-07-06 15:08:52 +0300182 if (qemu_chr_fe_backend_connected(&s->chr_out)) {
Zhang Chendc3c5ac2017-07-04 14:53:47 +0800183 ret = filter_send(s, iov, iovcnt);
Zhang Chend46f75b2016-03-17 16:16:26 +0800184 if (ret) {
Zhang Chene05dc4c2017-05-17 10:09:39 +0800185 error_report("filter redirector send failed(%s)", strerror(-ret));
Zhang Chend46f75b2016-03-17 16:16:26 +0800186 }
187 return iov_size(iov, iovcnt);
188 } else {
189 return 0;
190 }
191}
192
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800193static void filter_mirror_cleanup(NetFilterState *nf)
194{
195 MirrorState *s = FILTER_MIRROR(nf);
196
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400197 qemu_chr_fe_deinit(&s->chr_out, false);
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800198}
199
Zhang Chend46f75b2016-03-17 16:16:26 +0800200static void filter_redirector_cleanup(NetFilterState *nf)
201{
202 MirrorState *s = FILTER_REDIRECTOR(nf);
203
Marc-André Lureau1ce26102017-01-27 00:49:13 +0400204 qemu_chr_fe_deinit(&s->chr_in, false);
205 qemu_chr_fe_deinit(&s->chr_out, false);
Zhang Chend46f75b2016-03-17 16:16:26 +0800206}
207
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800208static void filter_mirror_setup(NetFilterState *nf, Error **errp)
209{
210 MirrorState *s = FILTER_MIRROR(nf);
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300211 Chardev *chr;
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800212
Eduardo Otuboaa1530d2017-09-29 14:03:39 +0200213 if (s->outdev == NULL) {
214 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, "filter-mirror parameter"\
215 " 'outdev' cannot be empty");
216 return;
217 }
218
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300219 chr = qemu_chr_find(s->outdev);
220 if (chr == NULL) {
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800221 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
222 "Device '%s' not found", s->outdev);
223 return;
224 }
225
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300226 qemu_chr_fe_init(&s->chr_out, chr, errp);
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800227}
228
Zhang Chen16a3df42016-05-13 15:35:19 +0800229static void redirector_rs_finalize(SocketReadState *rs)
230{
231 MirrorState *s = container_of(rs, MirrorState, rs);
232 NetFilterState *nf = NETFILTER(s);
233
234 redirector_to_filter(nf, rs->buf, rs->packet_len);
235}
236
Zhang Chend46f75b2016-03-17 16:16:26 +0800237static void filter_redirector_setup(NetFilterState *nf, Error **errp)
238{
239 MirrorState *s = FILTER_REDIRECTOR(nf);
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300240 Chardev *chr;
Zhang Chend46f75b2016-03-17 16:16:26 +0800241
242 if (!s->indev && !s->outdev) {
243 error_setg(errp, "filter redirector needs 'indev' or "
244 "'outdev' at least one property set");
245 return;
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");
250 return;
251 }
252 }
253
Zhang Chene2521f02017-07-04 14:53:48 +0800254 net_socket_rs_init(&s->rs, redirector_rs_finalize, s->vnet_hdr);
Zhang Chend46f75b2016-03-17 16:16:26 +0800255
256 if (s->indev) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300257 chr = qemu_chr_find(s->indev);
258 if (chr == NULL) {
Zhang Chend46f75b2016-03-17 16:16:26 +0800259 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
260 "IN Device '%s' not found", s->indev);
261 return;
262 }
263
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300264 if (!qemu_chr_fe_init(&s->chr_in, chr, errp)) {
265 return;
266 }
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300267
268 qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
269 redirector_chr_read, redirector_chr_event,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300270 NULL, nf, NULL, true);
Zhang Chend46f75b2016-03-17 16:16:26 +0800271 }
272
273 if (s->outdev) {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300274 chr = qemu_chr_find(s->outdev);
275 if (chr == NULL) {
Zhang Chend46f75b2016-03-17 16:16:26 +0800276 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
277 "OUT Device '%s' not found", s->outdev);
278 return;
279 }
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300280 if (!qemu_chr_fe_init(&s->chr_out, chr, errp)) {
281 return;
282 }
Zhang Chend46f75b2016-03-17 16:16:26 +0800283 }
284}
285
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800286static void filter_mirror_class_init(ObjectClass *oc, void *data)
287{
288 NetFilterClass *nfc = NETFILTER_CLASS(oc);
289
290 nfc->setup = filter_mirror_setup;
291 nfc->cleanup = filter_mirror_cleanup;
292 nfc->receive_iov = filter_mirror_receive_iov;
293}
294
Zhang Chend46f75b2016-03-17 16:16:26 +0800295static void filter_redirector_class_init(ObjectClass *oc, void *data)
296{
297 NetFilterClass *nfc = NETFILTER_CLASS(oc);
298
299 nfc->setup = filter_redirector_setup;
300 nfc->cleanup = filter_redirector_cleanup;
301 nfc->receive_iov = filter_redirector_receive_iov;
302}
303
304static char *filter_redirector_get_indev(Object *obj, Error **errp)
305{
306 MirrorState *s = FILTER_REDIRECTOR(obj);
307
308 return g_strdup(s->indev);
309}
310
Zhang Chene05dc4c2017-05-17 10:09:39 +0800311static void filter_redirector_set_indev(Object *obj,
312 const char *value,
313 Error **errp)
Zhang Chend46f75b2016-03-17 16:16:26 +0800314{
315 MirrorState *s = FILTER_REDIRECTOR(obj);
316
317 g_free(s->indev);
318 s->indev = g_strdup(value);
319}
320
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800321static char *filter_mirror_get_outdev(Object *obj, Error **errp)
322{
323 MirrorState *s = FILTER_MIRROR(obj);
324
325 return g_strdup(s->outdev);
326}
327
Zhang Chene05dc4c2017-05-17 10:09:39 +0800328static void filter_mirror_set_outdev(Object *obj,
329 const char *value,
330 Error **errp)
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800331{
332 MirrorState *s = FILTER_MIRROR(obj);
333
334 g_free(s->outdev);
335 s->outdev = g_strdup(value);
336 if (!s->outdev) {
Zhang Chen52cfcb42016-09-30 10:27:58 +0800337 error_setg(errp, "filter mirror needs 'outdev' "
Zhang Chend46f75b2016-03-17 16:16:26 +0800338 "property set");
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800339 return;
340 }
341}
342
Zhang Chene2521f02017-07-04 14:53:48 +0800343static bool filter_mirror_get_vnet_hdr(Object *obj, Error **errp)
344{
345 MirrorState *s = FILTER_MIRROR(obj);
346
347 return s->vnet_hdr;
348}
349
350static void filter_mirror_set_vnet_hdr(Object *obj, bool value, Error **errp)
351{
352 MirrorState *s = FILTER_MIRROR(obj);
353
354 s->vnet_hdr = value;
355}
356
Zhang Chend46f75b2016-03-17 16:16:26 +0800357static char *filter_redirector_get_outdev(Object *obj, Error **errp)
358{
359 MirrorState *s = FILTER_REDIRECTOR(obj);
360
361 return g_strdup(s->outdev);
362}
363
Zhang Chene05dc4c2017-05-17 10:09:39 +0800364static void filter_redirector_set_outdev(Object *obj,
365 const char *value,
366 Error **errp)
Zhang Chend46f75b2016-03-17 16:16:26 +0800367{
368 MirrorState *s = FILTER_REDIRECTOR(obj);
369
370 g_free(s->outdev);
371 s->outdev = g_strdup(value);
372}
373
Zhang Chen00d5c242017-07-04 14:53:49 +0800374static bool filter_redirector_get_vnet_hdr(Object *obj, Error **errp)
375{
376 MirrorState *s = FILTER_REDIRECTOR(obj);
377
378 return s->vnet_hdr;
379}
380
381static void filter_redirector_set_vnet_hdr(Object *obj,
382 bool value,
383 Error **errp)
384{
385 MirrorState *s = FILTER_REDIRECTOR(obj);
386
387 s->vnet_hdr = value;
388}
389
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800390static void filter_mirror_init(Object *obj)
391{
Zhang Chene2521f02017-07-04 14:53:48 +0800392 MirrorState *s = FILTER_MIRROR(obj);
393
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800394 object_property_add_str(obj, "outdev", filter_mirror_get_outdev,
395 filter_mirror_set_outdev, NULL);
Zhang Chene2521f02017-07-04 14:53:48 +0800396
397 s->vnet_hdr = false;
398 object_property_add_bool(obj, "vnet_hdr_support",
399 filter_mirror_get_vnet_hdr,
400 filter_mirror_set_vnet_hdr, NULL);
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800401}
402
Zhang Chend46f75b2016-03-17 16:16:26 +0800403static void filter_redirector_init(Object *obj)
404{
Zhang Chen00d5c242017-07-04 14:53:49 +0800405 MirrorState *s = FILTER_REDIRECTOR(obj);
406
Zhang Chend46f75b2016-03-17 16:16:26 +0800407 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);
Zhang Chen00d5c242017-07-04 14:53:49 +0800411
412 s->vnet_hdr = false;
413 object_property_add_bool(obj, "vnet_hdr_support",
414 filter_redirector_get_vnet_hdr,
415 filter_redirector_set_vnet_hdr, NULL);
Zhang Chend46f75b2016-03-17 16:16:26 +0800416}
417
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800418static void filter_mirror_fini(Object *obj)
419{
420 MirrorState *s = FILTER_MIRROR(obj);
421
422 g_free(s->outdev);
423}
424
Zhang Chend46f75b2016-03-17 16:16:26 +0800425static void filter_redirector_fini(Object *obj)
426{
427 MirrorState *s = FILTER_REDIRECTOR(obj);
428
429 g_free(s->indev);
430 g_free(s->outdev);
431}
432
433static 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),
440};
441
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800442static 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),
449};
450
451static void register_types(void)
452{
453 type_register_static(&filter_mirror_info);
Zhang Chend46f75b2016-03-17 16:16:26 +0800454 type_register_static(&filter_redirector_info);
Zhang Chenf6d3afb2016-03-15 15:41:33 +0800455}
456
457type_init(register_types);