xref: /openbmc/qemu/net/filter-mirror.c (revision 3f53bc61)
1 /*
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"
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"
26 
27 #define FILTER_MIRROR(obj) \
28     OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_MIRROR)
29 
30 #define FILTER_REDIRECTOR(obj) \
31     OBJECT_CHECK(MirrorState, (obj), TYPE_FILTER_REDIRECTOR)
32 
33 #define TYPE_FILTER_MIRROR "filter-mirror"
34 #define TYPE_FILTER_REDIRECTOR "filter-redirector"
35 #define REDIRECTOR_MAX_LEN NET_BUFSIZE
36 
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;
45 
46 static int filter_mirror_send(CharBackend *chr_out,
47                               const struct iovec *iov,
48                               int iovcnt)
49 {
50     int ret = 0;
51     ssize_t size = 0;
52     uint32_t len = 0;
53     char *buf;
54 
55     size = iov_size(iov, iovcnt);
56     if (!size) {
57         return 0;
58     }
59 
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;
64     }
65 
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;
72     }
73 
74     return 0;
75 
76 err:
77     return ret < 0 ? ret : -EIO;
78 }
79 
80 static void redirector_to_filter(NetFilterState *nf,
81                                  const uint8_t *buf,
82                                  int len)
83 {
84     struct iovec iov = {
85         .iov_base = (void *)buf,
86         .iov_len = len,
87     };
88 
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);
92     }
93 
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);
97      }
98 }
99 
100 static int redirector_chr_can_read(void *opaque)
101 {
102     return REDIRECTOR_MAX_LEN;
103 }
104 
105 static void redirector_chr_read(void *opaque, const uint8_t *buf, int size)
106 {
107     NetFilterState *nf = opaque;
108     MirrorState *s = FILTER_REDIRECTOR(nf);
109     int ret;
110 
111     ret = net_fill_rstate(&s->rs, buf, size);
112 
113     if (ret == -1) {
114         qemu_chr_fe_set_handlers(&s->chr_in, NULL, NULL, NULL,
115                                  NULL, NULL, true);
116     }
117 }
118 
119 static void redirector_chr_event(void *opaque, int event)
120 {
121     NetFilterState *nf = opaque;
122     MirrorState *s = FILTER_REDIRECTOR(nf);
123 
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;
131     }
132 }
133 
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)
140 {
141     MirrorState *s = FILTER_MIRROR(nf);
142     int ret;
143 
144     ret = filter_mirror_send(&s->chr_out, iov, iovcnt);
145     if (ret) {
146         error_report("filter_mirror_send failed(%s)", strerror(-ret));
147     }
148 
149     /*
150      * we don't hope this error interrupt the normal
151      * path of net packet, so we always return zero.
152      */
153     return 0;
154 }
155 
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)
162 {
163     MirrorState *s = FILTER_REDIRECTOR(nf);
164     int ret;
165 
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));
170         }
171         return iov_size(iov, iovcnt);
172     } else {
173         return 0;
174     }
175 }
176 
177 static void filter_mirror_cleanup(NetFilterState *nf)
178 {
179     MirrorState *s = FILTER_MIRROR(nf);
180 
181     qemu_chr_fe_deinit(&s->chr_out);
182 }
183 
184 static void filter_redirector_cleanup(NetFilterState *nf)
185 {
186     MirrorState *s = FILTER_REDIRECTOR(nf);
187 
188     qemu_chr_fe_deinit(&s->chr_in);
189     qemu_chr_fe_deinit(&s->chr_out);
190 }
191 
192 static void filter_mirror_setup(NetFilterState *nf, Error **errp)
193 {
194     MirrorState *s = FILTER_MIRROR(nf);
195     Chardev *chr;
196 
197     if (!s->outdev) {
198         error_setg(errp, "filter mirror needs 'outdev' "
199                    "property set");
200         return;
201     }
202 
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;
208     }
209 
210     qemu_chr_fe_init(&s->chr_out, chr, errp);
211 }
212 
213 static void redirector_rs_finalize(SocketReadState *rs)
214 {
215     MirrorState *s = container_of(rs, MirrorState, rs);
216     NetFilterState *nf = NETFILTER(s);
217 
218     redirector_to_filter(nf, rs->buf, rs->packet_len);
219 }
220 
221 static void filter_redirector_setup(NetFilterState *nf, Error **errp)
222 {
223     MirrorState *s = FILTER_REDIRECTOR(nf);
224     Chardev *chr;
225 
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;
235         }
236     }
237 
238     net_socket_rs_init(&s->rs, redirector_rs_finalize);
239 
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;
246         }
247 
248         if (!qemu_chr_fe_init(&s->chr_in, chr, errp)) {
249             return;
250         }
251 
252         qemu_chr_fe_set_handlers(&s->chr_in, redirector_chr_can_read,
253                                  redirector_chr_read, redirector_chr_event,
254                                  nf, NULL, true);
255     }
256 
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;
263         }
264         if (!qemu_chr_fe_init(&s->chr_out, chr, errp)) {
265             return;
266         }
267     }
268 }
269 
270 static void filter_mirror_class_init(ObjectClass *oc, void *data)
271 {
272     NetFilterClass *nfc = NETFILTER_CLASS(oc);
273 
274     nfc->setup = filter_mirror_setup;
275     nfc->cleanup = filter_mirror_cleanup;
276     nfc->receive_iov = filter_mirror_receive_iov;
277 }
278 
279 static void filter_redirector_class_init(ObjectClass *oc, void *data)
280 {
281     NetFilterClass *nfc = NETFILTER_CLASS(oc);
282 
283     nfc->setup = filter_redirector_setup;
284     nfc->cleanup = filter_redirector_cleanup;
285     nfc->receive_iov = filter_redirector_receive_iov;
286 }
287 
288 static char *filter_redirector_get_indev(Object *obj, Error **errp)
289 {
290     MirrorState *s = FILTER_REDIRECTOR(obj);
291 
292     return g_strdup(s->indev);
293 }
294 
295 static void
296 filter_redirector_set_indev(Object *obj, const char *value, Error **errp)
297 {
298     MirrorState *s = FILTER_REDIRECTOR(obj);
299 
300     g_free(s->indev);
301     s->indev = g_strdup(value);
302 }
303 
304 static char *filter_mirror_get_outdev(Object *obj, Error **errp)
305 {
306     MirrorState *s = FILTER_MIRROR(obj);
307 
308     return g_strdup(s->outdev);
309 }
310 
311 static void
312 filter_mirror_set_outdev(Object *obj, const char *value, Error **errp)
313 {
314     MirrorState *s = FILTER_MIRROR(obj);
315 
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;
322     }
323 }
324 
325 static char *filter_redirector_get_outdev(Object *obj, Error **errp)
326 {
327     MirrorState *s = FILTER_REDIRECTOR(obj);
328 
329     return g_strdup(s->outdev);
330 }
331 
332 static void
333 filter_redirector_set_outdev(Object *obj, const char *value, Error **errp)
334 {
335     MirrorState *s = FILTER_REDIRECTOR(obj);
336 
337     g_free(s->outdev);
338     s->outdev = g_strdup(value);
339 }
340 
341 static void filter_mirror_init(Object *obj)
342 {
343     object_property_add_str(obj, "outdev", filter_mirror_get_outdev,
344                             filter_mirror_set_outdev, NULL);
345 }
346 
347 static void filter_redirector_init(Object *obj)
348 {
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);
353 }
354 
355 static void filter_mirror_fini(Object *obj)
356 {
357     MirrorState *s = FILTER_MIRROR(obj);
358 
359     g_free(s->outdev);
360 }
361 
362 static void filter_redirector_fini(Object *obj)
363 {
364     MirrorState *s = FILTER_REDIRECTOR(obj);
365 
366     g_free(s->indev);
367     g_free(s->outdev);
368 }
369 
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),
377 };
378 
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),
386 };
387 
388 static void register_types(void)
389 {
390     type_register_static(&filter_mirror_info);
391     type_register_static(&filter_redirector_info);
392 }
393 
394 type_init(register_types);
395