target-arm: cpu: Move cpu_is_big_endian to header
[qemu/ar7.git] / net / filter-buffer.c
blob12ad2e30d4a701226c6e4f98bd6ae31880b14d61
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 "qemu-common.h"
13 #include "qemu/timer.h"
14 #include "qemu/iov.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qapi-visit.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 zero). 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(NetFilterState *nf, Error **errp)
105 FilterBufferState *s = FILTER_BUFFER(nf);
108 * We may want to accept zero interval when VM FT solutions like MC
109 * or COLO use this filter to release packets on demand.
111 if (!s->interval) {
112 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
113 "a non-zero interval");
114 return;
117 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
118 if (s->interval) {
119 timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
120 filter_buffer_release_timer, nf);
121 /* Timer armed to fire in s->interval microseconds. */
122 timer_mod(&s->release_timer,
123 qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
127 static void filter_buffer_class_init(ObjectClass *oc, void *data)
129 NetFilterClass *nfc = NETFILTER_CLASS(oc);
131 nfc->setup = filter_buffer_setup;
132 nfc->cleanup = filter_buffer_cleanup;
133 nfc->receive_iov = filter_buffer_receive_iov;
136 static void filter_buffer_get_interval(Object *obj, Visitor *v,
137 const char *name, void *opaque,
138 Error **errp)
140 FilterBufferState *s = FILTER_BUFFER(obj);
141 uint32_t value = s->interval;
143 visit_type_uint32(v, name, &value, errp);
146 static void filter_buffer_set_interval(Object *obj, Visitor *v,
147 const char *name, void *opaque,
148 Error **errp)
150 FilterBufferState *s = FILTER_BUFFER(obj);
151 Error *local_err = NULL;
152 uint32_t value;
154 visit_type_uint32(v, name, &value, &local_err);
155 if (local_err) {
156 goto out;
158 if (!value) {
159 error_setg(&local_err, "Property '%s.%s' requires a positive value",
160 object_get_typename(obj), name);
161 goto out;
163 s->interval = value;
165 out:
166 error_propagate(errp, local_err);
169 static void filter_buffer_init(Object *obj)
171 object_property_add(obj, "interval", "int",
172 filter_buffer_get_interval,
173 filter_buffer_set_interval, NULL, NULL, NULL);
176 static const TypeInfo filter_buffer_info = {
177 .name = TYPE_FILTER_BUFFER,
178 .parent = TYPE_NETFILTER,
179 .class_init = filter_buffer_class_init,
180 .instance_init = filter_buffer_init,
181 .instance_size = sizeof(FilterBufferState),
184 static void register_types(void)
186 type_register_static(&filter_buffer_info);
189 type_init(register_types);