Merge remote-tracking branch 'remotes/berrange/tags/pull-io-channel-base-2015-12...
[qemu.git] / net / filter-buffer.c
blob57be149413f928c629d1f93472372a9f9c104a72
1 /*
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.
7 */
9 #include "net/filter.h"
10 #include "net/queue.h"
11 #include "qemu-common.h"
12 #include "qemu/timer.h"
13 #include "qemu/iov.h"
14 #include "qapi/qmp/qerror.h"
15 #include "qapi-visit.h"
16 #include "qom/object.h"
18 #define TYPE_FILTER_BUFFER "filter-buffer"
20 #define FILTER_BUFFER(obj) \
21 OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER)
23 typedef struct FilterBufferState {
24 NetFilterState parent_obj;
26 NetQueue *incoming_queue;
27 uint32_t interval;
28 QEMUTimer release_timer;
29 } FilterBufferState;
31 static void filter_buffer_flush(NetFilterState *nf)
33 FilterBufferState *s = FILTER_BUFFER(nf);
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);
41 static void filter_buffer_release_timer(void *opaque)
43 NetFilterState *nf = opaque;
44 FilterBufferState *s = FILTER_BUFFER(nf);
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.
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);
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)
66 FilterBufferState *s = FILTER_BUFFER(nf);
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.
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 zero). 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).
82 qemu_net_queue_append_iov(s->incoming_queue, sender, flags,
83 iov, iovcnt, NULL);
84 return iov_size(iov, iovcnt);
87 static void filter_buffer_cleanup(NetFilterState *nf)
89 FilterBufferState *s = FILTER_BUFFER(nf);
91 if (s->interval) {
92 timer_del(&s->release_timer);
95 /* flush packets */
96 if (s->incoming_queue) {
97 filter_buffer_flush(nf);
98 g_free(s->incoming_queue);
102 static void filter_buffer_setup(NetFilterState *nf, Error **errp)
104 FilterBufferState *s = FILTER_BUFFER(nf);
107 * We may want to accept zero interval when VM FT solutions like MC
108 * or COLO use this filter to release packets on demand.
110 if (!s->interval) {
111 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
112 "a non-zero interval");
113 return;
116 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
117 if (s->interval) {
118 timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
119 filter_buffer_release_timer, nf);
120 /* Timer armed to fire in s->interval microseconds. */
121 timer_mod(&s->release_timer,
122 qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
126 static void filter_buffer_class_init(ObjectClass *oc, void *data)
128 NetFilterClass *nfc = NETFILTER_CLASS(oc);
130 nfc->setup = filter_buffer_setup;
131 nfc->cleanup = filter_buffer_cleanup;
132 nfc->receive_iov = filter_buffer_receive_iov;
135 static void filter_buffer_get_interval(Object *obj, Visitor *v, void *opaque,
136 const char *name, Error **errp)
138 FilterBufferState *s = FILTER_BUFFER(obj);
139 uint32_t value = s->interval;
141 visit_type_uint32(v, &value, name, errp);
144 static void filter_buffer_set_interval(Object *obj, Visitor *v, void *opaque,
145 const char *name, Error **errp)
147 FilterBufferState *s = FILTER_BUFFER(obj);
148 Error *local_err = NULL;
149 uint32_t value;
151 visit_type_uint32(v, &value, name, &local_err);
152 if (local_err) {
153 goto out;
155 if (!value) {
156 error_setg(&local_err, "Property '%s.%s' requires a positive value",
157 object_get_typename(obj), name);
158 goto out;
160 s->interval = value;
162 out:
163 error_propagate(errp, local_err);
166 static void filter_buffer_init(Object *obj)
168 object_property_add(obj, "interval", "int",
169 filter_buffer_get_interval,
170 filter_buffer_set_interval, NULL, NULL, NULL);
173 static const TypeInfo filter_buffer_info = {
174 .name = TYPE_FILTER_BUFFER,
175 .parent = TYPE_NETFILTER,
176 .class_init = filter_buffer_class_init,
177 .instance_init = filter_buffer_init,
178 .instance_size = sizeof(FilterBufferState),
181 static void register_types(void)
183 type_register_static(&filter_buffer_info);
186 type_init(register_types);