target/riscv: vector integer divide instructions
[qemu/ar7.git] / net / filter-buffer.c
blob93050f86cf34054e6e038a4b3fc62e7cc528b1df
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 #define FILTER_BUFFER(obj) \
22 OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER)
24 typedef struct FilterBufferState {
25 NetFilterState parent_obj;
27 NetQueue *incoming_queue;
28 uint32_t interval;
29 QEMUTimer release_timer;
30 } FilterBufferState;
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
51 * more packets.
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);
59 /* filter APIs */
60 static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
61 NetClientState *sender,
62 unsigned flags,
63 const struct iovec *iov,
64 int iovcnt,
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.
75 * For example:
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,
84 iov, iovcnt, NULL);
85 return iov_size(iov, iovcnt);
88 static void filter_buffer_cleanup(NetFilterState *nf)
90 FilterBufferState *s = FILTER_BUFFER(nf);
92 if (s->interval) {
93 timer_del(&s->release_timer);
96 /* flush packets */
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);
107 if (s->interval) {
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.
124 if (!s->interval) {
125 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
126 "a non-zero interval");
127 return;
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);
138 if (!nf->on) {
139 if (s->interval) {
140 timer_del(&s->release_timer);
142 filter_buffer_flush(nf);
143 } else {
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,
160 Error **errp)
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,
170 Error **errp)
172 FilterBufferState *s = FILTER_BUFFER(obj);
173 Error *local_err = NULL;
174 uint32_t value;
176 visit_type_uint32(v, name, &value, &local_err);
177 if (local_err) {
178 goto out;
180 if (!value) {
181 error_setg(&local_err, "Property '%s.%s' requires a positive value",
182 object_get_typename(obj), name);
183 goto out;
185 s->interval = value;
187 out:
188 error_propagate(errp, local_err);
191 static void filter_buffer_init(Object *obj)
193 object_property_add(obj, "interval", "uint32",
194 filter_buffer_get_interval,
195 filter_buffer_set_interval, NULL, NULL);
198 static const TypeInfo filter_buffer_info = {
199 .name = TYPE_FILTER_BUFFER,
200 .parent = TYPE_NETFILTER,
201 .class_init = filter_buffer_class_init,
202 .instance_init = filter_buffer_init,
203 .instance_size = sizeof(FilterBufferState),
206 static void register_types(void)
208 type_register_static(&filter_buffer_info);
211 type_init(register_types);