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.
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"
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
;
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
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
);
59 static ssize_t
filter_buffer_receive_iov(NetFilterState
*nf
,
60 NetClientState
*sender
,
62 const struct iovec
*iov
,
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.
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
,
84 return iov_size(iov
, iovcnt
);
87 static void filter_buffer_cleanup(NetFilterState
*nf
)
89 FilterBufferState
*s
= FILTER_BUFFER(nf
);
92 timer_del(&s
->release_timer
);
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
);
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.
124 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "interval",
125 "a non-zero interval");
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
);
139 timer_del(&s
->release_timer
);
141 filter_buffer_flush(nf
);
143 filter_buffer_setup_timer(nf
);
147 static void filter_buffer_class_init(ObjectClass
*oc
, void *data
)
149 NetFilterClass
*nfc
= NETFILTER_CLASS(oc
);
151 nfc
->setup
= filter_buffer_setup
;
152 nfc
->cleanup
= filter_buffer_cleanup
;
153 nfc
->receive_iov
= filter_buffer_receive_iov
;
154 nfc
->status_changed
= filter_buffer_status_changed
;
157 static void filter_buffer_get_interval(Object
*obj
, Visitor
*v
,
158 const char *name
, void *opaque
,
161 FilterBufferState
*s
= FILTER_BUFFER(obj
);
162 uint32_t value
= s
->interval
;
164 visit_type_uint32(v
, name
, &value
, errp
);
167 static void filter_buffer_set_interval(Object
*obj
, Visitor
*v
,
168 const char *name
, void *opaque
,
171 FilterBufferState
*s
= FILTER_BUFFER(obj
);
174 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
178 error_setg(errp
, "Property '%s.%s' requires a positive value",
179 object_get_typename(obj
), name
);
185 static void filter_buffer_init(Object
*obj
)
187 object_property_add(obj
, "interval", "uint32",
188 filter_buffer_get_interval
,
189 filter_buffer_set_interval
, NULL
, NULL
);
192 static const TypeInfo filter_buffer_info
= {
193 .name
= TYPE_FILTER_BUFFER
,
194 .parent
= TYPE_NETFILTER
,
195 .class_init
= filter_buffer_class_init
,
196 .instance_init
= filter_buffer_init
,
197 .instance_size
= sizeof(FilterBufferState
),
200 static void register_types(void)
202 type_register_static(&filter_buffer_info
);
205 type_init(register_types
);