]>
Commit | Line | Data |
---|---|---|
7dbb11c8 YH |
1 | /* |
2 | * Copyright (c) 2015 FUJITSU LIMITED | |
3 | * Author: Yang Hongyang <[email protected]> | |
4 | * | |
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 | */ | |
8 | ||
9 | #include "net/filter.h" | |
10 | #include "net/queue.h" | |
11 | #include "qemu-common.h" | |
12 | #include "qemu/timer.h" | |
13 | #include "qemu/iov.h" | |
14 | #include "qapi/qmp/qerror.h" | |
15 | #include "qapi-visit.h" | |
16 | #include "qom/object.h" | |
17 | ||
18 | #define TYPE_FILTER_BUFFER "filter-buffer" | |
19 | ||
20 | #define FILTER_BUFFER(obj) \ | |
21 | OBJECT_CHECK(FilterBufferState, (obj), TYPE_FILTER_BUFFER) | |
22 | ||
23 | typedef struct FilterBufferState { | |
24 | NetFilterState parent_obj; | |
25 | ||
26 | NetQueue *incoming_queue; | |
27 | uint32_t interval; | |
28 | QEMUTimer release_timer; | |
29 | } FilterBufferState; | |
30 | ||
31 | static void filter_buffer_flush(NetFilterState *nf) | |
32 | { | |
33 | FilterBufferState *s = FILTER_BUFFER(nf); | |
34 | ||
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); | |
38 | } | |
39 | } | |
40 | ||
41 | static void filter_buffer_release_timer(void *opaque) | |
42 | { | |
43 | NetFilterState *nf = opaque; | |
44 | FilterBufferState *s = FILTER_BUFFER(nf); | |
45 | ||
46 | /* | |
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. | |
51 | */ | |
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); | |
56 | } | |
57 | ||
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) | |
65 | { | |
66 | FilterBufferState *s = FILTER_BUFFER(nf); | |
67 | ||
68 | /* | |
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. | |
71 | * | |
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 zero). 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). | |
81 | */ | |
82 | qemu_net_queue_append_iov(s->incoming_queue, sender, flags, | |
83 | iov, iovcnt, NULL); | |
84 | return iov_size(iov, iovcnt); | |
85 | } | |
86 | ||
87 | static void filter_buffer_cleanup(NetFilterState *nf) | |
88 | { | |
89 | FilterBufferState *s = FILTER_BUFFER(nf); | |
90 | ||
91 | if (s->interval) { | |
92 | timer_del(&s->release_timer); | |
93 | } | |
94 | ||
95 | /* flush packets */ | |
96 | if (s->incoming_queue) { | |
97 | filter_buffer_flush(nf); | |
98 | g_free(s->incoming_queue); | |
99 | } | |
100 | } | |
101 | ||
102 | static void filter_buffer_setup(NetFilterState *nf, Error **errp) | |
103 | { | |
104 | FilterBufferState *s = FILTER_BUFFER(nf); | |
105 | ||
106 | /* | |
107 | * We may want to accept zero interval when VM FT solutions like MC | |
108 | * or COLO use this filter to release packets on demand. | |
109 | */ | |
110 | if (!s->interval) { | |
111 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval", | |
112 | "a non-zero interval"); | |
113 | return; | |
114 | } | |
115 | ||
116 | s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf); | |
117 | if (s->interval) { | |
118 | timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL, | |
119 | filter_buffer_release_timer, nf); | |
120 | /* Timer armed to fire in s->interval microseconds. */ | |
121 | timer_mod(&s->release_timer, | |
122 | qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval); | |
123 | } | |
124 | } | |
125 | ||
126 | static void filter_buffer_class_init(ObjectClass *oc, void *data) | |
127 | { | |
128 | NetFilterClass *nfc = NETFILTER_CLASS(oc); | |
129 | ||
130 | nfc->setup = filter_buffer_setup; | |
131 | nfc->cleanup = filter_buffer_cleanup; | |
132 | nfc->receive_iov = filter_buffer_receive_iov; | |
133 | } | |
134 | ||
135 | static void filter_buffer_get_interval(Object *obj, Visitor *v, void *opaque, | |
136 | const char *name, Error **errp) | |
137 | { | |
138 | FilterBufferState *s = FILTER_BUFFER(obj); | |
139 | uint32_t value = s->interval; | |
140 | ||
141 | visit_type_uint32(v, &value, name, errp); | |
142 | } | |
143 | ||
144 | static void filter_buffer_set_interval(Object *obj, Visitor *v, void *opaque, | |
145 | const char *name, Error **errp) | |
146 | { | |
147 | FilterBufferState *s = FILTER_BUFFER(obj); | |
148 | Error *local_err = NULL; | |
149 | uint32_t value; | |
150 | ||
151 | visit_type_uint32(v, &value, name, &local_err); | |
152 | if (local_err) { | |
153 | goto out; | |
154 | } | |
155 | if (!value) { | |
156 | error_setg(&local_err, "Property '%s.%s' requires a positive value", | |
157 | object_get_typename(obj), name); | |
158 | goto out; | |
159 | } | |
160 | s->interval = value; | |
161 | ||
162 | out: | |
163 | error_propagate(errp, local_err); | |
164 | } | |
165 | ||
166 | static void filter_buffer_init(Object *obj) | |
167 | { | |
168 | object_property_add(obj, "interval", "int", | |
169 | filter_buffer_get_interval, | |
170 | filter_buffer_set_interval, NULL, NULL, NULL); | |
171 | } | |
172 | ||
173 | static const TypeInfo filter_buffer_info = { | |
174 | .name = TYPE_FILTER_BUFFER, | |
175 | .parent = TYPE_NETFILTER, | |
176 | .class_init = filter_buffer_class_init, | |
177 | .instance_init = filter_buffer_init, | |
178 | .instance_size = sizeof(FilterBufferState), | |
179 | }; | |
180 | ||
181 | static void register_types(void) | |
182 | { | |
183 | type_register_static(&filter_buffer_info); | |
184 | } | |
185 | ||
186 | type_init(register_types); |