2 * Copyright (c) 2015 FUJITSU LIMITED
3 * Author: Yang Hongyang <yanghy@cn.fujitsu.com>
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.
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"
15 #include "qapi/qapi-builtin-visit.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qom/object.h"
19 #define TYPE_FILTER_BUFFER "filter-buffer"
21 #define FILTER_BUFFER(obj) \
22 OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER)
24 typedef struct FilterBufferState {
25 NetFilterState parent_obj;
27 NetQueue *incoming_queue;
29 QEMUTimer release_timer;
32 static void filter_buffer_flush(NetFilterState *nf)
34 FilterBufferState *s = FILTER_BUFFER(nf);
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);
42 static void filter_buffer_release_timer(void *opaque)
44 NetFilterState *nf = opaque;
45 FilterBufferState *s = FILTER_BUFFER(nf);
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
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);
60 static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
61 NetClientState *sender,
63 const struct iovec *iov,
65 NetPacketSent *sent_cb)
67 FilterBufferState *s = FILTER_BUFFER(nf);
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.
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.
76 * For some reason, receiver could not receive more packets
77 * (.can_receive() returns false). 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).
83 qemu_net_queue_append_iov(s->incoming_queue, sender, flags,
85 return iov_size(iov, iovcnt);
88 static void filter_buffer_cleanup(NetFilterState *nf)
90 FilterBufferState *s = FILTER_BUFFER(nf);
93 timer_del(&s->release_timer);
97 if (s->incoming_queue) {
98 filter_buffer_flush(nf);
99 g_free(s->incoming_queue);
103 static void filter_buffer_setup_timer(NetFilterState *nf)
105 FilterBufferState *s = FILTER_BUFFER(nf);
108 timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
109 filter_buffer_release_timer, nf);
110 /* Timer armed to fire in s->interval microseconds. */
111 timer_mod(&s->release_timer,
112 qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
116 static void filter_buffer_setup(NetFilterState *nf, Error **errp)
118 FilterBufferState *s = FILTER_BUFFER(nf);
121 * We may want to accept zero interval when VM FT solutions like MC
122 * or COLO use this filter to release packets on demand.
125 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
126 "a non-zero interval");
130 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
131 filter_buffer_setup_timer(nf);
134 static void filter_buffer_status_changed(NetFilterState *nf, Error **errp)
136 FilterBufferState *s = FILTER_BUFFER(nf);
140 timer_del(&s->release_timer);
142 filter_buffer_flush(nf);
144 filter_buffer_setup_timer(nf);
148 static void filter_buffer_class_init(ObjectClass *oc, void *data)
150 NetFilterClass *nfc = NETFILTER_CLASS(oc);
152 nfc->setup = filter_buffer_setup;
153 nfc->cleanup = filter_buffer_cleanup;
154 nfc->receive_iov = filter_buffer_receive_iov;
155 nfc->status_changed = filter_buffer_status_changed;
158 static void filter_buffer_get_interval(Object *obj, Visitor *v,
159 const char *name, void *opaque,
162 FilterBufferState *s = FILTER_BUFFER(obj);
163 uint32_t value = s->interval;
165 visit_type_uint32(v, name, &value, errp);
168 static void filter_buffer_set_interval(Object *obj, Visitor *v,
169 const char *name, void *opaque,
172 FilterBufferState *s = FILTER_BUFFER(obj);
173 Error *local_err = NULL;
176 if (!visit_type_uint32(v, name, &value, &local_err)) {
180 error_setg(&local_err, "Property '%s.%s' requires a positive value",
181 object_get_typename(obj), name);
187 error_propagate(errp, local_err);
190 static void filter_buffer_init(Object *obj)
192 object_property_add(obj, "interval", "uint32",
193 filter_buffer_get_interval,
194 filter_buffer_set_interval, NULL, NULL);
197 static const TypeInfo filter_buffer_info = {
198 .name = TYPE_FILTER_BUFFER,
199 .parent = TYPE_NETFILTER,
200 .class_init = filter_buffer_class_init,
201 .instance_init = filter_buffer_init,
202 .instance_size = sizeof(FilterBufferState),
205 static void register_types(void)
207 type_register_static(&filter_buffer_info);
210 type_init(register_types);