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