xref: /openbmc/qemu/net/filter-buffer.c (revision 8fa3b702)
1 /*
2  * Copyright (c) 2015 FUJITSU LIMITED
3  * Author: Yang Hongyang <yanghy@cn.fujitsu.com>
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 "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"
18 
19 #define TYPE_FILTER_BUFFER "filter-buffer"
20 
21 typedef struct FilterBufferState FilterBufferState;
22 DECLARE_INSTANCE_CHECKER(FilterBufferState, FILTER_BUFFER,
23                          TYPE_FILTER_BUFFER)
24 
25 struct FilterBufferState {
26     NetFilterState parent_obj;
27 
28     NetQueue *incoming_queue;
29     uint32_t interval;
30     QEMUTimer release_timer;
31 };
32 
33 static void filter_buffer_flush(NetFilterState *nf)
34 {
35     FilterBufferState *s = FILTER_BUFFER(nf);
36 
37     if (!qemu_net_queue_flush(s->incoming_queue)) {
38         /* Unable to empty the queue, purge remaining packets */
39         qemu_net_queue_purge(s->incoming_queue, nf->netdev);
40     }
41 }
42 
43 static void filter_buffer_release_timer(void *opaque)
44 {
45     NetFilterState *nf = opaque;
46     FilterBufferState *s = FILTER_BUFFER(nf);
47 
48     /*
49      * Note: filter_buffer_flush() drops packets that can't be sent
50      * TODO: We should leave them queued.  But currently there's no way
51      * for the next filter or receiver to notify us that it can receive
52      * more packets.
53      */
54     filter_buffer_flush(nf);
55     /* Timer rearmed to fire again in s->interval microseconds. */
56     timer_mod(&s->release_timer,
57               qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
58 }
59 
60 /* filter APIs */
61 static ssize_t filter_buffer_receive_iov(NetFilterState *nf,
62                                          NetClientState *sender,
63                                          unsigned flags,
64                                          const struct iovec *iov,
65                                          int iovcnt,
66                                          NetPacketSent *sent_cb)
67 {
68     FilterBufferState *s = FILTER_BUFFER(nf);
69 
70     /*
71      * We return size when buffer a packet, the sender will take it as
72      * a already sent packet, so sent_cb should not be called later.
73      *
74      * FIXME: Even if the guest can't receive packets for some reasons,
75      * the filter can still accept packets until its internal queue is full.
76      * For example:
77      *   For some reason, receiver could not receive more packets
78      * (.can_receive() returns false). Without a filter, at most one packet
79      * will be queued in incoming queue and sender's poll will be disabled
80      * unit its sent_cb() was called. With a filter, it will keep receiving
81      * the packets without caring about the receiver. This is suboptimal.
82      * May need more thoughts (e.g keeping sent_cb).
83      */
84     qemu_net_queue_append_iov(s->incoming_queue, sender, flags,
85                               iov, iovcnt, NULL);
86     return iov_size(iov, iovcnt);
87 }
88 
89 static void filter_buffer_cleanup(NetFilterState *nf)
90 {
91     FilterBufferState *s = FILTER_BUFFER(nf);
92 
93     if (s->interval) {
94         timer_del(&s->release_timer);
95     }
96 
97     /* flush packets */
98     if (s->incoming_queue) {
99         filter_buffer_flush(nf);
100         g_free(s->incoming_queue);
101     }
102 }
103 
104 static void filter_buffer_setup_timer(NetFilterState *nf)
105 {
106     FilterBufferState *s = FILTER_BUFFER(nf);
107 
108     if (s->interval) {
109         timer_init_us(&s->release_timer, QEMU_CLOCK_VIRTUAL,
110                       filter_buffer_release_timer, nf);
111         /* Timer armed to fire in s->interval microseconds. */
112         timer_mod(&s->release_timer,
113                   qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + s->interval);
114     }
115 }
116 
117 static void filter_buffer_setup(NetFilterState *nf, Error **errp)
118 {
119     FilterBufferState *s = FILTER_BUFFER(nf);
120 
121     /*
122      * We may want to accept zero interval when VM FT solutions like MC
123      * or COLO use this filter to release packets on demand.
124      */
125     if (!s->interval) {
126         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "interval",
127                    "a non-zero interval");
128         return;
129     }
130 
131     s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
132     filter_buffer_setup_timer(nf);
133 }
134 
135 static void filter_buffer_status_changed(NetFilterState *nf, Error **errp)
136 {
137     FilterBufferState *s = FILTER_BUFFER(nf);
138 
139     if (!nf->on) {
140         if (s->interval) {
141             timer_del(&s->release_timer);
142         }
143         filter_buffer_flush(nf);
144     } else {
145         filter_buffer_setup_timer(nf);
146     }
147 }
148 
149 static void filter_buffer_class_init(ObjectClass *oc, void *data)
150 {
151     NetFilterClass *nfc = NETFILTER_CLASS(oc);
152 
153     nfc->setup = filter_buffer_setup;
154     nfc->cleanup = filter_buffer_cleanup;
155     nfc->receive_iov = filter_buffer_receive_iov;
156     nfc->status_changed = filter_buffer_status_changed;
157 }
158 
159 static void filter_buffer_get_interval(Object *obj, Visitor *v,
160                                        const char *name, void *opaque,
161                                        Error **errp)
162 {
163     FilterBufferState *s = FILTER_BUFFER(obj);
164     uint32_t value = s->interval;
165 
166     visit_type_uint32(v, name, &value, errp);
167 }
168 
169 static void filter_buffer_set_interval(Object *obj, Visitor *v,
170                                        const char *name, void *opaque,
171                                        Error **errp)
172 {
173     FilterBufferState *s = FILTER_BUFFER(obj);
174     uint32_t value;
175 
176     if (!visit_type_uint32(v, name, &value, errp)) {
177         return;
178     }
179     if (!value) {
180         error_setg(errp, "Property '%s.%s' requires a positive value",
181                    object_get_typename(obj), name);
182         return;
183     }
184     s->interval = value;
185 }
186 
187 static void filter_buffer_init(Object *obj)
188 {
189     object_property_add(obj, "interval", "uint32",
190                         filter_buffer_get_interval,
191                         filter_buffer_set_interval, NULL, NULL);
192 }
193 
194 static const TypeInfo filter_buffer_info = {
195     .name = TYPE_FILTER_BUFFER,
196     .parent = TYPE_NETFILTER,
197     .class_init = filter_buffer_class_init,
198     .instance_init = filter_buffer_init,
199     .instance_size = sizeof(FilterBufferState),
200 };
201 
202 static void register_types(void)
203 {
204     type_register_static(&filter_buffer_info);
205 }
206 
207 type_init(register_types);
208