xref: /qemu/net/filter-buffer.c (revision 667ad26f)
1 /*
2  * Copyright (c) 2015 FUJITSU LIMITED
3  * Author: Yang Hongyang <yanghy@cn.fujitsu.com>
4  *
5  * This work is licensed under the terms of the GNU GPL, version 2 or
6  * later.  See the COPYING file in the top-level directory.
7  */
8 
9 #include "qemu/osdep.h"
10 #include "net/filter.h"
11 #include "net/queue.h"
12 #include "qemu-common.h"
13 #include "qemu/timer.h"
14 #include "qemu/iov.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qapi-visit.h"
17 #include "qom/object.h"
18 
19 #define TYPE_FILTER_BUFFER "filter-buffer"
20 
21 #define FILTER_BUFFER(obj) \
22     OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER)
23 
24 typedef struct FilterBufferState {
25     NetFilterState parent_obj;
26 
27     NetQueue *incoming_queue;
28     uint32_t interval;
29     QEMUTimer release_timer;
30 } FilterBufferState;
31 
32 static void filter_buffer_flush(NetFilterState *nf)
33 {
34     FilterBufferState *s = FILTER_BUFFER(nf);
35 
36     if (!qemu_net_queue_flush(s->incoming_queue)) {
37         /* Unable to empty the queue, purge remaining packets */
38         qemu_net_queue_purge(s->incoming_queue, nf->netdev);
39     }
40 }
41 
42 static void filter_buffer_release_timer(void *opaque)
43 {
44     NetFilterState *nf = opaque;
45     FilterBufferState *s = FILTER_BUFFER(nf);
46 
47     /*
48      * Note: filter_buffer_flush() drops packets that can't be sent
49      * TODO: We should leave them queued.  But currently there's no way
50      * for the next filter or receiver to notify us that it can receive
51      * more packets.
52      */
53     filter_buffer_flush(nf);
54     /* Timer rearmed to fire again in s->interval microseconds. */
55     timer_mod(&s->release_timer,
56               qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
57 }
58 
59 /* filter APIs */
60 static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
61                                          NetClientState *sender,
62                                          unsigned flags,
63                                          const struct iovec *iov,
64                                          int iovcnt,
65                                          NetPacketSent *sent_cb)
66 {
67     FilterBufferState *s = FILTER_BUFFER(nf);
68 
69     /*
70      * We return size when buffer a packet, the sender will take it as
71      * a already sent packet, so sent_cb should not be called later.
72      *
73      * FIXME: Even if the guest can't receive packets for some reasons,
74      * the filter can still accept packets until its internal queue is full.
75      * For example:
76      *   For some reason, receiver could not receive more packets
77      * (.can_receive() returns zero). Without a filter, at most one packet
78      * will be queued in incoming queue and sender's poll will be disabled
79      * unit its sent_cb() was called. With a filter, it will keep receiving
80      * the packets without caring about the receiver. This is suboptimal.
81      * May need more thoughts (e.g keeping sent_cb).
82      */
83     qemu_net_queue_append_iov(s->incoming_queue, sender, flags,
84                               iov, iovcnt, NULL);
85     return iov_size(iov, iovcnt);
86 }
87 
88 static void filter_buffer_cleanup(NetFilterState *nf)
89 {
90     FilterBufferState *s = FILTER_BUFFER(nf);
91 
92     if (s->interval) {
93         timer_del(&s->release_timer);
94     }
95 
96     /* flush packets */
97     if (s->incoming_queue) {
98         filter_buffer_flush(nf);
99         g_free(s->incoming_queue);
100     }
101 }
102 
103 static void filter_buffer_setup(NetFilterState *nf, Error **errp)
104 {
105     FilterBufferState *s = FILTER_BUFFER(nf);
106 
107     /*
108      * We may want to accept zero interval when VM FT solutions like MC
109      * or COLO use this filter to release packets on demand.
110      */
111     if (!s->interval) {
112         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
113                    "a non-zero interval");
114         return;
115     }
116 
117     s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
118     if (s->interval) {
119         timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
120                       filter_buffer_release_timer, nf);
121         /* Timer armed to fire in s->interval microseconds. */
122         timer_mod(&s->release_timer,
123                   qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
124     }
125 }
126 
127 static void filter_buffer_class_init(ObjectClass *oc, void *data)
128 {
129     NetFilterClass *nfc = NETFILTER_CLASS(oc);
130 
131     nfc->setup = filter_buffer_setup;
132     nfc->cleanup = filter_buffer_cleanup;
133     nfc->receive_iov = filter_buffer_receive_iov;
134 }
135 
136 static void filter_buffer_get_interval(Object *obj, Visitor *v,
137                                        const char *name, void *opaque,
138                                        Error **errp)
139 {
140     FilterBufferState *s = FILTER_BUFFER(obj);
141     uint32_t value = s->interval;
142 
143     visit_type_uint32(v, name, &value, errp);
144 }
145 
146 static void filter_buffer_set_interval(Object *obj, Visitor *v,
147                                        const char *name, void *opaque,
148                                        Error **errp)
149 {
150     FilterBufferState *s = FILTER_BUFFER(obj);
151     Error *local_err = NULL;
152     uint32_t value;
153 
154     visit_type_uint32(v, name, &value, &local_err);
155     if (local_err) {
156         goto out;
157     }
158     if (!value) {
159         error_setg(&local_err, "Property '%s.%s' requires a positive value",
160                    object_get_typename(obj), name);
161         goto out;
162     }
163     s->interval = value;
164 
165 out:
166     error_propagate(errp, local_err);
167 }
168 
169 static void filter_buffer_init(Object *obj)
170 {
171     object_property_add(obj, "interval", "int",
172                         filter_buffer_get_interval,
173                         filter_buffer_set_interval, NULL, NULL, NULL);
174 }
175 
176 static const TypeInfo filter_buffer_info = {
177     .name = TYPE_FILTER_BUFFER,
178     .parent = TYPE_NETFILTER,
179     .class_init = filter_buffer_class_init,
180     .instance_init = filter_buffer_init,
181     .instance_size = sizeof(FilterBufferState),
182 };
183 
184 static void register_types(void)
185 {
186     type_register_static(&filter_buffer_info);
187 }
188 
189 type_init(register_types);
190