kvm: use start + size for memory ranges
[qemu/ar7.git] / net / filter-buffer.c
blob9ce96aaa35466892468ba222a7147d7be70e525e
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 "qemu/osdep.h"
10 #include "net/filter.h"
11 #include "net/queue.h"
12 #include "qapi/error.h"
13 #include "qemu-common.h"
14 #include "qemu/timer.h"
15 #include "qemu/iov.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qapi-visit.h"
18 #include "qom/object.h"
20 #define TYPE_FILTER_BUFFER "filter-buffer"
22 #define FILTER_BUFFER(obj) \
23 OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER)
25 typedef struct FilterBufferState {
26 NetFilterState parent_obj;
28 NetQueue *incoming_queue;
29 uint32_t interval;
30 QEMUTimer release_timer;
31 } FilterBufferState;
33 static void filter_buffer_flush(NetFilterState *nf)
35 FilterBufferState *s = FILTER_BUFFER(nf);
37 if (!qemu_net_queue_flush(s->incoming_queue)) {
38 /* Unable to empty the queue, purge remaining packets */
39 qemu_net_queue_purge(s->incoming_queue, nf->netdev);
43 static void filter_buffer_release_timer(void *opaque)
45 NetFilterState *nf = opaque;
46 FilterBufferState *s = FILTER_BUFFER(nf);
49 * Note: filter_buffer_flush() drops packets that can't be sent
50 * TODO: We should leave them queued. But currently there's no way
51 * for the next filter or receiver to notify us that it can receive
52 * more packets.
54 filter_buffer_flush(nf);
55 /* Timer rearmed to fire again in s->interval microseconds. */
56 timer_mod(&s->release_timer,
57 qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
60 /* filter APIs */
61 static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
62 NetClientState *sender,
63 unsigned flags,
64 const struct iovec *iov,
65 int iovcnt,
66 NetPacketSent *sent_cb)
68 FilterBufferState *s = FILTER_BUFFER(nf);
71 * We return size when buffer a packet, the sender will take it as
72 * a already sent packet, so sent_cb should not be called later.
74 * FIXME: Even if the guest can't receive packets for some reasons,
75 * the filter can still accept packets until its internal queue is full.
76 * For example:
77 * For some reason, receiver could not receive more packets
78 * (.can_receive() returns zero). Without a filter, at most one packet
79 * will be queued in incoming queue and sender's poll will be disabled
80 * unit its sent_cb() was called. With a filter, it will keep receiving
81 * the packets without caring about the receiver. This is suboptimal.
82 * May need more thoughts (e.g keeping sent_cb).
84 qemu_net_queue_append_iov(s->incoming_queue, sender, flags,
85 iov, iovcnt, NULL);
86 return iov_size(iov, iovcnt);
89 static void filter_buffer_cleanup(NetFilterState *nf)
91 FilterBufferState *s = FILTER_BUFFER(nf);
93 if (s->interval) {
94 timer_del(&s->release_timer);
97 /* flush packets */
98 if (s->incoming_queue) {
99 filter_buffer_flush(nf);
100 g_free(s->incoming_queue);
104 static void filter_buffer_setup_timer(NetFilterState *nf)
106 FilterBufferState *s = FILTER_BUFFER(nf);
108 if (s->interval) {
109 timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
110 filter_buffer_release_timer, nf);
111 /* Timer armed to fire in s->interval microseconds. */
112 timer_mod(&s->release_timer,
113 qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
117 static void filter_buffer_setup(NetFilterState *nf, Error **errp)
119 FilterBufferState *s = FILTER_BUFFER(nf);
122 * We may want to accept zero interval when VM FT solutions like MC
123 * or COLO use this filter to release packets on demand.
125 if (!s->interval) {
126 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
127 "a non-zero interval");
128 return;
131 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
132 filter_buffer_setup_timer(nf);
135 static void filter_buffer_status_changed(NetFilterState *nf, Error **errp)
137 FilterBufferState *s = FILTER_BUFFER(nf);
139 if (!nf->on) {
140 if (s->interval) {
141 timer_del(&s->release_timer);
143 filter_buffer_flush(nf);
144 } else {
145 filter_buffer_setup_timer(nf);
149 static void filter_buffer_class_init(ObjectClass *oc, void *data)
151 NetFilterClass *nfc = NETFILTER_CLASS(oc);
153 nfc->setup = filter_buffer_setup;
154 nfc->cleanup = filter_buffer_cleanup;
155 nfc->receive_iov = filter_buffer_receive_iov;
156 nfc->status_changed = filter_buffer_status_changed;
159 static void filter_buffer_get_interval(Object *obj, Visitor *v,
160 const char *name, void *opaque,
161 Error **errp)
163 FilterBufferState *s = FILTER_BUFFER(obj);
164 uint32_t value = s->interval;
166 visit_type_uint32(v, name, &value, errp);
169 static void filter_buffer_set_interval(Object *obj, Visitor *v,
170 const char *name, void *opaque,
171 Error **errp)
173 FilterBufferState *s = FILTER_BUFFER(obj);
174 Error *local_err = NULL;
175 uint32_t value;
177 visit_type_uint32(v, name, &value, &local_err);
178 if (local_err) {
179 goto out;
181 if (!value) {
182 error_setg(&local_err, "Property '%s.%s' requires a positive value",
183 object_get_typename(obj), name);
184 goto out;
186 s->interval = value;
188 out:
189 error_propagate(errp, local_err);
192 static void filter_buffer_init(Object *obj)
194 object_property_add(obj, "interval", "uint32",
195 filter_buffer_get_interval,
196 filter_buffer_set_interval, NULL, NULL, NULL);
199 static const TypeInfo filter_buffer_info = {
200 .name = TYPE_FILTER_BUFFER,
201 .parent = TYPE_NETFILTER,
202 .class_init = filter_buffer_class_init,
203 .instance_init = filter_buffer_init,
204 .instance_size = sizeof(FilterBufferState),
207 static void register_types(void)
209 type_register_static(&filter_buffer_info);
212 type_init(register_types);