chardev/char.c: Move object_property_try_add_child out of chardev_new
[qemu/kevin.git] / net / filter-buffer.c
blob283dc9cbe6b6e0a9d7ba9a9158296545ff629aed
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/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"
19 #define TYPE_FILTER_BUFFER "filter-buffer"
21 OBJECT_DECLARE_SIMPLE_TYPE(FilterBufferState, FILTER_BUFFER)
23 struct FilterBufferState {
24 NetFilterState parent_obj;
26 NetQueue *incoming_queue;
27 uint32_t interval;
28 QEMUTimer release_timer;
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 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).
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_timer(NetFilterState *nf)
104 FilterBufferState *s = FILTER_BUFFER(nf);
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);
115 static void filter_buffer_setup(NetFilterState *nf, Error **errp)
117 FilterBufferState *s = FILTER_BUFFER(nf);
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.
123 if (!s->interval) {
124 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
125 "a non-zero interval");
126 return;
129 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
130 filter_buffer_setup_timer(nf);
133 static void filter_buffer_status_changed(NetFilterState *nf, Error **errp)
135 FilterBufferState *s = FILTER_BUFFER(nf);
137 if (!nf->on) {
138 if (s->interval) {
139 timer_del(&s->release_timer);
141 filter_buffer_flush(nf);
142 } else {
143 filter_buffer_setup_timer(nf);
147 static void filter_buffer_get_interval(Object *obj, Visitor *v,
148 const char *name, void *opaque,
149 Error **errp)
151 FilterBufferState *s = FILTER_BUFFER(obj);
152 uint32_t value = s->interval;
154 visit_type_uint32(v, name, &value, errp);
157 static void filter_buffer_set_interval(Object *obj, Visitor *v,
158 const char *name, void *opaque,
159 Error **errp)
161 FilterBufferState *s = FILTER_BUFFER(obj);
162 uint32_t value;
164 if (!visit_type_uint32(v, name, &value, errp)) {
165 return;
167 if (!value) {
168 error_setg(errp, "Property '%s.%s' requires a positive value",
169 object_get_typename(obj), name);
170 return;
172 s->interval = value;
175 static void filter_buffer_class_init(ObjectClass *oc, void *data)
177 NetFilterClass *nfc = NETFILTER_CLASS(oc);
179 object_class_property_add(oc, "interval", "uint32",
180 filter_buffer_get_interval,
181 filter_buffer_set_interval, NULL, NULL);
183 nfc->setup = filter_buffer_setup;
184 nfc->cleanup = filter_buffer_cleanup;
185 nfc->receive_iov = filter_buffer_receive_iov;
186 nfc->status_changed = filter_buffer_status_changed;
189 static const TypeInfo filter_buffer_info = {
190 .name = TYPE_FILTER_BUFFER,
191 .parent = TYPE_NETFILTER,
192 .class_init = filter_buffer_class_init,
193 .instance_size = sizeof(FilterBufferState),
196 static void register_types(void)
198 type_register_static(&filter_buffer_info);
201 type_init(register_types);